sqlgen

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package sqlgen generates SQL for liteorm's core. It is built around a Dialect interface plus a Feature capability bitmask and clause-assembly logic, and always emits BOUND placeholders (never inlined literals). The four dialect renderers live in dialects.go; all four render correctly under sqlgen_test.go, and SQLite is the dialect wired to a live DB.

Index

Constants

This section is empty.

Variables

View Source
var (
	SQLite   dialect.Dialect = sqliteDialect{}
	Postgres dialect.Dialect = postgresDialect{}
	MySQL    dialect.Dialect = mysqlDialect{}
	MSSQL    dialect.Dialect = mssqlDialect{}
)

The four render-only dialects. Each implements dialect.Dialect. SQLite is the dialect wired to a live DB; the other three exist so the builder's correctness is proven across placeholder/quote/limit/upsert/returning divergences in sqlgen_test.go.

Functions

This section is empty.

Types

type CTE

type CTE struct {
	Name      string
	Recursive bool
	Select    Select
}

CTE is one named common table expression in a WITH clause. Recursive marks the whole WITH as RECURSIVE (the keyword is emitted once if any CTE sets it).

type Column

type Column struct {
	Table string
	Name  string
}

Column is a (optionally table-qualified) identifier in a SELECT list. Both parts are quoted via the dialect; Table is omitted when empty.

type CompoundTerm

type CompoundTerm struct {
	Op     string
	All    bool
	Select Select
}

CompoundTerm is one arm of a compound select. Op is the set operator ("UNION", "INTERSECT", "EXCEPT"; empty means UNION); All keeps duplicates.

type Conflict

type Conflict struct {
	Columns []string
	Update  []string
	// Nothing renders ON CONFLICT ... DO NOTHING (ignore the conflicting row)
	// instead of DO UPDATE — the typed form of INSERT OR IGNORE. On MySQL it is a
	// no-op ON DUPLICATE KEY UPDATE; on SQL Server, a MERGE with no WHEN MATCHED arm.
	Nothing bool
}

Conflict is the upsert spec for an INSERT. Columns are the conflict-target columns; Update are the columns to overwrite on conflict.

type Delete

type Delete struct {
	Table     string
	Where     []Expr
	Returning []string
}

Delete is a DELETE statement model.

func (Delete) Build

func (del Delete) Build(d dialect.Dialect) (string, []any, error)

Build renders del.

type Expr

type Expr struct {
	SQL  string
	Args []any
}

Expr is a raw SQL fragment carrying positional "?" markers plus their args. The builder rewrites each "?" to the dialect's placeholder and appends args in order — this is how a single "?"-based fragment renders as $n (pg) or @pN (mssql).

type Insert

type Insert struct {
	Table      string
	Columns    []string
	Rows       [][]any
	OnConflict *Conflict
	Returning  []string
}

Insert is an INSERT statement model with optional multi-row VALUES, upsert, and RETURNING/OUTPUT. Identifiers are quoted; values bind as placeholders.

func (Insert) Build

func (ins Insert) Build(d dialect.Dialect) (string, []any, error)

Build renders ins for the dialect. Upsert syntax and the returning-clause shape are selected from the dialect's Feature bits, then the clause is assembled here.

type Lock

type Lock struct {
	Strength   string
	SkipLocked bool
	NoWait     bool
}

Lock models SELECT row locking. Strength is "UPDATE" or "SHARE"; at most one of SkipLocked / NoWait applies (NoWait wins if both are set).

type Select

type Select struct {
	With         []CTE   // WITH common table expressions, prepended to the statement
	Table        string  // the FROM table (quoted); ignored when FromSubquery is set
	FromSubquery *Select // a derived-table FROM source: FROM (sub) AS FromAlias
	FromAlias    string
	Columns      []Column
	Projection   []string // a raw select list (verbatim, no binds); overridden by ProjectionExprs
	// ProjectionExprs is a select list whose items may carry "?" binds (e.g. a
	// scalar subquery in SELECT). It renders before FROM so its binds number first;
	// it takes precedence over Projection and Columns.
	ProjectionExprs []Expr
	Distinct        bool
	DistinctOn      []string // SELECT DISTINCT ON (cols) — Postgres; overrides Distinct
	Joins           []Expr
	Where           []Expr
	GroupBy         []string
	Having          []Expr
	OrderBy         []string
	Limit           int
	Offset          int
	HasLimit        bool
	HasOffset       bool
	Union           []CompoundTerm
	Lock            *Lock // row-level locking (FOR UPDATE/SHARE), applied to the whole statement
}

Select is a SELECT statement model. Identifiers in Columns and Table are quoted; Joins/Where/OrderBy/GroupBy are raw SQL fragments (Joins/Where/Having may carry "?"). When Projection is non-empty it is the raw select list (used for count(*) and projections), overriding Columns. Union holds compound arms; the outer Select's ORDER BY/LIMIT apply to the whole compound.

func (Select) Build

func (s Select) Build(d dialect.Dialect) (string, []any, error)

Build renders s for the dialect, returning the SQL and ordered args. When s has Union arms it renders `core UNION [ALL] core …` and applies the outer ORDER BY/LIMIT to the whole compound. Placeholder numbering (n) is global across the join, where, having, and every union arm — so a single "?"-based model renders as $1,$2,… in order on Postgres.

type SetClause

type SetClause struct {
	Column string
	Arg    any
	SQL    string
	Args   []any
}

SetClause is one column assignment in an UPDATE. SetClause assigns a column. By default it binds Arg as a placeholder (col = ?); when SQL is non-empty it is a raw assignment expression (col = SQL, which may carry "?" markers bound by Args) — used for correlated UPDATE … FROM where the value comes from the FROM source.

type Update

type Update struct {
	Table     string
	Set       []SetClause
	From      *Expr
	Where     []Expr
	Returning []string
}

Update is an UPDATE statement model. From, when set, is a correlated `FROM <source>` fragment (UPDATE … FROM); it may carry "?" markers.

func (Update) Build

func (u Update) Build(d dialect.Dialect) (string, []any, error)

Build renders u. Args bind in clause order: SET, then FROM, then WHERE.

Jump to

Keyboard shortcuts

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