** Execute a statement or set of statements. Print ** any result rows/columns depending on the current mode ** set via the supplied callback. ** ** This is very similar to SQLite's built-in sqlite3_exec() ** function except it takes a slightly different callback ** and callback data argument. */
| 13565 | ** and callback data argument. |
| 13566 | */ |
| 13567 | static int shell_exec( |
| 13568 | ShellState *pArg, /* Pointer to ShellState */ |
| 13569 | const char *zSql, /* SQL to be evaluated */ |
| 13570 | char **pzErrMsg /* Error msg written here */ |
| 13571 | ){ |
| 13572 | sqlite3_stmt *pStmt = NULL; /* Statement to execute. */ |
| 13573 | int rc = SQLITE_OK; /* Return Code */ |
| 13574 | int rc2; |
| 13575 | const char *zLeftover; /* Tail of unprocessed SQL */ |
| 13576 | sqlite3 *db = pArg->db; |
| 13577 | |
| 13578 | if( pzErrMsg ){ |
| 13579 | *pzErrMsg = NULL; |
| 13580 | } |
| 13581 | |
| 13582 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 13583 | if( pArg->expert.pExpert ){ |
| 13584 | rc = expertHandleSQL(pArg, zSql, pzErrMsg); |
| 13585 | return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg); |
| 13586 | } |
| 13587 | #endif |
| 13588 | |
| 13589 | while( zSql[0] && (SQLITE_OK == rc) ){ |
| 13590 | static const char *zStmtSql; |
| 13591 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover); |
| 13592 | if( SQLITE_OK != rc ){ |
| 13593 | if( pzErrMsg ){ |
| 13594 | *pzErrMsg = save_err_msg(db); |
| 13595 | } |
| 13596 | }else{ |
| 13597 | if( !pStmt ){ |
| 13598 | /* this happens for a comment or white-space */ |
| 13599 | zSql = zLeftover; |
| 13600 | while( IsSpace(zSql[0]) ) zSql++; |
| 13601 | continue; |
| 13602 | } |
| 13603 | zStmtSql = sqlite3_sql(pStmt); |
| 13604 | if( zStmtSql==0 ) zStmtSql = ""; |
| 13605 | while( IsSpace(zStmtSql[0]) ) zStmtSql++; |
| 13606 | |
| 13607 | /* save off the prepared statment handle and reset row count */ |
| 13608 | if( pArg ){ |
| 13609 | pArg->pStmt = pStmt; |
| 13610 | pArg->cnt = 0; |
| 13611 | } |
| 13612 | |
| 13613 | /* echo the sql statement if echo on */ |
| 13614 | if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){ |
| 13615 | utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql); |
| 13616 | } |
| 13617 | |
| 13618 | /* Show the EXPLAIN QUERY PLAN if .eqp is on */ |
| 13619 | if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){ |
| 13620 | sqlite3_stmt *pExplain; |
| 13621 | char *zEQP; |
| 13622 | int triggerEQP = 0; |
| 13623 | disable_debug_trace_modes(); |
| 13624 | sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP); |
no test coverage detected