blob: cc32aa0190160bfc04a5ed93165f1524716b6371 [file] [log] [blame]
danielk1977aef0bf62005-12-30 16:28:01 +00001# 2005 December 30
2#
3# The author disclaims copyright to this source code. In place of
4# a legal notice, here is a blessing:
5#
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.
9#
10#***********************************************************************
11# This file implements regression tests for SQLite library. The
12# focus of this file is testing the SELECT statement.
13#
danielk1977de0fe3e2006-01-06 06:33:12 +000014# $Id: shared.test,v 1.3 2006/01/06 06:33:13 danielk1977 Exp $
danielk1977aef0bf62005-12-30 16:28:01 +000015
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18db close
19
20ifcapable !shared_cache {
21 finish_test
22 return
23}
danielk1977da184232006-01-05 11:34:32 +000024set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
danielk1977aef0bf62005-12-30 16:28:01 +000025
26# Test organization:
27#
28# shared-1.*: Simple test to verify basic sanity of table level locking when
29# two connections share a pager cache.
30# shared-2.*: Test that a read transaction can co-exist with a
31# write-transaction, including a simple test to ensure the
32# external locking protocol is still working.
danielk1977da184232006-01-05 11:34:32 +000033# shared-3.*: Simple test of read-uncommitted mode.
danielk1977aef0bf62005-12-30 16:28:01 +000034#
danielk1977de0fe3e2006-01-06 06:33:12 +000035# shared-4.*: Check that the schema is locked and unlocked correctly.
36#
danielk1977aef0bf62005-12-30 16:28:01 +000037
38do_test shared-1.1 {
39 # Open a second database on the file test.db. It should use the same pager
40 # cache and schema as the original connection. Verify that only 1 file is
41 # opened.
42 sqlite3 db2 test.db
43 sqlite3 db test.db
44 set ::sqlite_open_file_count
45} {1}
46do_test shared-1.2 {
47 # Add a table and a single row of data via the first connection.
48 # Ensure that the second connection can see them.
49 execsql {
50 CREATE TABLE abc(a, b, c);
51 INSERT INTO abc VALUES(1, 2, 3);
52 } db
53 execsql {
54 SELECT * FROM abc;
55 } db2
56} {1 2 3}
57do_test shared-1.3 {
58 # Have the first connection begin a transaction and obtain a read-lock
59 # on table abc. This should not prevent the second connection from
60 # querying abc.
61 execsql {
62 BEGIN;
63 SELECT * FROM abc;
64 }
65 execsql {
66 SELECT * FROM abc;
67 } db2
68} {1 2 3}
69do_test shared-1.4 {
70 # Try to insert a row into abc via connection 2. This should fail because
71 # of the read-lock connection 1 is holding on table abc (obtained in the
72 # previous test case).
73 catchsql {
74 INSERT INTO abc VALUES(4, 5, 6);
75 } db2
76} {1 {database is locked}}
77do_test shared-1.5 {
danielk1977da184232006-01-05 11:34:32 +000078 # Using connection 2 (the one without the open transaction), try to create
79 # a new table. This should fail because of the open read transaction
80 # held by connection 1.
81 catchsql {
82 CREATE TABLE def(d, e, f);
83 } db2
84} {1 {database is locked}}
85do_test shared-1.6 {
86 # Upgrade connection 1's transaction to a write transaction. Create
87 # a new table - def - and insert a row into it. Because the connection 1
88 # transaction modifies the schema, it should not be possible for
89 # connection 2 to access the database at all until the connection 1
90 # has finished the transaction.
danielk1977aef0bf62005-12-30 16:28:01 +000091 execsql {
92 CREATE TABLE def(d, e, f);
danielk1977aef0bf62005-12-30 16:28:01 +000093 INSERT INTO def VALUES('IV', 'V', 'VI');
94 }
95} {}
96do_test shared-1.7 {
97 # Read from the sqlite_master table with connection 1 (inside the
danielk1977da184232006-01-05 11:34:32 +000098 # transaction). Then test that we can not do this with connection 2. This
99 # is because of the schema-modified lock established by connection 1
100 # in the previous test case.
danielk1977aef0bf62005-12-30 16:28:01 +0000101 execsql {
102 SELECT * FROM sqlite_master;
103 }
104 catchsql {
danielk1977da184232006-01-05 11:34:32 +0000105 SELECT * FROM sqlite_master;
danielk1977aef0bf62005-12-30 16:28:01 +0000106 } db2
107} {1 {database is locked}}
108do_test shared-1.8 {
danielk1977aef0bf62005-12-30 16:28:01 +0000109 # Commit the connection 1 transaction.
110 execsql {
111 COMMIT;
112 }
113} {}
114
115do_test shared-2.1 {
116 # Open connection db3 to the database. Use a different path to the same
117 # file so that db3 does *not* share the same pager cache as db and db2
118 # (there should be two open file handles).
119 sqlite3 db3 ./test.db
120 set ::sqlite_open_file_count
121} {2}
122do_test shared-2.2 {
123 # Start read transactions on db and db2 (the shared pager cache). Ensure
124 # db3 cannot write to the database.
125 execsql {
126 BEGIN;
127 SELECT * FROM abc;
128 }
129 execsql {
130 BEGIN;
131 SELECT * FROM abc;
132 } db2
133 catchsql {
134 INSERT INTO abc VALUES(1, 2, 3);
135 } db2
136} {1 {database is locked}}
137do_test shared-2.3 {
138 # Turn db's transaction into a write-transaction. db3 should still be
139 # able to read from table def (but will not see the new row). Connection
140 # db2 should not be able to read def (because of the write-lock).
141
142# Todo: The failed "INSERT INTO abc ..." statement in the above test
143# has started a write-transaction on db2 (should this be so?). This
144# would prevent connection db from starting a write-transaction. So roll the
145# db2 transaction back and replace it with a new read transaction.
146 execsql {
147 ROLLBACK;
148 BEGIN;
149 SELECT * FROM abc;
150 } db2
151
152 execsql {
153 INSERT INTO def VALUES('VII', 'VIII', 'IX');
154 }
155 concat [
156 catchsql { SELECT * FROM def; } db3
157 ] [
158 catchsql { SELECT * FROM def; } db2
159 ]
danielk1977da184232006-01-05 11:34:32 +0000160} {0 {IV V VI} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000161do_test shared-2.4 {
162 # Commit the open transaction on db. db2 still holds a read-transaction.
163 # This should prevent db3 from writing to the database, but not from
164 # reading.
165 execsql {
166 COMMIT;
167 }
168 concat [
169 catchsql { SELECT * FROM def; } db3
170 ] [
171 catchsql { INSERT INTO def VALUES('X', 'XI', 'XII'); } db3
172 ]
danielk1977da184232006-01-05 11:34:32 +0000173} {0 {IV V VI VII VIII IX} 1 {database is locked}}
danielk1977aef0bf62005-12-30 16:28:01 +0000174
danielk1977da184232006-01-05 11:34:32 +0000175catchsql COMMIT db2
176
177do_test shared-3.1.1 {
178 # This test case starts a linear scan of table 'seq' using a
179 # read-uncommitted connection. In the middle of the scan, rows are added
180 # to the end of the seq table (ahead of the current cursor position).
181 # The uncommitted rows should be included in the results of the scan.
182 execsql "
183 CREATE TABLE seq(i, x);
184 INSERT INTO seq VALUES(1, '[string repeat X 500]');
185 INSERT INTO seq VALUES(2, '[string repeat X 500]');
186 "
187 execsql {SELECT * FROM sqlite_master} db2
188 execsql {PRAGMA read_uncommitted = 1} db2
189
190 set ret [list]
191 db2 eval {SELECT i FROM seq} {
192 if {$i < 4} {
193 execsql {
194 INSERT INTO seq SELECT i + (SELECT max(i) FROM seq), x FROM seq;
195 }
196 }
197 lappend ret $i
198 }
199 set ret
200} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
201do_test shared-3.1.2 {
202 # Another linear scan through table seq using a read-uncommitted connection.
203 # This time, delete each row as it is read. Should not affect the results of
204 # the scan, but the table should be empty after the scan is concluded
205 # (test 3.1.3 verifies this).
206 set ret [list]
207 db2 eval {SELECT i FROM seq} {
208 db eval {DELETE FROM seq WHERE i = $i}
209 lappend ret $i
210 }
211 set ret
212} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16}
213do_test shared-3.1.3 {
214 execsql {
215 SELECT * FROM seq;
216 }
217} {}
danielk1977aef0bf62005-12-30 16:28:01 +0000218
219catch {db close}
220catch {db2 close}
221catch {db3 close}
222
danielk1977de0fe3e2006-01-06 06:33:12 +0000223#--------------------------------------------------------------------------
224# Tests shared-4.* test that the schema locking rules are applied
225# correctly. i.e.:
226#
227# 1. All transactions require a read-lock on the schemas of databases they
228# access.
229# 2. Transactions that modify a database schema require a write-lock on that
230# schema.
231# 3. It is not possible to compile a statement while another handle has a
232# write-lock on the schema.
233#
234
235# Open two database handles db and db2. Each has a single attach database
236# (as well as main):
237#
238# db.main -> ./test.db
239# db.test2 -> ./test2.db
240# db2.main -> ./test2.db
241# db2.test -> ./test.db
242#
243file delete -force test.db
244file delete -force test2.db
245file delete -force test2.db-journal
246sqlite3 db test.db
247sqlite3 db2 test2.db
248do_test shared-4.1.1 {
249 set sqlite_open_file_count
250} {2}
251do_test shared-4.1.2 {
252 execsql {ATTACH 'test2.db' AS test2}
253 set sqlite_open_file_count
254} {2}
255do_test shared-4.1.3 {
256 execsql {ATTACH 'test.db' AS test} db2
257 set sqlite_open_file_count
258} {2}
259
260do_test shared-4.2.1 {
261 execsql {
262 CREATE TABLE abc(a, b, c);
263 INSERT INTO abc VALUES('i', 'ii', 'iii');
264 }
265} {}
266do_test shared-4.2.2 {
267 execsql {
268 SELECT * FROM test.abc;
269 } db2
270} {i ii iii}
271
272catch {db2 close}
273catch {db close}
274
danielk1977aef0bf62005-12-30 16:28:01 +0000275finish_test
276sqlite3_enable_shared_cache $::enable_shared_cache
277