fts3shared.test 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #
  2. # 2010 September 17
  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. # This file implements regression tests for SQLite library. The
  10. # focus of this file is the interactions between the FTS3/4 module
  11. # and shared-cache mode.
  12. #
  13. set testdir [file dirname $argv0]
  14. source $testdir/tester.tcl
  15. ifcapable !fts3||!shared_cache {
  16. finish_test
  17. return
  18. }
  19. set ::testprefix fts3shared
  20. db close
  21. set ::enable_shared_cache [sqlite3_enable_shared_cache 1]
  22. # Open two connections to the database in shared-cache mode.
  23. #
  24. sqlite3 db test.db
  25. sqlite3 db2 test.db
  26. # Create a virtual FTS3 table. Populate it with some initial data.
  27. #
  28. do_execsql_test fts3shared-1.1 {
  29. CREATE VIRTUAL TABLE t1 USING fts3(x);
  30. BEGIN;
  31. INSERT INTO t1 VALUES('We listened and looked sideways up!');
  32. INSERT INTO t1 VALUES('Fear at my heart, as at a cup,');
  33. INSERT INTO t1 VALUES('My life-blood seemed to sip!');
  34. INSERT INTO t1 VALUES('The stars were dim, and thick the night');
  35. COMMIT;
  36. } {}
  37. # Open a write transaction and insert rows into the FTS3 table. This takes
  38. # a write-lock on the underlying t1_content table.
  39. #
  40. do_execsql_test fts3shared-1.2 {
  41. BEGIN;
  42. INSERT INTO t1 VALUES('The steersman''s face by his lamp gleamed white;');
  43. } {}
  44. # Now try a SELECT on the full-text table. This particular SELECT does not
  45. # read data from the %_content table. But it still attempts to obtain a lock
  46. # on that table and so the SELECT fails.
  47. #
  48. do_test fts3shared-1.3 {
  49. catchsql {
  50. BEGIN;
  51. SELECT rowid FROM t1 WHERE t1 MATCH 'stars'
  52. } db2
  53. } {1 {database table is locked}}
  54. # Verify that the first connection can commit its transaction.
  55. #
  56. do_test fts3shared-1.4 { sqlite3_get_autocommit db } 0
  57. do_execsql_test fts3shared-1.5 { COMMIT } {}
  58. do_test fts3shared-1.6 { sqlite3_get_autocommit db } 1
  59. # Verify that the second connection still has an open transaction.
  60. #
  61. do_test fts3shared-1.6 { sqlite3_get_autocommit db2 } 0
  62. db close
  63. db2 close
  64. #-------------------------------------------------------------------------
  65. # The following tests - fts3shared-2.* - test that unless FTS is bypassed
  66. # and the underlying tables accessed directly, it is not possible for an
  67. # SQLITE_LOCKED error to be enountered when committing an FTS transaction.
  68. #
  69. # Any SQLITE_LOCKED error should be returned when the fts4 (or fts4aux)
  70. # table is first read/written within a transaction, not later on.
  71. #
  72. set LOCKED {1 {database table is locked}}
  73. forcedelete test.db
  74. sqlite3 dbR test.db
  75. sqlite3 dbW test.db
  76. do_test 2.1 {
  77. execsql {
  78. CREATE VIRTUAL TABLE t1 USING fts4;
  79. CREATE TABLE t2ext(a, b);
  80. CREATE VIRTUAL TABLE t2 USING fts4(content=t2ext);
  81. CREATE VIRTUAL TABLE t1aux USING fts4aux(t1);
  82. CREATE VIRTUAL TABLE t2aux USING fts4aux(t2);
  83. INSERT INTO t1 VALUES('a b c');
  84. INSERT INTO t2(rowid, a, b) VALUES(1, 'd e f', 'g h i');
  85. } dbW
  86. } {}
  87. # Test that once [dbW] has written to the FTS table, no client may read
  88. # from the FTS or fts4aux table.
  89. do_test 2.2.1 {
  90. execsql {
  91. BEGIN;
  92. INSERT INTO t1 VALUES('j k l');
  93. } dbW
  94. execsql BEGIN dbR
  95. } {}
  96. do_test 2.2.2 { catchsql "SELECT * FROM t1 WHERE rowid=1" dbR } $LOCKED
  97. do_test 2.2.3 { catchsql "SELECT * FROM t1 WHERE t1 MATCH 'a'" dbR } $LOCKED
  98. do_test 2.2.4 { catchsql "SELECT rowid FROM t1 WHERE t1 MATCH 'a'" dbR } $LOCKED
  99. do_test 2.2.5 { catchsql "SELECT * FROM t1" dbR } $LOCKED
  100. do_test 2.2.6 { catchsql "SELECT * FROM t1aux" dbR } $LOCKED
  101. do_test 2.2.7 { execsql COMMIT dbW } {}
  102. do_test 2.2.8 { execsql COMMIT dbR } {}
  103. # Same test as 2.2.*, except with a content= table.
  104. #
  105. do_test 2.3.1 {
  106. execsql {
  107. BEGIN;
  108. INSERT INTO t2(rowid, a, b) VALUES(2, 'j k l', 'm n o');
  109. } dbW
  110. execsql BEGIN dbR
  111. } {}
  112. do_test 2.3.3 { catchsql "SELECT * FROM t2 WHERE t2 MATCH 'a'" dbR } $LOCKED
  113. do_test 2.3.4 { catchsql "SELECT rowid FROM t2 WHERE t2 MATCH 'a'" dbR } $LOCKED
  114. do_test 2.3.6 { catchsql "SELECT * FROM t2aux" dbR } $LOCKED
  115. do_test 2.3.7 { execsql COMMIT dbW } {}
  116. do_test 2.3.8 { execsql COMMIT dbR } {}
  117. # Test that once a connection has read from the FTS or fts4aux table,
  118. # another connection may not write to the FTS table.
  119. #
  120. foreach {tn sql} {
  121. 1 "SELECT * FROM t1 WHERE rowid=1"
  122. 2 "SELECT * FROM t1 WHERE t1 MATCH 'a'"
  123. 3 "SELECT rowid FROM t1 WHERE t1 MATCH 'a'"
  124. 4 "SELECT * FROM t1"
  125. 5 "SELECT * FROM t1aux"
  126. } {
  127. do_test 2.4.$tn {
  128. execsql BEGIN dbR
  129. execsql $::sql dbR
  130. execsql BEGIN dbW
  131. catchsql "INSERT INTO t1 VALUES('p q r')" dbW
  132. } $LOCKED
  133. execsql ROLLBACK dbR
  134. execsql ROLLBACK dbW
  135. }
  136. # Same test as 2.4.*, except with a content= table.
  137. #
  138. foreach {tn sql} {
  139. 2 "SELECT * FROM t2 WHERE t2 MATCH 'a'"
  140. 3 "SELECT rowid FROM t2 WHERE t2 MATCH 'a'"
  141. 5 "SELECT * FROM t2aux"
  142. } {
  143. do_test 2.5.$tn {
  144. execsql BEGIN dbR
  145. execsql $::sql dbR
  146. execsql BEGIN dbW
  147. catchsql "INSERT INTO t2(rowid, a, b) VALUES(3, 's t u', 'v w x')" dbW
  148. } $LOCKED
  149. execsql ROLLBACK dbR
  150. execsql ROLLBACK dbW
  151. }
  152. dbW close
  153. dbR close
  154. sqlite3_enable_shared_cache $::enable_shared_cache
  155. finish_test