1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
# 2004 Jan 14
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for TCL interface to the
# SQLite library.
#
# The focus of the tests in this file is the following interface:
#
# sqlite_commit_hook (tests hook-1..hook-3 inclusive)
# sqlite_update_hook (tests hook-4-*)
# sqlite_rollback_hook (tests hook-5.*)
#
# $Id: hook.test,v 1.15 2009/04/07 14:14:23 danielk1977 Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
do_test hook-1.2 {
db commit_hook
} {}
do_test hook-3.1 {
set commit_cnt 0
proc commit_hook {} {
incr ::commit_cnt
return 0
}
db commit_hook ::commit_hook
db commit_hook
} {::commit_hook}
do_test hook-3.2 {
set commit_cnt
} {0}
do_test hook-3.3 {
execsql {
CREATE TABLE t2(a,b);
}
set commit_cnt
} {1}
do_test hook-3.4 {
execsql {
INSERT INTO t2 VALUES(1,2);
INSERT INTO t2 SELECT a+1, b+1 FROM t2;
INSERT INTO t2 SELECT a+2, b+2 FROM t2;
}
set commit_cnt
} {4}
do_test hook-3.5 {
set commit_cnt {}
proc commit_hook {} {
set ::commit_cnt [execsql {SELECT * FROM t2}]
return 0
}
execsql {
INSERT INTO t2 VALUES(5,6);
}
set commit_cnt
} {1 2 2 3 3 4 4 5 5 6}
do_test hook-3.6 {
set commit_cnt {}
proc commit_hook {} {
set ::commit_cnt [execsql {SELECT * FROM t2}]
return 1
}
catchsql {
INSERT INTO t2 VALUES(6,7);
}
} {1 {constraint failed}}
verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
do_test hook-3.7 {
set ::commit_cnt
} {1 2 2 3 3 4 4 5 5 6 6 7}
do_test hook-3.8 {
execsql {SELECT * FROM t2}
} {1 2 2 3 3 4 4 5 5 6}
# Test turnning off the commit hook
#
do_test hook-3.9 {
db commit_hook {}
set ::commit_cnt {}
execsql {
INSERT INTO t2 VALUES(7,8);
}
set ::commit_cnt
} {}
# Ticket #3564.
#
do_test hook-3.10 {
forcedelete test2.db test2.db-journal
sqlite3 db2 test2.db
proc commit_hook {} {
set y [db2 one {SELECT y FROM t3 WHERE y>10}]
return [expr {$y>10}]
}
db2 eval {CREATE TABLE t3(x,y)}
db2 commit_hook commit_hook
catchsql {INSERT INTO t3 VALUES(1,2)} db2
catchsql {INSERT INTO t3 VALUES(11,12)} db2
catchsql {INSERT INTO t3 VALUES(3,4)} db2
db2 eval {
SELECT * FROM t3 ORDER BY x;
}
} {1 2 3 4}
db2 close
#----------------------------------------------------------------------------
# Tests for the update-hook.
#
# 4.1.* - Very simple tests. Test that the update hook is invoked correctly
# for INSERT, DELETE and UPDATE statements, including DELETE
# statements with no WHERE clause.
# 4.2.* - Check that the update-hook is invoked for rows modified by trigger
# bodies. Also that the database name is correctly reported when
# an attached database is modified.
# 4.3.* - Do some sorting, grouping, compound queries, population and
# depopulation of indices, to make sure the update-hook is not
# invoked incorrectly.
#
# EVIDENCE-OF: R-21999-45122 The sqlite3_update_hook() interface
# registers a callback function with the database connection identified
# by the first argument to be invoked whenever a row is updated,
# inserted or deleted in a rowid table.
# Simple tests
do_test hook-4.1.1a {
catchsql {
DROP TABLE t1;
}
unset -nocomplain ::update_hook
set ::update_hook {}
db update_hook [list lappend ::update_hook]
#
# EVIDENCE-OF: R-52223-27275 The update hook is not invoked when
# internal system tables are modified (i.e. sqlite_master and
# sqlite_sequence).
#
execsql {
CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
CREATE TABLE t1w(a INT PRIMARY KEY, b) WITHOUT ROWID;
}
set ::update_hook
} {}
do_test hook-4.1.1b {
execsql {
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
INSERT INTO t1 VALUES(3, 'three');
INSERT INTO t1w SELECT * FROM t1;
}
} {}
# EVIDENCE-OF: R-15506-57666 The second callback argument is one of
# SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE, depending on the
# operation that caused the callback to be invoked.
#
# EVIDENCE-OF: R-29213-61195 The third and fourth arguments to the
# callback contain pointers to the database and table name containing
# the affected row.
#
# EVIDENCE-OF: R-30809-57812 The final callback parameter is the rowid
# of the row.
#
do_test hook-4.1.2 {
set ::update_hook {}
execsql {
INSERT INTO t1 VALUES(4, 'four');
DELETE FROM t1 WHERE b = 'two';
UPDATE t1 SET b = '' WHERE a = 1 OR a = 3;
DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now)
}
set ::update_hook
} [list \
INSERT main t1 4 \
DELETE main t1 2 \
UPDATE main t1 1 \
UPDATE main t1 3 \
DELETE main t1 1 \
DELETE main t1 3 \
DELETE main t1 4 \
]
# EVIDENCE-OF: R-61808-14344 The sqlite3_update_hook() interface does
# not fire callbacks for changes to a WITHOUT ROWID table.
#
# EVIDENCE-OF: R-33257-44249 The update hook is not invoked when WITHOUT
# ROWID tables are modified.
#
do_test hook-4.1.2w {
set ::update_hook {}
execsql {
INSERT INTO t1w VALUES(4, 'four');
DELETE FROM t1w WHERE b = 'two';
UPDATE t1w SET b = '' WHERE a = 1 OR a = 3;
DELETE FROM t1w WHERE 1; -- Avoid the truncate optimization (for now)
}
set ::update_hook
} {}
ifcapable trigger {
# Update hook is not invoked for changes to sqlite_master
#
do_test hook-4.1.3 {
set ::update_hook {}
execsql {
CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END;
}
set ::update_hook
} {}
do_test hook-4.1.4 {
set ::update_hook {}
execsql {
DROP TRIGGER r1;
}
set ::update_hook
} {}
set ::update_hook {}
do_test hook-4.2.1 {
catchsql {
DROP TABLE t2;
}
execsql {
CREATE TABLE t2(c INTEGER PRIMARY KEY, d);
CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN
INSERT INTO t2 VALUES(new.a, new.b);
UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c;
DELETE FROM t2 WHERE new.a = c;
END;
}
} {}
do_test hook-4.2.2 {
execsql {
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
}
set ::update_hook
} [list \
INSERT main t1 1 \
INSERT main t2 1 \
UPDATE main t2 1 \
DELETE main t2 1 \
INSERT main t1 2 \
INSERT main t2 2 \
UPDATE main t2 2 \
DELETE main t2 2 \
]
} else {
execsql {
INSERT INTO t1 VALUES(1, 'one');
INSERT INTO t1 VALUES(2, 'two');
}
}
# Update-hook + ATTACH
set ::update_hook {}
ifcapable attach {
do_test hook-4.2.3 {
forcedelete test2.db
execsql {
ATTACH 'test2.db' AS aux;
CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b);
INSERT INTO aux.t3 SELECT * FROM t1;
UPDATE t3 SET b = 'two or so' WHERE a = 2;
DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now)
}
set ::update_hook
} [list \
INSERT aux t3 1 \
INSERT aux t3 2 \
UPDATE aux t3 2 \
DELETE aux t3 1 \
DELETE aux t3 2 \
]
}
ifcapable trigger {
execsql {
DROP TRIGGER t1_trigger;
}
}
# Test that other vdbe operations involving btree structures do not
# incorrectly invoke the update-hook.
set ::update_hook {}
do_test hook-4.3.1 {
execsql {
CREATE INDEX t1_i ON t1(b);
INSERT INTO t1 VALUES(3, 'three');
UPDATE t1 SET b = '';
DELETE FROM t1 WHERE a > 1;
}
set ::update_hook
} [list \
INSERT main t1 3 \
UPDATE main t1 1 \
UPDATE main t1 2 \
UPDATE main t1 3 \
DELETE main t1 2 \
DELETE main t1 3 \
]
set ::update_hook {}
ifcapable compound&&attach {
do_test hook-4.3.2 {
execsql {
SELECT * FROM t1 UNION SELECT * FROM t3;
SELECT * FROM t1 UNION ALL SELECT * FROM t3;
SELECT * FROM t1 INTERSECT SELECT * FROM t3;
SELECT * FROM t1 EXCEPT SELECT * FROM t3;
SELECT * FROM t1 ORDER BY b;
SELECT * FROM t1 GROUP BY b;
}
set ::update_hook
} [list]
}
do_test hook-4.4 {
execsql {
CREATE TABLE t4(a UNIQUE, b);
INSERT INTO t4 VALUES(1, 'a');
INSERT INTO t4 VALUES(2, 'b');
}
set ::update_hook [list]
execsql {
REPLACE INTO t4 VALUES(1, 'c');
}
set ::update_hook
} [list INSERT main t4 3 ]
do_execsql_test hook-4.4.1 {
SELECT * FROM t4 ORDER BY a;
} {1 c 2 b}
do_test hook-4.4.2 {
set ::update_hook [list]
execsql {
PRAGMA recursive_triggers = on;
REPLACE INTO t4 VALUES(1, 'd');
}
set ::update_hook
} [list INSERT main t4 4 ]
do_execsql_test hook-4.4.3 {
SELECT * FROM t4 ORDER BY a;
} {1 d 2 b}
db update_hook {}
#
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# Test the rollback-hook. The rollback-hook is a bit more complicated than
# either the commit or update hooks because a rollback can happen
# explicitly (an sql ROLLBACK statement) or implicitly (a constraint or
# error condition).
#
# hook-5.1.* - Test explicit rollbacks.
# hook-5.2.* - Test implicit rollbacks caused by constraint failure.
#
# hook-5.3.* - Test implicit rollbacks caused by IO errors.
# hook-5.4.* - Test implicit rollbacks caused by malloc() failure.
# hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook
# not be called for these?
#
do_test hook-5.0 {
# Configure the rollback hook to increment global variable
# $::rollback_hook each time it is invoked.
set ::rollback_hook 0
db rollback_hook [list incr ::rollback_hook]
} {}
# Test explicit rollbacks. Not much can really go wrong here.
#
do_test hook-5.1.1 {
set ::rollback_hook 0
execsql {
BEGIN;
ROLLBACK;
}
set ::rollback_hook
} {1}
# Test implicit rollbacks caused by constraints.
#
do_test hook-5.2.1 {
set ::rollback_hook 0
catchsql {
DROP TABLE t1;
CREATE TABLE t1(a PRIMARY KEY, b);
INSERT INTO t1 VALUES('one', 'I');
INSERT INTO t1 VALUES('one', 'I');
}
set ::rollback_hook
} {1}
do_test hook-5.2.2 {
# Check that the INSERT transaction above really was rolled back.
execsql {
SELECT count(*) FROM t1;
}
} {1}
#
# End rollback-hook testing.
#----------------------------------------------------------------------------
#----------------------------------------------------------------------------
# Test that if a commit-hook returns non-zero (causing a rollback), the
# rollback-hook is invoked.
#
proc commit_hook {} {
lappend ::hooks COMMIT
return 1
}
proc rollback_hook {} {
lappend ::hooks ROLLBACK
}
do_test hook-6.1 {
set ::hooks [list]
db commit_hook commit_hook
db rollback_hook rollback_hook
catchsql {
BEGIN;
INSERT INTO t1 VALUES('two', 'II');
COMMIT;
}
execsql { SELECT * FROM t1 }
} {one I}
do_test hook-6.2 {
set ::hooks
} {COMMIT ROLLBACK}
unset ::hooks
finish_test
|