-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
magic.go
129 lines (122 loc) · 3.16 KB
/
magic.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
package mogi
import (
"fmt"
"log"
"strconv"
"strings"
// "github.com/davecgh/go-spew/spew"
"github.com/guregu/mogi/internal/sqlparser"
)
// transmogrify takes sqlparser expressions and turns them into useful go values
func transmogrify(v interface{}) interface{} {
switch x := v.(type) {
case *sqlparser.ColName:
name := string(x.Name)
if x.Qualifier != "" {
name = fmt.Sprintf("%s.%s", x.Qualifier, name)
}
return name
case *sqlparser.NonStarExpr:
if x.As != "" {
return string(x.As)
}
return transmogrify(x.Expr)
case *sqlparser.StarExpr:
return "*"
case *sqlparser.FuncExpr:
name := strings.ToUpper(string(x.Name))
var args []string
for _, expr := range x.Exprs {
args = append(args, stringify(transmogrify(expr)))
}
return fmt.Sprintf("%s(%s)", name, strings.Join(args, ", "))
case *sqlparser.BinaryExpr:
// TODO: figure out some way to make this work
return transmogrify(x.Left)
case sqlparser.ValArg:
// vitess makes args like :v1
str := string(x)
hdr, num := str[:2], str[2:]
if hdr != ":v" {
log.Panicln("unexpected arg format", str)
}
idx, err := strconv.Atoi(num)
if err != nil {
panic(err)
}
return arg(idx - 1)
case sqlparser.StrVal:
return string(x)
case sqlparser.NumVal:
s := string(x)
n, err := strconv.ParseInt(s, 10, 64)
if err == nil {
return n
}
f, err := strconv.ParseFloat(s, 64)
if err == nil {
return f
}
case sqlparser.ValTuple:
vals := make([]interface{}, 0, len(x))
for _, item := range x {
vals = append(vals, transmogrify(item))
}
return vals
default:
log.Printf("unknown transmogrify: (%T) %v\n", v, v)
//panic(x)
}
return nil
}
func extractColumnName(nse *sqlparser.NonStarExpr) string {
if nse.As != "" {
return string(nse.As)
}
return stringify(transmogrify(nse.Expr))
}
func extractTableNames(tables *[]string, from sqlparser.TableExpr) {
switch x := from.(type) {
case *sqlparser.AliasedTableExpr:
if name, ok := x.Expr.(*sqlparser.TableName); ok {
*tables = append(*tables, string(name.Name))
}
case *sqlparser.JoinTableExpr:
extractTableNames(tables, x.LeftExpr)
extractTableNames(tables, x.RightExpr)
}
}
func extractBoolExpr(vals map[string]interface{}, expr sqlparser.BoolExpr) map[string]interface{} {
if vals == nil {
vals = make(map[string]interface{})
}
switch x := expr.(type) {
case *sqlparser.OrExpr:
extractBoolExpr(vals, x.Left)
extractBoolExpr(vals, x.Right)
case *sqlparser.AndExpr:
extractBoolExpr(vals, x.Left)
extractBoolExpr(vals, x.Right)
case *sqlparser.ComparisonExpr:
column := transmogrify(x.Left).(string)
vals[column] = transmogrify(x.Right)
}
return vals
}
func extractBoolExprWithOps(vals map[colop]interface{}, expr sqlparser.BoolExpr) map[colop]interface{} {
if vals == nil {
vals = make(map[colop]interface{})
}
switch x := expr.(type) {
case *sqlparser.OrExpr:
extractBoolExprWithOps(vals, x.Left)
extractBoolExprWithOps(vals, x.Right)
case *sqlparser.AndExpr:
extractBoolExprWithOps(vals, x.Left)
extractBoolExprWithOps(vals, x.Right)
case *sqlparser.ComparisonExpr:
column := transmogrify(x.Left).(string)
vals[colop{column, x.Operator}] = transmogrify(x.Right)
}
return vals
}