xref: /onnv-gate/usr/src/lib/libsqlite/test/insert.test (revision 4520:7dbeadedd7fe)
1*4520Snw141292
2*4520Snw141292#pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292
4*4520Snw141292# 2001 September 15
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.  The
15*4520Snw141292# focus of this file is testing the INSERT statement.
16*4520Snw141292#
17*4520Snw141292# $Id: insert.test,v 1.15 2003/06/15 23:42:25 drh Exp $
18*4520Snw141292
19*4520Snw141292set testdir [file dirname $argv0]
20*4520Snw141292source $testdir/tester.tcl
21*4520Snw141292
22*4520Snw141292# Try to insert into a non-existant table.
23*4520Snw141292#
24*4520Snw141292do_test insert-1.1 {
25*4520Snw141292  set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3)}} msg]
26*4520Snw141292  lappend v $msg
27*4520Snw141292} {1 {no such table: test1}}
28*4520Snw141292
29*4520Snw141292# Try to insert into sqlite_master
30*4520Snw141292#
31*4520Snw141292do_test insert-1.2 {
32*4520Snw141292  set v [catch {execsql {INSERT INTO sqlite_master VALUES(1,2,3,4)}} msg]
33*4520Snw141292  lappend v $msg
34*4520Snw141292} {1 {table sqlite_master may not be modified}}
35*4520Snw141292
36*4520Snw141292# Try to insert the wrong number of entries.
37*4520Snw141292#
38*4520Snw141292do_test insert-1.3 {
39*4520Snw141292  execsql {CREATE TABLE test1(one int, two int, three int)}
40*4520Snw141292  set v [catch {execsql {INSERT INTO test1 VALUES(1,2)}} msg]
41*4520Snw141292  lappend v $msg
42*4520Snw141292} {1 {table test1 has 3 columns but 2 values were supplied}}
43*4520Snw141292do_test insert-1.3b {
44*4520Snw141292  set v [catch {execsql {INSERT INTO test1 VALUES(1,2,3,4)}} msg]
45*4520Snw141292  lappend v $msg
46*4520Snw141292} {1 {table test1 has 3 columns but 4 values were supplied}}
47*4520Snw141292do_test insert-1.3c {
48*4520Snw141292  set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1,2,3,4)}} msg]
49*4520Snw141292  lappend v $msg
50*4520Snw141292} {1 {4 values for 2 columns}}
51*4520Snw141292do_test insert-1.3d {
52*4520Snw141292  set v [catch {execsql {INSERT INTO test1(one,two) VALUES(1)}} msg]
53*4520Snw141292  lappend v $msg
54*4520Snw141292} {1 {1 values for 2 columns}}
55*4520Snw141292
56*4520Snw141292# Try to insert into a non-existant column of a table.
57*4520Snw141292#
58*4520Snw141292do_test insert-1.4 {
59*4520Snw141292  set v [catch {execsql {INSERT INTO test1(one,four) VALUES(1,2)}} msg]
60*4520Snw141292  lappend v $msg
61*4520Snw141292} {1 {table test1 has no column named four}}
62*4520Snw141292
63*4520Snw141292# Make sure the inserts actually happen
64*4520Snw141292#
65*4520Snw141292do_test insert-1.5 {
66*4520Snw141292  execsql {INSERT INTO test1 VALUES(1,2,3)}
67*4520Snw141292  execsql {SELECT * FROM test1}
68*4520Snw141292} {1 2 3}
69*4520Snw141292do_test insert-1.5b {
70*4520Snw141292  execsql {INSERT INTO test1 VALUES(4,5,6)}
71*4520Snw141292  execsql {SELECT * FROM test1 ORDER BY one}
72*4520Snw141292} {1 2 3 4 5 6}
73*4520Snw141292do_test insert-1.5c {
74*4520Snw141292  execsql {INSERT INTO test1 VALUES(7,8,9)}
75*4520Snw141292  execsql {SELECT * FROM test1 ORDER BY one}
76*4520Snw141292} {1 2 3 4 5 6 7 8 9}
77*4520Snw141292
78*4520Snw141292do_test insert-1.6 {
79*4520Snw141292  execsql {DELETE FROM test1}
80*4520Snw141292  execsql {INSERT INTO test1(one,two) VALUES(1,2)}
81*4520Snw141292  execsql {SELECT * FROM test1 ORDER BY one}
82*4520Snw141292} {1 2 {}}
83*4520Snw141292do_test insert-1.6b {
84*4520Snw141292  execsql {INSERT INTO test1(two,three) VALUES(5,6)}
85*4520Snw141292  execsql {SELECT * FROM test1 ORDER BY one}
86*4520Snw141292} {{} 5 6 1 2 {}}
87*4520Snw141292do_test insert-1.6c {
88*4520Snw141292  execsql {INSERT INTO test1(three,one) VALUES(7,8)}
89*4520Snw141292  execsql {SELECT * FROM test1 ORDER BY one}
90*4520Snw141292} {{} 5 6 1 2 {} 8 {} 7}
91*4520Snw141292
92*4520Snw141292# A table to use for testing default values
93*4520Snw141292#
94*4520Snw141292do_test insert-2.1 {
95*4520Snw141292  execsql {
96*4520Snw141292    CREATE TABLE test2(
97*4520Snw141292      f1 int default -111,
98*4520Snw141292      f2 real default +4.32,
99*4520Snw141292      f3 int default +222,
100*4520Snw141292      f4 int default 7.89
101*4520Snw141292    )
102*4520Snw141292  }
103*4520Snw141292  execsql {SELECT * from test2}
104*4520Snw141292} {}
105*4520Snw141292do_test insert-2.2 {
106*4520Snw141292  execsql {INSERT INTO test2(f1,f3) VALUES(+10,-10)}
107*4520Snw141292  execsql {SELECT * FROM test2}
108*4520Snw141292} {10 4.32 -10 7.89}
109*4520Snw141292do_test insert-2.3 {
110*4520Snw141292  execsql {INSERT INTO test2(f2,f4) VALUES(1.23,-3.45)}
111*4520Snw141292  execsql {SELECT * FROM test2 WHERE f1==-111}
112*4520Snw141292} {-111 1.23 222 -3.45}
113*4520Snw141292do_test insert-2.4 {
114*4520Snw141292  execsql {INSERT INTO test2(f1,f2,f4) VALUES(77,+1.23,3.45)}
115*4520Snw141292  execsql {SELECT * FROM test2 WHERE f1==77}
116*4520Snw141292} {77 1.23 222 3.45}
117*4520Snw141292do_test insert-2.10 {
118*4520Snw141292  execsql {
119*4520Snw141292    DROP TABLE test2;
120*4520Snw141292    CREATE TABLE test2(
121*4520Snw141292      f1 int default 111,
122*4520Snw141292      f2 real default -4.32,
123*4520Snw141292      f3 text default hi,
124*4520Snw141292      f4 text default 'abc-123',
125*4520Snw141292      f5 varchar(10)
126*4520Snw141292    )
127*4520Snw141292  }
128*4520Snw141292  execsql {SELECT * from test2}
129*4520Snw141292} {}
130*4520Snw141292do_test insert-2.11 {
131*4520Snw141292  execsql {INSERT INTO test2(f2,f4) VALUES(-2.22,'hi!')}
132*4520Snw141292  execsql {SELECT * FROM test2}
133*4520Snw141292} {111 -2.22 hi hi! {}}
134*4520Snw141292do_test insert-2.12 {
135*4520Snw141292  execsql {INSERT INTO test2(f1,f5) VALUES(1,'xyzzy')}
136*4520Snw141292  execsql {SELECT * FROM test2 ORDER BY f1}
137*4520Snw141292} {1 -4.32 hi abc-123 xyzzy 111 -2.22 hi hi! {}}
138*4520Snw141292
139*4520Snw141292# Do additional inserts with default values, but this time
140*4520Snw141292# on a table that has indices.  In particular we want to verify
141*4520Snw141292# that the correct default values are inserted into the indices.
142*4520Snw141292#
143*4520Snw141292do_test insert-3.1 {
144*4520Snw141292  execsql {
145*4520Snw141292    DELETE FROM test2;
146*4520Snw141292    CREATE INDEX index9 ON test2(f1,f2);
147*4520Snw141292    CREATE INDEX indext ON test2(f4,f5);
148*4520Snw141292    SELECT * from test2;
149*4520Snw141292  }
150*4520Snw141292} {}
151*4520Snw141292do_test insert-3.2 {
152*4520Snw141292  execsql {INSERT INTO test2(f2,f4) VALUES(-3.33,'hum')}
153*4520Snw141292  execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
154*4520Snw141292} {111 -3.33 hi hum {}}
155*4520Snw141292do_test insert-3.3 {
156*4520Snw141292  execsql {INSERT INTO test2(f1,f2,f5) VALUES(22,-4.44,'wham')}
157*4520Snw141292  execsql {SELECT * FROM test2 WHERE f1=111 AND f2=-3.33}
158*4520Snw141292} {111 -3.33 hi hum {}}
159*4520Snw141292do_test insert-3.4 {
160*4520Snw141292  execsql {SELECT * FROM test2 WHERE f1=22 AND f2=-4.44}
161*4520Snw141292} {22 -4.44 hi abc-123 wham}
162*4520Snw141292integrity_check insert-3.5
163*4520Snw141292
164*4520Snw141292# Test of expressions in the VALUES clause
165*4520Snw141292#
166*4520Snw141292do_test insert-4.1 {
167*4520Snw141292  execsql {
168*4520Snw141292    CREATE TABLE t3(a,b,c);
169*4520Snw141292    INSERT INTO t3 VALUES(1+2+3,4,5);
170*4520Snw141292    SELECT * FROM t3;
171*4520Snw141292  }
172*4520Snw141292} {6 4 5}
173*4520Snw141292do_test insert-4.2 {
174*4520Snw141292  execsql {
175*4520Snw141292    INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,5,6);
176*4520Snw141292    SELECT * FROM t3 ORDER BY a;
177*4520Snw141292  }
178*4520Snw141292} {6 4 5 7 5 6}
179*4520Snw141292do_test insert-4.3 {
180*4520Snw141292  catchsql {
181*4520Snw141292    INSERT INTO t3 VALUES((SELECT max(a) FROM t3)+1,t3.a,6);
182*4520Snw141292    SELECT * FROM t3 ORDER BY a;
183*4520Snw141292  }
184*4520Snw141292} {1 {no such column: t3.a}}
185*4520Snw141292do_test insert-4.4 {
186*4520Snw141292  execsql {
187*4520Snw141292    INSERT INTO t3 VALUES((SELECT b FROM t3 WHERE a=0),6,7);
188*4520Snw141292    SELECT * FROM t3 ORDER BY a;
189*4520Snw141292  }
190*4520Snw141292} {{} 6 7 6 4 5 7 5 6}
191*4520Snw141292do_test insert-4.5 {
192*4520Snw141292  execsql {
193*4520Snw141292    SELECT b,c FROM t3 WHERE a IS NULL;
194*4520Snw141292  }
195*4520Snw141292} {6 7}
196*4520Snw141292do_test insert-4.6 {
197*4520Snw141292  catchsql {
198*4520Snw141292    INSERT INTO t3 VALUES(notafunc(2,3),2,3);
199*4520Snw141292  }
200*4520Snw141292} {1 {no such function: notafunc}}
201*4520Snw141292do_test insert-4.7 {
202*4520Snw141292  execsql {
203*4520Snw141292    INSERT INTO t3 VALUES(min(1,2,3),max(1,2,3),99);
204*4520Snw141292    SELECT * FROM t3 WHERE c=99;
205*4520Snw141292  }
206*4520Snw141292} {1 3 99}
207*4520Snw141292
208*4520Snw141292# Test the ability to insert from a temporary table into itself.
209*4520Snw141292# Ticket #275.
210*4520Snw141292#
211*4520Snw141292do_test insert-5.1 {
212*4520Snw141292  execsql {
213*4520Snw141292    CREATE TEMP TABLE t4(x);
214*4520Snw141292    INSERT INTO t4 VALUES(1);
215*4520Snw141292    SELECT * FROM t4;
216*4520Snw141292  }
217*4520Snw141292} {1}
218*4520Snw141292do_test insert-5.2 {
219*4520Snw141292  execsql {
220*4520Snw141292    INSERT INTO t4 SELECT x+1 FROM t4;
221*4520Snw141292    SELECT * FROM t4;
222*4520Snw141292  }
223*4520Snw141292} {1 2}
224*4520Snw141292do_test insert-5.3 {
225*4520Snw141292  # verify that a temporary table is used to copy t4 to t4
226*4520Snw141292  set x [execsql {
227*4520Snw141292    EXPLAIN INSERT INTO t4 SELECT x+2 FROM t4;
228*4520Snw141292  }]
229*4520Snw141292  expr {[lsearch $x OpenTemp]>0}
230*4520Snw141292} {1}
231*4520Snw141292do_test insert-5.4 {
232*4520Snw141292  # Verify that table "test1" begins on page 3.  This should be the same
233*4520Snw141292  # page number used by "t4" above.
234*4520Snw141292  execsql {
235*4520Snw141292    SELECT rootpage FROM sqlite_master WHERE name='test1';
236*4520Snw141292  }
237*4520Snw141292} {3}
238*4520Snw141292do_test insert-5.5 {
239*4520Snw141292  # Verify that "t4" begins on page 3.
240*4520Snw141292  execsql {
241*4520Snw141292    SELECT rootpage FROM sqlite_temp_master WHERE name='t4';
242*4520Snw141292  }
243*4520Snw141292} {3}
244*4520Snw141292do_test insert-5.6 {
245*4520Snw141292  # This should not use an intermediate temporary table.
246*4520Snw141292  execsql {
247*4520Snw141292    INSERT INTO t4 SELECT one FROM test1 WHERE three=7;
248*4520Snw141292    SELECT * FROM t4
249*4520Snw141292  }
250*4520Snw141292} {1 2 8}
251*4520Snw141292do_test insert-5.7 {
252*4520Snw141292  # verify that no temporary table is used to copy test1 to t4
253*4520Snw141292  set x [execsql {
254*4520Snw141292    EXPLAIN INSERT INTO t4 SELECT one FROM test1;
255*4520Snw141292  }]
256*4520Snw141292  expr {[lsearch $x OpenTemp]>0}
257*4520Snw141292} {0}
258*4520Snw141292
259*4520Snw141292# Ticket #334:  REPLACE statement corrupting indices.
260*4520Snw141292#
261*4520Snw141292do_test insert-6.1 {
262*4520Snw141292  execsql {
263*4520Snw141292    CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);
264*4520Snw141292    INSERT INTO t1 VALUES(1,2);
265*4520Snw141292    INSERT INTO t1 VALUES(2,3);
266*4520Snw141292    SELECT b FROM t1 WHERE b=2;
267*4520Snw141292  }
268*4520Snw141292} {2}
269*4520Snw141292do_test insert-6.2 {
270*4520Snw141292  execsql {
271*4520Snw141292    REPLACE INTO t1 VALUES(1,4);
272*4520Snw141292    SELECT b FROM t1 WHERE b=2;
273*4520Snw141292  }
274*4520Snw141292} {}
275*4520Snw141292do_test insert-6.3 {
276*4520Snw141292  execsql {
277*4520Snw141292    UPDATE OR REPLACE t1 SET a=2 WHERE b=4;
278*4520Snw141292    SELECT * FROM t1 WHERE b=4;
279*4520Snw141292  }
280*4520Snw141292} {2 4}
281*4520Snw141292do_test insert-6.4 {
282*4520Snw141292  execsql {
283*4520Snw141292    SELECT * FROM t1 WHERE b=3;
284*4520Snw141292  }
285*4520Snw141292} {}
286*4520Snw141292
287*4520Snw141292integrity_check insert-99.0
288*4520Snw141292
289*4520Snw141292finish_test
290