github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/ast/ast.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 // Package ast is the abstract syntax tree parsed from a SQL statement by parser. 15 // It can be analysed and transformed by optimizer. 16 package ast 17 18 import ( 19 "github.com/insionng/yougam/libraries/pingcap/tidb/context" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/model" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/util/types" 22 ) 23 24 // Node is the basic element of the AST. 25 // Interfaces embed Node should have 'Node' name suffix. 26 type Node interface { 27 // Accept accepts Visitor to visit itself. 28 // The returned node should replace original node. 29 // ok returns false to stop visiting. 30 // 31 // Implementation of this method should first call visitor.Enter, 32 // assign the returned node to its method receiver, if skipChildren returns true, 33 // children should be skipped. Otherwise, call its children in particular order that 34 // later elements depends on former elements. Finally, return visitor.Leave. 35 Accept(v Visitor) (node Node, ok bool) 36 // Text returns the original text of the element. 37 Text() string 38 // SetText sets original text to the Node. 39 SetText(text string) 40 } 41 42 // Flags indicates whether an expression contains certain types of expression. 43 const ( 44 FlagConstant uint64 = 0 45 FlagHasParamMarker uint64 = 1 << iota 46 FlagHasFunc 47 FlagHasReference 48 FlagHasAggregateFunc 49 FlagHasSubquery 50 FlagHasVariable 51 FlagHasDefault 52 FlagPreEvaluated 53 ) 54 55 // ExprNode is a node that can be evaluated. 56 // Name of implementations should have 'Expr' suffix. 57 type ExprNode interface { 58 // Node is embedded in ExprNode. 59 Node 60 // SetType sets evaluation type to the expression. 61 SetType(tp *types.FieldType) 62 // GetType gets the evaluation type of the expression. 63 GetType() *types.FieldType 64 // SetValue sets value to the expression. 65 SetValue(val interface{}) 66 // GetValue gets value of the expression. 67 GetValue() interface{} 68 // SetDatum sets datum to the expression. 69 SetDatum(datum types.Datum) 70 // GetDatum gets datum of the expression. 71 GetDatum() *types.Datum 72 // SetFlag sets flag to the expression. 73 // Flag indicates whether the expression contains 74 // parameter marker, reference, aggregate function... 75 SetFlag(flag uint64) 76 // GetFlag returns the flag of the expression. 77 GetFlag() uint64 78 } 79 80 // FuncNode represents function call expression node. 81 type FuncNode interface { 82 ExprNode 83 functionExpression() 84 } 85 86 // StmtNode represents statement node. 87 // Name of implementations should have 'Stmt' suffix. 88 type StmtNode interface { 89 Node 90 statement() 91 } 92 93 // DDLNode represents DDL statement node. 94 type DDLNode interface { 95 StmtNode 96 ddlStatement() 97 } 98 99 // DMLNode represents DML statement node. 100 type DMLNode interface { 101 StmtNode 102 dmlStatement() 103 } 104 105 // ResultField represents a result field which can be a column from a table, 106 // or an expression in select field. It is a generated property during 107 // binding process. ResultField is the key element to evaluate a ColumnNameExpr. 108 // After resolving process, every ColumnNameExpr will be resolved to a ResultField. 109 // During execution, every row retrieved from table will set the row value to 110 // ResultFields of that table, so ColumnNameExpr resolved to that ResultField can be 111 // easily evaluated. 112 type ResultField struct { 113 Column *model.ColumnInfo 114 ColumnAsName model.CIStr 115 Table *model.TableInfo 116 TableAsName model.CIStr 117 DBName model.CIStr 118 119 // The expression for the result field. If it is generated from a select field, it would 120 // be the expression of that select field, otherwise the type would be ValueExpr and value 121 // will be set for every retrieved row. 122 Expr ExprNode 123 TableName *TableName 124 // Whether this result field has been referenced. 125 // If not, we don't need to get the values. 126 Referenced bool 127 } 128 129 // Row represents a single row from Recordset. 130 type Row struct { 131 Data []types.Datum 132 } 133 134 // RecordSet is an abstract result set interface to help get data from Plan. 135 type RecordSet interface { 136 137 // Fields gets result fields. 138 Fields() (fields []*ResultField, err error) 139 140 // Next returns the next row, nil row means there is no more to return. 141 Next() (row *Row, err error) 142 143 // Close closes the underlying iterator, call Next after Close will 144 // restart the iteration. 145 Close() error 146 } 147 148 // ResultSetNode interface has ResultFields property which is computed and set by 149 // optimizer.InfoBinder during binding process. Implementations include SelectStmt, 150 // SubqueryExpr, TableSource, TableName and Join. 151 type ResultSetNode interface { 152 Node 153 // GetResultFields gets result fields of the result set node. 154 GetResultFields() []*ResultField 155 // SetResultFields sets result fields of the result set node. 156 SetResultFields(fields []*ResultField) 157 } 158 159 // Statement is an interface for SQL execution. 160 // NOTE: all Statement implementations must be safe for 161 // concurrent using by multiple goroutines. 162 // If the Exec method requires any Execution domain local data, 163 // they must be held out of the implementing instance. 164 type Statement interface { 165 // Explain gets the execution plans. 166 //Explain(ctx context.Context, w format.Formatter) 167 168 // IsDDL shows whether the statement is an DDL operation. 169 IsDDL() bool 170 171 // OriginText gets the origin SQL text. 172 OriginText() string 173 174 // SetText sets the executive SQL text. 175 SetText(text string) 176 177 // Exec executes SQL and gets a Recordset. 178 Exec(ctx context.Context) (RecordSet, error) 179 } 180 181 // Visitor visits a Node. 182 type Visitor interface { 183 // Enter is called before children nodes are visited. 184 // The returned node must be the same type as the input node n. 185 // skipChildren returns true means children nodes should be skipped, 186 // this is useful when work is done in Enter and there is no need to visit children. 187 Enter(n Node) (node Node, skipChildren bool) 188 // Leave is called after children nodes have been visited. 189 // The returned node's type can be different from the input node if it is a ExprNode, 190 // Non-expression node must be the same type as the input node n. 191 // ok returns false to stop visiting. 192 Leave(n Node) (node Node, ok bool) 193 }