sqlparser

package module
v0.0.0-...-24085b5 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: BSD-3-Clause Imports: 12 Imported by: 0

README

sqlparser

This is copied directly from https://github.com/youtube/vitess/tree/master/go/vt/sqlparser with just a few modifications to make it work as a standalone repo.

There are, also, modifications to allow it to parse multiple sql statements in a file. As well and an example program sqlfmt that will pretty print sql file provided to it. Or show where the errors in the file are. It will only show the first error.

License

Unless otherwise noted, the vitess source files are distributed under the BSD-style license found in the LICENSE file.

Documentation

Index

Constants

View Source
const (
	AST_FOR_UPDATE = " for update"
	AST_SHARE_MODE = " lock in share mode"
)

Select.Lock

View Source
const (
	AST_UNION     = "union"
	AST_UNION_ALL = "union all"
	AST_SET_MINUS = "minus"
	AST_EXCEPT    = "except"
	AST_INTERSECT = "intersect"
)

Union.Type

View Source
const (
	AST_CREATE = "create"
	AST_ALTER  = "alter"
	AST_DROP   = "drop"
	AST_RENAME = "rename"
)
View Source
const (
	AST_JOIN          = "join"
	AST_STRAIGHT_JOIN = "straight_join"
	AST_LEFT_JOIN     = "left join"
	AST_RIGHT_JOIN    = "right join"
	AST_CROSS_JOIN    = "cross join"
	AST_NATURAL_JOIN  = "natural join"
)

JoinTableExpr.Join

View Source
const (
	AST_USE    = "use"
	AST_IGNORE = "ignore"
	AST_FORCE  = "force"
)
View Source
const (
	AST_WHERE  = "where"
	AST_HAVING = "having"
)

Where.Type

View Source
const (
	AST_EQ       = "="
	AST_LT       = "<"
	AST_GT       = ">"
	AST_LE       = "<="
	AST_GE       = ">="
	AST_NE       = "!="
	AST_NSE      = "<=>"
	AST_IN       = "in"
	AST_NOT_IN   = "not in"
	AST_LIKE     = "like"
	AST_NOT_LIKE = "not like"
)

ComparisonExpr.Operator

View Source
const (
	AST_BETWEEN     = "between"
	AST_NOT_BETWEEN = "not between"
)

RangeCond.Operator

View Source
const (
	AST_IS_NULL     = "is null"
	AST_IS_NOT_NULL = "is not null"
)

NullCheck.Operator

View Source
const (
	AST_BITAND = "&"
	AST_BITOR  = "|"
	AST_BITXOR = "^"
	AST_PLUS   = "+"
	AST_MINUS  = "-"
	AST_MULT   = "*"
	AST_DIV    = "/"
	AST_MOD    = "%"
)

BinaryExpr.Operator (single-byte constants for use in parser)

View Source
const (
	AST_UPLUS  = '+'
	AST_UMINUS = '-'
	AST_TILDA  = '~'
)

UnaryExpr.Operator

View Source
const (
	AST_ASC  = "asc"
	AST_DESC = "desc"
)

Order.Direction

View Source
const ADD = 57434
View Source
const ALL = 57359
View Source
const ALTER = 57417
View Source
const ANALYZE = 57420
View Source
const AND = 57404
View Source
const AS = 57361
View Source
const ASC = 57368
View Source
const (
	AST_DISTINCT = "distinct "
)

Select.Distinct

View Source
const BEGIN = 57436
View Source
const BETWEEN = 57366
View Source
const BY = 57356
View Source
const CASE = 57411
View Source
const CAST = 57410
View Source
const COLUMN = 57435
View Source
const COMMENT = 57383
View Source
const COMMIT = 57437
View Source
const CONCAT = 57406
View Source
const CREATE = 57416
View Source
const CROSS = 57398
View Source
const DEFAULT = 57374
View Source
const DELETE = 57350
View Source
const DESC = 57369
View Source
const DESCRIBE = 57440
View Source
const DISTINCT = 57360
View Source
const DROP = 57418
View Source
const DUPLICATE = 57372
View Source
const ELSE = 57414
View Source
const END = 57415
View Source
const EOFCHAR = 0x100
View Source
const EXCEPT = 57390
View Source
const EXISTS = 57362
View Source
const EXPLAIN = 57441
View Source
const FOR = 57358
View Source
const FORCE = 57401
View Source
const FOREIGN = 57433
View Source
const FROM = 57351
View Source
const GE = 57385
View Source
const GROUP = 57353
View Source
const HAVING = 57354
View Source
const ID = 57378
View Source
const IF = 57428
View Source
const IGNORE = 57427
View Source
const IN = 57363
View Source
const INDEX = 57424
View Source
const INNER = 57396
View Source
const INSERT = 57348
View Source
const INTERSECT = 57391
View Source
const INTO = 57371
View Source
const IS = 57364
View Source
const JOIN = 57392
View Source
const JSON_EXTRACT = 57407
View Source
const JSON_EXTRACT_TEXT = 57408
View Source
const KEY = 57373
View Source
const KEYRANGE = 57377
View Source
const LE = 57384
View Source
const LEFT = 57394
View Source
const LEX_ERROR = 57346
View Source
const LIKE = 57365
View Source
const LIMIT = 57357
View Source
const LIST_ARG = 57382
View Source
const LOCK = 57376
View Source
const MINUS = 57389
View Source
const NATURAL = 57399
View Source
const NE = 57386
View Source
const NOT = 57405
View Source
const NULL = 57367
View Source
const NULL_SAFE_EQUAL = 57387
View Source
const NUMBER = 57380
View Source
const ON = 57402
View Source
const OR = 57403
View Source
const ORDER = 57355
View Source
const OUTER = 57397
View Source
const PRIMARY = 57432
View Source
const REFERENCES = 57431
View Source
const RENAME = 57419
View Source
const RIGHT = 57395
View Source
const SELECT = 57347
View Source
const SET = 57375
View Source
const SHOW = 57439
View Source
const STRAIGHT_JOIN = 57393
View Source
const STRING = 57379
View Source
const TABLE = 57423
View Source
const TEMP = 57422
View Source
const TEMPORARY = 57421
View Source
const THEN = 57413
View Source
const TO = 57426
View Source
const UNARY = 57409
View Source
const UNION = 57388
View Source
const UNIQUE = 57429
View Source
const UPDATE = 57349
View Source
const USE = 57400
View Source
const USING = 57430
View Source
const VALUES = 57370
View Source
const VALUE_ARG = 57381
View Source
const VIEW = 57425
View Source
const WHEN = 57412
View Source
const WHERE = 57352
View Source
const WITH = 57438

