Skip to content

Commit

Permalink
Merge pull request pingcap#818 from pingcap/qiuyesuifeng/tiny-clean-up
Browse files Browse the repository at this point in the history
Tiny clean up ast package.
  • Loading branch information
qiuyesuifeng committed Dec 31, 2015
2 parents c064caf + 6ba33a0 commit 832ca7e
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 148 deletions.
25 changes: 12 additions & 13 deletions ast/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,22 @@ import (
)

var (
_ DDLNode = &AlterTableStmt{}
_ DDLNode = &CreateDatabaseStmt{}
_ DDLNode = &DropDatabaseStmt{}
_ DDLNode = &CreateTableStmt{}
_ DDLNode = &DropTableStmt{}
_ DDLNode = &CreateIndexStmt{}
_ DDLNode = &CreateTableStmt{}
_ DDLNode = &DropDatabaseStmt{}
_ DDLNode = &DropIndexStmt{}
_ DDLNode = &DropTableStmt{}
_ DDLNode = &AlterTableStmt{}
_ DDLNode = &TruncateTableStmt{}
_ Node = &IndexColName{}
_ Node = &ReferenceDef{}
_ Node = &ColumnOption{}
_ Node = &Constraint{}
_ Node = &ColumnDef{}
_ Node = &ColumnPosition{}
_ Node = &AlterTableSpec{}

_ Node = &AlterTableSpec{}
_ Node = &ColumnDef{}
_ Node = &ColumnOption{}
_ Node = &ColumnPosition{}
_ Node = &Constraint{}
_ Node = &IndexColName{}
_ Node = &ReferenceDef{}
)

