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