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 ¶
- func CollectRows[T any](rows *sql.Rows, fn func(Scanner) (T, error)) ([]T, error)
- func JSON(m map[string]string) driver.Valuer
- func JSONInto(m *map[string]string) sql.Scanner
- type Conn
- type DB
- func (db *DB) Begin(ctx context.Context) (*Tx, error)
- func (db *DB) Conn(ctx context.Context) (*Conn, error)
- func (db *DB) Dialect() dialect.Dialect
- func (db *DB) Exec(ctx context.Context, query string, args ...any) (Result, error)
- func (db *DB) InsertID(ctx context.Context, query string, args ...any) (int64, error)
- func (db *DB) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryRow(ctx context.Context, query string, args ...any) *sql.Row
- func (db *DB) SQL() *sql.DB
- type Result
- type Scanner
- type Tx
- func (tx *Tx) Commit(context.Context) error
- func (tx *Tx) Dialect() dialect.Dialect
- func (tx *Tx) Exec(ctx context.Context, query string, args ...any) (Result, error)
- func (tx *Tx) InsertID(ctx context.Context, query string, args ...any) (int64, error)
- func (tx *Tx) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryRow(ctx context.Context, query string, args ...any) *sql.Row
- func (tx *Tx) Rollback(context.Context) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollectRows ¶
CollectRows reads every row through fn and returns the results, closing rows and reporting any iteration error. It replaces pgx.CollectRows.
Types ¶
type Conn ¶
type Conn struct {
// contains filtered or unexported fields
}
Conn is a single reserved connection.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a connection pool that speaks the CMS's canonical SQL.
func (*DB) Conn ¶
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 ¶
Dialect returns the dialect in use, for the few places that must render a fragment differently per engine.
func (*DB) InsertID ¶
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.
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 ¶
RowsAffected returns the number of rows the statement changed.
type Scanner ¶
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) 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) InsertID ¶
InsertID runs an INSERT without a RETURNING clause inside the transaction and reports the generated id.