db

package
v0.0.0-...-0e5e1e9 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsDuplicate

func IsDuplicate(err error) bool

IsDuplicate is a utility function for determining if an error wrap MySQL's Error 1062: Duplicate entry. This error is returned when inserting a row would violate a unique key constraint.

func IsNoRows

func IsNoRows(err error) bool

IsNoRows is a utility function for determining if an error wraps the go sql package's ErrNoRows, which is returned when a Scan operation has no more results to return, and as such is returned by many borp methods.

func QuestionMarks

func QuestionMarks(n int) string

QuestionMarks returns a string consisting of N question marks, joined by commas. If n is <= 0, panics.

func WithTransaction

func WithTransaction(ctx context.Context, dbMap DatabaseMap, f txFunc) (interface{}, error)

WithTransaction runs the given function in a transaction, rolling back if it returns an error and committing if not. The provided context is also attached to the transaction. WithTransaction also passes through a value returned by `f`, if there is no error.

Types

type DatabaseMap

type DatabaseMap interface {
	OneSelector
	Inserter
	SelectExecer
	BeginTx(context.Context) (Transaction, error)
}

DatabaseMap offers the full combination of OneSelector, Inserter, SelectExecer, and a Begin function for creating a Transaction.

type ErrDatabaseOp

type ErrDatabaseOp struct {
	Op    string
	Table string
	Err   error
}

ErrDatabaseOp wraps an underlying err with a description of the operation that was being performed when the error occurred (insert, select, select one, exec, etc) and the table that the operation was being performed on.

func (ErrDatabaseOp) Error

func (e ErrDatabaseOp) Error() string

Error for an ErrDatabaseOp composes a message with context about the operation and table as well as the underlying Err's error message.

func (ErrDatabaseOp) Unwrap

func (e ErrDatabaseOp) Unwrap() error

Unwrap returns the inner error to allow inspection of error chains.

type Execer

type Execer interface {
	ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
}

A Execer is anything that provides an `ExecContext` function

type Executor

type Executor interface {
	OneSelector
	Inserter
	SelectExecer
	Queryer
	Delete(context.Context, ...interface{}) (int64, error)
	Get(context.Context, interface{}, ...interface{}) (interface{}, error)
	Update(context.Context, ...interface{}) (int64, error)
}

Executor offers the full combination of OneSelector, Inserter, SelectExecer and adds a handful of other high level borp methods we use in Boulder.

type Inserter

type Inserter interface {
	Insert(context.Context, ...interface{}) error
}

A Inserter is anything that provides an `Insert` function

type MappedExecutor

type MappedExecutor interface {
	TableFor(reflect.Type, bool) (*borp.TableMap, error)
	QueryContext(ctx context.Context, clauses string, args ...interface{}) (*sql.Rows, error)
}

MappedExecutor is anything that can map types to tables

type MappedSelector

type MappedSelector[T any] interface {
	QueryContext(ctx context.Context, clauses string, args ...interface{}) (Rows[T], error)
	QueryFrom(ctx context.Context, tablename string, clauses string, args ...interface{}) (Rows[T], error)
}

MappedSelector is anything that can execute various kinds of SQL statements against a table automatically determined from the parameterized type.

func NewMappedSelector

func NewMappedSelector[T any](executor MappedExecutor) (MappedSelector[T], error)

NewMappedSelector returns an object which can be used to automagically query the provided type-mapped database for rows of the parameterized type.

type MockSqlExecutor

type MockSqlExecutor struct{}

MockSqlExecutor implement SqlExecutor by returning errors from every call.

TODO: To mock out WithContext, we needed to be able to return objects that satisfy borp.SqlExecutor. That's a pretty big interface, so we specify one no-op mock that we can embed everywhere we need to satisfy it. Note: MockSqlExecutor does *not* implement WithContext. The expectation is that structs that embed MockSqlExecutor will define their own WithContext that returns a reference to themselves. That makes it easy for those structs to override the specific methods they need to implement (e.g. SelectOne).

func (MockSqlExecutor) Delete

func (mse MockSqlExecutor) Delete(ctx context.Context, list ...interface{}) (int64, error)