Variables

View Source
var (
	SHARE         = []byte("share")
	MODE          = []byte("mode")
	IF_BYTES      = []byte("if")
	VALUES_BYTES  = []byte("values")
	PRIMARY_BYTES = []byte("primary")
)
View Source
var Aggregates = map[string]bool{
	"avg":          true,
	"bit_and":      true,
	"bit_or":       true,
	"bit_xor":      true,
	"count":        true,
	"group_concat": true,
	"max":          true,
	"min":          true,
	"std":          true,
	"stddev_pop":   true,
	"stddev_samp":  true,
	"stddev":       true,
	"sum":          true,
	"var_pop":      true,
	"var_samp":     true,
	"variance":     true,
}

Aggregates is a map of all aggregate functions.

Functions

func AsInterface

func AsInterface(node ValExpr) (interface{}, error)

AsInterface converts the ValExpr to an interface. It converts ValTuple to []interface{}, ValArg to string, StrVal to sqltypes.String, NumVal to sqltypes.Numeric, NullVal to nil. Otherwise, it returns an error.

func EncodeValue

func EncodeValue(buf *bytes.Buffer, value interface{}) error

func FetchBindVar

func FetchBindVar(name string, bindVariables map[string]interface{}) (val interface{}, isList bool, err error)

func ForceEOF

func ForceEOF(yylex interface{})

func FormatWithComments

func FormatWithComments(stmts PositionedStatements, comments []CommentEntry) string

FormatWithComments produces SQL by interleaving the formatted statements with comments by source position. Comments that fall inside a statement's [Start, End) are omitted (they are assumed to be in the AST and appear in String(stmt)).

func GetColName

func GetColName(node Expr) string

GetColName returns the column name, only if it's a simple expression. Otherwise, it returns "".

func GetCurrentPos

func GetCurrentPos(yylex interface{}) int

func GetTableName

func GetTableName(node SimpleTableExpr) string

GetTableName returns the table name from the SimpleTableExpr only if it's a simple expression. Otherwise, it returns "".

func HasINClause

func HasINClause(conditions []BoolExpr) bool

HasINCaluse returns true if any of the conditions has an IN clause.

func IsColName

func IsColName(node ValExpr) bool

IsColName returns true if the ValExpr is a *ColName.

func IsSimpleTuple

func IsSimpleTuple(node ValExpr) bool

IsSimpleTuple returns true if the ValExpr is a ValTuple that contains simple values or if it's a list arg.

func IsValue

func IsValue(node ValExpr) bool

IsValue returns true if the ValExpr is a string, number or value arg. NULL is not considered to be a value.

func Parse

Parse parses the sql and returns the list of statements (with positions). A single-statement input returns a one-element list.

func ParseFile

func ParseFile(fsys fs.FS, fn string) (PositionedStatements, []CommentEntry, error)

ParseFile parses a sql file and returns the statements contained in the file. If fsys is nil, the file is read from the OS (using os.ReadFile). If fsys is non-nil, the file is read via fs.ReadFile, which uses the FS's ReadFile method when it implements fs.ReadFileFS, otherwise Open and io.ReadAll.

func ParseReader

func ParseReader(r io.Reader) (PositionedStatements, []CommentEntry, error)

ParseReader parses sql from an io.Reader. It is not a streaming parser; the reader is read fully into memory before parsing.

func SelectStatementReferencesAny

func SelectStatementReferencesAny(sel SelectStatement, tableNames map[string]struct{}) bool

SelectStatementReferencesAny returns true if the given SelectStatement's FROM clause (and nested subqueries) references any of the given table names.

