OLD | NEW |
(Empty) | |
| 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 |
| 22 set testdir [file dirname $argv0] |
| 23 source $testdir/tester.tcl |
| 24 set ::testprefix hook |
| 25 |
| 26 do_test hook-1.2 { |
| 27 db commit_hook |
| 28 } {} |
| 29 |
| 30 |
| 31 do_test hook-3.1 { |
| 32 set commit_cnt 0 |
| 33 proc commit_hook {} { |
| 34 incr ::commit_cnt |
| 35 return 0 |
| 36 } |
| 37 db commit_hook ::commit_hook |
| 38 db commit_hook |
| 39 } {::commit_hook} |
| 40 do_test hook-3.2 { |
| 41 set commit_cnt |
| 42 } {0} |
| 43 do_test hook-3.3 { |
| 44 execsql { |
| 45 CREATE TABLE t2(a,b); |
| 46 } |
| 47 set commit_cnt |
| 48 } {1} |
| 49 do_test hook-3.4 { |
| 50 execsql { |
| 51 INSERT INTO t2 VALUES(1,2); |
| 52 INSERT INTO t2 SELECT a+1, b+1 FROM t2; |
| 53 INSERT INTO t2 SELECT a+2, b+2 FROM t2; |
| 54 } |
| 55 set commit_cnt |
| 56 } {4} |
| 57 do_test hook-3.5 { |
| 58 set commit_cnt {} |
| 59 proc commit_hook {} { |
| 60 set ::commit_cnt [execsql {SELECT * FROM t2}] |
| 61 return 0 |
| 62 } |
| 63 execsql { |
| 64 INSERT INTO t2 VALUES(5,6); |
| 65 } |
| 66 set commit_cnt |
| 67 } {1 2 2 3 3 4 4 5 5 6} |
| 68 do_test hook-3.6 { |
| 69 set commit_cnt {} |
| 70 proc commit_hook {} { |
| 71 set ::commit_cnt [execsql {SELECT * FROM t2}] |
| 72 return 1 |
| 73 } |
| 74 catchsql { |
| 75 INSERT INTO t2 VALUES(6,7); |
| 76 } |
| 77 } {1 {constraint failed}} |
| 78 verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK |
| 79 do_test hook-3.7 { |
| 80 set ::commit_cnt |
| 81 } {1 2 2 3 3 4 4 5 5 6 6 7} |
| 82 do_test hook-3.8 { |
| 83 execsql {SELECT * FROM t2} |
| 84 } {1 2 2 3 3 4 4 5 5 6} |
| 85 |
| 86 # Test turnning off the commit hook |
| 87 # |
| 88 do_test hook-3.9 { |
| 89 db commit_hook {} |
| 90 set ::commit_cnt {} |
| 91 execsql { |
| 92 INSERT INTO t2 VALUES(7,8); |
| 93 } |
| 94 set ::commit_cnt |
| 95 } {} |
| 96 |
| 97 # Ticket #3564. |
| 98 # |
| 99 do_test hook-3.10 { |
| 100 forcedelete test2.db test2.db-journal |
| 101 sqlite3 db2 test2.db |
| 102 proc commit_hook {} { |
| 103 set y [db2 one {SELECT y FROM t3 WHERE y>10}] |
| 104 return [expr {$y>10}] |
| 105 } |
| 106 db2 eval {CREATE TABLE t3(x,y)} |
| 107 db2 commit_hook commit_hook |
| 108 catchsql {INSERT INTO t3 VALUES(1,2)} db2 |
| 109 catchsql {INSERT INTO t3 VALUES(11,12)} db2 |
| 110 catchsql {INSERT INTO t3 VALUES(3,4)} db2 |
| 111 db2 eval { |
| 112 SELECT * FROM t3 ORDER BY x; |
| 113 } |
| 114 } {1 2 3 4} |
| 115 db2 close |
| 116 |
| 117 |
| 118 #---------------------------------------------------------------------------- |
| 119 # Tests for the update-hook. |
| 120 # |
| 121 # 4.1.* - Very simple tests. Test that the update hook is invoked correctly |
| 122 # for INSERT, DELETE and UPDATE statements, including DELETE |
| 123 # statements with no WHERE clause. |
| 124 # 4.2.* - Check that the update-hook is invoked for rows modified by trigger |
| 125 # bodies. Also that the database name is correctly reported when |
| 126 # an attached database is modified. |
| 127 # 4.3.* - Do some sorting, grouping, compound queries, population and |
| 128 # depopulation of indices, to make sure the update-hook is not |
| 129 # invoked incorrectly. |
| 130 # |
| 131 # EVIDENCE-OF: R-21999-45122 The sqlite3_update_hook() interface |
| 132 # registers a callback function with the database connection identified |
| 133 # by the first argument to be invoked whenever a row is updated, |
| 134 # inserted or deleted in a rowid table. |
| 135 |
| 136 # Simple tests |
| 137 do_test hook-4.1.1a { |
| 138 catchsql { |
| 139 DROP TABLE t1; |
| 140 } |
| 141 unset -nocomplain ::update_hook |
| 142 set ::update_hook {} |
| 143 db update_hook [list lappend ::update_hook] |
| 144 # |
| 145 # EVIDENCE-OF: R-52223-27275 The update hook is not invoked when |
| 146 # internal system tables are modified (i.e. sqlite_master and |
| 147 # sqlite_sequence). |
| 148 # |
| 149 execsql { |
| 150 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); |
| 151 CREATE TABLE t1w(a INT PRIMARY KEY, b) WITHOUT ROWID; |
| 152 } |
| 153 set ::update_hook |
| 154 } {} |
| 155 do_test hook-4.1.1b { |
| 156 execsql { |
| 157 INSERT INTO t1 VALUES(1, 'one'); |
| 158 INSERT INTO t1 VALUES(2, 'two'); |
| 159 INSERT INTO t1 VALUES(3, 'three'); |
| 160 INSERT INTO t1w SELECT * FROM t1; |
| 161 } |
| 162 } {} |
| 163 |
| 164 # EVIDENCE-OF: R-15506-57666 The second callback argument is one of |
| 165 # SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE, depending on the |
| 166 # operation that caused the callback to be invoked. |
| 167 # |
| 168 # EVIDENCE-OF: R-29213-61195 The third and fourth arguments to the |
| 169 # callback contain pointers to the database and table name containing |
| 170 # the affected row. |
| 171 # |
| 172 # EVIDENCE-OF: R-30809-57812 The final callback parameter is the rowid |
| 173 # of the row. |
| 174 # |
| 175 do_test hook-4.1.2 { |
| 176 set ::update_hook {} |
| 177 execsql { |
| 178 INSERT INTO t1 VALUES(4, 'four'); |
| 179 DELETE FROM t1 WHERE b = 'two'; |
| 180 UPDATE t1 SET b = '' WHERE a = 1 OR a = 3; |
| 181 DELETE FROM t1 WHERE 1; -- Avoid the truncate optimization (for now) |
| 182 } |
| 183 set ::update_hook |
| 184 } [list \ |
| 185 INSERT main t1 4 \ |
| 186 DELETE main t1 2 \ |
| 187 UPDATE main t1 1 \ |
| 188 UPDATE main t1 3 \ |
| 189 DELETE main t1 1 \ |
| 190 DELETE main t1 3 \ |
| 191 DELETE main t1 4 \ |
| 192 ] |
| 193 |
| 194 # EVIDENCE-OF: R-61808-14344 The sqlite3_update_hook() interface does |
| 195 # not fire callbacks for changes to a WITHOUT ROWID table. |
| 196 # |
| 197 # EVIDENCE-OF: R-33257-44249 The update hook is not invoked when WITHOUT |
| 198 # ROWID tables are modified. |
| 199 # |
| 200 do_test hook-4.1.2w { |
| 201 set ::update_hook {} |
| 202 execsql { |
| 203 INSERT INTO t1w VALUES(4, 'four'); |
| 204 DELETE FROM t1w WHERE b = 'two'; |
| 205 UPDATE t1w SET b = '' WHERE a = 1 OR a = 3; |
| 206 DELETE FROM t1w WHERE 1; -- Avoid the truncate optimization (for now) |
| 207 } |
| 208 set ::update_hook |
| 209 } {} |
| 210 |
| 211 ifcapable trigger { |
| 212 # Update hook is not invoked for changes to sqlite_master |
| 213 # |
| 214 do_test hook-4.1.3 { |
| 215 set ::update_hook {} |
| 216 execsql { |
| 217 CREATE TRIGGER r1 AFTER INSERT ON t1 BEGIN SELECT RAISE(IGNORE); END; |
| 218 } |
| 219 set ::update_hook |
| 220 } {} |
| 221 do_test hook-4.1.4 { |
| 222 set ::update_hook {} |
| 223 execsql { |
| 224 DROP TRIGGER r1; |
| 225 } |
| 226 set ::update_hook |
| 227 } {} |
| 228 |
| 229 set ::update_hook {} |
| 230 do_test hook-4.2.1 { |
| 231 catchsql { |
| 232 DROP TABLE t2; |
| 233 } |
| 234 execsql { |
| 235 CREATE TABLE t2(c INTEGER PRIMARY KEY, d); |
| 236 CREATE TRIGGER t1_trigger AFTER INSERT ON t1 BEGIN |
| 237 INSERT INTO t2 VALUES(new.a, new.b); |
| 238 UPDATE t2 SET d = d || ' via trigger' WHERE new.a = c; |
| 239 DELETE FROM t2 WHERE new.a = c; |
| 240 END; |
| 241 } |
| 242 } {} |
| 243 do_test hook-4.2.2 { |
| 244 execsql { |
| 245 INSERT INTO t1 VALUES(1, 'one'); |
| 246 INSERT INTO t1 VALUES(2, 'two'); |
| 247 } |
| 248 set ::update_hook |
| 249 } [list \ |
| 250 INSERT main t1 1 \ |
| 251 INSERT main t2 1 \ |
| 252 UPDATE main t2 1 \ |
| 253 DELETE main t2 1 \ |
| 254 INSERT main t1 2 \ |
| 255 INSERT main t2 2 \ |
| 256 UPDATE main t2 2 \ |
| 257 DELETE main t2 2 \ |
| 258 ] |
| 259 } else { |
| 260 execsql { |
| 261 INSERT INTO t1 VALUES(1, 'one'); |
| 262 INSERT INTO t1 VALUES(2, 'two'); |
| 263 } |
| 264 } |
| 265 |
| 266 # Update-hook + ATTACH |
| 267 set ::update_hook {} |
| 268 ifcapable attach { |
| 269 do_test hook-4.2.3 { |
| 270 forcedelete test2.db |
| 271 execsql { |
| 272 ATTACH 'test2.db' AS aux; |
| 273 CREATE TABLE aux.t3(a INTEGER PRIMARY KEY, b); |
| 274 INSERT INTO aux.t3 SELECT * FROM t1; |
| 275 UPDATE t3 SET b = 'two or so' WHERE a = 2; |
| 276 DELETE FROM t3 WHERE 1; -- Avoid the truncate optimization (for now) |
| 277 } |
| 278 set ::update_hook |
| 279 } [list \ |
| 280 INSERT aux t3 1 \ |
| 281 INSERT aux t3 2 \ |
| 282 UPDATE aux t3 2 \ |
| 283 DELETE aux t3 1 \ |
| 284 DELETE aux t3 2 \ |
| 285 ] |
| 286 } |
| 287 |
| 288 ifcapable trigger { |
| 289 execsql { |
| 290 DROP TRIGGER t1_trigger; |
| 291 } |
| 292 } |
| 293 |
| 294 # Test that other vdbe operations involving btree structures do not |
| 295 # incorrectly invoke the update-hook. |
| 296 set ::update_hook {} |
| 297 do_test hook-4.3.1 { |
| 298 execsql { |
| 299 CREATE INDEX t1_i ON t1(b); |
| 300 INSERT INTO t1 VALUES(3, 'three'); |
| 301 UPDATE t1 SET b = ''; |
| 302 DELETE FROM t1 WHERE a > 1; |
| 303 } |
| 304 set ::update_hook |
| 305 } [list \ |
| 306 INSERT main t1 3 \ |
| 307 UPDATE main t1 1 \ |
| 308 UPDATE main t1 2 \ |
| 309 UPDATE main t1 3 \ |
| 310 DELETE main t1 2 \ |
| 311 DELETE main t1 3 \ |
| 312 ] |
| 313 set ::update_hook {} |
| 314 ifcapable compound&&attach { |
| 315 do_test hook-4.3.2 { |
| 316 execsql { |
| 317 SELECT * FROM t1 UNION SELECT * FROM t3; |
| 318 SELECT * FROM t1 UNION ALL SELECT * FROM t3; |
| 319 SELECT * FROM t1 INTERSECT SELECT * FROM t3; |
| 320 SELECT * FROM t1 EXCEPT SELECT * FROM t3; |
| 321 SELECT * FROM t1 ORDER BY b; |
| 322 SELECT * FROM t1 GROUP BY b; |
| 323 } |
| 324 set ::update_hook |
| 325 } [list] |
| 326 } |
| 327 |
| 328 do_test hook-4.4 { |
| 329 execsql { |
| 330 CREATE TABLE t4(a UNIQUE, b); |
| 331 INSERT INTO t4 VALUES(1, 'a'); |
| 332 INSERT INTO t4 VALUES(2, 'b'); |
| 333 } |
| 334 set ::update_hook [list] |
| 335 execsql { |
| 336 REPLACE INTO t4 VALUES(1, 'c'); |
| 337 } |
| 338 set ::update_hook |
| 339 } [list INSERT main t4 3 ] |
| 340 do_execsql_test hook-4.4.1 { |
| 341 SELECT * FROM t4 ORDER BY a; |
| 342 } {1 c 2 b} |
| 343 do_test hook-4.4.2 { |
| 344 set ::update_hook [list] |
| 345 execsql { |
| 346 PRAGMA recursive_triggers = on; |
| 347 REPLACE INTO t4 VALUES(1, 'd'); |
| 348 } |
| 349 set ::update_hook |
| 350 } [list INSERT main t4 4 ] |
| 351 do_execsql_test hook-4.4.3 { |
| 352 SELECT * FROM t4 ORDER BY a; |
| 353 } {1 d 2 b} |
| 354 |
| 355 db update_hook {} |
| 356 # |
| 357 #---------------------------------------------------------------------------- |
| 358 |
| 359 #---------------------------------------------------------------------------- |
| 360 # Test the rollback-hook. The rollback-hook is a bit more complicated than |
| 361 # either the commit or update hooks because a rollback can happen |
| 362 # explicitly (an sql ROLLBACK statement) or implicitly (a constraint or |
| 363 # error condition). |
| 364 # |
| 365 # hook-5.1.* - Test explicit rollbacks. |
| 366 # hook-5.2.* - Test implicit rollbacks caused by constraint failure. |
| 367 # |
| 368 # hook-5.3.* - Test implicit rollbacks caused by IO errors. |
| 369 # hook-5.4.* - Test implicit rollbacks caused by malloc() failure. |
| 370 # hook-5.5.* - Test hot-journal rollbacks. Or should the rollback hook |
| 371 # not be called for these? |
| 372 # |
| 373 |
| 374 do_test hook-5.0 { |
| 375 # Configure the rollback hook to increment global variable |
| 376 # $::rollback_hook each time it is invoked. |
| 377 set ::rollback_hook 0 |
| 378 db rollback_hook [list incr ::rollback_hook] |
| 379 } {} |
| 380 |
| 381 # Test explicit rollbacks. Not much can really go wrong here. |
| 382 # |
| 383 do_test hook-5.1.1 { |
| 384 set ::rollback_hook 0 |
| 385 execsql { |
| 386 BEGIN; |
| 387 ROLLBACK; |
| 388 } |
| 389 set ::rollback_hook |
| 390 } {1} |
| 391 |
| 392 # Test implicit rollbacks caused by constraints. |
| 393 # |
| 394 do_test hook-5.2.1 { |
| 395 set ::rollback_hook 0 |
| 396 catchsql { |
| 397 DROP TABLE t1; |
| 398 CREATE TABLE t1(a PRIMARY KEY, b); |
| 399 INSERT INTO t1 VALUES('one', 'I'); |
| 400 INSERT INTO t1 VALUES('one', 'I'); |
| 401 } |
| 402 set ::rollback_hook |
| 403 } {1} |
| 404 do_test hook-5.2.2 { |
| 405 # Check that the INSERT transaction above really was rolled back. |
| 406 execsql { |
| 407 SELECT count(*) FROM t1; |
| 408 } |
| 409 } {1} |
| 410 |
| 411 # |
| 412 # End rollback-hook testing. |
| 413 #---------------------------------------------------------------------------- |
| 414 |
| 415 #---------------------------------------------------------------------------- |
| 416 # Test that if a commit-hook returns non-zero (causing a rollback), the |
| 417 # rollback-hook is invoked. |
| 418 # |
| 419 proc commit_hook {} { |
| 420 lappend ::hooks COMMIT |
| 421 return 1 |
| 422 } |
| 423 proc rollback_hook {} { |
| 424 lappend ::hooks ROLLBACK |
| 425 } |
| 426 do_test hook-6.1 { |
| 427 set ::hooks [list] |
| 428 db commit_hook commit_hook |
| 429 db rollback_hook rollback_hook |
| 430 catchsql { |
| 431 BEGIN; |
| 432 INSERT INTO t1 VALUES('two', 'II'); |
| 433 COMMIT; |
| 434 } |
| 435 execsql { SELECT * FROM t1 } |
| 436 } {one I} |
| 437 do_test hook-6.2 { |
| 438 set ::hooks |
| 439 } {COMMIT ROLLBACK} |
| 440 unset ::hooks |
| 441 |
| 442 #---------------------------------------------------------------------------- |
| 443 # The following tests - hook-7.* - test the pre-update hook. |
| 444 # |
| 445 ifcapable !preupdate { |
| 446 finish_test |
| 447 return |
| 448 } |
| 449 # |
| 450 # 7.1.1 - INSERT statement. |
| 451 # 7.1.2 - INSERT INTO ... SELECT statement. |
| 452 # 7.1.3 - REPLACE INTO ... (rowid conflict) |
| 453 # 7.1.4 - REPLACE INTO ... (other index conflicts) |
| 454 # 7.1.5 - REPLACE INTO ... (both rowid and other index conflicts) |
| 455 # |
| 456 # 7.2.1 - DELETE statement. |
| 457 # 7.2.2 - DELETE statement that uses the truncate optimization. |
| 458 # |
| 459 # 7.3.1 - UPDATE statement. |
| 460 # 7.3.2 - UPDATE statement that modifies the rowid. |
| 461 # 7.3.3 - UPDATE OR REPLACE ... (rowid conflict). |
| 462 # 7.3.4 - UPDATE OR REPLACE ... (other index conflicts) |
| 463 # 7.3.4 - UPDATE OR REPLACE ... (both rowid and other index conflicts) |
| 464 # |
| 465 # 7.4.1 - Test that the pre-update-hook is invoked only once if a row being |
| 466 # deleted is removed by a BEFORE trigger. |
| 467 # |
| 468 # 7.4.2 - Test that the pre-update-hook is invoked if a BEFORE trigger |
| 469 # removes a row being updated. In this case the update hook should |
| 470 # be invoked with SQLITE_INSERT as the opcode when inserting the |
| 471 # new version of the row. |
| 472 # |
| 473 # TODO: Short records (those created before a column is added to a table |
| 474 # using ALTER TABLE) |
| 475 # |
| 476 |
| 477 proc do_preupdate_test {tn sql x} { |
| 478 set X [list] |
| 479 foreach elem $x {lappend X $elem} |
| 480 uplevel do_test $tn [list " |
| 481 set ::preupdate \[list\] |
| 482 execsql { $sql } |
| 483 set ::preupdate |
| 484 "] [list $X] |
| 485 } |
| 486 |
| 487 proc preupdate_hook {args} { |
| 488 set type [lindex $args 0] |
| 489 eval lappend ::preupdate $args |
| 490 if {$type != "INSERT"} { |
| 491 for {set i 0} {$i < [db preupdate count]} {incr i} { |
| 492 lappend ::preupdate [db preupdate old $i] |
| 493 } |
| 494 } |
| 495 if {$type != "DELETE"} { |
| 496 for {set i 0} {$i < [db preupdate count]} {incr i} { |
| 497 set rc [catch { db preupdate new $i } v] |
| 498 lappend ::preupdate $v |
| 499 } |
| 500 } |
| 501 } |
| 502 |
| 503 db close |
| 504 forcedelete test.db |
| 505 sqlite3 db test.db |
| 506 db preupdate hook preupdate_hook |
| 507 |
| 508 # Set up a schema to use for tests 7.1.* to 7.3.*. |
| 509 do_execsql_test 7.0 { |
| 510 CREATE TABLE t1(a, b); |
| 511 CREATE TABLE t2(x, y); |
| 512 CREATE TABLE t3(i, j, UNIQUE(i)); |
| 513 |
| 514 INSERT INTO t2 VALUES('a', 'b'); |
| 515 INSERT INTO t2 VALUES('c', 'd'); |
| 516 |
| 517 INSERT INTO t3 VALUES(4, 16); |
| 518 INSERT INTO t3 VALUES(5, 25); |
| 519 INSERT INTO t3 VALUES(6, 36); |
| 520 } |
| 521 |
| 522 do_preupdate_test 7.1.1 { |
| 523 INSERT INTO t1 VALUES('x', 'y') |
| 524 } {INSERT main t1 1 1 x y} |
| 525 |
| 526 # 7.1.2.1 does not use the xfer optimization. 7.1.2.2 does. |
| 527 do_preupdate_test 7.1.2.1 { |
| 528 INSERT INTO t1 SELECT y, x FROM t2; |
| 529 } {INSERT main t1 2 2 b a INSERT main t1 3 3 d c} |
| 530 do_preupdate_test 7.1.2.2 { |
| 531 INSERT INTO t1 SELECT * FROM t2; |
| 532 } {INSERT main t1 4 4 a b INSERT main t1 5 5 c d} |
| 533 |
| 534 do_preupdate_test 7.1.3 { |
| 535 REPLACE INTO t1(rowid, a, b) VALUES(1, 1, 1); |
| 536 } { |
| 537 DELETE main t1 1 1 x y |
| 538 INSERT main t1 1 1 1 1 |
| 539 } |
| 540 |
| 541 do_preupdate_test 7.1.4 { |
| 542 REPLACE INTO t3 VALUES(4, NULL); |
| 543 } { |
| 544 DELETE main t3 1 1 4 16 |
| 545 INSERT main t3 4 4 4 {} |
| 546 } |
| 547 |
| 548 do_preupdate_test 7.1.5 { |
| 549 REPLACE INTO t3(rowid, i, j) VALUES(2, 6, NULL); |
| 550 } { |
| 551 DELETE main t3 2 2 5 25 |
| 552 DELETE main t3 3 3 6 36 |
| 553 INSERT main t3 2 2 6 {} |
| 554 } |
| 555 |
| 556 do_execsql_test 7.2.0 { SELECT rowid FROM t1 } {1 2 3 4 5} |
| 557 |
| 558 do_preupdate_test 7.2.1 { |
| 559 DELETE FROM t1 WHERE rowid = 3 |
| 560 } { |
| 561 DELETE main t1 3 3 d c |
| 562 } |
| 563 do_preupdate_test 7.2.2 { |
| 564 DELETE FROM t1 |
| 565 } { |
| 566 DELETE main t1 1 1 1 1 |
| 567 DELETE main t1 2 2 b a |
| 568 DELETE main t1 4 4 a b |
| 569 DELETE main t1 5 5 c d |
| 570 } |
| 571 |
| 572 do_execsql_test 7.3.0 { |
| 573 DELETE FROM t1; |
| 574 DELETE FROM t2; |
| 575 DELETE FROM t3; |
| 576 |
| 577 INSERT INTO t2 VALUES('a', 'b'); |
| 578 INSERT INTO t2 VALUES('c', 'd'); |
| 579 |
| 580 INSERT INTO t3 VALUES(4, 16); |
| 581 INSERT INTO t3 VALUES(5, 25); |
| 582 INSERT INTO t3 VALUES(6, 36); |
| 583 } |
| 584 |
| 585 do_preupdate_test 7.3.1 { |
| 586 UPDATE t2 SET y = y||y; |
| 587 } { |
| 588 UPDATE main t2 1 1 a b a bb |
| 589 UPDATE main t2 2 2 c d c dd |
| 590 } |
| 591 |
| 592 do_preupdate_test 7.3.2 { |
| 593 UPDATE t2 SET rowid = rowid-1; |
| 594 } { |
| 595 UPDATE main t2 1 0 a bb a bb |
| 596 UPDATE main t2 2 1 c dd c dd |
| 597 } |
| 598 |
| 599 do_preupdate_test 7.3.3 { |
| 600 UPDATE OR REPLACE t2 SET rowid = 1 WHERE x = 'a' |
| 601 } { |
| 602 DELETE main t2 1 1 c dd |
| 603 UPDATE main t2 0 1 a bb a bb |
| 604 } |
| 605 |
| 606 do_preupdate_test 7.3.4.1 { |
| 607 UPDATE OR REPLACE t3 SET i = 5 WHERE i = 6 |
| 608 } { |
| 609 DELETE main t3 2 2 5 25 |
| 610 UPDATE main t3 3 3 6 36 5 36 |
| 611 } |
| 612 |
| 613 do_execsql_test 7.3.4.2 { |
| 614 INSERT INTO t3 VALUES(10, 100); |
| 615 SELECT rowid, * FROM t3; |
| 616 } {1 4 16 3 5 36 4 10 100} |
| 617 |
| 618 do_preupdate_test 7.3.5 { |
| 619 UPDATE OR REPLACE t3 SET rowid = 1, i = 5 WHERE j = 100; |
| 620 } { |
| 621 DELETE main t3 1 1 4 16 |
| 622 DELETE main t3 3 3 5 36 |
| 623 UPDATE main t3 4 1 10 100 5 100 |
| 624 } |
| 625 |
| 626 do_execsql_test 7.4.1.0 { |
| 627 CREATE TABLE t4(a, b); |
| 628 INSERT INTO t4 VALUES('a', 1); |
| 629 INSERT INTO t4 VALUES('b', 2); |
| 630 INSERT INTO t4 VALUES('c', 3); |
| 631 |
| 632 CREATE TRIGGER t4t BEFORE DELETE ON t4 BEGIN |
| 633 DELETE FROM t4 WHERE b = 1; |
| 634 END; |
| 635 } |
| 636 |
| 637 do_preupdate_test 7.4.1.1 { |
| 638 DELETE FROM t4 WHERE b = 3 |
| 639 } { |
| 640 DELETE main t4 1 1 a 1 |
| 641 DELETE main t4 3 3 c 3 |
| 642 } |
| 643 |
| 644 do_execsql_test 7.4.1.2 { |
| 645 INSERT INTO t4(rowid, a, b) VALUES(1, 'a', 1); |
| 646 INSERT INTO t4(rowid, a, b) VALUES(3, 'c', 3); |
| 647 } |
| 648 do_preupdate_test 7.4.1.3 { |
| 649 DELETE FROM t4 WHERE b = 1 |
| 650 } { |
| 651 DELETE main t4 1 1 a 1 |
| 652 } |
| 653 |
| 654 do_execsql_test 7.4.2.0 { |
| 655 CREATE TABLE t5(a, b); |
| 656 INSERT INTO t5 VALUES('a', 1); |
| 657 INSERT INTO t5 VALUES('b', 2); |
| 658 INSERT INTO t5 VALUES('c', 3); |
| 659 |
| 660 CREATE TRIGGER t5t BEFORE UPDATE ON t5 BEGIN |
| 661 DELETE FROM t5 WHERE b = 1; |
| 662 END; |
| 663 } |
| 664 do_preupdate_test 7.4.2.1 { |
| 665 UPDATE t5 SET b = 4 WHERE a = 'c' |
| 666 } { |
| 667 DELETE main t5 1 1 a 1 |
| 668 UPDATE main t5 3 3 c 3 c 4 |
| 669 } |
| 670 |
| 671 do_execsql_test 7.4.2.2 { |
| 672 INSERT INTO t5(rowid, a, b) VALUES(1, 'a', 1); |
| 673 } |
| 674 |
| 675 do_preupdate_test 7.4.2.3 { |
| 676 UPDATE t5 SET b = 5 WHERE a = 'a' |
| 677 } { |
| 678 DELETE main t5 1 1 a 1 |
| 679 } |
| 680 |
| 681 do_execsql_test 7.5.1.0 { |
| 682 CREATE TABLE t7(a, b); |
| 683 INSERT INTO t7 VALUES('one', 'two'); |
| 684 INSERT INTO t7 VALUES('three', 'four'); |
| 685 ALTER TABLE t7 ADD COLUMN c DEFAULT NULL; |
| 686 } |
| 687 |
| 688 do_preupdate_test 7.5.1.1 { |
| 689 DELETE FROM t7 WHERE a = 'one' |
| 690 } { |
| 691 DELETE main t7 1 1 one two {} |
| 692 } |
| 693 |
| 694 do_preupdate_test 7.5.1.2 { |
| 695 UPDATE t7 SET b = 'five' |
| 696 } { |
| 697 UPDATE main t7 2 2 three four {} three five {} |
| 698 } |
| 699 |
| 700 do_execsql_test 7.5.2.0 { |
| 701 CREATE TABLE t8(a, b); |
| 702 INSERT INTO t8 VALUES('one', 'two'); |
| 703 INSERT INTO t8 VALUES('three', 'four'); |
| 704 ALTER TABLE t8 ADD COLUMN c DEFAULT 'xxx'; |
| 705 } |
| 706 |
| 707 if 0 { |
| 708 # At time of writing, these two are broken. They demonstrate that the |
| 709 # sqlite3_preupdate_old() method does not handle the case where ALTER TABLE |
| 710 # has been used to add a column with a default value other than NULL. |
| 711 # |
| 712 do_preupdate_test 7.5.2.1 { |
| 713 DELETE FROM t8 WHERE a = 'one' |
| 714 } { |
| 715 DELETE main t8 1 1 one two xxx |
| 716 } |
| 717 do_preupdate_test 7.5.2.2 { |
| 718 UPDATE t8 SET b = 'five' |
| 719 } { |
| 720 UPDATE main t8 2 2 three four xxx three five xxx |
| 721 } |
| 722 } |
| 723 |
| 724 # This block of tests verifies that IPK values are correctly reported |
| 725 # by the sqlite3_preupdate_old() and sqlite3_preupdate_new() functions. |
| 726 # |
| 727 do_execsql_test 7.6.1 { CREATE TABLE t9(a, b INTEGER PRIMARY KEY, c) } |
| 728 do_preupdate_test 7.6.2 { |
| 729 INSERT INTO t9 VALUES(1, 2, 3); |
| 730 UPDATE t9 SET b = b+1, c = c+1; |
| 731 DELETE FROM t9 WHERE a = 1; |
| 732 } { |
| 733 INSERT main t9 2 2 1 2 3 |
| 734 UPDATE main t9 2 3 1 2 3 1 3 4 |
| 735 DELETE main t9 3 3 1 3 4 |
| 736 } |
| 737 |
| 738 #-------------------------------------------------------------------------- |
| 739 # Test that the sqlite3_preupdate_depth() API seems to work. |
| 740 # |
| 741 proc preupdate_hook {args} { |
| 742 set type [lindex $args 0] |
| 743 eval lappend ::preupdate $args |
| 744 eval lappend ::preupdate [db preupdate depth] |
| 745 |
| 746 if {$type != "INSERT"} { |
| 747 for {set i 0} {$i < [db preupdate count]} {incr i} { |
| 748 lappend ::preupdate [db preupdate old $i] |
| 749 } |
| 750 } |
| 751 if {$type != "DELETE"} { |
| 752 for {set i 0} {$i < [db preupdate count]} {incr i} { |
| 753 set rc [catch { db preupdate new $i } v] |
| 754 lappend ::preupdate $v |
| 755 } |
| 756 } |
| 757 } |
| 758 |
| 759 db close |
| 760 forcedelete test.db |
| 761 sqlite3 db test.db |
| 762 db preupdate hook preupdate_hook |
| 763 |
| 764 do_execsql_test 7.6.1 { |
| 765 CREATE TABLE t1(x PRIMARY KEY); |
| 766 CREATE TABLE t2(x PRIMARY KEY); |
| 767 CREATE TABLE t3(x PRIMARY KEY); |
| 768 CREATE TABLE t4(x PRIMARY KEY); |
| 769 |
| 770 CREATE TRIGGER a AFTER INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END; |
| 771 CREATE TRIGGER b AFTER INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END; |
| 772 CREATE TRIGGER c AFTER INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END; |
| 773 |
| 774 CREATE TRIGGER d AFTER UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END; |
| 775 CREATE TRIGGER e AFTER UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END; |
| 776 CREATE TRIGGER f AFTER UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END; |
| 777 |
| 778 CREATE TRIGGER g AFTER DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END; |
| 779 CREATE TRIGGER h AFTER DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END; |
| 780 CREATE TRIGGER i AFTER DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END; |
| 781 } |
| 782 |
| 783 do_preupdate_test 7.6.2 { |
| 784 INSERT INTO t1 VALUES('xyz'); |
| 785 } { |
| 786 INSERT main t1 1 1 0 xyz |
| 787 INSERT main t2 1 1 1 xyz |
| 788 INSERT main t3 1 1 2 xyz |
| 789 INSERT main t4 1 1 3 xyz |
| 790 } |
| 791 do_preupdate_test 7.6.3 { |
| 792 UPDATE t1 SET x = 'abc'; |
| 793 } { |
| 794 UPDATE main t1 1 1 0 xyz abc |
| 795 UPDATE main t2 1 1 1 xyz abc |
| 796 UPDATE main t3 1 1 2 xyz abc |
| 797 UPDATE main t4 1 1 3 xyz abc |
| 798 } |
| 799 do_preupdate_test 7.6.4 { |
| 800 DELETE FROM t1 WHERE 1; |
| 801 } { |
| 802 DELETE main t1 1 1 0 abc |
| 803 DELETE main t2 1 1 1 abc |
| 804 DELETE main t3 1 1 2 abc |
| 805 DELETE main t4 1 1 3 abc |
| 806 } |
| 807 |
| 808 do_execsql_test 7.6.5 { |
| 809 DROP TRIGGER a; DROP TRIGGER b; DROP TRIGGER c; |
| 810 DROP TRIGGER d; DROP TRIGGER e; DROP TRIGGER f; |
| 811 DROP TRIGGER g; DROP TRIGGER h; DROP TRIGGER i; |
| 812 |
| 813 CREATE TRIGGER a BEFORE INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END; |
| 814 CREATE TRIGGER b BEFORE INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END; |
| 815 CREATE TRIGGER c BEFORE INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END; |
| 816 |
| 817 CREATE TRIGGER d BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END; |
| 818 CREATE TRIGGER e BEFORE UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END; |
| 819 CREATE TRIGGER f BEFORE UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END; |
| 820 |
| 821 CREATE TRIGGER g BEFORE DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END; |
| 822 CREATE TRIGGER h BEFORE DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END; |
| 823 CREATE TRIGGER i BEFORE DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END; |
| 824 } |
| 825 |
| 826 do_preupdate_test 7.6.6 { |
| 827 INSERT INTO t1 VALUES('xyz'); |
| 828 } { |
| 829 INSERT main t4 1 1 3 xyz |
| 830 INSERT main t3 1 1 2 xyz |
| 831 INSERT main t2 1 1 1 xyz |
| 832 INSERT main t1 1 1 0 xyz |
| 833 } |
| 834 do_preupdate_test 7.6.3 { |
| 835 UPDATE t1 SET x = 'abc'; |
| 836 } { |
| 837 UPDATE main t4 1 1 3 xyz abc |
| 838 UPDATE main t3 1 1 2 xyz abc |
| 839 UPDATE main t2 1 1 1 xyz abc |
| 840 UPDATE main t1 1 1 0 xyz abc |
| 841 } |
| 842 do_preupdate_test 7.6.4 { |
| 843 DELETE FROM t1 WHERE 1; |
| 844 } { |
| 845 DELETE main t4 1 1 3 abc |
| 846 DELETE main t3 1 1 2 abc |
| 847 DELETE main t2 1 1 1 abc |
| 848 DELETE main t1 1 1 0 abc |
| 849 } |
| 850 |
| 851 # No preupdate callbacks for modifying sqlite_master. |
| 852 do_preupdate_test 8.1 { CREATE TABLE x1(x, y); } { } |
| 853 do_preupdate_test 8.2 { ALTER TABLE x1 ADD COLUMN z } { } |
| 854 do_preupdate_test 8.3 { ALTER TABLE x1 RENAME TO y1 } { } |
| 855 do_preupdate_test 8.4 { CREATE INDEX y1x ON y1(x) } { } |
| 856 do_preupdate_test 8.5 { CREATE VIEW v1 AS SELECT * FROM y1 } { } |
| 857 do_preupdate_test 8.6 { DROP TABLE y1 } { } |
| 858 |
| 859 #------------------------------------------------------------------------- |
| 860 reset_db |
| 861 db preupdate hook preupdate_hook |
| 862 do_execsql_test 9.0 { |
| 863 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); |
| 864 CREATE TABLE t2(a, b INTEGER PRIMARY KEY); |
| 865 } |
| 866 do_preupdate_test 9.1 { |
| 867 INSERT INTO t1 VALUES(456, NULL, NULL); |
| 868 } { |
| 869 INSERT main t1 456 456 0 456 {} {} |
| 870 } |
| 871 do_execsql_test 9.2 { |
| 872 ALTER TABLE t1 ADD COLUMN d; |
| 873 } |
| 874 do_preupdate_test 9.3 { |
| 875 INSERT INTO t1(a, b, c) VALUES(457, NULL, NULL); |
| 876 } { |
| 877 INSERT main t1 457 457 0 457 {} {} {} |
| 878 } |
| 879 do_preupdate_test 9.4 { |
| 880 DELETE FROM t1 WHERE a=456 |
| 881 } { |
| 882 DELETE main t1 456 456 0 456 {} {} {} |
| 883 } |
| 884 do_preupdate_test 9.5 { |
| 885 INSERT INTO t2 DEFAULT VALUES; |
| 886 } { |
| 887 INSERT main t2 1 1 0 {} 1 |
| 888 } |
| 889 do_preupdate_test 9.6 { |
| 890 INSERT INTO t1 DEFAULT VALUES; |
| 891 } { |
| 892 INSERT main t1 458 458 0 458 {} {} {} |
| 893 } |
| 894 |
| 895 |
| 896 do_execsql_test 10.0 { |
| 897 CREATE TABLE t3(a, b INTEGER PRIMARY KEY); |
| 898 } |
| 899 do_preupdate_test 10.1 { |
| 900 INSERT INTO t3 DEFAULT VALUES |
| 901 } { |
| 902 INSERT main t3 1 1 0 {} 1 |
| 903 } |
| 904 do_execsql_test 10.2 { SELECT * FROM t3 } {{} 1} |
| 905 do_preupdate_test 10.3 { |
| 906 DELETE FROM t3 WHERE b=1 |
| 907 } {DELETE main t3 1 1 0 {} 1} |
| 908 |
| 909 |
| 910 finish_test |
OLD | NEW |