1# 2001 October 7 2# 3# The author disclaims copyright to this source code. In place of 4# a legal notice, here is a blessing: 5# 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. 9# 10#*********************************************************************** 11# This file implements regression tests for SQLite library. 12# 13# This file implements tests for temporary tables and indices. 14# 15# $Id: temptable.test,v 1.5 2002/05/10 13:14:08 drh Exp $ 16 17set testdir [file dirname $argv0] 18source $testdir/tester.tcl 19 20# Create an alternative connection to the database 21# 22do_test temptable-1.0 { 23 sqlite db2 ./test.db 24 set dummy {} 25} {} 26 27# Create a permanent table. 28# 29do_test temptable-1.1 { 30 execsql {CREATE TABLE t1(a,b,c);} 31 execsql {INSERT INTO t1 VALUES(1,2,3);} 32 execsql {SELECT * FROM t1} 33} {1 2 3} 34do_test temptable-1.2 { 35 catch {db2 eval {SELECT * FROM sqlite_master}} 36 db2 eval {SELECT * FROM t1} 37} {1 2 3} 38do_test temptable-1.3 { 39 execsql {SELECT name FROM sqlite_master} 40} {t1} 41do_test temptable-1.4 { 42 db2 eval {SELECT name FROM sqlite_master} 43} {t1} 44 45# Create a temporary table. Verify that only one of the two 46# processes can see it. 47# 48do_test temptable-1.5 { 49 db2 eval { 50 CREATE TEMP TABLE t2(x,y,z); 51 INSERT INTO t2 VALUES(4,5,6); 52 } 53 db2 eval {SELECT * FROM t2} 54} {4 5 6} 55do_test temptable-1.6 { 56 catch {execsql {SELECT * FROM sqlite_master}} 57 catchsql {SELECT * FROM t2} 58} {1 {no such table: t2}} 59do_test temptable-1.7 { 60 catchsql {INSERT INTO t2 VALUES(8,9,0);} 61} {1 {no such table: t2}} 62do_test temptable-1.8 { 63 db2 eval {INSERT INTO t2 VALUES(8,9,0);} 64 db2 eval {SELECT * FROM t2 ORDER BY x} 65} {4 5 6 8 9 0} 66do_test temptable-1.9 { 67 db2 eval {DELETE FROM t2 WHERE x==8} 68 db2 eval {SELECT * FROM t2 ORDER BY x} 69} {4 5 6} 70do_test temptable-1.10 { 71 db2 eval {DELETE FROM t2} 72 db2 eval {SELECT * FROM t2} 73} {} 74do_test temptable-1.11 { 75 db2 eval { 76 INSERT INTO t2 VALUES(7,6,5); 77 INSERT INTO t2 VALUES(4,3,2); 78 SELECT * FROM t2 ORDER BY x; 79 } 80} {4 3 2 7 6 5} 81do_test temptable-1.12 { 82 db2 eval {DROP TABLE t2;} 83 set r [catch {db2 eval {SELECT * FROM t2}} msg] 84 lappend r $msg 85} {1 {no such table: t2}} 86 87# Make sure temporary tables work with transactions 88# 89do_test temptable-2.1 { 90 execsql { 91 BEGIN TRANSACTION; 92 CREATE TEMPORARY TABLE t2(x,y); 93 INSERT INTO t2 VALUES(1,2); 94 SELECT * FROM t2; 95 } 96} {1 2} 97do_test temptable-2.2 { 98 execsql {ROLLBACK} 99 catchsql {SELECT * FROM t2} 100} {1 {no such table: t2}} 101do_test temptable-2.3 { 102 execsql { 103 BEGIN TRANSACTION; 104 CREATE TEMPORARY TABLE t2(x,y); 105 INSERT INTO t2 VALUES(1,2); 106 SELECT * FROM t2; 107 } 108} {1 2} 109do_test temptable-2.4 { 110 execsql {COMMIT} 111 catchsql {SELECT * FROM t2} 112} {0 {1 2}} 113do_test temptable-2.5 { 114 set r [catch {db2 eval {SELECT * FROM t2}} msg] 115 lappend r $msg 116} {1 {no such table: t2}} 117 118 119# Make sure indices on temporary tables are also temporary. 120# 121do_test temptable-3.1 { 122 execsql { 123 CREATE INDEX i2 ON t2(x); 124 SELECT name FROM sqlite_master WHERE type='index'; 125 } 126} {} 127do_test temptable-3.2 { 128 execsql { 129 SELECT y FROM t2 WHERE x=1; 130 } 131} {2} 132do_test temptable-3.3 { 133 execsql { 134 DROP INDEX i2; 135 SELECT y FROM t2 WHERE x=1; 136 } 137} {2} 138do_test temptable-3.4 { 139 execsql { 140 CREATE INDEX i2 ON t2(x); 141 DROP TABLE t2; 142 } 143 catchsql {DROP INDEX i2} 144} {1 {no such index: i2}} 145 146# Check for correct name collision processing. A name collision can 147# occur when process A creates a temporary table T then process B 148# creates a permanent table also named T. The temp table in process A 149# hides the existance of the permanent table. 150# 151do_test temptable-4.1 { 152 execsql { 153 CREATE TEMP TABLE t2(x,y); 154 INSERT INTO t2 VALUES(10,20); 155 SELECT * FROM t2; 156 } db2 157} {10 20} 158do_test temptable-4.2 { 159 execsql { 160 CREATE TABLE t2(x,y,z); 161 INSERT INTO t2 VALUES(9,8,7); 162 SELECT * FROM t2; 163 } 164} {9 8 7} 165do_test temptable-4.3 { 166 catchsql { 167 SELECT * FROM t2; 168 } db2 169} {1 {database schema has changed}} 170do_test temptable-4.4 { 171 catchsql { 172 SELECT * FROM t2; 173 } db2 174} {0 {10 20}} 175do_test temptable-4.5 { 176 catchsql { 177 DROP TABLE t2; 178 SELECT * FROM t2; 179 } db2 180} {1 {no such table: t2}} 181do_test temptable-4.6 { 182 db2 close 183 sqlite db2 ./test.db 184 catchsql { 185 SELECT * FROM t2; 186 } db2 187} {0 {9 8 7}} 188do_test temptable-4.7 { 189 catchsql { 190 DROP TABLE t2; 191 SELECT * FROM t2; 192 } 193} {1 {no such table: t2}} 194do_test temptable-4.8 { 195 db2 close 196 sqlite db2 ./test.db 197 execsql { 198 CREATE TEMP TABLE t2(x unique,y); 199 INSERT INTO t2 VALUES(1,2); 200 SELECT * FROM t2; 201 } db2 202} {1 2} 203do_test temptable-4.9 { 204 execsql { 205 CREATE TABLE t2(x unique, y); 206 INSERT INTO t2 VALUES(3,4); 207 SELECT * FROM t2; 208 } 209} {3 4} 210do_test temptable-4.10 { 211 catchsql { 212 SELECT * FROM t2; 213 } db2 214} {1 {database schema has changed}} 215do_test temptable-4.11 { 216 execsql { 217 SELECT * FROM t2; 218 } db2 219} {1 2} 220do_test temptable-4.12 { 221 execsql { 222 SELECT * FROM t2; 223 } 224} {3 4} 225do_test temptable-4.13 { 226 catchsql { 227 DROP TABLE t2; 228 SELECT * FROM t2; 229 } db2 230} {1 {no such table: t2}} 231do_test temptable-4.14 { 232 execsql { 233 SELECT * FROM t2; 234 } 235} {3 4} 236do_test temptable-4.15 { 237 db2 close 238 sqlite db2 ./test.db 239 execsql { 240 SELECT * FROM t2; 241 } db2 242} {3 4} 243 244# Now create a temporary table in db2 and a permanent index in db. The 245# temporary table in db2 should mask the name of the permanent index, 246# but the permanent index should still be accessible and should still 247# be updated when its corresponding table changes. 248# 249do_test temptable-5.1 { 250 execsql { 251 CREATE TEMP TABLE mask(a,b,c) 252 } db2 253 execsql { 254 CREATE INDEX mask ON t2(x); 255 SELECT * FROM t2; 256 } 257} {3 4} 258do_test temptable-5.2 { 259 catchsql { 260 SELECT * FROM t2; 261 } db2 262} {1 {database schema has changed}} 263do_test temptable-5.3 { 264 catchsql { 265 SELECT * FROM t2; 266 } db2 267} {0 {3 4}} 268do_test temptable-5.4 { 269 execsql { 270 SELECT y FROM t2 WHERE x=3 271 } 272} {4} 273do_test temptable-5.5 { 274 execsql { 275 SELECT y FROM t2 WHERE x=3 276 } db2 277} {4} 278do_test temptable-5.6 { 279 execsql { 280 INSERT INTO t2 VALUES(1,2); 281 SELECT y FROM t2 WHERE x=1; 282 } db2 283} {2} 284do_test temptable-5.7 { 285 execsql { 286 SELECT y FROM t2 WHERE x=3 287 } db2 288} {4} 289do_test temptable-5.8 { 290 execsql { 291 SELECT y FROM t2 WHERE x=1; 292 } 293} {2} 294do_test temptable-5.9 { 295 execsql { 296 SELECT y FROM t2 WHERE x=3 297 } 298} {4} 299 300db2 close 301 302finish_test 303