Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/fts5/test/fts5content.test # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / fts5 / test / fts5content.test` 434 lines ยท 13168 bytes Raw Blame History 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 15 source [file join [file dirname [info script]] fts5_common.tcl] 16 set testprefix fts5content 17 18 # If SQLITE_ENABLE_FTS5 is not defined, omit this file. 19 ifcapable !fts5 { 20 finish_test 21 return 22 } 23 24 #------------------------------------------------------------------------- 25 # Contentless tables 26 # 27 do_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 34 do_execsql_test 1.2 { 35 SELECT rowid FROM f1 WHERE f1 MATCH 'o'; 36 } {1 2} 37 38 do_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 43 do_execsql_test 1.4 { 44 SELECT rowid, a, b FROM f1 WHERE f1 MATCH 'o'; 45 } {1 {} {} 2 {} {} 4 {} {}} 46 47 do_execsql_test 1.5 { 48 SELECT rowid, highlight(f1, 0, '[', ']') FROM f1 WHERE f1 MATCH 'o'; 49 } {1 {} 2 {} 4 {}} 50 51 do_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 55 do_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 60 do_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 65 do_execsql_test 1.9 { 66 SELECT rowid FROM f1; 67 } {1 2 3 4} 68 69 do_execsql_test 1.10 { 70 SELECT * FROM f1; 71 } {{} {} {} {} {} {} {} {}} 72 73 do_execsql_test 1.11 { 74 SELECT rowid, a, b FROM f1 ORDER BY rowid ASC; 75 } {1 {} {} 2 {} {} 3 {} {} 4 {} {}} 76 77 do_execsql_test 1.12 { 78 SELECT a IS NULL FROM f1; 79 } {1 1 1 1} 80 81 do_catchsql_test 1.13 { 82 DELETE FROM f1 WHERE rowid = 2; 83 } {1 {cannot DELETE from contentless fts5 table: f1}} 84 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}} 88 89 do_execsql_test 1.15 { 90 INSERT INTO f1(f1, rowid, a, b) VALUES('delete', 2, 'two', 't w o'); 91 } {} 92 93 do_execsql_test 1.16 { 94 SELECT rowid FROM f1 WHERE f1 MATCH 'o'; 95 } {1 4} 96 97 do_execsql_test 1.17 { 98 SELECT rowid FROM f1; 99 } {1 3 4} 100 101 #------------------------------------------------------------------------- 102 # External content tables 103 # 104 reset_db 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'); 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 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'); 129 } 130 131 do_execsql_test 2.3 { 132 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); 133 } 134 135 do_execsql_test 2.4 { 136 DELETE FROM tbl WHERE rowid=2; 137 INSERT INTO fts_idx(fts_idx) VALUES('integrity-check'); 138 } 139 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'); 143 } 144 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}} 148 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}} 153 154 #------------------------------------------------------------------------- 155 # Quick tests of the 'delete-all' command. 156 # 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'); 161 } 162 163 do_execsql_test 3.2 { 164 SELECT count(*) FROM t3_docsize; 165 SELECT count(*) FROM t3_data; 166 } {2 4} 167 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; 172 } {0 2} 173 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'; 178 } {2} 179 180 do_execsql_test 3.5 { 181 SELECT rowid FROM t3 WHERE t3 MATCH 'c'; 182 } {1} 183 184 do_execsql_test 3.6 { 185 SELECT count(*) FROM t3_docsize; 186 SELECT count(*) FROM t3_data; 187 } {2 4} 188 189 do_execsql_test 3.7 { 190 CREATE VIRTUAL TABLE t4 USING fts5(x); 191 } {} 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}} 195 196 #------------------------------------------------------------------------- 197 # Test an external content table with a more interesting schema. 198 # 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'); 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 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} 212 213 do_execsql_test 4.6 { INSERT INTO t2(t2) VALUES('integrity-check') } {} 214 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'); 219 } 220 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} 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 # 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'); 233 } 234 235 set ::checksum [execsql {SELECT md5sum(id, block) FROM t5_data}] 236 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; 240 } $::checksum 241 242 243 #------------------------------------------------------------------------- 244 # Check that a contentless table can be dropped. 245 # 246 reset_db 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 { 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 # 259 reset_db 260 do_execsql_test 7.1.1 { 261 CREATE VIRTUAL TABLE t1 USING fts5(a, c=t1 ); 262 INSERT INTO t1( a ) VALUES('abc'); 263 } 264 do_catchsql_test 7.1.2 { 265 SELECT * FROM t1; 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}} 276 277 reset_db 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'); 282 } 283 do_catchsql_test 7.2.2 { 284 SELECT * FROM t1; 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}} 295 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. 300 # 301 reset_db 302 do_execsql_test 8.1 { 303 CREATE VIEW a1 AS 304 SELECT 1 AS r, text_value(1) AS t 305 UNION ALL 306 SELECT 2 AS r, text_value(2) AS t; 307 308 CREATE VIRTUAL TABLE t1 USING fts5(t, content='a1', content_rowid='r'); 309 } 310 311 foreach {tn sql} { 312 1 "SELECT * FROM t1" 313 2 "INSERT INTO t1(t1) VALUES('rebuild')" 314 3 "SELECT * FROM t1 WHERE rowid=1" 315 } { 316 do_catchsql_test 8.2.$tn $sql {1 {no such function: text_value}} 317 } 318 319 proc text_value {i} { 320 if {$i==1} { return "one" } 321 if {$i==2} { return "two" } 322 return "many" 323 } 324 db func text_value text_value 325 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} 330 331 #------------------------------------------------------------------------- 332 reset_db 333 do_execsql_test 9.1 { 334 CREATE TABLE t1(a INTEGER PRIMARY KEY, b); 335 INSERT INTO t1 VALUES(1, 'one two three'); 336 INSERT INTO t1 VALUES(2, 'one two three'); 337 338 CREATE VIRTUAL TABLE ft USING fts5(b, content=t1, content_rowid=a); 339 INSERT INTO ft(ft) VALUES('rebuild'); 340 } 341 342 do_execsql_test 9.2 { 343 SELECT rowid, b FROM ft('two'); 344 } { 345 1 {one two three} 346 2 {one two three} 347 } 348 349 do_execsql_test 9.3 { 350 DELETE FROM t1 WHERE a=2; 351 } 352 353 do_catchsql_test 9.4 { 354 SELECT rowid FROM ft('two'); 355 } {0 {1 2}} 356 357 do_catchsql_test 9.5 { 358 SELECT * FROM ft('two'); 359 } {1 {fts5: missing row 2 from content table 'main'.'t1'}} 360 361 fts5_aux_test_functions db 362 363 do_catchsql_test 9.6 { 364 SELECT rowid, fts5_columntext(ft, 0) FROM ft('two'); 365 } {1 SQLITE_CORRUPT_VTAB} 366 367 #------------------------------------------------------------------------- 368 # Check that an external content table named by the schema is subject to 369 # the same restrictions as a virtual table named by a view in the schema. 370 # 371 # With trusted_schema=off, the content table may not be a virtual table 372 # that is not SQLITE_VTAB_INNOCUOUS. This applies to all statements that 373 # read from the content table: scans in either direction and rowid lookups. 374 # 375 reset_db 376 set unsafe {1 {unsafe use of virtual table "pragma_table_list"}} 377 do_execsql_test 10.0 { 378 PRAGMA trusted_schema = on; 379 CREATE VIEW v1 AS SELECT rowid, name FROM pragma_table_list; 380 CREATE VIRTUAL TABLE ft USING fts5( 381 name, content='pragma_table_list', content_rowid='rowid' 382 ); 383 SELECT count(*)>0 FROM v1; 384 SELECT count(*)>0 FROM ft; 385 } {1 1} 386 387 do_execsql_test 10.1 { PRAGMA trusted_schema = off; } 388 do_catchsql_test 10.2 { SELECT * FROM v1 } $unsafe 389 do_catchsql_test 10.3 { SELECT * FROM ft } $unsafe 390 do_catchsql_test 10.4 { SELECT * FROM ft ORDER BY rowid DESC } $unsafe 391 do_catchsql_test 10.5 { SELECT * FROM ft WHERE rowid=1 } $unsafe 392 393 # A table declared SQLITE_VTAB_DIRECTONLY may not be used as the content 394 # table regardless of the trusted_schema setting. 395 # 396 reset_db 397 if {[db one {SELECT count(*) FROM pragma_module_list WHERE name='sqlite_dbpage'}]} { 398 set unsafe {1 {unsafe use of virtual table "sqlite_dbpage"}} 399 do_execsql_test 10.6 { 400 CREATE TABLE secret(x); 401 INSERT INTO secret VALUES('private data'); 402 CREATE VIEW v2 AS SELECT pgno, data FROM sqlite_dbpage; 403 CREATE VIRTUAL TABLE ft2 USING fts5( 404 data, content='sqlite_dbpage', content_rowid='pgno' 405 ); 406 } 407 408 foreach {tn ts} {1 on 2 off} { 409 do_execsql_test 10.7.$tn.0 "PRAGMA trusted_schema = $ts" 410 do_catchsql_test 10.7.$tn.1 { SELECT * FROM v2 } $unsafe 411 do_catchsql_test 10.7.$tn.2 { SELECT * FROM ft2 } $unsafe 412 do_catchsql_test 10.7.$tn.3 { SELECT * FROM ft2 ORDER BY rowid DESC } $unsafe 413 do_catchsql_test 10.7.$tn.4 { SELECT * FROM ft2 WHERE rowid=1 } $unsafe 414 } 415 } 416 417 # An ordinary external content table continues to work with 418 # trusted_schema=off. 419 # 420 reset_db 421 do_execsql_test 10.8.0 { 422 PRAGMA trusted_schema = off; 423 CREATE TABLE src(a INTEGER PRIMARY KEY, b); 424 INSERT INTO src VALUES(1, 'one two'), (2, 'two three'); 425 CREATE VIRTUAL TABLE ft3 USING fts5(b, content='src', content_rowid='a'); 426 INSERT INTO ft3(ft3) VALUES('rebuild'); 427 } 428 do_execsql_test 10.8.1 { SELECT rowid, b FROM ft3('three') } {2 {two three}} 429 do_execsql_test 10.8.2 { SELECT b FROM ft3 WHERE rowid=1 } {{one two}} 430 do_execsql_test 10.8.3 { SELECT rowid FROM ft3('two') ORDER BY rank } {1 2} 431 do_execsql_test 10.8.4 { INSERT INTO ft3(ft3) VALUES('integrity-check') } 432 433 finish_test 434