database

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrContended = errors.New("database write contention")

ErrContended is returned when a write could not proceed because SQLite's single writer was held and busy_timeout expired. It is an availability signal, not a domain error: the HTTP boundary maps it to 503 Service Unavailable with a Retry-After header, never a 500.

Functions

func EscapeLIKE added in v0.4.0

func EscapeLIKE(s string) string

EscapeLIKE escapes SQL LIKE wildcards (`%`, `_`) and the escape character itself (`\`) in user-supplied input so it matches literally. Pair the returned value with an `ESCAPE '\'` clause in the query, e.g.:

sq.Expr(`col LIKE ? ESCAPE '\'`, "%"+database.EscapeLIKE(input)+"%")

func Exec added in v0.3.0

func Exec(ctx context.Context, db *sqlx.DB) sqlx.ExtContext

func WithContentionFlag added in v0.4.0

func WithContentionFlag(ctx context.Context) (context.Context, *atomic.Bool)

WithContentionFlag returns a context carrying a contention flag plus the flag itself. withinTx sets the flag when a write transaction fails with ErrContended; a caller (the HTTP layer) reads it after the work completes to translate the response — out of band, since the contention error is otherwise swallowed by handlers' own error mapping.

Types

type DB added in v0.3.0

type DB struct {
	// contains filtered or unexported fields
}

DB is the repo-facing handle. It has the full query surface + WithinTx.

func (*DB) ExecContext added in v0.3.0

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

func (*DB) GetContext added in v0.3.0

func (d *DB) GetContext(ctx context.Context, dest any, query string, args ...any) error

func (*DB) NamedExecContext added in v0.3.0

func (d *DB) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)

func (*DB) PreparexContext added in v0.4.0

func (d *DB) PreparexContext(ctx context.Context, query string) (*sqlx.Stmt, error)

PreparexContext prepares a statement against the ctx's transaction if one is present, else the pool. Reuse the returned *sqlx.Stmt to amortize statement compilation across many executions of the same query — e.g. a per-row insert inside a batch. The caller must Close it before the surrounding tx commits.

func (*DB) QueryRowxContext added in v0.3.0

func (d *DB) QueryRowxContext(ctx context.Context, query string, args ...any) *sqlx.Row

func (*DB) QueryxContext added in v0.3.0

func (d *DB) QueryxContext(ctx context.Context, query string, args ...any) (*sqlx.Rows, error)

func (*DB) Rebind added in v0.3.0

func (d *DB) Rebind(query string) string

Rebind translates `?` placeholders to whatever the underlying driver expects. Lets callers compose queries via sqlx.In (e.g. `WHERE id IN (?)`) without having to think about driver-specific bind styles.

func (*DB) SelectContext added in v0.3.0

func (d *DB) SelectContext(ctx context.Context, dest any, query string, args ...any) error

func (*DB) WithinTx added in v0.3.0

func (d *DB) WithinTx(ctx context.Context, fn func(context.Context) error) error

WithinTx available on DB so repos can scope their OWN atomic flows.

type DBTime

type DBTime struct {
	time.Time
}

DBTime is only needed for aggregate functions (MAX, MIN etc.) that return TEXT even for DATETIME columns. Regular columns declared as DATETIME scan directly into time.Time when using modernc.org/sqlite with _texttotime=1 in the DSN.

func (*DBTime) Scan

func (t *DBTime) Scan(value any) error

func (DBTime) Value

func (t DBTime) Value() (driver.Value, error)

type SQLite

type SQLite struct {
	// contains filtered or unexported fields
}

func NewSQLite

func NewSQLite(dbConf config.ConfDB) (*SQLite, error)

func (*SQLite) Close

func (s *SQLite) Close() error

func (*SQLite) DB

func (s *SQLite) DB() *DB

func (*SQLite) Migrate

func (s *SQLite) Migrate() error

func (*SQLite) Transactor added in v0.3.0

func (s *SQLite) Transactor() *Transactor

type Transactor added in v0.3.0

type Transactor struct {
	// contains filtered or unexported fields
}

Transactor is the service-facing handle. It exposes ONLY tx orchestration — no query methods. A service holding one can compose operations atomically but cannot perform SQL.

func NewTransactor added in v0.3.0

func NewTransactor(pool *sqlx.DB) *Transactor

func (*Transactor) WithinTx added in v0.3.0

func (t *Transactor) WithinTx(ctx context.Context, fn func(context.Context) error) error

Jump to

Keyboard shortcuts

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