sqlgen

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package sqlgen compiles named-parameter SQL into driver-specific positional SQL (§27.4). It is the seam at which database drivers differ: everything else in the repository pipeline is driver-neutral, and a driver adapter or plugin contributes only a Dialect. The package is build-time only and imports no driver.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compiled

type Compiled struct {
	// SQL is the query with named parameters replaced by dialect placeholders.
	SQL string
	// Params lists the named-parameter references in the order they appear,
	// e.g. ["id", "user.ID"]. Each occurrence produces one entry, so a name used
	// twice appears twice and is passed twice (dialect-uniform, since ?-style
	// dialects cannot reuse positions).
	Params []string
}

Compiled is the result of compiling named-parameter SQL.

func Compile

func Compile(sql string, dialect Dialect) Compiled

Compile rewrites `:name` and `:arg.field` references in the SQL into the dialect's positional placeholders and returns the ordered parameter list.

It skips references inside single-quoted string literals and treats `::` as a PostgreSQL type cast rather than a parameter. It never panics on arbitrary input.

type Dialect

type Dialect interface {
	// Name identifies the dialect, e.g. "postgres".
	Name() string
	// Placeholder renders the placeholder for the given 1-based position.
	Placeholder(index int) string
}

Dialect renders a positional placeholder for a database driver's SQL syntax.

var (
	// Postgres is the default dialect: $1, $2, ...
	Postgres Dialect = postgresDialect{}
	// Question uses ? placeholders, the generic positional style (SQLite).
	Question Dialect = questionDialect{}
	// MySQL uses ? placeholders (MySQL, go-sql-driver).
	MySQL Dialect = mysqlDialect{}
	// SQLServer uses @p1, @p2, ... placeholders (Microsoft SQL Server).
	SQLServer Dialect = sqlServerDialect{}
)

func DialectByName

func DialectByName(name string) (Dialect, bool)

DialectByName returns a built-in dialect by name and whether it exists.

Jump to

Keyboard shortcuts

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