Debian Codegraph @

normalizer_test.go

raw symbols SCIP
1 /*
2 Copyright 2017 Google Inc.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package sqlparser
18
19 import (
20 "fmt"
21 "reflect"
22 "testing"
23
24 "github.com/xwb1989/sqlparser/dependency/querypb"
25 "github.com/xwb1989/sqlparser/dependency/sqltypes"
26 )
27
28 func TestNormalize(t *testing.T) {
29 prefix := "bv"
30 testcases := []struct {
31 in string
32 outstmt string
33 outbv map[string]*querypb.BindVariable
34 }{{
35 // str val
36 in: "select * from t where v1 = 'aa'",
37 outstmt: "select * from t where v1 = :bv1",
38 outbv: map[string]*querypb.BindVariable{
39 "bv1": sqltypes.BytesBindVariable([]byte("aa")),
40 },
41 }, {
42 // int val
43 in: "select * from t where v1 = 1",
44 outstmt: "select * from t where v1 = :bv1",
45 outbv: map[string]*querypb.BindVariable{
46 "bv1": sqltypes.Int64BindVariable(1),
47 },
48 }, {
49 // float val
50 in: "select * from t where v1 = 1.2",
51 outstmt: "select * from t where v1 = :bv1",
52 outbv: map[string]*querypb.BindVariable{
53 "bv1": sqltypes.Float64BindVariable(1.2),
54 },
55 }, {
56 // multiple vals
57 in: "select * from t where v1 = 1.2 and v2 = 2",
58 outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
59 outbv: map[string]*querypb.BindVariable{
60 "bv1": sqltypes.Float64BindVariable(1.2),
61 "bv2": sqltypes.Int64BindVariable(2),
62 },
63 }, {
64 // bv collision
65 in: "select * from t where v1 = :bv1 and v2 = 1",
66 outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
67 outbv: map[string]*querypb.BindVariable{
68 "bv2": sqltypes.Int64BindVariable(1),
69 },
70 }, {
71 // val reuse
72 in: "select * from t where v1 = 1 and v2 = 1",
73 outstmt: "select * from t where v1 = :bv1 and v2 = :bv1",
74 outbv: map[string]*querypb.BindVariable{
75 "bv1": sqltypes.Int64BindVariable(1),
76 },
77 }, {
78 // ints and strings are different
79 in: "select * from t where v1 = 1 and v2 = '1'",
80 outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
81 outbv: map[string]*querypb.BindVariable{
82 "bv1": sqltypes.Int64BindVariable(1),
83 "bv2": sqltypes.BytesBindVariable([]byte("1")),
84 },
85 }, {
86 // val should not be reused for non-select statements
87 in: "insert into a values(1, 1)",
88 outstmt: "insert into a values (:bv1, :bv2)",
89 outbv: map[string]*querypb.BindVariable{
90 "bv1": sqltypes.Int64BindVariable(1),
91 "bv2": sqltypes.Int64BindVariable(1),
92 },
93 }, {
94 // val should be reused only in subqueries of DMLs
95 in: "update a set v1=(select 5 from t), v2=5, v3=(select 5 from t), v4=5",
96 outstmt: "update a set v1 = (select :bv1 from t), v2 = :bv2, v3 = (select :bv1 from t), v4 = :bv3",
97 outbv: map[string]*querypb.BindVariable{
98 "bv1": sqltypes.Int64BindVariable(5),
99 "bv2": sqltypes.Int64BindVariable(5),
100 "bv3": sqltypes.Int64BindVariable(5),
101 },
102 }, {
103 // list vars should work for DMLs also
104 in: "update a set v1=5 where v2 in (1, 4, 5)",
105 outstmt: "update a set v1 = :bv1 where v2 in ::bv2",
106 outbv: map[string]*querypb.BindVariable{
107 "bv1": sqltypes.Int64BindVariable(5),
108 "bv2": sqltypes.TestBindVariable([]interface{}{1, 4, 5}),
109 },
110 }, {
111 // Hex value does not convert
112 in: "select * from t where v1 = 0x1234",
113 outstmt: "select * from t where v1 = 0x1234",
114 outbv: map[string]*querypb.BindVariable{},
115 }, {
116 // Hex value does not convert for DMLs
117 in: "update a set v1 = 0x1234",
118 outstmt: "update a set v1 = 0x1234",
119 outbv: map[string]*querypb.BindVariable{},
120 }, {
121 // Values up to len 256 will reuse.
122 in: fmt.Sprintf("select * from t where v1 = '%256s' and v2 = '%256s'", "a", "a"),
123 outstmt: "select * from t where v1 = :bv1 and v2 = :bv1",
124 outbv: map[string]*querypb.BindVariable{
125 "bv1": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%256s", "a"))),
126 },
127 }, {
128 // Values greater than len 256 will not reuse.
129 in: fmt.Sprintf("select * from t where v1 = '%257s' and v2 = '%257s'", "b", "b"),
130 outstmt: "select * from t where v1 = :bv1 and v2 = :bv2",
131 outbv: map[string]*querypb.BindVariable{
132 "bv1": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%257s", "b"))),
133 "bv2": sqltypes.BytesBindVariable([]byte(fmt.Sprintf("%257s", "b"))),
134 },
135 }, {
136 // bad int
137 in: "select * from t where v1 = 12345678901234567890",
138 outstmt: "select * from t where v1 = 12345678901234567890",
139 outbv: map[string]*querypb.BindVariable{},
140 }, {
141 // comparison with no vals
142 in: "select * from t where v1 = v2",
143 outstmt: "select * from t where v1 = v2",
144 outbv: map[string]*querypb.BindVariable{},
145 }, {
146 // IN clause with existing bv
147 in: "select * from t where v1 in ::list",
148 outstmt: "select * from t where v1 in ::list",
149 outbv: map[string]*querypb.BindVariable{},
150 }, {
151 // IN clause with non-val values
152 in: "select * from t where v1 in (1, a)",
153 outstmt: "select * from t where v1 in (:bv1, a)",
154 outbv: map[string]*querypb.BindVariable{
155 "bv1": sqltypes.Int64BindVariable(1),
156 },
157 }, {
158 // IN clause with vals
159 in: "select * from t where v1 in (1, '2')",
160 outstmt: "select * from t where v1 in ::bv1",
161 outbv: map[string]*querypb.BindVariable{
162 "bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}),
163 },
164 }, {
165 // NOT IN clause
166 in: "select * from t where v1 not in (1, '2')",
167 outstmt: "select * from t where v1 not in ::bv1",
168 outbv: map[string]*querypb.BindVariable{
169 "bv1": sqltypes.TestBindVariable([]interface{}{1, []byte("2")}),
170 },
171 }}
172 for _, tc := range testcases {
173 stmt, err := Parse(tc.in)
174 if err != nil {
175 t.Error(err)
176 continue
177 }
178 bv := make(map[string]*querypb.BindVariable)
179 Normalize(stmt, bv, prefix)
180 outstmt := String(stmt)
181 if outstmt != tc.outstmt {
182 t.Errorf("Query:\n%s:\n%s, want\n%s", tc.in, outstmt, tc.outstmt)
183 }
184 if !reflect.DeepEqual(tc.outbv, bv) {
185 t.Errorf("Query:\n%s:\n%v, want\n%v", tc.in, bv, tc.outbv)
186 }
187 }
188 }
189
190 func TestGetBindVars(t *testing.T) {
191 stmt, err := Parse("select * from t where :v1 = :v2 and :v2 = :v3 and :v4 in ::v5")
192 if err != nil {
193 t.Fatal(err)
194 }
195 got := GetBindvars(stmt)
196 want := map[string]struct{}{
197 "v1": {},
198 "v2": {},
199 "v3": {},
200 "v4": {},
201 "v5": {},
202 }
203 if !reflect.DeepEqual(got, want) {
204 t.Errorf("GetBindVars: %v, want: %v", got, want)
205 }
206 }