sqlbind

package
v0.1.19 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package sqlbind provides generated, reflection-free database/sql row mapping.

Index

Constants

This section is empty.

Variables

View Source
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 ()".

View Source
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.

Functions

func AppendValues added in v0.1.16

func AppendValues[T any](b *Builder, values []T) error

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 Bool

func Bool(row Row, column string) (bool, error)

func Float64

func Float64(row Row, column string) (float64, error)

func ForEach

func ForEach(rows *sql.Rows, fn func(Row) error) error

ForEach scans rows without retaining the full result.

func Int

func Int(row Row, column string) (int, error)

func Int64

func Int64(row Row, column string) (int64, error)

func Key

func Key(row Row, column string) (key string, present bool, err error)

Key returns a stable grouping key. present is false for SQL NULL.

func RegisterScanRows

func RegisterScanRows[T any](fn func(*sql.Rows) ([]T, error))

RegisterScanRows registers a generated SQL tree scanner for T.

func RequiredKey

func RequiredKey(row Row, column string) (string, error)

RequiredKey is Key with NULL rejected for a root object.

func ScanRows

func ScanRows[T any](rows *sql.Rows) ([]T, error)

ScanRows maps joined SQL rows into a grouped object tree using generated code.

func String

func String(row Row, column string) (string, error)

func WithSQLExecutor added in v0.1.6

func WithSQLExecutor(ctx context.Context, executor SQLExecutor) 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

type Builder struct {
	strings.Builder
	// contains filtered or unexported fields
}

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.

func (*Builder) Arg added in v0.1.16

func (b *Builder) Arg(value any)

Arg binds one value and writes its placeholder. Appending the argument and emitting its placeholder is a single operation, so numbering always matches argument order.

func (*Builder) Statement added in v0.1.16

func (b *Builder) Statement() Statement

Statement returns the accumulated SQL and arguments.

type Execer added in v0.1.16

type Execer interface {
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

Execer is the minimal executor a generated sql.exec component needs. It is satisfied by *sql.DB, *sql.Conn, and *sql.Tx.

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

type Querier interface {
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
}

Querier is the minimal executor a generated row-returning component needs. It is satisfied by *sql.DB, *sql.Conn, and *sql.Tx.

type Row

type Row map[string]any

Row is a SQL row indexed by result column name.

type SQLExecutor added in v0.1.6

type SQLExecutor interface {
	Execer
	Querier
}

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.

type Statement added in v0.1.16

type Statement struct {
	SQL  string
	Args []any
}

Statement is the low-level result of a generated SQL component: the compiled SQL text plus its bound arguments, with no database handle attached.

Jump to

Keyboard shortcuts

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