OLD | NEW |
1 # 2009 August 24 | 1 # 2009 August 24 |
2 # | 2 # |
3 # The author disclaims copyright to this source code. In place of | 3 # The author disclaims copyright to this source code. In place of |
4 # a legal notice', here is a blessing: | 4 # a legal notice', here is a blessing: |
5 # | 5 # |
6 # May you do good and not evil. | 6 # May you do good and not evil. |
7 # May you find forgiveness for yourself and forgive others. | 7 # May you find forgiveness for yourself and forgive others. |
8 # May you share freely, never taking more than you give. | 8 # May you share freely, never taking more than you give. |
9 # | 9 # |
10 #*********************************************************************** | 10 #*********************************************************************** |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 catchsql { | 297 catchsql { |
298 INSERT INTO t3b VALUES(1); | 298 INSERT INTO t3b VALUES(1); |
299 } | 299 } |
300 } {1 {too many levels of trigger recursion}} | 300 } {1 {too many levels of trigger recursion}} |
301 do_test triggerC-3.2.2 { | 301 do_test triggerC-3.2.2 { |
302 db eval {SELECT * FROM t3b} | 302 db eval {SELECT * FROM t3b} |
303 } {} | 303 } {} |
304 | 304 |
305 do_test triggerC-3.3.1 { | 305 do_test triggerC-3.3.1 { |
306 catchsql { | 306 catchsql { |
307 INSERT INTO t3b VALUES(1000); | 307 INSERT INTO t3b VALUES(1001); |
308 } | 308 } |
309 } {0 {}} | 309 } {0 {}} |
310 do_test triggerC-3.3.2 { | 310 do_test triggerC-3.3.2 { |
311 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 311 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
312 } {1001 2000 1000} | 312 } {1000 2000 1001} |
313 | 313 |
314 do_test triggerC-3.4.1 { | 314 do_test triggerC-3.4.1 { |
315 catchsql { | 315 catchsql { |
316 DELETE FROM t3b; | 316 DELETE FROM t3b; |
317 INSERT INTO t3b VALUES(999); | 317 INSERT INTO t3b VALUES(999); |
318 } | 318 } |
319 } {1 {too many levels of trigger recursion}} | 319 } {1 {too many levels of trigger recursion}} |
320 do_test triggerC-3.4.2 { | 320 do_test triggerC-3.4.2 { |
321 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 321 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
322 } {0 {} {}} | 322 } {0 {} {}} |
323 | 323 |
324 do_test triggerC-3.5.1 { | 324 do_test triggerC-3.5.1 { |
325 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 100 | 325 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 100 |
326 catchsql { | 326 catchsql { |
327 INSERT INTO t3b VALUES(1900); | 327 INSERT INTO t3b VALUES(1901); |
328 } | 328 } |
329 } {0 {}} | 329 } {0 {}} |
330 do_test triggerC-3.5.2 { | 330 do_test triggerC-3.5.2 { |
331 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 331 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
332 } {101 2000 1900} | 332 } {100 2000 1901} |
333 | 333 |
334 do_test triggerC-3.5.3 { | 334 do_test triggerC-3.5.3 { |
335 catchsql { | 335 catchsql { |
336 DELETE FROM t3b; | 336 DELETE FROM t3b; |
337 INSERT INTO t3b VALUES(1899); | 337 INSERT INTO t3b VALUES(1900); |
338 } | 338 } |
339 } {1 {too many levels of trigger recursion}} | 339 } {1 {too many levels of trigger recursion}} |
340 do_test triggerC-3.5.4 { | 340 do_test triggerC-3.5.4 { |
341 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 341 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
342 } {0 {} {}} | 342 } {0 {} {}} |
343 | 343 |
344 do_test triggerC-3.6.1 { | 344 do_test triggerC-3.6.1 { |
345 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1 | 345 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1 |
346 catchsql { | 346 catchsql { |
347 INSERT INTO t3b VALUES(1999); | 347 INSERT INTO t3b VALUES(2000); |
348 } | 348 } |
349 } {0 {}} | 349 } {0 {}} |
350 do_test triggerC-3.6.2 { | 350 do_test triggerC-3.6.2 { |
351 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 351 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
352 } {2 2000 1999} | 352 } {1 2000 2000} |
353 | 353 |
354 do_test triggerC-3.6.3 { | 354 do_test triggerC-3.6.3 { |
355 catchsql { | 355 catchsql { |
356 DELETE FROM t3b; | 356 DELETE FROM t3b; |
357 INSERT INTO t3b VALUES(1998); | 357 INSERT INTO t3b VALUES(1999); |
358 } | 358 } |
359 } {1 {too many levels of trigger recursion}} | 359 } {1 {too many levels of trigger recursion}} |
360 do_test triggerC-3.6.4 { | 360 do_test triggerC-3.6.4 { |
361 db eval {SELECT count(*), max(x), min(x) FROM t3b} | 361 db eval {SELECT count(*), max(x), min(x) FROM t3b} |
362 } {0 {} {}} | 362 } {0 {} {}} |
363 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000 | 363 sqlite3_limit db SQLITE_LIMIT_TRIGGER_DEPTH 1000 |
364 | 364 |
365 | 365 |
366 #----------------------------------------------------------------------- | 366 #----------------------------------------------------------------------- |
367 # This next block of tests, triggerC-4.*, checks that affinity | 367 # This next block of tests, triggerC-4.*, checks that affinity |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 do_test triggerC-7.9 { | 754 do_test triggerC-7.9 { |
755 execsql { | 755 execsql { |
756 BEGIN; | 756 BEGIN; |
757 DELETE FROM t7 WHERE a = 1; | 757 DELETE FROM t7 WHERE a = 1; |
758 SELECT rowid, * FROM t7; | 758 SELECT rowid, * FROM t7; |
759 SELECT * FROM t8; | 759 SELECT * FROM t8; |
760 ROLLBACK; | 760 ROLLBACK; |
761 } | 761 } |
762 } {2 3 4 3 5 6 8 1 2} | 762 } {2 3 4 3 5 6 8 1 2} |
763 | 763 |
| 764 # Ticket [e25d9ea771febc9c311928c1c01c3163dcb26643] |
| 765 # |
| 766 do_test triggerC-9.1 { |
| 767 execsql { |
| 768 CREATE TABLE t9(a,b); |
| 769 CREATE INDEX t9b ON t9(b); |
| 770 INSERT INTO t9 VALUES(1,0); |
| 771 INSERT INTO t9 VALUES(2,1); |
| 772 INSERT INTO t9 VALUES(3,2); |
| 773 INSERT INTO t9 SELECT a+3, a+2 FROM t9; |
| 774 INSERT INTO t9 SELECT a+6, a+5 FROM t9; |
| 775 SELECT a FROM t9 ORDER BY a; |
| 776 } |
| 777 } {1 2 3 4 5 6 7 8 9 10 11 12} |
| 778 do_test triggerC-9.2 { |
| 779 execsql { |
| 780 CREATE TRIGGER t9r1 AFTER DELETE ON t9 BEGIN |
| 781 DELETE FROM t9 WHERE b=old.a; |
| 782 END; |
| 783 DELETE FROM t9 WHERE b=4; |
| 784 SELECT a FROM t9 ORDER BY a; |
| 785 } |
| 786 } {1 2 3 4} |
| 787 |
| 788 # At one point (between versions 3.6.18 and 3.6.20 inclusive), an UPDATE |
| 789 # that fired a BEFORE trigger that itself updated the same row as the |
| 790 # statement causing it to fire was causing a strange side-effect: The |
| 791 # values updated by the statement within the trigger were being overwritten |
| 792 # by the values in the new.* array, even if those values were not |
| 793 # themselves written by the parent UPDATE statement. |
| 794 # |
| 795 # Technically speaking this was not a bug. The SQLite documentation says |
| 796 # that if a BEFORE UPDATE or BEFORE DELETE trigger modifies or deletes the |
| 797 # row that the parent statement is operating on the results are undefined. |
| 798 # But as of 3.6.21 behaviour is restored to the way it was in versions |
| 799 # 3.6.17 and earlier to avoid causing unnecessary difficulties. |
| 800 # |
| 801 do_test triggerC-10.1 { |
| 802 execsql { |
| 803 CREATE TABLE t10(a, updatecnt DEFAULT 0); |
| 804 CREATE TRIGGER t10_bu BEFORE UPDATE OF a ON t10 BEGIN |
| 805 UPDATE t10 SET updatecnt = updatecnt+1 WHERE rowid = old.rowid; |
| 806 END; |
| 807 INSERT INTO t10(a) VALUES('hello'); |
| 808 } |
| 809 |
| 810 # Before the problem was fixed, table t10 would contain the tuple |
| 811 # (world, 0) after running the following script (because the value |
| 812 # 1 written to column "updatecnt" was clobbered by the old value 0). |
| 813 # |
| 814 execsql { |
| 815 UPDATE t10 SET a = 'world'; |
| 816 SELECT * FROM t10; |
| 817 } |
| 818 } {world 1} |
| 819 |
| 820 do_test triggerC-10.2 { |
| 821 execsql { |
| 822 UPDATE t10 SET a = 'tcl', updatecnt = 5; |
| 823 SELECT * FROM t10; |
| 824 } |
| 825 } {tcl 5} |
| 826 |
| 827 do_test triggerC-10.3 { |
| 828 execsql { |
| 829 CREATE TABLE t11( |
| 830 c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, |
| 831 c11, c12, c13, c14, c15, c16, c17, c18, c19, c20, |
| 832 c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, |
| 833 c31, c32, c33, c34, c35, c36, c37, c38, c39, c40 |
| 834 ); |
| 835 |
| 836 CREATE TRIGGER t11_bu BEFORE UPDATE OF c1 ON t11 BEGIN |
| 837 UPDATE t11 SET c31 = c31+1, c32=c32+1 WHERE rowid = old.rowid; |
| 838 END; |
| 839 |
| 840 INSERT INTO t11 VALUES( |
| 841 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 842 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, |
| 843 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, |
| 844 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 |
| 845 ); |
| 846 } |
| 847 |
| 848 # Before the problem was fixed, table t10 would contain the tuple |
| 849 # (world, 0) after running the following script (because the value |
| 850 # 1 written to column "updatecnt" was clobbered by the old value 0). |
| 851 # |
| 852 execsql { |
| 853 UPDATE t11 SET c4=35, c33=22, c1=5; |
| 854 SELECT * FROM t11; |
| 855 } |
| 856 } {5 2 3 35 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2
9 30 32 33 22 34 35 36 37 38 39 40} |
| 857 |
| 858 #------------------------------------------------------------------------- |
| 859 # Test that bug [371bab5d65] has been fixed. BEFORE INSERT and INSTEAD OF |
| 860 # INSERT triggers with the DEFAULT VALUES INSERT syntax. |
| 861 # |
| 862 do_test triggerC-11.0 { |
| 863 catchsql { DROP TABLE log } |
| 864 execsql { CREATE TABLE log(a, b) } |
| 865 } {} |
| 866 |
| 867 foreach {testno tbl defaults} { |
| 868 1 "CREATE TABLE t1(a, b)" {{} {}} |
| 869 2 "CREATE TABLE t1(a DEFAULT 1, b DEFAULT 'abc')" {1 abc} |
| 870 3 "CREATE TABLE t1(a, b DEFAULT 4.5)" {{} 4.5} |
| 871 } { |
| 872 do_test triggerC-11.$testno.1 { |
| 873 catchsql { DROP TABLE t1 } |
| 874 execsql { DELETE FROM log } |
| 875 execsql $tbl |
| 876 execsql { |
| 877 CREATE TRIGGER tt1 BEFORE INSERT ON t1 BEGIN |
| 878 INSERT INTO log VALUES(new.a, new.b); |
| 879 END; |
| 880 INSERT INTO t1 DEFAULT VALUES; |
| 881 SELECT * FROM log; |
| 882 } |
| 883 } $defaults |
| 884 |
| 885 do_test triggerC-11.$testno.2 { |
| 886 execsql { DELETE FROM log } |
| 887 execsql { |
| 888 CREATE TRIGGER tt2 AFTER INSERT ON t1 BEGIN |
| 889 INSERT INTO log VALUES(new.a, new.b); |
| 890 END; |
| 891 INSERT INTO t1 DEFAULT VALUES; |
| 892 SELECT * FROM log; |
| 893 } |
| 894 } [concat $defaults $defaults] |
| 895 |
| 896 do_test triggerC-11.$testno.3 { |
| 897 execsql { DROP TRIGGER tt1 } |
| 898 execsql { DELETE FROM log } |
| 899 execsql { |
| 900 INSERT INTO t1 DEFAULT VALUES; |
| 901 SELECT * FROM log; |
| 902 } |
| 903 } $defaults |
| 904 } |
| 905 do_test triggerC-11.4 { |
| 906 catchsql { DROP TABLE t2 } |
| 907 execsql { |
| 908 DELETE FROM log; |
| 909 CREATE TABLE t2(a, b); |
| 910 CREATE VIEW v2 AS SELECT * FROM t2; |
| 911 CREATE TRIGGER tv2 INSTEAD OF INSERT ON v2 BEGIN |
| 912 INSERT INTO log VALUES(new.a, new.b); |
| 913 END; |
| 914 INSERT INTO v2 DEFAULT VALUES; |
| 915 SELECT a, b, a IS NULL, b IS NULL FROM log; |
| 916 } |
| 917 } {{} {} 1 1} |
| 918 |
| 919 do_test triggerC-12.1 { |
| 920 db close |
| 921 file delete -force test.db |
| 922 sqlite3 db test.db |
| 923 |
| 924 execsql { |
| 925 CREATE TABLE t1(a, b); |
| 926 INSERT INTO t1 VALUES(1, 2); |
| 927 INSERT INTO t1 VALUES(3, 4); |
| 928 INSERT INTO t1 VALUES(5, 6); |
| 929 CREATE TRIGGER tr1 AFTER INSERT ON t1 BEGIN SELECT 1 ; END ; |
| 930 SELECT count(*) FROM sqlite_master; |
| 931 } |
| 932 } {2} |
| 933 do_test triggerC-12.2 { |
| 934 db eval { SELECT * FROM t1 } { |
| 935 if {$a == 3} { execsql { DROP TRIGGER tr1 } } |
| 936 } |
| 937 execsql { SELECT count(*) FROM sqlite_master } |
| 938 } {1} |
| 939 |
| 940 |
764 | 941 |
765 finish_test | 942 finish_test |
OLD | NEW |