dialect

package
v2.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2024 License: BSD-2-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package dialect handles various dialect-specific ways of generating SQL.

Index

Constants

This section is empty.

Variables

View Source
var (
	// SqliteQuoter uses ANSI double-quotes for Sqlite.
	// This can be modified, e.g. to None, before first use.
	SqliteQuoter = quote.ANSI

	// PostgresQuoter uses ANSI double-quotes for Postgres.
	// This can be modified, e.g. to None, before first use.
	PostgresQuoter = quote.ANSI

	// MySqlQuoter uses backticks for MySQL.
	// This can be modified, e.g. to None, before first use.
	MySqlQuoter = quote.Backticks

	// MSSqlQuoter uses square brackets for MS-SQL.
	// This can be modified, e.g. to None, before first use.
	MSSqlQuoter = quote.SquareBrackets
)

These are defaults used by each dialect; they can be altered before first use.

View Source
var DefaultDialect = Sqlite

DefaultDialect is Sqlite, chosen as being probably the simplest. This can be altered before first use.

Functions

This section is empty.

Types

type Dialect

type Dialect int

Dialect represents a dialect of SQL. All the defined dialects are non-zero.

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 Pick

func Pick(name string) Dialect

Pick finds a dialect that matches by name, ignoring letter case. It matches:

  • "sqlite", "sqlite3"
  • "mysql"
  • "postgres", "postgresql", "pgx"
  • "sqlserver", "sql-server", "mssql"

It returns 0 if not found.

func (Dialect) Placeholder

func (d Dialect) Placeholder() FormatOption

Placeholder returns Query, Dollar or AtP.

func (Dialect) Quoter

func (d Dialect) Quoter() quote.Quoter

Quoter returns the corresponding MySqlQuoter, PostgresQuoter, SqliteQuoter MSSqlQuoter or the quote.DefaultQuoter. All of these can be configured before first use.

func (Dialect) String

func (d Dialect) String() string

String is the inverse of Pick.

type FormatOption

type FormatOption int

FormatOption provides controls for where-expression formatting.

const (
	// Query indicates placeholders using queries '?'. For Sqlite & MySql.
	// Because where-expressions are constructed using queries, this option
	// specifies that no change is needed.
	Query FormatOption = iota

	// Dollar indicates placeholders using numbered $1, $2, ... format. For PostgreSQL.
	Dollar

	// AtP indicates placeholders using numbered @p1, @p2, ... format. For SQL-Server.
	AtP

	// Inline indicates that each placeholder is removed and its value is inlined.
	Inline
)

These options affect how placeholders are renderered.

const (
	// NoQuotes indicates identifiers will not be enclosed in quote marks.
	NoQuotes FormatOption = iota + 10

	// ANSIQuotes indicates identifiers will be enclosed in double quote marks. For Postgres.
	ANSIQuotes

	// Backticks indicates identifiers will be enclosed in back-tick marks. For MySql.
	Backticks

	// SquareBrackets indicates identifiers will be enclosed in square brackets. For SQL-Server.
	SquareBrackets
)

These options affect how column name identifiers are quoted, if required. Quoting identifiers is mandatory if the identifiers happen to collide with reserved names. Otherwise, it is optional.

Jump to

Keyboard shortcuts

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