func SetAllowComments

func SetAllowComments(yylex interface{}, allow bool)

func SetParseTree

func SetParseTree(yylex interface{}, stmt Statement)

func String

func String(node SQLNode) string

String returns a string representation of an SQLNode.

func StringIn

func StringIn(str string, values ...string) bool

StringIn is a convenience function that returns true if str matches any of the values.

func TableNamesFromTableExprs

func TableNamesFromTableExprs(from TableExprs) map[string]struct{}

TableNamesFromTableExprs returns the set of table names referenced in the FROM clause (and in any nested JOINs or subqueries). Names are returned as single identifiers (e.g. "parcels_data"); qualified names use the Name only (the rightmost identifier).

Types

type AliasedTableExpr

type AliasedTableExpr struct {
	Comments Comments
	Position int
	Expr     SimpleTableExpr
	As       []byte
	Hints    *IndexHints
}

AliasedTableExpr represents a table expression coupled with an optional alias or index hint.

func (*AliasedTableExpr) Format

func (node *AliasedTableExpr) Format(buf *TrackedBuffer)

func (*AliasedTableExpr) ITableExpr

func (*AliasedTableExpr) ITableExpr()

type AndExpr

type AndExpr struct {
	Left                BoolExpr
	CommentsAfterLeft   Comments
	CommentsBeforeRight Comments
	Right               BoolExpr
	CommentsAfterRight  Comments
}

AndExpr represents an AND expression.

func (*AndExpr) Format

func (node *AndExpr) Format(buf *TrackedBuffer)

func (*AndExpr) IBoolExpr

func (*AndExpr) IBoolExpr()

func (*AndExpr) IExpr

func (*AndExpr) IExpr()

type BinaryExpr

type BinaryExpr struct {
	Operator    string // e.g. "+", "-", "||", "&"
	Left, Right Expr
}

BinaryExpr represents a binary value expression.

func (*BinaryExpr) Format

func (node *BinaryExpr) Format(buf *TrackedBuffer)

func (*BinaryExpr) IExpr

func (*BinaryExpr) IExpr()

func (*BinaryExpr) IValExpr

func (*BinaryExpr) IValExpr()

type BoolExpr

type BoolExpr interface {
	IBoolExpr()
	Expr
}

BoolExpr represents a boolean expression.

type CaseExpr

type CaseExpr struct {
	Expr  ValExpr
	Whens []*When
	Else  ValExpr
}

CaseExpr represents a CASE expression.

func (*CaseExpr) Format

func (node *CaseExpr) Format(buf *TrackedBuffer)

func (*CaseExpr) IExpr

func (*CaseExpr) IExpr()

func (*CaseExpr) IValExpr

func (*CaseExpr) IValExpr()

type ColName

type ColName struct {
	Name, Qualifier []byte
}

ColName represents a column name.

func (*ColName) Format

func (node *ColName) Format(buf *TrackedBuffer)

func (*ColName) IExpr

func (*ColName) IExpr()

func (*ColName) IValExpr

func (*ColName) IValExpr()

type ColTuple

type ColTuple interface {
	IColTuple()
	ValExpr
}

ColTuple represents a list of column values. It can be ValTuple, Subquery, ListArg.

type ColumnDefinition

type ColumnDefinition struct {
	Name    []byte
	Type    ColumnType
	Options ColumnOptions
}

ColumnDefinition represents a single column definition in CREATE TABLE.

func (*ColumnDefinition) Format

func (node *ColumnDefinition) Format(buf *TrackedBuffer)

type ColumnDefinitions

type ColumnDefinitions []*ColumnDefinition

ColumnDefinitions is a list of column definitions.

func (ColumnDefinitions) Format

func (node ColumnDefinitions) Format(buf *TrackedBuffer)

type ColumnOptions

type ColumnOptions struct {
	NotNull    bool
	PrimaryKey bool
}

ColumnOptions represents column constraints we currently model.

func (ColumnOptions) Format

func (node ColumnOptions) Format(buf *TrackedBuffer)

type ColumnType

type ColumnType struct {
	Name   []byte
	Length *int // optional length (e.g. varchar(255)) or precision (e.g. decimal(10,2))
	Scale  *int // optional scale (e.g. decimal(10,2)); only used with Length
}

ColumnType represents a column type: "integer", "varchar(255)", or "decimal(10,2)".

func (ColumnType) Format

func (node ColumnType) Format(buf *TrackedBuffer)

type Columns

type Columns []SelectExpr

Columns represents an insert column list. The syntax for Columns is a subset of SelectExprs. So, it's castable to a SelectExprs and can be analyzed as such.

func (Columns) Format

func (node Columns) Format(buf *TrackedBuffer)

type CommentEntry

type CommentEntry struct {
	Position int
	Comment  []byte
}

type Comments

type Comments [][]byte

Comments represents a list of comments.

func (Comments) Format

func (node Comments) Format(buf *TrackedBuffer)

func (Comments) IStatement

func (Comments) IStatement()

func (Comments) IsEmpty

func (node Comments) IsEmpty() bool

Will check to see if the bytes are just spaces, if so we will ignore the comment.

