sqlgen

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package sqlgen provides database-specific SQL dialect definitions, identifier quoting, string and value literal formatting, parameter placeholders, and bulk-load script generation for mockagen.

Index

Constants

View Source
const DefaultDialect = "postgres"

DefaultDialect is what an empty dialect field resolves to. Postgres' double-quoted identifier syntax and standard string quoting make it the most portable default across SQL tools.

View Source
const MaxRowsPerStatement = 1000

MaxRowsPerStatement caps the number of rows batched into a single multi-row INSERT statement.

1000 is not a round number chosen for comfort: it is SQL Server's hard limit on the rows a table value constructor may carry, so raising this breaks mssql outright no matter how much headroom the other three have. It also sits well beneath SQLite's variable limit, MySQL's max_allowed_packet and PostgreSQL's 1GB message ceiling.

Variables

View Source
var ErrEmbeddedNUL = errors.New("sqlgen: embedded NUL byte in literal")

ErrEmbeddedNUL is returned by AppendValue when a string or byte slice contains a NUL byte (\x00). The importer rejects NULs rather than silently dropping them (as the generator does) to prevent undetectable data loss.

Functions

func AppendString

func AppendString[T StringLike](dst []byte, d Dialect, s T) []byte

AppendString appends s as a quoted string literal in dialect d. Doubling quotes handles single-quote escaping across all dialects. Backslashes and control characters (\r, \n) are escaped only when EscapeBackslash is enabled (MySQL), ensuring raw CR characters are not swallowed by line-based ingestion. Embedded NUL bytes are dropped to preserve file loadability across engines.

func BulkLoadScript

func BulkLoadScript(d Dialect, table string, cols []string, csvName string, includeHeader bool) []byte

BulkLoadScript renders the dialect's bulk-load command for csvName, which is a bare file name resolved relative to wherever the script is run.

func Table

func Table(name, table string) string

Table picks the target SQL table name: table when set, otherwise derived from name with words joined by underscores and lowercased. When both are blank, it falls back to "mockagen".

Types

type Dialect

type Dialect struct {
	Name string
	// IdentOpen and IdentClose bracket a quoted identifier. All four
	// dialects escape a literal IdentClose inside the identifier by doubling
	// it. Identifiers are always quoted to avoid collision with keywords.
	IdentOpen  byte
	IdentClose byte
	// EscapeBackslash indicates that backslash is an escape character inside
	// a string literal (MySQL with default NO_BACKSLASH_ESCAPES off).
	EscapeBackslash bool
	// UnicodePrefix is written before a literal containing non-ASCII text.
	// SQL Server requires N'...' for nvarchar literals to prevent silent
	// replacement of characters with "?" during ingestion.
	UnicodePrefix string
	// PlaceholderStyle describes how parameterized queries format placeholders.
	PlaceholderStyle PlaceholderStyle
	// MaxBoundParams is the maximum number of query parameters supported by
	// a prepared statement in this dialect (e.g. 65535 postgres, 2100 mssql).
	MaxBoundParams int
	// TrueLiteral and FalseLiteral spell a boolean value. SQL Server has no
	// boolean type and no TRUE/FALSE keywords - a BIT column takes 1 and 0,
	// and an INSERT carrying the bare word TRUE is rejected by the server
	// rather than coerced. The other three accept TRUE and FALSE.
	TrueLiteral  string
	FalseLiteral string
}

Dialect encodes the syntactic divergences among the supported SQL database engines: identifier quoting, string escaping, parameter placeholders, parameter limits, and bulk-load scripts.

func Lookup

func Lookup(name string) (Dialect, error)

Lookup resolves a dialect name or alias. An unrecognised name produces an error rather than a silent fallback to prevent generating invalid SQL that only fails downstream when executed against the database.

func (Dialect) AppendBytes

func (d Dialect) AppendBytes(dst []byte, b []byte) []byte

AppendBytes appends b as a quoted string literal in dialect d.

func (Dialect) AppendIdent

func (d Dialect) AppendIdent(dst []byte, ident string) []byte

AppendIdent appends ident as a quoted identifier to dst.

func (Dialect) AppendPlaceholder

func (d Dialect) AppendPlaceholder(dst []byte, n int) []byte

AppendPlaceholder appends the 1-based n-th parameter placeholder to dst.

func (Dialect) AppendQualifiedIdent

func (d Dialect) AppendQualifiedIdent(dst []byte, name string) []byte

AppendQualifiedIdent appends a possibly schema-qualified table name, quoting each dotted component separately so that "dbo.users" refers to table "users" in schema "dbo" rather than a single table named "dbo.users".

func (Dialect) AppendString

func (d Dialect) AppendString(dst []byte, s string) []byte

AppendString appends s as a quoted string literal in dialect d.

func (Dialect) AppendValue

func (d Dialect) AppendValue(dst []byte, val any) ([]byte, error)

AppendValue formats val as a SQL literal in dialect d. Bare NULL is returned for nil, numbers are formatted without quotes, Raw expressions are emitted verbatim, and strings are escaped. Unlike AppendString, an embedded NUL byte returns ErrEmbeddedNUL to prevent silent data corruption. On error dst is returned unchanged rather than as a nil slice, so that a caller writing `dst, err = d.AppendValue(dst, v)` still holds the statement it had built - otherwise the error destroys the context needed to report it.

func (Dialect) IdentList

func (d Dialect) IdentList(cols []string) string

IdentList renders cols as a quoted, comma-separated column list.

func (Dialect) Placeholder

func (d Dialect) Placeholder(n int) string

Placeholder returns the 1-based n-th parameter placeholder for dialect d.

type PlaceholderStyle

type PlaceholderStyle string

PlaceholderStyle specifies the syntax used by a database dialect for bound query parameters ($1, ?, or @p1).

const (
	PlaceholderDollar   PlaceholderStyle = "$1"
	PlaceholderQuestion PlaceholderStyle = "?"
	PlaceholderAtP      PlaceholderStyle = "@p1"
)

type Raw

type Raw string

Raw represents a pre-formatted SQL fragment that is appended verbatim by AppendValue rather than escaped as a literal.

type StringLike

type StringLike interface {
	~string | ~[]byte
}

StringLike constraints types accepted by AppendString to string and []byte.

Jump to

Keyboard shortcuts

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