generic

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
var ErrInsertMismatchedValueKeys = errors.New("mismatched value keys")

ErrInsertMismatchedValueKeys is returned by EncodeInsertQuery when the rows to insert have different sets of column names.

View Source
var ErrInsertNoRows = errors.New("no rows to insert")

ErrInsertNoRows is returned by EncodeInsertQuery when there are no rows to insert.

View Source
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 New

func New(c Core) *Generic

New returns a Generic dialect backed by core.

func (*Generic) EncodeAlterTableQuery

func (g *Generic) EncodeAlterTableQuery(q *dialects.AlterTableQuery) (dialects.RawQuery, error)

EncodeAlterTableQuery renders q as one or more ALTER TABLE statements, one for each column, foreign key, or index change.

func (*Generic) EncodeAny

func (g *Generic) EncodeAny(v any) (dialects.RawQuery, error)

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

func (g *Generic) EncodeColumn(c *dialects.Column) (dialects.RawQuery, error)

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

func (g *Generic) EncodeColumnDefinition(c *dialects.ColumnDefinition) (dialects.RawQuery, error)

EncodeColumnDefinition renders a single column definition with its data type and constraints.

func (*Generic) EncodeConditions

func (g *Generic) EncodeConditions(c []dialects.Condition) (dialects.RawQuery, error)

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

func (g *Generic) EncodeCreateTableQuery(q *dialects.CreateTableQuery) (dialects.RawQuery, error)

EncodeCreateTableQuery renders q as a CREATE TABLE statement followed by any index statements.

func (*Generic) EncodeDeleteQuery

func (g *Generic) EncodeDeleteQuery(q *dialects.DeleteQuery) (dialects.RawQuery, error)

EncodeDeleteQuery renders q as a DELETE statement, including any WHERE clause.

func (*Generic) EncodeDropTableQuery

func (g *Generic) EncodeDropTableQuery(q *dialects.DropTableQuery) (dialects.RawQuery, error)

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

func (g *Generic) EncodeForeignKey(f *dialects.ForeignKey) (dialects.RawQuery, error)

EncodeForeignKey renders a CONSTRAINT ... FOREIGN KEY clause.

func (*Generic) EncodeFrom

func (g *Generic) EncodeFrom(from string) (dialects.RawQuery, error)

EncodeFrom renders the FROM clause of a query, or an empty query if from is empty.

func (*Generic) EncodeFunctionCall

func (g *Generic) EncodeFunctionCall(fc *dialects.FunctionCall) (dialects.RawQuery, error)

EncodeFunctionCall renders a function applied to its arguments, such as count(*).

func (*Generic) EncodeGroupBy

func (g *Generic) EncodeGroupBy(groups []string) (dialects.RawQuery, error)

EncodeGroupBy renders the GROUP BY clause of a query, or nothing if there are no groups.

func (*Generic) EncodeHavings

func (g *Generic) EncodeHavings(c []dialects.Condition) (dialects.RawQuery, error)

EncodeHavings renders the HAVING clause of a query, or nothing if there are no conditions.

func (*Generic) EncodeIndex

func (g *Generic) EncodeIndex(i *dialects.Index) (dialects.RawQuery, error)

EncodeIndex renders i as a CREATE [UNIQUE] INDEX statement.

func (*Generic) EncodeInsertQuery

func (g *Generic) EncodeInsertQuery(q *dialects.InsertQuery) (dialects.RawQuery, error)

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

func (g *Generic) EncodeJoin(j *dialects.Join) (dialects.RawQuery, error)

EncodeJoin renders a single join clause, including its ON conditions when any are set.

func (*Generic) EncodeJoins

func (g *Generic) EncodeJoins(joins []dialects.Join) (dialects.RawQuery, error)

EncodeJoins renders all the joins of a query.

func (*Generic) EncodeLimit

func (g *Generic) EncodeLimit(l *dialects.Limit) (dialects.RawQuery, error)

EncodeLimit renders the LIMIT and OFFSET of a query, or nothing if both are zero.

func (*Generic) EncodeLiteral

func (g *Generic) EncodeLiteral(v any) (dialects.RawQuery, error)

EncodeLiteral renders v as a binding placeholder with the value as a binding.

func (*Generic) EncodeOrderBy

func (g *Generic) EncodeOrderBy(orderBys []dialects.OrderColumn) (dialects.RawQuery, error)

EncodeOrderBy renders the ORDER BY clause of a query, or nothing if there are no order columns.

func (*Generic) EncodeSelectQuery

func (g *Generic) EncodeSelectQuery(q *dialects.SelectQuery) (dialects.RawQuery, error)

EncodeSelectQuery renders q as a SELECT statement.

func (*Generic) EncodeSelects

func (g *Generic) EncodeSelects(s *dialects.Select) (dialects.RawQuery, error)

EncodeSelects renders the columns of a SELECT clause, prefixing DISTINCT when set.

func (*Generic) EncodeUpdateQuery

func (g *Generic) EncodeUpdateQuery(q *dialects.UpdateQuery) (dialects.RawQuery, error)

EncodeUpdateQuery renders q as an UPDATE statement, including any WHERE clause and a RETURNING clause when Returning is set.

func (*Generic) EncodeUpdateSet

func (g *Generic) EncodeUpdateSet(values map[string]any) (dialects.RawQuery, error)

EncodeUpdateSet renders the SET clause of an update with the columns sorted for deterministic output.

func (*Generic) EncodeWheres

func (g *Generic) EncodeWheres(c []dialects.Condition) (dialects.RawQuery, error)

EncodeWheres renders the WHERE clause of a query, or nothing if there are no conditions.

func (*Generic) Features

func (g *Generic) Features() dialects.Features

Features implements dialects.Dialect.

Jump to

Keyboard shortcuts

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