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