dialect

package
v0.6.28 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package dialect spells the parts of SQL that differ between engines.

Everything here returns a fragment of SQL text, so a caller composes with it the same way it composes with any other string and never branches on the engine itself. A caller that finds itself writing "if postgres" has found a spelling that belongs in this package.

The fragments assume the loose typing that both SQLite and Postgres allow for a column declared to hold JSON: the value may be a JSON array, or a bare scalar that should read as a one-element array. Each JSON fragment normalizes that before it does anything else, so Dialect.Each over a column holding "abc" yields one row, not an error.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect interface {
	// Name is the engine, "sqlite" or "postgres".
	Name() string

	// Quote wraps an identifier so it survives a keyword collision or an
	// uppercase letter.
	Quote(name string) string

	// Now is the current UTC instant in Base's stored layout,
	// 2006-01-02 15:04:05.000Z.
	Now() string

	// Random is n lowercase hexadecimal characters. n must be even.
	Random(n int) string

	// Json is the column type for a value holding JSON.
	Json() string

	// Bytes is the column type for a value holding opaque binary.
	Bytes() string

	// Like is the operator matching a value against a pattern without regard
	// to case, so one filter selects the same rows on either engine.
	Like() string

	// Bool is the literal for a truth value.
	Bool(v bool) string

	// Number reads expr — a value read out of a JSON document, which arrives
	// as text whatever the document held — as a number, for an engine that
	// compares a number only to a number. Text that is not a numeral reads as
	// null, so comparing it to a number is false rather than an error.
	Number(expr string) string

	// Array reads expr as a JSON array, wrapping a bare scalar in one.
	Array(expr string) string

	// Each is a relation with one row per element of expr, in a column
	// named value. It is correlated with the row expr came from, so it
	// belongs in a FROM or JOIN alongside that row's table.
	Each(expr string) string

	// Length counts the elements of expr. An empty or null column is 0.
	Length(expr string) string

	// Extract reads path out of expr as text, null when path is absent.
	// path is a JSON path relative to the value, ".a.b" or "[0]" or ""
	// for the value itself.
	Extract(expr, path string) string

	// Last reads the final element of expr as text, "" when there is none.
	Last(expr string) string

	// Catalog is a relation over the schema's own objects, with columns
	// type ("table", "view" or "index"), name, tbl_name and sql.
	Catalog() string

	// Columns is a relation over the schema's columns, with tbl_name, cid,
	// name, type, notnull, dflt_value and pk.
	Columns() string

	// Prelude is the statements a schema needs before any of it exists —
	// collations and the like — "" where the engine already has them.
	Prelude() string

	// Format is the name of the function rendering an instant through a
	// format string, "" where the engine has none and a caller must refuse
	// rather than approximate.
	Format() string

	// Row is the column carrying insertion order, "" where the engine has
	// none and a caller must fall back to an ordinary column.
	Row() string

	// Optimize refreshes the planner's statistics.
	Optimize() string

	// Checkpoint folds the write-ahead log back into the database, "" where
	// the engine has no log to fold.
	Checkpoint() string
}

Dialect is one engine's spelling.

func For

func For(driver string) Dialect

For returns the dialect of a database/sql driver name. An unrecognized name gets SQLite, which is what an embedded default resolves to everywhere else in the estate.

type Postgres

type Postgres struct{}

Postgres spells for the PostgreSQL engine.

func (Postgres) Array

func (Postgres) Array(expr string) string

func (Postgres) Bool added in v0.6.25

func (Postgres) Bool(v bool) string

Quoted, so that one spelling serves both operands a truth value is compared to: beside a boolean the literal reads as a boolean, and beside a value read out of a JSON document it reads as the text that document held.

func (Postgres) Bytes added in v0.6.24

func (Postgres) Bytes() string

func (Postgres) Catalog

func (Postgres) Catalog() string

func (Postgres) Checkpoint

func (Postgres) Checkpoint() string

func (Postgres) Columns

func (Postgres) Columns() string

func (Postgres) Each

func (d Postgres) Each(expr string) string

func (Postgres) Extract

func (Postgres) Extract(expr, path string) string

func (Postgres) Format added in v0.6.23

func (Postgres) Format() string

func (Postgres) Json

func (Postgres) Json() string

func (Postgres) Last

func (d Postgres) Last(expr string) string

func (Postgres) Length

func (Postgres) Length(expr string) string

func (Postgres) Like added in v0.6.25

func (Postgres) Like() string

ILIKE rather than the nocase collation below, which LIKE accepts only from PostgreSQL 18 on.

func (Postgres) Name

func (Postgres) Name() string

func (Postgres) Now

func (Postgres) Now() string

func (Postgres) Number added in v0.6.25

func (Postgres) Number(expr string) string

func (Postgres) Optimize

func (Postgres) Optimize() string

func (Postgres) Prelude added in v0.6.23

func (Postgres) Prelude() string

A case-insensitive comparison is spelled COLLATE NOCASE, which is a collation SQLite is born with and Postgres has to be given. Nondeterministic so that equality itself folds case, rather than only the sort order.

func (Postgres) Quote

func (Postgres) Quote(name string) string

func (Postgres) Random

func (Postgres) Random(n int) string

func (Postgres) Row

func (Postgres) Row() string

type SQLite

type SQLite struct{}

SQLite spells for the SQLite engine.

func (SQLite) Array

func (SQLite) Array(expr string) string

func (SQLite) Bool added in v0.6.25

func (SQLite) Bool(v bool) string

func (SQLite) Bytes added in v0.6.24

func (SQLite) Bytes() string

func (SQLite) Catalog

func (SQLite) Catalog() string

func (SQLite) Checkpoint

func (SQLite) Checkpoint() string

func (SQLite) Columns

func (SQLite) Columns() string

func (SQLite) Each

func (d SQLite) Each(expr string) string

func (SQLite) Extract

func (SQLite) Extract(expr, path string) string

func (SQLite) Format added in v0.6.23

func (SQLite) Format() string

func (SQLite) Json

func (SQLite) Json() string

func (SQLite) Last

func (d SQLite) Last(expr string) string

func (SQLite) Length

func (SQLite) Length(expr string) string

func (SQLite) Like added in v0.6.25

func (SQLite) Like() string

SQLite's LIKE folds the case of ASCII on its own.

func (SQLite) Name

func (SQLite) Name() string

func (SQLite) Now

func (SQLite) Now() string

func (SQLite) Number added in v0.6.25

func (SQLite) Number(expr string) string

SQLite compares a numeral as a number and anything else as what it is, which is what reading one as a number amounts to.

func (SQLite) Optimize

func (SQLite) Optimize() string

func (SQLite) Prelude added in v0.6.23

func (SQLite) Prelude() string

func (SQLite) Quote

func (SQLite) Quote(name string) string

func (SQLite) Random

func (SQLite) Random(n int) string

func (SQLite) Row

func (SQLite) Row() string

Jump to

Keyboard shortcuts

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