// CharsetOpt is used for parsing charset option from SQL.
Expand All @@ -53,8 +54,6 @@ const (

// DatabaseOption represents database option.
type DatabaseOption struct {
node

Tp DatabaseOptionType
Value string
}
Expand Down
156 changes: 124 additions & 32 deletions ast/dml.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,28 @@ import (
)

var (
_ DMLNode = &InsertStmt{}
_ DMLNode = &DeleteStmt{}
_ DMLNode = &InsertStmt{}
_ DMLNode = &UnionStmt{}
_ DMLNode = &UpdateStmt{}
_ DMLNode = &SelectStmt{}
_ DMLNode = &UnionStmt{}
_ Node = &Join{}
_ Node = &TableName{}
_ Node = &TableSource{}
_ Node = &Assignment{}
_ Node = &Limit{}
_ Node = &WildCardField{}
_ Node = &SelectField{}
_ DMLNode = &ShowStmt{}

_ Node = &Assignment{}
_ Node = &ByItem{}
_ Node = &FieldList{}
_ Node = &GroupByClause{}
_ Node = &HavingClause{}
_ Node = &Join{}
_ Node = &Limit{}
_ Node = &OnCondition{}
_ Node = &OrderByClause{}
_ Node = &SelectField{}
_ Node = &TableName{}
_ Node = &TableRefsClause{}
_ Node = &TableSource{}
_ Node = &UnionClause{}
_ Node = &WildCardField{}
)

// JoinType is join type, including cross/left/right/full.
Expand Down Expand Up @@ -110,52 +120,52 @@ func (n *TableName) Accept(v Visitor) (Node, bool) {
return v.Leave(n)
}

// TableSource represents table source with a name.
type TableSource struct {
// OnCondition represetns JOIN on condition.
type OnCondition struct {
node

// Source is the source of the data, can be a TableName,
// a SelectStmt, a UnionStmt, or a JoinNode.
Source ResultSetNode

// AsName is the as name of the table source.
AsName model.CIStr
Expr ExprNode
}

// Accept implements Node Accept interface.
func (n *TableSource) Accept(v Visitor) (Node, bool) {
func (n *OnCondition) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*TableSource)
node, ok := n.Source.Accept(v)
n = newNode.(*OnCondition)
node, ok := n.Expr.Accept(v)
if !ok {
return n, false
}
n.Source = node.(ResultSetNode)
n.Expr = node.(ExprNode)
return v.Leave(n)
}

// OnCondition represetns JOIN on condition.
type OnCondition struct {
// TableSource represents table source with a name.
type TableSource struct {
node

Expr ExprNode
// Source is the source of the data, can be a TableName,
// a SelectStmt, a UnionStmt, or a JoinNode.
Source ResultSetNode

// AsName is the alias name of the table source.
AsName model.CIStr
}

// Accept implements Node Accept interface.
func (n *OnCondition) Accept(v Visitor) (Node, bool) {
func (n *TableSource) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*OnCondition)
node, ok := n.Expr.Accept(v)
n = newNode.(*TableSource)
node, ok := n.Source.Accept(v)
if !ok {
return n, false
}
n.Expr = node.(ExprNode)
n.Source = node.(ResultSetNode)
return v.Leave(n)
}

Expand Down Expand Up @@ -209,7 +219,7 @@ type SelectField struct {
WildCard *WildCardField
// If Expr is not nil, WildCard will be nil.
Expr ExprNode
// AsName name for Expr.
// Alias name for Expr.
AsName model.CIStr
}

Expand Down Expand Up @@ -399,8 +409,8 @@ func (n *SelectStmt) Accept(v Visitor) (Node, bool) {
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*SelectStmt)

n = newNode.(*SelectStmt)
if n.From != nil {
node, ok := n.From.Accept(v)
if !ok {
Expand Down Expand Up @@ -456,6 +466,7 @@ func (n *SelectStmt) Accept(v Visitor) (Node, bool) {
}
n.Limit = node.(*Limit)
}

return v.Leave(n)
}

Expand Down Expand Up @@ -584,15 +595,16 @@ func (n *InsertStmt) Accept(v Visitor) (Node, bool) {
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*InsertStmt)

n = newNode.(*InsertStmt)
if n.Select != nil {
node, ok := n.Select.Accept(v)
if !ok {
return n, false
}
n.Select = node.(ResultSetNode)
}

node, ok := n.Table.Accept(v)
if !ok {
return n, false
Expand Down Expand Up @@ -657,8 +669,8 @@ func (n *DeleteStmt) Accept(v Visitor) (Node, bool) {
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*DeleteStmt)

n = newNode.(*DeleteStmt)
node, ok := n.TableRefs.Accept(v)
if !ok {
return n, false
Expand Down Expand Up @@ -772,3 +784,83 @@ func (n *Limit) Accept(v Visitor) (Node, bool) {
n = newNode.(*Limit)
return v.Leave(n)
}

// ShowStmtType is the type for SHOW statement.
type ShowStmtType int

// Show statement types.
const (
ShowNone = iota
ShowEngines
ShowDatabases
ShowTables
ShowTableStatus
ShowColumns
ShowWarnings
ShowCharset
ShowVariables
ShowStatus
ShowCollation
ShowCreateTable
ShowGrants
ShowTriggers
ShowProcedureStatus
ShowIndex
)

// ShowStmt is a statement to provide information about databases, tables, columns and so on.
// See: https://dev.mysql.com/doc/refman/5.7/en/show.html
type ShowStmt struct {
dmlNode

Tp ShowStmtType // Databases/Tables/Columns/....
DBName string
Table *TableName // Used for showing columns.
Column *ColumnName // Used for `desc table column`.
Flag int // Some flag parsed from sql, such as FULL.
Full bool
User string // Used for show grants.

// Used by show variables
GlobalScope bool
Pattern *PatternLikeExpr
Where ExprNode
}

// Accept implements Node Accept interface.
func (n *ShowStmt) Accept(v Visitor) (Node, bool) {
newNode, skipChildren := v.Enter(n)
if skipChildren {
return v.Leave(newNode)
}
n = newNode.(*ShowStmt)
if n.Table != nil {
node, ok := n.Table.Accept(v)
if !ok {
return n, false
}
n.Table = node.(*TableName)
}
if n.Column != nil {
node, ok := n.Column.Accept(v)
if !ok {
return n, false
}
n.Column = node.(*ColumnName)
}
if n.Pattern != nil {
node, ok := n.Pattern.Accept(v)
if !ok {
return n, false
}
n.Pattern = node.(*PatternLikeExpr)
}
if n.Where != nil {
node, ok := n.Where.Accept(v)
if !ok {
return n, false
}
n.Where = node.(ExprNode)
}
return v.Leave(n)
}
17 changes: 9 additions & 8 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,30 @@ import (
)

var (
_ ExprNode = &ValueExpr{}
_ ExprNode = &BetweenExpr{}
_ ExprNode = &BinaryOperationExpr{}
_ Node = &WhenClause{}
_ ExprNode = &CaseExpr{}
_ ExprNode = &SubqueryExpr{}
_ ExprNode = &CompareSubqueryExpr{}
_ Node = &ColumnName{}
_ ExprNode = &ColumnNameExpr{}
_ ExprNode = &CompareSubqueryExpr{}
_ ExprNode = &DefaultExpr{}
_ ExprNode = &ExistsSubqueryExpr{}
_ ExprNode = &PatternInExpr{}
_ ExprNode = &IsNullExpr{}
_ ExprNode = &IsTruthExpr{}
_ ExprNode = &PatternLikeExpr{}
_ ExprNode = &ParamMarkerExpr{}
_ ExprNode = &ParenthesesExpr{}
_ ExprNode = &PositionExpr{}
_ ExprNode = &PatternInExpr{}
_ ExprNode = &PatternLikeExpr{}
_ ExprNode = &PatternRegexpExpr{}
_ ExprNode = &PositionExpr{}
_ ExprNode = &RowExpr{}
_ ExprNode = &SubqueryExpr{}
_ ExprNode = &UnaryOperationExpr{}
_ ExprNode = &ValueExpr{}
_ ExprNode = &ValuesExpr{}
_ ExprNode = &VariableExpr{}

_ Node = &ColumnName{}
_ Node = &WhenClause{}
)

// ValueExpr is the simple value expression.
Expand Down
11 changes: 6 additions & 5 deletions ast/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ import (
)

var (
_ FuncNode = &AggregateFuncExpr{}
_ FuncNode = &FuncCallExpr{}
_ FuncNode = &FuncExtractExpr{}
_ FuncNode = &FuncConvertExpr{}
_ FuncNode = &FuncCastExpr{}
_ FuncNode = &FuncSubstringExpr{}
_ FuncNode = &FuncConvertExpr{}
_ FuncNode = &FuncDateArithExpr{}
_ FuncNode = &FuncExtractExpr{}
_ FuncNode = &FuncLocateExpr{}
_ FuncNode = &FuncSubstringExpr{}
_ FuncNode = &FuncSubstringIndexExpr{}
_ FuncNode = &FuncTrimExpr{}
_ FuncNode = &FuncDateArithExpr{}
_ FuncNode = &AggregateFuncExpr{}
)

// UnquoteString is not quoted when printed.
Expand Down
Loading

0 comments on commit 832ca7e

Please sign in to comment.