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 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) 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) Tx(ctx context.Context, fn func(tx *Tx) error) (err error)
- type Dialect
- type Driver
- type Executor
- type Manager
- type Option
- type Tx
- func (tx *Tx) Dialect() Dialect
- 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) 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.
type Dialect ¶
type Dialect interface {
// Name is "sqlite" or "postgres".
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
}
Driver connects a dialect to a database/sql driver. PostgreSQL's comes from the drivers/postgres module: database.Open(ctx, url, postgres.Driver()).
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.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is a transaction; it implements Executor.
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). |