3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
12 # Tests for the sqlite3_normalize() extension function.
15 set testdir [file dirname $argv0]
16 source $testdir/tester.tcl
17 set testprefix normalize
19 foreach {tnum sql norm} {
21 {SELECT * FROM t1 WHERE a IN (1) AND b=51.42}
22 {select*from t1 where a in(?,?,?)and b=?;}
25 {SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);}
26 {select a,b+?,c from t1 where d not in(select x from t2);}
29 { SELECT NULL, b FROM t1 -- comment text
30 WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
34 {select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;}
38 -- another comment line
39 SELECT NULL /* comment */ , b FROM t1 -- comment text
40 WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
44 {select?,b from t1 where d in(with t(a)as(values(?))select a from t)or e=?;}
47 {/* Query containing parameters */
48 SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */}
49 {select x,?,y,?,z,?,w from t1;}
52 {/* Long list on the RHS of IN */
53 SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);}
57 {SELECT x'abc'; -- illegal token}
61 {SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5}
62 {select a,?,b from t1 where c is not null or d is null or e=?;}
65 {/* IN list exactly 5 bytes long */
66 SELECT * FROM t1 WHERE x IN (1,2,3);}
67 {select*from t1 where x in(?,?,?);}
72 do_test $tnum [list sqlite3_normalize $sql] $norm
82 set STMT [sqlite3_prepare_v3 $DB \
83 "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 0 TAIL]
85 sqlite3_bind_null $STMT 1
88 sqlite3_normalized_sql $STMT
89 } {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
91 sqlite3_finalize $STMT
95 set STMT [sqlite3_prepare_v3 $DB \
96 "SELECT a, b FROM t1 WHERE b = ? ORDER BY a;" -1 2 TAIL]
98 sqlite3_bind_null $STMT 1
101 sqlite3_normalized_sql $STMT
102 } {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
104 sqlite3_finalize $STMT
108 set STMT [sqlite3_prepare_v3 $DB \
109 "SELECT a, b FROM t1 WHERE b = 'a' ORDER BY a;" -1 2 TAIL]
112 sqlite3_normalized_sql $STMT
113 } {SELECT a,b FROM t1 WHERE b=?ORDER BY a;}
115 sqlite3_finalize $STMT
125 CREATE TABLE t1(a,b,c,d,e,"col f",w,x,y,z);
126 CREATE TABLE t2(x,"col y");
130 sqlite3_create_function db
133 foreach {tnum sql flags norm} {
135 {SELECT * FROM t1 WHERE a IN (1) AND b=51.42}
137 {0 {SELECT*FROM t1 WHERE a IN(?,?,?)AND b=?;}}
140 {SELECT a, b+15, c FROM t1 WHERE d NOT IN (SELECT x FROM t2);}
142 {0 {SELECT a,b+?,c FROM t1 WHERE d NOT IN(SELECT x FROM t2);}}
145 { SELECT NULL, b FROM t1 -- comment text
146 WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
151 {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
155 -- another comment line
156 SELECT NULL /* comment */ , b FROM t1 -- comment text
157 WHERE d IN (WITH t(a) AS (VALUES(5)) /* CTE */
162 {0 {SELECT?,b FROM t1 WHERE d IN(WITH t(a)AS(VALUES(?))SELECT a FROM t)OR e=?;}}
165 {/* Query containing parameters */
166 SELECT x,$::abc(15),y,@abc,z,?99,w FROM t1 /* Trailing comment */}
168 {0 {SELECT x,?,y,?,z,?,w FROM t1;}}
171 {/* Long list on the RHS of IN */
172 SELECT 15 IN (1,2,3,(SELECT * FROM t1),'xyz',x'abcd',22*(x+5),null);}
174 {1 {(1) no such column: x}}
177 {SELECT x'abc'; -- illegal token}
179 {1 {(1) unrecognized token: "x'abc'"}}
182 {SELECT a,NULL,b FROM t1 WHERE c IS NOT NULL or D is null or e=5}
184 {0 {SELECT a,?,b FROM t1 WHERE c IS NOT NULL OR d IS NULL OR e=?;}}
187 {/* IN list exactly 5 bytes long */
188 SELECT * FROM t1 WHERE x IN (1,2,3);}
190 {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
193 {SELECT a FROM t1 WHERE x IN (1,2,3) AND sqlite_version();}
195 {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND sqlite_version();}}
198 {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8();}
200 {1 {(1) wrong number of arguments to function hex8()}}
203 {SELECT a FROM t1 WHERE x IN (1,2,3) AND hex8('abc');}
205 {0 {SELECT a FROM t1 WHERE x IN(?,?,?)AND hex8(?);}}
208 {SELECT "a" FROM t1 WHERE "x" IN ("1","2",'3');}
210 {0 {SELECT a FROM t1 WHERE x IN(?,?,?);}}
213 {SELECT 'a' FROM t1 WHERE 'x';}
215 {0 {SELECT?FROM t1 WHERE?;}}
218 {SELECT [a] FROM t1 WHERE [x];}
220 {0 {SELECT a FROM t1 WHERE x;}}
223 {SELECT * FROM t1 WHERE x IN (x);}
225 {0 {SELECT*FROM t1 WHERE x IN(x);}}
228 {SELECT * FROM t1 WHERE x IN (x,a);}
230 {0 {SELECT*FROM t1 WHERE x IN(x,a);}}
233 {SELECT * FROM t1 WHERE x IN ([x],"a");}
235 {0 {SELECT*FROM t1 WHERE x IN(x,a);}}
238 {SELECT * FROM t1 WHERE x IN ([x],"a",'b',sqlite_version());}
240 {0 {SELECT*FROM t1 WHERE x IN(x,a,?,sqlite_version());}}
243 {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
245 {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
248 {SELECT * FROM t1 WHERE x IN ((SELECT x FROM t1));}
250 {0 {SELECT*FROM t1 WHERE x IN((SELECT x FROM t1));}}
253 {SELECT a, a+1, a||'b', a+"b" FROM t1;}
255 {0 {SELECT a,a+?,a||?,a+b FROM t1;}}
258 {SELECT * FROM t1 WHERE x IN (1);}
260 {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
263 {SELECT * FROM t1 WHERE x IN (1,2);}
265 {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
268 {SELECT * FROM t1 WHERE x IN (1,2,3);}
270 {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
273 {SELECT * FROM t1 WHERE x IN (1,2,3,4);}
275 {0 {SELECT*FROM t1 WHERE x IN(?,?,?);}}
278 {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1);}
280 {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1);}}
283 {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (1,2,3));}
285 {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(?,?,?));}}
288 {SELECT * FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (x));}
290 {0 {SELECT*FROM t1 WHERE x IN(SELECT x FROM t1 WHERE x IN(x));}}
293 {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
294 SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
295 SELECT x FROM t1 WHERE x IN (x)))));}
297 {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)))));}}
300 {SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
301 SELECT x FROM t1 WHERE x IN (SELECT x FROM t1 WHERE x IN (
302 SELECT x FROM t1 WHERE x IN (1)))));}
304 {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(?,?,?)))));}}
307 {SELECT x FROM t1 WHERE x IN (1) UNION ALL SELECT x FROM t1 WHERE x IN (1);}
309 {0 {SELECT x FROM t1 WHERE x IN(?,?,?)UNION ALL SELECT x FROM t1 WHERE x IN(?,?,?);}}
312 {SELECT "col f", [col f] FROM t1;}
314 {0 {SELECT"col f","col f"FROM t1;}}
317 {SELECT a, "col f" FROM t1 LEFT OUTER JOIN t2 ON [t1].[col f] == [t2].[col y];}
319 {0 {SELECT a,"col f"FROM t1 LEFT OUTER JOIN t2 ON t1."col f"==t2."col y";}}
322 {SELECT * FROM ( WITH x AS ( SELECT * FROM t1 WHERE x IN ( 1)) SELECT 10);}
324 {0 {SELECT*FROM(WITH x AS(SELECT*FROM t1 WHERE x IN(?,?,?))SELECT?);}}
327 {SELECT rowid, oid, _rowid_ FROM t1;}
329 {0 {SELECT rowid,oid,_rowid_ FROM t1;}}
332 {SELECT x FROM t1 WHERE x IS NULL;}
334 {0 {SELECT x FROM t1 WHERE x IS NULL;}}
337 {SELECT x FROM t1 WHERE x IS NOT NULL;}
339 {0 {SELECT x FROM t1 WHERE x IS NOT NULL;}}
342 {SELECT x FROM t1 WHERE x = NULL;}
344 {0 {SELECT x FROM t1 WHERE x=?;}}
347 {SELECT x FROM t1 WHERE x IN ([x] IS NOT NULL, NULL, 1, 'a', "b", x'00');}
349 {0 {SELECT x FROM t1 WHERE x IN(x IS NOT NULL,?,?,?,b,?);}}
352 {ATTACH "normalize800.db" AS somefile;}
354 {0 {ATTACH"normalize800.db"AS somefile;}}
357 {ATTACH DATABASE "normalize810.db" AS somefile;}
359 {0 {ATTACH DATABASE"normalize810.db"AS somefile;}}
362 {INSERT INTO t1 (x) VALUES("sl1"), (1), ("sl2"), ('i');}
364 {0 {INSERT INTO t1(x)VALUES(?),(?),(?),(?);}}
367 {UPDATE t1 SET x = "sl1" WHERE x IN (1, "sl2", 'i');}
369 {0 {UPDATE t1 SET x=?WHERE x IN(?,?,?);}}
372 {UPDATE t1 SET x = "y" WHERE x IN (1, "sl1", 'i');}
374 {0 {UPDATE t1 SET x=y WHERE x IN(?,?,?);}}
377 {DELETE FROM t1 WHERE x IN (1, "sl1", 'i');}
379 {0 {DELETE FROM t1 WHERE x IN(?,?,?);}}
383 set STMT [sqlite3_prepare_v3 $DB $sql -1 $flags TAIL]
384 sqlite3_normalized_sql $STMT
386 if {[info exists STMT]} {
387 sqlite3_finalize $STMT; unset STMT