fts5onepass.test 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. # 2015 Sep 27
  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. #
  12. source [file join [file dirname [info script]] fts5_common.tcl]
  13. set testprefix fts5onepass
  14. # If SQLITE_ENABLE_FTS3 is defined, omit this file.
  15. ifcapable !fts5 {
  16. finish_test
  17. return
  18. }
  19. do_execsql_test 1.0 {
  20. CREATE VIRTUAL TABLE ft USING fts5(content);
  21. INSERT INTO ft(rowid, content) VALUES(1, '1 2 3');
  22. INSERT INTO ft(rowid, content) VALUES(2, '4 5 6');
  23. INSERT INTO ft(rowid, content) VALUES(3, '7 8 9');
  24. }
  25. #-------------------------------------------------------------------------
  26. # Check that UPDATE and DELETE statements that feature "WHERE rowid=?" or
  27. # or "WHERE rowid=?" clauses do not use statement journals. But that other
  28. # DELETE and UPDATE statements do.
  29. #
  30. # Note: "MATCH ? AND rowid=?" does use a statement journal.
  31. #
  32. foreach {tn sql uses} {
  33. 1.1 { DELETE FROM ft } 1
  34. 1.2 { DELETE FROM ft WHERE rowid=? } 0
  35. 1.3 { DELETE FROM ft WHERE rowid=? } 0
  36. 1.4 { DELETE FROM ft WHERE ft MATCH '1' } 1
  37. 1.5 { DELETE FROM ft WHERE ft MATCH '1' AND rowid=? } 1
  38. 1.6 { DELETE FROM ft WHERE ft MATCH '1' AND rowid=? } 1
  39. 2.1 { UPDATE ft SET content='a b c' } 1
  40. 2.2 { UPDATE ft SET content='a b c' WHERE rowid=? } 0
  41. 2.3 { UPDATE ft SET content='a b c' WHERE rowid=? } 0
  42. 2.4 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' } 1
  43. 2.5 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' AND rowid=? } 1
  44. 2.6 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' AND rowid=? } 1
  45. } {
  46. do_test 1.$tn { sql_uses_stmt db $sql } $uses
  47. }
  48. #-------------------------------------------------------------------------
  49. # Check that putting a "DELETE/UPDATE ... WHERE rowid=?" statement in a
  50. # trigger program does not prevent the VM from using a statement
  51. # transaction. Even if the calling statement cannot hit a constraint.
  52. #
  53. do_execsql_test 2.0 {
  54. CREATE TABLE t1(x);
  55. CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
  56. DELETE FROM ft WHERE rowid=new.x;
  57. END;
  58. CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
  59. UPDATE ft SET content = 'a b c' WHERE rowid=old.x;
  60. END;
  61. CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
  62. DELETE FROM ft WHERE rowid=old.x;
  63. END;
  64. }
  65. foreach {tn sql uses} {
  66. 1 { INSERT INTO t1 VALUES(1) } 1
  67. 2 { DELETE FROM t1 WHERE x=4 } 1
  68. 3 { UPDATE t1 SET x=10 WHERE x=11 } 1
  69. } {
  70. do_test 2.$tn { sql_uses_stmt db $sql } $uses
  71. }
  72. #-------------------------------------------------------------------------
  73. # Test that an "UPDATE ... WHERE rowid=?" works and does not corrupt the
  74. # index when it strikes a constraint. Both inside and outside a
  75. # transaction.
  76. #
  77. foreach {tn tcl1 tcl2} {
  78. 1 {} {}
  79. 2 {
  80. execsql BEGIN
  81. } {
  82. if {[sqlite3_get_autocommit db]==1} { error "transaction rolled back!" }
  83. execsql COMMIT
  84. }
  85. } {
  86. do_execsql_test 3.$tn.0 {
  87. DROP TABLE IF EXISTS ft2;
  88. CREATE VIRTUAL TABLE ft2 USING fts5(content);
  89. INSERT INTO ft2(rowid, content) VALUES(1, 'a b c');
  90. INSERT INTO ft2(rowid, content) VALUES(2, 'a b d');
  91. INSERT INTO ft2(rowid, content) VALUES(3, 'a b e');
  92. }
  93. eval $tcl1
  94. foreach {tn2 sql content} {
  95. 1 { UPDATE ft2 SET rowid=2 WHERE rowid=1 }
  96. { 1 {a b c} 2 {a b d} 3 {a b e} }
  97. 2 {
  98. INSERT INTO ft2(rowid, content) VALUES(4, 'a b f');
  99. UPDATE ft2 SET rowid=5 WHERE rowid=4;
  100. UPDATE ft2 SET rowid=3 WHERE rowid=5;
  101. } { 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
  102. 3 {
  103. UPDATE ft2 SET rowid=3 WHERE rowid=4; -- matches 0 rows
  104. UPDATE ft2 SET rowid=2 WHERE rowid=3;
  105. } { 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
  106. 4 {
  107. INSERT INTO ft2(rowid, content) VALUES(4, 'a b g');
  108. UPDATE ft2 SET rowid=-1 WHERE rowid=4;
  109. UPDATE ft2 SET rowid=3 WHERE rowid=-1;
  110. } {-1 {a b g} 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
  111. 5 {
  112. DELETE FROM ft2 WHERE rowid=451;
  113. DELETE FROM ft2 WHERE rowid=-1;
  114. UPDATE ft2 SET rowid = 2 WHERE rowid = 1;
  115. } {1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
  116. } {
  117. do_catchsql_test 3.$tn.$tn2.a $sql {1 {constraint failed}}
  118. do_execsql_test 3.$tn.$tn2.b { SELECT rowid, content FROM ft2 } $content
  119. do_execsql_test 3.$tn.$tn2.c {
  120. INSERT INTO ft2(ft2) VALUES('integrity-check');
  121. }
  122. }
  123. eval $tcl2
  124. }
  125. #-------------------------------------------------------------------------
  126. # Check that DELETE and UPDATE operations can be done without flushing
  127. # the in-memory hash table to disk.
  128. #
  129. reset_db
  130. do_execsql_test 4.1.1 {
  131. CREATE VIRTUAL TABLE ttt USING fts5(x);
  132. BEGIN;
  133. INSERT INTO ttt(rowid, x) VALUES(1, 'a b c');
  134. INSERT INTO ttt(rowid, x) VALUES(2, 'a b c');
  135. INSERT INTO ttt(rowid, x) VALUES(3, 'a b c');
  136. COMMIT
  137. }
  138. do_test 4.1.2 { fts5_level_segs ttt } {1}
  139. do_execsql_test 4.2.1 {
  140. BEGIN;
  141. DELETE FROM ttt WHERE rowid=1;
  142. DELETE FROM ttt WHERE rowid=3;
  143. INSERT INTO ttt(rowid, x) VALUES(4, 'd e f');
  144. INSERT INTO ttt(rowid, x) VALUES(5, 'd e f');
  145. COMMIT;
  146. } {}
  147. do_test 4.2.2 { fts5_level_segs ttt } {2}
  148. do_execsql_test 4.3.1 {
  149. BEGIN;
  150. UPDATE ttt SET x = 'd e f' WHERE rowid = 2;
  151. UPDATE ttt SET x = 'A B C' WHERE rowid = 4;
  152. INSERT INTO ttt(rowid, x) VALUES(6, 'd e f');
  153. COMMIT;
  154. } {}
  155. do_test 4.2.2 { fts5_level_segs ttt } {3}
  156. finish_test