blob: 9a2c9dba512a813bc1092c93e767af42f7e80770 [file] [log] [blame]
drh8ce10ba2003-06-22 01:41:49 +00001# 2003 June 21
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library.
12#
13# This file implements tests for miscellanous features that were
14# left out of other test files.
15#
drh798da522004-11-04 04:42:28 +000016# $Id: misc2.test,v 1.13 2004/11/04 04:42:28 drh Exp $
drh8ce10ba2003-06-22 01:41:49 +000017
18set testdir [file dirname $argv0]
19source $testdir/tester.tcl
20
drh798da522004-11-04 04:42:28 +000021ifcapable {trigger} {
drh8ce10ba2003-06-22 01:41:49 +000022# Test for ticket #360
23#
24do_test misc2-1.1 {
25 catchsql {
26 CREATE TABLE FOO(bar integer);
27 CREATE TRIGGER foo_insert BEFORE INSERT ON foo BEGIN
28 SELECT CASE WHEN (NOT new.bar BETWEEN 0 AND 20)
29 THEN raise(rollback, 'aiieee') END;
30 END;
31 INSERT INTO foo(bar) VALUES (1);
32 }
drhdc339ee2003-06-29 20:25:08 +000033} {0 {}}
34do_test misc2-1.2 {
35 catchsql {
36 INSERT INTO foo(bar) VALUES (111);
37 }
drh8ce10ba2003-06-22 01:41:49 +000038} {1 aiieee}
drh798da522004-11-04 04:42:28 +000039} ;# endif trigger
drhd60ccc62003-06-24 10:39:46 +000040
41# Make sure ROWID works on a view and a subquery. Ticket #364
42#
43do_test misc2-2.1 {
44 execsql {
45 CREATE TABLE t1(a,b,c);
46 INSERT INTO t1 VALUES(1,2,3);
drhda808d52003-07-09 16:34:56 +000047 CREATE TABLE t2(a,b,c);
drhd60ccc62003-06-24 10:39:46 +000048 INSERT INTO t2 VALUES(7,8,9);
49 SELECT rowid, * FROM (SELECT * FROM t1, t2);
50 }
51} {{} 1 2 3 7 8 9}
52do_test misc2-2.2 {
53 execsql {
54 CREATE VIEW v1 AS SELECT * FROM t1, t2;
55 SELECT rowid, * FROM v1;
56 }
57} {{} 1 2 3 7 8 9}
drhda808d52003-07-09 16:34:56 +000058
59# Check name binding precedence. Ticket #387
60#
61do_test misc2-3.1 {
62 catchsql {
63 SELECT t1.b+t2.b AS a, t1.a, t2.a FROM t1, t2 WHERE a==10
64 }
65} {1 {ambiguous column name: a}}
drhdc2d94d2003-07-27 17:16:06 +000066
67# Make sure 32-bit integer overflow is handled properly in queries.
68# ticket #408
69#
70do_test misc2-4.1 {
71 execsql {
72 INSERT INTO t1 VALUES(4000000000,'a','b');
73 SELECT a FROM t1 WHERE a>1;
74 }
75} {4000000000}
76do_test misc2-4.2 {
77 execsql {
78 INSERT INTO t1 VALUES(2147483648,'b2','c2');
79 INSERT INTO t1 VALUES(2147483647,'b3','c3');
80 SELECT a FROM t1 WHERE a>2147483647;
81 }
82} {4000000000 2147483648}
83do_test misc2-4.3 {
84 execsql {
85 SELECT a FROM t1 WHERE a<2147483648;
86 }
87} {1 2147483647}
88do_test misc2-4.4 {
89 execsql {
90 SELECT a FROM t1 WHERE a<=2147483648;
91 }
92} {1 2147483648 2147483647}
93do_test misc2-4.5 {
94 execsql {
95 SELECT a FROM t1 WHERE a<10000000000;
96 }
97} {1 4000000000 2147483648 2147483647}
98do_test misc2-4.6 {
99 execsql {
100 SELECT a FROM t1 WHERE a<1000000000000 ORDER BY 1;
101 }
102} {1 2147483647 2147483648 4000000000}
drh4305d102003-07-30 12:34:12 +0000103
104# There were some issues with expanding a SrcList object using a call
105# to sqliteSrcListAppend() if the SrcList had previously been duplicated
106# using a call to sqliteSrcListDup(). Ticket #416. The following test
107# makes sure the problem has been fixed.
108#
109do_test misc2-5.1 {
110 execsql {
111 CREATE TABLE x(a,b);
112 CREATE VIEW y AS
113 SELECT x1.b AS p, x2.b AS q FROM x AS x1, x AS x2 WHERE x1.a=x2.a;
114 CREATE VIEW z AS
115 SELECT y1.p, y2.p FROM y AS y1, y AS y2 WHERE y1.q=y2.q;
116 SELECT * from z;
117 }
118} {}
drh901afd42003-08-26 11:25:58 +0000119
120# Make sure we can open a database with an empty filename. What this
121# does is store the database in a temporary file that is deleted when
122# the database is closed. Ticket #432.
123#
124do_test misc2-6.1 {
125 db close
drhef4ac8f2004-06-19 00:16:31 +0000126 sqlite3 db {}
drh901afd42003-08-26 11:25:58 +0000127 execsql {
128 CREATE TABLE t1(a,b);
129 INSERT INTO t1 VALUES(1,2);
130 SELECT * FROM t1;
131 }
132} {1 2}
drhe4c61692003-08-27 22:54:31 +0000133
134# Make sure we get an error message (not a segfault) on an attempt to
135# update a table from within the callback of a select on that same
136# table.
137#
138do_test misc2-7.1 {
139 db close
140 file delete -force test.db
drhef4ac8f2004-06-19 00:16:31 +0000141 sqlite3 db test.db
drhe4c61692003-08-27 22:54:31 +0000142 execsql {
143 CREATE TABLE t1(x);
144 INSERT INTO t1 VALUES(1);
145 }
146 set rc [catch {
147 db eval {SELECT rowid FROM t1} {} {
148 db eval "DELETE FROM t1 WHERE rowid=$rowid"
149 }
150 } msg]
151 lappend rc $msg
152} {1 {database table is locked}}
153do_test misc2-7.2 {
154 set rc [catch {
155 db eval {SELECT rowid FROM t1} {} {
156 db eval "INSERT INTO t1 VALUES(3)"
157 }
158 } msg]
159 lappend rc $msg
160} {1 {database table is locked}}
161do_test misc2-7.3 {
162 db close
163 file delete -force test.db
drhef4ac8f2004-06-19 00:16:31 +0000164 sqlite3 db :memory:
drhe4c61692003-08-27 22:54:31 +0000165 execsql {
166 CREATE TABLE t1(x);
167 INSERT INTO t1 VALUES(1);
168 }
169 set rc [catch {
170 db eval {SELECT rowid FROM t1} {} {
171 db eval "DELETE FROM t1 WHERE rowid=$rowid"
172 }
173 } msg]
174 lappend rc $msg
175} {1 {database table is locked}}
176do_test misc2-7.4 {
177 set rc [catch {
178 db eval {SELECT rowid FROM t1} {} {
179 db eval "INSERT INTO t1 VALUES(3)"
180 }
181 } msg]
182 lappend rc $msg
183} {1 {database table is locked}}
drh61b487d2003-09-12 02:08:14 +0000184
185# Ticket #453. If the SQL ended with "-", the tokenizer was calling that
186# an incomplete token, which caused problem. The solution was to just call
187# it a minus sign.
188#
189do_test misc2-8.1 {
190 catchsql {-}
191} {1 {near "-": syntax error}}
drh0f18bfa2003-12-10 01:31:21 +0000192
193# Ticket #513. Make sure the VDBE stack does not grow on a 3-way join.
194#
195do_test misc2-9.1 {
196 execsql {
197 BEGIN;
198 CREATE TABLE counts(n INTEGER PRIMARY KEY);
199 INSERT INTO counts VALUES(0);
200 INSERT INTO counts VALUES(1);
201 INSERT INTO counts SELECT n+2 FROM counts;
202 INSERT INTO counts SELECT n+4 FROM counts;
203 INSERT INTO counts SELECT n+8 FROM counts;
204 COMMIT;
205
206 CREATE TEMP TABLE x AS
207 SELECT dim1.n, dim2.n, dim3.n
208 FROM counts AS dim1, counts AS dim2, counts AS dim3
209 WHERE dim1.n<10 AND dim2.n<10 AND dim3.n<10;
210
211 SELECT count(*) FROM x;
212 }
213} {1000}
214do_test misc2-9.2 {
215 execsql {
216 DROP TABLE x;
217 CREATE TEMP TABLE x AS
218 SELECT dim1.n, dim2.n, dim3.n
219 FROM counts AS dim1, counts AS dim2, counts AS dim3
220 WHERE dim1.n>=6 AND dim2.n>=6 AND dim3.n>=6;
221
222 SELECT count(*) FROM x;
223 }
224} {1000}
225do_test misc2-9.3 {
226 execsql {
227 DROP TABLE x;
228 CREATE TEMP TABLE x AS
229 SELECT dim1.n, dim2.n, dim3.n, dim4.n
230 FROM counts AS dim1, counts AS dim2, counts AS dim3, counts AS dim4
231 WHERE dim1.n<5 AND dim2.n<5 AND dim3.n<5 AND dim4.n<5;
232
233 SELECT count(*) FROM x;
234 }
235} [expr 5*5*5*5]
drhacf4ac92003-12-17 23:57:34 +0000236
237finish_test