1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
|
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
|
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
-
-
-
+
+
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
+
+
+
+
+
+
-
-
+
+
-
-
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
+
-
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
-
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
-
-
-
+
-
-
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
-
+
-
-
-
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
+
-
+
|
}
if( v->pTokenizer!=NULL ){
v->pTokenizer->pModule->xDestroy(v->pTokenizer);
v->pTokenizer = NULL;
}
free((void *) v->zColumnNames);
free((void *) v->zName);
free(v->azColumn);
free(v->zColumnList);
free(v);
}
/*
** Token types for parsing the arguments to xConnect or xCreate.
*/
#define TOKEN_EOF 0 /* End of file */
#define TOKEN_SPACE 1 /* Any kind of whitespace */
#define TOKEN_ID 2 /* An identifier */
#define TOKEN_STRING 3 /* A string literal */
#define TOKEN_PUNCT 4 /* A single punctuation character */
/* Return true if the string s begins with the string t, ignoring case. */
static int startsWithIgnoreCase(const char *s, const char *t){
while( *t )
if( tolower(*s++)!=tolower(*t++) ) return 0;
/*
** If X is a character that can be used in an identifier then
** IdChar(X) will be true. Otherwise it is false.
**
** For ASCII, any character with the high-order bit set is
** allowed in an identifier. For 7-bit characters,
** sqlite3IsIdChar[X] must be 1.
**
** Ticket #1066. the SQL standard does not allow '$' in the
** middle of identfiers. But many SQL implementations do.
** SQLite will allow '$' in identifiers for compatibility.
** But the feature is undocumented.
*/
static const char isIdChar[] = {
/* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 2x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 3x */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 4x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 5x */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 6x */
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 7x */
};
#define IdChar(C) (((c=C)&0x80)!=0 || (c>0x1f && isIdChar[c-0x20]))
/*
** Return the length of the token that begins at z[0].
** Store the token type in *tokenType before returning.
*/
static int getToken(const char *z, int *tokenType){
int i, c;
switch( *z ){
case 0: {
*tokenType = TOKEN_EOF;
return 0;
}
case ' ': case '\t': case '\n': case '\f': case '\r': {
for(i=1; isspace(z[i]); i++){}
*tokenType = TOKEN_SPACE;
return i;
}
case '\'':
case '"': {
int delim = z[0];
for(i=1; (c=z[i])!=0; i++){
if( c==delim ){
if( z[i+1]==delim ){
i++;
}else{
break;
}
}
}
*tokenType = TOKEN_STRING;
return i + (c!=0);
}
case '[': {
for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
*tokenType = TOKEN_ID;
return i;
}
default: {
if( !IdChar(*z) ){
break;
}
for(i=1; IdChar(z[i]); i++){}
*tokenType = TOKEN_ID;
return i;
}
}
*tokenType = TOKEN_PUNCT;
return 1;
}
/*
** A token extracted from a string is an instance of the following
** structure.
*/
typedef struct Token {
const char *z; /* Pointer to token text. Not '\000' terminated */
short int n; /* Length of the token text in bytes. */
} Token;
const char *kTokenize = "tokenize";
static int isTokenize(const char *arg){
return startsWithIgnoreCase(arg, kTokenize);
/*
** Given a input string (which is really one of the argv[] parameters
** passed into xConnect or xCreate) split the string up into tokens.
** Return an array of pointers to '\000' terminated strings, one string
** for each non-whitespace token.
**
** The returned array is terminated by a single NULL pointer.
**
** Space to hold the returned array is obtained from a single
** malloc and should be freed by passing the return value to free().
** The individual strings within the token list are all a part of
** the single memory allocation and will all be freed at once.
*/
static char **tokenizeString(const char *z, int *pnToken){
int nToken = 0;
Token *aToken = malloc( strlen(z) * sizeof(aToken[0]) );
int n = 1;
int e, i;
int totalSize = 0;
char **azToken;
char *zCopy;
while( n>0 ){
n = getToken(z, &e);
if( e!=TOKEN_SPACE ){
aToken[nToken].z = z;
aToken[nToken].n = n;
nToken++;
totalSize += n+1;
}
z += n;
}
azToken = (char**)malloc( nToken*sizeof(char*) + totalSize );
zCopy = (char*)&azToken[nToken];
nToken--;
for(i=0; i<nToken; i++){
azToken[i] = zCopy;
n = aToken[i].n;
memcpy(zCopy, aToken[i].z, n);
zCopy[n] = 0;
zCopy += n+1;
}
azToken[nToken] = 0;
free(aToken);
*pnToken = nToken;
return azToken;
}
/*
** Convert an SQL-style quoted string into a normal string by removing
** the quote characters. The conversion is done in-place. If the
** input does not begin with a quote character, then this routine
** is a no-op.
**
** Examples:
**
** "abc" becomes abc
** 'xyz' becomes xyz
** [pqr] becomes pqr
** `mno` becomes mno
*/
void dequoteString(char *z){
int quote;
int i, j;
if( z==0 ) return;
quote = z[0];
switch( quote ){
case '\'': break;
case '"': break;
case '`': break; /* For MySQL compatibility */
case '[': quote = ']'; break; /* For MS SqlServer compatibility */
default: return;
}
for(i=1, j=0; z[i]; i++){
if( z[i]==quote ){
if( z[i+1]==quote ){
z[j++] = quote;
i++;
}else{
z[j++] = 0;
break;
}
}else{
z[j++] = z[i];
}
}
}
/*
** The input azIn is a NULL-terminated list of tokens. Remove the first
** token and all punctuation tokens. Remove the quotes from
** around string literal tokens.
**
** Example:
**
** input: tokenize chinese ( 'simplifed' , 'mixed' )
** output: chinese simplifed mixed
**
** Another example:
**
** input: delimiters ( '[' , ']' , '...' )
** output: [ ] ...
*/
void tokenListToIdList(char **azIn){
int i, j;
if( azIn ){
for(i=0, j=-1; azIn[i]; i++){
if( isalnum(azIn[i][0]) || azIn[i][1] ){
dequoteString(azIn[i]);
if( j>=0 ){
azIn[j] = azIn[i];
}
j++;
}
}
azIn[j] = 0;
}
}
/*
** Find the first alphanumeric token in the string zIn. Null-terminate
** this token. Remove any quotation marks. And return a pointer to
** the result.
*/
static char *firstToken(char *zIn, char **pzTail){
int i, n, ttype;
i = 0;
while(1){
n = getToken(zIn, &ttype);
if( ttype==TOKEN_SPACE ){
zIn += n;
}else if( ttype==TOKEN_EOF ){
*pzTail = zIn;
return 0;
}else{
zIn[n] = 0;
*pzTail = &zIn[1];
dequoteString(zIn);
return zIn;
}
}
/*NOTREACHED*/
}
/* Return true if...
**
** * s begins with the string t, ignoring case
** * s is longer than t
** * The first character of s beyond t is not a alphanumeric
**
** Ignore leading space in *s.
**
** To put it another way, return true if the first token of
** s[] is t[].
*/
static int startsWith(const char *s, const char *t){
while( isspace(*s) ){ s++; }
while( *t ){
if( tolower(*s++)!=tolower(*t++) ) return 0;
}
return *s!='_' && !isalnum(*s);
}
static const char *tokenizerSpec(const char *arg){
return arg + strlen(kTokenize);
/*
** An instance of this structure defines the "spec" of a the
}
** full text index. This structure is populated by parseSpec
** and use by fulltextConnect and fulltextCreate.
*/
typedef struct TableSpec {
const char *zName;
int nColumns;
const char * const *zColumnNames;
char *zTokenizer;
char *zTokenizerArg;
const char *zName; /* Name of the full-text index */
int nColumn; /* Number of columns to be indexed */
char **azColumn; /* Original names of columns to be indexed */
char *zColumnList; /* Comma-separated list of names for %_content */
char **azTokenizer; /* Name of tokenizer and its arguments */
char **azDelimiter; /* Delimiters used for snippets */
} TableSpec;
/*
** Reclaim all of the memory used by a TableSpec
*/
void destroyTableSpec(TableSpec *p) {
free(p->zTokenizer);
free(p->zTokenizerArg);
void clearTableSpec(TableSpec *p) {
free(p->azColumn);
free(p->zColumnList);
free(p->azTokenizer);
free(p->azDelimiter);
}
/* Parse a CREATE VIRTUAL TABLE statement, which looks like this:
*
* CREATE VIRTUAL TABLE email
* USING fts1(subject, body, tokenize mytokenizer(myarg))
*
* We return parsed information in a TableSpec structure.
*
*/
int parseSpec(TableSpec *pSpec, int argc, const char * const *argv){
int i;
int parseSpec(TableSpec *pSpec, int argc, const char *const*argv, char**pzErr){
int i, j, n;
char *z, *zDummy;
char **azArg;
const char *zTokenizer = 0; /* argv[] entry describing the tokenizer */
const char *zDelimiter = 0; /* argv[] entry describing the delimiters */
assert( argc>=3 );
/* Current interface:
** argv[0] - module name
** argv[1] - database name
** argv[2] - table name
** argv[3..] - columns, optionally followed by tokenizer specification
** and snippet delimiters specification.
*/
pSpec->zName = argv[2];
for (i=3; i<argc && !isTokenize(argv[i]); ++i)
;
pSpec->nColumns = i-3;
if( pSpec->nColumns<1) return SQLITE_ERROR;
pSpec->zColumnNames = &argv[3];
/* Make a copy of the complete argv[][] array in a single allocation.
** The argv[][] array is read-only and transient. We can write to the
** copy in order to modify things and the copy is persistent.
*/
memset(pSpec, 0, sizeof(pSpec));
for(i=n=0; i<argc; i++){
n += strlen(argv[i]) + 1;
}
azArg = malloc( sizeof(char*)*argc + n );
if( azArg==0 ){
return SQLITE_NOMEM;
}
z = (char*)&azArg[argc];
pSpec->zTokenizer = pSpec->zTokenizerArg = NULL;
if( i<argc ){ /* we have a tokenizer */
for(i=0; i<argc; i++){
const char *start, *end;
assert( isTokenize(argv[i]) );
start = tokenizerSpec(argv[i]);
azArg[i] = z;
strcpy(z, argv[i]);
while( isspace(*start) ){
++start;
}
z += strlen(z)+1;
}
end = start;
while( isalnum(*end) ){
++end;
}
pSpec->zTokenizer = string_dup_n(start, end-start);
start = end;
/* Identify the column names and the tokenizer and delimiter arguments
** in the argv[][] array.
*/
pSpec->zName = azArg[2];
pSpec->nColumn = 0;
pSpec->azColumn = azArg;
zTokenizer = "tokenize simple";
zDelimiter = "delimiters('[',']','...')";
n = 0;
for(i=3, j=0; i<argc; ++i){
if( startsWith(azArg[i],"tokenize") ){
zTokenizer = azArg[i];
while( isspace(*start) ){
++start;
}
if( *start=='(' ){ /* tokenizer has an argument */
++start;
end = strchr(start, ')');
}else if( startsWith(azArg[i],"delimiters") ){
zDelimiter = azArg[i];
}else{
z = azArg[pSpec->nColumn] = firstToken(azArg[i], &zDummy);
pSpec->nColumn++;
n += strlen(z) + 6;
if( !end ) return SQLITE_ERROR;
pSpec->zTokenizerArg = string_dup_n(start, end-start);
}
}
if( pSpec->nColumn==0 ){
azArg[0] = "content";
return SQLITE_OK;
}
pSpec->nColumn = 1;
}
/* Concatenate an array of strings into a single string, separating with commas.
* The caller must free the returned string. */
static char *commaConcatenate(int nColumns, const char * const *zColumns){
/*
** Construct the comma-separated list of column names.
**
** Each column name will be of the form cNNAAAA
** where NN is the column number and AAAA is the sanitized
** column name. "sanitized" means that special characters are
** converted to "_". The cNN prefix guarantees that all column
** names are unique.
**
** The AAAA suffix is not strictly necessary. It is included
** for the convenience of people who might examine the generated
** %_content table and wonder what the columns are used for.
*/
z = pSpec->zColumnList = malloc( n );
StringBuffer buf;
int i;
if( z==0 ){
clearTableSpec(pSpec);
return SQLITE_NOMEM;
}
initStringBuffer(&buf);
for(i=0; i<nColumns ; ++i){
if( i>0 ){
for(i=0; i<pSpec->nColumn; i++){
sqlite3_snprintf(n, z, "c%d%s", i, azArg[i]);
for(j=0; z[j]; j++){
if( !isalnum(z[j]) ) z[j] = '_';
append(&buf, ", ");
}
z[j] = ',';
z += j+1;
}
z[-1] = 0;
append(&buf, zColumns[i]);
}
/*
** Parse the tokenizer specification string.
*/
pSpec->azTokenizer = tokenizeString(zTokenizer, &n);
tokenListToIdList(pSpec->azTokenizer);
/*
** Parse the delimiter specification string.
*/
pSpec->azDelimiter = tokenizeString(zDelimiter, &n);
tokenListToIdList(pSpec->azDelimiter);
return buf.s;
return SQLITE_OK;
}
static char *fulltextSchema(char *name, int nColumns,
const char * const *zColumns, int magic){
/*
** Generate a CREATE TABLE statement that describes the schema of
** the virtual table. Return a pointer to this schema.
**
** If the addAllColumn parameter is true, then add a column named
StringBuffer buf;
int i;
initStringBuffer(&buf);
** "_all" to the end of the schema.
**
** Space is obtained from sqlite3_mprintf() and should be freed
** using sqlite3_free().
*/
static char *fulltextSchema(
int nColumn, /* Number of columns */
const char *const* azColumn /* List of columns */
){
int i;
append(&buf, "create table ");
append(&buf, name);
append(&buf, "(");
for(i=0; i<nColumns; ++i){
char *zSchema, *zNext;
const char *zSep = "(";
zSchema = sqlite3_mprintf("CREATE TABLE x");
for(i=0; i<nColumn; i++){
if( i>0 ){
append(&buf, ", ");
}
append(&buf, zColumns[i]);
append(&buf, " text");
zNext = sqlite3_mprintf("%s%s%Q", zSchema, zSep, azColumn[i]);
sqlite3_free(zSchema);
zSchema = zNext;
}
if( magic ){
append(&buf, ", _all text");
zSep = ",";
}
append(&buf, ")");
return buf.s;
zNext = sqlite3_mprintf("%s,_all)", zSchema);
sqlite3_free(zSchema);
return zNext;
}
/*
** Build a new sqlite3_vtab structure that will describe the
** fulltext index defined by spec.
*/
static int connect(sqlite3 *db, TableSpec *spec,
sqlite3_vtab **ppVTab, char **pzErr){
static int constructVtab(
sqlite3 *db, /* The SQLite database connection */
TableSpec *spec, /* Parsed spec information from parseSpec() */
sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
char **pzErr /* Write any error message here */
){
int rc;
int n;
fulltext_vtab *v = 0;
const sqlite3_tokenizer_module *m = NULL;
char *schema;
v = (fulltext_vtab *) malloc(sizeof(fulltext_vtab));
if( v==0 ) return SQLITE_ERROR;
if( v==0 ) return SQLITE_NOMEM;
memset(v, 0, sizeof(*v));
/* sqlite will initialize v->base */
v->db = db;
v->zName = string_dup(spec->zName);
v->nColumns = spec->nColumns;
v->zColumnNames = commaConcatenate(spec->nColumns, spec->zColumnNames);
v->zName = spec->zName; /* Freed when azColumn is freed */
v->nColumn = spec->nColumn;
v->zColumnList = spec->zColumnList;
spec->zColumnList = 0;
v->azColumn = spec->azColumn;
spec->azColumn = 0;
if( spec->zTokenizer == NULL ){
if( spec->azTokenizer==0 ){
sqlite3Fts1SimpleTokenizerModule(&m);
} else {
/* TODO(shess) For now, add new tokenizers as else if clauses. */
if( !strcmp(spec->zTokenizer, "simple") ){
sqlite3Fts1SimpleTokenizerModule(&m);
} else {
*pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->zTokenizer);
rc = SQLITE_ERROR;
goto err;
}
}
return SQLITE_NOMEM;
}
/* TODO(shess) For now, add new tokenizers as else if clauses. */
if( spec->azTokenizer[0]==0 || !strcmp(spec->azTokenizer[0], "simple") ){
sqlite3Fts1SimpleTokenizerModule(&m);
} else {
*pzErr = sqlite3_mprintf("unknown tokenizer: %s", spec->azTokenizer[0]);
rc = SQLITE_ERROR;
goto err;
}
for(n=0; spec->azTokenizer[n]; n++){}
if( n ){
rc = m->xCreate(n-1, (const char*const*)&spec->azTokenizer[1],
&v->pTokenizer);
}else{
rc = m->xCreate(0, 0, &v->pTokenizer);
}
/* TODO: Support multiple arguments to tokenizers. */
rc = m->xCreate(1, &spec->zTokenizerArg, &v->pTokenizer);
if( rc!=SQLITE_OK ) goto err;
v->pTokenizer->pModule = m;
/* TODO: verify the existence of backing tables foo_content, foo_term */
schema = fulltextSchema("x", spec->nColumns, spec->zColumnNames, 1);
schema = fulltextSchema(v->nColumn, (const char*const*)v->azColumn);
rc = sqlite3_declare_vtab(db, schema);
free(schema);
sqlite3_free(schema);
if( rc!=SQLITE_OK ) goto err;
memset(v->pFulltextStatements, 0, sizeof(v->pFulltextStatements));
*ppVTab = &v->base;
TRACE(("FTS1 Connect %p\n", v));
|
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
|
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
|
-
+
-
-
+
-
-
-
+
-
-
-
+
+
-
+
-
-
+
-
+
|
sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
int idxNum, const char *idxStr, /* Which indexing scheme to use */
int argc, sqlite3_value **argv /* Arguments for the indexing scheme */
){
fulltext_cursor *c = (fulltext_cursor *) pCursor;
fulltext_vtab *v = cursor_vtab(c);
int rc;
StringBuffer sb;
char *zSql;
TRACE(("FTS1 Filter %p\n",pCursor));
initStringBuffer(&sb);
append(&sb, "select rowid, ");
zSql = sqlite3_mprintf("select rowid, * from %%_content %s",
append(&sb, v->zColumnNames);
append(&sb, " from %_content");
if( idxNum != QUERY_GENERIC) {
idxNum==QUERY_GENERIC ? "" : "where rowid=?");
append(&sb, " where rowid = ?");
}
rc = sql_prepare(v->db, v->zName, &c->pStmt, sb.s);
rc = sql_prepare(v->db, v->zName, &c->pStmt, zSql);
sqlite3_free(zSql);
if( rc!=SQLITE_OK ) goto out;
c->iCursorType = idxNum;
switch( idxNum ){
case QUERY_GENERIC:
break;
case QUERY_ROWID:
rc = sqlite3_bind_int64(c->pStmt, 1, sqlite3_value_int64(argv[0]));
if( rc!=SQLITE_OK ) goto out;
break;
default: /* full-text search */
{
const char *zQuery = (const char *)sqlite3_value_text(argv[0]);
DocList *pResult;
assert( idxNum<=QUERY_FULLTEXT+v->nColumns);
assert( idxNum<=QUERY_FULLTEXT+v->nColumn);
assert( argc==1 );
rc = fulltextQuery(v, idxNum-QUERY_FULLTEXT, zQuery, -1, &pResult);
if( rc!=SQLITE_OK ) goto out;
readerInit(&c->result, pResult);
break;
}
}
rc = fulltextNext(pCursor);
out:
free(sb.s);
return rc;
}
static int fulltextEof(sqlite3_vtab_cursor *pCursor){
fulltext_cursor *c = (fulltext_cursor *) pCursor;
return c->eof;
}
static int fulltextColumn(sqlite3_vtab_cursor *pCursor,
sqlite3_context *pContext, int idxCol){
fulltext_cursor *c = (fulltext_cursor *) pCursor;
fulltext_vtab *v = cursor_vtab(c);
const char *s;
if( idxCol==v->nColumns ){ /* a request for _all */
if( idxCol==v->nColumn ){ /* a request for _all */
sqlite3_result_null(pContext);
} else {
assert( idxCol<v->nColumns );
assert( idxCol<v->nColumn );
s = (const char *) sqlite3_column_text(c->pStmt, idxCol+1);
sqlite3_result_text(pContext, s, -1, SQLITE_TRANSIENT);
}
return SQLITE_OK;
}
|