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}}
77 verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
80 } {1 2 2 3 3 4 4 5 5 6 6 7}
82 execsql {SELECT * FROM t2}
83 } {1 2 2 3 3 4 4 5 5 6}
85 # Test turnning off the commit hook
91 INSERT INTO t2 VALUES(7,8);
99 forcedelete test2.db test2.db-journal
101 proc commit_hook {} {
102 set y [db2 one {SELECT y FROM t3 WHERE y>10}]
103 return [expr {$y>10}]
105 db2 eval {CREATE TABLE t3(x,y)}
106 db2 commit_hook commit_hook
107 catchsql {INSERT INTO t3 VALUES(1,2)} db2
108 catchsql {INSERT INTO t3 VALUES(11,12)} db2
109 catchsql {INSERT INTO t3 VALUES(3,4)} db2
111 SELECT * FROM t3 ORDER BY x;
117 #----------------------------------------------------------------------------
118 # Tests for the update-hook.
120 # 4.1.* - Very simple tests. Test that the update hook is invoked correctly
121 # for INSERT, DELETE and UPDATE statements, including DELETE
122 # statements with no WHERE clause.
123 # 4.2.* - Check that the update-hook is invoked for rows modified by trigger
124 # bodies. Also that the database name is correctly reported when
125 # an attached database is modified.
126 # 4.3.* - Do some sorting, grouping, compound queries, population and
127 # depopulation of indices, to make sure the update-hook is not
128 # invoked incorrectly.
130 # EVIDENCE-OF: R-21999-45122 The sqlite3_update_hook() interface
131 # registers a callback function with the database connection identified
132 # by the first argument to be invoked whenever a row is updated,
133 # inserted or deleted in a rowid table.
136 do_test hook-4.1.1a {
140 unset -nocomplain ::update_hook
142 db update_hook [list lappend ::update_hook]
144 # EVIDENCE-OF: R-52223-27275 The update hook is not invoked when
145 # internal system tables are modified (i.e. sqlite_master and
149 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
150 CREATE TABLE t1w(a INT PRIMARY KEY, b) WITHOUT ROWID;
154 do_test hook-4.1.1b {
156 INSERT INTO t1 VALUES(1, 'one');
157 INSERT INTO t1 VALUES(2, 'two');
158 INSERT INTO t1 VALUES(3, 'three');
159 INSERT INTO t1w SELECT * FROM t1;
163 # EVIDENCE-OF: R-15506-57666 The second callback argument is one of
164 # SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE, depending on the
165 # operation that caused the callback to be invoked.
167 # EVIDENCE-OF: R-29213-61195 The third and fourth arguments to the
168 # callback contain pointers to the database and table name containing
171 # EVIDENCE-OF: R-30809-57812 The final callback parameter is the rowid
177 INSERT INTO t1 VALUES(4, 'four');
178 DELETE FROM t1 WHERE b = 'two';
179 UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
180 DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
193 # EVIDENCE-OF: R-61808-14344 The sqlite3_update_hook() interface does
194 # not fire callbacks for changes to a WITHOUT ROWID table.
196 # EVIDENCE-OF: R-33257-44249 The update hook is not invoked when WITHOUT
197 # ROWID tables are modified.
199 do_test hook-4.1.2w {
202 INSERT INTO t1w VALUES(4, 'four');
203 DELETE FROM t1w WHERE b = 'two';
204 UPDATE t1w SET b = '' WHERE a = 1 OR a = 3;
205 DELETE FROM t1w WHERE 1; -- Avoid the truncate optimization (for now)
211 # Update hook is not invoked for changes to sqlite_master
216 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
234 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
235 CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
236 INSERT INTO t2 VALUES(new.a, new.b);
237 UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
238 DELETE FROM t2 WHERE new.a = c;
244 INSERT INTO t1 VALUES(1, 'one');
245 INSERT INTO t1 VALUES(2, 'two');
260 INSERT INTO t1 VALUES(1, 'one');
261 INSERT INTO t1 VALUES(2, 'two');
265 # Update-hook + ATTACH
271 ATTACH 'test2.db' AS aux;
272 CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
273 INSERT INTO aux.t3 SELECT * FROM t1;
274 UPDATE t3 SET b = 'two or so' WHERE a = 2;
275 DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
289 DROP TRIGGER t1_trigger;
293 # Test that other vdbe operations involving btree structures do not
294 # incorrectly invoke the update-hook.
298 CREATE INDEX t1_i ON t1(b);
299 INSERT INTO t1 VALUES(3, 'three');
300 UPDATE t1 SET b = '';
301 DELETE FROM t1 WHERE a > 1;
313 ifcapable compound&&attach {
316 SELECT * FROM t1 UNION SELECT * FROM t3;
317 SELECT * FROM t1 UNION ALL SELECT * FROM t3;
318 SELECT * FROM t1 INTERSECT SELECT * FROM t3;
319 SELECT * FROM t1 EXCEPT SELECT * FROM t3;
320 SELECT * FROM t1 ORDER BY b;
321 SELECT * FROM t1 GROUP BY b;
329 CREATE TABLE t4(a UNIQUE, b);
330 INSERT INTO t4 VALUES(1, 'a');
331 INSERT INTO t4 VALUES(2, 'b');
333 set ::update_hook [list]
335 REPLACE INTO t4 VALUES(1, 'c');
338 } [list INSERT main t4 3 ]
339 do_execsql_test hook-4.4.1 {
340 SELECT * FROM t4 ORDER BY a;
343 set ::update_hook [list]
345 PRAGMA recursive_triggers = on;
346 REPLACE INTO t4 VALUES(1, 'd');
349 } [list INSERT main t4 4 ]
350 do_execsql_test hook-4.4.3 {
351 SELECT * FROM t4 ORDER BY a;
356 #----------------------------------------------------------------------------
358 #----------------------------------------------------------------------------
359 # Test the rollback-hook. The rollback-hook is a bit more complicated than
360 # either the commit or update hooks because a rollback can happen
361 # explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
364 # hook-5.1.* - Test explicit rollbacks.
365 # hook-5.2.* - Test implicit rollbacks caused by constraint failure.
367 # hook-5.3.* - Test implicit rollbacks caused by IO errors.
368 # hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
369 # hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
370 # not be called for these?
374 # Configure the rollback hook to increment global variable
375 # $::rollback_hook each time it is invoked.
376 set ::rollback_hook 0
377 db rollback_hook [list incr ::rollback_hook]
380 # Test explicit rollbacks. Not much can really go wrong here.
383 set ::rollback_hook 0
391 # Test implicit rollbacks caused by constraints.
394 set ::rollback_hook 0
397 CREATE TABLE t1(a PRIMARY KEY, b);
398 INSERT INTO t1 VALUES('one', 'I');
399 INSERT INTO t1 VALUES('one', 'I');
404 # Check that the INSERT transaction above really was rolled back.
406 SELECT count(*) FROM t1;
411 # End rollback-hook testing.
412 #----------------------------------------------------------------------------
414 #----------------------------------------------------------------------------
415 # Test that if a commit-hook returns non-zero (causing a rollback), the
416 # rollback-hook is invoked.
418 proc commit_hook {} {
419 lappend ::hooks COMMIT
422 proc rollback_hook {} {
423 lappend ::hooks ROLLBACK
427 db commit_hook commit_hook
428 db rollback_hook rollback_hook
431 INSERT INTO t1 VALUES('two', 'II');
434 execsql { SELECT * FROM t1 }