schema4.test 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. # 2010 September 28
  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. The
  12. # focus of this file is testing that a trigger may have the same
  13. # name as an index, view or table in the same database.
  14. #
  15. set testdir [file dirname $argv0]
  16. source $testdir/tester.tcl
  17. #--------------------------------------------------------------------------
  18. # Test organization:
  19. #
  20. # schema4-1.*: Dropping and creating triggers and other objects where
  21. # triggers and at least on other object share a name.
  22. #
  23. # schema4-2.*: Renaming tables where there is a trigger that shares the
  24. # name of the table or one of its indices.
  25. #
  26. do_execsql_test schema4-1.1 {
  27. CREATE TABLE log(x, a, b);
  28. CREATE TABLE tbl(a, b);
  29. CREATE TABLE t1(a, b);
  30. CREATE VIEW v1 AS SELECT * FROM tbl;
  31. CREATE INDEX i1 ON tbl(a);
  32. } {}
  33. do_execsql_test schema4-1.2 {
  34. CREATE TRIGGER t1 AFTER INSERT ON tbl BEGIN
  35. INSERT INTO log VALUES('after insert', new.a, new.b);
  36. END;
  37. CREATE TRIGGER v1 AFTER UPDATE ON tbl BEGIN
  38. INSERT INTO log VALUES('after update', new.a, new.b);
  39. END;
  40. CREATE TRIGGER i1 AFTER DELETE ON tbl BEGIN
  41. INSERT INTO log VALUES('after delete', old.a, old.b);
  42. END;
  43. } {}
  44. do_execsql_test schema4-1.3 {
  45. INSERT INTO tbl VALUES(1, 2);
  46. UPDATE tbl SET b=a+b, a=a+1;
  47. DELETE FROM tbl;
  48. SELECT x, a, b FROM log;
  49. } {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
  50. do_execsql_test schema4-1.4 {
  51. DELETE FROM log;
  52. DROP INDEX i1;
  53. DROP TABLE t1;
  54. DROP VIEW v1;
  55. INSERT INTO tbl VALUES(1, 2);
  56. UPDATE tbl SET b=a+b, a=a+1;
  57. DELETE FROM tbl;
  58. SELECT x, a, b FROM log;
  59. } {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
  60. db close
  61. sqlite3 db test.db
  62. do_execsql_test schema4-1.5 {
  63. DELETE FROM log;
  64. INSERT INTO tbl VALUES(1, 2);
  65. UPDATE tbl SET b=a+b, a=a+1;
  66. DELETE FROM tbl;
  67. SELECT x, a, b FROM log;
  68. } {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
  69. do_execsql_test schema4-1.6 {
  70. CREATE TABLE t1(a, b);
  71. CREATE VIEW v1 AS SELECT * FROM tbl;
  72. CREATE INDEX i1 ON tbl(a);
  73. } {}
  74. ifcapable fts3 {
  75. do_execsql_test schema4-1.7 {
  76. DROP TABLE t1;
  77. CREATE VIRTUAL TABLE t1 USING fts3;
  78. } {}
  79. do_execsql_test schema4-1.8 {
  80. DELETE FROM log;
  81. DROP TABLE t1;
  82. INSERT INTO tbl VALUES(1, 2);
  83. UPDATE tbl SET b=a+b, a=a+1;
  84. DELETE FROM tbl;
  85. SELECT x, a, b FROM log;
  86. } {{after insert} 1 2 {after update} 2 3 {after delete} 2 3}
  87. }
  88. ifcapable altertable {
  89. drop_all_tables
  90. do_execsql_test schema4-2.1 {
  91. CREATE TABLE log(x, a, b);
  92. CREATE TABLE tbl(a, b);
  93. CREATE TABLE t1(a, b);
  94. CREATE INDEX i1 ON t1(a, b);
  95. } {}
  96. do_execsql_test schema4-2.2 {
  97. CREATE TRIGGER t1 AFTER INSERT ON tbl BEGIN
  98. INSERT INTO log VALUES('after insert', new.a, new.b);
  99. END;
  100. CREATE TRIGGER i1 AFTER DELETE ON tbl BEGIN
  101. INSERT INTO log VALUES('after delete', old.a, old.b);
  102. END;
  103. } {}
  104. do_execsql_test schema4-2.3 { ALTER TABLE t1 RENAME TO t2 } {}
  105. do_execsql_test schema4-2.4 {
  106. INSERT INTO tbl VALUES('a', 'b');
  107. DELETE FROM tbl;
  108. SELECT * FROM log;
  109. } {{after insert} a b {after delete} a b}
  110. db close
  111. sqlite3 db test.db
  112. do_execsql_test schema4-2.5 {
  113. DELETE FROM log;
  114. INSERT INTO tbl VALUES('c', 'd');
  115. DELETE FROM tbl;
  116. SELECT * FROM log;
  117. } {{after insert} c d {after delete} c d}
  118. do_execsql_test schema4-2.6 {
  119. CREATE TEMP TRIGGER x1 AFTER UPDATE ON tbl BEGIN
  120. INSERT INTO log VALUES('after update', new.a, new.b);
  121. END;
  122. CREATE TEMP TABLE x1(x);
  123. INSERT INTO x1 VALUES(123);
  124. } {}
  125. do_execsql_test schema4-2.8 {
  126. select sql from sqlite_temp_master WHERE type='table';
  127. } {{CREATE TABLE x1(x)}}
  128. do_execsql_test schema4-2.7 { ALTER TABLE tbl RENAME TO tbl2 } {}
  129. do_execsql_test schema4-2.9 {
  130. select sql from sqlite_temp_master WHERE type='table';
  131. } {{CREATE TABLE x1(x)}}
  132. do_execsql_test schema4-2.10 {
  133. DELETE FROM log;
  134. INSERT INTO tbl2 VALUES('e', 'f');
  135. UPDATE tbl2 SET a='g', b='h';
  136. DELETE FROM tbl2;
  137. SELECT * FROM log;
  138. } {{after insert} e f {after update} g h {after delete} g h}
  139. do_execsql_test schema4-2.11 {
  140. INSERT INTO x1 VALUES(456);
  141. SELECT * FROM x1
  142. } {123 456}
  143. }
  144. finish_test