Documentation
¶
Overview ¶
Package sqlbind provides generated, reflection-free database/sql row mapping.
Index ¶
- Variables
- func AppendValues[T any](b *Builder, values []T) error
- func Bool(row Row, column string) (bool, error)
- func Float64(row Row, column string) (float64, error)
- func ForEach(rows *sql.Rows, fn func(Row) error) error
- func Int(row Row, column string) (int, error)
- func Int64(row Row, column string) (int64, error)
- func Key(row Row, column string) (key string, present bool, err error)
- func RegisterScanRows[T any](fn func(*sql.Rows) ([]T, error))
- func RequiredKey(row Row, column string) (string, error)
- func ScanOptionalURL(target **url.URL) optionalURLTarget
- func ScanRows[T any](rows *sql.Rows) ([]T, error)
- func ScanURL(target *url.URL) urlTarget
- func String(row Row, column string) (string, error)
- func WithSQLExecutor(ctx context.Context, executor SQLExecutor, options ...ExecutorOption) context.Context
- type Builder
- type Execer
- type ExecutorOption
- type PlaceholderStyle
- type Querier
- type Row
- type SQLExecutor
- type Statement
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyValueList = errors.New("tinybind SQL: an expanded value list cannot be empty")
ErrEmptyValueList reports that an expanded value list had no elements, which would produce invalid SQL such as "IN ()".
var ErrNoSQLExecutor = errors.New("sqlbind: no SQL executor in context")
ErrNoSQLExecutor reports that a Context does not contain a database executor. It is returned instead of panicking so generated Context wrappers remain ordinary error-returning APIs.
var ErrReadOnlyExecutor = errors.New("sqlbind: SQL executor in context is read-only")
ErrReadOnlyExecutor reports that a write statement resolved an executor the caller declared read-only. It is returned before the statement is built, so a misrouted write fails deterministically instead of depending on whether the connection happens to reach a read replica.
Functions ¶
func AppendValues ¶ added in v0.1.16
AppendValues expands a slice into a comma-separated placeholder list. It is a package function because a Go method cannot introduce its own type parameter.
func RegisterScanRows ¶
RegisterScanRows registers a generated SQL tree scanner for T.
func RequiredKey ¶
RequiredKey is Key with NULL rejected for a root object.
func ScanOptionalURL ¶ added in v0.2.2
ScanOptionalURL is ScanURL for an optional field, where SQL NULL leaves a nil pointer instead of failing.
func ScanURL ¶ added in v0.2.2
ScanURL adapts a url.URL field to Rows.Scan. Generated code passes it in place of the field address, so the column is parsed rather than assigned.
func WithSQLExecutor ¶ added in v0.1.6
func WithSQLExecutor(ctx context.Context, executor SQLExecutor, options ...ExecutorOption) context.Context
WithSQLExecutor returns a child Context containing a database executor. Framework transaction middleware can store a *sql.Tx for generated <Component>Context wrappers to resolve.
Types ¶
type Builder ¶ added in v0.1.16
Builder accumulates SQL text and its bound arguments. Generated statement builders write into it; template authors never construct placeholders themselves.
func NewBuilder ¶ added in v0.1.16
func NewBuilder(style PlaceholderStyle) Builder
NewBuilder returns a Builder emitting placeholders in the given style.
type Execer ¶ added in v0.1.16
Execer is the minimal executor a generated sql.exec component needs. It is satisfied by *sql.DB, *sql.Conn, and *sql.Tx.
type ExecutorOption ¶ added in v0.2.1
type ExecutorOption func(*sqlExecutorEntry)
ExecutorOption configures how a stored executor may be used.
func AsReadOnly ¶ added in v0.2.1
func AsReadOnly() ExecutorOption
AsReadOnly marks the stored executor as read-only, so a generated write statement resolving it fails with ErrReadOnlyExecutor. Use it for a handle pointing at a read replica, or for a transaction begun with sql.TxOptions{ReadOnly: true}.
type PlaceholderStyle ¶ added in v0.1.16
type PlaceholderStyle int
PlaceholderStyle selects the bind-placeholder syntax of the target dialect. Generated code passes the style chosen at generation time, so one Builder implementation serves every dialect.
const ( // Dollar emits PostgreSQL-style $1, $2, ... placeholders. Dollar PlaceholderStyle = iota // Question emits ?-style placeholders. Question )
type Querier ¶ added in v0.1.16
Querier is the minimal executor a generated row-returning component needs. It is satisfied by *sql.DB, *sql.Conn, and *sql.Tx.
type SQLExecutor ¶ added in v0.1.6
SQLExecutor is implemented by *sql.DB, *sql.Conn, and *sql.Tx. It combines the two minimal interfaces generated code takes, so one Context value serves both mutating and row-returning components.
func SQLExecutorFromContext ¶ added in v0.1.6
func SQLExecutorFromContext(ctx context.Context) (SQLExecutor, error)
SQLExecutorFromContext returns the executor installed by WithSQLExecutor. It is the resolver generated read statements use, and it accepts a read-only executor.
func WriteExecutorFromContext ¶ added in v0.2.1
func WriteExecutorFromContext(ctx context.Context, statement string) (SQLExecutor, error)
WriteExecutorFromContext is the resolver generated write statements use. It rejects an executor stored with AsReadOnly, naming the statement so the error identifies which one was misrouted.