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