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);
59 SELECT rowid, * FROM (SELECT * FROM t1, t2);
66 CREATE VIEW v1 AS SELECT * FROM t1, t2;
67 SELECT rowid, * FROM v1;
72 # Ticket #2002 and #1952.
76 SELECT * FROM (SELECT a, b AS 'a', c AS 'a', 4 AS 'a' FROM t1)
78 } {a 1 a:1 2 a:2 3 a:3 4}
81 # Check name binding precedence. Ticket #387
85 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10
87 } {1 {ambiguous column name: a}}
89 # Make sure 32-bit integer overflow is handled properly in queries.
94 INSERT INTO t1 VALUES(4000000000,'a','b');
95 SELECT a FROM t1 WHERE a>1;
100 INSERT INTO t1 VALUES(2147483648,'b2','c2');
101 INSERT INTO t1 VALUES(2147483647,'b3','c3');
102 SELECT a FROM t1 WHERE a>2147483647;
104 } {4000000000 2147483648}
107 SELECT a FROM t1 WHERE a<2147483648;
112 SELECT a FROM t1 WHERE a<=2147483648;
114 } {1 2147483648 2147483647}
117 SELECT a FROM t1 WHERE a<10000000000;
119 } {1 4000000000 2147483648 2147483647}
122 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1;
124 } {1 2147483647 2147483648 4000000000}
126 # There were some issues with expanding a SrcList object using a call
127 # to sqliteSrcListAppend() if the SrcList had previously been duplicated
128 # using a call to sqliteSrcListDup(). Ticket #416. The following test
129 # makes sure the problem has been fixed.
136 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a;
138 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q;
144 # Make sure we can open a database with an empty filename. What this
145 # does is store the database in a temporary file that is deleted when
146 # the database is closed. Ticket #432.
152 CREATE TABLE t1(a,b);
153 INSERT INTO t1 VALUES(1,2);
158 # Make sure we get an error message (not a segfault) on an attempt to
159 # update a table from within the callback of a select on that same
162 # 2006-08-16: This has changed. It is now permitted to update
163 # the table being SELECTed from within the callback of the query.
172 INSERT INTO t1 VALUES(1);
173 INSERT INTO t1 VALUES(2);
174 INSERT INTO t1 VALUES(3);
180 db eval {SELECT rowid FROM t1} {} {
181 db eval "DELETE FROM t1 WHERE rowid=$rowid"
187 execsql {SELECT * FROM t1}
192 INSERT INTO t1 VALUES(1);
193 INSERT INTO t1 VALUES(2);
194 INSERT INTO t1 VALUES(3);
195 INSERT INTO t1 VALUES(4);
197 db eval {SELECT rowid, x FROM t1} {
199 db eval {DELETE FROM t1 WHERE rowid=$rowid}
202 execsql {SELECT * FROM t1}
207 INSERT INTO t1 VALUES(1);
208 INSERT INTO t1 VALUES(2);
209 INSERT INTO t1 VALUES(3);
210 INSERT INTO t1 VALUES(4);
212 db eval {SELECT rowid, x FROM t1} {
214 db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
217 execsql {SELECT * FROM t1}
222 INSERT INTO t1 VALUES(1);
223 INSERT INTO t1 VALUES(2);
224 INSERT INTO t1 VALUES(3);
225 INSERT INTO t1 VALUES(4);
227 db eval {SELECT rowid, x FROM t1} {
229 db eval {DELETE FROM t1}
232 execsql {SELECT * FROM t1}
237 INSERT INTO t1 VALUES(1);
238 INSERT INTO t1 VALUES(2);
239 INSERT INTO t1 VALUES(3);
240 INSERT INTO t1 VALUES(4);
242 db eval {SELECT rowid, x FROM t1} {
244 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
247 execsql {SELECT * FROM t1}
252 INSERT INTO t1 VALUES(1);
254 db eval {SELECT rowid, x FROM t1} {
256 db eval {INSERT INTO t1 VALUES($x+1)}
259 execsql {SELECT * FROM t1}
260 } {1 2 3 4 5 6 7 8 9 10}
262 # Repeat the tests 7.1 through 7.8 about but this time do the SELECTs
263 # in reverse order so that we exercise the sqlite3BtreePrev() routine
264 # instead of sqlite3BtreeNext()
272 INSERT INTO t1 VALUES(1);
273 INSERT INTO t1 VALUES(2);
274 INSERT INTO t1 VALUES(3);
280 db eval {SELECT rowid FROM t1 ORDER BY rowid DESC} {} {
281 db eval "DELETE FROM t1 WHERE rowid=$rowid"
287 execsql {SELECT * FROM t1}
292 INSERT INTO t1 VALUES(1);
293 INSERT INTO t1 VALUES(2);
294 INSERT INTO t1 VALUES(3);
295 INSERT INTO t1 VALUES(4);
297 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
299 db eval {DELETE FROM t1 WHERE rowid=$rowid}
302 execsql {SELECT * FROM t1}
307 INSERT INTO t1 VALUES(1);
308 INSERT INTO t1 VALUES(2);
309 INSERT INTO t1 VALUES(3);
310 INSERT INTO t1 VALUES(4);
312 db eval {SELECT rowid, x FROM t1} {
314 db eval {DELETE FROM t1 WHERE rowid=$rowid+1}
317 execsql {SELECT * FROM t1}
322 INSERT INTO t1 VALUES(1);
323 INSERT INTO t1 VALUES(2);
324 INSERT INTO t1 VALUES(3);
325 INSERT INTO t1 VALUES(4);
327 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
329 db eval {DELETE FROM t1}
332 execsql {SELECT * FROM t1}
337 INSERT INTO t1 VALUES(1);
338 INSERT INTO t1 VALUES(2);
339 INSERT INTO t1 VALUES(3);
340 INSERT INTO t1 VALUES(4);
342 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
344 db eval {UPDATE t1 SET x=x+100 WHERE rowid=$rowid}
347 execsql {SELECT * FROM t1}
352 INSERT INTO t1(rowid,x) VALUES(10,10);
354 db eval {SELECT rowid, x FROM t1 ORDER BY rowid DESC} {
356 db eval {INSERT INTO t1(rowid,x) VALUES($x-1,$x-1)}
359 execsql {SELECT * FROM t1}
360 } {1 2 3 4 5 6 7 8 9 10}
366 catchsql { pragma recursive_triggers = off }
368 # Ticket #453. If the SQL ended with "-", the tokenizer was calling that
369 # an incomplete token, which caused problem. The solution was to just call
374 } {1 {near "-": syntax error}}
376 # Ticket #513. Make sure the VDBE stack does not grow on a 3-way join.
382 CREATE TABLE counts(n INTEGER PRIMARY KEY);
383 INSERT INTO counts VALUES(0);
384 INSERT INTO counts VALUES(1);
385 INSERT INTO counts SELECT n+2 FROM counts;
386 INSERT INTO counts SELECT n+4 FROM counts;
387 INSERT INTO counts SELECT n+8 FROM counts;
390 CREATE TEMP TABLE x AS
391 SELECT dim1.n, dim2.n, dim3.n
392 FROM counts AS dim1, counts AS dim2, counts AS dim3
393 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10;
395 SELECT count(*) FROM x;
401 CREATE TEMP TABLE x AS
402 SELECT dim1.n, dim2.n, dim3.n
403 FROM counts AS dim1, counts AS dim2, counts AS dim3
404 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6;
406 SELECT count(*) FROM x;
412 CREATE TEMP TABLE x AS
413 SELECT dim1.n, dim2.n, dim3.n, dim4.n
414 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4
415 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5;
417 SELECT count(*) FROM x;
422 # Ticket #1229. Sometimes when a "NEW.X" appears in a SELECT without
423 # a FROM clause deep within a trigger, the code generator is unable to
424 # trace the NEW.X back to an original table and thus figure out its
427 # The SQL code below was causing a segfault.
429 ifcapable subquery&&trigger {
432 CREATE TABLE t1229(x);
433 CREATE TRIGGER r1229 BEFORE INSERT ON t1229 BEGIN
434 INSERT INTO t1229 SELECT y FROM (SELECT new.x y);
436 INSERT INTO t1229 VALUES(1);