4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file demonstrates an eponymous virtual table that returns information
14 ** about all prepared statements for the database connection.
21 ** SELECT * FROM stmt;
23 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB)
24 #if !defined(SQLITEINT_H)
25 #include "sqlite3ext.h"
27 SQLITE_EXTENSION_INIT1
31 #ifndef SQLITE_OMIT_VIRTUALTABLE
34 #define STMT_NUM_INTEGER_COLUMN 10
35 typedef struct StmtRow StmtRow
;
37 sqlite3_int64 iRowid
; /* Rowid value */
38 char *zSql
; /* column "sql" */
39 int aCol
[STMT_NUM_INTEGER_COLUMN
+1]; /* all other column values */
40 StmtRow
*pNext
; /* Next row to return */
43 /* stmt_vtab is a subclass of sqlite3_vtab which will
44 ** serve as the underlying representation of a stmt virtual table
46 typedef struct stmt_vtab stmt_vtab
;
48 sqlite3_vtab base
; /* Base class - must be first */
49 sqlite3
*db
; /* Database connection for this stmt vtab */
52 /* stmt_cursor is a subclass of sqlite3_vtab_cursor which will
53 ** serve as the underlying representation of a cursor that scans
54 ** over rows of the result
56 typedef struct stmt_cursor stmt_cursor
;
58 sqlite3_vtab_cursor base
; /* Base class - must be first */
59 sqlite3
*db
; /* Database connection for this cursor */
60 StmtRow
*pRow
; /* Current row */
64 ** The stmtConnect() method is invoked to create a new
65 ** stmt_vtab that describes the stmt virtual table.
67 ** Think of this routine as the constructor for stmt_vtab objects.
69 ** All this routine needs to do is:
71 ** (1) Allocate the stmt_vtab object and initialize all fields.
73 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
74 ** result set of queries against stmt will look like.
76 static int stmtConnect(
79 int argc
, const char *const*argv
,
80 sqlite3_vtab
**ppVtab
,
87 #define STMT_COLUMN_SQL 0 /* SQL for the statement */
88 #define STMT_COLUMN_NCOL 1 /* Number of result columns */
89 #define STMT_COLUMN_RO 2 /* True if read-only */
90 #define STMT_COLUMN_BUSY 3 /* True if currently busy */
91 #define STMT_COLUMN_NSCAN 4 /* SQLITE_STMTSTATUS_FULLSCAN_STEP */
92 #define STMT_COLUMN_NSORT 5 /* SQLITE_STMTSTATUS_SORT */
93 #define STMT_COLUMN_NAIDX 6 /* SQLITE_STMTSTATUS_AUTOINDEX */
94 #define STMT_COLUMN_NSTEP 7 /* SQLITE_STMTSTATUS_VM_STEP */
95 #define STMT_COLUMN_REPREP 8 /* SQLITE_STMTSTATUS_REPREPARE */
96 #define STMT_COLUMN_RUN 9 /* SQLITE_STMTSTATUS_RUN */
97 #define STMT_COLUMN_MEM 10 /* SQLITE_STMTSTATUS_MEMUSED */
104 rc
= sqlite3_declare_vtab(db
,
105 "CREATE TABLE x(sql,ncol,ro,busy,nscan,nsort,naidx,nstep,"
108 pNew
= sqlite3_malloc64( sizeof(*pNew
) );
109 *ppVtab
= (sqlite3_vtab
*)pNew
;
110 if( pNew
==0 ) return SQLITE_NOMEM
;
111 memset(pNew
, 0, sizeof(*pNew
));
118 ** This method is the destructor for stmt_cursor objects.
120 static int stmtDisconnect(sqlite3_vtab
*pVtab
){
126 ** Constructor for a new stmt_cursor object.
128 static int stmtOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
130 pCur
= sqlite3_malloc64( sizeof(*pCur
) );
131 if( pCur
==0 ) return SQLITE_NOMEM
;
132 memset(pCur
, 0, sizeof(*pCur
));
133 pCur
->db
= ((stmt_vtab
*)p
)->db
;
134 *ppCursor
= &pCur
->base
;
138 static void stmtCsrReset(stmt_cursor
*pCur
){
141 for(pRow
=pCur
->pRow
; pRow
; pRow
=pNext
){
149 ** Destructor for a stmt_cursor.
151 static int stmtClose(sqlite3_vtab_cursor
*cur
){
152 stmtCsrReset((stmt_cursor
*)cur
);
159 ** Advance a stmt_cursor to its next row of output.
161 static int stmtNext(sqlite3_vtab_cursor
*cur
){
162 stmt_cursor
*pCur
= (stmt_cursor
*)cur
;
163 StmtRow
*pNext
= pCur
->pRow
->pNext
;
164 sqlite3_free(pCur
->pRow
);
170 ** Return values of columns for the row at which the stmt_cursor
171 ** is currently pointing.
173 static int stmtColumn(
174 sqlite3_vtab_cursor
*cur
, /* The cursor */
175 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
176 int i
/* Which column to return */
178 stmt_cursor
*pCur
= (stmt_cursor
*)cur
;
179 StmtRow
*pRow
= pCur
->pRow
;
180 if( i
==STMT_COLUMN_SQL
){
181 sqlite3_result_text(ctx
, pRow
->zSql
, -1, SQLITE_TRANSIENT
);
183 sqlite3_result_int(ctx
, pRow
->aCol
[i
]);
189 ** Return the rowid for the current row. In this implementation, the
190 ** rowid is the same as the output value.
192 static int stmtRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
193 stmt_cursor
*pCur
= (stmt_cursor
*)cur
;
194 *pRowid
= pCur
->pRow
->iRowid
;
199 ** Return TRUE if the cursor has been moved off of the last
202 static int stmtEof(sqlite3_vtab_cursor
*cur
){
203 stmt_cursor
*pCur
= (stmt_cursor
*)cur
;
204 return pCur
->pRow
==0;
208 ** This method is called to "rewind" the stmt_cursor object back
209 ** to the first row of output. This method is always called at least
210 ** once prior to any call to stmtColumn() or stmtRowid() or
213 static int stmtFilter(
214 sqlite3_vtab_cursor
*pVtabCursor
,
215 int idxNum
, const char *idxStr
,
216 int argc
, sqlite3_value
**argv
218 stmt_cursor
*pCur
= (stmt_cursor
*)pVtabCursor
;
220 sqlite3_int64 iRowid
= 1;
229 for(p
=sqlite3_next_stmt(pCur
->db
, 0); p
; p
=sqlite3_next_stmt(pCur
->db
, p
)){
230 const char *zSql
= sqlite3_sql(p
);
231 sqlite3_int64 nSql
= zSql
? strlen(zSql
)+1 : 0;
232 StmtRow
*pNew
= (StmtRow
*)sqlite3_malloc64(sizeof(StmtRow
) + nSql
);
234 if( pNew
==0 ) return SQLITE_NOMEM
;
235 memset(pNew
, 0, sizeof(StmtRow
));
237 pNew
->zSql
= (char*)&pNew
[1];
238 memcpy(pNew
->zSql
, zSql
, nSql
);
240 pNew
->aCol
[STMT_COLUMN_NCOL
] = sqlite3_column_count(p
);
241 pNew
->aCol
[STMT_COLUMN_RO
] = sqlite3_stmt_readonly(p
);
242 pNew
->aCol
[STMT_COLUMN_BUSY
] = sqlite3_stmt_busy(p
);
243 pNew
->aCol
[STMT_COLUMN_NSCAN
] = sqlite3_stmt_status(
244 p
, SQLITE_STMTSTATUS_FULLSCAN_STEP
, 0
246 pNew
->aCol
[STMT_COLUMN_NSORT
] = sqlite3_stmt_status(
247 p
, SQLITE_STMTSTATUS_SORT
, 0
249 pNew
->aCol
[STMT_COLUMN_NAIDX
] = sqlite3_stmt_status(
250 p
, SQLITE_STMTSTATUS_AUTOINDEX
, 0
252 pNew
->aCol
[STMT_COLUMN_NSTEP
] = sqlite3_stmt_status(
253 p
, SQLITE_STMTSTATUS_VM_STEP
, 0
255 pNew
->aCol
[STMT_COLUMN_REPREP
] = sqlite3_stmt_status(
256 p
, SQLITE_STMTSTATUS_REPREPARE
, 0
258 pNew
->aCol
[STMT_COLUMN_RUN
] = sqlite3_stmt_status(
259 p
, SQLITE_STMTSTATUS_RUN
, 0
261 pNew
->aCol
[STMT_COLUMN_MEM
] = sqlite3_stmt_status(
262 p
, SQLITE_STMTSTATUS_MEMUSED
, 0
264 pNew
->iRowid
= iRowid
++;
266 ppRow
= &pNew
->pNext
;
273 ** SQLite will invoke this method one or more times while planning a query
274 ** that uses the stmt virtual table. This routine needs to create
275 ** a query plan for each invocation and compute an estimated cost for that
278 static int stmtBestIndex(
280 sqlite3_index_info
*pIdxInfo
283 pIdxInfo
->estimatedCost
= (double)500;
284 pIdxInfo
->estimatedRows
= 500;
289 ** This following structure defines all the methods for the
290 ** stmt virtual table.
292 static sqlite3_module stmtModule
= {
295 stmtConnect
, /* xConnect */
296 stmtBestIndex
, /* xBestIndex */
297 stmtDisconnect
, /* xDisconnect */
299 stmtOpen
, /* xOpen - open a cursor */
300 stmtClose
, /* xClose - close a cursor */
301 stmtFilter
, /* xFilter - configure scan constraints */
302 stmtNext
, /* xNext - advance a cursor */
303 stmtEof
, /* xEof - check for end of scan */
304 stmtColumn
, /* xColumn - read data */
305 stmtRowid
, /* xRowid - read data */
320 #endif /* SQLITE_OMIT_VIRTUALTABLE */
322 int sqlite3StmtVtabInit(sqlite3
*db
){
324 #ifndef SQLITE_OMIT_VIRTUALTABLE
325 rc
= sqlite3_create_module(db
, "sqlite_stmt", &stmtModule
, 0);
332 __declspec(dllexport
)
334 int sqlite3_stmt_init(
337 const sqlite3_api_routines
*pApi
340 SQLITE_EXTENSION_INIT2(pApi
);
341 #ifndef SQLITE_OMIT_VIRTUALTABLE
342 rc
= sqlite3StmtVtabInit(db
);
346 #endif /* SQLITE_CORE */
347 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_STMTVTAB) */