sqldb

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: MPL-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sqldb wraps database/sql with the CMS's dialect translation.

Its method set deliberately mirrors pgx's — Query/QueryRow/Exec take a context first, and Tx.Commit/Rollback take one too — so store code reads the same regardless of which engine is underneath and did not have to be restructured when the CMS stopped being Postgres-only. Every statement passes through the dialect on its way to the driver, so stores can keep writing canonical Postgres SQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CollectRows

func CollectRows[T any](rows *sql.Rows, fn func(Scanner) (T, error)) ([]T, error)

CollectRows reads every row through fn and returns the results, closing rows and reporting any iteration error. It replaces pgx.CollectRows.

func JSON

func JSON(m map[string]string) driver.Valuer

JSON binds m to a JSON column. A nil map is stored as NULL, which the snippet store uses to mean "a plain block rather than a section preset".

pgx did this conversion itself; database/sql does not, so the JSON columns are wrapped explicitly at each bind and scan site.

func JSONInto

func JSONInto(m *map[string]string) sql.Scanner

JSONInto decodes a JSON column into *m. A NULL column leaves m nil.

Types

type Conn

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

Conn is a single reserved connection.

func (*Conn) Close

func (c *Conn) Close() error

Close returns the connection to the pool.

func (*Conn) Exec

func (c *Conn) Exec(ctx context.Context, query string, args ...any) (Result, error)

Exec runs a statement on this connection.

func (*Conn) Execer

func (c *Conn) Execer() dialect.Execer

Execer exposes the connection to the dialect helpers.

type DB

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

DB is a connection pool that speaks the CMS's canonical SQL.

func New

func New(db *sql.DB, d dialect.Dialect) *DB

New wraps an open *sql.DB with the given dialect.

func (*DB) Begin

func (db *DB) Begin(ctx context.Context) (*Tx, error)

Begin starts a transaction.

func (*DB) Conn

func (db *DB) Conn(ctx context.Context) (*Conn, error)

Conn reserves a single connection from the pool. Session-scoped state — an advisory lock, a SET — only means anything when taken and released on one connection, which a pool otherwise gives no guarantee of.

func (*DB) Dialect

func (db *DB) Dialect() dialect.Dialect

Dialect returns the dialect in use, for the few places that must render a fragment differently per engine.

func (*DB) Exec

func (db *DB) Exec(ctx context.Context, query string, args ...any) (Result, error)

func (*DB) InsertID

func (db *DB) InsertID(ctx context.Context, query string, args ...any) (int64, error)

InsertID runs an INSERT written *without* a RETURNING clause and reports the generated id. The dialect decides how: Postgres appends RETURNING id, MySQL reads LastInsertId.

func (*DB) Query

func (db *DB) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*DB) QueryRow

func (db *DB) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

func (*DB) SQL

func (db *DB) SQL() *sql.DB

SQL returns the underlying pool, for callers that need database/sql directly (the migration runner takes a dedicated connection from it).

type Result

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

Result reports how many rows a statement changed.

RowsAffected drops the error database/sql returns with it, matching the pgx command tag the CMS was written against. Both supported drivers always report it, and the count is only ever used to tell "no such row" from a successful write.

func (Result) RowsAffected

func (r Result) RowsAffected() int64

RowsAffected returns the number of rows the statement changed.

type Scanner

type Scanner interface {
	Scan(dest ...any) error
}

Scanner is the one-row interface the scan helpers accept. Both *sql.Row and *sql.Rows satisfy it, so a helper written for a single-row lookup also works inside CollectRows.

type Tx

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

Tx is an open transaction. Commit and Rollback take a context they do not use, so that store code reads identically to the pooled path.

func (*Tx) Commit

func (tx *Tx) Commit(context.Context) error

func (*Tx) Dialect

func (tx *Tx) Dialect() dialect.Dialect

Dialect returns the dialect in use, matching DB.Dialect so a statement built inside a transaction reads the same as one built outside it.

func (*Tx) Exec

func (tx *Tx) Exec(ctx context.Context, query string, args ...any) (Result, error)

func (*Tx) InsertID

func (tx *Tx) InsertID(ctx context.Context, query string, args ...any) (int64, error)

InsertID runs an INSERT without a RETURNING clause inside the transaction and reports the generated id.

func (*Tx) Query

func (tx *Tx) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

func (*Tx) Rollback

func (tx *Tx) Rollback(context.Context) error

Jump to

Keyboard shortcuts

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