db

package
v0.0.0-...-4c11209 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMaxOpenConnections = 30

DefaultMaxOpenConnections the default value for max open connections in the PostgreSQL connection pool.

Variables

View Source
var (
	Global *sql.DB
)

Functions

func DoMigrate

func DoMigrate(m *migrate.Migrate) (err error)

DoMigrate do sql migrations.

func NewDB

func NewDB(cfg *conf.Config) (*sql.DB, error)

NewDB create a new DB pool.

func NewMigrate

func NewMigrate(db *sql.DB) *migrate.Migrate

NewMigrate load migrations.

func Open

func Open(dataSource string) (*sql.DB, error)

Open creates a new DB handle with the given schema by connecting to the database identified by dataSource (e.g., "dbname=mypgdb" or blank to use the PG* env vars).

Open assumes that the database already exists.

func TestContext

func TestContext(t testing.TB) context.Context

func WithTransaction

func WithTransaction(ctx context.Context, db *sql.DB, fn TxFn) (err error)

WithTransaction creates a new transaction and handles rollback/commit based on the error object returned by the `TxFn`.

Types

type Transaction

type Transaction interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*sql.Stmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Transaction is an interface that models the standard transaction in `database/sql`.

To ensure `TxFn` funcs cannot commit or rollback a transaction (which is handled by `WithTransaction`), those methods are not included here.

type TxFn

type TxFn func(Transaction) error

TxFn is a function that will be called with an initialised `Transaction` object that can be used for executing statements and queries against a database.

Jump to

Keyboard shortcuts

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