1
0

triggerB.test 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # 2008 April 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice', here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. Specifically,
  12. # it tests updating tables with constraints within a trigger. Ticket #3055.
  13. #
  14. set testdir [file dirname $argv0]
  15. source $testdir/tester.tcl
  16. ifcapable {!trigger} {
  17. finish_test
  18. return
  19. }
  20. # Create test tables with constraints.
  21. #
  22. do_test triggerB-1.1 {
  23. execsql {
  24. CREATE TABLE x(x INTEGER PRIMARY KEY, y INT NOT NULL);
  25. INSERT INTO x(y) VALUES(1);
  26. INSERT INTO x(y) VALUES(1);
  27. CREATE TEMP VIEW vx AS SELECT x, y, 0 AS yy FROM x;
  28. CREATE TEMP TRIGGER tx INSTEAD OF UPDATE OF y ON vx
  29. BEGIN
  30. UPDATE x SET y = new.y WHERE x = new.x;
  31. END;
  32. SELECT * FROM vx;
  33. }
  34. } {1 1 0 2 1 0}
  35. do_test triggerB-1.2 {
  36. execsql {
  37. UPDATE vx SET y = yy;
  38. SELECT * FROM vx;
  39. }
  40. } {1 0 0 2 0 0}
  41. # Added 2008-08-22:
  42. #
  43. # Name resolution within triggers.
  44. #
  45. do_test triggerB-2.1 {
  46. catchsql {
  47. CREATE TRIGGER ty AFTER INSERT ON x BEGIN
  48. SELECT wen.x; -- Unrecognized name
  49. END;
  50. INSERT INTO x VALUES(1,2);
  51. }
  52. } {1 {no such column: wen.x}}
  53. do_test triggerB-2.2 {
  54. catchsql {
  55. CREATE TRIGGER tz AFTER UPDATE ON x BEGIN
  56. SELECT dlo.x; -- Unrecognized name
  57. END;
  58. UPDATE x SET y=y+1;
  59. }
  60. } {1 {no such column: dlo.x}}
  61. do_test triggerB-2.3 {
  62. execsql {
  63. CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
  64. INSERT INTO t2 VALUES(1,2);
  65. CREATE TABLE changes(x,y);
  66. CREATE TRIGGER r1t2 AFTER UPDATE ON t2 BEGIN
  67. INSERT INTO changes VALUES(new.a, new.b);
  68. END;
  69. }
  70. execsql {
  71. UPDATE t2 SET a=a+10;
  72. SELECT * FROM changes;
  73. }
  74. } {11 2}
  75. do_test triggerB-2.4 {
  76. execsql {
  77. CREATE TRIGGER r2t2 AFTER DELETE ON t2 BEGIN
  78. INSERT INTO changes VALUES(old.a, old.c);
  79. END;
  80. }
  81. catchsql {
  82. DELETE FROM t2;
  83. }
  84. } {1 {no such column: old.c}}
  85. # Triggers maintain a mask of columns from the invoking table that are
  86. # used in the trigger body as NEW.column or OLD.column. That mask is then
  87. # used to reduce the amount of information that needs to be loaded into
  88. # the NEW and OLD pseudo-tables at run-time.
  89. #
  90. # These tests cases check the logic for when there are many columns - more
  91. # than will fit in a bitmask.
  92. #
  93. do_test triggerB-3.1 {
  94. execsql {
  95. CREATE TABLE t3(
  96. c0, c1, c2, c3, c4, c5, c6, c7, c8, c9,
  97. c10, c11, c12, c13, c14, c15, c16, c17, c18, c19,
  98. c20, c21, c22, c23, c24, c25, c26, c27, c28, c29,
  99. c30, c31, c32, c33, c34, c35, c36, c37, c38, c39,
  100. c40, c41, c42, c43, c44, c45, c46, c47, c48, c49,
  101. c50, c51, c52, c53, c54, c55, c56, c57, c58, c59,
  102. c60, c61, c62, c63, c64, c65
  103. );
  104. CREATE TABLE t3_changes(colnum, oldval, newval);
  105. INSERT INTO t3 VALUES(
  106. 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9',
  107. 'a10','a11','a12','a13','a14','a15','a16','a17','a18','a19',
  108. 'a20','a21','a22','a23','a24','a25','a26','a27','a28','a29',
  109. 'a30','a31','a32','a33','a34','a35','a36','a37','a38','a39',
  110. 'a40','a41','a42','a43','a44','a45','a46','a47','a48','a49',
  111. 'a50','a51','a52','a53','a54','a55','a56','a57','a58','a59',
  112. 'a60','a61','a62','a63','a64','a65'
  113. );
  114. }
  115. for {set i 0} {$i<=65} {incr i} {
  116. set sql [subst {
  117. CREATE TRIGGER t3c$i AFTER UPDATE ON t3
  118. WHEN old.c$i!=new.c$i BEGIN
  119. INSERT INTO t3_changes VALUES($i, old.c$i, new.c$i);
  120. END
  121. }]
  122. db eval $sql
  123. }
  124. execsql {
  125. SELECT * FROM t3_changes
  126. }
  127. } {}
  128. for {set i 0} {$i<=64} {incr i} {
  129. do_test triggerB-3.2.$i.1 [subst {
  130. execsql {
  131. UPDATE t3 SET c$i='b$i';
  132. SELECT * FROM t3_changes ORDER BY rowid DESC LIMIT 1;
  133. }
  134. }] [subst {$i a$i b$i}]
  135. do_test triggerB-3.2.$i.2 [subst {
  136. execsql {
  137. SELECT count(*) FROM t3_changes
  138. }
  139. }] [expr {$i+1}]
  140. do_test triggerB-3.2.$i.2 [subst {
  141. execsql {
  142. SELECT * FROM t3_changes WHERE colnum=$i
  143. }
  144. }] [subst {$i a$i b$i}]
  145. }
  146. finish_test