forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.go
153 lines (139 loc) · 3.82 KB
/
analyzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//Modified by Wenbin Xiao 2015.04.18
package sqlparser
// analyzer.go contains utility analysis functions.
import (
"errors"
"fmt"
"github.com/xwb1989/sqlparser/dependency/sqltypes"
)
// GetTableName returns the table name from the SimpleTableExpr
// only if it's a simple expression. Otherwise, it returns "".
func GetTableName(node SimpleTableExpr) string {
if n, ok := node.(*TableName); ok && n.Qualifier == nil {
return string(n.Name)
}
// sub-select or '.' expression
return ""
}
// Get the primary key ColumnDefinition of the table, sqlNode must be a CreateTable struct
func GetPrimaryKey(sqlNode SQLNode) (*ColumnDefinition, error) {
node, ok := sqlNode.(*CreateTable)
if !ok {
return nil, errors.New("fail to convert interface SQLNode to struct CreateTable")
}
for _, col := range node.ColumnDefinitions {
for _, att := range col.ColumnAtts {
if att == AST_PRIMARY_KEY {
return col, nil
}
}
}
return nil, errors.New("unable to find primary key")
}
//Get ColumnDefinition by name, sqlNode must be a CreateTable struct
func GetColumnByName(sqlNode SQLNode, name string) (*ColumnDefinition, error) {
node, ok := sqlNode.(*CreateTable)
if !ok {
return nil, errors.New("fail to convert interface SQLNode to struct CreateTable")
}
for _, col := range node.ColumnDefinitions {
if col.ColName == name {
return col, nil
}
}
return nil, errors.New("unable to find the column")
}
// GetColName returns the column name, only if
// it's a simple expression. Otherwise, it returns "".
func GetColName(node Expr) string {
if n, ok := node.(*ColName); ok {
return string(n.Name)
}
return ""
}
// IsColName returns true if the ValExpr is a *ColName.
func IsColName(node ValExpr) bool {
_, ok := node.(*ColName)
return ok
}
// IsValue returns true if the ValExpr is a string, number or value arg.
// NULL is not considered to be a value.
func IsValue(node ValExpr) bool {
switch node.(type) {
case StrVal, NumVal, ValArg:
return true
}
return false
}
// HasINCaluse returns true if any of the conditions has an IN clause.
func HasINClause(conditions []BoolExpr) bool {
for _, node := range conditions {
if c, ok := node.(*ComparisonExpr); ok && c.Operator == AST_IN {
return true
}
}
return false
}
// IsSimpleTuple returns true if the ValExpr is a ValTuple that
// contains simple values or if it's a list arg.
func IsSimpleTuple(node ValExpr) bool {
switch vals := node.(type) {
case ValTuple:
for _, n := range vals {
if !IsValue(n) {
return false
}
}
return true
case ListArg:
return true
}
// It's a subquery
return false
}
// 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 AsInterface(node ValExpr) (interface{}, error) {
switch node := node.(type) {
case ValTuple:
vals := make([]interface{}, 0, len(node))
for _, val := range node {
v, err := AsInterface(val)
if err != nil {
return nil, err
}
vals = append(vals, v)
}
return vals, nil
case ValArg:
return string(node), nil
case ListArg:
return string(node), nil
case StrVal:
return sqltypes.MakeString(node), nil
case NumVal:
n, err := sqltypes.BuildNumeric(string(node))
if err != nil {
return nil, fmt.Errorf("type mismatch: %s", err)
}
return n, nil
case *NullVal:
return nil, nil
}
return nil, fmt.Errorf("unexpected node %v", node)
}
// StringIn is a convenience function that returns
// true if str matches any of the values.
func StringIn(str string, values ...string) bool {
for _, val := range values {
if str == val {
return true
}
}
return false
}