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
37 db commit_hook ::commit_hook
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;
60 set ::commit_cnt [execsql {SELECT * FROM t2}]
64 INSERT INTO t2 VALUES(5,6);
67 } {1 2 2 3 3 4 4 5 5 6}
71 set ::commit_cnt [execsql {SELECT * FROM t2}]
75 INSERT INTO t2 VALUES(6,7);
77 } {1 {constraint failed}}
78 verify_ex_errcode hook-3.6b SQLITE_CONSTRAINT_COMMITHOOK
81 } {1 2 2 3 3 4 4 5 5 6 6 7}
83 execsql {SELECT * FROM t2}
84 } {1 2 2 3 3 4 4 5 5 6}
86 # Test turnning off the commit hook
92 INSERT INTO t2 VALUES(7,8);
100 forcedelete test2.db test2.db-journal
102 proc commit_hook {} {
103 set y [db2 one {SELECT y FROM t3 WHERE y>10}]
104 return [expr {$y>10}]
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
112 SELECT * FROM t3 ORDER BY x;
118 #----------------------------------------------------------------------------
119 # Tests for the update-hook.
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.
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.
137 do_test hook-4.1.1a {
141 unset -nocomplain ::update_hook
143 db update_hook [list lappend ::update_hook]
145 # EVIDENCE-OF: R-24531-54682 The update hook is not invoked when
146 # internal system tables are modified (i.e. sqlite_sequence).
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 }
441 #----------------------------------------------------------------------------
442 # The following tests - hook-7.* - test the pre-update hook.
444 ifcapable !preupdate {
449 # 7.1.1 - INSERT statement.
450 # 7.1.2 - INSERT INTO ... SELECT statement.
451 # 7.1.3 - REPLACE INTO ... (rowid conflict)
452 # 7.1.4 - REPLACE INTO ... (other index conflicts)
453 # 7.1.5 - REPLACE INTO ... (both rowid and other index conflicts)
455 # 7.2.1 - DELETE statement.
456 # 7.2.2 - DELETE statement that uses the truncate optimization.
458 # 7.3.1 - UPDATE statement.
459 # 7.3.2 - UPDATE statement that modifies the rowid.
460 # 7.3.3 - UPDATE OR REPLACE ... (rowid conflict).
461 # 7.3.4 - UPDATE OR REPLACE ... (other index conflicts)
462 # 7.3.4 - UPDATE OR REPLACE ... (both rowid and other index conflicts)
464 # 7.4.1 - Test that the pre-update-hook is invoked only once if a row being
465 # deleted is removed by a BEFORE trigger.
467 # 7.4.2 - Test that the pre-update-hook is invoked if a BEFORE trigger
468 # removes a row being updated. In this case the update hook should
469 # be invoked with SQLITE_INSERT as the opcode when inserting the
470 # new version of the row.
472 # TODO: Short records (those created before a column is added to a table
476 proc do_preupdate_test {tn sql x} {
478 foreach elem $x {lappend X $elem}
479 uplevel do_test $tn [list "
480 set ::preupdate \[list\]
486 proc preupdate_hook {args} {
487 set type [lindex $args 0]
488 eval lappend ::preupdate $args
489 if {$type != "INSERT"} {
490 for {set i 0} {$i < [db preupdate count]} {incr i} {
491 lappend ::preupdate [db preupdate old $i]
494 if {$type != "DELETE"} {
495 for {set i 0} {$i < [db preupdate count]} {incr i} {
496 set rc [catch { db preupdate new $i } v]
497 lappend ::preupdate $v
505 db preupdate hook preupdate_hook
507 # Set up a schema to use for tests 7.1.* to 7.3.*.
508 do_execsql_test 7.0 {
509 CREATE TABLE t1(a, b);
510 CREATE TABLE t2(x, y);
511 CREATE TABLE t3(i, j, UNIQUE(i));
513 INSERT INTO t2 VALUES('a', 'b');
514 INSERT INTO t2 VALUES('c', 'd');
516 INSERT INTO t3 VALUES(4, 16);
517 INSERT INTO t3 VALUES(5, 25);
518 INSERT INTO t3 VALUES(6, 36);
521 do_preupdate_test 7.1.1 {
522 INSERT INTO t1 VALUES('x', 'y')
523 } {INSERT main t1 1 1 x y}
525 # 7.1.2.1 does not use the xfer optimization. 7.1.2.2 does.
526 do_preupdate_test 7.1.2.1 {
527 INSERT INTO t1 SELECT y, x FROM t2;
528 } {INSERT main t1 2 2 b a INSERT main t1 3 3 d c}
529 do_preupdate_test 7.1.2.2 {
530 INSERT INTO t1 SELECT * FROM t2;
531 } {INSERT main t1 4 4 a b INSERT main t1 5 5 c d}
533 do_preupdate_test 7.1.3 {
534 REPLACE INTO t1(rowid, a, b) VALUES(1, 1, 1);
536 DELETE main t1 1 1 x y
537 INSERT main t1 1 1 1 1
540 do_preupdate_test 7.1.4 {
541 REPLACE INTO t3 VALUES(4, NULL);
543 DELETE main t3 1 1 4 16
544 INSERT main t3 4 4 4 {}
547 do_preupdate_test 7.1.5 {
548 REPLACE INTO t3(rowid, i, j) VALUES(2, 6, NULL);
550 DELETE main t3 2 2 5 25
551 DELETE main t3 3 3 6 36
552 INSERT main t3 2 2 6 {}
555 do_execsql_test 7.2.0 { SELECT rowid FROM t1 } {1 2 3 4 5}
557 do_preupdate_test 7.2.1 {
558 DELETE FROM t1 WHERE rowid = 3
560 DELETE main t1 3 3 d c
562 do_preupdate_test 7.2.2 {
565 DELETE main t1 1 1 1 1
566 DELETE main t1 2 2 b a
567 DELETE main t1 4 4 a b
568 DELETE main t1 5 5 c d
571 do_execsql_test 7.3.0 {
576 INSERT INTO t2 VALUES('a', 'b');
577 INSERT INTO t2 VALUES('c', 'd');
579 INSERT INTO t3 VALUES(4, 16);
580 INSERT INTO t3 VALUES(5, 25);
581 INSERT INTO t3 VALUES(6, 36);
584 do_preupdate_test 7.3.1 {
585 UPDATE t2 SET y = y||y;
587 UPDATE main t2 1 1 a b a bb
588 UPDATE main t2 2 2 c d c dd
591 do_preupdate_test 7.3.2 {
592 UPDATE t2 SET rowid = rowid-1;
594 UPDATE main t2 1 0 a bb a bb
595 UPDATE main t2 2 1 c dd c dd
598 do_preupdate_test 7.3.3 {
599 UPDATE OR REPLACE t2 SET rowid = 1 WHERE x = 'a'
601 DELETE main t2 1 1 c dd
602 UPDATE main t2 0 1 a bb a bb
605 do_preupdate_test 7.3.4.1 {
606 UPDATE OR REPLACE t3 SET i = 5 WHERE i = 6
608 DELETE main t3 2 2 5 25
609 UPDATE main t3 3 3 6 36 5 36
612 do_execsql_test 7.3.4.2 {
613 INSERT INTO t3 VALUES(10, 100);
614 SELECT rowid, * FROM t3;
615 } {1 4 16 3 5 36 4 10 100}
617 do_preupdate_test 7.3.5 {
618 UPDATE OR REPLACE t3 SET rowid = 1, i = 5 WHERE j = 100;
620 DELETE main t3 1 1 4 16
621 DELETE main t3 3 3 5 36
622 UPDATE main t3 4 1 10 100 5 100
625 do_execsql_test 7.4.1.0 {
626 CREATE TABLE t4(a, b);
627 INSERT INTO t4 VALUES('a', 1);
628 INSERT INTO t4 VALUES('b', 2);
629 INSERT INTO t4 VALUES('c', 3);
631 CREATE TRIGGER t4t BEFORE DELETE ON t4 BEGIN
632 DELETE FROM t4 WHERE b = 1;
636 do_preupdate_test 7.4.1.1 {
637 DELETE FROM t4 WHERE b = 3
639 DELETE main t4 1 1 a 1
640 DELETE main t4 3 3 c 3
643 do_execsql_test 7.4.1.2 {
644 INSERT INTO t4(rowid, a, b) VALUES(1, 'a', 1);
645 INSERT INTO t4(rowid, a, b) VALUES(3, 'c', 3);
647 do_preupdate_test 7.4.1.3 {
648 DELETE FROM t4 WHERE b = 1
650 DELETE main t4 1 1 a 1
653 do_execsql_test 7.4.2.0 {
654 CREATE TABLE t5(a, b);
655 INSERT INTO t5 VALUES('a', 1);
656 INSERT INTO t5 VALUES('b', 2);
657 INSERT INTO t5 VALUES('c', 3);
659 CREATE TRIGGER t5t BEFORE UPDATE ON t5 BEGIN
660 DELETE FROM t5 WHERE b = 1;
663 do_preupdate_test 7.4.2.1 {
664 UPDATE t5 SET b = 4 WHERE a = 'c'
666 DELETE main t5 1 1 a 1
667 UPDATE main t5 3 3 c 3 c 4
670 do_execsql_test 7.4.2.2 {
671 INSERT INTO t5(rowid, a, b) VALUES(1, 'a', 1);
674 do_preupdate_test 7.4.2.3 {
675 UPDATE t5 SET b = 5 WHERE a = 'a'
677 DELETE main t5 1 1 a 1
680 ifcapable altertable {
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;
688 do_preupdate_test 7.5.1.1 {
689 DELETE FROM t7 WHERE a = 'one'
691 DELETE main t7 1 1 one two {}
694 do_preupdate_test 7.5.1.2 {
695 UPDATE t7 SET b = 'five'
697 UPDATE main t7 2 2 three four {} three five {}
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';
709 # At time of writing, these two are broken. They demonstrate that the
710 # sqlite3_preupdate_old() method does not handle the case where ALTER TABLE
711 # has been used to add a column with a default value other than NULL.
713 do_preupdate_test 7.5.2.1 {
714 DELETE FROM t8 WHERE a = 'one'
716 DELETE main t8 1 1 one two xxx
718 do_preupdate_test 7.5.2.2 {
719 UPDATE t8 SET b = 'five'
721 UPDATE main t8 2 2 three four xxx three five xxx
725 # This block of tests verifies that IPK values are correctly reported
726 # by the sqlite3_preupdate_old() and sqlite3_preupdate_new() functions.
728 do_execsql_test 7.6.1 { CREATE TABLE t9(a, b INTEGER PRIMARY KEY, c) }
729 do_preupdate_test 7.6.2 {
730 INSERT INTO t9 VALUES(1, 2, 3);
731 UPDATE t9 SET b = b+1, c = c+1;
732 DELETE FROM t9 WHERE a = 1;
734 INSERT main t9 2 2 1 2 3
735 UPDATE main t9 2 3 1 2 3 1 3 4
736 DELETE main t9 3 3 1 3 4
739 #--------------------------------------------------------------------------
740 # Test that the sqlite3_preupdate_depth() API seems to work.
742 proc preupdate_hook {args} {
743 set type [lindex $args 0]
744 eval lappend ::preupdate $args
745 eval lappend ::preupdate [db preupdate depth]
747 if {$type != "INSERT"} {
748 for {set i 0} {$i < [db preupdate count]} {incr i} {
749 lappend ::preupdate [db preupdate old $i]
752 if {$type != "DELETE"} {
753 for {set i 0} {$i < [db preupdate count]} {incr i} {
754 set rc [catch { db preupdate new $i } v]
755 lappend ::preupdate $v
763 db preupdate hook preupdate_hook
765 do_execsql_test 7.6.1 {
766 CREATE TABLE t1(x PRIMARY KEY);
767 CREATE TABLE t2(x PRIMARY KEY);
768 CREATE TABLE t3(x PRIMARY KEY);
769 CREATE TABLE t4(x PRIMARY KEY);
771 CREATE TRIGGER a AFTER INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END;
772 CREATE TRIGGER b AFTER INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END;
773 CREATE TRIGGER c AFTER INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END;
775 CREATE TRIGGER d AFTER UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END;
776 CREATE TRIGGER e AFTER UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END;
777 CREATE TRIGGER f AFTER UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END;
779 CREATE TRIGGER g AFTER DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END;
780 CREATE TRIGGER h AFTER DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END;
781 CREATE TRIGGER i AFTER DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END;
784 do_preupdate_test 7.6.2 {
785 INSERT INTO t1 VALUES('xyz');
787 INSERT main t1 1 1 0 xyz
788 INSERT main t2 1 1 1 xyz
789 INSERT main t3 1 1 2 xyz
790 INSERT main t4 1 1 3 xyz
792 do_preupdate_test 7.6.3 {
793 UPDATE t1 SET x = 'abc';
795 UPDATE main t1 1 1 0 xyz abc
796 UPDATE main t2 1 1 1 xyz abc
797 UPDATE main t3 1 1 2 xyz abc
798 UPDATE main t4 1 1 3 xyz abc
800 do_preupdate_test 7.6.4 {
801 DELETE FROM t1 WHERE 1;
803 DELETE main t1 1 1 0 abc
804 DELETE main t2 1 1 1 abc
805 DELETE main t3 1 1 2 abc
806 DELETE main t4 1 1 3 abc
809 do_execsql_test 7.6.5 {
810 DROP TRIGGER a; DROP TRIGGER b; DROP TRIGGER c;
811 DROP TRIGGER d; DROP TRIGGER e; DROP TRIGGER f;
812 DROP TRIGGER g; DROP TRIGGER h; DROP TRIGGER i;
814 CREATE TRIGGER a BEFORE INSERT ON t1 BEGIN INSERT INTO t2 VALUES(new.x); END;
815 CREATE TRIGGER b BEFORE INSERT ON t2 BEGIN INSERT INTO t3 VALUES(new.x); END;
816 CREATE TRIGGER c BEFORE INSERT ON t3 BEGIN INSERT INTO t4 VALUES(new.x); END;
818 CREATE TRIGGER d BEFORE UPDATE ON t1 BEGIN UPDATE t2 SET x = new.x; END;
819 CREATE TRIGGER e BEFORE UPDATE ON t2 BEGIN UPDATE t3 SET x = new.x; END;
820 CREATE TRIGGER f BEFORE UPDATE ON t3 BEGIN UPDATE t4 SET x = new.x; END;
822 CREATE TRIGGER g BEFORE DELETE ON t1 BEGIN DELETE FROM t2 WHERE 1; END;
823 CREATE TRIGGER h BEFORE DELETE ON t2 BEGIN DELETE FROM t3 WHERE 1; END;
824 CREATE TRIGGER i BEFORE DELETE ON t3 BEGIN DELETE FROM t4 WHERE 1; END;
827 do_preupdate_test 7.6.6 {
828 INSERT INTO t1 VALUES('xyz');
830 INSERT main t4 1 1 3 xyz
831 INSERT main t3 1 1 2 xyz
832 INSERT main t2 1 1 1 xyz
833 INSERT main t1 1 1 0 xyz
835 do_preupdate_test 7.6.3 {
836 UPDATE t1 SET x = 'abc';
838 UPDATE main t4 1 1 3 xyz abc
839 UPDATE main t3 1 1 2 xyz abc
840 UPDATE main t2 1 1 1 xyz abc
841 UPDATE main t1 1 1 0 xyz abc
843 do_preupdate_test 7.6.4 {
844 DELETE FROM t1 WHERE 1;
846 DELETE main t4 1 1 3 abc
847 DELETE main t3 1 1 2 abc
848 DELETE main t2 1 1 1 abc
849 DELETE main t1 1 1 0 abc
852 # No preupdate callbacks for modifying sqlite_master.
853 ifcapable altertable {
854 do_preupdate_test 8.1 { CREATE TABLE x1(x, y); } { }
855 do_preupdate_test 8.2 { ALTER TABLE x1 ADD COLUMN z } { }
856 do_preupdate_test 8.3 { ALTER TABLE x1 RENAME TO y1 } { }
857 do_preupdate_test 8.4 { CREATE INDEX y1x ON y1(x) } { }
858 do_preupdate_test 8.5 { CREATE VIEW v1 AS SELECT * FROM y1 } { }
859 do_preupdate_test 8.6 { DROP TABLE y1 } { }
862 #-------------------------------------------------------------------------
864 db preupdate hook preupdate_hook
866 ifcapable altertable {
867 do_execsql_test 9.0 {
868 CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
869 CREATE TABLE t2(a, b INTEGER PRIMARY KEY);
871 do_preupdate_test 9.1 {
872 INSERT INTO t1 VALUES(456, NULL, NULL);
874 INSERT main t1 456 456 0 456 {} {}
876 do_execsql_test 9.2 {
877 ALTER TABLE t1 ADD COLUMN d;
879 do_preupdate_test 9.3 {
880 INSERT INTO t1(a, b, c) VALUES(457, NULL, NULL);
882 INSERT main t1 457 457 0 457 {} {} {}
884 do_preupdate_test 9.4 {
885 DELETE FROM t1 WHERE a=456
887 DELETE main t1 456 456 0 456 {} {} {}
889 do_preupdate_test 9.5 {
890 INSERT INTO t2 DEFAULT VALUES;
892 INSERT main t2 1 1 0 {} 1
894 do_preupdate_test 9.6 {
895 INSERT INTO t1 DEFAULT VALUES;
897 INSERT main t1 458 458 0 458 {} {} {}
902 do_execsql_test 10.0 {
903 CREATE TABLE t3(a, b INTEGER PRIMARY KEY);
905 do_preupdate_test 10.1 {
906 INSERT INTO t3 DEFAULT VALUES
908 INSERT main t3 1 1 0 {} 1
910 do_execsql_test 10.2 { SELECT * FROM t3 } {{} 1}
911 do_preupdate_test 10.3 {
912 DELETE FROM t3 WHERE b=1
913 } {DELETE main t3 1 1 0 {} 1}
915 #-------------------------------------------------------------------------
916 # Test that the "update" hook is not fired for operations on the
917 # sqlite_stat1 table performed by ANALYZE, even if a pre-update hook is
921 do_execsql_test 11.1 {
922 CREATE TABLE t1(a, b);
923 CREATE INDEX idx1 ON t1(a);
924 CREATE INDEX idx2 ON t1(b);
926 INSERT INTO t1 VALUES(1, 2);
927 INSERT INTO t1 VALUES(3, 4);
928 INSERT INTO t1 VALUES(5, 6);
929 INSERT INTO t1 VALUES(7, 8);
932 db preupdate hook preupdate_cb
933 db update_hook update_cb
935 proc preupdate_cb {args} { lappend ::res "preupdate" $args }
936 proc update_cb {args} { lappend ::res "update" $args }
943 preupdate {INSERT main sqlite_stat1 1 1}
944 preupdate {INSERT main sqlite_stat1 2 2}
947 do_execsql_test 11.3 {
948 INSERT INTO t1 VALUES(9, 10);
949 INSERT INTO t1 VALUES(11, 12);
950 INSERT INTO t1 VALUES(13, 14);
951 INSERT INTO t1 VALUES(15, 16);
959 preupdate {DELETE main sqlite_stat1 1 1}
960 preupdate {DELETE main sqlite_stat1 2 2}
961 preupdate {INSERT main sqlite_stat1 1 1}
962 preupdate {INSERT main sqlite_stat1 2 2}
966 #-------------------------------------------------------------------------
967 # Test that the pre-update hook is fired for INSERT statements that use
968 # the xfer optimization on without rowid tables.
971 do_execsql_test 12.1 {
972 CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
973 CREATE TABLE t2(a INTEGER PRIMARY KEY, b) WITHOUT ROWID;
975 INSERT INTO t1 VALUES(1, 2);
976 INSERT INTO t1 VALUES(3, 4);
977 INSERT INTO t2 VALUES(5, 6);
978 INSERT INTO t2 VALUES(7, 8);
980 CREATE TABLE t3 (a INTEGER PRIMARY KEY, b) WITHOUT ROWID;
983 db preupdate hook preupdate_cb
984 db update_hook update_cb
986 proc preupdate_cb {args} { lappend ::res "preupdate" $args }
987 proc update_cb {args} { lappend ::res "update" $args }
997 execsql { INSERT INTO t3 SELECT a, b FROM t2 }
999 } {preupdate {INSERT main t3 0 0} preupdate {INSERT main t3 0 0}}
1002 execsql { DELETE FROM t3 }
1004 execsql { INSERT INTO t3 SELECT * FROM t2 }
1006 } {preupdate {INSERT main t3 0 0} preupdate {INSERT main t3 0 0}}
1008 do_execsql_test 12.5 {
1009 CREATE TABLE t4(a COLLATE nocase PRIMARY KEY, b) WITHOUT ROWID;
1010 INSERT INTO t4 VALUES('abc', 1);
1011 INSERT INTO t4 VALUES('DEF', 2);
1020 do_catchsql_test 12.6 {
1021 INSERT INTO t4 VALUES('def', 3);
1022 } {1 {UNIQUE constraint failed: t4.a}}