postgres

package
v0.0.0-...-6aec01b Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTxOptions = pgx.TxOptions{
	IsoLevel: pgx.ReadCommitted,
}

Functions

func IsEmptyResponse

func IsEmptyResponse(err error) bool

IsEmptyResponse returns true if error means that response contains no data.

func IsEntityExists

func IsEntityExists(err error) bool

IsEntityExists returns true if the provided error related to cases when entity already exists in the database.

Types

type PgxIface

type PgxIface interface {
	Acquire(ctx context.Context) (*pgxpool.Conn, error)

	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row

	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
	Close()
}

type Postgres

type Postgres struct {
	Pool PgxIface
}

Postgres represents abstraction over database connection pool.

func New

func New(url string, log *logger.Logger) (*Postgres, error)

New initializes connection to database and creates new wrapper object.

func (*Postgres) BeginTx

func (p *Postgres) BeginTx(ctx context.Context) (pgx.Tx, error)

BeginTx starts new database transaction.

func (*Postgres) Close

func (p *Postgres) Close()

Close gracefully closes connection to database.

func (*Postgres) RunAtomic

func (p *Postgres) RunAtomic(ctx context.Context, operation func(tx Transaction) error) error

RunAtomic executes provided function in Postgres transaction.

func (*Postgres) Select

func (p *Postgres) Select(
	ctx context.Context,
	dst interface{},
	query string,
	args ...interface{},
) error

Select returns rows matching query.

type Transaction

type Transaction interface {
	Exec(ctx context.Context, sql string, arguments ...any) (commandTag pgconn.CommandTag, err error)
	QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
	Commit(ctx context.Context) error
	Rollback(ctx context.Context) error
}

Transaction is interface to generic Postgres transaction.

Jump to

Keyboard shortcuts

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