Add the tables_used() table-valued function as a variation on bytecode().
FossilOrigin-Name: 6283c677d57220e54375a6463f453c6d068e042263558df16cff1055d1b0d3f5
This commit is contained in:
parent
c5eeacd58b
commit
8f78a528d5
14
manifest
14
manifest
@ -1,5 +1,5 @@
|
||||
C Merge\supdates\sfrom\strunk.
|
||||
D 2020-03-26T15:28:46.653
|
||||
C Add\sthe\stables_used()\stable-valued\sfunction\sas\sa\svariation\son\sbytecode().
|
||||
D 2020-03-26T16:48:18.810
|
||||
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
|
||||
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
|
||||
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
|
||||
@ -607,12 +607,12 @@ F src/vdbe.c c1c123c6248fa88940b932a00bcc75056921b6d046d45a82566cb97415d2299c
|
||||
F src/vdbe.h 07b8c636a87df8b6e58f29d6badd7f10d5844353deff1d7c88ed1c2bfe3bbd35
|
||||
F src/vdbeInt.h 5f36e2261731e6a39c66c6902634e474d4a98ed14ca10be6dea5c3776ece4342
|
||||
F src/vdbeapi.c 1252d80c548711e47a6d84dae88ed4e95d3fbb4e7bd0eaa1347299af7efddf02
|
||||
F src/vdbeaux.c 9aceb1802cda5f0ce45951afc4b99d5b352d68f95acca7e731bbdc61cd585702
|
||||
F src/vdbeaux.c 2b81b42788e718786817fe339eb728e81401542fb221c7f95b24ce39a735eff8
|
||||
F src/vdbeblob.c 253ed82894924c362a7fa3079551d3554cd1cdace39aa833da77d3bc67e7c1b1
|
||||
F src/vdbemem.c 39b942ecca179f4f30a32b54579a85d74ccaefa5af2a0ad2700abe5ef0768b22
|
||||
F src/vdbesort.c 2be76d26998ce2b3324cdcc9f6443728e54b6c7677c553ad909c7d7cfab587df
|
||||
F src/vdbetrace.c fa3bf238002f0bbbdfb66cc8afb0cea284ff9f148d6439bc1f6f2b4c3b7143f0
|
||||
F src/vdbevtab.c 7239b3469ab67f4698889780ab6808a6fc2ba49ceaaaf477053acc9501d77403
|
||||
F src/vdbevtab.c 3f7fd339425729f6cef36ef0bc576bf42641d2a664c746b54a6e2c045ae1a248
|
||||
F src/vtab.c 7b704a90515a239c6cdba6a66b1bb3a385e62326cceb5ecb05ec7a091d6b8515
|
||||
F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9
|
||||
F src/wal.c 697424314e40d99f93f548c7bfa526c10e87f4bdf64d5a76a96b999dd7133ebc
|
||||
@ -1861,7 +1861,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
|
||||
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
|
||||
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
|
||||
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
|
||||
P ca8c5f028b9f6f32639c8bc9df5dc02537e21385fcbe7e1ae600c640977ea0d5 27936e6884e77093533719c7955a17f051cfb359872e51a6d1481152e6256443
|
||||
R 3e64a9cf6d09bafc409422474d49e6cf
|
||||
P d7db09101878102e192ee7a81437e8f6f2e317ddf110852673a2e81d1f80ae0e
|
||||
R 97037e53b3358656d7c80bf175451cab
|
||||
U drh
|
||||
Z 7c5407f80f4f891ef89724feeeb5e566
|
||||
Z 77036c852e70ef7951f1cc73bc25be37
|
||||
|
@ -1 +1 @@
|
||||
d7db09101878102e192ee7a81437e8f6f2e317ddf110852673a2e81d1f80ae0e
|
||||
6283c677d57220e54375a6463f453c6d068e042263558df16cff1055d1b0d3f5
|
@ -1953,7 +1953,7 @@ void sqlite3VdbeFrameMemDel(void *pArg){
|
||||
int sqlite3VdbeNextOpcode(
|
||||
Vdbe *p, /* The statement being explained */
|
||||
Mem *pSub, /* Storage for keeping track of subprogram nesting */
|
||||
int bEqp, /* True to return only OP_Explain opcodes */
|
||||
int eMode, /* 0: normal. 1: EQP. 2: TablesUsed */
|
||||
int *piPc, /* IN/OUT: Current rowid. Overwritten with next rowid */
|
||||
int *piAddr, /* OUT: Write index into (*paOp)[] here */
|
||||
Op **paOp /* OUT: Write the opcode array here */
|
||||
@ -2035,9 +2035,20 @@ int sqlite3VdbeNextOpcode(
|
||||
nRow += aOp[i].p4.pProgram->nOp;
|
||||
}
|
||||
}
|
||||
if( !bEqp ) break;
|
||||
if( aOp[i].opcode==OP_Explain ) break;
|
||||
if( aOp[i].opcode==OP_Init && p->pc>1 ) break;
|
||||
if( eMode==0 ) break;
|
||||
#ifdef SQLITE_ENABLE_BYTECODE_VTAB
|
||||
if( eMode==2 ){
|
||||
Op *pOp = aOp + i;
|
||||
if( pOp->opcode==OP_OpenRead ) break;
|
||||
if( pOp->opcode==OP_OpenWrite && (pOp->p5 & OPFLAG_P2ISREG)==0 ) break;
|
||||
if( pOp->opcode==OP_ReopenIdx ) break;
|
||||
}else
|
||||
#endif
|
||||
{
|
||||
assert( eMode==1 );
|
||||
if( aOp[i].opcode==OP_Explain ) break;
|
||||
if( aOp[i].opcode==OP_Init && p->pc>1 ) break;
|
||||
}
|
||||
}
|
||||
*piPc = iPc;
|
||||
*piAddr = i;
|
||||
|
118
src/vdbevtab.c
118
src/vdbevtab.c
@ -23,6 +23,7 @@ typedef struct bytecodevtab bytecodevtab;
|
||||
struct bytecodevtab {
|
||||
sqlite3_vtab base; /* Base class - must be first */
|
||||
sqlite3 *db; /* Database connection */
|
||||
int bTablesUsed; /* 2 for tables_used(). 0 for bytecode(). */
|
||||
};
|
||||
|
||||
/* A cursor for scanning through the bytecode
|
||||
@ -37,6 +38,9 @@ struct bytecodevtab_cursor {
|
||||
int showSubprograms; /* Provide a listing of subprograms */
|
||||
Op *aOp; /* Operand array */
|
||||
char *zP4; /* Rendered P4 value */
|
||||
const char *zType; /* tables_used.type */
|
||||
const char *zSchema; /* tables_used.schema */
|
||||
const char *zName; /* tables_used.name */
|
||||
Mem sub; /* Subprograms */
|
||||
};
|
||||
|
||||
@ -52,27 +56,41 @@ static int bytecodevtabConnect(
|
||||
){
|
||||
bytecodevtab *pNew;
|
||||
int rc;
|
||||
int isTabUsed = pAux!=0;
|
||||
const char *azSchema[2] = {
|
||||
/* bytecode() schema */
|
||||
"CREATE TABLE x("
|
||||
"addr INT,"
|
||||
"opcode TEXT,"
|
||||
"p1 INT,"
|
||||
"p2 INT,"
|
||||
"p3 INT,"
|
||||
"p4 TEXT,"
|
||||
"p5 INT,"
|
||||
"comment TEXT,"
|
||||
"subprog TEXT,"
|
||||
"stmt HIDDEN"
|
||||
");",
|
||||
|
||||
rc = sqlite3_declare_vtab(db,
|
||||
"CREATE TABLE x("
|
||||
"addr INT,"
|
||||
"opcode TEXT,"
|
||||
"p1 INT,"
|
||||
"p2 INT,"
|
||||
"p3 INT,"
|
||||
"p4 TEXT,"
|
||||
"p5 INT,"
|
||||
"comment TEXT,"
|
||||
"subprog TEXT,"
|
||||
"stmt HIDDEN"
|
||||
");"
|
||||
);
|
||||
/* Tables_used() schema */
|
||||
"CREATE TABLE x("
|
||||
"type TEXT,"
|
||||
"schema TEXT,"
|
||||
"name TEXT,"
|
||||
"wr INT,"
|
||||
"subprog TEXT,"
|
||||
"stmt HIDDEN"
|
||||
");"
|
||||
};
|
||||
|
||||
rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
|
||||
if( rc==SQLITE_OK ){
|
||||
pNew = sqlite3_malloc( sizeof(*pNew) );
|
||||
*ppVtab = (sqlite3_vtab*)pNew;
|
||||
if( pNew==0 ) return SQLITE_NOMEM;
|
||||
memset(pNew, 0, sizeof(*pNew));
|
||||
pNew->db = db;
|
||||
pNew->bTablesUsed = isTabUsed*2;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
@ -113,6 +131,9 @@ static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
|
||||
}
|
||||
pCur->pStmt = 0;
|
||||
pCur->needFinalize = 0;
|
||||
pCur->zType = 0;
|
||||
pCur->zSchema = 0;
|
||||
pCur->zName = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@ -131,15 +152,21 @@ static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
|
||||
*/
|
||||
static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
|
||||
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
|
||||
bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
|
||||
int rc;
|
||||
if( pCur->zP4 ){
|
||||
sqlite3_free(pCur->zP4);
|
||||
pCur->zP4 = 0;
|
||||
}
|
||||
if( pCur->zName ){
|
||||
pCur->zName = 0;
|
||||
pCur->zType = 0;
|
||||
pCur->zSchema = 0;
|
||||
}
|
||||
rc = sqlite3VdbeNextOpcode(
|
||||
(Vdbe*)pCur->pStmt,
|
||||
pCur->showSubprograms ? &pCur->sub : 0,
|
||||
0,
|
||||
pTab->bTablesUsed,
|
||||
&pCur->iRowid,
|
||||
&pCur->iAddr,
|
||||
&pCur->aOp);
|
||||
@ -169,8 +196,42 @@ static int bytecodevtabColumn(
|
||||
int i /* Which column to return */
|
||||
){
|
||||
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
|
||||
bytecodevtab *pVTab;
|
||||
bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
|
||||
Op *pOp = pCur->aOp + pCur->iAddr;
|
||||
if( pVTab->bTablesUsed ){
|
||||
if( i==4 ){
|
||||
i = 8;
|
||||
}else{
|
||||
if( i<=2 && pCur->zType==0 ){
|
||||
Schema *pSchema;
|
||||
HashElem *k;
|
||||
int iDb = pOp->p3;
|
||||
int iRoot = pOp->p2;
|
||||
sqlite3 *db = pVTab->db;
|
||||
pSchema = db->aDb[iDb].pSchema;
|
||||
pCur->zSchema = db->aDb[iDb].zDbSName;
|
||||
for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
|
||||
Table *pTab = (Table*)sqliteHashData(k);
|
||||
if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
|
||||
pCur->zName = pTab->zName;
|
||||
pCur->zType = "table";
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( pCur->zName==0 ){
|
||||
for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
|
||||
Index *pIdx = (Index*)sqliteHashData(k);
|
||||
if( pIdx->tnum==iRoot ){
|
||||
pCur->zName = pIdx->zName;
|
||||
pCur->zType = "index";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
i += 10;
|
||||
}
|
||||
}
|
||||
switch( i ){
|
||||
case 0: /* addr */
|
||||
sqlite3_result_int(ctx, pCur->iAddr);
|
||||
@ -190,7 +251,6 @@ static int bytecodevtabColumn(
|
||||
break;
|
||||
case 5: /* p4 */
|
||||
case 7: /* comment */
|
||||
pVTab = (bytecodevtab*)cur->pVtab;
|
||||
if( pCur->zP4==0 ){
|
||||
pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
|
||||
}
|
||||
@ -219,6 +279,18 @@ static int bytecodevtabColumn(
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 10: /* tables_used.type */
|
||||
sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
|
||||
break;
|
||||
case 11: /* tables_used.schema */
|
||||
sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
|
||||
break;
|
||||
case 12: /* tables_used.name */
|
||||
sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
|
||||
break;
|
||||
case 13: /* tables_used.wr */
|
||||
sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
|
||||
break;
|
||||
}
|
||||
return SQLITE_OK;
|
||||
}
|
||||
@ -266,7 +338,8 @@ static int bytecodevtabFilter(
|
||||
}
|
||||
if( pCur->pStmt==0 ){
|
||||
pVTab->base.zErrMsg = sqlite3_mprintf(
|
||||
"argument to bytecode() is not a valid SQL statement"
|
||||
"argument to %s() is not a valid SQL statement",
|
||||
pVTab->bTablesUsed ? "tables_used" : "bytecode"
|
||||
);
|
||||
rc = SQLITE_ERROR;
|
||||
}else{
|
||||
@ -287,17 +360,19 @@ static int bytecodevtabBestIndex(
|
||||
int i;
|
||||
int rc = SQLITE_CONSTRAINT;
|
||||
struct sqlite3_index_constraint *p;
|
||||
bytecodevtab *pVTab = (bytecodevtab*)tab;
|
||||
int iBaseCol = pVTab->bTablesUsed ? 4 : 8;
|
||||
pIdxInfo->estimatedCost = (double)100;
|
||||
pIdxInfo->estimatedRows = 100;
|
||||
pIdxInfo->idxNum = 0;
|
||||
for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
|
||||
if( p->usable==0 ) continue;
|
||||
if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==9 ){
|
||||
if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
|
||||
rc = SQLITE_OK;
|
||||
pIdxInfo->aConstraintUsage[i].omit = 1;
|
||||
pIdxInfo->aConstraintUsage[i].argvIndex = 1;
|
||||
}
|
||||
if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==8 ){
|
||||
if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
|
||||
pIdxInfo->aConstraintUsage[i].omit = 1;
|
||||
pIdxInfo->idxNum = 1;
|
||||
}
|
||||
@ -340,6 +415,9 @@ static sqlite3_module bytecodevtabModule = {
|
||||
int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
|
||||
int rc;
|
||||
rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
|
||||
if( rc==SQLITE_OK ){
|
||||
rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
#endif /* SQLITE_ENABLE_BYTECODE_VTAB */
|
||||
|
Loading…
Reference in New Issue
Block a user