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 ¶
var DefaultDialect = Sqlite // chosen as being probably the simplest
var MysqlConfig = DialectConfig{ Ident: Mysql, PlaceholderStyle: Queries, Quoter: quote.MySqlQuoter, CaseInsensitive: true, }
MysqlConfig handles the MySQL syntax.
var PostgresConfig = DialectConfig{ Ident: Postgres, PlaceholderStyle: Numbered, PlaceholderPrefix: "$", Quoter: quote.AnsiQuoter, }
PostgresConfig handles the PostgreSQL syntax.
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
var SqliteConfig = DialectConfig{ Ident: Sqlite, PlaceholderStyle: Queries, Quoter: quote.AnsiQuoter, }
SqliteConfig handles the MySQL syntax.
Functions ¶
func ReplacePlaceholdersWithNumbers ¶
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 ¶
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
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 )