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