Documentation
¶
Overview ¶
Package database manages SQL connections for Vukem: dialects, SQLite defaults (WAL, busy timeout, foreign keys, a single-connection writer pool beside a reader pool), PostgreSQL and MySQL through driver modules, and transactions that roll back on error or panic.
Index ¶
- func RunInTx(ctx context.Context, ex Executor, fn func(tx *Tx) error) error
- type Config
- type DB
- func (db *DB) Close() error
- func (db *DB) Dialect() Dialect
- func (db *DB) Encrypter() Encrypter
- func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (db *DB) SQL() *sql.DB
- func (db *DB) SetQueryObserver(obs QueryObserver)
- func (db *DB) Tx(ctx context.Context, fn func(tx *Tx) error) (err error)
- type Dialect
- type Driver
- type Encrypter
- type Executor
- type Manager
- type Option
- type Query
- type QueryObserver
- type Tx
- func (tx *Tx) Dialect() Dialect
- func (tx *Tx) Encrypter() Encrypter
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (tx *Tx) SQL() *sql.Tx
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
MaxOpen int
MaxIdle int
MaxLifetime time.Duration
MaxIdleTime time.Duration
Log *slog.Logger
}
Config tunes connection pools.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a connection with its dialect. For SQLite, writes and transactions go to a single-connection writer pool and reads to a reader pool, which avoids SQLITE_BUSY under concurrency.
func Open ¶
Open connects to url:
- SQLite: "file:storage/app.db", "sqlite:storage/app.db", ":memory:";
- other schemes need their Driver (e.g. postgres.Driver()).
func (*DB) Close ¶
Close closes the pools. Always close databases before deleting their files (required on Windows).
func (*DB) Encrypter ¶ added in v0.18.0
Encrypter returns the encrypter set with WithEncrypter, or nil.
func (*DB) ExecContext ¶
ExecContext runs a statement on the writer pool.
func (*DB) QueryContext ¶
QueryContext runs a query on the reader pool.
func (*DB) QueryRowContext ¶
QueryRowContext runs a single-row query on the reader pool, except for statements with RETURNING, which write and go to the writer pool.
func (*DB) SetQueryObserver ¶ added in v0.19.0
func (db *DB) SetQueryObserver(obs QueryObserver)
SetQueryObserver sets the observer of a DB built with FromSQL or Open (call it before the DB is shared between goroutines).
type Dialect ¶
type Dialect interface {
// Name is "sqlite", "postgres" or "mysql".
Name() string
// Placeholder returns the n-th (1-based) bind placeholder.
Placeholder(n int) string
// Quote quotes an identifier (already validated by callers).
Quote(ident string) string
}
Dialect describes SQL differences between databases.
type Driver ¶
type Driver struct {
// SQLName is the database/sql driver name ("pgx").
SQLName string
// Dialect is the SQL dialect.
Dialect Dialect
// Schemes are the URL schemes the driver accepts ("postgres").
Schemes []string
// DSN, when set, turns the URL into the database/sql driver's data
// source name (MySQL's driver does not take URLs). Its errors must not
// echo the URL, which may hold a password.
DSN func(rawURL string) (string, error)
}
Driver connects a dialect to a database/sql driver. PostgreSQL's comes from the drivers/postgres module: database.Open(ctx, url, postgres.Driver()).
type Encrypter ¶ added in v0.18.0
type Encrypter interface {
Seal(plaintext, ad []byte) string
Open(token string, ad []byte) ([]byte, error)
}
Encrypter seals and opens column values for orm.Encrypted; *crypt.Encrypter implements it. ad (additional data) binds a value to its table and column.
type Executor ¶
type Executor interface {
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
Dialect() Dialect
}
Executor runs SQL; *DB and *Tx implement it, so ORM and schema code works inside and outside transactions.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager holds named connections. The default connection is "default".
func NewManager ¶
NewManager returns a Manager with db as the default connection.
type Option ¶
type Option func(*openOptions)
Option configures Open.
func WithDriver ¶
WithDriver registers a non-SQLite driver for this Open call.
func WithEncrypter ¶ added in v0.18.0
WithEncrypter makes orm.Encrypted columns readable and writable through this connection (apps pass app.Crypt). Without it, writing or reading an Encrypted column fails.
func WithQueryObserver ¶ added in v0.19.0
func WithQueryObserver(obs QueryObserver) Option
WithQueryObserver reports every statement to obs (development tooling such as devtools). Without it the hot path costs one nil check.
type Query ¶ added in v0.19.0
type Query struct {
// SQL is the statement text as passed by the caller.
SQL string
// Args are the bound values; observers must not log or display them
// (they hold user data and secrets).
Args []any
// Duration is the time until the driver returned (for queries: until
// the rows were ready, not until they were read).
Duration time.Duration
// Err is the statement error, if any.
Err error
}
Query is one statement run through a DB or Tx, reported to a QueryObserver after it returns.
type QueryObserver ¶ added in v0.19.0
QueryObserver is notified of every statement run through a DB opened with WithQueryObserver (and its transactions). It runs on the caller's goroutine, so it must be fast and safe for concurrent use. Statements run directly on DB.SQL() bypass it.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a transaction; it implements Executor.
func (*Tx) Encrypter ¶ added in v0.18.0
Encrypter returns the encrypter of the DB that started the transaction.
func (*Tx) ExecContext ¶
ExecContext implements Executor.
func (*Tx) QueryContext ¶
QueryContext implements Executor.
func (*Tx) QueryRowContext ¶
QueryRowContext implements Executor.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dbtest is the database conformance suite: the same schema, ORM, transaction and migration checks run against every dialect (SQLite in core, Postgres and others in their driver modules).
|
Package dbtest is the database conformance suite: the same schema, ORM, transaction and migration checks run against every dialect (SQLite in core, Postgres and others in their driver modules). |