Documentation ¶
Index ¶
- Constants
- Variables
- func IsNodeHasValue(node SQLNode) bool
- func ParseDDL(sql string) (ast.StmtNode, error)
- func String(node SQLNode) string
- type AliasedTableExpr
- type AndExpr
- type BinaryExpr
- type BoolExpr
- type BoolVal
- type CaseExpr
- type ColName
- type ColTuple
- type ColumnDef
- type ColumnOption
- type ColumnOptionType
- type Columns
- type Comments
- type ComparisonExpr
- type Constraint
- type CreateTable
- type DDL
- type DatabaseOptionType
- type Delete
- type ExistsExpr
- type Explain
- type Expr
- type FieldType
- type FuncExpr
- type GroupBy
- type IndexHints
- type Insert
- type InsertRows
- type IsExpr
- type JoinTableExpr
- type KeyrangeExpr
- type Limit
- type ListArg
- type NonStarExpr
- type NotExpr
- type NullVal
- type NumVal
- type OnDup
- type OrExpr
- type Order
- type OrderBy
- type Other
- type ParenBoolExpr
- type ParenTableExpr
- type ParsedQuery
- type RangeCond
- type RowTuple
- type SQLName
- type SQLNode
- type Select
- type SelectExpr
- type SelectExprs
- type SelectStatement
- type Set
- type Show
- type SimpleTableExpr
- type StarExpr
- type Statement
- type Str
- type StrVal
- type Subquery
- type TableExpr
- type TableExprs
- type TableName
- type TableOption
- type TableOptionType
- type Tokenizer
- type TrackedBuffer
- type Tree
- type TreeNode
- type UnaryExpr
- type Union
- type Update
- type UpdateExpr
- type UpdateExprs
- type UseDB
- type ValArg
- type ValExpr
- type ValExprs
- type ValTuple
- type Values
- type When
- type Where
Constants ¶
const ( ForUpdateStr = " for update" )
Select.Lock
const ( UnionStr = "union" UnionAllStr = "union all" SetMinusStr = "minus" ExceptStr = "except" IntersectStr = "intersect" )
Union.Type
const ( CreateStr = "create" AlterStr = "alter" DropStr = "drop" RenameStr = "rename" )
DDL strings.
const ( JoinStr = "join" StraightJoinStr = "straight_join" LeftJoinStr = "left join" RightJoinStr = "right join" NaturalJoinStr = "natural join" NaturalLeftJoinStr = "natural left join" NaturalRightJoinStr = "natural right join" )
JoinTableExpr.Join
const ( UseStr = "use " IgnoreStr = "ignore " ForceStr = "force " )
Index hints.
const ( WhereStr = "where" HavingStr = "having" )
Where.Type
const ( EqualStr = "=" LessThanStr = "<" GreaterThanStr = ">" LessEqualStr = "<=" GreaterEqualStr = ">=" NotEqualStr = "!=" NullSafeEqualStr = "<=>" InStr = "in" NotInStr = "not in" LikeStr = "like" NotLikeStr = "not like" RegexpStr = "regexp" NotRegexpStr = "not regexp" )
ComparisonExpr.Operator
const ( BetweenStr = "between" NotBetweenStr = "not between" )
RangeCond.Operator
const ( IsNullStr = "is null" IsNotNullStr = "is not null" IsTrueStr = "is true" IsNotTrueStr = "is not true" IsFalseStr = "is false" IsNotFalseStr = "is not false" )
IsExpr.Operator
const ( BitAndStr = "&" BitOrStr = "|" BitXorStr = "^" PlusStr = "+" MinusStr = "-" MultStr = "*" DivStr = "/" ModStr = "%" ShiftLeftStr = "<<" ShiftRightStr = ">>" )
BinaryExpr.Operator
const ( UPlusStr = '+' UMinusStr = '-' TildaStr = '~' )
UnaryExpr.Operator
const ( AscScr = "asc" DescScr = "desc" )
Order.Direction
const ALL = 57363
const ALTER = 57419
const ANALYZE = 57422
const AND = 57399
const AS = 57365
const ASC = 57367
const BETWEEN = 57401
const BY = 57360
const CASE = 57402
const COMMENT = 57394
const CREATE = 57418
const CROSS = 57384
const DATABASE = 57424
const DEFAULT = 57372
const DELETE = 57354
const DESC = 57368
const DESCRIBE = 57433
const DISTINCT = 57364
const DROP = 57420
const DUPLICATE = 57370
const (
DistinctStr = "distinct "
)
Select.Distinct
const ELSE = 57405
const END = 57417
const EXCEPT = 57349
const EXISTS = 57366
const EXPLAIN = 57434
const FALSE = 57397
const FOR = 57362
const FORCE = 57387
const FROM = 57355
const GE = 57407
const GROUP = 57357
const HAVING = 57358
const ID = 57389
const IF = 57429
const IGNORE = 57428
const IN = 57413
const INDEX = 57425
const INNER = 57382
const INSERT = 57352
const INTERSECT = 57350
const INTO = 57369
const IS = 57410
const JOIN = 57378
const KEY = 57371
const KEYRANGE = 57375
const LAST_INSERT_ID = 57377
const LE = 57406
const LEFT = 57380
const LEX_ERROR = 57346
const LIKE = 57411
const LIMIT = 57361
const LIST_ARG = 57393
const LOCK = 57374
const MINUS = 57348
const NATURAL = 57385
const NE = 57408
const NOT = 57400
const NULL = 57395
const NULL_SAFE_EQUAL = 57409
const NUMBER = 57391
const ON = 57388
const OR = 57398
const ORDER = 57359
const OUTER = 57383
const REGEXP = 57412
const RENAME = 57421
const RIGHT = 57381
const SELECT = 57351
const SET = 57373
const SHIFT_LEFT = 57414
const SHIFT_RIGHT = 57415
const SHOW = 57432
const STRAIGHT_JOIN = 57379
const STRING = 57390
const TABLE = 57423
const THEN = 57404
const TO = 57427
const TRUE = 57396
const UNARY = 57416
const UNION = 57347
const UNIQUE = 57430
const UPDATE = 57353
const USE = 57386
const USING = 57431
const VALUES = 57376
const VALUE_ARG = 57392
const VIEW = 57426
const WHEN = 57403
const WHERE = 57356
Variables ¶
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.
var AllExprs = []Expr{ &AndExpr{}, &OrExpr{}, &NotExpr{}, &ParenBoolExpr{}, &ComparisonExpr{}, &RangeCond{}, &IsExpr{}, &ExistsExpr{}, &KeyrangeExpr{}, StrVal(""), NumVal(""), ValArg(""), &NullVal{}, BoolVal(false), &ColName{}, ValTuple{}, &Subquery{}, ListArg(""), &BinaryExpr{}, &UnaryExpr{}, &FuncExpr{}, &CaseExpr{}, }
AllExprs must contain one variable for each AST type that satisfies Expr. This allows for external packages to verify that they're not missing on any types.
Functions ¶
func IsNodeHasValue ¶
Types ¶
type AliasedTableExpr ¶
type AliasedTableExpr struct { Expr SimpleTableExpr As SQLName Hints *IndexHints }
AliasedTableExpr represents a table expression coupled with an optional alias or index hint.
func (*AliasedTableExpr) Format ¶
func (node *AliasedTableExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*AliasedTableExpr) Nodes ¶
func (node *AliasedTableExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type BinaryExpr ¶
BinaryExpr represents a binary value expression.
func (*BinaryExpr) Format ¶
func (node *BinaryExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*BinaryExpr) Nodes ¶
func (node *BinaryExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type BoolExpr ¶
type BoolExpr interface { Expr // contains filtered or unexported methods }
BoolExpr represents a boolean expression.
type ColTuple ¶
type ColTuple interface { ValExpr // contains filtered or unexported methods }
ColTuple represents a list of column values. It can be ValTuple, Subquery, ListArg.
type ColumnDef ¶
type ColumnDef struct { Name *ColName Type *FieldType Options []*ColumnOption }
ColumnDef is used for parsing column definition from SQL.
type ColumnOption ¶
type ColumnOption struct { Tp ColumnOptionType // The value For Default or On Update. Expr Expr }
ColumnOption is used for parsing column constraint info from SQL.
type ColumnOptionType ¶
type ColumnOptionType int
ColumnOptionType is the type for ColumnOption.
const ( ColumnOptionNoOption ColumnOptionType = iota ColumnOptionPrimaryKey ColumnOptionNotNull ColumnOptionAutoIncrement ColumnOptionDefaultValue ColumnOptionUniq ColumnOptionIndex ColumnOptionUniqIndex ColumnOptionKey ColumnOptionUniqKey ColumnOptionNull ColumnOptionOnUpdate // For Timestamp and Datetime only. ColumnOptionFulltext ColumnOptionComment )
ColumnOption types.
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.
type ComparisonExpr ¶
ComparisonExpr represents a two-value comparison expression.
func (*ComparisonExpr) Format ¶
func (node *ComparisonExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*ComparisonExpr) Nodes ¶
func (node *ComparisonExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type Constraint ¶
type Constraint struct { }
type CreateTable ¶
type CreateTable struct { IfNotExists bool Table *TableName Cols []*ColumnDef Constraints []*Constraint Options []*TableOption }
CreateTable is a create table ddl
type DDL ¶
DDL represents a CREATE, ALTER, DROP or RENAME statement. Table is set for AlterStr, DropStr, RenameStr. NewName is set for AlterStr, CreateStr, RenameStr.
type DatabaseOptionType ¶
type DatabaseOptionType int
const ( DatabaseOptionNone DatabaseOptionType = iota DatabaseOptionCharset DatabaseOptionCollate )
Database option types.
type ExistsExpr ¶
type ExistsExpr struct {
Subquery *Subquery
}
ExistsExpr represents an EXISTS expression.
func (*ExistsExpr) Format ¶
func (node *ExistsExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*ExistsExpr) Nodes ¶
func (node *ExistsExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type Expr ¶
type Expr interface { SQLNode // contains filtered or unexported methods }
Expr represents an expression.
type FuncExpr ¶
type FuncExpr struct { Name string Distinct bool Exprs SelectExprs }
FuncExpr represents a function call.
func (*FuncExpr) IsAggregate ¶
IsAggregate returns true if the function is an aggregate.
type IndexHints ¶
IndexHints represents a list of index hints.
func (*IndexHints) Format ¶
func (node *IndexHints) Format(buf *TrackedBuffer)
Format formats the node.
func (*IndexHints) Nodes ¶
func (node *IndexHints) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type Insert ¶
type Insert struct { Comments Comments Ignore string Table *TableName Columns Columns Rows InsertRows OnDup OnDup }
Insert represents an INSERT statement.
type InsertRows ¶
type InsertRows interface { SQLNode // contains filtered or unexported methods }
InsertRows represents the rows for an INSERT statement.
type JoinTableExpr ¶
JoinTableExpr represents a TableExpr that's a JOIN operation.
func (*JoinTableExpr) Format ¶
func (node *JoinTableExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*JoinTableExpr) Nodes ¶
func (node *JoinTableExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type KeyrangeExpr ¶
type KeyrangeExpr struct {
Start, End ValExpr
}
KeyrangeExpr represents a KEYRANGE expression.
func (*KeyrangeExpr) Format ¶
func (node *KeyrangeExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*KeyrangeExpr) Nodes ¶
func (node *KeyrangeExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type Limit ¶
type Limit struct {
Offset, Rowcount ValExpr
}
Limit represents a LIMIT clause.
type NonStarExpr ¶
NonStarExpr defines a non-'*' select expr.
func (*NonStarExpr) Format ¶
func (node *NonStarExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*NonStarExpr) Nodes ¶
func (node *NonStarExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type Other ¶
type Other struct{}
Other represents a SHOW, or DESCRIBE statement. It should be used only as an indicator. It does not contain the full AST for the statement.
type ParenBoolExpr ¶
type ParenBoolExpr struct {
Expr BoolExpr
}
ParenBoolExpr represents a parenthesized boolean expression.
func (*ParenBoolExpr) Format ¶
func (node *ParenBoolExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*ParenBoolExpr) Nodes ¶
func (node *ParenBoolExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type ParenTableExpr ¶
type ParenTableExpr struct {
Exprs TableExprs
}
ParenTableExpr represents a parenthesized list of TableExpr.
func (*ParenTableExpr) Format ¶
func (node *ParenTableExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*ParenTableExpr) Nodes ¶
func (node *ParenTableExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type ParsedQuery ¶
type ParsedQuery struct { Query string // contains filtered or unexported fields }
ParsedQuery represents a parsed query where bind locations are precompued for fast substitutions.
func GenerateParsedQuery ¶
func GenerateParsedQuery(node SQLNode) *ParsedQuery
GenerateParsedQuery returns a ParsedQuery of the ast.
type RangeCond ¶
RangeCond represents a BETWEEN or a NOT BETWEEN expression.
func (*RangeCond) Format ¶
func (node *RangeCond) Format(buf *TrackedBuffer)
Format formats the node.
type RowTuple ¶
type RowTuple interface { ValExpr // contains filtered or unexported methods }
RowTuple represents a row of values. It can be ValTuple, Subquery.
type SQLName ¶
type SQLName string
SQLName is an SQL identifier. It will be escaped with backquotes if it matches a keyword.
type SQLNode ¶
type SQLNode interface { Format(buf *TrackedBuffer) Nodes() []SQLNode }
SQLNode defines the interface for all nodes generated by the parser.
type Select ¶
type Select struct { Comments Comments Distinct string SelectExprs SelectExprs From TableExprs Where *Where GroupBy GroupBy Having *Where OrderBy OrderBy Limit *Limit Lock string }
Select represents a SELECT statement.
type SelectExpr ¶
type SelectExpr interface { SQLNode // contains filtered or unexported methods }
SelectExpr represents a SELECT expression.
type SelectExprs ¶
type SelectExprs []SelectExpr
SelectExprs represents SELECT expressions.
func (SelectExprs) Format ¶
func (node SelectExprs) Format(buf *TrackedBuffer)
Format formats the node.
func (SelectExprs) Nodes ¶
func (node SelectExprs) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type SelectStatement ¶
type SelectStatement interface { SQLNode // contains filtered or unexported methods }
SelectStatement any SELECT statement.
type Show ¶
type Show struct { Key string From TableExprs Where *Where Like *ComparisonExpr }
func (*Show) Format ¶
func (node *Show) Format(buf *TrackedBuffer)
type SimpleTableExpr ¶
type SimpleTableExpr interface { SQLNode // contains filtered or unexported methods }
SimpleTableExpr represents a simple table expression.
type StarExpr ¶
type StarExpr struct {
TableName SQLName
}
StarExpr defines a '*' or 'table.*' expression.
type Statement ¶
type Statement interface { SQLNode // contains filtered or unexported methods }
Statement represents a statement.
type TableExpr ¶
type TableExpr interface { SQLNode // contains filtered or unexported methods }
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)
Format formats the node.
func (TableExprs) Nodes ¶
func (node TableExprs) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type TableName ¶
type TableName struct {
Name, Qualifier SQLName
}
TableName represents a table name.
func (*TableName) Format ¶
func (node *TableName) Format(buf *TrackedBuffer)
Format formats the node.
type TableOption ¶
type TableOption struct { }
type TableOptionType ¶
type TableOptionType int
TableOptionType is the type for TableOption
const ( TableOptionNone TableOptionType = iota TableOptionEngine TableOptionCharset TableOptionCollate TableOptionAutoIncrement TableOptionComment TableOptionAvgRowLength TableOptionCheckSum TableOptionCompression TableOptionConnection TableOptionPassword TableOptionKeyBlockSize TableOptionMaxRows TableOptionMinRows TableOptionDelayKeyWrite )
TableOption types.
type Tokenizer ¶
type Tokenizer struct { InStream *strings.Reader AllowComments bool ForceEOF bool Position int LastError string ParseTree Statement // contains filtered or unexported fields }
Tokenizer is the struct used to generate SQL tokens for the parser.
func NewStringTokenizer ¶
NewStringTokenizer creates a new Tokenizer for the sql string.
type TrackedBuffer ¶
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
NewTrackedBuffer creates a new TrackedBuffer.
func (*TrackedBuffer) HasBindVars ¶
func (buf *TrackedBuffer) HasBindVars() bool
HasBindVars returns true if the parsed query uses bind vars.
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
ParsedQuery returns a ParsedQuery that contains bind locations for easy substitution.
func (*TrackedBuffer) WriteArg ¶
func (buf *TrackedBuffer) WriteArg(arg string)
WriteArg writes a value argument into the buffer along with tracking information for future substitutions. arg must contain the ":" or "::" prefix.
type UnaryExpr ¶
UnaryExpr represents a unary value expression.
func (*UnaryExpr) Format ¶
func (node *UnaryExpr) Format(buf *TrackedBuffer)
Format formats the node.
type Union ¶
type Union struct { Type string Left, Right SelectStatement }
Union represents a UNION statement.
type Update ¶
type Update struct { Comments Comments Table *TableName Exprs UpdateExprs Where *Where OrderBy OrderBy Limit *Limit }
Update represents an UPDATE statement.
type UpdateExpr ¶
UpdateExpr represents an update expression.
func (*UpdateExpr) Format ¶
func (node *UpdateExpr) Format(buf *TrackedBuffer)
Format formats the node.
func (*UpdateExpr) Nodes ¶
func (node *UpdateExpr) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type UpdateExprs ¶
type UpdateExprs []*UpdateExpr
UpdateExprs represents a list of update expressions.
func (UpdateExprs) Format ¶
func (node UpdateExprs) Format(buf *TrackedBuffer)
Format formats the node.
func (UpdateExprs) Nodes ¶
func (node UpdateExprs) Nodes() []SQLNode
Nodes renturn this sqlnode's all children node.
type UseDB ¶
type UseDB struct {
DB string
}
func (*UseDB) Format ¶
func (node *UseDB) Format(buf *TrackedBuffer)
type ValExpr ¶
type ValExpr interface { Expr // contains filtered or unexported methods }
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.