func (MockSqlExecutor) ExecContext

func (mse MockSqlExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (MockSqlExecutor) Get

func (mse MockSqlExecutor) Get(ctx context.Context, i interface{}, keys ...interface{}) (interface{}, error)

func (MockSqlExecutor) Insert

func (mse MockSqlExecutor) Insert(ctx context.Context, list ...interface{}) error

func (MockSqlExecutor) QueryContext

func (mse MockSqlExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (MockSqlExecutor) QueryRowContext

func (mse MockSqlExecutor) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (MockSqlExecutor) Select

func (mse MockSqlExecutor) Select(ctx context.Context, i interface{}, query string, args ...interface{}) ([]interface{}, error)

func (MockSqlExecutor) SelectFloat

func (mse MockSqlExecutor) SelectFloat(ctx context.Context, query string, args ...interface{}) (float64, error)

func (MockSqlExecutor) SelectInt

func (mse MockSqlExecutor) SelectInt(ctx context.Context, query string, args ...interface{}) (int64, error)

func (MockSqlExecutor) SelectNullFloat

func (mse MockSqlExecutor) SelectNullFloat(ctx context.Context, query string, args ...interface{}) (sql.NullFloat64, error)

func (MockSqlExecutor) SelectNullInt

func (mse MockSqlExecutor) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)

func (MockSqlExecutor) SelectNullStr

func (mse MockSqlExecutor) SelectNullStr(ctx context.Context, query string, args ...interface{}) (sql.NullString, error)

func (MockSqlExecutor) SelectOne

func (mse MockSqlExecutor) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error

func (MockSqlExecutor) SelectStr

func (mse MockSqlExecutor) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)

func (MockSqlExecutor) Update

func (mse MockSqlExecutor) Update(ctx context.Context, list ...interface{}) (int64, error)

type MultiInserter

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

MultiInserter makes it easy to construct a `INSERT INTO table (...) VALUES ... RETURNING id;` query which inserts multiple rows into the same table. It can also execute the resulting query.

func NewMultiInserter

func NewMultiInserter(table string, fields []string, returningColumn string) (*MultiInserter, error)

NewMultiInserter creates a new MultiInserter, checking for reasonable table name and list of fields. returningColumn is the name of a column to be used in a `RETURNING xyz` clause at the end. If it is empty, no `RETURNING xyz` clause is used. If returningColumn is present, it must refer to a column that can be parsed into an int64. Safety: `table`, `fields`, and `returningColumn` must contain only strings that are known at compile time. They must not contain user-controlled strings.

func (*MultiInserter) Add

func (mi *MultiInserter) Add(row []interface{}) error

Add registers another row to be included in the Insert query.

func (*MultiInserter) Insert

func (mi *MultiInserter) Insert(ctx context.Context, queryer Queryer) ([]int64, error)

Insert inserts all the collected rows into the database represented by `queryer`. If a non-empty returningColumn was provided, then it returns the list of values from that column returned by the query.

type OneSelector

type OneSelector interface {
	SelectOne(context.Context, interface{}, string, ...interface{}) error
}

A OneSelector is anything that provides a `SelectOne` function.

type Queryer

type Queryer interface {
	QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}

Queryer offers the QueryContext method. Note that this is not read-only (i.e. not Selector), since a QueryContext can be `INSERT`, `UPDATE`, etc. The difference between QueryContext and ExecContext is that QueryContext can return rows. So for instance it is suitable for inserting rows and getting back ids.

type RollbackError

type RollbackError struct {
	Err         error
	RollbackErr error
}

RollbackError is a combination of a database error and the error, if any, encountered while trying to rollback the transaction.

func (*RollbackError) Error

func (re *RollbackError) Error() string

Error implements the error interface

type Rows

type Rows[T any] interface {
	ForEach(func(*T) error) error
	Next() bool
	Get() (*T, error)
	Err() error
	Close() error
}

Rows is anything which lets you iterate over the result rows of a SELECT query. It is similar to sql.Rows, but generic.

type SelectExecer

type SelectExecer interface {
	Selector
	Execer
}

SelectExecer offers a subset of borp.SqlExecutor's methods: Select and ExecContext.

