3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #***********************************************************************
12 # $Id: temptrigger.test,v 1.3 2009/04/15 13:07:19 drh Exp $
14 set testdir [file dirname $argv0]
15 source $testdir/tester.tcl
16 set testprefix temptrigger
18 ifcapable {!trigger || !shared_cache} { finish_test ; return }
22 # temptrigger-1.*: Shared cache problem.
23 # temptrigger-2.*: A similar shared cache problem.
24 # temptrigger-3.*: Attached database problem.
27 #-------------------------------------------------------------------------
28 # Test case temptrigger-1.* demonstrates a problem with temp triggers
29 # in shared-cache mode. If process 1 connections to a shared-cache and
30 # creates a temp trigger, the temp trigger is linked into the shared-cache
31 # schema. If process 2 reloads the shared-cache schema from disk, then
32 # it does not recreate the temp trigger belonging to process 1. From the
33 # point of view of process 1, the temp trigger just disappeared.
35 # temptrigger-1.1: In shared cache mode, create a table in the main
36 # database and add a temp trigger to it.
38 # temptrigger-1.2: Check that the temp trigger is correctly fired. Check
39 # that the temp trigger is not fired by statements
40 # executed by a second connection connected to the
43 # temptrigger-1.3: Using the second connection to the shared-cache, cause
44 # the shared-cache schema to be reloaded.
46 # temptrigger-1.4: Check that the temp trigger is still fired correctly.
48 # temptrigger-1.5: Check that the temp trigger can be dropped without error.
51 set ::enable_shared_cache [sqlite3_enable_shared_cache]
52 sqlite3_enable_shared_cache 1
57 do_test temptrigger-1.1 {
59 CREATE TABLE t1(a, b);
60 CREATE TEMP TABLE tt1(a, b);
61 CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
62 INSERT INTO tt1 VALUES(new.a, new.b);
67 do_test temptrigger-1.2.1 {
68 execsql { INSERT INTO t1 VALUES(1, 2) }
69 execsql { SELECT * FROM t1 }
71 do_test temptrigger-1.2.2 {
72 execsql { SELECT * FROM tt1 }
74 do_test temptrigger-1.2.3 {
75 execsql { INSERT INTO t1 VALUES(3, 4) } db2
76 execsql { SELECT * FROM t1 }
78 do_test temptrigger-1.2.4 {
79 execsql { SELECT * FROM tt1 }
82 # Cause the shared-cache schema to be reloaded.
84 do_test temptrigger-1.3 {
85 execsql { BEGIN; CREATE TABLE t3(a, b); ROLLBACK; } db2
88 do_test temptrigger-1.4 {
89 execsql { INSERT INTO t1 VALUES(5, 6) }
90 execsql { SELECT * FROM tt1 }
93 do_test temptrigger-1.5 {
94 # Before the bug was fixed, the following 'DROP TRIGGER' hit an
96 #execsql { DROP TRIGGER tr1 }
102 #-------------------------------------------------------------------------
103 # Tests temptrigger-2.* are similar to temptrigger-1.*, except that
104 # temptrigger-2.3 simply opens and closes a connection to the shared-cache.
105 # It does not do anything special to cause the schema to be reloaded.
107 do_test temptrigger-2.1 {
111 CREATE TEMP TABLE tt1(a, b);
112 CREATE TEMP TRIGGER tr1 AFTER INSERT ON t1 BEGIN
113 INSERT INTO tt1 VALUES(new.a, new.b);
117 do_test temptrigger-2.2 {
119 INSERT INTO t1 VALUES(10, 20);
123 do_test temptrigger-2.3 {
127 do_test temptrigger-2.4 {
129 INSERT INTO t1 VALUES(30, 40);
133 do_test temptrigger-2.5 {
134 #execsql { DROP TRIGGER tr1 }
139 sqlite3_enable_shared_cache $::enable_shared_cache
141 #-------------------------------------------------------------------------
142 # Test case temptrigger-3.* demonstrates a problem with temp triggers
143 # on tables located in attached databases. At one point when SQLite reloaded
144 # the schema of an attached database (because some other connection had
145 # changed the schema cookie) it was not re-creating temp triggers attached
146 # to tables located within the attached database.
148 # temptrigger-3.1: Attach database 'test2.db' to connection [db]. Add a
149 # temp trigger to a table in 'test2.db'.
151 # temptrigger-3.2: Check that the temp trigger is correctly fired.
153 # temptrigger-3.3: Update the schema of 'test2.db' using an external
154 # connection. This forces [db] to reload the 'test2.db'
155 # schema. Check that the temp trigger is still fired
158 # temptrigger-3.4: Check that the temp trigger can be dropped without error.
160 do_test temptrigger-3.1 {
161 catch { forcedelete test2.db test2.db-journal }
162 catch { forcedelete test.db test.db-journal }
165 execsql { CREATE TABLE t2(a, b) } db2
167 ATTACH 'test2.db' AS aux;
168 CREATE TEMP TABLE tt2(a, b);
169 CREATE TEMP TRIGGER tr2 AFTER INSERT ON aux.t2 BEGIN
170 INSERT INTO tt2 VALUES(new.a, new.b);
175 do_test temptrigger-3.2.1 {
177 INSERT INTO aux.t2 VALUES(1, 2);
178 SELECT * FROM aux.t2;
181 do_test temptrigger-3.2.2 {
182 execsql { SELECT * FROM tt2 }
185 do_test temptrigger-3.3.1 {
186 execsql { CREATE TABLE t3(a, b) } db2
188 INSERT INTO aux.t2 VALUES(3, 4);
189 SELECT * FROM aux.t2;
192 do_test temptrigger-3.3.2 {
193 execsql { SELECT * FROM tt2 }
196 do_test temptrigger-3.4 {
197 # Before the bug was fixed, the following 'DROP TRIGGER' hit an
198 # assert if executed.
199 #execsql { DROP TRIGGER tr2 }
206 #-------------------------------------------------------------------------
207 # Test that creating a temp table after a temp trigger on the same name
208 # has been created is an error.
211 do_execsql_test 4.0 {
213 CREATE TEMP TRIGGER tr1 BEFORE INSERT ON t1 BEGIN
218 do_execsql_test 4.1 {
219 CREATE TEMP TABLE t1(x);
222 #-------------------------------------------------------------------------
223 # Test that no harm is done if the table a temp trigger is attached to is
224 # deleted by an external connection.
227 do_execsql_test 5.0 {
229 CREATE TEMP TRIGGER tr1 BEFORE INSERT ON t1 BEGIN SELECT 1,2,3; END;
234 execsql { DROP TABLE t1 } db2
237 do_execsql_test 5.2 {
238 SELECT * FROM sqlite_master;
239 SELECT * FROM sqlite_temp_master;
242 {CREATE TRIGGER tr1 BEFORE INSERT ON t1 BEGIN SELECT 1,2,3; END}
246 #-------------------------------------------------------------------------
247 # Check that if a second connection creates a table in an attached database
248 # with the same name as a table in the main database that has a temp
249 # trigger attached to it nothing goes awry.
254 do_execsql_test 6.0 {
256 CREATE TEMP TRIGGER tr1 BEFORE INSERT ON t1 BEGIN
257 SELECT raise(ABORT, 'error');
259 ATTACH 'test.db2' AS aux;
264 execsql { CREATE TABLE t1(a, b, c); } db2
267 do_execsql_test 6.2 {
268 SELECT type,name,tbl_name,sql FROM aux.sqlite_master;
269 INSERT INTO aux.t1 VALUES(1,2,3);
271 table t1 t1 {CREATE TABLE t1(a, b, c)}
274 do_catchsql_test 6.3 {
275 INSERT INTO main.t1 VALUES(1);