type ComparisonExpr

type ComparisonExpr struct {
	Operator    string
	Left, Right ValExpr
}

ComparisonExpr represents a two-value comparison expression.

func (*ComparisonExpr) Format

func (node *ComparisonExpr) Format(buf *TrackedBuffer)

func (*ComparisonExpr) IBoolExpr

func (*ComparisonExpr) IBoolExpr()

func (*ComparisonExpr) IExpr

func (*ComparisonExpr) IExpr()

type CreateTable

type CreateTable struct {
	Table       []byte
	Temporary   bool // true for CREATE TEMPORARY/TEMP TABLE
	Columns     ColumnDefinitions
	Constraints []*TableConstraint
}

CreateTable represents CREATE [TEMPORARY|TEMP] TABLE ... (column definitions and table constraints).

func (*CreateTable) Format

func (node *CreateTable) Format(buf *TrackedBuffer)

func (*CreateTable) IStatement

func (*CreateTable) IStatement()

func (*CreateTable) Temp

func (node *CreateTable) Temp() bool

Temp returns node.Temporary for convenience; both Temporary and Temp() can be used.

type CreateTableAsSelect

type CreateTableAsSelect struct {
	Table     []byte
	Select    SelectStatement
	Temporary bool // true for CREATE TEMPORARY/TEMP TABLE
}

CreateTableAsSelect represents CREATE [TEMPORARY|TEMP] TABLE ... AS [ ( ] SELECT [ ) ].

func (*CreateTableAsSelect) Format

func (node *CreateTableAsSelect) Format(buf *TrackedBuffer)

func (*CreateTableAsSelect) IStatement

func (*CreateTableAsSelect) IStatement()

func (*CreateTableAsSelect) Temp

func (node *CreateTableAsSelect) Temp() bool

Temp returns node.Temporary for convenience; both Temporary and Temp() can be used.

type DDL

type DDL struct {
	Action    string
	Table     []byte
	NewName   []byte
	Temporary bool
}

DDL represents a CREATE, ALTER, DROP or RENAME statement. Table is set for AST_ALTER, AST_DROP, AST_RENAME. NewName is set for AST_ALTER, AST_CREATE, AST_RENAME. Temporary is true for CREATE TEMPORARY/TEMP TABLE.

func (*DDL) Format

func (node *DDL) Format(buf *TrackedBuffer)

func (*DDL) IStatement

func (*DDL) IStatement()

func (*DDL) Temp

func (node *DDL) Temp() bool

Temp returns node.Temporary for convenience; both Temporary and Temp() can be used.

type Delete

type Delete struct {
	Comments Comments
	Table    *TableName
	Where    *Where
	OrderBy  OrderBy
	Limit    *Limit
}

Delete represents a DELETE statement.

func (*Delete) Format

func (node *Delete) Format(buf *TrackedBuffer)

func (*Delete) IStatement

func (*Delete) IStatement()

type EncoderFunc

type EncoderFunc func(value interface{}) ([]byte, error)

type ExistsExpr

type ExistsExpr struct {
	Subquery *Subquery
}

ExistsExpr represents an EXISTS expression.

func (*ExistsExpr) Format

func (node *ExistsExpr) Format(buf *TrackedBuffer)

func (*ExistsExpr) IBoolExpr

func (*ExistsExpr) IBoolExpr()

func (*ExistsExpr) IExpr

func (*ExistsExpr) IExpr()

type Expr

type Expr interface {
	IExpr()
	SQLNode
}

Expr represents an expression.

type FuncExpr

type FuncExpr struct {
	Name     []byte
	Distinct bool
	Exprs    SelectExprs
}

FuncExpr represents a function call.

func (*FuncExpr) Format

func (node *FuncExpr) Format(buf *TrackedBuffer)

func (*FuncExpr) IExpr

func (*FuncExpr) IExpr()

func (*FuncExpr) IValExpr

func (*FuncExpr) IValExpr()

func (*FuncExpr) IsAggregate

func (node *FuncExpr) IsAggregate() bool

type GroupBy

type GroupBy []ValExpr

GroupBy represents a GROUP BY clause.

func (GroupBy) Format

func (node GroupBy) Format(buf *TrackedBuffer)

type IndexHints

type IndexHints struct {
	Type    string
	Indexes [][]byte
}

IndexHints represents a list of index hints.

func (*IndexHints) Format

func (node *IndexHints) Format(buf *TrackedBuffer)

type Insert

type Insert struct {
	Comments Comments
	Table    *TableName
	Columns  Columns
	Rows     InsertRows
	OnDup    OnDup
}

Insert represents an INSERT statement.

func (*Insert) Format

func (node *Insert) Format(buf *TrackedBuffer)

func (*Insert) IStatement

func (*Insert) IStatement()

type InsertRows

type InsertRows interface {
	IInsertRows()
	SQLNode
}

InsertRows represents the rows for an INSERT statement.

type JoinTableExpr

type JoinTableExpr struct {
	LeftExpr  TableExpr
	Join      string
	RightExpr TableExpr
	On        BoolExpr
	Using     Columns // optional USING (col1, col2)
}

