Merge sqlite-release(3.42.0) into prerelease-integration
[sqlcipher.git] / src / shell.c.in
blobfe80749ae40c3f7bce9d6ba518ca3eb6fc8f3801
1 /*
2 ** 2001 September 15
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
18 #endif
19 typedef unsigned int u32;
20 typedef unsigned short int u16;
23 ** Optionally #include a user-defined header, whereby compilation options
24 ** may be set prior to where they take effect, but after platform setup.
25 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
26 ** file. Note that this macro has a like effect on sqlite3.c compilation.
28 # define SHELL_STRINGIFY_(f) #f
29 # define SHELL_STRINGIFY(f) SHELL_STRINGIFY_(f)
30 #ifdef SQLITE_CUSTOM_INCLUDE
31 # include SHELL_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
32 #endif
35 ** Determine if we are dealing with WinRT, which provides only a subset of
36 ** the full Win32 API.
38 #if !defined(SQLITE_OS_WINRT)
39 # define SQLITE_OS_WINRT 0
40 #endif
43 ** If SQLITE_SHELL_FIDDLE is defined then the shell is modified
44 ** somewhat for use as a WASM module in a web browser. This flag
45 ** should only be used when building the "fiddle" web application, as
46 ** the browser-mode build has much different user input requirements
47 ** and this build mode rewires the user input subsystem to account for
48 ** that.
52 ** Warning pragmas copied from msvc.h in the core.
54 #if defined(_MSC_VER)
55 #pragma warning(disable : 4054)
56 #pragma warning(disable : 4055)
57 #pragma warning(disable : 4100)
58 #pragma warning(disable : 4127)
59 #pragma warning(disable : 4130)
60 #pragma warning(disable : 4152)
61 #pragma warning(disable : 4189)
62 #pragma warning(disable : 4206)
63 #pragma warning(disable : 4210)
64 #pragma warning(disable : 4232)
65 #pragma warning(disable : 4244)
66 #pragma warning(disable : 4305)
67 #pragma warning(disable : 4306)
68 #pragma warning(disable : 4702)
69 #pragma warning(disable : 4706)
70 #endif /* defined(_MSC_VER) */
73 ** No support for loadable extensions in VxWorks.
75 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
76 # define SQLITE_OMIT_LOAD_EXTENSION 1
77 #endif
80 ** Enable large-file support for fopen() and friends on unix.
82 #ifndef SQLITE_DISABLE_LFS
83 # define _LARGE_FILE 1
84 # ifndef _FILE_OFFSET_BITS
85 # define _FILE_OFFSET_BITS 64
86 # endif
87 # define _LARGEFILE_SOURCE 1
88 #endif
90 #if defined(SQLITE_SHELL_FIDDLE) && !defined(_POSIX_SOURCE)
92 ** emcc requires _POSIX_SOURCE (or one of several similar defines)
93 ** to expose strdup().
95 # define _POSIX_SOURCE
96 #endif
98 #include <stdlib.h>
99 #include <string.h>
100 #include <stdio.h>
101 #include <assert.h>
102 #include <math.h>
103 #include "sqlite3.h"
104 typedef sqlite3_int64 i64;
105 typedef sqlite3_uint64 u64;
106 typedef unsigned char u8;
107 #if SQLITE_USER_AUTHENTICATION
108 # include "sqlite3userauth.h"
109 #endif
110 #include <ctype.h>
111 #include <stdarg.h>
113 #if !defined(_WIN32) && !defined(WIN32)
114 # include <signal.h>
115 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
116 # include <pwd.h>
117 # endif
118 #endif
119 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
120 # include <unistd.h>
121 # include <dirent.h>
122 # define GETPID getpid
123 # if defined(__MINGW32__)
124 # define DIRENT dirent
125 # ifndef S_ISLNK
126 # define S_ISLNK(mode) (0)
127 # endif
128 # endif
129 #else
130 # define GETPID (int)GetCurrentProcessId
131 #endif
132 #include <sys/types.h>
133 #include <sys/stat.h>
135 #if HAVE_READLINE
136 # include <readline/readline.h>
137 # include <readline/history.h>
138 #endif
140 #if HAVE_EDITLINE
141 # include <editline/readline.h>
142 #endif
144 #if HAVE_EDITLINE || HAVE_READLINE
146 # define shell_add_history(X) add_history(X)
147 # define shell_read_history(X) read_history(X)
148 # define shell_write_history(X) write_history(X)
149 # define shell_stifle_history(X) stifle_history(X)
150 # define shell_readline(X) readline(X)
152 #elif HAVE_LINENOISE
154 # include "linenoise.h"
155 # define shell_add_history(X) linenoiseHistoryAdd(X)
156 # define shell_read_history(X) linenoiseHistoryLoad(X)
157 # define shell_write_history(X) linenoiseHistorySave(X)
158 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
159 # define shell_readline(X) linenoise(X)
161 #else
163 # define shell_read_history(X)
164 # define shell_write_history(X)
165 # define shell_stifle_history(X)
167 # define SHELL_USE_LOCAL_GETLINE 1
168 #endif
170 #ifndef deliberate_fall_through
171 /* Quiet some compilers about some of our intentional code. */
172 # if defined(GCC_VERSION) && GCC_VERSION>=7000000
173 # define deliberate_fall_through __attribute__((fallthrough));
174 # else
175 # define deliberate_fall_through
176 # endif
177 #endif
179 #if defined(_WIN32) || defined(WIN32)
180 # if SQLITE_OS_WINRT
181 # define SQLITE_OMIT_POPEN 1
182 # else
183 # include <io.h>
184 # include <fcntl.h>
185 # define isatty(h) _isatty(h)
186 # ifndef access
187 # define access(f,m) _access((f),(m))
188 # endif
189 # ifndef unlink
190 # define unlink _unlink
191 # endif
192 # ifndef strdup
193 # define strdup _strdup
194 # endif
195 # undef popen
196 # define popen _popen
197 # undef pclose
198 # define pclose _pclose
199 # endif
200 #else
201 /* Make sure isatty() has a prototype. */
202 extern int isatty(int);
204 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
205 /* popen and pclose are not C89 functions and so are
206 ** sometimes omitted from the <stdio.h> header */
207 extern FILE *popen(const char*,const char*);
208 extern int pclose(FILE*);
209 # else
210 # define SQLITE_OMIT_POPEN 1
211 # endif
212 #endif
214 #if defined(_WIN32_WCE)
215 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
216 * thus we always assume that we have a console. That can be
217 * overridden with the -batch command line option.
219 #define isatty(x) 1
220 #endif
222 /* ctype macros that work with signed characters */
223 #define IsSpace(X) isspace((unsigned char)X)
224 #define IsDigit(X) isdigit((unsigned char)X)
225 #define ToLower(X) (char)tolower((unsigned char)X)
227 #if defined(_WIN32) || defined(WIN32)
228 #if SQLITE_OS_WINRT
229 #include <intrin.h>
230 #endif
231 #define WIN32_LEAN_AND_MEAN
232 #include <windows.h>
234 /* string conversion routines only needed on Win32 */
235 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
236 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
237 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
238 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
239 #endif
241 /* On Windows, we normally run with output mode of TEXT so that \n characters
242 ** are automatically translated into \r\n. However, this behavior needs
243 ** to be disabled in some cases (ex: when generating CSV output and when
244 ** rendering quoted strings that contain \n characters). The following
245 ** routines take care of that.
247 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
248 static void setBinaryMode(FILE *file, int isOutput){
249 if( isOutput ) fflush(file);
250 _setmode(_fileno(file), _O_BINARY);
252 static void setTextMode(FILE *file, int isOutput){
253 if( isOutput ) fflush(file);
254 _setmode(_fileno(file), _O_TEXT);
256 #else
257 # define setBinaryMode(X,Y)
258 # define setTextMode(X,Y)
259 #endif
261 /* True if the timer is enabled */
262 static int enableTimer = 0;
264 /* A version of strcmp() that works with NULL values */
265 static int cli_strcmp(const char *a, const char *b){
266 if( a==0 ) a = "";
267 if( b==0 ) b = "";
268 return strcmp(a,b);
270 static int cli_strncmp(const char *a, const char *b, size_t n){
271 if( a==0 ) a = "";
272 if( b==0 ) b = "";
273 return strncmp(a,b,n);
276 /* Return the current wall-clock time */
277 static sqlite3_int64 timeOfDay(void){
278 static sqlite3_vfs *clockVfs = 0;
279 sqlite3_int64 t;
280 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
281 if( clockVfs==0 ) return 0; /* Never actually happens */
282 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
283 clockVfs->xCurrentTimeInt64(clockVfs, &t);
284 }else{
285 double r;
286 clockVfs->xCurrentTime(clockVfs, &r);
287 t = (sqlite3_int64)(r*86400000.0);
289 return t;
292 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
293 #include <sys/time.h>
294 #include <sys/resource.h>
296 /* VxWorks does not support getrusage() as far as we can determine */
297 #if defined(_WRS_KERNEL) || defined(__RTP__)
298 struct rusage {
299 struct timeval ru_utime; /* user CPU time used */
300 struct timeval ru_stime; /* system CPU time used */
302 #define getrusage(A,B) memset(B,0,sizeof(*B))
303 #endif
305 /* Saved resource information for the beginning of an operation */
306 static struct rusage sBegin; /* CPU time at start */
307 static sqlite3_int64 iBegin; /* Wall-clock time at start */
310 ** Begin timing an operation
312 static void beginTimer(void){
313 if( enableTimer ){
314 getrusage(RUSAGE_SELF, &sBegin);
315 iBegin = timeOfDay();
319 /* Return the difference of two time_structs in seconds */
320 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
321 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
322 (double)(pEnd->tv_sec - pStart->tv_sec);
326 ** Print the timing results.
328 static void endTimer(void){
329 if( enableTimer ){
330 sqlite3_int64 iEnd = timeOfDay();
331 struct rusage sEnd;
332 getrusage(RUSAGE_SELF, &sEnd);
333 printf("Run Time: real %.3f user %f sys %f\n",
334 (iEnd - iBegin)*0.001,
335 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
336 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
340 #define BEGIN_TIMER beginTimer()
341 #define END_TIMER endTimer()
342 #define HAS_TIMER 1
344 #elif (defined(_WIN32) || defined(WIN32))
346 /* Saved resource information for the beginning of an operation */
347 static HANDLE hProcess;
348 static FILETIME ftKernelBegin;
349 static FILETIME ftUserBegin;
350 static sqlite3_int64 ftWallBegin;
351 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
352 LPFILETIME, LPFILETIME);
353 static GETPROCTIMES getProcessTimesAddr = NULL;
356 ** Check to see if we have timer support. Return 1 if necessary
357 ** support found (or found previously).
359 static int hasTimer(void){
360 if( getProcessTimesAddr ){
361 return 1;
362 } else {
363 #if !SQLITE_OS_WINRT
364 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
365 ** versions. See if the version we are running on has it, and if it
366 ** does, save off a pointer to it and the current process handle.
368 hProcess = GetCurrentProcess();
369 if( hProcess ){
370 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
371 if( NULL != hinstLib ){
372 getProcessTimesAddr =
373 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
374 if( NULL != getProcessTimesAddr ){
375 return 1;
377 FreeLibrary(hinstLib);
380 #endif
382 return 0;
386 ** Begin timing an operation
388 static void beginTimer(void){
389 if( enableTimer && getProcessTimesAddr ){
390 FILETIME ftCreation, ftExit;
391 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
392 &ftKernelBegin,&ftUserBegin);
393 ftWallBegin = timeOfDay();
397 /* Return the difference of two FILETIME structs in seconds */
398 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
399 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
400 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
401 return (double) ((i64End - i64Start) / 10000000.0);
405 ** Print the timing results.
407 static void endTimer(void){
408 if( enableTimer && getProcessTimesAddr){
409 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
410 sqlite3_int64 ftWallEnd = timeOfDay();
411 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
412 printf("Run Time: real %.3f user %f sys %f\n",
413 (ftWallEnd - ftWallBegin)*0.001,
414 timeDiff(&ftUserBegin, &ftUserEnd),
415 timeDiff(&ftKernelBegin, &ftKernelEnd));
419 #define BEGIN_TIMER beginTimer()
420 #define END_TIMER endTimer()
421 #define HAS_TIMER hasTimer()
423 #else
424 #define BEGIN_TIMER
425 #define END_TIMER
426 #define HAS_TIMER 0
427 #endif
430 ** Used to prevent warnings about unused parameters
432 #define UNUSED_PARAMETER(x) (void)(x)
435 ** Number of elements in an array
437 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
440 ** If the following flag is set, then command execution stops
441 ** at an error if we are not interactive.
443 static int bail_on_error = 0;
446 ** Treat stdin as an interactive input if the following variable
447 ** is true. Otherwise, assume stdin is connected to a file or pipe.
449 static int stdin_is_interactive = 1;
451 #if (defined(_WIN32) || defined(WIN32)) && SHELL_USE_LOCAL_GETLINE \
452 && !defined(SHELL_OMIT_WIN_UTF8)
453 # define SHELL_WIN_UTF8_OPT 1
454 #else
455 # define SHELL_WIN_UTF8_OPT 0
456 #endif
458 #if SHELL_WIN_UTF8_OPT
460 ** Setup console for UTF-8 input/output when following variable true.
462 static int console_utf8 = 0;
463 #endif
466 ** On Windows systems we have to know if standard output is a console
467 ** in order to translate UTF-8 into MBCS. The following variable is
468 ** true if translation is required.
470 static int stdout_is_console = 1;
473 ** The following is the open SQLite database. We make a pointer
474 ** to this database a static variable so that it can be accessed
475 ** by the SIGINT handler to interrupt database processing.
477 static sqlite3 *globalDb = 0;
480 ** True if an interrupt (Control-C) has been received.
482 static volatile int seenInterrupt = 0;
485 ** This is the name of our program. It is set in main(), used
486 ** in a number of other places, mostly for error messages.
488 static char *Argv0;
491 ** Prompt strings. Initialized in main. Settable with
492 ** .prompt main continue
494 #define PROMPT_LEN_MAX 20
495 /* First line prompt. default: "sqlite> " */
496 static char mainPrompt[PROMPT_LEN_MAX];
497 /* Continuation prompt. default: " ...> " */
498 static char continuePrompt[PROMPT_LEN_MAX];
500 /* This is variant of the standard-library strncpy() routine with the
501 ** one change that the destination string is always zero-terminated, even
502 ** if there is no zero-terminator in the first n-1 characters of the source
503 ** string.
505 static char *shell_strncpy(char *dest, const char *src, size_t n){
506 size_t i;
507 for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
508 dest[i] = 0;
509 return dest;
513 ** Optionally disable dynamic continuation prompt.
514 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
515 ** or open parentheses level if non-zero, or continuation prompt as set.
516 ** This facility interacts with the scanner and process_input() where the
517 ** below 5 macros are used.
519 #ifdef SQLITE_OMIT_DYNAPROMPT
520 # define CONTINUATION_PROMPT continuePrompt
521 # define CONTINUE_PROMPT_RESET
522 # define CONTINUE_PROMPT_AWAITS(p,s)
523 # define CONTINUE_PROMPT_AWAITC(p,c)
524 # define CONTINUE_PAREN_INCR(p,n)
525 # define CONTINUE_PROMPT_PSTATE 0
526 typedef void *t_NoDynaPrompt;
527 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
528 #else
529 # define CONTINUATION_PROMPT dynamicContinuePrompt()
530 # define CONTINUE_PROMPT_RESET \
531 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
532 # define CONTINUE_PROMPT_AWAITS(p,s) \
533 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
534 # define CONTINUE_PROMPT_AWAITC(p,c) \
535 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
536 # define CONTINUE_PAREN_INCR(p,n) \
537 if(p && stdin_is_interactive) (trackParenLevel(p,n))
538 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
539 typedef struct DynaPrompt *t_DynaPromptRef;
540 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
542 static struct DynaPrompt {
543 char dynamicPrompt[PROMPT_LEN_MAX];
544 char acAwait[2];
545 int inParenLevel;
546 char *zScannerAwaits;
547 } dynPrompt = { {0}, {0}, 0, 0 };
549 /* Record parenthesis nesting level change, or force level to 0. */
550 static void trackParenLevel(struct DynaPrompt *p, int ni){
551 p->inParenLevel += ni;
552 if( ni==0 ) p->inParenLevel = 0;
553 p->zScannerAwaits = 0;
556 /* Record that a lexeme is opened, or closed with args==0. */
557 static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
558 if( s!=0 || c==0 ){
559 p->zScannerAwaits = s;
560 p->acAwait[0] = 0;
561 }else{
562 p->acAwait[0] = c;
563 p->zScannerAwaits = p->acAwait;
567 /* Upon demand, derive the continuation prompt to display. */
568 static char *dynamicContinuePrompt(void){
569 if( continuePrompt[0]==0
570 || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
571 return continuePrompt;
572 }else{
573 if( dynPrompt.zScannerAwaits ){
574 size_t ncp = strlen(continuePrompt);
575 size_t ndp = strlen(dynPrompt.zScannerAwaits);
576 if( ndp > ncp-3 ) return continuePrompt;
577 strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
578 while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
579 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
580 PROMPT_LEN_MAX-4);
581 }else{
582 if( dynPrompt.inParenLevel>9 ){
583 shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
584 }else if( dynPrompt.inParenLevel<0 ){
585 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
586 }else{
587 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
588 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
590 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3, PROMPT_LEN_MAX-4);
593 return dynPrompt.dynamicPrompt;
595 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
597 #if SHELL_WIN_UTF8_OPT
598 /* Following struct is used for -utf8 operation. */
599 static struct ConsoleState {
600 int stdinEof; /* EOF has been seen on console input */
601 int infsMode; /* Input file stream mode upon shell start */
602 UINT inCodePage; /* Input code page upon shell start */
603 UINT outCodePage; /* Output code page upon shell start */
604 HANDLE hConsoleIn; /* Console input handle */
605 DWORD consoleMode; /* Console mode upon shell start */
606 } conState = { 0, 0, 0, 0, INVALID_HANDLE_VALUE, 0 };
608 #ifndef _O_U16TEXT /* For build environments lacking this constant: */
609 # define _O_U16TEXT 0x20000
610 #endif
613 ** Prepare console, (if known to be a WIN32 console), for UTF-8
614 ** input (from either typing or suitable paste operations) and for
615 ** UTF-8 rendering. This may "fail" with a message to stderr, where
616 ** the preparation is not done and common "code page" issues occur.
618 static void console_prepare(void){
619 HANDLE hCI = GetStdHandle(STD_INPUT_HANDLE);
620 DWORD consoleMode = 0;
621 if( isatty(0) && GetFileType(hCI)==FILE_TYPE_CHAR
622 && GetConsoleMode( hCI, &consoleMode) ){
623 if( !IsValidCodePage(CP_UTF8) ){
624 fprintf(stderr, "Cannot use UTF-8 code page.\n");
625 console_utf8 = 0;
626 return;
628 conState.hConsoleIn = hCI;
629 conState.consoleMode = consoleMode;
630 conState.inCodePage = GetConsoleCP();
631 conState.outCodePage = GetConsoleOutputCP();
632 SetConsoleCP(CP_UTF8);
633 SetConsoleOutputCP(CP_UTF8);
634 consoleMode |= ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
635 SetConsoleMode(conState.hConsoleIn, consoleMode);
636 conState.infsMode = _setmode(_fileno(stdin), _O_U16TEXT);
637 console_utf8 = 1;
638 }else{
639 console_utf8 = 0;
644 ** Undo the effects of console_prepare(), if any.
646 static void SQLITE_CDECL console_restore(void){
647 if( console_utf8 && conState.inCodePage!=0
648 && conState.hConsoleIn!=INVALID_HANDLE_VALUE ){
649 _setmode(_fileno(stdin), conState.infsMode);
650 SetConsoleCP(conState.inCodePage);
651 SetConsoleOutputCP(conState.outCodePage);
652 SetConsoleMode(conState.hConsoleIn, conState.consoleMode);
653 /* Avoid multiple calls. */
654 conState.hConsoleIn = INVALID_HANDLE_VALUE;
655 conState.consoleMode = 0;
656 console_utf8 = 0;
661 ** Collect input like fgets(...) with special provisions for input
662 ** from the Windows console to get around its strange coding issues.
663 ** Defers to plain fgets() when input is not interactive or when the
664 ** startup option, -utf8, has not been provided or taken effect.
666 static char* utf8_fgets(char *buf, int ncmax, FILE *fin){
667 if( fin==0 ) fin = stdin;
668 if( fin==stdin && stdin_is_interactive && console_utf8 ){
669 # define SQLITE_IALIM 150
670 wchar_t wbuf[SQLITE_IALIM];
671 int lend = 0;
672 int noc = 0;
673 if( ncmax==0 || conState.stdinEof ) return 0;
674 buf[0] = 0;
675 while( noc<ncmax-7-1 && !lend ){
676 /* There is room for at least 2 more characters and a 0-terminator. */
677 int na = (ncmax > SQLITE_IALIM*4+1 + noc)
678 ? SQLITE_IALIM : (ncmax-1 - noc)/4;
679 # undef SQLITE_IALIM
680 DWORD nbr = 0;
681 BOOL bRC = ReadConsoleW(conState.hConsoleIn, wbuf, na, &nbr, 0);
682 if( !bRC || (noc==0 && nbr==0) ) return 0;
683 if( nbr > 0 ){
684 int nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
685 wbuf,nbr,0,0,0,0);
686 if( nmb !=0 && noc+nmb <= ncmax ){
687 int iseg = noc;
688 nmb = WideCharToMultiByte(CP_UTF8,WC_COMPOSITECHECK|WC_DEFAULTCHAR,
689 wbuf,nbr,buf+noc,nmb,0,0);
690 noc += nmb;
691 /* Fixup line-ends as coded by Windows for CR (or "Enter".)*/
692 if( noc > 0 ){
693 if( buf[noc-1]=='\n' ){
694 lend = 1;
695 if( noc > 1 && buf[noc-2]=='\r' ){
696 buf[noc-2] = '\n';
697 --noc;
701 /* Check for ^Z (anywhere in line) too. */
702 while( iseg < noc ){
703 if( buf[iseg]==0x1a ){
704 conState.stdinEof = 1;
705 noc = iseg; /* Chop ^Z and anything following. */
706 break;
708 ++iseg;
710 }else break; /* Drop apparent garbage in. (Could assert.) */
711 }else break;
713 /* If got nothing, (after ^Z chop), must be at end-of-file. */
714 if( noc == 0 ) return 0;
715 buf[noc] = 0;
716 return buf;
717 }else{
718 return fgets(buf, ncmax, fin);
722 # define fgets(b,n,f) utf8_fgets(b,n,f)
723 #endif /* SHELL_WIN_UTF8_OPT */
726 ** Render output like fprintf(). Except, if the output is going to the
727 ** console and if this is running on a Windows machine, and if the -utf8
728 ** option is unavailable or (available and inactive), translate the
729 ** output from UTF-8 into MBCS for output through 8-bit stdout stream.
730 ** (With -utf8 active, no translation is needed and must not be done.)
732 #if defined(_WIN32) || defined(WIN32)
733 void utf8_printf(FILE *out, const char *zFormat, ...){
734 va_list ap;
735 va_start(ap, zFormat);
736 if( stdout_is_console && (out==stdout || out==stderr)
737 # if SHELL_WIN_UTF8_OPT
738 && !console_utf8
739 # endif
741 char *z1 = sqlite3_vmprintf(zFormat, ap);
742 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
743 sqlite3_free(z1);
744 fputs(z2, out);
745 sqlite3_free(z2);
746 }else{
747 vfprintf(out, zFormat, ap);
749 va_end(ap);
751 #elif !defined(utf8_printf)
752 # define utf8_printf fprintf
753 #endif
756 ** Render output like fprintf(). This should not be used on anything that
757 ** includes string formatting (e.g. "%s").
759 #if !defined(raw_printf)
760 # define raw_printf fprintf
761 #endif
763 /* Indicate out-of-memory and exit. */
764 static void shell_out_of_memory(void){
765 raw_printf(stderr,"Error: out of memory\n");
766 exit(1);
769 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
770 ** out-of-memory error.
772 static void shell_check_oom(const void *p){
773 if( p==0 ) shell_out_of_memory();
777 ** Write I/O traces to the following stream.
779 #ifdef SQLITE_ENABLE_IOTRACE
780 static FILE *iotrace = 0;
781 #endif
784 ** This routine works like printf in that its first argument is a
785 ** format string and subsequent arguments are values to be substituted
786 ** in place of % fields. The result of formatting this string
787 ** is written to iotrace.
789 #ifdef SQLITE_ENABLE_IOTRACE
790 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
791 va_list ap;
792 char *z;
793 if( iotrace==0 ) return;
794 va_start(ap, zFormat);
795 z = sqlite3_vmprintf(zFormat, ap);
796 va_end(ap);
797 utf8_printf(iotrace, "%s", z);
798 sqlite3_free(z);
800 #endif
803 ** Output string zUtf to stream pOut as w characters. If w is negative,
804 ** then right-justify the text. W is the width in UTF-8 characters, not
805 ** in bytes. This is different from the %*.*s specification in printf
806 ** since with %*.*s the width is measured in bytes, not characters.
808 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
809 int i;
810 int n;
811 int aw = w<0 ? -w : w;
812 if( zUtf==0 ) zUtf = "";
813 for(i=n=0; zUtf[i]; i++){
814 if( (zUtf[i]&0xc0)!=0x80 ){
815 n++;
816 if( n==aw ){
817 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
818 break;
822 if( n>=aw ){
823 utf8_printf(pOut, "%.*s", i, zUtf);
824 }else if( w<0 ){
825 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
826 }else{
827 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
833 ** Determines if a string is a number of not.
835 static int isNumber(const char *z, int *realnum){
836 if( *z=='-' || *z=='+' ) z++;
837 if( !IsDigit(*z) ){
838 return 0;
840 z++;
841 if( realnum ) *realnum = 0;
842 while( IsDigit(*z) ){ z++; }
843 if( *z=='.' ){
844 z++;
845 if( !IsDigit(*z) ) return 0;
846 while( IsDigit(*z) ){ z++; }
847 if( realnum ) *realnum = 1;
849 if( *z=='e' || *z=='E' ){
850 z++;
851 if( *z=='+' || *z=='-' ) z++;
852 if( !IsDigit(*z) ) return 0;
853 while( IsDigit(*z) ){ z++; }
854 if( realnum ) *realnum = 1;
856 return *z==0;
860 ** Compute a string length that is limited to what can be stored in
861 ** lower 30 bits of a 32-bit signed integer.
863 static int strlen30(const char *z){
864 const char *z2 = z;
865 while( *z2 ){ z2++; }
866 return 0x3fffffff & (int)(z2 - z);
870 ** Return the length of a string in characters. Multibyte UTF8 characters
871 ** count as a single character.
873 static int strlenChar(const char *z){
874 int n = 0;
875 while( *z ){
876 if( (0xc0&*(z++))!=0x80 ) n++;
878 return n;
882 ** Return open FILE * if zFile exists, can be opened for read
883 ** and is an ordinary file or a character stream source.
884 ** Otherwise return 0.
886 static FILE * openChrSource(const char *zFile){
887 #ifdef _WIN32
888 struct _stat x = {0};
889 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
890 /* On Windows, open first, then check the stream nature. This order
891 ** is necessary because _stat() and sibs, when checking a named pipe,
892 ** effectively break the pipe as its supplier sees it. */
893 FILE *rv = fopen(zFile, "rb");
894 if( rv==0 ) return 0;
895 if( _fstat(_fileno(rv), &x) != 0
896 || !STAT_CHR_SRC(x.st_mode)){
897 fclose(rv);
898 rv = 0;
900 return rv;
901 #else
902 struct stat x = {0};
903 int rc = stat(zFile, &x);
904 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
905 if( rc!=0 ) return 0;
906 if( STAT_CHR_SRC(x.st_mode) ){
907 return fopen(zFile, "rb");
908 }else{
909 return 0;
911 #endif
912 #undef STAT_CHR_SRC
916 ** This routine reads a line of text from FILE in, stores
917 ** the text in memory obtained from malloc() and returns a pointer
918 ** to the text. NULL is returned at end of file, or if malloc()
919 ** fails.
921 ** If zLine is not NULL then it is a malloced buffer returned from
922 ** a previous call to this routine that may be reused.
924 static char *local_getline(char *zLine, FILE *in){
925 int nLine = zLine==0 ? 0 : 100;
926 int n = 0;
928 while( 1 ){
929 if( n+100>nLine ){
930 nLine = nLine*2 + 100;
931 zLine = realloc(zLine, nLine);
932 shell_check_oom(zLine);
934 if( fgets(&zLine[n], nLine - n, in)==0 ){
935 if( n==0 ){
936 free(zLine);
937 return 0;
939 zLine[n] = 0;
940 break;
942 while( zLine[n] ) n++;
943 if( n>0 && zLine[n-1]=='\n' ){
944 n--;
945 if( n>0 && zLine[n-1]=='\r' ) n--;
946 zLine[n] = 0;
947 break;
950 #if defined(_WIN32) || defined(WIN32)
951 /* For interactive input on Windows systems, without -utf8,
952 ** translate the multi-byte characterset characters into UTF-8.
953 ** This is the translation that predates the -utf8 option. */
954 if( stdin_is_interactive && in==stdin
955 # if SHELL_WIN_UTF8_OPT
956 && !console_utf8
957 # endif /* SHELL_WIN_UTF8_OPT */
959 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
960 if( zTrans ){
961 i64 nTrans = strlen(zTrans)+1;
962 if( nTrans>nLine ){
963 zLine = realloc(zLine, nTrans);
964 shell_check_oom(zLine);
966 memcpy(zLine, zTrans, nTrans);
967 sqlite3_free(zTrans);
970 #endif /* defined(_WIN32) || defined(WIN32) */
971 return zLine;
975 ** Retrieve a single line of input text.
977 ** If in==0 then read from standard input and prompt before each line.
978 ** If isContinuation is true, then a continuation prompt is appropriate.
979 ** If isContinuation is zero, then the main prompt should be used.
981 ** If zPrior is not NULL then it is a buffer from a prior call to this
982 ** routine that can be reused.
984 ** The result is stored in space obtained from malloc() and must either
985 ** be freed by the caller or else passed back into this routine via the
986 ** zPrior argument for reuse.
988 #ifndef SQLITE_SHELL_FIDDLE
989 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
990 char *zPrompt;
991 char *zResult;
992 if( in!=0 ){
993 zResult = local_getline(zPrior, in);
994 }else{
995 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
996 #if SHELL_USE_LOCAL_GETLINE
997 printf("%s", zPrompt);
998 fflush(stdout);
1000 zResult = local_getline(zPrior, stdin);
1001 zPrior = 0;
1002 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1003 if( zResult==0 ) sqlite3_sleep(50);
1004 }while( zResult==0 && seenInterrupt>0 );
1005 #else
1006 free(zPrior);
1007 zResult = shell_readline(zPrompt);
1008 while( zResult==0 ){
1009 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1010 sqlite3_sleep(50);
1011 if( seenInterrupt==0 ) break;
1012 zResult = shell_readline("");
1014 /* BEGIN SQLCIPHER */
1015 #ifdef SQLITE_HAS_CODEC
1016 /* Simplistic filtering of input lines to prevent PRAGKA key and
1017 PRAGMA rekey statements from being stored in readline history.
1018 Note that this will only prevent single line statements, but that
1019 will be sufficient for common cases. */
1020 if(zResult && *zResult && (
1021 sqlite3_strlike("%pragma%key%=%", zResult, 0)==0 ||
1022 sqlite3_strlike("%attach%database%as%key%", zResult, 0)==0
1024 ) return zResult;
1025 #endif
1026 /* END SQLCIPHER */
1027 if( zResult && *zResult ) shell_add_history(zResult);
1028 #endif
1030 return zResult;
1032 #endif /* !SQLITE_SHELL_FIDDLE */
1035 ** Return the value of a hexadecimal digit. Return -1 if the input
1036 ** is not a hex digit.
1038 static int hexDigitValue(char c){
1039 if( c>='0' && c<='9' ) return c - '0';
1040 if( c>='a' && c<='f' ) return c - 'a' + 10;
1041 if( c>='A' && c<='F' ) return c - 'A' + 10;
1042 return -1;
1046 ** Interpret zArg as an integer value, possibly with suffixes.
1048 static sqlite3_int64 integerValue(const char *zArg){
1049 sqlite3_int64 v = 0;
1050 static const struct { char *zSuffix; int iMult; } aMult[] = {
1051 { "KiB", 1024 },
1052 { "MiB", 1024*1024 },
1053 { "GiB", 1024*1024*1024 },
1054 { "KB", 1000 },
1055 { "MB", 1000000 },
1056 { "GB", 1000000000 },
1057 { "K", 1000 },
1058 { "M", 1000000 },
1059 { "G", 1000000000 },
1061 int i;
1062 int isNeg = 0;
1063 if( zArg[0]=='-' ){
1064 isNeg = 1;
1065 zArg++;
1066 }else if( zArg[0]=='+' ){
1067 zArg++;
1069 if( zArg[0]=='0' && zArg[1]=='x' ){
1070 int x;
1071 zArg += 2;
1072 while( (x = hexDigitValue(zArg[0]))>=0 ){
1073 v = (v<<4) + x;
1074 zArg++;
1076 }else{
1077 while( IsDigit(zArg[0]) ){
1078 v = v*10 + zArg[0] - '0';
1079 zArg++;
1082 for(i=0; i<ArraySize(aMult); i++){
1083 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1084 v *= aMult[i].iMult;
1085 break;
1088 return isNeg? -v : v;
1092 ** A variable length string to which one can append text.
1094 typedef struct ShellText ShellText;
1095 struct ShellText {
1096 char *z;
1097 int n;
1098 int nAlloc;
1102 ** Initialize and destroy a ShellText object
1104 static void initText(ShellText *p){
1105 memset(p, 0, sizeof(*p));
1107 static void freeText(ShellText *p){
1108 free(p->z);
1109 initText(p);
1112 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1113 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1114 ** added to zIn, and the result returned in memory obtained from malloc().
1115 ** zIn, if it was not NULL, is freed.
1117 ** If the third argument, quote, is not '\0', then it is used as a
1118 ** quote character for zAppend.
1120 static void appendText(ShellText *p, const char *zAppend, char quote){
1121 i64 len;
1122 i64 i;
1123 i64 nAppend = strlen30(zAppend);
1125 len = nAppend+p->n+1;
1126 if( quote ){
1127 len += 2;
1128 for(i=0; i<nAppend; i++){
1129 if( zAppend[i]==quote ) len++;
1133 if( p->z==0 || p->n+len>=p->nAlloc ){
1134 p->nAlloc = p->nAlloc*2 + len + 20;
1135 p->z = realloc(p->z, p->nAlloc);
1136 shell_check_oom(p->z);
1139 if( quote ){
1140 char *zCsr = p->z+p->n;
1141 *zCsr++ = quote;
1142 for(i=0; i<nAppend; i++){
1143 *zCsr++ = zAppend[i];
1144 if( zAppend[i]==quote ) *zCsr++ = quote;
1146 *zCsr++ = quote;
1147 p->n = (int)(zCsr - p->z);
1148 *zCsr = '\0';
1149 }else{
1150 memcpy(p->z+p->n, zAppend, nAppend);
1151 p->n += nAppend;
1152 p->z[p->n] = '\0';
1157 ** Attempt to determine if identifier zName needs to be quoted, either
1158 ** because it contains non-alphanumeric characters, or because it is an
1159 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1160 ** that quoting is required.
1162 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1164 static char quoteChar(const char *zName){
1165 int i;
1166 if( zName==0 ) return '"';
1167 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
1168 for(i=0; zName[i]; i++){
1169 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
1171 return sqlite3_keyword_check(zName, i) ? '"' : 0;
1175 ** Construct a fake object name and column list to describe the structure
1176 ** of the view, virtual table, or table valued function zSchema.zName.
1178 static char *shellFakeSchema(
1179 sqlite3 *db, /* The database connection containing the vtab */
1180 const char *zSchema, /* Schema of the database holding the vtab */
1181 const char *zName /* The name of the virtual table */
1183 sqlite3_stmt *pStmt = 0;
1184 char *zSql;
1185 ShellText s;
1186 char cQuote;
1187 char *zDiv = "(";
1188 int nRow = 0;
1190 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1191 zSchema ? zSchema : "main", zName);
1192 shell_check_oom(zSql);
1193 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1194 sqlite3_free(zSql);
1195 initText(&s);
1196 if( zSchema ){
1197 cQuote = quoteChar(zSchema);
1198 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
1199 appendText(&s, zSchema, cQuote);
1200 appendText(&s, ".", 0);
1202 cQuote = quoteChar(zName);
1203 appendText(&s, zName, cQuote);
1204 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1205 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
1206 nRow++;
1207 appendText(&s, zDiv, 0);
1208 zDiv = ",";
1209 if( zCol==0 ) zCol = "";
1210 cQuote = quoteChar(zCol);
1211 appendText(&s, zCol, cQuote);
1213 appendText(&s, ")", 0);
1214 sqlite3_finalize(pStmt);
1215 if( nRow==0 ){
1216 freeText(&s);
1217 s.z = 0;
1219 return s.z;
1223 ** SQL function: shell_module_schema(X)
1225 ** Return a fake schema for the table-valued function or eponymous virtual
1226 ** table X.
1228 static void shellModuleSchema(
1229 sqlite3_context *pCtx,
1230 int nVal,
1231 sqlite3_value **apVal
1233 const char *zName;
1234 char *zFake;
1235 UNUSED_PARAMETER(nVal);
1236 zName = (const char*)sqlite3_value_text(apVal[0]);
1237 zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
1238 if( zFake ){
1239 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
1240 -1, sqlite3_free);
1241 free(zFake);
1246 ** SQL function: shell_add_schema(S,X)
1248 ** Add the schema name X to the CREATE statement in S and return the result.
1249 ** Examples:
1251 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1253 ** Also works on
1255 ** CREATE INDEX
1256 ** CREATE UNIQUE INDEX
1257 ** CREATE VIEW
1258 ** CREATE TRIGGER
1259 ** CREATE VIRTUAL TABLE
1261 ** This UDF is used by the .schema command to insert the schema name of
1262 ** attached databases into the middle of the sqlite_schema.sql field.
1264 static void shellAddSchemaName(
1265 sqlite3_context *pCtx,
1266 int nVal,
1267 sqlite3_value **apVal
1269 static const char *aPrefix[] = {
1270 "TABLE",
1271 "INDEX",
1272 "UNIQUE INDEX",
1273 "VIEW",
1274 "TRIGGER",
1275 "VIRTUAL TABLE"
1277 int i = 0;
1278 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1279 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1280 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1281 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1282 UNUSED_PARAMETER(nVal);
1283 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
1284 for(i=0; i<ArraySize(aPrefix); i++){
1285 int n = strlen30(aPrefix[i]);
1286 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1287 char *z = 0;
1288 char *zFake = 0;
1289 if( zSchema ){
1290 char cQuote = quoteChar(zSchema);
1291 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1292 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1293 }else{
1294 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1297 if( zName
1298 && aPrefix[i][0]=='V'
1299 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1301 if( z==0 ){
1302 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1303 }else{
1304 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1306 free(zFake);
1308 if( z ){
1309 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1310 return;
1315 sqlite3_result_value(pCtx, apVal[0]);
1319 ** The source code for several run-time loadable extensions is inserted
1320 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1321 ** code, we need to override some macros to make the included program code
1322 ** work here in the middle of this regular program.
1324 #define SQLITE_EXTENSION_INIT1
1325 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1327 #if defined(_WIN32) && defined(_MSC_VER)
1328 INCLUDE test_windirent.h
1329 INCLUDE test_windirent.c
1330 #define dirent DIRENT
1331 #endif
1332 INCLUDE ../ext/misc/memtrace.c
1333 INCLUDE ../ext/misc/shathree.c
1334 INCLUDE ../ext/misc/uint.c
1335 INCLUDE ../ext/misc/decimal.c
1336 #undef sqlite3_base_init
1337 #define sqlite3_base_init sqlite3_base64_init
1338 INCLUDE ../ext/misc/base64.c
1339 #undef sqlite3_base_init
1340 #define sqlite3_base_init sqlite3_base85_init
1341 #define OMIT_BASE85_CHECKER
1342 INCLUDE ../ext/misc/base85.c
1343 INCLUDE ../ext/misc/ieee754.c
1344 INCLUDE ../ext/misc/series.c
1345 INCLUDE ../ext/misc/regexp.c
1346 #ifndef SQLITE_SHELL_FIDDLE
1347 INCLUDE ../ext/misc/fileio.c
1348 INCLUDE ../ext/misc/completion.c
1349 INCLUDE ../ext/misc/appendvfs.c
1350 #endif
1351 #ifdef SQLITE_HAVE_ZLIB
1352 INCLUDE ../ext/misc/zipfile.c
1353 INCLUDE ../ext/misc/sqlar.c
1354 #endif
1355 INCLUDE ../ext/expert/sqlite3expert.h
1356 INCLUDE ../ext/expert/sqlite3expert.c
1358 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1359 #define SQLITE_SHELL_HAVE_RECOVER 1
1360 #else
1361 #define SQLITE_SHELL_HAVE_RECOVER 0
1362 #endif
1363 #if SQLITE_SHELL_HAVE_RECOVER
1364 INCLUDE ../ext/recover/sqlite3recover.h
1365 # ifndef SQLITE_HAVE_SQLITE3R
1366 INCLUDE ../ext/recover/dbdata.c
1367 INCLUDE ../ext/recover/sqlite3recover.c
1368 # endif /* SQLITE_HAVE_SQLITE3R */
1369 #endif
1370 #ifdef SQLITE_SHELL_EXTSRC
1371 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1372 #endif
1374 #if defined(SQLITE_ENABLE_SESSION)
1376 ** State information for a single open session
1378 typedef struct OpenSession OpenSession;
1379 struct OpenSession {
1380 char *zName; /* Symbolic name for this session */
1381 int nFilter; /* Number of xFilter rejection GLOB patterns */
1382 char **azFilter; /* Array of xFilter rejection GLOB patterns */
1383 sqlite3_session *p; /* The open session */
1385 #endif
1387 typedef struct ExpertInfo ExpertInfo;
1388 struct ExpertInfo {
1389 sqlite3expert *pExpert;
1390 int bVerbose;
1393 /* A single line in the EQP output */
1394 typedef struct EQPGraphRow EQPGraphRow;
1395 struct EQPGraphRow {
1396 int iEqpId; /* ID for this row */
1397 int iParentId; /* ID of the parent row */
1398 EQPGraphRow *pNext; /* Next row in sequence */
1399 char zText[1]; /* Text to display for this row */
1402 /* All EQP output is collected into an instance of the following */
1403 typedef struct EQPGraph EQPGraph;
1404 struct EQPGraph {
1405 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
1406 EQPGraphRow *pLast; /* Last element of the pRow list */
1407 char zPrefix[100]; /* Graph prefix */
1410 /* Parameters affecting columnar mode result display (defaulting together) */
1411 typedef struct ColModeOpts {
1412 int iWrap; /* In columnar modes, wrap lines reaching this limit */
1413 u8 bQuote; /* Quote results for .mode box and table */
1414 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */
1415 } ColModeOpts;
1416 #define ColModeOpts_default { 60, 0, 0 }
1417 #define ColModeOpts_default_qbox { 60, 1, 0 }
1420 ** State information about the database connection is contained in an
1421 ** instance of the following structure.
1423 typedef struct ShellState ShellState;
1424 struct ShellState {
1425 sqlite3 *db; /* The database */
1426 u8 autoExplain; /* Automatically turn on .explain mode */
1427 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1428 u8 autoEQPtest; /* autoEQP is in test mode */
1429 u8 autoEQPtrace; /* autoEQP is in trace mode */
1430 u8 scanstatsOn; /* True to display scan stats before each finalize */
1431 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1432 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
1433 u8 nEqpLevel; /* Depth of the EQP output graph */
1434 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
1435 u8 bSafeMode; /* True to prohibit unsafe operations */
1436 u8 bSafeModePersist; /* The long-term value of bSafeMode */
1437 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
1438 unsigned statsOn; /* True to display memory stats before each finalize */
1439 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
1440 int inputNesting; /* Track nesting level of .read and other redirects */
1441 int outCount; /* Revert to stdout when reaching zero */
1442 int cnt; /* Number of records displayed so far */
1443 int lineno; /* Line number of last line read from in */
1444 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
1445 FILE *in; /* Read commands from this stream */
1446 FILE *out; /* Write results here */
1447 FILE *traceOut; /* Output for sqlite3_trace() */
1448 int nErr; /* Number of errors seen */
1449 int mode; /* An output mode setting */
1450 int modePrior; /* Saved mode */
1451 int cMode; /* temporary output mode for the current query */
1452 int normalMode; /* Output mode before ".explain on" */
1453 int writableSchema; /* True if PRAGMA writable_schema=ON */
1454 int showHeader; /* True to show column names in List or Column mode */
1455 int nCheck; /* Number of ".check" commands run */
1456 unsigned nProgress; /* Number of progress callbacks encountered */
1457 unsigned mxProgress; /* Maximum progress callbacks before failing */
1458 unsigned flgProgress; /* Flags for the progress callback */
1459 unsigned shellFlgs; /* Various flags */
1460 unsigned priorShFlgs; /* Saved copy of flags */
1461 sqlite3_int64 szMax; /* --maxsize argument to .open */
1462 char *zDestTable; /* Name of destination table when MODE_Insert */
1463 char *zTempFile; /* Temporary file that might need deleting */
1464 char zTestcase[30]; /* Name of current test case */
1465 char colSeparator[20]; /* Column separator character for several modes */
1466 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1467 char colSepPrior[20]; /* Saved column separator */
1468 char rowSepPrior[20]; /* Saved row separator */
1469 int *colWidth; /* Requested width of each column in columnar modes */
1470 int *actualWidth; /* Actual width of each column */
1471 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
1472 char nullValue[20]; /* The text to print when a NULL comes back from
1473 ** the database */
1474 char outfile[FILENAME_MAX]; /* Filename for *out */
1475 sqlite3_stmt *pStmt; /* Current statement if any. */
1476 FILE *pLog; /* Write log output here */
1477 struct AuxDb { /* Storage space for auxiliary database connections */
1478 sqlite3 *db; /* Connection pointer */
1479 const char *zDbFilename; /* Filename used to open the connection */
1480 char *zFreeOnClose; /* Free this memory allocation on close */
1481 #if defined(SQLITE_ENABLE_SESSION)
1482 int nSession; /* Number of active sessions */
1483 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
1484 #endif
1485 } aAuxDb[5], /* Array of all database connections */
1486 *pAuxDb; /* Currently active database connection */
1487 int *aiIndent; /* Array of indents used in MODE_Explain */
1488 int nIndent; /* Size of array aiIndent[] */
1489 int iIndent; /* Index of current op in aiIndent[] */
1490 char *zNonce; /* Nonce for temporary safe-mode excapes */
1491 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
1492 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
1493 #ifdef SQLITE_SHELL_FIDDLE
1494 struct {
1495 const char * zInput; /* Input string from wasm/JS proxy */
1496 const char * zPos; /* Cursor pos into zInput */
1497 const char * zDefaultDbName; /* Default name for db file */
1498 } wasm;
1499 #endif
1502 #ifdef SQLITE_SHELL_FIDDLE
1503 static ShellState shellState;
1504 #endif
1507 /* Allowed values for ShellState.autoEQP
1509 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1510 #define AUTOEQP_on 1 /* Automatic EQP is on */
1511 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1512 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1514 /* Allowed values for ShellState.openMode
1516 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1517 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1518 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1519 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1520 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1521 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
1522 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
1524 /* Allowed values for ShellState.eTraceType
1526 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1527 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1528 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1530 /* Bits in the ShellState.flgProgress variable */
1531 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1532 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
1533 ** callback limit is reached, and for each
1534 ** top-level SQL statement */
1535 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
1538 ** These are the allowed shellFlgs values
1540 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1541 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1542 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1543 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1544 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1545 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1546 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
1547 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
1548 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
1549 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
1550 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
1553 ** Macros for testing and setting shellFlgs
1555 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1556 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1557 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1560 ** These are the allowed modes.
1562 #define MODE_Line 0 /* One column per line. Blank line between records */
1563 #define MODE_Column 1 /* One record per line in neat columns */
1564 #define MODE_List 2 /* One record per line with a separator */
1565 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1566 #define MODE_Html 4 /* Generate an XHTML table */
1567 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1568 #define MODE_Quote 6 /* Quote values as for SQL */
1569 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1570 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1571 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1572 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1573 #define MODE_Pretty 11 /* Pretty-print schemas */
1574 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1575 #define MODE_Json 13 /* Output JSON */
1576 #define MODE_Markdown 14 /* Markdown formatting */
1577 #define MODE_Table 15 /* MySQL-style table formatting */
1578 #define MODE_Box 16 /* Unicode box-drawing characters */
1579 #define MODE_Count 17 /* Output only a count of the rows of output */
1580 #define MODE_Off 18 /* No query output shown */
1582 static const char *modeDescr[] = {
1583 "line",
1584 "column",
1585 "list",
1586 "semi",
1587 "html",
1588 "insert",
1589 "quote",
1590 "tcl",
1591 "csv",
1592 "explain",
1593 "ascii",
1594 "prettyprint",
1595 "eqp",
1596 "json",
1597 "markdown",
1598 "table",
1599 "box",
1600 "count",
1601 "off"
1605 ** These are the column/row/line separators used by the various
1606 ** import/export modes.
1608 #define SEP_Column "|"
1609 #define SEP_Row "\n"
1610 #define SEP_Tab "\t"
1611 #define SEP_Space " "
1612 #define SEP_Comma ","
1613 #define SEP_CrLf "\r\n"
1614 #define SEP_Unit "\x1F"
1615 #define SEP_Record "\x1E"
1618 ** Limit input nesting via .read or any other input redirect.
1619 ** It's not too expensive, so a generous allowance can be made.
1621 #define MAX_INPUT_NESTING 25
1624 ** A callback for the sqlite3_log() interface.
1626 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1627 ShellState *p = (ShellState*)pArg;
1628 if( p->pLog==0 ) return;
1629 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1630 fflush(p->pLog);
1634 ** SQL function: shell_putsnl(X)
1636 ** Write the text X to the screen (or whatever output is being directed)
1637 ** adding a newline at the end, and then return X.
1639 static void shellPutsFunc(
1640 sqlite3_context *pCtx,
1641 int nVal,
1642 sqlite3_value **apVal
1644 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1645 (void)nVal;
1646 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1647 sqlite3_result_value(pCtx, apVal[0]);
1651 ** If in safe mode, print an error message described by the arguments
1652 ** and exit immediately.
1654 static void failIfSafeMode(
1655 ShellState *p,
1656 const char *zErrMsg,
1659 if( p->bSafeMode ){
1660 va_list ap;
1661 char *zMsg;
1662 va_start(ap, zErrMsg);
1663 zMsg = sqlite3_vmprintf(zErrMsg, ap);
1664 va_end(ap);
1665 raw_printf(stderr, "line %d: ", p->lineno);
1666 utf8_printf(stderr, "%s\n", zMsg);
1667 exit(1);
1672 ** SQL function: edit(VALUE)
1673 ** edit(VALUE,EDITOR)
1675 ** These steps:
1677 ** (1) Write VALUE into a temporary file.
1678 ** (2) Run program EDITOR on that temporary file.
1679 ** (3) Read the temporary file back and return its content as the result.
1680 ** (4) Delete the temporary file
1682 ** If the EDITOR argument is omitted, use the value in the VISUAL
1683 ** environment variable. If still there is no EDITOR, through an error.
1685 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1687 #ifndef SQLITE_NOHAVE_SYSTEM
1688 static void editFunc(
1689 sqlite3_context *context,
1690 int argc,
1691 sqlite3_value **argv
1693 const char *zEditor;
1694 char *zTempFile = 0;
1695 sqlite3 *db;
1696 char *zCmd = 0;
1697 int bBin;
1698 int rc;
1699 int hasCRNL = 0;
1700 FILE *f = 0;
1701 sqlite3_int64 sz;
1702 sqlite3_int64 x;
1703 unsigned char *p = 0;
1705 if( argc==2 ){
1706 zEditor = (const char*)sqlite3_value_text(argv[1]);
1707 }else{
1708 zEditor = getenv("VISUAL");
1710 if( zEditor==0 ){
1711 sqlite3_result_error(context, "no editor for edit()", -1);
1712 return;
1714 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1715 sqlite3_result_error(context, "NULL input to edit()", -1);
1716 return;
1718 db = sqlite3_context_db_handle(context);
1719 zTempFile = 0;
1720 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1721 if( zTempFile==0 ){
1722 sqlite3_uint64 r = 0;
1723 sqlite3_randomness(sizeof(r), &r);
1724 zTempFile = sqlite3_mprintf("temp%llx", r);
1725 if( zTempFile==0 ){
1726 sqlite3_result_error_nomem(context);
1727 return;
1730 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1731 /* When writing the file to be edited, do \n to \r\n conversions on systems
1732 ** that want \r\n line endings */
1733 f = fopen(zTempFile, bBin ? "wb" : "w");
1734 if( f==0 ){
1735 sqlite3_result_error(context, "edit() cannot open temp file", -1);
1736 goto edit_func_end;
1738 sz = sqlite3_value_bytes(argv[0]);
1739 if( bBin ){
1740 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1741 }else{
1742 const char *z = (const char*)sqlite3_value_text(argv[0]);
1743 /* Remember whether or not the value originally contained \r\n */
1744 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1745 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1747 fclose(f);
1748 f = 0;
1749 if( x!=sz ){
1750 sqlite3_result_error(context, "edit() could not write the whole file", -1);
1751 goto edit_func_end;
1753 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1754 if( zCmd==0 ){
1755 sqlite3_result_error_nomem(context);
1756 goto edit_func_end;
1758 rc = system(zCmd);
1759 sqlite3_free(zCmd);
1760 if( rc ){
1761 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1762 goto edit_func_end;
1764 f = fopen(zTempFile, "rb");
1765 if( f==0 ){
1766 sqlite3_result_error(context,
1767 "edit() cannot reopen temp file after edit", -1);
1768 goto edit_func_end;
1770 fseek(f, 0, SEEK_END);
1771 sz = ftell(f);
1772 rewind(f);
1773 p = sqlite3_malloc64( sz+1 );
1774 if( p==0 ){
1775 sqlite3_result_error_nomem(context);
1776 goto edit_func_end;
1778 x = fread(p, 1, (size_t)sz, f);
1779 fclose(f);
1780 f = 0;
1781 if( x!=sz ){
1782 sqlite3_result_error(context, "could not read back the whole file", -1);
1783 goto edit_func_end;
1785 if( bBin ){
1786 sqlite3_result_blob64(context, p, sz, sqlite3_free);
1787 }else{
1788 sqlite3_int64 i, j;
1789 if( hasCRNL ){
1790 /* If the original contains \r\n then do no conversions back to \n */
1791 }else{
1792 /* If the file did not originally contain \r\n then convert any new
1793 ** \r\n back into \n */
1794 p[sz] = 0;
1795 for(i=j=0; i<sz; i++){
1796 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1797 p[j++] = p[i];
1799 sz = j;
1800 p[sz] = 0;
1802 sqlite3_result_text64(context, (const char*)p, sz,
1803 sqlite3_free, SQLITE_UTF8);
1805 p = 0;
1807 edit_func_end:
1808 if( f ) fclose(f);
1809 unlink(zTempFile);
1810 sqlite3_free(zTempFile);
1811 sqlite3_free(p);
1813 #endif /* SQLITE_NOHAVE_SYSTEM */
1816 ** Save or restore the current output mode
1818 static void outputModePush(ShellState *p){
1819 p->modePrior = p->mode;
1820 p->priorShFlgs = p->shellFlgs;
1821 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1822 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1824 static void outputModePop(ShellState *p){
1825 p->mode = p->modePrior;
1826 p->shellFlgs = p->priorShFlgs;
1827 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1828 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1832 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1834 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1835 int i;
1836 unsigned char *aBlob = (unsigned char*)pBlob;
1838 char *zStr = sqlite3_malloc(nBlob*2 + 1);
1839 shell_check_oom(zStr);
1841 for(i=0; i<nBlob; i++){
1842 static const char aHex[] = {
1843 '0', '1', '2', '3', '4', '5', '6', '7',
1844 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1846 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1847 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1849 zStr[i*2] = '\0';
1851 raw_printf(out,"X'%s'", zStr);
1852 sqlite3_free(zStr);
1856 ** Find a string that is not found anywhere in z[]. Return a pointer
1857 ** to that string.
1859 ** Try to use zA and zB first. If both of those are already found in z[]
1860 ** then make up some string and store it in the buffer zBuf.
1862 static const char *unused_string(
1863 const char *z, /* Result must not appear anywhere in z */
1864 const char *zA, const char *zB, /* Try these first */
1865 char *zBuf /* Space to store a generated string */
1867 unsigned i = 0;
1868 if( strstr(z, zA)==0 ) return zA;
1869 if( strstr(z, zB)==0 ) return zB;
1871 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1872 }while( strstr(z,zBuf)!=0 );
1873 return zBuf;
1877 ** Output the given string as a quoted string using SQL quoting conventions.
1879 ** See also: output_quoted_escaped_string()
1881 static void output_quoted_string(FILE *out, const char *z){
1882 int i;
1883 char c;
1884 setBinaryMode(out, 1);
1885 if( z==0 ) return;
1886 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1887 if( c==0 ){
1888 utf8_printf(out,"'%s'",z);
1889 }else{
1890 raw_printf(out, "'");
1891 while( *z ){
1892 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1893 if( c=='\'' ) i++;
1894 if( i ){
1895 utf8_printf(out, "%.*s", i, z);
1896 z += i;
1898 if( c=='\'' ){
1899 raw_printf(out, "'");
1900 continue;
1902 if( c==0 ){
1903 break;
1905 z++;
1907 raw_printf(out, "'");
1909 setTextMode(out, 1);
1913 ** Output the given string as a quoted string using SQL quoting conventions.
1914 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1915 ** get corrupted by end-of-line translation facilities in some operating
1916 ** systems.
1918 ** This is like output_quoted_string() but with the addition of the \r\n
1919 ** escape mechanism.
1921 static void output_quoted_escaped_string(FILE *out, const char *z){
1922 int i;
1923 char c;
1924 setBinaryMode(out, 1);
1925 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1926 if( c==0 ){
1927 utf8_printf(out,"'%s'",z);
1928 }else{
1929 const char *zNL = 0;
1930 const char *zCR = 0;
1931 int nNL = 0;
1932 int nCR = 0;
1933 char zBuf1[20], zBuf2[20];
1934 for(i=0; z[i]; i++){
1935 if( z[i]=='\n' ) nNL++;
1936 if( z[i]=='\r' ) nCR++;
1938 if( nNL ){
1939 raw_printf(out, "replace(");
1940 zNL = unused_string(z, "\\n", "\\012", zBuf1);
1942 if( nCR ){
1943 raw_printf(out, "replace(");
1944 zCR = unused_string(z, "\\r", "\\015", zBuf2);
1946 raw_printf(out, "'");
1947 while( *z ){
1948 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1949 if( c=='\'' ) i++;
1950 if( i ){
1951 utf8_printf(out, "%.*s", i, z);
1952 z += i;
1954 if( c=='\'' ){
1955 raw_printf(out, "'");
1956 continue;
1958 if( c==0 ){
1959 break;
1961 z++;
1962 if( c=='\n' ){
1963 raw_printf(out, "%s", zNL);
1964 continue;
1966 raw_printf(out, "%s", zCR);
1968 raw_printf(out, "'");
1969 if( nCR ){
1970 raw_printf(out, ",'%s',char(13))", zCR);
1972 if( nNL ){
1973 raw_printf(out, ",'%s',char(10))", zNL);
1976 setTextMode(out, 1);
1980 ** Output the given string as a quoted according to C or TCL quoting rules.
1982 static void output_c_string(FILE *out, const char *z){
1983 unsigned int c;
1984 fputc('"', out);
1985 while( (c = *(z++))!=0 ){
1986 if( c=='\\' ){
1987 fputc(c, out);
1988 fputc(c, out);
1989 }else if( c=='"' ){
1990 fputc('\\', out);
1991 fputc('"', out);
1992 }else if( c=='\t' ){
1993 fputc('\\', out);
1994 fputc('t', out);
1995 }else if( c=='\n' ){
1996 fputc('\\', out);
1997 fputc('n', out);
1998 }else if( c=='\r' ){
1999 fputc('\\', out);
2000 fputc('r', out);
2001 }else if( !isprint(c&0xff) ){
2002 raw_printf(out, "\\%03o", c&0xff);
2003 }else{
2004 fputc(c, out);
2007 fputc('"', out);
2011 ** Output the given string as a quoted according to JSON quoting rules.
2013 static void output_json_string(FILE *out, const char *z, i64 n){
2014 unsigned int c;
2015 if( z==0 ) z = "";
2016 if( n<0 ) n = strlen(z);
2017 fputc('"', out);
2018 while( n-- ){
2019 c = *(z++);
2020 if( c=='\\' || c=='"' ){
2021 fputc('\\', out);
2022 fputc(c, out);
2023 }else if( c<=0x1f ){
2024 fputc('\\', out);
2025 if( c=='\b' ){
2026 fputc('b', out);
2027 }else if( c=='\f' ){
2028 fputc('f', out);
2029 }else if( c=='\n' ){
2030 fputc('n', out);
2031 }else if( c=='\r' ){
2032 fputc('r', out);
2033 }else if( c=='\t' ){
2034 fputc('t', out);
2035 }else{
2036 raw_printf(out, "u%04x",c);
2038 }else{
2039 fputc(c, out);
2042 fputc('"', out);
2046 ** Output the given string with characters that are special to
2047 ** HTML escaped.
2049 static void output_html_string(FILE *out, const char *z){
2050 int i;
2051 if( z==0 ) z = "";
2052 while( *z ){
2053 for(i=0; z[i]
2054 && z[i]!='<'
2055 && z[i]!='&'
2056 && z[i]!='>'
2057 && z[i]!='\"'
2058 && z[i]!='\'';
2059 i++){}
2060 if( i>0 ){
2061 utf8_printf(out,"%.*s",i,z);
2063 if( z[i]=='<' ){
2064 raw_printf(out,"&lt;");
2065 }else if( z[i]=='&' ){
2066 raw_printf(out,"&amp;");
2067 }else if( z[i]=='>' ){
2068 raw_printf(out,"&gt;");
2069 }else if( z[i]=='\"' ){
2070 raw_printf(out,"&quot;");
2071 }else if( z[i]=='\'' ){
2072 raw_printf(out,"&#39;");
2073 }else{
2074 break;
2076 z += i + 1;
2081 ** If a field contains any character identified by a 1 in the following
2082 ** array, then the string must be quoted for CSV.
2084 static const char needCsvQuote[] = {
2085 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2086 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2087 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
2088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2093 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2094 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2095 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2096 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2097 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2098 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2099 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2104 ** Output a single term of CSV. Actually, p->colSeparator is used for
2105 ** the separator, which may or may not be a comma. p->nullValue is
2106 ** the null value. Strings are quoted if necessary. The separator
2107 ** is only issued if bSep is true.
2109 static void output_csv(ShellState *p, const char *z, int bSep){
2110 FILE *out = p->out;
2111 if( z==0 ){
2112 utf8_printf(out,"%s",p->nullValue);
2113 }else{
2114 unsigned i;
2115 for(i=0; z[i]; i++){
2116 if( needCsvQuote[((unsigned char*)z)[i]] ){
2117 i = 0;
2118 break;
2121 if( i==0 || strstr(z, p->colSeparator)!=0 ){
2122 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
2123 shell_check_oom(zQuoted);
2124 utf8_printf(out, "%s", zQuoted);
2125 sqlite3_free(zQuoted);
2126 }else{
2127 utf8_printf(out, "%s", z);
2130 if( bSep ){
2131 utf8_printf(p->out, "%s", p->colSeparator);
2136 ** This routine runs when the user presses Ctrl-C
2138 static void interrupt_handler(int NotUsed){
2139 UNUSED_PARAMETER(NotUsed);
2140 if( ++seenInterrupt>1 ) exit(1);
2141 if( globalDb ) sqlite3_interrupt(globalDb);
2144 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2146 ** This routine runs for console events (e.g. Ctrl-C) on Win32
2148 static BOOL WINAPI ConsoleCtrlHandler(
2149 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
2151 if( dwCtrlType==CTRL_C_EVENT ){
2152 interrupt_handler(0);
2153 return TRUE;
2155 return FALSE;
2157 #endif
2159 #ifndef SQLITE_OMIT_AUTHORIZATION
2161 ** This authorizer runs in safe mode.
2163 static int safeModeAuth(
2164 void *pClientData,
2165 int op,
2166 const char *zA1,
2167 const char *zA2,
2168 const char *zA3,
2169 const char *zA4
2171 ShellState *p = (ShellState*)pClientData;
2172 static const char *azProhibitedFunctions[] = {
2173 "edit",
2174 "fts3_tokenizer",
2175 "load_extension",
2176 "readfile",
2177 "writefile",
2178 "zipfile",
2179 "zipfile_cds",
2181 UNUSED_PARAMETER(zA1);
2182 UNUSED_PARAMETER(zA3);
2183 UNUSED_PARAMETER(zA4);
2184 switch( op ){
2185 case SQLITE_ATTACH: {
2186 #ifndef SQLITE_SHELL_FIDDLE
2187 /* In WASM builds the filesystem is a virtual sandbox, so
2188 ** there's no harm in using ATTACH. */
2189 failIfSafeMode(p, "cannot run ATTACH in safe mode");
2190 #endif
2191 break;
2193 case SQLITE_FUNCTION: {
2194 int i;
2195 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
2196 if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
2197 failIfSafeMode(p, "cannot use the %s() function in safe mode",
2198 azProhibitedFunctions[i]);
2201 break;
2204 return SQLITE_OK;
2208 ** When the ".auth ON" is set, the following authorizer callback is
2209 ** invoked. It always returns SQLITE_OK.
2211 static int shellAuth(
2212 void *pClientData,
2213 int op,
2214 const char *zA1,
2215 const char *zA2,
2216 const char *zA3,
2217 const char *zA4
2219 ShellState *p = (ShellState*)pClientData;
2220 static const char *azAction[] = { 0,
2221 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2222 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2223 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2224 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2225 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2226 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2227 "PRAGMA", "READ", "SELECT",
2228 "TRANSACTION", "UPDATE", "ATTACH",
2229 "DETACH", "ALTER_TABLE", "REINDEX",
2230 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2231 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2233 int i;
2234 const char *az[4];
2235 az[0] = zA1;
2236 az[1] = zA2;
2237 az[2] = zA3;
2238 az[3] = zA4;
2239 utf8_printf(p->out, "authorizer: %s", azAction[op]);
2240 for(i=0; i<4; i++){
2241 raw_printf(p->out, " ");
2242 if( az[i] ){
2243 output_c_string(p->out, az[i]);
2244 }else{
2245 raw_printf(p->out, "NULL");
2248 raw_printf(p->out, "\n");
2249 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
2250 return SQLITE_OK;
2252 #endif
2255 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2257 ** This routine converts some CREATE TABLE statements for shadow tables
2258 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2260 ** If the schema statement in z[] contains a start-of-comment and if
2261 ** sqlite3_complete() returns false, try to terminate the comment before
2262 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2264 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
2265 char *zToFree = 0;
2266 if( z==0 ) return;
2267 if( zTail==0 ) return;
2268 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
2269 const char *zOrig = z;
2270 static const char *azTerm[] = { "", "*/", "\n" };
2271 int i;
2272 for(i=0; i<ArraySize(azTerm); i++){
2273 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
2274 shell_check_oom(zNew);
2275 if( sqlite3_complete(zNew) ){
2276 size_t n = strlen(zNew);
2277 zNew[n-1] = 0;
2278 zToFree = zNew;
2279 z = zNew;
2280 break;
2282 sqlite3_free(zNew);
2285 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
2286 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
2287 }else{
2288 utf8_printf(out, "%s%s", z, zTail);
2290 sqlite3_free(zToFree);
2292 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
2293 char c = z[n];
2294 z[n] = 0;
2295 printSchemaLine(out, z, zTail);
2296 z[n] = c;
2300 ** Return true if string z[] has nothing but whitespace and comments to the
2301 ** end of the first line.
2303 static int wsToEol(const char *z){
2304 int i;
2305 for(i=0; z[i]; i++){
2306 if( z[i]=='\n' ) return 1;
2307 if( IsSpace(z[i]) ) continue;
2308 if( z[i]=='-' && z[i+1]=='-' ) return 1;
2309 return 0;
2311 return 1;
2315 ** Add a new entry to the EXPLAIN QUERY PLAN data
2317 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2318 EQPGraphRow *pNew;
2319 i64 nText;
2320 if( zText==0 ) return;
2321 nText = strlen(zText);
2322 if( p->autoEQPtest ){
2323 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2325 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2326 shell_check_oom(pNew);
2327 pNew->iEqpId = iEqpId;
2328 pNew->iParentId = p2;
2329 memcpy(pNew->zText, zText, nText+1);
2330 pNew->pNext = 0;
2331 if( p->sGraph.pLast ){
2332 p->sGraph.pLast->pNext = pNew;
2333 }else{
2334 p->sGraph.pRow = pNew;
2336 p->sGraph.pLast = pNew;
2340 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2341 ** in p->sGraph.
2343 static void eqp_reset(ShellState *p){
2344 EQPGraphRow *pRow, *pNext;
2345 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2346 pNext = pRow->pNext;
2347 sqlite3_free(pRow);
2349 memset(&p->sGraph, 0, sizeof(p->sGraph));
2352 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2353 ** pOld, or return the first such line if pOld is NULL
2355 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2356 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2357 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2358 return pRow;
2361 /* Render a single level of the graph that has iEqpId as its parent. Called
2362 ** recursively to render sublevels.
2364 static void eqp_render_level(ShellState *p, int iEqpId){
2365 EQPGraphRow *pRow, *pNext;
2366 i64 n = strlen(p->sGraph.zPrefix);
2367 char *z;
2368 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2369 pNext = eqp_next_row(p, iEqpId, pRow);
2370 z = pRow->zText;
2371 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2372 pNext ? "|--" : "`--", z);
2373 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2374 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
2375 eqp_render_level(p, pRow->iEqpId);
2376 p->sGraph.zPrefix[n] = 0;
2382 ** Display and reset the EXPLAIN QUERY PLAN data
2384 static void eqp_render(ShellState *p, i64 nCycle){
2385 EQPGraphRow *pRow = p->sGraph.pRow;
2386 if( pRow ){
2387 if( pRow->zText[0]=='-' ){
2388 if( pRow->pNext==0 ){
2389 eqp_reset(p);
2390 return;
2392 utf8_printf(p->out, "%s\n", pRow->zText+3);
2393 p->sGraph.pRow = pRow->pNext;
2394 sqlite3_free(pRow);
2395 }else if( nCycle>0 ){
2396 utf8_printf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
2397 }else{
2398 utf8_printf(p->out, "QUERY PLAN\n");
2400 p->sGraph.zPrefix[0] = 0;
2401 eqp_render_level(p, 0);
2402 eqp_reset(p);
2406 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2408 ** Progress handler callback.
2410 static int progress_handler(void *pClientData) {
2411 ShellState *p = (ShellState*)pClientData;
2412 p->nProgress++;
2413 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2414 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2415 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2416 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2417 return 1;
2419 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2420 raw_printf(p->out, "Progress %u\n", p->nProgress);
2422 return 0;
2424 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2427 ** Print N dashes
2429 static void print_dashes(FILE *out, int N){
2430 const char zDash[] = "--------------------------------------------------";
2431 const int nDash = sizeof(zDash) - 1;
2432 while( N>nDash ){
2433 fputs(zDash, out);
2434 N -= nDash;
2436 raw_printf(out, "%.*s", N, zDash);
2440 ** Print a markdown or table-style row separator using ascii-art
2442 static void print_row_separator(
2443 ShellState *p,
2444 int nArg,
2445 const char *zSep
2447 int i;
2448 if( nArg>0 ){
2449 fputs(zSep, p->out);
2450 print_dashes(p->out, p->actualWidth[0]+2);
2451 for(i=1; i<nArg; i++){
2452 fputs(zSep, p->out);
2453 print_dashes(p->out, p->actualWidth[i]+2);
2455 fputs(zSep, p->out);
2457 fputs("\n", p->out);
2461 ** This is the callback routine that the shell
2462 ** invokes for each row of a query result.
2464 static int shell_callback(
2465 void *pArg,
2466 int nArg, /* Number of result columns */
2467 char **azArg, /* Text of each result column */
2468 char **azCol, /* Column names */
2469 int *aiType /* Column types. Might be NULL */
2471 int i;
2472 ShellState *p = (ShellState*)pArg;
2474 if( azArg==0 ) return 0;
2475 switch( p->cMode ){
2476 case MODE_Count:
2477 case MODE_Off: {
2478 break;
2480 case MODE_Line: {
2481 int w = 5;
2482 if( azArg==0 ) break;
2483 for(i=0; i<nArg; i++){
2484 int len = strlen30(azCol[i] ? azCol[i] : "");
2485 if( len>w ) w = len;
2487 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
2488 for(i=0; i<nArg; i++){
2489 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
2490 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2492 break;
2494 case MODE_Explain: {
2495 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2496 if( nArg>ArraySize(aExplainWidth) ){
2497 nArg = ArraySize(aExplainWidth);
2499 if( p->cnt++==0 ){
2500 for(i=0; i<nArg; i++){
2501 int w = aExplainWidth[i];
2502 utf8_width_print(p->out, w, azCol[i]);
2503 fputs(i==nArg-1 ? "\n" : " ", p->out);
2505 for(i=0; i<nArg; i++){
2506 int w = aExplainWidth[i];
2507 print_dashes(p->out, w);
2508 fputs(i==nArg-1 ? "\n" : " ", p->out);
2511 if( azArg==0 ) break;
2512 for(i=0; i<nArg; i++){
2513 int w = aExplainWidth[i];
2514 if( i==nArg-1 ) w = 0;
2515 if( azArg[i] && strlenChar(azArg[i])>w ){
2516 w = strlenChar(azArg[i]);
2518 if( i==1 && p->aiIndent && p->pStmt ){
2519 if( p->iIndent<p->nIndent ){
2520 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2522 p->iIndent++;
2524 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2525 fputs(i==nArg-1 ? "\n" : " ", p->out);
2527 break;
2529 case MODE_Semi: { /* .schema and .fullschema output */
2530 printSchemaLine(p->out, azArg[0], ";\n");
2531 break;
2533 case MODE_Pretty: { /* .schema and .fullschema with --indent */
2534 char *z;
2535 int j;
2536 int nParen = 0;
2537 char cEnd = 0;
2538 char c;
2539 int nLine = 0;
2540 assert( nArg==1 );
2541 if( azArg[0]==0 ) break;
2542 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2543 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2545 utf8_printf(p->out, "%s;\n", azArg[0]);
2546 break;
2548 z = sqlite3_mprintf("%s", azArg[0]);
2549 shell_check_oom(z);
2550 j = 0;
2551 for(i=0; IsSpace(z[i]); i++){}
2552 for(; (c = z[i])!=0; i++){
2553 if( IsSpace(c) ){
2554 if( z[j-1]=='\r' ) z[j-1] = '\n';
2555 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2556 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2557 j--;
2559 z[j++] = c;
2561 while( j>0 && IsSpace(z[j-1]) ){ j--; }
2562 z[j] = 0;
2563 if( strlen30(z)>=79 ){
2564 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2565 if( c==cEnd ){
2566 cEnd = 0;
2567 }else if( c=='"' || c=='\'' || c=='`' ){
2568 cEnd = c;
2569 }else if( c=='[' ){
2570 cEnd = ']';
2571 }else if( c=='-' && z[i+1]=='-' ){
2572 cEnd = '\n';
2573 }else if( c=='(' ){
2574 nParen++;
2575 }else if( c==')' ){
2576 nParen--;
2577 if( nLine>0 && nParen==0 && j>0 ){
2578 printSchemaLineN(p->out, z, j, "\n");
2579 j = 0;
2582 z[j++] = c;
2583 if( nParen==1 && cEnd==0
2584 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2586 if( c=='\n' ) j--;
2587 printSchemaLineN(p->out, z, j, "\n ");
2588 j = 0;
2589 nLine++;
2590 while( IsSpace(z[i+1]) ){ i++; }
2593 z[j] = 0;
2595 printSchemaLine(p->out, z, ";\n");
2596 sqlite3_free(z);
2597 break;
2599 case MODE_List: {
2600 if( p->cnt++==0 && p->showHeader ){
2601 for(i=0; i<nArg; i++){
2602 utf8_printf(p->out,"%s%s",azCol[i],
2603 i==nArg-1 ? p->rowSeparator : p->colSeparator);
2606 if( azArg==0 ) break;
2607 for(i=0; i<nArg; i++){
2608 char *z = azArg[i];
2609 if( z==0 ) z = p->nullValue;
2610 utf8_printf(p->out, "%s", z);
2611 if( i<nArg-1 ){
2612 utf8_printf(p->out, "%s", p->colSeparator);
2613 }else{
2614 utf8_printf(p->out, "%s", p->rowSeparator);
2617 break;
2619 case MODE_Html: {
2620 if( p->cnt++==0 && p->showHeader ){
2621 raw_printf(p->out,"<TR>");
2622 for(i=0; i<nArg; i++){
2623 raw_printf(p->out,"<TH>");
2624 output_html_string(p->out, azCol[i]);
2625 raw_printf(p->out,"</TH>\n");
2627 raw_printf(p->out,"</TR>\n");
2629 if( azArg==0 ) break;
2630 raw_printf(p->out,"<TR>");
2631 for(i=0; i<nArg; i++){
2632 raw_printf(p->out,"<TD>");
2633 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2634 raw_printf(p->out,"</TD>\n");
2636 raw_printf(p->out,"</TR>\n");
2637 break;
2639 case MODE_Tcl: {
2640 if( p->cnt++==0 && p->showHeader ){
2641 for(i=0; i<nArg; i++){
2642 output_c_string(p->out,azCol[i] ? azCol[i] : "");
2643 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2645 utf8_printf(p->out, "%s", p->rowSeparator);
2647 if( azArg==0 ) break;
2648 for(i=0; i<nArg; i++){
2649 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2650 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2652 utf8_printf(p->out, "%s", p->rowSeparator);
2653 break;
2655 case MODE_Csv: {
2656 setBinaryMode(p->out, 1);
2657 if( p->cnt++==0 && p->showHeader ){
2658 for(i=0; i<nArg; i++){
2659 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2661 utf8_printf(p->out, "%s", p->rowSeparator);
2663 if( nArg>0 ){
2664 for(i=0; i<nArg; i++){
2665 output_csv(p, azArg[i], i<nArg-1);
2667 utf8_printf(p->out, "%s", p->rowSeparator);
2669 setTextMode(p->out, 1);
2670 break;
2672 case MODE_Insert: {
2673 if( azArg==0 ) break;
2674 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2675 if( p->showHeader ){
2676 raw_printf(p->out,"(");
2677 for(i=0; i<nArg; i++){
2678 if( i>0 ) raw_printf(p->out, ",");
2679 if( quoteChar(azCol[i]) ){
2680 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2681 shell_check_oom(z);
2682 utf8_printf(p->out, "%s", z);
2683 sqlite3_free(z);
2684 }else{
2685 raw_printf(p->out, "%s", azCol[i]);
2688 raw_printf(p->out,")");
2690 p->cnt++;
2691 for(i=0; i<nArg; i++){
2692 raw_printf(p->out, i>0 ? "," : " VALUES(");
2693 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2694 utf8_printf(p->out,"NULL");
2695 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2696 if( ShellHasFlag(p, SHFLG_Newlines) ){
2697 output_quoted_string(p->out, azArg[i]);
2698 }else{
2699 output_quoted_escaped_string(p->out, azArg[i]);
2701 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2702 utf8_printf(p->out,"%s", azArg[i]);
2703 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2704 char z[50];
2705 double r = sqlite3_column_double(p->pStmt, i);
2706 sqlite3_uint64 ur;
2707 memcpy(&ur,&r,sizeof(r));
2708 if( ur==0x7ff0000000000000LL ){
2709 raw_printf(p->out, "9.0e+999");
2710 }else if( ur==0xfff0000000000000LL ){
2711 raw_printf(p->out, "-9.0e+999");
2712 }else{
2713 sqlite3_int64 ir = (sqlite3_int64)r;
2714 if( r==(double)ir ){
2715 sqlite3_snprintf(50,z,"%lld.0", ir);
2716 }else{
2717 sqlite3_snprintf(50,z,"%!.20g", r);
2719 raw_printf(p->out, "%s", z);
2721 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2722 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2723 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2724 output_hex_blob(p->out, pBlob, nBlob);
2725 }else if( isNumber(azArg[i], 0) ){
2726 utf8_printf(p->out,"%s", azArg[i]);
2727 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2728 output_quoted_string(p->out, azArg[i]);
2729 }else{
2730 output_quoted_escaped_string(p->out, azArg[i]);
2733 raw_printf(p->out,");\n");
2734 break;
2736 case MODE_Json: {
2737 if( azArg==0 ) break;
2738 if( p->cnt==0 ){
2739 fputs("[{", p->out);
2740 }else{
2741 fputs(",\n{", p->out);
2743 p->cnt++;
2744 for(i=0; i<nArg; i++){
2745 output_json_string(p->out, azCol[i], -1);
2746 putc(':', p->out);
2747 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2748 fputs("null",p->out);
2749 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2750 char z[50];
2751 double r = sqlite3_column_double(p->pStmt, i);
2752 sqlite3_uint64 ur;
2753 memcpy(&ur,&r,sizeof(r));
2754 if( ur==0x7ff0000000000000LL ){
2755 raw_printf(p->out, "9.0e+999");
2756 }else if( ur==0xfff0000000000000LL ){
2757 raw_printf(p->out, "-9.0e+999");
2758 }else{
2759 sqlite3_snprintf(50,z,"%!.20g", r);
2760 raw_printf(p->out, "%s", z);
2762 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2763 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2764 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2765 output_json_string(p->out, pBlob, nBlob);
2766 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2767 output_json_string(p->out, azArg[i], -1);
2768 }else{
2769 utf8_printf(p->out,"%s", azArg[i]);
2771 if( i<nArg-1 ){
2772 putc(',', p->out);
2775 putc('}', p->out);
2776 break;
2778 case MODE_Quote: {
2779 if( azArg==0 ) break;
2780 if( p->cnt==0 && p->showHeader ){
2781 for(i=0; i<nArg; i++){
2782 if( i>0 ) fputs(p->colSeparator, p->out);
2783 output_quoted_string(p->out, azCol[i]);
2785 fputs(p->rowSeparator, p->out);
2787 p->cnt++;
2788 for(i=0; i<nArg; i++){
2789 if( i>0 ) fputs(p->colSeparator, p->out);
2790 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2791 utf8_printf(p->out,"NULL");
2792 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2793 output_quoted_string(p->out, azArg[i]);
2794 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2795 utf8_printf(p->out,"%s", azArg[i]);
2796 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2797 char z[50];
2798 double r = sqlite3_column_double(p->pStmt, i);
2799 sqlite3_snprintf(50,z,"%!.20g", r);
2800 raw_printf(p->out, "%s", z);
2801 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2802 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2803 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2804 output_hex_blob(p->out, pBlob, nBlob);
2805 }else if( isNumber(azArg[i], 0) ){
2806 utf8_printf(p->out,"%s", azArg[i]);
2807 }else{
2808 output_quoted_string(p->out, azArg[i]);
2811 fputs(p->rowSeparator, p->out);
2812 break;
2814 case MODE_Ascii: {
2815 if( p->cnt++==0 && p->showHeader ){
2816 for(i=0; i<nArg; i++){
2817 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2818 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2820 utf8_printf(p->out, "%s", p->rowSeparator);
2822 if( azArg==0 ) break;
2823 for(i=0; i<nArg; i++){
2824 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2825 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2827 utf8_printf(p->out, "%s", p->rowSeparator);
2828 break;
2830 case MODE_EQP: {
2831 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2832 break;
2835 return 0;
2839 ** This is the callback routine that the SQLite library
2840 ** invokes for each row of a query result.
2842 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2843 /* since we don't have type info, call the shell_callback with a NULL value */
2844 return shell_callback(pArg, nArg, azArg, azCol, NULL);
2848 ** This is the callback routine from sqlite3_exec() that appends all
2849 ** output onto the end of a ShellText object.
2851 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2852 ShellText *p = (ShellText*)pArg;
2853 int i;
2854 UNUSED_PARAMETER(az);
2855 if( azArg==0 ) return 0;
2856 if( p->n ) appendText(p, "|", 0);
2857 for(i=0; i<nArg; i++){
2858 if( i ) appendText(p, ",", 0);
2859 if( azArg[i] ) appendText(p, azArg[i], 0);
2861 return 0;
2865 ** Generate an appropriate SELFTEST table in the main database.
2867 static void createSelftestTable(ShellState *p){
2868 char *zErrMsg = 0;
2869 sqlite3_exec(p->db,
2870 "SAVEPOINT selftest_init;\n"
2871 "CREATE TABLE IF NOT EXISTS selftest(\n"
2872 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2873 " op TEXT,\n" /* Operator: memo run */
2874 " cmd TEXT,\n" /* Command text */
2875 " ans TEXT\n" /* Desired answer */
2876 ");"
2877 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2878 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2879 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2880 " 'memo','Tests generated by --init');\n"
2881 "INSERT INTO [_shell$self]\n"
2882 " SELECT 'run',\n"
2883 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2884 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2885 " hex(sha3_query('SELECT type,name,tbl_name,sql "
2886 "FROM sqlite_schema ORDER BY 2',224));\n"
2887 "INSERT INTO [_shell$self]\n"
2888 " SELECT 'run',"
2889 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2890 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2891 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2892 " FROM (\n"
2893 " SELECT name FROM sqlite_schema\n"
2894 " WHERE type='table'\n"
2895 " AND name<>'selftest'\n"
2896 " AND coalesce(rootpage,0)>0\n"
2897 " )\n"
2898 " ORDER BY name;\n"
2899 "INSERT INTO [_shell$self]\n"
2900 " VALUES('run','PRAGMA integrity_check','ok');\n"
2901 "INSERT INTO selftest(tno,op,cmd,ans)"
2902 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2903 "DROP TABLE [_shell$self];"
2904 ,0,0,&zErrMsg);
2905 if( zErrMsg ){
2906 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2907 sqlite3_free(zErrMsg);
2909 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2914 ** Set the destination table field of the ShellState structure to
2915 ** the name of the table given. Escape any quote characters in the
2916 ** table name.
2918 static void set_table_name(ShellState *p, const char *zName){
2919 int i, n;
2920 char cQuote;
2921 char *z;
2923 if( p->zDestTable ){
2924 free(p->zDestTable);
2925 p->zDestTable = 0;
2927 if( zName==0 ) return;
2928 cQuote = quoteChar(zName);
2929 n = strlen30(zName);
2930 if( cQuote ) n += n+2;
2931 z = p->zDestTable = malloc( n+1 );
2932 shell_check_oom(z);
2933 n = 0;
2934 if( cQuote ) z[n++] = cQuote;
2935 for(i=0; zName[i]; i++){
2936 z[n++] = zName[i];
2937 if( zName[i]==cQuote ) z[n++] = cQuote;
2939 if( cQuote ) z[n++] = cQuote;
2940 z[n] = 0;
2944 ** Maybe construct two lines of text that point out the position of a
2945 ** syntax error. Return a pointer to the text, in memory obtained from
2946 ** sqlite3_malloc(). Or, if the most recent error does not involve a
2947 ** specific token that we can point to, return an empty string.
2949 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
2950 ** and should be released by the caller invoking sqlite3_free().
2952 static char *shell_error_context(const char *zSql, sqlite3 *db){
2953 int iOffset;
2954 size_t len;
2955 char *zCode;
2956 char *zMsg;
2957 int i;
2958 if( db==0
2959 || zSql==0
2960 || (iOffset = sqlite3_error_offset(db))<0
2961 || iOffset>=(int)strlen(zSql)
2963 return sqlite3_mprintf("");
2965 while( iOffset>50 ){
2966 iOffset--;
2967 zSql++;
2968 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
2970 len = strlen(zSql);
2971 if( len>78 ){
2972 len = 78;
2973 while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
2975 zCode = sqlite3_mprintf("%.*s", len, zSql);
2976 shell_check_oom(zCode);
2977 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
2978 if( iOffset<25 ){
2979 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,"");
2980 }else{
2981 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,"");
2983 return zMsg;
2988 ** Execute a query statement that will generate SQL output. Print
2989 ** the result columns, comma-separated, on a line and then add a
2990 ** semicolon terminator to the end of that line.
2992 ** If the number of columns is 1 and that column contains text "--"
2993 ** then write the semicolon on a separate line. That way, if a
2994 ** "--" comment occurs at the end of the statement, the comment
2995 ** won't consume the semicolon terminator.
2997 static int run_table_dump_query(
2998 ShellState *p, /* Query context */
2999 const char *zSelect /* SELECT statement to extract content */
3001 sqlite3_stmt *pSelect;
3002 int rc;
3003 int nResult;
3004 int i;
3005 const char *z;
3006 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
3007 if( rc!=SQLITE_OK || !pSelect ){
3008 char *zContext = shell_error_context(zSelect, p->db);
3009 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n%s", rc,
3010 sqlite3_errmsg(p->db), zContext);
3011 sqlite3_free(zContext);
3012 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3013 return rc;
3015 rc = sqlite3_step(pSelect);
3016 nResult = sqlite3_column_count(pSelect);
3017 while( rc==SQLITE_ROW ){
3018 z = (const char*)sqlite3_column_text(pSelect, 0);
3019 utf8_printf(p->out, "%s", z);
3020 for(i=1; i<nResult; i++){
3021 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
3023 if( z==0 ) z = "";
3024 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
3025 if( z[0] ){
3026 raw_printf(p->out, "\n;\n");
3027 }else{
3028 raw_printf(p->out, ";\n");
3030 rc = sqlite3_step(pSelect);
3032 rc = sqlite3_finalize(pSelect);
3033 if( rc!=SQLITE_OK ){
3034 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
3035 sqlite3_errmsg(p->db));
3036 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3038 return rc;
3042 ** Allocate space and save off string indicating current error.
3044 static char *save_err_msg(
3045 sqlite3 *db, /* Database to query */
3046 const char *zPhase, /* When the error occcurs */
3047 int rc, /* Error code returned from API */
3048 const char *zSql /* SQL string, or NULL */
3050 char *zErr;
3051 char *zContext;
3052 sqlite3_str *pStr = sqlite3_str_new(0);
3053 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
3054 if( rc>1 ){
3055 sqlite3_str_appendf(pStr, " (%d)", rc);
3057 zContext = shell_error_context(zSql, db);
3058 if( zContext ){
3059 sqlite3_str_appendall(pStr, zContext);
3060 sqlite3_free(zContext);
3062 zErr = sqlite3_str_finish(pStr);
3063 shell_check_oom(zErr);
3064 return zErr;
3067 #ifdef __linux__
3069 ** Attempt to display I/O stats on Linux using /proc/PID/io
3071 static void displayLinuxIoStats(FILE *out){
3072 FILE *in;
3073 char z[200];
3074 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
3075 in = fopen(z, "rb");
3076 if( in==0 ) return;
3077 while( fgets(z, sizeof(z), in)!=0 ){
3078 static const struct {
3079 const char *zPattern;
3080 const char *zDesc;
3081 } aTrans[] = {
3082 { "rchar: ", "Bytes received by read():" },
3083 { "wchar: ", "Bytes sent to write():" },
3084 { "syscr: ", "Read() system calls:" },
3085 { "syscw: ", "Write() system calls:" },
3086 { "read_bytes: ", "Bytes read from storage:" },
3087 { "write_bytes: ", "Bytes written to storage:" },
3088 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3090 int i;
3091 for(i=0; i<ArraySize(aTrans); i++){
3092 int n = strlen30(aTrans[i].zPattern);
3093 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
3094 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
3095 break;
3099 fclose(in);
3101 #endif
3104 ** Display a single line of status using 64-bit values.
3106 static void displayStatLine(
3107 ShellState *p, /* The shell context */
3108 char *zLabel, /* Label for this one line */
3109 char *zFormat, /* Format for the result */
3110 int iStatusCtrl, /* Which status to display */
3111 int bReset /* True to reset the stats */
3113 sqlite3_int64 iCur = -1;
3114 sqlite3_int64 iHiwtr = -1;
3115 int i, nPercent;
3116 char zLine[200];
3117 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
3118 for(i=0, nPercent=0; zFormat[i]; i++){
3119 if( zFormat[i]=='%' ) nPercent++;
3121 if( nPercent>1 ){
3122 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
3123 }else{
3124 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
3126 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
3130 ** Display memory stats.
3132 static int display_stats(
3133 sqlite3 *db, /* Database to query */
3134 ShellState *pArg, /* Pointer to ShellState */
3135 int bReset /* True to reset the stats */
3137 int iCur;
3138 int iHiwtr;
3139 FILE *out;
3140 if( pArg==0 || pArg->out==0 ) return 0;
3141 out = pArg->out;
3143 if( pArg->pStmt && pArg->statsOn==2 ){
3144 int nCol, i, x;
3145 sqlite3_stmt *pStmt = pArg->pStmt;
3146 char z[100];
3147 nCol = sqlite3_column_count(pStmt);
3148 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
3149 for(i=0; i<nCol; i++){
3150 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
3151 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
3152 #ifndef SQLITE_OMIT_DECLTYPE
3153 sqlite3_snprintf(30, z+x, "declared type:");
3154 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
3155 #endif
3156 #ifdef SQLITE_ENABLE_COLUMN_METADATA
3157 sqlite3_snprintf(30, z+x, "database name:");
3158 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
3159 sqlite3_snprintf(30, z+x, "table name:");
3160 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
3161 sqlite3_snprintf(30, z+x, "origin name:");
3162 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
3163 #endif
3167 if( pArg->statsOn==3 ){
3168 if( pArg->pStmt ){
3169 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
3170 raw_printf(pArg->out, "VM-steps: %d\n", iCur);
3172 return 0;
3175 displayStatLine(pArg, "Memory Used:",
3176 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
3177 displayStatLine(pArg, "Number of Outstanding Allocations:",
3178 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
3179 if( pArg->shellFlgs & SHFLG_Pagecache ){
3180 displayStatLine(pArg, "Number of Pcache Pages Used:",
3181 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
3183 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
3184 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
3185 displayStatLine(pArg, "Largest Allocation:",
3186 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
3187 displayStatLine(pArg, "Largest Pcache Allocation:",
3188 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
3189 #ifdef YYTRACKMAXSTACKDEPTH
3190 displayStatLine(pArg, "Deepest Parser Stack:",
3191 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
3192 #endif
3194 if( db ){
3195 if( pArg->shellFlgs & SHFLG_Lookaside ){
3196 iHiwtr = iCur = -1;
3197 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
3198 &iCur, &iHiwtr, bReset);
3199 raw_printf(pArg->out,
3200 "Lookaside Slots Used: %d (max %d)\n",
3201 iCur, iHiwtr);
3202 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
3203 &iCur, &iHiwtr, bReset);
3204 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
3205 iHiwtr);
3206 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
3207 &iCur, &iHiwtr, bReset);
3208 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
3209 iHiwtr);
3210 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
3211 &iCur, &iHiwtr, bReset);
3212 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
3213 iHiwtr);
3215 iHiwtr = iCur = -1;
3216 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
3217 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
3218 iCur);
3219 iHiwtr = iCur = -1;
3220 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
3221 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
3222 iHiwtr = iCur = -1;
3223 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
3224 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
3225 iHiwtr = iCur = -1;
3226 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
3227 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
3228 iHiwtr = iCur = -1;
3229 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
3230 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
3231 iHiwtr = iCur = -1;
3232 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
3233 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
3234 iCur);
3235 iHiwtr = iCur = -1;
3236 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
3237 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
3238 iCur);
3241 if( pArg->pStmt ){
3242 int iHit, iMiss;
3243 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
3244 bReset);
3245 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
3246 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
3247 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
3248 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
3249 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
3250 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
3251 bReset);
3252 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
3253 bReset);
3254 if( iHit || iMiss ){
3255 raw_printf(pArg->out, "Bloom filter bypass taken: %d/%d\n",
3256 iHit, iHit+iMiss);
3258 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
3259 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
3260 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
3261 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
3262 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
3263 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
3264 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
3265 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
3268 #ifdef __linux__
3269 displayLinuxIoStats(pArg->out);
3270 #endif
3272 /* Do not remove this machine readable comment: extra-stats-output-here */
3274 return 0;
3278 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3279 static int scanStatsHeight(sqlite3_stmt *p, int iEntry){
3280 int iPid = 0;
3281 int ret = 1;
3282 sqlite3_stmt_scanstatus_v2(p, iEntry,
3283 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3285 while( iPid!=0 ){
3286 int ii;
3287 for(ii=0; 1; ii++){
3288 int iId;
3289 int res;
3290 res = sqlite3_stmt_scanstatus_v2(p, ii,
3291 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
3293 if( res ) break;
3294 if( iId==iPid ){
3295 sqlite3_stmt_scanstatus_v2(p, ii,
3296 SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3300 ret++;
3302 return ret;
3304 #endif
3307 ** Display scan stats.
3309 static void display_scanstats(
3310 sqlite3 *db, /* Database to query */
3311 ShellState *pArg /* Pointer to ShellState */
3313 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3314 UNUSED_PARAMETER(db);
3315 UNUSED_PARAMETER(pArg);
3316 #else
3317 static const int f = SQLITE_SCANSTAT_COMPLEX;
3318 sqlite3_stmt *p = pArg->pStmt;
3319 int ii = 0;
3320 i64 nTotal = 0;
3321 int nWidth = 0;
3322 eqp_reset(pArg);
3324 for(ii=0; 1; ii++){
3325 const char *z = 0;
3326 int n = 0;
3327 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
3328 break;
3330 n = strlen(z) + scanStatsHeight(p, ii)*3;
3331 if( n>nWidth ) nWidth = n;
3333 nWidth += 4;
3335 sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
3336 for(ii=0; 1; ii++){
3337 i64 nLoop = 0;
3338 i64 nRow = 0;
3339 i64 nCycle = 0;
3340 int iId = 0;
3341 int iPid = 0;
3342 const char *z = 0;
3343 const char *zName = 0;
3344 char *zText = 0;
3345 double rEst = 0.0;
3347 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
3348 break;
3350 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
3351 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
3352 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
3353 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
3354 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
3355 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
3356 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
3358 zText = sqlite3_mprintf("%s", z);
3359 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
3360 char *z = 0;
3361 if( nCycle>=0 && nTotal>0 ){
3362 z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
3363 nCycle, ((nCycle*100)+nTotal/2) / nTotal
3366 if( nLoop>=0 ){
3367 z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop);
3369 if( nRow>=0 ){
3370 z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow);
3373 if( zName && pArg->scanstatsOn>1 ){
3374 double rpl = (double)nRow / (double)nLoop;
3375 z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst);
3378 zText = sqlite3_mprintf(
3379 "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z
3383 eqp_append(pArg, iId, iPid, zText);
3384 sqlite3_free(zText);
3387 eqp_render(pArg, nTotal);
3388 #endif
3392 ** Parameter azArray points to a zero-terminated array of strings. zStr
3393 ** points to a single nul-terminated string. Return non-zero if zStr
3394 ** is equal, according to strcmp(), to any of the strings in the array.
3395 ** Otherwise, return zero.
3397 static int str_in_array(const char *zStr, const char **azArray){
3398 int i;
3399 for(i=0; azArray[i]; i++){
3400 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3402 return 0;
3406 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3407 ** and populate the ShellState.aiIndent[] array with the number of
3408 ** spaces each opcode should be indented before it is output.
3410 ** The indenting rules are:
3412 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3413 ** all opcodes that occur between the p2 jump destination and the opcode
3414 ** itself by 2 spaces.
3416 ** * Do the previous for "Return" instructions for when P2 is positive.
3417 ** See tag-20220407a in wherecode.c and vdbe.c.
3419 ** * For each "Goto", if the jump destination is earlier in the program
3420 ** and ends on one of:
3421 ** Yield SeekGt SeekLt RowSetRead Rewind
3422 ** or if the P1 parameter is one instead of zero,
3423 ** then indent all opcodes between the earlier instruction
3424 ** and "Goto" by 2 spaces.
3426 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3427 const char *zSql; /* The text of the SQL statement */
3428 const char *z; /* Used to check if this is an EXPLAIN */
3429 int *abYield = 0; /* True if op is an OP_Yield */
3430 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
3431 int iOp; /* Index of operation in p->aiIndent[] */
3433 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3434 "Return", 0 };
3435 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3436 "Rewind", 0 };
3437 const char *azGoto[] = { "Goto", 0 };
3439 /* Try to figure out if this is really an EXPLAIN statement. If this
3440 ** cannot be verified, return early. */
3441 if( sqlite3_column_count(pSql)!=8 ){
3442 p->cMode = p->mode;
3443 return;
3445 zSql = sqlite3_sql(pSql);
3446 if( zSql==0 ) return;
3447 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
3448 if( sqlite3_strnicmp(z, "explain", 7) ){
3449 p->cMode = p->mode;
3450 return;
3453 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3454 int i;
3455 int iAddr = sqlite3_column_int(pSql, 0);
3456 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3458 /* Set p2 to the P2 field of the current opcode. Then, assuming that
3459 ** p2 is an instruction address, set variable p2op to the index of that
3460 ** instruction in the aiIndent[] array. p2 and p2op may be different if
3461 ** the current instruction is part of a sub-program generated by an
3462 ** SQL trigger or foreign key. */
3463 int p2 = sqlite3_column_int(pSql, 3);
3464 int p2op = (p2 + (iOp-iAddr));
3466 /* Grow the p->aiIndent array as required */
3467 if( iOp>=nAlloc ){
3468 if( iOp==0 ){
3469 /* Do further verfication that this is explain output. Abort if
3470 ** it is not */
3471 static const char *explainCols[] = {
3472 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3473 int jj;
3474 for(jj=0; jj<ArraySize(explainCols); jj++){
3475 if( cli_strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
3476 p->cMode = p->mode;
3477 sqlite3_reset(pSql);
3478 return;
3482 nAlloc += 100;
3483 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3484 shell_check_oom(p->aiIndent);
3485 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3486 shell_check_oom(abYield);
3488 abYield[iOp] = str_in_array(zOp, azYield);
3489 p->aiIndent[iOp] = 0;
3490 p->nIndent = iOp+1;
3492 if( str_in_array(zOp, azNext) && p2op>0 ){
3493 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3495 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
3496 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
3498 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3502 p->iIndent = 0;
3503 sqlite3_free(abYield);
3504 sqlite3_reset(pSql);
3508 ** Free the array allocated by explain_data_prepare().
3510 static void explain_data_delete(ShellState *p){
3511 sqlite3_free(p->aiIndent);
3512 p->aiIndent = 0;
3513 p->nIndent = 0;
3514 p->iIndent = 0;
3518 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3520 static unsigned int savedSelectTrace;
3521 static unsigned int savedWhereTrace;
3522 static void disable_debug_trace_modes(void){
3523 unsigned int zero = 0;
3524 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3525 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3526 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3527 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3529 static void restore_debug_trace_modes(void){
3530 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3531 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3534 /* Create the TEMP table used to store parameter bindings */
3535 static void bind_table_init(ShellState *p){
3536 int wrSchema = 0;
3537 int defensiveMode = 0;
3538 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3539 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3540 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3541 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3542 sqlite3_exec(p->db,
3543 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3544 " key TEXT PRIMARY KEY,\n"
3545 " value\n"
3546 ") WITHOUT ROWID;",
3547 0, 0, 0);
3548 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3549 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3553 ** Bind parameters on a prepared statement.
3555 ** Parameter bindings are taken from a TEMP table of the form:
3557 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3558 ** WITHOUT ROWID;
3560 ** No bindings occur if this table does not exist. The name of the table
3561 ** begins with "sqlite_" so that it will not collide with ordinary application
3562 ** tables. The table must be in the TEMP schema.
3564 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3565 int nVar;
3566 int i;
3567 int rc;
3568 sqlite3_stmt *pQ = 0;
3570 nVar = sqlite3_bind_parameter_count(pStmt);
3571 if( nVar==0 ) return; /* Nothing to do */
3572 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3573 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3574 rc = SQLITE_NOTFOUND;
3575 pQ = 0;
3576 }else{
3577 rc = sqlite3_prepare_v2(pArg->db,
3578 "SELECT value FROM temp.sqlite_parameters"
3579 " WHERE key=?1", -1, &pQ, 0);
3581 for(i=1; i<=nVar; i++){
3582 char zNum[30];
3583 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3584 if( zVar==0 ){
3585 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3586 zVar = zNum;
3588 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3589 if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
3590 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3591 #ifdef NAN
3592 }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
3593 sqlite3_bind_double(pStmt, i, NAN);
3594 #endif
3595 #ifdef INFINITY
3596 }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
3597 sqlite3_bind_double(pStmt, i, INFINITY);
3598 #endif
3599 }else{
3600 sqlite3_bind_null(pStmt, i);
3602 sqlite3_reset(pQ);
3604 sqlite3_finalize(pQ);
3608 ** UTF8 box-drawing characters. Imagine box lines like this:
3610 ** 1
3611 ** |
3612 ** 4 --+-- 2
3613 ** |
3614 ** 3
3616 ** Each box characters has between 2 and 4 of the lines leading from
3617 ** the center. The characters are here identified by the numbers of
3618 ** their corresponding lines.
3620 #define BOX_24 "\342\224\200" /* U+2500 --- */
3621 #define BOX_13 "\342\224\202" /* U+2502 | */
3622 #define BOX_23 "\342\224\214" /* U+250c ,- */
3623 #define BOX_34 "\342\224\220" /* U+2510 -, */
3624 #define BOX_12 "\342\224\224" /* U+2514 '- */
3625 #define BOX_14 "\342\224\230" /* U+2518 -' */
3626 #define BOX_123 "\342\224\234" /* U+251c |- */
3627 #define BOX_134 "\342\224\244" /* U+2524 -| */
3628 #define BOX_234 "\342\224\254" /* U+252c -,- */
3629 #define BOX_124 "\342\224\264" /* U+2534 -'- */
3630 #define BOX_1234 "\342\224\274" /* U+253c -|- */
3632 /* Draw horizontal line N characters long using unicode box
3633 ** characters
3635 static void print_box_line(FILE *out, int N){
3636 const char zDash[] =
3637 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3638 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3639 const int nDash = sizeof(zDash) - 1;
3640 N *= 3;
3641 while( N>nDash ){
3642 utf8_printf(out, zDash);
3643 N -= nDash;
3645 utf8_printf(out, "%.*s", N, zDash);
3649 ** Draw a horizontal separator for a MODE_Box table.
3651 static void print_box_row_separator(
3652 ShellState *p,
3653 int nArg,
3654 const char *zSep1,
3655 const char *zSep2,
3656 const char *zSep3
3658 int i;
3659 if( nArg>0 ){
3660 utf8_printf(p->out, "%s", zSep1);
3661 print_box_line(p->out, p->actualWidth[0]+2);
3662 for(i=1; i<nArg; i++){
3663 utf8_printf(p->out, "%s", zSep2);
3664 print_box_line(p->out, p->actualWidth[i]+2);
3666 utf8_printf(p->out, "%s", zSep3);
3668 fputs("\n", p->out);
3672 ** z[] is a line of text that is to be displayed the .mode box or table or
3673 ** similar tabular formats. z[] might contain control characters such
3674 ** as \n, \t, \f, or \r.
3676 ** Compute characters to display on the first line of z[]. Stop at the
3677 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
3678 ** from malloc()) of that first line, which caller should free sometime.
3679 ** Write anything to display on the next line into *pzTail. If this is
3680 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3682 static char *translateForDisplayAndDup(
3683 const unsigned char *z, /* Input text to be transformed */
3684 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
3685 int mxWidth, /* Max width. 0 means no limit */
3686 u8 bWordWrap /* If true, avoid breaking mid-word */
3688 int i; /* Input bytes consumed */
3689 int j; /* Output bytes generated */
3690 int k; /* Input bytes to be displayed */
3691 int n; /* Output column number */
3692 unsigned char *zOut; /* Output text */
3694 if( z==0 ){
3695 *pzTail = 0;
3696 return 0;
3698 if( mxWidth<0 ) mxWidth = -mxWidth;
3699 if( mxWidth==0 ) mxWidth = 1000000;
3700 i = j = n = 0;
3701 while( n<mxWidth ){
3702 if( z[i]>=' ' ){
3703 n++;
3704 do{ i++; j++; }while( (z[i]&0xc0)==0x80 );
3705 continue;
3707 if( z[i]=='\t' ){
3709 n++;
3710 j++;
3711 }while( (n&7)!=0 && n<mxWidth );
3712 i++;
3713 continue;
3715 break;
3717 if( n>=mxWidth && bWordWrap ){
3718 /* Perhaps try to back up to a better place to break the line */
3719 for(k=i; k>i/2; k--){
3720 if( isspace(z[k-1]) ) break;
3722 if( k<=i/2 ){
3723 for(k=i; k>i/2; k--){
3724 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3727 if( k<=i/2 ){
3728 k = i;
3729 }else{
3730 i = k;
3731 while( z[i]==' ' ) i++;
3733 }else{
3734 k = i;
3736 if( n>=mxWidth && z[i]>=' ' ){
3737 *pzTail = &z[i];
3738 }else if( z[i]=='\r' && z[i+1]=='\n' ){
3739 *pzTail = z[i+2] ? &z[i+2] : 0;
3740 }else if( z[i]==0 || z[i+1]==0 ){
3741 *pzTail = 0;
3742 }else{
3743 *pzTail = &z[i+1];
3745 zOut = malloc( j+1 );
3746 shell_check_oom(zOut);
3747 i = j = n = 0;
3748 while( i<k ){
3749 if( z[i]>=' ' ){
3750 n++;
3751 do{ zOut[j++] = z[i++]; }while( (z[i]&0xc0)==0x80 );
3752 continue;
3754 if( z[i]=='\t' ){
3756 n++;
3757 zOut[j++] = ' ';
3758 }while( (n&7)!=0 && n<mxWidth );
3759 i++;
3760 continue;
3762 break;
3764 zOut[j] = 0;
3765 return (char*)zOut;
3768 /* Extract the value of the i-th current column for pStmt as an SQL literal
3769 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
3770 ** the caller.
3772 static char *quoted_column(sqlite3_stmt *pStmt, int i){
3773 switch( sqlite3_column_type(pStmt, i) ){
3774 case SQLITE_NULL: {
3775 return sqlite3_mprintf("NULL");
3777 case SQLITE_INTEGER:
3778 case SQLITE_FLOAT: {
3779 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3781 case SQLITE_TEXT: {
3782 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3784 case SQLITE_BLOB: {
3785 int j;
3786 sqlite3_str *pStr = sqlite3_str_new(0);
3787 const unsigned char *a = sqlite3_column_blob(pStmt,i);
3788 int n = sqlite3_column_bytes(pStmt,i);
3789 sqlite3_str_append(pStr, "x'", 2);
3790 for(j=0; j<n; j++){
3791 sqlite3_str_appendf(pStr, "%02x", a[j]);
3793 sqlite3_str_append(pStr, "'", 1);
3794 return sqlite3_str_finish(pStr);
3797 return 0; /* Not reached */
3801 ** Run a prepared statement and output the result in one of the
3802 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3803 ** or MODE_Box.
3805 ** This is different from ordinary exec_prepared_stmt() in that
3806 ** it has to run the entire query and gather the results into memory
3807 ** first, in order to determine column widths, before providing
3808 ** any output.
3810 static void exec_prepared_stmt_columnar(
3811 ShellState *p, /* Pointer to ShellState */
3812 sqlite3_stmt *pStmt /* Statment to run */
3814 sqlite3_int64 nRow = 0;
3815 int nColumn = 0;
3816 char **azData = 0;
3817 sqlite3_int64 nAlloc = 0;
3818 char *abRowDiv = 0;
3819 const unsigned char *uz;
3820 const char *z;
3821 char **azQuoted = 0;
3822 int rc;
3823 sqlite3_int64 i, nData;
3824 int j, nTotal, w, n;
3825 const char *colSep = 0;
3826 const char *rowSep = 0;
3827 const unsigned char **azNextLine = 0;
3828 int bNextLine = 0;
3829 int bMultiLineRowExists = 0;
3830 int bw = p->cmOpts.bWordWrap;
3831 const char *zEmpty = "";
3832 const char *zShowNull = p->nullValue;
3834 rc = sqlite3_step(pStmt);
3835 if( rc!=SQLITE_ROW ) return;
3836 nColumn = sqlite3_column_count(pStmt);
3837 nAlloc = nColumn*4;
3838 if( nAlloc<=0 ) nAlloc = 1;
3839 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
3840 shell_check_oom(azData);
3841 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
3842 shell_check_oom(azNextLine);
3843 memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
3844 if( p->cmOpts.bQuote ){
3845 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
3846 shell_check_oom(azQuoted);
3847 memset(azQuoted, 0, nColumn*sizeof(char*) );
3849 abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
3850 shell_check_oom(abRowDiv);
3851 if( nColumn>p->nWidth ){
3852 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
3853 shell_check_oom(p->colWidth);
3854 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
3855 p->nWidth = nColumn;
3856 p->actualWidth = &p->colWidth[nColumn];
3858 memset(p->actualWidth, 0, nColumn*sizeof(int));
3859 for(i=0; i<nColumn; i++){
3860 w = p->colWidth[i];
3861 if( w<0 ) w = -w;
3862 p->actualWidth[i] = w;
3864 for(i=0; i<nColumn; i++){
3865 const unsigned char *zNotUsed;
3866 int wx = p->colWidth[i];
3867 if( wx==0 ){
3868 wx = p->cmOpts.iWrap;
3870 if( wx<0 ) wx = -wx;
3871 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
3872 if( uz==0 ) uz = (u8*)"";
3873 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
3876 int useNextLine = bNextLine;
3877 bNextLine = 0;
3878 if( (nRow+2)*nColumn >= nAlloc ){
3879 nAlloc *= 2;
3880 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
3881 shell_check_oom(azData);
3882 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
3883 shell_check_oom(abRowDiv);
3885 abRowDiv[nRow] = 1;
3886 nRow++;
3887 for(i=0; i<nColumn; i++){
3888 int wx = p->colWidth[i];
3889 if( wx==0 ){
3890 wx = p->cmOpts.iWrap;
3892 if( wx<0 ) wx = -wx;
3893 if( useNextLine ){
3894 uz = azNextLine[i];
3895 if( uz==0 ) uz = (u8*)zEmpty;
3896 }else if( p->cmOpts.bQuote ){
3897 sqlite3_free(azQuoted[i]);
3898 azQuoted[i] = quoted_column(pStmt,i);
3899 uz = (const unsigned char*)azQuoted[i];
3900 }else{
3901 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
3902 if( uz==0 ) uz = (u8*)zShowNull;
3904 azData[nRow*nColumn + i]
3905 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
3906 if( azNextLine[i] ){
3907 bNextLine = 1;
3908 abRowDiv[nRow-1] = 0;
3909 bMultiLineRowExists = 1;
3912 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
3913 nTotal = nColumn*(nRow+1);
3914 for(i=0; i<nTotal; i++){
3915 z = azData[i];
3916 if( z==0 ) z = (char*)zEmpty;
3917 n = strlenChar(z);
3918 j = i%nColumn;
3919 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
3921 if( seenInterrupt ) goto columnar_end;
3922 if( nColumn==0 ) goto columnar_end;
3923 switch( p->cMode ){
3924 case MODE_Column: {
3925 colSep = " ";
3926 rowSep = "\n";
3927 if( p->showHeader ){
3928 for(i=0; i<nColumn; i++){
3929 w = p->actualWidth[i];
3930 if( p->colWidth[i]<0 ) w = -w;
3931 utf8_width_print(p->out, w, azData[i]);
3932 fputs(i==nColumn-1?"\n":" ", p->out);
3934 for(i=0; i<nColumn; i++){
3935 print_dashes(p->out, p->actualWidth[i]);
3936 fputs(i==nColumn-1?"\n":" ", p->out);
3939 break;
3941 case MODE_Table: {
3942 colSep = " | ";
3943 rowSep = " |\n";
3944 print_row_separator(p, nColumn, "+");
3945 fputs("| ", p->out);
3946 for(i=0; i<nColumn; i++){
3947 w = p->actualWidth[i];
3948 n = strlenChar(azData[i]);
3949 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3950 fputs(i==nColumn-1?" |\n":" | ", p->out);
3952 print_row_separator(p, nColumn, "+");
3953 break;
3955 case MODE_Markdown: {
3956 colSep = " | ";
3957 rowSep = " |\n";
3958 fputs("| ", p->out);
3959 for(i=0; i<nColumn; i++){
3960 w = p->actualWidth[i];
3961 n = strlenChar(azData[i]);
3962 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
3963 fputs(i==nColumn-1?" |\n":" | ", p->out);
3965 print_row_separator(p, nColumn, "|");
3966 break;
3968 case MODE_Box: {
3969 colSep = " " BOX_13 " ";
3970 rowSep = " " BOX_13 "\n";
3971 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
3972 utf8_printf(p->out, BOX_13 " ");
3973 for(i=0; i<nColumn; i++){
3974 w = p->actualWidth[i];
3975 n = strlenChar(azData[i]);
3976 utf8_printf(p->out, "%*s%s%*s%s",
3977 (w-n)/2, "", azData[i], (w-n+1)/2, "",
3978 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
3980 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
3981 break;
3984 for(i=nColumn, j=0; i<nTotal; i++, j++){
3985 if( j==0 && p->cMode!=MODE_Column ){
3986 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
3988 z = azData[i];
3989 if( z==0 ) z = p->nullValue;
3990 w = p->actualWidth[j];
3991 if( p->colWidth[j]<0 ) w = -w;
3992 utf8_width_print(p->out, w, z);
3993 if( j==nColumn-1 ){
3994 utf8_printf(p->out, "%s", rowSep);
3995 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
3996 if( p->cMode==MODE_Table ){
3997 print_row_separator(p, nColumn, "+");
3998 }else if( p->cMode==MODE_Box ){
3999 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
4000 }else if( p->cMode==MODE_Column ){
4001 raw_printf(p->out, "\n");
4004 j = -1;
4005 if( seenInterrupt ) goto columnar_end;
4006 }else{
4007 utf8_printf(p->out, "%s", colSep);
4010 if( p->cMode==MODE_Table ){
4011 print_row_separator(p, nColumn, "+");
4012 }else if( p->cMode==MODE_Box ){
4013 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
4015 columnar_end:
4016 if( seenInterrupt ){
4017 utf8_printf(p->out, "Interrupt\n");
4019 nData = (nRow+1)*nColumn;
4020 for(i=0; i<nData; i++){
4021 z = azData[i];
4022 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
4024 sqlite3_free(azData);
4025 sqlite3_free((void*)azNextLine);
4026 sqlite3_free(abRowDiv);
4027 if( azQuoted ){
4028 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
4029 sqlite3_free(azQuoted);
4034 ** Run a prepared statement
4036 static void exec_prepared_stmt(
4037 ShellState *pArg, /* Pointer to ShellState */
4038 sqlite3_stmt *pStmt /* Statment to run */
4040 int rc;
4041 sqlite3_uint64 nRow = 0;
4043 if( pArg->cMode==MODE_Column
4044 || pArg->cMode==MODE_Table
4045 || pArg->cMode==MODE_Box
4046 || pArg->cMode==MODE_Markdown
4048 exec_prepared_stmt_columnar(pArg, pStmt);
4049 return;
4052 /* perform the first step. this will tell us if we
4053 ** have a result set or not and how wide it is.
4055 rc = sqlite3_step(pStmt);
4056 /* if we have a result set... */
4057 if( SQLITE_ROW == rc ){
4058 /* allocate space for col name ptr, value ptr, and type */
4059 int nCol = sqlite3_column_count(pStmt);
4060 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
4061 if( !pData ){
4062 shell_out_of_memory();
4063 }else{
4064 char **azCols = (char **)pData; /* Names of result columns */
4065 char **azVals = &azCols[nCol]; /* Results */
4066 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
4067 int i, x;
4068 assert(sizeof(int) <= sizeof(char *));
4069 /* save off ptrs to column names */
4070 for(i=0; i<nCol; i++){
4071 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
4074 nRow++;
4075 /* extract the data and data types */
4076 for(i=0; i<nCol; i++){
4077 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
4078 if( x==SQLITE_BLOB
4079 && pArg
4080 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
4082 azVals[i] = "";
4083 }else{
4084 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
4086 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
4087 rc = SQLITE_NOMEM;
4088 break; /* from for */
4090 } /* end for */
4092 /* if data and types extracted successfully... */
4093 if( SQLITE_ROW == rc ){
4094 /* call the supplied callback with the result row data */
4095 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
4096 rc = SQLITE_ABORT;
4097 }else{
4098 rc = sqlite3_step(pStmt);
4101 } while( SQLITE_ROW == rc );
4102 sqlite3_free(pData);
4103 if( pArg->cMode==MODE_Json ){
4104 fputs("]\n", pArg->out);
4105 }else if( pArg->cMode==MODE_Count ){
4106 char zBuf[200];
4107 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
4108 nRow, nRow!=1 ? "s" : "");
4109 printf("%s", zBuf);
4115 #ifndef SQLITE_OMIT_VIRTUALTABLE
4117 ** This function is called to process SQL if the previous shell command
4118 ** was ".expert". It passes the SQL in the second argument directly to
4119 ** the sqlite3expert object.
4121 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4122 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4123 ** an English language error message. It is the responsibility of the
4124 ** caller to eventually free this buffer using sqlite3_free().
4126 static int expertHandleSQL(
4127 ShellState *pState,
4128 const char *zSql,
4129 char **pzErr
4131 assert( pState->expert.pExpert );
4132 assert( pzErr==0 || *pzErr==0 );
4133 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
4137 ** This function is called either to silently clean up the object
4138 ** created by the ".expert" command (if bCancel==1), or to generate a
4139 ** report from it and then clean it up (if bCancel==0).
4141 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4142 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4143 ** an English language error message. It is the responsibility of the
4144 ** caller to eventually free this buffer using sqlite3_free().
4146 static int expertFinish(
4147 ShellState *pState,
4148 int bCancel,
4149 char **pzErr
4151 int rc = SQLITE_OK;
4152 sqlite3expert *p = pState->expert.pExpert;
4153 assert( p );
4154 assert( bCancel || pzErr==0 || *pzErr==0 );
4155 if( bCancel==0 ){
4156 FILE *out = pState->out;
4157 int bVerbose = pState->expert.bVerbose;
4159 rc = sqlite3_expert_analyze(p, pzErr);
4160 if( rc==SQLITE_OK ){
4161 int nQuery = sqlite3_expert_count(p);
4162 int i;
4164 if( bVerbose ){
4165 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
4166 raw_printf(out, "-- Candidates -----------------------------\n");
4167 raw_printf(out, "%s\n", zCand);
4169 for(i=0; i<nQuery; i++){
4170 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
4171 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
4172 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
4173 if( zIdx==0 ) zIdx = "(no new indexes)\n";
4174 if( bVerbose ){
4175 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
4176 raw_printf(out, "%s\n\n", zSql);
4178 raw_printf(out, "%s\n", zIdx);
4179 raw_printf(out, "%s\n", zEQP);
4183 sqlite3_expert_destroy(p);
4184 pState->expert.pExpert = 0;
4185 return rc;
4189 ** Implementation of ".expert" dot command.
4191 static int expertDotCommand(
4192 ShellState *pState, /* Current shell tool state */
4193 char **azArg, /* Array of arguments passed to dot command */
4194 int nArg /* Number of entries in azArg[] */
4196 int rc = SQLITE_OK;
4197 char *zErr = 0;
4198 int i;
4199 int iSample = 0;
4201 assert( pState->expert.pExpert==0 );
4202 memset(&pState->expert, 0, sizeof(ExpertInfo));
4204 for(i=1; rc==SQLITE_OK && i<nArg; i++){
4205 char *z = azArg[i];
4206 int n;
4207 if( z[0]=='-' && z[1]=='-' ) z++;
4208 n = strlen30(z);
4209 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
4210 pState->expert.bVerbose = 1;
4212 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
4213 if( i==(nArg-1) ){
4214 raw_printf(stderr, "option requires an argument: %s\n", z);
4215 rc = SQLITE_ERROR;
4216 }else{
4217 iSample = (int)integerValue(azArg[++i]);
4218 if( iSample<0 || iSample>100 ){
4219 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
4220 rc = SQLITE_ERROR;
4224 else{
4225 raw_printf(stderr, "unknown option: %s\n", z);
4226 rc = SQLITE_ERROR;
4230 if( rc==SQLITE_OK ){
4231 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
4232 if( pState->expert.pExpert==0 ){
4233 raw_printf(stderr, "sqlite3_expert_new: %s\n",
4234 zErr ? zErr : "out of memory");
4235 rc = SQLITE_ERROR;
4236 }else{
4237 sqlite3_expert_config(
4238 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
4242 sqlite3_free(zErr);
4244 return rc;
4246 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
4249 ** Execute a statement or set of statements. Print
4250 ** any result rows/columns depending on the current mode
4251 ** set via the supplied callback.
4253 ** This is very similar to SQLite's built-in sqlite3_exec()
4254 ** function except it takes a slightly different callback
4255 ** and callback data argument.
4257 static int shell_exec(
4258 ShellState *pArg, /* Pointer to ShellState */
4259 const char *zSql, /* SQL to be evaluated */
4260 char **pzErrMsg /* Error msg written here */
4262 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
4263 int rc = SQLITE_OK; /* Return Code */
4264 int rc2;
4265 const char *zLeftover; /* Tail of unprocessed SQL */
4266 sqlite3 *db = pArg->db;
4268 if( pzErrMsg ){
4269 *pzErrMsg = NULL;
4272 #ifndef SQLITE_OMIT_VIRTUALTABLE
4273 if( pArg->expert.pExpert ){
4274 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
4275 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
4277 #endif
4279 while( zSql[0] && (SQLITE_OK == rc) ){
4280 static const char *zStmtSql;
4281 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
4282 if( SQLITE_OK != rc ){
4283 if( pzErrMsg ){
4284 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
4286 }else{
4287 if( !pStmt ){
4288 /* this happens for a comment or white-space */
4289 zSql = zLeftover;
4290 while( IsSpace(zSql[0]) ) zSql++;
4291 continue;
4293 zStmtSql = sqlite3_sql(pStmt);
4294 if( zStmtSql==0 ) zStmtSql = "";
4295 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
4297 /* save off the prepared statment handle and reset row count */
4298 if( pArg ){
4299 pArg->pStmt = pStmt;
4300 pArg->cnt = 0;
4303 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
4304 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
4305 sqlite3_stmt *pExplain;
4306 char *zEQP;
4307 int triggerEQP = 0;
4308 disable_debug_trace_modes();
4309 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
4310 if( pArg->autoEQP>=AUTOEQP_trigger ){
4311 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
4313 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
4314 shell_check_oom(zEQP);
4315 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
4316 if( rc==SQLITE_OK ){
4317 while( sqlite3_step(pExplain)==SQLITE_ROW ){
4318 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
4319 int iEqpId = sqlite3_column_int(pExplain, 0);
4320 int iParentId = sqlite3_column_int(pExplain, 1);
4321 if( zEQPLine==0 ) zEQPLine = "";
4322 if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
4323 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
4325 eqp_render(pArg, 0);
4327 sqlite3_finalize(pExplain);
4328 sqlite3_free(zEQP);
4329 if( pArg->autoEQP>=AUTOEQP_full ){
4330 /* Also do an EXPLAIN for ".eqp full" mode */
4331 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
4332 shell_check_oom(zEQP);
4333 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
4334 if( rc==SQLITE_OK ){
4335 pArg->cMode = MODE_Explain;
4336 explain_data_prepare(pArg, pExplain);
4337 exec_prepared_stmt(pArg, pExplain);
4338 explain_data_delete(pArg);
4340 sqlite3_finalize(pExplain);
4341 sqlite3_free(zEQP);
4343 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
4344 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
4345 /* Reprepare pStmt before reactiving trace modes */
4346 sqlite3_finalize(pStmt);
4347 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
4348 if( pArg ) pArg->pStmt = pStmt;
4350 restore_debug_trace_modes();
4353 if( pArg ){
4354 pArg->cMode = pArg->mode;
4355 if( pArg->autoExplain ){
4356 if( sqlite3_stmt_isexplain(pStmt)==1 ){
4357 pArg->cMode = MODE_Explain;
4359 if( sqlite3_stmt_isexplain(pStmt)==2 ){
4360 pArg->cMode = MODE_EQP;
4364 /* If the shell is currently in ".explain" mode, gather the extra
4365 ** data required to add indents to the output.*/
4366 if( pArg->cMode==MODE_Explain ){
4367 explain_data_prepare(pArg, pStmt);
4371 bind_prepared_stmt(pArg, pStmt);
4372 exec_prepared_stmt(pArg, pStmt);
4373 explain_data_delete(pArg);
4374 eqp_render(pArg, 0);
4376 /* print usage stats if stats on */
4377 if( pArg && pArg->statsOn ){
4378 display_stats(db, pArg, 0);
4381 /* print loop-counters if required */
4382 if( pArg && pArg->scanstatsOn ){
4383 display_scanstats(db, pArg);
4386 /* Finalize the statement just executed. If this fails, save a
4387 ** copy of the error message. Otherwise, set zSql to point to the
4388 ** next statement to execute. */
4389 rc2 = sqlite3_finalize(pStmt);
4390 if( rc!=SQLITE_NOMEM ) rc = rc2;
4391 if( rc==SQLITE_OK ){
4392 zSql = zLeftover;
4393 while( IsSpace(zSql[0]) ) zSql++;
4394 }else if( pzErrMsg ){
4395 *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
4398 /* clear saved stmt handle */
4399 if( pArg ){
4400 pArg->pStmt = NULL;
4403 } /* end while */
4405 return rc;
4409 ** Release memory previously allocated by tableColumnList().
4411 static void freeColumnList(char **azCol){
4412 int i;
4413 for(i=1; azCol[i]; i++){
4414 sqlite3_free(azCol[i]);
4416 /* azCol[0] is a static string */
4417 sqlite3_free(azCol);
4421 ** Return a list of pointers to strings which are the names of all
4422 ** columns in table zTab. The memory to hold the names is dynamically
4423 ** allocated and must be released by the caller using a subsequent call
4424 ** to freeColumnList().
4426 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
4427 ** value that needs to be preserved, then azCol[0] is filled in with the
4428 ** name of the rowid column.
4430 ** The first regular column in the table is azCol[1]. The list is terminated
4431 ** by an entry with azCol[i]==0.
4433 static char **tableColumnList(ShellState *p, const char *zTab){
4434 char **azCol = 0;
4435 sqlite3_stmt *pStmt;
4436 char *zSql;
4437 int nCol = 0;
4438 int nAlloc = 0;
4439 int nPK = 0; /* Number of PRIMARY KEY columns seen */
4440 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
4441 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4442 int rc;
4444 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4445 shell_check_oom(zSql);
4446 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4447 sqlite3_free(zSql);
4448 if( rc ) return 0;
4449 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4450 if( nCol>=nAlloc-2 ){
4451 nAlloc = nAlloc*2 + nCol + 10;
4452 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4453 shell_check_oom(azCol);
4455 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4456 shell_check_oom(azCol[nCol]);
4457 if( sqlite3_column_int(pStmt, 5) ){
4458 nPK++;
4459 if( nPK==1
4460 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4461 "INTEGER")==0
4463 isIPK = 1;
4464 }else{
4465 isIPK = 0;
4469 sqlite3_finalize(pStmt);
4470 if( azCol==0 ) return 0;
4471 azCol[0] = 0;
4472 azCol[nCol+1] = 0;
4474 /* The decision of whether or not a rowid really needs to be preserved
4475 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
4476 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
4477 ** rowids on tables where the rowid is inaccessible because there are other
4478 ** columns in the table named "rowid", "_rowid_", and "oid".
4480 if( preserveRowid && isIPK ){
4481 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4482 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
4483 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4484 ** ROWID aliases. To distinguish these cases, check to see if
4485 ** there is a "pk" entry in "PRAGMA index_list". There will be
4486 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4488 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4489 " WHERE origin='pk'", zTab);
4490 shell_check_oom(zSql);
4491 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4492 sqlite3_free(zSql);
4493 if( rc ){
4494 freeColumnList(azCol);
4495 return 0;
4497 rc = sqlite3_step(pStmt);
4498 sqlite3_finalize(pStmt);
4499 preserveRowid = rc==SQLITE_ROW;
4501 if( preserveRowid ){
4502 /* Only preserve the rowid if we can find a name to use for the
4503 ** rowid */
4504 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4505 int i, j;
4506 for(j=0; j<3; j++){
4507 for(i=1; i<=nCol; i++){
4508 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4510 if( i>nCol ){
4511 /* At this point, we know that azRowid[j] is not the name of any
4512 ** ordinary column in the table. Verify that azRowid[j] is a valid
4513 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
4514 ** tables will fail this last check */
4515 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4516 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4517 break;
4521 return azCol;
4525 ** Toggle the reverse_unordered_selects setting.
4527 static void toggleSelectOrder(sqlite3 *db){
4528 sqlite3_stmt *pStmt = 0;
4529 int iSetting = 0;
4530 char zStmt[100];
4531 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4532 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4533 iSetting = sqlite3_column_int(pStmt, 0);
4535 sqlite3_finalize(pStmt);
4536 sqlite3_snprintf(sizeof(zStmt), zStmt,
4537 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4538 sqlite3_exec(db, zStmt, 0, 0, 0);
4542 ** This is a different callback routine used for dumping the database.
4543 ** Each row received by this callback consists of a table name,
4544 ** the table type ("index" or "table") and SQL to create the table.
4545 ** This routine should print text sufficient to recreate the table.
4547 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4548 int rc;
4549 const char *zTable;
4550 const char *zType;
4551 const char *zSql;
4552 ShellState *p = (ShellState *)pArg;
4553 int dataOnly;
4554 int noSys;
4556 UNUSED_PARAMETER(azNotUsed);
4557 if( nArg!=3 || azArg==0 ) return 0;
4558 zTable = azArg[0];
4559 zType = azArg[1];
4560 zSql = azArg[2];
4561 if( zTable==0 ) return 0;
4562 if( zType==0 ) return 0;
4563 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4564 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4566 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4567 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
4568 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4569 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
4570 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4571 return 0;
4572 }else if( dataOnly ){
4573 /* no-op */
4574 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4575 char *zIns;
4576 if( !p->writableSchema ){
4577 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
4578 p->writableSchema = 1;
4580 zIns = sqlite3_mprintf(
4581 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4582 "VALUES('table','%q','%q',0,'%q');",
4583 zTable, zTable, zSql);
4584 shell_check_oom(zIns);
4585 utf8_printf(p->out, "%s\n", zIns);
4586 sqlite3_free(zIns);
4587 return 0;
4588 }else{
4589 printSchemaLine(p->out, zSql, ";\n");
4592 if( cli_strcmp(zType, "table")==0 ){
4593 ShellText sSelect;
4594 ShellText sTable;
4595 char **azCol;
4596 int i;
4597 char *savedDestTable;
4598 int savedMode;
4600 azCol = tableColumnList(p, zTable);
4601 if( azCol==0 ){
4602 p->nErr++;
4603 return 0;
4606 /* Always quote the table name, even if it appears to be pure ascii,
4607 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
4608 initText(&sTable);
4609 appendText(&sTable, zTable, quoteChar(zTable));
4610 /* If preserving the rowid, add a column list after the table name.
4611 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4612 ** instead of the usual "INSERT INTO tab VALUES(...)".
4614 if( azCol[0] ){
4615 appendText(&sTable, "(", 0);
4616 appendText(&sTable, azCol[0], 0);
4617 for(i=1; azCol[i]; i++){
4618 appendText(&sTable, ",", 0);
4619 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4621 appendText(&sTable, ")", 0);
4624 /* Build an appropriate SELECT statement */
4625 initText(&sSelect);
4626 appendText(&sSelect, "SELECT ", 0);
4627 if( azCol[0] ){
4628 appendText(&sSelect, azCol[0], 0);
4629 appendText(&sSelect, ",", 0);
4631 for(i=1; azCol[i]; i++){
4632 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4633 if( azCol[i+1] ){
4634 appendText(&sSelect, ",", 0);
4637 freeColumnList(azCol);
4638 appendText(&sSelect, " FROM ", 0);
4639 appendText(&sSelect, zTable, quoteChar(zTable));
4641 savedDestTable = p->zDestTable;
4642 savedMode = p->mode;
4643 p->zDestTable = sTable.z;
4644 p->mode = p->cMode = MODE_Insert;
4645 rc = shell_exec(p, sSelect.z, 0);
4646 if( (rc&0xff)==SQLITE_CORRUPT ){
4647 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4648 toggleSelectOrder(p->db);
4649 shell_exec(p, sSelect.z, 0);
4650 toggleSelectOrder(p->db);
4652 p->zDestTable = savedDestTable;
4653 p->mode = savedMode;
4654 freeText(&sTable);
4655 freeText(&sSelect);
4656 if( rc ) p->nErr++;
4658 return 0;
4662 ** Run zQuery. Use dump_callback() as the callback routine so that
4663 ** the contents of the query are output as SQL statements.
4665 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
4666 ** "ORDER BY rowid DESC" to the end.
4668 static int run_schema_dump_query(
4669 ShellState *p,
4670 const char *zQuery
4672 int rc;
4673 char *zErr = 0;
4674 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4675 if( rc==SQLITE_CORRUPT ){
4676 char *zQ2;
4677 int len = strlen30(zQuery);
4678 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
4679 if( zErr ){
4680 utf8_printf(p->out, "/****** %s ******/\n", zErr);
4681 sqlite3_free(zErr);
4682 zErr = 0;
4684 zQ2 = malloc( len+100 );
4685 if( zQ2==0 ) return rc;
4686 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4687 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4688 if( rc ){
4689 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
4690 }else{
4691 rc = SQLITE_CORRUPT;
4693 sqlite3_free(zErr);
4694 free(zQ2);
4696 return rc;
4700 ** Text of help messages.
4702 ** The help text for each individual command begins with a line that starts
4703 ** with ".". Subsequent lines are supplemental information.
4705 ** There must be two or more spaces between the end of the command and the
4706 ** start of the description of what that command does.
4708 static const char *(azHelp[]) = {
4709 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4710 && !defined(SQLITE_SHELL_FIDDLE)
4711 ".archive ... Manage SQL archives",
4712 " Each command must have exactly one of the following options:",
4713 " -c, --create Create a new archive",
4714 " -u, --update Add or update files with changed mtime",
4715 " -i, --insert Like -u but always add even if unchanged",
4716 " -r, --remove Remove files from archive",
4717 " -t, --list List contents of archive",
4718 " -x, --extract Extract files from archive",
4719 " Optional arguments:",
4720 " -v, --verbose Print each filename as it is processed",
4721 " -f FILE, --file FILE Use archive FILE (default is current db)",
4722 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
4723 " -C DIR, --directory DIR Read/extract files from directory DIR",
4724 " -g, --glob Use glob matching for names in archive",
4725 " -n, --dryrun Show the SQL that would have occurred",
4726 " Examples:",
4727 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
4728 " .ar -tf ARCHIVE # List members of ARCHIVE",
4729 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
4730 " See also:",
4731 " http://sqlite.org/cli.html#sqlite_archive_support",
4732 #endif
4733 #ifndef SQLITE_OMIT_AUTHORIZATION
4734 ".auth ON|OFF Show authorizer callbacks",
4735 #endif
4736 #ifndef SQLITE_SHELL_FIDDLE
4737 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
4738 " Options:",
4739 " --append Use the appendvfs",
4740 " --async Write to FILE without journal and fsync()",
4741 #endif
4742 ".bail on|off Stop after hitting an error. Default OFF",
4743 ".binary on|off Turn binary output on or off. Default OFF",
4744 #ifndef SQLITE_SHELL_FIDDLE
4745 ".cd DIRECTORY Change the working directory to DIRECTORY",
4746 #endif
4747 ".changes on|off Show number of rows changed by SQL",
4748 #ifndef SQLITE_SHELL_FIDDLE
4749 ".check GLOB Fail if output since .testcase does not match",
4750 ".clone NEWDB Clone data into NEWDB from the existing database",
4751 #endif
4752 ".connection [close] [#] Open or close an auxiliary database connection",
4753 ".databases List names and files of attached databases",
4754 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
4755 #if SQLITE_SHELL_HAVE_RECOVER
4756 ".dbinfo ?DB? Show status information about the database",
4757 #endif
4758 ".dump ?OBJECTS? Render database content as SQL",
4759 " Options:",
4760 " --data-only Output only INSERT statements",
4761 " --newlines Allow unescaped newline characters in output",
4762 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
4763 " --preserve-rowids Include ROWID values in the output",
4764 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4765 " Additional LIKE patterns can be given in subsequent arguments",
4766 ".echo on|off Turn command echo on or off",
4767 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
4768 " Other Modes:",
4769 #ifdef SQLITE_DEBUG
4770 " test Show raw EXPLAIN QUERY PLAN output",
4771 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4772 #endif
4773 " trigger Like \"full\" but also show trigger bytecode",
4774 #ifndef SQLITE_SHELL_FIDDLE
4775 ".excel Display the output of next command in spreadsheet",
4776 " --bom Put a UTF8 byte-order mark on intermediate file",
4777 #endif
4778 #ifndef SQLITE_SHELL_FIDDLE
4779 ".exit ?CODE? Exit this program with return-code CODE",
4780 #endif
4781 ".expert EXPERIMENTAL. Suggest indexes for queries",
4782 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
4783 ".filectrl CMD ... Run various sqlite3_file_control() operations",
4784 " --schema SCHEMA Use SCHEMA instead of \"main\"",
4785 " --help Show CMD details",
4786 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
4787 ".headers on|off Turn display of headers on or off",
4788 ".help ?-all? ?PATTERN? Show help text for PATTERN",
4789 #ifndef SQLITE_SHELL_FIDDLE
4790 ".import FILE TABLE Import data from FILE into TABLE",
4791 " Options:",
4792 " --ascii Use \\037 and \\036 as column and row separators",
4793 " --csv Use , and \\n as column and row separators",
4794 " --skip N Skip the first N rows of input",
4795 " --schema S Target table to be S.TABLE",
4796 " -v \"Verbose\" - increase auxiliary output",
4797 " Notes:",
4798 " * If TABLE does not exist, it is created. The first row of input",
4799 " determines the column names.",
4800 " * If neither --csv or --ascii are used, the input mode is derived",
4801 " from the \".mode\" output mode",
4802 " * If FILE begins with \"|\" then it is a command that generates the",
4803 " input text.",
4804 #endif
4805 #ifndef SQLITE_OMIT_TEST_CONTROL
4806 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
4807 #endif
4808 ".indexes ?TABLE? Show names of indexes",
4809 " If TABLE is specified, only show indexes for",
4810 " tables matching TABLE using the LIKE operator.",
4811 #ifdef SQLITE_ENABLE_IOTRACE
4812 ",iotrace FILE Enable I/O diagnostic logging to FILE",
4813 #endif
4814 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
4815 ".lint OPTIONS Report potential schema issues.",
4816 " Options:",
4817 " fkey-indexes Find missing foreign key indexes",
4818 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4819 ".load FILE ?ENTRY? Load an extension library",
4820 #endif
4821 #if !defined(SQLITE_SHELL_FIDDLE)
4822 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
4823 #else
4824 ".log on|off Turn logging on or off.",
4825 #endif
4826 ".mode MODE ?OPTIONS? Set output mode",
4827 " MODE is one of:",
4828 " ascii Columns/rows delimited by 0x1F and 0x1E",
4829 " box Tables using unicode box-drawing characters",
4830 " csv Comma-separated values",
4831 " column Output in columns. (See .width)",
4832 " html HTML <table> code",
4833 " insert SQL insert statements for TABLE",
4834 " json Results in a JSON array",
4835 " line One value per line",
4836 " list Values delimited by \"|\"",
4837 " markdown Markdown table format",
4838 " qbox Shorthand for \"box --wrap 60 --quote\"",
4839 " quote Escape answers as for SQL",
4840 " table ASCII-art table",
4841 " tabs Tab-separated values",
4842 " tcl TCL list elements",
4843 " OPTIONS: (for columnar modes or insert mode):",
4844 " --wrap N Wrap output lines to no longer than N characters",
4845 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
4846 " --ww Shorthand for \"--wordwrap 1\"",
4847 " --quote Quote output text as SQL literals",
4848 " --noquote Do not quote output text",
4849 " TABLE The name of SQL table used for \"insert\" mode",
4850 #ifndef SQLITE_SHELL_FIDDLE
4851 ".nonce STRING Suspend safe mode for one command if nonce matches",
4852 #endif
4853 ".nullvalue STRING Use STRING in place of NULL values",
4854 #ifndef SQLITE_SHELL_FIDDLE
4855 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
4856 " If FILE begins with '|' then open as a pipe",
4857 " --bom Put a UTF8 byte-order mark at the beginning",
4858 " -e Send output to the system text editor",
4859 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
4860 /* Note that .open is (partially) available in WASM builds but is
4861 ** currently only intended to be used by the fiddle tool, not
4862 ** end users, so is "undocumented." */
4863 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
4864 " Options:",
4865 " --append Use appendvfs to append database to the end of FILE",
4866 #endif
4867 #ifndef SQLITE_OMIT_DESERIALIZE
4868 " --deserialize Load into memory using sqlite3_deserialize()",
4869 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
4870 " --maxsize N Maximum size for --hexdb or --deserialized database",
4871 #endif
4872 " --new Initialize FILE to an empty database",
4873 " --nofollow Do not follow symbolic links",
4874 " --readonly Open FILE readonly",
4875 " --zip FILE is a ZIP archive",
4876 #ifndef SQLITE_SHELL_FIDDLE
4877 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
4878 " If FILE begins with '|' then open it as a pipe.",
4879 " Options:",
4880 " --bom Prefix output with a UTF8 byte-order mark",
4881 " -e Send output to the system text editor",
4882 " -x Send output as CSV to a spreadsheet",
4883 #endif
4884 ".parameter CMD ... Manage SQL parameter bindings",
4885 " clear Erase all bindings",
4886 " init Initialize the TEMP table that holds bindings",
4887 " list List the current parameter bindings",
4888 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
4889 " PARAMETER should start with one of: $ : @ ?",
4890 " unset PARAMETER Remove PARAMETER from the binding table",
4891 ".print STRING... Print literal STRING",
4892 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4893 ".progress N Invoke progress handler after every N opcodes",
4894 " --limit N Interrupt after N progress callbacks",
4895 " --once Do no more than one progress interrupt",
4896 " --quiet|-q No output except at interrupts",
4897 " --reset Reset the count for each input and interrupt",
4898 #endif
4899 ".prompt MAIN CONTINUE Replace the standard prompts",
4900 #ifndef SQLITE_SHELL_FIDDLE
4901 ".quit Stop interpreting input stream, exit if primary.",
4902 ".read FILE Read input from FILE or command output",
4903 " If FILE begins with \"|\", it is a command that generates the input.",
4904 #endif
4905 #if SQLITE_SHELL_HAVE_RECOVER
4906 ".recover Recover as much data as possible from corrupt db.",
4907 " --ignore-freelist Ignore pages that appear to be on db freelist",
4908 " --lost-and-found TABLE Alternative name for the lost-and-found table",
4909 " --no-rowids Do not attempt to recover rowid values",
4910 " that are not also INTEGER PRIMARY KEYs",
4911 #endif
4912 #ifndef SQLITE_SHELL_FIDDLE
4913 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
4914 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
4915 #endif
4916 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
4917 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
4918 " Options:",
4919 " --indent Try to pretty-print the schema",
4920 " --nosys Omit objects whose names start with \"sqlite_\"",
4921 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
4922 " Options:",
4923 " --init Create a new SELFTEST table",
4924 " -v Verbose output",
4925 ".separator COL ?ROW? Change the column and row separators",
4926 #if defined(SQLITE_ENABLE_SESSION)
4927 ".session ?NAME? CMD ... Create or control sessions",
4928 " Subcommands:",
4929 " attach TABLE Attach TABLE",
4930 " changeset FILE Write a changeset into FILE",
4931 " close Close one session",
4932 " enable ?BOOLEAN? Set or query the enable bit",
4933 " filter GLOB... Reject tables matching GLOBs",
4934 " indirect ?BOOLEAN? Mark or query the indirect status",
4935 " isempty Query whether the session is empty",
4936 " list List currently open session names",
4937 " open DB NAME Open a new session on DB",
4938 " patchset FILE Write a patchset into FILE",
4939 " If ?NAME? is omitted, the first defined session is used.",
4940 #endif
4941 ".sha3sum ... Compute a SHA3 hash of database content",
4942 " Options:",
4943 " --schema Also hash the sqlite_schema table",
4944 " --sha3-224 Use the sha3-224 algorithm",
4945 " --sha3-256 Use the sha3-256 algorithm (default)",
4946 " --sha3-384 Use the sha3-384 algorithm",
4947 " --sha3-512 Use the sha3-512 algorithm",
4948 " Any other argument is a LIKE pattern for tables to hash",
4949 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4950 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
4951 #endif
4952 ".show Show the current values for various settings",
4953 ".stats ?ARG? Show stats or turn stats on or off",
4954 " off Turn off automatic stat display",
4955 " on Turn on automatic stat display",
4956 " stmt Show statement stats",
4957 " vmstep Show the virtual machine step count only",
4958 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4959 ".system CMD ARGS... Run CMD ARGS... in a system shell",
4960 #endif
4961 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
4962 #ifndef SQLITE_SHELL_FIDDLE
4963 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
4964 #endif
4965 ",testctrl CMD ... Run various sqlite3_test_control() operations",
4966 " Run \".testctrl\" with no arguments for details",
4967 ".timeout MS Try opening locked tables for MS milliseconds",
4968 ".timer on|off Turn SQL timer on or off",
4969 #ifndef SQLITE_OMIT_TRACE
4970 ".trace ?OPTIONS? Output each SQL statement as it is run",
4971 " FILE Send output to FILE",
4972 " stdout Send output to stdout",
4973 " stderr Send output to stderr",
4974 " off Disable tracing",
4975 " --expanded Expand query parameters",
4976 #ifdef SQLITE_ENABLE_NORMALIZE
4977 " --normalized Normal the SQL statements",
4978 #endif
4979 " --plain Show SQL as it is input",
4980 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
4981 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
4982 " --row Trace each row (SQLITE_TRACE_ROW)",
4983 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
4984 #endif /* SQLITE_OMIT_TRACE */
4985 #ifdef SQLITE_DEBUG
4986 ".unmodule NAME ... Unregister virtual table modules",
4987 " --allexcept Unregister everything except those named",
4988 #endif
4989 ".version Show source, library and compiler versions",
4990 ".vfsinfo ?AUX? Information about the top-level VFS",
4991 ".vfslist List all available VFSes",
4992 ".vfsname ?AUX? Print the name of the VFS stack",
4993 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
4994 " Negative values right-justify",
4998 ** Output help text.
5000 ** zPattern describes the set of commands for which help text is provided.
5001 ** If zPattern is NULL, then show all commands, but only give a one-line
5002 ** description of each.
5004 ** Return the number of matches.
5006 static int showHelp(FILE *out, const char *zPattern){
5007 int i = 0;
5008 int j = 0;
5009 int n = 0;
5010 char *zPat;
5011 if( zPattern==0
5012 || zPattern[0]=='0'
5013 || cli_strcmp(zPattern,"-a")==0
5014 || cli_strcmp(zPattern,"-all")==0
5015 || cli_strcmp(zPattern,"--all")==0
5017 enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
5018 enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
5019 /* Show all or most commands
5020 ** *zPattern==0 => summary of documented commands only
5021 ** *zPattern=='0' => whole help for undocumented commands
5022 ** Otherwise => whole help for documented commands
5024 enum HelpWanted hw = HW_SummaryOnly;
5025 enum HelpHave hh = HH_More;
5026 if( zPattern!=0 ){
5027 hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
5029 for(i=0; i<ArraySize(azHelp); i++){
5030 switch( azHelp[i][0] ){
5031 case ',':
5032 hh = HH_Summary|HH_Undoc;
5033 break;
5034 case '.':
5035 hh = HH_Summary;
5036 break;
5037 default:
5038 hh &= ~HH_Summary;
5039 break;
5041 if( ((hw^hh)&HH_Undoc)==0 ){
5042 if( (hh&HH_Summary)!=0 ){
5043 utf8_printf(out, ".%s\n", azHelp[i]+1);
5044 ++n;
5045 }else if( (hw&HW_SummaryOnly)==0 ){
5046 utf8_printf(out, "%s\n", azHelp[i]);
5050 }else{
5051 /* Seek documented commands for which zPattern is an exact prefix */
5052 zPat = sqlite3_mprintf(".%s*", zPattern);
5053 shell_check_oom(zPat);
5054 for(i=0; i<ArraySize(azHelp); i++){
5055 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
5056 utf8_printf(out, "%s\n", azHelp[i]);
5057 j = i+1;
5058 n++;
5061 sqlite3_free(zPat);
5062 if( n ){
5063 if( n==1 ){
5064 /* when zPattern is a prefix of exactly one command, then include
5065 ** the details of that command, which should begin at offset j */
5066 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
5067 utf8_printf(out, "%s\n", azHelp[j]);
5068 j++;
5071 return n;
5073 /* Look for documented commands that contain zPattern anywhere.
5074 ** Show complete text of all documented commands that match. */
5075 zPat = sqlite3_mprintf("%%%s%%", zPattern);
5076 shell_check_oom(zPat);
5077 for(i=0; i<ArraySize(azHelp); i++){
5078 if( azHelp[i][0]==',' ){
5079 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
5080 continue;
5082 if( azHelp[i][0]=='.' ) j = i;
5083 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
5084 utf8_printf(out, "%s\n", azHelp[j]);
5085 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
5086 j++;
5087 utf8_printf(out, "%s\n", azHelp[j]);
5089 i = j;
5090 n++;
5093 sqlite3_free(zPat);
5095 return n;
5098 /* Forward reference */
5099 static int process_input(ShellState *p);
5102 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
5103 ** and return a pointer to the buffer. The caller is responsible for freeing
5104 ** the memory.
5106 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
5107 ** read.
5109 ** For convenience, a nul-terminator byte is always appended to the data read
5110 ** from the file before the buffer is returned. This byte is not included in
5111 ** the final value of (*pnByte), if applicable.
5113 ** NULL is returned if any error is encountered. The final value of *pnByte
5114 ** is undefined in this case.
5116 static char *readFile(const char *zName, int *pnByte){
5117 FILE *in = fopen(zName, "rb");
5118 long nIn;
5119 size_t nRead;
5120 char *pBuf;
5121 int rc;
5122 if( in==0 ) return 0;
5123 rc = fseek(in, 0, SEEK_END);
5124 if( rc!=0 ){
5125 raw_printf(stderr, "Error: '%s' not seekable\n", zName);
5126 fclose(in);
5127 return 0;
5129 nIn = ftell(in);
5130 rewind(in);
5131 pBuf = sqlite3_malloc64( nIn+1 );
5132 if( pBuf==0 ){
5133 raw_printf(stderr, "Error: out of memory\n");
5134 fclose(in);
5135 return 0;
5137 nRead = fread(pBuf, nIn, 1, in);
5138 fclose(in);
5139 if( nRead!=1 ){
5140 sqlite3_free(pBuf);
5141 raw_printf(stderr, "Error: cannot read '%s'\n", zName);
5142 return 0;
5144 pBuf[nIn] = 0;
5145 if( pnByte ) *pnByte = nIn;
5146 return pBuf;
5149 #if defined(SQLITE_ENABLE_SESSION)
5151 ** Close a single OpenSession object and release all of its associated
5152 ** resources.
5154 static void session_close(OpenSession *pSession){
5155 int i;
5156 sqlite3session_delete(pSession->p);
5157 sqlite3_free(pSession->zName);
5158 for(i=0; i<pSession->nFilter; i++){
5159 sqlite3_free(pSession->azFilter[i]);
5161 sqlite3_free(pSession->azFilter);
5162 memset(pSession, 0, sizeof(OpenSession));
5164 #endif
5167 ** Close all OpenSession objects and release all associated resources.
5169 #if defined(SQLITE_ENABLE_SESSION)
5170 static void session_close_all(ShellState *p, int i){
5171 int j;
5172 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
5173 for(j=0; j<pAuxDb->nSession; j++){
5174 session_close(&pAuxDb->aSession[j]);
5176 pAuxDb->nSession = 0;
5178 #else
5179 # define session_close_all(X,Y)
5180 #endif
5183 ** Implementation of the xFilter function for an open session. Omit
5184 ** any tables named by ".session filter" but let all other table through.
5186 #if defined(SQLITE_ENABLE_SESSION)
5187 static int session_filter(void *pCtx, const char *zTab){
5188 OpenSession *pSession = (OpenSession*)pCtx;
5189 int i;
5190 for(i=0; i<pSession->nFilter; i++){
5191 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
5193 return 1;
5195 #endif
5198 ** Try to deduce the type of file for zName based on its content. Return
5199 ** one of the SHELL_OPEN_* constants.
5201 ** If the file does not exist or is empty but its name looks like a ZIP
5202 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
5203 ** Otherwise, assume an ordinary database regardless of the filename if
5204 ** the type cannot be determined from content.
5206 int deduceDatabaseType(const char *zName, int dfltZip){
5207 FILE *f = fopen(zName, "rb");
5208 size_t n;
5209 int rc = SHELL_OPEN_UNSPEC;
5210 char zBuf[100];
5211 if( f==0 ){
5212 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5213 return SHELL_OPEN_ZIPFILE;
5214 }else{
5215 return SHELL_OPEN_NORMAL;
5218 n = fread(zBuf, 16, 1, f);
5219 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
5220 fclose(f);
5221 return SHELL_OPEN_NORMAL;
5223 fseek(f, -25, SEEK_END);
5224 n = fread(zBuf, 25, 1, f);
5225 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
5226 rc = SHELL_OPEN_APPENDVFS;
5227 }else{
5228 fseek(f, -22, SEEK_END);
5229 n = fread(zBuf, 22, 1, f);
5230 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
5231 && zBuf[3]==0x06 ){
5232 rc = SHELL_OPEN_ZIPFILE;
5233 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5234 rc = SHELL_OPEN_ZIPFILE;
5237 fclose(f);
5238 return rc;
5241 #ifndef SQLITE_OMIT_DESERIALIZE
5243 ** Reconstruct an in-memory database using the output from the "dbtotxt"
5244 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
5245 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
5247 static unsigned char *readHexDb(ShellState *p, int *pnData){
5248 unsigned char *a = 0;
5249 int nLine;
5250 int n = 0;
5251 int pgsz = 0;
5252 int iOffset = 0;
5253 int j, k;
5254 int rc;
5255 FILE *in;
5256 const char *zDbFilename = p->pAuxDb->zDbFilename;
5257 unsigned int x[16];
5258 char zLine[1000];
5259 if( zDbFilename ){
5260 in = fopen(zDbFilename, "r");
5261 if( in==0 ){
5262 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
5263 return 0;
5265 nLine = 0;
5266 }else{
5267 in = p->in;
5268 nLine = p->lineno;
5269 if( in==0 ) in = stdin;
5271 *pnData = 0;
5272 nLine++;
5273 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
5274 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
5275 if( rc!=2 ) goto readHexDb_error;
5276 if( n<0 ) goto readHexDb_error;
5277 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
5278 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
5279 a = sqlite3_malloc( n ? n : 1 );
5280 shell_check_oom(a);
5281 memset(a, 0, n);
5282 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
5283 utf8_printf(stderr, "invalid pagesize\n");
5284 goto readHexDb_error;
5286 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
5287 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
5288 if( rc==2 ){
5289 iOffset = k;
5290 continue;
5292 if( cli_strncmp(zLine, "| end ", 6)==0 ){
5293 break;
5295 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5296 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
5297 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
5298 if( rc==17 ){
5299 k = iOffset+j;
5300 if( k+16<=n && k>=0 ){
5301 int ii;
5302 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
5306 *pnData = n;
5307 if( in!=p->in ){
5308 fclose(in);
5309 }else{
5310 p->lineno = nLine;
5312 return a;
5314 readHexDb_error:
5315 if( in!=p->in ){
5316 fclose(in);
5317 }else{
5318 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
5319 nLine++;
5320 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
5322 p->lineno = nLine;
5324 sqlite3_free(a);
5325 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
5326 return 0;
5328 #endif /* SQLITE_OMIT_DESERIALIZE */
5331 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
5333 static void shellUSleepFunc(
5334 sqlite3_context *context,
5335 int argcUnused,
5336 sqlite3_value **argv
5338 int sleep = sqlite3_value_int(argv[0]);
5339 (void)argcUnused;
5340 sqlite3_sleep(sleep/1000);
5341 sqlite3_result_int(context, sleep);
5344 /* Flags for open_db().
5346 ** The default behavior of open_db() is to exit(1) if the database fails to
5347 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5348 ** but still returns without calling exit.
5350 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5351 ** ZIP archive if the file does not exist or is empty and its name matches
5352 ** the *.zip pattern.
5354 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
5355 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
5358 ** Make sure the database is open. If it is not, then open it. If
5359 ** the database fails to open, print an error message and exit.
5361 static void open_db(ShellState *p, int openFlags){
5362 if( p->db==0 ){
5363 const char *zDbFilename = p->pAuxDb->zDbFilename;
5364 if( p->openMode==SHELL_OPEN_UNSPEC ){
5365 if( zDbFilename==0 || zDbFilename[0]==0 ){
5366 p->openMode = SHELL_OPEN_NORMAL;
5367 }else{
5368 p->openMode = (u8)deduceDatabaseType(zDbFilename,
5369 (openFlags & OPEN_DB_ZIPFILE)!=0);
5372 switch( p->openMode ){
5373 case SHELL_OPEN_APPENDVFS: {
5374 sqlite3_open_v2(zDbFilename, &p->db,
5375 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5376 break;
5378 case SHELL_OPEN_HEXDB:
5379 case SHELL_OPEN_DESERIALIZE: {
5380 sqlite3_open(0, &p->db);
5381 break;
5383 case SHELL_OPEN_ZIPFILE: {
5384 sqlite3_open(":memory:", &p->db);
5385 break;
5387 case SHELL_OPEN_READONLY: {
5388 sqlite3_open_v2(zDbFilename, &p->db,
5389 SQLITE_OPEN_READONLY|p->openFlags, 0);
5390 break;
5392 case SHELL_OPEN_UNSPEC:
5393 case SHELL_OPEN_NORMAL: {
5394 sqlite3_open_v2(zDbFilename, &p->db,
5395 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5396 break;
5399 globalDb = p->db;
5400 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5401 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
5402 zDbFilename, sqlite3_errmsg(p->db));
5403 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
5404 exit(1);
5406 sqlite3_close(p->db);
5407 sqlite3_open(":memory:", &p->db);
5408 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5409 utf8_printf(stderr,
5410 "Also: unable to open substitute in-memory database.\n"
5412 exit(1);
5413 }else{
5414 utf8_printf(stderr,
5415 "Notice: using substitute in-memory database instead of \"%s\"\n",
5416 zDbFilename);
5419 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
5421 /* Reflect the use or absence of --unsafe-testing invocation. */
5423 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
5424 sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
5425 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
5428 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5429 sqlite3_enable_load_extension(p->db, 1);
5430 #endif
5431 sqlite3_shathree_init(p->db, 0, 0);
5432 sqlite3_uint_init(p->db, 0, 0);
5433 sqlite3_decimal_init(p->db, 0, 0);
5434 sqlite3_base64_init(p->db, 0, 0);
5435 sqlite3_base85_init(p->db, 0, 0);
5436 sqlite3_regexp_init(p->db, 0, 0);
5437 sqlite3_ieee_init(p->db, 0, 0);
5438 sqlite3_series_init(p->db, 0, 0);
5439 #ifndef SQLITE_SHELL_FIDDLE
5440 sqlite3_fileio_init(p->db, 0, 0);
5441 sqlite3_completion_init(p->db, 0, 0);
5442 #endif
5443 #ifdef SQLITE_HAVE_ZLIB
5444 if( !p->bSafeModePersist ){
5445 sqlite3_zipfile_init(p->db, 0, 0);
5446 sqlite3_sqlar_init(p->db, 0, 0);
5448 #endif
5449 #ifdef SQLITE_SHELL_EXTFUNCS
5450 /* Create a preprocessing mechanism for extensions to make
5451 * their own provisions for being built into the shell.
5452 * This is a short-span macro. See further below for usage.
5454 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
5455 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
5456 /* Let custom-included extensions get their ..._init() called.
5457 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
5458 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
5459 * inititialization routine to be called.
5462 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
5463 /* Let custom-included extensions expose their functionality.
5464 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
5465 * the SQL functions, virtual tables, collating sequences or
5466 * VFS's implemented by the extension to be registered.
5468 if( irc==SQLITE_OK
5469 || irc==SQLITE_OK_LOAD_PERMANENTLY ){
5470 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
5472 #undef SHELL_SUB_MACRO
5473 #undef SHELL_SUBMACRO
5475 #endif
5477 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5478 shellAddSchemaName, 0, 0);
5479 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5480 shellModuleSchema, 0, 0);
5481 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5482 shellPutsFunc, 0, 0);
5483 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5484 shellUSleepFunc, 0, 0);
5485 #ifndef SQLITE_NOHAVE_SYSTEM
5486 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5487 editFunc, 0, 0);
5488 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5489 editFunc, 0, 0);
5490 #endif
5492 if( p->openMode==SHELL_OPEN_ZIPFILE ){
5493 char *zSql = sqlite3_mprintf(
5494 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5495 shell_check_oom(zSql);
5496 sqlite3_exec(p->db, zSql, 0, 0, 0);
5497 sqlite3_free(zSql);
5499 #ifndef SQLITE_OMIT_DESERIALIZE
5500 else
5501 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5502 int rc;
5503 int nData = 0;
5504 unsigned char *aData;
5505 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5506 aData = (unsigned char*)readFile(zDbFilename, &nData);
5507 }else{
5508 aData = readHexDb(p, &nData);
5510 if( aData==0 ){
5511 return;
5513 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5514 SQLITE_DESERIALIZE_RESIZEABLE |
5515 SQLITE_DESERIALIZE_FREEONCLOSE);
5516 if( rc ){
5517 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
5519 if( p->szMax>0 ){
5520 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5523 #endif
5525 if( p->db!=0 ){
5526 if( p->bSafeModePersist ){
5527 sqlite3_set_authorizer(p->db, safeModeAuth, p);
5529 sqlite3_db_config(
5530 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
5536 ** Attempt to close the databaes connection. Report errors.
5538 void close_db(sqlite3 *db){
5539 int rc = sqlite3_close(db);
5540 if( rc ){
5541 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
5542 rc, sqlite3_errmsg(db));
5546 #if HAVE_READLINE || HAVE_EDITLINE
5548 ** Readline completion callbacks
5550 static char *readline_completion_generator(const char *text, int state){
5551 static sqlite3_stmt *pStmt = 0;
5552 char *zRet;
5553 if( state==0 ){
5554 char *zSql;
5555 sqlite3_finalize(pStmt);
5556 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5557 " FROM completion(%Q) ORDER BY 1", text);
5558 shell_check_oom(zSql);
5559 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5560 sqlite3_free(zSql);
5562 if( sqlite3_step(pStmt)==SQLITE_ROW ){
5563 const char *z = (const char*)sqlite3_column_text(pStmt,0);
5564 zRet = z ? strdup(z) : 0;
5565 }else{
5566 sqlite3_finalize(pStmt);
5567 pStmt = 0;
5568 zRet = 0;
5570 return zRet;
5572 static char **readline_completion(const char *zText, int iStart, int iEnd){
5573 (void)iStart;
5574 (void)iEnd;
5575 rl_attempted_completion_over = 1;
5576 return rl_completion_matches(zText, readline_completion_generator);
5579 #elif HAVE_LINENOISE
5581 ** Linenoise completion callback
5583 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
5584 i64 nLine = strlen(zLine);
5585 i64 i, iStart;
5586 sqlite3_stmt *pStmt = 0;
5587 char *zSql;
5588 char zBuf[1000];
5590 if( nLine>(i64)sizeof(zBuf)-30 ) return;
5591 if( zLine[0]=='.' || zLine[0]=='#') return;
5592 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5593 if( i==nLine-1 ) return;
5594 iStart = i+1;
5595 memcpy(zBuf, zLine, iStart);
5596 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5597 " FROM completion(%Q,%Q) ORDER BY 1",
5598 &zLine[iStart], zLine);
5599 shell_check_oom(zSql);
5600 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5601 sqlite3_free(zSql);
5602 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5603 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5604 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5605 int nCompletion = sqlite3_column_bytes(pStmt, 0);
5606 if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
5607 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5608 linenoiseAddCompletion(lc, zBuf);
5611 sqlite3_finalize(pStmt);
5613 #endif
5616 ** Do C-language style dequoting.
5618 ** \a -> alarm
5619 ** \b -> backspace
5620 ** \t -> tab
5621 ** \n -> newline
5622 ** \v -> vertical tab
5623 ** \f -> form feed
5624 ** \r -> carriage return
5625 ** \s -> space
5626 ** \" -> "
5627 ** \' -> '
5628 ** \\ -> backslash
5629 ** \NNN -> ascii character NNN in octal
5630 ** \xHH -> ascii character HH in hexadecimal
5632 static void resolve_backslashes(char *z){
5633 int i, j;
5634 char c;
5635 while( *z && *z!='\\' ) z++;
5636 for(i=j=0; (c = z[i])!=0; i++, j++){
5637 if( c=='\\' && z[i+1]!=0 ){
5638 c = z[++i];
5639 if( c=='a' ){
5640 c = '\a';
5641 }else if( c=='b' ){
5642 c = '\b';
5643 }else if( c=='t' ){
5644 c = '\t';
5645 }else if( c=='n' ){
5646 c = '\n';
5647 }else if( c=='v' ){
5648 c = '\v';
5649 }else if( c=='f' ){
5650 c = '\f';
5651 }else if( c=='r' ){
5652 c = '\r';
5653 }else if( c=='"' ){
5654 c = '"';
5655 }else if( c=='\'' ){
5656 c = '\'';
5657 }else if( c=='\\' ){
5658 c = '\\';
5659 }else if( c=='x' ){
5660 int nhd = 0, hdv;
5661 u8 hv = 0;
5662 while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
5663 hv = (u8)((hv<<4)|hdv);
5664 ++nhd;
5666 i += nhd;
5667 c = (u8)hv;
5668 }else if( c>='0' && c<='7' ){
5669 c -= '0';
5670 if( z[i+1]>='0' && z[i+1]<='7' ){
5671 i++;
5672 c = (c<<3) + z[i] - '0';
5673 if( z[i+1]>='0' && z[i+1]<='7' ){
5674 i++;
5675 c = (c<<3) + z[i] - '0';
5680 z[j] = c;
5682 if( j<i ) z[j] = 0;
5686 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
5687 ** for TRUE and FALSE. Return the integer value if appropriate.
5689 static int booleanValue(const char *zArg){
5690 int i;
5691 if( zArg[0]=='0' && zArg[1]=='x' ){
5692 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5693 }else{
5694 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5696 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5697 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5698 return 1;
5700 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5701 return 0;
5703 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5704 zArg);
5705 return 0;
5709 ** Set or clear a shell flag according to a boolean value.
5711 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5712 if( booleanValue(zArg) ){
5713 ShellSetFlag(p, mFlag);
5714 }else{
5715 ShellClearFlag(p, mFlag);
5720 ** Close an output file, assuming it is not stderr or stdout
5722 static void output_file_close(FILE *f){
5723 if( f && f!=stdout && f!=stderr ) fclose(f);
5727 ** Try to open an output file. The names "stdout" and "stderr" are
5728 ** recognized and do the right thing. NULL is returned if the output
5729 ** filename is "off".
5731 static FILE *output_file_open(const char *zFile, int bTextMode){
5732 FILE *f;
5733 if( cli_strcmp(zFile,"stdout")==0 ){
5734 f = stdout;
5735 }else if( cli_strcmp(zFile, "stderr")==0 ){
5736 f = stderr;
5737 }else if( cli_strcmp(zFile, "off")==0 ){
5738 f = 0;
5739 }else{
5740 f = fopen(zFile, bTextMode ? "w" : "wb");
5741 if( f==0 ){
5742 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
5745 return f;
5748 #ifndef SQLITE_OMIT_TRACE
5750 ** A routine for handling output from sqlite3_trace().
5752 static int sql_trace_callback(
5753 unsigned mType, /* The trace type */
5754 void *pArg, /* The ShellState pointer */
5755 void *pP, /* Usually a pointer to sqlite_stmt */
5756 void *pX /* Auxiliary output */
5758 ShellState *p = (ShellState*)pArg;
5759 sqlite3_stmt *pStmt;
5760 const char *zSql;
5761 i64 nSql;
5762 if( p->traceOut==0 ) return 0;
5763 if( mType==SQLITE_TRACE_CLOSE ){
5764 utf8_printf(p->traceOut, "-- closing database connection\n");
5765 return 0;
5767 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
5768 zSql = (const char*)pX;
5769 }else{
5770 pStmt = (sqlite3_stmt*)pP;
5771 switch( p->eTraceType ){
5772 case SHELL_TRACE_EXPANDED: {
5773 zSql = sqlite3_expanded_sql(pStmt);
5774 break;
5776 #ifdef SQLITE_ENABLE_NORMALIZE
5777 case SHELL_TRACE_NORMALIZED: {
5778 zSql = sqlite3_normalized_sql(pStmt);
5779 break;
5781 #endif
5782 default: {
5783 zSql = sqlite3_sql(pStmt);
5784 break;
5788 if( zSql==0 ) return 0;
5789 nSql = strlen(zSql);
5790 if( nSql>1000000000 ) nSql = 1000000000;
5791 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5792 switch( mType ){
5793 case SQLITE_TRACE_ROW:
5794 case SQLITE_TRACE_STMT: {
5795 utf8_printf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5796 break;
5798 case SQLITE_TRACE_PROFILE: {
5799 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
5800 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5801 break;
5804 return 0;
5806 #endif
5809 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
5810 ** a useful spot to set a debugger breakpoint.
5812 ** This routine does not do anything practical. The code are there simply
5813 ** to prevent the compiler from optimizing this routine out.
5815 static void test_breakpoint(void){
5816 static unsigned int nCall = 0;
5817 if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
5821 ** An object used to read a CSV and other files for import.
5823 typedef struct ImportCtx ImportCtx;
5824 struct ImportCtx {
5825 const char *zFile; /* Name of the input file */
5826 FILE *in; /* Read the CSV text from this input stream */
5827 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
5828 char *z; /* Accumulated text for a field */
5829 int n; /* Number of bytes in z */
5830 int nAlloc; /* Space allocated for z[] */
5831 int nLine; /* Current line number */
5832 int nRow; /* Number of rows imported */
5833 int nErr; /* Number of errors encountered */
5834 int bNotFirst; /* True if one or more bytes already read */
5835 int cTerm; /* Character that terminated the most recent field */
5836 int cColSep; /* The column separator character. (Usually ",") */
5837 int cRowSep; /* The row separator character. (Usually "\n") */
5840 /* Clean up resourced used by an ImportCtx */
5841 static void import_cleanup(ImportCtx *p){
5842 if( p->in!=0 && p->xCloser!=0 ){
5843 p->xCloser(p->in);
5844 p->in = 0;
5846 sqlite3_free(p->z);
5847 p->z = 0;
5850 /* Append a single byte to z[] */
5851 static void import_append_char(ImportCtx *p, int c){
5852 if( p->n+1>=p->nAlloc ){
5853 p->nAlloc += p->nAlloc + 100;
5854 p->z = sqlite3_realloc64(p->z, p->nAlloc);
5855 shell_check_oom(p->z);
5857 p->z[p->n++] = (char)c;
5860 /* Read a single field of CSV text. Compatible with rfc4180 and extended
5861 ** with the option of having a separator other than ",".
5863 ** + Input comes from p->in.
5864 ** + Store results in p->z of length p->n. Space to hold p->z comes
5865 ** from sqlite3_malloc64().
5866 ** + Use p->cSep as the column separator. The default is ",".
5867 ** + Use p->rSep as the row separator. The default is "\n".
5868 ** + Keep track of the line number in p->nLine.
5869 ** + Store the character that terminates the field in p->cTerm. Store
5870 ** EOF on end-of-file.
5871 ** + Report syntax errors on stderr
5873 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
5874 int c;
5875 int cSep = (u8)p->cColSep;
5876 int rSep = (u8)p->cRowSep;
5877 p->n = 0;
5878 c = fgetc(p->in);
5879 if( c==EOF || seenInterrupt ){
5880 p->cTerm = EOF;
5881 return 0;
5883 if( c=='"' ){
5884 int pc, ppc;
5885 int startLine = p->nLine;
5886 int cQuote = c;
5887 pc = ppc = 0;
5888 while( 1 ){
5889 c = fgetc(p->in);
5890 if( c==rSep ) p->nLine++;
5891 if( c==cQuote ){
5892 if( pc==cQuote ){
5893 pc = 0;
5894 continue;
5897 if( (c==cSep && pc==cQuote)
5898 || (c==rSep && pc==cQuote)
5899 || (c==rSep && pc=='\r' && ppc==cQuote)
5900 || (c==EOF && pc==cQuote)
5902 do{ p->n--; }while( p->z[p->n]!=cQuote );
5903 p->cTerm = c;
5904 break;
5906 if( pc==cQuote && c!='\r' ){
5907 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
5908 p->zFile, p->nLine, cQuote);
5910 if( c==EOF ){
5911 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
5912 p->zFile, startLine, cQuote);
5913 p->cTerm = c;
5914 break;
5916 import_append_char(p, c);
5917 ppc = pc;
5918 pc = c;
5920 }else{
5921 /* If this is the first field being parsed and it begins with the
5922 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
5923 if( (c&0xff)==0xef && p->bNotFirst==0 ){
5924 import_append_char(p, c);
5925 c = fgetc(p->in);
5926 if( (c&0xff)==0xbb ){
5927 import_append_char(p, c);
5928 c = fgetc(p->in);
5929 if( (c&0xff)==0xbf ){
5930 p->bNotFirst = 1;
5931 p->n = 0;
5932 return csv_read_one_field(p);
5936 while( c!=EOF && c!=cSep && c!=rSep ){
5937 import_append_char(p, c);
5938 c = fgetc(p->in);
5940 if( c==rSep ){
5941 p->nLine++;
5942 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
5944 p->cTerm = c;
5946 if( p->z ) p->z[p->n] = 0;
5947 p->bNotFirst = 1;
5948 return p->z;
5951 /* Read a single field of ASCII delimited text.
5953 ** + Input comes from p->in.
5954 ** + Store results in p->z of length p->n. Space to hold p->z comes
5955 ** from sqlite3_malloc64().
5956 ** + Use p->cSep as the column separator. The default is "\x1F".
5957 ** + Use p->rSep as the row separator. The default is "\x1E".
5958 ** + Keep track of the row number in p->nLine.
5959 ** + Store the character that terminates the field in p->cTerm. Store
5960 ** EOF on end-of-file.
5961 ** + Report syntax errors on stderr
5963 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
5964 int c;
5965 int cSep = (u8)p->cColSep;
5966 int rSep = (u8)p->cRowSep;
5967 p->n = 0;
5968 c = fgetc(p->in);
5969 if( c==EOF || seenInterrupt ){
5970 p->cTerm = EOF;
5971 return 0;
5973 while( c!=EOF && c!=cSep && c!=rSep ){
5974 import_append_char(p, c);
5975 c = fgetc(p->in);
5977 if( c==rSep ){
5978 p->nLine++;
5980 p->cTerm = c;
5981 if( p->z ) p->z[p->n] = 0;
5982 return p->z;
5986 ** Try to transfer data for table zTable. If an error is seen while
5987 ** moving forward, try to go backwards. The backwards movement won't
5988 ** work for WITHOUT ROWID tables.
5990 static void tryToCloneData(
5991 ShellState *p,
5992 sqlite3 *newDb,
5993 const char *zTable
5995 sqlite3_stmt *pQuery = 0;
5996 sqlite3_stmt *pInsert = 0;
5997 char *zQuery = 0;
5998 char *zInsert = 0;
5999 int rc;
6000 int i, j, n;
6001 int nTable = strlen30(zTable);
6002 int k = 0;
6003 int cnt = 0;
6004 const int spinRate = 10000;
6006 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
6007 shell_check_oom(zQuery);
6008 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6009 if( rc ){
6010 utf8_printf(stderr, "Error %d: %s on [%s]\n",
6011 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
6012 zQuery);
6013 goto end_data_xfer;
6015 n = sqlite3_column_count(pQuery);
6016 zInsert = sqlite3_malloc64(200 + nTable + n*3);
6017 shell_check_oom(zInsert);
6018 sqlite3_snprintf(200+nTable,zInsert,
6019 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
6020 i = strlen30(zInsert);
6021 for(j=1; j<n; j++){
6022 memcpy(zInsert+i, ",?", 2);
6023 i += 2;
6025 memcpy(zInsert+i, ");", 3);
6026 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
6027 if( rc ){
6028 utf8_printf(stderr, "Error %d: %s on [%s]\n",
6029 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
6030 zQuery);
6031 goto end_data_xfer;
6033 for(k=0; k<2; k++){
6034 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6035 for(i=0; i<n; i++){
6036 switch( sqlite3_column_type(pQuery, i) ){
6037 case SQLITE_NULL: {
6038 sqlite3_bind_null(pInsert, i+1);
6039 break;
6041 case SQLITE_INTEGER: {
6042 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
6043 break;
6045 case SQLITE_FLOAT: {
6046 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
6047 break;
6049 case SQLITE_TEXT: {
6050 sqlite3_bind_text(pInsert, i+1,
6051 (const char*)sqlite3_column_text(pQuery,i),
6052 -1, SQLITE_STATIC);
6053 break;
6055 case SQLITE_BLOB: {
6056 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
6057 sqlite3_column_bytes(pQuery,i),
6058 SQLITE_STATIC);
6059 break;
6062 } /* End for */
6063 rc = sqlite3_step(pInsert);
6064 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
6065 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
6066 sqlite3_errmsg(newDb));
6068 sqlite3_reset(pInsert);
6069 cnt++;
6070 if( (cnt%spinRate)==0 ){
6071 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
6072 fflush(stdout);
6074 } /* End while */
6075 if( rc==SQLITE_DONE ) break;
6076 sqlite3_finalize(pQuery);
6077 sqlite3_free(zQuery);
6078 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
6079 zTable);
6080 shell_check_oom(zQuery);
6081 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6082 if( rc ){
6083 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
6084 break;
6086 } /* End for(k=0...) */
6088 end_data_xfer:
6089 sqlite3_finalize(pQuery);
6090 sqlite3_finalize(pInsert);
6091 sqlite3_free(zQuery);
6092 sqlite3_free(zInsert);
6097 ** Try to transfer all rows of the schema that match zWhere. For
6098 ** each row, invoke xForEach() on the object defined by that row.
6099 ** If an error is encountered while moving forward through the
6100 ** sqlite_schema table, try again moving backwards.
6102 static void tryToCloneSchema(
6103 ShellState *p,
6104 sqlite3 *newDb,
6105 const char *zWhere,
6106 void (*xForEach)(ShellState*,sqlite3*,const char*)
6108 sqlite3_stmt *pQuery = 0;
6109 char *zQuery = 0;
6110 int rc;
6111 const unsigned char *zName;
6112 const unsigned char *zSql;
6113 char *zErrMsg = 0;
6115 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6116 " WHERE %s ORDER BY rowid ASC", zWhere);
6117 shell_check_oom(zQuery);
6118 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6119 if( rc ){
6120 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
6121 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
6122 zQuery);
6123 goto end_schema_xfer;
6125 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6126 zName = sqlite3_column_text(pQuery, 0);
6127 zSql = sqlite3_column_text(pQuery, 1);
6128 if( zName==0 || zSql==0 ) continue;
6129 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
6130 printf("%s... ", zName); fflush(stdout);
6131 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6132 if( zErrMsg ){
6133 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6134 sqlite3_free(zErrMsg);
6135 zErrMsg = 0;
6138 if( xForEach ){
6139 xForEach(p, newDb, (const char*)zName);
6141 printf("done\n");
6143 if( rc!=SQLITE_DONE ){
6144 sqlite3_finalize(pQuery);
6145 sqlite3_free(zQuery);
6146 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6147 " WHERE %s ORDER BY rowid DESC", zWhere);
6148 shell_check_oom(zQuery);
6149 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6150 if( rc ){
6151 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
6152 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
6153 zQuery);
6154 goto end_schema_xfer;
6156 while( sqlite3_step(pQuery)==SQLITE_ROW ){
6157 zName = sqlite3_column_text(pQuery, 0);
6158 zSql = sqlite3_column_text(pQuery, 1);
6159 if( zName==0 || zSql==0 ) continue;
6160 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
6161 printf("%s... ", zName); fflush(stdout);
6162 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6163 if( zErrMsg ){
6164 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6165 sqlite3_free(zErrMsg);
6166 zErrMsg = 0;
6168 if( xForEach ){
6169 xForEach(p, newDb, (const char*)zName);
6171 printf("done\n");
6174 end_schema_xfer:
6175 sqlite3_finalize(pQuery);
6176 sqlite3_free(zQuery);
6180 ** Open a new database file named "zNewDb". Try to recover as much information
6181 ** as possible out of the main database (which might be corrupt) and write it
6182 ** into zNewDb.
6184 static void tryToClone(ShellState *p, const char *zNewDb){
6185 int rc;
6186 sqlite3 *newDb = 0;
6187 if( access(zNewDb,0)==0 ){
6188 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
6189 return;
6191 rc = sqlite3_open(zNewDb, &newDb);
6192 if( rc ){
6193 utf8_printf(stderr, "Cannot create output database: %s\n",
6194 sqlite3_errmsg(newDb));
6195 }else{
6196 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
6197 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
6198 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
6199 tryToCloneSchema(p, newDb, "type!='table'", 0);
6200 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
6201 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
6203 close_db(newDb);
6207 ** Change the output file back to stdout.
6209 ** If the p->doXdgOpen flag is set, that means the output was being
6210 ** redirected to a temporary file named by p->zTempFile. In that case,
6211 ** launch start/open/xdg-open on that temporary file.
6213 static void output_reset(ShellState *p){
6214 if( p->outfile[0]=='|' ){
6215 #ifndef SQLITE_OMIT_POPEN
6216 pclose(p->out);
6217 #endif
6218 }else{
6219 output_file_close(p->out);
6220 #ifndef SQLITE_NOHAVE_SYSTEM
6221 if( p->doXdgOpen ){
6222 const char *zXdgOpenCmd =
6223 #if defined(_WIN32)
6224 "start";
6225 #elif defined(__APPLE__)
6226 "open";
6227 #else
6228 "xdg-open";
6229 #endif
6230 char *zCmd;
6231 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
6232 if( system(zCmd) ){
6233 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
6234 }else{
6235 /* Give the start/open/xdg-open command some time to get
6236 ** going before we continue, and potential delete the
6237 ** p->zTempFile data file out from under it */
6238 sqlite3_sleep(2000);
6240 sqlite3_free(zCmd);
6241 outputModePop(p);
6242 p->doXdgOpen = 0;
6244 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
6246 p->outfile[0] = 0;
6247 p->out = stdout;
6251 ** Run an SQL command and return the single integer result.
6253 static int db_int(sqlite3 *db, const char *zSql){
6254 sqlite3_stmt *pStmt;
6255 int res = 0;
6256 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
6257 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
6258 res = sqlite3_column_int(pStmt,0);
6260 sqlite3_finalize(pStmt);
6261 return res;
6264 #if SQLITE_SHELL_HAVE_RECOVER
6266 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
6268 static unsigned int get2byteInt(unsigned char *a){
6269 return (a[0]<<8) + a[1];
6271 static unsigned int get4byteInt(unsigned char *a){
6272 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
6276 ** Implementation of the ".dbinfo" command.
6278 ** Return 1 on error, 2 to exit, and 0 otherwise.
6280 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
6281 static const struct { const char *zName; int ofst; } aField[] = {
6282 { "file change counter:", 24 },
6283 { "database page count:", 28 },
6284 { "freelist page count:", 36 },
6285 { "schema cookie:", 40 },
6286 { "schema format:", 44 },
6287 { "default cache size:", 48 },
6288 { "autovacuum top root:", 52 },
6289 { "incremental vacuum:", 64 },
6290 { "text encoding:", 56 },
6291 { "user version:", 60 },
6292 { "application id:", 68 },
6293 { "software version:", 96 },
6295 static const struct { const char *zName; const char *zSql; } aQuery[] = {
6296 { "number of tables:",
6297 "SELECT count(*) FROM %s WHERE type='table'" },
6298 { "number of indexes:",
6299 "SELECT count(*) FROM %s WHERE type='index'" },
6300 { "number of triggers:",
6301 "SELECT count(*) FROM %s WHERE type='trigger'" },
6302 { "number of views:",
6303 "SELECT count(*) FROM %s WHERE type='view'" },
6304 { "schema size:",
6305 "SELECT total(length(sql)) FROM %s" },
6307 int i, rc;
6308 unsigned iDataVersion;
6309 char *zSchemaTab;
6310 char *zDb = nArg>=2 ? azArg[1] : "main";
6311 sqlite3_stmt *pStmt = 0;
6312 unsigned char aHdr[100];
6313 open_db(p, 0);
6314 if( p->db==0 ) return 1;
6315 rc = sqlite3_prepare_v2(p->db,
6316 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
6317 -1, &pStmt, 0);
6318 if( rc ){
6319 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
6320 sqlite3_finalize(pStmt);
6321 return 1;
6323 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
6324 if( sqlite3_step(pStmt)==SQLITE_ROW
6325 && sqlite3_column_bytes(pStmt,0)>100
6327 const u8 *pb = sqlite3_column_blob(pStmt,0);
6328 shell_check_oom(pb);
6329 memcpy(aHdr, pb, 100);
6330 sqlite3_finalize(pStmt);
6331 }else{
6332 raw_printf(stderr, "unable to read database header\n");
6333 sqlite3_finalize(pStmt);
6334 return 1;
6336 i = get2byteInt(aHdr+16);
6337 if( i==1 ) i = 65536;
6338 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
6339 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
6340 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
6341 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
6342 for(i=0; i<ArraySize(aField); i++){
6343 int ofst = aField[i].ofst;
6344 unsigned int val = get4byteInt(aHdr + ofst);
6345 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
6346 switch( ofst ){
6347 case 56: {
6348 if( val==1 ) raw_printf(p->out, " (utf8)");
6349 if( val==2 ) raw_printf(p->out, " (utf16le)");
6350 if( val==3 ) raw_printf(p->out, " (utf16be)");
6353 raw_printf(p->out, "\n");
6355 if( zDb==0 ){
6356 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
6357 }else if( cli_strcmp(zDb,"temp")==0 ){
6358 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
6359 }else{
6360 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
6362 for(i=0; i<ArraySize(aQuery); i++){
6363 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
6364 int val = db_int(p->db, zSql);
6365 sqlite3_free(zSql);
6366 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
6368 sqlite3_free(zSchemaTab);
6369 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6370 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
6371 return 0;
6373 #endif /* SQLITE_SHELL_HAVE_RECOVER */
6376 ** Print the current sqlite3_errmsg() value to stderr and return 1.
6378 static int shellDatabaseError(sqlite3 *db){
6379 const char *zErr = sqlite3_errmsg(db);
6380 utf8_printf(stderr, "Error: %s\n", zErr);
6381 return 1;
6385 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
6386 ** if they match and FALSE (0) if they do not match.
6388 ** Globbing rules:
6390 ** '*' Matches any sequence of zero or more characters.
6392 ** '?' Matches exactly one character.
6394 ** [...] Matches one character from the enclosed list of
6395 ** characters.
6397 ** [^...] Matches one character not in the enclosed list.
6399 ** '#' Matches any sequence of one or more digits with an
6400 ** optional + or - sign in front
6402 ** ' ' Any span of whitespace matches any other span of
6403 ** whitespace.
6405 ** Extra whitespace at the end of z[] is ignored.
6407 static int testcase_glob(const char *zGlob, const char *z){
6408 int c, c2;
6409 int invert;
6410 int seen;
6412 while( (c = (*(zGlob++)))!=0 ){
6413 if( IsSpace(c) ){
6414 if( !IsSpace(*z) ) return 0;
6415 while( IsSpace(*zGlob) ) zGlob++;
6416 while( IsSpace(*z) ) z++;
6417 }else if( c=='*' ){
6418 while( (c=(*(zGlob++))) == '*' || c=='?' ){
6419 if( c=='?' && (*(z++))==0 ) return 0;
6421 if( c==0 ){
6422 return 1;
6423 }else if( c=='[' ){
6424 while( *z && testcase_glob(zGlob-1,z)==0 ){
6425 z++;
6427 return (*z)!=0;
6429 while( (c2 = (*(z++)))!=0 ){
6430 while( c2!=c ){
6431 c2 = *(z++);
6432 if( c2==0 ) return 0;
6434 if( testcase_glob(zGlob,z) ) return 1;
6436 return 0;
6437 }else if( c=='?' ){
6438 if( (*(z++))==0 ) return 0;
6439 }else if( c=='[' ){
6440 int prior_c = 0;
6441 seen = 0;
6442 invert = 0;
6443 c = *(z++);
6444 if( c==0 ) return 0;
6445 c2 = *(zGlob++);
6446 if( c2=='^' ){
6447 invert = 1;
6448 c2 = *(zGlob++);
6450 if( c2==']' ){
6451 if( c==']' ) seen = 1;
6452 c2 = *(zGlob++);
6454 while( c2 && c2!=']' ){
6455 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6456 c2 = *(zGlob++);
6457 if( c>=prior_c && c<=c2 ) seen = 1;
6458 prior_c = 0;
6459 }else{
6460 if( c==c2 ){
6461 seen = 1;
6463 prior_c = c2;
6465 c2 = *(zGlob++);
6467 if( c2==0 || (seen ^ invert)==0 ) return 0;
6468 }else if( c=='#' ){
6469 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6470 if( !IsDigit(z[0]) ) return 0;
6471 z++;
6472 while( IsDigit(z[0]) ){ z++; }
6473 }else{
6474 if( c!=(*(z++)) ) return 0;
6477 while( IsSpace(*z) ){ z++; }
6478 return *z==0;
6483 ** Compare the string as a command-line option with either one or two
6484 ** initial "-" characters.
6486 static int optionMatch(const char *zStr, const char *zOpt){
6487 if( zStr[0]!='-' ) return 0;
6488 zStr++;
6489 if( zStr[0]=='-' ) zStr++;
6490 return cli_strcmp(zStr, zOpt)==0;
6494 ** Delete a file.
6496 int shellDeleteFile(const char *zFilename){
6497 int rc;
6498 #ifdef _WIN32
6499 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6500 rc = _wunlink(z);
6501 sqlite3_free(z);
6502 #else
6503 rc = unlink(zFilename);
6504 #endif
6505 return rc;
6509 ** Try to delete the temporary file (if there is one) and free the
6510 ** memory used to hold the name of the temp file.
6512 static void clearTempFile(ShellState *p){
6513 if( p->zTempFile==0 ) return;
6514 if( p->doXdgOpen ) return;
6515 if( shellDeleteFile(p->zTempFile) ) return;
6516 sqlite3_free(p->zTempFile);
6517 p->zTempFile = 0;
6521 ** Create a new temp file name with the given suffix.
6523 static void newTempFile(ShellState *p, const char *zSuffix){
6524 clearTempFile(p);
6525 sqlite3_free(p->zTempFile);
6526 p->zTempFile = 0;
6527 if( p->db ){
6528 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6530 if( p->zTempFile==0 ){
6531 /* If p->db is an in-memory database then the TEMPFILENAME file-control
6532 ** will not work and we will need to fallback to guessing */
6533 char *zTemp;
6534 sqlite3_uint64 r;
6535 sqlite3_randomness(sizeof(r), &r);
6536 zTemp = getenv("TEMP");
6537 if( zTemp==0 ) zTemp = getenv("TMP");
6538 if( zTemp==0 ){
6539 #ifdef _WIN32
6540 zTemp = "\\tmp";
6541 #else
6542 zTemp = "/tmp";
6543 #endif
6545 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6546 }else{
6547 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6549 shell_check_oom(p->zTempFile);
6554 ** The implementation of SQL scalar function fkey_collate_clause(), used
6555 ** by the ".lint fkey-indexes" command. This scalar function is always
6556 ** called with four arguments - the parent table name, the parent column name,
6557 ** the child table name and the child column name.
6559 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6561 ** If either of the named tables or columns do not exist, this function
6562 ** returns an empty string. An empty string is also returned if both tables
6563 ** and columns exist but have the same default collation sequence. Or,
6564 ** if both exist but the default collation sequences are different, this
6565 ** function returns the string " COLLATE <parent-collation>", where
6566 ** <parent-collation> is the default collation sequence of the parent column.
6568 static void shellFkeyCollateClause(
6569 sqlite3_context *pCtx,
6570 int nVal,
6571 sqlite3_value **apVal
6573 sqlite3 *db = sqlite3_context_db_handle(pCtx);
6574 const char *zParent;
6575 const char *zParentCol;
6576 const char *zParentSeq;
6577 const char *zChild;
6578 const char *zChildCol;
6579 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
6580 int rc;
6582 assert( nVal==4 );
6583 zParent = (const char*)sqlite3_value_text(apVal[0]);
6584 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6585 zChild = (const char*)sqlite3_value_text(apVal[2]);
6586 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6588 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6589 rc = sqlite3_table_column_metadata(
6590 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6592 if( rc==SQLITE_OK ){
6593 rc = sqlite3_table_column_metadata(
6594 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6598 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6599 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6600 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6601 sqlite3_free(z);
6607 ** The implementation of dot-command ".lint fkey-indexes".
6609 static int lintFkeyIndexes(
6610 ShellState *pState, /* Current shell tool state */
6611 char **azArg, /* Array of arguments passed to dot command */
6612 int nArg /* Number of entries in azArg[] */
6614 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
6615 FILE *out = pState->out; /* Stream to write non-error output to */
6616 int bVerbose = 0; /* If -verbose is present */
6617 int bGroupByParent = 0; /* If -groupbyparent is present */
6618 int i; /* To iterate through azArg[] */
6619 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
6620 int rc; /* Return code */
6621 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
6624 ** This SELECT statement returns one row for each foreign key constraint
6625 ** in the schema of the main database. The column values are:
6627 ** 0. The text of an SQL statement similar to:
6629 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6631 ** This SELECT is similar to the one that the foreign keys implementation
6632 ** needs to run internally on child tables. If there is an index that can
6633 ** be used to optimize this query, then it can also be used by the FK
6634 ** implementation to optimize DELETE or UPDATE statements on the parent
6635 ** table.
6637 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6638 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6639 ** contains an index that can be used to optimize the query.
6641 ** 2. Human readable text that describes the child table and columns. e.g.
6643 ** "child_table(child_key1, child_key2)"
6645 ** 3. Human readable text that describes the parent table and columns. e.g.
6647 ** "parent_table(parent_key1, parent_key2)"
6649 ** 4. A full CREATE INDEX statement for an index that could be used to
6650 ** optimize DELETE or UPDATE statements on the parent table. e.g.
6652 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
6654 ** 5. The name of the parent table.
6656 ** These six values are used by the C logic below to generate the report.
6658 const char *zSql =
6659 "SELECT "
6660 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6661 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6662 " || fkey_collate_clause("
6663 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6664 ", "
6665 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6666 " || group_concat('*=?', ' AND ') || ')'"
6667 ", "
6668 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
6669 ", "
6670 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6671 ", "
6672 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6673 " || ' ON ' || quote(s.name) || '('"
6674 " || group_concat(quote(f.[from]) ||"
6675 " fkey_collate_clause("
6676 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6677 " || ');'"
6678 ", "
6679 " f.[table] "
6680 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6681 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6682 "GROUP BY s.name, f.id "
6683 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6685 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6687 for(i=2; i<nArg; i++){
6688 int n = strlen30(azArg[i]);
6689 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6690 bVerbose = 1;
6692 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6693 bGroupByParent = 1;
6694 zIndent = " ";
6696 else{
6697 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6698 azArg[0], azArg[1]
6700 return SQLITE_ERROR;
6704 /* Register the fkey_collate_clause() SQL function */
6705 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6706 0, shellFkeyCollateClause, 0, 0
6710 if( rc==SQLITE_OK ){
6711 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6713 if( rc==SQLITE_OK ){
6714 sqlite3_bind_int(pSql, 1, bGroupByParent);
6717 if( rc==SQLITE_OK ){
6718 int rc2;
6719 char *zPrev = 0;
6720 while( SQLITE_ROW==sqlite3_step(pSql) ){
6721 int res = -1;
6722 sqlite3_stmt *pExplain = 0;
6723 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6724 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6725 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6726 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6727 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6728 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6730 if( zEQP==0 ) continue;
6731 if( zGlob==0 ) continue;
6732 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6733 if( rc!=SQLITE_OK ) break;
6734 if( SQLITE_ROW==sqlite3_step(pExplain) ){
6735 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6736 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
6737 || 0==sqlite3_strglob(zGlobIPK, zPlan));
6739 rc = sqlite3_finalize(pExplain);
6740 if( rc!=SQLITE_OK ) break;
6742 if( res<0 ){
6743 raw_printf(stderr, "Error: internal error");
6744 break;
6745 }else{
6746 if( bGroupByParent
6747 && (bVerbose || res==0)
6748 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6750 raw_printf(out, "-- Parent table %s\n", zParent);
6751 sqlite3_free(zPrev);
6752 zPrev = sqlite3_mprintf("%s", zParent);
6755 if( res==0 ){
6756 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
6757 }else if( bVerbose ){
6758 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
6759 zIndent, zFrom, zTarget
6764 sqlite3_free(zPrev);
6766 if( rc!=SQLITE_OK ){
6767 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6770 rc2 = sqlite3_finalize(pSql);
6771 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6772 rc = rc2;
6773 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6775 }else{
6776 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
6779 return rc;
6783 ** Implementation of ".lint" dot command.
6785 static int lintDotCommand(
6786 ShellState *pState, /* Current shell tool state */
6787 char **azArg, /* Array of arguments passed to dot command */
6788 int nArg /* Number of entries in azArg[] */
6790 int n;
6791 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6792 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6793 return lintFkeyIndexes(pState, azArg, nArg);
6795 usage:
6796 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
6797 raw_printf(stderr, "Where sub-commands are:\n");
6798 raw_printf(stderr, " fkey-indexes\n");
6799 return SQLITE_ERROR;
6802 #if !defined SQLITE_OMIT_VIRTUALTABLE
6803 static void shellPrepare(
6804 sqlite3 *db,
6805 int *pRc,
6806 const char *zSql,
6807 sqlite3_stmt **ppStmt
6809 *ppStmt = 0;
6810 if( *pRc==SQLITE_OK ){
6811 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6812 if( rc!=SQLITE_OK ){
6813 raw_printf(stderr, "sql error: %s (%d)\n",
6814 sqlite3_errmsg(db), sqlite3_errcode(db)
6816 *pRc = rc;
6822 ** Create a prepared statement using printf-style arguments for the SQL.
6824 ** This routine is could be marked "static". But it is not always used,
6825 ** depending on compile-time options. By omitting the "static", we avoid
6826 ** nuisance compiler warnings about "defined but not used".
6828 void shellPreparePrintf(
6829 sqlite3 *db,
6830 int *pRc,
6831 sqlite3_stmt **ppStmt,
6832 const char *zFmt,
6835 *ppStmt = 0;
6836 if( *pRc==SQLITE_OK ){
6837 va_list ap;
6838 char *z;
6839 va_start(ap, zFmt);
6840 z = sqlite3_vmprintf(zFmt, ap);
6841 va_end(ap);
6842 if( z==0 ){
6843 *pRc = SQLITE_NOMEM;
6844 }else{
6845 shellPrepare(db, pRc, z, ppStmt);
6846 sqlite3_free(z);
6851 /* Finalize the prepared statement created using shellPreparePrintf().
6853 ** This routine is could be marked "static". But it is not always used,
6854 ** depending on compile-time options. By omitting the "static", we avoid
6855 ** nuisance compiler warnings about "defined but not used".
6857 void shellFinalize(
6858 int *pRc,
6859 sqlite3_stmt *pStmt
6861 if( pStmt ){
6862 sqlite3 *db = sqlite3_db_handle(pStmt);
6863 int rc = sqlite3_finalize(pStmt);
6864 if( *pRc==SQLITE_OK ){
6865 if( rc!=SQLITE_OK ){
6866 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6868 *pRc = rc;
6873 /* Reset the prepared statement created using shellPreparePrintf().
6875 ** This routine is could be marked "static". But it is not always used,
6876 ** depending on compile-time options. By omitting the "static", we avoid
6877 ** nuisance compiler warnings about "defined but not used".
6879 void shellReset(
6880 int *pRc,
6881 sqlite3_stmt *pStmt
6883 int rc = sqlite3_reset(pStmt);
6884 if( *pRc==SQLITE_OK ){
6885 if( rc!=SQLITE_OK ){
6886 sqlite3 *db = sqlite3_db_handle(pStmt);
6887 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
6889 *pRc = rc;
6892 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6894 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6895 /******************************************************************************
6896 ** The ".archive" or ".ar" command.
6899 ** Structure representing a single ".ar" command.
6901 typedef struct ArCommand ArCommand;
6902 struct ArCommand {
6903 u8 eCmd; /* An AR_CMD_* value */
6904 u8 bVerbose; /* True if --verbose */
6905 u8 bZip; /* True if the archive is a ZIP */
6906 u8 bDryRun; /* True if --dry-run */
6907 u8 bAppend; /* True if --append */
6908 u8 bGlob; /* True if --glob */
6909 u8 fromCmdLine; /* Run from -A instead of .archive */
6910 int nArg; /* Number of command arguments */
6911 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
6912 const char *zFile; /* --file argument, or NULL */
6913 const char *zDir; /* --directory argument, or NULL */
6914 char **azArg; /* Array of command arguments */
6915 ShellState *p; /* Shell state */
6916 sqlite3 *db; /* Database containing the archive */
6920 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6922 static int arUsage(FILE *f){
6923 showHelp(f,"archive");
6924 return SQLITE_ERROR;
6928 ** Print an error message for the .ar command to stderr and return
6929 ** SQLITE_ERROR.
6931 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
6932 va_list ap;
6933 char *z;
6934 va_start(ap, zFmt);
6935 z = sqlite3_vmprintf(zFmt, ap);
6936 va_end(ap);
6937 utf8_printf(stderr, "Error: %s\n", z);
6938 if( pAr->fromCmdLine ){
6939 utf8_printf(stderr, "Use \"-A\" for more help\n");
6940 }else{
6941 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
6943 sqlite3_free(z);
6944 return SQLITE_ERROR;
6948 ** Values for ArCommand.eCmd.
6950 #define AR_CMD_CREATE 1
6951 #define AR_CMD_UPDATE 2
6952 #define AR_CMD_INSERT 3
6953 #define AR_CMD_EXTRACT 4
6954 #define AR_CMD_LIST 5
6955 #define AR_CMD_HELP 6
6956 #define AR_CMD_REMOVE 7
6959 ** Other (non-command) switches.
6961 #define AR_SWITCH_VERBOSE 8
6962 #define AR_SWITCH_FILE 9
6963 #define AR_SWITCH_DIRECTORY 10
6964 #define AR_SWITCH_APPEND 11
6965 #define AR_SWITCH_DRYRUN 12
6966 #define AR_SWITCH_GLOB 13
6968 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
6969 switch( eSwitch ){
6970 case AR_CMD_CREATE:
6971 case AR_CMD_EXTRACT:
6972 case AR_CMD_LIST:
6973 case AR_CMD_REMOVE:
6974 case AR_CMD_UPDATE:
6975 case AR_CMD_INSERT:
6976 case AR_CMD_HELP:
6977 if( pAr->eCmd ){
6978 return arErrorMsg(pAr, "multiple command options");
6980 pAr->eCmd = eSwitch;
6981 break;
6983 case AR_SWITCH_DRYRUN:
6984 pAr->bDryRun = 1;
6985 break;
6986 case AR_SWITCH_GLOB:
6987 pAr->bGlob = 1;
6988 break;
6989 case AR_SWITCH_VERBOSE:
6990 pAr->bVerbose = 1;
6991 break;
6992 case AR_SWITCH_APPEND:
6993 pAr->bAppend = 1;
6994 deliberate_fall_through;
6995 case AR_SWITCH_FILE:
6996 pAr->zFile = zArg;
6997 break;
6998 case AR_SWITCH_DIRECTORY:
6999 pAr->zDir = zArg;
7000 break;
7003 return SQLITE_OK;
7007 ** Parse the command line for an ".ar" command. The results are written into
7008 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
7009 ** successfully, otherwise an error message is written to stderr and
7010 ** SQLITE_ERROR returned.
7012 static int arParseCommand(
7013 char **azArg, /* Array of arguments passed to dot command */
7014 int nArg, /* Number of entries in azArg[] */
7015 ArCommand *pAr /* Populate this object */
7017 struct ArSwitch {
7018 const char *zLong;
7019 char cShort;
7020 u8 eSwitch;
7021 u8 bArg;
7022 } aSwitch[] = {
7023 { "create", 'c', AR_CMD_CREATE, 0 },
7024 { "extract", 'x', AR_CMD_EXTRACT, 0 },
7025 { "insert", 'i', AR_CMD_INSERT, 0 },
7026 { "list", 't', AR_CMD_LIST, 0 },
7027 { "remove", 'r', AR_CMD_REMOVE, 0 },
7028 { "update", 'u', AR_CMD_UPDATE, 0 },
7029 { "help", 'h', AR_CMD_HELP, 0 },
7030 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
7031 { "file", 'f', AR_SWITCH_FILE, 1 },
7032 { "append", 'a', AR_SWITCH_APPEND, 1 },
7033 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
7034 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
7035 { "glob", 'g', AR_SWITCH_GLOB, 0 },
7037 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
7038 struct ArSwitch *pEnd = &aSwitch[nSwitch];
7040 if( nArg<=1 ){
7041 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
7042 return arUsage(stderr);
7043 }else{
7044 char *z = azArg[1];
7045 if( z[0]!='-' ){
7046 /* Traditional style [tar] invocation */
7047 int i;
7048 int iArg = 2;
7049 for(i=0; z[i]; i++){
7050 const char *zArg = 0;
7051 struct ArSwitch *pOpt;
7052 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7053 if( z[i]==pOpt->cShort ) break;
7055 if( pOpt==pEnd ){
7056 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7058 if( pOpt->bArg ){
7059 if( iArg>=nArg ){
7060 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
7062 zArg = azArg[iArg++];
7064 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7066 pAr->nArg = nArg-iArg;
7067 if( pAr->nArg>0 ){
7068 pAr->azArg = &azArg[iArg];
7070 }else{
7071 /* Non-traditional invocation */
7072 int iArg;
7073 for(iArg=1; iArg<nArg; iArg++){
7074 int n;
7075 z = azArg[iArg];
7076 if( z[0]!='-' ){
7077 /* All remaining command line words are command arguments. */
7078 pAr->azArg = &azArg[iArg];
7079 pAr->nArg = nArg-iArg;
7080 break;
7082 n = strlen30(z);
7084 if( z[1]!='-' ){
7085 int i;
7086 /* One or more short options */
7087 for(i=1; i<n; i++){
7088 const char *zArg = 0;
7089 struct ArSwitch *pOpt;
7090 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7091 if( z[i]==pOpt->cShort ) break;
7093 if( pOpt==pEnd ){
7094 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7096 if( pOpt->bArg ){
7097 if( i<(n-1) ){
7098 zArg = &z[i+1];
7099 i = n;
7100 }else{
7101 if( iArg>=(nArg-1) ){
7102 return arErrorMsg(pAr, "option requires an argument: %c",
7103 z[i]);
7105 zArg = azArg[++iArg];
7108 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7110 }else if( z[2]=='\0' ){
7111 /* A -- option, indicating that all remaining command line words
7112 ** are command arguments. */
7113 pAr->azArg = &azArg[iArg+1];
7114 pAr->nArg = nArg-iArg-1;
7115 break;
7116 }else{
7117 /* A long option */
7118 const char *zArg = 0; /* Argument for option, if any */
7119 struct ArSwitch *pMatch = 0; /* Matching option */
7120 struct ArSwitch *pOpt; /* Iterator */
7121 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7122 const char *zLong = pOpt->zLong;
7123 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
7124 if( pMatch ){
7125 return arErrorMsg(pAr, "ambiguous option: %s",z);
7126 }else{
7127 pMatch = pOpt;
7132 if( pMatch==0 ){
7133 return arErrorMsg(pAr, "unrecognized option: %s", z);
7135 if( pMatch->bArg ){
7136 if( iArg>=(nArg-1) ){
7137 return arErrorMsg(pAr, "option requires an argument: %s", z);
7139 zArg = azArg[++iArg];
7141 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
7146 if( pAr->eCmd==0 ){
7147 utf8_printf(stderr, "Required argument missing. Usage:\n");
7148 return arUsage(stderr);
7150 return SQLITE_OK;
7154 ** This function assumes that all arguments within the ArCommand.azArg[]
7155 ** array refer to archive members, as for the --extract, --list or --remove
7156 ** commands. It checks that each of them are "present". If any specified
7157 ** file is not present in the archive, an error is printed to stderr and an
7158 ** error code returned. Otherwise, if all specified arguments are present
7159 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
7160 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
7161 ** when pAr->bGlob is true.
7163 ** This function strips any trailing '/' characters from each argument.
7164 ** This is consistent with the way the [tar] command seems to work on
7165 ** Linux.
7167 static int arCheckEntries(ArCommand *pAr){
7168 int rc = SQLITE_OK;
7169 if( pAr->nArg ){
7170 int i, j;
7171 sqlite3_stmt *pTest = 0;
7172 const char *zSel = (pAr->bGlob)
7173 ? "SELECT name FROM %s WHERE glob($name,name)"
7174 : "SELECT name FROM %s WHERE name=$name";
7176 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
7177 j = sqlite3_bind_parameter_index(pTest, "$name");
7178 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7179 char *z = pAr->azArg[i];
7180 int n = strlen30(z);
7181 int bOk = 0;
7182 while( n>0 && z[n-1]=='/' ) n--;
7183 z[n] = '\0';
7184 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
7185 if( SQLITE_ROW==sqlite3_step(pTest) ){
7186 bOk = 1;
7188 shellReset(&rc, pTest);
7189 if( rc==SQLITE_OK && bOk==0 ){
7190 utf8_printf(stderr, "not found in archive: %s\n", z);
7191 rc = SQLITE_ERROR;
7194 shellFinalize(&rc, pTest);
7196 return rc;
7200 ** Format a WHERE clause that can be used against the "sqlar" table to
7201 ** identify all archive members that match the command arguments held
7202 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
7203 ** The caller is responsible for eventually calling sqlite3_free() on
7204 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
7205 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
7207 static void arWhereClause(
7208 int *pRc,
7209 ArCommand *pAr,
7210 char **pzWhere /* OUT: New WHERE clause */
7212 char *zWhere = 0;
7213 const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
7214 if( *pRc==SQLITE_OK ){
7215 if( pAr->nArg==0 ){
7216 zWhere = sqlite3_mprintf("1");
7217 }else{
7218 int i;
7219 const char *zSep = "";
7220 for(i=0; i<pAr->nArg; i++){
7221 const char *z = pAr->azArg[i];
7222 zWhere = sqlite3_mprintf(
7223 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
7224 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
7226 if( zWhere==0 ){
7227 *pRc = SQLITE_NOMEM;
7228 break;
7230 zSep = " OR ";
7234 *pzWhere = zWhere;
7238 ** Implementation of .ar "lisT" command.
7240 static int arListCommand(ArCommand *pAr){
7241 const char *zSql = "SELECT %s FROM %s WHERE %s";
7242 const char *azCols[] = {
7243 "name",
7244 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
7247 char *zWhere = 0;
7248 sqlite3_stmt *pSql = 0;
7249 int rc;
7251 rc = arCheckEntries(pAr);
7252 arWhereClause(&rc, pAr, &zWhere);
7254 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
7255 pAr->zSrcTable, zWhere);
7256 if( pAr->bDryRun ){
7257 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7258 }else{
7259 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7260 if( pAr->bVerbose ){
7261 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
7262 sqlite3_column_text(pSql, 0),
7263 sqlite3_column_int(pSql, 1),
7264 sqlite3_column_text(pSql, 2),
7265 sqlite3_column_text(pSql, 3)
7267 }else{
7268 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7272 shellFinalize(&rc, pSql);
7273 sqlite3_free(zWhere);
7274 return rc;
7279 ** Implementation of .ar "Remove" command.
7281 static int arRemoveCommand(ArCommand *pAr){
7282 int rc = 0;
7283 char *zSql = 0;
7284 char *zWhere = 0;
7286 if( pAr->nArg ){
7287 /* Verify that args actually exist within the archive before proceeding.
7288 ** And formulate a WHERE clause to match them. */
7289 rc = arCheckEntries(pAr);
7290 arWhereClause(&rc, pAr, &zWhere);
7292 if( rc==SQLITE_OK ){
7293 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
7294 pAr->zSrcTable, zWhere);
7295 if( pAr->bDryRun ){
7296 utf8_printf(pAr->p->out, "%s\n", zSql);
7297 }else{
7298 char *zErr = 0;
7299 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
7300 if( rc==SQLITE_OK ){
7301 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7302 if( rc!=SQLITE_OK ){
7303 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7304 }else{
7305 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
7308 if( zErr ){
7309 utf8_printf(stdout, "ERROR: %s\n", zErr);
7310 sqlite3_free(zErr);
7314 sqlite3_free(zWhere);
7315 sqlite3_free(zSql);
7316 return rc;
7320 ** Implementation of .ar "eXtract" command.
7322 static int arExtractCommand(ArCommand *pAr){
7323 const char *zSql1 =
7324 "SELECT "
7325 " ($dir || name),"
7326 " writefile(($dir || name), %s, mode, mtime) "
7327 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
7328 " AND name NOT GLOB '*..[/\\]*'";
7330 const char *azExtraArg[] = {
7331 "sqlar_uncompress(data, sz)",
7332 "data"
7335 sqlite3_stmt *pSql = 0;
7336 int rc = SQLITE_OK;
7337 char *zDir = 0;
7338 char *zWhere = 0;
7339 int i, j;
7341 /* If arguments are specified, check that they actually exist within
7342 ** the archive before proceeding. And formulate a WHERE clause to
7343 ** match them. */
7344 rc = arCheckEntries(pAr);
7345 arWhereClause(&rc, pAr, &zWhere);
7347 if( rc==SQLITE_OK ){
7348 if( pAr->zDir ){
7349 zDir = sqlite3_mprintf("%s/", pAr->zDir);
7350 }else{
7351 zDir = sqlite3_mprintf("");
7353 if( zDir==0 ) rc = SQLITE_NOMEM;
7356 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
7357 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
7360 if( rc==SQLITE_OK ){
7361 j = sqlite3_bind_parameter_index(pSql, "$dir");
7362 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
7364 /* Run the SELECT statement twice. The first time, writefile() is called
7365 ** for all archive members that should be extracted. The second time,
7366 ** only for the directories. This is because the timestamps for
7367 ** extracted directories must be reset after they are populated (as
7368 ** populating them changes the timestamp). */
7369 for(i=0; i<2; i++){
7370 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7371 sqlite3_bind_int(pSql, j, i);
7372 if( pAr->bDryRun ){
7373 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
7374 }else{
7375 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7376 if( i==0 && pAr->bVerbose ){
7377 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
7381 shellReset(&rc, pSql);
7383 shellFinalize(&rc, pSql);
7386 sqlite3_free(zDir);
7387 sqlite3_free(zWhere);
7388 return rc;
7392 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
7394 static int arExecSql(ArCommand *pAr, const char *zSql){
7395 int rc;
7396 if( pAr->bDryRun ){
7397 utf8_printf(pAr->p->out, "%s\n", zSql);
7398 rc = SQLITE_OK;
7399 }else{
7400 char *zErr = 0;
7401 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7402 if( zErr ){
7403 utf8_printf(stdout, "ERROR: %s\n", zErr);
7404 sqlite3_free(zErr);
7407 return rc;
7412 ** Implementation of .ar "create", "insert", and "update" commands.
7414 ** create -> Create a new SQL archive
7415 ** insert -> Insert or reinsert all files listed
7416 ** update -> Insert files that have changed or that were not
7417 ** previously in the archive
7419 ** Create the "sqlar" table in the database if it does not already exist.
7420 ** Then add each file in the azFile[] array to the archive. Directories
7421 ** are added recursively. If argument bVerbose is non-zero, a message is
7422 ** printed on stdout for each file archived.
7424 ** The create command is the same as update, except that it drops
7425 ** any existing "sqlar" table before beginning. The "insert" command
7426 ** always overwrites every file named on the command-line, where as
7427 ** "update" only overwrites if the size or mtime or mode has changed.
7429 static int arCreateOrUpdateCommand(
7430 ArCommand *pAr, /* Command arguments and options */
7431 int bUpdate, /* true for a --create. */
7432 int bOnlyIfChanged /* Only update if file has changed */
7434 const char *zCreate =
7435 "CREATE TABLE IF NOT EXISTS sqlar(\n"
7436 " name TEXT PRIMARY KEY, -- name of the file\n"
7437 " mode INT, -- access permissions\n"
7438 " mtime INT, -- last modification time\n"
7439 " sz INT, -- original file size\n"
7440 " data BLOB -- compressed content\n"
7441 ")";
7442 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7443 const char *zInsertFmt[2] = {
7444 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7445 " SELECT\n"
7446 " %s,\n"
7447 " mode,\n"
7448 " mtime,\n"
7449 " CASE substr(lsmode(mode),1,1)\n"
7450 " WHEN '-' THEN length(data)\n"
7451 " WHEN 'd' THEN 0\n"
7452 " ELSE -1 END,\n"
7453 " sqlar_compress(data)\n"
7454 " FROM fsdir(%Q,%Q) AS disk\n"
7455 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7457 "REPLACE INTO %s(name,mode,mtime,data)\n"
7458 " SELECT\n"
7459 " %s,\n"
7460 " mode,\n"
7461 " mtime,\n"
7462 " data\n"
7463 " FROM fsdir(%Q,%Q) AS disk\n"
7464 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7466 int i; /* For iterating through azFile[] */
7467 int rc; /* Return code */
7468 const char *zTab = 0; /* SQL table into which to insert */
7469 char *zSql;
7470 char zTemp[50];
7471 char *zExists = 0;
7473 arExecSql(pAr, "PRAGMA page_size=512");
7474 rc = arExecSql(pAr, "SAVEPOINT ar;");
7475 if( rc!=SQLITE_OK ) return rc;
7476 zTemp[0] = 0;
7477 if( pAr->bZip ){
7478 /* Initialize the zipfile virtual table, if necessary */
7479 if( pAr->zFile ){
7480 sqlite3_uint64 r;
7481 sqlite3_randomness(sizeof(r),&r);
7482 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7483 zTab = zTemp;
7484 zSql = sqlite3_mprintf(
7485 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7486 zTab, pAr->zFile
7488 rc = arExecSql(pAr, zSql);
7489 sqlite3_free(zSql);
7490 }else{
7491 zTab = "zip";
7493 }else{
7494 /* Initialize the table for an SQLAR */
7495 zTab = "sqlar";
7496 if( bUpdate==0 ){
7497 rc = arExecSql(pAr, zDrop);
7498 if( rc!=SQLITE_OK ) goto end_ar_transaction;
7500 rc = arExecSql(pAr, zCreate);
7502 if( bOnlyIfChanged ){
7503 zExists = sqlite3_mprintf(
7504 " AND NOT EXISTS("
7505 "SELECT 1 FROM %s AS mem"
7506 " WHERE mem.name=disk.name"
7507 " AND mem.mtime=disk.mtime"
7508 " AND mem.mode=disk.mode)", zTab);
7509 }else{
7510 zExists = sqlite3_mprintf("");
7512 if( zExists==0 ) rc = SQLITE_NOMEM;
7513 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7514 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7515 pAr->bVerbose ? "shell_putsnl(name)" : "name",
7516 pAr->azArg[i], pAr->zDir, zExists);
7517 rc = arExecSql(pAr, zSql2);
7518 sqlite3_free(zSql2);
7520 end_ar_transaction:
7521 if( rc!=SQLITE_OK ){
7522 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7523 }else{
7524 rc = arExecSql(pAr, "RELEASE ar;");
7525 if( pAr->bZip && pAr->zFile ){
7526 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7527 arExecSql(pAr, zSql);
7528 sqlite3_free(zSql);
7531 sqlite3_free(zExists);
7532 return rc;
7536 ** Implementation of ".ar" dot command.
7538 static int arDotCommand(
7539 ShellState *pState, /* Current shell tool state */
7540 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
7541 char **azArg, /* Array of arguments passed to dot command */
7542 int nArg /* Number of entries in azArg[] */
7544 ArCommand cmd;
7545 int rc;
7546 memset(&cmd, 0, sizeof(cmd));
7547 cmd.fromCmdLine = fromCmdLine;
7548 rc = arParseCommand(azArg, nArg, &cmd);
7549 if( rc==SQLITE_OK ){
7550 int eDbType = SHELL_OPEN_UNSPEC;
7551 cmd.p = pState;
7552 cmd.db = pState->db;
7553 if( cmd.zFile ){
7554 eDbType = deduceDatabaseType(cmd.zFile, 1);
7555 }else{
7556 eDbType = pState->openMode;
7558 if( eDbType==SHELL_OPEN_ZIPFILE ){
7559 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7560 if( cmd.zFile==0 ){
7561 cmd.zSrcTable = sqlite3_mprintf("zip");
7562 }else{
7563 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7566 cmd.bZip = 1;
7567 }else if( cmd.zFile ){
7568 int flags;
7569 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7570 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7571 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7572 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7573 }else{
7574 flags = SQLITE_OPEN_READONLY;
7576 cmd.db = 0;
7577 if( cmd.bDryRun ){
7578 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
7579 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7581 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7582 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7583 if( rc!=SQLITE_OK ){
7584 utf8_printf(stderr, "cannot open file: %s (%s)\n",
7585 cmd.zFile, sqlite3_errmsg(cmd.db)
7587 goto end_ar_command;
7589 sqlite3_fileio_init(cmd.db, 0, 0);
7590 sqlite3_sqlar_init(cmd.db, 0, 0);
7591 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7592 shellPutsFunc, 0, 0);
7595 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7596 if( cmd.eCmd!=AR_CMD_CREATE
7597 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7599 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
7600 rc = SQLITE_ERROR;
7601 goto end_ar_command;
7603 cmd.zSrcTable = sqlite3_mprintf("sqlar");
7606 switch( cmd.eCmd ){
7607 case AR_CMD_CREATE:
7608 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7609 break;
7611 case AR_CMD_EXTRACT:
7612 rc = arExtractCommand(&cmd);
7613 break;
7615 case AR_CMD_LIST:
7616 rc = arListCommand(&cmd);
7617 break;
7619 case AR_CMD_HELP:
7620 arUsage(pState->out);
7621 break;
7623 case AR_CMD_INSERT:
7624 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7625 break;
7627 case AR_CMD_REMOVE:
7628 rc = arRemoveCommand(&cmd);
7629 break;
7631 default:
7632 assert( cmd.eCmd==AR_CMD_UPDATE );
7633 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7634 break;
7637 end_ar_command:
7638 if( cmd.db!=pState->db ){
7639 close_db(cmd.db);
7641 sqlite3_free(cmd.zSrcTable);
7643 return rc;
7645 /* End of the ".archive" or ".ar" command logic
7646 *******************************************************************************/
7647 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7649 #if SQLITE_SHELL_HAVE_RECOVER
7652 ** This function is used as a callback by the recover extension. Simply
7653 ** print the supplied SQL statement to stdout.
7655 static int recoverSqlCb(void *pCtx, const char *zSql){
7656 ShellState *pState = (ShellState*)pCtx;
7657 utf8_printf(pState->out, "%s;\n", zSql);
7658 return SQLITE_OK;
7662 ** This function is called to recover data from the database. A script
7663 ** to construct a new database containing all recovered data is output
7664 ** on stream pState->out.
7666 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7667 int rc = SQLITE_OK;
7668 const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
7669 const char *zLAF = "lost_and_found";
7670 int bFreelist = 1; /* 0 if --ignore-freelist is specified */
7671 int bRowids = 1; /* 0 if --no-rowids */
7672 sqlite3_recover *p = 0;
7673 int i = 0;
7675 for(i=1; i<nArg; i++){
7676 char *z = azArg[i];
7677 int n;
7678 if( z[0]=='-' && z[1]=='-' ) z++;
7679 n = strlen30(z);
7680 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
7681 bFreelist = 0;
7682 }else
7683 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7684 /* This option determines the name of the ATTACH-ed database used
7685 ** internally by the recovery extension. The default is "" which
7686 ** means to use a temporary database that is automatically deleted
7687 ** when closed. This option is undocumented and might disappear at
7688 ** any moment. */
7689 i++;
7690 zRecoveryDb = azArg[i];
7691 }else
7692 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7693 i++;
7694 zLAF = azArg[i];
7695 }else
7696 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7697 bRowids = 0;
7699 else{
7700 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
7701 showHelp(pState->out, azArg[0]);
7702 return 1;
7706 p = sqlite3_recover_init_sql(
7707 pState->db, "main", recoverSqlCb, (void*)pState
7710 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
7711 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
7712 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
7713 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
7715 sqlite3_recover_run(p);
7716 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
7717 const char *zErr = sqlite3_recover_errmsg(p);
7718 int errCode = sqlite3_recover_errcode(p);
7719 raw_printf(stderr, "sql error: %s (%d)\n", zErr, errCode);
7721 rc = sqlite3_recover_finish(p);
7722 return rc;
7724 #endif /* SQLITE_SHELL_HAVE_RECOVER */
7728 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7729 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7730 * close db and set it to 0, and return the columns spec, to later
7731 * be sqlite3_free()'ed by the caller.
7732 * The return is 0 when either:
7733 * (a) The db was not initialized and zCol==0 (There are no columns.)
7734 * (b) zCol!=0 (Column was added, db initialized as needed.)
7735 * The 3rd argument, pRenamed, references an out parameter. If the
7736 * pointer is non-zero, its referent will be set to a summary of renames
7737 * done if renaming was necessary, or set to 0 if none was done. The out
7738 * string (if any) must be sqlite3_free()'ed by the caller.
7740 #ifdef SHELL_DEBUG
7741 #define rc_err_oom_die(rc) \
7742 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7743 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7744 fprintf(stderr,"E:%d\n",rc), assert(0)
7745 #else
7746 static void rc_err_oom_die(int rc){
7747 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7748 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7750 #endif
7752 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7753 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7754 #else /* Otherwise, memory is faster/better for the transient DB. */
7755 static const char *zCOL_DB = ":memory:";
7756 #endif
7758 /* Define character (as C string) to separate generated column ordinal
7759 * from protected part of incoming column names. This defaults to "_"
7760 * so that incoming column identifiers that did not need not be quoted
7761 * remain usable without being quoted. It must be one character.
7763 #ifndef SHELL_AUTOCOLUMN_SEP
7764 # define AUTOCOLUMN_SEP "_"
7765 #else
7766 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7767 #endif
7769 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7770 /* Queries and D{D,M}L used here */
7771 static const char * const zTabMake = "\
7772 CREATE TABLE ColNames(\
7773 cpos INTEGER PRIMARY KEY,\
7774 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7775 CREATE VIEW RepeatedNames AS \
7776 SELECT DISTINCT t.name FROM ColNames t \
7777 WHERE t.name COLLATE NOCASE IN (\
7778 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7781 static const char * const zTabFill = "\
7782 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7783 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7785 static const char * const zHasDupes = "\
7786 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7787 <count(name) FROM ColNames\
7789 #ifdef SHELL_COLUMN_RENAME_CLEAN
7790 static const char * const zDedoctor = "\
7791 UPDATE ColNames SET chop=iif(\
7792 (substring(name,nlen,1) BETWEEN '0' AND '9')\
7793 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
7794 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
7798 #endif
7799 static const char * const zSetReps = "\
7800 UPDATE ColNames AS t SET reps=\
7801 (SELECT count(*) FROM ColNames d \
7802 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
7803 COLLATE NOCASE\
7806 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
7807 static const char * const zColDigits = "\
7808 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
7810 #else
7811 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
7812 static const char * const zColDigits = "\
7813 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
7814 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
7815 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
7817 #endif
7818 static const char * const zRenameRank =
7819 #ifdef SHELL_COLUMN_RENAME_CLEAN
7820 "UPDATE ColNames AS t SET suff="
7821 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
7822 #else /* ...RENAME_MINIMAL_ONE_PASS */
7823 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
7824 " SELECT 0 AS nlz"
7825 " UNION"
7826 " SELECT nlz+1 AS nlz FROM Lzn"
7827 " WHERE EXISTS("
7828 " SELECT 1"
7829 " FROM ColNames t, ColNames o"
7830 " WHERE"
7831 " iif(t.name IN (SELECT * FROM RepeatedNames),"
7832 " printf('%s"AUTOCOLUMN_SEP"%s',"
7833 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
7834 " t.name"
7835 " )"
7836 " ="
7837 " iif(o.name IN (SELECT * FROM RepeatedNames),"
7838 " printf('%s"AUTOCOLUMN_SEP"%s',"
7839 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
7840 " o.name"
7841 " )"
7842 " COLLATE NOCASE"
7843 " AND o.cpos<>t.cpos"
7844 " GROUP BY t.cpos"
7845 " )"
7846 ") UPDATE Colnames AS t SET"
7847 " chop = 0," /* No chopping, never touch incoming names. */
7848 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
7849 " printf('"AUTOCOLUMN_SEP"%s', substring("
7850 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
7851 " ''"
7852 " )"
7853 #endif
7855 static const char * const zCollectVar = "\
7856 SELECT\
7857 '('||x'0a'\
7858 || group_concat(\
7859 cname||' TEXT',\
7860 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
7861 ||')' AS ColsSpec \
7862 FROM (\
7863 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
7864 FROM ColNames ORDER BY cpos\
7866 static const char * const zRenamesDone =
7867 "SELECT group_concat("
7868 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
7869 " ','||x'0a')"
7870 "FROM ColNames WHERE suff<>'' OR chop!=0"
7872 int rc;
7873 sqlite3_stmt *pStmt = 0;
7874 assert(pDb!=0);
7875 if( zColNew ){
7876 /* Add initial or additional column. Init db if necessary. */
7877 if( *pDb==0 ){
7878 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
7879 #ifdef SHELL_COLFIX_DB
7880 if(*zCOL_DB!=':')
7881 sqlite3_exec(*pDb,"drop table if exists ColNames;"
7882 "drop view if exists RepeatedNames;",0,0,0);
7883 #endif
7884 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
7885 rc_err_oom_die(rc);
7887 assert(*pDb!=0);
7888 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
7889 rc_err_oom_die(rc);
7890 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
7891 rc_err_oom_die(rc);
7892 rc = sqlite3_step(pStmt);
7893 rc_err_oom_die(rc);
7894 sqlite3_finalize(pStmt);
7895 return 0;
7896 }else if( *pDb==0 ){
7897 return 0;
7898 }else{
7899 /* Formulate the columns spec, close the DB, zero *pDb. */
7900 char *zColsSpec = 0;
7901 int hasDupes = db_int(*pDb, zHasDupes);
7902 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
7903 if( hasDupes ){
7904 #ifdef SHELL_COLUMN_RENAME_CLEAN
7905 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
7906 rc_err_oom_die(rc);
7907 #endif
7908 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
7909 rc_err_oom_die(rc);
7910 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
7911 rc_err_oom_die(rc);
7912 sqlite3_bind_int(pStmt, 1, nDigits);
7913 rc = sqlite3_step(pStmt);
7914 sqlite3_finalize(pStmt);
7915 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
7917 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
7918 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
7919 rc_err_oom_die(rc);
7920 rc = sqlite3_step(pStmt);
7921 if( rc==SQLITE_ROW ){
7922 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7923 }else{
7924 zColsSpec = 0;
7926 if( pzRenamed!=0 ){
7927 if( !hasDupes ) *pzRenamed = 0;
7928 else{
7929 sqlite3_finalize(pStmt);
7930 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
7931 && SQLITE_ROW==sqlite3_step(pStmt) ){
7932 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
7933 }else
7934 *pzRenamed = 0;
7937 sqlite3_finalize(pStmt);
7938 sqlite3_close(*pDb);
7939 *pDb = 0;
7940 return zColsSpec;
7945 ** If an input line begins with "." then invoke this routine to
7946 ** process that line.
7948 ** Return 1 on error, 2 to exit, and 0 otherwise.
7950 static int do_meta_command(char *zLine, ShellState *p){
7951 int h = 1;
7952 int nArg = 0;
7953 int n, c;
7954 int rc = 0;
7955 char *azArg[52];
7957 #ifndef SQLITE_OMIT_VIRTUALTABLE
7958 if( p->expert.pExpert ){
7959 expertFinish(p, 1, 0);
7961 #endif
7963 /* Parse the input line into tokens.
7965 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
7966 while( IsSpace(zLine[h]) ){ h++; }
7967 if( zLine[h]==0 ) break;
7968 if( zLine[h]=='\'' || zLine[h]=='"' ){
7969 int delim = zLine[h++];
7970 azArg[nArg++] = &zLine[h];
7971 while( zLine[h] && zLine[h]!=delim ){
7972 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7973 h++;
7975 if( zLine[h]==delim ){
7976 zLine[h++] = 0;
7978 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7979 }else{
7980 azArg[nArg++] = &zLine[h];
7981 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7982 if( zLine[h] ) zLine[h++] = 0;
7983 resolve_backslashes(azArg[nArg-1]);
7986 azArg[nArg] = 0;
7988 /* Process the input line.
7990 if( nArg==0 ) return 0; /* no tokens, no error */
7991 n = strlen30(azArg[0]);
7992 c = azArg[0][0];
7993 clearTempFile(p);
7995 #ifndef SQLITE_OMIT_AUTHORIZATION
7996 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
7997 if( nArg!=2 ){
7998 raw_printf(stderr, "Usage: .auth ON|OFF\n");
7999 rc = 1;
8000 goto meta_command_exit;
8002 open_db(p, 0);
8003 if( booleanValue(azArg[1]) ){
8004 sqlite3_set_authorizer(p->db, shellAuth, p);
8005 }else if( p->bSafeModePersist ){
8006 sqlite3_set_authorizer(p->db, safeModeAuth, p);
8007 }else{
8008 sqlite3_set_authorizer(p->db, 0, 0);
8010 }else
8011 #endif
8013 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8014 && !defined(SQLITE_SHELL_FIDDLE)
8015 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
8016 open_db(p, 0);
8017 failIfSafeMode(p, "cannot run .archive in safe mode");
8018 rc = arDotCommand(p, 0, azArg, nArg);
8019 }else
8020 #endif
8022 #ifndef SQLITE_SHELL_FIDDLE
8023 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
8024 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
8026 const char *zDestFile = 0;
8027 const char *zDb = 0;
8028 sqlite3 *pDest;
8029 sqlite3_backup *pBackup;
8030 int j;
8031 int bAsync = 0;
8032 const char *zVfs = 0;
8033 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8034 for(j=1; j<nArg; j++){
8035 const char *z = azArg[j];
8036 if( z[0]=='-' ){
8037 if( z[1]=='-' ) z++;
8038 if( cli_strcmp(z, "-append")==0 ){
8039 zVfs = "apndvfs";
8040 }else
8041 if( cli_strcmp(z, "-async")==0 ){
8042 bAsync = 1;
8043 }else
8045 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
8046 return 1;
8048 }else if( zDestFile==0 ){
8049 zDestFile = azArg[j];
8050 }else if( zDb==0 ){
8051 zDb = zDestFile;
8052 zDestFile = azArg[j];
8053 }else{
8054 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8055 return 1;
8058 if( zDestFile==0 ){
8059 raw_printf(stderr, "missing FILENAME argument on .backup\n");
8060 return 1;
8062 if( zDb==0 ) zDb = "main";
8063 rc = sqlite3_open_v2(zDestFile, &pDest,
8064 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8065 if( rc!=SQLITE_OK ){
8066 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
8067 close_db(pDest);
8068 return 1;
8070 if( bAsync ){
8071 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8072 0, 0, 0);
8074 open_db(p, 0);
8075 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8076 if( pBackup==0 ){
8077 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8078 close_db(pDest);
8079 return 1;
8081 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8082 sqlite3_backup_finish(pBackup);
8083 if( rc==SQLITE_DONE ){
8084 rc = 0;
8085 }else{
8086 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
8087 rc = 1;
8089 close_db(pDest);
8090 }else
8091 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8093 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
8094 if( nArg==2 ){
8095 bail_on_error = booleanValue(azArg[1]);
8096 }else{
8097 raw_printf(stderr, "Usage: .bail on|off\n");
8098 rc = 1;
8100 }else
8102 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
8103 if( nArg==2 ){
8104 if( booleanValue(azArg[1]) ){
8105 setBinaryMode(p->out, 1);
8106 }else{
8107 setTextMode(p->out, 1);
8109 }else{
8110 raw_printf(stderr, "Usage: .binary on|off\n");
8111 rc = 1;
8113 }else
8115 /* The undocumented ".breakpoint" command causes a call to the no-op
8116 ** routine named test_breakpoint().
8118 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
8119 test_breakpoint();
8120 }else
8122 #ifndef SQLITE_SHELL_FIDDLE
8123 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
8124 failIfSafeMode(p, "cannot run .cd in safe mode");
8125 if( nArg==2 ){
8126 #if defined(_WIN32) || defined(WIN32)
8127 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8128 rc = !SetCurrentDirectoryW(z);
8129 sqlite3_free(z);
8130 #else
8131 rc = chdir(azArg[1]);
8132 #endif
8133 if( rc ){
8134 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
8135 rc = 1;
8137 }else{
8138 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
8139 rc = 1;
8141 }else
8142 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8144 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
8145 if( nArg==2 ){
8146 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8147 }else{
8148 raw_printf(stderr, "Usage: .changes on|off\n");
8149 rc = 1;
8151 }else
8153 #ifndef SQLITE_SHELL_FIDDLE
8154 /* Cancel output redirection, if it is currently set (by .testcase)
8155 ** Then read the content of the testcase-out.txt file and compare against
8156 ** azArg[1]. If there are differences, report an error and exit.
8158 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
8159 char *zRes = 0;
8160 output_reset(p);
8161 if( nArg!=2 ){
8162 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
8163 rc = 2;
8164 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8165 rc = 2;
8166 }else if( testcase_glob(azArg[1],zRes)==0 ){
8167 utf8_printf(stderr,
8168 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
8169 p->zTestcase, azArg[1], zRes);
8170 rc = 1;
8171 }else{
8172 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
8173 p->nCheck++;
8175 sqlite3_free(zRes);
8176 }else
8177 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8179 #ifndef SQLITE_SHELL_FIDDLE
8180 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
8181 failIfSafeMode(p, "cannot run .clone in safe mode");
8182 if( nArg==2 ){
8183 tryToClone(p, azArg[1]);
8184 }else{
8185 raw_printf(stderr, "Usage: .clone FILENAME\n");
8186 rc = 1;
8188 }else
8189 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8191 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
8192 if( nArg==1 ){
8193 /* List available connections */
8194 int i;
8195 for(i=0; i<ArraySize(p->aAuxDb); i++){
8196 const char *zFile = p->aAuxDb[i].zDbFilename;
8197 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8198 zFile = "(not open)";
8199 }else if( zFile==0 ){
8200 zFile = "(memory)";
8201 }else if( zFile[0]==0 ){
8202 zFile = "(temporary-file)";
8204 if( p->pAuxDb == &p->aAuxDb[i] ){
8205 utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
8206 }else if( p->aAuxDb[i].db!=0 ){
8207 utf8_printf(stdout, " %d: %s\n", i, zFile);
8210 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8211 int i = azArg[1][0] - '0';
8212 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8213 p->pAuxDb->db = p->db;
8214 p->pAuxDb = &p->aAuxDb[i];
8215 globalDb = p->db = p->pAuxDb->db;
8216 p->pAuxDb->db = 0;
8218 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
8219 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8220 int i = azArg[2][0] - '0';
8221 if( i<0 || i>=ArraySize(p->aAuxDb) ){
8222 /* No-op */
8223 }else if( p->pAuxDb == &p->aAuxDb[i] ){
8224 raw_printf(stderr, "cannot close the active database connection\n");
8225 rc = 1;
8226 }else if( p->aAuxDb[i].db ){
8227 session_close_all(p, i);
8228 close_db(p->aAuxDb[i].db);
8229 p->aAuxDb[i].db = 0;
8231 }else{
8232 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8233 rc = 1;
8235 }else
8237 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
8238 char **azName = 0;
8239 int nName = 0;
8240 sqlite3_stmt *pStmt;
8241 int i;
8242 open_db(p, 0);
8243 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8244 if( rc ){
8245 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8246 rc = 1;
8247 }else{
8248 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8249 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8250 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8251 if( zSchema==0 || zFile==0 ) continue;
8252 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8253 shell_check_oom(azName);
8254 azName[nName*2] = strdup(zSchema);
8255 azName[nName*2+1] = strdup(zFile);
8256 nName++;
8259 sqlite3_finalize(pStmt);
8260 for(i=0; i<nName; i++){
8261 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8262 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8263 const char *z = azName[i*2+1];
8264 utf8_printf(p->out, "%s: %s %s%s\n",
8265 azName[i*2],
8266 z && z[0] ? z : "\"\"",
8267 bRdonly ? "r/o" : "r/w",
8268 eTxn==SQLITE_TXN_NONE ? "" :
8269 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8270 free(azName[i*2]);
8271 free(azName[i*2+1]);
8273 sqlite3_free(azName);
8274 }else
8276 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
8277 static const struct DbConfigChoices {
8278 const char *zName;
8279 int op;
8280 } aDbConfig[] = {
8281 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
8282 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
8283 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
8284 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
8285 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
8286 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8287 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
8288 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8289 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8290 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8291 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8292 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8293 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
8294 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER },
8295 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8296 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
8297 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
8298 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8300 int ii, v;
8301 open_db(p, 0);
8302 for(ii=0; ii<ArraySize(aDbConfig); ii++){
8303 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8304 if( nArg>=3 ){
8305 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8307 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8308 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8309 if( nArg>1 ) break;
8311 if( nArg>1 && ii==ArraySize(aDbConfig) ){
8312 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
8313 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
8315 }else
8317 #if SQLITE_SHELL_HAVE_RECOVER
8318 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
8319 rc = shell_dbinfo_command(p, nArg, azArg);
8320 }else
8322 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
8323 open_db(p, 0);
8324 rc = recoverDatabaseCmd(p, nArg, azArg);
8325 }else
8326 #endif /* SQLITE_SHELL_HAVE_RECOVER */
8328 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
8329 char *zLike = 0;
8330 char *zSql;
8331 int i;
8332 int savedShowHeader = p->showHeader;
8333 int savedShellFlags = p->shellFlgs;
8334 ShellClearFlag(p,
8335 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8336 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8337 for(i=1; i<nArg; i++){
8338 if( azArg[i][0]=='-' ){
8339 const char *z = azArg[i]+1;
8340 if( z[0]=='-' ) z++;
8341 if( cli_strcmp(z,"preserve-rowids")==0 ){
8342 #ifdef SQLITE_OMIT_VIRTUALTABLE
8343 raw_printf(stderr, "The --preserve-rowids option is not compatible"
8344 " with SQLITE_OMIT_VIRTUALTABLE\n");
8345 rc = 1;
8346 sqlite3_free(zLike);
8347 goto meta_command_exit;
8348 #else
8349 ShellSetFlag(p, SHFLG_PreserveRowid);
8350 #endif
8351 }else
8352 if( cli_strcmp(z,"newlines")==0 ){
8353 ShellSetFlag(p, SHFLG_Newlines);
8354 }else
8355 if( cli_strcmp(z,"data-only")==0 ){
8356 ShellSetFlag(p, SHFLG_DumpDataOnly);
8357 }else
8358 if( cli_strcmp(z,"nosys")==0 ){
8359 ShellSetFlag(p, SHFLG_DumpNoSys);
8360 }else
8362 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8363 rc = 1;
8364 sqlite3_free(zLike);
8365 goto meta_command_exit;
8367 }else{
8368 /* azArg[i] contains a LIKE pattern. This ".dump" request should
8369 ** only dump data for tables for which either the table name matches
8370 ** the LIKE pattern, or the table appears to be a shadow table of
8371 ** a virtual table for which the name matches the LIKE pattern.
8373 char *zExpr = sqlite3_mprintf(
8374 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8375 " SELECT 1 FROM sqlite_schema WHERE "
8376 " name LIKE %Q ESCAPE '\\' AND"
8377 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8378 " substr(o.name, 1, length(name)+1) == (name||'_')"
8379 ")", azArg[i], azArg[i]
8382 if( zLike ){
8383 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8384 }else{
8385 zLike = zExpr;
8390 open_db(p, 0);
8392 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8393 /* When playing back a "dump", the content might appear in an order
8394 ** which causes immediate foreign key constraints to be violated.
8395 ** So disable foreign-key constraint enforcement to prevent problems. */
8396 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
8397 raw_printf(p->out, "BEGIN TRANSACTION;\n");
8399 p->writableSchema = 0;
8400 p->showHeader = 0;
8401 /* Set writable_schema=ON since doing so forces SQLite to initialize
8402 ** as much of the schema as it can even if the sqlite_schema table is
8403 ** corrupt. */
8404 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8405 p->nErr = 0;
8406 if( zLike==0 ) zLike = sqlite3_mprintf("true");
8407 zSql = sqlite3_mprintf(
8408 "SELECT name, type, sql FROM sqlite_schema AS o "
8409 "WHERE (%s) AND type=='table'"
8410 " AND sql NOT NULL"
8411 " ORDER BY tbl_name='sqlite_sequence', rowid",
8412 zLike
8414 run_schema_dump_query(p,zSql);
8415 sqlite3_free(zSql);
8416 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8417 zSql = sqlite3_mprintf(
8418 "SELECT sql FROM sqlite_schema AS o "
8419 "WHERE (%s) AND sql NOT NULL"
8420 " AND type IN ('index','trigger','view')",
8421 zLike
8423 run_table_dump_query(p, zSql);
8424 sqlite3_free(zSql);
8426 sqlite3_free(zLike);
8427 if( p->writableSchema ){
8428 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
8429 p->writableSchema = 0;
8431 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8432 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8433 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8434 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8436 p->showHeader = savedShowHeader;
8437 p->shellFlgs = savedShellFlags;
8438 }else
8440 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8441 if( nArg==2 ){
8442 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8443 }else{
8444 raw_printf(stderr, "Usage: .echo on|off\n");
8445 rc = 1;
8447 }else
8449 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8450 if( nArg==2 ){
8451 p->autoEQPtest = 0;
8452 if( p->autoEQPtrace ){
8453 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8454 p->autoEQPtrace = 0;
8456 if( cli_strcmp(azArg[1],"full")==0 ){
8457 p->autoEQP = AUTOEQP_full;
8458 }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8459 p->autoEQP = AUTOEQP_trigger;
8460 #ifdef SQLITE_DEBUG
8461 }else if( cli_strcmp(azArg[1],"test")==0 ){
8462 p->autoEQP = AUTOEQP_on;
8463 p->autoEQPtest = 1;
8464 }else if( cli_strcmp(azArg[1],"trace")==0 ){
8465 p->autoEQP = AUTOEQP_full;
8466 p->autoEQPtrace = 1;
8467 open_db(p, 0);
8468 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8469 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8470 #endif
8471 }else{
8472 p->autoEQP = (u8)booleanValue(azArg[1]);
8474 }else{
8475 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
8476 rc = 1;
8478 }else
8480 #ifndef SQLITE_SHELL_FIDDLE
8481 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8482 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8483 rc = 2;
8484 }else
8485 #endif
8487 /* The ".explain" command is automatic now. It is largely pointless. It
8488 ** retained purely for backwards compatibility */
8489 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8490 int val = 1;
8491 if( nArg>=2 ){
8492 if( cli_strcmp(azArg[1],"auto")==0 ){
8493 val = 99;
8494 }else{
8495 val = booleanValue(azArg[1]);
8498 if( val==1 && p->mode!=MODE_Explain ){
8499 p->normalMode = p->mode;
8500 p->mode = MODE_Explain;
8501 p->autoExplain = 0;
8502 }else if( val==0 ){
8503 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8504 p->autoExplain = 0;
8505 }else if( val==99 ){
8506 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8507 p->autoExplain = 1;
8509 }else
8511 #ifndef SQLITE_OMIT_VIRTUALTABLE
8512 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8513 if( p->bSafeMode ){
8514 raw_printf(stderr,
8515 "Cannot run experimental commands such as \"%s\" in safe mode\n",
8516 azArg[0]);
8517 rc = 1;
8518 }else{
8519 open_db(p, 0);
8520 expertDotCommand(p, azArg, nArg);
8522 }else
8523 #endif
8525 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8526 static const struct {
8527 const char *zCtrlName; /* Name of a test-control option */
8528 int ctrlCode; /* Integer code for that option */
8529 const char *zUsage; /* Usage notes */
8530 } aCtrl[] = {
8531 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
8532 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
8533 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
8534 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
8535 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
8536 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
8537 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
8538 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
8539 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
8540 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
8541 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
8543 int filectrl = -1;
8544 int iCtrl = -1;
8545 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
8546 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
8547 int n2, i;
8548 const char *zCmd = 0;
8549 const char *zSchema = 0;
8551 open_db(p, 0);
8552 zCmd = nArg>=2 ? azArg[1] : "help";
8554 if( zCmd[0]=='-'
8555 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8556 && nArg>=4
8558 zSchema = azArg[2];
8559 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8560 nArg -= 2;
8561 zCmd = azArg[1];
8564 /* The argument can optionally begin with "-" or "--" */
8565 if( zCmd[0]=='-' && zCmd[1] ){
8566 zCmd++;
8567 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8570 /* --help lists all file-controls */
8571 if( cli_strcmp(zCmd,"help")==0 ){
8572 utf8_printf(p->out, "Available file-controls:\n");
8573 for(i=0; i<ArraySize(aCtrl); i++){
8574 utf8_printf(p->out, " .filectrl %s %s\n",
8575 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8577 rc = 1;
8578 goto meta_command_exit;
8581 /* convert filectrl text option to value. allow any unique prefix
8582 ** of the option name, or a numerical value. */
8583 n2 = strlen30(zCmd);
8584 for(i=0; i<ArraySize(aCtrl); i++){
8585 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8586 if( filectrl<0 ){
8587 filectrl = aCtrl[i].ctrlCode;
8588 iCtrl = i;
8589 }else{
8590 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
8591 "Use \".filectrl --help\" for help\n", zCmd);
8592 rc = 1;
8593 goto meta_command_exit;
8597 if( filectrl<0 ){
8598 utf8_printf(stderr,"Error: unknown file-control: %s\n"
8599 "Use \".filectrl --help\" for help\n", zCmd);
8600 }else{
8601 switch(filectrl){
8602 case SQLITE_FCNTL_SIZE_LIMIT: {
8603 if( nArg!=2 && nArg!=3 ) break;
8604 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8605 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8606 isOk = 1;
8607 break;
8609 case SQLITE_FCNTL_LOCK_TIMEOUT:
8610 case SQLITE_FCNTL_CHUNK_SIZE: {
8611 int x;
8612 if( nArg!=3 ) break;
8613 x = (int)integerValue(azArg[2]);
8614 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8615 isOk = 2;
8616 break;
8618 case SQLITE_FCNTL_PERSIST_WAL:
8619 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8620 int x;
8621 if( nArg!=2 && nArg!=3 ) break;
8622 x = nArg==3 ? booleanValue(azArg[2]) : -1;
8623 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8624 iRes = x;
8625 isOk = 1;
8626 break;
8628 case SQLITE_FCNTL_DATA_VERSION:
8629 case SQLITE_FCNTL_HAS_MOVED: {
8630 int x;
8631 if( nArg!=2 ) break;
8632 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8633 iRes = x;
8634 isOk = 1;
8635 break;
8637 case SQLITE_FCNTL_TEMPFILENAME: {
8638 char *z = 0;
8639 if( nArg!=2 ) break;
8640 sqlite3_file_control(p->db, zSchema, filectrl, &z);
8641 if( z ){
8642 utf8_printf(p->out, "%s\n", z);
8643 sqlite3_free(z);
8645 isOk = 2;
8646 break;
8648 case SQLITE_FCNTL_RESERVE_BYTES: {
8649 int x;
8650 if( nArg>=3 ){
8651 x = atoi(azArg[2]);
8652 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8654 x = -1;
8655 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8656 utf8_printf(p->out,"%d\n", x);
8657 isOk = 2;
8658 break;
8662 if( isOk==0 && iCtrl>=0 ){
8663 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8664 rc = 1;
8665 }else if( isOk==1 ){
8666 char zBuf[100];
8667 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8668 raw_printf(p->out, "%s\n", zBuf);
8670 }else
8672 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8673 ShellState data;
8674 int doStats = 0;
8675 memcpy(&data, p, sizeof(data));
8676 data.showHeader = 0;
8677 data.cMode = data.mode = MODE_Semi;
8678 if( nArg==2 && optionMatch(azArg[1], "indent") ){
8679 data.cMode = data.mode = MODE_Pretty;
8680 nArg = 1;
8682 if( nArg!=1 ){
8683 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
8684 rc = 1;
8685 goto meta_command_exit;
8687 open_db(p, 0);
8688 rc = sqlite3_exec(p->db,
8689 "SELECT sql FROM"
8690 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8691 " FROM sqlite_schema UNION ALL"
8692 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8693 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8694 "ORDER BY x",
8695 callback, &data, 0
8697 if( rc==SQLITE_OK ){
8698 sqlite3_stmt *pStmt;
8699 rc = sqlite3_prepare_v2(p->db,
8700 "SELECT rowid FROM sqlite_schema"
8701 " WHERE name GLOB 'sqlite_stat[134]'",
8702 -1, &pStmt, 0);
8703 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8704 sqlite3_finalize(pStmt);
8706 if( doStats==0 ){
8707 raw_printf(p->out, "/* No STAT tables available */\n");
8708 }else{
8709 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8710 data.cMode = data.mode = MODE_Insert;
8711 data.zDestTable = "sqlite_stat1";
8712 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8713 data.zDestTable = "sqlite_stat4";
8714 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8715 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
8717 }else
8719 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8720 if( nArg==2 ){
8721 p->showHeader = booleanValue(azArg[1]);
8722 p->shellFlgs |= SHFLG_HeaderSet;
8723 }else{
8724 raw_printf(stderr, "Usage: .headers on|off\n");
8725 rc = 1;
8727 }else
8729 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
8730 if( nArg>=2 ){
8731 n = showHelp(p->out, azArg[1]);
8732 if( n==0 ){
8733 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
8735 }else{
8736 showHelp(p->out, 0);
8738 }else
8740 #ifndef SQLITE_SHELL_FIDDLE
8741 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
8742 char *zTable = 0; /* Insert data into this table */
8743 char *zSchema = 0; /* within this schema (may default to "main") */
8744 char *zFile = 0; /* Name of file to extra content from */
8745 sqlite3_stmt *pStmt = NULL; /* A statement */
8746 int nCol; /* Number of columns in the table */
8747 int nByte; /* Number of bytes in an SQL string */
8748 int i, j; /* Loop counters */
8749 int needCommit; /* True to COMMIT or ROLLBACK at end */
8750 int nSep; /* Number of bytes in p->colSeparator[] */
8751 char *zSql; /* An SQL statement */
8752 char *zFullTabName; /* Table name with schema if applicable */
8753 ImportCtx sCtx; /* Reader context */
8754 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
8755 int eVerbose = 0; /* Larger for more console output */
8756 int nSkip = 0; /* Initial lines to skip */
8757 int useOutputMode = 1; /* Use output mode to determine separators */
8758 char *zCreate = 0; /* CREATE TABLE statement text */
8760 failIfSafeMode(p, "cannot run .import in safe mode");
8761 memset(&sCtx, 0, sizeof(sCtx));
8762 if( p->mode==MODE_Ascii ){
8763 xRead = ascii_read_one_field;
8764 }else{
8765 xRead = csv_read_one_field;
8767 rc = 1;
8768 for(i=1; i<nArg; i++){
8769 char *z = azArg[i];
8770 if( z[0]=='-' && z[1]=='-' ) z++;
8771 if( z[0]!='-' ){
8772 if( zFile==0 ){
8773 zFile = z;
8774 }else if( zTable==0 ){
8775 zTable = z;
8776 }else{
8777 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
8778 showHelp(p->out, "import");
8779 goto meta_command_exit;
8781 }else if( cli_strcmp(z,"-v")==0 ){
8782 eVerbose++;
8783 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
8784 zSchema = azArg[++i];
8785 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
8786 nSkip = integerValue(azArg[++i]);
8787 }else if( cli_strcmp(z,"-ascii")==0 ){
8788 sCtx.cColSep = SEP_Unit[0];
8789 sCtx.cRowSep = SEP_Record[0];
8790 xRead = ascii_read_one_field;
8791 useOutputMode = 0;
8792 }else if( cli_strcmp(z,"-csv")==0 ){
8793 sCtx.cColSep = ',';
8794 sCtx.cRowSep = '\n';
8795 xRead = csv_read_one_field;
8796 useOutputMode = 0;
8797 }else{
8798 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
8799 showHelp(p->out, "import");
8800 goto meta_command_exit;
8803 if( zTable==0 ){
8804 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
8805 zFile==0 ? "FILE" : "TABLE");
8806 showHelp(p->out, "import");
8807 goto meta_command_exit;
8809 seenInterrupt = 0;
8810 open_db(p, 0);
8811 if( useOutputMode ){
8812 /* If neither the --csv or --ascii options are specified, then set
8813 ** the column and row separator characters from the output mode. */
8814 nSep = strlen30(p->colSeparator);
8815 if( nSep==0 ){
8816 raw_printf(stderr,
8817 "Error: non-null column separator required for import\n");
8818 goto meta_command_exit;
8820 if( nSep>1 ){
8821 raw_printf(stderr,
8822 "Error: multi-character column separators not allowed"
8823 " for import\n");
8824 goto meta_command_exit;
8826 nSep = strlen30(p->rowSeparator);
8827 if( nSep==0 ){
8828 raw_printf(stderr,
8829 "Error: non-null row separator required for import\n");
8830 goto meta_command_exit;
8832 if( nSep==2 && p->mode==MODE_Csv
8833 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
8835 /* When importing CSV (only), if the row separator is set to the
8836 ** default output row separator, change it to the default input
8837 ** row separator. This avoids having to maintain different input
8838 ** and output row separators. */
8839 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8840 nSep = strlen30(p->rowSeparator);
8842 if( nSep>1 ){
8843 raw_printf(stderr, "Error: multi-character row separators not allowed"
8844 " for import\n");
8845 goto meta_command_exit;
8847 sCtx.cColSep = (u8)p->colSeparator[0];
8848 sCtx.cRowSep = (u8)p->rowSeparator[0];
8850 sCtx.zFile = zFile;
8851 sCtx.nLine = 1;
8852 if( sCtx.zFile[0]=='|' ){
8853 #ifdef SQLITE_OMIT_POPEN
8854 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8855 goto meta_command_exit;
8856 #else
8857 sCtx.in = popen(sCtx.zFile+1, "r");
8858 sCtx.zFile = "<pipe>";
8859 sCtx.xCloser = pclose;
8860 #endif
8861 }else{
8862 sCtx.in = fopen(sCtx.zFile, "rb");
8863 sCtx.xCloser = fclose;
8865 if( sCtx.in==0 ){
8866 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
8867 goto meta_command_exit;
8869 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
8870 char zSep[2];
8871 zSep[1] = 0;
8872 zSep[0] = sCtx.cColSep;
8873 utf8_printf(p->out, "Column separator ");
8874 output_c_string(p->out, zSep);
8875 utf8_printf(p->out, ", row separator ");
8876 zSep[0] = sCtx.cRowSep;
8877 output_c_string(p->out, zSep);
8878 utf8_printf(p->out, "\n");
8880 sCtx.z = sqlite3_malloc64(120);
8881 if( sCtx.z==0 ){
8882 import_cleanup(&sCtx);
8883 shell_out_of_memory();
8885 /* Below, resources must be freed before exit. */
8886 while( (nSkip--)>0 ){
8887 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
8889 if( zSchema!=0 ){
8890 zFullTabName = sqlite3_mprintf("\"%w\".\"%w\"", zSchema, zTable);
8891 }else{
8892 zFullTabName = sqlite3_mprintf("\"%w\"", zTable);
8894 zSql = sqlite3_mprintf("SELECT * FROM %s", zFullTabName);
8895 if( zSql==0 || zFullTabName==0 ){
8896 import_cleanup(&sCtx);
8897 shell_out_of_memory();
8899 nByte = strlen30(zSql);
8900 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8901 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
8902 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
8903 sqlite3 *dbCols = 0;
8904 char *zRenames = 0;
8905 char *zColDefs;
8906 zCreate = sqlite3_mprintf("CREATE TABLE %s", zFullTabName);
8907 while( xRead(&sCtx) ){
8908 zAutoColumn(sCtx.z, &dbCols, 0);
8909 if( sCtx.cTerm!=sCtx.cColSep ) break;
8911 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
8912 if( zRenames!=0 ){
8913 utf8_printf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
8914 "Columns renamed during .import %s due to duplicates:\n"
8915 "%s\n", sCtx.zFile, zRenames);
8916 sqlite3_free(zRenames);
8918 assert(dbCols==0);
8919 if( zColDefs==0 ){
8920 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
8921 import_fail:
8922 sqlite3_free(zCreate);
8923 sqlite3_free(zSql);
8924 sqlite3_free(zFullTabName);
8925 import_cleanup(&sCtx);
8926 rc = 1;
8927 goto meta_command_exit;
8929 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
8930 if( eVerbose>=1 ){
8931 utf8_printf(p->out, "%s\n", zCreate);
8933 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
8934 if( rc ){
8935 utf8_printf(stderr, "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
8936 goto import_fail;
8938 sqlite3_free(zCreate);
8939 zCreate = 0;
8940 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8942 if( rc ){
8943 if (pStmt) sqlite3_finalize(pStmt);
8944 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
8945 goto import_fail;
8947 sqlite3_free(zSql);
8948 nCol = sqlite3_column_count(pStmt);
8949 sqlite3_finalize(pStmt);
8950 pStmt = 0;
8951 if( nCol==0 ) return 0; /* no columns, no error */
8952 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
8953 if( zSql==0 ){
8954 import_cleanup(&sCtx);
8955 shell_out_of_memory();
8957 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO %s VALUES(?", zFullTabName);
8958 j = strlen30(zSql);
8959 for(i=1; i<nCol; i++){
8960 zSql[j++] = ',';
8961 zSql[j++] = '?';
8963 zSql[j++] = ')';
8964 zSql[j] = 0;
8965 if( eVerbose>=2 ){
8966 utf8_printf(p->out, "Insert using: %s\n", zSql);
8968 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8969 if( rc ){
8970 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8971 if (pStmt) sqlite3_finalize(pStmt);
8972 goto import_fail;
8974 sqlite3_free(zSql);
8975 sqlite3_free(zFullTabName);
8976 needCommit = sqlite3_get_autocommit(p->db);
8977 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
8979 int startLine = sCtx.nLine;
8980 for(i=0; i<nCol; i++){
8981 char *z = xRead(&sCtx);
8983 ** Did we reach end-of-file before finding any columns?
8984 ** If so, stop instead of NULL filling the remaining columns.
8986 if( z==0 && i==0 ) break;
8988 ** Did we reach end-of-file OR end-of-line before finding any
8989 ** columns in ASCII mode? If so, stop instead of NULL filling
8990 ** the remaining columns.
8992 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
8993 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
8994 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
8995 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
8996 "filling the rest with NULL\n",
8997 sCtx.zFile, startLine, nCol, i+1);
8998 i += 2;
8999 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9002 if( sCtx.cTerm==sCtx.cColSep ){
9004 xRead(&sCtx);
9005 i++;
9006 }while( sCtx.cTerm==sCtx.cColSep );
9007 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
9008 "extras ignored\n",
9009 sCtx.zFile, startLine, nCol, i);
9011 if( i>=nCol ){
9012 sqlite3_step(pStmt);
9013 rc = sqlite3_reset(pStmt);
9014 if( rc!=SQLITE_OK ){
9015 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
9016 startLine, sqlite3_errmsg(p->db));
9017 sCtx.nErr++;
9018 }else{
9019 sCtx.nRow++;
9022 }while( sCtx.cTerm!=EOF );
9024 import_cleanup(&sCtx);
9025 sqlite3_finalize(pStmt);
9026 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9027 if( eVerbose>0 ){
9028 utf8_printf(p->out,
9029 "Added %d rows with %d errors using %d lines of input\n",
9030 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9032 }else
9033 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9035 #ifndef SQLITE_UNTESTABLE
9036 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
9037 char *zSql;
9038 char *zCollist = 0;
9039 sqlite3_stmt *pStmt;
9040 int tnum = 0;
9041 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
9042 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9043 int i;
9044 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
9045 utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
9046 "imposter");
9047 rc = 1;
9048 goto meta_command_exit;
9050 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9051 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
9052 " .imposter off\n");
9053 /* Also allowed, but not documented:
9055 ** .imposter TABLE IMPOSTER
9057 ** where TABLE is a WITHOUT ROWID table. In that case, the
9058 ** imposter is another WITHOUT ROWID table with the columns in
9059 ** storage order. */
9060 rc = 1;
9061 goto meta_command_exit;
9063 open_db(p, 0);
9064 if( nArg==2 ){
9065 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9066 goto meta_command_exit;
9068 zSql = sqlite3_mprintf(
9069 "SELECT rootpage, 0 FROM sqlite_schema"
9070 " WHERE name='%q' AND type='index'"
9071 "UNION ALL "
9072 "SELECT rootpage, 1 FROM sqlite_schema"
9073 " WHERE name='%q' AND type='table'"
9074 " AND sql LIKE '%%without%%rowid%%'",
9075 azArg[1], azArg[1]
9077 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9078 sqlite3_free(zSql);
9079 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9080 tnum = sqlite3_column_int(pStmt, 0);
9081 isWO = sqlite3_column_int(pStmt, 1);
9083 sqlite3_finalize(pStmt);
9084 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9085 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9086 sqlite3_free(zSql);
9087 i = 0;
9088 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9089 char zLabel[20];
9090 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9091 i++;
9092 if( zCol==0 ){
9093 if( sqlite3_column_int(pStmt,1)==-1 ){
9094 zCol = "_ROWID_";
9095 }else{
9096 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9097 zCol = zLabel;
9100 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9101 lenPK = (int)strlen(zCollist);
9103 if( zCollist==0 ){
9104 zCollist = sqlite3_mprintf("\"%w\"", zCol);
9105 }else{
9106 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9109 sqlite3_finalize(pStmt);
9110 if( i==0 || tnum==0 ){
9111 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
9112 rc = 1;
9113 sqlite3_free(zCollist);
9114 goto meta_command_exit;
9116 if( lenPK==0 ) lenPK = 100000;
9117 zSql = sqlite3_mprintf(
9118 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9119 azArg[2], zCollist, lenPK, zCollist);
9120 sqlite3_free(zCollist);
9121 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9122 if( rc==SQLITE_OK ){
9123 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9124 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9125 if( rc ){
9126 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9127 }else{
9128 utf8_printf(stdout, "%s;\n", zSql);
9129 raw_printf(stdout,
9130 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9131 azArg[1], isWO ? "table" : "index"
9134 }else{
9135 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9136 rc = 1;
9138 sqlite3_free(zSql);
9139 }else
9140 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9142 #ifdef SQLITE_ENABLE_IOTRACE
9143 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
9144 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9145 if( iotrace && iotrace!=stdout ) fclose(iotrace);
9146 iotrace = 0;
9147 if( nArg<2 ){
9148 sqlite3IoTrace = 0;
9149 }else if( cli_strcmp(azArg[1], "-")==0 ){
9150 sqlite3IoTrace = iotracePrintf;
9151 iotrace = stdout;
9152 }else{
9153 iotrace = fopen(azArg[1], "w");
9154 if( iotrace==0 ){
9155 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9156 sqlite3IoTrace = 0;
9157 rc = 1;
9158 }else{
9159 sqlite3IoTrace = iotracePrintf;
9162 }else
9163 #endif
9165 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
9166 static const struct {
9167 const char *zLimitName; /* Name of a limit */
9168 int limitCode; /* Integer code for that limit */
9169 } aLimit[] = {
9170 { "length", SQLITE_LIMIT_LENGTH },
9171 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
9172 { "column", SQLITE_LIMIT_COLUMN },
9173 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
9174 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
9175 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
9176 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
9177 { "attached", SQLITE_LIMIT_ATTACHED },
9178 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
9179 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
9180 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
9181 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
9183 int i, n2;
9184 open_db(p, 0);
9185 if( nArg==1 ){
9186 for(i=0; i<ArraySize(aLimit); i++){
9187 printf("%20s %d\n", aLimit[i].zLimitName,
9188 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9190 }else if( nArg>3 ){
9191 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
9192 rc = 1;
9193 goto meta_command_exit;
9194 }else{
9195 int iLimit = -1;
9196 n2 = strlen30(azArg[1]);
9197 for(i=0; i<ArraySize(aLimit); i++){
9198 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9199 if( iLimit<0 ){
9200 iLimit = i;
9201 }else{
9202 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
9203 rc = 1;
9204 goto meta_command_exit;
9208 if( iLimit<0 ){
9209 utf8_printf(stderr, "unknown limit: \"%s\"\n"
9210 "enter \".limits\" with no arguments for a list.\n",
9211 azArg[1]);
9212 rc = 1;
9213 goto meta_command_exit;
9215 if( nArg==3 ){
9216 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9217 (int)integerValue(azArg[2]));
9219 printf("%20s %d\n", aLimit[iLimit].zLimitName,
9220 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9222 }else
9224 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
9225 open_db(p, 0);
9226 lintDotCommand(p, azArg, nArg);
9227 }else
9229 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9230 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
9231 const char *zFile, *zProc;
9232 char *zErrMsg = 0;
9233 failIfSafeMode(p, "cannot run .load in safe mode");
9234 if( nArg<2 || azArg[1][0]==0 ){
9235 /* Must have a non-empty FILE. (Will not load self.) */
9236 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
9237 rc = 1;
9238 goto meta_command_exit;
9240 zFile = azArg[1];
9241 zProc = nArg>=3 ? azArg[2] : 0;
9242 open_db(p, 0);
9243 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9244 if( rc!=SQLITE_OK ){
9245 utf8_printf(stderr, "Error: %s\n", zErrMsg);
9246 sqlite3_free(zErrMsg);
9247 rc = 1;
9249 }else
9250 #endif
9252 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
9253 if( nArg!=2 ){
9254 raw_printf(stderr, "Usage: .log FILENAME\n");
9255 rc = 1;
9256 }else{
9257 const char *zFile = azArg[1];
9258 if( p->bSafeMode
9259 && cli_strcmp(zFile,"on")!=0
9260 && cli_strcmp(zFile,"off")!=0
9262 raw_printf(stdout, "cannot set .log to anything other "
9263 "than \"on\" or \"off\"\n");
9264 zFile = "off";
9266 output_file_close(p->pLog);
9267 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
9268 p->pLog = output_file_open(zFile, 0);
9270 }else
9272 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
9273 const char *zMode = 0;
9274 const char *zTabname = 0;
9275 int i, n2;
9276 ColModeOpts cmOpts = ColModeOpts_default;
9277 for(i=1; i<nArg; i++){
9278 const char *z = azArg[i];
9279 if( optionMatch(z,"wrap") && i+1<nArg ){
9280 cmOpts.iWrap = integerValue(azArg[++i]);
9281 }else if( optionMatch(z,"ww") ){
9282 cmOpts.bWordWrap = 1;
9283 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9284 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9285 }else if( optionMatch(z,"quote") ){
9286 cmOpts.bQuote = 1;
9287 }else if( optionMatch(z,"noquote") ){
9288 cmOpts.bQuote = 0;
9289 }else if( zMode==0 ){
9290 zMode = z;
9291 /* Apply defaults for qbox pseudo-mode. If that
9292 * overwrites already-set values, user was informed of this.
9294 if( cli_strcmp(z, "qbox")==0 ){
9295 ColModeOpts cmo = ColModeOpts_default_qbox;
9296 zMode = "box";
9297 cmOpts = cmo;
9299 }else if( zTabname==0 ){
9300 zTabname = z;
9301 }else if( z[0]=='-' ){
9302 utf8_printf(stderr, "unknown option: %s\n", z);
9303 utf8_printf(stderr, "options:\n"
9304 " --noquote\n"
9305 " --quote\n"
9306 " --wordwrap on/off\n"
9307 " --wrap N\n"
9308 " --ww\n");
9309 rc = 1;
9310 goto meta_command_exit;
9311 }else{
9312 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9313 rc = 1;
9314 goto meta_command_exit;
9317 if( zMode==0 ){
9318 if( p->mode==MODE_Column
9319 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9321 raw_printf
9322 (p->out,
9323 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9324 modeDescr[p->mode], p->cmOpts.iWrap,
9325 p->cmOpts.bWordWrap ? "on" : "off",
9326 p->cmOpts.bQuote ? "" : "no");
9327 }else{
9328 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
9330 zMode = modeDescr[p->mode];
9332 n2 = strlen30(zMode);
9333 if( cli_strncmp(zMode,"lines",n2)==0 ){
9334 p->mode = MODE_Line;
9335 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9336 }else if( cli_strncmp(zMode,"columns",n2)==0 ){
9337 p->mode = MODE_Column;
9338 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9339 p->showHeader = 1;
9341 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9342 p->cmOpts = cmOpts;
9343 }else if( cli_strncmp(zMode,"list",n2)==0 ){
9344 p->mode = MODE_List;
9345 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9346 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9347 }else if( cli_strncmp(zMode,"html",n2)==0 ){
9348 p->mode = MODE_Html;
9349 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
9350 p->mode = MODE_Tcl;
9351 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9352 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9353 }else if( cli_strncmp(zMode,"csv",n2)==0 ){
9354 p->mode = MODE_Csv;
9355 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9356 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9357 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
9358 p->mode = MODE_List;
9359 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9360 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
9361 p->mode = MODE_Insert;
9362 set_table_name(p, zTabname ? zTabname : "table");
9363 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
9364 p->mode = MODE_Quote;
9365 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9366 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9367 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
9368 p->mode = MODE_Ascii;
9369 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9370 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9371 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
9372 p->mode = MODE_Markdown;
9373 p->cmOpts = cmOpts;
9374 }else if( cli_strncmp(zMode,"table",n2)==0 ){
9375 p->mode = MODE_Table;
9376 p->cmOpts = cmOpts;
9377 }else if( cli_strncmp(zMode,"box",n2)==0 ){
9378 p->mode = MODE_Box;
9379 p->cmOpts = cmOpts;
9380 }else if( cli_strncmp(zMode,"count",n2)==0 ){
9381 p->mode = MODE_Count;
9382 }else if( cli_strncmp(zMode,"off",n2)==0 ){
9383 p->mode = MODE_Off;
9384 }else if( cli_strncmp(zMode,"json",n2)==0 ){
9385 p->mode = MODE_Json;
9386 }else{
9387 raw_printf(stderr, "Error: mode should be one of: "
9388 "ascii box column csv html insert json line list markdown "
9389 "qbox quote table tabs tcl\n");
9390 rc = 1;
9392 p->cMode = p->mode;
9393 }else
9395 #ifndef SQLITE_SHELL_FIDDLE
9396 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9397 if( nArg!=2 ){
9398 raw_printf(stderr, "Usage: .nonce NONCE\n");
9399 rc = 1;
9400 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9401 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n",
9402 p->lineno, azArg[1]);
9403 exit(1);
9404 }else{
9405 p->bSafeMode = 0;
9406 return 0; /* Return immediately to bypass the safe mode reset
9407 ** at the end of this procedure */
9409 }else
9410 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9412 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9413 if( nArg==2 ){
9414 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9415 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9416 }else{
9417 raw_printf(stderr, "Usage: .nullvalue STRING\n");
9418 rc = 1;
9420 }else
9422 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9423 const char *zFN = 0; /* Pointer to constant filename */
9424 char *zNewFilename = 0; /* Name of the database file to open */
9425 int iName = 1; /* Index in azArg[] of the filename */
9426 int newFlag = 0; /* True to delete file before opening */
9427 int openMode = SHELL_OPEN_UNSPEC;
9429 /* Check for command-line arguments */
9430 for(iName=1; iName<nArg; iName++){
9431 const char *z = azArg[iName];
9432 #ifndef SQLITE_SHELL_FIDDLE
9433 if( optionMatch(z,"new") ){
9434 newFlag = 1;
9435 #ifdef SQLITE_HAVE_ZLIB
9436 }else if( optionMatch(z, "zip") ){
9437 openMode = SHELL_OPEN_ZIPFILE;
9438 #endif
9439 }else if( optionMatch(z, "append") ){
9440 openMode = SHELL_OPEN_APPENDVFS;
9441 }else if( optionMatch(z, "readonly") ){
9442 openMode = SHELL_OPEN_READONLY;
9443 }else if( optionMatch(z, "nofollow") ){
9444 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9445 #ifndef SQLITE_OMIT_DESERIALIZE
9446 }else if( optionMatch(z, "deserialize") ){
9447 openMode = SHELL_OPEN_DESERIALIZE;
9448 }else if( optionMatch(z, "hexdb") ){
9449 openMode = SHELL_OPEN_HEXDB;
9450 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9451 p->szMax = integerValue(azArg[++iName]);
9452 #endif /* SQLITE_OMIT_DESERIALIZE */
9453 }else
9454 #endif /* !SQLITE_SHELL_FIDDLE */
9455 if( z[0]=='-' ){
9456 utf8_printf(stderr, "unknown option: %s\n", z);
9457 rc = 1;
9458 goto meta_command_exit;
9459 }else if( zFN ){
9460 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
9461 rc = 1;
9462 goto meta_command_exit;
9463 }else{
9464 zFN = z;
9468 /* Close the existing database */
9469 session_close_all(p, -1);
9470 close_db(p->db);
9471 p->db = 0;
9472 p->pAuxDb->zDbFilename = 0;
9473 sqlite3_free(p->pAuxDb->zFreeOnClose);
9474 p->pAuxDb->zFreeOnClose = 0;
9475 p->openMode = openMode;
9476 p->openFlags = 0;
9477 p->szMax = 0;
9479 /* If a filename is specified, try to open it first */
9480 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9481 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9482 #ifndef SQLITE_SHELL_FIDDLE
9483 if( p->bSafeMode
9484 && p->openMode!=SHELL_OPEN_HEXDB
9485 && zFN
9486 && cli_strcmp(zFN,":memory:")!=0
9488 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9490 #else
9491 /* WASM mode has its own sandboxed pseudo-filesystem. */
9492 #endif
9493 if( zFN ){
9494 zNewFilename = sqlite3_mprintf("%s", zFN);
9495 shell_check_oom(zNewFilename);
9496 }else{
9497 zNewFilename = 0;
9499 p->pAuxDb->zDbFilename = zNewFilename;
9500 open_db(p, OPEN_DB_KEEPALIVE);
9501 if( p->db==0 ){
9502 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
9503 sqlite3_free(zNewFilename);
9504 }else{
9505 p->pAuxDb->zFreeOnClose = zNewFilename;
9508 if( p->db==0 ){
9509 /* As a fall-back open a TEMP database */
9510 p->pAuxDb->zDbFilename = 0;
9511 open_db(p, 0);
9513 }else
9515 #ifndef SQLITE_SHELL_FIDDLE
9516 if( (c=='o'
9517 && (cli_strncmp(azArg[0], "output", n)==0
9518 || cli_strncmp(azArg[0], "once", n)==0))
9519 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9521 char *zFile = 0;
9522 int bTxtMode = 0;
9523 int i;
9524 int eMode = 0;
9525 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
9526 unsigned char zBOM[4]; /* Byte-order mark to using if --bom is present */
9528 zBOM[0] = 0;
9529 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9530 if( c=='e' ){
9531 eMode = 'x';
9532 bOnce = 2;
9533 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9534 bOnce = 1;
9536 for(i=1; i<nArg; i++){
9537 char *z = azArg[i];
9538 if( z[0]=='-' ){
9539 if( z[1]=='-' ) z++;
9540 if( cli_strcmp(z,"-bom")==0 ){
9541 zBOM[0] = 0xef;
9542 zBOM[1] = 0xbb;
9543 zBOM[2] = 0xbf;
9544 zBOM[3] = 0;
9545 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9546 eMode = 'x'; /* spreadsheet */
9547 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9548 eMode = 'e'; /* text editor */
9549 }else{
9550 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
9551 azArg[i]);
9552 showHelp(p->out, azArg[0]);
9553 rc = 1;
9554 goto meta_command_exit;
9556 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9557 zFile = sqlite3_mprintf("%s", z);
9558 if( zFile && zFile[0]=='|' ){
9559 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9560 break;
9562 }else{
9563 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
9564 azArg[i]);
9565 showHelp(p->out, azArg[0]);
9566 rc = 1;
9567 sqlite3_free(zFile);
9568 goto meta_command_exit;
9571 if( zFile==0 ){
9572 zFile = sqlite3_mprintf("stdout");
9574 if( bOnce ){
9575 p->outCount = 2;
9576 }else{
9577 p->outCount = 0;
9579 output_reset(p);
9580 #ifndef SQLITE_NOHAVE_SYSTEM
9581 if( eMode=='e' || eMode=='x' ){
9582 p->doXdgOpen = 1;
9583 outputModePush(p);
9584 if( eMode=='x' ){
9585 /* spreadsheet mode. Output as CSV. */
9586 newTempFile(p, "csv");
9587 ShellClearFlag(p, SHFLG_Echo);
9588 p->mode = MODE_Csv;
9589 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9590 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9591 }else{
9592 /* text editor mode */
9593 newTempFile(p, "txt");
9594 bTxtMode = 1;
9596 sqlite3_free(zFile);
9597 zFile = sqlite3_mprintf("%s", p->zTempFile);
9599 #endif /* SQLITE_NOHAVE_SYSTEM */
9600 shell_check_oom(zFile);
9601 if( zFile[0]=='|' ){
9602 #ifdef SQLITE_OMIT_POPEN
9603 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9604 rc = 1;
9605 p->out = stdout;
9606 #else
9607 p->out = popen(zFile + 1, "w");
9608 if( p->out==0 ){
9609 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
9610 p->out = stdout;
9611 rc = 1;
9612 }else{
9613 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9614 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9616 #endif
9617 }else{
9618 p->out = output_file_open(zFile, bTxtMode);
9619 if( p->out==0 ){
9620 if( cli_strcmp(zFile,"off")!=0 ){
9621 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
9623 p->out = stdout;
9624 rc = 1;
9625 } else {
9626 if( zBOM[0] ) fwrite(zBOM, 1, 3, p->out);
9627 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9630 sqlite3_free(zFile);
9631 }else
9632 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9634 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9635 open_db(p,0);
9636 if( nArg<=1 ) goto parameter_syntax_error;
9638 /* .parameter clear
9639 ** Clear all bind parameters by dropping the TEMP table that holds them.
9641 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9642 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9643 0, 0, 0);
9644 }else
9646 /* .parameter list
9647 ** List all bind parameters.
9649 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9650 sqlite3_stmt *pStmt = 0;
9651 int rx;
9652 int len = 0;
9653 rx = sqlite3_prepare_v2(p->db,
9654 "SELECT max(length(key)) "
9655 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9656 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9657 len = sqlite3_column_int(pStmt, 0);
9658 if( len>40 ) len = 40;
9660 sqlite3_finalize(pStmt);
9661 pStmt = 0;
9662 if( len ){
9663 rx = sqlite3_prepare_v2(p->db,
9664 "SELECT key, quote(value) "
9665 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9666 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9667 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9668 sqlite3_column_text(pStmt,1));
9670 sqlite3_finalize(pStmt);
9672 }else
9674 /* .parameter init
9675 ** Make sure the TEMP table used to hold bind parameters exists.
9676 ** Create it if necessary.
9678 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9679 bind_table_init(p);
9680 }else
9682 /* .parameter set NAME VALUE
9683 ** Set or reset a bind parameter. NAME should be the full parameter
9684 ** name exactly as it appears in the query. (ex: $abc, @def). The
9685 ** VALUE can be in either SQL literal notation, or if not it will be
9686 ** understood to be a text string.
9688 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9689 int rx;
9690 char *zSql;
9691 sqlite3_stmt *pStmt;
9692 const char *zKey = azArg[2];
9693 const char *zValue = azArg[3];
9694 bind_table_init(p);
9695 zSql = sqlite3_mprintf(
9696 "REPLACE INTO temp.sqlite_parameters(key,value)"
9697 "VALUES(%Q,%s);", zKey, zValue);
9698 shell_check_oom(zSql);
9699 pStmt = 0;
9700 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9701 sqlite3_free(zSql);
9702 if( rx!=SQLITE_OK ){
9703 sqlite3_finalize(pStmt);
9704 pStmt = 0;
9705 zSql = sqlite3_mprintf(
9706 "REPLACE INTO temp.sqlite_parameters(key,value)"
9707 "VALUES(%Q,%Q);", zKey, zValue);
9708 shell_check_oom(zSql);
9709 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9710 sqlite3_free(zSql);
9711 if( rx!=SQLITE_OK ){
9712 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
9713 sqlite3_finalize(pStmt);
9714 pStmt = 0;
9715 rc = 1;
9718 sqlite3_step(pStmt);
9719 sqlite3_finalize(pStmt);
9720 }else
9722 /* .parameter unset NAME
9723 ** Remove the NAME binding from the parameter binding table, if it
9724 ** exists.
9726 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
9727 char *zSql = sqlite3_mprintf(
9728 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
9729 shell_check_oom(zSql);
9730 sqlite3_exec(p->db, zSql, 0, 0, 0);
9731 sqlite3_free(zSql);
9732 }else
9733 /* If no command name matches, show a syntax error */
9734 parameter_syntax_error:
9735 showHelp(p->out, "parameter");
9736 }else
9738 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
9739 int i;
9740 for(i=1; i<nArg; i++){
9741 if( i>1 ) raw_printf(p->out, " ");
9742 utf8_printf(p->out, "%s", azArg[i]);
9744 raw_printf(p->out, "\n");
9745 }else
9747 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9748 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
9749 int i;
9750 int nn = 0;
9751 p->flgProgress = 0;
9752 p->mxProgress = 0;
9753 p->nProgress = 0;
9754 for(i=1; i<nArg; i++){
9755 const char *z = azArg[i];
9756 if( z[0]=='-' ){
9757 z++;
9758 if( z[0]=='-' ) z++;
9759 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
9760 p->flgProgress |= SHELL_PROGRESS_QUIET;
9761 continue;
9763 if( cli_strcmp(z,"reset")==0 ){
9764 p->flgProgress |= SHELL_PROGRESS_RESET;
9765 continue;
9767 if( cli_strcmp(z,"once")==0 ){
9768 p->flgProgress |= SHELL_PROGRESS_ONCE;
9769 continue;
9771 if( cli_strcmp(z,"limit")==0 ){
9772 if( i+1>=nArg ){
9773 utf8_printf(stderr, "Error: missing argument on --limit\n");
9774 rc = 1;
9775 goto meta_command_exit;
9776 }else{
9777 p->mxProgress = (int)integerValue(azArg[++i]);
9779 continue;
9781 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
9782 rc = 1;
9783 goto meta_command_exit;
9784 }else{
9785 nn = (int)integerValue(z);
9788 open_db(p, 0);
9789 sqlite3_progress_handler(p->db, nn, progress_handler, p);
9790 }else
9791 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9793 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
9794 if( nArg >= 2) {
9795 shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
9797 if( nArg >= 3) {
9798 shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
9800 }else
9802 #ifndef SQLITE_SHELL_FIDDLE
9803 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
9804 rc = 2;
9805 }else
9806 #endif
9808 #ifndef SQLITE_SHELL_FIDDLE
9809 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
9810 FILE *inSaved = p->in;
9811 int savedLineno = p->lineno;
9812 failIfSafeMode(p, "cannot run .read in safe mode");
9813 if( nArg!=2 ){
9814 raw_printf(stderr, "Usage: .read FILE\n");
9815 rc = 1;
9816 goto meta_command_exit;
9818 if( azArg[1][0]=='|' ){
9819 #ifdef SQLITE_OMIT_POPEN
9820 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
9821 rc = 1;
9822 p->out = stdout;
9823 #else
9824 p->in = popen(azArg[1]+1, "r");
9825 if( p->in==0 ){
9826 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
9827 rc = 1;
9828 }else{
9829 rc = process_input(p);
9830 pclose(p->in);
9832 #endif
9833 }else if( (p->in = openChrSource(azArg[1]))==0 ){
9834 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9835 rc = 1;
9836 }else{
9837 rc = process_input(p);
9838 fclose(p->in);
9840 p->in = inSaved;
9841 p->lineno = savedLineno;
9842 }else
9843 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9845 #ifndef SQLITE_SHELL_FIDDLE
9846 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
9847 const char *zSrcFile;
9848 const char *zDb;
9849 sqlite3 *pSrc;
9850 sqlite3_backup *pBackup;
9851 int nTimeout = 0;
9853 failIfSafeMode(p, "cannot run .restore in safe mode");
9854 if( nArg==2 ){
9855 zSrcFile = azArg[1];
9856 zDb = "main";
9857 }else if( nArg==3 ){
9858 zSrcFile = azArg[2];
9859 zDb = azArg[1];
9860 }else{
9861 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
9862 rc = 1;
9863 goto meta_command_exit;
9865 rc = sqlite3_open(zSrcFile, &pSrc);
9866 if( rc!=SQLITE_OK ){
9867 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
9868 close_db(pSrc);
9869 return 1;
9871 open_db(p, 0);
9872 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
9873 if( pBackup==0 ){
9874 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9875 close_db(pSrc);
9876 return 1;
9878 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
9879 || rc==SQLITE_BUSY ){
9880 if( rc==SQLITE_BUSY ){
9881 if( nTimeout++ >= 3 ) break;
9882 sqlite3_sleep(100);
9885 sqlite3_backup_finish(pBackup);
9886 if( rc==SQLITE_DONE ){
9887 rc = 0;
9888 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
9889 raw_printf(stderr, "Error: source database is busy\n");
9890 rc = 1;
9891 }else{
9892 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9893 rc = 1;
9895 close_db(pSrc);
9896 }else
9897 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9899 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
9900 if( nArg==2 ){
9901 if( cli_strcmp(azArg[1], "est")==0 ){
9902 p->scanstatsOn = 2;
9903 }else{
9904 p->scanstatsOn = (u8)booleanValue(azArg[1]);
9906 open_db(p, 0);
9907 sqlite3_db_config(
9908 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
9910 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9911 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
9912 #endif
9913 }else{
9914 raw_printf(stderr, "Usage: .scanstats on|off|est\n");
9915 rc = 1;
9917 }else
9919 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
9920 ShellText sSelect;
9921 ShellState data;
9922 char *zErrMsg = 0;
9923 const char *zDiv = "(";
9924 const char *zName = 0;
9925 int iSchema = 0;
9926 int bDebug = 0;
9927 int bNoSystemTabs = 0;
9928 int ii;
9930 open_db(p, 0);
9931 memcpy(&data, p, sizeof(data));
9932 data.showHeader = 0;
9933 data.cMode = data.mode = MODE_Semi;
9934 initText(&sSelect);
9935 for(ii=1; ii<nArg; ii++){
9936 if( optionMatch(azArg[ii],"indent") ){
9937 data.cMode = data.mode = MODE_Pretty;
9938 }else if( optionMatch(azArg[ii],"debug") ){
9939 bDebug = 1;
9940 }else if( optionMatch(azArg[ii],"nosys") ){
9941 bNoSystemTabs = 1;
9942 }else if( azArg[ii][0]=='-' ){
9943 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
9944 rc = 1;
9945 goto meta_command_exit;
9946 }else if( zName==0 ){
9947 zName = azArg[ii];
9948 }else{
9949 raw_printf(stderr,
9950 "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9951 rc = 1;
9952 goto meta_command_exit;
9955 if( zName!=0 ){
9956 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
9957 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
9958 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
9959 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
9960 if( isSchema ){
9961 char *new_argv[2], *new_colv[2];
9962 new_argv[0] = sqlite3_mprintf(
9963 "CREATE TABLE %s (\n"
9964 " type text,\n"
9965 " name text,\n"
9966 " tbl_name text,\n"
9967 " rootpage integer,\n"
9968 " sql text\n"
9969 ")", zName);
9970 shell_check_oom(new_argv[0]);
9971 new_argv[1] = 0;
9972 new_colv[0] = "sql";
9973 new_colv[1] = 0;
9974 callback(&data, 1, new_argv, new_colv);
9975 sqlite3_free(new_argv[0]);
9978 if( zDiv ){
9979 sqlite3_stmt *pStmt = 0;
9980 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
9981 -1, &pStmt, 0);
9982 if( rc ){
9983 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
9984 sqlite3_finalize(pStmt);
9985 rc = 1;
9986 goto meta_command_exit;
9988 appendText(&sSelect, "SELECT sql FROM", 0);
9989 iSchema = 0;
9990 while( sqlite3_step(pStmt)==SQLITE_ROW ){
9991 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
9992 char zScNum[30];
9993 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
9994 appendText(&sSelect, zDiv, 0);
9995 zDiv = " UNION ALL ";
9996 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
9997 if( sqlite3_stricmp(zDb, "main")!=0 ){
9998 appendText(&sSelect, zDb, '\'');
9999 }else{
10000 appendText(&sSelect, "NULL", 0);
10002 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10003 appendText(&sSelect, zScNum, 0);
10004 appendText(&sSelect, " AS snum, ", 0);
10005 appendText(&sSelect, zDb, '\'');
10006 appendText(&sSelect, " AS sname FROM ", 0);
10007 appendText(&sSelect, zDb, quoteChar(zDb));
10008 appendText(&sSelect, ".sqlite_schema", 0);
10010 sqlite3_finalize(pStmt);
10011 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10012 if( zName ){
10013 appendText(&sSelect,
10014 " UNION ALL SELECT shell_module_schema(name),"
10015 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10018 #endif
10019 appendText(&sSelect, ") WHERE ", 0);
10020 if( zName ){
10021 char *zQarg = sqlite3_mprintf("%Q", zName);
10022 int bGlob;
10023 shell_check_oom(zQarg);
10024 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10025 strchr(zName, '[') != 0;
10026 if( strchr(zName, '.') ){
10027 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10028 }else{
10029 appendText(&sSelect, "lower(tbl_name)", 0);
10031 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10032 appendText(&sSelect, zQarg, 0);
10033 if( !bGlob ){
10034 appendText(&sSelect, " ESCAPE '\\' ", 0);
10036 appendText(&sSelect, " AND ", 0);
10037 sqlite3_free(zQarg);
10039 if( bNoSystemTabs ){
10040 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10042 appendText(&sSelect, "sql IS NOT NULL"
10043 " ORDER BY snum, rowid", 0);
10044 if( bDebug ){
10045 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
10046 }else{
10047 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10049 freeText(&sSelect);
10051 if( zErrMsg ){
10052 utf8_printf(stderr,"Error: %s\n", zErrMsg);
10053 sqlite3_free(zErrMsg);
10054 rc = 1;
10055 }else if( rc != SQLITE_OK ){
10056 raw_printf(stderr,"Error: querying schema information\n");
10057 rc = 1;
10058 }else{
10059 rc = 0;
10061 }else
10063 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
10064 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
10066 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10067 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10068 }else
10070 #if defined(SQLITE_ENABLE_SESSION)
10071 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
10072 struct AuxDb *pAuxDb = p->pAuxDb;
10073 OpenSession *pSession = &pAuxDb->aSession[0];
10074 char **azCmd = &azArg[1];
10075 int iSes = 0;
10076 int nCmd = nArg - 1;
10077 int i;
10078 if( nArg<=1 ) goto session_syntax_error;
10079 open_db(p, 0);
10080 if( nArg>=3 ){
10081 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10082 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10084 if( iSes<pAuxDb->nSession ){
10085 pSession = &pAuxDb->aSession[iSes];
10086 azCmd++;
10087 nCmd--;
10088 }else{
10089 pSession = &pAuxDb->aSession[0];
10090 iSes = 0;
10094 /* .session attach TABLE
10095 ** Invoke the sqlite3session_attach() interface to attach a particular
10096 ** table so that it is never filtered.
10098 if( cli_strcmp(azCmd[0],"attach")==0 ){
10099 if( nCmd!=2 ) goto session_syntax_error;
10100 if( pSession->p==0 ){
10101 session_not_open:
10102 raw_printf(stderr, "ERROR: No sessions are open\n");
10103 }else{
10104 rc = sqlite3session_attach(pSession->p, azCmd[1]);
10105 if( rc ){
10106 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
10107 rc = 0;
10110 }else
10112 /* .session changeset FILE
10113 ** .session patchset FILE
10114 ** Write a changeset or patchset into a file. The file is overwritten.
10116 if( cli_strcmp(azCmd[0],"changeset")==0
10117 || cli_strcmp(azCmd[0],"patchset")==0
10119 FILE *out = 0;
10120 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10121 if( nCmd!=2 ) goto session_syntax_error;
10122 if( pSession->p==0 ) goto session_not_open;
10123 out = fopen(azCmd[1], "wb");
10124 if( out==0 ){
10125 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
10126 azCmd[1]);
10127 }else{
10128 int szChng;
10129 void *pChng;
10130 if( azCmd[0][0]=='c' ){
10131 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10132 }else{
10133 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10135 if( rc ){
10136 printf("Error: error code %d\n", rc);
10137 rc = 0;
10139 if( pChng
10140 && fwrite(pChng, szChng, 1, out)!=1 ){
10141 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
10142 szChng);
10144 sqlite3_free(pChng);
10145 fclose(out);
10147 }else
10149 /* .session close
10150 ** Close the identified session
10152 if( cli_strcmp(azCmd[0], "close")==0 ){
10153 if( nCmd!=1 ) goto session_syntax_error;
10154 if( pAuxDb->nSession ){
10155 session_close(pSession);
10156 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10158 }else
10160 /* .session enable ?BOOLEAN?
10161 ** Query or set the enable flag
10163 if( cli_strcmp(azCmd[0], "enable")==0 ){
10164 int ii;
10165 if( nCmd>2 ) goto session_syntax_error;
10166 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10167 if( pAuxDb->nSession ){
10168 ii = sqlite3session_enable(pSession->p, ii);
10169 utf8_printf(p->out, "session %s enable flag = %d\n",
10170 pSession->zName, ii);
10172 }else
10174 /* .session filter GLOB ....
10175 ** Set a list of GLOB patterns of table names to be excluded.
10177 if( cli_strcmp(azCmd[0], "filter")==0 ){
10178 int ii, nByte;
10179 if( nCmd<2 ) goto session_syntax_error;
10180 if( pAuxDb->nSession ){
10181 for(ii=0; ii<pSession->nFilter; ii++){
10182 sqlite3_free(pSession->azFilter[ii]);
10184 sqlite3_free(pSession->azFilter);
10185 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10186 pSession->azFilter = sqlite3_malloc( nByte );
10187 if( pSession->azFilter==0 ){
10188 raw_printf(stderr, "Error: out or memory\n");
10189 exit(1);
10191 for(ii=1; ii<nCmd; ii++){
10192 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10193 shell_check_oom(x);
10195 pSession->nFilter = ii-1;
10197 }else
10199 /* .session indirect ?BOOLEAN?
10200 ** Query or set the indirect flag
10202 if( cli_strcmp(azCmd[0], "indirect")==0 ){
10203 int ii;
10204 if( nCmd>2 ) goto session_syntax_error;
10205 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10206 if( pAuxDb->nSession ){
10207 ii = sqlite3session_indirect(pSession->p, ii);
10208 utf8_printf(p->out, "session %s indirect flag = %d\n",
10209 pSession->zName, ii);
10211 }else
10213 /* .session isempty
10214 ** Determine if the session is empty
10216 if( cli_strcmp(azCmd[0], "isempty")==0 ){
10217 int ii;
10218 if( nCmd!=1 ) goto session_syntax_error;
10219 if( pAuxDb->nSession ){
10220 ii = sqlite3session_isempty(pSession->p);
10221 utf8_printf(p->out, "session %s isempty flag = %d\n",
10222 pSession->zName, ii);
10224 }else
10226 /* .session list
10227 ** List all currently open sessions
10229 if( cli_strcmp(azCmd[0],"list")==0 ){
10230 for(i=0; i<pAuxDb->nSession; i++){
10231 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10233 }else
10235 /* .session open DB NAME
10236 ** Open a new session called NAME on the attached database DB.
10237 ** DB is normally "main".
10239 if( cli_strcmp(azCmd[0],"open")==0 ){
10240 char *zName;
10241 if( nCmd!=3 ) goto session_syntax_error;
10242 zName = azCmd[2];
10243 if( zName[0]==0 ) goto session_syntax_error;
10244 for(i=0; i<pAuxDb->nSession; i++){
10245 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10246 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
10247 goto meta_command_exit;
10250 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10251 raw_printf(stderr,
10252 "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10253 goto meta_command_exit;
10255 pSession = &pAuxDb->aSession[pAuxDb->nSession];
10256 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10257 if( rc ){
10258 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
10259 rc = 0;
10260 goto meta_command_exit;
10262 pSession->nFilter = 0;
10263 sqlite3session_table_filter(pSession->p, session_filter, pSession);
10264 pAuxDb->nSession++;
10265 pSession->zName = sqlite3_mprintf("%s", zName);
10266 shell_check_oom(pSession->zName);
10267 }else
10268 /* If no command name matches, show a syntax error */
10269 session_syntax_error:
10270 showHelp(p->out, "session");
10271 }else
10272 #endif
10274 #ifdef SQLITE_DEBUG
10275 /* Undocumented commands for internal testing. Subject to change
10276 ** without notice. */
10277 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
10278 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10279 int i, v;
10280 for(i=1; i<nArg; i++){
10281 v = booleanValue(azArg[i]);
10282 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10285 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
10286 int i; sqlite3_int64 v;
10287 for(i=1; i<nArg; i++){
10288 char zBuf[200];
10289 v = integerValue(azArg[i]);
10290 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10291 utf8_printf(p->out, "%s", zBuf);
10294 }else
10295 #endif
10297 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
10298 int bIsInit = 0; /* True to initialize the SELFTEST table */
10299 int bVerbose = 0; /* Verbose output */
10300 int bSelftestExists; /* True if SELFTEST already exists */
10301 int i, k; /* Loop counters */
10302 int nTest = 0; /* Number of tests runs */
10303 int nErr = 0; /* Number of errors seen */
10304 ShellText str; /* Answer for a query */
10305 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10307 open_db(p,0);
10308 for(i=1; i<nArg; i++){
10309 const char *z = azArg[i];
10310 if( z[0]=='-' && z[1]=='-' ) z++;
10311 if( cli_strcmp(z,"-init")==0 ){
10312 bIsInit = 1;
10313 }else
10314 if( cli_strcmp(z,"-v")==0 ){
10315 bVerbose++;
10316 }else
10318 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10319 azArg[i], azArg[0]);
10320 raw_printf(stderr, "Should be one of: --init -v\n");
10321 rc = 1;
10322 goto meta_command_exit;
10325 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10326 != SQLITE_OK ){
10327 bSelftestExists = 0;
10328 }else{
10329 bSelftestExists = 1;
10331 if( bIsInit ){
10332 createSelftestTable(p);
10333 bSelftestExists = 1;
10335 initText(&str);
10336 appendText(&str, "x", 0);
10337 for(k=bSelftestExists; k>=0; k--){
10338 if( k==1 ){
10339 rc = sqlite3_prepare_v2(p->db,
10340 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10341 -1, &pStmt, 0);
10342 }else{
10343 rc = sqlite3_prepare_v2(p->db,
10344 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10345 " (1,'run','PRAGMA integrity_check','ok')",
10346 -1, &pStmt, 0);
10348 if( rc ){
10349 raw_printf(stderr, "Error querying the selftest table\n");
10350 rc = 1;
10351 sqlite3_finalize(pStmt);
10352 goto meta_command_exit;
10354 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10355 int tno = sqlite3_column_int(pStmt, 0);
10356 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10357 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10358 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10360 if( zOp==0 ) continue;
10361 if( zSql==0 ) continue;
10362 if( zAns==0 ) continue;
10363 k = 0;
10364 if( bVerbose>0 ){
10365 printf("%d: %s %s\n", tno, zOp, zSql);
10367 if( cli_strcmp(zOp,"memo")==0 ){
10368 utf8_printf(p->out, "%s\n", zSql);
10369 }else
10370 if( cli_strcmp(zOp,"run")==0 ){
10371 char *zErrMsg = 0;
10372 str.n = 0;
10373 str.z[0] = 0;
10374 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10375 nTest++;
10376 if( bVerbose ){
10377 utf8_printf(p->out, "Result: %s\n", str.z);
10379 if( rc || zErrMsg ){
10380 nErr++;
10381 rc = 1;
10382 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10383 sqlite3_free(zErrMsg);
10384 }else if( cli_strcmp(zAns,str.z)!=0 ){
10385 nErr++;
10386 rc = 1;
10387 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10388 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
10390 }else
10392 utf8_printf(stderr,
10393 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10394 rc = 1;
10395 break;
10397 } /* End loop over rows of content from SELFTEST */
10398 sqlite3_finalize(pStmt);
10399 } /* End loop over k */
10400 freeText(&str);
10401 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10402 }else
10404 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10405 if( nArg<2 || nArg>3 ){
10406 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
10407 rc = 1;
10409 if( nArg>=2 ){
10410 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10411 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10413 if( nArg>=3 ){
10414 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10415 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10417 }else
10419 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10420 const char *zLike = 0; /* Which table to checksum. 0 means everything */
10421 int i; /* Loop counter */
10422 int bSchema = 0; /* Also hash the schema */
10423 int bSeparate = 0; /* Hash each table separately */
10424 int iSize = 224; /* Hash algorithm to use */
10425 int bDebug = 0; /* Only show the query that would have run */
10426 sqlite3_stmt *pStmt; /* For querying tables names */
10427 char *zSql; /* SQL to be run */
10428 char *zSep; /* Separator */
10429 ShellText sSql; /* Complete SQL for the query to run the hash */
10430 ShellText sQuery; /* Set of queries used to read all content */
10431 open_db(p, 0);
10432 for(i=1; i<nArg; i++){
10433 const char *z = azArg[i];
10434 if( z[0]=='-' ){
10435 z++;
10436 if( z[0]=='-' ) z++;
10437 if( cli_strcmp(z,"schema")==0 ){
10438 bSchema = 1;
10439 }else
10440 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10441 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10443 iSize = atoi(&z[5]);
10444 }else
10445 if( cli_strcmp(z,"debug")==0 ){
10446 bDebug = 1;
10447 }else
10449 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
10450 azArg[i], azArg[0]);
10451 showHelp(p->out, azArg[0]);
10452 rc = 1;
10453 goto meta_command_exit;
10455 }else if( zLike ){
10456 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10457 rc = 1;
10458 goto meta_command_exit;
10459 }else{
10460 zLike = z;
10461 bSeparate = 1;
10462 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10465 if( bSchema ){
10466 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10467 " WHERE type='table' AND coalesce(rootpage,0)>1"
10468 " UNION ALL SELECT 'sqlite_schema'"
10469 " ORDER BY 1 collate nocase";
10470 }else{
10471 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10472 " WHERE type='table' AND coalesce(rootpage,0)>1"
10473 " AND name NOT LIKE 'sqlite_%'"
10474 " ORDER BY 1 collate nocase";
10476 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10477 initText(&sQuery);
10478 initText(&sSql);
10479 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10480 zSep = "VALUES(";
10481 while( SQLITE_ROW==sqlite3_step(pStmt) ){
10482 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10483 if( zTab==0 ) continue;
10484 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10485 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10486 appendText(&sQuery,"SELECT * FROM ", 0);
10487 appendText(&sQuery,zTab,'"');
10488 appendText(&sQuery," NOT INDEXED;", 0);
10489 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10490 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10491 " ORDER BY name;", 0);
10492 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10493 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10494 " ORDER BY name;", 0);
10495 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10496 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10497 " ORDER BY tbl,idx;", 0);
10498 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10499 appendText(&sQuery, "SELECT * FROM ", 0);
10500 appendText(&sQuery, zTab, 0);
10501 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10503 appendText(&sSql, zSep, 0);
10504 appendText(&sSql, sQuery.z, '\'');
10505 sQuery.n = 0;
10506 appendText(&sSql, ",", 0);
10507 appendText(&sSql, zTab, '\'');
10508 zSep = "),(";
10510 sqlite3_finalize(pStmt);
10511 if( bSeparate ){
10512 zSql = sqlite3_mprintf(
10513 "%s))"
10514 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10515 " FROM [sha3sum$query]",
10516 sSql.z, iSize);
10517 }else{
10518 zSql = sqlite3_mprintf(
10519 "%s))"
10520 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10521 " FROM [sha3sum$query]",
10522 sSql.z, iSize);
10524 shell_check_oom(zSql);
10525 freeText(&sQuery);
10526 freeText(&sSql);
10527 if( bDebug ){
10528 utf8_printf(p->out, "%s\n", zSql);
10529 }else{
10530 shell_exec(p, zSql, 0);
10532 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10534 int lrc;
10535 char *zRevText = /* Query for reversible to-blob-to-text check */
10536 "SELECT lower(name) as tname FROM sqlite_schema\n"
10537 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
10538 "AND name NOT LIKE 'sqlite_%%'%s\n"
10539 "ORDER BY 1 collate nocase";
10540 zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
10541 zRevText = sqlite3_mprintf(
10542 /* lower-case query is first run, producing upper-case query. */
10543 "with tabcols as materialized(\n"
10544 "select tname, cname\n"
10545 "from ("
10546 " select ss.tname as tname, ti.name as cname\n"
10547 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
10548 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
10549 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
10550 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
10551 "FROM '||tname||' WHERE '\n"
10552 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
10553 "|| ' AND typeof('||cname||')=''text'' ',\n"
10554 "' OR ') as query, tname from tabcols group by tname)"
10555 , zRevText);
10556 shell_check_oom(zRevText);
10557 if( bDebug ) utf8_printf(p->out, "%s\n", zRevText);
10558 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
10559 if( lrc!=SQLITE_OK ){
10560 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
10561 ** user does cruel and unnatural things like ".limit expr_depth 0". */
10562 rc = 1;
10563 }else{
10564 if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
10565 lrc = SQLITE_ROW==sqlite3_step(pStmt);
10566 if( lrc ){
10567 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
10568 sqlite3_stmt *pCheckStmt;
10569 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
10570 if( bDebug ) utf8_printf(p->out, "%s\n", zGenQuery);
10571 if( lrc!=SQLITE_OK ){
10572 rc = 1;
10573 }else{
10574 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
10575 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
10576 if( countIrreversible>0 ){
10577 int sz = (int)(countIrreversible + 0.5);
10578 utf8_printf(stderr,
10579 "Digest includes %d invalidly encoded text field%s.\n",
10580 sz, (sz>1)? "s": "");
10583 sqlite3_finalize(pCheckStmt);
10585 sqlite3_finalize(pStmt);
10588 if( rc ) utf8_printf(stderr, ".sha3sum failed.\n");
10589 sqlite3_free(zRevText);
10591 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
10592 sqlite3_free(zSql);
10593 }else
10595 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10596 if( c=='s'
10597 && (cli_strncmp(azArg[0], "shell", n)==0
10598 || cli_strncmp(azArg[0],"system",n)==0)
10600 char *zCmd;
10601 int i, x;
10602 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10603 if( nArg<2 ){
10604 raw_printf(stderr, "Usage: .system COMMAND\n");
10605 rc = 1;
10606 goto meta_command_exit;
10608 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10609 for(i=2; i<nArg && zCmd!=0; i++){
10610 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10611 zCmd, azArg[i]);
10613 x = zCmd!=0 ? system(zCmd) : 1;
10614 sqlite3_free(zCmd);
10615 if( x ) raw_printf(stderr, "System command returns %d\n", x);
10616 }else
10617 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10619 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10620 static const char *azBool[] = { "off", "on", "trigger", "full"};
10621 const char *zOut;
10622 int i;
10623 if( nArg!=1 ){
10624 raw_printf(stderr, "Usage: .show\n");
10625 rc = 1;
10626 goto meta_command_exit;
10628 utf8_printf(p->out, "%12.12s: %s\n","echo",
10629 azBool[ShellHasFlag(p, SHFLG_Echo)]);
10630 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10631 utf8_printf(p->out, "%12.12s: %s\n","explain",
10632 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10633 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10634 if( p->mode==MODE_Column
10635 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10637 utf8_printf
10638 (p->out, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10639 modeDescr[p->mode], p->cmOpts.iWrap,
10640 p->cmOpts.bWordWrap ? "on" : "off",
10641 p->cmOpts.bQuote ? "" : "no");
10642 }else{
10643 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
10645 utf8_printf(p->out, "%12.12s: ", "nullvalue");
10646 output_c_string(p->out, p->nullValue);
10647 raw_printf(p->out, "\n");
10648 utf8_printf(p->out,"%12.12s: %s\n","output",
10649 strlen30(p->outfile) ? p->outfile : "stdout");
10650 utf8_printf(p->out,"%12.12s: ", "colseparator");
10651 output_c_string(p->out, p->colSeparator);
10652 raw_printf(p->out, "\n");
10653 utf8_printf(p->out,"%12.12s: ", "rowseparator");
10654 output_c_string(p->out, p->rowSeparator);
10655 raw_printf(p->out, "\n");
10656 switch( p->statsOn ){
10657 case 0: zOut = "off"; break;
10658 default: zOut = "on"; break;
10659 case 2: zOut = "stmt"; break;
10660 case 3: zOut = "vmstep"; break;
10662 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
10663 utf8_printf(p->out, "%12.12s: ", "width");
10664 for (i=0;i<p->nWidth;i++) {
10665 raw_printf(p->out, "%d ", p->colWidth[i]);
10667 raw_printf(p->out, "\n");
10668 utf8_printf(p->out, "%12.12s: %s\n", "filename",
10669 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10670 }else
10672 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10673 if( nArg==2 ){
10674 if( cli_strcmp(azArg[1],"stmt")==0 ){
10675 p->statsOn = 2;
10676 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10677 p->statsOn = 3;
10678 }else{
10679 p->statsOn = (u8)booleanValue(azArg[1]);
10681 }else if( nArg==1 ){
10682 display_stats(p->db, p, 0);
10683 }else{
10684 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
10685 rc = 1;
10687 }else
10689 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10690 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10691 || cli_strncmp(azArg[0], "indexes", n)==0) )
10693 sqlite3_stmt *pStmt;
10694 char **azResult;
10695 int nRow, nAlloc;
10696 int ii;
10697 ShellText s;
10698 initText(&s);
10699 open_db(p, 0);
10700 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
10701 if( rc ){
10702 sqlite3_finalize(pStmt);
10703 return shellDatabaseError(p->db);
10706 if( nArg>2 && c=='i' ){
10707 /* It is an historical accident that the .indexes command shows an error
10708 ** when called with the wrong number of arguments whereas the .tables
10709 ** command does not. */
10710 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
10711 rc = 1;
10712 sqlite3_finalize(pStmt);
10713 goto meta_command_exit;
10715 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
10716 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
10717 if( zDbName==0 ) continue;
10718 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
10719 if( sqlite3_stricmp(zDbName, "main")==0 ){
10720 appendText(&s, "SELECT name FROM ", 0);
10721 }else{
10722 appendText(&s, "SELECT ", 0);
10723 appendText(&s, zDbName, '\'');
10724 appendText(&s, "||'.'||name FROM ", 0);
10726 appendText(&s, zDbName, '"');
10727 appendText(&s, ".sqlite_schema ", 0);
10728 if( c=='t' ){
10729 appendText(&s," WHERE type IN ('table','view')"
10730 " AND name NOT LIKE 'sqlite_%'"
10731 " AND name LIKE ?1", 0);
10732 }else{
10733 appendText(&s," WHERE type='index'"
10734 " AND tbl_name LIKE ?1", 0);
10737 rc = sqlite3_finalize(pStmt);
10738 if( rc==SQLITE_OK ){
10739 appendText(&s, " ORDER BY 1", 0);
10740 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
10742 freeText(&s);
10743 if( rc ) return shellDatabaseError(p->db);
10745 /* Run the SQL statement prepared by the above block. Store the results
10746 ** as an array of nul-terminated strings in azResult[]. */
10747 nRow = nAlloc = 0;
10748 azResult = 0;
10749 if( nArg>1 ){
10750 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
10751 }else{
10752 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
10754 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10755 if( nRow>=nAlloc ){
10756 char **azNew;
10757 int n2 = nAlloc*2 + 10;
10758 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
10759 shell_check_oom(azNew);
10760 nAlloc = n2;
10761 azResult = azNew;
10763 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
10764 shell_check_oom(azResult[nRow]);
10765 nRow++;
10767 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
10768 rc = shellDatabaseError(p->db);
10771 /* Pretty-print the contents of array azResult[] to the output */
10772 if( rc==0 && nRow>0 ){
10773 int len, maxlen = 0;
10774 int i, j;
10775 int nPrintCol, nPrintRow;
10776 for(i=0; i<nRow; i++){
10777 len = strlen30(azResult[i]);
10778 if( len>maxlen ) maxlen = len;
10780 nPrintCol = 80/(maxlen+2);
10781 if( nPrintCol<1 ) nPrintCol = 1;
10782 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
10783 for(i=0; i<nPrintRow; i++){
10784 for(j=i; j<nRow; j+=nPrintRow){
10785 char *zSp = j<nPrintRow ? "" : " ";
10786 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
10787 azResult[j] ? azResult[j]:"");
10789 raw_printf(p->out, "\n");
10793 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
10794 sqlite3_free(azResult);
10795 }else
10797 #ifndef SQLITE_SHELL_FIDDLE
10798 /* Begin redirecting output to the file "testcase-out.txt" */
10799 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
10800 output_reset(p);
10801 p->out = output_file_open("testcase-out.txt", 0);
10802 if( p->out==0 ){
10803 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
10805 if( nArg>=2 ){
10806 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
10807 }else{
10808 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
10810 }else
10811 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10813 #ifndef SQLITE_UNTESTABLE
10814 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
10815 static const struct {
10816 const char *zCtrlName; /* Name of a test-control option */
10817 int ctrlCode; /* Integer code for that option */
10818 int unSafe; /* Not valid for --safe mode */
10819 const char *zUsage; /* Usage notes */
10820 } aCtrl[] = {
10821 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
10822 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
10823 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
10824 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
10825 {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" },
10826 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" },
10827 /*{"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/
10828 {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
10829 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" },
10830 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" },
10831 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" },
10832 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK" },
10833 #ifdef YYCOVERAGE
10834 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" },
10835 #endif
10836 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,0, "OFFSET " },
10837 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" },
10838 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
10839 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
10840 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
10841 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
10842 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
10844 int testctrl = -1;
10845 int iCtrl = -1;
10846 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
10847 int isOk = 0;
10848 int i, n2;
10849 const char *zCmd = 0;
10851 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
10852 utf8_printf(stderr, ".%s unavailable without --unsafe-testing\n",
10853 "testctrl");
10854 rc = 1;
10855 goto meta_command_exit;
10857 open_db(p, 0);
10858 zCmd = nArg>=2 ? azArg[1] : "help";
10860 /* The argument can optionally begin with "-" or "--" */
10861 if( zCmd[0]=='-' && zCmd[1] ){
10862 zCmd++;
10863 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
10866 /* --help lists all test-controls */
10867 if( cli_strcmp(zCmd,"help")==0 ){
10868 utf8_printf(p->out, "Available test-controls:\n");
10869 for(i=0; i<ArraySize(aCtrl); i++){
10870 utf8_printf(p->out, " .testctrl %s %s\n",
10871 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
10873 rc = 1;
10874 goto meta_command_exit;
10877 /* convert testctrl text option to value. allow any unique prefix
10878 ** of the option name, or a numerical value. */
10879 n2 = strlen30(zCmd);
10880 for(i=0; i<ArraySize(aCtrl); i++){
10881 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
10882 if( testctrl<0 ){
10883 testctrl = aCtrl[i].ctrlCode;
10884 iCtrl = i;
10885 }else{
10886 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
10887 "Use \".testctrl --help\" for help\n", zCmd);
10888 rc = 1;
10889 goto meta_command_exit;
10893 if( testctrl<0 ){
10894 utf8_printf(stderr,"Error: unknown test-control: %s\n"
10895 "Use \".testctrl --help\" for help\n", zCmd);
10896 }else if( aCtrl[iCtrl].unSafe && p->bSafeMode ){
10897 utf8_printf(stderr,
10898 "line %d: \".testctrl %s\" may not be used in safe mode\n",
10899 p->lineno, aCtrl[iCtrl].zCtrlName);
10900 exit(1);
10901 }else{
10902 switch(testctrl){
10904 /* sqlite3_test_control(int, db, int) */
10905 case SQLITE_TESTCTRL_OPTIMIZATIONS:
10906 if( nArg==3 ){
10907 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
10908 rc2 = sqlite3_test_control(testctrl, p->db, opt);
10909 isOk = 3;
10911 break;
10913 /* sqlite3_test_control(int) */
10914 case SQLITE_TESTCTRL_PRNG_SAVE:
10915 case SQLITE_TESTCTRL_PRNG_RESTORE:
10916 case SQLITE_TESTCTRL_BYTEORDER:
10917 if( nArg==2 ){
10918 rc2 = sqlite3_test_control(testctrl);
10919 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
10921 break;
10923 /* sqlite3_test_control(int, uint) */
10924 case SQLITE_TESTCTRL_PENDING_BYTE:
10925 if( nArg==3 ){
10926 unsigned int opt = (unsigned int)integerValue(azArg[2]);
10927 rc2 = sqlite3_test_control(testctrl, opt);
10928 isOk = 3;
10930 break;
10932 /* sqlite3_test_control(int, int, sqlite3*) */
10933 case SQLITE_TESTCTRL_PRNG_SEED:
10934 if( nArg==3 || nArg==4 ){
10935 int ii = (int)integerValue(azArg[2]);
10936 sqlite3 *db;
10937 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
10938 sqlite3_randomness(sizeof(ii),&ii);
10939 printf("-- random seed: %d\n", ii);
10941 if( nArg==3 ){
10942 db = 0;
10943 }else{
10944 db = p->db;
10945 /* Make sure the schema has been loaded */
10946 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
10948 rc2 = sqlite3_test_control(testctrl, ii, db);
10949 isOk = 3;
10951 break;
10953 /* sqlite3_test_control(int, int) */
10954 case SQLITE_TESTCTRL_ASSERT:
10955 case SQLITE_TESTCTRL_ALWAYS:
10956 if( nArg==3 ){
10957 int opt = booleanValue(azArg[2]);
10958 rc2 = sqlite3_test_control(testctrl, opt);
10959 isOk = 1;
10961 break;
10963 /* sqlite3_test_control(int, int) */
10964 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
10965 case SQLITE_TESTCTRL_NEVER_CORRUPT:
10966 if( nArg==3 ){
10967 int opt = booleanValue(azArg[2]);
10968 rc2 = sqlite3_test_control(testctrl, opt);
10969 isOk = 3;
10971 break;
10973 /* sqlite3_test_control(sqlite3*) */
10974 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
10975 rc2 = sqlite3_test_control(testctrl, p->db);
10976 isOk = 3;
10977 break;
10979 case SQLITE_TESTCTRL_IMPOSTER:
10980 if( nArg==5 ){
10981 rc2 = sqlite3_test_control(testctrl, p->db,
10982 azArg[2],
10983 integerValue(azArg[3]),
10984 integerValue(azArg[4]));
10985 isOk = 3;
10987 break;
10989 case SQLITE_TESTCTRL_SEEK_COUNT: {
10990 u64 x = 0;
10991 rc2 = sqlite3_test_control(testctrl, p->db, &x);
10992 utf8_printf(p->out, "%llu\n", x);
10993 isOk = 3;
10994 break;
10997 #ifdef YYCOVERAGE
10998 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
10999 if( nArg==2 ){
11000 sqlite3_test_control(testctrl, p->out);
11001 isOk = 3;
11003 break;
11005 #endif
11006 #ifdef SQLITE_DEBUG
11007 case SQLITE_TESTCTRL_TUNE: {
11008 if( nArg==4 ){
11009 int id = (int)integerValue(azArg[2]);
11010 int val = (int)integerValue(azArg[3]);
11011 sqlite3_test_control(testctrl, id, &val);
11012 isOk = 3;
11013 }else if( nArg==3 ){
11014 int id = (int)integerValue(azArg[2]);
11015 sqlite3_test_control(testctrl, -id, &rc2);
11016 isOk = 1;
11017 }else if( nArg==2 ){
11018 int id = 1;
11019 while(1){
11020 int val = 0;
11021 rc2 = sqlite3_test_control(testctrl, -id, &val);
11022 if( rc2!=SQLITE_OK ) break;
11023 if( id>1 ) utf8_printf(p->out, " ");
11024 utf8_printf(p->out, "%d: %d", id, val);
11025 id++;
11027 if( id>1 ) utf8_printf(p->out, "\n");
11028 isOk = 3;
11030 break;
11032 #endif
11033 case SQLITE_TESTCTRL_SORTER_MMAP:
11034 if( nArg==3 ){
11035 int opt = (unsigned int)integerValue(azArg[2]);
11036 rc2 = sqlite3_test_control(testctrl, p->db, opt);
11037 isOk = 3;
11039 break;
11042 if( isOk==0 && iCtrl>=0 ){
11043 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11044 rc = 1;
11045 }else if( isOk==1 ){
11046 raw_printf(p->out, "%d\n", rc2);
11047 }else if( isOk==2 ){
11048 raw_printf(p->out, "0x%08x\n", rc2);
11050 }else
11051 #endif /* !defined(SQLITE_UNTESTABLE) */
11053 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
11054 open_db(p, 0);
11055 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11056 }else
11058 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
11059 if( nArg==2 ){
11060 enableTimer = booleanValue(azArg[1]);
11061 if( enableTimer && !HAS_TIMER ){
11062 raw_printf(stderr, "Error: timer not available on this system.\n");
11063 enableTimer = 0;
11065 }else{
11066 raw_printf(stderr, "Usage: .timer on|off\n");
11067 rc = 1;
11069 }else
11071 #ifndef SQLITE_OMIT_TRACE
11072 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
11073 int mType = 0;
11074 int jj;
11075 open_db(p, 0);
11076 for(jj=1; jj<nArg; jj++){
11077 const char *z = azArg[jj];
11078 if( z[0]=='-' ){
11079 if( optionMatch(z, "expanded") ){
11080 p->eTraceType = SHELL_TRACE_EXPANDED;
11082 #ifdef SQLITE_ENABLE_NORMALIZE
11083 else if( optionMatch(z, "normalized") ){
11084 p->eTraceType = SHELL_TRACE_NORMALIZED;
11086 #endif
11087 else if( optionMatch(z, "plain") ){
11088 p->eTraceType = SHELL_TRACE_PLAIN;
11090 else if( optionMatch(z, "profile") ){
11091 mType |= SQLITE_TRACE_PROFILE;
11093 else if( optionMatch(z, "row") ){
11094 mType |= SQLITE_TRACE_ROW;
11096 else if( optionMatch(z, "stmt") ){
11097 mType |= SQLITE_TRACE_STMT;
11099 else if( optionMatch(z, "close") ){
11100 mType |= SQLITE_TRACE_CLOSE;
11102 else {
11103 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
11104 rc = 1;
11105 goto meta_command_exit;
11107 }else{
11108 output_file_close(p->traceOut);
11109 p->traceOut = output_file_open(z, 0);
11112 if( p->traceOut==0 ){
11113 sqlite3_trace_v2(p->db, 0, 0, 0);
11114 }else{
11115 if( mType==0 ) mType = SQLITE_TRACE_STMT;
11116 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11118 }else
11119 #endif /* !defined(SQLITE_OMIT_TRACE) */
11121 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11122 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
11123 int ii;
11124 int lenOpt;
11125 char *zOpt;
11126 if( nArg<2 ){
11127 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
11128 rc = 1;
11129 goto meta_command_exit;
11131 open_db(p, 0);
11132 zOpt = azArg[1];
11133 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11134 lenOpt = (int)strlen(zOpt);
11135 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11136 assert( azArg[nArg]==0 );
11137 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11138 }else{
11139 for(ii=1; ii<nArg; ii++){
11140 sqlite3_create_module(p->db, azArg[ii], 0, 0);
11143 }else
11144 #endif
11146 #if SQLITE_USER_AUTHENTICATION
11147 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
11148 if( nArg<2 ){
11149 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
11150 rc = 1;
11151 goto meta_command_exit;
11153 open_db(p, 0);
11154 if( cli_strcmp(azArg[1],"login")==0 ){
11155 if( nArg!=4 ){
11156 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
11157 rc = 1;
11158 goto meta_command_exit;
11160 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11161 strlen30(azArg[3]));
11162 if( rc ){
11163 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
11164 rc = 1;
11166 }else if( cli_strcmp(azArg[1],"add")==0 ){
11167 if( nArg!=5 ){
11168 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
11169 rc = 1;
11170 goto meta_command_exit;
11172 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11173 booleanValue(azArg[4]));
11174 if( rc ){
11175 raw_printf(stderr, "User-Add failed: %d\n", rc);
11176 rc = 1;
11178 }else if( cli_strcmp(azArg[1],"edit")==0 ){
11179 if( nArg!=5 ){
11180 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
11181 rc = 1;
11182 goto meta_command_exit;
11184 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11185 booleanValue(azArg[4]));
11186 if( rc ){
11187 raw_printf(stderr, "User-Edit failed: %d\n", rc);
11188 rc = 1;
11190 }else if( cli_strcmp(azArg[1],"delete")==0 ){
11191 if( nArg!=3 ){
11192 raw_printf(stderr, "Usage: .user delete USER\n");
11193 rc = 1;
11194 goto meta_command_exit;
11196 rc = sqlite3_user_delete(p->db, azArg[2]);
11197 if( rc ){
11198 raw_printf(stderr, "User-Delete failed: %d\n", rc);
11199 rc = 1;
11201 }else{
11202 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
11203 rc = 1;
11204 goto meta_command_exit;
11206 }else
11207 #endif /* SQLITE_USER_AUTHENTICATION */
11209 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
11210 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11211 sqlite3_libversion(), sqlite3_sourceid());
11212 /* BEGIN SQLCIPHER */
11213 #ifdef SQLITE_HAS_CODEC
11215 extern char* sqlcipher_version();
11216 char *sqlcipher_ver = sqlcipher_version();
11217 utf8_printf(p->out, "SQLCipher %s\n", sqlcipher_ver);
11218 sqlite3_free(sqlcipher_ver);
11220 #endif
11221 /* END SQLCIPHER */
11222 #if SQLITE_HAVE_ZLIB
11223 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
11224 #endif
11225 #define CTIMEOPT_VAL_(opt) #opt
11226 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11227 #if defined(__clang__) && defined(__clang_major__)
11228 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11229 CTIMEOPT_VAL(__clang_minor__) "."
11230 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
11231 #elif defined(_MSC_VER)
11232 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
11233 #elif defined(__GNUC__) && defined(__VERSION__)
11234 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
11235 #endif
11236 }else
11238 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
11239 const char *zDbName = nArg==2 ? azArg[1] : "main";
11240 sqlite3_vfs *pVfs = 0;
11241 if( p->db ){
11242 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11243 if( pVfs ){
11244 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
11245 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
11246 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
11247 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11250 }else
11252 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
11253 sqlite3_vfs *pVfs;
11254 sqlite3_vfs *pCurrent = 0;
11255 if( p->db ){
11256 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11258 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11259 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
11260 pVfs==pCurrent ? " <--- CURRENT" : "");
11261 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
11262 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
11263 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11264 if( pVfs->pNext ){
11265 raw_printf(p->out, "-----------------------------------\n");
11268 }else
11270 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
11271 const char *zDbName = nArg==2 ? azArg[1] : "main";
11272 char *zVfsName = 0;
11273 if( p->db ){
11274 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11275 if( zVfsName ){
11276 utf8_printf(p->out, "%s\n", zVfsName);
11277 sqlite3_free(zVfsName);
11280 }else
11282 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
11283 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11284 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11285 }else
11287 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
11288 int j;
11289 assert( nArg<=ArraySize(azArg) );
11290 p->nWidth = nArg-1;
11291 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11292 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11293 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11294 for(j=1; j<nArg; j++){
11295 p->colWidth[j-1] = (int)integerValue(azArg[j]);
11297 }else
11300 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
11301 " \"%s\". Enter \".help\" for help\n", azArg[0]);
11302 rc = 1;
11305 meta_command_exit:
11306 if( p->outCount ){
11307 p->outCount--;
11308 if( p->outCount==0 ) output_reset(p);
11310 p->bSafeMode = p->bSafeModePersist;
11311 return rc;
11314 /* Line scan result and intermediate states (supporting scan resumption)
11316 #ifndef CHAR_BIT
11317 # define CHAR_BIT 8
11318 #endif
11319 typedef enum {
11320 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11321 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11322 QSS_Start = 0
11323 } QuickScanState;
11324 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11325 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11326 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11327 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11328 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11331 ** Scan line for classification to guide shell's handling.
11332 ** The scan is resumable for subsequent lines when prior
11333 ** return values are passed as the 2nd argument.
11335 static QuickScanState quickscan(char *zLine, QuickScanState qss,
11336 SCAN_TRACKER_REFTYPE pst){
11337 char cin;
11338 char cWait = (char)qss; /* intentional narrowing loss */
11339 if( cWait==0 ){
11340 PlainScan:
11341 assert( cWait==0 );
11342 while( (cin = *zLine++)!=0 ){
11343 if( IsSpace(cin) )
11344 continue;
11345 switch (cin){
11346 case '-':
11347 if( *zLine!='-' )
11348 break;
11349 while((cin = *++zLine)!=0 )
11350 if( cin=='\n')
11351 goto PlainScan;
11352 return qss;
11353 case ';':
11354 qss |= QSS_EndingSemi;
11355 continue;
11356 case '/':
11357 if( *zLine=='*' ){
11358 ++zLine;
11359 cWait = '*';
11360 CONTINUE_PROMPT_AWAITS(pst, "/*");
11361 qss = QSS_SETV(qss, cWait);
11362 goto TermScan;
11364 break;
11365 case '[':
11366 cin = ']';
11367 deliberate_fall_through;
11368 case '`': case '\'': case '"':
11369 cWait = cin;
11370 qss = QSS_HasDark | cWait;
11371 CONTINUE_PROMPT_AWAITC(pst, cin);
11372 goto TermScan;
11373 case '(':
11374 CONTINUE_PAREN_INCR(pst, 1);
11375 break;
11376 case ')':
11377 CONTINUE_PAREN_INCR(pst, -1);
11378 break;
11379 default:
11380 break;
11382 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11384 }else{
11385 TermScan:
11386 while( (cin = *zLine++)!=0 ){
11387 if( cin==cWait ){
11388 switch( cWait ){
11389 case '*':
11390 if( *zLine != '/' )
11391 continue;
11392 ++zLine;
11393 cWait = 0;
11394 CONTINUE_PROMPT_AWAITC(pst, 0);
11395 qss = QSS_SETV(qss, 0);
11396 goto PlainScan;
11397 case '`': case '\'': case '"':
11398 if(*zLine==cWait){
11399 /* Swallow doubled end-delimiter.*/
11400 ++zLine;
11401 continue;
11403 deliberate_fall_through;
11404 case ']':
11405 cWait = 0;
11406 CONTINUE_PROMPT_AWAITC(pst, 0);
11407 qss = QSS_SETV(qss, 0);
11408 goto PlainScan;
11409 default: assert(0);
11414 return qss;
11418 ** Return TRUE if the line typed in is an SQL command terminator other
11419 ** than a semi-colon. The SQL Server style "go" command is understood
11420 ** as is the Oracle "/".
11422 static int line_is_command_terminator(char *zLine){
11423 while( IsSpace(zLine[0]) ){ zLine++; };
11424 if( zLine[0]=='/' )
11425 zLine += 1; /* Oracle */
11426 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11427 zLine += 2; /* SQL Server */
11428 else
11429 return 0;
11430 return quickscan(zLine, QSS_Start, 0)==QSS_Start;
11434 ** The CLI needs a working sqlite3_complete() to work properly. So error
11435 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
11437 #ifdef SQLITE_OMIT_COMPLETE
11438 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
11439 #endif
11442 ** Return true if zSql is a complete SQL statement. Return false if it
11443 ** ends in the middle of a string literal or C-style comment.
11445 static int line_is_complete(char *zSql, int nSql){
11446 int rc;
11447 if( zSql==0 ) return 1;
11448 zSql[nSql] = ';';
11449 zSql[nSql+1] = 0;
11450 rc = sqlite3_complete(zSql);
11451 zSql[nSql] = 0;
11452 return rc;
11456 ** Run a single line of SQL. Return the number of errors.
11458 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
11459 int rc;
11460 char *zErrMsg = 0;
11462 open_db(p, 0);
11463 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
11464 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11465 BEGIN_TIMER;
11466 rc = shell_exec(p, zSql, &zErrMsg);
11467 END_TIMER;
11468 if( rc || zErrMsg ){
11469 char zPrefix[100];
11470 const char *zErrorTail;
11471 const char *zErrorType;
11472 if( zErrMsg==0 ){
11473 zErrorType = "Error";
11474 zErrorTail = sqlite3_errmsg(p->db);
11475 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
11476 zErrorType = "Parse error";
11477 zErrorTail = &zErrMsg[12];
11478 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
11479 zErrorType = "Runtime error";
11480 zErrorTail = &zErrMsg[10];
11481 }else{
11482 zErrorType = "Error";
11483 zErrorTail = zErrMsg;
11485 if( in!=0 || !stdin_is_interactive ){
11486 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
11487 "%s near line %d:", zErrorType, startline);
11488 }else{
11489 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
11491 utf8_printf(stderr, "%s %s\n", zPrefix, zErrorTail);
11492 sqlite3_free(zErrMsg);
11493 zErrMsg = 0;
11494 return 1;
11495 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
11496 char zLineBuf[2000];
11497 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
11498 "changes: %lld total_changes: %lld",
11499 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
11500 raw_printf(p->out, "%s\n", zLineBuf);
11502 return 0;
11505 static void echo_group_input(ShellState *p, const char *zDo){
11506 if( ShellHasFlag(p, SHFLG_Echo) ) utf8_printf(p->out, "%s\n", zDo);
11509 #ifdef SQLITE_SHELL_FIDDLE
11511 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
11512 ** impl because we need the global shellState and cannot access it from that
11513 ** function without moving lots of code around (creating a larger/messier diff).
11515 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
11516 /* Parse the next line from shellState.wasm.zInput. */
11517 const char *zBegin = shellState.wasm.zPos;
11518 const char *z = zBegin;
11519 char *zLine = 0;
11520 i64 nZ = 0;
11522 UNUSED_PARAMETER(in);
11523 UNUSED_PARAMETER(isContinuation);
11524 if(!z || !*z){
11525 return 0;
11527 while(*z && isspace(*z)) ++z;
11528 zBegin = z;
11529 for(; *z && '\n'!=*z; ++nZ, ++z){}
11530 if(nZ>0 && '\r'==zBegin[nZ-1]){
11531 --nZ;
11533 shellState.wasm.zPos = z;
11534 zLine = realloc(zPrior, nZ+1);
11535 shell_check_oom(zLine);
11536 memcpy(zLine, zBegin, nZ);
11537 zLine[nZ] = 0;
11538 return zLine;
11540 #endif /* SQLITE_SHELL_FIDDLE */
11543 ** Read input from *in and process it. If *in==0 then input
11544 ** is interactive - the user is typing it it. Otherwise, input
11545 ** is coming from a file or device. A prompt is issued and history
11546 ** is saved only if input is interactive. An interrupt signal will
11547 ** cause this routine to exit immediately, unless input is interactive.
11549 ** Return the number of errors.
11551 static int process_input(ShellState *p){
11552 char *zLine = 0; /* A single input line */
11553 char *zSql = 0; /* Accumulated SQL text */
11554 i64 nLine; /* Length of current line */
11555 i64 nSql = 0; /* Bytes of zSql[] used */
11556 i64 nAlloc = 0; /* Allocated zSql[] space */
11557 int rc; /* Error code */
11558 int errCnt = 0; /* Number of errors seen */
11559 i64 startline = 0; /* Line number for start of current input */
11560 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
11562 if( p->inputNesting==MAX_INPUT_NESTING ){
11563 /* This will be more informative in a later version. */
11564 utf8_printf(stderr,"Input nesting limit (%d) reached at line %d."
11565 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
11566 return 1;
11568 ++p->inputNesting;
11569 p->lineno = 0;
11570 CONTINUE_PROMPT_RESET;
11571 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
11572 fflush(p->out);
11573 zLine = one_input_line(p->in, zLine, nSql>0);
11574 if( zLine==0 ){
11575 /* End of input */
11576 if( p->in==0 && stdin_is_interactive ) printf("\n");
11577 break;
11579 if( seenInterrupt ){
11580 if( p->in!=0 ) break;
11581 seenInterrupt = 0;
11583 p->lineno++;
11584 if( QSS_INPLAIN(qss)
11585 && line_is_command_terminator(zLine)
11586 && line_is_complete(zSql, nSql) ){
11587 memcpy(zLine,";",2);
11589 qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
11590 if( QSS_PLAINWHITE(qss) && nSql==0 ){
11591 /* Just swallow single-line whitespace */
11592 echo_group_input(p, zLine);
11593 qss = QSS_Start;
11594 continue;
11596 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
11597 CONTINUE_PROMPT_RESET;
11598 echo_group_input(p, zLine);
11599 if( zLine[0]=='.' ){
11600 rc = do_meta_command(zLine, p);
11601 if( rc==2 ){ /* exit requested */
11602 break;
11603 }else if( rc ){
11604 errCnt++;
11607 qss = QSS_Start;
11608 continue;
11610 /* No single-line dispositions remain; accumulate line(s). */
11611 nLine = strlen(zLine);
11612 if( nSql+nLine+2>=nAlloc ){
11613 /* Grow buffer by half-again increments when big. */
11614 nAlloc = nSql+(nSql>>1)+nLine+100;
11615 zSql = realloc(zSql, nAlloc);
11616 shell_check_oom(zSql);
11618 if( nSql==0 ){
11619 i64 i;
11620 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
11621 assert( nAlloc>0 && zSql!=0 );
11622 memcpy(zSql, zLine+i, nLine+1-i);
11623 startline = p->lineno;
11624 nSql = nLine-i;
11625 }else{
11626 zSql[nSql++] = '\n';
11627 memcpy(zSql+nSql, zLine, nLine+1);
11628 nSql += nLine;
11630 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
11631 echo_group_input(p, zSql);
11632 errCnt += runOneSqlLine(p, zSql, p->in, startline);
11633 CONTINUE_PROMPT_RESET;
11634 nSql = 0;
11635 if( p->outCount ){
11636 output_reset(p);
11637 p->outCount = 0;
11638 }else{
11639 clearTempFile(p);
11641 p->bSafeMode = p->bSafeModePersist;
11642 qss = QSS_Start;
11643 }else if( nSql && QSS_PLAINWHITE(qss) ){
11644 echo_group_input(p, zSql);
11645 nSql = 0;
11646 qss = QSS_Start;
11649 if( nSql ){
11650 /* This may be incomplete. Let the SQL parser deal with that. */
11651 echo_group_input(p, zSql);
11652 errCnt += runOneSqlLine(p, zSql, p->in, startline);
11653 CONTINUE_PROMPT_RESET;
11655 free(zSql);
11656 free(zLine);
11657 --p->inputNesting;
11658 return errCnt>0;
11662 ** Return a pathname which is the user's home directory. A
11663 ** 0 return indicates an error of some kind.
11665 static char *find_home_dir(int clearFlag){
11666 static char *home_dir = NULL;
11667 if( clearFlag ){
11668 free(home_dir);
11669 home_dir = 0;
11670 return 0;
11672 if( home_dir ) return home_dir;
11674 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11675 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
11677 struct passwd *pwent;
11678 uid_t uid = getuid();
11679 if( (pwent=getpwuid(uid)) != NULL) {
11680 home_dir = pwent->pw_dir;
11683 #endif
11685 #if defined(_WIN32_WCE)
11686 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11688 home_dir = "/";
11689 #else
11691 #if defined(_WIN32) || defined(WIN32)
11692 if (!home_dir) {
11693 home_dir = getenv("USERPROFILE");
11695 #endif
11697 if (!home_dir) {
11698 home_dir = getenv("HOME");
11701 #if defined(_WIN32) || defined(WIN32)
11702 if (!home_dir) {
11703 char *zDrive, *zPath;
11704 int n;
11705 zDrive = getenv("HOMEDRIVE");
11706 zPath = getenv("HOMEPATH");
11707 if( zDrive && zPath ){
11708 n = strlen30(zDrive) + strlen30(zPath) + 1;
11709 home_dir = malloc( n );
11710 if( home_dir==0 ) return 0;
11711 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
11712 return home_dir;
11714 home_dir = "c:\\";
11716 #endif
11718 #endif /* !_WIN32_WCE */
11720 if( home_dir ){
11721 i64 n = strlen(home_dir) + 1;
11722 char *z = malloc( n );
11723 if( z ) memcpy(z, home_dir, n);
11724 home_dir = z;
11727 return home_dir;
11731 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
11732 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
11733 ** the path to it, else return 0. The result is cached for
11734 ** subsequent calls.
11736 static const char *find_xdg_config(void){
11737 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
11738 || defined(__RTP__) || defined(_WRS_KERNEL)
11739 return 0;
11740 #else
11741 static int alreadyTried = 0;
11742 static char *zConfig = 0;
11743 const char *zXdgHome;
11745 if( alreadyTried!=0 ){
11746 return zConfig;
11748 alreadyTried = 1;
11749 zXdgHome = getenv("XDG_CONFIG_HOME");
11750 if( zXdgHome==0 ){
11751 return 0;
11753 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
11754 shell_check_oom(zConfig);
11755 if( access(zConfig,0)!=0 ){
11756 sqlite3_free(zConfig);
11757 zConfig = 0;
11759 return zConfig;
11760 #endif
11764 ** Read input from the file given by sqliterc_override. Or if that
11765 ** parameter is NULL, take input from the first of find_xdg_config()
11766 ** or ~/.sqliterc which is found.
11768 ** Returns the number of errors.
11770 static void process_sqliterc(
11771 ShellState *p, /* Configuration data */
11772 const char *sqliterc_override /* Name of config file. NULL to use default */
11774 char *home_dir = NULL;
11775 const char *sqliterc = sqliterc_override;
11776 char *zBuf = 0;
11777 FILE *inSaved = p->in;
11778 int savedLineno = p->lineno;
11780 if( sqliterc == NULL ){
11781 sqliterc = find_xdg_config();
11783 if( sqliterc == NULL ){
11784 home_dir = find_home_dir(0);
11785 if( home_dir==0 ){
11786 raw_printf(stderr, "-- warning: cannot find home directory;"
11787 " cannot read ~/.sqliterc\n");
11788 return;
11790 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
11791 shell_check_oom(zBuf);
11792 sqliterc = zBuf;
11794 p->in = fopen(sqliterc,"rb");
11795 if( p->in ){
11796 if( stdin_is_interactive ){
11797 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
11799 if( process_input(p) && bail_on_error ) exit(1);
11800 fclose(p->in);
11801 }else if( sqliterc_override!=0 ){
11802 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
11803 if( bail_on_error ) exit(1);
11805 p->in = inSaved;
11806 p->lineno = savedLineno;
11807 sqlite3_free(zBuf);
11811 ** Show available command line options
11813 static const char zOptions[] =
11814 " -- treat no subsequent arguments as options\n"
11815 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11816 " -A ARGS... run \".archive ARGS\" and exit\n"
11817 #endif
11818 " -append append the database to the end of the file\n"
11819 " -ascii set output mode to 'ascii'\n"
11820 " -bail stop after hitting an error\n"
11821 " -batch force batch I/O\n"
11822 " -box set output mode to 'box'\n"
11823 " -column set output mode to 'column'\n"
11824 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
11825 " -csv set output mode to 'csv'\n"
11826 #if !defined(SQLITE_OMIT_DESERIALIZE)
11827 " -deserialize open the database using sqlite3_deserialize()\n"
11828 #endif
11829 " -echo print inputs before execution\n"
11830 " -init FILENAME read/process named file\n"
11831 " -[no]header turn headers on or off\n"
11832 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11833 " -heap SIZE Size of heap for memsys3 or memsys5\n"
11834 #endif
11835 " -help show this message\n"
11836 " -html set output mode to HTML\n"
11837 " -interactive force interactive I/O\n"
11838 " -json set output mode to 'json'\n"
11839 " -line set output mode to 'line'\n"
11840 " -list set output mode to 'list'\n"
11841 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
11842 " -markdown set output mode to 'markdown'\n"
11843 #if !defined(SQLITE_OMIT_DESERIALIZE)
11844 " -maxsize N maximum size for a --deserialize database\n"
11845 #endif
11846 " -memtrace trace all memory allocations and deallocations\n"
11847 " -mmap N default mmap size set to N\n"
11848 #ifdef SQLITE_ENABLE_MULTIPLEX
11849 " -multiplex enable the multiplexor VFS\n"
11850 #endif
11851 " -newline SEP set output row separator. Default: '\\n'\n"
11852 " -nofollow refuse to open symbolic links to database files\n"
11853 " -nonce STRING set the safe-mode escape nonce\n"
11854 " -nullvalue TEXT set text string for NULL values. Default ''\n"
11855 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
11856 " -quote set output mode to 'quote'\n"
11857 " -readonly open the database read-only\n"
11858 " -safe enable safe-mode\n"
11859 " -separator SEP set output column separator. Default: '|'\n"
11860 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
11861 " -sorterref SIZE sorter references threshold size\n"
11862 #endif
11863 " -stats print memory stats before each finalize\n"
11864 " -table set output mode to 'table'\n"
11865 " -tabs set output mode to 'tabs'\n"
11866 " -unsafe-testing allow unsafe commands and modes for testing\n"
11867 #if SHELL_WIN_UTF8_OPT
11868 " -utf8 setup interactive console code page for UTF-8\n"
11869 #endif
11870 " -version show SQLite version\n"
11871 " -vfs NAME use NAME as the default VFS\n"
11872 #ifdef SQLITE_ENABLE_VFSTRACE
11873 " -vfstrace enable tracing of all VFS calls\n"
11874 #endif
11875 #ifdef SQLITE_HAVE_ZLIB
11876 " -zip open the file as a ZIP Archive\n"
11877 #endif
11879 static void usage(int showDetail){
11880 utf8_printf(stderr,
11881 "Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
11882 "FILENAME is the name of an SQLite database. A new database is created\n"
11883 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
11884 if( showDetail ){
11885 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
11886 }else{
11887 raw_printf(stderr, "Use the -help option for additional information\n");
11889 exit(1);
11893 ** Internal check: Verify that the SQLite is uninitialized. Print a
11894 ** error message if it is initialized.
11896 static void verify_uninitialized(void){
11897 if( sqlite3_config(-1)==SQLITE_MISUSE ){
11898 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
11899 " initialization.\n");
11904 ** Initialize the state information in data
11906 static void main_init(ShellState *data) {
11907 memset(data, 0, sizeof(*data));
11908 data->normalMode = data->cMode = data->mode = MODE_List;
11909 data->autoExplain = 1;
11910 data->pAuxDb = &data->aAuxDb[0];
11911 memcpy(data->colSeparator,SEP_Column, 2);
11912 memcpy(data->rowSeparator,SEP_Row, 2);
11913 data->showHeader = 0;
11914 data->shellFlgs = SHFLG_Lookaside;
11915 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
11916 #if !defined(SQLITE_SHELL_FIDDLE)
11917 verify_uninitialized();
11918 #endif
11919 sqlite3_config(SQLITE_CONFIG_URI, 1);
11920 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
11921 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
11922 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
11926 ** Output text to the console in a font that attracts extra attention.
11928 #ifdef _WIN32
11929 static void printBold(const char *zText){
11930 #if !SQLITE_OS_WINRT
11931 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
11932 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
11933 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
11934 SetConsoleTextAttribute(out,
11935 FOREGROUND_RED|FOREGROUND_INTENSITY
11937 #endif
11938 printf("%s", zText);
11939 #if !SQLITE_OS_WINRT
11940 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
11941 #endif
11943 #else
11944 static void printBold(const char *zText){
11945 printf("\033[1m%s\033[0m", zText);
11947 #endif
11950 ** Get the argument to an --option. Throw an error and die if no argument
11951 ** is available.
11953 static char *cmdline_option_value(int argc, char **argv, int i){
11954 if( i==argc ){
11955 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
11956 argv[0], argv[argc-1]);
11957 exit(1);
11959 return argv[i];
11962 static void sayAbnormalExit(void){
11963 if( seenInterrupt ) fprintf(stderr, "Program interrupted.\n");
11966 #ifndef SQLITE_SHELL_IS_UTF8
11967 # if (defined(_WIN32) || defined(WIN32)) \
11968 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11969 # define SQLITE_SHELL_IS_UTF8 (0)
11970 # else
11971 # define SQLITE_SHELL_IS_UTF8 (1)
11972 # endif
11973 #endif
11975 #ifdef SQLITE_SHELL_FIDDLE
11976 # define main fiddle_main
11977 #endif
11979 #if SQLITE_SHELL_IS_UTF8
11980 int SQLITE_CDECL main(int argc, char **argv){
11981 #else
11982 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
11983 char **argv;
11984 #endif
11985 #ifdef SQLITE_DEBUG
11986 sqlite3_int64 mem_main_enter = 0;
11987 #endif
11988 char *zErrMsg = 0;
11989 #ifdef SQLITE_SHELL_FIDDLE
11990 # define data shellState
11991 #else
11992 ShellState data;
11993 #endif
11994 const char *zInitFile = 0;
11995 int i;
11996 int rc = 0;
11997 int warnInmemoryDb = 0;
11998 int readStdin = 1;
11999 int nCmd = 0;
12000 int nOptsEnd = argc;
12001 char **azCmd = 0;
12002 const char *zVfs = 0; /* Value of -vfs command-line option */
12003 #if !SQLITE_SHELL_IS_UTF8
12004 char **argvToFree = 0;
12005 int argcToFree = 0;
12006 #endif
12007 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12009 #ifdef SQLITE_SHELL_FIDDLE
12010 stdin_is_interactive = 0;
12011 stdout_is_console = 1;
12012 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12013 #else
12014 stdin_is_interactive = isatty(0);
12015 stdout_is_console = isatty(1);
12016 #endif
12017 #if SHELL_WIN_UTF8_OPT
12018 atexit(console_restore); /* Needs revision for CLI as library call */
12019 #endif
12020 atexit(sayAbnormalExit);
12021 #ifdef SQLITE_DEBUG
12022 mem_main_enter = sqlite3_memory_used();
12023 #endif
12024 #if !defined(_WIN32_WCE)
12025 if( getenv("SQLITE_DEBUG_BREAK") ){
12026 if( isatty(0) && isatty(2) ){
12027 fprintf(stderr,
12028 "attach debugger to process %d and press any key to continue.\n",
12029 GETPID());
12030 fgetc(stdin);
12031 }else{
12032 #if defined(_WIN32) || defined(WIN32)
12033 #if SQLITE_OS_WINRT
12034 __debugbreak();
12035 #else
12036 DebugBreak();
12037 #endif
12038 #elif defined(SIGTRAP)
12039 raise(SIGTRAP);
12040 #endif
12043 #endif
12044 /* Register a valid signal handler early, before much else is done. */
12045 #ifdef SIGINT
12046 signal(SIGINT, interrupt_handler);
12047 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12048 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
12049 fprintf(stderr, "No ^C handler.\n");
12051 #endif
12053 #if USE_SYSTEM_SQLITE+0!=1
12054 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12055 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
12056 sqlite3_sourceid(), SQLITE_SOURCE_ID);
12057 exit(1);
12059 #endif
12060 main_init(&data);
12062 /* On Windows, we must translate command-line arguments into UTF-8.
12063 ** The SQLite memory allocator subsystem has to be enabled in order to
12064 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
12065 ** subsequent sqlite3_config() calls will work. So copy all results into
12066 ** memory that does not come from the SQLite memory allocator.
12068 #if !SQLITE_SHELL_IS_UTF8
12069 sqlite3_initialize();
12070 argvToFree = malloc(sizeof(argv[0])*argc*2);
12071 shell_check_oom(argvToFree);
12072 argcToFree = argc;
12073 argv = argvToFree + argc;
12074 for(i=0; i<argc; i++){
12075 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12076 i64 n;
12077 shell_check_oom(z);
12078 n = strlen(z);
12079 argv[i] = malloc( n+1 );
12080 shell_check_oom(argv[i]);
12081 memcpy(argv[i], z, n+1);
12082 argvToFree[i] = argv[i];
12083 sqlite3_free(z);
12085 sqlite3_shutdown();
12086 #endif
12088 assert( argc>=1 && argv && argv[0] );
12089 Argv0 = argv[0];
12091 #ifdef SQLITE_SHELL_DBNAME_PROC
12093 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12094 ** of a C-function that will provide the name of the database file. Use
12095 ** this compile-time option to embed this shell program in larger
12096 ** applications. */
12097 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12098 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12099 warnInmemoryDb = 0;
12101 #endif
12103 /* Do an initial pass through the command-line argument to locate
12104 ** the name of the database file, the name of the initialization file,
12105 ** the size of the alternative malloc heap,
12106 ** and the first command to execute.
12108 #ifndef SQLITE_SHELL_FIDDLE
12109 verify_uninitialized();
12110 #endif
12111 for(i=1; i<argc; i++){
12112 char *z;
12113 z = argv[i];
12114 if( z[0]!='-' || i>nOptsEnd ){
12115 if( data.aAuxDb->zDbFilename==0 ){
12116 data.aAuxDb->zDbFilename = z;
12117 }else{
12118 /* Excesss arguments are interpreted as SQL (or dot-commands) and
12119 ** mean that nothing is read from stdin */
12120 readStdin = 0;
12121 nCmd++;
12122 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12123 shell_check_oom(azCmd);
12124 azCmd[nCmd-1] = z;
12126 continue;
12128 if( z[1]=='-' ) z++;
12129 if( cli_strcmp(z, "-")==0 ){
12130 nOptsEnd = i;
12131 continue;
12132 }else if( cli_strcmp(z,"-separator")==0
12133 || cli_strcmp(z,"-nullvalue")==0
12134 || cli_strcmp(z,"-newline")==0
12135 || cli_strcmp(z,"-cmd")==0
12137 (void)cmdline_option_value(argc, argv, ++i);
12138 }else if( cli_strcmp(z,"-init")==0 ){
12139 zInitFile = cmdline_option_value(argc, argv, ++i);
12140 }else if( cli_strcmp(z,"-batch")==0 ){
12141 /* Need to check for batch mode here to so we can avoid printing
12142 ** informational messages (like from process_sqliterc) before
12143 ** we do the actual processing of arguments later in a second pass.
12145 stdin_is_interactive = 0;
12146 }else if( cli_strcmp(z,"-heap")==0 ){
12147 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12148 const char *zSize;
12149 sqlite3_int64 szHeap;
12151 zSize = cmdline_option_value(argc, argv, ++i);
12152 szHeap = integerValue(zSize);
12153 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12154 verify_uninitialized();
12155 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12156 #else
12157 (void)cmdline_option_value(argc, argv, ++i);
12158 #endif
12159 }else if( cli_strcmp(z,"-pagecache")==0 ){
12160 sqlite3_int64 n, sz;
12161 sz = integerValue(cmdline_option_value(argc,argv,++i));
12162 if( sz>70000 ) sz = 70000;
12163 if( sz<0 ) sz = 0;
12164 n = integerValue(cmdline_option_value(argc,argv,++i));
12165 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12166 n = 0xffffffffffffLL/sz;
12168 verify_uninitialized();
12169 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12170 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12171 data.shellFlgs |= SHFLG_Pagecache;
12172 }else if( cli_strcmp(z,"-lookaside")==0 ){
12173 int n, sz;
12174 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12175 if( sz<0 ) sz = 0;
12176 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12177 if( n<0 ) n = 0;
12178 verify_uninitialized();
12179 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12180 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12181 }else if( cli_strcmp(z,"-threadsafe")==0 ){
12182 int n;
12183 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12184 verify_uninitialized();
12185 switch( n ){
12186 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
12187 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
12188 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
12190 #ifdef SQLITE_ENABLE_VFSTRACE
12191 }else if( cli_strcmp(z,"-vfstrace")==0 ){
12192 extern int vfstrace_register(
12193 const char *zTraceName,
12194 const char *zOldVfsName,
12195 int (*xOut)(const char*,void*),
12196 void *pOutArg,
12197 int makeDefault
12199 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12200 #endif
12201 #ifdef SQLITE_ENABLE_MULTIPLEX
12202 }else if( cli_strcmp(z,"-multiplex")==0 ){
12203 extern int sqlite3_multiple_initialize(const char*,int);
12204 sqlite3_multiplex_initialize(0, 1);
12205 #endif
12206 }else if( cli_strcmp(z,"-mmap")==0 ){
12207 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12208 verify_uninitialized();
12209 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12210 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
12211 }else if( cli_strcmp(z,"-sorterref")==0 ){
12212 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12213 verify_uninitialized();
12214 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12215 #endif
12216 }else if( cli_strcmp(z,"-vfs")==0 ){
12217 zVfs = cmdline_option_value(argc, argv, ++i);
12218 #ifdef SQLITE_HAVE_ZLIB
12219 }else if( cli_strcmp(z,"-zip")==0 ){
12220 data.openMode = SHELL_OPEN_ZIPFILE;
12221 #endif
12222 }else if( cli_strcmp(z,"-append")==0 ){
12223 data.openMode = SHELL_OPEN_APPENDVFS;
12224 #ifndef SQLITE_OMIT_DESERIALIZE
12225 }else if( cli_strcmp(z,"-deserialize")==0 ){
12226 data.openMode = SHELL_OPEN_DESERIALIZE;
12227 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12228 data.szMax = integerValue(argv[++i]);
12229 #endif
12230 }else if( cli_strcmp(z,"-readonly")==0 ){
12231 data.openMode = SHELL_OPEN_READONLY;
12232 }else if( cli_strcmp(z,"-nofollow")==0 ){
12233 data.openFlags = SQLITE_OPEN_NOFOLLOW;
12234 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12235 }else if( cli_strncmp(z, "-A",2)==0 ){
12236 /* All remaining command-line arguments are passed to the ".archive"
12237 ** command, so ignore them */
12238 break;
12239 #endif
12240 }else if( cli_strcmp(z, "-memtrace")==0 ){
12241 sqlite3MemTraceActivate(stderr);
12242 }else if( cli_strcmp(z,"-bail")==0 ){
12243 bail_on_error = 1;
12244 }else if( cli_strcmp(z,"-nonce")==0 ){
12245 free(data.zNonce);
12246 data.zNonce = strdup(argv[++i]);
12247 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
12248 ShellSetFlag(&data,SHFLG_TestingMode);
12249 }else if( cli_strcmp(z,"-safe")==0 ){
12250 /* no-op - catch this on the second pass */
12253 #ifndef SQLITE_SHELL_FIDDLE
12254 verify_uninitialized();
12255 #endif
12258 #ifdef SQLITE_SHELL_INIT_PROC
12260 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12261 ** of a C-function that will perform initialization actions on SQLite that
12262 ** occur just before or after sqlite3_initialize(). Use this compile-time
12263 ** option to embed this shell program in larger applications. */
12264 extern void SQLITE_SHELL_INIT_PROC(void);
12265 SQLITE_SHELL_INIT_PROC();
12267 #else
12268 /* All the sqlite3_config() calls have now been made. So it is safe
12269 ** to call sqlite3_initialize() and process any command line -vfs option. */
12270 sqlite3_initialize();
12271 #endif
12273 if( zVfs ){
12274 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12275 if( pVfs ){
12276 sqlite3_vfs_register(pVfs, 1);
12277 }else{
12278 utf8_printf(stderr, "no such VFS: \"%s\"\n", zVfs);
12279 exit(1);
12283 if( data.pAuxDb->zDbFilename==0 ){
12284 #ifndef SQLITE_OMIT_MEMORYDB
12285 data.pAuxDb->zDbFilename = ":memory:";
12286 warnInmemoryDb = argc==1;
12287 #else
12288 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
12289 return 1;
12290 #endif
12292 data.out = stdout;
12293 #ifndef SQLITE_SHELL_FIDDLE
12294 sqlite3_appendvfs_init(0,0,0);
12295 #endif
12297 /* Go ahead and open the database file if it already exists. If the
12298 ** file does not exist, delay opening it. This prevents empty database
12299 ** files from being created if a user mistypes the database name argument
12300 ** to the sqlite command-line tool.
12302 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12303 open_db(&data, 0);
12306 /* Process the initialization file if there is one. If no -init option
12307 ** is given on the command line, look for a file named ~/.sqliterc and
12308 ** try to process it.
12310 process_sqliterc(&data,zInitFile);
12312 /* Make a second pass through the command-line argument and set
12313 ** options. This second pass is delayed until after the initialization
12314 ** file is processed so that the command-line arguments will override
12315 ** settings in the initialization file.
12317 for(i=1; i<argc; i++){
12318 char *z = argv[i];
12319 if( z[0]!='-' || i>=nOptsEnd ) continue;
12320 if( z[1]=='-' ){ z++; }
12321 if( cli_strcmp(z,"-init")==0 ){
12322 i++;
12323 }else if( cli_strcmp(z,"-html")==0 ){
12324 data.mode = MODE_Html;
12325 }else if( cli_strcmp(z,"-list")==0 ){
12326 data.mode = MODE_List;
12327 }else if( cli_strcmp(z,"-quote")==0 ){
12328 data.mode = MODE_Quote;
12329 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12330 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12331 }else if( cli_strcmp(z,"-line")==0 ){
12332 data.mode = MODE_Line;
12333 }else if( cli_strcmp(z,"-column")==0 ){
12334 data.mode = MODE_Column;
12335 }else if( cli_strcmp(z,"-json")==0 ){
12336 data.mode = MODE_Json;
12337 }else if( cli_strcmp(z,"-markdown")==0 ){
12338 data.mode = MODE_Markdown;
12339 }else if( cli_strcmp(z,"-table")==0 ){
12340 data.mode = MODE_Table;
12341 }else if( cli_strcmp(z,"-box")==0 ){
12342 data.mode = MODE_Box;
12343 }else if( cli_strcmp(z,"-csv")==0 ){
12344 data.mode = MODE_Csv;
12345 memcpy(data.colSeparator,",",2);
12346 #ifdef SQLITE_HAVE_ZLIB
12347 }else if( cli_strcmp(z,"-zip")==0 ){
12348 data.openMode = SHELL_OPEN_ZIPFILE;
12349 #endif
12350 }else if( cli_strcmp(z,"-append")==0 ){
12351 data.openMode = SHELL_OPEN_APPENDVFS;
12352 #ifndef SQLITE_OMIT_DESERIALIZE
12353 }else if( cli_strcmp(z,"-deserialize")==0 ){
12354 data.openMode = SHELL_OPEN_DESERIALIZE;
12355 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12356 data.szMax = integerValue(argv[++i]);
12357 #endif
12358 }else if( cli_strcmp(z,"-readonly")==0 ){
12359 data.openMode = SHELL_OPEN_READONLY;
12360 }else if( cli_strcmp(z,"-nofollow")==0 ){
12361 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12362 }else if( cli_strcmp(z,"-ascii")==0 ){
12363 data.mode = MODE_Ascii;
12364 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
12365 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
12366 }else if( cli_strcmp(z,"-tabs")==0 ){
12367 data.mode = MODE_List;
12368 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab);
12369 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row);
12370 }else if( cli_strcmp(z,"-separator")==0 ){
12371 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12372 "%s",cmdline_option_value(argc,argv,++i));
12373 }else if( cli_strcmp(z,"-newline")==0 ){
12374 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12375 "%s",cmdline_option_value(argc,argv,++i));
12376 }else if( cli_strcmp(z,"-nullvalue")==0 ){
12377 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12378 "%s",cmdline_option_value(argc,argv,++i));
12379 }else if( cli_strcmp(z,"-header")==0 ){
12380 data.showHeader = 1;
12381 ShellSetFlag(&data, SHFLG_HeaderSet);
12382 }else if( cli_strcmp(z,"-noheader")==0 ){
12383 data.showHeader = 0;
12384 ShellSetFlag(&data, SHFLG_HeaderSet);
12385 }else if( cli_strcmp(z,"-echo")==0 ){
12386 ShellSetFlag(&data, SHFLG_Echo);
12387 }else if( cli_strcmp(z,"-eqp")==0 ){
12388 data.autoEQP = AUTOEQP_on;
12389 }else if( cli_strcmp(z,"-eqpfull")==0 ){
12390 data.autoEQP = AUTOEQP_full;
12391 }else if( cli_strcmp(z,"-stats")==0 ){
12392 data.statsOn = 1;
12393 }else if( cli_strcmp(z,"-scanstats")==0 ){
12394 data.scanstatsOn = 1;
12395 }else if( cli_strcmp(z,"-backslash")==0 ){
12396 /* Undocumented command-line option: -backslash
12397 ** Causes C-style backslash escapes to be evaluated in SQL statements
12398 ** prior to sending the SQL into SQLite. Useful for injecting
12399 ** crazy bytes in the middle of SQL statements for testing and debugging.
12401 ShellSetFlag(&data, SHFLG_Backslash);
12402 }else if( cli_strcmp(z,"-bail")==0 ){
12403 /* No-op. The bail_on_error flag should already be set. */
12404 }else if( cli_strcmp(z,"-version")==0 ){
12405 /* BEGIN SQLCIPHER */
12406 #ifdef SQLITE_HAS_CODEC
12407 extern char* sqlcipher_version();
12408 char *sqlcipher_ver = sqlcipher_version();
12409 printf("%s %s", sqlite3_libversion(), sqlite3_sourceid());
12410 printf(" (SQLCipher %s)\n", sqlcipher_ver);
12411 sqlite3_free(sqlcipher_ver);
12412 #else
12413 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12414 #endif
12415 /* END SQLCIPHER */
12416 return 0;
12417 }else if( cli_strcmp(z,"-interactive")==0 ){
12418 stdin_is_interactive = 1;
12419 }else if( cli_strcmp(z,"-batch")==0 ){
12420 stdin_is_interactive = 0;
12421 }else if( cli_strcmp(z,"-utf8")==0 ){
12422 #if SHELL_WIN_UTF8_OPT
12423 console_utf8 = 1;
12424 #endif /* SHELL_WIN_UTF8_OPT */
12425 }else if( cli_strcmp(z,"-heap")==0 ){
12426 i++;
12427 }else if( cli_strcmp(z,"-pagecache")==0 ){
12428 i+=2;
12429 }else if( cli_strcmp(z,"-lookaside")==0 ){
12430 i+=2;
12431 }else if( cli_strcmp(z,"-threadsafe")==0 ){
12432 i+=2;
12433 }else if( cli_strcmp(z,"-nonce")==0 ){
12434 i += 2;
12435 }else if( cli_strcmp(z,"-mmap")==0 ){
12436 i++;
12437 }else if( cli_strcmp(z,"-memtrace")==0 ){
12438 i++;
12439 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
12440 }else if( cli_strcmp(z,"-sorterref")==0 ){
12441 i++;
12442 #endif
12443 }else if( cli_strcmp(z,"-vfs")==0 ){
12444 i++;
12445 #ifdef SQLITE_ENABLE_VFSTRACE
12446 }else if( cli_strcmp(z,"-vfstrace")==0 ){
12447 i++;
12448 #endif
12449 #ifdef SQLITE_ENABLE_MULTIPLEX
12450 }else if( cli_strcmp(z,"-multiplex")==0 ){
12451 i++;
12452 #endif
12453 }else if( cli_strcmp(z,"-help")==0 ){
12454 usage(1);
12455 }else if( cli_strcmp(z,"-cmd")==0 ){
12456 /* Run commands that follow -cmd first and separately from commands
12457 ** that simply appear on the command-line. This seems goofy. It would
12458 ** be better if all commands ran in the order that they appear. But
12459 ** we retain the goofy behavior for historical compatibility. */
12460 if( i==argc-1 ) break;
12461 z = cmdline_option_value(argc,argv,++i);
12462 if( z[0]=='.' ){
12463 rc = do_meta_command(z, &data);
12464 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
12465 }else{
12466 open_db(&data, 0);
12467 rc = shell_exec(&data, z, &zErrMsg);
12468 if( zErrMsg!=0 ){
12469 utf8_printf(stderr,"Error: %s\n", zErrMsg);
12470 if( bail_on_error ) return rc!=0 ? rc : 1;
12471 }else if( rc!=0 ){
12472 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
12473 if( bail_on_error ) return rc;
12476 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12477 }else if( cli_strncmp(z, "-A", 2)==0 ){
12478 if( nCmd>0 ){
12479 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
12480 " with \"%s\"\n", z);
12481 return 1;
12483 open_db(&data, OPEN_DB_ZIPFILE);
12484 if( z[2] ){
12485 argv[i] = &z[2];
12486 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
12487 }else{
12488 arDotCommand(&data, 1, argv+i, argc-i);
12490 readStdin = 0;
12491 break;
12492 #endif
12493 }else if( cli_strcmp(z,"-safe")==0 ){
12494 data.bSafeMode = data.bSafeModePersist = 1;
12495 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
12496 /* Acted upon in first pass. */
12497 }else{
12498 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
12499 raw_printf(stderr,"Use -help for a list of options.\n");
12500 return 1;
12502 data.cMode = data.mode;
12504 #if SHELL_WIN_UTF8_OPT
12505 if( console_utf8 && stdin_is_interactive ){
12506 console_prepare();
12507 }else{
12508 setBinaryMode(stdin, 0);
12509 console_utf8 = 0;
12511 #endif
12513 if( !readStdin ){
12514 /* Run all arguments that do not begin with '-' as if they were separate
12515 ** command-line inputs, except for the argToSkip argument which contains
12516 ** the database filename.
12518 for(i=0; i<nCmd; i++){
12519 if( azCmd[i][0]=='.' ){
12520 rc = do_meta_command(azCmd[i], &data);
12521 if( rc ){
12522 free(azCmd);
12523 return rc==2 ? 0 : rc;
12525 }else{
12526 open_db(&data, 0);
12527 echo_group_input(&data, azCmd[i]);
12528 rc = shell_exec(&data, azCmd[i], &zErrMsg);
12529 if( zErrMsg || rc ){
12530 if( zErrMsg!=0 ){
12531 utf8_printf(stderr,"Error: %s\n", zErrMsg);
12532 }else{
12533 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
12535 sqlite3_free(zErrMsg);
12536 free(azCmd);
12537 return rc!=0 ? rc : 1;
12541 }else{
12542 /* Run commands received from standard input
12544 if( stdin_is_interactive ){
12545 char *zHome;
12546 char *zHistory;
12547 int nHistory;
12548 /* BEGIN SQLCIPHER */
12549 #ifdef SQLITE_HAS_CODEC
12550 extern char* sqlcipher_version();
12551 char *sqlcipher_ver = sqlcipher_version();
12552 printf(
12553 "SQLite version %s %.19s" /*extra-version-info*/
12554 " (SQLCipher %s)\n" /*sqlcipher version info*/
12555 "Enter \".help\" for usage hints.\n",
12556 sqlite3_libversion(), sqlite3_sourceid(), sqlcipher_ver
12558 sqlite3_free(sqlcipher_ver);
12559 #else
12560 printf(
12561 "SQLite version %s %.19s\n" /*extra-version-info*/
12562 "Enter \".help\" for usage hints.\n",
12563 sqlite3_libversion(), sqlite3_sourceid()
12565 #endif
12566 /* END SQLCIPHER */
12567 if( warnInmemoryDb ){
12568 printf("Connected to a ");
12569 printBold("transient in-memory database");
12570 printf(".\nUse \".open FILENAME\" to reopen on a "
12571 "persistent database.\n");
12573 zHistory = getenv("SQLITE_HISTORY");
12574 if( zHistory ){
12575 zHistory = strdup(zHistory);
12576 }else if( (zHome = find_home_dir(0))!=0 ){
12577 nHistory = strlen30(zHome) + 20;
12578 if( (zHistory = malloc(nHistory))!=0 ){
12579 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
12582 if( zHistory ){ shell_read_history(zHistory); }
12583 #if HAVE_READLINE || HAVE_EDITLINE
12584 rl_attempted_completion_function = readline_completion;
12585 #elif HAVE_LINENOISE
12586 linenoiseSetCompletionCallback(linenoise_completion);
12587 #endif
12588 data.in = 0;
12589 rc = process_input(&data);
12590 if( zHistory ){
12591 shell_stifle_history(2000);
12592 shell_write_history(zHistory);
12593 free(zHistory);
12595 }else{
12596 data.in = stdin;
12597 rc = process_input(&data);
12600 #ifndef SQLITE_SHELL_FIDDLE
12601 /* In WASM mode we have to leave the db state in place so that
12602 ** client code can "push" SQL into it after this call returns. */
12603 free(azCmd);
12604 set_table_name(&data, 0);
12605 if( data.db ){
12606 session_close_all(&data, -1);
12607 close_db(data.db);
12609 for(i=0; i<ArraySize(data.aAuxDb); i++){
12610 sqlite3_free(data.aAuxDb[i].zFreeOnClose);
12611 if( data.aAuxDb[i].db ){
12612 session_close_all(&data, i);
12613 close_db(data.aAuxDb[i].db);
12616 find_home_dir(1);
12617 output_reset(&data);
12618 data.doXdgOpen = 0;
12619 clearTempFile(&data);
12620 #if !SQLITE_SHELL_IS_UTF8
12621 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
12622 free(argvToFree);
12623 #endif
12624 free(data.colWidth);
12625 free(data.zNonce);
12626 /* Clear the global data structure so that valgrind will detect memory
12627 ** leaks */
12628 memset(&data, 0, sizeof(data));
12629 #ifdef SQLITE_DEBUG
12630 if( sqlite3_memory_used()>mem_main_enter ){
12631 utf8_printf(stderr, "Memory leaked: %u bytes\n",
12632 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
12634 #endif
12635 #endif /* !SQLITE_SHELL_FIDDLE */
12636 return rc;
12640 #ifdef SQLITE_SHELL_FIDDLE
12641 /* Only for emcc experimentation purposes. */
12642 int fiddle_experiment(int a,int b){
12643 return a + b;
12647 ** Returns a pointer to the current DB handle.
12649 sqlite3 * fiddle_db_handle(){
12650 return globalDb;
12654 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12655 ** "main" is assumed. Returns 0 if no db with the given name is
12656 ** open.
12658 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
12659 sqlite3_vfs * pVfs = 0;
12660 if(globalDb){
12661 sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
12662 SQLITE_FCNTL_VFS_POINTER, &pVfs);
12664 return pVfs;
12667 /* Only for emcc experimentation purposes. */
12668 sqlite3 * fiddle_db_arg(sqlite3 *arg){
12669 printf("fiddle_db_arg(%p)\n", (const void*)arg);
12670 return arg;
12674 ** Intended to be called via a SharedWorker() while a separate
12675 ** SharedWorker() (which manages the wasm module) is performing work
12676 ** which should be interrupted. Unfortunately, SharedWorker is not
12677 ** portable enough to make real use of.
12679 void fiddle_interrupt(void){
12680 if( globalDb ) sqlite3_interrupt(globalDb);
12684 ** Returns the filename of the given db name, assuming "main" if
12685 ** zDbName is NULL. Returns NULL if globalDb is not opened.
12687 const char * fiddle_db_filename(const char * zDbName){
12688 return globalDb
12689 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
12690 : NULL;
12694 ** Completely wipes out the contents of the currently-opened database
12695 ** but leaves its storage intact for reuse.
12697 void fiddle_reset_db(void){
12698 if( globalDb ){
12699 int rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
12700 if( 0==rc ) rc = sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
12701 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
12706 ** Uses the current database's VFS xRead to stream the db file's
12707 ** contents out to the given callback. The callback gets a single
12708 ** chunk of size n (its 2nd argument) on each call and must return 0
12709 ** on success, non-0 on error. This function returns 0 on success,
12710 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12711 ** code from the callback. Note that this is not thread-friendly: it
12712 ** expects that it will be the only thread reading the db file and
12713 ** takes no measures to ensure that is the case.
12715 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
12716 sqlite3_int64 nSize = 0;
12717 sqlite3_int64 nPos = 0;
12718 sqlite3_file * pFile = 0;
12719 unsigned char buf[1024 * 8];
12720 int nBuf = (int)sizeof(buf);
12721 int rc = shellState.db
12722 ? sqlite3_file_control(shellState.db, "main",
12723 SQLITE_FCNTL_FILE_POINTER, &pFile)
12724 : SQLITE_NOTFOUND;
12725 if( rc ) return rc;
12726 rc = pFile->pMethods->xFileSize(pFile, &nSize);
12727 if( rc ) return rc;
12728 if(nSize % nBuf){
12729 /* DB size is not an even multiple of the buffer size. Reduce
12730 ** buffer size so that we do not unduly inflate the db size when
12731 ** exporting. */
12732 if(0 == nSize % 4096) nBuf = 4096;
12733 else if(0 == nSize % 2048) nBuf = 2048;
12734 else if(0 == nSize % 1024) nBuf = 1024;
12735 else nBuf = 512;
12737 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
12738 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
12739 if(SQLITE_IOERR_SHORT_READ == rc){
12740 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
12742 if( 0==rc ) rc = xCallback(buf, nBuf);
12744 return rc;
12748 ** Trivial exportable function for emscripten. It processes zSql as if
12749 ** it were input to the sqlite3 shell and redirects all output to the
12750 ** wasm binding. fiddle_main() must have been called before this
12751 ** is called, or results are undefined.
12753 void fiddle_exec(const char * zSql){
12754 if(zSql && *zSql){
12755 if('.'==*zSql) puts(zSql);
12756 shellState.wasm.zInput = zSql;
12757 shellState.wasm.zPos = zSql;
12758 process_input(&shellState);
12759 shellState.wasm.zInput = shellState.wasm.zPos = 0;
12762 #endif /* SQLITE_SHELL_FIDDLE */