Skip to content

Commit

Permalink
get test_crsql_changes_filters working by including order by clause a…
Browse files Browse the repository at this point in the history
…nd correctly including `WHERE` in query creation
  • Loading branch information
tantaman committed Jun 29, 2023
1 parent cf9467a commit 8432f50
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
5 changes: 2 additions & 3 deletions core/src/changes-vtab-read.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ char *crsql_changesUnionQuery(crsql_TableInfo **tableInfos, int tableInfosLen,
return sqlite3_mprintf(
"SELECT tbl, pks, cid, col_vrsn, db_vrsn, site_id, _rowid_, seq FROM "
"(%z) "
"%s%s",
unionsStr, idxStr != 0 && strlen(idxStr) > 0 ? "WHERE " : "",
idxStr == 0 ? "" : idxStr);
"%s",
unionsStr, idxStr == 0 ? "" : idxStr);
// %z frees unionsStr https://www.sqlite.org/printf.html#percentz
}

Expand Down
30 changes: 21 additions & 9 deletions core/src/changes-vtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ static int changesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
sqlite3_free(zSql);
if (rc != SQLITE_OK) {
pTabBase->zErrMsg = sqlite3_mprintf(
"error preparing stmt to extract changes %s", sqlite3_errmsg(db));
"error preparing stmt to extract changes %s %s", sqlite3_errmsg(db));
sqlite3_finalize(pStmt);
return rc;
}
Expand Down Expand Up @@ -462,6 +462,13 @@ static const char *getClockTblColName(int colIdx) {
return 0;
}

static int colIsUsable(const struct sqlite3_index_constraint *pConstraint) {
return pConstraint->usable &&
pConstraint->iColumn != CHANGES_SINCE_VTAB_TBL &&
pConstraint->iColumn != CHANGES_SINCE_VTAB_PK &&
pConstraint->iColumn != CHANGES_SINCE_VTAB_CVAL;
}

/*
** SQLite will invoke this method one or more times while planning a query
** that uses the virtual table. This routine needs to create
Expand All @@ -478,18 +485,22 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
int firstConstraint = 1;
const char *colName = 0;
int argvIndex = 1;
for (int i = 0; i < pIdxInfo->nConstraint; i++) {
int numUsable = 0;
for (int i = 0; i < pIdxInfo->nConstraint; ++i) {
if (colIsUsable(&pIdxInfo->aConstraint[i])) {
++numUsable;
}
}
if (numUsable > 0) {
sqlite3_str_appendall(pStr, "WHERE ");
}
for (int i = 0; i < pIdxInfo->nConstraint && numUsable > 0; i++) {
const struct sqlite3_index_constraint *pConstraint =
&pIdxInfo->aConstraint[i];
if (pConstraint->usable == 0) {
if (!colIsUsable(&pIdxInfo->aConstraint[i])) {
continue;
}
if (pConstraint->iColumn != CHANGES_SINCE_VTAB_TBL &&
pConstraint->iColumn != CHANGES_SINCE_VTAB_PK &&
pConstraint->iColumn != CHANGES_SINCE_VTAB_CVAL) {
colName = getClockTblColName(pConstraint->iColumn);
}

colName = getClockTblColName(pConstraint->iColumn);
if (colName != 0) {
const char *opString = getOperatorString(pConstraint->op);
if (opString == 0) {
Expand Down Expand Up @@ -574,6 +585,7 @@ static int changesBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo) {
pIdxInfo->idxNum = idxNum;
pIdxInfo->orderByConsumed = 1;
pIdxInfo->idxStr = sqlite3_str_finish(pStr);
// printf("q: %s\n", pIdxInfo->idxStr);
pIdxInfo->needToFreeIdxStr = 1;
return SQLITE_OK;
}
Expand Down
2 changes: 1 addition & 1 deletion py/correctness/tests/test_crsql_changes_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def setup_db():
c.execute("DELETE FROM item WHERE id = 411")
c.commit()

return (c, c.execute(changes_query).fetchall())
return (c, c.execute(changes_query + " ORDER BY db_version, seq ASC").fetchall())


changes_query = "SELECT [table], pk, cid, val, col_version, db_version, site_id, seq FROM crsql_changes"
Expand Down

0 comments on commit 8432f50

Please sign in to comment.