JoinTableExpr represents a TableExpr that's a JOIN operation.

func (*JoinTableExpr) Format

func (node *JoinTableExpr) Format(buf *TrackedBuffer)

func (*JoinTableExpr) ITableExpr

func (*JoinTableExpr) ITableExpr()

type KeyrangeExpr

type KeyrangeExpr struct {
	Start, End ValExpr
}

KeyrangeExpr represents a KEYRANGE expression.

func (*KeyrangeExpr) Format

func (node *KeyrangeExpr) Format(buf *TrackedBuffer)

func (*KeyrangeExpr) IBoolExpr

func (*KeyrangeExpr) IBoolExpr()

func (*KeyrangeExpr) IExpr

func (*KeyrangeExpr) IExpr()

type Limit

type Limit struct {
	Offset, Rowcount ValExpr
}

Limit represents a LIMIT clause.

func (*Limit) Format

func (node *Limit) Format(buf *TrackedBuffer)

func (*Limit) Limits

func (node *Limit) Limits() (offset, rowcount interface{}, err error)

Limits returns the values of the LIMIT clause as interfaces. The returned values can be nil for absent field, string for bind variable names, or int64 for an actual number. Otherwise, it's an error.

type ListArg

type ListArg []byte

ListArg represents a named list argument.

func (ListArg) Format

func (node ListArg) Format(buf *TrackedBuffer)

func (ListArg) IColTuple

func (ListArg) IColTuple()

func (ListArg) IExpr

func (ListArg) IExpr()

func (ListArg) IValExpr

func (ListArg) IValExpr()

type NonStarExpr

type NonStarExpr struct {
	Expr Expr
	As   []byte
}

NonStarExpr defines a non-'*' select expr.

func (*NonStarExpr) Format

func (node *NonStarExpr) Format(buf *TrackedBuffer)

func (*NonStarExpr) ISelectExpr

func (*NonStarExpr) ISelectExpr()

type NotExpr

type NotExpr struct {
	Expr BoolExpr
}

NotExpr represents a NOT expression.

func (*NotExpr) Format

func (node *NotExpr) Format(buf *TrackedBuffer)

func (*NotExpr) IBoolExpr

func (*NotExpr) IBoolExpr()

func (*NotExpr) IExpr

func (*NotExpr) IExpr()

type NullCheck

type NullCheck struct {
	Operator string
	Expr     ValExpr
}

NullCheck represents an IS NULL or an IS NOT NULL expression.

func (*NullCheck) Format

func (node *NullCheck) Format(buf *TrackedBuffer)

func (*NullCheck) IBoolExpr

func (*NullCheck) IBoolExpr()

func (*NullCheck) IExpr

func (*NullCheck) IExpr()

type NullVal

type NullVal struct{}

NullVal represents a NULL value.

func (*NullVal) Format

func (node *NullVal) Format(buf *TrackedBuffer)

func (*NullVal) IExpr

func (*NullVal) IExpr()

func (*NullVal) IValExpr

func (*NullVal) IValExpr()

type NumVal

type NumVal []byte

NumVal represents a number.

func (NumVal) Format

func (node NumVal) Format(buf *TrackedBuffer)

func (NumVal) IExpr

func (NumVal) IExpr()

func (NumVal) IValExpr

func (NumVal) IValExpr()

type OnDup

type OnDup UpdateExprs

OnDup represents an ON DUPLICATE KEY clause.

func (OnDup) Format

func (node OnDup) Format(buf *TrackedBuffer)

type OrExpr

type OrExpr struct {
	Left, Right BoolExpr
}

OrExpr represents an OR expression.

func (*OrExpr) Format

func (node *OrExpr) Format(buf *TrackedBuffer)

func (*OrExpr) IBoolExpr

func (*OrExpr) IBoolExpr()

func (*OrExpr) IExpr

func (*OrExpr) IExpr()

type Order

type Order struct {
	Expr      ValExpr
	Direction string
}

Order represents an ordering expression.

func (*Order) Format

func (node *Order) Format(buf *TrackedBuffer)

type OrderBy

type OrderBy []*Order

OrderBy represents an ORDER By clause.

func (OrderBy) Format

func (node OrderBy) Format(buf *TrackedBuffer)

type Other

type Other struct{}

Other represents a SHOW, DESCRIBE, or EXPLAIN statement. It should be used only as an indicator. It does not contain the full AST for the statement.

func (*Other) Format

func (node *Other) Format(buf *TrackedBuffer)

func (*Other) IStatement

func (*Other) IStatement()

type ParenBoolExpr

type ParenBoolExpr struct {
	Expr BoolExpr
}

ParenBoolExpr represents a parenthesized boolean expression.

func (*ParenBoolExpr) Format

func (node *ParenBoolExpr) Format(buf *TrackedBuffer)

func (*ParenBoolExpr) IBoolExpr

func (*ParenBoolExpr) IBoolExpr()

func (*ParenBoolExpr) IExpr

func (*ParenBoolExpr) IExpr()

type ParenTableExpr

type ParenTableExpr struct {
	Expr TableExpr
}

ParenTableExpr represents a parenthesized TableExpr.

