sqladapter

package
v0.1.13 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 8 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.

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.

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

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