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

frag

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2026 License: MIT Imports: 15 Imported by: 1

Documentation

Overview

Package frag builds and flattens SQL fragments with bound arguments.

A Fragment yields query text chunks and args via Fragment.Frag. Collect concatenates them into a final query string and argument list for database/sql execution. [builder] statements are Fragments; this package is the low-level composition layer underneath.

q, args := frag.Collect(ctx, frag.Query("SELECT * FROM t WHERE id = ?", 1))
// q = "SELECT * FROM t WHERE id = ?"  args = [1]

Core types

Constructors

  • Query: template with `?` positional and `@name` named placeholders (NamedArg / NamedArgs). Nested Fragments, slices, and sequences expand in place (e.g. `IN (?)` with []int → `IN (?,?,?)`).

  • Lit / Empty: literal SQL with no args / empty fragment

  • Arg / ArgIter / ArgIterFunc / Values: expand a value into placeholders; supports CustomValueArg, driver.Valuer, slices, seqs

  • Func: wrap a context-aware iterator factory as a Fragment

    frag.Query("IN (?)", []int{1, 2, 3}) // IN (?,?,?) [1 2 3]

    frag.Query("time > @min AND time < @max", sql.Named("min", 10), sql.Named("max", 20)) // time > ? AND time < ? [10, 20]

Composition

Other

  • Alias: stable short alias for table__column names

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alias

func Alias(table, column string) string

Alias generates a deterministic alias name for a given table and column. If the combined length is shorter than 64 characters (minus separators), it returns a simple "table__column" alias. Otherwise, it generates a hashed form using a cached SHA1-based abbreviation to keep the alias short and stable.

func Collect

func Collect(ctx context.Context, f Fragment) (string, []any)

Collect flattens f into a query string and argument list.

func IsNil

func IsNil(e Fragment) bool

IsNil returns if a Fragment is nil

func NonNil

func NonNil[F Fragment](seq iter.Seq[F]) iter.Seq[Fragment]

NonNil filter nil fragments

func Stringify added in v0.3.1

func Stringify(f Fragment) string

Stringify returns a debug representation of f as "query | args".

Types

type CustomValueArg

type CustomValueArg interface {
	ValueEx() string
}

CustomValueArg supports replacing ? to some sql snippet eg: ? => UPPER(?)

type Fragment

type Fragment interface {
	// IsNil reports whether the fragment is empty.
	IsNil() bool
	// Frag yields SQL text chunks and their bound arguments.
	Frag(ctx context.Context) Iter
}

Fragment is a SQL fragment that yields query pieces and arguments.

func Arg

func Arg(v any) Fragment

Arg presents a single argument fragment

func ArgIterFunc

func ArgIterFunc(v any) Fragment

ArgIterFunc wraps ArgIter(v) as a Fragment.

func Block

func Block(f Fragment) Fragment

Block returns a bracketed Fragment eg:

select ... from ... => (select ... from ...)

func BlockWithoutBrackets

func BlockWithoutBrackets(seq iter.Seq[Fragment]) Fragment

BlockWithoutBrackets joins fragments with commas, without surrounding parentheses.

func Compose

func Compose(sep string, frags ...Fragment) Fragment

Compose joins frags with sep, skipping nil fragments.

func ComposeSeq

func ComposeSeq(sep string, frags iter.Seq[Fragment]) Fragment

ComposeSeq joins an iterator of fragments with sep, skipping nil fragments.

func Empty

func Empty() Fragment

Empty is an empty fragment (IsNil reports true).

func Func

func Func(f func(context.Context) Iter) Fragment

Func wraps a context-aware iterator factory as a Fragment.

func Lit

func Lit(q string) Fragment

Lit creates a literal SQL snippet with no arguments.

func Query

func Query(query string, args ...any) Fragment

Query creates a Fragment from a query template. Use `?` for positional args and `@name` for named args (NamedArg / NamedArgs).

type Iter

type Iter = iter.Seq2[string, []any]

Iter will yield a SQL fragment with a raw query(string with holder) and values eg:

query = INSERT INTO t_table (f_x,f_y,f_z) VALUES (?,?,?)
args  = 1, '2', 3.0

func ArgIter

func ArgIter(ctx context.Context, v any) Iter

ArgIter expands v into SQL placeholders and arguments. It supports Fragment, CustomValueArg, driver.Valuer, slices, and sequences.

type NamedArg

type NamedArg = sql.NamedArg

NamedArg is a named SQL argument.

type NamedArgs

type NamedArgs map[string]any

NamedArgs is a map of named argument values for @name placeholders.

type Values

type Values[T any] iter.Seq[T]

Values expands a sequence into comma-separated `?` placeholders.

func (Values[T]) Frag

func (vs Values[T]) Frag(_ context.Context) Iter

func (Values[T]) IsNil

func (vs Values[T]) IsNil() bool

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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