Skip to content

Commit a14d1ab

Browse files
committed
use idxStr in xFilter
1 parent d401331 commit a14d1ab

File tree

4 files changed

+60
-89
lines changed

4 files changed

+60
-89
lines changed

core/src/changes-vtab-read.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Construct the query to grab the changes made against
1010
* rows in a given table
1111
*/
12-
char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo, int idxNum) {
12+
char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo) {
1313
if (tableInfo->pksLen == 0) {
1414
return 0;
1515
}
@@ -22,13 +22,8 @@ char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo, int idxNum) {
2222
__crsql_col_version as col_vrsn,\
2323
__crsql_db_version as db_vrsn,\
2424
__crsql_site_id as site_id\
25-
FROM \"%s__crsql_clock\"\
26-
WHERE\
27-
site_id IS %s ?\
28-
AND\
29-
db_vrsn > ?",
30-
tableInfo->tblName, crsql_quoteConcat(tableInfo->pks, tableInfo->pksLen),
31-
tableInfo->tblName, (idxNum & 8) == 8 ? "" : "NOT");
25+
FROM \"%s__crsql_clock\"",
26+
tableInfo->tblName, crsql_quoteConcat(tableInfo->pks, tableInfo->pksLen));
3227

3328
return zSql;
3429
}
@@ -46,14 +41,14 @@ char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo, int idxNum) {
4641
* set of changes
4742
*/
4843
char *crsql_changesUnionQuery(crsql_TableInfo **tableInfos, int tableInfosLen,
49-
int idxNum) {
44+
const char *idxStr) {
5045
char **unionsArr = sqlite3_malloc(tableInfosLen * sizeof(char *));
5146
char *unionsStr = 0;
5247
int i = 0;
5348

5449
// TODO: what if there are no table infos?
5550
for (i = 0; i < tableInfosLen; ++i) {
56-
unionsArr[i] = crsql_changesQueryForTable(tableInfos[i], idxNum);
51+
unionsArr[i] = crsql_changesQueryForTable(tableInfos[i]);
5752
if (unionsArr[i] == 0) {
5853
for (int j = 0; j < i; j++) {
5954
sqlite3_free(unionsArr[j]);
@@ -76,9 +71,10 @@ char *crsql_changesUnionQuery(crsql_TableInfo **tableInfos, int tableInfosLen,
7671

7772
// compose the final query
7873
return sqlite3_mprintf(
79-
"SELECT tbl, pks, cid, col_vrsn, db_vrsn, site_id FROM (%z) ORDER BY "
74+
"SELECT tbl, pks, cid, col_vrsn, db_vrsn, site_id FROM (%z) %s%s ORDER "
75+
"BY "
8076
"db_vrsn, tbl ASC",
81-
unionsStr);
77+
unionsStr, strlen(idxStr) > 0 ? "WHERE " : "", idxStr);
8278
// %z frees unionsStr https://www.sqlite.org/printf.html#percentz
8379
}
8480

core/src/changes-vtab-read.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SQLITE_EXTENSION_INIT3
77
#include "changes-vtab-common.h"
88
#include "tableinfo.h"
99

10-
char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo, int idxNum);
10+
char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo);
1111

1212
#define TBL 0
1313
#define PKS 1
@@ -16,7 +16,7 @@ char *crsql_changesQueryForTable(crsql_TableInfo *tableInfo, int idxNum);
1616
#define DB_VRSN 4
1717
#define SITE_ID 5
1818
char *crsql_changesUnionQuery(crsql_TableInfo **tableInfos, int tableInfosLen,
19-
int idxNum);
19+
const char *idxStr);
2020
char *crsql_rowPatchDataQuery(sqlite3 *db, crsql_TableInfo *tblInfo,
2121
const char *colName, const char *pks);
2222

core/src/changes-vtab-read.test.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static void testChangesQueryForTable() {
2323
rc += crsql_getTableInfo(db, "foo", &tblInfo, &err);
2424
assert(rc == SQLITE_OK);
2525

26-
char *query = crsql_changesQueryForTable(tblInfo, 6);
26+
char *query = crsql_changesQueryForTable(tblInfo);
2727

2828
assert(strcmp(query,
2929
"SELECT \'foo\' as tbl, quote(\"a\") as pks, "
@@ -33,7 +33,7 @@ static void testChangesQueryForTable() {
3333
"WHERE site_id IS NOT ? AND db_vrsn > ?") == 0);
3434
sqlite3_free(query);
3535

36-
query = crsql_changesQueryForTable(tblInfo, 8);
36+
query = crsql_changesQueryForTable(tblInfo);
3737
assert(strcmp(query,
3838
"SELECT \'foo\' as tbl, quote(\"a\") as pks, "
3939
"__crsql_col_name as cid, __crsql_col_version as "
@@ -68,7 +68,7 @@ static void testChangesUnionQuery() {
6868
rc += crsql_getTableInfo(db, "bar", &tblInfos[1], &err);
6969
assert(rc == SQLITE_OK);
7070

71-
char *query = crsql_changesUnionQuery(tblInfos, 2, 6);
71+
char *query = crsql_changesUnionQuery(tblInfos, 2, "");
7272
assert(
7373
strcmp(
7474
query,
@@ -84,7 +84,7 @@ static void testChangesUnionQuery() {
8484
"NOT ? AND db_vrsn > ?) ORDER BY db_vrsn, tbl ASC") == 0);
8585
sqlite3_free(query);
8686

87-
query = crsql_changesUnionQuery(tblInfos, 2, 8);
87+
query = crsql_changesUnionQuery(tblInfos, 2, "");
8888
assert(
8989
strcmp(
9090
query,

core/src/changes-vtab.c

Lines changed: 46 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ static int changesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
361361
}
362362

363363
char *zSql = crsql_changesUnionQuery(pTab->pExtData->zpTableInfos,
364-
pTab->pExtData->tableInfosLen, idxNum);
364+
pTab->pExtData->tableInfosLen, idxStr);
365365

366366
if (zSql == 0) {
367367
pTabBase->zErrMsg = sqlite3_mprintf(
@@ -379,46 +379,59 @@ static int changesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
379379
return rc;
380380
}
381381

382-
// pull user provided params to `getChanges`
383-
int i = 0;
384-
sqlite3_int64 versionBound = MIN_POSSIBLE_DB_VERSION;
385-
const char *requestorSiteId = "aa";
386-
int siteIdType = SQLITE_BLOB;
387-
int requestorSiteIdLen = 1;
388-
if (idxNum & 2) {
389-
versionBound = sqlite3_value_int64(argv[i]);
390-
++i;
391-
}
392-
if (idxNum & 4) {
393-
siteIdType = sqlite3_value_type(argv[i]);
394-
requestorSiteIdLen = sqlite3_value_bytes(argv[i]);
395-
if (requestorSiteIdLen != 0) {
396-
requestorSiteId = (const char *)sqlite3_value_blob(argv[i]);
397-
} else {
398-
requestorSiteIdLen = 1;
399-
}
400-
++i;
401-
}
402-
403382
// now bind the params.
404-
// for each table info we need to bind 2 params:
405-
// 1. the site id
406-
// 2. the version
407-
int j = 1;
408-
for (i = 0; i < pTab->pExtData->tableInfosLen; ++i) {
409-
if (siteIdType == SQLITE_NULL) {
410-
sqlite3_bind_null(pStmt, j++);
411-
} else {
412-
sqlite3_bind_blob(pStmt, j++, requestorSiteId, requestorSiteIdLen,
413-
SQLITE_STATIC);
383+
// for each arg, bind.
384+
for (int i = 0; i < argc; ++i) {
385+
rc = sqlite3_bind_value(pStmt, i + 1, argv[i]);
386+
if (rc != SQLITE_OK) {
387+
pTabBase->zErrMsg = sqlite3_mprintf(
388+
"error binding params to the statement to extract "
389+
"changes.");
390+
sqlite3_finalize(pStmt);
391+
return rc;
414392
}
415-
sqlite3_bind_int64(pStmt, j++, versionBound);
416393
}
417394

418395
pCrsr->pChangesStmt = pStmt;
419396
return changesNext((sqlite3_vtab_cursor *)pCrsr);
420397
}
421398

399+
static const char *getOperatorString(unsigned char op) {
400+
// SQLITE_INDEX_CONSTRAINT_NE
401+
switch (op) {
402+
case SQLITE_INDEX_CONSTRAINT_EQ:
403+
return "=";
404+
case SQLITE_INDEX_CONSTRAINT_GT:
405+
return ">";
406+
case SQLITE_INDEX_CONSTRAINT_LE:
407+
return "<=";
408+
case SQLITE_INDEX_CONSTRAINT_LT:
409+
return "<";
410+
case SQLITE_INDEX_CONSTRAINT_GE:
411+
return ">=";
412+
case SQLITE_INDEX_CONSTRAINT_MATCH:
413+
return "MATCH";
414+
case SQLITE_INDEX_CONSTRAINT_LIKE:
415+
return "LIKE";
416+
case SQLITE_INDEX_CONSTRAINT_GLOB:
417+
return "GLOB";
418+
case SQLITE_INDEX_CONSTRAINT_REGEXP:
419+
return "REGEXP";
420+
case SQLITE_INDEX_CONSTRAINT_NE:
421+
return "!=";
422+
case SQLITE_INDEX_CONSTRAINT_ISNOT:
423+
return "IS NOT";
424+
case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
425+
return "IS NOT NULL";
426+
case SQLITE_INDEX_CONSTRAINT_ISNULL:
427+
return "IS NULL";
428+
case SQLITE_INDEX_CONSTRAINT_IS:
429+
return "IS";
430+
default:
431+
return 0;
432+
}
433+
}
434+
422435
/*
423436
** SQLite will invoke this method one or more times while planning a query
424437
** that uses the virtual table. This routine needs to create
@@ -481,8 +494,6 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
481494
argvIndex += 1;
482495
}
483496

484-
const struct sqlite3_index_constraint *pConstraint =
485-
&pIdxInfo->aConstraint[i];
486497
switch (pConstraint->iColumn) {
487498
case CHANGES_SINCE_VTAB_DB_VRSN:
488499
idxNum |= 2;
@@ -520,42 +531,6 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
520531
return SQLITE_OK;
521532
}
522533

523-
static const char *getOperatorString(unsigned char op) {
524-
// SQLITE_INDEX_CONSTRAINT_NE
525-
switch (op) {
526-
case SQLITE_INDEX_CONSTRAINT_EQ:
527-
return "=";
528-
case SQLITE_INDEX_CONSTRAINT_GT:
529-
return ">";
530-
case SQLITE_INDEX_CONSTRAINT_LE:
531-
return "<=";
532-
case SQLITE_INDEX_CONSTRAINT_LT:
533-
return "<";
534-
case SQLITE_INDEX_CONSTRAINT_GE:
535-
return ">=";
536-
case SQLITE_INDEX_CONSTRAINT_MATCH:
537-
return "MATCH";
538-
case SQLITE_INDEX_CONSTRAINT_LIKE:
539-
return "LIKE";
540-
case SQLITE_INDEX_CONSTRAINT_GLOB:
541-
return "GLOB";
542-
case SQLITE_INDEX_CONSTRAINT_REGEXP:
543-
return "REGEXP";
544-
case SQLITE_INDEX_CONSTRAINT_NE:
545-
return "!=";
546-
case SQLITE_INDEX_CONSTRAINT_ISNOT:
547-
return "IS NOT";
548-
case SQLITE_INDEX_CONSTRAINT_ISNOTNULL:
549-
return "IS NOT NULL";
550-
case SQLITE_INDEX_CONSTRAINT_ISNULL:
551-
return "IS NULL";
552-
case SQLITE_INDEX_CONSTRAINT_IS:
553-
return "IS";
554-
default:
555-
return 0;
556-
}
557-
}
558-
559534
static int changesApply(sqlite3_vtab *pVTab, int argc, sqlite3_value **argv,
560535
sqlite3_int64 *pRowid) {
561536
int argv0Type = sqlite3_value_type(argv[0]);

0 commit comments

Comments
 (0)