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 #***********************************************************************
12 # This file contains tests for the content= and content_rowid= options.
15 source [file join [file dirname [info script]] fts5_common.tcl]
16 set testprefix fts5content
18 # If SQLITE_ENABLE_FTS5 is defined, omit this file.
24 #-------------------------------------------------------------------------
28 CREATE VIRTUAL TABLE f1 USING fts5(a, b, content='');
29 INSERT INTO f1(rowid, a, b) VALUES(1, 'one', 'o n e');
30 INSERT INTO f1(rowid, a, b) VALUES(2, 'two', 't w o');
31 INSERT INTO f1(rowid, a, b) VALUES(3, 'three', 't h r e e');
35 SELECT rowid FROM f1 WHERE f1 MATCH 'o';
39 INSERT INTO f1(a, b) VALUES('four', 'f o u r');
40 SELECT rowid FROM f1 WHERE f1 MATCH 'o';
44 SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o';
45 } {1 {} {} 2 {} {} 4 {} {}}
48 SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o';
52 SELECT rowid, highlight(f1, 0, '[', ']') IS NULL FROM f1 WHERE f1 MATCH 'o';
56 SELECT rowid, snippet(f1, -1, '[', ']', '...', 5) IS NULL
57 FROM f1 WHERE f1 MATCH 'o';
61 SELECT rowid, snippet(f1, 1, '[', ']', '...', 5) IS NULL
62 FROM f1 WHERE f1 MATCH 'o';
69 do_execsql_test 1.10 {
71 } {{} {} {} {} {} {} {} {}}
73 do_execsql_test 1.11 {
74 SELECT rowid, a, b FROM f1 ORDER BY rowid ASC;
75 } {1 {} {} 2 {} {} 3 {} {} 4 {} {}}
77 do_execsql_test 1.12 {
78 SELECT a IS NULL FROM f1;
81 do_catchsql_test 1.13 {
82 DELETE FROM f1 WHERE rowid = 2;
83 } {1 {cannot DELETE from contentless fts5 table: f1}}
85 do_catchsql_test 1.14 {
86 UPDATE f1 SET a = 'a b c' WHERE rowid = 2;
87 } {1 {cannot UPDATE contentless fts5 table: f1}}
89 do_execsql_test 1.15 {
90 INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o');
93 do_execsql_test 1.16 {
94 SELECT rowid FROM f1 WHERE f1 MATCH 'o';
97 do_execsql_test 1.17 {
101 #-------------------------------------------------------------------------
102 # External content tables
105 do_execsql_test 2.1 {
106 -- Create a table. And an external content fts5 table to index it.
107 CREATE TABLE tbl(a INTEGER PRIMARY KEY, b, c);
108 CREATE VIRTUAL TABLE fts_idx USING fts5(b, c, content='tbl', content_rowid='a');
110 -- Triggers to keep the FTS index up to date.
111 CREATE TRIGGER tbl_ai AFTER INSERT ON tbl BEGIN
112 INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
114 CREATE TRIGGER tbl_ad AFTER DELETE ON tbl BEGIN
115 INSERT INTO fts_idx(fts_idx, rowid, b, c)
116 VALUES('delete', old.a, old.b, old.c);
118 CREATE TRIGGER tbl_au AFTER UPDATE ON tbl BEGIN
119 INSERT INTO fts_idx(fts_idx, rowid, b, c)
120 VALUES('delete', old.a, old.b, old.c);
121 INSERT INTO fts_idx(rowid, b, c) VALUES (new.a, new.b, new.c);
125 do_execsql_test 2.2 {
126 INSERT INTO tbl VALUES(1, 'one', 'o n e');
127 INSERT INTO tbl VALUES(NULL, 'two', 't w o');
128 INSERT INTO tbl VALUES(3, 'three', 't h r e e');
131 do_execsql_test 2.3 {
132 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
135 do_execsql_test 2.4 {
136 DELETE FROM tbl WHERE rowid=2;
137 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
140 do_execsql_test 2.5 {
141 UPDATE tbl SET c = c || ' x y z';
142 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check');
145 do_execsql_test 2.6 {
146 SELECT * FROM fts_idx WHERE fts_idx MATCH 't AND x';
147 } {three {t h r e e x y z}}
149 do_execsql_test 2.7 {
150 SELECT highlight(fts_idx, 1, '[', ']') FROM fts_idx
151 WHERE fts_idx MATCH 't AND x';
152 } {{[t] h r e e [x] y z}}
154 #-------------------------------------------------------------------------
155 # Quick tests of the 'delete-all' command.
157 do_execsql_test 3.1 {
158 CREATE VIRTUAL TABLE t3 USING fts5(x, content='');
159 INSERT INTO t3 VALUES('a b c');
160 INSERT INTO t3 VALUES('d e f');
163 do_execsql_test 3.2 {
164 SELECT count(*) FROM t3_docsize;
165 SELECT count(*) FROM t3_data;
168 do_execsql_test 3.3 {
169 INSERT INTO t3(t3) VALUES('delete-all');
170 SELECT count(*) FROM t3_docsize;
171 SELECT count(*) FROM t3_data;
174 do_execsql_test 3.4 {
175 INSERT INTO t3 VALUES('a b c');
176 INSERT INTO t3 VALUES('d e f');
177 SELECT rowid FROM t3 WHERE t3 MATCH 'e';
180 do_execsql_test 3.5 {
181 SELECT rowid FROM t3 WHERE t3 MATCH 'c';
184 do_execsql_test 3.6 {
185 SELECT count(*) FROM t3_docsize;
186 SELECT count(*) FROM t3_data;
189 do_execsql_test 3.7 {
190 CREATE VIRTUAL TABLE t4 USING fts5(x);
192 do_catchsql_test 3.8 {
193 INSERT INTO t4(t4) VALUES('delete-all');
194 } {1 {'delete-all' may only be used with a contentless or external content fts5 table}}
196 #-------------------------------------------------------------------------
197 # Test an external content table with a more interesting schema.
199 do_execsql_test 4.1 {
200 CREATE TABLE x2(a, "key col" PRIMARY KEY, b, c) WITHOUT ROWID;
201 INSERT INTO x2 VALUES('a b', 1, 'c d' , 'e f');
202 INSERT INTO x2 VALUES('x y', -40, 'z z' , 'y x');
204 CREATE VIRTUAL TABLE t2 USING fts5(a, c, content=x2, content_rowid='key col');
205 INSERT INTO t2(t2) VALUES('rebuild');
208 do_execsql_test 4.2 { SELECT rowid FROM t2 } {-40 1}
209 do_execsql_test 4.3 { SELECT rowid FROM t2 WHERE t2 MATCH 'c'} {}
210 do_execsql_test 4.4 { SELECT rowid FROM t2 WHERE t2 MATCH 'a'} {1}
211 do_execsql_test 4.5 { SELECT rowid FROM t2 WHERE t2 MATCH 'x'} {-40}
213 do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {}
215 do_execsql_test 4.7 {
216 DELETE FROM x2 WHERE "key col" = 1;
217 INSERT INTO t2(t2, rowid, a, c) VALUES('delete', 1, 'a b', 'e f');
218 INSERT INTO t2(t2) VALUES('integrity-check');
221 do_execsql_test 4.8 { SELECT rowid FROM t2 WHERE t2 MATCH 'b'} {}
222 do_execsql_test 4.9 { SELECT rowid FROM t2 WHERE t2 MATCH 'y'} {-40}
224 #-------------------------------------------------------------------------
225 # Test that if the 'rowid' field of a 'delete' is not an integer, no
226 # changes are made to the FTS index.
228 do_execsql_test 5.0 {
229 CREATE VIRTUAL TABLE t5 USING fts5(a, b, content=);
230 INSERT INTO t5(rowid, a, b) VALUES(-1, 'one', 'two');
231 INSERT INTO t5(rowid, a, b) VALUES( 0, 'three', 'four');
232 INSERT INTO t5(rowid, a, b) VALUES( 1, 'five', 'six');
235 set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}]
237 do_execsql_test 5.1 {
238 INSERT INTO t5(t5, rowid, a, b) VALUES('delete', NULL, 'three', 'four');
239 SELECT md5sum(id, block) FROM t5_data;
243 #-------------------------------------------------------------------------
244 # Check that a contentless table can be dropped.
247 do_execsql_test 6.1 {
248 CREATE VIRTUAL TABLE xx USING fts5(x, y, content="");
249 SELECT name FROM sqlite_master;
250 } {xx xx_data xx_idx xx_docsize xx_config}
251 do_execsql_test 6.2 {
253 SELECT name FROM sqlite_master;
256 #---------------------------------------------------------------------------
257 # Check that an fts5 table cannot be its own content table.
260 do_execsql_test 7.1.1 {
261 CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 );
262 INSERT INTO t1( a ) VALUES('abc');
264 do_catchsql_test 7.1.2 {
266 } {1 {recursively defined fts5 content table}}
267 do_catchsql_test 7.1.3 {
268 SELECT * FROM t1('abc');
269 } {1 {recursively defined fts5 content table}}
270 do_catchsql_test 7.1.4 {
271 SELECT count(*) FROM t1;
272 } {1 {recursively defined fts5 content table}}
273 do_catchsql_test 7.1.5 {
274 SELECT * FROM t1('abc') ORDER BY rank;
275 } {1 {recursively defined fts5 content table}}
278 do_execsql_test 7.2.1 {
279 CREATE VIRTUAL TABLE t1 USING fts5(a, c=t2 );
280 CREATE VIRTUAL TABLE t2 USING fts5(a, c=t1 );
281 INSERT INTO t1( a ) VALUES('abc');
283 do_catchsql_test 7.2.2 {
285 } {1 {recursively defined fts5 content table}}
286 do_catchsql_test 7.2.3 {
287 SELECT * FROM t1('abc');
288 } {1 {recursively defined fts5 content table}}
289 do_catchsql_test 7.2.4 {
290 SELECT count(*) FROM t1;
291 } {1 {recursively defined fts5 content table}}
292 do_catchsql_test 7.2.5 {
293 SELECT * FROM t1('abc') ORDER BY rank;
294 } {1 {recursively defined fts5 content table}}
296 #---------------------------------------------------------------------------
297 # Check that if the content table is a view, and that view contains an
298 # error, a reasonable error message is returned if the user tries to
299 # read from the view via the fts5 table.
302 do_execsql_test 8.1 {
304 SELECT 1 AS r, text_value(1) AS t
306 SELECT 2 AS r, text_value(2) AS t;
308 CREATE VIRTUAL TABLE t1 USING fts5(t, content='a1', content_rowid='r');
313 2 "INSERT INTO t1(t1) VALUES('rebuild')"
314 3 "SELECT * FROM t1 WHERE rowid=1"
316 do_catchsql_test 8.2.$tn $sql {1 {no such function: text_value}}
319 proc text_value {i} {
320 if {$i==1} { return "one" }
321 if {$i==2} { return "two" }
324 db func text_value text_value
326 do_execsql_test 8.3.1 { SELECT * FROM t1 } {one two}
327 do_execsql_test 8.3.2 { INSERT INTO t1(t1) VALUES('rebuild') }
328 do_execsql_test 8.3.3 { SELECT * FROM t1 WHERE rowid=1 } {one}
329 do_execsql_test 8.3.4 { SELECT rowid FROM t1('two') } {2}