forked from xelabs/go-mysqlstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizer_test.go
More file actions
186 lines (181 loc) · 4.55 KB
/
normalizer_test.go
File metadata and controls
186 lines (181 loc) · 4.55 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2016, 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.
package sqlparser
import (
"reflect"
"testing"
querypb "github.com/xelabs/go-mysqlstack/sqlparser/depends/query"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
)
func TestNormalize(t *testing.T) {
prefix := "bv"
testcases := []struct {
in string
outstmt string
outbv map[string]interface{}
}{{
// str val
in: "select * from t where v1 = 'aa'",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.VarBinary,
Value: []byte("aa"),
},
},
}, {
// int val
in: "select * from t where v1 = 1",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("1"),
},
},
}, {
// float val
in: "select * from t where v1 = 1.2",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Float64,
Value: []byte("1.2"),
},
},
}, {
// multiple vals
in: "select * from t where v1 = 1.2 and v2 = 2",
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Float64,
Value: []byte("1.2"),
},
"bv2": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("2"),
},
},
}, {
// bv collision
in: "select * from t where v1 = :bv1 and v2 = 1",
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
outbv: map[string]interface{}{
"bv2": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("1"),
},
},
}, {
// val reuse
in: "select * from t where v1 = 1 and v2 = 1",
outstmt: "select * from t where v1 = :bv1 and v2 = :bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("1"),
},
},
}, {
// ints and strings are different
in: "select * from t where v1 = 1 and v2 = '1'",
outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("1"),
},
"bv2": &querypb.BindVariable{
Type: sqltypes.VarBinary,
Value: []byte("1"),
},
},
}, {
// comparison with no vals
in: "select * from t where v1 = v2",
outstmt: "select * from t where v1 = v2",
outbv: map[string]interface{}{},
}, {
// IN clause with existing bv
in: "select * from t where v1 in ::list",
outstmt: "select * from t where v1 in ::list",
outbv: map[string]interface{}{},
}, {
// IN clause with non-val values
in: "select * from t where v1 in (1, a)",
outstmt: "select * from t where v1 in (:bv1, a)",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Int64,
Value: []byte("1"),
},
},
}, {
// IN clause with vals
in: "select * from t where v1 in (1, '2')",
outstmt: "select * from t where v1 in ::bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Tuple,
Values: []*querypb.Value{{
Type: sqltypes.Int64,
Value: []byte("1"),
}, {
Type: sqltypes.VarBinary,
Value: []byte("2"),
}},
},
},
}, {
// NOT IN clause
in: "select * from t where v1 not in (1, '2')",
outstmt: "select * from t where v1 not in ::bv1",
outbv: map[string]interface{}{
"bv1": &querypb.BindVariable{
Type: sqltypes.Tuple,
Values: []*querypb.Value{{
Type: sqltypes.Int64,
Value: []byte("1"),
}, {
Type: sqltypes.VarBinary,
Value: []byte("2"),
}},
},
},
}}
for _, tc := range testcases {
stmt, err := Parse(tc.in)
if err != nil {
t.Error(err)
continue
}
bv := make(map[string]interface{})
Normalize(stmt, bv, prefix)
outstmt := String(stmt)
if outstmt != tc.outstmt {
t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt)
}
if !reflect.DeepEqual(tc.outbv, bv) {
t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv)
}
}
}
func TestGetBindVars(t *testing.T) {
stmt, err := Parse("select * from t where :v1 = :v2 and :v2 = :v3 and :v4 in ::v5")
if err != nil {
t.Fatal(err)
}
got := GetBindvars(stmt)
want := map[string]struct{}{
"v1": {},
"v2": {},
"v3": {},
"v4": {},
"v5": {},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("GetBindVars: %v, want %v", got, want)
}
}