1# 2014 Dec 20 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# This file contains tests for the content= and content_rowid= options. 13# 14 15source [file join [file dirname [info script]] fts5_common.tcl] 16set testprefix fts5content 17 18# If SQLITE_ENABLE_FTS5 is defined, omit this file. 19ifcapable !fts5 { 20 finish_test 21 return 22} 23 24#------------------------------------------------------------------------- 25# Contentless tables 26# 27do_execsql_test 1.1 { 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'); 32} 33 34do_execsql_test 1.2 { 35 SELECT rowid FROM f1 WHERE f1 MATCH 'o'; 36} {1 2} 37 38do_execsql_test 1.3 { 39 INSERT INTO f1(a, b) VALUES('four', 'f o u r'); 40 SELECT rowid FROM f1 WHERE f1 MATCH 'o'; 41} {1 2 4} 42 43do_execsql_test 1.4 { 44 SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o'; 45} {1 {} {} 2 {} {} 4 {} {}} 46 47do_execsql_test 1.5 { 48 SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o'; 49} {1 {} 2 {} 4 {}} 50 51do_execsql_test 1.6 { 52 SELECT rowid, highlight(f1, 0, '[', ']') IS NULL FROM f1 WHERE f1 MATCH 'o'; 53} {1 1 2 1 4 1} 54 55do_execsql_test 1.7 { 56 SELECT rowid, snippet(f1, -1, '[', ']', '...', 5) IS NULL 57 FROM f1 WHERE f1 MATCH 'o'; 58} {1 1 2 1 4 1} 59 60do_execsql_test 1.8 { 61 SELECT rowid, snippet(f1, 1, '[', ']', '...', 5) IS NULL 62 FROM f1 WHERE f1 MATCH 'o'; 63} {1 1 2 1 4 1} 64 65do_execsql_test 1.9 { 66 SELECT rowid FROM f1; 67} {1 2 3 4} 68 69do_execsql_test 1.10 { 70 SELECT * FROM f1; 71} {{} {} {} {} {} {} {} {}} 72 73do_execsql_test 1.11 { 74 SELECT rowid, a, b FROM f1 ORDER BY rowid ASC; 75} {1 {} {} 2 {} {} 3 {} {} 4 {} {}} 76 77do_execsql_test 1.12 { 78 SELECT a IS NULL FROM f1; 79} {1 1 1 1} 80 81do_catchsql_test 1.13 { 82 DELETE FROM f1 WHERE rowid = 2; 83} {1 {cannot DELETE from contentless fts5 table: f1}} 84 85do_catchsql_test 1.14 { 86 UPDATE f1 SET a = 'a b c' WHERE rowid = 2; 87} {1 {cannot UPDATE contentless fts5 table: f1}} 88 89do_execsql_test 1.15 { 90 INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o'); 91} {} 92 93do_execsql_test 1.16 { 94 SELECT rowid FROM f1 WHERE f1 MATCH 'o'; 95} {1 4} 96 97do_execsql_test 1.17 { 98 SELECT rowid FROM f1; 99} {1 3 4} 100 101#------------------------------------------------------------------------- 102# External content tables 103# 104reset_db 105do_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'); 109 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); 113 END; 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); 117 END; 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); 122 END; 123} 124 125do_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'); 129} 130 131do_execsql_test 2.3 { 132 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); 133} 134 135do_execsql_test 2.4 { 136 DELETE FROM tbl WHERE rowid=2; 137 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); 138} 139 140do_execsql_test 2.5 { 141 UPDATE tbl SET c = c || ' x y z'; 142 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); 143} 144 145do_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}} 148 149do_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}} 153 154#------------------------------------------------------------------------- 155# Quick tests of the 'delete-all' command. 156# 157do_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'); 161} 162 163do_execsql_test 3.2 { 164 SELECT count(*) FROM t3_docsize; 165 SELECT count(*) FROM t3_data; 166} {2 4} 167 168do_execsql_test 3.3 { 169 INSERT INTO t3(t3) VALUES('delete-all'); 170 SELECT count(*) FROM t3_docsize; 171 SELECT count(*) FROM t3_data; 172} {0 2} 173 174do_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'; 178} {2} 179 180do_execsql_test 3.5 { 181 SELECT rowid FROM t3 WHERE t3 MATCH 'c'; 182} {1} 183 184do_execsql_test 3.6 { 185 SELECT count(*) FROM t3_docsize; 186 SELECT count(*) FROM t3_data; 187} {2 4} 188 189do_execsql_test 3.7 { 190 CREATE VIRTUAL TABLE t4 USING fts5(x); 191} {} 192do_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}} 195 196#------------------------------------------------------------------------- 197# Test an external content table with a more interesting schema. 198# 199do_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'); 203 204 CREATE VIRTUAL TABLE t2 USING fts5(a, c, content=x2, content_rowid='key col'); 205 INSERT INTO t2(t2) VALUES('rebuild'); 206} 207 208do_execsql_test 4.2 { SELECT rowid FROM t2 } {-40 1} 209do_execsql_test 4.3 { SELECT rowid FROM t2 WHERE t2 MATCH 'c'} {} 210do_execsql_test 4.4 { SELECT rowid FROM t2 WHERE t2 MATCH 'a'} {1} 211do_execsql_test 4.5 { SELECT rowid FROM t2 WHERE t2 MATCH 'x'} {-40} 212 213do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {} 214 215do_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'); 219} 220 221do_execsql_test 4.8 { SELECT rowid FROM t2 WHERE t2 MATCH 'b'} {} 222do_execsql_test 4.9 { SELECT rowid FROM t2 WHERE t2 MATCH 'y'} {-40} 223 224#------------------------------------------------------------------------- 225# Test that if the 'rowid' field of a 'delete' is not an integer, no 226# changes are made to the FTS index. 227# 228do_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'); 233} 234 235set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}] 236 237do_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; 240} $::checksum 241 242 243#------------------------------------------------------------------------- 244# Check that a contentless table can be dropped. 245# 246reset_db 247do_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} 251do_execsql_test 6.2 { 252 DROP TABLE xx; 253 SELECT name FROM sqlite_master; 254} {} 255 256#--------------------------------------------------------------------------- 257# Check that an fts5 table cannot be its own content table. 258# 259reset_db 260do_execsql_test 7.1.1 { 261 CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 ); 262 INSERT INTO t1( a ) VALUES('abc'); 263} 264do_catchsql_test 7.1.2 { 265 SELECT * FROM t1; 266} {1 {recursively defined fts5 content table}} 267do_catchsql_test 7.1.3 { 268 SELECT * FROM t1('abc'); 269} {1 {recursively defined fts5 content table}} 270do_catchsql_test 7.1.4 { 271 SELECT count(*) FROM t1; 272} {1 {recursively defined fts5 content table}} 273do_catchsql_test 7.1.5 { 274 SELECT * FROM t1('abc') ORDER BY rank; 275} {1 {recursively defined fts5 content table}} 276 277reset_db 278do_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'); 282} 283do_catchsql_test 7.2.2 { 284 SELECT * FROM t1; 285} {1 {recursively defined fts5 content table}} 286do_catchsql_test 7.2.3 { 287 SELECT * FROM t1('abc'); 288} {1 {recursively defined fts5 content table}} 289do_catchsql_test 7.2.4 { 290 SELECT count(*) FROM t1; 291} {1 {recursively defined fts5 content table}} 292do_catchsql_test 7.2.5 { 293 SELECT * FROM t1('abc') ORDER BY rank; 294} {1 {recursively defined fts5 content table}} 295 296finish_test 297 298