dialect

package
v0.0.18 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package dialect renders the database-specific pieces of SQL. The planner never writes a quote or a placeholder itself.

Index

Constants

This section is empty.

Variables

View Source
var ColumnFunctionArity = map[string]int{
	"day_of_week": 0, "year": 0, "month": 0, "date": 0,
}

ColumnFunctionArity is the number of function arguments (not counting the compared value) each column function takes.

View Source
var ColumnFunctionTypes = map[string][]string{
	"day_of_week": {"date", "datetime"},
	"year":        {"date", "datetime"},
	"month":       {"date", "datetime"},
	"date":        {"date", "datetime"},
}

ColumnFunctionTypes lists the column types each ORM column function accepts.

View Source
var ValueFunctionUnits = map[string]string{
	"seconds_ago": "second", "minutes_ago": "minute", "hours_ago": "hour", "days_ago": "day", "months_ago": "month",
	"seconds_later": "second", "minutes_later": "minute", "hours_later": "hour", "days_later": "day", "months_later": "month",
}

ValueFunctionUnits maps relative value functions to their interval unit.

Functions

func IsValueFunction

func IsValueFunction(name string) bool

IsValueFunction reports whether name is a value function.

func Literal

func Literal(text string) string

Literal은 text를 SQL 문자열 literal로 quote한다.

func QuoteWith

func QuoteWith(q string, ident string) string

QuoteWith is a helper for dialects using a single quote character.

Types

type Dialect

type Dialect interface {
	Name() string
	// Quote an identifier.
	Quote(ident string) string
	// Placeholder for the n-th (1-based) bind.
	Placeholder(n int) string
	// Like renders the case-insensitive col LIKE pattern of contains.
	Like(col, ph string) string
	// Limit renders LIMIT/OFFSET.
	Limit(offset, count int) string
	// ForceIndex renders an index hint or "" when unsupported.
	ForceIndex(name string) string
	// RowNumber renders ROW_NUMBER() OVER (PARTITION BY p ORDER BY o).
	RowNumber(partition, orderBy string) string
	// InsertReturningID reports whether INSERT … RETURNING pk is used (else last insert id).
	InsertReturningID() bool
	// Upsert renders the ON DUPLICATE/ON CONFLICT clause for the given conflict columns and assignments.
	Upsert(conflict []string, assigns string) string
	// ReadExpr wraps SQL-side stages for a style pipeline and returns the bind count.
	ReadExpr(col string, styles []string, ph func() string) (string, int)
	// WriteExpr wraps a bound value for a style pipeline on write.
	WriteExpr(ph func() string, styles []string) (string, int)
	// Now은 precision 자리 소수 초로 column에 쓰는 database clock을 render한다:
	// MySQL은 p > 0이면 CURRENT_TIMESTAMP(p), 나머지는 CURRENT_TIMESTAMP다.
	Now(precision int) string
	// HandlesStyle은 쓰기에서 codec stage를 이 dialect의 SQL에서 적용하는지 알린다
	// (MySQL: hex, ip). 나머지 stage는 executor가 적용한다.
	HandlesStyle(style string) bool
	// DecodesStyle은 읽기에서 codec stage를 이 dialect의 SQL에서 푸는지 알린다(MySQL: ip).
	// hex는 어느 dialect도 SQL에서 풀지 않는다: UNHEX는 홀수 길이를 받고 공백이 있는 셀에 NULL을
	// 내므로, executor가 HostDecode로 정확히 읽는다(docs/codec.md).
	DecodesStyle(style string) bool
	// HostNow reports that the database has no sub-second clock function, so
	// timestamps written by the ORM (updated_ts) come from the executor (a `now` slot).
	HostNow() bool
	// RowLock renders a row lock suffix. An empty suffix with true means the
	// executor owns a portable adapter-level lock implementation.
	RowLock(mode string) (string, bool)
	// ColumnFunction renders an ORM column function applied to col; arg(i)
	// returns a new placeholder bound to the i-th function argument.
	ColumnFunction(name, col string, arg func(int) string) (string, bool)
	// ValueFunction renders an ORM value function; arg returns a placeholder
	// for the interval amount and now binds the executor clock. Both are called
	// in SQL text order.
	ValueFunction(name string, arg, now func() string) (string, bool)
	// ContainsBinary renders a case-sensitive substring match; value returns a
	// placeholder whose bound value gets the named transform.
	ContainsBinary(col string, value func(transform string) string) string
	// TupleIn renders a multi-column IN or NOT IN list.
	TupleIn(cols []string, rows [][]string, negate bool) string
	// Random renders the random ordering function.
	Random() string
	// NullsOrder renders the clause that sorts NULL before every value ascending and after every value descending:
	// MySQL and SQLite do so natively and render nothing, PostgreSQL sorts NULL last ascending and needs it.
	NullsOrder(desc bool) string
	// RestartIdentity은 table의 identity column을 가장 큰 key 다음 값에서 다시
	// 시작하는 statement다. 빈 table이면 다음 key는 1이다.
	RestartIdentity(table, column string) string
}

