Documentation
¶
Overview ¶
Package sqladapter adapts a database/sql pool to the core store interfaces. It is shared by the providers built on database/sql drivers.
Index ¶
- func Each(ctx context.Context, db *sql.DB, query string, args []any, ...) error
- func SessionParams(params map[string]string) (map[string]string, error)
- type Base
- type ClassifyFunc
- type Pool
- func (d *Pool) BeginTx(ctx context.Context, opts *store.TxOptions) (store.Tx, error)
- func (d *Pool) Classify(err error) error
- func (d *Pool) Close() error
- func (d *Pool) DB() *sql.DB
- func (d *Pool) ExecContext(ctx context.Context, query string, args ...any) (store.Result, error)
- func (d *Pool) Explain(ctx context.Context, query string, args []any, sep string) (string, error)
- func (d *Pool) PrepareContext(ctx context.Context, query string) (store.Prepared, error)
- func (d *Pool) QueryContext(ctx context.Context, query string, args ...any) (store.Rows, error)
- type RowsFunc
- type Scanned
- type TableKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Each ¶ added in v0.1.12
func Each(ctx context.Context, db *sql.DB, query string, args []any, scan func(*sql.Rows) error) error
Each runs query on db and calls scan for every row.
func SessionParams ¶ added in v0.1.21
SessionParams are a DSN's session settings keyed in lower case, as the database reads their names (whatever the case): what the DSN set is then found however it was written. Two names differing in case alone are an error, as which one the database applies last is unknown.
Types ¶
type Base ¶ added in v0.1.13
type Base struct {
*Pool
// contains filtered or unexported fields
}
Base is what the providers on database/sql share: the pool and the dialect, EXPLAIN estimator and schema introspector of one database. A provider embeds it and adds what is its own.
func NewBase ¶ added in v0.1.13
func NewBase(pool *Pool, d dialect.Dialect, e cost.Explainer, i introspect.Introspector) Base
NewBase is a Base on pool with the given adapters.
func (Base) Introspector ¶ added in v0.1.13
func (b Base) Introspector() introspect.Introspector
Introspector returns the schema introspector.
type ClassifyFunc ¶
ClassifyFunc turns a driver error into a store error the gateway acts on (such as *store.ConstraintError), or returns it unchanged.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool wraps *sql.DB, satisfying store.DB, store.Preparer and store.TxBeginner. Embedders get DB(), which bootstrap uses to size the pool and ping it; the type is not named DB so an embedded field cannot shadow that method.
func New ¶
func New(db *sql.DB, rows RowsFunc, classify ClassifyFunc) *Pool
New wraps db. A nil rows returns the driver rows unchanged; a nil classify leaves errors unchanged. Statement errors and errors met while reading rows (where PostgreSQL reports a failed INSERT ... RETURNING) are classified.
func (*Pool) Classify ¶ added in v0.1.17
Classify classifies a driver error of the pool's database, as its statements' errors are.
func (*Pool) ExecContext ¶
ExecContext implements store.DB.
func (*Pool) Explain ¶ added in v0.1.21
Explain runs an EXPLAIN statement and returns the text of its rows, joined by sep ("" when the plan is none: no rows). The rows are read and closed before it returns, so the caller may query the pool again for what the plan leaves out (a table's size) without holding a second connection. An error running the statement or reading its rows is classified: a connection lost while reading is no plan, not one of unknown cost.
func (*Pool) PrepareContext ¶
PrepareContext implements store.Preparer.
type RowsFunc ¶
RowsFunc wraps driver rows, e.g. to normalize scanned values.
func TextRows ¶ added in v0.1.21
TextRows is a RowsFunc returning the values of text columns as strings: a driver scans some of them (MySQL's text and DECIMAL, PostgreSQL's json, jsonb and xml) into []byte, which would serialize to clients as base64. The columns of the types in binary (as DatabaseTypeName reports them) stay bytes.
func TypedTextRows ¶ added in v0.1.21
TypedTextRows is TextRows with the values of the column types in unsigned read as uint64 however the driver typed them. MySQL's driver reads an unsigned BIGINT as uint64 for a statement without arguments (the text protocol), but as int64, or as text past int64's range, for one with them (the binary protocol): the type of a value, its JSON and the join key expand matches it by would depend on whether the request had a filter.
type Scanned ¶ added in v0.1.13
type Scanned struct {
Attrs map[TableKey][]entity.Attribute
Keys map[TableKey][]entity.Key
Indexes map[TableKey][]entity.Index
FKs map[TableKey][]entity.ForeignKey
Cascades map[TableKey][]entity.Cascade
SideEffects map[TableKey]bool
}
Scanned is the metadata of the scanned schemas, by relation, as the queries of a scan fill it: each reads one kind of metadata of every relation at once, the way mysqldump reads a database, so the round trips do not grow with the relations. The Scan methods read the rows the providers' queries have in common.
func (*Scanned) Fill ¶ added in v0.1.13
Fill sets on e what the scan found of k: its columns, keys, indexes, foreign keys, cascades and write effects.
func (*Scanned) ScanColumn ¶ added in v0.1.13
ScanColumn reads a row of a columns query selecting, in order, the schema, relation, column, type, nullability (YES or NO), comment, and whether the database supplies a value, computes it and takes it from a sequence; the rows come in column order.
func (*Scanned) ScanForeignKeyColumn ¶ added in v0.1.13
ScanForeignKeyColumn reads a row of a foreign keys query selecting, in key order, the schema, relation, constraint, column, referenced schema, relation and column, and the delete and update rules, which actions maps to the entity's (a rule it leaves out only checks).