parser

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package parser implements a SQL parser for query validation and parameter extraction.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(blk block.Block) (Query, []Diagnostic)

Parse parses a query block into a structured Query object.

Types

type CTE

type CTE struct {
	Name      string
	Columns   []string
	SelectSQL string
	Line      int
	Column    int
}

CTE represents a Common Table Expression.

type Column

type Column struct {
	Expr        string
	Alias       string
	Table       string
	Line        int
	Column      int
	StartOffset int
	EndOffset   int
	// EmbedTable holds the table name or alias referenced by a sqlc.embed()
	// expression, e.g. "author" for sqlc.embed(author). It is only meaningful
	// when IsEmbed is true.
	EmbedTable string
	// IsEmbed reports whether this column is a sqlc.embed() expression, which
	// the analyzer expands into the full column set of EmbedTable and the
	// code generator collapses into one nested struct field.
	IsEmbed bool
	// EmbedSlice reports whether this column is a sqlc.embed_slice() expression
	// (a one-to-many embed). It is only meaningful when IsEmbed is true. The
	// analyzer carries it through to the result columns and the code generator
	// aggregates the child rows into a nested []Child slice field instead of a
	// single nested struct.
	EmbedSlice bool
	// EmbedAs holds the explicit alias given to a sqlc.embed() or
	// sqlc.embed_slice() expression, e.g. "writer" for
	// `sqlc.embed(author) as writer`. It renames the generated nested struct
	// field (Writer) while the backing model type stays the table (Author). It is
	// only meaningful when IsEmbed is true; empty means the field name derives
	// from the table name as before.
	EmbedAs string
}

Column represents a result column in a SELECT query.

type Diagnostic

type Diagnostic struct {
	Path      string
	Line      int
	Column    int
	EndColumn int // End column for multi-character spans
	Offset    int // Byte offset in file
	Length    int // Length of the problematic token/region
	Message   string
	Severity  Severity
}

Diagnostic represents an issue found during parsing.

type Param

type Param struct {
	Name          string
	Style         ParamStyle
	Order         int
	Line          int
	Column        int
	IsVariadic    bool
	VariadicCount int
	StartOffset   int
	EndOffset     int
}

Param represents a query parameter.

type ParamStyle

type ParamStyle int

ParamStyle indicates how parameters are specified (positional or named).

const (
	// ParamStyleUnknown indicates an unrecognized parameter style.
	ParamStyleUnknown ParamStyle = iota
	// ParamStylePositional indicates parameters using '?' or '?NNN'.
	ParamStylePositional
	// ParamStyleNamed indicates parameters using ':name'.
	ParamStyleNamed
)

type Query

type Query struct {
	Block       block.Block
	Verb        Verb
	Columns     []Column
	Params      []Param
	CTEs        []CTE
	Diagnostics []Diagnostic
}

Query represents a parsed SQL query with metadata.

type Severity

type Severity int

Severity indicates the seriousness of a diagnostic.

const (
	// SeverityError indicates a fatal parsing error.
	SeverityError Severity = iota
	// SeverityWarning indicates a non-fatal issue.
	SeverityWarning
)

type Verb

type Verb int

Verb indicates the type of SQL statement (SELECT, INSERT, etc.).

const (
	// VerbUnknown indicates an unrecognized statement type.
	VerbUnknown Verb = iota
	// VerbSelect indicates a SELECT statement.
	VerbSelect
	// VerbInsert indicates an INSERT statement.
	VerbInsert
	// VerbUpdate indicates an UPDATE statement.
	VerbUpdate
	// VerbDelete indicates a DELETE statement.
	VerbDelete
)

Jump to

Keyboard shortcuts

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