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.
13 # This file implements tests for temporary tables and indices.
15 # $Id: temptable.test,v 1.21 2009/06/16 17:49:36 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
25 # Create an alternative connection to the database
27 do_test temptable-1.0 {
32 # Create a permanent table.
34 do_test temptable-1.1 {
35 execsql {CREATE TABLE t1(a,b,c);}
36 execsql {INSERT INTO t1 VALUES(1,2,3);}
37 execsql {SELECT * FROM t1}
39 do_test temptable-1.2 {
40 catch {db2 eval {SELECT * FROM sqlite_master}}
41 db2 eval {SELECT * FROM t1}
43 do_test temptable-1.3 {
44 execsql {SELECT name FROM sqlite_master}
46 do_test temptable-1.4 {
47 db2 eval {SELECT name FROM sqlite_master}
50 # Create a temporary table. Verify that only one of the two
51 # processes can see it.
53 do_test temptable-1.5 {
55 CREATE TEMP TABLE t2(x,y,z);
56 INSERT INTO t2 VALUES(4,5,6);
58 db2 eval {SELECT * FROM t2}
60 do_test temptable-1.6 {
61 catch {execsql {SELECT * FROM sqlite_master}}
62 catchsql {SELECT * FROM t2}
63 } {1 {no such table: t2}}
64 do_test temptable-1.7 {
65 catchsql {INSERT INTO t2 VALUES(8,9,0);}
66 } {1 {no such table: t2}}
67 do_test temptable-1.8 {
68 db2 eval {INSERT INTO t2 VALUES(8,9,0);}
69 db2 eval {SELECT * FROM t2 ORDER BY x}
71 do_test temptable-1.9 {
72 db2 eval {DELETE FROM t2 WHERE x==8}
73 db2 eval {SELECT * FROM t2 ORDER BY x}
75 do_test temptable-1.10 {
76 db2 eval {DELETE FROM t2}
77 db2 eval {SELECT * FROM t2}
79 do_test temptable-1.11 {
81 INSERT INTO t2 VALUES(7,6,5);
82 INSERT INTO t2 VALUES(4,3,2);
83 SELECT * FROM t2 ORDER BY x;
86 do_test temptable-1.12 {
87 db2 eval {DROP TABLE t2;}
88 set r [catch {db2 eval {SELECT * FROM t2}} msg]
90 } {1 {no such table: t2}}
92 # Make sure temporary tables work with transactions
94 do_test temptable-2.1 {
97 CREATE TEMPORARY TABLE t2(x,y);
98 INSERT INTO t2 VALUES(1,2);
102 do_test temptable-2.2 {
104 catchsql {SELECT * FROM t2}
105 } {1 {no such table: t2}}
106 do_test temptable-2.3 {
109 CREATE TEMPORARY TABLE t2(x,y);
110 INSERT INTO t2 VALUES(1,2);
114 do_test temptable-2.4 {
116 catchsql {SELECT * FROM t2}
118 do_test temptable-2.5 {
119 set r [catch {db2 eval {SELECT * FROM t2}} msg]
121 } {1 {no such table: t2}}
123 # Make sure indices on temporary tables are also temporary.
125 do_test temptable-3.1 {
127 CREATE INDEX i2 ON t2(x);
128 SELECT name FROM sqlite_master WHERE type='index';
131 do_test temptable-3.2 {
133 SELECT y FROM t2 WHERE x=1;
136 do_test temptable-3.3 {
139 SELECT y FROM t2 WHERE x=1;
142 do_test temptable-3.4 {
144 CREATE INDEX i2 ON t2(x);
147 catchsql {DROP INDEX i2}
148 } {1 {no such index: i2}}
150 # Check for correct name collision processing. A name collision can
151 # occur when process A creates a temporary table T then process B
152 # creates a permanent table also named T. The temp table in process A
153 # hides the existence of the permanent table.
155 do_test temptable-4.1 {
157 CREATE TEMP TABLE t2(x,y);
158 INSERT INTO t2 VALUES(10,20);
162 do_test temptable-4.2 {
164 CREATE TABLE t2(x,y,z);
165 INSERT INTO t2 VALUES(9,8,7);
169 do_test temptable-4.3 {
174 do_test temptable-4.4.1 {
176 SELECT * FROM temp.t2;
179 do_test temptable-4.4.2 {
181 SELECT * FROM main.t2;
184 #do_test temptable-4.4.3 {
186 # SELECT name FROM main.sqlite_master WHERE type='table';
188 #} {1 {database schema has changed}}
189 do_test temptable-4.4.4 {
191 SELECT name FROM main.sqlite_master WHERE type='table';
194 do_test temptable-4.4.5 {
196 SELECT * FROM main.t2;
199 do_test temptable-4.4.6 {
200 # TEMP takes precedence over MAIN
205 do_test temptable-4.5 {
207 DROP TABLE t2; -- should drop TEMP
208 SELECT * FROM t2; -- data should be from MAIN
211 do_test temptable-4.6 {
213 sqlite3 db2 ./test.db
218 do_test temptable-4.7 {
223 } {1 {no such table: t2}}
224 do_test temptable-4.8 {
226 sqlite3 db2 ./test.db
228 CREATE TEMP TABLE t2(x unique,y);
229 INSERT INTO t2 VALUES(1,2);
233 do_test temptable-4.9 {
235 CREATE TABLE t2(x unique, y);
236 INSERT INTO t2 VALUES(3,4);
240 do_test temptable-4.10.1 {
245 # Update: The schema is reloaded in test temptable-4.10.1. And tclsqlite.c
246 # handles it and retries the query anyway.
247 # do_test temptable-4.10.2 {
249 # SELECT name FROM sqlite_master WHERE type='table'
251 # } {1 {database schema has changed}}
252 do_test temptable-4.10.3 {
254 SELECT name FROM sqlite_master WHERE type='table'
257 do_test temptable-4.11 {
262 do_test temptable-4.12 {
267 do_test temptable-4.13 {
269 DROP TABLE t2; -- drops TEMP.T2
270 SELECT * FROM t2; -- uses MAIN.T2
273 do_test temptable-4.14 {
278 do_test temptable-4.15 {
280 sqlite3 db2 ./test.db
286 # Now create a temporary table in db2 and a permanent index in db. The
287 # temporary table in db2 should mask the name of the permanent index,
288 # but the permanent index should still be accessible and should still
289 # be updated when its corresponding table changes.
291 do_test temptable-5.1 {
293 CREATE TEMP TABLE mask(a,b,c)
295 if {[permutation]=="prepare"} { db2 cache flush }
297 CREATE INDEX mask ON t2(x);
301 #do_test temptable-5.2 {
305 #} {1 {database schema has changed}}
306 do_test temptable-5.3 {
311 do_test temptable-5.4 {
313 SELECT y FROM t2 WHERE x=3
316 do_test temptable-5.5 {
318 SELECT y FROM t2 WHERE x=3
321 do_test temptable-5.6 {
323 INSERT INTO t2 VALUES(1,2);
324 SELECT y FROM t2 WHERE x=1;
327 do_test temptable-5.7 {
329 SELECT y FROM t2 WHERE x=3
332 do_test temptable-5.8 {
334 SELECT y FROM t2 WHERE x=1;
337 do_test temptable-5.9 {
339 SELECT y FROM t2 WHERE x=3
345 # Test for correct operation of read-only databases
347 do_test temptable-6.1 {
350 INSERT INTO t8 VALUES('xyzzy');
354 do_test temptable-6.2 {
356 catch {file attributes test.db -permissions 0444}
357 catch {file attributes test.db -readonly 1}
359 if {[file writable test.db]} {
360 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
366 do_test temptable-6.3 {
367 if {[file writable test.db]} {
368 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
371 CREATE TABLE t9(x,y);
373 } {1 {attempt to write a readonly database}}
374 do_test temptable-6.4 {
376 CREATE TEMP TABLE t9(x,y);
379 do_test temptable-6.5 {
381 INSERT INTO t9 VALUES(1,2);
385 do_test temptable-6.6 {
386 if {[file writable test.db]} {
387 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
390 INSERT INTO t8 VALUES('hello');
393 } {1 {attempt to write a readonly database}}
394 do_test temptable-6.7 {
399 do_test temptable-6.8 {
405 } {1 {no such table: t9}}
407 forcedelete test2.db test2.db-journal
409 do_test temptable-7.1 {
411 ATTACH 'test2.db' AS two;
412 CREATE TEMP TABLE two.abc(x,y);
414 } {1 {temporary table name must be unqualified}}
417 # Need to do the following for tcl 8.5 on mac. On that configuration, the
418 # -readonly flag is taken so seriously that a subsequent [forcedelete]
419 # (required before the next test file can be executed) will fail.
421 catch {file attributes test.db -readonly 0}
423 do_test temptable-8.0 {
425 catch {forcedelete test.db}
428 do_test temptable-8.1 {
429 execsql { CREATE TEMP TABLE tbl2(a, b); }
431 CREATE TABLE tbl(a, b);
432 INSERT INTO tbl VALUES(1, 2);
434 execsql {SELECT * FROM tbl}
436 do_test temptable-8.2 {
437 execsql { CREATE TEMP TABLE tbl(a, b); }
438 execsql {SELECT * FROM tbl}