2017-05-31 19:09:04 +03:00
|
|
|
/*
|
|
|
|
** 2017-05-31
|
|
|
|
**
|
|
|
|
** The author disclaims copyright to this source code. In place of
|
|
|
|
** a legal notice, here is a blessing:
|
|
|
|
**
|
|
|
|
** May you do good and not evil.
|
|
|
|
** May you find forgiveness for yourself and forgive others.
|
|
|
|
** May you share freely, never taking more than you give.
|
|
|
|
**
|
|
|
|
*************************************************************************
|
|
|
|
**
|
|
|
|
** This file demonstrates an eponymous virtual table that returns information
|
|
|
|
** about all prepared statements for the database connection.
|
|
|
|
**
|
|
|
|
** Usage example:
|
|
|
|
**
|
2017-06-29 17:33:51 +03:00
|
|
|
** .load ./stmt
|
2017-05-31 19:09:04 +03:00
|
|
|
** .mode line
|
|
|
|
** .header on
|
2017-06-29 17:33:51 +03:00
|
|
|
** SELECT * FROM stmt;
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
|
2017-06-28 18:47:29 +03:00
|
|
|
#if !defined(SQLITEINT_H)
|
2017-05-31 19:09:04 +03:00
|
|
|
#include "sqlite3ext.h"
|
2017-06-28 18:47:29 +03:00
|
|
|
#endif
|
2017-05-31 19:09:04 +03:00
|
|
|
SQLITE_EXTENSION_INIT1
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
|
|
|
|
2017-06-29 17:33:51 +03:00
|
|
|
/* stmt_vtab is a subclass of sqlite3_vtab which will
|
|
|
|
** serve as the underlying representation of a stmt virtual table
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
typedef struct stmt_vtab stmt_vtab;
|
|
|
|
struct stmt_vtab {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_vtab base; /* Base class - must be first */
|
2017-06-29 17:33:51 +03:00
|
|
|
sqlite3 *db; /* Database connection for this stmt vtab */
|
2017-05-31 19:09:04 +03:00
|
|
|
};
|
|
|
|
|
2017-06-29 17:33:51 +03:00
|
|
|
/* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
|
2017-05-31 19:09:04 +03:00
|
|
|
** serve as the underlying representation of a cursor that scans
|
|
|
|
** over rows of the result
|
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
typedef struct stmt_cursor stmt_cursor;
|
|
|
|
struct stmt_cursor {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_vtab_cursor base; /* Base class - must be first */
|
|
|
|
sqlite3 *db; /* Database connection for this cursor */
|
|
|
|
sqlite3_stmt *pStmt; /* Statement cursor is currently pointing at */
|
|
|
|
sqlite3_int64 iRowid; /* The rowid */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** The stmtConnect() method is invoked to create a new
|
2017-07-10 23:39:59 +03:00
|
|
|
** stmt_vtab that describes the stmt virtual table.
|
2017-05-31 19:09:04 +03:00
|
|
|
**
|
2017-06-29 17:33:51 +03:00
|
|
|
** Think of this routine as the constructor for stmt_vtab objects.
|
2017-05-31 19:09:04 +03:00
|
|
|
**
|
|
|
|
** All this routine needs to do is:
|
|
|
|
**
|
2017-06-29 17:33:51 +03:00
|
|
|
** (1) Allocate the stmt_vtab object and initialize all fields.
|
2017-05-31 19:09:04 +03:00
|
|
|
**
|
|
|
|
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
|
2017-07-10 23:39:59 +03:00
|
|
|
** result set of queries against stmt will look like.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtConnect(
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3 *db,
|
|
|
|
void *pAux,
|
|
|
|
int argc, const char *const*argv,
|
|
|
|
sqlite3_vtab **ppVtab,
|
|
|
|
char **pzErr
|
|
|
|
){
|
2017-06-29 17:33:51 +03:00
|
|
|
stmt_vtab *pNew;
|
2017-05-31 19:09:04 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Column numbers */
|
2017-07-14 18:14:21 +03:00
|
|
|
#define STMT_COLUMN_SQL 0 /* SQL for the statement */
|
|
|
|
#define STMT_COLUMN_NCOL 1 /* Number of result columns */
|
|
|
|
#define STMT_COLUMN_RO 2 /* True if read-only */
|
|
|
|
#define STMT_COLUMN_BUSY 3 /* True if currently busy */
|
|
|
|
#define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
|
|
|
|
#define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
|
|
|
|
#define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
|
|
|
|
#define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
|
|
|
|
#define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
|
|
|
|
#define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
|
|
|
|
#define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
|
2017-05-31 19:09:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
rc = sqlite3_declare_vtab(db,
|
2017-07-14 18:14:21 +03:00
|
|
|
"CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
|
2017-06-29 15:49:18 +03:00
|
|
|
"reprep,run,mem)");
|
2017-05-31 19:09:04 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** This method is the destructor for stmt_cursor objects.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtDisconnect(sqlite3_vtab *pVtab){
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_free(pVtab);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** Constructor for a new stmt_cursor object.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
|
|
|
|
stmt_cursor *pCur;
|
2017-05-31 19:09:04 +03:00
|
|
|
pCur = sqlite3_malloc( sizeof(*pCur) );
|
|
|
|
if( pCur==0 ) return SQLITE_NOMEM;
|
|
|
|
memset(pCur, 0, sizeof(*pCur));
|
2017-06-29 17:33:51 +03:00
|
|
|
pCur->db = ((stmt_vtab*)p)->db;
|
2017-05-31 19:09:04 +03:00
|
|
|
*ppCursor = &pCur->base;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** Destructor for a stmt_cursor.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtClose(sqlite3_vtab_cursor *cur){
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_free(cur);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** Advance a stmt_cursor to its next row of output.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtNext(sqlite3_vtab_cursor *cur){
|
|
|
|
stmt_cursor *pCur = (stmt_cursor*)cur;
|
2017-05-31 19:09:04 +03:00
|
|
|
pCur->iRowid++;
|
|
|
|
pCur->pStmt = sqlite3_next_stmt(pCur->db, pCur->pStmt);
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** Return values of columns for the row at which the stmt_cursor
|
2017-05-31 19:09:04 +03:00
|
|
|
** is currently pointing.
|
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtColumn(
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_vtab_cursor *cur, /* The cursor */
|
|
|
|
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
|
|
|
|
int i /* Which column to return */
|
|
|
|
){
|
2017-06-29 17:33:51 +03:00
|
|
|
stmt_cursor *pCur = (stmt_cursor*)cur;
|
2017-05-31 19:09:04 +03:00
|
|
|
switch( i ){
|
2017-06-29 17:33:51 +03:00
|
|
|
case STMT_COLUMN_SQL: {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_result_text(ctx, sqlite3_sql(pCur->pStmt), -1, SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 17:33:51 +03:00
|
|
|
case STMT_COLUMN_NCOL: {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_result_int(ctx, sqlite3_column_count(pCur->pStmt));
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 17:33:51 +03:00
|
|
|
case STMT_COLUMN_RO: {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_result_int(ctx, sqlite3_stmt_readonly(pCur->pStmt));
|
|
|
|
break;
|
|
|
|
}
|
2017-06-29 17:33:51 +03:00
|
|
|
case STMT_COLUMN_BUSY: {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_result_int(ctx, sqlite3_stmt_busy(pCur->pStmt));
|
|
|
|
break;
|
|
|
|
}
|
2020-05-04 22:04:23 +03:00
|
|
|
default: {
|
|
|
|
assert( i==STMT_COLUMN_MEM );
|
2017-06-29 15:49:18 +03:00
|
|
|
i = SQLITE_STMTSTATUS_MEMUSED +
|
2017-06-29 17:33:51 +03:00
|
|
|
STMT_COLUMN_NSCAN - SQLITE_STMTSTATUS_FULLSCAN_STEP;
|
2017-06-29 15:49:18 +03:00
|
|
|
/* Fall thru */
|
|
|
|
}
|
2017-06-29 17:33:51 +03:00
|
|
|
case STMT_COLUMN_NSCAN:
|
|
|
|
case STMT_COLUMN_NSORT:
|
|
|
|
case STMT_COLUMN_NAIDX:
|
|
|
|
case STMT_COLUMN_NSTEP:
|
|
|
|
case STMT_COLUMN_REPREP:
|
|
|
|
case STMT_COLUMN_RUN: {
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_result_int(ctx, sqlite3_stmt_status(pCur->pStmt,
|
2017-06-29 17:33:51 +03:00
|
|
|
i-STMT_COLUMN_NSCAN+SQLITE_STMTSTATUS_FULLSCAN_STEP, 0));
|
2017-05-31 19:09:04 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return the rowid for the current row. In this implementation, the
|
|
|
|
** rowid is the same as the output value.
|
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
|
|
|
|
stmt_cursor *pCur = (stmt_cursor*)cur;
|
2017-05-31 19:09:04 +03:00
|
|
|
*pRowid = pCur->iRowid;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Return TRUE if the cursor has been moved off of the last
|
|
|
|
** row of output.
|
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtEof(sqlite3_vtab_cursor *cur){
|
|
|
|
stmt_cursor *pCur = (stmt_cursor*)cur;
|
2017-05-31 19:09:04 +03:00
|
|
|
return pCur->pStmt==0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2017-06-29 17:33:51 +03:00
|
|
|
** This method is called to "rewind" the stmt_cursor object back
|
2017-05-31 19:09:04 +03:00
|
|
|
** to the first row of output. This method is always called at least
|
2017-06-29 17:33:51 +03:00
|
|
|
** once prior to any call to stmtColumn() or stmtRowid() or
|
|
|
|
** stmtEof().
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtFilter(
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_vtab_cursor *pVtabCursor,
|
|
|
|
int idxNum, const char *idxStr,
|
|
|
|
int argc, sqlite3_value **argv
|
|
|
|
){
|
2017-06-29 17:33:51 +03:00
|
|
|
stmt_cursor *pCur = (stmt_cursor *)pVtabCursor;
|
2017-05-31 19:09:04 +03:00
|
|
|
pCur->pStmt = 0;
|
|
|
|
pCur->iRowid = 0;
|
2017-06-29 17:33:51 +03:00
|
|
|
return stmtNext(pVtabCursor);
|
2017-05-31 19:09:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** SQLite will invoke this method one or more times while planning a query
|
2017-07-10 23:39:59 +03:00
|
|
|
** that uses the stmt virtual table. This routine needs to create
|
2017-05-31 19:09:04 +03:00
|
|
|
** a query plan for each invocation and compute an estimated cost for that
|
|
|
|
** plan.
|
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static int stmtBestIndex(
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3_vtab *tab,
|
|
|
|
sqlite3_index_info *pIdxInfo
|
|
|
|
){
|
|
|
|
pIdxInfo->estimatedCost = (double)500;
|
|
|
|
pIdxInfo->estimatedRows = 500;
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** This following structure defines all the methods for the
|
2017-07-10 23:39:59 +03:00
|
|
|
** stmt virtual table.
|
2017-05-31 19:09:04 +03:00
|
|
|
*/
|
2017-06-29 17:33:51 +03:00
|
|
|
static sqlite3_module stmtModule = {
|
2017-05-31 19:09:04 +03:00
|
|
|
0, /* iVersion */
|
|
|
|
0, /* xCreate */
|
2017-06-29 17:33:51 +03:00
|
|
|
stmtConnect, /* xConnect */
|
|
|
|
stmtBestIndex, /* xBestIndex */
|
|
|
|
stmtDisconnect, /* xDisconnect */
|
2017-05-31 19:09:04 +03:00
|
|
|
0, /* xDestroy */
|
2017-06-29 17:33:51 +03:00
|
|
|
stmtOpen, /* xOpen - open a cursor */
|
|
|
|
stmtClose, /* xClose - close a cursor */
|
|
|
|
stmtFilter, /* xFilter - configure scan constraints */
|
|
|
|
stmtNext, /* xNext - advance a cursor */
|
|
|
|
stmtEof, /* xEof - check for end of scan */
|
|
|
|
stmtColumn, /* xColumn - read data */
|
|
|
|
stmtRowid, /* xRowid - read data */
|
2017-05-31 19:09:04 +03:00
|
|
|
0, /* xUpdate */
|
|
|
|
0, /* xBegin */
|
|
|
|
0, /* xSync */
|
|
|
|
0, /* xCommit */
|
|
|
|
0, /* xRollback */
|
|
|
|
0, /* xFindMethod */
|
|
|
|
0, /* xRename */
|
2017-07-12 21:05:54 +03:00
|
|
|
0, /* xSavepoint */
|
|
|
|
0, /* xRelease */
|
|
|
|
0, /* xRollbackTo */
|
2018-11-06 02:01:45 +03:00
|
|
|
0, /* xShadowName */
|
2017-05-31 19:09:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* SQLITE_OMIT_VIRTUALTABLE */
|
|
|
|
|
2017-06-29 17:33:51 +03:00
|
|
|
int sqlite3StmtVtabInit(sqlite3 *db){
|
2017-06-28 18:47:29 +03:00
|
|
|
int rc = SQLITE_OK;
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2017-07-14 18:14:21 +03:00
|
|
|
rc = sqlite3_create_module(db, "sqlite_stmt", &stmtModule, 0);
|
2017-06-28 18:47:29 +03:00
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef SQLITE_CORE
|
2017-05-31 19:09:04 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
__declspec(dllexport)
|
|
|
|
#endif
|
2017-06-29 17:33:51 +03:00
|
|
|
int sqlite3_stmt_init(
|
2017-05-31 19:09:04 +03:00
|
|
|
sqlite3 *db,
|
|
|
|
char **pzErrMsg,
|
|
|
|
const sqlite3_api_routines *pApi
|
|
|
|
){
|
|
|
|
int rc = SQLITE_OK;
|
|
|
|
SQLITE_EXTENSION_INIT2(pApi);
|
|
|
|
#ifndef SQLITE_OMIT_VIRTUALTABLE
|
2017-06-29 17:33:51 +03:00
|
|
|
rc = sqlite3StmtVtabInit(db);
|
2017-05-31 19:09:04 +03:00
|
|
|
#endif
|
|
|
|
return rc;
|
|
|
|
}
|
2017-06-28 18:47:29 +03:00
|
|
|
#endif /* SQLITE_CORE */
|
2017-06-29 17:33:51 +03:00
|
|
|
#endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */
|