2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl. That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree. That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
22 ** The author disclaims copyright to this source code. In place of
23 ** a legal notice, here is a blessing:
25 ** May you do good and not evil.
26 ** May you find forgiveness for yourself and forgive others.
27 ** May you share freely, never taking more than you give.
29 *************************************************************************
30 ** This file contains code to implement the "sqlite" command line
31 ** utility for accessing SQLite databases.
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
39 ** Warning pragmas copied from msvc.h in the core.
42 #pragma warning(disable : 4054)
43 #pragma warning(disable : 4055)
44 #pragma warning(disable : 4100)
45 #pragma warning(disable : 4127)
46 #pragma warning(disable : 4130)
47 #pragma warning(disable : 4152)
48 #pragma warning(disable : 4189)
49 #pragma warning(disable : 4206)
50 #pragma warning(disable : 4210)
51 #pragma warning(disable : 4232)
52 #pragma warning(disable : 4244)
53 #pragma warning(disable : 4305)
54 #pragma warning(disable : 4306)
55 #pragma warning(disable : 4702)
56 #pragma warning(disable : 4706)
57 #endif /* defined(_MSC_VER) */
60 ** No support for loadable extensions in VxWorks.
62 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
63 # define SQLITE_OMIT_LOAD_EXTENSION 1
67 ** Enable large-file support for fopen() and friends on unix.
69 #ifndef SQLITE_DISABLE_LFS
70 # define _LARGE_FILE 1
71 # ifndef _FILE_OFFSET_BITS
72 # define _FILE_OFFSET_BITS 64
74 # define _LARGEFILE_SOURCE 1
82 #if SQLITE_USER_AUTHENTICATION
83 # include "sqlite3userauth.h"
88 #if !defined(_WIN32) && !defined(WIN32)
90 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
94 # include <sys/types.h>
98 # include <readline/readline.h>
99 # include <readline/history.h>
103 # include <editline/readline.h>
106 #if HAVE_EDITLINE || HAVE_READLINE
108 # define shell_add_history(X) add_history(X)
109 # define shell_read_history(X) read_history(X)
110 # define shell_write_history(X) write_history(X)
111 # define shell_stifle_history(X) stifle_history(X)
112 # define shell_readline(X) readline(X)
116 # include "linenoise.h"
117 # define shell_add_history(X) linenoiseHistoryAdd(X)
118 # define shell_read_history(X) linenoiseHistoryLoad(X)
119 # define shell_write_history(X) linenoiseHistorySave(X)
120 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
121 # define shell_readline(X) linenoise(X)
125 # define shell_read_history(X)
126 # define shell_write_history(X)
127 # define shell_stifle_history(X)
129 # define SHELL_USE_LOCAL_GETLINE 1
133 #if defined(_WIN32) || defined(WIN32)
136 # define isatty(h) _isatty(h)
138 # define access(f,m) _access((f),(m))
141 # define popen _popen
143 # define pclose _pclose
145 /* Make sure isatty() has a prototype. */
146 extern int isatty(int);
148 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
149 /* popen and pclose are not C89 functions and so are
150 ** sometimes omitted from the <stdio.h> header */
151 extern FILE *popen(const char*,const char*);
152 extern int pclose(FILE*);
154 # define SQLITE_OMIT_POPEN 1
158 #if defined(_WIN32_WCE)
159 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
160 * thus we always assume that we have a console. That can be
161 * overridden with the -batch command line option.
166 /* ctype macros that work with signed characters */
167 #define IsSpace(X) isspace((unsigned char)X)
168 #define IsDigit(X) isdigit((unsigned char)X)
169 #define ToLower(X) (char)tolower((unsigned char)X)
171 #if defined(_WIN32) || defined(WIN32)
174 /* string conversion routines only needed on Win32 */
175 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
176 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
177 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
178 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
181 /* On Windows, we normally run with output mode of TEXT so that \n characters
182 ** are automatically translated into \r\n. However, this behavior needs
183 ** to be disabled in some cases (ex: when generating CSV output and when
184 ** rendering quoted strings that contain \n characters). The following
185 ** routines take care of that.
187 #if defined(_WIN32) || defined(WIN32)
188 static void setBinaryMode(FILE *file, int isOutput){
189 if( isOutput ) fflush(file);
190 _setmode(_fileno(file), _O_BINARY);
192 static void setTextMode(FILE *file, int isOutput){
193 if( isOutput ) fflush(file);
194 _setmode(_fileno(file), _O_TEXT);
197 # define setBinaryMode(X,Y)
198 # define setTextMode(X,Y)
202 /* True if the timer is enabled */
203 static int enableTimer = 0;
205 /* Return the current wall-clock time */
206 static sqlite3_int64 timeOfDay(void){
207 static sqlite3_vfs *clockVfs = 0;
209 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
210 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
211 clockVfs->xCurrentTimeInt64(clockVfs, &t);
214 clockVfs->xCurrentTime(clockVfs, &r);
215 t = (sqlite3_int64)(r*86400000.0);
220 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
221 #include <sys/time.h>
222 #include <sys/resource.h>
224 /* VxWorks does not support getrusage() as far as we can determine */
225 #if defined(_WRS_KERNEL) || defined(__RTP__)
227 struct timeval ru_utime; /* user CPU time used */
228 struct timeval ru_stime; /* system CPU time used */
230 #define getrusage(A,B) memset(B,0,sizeof(*B))
233 /* Saved resource information for the beginning of an operation */
234 static struct rusage sBegin; /* CPU time at start */
235 static sqlite3_int64 iBegin; /* Wall-clock time at start */
238 ** Begin timing an operation
240 static void beginTimer(void){
242 getrusage(RUSAGE_SELF, &sBegin);
243 iBegin = timeOfDay();
247 /* Return the difference of two time_structs in seconds */
248 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
249 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
250 (double)(pEnd->tv_sec - pStart->tv_sec);
254 ** Print the timing results.
256 static void endTimer(void){
258 sqlite3_int64 iEnd = timeOfDay();
260 getrusage(RUSAGE_SELF, &sEnd);
261 printf("Run Time: real %.3f user %f sys %f\n",
262 (iEnd - iBegin)*0.001,
263 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
264 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
268 #define BEGIN_TIMER beginTimer()
269 #define END_TIMER endTimer()
272 #elif (defined(_WIN32) || defined(WIN32))
274 /* Saved resource information for the beginning of an operation */
275 static HANDLE hProcess;
276 static FILETIME ftKernelBegin;
277 static FILETIME ftUserBegin;
278 static sqlite3_int64 ftWallBegin;
279 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
280 LPFILETIME, LPFILETIME);
281 static GETPROCTIMES getProcessTimesAddr = NULL;
284 ** Check to see if we have timer support. Return 1 if necessary
285 ** support found (or found previously).
287 static int hasTimer(void){
288 if( getProcessTimesAddr ){
291 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
292 ** versions. See if the version we are running on has it, and if it
293 ** does, save off a pointer to it and the current process handle.
295 hProcess = GetCurrentProcess();
297 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
298 if( NULL != hinstLib ){
299 getProcessTimesAddr =
300 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
301 if( NULL != getProcessTimesAddr ){
304 FreeLibrary(hinstLib);
312 ** Begin timing an operation
314 static void beginTimer(void){
315 if( enableTimer && getProcessTimesAddr ){
316 FILETIME ftCreation, ftExit;
317 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
318 &ftKernelBegin,&ftUserBegin);
319 ftWallBegin = timeOfDay();
323 /* Return the difference of two FILETIME structs in seconds */
324 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
325 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
326 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
327 return (double) ((i64End - i64Start) / 10000000.0);
331 ** Print the timing results.
333 static void endTimer(void){
334 if( enableTimer && getProcessTimesAddr){
335 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
336 sqlite3_int64 ftWallEnd = timeOfDay();
337 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
338 printf("Run Time: real %.3f user %f sys %f\n",
339 (ftWallEnd - ftWallBegin)*0.001,
340 timeDiff(&ftUserBegin, &ftUserEnd),
341 timeDiff(&ftKernelBegin, &ftKernelEnd));
345 #define BEGIN_TIMER beginTimer()
346 #define END_TIMER endTimer()
347 #define HAS_TIMER hasTimer()
356 ** Used to prevent warnings about unused parameters
358 #define UNUSED_PARAMETER(x) (void)(x)
361 ** If the following flag is set, then command execution stops
362 ** at an error if we are not interactive.
364 static int bail_on_error = 0;
367 ** Threat stdin as an interactive input if the following variable
368 ** is true. Otherwise, assume stdin is connected to a file or pipe.
370 static int stdin_is_interactive = 1;
373 ** On Windows systems we have to know if standard output is a console
374 ** in order to translate UTF-8 into MBCS. The following variable is
375 ** true if translation is required.
377 static int stdout_is_console = 1;
380 ** The following is the open SQLite database. We make a pointer
381 ** to this database a static variable so that it can be accessed
382 ** by the SIGINT handler to interrupt database processing.
384 static sqlite3 *globalDb = 0;
387 ** True if an interrupt (Control-C) has been received.
389 static volatile int seenInterrupt = 0;
392 ** This is the name of our program. It is set in main(), used
393 ** in a number of other places, mostly for error messages.
398 ** Prompt strings. Initialized in main. Settable with
399 ** .prompt main continue
401 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
402 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
405 ** Render output like fprintf(). Except, if the output is going to the
406 ** console and if this is running on a Windows machine, translate the
407 ** output from UTF-8 into MBCS.
409 #if defined(_WIN32) || defined(WIN32)
410 void utf8_printf(FILE *out, const char *zFormat, ...){
412 va_start(ap, zFormat);
413 if( stdout_is_console && (out==stdout || out==stderr) ){
414 char *z1 = sqlite3_vmprintf(zFormat, ap);
415 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
420 vfprintf(out, zFormat, ap);
424 #elif !defined(utf8_printf)
425 # define utf8_printf fprintf
429 ** Render output like fprintf(). This should not be used on anything that
430 ** includes string formatting (e.g. "%s").
432 #if !defined(raw_printf)
433 # define raw_printf fprintf
437 ** Write I/O traces to the following stream.
439 #ifdef SQLITE_ENABLE_IOTRACE
440 static FILE *iotrace = 0;
444 ** This routine works like printf in that its first argument is a
445 ** format string and subsequent arguments are values to be substituted
446 ** in place of % fields. The result of formatting this string
447 ** is written to iotrace.
449 #ifdef SQLITE_ENABLE_IOTRACE
450 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
453 if( iotrace==0 ) return;
454 va_start(ap, zFormat);
455 z = sqlite3_vmprintf(zFormat, ap);
457 utf8_printf(iotrace, "%s", z);
463 ** Output string zUtf to stream pOut as w characters. If w is negative,
464 ** then right-justify the text. W is the width in UTF-8 characters, not
465 ** in bytes. This is different from the %*.*s specification in printf
466 ** since with %*.*s the width is measured in bytes, not characters.
468 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
471 int aw = w<0 ? -w : w;
473 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
474 for(i=n=0; zUtf[i]; i++){
475 if( (zUtf[i]&0xc0)!=0x80 ){
478 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
484 utf8_printf(pOut, "%.*s", i, zUtf);
486 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
488 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
494 ** Determines if a string is a number of not.
496 static int isNumber(const char *z, int *realnum){
497 if( *z=='-' || *z=='+' ) z++;
502 if( realnum ) *realnum = 0;
503 while( IsDigit(*z) ){ z++; }
506 if( !IsDigit(*z) ) return 0;
507 while( IsDigit(*z) ){ z++; }
508 if( realnum ) *realnum = 1;
510 if( *z=='e' || *z=='E' ){
512 if( *z=='+' || *z=='-' ) z++;
513 if( !IsDigit(*z) ) return 0;
514 while( IsDigit(*z) ){ z++; }
515 if( realnum ) *realnum = 1;
521 ** Compute a string length that is limited to what can be stored in
522 ** lower 30 bits of a 32-bit signed integer.
524 static int strlen30(const char *z){
526 while( *z2 ){ z2++; }
527 return 0x3fffffff & (int)(z2 - z);
531 ** Return the length of a string in characters. Multibyte UTF8 characters
532 ** count as a single character.
534 static int strlenChar(const char *z){
537 if( (0xc0&*(z++))!=0x80 ) n++;
543 ** This routine reads a line of text from FILE in, stores
544 ** the text in memory obtained from malloc() and returns a pointer
545 ** to the text. NULL is returned at end of file, or if malloc()
548 ** If zLine is not NULL then it is a malloced buffer returned from
549 ** a previous call to this routine that may be reused.
551 static char *local_getline(char *zLine, FILE *in){
552 int nLine = zLine==0 ? 0 : 100;
557 nLine = nLine*2 + 100;
558 zLine = realloc(zLine, nLine);
559 if( zLine==0 ) return 0;
561 if( fgets(&zLine[n], nLine - n, in)==0 ){
569 while( zLine[n] ) n++;
570 if( n>0 && zLine[n-1]=='\n' ){
572 if( n>0 && zLine[n-1]=='\r' ) n--;
577 #if defined(_WIN32) || defined(WIN32)
578 /* For interactive input on Windows systems, translate the
579 ** multi-byte characterset characters into UTF-8. */
580 if( stdin_is_interactive && in==stdin ){
581 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
583 int nTrans = strlen30(zTrans)+1;
585 zLine = realloc(zLine, nTrans);
587 sqlite3_free(zTrans);
591 memcpy(zLine, zTrans, nTrans);
592 sqlite3_free(zTrans);
595 #endif /* defined(_WIN32) || defined(WIN32) */
600 ** Retrieve a single line of input text.
602 ** If in==0 then read from standard input and prompt before each line.
603 ** If isContinuation is true, then a continuation prompt is appropriate.
604 ** If isContinuation is zero, then the main prompt should be used.
606 ** If zPrior is not NULL then it is a buffer from a prior call to this
607 ** routine that can be reused.
609 ** The result is stored in space obtained from malloc() and must either
610 ** be freed by the caller or else passed back into this routine via the
611 ** zPrior argument for reuse.
613 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
617 zResult = local_getline(zPrior, in);
619 zPrompt = isContinuation ? continuePrompt : mainPrompt;
620 #if SHELL_USE_LOCAL_GETLINE
621 printf("%s", zPrompt);
623 zResult = local_getline(zPrior, stdin);
626 zResult = shell_readline(zPrompt);
627 if( zResult && *zResult ) shell_add_history(zResult);
633 ** A variable length string to which one can append text.
635 typedef struct ShellText ShellText;
643 ** Initialize and destroy a ShellText object
645 static void initText(ShellText *p){
646 memset(p, 0, sizeof(*p));
648 static void freeText(ShellText *p){
653 /* zIn is either a pointer to a NULL-terminated string in memory obtained
654 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
655 ** added to zIn, and the result returned in memory obtained from malloc().
656 ** zIn, if it was not NULL, is freed.
658 ** If the third argument, quote, is not '\0', then it is used as a
659 ** quote character for zAppend.
661 static void appendText(ShellText *p, char const *zAppend, char quote){
664 int nAppend = strlen30(zAppend);
666 len = nAppend+p->n+1;
669 for(i=0; i<nAppend; i++){
670 if( zAppend[i]==quote ) len++;
674 if( p->n+len>=p->nAlloc ){
675 p->nAlloc = p->nAlloc*2 + len + 20;
676 p->z = realloc(p->z, p->nAlloc);
678 memset(p, 0, sizeof(*p));
684 char *zCsr = p->z+p->n;
686 for(i=0; i<nAppend; i++){
687 *zCsr++ = zAppend[i];
688 if( zAppend[i]==quote ) *zCsr++ = quote;
691 p->n = (int)(zCsr - p->z);
694 memcpy(p->z+p->n, zAppend, nAppend);
701 ** Attempt to determine if identifier zName needs to be quoted, either
702 ** because it contains non-alphanumeric characters, or because it is an
703 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
704 ** that quoting is required.
706 ** Return '"' if quoting is required. Return 0 if no quoting is required.
708 static char quoteChar(const char *zName){
709 /* All SQLite keywords, in alphabetical order */
710 static const char *azKeywords[] = {
711 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
712 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
713 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
714 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
715 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
716 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
717 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
718 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
719 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
720 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
721 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
722 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
723 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
724 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
725 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
726 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
727 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
730 int i, lwr, upr, mid, c;
731 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
732 for(i=0; zName[i]; i++){
733 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
736 upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
739 c = sqlite3_stricmp(azKeywords[mid], zName);
740 if( c==0 ) return '"';
751 ** SQL function: shell_add_schema(S,X)
753 ** Add the schema name X to the CREATE statement in S and return the result.
756 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
761 ** CREATE UNIQUE INDEX
764 ** CREATE VIRTUAL TABLE
766 ** This UDF is used by the .schema command to insert the schema name of
767 ** attached databases into the middle of the sqlite_master.sql field.
769 static void shellAddSchemaName(
770 sqlite3_context *pCtx,
772 sqlite3_value **apVal
774 static const char *aPrefix[] = {
783 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
784 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
786 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
787 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
788 int n = strlen30(aPrefix[i]);
789 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
790 char cQuote = quoteChar(zSchema);
793 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
795 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
797 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
802 sqlite3_result_value(pCtx, apVal[0]);
806 ** The source code for several run-time loadable extensions is inserted
807 ** below by the ../tool/mkshellc.tcl script. Before processing that included
808 ** code, we need to override some macros to make the included program code
809 ** work here in the middle of this regular program.
811 #define SQLITE_EXTENSION_INIT1
812 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
814 /************************* Begin ../ext/misc/shathree.c ******************/
818 ** The author disclaims copyright to this source code. In place of
819 ** a legal notice, here is a blessing:
821 ** May you do good and not evil.
822 ** May you find forgiveness for yourself and forgive others.
823 ** May you share freely, never taking more than you give.
825 ******************************************************************************
827 ** This SQLite extension implements a functions that compute SHA1 hashes.
828 ** Two SQL functions are implemented:
831 ** sha3_query(Y,SIZE)
833 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
836 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
837 ** and returns a hash of their results.
839 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
840 ** is used. If SIZE is included it must be one of the integers 224, 256,
841 ** 384, or 512, to determine SHA3 hash variant that is computed.
843 SQLITE_EXTENSION_INIT1
847 typedef sqlite3_uint64 u64;
849 /******************************************************************************
853 ** Macros to determine whether the machine is big or little endian,
854 ** and whether or not that determination is run-time or compile-time.
856 ** For best performance, an attempt is made to guess at the byte-order
857 ** using C-preprocessor macros. If that is unsuccessful, or if
858 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
861 #ifndef SHA3_BYTEORDER
862 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
863 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
864 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
866 # define SHA3_BYTEORDER 1234
867 # elif defined(sparc) || defined(__ppc__)
868 # define SHA3_BYTEORDER 4321
870 # define SHA3_BYTEORDER 0
876 ** State structure for a SHA3 hash in progress
878 typedef struct SHA3Context SHA3Context;
881 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
882 unsigned char x[1600]; /* ... or 1600 bytes */
884 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
885 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
886 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
890 ** A single step of the Keccak mixing function for a 1600-bit state
892 static void KeccakF1600Step(SHA3Context *p){
894 u64 B0, B1, B2, B3, B4;
895 u64 C0, C1, C2, C3, C4;
896 u64 D0, D1, D2, D3, D4;
897 static const u64 RC[] = {
898 0x0000000000000001ULL, 0x0000000000008082ULL,
899 0x800000000000808aULL, 0x8000000080008000ULL,
900 0x000000000000808bULL, 0x0000000080000001ULL,
901 0x8000000080008081ULL, 0x8000000000008009ULL,
902 0x000000000000008aULL, 0x0000000000000088ULL,
903 0x0000000080008009ULL, 0x000000008000000aULL,
904 0x000000008000808bULL, 0x800000000000008bULL,
905 0x8000000000008089ULL, 0x8000000000008003ULL,
906 0x8000000000008002ULL, 0x8000000000000080ULL,
907 0x000000000000800aULL, 0x800000008000000aULL,
908 0x8000000080008081ULL, 0x8000000000008080ULL,
909 0x0000000080000001ULL, 0x8000000080008008ULL
911 # define A00 (p->u.s[0])
912 # define A01 (p->u.s[1])
913 # define A02 (p->u.s[2])
914 # define A03 (p->u.s[3])
915 # define A04 (p->u.s[4])
916 # define A10 (p->u.s[5])
917 # define A11 (p->u.s[6])
918 # define A12 (p->u.s[7])
919 # define A13 (p->u.s[8])
920 # define A14 (p->u.s[9])
921 # define A20 (p->u.s[10])
922 # define A21 (p->u.s[11])
923 # define A22 (p->u.s[12])
924 # define A23 (p->u.s[13])
925 # define A24 (p->u.s[14])
926 # define A30 (p->u.s[15])
927 # define A31 (p->u.s[16])
928 # define A32 (p->u.s[17])
929 # define A33 (p->u.s[18])
930 # define A34 (p->u.s[19])
931 # define A40 (p->u.s[20])
932 # define A41 (p->u.s[21])
933 # define A42 (p->u.s[22])
934 # define A43 (p->u.s[23])
935 # define A44 (p->u.s[24])
936 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
938 for(i=0; i<24; i+=4){
939 C0 = A00^A10^A20^A30^A40;
940 C1 = A01^A11^A21^A31^A41;
941 C2 = A02^A12^A22^A32^A42;
942 C3 = A03^A13^A23^A33^A43;
943 C4 = A04^A14^A24^A34^A44;
944 D0 = C4^ROL64(C1, 1);
945 D1 = C0^ROL64(C2, 1);
946 D2 = C1^ROL64(C3, 1);
947 D3 = C2^ROL64(C4, 1);
948 D4 = C3^ROL64(C0, 1);
951 B1 = ROL64((A11^D1), 44);
952 B2 = ROL64((A22^D2), 43);
953 B3 = ROL64((A33^D3), 21);
954 B4 = ROL64((A44^D4), 14);
955 A00 = B0 ^((~B1)& B2 );
957 A11 = B1 ^((~B2)& B3 );
958 A22 = B2 ^((~B3)& B4 );
959 A33 = B3 ^((~B4)& B0 );
960 A44 = B4 ^((~B0)& B1 );
962 B2 = ROL64((A20^D0), 3);
963 B3 = ROL64((A31^D1), 45);
964 B4 = ROL64((A42^D2), 61);
965 B0 = ROL64((A03^D3), 28);
966 B1 = ROL64((A14^D4), 20);
967 A20 = B0 ^((~B1)& B2 );
968 A31 = B1 ^((~B2)& B3 );
969 A42 = B2 ^((~B3)& B4 );
970 A03 = B3 ^((~B4)& B0 );
971 A14 = B4 ^((~B0)& B1 );
973 B4 = ROL64((A40^D0), 18);
974 B0 = ROL64((A01^D1), 1);
975 B1 = ROL64((A12^D2), 6);
976 B2 = ROL64((A23^D3), 25);
977 B3 = ROL64((A34^D4), 8);
978 A40 = B0 ^((~B1)& B2 );
979 A01 = B1 ^((~B2)& B3 );
980 A12 = B2 ^((~B3)& B4 );
981 A23 = B3 ^((~B4)& B0 );
982 A34 = B4 ^((~B0)& B1 );
984 B1 = ROL64((A10^D0), 36);
985 B2 = ROL64((A21^D1), 10);
986 B3 = ROL64((A32^D2), 15);
987 B4 = ROL64((A43^D3), 56);
988 B0 = ROL64((A04^D4), 27);
989 A10 = B0 ^((~B1)& B2 );
990 A21 = B1 ^((~B2)& B3 );
991 A32 = B2 ^((~B3)& B4 );
992 A43 = B3 ^((~B4)& B0 );
993 A04 = B4 ^((~B0)& B1 );
995 B3 = ROL64((A30^D0), 41);
996 B4 = ROL64((A41^D1), 2);
997 B0 = ROL64((A02^D2), 62);
998 B1 = ROL64((A13^D3), 55);
999 B2 = ROL64((A24^D4), 39);
1000 A30 = B0 ^((~B1)& B2 );
1001 A41 = B1 ^((~B2)& B3 );
1002 A02 = B2 ^((~B3)& B4 );
1003 A13 = B3 ^((~B4)& B0 );
1004 A24 = B4 ^((~B0)& B1 );
1006 C0 = A00^A20^A40^A10^A30;
1007 C1 = A11^A31^A01^A21^A41;
1008 C2 = A22^A42^A12^A32^A02;
1009 C3 = A33^A03^A23^A43^A13;
1010 C4 = A44^A14^A34^A04^A24;
1011 D0 = C4^ROL64(C1, 1);
1012 D1 = C0^ROL64(C2, 1);
1013 D2 = C1^ROL64(C3, 1);
1014 D3 = C2^ROL64(C4, 1);
1015 D4 = C3^ROL64(C0, 1);
1018 B1 = ROL64((A31^D1), 44);
1019 B2 = ROL64((A12^D2), 43);
1020 B3 = ROL64((A43^D3), 21);
1021 B4 = ROL64((A24^D4), 14);
1022 A00 = B0 ^((~B1)& B2 );
1024 A31 = B1 ^((~B2)& B3 );
1025 A12 = B2 ^((~B3)& B4 );
1026 A43 = B3 ^((~B4)& B0 );
1027 A24 = B4 ^((~B0)& B1 );
1029 B2 = ROL64((A40^D0), 3);
1030 B3 = ROL64((A21^D1), 45);
1031 B4 = ROL64((A02^D2), 61);
1032 B0 = ROL64((A33^D3), 28);
1033 B1 = ROL64((A14^D4), 20);
1034 A40 = B0 ^((~B1)& B2 );
1035 A21 = B1 ^((~B2)& B3 );
1036 A02 = B2 ^((~B3)& B4 );
1037 A33 = B3 ^((~B4)& B0 );
1038 A14 = B4 ^((~B0)& B1 );
1040 B4 = ROL64((A30^D0), 18);
1041 B0 = ROL64((A11^D1), 1);
1042 B1 = ROL64((A42^D2), 6);
1043 B2 = ROL64((A23^D3), 25);
1044 B3 = ROL64((A04^D4), 8);
1045 A30 = B0 ^((~B1)& B2 );
1046 A11 = B1 ^((~B2)& B3 );
1047 A42 = B2 ^((~B3)& B4 );
1048 A23 = B3 ^((~B4)& B0 );
1049 A04 = B4 ^((~B0)& B1 );
1051 B1 = ROL64((A20^D0), 36);
1052 B2 = ROL64((A01^D1), 10);
1053 B3 = ROL64((A32^D2), 15);
1054 B4 = ROL64((A13^D3), 56);
1055 B0 = ROL64((A44^D4), 27);
1056 A20 = B0 ^((~B1)& B2 );
1057 A01 = B1 ^((~B2)& B3 );
1058 A32 = B2 ^((~B3)& B4 );
1059 A13 = B3 ^((~B4)& B0 );
1060 A44 = B4 ^((~B0)& B1 );
1062 B3 = ROL64((A10^D0), 41);
1063 B4 = ROL64((A41^D1), 2);
1064 B0 = ROL64((A22^D2), 62);
1065 B1 = ROL64((A03^D3), 55);
1066 B2 = ROL64((A34^D4), 39);
1067 A10 = B0 ^((~B1)& B2 );
1068 A41 = B1 ^((~B2)& B3 );
1069 A22 = B2 ^((~B3)& B4 );
1070 A03 = B3 ^((~B4)& B0 );
1071 A34 = B4 ^((~B0)& B1 );
1073 C0 = A00^A40^A30^A20^A10;
1074 C1 = A31^A21^A11^A01^A41;
1075 C2 = A12^A02^A42^A32^A22;
1076 C3 = A43^A33^A23^A13^A03;
1077 C4 = A24^A14^A04^A44^A34;
1078 D0 = C4^ROL64(C1, 1);
1079 D1 = C0^ROL64(C2, 1);
1080 D2 = C1^ROL64(C3, 1);
1081 D3 = C2^ROL64(C4, 1);
1082 D4 = C3^ROL64(C0, 1);
1085 B1 = ROL64((A21^D1), 44);
1086 B2 = ROL64((A42^D2), 43);
1087 B3 = ROL64((A13^D3), 21);
1088 B4 = ROL64((A34^D4), 14);
1089 A00 = B0 ^((~B1)& B2 );
1091 A21 = B1 ^((~B2)& B3 );
1092 A42 = B2 ^((~B3)& B4 );
1093 A13 = B3 ^((~B4)& B0 );
1094 A34 = B4 ^((~B0)& B1 );
1096 B2 = ROL64((A30^D0), 3);
1097 B3 = ROL64((A01^D1), 45);
1098 B4 = ROL64((A22^D2), 61);
1099 B0 = ROL64((A43^D3), 28);
1100 B1 = ROL64((A14^D4), 20);
1101 A30 = B0 ^((~B1)& B2 );
1102 A01 = B1 ^((~B2)& B3 );
1103 A22 = B2 ^((~B3)& B4 );
1104 A43 = B3 ^((~B4)& B0 );
1105 A14 = B4 ^((~B0)& B1 );
1107 B4 = ROL64((A10^D0), 18);
1108 B0 = ROL64((A31^D1), 1);
1109 B1 = ROL64((A02^D2), 6);
1110 B2 = ROL64((A23^D3), 25);
1111 B3 = ROL64((A44^D4), 8);
1112 A10 = B0 ^((~B1)& B2 );
1113 A31 = B1 ^((~B2)& B3 );
1114 A02 = B2 ^((~B3)& B4 );
1115 A23 = B3 ^((~B4)& B0 );
1116 A44 = B4 ^((~B0)& B1 );
1118 B1 = ROL64((A40^D0), 36);
1119 B2 = ROL64((A11^D1), 10);
1120 B3 = ROL64((A32^D2), 15);
1121 B4 = ROL64((A03^D3), 56);
1122 B0 = ROL64((A24^D4), 27);
1123 A40 = B0 ^((~B1)& B2 );
1124 A11 = B1 ^((~B2)& B3 );
1125 A32 = B2 ^((~B3)& B4 );
1126 A03 = B3 ^((~B4)& B0 );
1127 A24 = B4 ^((~B0)& B1 );
1129 B3 = ROL64((A20^D0), 41);
1130 B4 = ROL64((A41^D1), 2);
1131 B0 = ROL64((A12^D2), 62);
1132 B1 = ROL64((A33^D3), 55);
1133 B2 = ROL64((A04^D4), 39);
1134 A20 = B0 ^((~B1)& B2 );
1135 A41 = B1 ^((~B2)& B3 );
1136 A12 = B2 ^((~B3)& B4 );
1137 A33 = B3 ^((~B4)& B0 );
1138 A04 = B4 ^((~B0)& B1 );
1140 C0 = A00^A30^A10^A40^A20;
1141 C1 = A21^A01^A31^A11^A41;
1142 C2 = A42^A22^A02^A32^A12;
1143 C3 = A13^A43^A23^A03^A33;
1144 C4 = A34^A14^A44^A24^A04;
1145 D0 = C4^ROL64(C1, 1);
1146 D1 = C0^ROL64(C2, 1);
1147 D2 = C1^ROL64(C3, 1);
1148 D3 = C2^ROL64(C4, 1);
1149 D4 = C3^ROL64(C0, 1);
1152 B1 = ROL64((A01^D1), 44);
1153 B2 = ROL64((A02^D2), 43);
1154 B3 = ROL64((A03^D3), 21);
1155 B4 = ROL64((A04^D4), 14);
1156 A00 = B0 ^((~B1)& B2 );
1158 A01 = B1 ^((~B2)& B3 );
1159 A02 = B2 ^((~B3)& B4 );
1160 A03 = B3 ^((~B4)& B0 );
1161 A04 = B4 ^((~B0)& B1 );
1163 B2 = ROL64((A10^D0), 3);
1164 B3 = ROL64((A11^D1), 45);
1165 B4 = ROL64((A12^D2), 61);
1166 B0 = ROL64((A13^D3), 28);
1167 B1 = ROL64((A14^D4), 20);
1168 A10 = B0 ^((~B1)& B2 );
1169 A11 = B1 ^((~B2)& B3 );
1170 A12 = B2 ^((~B3)& B4 );
1171 A13 = B3 ^((~B4)& B0 );
1172 A14 = B4 ^((~B0)& B1 );
1174 B4 = ROL64((A20^D0), 18);
1175 B0 = ROL64((A21^D1), 1);
1176 B1 = ROL64((A22^D2), 6);
1177 B2 = ROL64((A23^D3), 25);
1178 B3 = ROL64((A24^D4), 8);
1179 A20 = B0 ^((~B1)& B2 );
1180 A21 = B1 ^((~B2)& B3 );
1181 A22 = B2 ^((~B3)& B4 );
1182 A23 = B3 ^((~B4)& B0 );
1183 A24 = B4 ^((~B0)& B1 );
1185 B1 = ROL64((A30^D0), 36);
1186 B2 = ROL64((A31^D1), 10);
1187 B3 = ROL64((A32^D2), 15);
1188 B4 = ROL64((A33^D3), 56);
1189 B0 = ROL64((A34^D4), 27);
1190 A30 = B0 ^((~B1)& B2 );
1191 A31 = B1 ^((~B2)& B3 );
1192 A32 = B2 ^((~B3)& B4 );
1193 A33 = B3 ^((~B4)& B0 );
1194 A34 = B4 ^((~B0)& B1 );
1196 B3 = ROL64((A40^D0), 41);
1197 B4 = ROL64((A41^D1), 2);
1198 B0 = ROL64((A42^D2), 62);
1199 B1 = ROL64((A43^D3), 55);
1200 B2 = ROL64((A44^D4), 39);
1201 A40 = B0 ^((~B1)& B2 );
1202 A41 = B1 ^((~B2)& B3 );
1203 A42 = B2 ^((~B3)& B4 );
1204 A43 = B3 ^((~B4)& B0 );
1205 A44 = B4 ^((~B0)& B1 );
1210 ** Initialize a new hash. iSize determines the size of the hash
1211 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1212 ** can be zero to use the default hash size of 256 bits.
1214 static void SHA3Init(SHA3Context *p, int iSize){
1215 memset(p, 0, sizeof(*p));
1216 if( iSize>=128 && iSize<=512 ){
1217 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1219 p->nRate = (1600 - 2*256)/8;
1221 #if SHA3_BYTEORDER==1234
1222 /* Known to be little-endian at compile-time. No-op */
1223 #elif SHA3_BYTEORDER==4321
1224 p->ixMask = 7; /* Big-endian */
1227 static unsigned int one = 1;
1228 if( 1==*(unsigned char*)&one ){
1229 /* Little endian. No byte swapping. */
1232 /* Big endian. Byte swap. */
1240 ** Make consecutive calls to the SHA3Update function to add new content
1243 static void SHA3Update(
1245 const unsigned char *aData,
1249 #if SHA3_BYTEORDER==1234
1250 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1251 for(; i+7<nData; i+=8){
1252 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1254 if( p->nLoaded>=p->nRate ){
1261 for(; i<nData; i++){
1262 #if SHA3_BYTEORDER==1234
1263 p->u.x[p->nLoaded] ^= aData[i];
1264 #elif SHA3_BYTEORDER==4321
1265 p->u.x[p->nLoaded^0x07] ^= aData[i];
1267 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1270 if( p->nLoaded==p->nRate ){
1278 ** After all content has been added, invoke SHA3Final() to compute
1279 ** the final hash. The function returns a pointer to the binary
1282 static unsigned char *SHA3Final(SHA3Context *p){
1284 if( p->nLoaded==p->nRate-1 ){
1285 const unsigned char c1 = 0x86;
1286 SHA3Update(p, &c1, 1);
1288 const unsigned char c2 = 0x06;
1289 const unsigned char c3 = 0x80;
1290 SHA3Update(p, &c2, 1);
1291 p->nLoaded = p->nRate - 1;
1292 SHA3Update(p, &c3, 1);
1294 for(i=0; i<p->nRate; i++){
1295 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1297 return &p->u.x[p->nRate];
1299 /* End of the hashing logic
1300 *****************************************************************************/
1303 ** Implementation of the sha3(X,SIZE) function.
1305 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1306 ** size is 256. If X is a BLOB, it is hashed as is.
1307 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1308 ** and the string is hashed without the trailing 0x00 terminator. The hash
1309 ** of a NULL value is NULL.
1311 static void sha3Func(
1312 sqlite3_context *context,
1314 sqlite3_value **argv
1317 int eType = sqlite3_value_type(argv[0]);
1318 int nByte = sqlite3_value_bytes(argv[0]);
1323 iSize = sqlite3_value_int(argv[1]);
1324 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1325 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1330 if( eType==SQLITE_NULL ) return;
1331 SHA3Init(&cx, iSize);
1332 if( eType==SQLITE_BLOB ){
1333 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1335 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1337 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1340 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1341 ** of 50 bytes and add it to the hash.
1343 static void hash_step_vformat(
1344 SHA3Context *p, /* Add content to this context */
1345 const char *zFormat,
1351 va_start(ap, zFormat);
1352 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1354 n = (int)strlen(zBuf);
1355 SHA3Update(p, (unsigned char*)zBuf, n);
1359 ** Implementation of the sha3_query(SQL,SIZE) function.
1361 ** This function compiles and runs the SQL statement(s) given in the
1362 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1365 ** The format of the byte stream that is hashed is summarized as follows:
1375 ** <sql> is the original SQL text for each statement run and <n> is
1376 ** the size of that text. The SQL text is UTF-8. A single R character
1377 ** occurs before the start of each row. N means a NULL value.
1378 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1379 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1380 ** B means blobs of <size> bytes. T means text rendered as <size>
1381 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1384 ** For each SQL statement in the X input, there is one S segment. Each
1385 ** S segment is followed by zero or more R segments, one for each row in the
1386 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1387 ** one for each column in the result set. Segments are concatentated directly
1388 ** with no delimiters of any kind.
1390 static void sha3QueryFunc(
1391 sqlite3_context *context,
1393 sqlite3_value **argv
1395 sqlite3 *db = sqlite3_context_db_handle(context);
1396 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1397 sqlite3_stmt *pStmt = 0;
1398 int nCol; /* Number of columns in the result set */
1399 int i; /* Loop counter */
1409 iSize = sqlite3_value_int(argv[1]);
1410 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1411 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1416 if( zSql==0 ) return;
1417 SHA3Init(&cx, iSize);
1419 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1421 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1422 zSql, sqlite3_errmsg(db));
1423 sqlite3_finalize(pStmt);
1424 sqlite3_result_error(context, zMsg, -1);
1428 if( !sqlite3_stmt_readonly(pStmt) ){
1429 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1430 sqlite3_finalize(pStmt);
1431 sqlite3_result_error(context, zMsg, -1);
1435 nCol = sqlite3_column_count(pStmt);
1436 z = sqlite3_sql(pStmt);
1438 hash_step_vformat(&cx,"S%d:",n);
1439 SHA3Update(&cx,(unsigned char*)z,n);
1441 /* Compute a hash over the result of the query */
1442 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1443 SHA3Update(&cx,(const unsigned char*)"R",1);
1444 for(i=0; i<nCol; i++){
1445 switch( sqlite3_column_type(pStmt,i) ){
1447 SHA3Update(&cx, (const unsigned char*)"N",1);
1450 case SQLITE_INTEGER: {
1454 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1456 for(j=8; j>=1; j--){
1461 SHA3Update(&cx, x, 9);
1464 case SQLITE_FLOAT: {
1468 double r = sqlite3_column_double(pStmt,i);
1470 for(j=8; j>=1; j--){
1475 SHA3Update(&cx,x,9);
1479 int n2 = sqlite3_column_bytes(pStmt, i);
1480 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1481 hash_step_vformat(&cx,"T%d:",n2);
1482 SHA3Update(&cx, z2, n2);
1486 int n2 = sqlite3_column_bytes(pStmt, i);
1487 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1488 hash_step_vformat(&cx,"B%d:",n2);
1489 SHA3Update(&cx, z2, n2);
1495 sqlite3_finalize(pStmt);
1497 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1502 __declspec(dllexport)
1504 int sqlite3_shathree_init(
1507 const sqlite3_api_routines *pApi
1510 SQLITE_EXTENSION_INIT2(pApi);
1511 (void)pzErrMsg; /* Unused parameter */
1512 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
1514 if( rc==SQLITE_OK ){
1515 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
1518 if( rc==SQLITE_OK ){
1519 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
1520 sha3QueryFunc, 0, 0);
1522 if( rc==SQLITE_OK ){
1523 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
1524 sha3QueryFunc, 0, 0);
1529 /************************* End ../ext/misc/shathree.c ********************/
1530 /************************* Begin ../ext/misc/fileio.c ******************/
1534 ** The author disclaims copyright to this source code. In place of
1535 ** a legal notice, here is a blessing:
1537 ** May you do good and not evil.
1538 ** May you find forgiveness for yourself and forgive others.
1539 ** May you share freely, never taking more than you give.
1541 ******************************************************************************
1543 ** This SQLite extension implements SQL functions readfile() and
1546 SQLITE_EXTENSION_INIT1
1550 ** Implementation of the "readfile(X)" SQL function. The entire content
1551 ** of the file named X is read and returned as a BLOB. NULL is returned
1552 ** if the file does not exist or is unreadable.
1554 static void readfileFunc(
1555 sqlite3_context *context,
1557 sqlite3_value **argv
1564 (void)(argc); /* Unused parameter */
1565 zName = (const char*)sqlite3_value_text(argv[0]);
1566 if( zName==0 ) return;
1567 in = fopen(zName, "rb");
1569 fseek(in, 0, SEEK_END);
1572 pBuf = sqlite3_malloc( nIn );
1573 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
1574 sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
1582 ** Implementation of the "writefile(X,Y)" SQL function. The argument Y
1583 ** is written into file X. The number of bytes written is returned. Or
1584 ** NULL is returned if something goes wrong, such as being unable to open
1585 ** file X for writing.
1587 static void writefileFunc(
1588 sqlite3_context *context,
1590 sqlite3_value **argv
1597 (void)(argc); /* Unused parameter */
1598 zFile = (const char*)sqlite3_value_text(argv[0]);
1599 if( zFile==0 ) return;
1600 out = fopen(zFile, "wb");
1601 if( out==0 ) return;
1602 z = (const char*)sqlite3_value_blob(argv[1]);
1606 rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
1609 sqlite3_result_int64(context, rc);
1614 __declspec(dllexport)
1616 int sqlite3_fileio_init(
1619 const sqlite3_api_routines *pApi
1622 SQLITE_EXTENSION_INIT2(pApi);
1623 (void)pzErrMsg; /* Unused parameter */
1624 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
1625 readfileFunc, 0, 0);
1626 if( rc==SQLITE_OK ){
1627 rc = sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
1628 writefileFunc, 0, 0);
1633 /************************* End ../ext/misc/fileio.c ********************/
1634 /************************* Begin ../ext/misc/completion.c ******************/
1638 ** The author disclaims copyright to this source code. In place of
1639 ** a legal notice, here is a blessing:
1641 ** May you do good and not evil.
1642 ** May you find forgiveness for yourself and forgive others.
1643 ** May you share freely, never taking more than you give.
1645 *************************************************************************
1647 ** This file implements an eponymous virtual table that returns suggested
1648 ** completions for a partial SQL input.
1652 ** SELECT DISTINCT candidate COLLATE nocase
1653 ** FROM completion($prefix,$wholeline)
1656 ** The two query parameters are optional. $prefix is the text of the
1657 ** current word being typed and that is to be completed. $wholeline is
1658 ** the complete input line, used for context.
1660 ** The raw completion() table might return the same candidate multiple
1661 ** times, for example if the same column name is used to two or more
1662 ** tables. And the candidates are returned in an arbitrary order. Hence,
1663 ** the DISTINCT and ORDER BY are recommended.
1665 ** This virtual table operates at the speed of human typing, and so there
1666 ** is no attempt to make it fast. Even a slow implementation will be much
1667 ** faster than any human can type.
1670 SQLITE_EXTENSION_INIT1
1675 #ifndef SQLITE_OMIT_VIRTUALTABLE
1677 /* completion_vtab is a subclass of sqlite3_vtab which will
1678 ** serve as the underlying representation of a completion virtual table
1680 typedef struct completion_vtab completion_vtab;
1681 struct completion_vtab {
1682 sqlite3_vtab base; /* Base class - must be first */
1683 sqlite3 *db; /* Database connection for this completion vtab */
1686 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
1687 ** serve as the underlying representation of a cursor that scans
1688 ** over rows of the result
1690 typedef struct completion_cursor completion_cursor;
1691 struct completion_cursor {
1692 sqlite3_vtab_cursor base; /* Base class - must be first */
1693 sqlite3 *db; /* Database connection for this cursor */
1694 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
1695 char *zPrefix; /* The prefix for the word we want to complete */
1696 char *zLine; /* The whole that we want to complete */
1697 const char *zCurrentRow; /* Current output row */
1698 sqlite3_stmt *pStmt; /* Current statement */
1699 sqlite3_int64 iRowid; /* The rowid */
1700 int ePhase; /* Current phase */
1701 int j; /* inter-phase counter */
1704 /* Values for ePhase:
1706 #define COMPLETION_FIRST_PHASE 1
1707 #define COMPLETION_KEYWORDS 1
1708 #define COMPLETION_PRAGMAS 2
1709 #define COMPLETION_FUNCTIONS 3
1710 #define COMPLETION_COLLATIONS 4
1711 #define COMPLETION_INDEXES 5
1712 #define COMPLETION_TRIGGERS 6
1713 #define COMPLETION_DATABASES 7
1714 #define COMPLETION_TABLES 8
1715 #define COMPLETION_COLUMNS 9
1716 #define COMPLETION_MODULES 10
1717 #define COMPLETION_EOF 11
1720 ** The completionConnect() method is invoked to create a new
1721 ** completion_vtab that describes the completion virtual table.
1723 ** Think of this routine as the constructor for completion_vtab objects.
1725 ** All this routine needs to do is:
1727 ** (1) Allocate the completion_vtab object and initialize all fields.
1729 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
1730 ** result set of queries against completion will look like.
1732 static int completionConnect(
1735 int argc, const char *const*argv,
1736 sqlite3_vtab **ppVtab,
1739 completion_vtab *pNew;
1742 (void)(pAux); /* Unused parameter */
1743 (void)(argc); /* Unused parameter */
1744 (void)(argv); /* Unused parameter */
1745 (void)(pzErr); /* Unused parameter */
1747 /* Column numbers */
1748 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
1749 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
1750 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
1751 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
1753 rc = sqlite3_declare_vtab(db,
1756 " prefix TEXT HIDDEN,"
1757 " wholeline TEXT HIDDEN,"
1758 " phase INT HIDDEN" /* Used for debugging only */
1760 if( rc==SQLITE_OK ){
1761 pNew = sqlite3_malloc( sizeof(*pNew) );
1762 *ppVtab = (sqlite3_vtab*)pNew;
1763 if( pNew==0 ) return SQLITE_NOMEM;
1764 memset(pNew, 0, sizeof(*pNew));
1771 ** This method is the destructor for completion_cursor objects.
1773 static int completionDisconnect(sqlite3_vtab *pVtab){
1774 sqlite3_free(pVtab);
1779 ** Constructor for a new completion_cursor object.
1781 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
1782 completion_cursor *pCur;
1783 pCur = sqlite3_malloc( sizeof(*pCur) );
1784 if( pCur==0 ) return SQLITE_NOMEM;
1785 memset(pCur, 0, sizeof(*pCur));
1786 pCur->db = ((completion_vtab*)p)->db;
1787 *ppCursor = &pCur->base;
1792 ** Reset the completion_cursor.
1794 static void completionCursorReset(completion_cursor *pCur){
1795 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
1796 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
1797 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
1802 ** Destructor for a completion_cursor.
1804 static int completionClose(sqlite3_vtab_cursor *cur){
1805 completionCursorReset((completion_cursor*)cur);
1811 ** All SQL keywords understood by SQLite
1813 static const char *completionKwrds[] = {
1814 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
1815 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
1816 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
1817 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
1818 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
1819 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
1820 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
1821 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
1822 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
1823 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
1824 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
1825 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
1826 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
1827 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
1828 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
1829 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
1830 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
1833 #define completionKwCount \
1834 (int)(sizeof(completionKwrds)/sizeof(completionKwrds[0]))
1837 ** Advance a completion_cursor to its next row of output.
1839 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
1840 ** record the current state of the scan. This routine sets ->zCurrentRow
1841 ** to the current row of output and then returns. If no more rows remain,
1842 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
1843 ** table that has reached the end of its scan.
1845 ** The current implementation just lists potential identifiers and
1846 ** keywords and filters them by zPrefix. Future enhancements should
1847 ** take zLine into account to try to restrict the set of identifiers and
1848 ** keywords based on what would be legal at the current point of input.
1850 static int completionNext(sqlite3_vtab_cursor *cur){
1851 completion_cursor *pCur = (completion_cursor*)cur;
1852 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
1853 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
1855 while( pCur->ePhase!=COMPLETION_EOF ){
1856 switch( pCur->ePhase ){
1857 case COMPLETION_KEYWORDS: {
1858 if( pCur->j >= completionKwCount ){
1859 pCur->zCurrentRow = 0;
1860 pCur->ePhase = COMPLETION_DATABASES;
1862 pCur->zCurrentRow = completionKwrds[pCur->j++];
1867 case COMPLETION_DATABASES: {
1868 if( pCur->pStmt==0 ){
1869 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
1873 eNextPhase = COMPLETION_TABLES;
1876 case COMPLETION_TABLES: {
1877 if( pCur->pStmt==0 ){
1880 const char *zSep = "";
1881 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1882 while( sqlite3_step(pS2)==SQLITE_ROW ){
1883 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1884 zSql = sqlite3_mprintf(
1886 "SELECT name FROM \"%w\".sqlite_master"
1887 " WHERE type='table'",
1890 if( zSql==0 ) return SQLITE_NOMEM;
1893 sqlite3_finalize(pS2);
1894 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1898 eNextPhase = COMPLETION_COLUMNS;
1901 case COMPLETION_COLUMNS: {
1902 if( pCur->pStmt==0 ){
1905 const char *zSep = "";
1906 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
1907 while( sqlite3_step(pS2)==SQLITE_ROW ){
1908 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
1909 zSql = sqlite3_mprintf(
1911 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
1912 " JOIN pragma_table_info(sm.name,%Q) AS pti"
1913 " WHERE sm.type='table'",
1914 zSql, zSep, zDb, zDb
1916 if( zSql==0 ) return SQLITE_NOMEM;
1919 sqlite3_finalize(pS2);
1920 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
1924 eNextPhase = COMPLETION_EOF;
1929 /* This case is when the phase presets zCurrentRow */
1930 if( pCur->zCurrentRow==0 ) continue;
1932 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
1933 /* Extract the next row of content */
1934 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
1936 /* When all rows are finished, advance to the next phase */
1937 sqlite3_finalize(pCur->pStmt);
1939 pCur->ePhase = eNextPhase;
1943 if( pCur->nPrefix==0 ) break;
1944 if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
1953 ** Return values of columns for the row at which the completion_cursor
1954 ** is currently pointing.
1956 static int completionColumn(
1957 sqlite3_vtab_cursor *cur, /* The cursor */
1958 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
1959 int i /* Which column to return */
1961 completion_cursor *pCur = (completion_cursor*)cur;
1963 case COMPLETION_COLUMN_CANDIDATE: {
1964 sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
1967 case COMPLETION_COLUMN_PREFIX: {
1968 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
1971 case COMPLETION_COLUMN_WHOLELINE: {
1972 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
1975 case COMPLETION_COLUMN_PHASE: {
1976 sqlite3_result_int(ctx, pCur->ePhase);
1984 ** Return the rowid for the current row. In this implementation, the
1985 ** rowid is the same as the output value.
1987 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
1988 completion_cursor *pCur = (completion_cursor*)cur;
1989 *pRowid = pCur->iRowid;
1994 ** Return TRUE if the cursor has been moved off of the last
1997 static int completionEof(sqlite3_vtab_cursor *cur){
1998 completion_cursor *pCur = (completion_cursor*)cur;
1999 return pCur->ePhase >= COMPLETION_EOF;
2003 ** This method is called to "rewind" the completion_cursor object back
2004 ** to the first row of output. This method is always called at least
2005 ** once prior to any call to completionColumn() or completionRowid() or
2008 static int completionFilter(
2009 sqlite3_vtab_cursor *pVtabCursor,
2010 int idxNum, const char *idxStr,
2011 int argc, sqlite3_value **argv
2013 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
2015 (void)(idxStr); /* Unused parameter */
2016 (void)(argc); /* Unused parameter */
2017 completionCursorReset(pCur);
2019 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
2020 if( pCur->nPrefix>0 ){
2021 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2022 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2027 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
2028 if( pCur->nLine>0 ){
2029 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
2030 if( pCur->zLine==0 ) return SQLITE_NOMEM;
2034 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
2035 int i = pCur->nLine;
2036 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
2039 pCur->nPrefix = pCur->nLine - i;
2040 if( pCur->nPrefix>0 ){
2041 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
2042 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
2046 pCur->ePhase = COMPLETION_FIRST_PHASE;
2047 return completionNext(pVtabCursor);
2051 ** SQLite will invoke this method one or more times while planning a query
2052 ** that uses the completion virtual table. This routine needs to create
2053 ** a query plan for each invocation and compute an estimated cost for that
2056 ** There are two hidden parameters that act as arguments to the table-valued
2057 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
2058 ** is available and bit 1 is set if "wholeline" is available.
2060 static int completionBestIndex(
2062 sqlite3_index_info *pIdxInfo
2064 int i; /* Loop over constraints */
2065 int idxNum = 0; /* The query plan bitmask */
2066 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
2067 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
2068 int nArg = 0; /* Number of arguments that completeFilter() expects */
2069 const struct sqlite3_index_constraint *pConstraint;
2071 (void)(tab); /* Unused parameter */
2072 pConstraint = pIdxInfo->aConstraint;
2073 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2074 if( pConstraint->usable==0 ) continue;
2075 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2076 switch( pConstraint->iColumn ){
2077 case COMPLETION_COLUMN_PREFIX:
2081 case COMPLETION_COLUMN_WHOLELINE:
2088 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
2089 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
2091 if( wholelineIdx>=0 ){
2092 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
2093 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
2095 pIdxInfo->idxNum = idxNum;
2096 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
2097 pIdxInfo->estimatedRows = 500 - 100*nArg;
2102 ** This following structure defines all the methods for the
2103 ** completion virtual table.
2105 static sqlite3_module completionModule = {
2108 completionConnect, /* xConnect */
2109 completionBestIndex, /* xBestIndex */
2110 completionDisconnect, /* xDisconnect */
2112 completionOpen, /* xOpen - open a cursor */
2113 completionClose, /* xClose - close a cursor */
2114 completionFilter, /* xFilter - configure scan constraints */
2115 completionNext, /* xNext - advance a cursor */
2116 completionEof, /* xEof - check for end of scan */
2117 completionColumn, /* xColumn - read data */
2118 completionRowid, /* xRowid - read data */
2124 0, /* xFindMethod */
2131 #endif /* SQLITE_OMIT_VIRTUALTABLE */
2133 int sqlite3CompletionVtabInit(sqlite3 *db){
2135 #ifndef SQLITE_OMIT_VIRTUALTABLE
2136 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
2142 __declspec(dllexport)
2144 int sqlite3_completion_init(
2147 const sqlite3_api_routines *pApi
2150 SQLITE_EXTENSION_INIT2(pApi);
2151 (void)(pzErrMsg); /* Unused parameter */
2152 #ifndef SQLITE_OMIT_VIRTUALTABLE
2153 rc = sqlite3CompletionVtabInit(db);
2158 /************************* End ../ext/misc/completion.c ********************/
2160 #if defined(SQLITE_ENABLE_SESSION)
2162 ** State information for a single open session
2164 typedef struct OpenSession OpenSession;
2165 struct OpenSession {
2166 char *zName; /* Symbolic name for this session */
2167 int nFilter; /* Number of xFilter rejection GLOB patterns */
2168 char **azFilter; /* Array of xFilter rejection GLOB patterns */
2169 sqlite3_session *p; /* The open session */
2174 ** Shell output mode information from before ".explain on",
2175 ** saved so that it can be restored by ".explain off"
2177 typedef struct SavedModeInfo SavedModeInfo;
2178 struct SavedModeInfo {
2179 int valid; /* Is there legit data in here? */
2180 int mode; /* Mode prior to ".explain on" */
2181 int showHeader; /* The ".header" setting prior to ".explain on" */
2182 int colWidth[100]; /* Column widths prior to ".explain on" */
2186 ** State information about the database connection is contained in an
2187 ** instance of the following structure.
2189 typedef struct ShellState ShellState;
2191 sqlite3 *db; /* The database */
2192 int autoExplain; /* Automatically turn on .explain mode */
2193 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
2194 int statsOn; /* True to display memory stats before each finalize */
2195 int scanstatsOn; /* True to display scan stats before each finalize */
2196 int outCount; /* Revert to stdout when reaching zero */
2197 int cnt; /* Number of records displayed so far */
2198 FILE *out; /* Write results here */
2199 FILE *traceOut; /* Output for sqlite3_trace() */
2200 int nErr; /* Number of errors seen */
2201 int mode; /* An output mode setting */
2202 int cMode; /* temporary output mode for the current query */
2203 int normalMode; /* Output mode before ".explain on" */
2204 int writableSchema; /* True if PRAGMA writable_schema=ON */
2205 int showHeader; /* True to show column names in List or Column mode */
2206 int nCheck; /* Number of ".check" commands run */
2207 unsigned shellFlgs; /* Various flags */
2208 char *zDestTable; /* Name of destination table when MODE_Insert */
2209 char zTestcase[30]; /* Name of current test case */
2210 char colSeparator[20]; /* Column separator character for several modes */
2211 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
2212 int colWidth[100]; /* Requested width of each column when in column mode*/
2213 int actualWidth[100]; /* Actual width of each column */
2214 char nullValue[20]; /* The text to print when a NULL comes back from
2216 char outfile[FILENAME_MAX]; /* Filename for *out */
2217 const char *zDbFilename; /* name of the database file */
2218 char *zFreeOnClose; /* Filename to free when closing */
2219 const char *zVfs; /* Name of VFS to use */
2220 sqlite3_stmt *pStmt; /* Current statement if any. */
2221 FILE *pLog; /* Write log output here */
2222 int *aiIndent; /* Array of indents used in MODE_Explain */
2223 int nIndent; /* Size of array aiIndent[] */
2224 int iIndent; /* Index of current op in aiIndent[] */
2225 #if defined(SQLITE_ENABLE_SESSION)
2226 int nSession; /* Number of active sessions */
2227 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
2232 ** These are the allowed shellFlgs values
2234 #define SHFLG_Scratch 0x00000001 /* The --scratch option is used */
2235 #define SHFLG_Pagecache 0x00000002 /* The --pagecache option is used */
2236 #define SHFLG_Lookaside 0x00000004 /* Lookaside memory is used */
2237 #define SHFLG_Backslash 0x00000008 /* The --backslash option is used */
2238 #define SHFLG_PreserveRowid 0x00000010 /* .dump preserves rowid values */
2239 #define SHFLG_Newlines 0x00000020 /* .dump --newline flag */
2240 #define SHFLG_CountChanges 0x00000040 /* .changes setting */
2241 #define SHFLG_Echo 0x00000080 /* .echo or --echo setting */
2244 ** Macros for testing and setting shellFlgs
2246 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
2247 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
2248 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
2251 ** These are the allowed modes.
2253 #define MODE_Line 0 /* One column per line. Blank line between records */
2254 #define MODE_Column 1 /* One record per line in neat columns */
2255 #define MODE_List 2 /* One record per line with a separator */
2256 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
2257 #define MODE_Html 4 /* Generate an XHTML table */
2258 #define MODE_Insert 5 /* Generate SQL "insert" statements */
2259 #define MODE_Quote 6 /* Quote values as for SQL */
2260 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
2261 #define MODE_Csv 8 /* Quote strings, numbers are plain */
2262 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
2263 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
2264 #define MODE_Pretty 11 /* Pretty-print schemas */
2266 static const char *modeDescr[] = {
2282 ** These are the column/row/line separators used by the various
2283 ** import/export modes.
2285 #define SEP_Column "|"
2286 #define SEP_Row "\n"
2287 #define SEP_Tab "\t"
2288 #define SEP_Space " "
2289 #define SEP_Comma ","
2290 #define SEP_CrLf "\r\n"
2291 #define SEP_Unit "\x1F"
2292 #define SEP_Record "\x1E"
2295 ** Number of elements in an array
2297 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
2300 ** A callback for the sqlite3_log() interface.
2302 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
2303 ShellState *p = (ShellState*)pArg;
2304 if( p->pLog==0 ) return;
2305 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
2310 ** Output the given string as a hex-encoded blob (eg. X'1234' )
2312 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
2314 char *zBlob = (char *)pBlob;
2315 raw_printf(out,"X'");
2316 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
2317 raw_printf(out,"'");
2321 ** Find a string that is not found anywhere in z[]. Return a pointer
2324 ** Try to use zA and zB first. If both of those are already found in z[]
2325 ** then make up some string and store it in the buffer zBuf.
2327 static const char *unused_string(
2328 const char *z, /* Result must not appear anywhere in z */
2329 const char *zA, const char *zB, /* Try these first */
2330 char *zBuf /* Space to store a generated string */
2333 if( strstr(z, zA)==0 ) return zA;
2334 if( strstr(z, zB)==0 ) return zB;
2336 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
2337 }while( strstr(z,zBuf)!=0 );
2342 ** Output the given string as a quoted string using SQL quoting conventions.
2344 ** See also: output_quoted_escaped_string()
2346 static void output_quoted_string(FILE *out, const char *z){
2349 setBinaryMode(out, 1);
2350 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
2352 utf8_printf(out,"'%s'",z);
2354 raw_printf(out, "'");
2356 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
2359 utf8_printf(out, "%.*s", i, z);
2363 raw_printf(out, "'");
2371 raw_printf(out, "'");
2373 setTextMode(out, 1);
2377 ** Output the given string as a quoted string using SQL quoting conventions.
2378 ** Additionallly , escape the "\n" and "\r" characters so that they do not
2379 ** get corrupted by end-of-line translation facilities in some operating
2382 ** This is like output_quoted_string() but with the addition of the \r\n
2383 ** escape mechanism.
2385 static void output_quoted_escaped_string(FILE *out, const char *z){
2388 setBinaryMode(out, 1);
2389 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
2391 utf8_printf(out,"'%s'",z);
2393 const char *zNL = 0;
2394 const char *zCR = 0;
2397 char zBuf1[20], zBuf2[20];
2398 for(i=0; z[i]; i++){
2399 if( z[i]=='\n' ) nNL++;
2400 if( z[i]=='\r' ) nCR++;
2403 raw_printf(out, "replace(");
2404 zNL = unused_string(z, "\\n", "\\012", zBuf1);
2407 raw_printf(out, "replace(");
2408 zCR = unused_string(z, "\\r", "\\015", zBuf2);
2410 raw_printf(out, "'");
2412 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
2415 utf8_printf(out, "%.*s", i, z);
2419 raw_printf(out, "'");
2427 raw_printf(out, "%s", zNL);
2430 raw_printf(out, "%s", zCR);
2432 raw_printf(out, "'");
2434 raw_printf(out, ",'%s',char(13))", zCR);
2437 raw_printf(out, ",'%s',char(10))", zNL);
2440 setTextMode(out, 1);
2444 ** Output the given string as a quoted according to C or TCL quoting rules.
2446 static void output_c_string(FILE *out, const char *z){
2449 while( (c = *(z++))!=0 ){
2456 }else if( c=='\t' ){
2459 }else if( c=='\n' ){
2462 }else if( c=='\r' ){
2465 }else if( !isprint(c&0xff) ){
2466 raw_printf(out, "\\%03o", c&0xff);
2475 ** Output the given string with characters that are special to
2478 static void output_html_string(FILE *out, const char *z){
2490 utf8_printf(out,"%.*s",i,z);
2493 raw_printf(out,"<");
2494 }else if( z[i]=='&' ){
2495 raw_printf(out,"&");
2496 }else if( z[i]=='>' ){
2497 raw_printf(out,">");
2498 }else if( z[i]=='\"' ){
2499 raw_printf(out,""");
2500 }else if( z[i]=='\'' ){
2501 raw_printf(out,"'");
2510 ** If a field contains any character identified by a 1 in the following
2511 ** array, then the string must be quoted for CSV.
2513 static const char needCsvQuote[] = {
2514 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2515 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2516 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
2517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2518 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2519 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2520 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2521 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2526 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2527 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2528 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2529 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2533 ** Output a single term of CSV. Actually, p->colSeparator is used for
2534 ** the separator, which may or may not be a comma. p->nullValue is
2535 ** the null value. Strings are quoted if necessary. The separator
2536 ** is only issued if bSep is true.
2538 static void output_csv(ShellState *p, const char *z, int bSep){
2541 utf8_printf(out,"%s",p->nullValue);
2544 int nSep = strlen30(p->colSeparator);
2545 for(i=0; z[i]; i++){
2546 if( needCsvQuote[((unsigned char*)z)[i]]
2547 || (z[i]==p->colSeparator[0] &&
2548 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
2555 for(i=0; z[i]; i++){
2556 if( z[i]=='"' ) putc('"', out);
2561 utf8_printf(out, "%s", z);
2565 utf8_printf(p->out, "%s", p->colSeparator);
2571 ** This routine runs when the user presses Ctrl-C
2573 static void interrupt_handler(int NotUsed){
2574 UNUSED_PARAMETER(NotUsed);
2576 if( seenInterrupt>2 ) exit(1);
2577 if( globalDb ) sqlite3_interrupt(globalDb);
2581 #ifndef SQLITE_OMIT_AUTHORIZATION
2583 ** When the ".auth ON" is set, the following authorizer callback is
2584 ** invoked. It always returns SQLITE_OK.
2586 static int shellAuth(
2594 ShellState *p = (ShellState*)pClientData;
2595 static const char *azAction[] = { 0,
2596 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2597 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2598 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2599 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2600 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2601 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2602 "PRAGMA", "READ", "SELECT",
2603 "TRANSACTION", "UPDATE", "ATTACH",
2604 "DETACH", "ALTER_TABLE", "REINDEX",
2605 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2606 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2614 utf8_printf(p->out, "authorizer: %s", azAction[op]);
2616 raw_printf(p->out, " ");
2618 output_c_string(p->out, az[i]);
2620 raw_printf(p->out, "NULL");
2623 raw_printf(p->out, "\n");
2629 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2631 ** This routine converts some CREATE TABLE statements for shadow tables
2632 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2634 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
2635 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
2636 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
2638 utf8_printf(out, "%s%s", z, zTail);
2641 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
2644 printSchemaLine(out, z, zTail);
2649 ** This is the callback routine that the shell
2650 ** invokes for each row of a query result.
2652 static int shell_callback(
2654 int nArg, /* Number of result columns */
2655 char **azArg, /* Text of each result column */
2656 char **azCol, /* Column names */
2657 int *aiType /* Column types */
2660 ShellState *p = (ShellState*)pArg;
2665 if( azArg==0 ) break;
2666 for(i=0; i<nArg; i++){
2667 int len = strlen30(azCol[i] ? azCol[i] : "");
2668 if( len>w ) w = len;
2670 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2671 for(i=0; i<nArg; i++){
2672 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2673 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2679 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
2680 const int *colWidth;
2683 if( p->cMode==MODE_Column ){
2684 colWidth = p->colWidth;
2685 showHdr = p->showHeader;
2686 rowSep = p->rowSeparator;
2688 colWidth = aExplainWidths;
2693 for(i=0; i<nArg; i++){
2695 if( i<ArraySize(p->colWidth) ){
2701 w = strlenChar(azCol[i] ? azCol[i] : "");
2703 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
2706 if( i<ArraySize(p->actualWidth) ){
2707 p->actualWidth[i] = w;
2710 utf8_width_print(p->out, w, azCol[i]);
2711 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
2715 for(i=0; i<nArg; i++){
2717 if( i<ArraySize(p->actualWidth) ){
2718 w = p->actualWidth[i];
2723 utf8_printf(p->out,"%-*.*s%s",w,w,
2724 "----------------------------------------------------------"
2725 "----------------------------------------------------------",
2726 i==nArg-1 ? rowSep : " ");
2730 if( azArg==0 ) break;
2731 for(i=0; i<nArg; i++){
2733 if( i<ArraySize(p->actualWidth) ){
2734 w = p->actualWidth[i];
2738 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
2739 w = strlenChar(azArg[i]);
2741 if( i==1 && p->aiIndent && p->pStmt ){
2742 if( p->iIndent<p->nIndent ){
2743 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2747 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2748 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
2752 case MODE_Semi: { /* .schema and .fullschema output */
2753 printSchemaLine(p->out, azArg[0], ";\n");
2756 case MODE_Pretty: { /* .schema and .fullschema with --indent */
2764 if( azArg[0]==0 ) break;
2765 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2766 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2768 utf8_printf(p->out, "%s;\n", azArg[0]);
2771 z = sqlite3_mprintf("%s", azArg[0]);
2773 for(i=0; IsSpace(z[i]); i++){}
2774 for(; (c = z[i])!=0; i++){
2776 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2777 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2782 while( j>0 && IsSpace(z[j-1]) ){ j--; }
2784 if( strlen30(z)>=79 ){
2785 for(i=j=0; (c = z[i])!=0; i++){
2788 }else if( c=='"' || c=='\'' || c=='`' ){
2796 if( nLine>0 && nParen==0 && j>0 ){
2797 printSchemaLineN(p->out, z, j, "\n");
2802 if( nParen==1 && (c=='(' || c==',' || c=='\n') ){
2804 printSchemaLineN(p->out, z, j, "\n ");
2807 while( IsSpace(z[i+1]) ){ i++; }
2812 printSchemaLine(p->out, z, ";\n");
2817 if( p->cnt++==0 && p->showHeader ){
2818 for(i=0; i<nArg; i++){
2819 utf8_printf(p->out,"%s%s",azCol[i],
2820 i==nArg-1 ? p->rowSeparator : p->colSeparator);
2823 if( azArg==0 ) break;
2824 for(i=0; i<nArg; i++){
2826 if( z==0 ) z = p->nullValue;
2827 utf8_printf(p->out, "%s", z);
2829 utf8_printf(p->out, "%s", p->colSeparator);
2831 utf8_printf(p->out, "%s", p->rowSeparator);
2837 if( p->cnt++==0 && p->showHeader ){
2838 raw_printf(p->out,"<TR>");
2839 for(i=0; i<nArg; i++){
2840 raw_printf(p->out,"<TH>");
2841 output_html_string(p->out, azCol[i]);
2842 raw_printf(p->out,"</TH>\n");
2844 raw_printf(p->out,"</TR>\n");
2846 if( azArg==0 ) break;
2847 raw_printf(p->out,"<TR>");
2848 for(i=0; i<nArg; i++){
2849 raw_printf(p->out,"<TD>");
2850 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2851 raw_printf(p->out,"</TD>\n");
2853 raw_printf(p->out,"</TR>\n");
2857 if( p->cnt++==0 && p->showHeader ){
2858 for(i=0; i<nArg; i++){
2859 output_c_string(p->out,azCol[i] ? azCol[i] : "");
2860 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2862 utf8_printf(p->out, "%s", p->rowSeparator);
2864 if( azArg==0 ) break;
2865 for(i=0; i<nArg; i++){
2866 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2867 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2869 utf8_printf(p->out, "%s", p->rowSeparator);
2873 setBinaryMode(p->out, 1);
2874 if( p->cnt++==0 && p->showHeader ){
2875 for(i=0; i<nArg; i++){
2876 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2878 utf8_printf(p->out, "%s", p->rowSeparator);
2881 for(i=0; i<nArg; i++){
2882 output_csv(p, azArg[i], i<nArg-1);
2884 utf8_printf(p->out, "%s", p->rowSeparator);
2886 setTextMode(p->out, 1);
2890 if( azArg==0 ) break;
2891 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2892 if( p->showHeader ){
2893 raw_printf(p->out,"(");
2894 for(i=0; i<nArg; i++){
2895 if( i>0 ) raw_printf(p->out, ",");
2896 if( quoteChar(azCol[i]) ){
2897 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2898 utf8_printf(p->out, "%s", z);
2901 raw_printf(p->out, "%s", azCol[i]);
2904 raw_printf(p->out,")");
2907 for(i=0; i<nArg; i++){
2908 raw_printf(p->out, i>0 ? "," : " VALUES(");
2909 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2910 utf8_printf(p->out,"NULL");
2911 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2912 if( ShellHasFlag(p, SHFLG_Newlines) ){
2913 output_quoted_string(p->out, azArg[i]);
2915 output_quoted_escaped_string(p->out, azArg[i]);
2917 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2918 utf8_printf(p->out,"%s", azArg[i]);
2919 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2921 double r = sqlite3_column_double(p->pStmt, i);
2922 sqlite3_snprintf(50,z,"%!.20g", r);
2923 raw_printf(p->out, "%s", z);
2924 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2925 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2926 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2927 output_hex_blob(p->out, pBlob, nBlob);
2928 }else if( isNumber(azArg[i], 0) ){
2929 utf8_printf(p->out,"%s", azArg[i]);
2930 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2931 output_quoted_string(p->out, azArg[i]);
2933 output_quoted_escaped_string(p->out, azArg[i]);
2936 raw_printf(p->out,");\n");
2940 if( azArg==0 ) break;
2941 if( p->cnt==0 && p->showHeader ){
2942 for(i=0; i<nArg; i++){
2943 if( i>0 ) raw_printf(p->out, ",");
2944 output_quoted_string(p->out, azCol[i]);
2946 raw_printf(p->out,"\n");
2949 for(i=0; i<nArg; i++){
2950 if( i>0 ) raw_printf(p->out, ",");
2951 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2952 utf8_printf(p->out,"NULL");
2953 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2954 output_quoted_string(p->out, azArg[i]);
2955 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2956 utf8_printf(p->out,"%s", azArg[i]);
2957 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2959 double r = sqlite3_column_double(p->pStmt, i);
2960 sqlite3_snprintf(50,z,"%!.20g", r);
2961 raw_printf(p->out, "%s", z);
2962 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2963 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2964 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2965 output_hex_blob(p->out, pBlob, nBlob);
2966 }else if( isNumber(azArg[i], 0) ){
2967 utf8_printf(p->out,"%s", azArg[i]);
2969 output_quoted_string(p->out, azArg[i]);
2972 raw_printf(p->out,"\n");
2976 if( p->cnt++==0 && p->showHeader ){
2977 for(i=0; i<nArg; i++){
2978 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2979 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2981 utf8_printf(p->out, "%s", p->rowSeparator);
2983 if( azArg==0 ) break;
2984 for(i=0; i<nArg; i++){
2985 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2986 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2988 utf8_printf(p->out, "%s", p->rowSeparator);
2996 ** This is the callback routine that the SQLite library
2997 ** invokes for each row of a query result.
2999 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
3000 /* since we don't have type info, call the shell_callback with a NULL value */
3001 return shell_callback(pArg, nArg, azArg, azCol, NULL);
3005 ** This is the callback routine from sqlite3_exec() that appends all
3006 ** output onto the end of a ShellText object.
3008 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
3009 ShellText *p = (ShellText*)pArg;
3011 UNUSED_PARAMETER(az);
3012 if( p->n ) appendText(p, "|", 0);
3013 for(i=0; i<nArg; i++){
3014 if( i ) appendText(p, ",", 0);
3015 if( azArg[i] ) appendText(p, azArg[i], 0);
3021 ** Generate an appropriate SELFTEST table in the main database.
3023 static void createSelftestTable(ShellState *p){
3026 "SAVEPOINT selftest_init;\n"
3027 "CREATE TABLE IF NOT EXISTS selftest(\n"
3028 " tno INTEGER PRIMARY KEY,\n" /* Test number */
3029 " op TEXT,\n" /* Operator: memo run */
3030 " cmd TEXT,\n" /* Command text */
3031 " ans TEXT\n" /* Desired answer */
3033 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
3034 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
3035 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
3036 " 'memo','Tests generated by --init');\n"
3037 "INSERT INTO [_shell$self]\n"
3039 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
3040 "FROM sqlite_master ORDER BY 2'',224))',\n"
3041 " hex(sha3_query('SELECT type,name,tbl_name,sql "
3042 "FROM sqlite_master ORDER BY 2',224));\n"
3043 "INSERT INTO [_shell$self]\n"
3045 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
3046 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
3047 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
3049 " SELECT name FROM sqlite_master\n"
3050 " WHERE type='table'\n"
3051 " AND name<>'selftest'\n"
3052 " AND coalesce(rootpage,0)>0\n"
3055 "INSERT INTO [_shell$self]\n"
3056 " VALUES('run','PRAGMA integrity_check','ok');\n"
3057 "INSERT INTO selftest(tno,op,cmd,ans)"
3058 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
3059 "DROP TABLE [_shell$self];"
3062 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
3063 sqlite3_free(zErrMsg);
3065 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
3070 ** Set the destination table field of the ShellState structure to
3071 ** the name of the table given. Escape any quote characters in the
3074 static void set_table_name(ShellState *p, const char *zName){
3079 if( p->zDestTable ){
3080 free(p->zDestTable);
3083 if( zName==0 ) return;
3084 cQuote = quoteChar(zName);
3085 n = strlen30(zName);
3086 if( cQuote ) n += n+2;
3087 z = p->zDestTable = malloc( n+1 );
3089 raw_printf(stderr,"Error: out of memory\n");
3093 if( cQuote ) z[n++] = cQuote;
3094 for(i=0; zName[i]; i++){
3096 if( zName[i]==cQuote ) z[n++] = cQuote;
3098 if( cQuote ) z[n++] = cQuote;
3104 ** Execute a query statement that will generate SQL output. Print
3105 ** the result columns, comma-separated, on a line and then add a
3106 ** semicolon terminator to the end of that line.
3108 ** If the number of columns is 1 and that column contains text "--"
3109 ** then write the semicolon on a separate line. That way, if a
3110 ** "--" comment occurs at the end of the statement, the comment
3111 ** won't consume the semicolon terminator.
3113 static int run_table_dump_query(
3114 ShellState *p, /* Query context */
3115 const char *zSelect, /* SELECT statement to extract content */
3116 const char *zFirstRow /* Print before first row, if not NULL */
3118 sqlite3_stmt *pSelect;
3123 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
3124 if( rc!=SQLITE_OK || !pSelect ){
3125 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
3126 sqlite3_errmsg(p->db));
3127 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3130 rc = sqlite3_step(pSelect);
3131 nResult = sqlite3_column_count(pSelect);
3132 while( rc==SQLITE_ROW ){
3134 utf8_printf(p->out, "%s", zFirstRow);
3137 z = (const char*)sqlite3_column_text(pSelect, 0);
3138 utf8_printf(p->out, "%s", z);
3139 for(i=1; i<nResult; i++){
3140 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
3143 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
3145 raw_printf(p->out, "\n;\n");
3147 raw_printf(p->out, ";\n");
3149 rc = sqlite3_step(pSelect);
3151 rc = sqlite3_finalize(pSelect);
3152 if( rc!=SQLITE_OK ){
3153 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
3154 sqlite3_errmsg(p->db));
3155 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3161 ** Allocate space and save off current error string.
3163 static char *save_err_msg(
3164 sqlite3 *db /* Database to query */
3166 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
3167 char *zErrMsg = sqlite3_malloc64(nErrMsg);
3169 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
3176 ** Attempt to display I/O stats on Linux using /proc/PID/io
3178 static void displayLinuxIoStats(FILE *out){
3181 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
3182 in = fopen(z, "rb");
3184 while( fgets(z, sizeof(z), in)!=0 ){
3185 static const struct {
3186 const char *zPattern;
3189 { "rchar: ", "Bytes received by read():" },
3190 { "wchar: ", "Bytes sent to write():" },
3191 { "syscr: ", "Read() system calls:" },
3192 { "syscw: ", "Write() system calls:" },
3193 { "read_bytes: ", "Bytes read from storage:" },
3194 { "write_bytes: ", "Bytes written to storage:" },
3195 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3198 for(i=0; i<ArraySize(aTrans); i++){
3199 int n = (int)strlen(aTrans[i].zPattern);
3200 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
3201 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
3211 ** Display a single line of status using 64-bit values.
3213 static void displayStatLine(
3214 ShellState *p, /* The shell context */
3215 char *zLabel, /* Label for this one line */
3216 char *zFormat, /* Format for the result */
3217 int iStatusCtrl, /* Which status to display */
3218 int bReset /* True to reset the stats */
3220 sqlite3_int64 iCur = -1;
3221 sqlite3_int64 iHiwtr = -1;
3224 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
3225 for(i=0, nPercent=0; zFormat[i]; i++){
3226 if( zFormat[i]=='%' ) nPercent++;
3229 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
3231 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
3233 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
3237 ** Display memory stats.
3239 static int display_stats(
3240 sqlite3 *db, /* Database to query */
3241 ShellState *pArg, /* Pointer to ShellState */
3242 int bReset /* True to reset the stats */
3247 if( pArg && pArg->out ){
3248 displayStatLine(pArg, "Memory Used:",
3249 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
3250 displayStatLine(pArg, "Number of Outstanding Allocations:",
3251 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
3252 if( pArg->shellFlgs & SHFLG_Pagecache ){
3253 displayStatLine(pArg, "Number of Pcache Pages Used:",
3254 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
3256 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
3257 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
3258 if( pArg->shellFlgs & SHFLG_Scratch ){
3259 displayStatLine(pArg, "Number of Scratch Allocations Used:",
3260 "%lld (max %lld)", SQLITE_STATUS_SCRATCH_USED, bReset);
3262 displayStatLine(pArg, "Number of Scratch Overflow Bytes:",
3263 "%lld (max %lld) bytes", SQLITE_STATUS_SCRATCH_OVERFLOW, bReset);
3264 displayStatLine(pArg, "Largest Allocation:",
3265 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
3266 displayStatLine(pArg, "Largest Pcache Allocation:",
3267 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
3268 displayStatLine(pArg, "Largest Scratch Allocation:",
3269 "%lld bytes", SQLITE_STATUS_SCRATCH_SIZE, bReset);
3270 #ifdef YYTRACKMAXSTACKDEPTH
3271 displayStatLine(pArg, "Deepest Parser Stack:",
3272 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
3276 if( pArg && pArg->out && db ){
3277 if( pArg->shellFlgs & SHFLG_Lookaside ){
3279 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
3280 &iCur, &iHiwtr, bReset);
3281 raw_printf(pArg->out,
3282 "Lookaside Slots Used: %d (max %d)\n",
3284 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
3285 &iCur, &iHiwtr, bReset);
3286 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
3288 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
3289 &iCur, &iHiwtr, bReset);
3290 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
3292 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
3293 &iCur, &iHiwtr, bReset);
3294 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
3298 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
3299 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
3302 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
3303 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
3305 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
3306 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
3308 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
3309 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
3311 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
3312 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
3315 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
3316 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
3320 if( pArg && pArg->out && db && pArg->pStmt ){
3321 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
3323 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
3324 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
3325 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
3326 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
3327 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
3328 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
3329 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
3333 displayLinuxIoStats(pArg->out);
3336 /* Do not remove this machine readable comment: extra-stats-output-here */
3342 ** Display scan stats.
3344 static void display_scanstats(
3345 sqlite3 *db, /* Database to query */
3346 ShellState *pArg /* Pointer to ShellState */
3348 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3349 UNUSED_PARAMETER(db);
3350 UNUSED_PARAMETER(pArg);
3353 raw_printf(pArg->out, "-------- scanstats --------\n");
3355 for(k=0; k<=mx; k++){
3356 double rEstLoop = 1.0;
3358 sqlite3_stmt *p = pArg->pStmt;
3359 sqlite3_int64 nLoop, nVisit;
3362 const char *zExplain;
3363 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
3366 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
3367 if( iSid>mx ) mx = iSid;
3368 if( iSid!=k ) continue;
3370 rEstLoop = (double)nLoop;
3371 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
3374 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
3375 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
3376 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
3377 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
3379 raw_printf(pArg->out,
3380 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
3381 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
3385 raw_printf(pArg->out, "---------------------------\n");
3390 ** Parameter azArray points to a zero-terminated array of strings. zStr
3391 ** points to a single nul-terminated string. Return non-zero if zStr
3392 ** is equal, according to strcmp(), to any of the strings in the array.
3393 ** Otherwise, return zero.
3395 static int str_in_array(const char *zStr, const char **azArray){
3397 for(i=0; azArray[i]; i++){
3398 if( 0==strcmp(zStr, azArray[i]) ) return 1;
3404 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3405 ** and populate the ShellState.aiIndent[] array with the number of
3406 ** spaces each opcode should be indented before it is output.
3408 ** The indenting rules are:
3410 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3411 ** all opcodes that occur between the p2 jump destination and the opcode
3412 ** itself by 2 spaces.
3414 ** * For each "Goto", if the jump destination is earlier in the program
3415 ** and ends on one of:
3416 ** Yield SeekGt SeekLt RowSetRead Rewind
3417 ** or if the P1 parameter is one instead of zero,
3418 ** then indent all opcodes between the earlier instruction
3419 ** and "Goto" by 2 spaces.
3421 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3422 const char *zSql; /* The text of the SQL statement */
3423 const char *z; /* Used to check if this is an EXPLAIN */
3424 int *abYield = 0; /* True if op is an OP_Yield */
3425 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
3426 int iOp; /* Index of operation in p->aiIndent[] */
3428 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3429 "NextIfOpen", "PrevIfOpen", 0 };
3430 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3432 const char *azGoto[] = { "Goto", 0 };
3434 /* Try to figure out if this is really an EXPLAIN statement. If this
3435 ** cannot be verified, return early. */
3436 if( sqlite3_column_count(pSql)!=8 ){
3440 zSql = sqlite3_sql(pSql);
3441 if( zSql==0 ) return;
3442 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3443 if( sqlite3_strnicmp(z, "explain", 7) ){
3448 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3450 int iAddr = sqlite3_column_int(pSql, 0);
3451 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3453 /* Set p2 to the P2 field of the current opcode. Then, assuming that
3454 ** p2 is an instruction address, set variable p2op to the index of that
3455 ** instruction in the aiIndent[] array. p2 and p2op may be different if
3456 ** the current instruction is part of a sub-program generated by an
3457 ** SQL trigger or foreign key. */
3458 int p2 = sqlite3_column_int(pSql, 3);
3459 int p2op = (p2 + (iOp-iAddr));
3461 /* Grow the p->aiIndent array as required */
3464 /* Do further verfication that this is explain output. Abort if
3466 static const char *explainCols[] = {
3467 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3469 for(jj=0; jj<ArraySize(explainCols); jj++){
3470 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3472 sqlite3_reset(pSql);
3478 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3479 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3481 abYield[iOp] = str_in_array(zOp, azYield);
3482 p->aiIndent[iOp] = 0;
3485 if( str_in_array(zOp, azNext) ){
3486 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3488 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3489 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3491 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3496 sqlite3_free(abYield);
3497 sqlite3_reset(pSql);
3501 ** Free the array allocated by explain_data_prepare().
3503 static void explain_data_delete(ShellState *p){
3504 sqlite3_free(p->aiIndent);
3511 ** Disable and restore .wheretrace and .selecttrace settings.
3513 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
3514 extern int sqlite3SelectTrace;
3515 static int savedSelectTrace;
3517 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
3518 extern int sqlite3WhereTrace;
3519 static int savedWhereTrace;
3521 static void disable_debug_trace_modes(void){
3522 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
3523 savedSelectTrace = sqlite3SelectTrace;
3524 sqlite3SelectTrace = 0;
3526 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
3527 savedWhereTrace = sqlite3WhereTrace;
3528 sqlite3WhereTrace = 0;
3531 static void restore_debug_trace_modes(void){
3532 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
3533 sqlite3SelectTrace = savedSelectTrace;
3535 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
3536 sqlite3WhereTrace = savedWhereTrace;
3541 ** Run a prepared statement
3543 static void exec_prepared_stmt(
3544 ShellState *pArg, /* Pointer to ShellState */
3545 sqlite3_stmt *pStmt, /* Statment to run */
3546 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
3550 /* perform the first step. this will tell us if we
3551 ** have a result set or not and how wide it is.
3553 rc = sqlite3_step(pStmt);
3554 /* if we have a result set... */
3555 if( SQLITE_ROW == rc ){
3556 /* if we have a callback... */
3558 /* allocate space for col name ptr, value ptr, and type */
3559 int nCol = sqlite3_column_count(pStmt);
3560 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
3564 char **azCols = (char **)pData; /* Names of result columns */
3565 char **azVals = &azCols[nCol]; /* Results */
3566 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
3568 assert(sizeof(int) <= sizeof(char *));
3569 /* save off ptrs to column names */
3570 for(i=0; i<nCol; i++){
3571 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
3574 /* extract the data and data types */
3575 for(i=0; i<nCol; i++){
3576 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
3577 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
3580 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
3582 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
3584 break; /* from for */
3588 /* if data and types extracted successfully... */
3589 if( SQLITE_ROW == rc ){
3590 /* call the supplied callback with the result row data */
3591 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
3594 rc = sqlite3_step(pStmt);
3597 } while( SQLITE_ROW == rc );
3598 sqlite3_free(pData);
3602 rc = sqlite3_step(pStmt);
3603 } while( rc == SQLITE_ROW );
3609 ** Execute a statement or set of statements. Print
3610 ** any result rows/columns depending on the current mode
3611 ** set via the supplied callback.
3613 ** This is very similar to SQLite's built-in sqlite3_exec()
3614 ** function except it takes a slightly different callback
3615 ** and callback data argument.
3617 static int shell_exec(
3618 sqlite3 *db, /* An open database */
3619 const char *zSql, /* SQL to be evaluated */
3620 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
3621 /* (not the same as sqlite3_exec) */
3622 ShellState *pArg, /* Pointer to ShellState */
3623 char **pzErrMsg /* Error msg written here */
3625 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
3626 int rc = SQLITE_OK; /* Return Code */
3628 const char *zLeftover; /* Tail of unprocessed SQL */
3634 while( zSql[0] && (SQLITE_OK == rc) ){
3635 static const char *zStmtSql;
3636 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3637 if( SQLITE_OK != rc ){
3639 *pzErrMsg = save_err_msg(db);
3643 /* this happens for a comment or white-space */
3645 while( IsSpace(zSql[0]) ) zSql++;
3648 zStmtSql = sqlite3_sql(pStmt);
3649 if( zStmtSql==0 ) zStmtSql = "";
3650 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3652 /* save off the prepared statment handle and reset row count */
3654 pArg->pStmt = pStmt;
3658 /* echo the sql statement if echo on */
3659 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3660 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3663 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
3664 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
3665 sqlite3_stmt *pExplain;
3667 disable_debug_trace_modes();
3668 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3669 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3670 if( rc==SQLITE_OK ){
3671 while( sqlite3_step(pExplain)==SQLITE_ROW ){
3672 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
3673 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
3674 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
3675 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
3678 sqlite3_finalize(pExplain);
3680 if( pArg->autoEQP>=2 ){
3681 /* Also do an EXPLAIN for ".eqp full" mode */
3682 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3683 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3684 if( rc==SQLITE_OK ){
3685 pArg->cMode = MODE_Explain;
3686 explain_data_prepare(pArg, pExplain);
3687 exec_prepared_stmt(pArg, pExplain, xCallback);
3688 explain_data_delete(pArg);
3690 sqlite3_finalize(pExplain);
3693 restore_debug_trace_modes();
3697 pArg->cMode = pArg->mode;
3698 if( pArg->autoExplain
3699 && sqlite3_column_count(pStmt)==8
3700 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
3702 pArg->cMode = MODE_Explain;
3705 /* If the shell is currently in ".explain" mode, gather the extra
3706 ** data required to add indents to the output.*/
3707 if( pArg->cMode==MODE_Explain ){
3708 explain_data_prepare(pArg, pStmt);
3712 exec_prepared_stmt(pArg, pStmt, xCallback);
3713 explain_data_delete(pArg);
3715 /* print usage stats if stats on */
3716 if( pArg && pArg->statsOn ){
3717 display_stats(db, pArg, 0);
3720 /* print loop-counters if required */
3721 if( pArg && pArg->scanstatsOn ){
3722 display_scanstats(db, pArg);
3725 /* Finalize the statement just executed. If this fails, save a
3726 ** copy of the error message. Otherwise, set zSql to point to the
3727 ** next statement to execute. */
3728 rc2 = sqlite3_finalize(pStmt);
3729 if( rc!=SQLITE_NOMEM ) rc = rc2;
3730 if( rc==SQLITE_OK ){
3732 while( IsSpace(zSql[0]) ) zSql++;
3733 }else if( pzErrMsg ){
3734 *pzErrMsg = save_err_msg(db);
3737 /* clear saved stmt handle */
3748 ** Release memory previously allocated by tableColumnList().
3750 static void freeColumnList(char **azCol){
3752 for(i=1; azCol[i]; i++){
3753 sqlite3_free(azCol[i]);
3755 /* azCol[0] is a static string */
3756 sqlite3_free(azCol);
3760 ** Return a list of pointers to strings which are the names of all
3761 ** columns in table zTab. The memory to hold the names is dynamically
3762 ** allocated and must be released by the caller using a subsequent call
3763 ** to freeColumnList().
3765 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
3766 ** value that needs to be preserved, then azCol[0] is filled in with the
3767 ** name of the rowid column.
3769 ** The first regular column in the table is azCol[1]. The list is terminated
3770 ** by an entry with azCol[i]==0.
3772 static char **tableColumnList(ShellState *p, const char *zTab){
3774 sqlite3_stmt *pStmt;
3778 int nPK = 0; /* Number of PRIMARY KEY columns seen */
3779 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
3780 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3783 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3784 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3787 while( sqlite3_step(pStmt)==SQLITE_ROW ){
3788 if( nCol>=nAlloc-2 ){
3789 nAlloc = nAlloc*2 + nCol + 10;
3790 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
3792 raw_printf(stderr, "Error: out of memory\n");
3796 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3797 if( sqlite3_column_int(pStmt, 5) ){
3800 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3809 sqlite3_finalize(pStmt);
3813 /* The decision of whether or not a rowid really needs to be preserved
3814 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
3815 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
3816 ** rowids on tables where the rowid is inaccessible because there are other
3817 ** columns in the table named "rowid", "_rowid_", and "oid".
3819 if( preserveRowid && isIPK ){
3820 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3821 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
3822 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3823 ** ROWID aliases. To distinguish these cases, check to see if
3824 ** there is a "pk" entry in "PRAGMA index_list". There will be
3825 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3827 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3828 " WHERE origin='pk'", zTab);
3829 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3832 freeColumnList(azCol);
3835 rc = sqlite3_step(pStmt);
3836 sqlite3_finalize(pStmt);
3837 preserveRowid = rc==SQLITE_ROW;
3839 if( preserveRowid ){
3840 /* Only preserve the rowid if we can find a name to use for the
3842 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3845 for(i=1; i<=nCol; i++){
3846 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3849 /* At this point, we know that azRowid[j] is not the name of any
3850 ** ordinary column in the table. Verify that azRowid[j] is a valid
3851 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
3852 ** tables will fail this last check */
3853 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3854 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3863 ** Toggle the reverse_unordered_selects setting.
3865 static void toggleSelectOrder(sqlite3 *db){
3866 sqlite3_stmt *pStmt = 0;
3869 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3870 if( sqlite3_step(pStmt)==SQLITE_ROW ){
3871 iSetting = sqlite3_column_int(pStmt, 0);
3873 sqlite3_finalize(pStmt);
3874 sqlite3_snprintf(sizeof(zStmt), zStmt,
3875 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3876 sqlite3_exec(db, zStmt, 0, 0, 0);
3880 ** This is a different callback routine used for dumping the database.
3881 ** Each row received by this callback consists of a table name,
3882 ** the table type ("index" or "table") and SQL to create the table.
3883 ** This routine should print text sufficient to recreate the table.
3885 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3890 ShellState *p = (ShellState *)pArg;
3892 UNUSED_PARAMETER(azNotUsed);
3893 if( nArg!=3 ) return 1;
3898 if( strcmp(zTable, "sqlite_sequence")==0 ){
3899 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3900 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3901 raw_printf(p->out, "ANALYZE sqlite_master;\n");
3902 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3904 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3906 if( !p->writableSchema ){
3907 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3908 p->writableSchema = 1;
3910 zIns = sqlite3_mprintf(
3911 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3912 "VALUES('table','%q','%q',0,'%q');",
3913 zTable, zTable, zSql);
3914 utf8_printf(p->out, "%s\n", zIns);
3918 printSchemaLine(p->out, zSql, ";\n");
3921 if( strcmp(zType, "table")==0 ){
3926 char *savedDestTable;
3929 azCol = tableColumnList(p, zTable);
3935 /* Always quote the table name, even if it appears to be pure ascii,
3936 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
3938 appendText(&sTable, zTable, quoteChar(zTable));
3939 /* If preserving the rowid, add a column list after the table name.
3940 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3941 ** instead of the usual "INSERT INTO tab VALUES(...)".
3944 appendText(&sTable, "(", 0);
3945 appendText(&sTable, azCol[0], 0);
3946 for(i=1; azCol[i]; i++){
3947 appendText(&sTable, ",", 0);
3948 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3950 appendText(&sTable, ")", 0);
3953 /* Build an appropriate SELECT statement */
3955 appendText(&sSelect, "SELECT ", 0);
3957 appendText(&sSelect, azCol[0], 0);
3958 appendText(&sSelect, ",", 0);
3960 for(i=1; azCol[i]; i++){
3961 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3963 appendText(&sSelect, ",", 0);
3966 freeColumnList(azCol);
3967 appendText(&sSelect, " FROM ", 0);
3968 appendText(&sSelect, zTable, quoteChar(zTable));
3970 savedDestTable = p->zDestTable;
3971 savedMode = p->mode;
3972 p->zDestTable = sTable.z;
3973 p->mode = p->cMode = MODE_Insert;
3974 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3975 if( (rc&0xff)==SQLITE_CORRUPT ){
3976 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3977 toggleSelectOrder(p->db);
3978 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
3979 toggleSelectOrder(p->db);
3981 p->zDestTable = savedDestTable;
3982 p->mode = savedMode;
3991 ** Run zQuery. Use dump_callback() as the callback routine so that
3992 ** the contents of the query are output as SQL statements.
3994 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
3995 ** "ORDER BY rowid DESC" to the end.
3997 static int run_schema_dump_query(
4003 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4004 if( rc==SQLITE_CORRUPT ){
4006 int len = strlen30(zQuery);
4007 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4009 utf8_printf(p->out, "/****** %s ******/\n", zErr);
4013 zQ2 = malloc( len+100 );
4014 if( zQ2==0 ) return rc;
4015 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4016 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4018 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4020 rc = SQLITE_CORRUPT;
4029 ** Text of a help message
4031 static char zHelp[] =
4032 #ifndef SQLITE_OMIT_AUTHORIZATION
4033 ".auth ON|OFF Show authorizer callbacks\n"
4035 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
4036 ".bail on|off Stop after hitting an error. Default OFF\n"
4037 ".binary on|off Turn binary output on or off. Default OFF\n"
4038 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
4039 ".changes on|off Show number of rows changed by SQL\n"
4040 ".check GLOB Fail if output since .testcase does not match\n"
4041 ".clone NEWDB Clone data into NEWDB from the existing database\n"
4042 ".databases List names and files of attached databases\n"
4043 ".dbinfo ?DB? Show status information about the database\n"
4044 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
4045 " If TABLE specified, only dump tables matching\n"
4046 " LIKE pattern TABLE.\n"
4047 ".echo on|off Turn command echo on or off\n"
4048 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
4049 ".exit Exit this program\n"
4050 /* Because explain mode comes on automatically now, the ".explain" mode
4051 ** is removed from the help screen. It is still supported for legacy, however */
4052 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
4053 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
4054 ".headers on|off Turn display of headers on or off\n"
4055 ".help Show this message\n"
4056 ".import FILE TABLE Import data from FILE into TABLE\n"
4057 #ifndef SQLITE_OMIT_TEST_CONTROL
4058 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
4060 ".indexes ?TABLE? Show names of all indexes\n"
4061 " If TABLE specified, only show indexes for tables\n"
4062 " matching LIKE pattern TABLE.\n"
4063 #ifdef SQLITE_ENABLE_IOTRACE
4064 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
4066 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
4067 ".lint OPTIONS Report potential schema issues. Options:\n"
4068 " fkey-indexes Find missing foreign key indexes\n"
4069 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4070 ".load FILE ?ENTRY? Load an extension library\n"
4072 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
4073 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
4074 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
4075 " csv Comma-separated values\n"
4076 " column Left-aligned columns. (See .width)\n"
4077 " html HTML <table> code\n"
4078 " insert SQL insert statements for TABLE\n"
4079 " line One value per line\n"
4080 " list Values delimited by \"|\"\n"
4081 " quote Escape answers as for SQL\n"
4082 " tabs Tab-separated values\n"
4083 " tcl TCL list elements\n"
4084 ".nullvalue STRING Use STRING in place of NULL values\n"
4085 ".once FILENAME Output for the next SQL command only to FILENAME\n"
4086 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
4087 " The --new option starts with an empty file\n"
4088 ".output ?FILENAME? Send output to FILENAME or stdout\n"
4089 ".print STRING... Print literal STRING\n"
4090 ".prompt MAIN CONTINUE Replace the standard prompts\n"
4091 ".quit Exit this program\n"
4092 ".read FILENAME Execute SQL in FILENAME\n"
4093 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
4094 ".save FILE Write in-memory database into FILE\n"
4095 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
4096 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
4097 " Add --indent for pretty-printing\n"
4098 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
4099 ".separator COL ?ROW? Change the column separator and optionally the row\n"
4100 " separator for both the output mode and .import\n"
4101 #if defined(SQLITE_ENABLE_SESSION)
4102 ".session CMD ... Create or control sessions\n"
4104 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
4105 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
4106 ".show Show the current values for various settings\n"
4107 ".stats ?on|off? Show stats or turn stats on or off\n"
4108 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
4109 ".tables ?TABLE? List names of tables\n"
4110 " If TABLE specified, only list tables matching\n"
4111 " LIKE pattern TABLE.\n"
4112 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
4113 ".timeout MS Try opening locked tables for MS milliseconds\n"
4114 ".timer on|off Turn SQL timer on or off\n"
4115 ".trace FILE|off Output each SQL statement as it is run\n"
4116 ".vfsinfo ?AUX? Information about the top-level VFS\n"
4117 ".vfslist List all available VFSes\n"
4118 ".vfsname ?AUX? Print the name of the VFS stack\n"
4119 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
4120 " Negative values right-justify\n"
4123 #if defined(SQLITE_ENABLE_SESSION)
4125 ** Print help information for the ".sessions" command
4127 void session_help(ShellState *p){
4129 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
4130 "If ?NAME? is omitted, the first defined session is used.\n"
4132 " attach TABLE Attach TABLE\n"
4133 " changeset FILE Write a changeset into FILE\n"
4134 " close Close one session\n"
4135 " enable ?BOOLEAN? Set or query the enable bit\n"
4136 " filter GLOB... Reject tables matching GLOBs\n"
4137 " indirect ?BOOLEAN? Mark or query the indirect status\n"
4138 " isempty Query whether the session is empty\n"
4139 " list List currently open session names\n"
4140 " open DB NAME Open a new session on DB\n"
4141 " patchset FILE Write a patchset into FILE\n"
4147 /* Forward reference */
4148 static int process_input(ShellState *p, FILE *in);
4151 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
4152 ** and return a pointer to the buffer. The caller is responsible for freeing
4155 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
4158 ** For convenience, a nul-terminator byte is always appended to the data read
4159 ** from the file before the buffer is returned. This byte is not included in
4160 ** the final value of (*pnByte), if applicable.
4162 ** NULL is returned if any error is encountered. The final value of *pnByte
4163 ** is undefined in this case.
4165 static char *readFile(const char *zName, int *pnByte){
4166 FILE *in = fopen(zName, "rb");
4170 if( in==0 ) return 0;
4171 fseek(in, 0, SEEK_END);
4174 pBuf = sqlite3_malloc64( nIn+1 );
4175 if( pBuf==0 ) return 0;
4176 nRead = fread(pBuf, nIn, 1, in);
4183 if( pnByte ) *pnByte = nIn;
4187 #if defined(SQLITE_ENABLE_SESSION)
4189 ** Close a single OpenSession object and release all of its associated
4192 static void session_close(OpenSession *pSession){
4194 sqlite3session_delete(pSession->p);
4195 sqlite3_free(pSession->zName);
4196 for(i=0; i<pSession->nFilter; i++){
4197 sqlite3_free(pSession->azFilter[i]);
4199 sqlite3_free(pSession->azFilter);
4200 memset(pSession, 0, sizeof(OpenSession));
4205 ** Close all OpenSession objects and release all associated resources.
4207 #if defined(SQLITE_ENABLE_SESSION)
4208 static void session_close_all(ShellState *p){
4210 for(i=0; i<p->nSession; i++){
4211 session_close(&p->aSession[i]);
4216 # define session_close_all(X)
4220 ** Implementation of the xFilter function for an open session. Omit
4221 ** any tables named by ".session filter" but let all other table through.
4223 #if defined(SQLITE_ENABLE_SESSION)
4224 static int session_filter(void *pCtx, const char *zTab){
4225 OpenSession *pSession = (OpenSession*)pCtx;
4227 for(i=0; i<pSession->nFilter; i++){
4228 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
4235 ** Make sure the database is open. If it is not, then open it. If
4236 ** the database fails to open, print an error message and exit.
4238 static void open_db(ShellState *p, int keepAlive){
4240 sqlite3_initialize();
4241 sqlite3_open(p->zDbFilename, &p->db);
4243 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4244 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4245 p->zDbFilename, sqlite3_errmsg(p->db));
4246 if( keepAlive ) return;
4249 #ifndef SQLITE_OMIT_LOAD_EXTENSION
4250 sqlite3_enable_load_extension(p->db, 1);
4252 sqlite3_fileio_init(p->db, 0, 0);
4253 sqlite3_shathree_init(p->db, 0, 0);
4254 sqlite3_completion_init(p->db, 0, 0);
4255 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
4256 shellAddSchemaName, 0, 0);
4260 #if HAVE_READLINE || HAVE_EDITLINE
4262 ** Readline completion callbacks
4264 static char *readline_completion_generator(const char *text, int state){
4265 static sqlite3_stmt *pStmt = 0;
4269 sqlite3_finalize(pStmt);
4270 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4271 " FROM completion(%Q) ORDER BY 1", text);
4272 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4275 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4276 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
4278 sqlite3_finalize(pStmt);
4284 static char **readline_completion(const char *zText, int iStart, int iEnd){
4285 rl_attempted_completion_over = 1;
4286 return rl_completion_matches(zText, readline_completion_generator);
4289 #elif HAVE_LINENOISE
4291 ** Linenoise completion callback
4293 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
4294 int nLine = (int)strlen(zLine);
4296 sqlite3_stmt *pStmt = 0;
4300 if( nLine>sizeof(zBuf)-30 ) return;
4301 if( zLine[0]=='.' ) return;
4302 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4303 if( i==nLine-1 ) return;
4305 memcpy(zBuf, zLine, iStart);
4306 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4307 " FROM completion(%Q,%Q) ORDER BY 1",
4308 &zLine[iStart], zLine);
4309 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4311 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4312 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4313 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4314 int nCompletion = sqlite3_column_bytes(pStmt, 0);
4315 if( iStart+nCompletion < sizeof(zBuf)-1 ){
4316 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4317 linenoiseAddCompletion(lc, zBuf);
4320 sqlite3_finalize(pStmt);
4325 ** Do C-language style dequoting.
4331 ** \v -> vertical tab
4333 ** \r -> carriage return
4338 ** \NNN -> ascii character NNN in octal
4340 static void resolve_backslashes(char *z){
4343 while( *z && *z!='\\' ) z++;
4344 for(i=j=0; (c = z[i])!=0; i++, j++){
4345 if( c=='\\' && z[i+1]!=0 ){
4363 }else if( c=='\'' ){
4365 }else if( c=='\\' ){
4367 }else if( c>='0' && c<='7' ){
4369 if( z[i+1]>='0' && z[i+1]<='7' ){
4371 c = (c<<3) + z[i] - '0';
4372 if( z[i+1]>='0' && z[i+1]<='7' ){
4374 c = (c<<3) + z[i] - '0';
4385 ** Return the value of a hexadecimal digit. Return -1 if the input
4386 ** is not a hex digit.
4388 static int hexDigitValue(char c){
4389 if( c>='0' && c<='9' ) return c - '0';
4390 if( c>='a' && c<='f' ) return c - 'a' + 10;
4391 if( c>='A' && c<='F' ) return c - 'A' + 10;
4396 ** Interpret zArg as an integer value, possibly with suffixes.
4398 static sqlite3_int64 integerValue(const char *zArg){
4399 sqlite3_int64 v = 0;
4400 static const struct { char *zSuffix; int iMult; } aMult[] = {
4402 { "MiB", 1024*1024 },
4403 { "GiB", 1024*1024*1024 },
4406 { "GB", 1000000000 },
4409 { "G", 1000000000 },
4416 }else if( zArg[0]=='+' ){
4419 if( zArg[0]=='0' && zArg[1]=='x' ){
4422 while( (x = hexDigitValue(zArg[0]))>=0 ){
4427 while( IsDigit(zArg[0]) ){
4428 v = v*10 + zArg[0] - '0';
4432 for(i=0; i<ArraySize(aMult); i++){
4433 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
4434 v *= aMult[i].iMult;
4438 return isNeg? -v : v;
4442 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
4443 ** for TRUE and FALSE. Return the integer value if appropriate.
4445 static int booleanValue(const char *zArg){
4447 if( zArg[0]=='0' && zArg[1]=='x' ){
4448 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4450 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4452 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4453 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4456 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4459 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4465 ** Set or clear a shell flag according to a boolean value.
4467 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4468 if( booleanValue(zArg) ){
4469 ShellSetFlag(p, mFlag);
4471 ShellClearFlag(p, mFlag);
4476 ** Close an output file, assuming it is not stderr or stdout
4478 static void output_file_close(FILE *f){
4479 if( f && f!=stdout && f!=stderr ) fclose(f);
4483 ** Try to open an output file. The names "stdout" and "stderr" are
4484 ** recognized and do the right thing. NULL is returned if the output
4485 ** filename is "off".
4487 static FILE *output_file_open(const char *zFile){
4489 if( strcmp(zFile,"stdout")==0 ){
4491 }else if( strcmp(zFile, "stderr")==0 ){
4493 }else if( strcmp(zFile, "off")==0 ){
4496 f = fopen(zFile, "wb");
4498 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4504 #if !defined(SQLITE_UNTESTABLE)
4505 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
4507 ** A routine for handling output from sqlite3_trace().
4509 static int sql_trace_callback(
4515 FILE *f = (FILE*)pArg;
4516 UNUSED_PARAMETER(mType);
4517 UNUSED_PARAMETER(pP);
4519 const char *z = (const char*)pX;
4520 int i = (int)strlen(z);
4521 while( i>0 && z[i-1]==';' ){ i--; }
4522 utf8_printf(f, "%.*s;\n", i, z);
4530 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
4531 ** a useful spot to set a debugger breakpoint.
4533 static void test_breakpoint(void){
4534 static int nCall = 0;
4539 ** An object used to read a CSV and other files for import.
4541 typedef struct ImportCtx ImportCtx;
4543 const char *zFile; /* Name of the input file */
4544 FILE *in; /* Read the CSV text from this input stream */
4545 char *z; /* Accumulated text for a field */
4546 int n; /* Number of bytes in z */
4547 int nAlloc; /* Space allocated for z[] */
4548 int nLine; /* Current line number */
4549 int bNotFirst; /* True if one or more bytes already read */
4550 int cTerm; /* Character that terminated the most recent field */
4551 int cColSep; /* The column separator character. (Usually ",") */
4552 int cRowSep; /* The row separator character. (Usually "\n") */
4555 /* Append a single byte to z[] */
4556 static void import_append_char(ImportCtx *p, int c){
4557 if( p->n+1>=p->nAlloc ){
4558 p->nAlloc += p->nAlloc + 100;
4559 p->z = sqlite3_realloc64(p->z, p->nAlloc);
4561 raw_printf(stderr, "out of memory\n");
4565 p->z[p->n++] = (char)c;
4568 /* Read a single field of CSV text. Compatible with rfc4180 and extended
4569 ** with the option of having a separator other than ",".
4571 ** + Input comes from p->in.
4572 ** + Store results in p->z of length p->n. Space to hold p->z comes
4573 ** from sqlite3_malloc64().
4574 ** + Use p->cSep as the column separator. The default is ",".
4575 ** + Use p->rSep as the row separator. The default is "\n".
4576 ** + Keep track of the line number in p->nLine.
4577 ** + Store the character that terminates the field in p->cTerm. Store
4578 ** EOF on end-of-file.
4579 ** + Report syntax errors on stderr
4581 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4583 int cSep = p->cColSep;
4584 int rSep = p->cRowSep;
4587 if( c==EOF || seenInterrupt ){
4593 int startLine = p->nLine;
4598 if( c==rSep ) p->nLine++;
4605 if( (c==cSep && pc==cQuote)
4606 || (c==rSep && pc==cQuote)
4607 || (c==rSep && pc=='\r' && ppc==cQuote)
4608 || (c==EOF && pc==cQuote)
4610 do{ p->n--; }while( p->z[p->n]!=cQuote );
4614 if( pc==cQuote && c!='\r' ){
4615 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
4616 p->zFile, p->nLine, cQuote);
4619 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
4620 p->zFile, startLine, cQuote);
4624 import_append_char(p, c);
4629 /* If this is the first field being parsed and it begins with the
4630 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
4631 if( (c&0xff)==0xef && p->bNotFirst==0 ){
4632 import_append_char(p, c);
4634 if( (c&0xff)==0xbb ){
4635 import_append_char(p, c);
4637 if( (c&0xff)==0xbf ){
4640 return csv_read_one_field(p);
4644 while( c!=EOF && c!=cSep && c!=rSep ){
4645 import_append_char(p, c);
4650 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
4654 if( p->z ) p->z[p->n] = 0;
4659 /* Read a single field of ASCII delimited text.
4661 ** + Input comes from p->in.
4662 ** + Store results in p->z of length p->n. Space to hold p->z comes
4663 ** from sqlite3_malloc64().
4664 ** + Use p->cSep as the column separator. The default is "\x1F".
4665 ** + Use p->rSep as the row separator. The default is "\x1E".
4666 ** + Keep track of the row number in p->nLine.
4667 ** + Store the character that terminates the field in p->cTerm. Store
4668 ** EOF on end-of-file.
4669 ** + Report syntax errors on stderr
4671 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
4673 int cSep = p->cColSep;
4674 int rSep = p->cRowSep;
4677 if( c==EOF || seenInterrupt ){
4681 while( c!=EOF && c!=cSep && c!=rSep ){
4682 import_append_char(p, c);
4689 if( p->z ) p->z[p->n] = 0;
4694 ** Try to transfer data for table zTable. If an error is seen while
4695 ** moving forward, try to go backwards. The backwards movement won't
4696 ** work for WITHOUT ROWID tables.
4698 static void tryToCloneData(
4703 sqlite3_stmt *pQuery = 0;
4704 sqlite3_stmt *pInsert = 0;
4709 int nTable = (int)strlen(zTable);
4712 const int spinRate = 10000;
4714 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
4715 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4717 utf8_printf(stderr, "Error %d: %s on [%s]\n",
4718 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4722 n = sqlite3_column_count(pQuery);
4723 zInsert = sqlite3_malloc64(200 + nTable + n*3);
4725 raw_printf(stderr, "out of memory\n");
4728 sqlite3_snprintf(200+nTable,zInsert,
4729 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
4730 i = (int)strlen(zInsert);
4732 memcpy(zInsert+i, ",?", 2);
4735 memcpy(zInsert+i, ");", 3);
4736 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
4738 utf8_printf(stderr, "Error %d: %s on [%s]\n",
4739 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
4744 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4746 switch( sqlite3_column_type(pQuery, i) ){
4748 sqlite3_bind_null(pInsert, i+1);
4751 case SQLITE_INTEGER: {
4752 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
4755 case SQLITE_FLOAT: {
4756 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
4760 sqlite3_bind_text(pInsert, i+1,
4761 (const char*)sqlite3_column_text(pQuery,i),
4766 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
4767 sqlite3_column_bytes(pQuery,i),
4773 rc = sqlite3_step(pInsert);
4774 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4775 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4776 sqlite3_errmsg(newDb));
4778 sqlite3_reset(pInsert);
4780 if( (cnt%spinRate)==0 ){
4781 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4785 if( rc==SQLITE_DONE ) break;
4786 sqlite3_finalize(pQuery);
4787 sqlite3_free(zQuery);
4788 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4790 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4792 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4795 } /* End for(k=0...) */
4798 sqlite3_finalize(pQuery);
4799 sqlite3_finalize(pInsert);
4800 sqlite3_free(zQuery);
4801 sqlite3_free(zInsert);
4806 ** Try to transfer all rows of the schema that match zWhere. For
4807 ** each row, invoke xForEach() on the object defined by that row.
4808 ** If an error is encountered while moving forward through the
4809 ** sqlite_master table, try again moving backwards.
4811 static void tryToCloneSchema(
4815 void (*xForEach)(ShellState*,sqlite3*,const char*)
4817 sqlite3_stmt *pQuery = 0;
4820 const unsigned char *zName;
4821 const unsigned char *zSql;
4824 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4825 " WHERE %s", zWhere);
4826 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4828 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4829 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4831 goto end_schema_xfer;
4833 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4834 zName = sqlite3_column_text(pQuery, 0);
4835 zSql = sqlite3_column_text(pQuery, 1);
4836 printf("%s... ", zName); fflush(stdout);
4837 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4839 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4840 sqlite3_free(zErrMsg);
4844 xForEach(p, newDb, (const char*)zName);
4848 if( rc!=SQLITE_DONE ){
4849 sqlite3_finalize(pQuery);
4850 sqlite3_free(zQuery);
4851 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4852 " WHERE %s ORDER BY rowid DESC", zWhere);
4853 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4855 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4856 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4858 goto end_schema_xfer;
4860 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4861 zName = sqlite3_column_text(pQuery, 0);
4862 zSql = sqlite3_column_text(pQuery, 1);
4863 printf("%s... ", zName); fflush(stdout);
4864 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4866 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4867 sqlite3_free(zErrMsg);
4871 xForEach(p, newDb, (const char*)zName);
4877 sqlite3_finalize(pQuery);
4878 sqlite3_free(zQuery);
4882 ** Open a new database file named "zNewDb". Try to recover as much information
4883 ** as possible out of the main database (which might be corrupt) and write it
4886 static void tryToClone(ShellState *p, const char *zNewDb){
4889 if( access(zNewDb,0)==0 ){
4890 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4893 rc = sqlite3_open(zNewDb, &newDb);
4895 utf8_printf(stderr, "Cannot create output database: %s\n",
4896 sqlite3_errmsg(newDb));
4898 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4899 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4900 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4901 tryToCloneSchema(p, newDb, "type!='table'", 0);
4902 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4903 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4905 sqlite3_close(newDb);
4909 ** Change the output file back to stdout
4911 static void output_reset(ShellState *p){
4912 if( p->outfile[0]=='|' ){
4913 #ifndef SQLITE_OMIT_POPEN
4917 output_file_close(p->out);
4924 ** Run an SQL command and return the single integer result.
4926 static int db_int(ShellState *p, const char *zSql){
4927 sqlite3_stmt *pStmt;
4929 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4930 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
4931 res = sqlite3_column_int(pStmt,0);
4933 sqlite3_finalize(pStmt);
4938 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
4940 static unsigned int get2byteInt(unsigned char *a){
4941 return (a[0]<<8) + a[1];
4943 static unsigned int get4byteInt(unsigned char *a){
4944 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
4948 ** Implementation of the ".info" command.
4950 ** Return 1 on error, 2 to exit, and 0 otherwise.
4952 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
4953 static const struct { const char *zName; int ofst; } aField[] = {
4954 { "file change counter:", 24 },
4955 { "database page count:", 28 },
4956 { "freelist page count:", 36 },
4957 { "schema cookie:", 40 },
4958 { "schema format:", 44 },
4959 { "default cache size:", 48 },
4960 { "autovacuum top root:", 52 },
4961 { "incremental vacuum:", 64 },
4962 { "text encoding:", 56 },
4963 { "user version:", 60 },
4964 { "application id:", 68 },
4965 { "software version:", 96 },
4967 static const struct { const char *zName; const char *zSql; } aQuery[] = {
4968 { "number of tables:",
4969 "SELECT count(*) FROM %s WHERE type='table'" },
4970 { "number of indexes:",
4971 "SELECT count(*) FROM %s WHERE type='index'" },
4972 { "number of triggers:",
4973 "SELECT count(*) FROM %s WHERE type='trigger'" },
4974 { "number of views:",
4975 "SELECT count(*) FROM %s WHERE type='view'" },
4977 "SELECT total(length(sql)) FROM %s" },
4979 sqlite3_file *pFile = 0;
4982 char *zDb = nArg>=2 ? azArg[1] : "main";
4983 unsigned char aHdr[100];
4985 if( p->db==0 ) return 1;
4986 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_FILE_POINTER, &pFile);
4987 if( pFile==0 || pFile->pMethods==0 || pFile->pMethods->xRead==0 ){
4990 i = pFile->pMethods->xRead(pFile, aHdr, 100, 0);
4992 raw_printf(stderr, "unable to read database header\n");
4995 i = get2byteInt(aHdr+16);
4996 if( i==1 ) i = 65536;
4997 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
4998 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
4999 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5000 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5001 for(i=0; i<ArraySize(aField); i++){
5002 int ofst = aField[i].ofst;
5003 unsigned int val = get4byteInt(aHdr + ofst);
5004 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5007 if( val==1 ) raw_printf(p->out, " (utf8)");
5008 if( val==2 ) raw_printf(p->out, " (utf16le)");
5009 if( val==3 ) raw_printf(p->out, " (utf16be)");
5012 raw_printf(p->out, "\n");
5015 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
5016 }else if( strcmp(zDb,"temp")==0 ){
5017 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
5019 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
5021 for(i=0; i<ArraySize(aQuery); i++){
5022 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5023 int val = db_int(p, zSql);
5025 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5027 sqlite3_free(zSchemaTab);
5032 ** Print the current sqlite3_errmsg() value to stderr and return 1.
5034 static int shellDatabaseError(sqlite3 *db){
5035 const char *zErr = sqlite3_errmsg(db);
5036 utf8_printf(stderr, "Error: %s\n", zErr);
5041 ** Print an out-of-memory message to stderr and return 1.
5043 static int shellNomemError(void){
5044 raw_printf(stderr, "Error: out of memory\n");
5049 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
5050 ** if they match and FALSE (0) if they do not match.
5054 ** '*' Matches any sequence of zero or more characters.
5056 ** '?' Matches exactly one character.
5058 ** [...] Matches one character from the enclosed list of
5061 ** [^...] Matches one character not in the enclosed list.
5063 ** '#' Matches any sequence of one or more digits with an
5064 ** optional + or - sign in front
5066 ** ' ' Any span of whitespace matches any other span of
5069 ** Extra whitespace at the end of z[] is ignored.
5071 static int testcase_glob(const char *zGlob, const char *z){
5076 while( (c = (*(zGlob++)))!=0 ){
5078 if( !IsSpace(*z) ) return 0;
5079 while( IsSpace(*zGlob) ) zGlob++;
5080 while( IsSpace(*z) ) z++;
5082 while( (c=(*(zGlob++))) == '*' || c=='?' ){
5083 if( c=='?' && (*(z++))==0 ) return 0;
5088 while( *z && testcase_glob(zGlob-1,z)==0 ){
5093 while( (c2 = (*(z++)))!=0 ){
5096 if( c2==0 ) return 0;
5098 if( testcase_glob(zGlob,z) ) return 1;
5102 if( (*(z++))==0 ) return 0;
5108 if( c==0 ) return 0;
5115 if( c==']' ) seen = 1;
5118 while( c2 && c2!=']' ){
5119 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5121 if( c>=prior_c && c<=c2 ) seen = 1;
5131 if( c2==0 || (seen ^ invert)==0 ) return 0;
5133 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5134 if( !IsDigit(z[0]) ) return 0;
5136 while( IsDigit(z[0]) ){ z++; }
5138 if( c!=(*(z++)) ) return 0;
5141 while( IsSpace(*z) ){ z++; }
5147 ** Compare the string as a command-line option with either one or two
5148 ** initial "-" characters.
5150 static int optionMatch(const char *zStr, const char *zOpt){
5151 if( zStr[0]!='-' ) return 0;
5153 if( zStr[0]=='-' ) zStr++;
5154 return strcmp(zStr, zOpt)==0;
5160 int shellDeleteFile(const char *zFilename){
5163 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5167 rc = unlink(zFilename);
5174 ** The implementation of SQL scalar function fkey_collate_clause(), used
5175 ** by the ".lint fkey-indexes" command. This scalar function is always
5176 ** called with four arguments - the parent table name, the parent column name,
5177 ** the child table name and the child column name.
5179 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5181 ** If either of the named tables or columns do not exist, this function
5182 ** returns an empty string. An empty string is also returned if both tables
5183 ** and columns exist but have the same default collation sequence. Or,
5184 ** if both exist but the default collation sequences are different, this
5185 ** function returns the string " COLLATE <parent-collation>", where
5186 ** <parent-collation> is the default collation sequence of the parent column.
5188 static void shellFkeyCollateClause(
5189 sqlite3_context *pCtx,
5191 sqlite3_value **apVal
5193 sqlite3 *db = sqlite3_context_db_handle(pCtx);
5194 const char *zParent;
5195 const char *zParentCol;
5196 const char *zParentSeq;
5198 const char *zChildCol;
5199 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
5203 zParent = (const char*)sqlite3_value_text(apVal[0]);
5204 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5205 zChild = (const char*)sqlite3_value_text(apVal[2]);
5206 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5208 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5209 rc = sqlite3_table_column_metadata(
5210 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5212 if( rc==SQLITE_OK ){
5213 rc = sqlite3_table_column_metadata(
5214 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5218 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5219 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5220 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5227 ** The implementation of dot-command ".lint fkey-indexes".
5229 static int lintFkeyIndexes(
5230 ShellState *pState, /* Current shell tool state */
5231 char **azArg, /* Array of arguments passed to dot command */
5232 int nArg /* Number of entries in azArg[] */
5234 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
5235 FILE *out = pState->out; /* Stream to write non-error output to */
5236 int bVerbose = 0; /* If -verbose is present */
5237 int bGroupByParent = 0; /* If -groupbyparent is present */
5238 int i; /* To iterate through azArg[] */
5239 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
5240 int rc; /* Return code */
5241 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
5244 ** This SELECT statement returns one row for each foreign key constraint
5245 ** in the schema of the main database. The column values are:
5247 ** 0. The text of an SQL statement similar to:
5249 ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?"
5251 ** This is the same SELECT that the foreign keys implementation needs
5252 ** to run internally on child tables. If there is an index that can
5253 ** be used to optimize this query, then it can also be used by the FK
5254 ** implementation to optimize DELETE or UPDATE statements on the parent
5257 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5258 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5259 ** contains an index that can be used to optimize the query.
5261 ** 2. Human readable text that describes the child table and columns. e.g.
5263 ** "child_table(child_key1, child_key2)"
5265 ** 3. Human readable text that describes the parent table and columns. e.g.
5267 ** "parent_table(parent_key1, parent_key2)"
5269 ** 4. A full CREATE INDEX statement for an index that could be used to
5270 ** optimize DELETE or UPDATE statements on the parent table. e.g.
5272 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
5274 ** 5. The name of the parent table.
5276 ** These six values are used by the C logic below to generate the report.
5280 " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
5281 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5282 " || fkey_collate_clause("
5283 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5285 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5286 " || group_concat('*=?', ' AND ') || ')'"
5288 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
5290 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5292 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5293 " || ' ON ' || quote(s.name) || '('"
5294 " || group_concat(quote(f.[from]) ||"
5295 " fkey_collate_clause("
5296 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5300 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
5301 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5302 "GROUP BY s.name, f.id "
5303 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5305 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5307 for(i=2; i<nArg; i++){
5308 int n = (int)strlen(azArg[i]);
5309 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5312 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5317 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5320 return SQLITE_ERROR;
5324 /* Register the fkey_collate_clause() SQL function */
5325 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5326 0, shellFkeyCollateClause, 0, 0
5330 if( rc==SQLITE_OK ){
5331 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5333 if( rc==SQLITE_OK ){
5334 sqlite3_bind_int(pSql, 1, bGroupByParent);
5337 if( rc==SQLITE_OK ){
5340 while( SQLITE_ROW==sqlite3_step(pSql) ){
5342 sqlite3_stmt *pExplain = 0;
5343 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5344 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5345 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5346 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5347 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5348 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5350 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5351 if( rc!=SQLITE_OK ) break;
5352 if( SQLITE_ROW==sqlite3_step(pExplain) ){
5353 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5355 0==sqlite3_strglob(zGlob, zPlan)
5356 || 0==sqlite3_strglob(zGlobIPK, zPlan)
5359 rc = sqlite3_finalize(pExplain);
5360 if( rc!=SQLITE_OK ) break;
5363 raw_printf(stderr, "Error: internal error");
5367 && (bVerbose || res==0)
5368 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5370 raw_printf(out, "-- Parent table %s\n", zParent);
5371 sqlite3_free(zPrev);
5372 zPrev = sqlite3_mprintf("%s", zParent);
5376 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5377 }else if( bVerbose ){
5378 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5379 zIndent, zFrom, zTarget
5384 sqlite3_free(zPrev);
5386 if( rc!=SQLITE_OK ){
5387 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5390 rc2 = sqlite3_finalize(pSql);
5391 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5393 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5396 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5403 ** Implementation of ".lint" dot command.
5405 static int lintDotCommand(
5406 ShellState *pState, /* Current shell tool state */
5407 char **azArg, /* Array of arguments passed to dot command */
5408 int nArg /* Number of entries in azArg[] */
5411 n = (nArg>=2 ? (int)strlen(azArg[1]) : 0);
5412 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5413 return lintFkeyIndexes(pState, azArg, nArg);
5416 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5417 raw_printf(stderr, "Where sub-commands are:\n");
5418 raw_printf(stderr, " fkey-indexes\n");
5419 return SQLITE_ERROR;
5424 ** If an input line begins with "." then invoke this routine to
5425 ** process that line.
5427 ** Return 1 on error, 2 to exit, and 0 otherwise.
5429 static int do_meta_command(char *zLine, ShellState *p){
5436 /* Parse the input line into tokens.
5438 while( zLine[h] && nArg<ArraySize(azArg) ){
5439 while( IsSpace(zLine[h]) ){ h++; }
5440 if( zLine[h]==0 ) break;
5441 if( zLine[h]=='\'' || zLine[h]=='"' ){
5442 int delim = zLine[h++];
5443 azArg[nArg++] = &zLine[h];
5444 while( zLine[h] && zLine[h]!=delim ){
5445 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
5448 if( zLine[h]==delim ){
5451 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
5453 azArg[nArg++] = &zLine[h];
5454 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
5455 if( zLine[h] ) zLine[h++] = 0;
5456 resolve_backslashes(azArg[nArg-1]);
5460 /* Process the input line.
5462 if( nArg==0 ) return 0; /* no tokens, no error */
5463 n = strlen30(azArg[0]);
5466 #ifndef SQLITE_OMIT_AUTHORIZATION
5467 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
5469 raw_printf(stderr, "Usage: .auth ON|OFF\n");
5471 goto meta_command_exit;
5474 if( booleanValue(azArg[1]) ){
5475 sqlite3_set_authorizer(p->db, shellAuth, p);
5477 sqlite3_set_authorizer(p->db, 0, 0);
5482 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
5483 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
5485 const char *zDestFile = 0;
5486 const char *zDb = 0;
5488 sqlite3_backup *pBackup;
5490 for(j=1; j<nArg; j++){
5491 const char *z = azArg[j];
5493 while( z[0]=='-' ) z++;
5494 /* No options to process at this time */
5496 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
5499 }else if( zDestFile==0 ){
5500 zDestFile = azArg[j];
5503 zDestFile = azArg[j];
5505 raw_printf(stderr, "too many arguments to .backup\n");
5510 raw_printf(stderr, "missing FILENAME argument on .backup\n");
5513 if( zDb==0 ) zDb = "main";
5514 rc = sqlite3_open(zDestFile, &pDest);
5515 if( rc!=SQLITE_OK ){
5516 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
5517 sqlite3_close(pDest);
5521 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
5523 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5524 sqlite3_close(pDest);
5527 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
5528 sqlite3_backup_finish(pBackup);
5529 if( rc==SQLITE_DONE ){
5532 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
5535 sqlite3_close(pDest);
5538 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
5540 bail_on_error = booleanValue(azArg[1]);
5542 raw_printf(stderr, "Usage: .bail on|off\n");
5547 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
5549 if( booleanValue(azArg[1]) ){
5550 setBinaryMode(p->out, 1);
5552 setTextMode(p->out, 1);
5555 raw_printf(stderr, "Usage: .binary on|off\n");
5560 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
5562 #if defined(_WIN32) || defined(WIN32)
5563 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
5564 rc = !SetCurrentDirectoryW(z);
5567 rc = chdir(azArg[1]);
5570 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
5574 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
5579 /* The undocumented ".breakpoint" command causes a call to the no-op
5580 ** routine named test_breakpoint().
5582 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
5586 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
5588 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
5590 raw_printf(stderr, "Usage: .changes on|off\n");
5595 /* Cancel output redirection, if it is currently set (by .testcase)
5596 ** Then read the content of the testcase-out.txt file and compare against
5597 ** azArg[1]. If there are differences, report an error and exit.
5599 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
5603 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
5605 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
5606 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
5608 }else if( testcase_glob(azArg[1],zRes)==0 ){
5610 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
5611 p->zTestcase, azArg[1], zRes);
5614 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
5620 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
5622 tryToClone(p, azArg[1]);
5624 raw_printf(stderr, "Usage: .clone FILENAME\n");
5629 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
5633 memcpy(&data, p, sizeof(data));
5634 data.showHeader = 0;
5635 data.cMode = data.mode = MODE_List;
5636 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
5638 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
5639 callback, &data, &zErrMsg);
5641 utf8_printf(stderr,"Error: %s\n", zErrMsg);
5642 sqlite3_free(zErrMsg);
5647 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
5648 rc = shell_dbinfo_command(p, nArg, azArg);
5651 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
5652 const char *zLike = 0;
5654 int savedShowHeader = p->showHeader;
5655 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
5656 for(i=1; i<nArg; i++){
5657 if( azArg[i][0]=='-' ){
5658 const char *z = azArg[i]+1;
5659 if( z[0]=='-' ) z++;
5660 if( strcmp(z,"preserve-rowids")==0 ){
5661 #ifdef SQLITE_OMIT_VIRTUALTABLE
5662 raw_printf(stderr, "The --preserve-rowids option is not compatible"
5663 " with SQLITE_OMIT_VIRTUALTABLE\n");
5665 goto meta_command_exit;
5667 ShellSetFlag(p, SHFLG_PreserveRowid);
5670 if( strcmp(z,"newlines")==0 ){
5671 ShellSetFlag(p, SHFLG_Newlines);
5674 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
5676 goto meta_command_exit;
5679 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
5680 "?--newlines? ?LIKE-PATTERN?\n");
5682 goto meta_command_exit;
5688 /* When playing back a "dump", the content might appear in an order
5689 ** which causes immediate foreign key constraints to be violated.
5690 ** So disable foreign-key constraint enforcement to prevent problems. */
5691 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
5692 raw_printf(p->out, "BEGIN TRANSACTION;\n");
5693 p->writableSchema = 0;
5695 /* Set writable_schema=ON since doing so forces SQLite to initialize
5696 ** as much of the schema as it can even if the sqlite_master table is
5698 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
5701 run_schema_dump_query(p,
5702 "SELECT name, type, sql FROM sqlite_master "
5703 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
5705 run_schema_dump_query(p,
5706 "SELECT name, type, sql FROM sqlite_master "
5707 "WHERE name=='sqlite_sequence'"
5709 run_table_dump_query(p,
5710 "SELECT sql FROM sqlite_master "
5711 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
5715 zSql = sqlite3_mprintf(
5716 "SELECT name, type, sql FROM sqlite_master "
5717 "WHERE tbl_name LIKE %Q AND type=='table'"
5718 " AND sql NOT NULL", zLike);
5719 run_schema_dump_query(p,zSql);
5721 zSql = sqlite3_mprintf(
5722 "SELECT sql FROM sqlite_master "
5723 "WHERE sql NOT NULL"
5724 " AND type IN ('index','trigger','view')"
5725 " AND tbl_name LIKE %Q", zLike);
5726 run_table_dump_query(p, zSql, 0);
5729 if( p->writableSchema ){
5730 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
5731 p->writableSchema = 0;
5733 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5734 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
5735 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
5736 p->showHeader = savedShowHeader;
5739 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
5741 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
5743 raw_printf(stderr, "Usage: .echo on|off\n");
5748 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
5750 if( strcmp(azArg[1],"full")==0 ){
5753 p->autoEQP = booleanValue(azArg[1]);
5756 raw_printf(stderr, "Usage: .eqp on|off|full\n");
5761 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
5762 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
5766 /* The ".explain" command is automatic now. It is largely pointless. It
5767 ** retained purely for backwards compatibility */
5768 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
5771 if( strcmp(azArg[1],"auto")==0 ){
5774 val = booleanValue(azArg[1]);
5777 if( val==1 && p->mode!=MODE_Explain ){
5778 p->normalMode = p->mode;
5779 p->mode = MODE_Explain;
5782 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5784 }else if( val==99 ){
5785 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
5790 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
5794 memcpy(&data, p, sizeof(data));
5795 data.showHeader = 0;
5796 data.cMode = data.mode = MODE_Semi;
5797 if( nArg==2 && optionMatch(azArg[1], "indent") ){
5798 data.cMode = data.mode = MODE_Pretty;
5802 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
5804 goto meta_command_exit;
5807 rc = sqlite3_exec(p->db,
5809 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
5810 " FROM sqlite_master UNION ALL"
5811 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
5812 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
5814 callback, &data, &zErrMsg
5816 if( rc==SQLITE_OK ){
5817 sqlite3_stmt *pStmt;
5818 rc = sqlite3_prepare_v2(p->db,
5819 "SELECT rowid FROM sqlite_master"
5820 " WHERE name GLOB 'sqlite_stat[134]'",
5822 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
5823 sqlite3_finalize(pStmt);
5826 raw_printf(p->out, "/* No STAT tables available */\n");
5828 raw_printf(p->out, "ANALYZE sqlite_master;\n");
5829 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
5830 callback, &data, &zErrMsg);
5831 data.cMode = data.mode = MODE_Insert;
5832 data.zDestTable = "sqlite_stat1";
5833 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
5834 shell_callback, &data,&zErrMsg);
5835 data.zDestTable = "sqlite_stat3";
5836 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
5837 shell_callback, &data,&zErrMsg);
5838 data.zDestTable = "sqlite_stat4";
5839 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
5840 shell_callback, &data, &zErrMsg);
5841 raw_printf(p->out, "ANALYZE sqlite_master;\n");
5845 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
5847 p->showHeader = booleanValue(azArg[1]);
5849 raw_printf(stderr, "Usage: .headers on|off\n");
5854 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
5855 utf8_printf(p->out, "%s", zHelp);
5858 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
5859 char *zTable; /* Insert data into this table */
5860 char *zFile; /* Name of file to extra content from */
5861 sqlite3_stmt *pStmt = NULL; /* A statement */
5862 int nCol; /* Number of columns in the table */
5863 int nByte; /* Number of bytes in an SQL string */
5864 int i, j; /* Loop counters */
5865 int needCommit; /* True to COMMIT or ROLLBACK at end */
5866 int nSep; /* Number of bytes in p->colSeparator[] */
5867 char *zSql; /* An SQL statement */
5868 ImportCtx sCtx; /* Reader context */
5869 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
5870 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
5873 raw_printf(stderr, "Usage: .import FILE TABLE\n");
5874 goto meta_command_exit;
5879 memset(&sCtx, 0, sizeof(sCtx));
5881 nSep = strlen30(p->colSeparator);
5884 "Error: non-null column separator required for import\n");
5888 raw_printf(stderr, "Error: multi-character column separators not allowed"
5892 nSep = strlen30(p->rowSeparator);
5894 raw_printf(stderr, "Error: non-null row separator required for import\n");
5897 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
5898 /* When importing CSV (only), if the row separator is set to the
5899 ** default output row separator, change it to the default input
5900 ** row separator. This avoids having to maintain different input
5901 ** and output row separators. */
5902 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
5903 nSep = strlen30(p->rowSeparator);
5906 raw_printf(stderr, "Error: multi-character row separators not allowed"
5912 if( sCtx.zFile[0]=='|' ){
5913 #ifdef SQLITE_OMIT_POPEN
5914 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5917 sCtx.in = popen(sCtx.zFile+1, "r");
5918 sCtx.zFile = "<pipe>";
5922 sCtx.in = fopen(sCtx.zFile, "rb");
5925 if( p->mode==MODE_Ascii ){
5926 xRead = ascii_read_one_field;
5928 xRead = csv_read_one_field;
5931 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5934 sCtx.cColSep = p->colSeparator[0];
5935 sCtx.cRowSep = p->rowSeparator[0];
5936 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
5938 raw_printf(stderr, "Error: out of memory\n");
5942 nByte = strlen30(zSql);
5943 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5944 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
5945 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
5946 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
5948 while( xRead(&sCtx) ){
5949 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
5951 if( sCtx.cTerm!=sCtx.cColSep ) break;
5954 sqlite3_free(zCreate);
5955 sqlite3_free(sCtx.z);
5957 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
5960 zCreate = sqlite3_mprintf("%z\n)", zCreate);
5961 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
5962 sqlite3_free(zCreate);
5964 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
5965 sqlite3_errmsg(p->db));
5966 sqlite3_free(sCtx.z);
5970 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5974 if (pStmt) sqlite3_finalize(pStmt);
5975 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
5979 nCol = sqlite3_column_count(pStmt);
5980 sqlite3_finalize(pStmt);
5982 if( nCol==0 ) return 0; /* no columns, no error */
5983 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
5985 raw_printf(stderr, "Error: out of memory\n");
5989 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
5991 for(i=1; i<nCol; i++){
5997 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6000 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6001 if (pStmt) sqlite3_finalize(pStmt);
6005 needCommit = sqlite3_get_autocommit(p->db);
6006 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
6008 int startLine = sCtx.nLine;
6009 for(i=0; i<nCol; i++){
6010 char *z = xRead(&sCtx);
6012 ** Did we reach end-of-file before finding any columns?
6013 ** If so, stop instead of NULL filling the remaining columns.
6015 if( z==0 && i==0 ) break;
6017 ** Did we reach end-of-file OR end-of-line before finding any
6018 ** columns in ASCII mode? If so, stop instead of NULL filling
6019 ** the remaining columns.
6021 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
6022 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
6023 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
6024 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6025 "filling the rest with NULL\n",
6026 sCtx.zFile, startLine, nCol, i+1);
6028 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
6031 if( sCtx.cTerm==sCtx.cColSep ){
6035 }while( sCtx.cTerm==sCtx.cColSep );
6036 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
6038 sCtx.zFile, startLine, nCol, i);
6041 sqlite3_step(pStmt);
6042 rc = sqlite3_reset(pStmt);
6043 if( rc!=SQLITE_OK ){
6044 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
6045 startLine, sqlite3_errmsg(p->db));
6048 }while( sCtx.cTerm!=EOF );
6051 sqlite3_free(sCtx.z);
6052 sqlite3_finalize(pStmt);
6053 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
6056 #ifndef SQLITE_UNTESTABLE
6057 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
6060 sqlite3_stmt *pStmt;
6064 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
6066 goto meta_command_exit;
6069 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
6070 " WHERE name='%q' AND type='index'", azArg[1]);
6071 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6073 if( sqlite3_step(pStmt)==SQLITE_ROW ){
6074 tnum = sqlite3_column_int(pStmt, 0);
6076 sqlite3_finalize(pStmt);
6078 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
6080 goto meta_command_exit;
6082 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
6083 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
6086 while( sqlite3_step(pStmt)==SQLITE_ROW ){
6088 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
6091 if( sqlite3_column_int(pStmt,1)==-1 ){
6094 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
6099 zCollist = sqlite3_mprintf("\"%w\"", zCol);
6101 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
6104 sqlite3_finalize(pStmt);
6105 zSql = sqlite3_mprintf(
6106 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
6107 azArg[2], zCollist, zCollist);
6108 sqlite3_free(zCollist);
6109 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
6110 if( rc==SQLITE_OK ){
6111 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
6112 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
6114 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
6116 utf8_printf(stdout, "%s;\n", zSql);
6118 "WARNING: writing to an imposter table will corrupt the index!\n"
6122 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
6127 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
6129 #ifdef SQLITE_ENABLE_IOTRACE
6130 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
6131 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
6132 if( iotrace && iotrace!=stdout ) fclose(iotrace);
6136 }else if( strcmp(azArg[1], "-")==0 ){
6137 sqlite3IoTrace = iotracePrintf;
6140 iotrace = fopen(azArg[1], "w");
6142 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
6146 sqlite3IoTrace = iotracePrintf;
6152 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
6153 static const struct {
6154 const char *zLimitName; /* Name of a limit */
6155 int limitCode; /* Integer code for that limit */
6157 { "length", SQLITE_LIMIT_LENGTH },
6158 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
6159 { "column", SQLITE_LIMIT_COLUMN },
6160 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
6161 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
6162 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
6163 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
6164 { "attached", SQLITE_LIMIT_ATTACHED },
6165 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
6166 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
6167 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
6168 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
6173 for(i=0; i<ArraySize(aLimit); i++){
6174 printf("%20s %d\n", aLimit[i].zLimitName,
6175 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
6178 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
6180 goto meta_command_exit;
6183 n2 = strlen30(azArg[1]);
6184 for(i=0; i<ArraySize(aLimit); i++){
6185 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
6189 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
6191 goto meta_command_exit;
6196 utf8_printf(stderr, "unknown limit: \"%s\"\n"
6197 "enter \".limits\" with no arguments for a list.\n",
6200 goto meta_command_exit;
6203 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
6204 (int)integerValue(azArg[2]));
6206 printf("%20s %d\n", aLimit[iLimit].zLimitName,
6207 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
6211 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
6213 lintDotCommand(p, azArg, nArg);
6216 #ifndef SQLITE_OMIT_LOAD_EXTENSION
6217 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
6218 const char *zFile, *zProc;
6221 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
6223 goto meta_command_exit;
6226 zProc = nArg>=3 ? azArg[2] : 0;
6228 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
6229 if( rc!=SQLITE_OK ){
6230 utf8_printf(stderr, "Error: %s\n", zErrMsg);
6231 sqlite3_free(zErrMsg);
6237 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
6239 raw_printf(stderr, "Usage: .log FILENAME\n");
6242 const char *zFile = azArg[1];
6243 output_file_close(p->pLog);
6244 p->pLog = output_file_open(zFile);
6248 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
6249 const char *zMode = nArg>=2 ? azArg[1] : "";
6250 int n2 = (int)strlen(zMode);
6252 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
6253 p->mode = MODE_Line;
6254 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6255 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
6256 p->mode = MODE_Column;
6257 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6258 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
6259 p->mode = MODE_List;
6260 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
6261 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6262 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
6263 p->mode = MODE_Html;
6264 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
6266 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
6267 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
6268 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
6270 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
6271 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
6272 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
6273 p->mode = MODE_List;
6274 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
6275 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
6276 p->mode = MODE_Insert;
6277 set_table_name(p, nArg>=3 ? azArg[2] : "table");
6278 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
6279 p->mode = MODE_Quote;
6280 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
6281 p->mode = MODE_Ascii;
6282 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
6283 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
6284 }else if( nArg==1 ){
6285 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
6287 raw_printf(stderr, "Error: mode should be one of: "
6288 "ascii column csv html insert line list quote tabs tcl\n");
6294 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
6296 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
6297 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
6299 raw_printf(stderr, "Usage: .nullvalue STRING\n");
6304 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
6305 char *zNewFilename; /* Name of the database file to open */
6306 int iName = 1; /* Index in azArg[] of the filename */
6307 int newFlag = 0; /* True to delete file before opening */
6308 /* Close the existing database */
6309 session_close_all(p);
6310 sqlite3_close(p->db);
6313 sqlite3_free(p->zFreeOnClose);
6314 p->zFreeOnClose = 0;
6315 /* Check for command-line arguments */
6316 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
6317 const char *z = azArg[iName];
6318 if( optionMatch(z,"new") ){
6320 }else if( z[0]=='-' ){
6321 utf8_printf(stderr, "unknown option: %s\n", z);
6323 goto meta_command_exit;
6326 /* If a filename is specified, try to open it first */
6327 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
6329 if( newFlag ) shellDeleteFile(zNewFilename);
6330 p->zDbFilename = zNewFilename;
6333 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
6334 sqlite3_free(zNewFilename);
6336 p->zFreeOnClose = zNewFilename;
6340 /* As a fall-back open a TEMP database */
6347 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
6349 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
6351 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
6353 goto meta_command_exit;
6355 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
6357 raw_printf(stderr, "Usage: .once FILE\n");
6359 goto meta_command_exit;
6366 if( zFile[0]=='|' ){
6367 #ifdef SQLITE_OMIT_POPEN
6368 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
6372 p->out = popen(zFile + 1, "w");
6374 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
6378 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6382 p->out = output_file_open(zFile);
6384 if( strcmp(zFile,"off")!=0 ){
6385 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
6390 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
6395 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
6397 for(i=1; i<nArg; i++){
6398 if( i>1 ) raw_printf(p->out, " ");
6399 utf8_printf(p->out, "%s", azArg[i]);
6401 raw_printf(p->out, "\n");
6404 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
6406 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
6409 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
6413 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
6417 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
6420 raw_printf(stderr, "Usage: .read FILE\n");
6422 goto meta_command_exit;
6424 alt = fopen(azArg[1], "rb");
6426 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
6429 rc = process_input(p, alt);
6434 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
6435 const char *zSrcFile;
6438 sqlite3_backup *pBackup;
6442 zSrcFile = azArg[1];
6444 }else if( nArg==3 ){
6445 zSrcFile = azArg[2];
6448 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
6450 goto meta_command_exit;
6452 rc = sqlite3_open(zSrcFile, &pSrc);
6453 if( rc!=SQLITE_OK ){
6454 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
6455 sqlite3_close(pSrc);
6459 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
6461 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6462 sqlite3_close(pSrc);
6465 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
6466 || rc==SQLITE_BUSY ){
6467 if( rc==SQLITE_BUSY ){
6468 if( nTimeout++ >= 3 ) break;
6472 sqlite3_backup_finish(pBackup);
6473 if( rc==SQLITE_DONE ){
6475 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
6476 raw_printf(stderr, "Error: source database is busy\n");
6479 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6482 sqlite3_close(pSrc);
6486 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
6488 p->scanstatsOn = booleanValue(azArg[1]);
6489 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
6490 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
6493 raw_printf(stderr, "Usage: .scanstats on|off\n");
6498 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
6502 const char *zDiv = 0;
6506 memcpy(&data, p, sizeof(data));
6507 data.showHeader = 0;
6508 data.cMode = data.mode = MODE_Semi;
6510 if( nArg>=2 && optionMatch(azArg[1], "indent") ){
6511 data.cMode = data.mode = MODE_Pretty;
6513 if( nArg==2 ) azArg[1] = azArg[2];
6515 if( nArg==2 && azArg[1][0]!='-' ){
6517 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
6518 if( strcmp(azArg[1],"sqlite_master")==0 ){
6519 char *new_argv[2], *new_colv[2];
6520 new_argv[0] = "CREATE TABLE sqlite_master (\n"
6524 " rootpage integer,\n"
6528 new_colv[0] = "sql";
6530 callback(&data, 1, new_argv, new_colv);
6532 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
6533 char *new_argv[2], *new_colv[2];
6534 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
6538 " rootpage integer,\n"
6542 new_colv[0] = "sql";
6544 callback(&data, 1, new_argv, new_colv);
6549 }else if( nArg==1 ){
6552 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
6554 goto meta_command_exit;
6557 sqlite3_stmt *pStmt = 0;
6558 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
6561 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
6562 sqlite3_finalize(pStmt);
6564 goto meta_command_exit;
6566 appendText(&sSelect, "SELECT sql FROM", 0);
6568 while( sqlite3_step(pStmt)==SQLITE_ROW ){
6569 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
6571 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
6572 appendText(&sSelect, zDiv, 0);
6573 zDiv = " UNION ALL ";
6574 if( strcmp(zDb, "main")!=0 ){
6575 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
6576 appendText(&sSelect, zDb, '"');
6577 appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0);
6578 appendText(&sSelect, zScNum, 0);
6579 appendText(&sSelect, " AS snum, ", 0);
6580 appendText(&sSelect, zDb, '\'');
6581 appendText(&sSelect, " AS sname FROM ", 0);
6582 appendText(&sSelect, zDb, '"');
6583 appendText(&sSelect, ".sqlite_master", 0);
6585 appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0);
6586 appendText(&sSelect, zScNum, 0);
6587 appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0);
6590 sqlite3_finalize(pStmt);
6591 appendText(&sSelect, ") WHERE ", 0);
6593 char *zQarg = sqlite3_mprintf("%Q", azArg[1]);
6594 if( strchr(azArg[1], '.') ){
6595 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
6597 appendText(&sSelect, "lower(tbl_name)", 0);
6599 appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0);
6600 appendText(&sSelect, zQarg, 0);
6601 appendText(&sSelect, " AND ", 0);
6602 sqlite3_free(zQarg);
6604 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
6605 " ORDER BY snum, rowid", 0);
6606 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
6610 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6611 sqlite3_free(zErrMsg);
6613 }else if( rc != SQLITE_OK ){
6614 raw_printf(stderr,"Error: querying schema information\n");
6621 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
6622 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
6623 sqlite3SelectTrace = (int)integerValue(azArg[1]);
6627 #if defined(SQLITE_ENABLE_SESSION)
6628 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
6629 OpenSession *pSession = &p->aSession[0];
6630 char **azCmd = &azArg[1];
6632 int nCmd = nArg - 1;
6634 if( nArg<=1 ) goto session_syntax_error;
6637 for(iSes=0; iSes<p->nSession; iSes++){
6638 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
6640 if( iSes<p->nSession ){
6641 pSession = &p->aSession[iSes];
6645 pSession = &p->aSession[0];
6650 /* .session attach TABLE
6651 ** Invoke the sqlite3session_attach() interface to attach a particular
6652 ** table so that it is never filtered.
6654 if( strcmp(azCmd[0],"attach")==0 ){
6655 if( nCmd!=2 ) goto session_syntax_error;
6656 if( pSession->p==0 ){
6658 raw_printf(stderr, "ERROR: No sessions are open\n");
6660 rc = sqlite3session_attach(pSession->p, azCmd[1]);
6662 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
6668 /* .session changeset FILE
6669 ** .session patchset FILE
6670 ** Write a changeset or patchset into a file. The file is overwritten.
6672 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
6674 if( nCmd!=2 ) goto session_syntax_error;
6675 if( pSession->p==0 ) goto session_not_open;
6676 out = fopen(azCmd[1], "wb");
6678 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
6682 if( azCmd[0][0]=='c' ){
6683 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
6685 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
6688 printf("Error: error code %d\n", rc);
6692 && fwrite(pChng, szChng, 1, out)!=1 ){
6693 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
6696 sqlite3_free(pChng);
6702 ** Close the identified session
6704 if( strcmp(azCmd[0], "close")==0 ){
6705 if( nCmd!=1 ) goto session_syntax_error;
6707 session_close(pSession);
6708 p->aSession[iSes] = p->aSession[--p->nSession];
6712 /* .session enable ?BOOLEAN?
6713 ** Query or set the enable flag
6715 if( strcmp(azCmd[0], "enable")==0 ){
6717 if( nCmd>2 ) goto session_syntax_error;
6718 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6720 ii = sqlite3session_enable(pSession->p, ii);
6721 utf8_printf(p->out, "session %s enable flag = %d\n",
6722 pSession->zName, ii);
6726 /* .session filter GLOB ....
6727 ** Set a list of GLOB patterns of table names to be excluded.
6729 if( strcmp(azCmd[0], "filter")==0 ){
6731 if( nCmd<2 ) goto session_syntax_error;
6733 for(ii=0; ii<pSession->nFilter; ii++){
6734 sqlite3_free(pSession->azFilter[ii]);
6736 sqlite3_free(pSession->azFilter);
6737 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
6738 pSession->azFilter = sqlite3_malloc( nByte );
6739 if( pSession->azFilter==0 ){
6740 raw_printf(stderr, "Error: out or memory\n");
6743 for(ii=1; ii<nCmd; ii++){
6744 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
6746 pSession->nFilter = ii-1;
6750 /* .session indirect ?BOOLEAN?
6751 ** Query or set the indirect flag
6753 if( strcmp(azCmd[0], "indirect")==0 ){
6755 if( nCmd>2 ) goto session_syntax_error;
6756 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
6758 ii = sqlite3session_indirect(pSession->p, ii);
6759 utf8_printf(p->out, "session %s indirect flag = %d\n",
6760 pSession->zName, ii);
6765 ** Determine if the session is empty
6767 if( strcmp(azCmd[0], "isempty")==0 ){
6769 if( nCmd!=1 ) goto session_syntax_error;
6771 ii = sqlite3session_isempty(pSession->p);
6772 utf8_printf(p->out, "session %s isempty flag = %d\n",
6773 pSession->zName, ii);
6778 ** List all currently open sessions
6780 if( strcmp(azCmd[0],"list")==0 ){
6781 for(i=0; i<p->nSession; i++){
6782 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
6786 /* .session open DB NAME
6787 ** Open a new session called NAME on the attached database DB.
6788 ** DB is normally "main".
6790 if( strcmp(azCmd[0],"open")==0 ){
6792 if( nCmd!=3 ) goto session_syntax_error;
6794 if( zName[0]==0 ) goto session_syntax_error;
6795 for(i=0; i<p->nSession; i++){
6796 if( strcmp(p->aSession[i].zName,zName)==0 ){
6797 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
6798 goto meta_command_exit;
6801 if( p->nSession>=ArraySize(p->aSession) ){
6802 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
6803 goto meta_command_exit;
6805 pSession = &p->aSession[p->nSession];
6806 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
6808 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
6810 goto meta_command_exit;
6812 pSession->nFilter = 0;
6813 sqlite3session_table_filter(pSession->p, session_filter, pSession);
6815 pSession->zName = sqlite3_mprintf("%s", zName);
6817 /* If no command name matches, show a syntax error */
6818 session_syntax_error:
6824 /* Undocumented commands for internal testing. Subject to change
6825 ** without notice. */
6826 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
6827 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
6829 for(i=1; i<nArg; i++){
6830 v = booleanValue(azArg[i]);
6831 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
6834 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
6835 int i; sqlite3_int64 v;
6836 for(i=1; i<nArg; i++){
6838 v = integerValue(azArg[i]);
6839 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
6840 utf8_printf(p->out, "%s", zBuf);
6846 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
6847 int bIsInit = 0; /* True to initialize the SELFTEST table */
6848 int bVerbose = 0; /* Verbose output */
6849 int bSelftestExists; /* True if SELFTEST already exists */
6850 int i, k; /* Loop counters */
6851 int nTest = 0; /* Number of tests runs */
6852 int nErr = 0; /* Number of errors seen */
6853 ShellText str; /* Answer for a query */
6854 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
6857 for(i=1; i<nArg; i++){
6858 const char *z = azArg[i];
6859 if( z[0]=='-' && z[1]=='-' ) z++;
6860 if( strcmp(z,"-init")==0 ){
6863 if( strcmp(z,"-v")==0 ){
6867 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
6868 azArg[i], azArg[0]);
6869 raw_printf(stderr, "Should be one of: --init -v\n");
6871 goto meta_command_exit;
6874 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
6876 bSelftestExists = 0;
6878 bSelftestExists = 1;
6881 createSelftestTable(p);
6882 bSelftestExists = 1;
6885 appendText(&str, "x", 0);
6886 for(k=bSelftestExists; k>=0; k--){
6888 rc = sqlite3_prepare_v2(p->db,
6889 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
6892 rc = sqlite3_prepare_v2(p->db,
6893 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
6894 " (1,'run','PRAGMA integrity_check','ok')",
6898 raw_printf(stderr, "Error querying the selftest table\n");
6900 sqlite3_finalize(pStmt);
6901 goto meta_command_exit;
6903 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
6904 int tno = sqlite3_column_int(pStmt, 0);
6905 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
6906 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
6907 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
6911 char *zQuote = sqlite3_mprintf("%q", zSql);
6912 printf("%d: %s %s\n", tno, zOp, zSql);
6913 sqlite3_free(zQuote);
6915 if( strcmp(zOp,"memo")==0 ){
6916 utf8_printf(p->out, "%s\n", zSql);
6918 if( strcmp(zOp,"run")==0 ){
6922 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
6925 utf8_printf(p->out, "Result: %s\n", str.z);
6927 if( rc || zErrMsg ){
6930 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
6931 sqlite3_free(zErrMsg);
6932 }else if( strcmp(zAns,str.z)!=0 ){
6935 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
6936 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
6941 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
6945 } /* End loop over rows of content from SELFTEST */
6946 sqlite3_finalize(pStmt);
6947 } /* End loop over k */
6949 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
6952 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
6953 if( nArg<2 || nArg>3 ){
6954 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
6958 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
6959 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
6962 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
6963 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
6967 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
6968 const char *zLike = 0; /* Which table to checksum. 0 means everything */
6969 int i; /* Loop counter */
6970 int bSchema = 0; /* Also hash the schema */
6971 int bSeparate = 0; /* Hash each table separately */
6972 int iSize = 224; /* Hash algorithm to use */
6973 int bDebug = 0; /* Only show the query that would have run */
6974 sqlite3_stmt *pStmt; /* For querying tables names */
6975 char *zSql; /* SQL to be run */
6976 char *zSep; /* Separator */
6977 ShellText sSql; /* Complete SQL for the query to run the hash */
6978 ShellText sQuery; /* Set of queries used to read all content */
6980 for(i=1; i<nArg; i++){
6981 const char *z = azArg[i];
6984 if( z[0]=='-' ) z++;
6985 if( strcmp(z,"schema")==0 ){
6988 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
6989 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
6991 iSize = atoi(&z[5]);
6993 if( strcmp(z,"debug")==0 ){
6997 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
6998 azArg[i], azArg[0]);
6999 raw_printf(stderr, "Should be one of: --schema"
7000 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
7002 goto meta_command_exit;
7005 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
7007 goto meta_command_exit;
7011 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
7015 zSql = "SELECT lower(name) FROM sqlite_master"
7016 " WHERE type='table' AND coalesce(rootpage,0)>1"
7017 " UNION ALL SELECT 'sqlite_master'"
7018 " ORDER BY 1 collate nocase";
7020 zSql = "SELECT lower(name) FROM sqlite_master"
7021 " WHERE type='table' AND coalesce(rootpage,0)>1"
7022 " AND name NOT LIKE 'sqlite_%'"
7023 " ORDER BY 1 collate nocase";
7025 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7028 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
7030 while( SQLITE_ROW==sqlite3_step(pStmt) ){
7031 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
7032 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
7033 if( strncmp(zTab, "sqlite_",7)!=0 ){
7034 appendText(&sQuery,"SELECT * FROM ", 0);
7035 appendText(&sQuery,zTab,'"');
7036 appendText(&sQuery," NOT INDEXED;", 0);
7037 }else if( strcmp(zTab, "sqlite_master")==0 ){
7038 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
7039 " ORDER BY name;", 0);
7040 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
7041 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
7042 " ORDER BY name;", 0);
7043 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
7044 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
7045 " ORDER BY tbl,idx;", 0);
7046 }else if( strcmp(zTab, "sqlite_stat3")==0
7047 || strcmp(zTab, "sqlite_stat4")==0 ){
7048 appendText(&sQuery, "SELECT * FROM ", 0);
7049 appendText(&sQuery, zTab, 0);
7050 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
7052 appendText(&sSql, zSep, 0);
7053 appendText(&sSql, sQuery.z, '\'');
7055 appendText(&sSql, ",", 0);
7056 appendText(&sSql, zTab, '\'');
7059 sqlite3_finalize(pStmt);
7061 zSql = sqlite3_mprintf(
7063 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
7064 " FROM [sha3sum$query]",
7067 zSql = sqlite3_mprintf(
7069 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
7070 " FROM [sha3sum$query]",
7076 utf8_printf(p->out, "%s\n", zSql);
7078 shell_exec(p->db, zSql, shell_callback, p, 0);
7084 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
7089 raw_printf(stderr, "Usage: .system COMMAND\n");
7091 goto meta_command_exit;
7093 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
7094 for(i=2; i<nArg; i++){
7095 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
7100 if( x ) raw_printf(stderr, "System command returns %d\n", x);
7103 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
7104 static const char *azBool[] = { "off", "on", "full", "unk" };
7107 raw_printf(stderr, "Usage: .show\n");
7109 goto meta_command_exit;
7111 utf8_printf(p->out, "%12.12s: %s\n","echo",
7112 azBool[ShellHasFlag(p, SHFLG_Echo)]);
7113 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
7114 utf8_printf(p->out, "%12.12s: %s\n","explain",
7115 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
7116 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
7117 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
7118 utf8_printf(p->out, "%12.12s: ", "nullvalue");
7119 output_c_string(p->out, p->nullValue);
7120 raw_printf(p->out, "\n");
7121 utf8_printf(p->out,"%12.12s: %s\n","output",
7122 strlen30(p->outfile) ? p->outfile : "stdout");
7123 utf8_printf(p->out,"%12.12s: ", "colseparator");
7124 output_c_string(p->out, p->colSeparator);
7125 raw_printf(p->out, "\n");
7126 utf8_printf(p->out,"%12.12s: ", "rowseparator");
7127 output_c_string(p->out, p->rowSeparator);
7128 raw_printf(p->out, "\n");
7129 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
7130 utf8_printf(p->out, "%12.12s: ", "width");
7131 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
7132 raw_printf(p->out, "%d ", p->colWidth[i]);
7134 raw_printf(p->out, "\n");
7135 utf8_printf(p->out, "%12.12s: %s\n", "filename",
7136 p->zDbFilename ? p->zDbFilename : "");
7139 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
7141 p->statsOn = booleanValue(azArg[1]);
7142 }else if( nArg==1 ){
7143 display_stats(p->db, p, 0);
7145 raw_printf(stderr, "Usage: .stats ?on|off?\n");
7150 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
7151 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
7152 || strncmp(azArg[0], "indexes", n)==0) )
7154 sqlite3_stmt *pStmt;
7161 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
7162 if( rc ) return shellDatabaseError(p->db);
7164 if( nArg>2 && c=='i' ){
7165 /* It is an historical accident that the .indexes command shows an error
7166 ** when called with the wrong number of arguments whereas the .tables
7167 ** command does not. */
7168 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
7170 goto meta_command_exit;
7172 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
7173 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
7174 if( zDbName==0 ) continue;
7175 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
7176 if( sqlite3_stricmp(zDbName, "main")==0 ){
7177 appendText(&s, "SELECT name FROM ", 0);
7179 appendText(&s, "SELECT ", 0);
7180 appendText(&s, zDbName, '\'');
7181 appendText(&s, "||'.'||name FROM ", 0);
7183 appendText(&s, zDbName, '"');
7184 appendText(&s, ".sqlite_master ", 0);
7186 appendText(&s," WHERE type IN ('table','view')"
7187 " AND name NOT LIKE 'sqlite_%'"
7188 " AND name LIKE ?1", 0);
7190 appendText(&s," WHERE type='index'"
7191 " AND tbl_name LIKE ?1", 0);
7194 rc = sqlite3_finalize(pStmt);
7195 appendText(&s, " ORDER BY 1", 0);
7196 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
7198 if( rc ) return shellDatabaseError(p->db);
7200 /* Run the SQL statement prepared by the above block. Store the results
7201 ** as an array of nul-terminated strings in azResult[]. */
7205 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
7207 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
7209 while( sqlite3_step(pStmt)==SQLITE_ROW ){
7212 int n2 = nAlloc*2 + 10;
7213 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
7215 rc = shellNomemError();
7221 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7222 if( 0==azResult[nRow] ){
7223 rc = shellNomemError();
7228 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
7229 rc = shellDatabaseError(p->db);
7232 /* Pretty-print the contents of array azResult[] to the output */
7233 if( rc==0 && nRow>0 ){
7234 int len, maxlen = 0;
7236 int nPrintCol, nPrintRow;
7237 for(i=0; i<nRow; i++){
7238 len = strlen30(azResult[i]);
7239 if( len>maxlen ) maxlen = len;
7241 nPrintCol = 80/(maxlen+2);
7242 if( nPrintCol<1 ) nPrintCol = 1;
7243 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
7244 for(i=0; i<nPrintRow; i++){
7245 for(j=i; j<nRow; j+=nPrintRow){
7246 char *zSp = j<nPrintRow ? "" : " ";
7247 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
7248 azResult[j] ? azResult[j]:"");
7250 raw_printf(p->out, "\n");
7254 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
7255 sqlite3_free(azResult);
7258 /* Begin redirecting output to the file "testcase-out.txt" */
7259 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
7261 p->out = output_file_open("testcase-out.txt");
7263 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
7266 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
7268 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
7272 #ifndef SQLITE_UNTESTABLE
7273 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
7274 static const struct {
7275 const char *zCtrlName; /* Name of a test-control option */
7276 int ctrlCode; /* Integer code for that option */
7278 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
7279 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
7280 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
7281 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
7282 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
7283 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
7284 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
7285 { "assert", SQLITE_TESTCTRL_ASSERT },
7286 { "always", SQLITE_TESTCTRL_ALWAYS },
7287 { "reserve", SQLITE_TESTCTRL_RESERVE },
7288 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
7289 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
7290 { "scratchmalloc", SQLITE_TESTCTRL_SCRATCHMALLOC },
7291 { "byteorder", SQLITE_TESTCTRL_BYTEORDER },
7292 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT },
7293 { "imposter", SQLITE_TESTCTRL_IMPOSTER },
7300 /* convert testctrl text option to value. allow any unique prefix
7301 ** of the option name, or a numerical value. */
7302 n2 = strlen30(azArg[1]);
7303 for(i=0; i<ArraySize(aCtrl); i++){
7304 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
7306 testctrl = aCtrl[i].ctrlCode;
7308 utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
7314 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
7315 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
7316 utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
7320 /* sqlite3_test_control(int, db, int) */
7321 case SQLITE_TESTCTRL_OPTIMIZATIONS:
7322 case SQLITE_TESTCTRL_RESERVE:
7324 int opt = (int)strtol(azArg[2], 0, 0);
7325 rc2 = sqlite3_test_control(testctrl, p->db, opt);
7326 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7328 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
7333 /* sqlite3_test_control(int) */
7334 case SQLITE_TESTCTRL_PRNG_SAVE:
7335 case SQLITE_TESTCTRL_PRNG_RESTORE:
7336 case SQLITE_TESTCTRL_PRNG_RESET:
7337 case SQLITE_TESTCTRL_BYTEORDER:
7339 rc2 = sqlite3_test_control(testctrl);
7340 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7342 utf8_printf(stderr,"Error: testctrl %s takes no options\n",
7347 /* sqlite3_test_control(int, uint) */
7348 case SQLITE_TESTCTRL_PENDING_BYTE:
7350 unsigned int opt = (unsigned int)integerValue(azArg[2]);
7351 rc2 = sqlite3_test_control(testctrl, opt);
7352 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7354 utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
7355 " int option\n", azArg[1]);
7359 /* sqlite3_test_control(int, int) */
7360 case SQLITE_TESTCTRL_ASSERT:
7361 case SQLITE_TESTCTRL_ALWAYS:
7362 case SQLITE_TESTCTRL_NEVER_CORRUPT:
7364 int opt = booleanValue(azArg[2]);
7365 rc2 = sqlite3_test_control(testctrl, opt);
7366 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7368 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
7373 /* sqlite3_test_control(int, char *) */
7374 #ifdef SQLITE_N_KEYWORD
7375 case SQLITE_TESTCTRL_ISKEYWORD:
7377 const char *opt = azArg[2];
7378 rc2 = sqlite3_test_control(testctrl, opt);
7379 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7382 "Error: testctrl %s takes a single char * option\n",
7388 case SQLITE_TESTCTRL_IMPOSTER:
7390 rc2 = sqlite3_test_control(testctrl, p->db,
7392 integerValue(azArg[3]),
7393 integerValue(azArg[4]));
7394 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
7396 raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
7400 case SQLITE_TESTCTRL_BITVEC_TEST:
7401 case SQLITE_TESTCTRL_FAULT_INSTALL:
7402 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
7403 case SQLITE_TESTCTRL_SCRATCHMALLOC:
7406 "Error: CLI support for testctrl %s not implemented\n",
7412 #endif /* !defined(SQLITE_UNTESTABLE) */
7414 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
7416 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
7419 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
7421 enableTimer = booleanValue(azArg[1]);
7422 if( enableTimer && !HAS_TIMER ){
7423 raw_printf(stderr, "Error: timer not available on this system.\n");
7427 raw_printf(stderr, "Usage: .timer on|off\n");
7432 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
7435 raw_printf(stderr, "Usage: .trace FILE|off\n");
7437 goto meta_command_exit;
7439 output_file_close(p->traceOut);
7440 p->traceOut = output_file_open(azArg[1]);
7441 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
7442 if( p->traceOut==0 ){
7443 sqlite3_trace_v2(p->db, 0, 0, 0);
7445 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
7450 #if SQLITE_USER_AUTHENTICATION
7451 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
7453 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
7455 goto meta_command_exit;
7458 if( strcmp(azArg[1],"login")==0 ){
7460 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
7462 goto meta_command_exit;
7464 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
7465 (int)strlen(azArg[3]));
7467 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
7470 }else if( strcmp(azArg[1],"add")==0 ){
7472 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
7474 goto meta_command_exit;
7476 rc = sqlite3_user_add(p->db, azArg[2],
7477 azArg[3], (int)strlen(azArg[3]),
7478 booleanValue(azArg[4]));
7480 raw_printf(stderr, "User-Add failed: %d\n", rc);
7483 }else if( strcmp(azArg[1],"edit")==0 ){
7485 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
7487 goto meta_command_exit;
7489 rc = sqlite3_user_change(p->db, azArg[2],
7490 azArg[3], (int)strlen(azArg[3]),
7491 booleanValue(azArg[4]));
7493 raw_printf(stderr, "User-Edit failed: %d\n", rc);
7496 }else if( strcmp(azArg[1],"delete")==0 ){
7498 raw_printf(stderr, "Usage: .user delete USER\n");
7500 goto meta_command_exit;
7502 rc = sqlite3_user_delete(p->db, azArg[2]);
7504 raw_printf(stderr, "User-Delete failed: %d\n", rc);
7508 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
7510 goto meta_command_exit;
7513 #endif /* SQLITE_USER_AUTHENTICATION */
7515 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
7516 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
7517 sqlite3_libversion(), sqlite3_sourceid());
7520 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
7521 const char *zDbName = nArg==2 ? azArg[1] : "main";
7522 sqlite3_vfs *pVfs = 0;
7524 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
7526 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
7527 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
7528 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
7529 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7534 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
7536 sqlite3_vfs *pCurrent = 0;
7538 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
7540 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
7541 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
7542 pVfs==pCurrent ? " <--- CURRENT" : "");
7543 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
7544 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
7545 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
7547 raw_printf(p->out, "-----------------------------------\n");
7552 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
7553 const char *zDbName = nArg==2 ? azArg[1] : "main";
7556 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
7558 utf8_printf(p->out, "%s\n", zVfsName);
7559 sqlite3_free(zVfsName);
7564 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
7565 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
7566 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
7570 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
7572 assert( nArg<=ArraySize(azArg) );
7573 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
7574 p->colWidth[j-1] = (int)integerValue(azArg[j]);
7579 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
7580 " \"%s\". Enter \".help\" for help\n", azArg[0]);
7587 if( p->outCount==0 ) output_reset(p);
7593 ** Return TRUE if a semicolon occurs anywhere in the first N characters
7596 static int line_contains_semicolon(const char *z, int N){
7598 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
7603 ** Test to see if a line consists entirely of whitespace.
7605 static int _all_whitespace(const char *z){
7607 if( IsSpace(z[0]) ) continue;
7608 if( *z=='/' && z[1]=='*' ){
7610 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
7611 if( *z==0 ) return 0;
7615 if( *z=='-' && z[1]=='-' ){
7617 while( *z && *z!='\n' ){ z++; }
7618 if( *z==0 ) return 1;
7627 ** Return TRUE if the line typed in is an SQL command terminator other
7628 ** than a semi-colon. The SQL Server style "go" command is understood
7629 ** as is the Oracle "/".
7631 static int line_is_command_terminator(const char *zLine){
7632 while( IsSpace(zLine[0]) ){ zLine++; };
7633 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
7634 return 1; /* Oracle */
7636 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
7637 && _all_whitespace(&zLine[2]) ){
7638 return 1; /* SQL Server */
7644 ** Return true if zSql is a complete SQL statement. Return false if it
7645 ** ends in the middle of a string literal or C-style comment.
7647 static int line_is_complete(char *zSql, int nSql){
7649 if( zSql==0 ) return 1;
7652 rc = sqlite3_complete(zSql);
7658 ** Run a single line of SQL
7660 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
7665 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
7667 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
7669 if( rc || zErrMsg ){
7671 if( in!=0 || !stdin_is_interactive ){
7672 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
7673 "Error: near line %d:", startline);
7675 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
7678 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
7679 sqlite3_free(zErrMsg);
7682 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
7685 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
7686 raw_printf(p->out, "changes: %3d total_changes: %d\n",
7687 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
7694 ** Read input from *in and process it. If *in==0 then input
7695 ** is interactive - the user is typing it it. Otherwise, input
7696 ** is coming from a file or device. A prompt is issued and history
7697 ** is saved only if input is interactive. An interrupt signal will
7698 ** cause this routine to exit immediately, unless input is interactive.
7700 ** Return the number of errors.
7702 static int process_input(ShellState *p, FILE *in){
7703 char *zLine = 0; /* A single input line */
7704 char *zSql = 0; /* Accumulated SQL text */
7705 int nLine; /* Length of current line */
7706 int nSql = 0; /* Bytes of zSql[] used */
7707 int nAlloc = 0; /* Allocated zSql[] space */
7708 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
7709 int rc; /* Error code */
7710 int errCnt = 0; /* Number of errors seen */
7711 int lineno = 0; /* Current line number */
7712 int startline = 0; /* Line number for start of current input */
7714 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
7716 zLine = one_input_line(in, zLine, nSql>0);
7719 if( in==0 && stdin_is_interactive ) printf("\n");
7722 if( seenInterrupt ){
7727 if( nSql==0 && _all_whitespace(zLine) ){
7728 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7731 if( zLine && zLine[0]=='.' && nSql==0 ){
7732 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
7733 rc = do_meta_command(zLine, p);
7734 if( rc==2 ){ /* exit requested */
7741 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
7742 memcpy(zLine,";",2);
7744 nLine = strlen30(zLine);
7745 if( nSql+nLine+2>=nAlloc ){
7746 nAlloc = nSql+nLine+100;
7747 zSql = realloc(zSql, nAlloc);
7749 raw_printf(stderr, "Error: out of memory\n");
7756 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
7757 assert( nAlloc>0 && zSql!=0 );
7758 memcpy(zSql, zLine+i, nLine+1-i);
7762 zSql[nSql++] = '\n';
7763 memcpy(zSql+nSql, zLine, nLine+1);
7766 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
7767 && sqlite3_complete(zSql) ){
7768 errCnt += runOneSqlLine(p, zSql, in, startline);
7774 }else if( nSql && _all_whitespace(zSql) ){
7775 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
7779 if( nSql && !_all_whitespace(zSql) ){
7780 runOneSqlLine(p, zSql, in, startline);
7788 ** Return a pathname which is the user's home directory. A
7789 ** 0 return indicates an error of some kind.
7791 static char *find_home_dir(int clearFlag){
7792 static char *home_dir = NULL;
7798 if( home_dir ) return home_dir;
7800 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
7801 && !defined(__RTP__) && !defined(_WRS_KERNEL)
7803 struct passwd *pwent;
7804 uid_t uid = getuid();
7805 if( (pwent=getpwuid(uid)) != NULL) {
7806 home_dir = pwent->pw_dir;
7811 #if defined(_WIN32_WCE)
7812 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
7817 #if defined(_WIN32) || defined(WIN32)
7819 home_dir = getenv("USERPROFILE");
7824 home_dir = getenv("HOME");
7827 #if defined(_WIN32) || defined(WIN32)
7829 char *zDrive, *zPath;
7831 zDrive = getenv("HOMEDRIVE");
7832 zPath = getenv("HOMEPATH");
7833 if( zDrive && zPath ){
7834 n = strlen30(zDrive) + strlen30(zPath) + 1;
7835 home_dir = malloc( n );
7836 if( home_dir==0 ) return 0;
7837 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
7844 #endif /* !_WIN32_WCE */
7847 int n = strlen30(home_dir) + 1;
7848 char *z = malloc( n );
7849 if( z ) memcpy(z, home_dir, n);
7857 ** Read input from the file given by sqliterc_override. Or if that
7858 ** parameter is NULL, take input from ~/.sqliterc
7860 ** Returns the number of errors.
7862 static void process_sqliterc(
7863 ShellState *p, /* Configuration data */
7864 const char *sqliterc_override /* Name of config file. NULL to use default */
7866 char *home_dir = NULL;
7867 const char *sqliterc = sqliterc_override;
7871 if (sqliterc == NULL) {
7872 home_dir = find_home_dir(0);
7874 raw_printf(stderr, "-- warning: cannot find home directory;"
7875 " cannot read ~/.sqliterc\n");
7878 sqlite3_initialize();
7879 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
7882 in = fopen(sqliterc,"rb");
7884 if( stdin_is_interactive ){
7885 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
7887 process_input(p,in);
7894 ** Show available command line options
7896 static const char zOptions[] =
7897 " -ascii set output mode to 'ascii'\n"
7898 " -bail stop after hitting an error\n"
7899 " -batch force batch I/O\n"
7900 " -column set output mode to 'column'\n"
7901 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
7902 " -csv set output mode to 'csv'\n"
7903 " -echo print commands before execution\n"
7904 " -init FILENAME read/process named file\n"
7905 " -[no]header turn headers on or off\n"
7906 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
7907 " -heap SIZE Size of heap for memsys3 or memsys5\n"
7909 " -help show this message\n"
7910 " -html set output mode to HTML\n"
7911 " -interactive force interactive I/O\n"
7912 " -line set output mode to 'line'\n"
7913 " -list set output mode to 'list'\n"
7914 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
7915 " -mmap N default mmap size set to N\n"
7916 #ifdef SQLITE_ENABLE_MULTIPLEX
7917 " -multiplex enable the multiplexor VFS\n"
7919 " -newline SEP set output row separator. Default: '\\n'\n"
7920 " -nullvalue TEXT set text string for NULL values. Default ''\n"
7921 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
7922 " -quote set output mode to 'quote'\n"
7923 " -scratch SIZE N use N slots of SZ bytes each for scratch memory\n"
7924 " -separator SEP set output column separator. Default: '|'\n"
7925 " -stats print memory stats before each finalize\n"
7926 " -version show SQLite version\n"
7927 " -vfs NAME use NAME as the default VFS\n"
7928 #ifdef SQLITE_ENABLE_VFSTRACE
7929 " -vfstrace enable tracing of all VFS calls\n"
7932 static void usage(int showDetail){
7934 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
7935 "FILENAME is the name of an SQLite database. A new database is created\n"
7936 "if the file does not previously exist.\n", Argv0);
7938 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
7940 raw_printf(stderr, "Use the -help option for additional information\n");
7946 ** Initialize the state information in data
7948 static void main_init(ShellState *data) {
7949 memset(data, 0, sizeof(*data));
7950 data->normalMode = data->cMode = data->mode = MODE_List;
7951 data->autoExplain = 1;
7952 memcpy(data->colSeparator,SEP_Column, 2);
7953 memcpy(data->rowSeparator,SEP_Row, 2);
7954 data->showHeader = 0;
7955 data->shellFlgs = SHFLG_Lookaside;
7956 sqlite3_config(SQLITE_CONFIG_URI, 1);
7957 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
7958 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
7959 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
7960 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
7964 ** Output text to the console in a font that attracts extra attention.
7967 static void printBold(const char *zText){
7968 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
7969 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
7970 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
7971 SetConsoleTextAttribute(out,
7972 FOREGROUND_RED|FOREGROUND_INTENSITY
7974 printf("%s", zText);
7975 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
7978 static void printBold(const char *zText){
7979 printf("\033[1m%s\033[0m", zText);
7984 ** Get the argument to an --option. Throw an error and die if no argument
7987 static char *cmdline_option_value(int argc, char **argv, int i){
7989 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
7990 argv[0], argv[argc-1]);
7996 #ifndef SQLITE_SHELL_IS_UTF8
7997 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
7998 # define SQLITE_SHELL_IS_UTF8 (0)
8000 # define SQLITE_SHELL_IS_UTF8 (1)
8004 #if SQLITE_SHELL_IS_UTF8
8005 int SQLITE_CDECL main(int argc, char **argv){
8007 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
8012 const char *zInitFile = 0;
8015 int warnInmemoryDb = 0;
8020 setBinaryMode(stdin, 0);
8021 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
8022 stdin_is_interactive = isatty(0);
8023 stdout_is_console = isatty(1);
8025 #if USE_SYSTEM_SQLITE+0!=1
8026 if( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)!=0 ){
8027 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
8028 sqlite3_sourceid(), SQLITE_SOURCE_ID);
8033 #if !SQLITE_SHELL_IS_UTF8
8034 sqlite3_initialize();
8035 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
8037 raw_printf(stderr, "out of memory\n");
8040 for(i=0; i<argc; i++){
8041 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
8043 raw_printf(stderr, "out of memory\n");
8048 assert( argc>=1 && argv && argv[0] );
8051 /* Make sure we have a valid signal handler early, before anything
8055 signal(SIGINT, interrupt_handler);
8058 #ifdef SQLITE_SHELL_DBNAME_PROC
8060 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
8061 ** of a C-function that will provide the name of the database file. Use
8062 ** this compile-time option to embed this shell program in larger
8064 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
8065 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
8070 /* Do an initial pass through the command-line argument to locate
8071 ** the name of the database file, the name of the initialization file,
8072 ** the size of the alternative malloc heap,
8073 ** and the first command to execute.
8075 for(i=1; i<argc; i++){
8079 if( data.zDbFilename==0 ){
8080 data.zDbFilename = z;
8082 /* Excesss arguments are interpreted as SQL (or dot-commands) and
8083 ** mean that nothing is read from stdin */
8086 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
8088 raw_printf(stderr, "out of memory\n");
8094 if( z[1]=='-' ) z++;
8095 if( strcmp(z,"-separator")==0
8096 || strcmp(z,"-nullvalue")==0
8097 || strcmp(z,"-newline")==0
8098 || strcmp(z,"-cmd")==0
8100 (void)cmdline_option_value(argc, argv, ++i);
8101 }else if( strcmp(z,"-init")==0 ){
8102 zInitFile = cmdline_option_value(argc, argv, ++i);
8103 }else if( strcmp(z,"-batch")==0 ){
8104 /* Need to check for batch mode here to so we can avoid printing
8105 ** informational messages (like from process_sqliterc) before
8106 ** we do the actual processing of arguments later in a second pass.
8108 stdin_is_interactive = 0;
8109 }else if( strcmp(z,"-heap")==0 ){
8110 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8112 sqlite3_int64 szHeap;
8114 zSize = cmdline_option_value(argc, argv, ++i);
8115 szHeap = integerValue(zSize);
8116 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
8117 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
8119 (void)cmdline_option_value(argc, argv, ++i);
8121 }else if( strcmp(z,"-scratch")==0 ){
8123 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8124 if( sz>400000 ) sz = 400000;
8125 if( sz<2500 ) sz = 2500;
8126 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8129 sqlite3_config(SQLITE_CONFIG_SCRATCH, malloc(n*sz+1), sz, n);
8130 data.shellFlgs |= SHFLG_Scratch;
8131 }else if( strcmp(z,"-pagecache")==0 ){
8133 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8134 if( sz>70000 ) sz = 70000;
8136 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8137 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
8138 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
8139 data.shellFlgs |= SHFLG_Pagecache;
8140 }else if( strcmp(z,"-lookaside")==0 ){
8142 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
8144 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
8146 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
8147 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
8148 #ifdef SQLITE_ENABLE_VFSTRACE
8149 }else if( strcmp(z,"-vfstrace")==0 ){
8150 extern int vfstrace_register(
8151 const char *zTraceName,
8152 const char *zOldVfsName,
8153 int (*xOut)(const char*,void*),
8157 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
8159 #ifdef SQLITE_ENABLE_MULTIPLEX
8160 }else if( strcmp(z,"-multiplex")==0 ){
8161 extern int sqlite3_multiple_initialize(const char*,int);
8162 sqlite3_multiplex_initialize(0, 1);
8164 }else if( strcmp(z,"-mmap")==0 ){
8165 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
8166 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
8167 }else if( strcmp(z,"-vfs")==0 ){
8168 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
8170 sqlite3_vfs_register(pVfs, 1);
8172 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
8177 if( data.zDbFilename==0 ){
8178 #ifndef SQLITE_OMIT_MEMORYDB
8179 data.zDbFilename = ":memory:";
8180 warnInmemoryDb = argc==1;
8182 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
8188 /* Go ahead and open the database file if it already exists. If the
8189 ** file does not exist, delay opening it. This prevents empty database
8190 ** files from being created if a user mistypes the database name argument
8191 ** to the sqlite command-line tool.
8193 if( access(data.zDbFilename, 0)==0 ){
8197 /* Process the initialization file if there is one. If no -init option
8198 ** is given on the command line, look for a file named ~/.sqliterc and
8199 ** try to process it.
8201 process_sqliterc(&data,zInitFile);
8203 /* Make a second pass through the command-line argument and set
8204 ** options. This second pass is delayed until after the initialization
8205 ** file is processed so that the command-line arguments will override
8206 ** settings in the initialization file.
8208 for(i=1; i<argc; i++){
8210 if( z[0]!='-' ) continue;
8211 if( z[1]=='-' ){ z++; }
8212 if( strcmp(z,"-init")==0 ){
8214 }else if( strcmp(z,"-html")==0 ){
8215 data.mode = MODE_Html;
8216 }else if( strcmp(z,"-list")==0 ){
8217 data.mode = MODE_List;
8218 }else if( strcmp(z,"-quote")==0 ){
8219 data.mode = MODE_Quote;
8220 }else if( strcmp(z,"-line")==0 ){
8221 data.mode = MODE_Line;
8222 }else if( strcmp(z,"-column")==0 ){
8223 data.mode = MODE_Column;
8224 }else if( strcmp(z,"-csv")==0 ){
8225 data.mode = MODE_Csv;
8226 memcpy(data.colSeparator,",",2);
8227 }else if( strcmp(z,"-ascii")==0 ){
8228 data.mode = MODE_Ascii;
8229 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8231 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8233 }else if( strcmp(z,"-separator")==0 ){
8234 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
8235 "%s",cmdline_option_value(argc,argv,++i));
8236 }else if( strcmp(z,"-newline")==0 ){
8237 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
8238 "%s",cmdline_option_value(argc,argv,++i));
8239 }else if( strcmp(z,"-nullvalue")==0 ){
8240 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
8241 "%s",cmdline_option_value(argc,argv,++i));
8242 }else if( strcmp(z,"-header")==0 ){
8243 data.showHeader = 1;
8244 }else if( strcmp(z,"-noheader")==0 ){
8245 data.showHeader = 0;
8246 }else if( strcmp(z,"-echo")==0 ){
8247 ShellSetFlag(&data, SHFLG_Echo);
8248 }else if( strcmp(z,"-eqp")==0 ){
8250 }else if( strcmp(z,"-eqpfull")==0 ){
8252 }else if( strcmp(z,"-stats")==0 ){
8254 }else if( strcmp(z,"-scanstats")==0 ){
8255 data.scanstatsOn = 1;
8256 }else if( strcmp(z,"-backslash")==0 ){
8257 /* Undocumented command-line option: -backslash
8258 ** Causes C-style backslash escapes to be evaluated in SQL statements
8259 ** prior to sending the SQL into SQLite. Useful for injecting
8260 ** crazy bytes in the middle of SQL statements for testing and debugging.
8262 ShellSetFlag(&data, SHFLG_Backslash);
8263 }else if( strcmp(z,"-bail")==0 ){
8265 }else if( strcmp(z,"-version")==0 ){
8266 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
8268 }else if( strcmp(z,"-interactive")==0 ){
8269 stdin_is_interactive = 1;
8270 }else if( strcmp(z,"-batch")==0 ){
8271 stdin_is_interactive = 0;
8272 }else if( strcmp(z,"-heap")==0 ){
8274 }else if( strcmp(z,"-scratch")==0 ){
8276 }else if( strcmp(z,"-pagecache")==0 ){
8278 }else if( strcmp(z,"-lookaside")==0 ){
8280 }else if( strcmp(z,"-mmap")==0 ){
8282 }else if( strcmp(z,"-vfs")==0 ){
8284 #ifdef SQLITE_ENABLE_VFSTRACE
8285 }else if( strcmp(z,"-vfstrace")==0 ){
8288 #ifdef SQLITE_ENABLE_MULTIPLEX
8289 }else if( strcmp(z,"-multiplex")==0 ){
8292 }else if( strcmp(z,"-help")==0 ){
8294 }else if( strcmp(z,"-cmd")==0 ){
8295 /* Run commands that follow -cmd first and separately from commands
8296 ** that simply appear on the command-line. This seems goofy. It would
8297 ** be better if all commands ran in the order that they appear. But
8298 ** we retain the goofy behavior for historical compatibility. */
8299 if( i==argc-1 ) break;
8300 z = cmdline_option_value(argc,argv,++i);
8302 rc = do_meta_command(z, &data);
8303 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
8306 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
8308 utf8_printf(stderr,"Error: %s\n", zErrMsg);
8309 if( bail_on_error ) return rc!=0 ? rc : 1;
8311 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
8312 if( bail_on_error ) return rc;
8316 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
8317 raw_printf(stderr,"Use -help for a list of options.\n");
8320 data.cMode = data.mode;
8324 /* Run all arguments that do not begin with '-' as if they were separate
8325 ** command-line inputs, except for the argToSkip argument which contains
8326 ** the database filename.
8328 for(i=0; i<nCmd; i++){
8329 if( azCmd[i][0]=='.' ){
8330 rc = do_meta_command(azCmd[i], &data);
8331 if( rc ) return rc==2 ? 0 : rc;
8334 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
8336 utf8_printf(stderr,"Error: %s\n", zErrMsg);
8337 return rc!=0 ? rc : 1;
8339 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
8346 /* Run commands received from standard input
8348 if( stdin_is_interactive ){
8353 "SQLite version %s %.19s\n" /*extra-version-info*/
8354 "Enter \".help\" for usage hints.\n",
8355 sqlite3_libversion(), sqlite3_sourceid()
8357 if( warnInmemoryDb ){
8358 printf("Connected to a ");
8359 printBold("transient in-memory database");
8360 printf(".\nUse \".open FILENAME\" to reopen on a "
8361 "persistent database.\n");
8363 zHome = find_home_dir(0);
8365 nHistory = strlen30(zHome) + 20;
8366 if( (zHistory = malloc(nHistory))!=0 ){
8367 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
8370 if( zHistory ){ shell_read_history(zHistory); }
8371 #if HAVE_READLINE || HAVE_EDITLINE
8372 rl_attempted_completion_function = readline_completion;
8373 #elif HAVE_LINENOISE
8374 linenoiseSetCompletionCallback(linenoise_completion);
8376 rc = process_input(&data, 0);
8378 shell_stifle_history(2000);
8379 shell_write_history(zHistory);
8383 rc = process_input(&data, stdin);
8386 set_table_name(&data, 0);
8388 session_close_all(&data);
8389 sqlite3_close(data.db);
8391 sqlite3_free(data.zFreeOnClose);
8393 #if !SQLITE_SHELL_IS_UTF8
8394 for(i=0; i<argc; i++) sqlite3_free(argv[i]);