Documentation ¶
Index ¶
- Variables
- type Client
- func (c *Client) Close() error
- func (c *Client) FetchChunks(ctx context.Context, query string, chunkSize int, args ...interface{}) ([][]Row, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (c *Client) Type() DatabaseType
- type Config
- type DatabaseClient
- type DatabaseType
- type Row
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidConfig = fmt.Errorf("invalid configuration provided") ErrNilConnection = fmt.Errorf("database connection is nil") ErrInvalidChunkSize = fmt.Errorf("chunk size must be positive") )
Common errors that can be returned by the package
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client implements the DatabaseClient interface
func NewClient ¶
NewClient creates a new instance of the Client type based on the given configuration. It creates a connection to the database and verifies it by pinging it. If the configuration is invalid, the connection can't be established or the ping fails, it returns an error. Otherwise, it returns a new instance of the Client type.
func (*Client) FetchChunks ¶
func (c *Client) FetchChunks(ctx context.Context, query string, chunkSize int, args ...interface{}) ([][]Row, error)
FetchChunks executes a query on the database with the given arguments and returns the result as a slice of slices of maps. Each inner slice represents a chunk of data and each map represents a row in the chunk. The size of each chunk is determined by the chunkSize parameter. If the query fails, an error is returned.
func (*Client) Ping ¶
Ping checks if the connection to the database is still alive, establishing a connection if necessary. If the connection is alive, the method returns nil. Otherwise, an error is returned.
If the database connection is nil, it returns ErrNilConnection.
func (*Client) Type ¶
func (c *Client) Type() DatabaseType
type Config ¶
type Config struct { Type DatabaseType Host string Port int User string Password string Database string SSLMode string MaxOpenConns int MaxIdleConns int ConnTimeout time.Duration // // Connection timeout duration // Optional, Additional database-specific options can be added here Options map[string]string }
Config encapsulates all configuration parameters needed to establish a connection with a Redshift database. It provides validation and connection string generation capabilities.
func NewDefaultConfig ¶
func NewDefaultConfig(dbType DatabaseType) *Config
NewDefaultConfig creates a new Config instance with default values. These values can be overridden before passing to the NewClient function.
Default values: - Port: 5439 - SSLMode: disable - MaxOpenConns: 10 - MaxIdleConns: 5 - ConnTimeout: 30 seconds
func (*Config) DNS ¶
DNS generates a connection string in DNS format (i.e. postgres://user:pass@host:port/dbname?sslmode=mode) from the given configuration.
func (*Config) Validate ¶
Validate checks if the configuration is valid.
Validation rules: - Host must not be empty - Port must be a positive integer - User must not be empty - Database must not be empty - MaxOpenConns must be >= MaxIdleConns
Returns an error if the configuration is invalid. Otherwise returns nil. Validate checks if the configuration is valid
type DatabaseClient ¶
type DatabaseClient interface { // Query executes a query on the database with the given arguments and // returns the result as a *sql.Rows. sql.Rows its cursor starts before // the first row. If the query fails, an error is returned. Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) // FetchChunks executes a query on the database with the given arguments // and returns the result as a slice of slices of maps. Each inner slice // represents a chunk of data and each map represents a row in the chunk. // The size of each chunk is determined by the chunkSize parameter. If the // query fails, an error is returned. FetchChunks(ctx context.Context, query string, chunkSize int, args ...interface{}) ([][]Row, error) // Close closes the connection to the database. Close() error // Ping checks if the connection to the database is still alive, // establishing a connection if necessary. If the connection is alive, // the method returns nil. Otherwise, an error is returned. Ping(ctx context.Context) error // Type returns the type of the database client Type() DatabaseType }
DatabaseClient is an interface that encapsulates the necessary methods to interact with a database. Specifically, it provides methods to query the database, fetch chunks of data, close the connection and ping the connection.
type DatabaseType ¶
type DatabaseType string
DatabaseType represents the type of database to connect to.
const ( // Supported Database Types TypePostgres DatabaseType = "postgres" TypeRedshift DatabaseType = "redshift" )
Default Configuration Values