|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if !defined(SQLITEINT_H) |
|
|
#include "sqlite3ext.h" |
|
|
#endif |
|
|
SQLITE_EXTENSION_INIT1 |
|
|
#include <assert.h> |
|
|
#include <string.h> |
|
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct explain_vtab explain_vtab; |
|
|
struct explain_vtab { |
|
|
sqlite3_vtab base; |
|
|
sqlite3 *db; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct explain_cursor explain_cursor; |
|
|
struct explain_cursor { |
|
|
sqlite3_vtab_cursor base; |
|
|
sqlite3 *db; |
|
|
char *zSql; |
|
|
sqlite3_stmt *pExplain; |
|
|
int rc; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainConnect( |
|
|
sqlite3 *db, |
|
|
void *pAux, |
|
|
int argc, const char *const*argv, |
|
|
sqlite3_vtab **ppVtab, |
|
|
char **pzErr |
|
|
){ |
|
|
explain_vtab *pNew; |
|
|
int rc; |
|
|
|
|
|
|
|
|
#define EXPLN_COLUMN_ADDR 0 |
|
|
#define EXPLN_COLUMN_OPCODE 1 |
|
|
#define EXPLN_COLUMN_P1 2 |
|
|
#define EXPLN_COLUMN_P2 3 |
|
|
#define EXPLN_COLUMN_P3 4 |
|
|
#define EXPLN_COLUMN_P4 5 |
|
|
#define EXPLN_COLUMN_P5 6 |
|
|
#define EXPLN_COLUMN_COMMENT 7 |
|
|
#define EXPLN_COLUMN_SQL 8 |
|
|
|
|
|
|
|
|
rc = sqlite3_declare_vtab(db, |
|
|
"CREATE TABLE x(addr,opcode,p1,p2,p3,p4,p5,comment,sql HIDDEN)"); |
|
|
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; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainDisconnect(sqlite3_vtab *pVtab){ |
|
|
sqlite3_free(pVtab); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){ |
|
|
explain_cursor *pCur; |
|
|
pCur = sqlite3_malloc( sizeof(*pCur) ); |
|
|
if( pCur==0 ) return SQLITE_NOMEM; |
|
|
memset(pCur, 0, sizeof(*pCur)); |
|
|
pCur->db = ((explain_vtab*)p)->db; |
|
|
*ppCursor = &pCur->base; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainClose(sqlite3_vtab_cursor *cur){ |
|
|
explain_cursor *pCur = (explain_cursor*)cur; |
|
|
sqlite3_finalize(pCur->pExplain); |
|
|
sqlite3_free(pCur->zSql); |
|
|
sqlite3_free(pCur); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainNext(sqlite3_vtab_cursor *cur){ |
|
|
explain_cursor *pCur = (explain_cursor*)cur; |
|
|
pCur->rc = sqlite3_step(pCur->pExplain); |
|
|
if( pCur->rc!=SQLITE_DONE && pCur->rc!=SQLITE_ROW ) return pCur->rc; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainColumn( |
|
|
sqlite3_vtab_cursor *cur, |
|
|
sqlite3_context *ctx, |
|
|
int i |
|
|
){ |
|
|
explain_cursor *pCur = (explain_cursor*)cur; |
|
|
if( i==EXPLN_COLUMN_SQL ){ |
|
|
sqlite3_result_text(ctx, pCur->zSql, -1, SQLITE_TRANSIENT); |
|
|
}else{ |
|
|
sqlite3_result_value(ctx, sqlite3_column_value(pCur->pExplain, i)); |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){ |
|
|
explain_cursor *pCur = (explain_cursor*)cur; |
|
|
*pRowid = sqlite3_column_int64(pCur->pExplain, 0); |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainEof(sqlite3_vtab_cursor *cur){ |
|
|
explain_cursor *pCur = (explain_cursor*)cur; |
|
|
return pCur->rc!=SQLITE_ROW; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainFilter( |
|
|
sqlite3_vtab_cursor *pVtabCursor, |
|
|
int idxNum, const char *idxStr, |
|
|
int argc, sqlite3_value **argv |
|
|
){ |
|
|
explain_cursor *pCur = (explain_cursor *)pVtabCursor; |
|
|
char *zSql = 0; |
|
|
int rc; |
|
|
sqlite3_finalize(pCur->pExplain); |
|
|
pCur->pExplain = 0; |
|
|
if( sqlite3_value_type(argv[0])!=SQLITE_TEXT ){ |
|
|
pCur->rc = SQLITE_DONE; |
|
|
return SQLITE_OK; |
|
|
} |
|
|
sqlite3_free(pCur->zSql); |
|
|
pCur->zSql = sqlite3_mprintf("%s", sqlite3_value_text(argv[0])); |
|
|
if( pCur->zSql ){ |
|
|
zSql = sqlite3_mprintf("EXPLAIN %s", pCur->zSql); |
|
|
} |
|
|
if( zSql==0 ){ |
|
|
rc = SQLITE_NOMEM; |
|
|
}else{ |
|
|
rc = sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pExplain, 0); |
|
|
sqlite3_free(zSql); |
|
|
} |
|
|
if( rc ){ |
|
|
sqlite3_finalize(pCur->pExplain); |
|
|
pCur->pExplain = 0; |
|
|
sqlite3_free(pCur->zSql); |
|
|
pCur->zSql = 0; |
|
|
}else{ |
|
|
pCur->rc = sqlite3_step(pCur->pExplain); |
|
|
rc = (pCur->rc==SQLITE_DONE || pCur->rc==SQLITE_ROW) ? SQLITE_OK : pCur->rc; |
|
|
} |
|
|
return rc; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int explainBestIndex( |
|
|
sqlite3_vtab *tab, |
|
|
sqlite3_index_info *pIdxInfo |
|
|
){ |
|
|
int i; |
|
|
int idx = -1; |
|
|
int unusable = 0; |
|
|
|
|
|
pIdxInfo->estimatedRows = 500; |
|
|
for(i=0; i<pIdxInfo->nConstraint; i++){ |
|
|
struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[i]; |
|
|
if( p->iColumn!=EXPLN_COLUMN_SQL ) continue; |
|
|
if( !p->usable ){ |
|
|
unusable = 1; |
|
|
}else if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){ |
|
|
idx = i; |
|
|
} |
|
|
} |
|
|
if( idx>=0 ){ |
|
|
|
|
|
pIdxInfo->estimatedCost = 10.0; |
|
|
pIdxInfo->idxNum = 1; |
|
|
pIdxInfo->aConstraintUsage[idx].argvIndex = 1; |
|
|
pIdxInfo->aConstraintUsage[idx].omit = 1; |
|
|
}else if( unusable ){ |
|
|
|
|
|
|
|
|
return SQLITE_CONSTRAINT; |
|
|
} |
|
|
return SQLITE_OK; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static sqlite3_module explainModule = { |
|
|
0, |
|
|
0, |
|
|
explainConnect, |
|
|
explainBestIndex, |
|
|
explainDisconnect, |
|
|
0, |
|
|
explainOpen, |
|
|
explainClose, |
|
|
explainFilter, |
|
|
explainNext, |
|
|
explainEof, |
|
|
explainColumn, |
|
|
explainRowid, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0, |
|
|
0 |
|
|
}; |
|
|
|
|
|
#endif |
|
|
|
|
|
int sqlite3ExplainVtabInit(sqlite3 *db){ |
|
|
int rc = SQLITE_OK; |
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
rc = sqlite3_create_module(db, "explain", &explainModule, 0); |
|
|
#endif |
|
|
return rc; |
|
|
} |
|
|
|
|
|
#ifdef _WIN32 |
|
|
__declspec(dllexport) |
|
|
#endif |
|
|
int sqlite3_explain_init( |
|
|
sqlite3 *db, |
|
|
char **pzErrMsg, |
|
|
const sqlite3_api_routines *pApi |
|
|
){ |
|
|
int rc = SQLITE_OK; |
|
|
SQLITE_EXTENSION_INIT2(pApi); |
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE |
|
|
rc = sqlite3ExplainVtabInit(db); |
|
|
#endif |
|
|
return rc; |
|
|
} |
|
|
|