#include "sqliteInt.h"
#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
#include "vdbeInt.h"
typedef struct bytecodevtab bytecodevtab;
struct bytecodevtab {
sqlite3_vtab base;
sqlite3 *db;
int bTablesUsed;
};
typedef struct bytecodevtab_cursor bytecodevtab_cursor;
struct bytecodevtab_cursor {
sqlite3_vtab_cursor base;
sqlite3_stmt *pStmt;
int iRowid;
int iAddr;
int needFinalize;
int showSubprograms;
Op *aOp;
char *zP4;
const char *zType;
const char *zSchema;
const char *zName;
Mem sub;
};
static int bytecodevtabConnect(
sqlite3 *db,
void *pAux,
int argc, const char *const*argv,
sqlite3_vtab **ppVtab,
char **pzErr
){
bytecodevtab *pNew;
int rc;
int isTabUsed = pAux!=0;
const char *azSchema[2] = {
"CREATE TABLE x("
"addr INT,"
"opcode TEXT,"
"p1 INT,"
"p2 INT,"
"p3 INT,"
"p4 TEXT,"
"p5 INT,"
"comment TEXT,"
"subprog TEXT,"
"stmt HIDDEN"
");",
"CREATE TABLE x("
"type TEXT,"
"schema TEXT,"
"name TEXT,"
"wr INT,"
"subprog TEXT,"
"stmt HIDDEN"
");"
};
(void)argc;
(void)argv;
(void)pzErr;
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;
}
static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
bytecodevtab *p = (bytecodevtab*)pVtab;
sqlite3_free(p);
return SQLITE_OK;
}
static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
bytecodevtab *pVTab = (bytecodevtab*)p;
bytecodevtab_cursor *pCur;
pCur = sqlite3_malloc( sizeof(*pCur) );
if( pCur==0 ) return SQLITE_NOMEM;
memset(pCur, 0, sizeof(*pCur));
sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
*ppCursor = &pCur->base;
return SQLITE_OK;
}
static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
sqlite3_free(pCur->zP4);
pCur->zP4 = 0;
sqlite3VdbeMemRelease(&pCur->sub);
sqlite3VdbeMemSetNull(&pCur->sub);
if( pCur->needFinalize ){
sqlite3_finalize(pCur->pStmt);
}
pCur->pStmt = 0;
pCur->needFinalize = 0;
pCur->zType = 0;
pCur->zSchema = 0;
pCur->zName = 0;
}
static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
bytecodevtabCursorClear(pCur);
sqlite3_free(pCur);
return SQLITE_OK;
}
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,
pTab->bTablesUsed,
&pCur->iRowid,
&pCur->iAddr,
&pCur->aOp);
if( rc!=SQLITE_OK ){
sqlite3VdbeMemSetNull(&pCur->sub);
pCur->aOp = 0;
}
return SQLITE_OK;
}
static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
return pCur->aOp==0;
}
static int bytecodevtabColumn(
sqlite3_vtab_cursor *cur,
sqlite3_context *ctx,
int i
){
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
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;
Pgno iRoot = (Pgno)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";
}
}
}
}
i += 10;
}
}
switch( i ){
case 0:
sqlite3_result_int(ctx, pCur->iAddr);
break;
case 1:
sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
-1, SQLITE_STATIC);
break;
case 2:
sqlite3_result_int(ctx, pOp->p1);
break;
case 3:
sqlite3_result_int(ctx, pOp->p2);
break;
case 4:
sqlite3_result_int(ctx, pOp->p3);
break;
case 5:
case 7:
if( pCur->zP4==0 ){
pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
}
if( i==5 ){
sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
}else{
#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
#endif
}
break;
case 6:
sqlite3_result_int(ctx, pOp->p5);
break;
case 8: {
Op *aOp = pCur->aOp;
assert( aOp[0].opcode==OP_Init );
assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
if( pCur->iRowid==pCur->iAddr+1 ){
break;
}else if( aOp[0].p4.z!=0 ){
sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
}else{
sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
}
break;
}
case 10:
sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
break;
case 11:
sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
break;
case 12:
sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
break;
case 13:
sqlite3_result_int(ctx, pOp->opcode==OP_OpenWrite);
break;
}
return SQLITE_OK;
}
static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
*pRowid = pCur->iRowid;
return SQLITE_OK;
}
static int bytecodevtabFilter(
sqlite3_vtab_cursor *pVtabCursor,
int idxNum, const char *idxStr,
int argc, sqlite3_value **argv
){
bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
int rc = SQLITE_OK;
(void)idxStr;
bytecodevtabCursorClear(pCur);
pCur->iRowid = 0;
pCur->iAddr = 0;
pCur->showSubprograms = idxNum==0;
assert( argc==1 );
if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
const char *zSql = (const char*)sqlite3_value_text(argv[0]);
if( zSql==0 ){
rc = SQLITE_NOMEM;
}else{
rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
pCur->needFinalize = 1;
}
}else{
pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
}
if( pCur->pStmt==0 ){
pVTab->base.zErrMsg = sqlite3_mprintf(
"argument to %s() is not a valid SQL statement",
pVTab->bTablesUsed ? "tables_used" : "bytecode"
);
rc = SQLITE_ERROR;
}else{
bytecodevtabNext(pVtabCursor);
}
return rc;
}
static int bytecodevtabBestIndex(
sqlite3_vtab *tab,
sqlite3_index_info *pIdxInfo
){
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==iBaseCol+1 ){
rc = SQLITE_OK;
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->aConstraintUsage[i].argvIndex = 1;
}
if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->idxNum = 1;
}
}
return rc;
}
static sqlite3_module bytecodevtabModule = {
0,
0,
bytecodevtabConnect,
bytecodevtabBestIndex,
bytecodevtabDisconnect,
0,
bytecodevtabOpen,
bytecodevtabClose,
bytecodevtabFilter,
bytecodevtabNext,
bytecodevtabEof,
bytecodevtabColumn,
bytecodevtabRowid,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
};
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;
}
#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
#endif