Documentation
¶
Index ¶
- Constants
- Variables
- func MigrateDatabase(db *sql.DB, cfg Config) error
- func NewConnection(cfg Config) (*sql.DB, error)
- func NewTestDB() (*sql.DB, error)
- func Transactionally(ctx context.Context, db *sql.DB, f func(tx *sql.Tx) error) (err error)
- type Config
- type Driver
- type ErrorHandler
- type PostgresErrorHandler
- type SqliteErrorHandler
Constants ¶
const ( ArgDriver = "database.driver" ArgDSN = "database.dsn" ArgMaxConnections = "database.max_connections" ArgMaxIdleConnections = "database.max_idle_connections" DriverUnrecognized Driver = -1 DriverUnspecified Driver = iota DriverPostgres DriverSqlite MaxConnectionsDefault = 5 MaxIdleConnectionsDefault = 1 )
Variables ¶
var ( DriverName = map[int32]string{ int32(DriverUnspecified): "unspecified", int32(DriverPostgres): "postgres", int32(DriverSqlite): "sqlite3", } DriverValue = map[string]int32{ "unspecified": int32(DriverUnspecified), "postgres": int32(DriverPostgres), "sqlite3": int32(DriverSqlite), } )
var ErrDuplicatePrimaryKey = errors.New("primary key constraint violated")
ErrDuplicatePrimaryKey is returned when an insert operation fails because the primary key already exists.
var ErrNotFound = errors.New("not found")
ErrNotFound is an error that occurs when a statement does not return any rows.
var ErrOperationFailed = errors.New("operation failed")
ErrOperationFailed is a fallback error for when an operation fails.
var ErrUniqueConstraint = errors.New("unique constraint violated")
ErrUniqueConstraint is an error that occurs when a unique constraint is violated.
Functions ¶
func MigrateDatabase ¶
MigrateDatabase migrates the database to the latest version.
func NewConnection ¶
NewConnection creates a new database connection using the provided configuration. It returns a pointer to the sql.DB object and an error, if any occurred during the connection process. The sql.DB object represents a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.
func NewTestDB ¶
NewDB creates a new in-memory SQLite database with the necessary schema for testing purposes.
func Transactionally ¶
Transactionally executes a function within a database transaction. It commits the transaction if the function succeeds, otherwise it rolls back. If rollback fails, both errors are returned.
Types ¶
type Config ¶
type Config struct {
// Driver is the name of the database driver.
Driver string `mapstructure:"driver"`
// DSN is the Data Source Name. It specifies the username, password, and database name
// that are used to connect to the database.
DSN string `mapstructure:"dsn"`
// MaxConnection is the maximum number of connections that can be opened to the database.
MaxConnections int `mapstructure:"max_connections"`
// MaxIdleConnections is the maximum number of idle connections that can be maintained.
// Idle connections are connections that are open but not in use.
MaxIdleConnections int `mapstructure:"max_idle_connections"`
}
Config represents the configuration for a database connection.
type ErrorHandler ¶
ErrorHandler is an interface for handling database errors. Implementations of this interface should handle database-driver specific errors.
func NewErrorHandler ¶
func NewErrorHandler(cfg Config) (ErrorHandler, error)
NewErrorHandler creates a new ErrorHandler based on the provided configuration.
type PostgresErrorHandler ¶
type PostgresErrorHandler struct{}
func (*PostgresErrorHandler) HandleError ¶
func (e *PostgresErrorHandler) HandleError(_ context.Context, err error) error
type SqliteErrorHandler ¶
type SqliteErrorHandler struct{}
func NewSqliteErrorHandler ¶
func NewSqliteErrorHandler() *SqliteErrorHandler
func (*SqliteErrorHandler) HandleError ¶
func (e *SqliteErrorHandler) HandleError(_ context.Context, err error) error