#define MODE_Box 16 /* Unicode box-drawing characters */
#define MODE_Count 17 /* Output only a count of the rows of output */
#define MODE_Off 18 /* No query output shown */
+#define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
static const char *modeDescr[] = {
"line",
}
break;
}
+ case MODE_ScanExp:
case MODE_Explain: {
- static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
- if( nArg>ArraySize(aExplainWidth) ){
- nArg = ArraySize(aExplainWidth);
+ static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
+ static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 };
+ static const int aScanExpWidth[] = {4, 6, 6, 13, 4, 4, 4, 13, 2, 13};
+ static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
+
+ const int *aWidth = aExplainWidth;
+ const int *aMap = aExplainMap;
+ int nWidth = ArraySize(aExplainWidth);
+ int iIndent = 1;
+
+ if( p->cMode==MODE_ScanExp ){
+ aWidth = aScanExpWidth;
+ aMap = aScanExpMap;
+ nWidth = ArraySize(aScanExpWidth);
+ iIndent = 3;
}
+ if( nArg>nWidth ) nArg = nWidth;
+
+ /* If this is the first row seen, print out the headers */
if( p->cnt++==0 ){
for(i=0; i<nArg; i++){
- int w = aExplainWidth[i];
- utf8_width_print(p->out, w, azCol[i]);
+ utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
fputs(i==nArg-1 ? "\n" : " ", p->out);
}
for(i=0; i<nArg; i++){
- int w = aExplainWidth[i];
- print_dashes(p->out, w);
+ print_dashes(p->out, aWidth[i]);
fputs(i==nArg-1 ? "\n" : " ", p->out);
}
}
+
+ /* If there is no data, exit early. */
if( azArg==0 ) break;
+
for(i=0; i<nArg; i++){
- int w = aExplainWidth[i];
+ const char *zSep = " ";
+ int w = aWidth[i];
+ const char *zVal = azArg[ aMap[i] ];
if( i==nArg-1 ) w = 0;
- if( azArg[i] && strlenChar(azArg[i])>w ){
- w = strlenChar(azArg[i]);
+ if( zVal && strlenChar(zVal)>w ){
+ w = strlenChar(zVal);
+ zSep = " ";
}
- if( i==1 && p->aiIndent && p->pStmt ){
+ if( i==iIndent && p->aiIndent && p->pStmt ){
if( p->iIndent<p->nIndent ){
utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
}
p->iIndent++;
}
- utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
- fputs(i==nArg-1 ? "\n" : " ", p->out);
+ utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
+ fputs(i==nArg-1 ? "\n" : zSep, p->out);
}
break;
}
}
#endif
-static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
-
-/*
-** Display scan stats.
-*/
-static void display_scanstats(
- sqlite3 *db, /* Database to query */
- ShellState *pArg /* Pointer to ShellState */
-){
-#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
- UNUSED_PARAMETER(db);
- UNUSED_PARAMETER(pArg);
-#else
- if( pArg->scanstatsOn==3 ){
- int rc = SQLITE_OK;
- sqlite3_stmt *pStmt = 0;
- rc = sqlite3_prepare_v2(db, "SELECT * FROM bytecode(?)", -1, &pStmt, 0);
- if( rc==SQLITE_OK ){
- sqlite3_stmt *pSave = pArg->pStmt;
- pArg->pStmt = pStmt;
- sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
- exec_prepared_stmt(pArg, pStmt);
- sqlite3_finalize(pStmt);
- pArg->pStmt = pSave;
- }
- }else{
- display_explain_scanstats(db, pArg);
- }
-#endif
-}
/*
** Parameter azArray points to a zero-terminated array of strings. zStr
** and "Goto" by 2 spaces.
*/
static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
- const char *zSql; /* The text of the SQL statement */
- const char *z; /* Used to check if this is an EXPLAIN */
int *abYield = 0; /* True if op is an OP_Yield */
int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
int iOp; /* Index of operation in p->aiIndent[] */
"Rewind", 0 };
const char *azGoto[] = { "Goto", 0 };
- /* Try to figure out if this is really an EXPLAIN statement. If this
- ** cannot be verified, return early. */
- if( sqlite3_column_count(pSql)!=8 ){
- p->cMode = p->mode;
- return;
- }
- zSql = sqlite3_sql(pSql);
- if( zSql==0 ) return;
- for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
- if( sqlite3_strnicmp(z, "explain", 7) ){
- p->cMode = p->mode;
- return;
- }
+ /* The caller guarantees that the leftmost 4 columns of the statement
+ ** passed to this function are equivalent to the leftmost 4 columns
+ ** of EXPLAIN statement output. In practice the statement may be
+ ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
+ assert( sqlite3_column_count(pSql)>=4 );
+ assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) );
+ assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) );
+ assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) );
+ assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) );
for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
int i;
int iAddr = sqlite3_column_int(pSql, 0);
const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
-
- /* Set p2 to the P2 field of the current opcode. Then, assuming that
- ** p2 is an instruction address, set variable p2op to the index of that
- ** instruction in the aiIndent[] array. p2 and p2op may be different if
- ** the current instruction is part of a sub-program generated by an
- ** SQL trigger or foreign key. */
+ int p1 = sqlite3_column_int(pSql, 2);
int p2 = sqlite3_column_int(pSql, 3);
+
+ /* Assuming that p2 is an instruction address, set variable p2op to the
+ ** index of that instruction in the aiIndent[] array. p2 and p2op may be
+ ** different if the current instruction is part of a sub-program generated
+ ** by an SQL trigger or foreign key. */
int p2op = (p2 + (iOp-iAddr));
/* Grow the p->aiIndent array as required */
if( iOp>=nAlloc ){
- if( iOp==0 ){
- /* Do further verification that this is explain output. Abort if
- ** it is not */
- static const char *explainCols[] = {
- "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
- int jj;
- for(jj=0; jj<ArraySize(explainCols); jj++){
- if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
- p->cMode = p->mode;
- sqlite3_reset(pSql);
- return;
- }
- }
- }
nAlloc += 100;
p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
shell_check_oom(p->aiIndent);
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
shell_check_oom(abYield);
}
+
abYield[iOp] = str_in_array(zOp, azYield);
p->aiIndent[iOp] = 0;
p->nIndent = iOp+1;
-
if( str_in_array(zOp, azNext) && p2op>0 ){
for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
}
- if( str_in_array(zOp, azGoto) && p2op<p->nIndent
- && (abYield[p2op] || sqlite3_column_int(pSql, 2))
- ){
+ if( str_in_array(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
}
}
p->iIndent = 0;
}
+static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
+
+/*
+** Display scan stats.
+*/
+static void display_scanstats(
+ sqlite3 *db, /* Database to query */
+ ShellState *pArg /* Pointer to ShellState */
+){
+#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
+ UNUSED_PARAMETER(db);
+ UNUSED_PARAMETER(pArg);
+#else
+ if( pArg->scanstatsOn==3 ){
+ const char *zSql =
+ " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
+ " round(ncycle*100.0 / (sum(ncycle) OVER ()), 2)||'%' AS cycles"
+ " FROM bytecode(?)";
+
+ int rc = SQLITE_OK;
+ sqlite3_stmt *pStmt = 0;
+ rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
+ if( rc==SQLITE_OK ){
+ sqlite3_stmt *pSave = pArg->pStmt;
+ pArg->pStmt = pStmt;
+ sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
+
+ pArg->cnt = 0;
+ pArg->cMode = MODE_ScanExp;
+ explain_data_prepare(pArg, pStmt);
+ exec_prepared_stmt(pArg, pStmt);
+ explain_data_delete(pArg);
+
+ sqlite3_finalize(pStmt);
+ pArg->pStmt = pSave;
+ }
+ }else{
+ display_explain_scanstats(db, pArg);
+ }
+#endif
+}
+
/*
** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
*/
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
if( rc==SQLITE_OK ){
pArg->cMode = MODE_Explain;
+ assert( sqlite3_stmt_isexplain(pExplain)==1 );
explain_data_prepare(pArg, pExplain);
exec_prepared_stmt(pArg, pExplain);
explain_data_delete(pArg);
}
if( pArg ){
+ int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
pArg->cMode = pArg->mode;
if( pArg->autoExplain ){
- if( sqlite3_stmt_isexplain(pStmt)==1 ){
+ if( bIsExplain ){
pArg->cMode = MODE_Explain;
}
if( sqlite3_stmt_isexplain(pStmt)==2 ){
/* If the shell is currently in ".explain" mode, gather the extra
** data required to add indents to the output.*/
- if( pArg->cMode==MODE_Explain ){
+ if( pArg->cMode==MODE_Explain && bIsExplain ){
explain_data_prepare(pArg, pStmt);
}
}