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