dbtx

package
v0.0.0-...-a3543a4 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func OpenDB

func OpenDB(cfg Config) (*pgxpool.Pool, error)

OpenDB init open database pool

Types

type Config

type Config struct {
	DSN          string
	MaxOpenConns int
	MinOpenConns int
}

type DBTX

type DBTX interface {
	Prepare(ctx context.Context, name, sql string) (*pgconn.StatementDescription, error)
	Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error)
	Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row

	Begin(ctx context.Context) (pgx.Tx, error)
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

DBTX adalah interface yang mengabstraksi operasi database, baik operasi biasa maupun operasi dalam transaksi. isi daripada interface ini tergantung driver database yang dipakai misalnya saya menggunakan pgxpool. maka kita akan meniru semua fungsi2nya

func ExtractTx

func ExtractTx(ctx context.Context, defaultPool *pgxpool.Pool) DBTX

ExtractTx extract transaction from context and transform database into db.DBTX

func NewPGStore

func NewPGStore(pool *pgxpool.Pool, tx pgx.Tx) DBTX

NewPGStore return interface can execute TX and pgx.Pool

type KeyTransaction

type KeyTransaction string
const TXKey KeyTransaction = "project-name"

type PGStore

type PGStore struct {
	NonTX *pgxpool.Pool
	Tx    pgx.Tx
}

func (*PGStore) Begin

func (p *PGStore) Begin(ctx context.Context) (pgx.Tx, error)

Begin implements DBTX

func (*PGStore) Commit

func (p *PGStore) Commit(ctx context.Context) error

Commit implements DBTX

func (*PGStore) Exec

func (p *PGStore) Exec(ctx context.Context, sql string, arguments ...interface{}) (commandTag pgconn.CommandTag, err error)

Exec implements DBTX

func (*PGStore) Prepare

func (p *PGStore) Prepare(ctx context.Context, name string, sql string) (*pgconn.StatementDescription, error)

Prepare implements DBTX

func (*PGStore) Query

func (p *PGStore) Query(ctx context.Context, sql string, args ...interface{}) (pgx.Rows, error)

Query implements DBTX

func (*PGStore) QueryRow

func (p *PGStore) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row

QueryRow implements DBTX

func (*PGStore) Rollback

func (p *PGStore) Rollback(ctx context.Context) error

Rollback implements DBTX

type TxManager

type TxManager interface {
	WithAtomic(ctx context.Context, tFunc func(ctx context.Context) error) error
}

func NewTxManager

func NewTxManager(sqlDB *pgxpool.Pool, log *slog.Logger) TxManager

Jump to

Keyboard shortcuts

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