Help us shape the future of Go! Take the 2026 Go Developer Survey

query

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package query composes SQL fragments and binds named values for its generated dialect. It uses only the standard library and does not execute SQL.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidName     = errors.New("invalid parameter name")
	ErrDuplicateBind   = errors.New("parameter already bound")
	ErrMissingBind     = errors.New("parameter has no binding")
	ErrUnusedBind      = errors.New("binding is not used in SQL")
	ErrEmptyList       = errors.New("cannot expand an empty list")
	ErrInvalidList     = errors.New("cannot expand a nested list")
	ErrSyntax          = errors.New("cannot scan SQL parameters")
	ErrMixedParameters = errors.New("use :name parameters instead of native placeholders")
)

Functions

func Scalar

func Scalar(value any) any

Scalar prevents list expansion for value. It does not convert the value or implement a database codec: the selected database driver must accept it.

Types

type Builder

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

Builder accumulates SQL and bindings. Its zero value is ready to use. Keep one builder per query; do not copy or mutate it concurrently with other calls. Bound values are retained, not deep-copied, and lists are expanded at Build time.

func New

func New() *Builder

New creates a builder configured for the dialect of the generated models.

func (*Builder) Bind

func (q *Builder) Bind(name string, value any) *Builder

Bind registers a case-sensitive name without the colon. Names use ASCII letters, digits and underscores, and must not begin with a digit. Duplicate/invalid names are recorded as errors and reported by Build and Preview, allowing chaining.

func (*Builder) Build

func (q *Builder) Build() (string, []any, error)

Build returns SQL with native placeholders and positional arguments in SQL order. It expands direct slices except byte slices and driver.Valuer implementations. Arrays and pointers remain scalar. Empty lists and unbound/unused names are errors. It does not evaluate Valuer methods, interpolate values, or validate SQL clauses. Every call compiles afresh and returns a new argument slice; values remain shared.

func (*Builder) Preview

func (q *Builder) Preview() (string, error)

Preview returns a debug string with the built SQL and numbered, typed arguments. It uses the same compilation as Build, never interpolates or executes SQL, and does not call driver.Valuer.Value. The result contains the actual bound values.

func (*Builder) Write

func (q *Builder) Write(fragment string) *Builder

Write appends a fragment verbatim. Successive fragments are separated by a newline. It neither adds nor validates SQL clauses. Errors from Bind are reported by Build.

Jump to

Keyboard shortcuts

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