Documentation
¶
Overview ¶
Package dialect renders the database-specific pieces of SQL. The planner never writes a quote or a placeholder itself.
Index ¶
- Variables
- func IsValueFunction(name string) bool
- func Literal(text string) string
- func QuoteWith(q string, ident string) string
- type Dialect
- type MySQL
- func (MySQL) ColumnFunction(name, col string, arg func(int) string) (string, bool)
- func (MySQL) ContainsBinary(col string, value func(string) string) string
- func (MySQL) DecodesStyle(s string) bool
- func (MySQL) ForceIndex(name string) string
- func (MySQL) HandlesStyle(s string) bool
- func (MySQL) HostNow() bool
- func (MySQL) InsertReturningID() bool
- func (MySQL) Like(col, ph string) string
- func (MySQL) Limit(offset, count int) string
- func (MySQL) Name() string
- func (MySQL) Now(precision int) string
- func (MySQL) NullsOrder(bool) string
- func (MySQL) Placeholder(int) string
- func (MySQL) Quote(ident string) string
- func (MySQL) Random() string
- func (MySQL) ReadExpr(col string, styles []string, ph func() string) (string, int)
- func (MySQL) RestartIdentity(table, _ string) string
- func (MySQL) RowLock(mode string) (string, bool)
- func (MySQL) RowNumber(partition, orderBy string) string
- func (MySQL) TupleIn(cols []string, rows [][]string, negate bool) string
- func (MySQL) Upsert(_ []string, assigns string) string
- func (MySQL) ValueFunction(name string, arg, _ func() string) (string, bool)
- func (MySQL) WriteExpr(ph func() string, styles []string) (string, int)
- type Postgres
- func (Postgres) ColumnFunction(name, col string, arg func(int) string) (string, bool)
- func (Postgres) ContainsBinary(col string, value func(string) string) string
- func (Postgres) DecodesStyle(string) bool
- func (Postgres) ForceIndex(string) string
- func (Postgres) HandlesStyle(string) bool
- func (Postgres) HostNow() bool
- func (Postgres) InsertReturningID() bool
- func (Postgres) Like(col, ph string) string
- func (Postgres) Limit(offset, count int) string
- func (Postgres) Name() string
- func (Postgres) Now(int) string
- func (Postgres) NullsOrder(desc bool) string
- func (Postgres) Placeholder(n int) string
- func (Postgres) Quote(ident string) string
- func (Postgres) Random() string
- func (Postgres) ReadExpr(col string, _ []string, _ func() string) (string, int)
- func (Postgres) RestartIdentity(table, column string) string
- func (Postgres) RowLock(mode string) (string, bool)
- func (Postgres) RowNumber(partition, orderBy string) string
- func (Postgres) TupleIn(cols []string, rows [][]string, negate bool) string
- func (Postgres) Upsert(conflict []string, assigns string) string
- func (Postgres) ValueFunction(name string, arg, _ func() string) (string, bool)
- func (Postgres) WriteExpr(ph func() string, _ []string) (string, int)
- type SQLite
- func (SQLite) ColumnFunction(name, col string, arg func(int) string) (string, bool)
- func (SQLite) ContainsBinary(col string, value func(string) string) string
- func (SQLite) DecodesStyle(string) bool
- func (SQLite) ForceIndex(name string) string
- func (SQLite) HandlesStyle(string) bool
- func (SQLite) HostNow() bool
- func (SQLite) InsertReturningID() bool
- func (SQLite) Like(col, ph string) string
- func (SQLite) Limit(offset, count int) string
- func (SQLite) Name() string
- func (SQLite) Now(int) string
- func (SQLite) NullsOrder(bool) string
- func (SQLite) Placeholder(int) string
- func (SQLite) Quote(ident string) string
- func (SQLite) Random() string
- func (SQLite) ReadExpr(col string, _ []string, _ func() string) (string, int)
- func (SQLite) RestartIdentity(table, column string) string
- func (SQLite) RowLock(mode string) (string, bool)
- func (SQLite) RowNumber(partition, orderBy string) string
- func (SQLite) TupleIn(cols []string, rows [][]string, negate bool) string
- func (SQLite) Upsert(conflict []string, assigns string) string
- func (SQLite) ValueFunction(name string, arg, now func() string) (string, bool)
- func (SQLite) WriteExpr(ph func() string, _ []string) (string, int)
Constants ¶
This section is empty.
Variables ¶
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.
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.
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 ¶
IsValueFunction reports whether name is a value function.
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) ContainsBinary ¶
func (MySQL) DecodesStyle ¶
func (MySQL) ForceIndex ¶
func (MySQL) HandlesStyle ¶
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) InsertReturningID ¶
func (MySQL) NullsOrder ¶
func (MySQL) Placeholder ¶
func (MySQL) ReadExpr ¶
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 ¶
RestartIdentity: InnoDB는 AUTO_INCREMENT를 column의 가장 큰 값 이하로 정하면 가장 큰 값에 1을 더한 값으로 정하므로 1은 언제나 가장 큰 key 다음이다. DDL이므로 열린 transaction을 commit한다.
func (MySQL) ValueFunction ¶
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) ContainsBinary ¶
func (Postgres) DecodesStyle ¶
func (Postgres) ForceIndex ¶
func (Postgres) HandlesStyle ¶
HandlesStyle은 false다. ip stage도 bytea column에 넣을 packed byte를 executor가 만든다.
func (Postgres) InsertReturningID ¶
func (Postgres) NullsOrder ¶
func (Postgres) Placeholder ¶
func (Postgres) RestartIdentity ¶
RestartIdentity: identity sequence를 가장 큰 key에 1을 더한 값으로 정하고 is_called를 false로 두어 다음 nextval이 그 값을 돌려주게 한다. pg_get_serial_sequence의 첫 인자는 identifier로 읽히므로 quote한 table 이름이고, 둘째 인자는 column 이름 그대로다.
func (Postgres) ValueFunction ¶
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) ContainsBinary ¶
ContainsBinary uses instr because SQLite LIKE ignores case for ASCII.
func (SQLite) DecodesStyle ¶
func (SQLite) ForceIndex ¶
func (SQLite) HandlesStyle ¶
func (SQLite) InsertReturningID ¶
func (SQLite) NullsOrder ¶
func (SQLite) Placeholder ¶
func (SQLite) RestartIdentity ¶
RestartIdentity: AUTOINCREMENT table의 다음 key는 sqlite_sequence의 seq에 1을 더한 값이므로 seq를 가장 큰 key로 정한다. table이 아직 행을 받은 적이 없어 sqlite_sequence에 행이 없으면 다음 key는 이미 가장 큰 key 다음이다.
func (SQLite) RowLock ¶
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) ValueFunction ¶
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은 같다.