3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # 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.
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.
15 # $Id: insert2.test,v 1.19 2008/01/16 18:20:42 danielk1977 Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
20 # Create some tables with data that we can select against
23 execsql {CREATE TABLE d1(n int, log int);}
24 for {set i 1} {$i<=20} {incr i} {
25 for {set j 0} {(1<<$j)<$i} {incr j} {}
26 execsql "INSERT INTO d1 VALUES($i,$j)"
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}
31 # Insert into a new table from the old one.
33 do_test insert2-1.1.1 {
35 CREATE TABLE t1(log int, cnt int);
36 PRAGMA count_changes=on;
40 EXPLAIN INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
44 INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log;
47 do_test insert2-1.1.2 {
50 do_test insert2-1.1.3 {
51 execsql {SELECT * FROM t1 ORDER BY log}
52 } {0 1 1 1 2 2 3 4 4 8 5 4}
55 do_test insert2-1.2.1 {
56 catch {execsql {DROP TABLE t1}}
58 CREATE TABLE t1(log int, cnt int);
60 SELECT log, count(*) FROM d1 GROUP BY log
61 EXCEPT SELECT n-1,log FROM d1;
64 do_test insert2-1.2.2 {
66 SELECT * FROM t1 ORDER BY log;
69 do_test insert2-1.3.1 {
70 catch {execsql {DROP TABLE t1}}
72 CREATE TABLE t1(log int, cnt int);
73 PRAGMA count_changes=off;
75 SELECT log, count(*) FROM d1 GROUP BY log
76 INTERSECT SELECT n-1,log FROM d1;
79 do_test insert2-1.3.2 {
81 SELECT * FROM t1 ORDER BY log;
84 } ;# ifcapable compound
85 execsql {PRAGMA count_changes=off;}
88 catch {execsql {DROP TABLE t1}}
90 CREATE TABLE t1(log int, cnt int);
91 CREATE INDEX i1 ON t1(log);
92 CREATE INDEX i2 ON t1(cnt);
93 INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log;
94 SELECT * FROM t1 ORDER BY log;
96 lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}]
97 lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}]
98 } {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}}
100 do_test insert2-2.0 {
102 CREATE TABLE t3(a,b,c);
103 CREATE TABLE t4(x,y);
104 INSERT INTO t4 VALUES(1,2);
108 do_test insert2-2.1 {
110 INSERT INTO t3(a,c) SELECT * FROM t4;
114 do_test insert2-2.2 {
117 INSERT INTO t3(c,b) SELECT * FROM t4;
121 do_test insert2-2.3 {
124 INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4;
129 integrity_check insert2-3.0
131 # File table t4 with lots of data
133 do_test insert2-3.1 {
138 do_test insert2-3.2 {
139 set x [db total_changes]
142 INSERT INTO t4 VALUES(2,4);
143 INSERT INTO t4 VALUES(3,6);
144 INSERT INTO t4 VALUES(4,8);
145 INSERT INTO t4 VALUES(5,10);
146 INSERT INTO t4 VALUES(6,12);
147 INSERT INTO t4 VALUES(7,14);
148 INSERT INTO t4 VALUES(8,16);
149 INSERT INTO t4 VALUES(9,18);
150 INSERT INTO t4 VALUES(10,20);
153 expr [db total_changes] - $x
155 do_test insert2-3.2.1 {
157 SELECT count(*) FROM t4;
160 do_test insert2-3.3 {
164 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
165 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
166 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
167 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4;
169 SELECT count(*) FROM t4;
172 db function max_x_t4 {execsql {SELECT max(x) FROM t4}}
175 INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
176 INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
177 INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
178 INSERT INTO t4 SELECT x+max_x_t4() ,y FROM t4;
180 SELECT count(*) FROM t4;
184 do_test insert2-3.4 {
187 UPDATE t4 SET y='lots of data for the row where x=' || x
188 || ' and y=' || y || ' - even more data to fill space';
190 SELECT count(*) FROM t4;
193 do_test insert2-3.5 {
197 INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4;
198 SELECT count(*) from t4;
204 INSERT INTO t4 SELECT x+max_x_t4()+1,y FROM t4;
205 SELECT count(*) from t4;
210 do_test insert2-3.6 {
212 SELECT count(*) FROM t4;
215 do_test insert2-3.7 {
218 DELETE FROM t4 WHERE x!=123;
219 SELECT count(*) FROM t4;
223 do_test insert2-3.8 {
226 integrity_check insert2-3.9
231 do_test insert2-4.1 {
233 CREATE TABLE Dependencies(depId integer primary key,
234 class integer, name str, flag str);
235 CREATE TEMPORARY TABLE DepCheck(troveId INT, depNum INT,
236 flagCount INT, isProvides BOOL, class INTEGER, name STRING,
239 VALUES(-1, 0, 1, 0, 2, 'libc.so.6', 'GLIBC_2.0');
240 INSERT INTO Dependencies
246 FROM DepCheck LEFT OUTER JOIN Dependencies ON
247 DepCheck.class == Dependencies.class AND
248 DepCheck.name == Dependencies.name AND
249 DepCheck.flag == Dependencies.flag
251 Dependencies.depId is NULL;
256 #--------------------------------------------------------------------
257 # Test that the INSERT works when the SELECT statement (a) references
258 # the table being inserted into and (b) is optimized to use an index
260 do_test insert2-5.1 {
262 CREATE TABLE t2(a, b);
263 INSERT INTO t2 VALUES(1, 2);
264 CREATE INDEX t2i1 ON t2(a);
265 INSERT INTO t2 SELECT a, 3 FROM t2 WHERE a = 1;
270 do_test insert2-5.2 {
272 INSERT INTO t2 SELECT (SELECT a FROM t2), 4;