dialect

package
v0.0.0-...-e974cb4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package dialect adapts SQL rendering — identifiers, placeholders, type mapping, autoincrement syntax, LIMIT/OFFSET — to one database engine.

Built-in implementations: SQLite, MySQL, Postgres (registered via init). Custom types registered via RegisterCustomType take precedence over the dialect's built-in MapType lookup.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDialectNotSupported = errors.New("dialect: not supported")
	ErrTypeNotSupported    = errors.New("dialect: type not supported")
	ErrDialectRegistered   = errors.New("dialect: already registered")
)

ErrDialectNotSupported is returned by Get for an unregistered dialect name. ErrTypeNotSupported is returned by a dialect's MapType for a Go type it cannot map to a SQL column type. ErrDialectRegistered is returned by RegisterE when name is already registered.

Functions

func Names

func Names() []string

Names returns the registered dialect names in sorted order.

func Register

func Register(name string, d Dialect)

Register adds a Dialect under name. It panics if name is already registered, mirroring database/sql.Register. Library code that prefers an error over a panic should call RegisterE.

func RegisterCustomType

func RegisterCustomType(rt reflect.Type, sqlType string)

RegisterCustomType maps a Go type to a SQL column type name. Registered types take precedence over the dialect's built-in MapType lookup. This allows mapping [16]byte to BINARY(16), *big.Int to DECIMAL, or any custom type.

func RegisterE

func RegisterE(name string, d Dialect) error

RegisterE adds a Dialect under name and returns an error instead of panicking when name is already registered. It is the error-returning variant of Register for library use; init-time callers keep using Register.

Types

type Dialect

type Dialect interface {
	Name() string
	Placeholder(i int) string
	QuoteIdent(s string) string
	MapType(t reflect.Type) (string, error)
	AutoIncrement() string
	// AutoIncrementInlinePK reports whether an autoincrement column must carry
	// PRIMARY KEY inline on its column definition (SQLite: INTEGER PRIMARY KEY
	// AUTOINCREMENT). When true the table-level PRIMARY KEY constraint is
	// omitted for the autoincrement column; when false it is emitted as a
	// separate constraint.
	AutoIncrementInlinePK() bool
	LimitOffset(limit, offset int) string
}

Dialect adapts SQL rendering — identifiers, placeholders, type mapping, autoincrement syntax, LIMIT/OFFSET — to one database engine. The three built-in implementations (SQLite, MySQL, Postgres) register themselves in init via dialect.Register and are reachable through dialect.Get.

func Get

func Get(name string) (Dialect, error)

Get returns the Dialect registered under name, or ErrDialectNotSupported.

func MustGet

func MustGet(name string) Dialect

MustGet is Get that panics on error, for package-level Dialect handles.

type MySQL

type MySQL struct{}

MySQL adapts SQL rendering for MySQL-compatible databases.

func (MySQL) AutoIncrement

func (MySQL) AutoIncrement() string

func (MySQL) AutoIncrementInlinePK

func (MySQL) AutoIncrementInlinePK() bool

func (MySQL) LimitOffset

func (MySQL) LimitOffset(limit, offset int) string

func (MySQL) MapType

func (MySQL) MapType(t reflect.Type) (string, error)

func (MySQL) Name

func (MySQL) Name() string

func (MySQL) Placeholder

func (MySQL) Placeholder(int) string

func (MySQL) QuoteIdent

func (MySQL) QuoteIdent(s string) string

type Postgres

type Postgres struct{}

Postgres adapts SQL rendering for PostgreSQL-compatible databases.

func (Postgres) AutoIncrement

func (Postgres) AutoIncrement() string

func (Postgres) AutoIncrementInlinePK

func (Postgres) AutoIncrementInlinePK() bool

func (Postgres) LimitOffset

func (Postgres) LimitOffset(limit, offset int) string

func (Postgres) MapType

func (Postgres) MapType(t reflect.Type) (string, error)

func (Postgres) Name

func (Postgres) Name() string

func (Postgres) Placeholder

func (Postgres) Placeholder(i int) string

func (Postgres) QuoteIdent

func (Postgres) QuoteIdent(s string) string

type SQLite

type SQLite struct{}

SQLite adapts SQL rendering for SQLite databases.

func (SQLite) AutoIncrement

func (SQLite) AutoIncrement() string

func (SQLite) AutoIncrementInlinePK

func (SQLite) AutoIncrementInlinePK() bool

func (SQLite) LimitOffset

func (SQLite) LimitOffset(limit, offset int) string

func (SQLite) MapType

func (SQLite) MapType(t reflect.Type) (string, error)

func (SQLite) Name

func (SQLite) Name() string

func (SQLite) Placeholder

func (SQLite) Placeholder(int) string

func (SQLite) QuoteIdent

func (SQLite) QuoteIdent(s string) string

Jump to

Keyboard shortcuts

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