db

package
v0.0.0-...-a4dcf50 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func NewClient(cfg *Config, logger *log.Logger) (*Client, error)

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) Close

func (c *Client) Close() error

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

func (c *Client) Ping(ctx context.Context) 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.

If the database connection is nil, it returns ErrNilConnection.

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

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

func (c *Config) DNS() string

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

func (c *Config) Validate() error

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

type Row

type Row map[string]interface{}

Row represents a single database row as a map of column names to values. Values can be of any type supported by the database driver.

Jump to

Keyboard shortcuts

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