Fix harmless compiler warnings seen with MSVC.
[sqlite.git] / src / shell.c.in
blob82680d484c724e767f90a305812c99029a5cb320
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
21 ** Warning pragmas copied from msvc.h in the core.
23 #if defined(_MSC_VER)
24 #pragma warning(disable : 4054)
25 #pragma warning(disable : 4055)
26 #pragma warning(disable : 4100)
27 #pragma warning(disable : 4127)
28 #pragma warning(disable : 4130)
29 #pragma warning(disable : 4152)
30 #pragma warning(disable : 4189)
31 #pragma warning(disable : 4206)
32 #pragma warning(disable : 4210)
33 #pragma warning(disable : 4232)
34 #pragma warning(disable : 4244)
35 #pragma warning(disable : 4305)
36 #pragma warning(disable : 4306)
37 #pragma warning(disable : 4702)
38 #pragma warning(disable : 4706)
39 #endif /* defined(_MSC_VER) */
42 ** No support for loadable extensions in VxWorks.
44 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
45 # define SQLITE_OMIT_LOAD_EXTENSION 1
46 #endif
49 ** Enable large-file support for fopen() and friends on unix.
51 #ifndef SQLITE_DISABLE_LFS
52 # define _LARGE_FILE 1
53 # ifndef _FILE_OFFSET_BITS
54 # define _FILE_OFFSET_BITS 64
55 # endif
56 # define _LARGEFILE_SOURCE 1
57 #endif
59 #include <stdlib.h>
60 #include <string.h>
61 #include <stdio.h>
62 #include <assert.h>
63 #include "sqlite3.h"
64 #if SQLITE_USER_AUTHENTICATION
65 # include "sqlite3userauth.h"
66 #endif
67 #include <ctype.h>
68 #include <stdarg.h>
70 #if !defined(_WIN32) && !defined(WIN32)
71 # include <signal.h>
72 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
73 # include <pwd.h>
74 # endif
75 # include <unistd.h>
76 # include <sys/types.h>
77 #endif
79 #if HAVE_READLINE
80 # include <readline/readline.h>
81 # include <readline/history.h>
82 #endif
84 #if HAVE_EDITLINE
85 # include <editline/readline.h>
86 #endif
88 #if HAVE_EDITLINE || HAVE_READLINE
90 # define shell_add_history(X) add_history(X)
91 # define shell_read_history(X) read_history(X)
92 # define shell_write_history(X) write_history(X)
93 # define shell_stifle_history(X) stifle_history(X)
94 # define shell_readline(X) readline(X)
96 #elif HAVE_LINENOISE
98 # include "linenoise.h"
99 # define shell_add_history(X) linenoiseHistoryAdd(X)
100 # define shell_read_history(X) linenoiseHistoryLoad(X)
101 # define shell_write_history(X) linenoiseHistorySave(X)
102 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
103 # define shell_readline(X) linenoise(X)
105 #else
107 # define shell_read_history(X)
108 # define shell_write_history(X)
109 # define shell_stifle_history(X)
111 # define SHELL_USE_LOCAL_GETLINE 1
112 #endif
115 #if defined(_WIN32) || defined(WIN32)
116 # include <io.h>
117 # include <fcntl.h>
118 # define isatty(h) _isatty(h)
119 # ifndef access
120 # define access(f,m) _access((f),(m))
121 # endif
122 # undef popen
123 # define popen _popen
124 # undef pclose
125 # define pclose _pclose
126 #else
127 /* Make sure isatty() has a prototype. */
128 extern int isatty(int);
130 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
131 /* popen and pclose are not C89 functions and so are
132 ** sometimes omitted from the <stdio.h> header */
133 extern FILE *popen(const char*,const char*);
134 extern int pclose(FILE*);
135 # else
136 # define SQLITE_OMIT_POPEN 1
137 # endif
138 #endif
140 #if defined(_WIN32_WCE)
141 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
142 * thus we always assume that we have a console. That can be
143 * overridden with the -batch command line option.
145 #define isatty(x) 1
146 #endif
148 /* ctype macros that work with signed characters */
149 #define IsSpace(X) isspace((unsigned char)X)
150 #define IsDigit(X) isdigit((unsigned char)X)
151 #define ToLower(X) (char)tolower((unsigned char)X)
153 #if defined(_WIN32) || defined(WIN32)
154 #include <windows.h>
156 /* string conversion routines only needed on Win32 */
157 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
158 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
159 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
160 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
161 #endif
163 /* On Windows, we normally run with output mode of TEXT so that \n characters
164 ** are automatically translated into \r\n. However, this behavior needs
165 ** to be disabled in some cases (ex: when generating CSV output and when
166 ** rendering quoted strings that contain \n characters). The following
167 ** routines take care of that.
169 #if defined(_WIN32) || defined(WIN32)
170 static void setBinaryMode(FILE *file, int isOutput){
171 if( isOutput ) fflush(file);
172 _setmode(_fileno(file), _O_BINARY);
174 static void setTextMode(FILE *file, int isOutput){
175 if( isOutput ) fflush(file);
176 _setmode(_fileno(file), _O_TEXT);
178 #else
179 # define setBinaryMode(X,Y)
180 # define setTextMode(X,Y)
181 #endif
184 /* True if the timer is enabled */
185 static int enableTimer = 0;
187 /* Return the current wall-clock time */
188 static sqlite3_int64 timeOfDay(void){
189 static sqlite3_vfs *clockVfs = 0;
190 sqlite3_int64 t;
191 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
192 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
193 clockVfs->xCurrentTimeInt64(clockVfs, &t);
194 }else{
195 double r;
196 clockVfs->xCurrentTime(clockVfs, &r);
197 t = (sqlite3_int64)(r*86400000.0);
199 return t;
202 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
203 #include <sys/time.h>
204 #include <sys/resource.h>
206 /* VxWorks does not support getrusage() as far as we can determine */
207 #if defined(_WRS_KERNEL) || defined(__RTP__)
208 struct rusage {
209 struct timeval ru_utime; /* user CPU time used */
210 struct timeval ru_stime; /* system CPU time used */
212 #define getrusage(A,B) memset(B,0,sizeof(*B))
213 #endif
215 /* Saved resource information for the beginning of an operation */
216 static struct rusage sBegin; /* CPU time at start */
217 static sqlite3_int64 iBegin; /* Wall-clock time at start */
220 ** Begin timing an operation
222 static void beginTimer(void){
223 if( enableTimer ){
224 getrusage(RUSAGE_SELF, &sBegin);
225 iBegin = timeOfDay();
229 /* Return the difference of two time_structs in seconds */
230 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
231 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
232 (double)(pEnd->tv_sec - pStart->tv_sec);
236 ** Print the timing results.
238 static void endTimer(void){
239 if( enableTimer ){
240 sqlite3_int64 iEnd = timeOfDay();
241 struct rusage sEnd;
242 getrusage(RUSAGE_SELF, &sEnd);
243 printf("Run Time: real %.3f user %f sys %f\n",
244 (iEnd - iBegin)*0.001,
245 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
246 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
250 #define BEGIN_TIMER beginTimer()
251 #define END_TIMER endTimer()
252 #define HAS_TIMER 1
254 #elif (defined(_WIN32) || defined(WIN32))
256 /* Saved resource information for the beginning of an operation */
257 static HANDLE hProcess;
258 static FILETIME ftKernelBegin;
259 static FILETIME ftUserBegin;
260 static sqlite3_int64 ftWallBegin;
261 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
262 LPFILETIME, LPFILETIME);
263 static GETPROCTIMES getProcessTimesAddr = NULL;
266 ** Check to see if we have timer support. Return 1 if necessary
267 ** support found (or found previously).
269 static int hasTimer(void){
270 if( getProcessTimesAddr ){
271 return 1;
272 } else {
273 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
274 ** versions. See if the version we are running on has it, and if it
275 ** does, save off a pointer to it and the current process handle.
277 hProcess = GetCurrentProcess();
278 if( hProcess ){
279 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
280 if( NULL != hinstLib ){
281 getProcessTimesAddr =
282 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
283 if( NULL != getProcessTimesAddr ){
284 return 1;
286 FreeLibrary(hinstLib);
290 return 0;
294 ** Begin timing an operation
296 static void beginTimer(void){
297 if( enableTimer && getProcessTimesAddr ){
298 FILETIME ftCreation, ftExit;
299 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
300 &ftKernelBegin,&ftUserBegin);
301 ftWallBegin = timeOfDay();
305 /* Return the difference of two FILETIME structs in seconds */
306 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
307 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
308 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
309 return (double) ((i64End - i64Start) / 10000000.0);
313 ** Print the timing results.
315 static void endTimer(void){
316 if( enableTimer && getProcessTimesAddr){
317 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
318 sqlite3_int64 ftWallEnd = timeOfDay();
319 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
320 printf("Run Time: real %.3f user %f sys %f\n",
321 (ftWallEnd - ftWallBegin)*0.001,
322 timeDiff(&ftUserBegin, &ftUserEnd),
323 timeDiff(&ftKernelBegin, &ftKernelEnd));
327 #define BEGIN_TIMER beginTimer()
328 #define END_TIMER endTimer()
329 #define HAS_TIMER hasTimer()
331 #else
332 #define BEGIN_TIMER
333 #define END_TIMER
334 #define HAS_TIMER 0
335 #endif
338 ** Used to prevent warnings about unused parameters
340 #define UNUSED_PARAMETER(x) (void)(x)
343 ** If the following flag is set, then command execution stops
344 ** at an error if we are not interactive.
346 static int bail_on_error = 0;
349 ** Threat stdin as an interactive input if the following variable
350 ** is true. Otherwise, assume stdin is connected to a file or pipe.
352 static int stdin_is_interactive = 1;
355 ** On Windows systems we have to know if standard output is a console
356 ** in order to translate UTF-8 into MBCS. The following variable is
357 ** true if translation is required.
359 static int stdout_is_console = 1;
362 ** The following is the open SQLite database. We make a pointer
363 ** to this database a static variable so that it can be accessed
364 ** by the SIGINT handler to interrupt database processing.
366 static sqlite3 *globalDb = 0;
369 ** True if an interrupt (Control-C) has been received.
371 static volatile int seenInterrupt = 0;
374 ** This is the name of our program. It is set in main(), used
375 ** in a number of other places, mostly for error messages.
377 static char *Argv0;
380 ** Prompt strings. Initialized in main. Settable with
381 ** .prompt main continue
383 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
384 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
387 ** Render output like fprintf(). Except, if the output is going to the
388 ** console and if this is running on a Windows machine, translate the
389 ** output from UTF-8 into MBCS.
391 #if defined(_WIN32) || defined(WIN32)
392 void utf8_printf(FILE *out, const char *zFormat, ...){
393 va_list ap;
394 va_start(ap, zFormat);
395 if( stdout_is_console && (out==stdout || out==stderr) ){
396 char *z1 = sqlite3_vmprintf(zFormat, ap);
397 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
398 sqlite3_free(z1);
399 fputs(z2, out);
400 sqlite3_free(z2);
401 }else{
402 vfprintf(out, zFormat, ap);
404 va_end(ap);
406 #elif !defined(utf8_printf)
407 # define utf8_printf fprintf
408 #endif
411 ** Render output like fprintf(). This should not be used on anything that
412 ** includes string formatting (e.g. "%s").
414 #if !defined(raw_printf)
415 # define raw_printf fprintf
416 #endif
419 ** Write I/O traces to the following stream.
421 #ifdef SQLITE_ENABLE_IOTRACE
422 static FILE *iotrace = 0;
423 #endif
426 ** This routine works like printf in that its first argument is a
427 ** format string and subsequent arguments are values to be substituted
428 ** in place of % fields. The result of formatting this string
429 ** is written to iotrace.
431 #ifdef SQLITE_ENABLE_IOTRACE
432 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
433 va_list ap;
434 char *z;
435 if( iotrace==0 ) return;
436 va_start(ap, zFormat);
437 z = sqlite3_vmprintf(zFormat, ap);
438 va_end(ap);
439 utf8_printf(iotrace, "%s", z);
440 sqlite3_free(z);
442 #endif
445 ** Output string zUtf to stream pOut as w characters. If w is negative,
446 ** then right-justify the text. W is the width in UTF-8 characters, not
447 ** in bytes. This is different from the %*.*s specification in printf
448 ** since with %*.*s the width is measured in bytes, not characters.
450 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
451 int i;
452 int n;
453 int aw = w<0 ? -w : w;
454 char zBuf[1000];
455 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
456 for(i=n=0; zUtf[i]; i++){
457 if( (zUtf[i]&0xc0)!=0x80 ){
458 n++;
459 if( n==aw ){
460 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
461 break;
465 if( n>=aw ){
466 utf8_printf(pOut, "%.*s", i, zUtf);
467 }else if( w<0 ){
468 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
469 }else{
470 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
476 ** Determines if a string is a number of not.
478 static int isNumber(const char *z, int *realnum){
479 if( *z=='-' || *z=='+' ) z++;
480 if( !IsDigit(*z) ){
481 return 0;
483 z++;
484 if( realnum ) *realnum = 0;
485 while( IsDigit(*z) ){ z++; }
486 if( *z=='.' ){
487 z++;
488 if( !IsDigit(*z) ) return 0;
489 while( IsDigit(*z) ){ z++; }
490 if( realnum ) *realnum = 1;
492 if( *z=='e' || *z=='E' ){
493 z++;
494 if( *z=='+' || *z=='-' ) z++;
495 if( !IsDigit(*z) ) return 0;
496 while( IsDigit(*z) ){ z++; }
497 if( realnum ) *realnum = 1;
499 return *z==0;
503 ** Compute a string length that is limited to what can be stored in
504 ** lower 30 bits of a 32-bit signed integer.
506 static int strlen30(const char *z){
507 const char *z2 = z;
508 while( *z2 ){ z2++; }
509 return 0x3fffffff & (int)(z2 - z);
513 ** Return the length of a string in characters. Multibyte UTF8 characters
514 ** count as a single character.
516 static int strlenChar(const char *z){
517 int n = 0;
518 while( *z ){
519 if( (0xc0&*(z++))!=0x80 ) n++;
521 return n;
525 ** This routine reads a line of text from FILE in, stores
526 ** the text in memory obtained from malloc() and returns a pointer
527 ** to the text. NULL is returned at end of file, or if malloc()
528 ** fails.
530 ** If zLine is not NULL then it is a malloced buffer returned from
531 ** a previous call to this routine that may be reused.
533 static char *local_getline(char *zLine, FILE *in){
534 int nLine = zLine==0 ? 0 : 100;
535 int n = 0;
537 while( 1 ){
538 if( n+100>nLine ){
539 nLine = nLine*2 + 100;
540 zLine = realloc(zLine, nLine);
541 if( zLine==0 ) return 0;
543 if( fgets(&zLine[n], nLine - n, in)==0 ){
544 if( n==0 ){
545 free(zLine);
546 return 0;
548 zLine[n] = 0;
549 break;
551 while( zLine[n] ) n++;
552 if( n>0 && zLine[n-1]=='\n' ){
553 n--;
554 if( n>0 && zLine[n-1]=='\r' ) n--;
555 zLine[n] = 0;
556 break;
559 #if defined(_WIN32) || defined(WIN32)
560 /* For interactive input on Windows systems, translate the
561 ** multi-byte characterset characters into UTF-8. */
562 if( stdin_is_interactive && in==stdin ){
563 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
564 if( zTrans ){
565 int nTrans = strlen30(zTrans)+1;
566 if( nTrans>nLine ){
567 zLine = realloc(zLine, nTrans);
568 if( zLine==0 ){
569 sqlite3_free(zTrans);
570 return 0;
573 memcpy(zLine, zTrans, nTrans);
574 sqlite3_free(zTrans);
577 #endif /* defined(_WIN32) || defined(WIN32) */
578 return zLine;
582 ** Retrieve a single line of input text.
584 ** If in==0 then read from standard input and prompt before each line.
585 ** If isContinuation is true, then a continuation prompt is appropriate.
586 ** If isContinuation is zero, then the main prompt should be used.
588 ** If zPrior is not NULL then it is a buffer from a prior call to this
589 ** routine that can be reused.
591 ** The result is stored in space obtained from malloc() and must either
592 ** be freed by the caller or else passed back into this routine via the
593 ** zPrior argument for reuse.
595 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
596 char *zPrompt;
597 char *zResult;
598 if( in!=0 ){
599 zResult = local_getline(zPrior, in);
600 }else{
601 zPrompt = isContinuation ? continuePrompt : mainPrompt;
602 #if SHELL_USE_LOCAL_GETLINE
603 printf("%s", zPrompt);
604 fflush(stdout);
605 zResult = local_getline(zPrior, stdin);
606 #else
607 free(zPrior);
608 zResult = shell_readline(zPrompt);
609 if( zResult && *zResult ) shell_add_history(zResult);
610 #endif
612 return zResult;
615 ** A variable length string to which one can append text.
617 typedef struct ShellText ShellText;
618 struct ShellText {
619 char *z;
620 int n;
621 int nAlloc;
625 ** Initialize and destroy a ShellText object
627 static void initText(ShellText *p){
628 memset(p, 0, sizeof(*p));
630 static void freeText(ShellText *p){
631 free(p->z);
632 initText(p);
635 /* zIn is either a pointer to a NULL-terminated string in memory obtained
636 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
637 ** added to zIn, and the result returned in memory obtained from malloc().
638 ** zIn, if it was not NULL, is freed.
640 ** If the third argument, quote, is not '\0', then it is used as a
641 ** quote character for zAppend.
643 static void appendText(ShellText *p, char const *zAppend, char quote){
644 int len;
645 int i;
646 int nAppend = strlen30(zAppend);
648 len = nAppend+p->n+1;
649 if( quote ){
650 len += 2;
651 for(i=0; i<nAppend; i++){
652 if( zAppend[i]==quote ) len++;
656 if( p->n+len>=p->nAlloc ){
657 p->nAlloc = p->nAlloc*2 + len + 20;
658 p->z = realloc(p->z, p->nAlloc);
659 if( p->z==0 ){
660 memset(p, 0, sizeof(*p));
661 return;
665 if( quote ){
666 char *zCsr = p->z+p->n;
667 *zCsr++ = quote;
668 for(i=0; i<nAppend; i++){
669 *zCsr++ = zAppend[i];
670 if( zAppend[i]==quote ) *zCsr++ = quote;
672 *zCsr++ = quote;
673 p->n = (int)(zCsr - p->z);
674 *zCsr = '\0';
675 }else{
676 memcpy(p->z+p->n, zAppend, nAppend);
677 p->n += nAppend;
678 p->z[p->n] = '\0';
683 ** Attempt to determine if identifier zName needs to be quoted, either
684 ** because it contains non-alphanumeric characters, or because it is an
685 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
686 ** that quoting is required.
688 ** Return '"' if quoting is required. Return 0 if no quoting is required.
690 static char quoteChar(const char *zName){
691 /* All SQLite keywords, in alphabetical order */
692 static const char *azKeywords[] = {
693 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
694 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
695 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
696 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
697 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
698 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
699 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
700 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
701 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
702 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
703 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
704 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
705 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
706 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
707 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
708 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
709 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
710 "WITH", "WITHOUT",
712 int i, lwr, upr, mid, c;
713 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
714 for(i=0; zName[i]; i++){
715 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
717 lwr = 0;
718 upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
719 while( lwr<=upr ){
720 mid = (lwr+upr)/2;
721 c = sqlite3_stricmp(azKeywords[mid], zName);
722 if( c==0 ) return '"';
723 if( c<0 ){
724 lwr = mid+1;
725 }else{
726 upr = mid-1;
729 return 0;
733 ** SQL function: shell_add_schema(S,X)
735 ** Add the schema name X to the CREATE statement in S and return the result.
736 ** Examples:
738 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
740 ** Also works on
742 ** CREATE INDEX
743 ** CREATE UNIQUE INDEX
744 ** CREATE VIEW
745 ** CREATE TRIGGER
746 ** CREATE VIRTUAL TABLE
748 ** This UDF is used by the .schema command to insert the schema name of
749 ** attached databases into the middle of the sqlite_master.sql field.
751 static void shellAddSchemaName(
752 sqlite3_context *pCtx,
753 int nVal,
754 sqlite3_value **apVal
756 static const char *aPrefix[] = {
757 "TABLE",
758 "INDEX",
759 "UNIQUE INDEX",
760 "VIEW",
761 "TRIGGER",
762 "VIRTUAL TABLE"
764 int i = 0;
765 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
766 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
767 assert( nVal==2 );
768 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
769 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
770 int n = strlen30(aPrefix[i]);
771 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
772 char cQuote = quoteChar(zSchema);
773 char *z;
774 if( cQuote ){
775 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
776 }else{
777 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
779 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
780 return;
784 sqlite3_result_value(pCtx, apVal[0]);
788 ** The source code for several run-time loadable extensions is inserted
789 ** below by the ../tool/mkshellc.tcl script. Before processing that included
790 ** code, we need to override some macros to make the included program code
791 ** work here in the middle of this regular program.
793 #define SQLITE_EXTENSION_INIT1
794 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
796 INCLUDE ../ext/misc/shathree.c
797 INCLUDE ../ext/misc/fileio.c
798 INCLUDE ../ext/misc/completion.c
800 #if defined(SQLITE_ENABLE_SESSION)
802 ** State information for a single open session
804 typedef struct OpenSession OpenSession;
805 struct OpenSession {
806 char *zName; /* Symbolic name for this session */
807 int nFilter; /* Number of xFilter rejection GLOB patterns */
808 char **azFilter; /* Array of xFilter rejection GLOB patterns */
809 sqlite3_session *p; /* The open session */
811 #endif
814 ** Shell output mode information from before ".explain on",
815 ** saved so that it can be restored by ".explain off"
817 typedef struct SavedModeInfo SavedModeInfo;
818 struct SavedModeInfo {
819 int valid; /* Is there legit data in here? */
820 int mode; /* Mode prior to ".explain on" */
821 int showHeader; /* The ".header" setting prior to ".explain on" */
822 int colWidth[100]; /* Column widths prior to ".explain on" */
826 ** State information about the database connection is contained in an
827 ** instance of the following structure.
829 typedef struct ShellState ShellState;
830 struct ShellState {
831 sqlite3 *db; /* The database */
832 int autoExplain; /* Automatically turn on .explain mode */
833 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
834 int statsOn; /* True to display memory stats before each finalize */
835 int scanstatsOn; /* True to display scan stats before each finalize */
836 int outCount; /* Revert to stdout when reaching zero */
837 int cnt; /* Number of records displayed so far */
838 FILE *out; /* Write results here */
839 FILE *traceOut; /* Output for sqlite3_trace() */
840 int nErr; /* Number of errors seen */
841 int mode; /* An output mode setting */
842 int cMode; /* temporary output mode for the current query */
843 int normalMode; /* Output mode before ".explain on" */
844 int writableSchema; /* True if PRAGMA writable_schema=ON */
845 int showHeader; /* True to show column names in List or Column mode */
846 int nCheck; /* Number of ".check" commands run */
847 unsigned shellFlgs; /* Various flags */
848 char *zDestTable; /* Name of destination table when MODE_Insert */
849 char zTestcase[30]; /* Name of current test case */
850 char colSeparator[20]; /* Column separator character for several modes */
851 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
852 int colWidth[100]; /* Requested width of each column when in column mode*/
853 int actualWidth[100]; /* Actual width of each column */
854 char nullValue[20]; /* The text to print when a NULL comes back from
855 ** the database */
856 char outfile[FILENAME_MAX]; /* Filename for *out */
857 const char *zDbFilename; /* name of the database file */
858 char *zFreeOnClose; /* Filename to free when closing */
859 const char *zVfs; /* Name of VFS to use */
860 sqlite3_stmt *pStmt; /* Current statement if any. */
861 FILE *pLog; /* Write log output here */
862 int *aiIndent; /* Array of indents used in MODE_Explain */
863 int nIndent; /* Size of array aiIndent[] */
864 int iIndent; /* Index of current op in aiIndent[] */
865 #if defined(SQLITE_ENABLE_SESSION)
866 int nSession; /* Number of active sessions */
867 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
868 #endif
872 ** These are the allowed shellFlgs values
874 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
875 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
876 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
877 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
878 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
879 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
880 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
883 ** Macros for testing and setting shellFlgs
885 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
886 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
887 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
890 ** These are the allowed modes.
892 #define MODE_Line 0 /* One column per line. Blank line between records */
893 #define MODE_Column 1 /* One record per line in neat columns */
894 #define MODE_List 2 /* One record per line with a separator */
895 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
896 #define MODE_Html 4 /* Generate an XHTML table */
897 #define MODE_Insert 5 /* Generate SQL "insert" statements */
898 #define MODE_Quote 6 /* Quote values as for SQL */
899 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
900 #define MODE_Csv 8 /* Quote strings, numbers are plain */
901 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
902 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
903 #define MODE_Pretty 11 /* Pretty-print schemas */
905 static const char *modeDescr[] = {
906 "line",
907 "column",
908 "list",
909 "semi",
910 "html",
911 "insert",
912 "quote",
913 "tcl",
914 "csv",
915 "explain",
916 "ascii",
917 "prettyprint",
921 ** These are the column/row/line separators used by the various
922 ** import/export modes.
924 #define SEP_Column "|"
925 #define SEP_Row "\n"
926 #define SEP_Tab "\t"
927 #define SEP_Space " "
928 #define SEP_Comma ","
929 #define SEP_CrLf "\r\n"
930 #define SEP_Unit "\x1F"
931 #define SEP_Record "\x1E"
934 ** Number of elements in an array
936 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
939 ** A callback for the sqlite3_log() interface.
941 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
942 ShellState *p = (ShellState*)pArg;
943 if( p->pLog==0 ) return;
944 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
945 fflush(p->pLog);
949 ** Output the given string as a hex-encoded blob (eg. X'1234' )
951 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
952 int i;
953 char *zBlob = (char *)pBlob;
954 raw_printf(out,"X'");
955 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
956 raw_printf(out,"'");
960 ** Find a string that is not found anywhere in z[]. Return a pointer
961 ** to that string.
963 ** Try to use zA and zB first. If both of those are already found in z[]
964 ** then make up some string and store it in the buffer zBuf.
966 static const char *unused_string(
967 const char *z, /* Result must not appear anywhere in z */
968 const char *zA, const char *zB, /* Try these first */
969 char *zBuf /* Space to store a generated string */
971 unsigned i = 0;
972 if( strstr(z, zA)==0 ) return zA;
973 if( strstr(z, zB)==0 ) return zB;
975 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
976 }while( strstr(z,zBuf)!=0 );
977 return zBuf;
981 ** Output the given string as a quoted string using SQL quoting conventions.
983 ** See also: output_quoted_escaped_string()
985 static void output_quoted_string(FILE *out, const char *z){
986 int i;
987 char c;
988 setBinaryMode(out, 1);
989 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
990 if( c==0 ){
991 utf8_printf(out,"'%s'",z);
992 }else{
993 raw_printf(out, "'");
994 while( *z ){
995 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
996 if( c=='\'' ) i++;
997 if( i ){
998 utf8_printf(out, "%.*s", i, z);
999 z += i;
1001 if( c=='\'' ){
1002 raw_printf(out, "'");
1003 continue;
1005 if( c==0 ){
1006 break;
1008 z++;
1010 raw_printf(out, "'");
1012 setTextMode(out, 1);
1016 ** Output the given string as a quoted string using SQL quoting conventions.
1017 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1018 ** get corrupted by end-of-line translation facilities in some operating
1019 ** systems.
1021 ** This is like output_quoted_string() but with the addition of the \r\n
1022 ** escape mechanism.
1024 static void output_quoted_escaped_string(FILE *out, const char *z){
1025 int i;
1026 char c;
1027 setBinaryMode(out, 1);
1028 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1029 if( c==0 ){
1030 utf8_printf(out,"'%s'",z);
1031 }else{
1032 const char *zNL = 0;
1033 const char *zCR = 0;
1034 int nNL = 0;
1035 int nCR = 0;
1036 char zBuf1[20], zBuf2[20];
1037 for(i=0; z[i]; i++){
1038 if( z[i]=='\n' ) nNL++;
1039 if( z[i]=='\r' ) nCR++;
1041 if( nNL ){
1042 raw_printf(out, "replace(");
1043 zNL = unused_string(z, "\\n", "\\012", zBuf1);
1045 if( nCR ){
1046 raw_printf(out, "replace(");
1047 zCR = unused_string(z, "\\r", "\\015", zBuf2);
1049 raw_printf(out, "'");
1050 while( *z ){
1051 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1052 if( c=='\'' ) i++;
1053 if( i ){
1054 utf8_printf(out, "%.*s", i, z);
1055 z += i;
1057 if( c=='\'' ){
1058 raw_printf(out, "'");
1059 continue;
1061 if( c==0 ){
1062 break;
1064 z++;
1065 if( c=='\n' ){
1066 raw_printf(out, "%s", zNL);
1067 continue;
1069 raw_printf(out, "%s", zCR);
1071 raw_printf(out, "'");
1072 if( nCR ){
1073 raw_printf(out, ",'%s',char(13))", zCR);
1075 if( nNL ){
1076 raw_printf(out, ",'%s',char(10))", zNL);
1079 setTextMode(out, 1);
1083 ** Output the given string as a quoted according to C or TCL quoting rules.
1085 static void output_c_string(FILE *out, const char *z){
1086 unsigned int c;
1087 fputc('"', out);
1088 while( (c = *(z++))!=0 ){
1089 if( c=='\\' ){
1090 fputc(c, out);
1091 fputc(c, out);
1092 }else if( c=='"' ){
1093 fputc('\\', out);
1094 fputc('"', out);
1095 }else if( c=='\t' ){
1096 fputc('\\', out);
1097 fputc('t', out);
1098 }else if( c=='\n' ){
1099 fputc('\\', out);
1100 fputc('n', out);
1101 }else if( c=='\r' ){
1102 fputc('\\', out);
1103 fputc('r', out);
1104 }else if( !isprint(c&0xff) ){
1105 raw_printf(out, "\\%03o", c&0xff);
1106 }else{
1107 fputc(c, out);
1110 fputc('"', out);
1114 ** Output the given string with characters that are special to
1115 ** HTML escaped.
1117 static void output_html_string(FILE *out, const char *z){
1118 int i;
1119 if( z==0 ) z = "";
1120 while( *z ){
1121 for(i=0; z[i]
1122 && z[i]!='<'
1123 && z[i]!='&'
1124 && z[i]!='>'
1125 && z[i]!='\"'
1126 && z[i]!='\'';
1127 i++){}
1128 if( i>0 ){
1129 utf8_printf(out,"%.*s",i,z);
1131 if( z[i]=='<' ){
1132 raw_printf(out,"&lt;");
1133 }else if( z[i]=='&' ){
1134 raw_printf(out,"&amp;");
1135 }else if( z[i]=='>' ){
1136 raw_printf(out,"&gt;");
1137 }else if( z[i]=='\"' ){
1138 raw_printf(out,"&quot;");
1139 }else if( z[i]=='\'' ){
1140 raw_printf(out,"&#39;");
1141 }else{
1142 break;
1144 z += i + 1;
1149 ** If a field contains any character identified by a 1 in the following
1150 ** array, then the string must be quoted for CSV.
1152 static const char needCsvQuote[] = {
1153 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1154 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1155 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1161 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1162 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1163 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1164 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1165 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1166 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1167 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1168 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1172 ** Output a single term of CSV. Actually, p->colSeparator is used for
1173 ** the separator, which may or may not be a comma. p->nullValue is
1174 ** the null value. Strings are quoted if necessary. The separator
1175 ** is only issued if bSep is true.
1177 static void output_csv(ShellState *p, const char *z, int bSep){
1178 FILE *out = p->out;
1179 if( z==0 ){
1180 utf8_printf(out,"%s",p->nullValue);
1181 }else{
1182 int i;
1183 int nSep = strlen30(p->colSeparator);
1184 for(i=0; z[i]; i++){
1185 if( needCsvQuote[((unsigned char*)z)[i]]
1186 || (z[i]==p->colSeparator[0] &&
1187 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1188 i = 0;
1189 break;
1192 if( i==0 ){
1193 putc('"', out);
1194 for(i=0; z[i]; i++){
1195 if( z[i]=='"' ) putc('"', out);
1196 putc(z[i], out);
1198 putc('"', out);
1199 }else{
1200 utf8_printf(out, "%s", z);
1203 if( bSep ){
1204 utf8_printf(p->out, "%s", p->colSeparator);
1208 #ifdef SIGINT
1210 ** This routine runs when the user presses Ctrl-C
1212 static void interrupt_handler(int NotUsed){
1213 UNUSED_PARAMETER(NotUsed);
1214 seenInterrupt++;
1215 if( seenInterrupt>2 ) exit(1);
1216 if( globalDb ) sqlite3_interrupt(globalDb);
1218 #endif
1220 #ifndef SQLITE_OMIT_AUTHORIZATION
1222 ** When the ".auth ON" is set, the following authorizer callback is
1223 ** invoked. It always returns SQLITE_OK.
1225 static int shellAuth(
1226 void *pClientData,
1227 int op,
1228 const char *zA1,
1229 const char *zA2,
1230 const char *zA3,
1231 const char *zA4
1233 ShellState *p = (ShellState*)pClientData;
1234 static const char *azAction[] = { 0,
1235 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
1236 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
1237 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
1238 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
1239 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
1240 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
1241 "PRAGMA", "READ", "SELECT",
1242 "TRANSACTION", "UPDATE", "ATTACH",
1243 "DETACH", "ALTER_TABLE", "REINDEX",
1244 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
1245 "FUNCTION", "SAVEPOINT", "RECURSIVE"
1247 int i;
1248 const char *az[4];
1249 az[0] = zA1;
1250 az[1] = zA2;
1251 az[2] = zA3;
1252 az[3] = zA4;
1253 utf8_printf(p->out, "authorizer: %s", azAction[op]);
1254 for(i=0; i<4; i++){
1255 raw_printf(p->out, " ");
1256 if( az[i] ){
1257 output_c_string(p->out, az[i]);
1258 }else{
1259 raw_printf(p->out, "NULL");
1262 raw_printf(p->out, "\n");
1263 return SQLITE_OK;
1265 #endif
1268 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
1270 ** This routine converts some CREATE TABLE statements for shadow tables
1271 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1273 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1274 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1275 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1276 }else{
1277 utf8_printf(out, "%s%s", z, zTail);
1280 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1281 char c = z[n];
1282 z[n] = 0;
1283 printSchemaLine(out, z, zTail);
1284 z[n] = c;
1288 ** This is the callback routine that the shell
1289 ** invokes for each row of a query result.
1291 static int shell_callback(
1292 void *pArg,
1293 int nArg, /* Number of result columns */
1294 char **azArg, /* Text of each result column */
1295 char **azCol, /* Column names */
1296 int *aiType /* Column types */
1298 int i;
1299 ShellState *p = (ShellState*)pArg;
1301 if( azArg==0 ) return 0;
1302 switch( p->cMode ){
1303 case MODE_Line: {
1304 int w = 5;
1305 if( azArg==0 ) break;
1306 for(i=0; i<nArg; i++){
1307 int len = strlen30(azCol[i] ? azCol[i] : "");
1308 if( len>w ) w = len;
1310 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1311 for(i=0; i<nArg; i++){
1312 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1313 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1315 break;
1317 case MODE_Explain:
1318 case MODE_Column: {
1319 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1320 const int *colWidth;
1321 int showHdr;
1322 char *rowSep;
1323 if( p->cMode==MODE_Column ){
1324 colWidth = p->colWidth;
1325 showHdr = p->showHeader;
1326 rowSep = p->rowSeparator;
1327 }else{
1328 colWidth = aExplainWidths;
1329 showHdr = 1;
1330 rowSep = SEP_Row;
1332 if( p->cnt++==0 ){
1333 for(i=0; i<nArg; i++){
1334 int w, n;
1335 if( i<ArraySize(p->colWidth) ){
1336 w = colWidth[i];
1337 }else{
1338 w = 0;
1340 if( w==0 ){
1341 w = strlenChar(azCol[i] ? azCol[i] : "");
1342 if( w<10 ) w = 10;
1343 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1344 if( w<n ) w = n;
1346 if( i<ArraySize(p->actualWidth) ){
1347 p->actualWidth[i] = w;
1349 if( showHdr ){
1350 utf8_width_print(p->out, w, azCol[i]);
1351 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
1354 if( showHdr ){
1355 for(i=0; i<nArg; i++){
1356 int w;
1357 if( i<ArraySize(p->actualWidth) ){
1358 w = p->actualWidth[i];
1359 if( w<0 ) w = -w;
1360 }else{
1361 w = 10;
1363 utf8_printf(p->out,"%-*.*s%s",w,w,
1364 "----------------------------------------------------------"
1365 "----------------------------------------------------------",
1366 i==nArg-1 ? rowSep : " ");
1370 if( azArg==0 ) break;
1371 for(i=0; i<nArg; i++){
1372 int w;
1373 if( i<ArraySize(p->actualWidth) ){
1374 w = p->actualWidth[i];
1375 }else{
1376 w = 10;
1378 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1379 w = strlenChar(azArg[i]);
1381 if( i==1 && p->aiIndent && p->pStmt ){
1382 if( p->iIndent<p->nIndent ){
1383 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1385 p->iIndent++;
1387 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
1388 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
1390 break;
1392 case MODE_Semi: { /* .schema and .fullschema output */
1393 printSchemaLine(p->out, azArg[0], ";\n");
1394 break;
1396 case MODE_Pretty: { /* .schema and .fullschema with --indent */
1397 char *z;
1398 int j;
1399 int nParen = 0;
1400 char cEnd = 0;
1401 char c;
1402 int nLine = 0;
1403 assert( nArg==1 );
1404 if( azArg[0]==0 ) break;
1405 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1406 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1408 utf8_printf(p->out, "%s;\n", azArg[0]);
1409 break;
1411 z = sqlite3_mprintf("%s", azArg[0]);
1412 j = 0;
1413 for(i=0; IsSpace(z[i]); i++){}
1414 for(; (c = z[i])!=0; i++){
1415 if( IsSpace(c) ){
1416 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1417 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1418 j--;
1420 z[j++] = c;
1422 while( j>0 && IsSpace(z[j-1]) ){ j--; }
1423 z[j] = 0;
1424 if( strlen30(z)>=79 ){
1425 for(i=j=0; (c = z[i])!=0; i++){
1426 if( c==cEnd ){
1427 cEnd = 0;
1428 }else if( c=='"' || c=='\'' || c=='`' ){
1429 cEnd = c;
1430 }else if( c=='[' ){
1431 cEnd = ']';
1432 }else if( c=='(' ){
1433 nParen++;
1434 }else if( c==')' ){
1435 nParen--;
1436 if( nLine>0 && nParen==0 && j>0 ){
1437 printSchemaLineN(p->out, z, j, "\n");
1438 j = 0;
1441 z[j++] = c;
1442 if( nParen==1 && (c=='(' || c==',' || c=='\n') ){
1443 if( c=='\n' ) j--;
1444 printSchemaLineN(p->out, z, j, "\n ");
1445 j = 0;
1446 nLine++;
1447 while( IsSpace(z[i+1]) ){ i++; }
1450 z[j] = 0;
1452 printSchemaLine(p->out, z, ";\n");
1453 sqlite3_free(z);
1454 break;
1456 case MODE_List: {
1457 if( p->cnt++==0 && p->showHeader ){
1458 for(i=0; i<nArg; i++){
1459 utf8_printf(p->out,"%s%s",azCol[i],
1460 i==nArg-1 ? p->rowSeparator : p->colSeparator);
1463 if( azArg==0 ) break;
1464 for(i=0; i<nArg; i++){
1465 char *z = azArg[i];
1466 if( z==0 ) z = p->nullValue;
1467 utf8_printf(p->out, "%s", z);
1468 if( i<nArg-1 ){
1469 utf8_printf(p->out, "%s", p->colSeparator);
1470 }else{
1471 utf8_printf(p->out, "%s", p->rowSeparator);
1474 break;
1476 case MODE_Html: {
1477 if( p->cnt++==0 && p->showHeader ){
1478 raw_printf(p->out,"<TR>");
1479 for(i=0; i<nArg; i++){
1480 raw_printf(p->out,"<TH>");
1481 output_html_string(p->out, azCol[i]);
1482 raw_printf(p->out,"</TH>\n");
1484 raw_printf(p->out,"</TR>\n");
1486 if( azArg==0 ) break;
1487 raw_printf(p->out,"<TR>");
1488 for(i=0; i<nArg; i++){
1489 raw_printf(p->out,"<TD>");
1490 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1491 raw_printf(p->out,"</TD>\n");
1493 raw_printf(p->out,"</TR>\n");
1494 break;
1496 case MODE_Tcl: {
1497 if( p->cnt++==0 && p->showHeader ){
1498 for(i=0; i<nArg; i++){
1499 output_c_string(p->out,azCol[i] ? azCol[i] : "");
1500 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1502 utf8_printf(p->out, "%s", p->rowSeparator);
1504 if( azArg==0 ) break;
1505 for(i=0; i<nArg; i++){
1506 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1507 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1509 utf8_printf(p->out, "%s", p->rowSeparator);
1510 break;
1512 case MODE_Csv: {
1513 setBinaryMode(p->out, 1);
1514 if( p->cnt++==0 && p->showHeader ){
1515 for(i=0; i<nArg; i++){
1516 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
1518 utf8_printf(p->out, "%s", p->rowSeparator);
1520 if( nArg>0 ){
1521 for(i=0; i<nArg; i++){
1522 output_csv(p, azArg[i], i<nArg-1);
1524 utf8_printf(p->out, "%s", p->rowSeparator);
1526 setTextMode(p->out, 1);
1527 break;
1529 case MODE_Insert: {
1530 if( azArg==0 ) break;
1531 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
1532 if( p->showHeader ){
1533 raw_printf(p->out,"(");
1534 for(i=0; i<nArg; i++){
1535 if( i>0 ) raw_printf(p->out, ",");
1536 if( quoteChar(azCol[i]) ){
1537 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
1538 utf8_printf(p->out, "%s", z);
1539 sqlite3_free(z);
1540 }else{
1541 raw_printf(p->out, "%s", azCol[i]);
1544 raw_printf(p->out,")");
1546 p->cnt++;
1547 for(i=0; i<nArg; i++){
1548 raw_printf(p->out, i>0 ? "," : " VALUES(");
1549 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1550 utf8_printf(p->out,"NULL");
1551 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1552 if( ShellHasFlag(p, SHFLG_Newlines) ){
1553 output_quoted_string(p->out, azArg[i]);
1554 }else{
1555 output_quoted_escaped_string(p->out, azArg[i]);
1557 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1558 utf8_printf(p->out,"%s", azArg[i]);
1559 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1560 char z[50];
1561 double r = sqlite3_column_double(p->pStmt, i);
1562 sqlite3_snprintf(50,z,"%!.20g", r);
1563 raw_printf(p->out, "%s", z);
1564 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1565 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1566 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1567 output_hex_blob(p->out, pBlob, nBlob);
1568 }else if( isNumber(azArg[i], 0) ){
1569 utf8_printf(p->out,"%s", azArg[i]);
1570 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
1571 output_quoted_string(p->out, azArg[i]);
1572 }else{
1573 output_quoted_escaped_string(p->out, azArg[i]);
1576 raw_printf(p->out,");\n");
1577 break;
1579 case MODE_Quote: {
1580 if( azArg==0 ) break;
1581 if( p->cnt==0 && p->showHeader ){
1582 for(i=0; i<nArg; i++){
1583 if( i>0 ) raw_printf(p->out, ",");
1584 output_quoted_string(p->out, azCol[i]);
1586 raw_printf(p->out,"\n");
1588 p->cnt++;
1589 for(i=0; i<nArg; i++){
1590 if( i>0 ) raw_printf(p->out, ",");
1591 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1592 utf8_printf(p->out,"NULL");
1593 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1594 output_quoted_string(p->out, azArg[i]);
1595 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1596 utf8_printf(p->out,"%s", azArg[i]);
1597 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1598 char z[50];
1599 double r = sqlite3_column_double(p->pStmt, i);
1600 sqlite3_snprintf(50,z,"%!.20g", r);
1601 raw_printf(p->out, "%s", z);
1602 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1603 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1604 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1605 output_hex_blob(p->out, pBlob, nBlob);
1606 }else if( isNumber(azArg[i], 0) ){
1607 utf8_printf(p->out,"%s", azArg[i]);
1608 }else{
1609 output_quoted_string(p->out, azArg[i]);
1612 raw_printf(p->out,"\n");
1613 break;
1615 case MODE_Ascii: {
1616 if( p->cnt++==0 && p->showHeader ){
1617 for(i=0; i<nArg; i++){
1618 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1619 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
1621 utf8_printf(p->out, "%s", p->rowSeparator);
1623 if( azArg==0 ) break;
1624 for(i=0; i<nArg; i++){
1625 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1626 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
1628 utf8_printf(p->out, "%s", p->rowSeparator);
1629 break;
1632 return 0;
1636 ** This is the callback routine that the SQLite library
1637 ** invokes for each row of a query result.
1639 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
1640 /* since we don't have type info, call the shell_callback with a NULL value */
1641 return shell_callback(pArg, nArg, azArg, azCol, NULL);
1645 ** This is the callback routine from sqlite3_exec() that appends all
1646 ** output onto the end of a ShellText object.
1648 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
1649 ShellText *p = (ShellText*)pArg;
1650 int i;
1651 UNUSED_PARAMETER(az);
1652 if( azArg==0 ) return 0;
1653 if( p->n ) appendText(p, "|", 0);
1654 for(i=0; i<nArg; i++){
1655 if( i ) appendText(p, ",", 0);
1656 if( azArg[i] ) appendText(p, azArg[i], 0);
1658 return 0;
1662 ** Generate an appropriate SELFTEST table in the main database.
1664 static void createSelftestTable(ShellState *p){
1665 char *zErrMsg = 0;
1666 sqlite3_exec(p->db,
1667 "SAVEPOINT selftest_init;\n"
1668 "CREATE TABLE IF NOT EXISTS selftest(\n"
1669 " tno INTEGER PRIMARY KEY,\n" /* Test number */
1670 " op TEXT,\n" /* Operator: memo run */
1671 " cmd TEXT,\n" /* Command text */
1672 " ans TEXT\n" /* Desired answer */
1673 ");"
1674 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
1675 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
1676 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
1677 " 'memo','Tests generated by --init');\n"
1678 "INSERT INTO [_shell$self]\n"
1679 " SELECT 'run',\n"
1680 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
1681 "FROM sqlite_master ORDER BY 2'',224))',\n"
1682 " hex(sha3_query('SELECT type,name,tbl_name,sql "
1683 "FROM sqlite_master ORDER BY 2',224));\n"
1684 "INSERT INTO [_shell$self]\n"
1685 " SELECT 'run',"
1686 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
1687 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
1688 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
1689 " FROM (\n"
1690 " SELECT name FROM sqlite_master\n"
1691 " WHERE type='table'\n"
1692 " AND name<>'selftest'\n"
1693 " AND coalesce(rootpage,0)>0\n"
1694 " )\n"
1695 " ORDER BY name;\n"
1696 "INSERT INTO [_shell$self]\n"
1697 " VALUES('run','PRAGMA integrity_check','ok');\n"
1698 "INSERT INTO selftest(tno,op,cmd,ans)"
1699 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
1700 "DROP TABLE [_shell$self];"
1701 ,0,0,&zErrMsg);
1702 if( zErrMsg ){
1703 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
1704 sqlite3_free(zErrMsg);
1706 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
1711 ** Set the destination table field of the ShellState structure to
1712 ** the name of the table given. Escape any quote characters in the
1713 ** table name.
1715 static void set_table_name(ShellState *p, const char *zName){
1716 int i, n;
1717 char cQuote;
1718 char *z;
1720 if( p->zDestTable ){
1721 free(p->zDestTable);
1722 p->zDestTable = 0;
1724 if( zName==0 ) return;
1725 cQuote = quoteChar(zName);
1726 n = strlen30(zName);
1727 if( cQuote ) n += n+2;
1728 z = p->zDestTable = malloc( n+1 );
1729 if( z==0 ){
1730 raw_printf(stderr,"Error: out of memory\n");
1731 exit(1);
1733 n = 0;
1734 if( cQuote ) z[n++] = cQuote;
1735 for(i=0; zName[i]; i++){
1736 z[n++] = zName[i];
1737 if( zName[i]==cQuote ) z[n++] = cQuote;
1739 if( cQuote ) z[n++] = cQuote;
1740 z[n] = 0;
1745 ** Execute a query statement that will generate SQL output. Print
1746 ** the result columns, comma-separated, on a line and then add a
1747 ** semicolon terminator to the end of that line.
1749 ** If the number of columns is 1 and that column contains text "--"
1750 ** then write the semicolon on a separate line. That way, if a
1751 ** "--" comment occurs at the end of the statement, the comment
1752 ** won't consume the semicolon terminator.
1754 static int run_table_dump_query(
1755 ShellState *p, /* Query context */
1756 const char *zSelect, /* SELECT statement to extract content */
1757 const char *zFirstRow /* Print before first row, if not NULL */
1759 sqlite3_stmt *pSelect;
1760 int rc;
1761 int nResult;
1762 int i;
1763 const char *z;
1764 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1765 if( rc!=SQLITE_OK || !pSelect ){
1766 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1767 sqlite3_errmsg(p->db));
1768 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1769 return rc;
1771 rc = sqlite3_step(pSelect);
1772 nResult = sqlite3_column_count(pSelect);
1773 while( rc==SQLITE_ROW ){
1774 if( zFirstRow ){
1775 utf8_printf(p->out, "%s", zFirstRow);
1776 zFirstRow = 0;
1778 z = (const char*)sqlite3_column_text(pSelect, 0);
1779 utf8_printf(p->out, "%s", z);
1780 for(i=1; i<nResult; i++){
1781 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
1783 if( z==0 ) z = "";
1784 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
1785 if( z[0] ){
1786 raw_printf(p->out, "\n;\n");
1787 }else{
1788 raw_printf(p->out, ";\n");
1790 rc = sqlite3_step(pSelect);
1792 rc = sqlite3_finalize(pSelect);
1793 if( rc!=SQLITE_OK ){
1794 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1795 sqlite3_errmsg(p->db));
1796 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1798 return rc;
1802 ** Allocate space and save off current error string.
1804 static char *save_err_msg(
1805 sqlite3 *db /* Database to query */
1807 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
1808 char *zErrMsg = sqlite3_malloc64(nErrMsg);
1809 if( zErrMsg ){
1810 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
1812 return zErrMsg;
1815 #ifdef __linux__
1817 ** Attempt to display I/O stats on Linux using /proc/PID/io
1819 static void displayLinuxIoStats(FILE *out){
1820 FILE *in;
1821 char z[200];
1822 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1823 in = fopen(z, "rb");
1824 if( in==0 ) return;
1825 while( fgets(z, sizeof(z), in)!=0 ){
1826 static const struct {
1827 const char *zPattern;
1828 const char *zDesc;
1829 } aTrans[] = {
1830 { "rchar: ", "Bytes received by read():" },
1831 { "wchar: ", "Bytes sent to write():" },
1832 { "syscr: ", "Read() system calls:" },
1833 { "syscw: ", "Write() system calls:" },
1834 { "read_bytes: ", "Bytes read from storage:" },
1835 { "write_bytes: ", "Bytes written to storage:" },
1836 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
1838 int i;
1839 for(i=0; i<ArraySize(aTrans); i++){
1840 int n = (int)strlen(aTrans[i].zPattern);
1841 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1842 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
1843 break;
1847 fclose(in);
1849 #endif
1852 ** Display a single line of status using 64-bit values.
1854 static void displayStatLine(
1855 ShellState *p, /* The shell context */
1856 char *zLabel, /* Label for this one line */
1857 char *zFormat, /* Format for the result */
1858 int iStatusCtrl, /* Which status to display */
1859 int bReset /* True to reset the stats */
1861 sqlite3_int64 iCur = -1;
1862 sqlite3_int64 iHiwtr = -1;
1863 int i, nPercent;
1864 char zLine[200];
1865 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
1866 for(i=0, nPercent=0; zFormat[i]; i++){
1867 if( zFormat[i]=='%' ) nPercent++;
1869 if( nPercent>1 ){
1870 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
1871 }else{
1872 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
1874 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
1878 ** Display memory stats.
1880 static int display_stats(
1881 sqlite3 *db, /* Database to query */
1882 ShellState *pArg, /* Pointer to ShellState */
1883 int bReset /* True to reset the stats */
1885 int iCur;
1886 int iHiwtr;
1888 if( pArg && pArg->out ){
1889 displayStatLine(pArg, "Memory Used:",
1890 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
1891 displayStatLine(pArg, "Number of Outstanding Allocations:",
1892 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
1893 if( pArg->shellFlgs & SHFLG_Pagecache ){
1894 displayStatLine(pArg, "Number of Pcache Pages Used:",
1895 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
1897 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
1898 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
1899 displayStatLine(pArg, "Largest Allocation:",
1900 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
1901 displayStatLine(pArg, "Largest Pcache Allocation:",
1902 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
1903 #ifdef YYTRACKMAXSTACKDEPTH
1904 displayStatLine(pArg, "Deepest Parser Stack:",
1905 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
1906 #endif
1909 if( pArg && pArg->out && db ){
1910 if( pArg->shellFlgs & SHFLG_Lookaside ){
1911 iHiwtr = iCur = -1;
1912 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1913 &iCur, &iHiwtr, bReset);
1914 raw_printf(pArg->out,
1915 "Lookaside Slots Used: %d (max %d)\n",
1916 iCur, iHiwtr);
1917 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1918 &iCur, &iHiwtr, bReset);
1919 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
1920 iHiwtr);
1921 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1922 &iCur, &iHiwtr, bReset);
1923 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
1924 iHiwtr);
1925 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1926 &iCur, &iHiwtr, bReset);
1927 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
1928 iHiwtr);
1930 iHiwtr = iCur = -1;
1931 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1932 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
1933 iCur);
1934 iHiwtr = iCur = -1;
1935 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1936 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
1937 iHiwtr = iCur = -1;
1938 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1939 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
1940 iHiwtr = iCur = -1;
1941 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1942 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
1943 iHiwtr = iCur = -1;
1944 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1945 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
1946 iCur);
1947 iHiwtr = iCur = -1;
1948 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1949 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
1950 iCur);
1953 if( pArg && pArg->out && db && pArg->pStmt ){
1954 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1955 bReset);
1956 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
1957 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1958 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
1959 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
1960 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1961 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1962 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1965 #ifdef __linux__
1966 displayLinuxIoStats(pArg->out);
1967 #endif
1969 /* Do not remove this machine readable comment: extra-stats-output-here */
1971 return 0;
1975 ** Display scan stats.
1977 static void display_scanstats(
1978 sqlite3 *db, /* Database to query */
1979 ShellState *pArg /* Pointer to ShellState */
1981 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1982 UNUSED_PARAMETER(db);
1983 UNUSED_PARAMETER(pArg);
1984 #else
1985 int i, k, n, mx;
1986 raw_printf(pArg->out, "-------- scanstats --------\n");
1987 mx = 0;
1988 for(k=0; k<=mx; k++){
1989 double rEstLoop = 1.0;
1990 for(i=n=0; 1; i++){
1991 sqlite3_stmt *p = pArg->pStmt;
1992 sqlite3_int64 nLoop, nVisit;
1993 double rEst;
1994 int iSid;
1995 const char *zExplain;
1996 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
1997 break;
1999 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2000 if( iSid>mx ) mx = iSid;
2001 if( iSid!=k ) continue;
2002 if( n==0 ){
2003 rEstLoop = (double)nLoop;
2004 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2006 n++;
2007 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2008 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2009 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2010 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2011 rEstLoop *= rEst;
2012 raw_printf(pArg->out,
2013 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2014 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2018 raw_printf(pArg->out, "---------------------------\n");
2019 #endif
2023 ** Parameter azArray points to a zero-terminated array of strings. zStr
2024 ** points to a single nul-terminated string. Return non-zero if zStr
2025 ** is equal, according to strcmp(), to any of the strings in the array.
2026 ** Otherwise, return zero.
2028 static int str_in_array(const char *zStr, const char **azArray){
2029 int i;
2030 for(i=0; azArray[i]; i++){
2031 if( 0==strcmp(zStr, azArray[i]) ) return 1;
2033 return 0;
2037 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2038 ** and populate the ShellState.aiIndent[] array with the number of
2039 ** spaces each opcode should be indented before it is output.
2041 ** The indenting rules are:
2043 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2044 ** all opcodes that occur between the p2 jump destination and the opcode
2045 ** itself by 2 spaces.
2047 ** * For each "Goto", if the jump destination is earlier in the program
2048 ** and ends on one of:
2049 ** Yield SeekGt SeekLt RowSetRead Rewind
2050 ** or if the P1 parameter is one instead of zero,
2051 ** then indent all opcodes between the earlier instruction
2052 ** and "Goto" by 2 spaces.
2054 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2055 const char *zSql; /* The text of the SQL statement */
2056 const char *z; /* Used to check if this is an EXPLAIN */
2057 int *abYield = 0; /* True if op is an OP_Yield */
2058 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
2059 int iOp; /* Index of operation in p->aiIndent[] */
2061 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
2062 "NextIfOpen", "PrevIfOpen", 0 };
2063 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2064 "Rewind", 0 };
2065 const char *azGoto[] = { "Goto", 0 };
2067 /* Try to figure out if this is really an EXPLAIN statement. If this
2068 ** cannot be verified, return early. */
2069 if( sqlite3_column_count(pSql)!=8 ){
2070 p->cMode = p->mode;
2071 return;
2073 zSql = sqlite3_sql(pSql);
2074 if( zSql==0 ) return;
2075 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2076 if( sqlite3_strnicmp(z, "explain", 7) ){
2077 p->cMode = p->mode;
2078 return;
2081 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2082 int i;
2083 int iAddr = sqlite3_column_int(pSql, 0);
2084 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2086 /* Set p2 to the P2 field of the current opcode. Then, assuming that
2087 ** p2 is an instruction address, set variable p2op to the index of that
2088 ** instruction in the aiIndent[] array. p2 and p2op may be different if
2089 ** the current instruction is part of a sub-program generated by an
2090 ** SQL trigger or foreign key. */
2091 int p2 = sqlite3_column_int(pSql, 3);
2092 int p2op = (p2 + (iOp-iAddr));
2094 /* Grow the p->aiIndent array as required */
2095 if( iOp>=nAlloc ){
2096 if( iOp==0 ){
2097 /* Do further verfication that this is explain output. Abort if
2098 ** it is not */
2099 static const char *explainCols[] = {
2100 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2101 int jj;
2102 for(jj=0; jj<ArraySize(explainCols); jj++){
2103 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2104 p->cMode = p->mode;
2105 sqlite3_reset(pSql);
2106 return;
2110 nAlloc += 100;
2111 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2112 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2114 abYield[iOp] = str_in_array(zOp, azYield);
2115 p->aiIndent[iOp] = 0;
2116 p->nIndent = iOp+1;
2118 if( str_in_array(zOp, azNext) ){
2119 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2121 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2122 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2124 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2128 p->iIndent = 0;
2129 sqlite3_free(abYield);
2130 sqlite3_reset(pSql);
2134 ** Free the array allocated by explain_data_prepare().
2136 static void explain_data_delete(ShellState *p){
2137 sqlite3_free(p->aiIndent);
2138 p->aiIndent = 0;
2139 p->nIndent = 0;
2140 p->iIndent = 0;
2144 ** Disable and restore .wheretrace and .selecttrace settings.
2146 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2147 extern int sqlite3SelectTrace;
2148 static int savedSelectTrace;
2149 #endif
2150 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2151 extern int sqlite3WhereTrace;
2152 static int savedWhereTrace;
2153 #endif
2154 static void disable_debug_trace_modes(void){
2155 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2156 savedSelectTrace = sqlite3SelectTrace;
2157 sqlite3SelectTrace = 0;
2158 #endif
2159 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2160 savedWhereTrace = sqlite3WhereTrace;
2161 sqlite3WhereTrace = 0;
2162 #endif
2164 static void restore_debug_trace_modes(void){
2165 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2166 sqlite3SelectTrace = savedSelectTrace;
2167 #endif
2168 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2169 sqlite3WhereTrace = savedWhereTrace;
2170 #endif
2174 ** Run a prepared statement
2176 static void exec_prepared_stmt(
2177 ShellState *pArg, /* Pointer to ShellState */
2178 sqlite3_stmt *pStmt, /* Statment to run */
2179 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
2181 int rc;
2183 /* perform the first step. this will tell us if we
2184 ** have a result set or not and how wide it is.
2186 rc = sqlite3_step(pStmt);
2187 /* if we have a result set... */
2188 if( SQLITE_ROW == rc ){
2189 /* if we have a callback... */
2190 if( xCallback ){
2191 /* allocate space for col name ptr, value ptr, and type */
2192 int nCol = sqlite3_column_count(pStmt);
2193 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2194 if( !pData ){
2195 rc = SQLITE_NOMEM;
2196 }else{
2197 char **azCols = (char **)pData; /* Names of result columns */
2198 char **azVals = &azCols[nCol]; /* Results */
2199 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2200 int i, x;
2201 assert(sizeof(int) <= sizeof(char *));
2202 /* save off ptrs to column names */
2203 for(i=0; i<nCol; i++){
2204 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2207 /* extract the data and data types */
2208 for(i=0; i<nCol; i++){
2209 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2210 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2211 azVals[i] = "";
2212 }else{
2213 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2215 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2216 rc = SQLITE_NOMEM;
2217 break; /* from for */
2219 } /* end for */
2221 /* if data and types extracted successfully... */
2222 if( SQLITE_ROW == rc ){
2223 /* call the supplied callback with the result row data */
2224 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
2225 rc = SQLITE_ABORT;
2226 }else{
2227 rc = sqlite3_step(pStmt);
2230 } while( SQLITE_ROW == rc );
2231 sqlite3_free(pData);
2233 }else{
2235 rc = sqlite3_step(pStmt);
2236 } while( rc == SQLITE_ROW );
2242 ** Execute a statement or set of statements. Print
2243 ** any result rows/columns depending on the current mode
2244 ** set via the supplied callback.
2246 ** This is very similar to SQLite's built-in sqlite3_exec()
2247 ** function except it takes a slightly different callback
2248 ** and callback data argument.
2250 static int shell_exec(
2251 sqlite3 *db, /* An open database */
2252 const char *zSql, /* SQL to be evaluated */
2253 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
2254 /* (not the same as sqlite3_exec) */
2255 ShellState *pArg, /* Pointer to ShellState */
2256 char **pzErrMsg /* Error msg written here */
2258 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
2259 int rc = SQLITE_OK; /* Return Code */
2260 int rc2;
2261 const char *zLeftover; /* Tail of unprocessed SQL */
2263 if( pzErrMsg ){
2264 *pzErrMsg = NULL;
2267 while( zSql[0] && (SQLITE_OK == rc) ){
2268 static const char *zStmtSql;
2269 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
2270 if( SQLITE_OK != rc ){
2271 if( pzErrMsg ){
2272 *pzErrMsg = save_err_msg(db);
2274 }else{
2275 if( !pStmt ){
2276 /* this happens for a comment or white-space */
2277 zSql = zLeftover;
2278 while( IsSpace(zSql[0]) ) zSql++;
2279 continue;
2281 zStmtSql = sqlite3_sql(pStmt);
2282 if( zStmtSql==0 ) zStmtSql = "";
2283 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
2285 /* save off the prepared statment handle and reset row count */
2286 if( pArg ){
2287 pArg->pStmt = pStmt;
2288 pArg->cnt = 0;
2291 /* echo the sql statement if echo on */
2292 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
2293 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
2296 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2297 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
2298 sqlite3_stmt *pExplain;
2299 char *zEQP;
2300 disable_debug_trace_modes();
2301 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
2302 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2303 if( rc==SQLITE_OK ){
2304 while( sqlite3_step(pExplain)==SQLITE_ROW ){
2305 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
2306 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
2307 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
2308 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
2311 sqlite3_finalize(pExplain);
2312 sqlite3_free(zEQP);
2313 if( pArg->autoEQP>=2 ){
2314 /* Also do an EXPLAIN for ".eqp full" mode */
2315 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
2316 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2317 if( rc==SQLITE_OK ){
2318 pArg->cMode = MODE_Explain;
2319 explain_data_prepare(pArg, pExplain);
2320 exec_prepared_stmt(pArg, pExplain, xCallback);
2321 explain_data_delete(pArg);
2323 sqlite3_finalize(pExplain);
2324 sqlite3_free(zEQP);
2326 restore_debug_trace_modes();
2329 if( pArg ){
2330 pArg->cMode = pArg->mode;
2331 if( pArg->autoExplain
2332 && sqlite3_column_count(pStmt)==8
2333 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
2335 pArg->cMode = MODE_Explain;
2338 /* If the shell is currently in ".explain" mode, gather the extra
2339 ** data required to add indents to the output.*/
2340 if( pArg->cMode==MODE_Explain ){
2341 explain_data_prepare(pArg, pStmt);
2345 exec_prepared_stmt(pArg, pStmt, xCallback);
2346 explain_data_delete(pArg);
2348 /* print usage stats if stats on */
2349 if( pArg && pArg->statsOn ){
2350 display_stats(db, pArg, 0);
2353 /* print loop-counters if required */
2354 if( pArg && pArg->scanstatsOn ){
2355 display_scanstats(db, pArg);
2358 /* Finalize the statement just executed. If this fails, save a
2359 ** copy of the error message. Otherwise, set zSql to point to the
2360 ** next statement to execute. */
2361 rc2 = sqlite3_finalize(pStmt);
2362 if( rc!=SQLITE_NOMEM ) rc = rc2;
2363 if( rc==SQLITE_OK ){
2364 zSql = zLeftover;
2365 while( IsSpace(zSql[0]) ) zSql++;
2366 }else if( pzErrMsg ){
2367 *pzErrMsg = save_err_msg(db);
2370 /* clear saved stmt handle */
2371 if( pArg ){
2372 pArg->pStmt = NULL;
2375 } /* end while */
2377 return rc;
2381 ** Release memory previously allocated by tableColumnList().
2383 static void freeColumnList(char **azCol){
2384 int i;
2385 for(i=1; azCol[i]; i++){
2386 sqlite3_free(azCol[i]);
2388 /* azCol[0] is a static string */
2389 sqlite3_free(azCol);
2393 ** Return a list of pointers to strings which are the names of all
2394 ** columns in table zTab. The memory to hold the names is dynamically
2395 ** allocated and must be released by the caller using a subsequent call
2396 ** to freeColumnList().
2398 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
2399 ** value that needs to be preserved, then azCol[0] is filled in with the
2400 ** name of the rowid column.
2402 ** The first regular column in the table is azCol[1]. The list is terminated
2403 ** by an entry with azCol[i]==0.
2405 static char **tableColumnList(ShellState *p, const char *zTab){
2406 char **azCol = 0;
2407 sqlite3_stmt *pStmt;
2408 char *zSql;
2409 int nCol = 0;
2410 int nAlloc = 0;
2411 int nPK = 0; /* Number of PRIMARY KEY columns seen */
2412 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2413 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2414 int rc;
2416 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2417 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2418 sqlite3_free(zSql);
2419 if( rc ) return 0;
2420 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2421 if( nCol>=nAlloc-2 ){
2422 nAlloc = nAlloc*2 + nCol + 10;
2423 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2424 if( azCol==0 ){
2425 raw_printf(stderr, "Error: out of memory\n");
2426 exit(1);
2429 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2430 if( sqlite3_column_int(pStmt, 5) ){
2431 nPK++;
2432 if( nPK==1
2433 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2434 "INTEGER")==0
2436 isIPK = 1;
2437 }else{
2438 isIPK = 0;
2442 sqlite3_finalize(pStmt);
2443 azCol[0] = 0;
2444 azCol[nCol+1] = 0;
2446 /* The decision of whether or not a rowid really needs to be preserved
2447 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
2448 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
2449 ** rowids on tables where the rowid is inaccessible because there are other
2450 ** columns in the table named "rowid", "_rowid_", and "oid".
2452 if( preserveRowid && isIPK ){
2453 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
2454 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
2455 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
2456 ** ROWID aliases. To distinguish these cases, check to see if
2457 ** there is a "pk" entry in "PRAGMA index_list". There will be
2458 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
2460 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
2461 " WHERE origin='pk'", zTab);
2462 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2463 sqlite3_free(zSql);
2464 if( rc ){
2465 freeColumnList(azCol);
2466 return 0;
2468 rc = sqlite3_step(pStmt);
2469 sqlite3_finalize(pStmt);
2470 preserveRowid = rc==SQLITE_ROW;
2472 if( preserveRowid ){
2473 /* Only preserve the rowid if we can find a name to use for the
2474 ** rowid */
2475 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
2476 int i, j;
2477 for(j=0; j<3; j++){
2478 for(i=1; i<=nCol; i++){
2479 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
2481 if( i>nCol ){
2482 /* At this point, we know that azRowid[j] is not the name of any
2483 ** ordinary column in the table. Verify that azRowid[j] is a valid
2484 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
2485 ** tables will fail this last check */
2486 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
2487 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
2488 break;
2492 return azCol;
2496 ** Toggle the reverse_unordered_selects setting.
2498 static void toggleSelectOrder(sqlite3 *db){
2499 sqlite3_stmt *pStmt = 0;
2500 int iSetting = 0;
2501 char zStmt[100];
2502 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
2503 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2504 iSetting = sqlite3_column_int(pStmt, 0);
2506 sqlite3_finalize(pStmt);
2507 sqlite3_snprintf(sizeof(zStmt), zStmt,
2508 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
2509 sqlite3_exec(db, zStmt, 0, 0, 0);
2513 ** This is a different callback routine used for dumping the database.
2514 ** Each row received by this callback consists of a table name,
2515 ** the table type ("index" or "table") and SQL to create the table.
2516 ** This routine should print text sufficient to recreate the table.
2518 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
2519 int rc;
2520 const char *zTable;
2521 const char *zType;
2522 const char *zSql;
2523 ShellState *p = (ShellState *)pArg;
2525 UNUSED_PARAMETER(azNotUsed);
2526 if( nArg!=3 || azArg==0 ) return 0;
2527 zTable = azArg[0];
2528 zType = azArg[1];
2529 zSql = azArg[2];
2531 if( strcmp(zTable, "sqlite_sequence")==0 ){
2532 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
2533 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
2534 raw_printf(p->out, "ANALYZE sqlite_master;\n");
2535 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
2536 return 0;
2537 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
2538 char *zIns;
2539 if( !p->writableSchema ){
2540 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
2541 p->writableSchema = 1;
2543 zIns = sqlite3_mprintf(
2544 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
2545 "VALUES('table','%q','%q',0,'%q');",
2546 zTable, zTable, zSql);
2547 utf8_printf(p->out, "%s\n", zIns);
2548 sqlite3_free(zIns);
2549 return 0;
2550 }else{
2551 printSchemaLine(p->out, zSql, ";\n");
2554 if( strcmp(zType, "table")==0 ){
2555 ShellText sSelect;
2556 ShellText sTable;
2557 char **azCol;
2558 int i;
2559 char *savedDestTable;
2560 int savedMode;
2562 azCol = tableColumnList(p, zTable);
2563 if( azCol==0 ){
2564 p->nErr++;
2565 return 0;
2568 /* Always quote the table name, even if it appears to be pure ascii,
2569 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
2570 initText(&sTable);
2571 appendText(&sTable, zTable, quoteChar(zTable));
2572 /* If preserving the rowid, add a column list after the table name.
2573 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
2574 ** instead of the usual "INSERT INTO tab VALUES(...)".
2576 if( azCol[0] ){
2577 appendText(&sTable, "(", 0);
2578 appendText(&sTable, azCol[0], 0);
2579 for(i=1; azCol[i]; i++){
2580 appendText(&sTable, ",", 0);
2581 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
2583 appendText(&sTable, ")", 0);
2586 /* Build an appropriate SELECT statement */
2587 initText(&sSelect);
2588 appendText(&sSelect, "SELECT ", 0);
2589 if( azCol[0] ){
2590 appendText(&sSelect, azCol[0], 0);
2591 appendText(&sSelect, ",", 0);
2593 for(i=1; azCol[i]; i++){
2594 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
2595 if( azCol[i+1] ){
2596 appendText(&sSelect, ",", 0);
2599 freeColumnList(azCol);
2600 appendText(&sSelect, " FROM ", 0);
2601 appendText(&sSelect, zTable, quoteChar(zTable));
2603 savedDestTable = p->zDestTable;
2604 savedMode = p->mode;
2605 p->zDestTable = sTable.z;
2606 p->mode = p->cMode = MODE_Insert;
2607 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2608 if( (rc&0xff)==SQLITE_CORRUPT ){
2609 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2610 toggleSelectOrder(p->db);
2611 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2612 toggleSelectOrder(p->db);
2614 p->zDestTable = savedDestTable;
2615 p->mode = savedMode;
2616 freeText(&sTable);
2617 freeText(&sSelect);
2618 if( rc ) p->nErr++;
2620 return 0;
2624 ** Run zQuery. Use dump_callback() as the callback routine so that
2625 ** the contents of the query are output as SQL statements.
2627 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
2628 ** "ORDER BY rowid DESC" to the end.
2630 static int run_schema_dump_query(
2631 ShellState *p,
2632 const char *zQuery
2634 int rc;
2635 char *zErr = 0;
2636 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
2637 if( rc==SQLITE_CORRUPT ){
2638 char *zQ2;
2639 int len = strlen30(zQuery);
2640 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2641 if( zErr ){
2642 utf8_printf(p->out, "/****** %s ******/\n", zErr);
2643 sqlite3_free(zErr);
2644 zErr = 0;
2646 zQ2 = malloc( len+100 );
2647 if( zQ2==0 ) return rc;
2648 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
2649 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
2650 if( rc ){
2651 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
2652 }else{
2653 rc = SQLITE_CORRUPT;
2655 sqlite3_free(zErr);
2656 free(zQ2);
2658 return rc;
2662 ** Text of a help message
2664 static char zHelp[] =
2665 #ifndef SQLITE_OMIT_AUTHORIZATION
2666 ".auth ON|OFF Show authorizer callbacks\n"
2667 #endif
2668 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
2669 ".bail on|off Stop after hitting an error. Default OFF\n"
2670 ".binary on|off Turn binary output on or off. Default OFF\n"
2671 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
2672 ".changes on|off Show number of rows changed by SQL\n"
2673 ".check GLOB Fail if output since .testcase does not match\n"
2674 ".clone NEWDB Clone data into NEWDB from the existing database\n"
2675 ".databases List names and files of attached databases\n"
2676 ".dbinfo ?DB? Show status information about the database\n"
2677 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
2678 " If TABLE specified, only dump tables matching\n"
2679 " LIKE pattern TABLE.\n"
2680 ".echo on|off Turn command echo on or off\n"
2681 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
2682 ".exit Exit this program\n"
2683 /* Because explain mode comes on automatically now, the ".explain" mode
2684 ** is removed from the help screen. It is still supported for legacy, however */
2685 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
2686 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
2687 ".headers on|off Turn display of headers on or off\n"
2688 ".help Show this message\n"
2689 ".import FILE TABLE Import data from FILE into TABLE\n"
2690 #ifndef SQLITE_OMIT_TEST_CONTROL
2691 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
2692 #endif
2693 ".indexes ?TABLE? Show names of all indexes\n"
2694 " If TABLE specified, only show indexes for tables\n"
2695 " matching LIKE pattern TABLE.\n"
2696 #ifdef SQLITE_ENABLE_IOTRACE
2697 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
2698 #endif
2699 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
2700 ".lint OPTIONS Report potential schema issues. Options:\n"
2701 " fkey-indexes Find missing foreign key indexes\n"
2702 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2703 ".load FILE ?ENTRY? Load an extension library\n"
2704 #endif
2705 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
2706 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
2707 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
2708 " csv Comma-separated values\n"
2709 " column Left-aligned columns. (See .width)\n"
2710 " html HTML <table> code\n"
2711 " insert SQL insert statements for TABLE\n"
2712 " line One value per line\n"
2713 " list Values delimited by \"|\"\n"
2714 " quote Escape answers as for SQL\n"
2715 " tabs Tab-separated values\n"
2716 " tcl TCL list elements\n"
2717 ".nullvalue STRING Use STRING in place of NULL values\n"
2718 ".once FILENAME Output for the next SQL command only to FILENAME\n"
2719 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
2720 " The --new option starts with an empty file\n"
2721 ".output ?FILENAME? Send output to FILENAME or stdout\n"
2722 ".print STRING... Print literal STRING\n"
2723 ".prompt MAIN CONTINUE Replace the standard prompts\n"
2724 ".quit Exit this program\n"
2725 ".read FILENAME Execute SQL in FILENAME\n"
2726 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
2727 ".save FILE Write in-memory database into FILE\n"
2728 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
2729 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
2730 " Add --indent for pretty-printing\n"
2731 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
2732 ".separator COL ?ROW? Change the column separator and optionally the row\n"
2733 " separator for both the output mode and .import\n"
2734 #if defined(SQLITE_ENABLE_SESSION)
2735 ".session CMD ... Create or control sessions\n"
2736 #endif
2737 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
2738 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
2739 ".show Show the current values for various settings\n"
2740 ".stats ?on|off? Show stats or turn stats on or off\n"
2741 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
2742 ".tables ?TABLE? List names of tables\n"
2743 " If TABLE specified, only list tables matching\n"
2744 " LIKE pattern TABLE.\n"
2745 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
2746 ".timeout MS Try opening locked tables for MS milliseconds\n"
2747 ".timer on|off Turn SQL timer on or off\n"
2748 ".trace FILE|off Output each SQL statement as it is run\n"
2749 ".vfsinfo ?AUX? Information about the top-level VFS\n"
2750 ".vfslist List all available VFSes\n"
2751 ".vfsname ?AUX? Print the name of the VFS stack\n"
2752 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
2753 " Negative values right-justify\n"
2756 #if defined(SQLITE_ENABLE_SESSION)
2758 ** Print help information for the ".sessions" command
2760 void session_help(ShellState *p){
2761 raw_printf(p->out,
2762 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
2763 "If ?NAME? is omitted, the first defined session is used.\n"
2764 "Subcommands:\n"
2765 " attach TABLE Attach TABLE\n"
2766 " changeset FILE Write a changeset into FILE\n"
2767 " close Close one session\n"
2768 " enable ?BOOLEAN? Set or query the enable bit\n"
2769 " filter GLOB... Reject tables matching GLOBs\n"
2770 " indirect ?BOOLEAN? Mark or query the indirect status\n"
2771 " isempty Query whether the session is empty\n"
2772 " list List currently open session names\n"
2773 " open DB NAME Open a new session on DB\n"
2774 " patchset FILE Write a patchset into FILE\n"
2777 #endif
2780 /* Forward reference */
2781 static int process_input(ShellState *p, FILE *in);
2784 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
2785 ** and return a pointer to the buffer. The caller is responsible for freeing
2786 ** the memory.
2788 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
2789 ** read.
2791 ** For convenience, a nul-terminator byte is always appended to the data read
2792 ** from the file before the buffer is returned. This byte is not included in
2793 ** the final value of (*pnByte), if applicable.
2795 ** NULL is returned if any error is encountered. The final value of *pnByte
2796 ** is undefined in this case.
2798 static char *readFile(const char *zName, int *pnByte){
2799 FILE *in = fopen(zName, "rb");
2800 long nIn;
2801 size_t nRead;
2802 char *pBuf;
2803 if( in==0 ) return 0;
2804 fseek(in, 0, SEEK_END);
2805 nIn = ftell(in);
2806 rewind(in);
2807 pBuf = sqlite3_malloc64( nIn+1 );
2808 if( pBuf==0 ) return 0;
2809 nRead = fread(pBuf, nIn, 1, in);
2810 fclose(in);
2811 if( nRead!=1 ){
2812 sqlite3_free(pBuf);
2813 return 0;
2815 pBuf[nIn] = 0;
2816 if( pnByte ) *pnByte = nIn;
2817 return pBuf;
2820 #if defined(SQLITE_ENABLE_SESSION)
2822 ** Close a single OpenSession object and release all of its associated
2823 ** resources.
2825 static void session_close(OpenSession *pSession){
2826 int i;
2827 sqlite3session_delete(pSession->p);
2828 sqlite3_free(pSession->zName);
2829 for(i=0; i<pSession->nFilter; i++){
2830 sqlite3_free(pSession->azFilter[i]);
2832 sqlite3_free(pSession->azFilter);
2833 memset(pSession, 0, sizeof(OpenSession));
2835 #endif
2838 ** Close all OpenSession objects and release all associated resources.
2840 #if defined(SQLITE_ENABLE_SESSION)
2841 static void session_close_all(ShellState *p){
2842 int i;
2843 for(i=0; i<p->nSession; i++){
2844 session_close(&p->aSession[i]);
2846 p->nSession = 0;
2848 #else
2849 # define session_close_all(X)
2850 #endif
2853 ** Implementation of the xFilter function for an open session. Omit
2854 ** any tables named by ".session filter" but let all other table through.
2856 #if defined(SQLITE_ENABLE_SESSION)
2857 static int session_filter(void *pCtx, const char *zTab){
2858 OpenSession *pSession = (OpenSession*)pCtx;
2859 int i;
2860 for(i=0; i<pSession->nFilter; i++){
2861 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
2863 return 1;
2865 #endif
2868 ** Make sure the database is open. If it is not, then open it. If
2869 ** the database fails to open, print an error message and exit.
2871 static void open_db(ShellState *p, int keepAlive){
2872 if( p->db==0 ){
2873 sqlite3_initialize();
2874 sqlite3_open(p->zDbFilename, &p->db);
2875 globalDb = p->db;
2876 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2877 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
2878 p->zDbFilename, sqlite3_errmsg(p->db));
2879 if( keepAlive ) return;
2880 exit(1);
2882 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2883 sqlite3_enable_load_extension(p->db, 1);
2884 #endif
2885 sqlite3_fileio_init(p->db, 0, 0);
2886 sqlite3_shathree_init(p->db, 0, 0);
2887 sqlite3_completion_init(p->db, 0, 0);
2888 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
2889 shellAddSchemaName, 0, 0);
2893 #if HAVE_READLINE || HAVE_EDITLINE
2895 ** Readline completion callbacks
2897 static char *readline_completion_generator(const char *text, int state){
2898 static sqlite3_stmt *pStmt = 0;
2899 char *zRet;
2900 if( state==0 ){
2901 char *zSql;
2902 sqlite3_finalize(pStmt);
2903 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2904 " FROM completion(%Q) ORDER BY 1", text);
2905 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2906 sqlite3_free(zSql);
2908 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2909 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
2910 }else{
2911 sqlite3_finalize(pStmt);
2912 pStmt = 0;
2913 zRet = 0;
2915 return zRet;
2917 static char **readline_completion(const char *zText, int iStart, int iEnd){
2918 rl_attempted_completion_over = 1;
2919 return rl_completion_matches(zText, readline_completion_generator);
2922 #elif HAVE_LINENOISE
2924 ** Linenoise completion callback
2926 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
2927 int nLine = (int)strlen(zLine);
2928 int i, iStart;
2929 sqlite3_stmt *pStmt = 0;
2930 char *zSql;
2931 char zBuf[1000];
2933 if( nLine>sizeof(zBuf)-30 ) return;
2934 if( zLine[0]=='.' ) return;
2935 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
2936 if( i==nLine-1 ) return;
2937 iStart = i+1;
2938 memcpy(zBuf, zLine, iStart);
2939 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2940 " FROM completion(%Q,%Q) ORDER BY 1",
2941 &zLine[iStart], zLine);
2942 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2943 sqlite3_free(zSql);
2944 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
2945 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2946 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
2947 int nCompletion = sqlite3_column_bytes(pStmt, 0);
2948 if( iStart+nCompletion < sizeof(zBuf)-1 ){
2949 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
2950 linenoiseAddCompletion(lc, zBuf);
2953 sqlite3_finalize(pStmt);
2955 #endif
2958 ** Do C-language style dequoting.
2960 ** \a -> alarm
2961 ** \b -> backspace
2962 ** \t -> tab
2963 ** \n -> newline
2964 ** \v -> vertical tab
2965 ** \f -> form feed
2966 ** \r -> carriage return
2967 ** \s -> space
2968 ** \" -> "
2969 ** \' -> '
2970 ** \\ -> backslash
2971 ** \NNN -> ascii character NNN in octal
2973 static void resolve_backslashes(char *z){
2974 int i, j;
2975 char c;
2976 while( *z && *z!='\\' ) z++;
2977 for(i=j=0; (c = z[i])!=0; i++, j++){
2978 if( c=='\\' && z[i+1]!=0 ){
2979 c = z[++i];
2980 if( c=='a' ){
2981 c = '\a';
2982 }else if( c=='b' ){
2983 c = '\b';
2984 }else if( c=='t' ){
2985 c = '\t';
2986 }else if( c=='n' ){
2987 c = '\n';
2988 }else if( c=='v' ){
2989 c = '\v';
2990 }else if( c=='f' ){
2991 c = '\f';
2992 }else if( c=='r' ){
2993 c = '\r';
2994 }else if( c=='"' ){
2995 c = '"';
2996 }else if( c=='\'' ){
2997 c = '\'';
2998 }else if( c=='\\' ){
2999 c = '\\';
3000 }else if( c>='0' && c<='7' ){
3001 c -= '0';
3002 if( z[i+1]>='0' && z[i+1]<='7' ){
3003 i++;
3004 c = (c<<3) + z[i] - '0';
3005 if( z[i+1]>='0' && z[i+1]<='7' ){
3006 i++;
3007 c = (c<<3) + z[i] - '0';
3012 z[j] = c;
3014 if( j<i ) z[j] = 0;
3018 ** Return the value of a hexadecimal digit. Return -1 if the input
3019 ** is not a hex digit.
3021 static int hexDigitValue(char c){
3022 if( c>='0' && c<='9' ) return c - '0';
3023 if( c>='a' && c<='f' ) return c - 'a' + 10;
3024 if( c>='A' && c<='F' ) return c - 'A' + 10;
3025 return -1;
3029 ** Interpret zArg as an integer value, possibly with suffixes.
3031 static sqlite3_int64 integerValue(const char *zArg){
3032 sqlite3_int64 v = 0;
3033 static const struct { char *zSuffix; int iMult; } aMult[] = {
3034 { "KiB", 1024 },
3035 { "MiB", 1024*1024 },
3036 { "GiB", 1024*1024*1024 },
3037 { "KB", 1000 },
3038 { "MB", 1000000 },
3039 { "GB", 1000000000 },
3040 { "K", 1000 },
3041 { "M", 1000000 },
3042 { "G", 1000000000 },
3044 int i;
3045 int isNeg = 0;
3046 if( zArg[0]=='-' ){
3047 isNeg = 1;
3048 zArg++;
3049 }else if( zArg[0]=='+' ){
3050 zArg++;
3052 if( zArg[0]=='0' && zArg[1]=='x' ){
3053 int x;
3054 zArg += 2;
3055 while( (x = hexDigitValue(zArg[0]))>=0 ){
3056 v = (v<<4) + x;
3057 zArg++;
3059 }else{
3060 while( IsDigit(zArg[0]) ){
3061 v = v*10 + zArg[0] - '0';
3062 zArg++;
3065 for(i=0; i<ArraySize(aMult); i++){
3066 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
3067 v *= aMult[i].iMult;
3068 break;
3071 return isNeg? -v : v;
3075 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
3076 ** for TRUE and FALSE. Return the integer value if appropriate.
3078 static int booleanValue(const char *zArg){
3079 int i;
3080 if( zArg[0]=='0' && zArg[1]=='x' ){
3081 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3082 }else{
3083 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
3085 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
3086 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
3087 return 1;
3089 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
3090 return 0;
3092 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3093 zArg);
3094 return 0;
3098 ** Set or clear a shell flag according to a boolean value.
3100 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3101 if( booleanValue(zArg) ){
3102 ShellSetFlag(p, mFlag);
3103 }else{
3104 ShellClearFlag(p, mFlag);
3109 ** Close an output file, assuming it is not stderr or stdout
3111 static void output_file_close(FILE *f){
3112 if( f && f!=stdout && f!=stderr ) fclose(f);
3116 ** Try to open an output file. The names "stdout" and "stderr" are
3117 ** recognized and do the right thing. NULL is returned if the output
3118 ** filename is "off".
3120 static FILE *output_file_open(const char *zFile){
3121 FILE *f;
3122 if( strcmp(zFile,"stdout")==0 ){
3123 f = stdout;
3124 }else if( strcmp(zFile, "stderr")==0 ){
3125 f = stderr;
3126 }else if( strcmp(zFile, "off")==0 ){
3127 f = 0;
3128 }else{
3129 f = fopen(zFile, "wb");
3130 if( f==0 ){
3131 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
3134 return f;
3137 #if !defined(SQLITE_UNTESTABLE)
3138 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3140 ** A routine for handling output from sqlite3_trace().
3142 static int sql_trace_callback(
3143 unsigned mType,
3144 void *pArg,
3145 void *pP,
3146 void *pX
3148 FILE *f = (FILE*)pArg;
3149 UNUSED_PARAMETER(mType);
3150 UNUSED_PARAMETER(pP);
3151 if( f ){
3152 const char *z = (const char*)pX;
3153 int i = (int)strlen(z);
3154 while( i>0 && z[i-1]==';' ){ i--; }
3155 utf8_printf(f, "%.*s;\n", i, z);
3157 return 0;
3159 #endif
3160 #endif
3163 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
3164 ** a useful spot to set a debugger breakpoint.
3166 static void test_breakpoint(void){
3167 static int nCall = 0;
3168 nCall++;
3172 ** An object used to read a CSV and other files for import.
3174 typedef struct ImportCtx ImportCtx;
3175 struct ImportCtx {
3176 const char *zFile; /* Name of the input file */
3177 FILE *in; /* Read the CSV text from this input stream */
3178 char *z; /* Accumulated text for a field */
3179 int n; /* Number of bytes in z */
3180 int nAlloc; /* Space allocated for z[] */
3181 int nLine; /* Current line number */
3182 int bNotFirst; /* True if one or more bytes already read */
3183 int cTerm; /* Character that terminated the most recent field */
3184 int cColSep; /* The column separator character. (Usually ",") */
3185 int cRowSep; /* The row separator character. (Usually "\n") */
3188 /* Append a single byte to z[] */
3189 static void import_append_char(ImportCtx *p, int c){
3190 if( p->n+1>=p->nAlloc ){
3191 p->nAlloc += p->nAlloc + 100;
3192 p->z = sqlite3_realloc64(p->z, p->nAlloc);
3193 if( p->z==0 ){
3194 raw_printf(stderr, "out of memory\n");
3195 exit(1);
3198 p->z[p->n++] = (char)c;
3201 /* Read a single field of CSV text. Compatible with rfc4180 and extended
3202 ** with the option of having a separator other than ",".
3204 ** + Input comes from p->in.
3205 ** + Store results in p->z of length p->n. Space to hold p->z comes
3206 ** from sqlite3_malloc64().
3207 ** + Use p->cSep as the column separator. The default is ",".
3208 ** + Use p->rSep as the row separator. The default is "\n".
3209 ** + Keep track of the line number in p->nLine.
3210 ** + Store the character that terminates the field in p->cTerm. Store
3211 ** EOF on end-of-file.
3212 ** + Report syntax errors on stderr
3214 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
3215 int c;
3216 int cSep = p->cColSep;
3217 int rSep = p->cRowSep;
3218 p->n = 0;
3219 c = fgetc(p->in);
3220 if( c==EOF || seenInterrupt ){
3221 p->cTerm = EOF;
3222 return 0;
3224 if( c=='"' ){
3225 int pc, ppc;
3226 int startLine = p->nLine;
3227 int cQuote = c;
3228 pc = ppc = 0;
3229 while( 1 ){
3230 c = fgetc(p->in);
3231 if( c==rSep ) p->nLine++;
3232 if( c==cQuote ){
3233 if( pc==cQuote ){
3234 pc = 0;
3235 continue;
3238 if( (c==cSep && pc==cQuote)
3239 || (c==rSep && pc==cQuote)
3240 || (c==rSep && pc=='\r' && ppc==cQuote)
3241 || (c==EOF && pc==cQuote)
3243 do{ p->n--; }while( p->z[p->n]!=cQuote );
3244 p->cTerm = c;
3245 break;
3247 if( pc==cQuote && c!='\r' ){
3248 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
3249 p->zFile, p->nLine, cQuote);
3251 if( c==EOF ){
3252 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
3253 p->zFile, startLine, cQuote);
3254 p->cTerm = c;
3255 break;
3257 import_append_char(p, c);
3258 ppc = pc;
3259 pc = c;
3261 }else{
3262 /* If this is the first field being parsed and it begins with the
3263 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
3264 if( (c&0xff)==0xef && p->bNotFirst==0 ){
3265 import_append_char(p, c);
3266 c = fgetc(p->in);
3267 if( (c&0xff)==0xbb ){
3268 import_append_char(p, c);
3269 c = fgetc(p->in);
3270 if( (c&0xff)==0xbf ){
3271 p->bNotFirst = 1;
3272 p->n = 0;
3273 return csv_read_one_field(p);
3277 while( c!=EOF && c!=cSep && c!=rSep ){
3278 import_append_char(p, c);
3279 c = fgetc(p->in);
3281 if( c==rSep ){
3282 p->nLine++;
3283 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
3285 p->cTerm = c;
3287 if( p->z ) p->z[p->n] = 0;
3288 p->bNotFirst = 1;
3289 return p->z;
3292 /* Read a single field of ASCII delimited text.
3294 ** + Input comes from p->in.
3295 ** + Store results in p->z of length p->n. Space to hold p->z comes
3296 ** from sqlite3_malloc64().
3297 ** + Use p->cSep as the column separator. The default is "\x1F".
3298 ** + Use p->rSep as the row separator. The default is "\x1E".
3299 ** + Keep track of the row number in p->nLine.
3300 ** + Store the character that terminates the field in p->cTerm. Store
3301 ** EOF on end-of-file.
3302 ** + Report syntax errors on stderr
3304 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
3305 int c;
3306 int cSep = p->cColSep;
3307 int rSep = p->cRowSep;
3308 p->n = 0;
3309 c = fgetc(p->in);
3310 if( c==EOF || seenInterrupt ){
3311 p->cTerm = EOF;
3312 return 0;
3314 while( c!=EOF && c!=cSep && c!=rSep ){
3315 import_append_char(p, c);
3316 c = fgetc(p->in);
3318 if( c==rSep ){
3319 p->nLine++;
3321 p->cTerm = c;
3322 if( p->z ) p->z[p->n] = 0;
3323 return p->z;
3327 ** Try to transfer data for table zTable. If an error is seen while
3328 ** moving forward, try to go backwards. The backwards movement won't
3329 ** work for WITHOUT ROWID tables.
3331 static void tryToCloneData(
3332 ShellState *p,
3333 sqlite3 *newDb,
3334 const char *zTable
3336 sqlite3_stmt *pQuery = 0;
3337 sqlite3_stmt *pInsert = 0;
3338 char *zQuery = 0;
3339 char *zInsert = 0;
3340 int rc;
3341 int i, j, n;
3342 int nTable = (int)strlen(zTable);
3343 int k = 0;
3344 int cnt = 0;
3345 const int spinRate = 10000;
3347 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
3348 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3349 if( rc ){
3350 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3351 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3352 zQuery);
3353 goto end_data_xfer;
3355 n = sqlite3_column_count(pQuery);
3356 zInsert = sqlite3_malloc64(200 + nTable + n*3);
3357 if( zInsert==0 ){
3358 raw_printf(stderr, "out of memory\n");
3359 goto end_data_xfer;
3361 sqlite3_snprintf(200+nTable,zInsert,
3362 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
3363 i = (int)strlen(zInsert);
3364 for(j=1; j<n; j++){
3365 memcpy(zInsert+i, ",?", 2);
3366 i += 2;
3368 memcpy(zInsert+i, ");", 3);
3369 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
3370 if( rc ){
3371 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3372 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
3373 zQuery);
3374 goto end_data_xfer;
3376 for(k=0; k<2; k++){
3377 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3378 for(i=0; i<n; i++){
3379 switch( sqlite3_column_type(pQuery, i) ){
3380 case SQLITE_NULL: {
3381 sqlite3_bind_null(pInsert, i+1);
3382 break;
3384 case SQLITE_INTEGER: {
3385 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
3386 break;
3388 case SQLITE_FLOAT: {
3389 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
3390 break;
3392 case SQLITE_TEXT: {
3393 sqlite3_bind_text(pInsert, i+1,
3394 (const char*)sqlite3_column_text(pQuery,i),
3395 -1, SQLITE_STATIC);
3396 break;
3398 case SQLITE_BLOB: {
3399 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
3400 sqlite3_column_bytes(pQuery,i),
3401 SQLITE_STATIC);
3402 break;
3405 } /* End for */
3406 rc = sqlite3_step(pInsert);
3407 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
3408 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
3409 sqlite3_errmsg(newDb));
3411 sqlite3_reset(pInsert);
3412 cnt++;
3413 if( (cnt%spinRate)==0 ){
3414 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
3415 fflush(stdout);
3417 } /* End while */
3418 if( rc==SQLITE_DONE ) break;
3419 sqlite3_finalize(pQuery);
3420 sqlite3_free(zQuery);
3421 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
3422 zTable);
3423 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3424 if( rc ){
3425 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
3426 break;
3428 } /* End for(k=0...) */
3430 end_data_xfer:
3431 sqlite3_finalize(pQuery);
3432 sqlite3_finalize(pInsert);
3433 sqlite3_free(zQuery);
3434 sqlite3_free(zInsert);
3439 ** Try to transfer all rows of the schema that match zWhere. For
3440 ** each row, invoke xForEach() on the object defined by that row.
3441 ** If an error is encountered while moving forward through the
3442 ** sqlite_master table, try again moving backwards.
3444 static void tryToCloneSchema(
3445 ShellState *p,
3446 sqlite3 *newDb,
3447 const char *zWhere,
3448 void (*xForEach)(ShellState*,sqlite3*,const char*)
3450 sqlite3_stmt *pQuery = 0;
3451 char *zQuery = 0;
3452 int rc;
3453 const unsigned char *zName;
3454 const unsigned char *zSql;
3455 char *zErrMsg = 0;
3457 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3458 " WHERE %s", zWhere);
3459 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3460 if( rc ){
3461 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3462 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3463 zQuery);
3464 goto end_schema_xfer;
3466 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3467 zName = sqlite3_column_text(pQuery, 0);
3468 zSql = sqlite3_column_text(pQuery, 1);
3469 printf("%s... ", zName); fflush(stdout);
3470 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3471 if( zErrMsg ){
3472 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3473 sqlite3_free(zErrMsg);
3474 zErrMsg = 0;
3476 if( xForEach ){
3477 xForEach(p, newDb, (const char*)zName);
3479 printf("done\n");
3481 if( rc!=SQLITE_DONE ){
3482 sqlite3_finalize(pQuery);
3483 sqlite3_free(zQuery);
3484 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3485 " WHERE %s ORDER BY rowid DESC", zWhere);
3486 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3487 if( rc ){
3488 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3489 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3490 zQuery);
3491 goto end_schema_xfer;
3493 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3494 zName = sqlite3_column_text(pQuery, 0);
3495 zSql = sqlite3_column_text(pQuery, 1);
3496 printf("%s... ", zName); fflush(stdout);
3497 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3498 if( zErrMsg ){
3499 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3500 sqlite3_free(zErrMsg);
3501 zErrMsg = 0;
3503 if( xForEach ){
3504 xForEach(p, newDb, (const char*)zName);
3506 printf("done\n");
3509 end_schema_xfer:
3510 sqlite3_finalize(pQuery);
3511 sqlite3_free(zQuery);
3515 ** Open a new database file named "zNewDb". Try to recover as much information
3516 ** as possible out of the main database (which might be corrupt) and write it
3517 ** into zNewDb.
3519 static void tryToClone(ShellState *p, const char *zNewDb){
3520 int rc;
3521 sqlite3 *newDb = 0;
3522 if( access(zNewDb,0)==0 ){
3523 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
3524 return;
3526 rc = sqlite3_open(zNewDb, &newDb);
3527 if( rc ){
3528 utf8_printf(stderr, "Cannot create output database: %s\n",
3529 sqlite3_errmsg(newDb));
3530 }else{
3531 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
3532 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
3533 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
3534 tryToCloneSchema(p, newDb, "type!='table'", 0);
3535 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
3536 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
3538 sqlite3_close(newDb);
3542 ** Change the output file back to stdout
3544 static void output_reset(ShellState *p){
3545 if( p->outfile[0]=='|' ){
3546 #ifndef SQLITE_OMIT_POPEN
3547 pclose(p->out);
3548 #endif
3549 }else{
3550 output_file_close(p->out);
3552 p->outfile[0] = 0;
3553 p->out = stdout;
3557 ** Run an SQL command and return the single integer result.
3559 static int db_int(ShellState *p, const char *zSql){
3560 sqlite3_stmt *pStmt;
3561 int res = 0;
3562 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3563 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
3564 res = sqlite3_column_int(pStmt,0);
3566 sqlite3_finalize(pStmt);
3567 return res;
3571 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
3573 static unsigned int get2byteInt(unsigned char *a){
3574 return (a[0]<<8) + a[1];
3576 static unsigned int get4byteInt(unsigned char *a){
3577 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
3581 ** Implementation of the ".info" command.
3583 ** Return 1 on error, 2 to exit, and 0 otherwise.
3585 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
3586 static const struct { const char *zName; int ofst; } aField[] = {
3587 { "file change counter:", 24 },
3588 { "database page count:", 28 },
3589 { "freelist page count:", 36 },
3590 { "schema cookie:", 40 },
3591 { "schema format:", 44 },
3592 { "default cache size:", 48 },
3593 { "autovacuum top root:", 52 },
3594 { "incremental vacuum:", 64 },
3595 { "text encoding:", 56 },
3596 { "user version:", 60 },
3597 { "application id:", 68 },
3598 { "software version:", 96 },
3600 static const struct { const char *zName; const char *zSql; } aQuery[] = {
3601 { "number of tables:",
3602 "SELECT count(*) FROM %s WHERE type='table'" },
3603 { "number of indexes:",
3604 "SELECT count(*) FROM %s WHERE type='index'" },
3605 { "number of triggers:",
3606 "SELECT count(*) FROM %s WHERE type='trigger'" },
3607 { "number of views:",
3608 "SELECT count(*) FROM %s WHERE type='view'" },
3609 { "schema size:",
3610 "SELECT total(length(sql)) FROM %s" },
3612 sqlite3_file *pFile = 0;
3613 int i;
3614 char *zSchemaTab;
3615 char *zDb = nArg>=2 ? azArg[1] : "main";
3616 unsigned char aHdr[100];
3617 open_db(p, 0);
3618 if( p->db==0 ) return 1;
3619 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
3620 if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
3621 return 1;
3623 i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
3624 if( i!=SQLITE_OK ){
3625 raw_printf(stderr, "unable to read database header\n");
3626 return 1;
3628 i = get2byteInt(aHdr+16);
3629 if( i==1 ) i = 65536;
3630 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
3631 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
3632 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
3633 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
3634 for(i=0; i<ArraySize(aField); i++){
3635 int ofst = aField[i].ofst;
3636 unsigned int val = get4byteInt(aHdr + ofst);
3637 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
3638 switch( ofst ){
3639 case 56: {
3640 if( val==1 ) raw_printf(p->out, " (utf8)");
3641 if( val==2 ) raw_printf(p->out, " (utf16le)");
3642 if( val==3 ) raw_printf(p->out, " (utf16be)");
3645 raw_printf(p->out, "\n");
3647 if( zDb==0 ){
3648 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
3649 }else if( strcmp(zDb,"temp")==0 ){
3650 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
3651 }else{
3652 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
3654 for(i=0; i<ArraySize(aQuery); i++){
3655 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
3656 int val = db_int(p, zSql);
3657 sqlite3_free(zSql);
3658 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
3660 sqlite3_free(zSchemaTab);
3661 return 0;
3665 ** Print the current sqlite3_errmsg() value to stderr and return 1.
3667 static int shellDatabaseError(sqlite3 *db){
3668 const char *zErr = sqlite3_errmsg(db);
3669 utf8_printf(stderr, "Error: %s\n", zErr);
3670 return 1;
3674 ** Print an out-of-memory message to stderr and return 1.
3676 static int shellNomemError(void){
3677 raw_printf(stderr, "Error: out of memory\n");
3678 return 1;
3682 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
3683 ** if they match and FALSE (0) if they do not match.
3685 ** Globbing rules:
3687 ** '*' Matches any sequence of zero or more characters.
3689 ** '?' Matches exactly one character.
3691 ** [...] Matches one character from the enclosed list of
3692 ** characters.
3694 ** [^...] Matches one character not in the enclosed list.
3696 ** '#' Matches any sequence of one or more digits with an
3697 ** optional + or - sign in front
3699 ** ' ' Any span of whitespace matches any other span of
3700 ** whitespace.
3702 ** Extra whitespace at the end of z[] is ignored.
3704 static int testcase_glob(const char *zGlob, const char *z){
3705 int c, c2;
3706 int invert;
3707 int seen;
3709 while( (c = (*(zGlob++)))!=0 ){
3710 if( IsSpace(c) ){
3711 if( !IsSpace(*z) ) return 0;
3712 while( IsSpace(*zGlob) ) zGlob++;
3713 while( IsSpace(*z) ) z++;
3714 }else if( c=='*' ){
3715 while( (c=(*(zGlob++))) == '*' || c=='?' ){
3716 if( c=='?' && (*(z++))==0 ) return 0;
3718 if( c==0 ){
3719 return 1;
3720 }else if( c=='[' ){
3721 while( *z && testcase_glob(zGlob-1,z)==0 ){
3722 z++;
3724 return (*z)!=0;
3726 while( (c2 = (*(z++)))!=0 ){
3727 while( c2!=c ){
3728 c2 = *(z++);
3729 if( c2==0 ) return 0;
3731 if( testcase_glob(zGlob,z) ) return 1;
3733 return 0;
3734 }else if( c=='?' ){
3735 if( (*(z++))==0 ) return 0;
3736 }else if( c=='[' ){
3737 int prior_c = 0;
3738 seen = 0;
3739 invert = 0;
3740 c = *(z++);
3741 if( c==0 ) return 0;
3742 c2 = *(zGlob++);
3743 if( c2=='^' ){
3744 invert = 1;
3745 c2 = *(zGlob++);
3747 if( c2==']' ){
3748 if( c==']' ) seen = 1;
3749 c2 = *(zGlob++);
3751 while( c2 && c2!=']' ){
3752 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
3753 c2 = *(zGlob++);
3754 if( c>=prior_c && c<=c2 ) seen = 1;
3755 prior_c = 0;
3756 }else{
3757 if( c==c2 ){
3758 seen = 1;
3760 prior_c = c2;
3762 c2 = *(zGlob++);
3764 if( c2==0 || (seen ^ invert)==0 ) return 0;
3765 }else if( c=='#' ){
3766 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
3767 if( !IsDigit(z[0]) ) return 0;
3768 z++;
3769 while( IsDigit(z[0]) ){ z++; }
3770 }else{
3771 if( c!=(*(z++)) ) return 0;
3774 while( IsSpace(*z) ){ z++; }
3775 return *z==0;
3780 ** Compare the string as a command-line option with either one or two
3781 ** initial "-" characters.
3783 static int optionMatch(const char *zStr, const char *zOpt){
3784 if( zStr[0]!='-' ) return 0;
3785 zStr++;
3786 if( zStr[0]=='-' ) zStr++;
3787 return strcmp(zStr, zOpt)==0;
3791 ** Delete a file.
3793 int shellDeleteFile(const char *zFilename){
3794 int rc;
3795 #ifdef _WIN32
3796 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
3797 rc = _wunlink(z);
3798 sqlite3_free(z);
3799 #else
3800 rc = unlink(zFilename);
3801 #endif
3802 return rc;
3807 ** The implementation of SQL scalar function fkey_collate_clause(), used
3808 ** by the ".lint fkey-indexes" command. This scalar function is always
3809 ** called with four arguments - the parent table name, the parent column name,
3810 ** the child table name and the child column name.
3812 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
3814 ** If either of the named tables or columns do not exist, this function
3815 ** returns an empty string. An empty string is also returned if both tables
3816 ** and columns exist but have the same default collation sequence. Or,
3817 ** if both exist but the default collation sequences are different, this
3818 ** function returns the string " COLLATE <parent-collation>", where
3819 ** <parent-collation> is the default collation sequence of the parent column.
3821 static void shellFkeyCollateClause(
3822 sqlite3_context *pCtx,
3823 int nVal,
3824 sqlite3_value **apVal
3826 sqlite3 *db = sqlite3_context_db_handle(pCtx);
3827 const char *zParent;
3828 const char *zParentCol;
3829 const char *zParentSeq;
3830 const char *zChild;
3831 const char *zChildCol;
3832 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
3833 int rc;
3835 assert( nVal==4 );
3836 zParent = (const char*)sqlite3_value_text(apVal[0]);
3837 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
3838 zChild = (const char*)sqlite3_value_text(apVal[2]);
3839 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
3841 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
3842 rc = sqlite3_table_column_metadata(
3843 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
3845 if( rc==SQLITE_OK ){
3846 rc = sqlite3_table_column_metadata(
3847 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
3851 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
3852 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
3853 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
3854 sqlite3_free(z);
3860 ** The implementation of dot-command ".lint fkey-indexes".
3862 static int lintFkeyIndexes(
3863 ShellState *pState, /* Current shell tool state */
3864 char **azArg, /* Array of arguments passed to dot command */
3865 int nArg /* Number of entries in azArg[] */
3867 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
3868 FILE *out = pState->out; /* Stream to write non-error output to */
3869 int bVerbose = 0; /* If -verbose is present */
3870 int bGroupByParent = 0; /* If -groupbyparent is present */
3871 int i; /* To iterate through azArg[] */
3872 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
3873 int rc; /* Return code */
3874 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
3877 ** This SELECT statement returns one row for each foreign key constraint
3878 ** in the schema of the main database. The column values are:
3880 ** 0. The text of an SQL statement similar to:
3882 ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?"
3884 ** This is the same SELECT that the foreign keys implementation needs
3885 ** to run internally on child tables. If there is an index that can
3886 ** be used to optimize this query, then it can also be used by the FK
3887 ** implementation to optimize DELETE or UPDATE statements on the parent
3888 ** table.
3890 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
3891 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
3892 ** contains an index that can be used to optimize the query.
3894 ** 2. Human readable text that describes the child table and columns. e.g.
3896 ** "child_table(child_key1, child_key2)"
3898 ** 3. Human readable text that describes the parent table and columns. e.g.
3900 ** "parent_table(parent_key1, parent_key2)"
3902 ** 4. A full CREATE INDEX statement for an index that could be used to
3903 ** optimize DELETE or UPDATE statements on the parent table. e.g.
3905 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
3907 ** 5. The name of the parent table.
3909 ** These six values are used by the C logic below to generate the report.
3911 const char *zSql =
3912 "SELECT "
3913 " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
3914 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
3915 " || fkey_collate_clause("
3916 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
3917 ", "
3918 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
3919 " || group_concat('*=?', ' AND ') || ')'"
3920 ", "
3921 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
3922 ", "
3923 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
3924 ", "
3925 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
3926 " || ' ON ' || quote(s.name) || '('"
3927 " || group_concat(quote(f.[from]) ||"
3928 " fkey_collate_clause("
3929 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
3930 " || ');'"
3931 ", "
3932 " f.[table] "
3933 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
3934 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
3935 "GROUP BY s.name, f.id "
3936 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
3938 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
3940 for(i=2; i<nArg; i++){
3941 int n = (int)strlen(azArg[i]);
3942 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
3943 bVerbose = 1;
3945 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
3946 bGroupByParent = 1;
3947 zIndent = " ";
3949 else{
3950 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
3951 azArg[0], azArg[1]
3953 return SQLITE_ERROR;
3957 /* Register the fkey_collate_clause() SQL function */
3958 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
3959 0, shellFkeyCollateClause, 0, 0
3963 if( rc==SQLITE_OK ){
3964 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
3966 if( rc==SQLITE_OK ){
3967 sqlite3_bind_int(pSql, 1, bGroupByParent);
3970 if( rc==SQLITE_OK ){
3971 int rc2;
3972 char *zPrev = 0;
3973 while( SQLITE_ROW==sqlite3_step(pSql) ){
3974 int res = -1;
3975 sqlite3_stmt *pExplain = 0;
3976 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
3977 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
3978 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
3979 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
3980 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
3981 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
3983 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3984 if( rc!=SQLITE_OK ) break;
3985 if( SQLITE_ROW==sqlite3_step(pExplain) ){
3986 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
3987 res = (
3988 0==sqlite3_strglob(zGlob, zPlan)
3989 || 0==sqlite3_strglob(zGlobIPK, zPlan)
3992 rc = sqlite3_finalize(pExplain);
3993 if( rc!=SQLITE_OK ) break;
3995 if( res<0 ){
3996 raw_printf(stderr, "Error: internal error");
3997 break;
3998 }else{
3999 if( bGroupByParent
4000 && (bVerbose || res==0)
4001 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
4003 raw_printf(out, "-- Parent table %s\n", zParent);
4004 sqlite3_free(zPrev);
4005 zPrev = sqlite3_mprintf("%s", zParent);
4008 if( res==0 ){
4009 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
4010 }else if( bVerbose ){
4011 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
4012 zIndent, zFrom, zTarget
4017 sqlite3_free(zPrev);
4019 if( rc!=SQLITE_OK ){
4020 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4023 rc2 = sqlite3_finalize(pSql);
4024 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
4025 rc = rc2;
4026 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4028 }else{
4029 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4032 return rc;
4036 ** Implementation of ".lint" dot command.
4038 static int lintDotCommand(
4039 ShellState *pState, /* Current shell tool state */
4040 char **azArg, /* Array of arguments passed to dot command */
4041 int nArg /* Number of entries in azArg[] */
4043 int n;
4044 n = (nArg>=2 ? (int)strlen(azArg[1]) : 0);
4045 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
4046 return lintFkeyIndexes(pState, azArg, nArg);
4048 usage:
4049 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
4050 raw_printf(stderr, "Where sub-commands are:\n");
4051 raw_printf(stderr, " fkey-indexes\n");
4052 return SQLITE_ERROR;
4057 ** If an input line begins with "." then invoke this routine to
4058 ** process that line.
4060 ** Return 1 on error, 2 to exit, and 0 otherwise.
4062 static int do_meta_command(char *zLine, ShellState *p){
4063 int h = 1;
4064 int nArg = 0;
4065 int n, c;
4066 int rc = 0;
4067 char *azArg[50];
4069 /* Parse the input line into tokens.
4071 while( zLine[h] && nArg<ArraySize(azArg) ){
4072 while( IsSpace(zLine[h]) ){ h++; }
4073 if( zLine[h]==0 ) break;
4074 if( zLine[h]=='\'' || zLine[h]=='"' ){
4075 int delim = zLine[h++];
4076 azArg[nArg++] = &zLine[h];
4077 while( zLine[h] && zLine[h]!=delim ){
4078 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
4079 h++;
4081 if( zLine[h]==delim ){
4082 zLine[h++] = 0;
4084 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
4085 }else{
4086 azArg[nArg++] = &zLine[h];
4087 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
4088 if( zLine[h] ) zLine[h++] = 0;
4089 resolve_backslashes(azArg[nArg-1]);
4093 /* Process the input line.
4095 if( nArg==0 ) return 0; /* no tokens, no error */
4096 n = strlen30(azArg[0]);
4097 c = azArg[0][0];
4099 #ifndef SQLITE_OMIT_AUTHORIZATION
4100 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
4101 if( nArg!=2 ){
4102 raw_printf(stderr, "Usage: .auth ON|OFF\n");
4103 rc = 1;
4104 goto meta_command_exit;
4106 open_db(p, 0);
4107 if( booleanValue(azArg[1]) ){
4108 sqlite3_set_authorizer(p->db, shellAuth, p);
4109 }else{
4110 sqlite3_set_authorizer(p->db, 0, 0);
4112 }else
4113 #endif
4115 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
4116 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
4118 const char *zDestFile = 0;
4119 const char *zDb = 0;
4120 sqlite3 *pDest;
4121 sqlite3_backup *pBackup;
4122 int j;
4123 for(j=1; j<nArg; j++){
4124 const char *z = azArg[j];
4125 if( z[0]=='-' ){
4126 while( z[0]=='-' ) z++;
4127 /* No options to process at this time */
4129 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
4130 return 1;
4132 }else if( zDestFile==0 ){
4133 zDestFile = azArg[j];
4134 }else if( zDb==0 ){
4135 zDb = zDestFile;
4136 zDestFile = azArg[j];
4137 }else{
4138 raw_printf(stderr, "too many arguments to .backup\n");
4139 return 1;
4142 if( zDestFile==0 ){
4143 raw_printf(stderr, "missing FILENAME argument on .backup\n");
4144 return 1;
4146 if( zDb==0 ) zDb = "main";
4147 rc = sqlite3_open(zDestFile, &pDest);
4148 if( rc!=SQLITE_OK ){
4149 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
4150 sqlite3_close(pDest);
4151 return 1;
4153 open_db(p, 0);
4154 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
4155 if( pBackup==0 ){
4156 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4157 sqlite3_close(pDest);
4158 return 1;
4160 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
4161 sqlite3_backup_finish(pBackup);
4162 if( rc==SQLITE_DONE ){
4163 rc = 0;
4164 }else{
4165 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4166 rc = 1;
4168 sqlite3_close(pDest);
4169 }else
4171 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
4172 if( nArg==2 ){
4173 bail_on_error = booleanValue(azArg[1]);
4174 }else{
4175 raw_printf(stderr, "Usage: .bail on|off\n");
4176 rc = 1;
4178 }else
4180 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
4181 if( nArg==2 ){
4182 if( booleanValue(azArg[1]) ){
4183 setBinaryMode(p->out, 1);
4184 }else{
4185 setTextMode(p->out, 1);
4187 }else{
4188 raw_printf(stderr, "Usage: .binary on|off\n");
4189 rc = 1;
4191 }else
4193 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
4194 if( nArg==2 ){
4195 #if defined(_WIN32) || defined(WIN32)
4196 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
4197 rc = !SetCurrentDirectoryW(z);
4198 sqlite3_free(z);
4199 #else
4200 rc = chdir(azArg[1]);
4201 #endif
4202 if( rc ){
4203 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
4204 rc = 1;
4206 }else{
4207 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
4208 rc = 1;
4210 }else
4212 /* The undocumented ".breakpoint" command causes a call to the no-op
4213 ** routine named test_breakpoint().
4215 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
4216 test_breakpoint();
4217 }else
4219 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
4220 if( nArg==2 ){
4221 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
4222 }else{
4223 raw_printf(stderr, "Usage: .changes on|off\n");
4224 rc = 1;
4226 }else
4228 /* Cancel output redirection, if it is currently set (by .testcase)
4229 ** Then read the content of the testcase-out.txt file and compare against
4230 ** azArg[1]. If there are differences, report an error and exit.
4232 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
4233 char *zRes = 0;
4234 output_reset(p);
4235 if( nArg!=2 ){
4236 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
4237 rc = 2;
4238 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
4239 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
4240 rc = 2;
4241 }else if( testcase_glob(azArg[1],zRes)==0 ){
4242 utf8_printf(stderr,
4243 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
4244 p->zTestcase, azArg[1], zRes);
4245 rc = 2;
4246 }else{
4247 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
4248 p->nCheck++;
4250 sqlite3_free(zRes);
4251 }else
4253 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
4254 if( nArg==2 ){
4255 tryToClone(p, azArg[1]);
4256 }else{
4257 raw_printf(stderr, "Usage: .clone FILENAME\n");
4258 rc = 1;
4260 }else
4262 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
4263 ShellState data;
4264 char *zErrMsg = 0;
4265 open_db(p, 0);
4266 memcpy(&data, p, sizeof(data));
4267 data.showHeader = 0;
4268 data.cMode = data.mode = MODE_List;
4269 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
4270 data.cnt = 0;
4271 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
4272 callback, &data, &zErrMsg);
4273 if( zErrMsg ){
4274 utf8_printf(stderr,"Error: %s\n", zErrMsg);
4275 sqlite3_free(zErrMsg);
4276 rc = 1;
4278 }else
4280 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
4281 rc = shell_dbinfo_command(p, nArg, azArg);
4282 }else
4284 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4285 const char *zLike = 0;
4286 int i;
4287 int savedShowHeader = p->showHeader;
4288 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
4289 for(i=1; i<nArg; i++){
4290 if( azArg[i][0]=='-' ){
4291 const char *z = azArg[i]+1;
4292 if( z[0]=='-' ) z++;
4293 if( strcmp(z,"preserve-rowids")==0 ){
4294 #ifdef SQLITE_OMIT_VIRTUALTABLE
4295 raw_printf(stderr, "The --preserve-rowids option is not compatible"
4296 " with SQLITE_OMIT_VIRTUALTABLE\n");
4297 rc = 1;
4298 goto meta_command_exit;
4299 #else
4300 ShellSetFlag(p, SHFLG_PreserveRowid);
4301 #endif
4302 }else
4303 if( strcmp(z,"newlines")==0 ){
4304 ShellSetFlag(p, SHFLG_Newlines);
4305 }else
4307 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4308 rc = 1;
4309 goto meta_command_exit;
4311 }else if( zLike ){
4312 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
4313 "?--newlines? ?LIKE-PATTERN?\n");
4314 rc = 1;
4315 goto meta_command_exit;
4316 }else{
4317 zLike = azArg[i];
4320 open_db(p, 0);
4321 /* When playing back a "dump", the content might appear in an order
4322 ** which causes immediate foreign key constraints to be violated.
4323 ** So disable foreign-key constraint enforcement to prevent problems. */
4324 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
4325 raw_printf(p->out, "BEGIN TRANSACTION;\n");
4326 p->writableSchema = 0;
4327 p->showHeader = 0;
4328 /* Set writable_schema=ON since doing so forces SQLite to initialize
4329 ** as much of the schema as it can even if the sqlite_master table is
4330 ** corrupt. */
4331 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
4332 p->nErr = 0;
4333 if( zLike==0 ){
4334 run_schema_dump_query(p,
4335 "SELECT name, type, sql FROM sqlite_master "
4336 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
4338 run_schema_dump_query(p,
4339 "SELECT name, type, sql FROM sqlite_master "
4340 "WHERE name=='sqlite_sequence'"
4342 run_table_dump_query(p,
4343 "SELECT sql FROM sqlite_master "
4344 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
4346 }else{
4347 char *zSql;
4348 zSql = sqlite3_mprintf(
4349 "SELECT name, type, sql FROM sqlite_master "
4350 "WHERE tbl_name LIKE %Q AND type=='table'"
4351 " AND sql NOT NULL", zLike);
4352 run_schema_dump_query(p,zSql);
4353 sqlite3_free(zSql);
4354 zSql = sqlite3_mprintf(
4355 "SELECT sql FROM sqlite_master "
4356 "WHERE sql NOT NULL"
4357 " AND type IN ('index','trigger','view')"
4358 " AND tbl_name LIKE %Q", zLike);
4359 run_table_dump_query(p, zSql, 0);
4360 sqlite3_free(zSql);
4362 if( p->writableSchema ){
4363 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
4364 p->writableSchema = 0;
4366 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4367 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
4368 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
4369 p->showHeader = savedShowHeader;
4370 }else
4372 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
4373 if( nArg==2 ){
4374 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
4375 }else{
4376 raw_printf(stderr, "Usage: .echo on|off\n");
4377 rc = 1;
4379 }else
4381 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
4382 if( nArg==2 ){
4383 if( strcmp(azArg[1],"full")==0 ){
4384 p->autoEQP = 2;
4385 }else{
4386 p->autoEQP = booleanValue(azArg[1]);
4388 }else{
4389 raw_printf(stderr, "Usage: .eqp on|off|full\n");
4390 rc = 1;
4392 }else
4394 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
4395 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
4396 rc = 2;
4397 }else
4399 /* The ".explain" command is automatic now. It is largely pointless. It
4400 ** retained purely for backwards compatibility */
4401 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
4402 int val = 1;
4403 if( nArg>=2 ){
4404 if( strcmp(azArg[1],"auto")==0 ){
4405 val = 99;
4406 }else{
4407 val = booleanValue(azArg[1]);
4410 if( val==1 && p->mode!=MODE_Explain ){
4411 p->normalMode = p->mode;
4412 p->mode = MODE_Explain;
4413 p->autoExplain = 0;
4414 }else if( val==0 ){
4415 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4416 p->autoExplain = 0;
4417 }else if( val==99 ){
4418 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4419 p->autoExplain = 1;
4421 }else
4423 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
4424 ShellState data;
4425 char *zErrMsg = 0;
4426 int doStats = 0;
4427 memcpy(&data, p, sizeof(data));
4428 data.showHeader = 0;
4429 data.cMode = data.mode = MODE_Semi;
4430 if( nArg==2 && optionMatch(azArg[1], "indent") ){
4431 data.cMode = data.mode = MODE_Pretty;
4432 nArg = 1;
4434 if( nArg!=1 ){
4435 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
4436 rc = 1;
4437 goto meta_command_exit;
4439 open_db(p, 0);
4440 rc = sqlite3_exec(p->db,
4441 "SELECT sql FROM"
4442 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
4443 " FROM sqlite_master UNION ALL"
4444 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
4445 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
4446 "ORDER BY rowid",
4447 callback, &data, &zErrMsg
4449 if( rc==SQLITE_OK ){
4450 sqlite3_stmt *pStmt;
4451 rc = sqlite3_prepare_v2(p->db,
4452 "SELECT rowid FROM sqlite_master"
4453 " WHERE name GLOB 'sqlite_stat[134]'",
4454 -1, &pStmt, 0);
4455 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
4456 sqlite3_finalize(pStmt);
4458 if( doStats==0 ){
4459 raw_printf(p->out, "/* No STAT tables available */\n");
4460 }else{
4461 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4462 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
4463 callback, &data, &zErrMsg);
4464 data.cMode = data.mode = MODE_Insert;
4465 data.zDestTable = "sqlite_stat1";
4466 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
4467 shell_callback, &data,&zErrMsg);
4468 data.zDestTable = "sqlite_stat3";
4469 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
4470 shell_callback, &data,&zErrMsg);
4471 data.zDestTable = "sqlite_stat4";
4472 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
4473 shell_callback, &data, &zErrMsg);
4474 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4476 }else
4478 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
4479 if( nArg==2 ){
4480 p->showHeader = booleanValue(azArg[1]);
4481 }else{
4482 raw_printf(stderr, "Usage: .headers on|off\n");
4483 rc = 1;
4485 }else
4487 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
4488 utf8_printf(p->out, "%s", zHelp);
4489 }else
4491 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
4492 char *zTable; /* Insert data into this table */
4493 char *zFile; /* Name of file to extra content from */
4494 sqlite3_stmt *pStmt = NULL; /* A statement */
4495 int nCol; /* Number of columns in the table */
4496 int nByte; /* Number of bytes in an SQL string */
4497 int i, j; /* Loop counters */
4498 int needCommit; /* True to COMMIT or ROLLBACK at end */
4499 int nSep; /* Number of bytes in p->colSeparator[] */
4500 char *zSql; /* An SQL statement */
4501 ImportCtx sCtx; /* Reader context */
4502 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
4503 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
4505 if( nArg!=3 ){
4506 raw_printf(stderr, "Usage: .import FILE TABLE\n");
4507 goto meta_command_exit;
4509 zFile = azArg[1];
4510 zTable = azArg[2];
4511 seenInterrupt = 0;
4512 memset(&sCtx, 0, sizeof(sCtx));
4513 open_db(p, 0);
4514 nSep = strlen30(p->colSeparator);
4515 if( nSep==0 ){
4516 raw_printf(stderr,
4517 "Error: non-null column separator required for import\n");
4518 return 1;
4520 if( nSep>1 ){
4521 raw_printf(stderr, "Error: multi-character column separators not allowed"
4522 " for import\n");
4523 return 1;
4525 nSep = strlen30(p->rowSeparator);
4526 if( nSep==0 ){
4527 raw_printf(stderr, "Error: non-null row separator required for import\n");
4528 return 1;
4530 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
4531 /* When importing CSV (only), if the row separator is set to the
4532 ** default output row separator, change it to the default input
4533 ** row separator. This avoids having to maintain different input
4534 ** and output row separators. */
4535 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4536 nSep = strlen30(p->rowSeparator);
4538 if( nSep>1 ){
4539 raw_printf(stderr, "Error: multi-character row separators not allowed"
4540 " for import\n");
4541 return 1;
4543 sCtx.zFile = zFile;
4544 sCtx.nLine = 1;
4545 if( sCtx.zFile[0]=='|' ){
4546 #ifdef SQLITE_OMIT_POPEN
4547 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
4548 return 1;
4549 #else
4550 sCtx.in = popen(sCtx.zFile+1, "r");
4551 sCtx.zFile = "<pipe>";
4552 xCloser = pclose;
4553 #endif
4554 }else{
4555 sCtx.in = fopen(sCtx.zFile, "rb");
4556 xCloser = fclose;
4558 if( p->mode==MODE_Ascii ){
4559 xRead = ascii_read_one_field;
4560 }else{
4561 xRead = csv_read_one_field;
4563 if( sCtx.in==0 ){
4564 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4565 return 1;
4567 sCtx.cColSep = p->colSeparator[0];
4568 sCtx.cRowSep = p->rowSeparator[0];
4569 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
4570 if( zSql==0 ){
4571 raw_printf(stderr, "Error: out of memory\n");
4572 xCloser(sCtx.in);
4573 return 1;
4575 nByte = strlen30(zSql);
4576 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4577 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
4578 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
4579 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
4580 char cSep = '(';
4581 while( xRead(&sCtx) ){
4582 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
4583 cSep = ',';
4584 if( sCtx.cTerm!=sCtx.cColSep ) break;
4586 if( cSep=='(' ){
4587 sqlite3_free(zCreate);
4588 sqlite3_free(sCtx.z);
4589 xCloser(sCtx.in);
4590 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
4591 return 1;
4593 zCreate = sqlite3_mprintf("%z\n)", zCreate);
4594 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
4595 sqlite3_free(zCreate);
4596 if( rc ){
4597 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
4598 sqlite3_errmsg(p->db));
4599 sqlite3_free(sCtx.z);
4600 xCloser(sCtx.in);
4601 return 1;
4603 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4605 sqlite3_free(zSql);
4606 if( rc ){
4607 if (pStmt) sqlite3_finalize(pStmt);
4608 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
4609 xCloser(sCtx.in);
4610 return 1;
4612 nCol = sqlite3_column_count(pStmt);
4613 sqlite3_finalize(pStmt);
4614 pStmt = 0;
4615 if( nCol==0 ) return 0; /* no columns, no error */
4616 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
4617 if( zSql==0 ){
4618 raw_printf(stderr, "Error: out of memory\n");
4619 xCloser(sCtx.in);
4620 return 1;
4622 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
4623 j = strlen30(zSql);
4624 for(i=1; i<nCol; i++){
4625 zSql[j++] = ',';
4626 zSql[j++] = '?';
4628 zSql[j++] = ')';
4629 zSql[j] = 0;
4630 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4631 sqlite3_free(zSql);
4632 if( rc ){
4633 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
4634 if (pStmt) sqlite3_finalize(pStmt);
4635 xCloser(sCtx.in);
4636 return 1;
4638 needCommit = sqlite3_get_autocommit(p->db);
4639 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
4641 int startLine = sCtx.nLine;
4642 for(i=0; i<nCol; i++){
4643 char *z = xRead(&sCtx);
4645 ** Did we reach end-of-file before finding any columns?
4646 ** If so, stop instead of NULL filling the remaining columns.
4648 if( z==0 && i==0 ) break;
4650 ** Did we reach end-of-file OR end-of-line before finding any
4651 ** columns in ASCII mode? If so, stop instead of NULL filling
4652 ** the remaining columns.
4654 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
4655 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
4656 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
4657 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4658 "filling the rest with NULL\n",
4659 sCtx.zFile, startLine, nCol, i+1);
4660 i += 2;
4661 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
4664 if( sCtx.cTerm==sCtx.cColSep ){
4666 xRead(&sCtx);
4667 i++;
4668 }while( sCtx.cTerm==sCtx.cColSep );
4669 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4670 "extras ignored\n",
4671 sCtx.zFile, startLine, nCol, i);
4673 if( i>=nCol ){
4674 sqlite3_step(pStmt);
4675 rc = sqlite3_reset(pStmt);
4676 if( rc!=SQLITE_OK ){
4677 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
4678 startLine, sqlite3_errmsg(p->db));
4681 }while( sCtx.cTerm!=EOF );
4683 xCloser(sCtx.in);
4684 sqlite3_free(sCtx.z);
4685 sqlite3_finalize(pStmt);
4686 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
4687 }else
4689 #ifndef SQLITE_UNTESTABLE
4690 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
4691 char *zSql;
4692 char *zCollist = 0;
4693 sqlite3_stmt *pStmt;
4694 int tnum = 0;
4695 int i;
4696 if( nArg!=3 ){
4697 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
4698 rc = 1;
4699 goto meta_command_exit;
4701 open_db(p, 0);
4702 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
4703 " WHERE name='%q' AND type='index'", azArg[1]);
4704 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4705 sqlite3_free(zSql);
4706 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4707 tnum = sqlite3_column_int(pStmt, 0);
4709 sqlite3_finalize(pStmt);
4710 if( tnum==0 ){
4711 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
4712 rc = 1;
4713 goto meta_command_exit;
4715 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
4716 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4717 sqlite3_free(zSql);
4718 i = 0;
4719 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4720 char zLabel[20];
4721 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
4722 i++;
4723 if( zCol==0 ){
4724 if( sqlite3_column_int(pStmt,1)==-1 ){
4725 zCol = "_ROWID_";
4726 }else{
4727 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
4728 zCol = zLabel;
4731 if( zCollist==0 ){
4732 zCollist = sqlite3_mprintf("\"%w\"", zCol);
4733 }else{
4734 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
4737 sqlite3_finalize(pStmt);
4738 zSql = sqlite3_mprintf(
4739 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
4740 azArg[2], zCollist, zCollist);
4741 sqlite3_free(zCollist);
4742 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
4743 if( rc==SQLITE_OK ){
4744 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
4745 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
4746 if( rc ){
4747 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
4748 }else{
4749 utf8_printf(stdout, "%s;\n", zSql);
4750 raw_printf(stdout,
4751 "WARNING: writing to an imposter table will corrupt the index!\n"
4754 }else{
4755 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
4756 rc = 1;
4758 sqlite3_free(zSql);
4759 }else
4760 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
4762 #ifdef SQLITE_ENABLE_IOTRACE
4763 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
4764 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
4765 if( iotrace && iotrace!=stdout ) fclose(iotrace);
4766 iotrace = 0;
4767 if( nArg<2 ){
4768 sqlite3IoTrace = 0;
4769 }else if( strcmp(azArg[1], "-")==0 ){
4770 sqlite3IoTrace = iotracePrintf;
4771 iotrace = stdout;
4772 }else{
4773 iotrace = fopen(azArg[1], "w");
4774 if( iotrace==0 ){
4775 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
4776 sqlite3IoTrace = 0;
4777 rc = 1;
4778 }else{
4779 sqlite3IoTrace = iotracePrintf;
4782 }else
4783 #endif
4785 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
4786 static const struct {
4787 const char *zLimitName; /* Name of a limit */
4788 int limitCode; /* Integer code for that limit */
4789 } aLimit[] = {
4790 { "length", SQLITE_LIMIT_LENGTH },
4791 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
4792 { "column", SQLITE_LIMIT_COLUMN },
4793 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
4794 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
4795 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
4796 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
4797 { "attached", SQLITE_LIMIT_ATTACHED },
4798 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4799 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
4800 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
4801 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
4803 int i, n2;
4804 open_db(p, 0);
4805 if( nArg==1 ){
4806 for(i=0; i<ArraySize(aLimit); i++){
4807 printf("%20s %d\n", aLimit[i].zLimitName,
4808 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
4810 }else if( nArg>3 ){
4811 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
4812 rc = 1;
4813 goto meta_command_exit;
4814 }else{
4815 int iLimit = -1;
4816 n2 = strlen30(azArg[1]);
4817 for(i=0; i<ArraySize(aLimit); i++){
4818 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
4819 if( iLimit<0 ){
4820 iLimit = i;
4821 }else{
4822 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
4823 rc = 1;
4824 goto meta_command_exit;
4828 if( iLimit<0 ){
4829 utf8_printf(stderr, "unknown limit: \"%s\"\n"
4830 "enter \".limits\" with no arguments for a list.\n",
4831 azArg[1]);
4832 rc = 1;
4833 goto meta_command_exit;
4835 if( nArg==3 ){
4836 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
4837 (int)integerValue(azArg[2]));
4839 printf("%20s %d\n", aLimit[iLimit].zLimitName,
4840 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
4842 }else
4844 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
4845 open_db(p, 0);
4846 lintDotCommand(p, azArg, nArg);
4847 }else
4849 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4850 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
4851 const char *zFile, *zProc;
4852 char *zErrMsg = 0;
4853 if( nArg<2 ){
4854 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
4855 rc = 1;
4856 goto meta_command_exit;
4858 zFile = azArg[1];
4859 zProc = nArg>=3 ? azArg[2] : 0;
4860 open_db(p, 0);
4861 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
4862 if( rc!=SQLITE_OK ){
4863 utf8_printf(stderr, "Error: %s\n", zErrMsg);
4864 sqlite3_free(zErrMsg);
4865 rc = 1;
4867 }else
4868 #endif
4870 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
4871 if( nArg!=2 ){
4872 raw_printf(stderr, "Usage: .log FILENAME\n");
4873 rc = 1;
4874 }else{
4875 const char *zFile = azArg[1];
4876 output_file_close(p->pLog);
4877 p->pLog = output_file_open(zFile);
4879 }else
4881 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
4882 const char *zMode = nArg>=2 ? azArg[1] : "";
4883 int n2 = (int)strlen(zMode);
4884 int c2 = zMode[0];
4885 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
4886 p->mode = MODE_Line;
4887 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4888 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
4889 p->mode = MODE_Column;
4890 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4891 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
4892 p->mode = MODE_List;
4893 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
4894 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4895 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
4896 p->mode = MODE_Html;
4897 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
4898 p->mode = MODE_Tcl;
4899 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
4900 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4901 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
4902 p->mode = MODE_Csv;
4903 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
4904 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
4905 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
4906 p->mode = MODE_List;
4907 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
4908 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
4909 p->mode = MODE_Insert;
4910 set_table_name(p, nArg>=3 ? azArg[2] : "table");
4911 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
4912 p->mode = MODE_Quote;
4913 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
4914 p->mode = MODE_Ascii;
4915 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
4916 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
4917 }else if( nArg==1 ){
4918 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
4919 }else{
4920 raw_printf(stderr, "Error: mode should be one of: "
4921 "ascii column csv html insert line list quote tabs tcl\n");
4922 rc = 1;
4924 p->cMode = p->mode;
4925 }else
4927 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
4928 if( nArg==2 ){
4929 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
4930 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
4931 }else{
4932 raw_printf(stderr, "Usage: .nullvalue STRING\n");
4933 rc = 1;
4935 }else
4937 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
4938 char *zNewFilename; /* Name of the database file to open */
4939 int iName = 1; /* Index in azArg[] of the filename */
4940 int newFlag = 0; /* True to delete file before opening */
4941 /* Close the existing database */
4942 session_close_all(p);
4943 sqlite3_close(p->db);
4944 p->db = 0;
4945 p->zDbFilename = 0;
4946 sqlite3_free(p->zFreeOnClose);
4947 p->zFreeOnClose = 0;
4948 /* Check for command-line arguments */
4949 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
4950 const char *z = azArg[iName];
4951 if( optionMatch(z,"new") ){
4952 newFlag = 1;
4953 }else if( z[0]=='-' ){
4954 utf8_printf(stderr, "unknown option: %s\n", z);
4955 rc = 1;
4956 goto meta_command_exit;
4959 /* If a filename is specified, try to open it first */
4960 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
4961 if( zNewFilename ){
4962 if( newFlag ) shellDeleteFile(zNewFilename);
4963 p->zDbFilename = zNewFilename;
4964 open_db(p, 1);
4965 if( p->db==0 ){
4966 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
4967 sqlite3_free(zNewFilename);
4968 }else{
4969 p->zFreeOnClose = zNewFilename;
4972 if( p->db==0 ){
4973 /* As a fall-back open a TEMP database */
4974 p->zDbFilename = 0;
4975 open_db(p, 0);
4977 }else
4979 if( c=='o'
4980 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
4982 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
4983 if( nArg>2 ){
4984 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
4985 rc = 1;
4986 goto meta_command_exit;
4988 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
4989 if( nArg<2 ){
4990 raw_printf(stderr, "Usage: .once FILE\n");
4991 rc = 1;
4992 goto meta_command_exit;
4994 p->outCount = 2;
4995 }else{
4996 p->outCount = 0;
4998 output_reset(p);
4999 if( zFile[0]=='|' ){
5000 #ifdef SQLITE_OMIT_POPEN
5001 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5002 rc = 1;
5003 p->out = stdout;
5004 #else
5005 p->out = popen(zFile + 1, "w");
5006 if( p->out==0 ){
5007 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
5008 p->out = stdout;
5009 rc = 1;
5010 }else{
5011 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5013 #endif
5014 }else{
5015 p->out = output_file_open(zFile);
5016 if( p->out==0 ){
5017 if( strcmp(zFile,"off")!=0 ){
5018 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
5020 p->out = stdout;
5021 rc = 1;
5022 } else {
5023 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5026 }else
5028 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
5029 int i;
5030 for(i=1; i<nArg; i++){
5031 if( i>1 ) raw_printf(p->out, " ");
5032 utf8_printf(p->out, "%s", azArg[i]);
5034 raw_printf(p->out, "\n");
5035 }else
5037 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
5038 if( nArg >= 2) {
5039 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
5041 if( nArg >= 3) {
5042 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
5044 }else
5046 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
5047 rc = 2;
5048 }else
5050 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
5051 FILE *alt;
5052 if( nArg!=2 ){
5053 raw_printf(stderr, "Usage: .read FILE\n");
5054 rc = 1;
5055 goto meta_command_exit;
5057 alt = fopen(azArg[1], "rb");
5058 if( alt==0 ){
5059 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
5060 rc = 1;
5061 }else{
5062 rc = process_input(p, alt);
5063 fclose(alt);
5065 }else
5067 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
5068 const char *zSrcFile;
5069 const char *zDb;
5070 sqlite3 *pSrc;
5071 sqlite3_backup *pBackup;
5072 int nTimeout = 0;
5074 if( nArg==2 ){
5075 zSrcFile = azArg[1];
5076 zDb = "main";
5077 }else if( nArg==3 ){
5078 zSrcFile = azArg[2];
5079 zDb = azArg[1];
5080 }else{
5081 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
5082 rc = 1;
5083 goto meta_command_exit;
5085 rc = sqlite3_open(zSrcFile, &pSrc);
5086 if( rc!=SQLITE_OK ){
5087 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
5088 sqlite3_close(pSrc);
5089 return 1;
5091 open_db(p, 0);
5092 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
5093 if( pBackup==0 ){
5094 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5095 sqlite3_close(pSrc);
5096 return 1;
5098 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
5099 || rc==SQLITE_BUSY ){
5100 if( rc==SQLITE_BUSY ){
5101 if( nTimeout++ >= 3 ) break;
5102 sqlite3_sleep(100);
5105 sqlite3_backup_finish(pBackup);
5106 if( rc==SQLITE_DONE ){
5107 rc = 0;
5108 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
5109 raw_printf(stderr, "Error: source database is busy\n");
5110 rc = 1;
5111 }else{
5112 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5113 rc = 1;
5115 sqlite3_close(pSrc);
5116 }else
5119 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
5120 if( nArg==2 ){
5121 p->scanstatsOn = booleanValue(azArg[1]);
5122 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
5123 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
5124 #endif
5125 }else{
5126 raw_printf(stderr, "Usage: .scanstats on|off\n");
5127 rc = 1;
5129 }else
5131 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
5132 ShellText sSelect;
5133 ShellState data;
5134 char *zErrMsg = 0;
5135 const char *zDiv = 0;
5136 int iSchema = 0;
5138 open_db(p, 0);
5139 memcpy(&data, p, sizeof(data));
5140 data.showHeader = 0;
5141 data.cMode = data.mode = MODE_Semi;
5142 initText(&sSelect);
5143 if( nArg>=2 && optionMatch(azArg[1], "indent") ){
5144 data.cMode = data.mode = MODE_Pretty;
5145 nArg--;
5146 if( nArg==2 ) azArg[1] = azArg[2];
5148 if( nArg==2 && azArg[1][0]!='-' ){
5149 int i;
5150 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
5151 if( strcmp(azArg[1],"sqlite_master")==0 ){
5152 char *new_argv[2], *new_colv[2];
5153 new_argv[0] = "CREATE TABLE sqlite_master (\n"
5154 " type text,\n"
5155 " name text,\n"
5156 " tbl_name text,\n"
5157 " rootpage integer,\n"
5158 " sql text\n"
5159 ")";
5160 new_argv[1] = 0;
5161 new_colv[0] = "sql";
5162 new_colv[1] = 0;
5163 callback(&data, 1, new_argv, new_colv);
5164 rc = SQLITE_OK;
5165 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
5166 char *new_argv[2], *new_colv[2];
5167 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
5168 " type text,\n"
5169 " name text,\n"
5170 " tbl_name text,\n"
5171 " rootpage integer,\n"
5172 " sql text\n"
5173 ")";
5174 new_argv[1] = 0;
5175 new_colv[0] = "sql";
5176 new_colv[1] = 0;
5177 callback(&data, 1, new_argv, new_colv);
5178 rc = SQLITE_OK;
5179 }else{
5180 zDiv = "(";
5182 }else if( nArg==1 ){
5183 zDiv = "(";
5184 }else{
5185 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
5186 rc = 1;
5187 goto meta_command_exit;
5189 if( zDiv ){
5190 sqlite3_stmt *pStmt = 0;
5191 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
5192 -1, &pStmt, 0);
5193 if( rc ){
5194 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5195 sqlite3_finalize(pStmt);
5196 rc = 1;
5197 goto meta_command_exit;
5199 appendText(&sSelect, "SELECT sql FROM", 0);
5200 iSchema = 0;
5201 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5202 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
5203 char zScNum[30];
5204 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
5205 appendText(&sSelect, zDiv, 0);
5206 zDiv = " UNION ALL ";
5207 if( strcmp(zDb, "main")!=0 ){
5208 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
5209 appendText(&sSelect, zDb, '"');
5210 appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0);
5211 appendText(&sSelect, zScNum, 0);
5212 appendText(&sSelect, " AS snum, ", 0);
5213 appendText(&sSelect, zDb, '\'');
5214 appendText(&sSelect, " AS sname FROM ", 0);
5215 appendText(&sSelect, zDb, '"');
5216 appendText(&sSelect, ".sqlite_master", 0);
5217 }else{
5218 appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0);
5219 appendText(&sSelect, zScNum, 0);
5220 appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0);
5223 sqlite3_finalize(pStmt);
5224 appendText(&sSelect, ") WHERE ", 0);
5225 if( nArg>1 ){
5226 char *zQarg = sqlite3_mprintf("%Q", azArg[1]);
5227 if( strchr(azArg[1], '.') ){
5228 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
5229 }else{
5230 appendText(&sSelect, "lower(tbl_name)", 0);
5232 appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0);
5233 appendText(&sSelect, zQarg, 0);
5234 appendText(&sSelect, " AND ", 0);
5235 sqlite3_free(zQarg);
5237 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
5238 " ORDER BY snum, rowid", 0);
5239 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
5240 freeText(&sSelect);
5242 if( zErrMsg ){
5243 utf8_printf(stderr,"Error: %s\n", zErrMsg);
5244 sqlite3_free(zErrMsg);
5245 rc = 1;
5246 }else if( rc != SQLITE_OK ){
5247 raw_printf(stderr,"Error: querying schema information\n");
5248 rc = 1;
5249 }else{
5250 rc = 0;
5252 }else
5254 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
5255 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
5256 sqlite3SelectTrace = (int)integerValue(azArg[1]);
5257 }else
5258 #endif
5260 #if defined(SQLITE_ENABLE_SESSION)
5261 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
5262 OpenSession *pSession = &p->aSession[0];
5263 char **azCmd = &azArg[1];
5264 int iSes = 0;
5265 int nCmd = nArg - 1;
5266 int i;
5267 if( nArg<=1 ) goto session_syntax_error;
5268 open_db(p, 0);
5269 if( nArg>=3 ){
5270 for(iSes=0; iSes<p->nSession; iSes++){
5271 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
5273 if( iSes<p->nSession ){
5274 pSession = &p->aSession[iSes];
5275 azCmd++;
5276 nCmd--;
5277 }else{
5278 pSession = &p->aSession[0];
5279 iSes = 0;
5283 /* .session attach TABLE
5284 ** Invoke the sqlite3session_attach() interface to attach a particular
5285 ** table so that it is never filtered.
5287 if( strcmp(azCmd[0],"attach")==0 ){
5288 if( nCmd!=2 ) goto session_syntax_error;
5289 if( pSession->p==0 ){
5290 session_not_open:
5291 raw_printf(stderr, "ERROR: No sessions are open\n");
5292 }else{
5293 rc = sqlite3session_attach(pSession->p, azCmd[1]);
5294 if( rc ){
5295 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
5296 rc = 0;
5299 }else
5301 /* .session changeset FILE
5302 ** .session patchset FILE
5303 ** Write a changeset or patchset into a file. The file is overwritten.
5305 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
5306 FILE *out = 0;
5307 if( nCmd!=2 ) goto session_syntax_error;
5308 if( pSession->p==0 ) goto session_not_open;
5309 out = fopen(azCmd[1], "wb");
5310 if( out==0 ){
5311 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
5312 }else{
5313 int szChng;
5314 void *pChng;
5315 if( azCmd[0][0]=='c' ){
5316 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
5317 }else{
5318 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
5320 if( rc ){
5321 printf("Error: error code %d\n", rc);
5322 rc = 0;
5324 if( pChng
5325 && fwrite(pChng, szChng, 1, out)!=1 ){
5326 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
5327 szChng);
5329 sqlite3_free(pChng);
5330 fclose(out);
5332 }else
5334 /* .session close
5335 ** Close the identified session
5337 if( strcmp(azCmd[0], "close")==0 ){
5338 if( nCmd!=1 ) goto session_syntax_error;
5339 if( p->nSession ){
5340 session_close(pSession);
5341 p->aSession[iSes] = p->aSession[--p->nSession];
5343 }else
5345 /* .session enable ?BOOLEAN?
5346 ** Query or set the enable flag
5348 if( strcmp(azCmd[0], "enable")==0 ){
5349 int ii;
5350 if( nCmd>2 ) goto session_syntax_error;
5351 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5352 if( p->nSession ){
5353 ii = sqlite3session_enable(pSession->p, ii);
5354 utf8_printf(p->out, "session %s enable flag = %d\n",
5355 pSession->zName, ii);
5357 }else
5359 /* .session filter GLOB ....
5360 ** Set a list of GLOB patterns of table names to be excluded.
5362 if( strcmp(azCmd[0], "filter")==0 ){
5363 int ii, nByte;
5364 if( nCmd<2 ) goto session_syntax_error;
5365 if( p->nSession ){
5366 for(ii=0; ii<pSession->nFilter; ii++){
5367 sqlite3_free(pSession->azFilter[ii]);
5369 sqlite3_free(pSession->azFilter);
5370 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
5371 pSession->azFilter = sqlite3_malloc( nByte );
5372 if( pSession->azFilter==0 ){
5373 raw_printf(stderr, "Error: out or memory\n");
5374 exit(1);
5376 for(ii=1; ii<nCmd; ii++){
5377 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
5379 pSession->nFilter = ii-1;
5381 }else
5383 /* .session indirect ?BOOLEAN?
5384 ** Query or set the indirect flag
5386 if( strcmp(azCmd[0], "indirect")==0 ){
5387 int ii;
5388 if( nCmd>2 ) goto session_syntax_error;
5389 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5390 if( p->nSession ){
5391 ii = sqlite3session_indirect(pSession->p, ii);
5392 utf8_printf(p->out, "session %s indirect flag = %d\n",
5393 pSession->zName, ii);
5395 }else
5397 /* .session isempty
5398 ** Determine if the session is empty
5400 if( strcmp(azCmd[0], "isempty")==0 ){
5401 int ii;
5402 if( nCmd!=1 ) goto session_syntax_error;
5403 if( p->nSession ){
5404 ii = sqlite3session_isempty(pSession->p);
5405 utf8_printf(p->out, "session %s isempty flag = %d\n",
5406 pSession->zName, ii);
5408 }else
5410 /* .session list
5411 ** List all currently open sessions
5413 if( strcmp(azCmd[0],"list")==0 ){
5414 for(i=0; i<p->nSession; i++){
5415 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
5417 }else
5419 /* .session open DB NAME
5420 ** Open a new session called NAME on the attached database DB.
5421 ** DB is normally "main".
5423 if( strcmp(azCmd[0],"open")==0 ){
5424 char *zName;
5425 if( nCmd!=3 ) goto session_syntax_error;
5426 zName = azCmd[2];
5427 if( zName[0]==0 ) goto session_syntax_error;
5428 for(i=0; i<p->nSession; i++){
5429 if( strcmp(p->aSession[i].zName,zName)==0 ){
5430 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
5431 goto meta_command_exit;
5434 if( p->nSession>=ArraySize(p->aSession) ){
5435 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
5436 goto meta_command_exit;
5438 pSession = &p->aSession[p->nSession];
5439 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
5440 if( rc ){
5441 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
5442 rc = 0;
5443 goto meta_command_exit;
5445 pSession->nFilter = 0;
5446 sqlite3session_table_filter(pSession->p, session_filter, pSession);
5447 p->nSession++;
5448 pSession->zName = sqlite3_mprintf("%s", zName);
5449 }else
5450 /* If no command name matches, show a syntax error */
5451 session_syntax_error:
5452 session_help(p);
5453 }else
5454 #endif
5456 #ifdef SQLITE_DEBUG
5457 /* Undocumented commands for internal testing. Subject to change
5458 ** without notice. */
5459 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
5460 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
5461 int i, v;
5462 for(i=1; i<nArg; i++){
5463 v = booleanValue(azArg[i]);
5464 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
5467 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
5468 int i; sqlite3_int64 v;
5469 for(i=1; i<nArg; i++){
5470 char zBuf[200];
5471 v = integerValue(azArg[i]);
5472 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
5473 utf8_printf(p->out, "%s", zBuf);
5476 }else
5477 #endif
5479 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
5480 int bIsInit = 0; /* True to initialize the SELFTEST table */
5481 int bVerbose = 0; /* Verbose output */
5482 int bSelftestExists; /* True if SELFTEST already exists */
5483 int i, k; /* Loop counters */
5484 int nTest = 0; /* Number of tests runs */
5485 int nErr = 0; /* Number of errors seen */
5486 ShellText str; /* Answer for a query */
5487 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
5489 open_db(p,0);
5490 for(i=1; i<nArg; i++){
5491 const char *z = azArg[i];
5492 if( z[0]=='-' && z[1]=='-' ) z++;
5493 if( strcmp(z,"-init")==0 ){
5494 bIsInit = 1;
5495 }else
5496 if( strcmp(z,"-v")==0 ){
5497 bVerbose++;
5498 }else
5500 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5501 azArg[i], azArg[0]);
5502 raw_printf(stderr, "Should be one of: --init -v\n");
5503 rc = 1;
5504 goto meta_command_exit;
5507 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
5508 != SQLITE_OK ){
5509 bSelftestExists = 0;
5510 }else{
5511 bSelftestExists = 1;
5513 if( bIsInit ){
5514 createSelftestTable(p);
5515 bSelftestExists = 1;
5517 initText(&str);
5518 appendText(&str, "x", 0);
5519 for(k=bSelftestExists; k>=0; k--){
5520 if( k==1 ){
5521 rc = sqlite3_prepare_v2(p->db,
5522 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
5523 -1, &pStmt, 0);
5524 }else{
5525 rc = sqlite3_prepare_v2(p->db,
5526 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
5527 " (1,'run','PRAGMA integrity_check','ok')",
5528 -1, &pStmt, 0);
5530 if( rc ){
5531 raw_printf(stderr, "Error querying the selftest table\n");
5532 rc = 1;
5533 sqlite3_finalize(pStmt);
5534 goto meta_command_exit;
5536 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
5537 int tno = sqlite3_column_int(pStmt, 0);
5538 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
5539 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
5540 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
5542 k = 0;
5543 if( bVerbose>0 ){
5544 char *zQuote = sqlite3_mprintf("%q", zSql);
5545 printf("%d: %s %s\n", tno, zOp, zSql);
5546 sqlite3_free(zQuote);
5548 if( strcmp(zOp,"memo")==0 ){
5549 utf8_printf(p->out, "%s\n", zSql);
5550 }else
5551 if( strcmp(zOp,"run")==0 ){
5552 char *zErrMsg = 0;
5553 str.n = 0;
5554 str.z[0] = 0;
5555 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
5556 nTest++;
5557 if( bVerbose ){
5558 utf8_printf(p->out, "Result: %s\n", str.z);
5560 if( rc || zErrMsg ){
5561 nErr++;
5562 rc = 1;
5563 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
5564 sqlite3_free(zErrMsg);
5565 }else if( strcmp(zAns,str.z)!=0 ){
5566 nErr++;
5567 rc = 1;
5568 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
5569 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
5571 }else
5573 utf8_printf(stderr,
5574 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
5575 rc = 1;
5576 break;
5578 } /* End loop over rows of content from SELFTEST */
5579 sqlite3_finalize(pStmt);
5580 } /* End loop over k */
5581 freeText(&str);
5582 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
5583 }else
5585 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
5586 if( nArg<2 || nArg>3 ){
5587 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
5588 rc = 1;
5590 if( nArg>=2 ){
5591 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
5592 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
5594 if( nArg>=3 ){
5595 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
5596 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
5598 }else
5600 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
5601 const char *zLike = 0; /* Which table to checksum. 0 means everything */
5602 int i; /* Loop counter */
5603 int bSchema = 0; /* Also hash the schema */
5604 int bSeparate = 0; /* Hash each table separately */
5605 int iSize = 224; /* Hash algorithm to use */
5606 int bDebug = 0; /* Only show the query that would have run */
5607 sqlite3_stmt *pStmt; /* For querying tables names */
5608 char *zSql; /* SQL to be run */
5609 char *zSep; /* Separator */
5610 ShellText sSql; /* Complete SQL for the query to run the hash */
5611 ShellText sQuery; /* Set of queries used to read all content */
5612 open_db(p, 0);
5613 for(i=1; i<nArg; i++){
5614 const char *z = azArg[i];
5615 if( z[0]=='-' ){
5616 z++;
5617 if( z[0]=='-' ) z++;
5618 if( strcmp(z,"schema")==0 ){
5619 bSchema = 1;
5620 }else
5621 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
5622 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
5624 iSize = atoi(&z[5]);
5625 }else
5626 if( strcmp(z,"debug")==0 ){
5627 bDebug = 1;
5628 }else
5630 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5631 azArg[i], azArg[0]);
5632 raw_printf(stderr, "Should be one of: --schema"
5633 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
5634 rc = 1;
5635 goto meta_command_exit;
5637 }else if( zLike ){
5638 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
5639 rc = 1;
5640 goto meta_command_exit;
5641 }else{
5642 zLike = z;
5643 bSeparate = 1;
5644 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
5647 if( bSchema ){
5648 zSql = "SELECT lower(name) FROM sqlite_master"
5649 " WHERE type='table' AND coalesce(rootpage,0)>1"
5650 " UNION ALL SELECT 'sqlite_master'"
5651 " ORDER BY 1 collate nocase";
5652 }else{
5653 zSql = "SELECT lower(name) FROM sqlite_master"
5654 " WHERE type='table' AND coalesce(rootpage,0)>1"
5655 " AND name NOT LIKE 'sqlite_%'"
5656 " ORDER BY 1 collate nocase";
5658 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5659 initText(&sQuery);
5660 initText(&sSql);
5661 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
5662 zSep = "VALUES(";
5663 while( SQLITE_ROW==sqlite3_step(pStmt) ){
5664 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
5665 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
5666 if( strncmp(zTab, "sqlite_",7)!=0 ){
5667 appendText(&sQuery,"SELECT * FROM ", 0);
5668 appendText(&sQuery,zTab,'"');
5669 appendText(&sQuery," NOT INDEXED;", 0);
5670 }else if( strcmp(zTab, "sqlite_master")==0 ){
5671 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
5672 " ORDER BY name;", 0);
5673 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
5674 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
5675 " ORDER BY name;", 0);
5676 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
5677 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
5678 " ORDER BY tbl,idx;", 0);
5679 }else if( strcmp(zTab, "sqlite_stat3")==0
5680 || strcmp(zTab, "sqlite_stat4")==0 ){
5681 appendText(&sQuery, "SELECT * FROM ", 0);
5682 appendText(&sQuery, zTab, 0);
5683 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
5685 appendText(&sSql, zSep, 0);
5686 appendText(&sSql, sQuery.z, '\'');
5687 sQuery.n = 0;
5688 appendText(&sSql, ",", 0);
5689 appendText(&sSql, zTab, '\'');
5690 zSep = "),(";
5692 sqlite3_finalize(pStmt);
5693 if( bSeparate ){
5694 zSql = sqlite3_mprintf(
5695 "%s))"
5696 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
5697 " FROM [sha3sum$query]",
5698 sSql.z, iSize);
5699 }else{
5700 zSql = sqlite3_mprintf(
5701 "%s))"
5702 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
5703 " FROM [sha3sum$query]",
5704 sSql.z, iSize);
5706 freeText(&sQuery);
5707 freeText(&sSql);
5708 if( bDebug ){
5709 utf8_printf(p->out, "%s\n", zSql);
5710 }else{
5711 shell_exec(p->db, zSql, shell_callback, p, 0);
5713 sqlite3_free(zSql);
5714 }else
5716 if( c=='s'
5717 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
5719 char *zCmd;
5720 int i, x;
5721 if( nArg<2 ){
5722 raw_printf(stderr, "Usage: .system COMMAND\n");
5723 rc = 1;
5724 goto meta_command_exit;
5726 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
5727 for(i=2; i<nArg; i++){
5728 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
5729 zCmd, azArg[i]);
5731 x = system(zCmd);
5732 sqlite3_free(zCmd);
5733 if( x ) raw_printf(stderr, "System command returns %d\n", x);
5734 }else
5736 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
5737 static const char *azBool[] = { "off", "on", "full", "unk" };
5738 int i;
5739 if( nArg!=1 ){
5740 raw_printf(stderr, "Usage: .show\n");
5741 rc = 1;
5742 goto meta_command_exit;
5744 utf8_printf(p->out, "%12.12s: %s\n","echo",
5745 azBool[ShellHasFlag(p, SHFLG_Echo)]);
5746 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
5747 utf8_printf(p->out, "%12.12s: %s\n","explain",
5748 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
5749 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
5750 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
5751 utf8_printf(p->out, "%12.12s: ", "nullvalue");
5752 output_c_string(p->out, p->nullValue);
5753 raw_printf(p->out, "\n");
5754 utf8_printf(p->out,"%12.12s: %s\n","output",
5755 strlen30(p->outfile) ? p->outfile : "stdout");
5756 utf8_printf(p->out,"%12.12s: ", "colseparator");
5757 output_c_string(p->out, p->colSeparator);
5758 raw_printf(p->out, "\n");
5759 utf8_printf(p->out,"%12.12s: ", "rowseparator");
5760 output_c_string(p->out, p->rowSeparator);
5761 raw_printf(p->out, "\n");
5762 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
5763 utf8_printf(p->out, "%12.12s: ", "width");
5764 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
5765 raw_printf(p->out, "%d ", p->colWidth[i]);
5767 raw_printf(p->out, "\n");
5768 utf8_printf(p->out, "%12.12s: %s\n", "filename",
5769 p->zDbFilename ? p->zDbFilename : "");
5770 }else
5772 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
5773 if( nArg==2 ){
5774 p->statsOn = booleanValue(azArg[1]);
5775 }else if( nArg==1 ){
5776 display_stats(p->db, p, 0);
5777 }else{
5778 raw_printf(stderr, "Usage: .stats ?on|off?\n");
5779 rc = 1;
5781 }else
5783 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
5784 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
5785 || strncmp(azArg[0], "indexes", n)==0) )
5787 sqlite3_stmt *pStmt;
5788 char **azResult;
5789 int nRow, nAlloc;
5790 int ii;
5791 ShellText s;
5792 initText(&s);
5793 open_db(p, 0);
5794 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
5795 if( rc ) return shellDatabaseError(p->db);
5797 if( nArg>2 && c=='i' ){
5798 /* It is an historical accident that the .indexes command shows an error
5799 ** when called with the wrong number of arguments whereas the .tables
5800 ** command does not. */
5801 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
5802 rc = 1;
5803 goto meta_command_exit;
5805 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
5806 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
5807 if( zDbName==0 ) continue;
5808 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
5809 if( sqlite3_stricmp(zDbName, "main")==0 ){
5810 appendText(&s, "SELECT name FROM ", 0);
5811 }else{
5812 appendText(&s, "SELECT ", 0);
5813 appendText(&s, zDbName, '\'');
5814 appendText(&s, "||'.'||name FROM ", 0);
5816 appendText(&s, zDbName, '"');
5817 appendText(&s, ".sqlite_master ", 0);
5818 if( c=='t' ){
5819 appendText(&s," WHERE type IN ('table','view')"
5820 " AND name NOT LIKE 'sqlite_%'"
5821 " AND name LIKE ?1", 0);
5822 }else{
5823 appendText(&s," WHERE type='index'"
5824 " AND tbl_name LIKE ?1", 0);
5827 rc = sqlite3_finalize(pStmt);
5828 appendText(&s, " ORDER BY 1", 0);
5829 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
5830 freeText(&s);
5831 if( rc ) return shellDatabaseError(p->db);
5833 /* Run the SQL statement prepared by the above block. Store the results
5834 ** as an array of nul-terminated strings in azResult[]. */
5835 nRow = nAlloc = 0;
5836 azResult = 0;
5837 if( nArg>1 ){
5838 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
5839 }else{
5840 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
5842 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5843 if( nRow>=nAlloc ){
5844 char **azNew;
5845 int n2 = nAlloc*2 + 10;
5846 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
5847 if( azNew==0 ){
5848 rc = shellNomemError();
5849 break;
5851 nAlloc = n2;
5852 azResult = azNew;
5854 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
5855 if( 0==azResult[nRow] ){
5856 rc = shellNomemError();
5857 break;
5859 nRow++;
5861 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
5862 rc = shellDatabaseError(p->db);
5865 /* Pretty-print the contents of array azResult[] to the output */
5866 if( rc==0 && nRow>0 ){
5867 int len, maxlen = 0;
5868 int i, j;
5869 int nPrintCol, nPrintRow;
5870 for(i=0; i<nRow; i++){
5871 len = strlen30(azResult[i]);
5872 if( len>maxlen ) maxlen = len;
5874 nPrintCol = 80/(maxlen+2);
5875 if( nPrintCol<1 ) nPrintCol = 1;
5876 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
5877 for(i=0; i<nPrintRow; i++){
5878 for(j=i; j<nRow; j+=nPrintRow){
5879 char *zSp = j<nPrintRow ? "" : " ";
5880 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
5881 azResult[j] ? azResult[j]:"");
5883 raw_printf(p->out, "\n");
5887 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
5888 sqlite3_free(azResult);
5889 }else
5891 /* Begin redirecting output to the file "testcase-out.txt" */
5892 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
5893 output_reset(p);
5894 p->out = output_file_open("testcase-out.txt");
5895 if( p->out==0 ){
5896 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
5898 if( nArg>=2 ){
5899 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
5900 }else{
5901 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
5903 }else
5905 #ifndef SQLITE_UNTESTABLE
5906 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
5907 static const struct {
5908 const char *zCtrlName; /* Name of a test-control option */
5909 int ctrlCode; /* Integer code for that option */
5910 } aCtrl[] = {
5911 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
5912 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
5913 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
5914 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
5915 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
5916 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
5917 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
5918 { "assert", SQLITE_TESTCTRL_ASSERT },
5919 { "always", SQLITE_TESTCTRL_ALWAYS },
5920 { "reserve", SQLITE_TESTCTRL_RESERVE },
5921 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
5922 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
5923 { "byteorder", SQLITE_TESTCTRL_BYTEORDER },
5924 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT },
5925 { "imposter", SQLITE_TESTCTRL_IMPOSTER },
5927 int testctrl = -1;
5928 int rc2 = 0;
5929 int i, n2;
5930 open_db(p, 0);
5932 /* convert testctrl text option to value. allow any unique prefix
5933 ** of the option name, or a numerical value. */
5934 n2 = strlen30(azArg[1]);
5935 for(i=0; i<ArraySize(aCtrl); i++){
5936 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
5937 if( testctrl<0 ){
5938 testctrl = aCtrl[i].ctrlCode;
5939 }else{
5940 utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
5941 testctrl = -1;
5942 break;
5946 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
5947 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
5948 utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
5949 }else{
5950 switch(testctrl){
5952 /* sqlite3_test_control(int, db, int) */
5953 case SQLITE_TESTCTRL_OPTIMIZATIONS:
5954 case SQLITE_TESTCTRL_RESERVE:
5955 if( nArg==3 ){
5956 int opt = (int)strtol(azArg[2], 0, 0);
5957 rc2 = sqlite3_test_control(testctrl, p->db, opt);
5958 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5959 } else {
5960 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
5961 azArg[1]);
5963 break;
5965 /* sqlite3_test_control(int) */
5966 case SQLITE_TESTCTRL_PRNG_SAVE:
5967 case SQLITE_TESTCTRL_PRNG_RESTORE:
5968 case SQLITE_TESTCTRL_PRNG_RESET:
5969 case SQLITE_TESTCTRL_BYTEORDER:
5970 if( nArg==2 ){
5971 rc2 = sqlite3_test_control(testctrl);
5972 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5973 } else {
5974 utf8_printf(stderr,"Error: testctrl %s takes no options\n",
5975 azArg[1]);
5977 break;
5979 /* sqlite3_test_control(int, uint) */
5980 case SQLITE_TESTCTRL_PENDING_BYTE:
5981 if( nArg==3 ){
5982 unsigned int opt = (unsigned int)integerValue(azArg[2]);
5983 rc2 = sqlite3_test_control(testctrl, opt);
5984 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5985 } else {
5986 utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
5987 " int option\n", azArg[1]);
5989 break;
5991 /* sqlite3_test_control(int, int) */
5992 case SQLITE_TESTCTRL_ASSERT:
5993 case SQLITE_TESTCTRL_ALWAYS:
5994 case SQLITE_TESTCTRL_NEVER_CORRUPT:
5995 if( nArg==3 ){
5996 int opt = booleanValue(azArg[2]);
5997 rc2 = sqlite3_test_control(testctrl, opt);
5998 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5999 } else {
6000 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
6001 azArg[1]);
6003 break;
6005 /* sqlite3_test_control(int, char *) */
6006 #ifdef SQLITE_N_KEYWORD
6007 case SQLITE_TESTCTRL_ISKEYWORD:
6008 if( nArg==3 ){
6009 const char *opt = azArg[2];
6010 rc2 = sqlite3_test_control(testctrl, opt);
6011 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6012 } else {
6013 utf8_printf(stderr,
6014 "Error: testctrl %s takes a single char * option\n",
6015 azArg[1]);
6017 break;
6018 #endif
6020 case SQLITE_TESTCTRL_IMPOSTER:
6021 if( nArg==5 ){
6022 rc2 = sqlite3_test_control(testctrl, p->db,
6023 azArg[2],
6024 integerValue(azArg[3]),
6025 integerValue(azArg[4]));
6026 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6027 }else{
6028 raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
6030 break;
6032 case SQLITE_TESTCTRL_BITVEC_TEST:
6033 case SQLITE_TESTCTRL_FAULT_INSTALL:
6034 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
6035 default:
6036 utf8_printf(stderr,
6037 "Error: CLI support for testctrl %s not implemented\n",
6038 azArg[1]);
6039 break;
6042 }else
6043 #endif /* !defined(SQLITE_UNTESTABLE) */
6045 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
6046 open_db(p, 0);
6047 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
6048 }else
6050 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
6051 if( nArg==2 ){
6052 enableTimer = booleanValue(azArg[1]);
6053 if( enableTimer && !HAS_TIMER ){
6054 raw_printf(stderr, "Error: timer not available on this system.\n");
6055 enableTimer = 0;
6057 }else{
6058 raw_printf(stderr, "Usage: .timer on|off\n");
6059 rc = 1;
6061 }else
6063 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
6064 open_db(p, 0);
6065 if( nArg!=2 ){
6066 raw_printf(stderr, "Usage: .trace FILE|off\n");
6067 rc = 1;
6068 goto meta_command_exit;
6070 output_file_close(p->traceOut);
6071 p->traceOut = output_file_open(azArg[1]);
6072 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
6073 if( p->traceOut==0 ){
6074 sqlite3_trace_v2(p->db, 0, 0, 0);
6075 }else{
6076 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
6078 #endif
6079 }else
6081 #if SQLITE_USER_AUTHENTICATION
6082 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
6083 if( nArg<2 ){
6084 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
6085 rc = 1;
6086 goto meta_command_exit;
6088 open_db(p, 0);
6089 if( strcmp(azArg[1],"login")==0 ){
6090 if( nArg!=4 ){
6091 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
6092 rc = 1;
6093 goto meta_command_exit;
6095 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
6096 (int)strlen(azArg[3]));
6097 if( rc ){
6098 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
6099 rc = 1;
6101 }else if( strcmp(azArg[1],"add")==0 ){
6102 if( nArg!=5 ){
6103 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
6104 rc = 1;
6105 goto meta_command_exit;
6107 rc = sqlite3_user_add(p->db, azArg[2],
6108 azArg[3], (int)strlen(azArg[3]),
6109 booleanValue(azArg[4]));
6110 if( rc ){
6111 raw_printf(stderr, "User-Add failed: %d\n", rc);
6112 rc = 1;
6114 }else if( strcmp(azArg[1],"edit")==0 ){
6115 if( nArg!=5 ){
6116 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
6117 rc = 1;
6118 goto meta_command_exit;
6120 rc = sqlite3_user_change(p->db, azArg[2],
6121 azArg[3], (int)strlen(azArg[3]),
6122 booleanValue(azArg[4]));
6123 if( rc ){
6124 raw_printf(stderr, "User-Edit failed: %d\n", rc);
6125 rc = 1;
6127 }else if( strcmp(azArg[1],"delete")==0 ){
6128 if( nArg!=3 ){
6129 raw_printf(stderr, "Usage: .user delete USER\n");
6130 rc = 1;
6131 goto meta_command_exit;
6133 rc = sqlite3_user_delete(p->db, azArg[2]);
6134 if( rc ){
6135 raw_printf(stderr, "User-Delete failed: %d\n", rc);
6136 rc = 1;
6138 }else{
6139 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
6140 rc = 1;
6141 goto meta_command_exit;
6143 }else
6144 #endif /* SQLITE_USER_AUTHENTICATION */
6146 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
6147 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
6148 sqlite3_libversion(), sqlite3_sourceid());
6149 }else
6151 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
6152 const char *zDbName = nArg==2 ? azArg[1] : "main";
6153 sqlite3_vfs *pVfs = 0;
6154 if( p->db ){
6155 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
6156 if( pVfs ){
6157 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
6158 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6159 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6160 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6163 }else
6165 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
6166 sqlite3_vfs *pVfs;
6167 sqlite3_vfs *pCurrent = 0;
6168 if( p->db ){
6169 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
6171 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
6172 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
6173 pVfs==pCurrent ? " <--- CURRENT" : "");
6174 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6175 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6176 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6177 if( pVfs->pNext ){
6178 raw_printf(p->out, "-----------------------------------\n");
6181 }else
6183 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
6184 const char *zDbName = nArg==2 ? azArg[1] : "main";
6185 char *zVfsName = 0;
6186 if( p->db ){
6187 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
6188 if( zVfsName ){
6189 utf8_printf(p->out, "%s\n", zVfsName);
6190 sqlite3_free(zVfsName);
6193 }else
6195 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
6196 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
6197 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
6198 }else
6199 #endif
6201 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
6202 int j;
6203 assert( nArg<=ArraySize(azArg) );
6204 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
6205 p->colWidth[j-1] = (int)integerValue(azArg[j]);
6207 }else
6210 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
6211 " \"%s\". Enter \".help\" for help\n", azArg[0]);
6212 rc = 1;
6215 meta_command_exit:
6216 if( p->outCount ){
6217 p->outCount--;
6218 if( p->outCount==0 ) output_reset(p);
6220 return rc;
6224 ** Return TRUE if a semicolon occurs anywhere in the first N characters
6225 ** of string z[].
6227 static int line_contains_semicolon(const char *z, int N){
6228 int i;
6229 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
6230 return 0;
6234 ** Test to see if a line consists entirely of whitespace.
6236 static int _all_whitespace(const char *z){
6237 for(; *z; z++){
6238 if( IsSpace(z[0]) ) continue;
6239 if( *z=='/' && z[1]=='*' ){
6240 z += 2;
6241 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
6242 if( *z==0 ) return 0;
6243 z++;
6244 continue;
6246 if( *z=='-' && z[1]=='-' ){
6247 z += 2;
6248 while( *z && *z!='\n' ){ z++; }
6249 if( *z==0 ) return 1;
6250 continue;
6252 return 0;
6254 return 1;
6258 ** Return TRUE if the line typed in is an SQL command terminator other
6259 ** than a semi-colon. The SQL Server style "go" command is understood
6260 ** as is the Oracle "/".
6262 static int line_is_command_terminator(const char *zLine){
6263 while( IsSpace(zLine[0]) ){ zLine++; };
6264 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
6265 return 1; /* Oracle */
6267 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
6268 && _all_whitespace(&zLine[2]) ){
6269 return 1; /* SQL Server */
6271 return 0;
6275 ** Return true if zSql is a complete SQL statement. Return false if it
6276 ** ends in the middle of a string literal or C-style comment.
6278 static int line_is_complete(char *zSql, int nSql){
6279 int rc;
6280 if( zSql==0 ) return 1;
6281 zSql[nSql] = ';';
6282 zSql[nSql+1] = 0;
6283 rc = sqlite3_complete(zSql);
6284 zSql[nSql] = 0;
6285 return rc;
6289 ** Run a single line of SQL
6291 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
6292 int rc;
6293 char *zErrMsg = 0;
6295 open_db(p, 0);
6296 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
6297 BEGIN_TIMER;
6298 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
6299 END_TIMER;
6300 if( rc || zErrMsg ){
6301 char zPrefix[100];
6302 if( in!=0 || !stdin_is_interactive ){
6303 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
6304 "Error: near line %d:", startline);
6305 }else{
6306 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
6308 if( zErrMsg!=0 ){
6309 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
6310 sqlite3_free(zErrMsg);
6311 zErrMsg = 0;
6312 }else{
6313 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
6315 return 1;
6316 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
6317 raw_printf(p->out, "changes: %3d total_changes: %d\n",
6318 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
6320 return 0;
6325 ** Read input from *in and process it. If *in==0 then input
6326 ** is interactive - the user is typing it it. Otherwise, input
6327 ** is coming from a file or device. A prompt is issued and history
6328 ** is saved only if input is interactive. An interrupt signal will
6329 ** cause this routine to exit immediately, unless input is interactive.
6331 ** Return the number of errors.
6333 static int process_input(ShellState *p, FILE *in){
6334 char *zLine = 0; /* A single input line */
6335 char *zSql = 0; /* Accumulated SQL text */
6336 int nLine; /* Length of current line */
6337 int nSql = 0; /* Bytes of zSql[] used */
6338 int nAlloc = 0; /* Allocated zSql[] space */
6339 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
6340 int rc; /* Error code */
6341 int errCnt = 0; /* Number of errors seen */
6342 int lineno = 0; /* Current line number */
6343 int startline = 0; /* Line number for start of current input */
6345 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
6346 fflush(p->out);
6347 zLine = one_input_line(in, zLine, nSql>0);
6348 if( zLine==0 ){
6349 /* End of input */
6350 if( in==0 && stdin_is_interactive ) printf("\n");
6351 break;
6353 if( seenInterrupt ){
6354 if( in!=0 ) break;
6355 seenInterrupt = 0;
6357 lineno++;
6358 if( nSql==0 && _all_whitespace(zLine) ){
6359 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6360 continue;
6362 if( zLine && zLine[0]=='.' && nSql==0 ){
6363 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6364 rc = do_meta_command(zLine, p);
6365 if( rc==2 ){ /* exit requested */
6366 break;
6367 }else if( rc ){
6368 errCnt++;
6370 continue;
6372 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
6373 memcpy(zLine,";",2);
6375 nLine = strlen30(zLine);
6376 if( nSql+nLine+2>=nAlloc ){
6377 nAlloc = nSql+nLine+100;
6378 zSql = realloc(zSql, nAlloc);
6379 if( zSql==0 ){
6380 raw_printf(stderr, "Error: out of memory\n");
6381 exit(1);
6384 nSqlPrior = nSql;
6385 if( nSql==0 ){
6386 int i;
6387 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
6388 assert( nAlloc>0 && zSql!=0 );
6389 memcpy(zSql, zLine+i, nLine+1-i);
6390 startline = lineno;
6391 nSql = nLine-i;
6392 }else{
6393 zSql[nSql++] = '\n';
6394 memcpy(zSql+nSql, zLine, nLine+1);
6395 nSql += nLine;
6397 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
6398 && sqlite3_complete(zSql) ){
6399 errCnt += runOneSqlLine(p, zSql, in, startline);
6400 nSql = 0;
6401 if( p->outCount ){
6402 output_reset(p);
6403 p->outCount = 0;
6405 }else if( nSql && _all_whitespace(zSql) ){
6406 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
6407 nSql = 0;
6410 if( nSql && !_all_whitespace(zSql) ){
6411 runOneSqlLine(p, zSql, in, startline);
6413 free(zSql);
6414 free(zLine);
6415 return errCnt>0;
6419 ** Return a pathname which is the user's home directory. A
6420 ** 0 return indicates an error of some kind.
6422 static char *find_home_dir(int clearFlag){
6423 static char *home_dir = NULL;
6424 if( clearFlag ){
6425 free(home_dir);
6426 home_dir = 0;
6427 return 0;
6429 if( home_dir ) return home_dir;
6431 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
6432 && !defined(__RTP__) && !defined(_WRS_KERNEL)
6434 struct passwd *pwent;
6435 uid_t uid = getuid();
6436 if( (pwent=getpwuid(uid)) != NULL) {
6437 home_dir = pwent->pw_dir;
6440 #endif
6442 #if defined(_WIN32_WCE)
6443 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
6445 home_dir = "/";
6446 #else
6448 #if defined(_WIN32) || defined(WIN32)
6449 if (!home_dir) {
6450 home_dir = getenv("USERPROFILE");
6452 #endif
6454 if (!home_dir) {
6455 home_dir = getenv("HOME");
6458 #if defined(_WIN32) || defined(WIN32)
6459 if (!home_dir) {
6460 char *zDrive, *zPath;
6461 int n;
6462 zDrive = getenv("HOMEDRIVE");
6463 zPath = getenv("HOMEPATH");
6464 if( zDrive && zPath ){
6465 n = strlen30(zDrive) + strlen30(zPath) + 1;
6466 home_dir = malloc( n );
6467 if( home_dir==0 ) return 0;
6468 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
6469 return home_dir;
6471 home_dir = "c:\\";
6473 #endif
6475 #endif /* !_WIN32_WCE */
6477 if( home_dir ){
6478 int n = strlen30(home_dir) + 1;
6479 char *z = malloc( n );
6480 if( z ) memcpy(z, home_dir, n);
6481 home_dir = z;
6484 return home_dir;
6488 ** Read input from the file given by sqliterc_override. Or if that
6489 ** parameter is NULL, take input from ~/.sqliterc
6491 ** Returns the number of errors.
6493 static void process_sqliterc(
6494 ShellState *p, /* Configuration data */
6495 const char *sqliterc_override /* Name of config file. NULL to use default */
6497 char *home_dir = NULL;
6498 const char *sqliterc = sqliterc_override;
6499 char *zBuf = 0;
6500 FILE *in = NULL;
6502 if (sqliterc == NULL) {
6503 home_dir = find_home_dir(0);
6504 if( home_dir==0 ){
6505 raw_printf(stderr, "-- warning: cannot find home directory;"
6506 " cannot read ~/.sqliterc\n");
6507 return;
6509 sqlite3_initialize();
6510 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
6511 sqliterc = zBuf;
6513 in = fopen(sqliterc,"rb");
6514 if( in ){
6515 if( stdin_is_interactive ){
6516 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
6518 process_input(p,in);
6519 fclose(in);
6521 sqlite3_free(zBuf);
6525 ** Show available command line options
6527 static const char zOptions[] =
6528 " -ascii set output mode to 'ascii'\n"
6529 " -bail stop after hitting an error\n"
6530 " -batch force batch I/O\n"
6531 " -column set output mode to 'column'\n"
6532 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
6533 " -csv set output mode to 'csv'\n"
6534 " -echo print commands before execution\n"
6535 " -init FILENAME read/process named file\n"
6536 " -[no]header turn headers on or off\n"
6537 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6538 " -heap SIZE Size of heap for memsys3 or memsys5\n"
6539 #endif
6540 " -help show this message\n"
6541 " -html set output mode to HTML\n"
6542 " -interactive force interactive I/O\n"
6543 " -line set output mode to 'line'\n"
6544 " -list set output mode to 'list'\n"
6545 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
6546 " -mmap N default mmap size set to N\n"
6547 #ifdef SQLITE_ENABLE_MULTIPLEX
6548 " -multiplex enable the multiplexor VFS\n"
6549 #endif
6550 " -newline SEP set output row separator. Default: '\\n'\n"
6551 " -nullvalue TEXT set text string for NULL values. Default ''\n"
6552 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
6553 " -quote set output mode to 'quote'\n"
6554 " -separator SEP set output column separator. Default: '|'\n"
6555 " -stats print memory stats before each finalize\n"
6556 " -version show SQLite version\n"
6557 " -vfs NAME use NAME as the default VFS\n"
6558 #ifdef SQLITE_ENABLE_VFSTRACE
6559 " -vfstrace enable tracing of all VFS calls\n"
6560 #endif
6562 static void usage(int showDetail){
6563 utf8_printf(stderr,
6564 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
6565 "FILENAME is the name of an SQLite database. A new database is created\n"
6566 "if the file does not previously exist.\n", Argv0);
6567 if( showDetail ){
6568 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
6569 }else{
6570 raw_printf(stderr, "Use the -help option for additional information\n");
6572 exit(1);
6576 ** Initialize the state information in data
6578 static void main_init(ShellState *data) {
6579 memset(data, 0, sizeof(*data));
6580 data->normalMode = data->cMode = data->mode = MODE_List;
6581 data->autoExplain = 1;
6582 memcpy(data->colSeparator,SEP_Column, 2);
6583 memcpy(data->rowSeparator,SEP_Row, 2);
6584 data->showHeader = 0;
6585 data->shellFlgs = SHFLG_Lookaside;
6586 sqlite3_config(SQLITE_CONFIG_URI, 1);
6587 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
6588 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
6589 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
6590 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
6594 ** Output text to the console in a font that attracts extra attention.
6596 #ifdef _WIN32
6597 static void printBold(const char *zText){
6598 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
6599 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
6600 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
6601 SetConsoleTextAttribute(out,
6602 FOREGROUND_RED|FOREGROUND_INTENSITY
6604 printf("%s", zText);
6605 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
6607 #else
6608 static void printBold(const char *zText){
6609 printf("\033[1m%s\033[0m", zText);
6611 #endif
6614 ** Get the argument to an --option. Throw an error and die if no argument
6615 ** is available.
6617 static char *cmdline_option_value(int argc, char **argv, int i){
6618 if( i==argc ){
6619 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
6620 argv[0], argv[argc-1]);
6621 exit(1);
6623 return argv[i];
6626 #ifndef SQLITE_SHELL_IS_UTF8
6627 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
6628 # define SQLITE_SHELL_IS_UTF8 (0)
6629 # else
6630 # define SQLITE_SHELL_IS_UTF8 (1)
6631 # endif
6632 #endif
6634 #if SQLITE_SHELL_IS_UTF8
6635 int SQLITE_CDECL main(int argc, char **argv){
6636 #else
6637 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
6638 char **argv;
6639 #endif
6640 char *zErrMsg = 0;
6641 ShellState data;
6642 const char *zInitFile = 0;
6643 int i;
6644 int rc = 0;
6645 int warnInmemoryDb = 0;
6646 int readStdin = 1;
6647 int nCmd = 0;
6648 char **azCmd = 0;
6650 setBinaryMode(stdin, 0);
6651 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
6652 stdin_is_interactive = isatty(0);
6653 stdout_is_console = isatty(1);
6655 #if USE_SYSTEM_SQLITE+0!=1
6656 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
6657 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
6658 sqlite3_sourceid(), SQLITE_SOURCE_ID);
6659 exit(1);
6661 #endif
6662 main_init(&data);
6663 #if !SQLITE_SHELL_IS_UTF8
6664 sqlite3_initialize();
6665 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
6666 if( argv==0 ){
6667 raw_printf(stderr, "out of memory\n");
6668 exit(1);
6670 for(i=0; i<argc; i++){
6671 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
6672 if( argv[i]==0 ){
6673 raw_printf(stderr, "out of memory\n");
6674 exit(1);
6677 #endif
6678 assert( argc>=1 && argv && argv[0] );
6679 Argv0 = argv[0];
6681 /* Make sure we have a valid signal handler early, before anything
6682 ** else is done.
6684 #ifdef SIGINT
6685 signal(SIGINT, interrupt_handler);
6686 #endif
6688 #ifdef SQLITE_SHELL_DBNAME_PROC
6690 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
6691 ** of a C-function that will provide the name of the database file. Use
6692 ** this compile-time option to embed this shell program in larger
6693 ** applications. */
6694 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
6695 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
6696 warnInmemoryDb = 0;
6698 #endif
6700 /* Do an initial pass through the command-line argument to locate
6701 ** the name of the database file, the name of the initialization file,
6702 ** the size of the alternative malloc heap,
6703 ** and the first command to execute.
6705 for(i=1; i<argc; i++){
6706 char *z;
6707 z = argv[i];
6708 if( z[0]!='-' ){
6709 if( data.zDbFilename==0 ){
6710 data.zDbFilename = z;
6711 }else{
6712 /* Excesss arguments are interpreted as SQL (or dot-commands) and
6713 ** mean that nothing is read from stdin */
6714 readStdin = 0;
6715 nCmd++;
6716 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
6717 if( azCmd==0 ){
6718 raw_printf(stderr, "out of memory\n");
6719 exit(1);
6721 azCmd[nCmd-1] = z;
6724 if( z[1]=='-' ) z++;
6725 if( strcmp(z,"-separator")==0
6726 || strcmp(z,"-nullvalue")==0
6727 || strcmp(z,"-newline")==0
6728 || strcmp(z,"-cmd")==0
6730 (void)cmdline_option_value(argc, argv, ++i);
6731 }else if( strcmp(z,"-init")==0 ){
6732 zInitFile = cmdline_option_value(argc, argv, ++i);
6733 }else if( strcmp(z,"-batch")==0 ){
6734 /* Need to check for batch mode here to so we can avoid printing
6735 ** informational messages (like from process_sqliterc) before
6736 ** we do the actual processing of arguments later in a second pass.
6738 stdin_is_interactive = 0;
6739 }else if( strcmp(z,"-heap")==0 ){
6740 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6741 const char *zSize;
6742 sqlite3_int64 szHeap;
6744 zSize = cmdline_option_value(argc, argv, ++i);
6745 szHeap = integerValue(zSize);
6746 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
6747 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
6748 #else
6749 (void)cmdline_option_value(argc, argv, ++i);
6750 #endif
6751 }else if( strcmp(z,"-pagecache")==0 ){
6752 int n, sz;
6753 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6754 if( sz>70000 ) sz = 70000;
6755 if( sz<0 ) sz = 0;
6756 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6757 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
6758 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
6759 data.shellFlgs |= SHFLG_Pagecache;
6760 }else if( strcmp(z,"-lookaside")==0 ){
6761 int n, sz;
6762 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6763 if( sz<0 ) sz = 0;
6764 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6765 if( n<0 ) n = 0;
6766 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
6767 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
6768 #ifdef SQLITE_ENABLE_VFSTRACE
6769 }else if( strcmp(z,"-vfstrace")==0 ){
6770 extern int vfstrace_register(
6771 const char *zTraceName,
6772 const char *zOldVfsName,
6773 int (*xOut)(const char*,void*),
6774 void *pOutArg,
6775 int makeDefault
6777 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
6778 #endif
6779 #ifdef SQLITE_ENABLE_MULTIPLEX
6780 }else if( strcmp(z,"-multiplex")==0 ){
6781 extern int sqlite3_multiple_initialize(const char*,int);
6782 sqlite3_multiplex_initialize(0, 1);
6783 #endif
6784 }else if( strcmp(z,"-mmap")==0 ){
6785 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
6786 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
6787 }else if( strcmp(z,"-vfs")==0 ){
6788 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
6789 if( pVfs ){
6790 sqlite3_vfs_register(pVfs, 1);
6791 }else{
6792 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
6793 exit(1);
6797 if( data.zDbFilename==0 ){
6798 #ifndef SQLITE_OMIT_MEMORYDB
6799 data.zDbFilename = ":memory:";
6800 warnInmemoryDb = argc==1;
6801 #else
6802 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
6803 return 1;
6804 #endif
6806 data.out = stdout;
6808 /* Go ahead and open the database file if it already exists. If the
6809 ** file does not exist, delay opening it. This prevents empty database
6810 ** files from being created if a user mistypes the database name argument
6811 ** to the sqlite command-line tool.
6813 if( access(data.zDbFilename, 0)==0 ){
6814 open_db(&data, 0);
6817 /* Process the initialization file if there is one. If no -init option
6818 ** is given on the command line, look for a file named ~/.sqliterc and
6819 ** try to process it.
6821 process_sqliterc(&data,zInitFile);
6823 /* Make a second pass through the command-line argument and set
6824 ** options. This second pass is delayed until after the initialization
6825 ** file is processed so that the command-line arguments will override
6826 ** settings in the initialization file.
6828 for(i=1; i<argc; i++){
6829 char *z = argv[i];
6830 if( z[0]!='-' ) continue;
6831 if( z[1]=='-' ){ z++; }
6832 if( strcmp(z,"-init")==0 ){
6833 i++;
6834 }else if( strcmp(z,"-html")==0 ){
6835 data.mode = MODE_Html;
6836 }else if( strcmp(z,"-list")==0 ){
6837 data.mode = MODE_List;
6838 }else if( strcmp(z,"-quote")==0 ){
6839 data.mode = MODE_Quote;
6840 }else if( strcmp(z,"-line")==0 ){
6841 data.mode = MODE_Line;
6842 }else if( strcmp(z,"-column")==0 ){
6843 data.mode = MODE_Column;
6844 }else if( strcmp(z,"-csv")==0 ){
6845 data.mode = MODE_Csv;
6846 memcpy(data.colSeparator,",",2);
6847 }else if( strcmp(z,"-ascii")==0 ){
6848 data.mode = MODE_Ascii;
6849 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6850 SEP_Unit);
6851 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6852 SEP_Record);
6853 }else if( strcmp(z,"-separator")==0 ){
6854 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6855 "%s",cmdline_option_value(argc,argv,++i));
6856 }else if( strcmp(z,"-newline")==0 ){
6857 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6858 "%s",cmdline_option_value(argc,argv,++i));
6859 }else if( strcmp(z,"-nullvalue")==0 ){
6860 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
6861 "%s",cmdline_option_value(argc,argv,++i));
6862 }else if( strcmp(z,"-header")==0 ){
6863 data.showHeader = 1;
6864 }else if( strcmp(z,"-noheader")==0 ){
6865 data.showHeader = 0;
6866 }else if( strcmp(z,"-echo")==0 ){
6867 ShellSetFlag(&data, SHFLG_Echo);
6868 }else if( strcmp(z,"-eqp")==0 ){
6869 data.autoEQP = 1;
6870 }else if( strcmp(z,"-eqpfull")==0 ){
6871 data.autoEQP = 2;
6872 }else if( strcmp(z,"-stats")==0 ){
6873 data.statsOn = 1;
6874 }else if( strcmp(z,"-scanstats")==0 ){
6875 data.scanstatsOn = 1;
6876 }else if( strcmp(z,"-backslash")==0 ){
6877 /* Undocumented command-line option: -backslash
6878 ** Causes C-style backslash escapes to be evaluated in SQL statements
6879 ** prior to sending the SQL into SQLite. Useful for injecting
6880 ** crazy bytes in the middle of SQL statements for testing and debugging.
6882 ShellSetFlag(&data, SHFLG_Backslash);
6883 }else if( strcmp(z,"-bail")==0 ){
6884 bail_on_error = 1;
6885 }else if( strcmp(z,"-version")==0 ){
6886 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
6887 return 0;
6888 }else if( strcmp(z,"-interactive")==0 ){
6889 stdin_is_interactive = 1;
6890 }else if( strcmp(z,"-batch")==0 ){
6891 stdin_is_interactive = 0;
6892 }else if( strcmp(z,"-heap")==0 ){
6893 i++;
6894 }else if( strcmp(z,"-pagecache")==0 ){
6895 i+=2;
6896 }else if( strcmp(z,"-lookaside")==0 ){
6897 i+=2;
6898 }else if( strcmp(z,"-mmap")==0 ){
6899 i++;
6900 }else if( strcmp(z,"-vfs")==0 ){
6901 i++;
6902 #ifdef SQLITE_ENABLE_VFSTRACE
6903 }else if( strcmp(z,"-vfstrace")==0 ){
6904 i++;
6905 #endif
6906 #ifdef SQLITE_ENABLE_MULTIPLEX
6907 }else if( strcmp(z,"-multiplex")==0 ){
6908 i++;
6909 #endif
6910 }else if( strcmp(z,"-help")==0 ){
6911 usage(1);
6912 }else if( strcmp(z,"-cmd")==0 ){
6913 /* Run commands that follow -cmd first and separately from commands
6914 ** that simply appear on the command-line. This seems goofy. It would
6915 ** be better if all commands ran in the order that they appear. But
6916 ** we retain the goofy behavior for historical compatibility. */
6917 if( i==argc-1 ) break;
6918 z = cmdline_option_value(argc,argv,++i);
6919 if( z[0]=='.' ){
6920 rc = do_meta_command(z, &data);
6921 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
6922 }else{
6923 open_db(&data, 0);
6924 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
6925 if( zErrMsg!=0 ){
6926 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6927 if( bail_on_error ) return rc!=0 ? rc : 1;
6928 }else if( rc!=0 ){
6929 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
6930 if( bail_on_error ) return rc;
6933 }else{
6934 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
6935 raw_printf(stderr,"Use -help for a list of options.\n");
6936 return 1;
6938 data.cMode = data.mode;
6941 if( !readStdin ){
6942 /* Run all arguments that do not begin with '-' as if they were separate
6943 ** command-line inputs, except for the argToSkip argument which contains
6944 ** the database filename.
6946 for(i=0; i<nCmd; i++){
6947 if( azCmd[i][0]=='.' ){
6948 rc = do_meta_command(azCmd[i], &data);
6949 if( rc ) return rc==2 ? 0 : rc;
6950 }else{
6951 open_db(&data, 0);
6952 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
6953 if( zErrMsg!=0 ){
6954 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6955 return rc!=0 ? rc : 1;
6956 }else if( rc!=0 ){
6957 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
6958 return rc;
6962 free(azCmd);
6963 }else{
6964 /* Run commands received from standard input
6966 if( stdin_is_interactive ){
6967 char *zHome;
6968 char *zHistory = 0;
6969 int nHistory;
6970 printf(
6971 "SQLite version %s %.19s\n" /*extra-version-info*/
6972 "Enter \".help\" for usage hints.\n",
6973 sqlite3_libversion(), sqlite3_sourceid()
6975 if( warnInmemoryDb ){
6976 printf("Connected to a ");
6977 printBold("transient in-memory database");
6978 printf(".\nUse \".open FILENAME\" to reopen on a "
6979 "persistent database.\n");
6981 zHome = find_home_dir(0);
6982 if( zHome ){
6983 nHistory = strlen30(zHome) + 20;
6984 if( (zHistory = malloc(nHistory))!=0 ){
6985 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
6988 if( zHistory ){ shell_read_history(zHistory); }
6989 #if HAVE_READLINE || HAVE_EDITLINE
6990 rl_attempted_completion_function = readline_completion;
6991 #elif HAVE_LINENOISE
6992 linenoiseSetCompletionCallback(linenoise_completion);
6993 #endif
6994 rc = process_input(&data, 0);
6995 if( zHistory ){
6996 shell_stifle_history(2000);
6997 shell_write_history(zHistory);
6998 free(zHistory);
7000 }else{
7001 rc = process_input(&data, stdin);
7004 set_table_name(&data, 0);
7005 if( data.db ){
7006 session_close_all(&data);
7007 sqlite3_close(data.db);
7009 sqlite3_free(data.zFreeOnClose);
7010 find_home_dir(1);
7011 #if !SQLITE_SHELL_IS_UTF8
7012 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
7013 sqlite3_free(argv);
7014 #endif
7015 return rc;