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 implements virtual-tables for examining the bytecode content
14 ** of a prepared statement.
16 #include "sqliteInt.h"
17 #if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20 /* An instance of the bytecode() table-valued function.
22 typedef struct bytecodevtab bytecodevtab
;
24 sqlite3_vtab base
; /* Base class - must be first */
25 sqlite3
*db
; /* Database connection */
26 int bTablesUsed
; /* 2 for tables_used(). 0 for bytecode(). */
29 /* A cursor for scanning through the bytecode
31 typedef struct bytecodevtab_cursor bytecodevtab_cursor
;
32 struct bytecodevtab_cursor
{
33 sqlite3_vtab_cursor base
; /* Base class - must be first */
34 sqlite3_stmt
*pStmt
; /* The statement whose bytecode is displayed */
35 int iRowid
; /* The rowid of the output table */
36 int iAddr
; /* Address */
37 int needFinalize
; /* Cursors owns pStmt and must finalize it */
38 int showSubprograms
; /* Provide a listing of subprograms */
39 Op
*aOp
; /* Operand array */
40 char *zP4
; /* Rendered P4 value */
41 const char *zType
; /* tables_used.type */
42 const char *zSchema
; /* tables_used.schema */
43 const char *zName
; /* tables_used.name */
44 Mem sub
; /* Subprograms */
48 ** Create a new bytecode() table-valued function.
50 static int bytecodevtabConnect(
53 int argc
, const char *const*argv
,
54 sqlite3_vtab
**ppVtab
,
59 int isTabUsed
= pAux
!=0;
60 const char *azSchema
[2] = {
61 /* bytecode() schema */
75 /* Tables_used() schema */
86 rc
= sqlite3_declare_vtab(db
, azSchema
[isTabUsed
]);
88 pNew
= sqlite3_malloc( sizeof(*pNew
) );
89 *ppVtab
= (sqlite3_vtab
*)pNew
;
90 if( pNew
==0 ) return SQLITE_NOMEM
;
91 memset(pNew
, 0, sizeof(*pNew
));
93 pNew
->bTablesUsed
= isTabUsed
*2;
99 ** This method is the destructor for bytecodevtab objects.
101 static int bytecodevtabDisconnect(sqlite3_vtab
*pVtab
){
102 bytecodevtab
*p
= (bytecodevtab
*)pVtab
;
108 ** Constructor for a new bytecodevtab_cursor object.
110 static int bytecodevtabOpen(sqlite3_vtab
*p
, sqlite3_vtab_cursor
**ppCursor
){
111 bytecodevtab
*pVTab
= (bytecodevtab
*)p
;
112 bytecodevtab_cursor
*pCur
;
113 pCur
= sqlite3_malloc( sizeof(*pCur
) );
114 if( pCur
==0 ) return SQLITE_NOMEM
;
115 memset(pCur
, 0, sizeof(*pCur
));
116 sqlite3VdbeMemInit(&pCur
->sub
, pVTab
->db
, 1);
117 *ppCursor
= &pCur
->base
;
122 ** Clear all internal content from a bytecodevtab cursor.
124 static void bytecodevtabCursorClear(bytecodevtab_cursor
*pCur
){
125 sqlite3_free(pCur
->zP4
);
127 sqlite3VdbeMemRelease(&pCur
->sub
);
128 sqlite3VdbeMemSetNull(&pCur
->sub
);
129 if( pCur
->needFinalize
){
130 sqlite3_finalize(pCur
->pStmt
);
133 pCur
->needFinalize
= 0;
140 ** Destructor for a bytecodevtab_cursor.
142 static int bytecodevtabClose(sqlite3_vtab_cursor
*cur
){
143 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)cur
;
144 bytecodevtabCursorClear(pCur
);
151 ** Advance a bytecodevtab_cursor to its next row of output.
153 static int bytecodevtabNext(sqlite3_vtab_cursor
*cur
){
154 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)cur
;
155 bytecodevtab
*pTab
= (bytecodevtab
*)cur
->pVtab
;
158 sqlite3_free(pCur
->zP4
);
166 rc
= sqlite3VdbeNextOpcode(
168 pCur
->showSubprograms
? &pCur
->sub
: 0,
174 sqlite3VdbeMemSetNull(&pCur
->sub
);
181 ** Return TRUE if the cursor has been moved off of the last
184 static int bytecodevtabEof(sqlite3_vtab_cursor
*cur
){
185 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)cur
;
190 ** Return values of columns for the row at which the bytecodevtab_cursor
191 ** is currently pointing.
193 static int bytecodevtabColumn(
194 sqlite3_vtab_cursor
*cur
, /* The cursor */
195 sqlite3_context
*ctx
, /* First argument to sqlite3_result_...() */
196 int i
/* Which column to return */
198 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)cur
;
199 bytecodevtab
*pVTab
= (bytecodevtab
*)cur
->pVtab
;
200 Op
*pOp
= pCur
->aOp
+ pCur
->iAddr
;
201 if( pVTab
->bTablesUsed
){
205 if( i
<=2 && pCur
->zType
==0 ){
209 Pgno iRoot
= (Pgno
)pOp
->p2
;
210 sqlite3
*db
= pVTab
->db
;
211 pSchema
= db
->aDb
[iDb
].pSchema
;
212 pCur
->zSchema
= db
->aDb
[iDb
].zDbSName
;
213 for(k
=sqliteHashFirst(&pSchema
->tblHash
); k
; k
=sqliteHashNext(k
)){
214 Table
*pTab
= (Table
*)sqliteHashData(k
);
215 if( !IsVirtual(pTab
) && pTab
->tnum
==iRoot
){
216 pCur
->zName
= pTab
->zName
;
217 pCur
->zType
= "table";
221 if( pCur
->zName
==0 ){
222 for(k
=sqliteHashFirst(&pSchema
->idxHash
); k
; k
=sqliteHashNext(k
)){
223 Index
*pIdx
= (Index
*)sqliteHashData(k
);
224 if( pIdx
->tnum
==iRoot
){
225 pCur
->zName
= pIdx
->zName
;
226 pCur
->zType
= "index";
236 sqlite3_result_int(ctx
, pCur
->iAddr
);
239 sqlite3_result_text(ctx
, (char*)sqlite3OpcodeName(pOp
->opcode
),
243 sqlite3_result_int(ctx
, pOp
->p1
);
246 sqlite3_result_int(ctx
, pOp
->p2
);
249 sqlite3_result_int(ctx
, pOp
->p3
);
252 case 7: /* comment */
254 pCur
->zP4
= sqlite3VdbeDisplayP4(pVTab
->db
, pOp
);
257 sqlite3_result_text(ctx
, pCur
->zP4
, -1, SQLITE_STATIC
);
259 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
260 char *zCom
= sqlite3VdbeDisplayComment(pVTab
->db
, pOp
, pCur
->zP4
);
261 sqlite3_result_text(ctx
, zCom
, -1, sqlite3_free
);
266 sqlite3_result_int(ctx
, pOp
->p5
);
268 case 8: { /* subprog */
270 assert( aOp
[0].opcode
==OP_Init
);
271 assert( aOp
[0].p4
.z
==0 || strncmp(aOp
[0].p4
.z
,"-" "- ",3)==0 );
272 if( pCur
->iRowid
==pCur
->iAddr
+1 ){
273 break; /* Result is NULL for the main program */
274 }else if( aOp
[0].p4
.z
!=0 ){
275 sqlite3_result_text(ctx
, aOp
[0].p4
.z
+3, -1, SQLITE_STATIC
);
277 sqlite3_result_text(ctx
, "(FK)", 4, SQLITE_STATIC
);
281 case 10: /* tables_used.type */
282 sqlite3_result_text(ctx
, pCur
->zType
, -1, SQLITE_STATIC
);
284 case 11: /* tables_used.schema */
285 sqlite3_result_text(ctx
, pCur
->zSchema
, -1, SQLITE_STATIC
);
287 case 12: /* tables_used.name */
288 sqlite3_result_text(ctx
, pCur
->zName
, -1, SQLITE_STATIC
);
290 case 13: /* tables_used.wr */
291 sqlite3_result_int(ctx
, pOp
->opcode
==OP_OpenWrite
);
298 ** Return the rowid for the current row. In this implementation, the
299 ** rowid is the same as the output value.
301 static int bytecodevtabRowid(sqlite3_vtab_cursor
*cur
, sqlite_int64
*pRowid
){
302 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)cur
;
303 *pRowid
= pCur
->iRowid
;
308 ** Initialize a cursor.
310 ** idxNum==0 means show all subprograms
311 ** idxNum==1 means show only the main bytecode and omit subprograms.
313 static int bytecodevtabFilter(
314 sqlite3_vtab_cursor
*pVtabCursor
,
315 int idxNum
, const char *idxStr
,
316 int argc
, sqlite3_value
**argv
318 bytecodevtab_cursor
*pCur
= (bytecodevtab_cursor
*)pVtabCursor
;
319 bytecodevtab
*pVTab
= (bytecodevtab
*)pVtabCursor
->pVtab
;
322 bytecodevtabCursorClear(pCur
);
325 pCur
->showSubprograms
= idxNum
==0;
327 if( sqlite3_value_type(argv
[0])==SQLITE_TEXT
){
328 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
332 rc
= sqlite3_prepare_v2(pVTab
->db
, zSql
, -1, &pCur
->pStmt
, 0);
333 pCur
->needFinalize
= 1;
336 pCur
->pStmt
= (sqlite3_stmt
*)sqlite3_value_pointer(argv
[0],"stmt-pointer");
338 if( pCur
->pStmt
==0 ){
339 pVTab
->base
.zErrMsg
= sqlite3_mprintf(
340 "argument to %s() is not a valid SQL statement",
341 pVTab
->bTablesUsed
? "tables_used" : "bytecode"
345 bytecodevtabNext(pVtabCursor
);
351 ** We must have a single stmt=? constraint that will be passed through
352 ** into the xFilter method. If there is no valid stmt=? constraint,
353 ** then return an SQLITE_CONSTRAINT error.
355 static int bytecodevtabBestIndex(
357 sqlite3_index_info
*pIdxInfo
360 int rc
= SQLITE_CONSTRAINT
;
361 struct sqlite3_index_constraint
*p
;
362 bytecodevtab
*pVTab
= (bytecodevtab
*)tab
;
363 int iBaseCol
= pVTab
->bTablesUsed
? 4 : 8;
364 pIdxInfo
->estimatedCost
= (double)100;
365 pIdxInfo
->estimatedRows
= 100;
366 pIdxInfo
->idxNum
= 0;
367 for(i
=0, p
=pIdxInfo
->aConstraint
; i
<pIdxInfo
->nConstraint
; i
++, p
++){
368 if( p
->usable
==0 ) continue;
369 if( p
->op
==SQLITE_INDEX_CONSTRAINT_EQ
&& p
->iColumn
==iBaseCol
+1 ){
371 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
372 pIdxInfo
->aConstraintUsage
[i
].argvIndex
= 1;
374 if( p
->op
==SQLITE_INDEX_CONSTRAINT_ISNULL
&& p
->iColumn
==iBaseCol
){
375 pIdxInfo
->aConstraintUsage
[i
].omit
= 1;
376 pIdxInfo
->idxNum
= 1;
383 ** This following structure defines all the methods for the
386 static sqlite3_module bytecodevtabModule
= {
389 /* xConnect */ bytecodevtabConnect
,
390 /* xBestIndex */ bytecodevtabBestIndex
,
391 /* xDisconnect */ bytecodevtabDisconnect
,
393 /* xOpen */ bytecodevtabOpen
,
394 /* xClose */ bytecodevtabClose
,
395 /* xFilter */ bytecodevtabFilter
,
396 /* xNext */ bytecodevtabNext
,
397 /* xEof */ bytecodevtabEof
,
398 /* xColumn */ bytecodevtabColumn
,
399 /* xRowid */ bytecodevtabRowid
,
414 int sqlite3VdbeBytecodeVtabInit(sqlite3
*db
){
416 rc
= sqlite3_create_module(db
, "bytecode", &bytecodevtabModule
, 0);
418 rc
= sqlite3_create_module(db
, "tables_used", &bytecodevtabModule
, &db
);
422 #elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
423 int sqlite3VdbeBytecodeVtabInit(sqlite3
*db
){ return SQLITE_OK
; }
424 #endif /* SQLITE_ENABLE_BYTECODE_VTAB */