sql

package
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2023 License: Apache-2.0 Imports: 7 Imported by: 4

Documentation

Overview

Package sql provides a database/sql wrapper.

Package sql provides a database/sql wrapper.

Index

Constants

View Source
const (
	DefaultReconnectRetries = 5
	DefaultTxRetries        = 10
)

Variables

View Source
var (
	ErrNoRows = sql.ErrNoRows
)

Functions

func ErrCode

func ErrCode(err error) string

func IsAdminShutdown

func IsAdminShutdown(err error) bool

func IsRetryable

func IsRetryable(err error) bool

Types

type DB

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

DB is a simple wrapper of sql.db.

func Open

func Open(ctx context.Context, driver, connectURL string, opts ...Option) (*DB, error)

Open opens a database specified by its database driver name and a driver-specific connect url.

func (*DB) BeginTx

func (db *DB) BeginTx(ctx context.Context, opts *TxOptions) (*Tx, error)

BeginTx starts a transaction.

func (*DB) Close

func (db *DB) Close() error

Close closes the database and prevents new queries from starting.

func (*DB) Exec

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

Exec executes a query without returning any rows.

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping checks DB status for the health checker

func (*DB) Query

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

Query executes a query that returns rows, typically a SELECT.

func (*DB) QueryRow

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

QueryRow executes a query that is expected to return at most one row.

func (*DB) Tx

func (db *DB) Tx(ctx context.Context, opts *TxOptions, fn func(tx *Tx) error) error

Tx wraps a transaction, implementing retries.

type Func

type Func func(ctx context.Context) error

Func is used to do some work.

type MiddlewareFunc

type MiddlewareFunc func(Func) Func

MiddlewareFunc is a function that receives a Func and returns a Func.

type NullString

type NullString = sql.NullString

type Option

type Option interface {
	Apply(*Options)
}

Option provides options for the creation of a db connection.

func WithReconnect

func WithReconnect(numRetries int) Option

WithReconnect will configure the db connection to reconnect if the connection has been closed due to an admin shutdown.

func WithTxRetries

func WithTxRetries(numRetries int) Option

WithTxRetries will configure the db to retry failed transactions.

type Options

type Options struct {
	ReconnectEnabled bool
	ReconnectRetries int
	TxRetries        int
}

Options holds the db options.

type Result

type Result = sql.Result

type Row

type Row = sql.Row

type Rows

type Rows = sql.Rows

type Tx

type Tx = sql.Tx

type TxOptions

type TxOptions = sql.TxOptions

Jump to

Keyboard shortcuts

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