database

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2025 License: GPL-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

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

View Source
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),
	}
)
View Source
var ErrDuplicatePrimaryKey = errors.New("primary key constraint violated")

ErrDuplicatePrimaryKey is returned when an insert operation fails because the primary key already exists.

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is an error that occurs when a statement does not return any rows.

View Source
var ErrOperationFailed = errors.New("operation failed")

ErrOperationFailed is a fallback error for when an operation fails.

View Source
var ErrUniqueConstraint = errors.New("unique constraint violated")

ErrUniqueConstraint is an error that occurs when a unique constraint is violated.

Functions

func MigrateDatabase

func MigrateDatabase(db *sql.DB, cfg Config) error

MigrateDatabase migrates the database to the latest version.

func NewConnection

func NewConnection(cfg Config) (*sql.DB, error)

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

func NewTestDB() (*sql.DB, error)

NewDB creates a new in-memory SQLite database with the necessary schema for testing purposes.

func Transactionally

func Transactionally(ctx context.Context, db *sql.DB, f func(tx *sql.Tx) error) (err error)

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 Driver

type Driver int32

type ErrorHandler

type ErrorHandler interface {
	HandleError(ctx context.Context, err error) error
}

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

Jump to

Keyboard shortcuts

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