3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/fts3_common.tcl
16 set ::testprefix fts4onepass
18 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
25 CREATE VIRTUAL TABLE ft USING fts3;
26 INSERT INTO ft(rowid, content) VALUES(1, '1 2 3');
27 INSERT INTO ft(rowid, content) VALUES(2, '4 5 6');
28 INSERT INTO ft(rowid, content) VALUES(3, '7 8 9');
31 #-------------------------------------------------------------------------
32 # Check that UPDATE and DELETE statements that feature "WHERE rowid=?" or
33 # or "WHERE docid=?" clauses do not use statement journals. But that other
34 # DELETE and UPDATE statements do.
36 # Note: "MATCH ? AND docid=?" does use a statement journal.
38 foreach {tn sql uses} {
39 1.1 { DELETE FROM ft } 1
40 1.2 { DELETE FROM ft WHERE docid=? } 0
41 1.3 { DELETE FROM ft WHERE rowid=? } 0
42 1.4 { DELETE FROM ft WHERE ft MATCH '1' } 1
43 1.5 { DELETE FROM ft WHERE ft MATCH '1' AND docid=? } 1
44 1.6 { DELETE FROM ft WHERE ft MATCH '1' AND rowid=? } 1
46 2.1 { UPDATE ft SET content='a b c' } 1
47 2.2 { UPDATE ft SET content='a b c' WHERE docid=? } 0
48 2.3 { UPDATE ft SET content='a b c' WHERE rowid=? } 0
49 2.4 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' } 1
50 2.5 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' AND docid=? } 1
51 2.6 { UPDATE ft SET content='a b c' WHERE ft MATCH '1' AND rowid=? } 1
53 do_test 1.$tn { sql_uses_stmt db $sql } $uses
56 #-------------------------------------------------------------------------
57 # Check that putting a "DELETE/UPDATE ... WHERE rowid=?" statement in a
58 # trigger program does not prevent the VM from using a statement
59 # transaction. Even if the calling statement cannot hit a constraint.
64 CREATE TRIGGER t1_ai AFTER INSERT ON t1 BEGIN
65 DELETE FROM ft WHERE rowid=new.x;
68 CREATE TRIGGER t1_ad AFTER DELETE ON t1 BEGIN
69 UPDATE ft SET content = 'a b c' WHERE rowid=old.x;
72 CREATE TRIGGER t1_bu BEFORE UPDATE ON t1 BEGIN
73 DELETE FROM ft WHERE rowid=old.x;
77 foreach {tn sql uses} {
78 1 { INSERT INTO t1 VALUES(1) } 1
79 2 { DELETE FROM t1 WHERE x=4 } 1
80 3 { UPDATE t1 SET x=10 WHERE x=11 } 1
82 do_test 2.$tn { sql_uses_stmt db $sql } $uses
85 #-------------------------------------------------------------------------
86 # Test that an "UPDATE ... WHERE rowid=?" works and does not corrupt the
87 # index when it strikes a constraint. Both inside and outside a
90 foreach {tn tcl1 tcl2} {
96 if {[sqlite3_get_autocommit db]==1} { error "transaction rolled back!" }
101 do_execsql_test 3.$tn.0 {
102 DROP TABLE IF EXISTS ft2;
103 CREATE VIRTUAL TABLE ft2 USING fts4;
104 INSERT INTO ft2(rowid, content) VALUES(1, 'a b c');
105 INSERT INTO ft2(rowid, content) VALUES(2, 'a b d');
106 INSERT INTO ft2(rowid, content) VALUES(3, 'a b e');
110 foreach {tn2 sql content} {
111 1 { UPDATE ft2 SET docid=2 WHERE docid=1 }
112 { 1 {a b c} 2 {a b d} 3 {a b e} }
115 INSERT INTO ft2(rowid, content) VALUES(4, 'a b f');
116 UPDATE ft2 SET docid=5 WHERE docid=4;
117 UPDATE ft2 SET docid=3 WHERE docid=5;
118 } { 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
121 UPDATE ft2 SET docid=3 WHERE docid=4; -- matches 0 rows
122 UPDATE ft2 SET docid=2 WHERE docid=3;
123 } { 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
126 INSERT INTO ft2(rowid, content) VALUES(4, 'a b g');
127 UPDATE ft2 SET docid=-1 WHERE docid=4;
128 UPDATE ft2 SET docid=3 WHERE docid=-1;
129 } {-1 {a b g} 1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
132 DELETE FROM ft2 WHERE rowid=451;
133 DELETE FROM ft2 WHERE rowid=-1;
134 UPDATE ft2 SET docid = 2 WHERE docid = 1;
135 } {1 {a b c} 2 {a b d} 3 {a b e} 5 {a b f} }
137 do_catchsql_test 3.$tn.$tn2.a $sql {1 {constraint failed}}
138 do_execsql_test 3.$tn.$tn2.b { SELECT rowid, content FROM ft2 } $content
139 do_execsql_test 3.$tn.$tn2.c {
140 INSERT INTO ft2(ft2) VALUES('integrity-check');
146 do_execsql_test 4.0 {
147 CREATE VIRTUAL TABLE zt USING fts4(a, b);
148 INSERT INTO zt(rowid, a, b) VALUES(1, 'unus duo', NULL);
149 INSERT INTO zt(rowid, a, b) VALUES(2, NULL, NULL);
152 UPDATE zt SET b='septum' WHERE rowid = 1;
153 UPDATE zt SET b='octo' WHERE rowid = 1;
156 SELECT count(*) FROM zt_segdir;