trigger1.test 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # The author disclaims copyright to this source code. In place of
  2. # a legal notice, here is a blessing:
  3. #
  4. # May you do good and not evil.
  5. # May you find forgiveness for yourself and forgive others.
  6. # May you share freely, never taking more than you give.
  7. #
  8. #***********************************************************************
  9. #
  10. # This file tests creating and dropping triggers, and interaction thereof
  11. # with the database COMMIT/ROLLBACK logic.
  12. #
  13. # 1. CREATE and DROP TRIGGER tests
  14. # trig-1.1: Error if table does not exist
  15. # trig-1.2: Error if trigger already exists
  16. # trig-1.3: Created triggers are deleted if the transaction is rolled back
  17. # trig-1.4: DROP TRIGGER removes trigger
  18. # trig-1.5: Dropped triggers are restored if the transaction is rolled back
  19. # trig-1.6: Error if dropped trigger doesn't exist
  20. # trig-1.7: Dropping the table automatically drops all triggers
  21. # trig-1.8: A trigger created on a TEMP table is not inserted into sqlite_master
  22. # trig-1.9: Ensure that we cannot create a trigger on sqlite_master
  23. # trig-1.10:
  24. # trig-1.11:
  25. # trig-1.12: Ensure that INSTEAD OF triggers cannot be created on tables
  26. # trig-1.13: Ensure that AFTER triggers cannot be created on views
  27. # trig-1.14: Ensure that BEFORE triggers cannot be created on views
  28. #
  29. set testdir [file dirname $argv0]
  30. source $testdir/tester.tcl
  31. do_test trig_cd-1.1 {
  32. catchsql {
  33. CREATE TRIGGER trig UPDATE ON no_such_table BEGIN
  34. SELECT * from sqlite_master;
  35. END;
  36. }
  37. } {1 {no such table: no_such_table}}
  38. execsql {
  39. CREATE TABLE t1(a);
  40. }
  41. execsql {
  42. CREATE TRIGGER tr1 INSERT ON t1 BEGIN
  43. INSERT INTO t1 values(1);
  44. END;
  45. }
  46. do_test trig_cd-1.2 {
  47. catchsql {
  48. CREATE TRIGGER tr1 DELETE ON t1 BEGIN
  49. SELECT * FROM sqlite_master;
  50. END
  51. }
  52. } {1 {trigger tr1 already exists}}
  53. do_test trig_cd-1.3 {
  54. catchsql {
  55. BEGIN;
  56. CREATE TRIGGER tr2 INSERT ON t1 BEGIN
  57. SELECT * from sqlite_master; END;
  58. ROLLBACK;
  59. CREATE TRIGGER tr2 INSERT ON t1 BEGIN
  60. SELECT * from sqlite_master; END;
  61. }
  62. } {0 {}}
  63. do_test trig_cd-1.4 {
  64. catchsql {
  65. DROP TRIGGER tr1;
  66. CREATE TRIGGER tr1 DELETE ON t1 BEGIN
  67. SELECT * FROM sqlite_master;
  68. END
  69. }
  70. } {0 {}}
  71. do_test trig_cd-1.5 {
  72. execsql {
  73. BEGIN;
  74. DROP TRIGGER tr2;
  75. ROLLBACK;
  76. DROP TRIGGER tr2;
  77. }
  78. } {}
  79. do_test trig_cd-1.6 {
  80. catchsql {
  81. DROP TRIGGER biggles;
  82. }
  83. } {1 {no such trigger: biggles}}
  84. do_test trig_cd-1.7 {
  85. catchsql {
  86. DROP TABLE t1;
  87. DROP TRIGGER tr1;
  88. }
  89. } {1 {no such trigger: tr1}}
  90. execsql {
  91. CREATE TEMP TABLE temp_table(a);
  92. }
  93. do_test trig_cd-1.8 {
  94. execsql {
  95. CREATE TRIGGER temp_trig UPDATE ON temp_table BEGIN
  96. SELECT * from sqlite_master;
  97. END;
  98. SELECT count(*) FROM sqlite_master WHERE name = 'temp_trig';
  99. }
  100. } {0}
  101. do_test trig_cd-1.9 {
  102. catchsql {
  103. CREATE TRIGGER tr1 AFTER UPDATE ON sqlite_master BEGIN
  104. SELECT * FROM sqlite_master;
  105. END;
  106. }
  107. } {1 {cannot create trigger on system table: sqlite_master}}
  108. # Check to make sure that a DELETE statement within the body of
  109. # a trigger does not mess up the DELETE that caused the trigger to
  110. # run in the first place.
  111. #
  112. do_test trig_cd-1.10 {
  113. execsql {
  114. create table t1(a,b);
  115. insert into t1 values(1,'a');
  116. insert into t1 values(2,'b');
  117. insert into t1 values(3,'c');
  118. insert into t1 values(4,'d');
  119. create trigger r1 after delete on t1 for each row begin
  120. delete from t1 WHERE a=old.a+2;
  121. end;
  122. delete from t1 where a in (1,3);
  123. select * from t1;
  124. drop table t1;
  125. }
  126. } {2 b 4 d}
  127. do_test trig_cd-1.11 {
  128. execsql {
  129. create table t1(a,b);
  130. insert into t1 values(1,'a');
  131. insert into t1 values(2,'b');
  132. insert into t1 values(3,'c');
  133. insert into t1 values(4,'d');
  134. create trigger r1 after update on t1 for each row begin
  135. delete from t1 WHERE a=old.a+2;
  136. end;
  137. update t1 set b='x-' || b where a in (1,3);
  138. select * from t1;
  139. drop table t1;
  140. }
  141. } {1 x-a 2 b 4 d}
  142. # Ensure that we cannot create INSTEAD OF triggers on tables
  143. do_test trig_cd-1.12 {
  144. catchsql {
  145. create table t1(a,b);
  146. create trigger t1t instead of update on t1 for each row begin
  147. delete from t1 WHERE a=old.a+2;
  148. end;
  149. }
  150. } {1 {cannot create INSTEAD OF trigger on table: t1}}
  151. # Ensure that we cannot create BEFORE triggers on views
  152. do_test trig_cd-1.13 {
  153. catchsql {
  154. create view v1 as select * from t1;
  155. create trigger v1t before update on v1 for each row begin
  156. delete from t1 WHERE a=old.a+2;
  157. end;
  158. }
  159. } {1 {cannot create BEFORE trigger on view: v1}}
  160. # Ensure that we cannot create AFTER triggers on views
  161. do_test trig_cd-1.14 {
  162. catchsql {
  163. create table t1(a,b);
  164. create view v1 as select * from t1;
  165. create trigger v1t AFTER update on v1 for each row begin
  166. delete from t1 WHERE a=old.a+2;
  167. end;
  168. }
  169. } {1 {cannot create AFTER trigger on view: v1}}
  170. finish_test