type Selector

type Selector interface {
	Select(context.Context, interface{}, string, ...interface{}) ([]interface{}, error)
}

A Selector is anything that provides a `Select` function.

type Transaction

type Transaction interface {
	Executor
	Rollback() error
	Commit() error
}

Transaction extends an Executor and adds Rollback and Commit

type WrappedExecutor

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

WrappedExecutor wraps a borp.SqlExecutor such that its major functions wrap error results in ErrDatabaseOp instances before returning them to the caller.

func (WrappedExecutor) Delete

func (we WrappedExecutor) Delete(ctx context.Context, list ...interface{}) (int64, error)

func (WrappedExecutor) ExecContext

func (we WrappedExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (WrappedExecutor) Get

func (we WrappedExecutor) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)

func (WrappedExecutor) Insert

func (we WrappedExecutor) Insert(ctx context.Context, list ...interface{}) error

func (WrappedExecutor) QueryContext

func (we WrappedExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (WrappedExecutor) QueryRowContext

func (we WrappedExecutor) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (WrappedExecutor) Select

func (we WrappedExecutor) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)

func (WrappedExecutor) SelectNullInt

func (we WrappedExecutor) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)

func (WrappedExecutor) SelectOne

func (we WrappedExecutor) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error

func (WrappedExecutor) SelectStr

func (we WrappedExecutor) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)

func (WrappedExecutor) Update

func (we WrappedExecutor) Update(ctx context.Context, list ...interface{}) (int64, error)

type WrappedMap

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

WrappedMap wraps a *borp.DbMap such that its major functions wrap error results in ErrDatabaseOp instances before returning them to the caller.

func NewWrappedMap

func NewWrappedMap(dbMap *borp.DbMap) *WrappedMap

func (*WrappedMap) BeginTx

func (m *WrappedMap) BeginTx(ctx context.Context) (Transaction, error)

func (*WrappedMap) Delete

func (m *WrappedMap) Delete(ctx context.Context, list ...interface{}) (int64, error)

func (*WrappedMap) ExecContext

func (m *WrappedMap) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (*WrappedMap) Get

func (m *WrappedMap) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)

func (*WrappedMap) Insert

func (m *WrappedMap) Insert(ctx context.Context, list ...interface{}) error

func (*WrappedMap) QueryContext

func (m *WrappedMap) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (*WrappedMap) QueryRowContext

func (m *WrappedMap) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

func (*WrappedMap) Select

func (m *WrappedMap) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)

func (*WrappedMap) SelectNullInt

func (m *WrappedMap) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)

func (*WrappedMap) SelectOne

func (m *WrappedMap) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error

func (*WrappedMap) SelectStr

func (m *WrappedMap) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)

func (*WrappedMap) TableFor

func (m *WrappedMap) TableFor(t reflect.Type, checkPK bool) (*borp.TableMap, error)

func (*WrappedMap) Update

func (m *WrappedMap) Update(ctx context.Context, list ...interface{}) (int64, error)

type WrappedTransaction

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

WrappedTransaction wraps a *borp.Transaction such that its major functions wrap error results in ErrDatabaseOp instances before returning them to the caller.

func (WrappedTransaction) Commit

func (tx WrappedTransaction) Commit() error

func (WrappedTransaction) Delete

func (tx WrappedTransaction) Delete(ctx context.Context, list ...interface{}) (int64, error)

func (WrappedTransaction) ExecContext

func (tx WrappedTransaction) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

func (WrappedTransaction) Get

func (tx WrappedTransaction) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)

func (WrappedTransaction) Insert

func (tx WrappedTransaction) Insert(ctx context.Context, list ...interface{}) error

func (WrappedTransaction) QueryContext

func (tx WrappedTransaction) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

func (WrappedTransaction) Rollback

func (tx WrappedTransaction) Rollback() error

func (WrappedTransaction) Select

func (tx WrappedTransaction) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)

func (WrappedTransaction) SelectOne

func (tx WrappedTransaction) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error

func (WrappedTransaction) Update

func (tx WrappedTransaction) Update(ctx context.Context, list ...interface{}) (int64, error)

Jump to

Keyboard shortcuts

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