mysql

package
v0.1.0 Latest Latest
Warning

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

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

README

dialect/mysql — the MySQL dialect

Placeholders (?), identifier quoting (backquotes), and the capabilities MySQL 8 has.

It imports render and nothing else, which TestDialects_ImportOnlyRender checks. It declares SupportsLikeEscape, SupportsCombinedAlter and SupportsUnrestrictedAddColumn, and implements LiteralQuoting. It does not declare SupportsReturning, because MySQL has no RETURNING, so an InsertReturning on a MySQL statement does not compile.

Two rules are worth reading before relying on it:

  • Identifiers are backquoted, because a double-quoted token is a string in MySQL unless ANSI_QUOTES is on, and a builder cannot depend on a server's mode.
  • A string containing a backslash cannot be written as a literal. MySQL reads a backslash differently under NO_BACKSLASH_ESCAPES, and it stores an expression default as printed text and re-parses it under the session's mode, so no spelling reads the same under both. QuoteLiteral returns ErrNotInlinable for it, measured on MySQL 8.4. A bound parameter has no such problem, so this only affects DDL.

contrib/driver-mysql runs the shared conformance suite against a real MySQL 8.4.

Full documentation

docs/dialect/README.md covers every dialect and how to write another; docs/dialects.md covers what a dialect decides.

Documentation

Overview

Package mysql is the MySQL dialect: how MySQL 8 writes a bound parameter and quotes an identifier, and which optional capabilities it has.

Like every dialect here it imports the render layer and nothing else, which is exactly what a dialect from another repository imports. The constructs only MySQL has live in github.com/ubgo/sqlb/mysql.

Until 2026-09-22 MySQL was a reference engine only: the universality rule named it and nothing here could render for it, so that quarter of the rule was checked by reading its documentation. This package and contrib/driver-mysql, which runs the conformance suite on a real MySQL 8.4, replace the reading with a measurement.

Index

Constants

Name is the engine name this dialect reports.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect struct{}

Dialect renders ? placeholders and backquoted identifiers.

func (Dialect) Name

func (Dialect) Name() string

Name reports "mysql".

func (Dialect) NumericParam

func (Dialect) NumericParam(placeholder string, v any) string

NumericParam casts an integer parameter to SIGNED, or an unsigned one to UNSIGNED, so a DECIMAL beside it in COALESCE or arithmetic does not widen the result to DECIMAL(65,30). See render.NumericParameters for the measurement.

It reads the kind rather than switching on types, so a named integer type (type Age int64) is cast like int64. A nil pointer and every non-integer value are left alone: a NULL has no type to give, and a float or a string was measured not to widen.

func (Dialect) Placeholder

func (Dialect) Placeholder(int) string

Placeholder renders ?. MySQL's placeholders are positional and unnumbered, so a value used twice is bound twice.

func (Dialect) QuoteIdent

func (Dialect) QuoteIdent(ident string) string

QuoteIdent backquotes an identifier, doubling any embedded backquote. It is not the double quote the other engines use: a double-quoted token is a string in MySQL unless the ANSI_QUOTES mode is on, and a builder cannot depend on a server's mode.

func (Dialect) QuoteLiteral

func (Dialect) QuoteLiteral(v any) (string, error)

QuoteLiteral writes v as a MySQL literal that reads back as exactly v, whatever the server's sql_mode.

The mode is the difficulty, as standard_conforming_strings is on Postgres. With NO_BACKSLASH_ESCAPES off, the default, a backslash in a string literal is an escape; with it on, it is a character. A string without a backslash, quotes doubled, reads the same under both, measured on MySQL 8.4. A string with one has no spelling that does: the hex form _utf8mb4 X'...' reads correctly in a query, but MySQL stores an expression default as printed text and re-parses it under the session's mode, so a DEFAULT of that form holding the three characters a\b read back as four with NO_BACKSLASH_ESCAPES on. A string containing a backslash is therefore ErrNotInlinable, not a best guess; bind it where a parameter is allowed, which is everywhere but DDL.

The value set is otherwise the Postgres one: strings, bools, every integer and float kind, []byte, time.Time (in UTC, for a DATETIME), nil, pointers and named types over those. A NUL, a NaN and an infinity are refused.

func (Dialect) SupportsCombinedAlter

func (Dialect) SupportsCombinedAlter() bool

SupportsCombinedAlter reports true: MySQL applies several ALTER TABLE actions in one statement.

func (Dialect) SupportsLikeEscape

func (Dialect) SupportsLikeEscape() bool

SupportsLikeEscape reports true: MySQL accepts an ESCAPE clause on LIKE.

func (Dialect) SupportsUnrestrictedAddColumn

func (Dialect) SupportsUnrestrictedAddColumn() bool

SupportsUnrestrictedAddColumn reports true: MySQL's ADD COLUMN takes any column CREATE TABLE would, filling existing rows with the column's implicit default when it is NOT NULL.

Jump to

Keyboard shortcuts

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