2001-10-08 13:22:32 +00:00
# 2001 October 7
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library.
#
# This file implements tests for temporary tables and indices.
#
2009-06-16 17:49:36 +00:00
# $Id: temptable.test,v 1.21 2009/06/16 17:49:36 drh Exp $
2001-10-08 13:22:32 +00:00
set testdir [file dirname $argv0]
source $testdir/tester.tcl
2005-03-29 03:10:59 +00:00
ifcapable !tempdb {
finish_test
return
}
2001-10-08 13:22:32 +00:00
# Create an alternative connection to the database
#
do_test temptable-1.0 {
2004-06-19 00:16:31 +00:00
sqlite3 db2 ./test.db
2002-05-10 13:14:07 +00:00
set dummy {}
2001-10-08 13:22:32 +00:00
} {}
# Create a permanent table.
#
do_test temptable-1.1 {
execsql {CREATE TABLE t1(a,b,c);}
execsql {INSERT INTO t1 VALUES(1,2,3);}
execsql {SELECT * FROM t1}
} {1 2 3}
do_test temptable-1.2 {
catch {db2 eval {SELECT * FROM sqlite_master}}
db2 eval {SELECT * FROM t1}
} {1 2 3}
2001-10-09 04:19:46 +00:00
do_test temptable-1.3 {
2001-10-08 13:22:32 +00:00
execsql {SELECT name FROM sqlite_master}
} {t1}
2001-10-09 04:19:46 +00:00
do_test temptable-1.4 {
2001-10-08 13:22:32 +00:00
db2 eval {SELECT name FROM sqlite_master}
} {t1}
# Create a temporary table. Verify that only one of the two
# processes can see it.
#
2001-10-09 04:19:46 +00:00
do_test temptable-1.5 {
2001-10-08 13:22:32 +00:00
db2 eval {
CREATE TEMP TABLE t2(x,y,z);
INSERT INTO t2 VALUES(4,5,6);
}
db2 eval {SELECT * FROM t2}
} {4 5 6}
2001-10-09 04:19:46 +00:00
do_test temptable-1.6 {
2001-10-08 13:22:32 +00:00
catch {execsql {SELECT * FROM sqlite_master}}
catchsql {SELECT * FROM t2}
} {1 {no such table: t2}}
2001-10-09 04:19:46 +00:00
do_test temptable-1.7 {
2001-10-08 13:22:32 +00:00
catchsql {INSERT INTO t2 VALUES(8,9,0);}
} {1 {no such table: t2}}
2001-10-09 04:19:46 +00:00
do_test temptable-1.8 {
2001-10-08 13:22:32 +00:00
db2 eval {INSERT INTO t2 VALUES(8,9,0);}
db2 eval {SELECT * FROM t2 ORDER BY x}
} {4 5 6 8 9 0}
2001-10-09 04:19:46 +00:00
do_test temptable-1.9 {
2001-10-08 13:22:32 +00:00
db2 eval {DELETE FROM t2 WHERE x==8}
db2 eval {SELECT * FROM t2 ORDER BY x}
} {4 5 6}
2001-10-09 04:19:46 +00:00
do_test temptable-1.10 {
2001-10-08 13:22:32 +00:00
db2 eval {DELETE FROM t2}
db2 eval {SELECT * FROM t2}
} {}
2001-10-09 04:19:46 +00:00
do_test temptable-1.11 {
2001-10-08 13:22:32 +00:00
db2 eval {
INSERT INTO t2 VALUES(7,6,5);
INSERT INTO t2 VALUES(4,3,2);
SELECT * FROM t2 ORDER BY x;
}
} {4 3 2 7 6 5}
2001-10-09 04:19:46 +00:00
do_test temptable-1.12 {
2001-10-08 13:22:32 +00:00
db2 eval {DROP TABLE t2;}
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {no such table: t2}}
# Make sure temporary tables work with transactions
#
2001-10-09 04:19:46 +00:00
do_test temptable-2.1 {
2001-10-08 13:22:32 +00:00
execsql {
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t2(x,y);
INSERT INTO t2 VALUES(1,2);
SELECT * FROM t2;
}
} {1 2}
2001-10-09 04:19:46 +00:00
do_test temptable-2.2 {
2001-10-08 13:22:32 +00:00
execsql {ROLLBACK}
catchsql {SELECT * FROM t2}
} {1 {no such table: t2}}
2001-10-09 04:19:46 +00:00
do_test temptable-2.3 {
2001-10-08 13:22:32 +00:00
execsql {
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t2(x,y);
INSERT INTO t2 VALUES(1,2);
SELECT * FROM t2;
}
} {1 2}
2001-10-09 04:19:46 +00:00
do_test temptable-2.4 {
2001-10-08 13:22:32 +00:00
execsql {COMMIT}
catchsql {SELECT * FROM t2}
} {0 {1 2}}
2001-10-09 04:19:46 +00:00
do_test temptable-2.5 {
2001-10-08 13:22:32 +00:00
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {no such table: t2}}
2001-10-09 04:19:46 +00:00
# Make sure indices on temporary tables are also temporary.
#
do_test temptable-3.1 {
execsql {
CREATE INDEX i2 ON t2(x);
SELECT name FROM sqlite_master WHERE type='index';
}
} {}
do_test temptable-3.2 {
execsql {
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-3.3 {
execsql {
DROP INDEX i2;
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-3.4 {
execsql {
CREATE INDEX i2 ON t2(x);
DROP TABLE t2;
}
catchsql {DROP INDEX i2}
} {1 {no such index: i2}}
2001-10-08 13:22:32 +00:00
# Check for correct name collision processing. A name collision can
# occur when process A creates a temporary table T then process B
# creates a permanent table also named T. The temp table in process A
2013-03-21 21:20:32 +00:00
# hides the existence of the permanent table.
2001-10-08 13:22:32 +00:00
#
2001-10-09 04:19:46 +00:00
do_test temptable-4.1 {
2002-01-10 14:31:48 +00:00
execsql {
2001-10-09 04:19:46 +00:00
CREATE TEMP TABLE t2(x,y);
INSERT INTO t2 VALUES(10,20);
SELECT * FROM t2;
2002-01-10 14:31:48 +00:00
} db2
2001-10-09 04:19:46 +00:00
} {10 20}
do_test temptable-4.2 {
execsql {
CREATE TABLE t2(x,y,z);
INSERT INTO t2 VALUES(9,8,7);
SELECT * FROM t2;
}
} {9 8 7}
do_test temptable-4.3 {
2002-01-10 14:31:48 +00:00
catchsql {
SELECT * FROM t2;
} db2
2003-05-17 17:35:10 +00:00
} {0 {10 20}}
2003-03-30 00:19:49 +00:00
do_test temptable-4.4.1 {
catchsql {
SELECT * FROM temp.t2;
} db2
} {0 {10 20}}
do_test temptable-4.4.2 {
catchsql {
SELECT * FROM main.t2;
} db2
2009-06-16 17:49:36 +00:00
} {0 {9 8 7}}
2004-02-14 16:31:02 +00:00
#do_test temptable-4.4.3 {
# catchsql {
# SELECT name FROM main.sqlite_master WHERE type='table';
# } db2
#} {1 {database schema has changed}}
2003-05-17 17:35:10 +00:00
do_test temptable-4.4.4 {
catchsql {
SELECT name FROM main.sqlite_master WHERE type='table';
} db2
} {0 {t1 t2}}
do_test temptable-4.4.5 {
catchsql {
SELECT * FROM main.t2;
} db2
} {0 {9 8 7}}
do_test temptable-4.4.6 {
2003-03-30 00:19:49 +00:00
# TEMP takes precedence over MAIN
2002-01-10 14:31:48 +00:00
catchsql {
SELECT * FROM t2;
} db2
2001-10-09 04:19:46 +00:00
} {0 {10 20}}
do_test temptable-4.5 {
2002-01-10 14:31:48 +00:00
catchsql {
2003-03-30 00:19:49 +00:00
DROP TABLE t2; -- should drop TEMP
SELECT * FROM t2; -- data should be from MAIN
2002-01-10 14:31:48 +00:00
} db2
2003-03-30 00:19:49 +00:00
} {0 {9 8 7}}
2001-10-09 04:19:46 +00:00
do_test temptable-4.6 {
db2 close
2004-06-19 00:16:31 +00:00
sqlite3 db2 ./test.db
2002-01-10 14:31:48 +00:00
catchsql {
SELECT * FROM t2;
} db2
2001-10-09 04:19:46 +00:00
} {0 {9 8 7}}
2002-01-10 14:31:48 +00:00
do_test temptable-4.7 {
catchsql {
DROP TABLE t2;
SELECT * FROM t2;
}
} {1 {no such table: t2}}
do_test temptable-4.8 {
db2 close
2004-06-19 00:16:31 +00:00
sqlite3 db2 ./test.db
2002-01-10 14:31:48 +00:00
execsql {
CREATE TEMP TABLE t2(x unique,y);
INSERT INTO t2 VALUES(1,2);
SELECT * FROM t2;
} db2
} {1 2}
do_test temptable-4.9 {
execsql {
CREATE TABLE t2(x unique, y);
INSERT INTO t2 VALUES(3,4);
SELECT * FROM t2;
}
} {3 4}
2003-05-17 17:35:10 +00:00
do_test temptable-4.10.1 {
2002-01-10 14:31:48 +00:00
catchsql {
SELECT * FROM t2;
} db2
2003-05-17 17:35:10 +00:00
} {0 {1 2}}
2004-06-14 08:26:35 +00:00
# Update: The schema is reloaded in test temptable-4.10.1. And tclsqlite.c
# handles it and retries the query anyway.
# do_test temptable-4.10.2 {
# catchsql {
# SELECT name FROM sqlite_master WHERE type='table'
# } db2
# } {1 {database schema has changed}}
2003-05-17 17:35:10 +00:00
do_test temptable-4.10.3 {
catchsql {
SELECT name FROM sqlite_master WHERE type='table'
} db2
} {0 {t1 t2}}
2002-01-10 14:31:48 +00:00
do_test temptable-4.11 {
execsql {
SELECT * FROM t2;
} db2
} {1 2}
do_test temptable-4.12 {
execsql {
SELECT * FROM t2;
}
} {3 4}
do_test temptable-4.13 {
catchsql {
2003-03-30 00:19:49 +00:00
DROP TABLE t2; -- drops TEMP.T2
SELECT * FROM t2; -- uses MAIN.T2
2002-01-10 14:31:48 +00:00
} db2
2003-03-30 00:19:49 +00:00
} {0 {3 4}}
2002-01-10 14:31:48 +00:00
do_test temptable-4.14 {
execsql {
SELECT * FROM t2;
}
} {3 4}
do_test temptable-4.15 {
db2 close
2004-06-19 00:16:31 +00:00
sqlite3 db2 ./test.db
2002-01-10 14:31:48 +00:00
execsql {
SELECT * FROM t2;
} db2
} {3 4}
2001-10-09 04:19:46 +00:00
# Now create a temporary table in db2 and a permanent index in db. The
# temporary table in db2 should mask the name of the permanent index,
# but the permanent index should still be accessible and should still
2002-01-10 14:31:48 +00:00
# be updated when its corresponding table changes.
2001-10-09 04:19:46 +00:00
#
do_test temptable-5.1 {
2002-01-10 14:31:48 +00:00
execsql {
CREATE TEMP TABLE mask(a,b,c)
} db2
2011-06-27 16:55:50 +00:00
if {[permutation]=="prepare"} { db2 cache flush }
2001-10-09 04:19:46 +00:00
execsql {
CREATE INDEX mask ON t2(x);
SELECT * FROM t2;
}
2002-01-10 14:31:48 +00:00
} {3 4}
2004-02-14 16:31:02 +00:00
#do_test temptable-5.2 {
# catchsql {
# SELECT * FROM t2;
# } db2
#} {1 {database schema has changed}}
2001-10-09 04:19:46 +00:00
do_test temptable-5.3 {
2002-01-10 14:31:48 +00:00
catchsql {
SELECT * FROM t2;
} db2
} {0 {3 4}}
2001-10-09 04:19:46 +00:00
do_test temptable-5.4 {
2002-01-10 14:31:48 +00:00
execsql {
SELECT y FROM t2 WHERE x=3
}
} {4}
2001-10-09 04:19:46 +00:00
do_test temptable-5.5 {
2002-01-10 14:31:48 +00:00
execsql {
SELECT y FROM t2 WHERE x=3
} db2
} {4}
2001-10-09 04:19:46 +00:00
do_test temptable-5.6 {
2002-01-10 14:31:48 +00:00
execsql {
INSERT INTO t2 VALUES(1,2);
2001-10-09 04:19:46 +00:00
SELECT y FROM t2 WHERE x=1;
2002-01-10 14:31:48 +00:00
} db2
2001-10-09 04:19:46 +00:00
} {2}
do_test temptable-5.7 {
2002-01-10 14:31:48 +00:00
execsql {
SELECT y FROM t2 WHERE x=3
} db2
} {4}
2001-10-09 04:19:46 +00:00
do_test temptable-5.8 {
execsql {
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-5.9 {
2002-01-10 14:31:48 +00:00
execsql {
SELECT y FROM t2 WHERE x=3
}
} {4}
2001-10-08 13:22:32 +00:00
2001-10-09 12:39:24 +00:00
db2 close
2002-06-06 23:16:05 +00:00
# Test for correct operation of read-only databases
#
do_test temptable-6.1 {
execsql {
CREATE TABLE t8(x);
INSERT INTO t8 VALUES('xyzzy');
SELECT * FROM t8;
}
} {xyzzy}
do_test temptable-6.2 {
db close
catch {file attributes test.db -permissions 0444}
catch {file attributes test.db -readonly 1}
2004-06-19 00:16:31 +00:00
sqlite3 db test.db
2002-09-02 12:14:50 +00:00
if {[file writable test.db]} {
error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
}
2002-06-06 23:16:05 +00:00
execsql {
SELECT * FROM t8;
}
} {xyzzy}
do_test temptable-6.3 {
2002-09-02 12:14:50 +00:00
if {[file writable test.db]} {
error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
}
2002-06-06 23:16:05 +00:00
catchsql {
CREATE TABLE t9(x,y);
}
} {1 {attempt to write a readonly database}}
do_test temptable-6.4 {
catchsql {
CREATE TEMP TABLE t9(x,y);
}
} {0 {}}
do_test temptable-6.5 {
catchsql {
INSERT INTO t9 VALUES(1,2);
SELECT * FROM t9;
}
} {0 {1 2}}
do_test temptable-6.6 {
2002-09-02 12:14:50 +00:00
if {[file writable test.db]} {
error "Unable to make the database file test.db readonly - rerun this test as an unprivileged user"
}
2002-06-06 23:16:05 +00:00
catchsql {
INSERT INTO t8 VALUES('hello');
SELECT * FROM t8;
}
} {1 {attempt to write a readonly database}}
do_test temptable-6.7 {
catchsql {
SELECT * FROM t8,t9;
}
} {0 {xyzzy 1 2}}
do_test temptable-6.8 {
db close
2004-06-19 00:16:31 +00:00
sqlite3 db test.db
2002-06-06 23:16:05 +00:00
catchsql {
SELECT * FROM t8,t9;
}
} {1 {no such table: t9}}
2011-08-02 00:57:34 +00:00
forcedelete test2.db test2.db-journal
2007-10-09 08:29:32 +00:00
ifcapable attach {
do_test temptable-7.1 {
catchsql {
ATTACH 'test2.db' AS two;
CREATE TEMP TABLE two.abc(x,y);
}
} {1 {temporary table name must be unqualified}}
}
2005-08-20 03:03:04 +00:00
2007-10-05 15:53:29 +00:00
# Need to do the following for tcl 8.5 on mac. On that configuration, the
2011-08-02 00:57:34 +00:00
# -readonly flag is taken so seriously that a subsequent [forcedelete]
2007-10-05 15:53:29 +00:00
# (required before the next test file can be executed) will fail.
#
catch {file attributes test.db -readonly 0}
2009-02-10 11:17:43 +00:00
do_test temptable-8.0 {
db close
2011-08-02 00:57:34 +00:00
catch {forcedelete test.db}
2009-02-10 11:17:43 +00:00
sqlite3 db test.db
} {}
do_test temptable-8.1 {
execsql { CREATE TEMP TABLE tbl2(a, b); }
execsql {
CREATE TABLE tbl(a, b);
INSERT INTO tbl VALUES(1, 2);
}
execsql {SELECT * FROM tbl}
} {1 2}
do_test temptable-8.2 {
execsql { CREATE TEMP TABLE tbl(a, b); }
execsql {SELECT * FROM tbl}
} {}
2001-10-08 13:22:32 +00:00
finish_test