type MySQL

type MySQL struct{}

MySQL is the primary dialect. Style expressions are defined to preserve the stored byte representation AES is host-side; hex and IP packing remain SQL-side.

func (MySQL) ColumnFunction

func (MySQL) ColumnFunction(name, col string, arg func(int) string) (string, bool)

func (MySQL) ContainsBinary

func (MySQL) ContainsBinary(col string, value func(string) string) string

func (MySQL) DecodesStyle

func (MySQL) DecodesStyle(s string) bool

func (MySQL) ForceIndex

func (MySQL) ForceIndex(name string) string

func (MySQL) HandlesStyle

func (MySQL) HandlesStyle(s string) bool

AES is host-side so row version metadata can select the key before decode. hex and ip remain SQL-side because they do not depend on a secret.

func (MySQL) HostNow

func (MySQL) HostNow() bool

func (MySQL) InsertReturningID

func (MySQL) InsertReturningID() bool

func (MySQL) Like

func (MySQL) Like(col, ph string) string

func (MySQL) Limit

func (MySQL) Limit(offset, count int) string

func (MySQL) Name

func (MySQL) Name() string

func (MySQL) Now

func (MySQL) Now(precision int) string

func (MySQL) NullsOrder

func (MySQL) NullsOrder(bool) string

func (MySQL) Placeholder

func (MySQL) Placeholder(int) string

func (MySQL) Quote

func (MySQL) Quote(ident string) string

func (MySQL) Random

func (MySQL) Random() string

func (MySQL) ReadExpr

func (MySQL) ReadExpr(col string, styles []string, ph func() string) (string, int)

ReadExpr applies SQL-side stages. AES is handled by the executor so it can select the key from the row's version metadata.

func (MySQL) RestartIdentity

func (MySQL) RestartIdentity(table, _ string) string

RestartIdentity: InnoDB는 AUTO_INCREMENT를 column의 가장 큰 값 이하로 정하면 가장 큰 값에 1을 더한 값으로 정하므로 1은 언제나 가장 큰 key 다음이다. DDL이므로 열린 transaction을 commit한다.

func (MySQL) RowLock

func (MySQL) RowLock(mode string) (string, bool)

func (MySQL) RowNumber

func (MySQL) RowNumber(partition, orderBy string) string

func (MySQL) TupleIn

func (MySQL) TupleIn(cols []string, rows [][]string, negate bool) string

func (MySQL) Upsert

func (MySQL) Upsert(_ []string, assigns string) string

func (MySQL) ValueFunction

func (MySQL) ValueFunction(name string, arg, _ func() string) (string, bool)

func (MySQL) WriteExpr

func (MySQL) WriteExpr(ph func() string, styles []string) (string, int)

type Postgres

type Postgres struct{}

Postgres: "quoted" identifiers, $n placeholders, ILIKE for the case-insensitive LIKE MySQL gives by collation, ON CONFLICT upserts with RETURNING ids. AES and HEX have no SQL-side stage here: those styles stay client-side (host AES, S6).

func (Postgres) ColumnFunction

func (Postgres) ColumnFunction(name, col string, arg func(int) string) (string, bool)

func (Postgres) ContainsBinary

func (Postgres) ContainsBinary(col string, value func(string) string) string

func (Postgres) DecodesStyle

func (Postgres) DecodesStyle(string) bool

func (Postgres) ForceIndex

func (Postgres) ForceIndex(string) string

func (Postgres) HandlesStyle

func (Postgres) HandlesStyle(string) bool

HandlesStyle은 false다. ip stage도 bytea column에 넣을 packed byte를 executor가 만든다.

func (Postgres) HostNow

func (Postgres) HostNow() bool

func (Postgres) InsertReturningID

func (Postgres) InsertReturningID() bool

func (Postgres) Like

func (Postgres) Like(col, ph string) string

func (Postgres) Limit

func (Postgres) Limit(offset, count int) string

func (Postgres) Name

func (Postgres) Name() string

func (Postgres) Now

func (Postgres) Now(int) string

func (Postgres) NullsOrder

func (Postgres) NullsOrder(desc bool) string

