forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalizer_test.go
More file actions
155 lines (147 loc) · 4.47 KB
/
normalizer_test.go
File metadata and controls
155 lines (147 loc) · 4.47 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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sqlparser
import (
"reflect"
"testing"
"github.com/xwb1989/sqlparser/dependency/querypb"
"github.com/xwb1989/sqlparser/dependency/sqltypes"
)
func TestNormalize(t *testing.T) {
prefix := "bv"
testcases := []struct {
in string
outstmt string
outbv map[string]*querypb.BindVariable
}{{
// str val
in: "select * from t where v1 = 'aa'",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.BytesBindVariable([]byte("aa")),
},
}, {
// int val
in: "select * from t where v1 = 1",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.Int64BindVariable(1),
},
}, {
// float val
in: "select * from t where v1 = 1.2",
outstmt: "select * from t where v1 = :bv1",
outbv: map[string]*querypb.BindVariable{
"bv1": sqltypes.Float64BindVariable(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]*querypb.BindVariable{
"bv1": sqltypes.Float64BindVariable(1.2),
"bv2": sqltypes.Int64BindVariable(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]*querypb.BindVariable{
"bv2": sqltypes.Int64BindVariable(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]*querypb.BindVariable{
"bv1": sqltypes.Int64BindVariable(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]*querypb.BindVariable{
"bv1": sqltypes.Int64BindVariable(1),
"bv2": sqltypes.BytesBindVariable([]byte("1")),
},
}, {
// bad int
in: "select * from t where v1 = 12345678901234567890",
outstmt: "select * from t where v1 = 12345678901234567890",
outbv: map[string]*querypb.BindVariable{},
}, {
// comparison with no vals
in: "select * from t where v1 = v2",
outstmt: "select * from t where v1 = v2",
outbv: map[string]*querypb.BindVariable{},
}, {
// IN clause with existing bv
in: "select * from t where v1 in ::list",
outstmt: "select * from t where v1 in ::list",
outbv: map[string]*querypb.BindVariable{},
}, {
// 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]*querypb.BindVariable{
"bv1": sqltypes.Int64BindVariable(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]*querypb.BindVariable{
"bv1": sqltypes.TestBindVariable([]interface{}{1, []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]*querypb.BindVariable{
"bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}),
},
}}
for _, tc := range testcases {
stmt, err := Parse(tc.in)
if err != nil {
t.Error(err)
continue
}
bv := make(map[string]*querypb.BindVariable)
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, wnat %v", got, want)
}
}