parser

package
v1.3.0 Latest Latest
Warning

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

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

Documentation

Overview

Package parser loads SQL text into TiDB parser ASTs (after token-level preprocessing of MariaDB-only syntax the parser rejects) and restores ASTs back into formatted SQL.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SplitScript added in v1.3.0

func SplitScript(s string) []string

SplitScript splits a mysql-client script into statements without their delimiters, honoring DELIMITER directives; blank statements are omitted.

Types

type BaseVisitor

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

BaseVisitor provides a default implementation of Visitor

func NewBaseVisitor

func NewBaseVisitor() *BaseVisitor

NewBaseVisitor creates a new base visitor

func (*BaseVisitor) Enter

func (v *BaseVisitor) Enter(node ast.Node) (ast.Node, bool)

Enter implements Visitor interface

func (*BaseVisitor) Leave

func (v *BaseVisitor) Leave(node ast.Node) (ast.Node, bool)

Leave implements Visitor interface

type Loader

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

Loader handles loading and parsing SQL files

func NewLoader

func NewLoader() *Loader

NewLoader creates a SQL loader without charset mappings; collations TiDB does not support still fall back to a supported one of the same charset.

func NewLoaderWithConfig

func NewLoaderWithConfig(cfg *config.Config) *Loader

NewLoaderWithConfig creates a loader configured with charset mappings from config

func (*Loader) LoadFromFile

func (l *Loader) LoadFromFile(filename string) ([]ast.StmtNode, error)

LoadFromFile loads and parses SQL from a file

func (*Loader) LoadFromReader

func (l *Loader) LoadFromReader(reader io.Reader) ([]ast.StmtNode, error)

LoadFromReader loads and parses SQL from an io.Reader

func (*Loader) LoadFromString

func (l *Loader) LoadFromString(sql string) ([]ast.StmtNode, error)

LoadFromString loads and parses SQL from a string

func (*Loader) ParseTiDB added in v1.3.0

func (l *Loader) ParseTiDB(sql string) ([]ast.StmtNode, error)

ParseTiDB parses SQL with the TiDB parser only, without preprocessing.

func (*Loader) ValidateFile added in v1.1.0

func (l *Loader) ValidateFile(filename string) (int, []error, error)

ValidateFile parses a file with the TiDB parser without any preprocessing and returns the statement count, parser warnings, and the first hard error.

func (*Loader) ValidateSQL added in v1.1.0

func (l *Loader) ValidateSQL(sql string) (int, []error, error)

ValidateSQL parses SQL text with the TiDB parser without any preprocessing and returns the statement count, parser warnings, and the first hard error.

func (*Loader) WithCharsetMappings

func (l *Loader) WithCharsetMappings(charsetMap map[string]string, collationMap map[string]string) *Loader

WithCharsetMappings configures the loader with charset and collation mappings

func (*Loader) WithReporter added in v1.3.0

func (l *Loader) WithReporter(r *compat.Reporter) *Loader

WithReporter makes the loader record migration warnings (dropped stored programs, stripped MariaDB-only attributes, ...) in r.

type Visitor

type Visitor interface {
	// Enter is called when entering a node
	Enter(node ast.Node) (ast.Node, bool)
	// Leave is called when leaving a node
	Leave(node ast.Node) (ast.Node, bool)
}

Visitor interface for traversing AST nodes

type Walker

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

Walker provides functionality to walk the AST with a visitor

func NewWalker

func NewWalker() *Walker

NewWalker creates a new AST walker

func (*Walker) Walk

func (w *Walker) Walk(stmts []ast.StmtNode, visitor Visitor) ([]ast.StmtNode, error)

Walk traverses the AST with the given visitor

type Writer

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

Writer handles writing AST back to formatted SQL. Output is produced by the TiDB parser's own Restore, so keywords and identifier quoting always follow the TiDB grammar that validated the input.

func NewWriter

func NewWriter() *Writer

NewWriter creates a new SQL writer with default formatting options.

func NewWriterWithConfig added in v1.2.0

func NewWriterWithConfig(cfg *config.Config) *Writer

NewWriterWithConfig creates a new SQL writer honoring cfg.FormattingOptions. A nil cfg means the default configuration.

func (*Writer) WriteStatement

func (w *Writer) WriteStatement(stmt ast.StmtNode) (string, error)

WriteStatement writes a single statement to formatted SQL

func (*Writer) WriteStatements

func (w *Writer) WriteStatements(stmts []ast.StmtNode) (string, error)

WriteStatements writes multiple statements to formatted SQL, separated by a blank line and terminated by a newline.

Jump to

Keyboard shortcuts

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