Skip to content

Commit bced109

Browse files
committed
fix col names in vtab constraint creation
1 parent 978c644 commit bced109

File tree

3 files changed

+31
-22
lines changed

3 files changed

+31
-22
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ static void testChangesUnionQuery() {
7474
sqlite3_free(query);
7575

7676
query = crsql_changesUnionQuery(tblInfos, 2, "site_id IS ? AND db_vrsn > ?");
77-
printf("\tquery: %s\n", query);
7877
assert(
7978
strcmp(query,
8079
"SELECT tbl, pks, cid, col_vrsn, db_vrsn, site_id FROM (SELECT "

core/src/changes-vtab.c

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,9 @@ static int changesColumn(
312312
break;
313313
case CHANGES_SINCE_VTAB_SITE_ID:
314314
if (sqlite3_column_type(pCur->pChangesStmt, SITE_ID) == SQLITE_NULL) {
315-
sqlite3_result_blob(ctx, pCur->pTab->pExtData->siteId, SITE_ID_LEN,
316-
SQLITE_STATIC);
315+
sqlite3_result_null(ctx);
316+
// sqlite3_result_blob(ctx, pCur->pTab->pExtData->siteId, SITE_ID_LEN,
317+
// SQLITE_STATIC);
317318
} else {
318319
sqlite3_result_value(ctx,
319320
sqlite3_column_value(pCur->pChangesStmt, SITE_ID));
@@ -374,7 +375,7 @@ static int changesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
374375
sqlite3_free(zSql);
375376
if (rc != SQLITE_OK) {
376377
pTabBase->zErrMsg = sqlite3_mprintf(
377-
"crsql internal error preparing the statement to extract changes.");
378+
"error preparing stmt to extract changes %s", sqlite3_errmsg(db));
378379
sqlite3_finalize(pStmt);
379380
return rc;
380381
}
@@ -446,7 +447,7 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
446447
sqlite3_str *pStr = sqlite3_str_new(crsqlTab->db);
447448

448449
int firstConstaint = 1;
449-
char *constraint = 0;
450+
char *colName = 0;
450451
int argvIndex = 1;
451452
for (int i = 0; i < pIdxInfo->nConstraint; i++) {
452453
const struct sqlite3_index_constraint *pConstraint =
@@ -461,36 +462,44 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
461462
// the clock table has pks split out.
462463
break;
463464
case CHANGES_SINCE_VTAB_CID:
464-
constraint = "cid";
465+
colName = "cid";
465466
break;
466467
case CHANGES_SINCE_VTAB_CVAL:
467468
break;
468469
case CHANGES_SINCE_VTAB_COL_VRSN:
469-
constraint = "col_vrsn";
470+
colName = "col_vrsn";
470471
break;
471472
case CHANGES_SINCE_VTAB_DB_VRSN:
472-
constraint = "db_vrsn";
473+
colName = "db_vrsn";
473474
break;
474475
case CHANGES_SINCE_VTAB_SITE_ID:
475-
constraint = "site_id";
476+
colName = "site_id";
476477
break;
477478
}
478479

479-
if (constraint != 0) {
480+
if (colName != 0) {
480481
const char *opString = getOperatorString(pConstraint->op);
481482
if (opString == 0) {
482483
continue;
483484
}
484485
if (firstConstaint) {
485486
firstConstaint = 0;
486487
} else {
487-
sqlite3_str_append(pStr, " AND ", -1);
488+
sqlite3_str_appendall(pStr, " AND ");
489+
}
490+
491+
if (pConstraint->op == SQLITE_INDEX_CONSTRAINT_ISNOTNULL ||
492+
pConstraint->op == SQLITE_INDEX_CONSTRAINT_ISNULL) {
493+
sqlite3_str_appendf(pStr, "%s %s", colName, opString);
494+
pIdxInfo->aConstraintUsage[i].argvIndex = 0;
495+
pIdxInfo->aConstraintUsage[i].omit = 1;
496+
} else {
497+
sqlite3_str_appendf(pStr, "%s %s ?", colName, opString);
498+
pIdxInfo->aConstraintUsage[i].argvIndex = argvIndex;
499+
pIdxInfo->aConstraintUsage[i].omit = 1;
500+
argvIndex += 1;
488501
}
489-
sqlite3_str_appendf(pStr, "%s %s ?", constraint, opString);
490-
constraint = 0;
491-
pIdxInfo->aConstraintUsage[i].argvIndex = argvIndex;
492-
pIdxInfo->aConstraintUsage[i].omit = 1;
493-
argvIndex += 1;
502+
colName = 0;
494503
}
495504

496505
switch (pConstraint->iColumn) {

core/src/crsqlite.test.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,11 @@ static void teste2e() {
269269
// printf("db2sid: %s\n", db2siteid);
270270
// printf("db3sid: %s\n", db3siteid);
271271
// printf("tempsid: %s\n", tmpSiteid);
272-
assert(strcmp(tmpSiteid, db1siteid) == 0);
272+
assert(strcmp(tmpSiteid, "NULL") == 0);
273273

274274
rc = sqlite3_step(pStmt3);
275275
assert(rc == SQLITE_ROW);
276-
assert(strcmp((const char *)sqlite3_column_text(pStmt3, 0), db2siteid) == 0);
276+
assert(strcmp((const char *)sqlite3_column_text(pStmt3, 0), "NULL") == 0);
277277
sqlite3_finalize(pStmt3);
278278

279279
rc = sqlite3_prepare_v2(db2, "SELECT * FROM foo ORDER BY a ASC", -1, &pStmt2,
@@ -586,20 +586,21 @@ static void testPullingOnlyLocalChanges() {
586586
// `IS NOT NULL` also fails to call the virtual table bestIndex function with
587587
// any constraints p pIdxInfo->nConstraint
588588
sqlite3_prepare_v2(db,
589-
"SELECT count(*) FROM crsql_changes WHERE site_id = NULL",
589+
"SELECT count(*) FROM crsql_changes WHERE site_id IS NULL",
590590
-1, &pStmt, 0);
591591

592592
rc = sqlite3_step(pStmt);
593593
assert(rc == SQLITE_ROW);
594594

595595
int count = sqlite3_column_int(pStmt, 0);
596596
// we created 2 local changes, we should get 2 changes back
597+
printf("count: %d\n", count);
597598
assert(count == 2);
598599
sqlite3_finalize(pStmt);
599600

600-
sqlite3_prepare_v2(db,
601-
"SELECT count(*) FROM crsql_changes WHERE site_id != NULL",
602-
-1, &pStmt, 0);
601+
sqlite3_prepare_v2(
602+
db, "SELECT count(*) FROM crsql_changes WHERE site_id IS NOT NULL", -1,
603+
&pStmt, 0);
603604
rc = sqlite3_step(pStmt);
604605
assert(rc == SQLITE_ROW);
605606
count = sqlite3_column_int(pStmt, 0);

0 commit comments

Comments
 (0)