2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.
16 # This file implements tests for temporary tables and indices.
18 # $Id: temptable.test,v 1.11 2004/02/14 16:31:04 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Create an alternative connection to the database
25 do_test temptable-1.0 {
30 # Create a permanent table.
32 do_test temptable-1.1 {
33 execsql {CREATE TABLE t1(a,b,c);}
34 execsql {INSERT INTO t1 VALUES(1,2,3);}
35 execsql {SELECT * FROM t1}
37 do_test temptable-1.2 {
38 catch {db2 eval {SELECT * FROM sqlite_master}}
39 db2 eval {SELECT * FROM t1}
41 do_test temptable-1.3 {
42 execsql {SELECT name FROM sqlite_master}
44 do_test temptable-1.4 {
45 db2 eval {SELECT name FROM sqlite_master}
48 # Create a temporary table. Verify that only one of the two
49 # processes can see it.
51 do_test temptable-1.5 {
53 CREATE TEMP TABLE t2(x,y,z);
54 INSERT INTO t2 VALUES(4,5,6);
56 db2 eval {SELECT * FROM t2}
58 do_test temptable-1.6 {
59 catch {execsql {SELECT * FROM sqlite_master}}
60 catchsql {SELECT * FROM t2}
61 } {1 {no such table: t2}}
62 do_test temptable-1.7 {
63 catchsql {INSERT INTO t2 VALUES(8,9,0);}
64 } {1 {no such table: t2}}
65 do_test temptable-1.8 {
66 db2 eval {INSERT INTO t2 VALUES(8,9,0);}
67 db2 eval {SELECT * FROM t2 ORDER BY x}
69 do_test temptable-1.9 {
70 db2 eval {DELETE FROM t2 WHERE x==8}
71 db2 eval {SELECT * FROM t2 ORDER BY x}
73 do_test temptable-1.10 {
74 db2 eval {DELETE FROM t2}
75 db2 eval {SELECT * FROM t2}
77 do_test temptable-1.11 {
79 INSERT INTO t2 VALUES(7,6,5);
80 INSERT INTO t2 VALUES(4,3,2);
81 SELECT * FROM t2 ORDER BY x;
84 do_test temptable-1.12 {
85 db2 eval {DROP TABLE t2;}
86 set r [catch {db2 eval {SELECT * FROM t2}} msg]
88 } {1 {no such table: t2}}
90 # Make sure temporary tables work with transactions
92 do_test temptable-2.1 {
95 CREATE TEMPORARY TABLE t2(x,y);
96 INSERT INTO t2 VALUES(1,2);
100 do_test temptable-2.2 {
102 catchsql {SELECT * FROM t2}
103 } {1 {no such table: t2}}
104 do_test temptable-2.3 {
107 CREATE TEMPORARY TABLE t2(x,y);
108 INSERT INTO t2 VALUES(1,2);
112 do_test temptable-2.4 {
114 catchsql {SELECT * FROM t2}
116 do_test temptable-2.5 {
117 set r [catch {db2 eval {SELECT * FROM t2}} msg]
119 } {1 {no such table: t2}}
121 # Make sure indices on temporary tables are also temporary.
123 do_test temptable-3.1 {
125 CREATE INDEX i2 ON t2(x);
126 SELECT name FROM sqlite_master WHERE type='index';
129 do_test temptable-3.2 {
131 SELECT y FROM t2 WHERE x=1;
134 do_test temptable-3.3 {
137 SELECT y FROM t2 WHERE x=1;
140 do_test temptable-3.4 {
142 CREATE INDEX i2 ON t2(x);
145 catchsql {DROP INDEX i2}
146 } {1 {no such index: i2}}
148 # Check for correct name collision processing. A name collision can
149 # occur when process A creates a temporary table T then process B
150 # creates a permanent table also named T. The temp table in process A
151 # hides the existance of the permanent table.
153 do_test temptable-4.1 {
155 CREATE TEMP TABLE t2(x,y);
156 INSERT INTO t2 VALUES(10,20);
160 do_test temptable-4.2 {
162 CREATE TABLE t2(x,y,z);
163 INSERT INTO t2 VALUES(9,8,7);
167 do_test temptable-4.3 {
172 do_test temptable-4.4.1 {
174 SELECT * FROM temp.t2;
177 do_test temptable-4.4.2 {
179 SELECT * FROM main.t2;
181 } {1 {no such table: main.t2}}
182 #do_test temptable-4.4.3 {
184 # SELECT name FROM main.sqlite_master WHERE type='table';
186 #} {1 {database schema has changed}}
187 do_test temptable-4.4.4 {
189 SELECT name FROM main.sqlite_master WHERE type='table';
192 do_test temptable-4.4.5 {
194 SELECT * FROM main.t2;
197 do_test temptable-4.4.6 {
198 # TEMP takes precedence over MAIN
203 do_test temptable-4.5 {
205 DROP TABLE t2; -- should drop TEMP
206 SELECT * FROM t2; -- data should be from MAIN
209 do_test temptable-4.6 {
216 do_test temptable-4.7 {
221 } {1 {no such table: t2}}
222 do_test temptable-4.8 {
226 CREATE TEMP TABLE t2(x unique,y);
227 INSERT INTO t2 VALUES(1,2);
231 do_test temptable-4.9 {
233 CREATE TABLE t2(x unique, y);
234 INSERT INTO t2 VALUES(3,4);
238 do_test temptable-4.10.1 {
243 #do_test temptable-4.10.2 {
245 # SELECT name FROM sqlite_master WHERE type='table'
247 #} {1 {database schema has changed}}
248 do_test temptable-4.10.3 {
250 SELECT name FROM sqlite_master WHERE type='table'
253 do_test temptable-4.11 {
258 do_test temptable-4.12 {
263 do_test temptable-4.13 {
265 DROP TABLE t2; -- drops TEMP.T2
266 SELECT * FROM t2; -- uses MAIN.T2
269 do_test temptable-4.14 {
274 do_test temptable-4.15 {
282 # Now create a temporary table in db2 and a permanent index in db. The
283 # temporary table in db2 should mask the name of the permanent index,
284 # but the permanent index should still be accessible and should still
285 # be updated when its corresponding table changes.
287 do_test temptable-5.1 {
289 CREATE TEMP TABLE mask(a,b,c)
292 CREATE INDEX mask ON t2(x);
296 #do_test temptable-5.2 {
300 #} {1 {database schema has changed}}
301 do_test temptable-5.3 {
306 do_test temptable-5.4 {
308 SELECT y FROM t2 WHERE x=3
311 do_test temptable-5.5 {
313 SELECT y FROM t2 WHERE x=3
316 do_test temptable-5.6 {
318 INSERT INTO t2 VALUES(1,2);
319 SELECT y FROM t2 WHERE x=1;
322 do_test temptable-5.7 {
324 SELECT y FROM t2 WHERE x=3
327 do_test temptable-5.8 {
329 SELECT y FROM t2 WHERE x=1;
332 do_test temptable-5.9 {
334 SELECT y FROM t2 WHERE x=3
340 # Test for correct operation of read-only databases
342 do_test temptable-6.1 {
345 INSERT INTO t8 VALUES('xyzzy');
349 do_test temptable-6.2 {
351 catch {file attributes test.db -permissions 0444}
352 catch {file attributes test.db -readonly 1}
354 if {[file writable test.db]} {
355 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
361 do_test temptable-6.3 {
362 if {[file writable test.db]} {
363 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
366 CREATE TABLE t9(x,y);
368 } {1 {attempt to write a readonly database}}
369 do_test temptable-6.4 {
371 CREATE TEMP TABLE t9(x,y);
374 do_test temptable-6.5 {
376 INSERT INTO t9 VALUES(1,2);
380 do_test temptable-6.6 {
381 if {[file writable test.db]} {
382 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
385 INSERT INTO t8 VALUES('hello');
388 } {1 {attempt to write a readonly database}}
389 do_test temptable-6.7 {
394 do_test temptable-6.8 {
400 } {1 {no such table: t9}}