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.15 2005/03/29 03:11:00 danielk1977 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 existance 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;
183 } {1 {no such table: 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)
296 CREATE INDEX mask ON t2(x);
300 #do_test temptable-5.2 {
304 #} {1 {database schema has changed}}
305 do_test temptable-5.3 {
310 do_test temptable-5.4 {
312 SELECT y FROM t2 WHERE x=3
315 do_test temptable-5.5 {
317 SELECT y FROM t2 WHERE x=3
320 do_test temptable-5.6 {
322 INSERT INTO t2 VALUES(1,2);
323 SELECT y FROM t2 WHERE x=1;
326 do_test temptable-5.7 {
328 SELECT y FROM t2 WHERE x=3
331 do_test temptable-5.8 {
333 SELECT y FROM t2 WHERE x=1;
336 do_test temptable-5.9 {
338 SELECT y FROM t2 WHERE x=3
344 # Test for correct operation of read-only databases
346 do_test temptable-6.1 {
349 INSERT INTO t8 VALUES('xyzzy');
353 do_test temptable-6.2 {
355 catch {file attributes test.db -permissions 0444}
356 catch {file attributes test.db -readonly 1}
358 if {[file writable test.db]} {
359 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
365 do_test temptable-6.3 {
366 if {[file writable test.db]} {
367 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
370 CREATE TABLE t9(x,y);
372 } {1 {attempt to write a readonly database}}
373 do_test temptable-6.4 {
375 CREATE TEMP TABLE t9(x,y);
378 do_test temptable-6.5 {
380 INSERT INTO t9 VALUES(1,2);
384 do_test temptable-6.6 {
385 if {[file writable test.db]} {
386 error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
389 INSERT INTO t8 VALUES('hello');
392 } {1 {attempt to write a readonly database}}
393 do_test temptable-6.7 {
398 do_test temptable-6.8 {
404 } {1 {no such table: t9}}