func (*ParenTableExpr) Format

func (node *ParenTableExpr) Format(buf *TrackedBuffer)

func (*ParenTableExpr) ITableExpr

func (*ParenTableExpr) ITableExpr()

type ParsedQuery

type ParsedQuery struct {
	Query string
	// contains filtered or unexported fields
}

func (*ParsedQuery) GenerateQuery

func (pq *ParsedQuery) GenerateQuery(bindVariables map[string]interface{}) ([]byte, error)

func (*ParsedQuery) MarshalJSON

func (pq *ParsedQuery) MarshalJSON() ([]byte, error)

type PositionEmpty

type PositionEmpty struct {
	Position int
}

type PositionStatement

type PositionStatement struct {
	Start     int
	End       int
	Statement Statement
}

PositionStatement holds a Statement and its extent (Start, End) in the source. Used with CommentsTable to interleave comments when formatting. Comments inside [Start, End) are assumed to be in the AST and are not emitted again.

func (PositionStatement) Format

func (p PositionStatement) Format(buf *TrackedBuffer)

Format implements SQLNode by delegating to the inner Statement.

func (PositionStatement) IStatement

func (PositionStatement) IStatement()

func (PositionStatement) Position

func (p PositionStatement) Position() int

Position returns Start for backward compatibility.

type Positional

type Positional[T any] struct {
	Position int
	Value    T
}

type PositionedStatements

type PositionedStatements []PositionStatement

PositionedStatements is a list of statements with start positions. It implements Statement so it can be the parse tree root.

func (PositionedStatements) Format

func (p PositionedStatements) Format(buf *TrackedBuffer)

Format implements SQLNode by formatting each statement with " ;\n\n" between.

func (PositionedStatements) IStatement

func (PositionedStatements) IStatement()

type RangeCond

type RangeCond struct {
	Operator string
	Left     ValExpr
	From, To ValExpr
}

RangeCond represents a BETWEEN or a NOT BETWEEN expression.

func (*RangeCond) Format

func (node *RangeCond) Format(buf *TrackedBuffer)

func (*RangeCond) IBoolExpr

func (*RangeCond) IBoolExpr()

func (*RangeCond) IExpr

func (*RangeCond) IExpr()

type RowTuple

type RowTuple interface {
	IRowTuple()
	ValExpr
}

RowTuple represents a row of values. It can be ValTuple, Subquery.

type SQLNode

type SQLNode interface {
	Format(buf *TrackedBuffer)
}

SQLNode defines the interface for all nodes generated by the parser.

type Select

type Select struct {
	With         []*WithCTE // optional WITH clause (CTEs)
	Comments     Comments
	Distinct     string
	SelectExprs  SelectExprs
	From         TableExprs
	FromComments Comments
	Where        *Where
	GroupBy      GroupBy
	Having       *Where
	OrderBy      OrderBy
	Limit        *Limit
	Lock         string
}

Select represents a SELECT statement.

func (*Select) Format

func (node *Select) Format(buf *TrackedBuffer)

func (*Select) IInsertRows

func (*Select) IInsertRows()

func (*Select) ISelectStatement

func (*Select) ISelectStatement()

func (*Select) IStatement

func (*Select) IStatement()

type SelectExpr

type SelectExpr interface {
	ISelectExpr()
	SQLNode
}

SelectExpr represents a SELECT expression.

type SelectExprs

type SelectExprs []SelectExpr

SelectExprs represents SELECT expressions.

func (SelectExprs) Format

func (node SelectExprs) Format(buf *TrackedBuffer)

type SelectStatement

type SelectStatement interface {
	ISelectStatement()
	IStatement()
	IInsertRows()
	SQLNode
}

SelectStatement any SELECT statement.

type Set

type Set struct {
	Comments Comments
	Exprs    UpdateExprs
}

Set represents a SET statement.

func (*Set) Format

func (node *Set) Format(buf *TrackedBuffer)

func (*Set) IStatement

func (*Set) IStatement()

type SimpleTableExpr

type SimpleTableExpr interface {
	ISimpleTableExpr()
	SQLNode
}

SimpleTableExpr represents a simple table expression.

type StarExpr

type StarExpr struct {
	TableName []byte
}

StarExpr defines a '*' or 'table.*' expression.

func (*StarExpr) Format

func (node *StarExpr) Format(buf *TrackedBuffer)

func (*StarExpr) ISelectExpr

func (*StarExpr) ISelectExpr()

type Statement

type Statement interface {
	IStatement()
	SQLNode
}

Statement represents a statement.

type Statements

type Statements []Statement

Statements represents a set of statements.

func (Statements) Format

func (sts Statements) Format(buf *TrackedBuffer)

func (Statements) IStatement

func (Statements) IStatement()

type StrVal

type StrVal []byte

StrVal represents a string value.

func (StrVal) Format

func (node StrVal) Format(buf *TrackedBuffer)

func (StrVal) IExpr

func (StrVal) IExpr()

func (StrVal) IValExpr

func (StrVal) IValExpr()

type Subquery

type Subquery struct {
	Select SelectStatement
}

Subquery represents a subquery.

