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.3 2001/10/09 12:39:24 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} {} 25 26# Create a permanent table. 27# 28do_test temptable-1.1 { 29 execsql {CREATE TABLE t1(a,b,c);} 30 execsql {INSERT INTO t1 VALUES(1,2,3);} 31 execsql {SELECT * FROM t1} 32} {1 2 3} 33do_test temptable-1.2 { 34 catch {db2 eval {SELECT * FROM sqlite_master}} 35 db2 eval {SELECT * FROM t1} 36} {1 2 3} 37do_test temptable-1.3 { 38 execsql {SELECT name FROM sqlite_master} 39} {t1} 40do_test temptable-1.4 { 41 db2 eval {SELECT name FROM sqlite_master} 42} {t1} 43 44# Create a temporary table. Verify that only one of the two 45# processes can see it. 46# 47do_test temptable-1.5 { 48 db2 eval { 49 CREATE TEMP TABLE t2(x,y,z); 50 INSERT INTO t2 VALUES(4,5,6); 51 } 52 db2 eval {SELECT * FROM t2} 53} {4 5 6} 54do_test temptable-1.6 { 55 catch {execsql {SELECT * FROM sqlite_master}} 56 catchsql {SELECT * FROM t2} 57} {1 {no such table: t2}} 58do_test temptable-1.7 { 59 catchsql {INSERT INTO t2 VALUES(8,9,0);} 60} {1 {no such table: t2}} 61do_test temptable-1.8 { 62 db2 eval {INSERT INTO t2 VALUES(8,9,0);} 63 db2 eval {SELECT * FROM t2 ORDER BY x} 64} {4 5 6 8 9 0} 65do_test temptable-1.9 { 66 db2 eval {DELETE FROM t2 WHERE x==8} 67 db2 eval {SELECT * FROM t2 ORDER BY x} 68} {4 5 6} 69do_test temptable-1.10 { 70 db2 eval {DELETE FROM t2} 71 db2 eval {SELECT * FROM t2} 72} {} 73do_test temptable-1.11 { 74 db2 eval { 75 INSERT INTO t2 VALUES(7,6,5); 76 INSERT INTO t2 VALUES(4,3,2); 77 SELECT * FROM t2 ORDER BY x; 78 } 79} {4 3 2 7 6 5} 80do_test temptable-1.12 { 81 db2 eval {DROP TABLE t2;} 82 set r [catch {db2 eval {SELECT * FROM t2}} msg] 83 lappend r $msg 84} {1 {no such table: t2}} 85 86# Make sure temporary tables work with transactions 87# 88do_test temptable-2.1 { 89 execsql { 90 BEGIN TRANSACTION; 91 CREATE TEMPORARY TABLE t2(x,y); 92 INSERT INTO t2 VALUES(1,2); 93 SELECT * FROM t2; 94 } 95} {1 2} 96do_test temptable-2.2 { 97 execsql {ROLLBACK} 98 catchsql {SELECT * FROM t2} 99} {1 {no such table: t2}} 100do_test temptable-2.3 { 101 execsql { 102 BEGIN TRANSACTION; 103 CREATE TEMPORARY TABLE t2(x,y); 104 INSERT INTO t2 VALUES(1,2); 105 SELECT * FROM t2; 106 } 107} {1 2} 108do_test temptable-2.4 { 109 execsql {COMMIT} 110 catchsql {SELECT * FROM t2} 111} {0 {1 2}} 112do_test temptable-2.5 { 113 set r [catch {db2 eval {SELECT * FROM t2}} msg] 114 lappend r $msg 115} {1 {no such table: t2}} 116 117 118# Make sure indices on temporary tables are also temporary. 119# 120do_test temptable-3.1 { 121 execsql { 122 CREATE INDEX i2 ON t2(x); 123 SELECT name FROM sqlite_master WHERE type='index'; 124 } 125} {} 126do_test temptable-3.2 { 127 execsql { 128 SELECT y FROM t2 WHERE x=1; 129 } 130} {2} 131do_test temptable-3.3 { 132 execsql { 133 DROP INDEX i2; 134 SELECT y FROM t2 WHERE x=1; 135 } 136} {2} 137do_test temptable-3.4 { 138 execsql { 139 CREATE INDEX i2 ON t2(x); 140 DROP TABLE t2; 141 } 142 catchsql {DROP INDEX i2} 143} {1 {no such index: i2}} 144 145# Check for correct name collision processing. A name collision can 146# occur when process A creates a temporary table T then process B 147# creates a permanent table also named T. The temp table in process A 148# hides the existance of the permanent table. 149# 150do_test temptable-4.1 { 151 db2 eval { 152 CREATE TEMP TABLE t2(x,y); 153 INSERT INTO t2 VALUES(10,20); 154 SELECT * FROM t2; 155 } 156} {10 20} 157do_test temptable-4.2 { 158 execsql { 159 CREATE TABLE t2(x,y,z); 160 INSERT INTO t2 VALUES(9,8,7); 161 SELECT * FROM t2; 162 } 163} {9 8 7} 164do_test temptable-4.3 { 165 set r [catch {db2 eval {SELECT * FROM t2}} msg] 166 lappend r $msg 167} {1 {database schema has changed}} 168do_test temptable-4.4 { 169 set r [catch {db2 eval {SELECT * FROM t2}} msg] 170 lappend r $msg 171} {0 {10 20}} 172do_test temptable-4.5 { 173 db2 eval {DROP TABLE t2} 174 set r [catch {db2 eval {SELECT * FROM t2}} msg] 175 lappend r $msg 176} {1 {no such table: t2}} 177do_test temptable-4.6 { 178 db2 close 179 sqlite db2 ./test.db 180 set r [catch {db2 eval {SELECT * FROM t2}} msg] 181 lappend r $msg 182} {0 {9 8 7}} 183 184# Now create a temporary table in db2 and a permanent index in db. The 185# temporary table in db2 should mask the name of the permanent index, 186# but the permanent index should still be accessible and should still 187# be updated when its correspnding table changes. 188# 189do_test temptable-5.1 { 190 db2 eval {CREATE TEMP TABLE mask(a,b,c)} 191 execsql { 192 CREATE INDEX mask ON t2(x); 193 SELECT * FROM t2; 194 } 195} {9 8 7} 196do_test temptable-5.2 { 197 set r [catch {db2 eval {SELECT * FROM t2}} msg] 198 lappend r $msg 199} {1 {database schema has changed}} 200do_test temptable-5.3 { 201 set r [catch {db2 eval {SELECT * FROM t2}} msg] 202 lappend r $msg 203} {0 {9 8 7}} 204do_test temptable-5.4 { 205 execsql {SELECT y FROM t2 WHERE x=9} 206} {8} 207do_test temptable-5.5 { 208 db2 eval {SELECT y FROM t2 WHERE x=9} 209} {8} 210do_test temptable-5.6 { 211 db2 eval { 212 INSERT INTO t2 VALUES(1,2,3); 213 SELECT y FROM t2 WHERE x=1; 214 } 215} {2} 216do_test temptable-5.7 { 217 db2 eval {SELECT y FROM t2 WHERE x=9} 218} {8} 219do_test temptable-5.8 { 220 execsql { 221 SELECT y FROM t2 WHERE x=1; 222 } 223} {2} 224do_test temptable-5.9 { 225 execsql {SELECT y FROM t2 WHERE x=9} 226} {8} 227 228db2 close 229 230finish_test 231