blob: 6346cc77a7a8239611ad84a4174011f4febdd71a [file] [log] [blame]
drhaa940ea2004-01-15 02:44:03 +00001# 2004 Jan 14
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# This file implements regression tests for TCL interface to the
12# SQLite library.
13#
14# The focus of the tests in this file is the following interface:
15#
danielk197771fd80b2005-12-16 06:54:01 +000016# sqlite_commit_hook (tests hook-1..hook-3 inclusive)
17# sqlite_update_hook (tests hook-4-*)
18# sqlite_rollback_hook (tests hook-5.*)
drhaa940ea2004-01-15 02:44:03 +000019#
danielk19772943c372009-04-07 14:14:22 +000020# $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $
drhaa940ea2004-01-15 02:44:03 +000021
22set testdir [file dirname $argv0]
23source $testdir/tester.tcl
24
25do_test hook-1.2 {
26 db commit_hook
27} {}
28
29
30do_test hook-3.1 {
31 set commit_cnt 0
32 proc commit_hook {} {
33 incr ::commit_cnt
34 return 0
35 }
36 db commit_hook ::commit_hook
37 db commit_hook
38} {::commit_hook}
39do_test hook-3.2 {
40 set commit_cnt
41} {0}
42do_test hook-3.3 {
43 execsql {
44 CREATE TABLE t2(a,b);
45 }
46 set commit_cnt
47} {1}
48do_test hook-3.4 {
49 execsql {
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;
53 }
54 set commit_cnt
55} {4}
56do_test hook-3.5 {
57 set commit_cnt {}
58 proc commit_hook {} {
59 set ::commit_cnt [execsql {SELECT * FROM t2}]
60 return 0
61 }
62 execsql {
63 INSERT INTO t2 VALUES(5,6);
64 }
65 set commit_cnt
66} {1 2 2 3 3 4 4 5 5 6}
67do_test hook-3.6 {
68 set commit_cnt {}
69 proc commit_hook {} {
danielk19771d850a72004-05-31 08:26:49 +000070 set ::commit_cnt [execsql {SELECT * FROM t2}]
drhaa940ea2004-01-15 02:44:03 +000071 return 1
72 }
73 catchsql {
74 INSERT INTO t2 VALUES(6,7);
75 }
76} {1 {constraint failed}}
drh433dccf2013-02-09 15:37:11 +000077verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
drhaa940ea2004-01-15 02:44:03 +000078do_test hook-3.7 {
danielk19771d850a72004-05-31 08:26:49 +000079 set ::commit_cnt
drhaa940ea2004-01-15 02:44:03 +000080} {1 2 2 3 3 4 4 5 5 6 6 7}
81do_test hook-3.8 {
82 execsql {SELECT * FROM t2}
83} {1 2 2 3 3 4 4 5 5 6}
84
drh0f14e2e2004-06-29 12:39:08 +000085# Test turnning off the commit hook
86#
87do_test hook-3.9 {
88 db commit_hook {}
89 set ::commit_cnt {}
90 execsql {
91 INSERT INTO t2 VALUES(7,8);
92 }
93 set ::commit_cnt
94} {}
drhaa940ea2004-01-15 02:44:03 +000095
drh853799a2009-01-03 14:04:38 +000096# Ticket #3564.
97#
98do_test hook-3.10 {
mistachkinfda06be2011-08-02 00:57:34 +000099 forcedelete test2.db test2.db-journal
drh853799a2009-01-03 14:04:38 +0000100 sqlite3 db2 test2.db
101 proc commit_hook {} {
102 set y [db2 one {SELECT y FROM t3 WHERE y>10}]
103 return [expr {$y>10}]
104 }
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
110 db2 eval {
111 SELECT * FROM t3 ORDER BY x;
112 }
113} {1 2 3 4}
114db2 close
115
116
danielk197771fd80b2005-12-16 06:54:01 +0000117#----------------------------------------------------------------------------
118# Tests for the update-hook.
danielk197794eb6a12005-12-15 15:22:08 +0000119#
danielk197771fd80b2005-12-16 06:54:01 +0000120# 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.
129#
130
131# Simple tests
132do_test hook-4.1.1 {
danielk197794eb6a12005-12-15 15:22:08 +0000133 catchsql {
134 DROP TABLE t1;
135 }
136 execsql {
137 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
138 INSERT INTO t1 VALUES(1, 'one');
139 INSERT INTO t1 VALUES(2, 'two');
140 INSERT INTO t1 VALUES(3, 'three');
141 }
142 db update_hook [list lappend ::update_hook]
143} {}
danielk197771fd80b2005-12-16 06:54:01 +0000144do_test hook-4.1.2 {
danielk197794eb6a12005-12-15 15:22:08 +0000145 execsql {
146 INSERT INTO t1 VALUES(4, 'four');
147 DELETE FROM t1 WHERE b = 'two';
148 UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
drhd78901d2006-01-05 23:42:50 +0000149 DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
danielk197794eb6a12005-12-15 15:22:08 +0000150 }
151 set ::update_hook
152} [list \
153 INSERT main t1 4 \
154 DELETE main t1 2 \
155 UPDATE main t1 1 \
156 UPDATE main t1 3 \
157 DELETE main t1 1 \
158 DELETE main t1 3 \
159 DELETE main t1 4 \
160]
161
danielk19773bdca9c2006-01-17 09:35:01 +0000162ifcapable trigger {
danielk19772943c372009-04-07 14:14:22 +0000163 # Update hook is not invoked for changes to sqlite_master
164 #
165 do_test hook-4.1.3 {
166 set ::update_hook {}
167 execsql {
168 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
169 }
170 set ::update_hook
171 } {}
172 do_test hook-4.1.4 {
173 set ::update_hook {}
174 execsql {
175 DROP TRIGGER r1;
176 }
177 set ::update_hook
178 } {}
179
180 set ::update_hook {}
danielk19773bdca9c2006-01-17 09:35:01 +0000181 do_test hook-4.2.1 {
182 catchsql {
183 DROP TABLE t2;
184 }
185 execsql {
186 CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
187 CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
188 INSERT INTO t2 VALUES(new.a, new.b);
189 UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
190 DELETE FROM t2 WHERE new.a = c;
191 END;
192 }
193 } {}
194 do_test hook-4.2.2 {
195 execsql {
196 INSERT INTO t1 VALUES(1, 'one');
197 INSERT INTO t1 VALUES(2, 'two');
198 }
199 set ::update_hook
200 } [list \
201 INSERT main t1 1 \
202 INSERT main t2 1 \
203 UPDATE main t2 1 \
204 DELETE main t2 1 \
205 INSERT main t1 2 \
206 INSERT main t2 2 \
207 UPDATE main t2 2 \
208 DELETE main t2 2 \
209 ]
210} else {
danielk197794eb6a12005-12-15 15:22:08 +0000211 execsql {
212 INSERT INTO t1 VALUES(1, 'one');
213 INSERT INTO t1 VALUES(2, 'two');
214 }
danielk19773bdca9c2006-01-17 09:35:01 +0000215}
danielk197794eb6a12005-12-15 15:22:08 +0000216
danielk19773bdca9c2006-01-17 09:35:01 +0000217# Update-hook + ATTACH
danielk197794eb6a12005-12-15 15:22:08 +0000218set ::update_hook {}
danielk19775a8f9372007-10-09 08:29:32 +0000219ifcapable attach {
220 do_test hook-4.2.3 {
mistachkinfda06be2011-08-02 00:57:34 +0000221 forcedelete test2.db
danielk19775a8f9372007-10-09 08:29:32 +0000222 execsql {
223 ATTACH 'test2.db' AS aux;
224 CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
225 INSERT INTO aux.t3 SELECT * FROM t1;
226 UPDATE t3 SET b = 'two or so' WHERE a = 2;
227 DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
228 }
229 set ::update_hook
230 } [list \
231 INSERT aux t3 1 \
232 INSERT aux t3 2 \
233 UPDATE aux t3 2 \
234 DELETE aux t3 1 \
235 DELETE aux t3 2 \
236 ]
237}
danielk197794eb6a12005-12-15 15:22:08 +0000238
danielk19773bdca9c2006-01-17 09:35:01 +0000239ifcapable trigger {
240 execsql {
241 DROP TRIGGER t1_trigger;
242 }
243}
244
danielk197771fd80b2005-12-16 06:54:01 +0000245# Test that other vdbe operations involving btree structures do not
246# incorrectly invoke the update-hook.
danielk197794eb6a12005-12-15 15:22:08 +0000247set ::update_hook {}
danielk197771fd80b2005-12-16 06:54:01 +0000248do_test hook-4.3.1 {
danielk197794eb6a12005-12-15 15:22:08 +0000249 execsql {
danielk197794eb6a12005-12-15 15:22:08 +0000250 CREATE INDEX t1_i ON t1(b);
251 INSERT INTO t1 VALUES(3, 'three');
252 UPDATE t1 SET b = '';
253 DELETE FROM t1 WHERE a > 1;
254 }
255 set ::update_hook
256} [list \
257 INSERT main t1 3 \
258 UPDATE main t1 1 \
259 UPDATE main t1 2 \
260 UPDATE main t1 3 \
261 DELETE main t1 2 \
262 DELETE main t1 3 \
263]
264set ::update_hook {}
danielk19775a8f9372007-10-09 08:29:32 +0000265ifcapable compound&&attach {
danielk1977ff890792006-01-16 16:24:25 +0000266 do_test hook-4.3.2 {
267 execsql {
268 SELECT * FROM t1 UNION SELECT * FROM t3;
269 SELECT * FROM t1 UNION ALL SELECT * FROM t3;
270 SELECT * FROM t1 INTERSECT SELECT * FROM t3;
271 SELECT * FROM t1 EXCEPT SELECT * FROM t3;
272 SELECT * FROM t1 ORDER BY b;
273 SELECT * FROM t1 GROUP BY b;
274 }
275 set ::update_hook
276 } [list]
277}
dan382874f2011-05-28 15:57:40 +0000278
279do_test hook-4.4 {
280 execsql {
281 CREATE TABLE t4(a UNIQUE, b);
282 INSERT INTO t4 VALUES(1, 'a');
283 INSERT INTO t4 VALUES(2, 'b');
284 }
285 set ::update_hook [list]
286 execsql {
287 REPLACE INTO t4 VALUES(1, 'c');
288 }
289 set ::update_hook
290} [list INSERT main t4 3 ]
291do_execsql_test hook-4.4.1 {
292 SELECT * FROM t4 ORDER BY a;
293} {1 c 2 b}
294do_test hook-4.4.2 {
295 set ::update_hook [list]
296 execsql {
297 PRAGMA recursive_triggers = on;
298 REPLACE INTO t4 VALUES(1, 'd');
299 }
300 set ::update_hook
301} [list INSERT main t4 4 ]
302do_execsql_test hook-4.4.3 {
303 SELECT * FROM t4 ORDER BY a;
304} {1 d 2 b}
305
danielk197771fd80b2005-12-16 06:54:01 +0000306db update_hook {}
307#
308#----------------------------------------------------------------------------
309
310#----------------------------------------------------------------------------
311# Test the rollback-hook. The rollback-hook is a bit more complicated than
312# either the commit or update hooks because a rollback can happen
313# explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
314# error condition).
315#
316# hook-5.1.* - Test explicit rollbacks.
danielk1977f3f06bb2005-12-16 15:24:28 +0000317# hook-5.2.* - Test implicit rollbacks caused by constraint failure.
318#
319# hook-5.3.* - Test implicit rollbacks caused by IO errors.
320# hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
321# hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
322# not be called for these?
danielk197771fd80b2005-12-16 06:54:01 +0000323#
324
325do_test hook-5.0 {
326 # Configure the rollback hook to increment global variable
327 # $::rollback_hook each time it is invoked.
328 set ::rollback_hook 0
329 db rollback_hook [list incr ::rollback_hook]
330} {}
331
332# Test explicit rollbacks. Not much can really go wrong here.
danielk1977f3f06bb2005-12-16 15:24:28 +0000333#
danielk197771fd80b2005-12-16 06:54:01 +0000334do_test hook-5.1.1 {
335 set ::rollback_hook 0
336 execsql {
337 BEGIN;
338 ROLLBACK;
339 }
340 set ::rollback_hook
341} {1}
342
343# Test implicit rollbacks caused by constraints.
danielk1977f3f06bb2005-12-16 15:24:28 +0000344#
danielk197771fd80b2005-12-16 06:54:01 +0000345do_test hook-5.2.1 {
346 set ::rollback_hook 0
347 catchsql {
348 DROP TABLE t1;
349 CREATE TABLE t1(a PRIMARY KEY, b);
350 INSERT INTO t1 VALUES('one', 'I');
351 INSERT INTO t1 VALUES('one', 'I');
352 }
353 set ::rollback_hook
354} {1}
355do_test hook-5.2.2 {
356 # Check that the INSERT transaction above really was rolled back.
357 execsql {
358 SELECT count(*) FROM t1;
359 }
360} {1}
361
362#
danielk1977f3f06bb2005-12-16 15:24:28 +0000363# End rollback-hook testing.
danielk197771fd80b2005-12-16 06:54:01 +0000364#----------------------------------------------------------------------------
danielk197794eb6a12005-12-15 15:22:08 +0000365
danc9206ed2010-04-13 06:18:02 +0000366#----------------------------------------------------------------------------
367# Test that if a commit-hook returns non-zero (causing a rollback), the
368# rollback-hook is invoked.
369#
370proc commit_hook {} {
371 lappend ::hooks COMMIT
372 return 1
373}
374proc rollback_hook {} {
375 lappend ::hooks ROLLBACK
376}
377do_test hook-6.1 {
378 set ::hooks [list]
379 db commit_hook commit_hook
380 db rollback_hook rollback_hook
381 catchsql {
382 BEGIN;
383 INSERT INTO t1 VALUES('two', 'II');
384 COMMIT;
385 }
386 execsql { SELECT * FROM t1 }
387} {one I}
388do_test hook-6.2 {
389 set ::hooks
390} {COMMIT ROLLBACK}
391unset ::hooks
392
drhaa940ea2004-01-15 02:44:03 +0000393finish_test