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