blob: f90c320304599108a8240e6cb641db70696125a5 [file] [log] [blame]
drhb19a2bc2001-09-16 00:13:26 +00001# 2001 September 15
drh5974a302000-06-07 14:42:26 +00002#
drhb19a2bc2001-09-16 00:13:26 +00003# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
drh5974a302000-06-07 14:42:26 +00005#
drhb19a2bc2001-09-16 00:13:26 +00006# May you do good and not evil.
7# May you find forgiveness for yourself and forgive others.
8# May you share freely, never taking more than you give.
drh5974a302000-06-07 14:42:26 +00009#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the INSERT statement that takes is
13# result from a SELECT.
14#
drhc8d30ac2002-04-12 10:08:59 +000015# $Id: insert2.test,v 1.9 2002/04/12 10:09:00 drh Exp $
drh5974a302000-06-07 14:42:26 +000016
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19
20# Create some tables with data that we can select against
21#
22do_test insert2-1.0 {
23 execsql {CREATE TABLE d1(n int, log int);}
24 for {set i 1} {$i<=20} {incr i} {
25 for {set j 0} {pow(2,$j)<$i} {incr j} {}
26 execsql "INSERT INTO d1 VALUES($i,$j)"
27 }
28 execsql {SELECT * FROM d1 ORDER BY n}
29} {1 0 2 1 3 2 4 2 5 3 6 3 7 3 8 3 9 4 10 4 11 4 12 4 13 4 14 4 15 4 16 4 17 5 18 5 19 5 20 5}
30
31# Insert into a new table from the old one.
32#
drh1bee3d72001-10-15 00:44:35 +000033do_test insert2-1.1.1 {
drh5974a302000-06-07 14:42:26 +000034 execsql {
35 CREATE TABLE t1(log int, cnt int);
drh1bee3d72001-10-15 00:44:35 +000036 PRAGMA count_changes=on;
drh5974a302000-06-07 14:42:26 +000037 INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
38 }
drh1bee3d72001-10-15 00:44:35 +000039} {6}
40do_test insert2-1.1.2 {
drhc8d30ac2002-04-12 10:08:59 +000041 db changes
42} {6}
43do_test insert2-1.1.3 {
drh5974a302000-06-07 14:42:26 +000044 execsql {SELECT * FROM t1 ORDER BY log}
45} {0 1 1 1 2 2 3 4 4 8 5 4}
drh3fc190c2001-09-14 03:24:23 +000046
drh1bee3d72001-10-15 00:44:35 +000047do_test insert2-1.2.1 {
drh5974a302000-06-07 14:42:26 +000048 catch {execsql {DROP TABLE t1}}
49 execsql {
50 CREATE TABLE t1(log int, cnt int);
51 INSERT INTO t1
52 SELECT log, count(*) FROM d1 GROUP BY log
53 EXCEPT SELECT n-1,log FROM d1;
drh1bee3d72001-10-15 00:44:35 +000054 }
55} {4}
56do_test insert2-1.2.2 {
57 execsql {
drh5974a302000-06-07 14:42:26 +000058 SELECT * FROM t1 ORDER BY log;
59 }
60} {0 1 3 4 4 8 5 4}
drh1bee3d72001-10-15 00:44:35 +000061do_test insert2-1.3.1 {
drh5974a302000-06-07 14:42:26 +000062 catch {execsql {DROP TABLE t1}}
63 execsql {
64 CREATE TABLE t1(log int, cnt int);
drh1bee3d72001-10-15 00:44:35 +000065 PRAGMA count_changes=off;
drh5974a302000-06-07 14:42:26 +000066 INSERT INTO t1
67 SELECT log, count(*) FROM d1 GROUP BY log
68 INTERSECT SELECT n-1,log FROM d1;
drh1bee3d72001-10-15 00:44:35 +000069 }
70} {}
71do_test insert2-1.3.2 {
72 execsql {
drh5974a302000-06-07 14:42:26 +000073 SELECT * FROM t1 ORDER BY log;
74 }
75} {1 1 2 2}
drhcc85b412000-06-07 15:11:27 +000076do_test insert2-1.4 {
77 catch {execsql {DROP TABLE t1}}
78 set r [execsql {
79 CREATE TABLE t1(log int, cnt int);
80 CREATE INDEX i1 ON t1(log);
81 CREATE INDEX i2 ON t1(cnt);
82 INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
83 SELECT * FROM t1 ORDER BY log;
84 }]
85 lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
86 lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
87} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
drh5974a302000-06-07 14:42:26 +000088
drh24e97df2002-02-03 19:06:02 +000089do_test insert2-2.0 {
90 execsql {
91 CREATE TABLE t3(a,b,c);
92 CREATE TABLE t4(x,y);
93 INSERT INTO t4 VALUES(1,2);
94 SELECT * FROM t4;
95 }
96} {1 2}
97do_test insert2-2.1 {
98 execsql {
99 INSERT INTO t3(a,c) SELECT * FROM t4;
100 SELECT * FROM t3;
101 }
102} {1 {} 2}
103do_test insert2-2.2 {
104 execsql {
105 DELETE FROM t3;
106 INSERT INTO t3(c,b) SELECT * FROM t4;
107 SELECT * FROM t3;
108 }
109} {{} 2 1}
110do_test insert2-2.3 {
111 execsql {
112 DELETE FROM t3;
113 INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
114 SELECT * FROM t3;
115 }
116} {hi 2 1}
117
drh1b2e0322002-03-03 02:49:51 +0000118do_test insert2-3.0 {
drhaaab5722002-02-19 13:39:21 +0000119 set x [execsql {PRAGMA integrity_check}]
drh24e97df2002-02-03 19:06:02 +0000120 if {$x==""} {set x ok}
121 set x
122} {ok}
123
drh1b2e0322002-03-03 02:49:51 +0000124# File table t4 with lots of data
125#
126do_test insert2-3.1 {
127 execsql {
128 SELECT * from t4;
129 }
130} {1 2}
131do_test insert2-3.2 {
132 execsql {
133 BEGIN;
134 INSERT INTO t4 VALUES(2,4);
135 INSERT INTO t4 VALUES(3,6);
136 INSERT INTO t4 VALUES(4,8);
137 INSERT INTO t4 VALUES(5,10);
138 INSERT INTO t4 VALUES(6,12);
139 INSERT INTO t4 VALUES(7,14);
140 INSERT INTO t4 VALUES(8,16);
141 INSERT INTO t4 VALUES(9,18);
142 INSERT INTO t4 VALUES(10,20);
143 COMMIT;
drhc8d30ac2002-04-12 10:08:59 +0000144 }
145 db changes
146} {9}
147do_test insert2-3.2.1 {
148 execsql {
drh1b2e0322002-03-03 02:49:51 +0000149 SELECT count(*) FROM t4;
150 }
151} {10}
152do_test insert2-3.3 {
153 execsql {
154 BEGIN;
155 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
156 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
157 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
158 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
159 COMMIT;
160 SELECT count(*) FROM t4;
161 }
162} {160}
163do_test insert2-3.4 {
164 execsql {
165 BEGIN;
166 UPDATE t4 SET y='lots of data for the row where x=' || x
167 || ' and y=' || y || ' - even more data to fill space';
168 COMMIT;
169 SELECT count(*) FROM t4;
170 }
171} {160}
172do_test insert2-3.5 {
173 execsql {
174 BEGIN;
175 INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
176 SELECT count(*) from t4;
177 ROLLBACK;
178 }
179} {320}
180do_test insert2-3.6 {
181 execsql {
182 SELECT count(*) FROM t4;
183 }
184} {160}
185do_test insert2-3.7 {
186 execsql {
187 BEGIN;
188 DELETE FROM t4 WHERE x!=123;
189 SELECT count(*) FROM t4;
190 ROLLBACK;
191 }
192} {1}
drhc8d30ac2002-04-12 10:08:59 +0000193do_test insert2-3.8 {
194 db changes
195} {159}
drh1b2e0322002-03-03 02:49:51 +0000196
drh5974a302000-06-07 14:42:26 +0000197finish_test