dbutil

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package dbutil basically provides DbContext to simplify DB interaction

Index

Constants

This section is empty.

Variables

View Source
var SKIP_ERROR error = fmt.Errorf("Skipping due to previous error")

Functions

This section is empty.

Types

type DatasetFlag

type DatasetFlag uint64
const (
	Committed DatasetFlag = 1 << iota
	CheckedOut
)

type DbAccessor

type DbAccessor interface {
	RegisterErrorHandler(errorHandler func(err error))
	QueryRow(query string, args ...interface{}) (*sql.Row, error)
	ScanQueryRow(supressErrNoRows bool, query Query, destination []interface{}) error
	Query(query string, args ...interface{}) (RowsAccessor, error)
	Execute(query string, args ...interface{}) error
	Commit(restartTx bool) error
	Rollback(restartTx bool) error
	Close() error
	GetPGXConn() (*pgx.Conn, error)
	CheckPGXBatchErr(br pgx.BatchResults) error
	LastError() error
	SetLastError(err error)
	ResetError()
}

type DbConnectionHelper

type DbConnectionHelper struct {
	DbConnectionURL string
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifeTime int
	// contains filtered or unexported fields
}

func (*DbConnectionHelper) CloseContexts

func (helper *DbConnectionHelper) CloseContexts()

CloseContexts closes all open db connections

func (*DbConnectionHelper) GetDbContext

func (helper *DbConnectionHelper) GetDbContext(ctx *context.Context, useTransaction bool) (dbContext *DbContext)

GetDbContext returns a context in which queries (including inserts, deletes) can be executed. ctx allows optional context cancellation if not nil DbContext.Err and any transaction are resetted

type DbContext

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

func (*DbContext) CheckPGXBatchErr

func (dbContext *DbContext) CheckPGXBatchErr(br pgx.BatchResults) error

CheckPGXErr applies the first error of a PGX batch to dbContext.Err, and logs all of them

func (*DbContext) Close

func (dbContext *DbContext) Close() error

Close commits the transaction. In case of an error the transaction is rolled back. dbContext.Err is set to nil

func (*DbContext) Commit

func (dbContext *DbContext) Commit(restartTx bool) error

Commit commits any open transaction if there is one

func (*DbContext) Execute

func (dbContext *DbContext) Execute(query string, args ...interface{}) error

Execute runs given query with given substitution parameters as for $1 etc. The operation becomes a no-op if there is a previous error in DbContext.err

func (*DbContext) GetPGXConn

func (dbContext *DbContext) GetPGXConn() (*pgx.Conn, error)

func (*DbContext) LastError

func (dbContext *DbContext) LastError() error

func (*DbContext) Query

func (dbContext *DbContext) Query(query string, args ...interface{}) (RowsAccessor, error)

Query returns all rows for given query with given substituion paramaters. The operation becomes a no-op if there is a previous error in DbContext.err.

func (*DbContext) QueryRow

func (dbContext *DbContext) QueryRow(query string, args ...interface{}) (*sql.Row, error)

QueryRow returns at most one row for given query with given substituion paramaters. The operation becomes a no-op if there is a previous error in DbContext.err.

func (*DbContext) RegisterErrorHandler

func (dbContext *DbContext) RegisterErrorHandler(errorHandler func(err error))

RegisterErrorHandler registers function as error handler to call in case DbContext.Err is set. Any previous handler is overwritten.

func (*DbContext) ResetError

func (dbContext *DbContext) ResetError()

func (*DbContext) Rollback

func (dbContext *DbContext) Rollback(restartTx bool) error

Rollback rolls any open transaction back if there is one

func (*DbContext) ScanQueryRow

func (dbContext *DbContext) ScanQueryRow(supressErrNoRows bool, query Query, destination []interface{}) error

ScanQueryRow executes the given query with optional args and writes colums of the first row (if present) to given destination parameters The operation becomes a no-op if there is a previous error in DbContext.err. If supressErrNoRows and error occurrs, destination value are reset to ""

func (*DbContext) SetLastError

func (dbContext *DbContext) SetLastError(err error)

type Query

type Query struct {
	Query string
	Args  []interface{}
}

Query allows to pass parametrized query an single function parameter

type RowsAccessor

type RowsAccessor interface {
	Next() bool
	Scan(dest ...interface{}) error
}

Jump to

Keyboard shortcuts

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