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 that takes is 14# result from a SELECT. 15# 16# $Id: insert2.test,v 1.10 2002/06/25 13:16:04 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Create some tables with data that we can select against 22# 23do_test insert2-1.0 { 24 execsql {CREATE TABLE d1(n int, log int);} 25 for {set i 1} {$i<=20} {incr i} { 26 for {set j 0} {pow(2,$j)<$i} {incr j} {} 27 execsql "INSERT INTO d1 VALUES($i,$j)" 28 } 29 execsql {SELECT * FROM d1 ORDER BY n} 30} {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 32# Insert into a new table from the old one. 33# 34do_test insert2-1.1.1 { 35 execsql { 36 CREATE TABLE t1(log int, cnt int); 37 PRAGMA count_changes=on; 38 INSERT INTO t1 SELECT log, count(*) FROM d1 GROUP BY log; 39 } 40} {6} 41do_test insert2-1.1.2 { 42 db changes 43} {6} 44do_test insert2-1.1.3 { 45 execsql {SELECT * FROM t1 ORDER BY log} 46} {0 1 1 1 2 2 3 4 4 8 5 4} 47 48do_test insert2-1.2.1 { 49 catch {execsql {DROP TABLE t1}} 50 execsql { 51 CREATE TABLE t1(log int, cnt int); 52 INSERT INTO t1 53 SELECT log, count(*) FROM d1 GROUP BY log 54 EXCEPT SELECT n-1,log FROM d1; 55 } 56} {4} 57do_test insert2-1.2.2 { 58 execsql { 59 SELECT * FROM t1 ORDER BY log; 60 } 61} {0 1 3 4 4 8 5 4} 62do_test insert2-1.3.1 { 63 catch {execsql {DROP TABLE t1}} 64 execsql { 65 CREATE TABLE t1(log int, cnt int); 66 PRAGMA count_changes=off; 67 INSERT INTO t1 68 SELECT log, count(*) FROM d1 GROUP BY log 69 INTERSECT SELECT n-1,log FROM d1; 70 } 71} {} 72do_test insert2-1.3.2 { 73 execsql { 74 SELECT * FROM t1 ORDER BY log; 75 } 76} {1 1 2 2} 77do_test insert2-1.4 { 78 catch {execsql {DROP TABLE t1}} 79 set r [execsql { 80 CREATE TABLE t1(log int, cnt int); 81 CREATE INDEX i1 ON t1(log); 82 CREATE INDEX i2 ON t1(cnt); 83 INSERT INTO t1 SELECT log, count() FROM d1 GROUP BY log; 84 SELECT * FROM t1 ORDER BY log; 85 }] 86 lappend r [execsql {SELECT cnt FROM t1 WHERE log=3}] 87 lappend r [execsql {SELECT log FROM t1 WHERE cnt=4 ORDER BY log}] 88} {0 1 1 1 2 2 3 4 4 8 5 4 4 {3 5}} 89 90do_test insert2-2.0 { 91 execsql { 92 CREATE TABLE t3(a,b,c); 93 CREATE TABLE t4(x,y); 94 INSERT INTO t4 VALUES(1,2); 95 SELECT * FROM t4; 96 } 97} {1 2} 98do_test insert2-2.1 { 99 execsql { 100 INSERT INTO t3(a,c) SELECT * FROM t4; 101 SELECT * FROM t3; 102 } 103} {1 {} 2} 104do_test insert2-2.2 { 105 execsql { 106 DELETE FROM t3; 107 INSERT INTO t3(c,b) SELECT * FROM t4; 108 SELECT * FROM t3; 109 } 110} {{} 2 1} 111do_test insert2-2.3 { 112 execsql { 113 DELETE FROM t3; 114 INSERT INTO t3(c,a,b) SELECT x, 'hi', y FROM t4; 115 SELECT * FROM t3; 116 } 117} {hi 2 1} 118 119integrity_check insert2-3.0 120 121# File table t4 with lots of data 122# 123do_test insert2-3.1 { 124 execsql { 125 SELECT * from t4; 126 } 127} {1 2} 128do_test insert2-3.2 { 129 execsql { 130 BEGIN; 131 INSERT INTO t4 VALUES(2,4); 132 INSERT INTO t4 VALUES(3,6); 133 INSERT INTO t4 VALUES(4,8); 134 INSERT INTO t4 VALUES(5,10); 135 INSERT INTO t4 VALUES(6,12); 136 INSERT INTO t4 VALUES(7,14); 137 INSERT INTO t4 VALUES(8,16); 138 INSERT INTO t4 VALUES(9,18); 139 INSERT INTO t4 VALUES(10,20); 140 COMMIT; 141 } 142 db changes 143} {9} 144do_test insert2-3.2.1 { 145 execsql { 146 SELECT count(*) FROM t4; 147 } 148} {10} 149do_test insert2-3.3 { 150 execsql { 151 BEGIN; 152 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; 153 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; 154 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; 155 INSERT INTO t4 SELECT x+(SELECT max(x) FROM t4),y FROM t4; 156 COMMIT; 157 SELECT count(*) FROM t4; 158 } 159} {160} 160do_test insert2-3.4 { 161 execsql { 162 BEGIN; 163 UPDATE t4 SET y='lots of data for the row where x=' || x 164 || ' and y=' || y || ' - even more data to fill space'; 165 COMMIT; 166 SELECT count(*) FROM t4; 167 } 168} {160} 169do_test insert2-3.5 { 170 execsql { 171 BEGIN; 172 INSERT INTO t4 SELECT x+(SELECT max(x)+1 FROM t4),y FROM t4; 173 SELECT count(*) from t4; 174 ROLLBACK; 175 } 176} {320} 177do_test insert2-3.6 { 178 execsql { 179 SELECT count(*) FROM t4; 180 } 181} {160} 182do_test insert2-3.7 { 183 execsql { 184 BEGIN; 185 DELETE FROM t4 WHERE x!=123; 186 SELECT count(*) FROM t4; 187 ROLLBACK; 188 } 189} {1} 190do_test insert2-3.8 { 191 db changes 192} {159} 193integrity_check insert2-3.9 194 195finish_test 196