sql

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultPingTimeout = 15 * time.Second

Variables

This section is empty.

Functions

func Query

func Query[T any](db *DB, query string,
	pointers func(*T) []interface{}, args ...any) ([]T, error)

Query is a generic query helper.

func QueryContext

func QueryContext[T any](ctx context.Context, db *DB, query string,
	pointers func(*T) []interface{}, args ...any) ([]T, error)

QueryContext is a generic query helper with context.

Types

type DB

type DB struct {
	*dbx.DB[*sql.DB]
	// contains filtered or unexported fields
}

func NewDB

func NewDB(driverName string, dsns []string,
	checker cluster.NodeChecker[*sql.DB], options ...Option) (*DB, error)

NewDB returns an instance of *DB.

func (*DB) DoTx

func (db *DB) DoTx(f func(db dbx.DBxer[*sql.DB, *sql.Tx, *sql.TxOptions]) error, opts *sql.TxOptions) error

DoTx executes passed function in transaction.

func (*DB) DoTxContext

func (db *DB) DoTxContext(
	ctx context.Context,
	f func(ctx context.Context, db dbx.DBxer[*sql.DB, *sql.Tx, *sql.TxOptions]) error,
	opts *sql.TxOptions) error

DoTxContext executes passed function in transaction.

func (*DB) Exec

func (db *DB) Exec(query string, args ...any) (sql.Result, error)

Exec executes query.

func (*DB) ExecContext

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

ExecContext executes query with context.

func (*DB) Prepare

func (db *DB) Prepare(query string) (*sql.Stmt, error)

Prepare prepares query.

func (*DB) PrepareContext

func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext prepares query with context.

func (*DB) Query

func (db *DB) Query(query string, args ...any) (*sql.Rows, error)

Query queries underlying cluster.

func (*DB) QueryContext

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

QueryContext queries underlying cluster with context.

func (*DB) QueryRow

func (db *DB) QueryRow(query string, args ...any) dbx.Row

QueryRow queries row from underlying cluster.

func (*DB) QueryRowContext

func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) dbx.Row

QueryRowContext queries row from underlying cluster with context.

func (*DB) Tx

func (db *DB) Tx() (*sql.Tx, error)

func (*DB) WithCtx added in v0.0.2

func (db *DB) WithCtx(ctx context.Context) *DB

func (*DB) WithDefaultNodeStrategy added in v0.0.3

func (db *DB) WithDefaultNodeStrategy(strategy dbx.GetNodeStragegy) *DB

func (*DB) WithNodeWaitTimeout added in v0.0.3

func (db *DB) WithNodeWaitTimeout(timeout time.Duration) *DB

func (*DB) WithReadFromNodeStrategy added in v0.0.3

func (db *DB) WithReadFromNodeStrategy(strategy dbx.GetNodeStragegy) *DB

func (*DB) WithWriteToNodeStrategy added in v0.0.3

func (db *DB) WithWriteToNodeStrategy(strategy dbx.GetNodeStragegy) *DB

type DBOpener

type DBOpener func(ctx context.Context, driverName, dsn string) (*sql.DB, error)
var DefaultDBOpener DBOpener = func(ctx context.Context, driverName, dsn string) (*sql.DB, error) {
	db, err := sql.Open(driverName, dsn)
	if err != nil {
		return nil, errx.Wrap("open db", err)
	}

	pingCtx, cancel := context.WithTimeout(context.Background(), DefaultPingTimeout)
	defer cancel()

	if err := db.PingContext(pingCtx); err != nil {
		return nil, errx.Wrap("ping db", err)
	}

	return db, nil
}

func CustomDBOpener added in v0.0.9

func CustomDBOpener(dbOpener DBOpener, callbacks ...func(dsn string, db *sql.DB) error) DBOpener

type ErrRow

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

func (*ErrRow) Err

func (r *ErrRow) Err() error

func (*ErrRow) Scan

func (r *ErrRow) Scan(dest ...any) error

type Option

type Option func(db *DB)

func WithDBOpener

func WithDBOpener(dbOpener DBOpener) Option

func WithGenericOptions

func WithGenericOptions(options ...dbx.Option[*sql.DB]) Option

Jump to

Keyboard shortcuts

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