func (*Subquery) Format

func (node *Subquery) Format(buf *TrackedBuffer)

func (*Subquery) IColTuple

func (*Subquery) IColTuple()

func (*Subquery) IExpr

func (*Subquery) IExpr()

func (*Subquery) IRowTuple

func (*Subquery) IRowTuple()

func (*Subquery) ISimpleTableExpr

func (*Subquery) ISimpleTableExpr()

func (*Subquery) IValExpr

func (*Subquery) IValExpr()

type TableConstraint

type TableConstraint struct {
	Kind       string   // "primary key", "unique", or "foreign key"
	Columns    [][]byte // column names
	RefTable   []byte   // for FOREIGN KEY: referenced table
	RefColumns [][]byte // for FOREIGN KEY: referenced column names
}

TableConstraint represents a table-level constraint: PRIMARY KEY, UNIQUE, or FOREIGN KEY.

func (*TableConstraint) Format

func (node *TableConstraint) Format(buf *TrackedBuffer)

type TableExpr

type TableExpr interface {
	ITableExpr()
	SQLNode
}

TableExpr represents a table expression.

type TableExprs

type TableExprs []TableExpr

TableExprs represents a list of table expressions.

func (TableExprs) Format

func (node TableExprs) Format(buf *TrackedBuffer)

type TableFunc

type TableFunc struct {
	Name  []byte
	Exprs SelectExprs
}

TableFunc represents a table-valued function call in FROM (e.g. json_each(...)).

func (*TableFunc) Format

func (node *TableFunc) Format(buf *TrackedBuffer)

func (*TableFunc) ISimpleTableExpr

func (*TableFunc) ISimpleTableExpr()

type TableName

type TableName struct {
	Name, Qualifier []byte
}

TableName represents a table name.

func (*TableName) Format

func (node *TableName) Format(buf *TrackedBuffer)

func (*TableName) ISimpleTableExpr

func (*TableName) ISimpleTableExpr()

type Tokenizer

type Tokenizer struct {
	InStream      *strings.Reader
	Input         string // original SQL, used to compute line/column for errors
	AllowComments bool
	CommentsTable []CommentEntry
	ForceEOF      bool

	Position int

	LastError *TokenizerError

	ParseTree Statement
	// contains filtered or unexported fields
}

Tokenizer is the struct used to generate SQL tokens for the parser.

func NewStringTokenizer

func NewStringTokenizer(sql string) *Tokenizer

NewStringTokenizer creates a new Tokenizer for the sql string.

func (*Tokenizer) ConsumeNext

func (tkn *Tokenizer) ConsumeNext(buffer *bytes.Buffer)

func (*Tokenizer) Error

func (tkn *Tokenizer) Error(err string)

Error is called by go yacc when the parser hits a syntax error. If the lexer already set LastError (e.g. for LEX_ERROR), we keep it so the more specific message is preserved.

func (*Tokenizer) GetAndPopStatementStart

func (tkn *Tokenizer) GetAndPopStatementStart() int

GetAndPopStatementStart returns the position at which the current statement started and clears it. Call from the grammar when reducing a "command". Returns 0 if the stack is empty.

func (*Tokenizer) Lex

func (tkn *Tokenizer) Lex(lval *yySymType) int

Lex returns the next token form the Tokenizer. This function is used by go yacc.

func (*Tokenizer) Scan

func (tkn *Tokenizer) Scan() (int, []byte)

Scan scans the tokenizer for the next token and returns the token type and an optional value.

type TokenizerError

type TokenizerError struct {
	Err      string
	Position int
	Line     int // 1-based line number
	Column   int // 1-based column number
	ErrToken []byte
}

TokenizerError is the struct used to hold the errors if there is any from Parsing. Line and Column are 1-based; Position is 0-based byte offset. Error() returns a string like "line N, column M (position P): message" and optionally " near \"token\"" when ErrToken is set.

func (*TokenizerError) Error

func (te *TokenizerError) Error() string

type TrackedBuffer

type TrackedBuffer struct {
	*bytes.Buffer
	// contains filtered or unexported fields
}

TrackedBuffer is used to rebuild a query from the ast. bindLocations keeps track of locations in the buffer that use bind variables for efficient future substitutions. nodeFormatter is the formatting function the buffer will use to format a node. By default(nil), it's FormatNode. But you can supply a different formatting function if you want to generate a query that's different from the default.

func NewTrackedBuffer

func NewTrackedBuffer(nodeFormatter func(buf *TrackedBuffer, node SQLNode)) *TrackedBuffer

func (*TrackedBuffer) HasBindVars

func (buf *TrackedBuffer) HasBindVars() bool

func (*TrackedBuffer) Myprintf

func (buf *TrackedBuffer) Myprintf(format string, values ...interface{})

Myprintf mimics fmt.Fprintf(buf, ...), but limited to Node(%v), Node.Value(%s) and string(%s). It also allows a %a for a value argument, in which case it adds tracking info for future substitutions.

The name must be something other than the usual Printf() to avoid "go vet" warnings due to our custom format specifiers.

func (*TrackedBuffer) ParsedQuery

