xref: /sqlite-3.40.0/test/shared9.test (revision a2ebe4b4)
1# 2012 October 5
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#
12# The tests in this file are intended to show if two connections attach
13# to the same shared cache using different database names, views and
14# virtual tables may still be accessed.
15#
16
17set testdir [file dirname $argv0]
18source $testdir/tester.tcl
19set testprefix shared9
20
21ifcapable !view||!trigger {
22  finish_test
23  return
24}
25
26db close
27set enable_shared_cache [sqlite3_enable_shared_cache 1]
28
29sqlite3 db1 test.db
30sqlite3 db2 test.db
31forcedelete test.db2
32
33do_test 1.1 {
34  db1 eval {
35    ATTACH 'test.db2' AS 'fred';
36    CREATE TABLE fred.t1(a, b, c);
37    CREATE VIEW fred.v1 AS SELECT * FROM t1;
38
39    CREATE TABLE fred.t2(a, b);
40    CREATE TABLE fred.t3(a, b);
41    CREATE TRIGGER fred.trig AFTER INSERT ON t2 BEGIN
42      DELETE FROM t3;
43      INSERT INTO t3 SELECT * FROM t2;
44    END;
45    INSERT INTO t2 VALUES(1, 2);
46    SELECT * FROM t3;
47  }
48} {1 2}
49
50do_test 1.2 { db2 eval "ATTACH 'test.db2' AS 'jones'" } {}
51do_test 1.3 { db2 eval "SELECT * FROM v1"             } {}
52do_test 1.4 { db2 eval "INSERT INTO t2 VALUES(3, 4)"  } {}
53
54ifcapable fts3 {
55  do_test 1.5 {
56    db1 eval {
57      CREATE VIRTUAL TABLE fred.t4 USING fts4;
58      INSERT INTO t4 VALUES('hello world');
59    }
60  } {}
61
62  do_test 1.6 {
63    db2 eval {
64      INSERT INTO t4 VALUES('shared cache');
65      SELECT * FROM t4 WHERE t4 MATCH 'hello';
66    }
67  } {{hello world}}
68
69  do_test 1.7 {
70    db1 eval {
71      SELECT * FROM t4 WHERE t4 MATCH 'c*';
72    }
73  } {{shared cache}}
74}
75
76db1 close
77db2 close
78
79#-------------------------------------------------------------------------
80# The following tests attempt to find a similar problem with collation
81# sequence names - pointers to database handle specific allocations leaking
82# into schema objects and being used after the original handle has been
83# closed.
84#
85forcedelete test.db test.db2
86sqlite3 db1 test.db
87sqlite3 db2 test.db
88foreach x {collate1 collate2 collate3} {
89  proc $x {a b} { string compare $a $b }
90  db1 collate $x $x
91  db2 collate $x $x
92}
93do_test 2.1 {
94  db1 eval {
95    CREATE TABLE t1(a, b, c COLLATE collate1);
96    CREATE INDEX i1 ON t1(a COLLATE collate2, c, b);
97  }
98} {}
99do_test 2.2 {
100  db1 close
101  db2 eval "INSERT INTO t1 VALUES('abc', 'def', 'ghi')"
102} {}
103db2 close
104
105#-------------------------------------------------------------------------
106# At one point, the following would cause a collation sequence belonging
107# to connection [db1] to be invoked by a call to [db2 eval]. Which is a
108# problem if [db1] has already been closed.
109#
110forcedelete test.db test.db2
111sqlite3 db1 test.db
112sqlite3 db2 test.db
113
114proc mycollate_db1 {a b} {set ::invoked_mycollate_db1 1 ; string compare $a $b}
115proc mycollate_db2 {a b} {string compare $a $b}
116
117db1 collate mycollate mycollate_db1
118db2 collate mycollate mycollate_db2
119
120do_test 2.3 {
121  set ::invoked_mycollate_db1 0
122  db1 eval {
123    CREATE TABLE t1(a COLLATE mycollate, CHECK (a IN ('one', 'two', 'three')));
124    INSERT INTO t1 VALUES('one');
125  }
126  db1 close
127  set ::invoked_mycollate_db1
128} {1}
129do_test 2.4 {
130  set ::invoked_mycollate_db1 0
131  db2 eval {
132    INSERT INTO t1 VALUES('two');
133  }
134  db2 close
135  set ::invoked_mycollate_db1
136} {0}
137
138sqlite3_enable_shared_cache $::enable_shared_cache
139finish_test
140
141