2005-12-30 19:28:01 +03:00
|
|
|
# 2005 December 30
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
#***********************************************************************
|
|
|
|
#
|
2009-03-16 16:19:36 +03:00
|
|
|
# $Id: shared.test,v 1.36 2009/03/16 13:19:36 danielk1977 Exp $
|
2005-12-30 19:28:01 +03:00
|
|
|
|
|
|
|
set testdir [file dirname $argv0]
|
|
|
|
source $testdir/tester.tcl
|
|
|
|
db close
|
|
|
|
|
2007-10-09 12:29:32 +04:00
|
|
|
# These tests cannot be run without the ATTACH command.
|
|
|
|
#
|
|
|
|
ifcapable !shared_cache||!attach {
|
2005-12-30 19:28:01 +03:00
|
|
|
finish_test
|
|
|
|
return
|
|
|
|
}
|
2006-01-16 15:46:41 +03:00
|
|
|
|
2006-01-05 14:34:32 +03:00
|
|
|
set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
|
2005-12-30 19:28:01 +03:00
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
foreach av [list 0 1] {
|
|
|
|
|
2006-01-16 18:14:27 +03:00
|
|
|
# Open the database connection and execute the auto-vacuum pragma
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
2006-01-16 18:14:27 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
|
|
|
|
ifcapable autovacuum {
|
|
|
|
do_test shared-[expr $av+1].1.0 {
|
|
|
|
execsql "pragma auto_vacuum=$::av"
|
|
|
|
execsql {pragma auto_vacuum}
|
|
|
|
} "$av"
|
|
|
|
} else {
|
|
|
|
if {$av} {
|
|
|
|
db close
|
|
|
|
break
|
|
|
|
}
|
2006-01-16 15:46:41 +03:00
|
|
|
}
|
|
|
|
|
2008-11-21 03:10:35 +03:00
|
|
|
# if we're using proxy locks, we use 2 filedescriptors for a db
|
|
|
|
# that is open but NOT yet locked, after a lock is taken we'll have 3,
|
|
|
|
# normally sqlite uses 1 (proxy locking adds the conch and the local lock)
|
|
|
|
set using_proxy 0
|
|
|
|
foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
|
|
|
|
set using_proxy $value
|
|
|
|
}
|
|
|
|
set extrafds_prelock 0
|
|
|
|
set extrafds_postlock 0
|
|
|
|
if {$using_proxy>0} {
|
|
|
|
set extrafds_prelock 1
|
|
|
|
set extrafds_postlock 2
|
|
|
|
}
|
|
|
|
|
2006-01-19 10:18:14 +03:00
|
|
|
# $av is currently 0 if this loop iteration is to test with auto-vacuum turned
|
|
|
|
# off, and 1 if it is turned on. Increment it so that (1 -> no auto-vacuum)
|
|
|
|
# and (2 -> auto-vacuum). The sole reason for this is so that it looks nicer
|
|
|
|
# when we use this variable as part of test-case names.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
incr av
|
|
|
|
|
2005-12-30 19:28:01 +03:00
|
|
|
# Test organization:
|
|
|
|
#
|
|
|
|
# shared-1.*: Simple test to verify basic sanity of table level locking when
|
|
|
|
# two connections share a pager cache.
|
|
|
|
# shared-2.*: Test that a read transaction can co-exist with a
|
|
|
|
# write-transaction, including a simple test to ensure the
|
|
|
|
# external locking protocol is still working.
|
2006-01-05 14:34:32 +03:00
|
|
|
# shared-3.*: Simple test of read-uncommitted mode.
|
2006-01-06 09:33:12 +03:00
|
|
|
# shared-4.*: Check that the schema is locked and unlocked correctly.
|
2006-01-06 18:03:48 +03:00
|
|
|
# shared-5.*: Test that creating/dropping schema items works when databases
|
|
|
|
# are attached in different orders to different handles.
|
2006-01-07 16:21:04 +03:00
|
|
|
# shared-6.*: Locking, UNION ALL queries and sub-queries.
|
2006-01-09 19:12:04 +03:00
|
|
|
# shared-7.*: Autovacuum and shared-cache.
|
2006-01-19 11:43:31 +03:00
|
|
|
# shared-8.*: Tests related to the text encoding of shared-cache databases.
|
|
|
|
# shared-9.*: TEMP triggers and shared-cache databases.
|
|
|
|
# shared-10.*: Tests of sqlite3_close().
|
2006-01-23 08:50:58 +03:00
|
|
|
# shared-11.*: Test transaction locking.
|
2006-01-06 09:33:12 +03:00
|
|
|
#
|
2005-12-30 19:28:01 +03:00
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.1 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Open a second database on the file test.db. It should use the same pager
|
|
|
|
# cache and schema as the original connection. Verify that only 1 file is
|
|
|
|
# opened.
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
set ::sqlite_open_file_count
|
2008-11-21 03:10:35 +03:00
|
|
|
expr $sqlite_open_file_count-$extrafds_postlock
|
2005-12-30 19:28:01 +03:00
|
|
|
} {1}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.2 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Add a table and a single row of data via the first connection.
|
|
|
|
# Ensure that the second connection can see them.
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE abc(a, b, c);
|
|
|
|
INSERT INTO abc VALUES(1, 2, 3);
|
|
|
|
} db
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM abc;
|
|
|
|
} db2
|
|
|
|
} {1 2 3}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.3 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Have the first connection begin a transaction and obtain a read-lock
|
|
|
|
# on table abc. This should not prevent the second connection from
|
|
|
|
# querying abc.
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM abc;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM abc;
|
|
|
|
} db2
|
|
|
|
} {1 2 3}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.4 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Try to insert a row into abc via connection 2. This should fail because
|
|
|
|
# of the read-lock connection 1 is holding on table abc (obtained in the
|
|
|
|
# previous test case).
|
|
|
|
catchsql {
|
|
|
|
INSERT INTO abc VALUES(4, 5, 6);
|
|
|
|
} db2
|
2006-01-07 16:21:04 +03:00
|
|
|
} {1 {database table is locked: abc}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.5 {
|
2006-01-05 14:34:32 +03:00
|
|
|
# Using connection 2 (the one without the open transaction), try to create
|
|
|
|
# a new table. This should fail because of the open read transaction
|
|
|
|
# held by connection 1.
|
|
|
|
catchsql {
|
2005-12-30 19:28:01 +03:00
|
|
|
CREATE TABLE def(d, e, f);
|
|
|
|
} db2
|
2006-01-07 16:21:04 +03:00
|
|
|
} {1 {database table is locked: sqlite_master}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.6 {
|
2006-01-05 14:34:32 +03:00
|
|
|
# Upgrade connection 1's transaction to a write transaction. Create
|
|
|
|
# a new table - def - and insert a row into it. Because the connection 1
|
|
|
|
# transaction modifies the schema, it should not be possible for
|
|
|
|
# connection 2 to access the database at all until the connection 1
|
|
|
|
# has finished the transaction.
|
2005-12-30 19:28:01 +03:00
|
|
|
execsql {
|
2006-01-05 14:34:32 +03:00
|
|
|
CREATE TABLE def(d, e, f);
|
2005-12-30 19:28:01 +03:00
|
|
|
INSERT INTO def VALUES('IV', 'V', 'VI');
|
|
|
|
}
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.7 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Read from the sqlite_master table with connection 1 (inside the
|
2006-01-05 14:34:32 +03:00
|
|
|
# transaction). Then test that we can not do this with connection 2. This
|
|
|
|
# is because of the schema-modified lock established by connection 1
|
|
|
|
# in the previous test case.
|
2005-12-30 19:28:01 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM sqlite_master;
|
|
|
|
}
|
|
|
|
catchsql {
|
|
|
|
SELECT * FROM sqlite_master;
|
|
|
|
} db2
|
2006-01-06 16:00:28 +03:00
|
|
|
} {1 {database schema is locked: main}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.1.8 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Commit the connection 1 transaction.
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.2.1 {
|
2020-11-16 21:45:21 +03:00
|
|
|
# Open connection db3 to the database.
|
2006-01-24 00:38:03 +03:00
|
|
|
if {$::tcl_platform(platform)=="unix"} {
|
2020-11-16 21:45:21 +03:00
|
|
|
sqlite3 db3 "file:test.db?cache=private" -uri 1
|
2006-01-24 00:38:03 +03:00
|
|
|
} else {
|
|
|
|
sqlite3 db3 TEST.DB
|
|
|
|
}
|
2005-12-30 19:28:01 +03:00
|
|
|
set ::sqlite_open_file_count
|
2008-11-21 03:10:35 +03:00
|
|
|
expr $sqlite_open_file_count-($extrafds_prelock+$extrafds_postlock)
|
2005-12-30 19:28:01 +03:00
|
|
|
} {2}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.2.2 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Start read transactions on db and db2 (the shared pager cache). Ensure
|
|
|
|
# db3 cannot write to the database.
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM abc;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM abc;
|
|
|
|
} db2
|
|
|
|
catchsql {
|
|
|
|
INSERT INTO abc VALUES(1, 2, 3);
|
|
|
|
} db2
|
2006-01-07 16:21:04 +03:00
|
|
|
} {1 {database table is locked: abc}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.2.3 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Turn db's transaction into a write-transaction. db3 should still be
|
|
|
|
# able to read from table def (but will not see the new row). Connection
|
|
|
|
# db2 should not be able to read def (because of the write-lock).
|
|
|
|
|
|
|
|
# Todo: The failed "INSERT INTO abc ..." statement in the above test
|
|
|
|
# has started a write-transaction on db2 (should this be so?). This
|
|
|
|
# would prevent connection db from starting a write-transaction. So roll the
|
|
|
|
# db2 transaction back and replace it with a new read transaction.
|
|
|
|
execsql {
|
|
|
|
ROLLBACK;
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM abc;
|
|
|
|
} db2
|
|
|
|
|
|
|
|
execsql {
|
|
|
|
INSERT INTO def VALUES('VII', 'VIII', 'IX');
|
|
|
|
}
|
|
|
|
concat [
|
|
|
|
catchsql { SELECT * FROM def; } db3
|
|
|
|
] [
|
|
|
|
catchsql { SELECT * FROM def; } db2
|
|
|
|
]
|
2006-01-07 16:21:04 +03:00
|
|
|
} {0 {IV V VI} 1 {database table is locked: def}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.2.4 {
|
2005-12-30 19:28:01 +03:00
|
|
|
# Commit the open transaction on db. db2 still holds a read-transaction.
|
|
|
|
# This should prevent db3 from writing to the database, but not from
|
|
|
|
# reading.
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
concat [
|
|
|
|
catchsql { SELECT * FROM def; } db3
|
|
|
|
] [
|
|
|
|
catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
|
|
|
|
]
|
2006-01-05 14:34:32 +03:00
|
|
|
} {0 {IV V VI VII VIII IX} 1 {database is locked}}
|
|
|
|
|
|
|
|
catchsql COMMIT db2
|
2005-12-30 19:28:01 +03:00
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.3.1.1 {
|
2006-01-05 14:34:32 +03:00
|
|
|
# This test case starts a linear scan of table 'seq' using a
|
|
|
|
# read-uncommitted connection. In the middle of the scan, rows are added
|
|
|
|
# to the end of the seq table (ahead of the current cursor position).
|
|
|
|
# The uncommitted rows should be included in the results of the scan.
|
|
|
|
execsql "
|
2006-01-19 10:18:14 +03:00
|
|
|
CREATE TABLE seq(i PRIMARY KEY, x);
|
2006-01-05 14:34:32 +03:00
|
|
|
INSERT INTO seq VALUES(1, '[string repeat X 500]');
|
|
|
|
INSERT INTO seq VALUES(2, '[string repeat X 500]');
|
|
|
|
"
|
|
|
|
execsql {SELECT * FROM sqlite_master} db2
|
|
|
|
execsql {PRAGMA read_uncommitted = 1} db2
|
|
|
|
|
|
|
|
set ret [list]
|
2006-01-19 10:18:14 +03:00
|
|
|
db2 eval {SELECT i FROM seq ORDER BY i} {
|
2006-01-05 14:34:32 +03:00
|
|
|
if {$i < 4} {
|
2006-01-14 11:02:28 +03:00
|
|
|
set max [execsql {SELECT max(i) FROM seq}]
|
|
|
|
db eval {
|
2006-01-17 12:35:01 +03:00
|
|
|
INSERT INTO seq SELECT i + :max, x FROM seq;
|
2006-01-05 14:34:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
lappend ret $i
|
|
|
|
}
|
|
|
|
set ret
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.3.1.2 {
|
2006-01-05 14:34:32 +03:00
|
|
|
# Another linear scan through table seq using a read-uncommitted connection.
|
|
|
|
# This time, delete each row as it is read. Should not affect the results of
|
|
|
|
# the scan, but the table should be empty after the scan is concluded
|
|
|
|
# (test 3.1.3 verifies this).
|
|
|
|
set ret [list]
|
|
|
|
db2 eval {SELECT i FROM seq} {
|
2006-01-17 12:35:01 +03:00
|
|
|
db eval {DELETE FROM seq WHERE i = :i}
|
2006-01-05 14:34:32 +03:00
|
|
|
lappend ret $i
|
|
|
|
}
|
|
|
|
set ret
|
|
|
|
} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.3.1.3 {
|
2006-01-05 14:34:32 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM seq;
|
|
|
|
}
|
|
|
|
} {}
|
2005-12-30 19:28:01 +03:00
|
|
|
|
|
|
|
catch {db close}
|
|
|
|
catch {db2 close}
|
|
|
|
catch {db3 close}
|
|
|
|
|
2006-01-06 09:33:12 +03:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# Tests shared-4.* test that the schema locking rules are applied
|
|
|
|
# correctly. i.e.:
|
|
|
|
#
|
|
|
|
# 1. All transactions require a read-lock on the schemas of databases they
|
|
|
|
# access.
|
|
|
|
# 2. Transactions that modify a database schema require a write-lock on that
|
|
|
|
# schema.
|
|
|
|
# 3. It is not possible to compile a statement while another handle has a
|
|
|
|
# write-lock on the schema.
|
|
|
|
#
|
|
|
|
|
|
|
|
# Open two database handles db and db2. Each has a single attach database
|
|
|
|
# (as well as main):
|
|
|
|
#
|
|
|
|
# db.main -> ./test.db
|
|
|
|
# db.test2 -> ./test2.db
|
|
|
|
# db2.main -> ./test2.db
|
|
|
|
# db2.test -> ./test.db
|
|
|
|
#
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
|
|
|
forcedelete test2.db
|
|
|
|
forcedelete test2.db-journal
|
2006-01-06 09:33:12 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test2.db
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.1.1 {
|
2006-01-06 09:33:12 +03:00
|
|
|
set sqlite_open_file_count
|
2008-11-21 03:10:35 +03:00
|
|
|
expr $sqlite_open_file_count-($extrafds_prelock*2)
|
2006-01-06 09:33:12 +03:00
|
|
|
} {2}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.1.2 {
|
2006-01-06 09:33:12 +03:00
|
|
|
execsql {ATTACH 'test2.db' AS test2}
|
|
|
|
set sqlite_open_file_count
|
2008-11-21 03:10:35 +03:00
|
|
|
expr $sqlite_open_file_count-($extrafds_postlock*2)
|
2006-01-06 09:33:12 +03:00
|
|
|
} {2}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.1.3 {
|
2006-01-06 09:33:12 +03:00
|
|
|
execsql {ATTACH 'test.db' AS test} db2
|
|
|
|
set sqlite_open_file_count
|
2008-11-21 03:10:35 +03:00
|
|
|
expr $sqlite_open_file_count-($extrafds_postlock*2)
|
2006-01-06 09:33:12 +03:00
|
|
|
} {2}
|
|
|
|
|
2006-01-06 16:00:28 +03:00
|
|
|
# Sanity check: Create a table in ./test.db via handle db, and test that handle
|
|
|
|
# db2 can "see" the new table immediately. A handle using a seperate pager
|
|
|
|
# cache would have to reload the database schema before this were possible.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.2.1 {
|
2006-01-06 09:33:12 +03:00
|
|
|
execsql {
|
|
|
|
CREATE TABLE abc(a, b, c);
|
2006-01-06 16:00:28 +03:00
|
|
|
CREATE TABLE def(d, e, f);
|
2006-01-06 09:33:12 +03:00
|
|
|
INSERT INTO abc VALUES('i', 'ii', 'iii');
|
2006-01-06 16:00:28 +03:00
|
|
|
INSERT INTO def VALUES('I', 'II', 'III');
|
2006-01-06 09:33:12 +03:00
|
|
|
}
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.2.2 {
|
2006-01-06 09:33:12 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM test.abc;
|
|
|
|
} db2
|
|
|
|
} {i ii iii}
|
|
|
|
|
2006-01-06 16:00:28 +03:00
|
|
|
# Open a read-transaction and read from table abc via handle 2. Check that
|
|
|
|
# handle 1 can read table abc. Check that handle 1 cannot modify table abc
|
|
|
|
# or the database schema. Then check that handle 1 can modify table def.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.3.1 {
|
2006-01-06 16:00:28 +03:00
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM test.abc;
|
|
|
|
} db2
|
|
|
|
} {i ii iii}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.3.2 {
|
2006-01-06 16:00:28 +03:00
|
|
|
catchsql {
|
|
|
|
INSERT INTO abc VALUES('iv', 'v', 'vi');
|
|
|
|
}
|
2006-01-07 16:21:04 +03:00
|
|
|
} {1 {database table is locked: abc}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.3.3 {
|
2006-01-06 16:00:28 +03:00
|
|
|
catchsql {
|
|
|
|
CREATE TABLE ghi(g, h, i);
|
|
|
|
}
|
2006-01-07 16:21:04 +03:00
|
|
|
} {1 {database table is locked: sqlite_master}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.3.3 {
|
2006-01-06 16:00:28 +03:00
|
|
|
catchsql {
|
|
|
|
INSERT INTO def VALUES('IV', 'V', 'VI');
|
|
|
|
}
|
|
|
|
} {0 {}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.3.4 {
|
2006-01-06 16:00:28 +03:00
|
|
|
# Cleanup: commit the transaction opened by db2.
|
|
|
|
execsql {
|
|
|
|
COMMIT
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
|
|
|
|
# Open a write-transaction using handle 1 and modify the database schema.
|
|
|
|
# Then try to execute a compiled statement to read from the same
|
|
|
|
# database via handle 2 (fails to get the lock on sqlite_master). Also
|
|
|
|
# try to compile a read of the same database using handle 2 (also fails).
|
|
|
|
# Finally, compile a read of the other database using handle 2. This
|
|
|
|
# should also fail.
|
|
|
|
#
|
2006-01-16 19:24:25 +03:00
|
|
|
ifcapable compound {
|
|
|
|
do_test shared-$av.4.4.1.2 {
|
|
|
|
# Sanity check 1: Check that the schema is what we think it is when viewed
|
|
|
|
# via handle 1.
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE test2.ghi(g, h, i);
|
|
|
|
SELECT 'test.db:'||name FROM sqlite_master
|
|
|
|
UNION ALL
|
|
|
|
SELECT 'test2.db:'||name FROM test2.sqlite_master;
|
|
|
|
}
|
|
|
|
} {test.db:abc test.db:def test2.db:ghi}
|
|
|
|
do_test shared-$av.4.4.1.2 {
|
|
|
|
# Sanity check 2: Check that the schema is what we think it is when viewed
|
|
|
|
# via handle 2.
|
|
|
|
execsql {
|
|
|
|
SELECT 'test2.db:'||name FROM sqlite_master
|
|
|
|
UNION ALL
|
|
|
|
SELECT 'test.db:'||name FROM test.sqlite_master;
|
|
|
|
} db2
|
|
|
|
} {test2.db:ghi test.db:abc test.db:def}
|
|
|
|
}
|
2006-01-06 16:00:28 +03:00
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.4.2 {
|
2006-01-06 16:00:28 +03:00
|
|
|
set ::DB2 [sqlite3_connection_pointer db2]
|
|
|
|
set sql {SELECT * FROM abc}
|
|
|
|
set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE jkl(j, k, l);
|
|
|
|
}
|
|
|
|
sqlite3_step $::STMT1
|
|
|
|
} {SQLITE_ERROR}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.4.3 {
|
2006-01-06 16:00:28 +03:00
|
|
|
sqlite3_finalize $::STMT1
|
|
|
|
} {SQLITE_LOCKED}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.4.4 {
|
2006-01-06 16:00:28 +03:00
|
|
|
set rc [catch {
|
|
|
|
set ::STMT1 [sqlite3_prepare $::DB2 $sql -1 DUMMY]
|
|
|
|
} msg]
|
|
|
|
list $rc $msg
|
|
|
|
} {1 {(6) database schema is locked: test}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.4.4.5 {
|
2006-01-06 16:00:28 +03:00
|
|
|
set rc [catch {
|
|
|
|
set ::STMT1 [sqlite3_prepare $::DB2 "SELECT * FROM ghi" -1 DUMMY]
|
|
|
|
} msg]
|
|
|
|
list $rc $msg
|
|
|
|
} {1 {(6) database schema is locked: test}}
|
|
|
|
|
2006-01-06 18:03:48 +03:00
|
|
|
|
2006-01-06 09:33:12 +03:00
|
|
|
catch {db2 close}
|
|
|
|
catch {db close}
|
|
|
|
|
2006-01-06 18:03:48 +03:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# Tests shared-5.*
|
|
|
|
#
|
|
|
|
foreach db [list test.db test1.db test2.db test3.db] {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete $db ${db}-journal
|
2006-01-06 18:03:48 +03:00
|
|
|
}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.5.1.1 {
|
2006-01-06 18:03:48 +03:00
|
|
|
sqlite3 db1 test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
ATTACH 'test1.db' AS test1;
|
|
|
|
ATTACH 'test2.db' AS test2;
|
|
|
|
ATTACH 'test3.db' AS test3;
|
|
|
|
} db1
|
|
|
|
execsql {
|
|
|
|
ATTACH 'test3.db' AS test3;
|
|
|
|
ATTACH 'test2.db' AS test2;
|
|
|
|
ATTACH 'test1.db' AS test1;
|
|
|
|
} db2
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.5.1.2 {
|
2006-01-06 18:03:48 +03:00
|
|
|
execsql {
|
|
|
|
CREATE TABLE test1.t1(a, b);
|
|
|
|
CREATE INDEX test1.i1 ON t1(a, b);
|
|
|
|
} db1
|
2006-01-17 12:35:01 +03:00
|
|
|
} {}
|
|
|
|
ifcapable view {
|
|
|
|
do_test shared-$av.5.1.3 {
|
|
|
|
execsql {
|
|
|
|
CREATE VIEW test1.v1 AS SELECT * FROM t1;
|
|
|
|
} db1
|
|
|
|
} {}
|
|
|
|
}
|
|
|
|
ifcapable trigger {
|
|
|
|
do_test shared-$av.5.1.4 {
|
|
|
|
execsql {
|
|
|
|
CREATE TRIGGER test1.trig1 AFTER INSERT ON t1 BEGIN
|
|
|
|
INSERT INTO t1 VALUES(new.a, new.b);
|
|
|
|
END;
|
|
|
|
} db1
|
|
|
|
} {}
|
|
|
|
}
|
|
|
|
do_test shared-$av.5.1.5 {
|
2006-01-06 18:03:48 +03:00
|
|
|
execsql {
|
|
|
|
DROP INDEX i1;
|
2006-01-17 12:35:01 +03:00
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
ifcapable view {
|
|
|
|
do_test shared-$av.5.1.6 {
|
|
|
|
execsql {
|
|
|
|
DROP VIEW v1;
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
}
|
|
|
|
ifcapable trigger {
|
|
|
|
do_test shared-$av.5.1.7 {
|
|
|
|
execsql {
|
|
|
|
DROP TRIGGER trig1;
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
}
|
|
|
|
do_test shared-$av.5.1.8 {
|
|
|
|
execsql {
|
2006-01-06 18:03:48 +03:00
|
|
|
DROP TABLE t1;
|
|
|
|
} db2
|
|
|
|
} {}
|
2006-01-16 19:24:25 +03:00
|
|
|
ifcapable compound {
|
2006-01-17 12:35:01 +03:00
|
|
|
do_test shared-$av.5.1.9 {
|
2006-01-16 19:24:25 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM sqlite_master UNION ALL SELECT * FROM test1.sqlite_master
|
|
|
|
} db1
|
|
|
|
} {}
|
|
|
|
}
|
2006-01-06 18:03:48 +03:00
|
|
|
|
2006-01-07 16:21:04 +03:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# Tests shared-6.* test that a query obtains all the read-locks it needs
|
|
|
|
# before starting execution of the query. This means that there is no chance
|
|
|
|
# some rows of data will be returned before a lock fails and SQLITE_LOCK
|
|
|
|
# is returned.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.6.1.1 {
|
2006-01-07 16:21:04 +03:00
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(a, b);
|
|
|
|
CREATE TABLE t2(a, b);
|
|
|
|
INSERT INTO t1 VALUES(1, 2);
|
|
|
|
INSERT INTO t2 VALUES(3, 4);
|
|
|
|
} db1
|
2006-01-16 19:24:25 +03:00
|
|
|
} {}
|
|
|
|
ifcapable compound {
|
|
|
|
do_test shared-$av.6.1.2 {
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM t1 UNION ALL SELECT * FROM t2;
|
|
|
|
} db2
|
|
|
|
} {1 2 3 4}
|
|
|
|
}
|
|
|
|
do_test shared-$av.6.1.3 {
|
2006-01-07 16:21:04 +03:00
|
|
|
# Establish a write lock on table t2 via connection db2. Then make a
|
|
|
|
# UNION all query using connection db1 that first accesses t1, followed
|
|
|
|
# by t2. If the locks are grabbed at the start of the statement (as
|
|
|
|
# they should be), no rows are returned. If (as was previously the case)
|
|
|
|
# they are grabbed as the tables are accessed, the t1 rows will be
|
|
|
|
# returned before the query fails.
|
|
|
|
#
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t2 VALUES(5, 6);
|
|
|
|
} db2
|
|
|
|
set ret [list]
|
|
|
|
catch {
|
|
|
|
db1 eval {SELECT * FROM t1 UNION ALL SELECT * FROM t2} {
|
|
|
|
lappend ret $a $b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set ret
|
|
|
|
} {}
|
2006-01-16 19:24:25 +03:00
|
|
|
do_test shared-$av.6.1.4 {
|
2006-01-07 16:21:04 +03:00
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO t1 VALUES(7, 8);
|
|
|
|
} db2
|
|
|
|
set ret [list]
|
|
|
|
catch {
|
|
|
|
db1 eval {
|
|
|
|
SELECT (CASE WHEN a>4 THEN (SELECT a FROM t1) ELSE 0 END) AS d FROM t2;
|
|
|
|
} {
|
|
|
|
lappend ret $d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set ret
|
|
|
|
} {}
|
|
|
|
|
2006-01-06 18:03:48 +03:00
|
|
|
catch {db1 close}
|
|
|
|
catch {db2 close}
|
2006-01-09 09:29:47 +03:00
|
|
|
foreach f [list test.db test2.db] {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete $f ${f}-journal
|
2006-01-09 09:29:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# Tests shared-7.* test auto-vacuum does not invalidate cursors from
|
|
|
|
# other shared-cache users when it reorganizes the database on
|
|
|
|
# COMMIT.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.7.1 {
|
2006-01-09 19:12:04 +03:00
|
|
|
# This test case sets up a test database in auto-vacuum mode consisting
|
|
|
|
# of two tables, t1 and t2. Both have a single index. Table t1 is
|
|
|
|
# populated first (so consists of pages toward the start of the db file),
|
|
|
|
# t2 second (pages toward the end of the file).
|
2006-01-09 09:29:47 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
CREATE TABLE t1(a PRIMARY KEY, b);
|
|
|
|
CREATE TABLE t2(a PRIMARY KEY, b);
|
|
|
|
}
|
2006-01-11 04:08:34 +03:00
|
|
|
set ::contents {}
|
2006-01-09 09:29:47 +03:00
|
|
|
for {set i 0} {$i < 100} {incr i} {
|
|
|
|
set a [string repeat "$i " 20]
|
|
|
|
set b [string repeat "$i " 20]
|
|
|
|
db eval {
|
2006-01-17 12:35:01 +03:00
|
|
|
INSERT INTO t1 VALUES(:a, :b);
|
2006-01-09 09:29:47 +03:00
|
|
|
}
|
|
|
|
lappend ::contents [list [expr $i+1] $a $b]
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
INSERT INTO t2 SELECT * FROM t1;
|
|
|
|
COMMIT;
|
|
|
|
}
|
2006-01-16 18:14:27 +03:00
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.7.2 {
|
2006-01-09 19:12:04 +03:00
|
|
|
# This test case deletes the contents of table t1 (the one at the start of
|
2007-12-14 00:54:09 +03:00
|
|
|
# the file) while many cursors are open on table t2 and its index. All of
|
2006-01-09 19:12:04 +03:00
|
|
|
# the non-root pages will be moved from the end to the start of the file
|
|
|
|
# when the DELETE is committed - this test verifies that moving the pages
|
|
|
|
# does not disturb the open cursors.
|
|
|
|
#
|
|
|
|
|
2006-01-09 09:29:47 +03:00
|
|
|
proc lockrow {db tbl oids body} {
|
|
|
|
set ret [list]
|
|
|
|
db eval "SELECT oid AS i, a, b FROM $tbl ORDER BY a" {
|
|
|
|
if {$i==[lindex $oids 0]} {
|
|
|
|
set noids [lrange $oids 1 end]
|
|
|
|
if {[llength $noids]==0} {
|
|
|
|
set subret [eval $body]
|
|
|
|
} else {
|
|
|
|
set subret [lockrow $db $tbl $noids $body]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lappend ret [list $i $a $b]
|
|
|
|
}
|
|
|
|
return [linsert $subret 0 $ret]
|
|
|
|
}
|
|
|
|
proc locktblrows {db tbl body} {
|
|
|
|
set oids [db eval "SELECT oid FROM $tbl"]
|
|
|
|
lockrow $db $tbl $oids $body
|
|
|
|
}
|
|
|
|
|
|
|
|
set scans [locktblrows db t2 {
|
|
|
|
execsql {
|
|
|
|
DELETE FROM t1;
|
|
|
|
} db2
|
|
|
|
}]
|
|
|
|
set error 0
|
2006-01-09 19:12:04 +03:00
|
|
|
|
|
|
|
# Test that each SELECT query returned the expected contents of t2.
|
2006-01-09 09:29:47 +03:00
|
|
|
foreach s $scans {
|
|
|
|
if {[lsort -integer -index 0 $s]!=$::contents} {
|
|
|
|
set error 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
set error
|
|
|
|
} {0}
|
|
|
|
|
|
|
|
catch {db close}
|
|
|
|
catch {db2 close}
|
2006-01-11 04:08:34 +03:00
|
|
|
unset -nocomplain contents
|
2006-01-06 18:03:48 +03:00
|
|
|
|
2006-01-09 19:12:04 +03:00
|
|
|
#--------------------------------------------------------------------------
|
|
|
|
# The following tests try to trick the shared-cache code into assuming
|
|
|
|
# the wrong encoding for a database.
|
|
|
|
#
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test.db-journal
|
2006-01-17 12:35:01 +03:00
|
|
|
ifcapable utf16 {
|
|
|
|
do_test shared-$av.8.1.1 {
|
|
|
|
sqlite3 db test.db
|
|
|
|
execsql {
|
|
|
|
PRAGMA encoding = 'UTF-16';
|
|
|
|
SELECT * FROM sqlite_master;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.8.1.2 {
|
|
|
|
string range [execsql {PRAGMA encoding;}] 0 end-2
|
|
|
|
} {UTF-16}
|
2008-04-28 20:19:35 +04:00
|
|
|
|
2006-01-17 12:35:01 +03:00
|
|
|
do_test shared-$av.8.1.3 {
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
PRAGMA encoding = 'UTF-8';
|
|
|
|
CREATE TABLE abc(a, b, c);
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.8.1.4 {
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM sqlite_master;
|
|
|
|
}
|
|
|
|
} "table abc abc [expr $AUTOVACUUM?3:2] {CREATE TABLE abc(a, b, c)}"
|
|
|
|
do_test shared-$av.8.1.5 {
|
|
|
|
db2 close
|
|
|
|
execsql {
|
|
|
|
PRAGMA encoding;
|
|
|
|
}
|
|
|
|
} {UTF-8}
|
2008-04-28 20:19:35 +04:00
|
|
|
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test2.db test2.db-journal
|
2006-01-17 12:35:01 +03:00
|
|
|
do_test shared-$av.8.2.1 {
|
|
|
|
execsql {
|
|
|
|
ATTACH 'test2.db' AS aux;
|
|
|
|
SELECT * FROM aux.sqlite_master;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.8.2.2 {
|
|
|
|
sqlite3 db2 test2.db
|
|
|
|
execsql {
|
|
|
|
PRAGMA encoding = 'UTF-16';
|
|
|
|
CREATE TABLE def(d, e, f);
|
|
|
|
} db2
|
|
|
|
string range [execsql {PRAGMA encoding;} db2] 0 end-2
|
|
|
|
} {UTF-16}
|
2007-08-03 11:33:08 +04:00
|
|
|
|
2008-04-28 20:19:35 +04:00
|
|
|
catch {db close}
|
|
|
|
catch {db2 close}
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test2.db
|
2008-04-28 20:19:35 +04:00
|
|
|
|
|
|
|
do_test shared-$av.8.3.2 {
|
|
|
|
sqlite3 db test.db
|
|
|
|
execsql { CREATE TABLE def(d, e, f) }
|
|
|
|
execsql { PRAGMA encoding }
|
|
|
|
} {UTF-8}
|
|
|
|
do_test shared-$av.8.3.3 {
|
|
|
|
set zDb16 "[encoding convertto unicode test.db]\x00\x00"
|
|
|
|
set db16 [sqlite3_open16 $zDb16 {}]
|
|
|
|
|
|
|
|
set stmt [sqlite3_prepare $db16 "SELECT sql FROM sqlite_master" -1 DUMMY]
|
|
|
|
sqlite3_step $stmt
|
|
|
|
set sql [sqlite3_column_text $stmt 0]
|
|
|
|
sqlite3_finalize $stmt
|
|
|
|
set sql
|
|
|
|
} {CREATE TABLE def(d, e, f)}
|
|
|
|
do_test shared-$av.8.3.4 {
|
|
|
|
set stmt [sqlite3_prepare $db16 "PRAGMA encoding" -1 DUMMY]
|
|
|
|
sqlite3_step $stmt
|
|
|
|
set enc [sqlite3_column_text $stmt 0]
|
|
|
|
sqlite3_finalize $stmt
|
|
|
|
set enc
|
|
|
|
} {UTF-8}
|
|
|
|
|
|
|
|
sqlite3_close $db16
|
|
|
|
|
2007-08-03 11:33:08 +04:00
|
|
|
# Bug #2547 is causing this to fail.
|
|
|
|
if 0 {
|
2006-01-17 12:35:01 +03:00
|
|
|
do_test shared-$av.8.2.3 {
|
|
|
|
catchsql {
|
|
|
|
SELECT * FROM aux.sqlite_master;
|
|
|
|
}
|
|
|
|
} {1 {attached databases must use the same text encoding as main database}}
|
|
|
|
}
|
2007-08-03 11:33:08 +04:00
|
|
|
}
|
2006-01-09 19:12:04 +03:00
|
|
|
|
|
|
|
catch {db close}
|
|
|
|
catch {db2 close}
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test2.db
|
2006-01-10 15:31:39 +03:00
|
|
|
|
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# The following tests - shared-9.* - test interactions between TEMP triggers
|
|
|
|
# and shared-schemas.
|
|
|
|
#
|
|
|
|
ifcapable trigger&&tempdb {
|
|
|
|
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.9.1 {
|
2006-01-10 15:31:39 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE abc(a, b, c);
|
|
|
|
CREATE TABLE abc_mirror(a, b, c);
|
|
|
|
CREATE TEMP TRIGGER BEFORE INSERT ON abc BEGIN
|
|
|
|
INSERT INTO abc_mirror(a, b, c) VALUES(new.a, new.b, new.c);
|
|
|
|
END;
|
|
|
|
INSERT INTO abc VALUES(1, 2, 3);
|
|
|
|
SELECT * FROM abc_mirror;
|
|
|
|
}
|
|
|
|
} {1 2 3}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.9.2 {
|
2006-01-10 15:31:39 +03:00
|
|
|
execsql {
|
|
|
|
INSERT INTO abc VALUES(4, 5, 6);
|
|
|
|
SELECT * FROM abc_mirror;
|
|
|
|
} db2
|
|
|
|
} {1 2 3}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.9.3 {
|
2006-01-10 15:31:39 +03:00
|
|
|
db close
|
|
|
|
db2 close
|
|
|
|
} {}
|
|
|
|
|
|
|
|
} ; # End shared-9.*
|
2006-01-09 19:12:04 +03:00
|
|
|
|
2006-01-15 14:39:18 +03:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
# The following tests - shared-10.* - test that the library behaves
|
|
|
|
# correctly when a connection to a shared-cache is closed.
|
|
|
|
#
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.1 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Create a small sample database with two connections to it (db and db2).
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
2006-01-15 14:39:18 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE ab(a PRIMARY KEY, b);
|
|
|
|
CREATE TABLE de(d PRIMARY KEY, e);
|
|
|
|
INSERT INTO ab VALUES('Chiang Mai', 100000);
|
|
|
|
INSERT INTO ab VALUES('Bangkok', 8000000);
|
|
|
|
INSERT INTO de VALUES('Ubon', 120000);
|
|
|
|
INSERT INTO de VALUES('Khon Kaen', 200000);
|
|
|
|
}
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.2 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Open a read-transaction with the first connection, a write-transaction
|
|
|
|
# with the second.
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
SELECT * FROM ab;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO de VALUES('Pataya', 30000);
|
|
|
|
} db2
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.3 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# An external connection should be able to read the database, but not
|
|
|
|
# prepare a write operation.
|
2006-01-24 00:38:03 +03:00
|
|
|
if {$::tcl_platform(platform)=="unix"} {
|
2020-11-16 21:45:21 +03:00
|
|
|
sqlite3 db3 "file:test.db?cache=private" -uri 1
|
2006-01-24 00:38:03 +03:00
|
|
|
} else {
|
|
|
|
sqlite3 db3 TEST.DB
|
|
|
|
}
|
2006-01-15 14:39:18 +03:00
|
|
|
execsql {
|
|
|
|
SELECT * FROM ab;
|
|
|
|
} db3
|
|
|
|
catchsql {
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO de VALUES('Pataya', 30000);
|
|
|
|
} db3
|
|
|
|
} {1 {database is locked}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.4 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Close the connection with the write-transaction open
|
|
|
|
db2 close
|
|
|
|
} {}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.5 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Test that the db2 transaction has been automatically rolled back.
|
|
|
|
# If it has not the ('Pataya', 30000) entry will still be in the table.
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM de;
|
|
|
|
}
|
|
|
|
} {Ubon 120000 {Khon Kaen} 200000}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.5 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Closing db2 should have dropped the shared-cache back to a read-lock.
|
|
|
|
# So db3 should be able to prepare a write...
|
|
|
|
catchsql {INSERT INTO de VALUES('Pataya', 30000);} db3
|
|
|
|
} {0 {}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.6 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# ... but not commit it.
|
|
|
|
catchsql {COMMIT} db3
|
|
|
|
} {1 {database is locked}}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.7 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Commit the (read-only) db transaction. Check via db3 to make sure the
|
|
|
|
# contents of table "de" are still as they should be.
|
|
|
|
execsql {
|
|
|
|
COMMIT;
|
|
|
|
}
|
|
|
|
execsql {
|
|
|
|
SELECT * FROM de;
|
|
|
|
} db3
|
|
|
|
} {Ubon 120000 {Khon Kaen} 200000 Pataya 30000}
|
2006-01-16 15:46:41 +03:00
|
|
|
do_test shared-$av.10.9 {
|
2006-01-15 14:39:18 +03:00
|
|
|
# Commit the external transaction.
|
|
|
|
catchsql {COMMIT} db3
|
|
|
|
} {0 {}}
|
2006-01-16 15:46:41 +03:00
|
|
|
integrity_check shared-$av.10.10
|
|
|
|
do_test shared-$av.10.11 {
|
2006-01-15 14:39:18 +03:00
|
|
|
db close
|
|
|
|
db3 close
|
|
|
|
} {}
|
|
|
|
|
2006-01-23 08:50:58 +03:00
|
|
|
do_test shared-$av.11.1 {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
2006-01-23 08:50:58 +03:00
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE abc(a, b, c);
|
|
|
|
CREATE TABLE abc2(a, b, c);
|
|
|
|
BEGIN;
|
|
|
|
INSERT INTO abc VALUES(1, 2, 3);
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.11.2 {
|
|
|
|
catchsql {BEGIN;} db2
|
|
|
|
catchsql {SELECT * FROM abc;} db2
|
|
|
|
} {1 {database table is locked: abc}}
|
|
|
|
do_test shared-$av.11.3 {
|
|
|
|
catchsql {BEGIN} db2
|
|
|
|
} {1 {cannot start a transaction within a transaction}}
|
|
|
|
do_test shared-$av.11.4 {
|
|
|
|
catchsql {SELECT * FROM abc2;} db2
|
|
|
|
} {0 {}}
|
|
|
|
do_test shared-$av.11.5 {
|
|
|
|
catchsql {INSERT INTO abc2 VALUES(1, 2, 3);} db2
|
2009-03-16 16:19:36 +03:00
|
|
|
} {1 {database table is locked}}
|
2006-01-23 08:50:58 +03:00
|
|
|
do_test shared-$av.11.6 {
|
|
|
|
catchsql {SELECT * FROM abc2}
|
|
|
|
} {0 {}}
|
|
|
|
do_test shared-$av.11.6 {
|
|
|
|
execsql {
|
|
|
|
ROLLBACK;
|
|
|
|
PRAGMA read_uncommitted = 1;
|
|
|
|
} db2
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.11.7 {
|
|
|
|
execsql {
|
|
|
|
INSERT INTO abc2 VALUES(4, 5, 6);
|
|
|
|
INSERT INTO abc2 VALUES(7, 8, 9);
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.11.8 {
|
|
|
|
set res [list]
|
|
|
|
db2 eval {
|
|
|
|
SELECT abc.a as I, abc2.a as II FROM abc, abc2;
|
|
|
|
} {
|
|
|
|
execsql {
|
|
|
|
DELETE FROM abc WHERE 1;
|
|
|
|
}
|
|
|
|
lappend res $I $II
|
|
|
|
}
|
|
|
|
set res
|
|
|
|
} {1 4 {} 7}
|
2007-05-05 22:39:25 +04:00
|
|
|
if {[llength [info command sqlite3_shared_cache_report]]==1} {
|
2012-03-06 07:00:49 +04:00
|
|
|
ifcapable curdir {
|
|
|
|
do_test shared-$av.11.9 {
|
|
|
|
string tolower [sqlite3_shared_cache_report]
|
|
|
|
} [string tolower [list [file nativename [file normalize test.db]] 2]]
|
|
|
|
}
|
2007-04-05 15:25:58 +04:00
|
|
|
}
|
2006-01-23 08:50:58 +03:00
|
|
|
|
|
|
|
do_test shared-$av.11.11 {
|
|
|
|
db close
|
|
|
|
db2 close
|
|
|
|
} {}
|
|
|
|
|
2007-09-01 20:16:15 +04:00
|
|
|
# This tests that if it is impossible to free any pages, SQLite will
|
|
|
|
# exceed the limit set by PRAGMA cache_size.
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db test.db-journal
|
2007-09-12 21:01:45 +04:00
|
|
|
sqlite3 db test.db
|
|
|
|
ifcapable pager_pragmas {
|
|
|
|
do_test shared-$av.12.1 {
|
|
|
|
execsql {
|
|
|
|
PRAGMA cache_size = 10;
|
|
|
|
PRAGMA cache_size;
|
|
|
|
}
|
|
|
|
} {10}
|
|
|
|
}
|
2007-09-01 20:16:15 +04:00
|
|
|
do_test shared-$av.12.2 {
|
|
|
|
set ::db_handles [list]
|
|
|
|
for {set i 1} {$i < 15} {incr i} {
|
|
|
|
lappend ::db_handles db$i
|
|
|
|
sqlite3 db$i test.db
|
|
|
|
execsql "CREATE TABLE db${i}(a, b, c)" db$i
|
|
|
|
execsql "INSERT INTO db${i} VALUES(1, 2, 3)"
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
proc nested_select {handles} {
|
|
|
|
[lindex $handles 0] eval "SELECT * FROM [lindex $handles 0]" {
|
|
|
|
lappend ::res $a $b $c
|
|
|
|
if {[llength $handles]>1} {
|
|
|
|
nested_select [lrange $handles 1 end]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
do_test shared-$av.12.3 {
|
|
|
|
set ::res [list]
|
|
|
|
nested_select $::db_handles
|
|
|
|
set ::res
|
|
|
|
} [string range [string repeat "1 2 3 " [llength $::db_handles]] 0 end-1]
|
|
|
|
|
|
|
|
do_test shared-$av.12.X {
|
|
|
|
db close
|
|
|
|
foreach h $::db_handles {
|
|
|
|
$h close
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
|
2008-07-08 11:35:51 +04:00
|
|
|
# Internally, locks are acquired on shared B-Tree structures in the order
|
|
|
|
# that the structures appear in the virtual memory address space. This
|
|
|
|
# test case attempts to cause the order of the structures in memory
|
|
|
|
# to be different from the order in which they are attached to a given
|
|
|
|
# database handle. This covers an extra line or two.
|
|
|
|
#
|
|
|
|
do_test shared-$av.13.1 {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test2.db test3.db test4.db test5.db
|
2008-07-08 11:35:51 +04:00
|
|
|
sqlite3 db :memory:
|
|
|
|
execsql {
|
|
|
|
ATTACH 'test2.db' AS aux2;
|
|
|
|
ATTACH 'test3.db' AS aux3;
|
|
|
|
ATTACH 'test4.db' AS aux4;
|
|
|
|
ATTACH 'test5.db' AS aux5;
|
|
|
|
DETACH aux2;
|
|
|
|
DETACH aux3;
|
|
|
|
DETACH aux4;
|
|
|
|
ATTACH 'test2.db' AS aux2;
|
|
|
|
ATTACH 'test3.db' AS aux3;
|
|
|
|
ATTACH 'test4.db' AS aux4;
|
|
|
|
}
|
|
|
|
} {}
|
|
|
|
do_test shared-$av.13.2 {
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(a, b, c);
|
|
|
|
CREATE TABLE aux2.t2(a, b, c);
|
|
|
|
CREATE TABLE aux3.t3(a, b, c);
|
|
|
|
CREATE TABLE aux4.t4(a, b, c);
|
|
|
|
CREATE TABLE aux5.t5(a, b, c);
|
|
|
|
SELECT count(*) FROM
|
|
|
|
aux2.sqlite_master,
|
|
|
|
aux3.sqlite_master,
|
|
|
|
aux4.sqlite_master,
|
|
|
|
aux5.sqlite_master
|
|
|
|
}
|
|
|
|
} {1}
|
|
|
|
do_test shared-$av.13.3 {
|
|
|
|
db close
|
|
|
|
} {}
|
|
|
|
|
|
|
|
# Test that nothing horrible happens if a connection to a shared B-Tree
|
|
|
|
# structure is closed while some other connection has an open cursor.
|
|
|
|
#
|
|
|
|
do_test shared-$av.14.1 {
|
|
|
|
sqlite3 db test.db
|
|
|
|
sqlite3 db2 test.db
|
|
|
|
execsql {SELECT name FROM sqlite_master}
|
|
|
|
} {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
|
|
|
|
do_test shared-$av.14.2 {
|
|
|
|
set res [list]
|
|
|
|
db eval {SELECT name FROM sqlite_master} {
|
|
|
|
if {$name eq "db7"} {
|
|
|
|
db2 close
|
|
|
|
}
|
|
|
|
lappend res $name
|
|
|
|
}
|
|
|
|
set res
|
|
|
|
} {db1 db2 db3 db4 db5 db6 db7 db8 db9 db10 db11 db12 db13 db14}
|
|
|
|
do_test shared-$av.14.3 {
|
|
|
|
db close
|
|
|
|
} {}
|
|
|
|
|
2010-07-23 19:55:31 +04:00
|
|
|
# Populate a database schema using connection [db]. Then drop it using
|
|
|
|
# [db2]. This is to try to find any points where shared-schema elements
|
|
|
|
# are allocated using the lookaside buffer of [db].
|
|
|
|
#
|
|
|
|
# Mutexes are enabled for this test as that activates a couple of useful
|
|
|
|
# assert() statements in the C code.
|
|
|
|
#
|
|
|
|
do_test shared-$av-15.1 {
|
2011-08-02 04:57:34 +04:00
|
|
|
forcedelete test.db
|
2010-07-23 19:55:31 +04:00
|
|
|
sqlite3 db test.db -fullmutex 1
|
|
|
|
sqlite3 db2 test.db -fullmutex 1
|
|
|
|
execsql {
|
|
|
|
CREATE TABLE t1(a, b, c);
|
|
|
|
CREATE INDEX i1 ON t1(a, b);
|
|
|
|
CREATE VIEW v1 AS SELECT * FROM t1;
|
|
|
|
CREATE VIEW v2 AS SELECT * FROM t1, v1
|
|
|
|
WHERE t1.c=v1.c GROUP BY t1.a ORDER BY v1.b;
|
|
|
|
CREATE TRIGGER tr1 AFTER INSERT ON t1
|
|
|
|
WHEN new.a!=1
|
|
|
|
BEGIN
|
|
|
|
DELETE FROM t1 WHERE a=5;
|
|
|
|
INSERT INTO t1 VALUES(1, 2, 3);
|
|
|
|
UPDATE t1 SET c=c+1;
|
|
|
|
END;
|
|
|
|
|
|
|
|
INSERT INTO t1 VALUES(5, 6, 7);
|
|
|
|
INSERT INTO t1 VALUES(8, 9, 10);
|
|
|
|
INSERT INTO t1 VALUES(11, 12, 13);
|
|
|
|
ANALYZE;
|
|
|
|
SELECT * FROM t1;
|
|
|
|
}
|
|
|
|
} {1 2 6 8 9 12 1 2 5 11 12 14 1 2 4}
|
|
|
|
do_test shared-$av-15.2 {
|
|
|
|
execsql { DROP TABLE t1 } db2
|
|
|
|
} {}
|
|
|
|
db close
|
|
|
|
db2 close
|
|
|
|
|
2012-05-27 00:08:49 +04:00
|
|
|
# Shared cache on a :memory: database. This only works for URI filenames.
|
2012-05-26 22:06:38 +04:00
|
|
|
#
|
|
|
|
do_test shared-$av-16.1 {
|
2012-05-27 00:08:49 +04:00
|
|
|
sqlite3 db1 file::memory: -uri 1
|
|
|
|
sqlite3 db2 file::memory: -uri 1
|
2012-05-26 22:06:38 +04:00
|
|
|
db1 eval {
|
|
|
|
CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
|
|
|
|
}
|
|
|
|
db2 eval {
|
|
|
|
SELECT x FROM t1 ORDER BY x;
|
|
|
|
}
|
|
|
|
} {1 2 3}
|
|
|
|
do_test shared-$av-16.2 {
|
|
|
|
db2 eval {
|
|
|
|
INSERT INTO t1 VALUES(99);
|
|
|
|
DELETE FROM t1 WHERE x=2;
|
|
|
|
}
|
|
|
|
db1 eval {
|
|
|
|
SELECT x FROM t1 ORDER BY x;
|
|
|
|
}
|
|
|
|
} {1 3 99}
|
|
|
|
|
2012-05-27 00:08:49 +04:00
|
|
|
# Verify that there is no cache sharing ordinary (non-URI) filenames are
|
|
|
|
# used.
|
|
|
|
#
|
|
|
|
do_test shared-$av-16.3 {
|
|
|
|
db1 close
|
|
|
|
db2 close
|
|
|
|
sqlite3 db1 :memory:
|
|
|
|
sqlite3 db2 :memory:
|
|
|
|
db1 eval {
|
|
|
|
CREATE TABLE t1(x); INSERT INTO t1 VALUES(4),(5),(6);
|
|
|
|
}
|
|
|
|
catchsql {
|
|
|
|
SELECT * FROM t1;
|
|
|
|
} db2
|
|
|
|
} {1 {no such table: t1}}
|
2012-05-28 17:58:00 +04:00
|
|
|
|
|
|
|
# Shared cache on named memory databases.
|
|
|
|
#
|
|
|
|
do_test shared-$av-16.4 {
|
|
|
|
db1 close
|
|
|
|
db2 close
|
|
|
|
forcedelete test.db test.db-wal test.db-journal
|
|
|
|
sqlite3 db1 file:test.db?mode=memory -uri 1
|
|
|
|
sqlite3 db2 file:test.db?mode=memory -uri 1
|
|
|
|
db1 eval {
|
|
|
|
CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
|
|
|
|
}
|
|
|
|
db2 eval {
|
|
|
|
SELECT x FROM t1 ORDER BY x;
|
|
|
|
}
|
|
|
|
} {1 2 3}
|
|
|
|
do_test shared-$av-16.5 {
|
|
|
|
db2 eval {
|
|
|
|
INSERT INTO t1 VALUES(99);
|
|
|
|
DELETE FROM t1 WHERE x=2;
|
|
|
|
}
|
|
|
|
db1 eval {
|
|
|
|
SELECT x FROM t1 ORDER BY x;
|
|
|
|
}
|
|
|
|
} {1 3 99}
|
|
|
|
do_test shared-$av-16.6 {
|
|
|
|
file exists test.db
|
|
|
|
} {0} ;# Verify that the database is in-memory
|
|
|
|
|
|
|
|
# Shared cache on named memory databases with different names.
|
|
|
|
#
|
|
|
|
do_test shared-$av-16.7 {
|
|
|
|
db1 close
|
|
|
|
db2 close
|
|
|
|
forcedelete test1.db test2.db
|
|
|
|
sqlite3 db1 file:test1.db?mode=memory -uri 1
|
|
|
|
sqlite3 db2 file:test2.db?mode=memory -uri 1
|
|
|
|
db1 eval {
|
|
|
|
CREATE TABLE t1(x); INSERT INTO t1 VALUES(1),(2),(3);
|
|
|
|
}
|
|
|
|
catchsql {
|
|
|
|
SELECT x FROM t1 ORDER BY x;
|
|
|
|
} db2
|
|
|
|
} {1 {no such table: t1}}
|
|
|
|
do_test shared-$av-16.8 {
|
|
|
|
file exists test1.db
|
|
|
|
} {0} ;# Verify that the database is in-memory
|
|
|
|
|
2012-06-07 21:16:04 +04:00
|
|
|
# Shared cache on named memory databases attached to readonly connections.
|
|
|
|
#
|
2018-10-12 18:01:56 +03:00
|
|
|
if {![sqlite3 -has-codec]} {
|
|
|
|
do_test shared-$av-16.8.1 {
|
|
|
|
db1 close
|
|
|
|
db2 close
|
|
|
|
|
|
|
|
sqlite3 db test1.db
|
|
|
|
db eval {
|
|
|
|
CREATE TABLE yy(a, b);
|
|
|
|
INSERT INTO yy VALUES(77, 88);
|
|
|
|
}
|
|
|
|
db close
|
|
|
|
|
|
|
|
sqlite3 db1 test1.db -uri 1 -readonly 1
|
|
|
|
sqlite3 db2 test2.db -uri 1
|
|
|
|
|
|
|
|
db1 eval {
|
|
|
|
ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
|
|
|
|
CREATE TABLE shared.xx(a, b);
|
|
|
|
INSERT INTO xx VALUES(55, 66);
|
|
|
|
}
|
|
|
|
db2 eval {
|
|
|
|
ATTACH 'file:mem?mode=memory&cache=shared' AS shared;
|
|
|
|
SELECT * FROM xx;
|
|
|
|
}
|
|
|
|
} {55 66}
|
|
|
|
|
|
|
|
do_test shared-$av-16.8.2 { db1 eval { SELECT * FROM yy } } {77 88}
|
|
|
|
do_test shared-$av-16.8.3 {
|
|
|
|
list [catch {db1 eval { INSERT INTO yy VALUES(1, 2) }} msg] $msg
|
|
|
|
} {1 {attempt to write a readonly database}}
|
|
|
|
|
2012-06-07 21:16:04 +04:00
|
|
|
db1 close
|
|
|
|
db2 close
|
2018-10-12 18:01:56 +03:00
|
|
|
}
|
2012-05-27 00:08:49 +04:00
|
|
|
|
2012-05-26 22:06:38 +04:00
|
|
|
} ;# end of autovacuum on/off loop
|
2006-01-16 15:46:41 +03:00
|
|
|
|
2005-12-30 19:28:01 +03:00
|
|
|
sqlite3_enable_shared_cache $::enable_shared_cache
|
2006-01-16 15:46:41 +03:00
|
|
|
finish_test
|