database

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrConnectionClosed = fmt.Errorf("connection is closed")

ErrConnectionClosed is returned when an operation is attempted on a closed connection.

View Source
var ErrPoolClosed = fmt.Errorf("pool is closed")

ErrPoolClosed is returned when an operation is attempted on a closed pool.

Functions

This section is empty.

Types

type Connection

type Connection interface {

	// Query executes a SQL query against Firebolt and returns the result.
	// The sql parameter can be any valid Firebolt SQL statement, including
	// SELECT, INSERT, UPDATE, DELETE, etc.
	Query(ctx context.Context, sql string, args ...any) ([]map[string]any, error)
}

Connection defines the methods for interacting with Firebolt via SQL.

It provides a standardized interface to execute queries against a Firebolt database instance while abstracting away the underlying implementation details of the connection.

type ConnectionCloser

type ConnectionCloser interface {
	Connection

	// Close terminates the underlying database connection and releases
	// all associated resources.
	Close()
}

ConnectionCloser extends the Connection interface to include a method for closing the connection.

func NewConnection

func NewConnection(
	logger *slog.Logger,
	params DSNProvider,
) (ConnectionCloser, func(), error)

NewConnection creates a new Firebolt connection using the provided parameters. It returns a ConnectionCloser interface, a function to close the connection, and an error if any occurred during the connection setup.

type ConnectionParams

type ConnectionParams struct {
	ClientID     string
	ClientSecret string
	AccountName  string
	DatabaseName *string
	EngineName   *string
}

ConnectionParams holds the parameters required to establish a connection to Firebolt. It contains authentication credentials, account information, and optional database and engine specifications.

func (ConnectionParams) DSN

func (c ConnectionParams) DSN() string

DSN returns the Data Source Name expected by the Firebolt Go SDK. This is a connection string that contains all the necessary information to connect to the Firebolt database. The format follows the pattern: firebolt://database?account_name=[account]&client_id=[id]&client_secret=[secret]&engine=[engine]

func (ConnectionParams) DriverName

func (c ConnectionParams) DriverName() string

DriverName returns the name of the database driver. In this case, it returns "firebolt", which is the driver name used by the Firebolt Go SDK.

func (ConnectionParams) Hash

func (c ConnectionParams) Hash() string

Hash returns a SHA-512/256 hash of the connection parameters. This is useful for caching connections or comparing parameter sets without exposing sensitive information. The hash is computed from the full DSN string and returned as a hex-encoded string.

func (ConnectionParams) String

func (c ConnectionParams) String() string

String returns a string representation of the connection parameters. The format is similar to a DSN, but sensitive information (ClientID and ClientSecret) is masked, making this string safe to log or display.

type DSNProvider

type DSNProvider interface {

	// DriverName returns the name of the database driver.
	DriverName() string

	// DSN returns the Data Source Name (DSN) for the connection.
	DSN() string
}

DSNProvider defines the interface for providing a Data Source Name (DSN). The only DSNProvider implementation is ConnectionParams, but this interface allows to write mock implementations for testing purposes.

type NewConnectionFunc

type NewConnectionFunc func(*slog.Logger, DSNProvider) (ConnectionCloser, func(), error)

NewConnectionFunc defines a function type for creating new connections.

type Pool

type Pool interface {

	// GetConnection returns a connection for the specified parameters.
	// It first checks if a matching connection exists in the pool.
	// If not found, it creates a new connection, adds it to the pool, and returns it.
	GetConnection(params PoolParams) (Connection, error)

	// Close closes all connections in the pool and releases associated resources.
	Close()
}

Pool implements a thread-safe connection pool for Firebolt database connections.

This MCP server is designed to provide access to multiple Firebolt accounts and databases simultaneously, which the current identity has access to. Since the Firebolt Go SDK creates connections to a single account only, we need to maintain a pool of connections to different accounts.

The implementation creates a pool of connections for different combinations of: - Accounts - Databases - Engines

This approach helps LLMs switch between different databases and engines with fewer mistakes, trading some connection efficiency for improved reliability.

func NewPool

func NewPool(logger *slog.Logger, clientID, clientSecret string) (Pool, func())

NewPool creates a new connection pool with the provided logger. It returns the pool implementation and a function to close all connections.

func NewPoolWithConnectionFactory

func NewPoolWithConnectionFactory(
	logger *slog.Logger,
	clientID, clientSecret string,
	newConnectionFunc NewConnectionFunc,
) (Pool, func())

NewPoolWithConnectionFactory creates a new connection pool with a custom connection factory. It allows you to provide a custom function to create connections which can be useful for testing.

type PoolParams

type PoolParams struct {
	AccountName  string
	DatabaseName *string
	EngineName   *string
}

PoolParams holds the parameters required to create a connection in the pool. The pool will convert these parameters into a ConnectionParams struct by adding the ClientID and ClientSecret.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL