dialect

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2022 License: BSD-2-Clause Imports: 3 Imported by: 6

Documentation

Overview

Package dialect handles quote marks and SQL placeholders in various dialect-specific ways. Queries should be written using '?' query placeholders throughout, and then this package will translate to the form needed by the chosen dialect.

The XyzConfig variables are mutable so you can alter them if required. You can also alter DefaultDialect.

Index

Constants

This section is empty.

Variables

View Source
var DefaultDialect = Sqlite // chosen as being probably the simplest
View Source
var MysqlConfig = DialectConfig{
	Ident:            Mysql,
	PlaceholderStyle: Queries,
	Quoter:           quote.MySqlQuoter,
	CaseInsensitive:  true,
}

MysqlConfig handles the MySQL syntax.

View Source
var PostgresConfig = DialectConfig{
	Ident:             Postgres,
	PlaceholderStyle:  Numbered,
	PlaceholderPrefix: "$",
	Quoter:            quote.AnsiQuoter,
}

PostgresConfig handles the PostgreSQL syntax.

View Source
var SqlServerConfig = DialectConfig{
	Ident:             SqlServer,
	PlaceholderStyle:  Numbered,
	PlaceholderPrefix: "@p",
	Quoter:            quote.AnsiQuoter,
}

SqlServerConfig handles the T-SQL syntax. https://docs.microsoft.com/en-us/sql/t-sql/language-reference?view=sql-server-ver15

View Source
var SqliteConfig = DialectConfig{
	Ident:            Sqlite,
	PlaceholderStyle: Queries,
	Quoter:           quote.AnsiQuoter,
}

SqliteConfig handles the MySQL syntax.

Functions

func ReplacePlaceholdersWithNumbers

func ReplacePlaceholdersWithNumbers(sql, prefix string) string

ReplacePlaceholdersWithNumbers replaces all "?" placeholders with numbered placeholders, using the given prefix. For PostgreSQL these will be "$1" and upward placeholders so the prefix should be "$". For SQL-Server there will be "@p1" and upward placeholders so the prefix should be "@p".

Types

type Dialect

type Dialect int
const (

	// Sqlite identifies SQLite
	Sqlite Dialect
	// Mysql identifies MySQL (also works for MariaDB)
	Mysql
	// Postgres identifies PostgreSQL
	Postgres
	// SqlServer identifies SqlServer (MS-SQL)
	SqlServer
)

func PickDialect

func PickDialect(name string) (Dialect, bool)

PickDialect finds a dialect that matches by name, ignoring letter case. It returns false if not found.

func (Dialect) Config added in v0.10.0

func (d Dialect) Config() DialectConfig

func (Dialect) ReplacePlaceholders

func (d Dialect) ReplacePlaceholders(sql string, names []string) string

ReplacePlaceholders converts a string containing '?' placeholders to the form used by the dialect.

type DialectConfig added in v0.10.0

type DialectConfig struct {
	// Name is used for
	Ident Dialect

	// PlaceholderStyle specifies the way of including placeholders in SQL.
	PlaceholderStyle PlaceholderStyle

	// PlaceholderPrefix specifies the string that marks a placeholder, when numbered
	PlaceholderPrefix string

	// Quoter determines the quote marks surrounding identifiers.
	Quoter quote.Quoter

	// CaseInsensitive is true when identifiers are not case-sensitive
	CaseInsensitive bool
}

DialectConfig holds the settings to be used in SQL translation functions.

func (DialectConfig) ReplacePlaceholders added in v0.10.0

func (dc DialectConfig) ReplacePlaceholders(sql string, names []string) string

ReplacePlaceholders converts a string containing '?' placeholders to the form used by the dialect.

type PlaceholderStyle

type PlaceholderStyle int

PlaceholderStyle enumerates the different ways of including placeholders in SQL.

const (
	// Queries is the '?' placeholder style and is assumed to be used prior to translation.
	Queries PlaceholderStyle = iota
	// Numbered placeholders '$1', '$2' etc are used (e.g.) in PostgreSQL.
	Numbered
)

Jump to

Keyboard shortcuts

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