sqlbuild

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package sqlbuild assembles the handful of SQL statement shapes FSC's storage drivers need, together with their positional parameters.

It is deliberately not a query builder: there is no support for joins, aliases, DISTINCT, GROUP BY, HAVING or subqueries. Callers write the static parts of their statement as plain strings and use this package only for the parts that vary — parameter numbering, value tuples, WHERE conditions and pagination suffixes.

Placeholders are always $N, numbered in write order. Both PostgreSQL and modernc.org/sqlite bind $N by number, so the same statement text works on both.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoValues = errors.New("insert statements must have at least one set of values or select clause")

ErrNoValues reports an INSERT with no rows to insert. Callers return it instead of building the statement; Builder.WriteTuples panics if they do not. The message is squirrel's, so the error a caller surfaces for an empty insert is unchanged from before this package existed.

Functions

This section is empty.

Types

type Builder

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

Builder accumulates a SQL statement and its positional parameters. The zero value is not usable; call New.

func New

func New() *Builder

New returns an empty Builder.

func (*Builder) Build

func (b *Builder) Build() (string, []Param)

Build returns the accumulated statement and its parameters. The parameter slice is nil when the statement takes no parameters.

func (*Builder) WritePaging

func (b *Builder) WritePaging(p Paging) *Builder

WritePaging appends the ORDER BY, LIMIT and OFFSET clauses p calls for, in that order. LIMIT and OFFSET are written as literals rather than parameters, so they do not appear in the parameters Builder.Build returns.

func (*Builder) WriteParam

func (b *Builder) WriteParam(v Param) *Builder

WriteParam appends the next placeholder ($1, $2, …) and records v as its value.

func (*Builder) WriteString

func (b *Builder) WriteString(s string) *Builder

WriteString appends raw SQL text verbatim. The text is never escaped or inspected, so it must not contain caller-supplied values — use Builder.WriteParam for those.

func (*Builder) WriteTuples

func (b *Builder) WriteTuples(rows []Tuple) *Builder

WriteTuples appends the VALUES rows of an INSERT as ($1,$2),($3,$4), with no spaces around the separators. It panics when rows is empty: there is no SQL for a VALUES clause with no rows, so callers reject that with ErrNoValues before they start building.

func (*Builder) WriteWhere

func (b *Builder) WriteWhere(conds ...Condition) *Builder

WriteWhere appends " WHERE " followed by the non-nil conditions joined with " AND ", or nothing at all when none are given. The conditions are joined without wrapping parentheses; use And where a bracketed group is needed.

type CondFunc

type CondFunc func(*Builder)

CondFunc adapts a function to Condition. Use it for the rare fragment the constructors in this package do not cover, such as a scalar subquery.

func (CondFunc) WriteTo

func (f CondFunc) WriteTo(b *Builder)

WriteTo implements Condition.

type Condition

type Condition interface {
	WriteTo(*Builder)
}

Condition is a fragment of a WHERE clause. Implementations write themselves into a Builder, taking their placeholder numbers from it.

func And

func And(conds ...Condition) Condition

And renders "(a AND b AND …)", keeping the parentheses even for a single condition. nil conditions are skipped, so optional bounds can be passed through directly. With nothing left it renders the always-true "(1=1)".

func Eq

func Eq(col string, val Param) Condition

Eq renders "col = $n".

func Gt

func Gt(col string, val Param) Condition

Gt renders "col > $n".

func Gte

func Gte(col string, val Param) Condition

Gte renders "col >= $n".

func In

func In[V any](col string, vals ...V) Condition

In renders "col IN ($n,…)". A single value still renders as IN ($n) rather than "col = $n"; callers that want the equality form call Eq. With no values it renders the always-false "(1=0)", so an empty key set matches no rows.

func Lt

func Lt(col string, val Param) Condition

Lt renders "col < $n".

type Paging

type Paging struct {
	// OrderBy is the column to sort ascending by, or "" for no ORDER BY.
	OrderBy string
	// Limit caps the rows returned. A nil Limit — or one pointing at a negative
	// value — writes no LIMIT clause.
	Limit *int
	// Offset is the number of rows to skip. It is written only when Limit is
	// set and Offset is positive.
	Offset int
	// Where is the keyset cursor condition, or nil.
	Where Condition
}

Paging is the contribution a pagination makes to a SELECT statement.

The zero value contributes nothing, which is why Limit is a pointer rather than an int with a sentinel: forgetting to set it yields an unpaginated read rather than a silently empty result set. LIMIT 0 stays expressible as new(0), because it is a meaningful query that returns no rows.

Where is not written by Builder.WritePaging: it has to be merged into the statement's WHERE clause, which comes before ORDER BY, so the caller passes it to Builder.WriteWhere alongside its own conditions.

type Param

type Param = any

Param is a single positional statement parameter.

type Tuple

type Tuple = []Param

Tuple is one row of values for a multi-row INSERT.

Jump to

Keyboard shortcuts

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