sqllex

package
v0.9.0 Latest Latest
Warning

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

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

Documentation

Overview

Package sqllex lexes the SQL sqlc hands the plugin, and the pyformat text the MySQL rewriter produces from it, far enough to tell a bindable placeholder from a "?" that merely sits inside a string, an identifier or a comment. The rewriter and the drivers' argument ordering share it so the rules cannot drift apart.

Index

Constants

This section is empty.

Variables

View Source
var (
	// MySQLRaw is sqlc's MySQL output, before the pyformat rewrite. Only
	// default sql_mode is supported: sqlc's dolphin parser lexes with
	// backslash escapes on and treats "..." as a string, so a query that
	// reached the plugin already parsed under those rules.
	MySQLRaw = Dialect{
				// contains filtered or unexported fields
	}

	// MySQLPyformat is the same text after the rewrite, where placeholders
	// are "%s" and a literal percent has been doubled.
	MySQLPyformat = Dialect{
					// contains filtered or unexported fields
	}

	// SQLite is sqlc's SQLite output, which the drivers execute unchanged.
	// sqlc numbers named parameters (?1, ?2); "#" is not a comment and
	// backslashes are not escapes, but both `...` and [...] quote
	// identifiers.
	SQLite = Dialect{
			// contains filtered or unexported fields
	}
)

The three texts the plugin lexes. MySQLRaw and MySQLPyformat describe the same grammar either side of the rewrite, which is why they must be defined together: the rewriter reads the first and every consumer of its output reads the second.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	// contains filtered or unexported fields
}

Dialect is the rule set for one SQL text. Its fields are unexported and only the values below are exported: every caller picks a named dialect instead of assembling flags, so no call site can lex with rules the producer of the text never used.

func (Dialect) Placeholder

func (d Dialect) Placeholder() string

Placeholder returns one bindable placeholder as it appears in this dialect's text. Emitters use it so the token they write and the token the scanner looks for can never be two different literals.

func (Dialect) SliceMarker

func (d Dialect) SliceMarker(name string) string

SliceMarker returns the marker sqlc leaves for a sqlc.slice parameter, together with the placeholder it binds. Scanning reports the marker's actual text; this rebuilds it for the one caller that has a name but no scanned text to point at.

type Kind

type Kind uint8

Kind classifies a scanned token.

const (
	// KindText is ordinary SQL: copied as-is, holds nothing bindable.
	KindText Kind = iota
	// KindSkipped is a string literal, quoted identifier or comment. It is
	// copied as-is too, but is called out because a placeholder inside one
	// does not bind.
	KindSkipped
	// KindPlaceholder is one bindable slot.
	KindPlaceholder
	// KindSliceMarker is a /*SLICE:name*/ marker and the placeholder it
	// binds, which sqlc always emits adjacent.
	KindSliceMarker
)

type Slot

type Slot struct {
	Name   string
	Marker string
	// Number is the explicit index of a numbered placeholder (sqlite's ?N),
	// or 0 when the placeholder carries none. SQLite binds ?N to slot N and
	// a bare ? to one past the highest slot seen so far, so the two spell
	// different bindings for the same text position.
	Number int
}

Slot is a bindable position in the text, in text order. Name and Marker are empty for a plain placeholder; for a sqlc.slice they carry the raw name and the marker's exact text, which is what generated code has to replace.

func Slots

func Slots(sql string, d Dialect) []Slot

Slots reports the bindable positions of sql in text order.

type Token

type Token struct {
	Kind  Kind
	Start int
	End   int
	// Name is the sqlc.slice name of a KindSliceMarker token.
	Name string
	// Number is the explicit index of a numbered placeholder (sqlite's ?N),
	// or 0 when the placeholder carries none.
	Number int
	// MarkerEnd splits a KindSliceMarker: [Start, MarkerEnd) is the comment,
	// [MarkerEnd, End) the placeholder it binds. Zero for other kinds.
	MarkerEnd int
}

Token is one scanned span of the input, [Start, End).

func Scan

func Scan(sql string, d Dialect) []Token

Scan splits sql into tokens under the given dialect. Unterminated strings, identifiers and comments consume the rest of the input, matching how sqlc's own parsers recover.

Jump to

Keyboard shortcuts

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