Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let virtual table implementations know which SQL string is being executed #87

Merged
merged 4 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/sqlite.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -7076,6 +7076,9 @@ struct sqlite3_module {
/* The methods above are in versions 1 and 2 of the sqlite_module object.
** Those below are for version 3 and greater. */
int (*xShadowName)(const char*);
/* The methods below relate to features contributed by the community and
** are available for version 700 and greater. */
int (*xPreparedSql)(sqlite3_vtab_cursor*, const char*);
};

/*
Expand Down
17 changes: 15 additions & 2 deletions src/test8.c
Original file line number Diff line number Diff line change
Expand Up @@ -1293,6 +1293,17 @@ static int echoRollbackTo(sqlite3_vtab *pVTab, int iSavepoint){
return SQLITE_OK;
}

static int echoShadowName(const char *name){
assert( name );
return SQLITE_OK;
}

static int echoPreparedSql(sqlite3_vtab_cursor *cur, const char *sql){
assert( cur );
assert( sql );
return SQLITE_OK;
}

/*
** A virtual table module that merely "echos" the contents of another
** table (like an SQL VIEW).
Expand Down Expand Up @@ -1321,7 +1332,7 @@ static sqlite3_module echoModule = {
};

static sqlite3_module echoModuleV2 = {
2, /* iVersion */
700, /* iVersion */
echoCreate,
echoConnect,
echoBestIndex,
Expand All @@ -1343,7 +1354,9 @@ static sqlite3_module echoModuleV2 = {
echoRename, /* xRename - rename the table */
echoSavepoint,
echoRelease,
echoRollbackTo
echoRollbackTo,
echoShadowName,
echoPreparedSql,
};

/*
Expand Down
33 changes: 33 additions & 0 deletions src/vdbe.c
Original file line number Diff line number Diff line change
Expand Up @@ -8072,6 +8072,39 @@ case OP_VInitIn: { /* out2 */
}
#endif /* SQLITE_OMIT_VIRTUALTABLE */

#ifndef SQLITE_OMIT_VIRTUALTABLE
/* Opcode: VPreparedSql P1 * * * *
**
** P1 is a cursor opened using VOpen.
**
** This opcode invokes the xPreparedSql method on the virtual table specified
** by P1. The SQL text parameter to xPreparedSql is obtained from Vdbe* `p`.
**
*/
case OP_VPreparedSql: {
const sqlite3_module *pModule;
sqlite3_vtab_cursor *pVCur;
sqlite3_vtab *pVtab;
VdbeCursor *pCur;

pCur = p->apCsr[pOp->p1];
assert( pCur!=0 );
assert( pCur->eCurType==CURTYPE_VTAB );
pVCur = pCur->uc.pVCur;
pVtab = pVCur->pVtab;
pModule = pVtab->pModule;

/* Invoke the xPreparedSql method */
if( pModule->iVersion>=700 ){
if( pModule->xPreparedSql && p->zSql ){
rc = pModule->xPreparedSql(pVCur, p->zSql);
if( rc ) goto abort_due_to_error;
}
}

break;
}
#endif /* SQLITE_OMIT_VIRTUALTABLE */

#ifndef SQLITE_OMIT_VIRTUALTABLE
/* Opcode: VFilter P1 P2 P3 P4 *
Expand Down
2 changes: 2 additions & 0 deletions src/wherecode.c
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,8 @@ Bitmask sqlite3WhereCodeOneLoopStart(
int addrNotFound;
int nConstraint = pLoop->nLTerm;

sqlite3VdbeAddOp1(v, OP_VPreparedSql, iCur);

iReg = sqlite3GetTempRange(pParse, nConstraint+2);
addrNotFound = pLevel->addrBrk;
for(j=0; j<nConstraint; j++){
Expand Down
40 changes: 40 additions & 0 deletions test/rowvaluevtab.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ ifcapable !vtab {

register_echo_module db

#############
# Test echo
#############

do_execsql_test 1.0 {
CREATE TABLE t1(a, b, c);
CREATE INDEX t1b ON t1(b);
Expand Down Expand Up @@ -92,4 +96,40 @@ do_vfilter4_test 1.4f {
SELECT a FROM e1 WHERE (b, c) IN ( VALUES(2, 2) )
} {{SELECT rowid, a, b, c FROM 't1' WHERE b = ?}}

#######################################################################
# Test echo_v2. We simply want to ensure that OP_VPreparedSql executes
#######################################################################

do_execsql_test 2.0 {
CREATE TABLE t2(a, b, c);
CREATE INDEX t2b ON t2(b);
INSERT INTO t2 VALUES('one', 1, 1);
INSERT INTO t2 VALUES('two', 1, 2);
INSERT INTO t2 VALUES('three', 1, 3);

WITH s(i) AS (
SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<10000
) INSERT INTO t2 SELECT NULL, NULL, NULL FROM s;
CREATE VIRTUAL TABLE e2 USING echo_v2(t2);
}

proc do_vpreparedsql1_test {tn sql expected} {
set rc -1
db eval "explain $sql" {
if {$opcode=="VPreparedSql"} {
set rc 0
}
}
if {$rc != $expected} {
error "Unexpected result $rc, was hoping for $expected"
}
}

do_execsql_test 2.1 {
SELECT a FROM e2 WHERE (b, c) IN ( VALUES(1, 3) )
} {three}
do_vpreparedsql1_test 2.1f {
SELECT a FROM e2 WHERE (b, c) IN ( VALUES(2, 2) )
} {0}

finish_test