func (buf *TrackedBuffer) ParsedQuery() *ParsedQuery

func (*TrackedBuffer) WriteArg

func (buf *TrackedBuffer) WriteArg(arg string)

WriteArg writes a value argument into the buffer. arg should not contain the ':' prefix. It also adds tracking info for future substitutions.

type TupleEqualityList

type TupleEqualityList struct {
	Columns []string
	Rows    [][]sqltypes.Value
}

func (*TupleEqualityList) Encode

func (tpl *TupleEqualityList) Encode(buf *bytes.Buffer) error

type UnaryExpr

type UnaryExpr struct {
	Operator byte
	Expr     Expr
}

UnaryExpr represents a unary value expression.

func (*UnaryExpr) Format

func (node *UnaryExpr) Format(buf *TrackedBuffer)

func (*UnaryExpr) IExpr

func (*UnaryExpr) IExpr()

func (*UnaryExpr) IValExpr

func (*UnaryExpr) IValExpr()

type Union

type Union struct {
	Type        string
	Left, Right SelectStatement
}

Union represents a UNION statement.

func (*Union) Format

func (node *Union) Format(buf *TrackedBuffer)

func (*Union) IInsertRows

func (*Union) IInsertRows()

func (*Union) ISelectStatement

func (*Union) ISelectStatement()

func (*Union) IStatement

func (*Union) IStatement()

type Update

type Update struct {
	Comments Comments
	Table    *TableName
	Exprs    UpdateExprs
	Where    *Where
	OrderBy  OrderBy
	Limit    *Limit
}

Update represents an UPDATE statement.

func (*Update) Format

func (node *Update) Format(buf *TrackedBuffer)

func (*Update) IStatement

func (*Update) IStatement()

type UpdateExpr

type UpdateExpr struct {
	Name *ColName
	Expr ValExpr
}

UpdateExpr represents an update expression.

func (*UpdateExpr) Format

func (node *UpdateExpr) Format(buf *TrackedBuffer)

type UpdateExprs

type UpdateExprs []*UpdateExpr

UpdateExprs represents a list of update expressions.

func (UpdateExprs) Format

func (node UpdateExprs) Format(buf *TrackedBuffer)

type ValArg

type ValArg []byte

ValArg represents a named bind var argument.

func (ValArg) Format

func (node ValArg) Format(buf *TrackedBuffer)

func (ValArg) IExpr

func (ValArg) IExpr()

func (ValArg) IValExpr

func (ValArg) IValExpr()

type ValExpr

type ValExpr interface {
	IValExpr()
	Expr
}

ValExpr represents a value expression.

type ValExprs

type ValExprs []ValExpr

ValExprs represents a list of value expressions. It's not a valid expression because it's not parenthesized.

func (ValExprs) Format

func (node ValExprs) Format(buf *TrackedBuffer)

type ValTuple

type ValTuple ValExprs

ValTuple represents a tuple of actual values.

func (ValTuple) Format

func (node ValTuple) Format(buf *TrackedBuffer)

func (ValTuple) IColTuple

func (ValTuple) IColTuple()

func (ValTuple) IExpr

func (ValTuple) IExpr()

func (ValTuple) IRowTuple

func (ValTuple) IRowTuple()

func (ValTuple) IValExpr

func (ValTuple) IValExpr()

type Values

type Values []RowTuple

Values represents a VALUES clause.

func (Values) Format

func (node Values) Format(buf *TrackedBuffer)

func (Values) IInsertRows

func (Values) IInsertRows()

type When

type When struct {
	Cond BoolExpr
	Val  ValExpr
}

When represents a WHEN sub-expression.

func (*When) Format

func (node *When) Format(buf *TrackedBuffer)

type Where

type Where struct {
	Type     string
	Comments Comments
	Expr     BoolExpr
}

Where represents a WHERE or HAVING clause.

func NewWhere

func NewWhere(typ string, expr BoolExpr) *Where

NewWhere creates a WHERE or HAVING clause out of a BoolExpr. If the expression is nil, it returns nil.

func (*Where) Format

func (node *Where) Format(buf *TrackedBuffer)

type WithCTE

type WithCTE struct {
	Name   []byte
	Select SelectStatement
}

WithCTE represents one common table expression: name AS (SELECT ...).

func (*WithCTE) Format

func (node *WithCTE) Format(buf *TrackedBuffer)

Directories

Path Synopsis
cmd
sqlfmt command
sqlfmt — Will take an sql file and print out a pretty version, if it can not parse the sql file.
sqlfmt — Will take an sql file and print out a pretty version, if it can not parse the sql file.
examples
find_tables_from_sources command
FindTablesFromSources parses a SQL script and reports table names that are created using data from a given set of source tables (e.g.
FindTablesFromSources parses a SQL script and reports table names that are created using data from a given set of source tables (e.g.
Package hack gives you some efficient functionality at the cost of breaking some Go rules.
Package hack gives you some efficient functionality at the cost of breaking some Go rules.
Package sqltypes implements interfaces and types that represent SQL values.
Package sqltypes implements interfaces and types that represent SQL values.

Jump to

Keyboard shortcuts

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