interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / vdbevtab.c

465 lines · 13045 bytes Raw Blame History
1/*
2** 2020-03-23
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
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.
10**
11*************************************************************************
12**
13** This file implements virtual-tables for examining the bytecode content
14** of a prepared statement.
15*/
16#include "sqliteInt.h"
17#if defined(SQLITE_ENABLE_BYTECODE_VTAB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18#include "vdbeInt.h"
19
20/* An instance of the bytecode() table-valued function.
21*/
22typedef struct bytecodevtab bytecodevtab;
23struct 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(). */
27};
28
29/* A cursor for scanning through the bytecode
30*/
31typedef struct bytecodevtab_cursor bytecodevtab_cursor;
32struct 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 */
45};
46
47/*
48** Create a new bytecode() table-valued function.
49*/
50static int bytecodevtabConnect(
51 sqlite3 *db,
52 void *pAux,
53 int argc, const char *const*argv,
54 sqlite3_vtab **ppVtab,
55 char **pzErr
56){
57 bytecodevtab *pNew;
58 int rc;
59 int isTabUsed = pAux!=0;
60 const char *azSchema[2] = {
61 /* bytecode() schema */
62 "CREATE TABLE x("
63 "addr INT,"
64 "opcode TEXT,"
65 "p1 INT,"
66 "p2 INT,"
67 "p3 INT,"
68 "p4 TEXT,"
69 "p5 INT,"
70 "comment TEXT,"
71 "subprog TEXT,"
72 "nexec INT,"
73 "ncycle INT,"
74 "stmt HIDDEN"
75 ");",
76
77 /* Tables_used() schema */
78 "CREATE TABLE x("
79 "type TEXT,"
80 "schema TEXT,"
81 "name TEXT,"
82 "wr INT,"
83 "subprog TEXT,"
84 "stmt HIDDEN"
85 ");"
86 };
87
88 (void)argc;
89 (void)argv;
90 (void)pzErr;
91 rc = sqlite3_declare_vtab(db, azSchema[isTabUsed]);
92 if( rc==SQLITE_OK ){
93 pNew = sqlite3_malloc( sizeof(*pNew) );
94 *ppVtab = (sqlite3_vtab*)pNew;
95 if( pNew==0 ) return SQLITE_NOMEM;
96 memset(pNew, 0, sizeof(*pNew));
97 pNew->db = db;
98 pNew->bTablesUsed = isTabUsed*2;
99 }
100 return rc;
101}
102
103/*
104** This method is the destructor for bytecodevtab objects.
105*/
106static int bytecodevtabDisconnect(sqlite3_vtab *pVtab){
107 bytecodevtab *p = (bytecodevtab*)pVtab;
108 sqlite3_free(p);
109 return SQLITE_OK;
110}
111
112/*
113** Constructor for a new bytecodevtab_cursor object.
114*/
115static int bytecodevtabOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
116 bytecodevtab *pVTab = (bytecodevtab*)p;
117 bytecodevtab_cursor *pCur;
118 pCur = sqlite3_malloc( sizeof(*pCur) );
119 if( pCur==0 ) return SQLITE_NOMEM;
120 memset(pCur, 0, sizeof(*pCur));
121 sqlite3VdbeMemInit(&pCur->sub, pVTab->db, 1);
122 *ppCursor = &pCur->base;
123 return SQLITE_OK;
124}
125
126/*
127** Clear all internal content from a bytecodevtab cursor.
128*/
129static void bytecodevtabCursorClear(bytecodevtab_cursor *pCur){
130 sqlite3_free(pCur->zP4);
131 pCur->zP4 = 0;
132 sqlite3VdbeMemRelease(&pCur->sub);
133 sqlite3VdbeMemSetNull(&pCur->sub);
134 if( pCur->needFinalize ){
135 sqlite3_finalize(pCur->pStmt);
136 }
137 pCur->pStmt = 0;
138 pCur->needFinalize = 0;
139 pCur->zType = 0;
140 pCur->zSchema = 0;
141 pCur->zName = 0;
142}
143
144/*
145** Destructor for a bytecodevtab_cursor.
146*/
147static int bytecodevtabClose(sqlite3_vtab_cursor *cur){
148 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
149 bytecodevtabCursorClear(pCur);
150 sqlite3_free(pCur);
151 return SQLITE_OK;
152}
153
154
155/*
156** Advance a bytecodevtab_cursor to its next row of output.
157*/
158static int bytecodevtabNext(sqlite3_vtab_cursor *cur){
159 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
160 bytecodevtab *pTab = (bytecodevtab*)cur->pVtab;
161 int rc;
162 if( pCur->zP4 ){
163 sqlite3_free(pCur->zP4);
164 pCur->zP4 = 0;
165 }
166 if( pCur->zName ){
167 pCur->zName = 0;
168 pCur->zType = 0;
169 pCur->zSchema = 0;
170 }
171 rc = sqlite3VdbeNextOpcode(
172 (Vdbe*)pCur->pStmt,
173 pCur->showSubprograms ? &pCur->sub : 0,
174 pTab->bTablesUsed,
175 &pCur->iRowid,
176 &pCur->iAddr,
177 &pCur->aOp);
178 if( rc!=SQLITE_OK ){
179 sqlite3VdbeMemSetNull(&pCur->sub);
180 pCur->aOp = 0;
181 }
182 return SQLITE_OK;
183}
184
185/*
186** Return TRUE if the cursor has been moved off of the last
187** row of output.
188*/
189static int bytecodevtabEof(sqlite3_vtab_cursor *cur){
190 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
191 return pCur->aOp==0;
192}
193
194/*
195** Return values of columns for the row at which the bytecodevtab_cursor
196** is currently pointing.
197*/
198static int bytecodevtabColumn(
199 sqlite3_vtab_cursor *cur, /* The cursor */
200 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
201 int i /* Which column to return */
202){
203 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
204 bytecodevtab *pVTab = (bytecodevtab*)cur->pVtab;
205 Op *pOp = pCur->aOp + pCur->iAddr;
206 if( pVTab->bTablesUsed ){
207 if( i==4 ){
208 i = 8;
209 }else{
210 if( i<=2 && pCur->zType==0 ){
211 Schema *pSchema;
212 HashElem *k;
213 int iDb;
214 Pgno iRoot;
215 switch( pOp->opcode ){
216 case OP_Clear: {
217 iRoot = (Pgno)pOp->p1;
218 iDb = pOp->p2;
219 break;
220 }
221 case OP_Destroy: {
222 iRoot = (Pgno)pOp->p1;
223 iDb = pOp->p3;
224 break;
225 }
226 default: {
227 iRoot = (Pgno)pOp->p2;
228 iDb = pOp->p3;
229 }
230 }
231 sqlite3 *db = pVTab->db;
232 pSchema = db->aDb[iDb].pSchema;
233 pCur->zSchema = db->aDb[iDb].zDbSName;
234 for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
235 Table *pTab = (Table*)sqliteHashData(k);
236 if( !IsVirtual(pTab) && pTab->tnum==iRoot ){
237 pCur->zName = pTab->zName;
238 pCur->zType = "table";
239 break;
240 }
241 }
242 if( pCur->zName==0 ){
243 for(k=sqliteHashFirst(&pSchema->idxHash); k; k=sqliteHashNext(k)){
244 Index *pIdx = (Index*)sqliteHashData(k);
245 if( pIdx->tnum==iRoot ){
246 pCur->zName = pIdx->zName;
247 pCur->zType = "index";
248 }
249 }
250 }
251 }
252 i += 20;
253 }
254 }
255 switch( i ){
256 case 0: /* addr */
257 sqlite3_result_int(ctx, pCur->iAddr);
258 break;
259 case 1: /* opcode */
260 sqlite3_result_text(ctx, (char*)sqlite3OpcodeName(pOp->opcode),
261 -1, SQLITE_STATIC);
262 break;
263 case 2: /* p1 */
264 sqlite3_result_int(ctx, pOp->p1);
265 break;
266 case 3: /* p2 */
267 sqlite3_result_int(ctx, pOp->p2);
268 break;
269 case 4: /* p3 */
270 sqlite3_result_int(ctx, pOp->p3);
271 break;
272 case 5: /* p4 */
273 case 7: /* comment */
274 if( pCur->zP4==0 ){
275 pCur->zP4 = sqlite3VdbeDisplayP4(pVTab->db, pOp);
276 }
277 if( i==5 ){
278 sqlite3_result_text(ctx, pCur->zP4, -1, SQLITE_STATIC);
279 }else{
280#ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
281 char *zCom = sqlite3VdbeDisplayComment(pVTab->db, pOp, pCur->zP4);
282 sqlite3_result_text(ctx, zCom, -1, sqlite3_free);
283#endif
284 }
285 break;
286 case 6: /* p5 */
287 sqlite3_result_int(ctx, pOp->p5);
288 break;
289 case 8: { /* subprog */
290 Op *aOp = pCur->aOp;
291 assert( aOp[0].opcode==OP_Init );
292 assert( aOp[0].p4.z==0 || strncmp(aOp[0].p4.z,"-" "- ",3)==0 );
293 if( pCur->iRowid==pCur->iAddr+1 ){
294 break; /* Result is NULL for the main program */
295 }else if( aOp[0].p4.z!=0 ){
296 sqlite3_result_text(ctx, aOp[0].p4.z+3, -1, SQLITE_STATIC);
297 }else{
298 sqlite3_result_text(ctx, "(FK)", 4, SQLITE_STATIC);
299 }
300 break;
301 }
302
303#ifdef SQLITE_ENABLE_STMT_SCANSTATUS
304 case 9: /* nexec */
305 sqlite3_result_int64(ctx, pOp->nExec);
306 break;
307 case 10: /* ncycle */
308 sqlite3_result_int64(ctx, pOp->nCycle);
309 break;
310#else
311 case 9: /* nexec */
312 case 10: /* ncycle */
313 sqlite3_result_int(ctx, 0);
314 break;
315#endif
316
317 case 20: /* tables_used.type */
318 sqlite3_result_text(ctx, pCur->zType, -1, SQLITE_STATIC);
319 break;
320 case 21: /* tables_used.schema */
321 sqlite3_result_text(ctx, pCur->zSchema, -1, SQLITE_STATIC);
322 break;
323 case 22: /* tables_used.name */
324 sqlite3_result_text(ctx, pCur->zName, -1, SQLITE_STATIC);
325 break;
326 case 23: /* tables_used.wr */
327 sqlite3_result_int(ctx,
328 pOp->opcode==OP_OpenWrite ||
329 pOp->opcode==OP_Clear ||
330 pOp->opcode==OP_Destroy);
331 break;
332 }
333 return SQLITE_OK;
334}
335
336/*
337** Return the rowid for the current row. In this implementation, the
338** rowid is the same as the output value.
339*/
340static int bytecodevtabRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
341 bytecodevtab_cursor *pCur = (bytecodevtab_cursor*)cur;
342 *pRowid = pCur->iRowid;
343 return SQLITE_OK;
344}
345
346/*
347** Initialize a cursor.
348**
349** idxNum==0 means show all subprograms
350** idxNum==1 means show only the main bytecode and omit subprograms.
351*/
352static int bytecodevtabFilter(
353 sqlite3_vtab_cursor *pVtabCursor,
354 int idxNum, const char *idxStr,
355 int argc, sqlite3_value **argv
356){
357 bytecodevtab_cursor *pCur = (bytecodevtab_cursor *)pVtabCursor;
358 bytecodevtab *pVTab = (bytecodevtab *)pVtabCursor->pVtab;
359 int rc = SQLITE_OK;
360 (void)idxStr;
361
362 bytecodevtabCursorClear(pCur);
363 pCur->iRowid = 0;
364 pCur->iAddr = 0;
365 pCur->showSubprograms = idxNum==0;
366 assert( argc==1 );
367 if( sqlite3_value_type(argv[0])==SQLITE_TEXT ){
368 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
369 if( zSql==0 ){
370 rc = SQLITE_NOMEM;
371 }else{
372 rc = sqlite3_prepare_v2(pVTab->db, zSql, -1, &pCur->pStmt, 0);
373 pCur->needFinalize = 1;
374 }
375 }else{
376 pCur->pStmt = (sqlite3_stmt*)sqlite3_value_pointer(argv[0],"stmt-pointer");
377 }
378 if( pCur->pStmt==0 ){
379 pVTab->base.zErrMsg = sqlite3_mprintf(
380 "argument to %s() is not a valid SQL statement",
381 pVTab->bTablesUsed ? "tables_used" : "bytecode"
382 );
383 rc = SQLITE_ERROR;
384 }else{
385 bytecodevtabNext(pVtabCursor);
386 }
387 return rc;
388}
389
390/*
391** We must have a single stmt=? constraint that will be passed through
392** into the xFilter method. If there is no valid stmt=? constraint,
393** then return an SQLITE_CONSTRAINT error.
394*/
395static int bytecodevtabBestIndex(
396 sqlite3_vtab *tab,
397 sqlite3_index_info *pIdxInfo
398){
399 int i;
400 int rc = SQLITE_CONSTRAINT;
401 struct sqlite3_index_constraint *p;
402 bytecodevtab *pVTab = (bytecodevtab*)tab;
403 int iBaseCol = pVTab->bTablesUsed ? 4 : 10;
404 pIdxInfo->estimatedCost = (double)100;
405 pIdxInfo->estimatedRows = 100;
406 pIdxInfo->idxNum = 0;
407 for(i=0, p=pIdxInfo->aConstraint; i<pIdxInfo->nConstraint; i++, p++){
408 if( p->usable==0 ) continue;
409 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ && p->iColumn==iBaseCol+1 ){
410 rc = SQLITE_OK;
411 pIdxInfo->aConstraintUsage[i].omit = 1;
412 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
413 }
414 if( p->op==SQLITE_INDEX_CONSTRAINT_ISNULL && p->iColumn==iBaseCol ){
415 pIdxInfo->aConstraintUsage[i].omit = 1;
416 pIdxInfo->idxNum = 1;
417 }
418 }
419 return rc;
420}
421
422/*
423** This following structure defines all the methods for the
424** virtual table.
425*/
426static sqlite3_module bytecodevtabModule = {
427 /* iVersion */ 0,
428 /* xCreate */ 0,
429 /* xConnect */ bytecodevtabConnect,
430 /* xBestIndex */ bytecodevtabBestIndex,
431 /* xDisconnect */ bytecodevtabDisconnect,
432 /* xDestroy */ 0,
433 /* xOpen */ bytecodevtabOpen,
434 /* xClose */ bytecodevtabClose,
435 /* xFilter */ bytecodevtabFilter,
436 /* xNext */ bytecodevtabNext,
437 /* xEof */ bytecodevtabEof,
438 /* xColumn */ bytecodevtabColumn,
439 /* xRowid */ bytecodevtabRowid,
440 /* xUpdate */ 0,
441 /* xBegin */ 0,
442 /* xSync */ 0,
443 /* xCommit */ 0,
444 /* xRollback */ 0,
445 /* xFindMethod */ 0,
446 /* xRename */ 0,
447 /* xSavepoint */ 0,
448 /* xRelease */ 0,
449 /* xRollbackTo */ 0,
450 /* xShadowName */ 0,
451 /* xIntegrity */ 0
452};
453
454
455int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){
456 int rc;
457 rc = sqlite3_create_module(db, "bytecode", &bytecodevtabModule, 0);
458 if( rc==SQLITE_OK ){
459 rc = sqlite3_create_module(db, "tables_used", &bytecodevtabModule, &db);
460 }
461 return rc;
462}
463#elif defined(SQLITE_ENABLE_BYTECODE_VTAB)
464int sqlite3VdbeBytecodeVtabInit(sqlite3 *db){ return SQLITE_OK; }
465#endif /* SQLITE_ENABLE_BYTECODE_VTAB */