blob: d7428fa51d2f34438c23c67282e1d5a48635083c [file] [log] [blame]
danielk1977ed429312006-01-19 08:43:31 +00001# 2005 January 19
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#
danielk19772b8c13e2006-01-24 14:21:24 +000012# $Id: shared2.test,v 1.3 2006/01/24 14:21:24 danielk1977 Exp $
danielk1977ed429312006-01-19 08:43:31 +000013
14set testdir [file dirname $argv0]
15source $testdir/tester.tcl
16db close
17
18ifcapable !shared_cache {
19 finish_test
20 return
21}
22set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
23
24
25# Test that if we delete all rows from a table any read-uncommitted
26# cursors are correctly invalidated. Test on both table and index btrees.
27do_test shared2-1.1 {
28 sqlite3 db1 test.db
29 sqlite3 db2 test.db
30
31 # Set up some data. Table "numbers" has 64 rows after this block
32 # is executed.
33 execsql {
34 BEGIN;
35 CREATE TABLE numbers(a PRIMARY KEY, b);
36 INSERT INTO numbers(oid) VALUES(NULL);
37 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
38 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
39 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
40 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
41 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
42 INSERT INTO numbers(oid) SELECT NULL FROM numbers;
43 UPDATE numbers set a = oid, b = 'abcdefghijklmnopqrstuvwxyz0123456789';
44 COMMIT;
45 } db1
46} {}
47do_test shared2-1.2 {
48 # Put connection 2 in read-uncommitted mode and start a SELECT on table
49 # 'numbers'. Half way through the SELECT, use connection 1 to delete the
50 # contents of this table.
51 execsql {
52 pragma read_uncommitted = 1;
53 } db2
54 set count [execsql {SELECT count(*) FROM numbers} db2]
55 db2 eval {SELECT a FROM numbers ORDER BY oid} {
56 if {$a==32} {
57 execsql {
58 BEGIN;
59 DELETE FROM numbers;
60 } db1
61 }
62 }
63 list $a $count
64} {32 64}
65do_test shared2-1.3 {
66 # Same test as 1.2, except scan using the index this time.
67 execsql {
68 ROLLBACK;
69 } db1
70 set count [execsql {SELECT count(*) FROM numbers} db2]
71 db2 eval {SELECT a, b FROM numbers ORDER BY a} {
72 if {$a==32} {
73 execsql {
74 DELETE FROM numbers;
75 } db1
76 }
77 }
78 list $a $count
79} {32 64}
80
danielk19772b8c13e2006-01-24 14:21:24 +000081# Rollback data into or out of a table while a read-uncommitted
82# cursor is scanning it.
83#
84do_test shared2-2.1 {
85 execsql {
86 INSERT INTO numbers VALUES(1, 'Medium length text field');
87 INSERT INTO numbers VALUES(2, 'Medium length text field');
88 INSERT INTO numbers VALUES(3, 'Medium length text field');
89 INSERT INTO numbers VALUES(4, 'Medium length text field');
90 BEGIN;
91 DELETE FROM numbers WHERE (a%2)=0;
92 } db1
93 set res [list]
94 db2 eval {
95 SELECT a FROM numbers ORDER BY a;
96 } {
97 lappend res $a
98 if {$a==3} {
99 execsql {ROLLBACK} db1
100 }
101 }
102 set res
103} {1 3 4}
104do_test shared2-2.2 {
105 execsql {
106 BEGIN;
107 INSERT INTO numbers VALUES(5, 'Medium length text field');
108 INSERT INTO numbers VALUES(6, 'Medium length text field');
109 } db1
110 set res [list]
111 db2 eval {
112 SELECT a FROM numbers ORDER BY a;
113 } {
114 lappend res $a
115 if {$a==5} {
116 execsql {ROLLBACK} db1
117 }
118 }
119 set res
120} {1 2 3 4 5}
121
danielk1977ed429312006-01-19 08:43:31 +0000122db1 close
123db2 close
124
danielk19772b8c13e2006-01-24 14:21:24 +0000125do_test shared2-3.2 {
danielk19777246f5b2006-01-24 11:30:27 +0000126 sqlite3_thread_cleanup
127 sqlite3_enable_shared_cache 1
128} {0}
129
danielk1977ed429312006-01-19 08:43:31 +0000130sqlite3_enable_shared_cache $::enable_shared_cache
131finish_test