Revise variable declaration moved in the previous check-in so sqlite3VdbeReset()...
[sqlite.git] / src / shell.c.in
blobdb4f2e1128e1d226d0189527fedec1b087dca68d
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( z[j-1]=='\r' ) z[j-1] = '\n';
1417 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1418 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1419 j--;
1421 z[j++] = c;
1423 while( j>0 && IsSpace(z[j-1]) ){ j--; }
1424 z[j] = 0;
1425 if( strlen30(z)>=79 ){
1426 for(i=j=0; (c = z[i])!=0; i++){
1427 if( c==cEnd ){
1428 cEnd = 0;
1429 }else if( c=='"' || c=='\'' || c=='`' ){
1430 cEnd = c;
1431 }else if( c=='[' ){
1432 cEnd = ']';
1433 }else if( c=='(' ){
1434 nParen++;
1435 }else if( c==')' ){
1436 nParen--;
1437 if( nLine>0 && nParen==0 && j>0 ){
1438 printSchemaLineN(p->out, z, j, "\n");
1439 j = 0;
1442 z[j++] = c;
1443 if( nParen==1 && (c=='(' || c==',' || c=='\n') ){
1444 if( c=='\n' ) j--;
1445 printSchemaLineN(p->out, z, j, "\n ");
1446 j = 0;
1447 nLine++;
1448 while( IsSpace(z[i+1]) ){ i++; }
1451 z[j] = 0;
1453 printSchemaLine(p->out, z, ";\n");
1454 sqlite3_free(z);
1455 break;
1457 case MODE_List: {
1458 if( p->cnt++==0 && p->showHeader ){
1459 for(i=0; i<nArg; i++){
1460 utf8_printf(p->out,"%s%s",azCol[i],
1461 i==nArg-1 ? p->rowSeparator : p->colSeparator);
1464 if( azArg==0 ) break;
1465 for(i=0; i<nArg; i++){
1466 char *z = azArg[i];
1467 if( z==0 ) z = p->nullValue;
1468 utf8_printf(p->out, "%s", z);
1469 if( i<nArg-1 ){
1470 utf8_printf(p->out, "%s", p->colSeparator);
1471 }else{
1472 utf8_printf(p->out, "%s", p->rowSeparator);
1475 break;
1477 case MODE_Html: {
1478 if( p->cnt++==0 && p->showHeader ){
1479 raw_printf(p->out,"<TR>");
1480 for(i=0; i<nArg; i++){
1481 raw_printf(p->out,"<TH>");
1482 output_html_string(p->out, azCol[i]);
1483 raw_printf(p->out,"</TH>\n");
1485 raw_printf(p->out,"</TR>\n");
1487 if( azArg==0 ) break;
1488 raw_printf(p->out,"<TR>");
1489 for(i=0; i<nArg; i++){
1490 raw_printf(p->out,"<TD>");
1491 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1492 raw_printf(p->out,"</TD>\n");
1494 raw_printf(p->out,"</TR>\n");
1495 break;
1497 case MODE_Tcl: {
1498 if( p->cnt++==0 && p->showHeader ){
1499 for(i=0; i<nArg; i++){
1500 output_c_string(p->out,azCol[i] ? azCol[i] : "");
1501 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1503 utf8_printf(p->out, "%s", p->rowSeparator);
1505 if( azArg==0 ) break;
1506 for(i=0; i<nArg; i++){
1507 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1508 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1510 utf8_printf(p->out, "%s", p->rowSeparator);
1511 break;
1513 case MODE_Csv: {
1514 setBinaryMode(p->out, 1);
1515 if( p->cnt++==0 && p->showHeader ){
1516 for(i=0; i<nArg; i++){
1517 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
1519 utf8_printf(p->out, "%s", p->rowSeparator);
1521 if( nArg>0 ){
1522 for(i=0; i<nArg; i++){
1523 output_csv(p, azArg[i], i<nArg-1);
1525 utf8_printf(p->out, "%s", p->rowSeparator);
1527 setTextMode(p->out, 1);
1528 break;
1530 case MODE_Insert: {
1531 if( azArg==0 ) break;
1532 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
1533 if( p->showHeader ){
1534 raw_printf(p->out,"(");
1535 for(i=0; i<nArg; i++){
1536 if( i>0 ) raw_printf(p->out, ",");
1537 if( quoteChar(azCol[i]) ){
1538 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
1539 utf8_printf(p->out, "%s", z);
1540 sqlite3_free(z);
1541 }else{
1542 raw_printf(p->out, "%s", azCol[i]);
1545 raw_printf(p->out,")");
1547 p->cnt++;
1548 for(i=0; i<nArg; i++){
1549 raw_printf(p->out, i>0 ? "," : " VALUES(");
1550 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1551 utf8_printf(p->out,"NULL");
1552 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1553 if( ShellHasFlag(p, SHFLG_Newlines) ){
1554 output_quoted_string(p->out, azArg[i]);
1555 }else{
1556 output_quoted_escaped_string(p->out, azArg[i]);
1558 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1559 utf8_printf(p->out,"%s", azArg[i]);
1560 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1561 char z[50];
1562 double r = sqlite3_column_double(p->pStmt, i);
1563 sqlite3_snprintf(50,z,"%!.20g", r);
1564 raw_printf(p->out, "%s", z);
1565 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1566 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1567 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1568 output_hex_blob(p->out, pBlob, nBlob);
1569 }else if( isNumber(azArg[i], 0) ){
1570 utf8_printf(p->out,"%s", azArg[i]);
1571 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
1572 output_quoted_string(p->out, azArg[i]);
1573 }else{
1574 output_quoted_escaped_string(p->out, azArg[i]);
1577 raw_printf(p->out,");\n");
1578 break;
1580 case MODE_Quote: {
1581 if( azArg==0 ) break;
1582 if( p->cnt==0 && p->showHeader ){
1583 for(i=0; i<nArg; i++){
1584 if( i>0 ) raw_printf(p->out, ",");
1585 output_quoted_string(p->out, azCol[i]);
1587 raw_printf(p->out,"\n");
1589 p->cnt++;
1590 for(i=0; i<nArg; i++){
1591 if( i>0 ) raw_printf(p->out, ",");
1592 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1593 utf8_printf(p->out,"NULL");
1594 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1595 output_quoted_string(p->out, azArg[i]);
1596 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1597 utf8_printf(p->out,"%s", azArg[i]);
1598 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1599 char z[50];
1600 double r = sqlite3_column_double(p->pStmt, i);
1601 sqlite3_snprintf(50,z,"%!.20g", r);
1602 raw_printf(p->out, "%s", z);
1603 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1604 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1605 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1606 output_hex_blob(p->out, pBlob, nBlob);
1607 }else if( isNumber(azArg[i], 0) ){
1608 utf8_printf(p->out,"%s", azArg[i]);
1609 }else{
1610 output_quoted_string(p->out, azArg[i]);
1613 raw_printf(p->out,"\n");
1614 break;
1616 case MODE_Ascii: {
1617 if( p->cnt++==0 && p->showHeader ){
1618 for(i=0; i<nArg; i++){
1619 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1620 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
1622 utf8_printf(p->out, "%s", p->rowSeparator);
1624 if( azArg==0 ) break;
1625 for(i=0; i<nArg; i++){
1626 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1627 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
1629 utf8_printf(p->out, "%s", p->rowSeparator);
1630 break;
1633 return 0;
1637 ** This is the callback routine that the SQLite library
1638 ** invokes for each row of a query result.
1640 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
1641 /* since we don't have type info, call the shell_callback with a NULL value */
1642 return shell_callback(pArg, nArg, azArg, azCol, NULL);
1646 ** This is the callback routine from sqlite3_exec() that appends all
1647 ** output onto the end of a ShellText object.
1649 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
1650 ShellText *p = (ShellText*)pArg;
1651 int i;
1652 UNUSED_PARAMETER(az);
1653 if( azArg==0 ) return 0;
1654 if( p->n ) appendText(p, "|", 0);
1655 for(i=0; i<nArg; i++){
1656 if( i ) appendText(p, ",", 0);
1657 if( azArg[i] ) appendText(p, azArg[i], 0);
1659 return 0;
1663 ** Generate an appropriate SELFTEST table in the main database.
1665 static void createSelftestTable(ShellState *p){
1666 char *zErrMsg = 0;
1667 sqlite3_exec(p->db,
1668 "SAVEPOINT selftest_init;\n"
1669 "CREATE TABLE IF NOT EXISTS selftest(\n"
1670 " tno INTEGER PRIMARY KEY,\n" /* Test number */
1671 " op TEXT,\n" /* Operator: memo run */
1672 " cmd TEXT,\n" /* Command text */
1673 " ans TEXT\n" /* Desired answer */
1674 ");"
1675 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
1676 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
1677 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
1678 " 'memo','Tests generated by --init');\n"
1679 "INSERT INTO [_shell$self]\n"
1680 " SELECT 'run',\n"
1681 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
1682 "FROM sqlite_master ORDER BY 2'',224))',\n"
1683 " hex(sha3_query('SELECT type,name,tbl_name,sql "
1684 "FROM sqlite_master ORDER BY 2',224));\n"
1685 "INSERT INTO [_shell$self]\n"
1686 " SELECT 'run',"
1687 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
1688 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
1689 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
1690 " FROM (\n"
1691 " SELECT name FROM sqlite_master\n"
1692 " WHERE type='table'\n"
1693 " AND name<>'selftest'\n"
1694 " AND coalesce(rootpage,0)>0\n"
1695 " )\n"
1696 " ORDER BY name;\n"
1697 "INSERT INTO [_shell$self]\n"
1698 " VALUES('run','PRAGMA integrity_check','ok');\n"
1699 "INSERT INTO selftest(tno,op,cmd,ans)"
1700 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
1701 "DROP TABLE [_shell$self];"
1702 ,0,0,&zErrMsg);
1703 if( zErrMsg ){
1704 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
1705 sqlite3_free(zErrMsg);
1707 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
1712 ** Set the destination table field of the ShellState structure to
1713 ** the name of the table given. Escape any quote characters in the
1714 ** table name.
1716 static void set_table_name(ShellState *p, const char *zName){
1717 int i, n;
1718 char cQuote;
1719 char *z;
1721 if( p->zDestTable ){
1722 free(p->zDestTable);
1723 p->zDestTable = 0;
1725 if( zName==0 ) return;
1726 cQuote = quoteChar(zName);
1727 n = strlen30(zName);
1728 if( cQuote ) n += n+2;
1729 z = p->zDestTable = malloc( n+1 );
1730 if( z==0 ){
1731 raw_printf(stderr,"Error: out of memory\n");
1732 exit(1);
1734 n = 0;
1735 if( cQuote ) z[n++] = cQuote;
1736 for(i=0; zName[i]; i++){
1737 z[n++] = zName[i];
1738 if( zName[i]==cQuote ) z[n++] = cQuote;
1740 if( cQuote ) z[n++] = cQuote;
1741 z[n] = 0;
1746 ** Execute a query statement that will generate SQL output. Print
1747 ** the result columns, comma-separated, on a line and then add a
1748 ** semicolon terminator to the end of that line.
1750 ** If the number of columns is 1 and that column contains text "--"
1751 ** then write the semicolon on a separate line. That way, if a
1752 ** "--" comment occurs at the end of the statement, the comment
1753 ** won't consume the semicolon terminator.
1755 static int run_table_dump_query(
1756 ShellState *p, /* Query context */
1757 const char *zSelect, /* SELECT statement to extract content */
1758 const char *zFirstRow /* Print before first row, if not NULL */
1760 sqlite3_stmt *pSelect;
1761 int rc;
1762 int nResult;
1763 int i;
1764 const char *z;
1765 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1766 if( rc!=SQLITE_OK || !pSelect ){
1767 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1768 sqlite3_errmsg(p->db));
1769 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1770 return rc;
1772 rc = sqlite3_step(pSelect);
1773 nResult = sqlite3_column_count(pSelect);
1774 while( rc==SQLITE_ROW ){
1775 if( zFirstRow ){
1776 utf8_printf(p->out, "%s", zFirstRow);
1777 zFirstRow = 0;
1779 z = (const char*)sqlite3_column_text(pSelect, 0);
1780 utf8_printf(p->out, "%s", z);
1781 for(i=1; i<nResult; i++){
1782 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
1784 if( z==0 ) z = "";
1785 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
1786 if( z[0] ){
1787 raw_printf(p->out, "\n;\n");
1788 }else{
1789 raw_printf(p->out, ";\n");
1791 rc = sqlite3_step(pSelect);
1793 rc = sqlite3_finalize(pSelect);
1794 if( rc!=SQLITE_OK ){
1795 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1796 sqlite3_errmsg(p->db));
1797 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1799 return rc;
1803 ** Allocate space and save off current error string.
1805 static char *save_err_msg(
1806 sqlite3 *db /* Database to query */
1808 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
1809 char *zErrMsg = sqlite3_malloc64(nErrMsg);
1810 if( zErrMsg ){
1811 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
1813 return zErrMsg;
1816 #ifdef __linux__
1818 ** Attempt to display I/O stats on Linux using /proc/PID/io
1820 static void displayLinuxIoStats(FILE *out){
1821 FILE *in;
1822 char z[200];
1823 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1824 in = fopen(z, "rb");
1825 if( in==0 ) return;
1826 while( fgets(z, sizeof(z), in)!=0 ){
1827 static const struct {
1828 const char *zPattern;
1829 const char *zDesc;
1830 } aTrans[] = {
1831 { "rchar: ", "Bytes received by read():" },
1832 { "wchar: ", "Bytes sent to write():" },
1833 { "syscr: ", "Read() system calls:" },
1834 { "syscw: ", "Write() system calls:" },
1835 { "read_bytes: ", "Bytes read from storage:" },
1836 { "write_bytes: ", "Bytes written to storage:" },
1837 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
1839 int i;
1840 for(i=0; i<ArraySize(aTrans); i++){
1841 int n = (int)strlen(aTrans[i].zPattern);
1842 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1843 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
1844 break;
1848 fclose(in);
1850 #endif
1853 ** Display a single line of status using 64-bit values.
1855 static void displayStatLine(
1856 ShellState *p, /* The shell context */
1857 char *zLabel, /* Label for this one line */
1858 char *zFormat, /* Format for the result */
1859 int iStatusCtrl, /* Which status to display */
1860 int bReset /* True to reset the stats */
1862 sqlite3_int64 iCur = -1;
1863 sqlite3_int64 iHiwtr = -1;
1864 int i, nPercent;
1865 char zLine[200];
1866 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
1867 for(i=0, nPercent=0; zFormat[i]; i++){
1868 if( zFormat[i]=='%' ) nPercent++;
1870 if( nPercent>1 ){
1871 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
1872 }else{
1873 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
1875 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
1879 ** Display memory stats.
1881 static int display_stats(
1882 sqlite3 *db, /* Database to query */
1883 ShellState *pArg, /* Pointer to ShellState */
1884 int bReset /* True to reset the stats */
1886 int iCur;
1887 int iHiwtr;
1889 if( pArg && pArg->out ){
1890 displayStatLine(pArg, "Memory Used:",
1891 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
1892 displayStatLine(pArg, "Number of Outstanding Allocations:",
1893 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
1894 if( pArg->shellFlgs & SHFLG_Pagecache ){
1895 displayStatLine(pArg, "Number of Pcache Pages Used:",
1896 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
1898 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
1899 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
1900 displayStatLine(pArg, "Largest Allocation:",
1901 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
1902 displayStatLine(pArg, "Largest Pcache Allocation:",
1903 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
1904 #ifdef YYTRACKMAXSTACKDEPTH
1905 displayStatLine(pArg, "Deepest Parser Stack:",
1906 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
1907 #endif
1910 if( pArg && pArg->out && db ){
1911 if( pArg->shellFlgs & SHFLG_Lookaside ){
1912 iHiwtr = iCur = -1;
1913 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1914 &iCur, &iHiwtr, bReset);
1915 raw_printf(pArg->out,
1916 "Lookaside Slots Used: %d (max %d)\n",
1917 iCur, iHiwtr);
1918 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1919 &iCur, &iHiwtr, bReset);
1920 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
1921 iHiwtr);
1922 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1923 &iCur, &iHiwtr, bReset);
1924 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
1925 iHiwtr);
1926 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1927 &iCur, &iHiwtr, bReset);
1928 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
1929 iHiwtr);
1931 iHiwtr = iCur = -1;
1932 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1933 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
1934 iCur);
1935 iHiwtr = iCur = -1;
1936 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1937 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
1938 iHiwtr = iCur = -1;
1939 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1940 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
1941 iHiwtr = iCur = -1;
1942 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1943 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
1944 iHiwtr = iCur = -1;
1945 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1946 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
1947 iCur);
1948 iHiwtr = iCur = -1;
1949 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1950 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
1951 iCur);
1954 if( pArg && pArg->out && db && pArg->pStmt ){
1955 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1956 bReset);
1957 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
1958 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1959 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
1960 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
1961 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1962 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1963 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1966 #ifdef __linux__
1967 displayLinuxIoStats(pArg->out);
1968 #endif
1970 /* Do not remove this machine readable comment: extra-stats-output-here */
1972 return 0;
1976 ** Display scan stats.
1978 static void display_scanstats(
1979 sqlite3 *db, /* Database to query */
1980 ShellState *pArg /* Pointer to ShellState */
1982 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1983 UNUSED_PARAMETER(db);
1984 UNUSED_PARAMETER(pArg);
1985 #else
1986 int i, k, n, mx;
1987 raw_printf(pArg->out, "-------- scanstats --------\n");
1988 mx = 0;
1989 for(k=0; k<=mx; k++){
1990 double rEstLoop = 1.0;
1991 for(i=n=0; 1; i++){
1992 sqlite3_stmt *p = pArg->pStmt;
1993 sqlite3_int64 nLoop, nVisit;
1994 double rEst;
1995 int iSid;
1996 const char *zExplain;
1997 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
1998 break;
2000 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2001 if( iSid>mx ) mx = iSid;
2002 if( iSid!=k ) continue;
2003 if( n==0 ){
2004 rEstLoop = (double)nLoop;
2005 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2007 n++;
2008 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2009 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2010 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2011 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2012 rEstLoop *= rEst;
2013 raw_printf(pArg->out,
2014 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2015 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2019 raw_printf(pArg->out, "---------------------------\n");
2020 #endif
2024 ** Parameter azArray points to a zero-terminated array of strings. zStr
2025 ** points to a single nul-terminated string. Return non-zero if zStr
2026 ** is equal, according to strcmp(), to any of the strings in the array.
2027 ** Otherwise, return zero.
2029 static int str_in_array(const char *zStr, const char **azArray){
2030 int i;
2031 for(i=0; azArray[i]; i++){
2032 if( 0==strcmp(zStr, azArray[i]) ) return 1;
2034 return 0;
2038 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2039 ** and populate the ShellState.aiIndent[] array with the number of
2040 ** spaces each opcode should be indented before it is output.
2042 ** The indenting rules are:
2044 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2045 ** all opcodes that occur between the p2 jump destination and the opcode
2046 ** itself by 2 spaces.
2048 ** * For each "Goto", if the jump destination is earlier in the program
2049 ** and ends on one of:
2050 ** Yield SeekGt SeekLt RowSetRead Rewind
2051 ** or if the P1 parameter is one instead of zero,
2052 ** then indent all opcodes between the earlier instruction
2053 ** and "Goto" by 2 spaces.
2055 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2056 const char *zSql; /* The text of the SQL statement */
2057 const char *z; /* Used to check if this is an EXPLAIN */
2058 int *abYield = 0; /* True if op is an OP_Yield */
2059 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
2060 int iOp; /* Index of operation in p->aiIndent[] */
2062 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
2063 "NextIfOpen", "PrevIfOpen", 0 };
2064 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2065 "Rewind", 0 };
2066 const char *azGoto[] = { "Goto", 0 };
2068 /* Try to figure out if this is really an EXPLAIN statement. If this
2069 ** cannot be verified, return early. */
2070 if( sqlite3_column_count(pSql)!=8 ){
2071 p->cMode = p->mode;
2072 return;
2074 zSql = sqlite3_sql(pSql);
2075 if( zSql==0 ) return;
2076 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2077 if( sqlite3_strnicmp(z, "explain", 7) ){
2078 p->cMode = p->mode;
2079 return;
2082 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2083 int i;
2084 int iAddr = sqlite3_column_int(pSql, 0);
2085 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2087 /* Set p2 to the P2 field of the current opcode. Then, assuming that
2088 ** p2 is an instruction address, set variable p2op to the index of that
2089 ** instruction in the aiIndent[] array. p2 and p2op may be different if
2090 ** the current instruction is part of a sub-program generated by an
2091 ** SQL trigger or foreign key. */
2092 int p2 = sqlite3_column_int(pSql, 3);
2093 int p2op = (p2 + (iOp-iAddr));
2095 /* Grow the p->aiIndent array as required */
2096 if( iOp>=nAlloc ){
2097 if( iOp==0 ){
2098 /* Do further verfication that this is explain output. Abort if
2099 ** it is not */
2100 static const char *explainCols[] = {
2101 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2102 int jj;
2103 for(jj=0; jj<ArraySize(explainCols); jj++){
2104 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2105 p->cMode = p->mode;
2106 sqlite3_reset(pSql);
2107 return;
2111 nAlloc += 100;
2112 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2113 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2115 abYield[iOp] = str_in_array(zOp, azYield);
2116 p->aiIndent[iOp] = 0;
2117 p->nIndent = iOp+1;
2119 if( str_in_array(zOp, azNext) ){
2120 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2122 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2123 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2125 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2129 p->iIndent = 0;
2130 sqlite3_free(abYield);
2131 sqlite3_reset(pSql);
2135 ** Free the array allocated by explain_data_prepare().
2137 static void explain_data_delete(ShellState *p){
2138 sqlite3_free(p->aiIndent);
2139 p->aiIndent = 0;
2140 p->nIndent = 0;
2141 p->iIndent = 0;
2145 ** Disable and restore .wheretrace and .selecttrace settings.
2147 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2148 extern int sqlite3SelectTrace;
2149 static int savedSelectTrace;
2150 #endif
2151 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2152 extern int sqlite3WhereTrace;
2153 static int savedWhereTrace;
2154 #endif
2155 static void disable_debug_trace_modes(void){
2156 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2157 savedSelectTrace = sqlite3SelectTrace;
2158 sqlite3SelectTrace = 0;
2159 #endif
2160 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2161 savedWhereTrace = sqlite3WhereTrace;
2162 sqlite3WhereTrace = 0;
2163 #endif
2165 static void restore_debug_trace_modes(void){
2166 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2167 sqlite3SelectTrace = savedSelectTrace;
2168 #endif
2169 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2170 sqlite3WhereTrace = savedWhereTrace;
2171 #endif
2175 ** Run a prepared statement
2177 static void exec_prepared_stmt(
2178 ShellState *pArg, /* Pointer to ShellState */
2179 sqlite3_stmt *pStmt, /* Statment to run */
2180 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
2182 int rc;
2184 /* perform the first step. this will tell us if we
2185 ** have a result set or not and how wide it is.
2187 rc = sqlite3_step(pStmt);
2188 /* if we have a result set... */
2189 if( SQLITE_ROW == rc ){
2190 /* if we have a callback... */
2191 if( xCallback ){
2192 /* allocate space for col name ptr, value ptr, and type */
2193 int nCol = sqlite3_column_count(pStmt);
2194 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2195 if( !pData ){
2196 rc = SQLITE_NOMEM;
2197 }else{
2198 char **azCols = (char **)pData; /* Names of result columns */
2199 char **azVals = &azCols[nCol]; /* Results */
2200 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2201 int i, x;
2202 assert(sizeof(int) <= sizeof(char *));
2203 /* save off ptrs to column names */
2204 for(i=0; i<nCol; i++){
2205 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2208 /* extract the data and data types */
2209 for(i=0; i<nCol; i++){
2210 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2211 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2212 azVals[i] = "";
2213 }else{
2214 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2216 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2217 rc = SQLITE_NOMEM;
2218 break; /* from for */
2220 } /* end for */
2222 /* if data and types extracted successfully... */
2223 if( SQLITE_ROW == rc ){
2224 /* call the supplied callback with the result row data */
2225 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
2226 rc = SQLITE_ABORT;
2227 }else{
2228 rc = sqlite3_step(pStmt);
2231 } while( SQLITE_ROW == rc );
2232 sqlite3_free(pData);
2234 }else{
2236 rc = sqlite3_step(pStmt);
2237 } while( rc == SQLITE_ROW );
2243 ** Execute a statement or set of statements. Print
2244 ** any result rows/columns depending on the current mode
2245 ** set via the supplied callback.
2247 ** This is very similar to SQLite's built-in sqlite3_exec()
2248 ** function except it takes a slightly different callback
2249 ** and callback data argument.
2251 static int shell_exec(
2252 sqlite3 *db, /* An open database */
2253 const char *zSql, /* SQL to be evaluated */
2254 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
2255 /* (not the same as sqlite3_exec) */
2256 ShellState *pArg, /* Pointer to ShellState */
2257 char **pzErrMsg /* Error msg written here */
2259 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
2260 int rc = SQLITE_OK; /* Return Code */
2261 int rc2;
2262 const char *zLeftover; /* Tail of unprocessed SQL */
2264 if( pzErrMsg ){
2265 *pzErrMsg = NULL;
2268 while( zSql[0] && (SQLITE_OK == rc) ){
2269 static const char *zStmtSql;
2270 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
2271 if( SQLITE_OK != rc ){
2272 if( pzErrMsg ){
2273 *pzErrMsg = save_err_msg(db);
2275 }else{
2276 if( !pStmt ){
2277 /* this happens for a comment or white-space */
2278 zSql = zLeftover;
2279 while( IsSpace(zSql[0]) ) zSql++;
2280 continue;
2282 zStmtSql = sqlite3_sql(pStmt);
2283 if( zStmtSql==0 ) zStmtSql = "";
2284 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
2286 /* save off the prepared statment handle and reset row count */
2287 if( pArg ){
2288 pArg->pStmt = pStmt;
2289 pArg->cnt = 0;
2292 /* echo the sql statement if echo on */
2293 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
2294 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
2297 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2298 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
2299 sqlite3_stmt *pExplain;
2300 char *zEQP;
2301 disable_debug_trace_modes();
2302 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
2303 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2304 if( rc==SQLITE_OK ){
2305 while( sqlite3_step(pExplain)==SQLITE_ROW ){
2306 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
2307 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
2308 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
2309 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
2312 sqlite3_finalize(pExplain);
2313 sqlite3_free(zEQP);
2314 if( pArg->autoEQP>=2 ){
2315 /* Also do an EXPLAIN for ".eqp full" mode */
2316 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
2317 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2318 if( rc==SQLITE_OK ){
2319 pArg->cMode = MODE_Explain;
2320 explain_data_prepare(pArg, pExplain);
2321 exec_prepared_stmt(pArg, pExplain, xCallback);
2322 explain_data_delete(pArg);
2324 sqlite3_finalize(pExplain);
2325 sqlite3_free(zEQP);
2327 restore_debug_trace_modes();
2330 if( pArg ){
2331 pArg->cMode = pArg->mode;
2332 if( pArg->autoExplain
2333 && sqlite3_column_count(pStmt)==8
2334 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
2336 pArg->cMode = MODE_Explain;
2339 /* If the shell is currently in ".explain" mode, gather the extra
2340 ** data required to add indents to the output.*/
2341 if( pArg->cMode==MODE_Explain ){
2342 explain_data_prepare(pArg, pStmt);
2346 exec_prepared_stmt(pArg, pStmt, xCallback);
2347 explain_data_delete(pArg);
2349 /* print usage stats if stats on */
2350 if( pArg && pArg->statsOn ){
2351 display_stats(db, pArg, 0);
2354 /* print loop-counters if required */
2355 if( pArg && pArg->scanstatsOn ){
2356 display_scanstats(db, pArg);
2359 /* Finalize the statement just executed. If this fails, save a
2360 ** copy of the error message. Otherwise, set zSql to point to the
2361 ** next statement to execute. */
2362 rc2 = sqlite3_finalize(pStmt);
2363 if( rc!=SQLITE_NOMEM ) rc = rc2;
2364 if( rc==SQLITE_OK ){
2365 zSql = zLeftover;
2366 while( IsSpace(zSql[0]) ) zSql++;
2367 }else if( pzErrMsg ){
2368 *pzErrMsg = save_err_msg(db);
2371 /* clear saved stmt handle */
2372 if( pArg ){
2373 pArg->pStmt = NULL;
2376 } /* end while */
2378 return rc;
2382 ** Release memory previously allocated by tableColumnList().
2384 static void freeColumnList(char **azCol){
2385 int i;
2386 for(i=1; azCol[i]; i++){
2387 sqlite3_free(azCol[i]);
2389 /* azCol[0] is a static string */
2390 sqlite3_free(azCol);
2394 ** Return a list of pointers to strings which are the names of all
2395 ** columns in table zTab. The memory to hold the names is dynamically
2396 ** allocated and must be released by the caller using a subsequent call
2397 ** to freeColumnList().
2399 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
2400 ** value that needs to be preserved, then azCol[0] is filled in with the
2401 ** name of the rowid column.
2403 ** The first regular column in the table is azCol[1]. The list is terminated
2404 ** by an entry with azCol[i]==0.
2406 static char **tableColumnList(ShellState *p, const char *zTab){
2407 char **azCol = 0;
2408 sqlite3_stmt *pStmt;
2409 char *zSql;
2410 int nCol = 0;
2411 int nAlloc = 0;
2412 int nPK = 0; /* Number of PRIMARY KEY columns seen */
2413 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2414 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2415 int rc;
2417 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2418 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2419 sqlite3_free(zSql);
2420 if( rc ) return 0;
2421 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2422 if( nCol>=nAlloc-2 ){
2423 nAlloc = nAlloc*2 + nCol + 10;
2424 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2425 if( azCol==0 ){
2426 raw_printf(stderr, "Error: out of memory\n");
2427 exit(1);
2430 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2431 if( sqlite3_column_int(pStmt, 5) ){
2432 nPK++;
2433 if( nPK==1
2434 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2435 "INTEGER")==0
2437 isIPK = 1;
2438 }else{
2439 isIPK = 0;
2443 sqlite3_finalize(pStmt);
2444 azCol[0] = 0;
2445 azCol[nCol+1] = 0;
2447 /* The decision of whether or not a rowid really needs to be preserved
2448 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
2449 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
2450 ** rowids on tables where the rowid is inaccessible because there are other
2451 ** columns in the table named "rowid", "_rowid_", and "oid".
2453 if( preserveRowid && isIPK ){
2454 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
2455 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
2456 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
2457 ** ROWID aliases. To distinguish these cases, check to see if
2458 ** there is a "pk" entry in "PRAGMA index_list". There will be
2459 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
2461 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
2462 " WHERE origin='pk'", zTab);
2463 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2464 sqlite3_free(zSql);
2465 if( rc ){
2466 freeColumnList(azCol);
2467 return 0;
2469 rc = sqlite3_step(pStmt);
2470 sqlite3_finalize(pStmt);
2471 preserveRowid = rc==SQLITE_ROW;
2473 if( preserveRowid ){
2474 /* Only preserve the rowid if we can find a name to use for the
2475 ** rowid */
2476 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
2477 int i, j;
2478 for(j=0; j<3; j++){
2479 for(i=1; i<=nCol; i++){
2480 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
2482 if( i>nCol ){
2483 /* At this point, we know that azRowid[j] is not the name of any
2484 ** ordinary column in the table. Verify that azRowid[j] is a valid
2485 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
2486 ** tables will fail this last check */
2487 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
2488 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
2489 break;
2493 return azCol;
2497 ** Toggle the reverse_unordered_selects setting.
2499 static void toggleSelectOrder(sqlite3 *db){
2500 sqlite3_stmt *pStmt = 0;
2501 int iSetting = 0;
2502 char zStmt[100];
2503 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
2504 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2505 iSetting = sqlite3_column_int(pStmt, 0);
2507 sqlite3_finalize(pStmt);
2508 sqlite3_snprintf(sizeof(zStmt), zStmt,
2509 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
2510 sqlite3_exec(db, zStmt, 0, 0, 0);
2514 ** This is a different callback routine used for dumping the database.
2515 ** Each row received by this callback consists of a table name,
2516 ** the table type ("index" or "table") and SQL to create the table.
2517 ** This routine should print text sufficient to recreate the table.
2519 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
2520 int rc;
2521 const char *zTable;
2522 const char *zType;
2523 const char *zSql;
2524 ShellState *p = (ShellState *)pArg;
2526 UNUSED_PARAMETER(azNotUsed);
2527 if( nArg!=3 || azArg==0 ) return 0;
2528 zTable = azArg[0];
2529 zType = azArg[1];
2530 zSql = azArg[2];
2532 if( strcmp(zTable, "sqlite_sequence")==0 ){
2533 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
2534 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
2535 raw_printf(p->out, "ANALYZE sqlite_master;\n");
2536 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
2537 return 0;
2538 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
2539 char *zIns;
2540 if( !p->writableSchema ){
2541 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
2542 p->writableSchema = 1;
2544 zIns = sqlite3_mprintf(
2545 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
2546 "VALUES('table','%q','%q',0,'%q');",
2547 zTable, zTable, zSql);
2548 utf8_printf(p->out, "%s\n", zIns);
2549 sqlite3_free(zIns);
2550 return 0;
2551 }else{
2552 printSchemaLine(p->out, zSql, ";\n");
2555 if( strcmp(zType, "table")==0 ){
2556 ShellText sSelect;
2557 ShellText sTable;
2558 char **azCol;
2559 int i;
2560 char *savedDestTable;
2561 int savedMode;
2563 azCol = tableColumnList(p, zTable);
2564 if( azCol==0 ){
2565 p->nErr++;
2566 return 0;
2569 /* Always quote the table name, even if it appears to be pure ascii,
2570 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
2571 initText(&sTable);
2572 appendText(&sTable, zTable, quoteChar(zTable));
2573 /* If preserving the rowid, add a column list after the table name.
2574 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
2575 ** instead of the usual "INSERT INTO tab VALUES(...)".
2577 if( azCol[0] ){
2578 appendText(&sTable, "(", 0);
2579 appendText(&sTable, azCol[0], 0);
2580 for(i=1; azCol[i]; i++){
2581 appendText(&sTable, ",", 0);
2582 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
2584 appendText(&sTable, ")", 0);
2587 /* Build an appropriate SELECT statement */
2588 initText(&sSelect);
2589 appendText(&sSelect, "SELECT ", 0);
2590 if( azCol[0] ){
2591 appendText(&sSelect, azCol[0], 0);
2592 appendText(&sSelect, ",", 0);
2594 for(i=1; azCol[i]; i++){
2595 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
2596 if( azCol[i+1] ){
2597 appendText(&sSelect, ",", 0);
2600 freeColumnList(azCol);
2601 appendText(&sSelect, " FROM ", 0);
2602 appendText(&sSelect, zTable, quoteChar(zTable));
2604 savedDestTable = p->zDestTable;
2605 savedMode = p->mode;
2606 p->zDestTable = sTable.z;
2607 p->mode = p->cMode = MODE_Insert;
2608 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2609 if( (rc&0xff)==SQLITE_CORRUPT ){
2610 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2611 toggleSelectOrder(p->db);
2612 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2613 toggleSelectOrder(p->db);
2615 p->zDestTable = savedDestTable;
2616 p->mode = savedMode;
2617 freeText(&sTable);
2618 freeText(&sSelect);
2619 if( rc ) p->nErr++;
2621 return 0;
2625 ** Run zQuery. Use dump_callback() as the callback routine so that
2626 ** the contents of the query are output as SQL statements.
2628 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
2629 ** "ORDER BY rowid DESC" to the end.
2631 static int run_schema_dump_query(
2632 ShellState *p,
2633 const char *zQuery
2635 int rc;
2636 char *zErr = 0;
2637 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
2638 if( rc==SQLITE_CORRUPT ){
2639 char *zQ2;
2640 int len = strlen30(zQuery);
2641 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2642 if( zErr ){
2643 utf8_printf(p->out, "/****** %s ******/\n", zErr);
2644 sqlite3_free(zErr);
2645 zErr = 0;
2647 zQ2 = malloc( len+100 );
2648 if( zQ2==0 ) return rc;
2649 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
2650 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
2651 if( rc ){
2652 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
2653 }else{
2654 rc = SQLITE_CORRUPT;
2656 sqlite3_free(zErr);
2657 free(zQ2);
2659 return rc;
2663 ** Text of a help message
2665 static char zHelp[] =
2666 #ifndef SQLITE_OMIT_AUTHORIZATION
2667 ".auth ON|OFF Show authorizer callbacks\n"
2668 #endif
2669 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
2670 ".bail on|off Stop after hitting an error. Default OFF\n"
2671 ".binary on|off Turn binary output on or off. Default OFF\n"
2672 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
2673 ".changes on|off Show number of rows changed by SQL\n"
2674 ".check GLOB Fail if output since .testcase does not match\n"
2675 ".clone NEWDB Clone data into NEWDB from the existing database\n"
2676 ".databases List names and files of attached databases\n"
2677 ".dbinfo ?DB? Show status information about the database\n"
2678 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
2679 " If TABLE specified, only dump tables matching\n"
2680 " LIKE pattern TABLE.\n"
2681 ".echo on|off Turn command echo on or off\n"
2682 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
2683 ".exit Exit this program\n"
2684 /* Because explain mode comes on automatically now, the ".explain" mode
2685 ** is removed from the help screen. It is still supported for legacy, however */
2686 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
2687 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
2688 ".headers on|off Turn display of headers on or off\n"
2689 ".help Show this message\n"
2690 ".import FILE TABLE Import data from FILE into TABLE\n"
2691 #ifndef SQLITE_OMIT_TEST_CONTROL
2692 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
2693 #endif
2694 ".indexes ?TABLE? Show names of all indexes\n"
2695 " If TABLE specified, only show indexes for tables\n"
2696 " matching LIKE pattern TABLE.\n"
2697 #ifdef SQLITE_ENABLE_IOTRACE
2698 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
2699 #endif
2700 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
2701 ".lint OPTIONS Report potential schema issues. Options:\n"
2702 " fkey-indexes Find missing foreign key indexes\n"
2703 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2704 ".load FILE ?ENTRY? Load an extension library\n"
2705 #endif
2706 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
2707 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
2708 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
2709 " csv Comma-separated values\n"
2710 " column Left-aligned columns. (See .width)\n"
2711 " html HTML <table> code\n"
2712 " insert SQL insert statements for TABLE\n"
2713 " line One value per line\n"
2714 " list Values delimited by \"|\"\n"
2715 " quote Escape answers as for SQL\n"
2716 " tabs Tab-separated values\n"
2717 " tcl TCL list elements\n"
2718 ".nullvalue STRING Use STRING in place of NULL values\n"
2719 ".once FILENAME Output for the next SQL command only to FILENAME\n"
2720 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
2721 " The --new option starts with an empty file\n"
2722 ".output ?FILENAME? Send output to FILENAME or stdout\n"
2723 ".print STRING... Print literal STRING\n"
2724 ".prompt MAIN CONTINUE Replace the standard prompts\n"
2725 ".quit Exit this program\n"
2726 ".read FILENAME Execute SQL in FILENAME\n"
2727 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
2728 ".save FILE Write in-memory database into FILE\n"
2729 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
2730 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
2731 " Add --indent for pretty-printing\n"
2732 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
2733 ".separator COL ?ROW? Change the column separator and optionally the row\n"
2734 " separator for both the output mode and .import\n"
2735 #if defined(SQLITE_ENABLE_SESSION)
2736 ".session CMD ... Create or control sessions\n"
2737 #endif
2738 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
2739 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
2740 ".show Show the current values for various settings\n"
2741 ".stats ?on|off? Show stats or turn stats on or off\n"
2742 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
2743 ".tables ?TABLE? List names of tables\n"
2744 " If TABLE specified, only list tables matching\n"
2745 " LIKE pattern TABLE.\n"
2746 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
2747 ".timeout MS Try opening locked tables for MS milliseconds\n"
2748 ".timer on|off Turn SQL timer on or off\n"
2749 ".trace FILE|off Output each SQL statement as it is run\n"
2750 ".vfsinfo ?AUX? Information about the top-level VFS\n"
2751 ".vfslist List all available VFSes\n"
2752 ".vfsname ?AUX? Print the name of the VFS stack\n"
2753 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
2754 " Negative values right-justify\n"
2757 #if defined(SQLITE_ENABLE_SESSION)
2759 ** Print help information for the ".sessions" command
2761 void session_help(ShellState *p){
2762 raw_printf(p->out,
2763 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
2764 "If ?NAME? is omitted, the first defined session is used.\n"
2765 "Subcommands:\n"
2766 " attach TABLE Attach TABLE\n"
2767 " changeset FILE Write a changeset into FILE\n"
2768 " close Close one session\n"
2769 " enable ?BOOLEAN? Set or query the enable bit\n"
2770 " filter GLOB... Reject tables matching GLOBs\n"
2771 " indirect ?BOOLEAN? Mark or query the indirect status\n"
2772 " isempty Query whether the session is empty\n"
2773 " list List currently open session names\n"
2774 " open DB NAME Open a new session on DB\n"
2775 " patchset FILE Write a patchset into FILE\n"
2778 #endif
2781 /* Forward reference */
2782 static int process_input(ShellState *p, FILE *in);
2785 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
2786 ** and return a pointer to the buffer. The caller is responsible for freeing
2787 ** the memory.
2789 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
2790 ** read.
2792 ** For convenience, a nul-terminator byte is always appended to the data read
2793 ** from the file before the buffer is returned. This byte is not included in
2794 ** the final value of (*pnByte), if applicable.
2796 ** NULL is returned if any error is encountered. The final value of *pnByte
2797 ** is undefined in this case.
2799 static char *readFile(const char *zName, int *pnByte){
2800 FILE *in = fopen(zName, "rb");
2801 long nIn;
2802 size_t nRead;
2803 char *pBuf;
2804 if( in==0 ) return 0;
2805 fseek(in, 0, SEEK_END);
2806 nIn = ftell(in);
2807 rewind(in);
2808 pBuf = sqlite3_malloc64( nIn+1 );
2809 if( pBuf==0 ) return 0;
2810 nRead = fread(pBuf, nIn, 1, in);
2811 fclose(in);
2812 if( nRead!=1 ){
2813 sqlite3_free(pBuf);
2814 return 0;
2816 pBuf[nIn] = 0;
2817 if( pnByte ) *pnByte = nIn;
2818 return pBuf;
2821 #if defined(SQLITE_ENABLE_SESSION)
2823 ** Close a single OpenSession object and release all of its associated
2824 ** resources.
2826 static void session_close(OpenSession *pSession){
2827 int i;
2828 sqlite3session_delete(pSession->p);
2829 sqlite3_free(pSession->zName);
2830 for(i=0; i<pSession->nFilter; i++){
2831 sqlite3_free(pSession->azFilter[i]);
2833 sqlite3_free(pSession->azFilter);
2834 memset(pSession, 0, sizeof(OpenSession));
2836 #endif
2839 ** Close all OpenSession objects and release all associated resources.
2841 #if defined(SQLITE_ENABLE_SESSION)
2842 static void session_close_all(ShellState *p){
2843 int i;
2844 for(i=0; i<p->nSession; i++){
2845 session_close(&p->aSession[i]);
2847 p->nSession = 0;
2849 #else
2850 # define session_close_all(X)
2851 #endif
2854 ** Implementation of the xFilter function for an open session. Omit
2855 ** any tables named by ".session filter" but let all other table through.
2857 #if defined(SQLITE_ENABLE_SESSION)
2858 static int session_filter(void *pCtx, const char *zTab){
2859 OpenSession *pSession = (OpenSession*)pCtx;
2860 int i;
2861 for(i=0; i<pSession->nFilter; i++){
2862 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
2864 return 1;
2866 #endif
2869 ** Make sure the database is open. If it is not, then open it. If
2870 ** the database fails to open, print an error message and exit.
2872 static void open_db(ShellState *p, int keepAlive){
2873 if( p->db==0 ){
2874 sqlite3_initialize();
2875 sqlite3_open(p->zDbFilename, &p->db);
2876 globalDb = p->db;
2877 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2878 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
2879 p->zDbFilename, sqlite3_errmsg(p->db));
2880 if( keepAlive ) return;
2881 exit(1);
2883 #ifndef SQLITE_OMIT_LOAD_EXTENSION
2884 sqlite3_enable_load_extension(p->db, 1);
2885 #endif
2886 sqlite3_fileio_init(p->db, 0, 0);
2887 sqlite3_shathree_init(p->db, 0, 0);
2888 sqlite3_completion_init(p->db, 0, 0);
2889 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
2890 shellAddSchemaName, 0, 0);
2894 #if HAVE_READLINE || HAVE_EDITLINE
2896 ** Readline completion callbacks
2898 static char *readline_completion_generator(const char *text, int state){
2899 static sqlite3_stmt *pStmt = 0;
2900 char *zRet;
2901 if( state==0 ){
2902 char *zSql;
2903 sqlite3_finalize(pStmt);
2904 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2905 " FROM completion(%Q) ORDER BY 1", text);
2906 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2907 sqlite3_free(zSql);
2909 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2910 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
2911 }else{
2912 sqlite3_finalize(pStmt);
2913 pStmt = 0;
2914 zRet = 0;
2916 return zRet;
2918 static char **readline_completion(const char *zText, int iStart, int iEnd){
2919 rl_attempted_completion_over = 1;
2920 return rl_completion_matches(zText, readline_completion_generator);
2923 #elif HAVE_LINENOISE
2925 ** Linenoise completion callback
2927 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
2928 int nLine = (int)strlen(zLine);
2929 int i, iStart;
2930 sqlite3_stmt *pStmt = 0;
2931 char *zSql;
2932 char zBuf[1000];
2934 if( nLine>sizeof(zBuf)-30 ) return;
2935 if( zLine[0]=='.' ) return;
2936 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
2937 if( i==nLine-1 ) return;
2938 iStart = i+1;
2939 memcpy(zBuf, zLine, iStart);
2940 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2941 " FROM completion(%Q,%Q) ORDER BY 1",
2942 &zLine[iStart], zLine);
2943 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2944 sqlite3_free(zSql);
2945 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
2946 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2947 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
2948 int nCompletion = sqlite3_column_bytes(pStmt, 0);
2949 if( iStart+nCompletion < sizeof(zBuf)-1 ){
2950 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
2951 linenoiseAddCompletion(lc, zBuf);
2954 sqlite3_finalize(pStmt);
2956 #endif
2959 ** Do C-language style dequoting.
2961 ** \a -> alarm
2962 ** \b -> backspace
2963 ** \t -> tab
2964 ** \n -> newline
2965 ** \v -> vertical tab
2966 ** \f -> form feed
2967 ** \r -> carriage return
2968 ** \s -> space
2969 ** \" -> "
2970 ** \' -> '
2971 ** \\ -> backslash
2972 ** \NNN -> ascii character NNN in octal
2974 static void resolve_backslashes(char *z){
2975 int i, j;
2976 char c;
2977 while( *z && *z!='\\' ) z++;
2978 for(i=j=0; (c = z[i])!=0; i++, j++){
2979 if( c=='\\' && z[i+1]!=0 ){
2980 c = z[++i];
2981 if( c=='a' ){
2982 c = '\a';
2983 }else if( c=='b' ){
2984 c = '\b';
2985 }else if( c=='t' ){
2986 c = '\t';
2987 }else if( c=='n' ){
2988 c = '\n';
2989 }else if( c=='v' ){
2990 c = '\v';
2991 }else if( c=='f' ){
2992 c = '\f';
2993 }else if( c=='r' ){
2994 c = '\r';
2995 }else if( c=='"' ){
2996 c = '"';
2997 }else if( c=='\'' ){
2998 c = '\'';
2999 }else if( c=='\\' ){
3000 c = '\\';
3001 }else if( c>='0' && c<='7' ){
3002 c -= '0';
3003 if( z[i+1]>='0' && z[i+1]<='7' ){
3004 i++;
3005 c = (c<<3) + z[i] - '0';
3006 if( z[i+1]>='0' && z[i+1]<='7' ){
3007 i++;
3008 c = (c<<3) + z[i] - '0';
3013 z[j] = c;
3015 if( j<i ) z[j] = 0;
3019 ** Return the value of a hexadecimal digit. Return -1 if the input
3020 ** is not a hex digit.
3022 static int hexDigitValue(char c){
3023 if( c>='0' && c<='9' ) return c - '0';
3024 if( c>='a' && c<='f' ) return c - 'a' + 10;
3025 if( c>='A' && c<='F' ) return c - 'A' + 10;
3026 return -1;
3030 ** Interpret zArg as an integer value, possibly with suffixes.
3032 static sqlite3_int64 integerValue(const char *zArg){
3033 sqlite3_int64 v = 0;
3034 static const struct { char *zSuffix; int iMult; } aMult[] = {
3035 { "KiB", 1024 },
3036 { "MiB", 1024*1024 },
3037 { "GiB", 1024*1024*1024 },
3038 { "KB", 1000 },
3039 { "MB", 1000000 },
3040 { "GB", 1000000000 },
3041 { "K", 1000 },
3042 { "M", 1000000 },
3043 { "G", 1000000000 },
3045 int i;
3046 int isNeg = 0;
3047 if( zArg[0]=='-' ){
3048 isNeg = 1;
3049 zArg++;
3050 }else if( zArg[0]=='+' ){
3051 zArg++;
3053 if( zArg[0]=='0' && zArg[1]=='x' ){
3054 int x;
3055 zArg += 2;
3056 while( (x = hexDigitValue(zArg[0]))>=0 ){
3057 v = (v<<4) + x;
3058 zArg++;
3060 }else{
3061 while( IsDigit(zArg[0]) ){
3062 v = v*10 + zArg[0] - '0';
3063 zArg++;
3066 for(i=0; i<ArraySize(aMult); i++){
3067 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
3068 v *= aMult[i].iMult;
3069 break;
3072 return isNeg? -v : v;
3076 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
3077 ** for TRUE and FALSE. Return the integer value if appropriate.
3079 static int booleanValue(const char *zArg){
3080 int i;
3081 if( zArg[0]=='0' && zArg[1]=='x' ){
3082 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3083 }else{
3084 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
3086 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
3087 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
3088 return 1;
3090 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
3091 return 0;
3093 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3094 zArg);
3095 return 0;
3099 ** Set or clear a shell flag according to a boolean value.
3101 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3102 if( booleanValue(zArg) ){
3103 ShellSetFlag(p, mFlag);
3104 }else{
3105 ShellClearFlag(p, mFlag);
3110 ** Close an output file, assuming it is not stderr or stdout
3112 static void output_file_close(FILE *f){
3113 if( f && f!=stdout && f!=stderr ) fclose(f);
3117 ** Try to open an output file. The names "stdout" and "stderr" are
3118 ** recognized and do the right thing. NULL is returned if the output
3119 ** filename is "off".
3121 static FILE *output_file_open(const char *zFile){
3122 FILE *f;
3123 if( strcmp(zFile,"stdout")==0 ){
3124 f = stdout;
3125 }else if( strcmp(zFile, "stderr")==0 ){
3126 f = stderr;
3127 }else if( strcmp(zFile, "off")==0 ){
3128 f = 0;
3129 }else{
3130 f = fopen(zFile, "wb");
3131 if( f==0 ){
3132 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
3135 return f;
3138 #if !defined(SQLITE_UNTESTABLE)
3139 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3141 ** A routine for handling output from sqlite3_trace().
3143 static int sql_trace_callback(
3144 unsigned mType,
3145 void *pArg,
3146 void *pP,
3147 void *pX
3149 FILE *f = (FILE*)pArg;
3150 UNUSED_PARAMETER(mType);
3151 UNUSED_PARAMETER(pP);
3152 if( f ){
3153 const char *z = (const char*)pX;
3154 int i = (int)strlen(z);
3155 while( i>0 && z[i-1]==';' ){ i--; }
3156 utf8_printf(f, "%.*s;\n", i, z);
3158 return 0;
3160 #endif
3161 #endif
3164 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
3165 ** a useful spot to set a debugger breakpoint.
3167 static void test_breakpoint(void){
3168 static int nCall = 0;
3169 nCall++;
3173 ** An object used to read a CSV and other files for import.
3175 typedef struct ImportCtx ImportCtx;
3176 struct ImportCtx {
3177 const char *zFile; /* Name of the input file */
3178 FILE *in; /* Read the CSV text from this input stream */
3179 char *z; /* Accumulated text for a field */
3180 int n; /* Number of bytes in z */
3181 int nAlloc; /* Space allocated for z[] */
3182 int nLine; /* Current line number */
3183 int bNotFirst; /* True if one or more bytes already read */
3184 int cTerm; /* Character that terminated the most recent field */
3185 int cColSep; /* The column separator character. (Usually ",") */
3186 int cRowSep; /* The row separator character. (Usually "\n") */
3189 /* Append a single byte to z[] */
3190 static void import_append_char(ImportCtx *p, int c){
3191 if( p->n+1>=p->nAlloc ){
3192 p->nAlloc += p->nAlloc + 100;
3193 p->z = sqlite3_realloc64(p->z, p->nAlloc);
3194 if( p->z==0 ){
3195 raw_printf(stderr, "out of memory\n");
3196 exit(1);
3199 p->z[p->n++] = (char)c;
3202 /* Read a single field of CSV text. Compatible with rfc4180 and extended
3203 ** with the option of having a separator other than ",".
3205 ** + Input comes from p->in.
3206 ** + Store results in p->z of length p->n. Space to hold p->z comes
3207 ** from sqlite3_malloc64().
3208 ** + Use p->cSep as the column separator. The default is ",".
3209 ** + Use p->rSep as the row separator. The default is "\n".
3210 ** + Keep track of the line number in p->nLine.
3211 ** + Store the character that terminates the field in p->cTerm. Store
3212 ** EOF on end-of-file.
3213 ** + Report syntax errors on stderr
3215 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
3216 int c;
3217 int cSep = p->cColSep;
3218 int rSep = p->cRowSep;
3219 p->n = 0;
3220 c = fgetc(p->in);
3221 if( c==EOF || seenInterrupt ){
3222 p->cTerm = EOF;
3223 return 0;
3225 if( c=='"' ){
3226 int pc, ppc;
3227 int startLine = p->nLine;
3228 int cQuote = c;
3229 pc = ppc = 0;
3230 while( 1 ){
3231 c = fgetc(p->in);
3232 if( c==rSep ) p->nLine++;
3233 if( c==cQuote ){
3234 if( pc==cQuote ){
3235 pc = 0;
3236 continue;
3239 if( (c==cSep && pc==cQuote)
3240 || (c==rSep && pc==cQuote)
3241 || (c==rSep && pc=='\r' && ppc==cQuote)
3242 || (c==EOF && pc==cQuote)
3244 do{ p->n--; }while( p->z[p->n]!=cQuote );
3245 p->cTerm = c;
3246 break;
3248 if( pc==cQuote && c!='\r' ){
3249 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
3250 p->zFile, p->nLine, cQuote);
3252 if( c==EOF ){
3253 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
3254 p->zFile, startLine, cQuote);
3255 p->cTerm = c;
3256 break;
3258 import_append_char(p, c);
3259 ppc = pc;
3260 pc = c;
3262 }else{
3263 /* If this is the first field being parsed and it begins with the
3264 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
3265 if( (c&0xff)==0xef && p->bNotFirst==0 ){
3266 import_append_char(p, c);
3267 c = fgetc(p->in);
3268 if( (c&0xff)==0xbb ){
3269 import_append_char(p, c);
3270 c = fgetc(p->in);
3271 if( (c&0xff)==0xbf ){
3272 p->bNotFirst = 1;
3273 p->n = 0;
3274 return csv_read_one_field(p);
3278 while( c!=EOF && c!=cSep && c!=rSep ){
3279 import_append_char(p, c);
3280 c = fgetc(p->in);
3282 if( c==rSep ){
3283 p->nLine++;
3284 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
3286 p->cTerm = c;
3288 if( p->z ) p->z[p->n] = 0;
3289 p->bNotFirst = 1;
3290 return p->z;
3293 /* Read a single field of ASCII delimited text.
3295 ** + Input comes from p->in.
3296 ** + Store results in p->z of length p->n. Space to hold p->z comes
3297 ** from sqlite3_malloc64().
3298 ** + Use p->cSep as the column separator. The default is "\x1F".
3299 ** + Use p->rSep as the row separator. The default is "\x1E".
3300 ** + Keep track of the row number in p->nLine.
3301 ** + Store the character that terminates the field in p->cTerm. Store
3302 ** EOF on end-of-file.
3303 ** + Report syntax errors on stderr
3305 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
3306 int c;
3307 int cSep = p->cColSep;
3308 int rSep = p->cRowSep;
3309 p->n = 0;
3310 c = fgetc(p->in);
3311 if( c==EOF || seenInterrupt ){
3312 p->cTerm = EOF;
3313 return 0;
3315 while( c!=EOF && c!=cSep && c!=rSep ){
3316 import_append_char(p, c);
3317 c = fgetc(p->in);
3319 if( c==rSep ){
3320 p->nLine++;
3322 p->cTerm = c;
3323 if( p->z ) p->z[p->n] = 0;
3324 return p->z;
3328 ** Try to transfer data for table zTable. If an error is seen while
3329 ** moving forward, try to go backwards. The backwards movement won't
3330 ** work for WITHOUT ROWID tables.
3332 static void tryToCloneData(
3333 ShellState *p,
3334 sqlite3 *newDb,
3335 const char *zTable
3337 sqlite3_stmt *pQuery = 0;
3338 sqlite3_stmt *pInsert = 0;
3339 char *zQuery = 0;
3340 char *zInsert = 0;
3341 int rc;
3342 int i, j, n;
3343 int nTable = (int)strlen(zTable);
3344 int k = 0;
3345 int cnt = 0;
3346 const int spinRate = 10000;
3348 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
3349 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3350 if( rc ){
3351 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3352 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3353 zQuery);
3354 goto end_data_xfer;
3356 n = sqlite3_column_count(pQuery);
3357 zInsert = sqlite3_malloc64(200 + nTable + n*3);
3358 if( zInsert==0 ){
3359 raw_printf(stderr, "out of memory\n");
3360 goto end_data_xfer;
3362 sqlite3_snprintf(200+nTable,zInsert,
3363 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
3364 i = (int)strlen(zInsert);
3365 for(j=1; j<n; j++){
3366 memcpy(zInsert+i, ",?", 2);
3367 i += 2;
3369 memcpy(zInsert+i, ");", 3);
3370 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
3371 if( rc ){
3372 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3373 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
3374 zQuery);
3375 goto end_data_xfer;
3377 for(k=0; k<2; k++){
3378 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3379 for(i=0; i<n; i++){
3380 switch( sqlite3_column_type(pQuery, i) ){
3381 case SQLITE_NULL: {
3382 sqlite3_bind_null(pInsert, i+1);
3383 break;
3385 case SQLITE_INTEGER: {
3386 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
3387 break;
3389 case SQLITE_FLOAT: {
3390 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
3391 break;
3393 case SQLITE_TEXT: {
3394 sqlite3_bind_text(pInsert, i+1,
3395 (const char*)sqlite3_column_text(pQuery,i),
3396 -1, SQLITE_STATIC);
3397 break;
3399 case SQLITE_BLOB: {
3400 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
3401 sqlite3_column_bytes(pQuery,i),
3402 SQLITE_STATIC);
3403 break;
3406 } /* End for */
3407 rc = sqlite3_step(pInsert);
3408 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
3409 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
3410 sqlite3_errmsg(newDb));
3412 sqlite3_reset(pInsert);
3413 cnt++;
3414 if( (cnt%spinRate)==0 ){
3415 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
3416 fflush(stdout);
3418 } /* End while */
3419 if( rc==SQLITE_DONE ) break;
3420 sqlite3_finalize(pQuery);
3421 sqlite3_free(zQuery);
3422 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
3423 zTable);
3424 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3425 if( rc ){
3426 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
3427 break;
3429 } /* End for(k=0...) */
3431 end_data_xfer:
3432 sqlite3_finalize(pQuery);
3433 sqlite3_finalize(pInsert);
3434 sqlite3_free(zQuery);
3435 sqlite3_free(zInsert);
3440 ** Try to transfer all rows of the schema that match zWhere. For
3441 ** each row, invoke xForEach() on the object defined by that row.
3442 ** If an error is encountered while moving forward through the
3443 ** sqlite_master table, try again moving backwards.
3445 static void tryToCloneSchema(
3446 ShellState *p,
3447 sqlite3 *newDb,
3448 const char *zWhere,
3449 void (*xForEach)(ShellState*,sqlite3*,const char*)
3451 sqlite3_stmt *pQuery = 0;
3452 char *zQuery = 0;
3453 int rc;
3454 const unsigned char *zName;
3455 const unsigned char *zSql;
3456 char *zErrMsg = 0;
3458 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3459 " WHERE %s", zWhere);
3460 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3461 if( rc ){
3462 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3463 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3464 zQuery);
3465 goto end_schema_xfer;
3467 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3468 zName = sqlite3_column_text(pQuery, 0);
3469 zSql = sqlite3_column_text(pQuery, 1);
3470 printf("%s... ", zName); fflush(stdout);
3471 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3472 if( zErrMsg ){
3473 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3474 sqlite3_free(zErrMsg);
3475 zErrMsg = 0;
3477 if( xForEach ){
3478 xForEach(p, newDb, (const char*)zName);
3480 printf("done\n");
3482 if( rc!=SQLITE_DONE ){
3483 sqlite3_finalize(pQuery);
3484 sqlite3_free(zQuery);
3485 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3486 " WHERE %s ORDER BY rowid DESC", zWhere);
3487 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3488 if( rc ){
3489 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3490 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3491 zQuery);
3492 goto end_schema_xfer;
3494 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3495 zName = sqlite3_column_text(pQuery, 0);
3496 zSql = sqlite3_column_text(pQuery, 1);
3497 printf("%s... ", zName); fflush(stdout);
3498 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3499 if( zErrMsg ){
3500 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3501 sqlite3_free(zErrMsg);
3502 zErrMsg = 0;
3504 if( xForEach ){
3505 xForEach(p, newDb, (const char*)zName);
3507 printf("done\n");
3510 end_schema_xfer:
3511 sqlite3_finalize(pQuery);
3512 sqlite3_free(zQuery);
3516 ** Open a new database file named "zNewDb". Try to recover as much information
3517 ** as possible out of the main database (which might be corrupt) and write it
3518 ** into zNewDb.
3520 static void tryToClone(ShellState *p, const char *zNewDb){
3521 int rc;
3522 sqlite3 *newDb = 0;
3523 if( access(zNewDb,0)==0 ){
3524 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
3525 return;
3527 rc = sqlite3_open(zNewDb, &newDb);
3528 if( rc ){
3529 utf8_printf(stderr, "Cannot create output database: %s\n",
3530 sqlite3_errmsg(newDb));
3531 }else{
3532 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
3533 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
3534 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
3535 tryToCloneSchema(p, newDb, "type!='table'", 0);
3536 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
3537 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
3539 sqlite3_close(newDb);
3543 ** Change the output file back to stdout
3545 static void output_reset(ShellState *p){
3546 if( p->outfile[0]=='|' ){
3547 #ifndef SQLITE_OMIT_POPEN
3548 pclose(p->out);
3549 #endif
3550 }else{
3551 output_file_close(p->out);
3553 p->outfile[0] = 0;
3554 p->out = stdout;
3558 ** Run an SQL command and return the single integer result.
3560 static int db_int(ShellState *p, const char *zSql){
3561 sqlite3_stmt *pStmt;
3562 int res = 0;
3563 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3564 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
3565 res = sqlite3_column_int(pStmt,0);
3567 sqlite3_finalize(pStmt);
3568 return res;
3572 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
3574 static unsigned int get2byteInt(unsigned char *a){
3575 return (a[0]<<8) + a[1];
3577 static unsigned int get4byteInt(unsigned char *a){
3578 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
3582 ** Implementation of the ".info" command.
3584 ** Return 1 on error, 2 to exit, and 0 otherwise.
3586 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
3587 static const struct { const char *zName; int ofst; } aField[] = {
3588 { "file change counter:", 24 },
3589 { "database page count:", 28 },
3590 { "freelist page count:", 36 },
3591 { "schema cookie:", 40 },
3592 { "schema format:", 44 },
3593 { "default cache size:", 48 },
3594 { "autovacuum top root:", 52 },
3595 { "incremental vacuum:", 64 },
3596 { "text encoding:", 56 },
3597 { "user version:", 60 },
3598 { "application id:", 68 },
3599 { "software version:", 96 },
3601 static const struct { const char *zName; const char *zSql; } aQuery[] = {
3602 { "number of tables:",
3603 "SELECT count(*) FROM %s WHERE type='table'" },
3604 { "number of indexes:",
3605 "SELECT count(*) FROM %s WHERE type='index'" },
3606 { "number of triggers:",
3607 "SELECT count(*) FROM %s WHERE type='trigger'" },
3608 { "number of views:",
3609 "SELECT count(*) FROM %s WHERE type='view'" },
3610 { "schema size:",
3611 "SELECT total(length(sql)) FROM %s" },
3613 sqlite3_file *pFile = 0;
3614 int i;
3615 char *zSchemaTab;
3616 char *zDb = nArg>=2 ? azArg[1] : "main";
3617 unsigned char aHdr[100];
3618 open_db(p, 0);
3619 if( p->db==0 ) return 1;
3620 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
3621 if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
3622 return 1;
3624 i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
3625 if( i!=SQLITE_OK ){
3626 raw_printf(stderr, "unable to read database header\n");
3627 return 1;
3629 i = get2byteInt(aHdr+16);
3630 if( i==1 ) i = 65536;
3631 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
3632 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
3633 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
3634 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
3635 for(i=0; i<ArraySize(aField); i++){
3636 int ofst = aField[i].ofst;
3637 unsigned int val = get4byteInt(aHdr + ofst);
3638 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
3639 switch( ofst ){
3640 case 56: {
3641 if( val==1 ) raw_printf(p->out, " (utf8)");
3642 if( val==2 ) raw_printf(p->out, " (utf16le)");
3643 if( val==3 ) raw_printf(p->out, " (utf16be)");
3646 raw_printf(p->out, "\n");
3648 if( zDb==0 ){
3649 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
3650 }else if( strcmp(zDb,"temp")==0 ){
3651 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
3652 }else{
3653 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
3655 for(i=0; i<ArraySize(aQuery); i++){
3656 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
3657 int val = db_int(p, zSql);
3658 sqlite3_free(zSql);
3659 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
3661 sqlite3_free(zSchemaTab);
3662 return 0;
3666 ** Print the current sqlite3_errmsg() value to stderr and return 1.
3668 static int shellDatabaseError(sqlite3 *db){
3669 const char *zErr = sqlite3_errmsg(db);
3670 utf8_printf(stderr, "Error: %s\n", zErr);
3671 return 1;
3675 ** Print an out-of-memory message to stderr and return 1.
3677 static int shellNomemError(void){
3678 raw_printf(stderr, "Error: out of memory\n");
3679 return 1;
3683 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
3684 ** if they match and FALSE (0) if they do not match.
3686 ** Globbing rules:
3688 ** '*' Matches any sequence of zero or more characters.
3690 ** '?' Matches exactly one character.
3692 ** [...] Matches one character from the enclosed list of
3693 ** characters.
3695 ** [^...] Matches one character not in the enclosed list.
3697 ** '#' Matches any sequence of one or more digits with an
3698 ** optional + or - sign in front
3700 ** ' ' Any span of whitespace matches any other span of
3701 ** whitespace.
3703 ** Extra whitespace at the end of z[] is ignored.
3705 static int testcase_glob(const char *zGlob, const char *z){
3706 int c, c2;
3707 int invert;
3708 int seen;
3710 while( (c = (*(zGlob++)))!=0 ){
3711 if( IsSpace(c) ){
3712 if( !IsSpace(*z) ) return 0;
3713 while( IsSpace(*zGlob) ) zGlob++;
3714 while( IsSpace(*z) ) z++;
3715 }else if( c=='*' ){
3716 while( (c=(*(zGlob++))) == '*' || c=='?' ){
3717 if( c=='?' && (*(z++))==0 ) return 0;
3719 if( c==0 ){
3720 return 1;
3721 }else if( c=='[' ){
3722 while( *z && testcase_glob(zGlob-1,z)==0 ){
3723 z++;
3725 return (*z)!=0;
3727 while( (c2 = (*(z++)))!=0 ){
3728 while( c2!=c ){
3729 c2 = *(z++);
3730 if( c2==0 ) return 0;
3732 if( testcase_glob(zGlob,z) ) return 1;
3734 return 0;
3735 }else if( c=='?' ){
3736 if( (*(z++))==0 ) return 0;
3737 }else if( c=='[' ){
3738 int prior_c = 0;
3739 seen = 0;
3740 invert = 0;
3741 c = *(z++);
3742 if( c==0 ) return 0;
3743 c2 = *(zGlob++);
3744 if( c2=='^' ){
3745 invert = 1;
3746 c2 = *(zGlob++);
3748 if( c2==']' ){
3749 if( c==']' ) seen = 1;
3750 c2 = *(zGlob++);
3752 while( c2 && c2!=']' ){
3753 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
3754 c2 = *(zGlob++);
3755 if( c>=prior_c && c<=c2 ) seen = 1;
3756 prior_c = 0;
3757 }else{
3758 if( c==c2 ){
3759 seen = 1;
3761 prior_c = c2;
3763 c2 = *(zGlob++);
3765 if( c2==0 || (seen ^ invert)==0 ) return 0;
3766 }else if( c=='#' ){
3767 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
3768 if( !IsDigit(z[0]) ) return 0;
3769 z++;
3770 while( IsDigit(z[0]) ){ z++; }
3771 }else{
3772 if( c!=(*(z++)) ) return 0;
3775 while( IsSpace(*z) ){ z++; }
3776 return *z==0;
3781 ** Compare the string as a command-line option with either one or two
3782 ** initial "-" characters.
3784 static int optionMatch(const char *zStr, const char *zOpt){
3785 if( zStr[0]!='-' ) return 0;
3786 zStr++;
3787 if( zStr[0]=='-' ) zStr++;
3788 return strcmp(zStr, zOpt)==0;
3792 ** Delete a file.
3794 int shellDeleteFile(const char *zFilename){
3795 int rc;
3796 #ifdef _WIN32
3797 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
3798 rc = _wunlink(z);
3799 sqlite3_free(z);
3800 #else
3801 rc = unlink(zFilename);
3802 #endif
3803 return rc;
3808 ** The implementation of SQL scalar function fkey_collate_clause(), used
3809 ** by the ".lint fkey-indexes" command. This scalar function is always
3810 ** called with four arguments - the parent table name, the parent column name,
3811 ** the child table name and the child column name.
3813 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
3815 ** If either of the named tables or columns do not exist, this function
3816 ** returns an empty string. An empty string is also returned if both tables
3817 ** and columns exist but have the same default collation sequence. Or,
3818 ** if both exist but the default collation sequences are different, this
3819 ** function returns the string " COLLATE <parent-collation>", where
3820 ** <parent-collation> is the default collation sequence of the parent column.
3822 static void shellFkeyCollateClause(
3823 sqlite3_context *pCtx,
3824 int nVal,
3825 sqlite3_value **apVal
3827 sqlite3 *db = sqlite3_context_db_handle(pCtx);
3828 const char *zParent;
3829 const char *zParentCol;
3830 const char *zParentSeq;
3831 const char *zChild;
3832 const char *zChildCol;
3833 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
3834 int rc;
3836 assert( nVal==4 );
3837 zParent = (const char*)sqlite3_value_text(apVal[0]);
3838 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
3839 zChild = (const char*)sqlite3_value_text(apVal[2]);
3840 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
3842 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
3843 rc = sqlite3_table_column_metadata(
3844 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
3846 if( rc==SQLITE_OK ){
3847 rc = sqlite3_table_column_metadata(
3848 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
3852 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
3853 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
3854 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
3855 sqlite3_free(z);
3861 ** The implementation of dot-command ".lint fkey-indexes".
3863 static int lintFkeyIndexes(
3864 ShellState *pState, /* Current shell tool state */
3865 char **azArg, /* Array of arguments passed to dot command */
3866 int nArg /* Number of entries in azArg[] */
3868 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
3869 FILE *out = pState->out; /* Stream to write non-error output to */
3870 int bVerbose = 0; /* If -verbose is present */
3871 int bGroupByParent = 0; /* If -groupbyparent is present */
3872 int i; /* To iterate through azArg[] */
3873 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
3874 int rc; /* Return code */
3875 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
3878 ** This SELECT statement returns one row for each foreign key constraint
3879 ** in the schema of the main database. The column values are:
3881 ** 0. The text of an SQL statement similar to:
3883 ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?"
3885 ** This is the same SELECT that the foreign keys implementation needs
3886 ** to run internally on child tables. If there is an index that can
3887 ** be used to optimize this query, then it can also be used by the FK
3888 ** implementation to optimize DELETE or UPDATE statements on the parent
3889 ** table.
3891 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
3892 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
3893 ** contains an index that can be used to optimize the query.
3895 ** 2. Human readable text that describes the child table and columns. e.g.
3897 ** "child_table(child_key1, child_key2)"
3899 ** 3. Human readable text that describes the parent table and columns. e.g.
3901 ** "parent_table(parent_key1, parent_key2)"
3903 ** 4. A full CREATE INDEX statement for an index that could be used to
3904 ** optimize DELETE or UPDATE statements on the parent table. e.g.
3906 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
3908 ** 5. The name of the parent table.
3910 ** These six values are used by the C logic below to generate the report.
3912 const char *zSql =
3913 "SELECT "
3914 " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
3915 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
3916 " || fkey_collate_clause("
3917 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
3918 ", "
3919 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
3920 " || group_concat('*=?', ' AND ') || ')'"
3921 ", "
3922 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
3923 ", "
3924 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
3925 ", "
3926 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
3927 " || ' ON ' || quote(s.name) || '('"
3928 " || group_concat(quote(f.[from]) ||"
3929 " fkey_collate_clause("
3930 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
3931 " || ');'"
3932 ", "
3933 " f.[table] "
3934 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
3935 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
3936 "GROUP BY s.name, f.id "
3937 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
3939 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
3941 for(i=2; i<nArg; i++){
3942 int n = (int)strlen(azArg[i]);
3943 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
3944 bVerbose = 1;
3946 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
3947 bGroupByParent = 1;
3948 zIndent = " ";
3950 else{
3951 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
3952 azArg[0], azArg[1]
3954 return SQLITE_ERROR;
3958 /* Register the fkey_collate_clause() SQL function */
3959 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
3960 0, shellFkeyCollateClause, 0, 0
3964 if( rc==SQLITE_OK ){
3965 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
3967 if( rc==SQLITE_OK ){
3968 sqlite3_bind_int(pSql, 1, bGroupByParent);
3971 if( rc==SQLITE_OK ){
3972 int rc2;
3973 char *zPrev = 0;
3974 while( SQLITE_ROW==sqlite3_step(pSql) ){
3975 int res = -1;
3976 sqlite3_stmt *pExplain = 0;
3977 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
3978 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
3979 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
3980 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
3981 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
3982 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
3984 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3985 if( rc!=SQLITE_OK ) break;
3986 if( SQLITE_ROW==sqlite3_step(pExplain) ){
3987 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
3988 res = (
3989 0==sqlite3_strglob(zGlob, zPlan)
3990 || 0==sqlite3_strglob(zGlobIPK, zPlan)
3993 rc = sqlite3_finalize(pExplain);
3994 if( rc!=SQLITE_OK ) break;
3996 if( res<0 ){
3997 raw_printf(stderr, "Error: internal error");
3998 break;
3999 }else{
4000 if( bGroupByParent
4001 && (bVerbose || res==0)
4002 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
4004 raw_printf(out, "-- Parent table %s\n", zParent);
4005 sqlite3_free(zPrev);
4006 zPrev = sqlite3_mprintf("%s", zParent);
4009 if( res==0 ){
4010 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
4011 }else if( bVerbose ){
4012 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
4013 zIndent, zFrom, zTarget
4018 sqlite3_free(zPrev);
4020 if( rc!=SQLITE_OK ){
4021 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4024 rc2 = sqlite3_finalize(pSql);
4025 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
4026 rc = rc2;
4027 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4029 }else{
4030 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4033 return rc;
4037 ** Implementation of ".lint" dot command.
4039 static int lintDotCommand(
4040 ShellState *pState, /* Current shell tool state */
4041 char **azArg, /* Array of arguments passed to dot command */
4042 int nArg /* Number of entries in azArg[] */
4044 int n;
4045 n = (nArg>=2 ? (int)strlen(azArg[1]) : 0);
4046 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
4047 return lintFkeyIndexes(pState, azArg, nArg);
4049 usage:
4050 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
4051 raw_printf(stderr, "Where sub-commands are:\n");
4052 raw_printf(stderr, " fkey-indexes\n");
4053 return SQLITE_ERROR;
4058 ** If an input line begins with "." then invoke this routine to
4059 ** process that line.
4061 ** Return 1 on error, 2 to exit, and 0 otherwise.
4063 static int do_meta_command(char *zLine, ShellState *p){
4064 int h = 1;
4065 int nArg = 0;
4066 int n, c;
4067 int rc = 0;
4068 char *azArg[50];
4070 /* Parse the input line into tokens.
4072 while( zLine[h] && nArg<ArraySize(azArg) ){
4073 while( IsSpace(zLine[h]) ){ h++; }
4074 if( zLine[h]==0 ) break;
4075 if( zLine[h]=='\'' || zLine[h]=='"' ){
4076 int delim = zLine[h++];
4077 azArg[nArg++] = &zLine[h];
4078 while( zLine[h] && zLine[h]!=delim ){
4079 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
4080 h++;
4082 if( zLine[h]==delim ){
4083 zLine[h++] = 0;
4085 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
4086 }else{
4087 azArg[nArg++] = &zLine[h];
4088 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
4089 if( zLine[h] ) zLine[h++] = 0;
4090 resolve_backslashes(azArg[nArg-1]);
4094 /* Process the input line.
4096 if( nArg==0 ) return 0; /* no tokens, no error */
4097 n = strlen30(azArg[0]);
4098 c = azArg[0][0];
4100 #ifndef SQLITE_OMIT_AUTHORIZATION
4101 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
4102 if( nArg!=2 ){
4103 raw_printf(stderr, "Usage: .auth ON|OFF\n");
4104 rc = 1;
4105 goto meta_command_exit;
4107 open_db(p, 0);
4108 if( booleanValue(azArg[1]) ){
4109 sqlite3_set_authorizer(p->db, shellAuth, p);
4110 }else{
4111 sqlite3_set_authorizer(p->db, 0, 0);
4113 }else
4114 #endif
4116 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
4117 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
4119 const char *zDestFile = 0;
4120 const char *zDb = 0;
4121 sqlite3 *pDest;
4122 sqlite3_backup *pBackup;
4123 int j;
4124 for(j=1; j<nArg; j++){
4125 const char *z = azArg[j];
4126 if( z[0]=='-' ){
4127 while( z[0]=='-' ) z++;
4128 /* No options to process at this time */
4130 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
4131 return 1;
4133 }else if( zDestFile==0 ){
4134 zDestFile = azArg[j];
4135 }else if( zDb==0 ){
4136 zDb = zDestFile;
4137 zDestFile = azArg[j];
4138 }else{
4139 raw_printf(stderr, "too many arguments to .backup\n");
4140 return 1;
4143 if( zDestFile==0 ){
4144 raw_printf(stderr, "missing FILENAME argument on .backup\n");
4145 return 1;
4147 if( zDb==0 ) zDb = "main";
4148 rc = sqlite3_open(zDestFile, &pDest);
4149 if( rc!=SQLITE_OK ){
4150 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
4151 sqlite3_close(pDest);
4152 return 1;
4154 open_db(p, 0);
4155 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
4156 if( pBackup==0 ){
4157 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4158 sqlite3_close(pDest);
4159 return 1;
4161 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
4162 sqlite3_backup_finish(pBackup);
4163 if( rc==SQLITE_DONE ){
4164 rc = 0;
4165 }else{
4166 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4167 rc = 1;
4169 sqlite3_close(pDest);
4170 }else
4172 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
4173 if( nArg==2 ){
4174 bail_on_error = booleanValue(azArg[1]);
4175 }else{
4176 raw_printf(stderr, "Usage: .bail on|off\n");
4177 rc = 1;
4179 }else
4181 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
4182 if( nArg==2 ){
4183 if( booleanValue(azArg[1]) ){
4184 setBinaryMode(p->out, 1);
4185 }else{
4186 setTextMode(p->out, 1);
4188 }else{
4189 raw_printf(stderr, "Usage: .binary on|off\n");
4190 rc = 1;
4192 }else
4194 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
4195 if( nArg==2 ){
4196 #if defined(_WIN32) || defined(WIN32)
4197 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
4198 rc = !SetCurrentDirectoryW(z);
4199 sqlite3_free(z);
4200 #else
4201 rc = chdir(azArg[1]);
4202 #endif
4203 if( rc ){
4204 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
4205 rc = 1;
4207 }else{
4208 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
4209 rc = 1;
4211 }else
4213 /* The undocumented ".breakpoint" command causes a call to the no-op
4214 ** routine named test_breakpoint().
4216 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
4217 test_breakpoint();
4218 }else
4220 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
4221 if( nArg==2 ){
4222 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
4223 }else{
4224 raw_printf(stderr, "Usage: .changes on|off\n");
4225 rc = 1;
4227 }else
4229 /* Cancel output redirection, if it is currently set (by .testcase)
4230 ** Then read the content of the testcase-out.txt file and compare against
4231 ** azArg[1]. If there are differences, report an error and exit.
4233 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
4234 char *zRes = 0;
4235 output_reset(p);
4236 if( nArg!=2 ){
4237 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
4238 rc = 2;
4239 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
4240 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
4241 rc = 2;
4242 }else if( testcase_glob(azArg[1],zRes)==0 ){
4243 utf8_printf(stderr,
4244 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
4245 p->zTestcase, azArg[1], zRes);
4246 rc = 2;
4247 }else{
4248 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
4249 p->nCheck++;
4251 sqlite3_free(zRes);
4252 }else
4254 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
4255 if( nArg==2 ){
4256 tryToClone(p, azArg[1]);
4257 }else{
4258 raw_printf(stderr, "Usage: .clone FILENAME\n");
4259 rc = 1;
4261 }else
4263 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
4264 ShellState data;
4265 char *zErrMsg = 0;
4266 open_db(p, 0);
4267 memcpy(&data, p, sizeof(data));
4268 data.showHeader = 0;
4269 data.cMode = data.mode = MODE_List;
4270 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
4271 data.cnt = 0;
4272 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
4273 callback, &data, &zErrMsg);
4274 if( zErrMsg ){
4275 utf8_printf(stderr,"Error: %s\n", zErrMsg);
4276 sqlite3_free(zErrMsg);
4277 rc = 1;
4279 }else
4281 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
4282 rc = shell_dbinfo_command(p, nArg, azArg);
4283 }else
4285 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4286 const char *zLike = 0;
4287 int i;
4288 int savedShowHeader = p->showHeader;
4289 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
4290 for(i=1; i<nArg; i++){
4291 if( azArg[i][0]=='-' ){
4292 const char *z = azArg[i]+1;
4293 if( z[0]=='-' ) z++;
4294 if( strcmp(z,"preserve-rowids")==0 ){
4295 #ifdef SQLITE_OMIT_VIRTUALTABLE
4296 raw_printf(stderr, "The --preserve-rowids option is not compatible"
4297 " with SQLITE_OMIT_VIRTUALTABLE\n");
4298 rc = 1;
4299 goto meta_command_exit;
4300 #else
4301 ShellSetFlag(p, SHFLG_PreserveRowid);
4302 #endif
4303 }else
4304 if( strcmp(z,"newlines")==0 ){
4305 ShellSetFlag(p, SHFLG_Newlines);
4306 }else
4308 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4309 rc = 1;
4310 goto meta_command_exit;
4312 }else if( zLike ){
4313 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
4314 "?--newlines? ?LIKE-PATTERN?\n");
4315 rc = 1;
4316 goto meta_command_exit;
4317 }else{
4318 zLike = azArg[i];
4321 open_db(p, 0);
4322 /* When playing back a "dump", the content might appear in an order
4323 ** which causes immediate foreign key constraints to be violated.
4324 ** So disable foreign-key constraint enforcement to prevent problems. */
4325 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
4326 raw_printf(p->out, "BEGIN TRANSACTION;\n");
4327 p->writableSchema = 0;
4328 p->showHeader = 0;
4329 /* Set writable_schema=ON since doing so forces SQLite to initialize
4330 ** as much of the schema as it can even if the sqlite_master table is
4331 ** corrupt. */
4332 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
4333 p->nErr = 0;
4334 if( zLike==0 ){
4335 run_schema_dump_query(p,
4336 "SELECT name, type, sql FROM sqlite_master "
4337 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
4339 run_schema_dump_query(p,
4340 "SELECT name, type, sql FROM sqlite_master "
4341 "WHERE name=='sqlite_sequence'"
4343 run_table_dump_query(p,
4344 "SELECT sql FROM sqlite_master "
4345 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
4347 }else{
4348 char *zSql;
4349 zSql = sqlite3_mprintf(
4350 "SELECT name, type, sql FROM sqlite_master "
4351 "WHERE tbl_name LIKE %Q AND type=='table'"
4352 " AND sql NOT NULL", zLike);
4353 run_schema_dump_query(p,zSql);
4354 sqlite3_free(zSql);
4355 zSql = sqlite3_mprintf(
4356 "SELECT sql FROM sqlite_master "
4357 "WHERE sql NOT NULL"
4358 " AND type IN ('index','trigger','view')"
4359 " AND tbl_name LIKE %Q", zLike);
4360 run_table_dump_query(p, zSql, 0);
4361 sqlite3_free(zSql);
4363 if( p->writableSchema ){
4364 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
4365 p->writableSchema = 0;
4367 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4368 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
4369 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
4370 p->showHeader = savedShowHeader;
4371 }else
4373 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
4374 if( nArg==2 ){
4375 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
4376 }else{
4377 raw_printf(stderr, "Usage: .echo on|off\n");
4378 rc = 1;
4380 }else
4382 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
4383 if( nArg==2 ){
4384 if( strcmp(azArg[1],"full")==0 ){
4385 p->autoEQP = 2;
4386 }else{
4387 p->autoEQP = booleanValue(azArg[1]);
4389 }else{
4390 raw_printf(stderr, "Usage: .eqp on|off|full\n");
4391 rc = 1;
4393 }else
4395 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
4396 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
4397 rc = 2;
4398 }else
4400 /* The ".explain" command is automatic now. It is largely pointless. It
4401 ** retained purely for backwards compatibility */
4402 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
4403 int val = 1;
4404 if( nArg>=2 ){
4405 if( strcmp(azArg[1],"auto")==0 ){
4406 val = 99;
4407 }else{
4408 val = booleanValue(azArg[1]);
4411 if( val==1 && p->mode!=MODE_Explain ){
4412 p->normalMode = p->mode;
4413 p->mode = MODE_Explain;
4414 p->autoExplain = 0;
4415 }else if( val==0 ){
4416 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4417 p->autoExplain = 0;
4418 }else if( val==99 ){
4419 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4420 p->autoExplain = 1;
4422 }else
4424 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
4425 ShellState data;
4426 char *zErrMsg = 0;
4427 int doStats = 0;
4428 memcpy(&data, p, sizeof(data));
4429 data.showHeader = 0;
4430 data.cMode = data.mode = MODE_Semi;
4431 if( nArg==2 && optionMatch(azArg[1], "indent") ){
4432 data.cMode = data.mode = MODE_Pretty;
4433 nArg = 1;
4435 if( nArg!=1 ){
4436 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
4437 rc = 1;
4438 goto meta_command_exit;
4440 open_db(p, 0);
4441 rc = sqlite3_exec(p->db,
4442 "SELECT sql FROM"
4443 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
4444 " FROM sqlite_master UNION ALL"
4445 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
4446 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
4447 "ORDER BY rowid",
4448 callback, &data, &zErrMsg
4450 if( rc==SQLITE_OK ){
4451 sqlite3_stmt *pStmt;
4452 rc = sqlite3_prepare_v2(p->db,
4453 "SELECT rowid FROM sqlite_master"
4454 " WHERE name GLOB 'sqlite_stat[134]'",
4455 -1, &pStmt, 0);
4456 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
4457 sqlite3_finalize(pStmt);
4459 if( doStats==0 ){
4460 raw_printf(p->out, "/* No STAT tables available */\n");
4461 }else{
4462 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4463 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
4464 callback, &data, &zErrMsg);
4465 data.cMode = data.mode = MODE_Insert;
4466 data.zDestTable = "sqlite_stat1";
4467 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
4468 shell_callback, &data,&zErrMsg);
4469 data.zDestTable = "sqlite_stat3";
4470 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
4471 shell_callback, &data,&zErrMsg);
4472 data.zDestTable = "sqlite_stat4";
4473 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
4474 shell_callback, &data, &zErrMsg);
4475 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4477 }else
4479 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
4480 if( nArg==2 ){
4481 p->showHeader = booleanValue(azArg[1]);
4482 }else{
4483 raw_printf(stderr, "Usage: .headers on|off\n");
4484 rc = 1;
4486 }else
4488 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
4489 utf8_printf(p->out, "%s", zHelp);
4490 }else
4492 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
4493 char *zTable; /* Insert data into this table */
4494 char *zFile; /* Name of file to extra content from */
4495 sqlite3_stmt *pStmt = NULL; /* A statement */
4496 int nCol; /* Number of columns in the table */
4497 int nByte; /* Number of bytes in an SQL string */
4498 int i, j; /* Loop counters */
4499 int needCommit; /* True to COMMIT or ROLLBACK at end */
4500 int nSep; /* Number of bytes in p->colSeparator[] */
4501 char *zSql; /* An SQL statement */
4502 ImportCtx sCtx; /* Reader context */
4503 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
4504 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
4506 if( nArg!=3 ){
4507 raw_printf(stderr, "Usage: .import FILE TABLE\n");
4508 goto meta_command_exit;
4510 zFile = azArg[1];
4511 zTable = azArg[2];
4512 seenInterrupt = 0;
4513 memset(&sCtx, 0, sizeof(sCtx));
4514 open_db(p, 0);
4515 nSep = strlen30(p->colSeparator);
4516 if( nSep==0 ){
4517 raw_printf(stderr,
4518 "Error: non-null column separator required for import\n");
4519 return 1;
4521 if( nSep>1 ){
4522 raw_printf(stderr, "Error: multi-character column separators not allowed"
4523 " for import\n");
4524 return 1;
4526 nSep = strlen30(p->rowSeparator);
4527 if( nSep==0 ){
4528 raw_printf(stderr, "Error: non-null row separator required for import\n");
4529 return 1;
4531 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
4532 /* When importing CSV (only), if the row separator is set to the
4533 ** default output row separator, change it to the default input
4534 ** row separator. This avoids having to maintain different input
4535 ** and output row separators. */
4536 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4537 nSep = strlen30(p->rowSeparator);
4539 if( nSep>1 ){
4540 raw_printf(stderr, "Error: multi-character row separators not allowed"
4541 " for import\n");
4542 return 1;
4544 sCtx.zFile = zFile;
4545 sCtx.nLine = 1;
4546 if( sCtx.zFile[0]=='|' ){
4547 #ifdef SQLITE_OMIT_POPEN
4548 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
4549 return 1;
4550 #else
4551 sCtx.in = popen(sCtx.zFile+1, "r");
4552 sCtx.zFile = "<pipe>";
4553 xCloser = pclose;
4554 #endif
4555 }else{
4556 sCtx.in = fopen(sCtx.zFile, "rb");
4557 xCloser = fclose;
4559 if( p->mode==MODE_Ascii ){
4560 xRead = ascii_read_one_field;
4561 }else{
4562 xRead = csv_read_one_field;
4564 if( sCtx.in==0 ){
4565 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4566 return 1;
4568 sCtx.cColSep = p->colSeparator[0];
4569 sCtx.cRowSep = p->rowSeparator[0];
4570 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
4571 if( zSql==0 ){
4572 raw_printf(stderr, "Error: out of memory\n");
4573 xCloser(sCtx.in);
4574 return 1;
4576 nByte = strlen30(zSql);
4577 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4578 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
4579 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
4580 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
4581 char cSep = '(';
4582 while( xRead(&sCtx) ){
4583 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
4584 cSep = ',';
4585 if( sCtx.cTerm!=sCtx.cColSep ) break;
4587 if( cSep=='(' ){
4588 sqlite3_free(zCreate);
4589 sqlite3_free(sCtx.z);
4590 xCloser(sCtx.in);
4591 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
4592 return 1;
4594 zCreate = sqlite3_mprintf("%z\n)", zCreate);
4595 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
4596 sqlite3_free(zCreate);
4597 if( rc ){
4598 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
4599 sqlite3_errmsg(p->db));
4600 sqlite3_free(sCtx.z);
4601 xCloser(sCtx.in);
4602 return 1;
4604 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4606 sqlite3_free(zSql);
4607 if( rc ){
4608 if (pStmt) sqlite3_finalize(pStmt);
4609 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
4610 xCloser(sCtx.in);
4611 return 1;
4613 nCol = sqlite3_column_count(pStmt);
4614 sqlite3_finalize(pStmt);
4615 pStmt = 0;
4616 if( nCol==0 ) return 0; /* no columns, no error */
4617 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
4618 if( zSql==0 ){
4619 raw_printf(stderr, "Error: out of memory\n");
4620 xCloser(sCtx.in);
4621 return 1;
4623 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
4624 j = strlen30(zSql);
4625 for(i=1; i<nCol; i++){
4626 zSql[j++] = ',';
4627 zSql[j++] = '?';
4629 zSql[j++] = ')';
4630 zSql[j] = 0;
4631 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4632 sqlite3_free(zSql);
4633 if( rc ){
4634 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
4635 if (pStmt) sqlite3_finalize(pStmt);
4636 xCloser(sCtx.in);
4637 return 1;
4639 needCommit = sqlite3_get_autocommit(p->db);
4640 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
4642 int startLine = sCtx.nLine;
4643 for(i=0; i<nCol; i++){
4644 char *z = xRead(&sCtx);
4646 ** Did we reach end-of-file before finding any columns?
4647 ** If so, stop instead of NULL filling the remaining columns.
4649 if( z==0 && i==0 ) break;
4651 ** Did we reach end-of-file OR end-of-line before finding any
4652 ** columns in ASCII mode? If so, stop instead of NULL filling
4653 ** the remaining columns.
4655 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
4656 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
4657 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
4658 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4659 "filling the rest with NULL\n",
4660 sCtx.zFile, startLine, nCol, i+1);
4661 i += 2;
4662 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
4665 if( sCtx.cTerm==sCtx.cColSep ){
4667 xRead(&sCtx);
4668 i++;
4669 }while( sCtx.cTerm==sCtx.cColSep );
4670 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4671 "extras ignored\n",
4672 sCtx.zFile, startLine, nCol, i);
4674 if( i>=nCol ){
4675 sqlite3_step(pStmt);
4676 rc = sqlite3_reset(pStmt);
4677 if( rc!=SQLITE_OK ){
4678 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
4679 startLine, sqlite3_errmsg(p->db));
4682 }while( sCtx.cTerm!=EOF );
4684 xCloser(sCtx.in);
4685 sqlite3_free(sCtx.z);
4686 sqlite3_finalize(pStmt);
4687 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
4688 }else
4690 #ifndef SQLITE_UNTESTABLE
4691 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
4692 char *zSql;
4693 char *zCollist = 0;
4694 sqlite3_stmt *pStmt;
4695 int tnum = 0;
4696 int i;
4697 if( nArg!=3 ){
4698 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
4699 rc = 1;
4700 goto meta_command_exit;
4702 open_db(p, 0);
4703 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
4704 " WHERE name='%q' AND type='index'", azArg[1]);
4705 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4706 sqlite3_free(zSql);
4707 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4708 tnum = sqlite3_column_int(pStmt, 0);
4710 sqlite3_finalize(pStmt);
4711 if( tnum==0 ){
4712 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
4713 rc = 1;
4714 goto meta_command_exit;
4716 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
4717 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4718 sqlite3_free(zSql);
4719 i = 0;
4720 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4721 char zLabel[20];
4722 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
4723 i++;
4724 if( zCol==0 ){
4725 if( sqlite3_column_int(pStmt,1)==-1 ){
4726 zCol = "_ROWID_";
4727 }else{
4728 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
4729 zCol = zLabel;
4732 if( zCollist==0 ){
4733 zCollist = sqlite3_mprintf("\"%w\"", zCol);
4734 }else{
4735 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
4738 sqlite3_finalize(pStmt);
4739 zSql = sqlite3_mprintf(
4740 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
4741 azArg[2], zCollist, zCollist);
4742 sqlite3_free(zCollist);
4743 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
4744 if( rc==SQLITE_OK ){
4745 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
4746 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
4747 if( rc ){
4748 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
4749 }else{
4750 utf8_printf(stdout, "%s;\n", zSql);
4751 raw_printf(stdout,
4752 "WARNING: writing to an imposter table will corrupt the index!\n"
4755 }else{
4756 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
4757 rc = 1;
4759 sqlite3_free(zSql);
4760 }else
4761 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
4763 #ifdef SQLITE_ENABLE_IOTRACE
4764 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
4765 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
4766 if( iotrace && iotrace!=stdout ) fclose(iotrace);
4767 iotrace = 0;
4768 if( nArg<2 ){
4769 sqlite3IoTrace = 0;
4770 }else if( strcmp(azArg[1], "-")==0 ){
4771 sqlite3IoTrace = iotracePrintf;
4772 iotrace = stdout;
4773 }else{
4774 iotrace = fopen(azArg[1], "w");
4775 if( iotrace==0 ){
4776 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
4777 sqlite3IoTrace = 0;
4778 rc = 1;
4779 }else{
4780 sqlite3IoTrace = iotracePrintf;
4783 }else
4784 #endif
4786 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
4787 static const struct {
4788 const char *zLimitName; /* Name of a limit */
4789 int limitCode; /* Integer code for that limit */
4790 } aLimit[] = {
4791 { "length", SQLITE_LIMIT_LENGTH },
4792 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
4793 { "column", SQLITE_LIMIT_COLUMN },
4794 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
4795 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
4796 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
4797 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
4798 { "attached", SQLITE_LIMIT_ATTACHED },
4799 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4800 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
4801 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
4802 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
4804 int i, n2;
4805 open_db(p, 0);
4806 if( nArg==1 ){
4807 for(i=0; i<ArraySize(aLimit); i++){
4808 printf("%20s %d\n", aLimit[i].zLimitName,
4809 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
4811 }else if( nArg>3 ){
4812 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
4813 rc = 1;
4814 goto meta_command_exit;
4815 }else{
4816 int iLimit = -1;
4817 n2 = strlen30(azArg[1]);
4818 for(i=0; i<ArraySize(aLimit); i++){
4819 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
4820 if( iLimit<0 ){
4821 iLimit = i;
4822 }else{
4823 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
4824 rc = 1;
4825 goto meta_command_exit;
4829 if( iLimit<0 ){
4830 utf8_printf(stderr, "unknown limit: \"%s\"\n"
4831 "enter \".limits\" with no arguments for a list.\n",
4832 azArg[1]);
4833 rc = 1;
4834 goto meta_command_exit;
4836 if( nArg==3 ){
4837 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
4838 (int)integerValue(azArg[2]));
4840 printf("%20s %d\n", aLimit[iLimit].zLimitName,
4841 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
4843 }else
4845 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
4846 open_db(p, 0);
4847 lintDotCommand(p, azArg, nArg);
4848 }else
4850 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4851 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
4852 const char *zFile, *zProc;
4853 char *zErrMsg = 0;
4854 if( nArg<2 ){
4855 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
4856 rc = 1;
4857 goto meta_command_exit;
4859 zFile = azArg[1];
4860 zProc = nArg>=3 ? azArg[2] : 0;
4861 open_db(p, 0);
4862 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
4863 if( rc!=SQLITE_OK ){
4864 utf8_printf(stderr, "Error: %s\n", zErrMsg);
4865 sqlite3_free(zErrMsg);
4866 rc = 1;
4868 }else
4869 #endif
4871 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
4872 if( nArg!=2 ){
4873 raw_printf(stderr, "Usage: .log FILENAME\n");
4874 rc = 1;
4875 }else{
4876 const char *zFile = azArg[1];
4877 output_file_close(p->pLog);
4878 p->pLog = output_file_open(zFile);
4880 }else
4882 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
4883 const char *zMode = nArg>=2 ? azArg[1] : "";
4884 int n2 = (int)strlen(zMode);
4885 int c2 = zMode[0];
4886 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
4887 p->mode = MODE_Line;
4888 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4889 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
4890 p->mode = MODE_Column;
4891 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4892 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
4893 p->mode = MODE_List;
4894 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
4895 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4896 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
4897 p->mode = MODE_Html;
4898 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
4899 p->mode = MODE_Tcl;
4900 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
4901 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4902 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
4903 p->mode = MODE_Csv;
4904 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
4905 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
4906 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
4907 p->mode = MODE_List;
4908 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
4909 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
4910 p->mode = MODE_Insert;
4911 set_table_name(p, nArg>=3 ? azArg[2] : "table");
4912 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
4913 p->mode = MODE_Quote;
4914 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
4915 p->mode = MODE_Ascii;
4916 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
4917 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
4918 }else if( nArg==1 ){
4919 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
4920 }else{
4921 raw_printf(stderr, "Error: mode should be one of: "
4922 "ascii column csv html insert line list quote tabs tcl\n");
4923 rc = 1;
4925 p->cMode = p->mode;
4926 }else
4928 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
4929 if( nArg==2 ){
4930 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
4931 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
4932 }else{
4933 raw_printf(stderr, "Usage: .nullvalue STRING\n");
4934 rc = 1;
4936 }else
4938 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
4939 char *zNewFilename; /* Name of the database file to open */
4940 int iName = 1; /* Index in azArg[] of the filename */
4941 int newFlag = 0; /* True to delete file before opening */
4942 /* Close the existing database */
4943 session_close_all(p);
4944 sqlite3_close(p->db);
4945 p->db = 0;
4946 p->zDbFilename = 0;
4947 sqlite3_free(p->zFreeOnClose);
4948 p->zFreeOnClose = 0;
4949 /* Check for command-line arguments */
4950 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
4951 const char *z = azArg[iName];
4952 if( optionMatch(z,"new") ){
4953 newFlag = 1;
4954 }else if( z[0]=='-' ){
4955 utf8_printf(stderr, "unknown option: %s\n", z);
4956 rc = 1;
4957 goto meta_command_exit;
4960 /* If a filename is specified, try to open it first */
4961 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
4962 if( zNewFilename ){
4963 if( newFlag ) shellDeleteFile(zNewFilename);
4964 p->zDbFilename = zNewFilename;
4965 open_db(p, 1);
4966 if( p->db==0 ){
4967 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
4968 sqlite3_free(zNewFilename);
4969 }else{
4970 p->zFreeOnClose = zNewFilename;
4973 if( p->db==0 ){
4974 /* As a fall-back open a TEMP database */
4975 p->zDbFilename = 0;
4976 open_db(p, 0);
4978 }else
4980 if( c=='o'
4981 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
4983 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
4984 if( nArg>2 ){
4985 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
4986 rc = 1;
4987 goto meta_command_exit;
4989 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
4990 if( nArg<2 ){
4991 raw_printf(stderr, "Usage: .once FILE\n");
4992 rc = 1;
4993 goto meta_command_exit;
4995 p->outCount = 2;
4996 }else{
4997 p->outCount = 0;
4999 output_reset(p);
5000 if( zFile[0]=='|' ){
5001 #ifdef SQLITE_OMIT_POPEN
5002 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5003 rc = 1;
5004 p->out = stdout;
5005 #else
5006 p->out = popen(zFile + 1, "w");
5007 if( p->out==0 ){
5008 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
5009 p->out = stdout;
5010 rc = 1;
5011 }else{
5012 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5014 #endif
5015 }else{
5016 p->out = output_file_open(zFile);
5017 if( p->out==0 ){
5018 if( strcmp(zFile,"off")!=0 ){
5019 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
5021 p->out = stdout;
5022 rc = 1;
5023 } else {
5024 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5027 }else
5029 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
5030 int i;
5031 for(i=1; i<nArg; i++){
5032 if( i>1 ) raw_printf(p->out, " ");
5033 utf8_printf(p->out, "%s", azArg[i]);
5035 raw_printf(p->out, "\n");
5036 }else
5038 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
5039 if( nArg >= 2) {
5040 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
5042 if( nArg >= 3) {
5043 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
5045 }else
5047 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
5048 rc = 2;
5049 }else
5051 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
5052 FILE *alt;
5053 if( nArg!=2 ){
5054 raw_printf(stderr, "Usage: .read FILE\n");
5055 rc = 1;
5056 goto meta_command_exit;
5058 alt = fopen(azArg[1], "rb");
5059 if( alt==0 ){
5060 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
5061 rc = 1;
5062 }else{
5063 rc = process_input(p, alt);
5064 fclose(alt);
5066 }else
5068 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
5069 const char *zSrcFile;
5070 const char *zDb;
5071 sqlite3 *pSrc;
5072 sqlite3_backup *pBackup;
5073 int nTimeout = 0;
5075 if( nArg==2 ){
5076 zSrcFile = azArg[1];
5077 zDb = "main";
5078 }else if( nArg==3 ){
5079 zSrcFile = azArg[2];
5080 zDb = azArg[1];
5081 }else{
5082 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
5083 rc = 1;
5084 goto meta_command_exit;
5086 rc = sqlite3_open(zSrcFile, &pSrc);
5087 if( rc!=SQLITE_OK ){
5088 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
5089 sqlite3_close(pSrc);
5090 return 1;
5092 open_db(p, 0);
5093 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
5094 if( pBackup==0 ){
5095 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5096 sqlite3_close(pSrc);
5097 return 1;
5099 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
5100 || rc==SQLITE_BUSY ){
5101 if( rc==SQLITE_BUSY ){
5102 if( nTimeout++ >= 3 ) break;
5103 sqlite3_sleep(100);
5106 sqlite3_backup_finish(pBackup);
5107 if( rc==SQLITE_DONE ){
5108 rc = 0;
5109 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
5110 raw_printf(stderr, "Error: source database is busy\n");
5111 rc = 1;
5112 }else{
5113 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5114 rc = 1;
5116 sqlite3_close(pSrc);
5117 }else
5120 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
5121 if( nArg==2 ){
5122 p->scanstatsOn = booleanValue(azArg[1]);
5123 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
5124 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
5125 #endif
5126 }else{
5127 raw_printf(stderr, "Usage: .scanstats on|off\n");
5128 rc = 1;
5130 }else
5132 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
5133 ShellText sSelect;
5134 ShellState data;
5135 char *zErrMsg = 0;
5136 const char *zDiv = 0;
5137 int iSchema = 0;
5139 open_db(p, 0);
5140 memcpy(&data, p, sizeof(data));
5141 data.showHeader = 0;
5142 data.cMode = data.mode = MODE_Semi;
5143 initText(&sSelect);
5144 if( nArg>=2 && optionMatch(azArg[1], "indent") ){
5145 data.cMode = data.mode = MODE_Pretty;
5146 nArg--;
5147 if( nArg==2 ) azArg[1] = azArg[2];
5149 if( nArg==2 && azArg[1][0]!='-' ){
5150 int i;
5151 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
5152 if( strcmp(azArg[1],"sqlite_master")==0 ){
5153 char *new_argv[2], *new_colv[2];
5154 new_argv[0] = "CREATE TABLE sqlite_master (\n"
5155 " type text,\n"
5156 " name text,\n"
5157 " tbl_name text,\n"
5158 " rootpage integer,\n"
5159 " sql text\n"
5160 ")";
5161 new_argv[1] = 0;
5162 new_colv[0] = "sql";
5163 new_colv[1] = 0;
5164 callback(&data, 1, new_argv, new_colv);
5165 rc = SQLITE_OK;
5166 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
5167 char *new_argv[2], *new_colv[2];
5168 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
5169 " type text,\n"
5170 " name text,\n"
5171 " tbl_name text,\n"
5172 " rootpage integer,\n"
5173 " sql text\n"
5174 ")";
5175 new_argv[1] = 0;
5176 new_colv[0] = "sql";
5177 new_colv[1] = 0;
5178 callback(&data, 1, new_argv, new_colv);
5179 rc = SQLITE_OK;
5180 }else{
5181 zDiv = "(";
5183 }else if( nArg==1 ){
5184 zDiv = "(";
5185 }else{
5186 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
5187 rc = 1;
5188 goto meta_command_exit;
5190 if( zDiv ){
5191 sqlite3_stmt *pStmt = 0;
5192 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
5193 -1, &pStmt, 0);
5194 if( rc ){
5195 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5196 sqlite3_finalize(pStmt);
5197 rc = 1;
5198 goto meta_command_exit;
5200 appendText(&sSelect, "SELECT sql FROM", 0);
5201 iSchema = 0;
5202 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5203 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
5204 char zScNum[30];
5205 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
5206 appendText(&sSelect, zDiv, 0);
5207 zDiv = " UNION ALL ";
5208 if( strcmp(zDb, "main")!=0 ){
5209 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
5210 appendText(&sSelect, zDb, '"');
5211 appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0);
5212 appendText(&sSelect, zScNum, 0);
5213 appendText(&sSelect, " AS snum, ", 0);
5214 appendText(&sSelect, zDb, '\'');
5215 appendText(&sSelect, " AS sname FROM ", 0);
5216 appendText(&sSelect, zDb, '"');
5217 appendText(&sSelect, ".sqlite_master", 0);
5218 }else{
5219 appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0);
5220 appendText(&sSelect, zScNum, 0);
5221 appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0);
5224 sqlite3_finalize(pStmt);
5225 appendText(&sSelect, ") WHERE ", 0);
5226 if( nArg>1 ){
5227 char *zQarg = sqlite3_mprintf("%Q", azArg[1]);
5228 if( strchr(azArg[1], '.') ){
5229 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
5230 }else{
5231 appendText(&sSelect, "lower(tbl_name)", 0);
5233 appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0);
5234 appendText(&sSelect, zQarg, 0);
5235 appendText(&sSelect, " AND ", 0);
5236 sqlite3_free(zQarg);
5238 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
5239 " ORDER BY snum, rowid", 0);
5240 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
5241 freeText(&sSelect);
5243 if( zErrMsg ){
5244 utf8_printf(stderr,"Error: %s\n", zErrMsg);
5245 sqlite3_free(zErrMsg);
5246 rc = 1;
5247 }else if( rc != SQLITE_OK ){
5248 raw_printf(stderr,"Error: querying schema information\n");
5249 rc = 1;
5250 }else{
5251 rc = 0;
5253 }else
5255 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
5256 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
5257 sqlite3SelectTrace = (int)integerValue(azArg[1]);
5258 }else
5259 #endif
5261 #if defined(SQLITE_ENABLE_SESSION)
5262 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
5263 OpenSession *pSession = &p->aSession[0];
5264 char **azCmd = &azArg[1];
5265 int iSes = 0;
5266 int nCmd = nArg - 1;
5267 int i;
5268 if( nArg<=1 ) goto session_syntax_error;
5269 open_db(p, 0);
5270 if( nArg>=3 ){
5271 for(iSes=0; iSes<p->nSession; iSes++){
5272 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
5274 if( iSes<p->nSession ){
5275 pSession = &p->aSession[iSes];
5276 azCmd++;
5277 nCmd--;
5278 }else{
5279 pSession = &p->aSession[0];
5280 iSes = 0;
5284 /* .session attach TABLE
5285 ** Invoke the sqlite3session_attach() interface to attach a particular
5286 ** table so that it is never filtered.
5288 if( strcmp(azCmd[0],"attach")==0 ){
5289 if( nCmd!=2 ) goto session_syntax_error;
5290 if( pSession->p==0 ){
5291 session_not_open:
5292 raw_printf(stderr, "ERROR: No sessions are open\n");
5293 }else{
5294 rc = sqlite3session_attach(pSession->p, azCmd[1]);
5295 if( rc ){
5296 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
5297 rc = 0;
5300 }else
5302 /* .session changeset FILE
5303 ** .session patchset FILE
5304 ** Write a changeset or patchset into a file. The file is overwritten.
5306 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
5307 FILE *out = 0;
5308 if( nCmd!=2 ) goto session_syntax_error;
5309 if( pSession->p==0 ) goto session_not_open;
5310 out = fopen(azCmd[1], "wb");
5311 if( out==0 ){
5312 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
5313 }else{
5314 int szChng;
5315 void *pChng;
5316 if( azCmd[0][0]=='c' ){
5317 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
5318 }else{
5319 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
5321 if( rc ){
5322 printf("Error: error code %d\n", rc);
5323 rc = 0;
5325 if( pChng
5326 && fwrite(pChng, szChng, 1, out)!=1 ){
5327 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
5328 szChng);
5330 sqlite3_free(pChng);
5331 fclose(out);
5333 }else
5335 /* .session close
5336 ** Close the identified session
5338 if( strcmp(azCmd[0], "close")==0 ){
5339 if( nCmd!=1 ) goto session_syntax_error;
5340 if( p->nSession ){
5341 session_close(pSession);
5342 p->aSession[iSes] = p->aSession[--p->nSession];
5344 }else
5346 /* .session enable ?BOOLEAN?
5347 ** Query or set the enable flag
5349 if( strcmp(azCmd[0], "enable")==0 ){
5350 int ii;
5351 if( nCmd>2 ) goto session_syntax_error;
5352 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5353 if( p->nSession ){
5354 ii = sqlite3session_enable(pSession->p, ii);
5355 utf8_printf(p->out, "session %s enable flag = %d\n",
5356 pSession->zName, ii);
5358 }else
5360 /* .session filter GLOB ....
5361 ** Set a list of GLOB patterns of table names to be excluded.
5363 if( strcmp(azCmd[0], "filter")==0 ){
5364 int ii, nByte;
5365 if( nCmd<2 ) goto session_syntax_error;
5366 if( p->nSession ){
5367 for(ii=0; ii<pSession->nFilter; ii++){
5368 sqlite3_free(pSession->azFilter[ii]);
5370 sqlite3_free(pSession->azFilter);
5371 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
5372 pSession->azFilter = sqlite3_malloc( nByte );
5373 if( pSession->azFilter==0 ){
5374 raw_printf(stderr, "Error: out or memory\n");
5375 exit(1);
5377 for(ii=1; ii<nCmd; ii++){
5378 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
5380 pSession->nFilter = ii-1;
5382 }else
5384 /* .session indirect ?BOOLEAN?
5385 ** Query or set the indirect flag
5387 if( strcmp(azCmd[0], "indirect")==0 ){
5388 int ii;
5389 if( nCmd>2 ) goto session_syntax_error;
5390 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5391 if( p->nSession ){
5392 ii = sqlite3session_indirect(pSession->p, ii);
5393 utf8_printf(p->out, "session %s indirect flag = %d\n",
5394 pSession->zName, ii);
5396 }else
5398 /* .session isempty
5399 ** Determine if the session is empty
5401 if( strcmp(azCmd[0], "isempty")==0 ){
5402 int ii;
5403 if( nCmd!=1 ) goto session_syntax_error;
5404 if( p->nSession ){
5405 ii = sqlite3session_isempty(pSession->p);
5406 utf8_printf(p->out, "session %s isempty flag = %d\n",
5407 pSession->zName, ii);
5409 }else
5411 /* .session list
5412 ** List all currently open sessions
5414 if( strcmp(azCmd[0],"list")==0 ){
5415 for(i=0; i<p->nSession; i++){
5416 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
5418 }else
5420 /* .session open DB NAME
5421 ** Open a new session called NAME on the attached database DB.
5422 ** DB is normally "main".
5424 if( strcmp(azCmd[0],"open")==0 ){
5425 char *zName;
5426 if( nCmd!=3 ) goto session_syntax_error;
5427 zName = azCmd[2];
5428 if( zName[0]==0 ) goto session_syntax_error;
5429 for(i=0; i<p->nSession; i++){
5430 if( strcmp(p->aSession[i].zName,zName)==0 ){
5431 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
5432 goto meta_command_exit;
5435 if( p->nSession>=ArraySize(p->aSession) ){
5436 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
5437 goto meta_command_exit;
5439 pSession = &p->aSession[p->nSession];
5440 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
5441 if( rc ){
5442 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
5443 rc = 0;
5444 goto meta_command_exit;
5446 pSession->nFilter = 0;
5447 sqlite3session_table_filter(pSession->p, session_filter, pSession);
5448 p->nSession++;
5449 pSession->zName = sqlite3_mprintf("%s", zName);
5450 }else
5451 /* If no command name matches, show a syntax error */
5452 session_syntax_error:
5453 session_help(p);
5454 }else
5455 #endif
5457 #ifdef SQLITE_DEBUG
5458 /* Undocumented commands for internal testing. Subject to change
5459 ** without notice. */
5460 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
5461 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
5462 int i, v;
5463 for(i=1; i<nArg; i++){
5464 v = booleanValue(azArg[i]);
5465 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
5468 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
5469 int i; sqlite3_int64 v;
5470 for(i=1; i<nArg; i++){
5471 char zBuf[200];
5472 v = integerValue(azArg[i]);
5473 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
5474 utf8_printf(p->out, "%s", zBuf);
5477 }else
5478 #endif
5480 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
5481 int bIsInit = 0; /* True to initialize the SELFTEST table */
5482 int bVerbose = 0; /* Verbose output */
5483 int bSelftestExists; /* True if SELFTEST already exists */
5484 int i, k; /* Loop counters */
5485 int nTest = 0; /* Number of tests runs */
5486 int nErr = 0; /* Number of errors seen */
5487 ShellText str; /* Answer for a query */
5488 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
5490 open_db(p,0);
5491 for(i=1; i<nArg; i++){
5492 const char *z = azArg[i];
5493 if( z[0]=='-' && z[1]=='-' ) z++;
5494 if( strcmp(z,"-init")==0 ){
5495 bIsInit = 1;
5496 }else
5497 if( strcmp(z,"-v")==0 ){
5498 bVerbose++;
5499 }else
5501 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5502 azArg[i], azArg[0]);
5503 raw_printf(stderr, "Should be one of: --init -v\n");
5504 rc = 1;
5505 goto meta_command_exit;
5508 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
5509 != SQLITE_OK ){
5510 bSelftestExists = 0;
5511 }else{
5512 bSelftestExists = 1;
5514 if( bIsInit ){
5515 createSelftestTable(p);
5516 bSelftestExists = 1;
5518 initText(&str);
5519 appendText(&str, "x", 0);
5520 for(k=bSelftestExists; k>=0; k--){
5521 if( k==1 ){
5522 rc = sqlite3_prepare_v2(p->db,
5523 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
5524 -1, &pStmt, 0);
5525 }else{
5526 rc = sqlite3_prepare_v2(p->db,
5527 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
5528 " (1,'run','PRAGMA integrity_check','ok')",
5529 -1, &pStmt, 0);
5531 if( rc ){
5532 raw_printf(stderr, "Error querying the selftest table\n");
5533 rc = 1;
5534 sqlite3_finalize(pStmt);
5535 goto meta_command_exit;
5537 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
5538 int tno = sqlite3_column_int(pStmt, 0);
5539 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
5540 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
5541 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
5543 k = 0;
5544 if( bVerbose>0 ){
5545 char *zQuote = sqlite3_mprintf("%q", zSql);
5546 printf("%d: %s %s\n", tno, zOp, zSql);
5547 sqlite3_free(zQuote);
5549 if( strcmp(zOp,"memo")==0 ){
5550 utf8_printf(p->out, "%s\n", zSql);
5551 }else
5552 if( strcmp(zOp,"run")==0 ){
5553 char *zErrMsg = 0;
5554 str.n = 0;
5555 str.z[0] = 0;
5556 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
5557 nTest++;
5558 if( bVerbose ){
5559 utf8_printf(p->out, "Result: %s\n", str.z);
5561 if( rc || zErrMsg ){
5562 nErr++;
5563 rc = 1;
5564 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
5565 sqlite3_free(zErrMsg);
5566 }else if( strcmp(zAns,str.z)!=0 ){
5567 nErr++;
5568 rc = 1;
5569 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
5570 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
5572 }else
5574 utf8_printf(stderr,
5575 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
5576 rc = 1;
5577 break;
5579 } /* End loop over rows of content from SELFTEST */
5580 sqlite3_finalize(pStmt);
5581 } /* End loop over k */
5582 freeText(&str);
5583 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
5584 }else
5586 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
5587 if( nArg<2 || nArg>3 ){
5588 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
5589 rc = 1;
5591 if( nArg>=2 ){
5592 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
5593 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
5595 if( nArg>=3 ){
5596 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
5597 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
5599 }else
5601 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
5602 const char *zLike = 0; /* Which table to checksum. 0 means everything */
5603 int i; /* Loop counter */
5604 int bSchema = 0; /* Also hash the schema */
5605 int bSeparate = 0; /* Hash each table separately */
5606 int iSize = 224; /* Hash algorithm to use */
5607 int bDebug = 0; /* Only show the query that would have run */
5608 sqlite3_stmt *pStmt; /* For querying tables names */
5609 char *zSql; /* SQL to be run */
5610 char *zSep; /* Separator */
5611 ShellText sSql; /* Complete SQL for the query to run the hash */
5612 ShellText sQuery; /* Set of queries used to read all content */
5613 open_db(p, 0);
5614 for(i=1; i<nArg; i++){
5615 const char *z = azArg[i];
5616 if( z[0]=='-' ){
5617 z++;
5618 if( z[0]=='-' ) z++;
5619 if( strcmp(z,"schema")==0 ){
5620 bSchema = 1;
5621 }else
5622 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
5623 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
5625 iSize = atoi(&z[5]);
5626 }else
5627 if( strcmp(z,"debug")==0 ){
5628 bDebug = 1;
5629 }else
5631 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5632 azArg[i], azArg[0]);
5633 raw_printf(stderr, "Should be one of: --schema"
5634 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
5635 rc = 1;
5636 goto meta_command_exit;
5638 }else if( zLike ){
5639 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
5640 rc = 1;
5641 goto meta_command_exit;
5642 }else{
5643 zLike = z;
5644 bSeparate = 1;
5645 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
5648 if( bSchema ){
5649 zSql = "SELECT lower(name) FROM sqlite_master"
5650 " WHERE type='table' AND coalesce(rootpage,0)>1"
5651 " UNION ALL SELECT 'sqlite_master'"
5652 " ORDER BY 1 collate nocase";
5653 }else{
5654 zSql = "SELECT lower(name) FROM sqlite_master"
5655 " WHERE type='table' AND coalesce(rootpage,0)>1"
5656 " AND name NOT LIKE 'sqlite_%'"
5657 " ORDER BY 1 collate nocase";
5659 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5660 initText(&sQuery);
5661 initText(&sSql);
5662 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
5663 zSep = "VALUES(";
5664 while( SQLITE_ROW==sqlite3_step(pStmt) ){
5665 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
5666 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
5667 if( strncmp(zTab, "sqlite_",7)!=0 ){
5668 appendText(&sQuery,"SELECT * FROM ", 0);
5669 appendText(&sQuery,zTab,'"');
5670 appendText(&sQuery," NOT INDEXED;", 0);
5671 }else if( strcmp(zTab, "sqlite_master")==0 ){
5672 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
5673 " ORDER BY name;", 0);
5674 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
5675 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
5676 " ORDER BY name;", 0);
5677 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
5678 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
5679 " ORDER BY tbl,idx;", 0);
5680 }else if( strcmp(zTab, "sqlite_stat3")==0
5681 || strcmp(zTab, "sqlite_stat4")==0 ){
5682 appendText(&sQuery, "SELECT * FROM ", 0);
5683 appendText(&sQuery, zTab, 0);
5684 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
5686 appendText(&sSql, zSep, 0);
5687 appendText(&sSql, sQuery.z, '\'');
5688 sQuery.n = 0;
5689 appendText(&sSql, ",", 0);
5690 appendText(&sSql, zTab, '\'');
5691 zSep = "),(";
5693 sqlite3_finalize(pStmt);
5694 if( bSeparate ){
5695 zSql = sqlite3_mprintf(
5696 "%s))"
5697 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
5698 " FROM [sha3sum$query]",
5699 sSql.z, iSize);
5700 }else{
5701 zSql = sqlite3_mprintf(
5702 "%s))"
5703 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
5704 " FROM [sha3sum$query]",
5705 sSql.z, iSize);
5707 freeText(&sQuery);
5708 freeText(&sSql);
5709 if( bDebug ){
5710 utf8_printf(p->out, "%s\n", zSql);
5711 }else{
5712 shell_exec(p->db, zSql, shell_callback, p, 0);
5714 sqlite3_free(zSql);
5715 }else
5717 if( c=='s'
5718 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
5720 char *zCmd;
5721 int i, x;
5722 if( nArg<2 ){
5723 raw_printf(stderr, "Usage: .system COMMAND\n");
5724 rc = 1;
5725 goto meta_command_exit;
5727 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
5728 for(i=2; i<nArg; i++){
5729 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
5730 zCmd, azArg[i]);
5732 x = system(zCmd);
5733 sqlite3_free(zCmd);
5734 if( x ) raw_printf(stderr, "System command returns %d\n", x);
5735 }else
5737 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
5738 static const char *azBool[] = { "off", "on", "full", "unk" };
5739 int i;
5740 if( nArg!=1 ){
5741 raw_printf(stderr, "Usage: .show\n");
5742 rc = 1;
5743 goto meta_command_exit;
5745 utf8_printf(p->out, "%12.12s: %s\n","echo",
5746 azBool[ShellHasFlag(p, SHFLG_Echo)]);
5747 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
5748 utf8_printf(p->out, "%12.12s: %s\n","explain",
5749 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
5750 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
5751 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
5752 utf8_printf(p->out, "%12.12s: ", "nullvalue");
5753 output_c_string(p->out, p->nullValue);
5754 raw_printf(p->out, "\n");
5755 utf8_printf(p->out,"%12.12s: %s\n","output",
5756 strlen30(p->outfile) ? p->outfile : "stdout");
5757 utf8_printf(p->out,"%12.12s: ", "colseparator");
5758 output_c_string(p->out, p->colSeparator);
5759 raw_printf(p->out, "\n");
5760 utf8_printf(p->out,"%12.12s: ", "rowseparator");
5761 output_c_string(p->out, p->rowSeparator);
5762 raw_printf(p->out, "\n");
5763 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
5764 utf8_printf(p->out, "%12.12s: ", "width");
5765 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
5766 raw_printf(p->out, "%d ", p->colWidth[i]);
5768 raw_printf(p->out, "\n");
5769 utf8_printf(p->out, "%12.12s: %s\n", "filename",
5770 p->zDbFilename ? p->zDbFilename : "");
5771 }else
5773 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
5774 if( nArg==2 ){
5775 p->statsOn = booleanValue(azArg[1]);
5776 }else if( nArg==1 ){
5777 display_stats(p->db, p, 0);
5778 }else{
5779 raw_printf(stderr, "Usage: .stats ?on|off?\n");
5780 rc = 1;
5782 }else
5784 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
5785 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
5786 || strncmp(azArg[0], "indexes", n)==0) )
5788 sqlite3_stmt *pStmt;
5789 char **azResult;
5790 int nRow, nAlloc;
5791 int ii;
5792 ShellText s;
5793 initText(&s);
5794 open_db(p, 0);
5795 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
5796 if( rc ) return shellDatabaseError(p->db);
5798 if( nArg>2 && c=='i' ){
5799 /* It is an historical accident that the .indexes command shows an error
5800 ** when called with the wrong number of arguments whereas the .tables
5801 ** command does not. */
5802 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
5803 rc = 1;
5804 goto meta_command_exit;
5806 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
5807 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
5808 if( zDbName==0 ) continue;
5809 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
5810 if( sqlite3_stricmp(zDbName, "main")==0 ){
5811 appendText(&s, "SELECT name FROM ", 0);
5812 }else{
5813 appendText(&s, "SELECT ", 0);
5814 appendText(&s, zDbName, '\'');
5815 appendText(&s, "||'.'||name FROM ", 0);
5817 appendText(&s, zDbName, '"');
5818 appendText(&s, ".sqlite_master ", 0);
5819 if( c=='t' ){
5820 appendText(&s," WHERE type IN ('table','view')"
5821 " AND name NOT LIKE 'sqlite_%'"
5822 " AND name LIKE ?1", 0);
5823 }else{
5824 appendText(&s," WHERE type='index'"
5825 " AND tbl_name LIKE ?1", 0);
5828 rc = sqlite3_finalize(pStmt);
5829 appendText(&s, " ORDER BY 1", 0);
5830 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
5831 freeText(&s);
5832 if( rc ) return shellDatabaseError(p->db);
5834 /* Run the SQL statement prepared by the above block. Store the results
5835 ** as an array of nul-terminated strings in azResult[]. */
5836 nRow = nAlloc = 0;
5837 azResult = 0;
5838 if( nArg>1 ){
5839 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
5840 }else{
5841 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
5843 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5844 if( nRow>=nAlloc ){
5845 char **azNew;
5846 int n2 = nAlloc*2 + 10;
5847 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
5848 if( azNew==0 ){
5849 rc = shellNomemError();
5850 break;
5852 nAlloc = n2;
5853 azResult = azNew;
5855 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
5856 if( 0==azResult[nRow] ){
5857 rc = shellNomemError();
5858 break;
5860 nRow++;
5862 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
5863 rc = shellDatabaseError(p->db);
5866 /* Pretty-print the contents of array azResult[] to the output */
5867 if( rc==0 && nRow>0 ){
5868 int len, maxlen = 0;
5869 int i, j;
5870 int nPrintCol, nPrintRow;
5871 for(i=0; i<nRow; i++){
5872 len = strlen30(azResult[i]);
5873 if( len>maxlen ) maxlen = len;
5875 nPrintCol = 80/(maxlen+2);
5876 if( nPrintCol<1 ) nPrintCol = 1;
5877 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
5878 for(i=0; i<nPrintRow; i++){
5879 for(j=i; j<nRow; j+=nPrintRow){
5880 char *zSp = j<nPrintRow ? "" : " ";
5881 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
5882 azResult[j] ? azResult[j]:"");
5884 raw_printf(p->out, "\n");
5888 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
5889 sqlite3_free(azResult);
5890 }else
5892 /* Begin redirecting output to the file "testcase-out.txt" */
5893 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
5894 output_reset(p);
5895 p->out = output_file_open("testcase-out.txt");
5896 if( p->out==0 ){
5897 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
5899 if( nArg>=2 ){
5900 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
5901 }else{
5902 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
5904 }else
5906 #ifndef SQLITE_UNTESTABLE
5907 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
5908 static const struct {
5909 const char *zCtrlName; /* Name of a test-control option */
5910 int ctrlCode; /* Integer code for that option */
5911 } aCtrl[] = {
5912 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
5913 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
5914 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
5915 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
5916 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
5917 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
5918 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
5919 { "assert", SQLITE_TESTCTRL_ASSERT },
5920 { "always", SQLITE_TESTCTRL_ALWAYS },
5921 { "reserve", SQLITE_TESTCTRL_RESERVE },
5922 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
5923 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
5924 { "byteorder", SQLITE_TESTCTRL_BYTEORDER },
5925 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT },
5926 { "imposter", SQLITE_TESTCTRL_IMPOSTER },
5928 int testctrl = -1;
5929 int rc2 = 0;
5930 int i, n2;
5931 open_db(p, 0);
5933 /* convert testctrl text option to value. allow any unique prefix
5934 ** of the option name, or a numerical value. */
5935 n2 = strlen30(azArg[1]);
5936 for(i=0; i<ArraySize(aCtrl); i++){
5937 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
5938 if( testctrl<0 ){
5939 testctrl = aCtrl[i].ctrlCode;
5940 }else{
5941 utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
5942 testctrl = -1;
5943 break;
5947 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
5948 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
5949 utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
5950 }else{
5951 switch(testctrl){
5953 /* sqlite3_test_control(int, db, int) */
5954 case SQLITE_TESTCTRL_OPTIMIZATIONS:
5955 case SQLITE_TESTCTRL_RESERVE:
5956 if( nArg==3 ){
5957 int opt = (int)strtol(azArg[2], 0, 0);
5958 rc2 = sqlite3_test_control(testctrl, p->db, opt);
5959 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5960 } else {
5961 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
5962 azArg[1]);
5964 break;
5966 /* sqlite3_test_control(int) */
5967 case SQLITE_TESTCTRL_PRNG_SAVE:
5968 case SQLITE_TESTCTRL_PRNG_RESTORE:
5969 case SQLITE_TESTCTRL_PRNG_RESET:
5970 case SQLITE_TESTCTRL_BYTEORDER:
5971 if( nArg==2 ){
5972 rc2 = sqlite3_test_control(testctrl);
5973 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5974 } else {
5975 utf8_printf(stderr,"Error: testctrl %s takes no options\n",
5976 azArg[1]);
5978 break;
5980 /* sqlite3_test_control(int, uint) */
5981 case SQLITE_TESTCTRL_PENDING_BYTE:
5982 if( nArg==3 ){
5983 unsigned int opt = (unsigned int)integerValue(azArg[2]);
5984 rc2 = sqlite3_test_control(testctrl, opt);
5985 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5986 } else {
5987 utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
5988 " int option\n", azArg[1]);
5990 break;
5992 /* sqlite3_test_control(int, int) */
5993 case SQLITE_TESTCTRL_ASSERT:
5994 case SQLITE_TESTCTRL_ALWAYS:
5995 case SQLITE_TESTCTRL_NEVER_CORRUPT:
5996 if( nArg==3 ){
5997 int opt = booleanValue(azArg[2]);
5998 rc2 = sqlite3_test_control(testctrl, opt);
5999 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6000 } else {
6001 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
6002 azArg[1]);
6004 break;
6006 /* sqlite3_test_control(int, char *) */
6007 #ifdef SQLITE_N_KEYWORD
6008 case SQLITE_TESTCTRL_ISKEYWORD:
6009 if( nArg==3 ){
6010 const char *opt = azArg[2];
6011 rc2 = sqlite3_test_control(testctrl, opt);
6012 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6013 } else {
6014 utf8_printf(stderr,
6015 "Error: testctrl %s takes a single char * option\n",
6016 azArg[1]);
6018 break;
6019 #endif
6021 case SQLITE_TESTCTRL_IMPOSTER:
6022 if( nArg==5 ){
6023 rc2 = sqlite3_test_control(testctrl, p->db,
6024 azArg[2],
6025 integerValue(azArg[3]),
6026 integerValue(azArg[4]));
6027 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6028 }else{
6029 raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
6031 break;
6033 case SQLITE_TESTCTRL_BITVEC_TEST:
6034 case SQLITE_TESTCTRL_FAULT_INSTALL:
6035 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
6036 default:
6037 utf8_printf(stderr,
6038 "Error: CLI support for testctrl %s not implemented\n",
6039 azArg[1]);
6040 break;
6043 }else
6044 #endif /* !defined(SQLITE_UNTESTABLE) */
6046 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
6047 open_db(p, 0);
6048 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
6049 }else
6051 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
6052 if( nArg==2 ){
6053 enableTimer = booleanValue(azArg[1]);
6054 if( enableTimer && !HAS_TIMER ){
6055 raw_printf(stderr, "Error: timer not available on this system.\n");
6056 enableTimer = 0;
6058 }else{
6059 raw_printf(stderr, "Usage: .timer on|off\n");
6060 rc = 1;
6062 }else
6064 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
6065 open_db(p, 0);
6066 if( nArg!=2 ){
6067 raw_printf(stderr, "Usage: .trace FILE|off\n");
6068 rc = 1;
6069 goto meta_command_exit;
6071 output_file_close(p->traceOut);
6072 p->traceOut = output_file_open(azArg[1]);
6073 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
6074 if( p->traceOut==0 ){
6075 sqlite3_trace_v2(p->db, 0, 0, 0);
6076 }else{
6077 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
6079 #endif
6080 }else
6082 #if SQLITE_USER_AUTHENTICATION
6083 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
6084 if( nArg<2 ){
6085 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
6086 rc = 1;
6087 goto meta_command_exit;
6089 open_db(p, 0);
6090 if( strcmp(azArg[1],"login")==0 ){
6091 if( nArg!=4 ){
6092 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
6093 rc = 1;
6094 goto meta_command_exit;
6096 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
6097 (int)strlen(azArg[3]));
6098 if( rc ){
6099 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
6100 rc = 1;
6102 }else if( strcmp(azArg[1],"add")==0 ){
6103 if( nArg!=5 ){
6104 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
6105 rc = 1;
6106 goto meta_command_exit;
6108 rc = sqlite3_user_add(p->db, azArg[2],
6109 azArg[3], (int)strlen(azArg[3]),
6110 booleanValue(azArg[4]));
6111 if( rc ){
6112 raw_printf(stderr, "User-Add failed: %d\n", rc);
6113 rc = 1;
6115 }else if( strcmp(azArg[1],"edit")==0 ){
6116 if( nArg!=5 ){
6117 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
6118 rc = 1;
6119 goto meta_command_exit;
6121 rc = sqlite3_user_change(p->db, azArg[2],
6122 azArg[3], (int)strlen(azArg[3]),
6123 booleanValue(azArg[4]));
6124 if( rc ){
6125 raw_printf(stderr, "User-Edit failed: %d\n", rc);
6126 rc = 1;
6128 }else if( strcmp(azArg[1],"delete")==0 ){
6129 if( nArg!=3 ){
6130 raw_printf(stderr, "Usage: .user delete USER\n");
6131 rc = 1;
6132 goto meta_command_exit;
6134 rc = sqlite3_user_delete(p->db, azArg[2]);
6135 if( rc ){
6136 raw_printf(stderr, "User-Delete failed: %d\n", rc);
6137 rc = 1;
6139 }else{
6140 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
6141 rc = 1;
6142 goto meta_command_exit;
6144 }else
6145 #endif /* SQLITE_USER_AUTHENTICATION */
6147 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
6148 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
6149 sqlite3_libversion(), sqlite3_sourceid());
6150 }else
6152 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
6153 const char *zDbName = nArg==2 ? azArg[1] : "main";
6154 sqlite3_vfs *pVfs = 0;
6155 if( p->db ){
6156 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
6157 if( pVfs ){
6158 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
6159 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6160 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6161 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6164 }else
6166 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
6167 sqlite3_vfs *pVfs;
6168 sqlite3_vfs *pCurrent = 0;
6169 if( p->db ){
6170 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
6172 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
6173 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
6174 pVfs==pCurrent ? " <--- CURRENT" : "");
6175 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6176 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6177 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6178 if( pVfs->pNext ){
6179 raw_printf(p->out, "-----------------------------------\n");
6182 }else
6184 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
6185 const char *zDbName = nArg==2 ? azArg[1] : "main";
6186 char *zVfsName = 0;
6187 if( p->db ){
6188 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
6189 if( zVfsName ){
6190 utf8_printf(p->out, "%s\n", zVfsName);
6191 sqlite3_free(zVfsName);
6194 }else
6196 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
6197 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
6198 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
6199 }else
6200 #endif
6202 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
6203 int j;
6204 assert( nArg<=ArraySize(azArg) );
6205 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
6206 p->colWidth[j-1] = (int)integerValue(azArg[j]);
6208 }else
6211 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
6212 " \"%s\". Enter \".help\" for help\n", azArg[0]);
6213 rc = 1;
6216 meta_command_exit:
6217 if( p->outCount ){
6218 p->outCount--;
6219 if( p->outCount==0 ) output_reset(p);
6221 return rc;
6225 ** Return TRUE if a semicolon occurs anywhere in the first N characters
6226 ** of string z[].
6228 static int line_contains_semicolon(const char *z, int N){
6229 int i;
6230 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
6231 return 0;
6235 ** Test to see if a line consists entirely of whitespace.
6237 static int _all_whitespace(const char *z){
6238 for(; *z; z++){
6239 if( IsSpace(z[0]) ) continue;
6240 if( *z=='/' && z[1]=='*' ){
6241 z += 2;
6242 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
6243 if( *z==0 ) return 0;
6244 z++;
6245 continue;
6247 if( *z=='-' && z[1]=='-' ){
6248 z += 2;
6249 while( *z && *z!='\n' ){ z++; }
6250 if( *z==0 ) return 1;
6251 continue;
6253 return 0;
6255 return 1;
6259 ** Return TRUE if the line typed in is an SQL command terminator other
6260 ** than a semi-colon. The SQL Server style "go" command is understood
6261 ** as is the Oracle "/".
6263 static int line_is_command_terminator(const char *zLine){
6264 while( IsSpace(zLine[0]) ){ zLine++; };
6265 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
6266 return 1; /* Oracle */
6268 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
6269 && _all_whitespace(&zLine[2]) ){
6270 return 1; /* SQL Server */
6272 return 0;
6276 ** Return true if zSql is a complete SQL statement. Return false if it
6277 ** ends in the middle of a string literal or C-style comment.
6279 static int line_is_complete(char *zSql, int nSql){
6280 int rc;
6281 if( zSql==0 ) return 1;
6282 zSql[nSql] = ';';
6283 zSql[nSql+1] = 0;
6284 rc = sqlite3_complete(zSql);
6285 zSql[nSql] = 0;
6286 return rc;
6290 ** Run a single line of SQL
6292 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
6293 int rc;
6294 char *zErrMsg = 0;
6296 open_db(p, 0);
6297 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
6298 BEGIN_TIMER;
6299 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
6300 END_TIMER;
6301 if( rc || zErrMsg ){
6302 char zPrefix[100];
6303 if( in!=0 || !stdin_is_interactive ){
6304 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
6305 "Error: near line %d:", startline);
6306 }else{
6307 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
6309 if( zErrMsg!=0 ){
6310 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
6311 sqlite3_free(zErrMsg);
6312 zErrMsg = 0;
6313 }else{
6314 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
6316 return 1;
6317 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
6318 raw_printf(p->out, "changes: %3d total_changes: %d\n",
6319 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
6321 return 0;
6326 ** Read input from *in and process it. If *in==0 then input
6327 ** is interactive - the user is typing it it. Otherwise, input
6328 ** is coming from a file or device. A prompt is issued and history
6329 ** is saved only if input is interactive. An interrupt signal will
6330 ** cause this routine to exit immediately, unless input is interactive.
6332 ** Return the number of errors.
6334 static int process_input(ShellState *p, FILE *in){
6335 char *zLine = 0; /* A single input line */
6336 char *zSql = 0; /* Accumulated SQL text */
6337 int nLine; /* Length of current line */
6338 int nSql = 0; /* Bytes of zSql[] used */
6339 int nAlloc = 0; /* Allocated zSql[] space */
6340 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
6341 int rc; /* Error code */
6342 int errCnt = 0; /* Number of errors seen */
6343 int lineno = 0; /* Current line number */
6344 int startline = 0; /* Line number for start of current input */
6346 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
6347 fflush(p->out);
6348 zLine = one_input_line(in, zLine, nSql>0);
6349 if( zLine==0 ){
6350 /* End of input */
6351 if( in==0 && stdin_is_interactive ) printf("\n");
6352 break;
6354 if( seenInterrupt ){
6355 if( in!=0 ) break;
6356 seenInterrupt = 0;
6358 lineno++;
6359 if( nSql==0 && _all_whitespace(zLine) ){
6360 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6361 continue;
6363 if( zLine && zLine[0]=='.' && nSql==0 ){
6364 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6365 rc = do_meta_command(zLine, p);
6366 if( rc==2 ){ /* exit requested */
6367 break;
6368 }else if( rc ){
6369 errCnt++;
6371 continue;
6373 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
6374 memcpy(zLine,";",2);
6376 nLine = strlen30(zLine);
6377 if( nSql+nLine+2>=nAlloc ){
6378 nAlloc = nSql+nLine+100;
6379 zSql = realloc(zSql, nAlloc);
6380 if( zSql==0 ){
6381 raw_printf(stderr, "Error: out of memory\n");
6382 exit(1);
6385 nSqlPrior = nSql;
6386 if( nSql==0 ){
6387 int i;
6388 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
6389 assert( nAlloc>0 && zSql!=0 );
6390 memcpy(zSql, zLine+i, nLine+1-i);
6391 startline = lineno;
6392 nSql = nLine-i;
6393 }else{
6394 zSql[nSql++] = '\n';
6395 memcpy(zSql+nSql, zLine, nLine+1);
6396 nSql += nLine;
6398 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
6399 && sqlite3_complete(zSql) ){
6400 errCnt += runOneSqlLine(p, zSql, in, startline);
6401 nSql = 0;
6402 if( p->outCount ){
6403 output_reset(p);
6404 p->outCount = 0;
6406 }else if( nSql && _all_whitespace(zSql) ){
6407 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
6408 nSql = 0;
6411 if( nSql && !_all_whitespace(zSql) ){
6412 runOneSqlLine(p, zSql, in, startline);
6414 free(zSql);
6415 free(zLine);
6416 return errCnt>0;
6420 ** Return a pathname which is the user's home directory. A
6421 ** 0 return indicates an error of some kind.
6423 static char *find_home_dir(int clearFlag){
6424 static char *home_dir = NULL;
6425 if( clearFlag ){
6426 free(home_dir);
6427 home_dir = 0;
6428 return 0;
6430 if( home_dir ) return home_dir;
6432 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
6433 && !defined(__RTP__) && !defined(_WRS_KERNEL)
6435 struct passwd *pwent;
6436 uid_t uid = getuid();
6437 if( (pwent=getpwuid(uid)) != NULL) {
6438 home_dir = pwent->pw_dir;
6441 #endif
6443 #if defined(_WIN32_WCE)
6444 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
6446 home_dir = "/";
6447 #else
6449 #if defined(_WIN32) || defined(WIN32)
6450 if (!home_dir) {
6451 home_dir = getenv("USERPROFILE");
6453 #endif
6455 if (!home_dir) {
6456 home_dir = getenv("HOME");
6459 #if defined(_WIN32) || defined(WIN32)
6460 if (!home_dir) {
6461 char *zDrive, *zPath;
6462 int n;
6463 zDrive = getenv("HOMEDRIVE");
6464 zPath = getenv("HOMEPATH");
6465 if( zDrive && zPath ){
6466 n = strlen30(zDrive) + strlen30(zPath) + 1;
6467 home_dir = malloc( n );
6468 if( home_dir==0 ) return 0;
6469 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
6470 return home_dir;
6472 home_dir = "c:\\";
6474 #endif
6476 #endif /* !_WIN32_WCE */
6478 if( home_dir ){
6479 int n = strlen30(home_dir) + 1;
6480 char *z = malloc( n );
6481 if( z ) memcpy(z, home_dir, n);
6482 home_dir = z;
6485 return home_dir;
6489 ** Read input from the file given by sqliterc_override. Or if that
6490 ** parameter is NULL, take input from ~/.sqliterc
6492 ** Returns the number of errors.
6494 static void process_sqliterc(
6495 ShellState *p, /* Configuration data */
6496 const char *sqliterc_override /* Name of config file. NULL to use default */
6498 char *home_dir = NULL;
6499 const char *sqliterc = sqliterc_override;
6500 char *zBuf = 0;
6501 FILE *in = NULL;
6503 if (sqliterc == NULL) {
6504 home_dir = find_home_dir(0);
6505 if( home_dir==0 ){
6506 raw_printf(stderr, "-- warning: cannot find home directory;"
6507 " cannot read ~/.sqliterc\n");
6508 return;
6510 sqlite3_initialize();
6511 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
6512 sqliterc = zBuf;
6514 in = fopen(sqliterc,"rb");
6515 if( in ){
6516 if( stdin_is_interactive ){
6517 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
6519 process_input(p,in);
6520 fclose(in);
6522 sqlite3_free(zBuf);
6526 ** Show available command line options
6528 static const char zOptions[] =
6529 " -ascii set output mode to 'ascii'\n"
6530 " -bail stop after hitting an error\n"
6531 " -batch force batch I/O\n"
6532 " -column set output mode to 'column'\n"
6533 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
6534 " -csv set output mode to 'csv'\n"
6535 " -echo print commands before execution\n"
6536 " -init FILENAME read/process named file\n"
6537 " -[no]header turn headers on or off\n"
6538 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6539 " -heap SIZE Size of heap for memsys3 or memsys5\n"
6540 #endif
6541 " -help show this message\n"
6542 " -html set output mode to HTML\n"
6543 " -interactive force interactive I/O\n"
6544 " -line set output mode to 'line'\n"
6545 " -list set output mode to 'list'\n"
6546 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
6547 " -mmap N default mmap size set to N\n"
6548 #ifdef SQLITE_ENABLE_MULTIPLEX
6549 " -multiplex enable the multiplexor VFS\n"
6550 #endif
6551 " -newline SEP set output row separator. Default: '\\n'\n"
6552 " -nullvalue TEXT set text string for NULL values. Default ''\n"
6553 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
6554 " -quote set output mode to 'quote'\n"
6555 " -separator SEP set output column separator. Default: '|'\n"
6556 " -stats print memory stats before each finalize\n"
6557 " -version show SQLite version\n"
6558 " -vfs NAME use NAME as the default VFS\n"
6559 #ifdef SQLITE_ENABLE_VFSTRACE
6560 " -vfstrace enable tracing of all VFS calls\n"
6561 #endif
6563 static void usage(int showDetail){
6564 utf8_printf(stderr,
6565 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
6566 "FILENAME is the name of an SQLite database. A new database is created\n"
6567 "if the file does not previously exist.\n", Argv0);
6568 if( showDetail ){
6569 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
6570 }else{
6571 raw_printf(stderr, "Use the -help option for additional information\n");
6573 exit(1);
6577 ** Initialize the state information in data
6579 static void main_init(ShellState *data) {
6580 memset(data, 0, sizeof(*data));
6581 data->normalMode = data->cMode = data->mode = MODE_List;
6582 data->autoExplain = 1;
6583 memcpy(data->colSeparator,SEP_Column, 2);
6584 memcpy(data->rowSeparator,SEP_Row, 2);
6585 data->showHeader = 0;
6586 data->shellFlgs = SHFLG_Lookaside;
6587 sqlite3_config(SQLITE_CONFIG_URI, 1);
6588 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
6589 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
6590 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
6591 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
6595 ** Output text to the console in a font that attracts extra attention.
6597 #ifdef _WIN32
6598 static void printBold(const char *zText){
6599 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
6600 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
6601 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
6602 SetConsoleTextAttribute(out,
6603 FOREGROUND_RED|FOREGROUND_INTENSITY
6605 printf("%s", zText);
6606 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
6608 #else
6609 static void printBold(const char *zText){
6610 printf("\033[1m%s\033[0m", zText);
6612 #endif
6615 ** Get the argument to an --option. Throw an error and die if no argument
6616 ** is available.
6618 static char *cmdline_option_value(int argc, char **argv, int i){
6619 if( i==argc ){
6620 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
6621 argv[0], argv[argc-1]);
6622 exit(1);
6624 return argv[i];
6627 #ifndef SQLITE_SHELL_IS_UTF8
6628 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
6629 # define SQLITE_SHELL_IS_UTF8 (0)
6630 # else
6631 # define SQLITE_SHELL_IS_UTF8 (1)
6632 # endif
6633 #endif
6635 #if SQLITE_SHELL_IS_UTF8
6636 int SQLITE_CDECL main(int argc, char **argv){
6637 #else
6638 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
6639 char **argv;
6640 #endif
6641 char *zErrMsg = 0;
6642 ShellState data;
6643 const char *zInitFile = 0;
6644 int i;
6645 int rc = 0;
6646 int warnInmemoryDb = 0;
6647 int readStdin = 1;
6648 int nCmd = 0;
6649 char **azCmd = 0;
6651 setBinaryMode(stdin, 0);
6652 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
6653 stdin_is_interactive = isatty(0);
6654 stdout_is_console = isatty(1);
6656 #if USE_SYSTEM_SQLITE+0!=1
6657 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
6658 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
6659 sqlite3_sourceid(), SQLITE_SOURCE_ID);
6660 exit(1);
6662 #endif
6663 main_init(&data);
6664 #if !SQLITE_SHELL_IS_UTF8
6665 sqlite3_initialize();
6666 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
6667 if( argv==0 ){
6668 raw_printf(stderr, "out of memory\n");
6669 exit(1);
6671 for(i=0; i<argc; i++){
6672 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
6673 if( argv[i]==0 ){
6674 raw_printf(stderr, "out of memory\n");
6675 exit(1);
6678 #endif
6679 assert( argc>=1 && argv && argv[0] );
6680 Argv0 = argv[0];
6682 /* Make sure we have a valid signal handler early, before anything
6683 ** else is done.
6685 #ifdef SIGINT
6686 signal(SIGINT, interrupt_handler);
6687 #endif
6689 #ifdef SQLITE_SHELL_DBNAME_PROC
6691 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
6692 ** of a C-function that will provide the name of the database file. Use
6693 ** this compile-time option to embed this shell program in larger
6694 ** applications. */
6695 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
6696 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
6697 warnInmemoryDb = 0;
6699 #endif
6701 /* Do an initial pass through the command-line argument to locate
6702 ** the name of the database file, the name of the initialization file,
6703 ** the size of the alternative malloc heap,
6704 ** and the first command to execute.
6706 for(i=1; i<argc; i++){
6707 char *z;
6708 z = argv[i];
6709 if( z[0]!='-' ){
6710 if( data.zDbFilename==0 ){
6711 data.zDbFilename = z;
6712 }else{
6713 /* Excesss arguments are interpreted as SQL (or dot-commands) and
6714 ** mean that nothing is read from stdin */
6715 readStdin = 0;
6716 nCmd++;
6717 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
6718 if( azCmd==0 ){
6719 raw_printf(stderr, "out of memory\n");
6720 exit(1);
6722 azCmd[nCmd-1] = z;
6725 if( z[1]=='-' ) z++;
6726 if( strcmp(z,"-separator")==0
6727 || strcmp(z,"-nullvalue")==0
6728 || strcmp(z,"-newline")==0
6729 || strcmp(z,"-cmd")==0
6731 (void)cmdline_option_value(argc, argv, ++i);
6732 }else if( strcmp(z,"-init")==0 ){
6733 zInitFile = cmdline_option_value(argc, argv, ++i);
6734 }else if( strcmp(z,"-batch")==0 ){
6735 /* Need to check for batch mode here to so we can avoid printing
6736 ** informational messages (like from process_sqliterc) before
6737 ** we do the actual processing of arguments later in a second pass.
6739 stdin_is_interactive = 0;
6740 }else if( strcmp(z,"-heap")==0 ){
6741 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6742 const char *zSize;
6743 sqlite3_int64 szHeap;
6745 zSize = cmdline_option_value(argc, argv, ++i);
6746 szHeap = integerValue(zSize);
6747 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
6748 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
6749 #else
6750 (void)cmdline_option_value(argc, argv, ++i);
6751 #endif
6752 }else if( strcmp(z,"-pagecache")==0 ){
6753 int n, sz;
6754 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6755 if( sz>70000 ) sz = 70000;
6756 if( sz<0 ) sz = 0;
6757 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6758 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
6759 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
6760 data.shellFlgs |= SHFLG_Pagecache;
6761 }else if( strcmp(z,"-lookaside")==0 ){
6762 int n, sz;
6763 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6764 if( sz<0 ) sz = 0;
6765 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6766 if( n<0 ) n = 0;
6767 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
6768 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
6769 #ifdef SQLITE_ENABLE_VFSTRACE
6770 }else if( strcmp(z,"-vfstrace")==0 ){
6771 extern int vfstrace_register(
6772 const char *zTraceName,
6773 const char *zOldVfsName,
6774 int (*xOut)(const char*,void*),
6775 void *pOutArg,
6776 int makeDefault
6778 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
6779 #endif
6780 #ifdef SQLITE_ENABLE_MULTIPLEX
6781 }else if( strcmp(z,"-multiplex")==0 ){
6782 extern int sqlite3_multiple_initialize(const char*,int);
6783 sqlite3_multiplex_initialize(0, 1);
6784 #endif
6785 }else if( strcmp(z,"-mmap")==0 ){
6786 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
6787 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
6788 }else if( strcmp(z,"-vfs")==0 ){
6789 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
6790 if( pVfs ){
6791 sqlite3_vfs_register(pVfs, 1);
6792 }else{
6793 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
6794 exit(1);
6798 if( data.zDbFilename==0 ){
6799 #ifndef SQLITE_OMIT_MEMORYDB
6800 data.zDbFilename = ":memory:";
6801 warnInmemoryDb = argc==1;
6802 #else
6803 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
6804 return 1;
6805 #endif
6807 data.out = stdout;
6809 /* Go ahead and open the database file if it already exists. If the
6810 ** file does not exist, delay opening it. This prevents empty database
6811 ** files from being created if a user mistypes the database name argument
6812 ** to the sqlite command-line tool.
6814 if( access(data.zDbFilename, 0)==0 ){
6815 open_db(&data, 0);
6818 /* Process the initialization file if there is one. If no -init option
6819 ** is given on the command line, look for a file named ~/.sqliterc and
6820 ** try to process it.
6822 process_sqliterc(&data,zInitFile);
6824 /* Make a second pass through the command-line argument and set
6825 ** options. This second pass is delayed until after the initialization
6826 ** file is processed so that the command-line arguments will override
6827 ** settings in the initialization file.
6829 for(i=1; i<argc; i++){
6830 char *z = argv[i];
6831 if( z[0]!='-' ) continue;
6832 if( z[1]=='-' ){ z++; }
6833 if( strcmp(z,"-init")==0 ){
6834 i++;
6835 }else if( strcmp(z,"-html")==0 ){
6836 data.mode = MODE_Html;
6837 }else if( strcmp(z,"-list")==0 ){
6838 data.mode = MODE_List;
6839 }else if( strcmp(z,"-quote")==0 ){
6840 data.mode = MODE_Quote;
6841 }else if( strcmp(z,"-line")==0 ){
6842 data.mode = MODE_Line;
6843 }else if( strcmp(z,"-column")==0 ){
6844 data.mode = MODE_Column;
6845 }else if( strcmp(z,"-csv")==0 ){
6846 data.mode = MODE_Csv;
6847 memcpy(data.colSeparator,",",2);
6848 }else if( strcmp(z,"-ascii")==0 ){
6849 data.mode = MODE_Ascii;
6850 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6851 SEP_Unit);
6852 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6853 SEP_Record);
6854 }else if( strcmp(z,"-separator")==0 ){
6855 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6856 "%s",cmdline_option_value(argc,argv,++i));
6857 }else if( strcmp(z,"-newline")==0 ){
6858 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6859 "%s",cmdline_option_value(argc,argv,++i));
6860 }else if( strcmp(z,"-nullvalue")==0 ){
6861 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
6862 "%s",cmdline_option_value(argc,argv,++i));
6863 }else if( strcmp(z,"-header")==0 ){
6864 data.showHeader = 1;
6865 }else if( strcmp(z,"-noheader")==0 ){
6866 data.showHeader = 0;
6867 }else if( strcmp(z,"-echo")==0 ){
6868 ShellSetFlag(&data, SHFLG_Echo);
6869 }else if( strcmp(z,"-eqp")==0 ){
6870 data.autoEQP = 1;
6871 }else if( strcmp(z,"-eqpfull")==0 ){
6872 data.autoEQP = 2;
6873 }else if( strcmp(z,"-stats")==0 ){
6874 data.statsOn = 1;
6875 }else if( strcmp(z,"-scanstats")==0 ){
6876 data.scanstatsOn = 1;
6877 }else if( strcmp(z,"-backslash")==0 ){
6878 /* Undocumented command-line option: -backslash
6879 ** Causes C-style backslash escapes to be evaluated in SQL statements
6880 ** prior to sending the SQL into SQLite. Useful for injecting
6881 ** crazy bytes in the middle of SQL statements for testing and debugging.
6883 ShellSetFlag(&data, SHFLG_Backslash);
6884 }else if( strcmp(z,"-bail")==0 ){
6885 bail_on_error = 1;
6886 }else if( strcmp(z,"-version")==0 ){
6887 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
6888 return 0;
6889 }else if( strcmp(z,"-interactive")==0 ){
6890 stdin_is_interactive = 1;
6891 }else if( strcmp(z,"-batch")==0 ){
6892 stdin_is_interactive = 0;
6893 }else if( strcmp(z,"-heap")==0 ){
6894 i++;
6895 }else if( strcmp(z,"-pagecache")==0 ){
6896 i+=2;
6897 }else if( strcmp(z,"-lookaside")==0 ){
6898 i+=2;
6899 }else if( strcmp(z,"-mmap")==0 ){
6900 i++;
6901 }else if( strcmp(z,"-vfs")==0 ){
6902 i++;
6903 #ifdef SQLITE_ENABLE_VFSTRACE
6904 }else if( strcmp(z,"-vfstrace")==0 ){
6905 i++;
6906 #endif
6907 #ifdef SQLITE_ENABLE_MULTIPLEX
6908 }else if( strcmp(z,"-multiplex")==0 ){
6909 i++;
6910 #endif
6911 }else if( strcmp(z,"-help")==0 ){
6912 usage(1);
6913 }else if( strcmp(z,"-cmd")==0 ){
6914 /* Run commands that follow -cmd first and separately from commands
6915 ** that simply appear on the command-line. This seems goofy. It would
6916 ** be better if all commands ran in the order that they appear. But
6917 ** we retain the goofy behavior for historical compatibility. */
6918 if( i==argc-1 ) break;
6919 z = cmdline_option_value(argc,argv,++i);
6920 if( z[0]=='.' ){
6921 rc = do_meta_command(z, &data);
6922 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
6923 }else{
6924 open_db(&data, 0);
6925 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
6926 if( zErrMsg!=0 ){
6927 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6928 if( bail_on_error ) return rc!=0 ? rc : 1;
6929 }else if( rc!=0 ){
6930 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
6931 if( bail_on_error ) return rc;
6934 }else{
6935 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
6936 raw_printf(stderr,"Use -help for a list of options.\n");
6937 return 1;
6939 data.cMode = data.mode;
6942 if( !readStdin ){
6943 /* Run all arguments that do not begin with '-' as if they were separate
6944 ** command-line inputs, except for the argToSkip argument which contains
6945 ** the database filename.
6947 for(i=0; i<nCmd; i++){
6948 if( azCmd[i][0]=='.' ){
6949 rc = do_meta_command(azCmd[i], &data);
6950 if( rc ) return rc==2 ? 0 : rc;
6951 }else{
6952 open_db(&data, 0);
6953 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
6954 if( zErrMsg!=0 ){
6955 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6956 return rc!=0 ? rc : 1;
6957 }else if( rc!=0 ){
6958 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
6959 return rc;
6963 free(azCmd);
6964 }else{
6965 /* Run commands received from standard input
6967 if( stdin_is_interactive ){
6968 char *zHome;
6969 char *zHistory = 0;
6970 int nHistory;
6971 printf(
6972 "SQLite version %s %.19s\n" /*extra-version-info*/
6973 "Enter \".help\" for usage hints.\n",
6974 sqlite3_libversion(), sqlite3_sourceid()
6976 if( warnInmemoryDb ){
6977 printf("Connected to a ");
6978 printBold("transient in-memory database");
6979 printf(".\nUse \".open FILENAME\" to reopen on a "
6980 "persistent database.\n");
6982 zHome = find_home_dir(0);
6983 if( zHome ){
6984 nHistory = strlen30(zHome) + 20;
6985 if( (zHistory = malloc(nHistory))!=0 ){
6986 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
6989 if( zHistory ){ shell_read_history(zHistory); }
6990 #if HAVE_READLINE || HAVE_EDITLINE
6991 rl_attempted_completion_function = readline_completion;
6992 #elif HAVE_LINENOISE
6993 linenoiseSetCompletionCallback(linenoise_completion);
6994 #endif
6995 rc = process_input(&data, 0);
6996 if( zHistory ){
6997 shell_stifle_history(2000);
6998 shell_write_history(zHistory);
6999 free(zHistory);
7001 }else{
7002 rc = process_input(&data, stdin);
7005 set_table_name(&data, 0);
7006 if( data.db ){
7007 session_close_all(&data);
7008 sqlite3_close(data.db);
7010 sqlite3_free(data.zFreeOnClose);
7011 find_home_dir(1);
7012 #if !SQLITE_SHELL_IS_UTF8
7013 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
7014 sqlite3_free(argv);
7015 #endif
7016 return rc;