dialect

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package dialect defines the per-database adapter contract and a registry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONPathString

func JSONPathString(path []string) string

JSONPathString renders path as "$.a.b[0]" for databases that take a JSON path string.

func QuoteWith

func QuoteWith(ident, open, closing string, upper bool) string

QuoteWith quotes each dot-separated part using open/close delimiters, escaping the close delimiter.

func Register

func Register(d Dialect, aliases ...string)

Register makes a dialect available under its name and any aliases.

Types

type Base

type Base struct{}

Base provides ANSI defaults; dialects embed it and override what differs.

func (Base) AddColumnKeyword

func (Base) AddColumnKeyword() string

func (Base) BoolLiteral

func (Base) BoolLiteral(b bool) string

func (Base) DatePart

func (Base) DatePart(col string, part DatePart) string

func (Base) DriverDSN

func (Base) DriverDSN(dsn string) string

func (Base) FullTextIndex

func (Base) FullTextIndex(string, string, []string) string

func (Base) FullTextMatch

func (Base) FullTextMatch([]string, bool) string

func (Base) JSONExtract

func (Base) JSONExtract(string, []string) string

func (Base) LockSQL

func (Base) LockSQL(mode LockMode) string

func (Base) NativeEnum

func (Base) NativeEnum() bool

func (Base) SupportsAddConstraint

func (Base) SupportsAddConstraint() bool

func (Base) SupportsIndexes

func (Base) SupportsIndexes() bool

func (Base) TransactionalDDL

func (Base) TransactionalDDL() bool

type Column

type Column struct {
	Kind      Kind
	Length    int
	Precision int
	Scale     int
	Unsigned  bool
	Enum      []string
}

Column carries what a dialect needs to render a type.

type DatePart

type DatePart string

DatePart selects what WhereDate and friends compare.

const (
	PartDate  DatePart = "date"
	PartYear  DatePart = "year"
	PartMonth DatePart = "month"
	PartDay   DatePart = "day"
)

type Dialect

type Dialect interface {
	Name() string
	Dialector(dsn string) gorm.Dialector
	// DriverName is the database/sql driver name; DriverDSN adapts the DSN for it.
	// FromConn builds a Dialector on top of an existing pool (used for AfterCreate hooks,
	// acquire timeouts and replicas).
	DriverName() string
	DriverDSN(dsn string) string
	FromConn(conn gorm.ConnPool) gorm.Dialector
	Quote(ident string) string
	ColumnType(c Column) string
	// AutoIncrement returns the full column definition (after the column name) for an auto-increment primary key.
	AutoIncrement(big bool) string
	BoolLiteral(b bool) string
	AddColumnKeyword() string
	RenameTable(from, to string) string
	RenameColumn(table, from, to string) string
	DropIndex(table, name string) string
	DropConstraint(table, name string) string
	DropForeign(table, name string) string
	SupportsAddConstraint() bool
	SupportsIndexes() bool
	NativeEnum() bool
	TransactionalDDL() bool
}

Dialect is implemented once per supported database family. Methods returning SQL return "" when the operation is unsupported.

func Get

func Get(name string) (Dialect, error)

Get resolves a registered dialect. The error tells the caller which package to import.

type Features

type Features interface {
	// LockSQL is the clause appended to a SELECT, e.g. "FOR UPDATE".
	LockSQL(mode LockMode) string
	// JSONExtract reads the text at a JSON path (already validated: dot-separated names or indexes).
	JSONExtract(col string, path []string) string
	// DatePart reduces a date/time column to its date, year, month or day.
	DatePart(col string, part DatePart) string
	// FullTextMatch is a boolean expression with exactly one "?" for the search text.
	// boolean asks for operator syntax (+word -word "phrase") where the database has it.
	FullTextMatch(cols []string, boolean bool) string
	// FullTextIndex is the statement that creates an index FullTextMatch can use.
	FullTextIndex(table, name string, cols []string) string
}

Features are the query-building differences between databases that the ORM needs beyond Quote. Every method returns "" when the database cannot do it, and the caller turns that into an UNSUPPORTED_FEATURE error. Identifiers arrive already quoted; Base supplies ANSI defaults, so a dialect overrides only what differs.

func FeaturesOf

func FeaturesOf(d Dialect) Features

FeaturesOf returns d's Features, or one that supports nothing when d predates them.

type Kind

type Kind string

Kind is a database-agnostic column type.

const (
	KindString   Kind = "string"
	KindText     Kind = "text"
	KindSmallInt Kind = "smallint"
	KindInteger  Kind = "integer"
	KindBigInt   Kind = "bigint"
	KindBoolean  Kind = "boolean"
	KindFloat    Kind = "float"
	KindDouble   Kind = "double"
	KindDecimal  Kind = "decimal"
	KindDate     Kind = "date"
	KindTime     Kind = "time"
	KindDateTime Kind = "datetime"
	KindStamp    Kind = "timestamp"
	KindJSON     Kind = "json"
	KindUUID     Kind = "uuid"
	KindBinary   Kind = "binary"
	KindEnum     Kind = "enum"
)

type LockMode

type LockMode string

LockMode is a row-lock request for a SELECT.

const (
	LockUpdate LockMode = "update" // FOR UPDATE
	LockShare  LockMode = "share"  // shared lock
)

Directories

Path Synopsis
Package mysql registers MySQL and MariaDB.
Package mysql registers MySQL and MariaDB.
Package oracle registers Oracle Database using the pure-Go go-ora driver (no cgo).
Package oracle registers Oracle Database using the pure-Go go-ora driver (no cgo).
Package postgres registers PostgreSQL, CockroachDB and Redshift.
Package postgres registers PostgreSQL, CockroachDB and Redshift.
Package sqlite registers SQLite using the pure-Go (no cgo) glebarez driver.
Package sqlite registers SQLite using the pure-Go (no cgo) glebarez driver.
Package sqlserver registers Microsoft SQL Server.
Package sqlserver registers Microsoft SQL Server.

Jump to

Keyboard shortcuts

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