Skip to content

Commit

Permalink
fix col names in vtab constraint creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tantaman committed Apr 22, 2023
1 parent 978c644 commit bced109
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
1 change: 0 additions & 1 deletion core/src/changes-vtab-read.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ static void testChangesUnionQuery() {
sqlite3_free(query);

query = crsql_changesUnionQuery(tblInfos, 2, "site_id IS ? AND db_vrsn > ?");
printf("\tquery: %s\n", query);
assert(
strcmp(query,
"SELECT tbl, pks, cid, col_vrsn, db_vrsn, site_id FROM (SELECT "
Expand Down
39 changes: 24 additions & 15 deletions core/src/changes-vtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,9 @@ static int changesColumn(
break;
case CHANGES_SINCE_VTAB_SITE_ID:
if (sqlite3_column_type(pCur->pChangesStmt, SITE_ID) == SQLITE_NULL) {
sqlite3_result_blob(ctx, pCur->pTab->pExtData->siteId, SITE_ID_LEN,
SQLITE_STATIC);
sqlite3_result_null(ctx);
// sqlite3_result_blob(ctx, pCur->pTab->pExtData->siteId, SITE_ID_LEN,
// SQLITE_STATIC);
} else {
sqlite3_result_value(ctx,
sqlite3_column_value(pCur->pChangesStmt, SITE_ID));
Expand Down Expand Up @@ -374,7 +375,7 @@ static int changesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
sqlite3_free(zSql);
if (rc != SQLITE_OK) {
pTabBase->zErrMsg = sqlite3_mprintf(
"crsql internal error preparing the statement to extract changes.");
"error preparing stmt to extract changes %s", sqlite3_errmsg(db));
sqlite3_finalize(pStmt);
return rc;
}
Expand Down Expand Up @@ -446,7 +447,7 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
sqlite3_str *pStr = sqlite3_str_new(crsqlTab->db);

int firstConstaint = 1;
char *constraint = 0;
char *colName = 0;
int argvIndex = 1;
for (int i = 0; i < pIdxInfo->nConstraint; i++) {
const struct sqlite3_index_constraint *pConstraint =
Expand All @@ -461,36 +462,44 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
// the clock table has pks split out.
break;
case CHANGES_SINCE_VTAB_CID:
constraint = "cid";
colName = "cid";
break;
case CHANGES_SINCE_VTAB_CVAL:
break;
case CHANGES_SINCE_VTAB_COL_VRSN:
constraint = "col_vrsn";
colName = "col_vrsn";
break;
case CHANGES_SINCE_VTAB_DB_VRSN:
constraint = "db_vrsn";
colName = "db_vrsn";
break;
case CHANGES_SINCE_VTAB_SITE_ID:
constraint = "site_id";
colName = "site_id";
break;
}

if (constraint != 0) {
if (colName != 0) {
const char *opString = getOperatorString(pConstraint->op);
if (opString == 0) {
continue;
}
if (firstConstaint) {
firstConstaint = 0;
} else {
sqlite3_str_append(pStr, " AND ", -1);
sqlite3_str_appendall(pStr, " AND ");
}

if (pConstraint->op == SQLITE_INDEX_CONSTRAINT_ISNOTNULL ||
pConstraint->op == SQLITE_INDEX_CONSTRAINT_ISNULL) {
sqlite3_str_appendf(pStr, "%s %s", colName, opString);
pIdxInfo->aConstraintUsage[i].argvIndex = 0;
pIdxInfo->aConstraintUsage[i].omit = 1;
} else {
sqlite3_str_appendf(pStr, "%s %s ?", colName, opString);
pIdxInfo->aConstraintUsage[i].argvIndex = argvIndex;
pIdxInfo->aConstraintUsage[i].omit = 1;
argvIndex += 1;
}
sqlite3_str_appendf(pStr, "%s %s ?", constraint, opString);
constraint = 0;
pIdxInfo->aConstraintUsage[i].argvIndex = argvIndex;
pIdxInfo->aConstraintUsage[i].omit = 1;
argvIndex += 1;
colName = 0;
}

switch (pConstraint->iColumn) {
Expand Down
13 changes: 7 additions & 6 deletions core/src/crsqlite.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ static void teste2e() {
// printf("db2sid: %s\n", db2siteid);
// printf("db3sid: %s\n", db3siteid);
// printf("tempsid: %s\n", tmpSiteid);
assert(strcmp(tmpSiteid, db1siteid) == 0);
assert(strcmp(tmpSiteid, "NULL") == 0);

rc = sqlite3_step(pStmt3);
assert(rc == SQLITE_ROW);
assert(strcmp((const char *)sqlite3_column_text(pStmt3, 0), db2siteid) == 0);
assert(strcmp((const char *)sqlite3_column_text(pStmt3, 0), "NULL") == 0);
sqlite3_finalize(pStmt3);

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

rc = sqlite3_step(pStmt);
assert(rc == SQLITE_ROW);

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

sqlite3_prepare_v2(db,
"SELECT count(*) FROM crsql_changes WHERE site_id != NULL",
-1, &pStmt, 0);
sqlite3_prepare_v2(
db, "SELECT count(*) FROM crsql_changes WHERE site_id IS NOT NULL", -1,
&pStmt, 0);
rc = sqlite3_step(pStmt);
assert(rc == SQLITE_ROW);
count = sqlite3_column_int(pStmt, 0);
Expand Down

0 comments on commit bced109

Please sign in to comment.