Help us shape the future of Go! Take the 2026 Go Developer Survey

sqllex

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package sqllex reads rendered SQL back into tokens under one dialect's quoting rules. It exists for tests: a fuzz property checked against an independent reading of the output catches what a check written against the code that produced it would share.

It knows quoting and nothing else. Quoted identifiers and string literals are decoded; every other run of bytes between spaces and quotes is one Other token, kept verbatim.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Of

func Of(ts []Token, k Kind) []string

Of returns the values of every token of kind k, in order, or nil if none.

Types

type Dialect

type Dialect int

Dialect selects the quoting rules Lex applies.

const (
	// ANSI is PostgreSQL and SQLite: "identifiers" with "" doubling, and
	// 'strings' with ” doubling, where a backslash is an ordinary byte.
	ANSI Dialect = iota
	// MySQL has `identifiers` with “ doubling, and 'strings' or "strings"
	// (the default sql_mode, without ANSI_QUOTES) with the quote doubled or
	// backslash escaped. \x decodes to x for every x. That is exact for the
	// escaping gosbee emits (\\ and ”), but not for MySQL in general, which
	// keeps the backslash in \% and \_ and reads \n, \0 and \Z as control bytes.
	// Lex reads bytes, so it cannot model a multibyte client charset such as
	// GBK, where a lead byte can swallow an escaping backslash.
	MySQL
)

type Kind

type Kind int

Kind is a token's kind.

const (
	// Ident is a quoted identifier; its Value is decoded.
	Ident Kind = iota
	// String is a string literal; its Value is decoded.
	String
	// Other is any other run of bytes, verbatim.
	Other
)

type Token

type Token struct {
	Kind  Kind
	Value string
}

Token is one lexed token.

func Lex

func Lex(d Dialect, sql string) ([]Token, error)

Lex splits sql into tokens under d's rules. It returns an error for an unterminated quoted identifier or string.

Jump to

Keyboard shortcuts

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