sqladapter

package
v0.1.21 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2026 License: MIT Imports: 10 Imported by: 0

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

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

func SessionParams(params map[string]string) (map[string]string, error)

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) Dialect added in v0.1.13

func (b Base) Dialect() dialect.Dialect

Dialect returns the database's dialect.

func (Base) Explainer added in v0.1.13

func (b Base) Explainer() cost.Explainer

Explainer returns the EXPLAIN-based plan estimator.

func (Base) Introspector added in v0.1.13

func (b Base) Introspector() introspect.Introspector

Introspector returns the schema introspector.

func (Base) Profile added in v0.1.15

func (b Base) Profile() dialect.Profile

Profile is the connection's capabilities: its dialect's and its limits.

func (Base) WithLimits added in v0.1.15

func (b Base) WithLimits(limits dialect.Profile) Base

WithLimits returns b with the claims that depend on how its connection was opened (timeouts, native write refusals).

type ClassifyFunc

type ClassifyFunc func(error) error

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

func (d *Pool) BeginTx(ctx context.Context, opts *store.TxOptions) (store.Tx, error)

BeginTx implements store.TxBeginner.

func (*Pool) Classify added in v0.1.17

func (d *Pool) Classify(err error) error

Classify classifies a driver error of the pool's database, as its statements' errors are.

func (*Pool) Close

func (d *Pool) Close() error

Close closes the pool.

func (*Pool) DB

func (d *Pool) DB() *sql.DB

DB returns the underlying pool, for pool configuration and native pings.

func (*Pool) ExecContext

func (d *Pool) ExecContext(ctx context.Context, query string, args ...any) (store.Result, error)

ExecContext implements store.DB.

func (*Pool) Explain added in v0.1.21

func (d *Pool) Explain(ctx context.Context, query string, args []any, sep string) (string, error)

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

func (d *Pool) PrepareContext(ctx context.Context, query string) (store.Prepared, error)

PrepareContext implements store.Preparer.

func (*Pool) QueryContext

func (d *Pool) QueryContext(ctx context.Context, query string, args ...any) (store.Rows, error)

QueryContext implements store.DB.

type RowsFunc

type RowsFunc func(*sql.Rows) store.Rows

RowsFunc wraps driver rows, e.g. to normalize scanned values.

func TextRows added in v0.1.21

func TextRows(binary map[string]bool) RowsFunc

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

func TypedTextRows(binary, unsigned map[string]bool) RowsFunc

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 NewScanned added in v0.1.13

func NewScanned() *Scanned

NewScanned is an empty Scanned.

func (*Scanned) Fill added in v0.1.13

func (s *Scanned) Fill(k TableKey, e *entity.Entity)

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

func (s *Scanned) ScanColumn(rows *sql.Rows) error

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

func (s *Scanned) ScanForeignKeyColumn(rows *sql.Rows, actions map[string]string) error

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

func (*Scanned) ScanSideEffect added in v0.1.13

func (s *Scanned) ScanSideEffect(rows *sql.Rows) error

ScanSideEffect reads a row naming a relation (schema, relation) whose writes have effects the gateway cannot know: a trigger or a rule.

type TableKey added in v0.1.13

type TableKey struct{ Schema, Table string }

TableKey names a relation across the schemas of one scan.

Jump to

Keyboard shortcuts

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