db

package
v0.0.0-...-8ec3fd9 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrAlreadyExists = "duplicate key value violates unique constraint"
)

Variables

This section is empty.

Functions

func InitTimescale

func InitTimescale(ctx context.Context, db DB) (err error)

func IsErrAlreadyExists

func IsErrAlreadyExists(err error) bool

func Migrate

func Migrate(ctx context.Context, logger *slog.Logger, db DB, migrations []Migration) (err error)

func Rollback

func Rollback(ctx context.Context, db DB, migrations []Migration, numberToRollback int64) (err error)

Rollback undo the latest migration

Types

type DB

type DB interface {
	Ping(ctx context.Context) error
	SetMaxIdleConns(n int)
	SetMaxOpenConns(n int)
	SetConnMaxIdleTime(d time.Duration)
	SetConnMaxLifetime(d time.Duration)
	Stats() sql.DBStats
	Acquire(ctx context.Context) (conn *sql.Conn, err error)
	Close() error
	Queryer
	Txer
}

DB represents a pool of zero or more underlying connections. It must be safe for concurrent use by multiple goroutines.

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database is wrapper of `sqlx.DB` which implements `DB`

func Connect

func Connect(databaseURL string, poolSize int) (dbPool *Database, err error)

Connect to a database and verify the connections with a ping. See https://www.alexedwards.net/blog/configuring-sqldb and https://making.pusher.com/production-ready-connection-pooling-in-go https://brandur.org/fragments/postgres-parameters for the details

func (*Database) Acquire

func (db *Database) Acquire(ctx context.Context) (*sql.Conn, error)

func (*Database) Begin

func (db *Database) Begin(ctx context.Context) (Tx, error)

Begin starts a transaction. The default isolation level is dependent on the driver. The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

func (*Database) BeginTx

func (db *Database) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)

BeginTx starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*Database) Close

func (db *Database) Close() error

func (*Database) Exec

func (db *Database) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Database) Get

func (db *Database) Get(ctx context.Context, dest any, query string, args ...any) error

Get a single record. Any placeholder parameters are replaced with supplied args. An `ErrNoRows` error is returned if the result set is empty.

func (*Database) Ping

func (db *Database) Ping(ctx context.Context) error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*Database) Query

func (db *Database) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Database) Rebind

func (db *Database) Rebind(query string) (ret string)

func (*Database) Select

func (db *Database) Select(ctx context.Context, dest any, query string, args ...any) error

Select an array of records. Any placeholder parameters are replaced with supplied args.

func (*Database) SetConnMaxIdleTime

func (db *Database) SetConnMaxIdleTime(duration time.Duration)

SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are not closed due to a connection's idle time.

func (*Database) SetConnMaxLifetime

func (db *Database) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are not closed due to a connection's age.

func (*Database) SetMaxIdleConns

func (db *Database) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.

If n <= 0, no idle connections are retained.

The default max idle connections is currently 2. This may change in a future release.

func (*Database) SetMaxOpenConns

func (db *Database) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to the database.

If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit.

If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

func (*Database) Stats

func (db *Database) Stats() sql.DBStats

Stats returns database statistics.

func (*Database) Transaction

func (db *Database) Transaction(ctx context.Context, fn func(tx Tx) error) (err error)

type Migration

type Migration struct {
	ID   int64
	Name string
	Up   func(ctx context.Context, tx Queryer) (err error)
	Down func(ctx context.Context, tx Queryer) (err error)
}

type Queryer

type Queryer interface {
	Get(ctx context.Context, dest any, query string, args ...any) error
	Select(ctx context.Context, dest any, query string, args ...any) error
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	Rebind(query string) (ret string)
}

Queryer allows to query a database.

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

Transaction is wrapper of `sqlx.Tx` which implements `Tx`

func (*Transaction) Commit

func (tx *Transaction) Commit() error

Commit commits the transaction.

func (*Transaction) Exec

func (tx *Transaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Transaction) Get

func (tx *Transaction) Get(ctx context.Context, dest any, query string, args ...any) error

Get a single record. Any placeholder parameters are replaced with supplied args. An `ErrNoRows` error is returned if the result set is empty.

func (*Transaction) Query

func (tx *Transaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Transaction) Rebind

func (tx *Transaction) Rebind(query string) (ret string)

func (*Transaction) Rollback

func (tx *Transaction) Rollback() error

Rollback aborts the transaction.

func (*Transaction) Select

func (tx *Transaction) Select(ctx context.Context, dest any, query string, args ...any) error

Select an array of records. Any placeholder parameters are replaced with supplied args.

type Tx

type Tx interface {
	Commit() error
	Rollback() error
	Queryer
}

Tx represents an in-progress database transaction.

type Txer

type Txer interface {
	Begin(ctx context.Context) (Tx, error)
	BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
	Transaction(ctx context.Context, fn func(tx Tx) error) (err error)
}

Txer is the ability to start transactions

Jump to

Keyboard shortcuts

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