Documentation
¶
Overview ¶
Package generic provides a database-agnostic implementation of the dialects.Dialect interface.
A Generic encoder turns dialects query ASTs into RawQueries by assembling the SQL that is common to all databases and delegating the database-specific details to a Core: identifier quoting, data type names, literal escaping, binding placeholders, and capability flags.
The concrete dialect packages (sqlite, mysql, and postgres) each supply a Core and wrap it in a Generic with New.
Index ¶
- Variables
- type Core
- type Generic
- func (g *Generic) EncodeAlterTableQuery(q *dialects.AlterTableQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeAny(v any) (dialects.RawQuery, error)
- func (g *Generic) EncodeColumn(c *dialects.Column) (dialects.RawQuery, error)
- func (g *Generic) EncodeColumnDefinition(c *dialects.ColumnDefinition) (dialects.RawQuery, error)
- func (g *Generic) EncodeConditions(c []dialects.Condition) (dialects.RawQuery, error)
- func (g *Generic) EncodeCreateTableQuery(q *dialects.CreateTableQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeDeleteQuery(q *dialects.DeleteQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeDropTableQuery(q *dialects.DropTableQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeForeignKey(f *dialects.ForeignKey) (dialects.RawQuery, error)
- func (g *Generic) EncodeFrom(from string) (dialects.RawQuery, error)
- func (g *Generic) EncodeFunctionCall(fc *dialects.FunctionCall) (dialects.RawQuery, error)
- func (g *Generic) EncodeGroupBy(groups []string) (dialects.RawQuery, error)
- func (g *Generic) EncodeHavings(c []dialects.Condition) (dialects.RawQuery, error)
- func (g *Generic) EncodeIndex(i *dialects.Index) (dialects.RawQuery, error)
- func (g *Generic) EncodeInsertQuery(q *dialects.InsertQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeJoin(j *dialects.Join) (dialects.RawQuery, error)
- func (g *Generic) EncodeJoins(joins []dialects.Join) (dialects.RawQuery, error)
- func (g *Generic) EncodeLimit(l *dialects.Limit) (dialects.RawQuery, error)
- func (g *Generic) EncodeLiteral(v any) (dialects.RawQuery, error)
- func (g *Generic) EncodeOrderBy(orderBys []dialects.OrderColumn) (dialects.RawQuery, error)
- func (g *Generic) EncodeSelectQuery(q *dialects.SelectQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeSelects(s *dialects.Select) (dialects.RawQuery, error)
- func (g *Generic) EncodeUpdateQuery(q *dialects.UpdateQuery) (dialects.RawQuery, error)
- func (g *Generic) EncodeUpdateSet(values map[string]any) (dialects.RawQuery, error)
- func (g *Generic) EncodeWheres(c []dialects.Condition) (dialects.RawQuery, error)
- func (g *Generic) Features() dialects.Features
Constants ¶
This section is empty.
Variables ¶
var ErrInsertMismatchedValueKeys = errors.New("mismatched value keys")
ErrInsertMismatchedValueKeys is returned by EncodeInsertQuery when the rows to insert have different sets of column names.
var ErrInsertNoRows = errors.New("no rows to insert")
ErrInsertNoRows is returned by EncodeInsertQuery when there are no rows to insert.
var ErrUnkownExprType = errors.New("unknown dialects.Expr type")
ErrUnkownExprType is returned when a value has a type that cannot be encoded as SQL.
Functions ¶
This section is empty.
Types ¶
type Core ¶
type Core interface {
Identifier(string) string
DataType(dialects.DataType) string
CurrentTime() string
AutoIncrement() string
Escape(v any) string
Binding() string
Features() dialects.Features
}
Core provides the database-specific behavior used by Generic to render SQL: identifier quoting, data type names, current-time literals, auto-increment clauses, literal escaping, binding placeholders, and capability flags.
type Generic ¶
type Generic struct {
// contains filtered or unexported fields
}
Generic encodes dialects query ASTs into RawQueries, using a Core for all database-specific syntax.
func (*Generic) EncodeAlterTableQuery ¶
EncodeAlterTableQuery renders q as one or more ALTER TABLE statements, one for each column, foreign key, or index change.
func (*Generic) EncodeAny ¶
EncodeAny renders a value, dispatching on its type: a QueryBuilder becomes a subquery, a []Condition a grouped condition, a Column a column reference, a RawQuery or RawString raw SQL, and anything else a literal binding.
func (*Generic) EncodeColumn ¶
EncodeColumn renders a single selectable expression: a column name, a function call, a subquery, or raw SQL, with an optional AS alias.
func (*Generic) EncodeColumnDefinition ¶
EncodeColumnDefinition renders a single column definition with its data type and constraints.
func (*Generic) EncodeConditions ¶
EncodeConditions renders the conditions joined by AND, or OR for a condition with Or set. A nil value with an = or != operator becomes IS NULL or IS NOT NULL, and a []any value becomes an IN (...) list.
func (*Generic) EncodeCreateTableQuery ¶
EncodeCreateTableQuery renders q as a CREATE TABLE statement followed by any index statements.
func (*Generic) EncodeDeleteQuery ¶
EncodeDeleteQuery renders q as a DELETE statement, including any WHERE clause.
func (*Generic) EncodeDropTableQuery ¶
return runQuery(ctx, tx, helpers.Concat(helpers.Raw("DROP TABLE IF EXISTS "), helpers.Identifier(table))) EncodeDropTableQuery renders q as a DROP TABLE statement, including IF EXISTS when set.
func (*Generic) EncodeForeignKey ¶
EncodeForeignKey renders a CONSTRAINT ... FOREIGN KEY clause.
func (*Generic) EncodeFrom ¶
EncodeFrom renders the FROM clause of a query, or an empty query if from is empty.
func (*Generic) EncodeFunctionCall ¶
EncodeFunctionCall renders a function applied to its arguments, such as count(*).
func (*Generic) EncodeGroupBy ¶
EncodeGroupBy renders the GROUP BY clause of a query, or nothing if there are no groups.
func (*Generic) EncodeHavings ¶
EncodeHavings renders the HAVING clause of a query, or nothing if there are no conditions.
func (*Generic) EncodeIndex ¶
EncodeIndex renders i as a CREATE [UNIQUE] INDEX statement.
func (*Generic) EncodeInsertQuery ¶
EncodeInsertQuery renders q as an INSERT statement. Column names are sorted for deterministic output, and RETURNING is added when Returning is set.
func (*Generic) EncodeJoin ¶
EncodeJoin renders a single join clause, including its ON conditions when any are set.
func (*Generic) EncodeJoins ¶
EncodeJoins renders all the joins of a query.
func (*Generic) EncodeLimit ¶
EncodeLimit renders the LIMIT and OFFSET of a query, or nothing if both are zero.
func (*Generic) EncodeLiteral ¶
EncodeLiteral renders v as a binding placeholder with the value as a binding.
func (*Generic) EncodeOrderBy ¶
EncodeOrderBy renders the ORDER BY clause of a query, or nothing if there are no order columns.
func (*Generic) EncodeSelectQuery ¶
EncodeSelectQuery renders q as a SELECT statement.
func (*Generic) EncodeSelects ¶
EncodeSelects renders the columns of a SELECT clause, prefixing DISTINCT when set.
func (*Generic) EncodeUpdateQuery ¶
EncodeUpdateQuery renders q as an UPDATE statement, including any WHERE clause and a RETURNING clause when Returning is set.
func (*Generic) EncodeUpdateSet ¶
EncodeUpdateSet renders the SET clause of an update with the columns sorted for deterministic output.
func (*Generic) EncodeWheres ¶
EncodeWheres renders the WHERE clause of a query, or nothing if there are no conditions.