...

Package db

import "github.com/letsencrypt/boulder/db"
Overview
Index

Overview ▾

Index ▾

func IsDuplicate(err error) bool
func IsNoRows(err error) bool
func QuestionMarks(n int) string
func WithTransaction(ctx context.Context, dbMap DatabaseMap, f txFunc) (interface{}, error)
type DatabaseMap
type ErrDatabaseOp
    func (e ErrDatabaseOp) Error() string
    func (e ErrDatabaseOp) Unwrap() error
type Execer
type Executor
type Inserter
type MappedExecutor
type MappedSelector
    func NewMappedSelector[T any](executor MappedExecutor) (MappedSelector[T], error)
type MockSqlExecutor
    func (mse MockSqlExecutor) Delete(ctx context.Context, list ...interface{}) (int64, error)
    func (mse MockSqlExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    func (mse MockSqlExecutor) Get(ctx context.Context, i interface{}, keys ...interface{}) (interface{}, error)
    func (mse MockSqlExecutor) Insert(ctx context.Context, list ...interface{}) error
    func (mse MockSqlExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    func (mse MockSqlExecutor) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
    func (mse MockSqlExecutor) Select(ctx context.Context, i interface{}, query string, args ...interface{}) ([]interface{}, error)
    func (mse MockSqlExecutor) SelectFloat(ctx context.Context, query string, args ...interface{}) (float64, error)
    func (mse MockSqlExecutor) SelectInt(ctx context.Context, query string, args ...interface{}) (int64, error)
    func (mse MockSqlExecutor) SelectNullFloat(ctx context.Context, query string, args ...interface{}) (sql.NullFloat64, error)
    func (mse MockSqlExecutor) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)
    func (mse MockSqlExecutor) SelectNullStr(ctx context.Context, query string, args ...interface{}) (sql.NullString, error)
    func (mse MockSqlExecutor) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error
    func (mse MockSqlExecutor) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)
    func (mse MockSqlExecutor) Update(ctx context.Context, list ...interface{}) (int64, error)
type MultiInserter
    func NewMultiInserter(table string, fields []string, returningColumn string) (*MultiInserter, error)
    func (mi *MultiInserter) Add(row []interface{}) error
    func (mi *MultiInserter) Insert(ctx context.Context, queryer Queryer) ([]int64, error)
type OneSelector
type Queryer
type RollbackError
    func (re *RollbackError) Error() string
type Rows
type SelectExecer
type Selector
type Transaction
type WrappedExecutor
    func (we WrappedExecutor) Delete(ctx context.Context, list ...interface{}) (int64, error)
    func (we WrappedExecutor) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    func (we WrappedExecutor) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)
    func (we WrappedExecutor) Insert(ctx context.Context, list ...interface{}) error
    func (we WrappedExecutor) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    func (we WrappedExecutor) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
    func (we WrappedExecutor) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)
    func (we WrappedExecutor) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)
    func (we WrappedExecutor) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error
    func (we WrappedExecutor) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)
    func (we WrappedExecutor) Update(ctx context.Context, list ...interface{}) (int64, error)
type WrappedMap
    func NewWrappedMap(dbMap *borp.DbMap) *WrappedMap
    func (m *WrappedMap) BeginTx(ctx context.Context) (Transaction, error)
    func (m *WrappedMap) Delete(ctx context.Context, list ...interface{}) (int64, error)
    func (m *WrappedMap) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    func (m *WrappedMap) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)
    func (m *WrappedMap) Insert(ctx context.Context, list ...interface{}) error
    func (m *WrappedMap) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    func (m *WrappedMap) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
    func (m *WrappedMap) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)
    func (m *WrappedMap) SelectNullInt(ctx context.Context, query string, args ...interface{}) (sql.NullInt64, error)
    func (m *WrappedMap) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error
    func (m *WrappedMap) SelectStr(ctx context.Context, query string, args ...interface{}) (string, error)
    func (m *WrappedMap) TableFor(t reflect.Type, checkPK bool) (*borp.TableMap, error)
    func (m *WrappedMap) Update(ctx context.Context, list ...interface{}) (int64, error)
type WrappedTransaction
    func (tx WrappedTransaction) Commit() error
    func (tx WrappedTransaction) Delete(ctx context.Context, list ...interface{}) (int64, error)
    func (tx WrappedTransaction) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
    func (tx WrappedTransaction) Get(ctx context.Context, holder interface{}, keys ...interface{}) (interface{}, error)
    func (tx WrappedTransaction) Insert(ctx context.Context, list ...interface{}) error
    func (tx WrappedTransaction) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
    func (tx WrappedTransaction) Rollback() error
    func (tx WrappedTransaction) Select(ctx context.Context, holder interface{}, query string, args ...interface{}) ([]interface{}, error)
    func (tx WrappedTransaction) SelectOne(ctx context.Context, holder interface{}, query string, args ...interface{}) error
    func (tx WrappedTransaction) Update(ctx context.Context, list ...interface{}) (int64, error)

Package files

gorm.go interfaces.go map.go multi.go qmarks.go rollback.go transaction.go

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.

type DatabaseMap

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

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

type ErrDatabaseOp

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.

type ErrDatabaseOp struct {
    Op    string
    Table string
    Err   error
}

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

A Execer is anything that provides an `ExecContext` function

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

type Executor

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

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)
}

type Inserter

A Inserter is anything that provides an `Insert` function

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

type MappedExecutor

MappedExecutor is anything that can map types to tables

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

type MappedSelector

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

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)
}

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

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).

type MockSqlExecutor struct{}

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

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.

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

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

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

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

type Queryer

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 Queryer interface {
    QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
}

type RollbackError

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

type RollbackError struct {
    Err         error
    RollbackErr error
}

func (*RollbackError) Error

func (re *RollbackError) Error() string

Error implements the error interface

type Rows

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

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

type SelectExecer

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

type SelectExecer interface {
    Selector
    Execer
}

type Selector

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

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

type Transaction

Transaction extends an Executor and adds Rollback and Commit

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

type WrappedExecutor

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

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

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

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

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

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

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

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

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)