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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
# 2018-01-08
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
#
# Tests for the sqlite3_normalize() extension function.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
set testprefix normalize
foreach {tnum sql norm} {
100
{SELECT * FROM t1 WHERE a IN (1) AND b=51.42}
{select*from t1 where a in(?,?,?)and b=?;}
110
{SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);}
{select a,b+?,c from t1 where d not in(select x from t2);}
120
{ SELECT NULL, b FROM t1 -- comment text
WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
SELECT a FROM t)
OR e='hello';
}
{select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;}
121
{/*Initial comment*/
-- another comment line
SELECT NULL /* comment */ , b FROM t1 -- comment text
WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
SELECT a FROM t)
OR e='hello';
}
{select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;}
130
{/* Query containing parameters */
SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */}
{select x,?,y,?,z,?,w from t1;}
140
{/* Long list on the RHS of IN */
SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);}
{select?in(?,?,?);}
150
{SELECT x'abc'; -- illegal token}
{}
160
{SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5}
{select a,?,b from t1 where c is not null or d is null or e=?;}
170
{/* IN list exactly 5 bytes long */
SELECT * FROM t1 WHERE x IN (1,2,3);}
{select*from t1 where x in(?,?,?);}
180
{ }
{}
} {
do_test $tnum [list sqlite3_normalize $sql] $norm
}
ifcapable normalize {
do_test 200 {
execsql {
CREATE TABLE t1(a,b);
}
} {}
do_test 201 {
set STMT [sqlite3_prepare_v3 $DB \
"SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 0 TAIL]
sqlite3_bind_null $STMT 1
} {}
do_test 202 {
sqlite3_normalized_sql $STMT
} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
do_test 203 {
sqlite3_finalize $STMT
} {SQLITE_OK}
do_test 210 {
set STMT [sqlite3_prepare_v3 $DB \
"SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 2 TAIL]
sqlite3_bind_null $STMT 1
} {}
do_test 211 {
sqlite3_normalized_sql $STMT
} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
do_test 212 {
sqlite3_finalize $STMT
} {SQLITE_OK}
do_test 220 {
set STMT [sqlite3_prepare_v3 $DB \
"SELECT a, b FROM t1 WHERE b = 'a' ORDER BY a;" -1 2 TAIL]
} {/^[0-9A-Fa-f]+$/}
do_test 221 {
sqlite3_normalized_sql $STMT
} {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
do_test 222 {
sqlite3_finalize $STMT
} {SQLITE_OK}
do_test 297 {
execsql {
DROP TABLE t1;
}
} {}
do_test 298 {
execsql {
CREATE TABLE t1(a,b,c,d,e,"col f",w,x,y,z);
CREATE TABLE t2(x,"col y");
}
} {}
do_test 299 {
sqlite3_create_function db
} {SQLITE_OK}
foreach {tnum sql flags norm} {
300
{SELECT * FROM t1 WHERE a IN (1) AND b=51.42}
0x2
{0 {SELECT*FROM t1 WHERE a IN(?,?,?)AND b=?;}}
310
{SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);}
0x2
{0 {SELECT a,b+?,c FROM t1 WHERE d NOT IN(SELECT x FROM t2);}}
320
{ SELECT NULL, b FROM t1 -- comment text
WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
SELECT a FROM t)
OR e='hello';
}
0x2
{0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
321
{/*Initial comment*/
-- another comment line
SELECT NULL /* comment */ , b FROM t1 -- comment text
WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
SELECT a FROM t)
OR e='hello';
}
0x2
{0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
330
{/* Query containing parameters */
SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */}
0x2
{0 {SELECT x,?,y,?,z,?,w FROM t1;}}
340
{/* Long list on the RHS of IN */
SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);}
0x2
{1 {(1) no such column: x}}
350
{SELECT x'abc'; -- illegal token}
0x2
{1 {(1) unrecognized token: "x'abc'"}}
360
{SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5}
0x2
{0 {SELECT a,?,b FROM t1 WHERE c IS NOT NULL OR d IS NULL OR e=?;}}
370
{/* IN list exactly 5 bytes long */
SELECT * FROM t1 WHERE x IN (1,2,3);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
400
{SELECT a FROM t1 WHERE x IN (1,2,3) AND sqlite_version();}
0x2
{0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND sqlite_version();}}
410
{SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8();}
0x2
{1 {(1) wrong number of arguments to function hex8()}}
420
{SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8('abc');}
0x2
{0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND hex8(?);}}
430
{SELECT "a" FROM t1 WHERE "x" IN ("1","2",'3');}
0x2
{0 {SELECT a FROM t1 WHERE x IN(?,?,?);}}
440
{SELECT 'a' FROM t1 WHERE 'x';}
0x2
{0 {SELECT?FROM t1 WHERE?;}}
450
{SELECT [a] FROM t1 WHERE [x];}
0x2
{0 {SELECT a FROM t1 WHERE x;}}
460
{SELECT * FROM t1 WHERE x IN (x);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(x);}}
470
{SELECT * FROM t1 WHERE x IN (x,a);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(x,a);}}
480
{SELECT * FROM t1 WHERE x IN ([x],"a");}
0x2
{0 {SELECT*FROM t1 WHERE x IN(x,a);}}
500
{SELECT * FROM t1 WHERE x IN ([x],"a",'b',sqlite_version());}
0x2
{0 {SELECT*FROM t1 WHERE x IN(x,a,?,sqlite_version());}}
520
{SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
540
{SELECT * FROM t1 WHERE x IN ((SELECT x FROM t1));}
0x2
{0 {SELECT*FROM t1 WHERE x IN((SELECT x FROM t1));}}
550
{SELECT a, a+1, a||'b', a+"b" FROM t1;}
0x2
{0 {SELECT a,a+?,a||?,a+b FROM t1;}}
570
{SELECT * FROM t1 WHERE x IN (1);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
580
{SELECT * FROM t1 WHERE x IN (1,2);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
590
{SELECT * FROM t1 WHERE x IN (1,2,3);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
600
{SELECT * FROM t1 WHERE x IN (1,2,3,4);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
610
{SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
0x2
{0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
620
{SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (1,2,3));}
0x2
{0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?));}}
630
{SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (x));}
0x2
{0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x));}}
640
{SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
SELECT x FROM t1 WHERE x IN (x)))));}
0x2
{0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x)))));}}
650
{SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
SELECT x FROM t1 WHERE x IN (1)))));}
0x2
{0 {SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?)))));}}
660
{SELECT x FROM t1 WHERE x IN (1) UNION ALL SELECT x FROM t1 WHERE x IN (1);}
0x2
{0 {SELECT x FROM t1 WHERE x IN(?,?,?)UNION ALL SELECT x FROM t1 WHERE x IN(?,?,?);}}
670
{SELECT "col f", [col f] FROM t1;}
0x2
{0 {SELECT"col f","col f"FROM t1;}}
680
{SELECT a, "col f" FROM t1 LEFT OUTER JOIN t2 ON [t1].[col f] == [t2].[col y];}
0x2
{0 {SELECT a,"col f"FROM t1 LEFT OUTER JOIN t2 ON t1."col f"==t2."col y";}}
690
{SELECT * FROM ( WITH x AS ( SELECT * FROM t1 WHERE x IN ( 1)) SELECT 10);}
0x2
{0 {SELECT*FROM(WITH x AS(SELECT*FROM t1 WHERE x IN(?,?,?))SELECT?);}}
700
{SELECT rowid, oid, _rowid_ FROM t1;}
0x2
{0 {SELECT rowid,oid,_rowid_ FROM t1;}}
710
{SELECT x FROM t1 WHERE x IS NULL;}
0x2
{0 {SELECT x FROM t1 WHERE x IS NULL;}}
740
{SELECT x FROM t1 WHERE x IS NOT NULL;}
0x2
{0 {SELECT x FROM t1 WHERE x IS NOT NULL;}}
750
{SELECT x FROM t1 WHERE x = NULL;}
0x2
{0 {SELECT x FROM t1 WHERE x=?;}}
760
{SELECT x FROM t1 WHERE x IN ([x] IS NOT NULL, NULL, 1, 'a', "b", x'00');}
0x2
{0 {SELECT x FROM t1 WHERE x IN(x IS NOT NULL,?,?,?,b,?);}}
800
{ATTACH "normalize800.db" AS somefile;}
0x2
{0 {ATTACH"normalize800.db"AS somefile;}}
810
{ATTACH DATABASE "normalize810.db" AS somefile;}
0x2
{0 {ATTACH DATABASE"normalize810.db"AS somefile;}}
900
{INSERT INTO t1 (x) VALUES("sl1"), (1), ("sl2"), ('i');}
0x2
{0 {INSERT INTO t1(x)VALUES(?),(?),(?),(?);}}
910
{UPDATE t1 SET x = "sl1" WHERE x IN (1, "sl2", 'i');}
0x2
{0 {UPDATE t1 SET x=?WHERE x IN(?,?,?);}}
920
{UPDATE t1 SET x = "y" WHERE x IN (1, "sl1", 'i');}
0x2
{0 {UPDATE t1 SET x=y WHERE x IN(?,?,?);}}
930
{DELETE FROM t1 WHERE x IN (1, "sl1", 'i');}
0x2
{0 {DELETE FROM t1 WHERE x IN(?,?,?);}}
} {
do_test $tnum {
set code [catch {
set STMT [sqlite3_prepare_v3 $DB $sql -1 $flags TAIL]
sqlite3_normalized_sql $STMT
} res]
if {[info exists STMT]} {
sqlite3_finalize $STMT; unset STMT
}
list $code $res
} $norm
}
}
finish_test
|