func (Postgres) Placeholder

func (Postgres) Placeholder(n int) string

func (Postgres) Quote

func (Postgres) Quote(ident string) string

func (Postgres) Random

func (Postgres) Random() string

func (Postgres) ReadExpr

func (Postgres) ReadExpr(col string, _ []string, _ func() string) (string, int)

func (Postgres) RestartIdentity

func (Postgres) RestartIdentity(table, column string) string

RestartIdentity: identity sequence를 가장 큰 key에 1을 더한 값으로 정하고 is_called를 false로 두어 다음 nextval이 그 값을 돌려주게 한다. pg_get_serial_sequence의 첫 인자는 identifier로 읽히므로 quote한 table 이름이고, 둘째 인자는 column 이름 그대로다.

func (Postgres) RowLock

func (Postgres) RowLock(mode string) (string, bool)

func (Postgres) RowNumber

func (Postgres) RowNumber(partition, orderBy string) string

func (Postgres) TupleIn

func (Postgres) TupleIn(cols []string, rows [][]string, negate bool) string

func (Postgres) Upsert

func (Postgres) Upsert(conflict []string, assigns string) string

func (Postgres) ValueFunction

func (Postgres) ValueFunction(name string, arg, _ func() string) (string, bool)

func (Postgres) WriteExpr

func (Postgres) WriteExpr(ph func() string, _ []string) (string, int)

type SQLite

type SQLite struct{}

SQLite (3.35+ for RETURNING, 3.25+ for window functions). No fulltext outside FTS5 virtual tables, so `match` is rejected at compile time rather than approximated. Every style stage is client-side here (host AES, packed inet).

func (SQLite) ColumnFunction

func (SQLite) ColumnFunction(name, col string, arg func(int) string) (string, bool)

func (SQLite) ContainsBinary

func (SQLite) ContainsBinary(col string, value func(string) string) string

ContainsBinary uses instr because SQLite LIKE ignores case for ASCII.

func (SQLite) DecodesStyle

func (SQLite) DecodesStyle(string) bool

func (SQLite) ForceIndex

func (SQLite) ForceIndex(name string) string

func (SQLite) HandlesStyle

func (SQLite) HandlesStyle(string) bool

func (SQLite) HostNow

func (SQLite) HostNow() bool

func (SQLite) InsertReturningID

func (SQLite) InsertReturningID() bool

func (SQLite) Like

func (SQLite) Like(col, ph string) string

func (SQLite) Limit

func (SQLite) Limit(offset, count int) string

func (SQLite) Name

func (SQLite) Name() string

func (SQLite) Now

func (SQLite) Now(int) string

func (SQLite) NullsOrder

func (SQLite) NullsOrder(bool) string

func (SQLite) Placeholder

func (SQLite) Placeholder(int) string

func (SQLite) Quote

func (SQLite) Quote(ident string) string

func (SQLite) Random

func (SQLite) Random() string

func (SQLite) ReadExpr

func (SQLite) ReadExpr(col string, _ []string, _ func() string) (string, int)

func (SQLite) RestartIdentity

func (SQLite) RestartIdentity(table, column string) string

RestartIdentity: AUTOINCREMENT table의 다음 key는 sqlite_sequence의 seq에 1을 더한 값이므로 seq를 가장 큰 key로 정한다. table이 아직 행을 받은 적이 없어 sqlite_sequence에 행이 없으면 다음 key는 이미 가장 큰 key 다음이다.

func (SQLite) RowLock

func (SQLite) RowLock(mode string) (string, bool)

SQLite has no row-lock clause. Each executor implements the common lock modes with an ORM-owned transaction-scoped lock row; the SQL suffix remains empty because the physical lock is acquired by the adapter.

func (SQLite) RowNumber

func (SQLite) RowNumber(partition, orderBy string) string

func (SQLite) TupleIn

func (SQLite) TupleIn(cols []string, rows [][]string, negate bool) string

func (SQLite) Upsert

func (SQLite) Upsert(conflict []string, assigns string) string

func (SQLite) ValueFunction

func (SQLite) ValueFunction(name string, arg, now func() string) (string, bool)

ValueFunction binds the executor clock on SQLite and applies the interval with datetime modifiers; `floor` keeps the last valid day of the month. datetime은 초 단위를 돌려주므로 상대 형식은 두 번째 clock slot의 소수 여섯 자리를 붙인다. 한 statement에서 두 slot은 같다.

func (SQLite) WriteExpr

func (SQLite) WriteExpr(ph func() string, _ []string) (string, int)

Jump to

Keyboard shortcuts

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