summaryrefslogtreecommitdiffstats
path: root/contrib/sqlite3/shell.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/sqlite3/shell.c')
-rw-r--r--contrib/sqlite3/shell.c1885
1 files changed, 1336 insertions, 549 deletions
diff --git a/contrib/sqlite3/shell.c b/contrib/sqlite3/shell.c
index d097e91..18c6ef7 100644
--- a/contrib/sqlite3/shell.c
+++ b/contrib/sqlite3/shell.c
@@ -90,7 +90,7 @@
#else
-# define shell_read_history(X)
+# define shell_read_history(X)
# define shell_write_history(X)
# define shell_stifle_history(X)
@@ -136,6 +136,15 @@
#define IsDigit(X) isdigit((unsigned char)X)
#define ToLower(X) (char)tolower((unsigned char)X)
+#if defined(_WIN32) || defined(WIN32)
+#include <windows.h>
+
+/* string conversion routines only needed on Win32 */
+extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
+extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
+extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
+#endif
+
/* On Windows, we normally run with output mode of TEXT so that \n characters
** are automatically translated into \r\n. However, this behavior needs
** to be disabled in some cases (ex: when generating CSV output and when
@@ -143,17 +152,17 @@
** routines take care of that.
*/
#if defined(_WIN32) || defined(WIN32)
-static void setBinaryMode(FILE *out){
- fflush(out);
- _setmode(_fileno(out), _O_BINARY);
+static void setBinaryMode(FILE *file, int isOutput){
+ if( isOutput ) fflush(file);
+ _setmode(_fileno(file), _O_BINARY);
}
-static void setTextMode(FILE *out){
- fflush(out);
- _setmode(_fileno(out), _O_TEXT);
+static void setTextMode(FILE *file, int isOutput){
+ if( isOutput ) fflush(file);
+ _setmode(_fileno(file), _O_TEXT);
}
#else
-# define setBinaryMode(X)
-# define setTextMode(X)
+# define setBinaryMode(X,Y)
+# define setTextMode(X,Y)
#endif
@@ -165,7 +174,7 @@ static sqlite3_int64 timeOfDay(void){
static sqlite3_vfs *clockVfs = 0;
sqlite3_int64 t;
if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
- if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
+ if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
clockVfs->xCurrentTimeInt64(clockVfs, &t);
}else{
double r;
@@ -204,7 +213,7 @@ static void beginTimer(void){
/* Return the difference of two time_structs in seconds */
static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
- return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
+ return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
(double)(pEnd->tv_sec - pStart->tv_sec);
}
@@ -229,8 +238,6 @@ static void endTimer(void){
#elif (defined(_WIN32) || defined(WIN32))
-#include <windows.h>
-
/* Saved resource information for the beginning of an operation */
static HANDLE hProcess;
static FILETIME ftKernelBegin;
@@ -261,7 +268,7 @@ static int hasTimer(void){
if( NULL != getProcessTimesAddr ){
return 1;
}
- FreeLibrary(hinstLib);
+ FreeLibrary(hinstLib);
}
}
}
@@ -307,7 +314,7 @@ static void endTimer(void){
#define HAS_TIMER hasTimer()
#else
-#define BEGIN_TIMER
+#define BEGIN_TIMER
#define END_TIMER
#define HAS_TIMER 0
#endif
@@ -330,6 +337,13 @@ static int bail_on_error = 0;
static int stdin_is_interactive = 1;
/*
+** On Windows systems we have to know if standard output is a console
+** in order to translate UTF-8 into MBCS. The following variable is
+** true if translation is required.
+*/
+static int stdout_is_console = 1;
+
+/*
** The following is the open SQLite database. We make a pointer
** to this database a static variable so that it can be accessed
** by the SIGINT handler to interrupt database processing.
@@ -355,6 +369,38 @@ static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
/*
+** Render output like fprintf(). Except, if the output is going to the
+** console and if this is running on a Windows machine, translate the
+** output from UTF-8 into MBCS.
+*/
+#if defined(_WIN32) || defined(WIN32)
+void utf8_printf(FILE *out, const char *zFormat, ...){
+ va_list ap;
+ va_start(ap, zFormat);
+ if( stdout_is_console && (out==stdout || out==stderr) ){
+ char *z1 = sqlite3_vmprintf(zFormat, ap);
+ char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
+ sqlite3_free(z1);
+ fputs(z2, out);
+ sqlite3_free(z2);
+ }else{
+ vfprintf(out, zFormat, ap);
+ }
+ va_end(ap);
+}
+#elif !defined(utf8_printf)
+# define utf8_printf fprintf
+#endif
+
+/*
+** Render output like fprintf(). This should not be used on anything that
+** includes string formatting (e.g. "%s").
+*/
+#if !defined(raw_printf)
+# define raw_printf fprintf
+#endif
+
+/*
** Write I/O traces to the following stream.
*/
#ifdef SQLITE_ENABLE_IOTRACE
@@ -375,7 +421,7 @@ static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
va_start(ap, zFormat);
z = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
- fprintf(iotrace, "%s", z);
+ utf8_printf(iotrace, "%s", z);
sqlite3_free(z);
}
#endif
@@ -409,8 +455,8 @@ static int isNumber(const char *z, int *realnum){
}
/*
-** A global char* and an SQL function to access its current value
-** from within an SQL statement. This program used to use the
+** A global char* and an SQL function to access its current value
+** from within an SQL statement. This program used to use the
** sqlite_exec_printf() API to substitue a string into an SQL statement.
** The correct way to do this with sqlite3 is to use the bind API, but
** since the shell is built around the callback paradigm it would be a lot
@@ -431,6 +477,16 @@ static void shellstaticFunc(
/*
+** Compute a string length that is limited to what can be stored in
+** lower 30 bits of a 32-bit signed integer.
+*/
+static int strlen30(const char *z){
+ const char *z2 = z;
+ while( *z2 ){ z2++; }
+ return 0x3fffffff & (int)(z2 - z);
+}
+
+/*
** This routine reads a line of text from FILE in, stores
** the text in memory obtained from malloc() and returns a pointer
** to the text. NULL is returned at end of file, or if malloc()
@@ -465,6 +521,25 @@ static char *local_getline(char *zLine, FILE *in){
break;
}
}
+#if defined(_WIN32) || defined(WIN32)
+ /* For interactive input on Windows systems, translate the
+ ** multi-byte characterset characters into UTF-8. */
+ if( stdin_is_interactive ){
+ char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
+ if( zTrans ){
+ int nTrans = strlen30(zTrans)+1;
+ if( nTrans>nLine ){
+ zLine = realloc(zLine, nTrans);
+ if( zLine==0 ){
+ sqlite3_free(zTrans);
+ return 0;
+ }
+ }
+ memcpy(zLine, zTrans, nTrans);
+ sqlite3_free(zTrans);
+ }
+ }
+#endif /* defined(_WIN32) || defined(WIN32) */
return zLine;
}
@@ -502,8 +577,21 @@ static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
return zResult;
}
+#if defined(SQLITE_ENABLE_SESSION)
+/*
+** State information for a single open session
+*/
+typedef struct OpenSession OpenSession;
+struct OpenSession {
+ char *zName; /* Symbolic name for this session */
+ int nFilter; /* Number of xFilter rejection GLOB patterns */
+ char **azFilter; /* Array of xFilter rejection GLOB patterns */
+ sqlite3_session *p; /* The open session */
+};
+#endif
+
/*
-** Shell output mode information from before ".explain on",
+** Shell output mode information from before ".explain on",
** saved so that it can be restored by ".explain off"
*/
typedef struct SavedModeInfo SavedModeInfo;
@@ -522,9 +610,11 @@ typedef struct ShellState ShellState;
struct ShellState {
sqlite3 *db; /* The database */
int echoOn; /* True to echo input commands */
+ int autoExplain; /* Automatically turn on .explain mode */
int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
int statsOn; /* True to display memory stats before each finalize */
int scanstatsOn; /* True to display scan stats before each finalize */
+ int countChanges; /* True to display change counts */
int backslashOn; /* Resolve C-style \x escapes in SQL input text */
int outCount; /* Revert to stdout when reaching zero */
int cnt; /* Number of records displayed so far */
@@ -532,6 +622,8 @@ struct ShellState {
FILE *traceOut; /* Output for sqlite3_trace() */
int nErr; /* Number of errors seen */
int mode; /* An output mode setting */
+ int cMode; /* temporary output mode for the current query */
+ int normalMode; /* Output mode before ".explain on" */
int writableSchema; /* True if PRAGMA writable_schema=ON */
int showHeader; /* True to show column names in List or Column mode */
unsigned shellFlgs; /* Various flags */
@@ -542,7 +634,6 @@ struct ShellState {
int actualWidth[100]; /* Actual width of each column */
char nullValue[20]; /* The text to print when a NULL comes back from
** the database */
- SavedModeInfo normalMode;/* Holds the mode just before .explain ON */
char outfile[FILENAME_MAX]; /* Filename for *out */
const char *zDbFilename; /* name of the database file */
char *zFreeOnClose; /* Filename to free when closing */
@@ -552,6 +643,10 @@ struct ShellState {
int *aiIndent; /* Array of indents used in MODE_Explain */
int nIndent; /* Size of array aiIndent[] */
int iIndent; /* Index of current op in aiIndent[] */
+#if defined(SQLITE_ENABLE_SESSION)
+ int nSession; /* Number of active sessions */
+ OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
+#endif
};
/*
@@ -574,6 +669,7 @@ struct ShellState {
#define MODE_Csv 7 /* Quote strings, numbers are plain */
#define MODE_Explain 8 /* Like MODE_Column, but do not truncate data */
#define MODE_Ascii 9 /* Use ASCII unit and record separators (0x1F/0x1E) */
+#define MODE_Pretty 10 /* Pretty-print schemas */
static const char *modeDescr[] = {
"line",
@@ -586,6 +682,7 @@ static const char *modeDescr[] = {
"csv",
"explain",
"ascii",
+ "prettyprint",
};
/*
@@ -607,22 +704,12 @@ static const char *modeDescr[] = {
#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
/*
-** Compute a string length that is limited to what can be stored in
-** lower 30 bits of a 32-bit signed integer.
-*/
-static int strlen30(const char *z){
- const char *z2 = z;
- while( *z2 ){ z2++; }
- return 0x3fffffff & (int)(z2 - z);
-}
-
-/*
** A callback for the sqlite3_log() interface.
*/
static void shellLog(void *pArg, int iErrCode, const char *zMsg){
ShellState *p = (ShellState*)pArg;
if( p->pLog==0 ) return;
- fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
+ utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
fflush(p->pLog);
}
@@ -632,9 +719,9 @@ static void shellLog(void *pArg, int iErrCode, const char *zMsg){
static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
int i;
char *zBlob = (char *)pBlob;
- fprintf(out,"X'");
- for(i=0; i<nBlob; i++){ fprintf(out,"%02x",zBlob[i]&0xff); }
- fprintf(out,"'");
+ raw_printf(out,"X'");
+ for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
+ raw_printf(out,"'");
}
/*
@@ -643,30 +730,30 @@ static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
static void output_quoted_string(FILE *out, const char *z){
int i;
int nSingle = 0;
- setBinaryMode(out);
+ setBinaryMode(out, 1);
for(i=0; z[i]; i++){
if( z[i]=='\'' ) nSingle++;
}
if( nSingle==0 ){
- fprintf(out,"'%s'",z);
+ utf8_printf(out,"'%s'",z);
}else{
- fprintf(out,"'");
+ raw_printf(out,"'");
while( *z ){
for(i=0; z[i] && z[i]!='\''; i++){}
if( i==0 ){
- fprintf(out,"''");
+ raw_printf(out,"''");
z++;
}else if( z[i]=='\'' ){
- fprintf(out,"%.*s''",i,z);
+ utf8_printf(out,"%.*s''",i,z);
z += i+1;
}else{
- fprintf(out,"%s",z);
+ utf8_printf(out,"%s",z);
break;
}
}
- fprintf(out,"'");
+ raw_printf(out,"'");
}
- setTextMode(out);
+ setTextMode(out, 1);
}
/*
@@ -692,7 +779,7 @@ static void output_c_string(FILE *out, const char *z){
fputc('\\', out);
fputc('r', out);
}else if( !isprint(c&0xff) ){
- fprintf(out, "\\%03o", c&0xff);
+ raw_printf(out, "\\%03o", c&0xff);
}else{
fputc(c, out);
}
@@ -708,26 +795,26 @@ static void output_html_string(FILE *out, const char *z){
int i;
if( z==0 ) z = "";
while( *z ){
- for(i=0; z[i]
- && z[i]!='<'
- && z[i]!='&'
- && z[i]!='>'
- && z[i]!='\"'
+ for(i=0; z[i]
+ && z[i]!='<'
+ && z[i]!='&'
+ && z[i]!='>'
+ && z[i]!='\"'
&& z[i]!='\'';
i++){}
if( i>0 ){
- fprintf(out,"%.*s",i,z);
+ utf8_printf(out,"%.*s",i,z);
}
if( z[i]=='<' ){
- fprintf(out,"&lt;");
+ raw_printf(out,"&lt;");
}else if( z[i]=='&' ){
- fprintf(out,"&amp;");
+ raw_printf(out,"&amp;");
}else if( z[i]=='>' ){
- fprintf(out,"&gt;");
+ raw_printf(out,"&gt;");
}else if( z[i]=='\"' ){
- fprintf(out,"&quot;");
+ raw_printf(out,"&quot;");
}else if( z[i]=='\'' ){
- fprintf(out,"&#39;");
+ raw_printf(out,"&#39;");
}else{
break;
}
@@ -740,22 +827,22 @@ static void output_html_string(FILE *out, const char *z){
** array, then the string must be quoted for CSV.
*/
static const char needCsvQuote[] = {
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
};
/*
@@ -767,13 +854,13 @@ static const char needCsvQuote[] = {
static void output_csv(ShellState *p, const char *z, int bSep){
FILE *out = p->out;
if( z==0 ){
- fprintf(out,"%s",p->nullValue);
+ utf8_printf(out,"%s",p->nullValue);
}else{
int i;
int nSep = strlen30(p->colSeparator);
for(i=0; z[i]; i++){
- if( needCsvQuote[((unsigned char*)z)[i]]
- || (z[i]==p->colSeparator[0] &&
+ if( needCsvQuote[((unsigned char*)z)[i]]
+ || (z[i]==p->colSeparator[0] &&
(nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
i = 0;
break;
@@ -787,11 +874,11 @@ static void output_csv(ShellState *p, const char *z, int bSep){
}
putc('"', out);
}else{
- fprintf(out, "%s", z);
+ utf8_printf(out, "%s", z);
}
}
if( bSep ){
- fprintf(p->out, "%s", p->colSeparator);
+ utf8_printf(p->out, "%s", p->colSeparator);
}
}
@@ -808,6 +895,52 @@ static void interrupt_handler(int NotUsed){
#endif
/*
+** When the ".auth ON" is set, the following authorizer callback is
+** invoked. It always returns SQLITE_OK.
+*/
+static int shellAuth(
+ void *pClientData,
+ int op,
+ const char *zA1,
+ const char *zA2,
+ const char *zA3,
+ const char *zA4
+){
+ ShellState *p = (ShellState*)pClientData;
+ static const char *azAction[] = { 0,
+ "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
+ "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
+ "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
+ "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
+ "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
+ "DROP_TRIGGER", "DROP_VIEW", "INSERT",
+ "PRAGMA", "READ", "SELECT",
+ "TRANSACTION", "UPDATE", "ATTACH",
+ "DETACH", "ALTER_TABLE", "REINDEX",
+ "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
+ "FUNCTION", "SAVEPOINT", "RECURSIVE"
+ };
+ int i;
+ const char *az[4];
+ az[0] = zA1;
+ az[1] = zA2;
+ az[2] = zA3;
+ az[3] = zA4;
+ raw_printf(p->out, "authorizer: %s", azAction[op]);
+ for(i=0; i<4; i++){
+ raw_printf(p->out, " ");
+ if( az[i] ){
+ output_c_string(p->out, az[i]);
+ }else{
+ raw_printf(p->out, "NULL");
+ }
+ }
+ raw_printf(p->out, "\n");
+ return SQLITE_OK;
+}
+
+
+/*
** This is the callback routine that the shell
** invokes for each row of a query result.
*/
@@ -821,7 +954,7 @@ static int shell_callback(
int i;
ShellState *p = (ShellState*)pArg;
- switch( p->mode ){
+ switch( p->cMode ){
case MODE_Line: {
int w = 5;
if( azArg==0 ) break;
@@ -829,20 +962,33 @@ static int shell_callback(
int len = strlen30(azCol[i] ? azCol[i] : "");
if( len>w ) w = len;
}
- if( p->cnt++>0 ) fprintf(p->out, "%s", p->rowSeparator);
+ if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
for(i=0; i<nArg; i++){
- fprintf(p->out,"%*s = %s%s", w, azCol[i],
+ utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
}
break;
}
case MODE_Explain:
case MODE_Column: {
+ static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
+ const int *colWidth;
+ int showHdr;
+ char *rowSep;
+ if( p->cMode==MODE_Column ){
+ colWidth = p->colWidth;
+ showHdr = p->showHeader;
+ rowSep = p->rowSeparator;
+ }else{
+ colWidth = aExplainWidths;
+ showHdr = 1;
+ rowSep = SEP_Row;
+ }
if( p->cnt++==0 ){
for(i=0; i<nArg; i++){
int w, n;
if( i<ArraySize(p->colWidth) ){
- w = p->colWidth[i];
+ w = colWidth[i];
}else{
w = 0;
}
@@ -855,17 +1001,17 @@ static int shell_callback(
if( i<ArraySize(p->actualWidth) ){
p->actualWidth[i] = w;
}
- if( p->showHeader ){
+ if( showHdr ){
if( w<0 ){
- fprintf(p->out,"%*.*s%s",-w,-w,azCol[i],
- i==nArg-1 ? p->rowSeparator : " ");
+ utf8_printf(p->out,"%*.*s%s",-w,-w,azCol[i],
+ i==nArg-1 ? rowSep : " ");
}else{
- fprintf(p->out,"%-*.*s%s",w,w,azCol[i],
- i==nArg-1 ? p->rowSeparator : " ");
+ utf8_printf(p->out,"%-*.*s%s",w,w,azCol[i],
+ i==nArg-1 ? rowSep : " ");
}
}
}
- if( p->showHeader ){
+ if( showHdr ){
for(i=0; i<nArg; i++){
int w;
if( i<ArraySize(p->actualWidth) ){
@@ -874,9 +1020,10 @@ static int shell_callback(
}else{
w = 10;
}
- fprintf(p->out,"%-*.*s%s",w,w,"-----------------------------------"
+ utf8_printf(p->out,"%-*.*s%s",w,w,
+ "----------------------------------------------------------"
"----------------------------------------------------------",
- i==nArg-1 ? p->rowSeparator : " ");
+ i==nArg-1 ? rowSep : " ");
}
}
}
@@ -888,32 +1035,95 @@ static int shell_callback(
}else{
w = 10;
}
- if( p->mode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
+ if( p->cMode==MODE_Explain && azArg[i] && strlen30(azArg[i])>w ){
w = strlen30(azArg[i]);
}
if( i==1 && p->aiIndent && p->pStmt ){
if( p->iIndent<p->nIndent ){
- fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
+ utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
}
p->iIndent++;
}
if( w<0 ){
- fprintf(p->out,"%*.*s%s",-w,-w,
+ utf8_printf(p->out,"%*.*s%s",-w,-w,
azArg[i] ? azArg[i] : p->nullValue,
- i==nArg-1 ? p->rowSeparator : " ");
+ i==nArg-1 ? rowSep : " ");
}else{
- fprintf(p->out,"%-*.*s%s",w,w,
+ utf8_printf(p->out,"%-*.*s%s",w,w,
azArg[i] ? azArg[i] : p->nullValue,
- i==nArg-1 ? p->rowSeparator : " ");
+ i==nArg-1 ? rowSep : " ");
+ }
+ }
+ break;
+ }
+ case MODE_Semi: { /* .schema and .fullschema output */
+ utf8_printf(p->out, "%s;\n", azArg[0]);
+ break;
+ }
+ case MODE_Pretty: { /* .schema and .fullschema with --indent */
+ char *z;
+ int j;
+ int nParen = 0;
+ char cEnd = 0;
+ char c;
+ int nLine = 0;
+ assert( nArg==1 );
+ if( azArg[0]==0 ) break;
+ if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
+ || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
+ ){
+ utf8_printf(p->out, "%s;\n", azArg[0]);
+ break;
+ }
+ z = sqlite3_mprintf("%s", azArg[0]);
+ j = 0;
+ for(i=0; IsSpace(z[i]); i++){}
+ for(; (c = z[i])!=0; i++){
+ if( IsSpace(c) ){
+ if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
+ }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
+ j--;
+ }
+ z[j++] = c;
+ }
+ while( j>0 && IsSpace(z[j-1]) ){ j--; }
+ z[j] = 0;
+ if( strlen30(z)>=79 ){
+ for(i=j=0; (c = z[i])!=0; i++){
+ if( c==cEnd ){
+ cEnd = 0;
+ }else if( c=='"' || c=='\'' || c=='`' ){
+ cEnd = c;
+ }else if( c=='[' ){
+ cEnd = ']';
+ }else if( c=='(' ){
+ nParen++;
+ }else if( c==')' ){
+ nParen--;
+ if( nLine>0 && nParen==0 && j>0 ){
+ utf8_printf(p->out, "%.*s\n", j, z);
+ j = 0;
+ }
+ }
+ z[j++] = c;
+ if( nParen==1 && (c=='(' || c==',' || c=='\n') ){
+ if( c=='\n' ) j--;
+ utf8_printf(p->out, "%.*s\n ", j, z);
+ j = 0;
+ nLine++;
+ while( IsSpace(z[i+1]) ){ i++; }
+ }
}
+ z[j] = 0;
}
+ utf8_printf(p->out, "%s;\n", z);
+ sqlite3_free(z);
break;
}
- case MODE_Semi:
case MODE_List: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- fprintf(p->out,"%s%s",azCol[i],
+ utf8_printf(p->out,"%s%s",azCol[i],
i==nArg-1 ? p->rowSeparator : p->colSeparator);
}
}
@@ -921,122 +1131,120 @@ static int shell_callback(
for(i=0; i<nArg; i++){
char *z = azArg[i];
if( z==0 ) z = p->nullValue;
- fprintf(p->out, "%s", z);
+ utf8_printf(p->out, "%s", z);
if( i<nArg-1 ){
- fprintf(p->out, "%s", p->colSeparator);
- }else if( p->mode==MODE_Semi ){
- fprintf(p->out, ";%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->colSeparator);
}else{
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
}
}
break;
}
case MODE_Html: {
if( p->cnt++==0 && p->showHeader ){
- fprintf(p->out,"<TR>");
+ raw_printf(p->out,"<TR>");
for(i=0; i<nArg; i++){
- fprintf(p->out,"<TH>");
+ raw_printf(p->out,"<TH>");
output_html_string(p->out, azCol[i]);
- fprintf(p->out,"</TH>\n");
+ raw_printf(p->out,"</TH>\n");
}
- fprintf(p->out,"</TR>\n");
+ raw_printf(p->out,"</TR>\n");
}
if( azArg==0 ) break;
- fprintf(p->out,"<TR>");
+ raw_printf(p->out,"<TR>");
for(i=0; i<nArg; i++){
- fprintf(p->out,"<TD>");
+ raw_printf(p->out,"<TD>");
output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
- fprintf(p->out,"</TD>\n");
+ raw_printf(p->out,"</TD>\n");
}
- fprintf(p->out,"</TR>\n");
+ raw_printf(p->out,"</TR>\n");
break;
}
case MODE_Tcl: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_c_string(p->out,azCol[i] ? azCol[i] : "");
- if(i<nArg-1) fprintf(p->out, "%s", p->colSeparator);
+ if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
- if(i<nArg-1) fprintf(p->out, "%s", p->colSeparator);
+ if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
break;
}
case MODE_Csv: {
- setBinaryMode(p->out);
+ setBinaryMode(p->out, 1);
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
}
if( nArg>0 ){
for(i=0; i<nArg; i++){
output_csv(p, azArg[i], i<nArg-1);
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
}
- setTextMode(p->out);
+ setTextMode(p->out, 1);
break;
}
case MODE_Insert: {
p->cnt++;
if( azArg==0 ) break;
- fprintf(p->out,"INSERT INTO %s",p->zDestTable);
+ utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
if( p->showHeader ){
- fprintf(p->out,"(");
+ raw_printf(p->out,"(");
for(i=0; i<nArg; i++){
char *zSep = i>0 ? ",": "";
- fprintf(p->out, "%s%s", zSep, azCol[i]);
+ utf8_printf(p->out, "%s%s", zSep, azCol[i]);
}
- fprintf(p->out,")");
+ raw_printf(p->out,")");
}
- fprintf(p->out," VALUES(");
+ raw_printf(p->out," VALUES(");
for(i=0; i<nArg; i++){
char *zSep = i>0 ? ",": "";
if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
- fprintf(p->out,"%sNULL",zSep);
+ utf8_printf(p->out,"%sNULL",zSep);
}else if( aiType && aiType[i]==SQLITE_TEXT ){
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
+ if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
output_quoted_string(p->out, azArg[i]);
}else if( aiType && (aiType[i]==SQLITE_INTEGER
|| aiType[i]==SQLITE_FLOAT) ){
- fprintf(p->out,"%s%s",zSep, azArg[i]);
+ utf8_printf(p->out,"%s%s",zSep, azArg[i]);
}else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
const void *pBlob = sqlite3_column_blob(p->pStmt, i);
int nBlob = sqlite3_column_bytes(p->pStmt, i);
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
+ if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
output_hex_blob(p->out, pBlob, nBlob);
}else if( isNumber(azArg[i], 0) ){
- fprintf(p->out,"%s%s",zSep, azArg[i]);
+ utf8_printf(p->out,"%s%s",zSep, azArg[i]);
}else{
- if( zSep[0] ) fprintf(p->out,"%s",zSep);
+ if( zSep[0] ) utf8_printf(p->out,"%s",zSep);
output_quoted_string(p->out, azArg[i]);
}
}
- fprintf(p->out,");\n");
+ raw_printf(p->out,");\n");
break;
}
case MODE_Ascii: {
if( p->cnt++==0 && p->showHeader ){
for(i=0; i<nArg; i++){
- if( i>0 ) fprintf(p->out, "%s", p->colSeparator);
- fprintf(p->out,"%s",azCol[i] ? azCol[i] : "");
+ if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
+ utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
}
if( azArg==0 ) break;
for(i=0; i<nArg; i++){
- if( i>0 ) fprintf(p->out, "%s", p->colSeparator);
- fprintf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
+ if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
+ utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
}
- fprintf(p->out, "%s", p->rowSeparator);
+ utf8_printf(p->out, "%s", p->rowSeparator);
break;
}
}
@@ -1077,7 +1285,7 @@ static void set_table_name(ShellState *p, const char *zName){
if( needQuote ) n += 2;
z = p->zDestTable = malloc( n+1 );
if( z==0 ){
- fprintf(stderr,"Error: out of memory\n");
+ raw_printf(stderr,"Error: out of memory\n");
exit(1);
}
n = 0;
@@ -1095,7 +1303,7 @@ static void set_table_name(ShellState *p, const char *zName){
** added to zIn, and the result returned in memory obtained from malloc().
** zIn, if it was not NULL, is freed.
**
-** If the third argument, quote, is not '\0', then it is used as a
+** If the third argument, quote, is not '\0', then it is used as a
** quote character for zAppend.
*/
static char *appendText(char *zIn, char const *zAppend, char quote){
@@ -1142,7 +1350,7 @@ static char *appendText(char *zIn, char const *zAppend, char quote){
** semicolon terminator to the end of that line.
**
** If the number of columns is 1 and that column contains text "--"
-** then write the semicolon on a separate line. That way, if a
+** then write the semicolon on a separate line. That way, if a
** "--" comment occurs at the end of the statement, the comment
** won't consume the semicolon terminator.
*/
@@ -1158,7 +1366,8 @@ static int run_table_dump_query(
const char *z;
rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
if( rc!=SQLITE_OK || !pSelect ){
- fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
+ utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
+ sqlite3_errmsg(p->db));
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
return rc;
}
@@ -1166,26 +1375,27 @@ static int run_table_dump_query(
nResult = sqlite3_column_count(pSelect);
while( rc==SQLITE_ROW ){
if( zFirstRow ){
- fprintf(p->out, "%s", zFirstRow);
+ utf8_printf(p->out, "%s", zFirstRow);
zFirstRow = 0;
}
z = (const char*)sqlite3_column_text(pSelect, 0);
- fprintf(p->out, "%s", z);
- for(i=1; i<nResult; i++){
- fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
+ utf8_printf(p->out, "%s", z);
+ for(i=1; i<nResult; i++){
+ utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
}
if( z==0 ) z = "";
while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
if( z[0] ){
- fprintf(p->out, "\n;\n");
+ raw_printf(p->out, "\n;\n");
}else{
- fprintf(p->out, ";\n");
- }
+ raw_printf(p->out, ";\n");
+ }
rc = sqlite3_step(pSelect);
}
rc = sqlite3_finalize(pSelect);
if( rc!=SQLITE_OK ){
- fprintf(p->out, "/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
+ utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
+ sqlite3_errmsg(p->db));
if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
}
return rc;
@@ -1205,6 +1415,43 @@ static char *save_err_msg(
return zErrMsg;
}
+#ifdef __linux__
+/*
+** Attempt to display I/O stats on Linux using /proc/PID/io
+*/
+static void displayLinuxIoStats(FILE *out){
+ FILE *in;
+ char z[200];
+ sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
+ in = fopen(z, "rb");
+ if( in==0 ) return;
+ while( fgets(z, sizeof(z), in)!=0 ){
+ static const struct {
+ const char *zPattern;
+ const char *zDesc;
+ } aTrans[] = {
+ { "rchar: ", "Bytes received by read():" },
+ { "wchar: ", "Bytes sent to write():" },
+ { "syscr: ", "Read() system calls:" },
+ { "syscw: ", "Write() system calls:" },
+ { "read_bytes: ", "Bytes read from storage:" },
+ { "write_bytes: ", "Bytes written to storage:" },
+ { "cancelled_write_bytes: ", "Cancelled write bytes:" },
+ };
+ int i;
+ for(i=0; i<ArraySize(aTrans); i++){
+ int n = (int)strlen(aTrans[i].zPattern);
+ if( strncmp(aTrans[i].zPattern, z, n)==0 ){
+ raw_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
+ break;
+ }
+ }
+ }
+ fclose(in);
+}
+#endif
+
+
/*
** Display memory stats.
*/
@@ -1217,55 +1464,56 @@ static int display_stats(
int iHiwtr;
if( pArg && pArg->out ){
-
+
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out,
+ raw_printf(pArg->out,
"Memory Used: %d (max %d) bytes\n",
iCur, iHiwtr);
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
+ raw_printf(pArg->out, "Number of Outstanding Allocations: %d (max %d)\n",
iCur, iHiwtr);
if( pArg->shellFlgs & SHFLG_Pagecache ){
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_PAGECACHE_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out,
+ raw_printf(pArg->out,
"Number of Pcache Pages Used: %d (max %d) pages\n",
iCur, iHiwtr);
}
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out,
+ raw_printf(pArg->out,
"Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
iCur, iHiwtr);
if( pArg->shellFlgs & SHFLG_Scratch ){
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_SCRATCH_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Number of Scratch Allocations Used: %d (max %d)\n",
+ raw_printf(pArg->out,
+ "Number of Scratch Allocations Used: %d (max %d)\n",
iCur, iHiwtr);
}
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out,
+ raw_printf(pArg->out,
"Number of Scratch Overflow Bytes: %d (max %d) bytes\n",
iCur, iHiwtr);
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Allocation: %d bytes\n",
+ raw_printf(pArg->out, "Largest Allocation: %d bytes\n",
iHiwtr);
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
+ raw_printf(pArg->out, "Largest Pcache Allocation: %d bytes\n",
iHiwtr);
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
+ raw_printf(pArg->out, "Largest Scratch Allocation: %d bytes\n",
iHiwtr);
#ifdef YYTRACKMAXSTACKDEPTH
iHiwtr = iCur = -1;
sqlite3_status(SQLITE_STATUS_PARSER_STACK, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
+ raw_printf(pArg->out, "Deepest Parser Stack: %d (max %d)\n",
iCur, iHiwtr);
#endif
}
@@ -1275,50 +1523,63 @@ static int display_stats(
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
&iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Lookaside Slots Used: %d (max %d)\n",
+ raw_printf(pArg->out,
+ "Lookaside Slots Used: %d (max %d)\n",
iCur, iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
&iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Successful lookaside attempts: %d\n", iHiwtr);
+ raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
+ iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
&iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Lookaside failures due to size: %d\n", iHiwtr);
+ raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
+ iHiwtr);
sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
&iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Lookaside failures due to OOM: %d\n", iHiwtr);
+ raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
+ iHiwtr);
}
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Pager Heap Usage: %d bytes\n",iCur);
+ raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
+ iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
- fprintf(pArg->out, "Page cache hits: %d\n", iCur);
+ raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
- fprintf(pArg->out, "Page cache misses: %d\n", iCur);
+ raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
- fprintf(pArg->out, "Page cache writes: %d\n", iCur);
+ raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Schema Heap Usage: %d bytes\n",iCur);
+ raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
+ iCur);
iHiwtr = iCur = -1;
sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
- fprintf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",iCur);
+ raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
+ iCur);
}
if( pArg && pArg->out && db && pArg->pStmt ){
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
bReset);
- fprintf(pArg->out, "Fullscan Steps: %d\n", iCur);
+ raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
- fprintf(pArg->out, "Sort Operations: %d\n", iCur);
+ raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
- fprintf(pArg->out, "Autoindex Inserts: %d\n", iCur);
+ raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
- fprintf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
+ raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
}
+#ifdef __linux__
+ displayLinuxIoStats(pArg->out);
+#endif
+
+ /* Do not remove this machine readable comment: extra-stats-output-here */
+
return 0;
}
@@ -1334,7 +1595,7 @@ static void display_scanstats(
UNUSED_PARAMETER(pArg);
#else
int i, k, n, mx;
- fprintf(pArg->out, "-------- scanstats --------\n");
+ raw_printf(pArg->out, "-------- scanstats --------\n");
mx = 0;
for(k=0; k<=mx; k++){
double rEstLoop = 1.0;
@@ -1352,21 +1613,21 @@ static void display_scanstats(
if( iSid!=k ) continue;
if( n==0 ){
rEstLoop = (double)nLoop;
- if( k>0 ) fprintf(pArg->out, "-------- subquery %d -------\n", k);
+ if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
}
n++;
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
- fprintf(pArg->out, "Loop %2d: %s\n", n, zExplain);
+ utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
rEstLoop *= rEst;
- fprintf(pArg->out,
+ raw_printf(pArg->out,
" nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
);
}
}
- fprintf(pArg->out, "---------------------------\n");
+ raw_printf(pArg->out, "---------------------------\n");
#endif
}
@@ -1387,7 +1648,7 @@ static int str_in_array(const char *zStr, const char **azArray){
/*
** If compiled statement pSql appears to be an EXPLAIN statement, allocate
** and populate the ShellState.aiIndent[] array with the number of
-** spaces each opcode should be indented before it is output.
+** spaces each opcode should be indented before it is output.
**
** The indenting rules are:
**
@@ -1417,10 +1678,17 @@ static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
/* 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) ) return;
+ if( sqlite3_strnicmp(z, "explain", 7) ){
+ p->cMode = p->mode;
+ return;
+ }
for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
int i;
@@ -1437,6 +1705,20 @@ static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
/* Grow the p->aiIndent array as required */
if( iOp>=nAlloc ){
+ if( iOp==0 ){
+ /* Do further verfication 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( 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));
abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
@@ -1451,7 +1733,7 @@ static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
if( str_in_array(zOp, azGoto) && p2op<p->nIndent
&& (abYield[p2op] || sqlite3_column_int(pSql, 2))
){
- for(i=p2op+1; i<iOp; i++) p->aiIndent[i] += 2;
+ for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
}
}
@@ -1471,12 +1753,110 @@ static void explain_data_delete(ShellState *p){
}
/*
-** Execute a statement or set of statements. Print
-** any result rows/columns depending on the current mode
+** Disable and restore .wheretrace and .selecttrace settings.
+*/
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
+extern int sqlite3SelectTrace;
+static int savedSelectTrace;
+#endif
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
+extern int sqlite3WhereTrace;
+static int savedWhereTrace;
+#endif
+static void disable_debug_trace_modes(void){
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
+ savedSelectTrace = sqlite3SelectTrace;
+ sqlite3SelectTrace = 0;
+#endif
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
+ savedWhereTrace = sqlite3WhereTrace;
+ sqlite3WhereTrace = 0;
+#endif
+}
+static void restore_debug_trace_modes(void){
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
+ sqlite3SelectTrace = savedSelectTrace;
+#endif
+#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
+ sqlite3WhereTrace = savedWhereTrace;
+#endif
+}
+
+/*
+** Run a prepared statement
+*/
+static void exec_prepared_stmt(
+ ShellState *pArg, /* Pointer to ShellState */
+ sqlite3_stmt *pStmt, /* Statment to run */
+ int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
+){
+ int rc;
+
+ /* perform the first step. this will tell us if we
+ ** have a result set or not and how wide it is.
+ */
+ rc = sqlite3_step(pStmt);
+ /* if we have a result set... */
+ if( SQLITE_ROW == rc ){
+ /* if we have a callback... */
+ if( xCallback ){
+ /* allocate space for col name ptr, value ptr, and type */
+ int nCol = sqlite3_column_count(pStmt);
+ void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
+ if( !pData ){
+ rc = SQLITE_NOMEM;
+ }else{
+ char **azCols = (char **)pData; /* Names of result columns */
+ char **azVals = &azCols[nCol]; /* Results */
+ int *aiTypes = (int *)&azVals[nCol]; /* Result types */
+ int i, x;
+ assert(sizeof(int) <= sizeof(char *));
+ /* save off ptrs to column names */
+ for(i=0; i<nCol; i++){
+ azCols[i] = (char *)sqlite3_column_name(pStmt, i);
+ }
+ do{
+ /* extract the data and data types */
+ for(i=0; i<nCol; i++){
+ aiTypes[i] = x = sqlite3_column_type(pStmt, i);
+ if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
+ azVals[i] = "";
+ }else{
+ azVals[i] = (char*)sqlite3_column_text(pStmt, i);
+ }
+ if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
+ rc = SQLITE_NOMEM;
+ break; /* from for */
+ }
+ } /* end for */
+
+ /* if data and types extracted successfully... */
+ if( SQLITE_ROW == rc ){
+ /* call the supplied callback with the result row data */
+ if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
+ rc = SQLITE_ABORT;
+ }else{
+ rc = sqlite3_step(pStmt);
+ }
+ }
+ } while( SQLITE_ROW == rc );
+ sqlite3_free(pData);
+ }
+ }else{
+ do{
+ rc = sqlite3_step(pStmt);
+ } while( rc == SQLITE_ROW );
+ }
+ }
+}
+
+/*
+** 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
+** This is very similar to SQLite's built-in sqlite3_exec()
+** function except it takes a slightly different callback
** and callback data argument.
*/
static int shell_exec(
@@ -1497,6 +1877,7 @@ static int shell_exec(
}
while( zSql[0] && (SQLITE_OK == rc) ){
+ static const char *zStmtSql;
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
if( SQLITE_OK != rc ){
if( pzErrMsg ){
@@ -1509,6 +1890,8 @@ static int shell_exec(
while( IsSpace(zSql[0]) ) zSql++;
continue;
}
+ zStmtSql = sqlite3_sql(pStmt);
+ while( IsSpace(zStmtSql[0]) ) zStmtSql++;
/* save off the prepared statment handle and reset row count */
if( pArg ){
@@ -1518,91 +1901,59 @@ static int shell_exec(
/* echo the sql statement if echo on */
if( pArg && pArg->echoOn ){
- const char *zStmtSql = sqlite3_sql(pStmt);
- fprintf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
+ utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
}
/* Show the EXPLAIN QUERY PLAN if .eqp is on */
- if( pArg && pArg->autoEQP ){
+ if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
sqlite3_stmt *pExplain;
- char *zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s",
- sqlite3_sql(pStmt));
+ char *zEQP;
+ disable_debug_trace_modes();
+ zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
if( rc==SQLITE_OK ){
while( sqlite3_step(pExplain)==SQLITE_ROW ){
- fprintf(pArg->out,"--EQP-- %d,", sqlite3_column_int(pExplain, 0));
- fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
- fprintf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
- fprintf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
+ raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
+ raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
+ raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
+ utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
}
}
sqlite3_finalize(pExplain);
sqlite3_free(zEQP);
+ if( pArg->autoEQP>=2 ){
+ /* Also do an EXPLAIN for ".eqp full" mode */
+ zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
+ rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
+ if( rc==SQLITE_OK ){
+ pArg->cMode = MODE_Explain;
+ explain_data_prepare(pArg, pExplain);
+ exec_prepared_stmt(pArg, pExplain, xCallback);
+ explain_data_delete(pArg);
+ }
+ sqlite3_finalize(pExplain);
+ sqlite3_free(zEQP);
+ }
+ restore_debug_trace_modes();
}
- /* If the shell is currently in ".explain" mode, gather the extra
- ** data required to add indents to the output.*/
- if( pArg && pArg->mode==MODE_Explain ){
- explain_data_prepare(pArg, pStmt);
- }
+ if( pArg ){
+ pArg->cMode = pArg->mode;
+ if( pArg->autoExplain
+ && sqlite3_column_count(pStmt)==8
+ && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
+ ){
+ pArg->cMode = MODE_Explain;
+ }
- /* perform the first step. this will tell us if we
- ** have a result set or not and how wide it is.
- */
- rc = sqlite3_step(pStmt);
- /* if we have a result set... */
- if( SQLITE_ROW == rc ){
- /* if we have a callback... */
- if( xCallback ){
- /* allocate space for col name ptr, value ptr, and type */
- int nCol = sqlite3_column_count(pStmt);
- void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
- if( !pData ){
- rc = SQLITE_NOMEM;
- }else{
- char **azCols = (char **)pData; /* Names of result columns */
- char **azVals = &azCols[nCol]; /* Results */
- int *aiTypes = (int *)&azVals[nCol]; /* Result types */
- int i, x;
- assert(sizeof(int) <= sizeof(char *));
- /* save off ptrs to column names */
- for(i=0; i<nCol; i++){
- azCols[i] = (char *)sqlite3_column_name(pStmt, i);
- }
- do{
- /* extract the data and data types */
- for(i=0; i<nCol; i++){
- aiTypes[i] = x = sqlite3_column_type(pStmt, i);
- if( x==SQLITE_BLOB && pArg && pArg->mode==MODE_Insert ){
- azVals[i] = "";
- }else{
- azVals[i] = (char*)sqlite3_column_text(pStmt, i);
- }
- if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
- rc = SQLITE_NOMEM;
- break; /* from for */
- }
- } /* end for */
-
- /* if data and types extracted successfully... */
- if( SQLITE_ROW == rc ){
- /* call the supplied callback with the result row data */
- if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
- rc = SQLITE_ABORT;
- }else{
- rc = sqlite3_step(pStmt);
- }
- }
- } while( SQLITE_ROW == rc );
- sqlite3_free(pData);
- }
- }else{
- do{
- rc = sqlite3_step(pStmt);
- } while( rc == SQLITE_ROW );
+ /* If the shell is currently in ".explain" mode, gather the extra
+ ** data required to add indents to the output.*/
+ if( pArg->cMode==MODE_Explain ){
+ explain_data_prepare(pArg, pStmt);
}
}
+ exec_prepared_stmt(pArg, pStmt, xCallback);
explain_data_delete(pArg);
/* print usage stats if stats on */
@@ -1615,7 +1966,7 @@ static int shell_exec(
display_scanstats(db, pArg);
}
- /* Finalize the statement just executed. If this fails, save a
+ /* Finalize the statement just executed. If this fails, save a
** copy of the error message. Otherwise, set zSql to point to the
** next statement to execute. */
rc2 = sqlite3_finalize(pStmt);
@@ -1657,28 +2008,28 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
zTable = azArg[0];
zType = azArg[1];
zSql = azArg[2];
-
+
if( strcmp(zTable, "sqlite_sequence")==0 ){
zPrepStmt = "DELETE FROM sqlite_sequence;\n";
}else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
- fprintf(p->out, "ANALYZE sqlite_master;\n");
+ raw_printf(p->out, "ANALYZE sqlite_master;\n");
}else if( strncmp(zTable, "sqlite_", 7)==0 ){
return 0;
}else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
char *zIns;
if( !p->writableSchema ){
- fprintf(p->out, "PRAGMA writable_schema=ON;\n");
+ raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
p->writableSchema = 1;
}
zIns = sqlite3_mprintf(
"INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
"VALUES('table','%q','%q',0,'%q');",
zTable, zTable, zSql);
- fprintf(p->out, "%s\n", zIns);
+ utf8_printf(p->out, "%s\n", zIns);
sqlite3_free(zIns);
return 0;
}else{
- fprintf(p->out, "%s;\n", zSql);
+ utf8_printf(p->out, "%s;\n", zSql);
}
if( strcmp(zType, "table")==0 ){
@@ -1687,7 +2038,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
char *zTableInfo = 0;
char *zTmp = 0;
int nRow = 0;
-
+
zTableInfo = appendText(zTableInfo, "PRAGMA table_info(", 0);
zTableInfo = appendText(zTableInfo, zTable, '"');
zTableInfo = appendText(zTableInfo, ");", 0);
@@ -1746,7 +2097,7 @@ static int dump_callback(void *pArg, int nArg, char **azArg, char **azCol){
** "ORDER BY rowid DESC" to the end.
*/
static int run_schema_dump_query(
- ShellState *p,
+ ShellState *p,
const char *zQuery
){
int rc;
@@ -1755,9 +2106,9 @@ static int run_schema_dump_query(
if( rc==SQLITE_CORRUPT ){
char *zQ2;
int len = strlen30(zQuery);
- fprintf(p->out, "/****** CORRUPTION ERROR *******/\n");
+ raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
if( zErr ){
- fprintf(p->out, "/****** %s ******/\n", zErr);
+ utf8_printf(p->out, "/****** %s ******/\n", zErr);
sqlite3_free(zErr);
zErr = 0;
}
@@ -1766,7 +2117,7 @@ static int run_schema_dump_query(
sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
if( rc ){
- fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
+ utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
}else{
rc = SQLITE_CORRUPT;
}
@@ -1780,9 +2131,11 @@ static int run_schema_dump_query(
** Text of a help message
*/
static char zHelp[] =
+ ".auth ON|OFF Show authorizer callbacks\n"
".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
".bail on|off Stop after hitting an error. Default OFF\n"
".binary on|off Turn binary output on or off. Default OFF\n"
+ ".changes on|off Show number of rows changed by SQL\n"
".clone NEWDB Clone data into NEWDB from the existing database\n"
".databases List names and files of attached databases\n"
".dbinfo ?DB? Show status information about the database\n"
@@ -1790,11 +2143,10 @@ static char zHelp[] =
" If TABLE specified, only dump tables matching\n"
" LIKE pattern TABLE.\n"
".echo on|off Turn command echo on or off\n"
- ".eqp on|off Enable or disable automatic EXPLAIN QUERY PLAN\n"
+ ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
".exit Exit this program\n"
- ".explain ?on|off? Turn output mode suitable for EXPLAIN on or off.\n"
- " With no args, it turns EXPLAIN on.\n"
- ".fullschema Show schema and the content of sqlite_stat tables\n"
+ ".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"
+ ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
".headers on|off Turn display of headers on or off\n"
".help Show this message\n"
".import FILE TABLE Import data from FILE into TABLE\n"
@@ -1830,14 +2182,16 @@ static char zHelp[] =
".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
".save FILE Write in-memory database into FILE\n"
".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
- ".schema ?TABLE? Show the CREATE statements\n"
- " If TABLE specified, only show tables matching\n"
- " LIKE pattern TABLE.\n"
+ ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
+ " Add --indent for pretty-printing\n"
".separator COL ?ROW? Change the column separator and optionally the row\n"
" separator for both the output mode and .import\n"
+#if defined(SQLITE_ENABLE_SESSION)
+ ".session CMD ... Create or control sessions\n"
+#endif
".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
".show Show the current values for various settings\n"
- ".stats on|off Turn stats on or off\n"
+ ".stats ?on|off? Show stats or turn stats on or off\n"
".system CMD ARGS... Run CMD ARGS... in a system shell\n"
".tables ?TABLE? List names of tables\n"
" If TABLE specified, only list tables matching\n"
@@ -1845,11 +2199,37 @@ static char zHelp[] =
".timeout MS Try opening locked tables for MS milliseconds\n"
".timer on|off Turn SQL timer on or off\n"
".trace FILE|off Output each SQL statement as it is run\n"
+ ".vfsinfo ?AUX? Information about the top-level VFS\n"
+ ".vfslist List all available VFSes\n"
".vfsname ?AUX? Print the name of the VFS stack\n"
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
" Negative values right-justify\n"
;
+#if defined(SQLITE_ENABLE_SESSION)
+/*
+** Print help information for the ".sessions" command
+*/
+void session_help(ShellState *p){
+ raw_printf(p->out,
+ ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
+ "If ?NAME? is omitted, the first defined session is used.\n"
+ "Subcommands:\n"
+ " attach TABLE Attach TABLE\n"
+ " changeset FILE Write a changeset into FILE\n"
+ " close Close one session\n"
+ " enable ?BOOLEAN? Set or query the enable bit\n"
+ " filter GLOB... Reject tables matching GLOBs\n"
+ " indirect ?BOOLEAN? Mark or query the indirect status\n"
+ " isempty Query whether the session is empty\n"
+ " list List currently open session names\n"
+ " open DB NAME Open a new session on DB\n"
+ " patchset FILE Write a patchset into FILE\n"
+ );
+}
+#endif
+
+
/* Forward reference */
static int process_input(ShellState *p, FILE *in);
/*
@@ -1915,6 +2295,53 @@ static void writefileFunc(
sqlite3_result_int64(context, rc);
}
+#if defined(SQLITE_ENABLE_SESSION)
+/*
+** Close a single OpenSession object and release all of its associated
+** resources.
+*/
+static void session_close(OpenSession *pSession){
+ int i;
+ sqlite3session_delete(pSession->p);
+ sqlite3_free(pSession->zName);
+ for(i=0; i<pSession->nFilter; i++){
+ sqlite3_free(pSession->azFilter[i]);
+ }
+ sqlite3_free(pSession->azFilter);
+ memset(pSession, 0, sizeof(OpenSession));
+}
+#endif
+
+/*
+** Close all OpenSession objects and release all associated resources.
+*/
+#if defined(SQLITE_ENABLE_SESSION)
+static void session_close_all(ShellState *p){
+ int i;
+ for(i=0; i<p->nSession; i++){
+ session_close(&p->aSession[i]);
+ }
+ p->nSession = 0;
+}
+#else
+# define session_close_all(X)
+#endif
+
+/*
+** Implementation of the xFilter function for an open session. Omit
+** any tables named by ".session filter" but let all other table through.
+*/
+#if defined(SQLITE_ENABLE_SESSION)
+static int session_filter(void *pCtx, const char *zTab){
+ OpenSession *pSession = (OpenSession*)pCtx;
+ int i;
+ for(i=0; i<pSession->nFilter; i++){
+ if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
+ }
+ return 1;
+}
+#endif
+
/*
** Make sure the database is open. If it is not, then open it. If
** the database fails to open, print an error message and exit.
@@ -1929,7 +2356,7 @@ static void open_db(ShellState *p, int keepAlive){
shellstaticFunc, 0, 0);
}
if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
- fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
+ utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
p->zDbFilename, sqlite3_errmsg(p->db));
if( keepAlive ) return;
exit(1);
@@ -2079,7 +2506,7 @@ static int booleanValue(char *zArg){
if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
return 0;
}
- fprintf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
+ utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
zArg);
return 0;
}
@@ -2093,7 +2520,7 @@ static void output_file_close(FILE *f){
/*
** Try to open an output file. The names "stdout" and "stderr" are
-** recognized and do the right thing. NULL is returned if the output
+** recognized and do the right thing. NULL is returned if the output
** filename is "off".
*/
static FILE *output_file_open(const char *zFile){
@@ -2107,7 +2534,7 @@ static FILE *output_file_open(const char *zFile){
}else{
f = fopen(zFile, "wb");
if( f==0 ){
- fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
+ utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
}
}
return f;
@@ -2116,13 +2543,22 @@ static FILE *output_file_open(const char *zFile){
/*
** A routine for handling output from sqlite3_trace().
*/
-static void sql_trace_callback(void *pArg, const char *z){
+static int sql_trace_callback(
+ unsigned mType,
+ void *pArg,
+ void *pP,
+ void *pX
+){
FILE *f = (FILE*)pArg;
+ UNUSED_PARAMETER(mType);
+ UNUSED_PARAMETER(pP);
if( f ){
+ const char *z = (const char*)pX;
int i = (int)strlen(z);
while( i>0 && z[i-1]==';' ){ i--; }
- fprintf(f, "%.*s;\n", i, z);
+ utf8_printf(f, "%.*s;\n", i, z);
}
+ return 0;
}
/*
@@ -2156,7 +2592,7 @@ static void import_append_char(ImportCtx *p, int c){
p->nAlloc += p->nAlloc + 100;
p->z = sqlite3_realloc64(p->z, p->nAlloc);
if( p->z==0 ){
- fprintf(stderr, "out of memory\n");
+ raw_printf(stderr, "out of memory\n");
exit(1);
}
}
@@ -2210,11 +2646,11 @@ static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
break;
}
if( pc==cQuote && c!='\r' ){
- fprintf(stderr, "%s:%d: unescaped %c character\n",
+ utf8_printf(stderr, "%s:%d: unescaped %c character\n",
p->zFile, p->nLine, cQuote);
}
if( c==EOF ){
- fprintf(stderr, "%s:%d: unterminated %c-quoted field\n",
+ utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
p->zFile, startLine, cQuote);
p->cTerm = c;
break;
@@ -2282,7 +2718,7 @@ static void tryToCloneData(
sqlite3 *newDb,
const char *zTable
){
- sqlite3_stmt *pQuery = 0;
+ sqlite3_stmt *pQuery = 0;
sqlite3_stmt *pInsert = 0;
char *zQuery = 0;
char *zInsert = 0;
@@ -2296,7 +2732,7 @@ static void tryToCloneData(
zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- fprintf(stderr, "Error %d: %s on [%s]\n",
+ utf8_printf(stderr, "Error %d: %s on [%s]\n",
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
zQuery);
goto end_data_xfer;
@@ -2304,7 +2740,7 @@ static void tryToCloneData(
n = sqlite3_column_count(pQuery);
zInsert = sqlite3_malloc64(200 + nTable + n*3);
if( zInsert==0 ){
- fprintf(stderr, "out of memory\n");
+ raw_printf(stderr, "out of memory\n");
goto end_data_xfer;
}
sqlite3_snprintf(200+nTable,zInsert,
@@ -2317,7 +2753,7 @@ static void tryToCloneData(
memcpy(zInsert+i, ");", 3);
rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
if( rc ){
- fprintf(stderr, "Error %d: %s on [%s]\n",
+ utf8_printf(stderr, "Error %d: %s on [%s]\n",
sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
zQuery);
goto end_data_xfer;
@@ -2354,7 +2790,7 @@ static void tryToCloneData(
} /* End for */
rc = sqlite3_step(pInsert);
if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
- fprintf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
+ utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
sqlite3_errmsg(newDb));
}
sqlite3_reset(pInsert);
@@ -2371,7 +2807,7 @@ static void tryToCloneData(
zTable);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- fprintf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
+ utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
break;
}
} /* End for(k=0...) */
@@ -2407,7 +2843,7 @@ static void tryToCloneSchema(
" WHERE %s", zWhere);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- fprintf(stderr, "Error: (%d) %s on [%s]\n",
+ utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
zQuery);
goto end_schema_xfer;
@@ -2418,7 +2854,7 @@ static void tryToCloneSchema(
printf("%s... ", zName); fflush(stdout);
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
if( zErrMsg ){
- fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
+ utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
sqlite3_free(zErrMsg);
zErrMsg = 0;
}
@@ -2434,7 +2870,7 @@ static void tryToCloneSchema(
" WHERE %s ORDER BY rowid DESC", zWhere);
rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
if( rc ){
- fprintf(stderr, "Error: (%d) %s on [%s]\n",
+ utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
zQuery);
goto end_schema_xfer;
@@ -2445,7 +2881,7 @@ static void tryToCloneSchema(
printf("%s... ", zName); fflush(stdout);
sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
if( zErrMsg ){
- fprintf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
+ utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
sqlite3_free(zErrMsg);
zErrMsg = 0;
}
@@ -2469,12 +2905,12 @@ static void tryToClone(ShellState *p, const char *zNewDb){
int rc;
sqlite3 *newDb = 0;
if( access(zNewDb,0)==0 ){
- fprintf(stderr, "File \"%s\" already exists.\n", zNewDb);
+ utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
return;
}
rc = sqlite3_open(zNewDb, &newDb);
if( rc ){
- fprintf(stderr, "Cannot create output database: %s\n",
+ utf8_printf(stderr, "Cannot create output database: %s\n",
sqlite3_errmsg(newDb));
}else{
sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
@@ -2519,10 +2955,10 @@ static int db_int(ShellState *p, const char *zSql){
/*
** Convert a 2-byte or 4-byte big-endian integer into a native integer
*/
-unsigned int get2byteInt(unsigned char *a){
+static unsigned int get2byteInt(unsigned char *a){
return (a[0]<<8) + a[1];
}
-unsigned int get4byteInt(unsigned char *a){
+static unsigned int get4byteInt(unsigned char *a){
return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
}
@@ -2558,7 +2994,7 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
{ "schema size:",
"SELECT total(length(sql)) FROM %s" },
};
- sqlite3_file *pFile;
+ sqlite3_file *pFile = 0;
int i;
char *zSchemaTab;
char *zDb = nArg>=2 ? azArg[1] : "main";
@@ -2571,27 +3007,27 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
}
i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
if( i!=SQLITE_OK ){
- fprintf(stderr, "unable to read database header\n");
+ raw_printf(stderr, "unable to read database header\n");
return 1;
}
i = get2byteInt(aHdr+16);
if( i==1 ) i = 65536;
- fprintf(p->out, "%-20s %d\n", "database page size:", i);
- fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
- fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
- fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
+ utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
+ utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
+ utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
+ utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
for(i=0; i<ArraySize(aField); i++){
int ofst = aField[i].ofst;
unsigned int val = get4byteInt(aHdr + ofst);
- fprintf(p->out, "%-20s %u", aField[i].zName, val);
+ utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
switch( ofst ){
case 56: {
- if( val==1 ) fprintf(p->out, " (utf8)");
- if( val==2 ) fprintf(p->out, " (utf16le)");
- if( val==3 ) fprintf(p->out, " (utf16be)");
+ if( val==1 ) raw_printf(p->out, " (utf8)");
+ if( val==2 ) raw_printf(p->out, " (utf16le)");
+ if( val==3 ) raw_printf(p->out, " (utf16be)");
}
}
- fprintf(p->out, "\n");
+ raw_printf(p->out, "\n");
}
if( zDb==0 ){
zSchemaTab = sqlite3_mprintf("main.sqlite_master");
@@ -2604,12 +3040,39 @@ static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
int val = db_int(p, zSql);
sqlite3_free(zSql);
- fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
+ utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
}
sqlite3_free(zSchemaTab);
return 0;
}
+/*
+** Print the current sqlite3_errmsg() value to stderr and return 1.
+*/
+static int shellDatabaseError(sqlite3 *db){
+ const char *zErr = sqlite3_errmsg(db);
+ utf8_printf(stderr, "Error: %s\n", zErr);
+ return 1;
+}
+
+/*
+** Print an out-of-memory message to stderr and return 1.
+*/
+static int shellNomemError(void){
+ raw_printf(stderr, "Error: out of memory\n");
+ return 1;
+}
+
+/*
+** Compare the string as a command-line option with either one or two
+** initial "-" characters.
+*/
+static int optionMatch(const char *zStr, const char *zOpt){
+ if( zStr[0]!='-' ) return 0;
+ zStr++;
+ if( zStr[0]=='-' ) zStr++;
+ return strcmp(zStr, zOpt)==0;
+}
/*
** If an input line begins with "." then invoke this routine to
@@ -2632,9 +3095,9 @@ static int do_meta_command(char *zLine, ShellState *p){
if( zLine[h]=='\'' || zLine[h]=='"' ){
int delim = zLine[h++];
azArg[nArg++] = &zLine[h];
- while( zLine[h] && zLine[h]!=delim ){
+ while( zLine[h] && zLine[h]!=delim ){
if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
- h++;
+ h++;
}
if( zLine[h]==delim ){
zLine[h++] = 0;
@@ -2653,6 +3116,21 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg==0 ) return 0; /* no tokens, no error */
n = strlen30(azArg[0]);
c = azArg[0][0];
+
+ if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
+ if( nArg!=2 ){
+ raw_printf(stderr, "Usage: .auth ON|OFF\n");
+ rc = 1;
+ goto meta_command_exit;
+ }
+ open_db(p, 0);
+ if( booleanValue(azArg[1]) ){
+ sqlite3_set_authorizer(p->db, shellAuth, p);
+ }else{
+ sqlite3_set_authorizer(p->db, 0, 0);
+ }
+ }else
+
if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
|| (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
){
@@ -2667,7 +3145,7 @@ static int do_meta_command(char *zLine, ShellState *p){
while( z[0]=='-' ) z++;
/* No options to process at this time */
{
- fprintf(stderr, "unknown option: %s\n", azArg[j]);
+ utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
return 1;
}
}else if( zDestFile==0 ){
@@ -2676,25 +3154,25 @@ static int do_meta_command(char *zLine, ShellState *p){
zDb = zDestFile;
zDestFile = azArg[j];
}else{
- fprintf(stderr, "too many arguments to .backup\n");
+ raw_printf(stderr, "too many arguments to .backup\n");
return 1;
}
}
if( zDestFile==0 ){
- fprintf(stderr, "missing FILENAME argument on .backup\n");
+ raw_printf(stderr, "missing FILENAME argument on .backup\n");
return 1;
}
if( zDb==0 ) zDb = "main";
rc = sqlite3_open(zDestFile, &pDest);
if( rc!=SQLITE_OK ){
- fprintf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
+ utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
sqlite3_close(pDest);
return 1;
}
open_db(p, 0);
pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
if( pBackup==0 ){
- fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
sqlite3_close(pDest);
return 1;
}
@@ -2703,7 +3181,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( rc==SQLITE_DONE ){
rc = 0;
}else{
- fprintf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
rc = 1;
}
sqlite3_close(pDest);
@@ -2713,7 +3191,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg==2 ){
bail_on_error = booleanValue(azArg[1]);
}else{
- fprintf(stderr, "Usage: .bail on|off\n");
+ raw_printf(stderr, "Usage: .bail on|off\n");
rc = 1;
}
}else
@@ -2721,12 +3199,12 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
if( nArg==2 ){
if( booleanValue(azArg[1]) ){
- setBinaryMode(p->out);
+ setBinaryMode(p->out, 1);
}else{
- setTextMode(p->out);
+ setTextMode(p->out, 1);
}
}else{
- fprintf(stderr, "Usage: .binary on|off\n");
+ raw_printf(stderr, "Usage: .binary on|off\n");
rc = 1;
}
}else
@@ -2738,11 +3216,20 @@ static int do_meta_command(char *zLine, ShellState *p){
test_breakpoint();
}else
+ if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
+ if( nArg==2 ){
+ p->countChanges = booleanValue(azArg[1]);
+ }else{
+ raw_printf(stderr, "Usage: .changes on|off\n");
+ rc = 1;
+ }
+ }else
+
if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
if( nArg==2 ){
tryToClone(p, azArg[1]);
}else{
- fprintf(stderr, "Usage: .clone FILENAME\n");
+ raw_printf(stderr, "Usage: .clone FILENAME\n");
rc = 1;
}
}else
@@ -2753,14 +3240,14 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
memcpy(&data, p, sizeof(data));
data.showHeader = 1;
- data.mode = MODE_Column;
+ data.cMode = data.mode = MODE_Column;
data.colWidth[0] = 3;
data.colWidth[1] = 15;
data.colWidth[2] = 58;
data.cnt = 0;
sqlite3_exec(p->db, "PRAGMA database_list; ", callback, &data, &zErrMsg);
if( zErrMsg ){
- fprintf(stderr,"Error: %s\n", zErrMsg);
+ utf8_printf(stderr,"Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}
@@ -2776,21 +3263,21 @@ static int do_meta_command(char *zLine, ShellState *p){
** which causes immediate foreign key constraints to be violated.
** So disable foreign-key constraint enforcement to prevent problems. */
if( nArg!=1 && nArg!=2 ){
- fprintf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
+ raw_printf(stderr, "Usage: .dump ?LIKE-PATTERN?\n");
rc = 1;
goto meta_command_exit;
}
- fprintf(p->out, "PRAGMA foreign_keys=OFF;\n");
- fprintf(p->out, "BEGIN TRANSACTION;\n");
+ raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
+ raw_printf(p->out, "BEGIN TRANSACTION;\n");
p->writableSchema = 0;
sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
p->nErr = 0;
if( nArg==1 ){
- run_schema_dump_query(p,
+ run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master "
"WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
);
- run_schema_dump_query(p,
+ run_schema_dump_query(p,
"SELECT name, type, sql FROM sqlite_master "
"WHERE name=='sqlite_sequence'"
);
@@ -2816,30 +3303,34 @@ static int do_meta_command(char *zLine, ShellState *p){
}
}
if( p->writableSchema ){
- fprintf(p->out, "PRAGMA writable_schema=OFF;\n");
+ raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
p->writableSchema = 0;
}
sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
- fprintf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
+ raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
}else
if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
if( nArg==2 ){
p->echoOn = booleanValue(azArg[1]);
}else{
- fprintf(stderr, "Usage: .echo on|off\n");
+ raw_printf(stderr, "Usage: .echo on|off\n");
rc = 1;
}
}else
if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
if( nArg==2 ){
- p->autoEQP = booleanValue(azArg[1]);
+ if( strcmp(azArg[1],"full")==0 ){
+ p->autoEQP = 2;
+ }else{
+ p->autoEQP = booleanValue(azArg[1]);
+ }
}else{
- fprintf(stderr, "Usage: .eqp on|off\n");
+ raw_printf(stderr, "Usage: .eqp on|off|full\n");
rc = 1;
- }
+ }
}else
if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
@@ -2848,37 +3339,24 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
- int val = nArg>=2 ? booleanValue(azArg[1]) : 1;
- if(val == 1) {
- if(!p->normalMode.valid) {
- p->normalMode.valid = 1;
- p->normalMode.mode = p->mode;
- p->normalMode.showHeader = p->showHeader;
- memcpy(p->normalMode.colWidth,p->colWidth,sizeof(p->colWidth));
+ int val = 1;
+ if( nArg>=2 ){
+ if( strcmp(azArg[1],"auto")==0 ){
+ val = 99;
+ }else{
+ val = booleanValue(azArg[1]);
}
- /* We could put this code under the !p->explainValid
- ** condition so that it does not execute if we are already in
- ** explain mode. However, always executing it allows us an easy
- ** was to reset to explain mode in case the user previously
- ** did an .explain followed by a .width, .mode or .header
- ** command.
- */
+ }
+ if( val==1 && p->mode!=MODE_Explain ){
+ p->normalMode = p->mode;
p->mode = MODE_Explain;
- p->showHeader = 1;
- memset(p->colWidth,0,sizeof(p->colWidth));
- p->colWidth[0] = 4; /* addr */
- p->colWidth[1] = 13; /* opcode */
- p->colWidth[2] = 4; /* P1 */
- p->colWidth[3] = 4; /* P2 */
- p->colWidth[4] = 4; /* P3 */
- p->colWidth[5] = 13; /* P4 */
- p->colWidth[6] = 2; /* P5 */
- p->colWidth[7] = 13; /* Comment */
- }else if (p->normalMode.valid) {
- p->normalMode.valid = 0;
- p->mode = p->normalMode.mode;
- p->showHeader = p->normalMode.showHeader;
- memcpy(p->colWidth,p->normalMode.colWidth,sizeof(p->colWidth));
+ p->autoExplain = 0;
+ }else if( val==0 ){
+ if( p->mode==MODE_Explain ) p->mode = p->normalMode;
+ p->autoExplain = 0;
+ }else if( val==99 ){
+ if( p->mode==MODE_Explain ) p->mode = p->normalMode;
+ p->autoExplain = 1;
}
}else
@@ -2886,15 +3364,19 @@ static int do_meta_command(char *zLine, ShellState *p){
ShellState data;
char *zErrMsg = 0;
int doStats = 0;
+ memcpy(&data, p, sizeof(data));
+ data.showHeader = 0;
+ data.cMode = data.mode = MODE_Semi;
+ if( nArg==2 && optionMatch(azArg[1], "indent") ){
+ data.cMode = data.mode = MODE_Pretty;
+ nArg = 1;
+ }
if( nArg!=1 ){
- fprintf(stderr, "Usage: .fullschema\n");
+ raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
rc = 1;
goto meta_command_exit;
}
open_db(p, 0);
- memcpy(&data, p, sizeof(data));
- data.showHeader = 0;
- data.mode = MODE_Semi;
rc = sqlite3_exec(p->db,
"SELECT sql FROM"
" (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
@@ -2914,12 +3396,12 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_finalize(pStmt);
}
if( doStats==0 ){
- fprintf(p->out, "/* No STAT tables available */\n");
+ raw_printf(p->out, "/* No STAT tables available */\n");
}else{
- fprintf(p->out, "ANALYZE sqlite_master;\n");
+ raw_printf(p->out, "ANALYZE sqlite_master;\n");
sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
callback, &data, &zErrMsg);
- data.mode = MODE_Insert;
+ data.cMode = data.mode = MODE_Insert;
data.zDestTable = "sqlite_stat1";
shell_exec(p->db, "SELECT * FROM sqlite_stat1",
shell_callback, &data,&zErrMsg);
@@ -2929,7 +3411,7 @@ static int do_meta_command(char *zLine, ShellState *p){
data.zDestTable = "sqlite_stat4";
shell_exec(p->db, "SELECT * FROM sqlite_stat4",
shell_callback, &data, &zErrMsg);
- fprintf(p->out, "ANALYZE sqlite_master;\n");
+ raw_printf(p->out, "ANALYZE sqlite_master;\n");
}
}else
@@ -2937,13 +3419,13 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg==2 ){
p->showHeader = booleanValue(azArg[1]);
}else{
- fprintf(stderr, "Usage: .headers on|off\n");
+ raw_printf(stderr, "Usage: .headers on|off\n");
rc = 1;
}
}else
if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
- fprintf(p->out, "%s", zHelp);
+ utf8_printf(p->out, "%s", zHelp);
}else
if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
@@ -2961,7 +3443,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
if( nArg!=3 ){
- fprintf(stderr, "Usage: .import FILE TABLE\n");
+ raw_printf(stderr, "Usage: .import FILE TABLE\n");
goto meta_command_exit;
}
zFile = azArg[1];
@@ -2971,17 +3453,18 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
nSep = strlen30(p->colSeparator);
if( nSep==0 ){
- fprintf(stderr, "Error: non-null column separator required for import\n");
+ raw_printf(stderr,
+ "Error: non-null column separator required for import\n");
return 1;
}
if( nSep>1 ){
- fprintf(stderr, "Error: multi-character column separators not allowed"
+ raw_printf(stderr, "Error: multi-character column separators not allowed"
" for import\n");
return 1;
}
nSep = strlen30(p->rowSeparator);
if( nSep==0 ){
- fprintf(stderr, "Error: non-null row separator required for import\n");
+ raw_printf(stderr, "Error: non-null row separator required for import\n");
return 1;
}
if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
@@ -2993,7 +3476,7 @@ static int do_meta_command(char *zLine, ShellState *p){
nSep = strlen30(p->rowSeparator);
}
if( nSep>1 ){
- fprintf(stderr, "Error: multi-character row separators not allowed"
+ raw_printf(stderr, "Error: multi-character row separators not allowed"
" for import\n");
return 1;
}
@@ -3001,7 +3484,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sCtx.nLine = 1;
if( sCtx.zFile[0]=='|' ){
#ifdef SQLITE_OMIT_POPEN
- fprintf(stderr, "Error: pipes are not supported in this OS\n");
+ raw_printf(stderr, "Error: pipes are not supported in this OS\n");
return 1;
#else
sCtx.in = popen(sCtx.zFile+1, "r");
@@ -3018,14 +3501,14 @@ static int do_meta_command(char *zLine, ShellState *p){
xRead = csv_read_one_field;
}
if( sCtx.in==0 ){
- fprintf(stderr, "Error: cannot open \"%s\"\n", zFile);
+ utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
return 1;
}
sCtx.cColSep = p->colSeparator[0];
sCtx.cRowSep = p->rowSeparator[0];
zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
if( zSql==0 ){
- fprintf(stderr, "Error: out of memory\n");
+ raw_printf(stderr, "Error: out of memory\n");
xCloser(sCtx.in);
return 1;
}
@@ -3036,7 +3519,7 @@ static int do_meta_command(char *zLine, ShellState *p){
char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
char cSep = '(';
while( xRead(&sCtx) ){
- zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCtx.z);
+ zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
cSep = ',';
if( sCtx.cTerm!=sCtx.cColSep ) break;
}
@@ -3044,14 +3527,14 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_free(zCreate);
sqlite3_free(sCtx.z);
xCloser(sCtx.in);
- fprintf(stderr,"%s: empty file\n", sCtx.zFile);
+ utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
return 1;
}
zCreate = sqlite3_mprintf("%z\n)", zCreate);
rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
sqlite3_free(zCreate);
if( rc ){
- fprintf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
+ utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
sqlite3_errmsg(p->db));
sqlite3_free(sCtx.z);
xCloser(sCtx.in);
@@ -3062,7 +3545,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_free(zSql);
if( rc ){
if (pStmt) sqlite3_finalize(pStmt);
- fprintf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
+ utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
xCloser(sCtx.in);
return 1;
}
@@ -3072,7 +3555,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nCol==0 ) return 0; /* no columns, no error */
zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
if( zSql==0 ){
- fprintf(stderr, "Error: out of memory\n");
+ raw_printf(stderr, "Error: out of memory\n");
xCloser(sCtx.in);
return 1;
}
@@ -3087,7 +3570,7 @@ static int do_meta_command(char *zLine, ShellState *p){
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
sqlite3_free(zSql);
if( rc ){
- fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
if (pStmt) sqlite3_finalize(pStmt);
xCloser(sCtx.in);
return 1;
@@ -3111,7 +3594,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
- fprintf(stderr, "%s:%d: expected %d columns but found %d - "
+ utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
"filling the rest with NULL\n",
sCtx.zFile, startLine, nCol, i+1);
i += 2;
@@ -3123,7 +3606,7 @@ static int do_meta_command(char *zLine, ShellState *p){
xRead(&sCtx);
i++;
}while( sCtx.cTerm==sCtx.cColSep );
- fprintf(stderr, "%s:%d: expected %d columns but found %d - "
+ utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
"extras ignored\n",
sCtx.zFile, startLine, nCol, i);
}
@@ -3131,8 +3614,8 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_step(pStmt);
rc = sqlite3_reset(pStmt);
if( rc!=SQLITE_OK ){
- fprintf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile, startLine,
- sqlite3_errmsg(p->db));
+ utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
+ startLine, sqlite3_errmsg(p->db));
}
}
}while( sCtx.cTerm!=EOF );
@@ -3150,7 +3633,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
memcpy(&data, p, sizeof(data));
data.showHeader = 0;
- data.mode = MODE_List;
+ data.cMode = data.mode = MODE_List;
if( nArg==1 ){
rc = sqlite3_exec(p->db,
"SELECT name FROM sqlite_master "
@@ -3174,16 +3657,17 @@ static int do_meta_command(char *zLine, ShellState *p){
);
zShellStatic = 0;
}else{
- fprintf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
+ raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
rc = 1;
goto meta_command_exit;
}
if( zErrMsg ){
- fprintf(stderr,"Error: %s\n", zErrMsg);
+ utf8_printf(stderr,"Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}else if( rc != SQLITE_OK ){
- fprintf(stderr,"Error: querying sqlite_master and sqlite_temp_master\n");
+ raw_printf(stderr,
+ "Error: querying sqlite_master and sqlite_temp_master\n");
rc = 1;
}
}else
@@ -3201,7 +3685,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}else{
iotrace = fopen(azArg[1], "w");
if( iotrace==0 ){
- fprintf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
+ utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
sqlite3IoTrace = 0;
rc = 1;
}else{
@@ -3232,11 +3716,11 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
if( nArg==1 ){
for(i=0; i<ArraySize(aLimit); i++){
- printf("%20s %d\n", aLimit[i].zLimitName,
+ printf("%20s %d\n", aLimit[i].zLimitName,
sqlite3_limit(p->db, aLimit[i].limitCode, -1));
}
}else if( nArg>3 ){
- fprintf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
+ raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
rc = 1;
goto meta_command_exit;
}else{
@@ -3247,14 +3731,14 @@ static int do_meta_command(char *zLine, ShellState *p){
if( iLimit<0 ){
iLimit = i;
}else{
- fprintf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
+ utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
rc = 1;
goto meta_command_exit;
}
}
}
if( iLimit<0 ){
- fprintf(stderr, "unknown limit: \"%s\"\n"
+ utf8_printf(stderr, "unknown limit: \"%s\"\n"
"enter \".limits\" with no arguments for a list.\n",
azArg[1]);
rc = 1;
@@ -3274,7 +3758,7 @@ static int do_meta_command(char *zLine, ShellState *p){
const char *zFile, *zProc;
char *zErrMsg = 0;
if( nArg<2 ){
- fprintf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
+ raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
rc = 1;
goto meta_command_exit;
}
@@ -3283,7 +3767,7 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
if( rc!=SQLITE_OK ){
- fprintf(stderr, "Error: %s\n", zErrMsg);
+ utf8_printf(stderr, "Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}
@@ -3292,7 +3776,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
if( nArg!=2 ){
- fprintf(stderr, "Usage: .log FILENAME\n");
+ raw_printf(stderr, "Usage: .log FILENAME\n");
rc = 1;
}else{
const char *zFile = azArg[1];
@@ -3331,10 +3815,11 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
}else {
- fprintf(stderr,"Error: mode should be one of: "
+ raw_printf(stderr, "Error: mode should be one of: "
"ascii column csv html insert line list tabs tcl\n");
rc = 1;
}
+ p->cMode = p->mode;
}else
if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
@@ -3342,7 +3827,7 @@ static int do_meta_command(char *zLine, ShellState *p){
sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
"%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
}else{
- fprintf(stderr, "Usage: .nullvalue STRING\n");
+ raw_printf(stderr, "Usage: .nullvalue STRING\n");
rc = 1;
}
}else
@@ -3356,6 +3841,7 @@ static int do_meta_command(char *zLine, ShellState *p){
p->zDbFilename = zNewFilename;
open_db(p, 1);
if( p->db!=0 ){
+ session_close_all(p);
sqlite3_close(savedDb);
sqlite3_free(p->zFreeOnClose);
p->zFreeOnClose = zNewFilename;
@@ -3371,13 +3857,13 @@ static int do_meta_command(char *zLine, ShellState *p){
){
const char *zFile = nArg>=2 ? azArg[1] : "stdout";
if( nArg>2 ){
- fprintf(stderr, "Usage: .%s FILE\n", azArg[0]);
+ utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
rc = 1;
goto meta_command_exit;
}
if( n>1 && strncmp(azArg[0], "once", n)==0 ){
if( nArg<2 ){
- fprintf(stderr, "Usage: .once FILE\n");
+ raw_printf(stderr, "Usage: .once FILE\n");
rc = 1;
goto meta_command_exit;
}
@@ -3388,13 +3874,13 @@ static int do_meta_command(char *zLine, ShellState *p){
output_reset(p);
if( zFile[0]=='|' ){
#ifdef SQLITE_OMIT_POPEN
- fprintf(stderr,"Error: pipes are not supported in this OS\n");
+ raw_printf(stderr, "Error: pipes are not supported in this OS\n");
rc = 1;
p->out = stdout;
#else
p->out = popen(zFile + 1, "w");
if( p->out==0 ){
- fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
+ utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
p->out = stdout;
rc = 1;
}else{
@@ -3405,7 +3891,7 @@ static int do_meta_command(char *zLine, ShellState *p){
p->out = output_file_open(zFile);
if( p->out==0 ){
if( strcmp(zFile,"off")!=0 ){
- fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
+ utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
}
p->out = stdout;
rc = 1;
@@ -3418,10 +3904,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
int i;
for(i=1; i<nArg; i++){
- if( i>1 ) fprintf(p->out, " ");
- fprintf(p->out, "%s", azArg[i]);
+ if( i>1 ) raw_printf(p->out, " ");
+ utf8_printf(p->out, "%s", azArg[i]);
}
- fprintf(p->out, "\n");
+ raw_printf(p->out, "\n");
}else
if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
@@ -3440,13 +3926,13 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
FILE *alt;
if( nArg!=2 ){
- fprintf(stderr, "Usage: .read FILE\n");
+ raw_printf(stderr, "Usage: .read FILE\n");
rc = 1;
goto meta_command_exit;
}
alt = fopen(azArg[1], "rb");
if( alt==0 ){
- fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
+ utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
rc = 1;
}else{
rc = process_input(p, alt);
@@ -3468,20 +3954,20 @@ static int do_meta_command(char *zLine, ShellState *p){
zSrcFile = azArg[2];
zDb = azArg[1];
}else{
- fprintf(stderr, "Usage: .restore ?DB? FILE\n");
+ raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
rc = 1;
goto meta_command_exit;
}
rc = sqlite3_open(zSrcFile, &pSrc);
if( rc!=SQLITE_OK ){
- fprintf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
+ utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
sqlite3_close(pSrc);
return 1;
}
open_db(p, 0);
pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
if( pBackup==0 ){
- fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
sqlite3_close(pSrc);
return 1;
}
@@ -3496,10 +3982,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( rc==SQLITE_DONE ){
rc = 0;
}else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
- fprintf(stderr, "Error: source database is busy\n");
+ raw_printf(stderr, "Error: source database is busy\n");
rc = 1;
}else{
- fprintf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
+ utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
rc = 1;
}
sqlite3_close(pSrc);
@@ -3510,10 +3996,10 @@ static int do_meta_command(char *zLine, ShellState *p){
if( nArg==2 ){
p->scanstatsOn = booleanValue(azArg[1]);
#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
- fprintf(stderr, "Warning: .scanstats not available in this build.\n");
+ raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
#endif
}else{
- fprintf(stderr, "Usage: .scanstats on|off\n");
+ raw_printf(stderr, "Usage: .scanstats on|off\n");
rc = 1;
}
}else
@@ -3524,8 +4010,13 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
memcpy(&data, p, sizeof(data));
data.showHeader = 0;
- data.mode = MODE_Semi;
- if( nArg==2 ){
+ data.cMode = data.mode = MODE_Semi;
+ if( nArg>=2 && optionMatch(azArg[1], "indent") ){
+ data.cMode = data.mode = MODE_Pretty;
+ nArg--;
+ if( nArg==2 ) azArg[1] = azArg[2];
+ }
+ if( nArg==2 && azArg[1][0]!='-' ){
int i;
for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
if( strcmp(azArg[1],"sqlite_master")==0 ){
@@ -3580,30 +4071,223 @@ static int do_meta_command(char *zLine, ShellState *p){
callback, &data, &zErrMsg
);
}else{
- fprintf(stderr, "Usage: .schema ?LIKE-PATTERN?\n");
+ raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
rc = 1;
goto meta_command_exit;
}
if( zErrMsg ){
- fprintf(stderr,"Error: %s\n", zErrMsg);
+ utf8_printf(stderr,"Error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
rc = 1;
}else if( rc != SQLITE_OK ){
- fprintf(stderr,"Error: querying schema information\n");
+ raw_printf(stderr,"Error: querying schema information\n");
rc = 1;
}else{
rc = 0;
}
}else
-
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
- extern int sqlite3SelectTrace;
sqlite3SelectTrace = integerValue(azArg[1]);
}else
#endif
+#if defined(SQLITE_ENABLE_SESSION)
+ if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
+ OpenSession *pSession = &p->aSession[0];
+ char **azCmd = &azArg[1];
+ int iSes = 0;
+ int nCmd = nArg - 1;
+ int i;
+ if( nArg<=1 ) goto session_syntax_error;
+ open_db(p, 0);
+ if( nArg>=3 ){
+ for(iSes=0; iSes<p->nSession; iSes++){
+ if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
+ }
+ if( iSes<p->nSession ){
+ pSession = &p->aSession[iSes];
+ azCmd++;
+ nCmd--;
+ }else{
+ pSession = &p->aSession[0];
+ iSes = 0;
+ }
+ }
+
+ /* .session attach TABLE
+ ** Invoke the sqlite3session_attach() interface to attach a particular
+ ** table so that it is never filtered.
+ */
+ if( strcmp(azCmd[0],"attach")==0 ){
+ if( nCmd!=2 ) goto session_syntax_error;
+ if( pSession->p==0 ){
+ session_not_open:
+ raw_printf(stderr, "ERROR: No sessions are open\n");
+ }else{
+ rc = sqlite3session_attach(pSession->p, azCmd[1]);
+ if( rc ){
+ raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
+ rc = 0;
+ }
+ }
+ }else
+
+ /* .session changeset FILE
+ ** .session patchset FILE
+ ** Write a changeset or patchset into a file. The file is overwritten.
+ */
+ if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
+ FILE *out = 0;
+ if( nCmd!=2 ) goto session_syntax_error;
+ if( pSession->p==0 ) goto session_not_open;
+ out = fopen(azCmd[1], "wb");
+ if( out==0 ){
+ utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
+ }else{
+ int szChng;
+ void *pChng;
+ if( azCmd[0][0]=='c' ){
+ rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
+ }else{
+ rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
+ }
+ if( rc ){
+ printf("Error: error code %d\n", rc);
+ rc = 0;
+ }
+ if( pChng
+ && fwrite(pChng, szChng, 1, out)!=1 ){
+ raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
+ szChng);
+ }
+ sqlite3_free(pChng);
+ fclose(out);
+ }
+ }else
+
+ /* .session close
+ ** Close the identified session
+ */
+ if( strcmp(azCmd[0], "close")==0 ){
+ if( nCmd!=1 ) goto session_syntax_error;
+ if( p->nSession ){
+ session_close(pSession);
+ p->aSession[iSes] = p->aSession[--p->nSession];
+ }
+ }else
+
+ /* .session enable ?BOOLEAN?
+ ** Query or set the enable flag
+ */
+ if( strcmp(azCmd[0], "enable")==0 ){
+ int ii;
+ if( nCmd>2 ) goto session_syntax_error;
+ ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
+ if( p->nSession ){
+ ii = sqlite3session_enable(pSession->p, ii);
+ utf8_printf(p->out, "session %s enable flag = %d\n",
+ pSession->zName, ii);
+ }
+ }else
+
+ /* .session filter GLOB ....
+ ** Set a list of GLOB patterns of table names to be excluded.
+ */
+ if( strcmp(azCmd[0], "filter")==0 ){
+ int ii, nByte;
+ if( nCmd<2 ) goto session_syntax_error;
+ if( p->nSession ){
+ for(ii=0; ii<pSession->nFilter; ii++){
+ sqlite3_free(pSession->azFilter[ii]);
+ }
+ sqlite3_free(pSession->azFilter);
+ nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
+ pSession->azFilter = sqlite3_malloc( nByte );
+ if( pSession->azFilter==0 ){
+ raw_printf(stderr, "Error: out or memory\n");
+ exit(1);
+ }
+ for(ii=1; ii<nCmd; ii++){
+ pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
+ }
+ pSession->nFilter = ii-1;
+ }
+ }else
+
+ /* .session indirect ?BOOLEAN?
+ ** Query or set the indirect flag
+ */
+ if( strcmp(azCmd[0], "indirect")==0 ){
+ int ii;
+ if( nCmd>2 ) goto session_syntax_error;
+ ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
+ if( p->nSession ){
+ ii = sqlite3session_indirect(pSession->p, ii);
+ utf8_printf(p->out, "session %s indirect flag = %d\n",
+ pSession->zName, ii);
+ }
+ }else
+
+ /* .session isempty
+ ** Determine if the session is empty
+ */
+ if( strcmp(azCmd[0], "isempty")==0 ){
+ int ii;
+ if( nCmd!=1 ) goto session_syntax_error;
+ if( p->nSession ){
+ ii = sqlite3session_isempty(pSession->p);
+ utf8_printf(p->out, "session %s isempty flag = %d\n",
+ pSession->zName, ii);
+ }
+ }else
+
+ /* .session list
+ ** List all currently open sessions
+ */
+ if( strcmp(azCmd[0],"list")==0 ){
+ for(i=0; i<p->nSession; i++){
+ utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
+ }
+ }else
+
+ /* .session open DB NAME
+ ** Open a new session called NAME on the attached database DB.
+ ** DB is normally "main".
+ */
+ if( strcmp(azCmd[0],"open")==0 ){
+ char *zName;
+ if( nCmd!=3 ) goto session_syntax_error;
+ zName = azCmd[2];
+ if( zName[0]==0 ) goto session_syntax_error;
+ for(i=0; i<p->nSession; i++){
+ if( strcmp(p->aSession[i].zName,zName)==0 ){
+ utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
+ goto meta_command_exit;
+ }
+ }
+ if( p->nSession>=ArraySize(p->aSession) ){
+ raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
+ goto meta_command_exit;
+ }
+ pSession = &p->aSession[p->nSession];
+ rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
+ if( rc ){
+ raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
+ rc = 0;
+ goto meta_command_exit;
+ }
+ pSession->nFilter = 0;
+ sqlite3session_table_filter(pSession->p, session_filter, pSession);
+ p->nSession++;
+ pSession->zName = sqlite3_mprintf("%s", zName);
+ }else
+ /* If no command name matches, show a syntax error */
+ session_syntax_error:
+ session_help(p);
+ }else
+#endif
#ifdef SQLITE_DEBUG
/* Undocumented commands for internal testing. Subject to change
@@ -3613,7 +4297,7 @@ static int do_meta_command(char *zLine, ShellState *p){
int i, v;
for(i=1; i<nArg; i++){
v = booleanValue(azArg[i]);
- fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
+ utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
}
}
if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
@@ -3622,7 +4306,7 @@ static int do_meta_command(char *zLine, ShellState *p){
char zBuf[200];
v = integerValue(azArg[i]);
sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
- fprintf(p->out, "%s", zBuf);
+ utf8_printf(p->out, "%s", zBuf);
}
}
}else
@@ -3630,7 +4314,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
if( nArg<2 || nArg>3 ){
- fprintf(stderr, "Usage: .separator COL ?ROW?\n");
+ raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
rc = 1;
}
if( nArg>=2 ){
@@ -3649,7 +4333,7 @@ static int do_meta_command(char *zLine, ShellState *p){
char *zCmd;
int i, x;
if( nArg<2 ){
- fprintf(stderr, "Usage: .system COMMAND\n");
+ raw_printf(stderr, "Usage: .system COMMAND\n");
rc = 1;
goto meta_command_exit;
}
@@ -3660,45 +4344,49 @@ static int do_meta_command(char *zLine, ShellState *p){
}
x = system(zCmd);
sqlite3_free(zCmd);
- if( x ) fprintf(stderr, "System command returns %d\n", x);
+ if( x ) raw_printf(stderr, "System command returns %d\n", x);
}else
if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
+ static const char *azBool[] = { "off", "on", "full", "unk" };
int i;
if( nArg!=1 ){
- fprintf(stderr, "Usage: .show\n");
+ raw_printf(stderr, "Usage: .show\n");
rc = 1;
goto meta_command_exit;
}
- fprintf(p->out,"%12.12s: %s\n","echo", p->echoOn ? "on" : "off");
- fprintf(p->out,"%12.12s: %s\n","eqp", p->autoEQP ? "on" : "off");
- fprintf(p->out,"%9.9s: %s\n","explain", p->normalMode.valid ? "on" :"off");
- fprintf(p->out,"%12.12s: %s\n","headers", p->showHeader ? "on" : "off");
- fprintf(p->out,"%12.12s: %s\n","mode", modeDescr[p->mode]);
- fprintf(p->out,"%12.12s: ", "nullvalue");
+ utf8_printf(p->out, "%12.12s: %s\n","echo", azBool[p->echoOn!=0]);
+ utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
+ utf8_printf(p->out, "%12.12s: %s\n","explain",
+ p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
+ utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
+ utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
+ utf8_printf(p->out, "%12.12s: ", "nullvalue");
output_c_string(p->out, p->nullValue);
- fprintf(p->out, "\n");
- fprintf(p->out,"%12.12s: %s\n","output",
+ raw_printf(p->out, "\n");
+ utf8_printf(p->out,"%12.12s: %s\n","output",
strlen30(p->outfile) ? p->outfile : "stdout");
- fprintf(p->out,"%12.12s: ", "colseparator");
+ utf8_printf(p->out,"%12.12s: ", "colseparator");
output_c_string(p->out, p->colSeparator);
- fprintf(p->out, "\n");
- fprintf(p->out,"%12.12s: ", "rowseparator");
+ raw_printf(p->out, "\n");
+ utf8_printf(p->out,"%12.12s: ", "rowseparator");
output_c_string(p->out, p->rowSeparator);
- fprintf(p->out, "\n");
- fprintf(p->out,"%12.12s: %s\n","stats", p->statsOn ? "on" : "off");
- fprintf(p->out,"%12.12s: ","width");
+ raw_printf(p->out, "\n");
+ utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
+ utf8_printf(p->out, "%12.12s: ", "width");
for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
- fprintf(p->out,"%d ",p->colWidth[i]);
+ raw_printf(p->out, "%d ", p->colWidth[i]);
}
- fprintf(p->out,"\n");
+ raw_printf(p->out, "\n");
}else
if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
if( nArg==2 ){
p->statsOn = booleanValue(azArg[1]);
+ }else if( nArg==1 ){
+ display_stats(p->db, p, 0);
}else{
- fprintf(stderr, "Usage: .stats on|off\n");
+ raw_printf(stderr, "Usage: .stats ?on|off?\n");
rc = 1;
}
}else
@@ -3711,13 +4399,17 @@ static int do_meta_command(char *zLine, ShellState *p){
int ii;
open_db(p, 0);
rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
- if( rc ) return rc;
+ if( rc ) return shellDatabaseError(p->db);
+
+ /* Create an SQL statement to query for the list of tables in the
+ ** main and all attached databases where the table name matches the
+ ** LIKE pattern bound to variable "?1". */
zSql = sqlite3_mprintf(
"SELECT name FROM sqlite_master"
" WHERE type IN ('table','view')"
" AND name NOT LIKE 'sqlite_%%'"
" AND name LIKE ?1");
- while( sqlite3_step(pStmt)==SQLITE_ROW ){
+ while( zSql && sqlite3_step(pStmt)==SQLITE_ROW ){
const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
if( zDbName==0 || strcmp(zDbName,"main")==0 ) continue;
if( strcmp(zDbName,"temp")==0 ){
@@ -3736,11 +4428,17 @@ static int do_meta_command(char *zLine, ShellState *p){
" AND name LIKE ?1", zSql, zDbName, zDbName);
}
}
- sqlite3_finalize(pStmt);
- zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
- rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
+ rc = sqlite3_finalize(pStmt);
+ if( zSql && rc==SQLITE_OK ){
+ zSql = sqlite3_mprintf("%z ORDER BY 1", zSql);
+ if( zSql ) rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
+ }
sqlite3_free(zSql);
- if( rc ) return rc;
+ if( !zSql ) return shellNomemError();
+ if( rc ) return shellDatabaseError(p->db);
+
+ /* Run the SQL statement prepared by the above block. Store the results
+ ** as an array of nul-terminated strings in azResult[]. */
nRow = nAlloc = 0;
azResult = 0;
if( nArg>1 ){
@@ -3754,17 +4452,25 @@ static int do_meta_command(char *zLine, ShellState *p){
int n2 = nAlloc*2 + 10;
azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
if( azNew==0 ){
- fprintf(stderr, "Error: out of memory\n");
+ rc = shellNomemError();
break;
}
nAlloc = n2;
azResult = azNew;
}
azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
- if( azResult[nRow] ) nRow++;
+ if( 0==azResult[nRow] ){
+ rc = shellNomemError();
+ break;
+ }
+ nRow++;
+ }
+ if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
+ rc = shellDatabaseError(p->db);
}
- sqlite3_finalize(pStmt);
- if( nRow>0 ){
+
+ /* Pretty-print the contents of array azResult[] to the output */
+ if( rc==0 && nRow>0 ){
int len, maxlen = 0;
int i, j;
int nPrintCol, nPrintRow;
@@ -3778,11 +4484,13 @@ static int do_meta_command(char *zLine, ShellState *p){
for(i=0; i<nPrintRow; i++){
for(j=i; j<nRow; j+=nPrintRow){
char *zSp = j<nPrintRow ? "" : " ";
- fprintf(p->out, "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
+ utf8_printf(p->out, "%s%-*s", zSp, maxlen,
+ azResult[j] ? azResult[j]:"");
}
- fprintf(p->out, "\n");
+ raw_printf(p->out, "\n");
}
}
+
for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
sqlite3_free(azResult);
}else
@@ -3822,7 +4530,7 @@ static int do_meta_command(char *zLine, ShellState *p){
if( testctrl<0 ){
testctrl = aCtrl[i].ctrlCode;
}else{
- fprintf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
+ utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
testctrl = -1;
break;
}
@@ -3830,19 +4538,19 @@ static int do_meta_command(char *zLine, ShellState *p){
}
if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
- fprintf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
+ utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
}else{
switch(testctrl){
/* sqlite3_test_control(int, db, int) */
case SQLITE_TESTCTRL_OPTIMIZATIONS:
- case SQLITE_TESTCTRL_RESERVE:
+ case SQLITE_TESTCTRL_RESERVE:
if( nArg==3 ){
- int opt = (int)strtol(azArg[2], 0, 0);
+ int opt = (int)strtol(azArg[2], 0, 0);
rc2 = sqlite3_test_control(testctrl, p->db, opt);
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
} else {
- fprintf(stderr,"Error: testctrl %s takes a single int option\n",
+ utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
}
break;
@@ -3854,71 +4562,74 @@ static int do_meta_command(char *zLine, ShellState *p){
case SQLITE_TESTCTRL_BYTEORDER:
if( nArg==2 ){
rc2 = sqlite3_test_control(testctrl);
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
} else {
- fprintf(stderr,"Error: testctrl %s takes no options\n", azArg[1]);
+ utf8_printf(stderr,"Error: testctrl %s takes no options\n",
+ azArg[1]);
}
break;
/* sqlite3_test_control(int, uint) */
- case SQLITE_TESTCTRL_PENDING_BYTE:
+ case SQLITE_TESTCTRL_PENDING_BYTE:
if( nArg==3 ){
unsigned int opt = (unsigned int)integerValue(azArg[2]);
rc2 = sqlite3_test_control(testctrl, opt);
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
} else {
- fprintf(stderr,"Error: testctrl %s takes a single unsigned"
+ utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
" int option\n", azArg[1]);
}
break;
-
+
/* sqlite3_test_control(int, int) */
- case SQLITE_TESTCTRL_ASSERT:
- case SQLITE_TESTCTRL_ALWAYS:
- case SQLITE_TESTCTRL_NEVER_CORRUPT:
+ case SQLITE_TESTCTRL_ASSERT:
+ case SQLITE_TESTCTRL_ALWAYS:
+ case SQLITE_TESTCTRL_NEVER_CORRUPT:
if( nArg==3 ){
- int opt = booleanValue(azArg[2]);
+ int opt = booleanValue(azArg[2]);
rc2 = sqlite3_test_control(testctrl, opt);
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
} else {
- fprintf(stderr,"Error: testctrl %s takes a single int option\n",
+ utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
azArg[1]);
}
break;
/* sqlite3_test_control(int, char *) */
#ifdef SQLITE_N_KEYWORD
- case SQLITE_TESTCTRL_ISKEYWORD:
+ case SQLITE_TESTCTRL_ISKEYWORD:
if( nArg==3 ){
- const char *opt = azArg[2];
+ const char *opt = azArg[2];
rc2 = sqlite3_test_control(testctrl, opt);
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
} else {
- fprintf(stderr,"Error: testctrl %s takes a single char * option\n",
- azArg[1]);
+ utf8_printf(stderr,
+ "Error: testctrl %s takes a single char * option\n",
+ azArg[1]);
}
break;
#endif
case SQLITE_TESTCTRL_IMPOSTER:
if( nArg==5 ){
- rc2 = sqlite3_test_control(testctrl, p->db,
+ rc2 = sqlite3_test_control(testctrl, p->db,
azArg[2],
integerValue(azArg[3]),
integerValue(azArg[4]));
- fprintf(p->out, "%d (0x%08x)\n", rc2, rc2);
+ raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
}else{
- fprintf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
+ raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
}
break;
- case SQLITE_TESTCTRL_BITVEC_TEST:
- case SQLITE_TESTCTRL_FAULT_INSTALL:
- case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
- case SQLITE_TESTCTRL_SCRATCHMALLOC:
+ case SQLITE_TESTCTRL_BITVEC_TEST:
+ case SQLITE_TESTCTRL_FAULT_INSTALL:
+ case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
+ case SQLITE_TESTCTRL_SCRATCHMALLOC:
default:
- fprintf(stderr,"Error: CLI support for testctrl %s not implemented\n",
- azArg[1]);
+ utf8_printf(stderr,
+ "Error: CLI support for testctrl %s not implemented\n",
+ azArg[1]);
break;
}
}
@@ -3928,24 +4639,24 @@ static int do_meta_command(char *zLine, ShellState *p){
open_db(p, 0);
sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
}else
-
+
if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
if( nArg==2 ){
enableTimer = booleanValue(azArg[1]);
if( enableTimer && !HAS_TIMER ){
- fprintf(stderr, "Error: timer not available on this system.\n");
+ raw_printf(stderr, "Error: timer not available on this system.\n");
enableTimer = 0;
}
}else{
- fprintf(stderr, "Usage: .timer on|off\n");
+ raw_printf(stderr, "Usage: .timer on|off\n");
rc = 1;
}
}else
-
+
if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
open_db(p, 0);
if( nArg!=2 ){
- fprintf(stderr, "Usage: .trace FILE|off\n");
+ raw_printf(stderr, "Usage: .trace FILE|off\n");
rc = 1;
goto meta_command_exit;
}
@@ -3953,9 +4664,9 @@ static int do_meta_command(char *zLine, ShellState *p){
p->traceOut = output_file_open(azArg[1]);
#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
if( p->traceOut==0 ){
- sqlite3_trace(p->db, 0, 0);
+ sqlite3_trace_v2(p->db, 0, 0, 0);
}else{
- sqlite3_trace(p->db, sql_trace_callback, p->traceOut);
+ sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
}
#endif
}else
@@ -3963,26 +4674,26 @@ static int do_meta_command(char *zLine, ShellState *p){
#if SQLITE_USER_AUTHENTICATION
if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
if( nArg<2 ){
- fprintf(stderr, "Usage: .user SUBCOMMAND ...\n");
+ raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
rc = 1;
goto meta_command_exit;
}
open_db(p, 0);
if( strcmp(azArg[1],"login")==0 ){
if( nArg!=4 ){
- fprintf(stderr, "Usage: .user login USER PASSWORD\n");
+ raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
rc = 1;
goto meta_command_exit;
}
rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
(int)strlen(azArg[3]));
if( rc ){
- fprintf(stderr, "Authentication failed for user %s\n", azArg[2]);
+ utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
rc = 1;
}
}else if( strcmp(azArg[1],"add")==0 ){
if( nArg!=5 ){
- fprintf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
+ raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
rc = 1;
goto meta_command_exit;
}
@@ -3990,12 +4701,12 @@ static int do_meta_command(char *zLine, ShellState *p){
azArg[3], (int)strlen(azArg[3]),
booleanValue(azArg[4]));
if( rc ){
- fprintf(stderr, "User-Add failed: %d\n", rc);
+ raw_printf(stderr, "User-Add failed: %d\n", rc);
rc = 1;
}
}else if( strcmp(azArg[1],"edit")==0 ){
if( nArg!=5 ){
- fprintf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
+ raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
rc = 1;
goto meta_command_exit;
}
@@ -4003,40 +4714,72 @@ static int do_meta_command(char *zLine, ShellState *p){
azArg[3], (int)strlen(azArg[3]),
booleanValue(azArg[4]));
if( rc ){
- fprintf(stderr, "User-Edit failed: %d\n", rc);
+ raw_printf(stderr, "User-Edit failed: %d\n", rc);
rc = 1;
}
}else if( strcmp(azArg[1],"delete")==0 ){
if( nArg!=3 ){
- fprintf(stderr, "Usage: .user delete USER\n");
+ raw_printf(stderr, "Usage: .user delete USER\n");
rc = 1;
goto meta_command_exit;
}
rc = sqlite3_user_delete(p->db, azArg[2]);
if( rc ){
- fprintf(stderr, "User-Delete failed: %d\n", rc);
+ raw_printf(stderr, "User-Delete failed: %d\n", rc);
rc = 1;
}
}else{
- fprintf(stderr, "Usage: .user login|add|edit|delete ...\n");
+ raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
rc = 1;
goto meta_command_exit;
- }
+ }
}else
#endif /* SQLITE_USER_AUTHENTICATION */
if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
- fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
+ utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
sqlite3_libversion(), sqlite3_sourceid());
}else
+ if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
+ const char *zDbName = nArg==2 ? azArg[1] : "main";
+ sqlite3_vfs *pVfs;
+ if( p->db ){
+ sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
+ if( pVfs ){
+ utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
+ raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
+ raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
+ raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
+ }
+ }
+ }else
+
+ if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
+ sqlite3_vfs *pVfs;
+ sqlite3_vfs *pCurrent = 0;
+ if( p->db ){
+ sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
+ }
+ for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
+ utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
+ pVfs==pCurrent ? " <--- CURRENT" : "");
+ raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
+ raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
+ raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
+ if( pVfs->pNext ){
+ raw_printf(p->out, "-----------------------------------\n");
+ }
+ }
+ }else
+
if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
const char *zDbName = nArg==2 ? azArg[1] : "main";
char *zVfsName = 0;
if( p->db ){
sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
if( zVfsName ){
- fprintf(p->out, "%s\n", zVfsName);
+ utf8_printf(p->out, "%s\n", zVfsName);
sqlite3_free(zVfsName);
}
}
@@ -4044,7 +4787,6 @@ static int do_meta_command(char *zLine, ShellState *p){
#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
- extern int sqlite3WhereTrace;
sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
}else
#endif
@@ -4058,7 +4800,7 @@ static int do_meta_command(char *zLine, ShellState *p){
}else
{
- fprintf(stderr, "Error: unknown command or invalid arguments: "
+ utf8_printf(stderr, "Error: unknown command or invalid arguments: "
" \"%s\". Enter \".help\" for help\n", azArg[0]);
rc = 1;
}
@@ -4193,7 +4935,7 @@ static int process_input(ShellState *p, FILE *in){
nAlloc = nSql+nLine+100;
zSql = realloc(zSql, nAlloc);
if( zSql==0 ){
- fprintf(stderr, "Error: out of memory\n");
+ raw_printf(stderr, "Error: out of memory\n");
exit(1);
}
}
@@ -4221,19 +4963,22 @@ static int process_input(ShellState *p, FILE *in){
if( rc || zErrMsg ){
char zPrefix[100];
if( in!=0 || !stdin_is_interactive ){
- sqlite3_snprintf(sizeof(zPrefix), zPrefix,
+ sqlite3_snprintf(sizeof(zPrefix), zPrefix,
"Error: near line %d:", startline);
}else{
sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
}
if( zErrMsg!=0 ){
- fprintf(stderr, "%s %s\n", zPrefix, zErrMsg);
+ utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
sqlite3_free(zErrMsg);
zErrMsg = 0;
}else{
- fprintf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
+ utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
}
errCnt++;
+ }else if( p->countChanges ){
+ raw_printf(p->out, "changes: %3d total_changes: %d\n",
+ sqlite3_changes(p->db), sqlite3_total_changes(p->db));
}
nSql = 0;
if( p->outCount ){
@@ -4247,11 +4992,11 @@ static int process_input(ShellState *p, FILE *in){
}
if( nSql ){
if( !_all_whitespace(zSql) ){
- fprintf(stderr, "Error: incomplete SQL: %s\n", zSql);
+ utf8_printf(stderr, "Error: incomplete SQL: %s\n", zSql);
errCnt++;
}
- free(zSql);
}
+ free(zSql);
free(zLine);
return errCnt>0;
}
@@ -4338,7 +5083,7 @@ static void process_sqliterc(
if (sqliterc == NULL) {
home_dir = find_home_dir();
if( home_dir==0 ){
- fprintf(stderr, "-- warning: cannot find home directory;"
+ raw_printf(stderr, "-- warning: cannot find home directory;"
" cannot read ~/.sqliterc\n");
return;
}
@@ -4349,7 +5094,7 @@ static void process_sqliterc(
in = fopen(sqliterc,"rb");
if( in ){
if( stdin_is_interactive ){
- fprintf(stderr,"-- Loading resources from %s\n",sqliterc);
+ utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
}
process_input(p,in);
fclose(in);
@@ -4360,7 +5105,7 @@ static void process_sqliterc(
/*
** Show available command line options
*/
-static const char zOptions[] =
+static const char zOptions[] =
" -ascii set output mode to 'ascii'\n"
" -bail stop after hitting an error\n"
" -batch force batch I/O\n"
@@ -4396,14 +5141,14 @@ static const char zOptions[] =
#endif
;
static void usage(int showDetail){
- fprintf(stderr,
- "Usage: %s [OPTIONS] FILENAME [SQL]\n"
+ utf8_printf(stderr,
+ "Usage: %s [OPTIONS] FILENAME [SQL]\n"
"FILENAME is the name of an SQLite database. A new database is created\n"
"if the file does not previously exist.\n", Argv0);
if( showDetail ){
- fprintf(stderr, "OPTIONS include:\n%s", zOptions);
+ utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
}else{
- fprintf(stderr, "Use the -help option for additional information\n");
+ raw_printf(stderr, "Use the -help option for additional information\n");
}
exit(1);
}
@@ -4413,7 +5158,8 @@ static void usage(int showDetail){
*/
static void main_init(ShellState *data) {
memset(data, 0, sizeof(*data));
- data->mode = MODE_List;
+ data->normalMode = data->cMode = data->mode = MODE_List;
+ data->autoExplain = 1;
memcpy(data->colSeparator,SEP_Column, 2);
memcpy(data->rowSeparator,SEP_Row, 2);
data->showHeader = 0;
@@ -4451,14 +5197,27 @@ static void printBold(const char *zText){
*/
static char *cmdline_option_value(int argc, char **argv, int i){
if( i==argc ){
- fprintf(stderr, "%s: Error: missing argument to %s\n",
+ utf8_printf(stderr, "%s: Error: missing argument to %s\n",
argv[0], argv[argc-1]);
exit(1);
}
return argv[i];
}
+#ifndef SQLITE_SHELL_IS_UTF8
+# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
+# define SQLITE_SHELL_IS_UTF8 (0)
+# else
+# define SQLITE_SHELL_IS_UTF8 (1)
+# endif
+#endif
+
+#if SQLITE_SHELL_IS_UTF8
int SQLITE_CDECL main(int argc, char **argv){
+#else
+int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
+ char **argv;
+#endif
char *zErrMsg = 0;
ShellState data;
const char *zInitFile = 0;
@@ -4469,18 +5228,36 @@ int SQLITE_CDECL main(int argc, char **argv){
int nCmd = 0;
char **azCmd = 0;
+ setBinaryMode(stdin, 0);
+ setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
+ stdin_is_interactive = isatty(0);
+ stdout_is_console = isatty(1);
+
#if USE_SYSTEM_SQLITE+0!=1
if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
- fprintf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
+ utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
sqlite3_sourceid(), SQLITE_SOURCE_ID);
exit(1);
}
#endif
- setBinaryMode(stdin);
- setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
- Argv0 = argv[0];
main_init(&data);
- stdin_is_interactive = isatty(0);
+#if !SQLITE_SHELL_IS_UTF8
+ sqlite3_initialize();
+ argv = sqlite3_malloc64(sizeof(argv[0])*argc);
+ if( argv==0 ){
+ raw_printf(stderr, "out of memory\n");
+ exit(1);
+ }
+ for(i=0; i<argc; i++){
+ argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
+ if( argv[i]==0 ){
+ raw_printf(stderr, "out of memory\n");
+ exit(1);
+ }
+ }
+#endif
+ assert( argc>=1 && argv && argv[0] );
+ Argv0 = argv[0];
/* Make sure we have a valid signal handler early, before anything
** else is done.
@@ -4519,7 +5296,7 @@ int SQLITE_CDECL main(int argc, char **argv){
nCmd++;
azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
if( azCmd==0 ){
- fprintf(stderr, "out of memory\n");
+ raw_printf(stderr, "out of memory\n");
exit(1);
}
azCmd[nCmd-1] = z;
@@ -4536,7 +5313,7 @@ int SQLITE_CDECL main(int argc, char **argv){
zInitFile = cmdline_option_value(argc, argv, ++i);
}else if( strcmp(z,"-batch")==0 ){
/* Need to check for batch mode here to so we can avoid printing
- ** informational messages (like from process_sqliterc) before
+ ** informational messages (like from process_sqliterc) before
** we do the actual processing of arguments later in a second pass.
*/
stdin_is_interactive = 0;
@@ -4549,6 +5326,8 @@ int SQLITE_CDECL main(int argc, char **argv){
szHeap = integerValue(zSize);
if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
+#else
+ (void)cmdline_option_value(argc, argv, ++i);
#endif
}else if( strcmp(z,"-scratch")==0 ){
int n, sz;
@@ -4564,10 +5343,10 @@ int SQLITE_CDECL main(int argc, char **argv){
int n, sz;
sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
if( sz>70000 ) sz = 70000;
- if( sz<800 ) sz = 800;
+ if( sz<0 ) sz = 0;
n = (int)integerValue(cmdline_option_value(argc,argv,++i));
- if( n<10 ) n = 10;
- sqlite3_config(SQLITE_CONFIG_PAGECACHE, malloc(n*sz+1), sz, n);
+ sqlite3_config(SQLITE_CONFIG_PAGECACHE,
+ (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
data.shellFlgs |= SHFLG_Pagecache;
}else if( strcmp(z,"-lookaside")==0 ){
int n, sz;
@@ -4601,7 +5380,7 @@ int SQLITE_CDECL main(int argc, char **argv){
if( pVfs ){
sqlite3_vfs_register(pVfs, 1);
}else{
- fprintf(stderr, "no such VFS: \"%s\"\n", argv[i]);
+ utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
exit(1);
}
}
@@ -4611,7 +5390,7 @@ int SQLITE_CDECL main(int argc, char **argv){
data.zDbFilename = ":memory:";
warnInmemoryDb = argc==1;
#else
- fprintf(stderr,"%s: Error: no database filename specified\n", Argv0);
+ utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
return 1;
#endif
}
@@ -4677,6 +5456,8 @@ int SQLITE_CDECL main(int argc, char **argv){
data.echoOn = 1;
}else if( strcmp(z,"-eqp")==0 ){
data.autoEQP = 1;
+ }else if( strcmp(z,"-eqpfull")==0 ){
+ data.autoEQP = 2;
}else if( strcmp(z,"-stats")==0 ){
data.statsOn = 1;
}else if( strcmp(z,"-scanstats")==0 ){
@@ -4733,18 +5514,19 @@ int SQLITE_CDECL main(int argc, char **argv){
open_db(&data, 0);
rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
if( zErrMsg!=0 ){
- fprintf(stderr,"Error: %s\n", zErrMsg);
+ utf8_printf(stderr,"Error: %s\n", zErrMsg);
if( bail_on_error ) return rc!=0 ? rc : 1;
}else if( rc!=0 ){
- fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
+ utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
if( bail_on_error ) return rc;
}
}
}else{
- fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
- fprintf(stderr,"Use -help for a list of options.\n");
+ utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
+ raw_printf(stderr,"Use -help for a list of options.\n");
return 1;
}
+ data.cMode = data.mode;
}
if( !readStdin ){
@@ -4760,10 +5542,10 @@ int SQLITE_CDECL main(int argc, char **argv){
open_db(&data, 0);
rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
if( zErrMsg!=0 ){
- fprintf(stderr,"Error: %s\n", zErrMsg);
+ utf8_printf(stderr,"Error: %s\n", zErrMsg);
return rc!=0 ? rc : 1;
}else if( rc!=0 ){
- fprintf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
+ utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
return rc;
}
}
@@ -4807,8 +5589,13 @@ int SQLITE_CDECL main(int argc, char **argv){
}
set_table_name(&data, 0);
if( data.db ){
+ session_close_all(&data);
sqlite3_close(data.db);
}
- sqlite3_free(data.zFreeOnClose);
+ sqlite3_free(data.zFreeOnClose);
+#if !SQLITE_SHELL_IS_UTF8
+ for(i=0; i<argc; i++) sqlite3_free(argv[i]);
+ sqlite3_free(argv);
+#endif
return rc;
}
OpenPOWER on IntegriCloud