xref: /sqlite-3.40.0/test/hook.test (revision bbbb0e80)
1# 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#
16#      sqlite_commit_hook    (tests hook-1..hook-3 inclusive)
17#      sqlite_update_hook    (tests hook-4-*)
18#      sqlite_rollback_hook  (tests hook-5.*)
19#
20# $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $
21
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 {} {
70    set ::commit_cnt [execsql {SELECT * FROM t2}]
71    return 1
72  }
73  catchsql {
74    INSERT INTO t2 VALUES(6,7);
75  }
76} {1 {constraint failed}}
77verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
78do_test hook-3.7 {
79  set ::commit_cnt
80} {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
85# 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} {}
95
96# Ticket #3564.
97#
98do_test hook-3.10 {
99  forcedelete test2.db test2.db-journal
100  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
117#----------------------------------------------------------------------------
118# Tests for the update-hook.
119#
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.
129#
130
131# Simple tests
132do_test hook-4.1.1 {
133  catchsql {
134    DROP TABLE t1;
135  }
136  execsql {
137    CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
138    CREATE TABLE t1w(a INT PRIMARY KEY, b) WITHOUT ROWID;
139    INSERT INTO t1 VALUES(1, 'one');
140    INSERT INTO t1 VALUES(2, 'two');
141    INSERT INTO t1 VALUES(3, 'three');
142    INSERT INTO t1w SELECT * FROM t1;
143  }
144  db update_hook [list lappend ::update_hook]
145} {}
146do_test hook-4.1.2 {
147  execsql {
148    INSERT INTO t1 VALUES(4, 'four');
149    DELETE FROM t1 WHERE b = 'two';
150    UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
151    DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
152  }
153  set ::update_hook
154} [list \
155    INSERT main t1 4 \
156    DELETE main t1 2 \
157    UPDATE main t1 1 \
158    UPDATE main t1 3 \
159    DELETE main t1 1 \
160    DELETE main t1 3 \
161    DELETE main t1 4 \
162]
163
164# EVIDENCE-OF: R-61808-14344 The sqlite3_update_hook() interface does
165# not fire callbacks for changes to a WITHOUT ROWID table.
166#
167do_test hook-4.1.2w {
168  set ::update_hook {}
169  execsql {
170    INSERT INTO t1w VALUES(4, 'four');
171    DELETE FROM t1w WHERE b = 'two';
172    UPDATE t1w SET b = '' WHERE a = 1 OR a = 3;
173    DELETE FROM t1w WHERE 1; -- Avoid the truncate optimization (for now)
174  }
175  set ::update_hook
176} {}
177
178ifcapable trigger {
179  # Update hook is not invoked for changes to sqlite_master
180  #
181  do_test hook-4.1.3 {
182    set ::update_hook {}
183    execsql {
184      CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
185    }
186    set ::update_hook
187  } {}
188  do_test hook-4.1.4 {
189    set ::update_hook {}
190    execsql {
191      DROP TRIGGER r1;
192    }
193    set ::update_hook
194  } {}
195
196  set ::update_hook {}
197  do_test hook-4.2.1 {
198    catchsql {
199      DROP TABLE t2;
200    }
201    execsql {
202      CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
203      CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
204        INSERT INTO t2 VALUES(new.a, new.b);
205        UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
206        DELETE FROM t2 WHERE new.a = c;
207      END;
208    }
209  } {}
210  do_test hook-4.2.2 {
211    execsql {
212      INSERT INTO t1 VALUES(1, 'one');
213      INSERT INTO t1 VALUES(2, 'two');
214    }
215    set ::update_hook
216  } [list \
217      INSERT main t1 1 \
218      INSERT main t2 1 \
219      UPDATE main t2 1 \
220      DELETE main t2 1 \
221      INSERT main t1 2 \
222      INSERT main t2 2 \
223      UPDATE main t2 2 \
224      DELETE main t2 2 \
225  ]
226} else {
227  execsql {
228    INSERT INTO t1 VALUES(1, 'one');
229    INSERT INTO t1 VALUES(2, 'two');
230  }
231}
232
233# Update-hook + ATTACH
234set ::update_hook {}
235ifcapable attach {
236  do_test hook-4.2.3 {
237    forcedelete test2.db
238    execsql {
239      ATTACH 'test2.db' AS aux;
240      CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
241      INSERT INTO aux.t3 SELECT * FROM t1;
242      UPDATE t3 SET b = 'two or so' WHERE a = 2;
243      DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
244    }
245    set ::update_hook
246  } [list \
247      INSERT aux t3 1 \
248      INSERT aux t3 2 \
249      UPDATE aux t3 2 \
250      DELETE aux t3 1 \
251      DELETE aux t3 2 \
252  ]
253}
254
255ifcapable trigger {
256  execsql {
257    DROP TRIGGER t1_trigger;
258  }
259}
260
261# Test that other vdbe operations involving btree structures do not
262# incorrectly invoke the update-hook.
263set ::update_hook {}
264do_test hook-4.3.1 {
265  execsql {
266    CREATE INDEX t1_i ON t1(b);
267    INSERT INTO t1 VALUES(3, 'three');
268    UPDATE t1 SET b = '';
269    DELETE FROM t1 WHERE a > 1;
270  }
271  set ::update_hook
272} [list \
273    INSERT main t1 3 \
274    UPDATE main t1 1 \
275    UPDATE main t1 2 \
276    UPDATE main t1 3 \
277    DELETE main t1 2 \
278    DELETE main t1 3 \
279]
280set ::update_hook {}
281ifcapable compound&&attach {
282  do_test hook-4.3.2 {
283    execsql {
284      SELECT * FROM t1 UNION SELECT * FROM t3;
285      SELECT * FROM t1 UNION ALL SELECT * FROM t3;
286      SELECT * FROM t1 INTERSECT SELECT * FROM t3;
287      SELECT * FROM t1 EXCEPT SELECT * FROM t3;
288      SELECT * FROM t1 ORDER BY b;
289      SELECT * FROM t1 GROUP BY b;
290    }
291    set ::update_hook
292  } [list]
293}
294
295do_test hook-4.4 {
296  execsql {
297    CREATE TABLE t4(a UNIQUE, b);
298    INSERT INTO t4 VALUES(1, 'a');
299    INSERT INTO t4 VALUES(2, 'b');
300  }
301  set ::update_hook [list]
302  execsql {
303    REPLACE INTO t4 VALUES(1, 'c');
304  }
305  set ::update_hook
306} [list INSERT main t4 3 ]
307do_execsql_test hook-4.4.1 {
308  SELECT * FROM t4 ORDER BY a;
309} {1 c 2 b}
310do_test hook-4.4.2 {
311  set ::update_hook [list]
312  execsql {
313    PRAGMA recursive_triggers = on;
314    REPLACE INTO t4 VALUES(1, 'd');
315  }
316  set ::update_hook
317} [list INSERT main t4 4 ]
318do_execsql_test hook-4.4.3 {
319  SELECT * FROM t4 ORDER BY a;
320} {1 d 2 b}
321
322db update_hook {}
323#
324#----------------------------------------------------------------------------
325
326#----------------------------------------------------------------------------
327# Test the rollback-hook. The rollback-hook is a bit more complicated than
328# either the commit or update hooks because a rollback can happen
329# explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
330# error condition).
331#
332# hook-5.1.* - Test explicit rollbacks.
333# hook-5.2.* - Test implicit rollbacks caused by constraint failure.
334#
335# hook-5.3.* - Test implicit rollbacks caused by IO errors.
336# hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
337# hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
338#              not be called for these?
339#
340
341do_test hook-5.0 {
342  # Configure the rollback hook to increment global variable
343  # $::rollback_hook each time it is invoked.
344  set ::rollback_hook 0
345  db rollback_hook [list incr ::rollback_hook]
346} {}
347
348# Test explicit rollbacks. Not much can really go wrong here.
349#
350do_test hook-5.1.1 {
351  set ::rollback_hook 0
352  execsql {
353    BEGIN;
354    ROLLBACK;
355  }
356  set ::rollback_hook
357} {1}
358
359# Test implicit rollbacks caused by constraints.
360#
361do_test hook-5.2.1 {
362  set ::rollback_hook 0
363  catchsql {
364    DROP TABLE t1;
365    CREATE TABLE t1(a PRIMARY KEY, b);
366    INSERT INTO t1 VALUES('one', 'I');
367    INSERT INTO t1 VALUES('one', 'I');
368  }
369  set ::rollback_hook
370} {1}
371do_test hook-5.2.2 {
372  # Check that the INSERT transaction above really was rolled back.
373  execsql {
374    SELECT count(*) FROM t1;
375  }
376} {1}
377
378#
379# End rollback-hook testing.
380#----------------------------------------------------------------------------
381
382#----------------------------------------------------------------------------
383# Test that if a commit-hook returns non-zero (causing a rollback), the
384# rollback-hook is invoked.
385#
386proc commit_hook {} {
387  lappend ::hooks COMMIT
388  return 1
389}
390proc rollback_hook {} {
391  lappend ::hooks ROLLBACK
392}
393do_test hook-6.1 {
394  set ::hooks [list]
395  db commit_hook commit_hook
396  db rollback_hook rollback_hook
397  catchsql {
398    BEGIN;
399      INSERT INTO t1 VALUES('two', 'II');
400    COMMIT;
401  }
402  execsql { SELECT * FROM t1 }
403} {one I}
404do_test hook-6.2 {
405  set ::hooks
406} {COMMIT ROLLBACK}
407unset ::hooks
408
409finish_test
410