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 #***********************************************************************
11 # This file implements regression tests for TCL interface to the
14 # The focus of the tests in this file is the following interface:
16 # sqlite_commit_hook (tests hook-1..hook-3 inclusive)
17 # sqlite_update_hook (tests hook-4-*)
18 # sqlite_rollback_hook (tests hook-5.*)
20 # $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $
22 set testdir [file dirname $argv0]
23 source $testdir/tester.tcl
36 db commit_hook ::commit_hook
50 INSERT INTO t2 VALUES(1,2);
51 INSERT INTO t2 SELECT a+1, b+1 FROM t2;
52 INSERT INTO t2 SELECT a+2, b+2 FROM t2;
59 set ::commit_cnt [execsql {SELECT * FROM t2}]
63 INSERT INTO t2 VALUES(5,6);
66 } {1 2 2 3 3 4 4 5 5 6}
70 set ::commit_cnt [execsql {SELECT * FROM t2}]
74 INSERT INTO t2 VALUES(6,7);
76 } {1 {constraint failed}}
79 } {1 2 2 3 3 4 4 5 5 6 6 7}
81 execsql {SELECT * FROM t2}
82 } {1 2 2 3 3 4 4 5 5 6}
84 # Test turnning off the commit hook
90 INSERT INTO t2 VALUES(7,8);
98 file delete -force test2.db test2.db-journal
100 proc commit_hook {} {
101 set y [db2 one {SELECT y FROM t3 WHERE y>10}]
102 return [expr {$y>10}]
104 db2 eval {CREATE TABLE t3(x,y)}
105 db2 commit_hook commit_hook
106 catchsql {INSERT INTO t3 VALUES(1,2)} db2
107 catchsql {INSERT INTO t3 VALUES(11,12)} db2
108 catchsql {INSERT INTO t3 VALUES(3,4)} db2
110 SELECT * FROM t3 ORDER BY x;
116 #----------------------------------------------------------------------------
117 # Tests for the update-hook.
119 # 4.1.* - Very simple tests. Test that the update hook is invoked correctly
120 # for INSERT, DELETE and UPDATE statements, including DELETE
121 # statements with no WHERE clause.
122 # 4.2.* - Check that the update-hook is invoked for rows modified by trigger
123 # bodies. Also that the database name is correctly reported when
124 # an attached database is modified.
125 # 4.3.* - Do some sorting, grouping, compound queries, population and
126 # depopulation of indices, to make sure the update-hook is not
127 # invoked incorrectly.
136 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
137 INSERT INTO t1 VALUES(1, 'one');
138 INSERT INTO t1 VALUES(2, 'two');
139 INSERT INTO t1 VALUES(3, 'three');
141 db update_hook [list lappend ::update_hook]
145 INSERT INTO t1 VALUES(4, 'four');
146 DELETE FROM t1 WHERE b = 'two';
147 UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
148 DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
162 # Update hook is not invoked for changes to sqlite_master
167 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
185 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
186 CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
187 INSERT INTO t2 VALUES(new.a, new.b);
188 UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
189 DELETE FROM t2 WHERE new.a = c;
195 INSERT INTO t1 VALUES(1, 'one');
196 INSERT INTO t1 VALUES(2, 'two');
211 INSERT INTO t1 VALUES(1, 'one');
212 INSERT INTO t1 VALUES(2, 'two');
216 # Update-hook + ATTACH
220 file delete -force test2.db
222 ATTACH 'test2.db' AS aux;
223 CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
224 INSERT INTO aux.t3 SELECT * FROM t1;
225 UPDATE t3 SET b = 'two or so' WHERE a = 2;
226 DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
240 DROP TRIGGER t1_trigger;
244 # Test that other vdbe operations involving btree structures do not
245 # incorrectly invoke the update-hook.
249 CREATE INDEX t1_i ON t1(b);
250 INSERT INTO t1 VALUES(3, 'three');
251 UPDATE t1 SET b = '';
252 DELETE FROM t1 WHERE a > 1;
264 ifcapable compound&&attach {
267 SELECT * FROM t1 UNION SELECT * FROM t3;
268 SELECT * FROM t1 UNION ALL SELECT * FROM t3;
269 SELECT * FROM t1 INTERSECT SELECT * FROM t3;
270 SELECT * FROM t1 EXCEPT SELECT * FROM t3;
271 SELECT * FROM t1 ORDER BY b;
272 SELECT * FROM t1 GROUP BY b;
279 #----------------------------------------------------------------------------
281 #----------------------------------------------------------------------------
282 # Test the rollback-hook. The rollback-hook is a bit more complicated than
283 # either the commit or update hooks because a rollback can happen
284 # explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
287 # hook-5.1.* - Test explicit rollbacks.
288 # hook-5.2.* - Test implicit rollbacks caused by constraint failure.
290 # hook-5.3.* - Test implicit rollbacks caused by IO errors.
291 # hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
292 # hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
293 # not be called for these?
297 # Configure the rollback hook to increment global variable
298 # $::rollback_hook each time it is invoked.
299 set ::rollback_hook 0
300 db rollback_hook [list incr ::rollback_hook]
303 # Test explicit rollbacks. Not much can really go wrong here.
306 set ::rollback_hook 0
314 # Test implicit rollbacks caused by constraints.
317 set ::rollback_hook 0
320 CREATE TABLE t1(a PRIMARY KEY, b);
321 INSERT INTO t1 VALUES('one', 'I');
322 INSERT INTO t1 VALUES('one', 'I');
327 # Check that the INSERT transaction above really was rolled back.
329 SELECT count(*) FROM t1;
334 # End rollback-hook testing.
335 #----------------------------------------------------------------------------
337 #----------------------------------------------------------------------------
338 # Test that if a commit-hook returns non-zero (causing a rollback), the
339 # rollback-hook is invoked.
341 proc commit_hook {} {
342 lappend ::hooks COMMIT
345 proc rollback_hook {} {
346 lappend ::hooks ROLLBACK
350 db commit_hook commit_hook
351 db rollback_hook rollback_hook
354 INSERT INTO t1 VALUES('two', 'II');
357 execsql { SELECT * FROM t1 }