trigger3.test 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 the RAISE() function.
  11. #
  12. set testdir [file dirname $argv0]
  13. source $testdir/tester.tcl
  14. # Test that we can cause ROLLBACK, FAIL and ABORT correctly
  15. # catchsql { DROP TABLE tbl; }
  16. catchsql { CREATE TABLE tbl (a, b, c) }
  17. execsql {
  18. CREATE TRIGGER before_tbl_insert BEFORE INSERT ON tbl BEGIN SELECT CASE
  19. WHEN (new.a = 4) THEN RAISE(IGNORE) END;
  20. END;
  21. CREATE TRIGGER after_tbl_insert AFTER INSERT ON tbl BEGIN SELECT CASE
  22. WHEN (new.a = 1) THEN RAISE(ABORT, 'Trigger abort')
  23. WHEN (new.a = 2) THEN RAISE(FAIL, 'Trigger fail')
  24. WHEN (new.a = 3) THEN RAISE(ROLLBACK, 'Trigger rollback') END;
  25. END;
  26. }
  27. # ABORT
  28. do_test trig-raise-1.1 {
  29. catchsql {
  30. BEGIN;
  31. INSERT INTO tbl VALUES (5, 5, 6);
  32. INSERT INTO tbl VALUES (1, 5, 6);
  33. }
  34. } {1 {Trigger abort}}
  35. do_test trig-raise-1.2 {
  36. execsql {
  37. SELECT * FROM tbl;
  38. ROLLBACK;
  39. }
  40. } {5 5 6}
  41. # FAIL
  42. do_test trig-raise-2.1 {
  43. catchsql {
  44. BEGIN;
  45. INSERT INTO tbl VALUES (5, 5, 6);
  46. INSERT INTO tbl VALUES (2, 5, 6);
  47. }
  48. } {1 {Trigger fail}}
  49. do_test trig-raise-2.2 {
  50. execsql {
  51. SELECT * FROM tbl;
  52. ROLLBACK;
  53. }
  54. } {5 5 6 2 5 6}
  55. # ROLLBACK
  56. do_test trig-raise-3.1 {
  57. catchsql {
  58. BEGIN;
  59. INSERT INTO tbl VALUES (5, 5, 6);
  60. INSERT INTO tbl VALUES (3, 5, 6);
  61. }
  62. } {1 {Trigger rollback}}
  63. do_test trig-raise-3.2 {
  64. execsql {
  65. SELECT * FROM tbl;
  66. ROLLBACK;
  67. }
  68. } {}
  69. # IGNORE
  70. do_test trig-raise-4.1 {
  71. catchsql {
  72. BEGIN;
  73. INSERT INTO tbl VALUES (5, 5, 6);
  74. INSERT INTO tbl VALUES (4, 5, 6);
  75. }
  76. } {0 {}}
  77. do_test trig-raise-4.2 {
  78. execsql {
  79. SELECT * FROM tbl;
  80. ROLLBACK;
  81. }
  82. } {5 5 6}
  83. # Check that we can also do RAISE(IGNORE) for UPDATE and DELETE
  84. execsql {DROP TABLE tbl;}
  85. execsql {CREATE TABLE tbl (a, b, c);}
  86. execsql {INSERT INTO tbl VALUES(1, 2, 3);}
  87. execsql {INSERT INTO tbl VALUES(4, 5, 6);}
  88. execsql {
  89. CREATE TRIGGER before_tbl_update BEFORE UPDATE ON tbl BEGIN
  90. SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
  91. END;
  92. CREATE TRIGGER before_tbl_delete BEFORE DELETE ON tbl BEGIN
  93. SELECT CASE WHEN (old.a = 1) THEN RAISE(IGNORE) END;
  94. END;
  95. }
  96. do_test trig-raise-5.1 {
  97. execsql {
  98. UPDATE tbl SET c = 10;
  99. SELECT * FROM tbl;
  100. }
  101. } {1 2 3 4 5 10}
  102. do_test trig-raise-5.2 {
  103. execsql {
  104. DELETE FROM tbl;
  105. SELECT * FROM tbl;
  106. }
  107. } {1 2 3}
  108. # Check that RAISE(IGNORE) works correctly for nested triggers:
  109. execsql {CREATE TABLE tbl2(a, b, c)}
  110. execsql {
  111. CREATE TRIGGER after_tbl2_insert AFTER INSERT ON tbl2 BEGIN
  112. UPDATE tbl SET c = 10;
  113. INSERT INTO tbl2 VALUES (new.a, new.b, new.c);
  114. END;
  115. }
  116. do_test trig-raise-6 {
  117. execsql {
  118. INSERT INTO tbl2 VALUES (1, 2, 3);
  119. SELECT * FROM tbl2;
  120. SELECT * FROM tbl;
  121. }
  122. } {1 2 3 1 2 3 1 2 3}
  123. # Check that things also work for view-triggers
  124. execsql {CREATE VIEW tbl_view AS SELECT * FROM tbl}
  125. execsql {
  126. CREATE TRIGGER tbl_view_insert INSTEAD OF INSERT ON tbl_view BEGIN
  127. SELECT CASE WHEN (new.a = 1) THEN RAISE(ROLLBACK, 'View rollback')
  128. WHEN (new.a = 2) THEN RAISE(IGNORE)
  129. WHEN (new.a = 3) THEN RAISE(ABORT, 'View abort') END;
  130. END;
  131. }
  132. do_test trig-raise-7.1 {
  133. catchsql {
  134. INSERT INTO tbl_view VALUES(1, 2, 3);
  135. }
  136. } {1 {View rollback}}
  137. do_test trig-raise-7.2 {
  138. catchsql {
  139. INSERT INTO tbl_view VALUES(2, 2, 3);
  140. }
  141. } {0 {}}
  142. do_test trig-raise-7.3 {
  143. catchsql {
  144. INSERT INTO tbl_view VALUES(3, 2, 3);
  145. }
  146. } {1 {View abort}}
  147. catchsql { DROP TABLE tbl; }
  148. catchsql { DROP TABLE tbl2; }
  149. catchsql { DROP VIEW tbl_view; }