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