db

package
v0.0.0-...-43207eb Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	MaxDBConnIdleTime = 10 * time.Second
	MaxOpenDBConns    = 30
)

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, databaseURL string, direction migrate.MigrationDirection, count int) (int, error)

func RunInTransaction

func RunInTransaction(ctx context.Context, dbConnectionPool ConnectionPool, opts *sql.TxOptions, atomicFunction func(dbTx Transaction) error) error

RunInTransaction runs the given atomic function in an atomic database transaction and returns an error. Boilerplate code for database transactions.

func RunInTransactionWithResult

func RunInTransactionWithResult[T any](ctx context.Context, dbConnectionPool ConnectionPool, opts *sql.TxOptions, atomicFunction func(dbTx Transaction) (T, error)) (result T, err error)

RunInTransactionWithResult runs the given atomic function in an atomic database transaction and returns a result and an error. Boilerplate code for database transactions.

Types

type ConnectionPool

type ConnectionPool interface {
	SQLExecuter
	BeginTxx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
	Close() error
	Ping(ctx context.Context) error
	SqlDB(ctx context.Context) (*sql.DB, error)
	SqlxDB(ctx context.Context) (*sqlx.DB, error)
}

func OpenDBConnectionPool

func OpenDBConnectionPool(dataSourceName string) (ConnectionPool, error)

type ConnectionPoolImplementation

type ConnectionPoolImplementation struct {
	*sqlx.DB
}

func (*ConnectionPoolImplementation) BeginTxx

func (*ConnectionPoolImplementation) Ping

func (*ConnectionPoolImplementation) SqlDB

func (*ConnectionPoolImplementation) SqlxDB

type SQLExecuter

type SQLExecuter interface {
	DriverName() string
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
	sqlx.PreparerContext
	sqlx.QueryerContext
	Rebind(query string) string
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
}

SQLExecuter is an interface that wraps the *sqlx.DB and *sqlx.Tx structs methods.

type Transaction

type Transaction interface {
	SQLExecuter
	Rollback() error
	Commit() error
}

Transaction is an interface that wraps the sqlx.Tx structs methods.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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