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 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for miscellanous features that were
14 # left out of other test files.
16 # $Id: misc2.test,v 1.28 2007/09/12 17:01:45 danielk1977 Exp $
18 set testdir [file dirname $argv0]
19 source $testdir/tester.tcl
21 # The tests in this file were written before SQLite supported recursive
22 # trigger invocation, and some tests depend on that to pass. So disable
23 # recursive triggers for this file.
24 catchsql { pragma recursive_triggers = off }
27 # Test for ticket #360
31 CREATE TABLE FOO(bar integer);
32 CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN
33 SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20)
34 THEN raise(rollback, 'aiieee') END;
36 INSERT INTO foo(bar) VALUES (1);
41 INSERT INTO foo(bar) VALUES (111);
46 # Make sure ROWID works on a view and a subquery. Ticket #364
50 CREATE TABLE t1(a,b,c);
51 INSERT INTO t1 VALUES(1,2,3);
52 CREATE TABLE t2(a,b,c);
53 INSERT INTO t2 VALUES(7,8,9);
57 do_catchsql_test misc2-2.2 {
58 SELECT rowid, * FROM (SELECT * FROM t1, t2);
59 } {1 {no such column: rowid}}
60 do_catchsql_test misc2-2.2b {
61 SELECT 'rowid', * FROM (SELECT * FROM t1, t2);
62 } {0 {rowid 1 2 3 7 8 9}}
66 do_catchsql_test misc2-2.3 {
67 CREATE VIEW v1 AS SELECT * FROM t1, t2;
68 SELECT rowid, * FROM v1;
69 } {1 {no such column: rowid}}
70 do_catchsql_test misc2-2.3b {
71 SELECT 'rowid', * FROM v1;
72 } {0 {rowid 1 2 3 7 8 9}}
75 # Ticket #2002 and #1952.
79 SELECT * FROM (SELECT a, b AS 'a', c AS 'a', 4 AS 'a' FROM t1)
81 } {a 1 a:1 2 a:2 3 a:3 4}
84 # Check name binding precedence. Ticket #387
88 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10
90 } {1 {ambiguous column name: a}}
92 # Make sure 32-bit integer overflow is handled properly in queries.
97 INSERT INTO t1 VALUES(4000000000,'a','b');
98 SELECT a FROM t1 WHERE a>1;
103 INSERT INTO t1 VALUES(2147483648,'b2','c2');
104 INSERT INTO t1 VALUES(2147483647,'b3','c3');
105 SELECT a FROM t1 WHERE a>2147483647;
107 } {4000000000 2147483648}
110 SELECT a FROM t1 WHERE a<2147483648;
115 SELECT a FROM t1 WHERE a<=2147483648;
117 } {1 2147483648 2147483647}
120 SELECT a FROM t1 WHERE a<10000000000;
122 } {1 4000000000 2147483648 2147483647}
125 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1;
127 } {1 2147483647 2147483648 4000000000}
129 # There were some issues with expanding a SrcList object using a call
130 # to sqliteSrcListAppend() if the SrcList had previously been duplicated
131 # using a call to sqliteSrcListDup(). Ticket #416. The following test
132 # makes sure the problem has been fixed.
139 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a;
141 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q;
147 # Make sure we can open a database with an empty filename. What this
148 # does is store the database in a temporary file that is deleted when
149 # the database is closed. Ticket #432.
155 CREATE TABLE t1(a,b);
156 INSERT INTO t1 VALUES(1,2);
161 # Make sure we get an error message (not a segfault) on an attempt to
162 # update a table from within the callback of a select on that same
165 # 2006-08-16: This has changed. It is now permitted to update
166 # the table being SELECTed from within the callback of the query.
175 INSERT INTO t1 VALUES(1);
176 INSERT INTO t1 VALUES(2);
177 INSERT INTO t1 VALUES(3);
183 db eval {SELECT rowid FROM t1} {} {
184 db eval "DELETE FROM t1 WHERE rowid=$rowid"
190 execsql {SELECT * FROM t1}
195 INSERT INTO t1 VALUES(1);
196 INSERT INTO t1 VALUES(2);
197 INSERT INTO t1 VALUES(3);
198 INSERT INTO t1 VALUES(4);
200 db eval {SELECT rowid, x FROM t1} {
202 db eval {DELETE FROM t1 WHERE rowid=$rowid}
205 execsql {SELECT * FROM t1}
210 INSERT INTO t1 VALUES(1);
211 INSERT INTO t1 VALUES(2);
212 INSERT INTO t1 VALUES(3);
213 INSERT INTO t1 VALUES(4);
215 db eval {SELECT rowid, x FROM t1} {
217 db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
220 execsql {SELECT * FROM t1}
225 INSERT INTO t1 VALUES(1);
226 INSERT INTO t1 VALUES(2);
227 INSERT INTO t1 VALUES(3);
228 INSERT INTO t1 VALUES(4);
230 db eval {SELECT rowid, x FROM t1} {
232 db eval {DELETE FROM t1}
235 execsql {SELECT * FROM t1}
240 INSERT INTO t1 VALUES(1);
241 INSERT INTO t1 VALUES(2);
242 INSERT INTO t1 VALUES(3);
243 INSERT INTO t1 VALUES(4);
245 db eval {SELECT rowid, x FROM t1} {
247 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
250 execsql {SELECT * FROM t1}
255 INSERT INTO t1 VALUES(1);
257 db eval {SELECT rowid, x FROM t1} {
259 db eval {INSERT INTO t1 VALUES($x+1)}
262 execsql {SELECT * FROM t1}
263 } {1 2 3 4 5 6 7 8 9 10}
265 # Repeat the tests 7.1 through 7.8 about but this time do the SELECTs
266 # in reverse order so that we exercise the sqlite3BtreePrev() routine
267 # instead of sqlite3BtreeNext()
275 INSERT INTO t1 VALUES(1);
276 INSERT INTO t1 VALUES(2);
277 INSERT INTO t1 VALUES(3);
283 db eval {SELECT rowid FROM t1 ORDER BY rowid DESC} {} {
284 db eval "DELETE FROM t1 WHERE rowid=$rowid"
290 execsql {SELECT * FROM t1}
295 INSERT INTO t1 VALUES(1);
296 INSERT INTO t1 VALUES(2);
297 INSERT INTO t1 VALUES(3);
298 INSERT INTO t1 VALUES(4);
300 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
302 db eval {DELETE FROM t1 WHERE rowid=$rowid}
305 execsql {SELECT * FROM t1}
310 INSERT INTO t1 VALUES(1);
311 INSERT INTO t1 VALUES(2);
312 INSERT INTO t1 VALUES(3);
313 INSERT INTO t1 VALUES(4);
315 db eval {SELECT rowid, x FROM t1} {
317 db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
320 execsql {SELECT * FROM t1}
325 INSERT INTO t1 VALUES(1);
326 INSERT INTO t1 VALUES(2);
327 INSERT INTO t1 VALUES(3);
328 INSERT INTO t1 VALUES(4);
330 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
332 db eval {DELETE FROM t1}
335 execsql {SELECT * FROM t1}
340 INSERT INTO t1 VALUES(1);
341 INSERT INTO t1 VALUES(2);
342 INSERT INTO t1 VALUES(3);
343 INSERT INTO t1 VALUES(4);
345 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
347 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
350 execsql {SELECT * FROM t1}
355 INSERT INTO t1(rowid,x) VALUES(10,10);
357 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
359 db eval {INSERT INTO t1(rowid,x) VALUES($x-1,$x-1)}
362 execsql {SELECT * FROM t1}
363 } {1 2 3 4 5 6 7 8 9 10}
369 catchsql { pragma recursive_triggers = off }
371 # Ticket #453. If the SQL ended with "-", the tokenizer was calling that
372 # an incomplete token, which caused problem. The solution was to just call
377 } {1 {near "-": syntax error}}
379 # Ticket #513. Make sure the VDBE stack does not grow on a 3-way join.
385 CREATE TABLE counts(n INTEGER PRIMARY KEY);
386 INSERT INTO counts VALUES(0);
387 INSERT INTO counts VALUES(1);
388 INSERT INTO counts SELECT n+2 FROM counts;
389 INSERT INTO counts SELECT n+4 FROM counts;
390 INSERT INTO counts SELECT n+8 FROM counts;
393 CREATE TEMP TABLE x AS
394 SELECT dim1.n, dim2.n, dim3.n
395 FROM counts AS dim1, counts AS dim2, counts AS dim3
396 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10;
398 SELECT count(*) FROM x;
404 CREATE TEMP TABLE x AS
405 SELECT dim1.n, dim2.n, dim3.n
406 FROM counts AS dim1, counts AS dim2, counts AS dim3
407 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6;
409 SELECT count(*) FROM x;
415 CREATE TEMP TABLE x AS
416 SELECT dim1.n, dim2.n, dim3.n, dim4.n
417 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4
418 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5;
420 SELECT count(*) FROM x;
425 # Ticket #1229. Sometimes when a "NEW.X" appears in a SELECT without
426 # a FROM clause deep within a trigger, the code generator is unable to
427 # trace the NEW.X back to an original table and thus figure out its
430 # The SQL code below was causing a segfault.
432 ifcapable subquery&&trigger {
435 CREATE TABLE t1229(x);
436 CREATE TRIGGER r1229 BEFORE INSERT ON t1229 BEGIN
437 INSERT INTO t1229 SELECT y FROM (SELECT new.x y);
439 INSERT INTO t1229 VALUES(1);