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 #***********************************************************************
12 # This file implements tests to verify that the "testable statements" in
13 # the lang_droptrigger.html document are correct.
16 set testdir [file dirname $argv0]
17 source $testdir/tester.tcl
18 set ::testprefix e_droptrigger
20 ifcapable !trigger { finish_test ; return }
22 proc do_droptrigger_tests {nm args} {
23 uplevel do_select_tests [list e_createtable-$nm] $args
26 proc list_all_triggers {{db db}} {
28 $db eval { PRAGMA database_list } {
29 if {$name == "temp"} {
30 set tbl sqlite_temp_master
32 set tbl "$name.sqlite_master"
35 db eval "SELECT '$name.' || name FROM $tbl WHERE type = 'trigger'"
42 proc droptrigger_reopen_db {{event INSERT}} {
44 forcedelete test.db test.db2
47 set ::triggers_fired [list]
48 proc r {x} { lappend ::triggers_fired $x }
52 ATTACH 'test.db2' AS aux;
54 CREATE TEMP TABLE t1(a, b);
55 INSERT INTO t1 VALUES('a', 'b');
56 CREATE TRIGGER tr1 AFTER $event ON t1 BEGIN SELECT r('temp.tr1') ; END;
58 CREATE TABLE t2(a, b);
59 INSERT INTO t2 VALUES('a', 'b');
60 CREATE TRIGGER tr1 BEFORE $event ON t2 BEGIN SELECT r('main.tr1') ; END;
61 CREATE TRIGGER tr2 AFTER $event ON t2 BEGIN SELECT r('main.tr2') ; END;
63 CREATE TABLE aux.t3(a, b);
64 INSERT INTO t3 VALUES('a', 'b');
65 CREATE TRIGGER aux.tr1 BEFORE $event ON t3 BEGIN SELECT r('aux.tr1') ; END;
66 CREATE TRIGGER aux.tr2 AFTER $event ON t3 BEGIN SELECT r('aux.tr2') ; END;
67 CREATE TRIGGER aux.tr3 AFTER $event ON t3 BEGIN SELECT r('aux.tr3') ; END;
72 # -- syntax diagram drop-trigger-stmt
74 do_droptrigger_tests 1.1 -repair {
79 1 "DROP TRIGGER main.tr1"
80 {main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3}
81 2 "DROP TRIGGER IF EXISTS main.tr1"
82 {main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3}
84 {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3}
85 4 "DROP TRIGGER IF EXISTS tr1"
86 {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3}
88 5 "DROP TRIGGER aux.tr1"
89 {main.tr1 main.tr2 temp.tr1 aux.tr2 aux.tr3}
90 6 "DROP TRIGGER IF EXISTS aux.tr1"
91 {main.tr1 main.tr2 temp.tr1 aux.tr2 aux.tr3}
93 7 "DROP TRIGGER IF EXISTS aux.xxx"
94 {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3}
95 8 "DROP TRIGGER IF EXISTS aux.xxx"
96 {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3}
99 # EVIDENCE-OF: R-61172-15671 The DROP TRIGGER statement removes a
100 # trigger created by the CREATE TRIGGER statement.
102 foreach {tn tbl droptrigger before after} {
103 1 t1 "DROP TRIGGER tr1" {temp.tr1} {}
104 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2}
105 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
107 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1}
108 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1}
109 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
111 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1}
112 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2}
113 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2}
117 droptrigger_reopen_db
118 execsql " INSERT INTO $tbl VALUES('1', '2') "
123 droptrigger_reopen_db
125 execsql " INSERT INTO $tbl VALUES('1', '2') "
130 # EVIDENCE-OF: R-04950-25529 Once removed, the trigger definition is no
131 # longer present in the sqlite_schema (or sqlite_temp_schema) table and
132 # is not fired by any subsequent INSERT, UPDATE or DELETE statements.
134 # Test cases e_droptrigger-1.* test the first part of this statement
135 # (that dropped triggers do not appear in the schema table), and tests
136 # droptrigger-2.* test that dropped triggers are not fired by INSERT
137 # statements. The following tests verify that they are not fired by
138 # UPDATE or DELETE statements.
140 foreach {tn tbl droptrigger before after} {
141 1 t1 "DROP TRIGGER tr1" {temp.tr1} {}
142 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2}
143 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
145 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1}
146 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1}
147 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
149 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1}
150 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2}
151 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2}
155 droptrigger_reopen_db UPDATE
156 execsql "UPDATE $tbl SET a = 'abc'"
161 droptrigger_reopen_db UPDATE
163 execsql "UPDATE $tbl SET a = 'abc'"
167 foreach {tn tbl droptrigger before after} {
168 1 t1 "DROP TRIGGER tr1" {temp.tr1} {}
169 2 t2 "DROP TRIGGER tr1" {main.tr1 main.tr2} {main.tr1 main.tr2}
170 3 t3 "DROP TRIGGER tr1" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
172 4 t1 "DROP TRIGGER tr2" {temp.tr1} {temp.tr1}
173 5 t2 "DROP TRIGGER tr2" {main.tr1 main.tr2} {main.tr1}
174 6 t3 "DROP TRIGGER tr2" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr3 aux.tr2}
176 7 t1 "DROP TRIGGER tr3" {temp.tr1} {temp.tr1}
177 8 t2 "DROP TRIGGER tr3" {main.tr1 main.tr2} {main.tr1 main.tr2}
178 9 t3 "DROP TRIGGER tr3" {aux.tr1 aux.tr3 aux.tr2} {aux.tr1 aux.tr2}
182 droptrigger_reopen_db DELETE
183 execsql "DELETE FROM $tbl"
188 droptrigger_reopen_db DELETE
190 execsql "DELETE FROM $tbl"
195 # EVIDENCE-OF: R-37808-62273 Note that triggers are automatically
196 # dropped when the associated table is dropped.
199 droptrigger_reopen_db
201 } {main.tr1 main.tr2 temp.tr1 aux.tr1 aux.tr2 aux.tr3}
203 droptrigger_reopen_db
204 execsql "DROP TABLE t1"
206 } {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3}
208 droptrigger_reopen_db
209 execsql "DROP TABLE t1"
211 } {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3}
213 droptrigger_reopen_db
214 execsql "DROP TABLE t1"
216 } {main.tr1 main.tr2 aux.tr1 aux.tr2 aux.tr3}