dbq

package module
v0.0.0-...-b5c4152 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2023 License: MIT Imports: 11 Imported by: 0

README

dbq

Database helper lib for Golang

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTxOpts = sql.TxOptions{
	Isolation: sql.LevelDefault,
	ReadOnly:  false,
}

DefaultTxOpts is package variable with default transaction level

Functions

func Exec

func Exec(ctx TxContext, query string, args ...any) (int64, error)

func Query

func Query[T any](ctx TxContext, query string, binder func(*T) []any, args ...any) ([]T, error)

func QueryRow

func QueryRow[T any](ctx TxContext, query string, binder func(*T) []any, args ...any) (T, error)

Types

type Access

type Access interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

Access interface for simple DML operations.

func FromCtxOr

func FromCtxOr(ctx context.Context, data Access) Access

FromCtxOr returns access interface from context or data arg.

type Connector

type Connector interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

Connector for sql database.

type CtxDataSourceKey

type CtxDataSourceKey struct{} // Datasource context key, useful only error handling

type NotFoundError

type NotFoundError struct {
	DataSource string
}

func (NotFoundError) Error

func (e NotFoundError) Error() string

type Null

type Null[T Type] struct {
	Val   T
	Valid bool // Valid is true if T is not NULL
}

Null is generic type which can be nullable..

func FromPtr

func FromPtr[T Type](n *T) Null[T]

FromPtr creates a new T that will be null if n is nil.

func FromValue

func FromValue[T Type](n T) Null[T]

FromValue creates a new T that will always be valid.

func NewNull

func NewNull[T Type](val T, valid bool) Null[T]

New creates a new Null[T]

func (Null[T]) Equal

func (n Null[T]) Equal(other Null[T]) bool

Equal returns true if have the same value or are both null.

func (Null[T]) IsZero

func (n Null[T]) IsZero() bool

IsZero returns true for invalid value

func (Null[T]) MarshalJSON

func (n Null[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Null[T]) Ptr

func (n Null[T]) Ptr() *T

Ptr returns a pointer to this T value, or a nil pointer if Val is null.

func (*Null[T]) Scan

func (n *Null[T]) Scan(value any) error

Scan implements the Scanner interface.

func (*Null[T]) SetValid

func (n *Null[T]) SetValid(v T)

SetValid changes this T value and also sets it to be non-null.

func (*Null[T]) UnmarshalJSON

func (n *Null[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (Null[T]) Value

func (n Null[T]) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Null[T]) ValueOrZero

func (n Null[T]) ValueOrZero() T

ValueOrZero returns the inner value if valid, otherwise false.

type Number

type Number interface {
	~byte | ~int | ~int16 | ~int32 | ~int64 | ~float64
}

Number constraint.

type Tx

type Tx struct {
	context.Context //nolint:containedctx
	Tx              *sql.Tx
}

Tx represents transaction with context as inner object.

func (*Tx) Commit

func (t *Tx) Commit() error

Commit this transaction.

func (*Tx) Exec

func (t *Tx) Exec(query string, args ...any) (sql.Result, error)

Exec executes query with args.

func (*Tx) Prepare

func (t *Tx) Prepare(query string) (*sql.Stmt, error)

Prepare query.

func (*Tx) Query

func (t *Tx) Query(query string, args ...any) (*sql.Rows, error)

Query loads data from db.

func (*Tx) QueryRow

func (t *Tx) QueryRow(query string, args ...any) *sql.Row

QueryRow loads single row from db.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback cancel this transaction.

func (*Tx) WithValue

func (t *Tx) WithValue(key, value any) TxContext

type TxContext

type TxContext interface {
	context.Context
	WithValue(key, value any) TxContext
	Prepare(query string) (*sql.Stmt, error)
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
}

TxContext interface for DAO operations with context.

type TxProvider

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

TxProvider ...

func NewTxProvider

func NewTxProvider(conn Connector) *TxProvider

NewTxProvider ...

func (*TxProvider) Acquire

func (t *TxProvider) Acquire(ctx context.Context) (*Tx, error)

Acquire transaction from db

func (*TxProvider) AcquireWithOpts

func (t *TxProvider) AcquireWithOpts(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

AcquireWithOpts transaction from db

func (*TxProvider) Tx

func (t *TxProvider) Tx(ctx context.Context, fn func(TxContext) error) error

Tx runs fn in transaction.

func (*TxProvider) TxWithOpts

func (t *TxProvider) TxWithOpts(ctx context.Context, fn func(TxContext) error, opts *sql.TxOptions) error

TxWithOpts ...

type Type

type Type interface {
	Number | ~bool | ~string | time.Time
}

Type constraint.

Jump to

Keyboard shortcuts

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