In the CLI, when displaying results in a columnar format, take into account
[sqlite.git] / src / shell.c.in
blob8185e4c4ce0d377f482d9a9e22d383e32df2064e
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 #undef WIN32_LEAN_AND_MEAN
232 #define WIN32_LEAN_AND_MEAN
233 #include <windows.h>
235 /* string conversion routines only needed on Win32 */
236 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
237 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
238 #endif
240 /* Use console I/O package as a direct INCLUDE. */
241 #define SQLITE_INTERNAL_LINKAGE static
243 #ifdef SQLITE_SHELL_FIDDLE
244 /* Deselect most features from the console I/O package for Fiddle. */
245 # define SQLITE_CIO_NO_REDIRECT
246 # define SQLITE_CIO_NO_CLASSIFY
247 # define SQLITE_CIO_NO_TRANSLATE
248 # define SQLITE_CIO_NO_SETMODE
249 # define SQLITE_CIO_NO_FLUSH
250 #endif
251 INCLUDE ../ext/consio/console_io.h
252 INCLUDE ../ext/consio/console_io.c
254 #ifndef SQLITE_SHELL_FIDDLE
256 /* From here onward, fgets() is redirected to the console_io library. */
257 # define fgets(b,n,f) fGetsUtf8(b,n,f)
259 * Define macros for emitting output text in various ways:
260 * sputz(s, z) => emit 0-terminated string z to given stream s
261 * sputf(s, f, ...) => emit varargs per format f to given stream s
262 * oputz(z) => emit 0-terminated string z to default stream
263 * oputf(f, ...) => emit varargs per format f to default stream
264 * eputz(z) => emit 0-terminated string z to error stream
265 * eputf(f, ...) => emit varargs per format f to error stream
266 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
268 * Note that the default stream is whatever has been last set via:
269 * setOutputStream(FILE *pf)
270 * This is normally the stream that CLI normal output goes to.
271 * For the stand-alone CLI, it is stdout with no .output redirect.
273 * The ?putz(z) forms are required for the Fiddle builds for string literal
274 * output, in aid of enforcing format string to argument correspondence.
276 # define sputz(s,z) fPutsUtf8(z,s)
277 # define sputf fPrintfUtf8
278 # define oputz(z) oPutsUtf8(z)
279 # define oputf oPrintfUtf8
280 # define eputz(z) ePutsUtf8(z)
281 # define eputf ePrintfUtf8
282 # define oputb(buf,na) oPutbUtf8(buf,na)
283 # define fflush(s) fFlushBuffer(s);
285 #else
286 /* For Fiddle, all console handling and emit redirection is omitted. */
287 /* These next 3 macros are for emitting formatted output. When complaints
288 * from the WASM build are issued for non-formatted output, when a mere
289 * string literal is to be emitted, the ?putz(z) forms should be used.
290 * (This permits compile-time checking of format string / argument mismatch.)
292 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
293 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
294 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
295 /* These next 3 macros are for emitting simple string literals. */
296 # define oputz(z) fputs(z,stdout)
297 # define eputz(z) fputs(z,stderr)
298 # define sputz(fp,z) fputs(z,fp)
299 # define oputb(buf,na) fwrite(buf,1,na,stdout)
300 # undef fflush
301 #endif
303 /* True if the timer is enabled */
304 static int enableTimer = 0;
306 /* A version of strcmp() that works with NULL values */
307 static int cli_strcmp(const char *a, const char *b){
308 if( a==0 ) a = "";
309 if( b==0 ) b = "";
310 return strcmp(a,b);
312 static int cli_strncmp(const char *a, const char *b, size_t n){
313 if( a==0 ) a = "";
314 if( b==0 ) b = "";
315 return strncmp(a,b,n);
318 /* Return the current wall-clock time */
319 static sqlite3_int64 timeOfDay(void){
320 static sqlite3_vfs *clockVfs = 0;
321 sqlite3_int64 t;
322 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
323 if( clockVfs==0 ) return 0; /* Never actually happens */
324 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
325 clockVfs->xCurrentTimeInt64(clockVfs, &t);
326 }else{
327 double r;
328 clockVfs->xCurrentTime(clockVfs, &r);
329 t = (sqlite3_int64)(r*86400000.0);
331 return t;
334 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
335 #include <sys/time.h>
336 #include <sys/resource.h>
338 /* VxWorks does not support getrusage() as far as we can determine */
339 #if defined(_WRS_KERNEL) || defined(__RTP__)
340 struct rusage {
341 struct timeval ru_utime; /* user CPU time used */
342 struct timeval ru_stime; /* system CPU time used */
344 #define getrusage(A,B) memset(B,0,sizeof(*B))
345 #endif
347 /* Saved resource information for the beginning of an operation */
348 static struct rusage sBegin; /* CPU time at start */
349 static sqlite3_int64 iBegin; /* Wall-clock time at start */
352 ** Begin timing an operation
354 static void beginTimer(void){
355 if( enableTimer ){
356 getrusage(RUSAGE_SELF, &sBegin);
357 iBegin = timeOfDay();
361 /* Return the difference of two time_structs in seconds */
362 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
363 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
364 (double)(pEnd->tv_sec - pStart->tv_sec);
368 ** Print the timing results.
370 static void endTimer(void){
371 if( enableTimer ){
372 sqlite3_int64 iEnd = timeOfDay();
373 struct rusage sEnd;
374 getrusage(RUSAGE_SELF, &sEnd);
375 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
376 (iEnd - iBegin)*0.001,
377 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
378 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
382 #define BEGIN_TIMER beginTimer()
383 #define END_TIMER endTimer()
384 #define HAS_TIMER 1
386 #elif (defined(_WIN32) || defined(WIN32))
388 /* Saved resource information for the beginning of an operation */
389 static HANDLE hProcess;
390 static FILETIME ftKernelBegin;
391 static FILETIME ftUserBegin;
392 static sqlite3_int64 ftWallBegin;
393 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
394 LPFILETIME, LPFILETIME);
395 static GETPROCTIMES getProcessTimesAddr = NULL;
398 ** Check to see if we have timer support. Return 1 if necessary
399 ** support found (or found previously).
401 static int hasTimer(void){
402 if( getProcessTimesAddr ){
403 return 1;
404 } else {
405 #if !SQLITE_OS_WINRT
406 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
407 ** versions. See if the version we are running on has it, and if it
408 ** does, save off a pointer to it and the current process handle.
410 hProcess = GetCurrentProcess();
411 if( hProcess ){
412 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
413 if( NULL != hinstLib ){
414 getProcessTimesAddr =
415 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
416 if( NULL != getProcessTimesAddr ){
417 return 1;
419 FreeLibrary(hinstLib);
422 #endif
424 return 0;
428 ** Begin timing an operation
430 static void beginTimer(void){
431 if( enableTimer && getProcessTimesAddr ){
432 FILETIME ftCreation, ftExit;
433 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
434 &ftKernelBegin,&ftUserBegin);
435 ftWallBegin = timeOfDay();
439 /* Return the difference of two FILETIME structs in seconds */
440 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
441 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
442 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
443 return (double) ((i64End - i64Start) / 10000000.0);
447 ** Print the timing results.
449 static void endTimer(void){
450 if( enableTimer && getProcessTimesAddr){
451 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
452 sqlite3_int64 ftWallEnd = timeOfDay();
453 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
454 sputf(stdout, "Run Time: real %.3f user %f sys %f\n",
455 (ftWallEnd - ftWallBegin)*0.001,
456 timeDiff(&ftUserBegin, &ftUserEnd),
457 timeDiff(&ftKernelBegin, &ftKernelEnd));
461 #define BEGIN_TIMER beginTimer()
462 #define END_TIMER endTimer()
463 #define HAS_TIMER hasTimer()
465 #else
466 #define BEGIN_TIMER
467 #define END_TIMER
468 #define HAS_TIMER 0
469 #endif
472 ** Used to prevent warnings about unused parameters
474 #define UNUSED_PARAMETER(x) (void)(x)
477 ** Number of elements in an array
479 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
482 ** If the following flag is set, then command execution stops
483 ** at an error if we are not interactive.
485 static int bail_on_error = 0;
488 ** Treat stdin as an interactive input if the following variable
489 ** is true. Otherwise, assume stdin is connected to a file or pipe.
491 static int stdin_is_interactive = 1;
494 ** On Windows systems we need to know if standard output is a console
495 ** in order to show that UTF-16 translation is done in the sign-on
496 ** banner. The following variable is true if it is the console.
498 static int stdout_is_console = 1;
501 ** The following is the open SQLite database. We make a pointer
502 ** to this database a static variable so that it can be accessed
503 ** by the SIGINT handler to interrupt database processing.
505 static sqlite3 *globalDb = 0;
508 ** True if an interrupt (Control-C) has been received.
510 static volatile int seenInterrupt = 0;
513 ** This is the name of our program. It is set in main(), used
514 ** in a number of other places, mostly for error messages.
516 static char *Argv0;
519 ** Prompt strings. Initialized in main. Settable with
520 ** .prompt main continue
522 #define PROMPT_LEN_MAX 20
523 /* First line prompt. default: "sqlite> " */
524 static char mainPrompt[PROMPT_LEN_MAX];
525 /* Continuation prompt. default: " ...> " */
526 static char continuePrompt[PROMPT_LEN_MAX];
528 /* This is variant of the standard-library strncpy() routine with the
529 ** one change that the destination string is always zero-terminated, even
530 ** if there is no zero-terminator in the first n-1 characters of the source
531 ** string.
533 static char *shell_strncpy(char *dest, const char *src, size_t n){
534 size_t i;
535 for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
536 dest[i] = 0;
537 return dest;
541 ** strcpy() workalike to squelch an unwarranted link-time warning
542 ** from OpenBSD.
544 static void shell_strcpy(char *dest, const char *src){
545 while( (*(dest++) = *(src++))!=0 ){}
549 ** Optionally disable dynamic continuation prompt.
550 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
551 ** or open parentheses level if non-zero, or continuation prompt as set.
552 ** This facility interacts with the scanner and process_input() where the
553 ** below 5 macros are used.
555 #ifdef SQLITE_OMIT_DYNAPROMPT
556 # define CONTINUATION_PROMPT continuePrompt
557 # define CONTINUE_PROMPT_RESET
558 # define CONTINUE_PROMPT_AWAITS(p,s)
559 # define CONTINUE_PROMPT_AWAITC(p,c)
560 # define CONTINUE_PAREN_INCR(p,n)
561 # define CONTINUE_PROMPT_PSTATE 0
562 typedef void *t_NoDynaPrompt;
563 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
564 #else
565 # define CONTINUATION_PROMPT dynamicContinuePrompt()
566 # define CONTINUE_PROMPT_RESET \
567 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
568 # define CONTINUE_PROMPT_AWAITS(p,s) \
569 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
570 # define CONTINUE_PROMPT_AWAITC(p,c) \
571 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
572 # define CONTINUE_PAREN_INCR(p,n) \
573 if(p && stdin_is_interactive) (trackParenLevel(p,n))
574 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
575 typedef struct DynaPrompt *t_DynaPromptRef;
576 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
578 static struct DynaPrompt {
579 char dynamicPrompt[PROMPT_LEN_MAX];
580 char acAwait[2];
581 int inParenLevel;
582 char *zScannerAwaits;
583 } dynPrompt = { {0}, {0}, 0, 0 };
585 /* Record parenthesis nesting level change, or force level to 0. */
586 static void trackParenLevel(struct DynaPrompt *p, int ni){
587 p->inParenLevel += ni;
588 if( ni==0 ) p->inParenLevel = 0;
589 p->zScannerAwaits = 0;
592 /* Record that a lexeme is opened, or closed with args==0. */
593 static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
594 if( s!=0 || c==0 ){
595 p->zScannerAwaits = s;
596 p->acAwait[0] = 0;
597 }else{
598 p->acAwait[0] = c;
599 p->zScannerAwaits = p->acAwait;
603 /* Upon demand, derive the continuation prompt to display. */
604 static char *dynamicContinuePrompt(void){
605 if( continuePrompt[0]==0
606 || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
607 return continuePrompt;
608 }else{
609 if( dynPrompt.zScannerAwaits ){
610 size_t ncp = strlen(continuePrompt);
611 size_t ndp = strlen(dynPrompt.zScannerAwaits);
612 if( ndp > ncp-3 ) return continuePrompt;
613 shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
614 while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
615 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
616 PROMPT_LEN_MAX-4);
617 }else{
618 if( dynPrompt.inParenLevel>9 ){
619 shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
620 }else if( dynPrompt.inParenLevel<0 ){
621 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
622 }else{
623 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
624 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
626 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
627 PROMPT_LEN_MAX-4);
630 return dynPrompt.dynamicPrompt;
632 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
634 /* Indicate out-of-memory and exit. */
635 static void shell_out_of_memory(void){
636 eputz("Error: out of memory\n");
637 exit(1);
640 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
641 ** out-of-memory error.
643 static void shell_check_oom(const void *p){
644 if( p==0 ) shell_out_of_memory();
648 ** Write I/O traces to the following stream.
650 #ifdef SQLITE_ENABLE_IOTRACE
651 static FILE *iotrace = 0;
652 #endif
655 ** This routine works like printf in that its first argument is a
656 ** format string and subsequent arguments are values to be substituted
657 ** in place of % fields. The result of formatting this string
658 ** is written to iotrace.
660 #ifdef SQLITE_ENABLE_IOTRACE
661 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
662 va_list ap;
663 char *z;
664 if( iotrace==0 ) return;
665 va_start(ap, zFormat);
666 z = sqlite3_vmprintf(zFormat, ap);
667 va_end(ap);
668 sputf(iotrace, "%s", z);
669 sqlite3_free(z);
671 #endif
673 /* Lookup table to determine the number of columns consumed by a Unicode
674 ** character.
676 static const struct {
677 unsigned char w; /* Width of the character in columns */
678 int iFirst; /* First character in a span having this width */
679 } aUWidth[] = {
680 /* {0, 0x00000}, {1, 0x00020}, {0, 0x0007f}, {1, 0x000a0}, */
681 {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
682 {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
683 {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
684 {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
685 {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
686 {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
687 {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
688 {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
689 {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
690 {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
691 {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
692 {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
693 {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
694 {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
695 {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
696 {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
697 {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
698 {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
699 {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
700 {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
701 {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
702 {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
703 {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
704 {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
705 {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
706 {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
707 {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
708 {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
709 {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
710 {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
711 {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
712 {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
713 {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
714 {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
715 {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
716 {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
717 {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
718 {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
719 {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
720 {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
721 {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
722 {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
723 {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
724 {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
725 {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
726 {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
727 {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
728 {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
729 {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
730 {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
731 {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
732 {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
733 {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
734 {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
735 {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
736 {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
737 {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
738 {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
739 {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
740 {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
741 {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
745 ** Return the width, in columns, of the single Unicode character c.
746 ** For normal characters, the answer is always 1. But it might be 0 or 2
747 ** for zero-width and double-width characters.
749 int cli_wcwidth(int c){
750 int iFirst, iLast;
752 /* Fast path for common characters */
753 if( c<0x20 ) return 0;
754 if( c<0x7f ) return 1;
755 if( c<0xa0 ) return 0;
756 if( c<=0x300 ) return 1;
758 /* The general case */
759 iFirst = 0;
760 iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1;
761 while( iFirst<iLast-1 ){
762 int iMid = (iFirst+iLast)/2;
763 int cMid = aUWidth[iMid].iFirst;
764 if( cMid < c ){
765 iFirst = iMid;
766 }else if( cMid > c ){
767 iLast = iMid - 1;
768 }else{
769 return aUWidth[iMid].w;
772 if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w;
773 return aUWidth[iLast].w;
777 ** Compute the value and length of a multi-byte UTF-8 character that
778 ** begins at z[0]. Return the length. Write the Unicode value into *pU.
780 static int decodeUtf8(const unsigned char *z, int *pU){
781 if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
782 *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
783 return 2;
785 if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
786 *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
787 return 3;
789 if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
790 && (z[3] & 0xc0)==0x80
792 *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
793 | (z[4] & 0x3f);
794 return 4;
796 *pU = 0;
797 return 1;
801 #if 0 /* NOT USED */
803 ** Return the width, in display columns, of a UTF-8 string.
805 ** Each normal character counts as 1. Zero-width characters count
806 ** as zero, and double-width characters count as 2.
808 int cli_wcswidth(const char *z){
809 const unsigned char *a = (const unsigned char*)z;
810 int n = 0;
811 int i = 0;
812 unsigned char c;
813 while( (c = a[i])!=0 ){
814 if( c>=0xc0 ){
815 int u;
816 int len = decodeUtf8(&a[i], &u);
817 i += len;
818 n += cli_wcwidth(u);
819 }else if( c>=' ' ){
820 n++;
821 i++;
822 }else{
823 i++;
826 return n;
828 #endif
831 ** Output string zUtf to stdout as w characters. If w is negative,
832 ** then right-justify the text. W is the width in UTF-8 characters, not
833 ** in bytes. This is different from the %*.*s specification in printf
834 ** since with %*.*s the width is measured in bytes, not characters.
836 ** Take into account zero-width and double-width Unicode characters.
837 ** In other words, a zero-width character does not count toward the
838 ** the w limit. A double-width character counts as two.
840 static void utf8_width_print(int w, const char *zUtf){
841 const unsigned char *a = (const unsigned char*)zUtf;
842 unsigned char c;
843 int i = 0;
844 int n = 0;
845 int aw = w<0 ? -w : w;
846 if( zUtf==0 ) zUtf = "";
847 while( (c = a[i])!=0 ){
848 if( (c&0xc0)==0xc0 ){
849 int u;
850 int len = decodeUtf8(a+i, &u);
851 int x = cli_wcwidth(u);
852 if( x+n>aw ){
853 break;
855 i += len;
856 n += x;
857 }else if( n>=aw ){
858 break;
859 }else{
860 n++;
861 i++;
864 if( n>=aw ){
865 oputf("%.*s", i, zUtf);
866 }else if( w<0 ){
867 oputf("%*s%s", aw-n, "", zUtf);
868 }else{
869 oputf("%s%*s", zUtf, aw-n, "");
875 ** Determines if a string is a number of not.
877 static int isNumber(const char *z, int *realnum){
878 if( *z=='-' || *z=='+' ) z++;
879 if( !IsDigit(*z) ){
880 return 0;
882 z++;
883 if( realnum ) *realnum = 0;
884 while( IsDigit(*z) ){ z++; }
885 if( *z=='.' ){
886 z++;
887 if( !IsDigit(*z) ) return 0;
888 while( IsDigit(*z) ){ z++; }
889 if( realnum ) *realnum = 1;
891 if( *z=='e' || *z=='E' ){
892 z++;
893 if( *z=='+' || *z=='-' ) z++;
894 if( !IsDigit(*z) ) return 0;
895 while( IsDigit(*z) ){ z++; }
896 if( realnum ) *realnum = 1;
898 return *z==0;
902 ** Compute a string length that is limited to what can be stored in
903 ** lower 30 bits of a 32-bit signed integer.
905 static int strlen30(const char *z){
906 const char *z2 = z;
907 while( *z2 ){ z2++; }
908 return 0x3fffffff & (int)(z2 - z);
912 ** Return the length of a string in characters. Multibyte UTF8 characters
913 ** count as a single character.
915 static int strlenChar(const char *z){
916 int n = 0;
917 while( *z ){
918 if( (0xc0&*(z++))!=0x80 ) n++;
920 return n;
924 ** Return open FILE * if zFile exists, can be opened for read
925 ** and is an ordinary file or a character stream source.
926 ** Otherwise return 0.
928 static FILE * openChrSource(const char *zFile){
929 #if defined(_WIN32) || defined(WIN32)
930 struct __stat64 x = {0};
931 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
932 /* On Windows, open first, then check the stream nature. This order
933 ** is necessary because _stat() and sibs, when checking a named pipe,
934 ** effectively break the pipe as its supplier sees it. */
935 FILE *rv = fopen(zFile, "rb");
936 if( rv==0 ) return 0;
937 if( _fstat64(_fileno(rv), &x) != 0
938 || !STAT_CHR_SRC(x.st_mode)){
939 fclose(rv);
940 rv = 0;
942 return rv;
943 #else
944 struct stat x = {0};
945 int rc = stat(zFile, &x);
946 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
947 if( rc!=0 ) return 0;
948 if( STAT_CHR_SRC(x.st_mode) ){
949 return fopen(zFile, "rb");
950 }else{
951 return 0;
953 #endif
954 #undef STAT_CHR_SRC
958 ** This routine reads a line of text from FILE in, stores
959 ** the text in memory obtained from malloc() and returns a pointer
960 ** to the text. NULL is returned at end of file, or if malloc()
961 ** fails.
963 ** If zLine is not NULL then it is a malloced buffer returned from
964 ** a previous call to this routine that may be reused.
966 static char *local_getline(char *zLine, FILE *in){
967 int nLine = zLine==0 ? 0 : 100;
968 int n = 0;
970 while( 1 ){
971 if( n+100>nLine ){
972 nLine = nLine*2 + 100;
973 zLine = realloc(zLine, nLine);
974 shell_check_oom(zLine);
976 if( fgets(&zLine[n], nLine - n, in)==0 ){
977 if( n==0 ){
978 free(zLine);
979 return 0;
981 zLine[n] = 0;
982 break;
984 while( zLine[n] ) n++;
985 if( n>0 && zLine[n-1]=='\n' ){
986 n--;
987 if( n>0 && zLine[n-1]=='\r' ) n--;
988 zLine[n] = 0;
989 break;
992 return zLine;
996 ** Retrieve a single line of input text.
998 ** If in==0 then read from standard input and prompt before each line.
999 ** If isContinuation is true, then a continuation prompt is appropriate.
1000 ** If isContinuation is zero, then the main prompt should be used.
1002 ** If zPrior is not NULL then it is a buffer from a prior call to this
1003 ** routine that can be reused.
1005 ** The result is stored in space obtained from malloc() and must either
1006 ** be freed by the caller or else passed back into this routine via the
1007 ** zPrior argument for reuse.
1009 #ifndef SQLITE_SHELL_FIDDLE
1010 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
1011 char *zPrompt;
1012 char *zResult;
1013 if( in!=0 ){
1014 zResult = local_getline(zPrior, in);
1015 }else{
1016 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
1017 #if SHELL_USE_LOCAL_GETLINE
1018 sputz(stdout, zPrompt);
1019 fflush(stdout);
1021 zResult = local_getline(zPrior, stdin);
1022 zPrior = 0;
1023 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1024 if( zResult==0 ) sqlite3_sleep(50);
1025 }while( zResult==0 && seenInterrupt>0 );
1026 #else
1027 free(zPrior);
1028 zResult = shell_readline(zPrompt);
1029 while( zResult==0 ){
1030 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1031 sqlite3_sleep(50);
1032 if( seenInterrupt==0 ) break;
1033 zResult = shell_readline("");
1035 if( zResult && *zResult ) shell_add_history(zResult);
1036 #endif
1038 return zResult;
1040 #endif /* !SQLITE_SHELL_FIDDLE */
1043 ** Return the value of a hexadecimal digit. Return -1 if the input
1044 ** is not a hex digit.
1046 static int hexDigitValue(char c){
1047 if( c>='0' && c<='9' ) return c - '0';
1048 if( c>='a' && c<='f' ) return c - 'a' + 10;
1049 if( c>='A' && c<='F' ) return c - 'A' + 10;
1050 return -1;
1054 ** Interpret zArg as an integer value, possibly with suffixes.
1056 static sqlite3_int64 integerValue(const char *zArg){
1057 sqlite3_int64 v = 0;
1058 static const struct { char *zSuffix; int iMult; } aMult[] = {
1059 { "KiB", 1024 },
1060 { "MiB", 1024*1024 },
1061 { "GiB", 1024*1024*1024 },
1062 { "KB", 1000 },
1063 { "MB", 1000000 },
1064 { "GB", 1000000000 },
1065 { "K", 1000 },
1066 { "M", 1000000 },
1067 { "G", 1000000000 },
1069 int i;
1070 int isNeg = 0;
1071 if( zArg[0]=='-' ){
1072 isNeg = 1;
1073 zArg++;
1074 }else if( zArg[0]=='+' ){
1075 zArg++;
1077 if( zArg[0]=='0' && zArg[1]=='x' ){
1078 int x;
1079 zArg += 2;
1080 while( (x = hexDigitValue(zArg[0]))>=0 ){
1081 v = (v<<4) + x;
1082 zArg++;
1084 }else{
1085 while( IsDigit(zArg[0]) ){
1086 v = v*10 + zArg[0] - '0';
1087 zArg++;
1090 for(i=0; i<ArraySize(aMult); i++){
1091 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1092 v *= aMult[i].iMult;
1093 break;
1096 return isNeg? -v : v;
1100 ** A variable length string to which one can append text.
1102 typedef struct ShellText ShellText;
1103 struct ShellText {
1104 char *z;
1105 int n;
1106 int nAlloc;
1110 ** Initialize and destroy a ShellText object
1112 static void initText(ShellText *p){
1113 memset(p, 0, sizeof(*p));
1115 static void freeText(ShellText *p){
1116 free(p->z);
1117 initText(p);
1120 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1121 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1122 ** added to zIn, and the result returned in memory obtained from malloc().
1123 ** zIn, if it was not NULL, is freed.
1125 ** If the third argument, quote, is not '\0', then it is used as a
1126 ** quote character for zAppend.
1128 static void appendText(ShellText *p, const char *zAppend, char quote){
1129 i64 len;
1130 i64 i;
1131 i64 nAppend = strlen30(zAppend);
1133 len = nAppend+p->n+1;
1134 if( quote ){
1135 len += 2;
1136 for(i=0; i<nAppend; i++){
1137 if( zAppend[i]==quote ) len++;
1141 if( p->z==0 || p->n+len>=p->nAlloc ){
1142 p->nAlloc = p->nAlloc*2 + len + 20;
1143 p->z = realloc(p->z, p->nAlloc);
1144 shell_check_oom(p->z);
1147 if( quote ){
1148 char *zCsr = p->z+p->n;
1149 *zCsr++ = quote;
1150 for(i=0; i<nAppend; i++){
1151 *zCsr++ = zAppend[i];
1152 if( zAppend[i]==quote ) *zCsr++ = quote;
1154 *zCsr++ = quote;
1155 p->n = (int)(zCsr - p->z);
1156 *zCsr = '\0';
1157 }else{
1158 memcpy(p->z+p->n, zAppend, nAppend);
1159 p->n += nAppend;
1160 p->z[p->n] = '\0';
1165 ** Attempt to determine if identifier zName needs to be quoted, either
1166 ** because it contains non-alphanumeric characters, or because it is an
1167 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1168 ** that quoting is required.
1170 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1172 static char quoteChar(const char *zName){
1173 int i;
1174 if( zName==0 ) return '"';
1175 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
1176 for(i=0; zName[i]; i++){
1177 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
1179 return sqlite3_keyword_check(zName, i) ? '"' : 0;
1183 ** Construct a fake object name and column list to describe the structure
1184 ** of the view, virtual table, or table valued function zSchema.zName.
1186 static char *shellFakeSchema(
1187 sqlite3 *db, /* The database connection containing the vtab */
1188 const char *zSchema, /* Schema of the database holding the vtab */
1189 const char *zName /* The name of the virtual table */
1191 sqlite3_stmt *pStmt = 0;
1192 char *zSql;
1193 ShellText s;
1194 char cQuote;
1195 char *zDiv = "(";
1196 int nRow = 0;
1198 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1199 zSchema ? zSchema : "main", zName);
1200 shell_check_oom(zSql);
1201 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1202 sqlite3_free(zSql);
1203 initText(&s);
1204 if( zSchema ){
1205 cQuote = quoteChar(zSchema);
1206 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
1207 appendText(&s, zSchema, cQuote);
1208 appendText(&s, ".", 0);
1210 cQuote = quoteChar(zName);
1211 appendText(&s, zName, cQuote);
1212 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1213 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
1214 nRow++;
1215 appendText(&s, zDiv, 0);
1216 zDiv = ",";
1217 if( zCol==0 ) zCol = "";
1218 cQuote = quoteChar(zCol);
1219 appendText(&s, zCol, cQuote);
1221 appendText(&s, ")", 0);
1222 sqlite3_finalize(pStmt);
1223 if( nRow==0 ){
1224 freeText(&s);
1225 s.z = 0;
1227 return s.z;
1231 ** SQL function: strtod(X)
1233 ** Use the C-library strtod() function to convert string X into a double.
1234 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1235 ** routines against the C-library.
1237 static void shellStrtod(
1238 sqlite3_context *pCtx,
1239 int nVal,
1240 sqlite3_value **apVal
1242 char *z = (char*)sqlite3_value_text(apVal[0]);
1243 UNUSED_PARAMETER(nVal);
1244 if( z==0 ) return;
1245 sqlite3_result_double(pCtx, strtod(z,0));
1249 ** SQL function: dtostr(X)
1251 ** Use the C-library printf() function to convert real value X into a string.
1252 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1253 ** routines against the C-library.
1255 static void shellDtostr(
1256 sqlite3_context *pCtx,
1257 int nVal,
1258 sqlite3_value **apVal
1260 double r = sqlite3_value_double(apVal[0]);
1261 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1262 char z[400];
1263 if( n<1 ) n = 1;
1264 if( n>350 ) n = 350;
1265 sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
1266 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1271 ** SQL function: shell_module_schema(X)
1273 ** Return a fake schema for the table-valued function or eponymous virtual
1274 ** table X.
1276 static void shellModuleSchema(
1277 sqlite3_context *pCtx,
1278 int nVal,
1279 sqlite3_value **apVal
1281 const char *zName;
1282 char *zFake;
1283 UNUSED_PARAMETER(nVal);
1284 zName = (const char*)sqlite3_value_text(apVal[0]);
1285 zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
1286 if( zFake ){
1287 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
1288 -1, sqlite3_free);
1289 free(zFake);
1294 ** SQL function: shell_add_schema(S,X)
1296 ** Add the schema name X to the CREATE statement in S and return the result.
1297 ** Examples:
1299 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1301 ** Also works on
1303 ** CREATE INDEX
1304 ** CREATE UNIQUE INDEX
1305 ** CREATE VIEW
1306 ** CREATE TRIGGER
1307 ** CREATE VIRTUAL TABLE
1309 ** This UDF is used by the .schema command to insert the schema name of
1310 ** attached databases into the middle of the sqlite_schema.sql field.
1312 static void shellAddSchemaName(
1313 sqlite3_context *pCtx,
1314 int nVal,
1315 sqlite3_value **apVal
1317 static const char *aPrefix[] = {
1318 "TABLE",
1319 "INDEX",
1320 "UNIQUE INDEX",
1321 "VIEW",
1322 "TRIGGER",
1323 "VIRTUAL TABLE"
1325 int i = 0;
1326 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1327 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1328 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1329 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1330 UNUSED_PARAMETER(nVal);
1331 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
1332 for(i=0; i<ArraySize(aPrefix); i++){
1333 int n = strlen30(aPrefix[i]);
1334 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1335 char *z = 0;
1336 char *zFake = 0;
1337 if( zSchema ){
1338 char cQuote = quoteChar(zSchema);
1339 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1340 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1341 }else{
1342 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1345 if( zName
1346 && aPrefix[i][0]=='V'
1347 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1349 if( z==0 ){
1350 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1351 }else{
1352 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1354 free(zFake);
1356 if( z ){
1357 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1358 return;
1363 sqlite3_result_value(pCtx, apVal[0]);
1367 ** The source code for several run-time loadable extensions is inserted
1368 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1369 ** code, we need to override some macros to make the included program code
1370 ** work here in the middle of this regular program.
1372 #define SQLITE_EXTENSION_INIT1
1373 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1375 #if defined(_WIN32) && defined(_MSC_VER)
1376 INCLUDE test_windirent.h
1377 INCLUDE test_windirent.c
1378 #define dirent DIRENT
1379 #endif
1380 INCLUDE ../ext/misc/memtrace.c
1381 INCLUDE ../ext/misc/pcachetrace.c
1382 INCLUDE ../ext/misc/shathree.c
1383 INCLUDE ../ext/misc/sha1.c
1384 INCLUDE ../ext/misc/uint.c
1385 INCLUDE ../ext/misc/decimal.c
1386 INCLUDE ../ext/misc/percentile.c
1387 #undef sqlite3_base_init
1388 #define sqlite3_base_init sqlite3_base64_init
1389 INCLUDE ../ext/misc/base64.c
1390 #undef sqlite3_base_init
1391 #define sqlite3_base_init sqlite3_base85_init
1392 #define OMIT_BASE85_CHECKER
1393 INCLUDE ../ext/misc/base85.c
1394 INCLUDE ../ext/misc/ieee754.c
1395 INCLUDE ../ext/misc/series.c
1396 INCLUDE ../ext/misc/regexp.c
1397 #ifndef SQLITE_SHELL_FIDDLE
1398 INCLUDE ../ext/misc/fileio.c
1399 INCLUDE ../ext/misc/completion.c
1400 INCLUDE ../ext/misc/appendvfs.c
1401 #endif
1402 #ifdef SQLITE_HAVE_ZLIB
1403 INCLUDE ../ext/misc/zipfile.c
1404 INCLUDE ../ext/misc/sqlar.c
1405 #endif
1406 INCLUDE ../ext/expert/sqlite3expert.h
1407 INCLUDE ../ext/expert/sqlite3expert.c
1408 INCLUDE ../ext/intck/sqlite3intck.h
1409 INCLUDE ../ext/intck/sqlite3intck.c
1410 INCLUDE ../ext/misc/stmtrand.c
1411 INCLUDE ../ext/misc/vfstrace.c
1413 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1414 #define SQLITE_SHELL_HAVE_RECOVER 1
1415 #else
1416 #define SQLITE_SHELL_HAVE_RECOVER 0
1417 #endif
1418 #if SQLITE_SHELL_HAVE_RECOVER
1419 INCLUDE ../ext/recover/sqlite3recover.h
1420 # ifndef SQLITE_HAVE_SQLITE3R
1421 INCLUDE ../ext/recover/dbdata.c
1422 INCLUDE ../ext/recover/sqlite3recover.c
1423 # endif /* SQLITE_HAVE_SQLITE3R */
1424 #endif
1425 #ifdef SQLITE_SHELL_EXTSRC
1426 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1427 #endif
1429 #if defined(SQLITE_ENABLE_SESSION)
1431 ** State information for a single open session
1433 typedef struct OpenSession OpenSession;
1434 struct OpenSession {
1435 char *zName; /* Symbolic name for this session */
1436 int nFilter; /* Number of xFilter rejection GLOB patterns */
1437 char **azFilter; /* Array of xFilter rejection GLOB patterns */
1438 sqlite3_session *p; /* The open session */
1440 #endif
1442 typedef struct ExpertInfo ExpertInfo;
1443 struct ExpertInfo {
1444 sqlite3expert *pExpert;
1445 int bVerbose;
1448 /* A single line in the EQP output */
1449 typedef struct EQPGraphRow EQPGraphRow;
1450 struct EQPGraphRow {
1451 int iEqpId; /* ID for this row */
1452 int iParentId; /* ID of the parent row */
1453 EQPGraphRow *pNext; /* Next row in sequence */
1454 char zText[1]; /* Text to display for this row */
1457 /* All EQP output is collected into an instance of the following */
1458 typedef struct EQPGraph EQPGraph;
1459 struct EQPGraph {
1460 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
1461 EQPGraphRow *pLast; /* Last element of the pRow list */
1462 char zPrefix[100]; /* Graph prefix */
1465 /* Parameters affecting columnar mode result display (defaulting together) */
1466 typedef struct ColModeOpts {
1467 int iWrap; /* In columnar modes, wrap lines reaching this limit */
1468 u8 bQuote; /* Quote results for .mode box and table */
1469 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */
1470 } ColModeOpts;
1471 #define ColModeOpts_default { 60, 0, 0 }
1472 #define ColModeOpts_default_qbox { 60, 1, 0 }
1475 ** State information about the database connection is contained in an
1476 ** instance of the following structure.
1478 typedef struct ShellState ShellState;
1479 struct ShellState {
1480 sqlite3 *db; /* The database */
1481 u8 autoExplain; /* Automatically turn on .explain mode */
1482 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
1483 u8 autoEQPtest; /* autoEQP is in test mode */
1484 u8 autoEQPtrace; /* autoEQP is in trace mode */
1485 u8 scanstatsOn; /* True to display scan stats before each finalize */
1486 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1487 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
1488 u8 nEqpLevel; /* Depth of the EQP output graph */
1489 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
1490 u8 bSafeMode; /* True to prohibit unsafe operations */
1491 u8 bSafeModePersist; /* The long-term value of bSafeMode */
1492 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
1493 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
1494 unsigned statsOn; /* True to display memory stats before each finalize */
1495 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
1496 int inputNesting; /* Track nesting level of .read and other redirects */
1497 int outCount; /* Revert to stdout when reaching zero */
1498 int cnt; /* Number of records displayed so far */
1499 int lineno; /* Line number of last line read from in */
1500 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
1501 FILE *in; /* Read commands from this stream */
1502 FILE *out; /* Write results here */
1503 FILE *traceOut; /* Output for sqlite3_trace() */
1504 int nErr; /* Number of errors seen */
1505 int mode; /* An output mode setting */
1506 int modePrior; /* Saved mode */
1507 int cMode; /* temporary output mode for the current query */
1508 int normalMode; /* Output mode before ".explain on" */
1509 int writableSchema; /* True if PRAGMA writable_schema=ON */
1510 int showHeader; /* True to show column names in List or Column mode */
1511 int nCheck; /* Number of ".check" commands run */
1512 unsigned nProgress; /* Number of progress callbacks encountered */
1513 unsigned mxProgress; /* Maximum progress callbacks before failing */
1514 unsigned flgProgress; /* Flags for the progress callback */
1515 unsigned shellFlgs; /* Various flags */
1516 unsigned priorShFlgs; /* Saved copy of flags */
1517 sqlite3_int64 szMax; /* --maxsize argument to .open */
1518 char *zDestTable; /* Name of destination table when MODE_Insert */
1519 char *zTempFile; /* Temporary file that might need deleting */
1520 char zTestcase[30]; /* Name of current test case */
1521 char colSeparator[20]; /* Column separator character for several modes */
1522 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1523 char colSepPrior[20]; /* Saved column separator */
1524 char rowSepPrior[20]; /* Saved row separator */
1525 int *colWidth; /* Requested width of each column in columnar modes */
1526 int *actualWidth; /* Actual width of each column */
1527 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
1528 char nullValue[20]; /* The text to print when a NULL comes back from
1529 ** the database */
1530 char outfile[FILENAME_MAX]; /* Filename for *out */
1531 sqlite3_stmt *pStmt; /* Current statement if any. */
1532 FILE *pLog; /* Write log output here */
1533 struct AuxDb { /* Storage space for auxiliary database connections */
1534 sqlite3 *db; /* Connection pointer */
1535 const char *zDbFilename; /* Filename used to open the connection */
1536 char *zFreeOnClose; /* Free this memory allocation on close */
1537 #if defined(SQLITE_ENABLE_SESSION)
1538 int nSession; /* Number of active sessions */
1539 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
1540 #endif
1541 } aAuxDb[5], /* Array of all database connections */
1542 *pAuxDb; /* Currently active database connection */
1543 int *aiIndent; /* Array of indents used in MODE_Explain */
1544 int nIndent; /* Size of array aiIndent[] */
1545 int iIndent; /* Index of current op in aiIndent[] */
1546 char *zNonce; /* Nonce for temporary safe-mode escapes */
1547 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
1548 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
1549 #ifdef SQLITE_SHELL_FIDDLE
1550 struct {
1551 const char * zInput; /* Input string from wasm/JS proxy */
1552 const char * zPos; /* Cursor pos into zInput */
1553 const char * zDefaultDbName; /* Default name for db file */
1554 } wasm;
1555 #endif
1558 #ifdef SQLITE_SHELL_FIDDLE
1559 static ShellState shellState;
1560 #endif
1563 /* Allowed values for ShellState.autoEQP
1565 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1566 #define AUTOEQP_on 1 /* Automatic EQP is on */
1567 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1568 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1570 /* Allowed values for ShellState.openMode
1572 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1573 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1574 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1575 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1576 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1577 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
1578 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
1580 /* Allowed values for ShellState.eTraceType
1582 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1583 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1584 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1586 /* Bits in the ShellState.flgProgress variable */
1587 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1588 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
1589 ** callback limit is reached, and for each
1590 ** top-level SQL statement */
1591 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
1594 ** These are the allowed shellFlgs values
1596 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1597 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1598 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1599 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1600 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1601 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1602 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
1603 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
1604 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
1605 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
1606 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
1609 ** Macros for testing and setting shellFlgs
1611 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1612 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1613 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1616 ** These are the allowed modes.
1618 #define MODE_Line 0 /* One column per line. Blank line between records */
1619 #define MODE_Column 1 /* One record per line in neat columns */
1620 #define MODE_List 2 /* One record per line with a separator */
1621 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1622 #define MODE_Html 4 /* Generate an XHTML table */
1623 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1624 #define MODE_Quote 6 /* Quote values as for SQL */
1625 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1626 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1627 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1628 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1629 #define MODE_Pretty 11 /* Pretty-print schemas */
1630 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1631 #define MODE_Json 13 /* Output JSON */
1632 #define MODE_Markdown 14 /* Markdown formatting */
1633 #define MODE_Table 15 /* MySQL-style table formatting */
1634 #define MODE_Box 16 /* Unicode box-drawing characters */
1635 #define MODE_Count 17 /* Output only a count of the rows of output */
1636 #define MODE_Off 18 /* No query output shown */
1637 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
1639 static const char *modeDescr[] = {
1640 "line",
1641 "column",
1642 "list",
1643 "semi",
1644 "html",
1645 "insert",
1646 "quote",
1647 "tcl",
1648 "csv",
1649 "explain",
1650 "ascii",
1651 "prettyprint",
1652 "eqp",
1653 "json",
1654 "markdown",
1655 "table",
1656 "box",
1657 "count",
1658 "off"
1662 ** These are the column/row/line separators used by the various
1663 ** import/export modes.
1665 #define SEP_Column "|"
1666 #define SEP_Row "\n"
1667 #define SEP_Tab "\t"
1668 #define SEP_Space " "
1669 #define SEP_Comma ","
1670 #define SEP_CrLf "\r\n"
1671 #define SEP_Unit "\x1F"
1672 #define SEP_Record "\x1E"
1675 ** Limit input nesting via .read or any other input redirect.
1676 ** It's not too expensive, so a generous allowance can be made.
1678 #define MAX_INPUT_NESTING 25
1681 ** A callback for the sqlite3_log() interface.
1683 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1684 ShellState *p = (ShellState*)pArg;
1685 if( p->pLog==0 ) return;
1686 sputf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1687 fflush(p->pLog);
1691 ** SQL function: shell_putsnl(X)
1693 ** Write the text X to the screen (or whatever output is being directed)
1694 ** adding a newline at the end, and then return X.
1696 static void shellPutsFunc(
1697 sqlite3_context *pCtx,
1698 int nVal,
1699 sqlite3_value **apVal
1701 /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
1702 (void)nVal;
1703 oputf("%s\n", sqlite3_value_text(apVal[0]));
1704 sqlite3_result_value(pCtx, apVal[0]);
1708 ** If in safe mode, print an error message described by the arguments
1709 ** and exit immediately.
1711 static void failIfSafeMode(
1712 ShellState *p,
1713 const char *zErrMsg,
1716 if( p->bSafeMode ){
1717 va_list ap;
1718 char *zMsg;
1719 va_start(ap, zErrMsg);
1720 zMsg = sqlite3_vmprintf(zErrMsg, ap);
1721 va_end(ap);
1722 eputf("line %d: %s\n", p->lineno, zMsg);
1723 exit(1);
1728 ** SQL function: edit(VALUE)
1729 ** edit(VALUE,EDITOR)
1731 ** These steps:
1733 ** (1) Write VALUE into a temporary file.
1734 ** (2) Run program EDITOR on that temporary file.
1735 ** (3) Read the temporary file back and return its content as the result.
1736 ** (4) Delete the temporary file
1738 ** If the EDITOR argument is omitted, use the value in the VISUAL
1739 ** environment variable. If still there is no EDITOR, through an error.
1741 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1743 #ifndef SQLITE_NOHAVE_SYSTEM
1744 static void editFunc(
1745 sqlite3_context *context,
1746 int argc,
1747 sqlite3_value **argv
1749 const char *zEditor;
1750 char *zTempFile = 0;
1751 sqlite3 *db;
1752 char *zCmd = 0;
1753 int bBin;
1754 int rc;
1755 int hasCRNL = 0;
1756 FILE *f = 0;
1757 sqlite3_int64 sz;
1758 sqlite3_int64 x;
1759 unsigned char *p = 0;
1761 if( argc==2 ){
1762 zEditor = (const char*)sqlite3_value_text(argv[1]);
1763 }else{
1764 zEditor = getenv("VISUAL");
1766 if( zEditor==0 ){
1767 sqlite3_result_error(context, "no editor for edit()", -1);
1768 return;
1770 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1771 sqlite3_result_error(context, "NULL input to edit()", -1);
1772 return;
1774 db = sqlite3_context_db_handle(context);
1775 zTempFile = 0;
1776 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1777 if( zTempFile==0 ){
1778 sqlite3_uint64 r = 0;
1779 sqlite3_randomness(sizeof(r), &r);
1780 zTempFile = sqlite3_mprintf("temp%llx", r);
1781 if( zTempFile==0 ){
1782 sqlite3_result_error_nomem(context);
1783 return;
1786 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1787 /* When writing the file to be edited, do \n to \r\n conversions on systems
1788 ** that want \r\n line endings */
1789 f = fopen(zTempFile, bBin ? "wb" : "w");
1790 if( f==0 ){
1791 sqlite3_result_error(context, "edit() cannot open temp file", -1);
1792 goto edit_func_end;
1794 sz = sqlite3_value_bytes(argv[0]);
1795 if( bBin ){
1796 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1797 }else{
1798 const char *z = (const char*)sqlite3_value_text(argv[0]);
1799 /* Remember whether or not the value originally contained \r\n */
1800 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
1801 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1803 fclose(f);
1804 f = 0;
1805 if( x!=sz ){
1806 sqlite3_result_error(context, "edit() could not write the whole file", -1);
1807 goto edit_func_end;
1809 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1810 if( zCmd==0 ){
1811 sqlite3_result_error_nomem(context);
1812 goto edit_func_end;
1814 rc = system(zCmd);
1815 sqlite3_free(zCmd);
1816 if( rc ){
1817 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1818 goto edit_func_end;
1820 f = fopen(zTempFile, "rb");
1821 if( f==0 ){
1822 sqlite3_result_error(context,
1823 "edit() cannot reopen temp file after edit", -1);
1824 goto edit_func_end;
1826 fseek(f, 0, SEEK_END);
1827 sz = ftell(f);
1828 rewind(f);
1829 p = sqlite3_malloc64( sz+1 );
1830 if( p==0 ){
1831 sqlite3_result_error_nomem(context);
1832 goto edit_func_end;
1834 x = fread(p, 1, (size_t)sz, f);
1835 fclose(f);
1836 f = 0;
1837 if( x!=sz ){
1838 sqlite3_result_error(context, "could not read back the whole file", -1);
1839 goto edit_func_end;
1841 if( bBin ){
1842 sqlite3_result_blob64(context, p, sz, sqlite3_free);
1843 }else{
1844 sqlite3_int64 i, j;
1845 if( hasCRNL ){
1846 /* If the original contains \r\n then do no conversions back to \n */
1847 }else{
1848 /* If the file did not originally contain \r\n then convert any new
1849 ** \r\n back into \n */
1850 p[sz] = 0;
1851 for(i=j=0; i<sz; i++){
1852 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1853 p[j++] = p[i];
1855 sz = j;
1856 p[sz] = 0;
1858 sqlite3_result_text64(context, (const char*)p, sz,
1859 sqlite3_free, SQLITE_UTF8);
1861 p = 0;
1863 edit_func_end:
1864 if( f ) fclose(f);
1865 unlink(zTempFile);
1866 sqlite3_free(zTempFile);
1867 sqlite3_free(p);
1869 #endif /* SQLITE_NOHAVE_SYSTEM */
1872 ** Save or restore the current output mode
1874 static void outputModePush(ShellState *p){
1875 p->modePrior = p->mode;
1876 p->priorShFlgs = p->shellFlgs;
1877 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1878 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1880 static void outputModePop(ShellState *p){
1881 p->mode = p->modePrior;
1882 p->shellFlgs = p->priorShFlgs;
1883 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1884 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1888 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1890 static void output_hex_blob(const void *pBlob, int nBlob){
1891 int i;
1892 unsigned char *aBlob = (unsigned char*)pBlob;
1894 char *zStr = sqlite3_malloc(nBlob*2 + 1);
1895 shell_check_oom(zStr);
1897 for(i=0; i<nBlob; i++){
1898 static const char aHex[] = {
1899 '0', '1', '2', '3', '4', '5', '6', '7',
1900 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1902 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1903 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1905 zStr[i*2] = '\0';
1907 oputf("X'%s'", zStr);
1908 sqlite3_free(zStr);
1912 ** Find a string that is not found anywhere in z[]. Return a pointer
1913 ** to that string.
1915 ** Try to use zA and zB first. If both of those are already found in z[]
1916 ** then make up some string and store it in the buffer zBuf.
1918 static const char *unused_string(
1919 const char *z, /* Result must not appear anywhere in z */
1920 const char *zA, const char *zB, /* Try these first */
1921 char *zBuf /* Space to store a generated string */
1923 unsigned i = 0;
1924 if( strstr(z, zA)==0 ) return zA;
1925 if( strstr(z, zB)==0 ) return zB;
1927 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1928 }while( strstr(z,zBuf)!=0 );
1929 return zBuf;
1933 ** Output the given string as a quoted string using SQL quoting conventions.
1935 ** See also: output_quoted_escaped_string()
1937 static void output_quoted_string(const char *z){
1938 int i;
1939 char c;
1940 #ifndef SQLITE_SHELL_FIDDLE
1941 FILE *pfO = setOutputStream(invalidFileStream);
1942 setBinaryMode(pfO, 1);
1943 #endif
1944 if( z==0 ) return;
1945 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1946 if( c==0 ){
1947 oputf("'%s'",z);
1948 }else{
1949 oputz("'");
1950 while( *z ){
1951 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1952 if( c=='\'' ) i++;
1953 if( i ){
1954 oputf("%.*s", i, z);
1955 z += i;
1957 if( c=='\'' ){
1958 oputz("'");
1959 continue;
1961 if( c==0 ){
1962 break;
1964 z++;
1966 oputz("'");
1968 #ifndef SQLITE_SHELL_FIDDLE
1969 setTextMode(pfO, 1);
1970 #else
1971 setTextMode(stdout, 1);
1972 #endif
1976 ** Output the given string as a quoted string using SQL quoting conventions.
1977 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1978 ** get corrupted by end-of-line translation facilities in some operating
1979 ** systems.
1981 ** This is like output_quoted_string() but with the addition of the \r\n
1982 ** escape mechanism.
1984 static void output_quoted_escaped_string(const char *z){
1985 int i;
1986 char c;
1987 #ifndef SQLITE_SHELL_FIDDLE
1988 FILE *pfO = setOutputStream(invalidFileStream);
1989 setBinaryMode(pfO, 1);
1990 #endif
1991 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1992 if( c==0 ){
1993 oputf("'%s'",z);
1994 }else{
1995 const char *zNL = 0;
1996 const char *zCR = 0;
1997 int nNL = 0;
1998 int nCR = 0;
1999 char zBuf1[20], zBuf2[20];
2000 for(i=0; z[i]; i++){
2001 if( z[i]=='\n' ) nNL++;
2002 if( z[i]=='\r' ) nCR++;
2004 if( nNL ){
2005 oputz("replace(");
2006 zNL = unused_string(z, "\\n", "\\012", zBuf1);
2008 if( nCR ){
2009 oputz("replace(");
2010 zCR = unused_string(z, "\\r", "\\015", zBuf2);
2012 oputz("'");
2013 while( *z ){
2014 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
2015 if( c=='\'' ) i++;
2016 if( i ){
2017 oputf("%.*s", i, z);
2018 z += i;
2020 if( c=='\'' ){
2021 oputz("'");
2022 continue;
2024 if( c==0 ){
2025 break;
2027 z++;
2028 if( c=='\n' ){
2029 oputz(zNL);
2030 continue;
2032 oputz(zCR);
2034 oputz("'");
2035 if( nCR ){
2036 oputf(",'%s',char(13))", zCR);
2038 if( nNL ){
2039 oputf(",'%s',char(10))", zNL);
2042 #ifndef SQLITE_SHELL_FIDDLE
2043 setTextMode(pfO, 1);
2044 #else
2045 setTextMode(stdout, 1);
2046 #endif
2050 ** Find earliest of chars within s specified in zAny.
2051 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
2053 static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
2054 const char *pcFirst = 0;
2055 if( ns == ~(size_t)0 ) ns = strlen(s);
2056 while(*zAny){
2057 const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
2058 if( pc ){
2059 pcFirst = pc;
2060 ns = pcFirst - s;
2062 ++zAny;
2064 return pcFirst;
2067 ** Output the given string as a quoted according to C or TCL quoting rules.
2069 static void output_c_string(const char *z){
2070 char c;
2071 static const char *zq = "\"";
2072 static long ctrlMask = ~0L;
2073 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
2074 char ace[3] = "\\?";
2075 char cbsSay;
2076 oputz(zq);
2077 while( *z!=0 ){
2078 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
2079 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
2080 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
2081 if( pcEnd > z ) oputb(z, (int)(pcEnd-z));
2082 if( (c = *pcEnd)==0 ) break;
2083 ++pcEnd;
2084 switch( c ){
2085 case '\\': case '"':
2086 cbsSay = (char)c;
2087 break;
2088 case '\t': cbsSay = 't'; break;
2089 case '\n': cbsSay = 'n'; break;
2090 case '\r': cbsSay = 'r'; break;
2091 case '\f': cbsSay = 'f'; break;
2092 default: cbsSay = 0; break;
2094 if( cbsSay ){
2095 ace[1] = cbsSay;
2096 oputz(ace);
2097 }else if( !isprint(c&0xff) ){
2098 oputf("\\%03o", c&0xff);
2099 }else{
2100 ace[1] = (char)c;
2101 oputz(ace+1);
2103 z = pcEnd;
2105 oputz(zq);
2109 ** Output the given string as a quoted according to JSON quoting rules.
2111 static void output_json_string(const char *z, i64 n){
2112 char c;
2113 static const char *zq = "\"";
2114 static long ctrlMask = ~0L;
2115 static const char *zDQBS = "\"\\";
2116 const char *pcLimit;
2117 char ace[3] = "\\?";
2118 char cbsSay;
2120 if( z==0 ) z = "";
2121 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
2122 oputz(zq);
2123 while( z < pcLimit ){
2124 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
2125 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
2126 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
2127 if( pcEnd > z ){
2128 oputb(z, (int)(pcEnd-z));
2129 z = pcEnd;
2131 if( z >= pcLimit ) break;
2132 c = *(z++);
2133 switch( c ){
2134 case '"': case '\\':
2135 cbsSay = (char)c;
2136 break;
2137 case '\b': cbsSay = 'b'; break;
2138 case '\f': cbsSay = 'f'; break;
2139 case '\n': cbsSay = 'n'; break;
2140 case '\r': cbsSay = 'r'; break;
2141 case '\t': cbsSay = 't'; break;
2142 default: cbsSay = 0; break;
2144 if( cbsSay ){
2145 ace[1] = cbsSay;
2146 oputz(ace);
2147 }else if( c<=0x1f ){
2148 oputf("u%04x", c);
2149 }else{
2150 ace[1] = (char)c;
2151 oputz(ace+1);
2154 oputz(zq);
2158 ** Output the given string with characters that are special to
2159 ** HTML escaped.
2161 static void output_html_string(const char *z){
2162 int i;
2163 if( z==0 ) z = "";
2164 while( *z ){
2165 for(i=0; z[i]
2166 && z[i]!='<'
2167 && z[i]!='&'
2168 && z[i]!='>'
2169 && z[i]!='\"'
2170 && z[i]!='\'';
2171 i++){}
2172 if( i>0 ){
2173 oputf("%.*s",i,z);
2175 if( z[i]=='<' ){
2176 oputz("&lt;");
2177 }else if( z[i]=='&' ){
2178 oputz("&amp;");
2179 }else if( z[i]=='>' ){
2180 oputz("&gt;");
2181 }else if( z[i]=='\"' ){
2182 oputz("&quot;");
2183 }else if( z[i]=='\'' ){
2184 oputz("&#39;");
2185 }else{
2186 break;
2188 z += i + 1;
2193 ** If a field contains any character identified by a 1 in the following
2194 ** array, then the string must be quoted for CSV.
2196 static const char needCsvQuote[] = {
2197 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2198 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2199 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
2200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2205 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2207 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2208 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2209 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2210 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2211 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2212 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2216 ** Output a single term of CSV. Actually, p->colSeparator is used for
2217 ** the separator, which may or may not be a comma. p->nullValue is
2218 ** the null value. Strings are quoted if necessary. The separator
2219 ** is only issued if bSep is true.
2221 static void output_csv(ShellState *p, const char *z, int bSep){
2222 if( z==0 ){
2223 oputf("%s",p->nullValue);
2224 }else{
2225 unsigned i;
2226 for(i=0; z[i]; i++){
2227 if( needCsvQuote[((unsigned char*)z)[i]] ){
2228 i = 0;
2229 break;
2232 if( i==0 || strstr(z, p->colSeparator)!=0 ){
2233 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
2234 shell_check_oom(zQuoted);
2235 oputz(zQuoted);
2236 sqlite3_free(zQuoted);
2237 }else{
2238 oputz(z);
2241 if( bSep ){
2242 oputz(p->colSeparator);
2247 ** This routine runs when the user presses Ctrl-C
2249 static void interrupt_handler(int NotUsed){
2250 UNUSED_PARAMETER(NotUsed);
2251 if( ++seenInterrupt>1 ) exit(1);
2252 if( globalDb ) sqlite3_interrupt(globalDb);
2255 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2257 ** This routine runs for console events (e.g. Ctrl-C) on Win32
2259 static BOOL WINAPI ConsoleCtrlHandler(
2260 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
2262 if( dwCtrlType==CTRL_C_EVENT ){
2263 interrupt_handler(0);
2264 return TRUE;
2266 return FALSE;
2268 #endif
2270 #ifndef SQLITE_OMIT_AUTHORIZATION
2272 ** This authorizer runs in safe mode.
2274 static int safeModeAuth(
2275 void *pClientData,
2276 int op,
2277 const char *zA1,
2278 const char *zA2,
2279 const char *zA3,
2280 const char *zA4
2282 ShellState *p = (ShellState*)pClientData;
2283 static const char *azProhibitedFunctions[] = {
2284 "edit",
2285 "fts3_tokenizer",
2286 "load_extension",
2287 "readfile",
2288 "writefile",
2289 "zipfile",
2290 "zipfile_cds",
2292 UNUSED_PARAMETER(zA1);
2293 UNUSED_PARAMETER(zA3);
2294 UNUSED_PARAMETER(zA4);
2295 switch( op ){
2296 case SQLITE_ATTACH: {
2297 #ifndef SQLITE_SHELL_FIDDLE
2298 /* In WASM builds the filesystem is a virtual sandbox, so
2299 ** there's no harm in using ATTACH. */
2300 failIfSafeMode(p, "cannot run ATTACH in safe mode");
2301 #endif
2302 break;
2304 case SQLITE_FUNCTION: {
2305 int i;
2306 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
2307 if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
2308 failIfSafeMode(p, "cannot use the %s() function in safe mode",
2309 azProhibitedFunctions[i]);
2312 break;
2315 return SQLITE_OK;
2319 ** When the ".auth ON" is set, the following authorizer callback is
2320 ** invoked. It always returns SQLITE_OK.
2322 static int shellAuth(
2323 void *pClientData,
2324 int op,
2325 const char *zA1,
2326 const char *zA2,
2327 const char *zA3,
2328 const char *zA4
2330 ShellState *p = (ShellState*)pClientData;
2331 static const char *azAction[] = { 0,
2332 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2333 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2334 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2335 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2336 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2337 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2338 "PRAGMA", "READ", "SELECT",
2339 "TRANSACTION", "UPDATE", "ATTACH",
2340 "DETACH", "ALTER_TABLE", "REINDEX",
2341 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2342 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2344 int i;
2345 const char *az[4];
2346 az[0] = zA1;
2347 az[1] = zA2;
2348 az[2] = zA3;
2349 az[3] = zA4;
2350 oputf("authorizer: %s", azAction[op]);
2351 for(i=0; i<4; i++){
2352 oputz(" ");
2353 if( az[i] ){
2354 output_c_string(az[i]);
2355 }else{
2356 oputz("NULL");
2359 oputz("\n");
2360 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
2361 return SQLITE_OK;
2363 #endif
2366 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2368 ** This routine converts some CREATE TABLE statements for shadow tables
2369 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2371 ** If the schema statement in z[] contains a start-of-comment and if
2372 ** sqlite3_complete() returns false, try to terminate the comment before
2373 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2375 static void printSchemaLine(const char *z, const char *zTail){
2376 char *zToFree = 0;
2377 if( z==0 ) return;
2378 if( zTail==0 ) return;
2379 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
2380 const char *zOrig = z;
2381 static const char *azTerm[] = { "", "*/", "\n" };
2382 int i;
2383 for(i=0; i<ArraySize(azTerm); i++){
2384 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
2385 shell_check_oom(zNew);
2386 if( sqlite3_complete(zNew) ){
2387 size_t n = strlen(zNew);
2388 zNew[n-1] = 0;
2389 zToFree = zNew;
2390 z = zNew;
2391 break;
2393 sqlite3_free(zNew);
2396 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
2397 oputf("CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
2398 }else{
2399 oputf("%s%s", z, zTail);
2401 sqlite3_free(zToFree);
2403 static void printSchemaLineN(char *z, int n, const char *zTail){
2404 char c = z[n];
2405 z[n] = 0;
2406 printSchemaLine(z, zTail);
2407 z[n] = c;
2411 ** Return true if string z[] has nothing but whitespace and comments to the
2412 ** end of the first line.
2414 static int wsToEol(const char *z){
2415 int i;
2416 for(i=0; z[i]; i++){
2417 if( z[i]=='\n' ) return 1;
2418 if( IsSpace(z[i]) ) continue;
2419 if( z[i]=='-' && z[i+1]=='-' ) return 1;
2420 return 0;
2422 return 1;
2426 ** Add a new entry to the EXPLAIN QUERY PLAN data
2428 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2429 EQPGraphRow *pNew;
2430 i64 nText;
2431 if( zText==0 ) return;
2432 nText = strlen(zText);
2433 if( p->autoEQPtest ){
2434 oputf("%d,%d,%s\n", iEqpId, p2, zText);
2436 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2437 shell_check_oom(pNew);
2438 pNew->iEqpId = iEqpId;
2439 pNew->iParentId = p2;
2440 memcpy(pNew->zText, zText, nText+1);
2441 pNew->pNext = 0;
2442 if( p->sGraph.pLast ){
2443 p->sGraph.pLast->pNext = pNew;
2444 }else{
2445 p->sGraph.pRow = pNew;
2447 p->sGraph.pLast = pNew;
2451 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2452 ** in p->sGraph.
2454 static void eqp_reset(ShellState *p){
2455 EQPGraphRow *pRow, *pNext;
2456 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2457 pNext = pRow->pNext;
2458 sqlite3_free(pRow);
2460 memset(&p->sGraph, 0, sizeof(p->sGraph));
2463 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2464 ** pOld, or return the first such line if pOld is NULL
2466 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2467 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2468 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2469 return pRow;
2472 /* Render a single level of the graph that has iEqpId as its parent. Called
2473 ** recursively to render sublevels.
2475 static void eqp_render_level(ShellState *p, int iEqpId){
2476 EQPGraphRow *pRow, *pNext;
2477 i64 n = strlen(p->sGraph.zPrefix);
2478 char *z;
2479 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2480 pNext = eqp_next_row(p, iEqpId, pRow);
2481 z = pRow->zText;
2482 oputf("%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
2483 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2484 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
2485 eqp_render_level(p, pRow->iEqpId);
2486 p->sGraph.zPrefix[n] = 0;
2492 ** Display and reset the EXPLAIN QUERY PLAN data
2494 static void eqp_render(ShellState *p, i64 nCycle){
2495 EQPGraphRow *pRow = p->sGraph.pRow;
2496 if( pRow ){
2497 if( pRow->zText[0]=='-' ){
2498 if( pRow->pNext==0 ){
2499 eqp_reset(p);
2500 return;
2502 oputf("%s\n", pRow->zText+3);
2503 p->sGraph.pRow = pRow->pNext;
2504 sqlite3_free(pRow);
2505 }else if( nCycle>0 ){
2506 oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
2507 }else{
2508 oputz("QUERY PLAN\n");
2510 p->sGraph.zPrefix[0] = 0;
2511 eqp_render_level(p, 0);
2512 eqp_reset(p);
2516 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2518 ** Progress handler callback.
2520 static int progress_handler(void *pClientData) {
2521 ShellState *p = (ShellState*)pClientData;
2522 p->nProgress++;
2523 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2524 oputf("Progress limit reached (%u)\n", p->nProgress);
2525 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2526 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2527 return 1;
2529 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2530 oputf("Progress %u\n", p->nProgress);
2532 return 0;
2534 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2537 ** Print N dashes
2539 static void print_dashes(int N){
2540 const char zDash[] = "--------------------------------------------------";
2541 const int nDash = sizeof(zDash) - 1;
2542 while( N>nDash ){
2543 oputz(zDash);
2544 N -= nDash;
2546 oputf("%.*s", N, zDash);
2550 ** Print a markdown or table-style row separator using ascii-art
2552 static void print_row_separator(
2553 ShellState *p,
2554 int nArg,
2555 const char *zSep
2557 int i;
2558 if( nArg>0 ){
2559 oputz(zSep);
2560 print_dashes(p->actualWidth[0]+2);
2561 for(i=1; i<nArg; i++){
2562 oputz(zSep);
2563 print_dashes(p->actualWidth[i]+2);
2565 oputz(zSep);
2567 oputz("\n");
2571 ** This is the callback routine that the shell
2572 ** invokes for each row of a query result.
2574 static int shell_callback(
2575 void *pArg,
2576 int nArg, /* Number of result columns */
2577 char **azArg, /* Text of each result column */
2578 char **azCol, /* Column names */
2579 int *aiType /* Column types. Might be NULL */
2581 int i;
2582 ShellState *p = (ShellState*)pArg;
2584 if( azArg==0 ) return 0;
2585 switch( p->cMode ){
2586 case MODE_Count:
2587 case MODE_Off: {
2588 break;
2590 case MODE_Line: {
2591 int w = 5;
2592 if( azArg==0 ) break;
2593 for(i=0; i<nArg; i++){
2594 int len = strlen30(azCol[i] ? azCol[i] : "");
2595 if( len>w ) w = len;
2597 if( p->cnt++>0 ) oputz(p->rowSeparator);
2598 for(i=0; i<nArg; i++){
2599 oputf("%*s = %s%s", w, azCol[i],
2600 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2602 break;
2604 case MODE_ScanExp:
2605 case MODE_Explain: {
2606 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2607 static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 };
2608 static const int aScanExpWidth[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13};
2609 static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
2611 const int *aWidth = aExplainWidth;
2612 const int *aMap = aExplainMap;
2613 int nWidth = ArraySize(aExplainWidth);
2614 int iIndent = 1;
2616 if( p->cMode==MODE_ScanExp ){
2617 aWidth = aScanExpWidth;
2618 aMap = aScanExpMap;
2619 nWidth = ArraySize(aScanExpWidth);
2620 iIndent = 3;
2622 if( nArg>nWidth ) nArg = nWidth;
2624 /* If this is the first row seen, print out the headers */
2625 if( p->cnt++==0 ){
2626 for(i=0; i<nArg; i++){
2627 utf8_width_print(aWidth[i], azCol[ aMap[i] ]);
2628 oputz(i==nArg-1 ? "\n" : " ");
2630 for(i=0; i<nArg; i++){
2631 print_dashes(aWidth[i]);
2632 oputz(i==nArg-1 ? "\n" : " ");
2636 /* If there is no data, exit early. */
2637 if( azArg==0 ) break;
2639 for(i=0; i<nArg; i++){
2640 const char *zSep = " ";
2641 int w = aWidth[i];
2642 const char *zVal = azArg[ aMap[i] ];
2643 if( i==nArg-1 ) w = 0;
2644 if( zVal && strlenChar(zVal)>w ){
2645 w = strlenChar(zVal);
2646 zSep = " ";
2648 if( i==iIndent && p->aiIndent && p->pStmt ){
2649 if( p->iIndent<p->nIndent ){
2650 oputf("%*.s", p->aiIndent[p->iIndent], "");
2652 p->iIndent++;
2654 utf8_width_print(w, zVal ? zVal : p->nullValue);
2655 oputz(i==nArg-1 ? "\n" : zSep);
2657 break;
2659 case MODE_Semi: { /* .schema and .fullschema output */
2660 printSchemaLine(azArg[0], ";\n");
2661 break;
2663 case MODE_Pretty: { /* .schema and .fullschema with --indent */
2664 char *z;
2665 int j;
2666 int nParen = 0;
2667 char cEnd = 0;
2668 char c;
2669 int nLine = 0;
2670 assert( nArg==1 );
2671 if( azArg[0]==0 ) break;
2672 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2673 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2675 oputf("%s;\n", azArg[0]);
2676 break;
2678 z = sqlite3_mprintf("%s", azArg[0]);
2679 shell_check_oom(z);
2680 j = 0;
2681 for(i=0; IsSpace(z[i]); i++){}
2682 for(; (c = z[i])!=0; i++){
2683 if( IsSpace(c) ){
2684 if( z[j-1]=='\r' ) z[j-1] = '\n';
2685 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2686 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2687 j--;
2689 z[j++] = c;
2691 while( j>0 && IsSpace(z[j-1]) ){ j--; }
2692 z[j] = 0;
2693 if( strlen30(z)>=79 ){
2694 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2695 if( c==cEnd ){
2696 cEnd = 0;
2697 }else if( c=='"' || c=='\'' || c=='`' ){
2698 cEnd = c;
2699 }else if( c=='[' ){
2700 cEnd = ']';
2701 }else if( c=='-' && z[i+1]=='-' ){
2702 cEnd = '\n';
2703 }else if( c=='(' ){
2704 nParen++;
2705 }else if( c==')' ){
2706 nParen--;
2707 if( nLine>0 && nParen==0 && j>0 ){
2708 printSchemaLineN(z, j, "\n");
2709 j = 0;
2712 z[j++] = c;
2713 if( nParen==1 && cEnd==0
2714 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2716 if( c=='\n' ) j--;
2717 printSchemaLineN(z, j, "\n ");
2718 j = 0;
2719 nLine++;
2720 while( IsSpace(z[i+1]) ){ i++; }
2723 z[j] = 0;
2725 printSchemaLine(z, ";\n");
2726 sqlite3_free(z);
2727 break;
2729 case MODE_List: {
2730 if( p->cnt++==0 && p->showHeader ){
2731 for(i=0; i<nArg; i++){
2732 oputf("%s%s",azCol[i], i==nArg-1 ? p->rowSeparator : p->colSeparator);
2735 if( azArg==0 ) break;
2736 for(i=0; i<nArg; i++){
2737 char *z = azArg[i];
2738 if( z==0 ) z = p->nullValue;
2739 oputz(z);
2740 oputz((i<nArg-1)? p->colSeparator : p->rowSeparator);
2742 break;
2744 case MODE_Html: {
2745 if( p->cnt++==0 && p->showHeader ){
2746 oputz("<TR>");
2747 for(i=0; i<nArg; i++){
2748 oputz("<TH>");
2749 output_html_string(azCol[i]);
2750 oputz("</TH>\n");
2752 oputz("</TR>\n");
2754 if( azArg==0 ) break;
2755 oputz("<TR>");
2756 for(i=0; i<nArg; i++){
2757 oputz("<TD>");
2758 output_html_string(azArg[i] ? azArg[i] : p->nullValue);
2759 oputz("</TD>\n");
2761 oputz("</TR>\n");
2762 break;
2764 case MODE_Tcl: {
2765 if( p->cnt++==0 && p->showHeader ){
2766 for(i=0; i<nArg; i++){
2767 output_c_string(azCol[i] ? azCol[i] : "");
2768 if(i<nArg-1) oputz(p->colSeparator);
2770 oputz(p->rowSeparator);
2772 if( azArg==0 ) break;
2773 for(i=0; i<nArg; i++){
2774 output_c_string(azArg[i] ? azArg[i] : p->nullValue);
2775 if(i<nArg-1) oputz(p->colSeparator);
2777 oputz(p->rowSeparator);
2778 break;
2780 case MODE_Csv: {
2781 setBinaryMode(p->out, 1);
2782 if( p->cnt++==0 && p->showHeader ){
2783 for(i=0; i<nArg; i++){
2784 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2786 oputz(p->rowSeparator);
2788 if( nArg>0 ){
2789 for(i=0; i<nArg; i++){
2790 output_csv(p, azArg[i], i<nArg-1);
2792 oputz(p->rowSeparator);
2794 setTextMode(p->out, 1);
2795 break;
2797 case MODE_Insert: {
2798 if( azArg==0 ) break;
2799 oputf("INSERT INTO %s",p->zDestTable);
2800 if( p->showHeader ){
2801 oputz("(");
2802 for(i=0; i<nArg; i++){
2803 if( i>0 ) oputz(",");
2804 if( quoteChar(azCol[i]) ){
2805 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2806 shell_check_oom(z);
2807 oputz(z);
2808 sqlite3_free(z);
2809 }else{
2810 oputf("%s", azCol[i]);
2813 oputz(")");
2815 p->cnt++;
2816 for(i=0; i<nArg; i++){
2817 oputz(i>0 ? "," : " VALUES(");
2818 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2819 oputz("NULL");
2820 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2821 if( ShellHasFlag(p, SHFLG_Newlines) ){
2822 output_quoted_string(azArg[i]);
2823 }else{
2824 output_quoted_escaped_string(azArg[i]);
2826 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2827 oputz(azArg[i]);
2828 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2829 char z[50];
2830 double r = sqlite3_column_double(p->pStmt, i);
2831 sqlite3_uint64 ur;
2832 memcpy(&ur,&r,sizeof(r));
2833 if( ur==0x7ff0000000000000LL ){
2834 oputz("9.0e+999");
2835 }else if( ur==0xfff0000000000000LL ){
2836 oputz("-9.0e+999");
2837 }else{
2838 sqlite3_int64 ir = (sqlite3_int64)r;
2839 if( r==(double)ir ){
2840 sqlite3_snprintf(50,z,"%lld.0", ir);
2841 }else{
2842 sqlite3_snprintf(50,z,"%!.20g", r);
2844 oputz(z);
2846 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2847 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2848 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2849 output_hex_blob(pBlob, nBlob);
2850 }else if( isNumber(azArg[i], 0) ){
2851 oputz(azArg[i]);
2852 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2853 output_quoted_string(azArg[i]);
2854 }else{
2855 output_quoted_escaped_string(azArg[i]);
2858 oputz(");\n");
2859 break;
2861 case MODE_Json: {
2862 if( azArg==0 ) break;
2863 if( p->cnt==0 ){
2864 fputs("[{", p->out);
2865 }else{
2866 fputs(",\n{", p->out);
2868 p->cnt++;
2869 for(i=0; i<nArg; i++){
2870 output_json_string(azCol[i], -1);
2871 oputz(":");
2872 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2873 oputz("null");
2874 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2875 char z[50];
2876 double r = sqlite3_column_double(p->pStmt, i);
2877 sqlite3_uint64 ur;
2878 memcpy(&ur,&r,sizeof(r));
2879 if( ur==0x7ff0000000000000LL ){
2880 oputz("9.0e+999");
2881 }else if( ur==0xfff0000000000000LL ){
2882 oputz("-9.0e+999");
2883 }else{
2884 sqlite3_snprintf(50,z,"%!.20g", r);
2885 oputz(z);
2887 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2888 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2889 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2890 output_json_string(pBlob, nBlob);
2891 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2892 output_json_string(azArg[i], -1);
2893 }else{
2894 oputz(azArg[i]);
2896 if( i<nArg-1 ){
2897 oputz(",");
2900 oputz("}");
2901 break;
2903 case MODE_Quote: {
2904 if( azArg==0 ) break;
2905 if( p->cnt==0 && p->showHeader ){
2906 for(i=0; i<nArg; i++){
2907 if( i>0 ) fputs(p->colSeparator, p->out);
2908 output_quoted_string(azCol[i]);
2910 fputs(p->rowSeparator, p->out);
2912 p->cnt++;
2913 for(i=0; i<nArg; i++){
2914 if( i>0 ) fputs(p->colSeparator, p->out);
2915 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2916 oputz("NULL");
2917 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2918 output_quoted_string(azArg[i]);
2919 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2920 oputz(azArg[i]);
2921 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2922 char z[50];
2923 double r = sqlite3_column_double(p->pStmt, i);
2924 sqlite3_snprintf(50,z,"%!.20g", r);
2925 oputz(z);
2926 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2927 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2928 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2929 output_hex_blob(pBlob, nBlob);
2930 }else if( isNumber(azArg[i], 0) ){
2931 oputz(azArg[i]);
2932 }else{
2933 output_quoted_string(azArg[i]);
2936 fputs(p->rowSeparator, p->out);
2937 break;
2939 case MODE_Ascii: {
2940 if( p->cnt++==0 && p->showHeader ){
2941 for(i=0; i<nArg; i++){
2942 if( i>0 ) oputz(p->colSeparator);
2943 oputz(azCol[i] ? azCol[i] : "");
2945 oputz(p->rowSeparator);
2947 if( azArg==0 ) break;
2948 for(i=0; i<nArg; i++){
2949 if( i>0 ) oputz(p->colSeparator);
2950 oputz(azArg[i] ? azArg[i] : p->nullValue);
2952 oputz(p->rowSeparator);
2953 break;
2955 case MODE_EQP: {
2956 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2957 break;
2960 return 0;
2964 ** This is the callback routine that the SQLite library
2965 ** invokes for each row of a query result.
2967 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2968 /* since we don't have type info, call the shell_callback with a NULL value */
2969 return shell_callback(pArg, nArg, azArg, azCol, NULL);
2973 ** This is the callback routine from sqlite3_exec() that appends all
2974 ** output onto the end of a ShellText object.
2976 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2977 ShellText *p = (ShellText*)pArg;
2978 int i;
2979 UNUSED_PARAMETER(az);
2980 if( azArg==0 ) return 0;
2981 if( p->n ) appendText(p, "|", 0);
2982 for(i=0; i<nArg; i++){
2983 if( i ) appendText(p, ",", 0);
2984 if( azArg[i] ) appendText(p, azArg[i], 0);
2986 return 0;
2990 ** Generate an appropriate SELFTEST table in the main database.
2992 static void createSelftestTable(ShellState *p){
2993 char *zErrMsg = 0;
2994 sqlite3_exec(p->db,
2995 "SAVEPOINT selftest_init;\n"
2996 "CREATE TABLE IF NOT EXISTS selftest(\n"
2997 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2998 " op TEXT,\n" /* Operator: memo run */
2999 " cmd TEXT,\n" /* Command text */
3000 " ans TEXT\n" /* Desired answer */
3001 ");"
3002 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
3003 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
3004 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
3005 " 'memo','Tests generated by --init');\n"
3006 "INSERT INTO [_shell$self]\n"
3007 " SELECT 'run',\n"
3008 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
3009 "FROM sqlite_schema ORDER BY 2'',224))',\n"
3010 " hex(sha3_query('SELECT type,name,tbl_name,sql "
3011 "FROM sqlite_schema ORDER BY 2',224));\n"
3012 "INSERT INTO [_shell$self]\n"
3013 " SELECT 'run',"
3014 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
3015 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
3016 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
3017 " FROM (\n"
3018 " SELECT name FROM sqlite_schema\n"
3019 " WHERE type='table'\n"
3020 " AND name<>'selftest'\n"
3021 " AND coalesce(rootpage,0)>0\n"
3022 " )\n"
3023 " ORDER BY name;\n"
3024 "INSERT INTO [_shell$self]\n"
3025 " VALUES('run','PRAGMA integrity_check','ok');\n"
3026 "INSERT INTO selftest(tno,op,cmd,ans)"
3027 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
3028 "DROP TABLE [_shell$self];"
3029 ,0,0,&zErrMsg);
3030 if( zErrMsg ){
3031 eputf("SELFTEST initialization failure: %s\n", zErrMsg);
3032 sqlite3_free(zErrMsg);
3034 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
3039 ** Set the destination table field of the ShellState structure to
3040 ** the name of the table given. Escape any quote characters in the
3041 ** table name.
3043 static void set_table_name(ShellState *p, const char *zName){
3044 int i, n;
3045 char cQuote;
3046 char *z;
3048 if( p->zDestTable ){
3049 free(p->zDestTable);
3050 p->zDestTable = 0;
3052 if( zName==0 ) return;
3053 cQuote = quoteChar(zName);
3054 n = strlen30(zName);
3055 if( cQuote ) n += n+2;
3056 z = p->zDestTable = malloc( n+1 );
3057 shell_check_oom(z);
3058 n = 0;
3059 if( cQuote ) z[n++] = cQuote;
3060 for(i=0; zName[i]; i++){
3061 z[n++] = zName[i];
3062 if( zName[i]==cQuote ) z[n++] = cQuote;
3064 if( cQuote ) z[n++] = cQuote;
3065 z[n] = 0;
3069 ** Maybe construct two lines of text that point out the position of a
3070 ** syntax error. Return a pointer to the text, in memory obtained from
3071 ** sqlite3_malloc(). Or, if the most recent error does not involve a
3072 ** specific token that we can point to, return an empty string.
3074 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
3075 ** and should be released by the caller invoking sqlite3_free().
3077 static char *shell_error_context(const char *zSql, sqlite3 *db){
3078 int iOffset;
3079 size_t len;
3080 char *zCode;
3081 char *zMsg;
3082 int i;
3083 if( db==0
3084 || zSql==0
3085 || (iOffset = sqlite3_error_offset(db))<0
3086 || iOffset>=(int)strlen(zSql)
3088 return sqlite3_mprintf("");
3090 while( iOffset>50 ){
3091 iOffset--;
3092 zSql++;
3093 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
3095 len = strlen(zSql);
3096 if( len>78 ){
3097 len = 78;
3098 while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
3100 zCode = sqlite3_mprintf("%.*s", len, zSql);
3101 shell_check_oom(zCode);
3102 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
3103 if( iOffset<25 ){
3104 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,"");
3105 }else{
3106 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,"");
3108 return zMsg;
3113 ** Execute a query statement that will generate SQL output. Print
3114 ** the result columns, comma-separated, on a line and then add a
3115 ** semicolon terminator to the end of that line.
3117 ** If the number of columns is 1 and that column contains text "--"
3118 ** then write the semicolon on a separate line. That way, if a
3119 ** "--" comment occurs at the end of the statement, the comment
3120 ** won't consume the semicolon terminator.
3122 static int run_table_dump_query(
3123 ShellState *p, /* Query context */
3124 const char *zSelect /* SELECT statement to extract content */
3126 sqlite3_stmt *pSelect;
3127 int rc;
3128 int nResult;
3129 int i;
3130 const char *z;
3131 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
3132 if( rc!=SQLITE_OK || !pSelect ){
3133 char *zContext = shell_error_context(zSelect, p->db);
3134 oputf("/**** ERROR: (%d) %s *****/\n%s",
3135 rc, sqlite3_errmsg(p->db), zContext);
3136 sqlite3_free(zContext);
3137 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3138 return rc;
3140 rc = sqlite3_step(pSelect);
3141 nResult = sqlite3_column_count(pSelect);
3142 while( rc==SQLITE_ROW ){
3143 z = (const char*)sqlite3_column_text(pSelect, 0);
3144 oputf("%s", z);
3145 for(i=1; i<nResult; i++){
3146 oputf(",%s", sqlite3_column_text(pSelect, i));
3148 if( z==0 ) z = "";
3149 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
3150 if( z[0] ){
3151 oputz("\n;\n");
3152 }else{
3153 oputz(";\n");
3155 rc = sqlite3_step(pSelect);
3157 rc = sqlite3_finalize(pSelect);
3158 if( rc!=SQLITE_OK ){
3159 oputf("/**** ERROR: (%d) %s *****/\n", rc, sqlite3_errmsg(p->db));
3160 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3162 return rc;
3166 ** Allocate space and save off string indicating current error.
3168 static char *save_err_msg(
3169 sqlite3 *db, /* Database to query */
3170 const char *zPhase, /* When the error occurs */
3171 int rc, /* Error code returned from API */
3172 const char *zSql /* SQL string, or NULL */
3174 char *zErr;
3175 char *zContext;
3176 sqlite3_str *pStr = sqlite3_str_new(0);
3177 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
3178 if( rc>1 ){
3179 sqlite3_str_appendf(pStr, " (%d)", rc);
3181 zContext = shell_error_context(zSql, db);
3182 if( zContext ){
3183 sqlite3_str_appendall(pStr, zContext);
3184 sqlite3_free(zContext);
3186 zErr = sqlite3_str_finish(pStr);
3187 shell_check_oom(zErr);
3188 return zErr;
3191 #ifdef __linux__
3193 ** Attempt to display I/O stats on Linux using /proc/PID/io
3195 static void displayLinuxIoStats(void){
3196 FILE *in;
3197 char z[200];
3198 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
3199 in = fopen(z, "rb");
3200 if( in==0 ) return;
3201 while( fgets(z, sizeof(z), in)!=0 ){
3202 static const struct {
3203 const char *zPattern;
3204 const char *zDesc;
3205 } aTrans[] = {
3206 { "rchar: ", "Bytes received by read():" },
3207 { "wchar: ", "Bytes sent to write():" },
3208 { "syscr: ", "Read() system calls:" },
3209 { "syscw: ", "Write() system calls:" },
3210 { "read_bytes: ", "Bytes read from storage:" },
3211 { "write_bytes: ", "Bytes written to storage:" },
3212 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3214 int i;
3215 for(i=0; i<ArraySize(aTrans); i++){
3216 int n = strlen30(aTrans[i].zPattern);
3217 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
3218 oputf("%-36s %s", aTrans[i].zDesc, &z[n]);
3219 break;
3223 fclose(in);
3225 #endif
3228 ** Display a single line of status using 64-bit values.
3230 static void displayStatLine(
3231 char *zLabel, /* Label for this one line */
3232 char *zFormat, /* Format for the result */
3233 int iStatusCtrl, /* Which status to display */
3234 int bReset /* True to reset the stats */
3236 sqlite3_int64 iCur = -1;
3237 sqlite3_int64 iHiwtr = -1;
3238 int i, nPercent;
3239 char zLine[200];
3240 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
3241 for(i=0, nPercent=0; zFormat[i]; i++){
3242 if( zFormat[i]=='%' ) nPercent++;
3244 if( nPercent>1 ){
3245 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
3246 }else{
3247 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
3249 oputf("%-36s %s\n", zLabel, zLine);
3253 ** Display memory stats.
3255 static int display_stats(
3256 sqlite3 *db, /* Database to query */
3257 ShellState *pArg, /* Pointer to ShellState */
3258 int bReset /* True to reset the stats */
3260 int iCur;
3261 int iHiwtr;
3262 if( pArg==0 || pArg->out==0 ) return 0;
3264 if( pArg->pStmt && pArg->statsOn==2 ){
3265 int nCol, i, x;
3266 sqlite3_stmt *pStmt = pArg->pStmt;
3267 char z[100];
3268 nCol = sqlite3_column_count(pStmt);
3269 oputf("%-36s %d\n", "Number of output columns:", nCol);
3270 for(i=0; i<nCol; i++){
3271 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
3272 oputf("%-36s %s\n", z, sqlite3_column_name(pStmt,i));
3273 #ifndef SQLITE_OMIT_DECLTYPE
3274 sqlite3_snprintf(30, z+x, "declared type:");
3275 oputf("%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
3276 #endif
3277 #ifdef SQLITE_ENABLE_COLUMN_METADATA
3278 sqlite3_snprintf(30, z+x, "database name:");
3279 oputf("%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
3280 sqlite3_snprintf(30, z+x, "table name:");
3281 oputf("%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
3282 sqlite3_snprintf(30, z+x, "origin name:");
3283 oputf("%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
3284 #endif
3288 if( pArg->statsOn==3 ){
3289 if( pArg->pStmt ){
3290 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
3291 oputf("VM-steps: %d\n", iCur);
3293 return 0;
3296 displayStatLine("Memory Used:",
3297 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
3298 displayStatLine("Number of Outstanding Allocations:",
3299 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
3300 if( pArg->shellFlgs & SHFLG_Pagecache ){
3301 displayStatLine("Number of Pcache Pages Used:",
3302 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
3304 displayStatLine("Number of Pcache Overflow Bytes:",
3305 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
3306 displayStatLine("Largest Allocation:",
3307 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
3308 displayStatLine("Largest Pcache Allocation:",
3309 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
3310 #ifdef YYTRACKMAXSTACKDEPTH
3311 displayStatLine("Deepest Parser Stack:",
3312 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
3313 #endif
3315 if( db ){
3316 if( pArg->shellFlgs & SHFLG_Lookaside ){
3317 iHiwtr = iCur = -1;
3318 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
3319 &iCur, &iHiwtr, bReset);
3320 oputf("Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
3321 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
3322 &iCur, &iHiwtr, bReset);
3323 oputf("Successful lookaside attempts: %d\n", iHiwtr);
3324 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
3325 &iCur, &iHiwtr, bReset);
3326 oputf("Lookaside failures due to size: %d\n", iHiwtr);
3327 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
3328 &iCur, &iHiwtr, bReset);
3329 oputf("Lookaside failures due to OOM: %d\n", iHiwtr);
3331 iHiwtr = iCur = -1;
3332 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
3333 oputf("Pager Heap Usage: %d bytes\n", iCur);
3334 iHiwtr = iCur = -1;
3335 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
3336 oputf("Page cache hits: %d\n", iCur);
3337 iHiwtr = iCur = -1;
3338 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
3339 oputf("Page cache misses: %d\n", iCur);
3340 iHiwtr = iCur = -1;
3341 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
3342 oputf("Page cache writes: %d\n", iCur);
3343 iHiwtr = iCur = -1;
3344 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
3345 oputf("Page cache spills: %d\n", iCur);
3346 iHiwtr = iCur = -1;
3347 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
3348 oputf("Schema Heap Usage: %d bytes\n", iCur);
3349 iHiwtr = iCur = -1;
3350 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
3351 oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur);
3354 if( pArg->pStmt ){
3355 int iHit, iMiss;
3356 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
3357 bReset);
3358 oputf("Fullscan Steps: %d\n", iCur);
3359 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
3360 oputf("Sort Operations: %d\n", iCur);
3361 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
3362 oputf("Autoindex Inserts: %d\n", iCur);
3363 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
3364 bReset);
3365 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
3366 bReset);
3367 if( iHit || iMiss ){
3368 oputf("Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
3370 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
3371 oputf("Virtual Machine Steps: %d\n", iCur);
3372 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
3373 oputf("Reprepare operations: %d\n", iCur);
3374 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
3375 oputf("Number of times run: %d\n", iCur);
3376 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
3377 oputf("Memory used by prepared stmt: %d\n", iCur);
3380 #ifdef __linux__
3381 displayLinuxIoStats();
3382 #endif
3384 /* Do not remove this machine readable comment: extra-stats-output-here */
3386 return 0;
3390 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3391 static int scanStatsHeight(sqlite3_stmt *p, int iEntry){
3392 int iPid = 0;
3393 int ret = 1;
3394 sqlite3_stmt_scanstatus_v2(p, iEntry,
3395 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3397 while( iPid!=0 ){
3398 int ii;
3399 for(ii=0; 1; ii++){
3400 int iId;
3401 int res;
3402 res = sqlite3_stmt_scanstatus_v2(p, ii,
3403 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
3405 if( res ) break;
3406 if( iId==iPid ){
3407 sqlite3_stmt_scanstatus_v2(p, ii,
3408 SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3412 ret++;
3414 return ret;
3416 #endif
3418 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3419 static void display_explain_scanstats(
3420 sqlite3 *db, /* Database to query */
3421 ShellState *pArg /* Pointer to ShellState */
3423 static const int f = SQLITE_SCANSTAT_COMPLEX;
3424 sqlite3_stmt *p = pArg->pStmt;
3425 int ii = 0;
3426 i64 nTotal = 0;
3427 int nWidth = 0;
3428 eqp_reset(pArg);
3430 for(ii=0; 1; ii++){
3431 const char *z = 0;
3432 int n = 0;
3433 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
3434 break;
3436 n = (int)strlen(z) + scanStatsHeight(p, ii)*3;
3437 if( n>nWidth ) nWidth = n;
3439 nWidth += 4;
3441 sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
3442 for(ii=0; 1; ii++){
3443 i64 nLoop = 0;
3444 i64 nRow = 0;
3445 i64 nCycle = 0;
3446 int iId = 0;
3447 int iPid = 0;
3448 const char *zo = 0;
3449 const char *zName = 0;
3450 char *zText = 0;
3451 double rEst = 0.0;
3453 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
3454 break;
3456 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
3457 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
3458 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
3459 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
3460 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
3461 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
3462 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
3464 zText = sqlite3_mprintf("%s", zo);
3465 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
3466 char *z = 0;
3467 if( nCycle>=0 && nTotal>0 ){
3468 z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
3469 nCycle, ((nCycle*100)+nTotal/2) / nTotal
3472 if( nLoop>=0 ){
3473 z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop);
3475 if( nRow>=0 ){
3476 z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow);
3479 if( zName && pArg->scanstatsOn>1 ){
3480 double rpl = (double)nRow / (double)nLoop;
3481 z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst);
3484 zText = sqlite3_mprintf(
3485 "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z
3489 eqp_append(pArg, iId, iPid, zText);
3490 sqlite3_free(zText);
3493 eqp_render(pArg, nTotal);
3495 #endif
3499 ** Parameter azArray points to a zero-terminated array of strings. zStr
3500 ** points to a single nul-terminated string. Return non-zero if zStr
3501 ** is equal, according to strcmp(), to any of the strings in the array.
3502 ** Otherwise, return zero.
3504 static int str_in_array(const char *zStr, const char **azArray){
3505 int i;
3506 for(i=0; azArray[i]; i++){
3507 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3509 return 0;
3513 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3514 ** and populate the ShellState.aiIndent[] array with the number of
3515 ** spaces each opcode should be indented before it is output.
3517 ** The indenting rules are:
3519 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3520 ** all opcodes that occur between the p2 jump destination and the opcode
3521 ** itself by 2 spaces.
3523 ** * Do the previous for "Return" instructions for when P2 is positive.
3524 ** See tag-20220407a in wherecode.c and vdbe.c.
3526 ** * For each "Goto", if the jump destination is earlier in the program
3527 ** and ends on one of:
3528 ** Yield SeekGt SeekLt RowSetRead Rewind
3529 ** or if the P1 parameter is one instead of zero,
3530 ** then indent all opcodes between the earlier instruction
3531 ** and "Goto" by 2 spaces.
3533 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3534 int *abYield = 0; /* True if op is an OP_Yield */
3535 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
3536 int iOp; /* Index of operation in p->aiIndent[] */
3538 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3539 "Return", 0 };
3540 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3541 "Rewind", 0 };
3542 const char *azGoto[] = { "Goto", 0 };
3544 /* The caller guarantees that the leftmost 4 columns of the statement
3545 ** passed to this function are equivalent to the leftmost 4 columns
3546 ** of EXPLAIN statement output. In practice the statement may be
3547 ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
3548 assert( sqlite3_column_count(pSql)>=4 );
3549 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) );
3550 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) );
3551 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) );
3552 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) );
3554 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3555 int i;
3556 int iAddr = sqlite3_column_int(pSql, 0);
3557 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3558 int p1 = sqlite3_column_int(pSql, 2);
3559 int p2 = sqlite3_column_int(pSql, 3);
3561 /* Assuming that p2 is an instruction address, set variable p2op to the
3562 ** index of that instruction in the aiIndent[] array. p2 and p2op may be
3563 ** different if the current instruction is part of a sub-program generated
3564 ** by an SQL trigger or foreign key. */
3565 int p2op = (p2 + (iOp-iAddr));
3567 /* Grow the p->aiIndent array as required */
3568 if( iOp>=nAlloc ){
3569 nAlloc += 100;
3570 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3571 shell_check_oom(p->aiIndent);
3572 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3573 shell_check_oom(abYield);
3576 abYield[iOp] = str_in_array(zOp, azYield);
3577 p->aiIndent[iOp] = 0;
3578 p->nIndent = iOp+1;
3579 if( str_in_array(zOp, azNext) && p2op>0 ){
3580 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3582 if( str_in_array(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
3583 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3587 p->iIndent = 0;
3588 sqlite3_free(abYield);
3589 sqlite3_reset(pSql);
3593 ** Free the array allocated by explain_data_prepare().
3595 static void explain_data_delete(ShellState *p){
3596 sqlite3_free(p->aiIndent);
3597 p->aiIndent = 0;
3598 p->nIndent = 0;
3599 p->iIndent = 0;
3602 static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
3605 ** Display scan stats.
3607 static void display_scanstats(
3608 sqlite3 *db, /* Database to query */
3609 ShellState *pArg /* Pointer to ShellState */
3611 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3612 UNUSED_PARAMETER(db);
3613 UNUSED_PARAMETER(pArg);
3614 #else
3615 if( pArg->scanstatsOn==3 ){
3616 const char *zSql =
3617 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
3618 " format('% 6s (%.2f%%)',"
3619 " CASE WHEN ncycle<100_000 THEN ncycle || ' '"
3620 " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'"
3621 " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'"
3622 " ELSE (ncycle/1000_000_000) || 'G' END,"
3623 " ncycle*100.0/(sum(ncycle) OVER ())"
3624 " ) AS cycles"
3625 " FROM bytecode(?)";
3627 int rc = SQLITE_OK;
3628 sqlite3_stmt *pStmt = 0;
3629 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3630 if( rc==SQLITE_OK ){
3631 sqlite3_stmt *pSave = pArg->pStmt;
3632 pArg->pStmt = pStmt;
3633 sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
3635 pArg->cnt = 0;
3636 pArg->cMode = MODE_ScanExp;
3637 explain_data_prepare(pArg, pStmt);
3638 exec_prepared_stmt(pArg, pStmt);
3639 explain_data_delete(pArg);
3641 sqlite3_finalize(pStmt);
3642 pArg->pStmt = pSave;
3644 }else{
3645 display_explain_scanstats(db, pArg);
3647 #endif
3651 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3653 static unsigned int savedSelectTrace;
3654 static unsigned int savedWhereTrace;
3655 static void disable_debug_trace_modes(void){
3656 unsigned int zero = 0;
3657 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3658 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3659 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3660 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3662 static void restore_debug_trace_modes(void){
3663 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3664 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3667 /* Create the TEMP table used to store parameter bindings */
3668 static void bind_table_init(ShellState *p){
3669 int wrSchema = 0;
3670 int defensiveMode = 0;
3671 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3672 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3673 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3674 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3675 sqlite3_exec(p->db,
3676 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3677 " key TEXT PRIMARY KEY,\n"
3678 " value\n"
3679 ") WITHOUT ROWID;",
3680 0, 0, 0);
3681 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3682 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3686 ** Bind parameters on a prepared statement.
3688 ** Parameter bindings are taken from a TEMP table of the form:
3690 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3691 ** WITHOUT ROWID;
3693 ** No bindings occur if this table does not exist. The name of the table
3694 ** begins with "sqlite_" so that it will not collide with ordinary application
3695 ** tables. The table must be in the TEMP schema.
3697 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3698 int nVar;
3699 int i;
3700 int rc;
3701 sqlite3_stmt *pQ = 0;
3703 nVar = sqlite3_bind_parameter_count(pStmt);
3704 if( nVar==0 ) return; /* Nothing to do */
3705 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3706 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3707 rc = SQLITE_NOTFOUND;
3708 pQ = 0;
3709 }else{
3710 rc = sqlite3_prepare_v2(pArg->db,
3711 "SELECT value FROM temp.sqlite_parameters"
3712 " WHERE key=?1", -1, &pQ, 0);
3714 for(i=1; i<=nVar; i++){
3715 char zNum[30];
3716 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3717 if( zVar==0 ){
3718 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3719 zVar = zNum;
3721 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3722 if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
3723 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3724 #ifdef NAN
3725 }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
3726 sqlite3_bind_double(pStmt, i, NAN);
3727 #endif
3728 #ifdef INFINITY
3729 }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
3730 sqlite3_bind_double(pStmt, i, INFINITY);
3731 #endif
3732 }else if( strncmp(zVar, "$int_", 5)==0 ){
3733 sqlite3_bind_int(pStmt, i, atoi(&zVar[5]));
3734 }else if( strncmp(zVar, "$text_", 6)==0 ){
3735 size_t szVar = strlen(zVar);
3736 char *zBuf = sqlite3_malloc64( szVar-5 );
3737 if( zBuf ){
3738 memcpy(zBuf, &zVar[6], szVar-5);
3739 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
3741 }else{
3742 sqlite3_bind_null(pStmt, i);
3744 sqlite3_reset(pQ);
3746 sqlite3_finalize(pQ);
3750 ** UTF8 box-drawing characters. Imagine box lines like this:
3752 ** 1
3753 ** |
3754 ** 4 --+-- 2
3755 ** |
3756 ** 3
3758 ** Each box characters has between 2 and 4 of the lines leading from
3759 ** the center. The characters are here identified by the numbers of
3760 ** their corresponding lines.
3762 #define BOX_24 "\342\224\200" /* U+2500 --- */
3763 #define BOX_13 "\342\224\202" /* U+2502 | */
3764 #define BOX_23 "\342\224\214" /* U+250c ,- */
3765 #define BOX_34 "\342\224\220" /* U+2510 -, */
3766 #define BOX_12 "\342\224\224" /* U+2514 '- */
3767 #define BOX_14 "\342\224\230" /* U+2518 -' */
3768 #define BOX_123 "\342\224\234" /* U+251c |- */
3769 #define BOX_134 "\342\224\244" /* U+2524 -| */
3770 #define BOX_234 "\342\224\254" /* U+252c -,- */
3771 #define BOX_124 "\342\224\264" /* U+2534 -'- */
3772 #define BOX_1234 "\342\224\274" /* U+253c -|- */
3774 /* Draw horizontal line N characters long using unicode box
3775 ** characters
3777 static void print_box_line(int N){
3778 const char zDash[] =
3779 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3780 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3781 const int nDash = sizeof(zDash) - 1;
3782 N *= 3;
3783 while( N>nDash ){
3784 oputz(zDash);
3785 N -= nDash;
3787 oputf("%.*s", N, zDash);
3791 ** Draw a horizontal separator for a MODE_Box table.
3793 static void print_box_row_separator(
3794 ShellState *p,
3795 int nArg,
3796 const char *zSep1,
3797 const char *zSep2,
3798 const char *zSep3
3800 int i;
3801 if( nArg>0 ){
3802 oputz(zSep1);
3803 print_box_line(p->actualWidth[0]+2);
3804 for(i=1; i<nArg; i++){
3805 oputz(zSep2);
3806 print_box_line(p->actualWidth[i]+2);
3808 oputz(zSep3);
3810 oputz("\n");
3814 ** z[] is a line of text that is to be displayed the .mode box or table or
3815 ** similar tabular formats. z[] might contain control characters such
3816 ** as \n, \t, \f, or \r.
3818 ** Compute characters to display on the first line of z[]. Stop at the
3819 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
3820 ** from malloc()) of that first line, which caller should free sometime.
3821 ** Write anything to display on the next line into *pzTail. If this is
3822 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3824 static char *translateForDisplayAndDup(
3825 const unsigned char *z, /* Input text to be transformed */
3826 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
3827 int mxWidth, /* Max width. 0 means no limit */
3828 u8 bWordWrap /* If true, avoid breaking mid-word */
3830 int i; /* Input bytes consumed */
3831 int j; /* Output bytes generated */
3832 int k; /* Input bytes to be displayed */
3833 int n; /* Output column number */
3834 unsigned char *zOut; /* Output text */
3836 if( z==0 ){
3837 *pzTail = 0;
3838 return 0;
3840 if( mxWidth<0 ) mxWidth = -mxWidth;
3841 if( mxWidth==0 ) mxWidth = 1000000;
3842 i = j = n = 0;
3843 while( n<mxWidth ){
3844 unsigned char c = z[i];
3845 if( c>=0xc0 ){
3846 int u;
3847 int len = decodeUtf8(&z[i], &u);
3848 i += len;
3849 j += len;
3850 n += cli_wcwidth(u);
3851 continue;
3853 if( c>=' ' ){
3854 n++;
3855 i++;
3856 j++;
3857 continue;
3859 if( c=='\t' ){
3861 n++;
3862 j++;
3863 }while( (n&7)!=0 && n<mxWidth );
3864 i++;
3865 continue;
3867 break;
3869 if( n>=mxWidth && bWordWrap ){
3870 /* Perhaps try to back up to a better place to break the line */
3871 for(k=i; k>i/2; k--){
3872 if( isspace(z[k-1]) ) break;
3874 if( k<=i/2 ){
3875 for(k=i; k>i/2; k--){
3876 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3879 if( k<=i/2 ){
3880 k = i;
3881 }else{
3882 i = k;
3883 while( z[i]==' ' ) i++;
3885 }else{
3886 k = i;
3888 if( n>=mxWidth && z[i]>=' ' ){
3889 *pzTail = &z[i];
3890 }else if( z[i]=='\r' && z[i+1]=='\n' ){
3891 *pzTail = z[i+2] ? &z[i+2] : 0;
3892 }else if( z[i]==0 || z[i+1]==0 ){
3893 *pzTail = 0;
3894 }else{
3895 *pzTail = &z[i+1];
3897 zOut = malloc( j+1 );
3898 shell_check_oom(zOut);
3899 i = j = n = 0;
3900 while( i<k ){
3901 unsigned char c = z[i];
3902 if( c>=0xc0 ){
3903 int u;
3904 int len = decodeUtf8(&z[i], &u);
3905 do{ zOut[j++] = z[i++]; }while( (--len)>0 );
3906 n += cli_wcwidth(u);
3907 continue;
3909 if( c>=' ' ){
3910 n++;
3911 zOut[j++] = z[i++];
3912 continue;
3914 if( z[i]=='\t' ){
3916 n++;
3917 zOut[j++] = ' ';
3918 }while( (n&7)!=0 && n<mxWidth );
3919 i++;
3920 continue;
3922 break;
3924 zOut[j] = 0;
3925 return (char*)zOut;
3928 /* Extract the value of the i-th current column for pStmt as an SQL literal
3929 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
3930 ** the caller.
3932 static char *quoted_column(sqlite3_stmt *pStmt, int i){
3933 switch( sqlite3_column_type(pStmt, i) ){
3934 case SQLITE_NULL: {
3935 return sqlite3_mprintf("NULL");
3937 case SQLITE_INTEGER:
3938 case SQLITE_FLOAT: {
3939 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3941 case SQLITE_TEXT: {
3942 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3944 case SQLITE_BLOB: {
3945 int j;
3946 sqlite3_str *pStr = sqlite3_str_new(0);
3947 const unsigned char *a = sqlite3_column_blob(pStmt,i);
3948 int n = sqlite3_column_bytes(pStmt,i);
3949 sqlite3_str_append(pStr, "x'", 2);
3950 for(j=0; j<n; j++){
3951 sqlite3_str_appendf(pStr, "%02x", a[j]);
3953 sqlite3_str_append(pStr, "'", 1);
3954 return sqlite3_str_finish(pStr);
3957 return 0; /* Not reached */
3961 ** Run a prepared statement and output the result in one of the
3962 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3963 ** or MODE_Box.
3965 ** This is different from ordinary exec_prepared_stmt() in that
3966 ** it has to run the entire query and gather the results into memory
3967 ** first, in order to determine column widths, before providing
3968 ** any output.
3970 static void exec_prepared_stmt_columnar(
3971 ShellState *p, /* Pointer to ShellState */
3972 sqlite3_stmt *pStmt /* Statement to run */
3974 sqlite3_int64 nRow = 0;
3975 int nColumn = 0;
3976 char **azData = 0;
3977 sqlite3_int64 nAlloc = 0;
3978 char *abRowDiv = 0;
3979 const unsigned char *uz;
3980 const char *z;
3981 char **azQuoted = 0;
3982 int rc;
3983 sqlite3_int64 i, nData;
3984 int j, nTotal, w, n;
3985 const char *colSep = 0;
3986 const char *rowSep = 0;
3987 const unsigned char **azNextLine = 0;
3988 int bNextLine = 0;
3989 int bMultiLineRowExists = 0;
3990 int bw = p->cmOpts.bWordWrap;
3991 const char *zEmpty = "";
3992 const char *zShowNull = p->nullValue;
3994 rc = sqlite3_step(pStmt);
3995 if( rc!=SQLITE_ROW ) return;
3996 nColumn = sqlite3_column_count(pStmt);
3997 if( nColumn==0 ) goto columnar_end;
3998 nAlloc = nColumn*4;
3999 if( nAlloc<=0 ) nAlloc = 1;
4000 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
4001 shell_check_oom(azData);
4002 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
4003 shell_check_oom(azNextLine);
4004 memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
4005 if( p->cmOpts.bQuote ){
4006 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
4007 shell_check_oom(azQuoted);
4008 memset(azQuoted, 0, nColumn*sizeof(char*) );
4010 abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
4011 shell_check_oom(abRowDiv);
4012 if( nColumn>p->nWidth ){
4013 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
4014 shell_check_oom(p->colWidth);
4015 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
4016 p->nWidth = nColumn;
4017 p->actualWidth = &p->colWidth[nColumn];
4019 memset(p->actualWidth, 0, nColumn*sizeof(int));
4020 for(i=0; i<nColumn; i++){
4021 w = p->colWidth[i];
4022 if( w<0 ) w = -w;
4023 p->actualWidth[i] = w;
4025 for(i=0; i<nColumn; i++){
4026 const unsigned char *zNotUsed;
4027 int wx = p->colWidth[i];
4028 if( wx==0 ){
4029 wx = p->cmOpts.iWrap;
4031 if( wx<0 ) wx = -wx;
4032 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
4033 if( uz==0 ) uz = (u8*)"";
4034 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
4037 int useNextLine = bNextLine;
4038 bNextLine = 0;
4039 if( (nRow+2)*nColumn >= nAlloc ){
4040 nAlloc *= 2;
4041 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
4042 shell_check_oom(azData);
4043 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
4044 shell_check_oom(abRowDiv);
4046 abRowDiv[nRow] = 1;
4047 nRow++;
4048 for(i=0; i<nColumn; i++){
4049 int wx = p->colWidth[i];
4050 if( wx==0 ){
4051 wx = p->cmOpts.iWrap;
4053 if( wx<0 ) wx = -wx;
4054 if( useNextLine ){
4055 uz = azNextLine[i];
4056 if( uz==0 ) uz = (u8*)zEmpty;
4057 }else if( p->cmOpts.bQuote ){
4058 sqlite3_free(azQuoted[i]);
4059 azQuoted[i] = quoted_column(pStmt,i);
4060 uz = (const unsigned char*)azQuoted[i];
4061 }else{
4062 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
4063 if( uz==0 ) uz = (u8*)zShowNull;
4065 azData[nRow*nColumn + i]
4066 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
4067 if( azNextLine[i] ){
4068 bNextLine = 1;
4069 abRowDiv[nRow-1] = 0;
4070 bMultiLineRowExists = 1;
4073 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
4074 nTotal = nColumn*(nRow+1);
4075 for(i=0; i<nTotal; i++){
4076 z = azData[i];
4077 if( z==0 ) z = (char*)zEmpty;
4078 n = strlenChar(z);
4079 j = i%nColumn;
4080 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
4082 if( seenInterrupt ) goto columnar_end;
4083 switch( p->cMode ){
4084 case MODE_Column: {
4085 colSep = " ";
4086 rowSep = "\n";
4087 if( p->showHeader ){
4088 for(i=0; i<nColumn; i++){
4089 w = p->actualWidth[i];
4090 if( p->colWidth[i]<0 ) w = -w;
4091 utf8_width_print(w, azData[i]);
4092 fputs(i==nColumn-1?"\n":" ", p->out);
4094 for(i=0; i<nColumn; i++){
4095 print_dashes(p->actualWidth[i]);
4096 fputs(i==nColumn-1?"\n":" ", p->out);
4099 break;
4101 case MODE_Table: {
4102 colSep = " | ";
4103 rowSep = " |\n";
4104 print_row_separator(p, nColumn, "+");
4105 fputs("| ", p->out);
4106 for(i=0; i<nColumn; i++){
4107 w = p->actualWidth[i];
4108 n = strlenChar(azData[i]);
4109 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
4110 oputz(i==nColumn-1?" |\n":" | ");
4112 print_row_separator(p, nColumn, "+");
4113 break;
4115 case MODE_Markdown: {
4116 colSep = " | ";
4117 rowSep = " |\n";
4118 fputs("| ", p->out);
4119 for(i=0; i<nColumn; i++){
4120 w = p->actualWidth[i];
4121 n = strlenChar(azData[i]);
4122 oputf("%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
4123 oputz(i==nColumn-1?" |\n":" | ");
4125 print_row_separator(p, nColumn, "|");
4126 break;
4128 case MODE_Box: {
4129 colSep = " " BOX_13 " ";
4130 rowSep = " " BOX_13 "\n";
4131 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
4132 oputz(BOX_13 " ");
4133 for(i=0; i<nColumn; i++){
4134 w = p->actualWidth[i];
4135 n = strlenChar(azData[i]);
4136 oputf("%*s%s%*s%s",
4137 (w-n)/2, "", azData[i], (w-n+1)/2, "",
4138 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
4140 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
4141 break;
4144 for(i=nColumn, j=0; i<nTotal; i++, j++){
4145 if( j==0 && p->cMode!=MODE_Column ){
4146 oputz(p->cMode==MODE_Box?BOX_13" ":"| ");
4148 z = azData[i];
4149 if( z==0 ) z = p->nullValue;
4150 w = p->actualWidth[j];
4151 if( p->colWidth[j]<0 ) w = -w;
4152 utf8_width_print(w, z);
4153 if( j==nColumn-1 ){
4154 oputz(rowSep);
4155 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
4156 if( p->cMode==MODE_Table ){
4157 print_row_separator(p, nColumn, "+");
4158 }else if( p->cMode==MODE_Box ){
4159 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
4160 }else if( p->cMode==MODE_Column ){
4161 oputz("\n");
4164 j = -1;
4165 if( seenInterrupt ) goto columnar_end;
4166 }else{
4167 oputz(colSep);
4170 if( p->cMode==MODE_Table ){
4171 print_row_separator(p, nColumn, "+");
4172 }else if( p->cMode==MODE_Box ){
4173 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
4175 columnar_end:
4176 if( seenInterrupt ){
4177 oputz("Interrupt\n");
4179 nData = (nRow+1)*nColumn;
4180 for(i=0; i<nData; i++){
4181 z = azData[i];
4182 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
4184 sqlite3_free(azData);
4185 sqlite3_free((void*)azNextLine);
4186 sqlite3_free(abRowDiv);
4187 if( azQuoted ){
4188 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
4189 sqlite3_free(azQuoted);
4194 ** Run a prepared statement
4196 static void exec_prepared_stmt(
4197 ShellState *pArg, /* Pointer to ShellState */
4198 sqlite3_stmt *pStmt /* Statement to run */
4200 int rc;
4201 sqlite3_uint64 nRow = 0;
4203 if( pArg->cMode==MODE_Column
4204 || pArg->cMode==MODE_Table
4205 || pArg->cMode==MODE_Box
4206 || pArg->cMode==MODE_Markdown
4208 exec_prepared_stmt_columnar(pArg, pStmt);
4209 return;
4212 /* perform the first step. this will tell us if we
4213 ** have a result set or not and how wide it is.
4215 rc = sqlite3_step(pStmt);
4216 /* if we have a result set... */
4217 if( SQLITE_ROW == rc ){
4218 /* allocate space for col name ptr, value ptr, and type */
4219 int nCol = sqlite3_column_count(pStmt);
4220 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
4221 if( !pData ){
4222 shell_out_of_memory();
4223 }else{
4224 char **azCols = (char **)pData; /* Names of result columns */
4225 char **azVals = &azCols[nCol]; /* Results */
4226 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
4227 int i, x;
4228 assert(sizeof(int) <= sizeof(char *));
4229 /* save off ptrs to column names */
4230 for(i=0; i<nCol; i++){
4231 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
4234 nRow++;
4235 /* extract the data and data types */
4236 for(i=0; i<nCol; i++){
4237 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
4238 if( x==SQLITE_BLOB
4239 && pArg
4240 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
4242 azVals[i] = "";
4243 }else{
4244 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
4246 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
4247 rc = SQLITE_NOMEM;
4248 break; /* from for */
4250 } /* end for */
4252 /* if data and types extracted successfully... */
4253 if( SQLITE_ROW == rc ){
4254 /* call the supplied callback with the result row data */
4255 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
4256 rc = SQLITE_ABORT;
4257 }else{
4258 rc = sqlite3_step(pStmt);
4261 } while( SQLITE_ROW == rc );
4262 sqlite3_free(pData);
4263 if( pArg->cMode==MODE_Json ){
4264 fputs("]\n", pArg->out);
4265 }else if( pArg->cMode==MODE_Count ){
4266 char zBuf[200];
4267 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
4268 nRow, nRow!=1 ? "s" : "");
4269 printf("%s", zBuf);
4275 #ifndef SQLITE_OMIT_VIRTUALTABLE
4277 ** This function is called to process SQL if the previous shell command
4278 ** was ".expert". It passes the SQL in the second argument directly to
4279 ** the sqlite3expert object.
4281 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4282 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4283 ** an English language error message. It is the responsibility of the
4284 ** caller to eventually free this buffer using sqlite3_free().
4286 static int expertHandleSQL(
4287 ShellState *pState,
4288 const char *zSql,
4289 char **pzErr
4291 assert( pState->expert.pExpert );
4292 assert( pzErr==0 || *pzErr==0 );
4293 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
4297 ** This function is called either to silently clean up the object
4298 ** created by the ".expert" command (if bCancel==1), or to generate a
4299 ** report from it and then clean it up (if bCancel==0).
4301 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4302 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4303 ** an English language error message. It is the responsibility of the
4304 ** caller to eventually free this buffer using sqlite3_free().
4306 static int expertFinish(
4307 ShellState *pState,
4308 int bCancel,
4309 char **pzErr
4311 int rc = SQLITE_OK;
4312 sqlite3expert *p = pState->expert.pExpert;
4313 assert( p );
4314 assert( bCancel || pzErr==0 || *pzErr==0 );
4315 if( bCancel==0 ){
4316 int bVerbose = pState->expert.bVerbose;
4318 rc = sqlite3_expert_analyze(p, pzErr);
4319 if( rc==SQLITE_OK ){
4320 int nQuery = sqlite3_expert_count(p);
4321 int i;
4323 if( bVerbose ){
4324 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
4325 oputz("-- Candidates -----------------------------\n");
4326 oputf("%s\n", zCand);
4328 for(i=0; i<nQuery; i++){
4329 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
4330 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
4331 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
4332 if( zIdx==0 ) zIdx = "(no new indexes)\n";
4333 if( bVerbose ){
4334 oputf("-- Query %d --------------------------------\n",i+1);
4335 oputf("%s\n\n", zSql);
4337 oputf("%s\n", zIdx);
4338 oputf("%s\n", zEQP);
4342 sqlite3_expert_destroy(p);
4343 pState->expert.pExpert = 0;
4344 return rc;
4348 ** Implementation of ".expert" dot command.
4350 static int expertDotCommand(
4351 ShellState *pState, /* Current shell tool state */
4352 char **azArg, /* Array of arguments passed to dot command */
4353 int nArg /* Number of entries in azArg[] */
4355 int rc = SQLITE_OK;
4356 char *zErr = 0;
4357 int i;
4358 int iSample = 0;
4360 assert( pState->expert.pExpert==0 );
4361 memset(&pState->expert, 0, sizeof(ExpertInfo));
4363 for(i=1; rc==SQLITE_OK && i<nArg; i++){
4364 char *z = azArg[i];
4365 int n;
4366 if( z[0]=='-' && z[1]=='-' ) z++;
4367 n = strlen30(z);
4368 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
4369 pState->expert.bVerbose = 1;
4371 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
4372 if( i==(nArg-1) ){
4373 eputf("option requires an argument: %s\n", z);
4374 rc = SQLITE_ERROR;
4375 }else{
4376 iSample = (int)integerValue(azArg[++i]);
4377 if( iSample<0 || iSample>100 ){
4378 eputf("value out of range: %s\n", azArg[i]);
4379 rc = SQLITE_ERROR;
4383 else{
4384 eputf("unknown option: %s\n", z);
4385 rc = SQLITE_ERROR;
4389 if( rc==SQLITE_OK ){
4390 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
4391 if( pState->expert.pExpert==0 ){
4392 eputf("sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
4393 rc = SQLITE_ERROR;
4394 }else{
4395 sqlite3_expert_config(
4396 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
4400 sqlite3_free(zErr);
4402 return rc;
4404 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
4407 ** Execute a statement or set of statements. Print
4408 ** any result rows/columns depending on the current mode
4409 ** set via the supplied callback.
4411 ** This is very similar to SQLite's built-in sqlite3_exec()
4412 ** function except it takes a slightly different callback
4413 ** and callback data argument.
4415 static int shell_exec(
4416 ShellState *pArg, /* Pointer to ShellState */
4417 const char *zSql, /* SQL to be evaluated */
4418 char **pzErrMsg /* Error msg written here */
4420 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
4421 int rc = SQLITE_OK; /* Return Code */
4422 int rc2;
4423 const char *zLeftover; /* Tail of unprocessed SQL */
4424 sqlite3 *db = pArg->db;
4426 if( pzErrMsg ){
4427 *pzErrMsg = NULL;
4430 #ifndef SQLITE_OMIT_VIRTUALTABLE
4431 if( pArg->expert.pExpert ){
4432 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
4433 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
4435 #endif
4437 while( zSql[0] && (SQLITE_OK == rc) ){
4438 static const char *zStmtSql;
4439 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
4440 if( SQLITE_OK != rc ){
4441 if( pzErrMsg ){
4442 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
4444 }else{
4445 if( !pStmt ){
4446 /* this happens for a comment or white-space */
4447 zSql = zLeftover;
4448 while( IsSpace(zSql[0]) ) zSql++;
4449 continue;
4451 zStmtSql = sqlite3_sql(pStmt);
4452 if( zStmtSql==0 ) zStmtSql = "";
4453 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
4455 /* save off the prepared statement handle and reset row count */
4456 if( pArg ){
4457 pArg->pStmt = pStmt;
4458 pArg->cnt = 0;
4461 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
4462 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
4463 sqlite3_stmt *pExplain;
4464 int triggerEQP = 0;
4465 disable_debug_trace_modes();
4466 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
4467 if( pArg->autoEQP>=AUTOEQP_trigger ){
4468 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
4470 pExplain = pStmt;
4471 sqlite3_reset(pExplain);
4472 rc = sqlite3_stmt_explain(pExplain, 2);
4473 if( rc==SQLITE_OK ){
4474 bind_prepared_stmt(pArg, pExplain);
4475 while( sqlite3_step(pExplain)==SQLITE_ROW ){
4476 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
4477 int iEqpId = sqlite3_column_int(pExplain, 0);
4478 int iParentId = sqlite3_column_int(pExplain, 1);
4479 if( zEQPLine==0 ) zEQPLine = "";
4480 if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
4481 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
4483 eqp_render(pArg, 0);
4485 if( pArg->autoEQP>=AUTOEQP_full ){
4486 /* Also do an EXPLAIN for ".eqp full" mode */
4487 sqlite3_reset(pExplain);
4488 rc = sqlite3_stmt_explain(pExplain, 1);
4489 if( rc==SQLITE_OK ){
4490 pArg->cMode = MODE_Explain;
4491 assert( sqlite3_stmt_isexplain(pExplain)==1 );
4492 bind_prepared_stmt(pArg, pExplain);
4493 explain_data_prepare(pArg, pExplain);
4494 exec_prepared_stmt(pArg, pExplain);
4495 explain_data_delete(pArg);
4498 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
4499 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
4501 sqlite3_reset(pStmt);
4502 sqlite3_stmt_explain(pStmt, 0);
4503 restore_debug_trace_modes();
4506 if( pArg ){
4507 int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
4508 pArg->cMode = pArg->mode;
4509 if( pArg->autoExplain ){
4510 if( bIsExplain ){
4511 pArg->cMode = MODE_Explain;
4513 if( sqlite3_stmt_isexplain(pStmt)==2 ){
4514 pArg->cMode = MODE_EQP;
4518 /* If the shell is currently in ".explain" mode, gather the extra
4519 ** data required to add indents to the output.*/
4520 if( pArg->cMode==MODE_Explain && bIsExplain ){
4521 explain_data_prepare(pArg, pStmt);
4525 bind_prepared_stmt(pArg, pStmt);
4526 exec_prepared_stmt(pArg, pStmt);
4527 explain_data_delete(pArg);
4528 eqp_render(pArg, 0);
4530 /* print usage stats if stats on */
4531 if( pArg && pArg->statsOn ){
4532 display_stats(db, pArg, 0);
4535 /* print loop-counters if required */
4536 if( pArg && pArg->scanstatsOn ){
4537 display_scanstats(db, pArg);
4540 /* Finalize the statement just executed. If this fails, save a
4541 ** copy of the error message. Otherwise, set zSql to point to the
4542 ** next statement to execute. */
4543 rc2 = sqlite3_finalize(pStmt);
4544 if( rc!=SQLITE_NOMEM ) rc = rc2;
4545 if( rc==SQLITE_OK ){
4546 zSql = zLeftover;
4547 while( IsSpace(zSql[0]) ) zSql++;
4548 }else if( pzErrMsg ){
4549 *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
4552 /* clear saved stmt handle */
4553 if( pArg ){
4554 pArg->pStmt = NULL;
4557 } /* end while */
4559 return rc;
4563 ** Release memory previously allocated by tableColumnList().
4565 static void freeColumnList(char **azCol){
4566 int i;
4567 for(i=1; azCol[i]; i++){
4568 sqlite3_free(azCol[i]);
4570 /* azCol[0] is a static string */
4571 sqlite3_free(azCol);
4575 ** Return a list of pointers to strings which are the names of all
4576 ** columns in table zTab. The memory to hold the names is dynamically
4577 ** allocated and must be released by the caller using a subsequent call
4578 ** to freeColumnList().
4580 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
4581 ** value that needs to be preserved, then azCol[0] is filled in with the
4582 ** name of the rowid column.
4584 ** The first regular column in the table is azCol[1]. The list is terminated
4585 ** by an entry with azCol[i]==0.
4587 static char **tableColumnList(ShellState *p, const char *zTab){
4588 char **azCol = 0;
4589 sqlite3_stmt *pStmt;
4590 char *zSql;
4591 int nCol = 0;
4592 int nAlloc = 0;
4593 int nPK = 0; /* Number of PRIMARY KEY columns seen */
4594 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
4595 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4596 int rc;
4598 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4599 shell_check_oom(zSql);
4600 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4601 sqlite3_free(zSql);
4602 if( rc ) return 0;
4603 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4604 if( nCol>=nAlloc-2 ){
4605 nAlloc = nAlloc*2 + nCol + 10;
4606 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4607 shell_check_oom(azCol);
4609 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4610 shell_check_oom(azCol[nCol]);
4611 if( sqlite3_column_int(pStmt, 5) ){
4612 nPK++;
4613 if( nPK==1
4614 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4615 "INTEGER")==0
4617 isIPK = 1;
4618 }else{
4619 isIPK = 0;
4623 sqlite3_finalize(pStmt);
4624 if( azCol==0 ) return 0;
4625 azCol[0] = 0;
4626 azCol[nCol+1] = 0;
4628 /* The decision of whether or not a rowid really needs to be preserved
4629 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
4630 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
4631 ** rowids on tables where the rowid is inaccessible because there are other
4632 ** columns in the table named "rowid", "_rowid_", and "oid".
4634 if( preserveRowid && isIPK ){
4635 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4636 ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID
4637 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4638 ** ROWID aliases. To distinguish these cases, check to see if
4639 ** there is a "pk" entry in "PRAGMA index_list". There will be
4640 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4642 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4643 " WHERE origin='pk'", zTab);
4644 shell_check_oom(zSql);
4645 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4646 sqlite3_free(zSql);
4647 if( rc ){
4648 freeColumnList(azCol);
4649 return 0;
4651 rc = sqlite3_step(pStmt);
4652 sqlite3_finalize(pStmt);
4653 preserveRowid = rc==SQLITE_ROW;
4655 if( preserveRowid ){
4656 /* Only preserve the rowid if we can find a name to use for the
4657 ** rowid */
4658 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4659 int i, j;
4660 for(j=0; j<3; j++){
4661 for(i=1; i<=nCol; i++){
4662 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4664 if( i>nCol ){
4665 /* At this point, we know that azRowid[j] is not the name of any
4666 ** ordinary column in the table. Verify that azRowid[j] is a valid
4667 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
4668 ** tables will fail this last check */
4669 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4670 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4671 break;
4675 return azCol;
4679 ** Toggle the reverse_unordered_selects setting.
4681 static void toggleSelectOrder(sqlite3 *db){
4682 sqlite3_stmt *pStmt = 0;
4683 int iSetting = 0;
4684 char zStmt[100];
4685 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4686 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4687 iSetting = sqlite3_column_int(pStmt, 0);
4689 sqlite3_finalize(pStmt);
4690 sqlite3_snprintf(sizeof(zStmt), zStmt,
4691 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4692 sqlite3_exec(db, zStmt, 0, 0, 0);
4696 ** This is a different callback routine used for dumping the database.
4697 ** Each row received by this callback consists of a table name,
4698 ** the table type ("index" or "table") and SQL to create the table.
4699 ** This routine should print text sufficient to recreate the table.
4701 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4702 int rc;
4703 const char *zTable;
4704 const char *zType;
4705 const char *zSql;
4706 ShellState *p = (ShellState *)pArg;
4707 int dataOnly;
4708 int noSys;
4710 UNUSED_PARAMETER(azNotUsed);
4711 if( nArg!=3 || azArg==0 ) return 0;
4712 zTable = azArg[0];
4713 zType = azArg[1];
4714 zSql = azArg[2];
4715 if( zTable==0 ) return 0;
4716 if( zType==0 ) return 0;
4717 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4718 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4720 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4721 if( !dataOnly ) oputz("DELETE FROM sqlite_sequence;\n");
4722 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4723 if( !dataOnly ) oputz("ANALYZE sqlite_schema;\n");
4724 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4725 return 0;
4726 }else if( dataOnly ){
4727 /* no-op */
4728 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4729 char *zIns;
4730 if( !p->writableSchema ){
4731 oputz("PRAGMA writable_schema=ON;\n");
4732 p->writableSchema = 1;
4734 zIns = sqlite3_mprintf(
4735 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4736 "VALUES('table','%q','%q',0,'%q');",
4737 zTable, zTable, zSql);
4738 shell_check_oom(zIns);
4739 oputf("%s\n", zIns);
4740 sqlite3_free(zIns);
4741 return 0;
4742 }else{
4743 printSchemaLine(zSql, ";\n");
4746 if( cli_strcmp(zType, "table")==0 ){
4747 ShellText sSelect;
4748 ShellText sTable;
4749 char **azCol;
4750 int i;
4751 char *savedDestTable;
4752 int savedMode;
4754 azCol = tableColumnList(p, zTable);
4755 if( azCol==0 ){
4756 p->nErr++;
4757 return 0;
4760 /* Always quote the table name, even if it appears to be pure ascii,
4761 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
4762 initText(&sTable);
4763 appendText(&sTable, zTable, quoteChar(zTable));
4764 /* If preserving the rowid, add a column list after the table name.
4765 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4766 ** instead of the usual "INSERT INTO tab VALUES(...)".
4768 if( azCol[0] ){
4769 appendText(&sTable, "(", 0);
4770 appendText(&sTable, azCol[0], 0);
4771 for(i=1; azCol[i]; i++){
4772 appendText(&sTable, ",", 0);
4773 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4775 appendText(&sTable, ")", 0);
4778 /* Build an appropriate SELECT statement */
4779 initText(&sSelect);
4780 appendText(&sSelect, "SELECT ", 0);
4781 if( azCol[0] ){
4782 appendText(&sSelect, azCol[0], 0);
4783 appendText(&sSelect, ",", 0);
4785 for(i=1; azCol[i]; i++){
4786 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4787 if( azCol[i+1] ){
4788 appendText(&sSelect, ",", 0);
4791 freeColumnList(azCol);
4792 appendText(&sSelect, " FROM ", 0);
4793 appendText(&sSelect, zTable, quoteChar(zTable));
4795 savedDestTable = p->zDestTable;
4796 savedMode = p->mode;
4797 p->zDestTable = sTable.z;
4798 p->mode = p->cMode = MODE_Insert;
4799 rc = shell_exec(p, sSelect.z, 0);
4800 if( (rc&0xff)==SQLITE_CORRUPT ){
4801 oputz("/****** CORRUPTION ERROR *******/\n");
4802 toggleSelectOrder(p->db);
4803 shell_exec(p, sSelect.z, 0);
4804 toggleSelectOrder(p->db);
4806 p->zDestTable = savedDestTable;
4807 p->mode = savedMode;
4808 freeText(&sTable);
4809 freeText(&sSelect);
4810 if( rc ) p->nErr++;
4812 return 0;
4816 ** Run zQuery. Use dump_callback() as the callback routine so that
4817 ** the contents of the query are output as SQL statements.
4819 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
4820 ** "ORDER BY rowid DESC" to the end.
4822 static int run_schema_dump_query(
4823 ShellState *p,
4824 const char *zQuery
4826 int rc;
4827 char *zErr = 0;
4828 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4829 if( rc==SQLITE_CORRUPT ){
4830 char *zQ2;
4831 int len = strlen30(zQuery);
4832 oputz("/****** CORRUPTION ERROR *******/\n");
4833 if( zErr ){
4834 oputf("/****** %s ******/\n", zErr);
4835 sqlite3_free(zErr);
4836 zErr = 0;
4838 zQ2 = malloc( len+100 );
4839 if( zQ2==0 ) return rc;
4840 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4841 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4842 if( rc ){
4843 oputf("/****** ERROR: %s ******/\n", zErr);
4844 }else{
4845 rc = SQLITE_CORRUPT;
4847 sqlite3_free(zErr);
4848 free(zQ2);
4850 return rc;
4854 ** Text of help messages.
4856 ** The help text for each individual command begins with a line that starts
4857 ** with ".". Subsequent lines are supplemental information.
4859 ** There must be two or more spaces between the end of the command and the
4860 ** start of the description of what that command does.
4862 static const char *(azHelp[]) = {
4863 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4864 && !defined(SQLITE_SHELL_FIDDLE)
4865 ".archive ... Manage SQL archives",
4866 " Each command must have exactly one of the following options:",
4867 " -c, --create Create a new archive",
4868 " -u, --update Add or update files with changed mtime",
4869 " -i, --insert Like -u but always add even if unchanged",
4870 " -r, --remove Remove files from archive",
4871 " -t, --list List contents of archive",
4872 " -x, --extract Extract files from archive",
4873 " Optional arguments:",
4874 " -v, --verbose Print each filename as it is processed",
4875 " -f FILE, --file FILE Use archive FILE (default is current db)",
4876 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
4877 " -C DIR, --directory DIR Read/extract files from directory DIR",
4878 " -g, --glob Use glob matching for names in archive",
4879 " -n, --dryrun Show the SQL that would have occurred",
4880 " Examples:",
4881 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
4882 " .ar -tf ARCHIVE # List members of ARCHIVE",
4883 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
4884 " See also:",
4885 " http://sqlite.org/cli.html#sqlite_archive_support",
4886 #endif
4887 #ifndef SQLITE_OMIT_AUTHORIZATION
4888 ".auth ON|OFF Show authorizer callbacks",
4889 #endif
4890 #ifndef SQLITE_SHELL_FIDDLE
4891 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
4892 " Options:",
4893 " --append Use the appendvfs",
4894 " --async Write to FILE without journal and fsync()",
4895 #endif
4896 ".bail on|off Stop after hitting an error. Default OFF",
4897 #ifndef SQLITE_SHELL_FIDDLE
4898 ".cd DIRECTORY Change the working directory to DIRECTORY",
4899 #endif
4900 ".changes on|off Show number of rows changed by SQL",
4901 #ifndef SQLITE_SHELL_FIDDLE
4902 ".check GLOB Fail if output since .testcase does not match",
4903 ".clone NEWDB Clone data into NEWDB from the existing database",
4904 #endif
4905 ".connection [close] [#] Open or close an auxiliary database connection",
4906 #if defined(_WIN32) || defined(WIN32)
4907 ".crnl on|off Translate \\n to \\r\\n. Default ON",
4908 #endif
4909 ".databases List names and files of attached databases",
4910 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
4911 #if SQLITE_SHELL_HAVE_RECOVER
4912 ".dbinfo ?DB? Show status information about the database",
4913 #endif
4914 ".dump ?OBJECTS? Render database content as SQL",
4915 " Options:",
4916 " --data-only Output only INSERT statements",
4917 " --newlines Allow unescaped newline characters in output",
4918 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
4919 " --preserve-rowids Include ROWID values in the output",
4920 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4921 " Additional LIKE patterns can be given in subsequent arguments",
4922 ".echo on|off Turn command echo on or off",
4923 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
4924 " Other Modes:",
4925 #ifdef SQLITE_DEBUG
4926 " test Show raw EXPLAIN QUERY PLAN output",
4927 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4928 #endif
4929 " trigger Like \"full\" but also show trigger bytecode",
4930 #ifndef SQLITE_SHELL_FIDDLE
4931 ".excel Display the output of next command in spreadsheet",
4932 " --bom Put a UTF8 byte-order mark on intermediate file",
4933 #endif
4934 #ifndef SQLITE_SHELL_FIDDLE
4935 ".exit ?CODE? Exit this program with return-code CODE",
4936 #endif
4937 ".expert EXPERIMENTAL. Suggest indexes for queries",
4938 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
4939 ".filectrl CMD ... Run various sqlite3_file_control() operations",
4940 " --schema SCHEMA Use SCHEMA instead of \"main\"",
4941 " --help Show CMD details",
4942 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
4943 ".headers on|off Turn display of headers on or off",
4944 ".help ?-all? ?PATTERN? Show help text for PATTERN",
4945 #ifndef SQLITE_SHELL_FIDDLE
4946 ".import FILE TABLE Import data from FILE into TABLE",
4947 " Options:",
4948 " --ascii Use \\037 and \\036 as column and row separators",
4949 " --csv Use , and \\n as column and row separators",
4950 " --skip N Skip the first N rows of input",
4951 " --schema S Target table to be S.TABLE",
4952 " -v \"Verbose\" - increase auxiliary output",
4953 " Notes:",
4954 " * If TABLE does not exist, it is created. The first row of input",
4955 " determines the column names.",
4956 " * If neither --csv or --ascii are used, the input mode is derived",
4957 " from the \".mode\" output mode",
4958 " * If FILE begins with \"|\" then it is a command that generates the",
4959 " input text.",
4960 #endif
4961 #ifndef SQLITE_OMIT_TEST_CONTROL
4962 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
4963 #endif
4964 ".indexes ?TABLE? Show names of indexes",
4965 " If TABLE is specified, only show indexes for",
4966 " tables matching TABLE using the LIKE operator.",
4967 ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
4968 #ifdef SQLITE_ENABLE_IOTRACE
4969 ",iotrace FILE Enable I/O diagnostic logging to FILE",
4970 #endif
4971 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
4972 ".lint OPTIONS Report potential schema issues.",
4973 " Options:",
4974 " fkey-indexes Find missing foreign key indexes",
4975 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4976 ".load FILE ?ENTRY? Load an extension library",
4977 #endif
4978 #if !defined(SQLITE_SHELL_FIDDLE)
4979 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
4980 #else
4981 ".log on|off Turn logging on or off.",
4982 #endif
4983 ".mode MODE ?OPTIONS? Set output mode",
4984 " MODE is one of:",
4985 " ascii Columns/rows delimited by 0x1F and 0x1E",
4986 " box Tables using unicode box-drawing characters",
4987 " csv Comma-separated values",
4988 " column Output in columns. (See .width)",
4989 " html HTML <table> code",
4990 " insert SQL insert statements for TABLE",
4991 " json Results in a JSON array",
4992 " line One value per line",
4993 " list Values delimited by \"|\"",
4994 " markdown Markdown table format",
4995 " qbox Shorthand for \"box --wrap 60 --quote\"",
4996 " quote Escape answers as for SQL",
4997 " table ASCII-art table",
4998 " tabs Tab-separated values",
4999 " tcl TCL list elements",
5000 " OPTIONS: (for columnar modes or insert mode):",
5001 " --wrap N Wrap output lines to no longer than N characters",
5002 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
5003 " --ww Shorthand for \"--wordwrap 1\"",
5004 " --quote Quote output text as SQL literals",
5005 " --noquote Do not quote output text",
5006 " TABLE The name of SQL table used for \"insert\" mode",
5007 #ifndef SQLITE_SHELL_FIDDLE
5008 ".nonce STRING Suspend safe mode for one command if nonce matches",
5009 #endif
5010 ".nullvalue STRING Use STRING in place of NULL values",
5011 #ifndef SQLITE_SHELL_FIDDLE
5012 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
5013 " If FILE begins with '|' then open as a pipe",
5014 " --bom Put a UTF8 byte-order mark at the beginning",
5015 " -e Send output to the system text editor",
5016 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
5017 /* Note that .open is (partially) available in WASM builds but is
5018 ** currently only intended to be used by the fiddle tool, not
5019 ** end users, so is "undocumented." */
5020 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
5021 " Options:",
5022 " --append Use appendvfs to append database to the end of FILE",
5023 #endif
5024 #ifndef SQLITE_OMIT_DESERIALIZE
5025 " --deserialize Load into memory using sqlite3_deserialize()",
5026 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
5027 " --maxsize N Maximum size for --hexdb or --deserialized database",
5028 #endif
5029 " --new Initialize FILE to an empty database",
5030 " --nofollow Do not follow symbolic links",
5031 " --readonly Open FILE readonly",
5032 " --zip FILE is a ZIP archive",
5033 #ifndef SQLITE_SHELL_FIDDLE
5034 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
5035 " If FILE begins with '|' then open it as a pipe.",
5036 " Options:",
5037 " --bom Prefix output with a UTF8 byte-order mark",
5038 " -e Send output to the system text editor",
5039 " -x Send output as CSV to a spreadsheet",
5040 #endif
5041 ".parameter CMD ... Manage SQL parameter bindings",
5042 " clear Erase all bindings",
5043 " init Initialize the TEMP table that holds bindings",
5044 " list List the current parameter bindings",
5045 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
5046 " PARAMETER should start with one of: $ : @ ?",
5047 " unset PARAMETER Remove PARAMETER from the binding table",
5048 ".print STRING... Print literal STRING",
5049 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
5050 ".progress N Invoke progress handler after every N opcodes",
5051 " --limit N Interrupt after N progress callbacks",
5052 " --once Do no more than one progress interrupt",
5053 " --quiet|-q No output except at interrupts",
5054 " --reset Reset the count for each input and interrupt",
5055 #endif
5056 ".prompt MAIN CONTINUE Replace the standard prompts",
5057 #ifndef SQLITE_SHELL_FIDDLE
5058 ".quit Stop interpreting input stream, exit if primary.",
5059 ".read FILE Read input from FILE or command output",
5060 " If FILE begins with \"|\", it is a command that generates the input.",
5061 #endif
5062 #if SQLITE_SHELL_HAVE_RECOVER
5063 ".recover Recover as much data as possible from corrupt db.",
5064 " --ignore-freelist Ignore pages that appear to be on db freelist",
5065 " --lost-and-found TABLE Alternative name for the lost-and-found table",
5066 " --no-rowids Do not attempt to recover rowid values",
5067 " that are not also INTEGER PRIMARY KEYs",
5068 #endif
5069 #ifndef SQLITE_SHELL_FIDDLE
5070 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
5071 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
5072 #endif
5073 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
5074 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
5075 " Options:",
5076 " --indent Try to pretty-print the schema",
5077 " --nosys Omit objects whose names start with \"sqlite_\"",
5078 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
5079 " Options:",
5080 " --init Create a new SELFTEST table",
5081 " -v Verbose output",
5082 ".separator COL ?ROW? Change the column and row separators",
5083 #if defined(SQLITE_ENABLE_SESSION)
5084 ".session ?NAME? CMD ... Create or control sessions",
5085 " Subcommands:",
5086 " attach TABLE Attach TABLE",
5087 " changeset FILE Write a changeset into FILE",
5088 " close Close one session",
5089 " enable ?BOOLEAN? Set or query the enable bit",
5090 " filter GLOB... Reject tables matching GLOBs",
5091 " indirect ?BOOLEAN? Mark or query the indirect status",
5092 " isempty Query whether the session is empty",
5093 " list List currently open session names",
5094 " open DB NAME Open a new session on DB",
5095 " patchset FILE Write a patchset into FILE",
5096 " If ?NAME? is omitted, the first defined session is used.",
5097 #endif
5098 ".sha3sum ... Compute a SHA3 hash of database content",
5099 " Options:",
5100 " --schema Also hash the sqlite_schema table",
5101 " --sha3-224 Use the sha3-224 algorithm",
5102 " --sha3-256 Use the sha3-256 algorithm (default)",
5103 " --sha3-384 Use the sha3-384 algorithm",
5104 " --sha3-512 Use the sha3-512 algorithm",
5105 " Any other argument is a LIKE pattern for tables to hash",
5106 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5107 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
5108 #endif
5109 ".show Show the current values for various settings",
5110 ".stats ?ARG? Show stats or turn stats on or off",
5111 " off Turn off automatic stat display",
5112 " on Turn on automatic stat display",
5113 " stmt Show statement stats",
5114 " vmstep Show the virtual machine step count only",
5115 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5116 ".system CMD ARGS... Run CMD ARGS... in a system shell",
5117 #endif
5118 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
5119 #ifndef SQLITE_SHELL_FIDDLE
5120 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
5121 #endif
5122 ",testctrl CMD ... Run various sqlite3_test_control() operations",
5123 " Run \".testctrl\" with no arguments for details",
5124 ".timeout MS Try opening locked tables for MS milliseconds",
5125 ".timer on|off Turn SQL timer on or off",
5126 #ifndef SQLITE_OMIT_TRACE
5127 ".trace ?OPTIONS? Output each SQL statement as it is run",
5128 " FILE Send output to FILE",
5129 " stdout Send output to stdout",
5130 " stderr Send output to stderr",
5131 " off Disable tracing",
5132 " --expanded Expand query parameters",
5133 #ifdef SQLITE_ENABLE_NORMALIZE
5134 " --normalized Normal the SQL statements",
5135 #endif
5136 " --plain Show SQL as it is input",
5137 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
5138 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
5139 " --row Trace each row (SQLITE_TRACE_ROW)",
5140 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
5141 #endif /* SQLITE_OMIT_TRACE */
5142 #ifdef SQLITE_DEBUG
5143 ".unmodule NAME ... Unregister virtual table modules",
5144 " --allexcept Unregister everything except those named",
5145 #endif
5146 ".version Show source, library and compiler versions",
5147 ".vfsinfo ?AUX? Information about the top-level VFS",
5148 ".vfslist List all available VFSes",
5149 ".vfsname ?AUX? Print the name of the VFS stack",
5150 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
5151 " Negative values right-justify",
5155 ** Output help text.
5157 ** zPattern describes the set of commands for which help text is provided.
5158 ** If zPattern is NULL, then show all commands, but only give a one-line
5159 ** description of each.
5161 ** Return the number of matches.
5163 static int showHelp(FILE *out, const char *zPattern){
5164 int i = 0;
5165 int j = 0;
5166 int n = 0;
5167 char *zPat;
5168 if( zPattern==0
5169 || zPattern[0]=='0'
5170 || cli_strcmp(zPattern,"-a")==0
5171 || cli_strcmp(zPattern,"-all")==0
5172 || cli_strcmp(zPattern,"--all")==0
5174 enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
5175 enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
5176 /* Show all or most commands
5177 ** *zPattern==0 => summary of documented commands only
5178 ** *zPattern=='0' => whole help for undocumented commands
5179 ** Otherwise => whole help for documented commands
5181 enum HelpWanted hw = HW_SummaryOnly;
5182 enum HelpHave hh = HH_More;
5183 if( zPattern!=0 ){
5184 hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
5186 for(i=0; i<ArraySize(azHelp); i++){
5187 switch( azHelp[i][0] ){
5188 case ',':
5189 hh = HH_Summary|HH_Undoc;
5190 break;
5191 case '.':
5192 hh = HH_Summary;
5193 break;
5194 default:
5195 hh &= ~HH_Summary;
5196 break;
5198 if( ((hw^hh)&HH_Undoc)==0 ){
5199 if( (hh&HH_Summary)!=0 ){
5200 sputf(out, ".%s\n", azHelp[i]+1);
5201 ++n;
5202 }else if( (hw&HW_SummaryOnly)==0 ){
5203 sputf(out, "%s\n", azHelp[i]);
5207 }else{
5208 /* Seek documented commands for which zPattern is an exact prefix */
5209 zPat = sqlite3_mprintf(".%s*", zPattern);
5210 shell_check_oom(zPat);
5211 for(i=0; i<ArraySize(azHelp); i++){
5212 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
5213 sputf(out, "%s\n", azHelp[i]);
5214 j = i+1;
5215 n++;
5218 sqlite3_free(zPat);
5219 if( n ){
5220 if( n==1 ){
5221 /* when zPattern is a prefix of exactly one command, then include
5222 ** the details of that command, which should begin at offset j */
5223 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
5224 sputf(out, "%s\n", azHelp[j]);
5225 j++;
5228 return n;
5230 /* Look for documented commands that contain zPattern anywhere.
5231 ** Show complete text of all documented commands that match. */
5232 zPat = sqlite3_mprintf("%%%s%%", zPattern);
5233 shell_check_oom(zPat);
5234 for(i=0; i<ArraySize(azHelp); i++){
5235 if( azHelp[i][0]==',' ){
5236 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
5237 continue;
5239 if( azHelp[i][0]=='.' ) j = i;
5240 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
5241 sputf(out, "%s\n", azHelp[j]);
5242 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
5243 j++;
5244 sputf(out, "%s\n", azHelp[j]);
5246 i = j;
5247 n++;
5250 sqlite3_free(zPat);
5252 return n;
5255 /* Forward reference */
5256 static int process_input(ShellState *p);
5259 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
5260 ** and return a pointer to the buffer. The caller is responsible for freeing
5261 ** the memory.
5263 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
5264 ** read.
5266 ** For convenience, a nul-terminator byte is always appended to the data read
5267 ** from the file before the buffer is returned. This byte is not included in
5268 ** the final value of (*pnByte), if applicable.
5270 ** NULL is returned if any error is encountered. The final value of *pnByte
5271 ** is undefined in this case.
5273 static char *readFile(const char *zName, int *pnByte){
5274 FILE *in = fopen(zName, "rb");
5275 long nIn;
5276 size_t nRead;
5277 char *pBuf;
5278 int rc;
5279 if( in==0 ) return 0;
5280 rc = fseek(in, 0, SEEK_END);
5281 if( rc!=0 ){
5282 eputf("Error: '%s' not seekable\n", zName);
5283 fclose(in);
5284 return 0;
5286 nIn = ftell(in);
5287 rewind(in);
5288 pBuf = sqlite3_malloc64( nIn+1 );
5289 if( pBuf==0 ){
5290 eputz("Error: out of memory\n");
5291 fclose(in);
5292 return 0;
5294 nRead = fread(pBuf, nIn, 1, in);
5295 fclose(in);
5296 if( nRead!=1 ){
5297 sqlite3_free(pBuf);
5298 eputf("Error: cannot read '%s'\n", zName);
5299 return 0;
5301 pBuf[nIn] = 0;
5302 if( pnByte ) *pnByte = nIn;
5303 return pBuf;
5306 #if defined(SQLITE_ENABLE_SESSION)
5308 ** Close a single OpenSession object and release all of its associated
5309 ** resources.
5311 static void session_close(OpenSession *pSession){
5312 int i;
5313 sqlite3session_delete(pSession->p);
5314 sqlite3_free(pSession->zName);
5315 for(i=0; i<pSession->nFilter; i++){
5316 sqlite3_free(pSession->azFilter[i]);
5318 sqlite3_free(pSession->azFilter);
5319 memset(pSession, 0, sizeof(OpenSession));
5321 #endif
5324 ** Close all OpenSession objects and release all associated resources.
5326 #if defined(SQLITE_ENABLE_SESSION)
5327 static void session_close_all(ShellState *p, int i){
5328 int j;
5329 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
5330 for(j=0; j<pAuxDb->nSession; j++){
5331 session_close(&pAuxDb->aSession[j]);
5333 pAuxDb->nSession = 0;
5335 #else
5336 # define session_close_all(X,Y)
5337 #endif
5340 ** Implementation of the xFilter function for an open session. Omit
5341 ** any tables named by ".session filter" but let all other table through.
5343 #if defined(SQLITE_ENABLE_SESSION)
5344 static int session_filter(void *pCtx, const char *zTab){
5345 OpenSession *pSession = (OpenSession*)pCtx;
5346 int i;
5347 for(i=0; i<pSession->nFilter; i++){
5348 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
5350 return 1;
5352 #endif
5355 ** Try to deduce the type of file for zName based on its content. Return
5356 ** one of the SHELL_OPEN_* constants.
5358 ** If the file does not exist or is empty but its name looks like a ZIP
5359 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
5360 ** Otherwise, assume an ordinary database regardless of the filename if
5361 ** the type cannot be determined from content.
5363 int deduceDatabaseType(const char *zName, int dfltZip){
5364 FILE *f = fopen(zName, "rb");
5365 size_t n;
5366 int rc = SHELL_OPEN_UNSPEC;
5367 char zBuf[100];
5368 if( f==0 ){
5369 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5370 return SHELL_OPEN_ZIPFILE;
5371 }else{
5372 return SHELL_OPEN_NORMAL;
5375 n = fread(zBuf, 16, 1, f);
5376 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
5377 fclose(f);
5378 return SHELL_OPEN_NORMAL;
5380 fseek(f, -25, SEEK_END);
5381 n = fread(zBuf, 25, 1, f);
5382 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
5383 rc = SHELL_OPEN_APPENDVFS;
5384 }else{
5385 fseek(f, -22, SEEK_END);
5386 n = fread(zBuf, 22, 1, f);
5387 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
5388 && zBuf[3]==0x06 ){
5389 rc = SHELL_OPEN_ZIPFILE;
5390 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5391 rc = SHELL_OPEN_ZIPFILE;
5394 fclose(f);
5395 return rc;
5398 #ifndef SQLITE_OMIT_DESERIALIZE
5400 ** Reconstruct an in-memory database using the output from the "dbtotxt"
5401 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
5402 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
5404 static unsigned char *readHexDb(ShellState *p, int *pnData){
5405 unsigned char *a = 0;
5406 int nLine;
5407 int n = 0;
5408 int pgsz = 0;
5409 int iOffset = 0;
5410 int j, k;
5411 int rc;
5412 FILE *in;
5413 const char *zDbFilename = p->pAuxDb->zDbFilename;
5414 unsigned int x[16];
5415 char zLine[1000];
5416 if( zDbFilename ){
5417 in = fopen(zDbFilename, "r");
5418 if( in==0 ){
5419 eputf("cannot open \"%s\" for reading\n", zDbFilename);
5420 return 0;
5422 nLine = 0;
5423 }else{
5424 in = p->in;
5425 nLine = p->lineno;
5426 if( in==0 ) in = stdin;
5428 *pnData = 0;
5429 nLine++;
5430 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
5431 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
5432 if( rc!=2 ) goto readHexDb_error;
5433 if( n<0 ) goto readHexDb_error;
5434 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
5435 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
5436 a = sqlite3_malloc( n ? n : 1 );
5437 shell_check_oom(a);
5438 memset(a, 0, n);
5439 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
5440 eputz("invalid pagesize\n");
5441 goto readHexDb_error;
5443 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
5444 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
5445 if( rc==2 ){
5446 iOffset = k;
5447 continue;
5449 if( cli_strncmp(zLine, "| end ", 6)==0 ){
5450 break;
5452 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5453 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
5454 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
5455 if( rc==17 ){
5456 k = iOffset+j;
5457 if( k+16<=n && k>=0 ){
5458 int ii;
5459 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
5463 *pnData = n;
5464 if( in!=p->in ){
5465 fclose(in);
5466 }else{
5467 p->lineno = nLine;
5469 return a;
5471 readHexDb_error:
5472 if( in!=p->in ){
5473 fclose(in);
5474 }else{
5475 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
5476 nLine++;
5477 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
5479 p->lineno = nLine;
5481 sqlite3_free(a);
5482 eputf("Error on line %d of --hexdb input\n", nLine);
5483 return 0;
5485 #endif /* SQLITE_OMIT_DESERIALIZE */
5488 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
5490 static void shellUSleepFunc(
5491 sqlite3_context *context,
5492 int argcUnused,
5493 sqlite3_value **argv
5495 int sleep = sqlite3_value_int(argv[0]);
5496 (void)argcUnused;
5497 sqlite3_sleep(sleep/1000);
5498 sqlite3_result_int(context, sleep);
5501 /* Flags for open_db().
5503 ** The default behavior of open_db() is to exit(1) if the database fails to
5504 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5505 ** but still returns without calling exit.
5507 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5508 ** ZIP archive if the file does not exist or is empty and its name matches
5509 ** the *.zip pattern.
5511 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
5512 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
5515 ** Make sure the database is open. If it is not, then open it. If
5516 ** the database fails to open, print an error message and exit.
5518 static void open_db(ShellState *p, int openFlags){
5519 if( p->db==0 ){
5520 const char *zDbFilename = p->pAuxDb->zDbFilename;
5521 if( p->openMode==SHELL_OPEN_UNSPEC ){
5522 if( zDbFilename==0 || zDbFilename[0]==0 ){
5523 p->openMode = SHELL_OPEN_NORMAL;
5524 }else{
5525 p->openMode = (u8)deduceDatabaseType(zDbFilename,
5526 (openFlags & OPEN_DB_ZIPFILE)!=0);
5529 switch( p->openMode ){
5530 case SHELL_OPEN_APPENDVFS: {
5531 sqlite3_open_v2(zDbFilename, &p->db,
5532 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5533 break;
5535 case SHELL_OPEN_HEXDB:
5536 case SHELL_OPEN_DESERIALIZE: {
5537 sqlite3_open(0, &p->db);
5538 break;
5540 case SHELL_OPEN_ZIPFILE: {
5541 sqlite3_open(":memory:", &p->db);
5542 break;
5544 case SHELL_OPEN_READONLY: {
5545 sqlite3_open_v2(zDbFilename, &p->db,
5546 SQLITE_OPEN_READONLY|p->openFlags, 0);
5547 break;
5549 case SHELL_OPEN_UNSPEC:
5550 case SHELL_OPEN_NORMAL: {
5551 sqlite3_open_v2(zDbFilename, &p->db,
5552 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5553 break;
5556 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5557 eputf("Error: unable to open database \"%s\": %s\n",
5558 zDbFilename, sqlite3_errmsg(p->db));
5559 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
5560 exit(1);
5562 sqlite3_close(p->db);
5563 sqlite3_open(":memory:", &p->db);
5564 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5565 eputz("Also: unable to open substitute in-memory database.\n");
5566 exit(1);
5567 }else{
5568 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
5569 zDbFilename);
5572 globalDb = p->db;
5573 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
5575 /* Reflect the use or absence of --unsafe-testing invocation. */
5577 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
5578 sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
5579 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
5582 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5583 sqlite3_enable_load_extension(p->db, 1);
5584 #endif
5585 sqlite3_sha_init(p->db, 0, 0);
5586 sqlite3_shathree_init(p->db, 0, 0);
5587 sqlite3_uint_init(p->db, 0, 0);
5588 sqlite3_stmtrand_init(p->db, 0, 0);
5589 sqlite3_decimal_init(p->db, 0, 0);
5590 sqlite3_percentile_init(p->db, 0, 0);
5591 sqlite3_base64_init(p->db, 0, 0);
5592 sqlite3_base85_init(p->db, 0, 0);
5593 sqlite3_regexp_init(p->db, 0, 0);
5594 sqlite3_ieee_init(p->db, 0, 0);
5595 sqlite3_series_init(p->db, 0, 0);
5596 #ifndef SQLITE_SHELL_FIDDLE
5597 sqlite3_fileio_init(p->db, 0, 0);
5598 sqlite3_completion_init(p->db, 0, 0);
5599 #endif
5600 #ifdef SQLITE_HAVE_ZLIB
5601 if( !p->bSafeModePersist ){
5602 sqlite3_zipfile_init(p->db, 0, 0);
5603 sqlite3_sqlar_init(p->db, 0, 0);
5605 #endif
5606 #ifdef SQLITE_SHELL_EXTFUNCS
5607 /* Create a preprocessing mechanism for extensions to make
5608 * their own provisions for being built into the shell.
5609 * This is a short-span macro. See further below for usage.
5611 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
5612 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
5613 /* Let custom-included extensions get their ..._init() called.
5614 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
5615 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
5616 * initialization routine to be called.
5619 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
5620 /* Let custom-included extensions expose their functionality.
5621 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
5622 * the SQL functions, virtual tables, collating sequences or
5623 * VFS's implemented by the extension to be registered.
5625 if( irc==SQLITE_OK
5626 || irc==SQLITE_OK_LOAD_PERMANENTLY ){
5627 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
5629 #undef SHELL_SUB_MACRO
5630 #undef SHELL_SUBMACRO
5632 #endif
5634 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
5635 shellStrtod, 0, 0);
5636 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
5637 shellDtostr, 0, 0);
5638 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
5639 shellDtostr, 0, 0);
5640 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5641 shellAddSchemaName, 0, 0);
5642 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5643 shellModuleSchema, 0, 0);
5644 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5645 shellPutsFunc, 0, 0);
5646 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5647 shellUSleepFunc, 0, 0);
5648 #ifndef SQLITE_NOHAVE_SYSTEM
5649 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5650 editFunc, 0, 0);
5651 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5652 editFunc, 0, 0);
5653 #endif
5655 if( p->openMode==SHELL_OPEN_ZIPFILE ){
5656 char *zSql = sqlite3_mprintf(
5657 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5658 shell_check_oom(zSql);
5659 sqlite3_exec(p->db, zSql, 0, 0, 0);
5660 sqlite3_free(zSql);
5662 #ifndef SQLITE_OMIT_DESERIALIZE
5663 else
5664 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5665 int rc;
5666 int nData = 0;
5667 unsigned char *aData;
5668 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5669 aData = (unsigned char*)readFile(zDbFilename, &nData);
5670 }else{
5671 aData = readHexDb(p, &nData);
5673 if( aData==0 ){
5674 return;
5676 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5677 SQLITE_DESERIALIZE_RESIZEABLE |
5678 SQLITE_DESERIALIZE_FREEONCLOSE);
5679 if( rc ){
5680 eputf("Error: sqlite3_deserialize() returns %d\n", rc);
5682 if( p->szMax>0 ){
5683 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5686 #endif
5688 if( p->db!=0 ){
5689 if( p->bSafeModePersist ){
5690 sqlite3_set_authorizer(p->db, safeModeAuth, p);
5692 sqlite3_db_config(
5693 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
5699 ** Attempt to close the database connection. Report errors.
5701 void close_db(sqlite3 *db){
5702 int rc = sqlite3_close(db);
5703 if( rc ){
5704 eputf("Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
5708 #if HAVE_READLINE || HAVE_EDITLINE
5710 ** Readline completion callbacks
5712 static char *readline_completion_generator(const char *text, int state){
5713 static sqlite3_stmt *pStmt = 0;
5714 char *zRet;
5715 if( state==0 ){
5716 char *zSql;
5717 sqlite3_finalize(pStmt);
5718 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5719 " FROM completion(%Q) ORDER BY 1", text);
5720 shell_check_oom(zSql);
5721 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5722 sqlite3_free(zSql);
5724 if( sqlite3_step(pStmt)==SQLITE_ROW ){
5725 const char *z = (const char*)sqlite3_column_text(pStmt,0);
5726 zRet = z ? strdup(z) : 0;
5727 }else{
5728 sqlite3_finalize(pStmt);
5729 pStmt = 0;
5730 zRet = 0;
5732 return zRet;
5734 static char **readline_completion(const char *zText, int iStart, int iEnd){
5735 (void)iStart;
5736 (void)iEnd;
5737 rl_attempted_completion_over = 1;
5738 return rl_completion_matches(zText, readline_completion_generator);
5741 #elif HAVE_LINENOISE
5743 ** Linenoise completion callback. Note that the 3rd argument is from
5744 ** the "msteveb" version of linenoise, not the "antirez" version.
5746 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc,
5747 void *pUserData){
5748 i64 nLine = strlen(zLine);
5749 i64 i, iStart;
5750 sqlite3_stmt *pStmt = 0;
5751 char *zSql;
5752 char zBuf[1000];
5754 UNUSED_PARAMETER(pUserData);
5755 if( nLine>(i64)sizeof(zBuf)-30 ) return;
5756 if( zLine[0]=='.' || zLine[0]=='#') return;
5757 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5758 if( i==nLine-1 ) return;
5759 iStart = i+1;
5760 memcpy(zBuf, zLine, iStart);
5761 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5762 " FROM completion(%Q,%Q) ORDER BY 1",
5763 &zLine[iStart], zLine);
5764 shell_check_oom(zSql);
5765 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5766 sqlite3_free(zSql);
5767 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5768 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5769 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5770 int nCompletion = sqlite3_column_bytes(pStmt, 0);
5771 if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
5772 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5773 linenoiseAddCompletion(lc, zBuf);
5776 sqlite3_finalize(pStmt);
5778 #endif
5781 ** Do C-language style dequoting.
5783 ** \a -> alarm
5784 ** \b -> backspace
5785 ** \t -> tab
5786 ** \n -> newline
5787 ** \v -> vertical tab
5788 ** \f -> form feed
5789 ** \r -> carriage return
5790 ** \s -> space
5791 ** \" -> "
5792 ** \' -> '
5793 ** \\ -> backslash
5794 ** \NNN -> ascii character NNN in octal
5795 ** \xHH -> ascii character HH in hexadecimal
5797 static void resolve_backslashes(char *z){
5798 int i, j;
5799 char c;
5800 while( *z && *z!='\\' ) z++;
5801 for(i=j=0; (c = z[i])!=0; i++, j++){
5802 if( c=='\\' && z[i+1]!=0 ){
5803 c = z[++i];
5804 if( c=='a' ){
5805 c = '\a';
5806 }else if( c=='b' ){
5807 c = '\b';
5808 }else if( c=='t' ){
5809 c = '\t';
5810 }else if( c=='n' ){
5811 c = '\n';
5812 }else if( c=='v' ){
5813 c = '\v';
5814 }else if( c=='f' ){
5815 c = '\f';
5816 }else if( c=='r' ){
5817 c = '\r';
5818 }else if( c=='"' ){
5819 c = '"';
5820 }else if( c=='\'' ){
5821 c = '\'';
5822 }else if( c=='\\' ){
5823 c = '\\';
5824 }else if( c=='x' ){
5825 int nhd = 0, hdv;
5826 u8 hv = 0;
5827 while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
5828 hv = (u8)((hv<<4)|hdv);
5829 ++nhd;
5831 i += nhd;
5832 c = (u8)hv;
5833 }else if( c>='0' && c<='7' ){
5834 c -= '0';
5835 if( z[i+1]>='0' && z[i+1]<='7' ){
5836 i++;
5837 c = (c<<3) + z[i] - '0';
5838 if( z[i+1]>='0' && z[i+1]<='7' ){
5839 i++;
5840 c = (c<<3) + z[i] - '0';
5845 z[j] = c;
5847 if( j<i ) z[j] = 0;
5851 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
5852 ** for TRUE and FALSE. Return the integer value if appropriate.
5854 static int booleanValue(const char *zArg){
5855 int i;
5856 if( zArg[0]=='0' && zArg[1]=='x' ){
5857 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5858 }else{
5859 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5861 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5862 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5863 return 1;
5865 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5866 return 0;
5868 eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
5869 return 0;
5873 ** Set or clear a shell flag according to a boolean value.
5875 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5876 if( booleanValue(zArg) ){
5877 ShellSetFlag(p, mFlag);
5878 }else{
5879 ShellClearFlag(p, mFlag);
5884 ** Close an output file, assuming it is not stderr or stdout
5886 static void output_file_close(FILE *f){
5887 if( f && f!=stdout && f!=stderr ) fclose(f);
5891 ** Try to open an output file. The names "stdout" and "stderr" are
5892 ** recognized and do the right thing. NULL is returned if the output
5893 ** filename is "off".
5895 static FILE *output_file_open(const char *zFile, int bTextMode){
5896 FILE *f;
5897 if( cli_strcmp(zFile,"stdout")==0 ){
5898 f = stdout;
5899 }else if( cli_strcmp(zFile, "stderr")==0 ){
5900 f = stderr;
5901 }else if( cli_strcmp(zFile, "off")==0 ){
5902 f = 0;
5903 }else{
5904 f = fopen(zFile, bTextMode ? "w" : "wb");
5905 if( f==0 ){
5906 eputf("Error: cannot open \"%s\"\n", zFile);
5909 return f;
5912 #ifndef SQLITE_OMIT_TRACE
5914 ** A routine for handling output from sqlite3_trace().
5916 static int sql_trace_callback(
5917 unsigned mType, /* The trace type */
5918 void *pArg, /* The ShellState pointer */
5919 void *pP, /* Usually a pointer to sqlite_stmt */
5920 void *pX /* Auxiliary output */
5922 ShellState *p = (ShellState*)pArg;
5923 sqlite3_stmt *pStmt;
5924 const char *zSql;
5925 i64 nSql;
5926 if( p->traceOut==0 ) return 0;
5927 if( mType==SQLITE_TRACE_CLOSE ){
5928 sputz(p->traceOut, "-- closing database connection\n");
5929 return 0;
5931 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
5932 zSql = (const char*)pX;
5933 }else{
5934 pStmt = (sqlite3_stmt*)pP;
5935 switch( p->eTraceType ){
5936 case SHELL_TRACE_EXPANDED: {
5937 zSql = sqlite3_expanded_sql(pStmt);
5938 break;
5940 #ifdef SQLITE_ENABLE_NORMALIZE
5941 case SHELL_TRACE_NORMALIZED: {
5942 zSql = sqlite3_normalized_sql(pStmt);
5943 break;
5945 #endif
5946 default: {
5947 zSql = sqlite3_sql(pStmt);
5948 break;
5952 if( zSql==0 ) return 0;
5953 nSql = strlen(zSql);
5954 if( nSql>1000000000 ) nSql = 1000000000;
5955 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
5956 switch( mType ){
5957 case SQLITE_TRACE_ROW:
5958 case SQLITE_TRACE_STMT: {
5959 sputf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
5960 break;
5962 case SQLITE_TRACE_PROFILE: {
5963 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
5964 sputf(p->traceOut, "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
5965 break;
5968 return 0;
5970 #endif
5973 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
5974 ** a useful spot to set a debugger breakpoint.
5976 ** This routine does not do anything practical. The code are there simply
5977 ** to prevent the compiler from optimizing this routine out.
5979 static void test_breakpoint(void){
5980 static unsigned int nCall = 0;
5981 if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
5985 ** An object used to read a CSV and other files for import.
5987 typedef struct ImportCtx ImportCtx;
5988 struct ImportCtx {
5989 const char *zFile; /* Name of the input file */
5990 FILE *in; /* Read the CSV text from this input stream */
5991 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
5992 char *z; /* Accumulated text for a field */
5993 int n; /* Number of bytes in z */
5994 int nAlloc; /* Space allocated for z[] */
5995 int nLine; /* Current line number */
5996 int nRow; /* Number of rows imported */
5997 int nErr; /* Number of errors encountered */
5998 int bNotFirst; /* True if one or more bytes already read */
5999 int cTerm; /* Character that terminated the most recent field */
6000 int cColSep; /* The column separator character. (Usually ",") */
6001 int cRowSep; /* The row separator character. (Usually "\n") */
6004 /* Clean up resourced used by an ImportCtx */
6005 static void import_cleanup(ImportCtx *p){
6006 if( p->in!=0 && p->xCloser!=0 ){
6007 p->xCloser(p->in);
6008 p->in = 0;
6010 sqlite3_free(p->z);
6011 p->z = 0;
6014 /* Append a single byte to z[] */
6015 static void import_append_char(ImportCtx *p, int c){
6016 if( p->n+1>=p->nAlloc ){
6017 p->nAlloc += p->nAlloc + 100;
6018 p->z = sqlite3_realloc64(p->z, p->nAlloc);
6019 shell_check_oom(p->z);
6021 p->z[p->n++] = (char)c;
6024 /* Read a single field of CSV text. Compatible with rfc4180 and extended
6025 ** with the option of having a separator other than ",".
6027 ** + Input comes from p->in.
6028 ** + Store results in p->z of length p->n. Space to hold p->z comes
6029 ** from sqlite3_malloc64().
6030 ** + Use p->cSep as the column separator. The default is ",".
6031 ** + Use p->rSep as the row separator. The default is "\n".
6032 ** + Keep track of the line number in p->nLine.
6033 ** + Store the character that terminates the field in p->cTerm. Store
6034 ** EOF on end-of-file.
6035 ** + Report syntax errors on stderr
6037 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
6038 int c;
6039 int cSep = (u8)p->cColSep;
6040 int rSep = (u8)p->cRowSep;
6041 p->n = 0;
6042 c = fgetc(p->in);
6043 if( c==EOF || seenInterrupt ){
6044 p->cTerm = EOF;
6045 return 0;
6047 if( c=='"' ){
6048 int pc, ppc;
6049 int startLine = p->nLine;
6050 int cQuote = c;
6051 pc = ppc = 0;
6052 while( 1 ){
6053 c = fgetc(p->in);
6054 if( c==rSep ) p->nLine++;
6055 if( c==cQuote ){
6056 if( pc==cQuote ){
6057 pc = 0;
6058 continue;
6061 if( (c==cSep && pc==cQuote)
6062 || (c==rSep && pc==cQuote)
6063 || (c==rSep && pc=='\r' && ppc==cQuote)
6064 || (c==EOF && pc==cQuote)
6066 do{ p->n--; }while( p->z[p->n]!=cQuote );
6067 p->cTerm = c;
6068 break;
6070 if( pc==cQuote && c!='\r' ){
6071 eputf("%s:%d: unescaped %c character\n", p->zFile, p->nLine, cQuote);
6073 if( c==EOF ){
6074 eputf("%s:%d: unterminated %c-quoted field\n",
6075 p->zFile, startLine, cQuote);
6076 p->cTerm = c;
6077 break;
6079 import_append_char(p, c);
6080 ppc = pc;
6081 pc = c;
6083 }else{
6084 /* If this is the first field being parsed and it begins with the
6085 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
6086 if( (c&0xff)==0xef && p->bNotFirst==0 ){
6087 import_append_char(p, c);
6088 c = fgetc(p->in);
6089 if( (c&0xff)==0xbb ){
6090 import_append_char(p, c);
6091 c = fgetc(p->in);
6092 if( (c&0xff)==0xbf ){
6093 p->bNotFirst = 1;
6094 p->n = 0;
6095 return csv_read_one_field(p);
6099 while( c!=EOF && c!=cSep && c!=rSep ){
6100 import_append_char(p, c);
6101 c = fgetc(p->in);
6103 if( c==rSep ){
6104 p->nLine++;
6105 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
6107 p->cTerm = c;
6109 if( p->z ) p->z[p->n] = 0;
6110 p->bNotFirst = 1;
6111 return p->z;
6114 /* Read a single field of ASCII delimited text.
6116 ** + Input comes from p->in.
6117 ** + Store results in p->z of length p->n. Space to hold p->z comes
6118 ** from sqlite3_malloc64().
6119 ** + Use p->cSep as the column separator. The default is "\x1F".
6120 ** + Use p->rSep as the row separator. The default is "\x1E".
6121 ** + Keep track of the row number in p->nLine.
6122 ** + Store the character that terminates the field in p->cTerm. Store
6123 ** EOF on end-of-file.
6124 ** + Report syntax errors on stderr
6126 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
6127 int c;
6128 int cSep = (u8)p->cColSep;
6129 int rSep = (u8)p->cRowSep;
6130 p->n = 0;
6131 c = fgetc(p->in);
6132 if( c==EOF || seenInterrupt ){
6133 p->cTerm = EOF;
6134 return 0;
6136 while( c!=EOF && c!=cSep && c!=rSep ){
6137 import_append_char(p, c);
6138 c = fgetc(p->in);
6140 if( c==rSep ){
6141 p->nLine++;
6143 p->cTerm = c;
6144 if( p->z ) p->z[p->n] = 0;
6145 return p->z;
6149 ** Try to transfer data for table zTable. If an error is seen while
6150 ** moving forward, try to go backwards. The backwards movement won't
6151 ** work for WITHOUT ROWID tables.
6153 static void tryToCloneData(
6154 ShellState *p,
6155 sqlite3 *newDb,
6156 const char *zTable
6158 sqlite3_stmt *pQuery = 0;
6159 sqlite3_stmt *pInsert = 0;
6160 char *zQuery = 0;
6161 char *zInsert = 0;
6162 int rc;
6163 int i, j, n;
6164 int nTable = strlen30(zTable);
6165 int k = 0;
6166 int cnt = 0;
6167 const int spinRate = 10000;
6169 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
6170 shell_check_oom(zQuery);
6171 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6172 if( rc ){
6173 eputf("Error %d: %s on [%s]\n",
6174 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
6175 goto end_data_xfer;
6177 n = sqlite3_column_count(pQuery);
6178 zInsert = sqlite3_malloc64(200 + nTable + n*3);
6179 shell_check_oom(zInsert);
6180 sqlite3_snprintf(200+nTable,zInsert,
6181 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
6182 i = strlen30(zInsert);
6183 for(j=1; j<n; j++){
6184 memcpy(zInsert+i, ",?", 2);
6185 i += 2;
6187 memcpy(zInsert+i, ");", 3);
6188 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
6189 if( rc ){
6190 eputf("Error %d: %s on [%s]\n",
6191 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
6192 goto end_data_xfer;
6194 for(k=0; k<2; k++){
6195 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6196 for(i=0; i<n; i++){
6197 switch( sqlite3_column_type(pQuery, i) ){
6198 case SQLITE_NULL: {
6199 sqlite3_bind_null(pInsert, i+1);
6200 break;
6202 case SQLITE_INTEGER: {
6203 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
6204 break;
6206 case SQLITE_FLOAT: {
6207 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
6208 break;
6210 case SQLITE_TEXT: {
6211 sqlite3_bind_text(pInsert, i+1,
6212 (const char*)sqlite3_column_text(pQuery,i),
6213 -1, SQLITE_STATIC);
6214 break;
6216 case SQLITE_BLOB: {
6217 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
6218 sqlite3_column_bytes(pQuery,i),
6219 SQLITE_STATIC);
6220 break;
6223 } /* End for */
6224 rc = sqlite3_step(pInsert);
6225 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
6226 eputf("Error %d: %s\n",
6227 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
6229 sqlite3_reset(pInsert);
6230 cnt++;
6231 if( (cnt%spinRate)==0 ){
6232 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
6233 fflush(stdout);
6235 } /* End while */
6236 if( rc==SQLITE_DONE ) break;
6237 sqlite3_finalize(pQuery);
6238 sqlite3_free(zQuery);
6239 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
6240 zTable);
6241 shell_check_oom(zQuery);
6242 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6243 if( rc ){
6244 eputf("Warning: cannot step \"%s\" backwards", zTable);
6245 break;
6247 } /* End for(k=0...) */
6249 end_data_xfer:
6250 sqlite3_finalize(pQuery);
6251 sqlite3_finalize(pInsert);
6252 sqlite3_free(zQuery);
6253 sqlite3_free(zInsert);
6258 ** Try to transfer all rows of the schema that match zWhere. For
6259 ** each row, invoke xForEach() on the object defined by that row.
6260 ** If an error is encountered while moving forward through the
6261 ** sqlite_schema table, try again moving backwards.
6263 static void tryToCloneSchema(
6264 ShellState *p,
6265 sqlite3 *newDb,
6266 const char *zWhere,
6267 void (*xForEach)(ShellState*,sqlite3*,const char*)
6269 sqlite3_stmt *pQuery = 0;
6270 char *zQuery = 0;
6271 int rc;
6272 const unsigned char *zName;
6273 const unsigned char *zSql;
6274 char *zErrMsg = 0;
6276 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6277 " WHERE %s ORDER BY rowid ASC", zWhere);
6278 shell_check_oom(zQuery);
6279 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6280 if( rc ){
6281 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
6282 sqlite3_errmsg(p->db), zQuery);
6283 goto end_schema_xfer;
6285 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6286 zName = sqlite3_column_text(pQuery, 0);
6287 zSql = sqlite3_column_text(pQuery, 1);
6288 if( zName==0 || zSql==0 ) continue;
6289 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
6290 sputf(stdout, "%s... ", zName); fflush(stdout);
6291 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6292 if( zErrMsg ){
6293 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6294 sqlite3_free(zErrMsg);
6295 zErrMsg = 0;
6298 if( xForEach ){
6299 xForEach(p, newDb, (const char*)zName);
6301 sputz(stdout, "done\n");
6303 if( rc!=SQLITE_DONE ){
6304 sqlite3_finalize(pQuery);
6305 sqlite3_free(zQuery);
6306 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6307 " WHERE %s ORDER BY rowid DESC", zWhere);
6308 shell_check_oom(zQuery);
6309 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6310 if( rc ){
6311 eputf("Error: (%d) %s on [%s]\n",
6312 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
6313 goto end_schema_xfer;
6315 while( sqlite3_step(pQuery)==SQLITE_ROW ){
6316 zName = sqlite3_column_text(pQuery, 0);
6317 zSql = sqlite3_column_text(pQuery, 1);
6318 if( zName==0 || zSql==0 ) continue;
6319 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
6320 sputf(stdout, "%s... ", zName); fflush(stdout);
6321 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6322 if( zErrMsg ){
6323 eputf("Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6324 sqlite3_free(zErrMsg);
6325 zErrMsg = 0;
6327 if( xForEach ){
6328 xForEach(p, newDb, (const char*)zName);
6330 sputz(stdout, "done\n");
6333 end_schema_xfer:
6334 sqlite3_finalize(pQuery);
6335 sqlite3_free(zQuery);
6339 ** Open a new database file named "zNewDb". Try to recover as much information
6340 ** as possible out of the main database (which might be corrupt) and write it
6341 ** into zNewDb.
6343 static void tryToClone(ShellState *p, const char *zNewDb){
6344 int rc;
6345 sqlite3 *newDb = 0;
6346 if( access(zNewDb,0)==0 ){
6347 eputf("File \"%s\" already exists.\n", zNewDb);
6348 return;
6350 rc = sqlite3_open(zNewDb, &newDb);
6351 if( rc ){
6352 eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb));
6353 }else{
6354 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
6355 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
6356 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
6357 tryToCloneSchema(p, newDb, "type!='table'", 0);
6358 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
6359 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
6361 close_db(newDb);
6364 #ifndef SQLITE_SHELL_FIDDLE
6366 ** Change the output stream (file or pipe or console) to something else.
6368 static void output_redir(ShellState *p, FILE *pfNew){
6369 if( p->out != stdout ) eputz("Output already redirected.\n");
6370 else{
6371 p->out = pfNew;
6372 setOutputStream(pfNew);
6377 ** Change the output file back to stdout.
6379 ** If the p->doXdgOpen flag is set, that means the output was being
6380 ** redirected to a temporary file named by p->zTempFile. In that case,
6381 ** launch start/open/xdg-open on that temporary file.
6383 static void output_reset(ShellState *p){
6384 if( p->outfile[0]=='|' ){
6385 #ifndef SQLITE_OMIT_POPEN
6386 pclose(p->out);
6387 #endif
6388 }else{
6389 output_file_close(p->out);
6390 #ifndef SQLITE_NOHAVE_SYSTEM
6391 if( p->doXdgOpen ){
6392 const char *zXdgOpenCmd =
6393 #if defined(_WIN32)
6394 "start";
6395 #elif defined(__APPLE__)
6396 "open";
6397 #else
6398 "xdg-open";
6399 #endif
6400 char *zCmd;
6401 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
6402 if( system(zCmd) ){
6403 eputf("Failed: [%s]\n", zCmd);
6404 }else{
6405 /* Give the start/open/xdg-open command some time to get
6406 ** going before we continue, and potential delete the
6407 ** p->zTempFile data file out from under it */
6408 sqlite3_sleep(2000);
6410 sqlite3_free(zCmd);
6411 outputModePop(p);
6412 p->doXdgOpen = 0;
6414 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
6416 p->outfile[0] = 0;
6417 p->out = stdout;
6418 setOutputStream(stdout);
6420 #else
6421 # define output_redir(SS,pfO)
6422 # define output_reset(SS)
6423 #endif
6426 ** Run an SQL command and return the single integer result.
6428 static int db_int(sqlite3 *db, const char *zSql){
6429 sqlite3_stmt *pStmt;
6430 int res = 0;
6431 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
6432 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
6433 res = sqlite3_column_int(pStmt,0);
6435 sqlite3_finalize(pStmt);
6436 return res;
6439 #if SQLITE_SHELL_HAVE_RECOVER
6441 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
6443 static unsigned int get2byteInt(unsigned char *a){
6444 return (a[0]<<8) + a[1];
6446 static unsigned int get4byteInt(unsigned char *a){
6447 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
6451 ** Implementation of the ".dbinfo" command.
6453 ** Return 1 on error, 2 to exit, and 0 otherwise.
6455 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
6456 static const struct { const char *zName; int ofst; } aField[] = {
6457 { "file change counter:", 24 },
6458 { "database page count:", 28 },
6459 { "freelist page count:", 36 },
6460 { "schema cookie:", 40 },
6461 { "schema format:", 44 },
6462 { "default cache size:", 48 },
6463 { "autovacuum top root:", 52 },
6464 { "incremental vacuum:", 64 },
6465 { "text encoding:", 56 },
6466 { "user version:", 60 },
6467 { "application id:", 68 },
6468 { "software version:", 96 },
6470 static const struct { const char *zName; const char *zSql; } aQuery[] = {
6471 { "number of tables:",
6472 "SELECT count(*) FROM %s WHERE type='table'" },
6473 { "number of indexes:",
6474 "SELECT count(*) FROM %s WHERE type='index'" },
6475 { "number of triggers:",
6476 "SELECT count(*) FROM %s WHERE type='trigger'" },
6477 { "number of views:",
6478 "SELECT count(*) FROM %s WHERE type='view'" },
6479 { "schema size:",
6480 "SELECT total(length(sql)) FROM %s" },
6482 int i, rc;
6483 unsigned iDataVersion;
6484 char *zSchemaTab;
6485 char *zDb = nArg>=2 ? azArg[1] : "main";
6486 sqlite3_stmt *pStmt = 0;
6487 unsigned char aHdr[100];
6488 open_db(p, 0);
6489 if( p->db==0 ) return 1;
6490 rc = sqlite3_prepare_v2(p->db,
6491 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
6492 -1, &pStmt, 0);
6493 if( rc ){
6494 eputf("error: %s\n", sqlite3_errmsg(p->db));
6495 sqlite3_finalize(pStmt);
6496 return 1;
6498 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
6499 if( sqlite3_step(pStmt)==SQLITE_ROW
6500 && sqlite3_column_bytes(pStmt,0)>100
6502 const u8 *pb = sqlite3_column_blob(pStmt,0);
6503 shell_check_oom(pb);
6504 memcpy(aHdr, pb, 100);
6505 sqlite3_finalize(pStmt);
6506 }else{
6507 eputz("unable to read database header\n");
6508 sqlite3_finalize(pStmt);
6509 return 1;
6511 i = get2byteInt(aHdr+16);
6512 if( i==1 ) i = 65536;
6513 oputf("%-20s %d\n", "database page size:", i);
6514 oputf("%-20s %d\n", "write format:", aHdr[18]);
6515 oputf("%-20s %d\n", "read format:", aHdr[19]);
6516 oputf("%-20s %d\n", "reserved bytes:", aHdr[20]);
6517 for(i=0; i<ArraySize(aField); i++){
6518 int ofst = aField[i].ofst;
6519 unsigned int val = get4byteInt(aHdr + ofst);
6520 oputf("%-20s %u", aField[i].zName, val);
6521 switch( ofst ){
6522 case 56: {
6523 if( val==1 ) oputz(" (utf8)");
6524 if( val==2 ) oputz(" (utf16le)");
6525 if( val==3 ) oputz(" (utf16be)");
6528 oputz("\n");
6530 if( zDb==0 ){
6531 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
6532 }else if( cli_strcmp(zDb,"temp")==0 ){
6533 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
6534 }else{
6535 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
6537 for(i=0; i<ArraySize(aQuery); i++){
6538 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
6539 int val = db_int(p->db, zSql);
6540 sqlite3_free(zSql);
6541 oputf("%-20s %d\n", aQuery[i].zName, val);
6543 sqlite3_free(zSchemaTab);
6544 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6545 oputf("%-20s %u\n", "data version", iDataVersion);
6546 return 0;
6548 #endif /* SQLITE_SHELL_HAVE_RECOVER */
6551 ** Print the given string as an error message.
6553 static void shellEmitError(const char *zErr){
6554 eputf("Error: %s\n", zErr);
6557 ** Print the current sqlite3_errmsg() value to stderr and return 1.
6559 static int shellDatabaseError(sqlite3 *db){
6560 shellEmitError(sqlite3_errmsg(db));
6561 return 1;
6565 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
6566 ** if they match and FALSE (0) if they do not match.
6568 ** Globbing rules:
6570 ** '*' Matches any sequence of zero or more characters.
6572 ** '?' Matches exactly one character.
6574 ** [...] Matches one character from the enclosed list of
6575 ** characters.
6577 ** [^...] Matches one character not in the enclosed list.
6579 ** '#' Matches any sequence of one or more digits with an
6580 ** optional + or - sign in front
6582 ** ' ' Any span of whitespace matches any other span of
6583 ** whitespace.
6585 ** Extra whitespace at the end of z[] is ignored.
6587 static int testcase_glob(const char *zGlob, const char *z){
6588 int c, c2;
6589 int invert;
6590 int seen;
6592 while( (c = (*(zGlob++)))!=0 ){
6593 if( IsSpace(c) ){
6594 if( !IsSpace(*z) ) return 0;
6595 while( IsSpace(*zGlob) ) zGlob++;
6596 while( IsSpace(*z) ) z++;
6597 }else if( c=='*' ){
6598 while( (c=(*(zGlob++))) == '*' || c=='?' ){
6599 if( c=='?' && (*(z++))==0 ) return 0;
6601 if( c==0 ){
6602 return 1;
6603 }else if( c=='[' ){
6604 while( *z && testcase_glob(zGlob-1,z)==0 ){
6605 z++;
6607 return (*z)!=0;
6609 while( (c2 = (*(z++)))!=0 ){
6610 while( c2!=c ){
6611 c2 = *(z++);
6612 if( c2==0 ) return 0;
6614 if( testcase_glob(zGlob,z) ) return 1;
6616 return 0;
6617 }else if( c=='?' ){
6618 if( (*(z++))==0 ) return 0;
6619 }else if( c=='[' ){
6620 int prior_c = 0;
6621 seen = 0;
6622 invert = 0;
6623 c = *(z++);
6624 if( c==0 ) return 0;
6625 c2 = *(zGlob++);
6626 if( c2=='^' ){
6627 invert = 1;
6628 c2 = *(zGlob++);
6630 if( c2==']' ){
6631 if( c==']' ) seen = 1;
6632 c2 = *(zGlob++);
6634 while( c2 && c2!=']' ){
6635 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6636 c2 = *(zGlob++);
6637 if( c>=prior_c && c<=c2 ) seen = 1;
6638 prior_c = 0;
6639 }else{
6640 if( c==c2 ){
6641 seen = 1;
6643 prior_c = c2;
6645 c2 = *(zGlob++);
6647 if( c2==0 || (seen ^ invert)==0 ) return 0;
6648 }else if( c=='#' ){
6649 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6650 if( !IsDigit(z[0]) ) return 0;
6651 z++;
6652 while( IsDigit(z[0]) ){ z++; }
6653 }else{
6654 if( c!=(*(z++)) ) return 0;
6657 while( IsSpace(*z) ){ z++; }
6658 return *z==0;
6663 ** Compare the string as a command-line option with either one or two
6664 ** initial "-" characters.
6666 static int optionMatch(const char *zStr, const char *zOpt){
6667 if( zStr[0]!='-' ) return 0;
6668 zStr++;
6669 if( zStr[0]=='-' ) zStr++;
6670 return cli_strcmp(zStr, zOpt)==0;
6674 ** Delete a file.
6676 int shellDeleteFile(const char *zFilename){
6677 int rc;
6678 #ifdef _WIN32
6679 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6680 rc = _wunlink(z);
6681 sqlite3_free(z);
6682 #else
6683 rc = unlink(zFilename);
6684 #endif
6685 return rc;
6689 ** Try to delete the temporary file (if there is one) and free the
6690 ** memory used to hold the name of the temp file.
6692 static void clearTempFile(ShellState *p){
6693 if( p->zTempFile==0 ) return;
6694 if( p->doXdgOpen ) return;
6695 if( shellDeleteFile(p->zTempFile) ) return;
6696 sqlite3_free(p->zTempFile);
6697 p->zTempFile = 0;
6701 ** Create a new temp file name with the given suffix.
6703 static void newTempFile(ShellState *p, const char *zSuffix){
6704 clearTempFile(p);
6705 sqlite3_free(p->zTempFile);
6706 p->zTempFile = 0;
6707 if( p->db ){
6708 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6710 if( p->zTempFile==0 ){
6711 /* If p->db is an in-memory database then the TEMPFILENAME file-control
6712 ** will not work and we will need to fallback to guessing */
6713 char *zTemp;
6714 sqlite3_uint64 r;
6715 sqlite3_randomness(sizeof(r), &r);
6716 zTemp = getenv("TEMP");
6717 if( zTemp==0 ) zTemp = getenv("TMP");
6718 if( zTemp==0 ){
6719 #ifdef _WIN32
6720 zTemp = "\\tmp";
6721 #else
6722 zTemp = "/tmp";
6723 #endif
6725 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6726 }else{
6727 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6729 shell_check_oom(p->zTempFile);
6734 ** The implementation of SQL scalar function fkey_collate_clause(), used
6735 ** by the ".lint fkey-indexes" command. This scalar function is always
6736 ** called with four arguments - the parent table name, the parent column name,
6737 ** the child table name and the child column name.
6739 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6741 ** If either of the named tables or columns do not exist, this function
6742 ** returns an empty string. An empty string is also returned if both tables
6743 ** and columns exist but have the same default collation sequence. Or,
6744 ** if both exist but the default collation sequences are different, this
6745 ** function returns the string " COLLATE <parent-collation>", where
6746 ** <parent-collation> is the default collation sequence of the parent column.
6748 static void shellFkeyCollateClause(
6749 sqlite3_context *pCtx,
6750 int nVal,
6751 sqlite3_value **apVal
6753 sqlite3 *db = sqlite3_context_db_handle(pCtx);
6754 const char *zParent;
6755 const char *zParentCol;
6756 const char *zParentSeq;
6757 const char *zChild;
6758 const char *zChildCol;
6759 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
6760 int rc;
6762 assert( nVal==4 );
6763 zParent = (const char*)sqlite3_value_text(apVal[0]);
6764 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6765 zChild = (const char*)sqlite3_value_text(apVal[2]);
6766 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6768 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6769 rc = sqlite3_table_column_metadata(
6770 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6772 if( rc==SQLITE_OK ){
6773 rc = sqlite3_table_column_metadata(
6774 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6778 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6779 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6780 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6781 sqlite3_free(z);
6787 ** The implementation of dot-command ".lint fkey-indexes".
6789 static int lintFkeyIndexes(
6790 ShellState *pState, /* Current shell tool state */
6791 char **azArg, /* Array of arguments passed to dot command */
6792 int nArg /* Number of entries in azArg[] */
6794 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
6795 int bVerbose = 0; /* If -verbose is present */
6796 int bGroupByParent = 0; /* If -groupbyparent is present */
6797 int i; /* To iterate through azArg[] */
6798 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
6799 int rc; /* Return code */
6800 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
6803 ** This SELECT statement returns one row for each foreign key constraint
6804 ** in the schema of the main database. The column values are:
6806 ** 0. The text of an SQL statement similar to:
6808 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6810 ** This SELECT is similar to the one that the foreign keys implementation
6811 ** needs to run internally on child tables. If there is an index that can
6812 ** be used to optimize this query, then it can also be used by the FK
6813 ** implementation to optimize DELETE or UPDATE statements on the parent
6814 ** table.
6816 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6817 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6818 ** contains an index that can be used to optimize the query.
6820 ** 2. Human readable text that describes the child table and columns. e.g.
6822 ** "child_table(child_key1, child_key2)"
6824 ** 3. Human readable text that describes the parent table and columns. e.g.
6826 ** "parent_table(parent_key1, parent_key2)"
6828 ** 4. A full CREATE INDEX statement for an index that could be used to
6829 ** optimize DELETE or UPDATE statements on the parent table. e.g.
6831 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
6833 ** 5. The name of the parent table.
6835 ** These six values are used by the C logic below to generate the report.
6837 const char *zSql =
6838 "SELECT "
6839 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6840 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6841 " || fkey_collate_clause("
6842 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6843 ", "
6844 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6845 " || group_concat('*=?', ' AND ') || ')'"
6846 ", "
6847 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
6848 ", "
6849 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6850 ", "
6851 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6852 " || ' ON ' || quote(s.name) || '('"
6853 " || group_concat(quote(f.[from]) ||"
6854 " fkey_collate_clause("
6855 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6856 " || ');'"
6857 ", "
6858 " f.[table] "
6859 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6860 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6861 "GROUP BY s.name, f.id "
6862 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6864 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6866 for(i=2; i<nArg; i++){
6867 int n = strlen30(azArg[i]);
6868 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6869 bVerbose = 1;
6871 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6872 bGroupByParent = 1;
6873 zIndent = " ";
6875 else{
6876 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
6877 return SQLITE_ERROR;
6881 /* Register the fkey_collate_clause() SQL function */
6882 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6883 0, shellFkeyCollateClause, 0, 0
6887 if( rc==SQLITE_OK ){
6888 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6890 if( rc==SQLITE_OK ){
6891 sqlite3_bind_int(pSql, 1, bGroupByParent);
6894 if( rc==SQLITE_OK ){
6895 int rc2;
6896 char *zPrev = 0;
6897 while( SQLITE_ROW==sqlite3_step(pSql) ){
6898 int res = -1;
6899 sqlite3_stmt *pExplain = 0;
6900 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6901 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6902 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6903 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6904 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6905 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6907 if( zEQP==0 ) continue;
6908 if( zGlob==0 ) continue;
6909 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6910 if( rc!=SQLITE_OK ) break;
6911 if( SQLITE_ROW==sqlite3_step(pExplain) ){
6912 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6913 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
6914 || 0==sqlite3_strglob(zGlobIPK, zPlan));
6916 rc = sqlite3_finalize(pExplain);
6917 if( rc!=SQLITE_OK ) break;
6919 if( res<0 ){
6920 eputz("Error: internal error");
6921 break;
6922 }else{
6923 if( bGroupByParent
6924 && (bVerbose || res==0)
6925 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6927 oputf("-- Parent table %s\n", zParent);
6928 sqlite3_free(zPrev);
6929 zPrev = sqlite3_mprintf("%s", zParent);
6932 if( res==0 ){
6933 oputf("%s%s --> %s\n", zIndent, zCI, zTarget);
6934 }else if( bVerbose ){
6935 oputf("%s/* no extra indexes required for %s -> %s */\n",
6936 zIndent, zFrom, zTarget
6941 sqlite3_free(zPrev);
6943 if( rc!=SQLITE_OK ){
6944 eputf("%s\n", sqlite3_errmsg(db));
6947 rc2 = sqlite3_finalize(pSql);
6948 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
6949 rc = rc2;
6950 eputf("%s\n", sqlite3_errmsg(db));
6952 }else{
6953 eputf("%s\n", sqlite3_errmsg(db));
6956 return rc;
6960 ** Implementation of ".lint" dot command.
6962 static int lintDotCommand(
6963 ShellState *pState, /* Current shell tool state */
6964 char **azArg, /* Array of arguments passed to dot command */
6965 int nArg /* Number of entries in azArg[] */
6967 int n;
6968 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
6969 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
6970 return lintFkeyIndexes(pState, azArg, nArg);
6972 usage:
6973 eputf("Usage %s sub-command ?switches...?\n", azArg[0]);
6974 eputz("Where sub-commands are:\n");
6975 eputz(" fkey-indexes\n");
6976 return SQLITE_ERROR;
6979 static void shellPrepare(
6980 sqlite3 *db,
6981 int *pRc,
6982 const char *zSql,
6983 sqlite3_stmt **ppStmt
6985 *ppStmt = 0;
6986 if( *pRc==SQLITE_OK ){
6987 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6988 if( rc!=SQLITE_OK ){
6989 eputf("sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
6990 *pRc = rc;
6996 ** Create a prepared statement using printf-style arguments for the SQL.
6998 static void shellPreparePrintf(
6999 sqlite3 *db,
7000 int *pRc,
7001 sqlite3_stmt **ppStmt,
7002 const char *zFmt,
7005 *ppStmt = 0;
7006 if( *pRc==SQLITE_OK ){
7007 va_list ap;
7008 char *z;
7009 va_start(ap, zFmt);
7010 z = sqlite3_vmprintf(zFmt, ap);
7011 va_end(ap);
7012 if( z==0 ){
7013 *pRc = SQLITE_NOMEM;
7014 }else{
7015 shellPrepare(db, pRc, z, ppStmt);
7016 sqlite3_free(z);
7022 ** Finalize the prepared statement created using shellPreparePrintf().
7024 static void shellFinalize(
7025 int *pRc,
7026 sqlite3_stmt *pStmt
7028 if( pStmt ){
7029 sqlite3 *db = sqlite3_db_handle(pStmt);
7030 int rc = sqlite3_finalize(pStmt);
7031 if( *pRc==SQLITE_OK ){
7032 if( rc!=SQLITE_OK ){
7033 eputf("SQL error: %s\n", sqlite3_errmsg(db));
7035 *pRc = rc;
7040 #if !defined SQLITE_OMIT_VIRTUALTABLE
7041 /* Reset the prepared statement created using shellPreparePrintf().
7043 ** This routine is could be marked "static". But it is not always used,
7044 ** depending on compile-time options. By omitting the "static", we avoid
7045 ** nuisance compiler warnings about "defined but not used".
7047 void shellReset(
7048 int *pRc,
7049 sqlite3_stmt *pStmt
7051 int rc = sqlite3_reset(pStmt);
7052 if( *pRc==SQLITE_OK ){
7053 if( rc!=SQLITE_OK ){
7054 sqlite3 *db = sqlite3_db_handle(pStmt);
7055 eputf("SQL error: %s\n", sqlite3_errmsg(db));
7057 *pRc = rc;
7060 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
7062 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7063 /******************************************************************************
7064 ** The ".archive" or ".ar" command.
7067 ** Structure representing a single ".ar" command.
7069 typedef struct ArCommand ArCommand;
7070 struct ArCommand {
7071 u8 eCmd; /* An AR_CMD_* value */
7072 u8 bVerbose; /* True if --verbose */
7073 u8 bZip; /* True if the archive is a ZIP */
7074 u8 bDryRun; /* True if --dry-run */
7075 u8 bAppend; /* True if --append */
7076 u8 bGlob; /* True if --glob */
7077 u8 fromCmdLine; /* Run from -A instead of .archive */
7078 int nArg; /* Number of command arguments */
7079 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
7080 const char *zFile; /* --file argument, or NULL */
7081 const char *zDir; /* --directory argument, or NULL */
7082 char **azArg; /* Array of command arguments */
7083 ShellState *p; /* Shell state */
7084 sqlite3 *db; /* Database containing the archive */
7088 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
7090 static int arUsage(FILE *f){
7091 showHelp(f,"archive");
7092 return SQLITE_ERROR;
7096 ** Print an error message for the .ar command to stderr and return
7097 ** SQLITE_ERROR.
7099 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
7100 va_list ap;
7101 char *z;
7102 va_start(ap, zFmt);
7103 z = sqlite3_vmprintf(zFmt, ap);
7104 va_end(ap);
7105 shellEmitError(z);
7106 if( pAr->fromCmdLine ){
7107 eputz("Use \"-A\" for more help\n");
7108 }else{
7109 eputz("Use \".archive --help\" for more help\n");
7111 sqlite3_free(z);
7112 return SQLITE_ERROR;
7116 ** Values for ArCommand.eCmd.
7118 #define AR_CMD_CREATE 1
7119 #define AR_CMD_UPDATE 2
7120 #define AR_CMD_INSERT 3
7121 #define AR_CMD_EXTRACT 4
7122 #define AR_CMD_LIST 5
7123 #define AR_CMD_HELP 6
7124 #define AR_CMD_REMOVE 7
7127 ** Other (non-command) switches.
7129 #define AR_SWITCH_VERBOSE 8
7130 #define AR_SWITCH_FILE 9
7131 #define AR_SWITCH_DIRECTORY 10
7132 #define AR_SWITCH_APPEND 11
7133 #define AR_SWITCH_DRYRUN 12
7134 #define AR_SWITCH_GLOB 13
7136 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
7137 switch( eSwitch ){
7138 case AR_CMD_CREATE:
7139 case AR_CMD_EXTRACT:
7140 case AR_CMD_LIST:
7141 case AR_CMD_REMOVE:
7142 case AR_CMD_UPDATE:
7143 case AR_CMD_INSERT:
7144 case AR_CMD_HELP:
7145 if( pAr->eCmd ){
7146 return arErrorMsg(pAr, "multiple command options");
7148 pAr->eCmd = eSwitch;
7149 break;
7151 case AR_SWITCH_DRYRUN:
7152 pAr->bDryRun = 1;
7153 break;
7154 case AR_SWITCH_GLOB:
7155 pAr->bGlob = 1;
7156 break;
7157 case AR_SWITCH_VERBOSE:
7158 pAr->bVerbose = 1;
7159 break;
7160 case AR_SWITCH_APPEND:
7161 pAr->bAppend = 1;
7162 deliberate_fall_through;
7163 case AR_SWITCH_FILE:
7164 pAr->zFile = zArg;
7165 break;
7166 case AR_SWITCH_DIRECTORY:
7167 pAr->zDir = zArg;
7168 break;
7171 return SQLITE_OK;
7175 ** Parse the command line for an ".ar" command. The results are written into
7176 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
7177 ** successfully, otherwise an error message is written to stderr and
7178 ** SQLITE_ERROR returned.
7180 static int arParseCommand(
7181 char **azArg, /* Array of arguments passed to dot command */
7182 int nArg, /* Number of entries in azArg[] */
7183 ArCommand *pAr /* Populate this object */
7185 struct ArSwitch {
7186 const char *zLong;
7187 char cShort;
7188 u8 eSwitch;
7189 u8 bArg;
7190 } aSwitch[] = {
7191 { "create", 'c', AR_CMD_CREATE, 0 },
7192 { "extract", 'x', AR_CMD_EXTRACT, 0 },
7193 { "insert", 'i', AR_CMD_INSERT, 0 },
7194 { "list", 't', AR_CMD_LIST, 0 },
7195 { "remove", 'r', AR_CMD_REMOVE, 0 },
7196 { "update", 'u', AR_CMD_UPDATE, 0 },
7197 { "help", 'h', AR_CMD_HELP, 0 },
7198 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
7199 { "file", 'f', AR_SWITCH_FILE, 1 },
7200 { "append", 'a', AR_SWITCH_APPEND, 1 },
7201 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
7202 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
7203 { "glob", 'g', AR_SWITCH_GLOB, 0 },
7205 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
7206 struct ArSwitch *pEnd = &aSwitch[nSwitch];
7208 if( nArg<=1 ){
7209 eputz("Wrong number of arguments. Usage:\n");
7210 return arUsage(stderr);
7211 }else{
7212 char *z = azArg[1];
7213 if( z[0]!='-' ){
7214 /* Traditional style [tar] invocation */
7215 int i;
7216 int iArg = 2;
7217 for(i=0; z[i]; i++){
7218 const char *zArg = 0;
7219 struct ArSwitch *pOpt;
7220 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7221 if( z[i]==pOpt->cShort ) break;
7223 if( pOpt==pEnd ){
7224 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7226 if( pOpt->bArg ){
7227 if( iArg>=nArg ){
7228 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
7230 zArg = azArg[iArg++];
7232 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7234 pAr->nArg = nArg-iArg;
7235 if( pAr->nArg>0 ){
7236 pAr->azArg = &azArg[iArg];
7238 }else{
7239 /* Non-traditional invocation */
7240 int iArg;
7241 for(iArg=1; iArg<nArg; iArg++){
7242 int n;
7243 z = azArg[iArg];
7244 if( z[0]!='-' ){
7245 /* All remaining command line words are command arguments. */
7246 pAr->azArg = &azArg[iArg];
7247 pAr->nArg = nArg-iArg;
7248 break;
7250 n = strlen30(z);
7252 if( z[1]!='-' ){
7253 int i;
7254 /* One or more short options */
7255 for(i=1; i<n; i++){
7256 const char *zArg = 0;
7257 struct ArSwitch *pOpt;
7258 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7259 if( z[i]==pOpt->cShort ) break;
7261 if( pOpt==pEnd ){
7262 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7264 if( pOpt->bArg ){
7265 if( i<(n-1) ){
7266 zArg = &z[i+1];
7267 i = n;
7268 }else{
7269 if( iArg>=(nArg-1) ){
7270 return arErrorMsg(pAr, "option requires an argument: %c",
7271 z[i]);
7273 zArg = azArg[++iArg];
7276 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7278 }else if( z[2]=='\0' ){
7279 /* A -- option, indicating that all remaining command line words
7280 ** are command arguments. */
7281 pAr->azArg = &azArg[iArg+1];
7282 pAr->nArg = nArg-iArg-1;
7283 break;
7284 }else{
7285 /* A long option */
7286 const char *zArg = 0; /* Argument for option, if any */
7287 struct ArSwitch *pMatch = 0; /* Matching option */
7288 struct ArSwitch *pOpt; /* Iterator */
7289 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7290 const char *zLong = pOpt->zLong;
7291 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
7292 if( pMatch ){
7293 return arErrorMsg(pAr, "ambiguous option: %s",z);
7294 }else{
7295 pMatch = pOpt;
7300 if( pMatch==0 ){
7301 return arErrorMsg(pAr, "unrecognized option: %s", z);
7303 if( pMatch->bArg ){
7304 if( iArg>=(nArg-1) ){
7305 return arErrorMsg(pAr, "option requires an argument: %s", z);
7307 zArg = azArg[++iArg];
7309 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
7314 if( pAr->eCmd==0 ){
7315 eputz("Required argument missing. Usage:\n");
7316 return arUsage(stderr);
7318 return SQLITE_OK;
7322 ** This function assumes that all arguments within the ArCommand.azArg[]
7323 ** array refer to archive members, as for the --extract, --list or --remove
7324 ** commands. It checks that each of them are "present". If any specified
7325 ** file is not present in the archive, an error is printed to stderr and an
7326 ** error code returned. Otherwise, if all specified arguments are present
7327 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
7328 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
7329 ** when pAr->bGlob is true.
7331 ** This function strips any trailing '/' characters from each argument.
7332 ** This is consistent with the way the [tar] command seems to work on
7333 ** Linux.
7335 static int arCheckEntries(ArCommand *pAr){
7336 int rc = SQLITE_OK;
7337 if( pAr->nArg ){
7338 int i, j;
7339 sqlite3_stmt *pTest = 0;
7340 const char *zSel = (pAr->bGlob)
7341 ? "SELECT name FROM %s WHERE glob($name,name)"
7342 : "SELECT name FROM %s WHERE name=$name";
7344 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
7345 j = sqlite3_bind_parameter_index(pTest, "$name");
7346 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7347 char *z = pAr->azArg[i];
7348 int n = strlen30(z);
7349 int bOk = 0;
7350 while( n>0 && z[n-1]=='/' ) n--;
7351 z[n] = '\0';
7352 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
7353 if( SQLITE_ROW==sqlite3_step(pTest) ){
7354 bOk = 1;
7356 shellReset(&rc, pTest);
7357 if( rc==SQLITE_OK && bOk==0 ){
7358 eputf("not found in archive: %s\n", z);
7359 rc = SQLITE_ERROR;
7362 shellFinalize(&rc, pTest);
7364 return rc;
7368 ** Format a WHERE clause that can be used against the "sqlar" table to
7369 ** identify all archive members that match the command arguments held
7370 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
7371 ** The caller is responsible for eventually calling sqlite3_free() on
7372 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
7373 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
7375 static void arWhereClause(
7376 int *pRc,
7377 ArCommand *pAr,
7378 char **pzWhere /* OUT: New WHERE clause */
7380 char *zWhere = 0;
7381 const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
7382 if( *pRc==SQLITE_OK ){
7383 if( pAr->nArg==0 ){
7384 zWhere = sqlite3_mprintf("1");
7385 }else{
7386 int i;
7387 const char *zSep = "";
7388 for(i=0; i<pAr->nArg; i++){
7389 const char *z = pAr->azArg[i];
7390 zWhere = sqlite3_mprintf(
7391 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
7392 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
7394 if( zWhere==0 ){
7395 *pRc = SQLITE_NOMEM;
7396 break;
7398 zSep = " OR ";
7402 *pzWhere = zWhere;
7406 ** Implementation of .ar "lisT" command.
7408 static int arListCommand(ArCommand *pAr){
7409 const char *zSql = "SELECT %s FROM %s WHERE %s";
7410 const char *azCols[] = {
7411 "name",
7412 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
7415 char *zWhere = 0;
7416 sqlite3_stmt *pSql = 0;
7417 int rc;
7419 rc = arCheckEntries(pAr);
7420 arWhereClause(&rc, pAr, &zWhere);
7422 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
7423 pAr->zSrcTable, zWhere);
7424 if( pAr->bDryRun ){
7425 oputf("%s\n", sqlite3_sql(pSql));
7426 }else{
7427 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7428 if( pAr->bVerbose ){
7429 oputf("%s % 10d %s %s\n",
7430 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
7431 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
7432 }else{
7433 oputf("%s\n", sqlite3_column_text(pSql, 0));
7437 shellFinalize(&rc, pSql);
7438 sqlite3_free(zWhere);
7439 return rc;
7443 ** Implementation of .ar "Remove" command.
7445 static int arRemoveCommand(ArCommand *pAr){
7446 int rc = 0;
7447 char *zSql = 0;
7448 char *zWhere = 0;
7450 if( pAr->nArg ){
7451 /* Verify that args actually exist within the archive before proceeding.
7452 ** And formulate a WHERE clause to match them. */
7453 rc = arCheckEntries(pAr);
7454 arWhereClause(&rc, pAr, &zWhere);
7456 if( rc==SQLITE_OK ){
7457 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
7458 pAr->zSrcTable, zWhere);
7459 if( pAr->bDryRun ){
7460 oputf("%s\n", zSql);
7461 }else{
7462 char *zErr = 0;
7463 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
7464 if( rc==SQLITE_OK ){
7465 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7466 if( rc!=SQLITE_OK ){
7467 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7468 }else{
7469 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
7472 if( zErr ){
7473 sputf(stdout, "ERROR: %s\n", zErr); /* stdout? */
7474 sqlite3_free(zErr);
7478 sqlite3_free(zWhere);
7479 sqlite3_free(zSql);
7480 return rc;
7484 ** Implementation of .ar "eXtract" command.
7486 static int arExtractCommand(ArCommand *pAr){
7487 const char *zSql1 =
7488 "SELECT "
7489 " ($dir || name),"
7490 " writefile(($dir || name), %s, mode, mtime) "
7491 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
7492 " AND name NOT GLOB '*..[/\\]*'";
7494 const char *azExtraArg[] = {
7495 "sqlar_uncompress(data, sz)",
7496 "data"
7499 sqlite3_stmt *pSql = 0;
7500 int rc = SQLITE_OK;
7501 char *zDir = 0;
7502 char *zWhere = 0;
7503 int i, j;
7505 /* If arguments are specified, check that they actually exist within
7506 ** the archive before proceeding. And formulate a WHERE clause to
7507 ** match them. */
7508 rc = arCheckEntries(pAr);
7509 arWhereClause(&rc, pAr, &zWhere);
7511 if( rc==SQLITE_OK ){
7512 if( pAr->zDir ){
7513 zDir = sqlite3_mprintf("%s/", pAr->zDir);
7514 }else{
7515 zDir = sqlite3_mprintf("");
7517 if( zDir==0 ) rc = SQLITE_NOMEM;
7520 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
7521 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
7524 if( rc==SQLITE_OK ){
7525 j = sqlite3_bind_parameter_index(pSql, "$dir");
7526 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
7528 /* Run the SELECT statement twice. The first time, writefile() is called
7529 ** for all archive members that should be extracted. The second time,
7530 ** only for the directories. This is because the timestamps for
7531 ** extracted directories must be reset after they are populated (as
7532 ** populating them changes the timestamp). */
7533 for(i=0; i<2; i++){
7534 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7535 sqlite3_bind_int(pSql, j, i);
7536 if( pAr->bDryRun ){
7537 oputf("%s\n", sqlite3_sql(pSql));
7538 }else{
7539 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7540 if( i==0 && pAr->bVerbose ){
7541 oputf("%s\n", sqlite3_column_text(pSql, 0));
7545 shellReset(&rc, pSql);
7547 shellFinalize(&rc, pSql);
7550 sqlite3_free(zDir);
7551 sqlite3_free(zWhere);
7552 return rc;
7556 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
7558 static int arExecSql(ArCommand *pAr, const char *zSql){
7559 int rc;
7560 if( pAr->bDryRun ){
7561 oputf("%s\n", zSql);
7562 rc = SQLITE_OK;
7563 }else{
7564 char *zErr = 0;
7565 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7566 if( zErr ){
7567 sputf(stdout, "ERROR: %s\n", zErr);
7568 sqlite3_free(zErr);
7571 return rc;
7576 ** Implementation of .ar "create", "insert", and "update" commands.
7578 ** create -> Create a new SQL archive
7579 ** insert -> Insert or reinsert all files listed
7580 ** update -> Insert files that have changed or that were not
7581 ** previously in the archive
7583 ** Create the "sqlar" table in the database if it does not already exist.
7584 ** Then add each file in the azFile[] array to the archive. Directories
7585 ** are added recursively. If argument bVerbose is non-zero, a message is
7586 ** printed on stdout for each file archived.
7588 ** The create command is the same as update, except that it drops
7589 ** any existing "sqlar" table before beginning. The "insert" command
7590 ** always overwrites every file named on the command-line, where as
7591 ** "update" only overwrites if the size or mtime or mode has changed.
7593 static int arCreateOrUpdateCommand(
7594 ArCommand *pAr, /* Command arguments and options */
7595 int bUpdate, /* true for a --create. */
7596 int bOnlyIfChanged /* Only update if file has changed */
7598 const char *zCreate =
7599 "CREATE TABLE IF NOT EXISTS sqlar(\n"
7600 " name TEXT PRIMARY KEY, -- name of the file\n"
7601 " mode INT, -- access permissions\n"
7602 " mtime INT, -- last modification time\n"
7603 " sz INT, -- original file size\n"
7604 " data BLOB -- compressed content\n"
7605 ")";
7606 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7607 const char *zInsertFmt[2] = {
7608 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7609 " SELECT\n"
7610 " %s,\n"
7611 " mode,\n"
7612 " mtime,\n"
7613 " CASE substr(lsmode(mode),1,1)\n"
7614 " WHEN '-' THEN length(data)\n"
7615 " WHEN 'd' THEN 0\n"
7616 " ELSE -1 END,\n"
7617 " sqlar_compress(data)\n"
7618 " FROM fsdir(%Q,%Q) AS disk\n"
7619 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7621 "REPLACE INTO %s(name,mode,mtime,data)\n"
7622 " SELECT\n"
7623 " %s,\n"
7624 " mode,\n"
7625 " mtime,\n"
7626 " data\n"
7627 " FROM fsdir(%Q,%Q) AS disk\n"
7628 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7630 int i; /* For iterating through azFile[] */
7631 int rc; /* Return code */
7632 const char *zTab = 0; /* SQL table into which to insert */
7633 char *zSql;
7634 char zTemp[50];
7635 char *zExists = 0;
7637 arExecSql(pAr, "PRAGMA page_size=512");
7638 rc = arExecSql(pAr, "SAVEPOINT ar;");
7639 if( rc!=SQLITE_OK ) return rc;
7640 zTemp[0] = 0;
7641 if( pAr->bZip ){
7642 /* Initialize the zipfile virtual table, if necessary */
7643 if( pAr->zFile ){
7644 sqlite3_uint64 r;
7645 sqlite3_randomness(sizeof(r),&r);
7646 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7647 zTab = zTemp;
7648 zSql = sqlite3_mprintf(
7649 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7650 zTab, pAr->zFile
7652 rc = arExecSql(pAr, zSql);
7653 sqlite3_free(zSql);
7654 }else{
7655 zTab = "zip";
7657 }else{
7658 /* Initialize the table for an SQLAR */
7659 zTab = "sqlar";
7660 if( bUpdate==0 ){
7661 rc = arExecSql(pAr, zDrop);
7662 if( rc!=SQLITE_OK ) goto end_ar_transaction;
7664 rc = arExecSql(pAr, zCreate);
7666 if( bOnlyIfChanged ){
7667 zExists = sqlite3_mprintf(
7668 " AND NOT EXISTS("
7669 "SELECT 1 FROM %s AS mem"
7670 " WHERE mem.name=disk.name"
7671 " AND mem.mtime=disk.mtime"
7672 " AND mem.mode=disk.mode)", zTab);
7673 }else{
7674 zExists = sqlite3_mprintf("");
7676 if( zExists==0 ) rc = SQLITE_NOMEM;
7677 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7678 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7679 pAr->bVerbose ? "shell_putsnl(name)" : "name",
7680 pAr->azArg[i], pAr->zDir, zExists);
7681 rc = arExecSql(pAr, zSql2);
7682 sqlite3_free(zSql2);
7684 end_ar_transaction:
7685 if( rc!=SQLITE_OK ){
7686 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7687 }else{
7688 rc = arExecSql(pAr, "RELEASE ar;");
7689 if( pAr->bZip && pAr->zFile ){
7690 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7691 arExecSql(pAr, zSql);
7692 sqlite3_free(zSql);
7695 sqlite3_free(zExists);
7696 return rc;
7700 ** Implementation of ".ar" dot command.
7702 static int arDotCommand(
7703 ShellState *pState, /* Current shell tool state */
7704 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
7705 char **azArg, /* Array of arguments passed to dot command */
7706 int nArg /* Number of entries in azArg[] */
7708 ArCommand cmd;
7709 int rc;
7710 memset(&cmd, 0, sizeof(cmd));
7711 cmd.fromCmdLine = fromCmdLine;
7712 rc = arParseCommand(azArg, nArg, &cmd);
7713 if( rc==SQLITE_OK ){
7714 int eDbType = SHELL_OPEN_UNSPEC;
7715 cmd.p = pState;
7716 cmd.db = pState->db;
7717 if( cmd.zFile ){
7718 eDbType = deduceDatabaseType(cmd.zFile, 1);
7719 }else{
7720 eDbType = pState->openMode;
7722 if( eDbType==SHELL_OPEN_ZIPFILE ){
7723 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7724 if( cmd.zFile==0 ){
7725 cmd.zSrcTable = sqlite3_mprintf("zip");
7726 }else{
7727 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7730 cmd.bZip = 1;
7731 }else if( cmd.zFile ){
7732 int flags;
7733 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7734 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7735 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7736 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7737 }else{
7738 flags = SQLITE_OPEN_READONLY;
7740 cmd.db = 0;
7741 if( cmd.bDryRun ){
7742 oputf("-- open database '%s'%s\n", cmd.zFile,
7743 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7745 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7746 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7747 if( rc!=SQLITE_OK ){
7748 eputf("cannot open file: %s (%s)\n", cmd.zFile, sqlite3_errmsg(cmd.db));
7749 goto end_ar_command;
7751 sqlite3_fileio_init(cmd.db, 0, 0);
7752 sqlite3_sqlar_init(cmd.db, 0, 0);
7753 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7754 shellPutsFunc, 0, 0);
7757 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7758 if( cmd.eCmd!=AR_CMD_CREATE
7759 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7761 eputz("database does not contain an 'sqlar' table\n");
7762 rc = SQLITE_ERROR;
7763 goto end_ar_command;
7765 cmd.zSrcTable = sqlite3_mprintf("sqlar");
7768 switch( cmd.eCmd ){
7769 case AR_CMD_CREATE:
7770 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7771 break;
7773 case AR_CMD_EXTRACT:
7774 rc = arExtractCommand(&cmd);
7775 break;
7777 case AR_CMD_LIST:
7778 rc = arListCommand(&cmd);
7779 break;
7781 case AR_CMD_HELP:
7782 arUsage(pState->out);
7783 break;
7785 case AR_CMD_INSERT:
7786 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7787 break;
7789 case AR_CMD_REMOVE:
7790 rc = arRemoveCommand(&cmd);
7791 break;
7793 default:
7794 assert( cmd.eCmd==AR_CMD_UPDATE );
7795 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7796 break;
7799 end_ar_command:
7800 if( cmd.db!=pState->db ){
7801 close_db(cmd.db);
7803 sqlite3_free(cmd.zSrcTable);
7805 return rc;
7807 /* End of the ".archive" or ".ar" command logic
7808 *******************************************************************************/
7809 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7811 #if SQLITE_SHELL_HAVE_RECOVER
7814 ** This function is used as a callback by the recover extension. Simply
7815 ** print the supplied SQL statement to stdout.
7817 static int recoverSqlCb(void *pCtx, const char *zSql){
7818 ShellState *pState = (ShellState*)pCtx;
7819 sputf(pState->out, "%s;\n", zSql);
7820 return SQLITE_OK;
7824 ** This function is called to recover data from the database. A script
7825 ** to construct a new database containing all recovered data is output
7826 ** on stream pState->out.
7828 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7829 int rc = SQLITE_OK;
7830 const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
7831 const char *zLAF = "lost_and_found";
7832 int bFreelist = 1; /* 0 if --ignore-freelist is specified */
7833 int bRowids = 1; /* 0 if --no-rowids */
7834 sqlite3_recover *p = 0;
7835 int i = 0;
7837 for(i=1; i<nArg; i++){
7838 char *z = azArg[i];
7839 int n;
7840 if( z[0]=='-' && z[1]=='-' ) z++;
7841 n = strlen30(z);
7842 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
7843 bFreelist = 0;
7844 }else
7845 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7846 /* This option determines the name of the ATTACH-ed database used
7847 ** internally by the recovery extension. The default is "" which
7848 ** means to use a temporary database that is automatically deleted
7849 ** when closed. This option is undocumented and might disappear at
7850 ** any moment. */
7851 i++;
7852 zRecoveryDb = azArg[i];
7853 }else
7854 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7855 i++;
7856 zLAF = azArg[i];
7857 }else
7858 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7859 bRowids = 0;
7861 else{
7862 eputf("unexpected option: %s\n", azArg[i]);
7863 showHelp(pState->out, azArg[0]);
7864 return 1;
7868 p = sqlite3_recover_init_sql(
7869 pState->db, "main", recoverSqlCb, (void*)pState
7872 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
7873 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
7874 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
7875 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
7877 sqlite3_recover_run(p);
7878 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
7879 const char *zErr = sqlite3_recover_errmsg(p);
7880 int errCode = sqlite3_recover_errcode(p);
7881 eputf("sql error: %s (%d)\n", zErr, errCode);
7883 rc = sqlite3_recover_finish(p);
7884 return rc;
7886 #endif /* SQLITE_SHELL_HAVE_RECOVER */
7889 ** Implementation of ".intck STEPS_PER_UNLOCK" command.
7891 static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
7892 sqlite3_intck *p = 0;
7893 int rc = SQLITE_OK;
7895 rc = sqlite3_intck_open(pState->db, "main", &p);
7896 if( rc==SQLITE_OK ){
7897 i64 nStep = 0;
7898 i64 nError = 0;
7899 const char *zErr = 0;
7900 while( SQLITE_OK==sqlite3_intck_step(p) ){
7901 const char *zMsg = sqlite3_intck_message(p);
7902 if( zMsg ){
7903 oputf("%s\n", zMsg);
7904 nError++;
7906 nStep++;
7907 if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
7908 sqlite3_intck_unlock(p);
7911 rc = sqlite3_intck_error(p, &zErr);
7912 if( zErr ){
7913 eputf("%s\n", zErr);
7915 sqlite3_intck_close(p);
7917 oputf("%lld steps, %lld errors\n", nStep, nError);
7920 return rc;
7924 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7925 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7926 * close db and set it to 0, and return the columns spec, to later
7927 * be sqlite3_free()'ed by the caller.
7928 * The return is 0 when either:
7929 * (a) The db was not initialized and zCol==0 (There are no columns.)
7930 * (b) zCol!=0 (Column was added, db initialized as needed.)
7931 * The 3rd argument, pRenamed, references an out parameter. If the
7932 * pointer is non-zero, its referent will be set to a summary of renames
7933 * done if renaming was necessary, or set to 0 if none was done. The out
7934 * string (if any) must be sqlite3_free()'ed by the caller.
7936 #ifdef SHELL_DEBUG
7937 #define rc_err_oom_die(rc) \
7938 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7939 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7940 eputf("E:%d\n",rc), assert(0)
7941 #else
7942 static void rc_err_oom_die(int rc){
7943 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
7944 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
7946 #endif
7948 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7949 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
7950 #else /* Otherwise, memory is faster/better for the transient DB. */
7951 static const char *zCOL_DB = ":memory:";
7952 #endif
7954 /* Define character (as C string) to separate generated column ordinal
7955 * from protected part of incoming column names. This defaults to "_"
7956 * so that incoming column identifiers that did not need not be quoted
7957 * remain usable without being quoted. It must be one character.
7959 #ifndef SHELL_AUTOCOLUMN_SEP
7960 # define AUTOCOLUMN_SEP "_"
7961 #else
7962 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7963 #endif
7965 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
7966 /* Queries and D{D,M}L used here */
7967 static const char * const zTabMake = "\
7968 CREATE TABLE ColNames(\
7969 cpos INTEGER PRIMARY KEY,\
7970 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7971 CREATE VIEW RepeatedNames AS \
7972 SELECT DISTINCT t.name FROM ColNames t \
7973 WHERE t.name COLLATE NOCASE IN (\
7974 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7977 static const char * const zTabFill = "\
7978 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7979 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7981 static const char * const zHasDupes = "\
7982 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7983 <count(name) FROM ColNames\
7985 #ifdef SHELL_COLUMN_RENAME_CLEAN
7986 static const char * const zDedoctor = "\
7987 UPDATE ColNames SET chop=iif(\
7988 (substring(name,nlen,1) BETWEEN '0' AND '9')\
7989 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
7990 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
7994 #endif
7995 static const char * const zSetReps = "\
7996 UPDATE ColNames AS t SET reps=\
7997 (SELECT count(*) FROM ColNames d \
7998 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
7999 COLLATE NOCASE\
8002 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8003 static const char * const zColDigits = "\
8004 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8006 #else
8007 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8008 static const char * const zColDigits = "\
8009 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8010 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8011 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8013 #endif
8014 static const char * const zRenameRank =
8015 #ifdef SHELL_COLUMN_RENAME_CLEAN
8016 "UPDATE ColNames AS t SET suff="
8017 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8018 #else /* ...RENAME_MINIMAL_ONE_PASS */
8019 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8020 " SELECT 0 AS nlz"
8021 " UNION"
8022 " SELECT nlz+1 AS nlz FROM Lzn"
8023 " WHERE EXISTS("
8024 " SELECT 1"
8025 " FROM ColNames t, ColNames o"
8026 " WHERE"
8027 " iif(t.name IN (SELECT * FROM RepeatedNames),"
8028 " printf('%s"AUTOCOLUMN_SEP"%s',"
8029 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8030 " t.name"
8031 " )"
8032 " ="
8033 " iif(o.name IN (SELECT * FROM RepeatedNames),"
8034 " printf('%s"AUTOCOLUMN_SEP"%s',"
8035 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8036 " o.name"
8037 " )"
8038 " COLLATE NOCASE"
8039 " AND o.cpos<>t.cpos"
8040 " GROUP BY t.cpos"
8041 " )"
8042 ") UPDATE Colnames AS t SET"
8043 " chop = 0," /* No chopping, never touch incoming names. */
8044 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
8045 " printf('"AUTOCOLUMN_SEP"%s', substring("
8046 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8047 " ''"
8048 " )"
8049 #endif
8051 static const char * const zCollectVar = "\
8052 SELECT\
8053 '('||x'0a'\
8054 || group_concat(\
8055 cname||' TEXT',\
8056 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8057 ||')' AS ColsSpec \
8058 FROM (\
8059 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8060 FROM ColNames ORDER BY cpos\
8062 static const char * const zRenamesDone =
8063 "SELECT group_concat("
8064 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8065 " ','||x'0a')"
8066 "FROM ColNames WHERE suff<>'' OR chop!=0"
8068 int rc;
8069 sqlite3_stmt *pStmt = 0;
8070 assert(pDb!=0);
8071 if( zColNew ){
8072 /* Add initial or additional column. Init db if necessary. */
8073 if( *pDb==0 ){
8074 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8075 #ifdef SHELL_COLFIX_DB
8076 if(*zCOL_DB!=':')
8077 sqlite3_exec(*pDb,"drop table if exists ColNames;"
8078 "drop view if exists RepeatedNames;",0,0,0);
8079 #endif
8080 #undef SHELL_COLFIX_DB
8081 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8082 rc_err_oom_die(rc);
8084 assert(*pDb!=0);
8085 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8086 rc_err_oom_die(rc);
8087 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8088 rc_err_oom_die(rc);
8089 rc = sqlite3_step(pStmt);
8090 rc_err_oom_die(rc);
8091 sqlite3_finalize(pStmt);
8092 return 0;
8093 }else if( *pDb==0 ){
8094 return 0;
8095 }else{
8096 /* Formulate the columns spec, close the DB, zero *pDb. */
8097 char *zColsSpec = 0;
8098 int hasDupes = db_int(*pDb, zHasDupes);
8099 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8100 if( hasDupes ){
8101 #ifdef SHELL_COLUMN_RENAME_CLEAN
8102 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8103 rc_err_oom_die(rc);
8104 #endif
8105 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8106 rc_err_oom_die(rc);
8107 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8108 rc_err_oom_die(rc);
8109 sqlite3_bind_int(pStmt, 1, nDigits);
8110 rc = sqlite3_step(pStmt);
8111 sqlite3_finalize(pStmt);
8112 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
8114 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8115 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8116 rc_err_oom_die(rc);
8117 rc = sqlite3_step(pStmt);
8118 if( rc==SQLITE_ROW ){
8119 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8120 }else{
8121 zColsSpec = 0;
8123 if( pzRenamed!=0 ){
8124 if( !hasDupes ) *pzRenamed = 0;
8125 else{
8126 sqlite3_finalize(pStmt);
8127 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8128 && SQLITE_ROW==sqlite3_step(pStmt) ){
8129 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8130 }else
8131 *pzRenamed = 0;
8134 sqlite3_finalize(pStmt);
8135 sqlite3_close(*pDb);
8136 *pDb = 0;
8137 return zColsSpec;
8142 ** Check if the sqlite_schema table contains one or more virtual tables. If
8143 ** parameter zLike is not NULL, then it is an SQL expression that the
8144 ** sqlite_schema row must also match. If one or more such rows are found,
8145 ** print the following warning to the output:
8147 ** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
8149 static int outputDumpWarning(ShellState *p, const char *zLike){
8150 int rc = SQLITE_OK;
8151 sqlite3_stmt *pStmt = 0;
8152 shellPreparePrintf(p->db, &rc, &pStmt,
8153 "SELECT 1 FROM sqlite_schema o WHERE "
8154 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
8156 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8157 oputz("/* WARNING: "
8158 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
8161 shellFinalize(&rc, pStmt);
8162 return rc;
8166 ** Fault-Simulator state and logic.
8168 static struct {
8169 int iId; /* ID that triggers a simulated fault. -1 means "any" */
8170 int iErr; /* The error code to return on a fault */
8171 int iCnt; /* Trigger the fault only if iCnt is already zero */
8172 int iInterval; /* Reset iCnt to this value after each fault */
8173 int eVerbose; /* When to print output */
8174 int nHit; /* Number of hits seen so far */
8175 int nRepeat; /* Turn off after this many hits. 0 for never */
8176 int nSkip; /* Skip this many before first fault */
8177 } faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0};
8180 ** This is the fault-sim callback
8182 static int faultsim_callback(int iArg){
8183 if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){
8184 return SQLITE_OK;
8186 if( faultsim_state.iCnt ){
8187 if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
8188 if( faultsim_state.eVerbose>=2 ){
8189 oputf("FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
8191 return SQLITE_OK;
8193 if( faultsim_state.eVerbose>=1 ){
8194 oputf("FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
8196 faultsim_state.iCnt = faultsim_state.iInterval;
8197 faultsim_state.nHit++;
8198 if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
8199 faultsim_state.iCnt = -1;
8201 return faultsim_state.iErr;
8205 ** If an input line begins with "." then invoke this routine to
8206 ** process that line.
8208 ** Return 1 on error, 2 to exit, and 0 otherwise.
8210 static int do_meta_command(char *zLine, ShellState *p){
8211 int h = 1;
8212 int nArg = 0;
8213 int n, c;
8214 int rc = 0;
8215 char *azArg[52];
8217 #ifndef SQLITE_OMIT_VIRTUALTABLE
8218 if( p->expert.pExpert ){
8219 expertFinish(p, 1, 0);
8221 #endif
8223 /* Parse the input line into tokens.
8225 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8226 while( IsSpace(zLine[h]) ){ h++; }
8227 if( zLine[h]==0 ) break;
8228 if( zLine[h]=='\'' || zLine[h]=='"' ){
8229 int delim = zLine[h++];
8230 azArg[nArg++] = &zLine[h];
8231 while( zLine[h] && zLine[h]!=delim ){
8232 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8233 h++;
8235 if( zLine[h]==delim ){
8236 zLine[h++] = 0;
8238 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8239 }else{
8240 azArg[nArg++] = &zLine[h];
8241 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8242 if( zLine[h] ) zLine[h++] = 0;
8245 azArg[nArg] = 0;
8247 /* Process the input line.
8249 if( nArg==0 ) return 0; /* no tokens, no error */
8250 n = strlen30(azArg[0]);
8251 c = azArg[0][0];
8252 clearTempFile(p);
8254 #ifndef SQLITE_OMIT_AUTHORIZATION
8255 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
8256 if( nArg!=2 ){
8257 eputz("Usage: .auth ON|OFF\n");
8258 rc = 1;
8259 goto meta_command_exit;
8261 open_db(p, 0);
8262 if( booleanValue(azArg[1]) ){
8263 sqlite3_set_authorizer(p->db, shellAuth, p);
8264 }else if( p->bSafeModePersist ){
8265 sqlite3_set_authorizer(p->db, safeModeAuth, p);
8266 }else{
8267 sqlite3_set_authorizer(p->db, 0, 0);
8269 }else
8270 #endif
8272 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8273 && !defined(SQLITE_SHELL_FIDDLE)
8274 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
8275 open_db(p, 0);
8276 failIfSafeMode(p, "cannot run .archive in safe mode");
8277 rc = arDotCommand(p, 0, azArg, nArg);
8278 }else
8279 #endif
8281 #ifndef SQLITE_SHELL_FIDDLE
8282 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
8283 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
8285 const char *zDestFile = 0;
8286 const char *zDb = 0;
8287 sqlite3 *pDest;
8288 sqlite3_backup *pBackup;
8289 int j;
8290 int bAsync = 0;
8291 const char *zVfs = 0;
8292 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8293 for(j=1; j<nArg; j++){
8294 const char *z = azArg[j];
8295 if( z[0]=='-' ){
8296 if( z[1]=='-' ) z++;
8297 if( cli_strcmp(z, "-append")==0 ){
8298 zVfs = "apndvfs";
8299 }else
8300 if( cli_strcmp(z, "-async")==0 ){
8301 bAsync = 1;
8302 }else
8304 eputf("unknown option: %s\n", azArg[j]);
8305 return 1;
8307 }else if( zDestFile==0 ){
8308 zDestFile = azArg[j];
8309 }else if( zDb==0 ){
8310 zDb = zDestFile;
8311 zDestFile = azArg[j];
8312 }else{
8313 eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8314 return 1;
8317 if( zDestFile==0 ){
8318 eputz("missing FILENAME argument on .backup\n");
8319 return 1;
8321 if( zDb==0 ) zDb = "main";
8322 rc = sqlite3_open_v2(zDestFile, &pDest,
8323 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8324 if( rc!=SQLITE_OK ){
8325 eputf("Error: cannot open \"%s\"\n", zDestFile);
8326 close_db(pDest);
8327 return 1;
8329 if( bAsync ){
8330 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8331 0, 0, 0);
8333 open_db(p, 0);
8334 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8335 if( pBackup==0 ){
8336 shellDatabaseError(pDest);
8337 close_db(pDest);
8338 return 1;
8340 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8341 sqlite3_backup_finish(pBackup);
8342 if( rc==SQLITE_DONE ){
8343 rc = 0;
8344 }else{
8345 shellDatabaseError(pDest);
8346 rc = 1;
8348 close_db(pDest);
8349 }else
8350 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8352 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
8353 if( nArg==2 ){
8354 bail_on_error = booleanValue(azArg[1]);
8355 }else{
8356 eputz("Usage: .bail on|off\n");
8357 rc = 1;
8359 }else
8361 /* Undocumented. Legacy only. See "crnl" below */
8362 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
8363 if( nArg==2 ){
8364 if( booleanValue(azArg[1]) ){
8365 setBinaryMode(p->out, 1);
8366 }else{
8367 setTextMode(p->out, 1);
8369 }else{
8370 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
8371 "Usage: .binary on|off\n");
8372 rc = 1;
8374 }else
8376 /* The undocumented ".breakpoint" command causes a call to the no-op
8377 ** routine named test_breakpoint().
8379 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
8380 test_breakpoint();
8381 }else
8383 #ifndef SQLITE_SHELL_FIDDLE
8384 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
8385 failIfSafeMode(p, "cannot run .cd in safe mode");
8386 if( nArg==2 ){
8387 #if defined(_WIN32) || defined(WIN32)
8388 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8389 rc = !SetCurrentDirectoryW(z);
8390 sqlite3_free(z);
8391 #else
8392 rc = chdir(azArg[1]);
8393 #endif
8394 if( rc ){
8395 eputf("Cannot change to directory \"%s\"\n", azArg[1]);
8396 rc = 1;
8398 }else{
8399 eputz("Usage: .cd DIRECTORY\n");
8400 rc = 1;
8402 }else
8403 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8405 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
8406 if( nArg==2 ){
8407 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8408 }else{
8409 eputz("Usage: .changes on|off\n");
8410 rc = 1;
8412 }else
8414 #ifndef SQLITE_SHELL_FIDDLE
8415 /* Cancel output redirection, if it is currently set (by .testcase)
8416 ** Then read the content of the testcase-out.txt file and compare against
8417 ** azArg[1]. If there are differences, report an error and exit.
8419 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
8420 char *zRes = 0;
8421 output_reset(p);
8422 if( nArg!=2 ){
8423 eputz("Usage: .check GLOB-PATTERN\n");
8424 rc = 2;
8425 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8426 rc = 2;
8427 }else if( testcase_glob(azArg[1],zRes)==0 ){
8428 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
8429 p->zTestcase, azArg[1], zRes);
8430 rc = 1;
8431 }else{
8432 oputf("testcase-%s ok\n", p->zTestcase);
8433 p->nCheck++;
8435 sqlite3_free(zRes);
8436 }else
8437 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8439 #ifndef SQLITE_SHELL_FIDDLE
8440 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
8441 failIfSafeMode(p, "cannot run .clone in safe mode");
8442 if( nArg==2 ){
8443 tryToClone(p, azArg[1]);
8444 }else{
8445 eputz("Usage: .clone FILENAME\n");
8446 rc = 1;
8448 }else
8449 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8451 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
8452 if( nArg==1 ){
8453 /* List available connections */
8454 int i;
8455 for(i=0; i<ArraySize(p->aAuxDb); i++){
8456 const char *zFile = p->aAuxDb[i].zDbFilename;
8457 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8458 zFile = "(not open)";
8459 }else if( zFile==0 ){
8460 zFile = "(memory)";
8461 }else if( zFile[0]==0 ){
8462 zFile = "(temporary-file)";
8464 if( p->pAuxDb == &p->aAuxDb[i] ){
8465 sputf(stdout, "ACTIVE %d: %s\n", i, zFile);
8466 }else if( p->aAuxDb[i].db!=0 ){
8467 sputf(stdout, " %d: %s\n", i, zFile);
8470 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8471 int i = azArg[1][0] - '0';
8472 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8473 p->pAuxDb->db = p->db;
8474 p->pAuxDb = &p->aAuxDb[i];
8475 globalDb = p->db = p->pAuxDb->db;
8476 p->pAuxDb->db = 0;
8478 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
8479 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8480 int i = azArg[2][0] - '0';
8481 if( i<0 || i>=ArraySize(p->aAuxDb) ){
8482 /* No-op */
8483 }else if( p->pAuxDb == &p->aAuxDb[i] ){
8484 eputz("cannot close the active database connection\n");
8485 rc = 1;
8486 }else if( p->aAuxDb[i].db ){
8487 session_close_all(p, i);
8488 close_db(p->aAuxDb[i].db);
8489 p->aAuxDb[i].db = 0;
8491 }else{
8492 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
8493 rc = 1;
8495 }else
8497 if( c=='c' && n==4 && cli_strncmp(azArg[0], "crnl", n)==0 ){
8498 if( nArg==2 ){
8499 if( booleanValue(azArg[1]) ){
8500 setTextMode(p->out, 1);
8501 }else{
8502 setBinaryMode(p->out, 1);
8504 }else{
8505 #if !defined(_WIN32) && !defined(WIN32)
8506 eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
8507 #endif
8508 eputz("Usage: .crnl on|off\n");
8509 rc = 1;
8511 }else
8513 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
8514 char **azName = 0;
8515 int nName = 0;
8516 sqlite3_stmt *pStmt;
8517 int i;
8518 open_db(p, 0);
8519 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8520 if( rc ){
8521 shellDatabaseError(p->db);
8522 rc = 1;
8523 }else{
8524 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8525 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8526 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8527 if( zSchema==0 || zFile==0 ) continue;
8528 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8529 shell_check_oom(azName);
8530 azName[nName*2] = strdup(zSchema);
8531 azName[nName*2+1] = strdup(zFile);
8532 nName++;
8535 sqlite3_finalize(pStmt);
8536 for(i=0; i<nName; i++){
8537 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8538 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8539 const char *z = azName[i*2+1];
8540 oputf("%s: %s %s%s\n",
8541 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
8542 eTxn==SQLITE_TXN_NONE ? "" :
8543 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8544 free(azName[i*2]);
8545 free(azName[i*2+1]);
8547 sqlite3_free(azName);
8548 }else
8550 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
8551 static const struct DbConfigChoices {
8552 const char *zName;
8553 int op;
8554 } aDbConfig[] = {
8555 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
8556 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
8557 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
8558 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
8559 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
8560 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8561 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
8562 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8563 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8564 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8565 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8566 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8567 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
8568 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER },
8569 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8570 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
8571 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
8572 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8574 int ii, v;
8575 open_db(p, 0);
8576 for(ii=0; ii<ArraySize(aDbConfig); ii++){
8577 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8578 if( nArg>=3 ){
8579 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8581 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8582 oputf("%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
8583 if( nArg>1 ) break;
8585 if( nArg>1 && ii==ArraySize(aDbConfig) ){
8586 eputf("Error: unknown dbconfig \"%s\"\n", azArg[1]);
8587 eputz("Enter \".dbconfig\" with no arguments for a list\n");
8589 }else
8591 #if SQLITE_SHELL_HAVE_RECOVER
8592 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
8593 rc = shell_dbinfo_command(p, nArg, azArg);
8594 }else
8596 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
8597 open_db(p, 0);
8598 rc = recoverDatabaseCmd(p, nArg, azArg);
8599 }else
8600 #endif /* SQLITE_SHELL_HAVE_RECOVER */
8602 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
8603 char *zLike = 0;
8604 char *zSql;
8605 int i;
8606 int savedShowHeader = p->showHeader;
8607 int savedShellFlags = p->shellFlgs;
8608 ShellClearFlag(p,
8609 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8610 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8611 for(i=1; i<nArg; i++){
8612 if( azArg[i][0]=='-' ){
8613 const char *z = azArg[i]+1;
8614 if( z[0]=='-' ) z++;
8615 if( cli_strcmp(z,"preserve-rowids")==0 ){
8616 #ifdef SQLITE_OMIT_VIRTUALTABLE
8617 eputz("The --preserve-rowids option is not compatible"
8618 " with SQLITE_OMIT_VIRTUALTABLE\n");
8619 rc = 1;
8620 sqlite3_free(zLike);
8621 goto meta_command_exit;
8622 #else
8623 ShellSetFlag(p, SHFLG_PreserveRowid);
8624 #endif
8625 }else
8626 if( cli_strcmp(z,"newlines")==0 ){
8627 ShellSetFlag(p, SHFLG_Newlines);
8628 }else
8629 if( cli_strcmp(z,"data-only")==0 ){
8630 ShellSetFlag(p, SHFLG_DumpDataOnly);
8631 }else
8632 if( cli_strcmp(z,"nosys")==0 ){
8633 ShellSetFlag(p, SHFLG_DumpNoSys);
8634 }else
8636 eputf("Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8637 rc = 1;
8638 sqlite3_free(zLike);
8639 goto meta_command_exit;
8641 }else{
8642 /* azArg[i] contains a LIKE pattern. This ".dump" request should
8643 ** only dump data for tables for which either the table name matches
8644 ** the LIKE pattern, or the table appears to be a shadow table of
8645 ** a virtual table for which the name matches the LIKE pattern.
8647 char *zExpr = sqlite3_mprintf(
8648 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8649 " SELECT 1 FROM sqlite_schema WHERE "
8650 " name LIKE %Q ESCAPE '\\' AND"
8651 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8652 " substr(o.name, 1, length(name)+1) == (name||'_')"
8653 ")", azArg[i], azArg[i]
8656 if( zLike ){
8657 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8658 }else{
8659 zLike = zExpr;
8664 open_db(p, 0);
8666 outputDumpWarning(p, zLike);
8667 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8668 /* When playing back a "dump", the content might appear in an order
8669 ** which causes immediate foreign key constraints to be violated.
8670 ** So disable foreign-key constraint enforcement to prevent problems. */
8671 oputz("PRAGMA foreign_keys=OFF;\n");
8672 oputz("BEGIN TRANSACTION;\n");
8674 p->writableSchema = 0;
8675 p->showHeader = 0;
8676 /* Set writable_schema=ON since doing so forces SQLite to initialize
8677 ** as much of the schema as it can even if the sqlite_schema table is
8678 ** corrupt. */
8679 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8680 p->nErr = 0;
8681 if( zLike==0 ) zLike = sqlite3_mprintf("true");
8682 zSql = sqlite3_mprintf(
8683 "SELECT name, type, sql FROM sqlite_schema AS o "
8684 "WHERE (%s) AND type=='table'"
8685 " AND sql NOT NULL"
8686 " ORDER BY tbl_name='sqlite_sequence', rowid",
8687 zLike
8689 run_schema_dump_query(p,zSql);
8690 sqlite3_free(zSql);
8691 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8692 zSql = sqlite3_mprintf(
8693 "SELECT sql FROM sqlite_schema AS o "
8694 "WHERE (%s) AND sql NOT NULL"
8695 " AND type IN ('index','trigger','view') "
8696 "ORDER BY type COLLATE NOCASE DESC",
8697 zLike
8699 run_table_dump_query(p, zSql);
8700 sqlite3_free(zSql);
8702 sqlite3_free(zLike);
8703 if( p->writableSchema ){
8704 oputz("PRAGMA writable_schema=OFF;\n");
8705 p->writableSchema = 0;
8707 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8708 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8709 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8710 oputz(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8712 p->showHeader = savedShowHeader;
8713 p->shellFlgs = savedShellFlags;
8714 }else
8716 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8717 if( nArg==2 ){
8718 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8719 }else{
8720 eputz("Usage: .echo on|off\n");
8721 rc = 1;
8723 }else
8725 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8726 if( nArg==2 ){
8727 p->autoEQPtest = 0;
8728 if( p->autoEQPtrace ){
8729 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8730 p->autoEQPtrace = 0;
8732 if( cli_strcmp(azArg[1],"full")==0 ){
8733 p->autoEQP = AUTOEQP_full;
8734 }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8735 p->autoEQP = AUTOEQP_trigger;
8736 #ifdef SQLITE_DEBUG
8737 }else if( cli_strcmp(azArg[1],"test")==0 ){
8738 p->autoEQP = AUTOEQP_on;
8739 p->autoEQPtest = 1;
8740 }else if( cli_strcmp(azArg[1],"trace")==0 ){
8741 p->autoEQP = AUTOEQP_full;
8742 p->autoEQPtrace = 1;
8743 open_db(p, 0);
8744 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8745 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8746 #endif
8747 }else{
8748 p->autoEQP = (u8)booleanValue(azArg[1]);
8750 }else{
8751 eputz("Usage: .eqp off|on|trace|trigger|full\n");
8752 rc = 1;
8754 }else
8756 #ifndef SQLITE_SHELL_FIDDLE
8757 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8758 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8759 rc = 2;
8760 }else
8761 #endif
8763 /* The ".explain" command is automatic now. It is largely pointless. It
8764 ** retained purely for backwards compatibility */
8765 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8766 int val = 1;
8767 if( nArg>=2 ){
8768 if( cli_strcmp(azArg[1],"auto")==0 ){
8769 val = 99;
8770 }else{
8771 val = booleanValue(azArg[1]);
8774 if( val==1 && p->mode!=MODE_Explain ){
8775 p->normalMode = p->mode;
8776 p->mode = MODE_Explain;
8777 p->autoExplain = 0;
8778 }else if( val==0 ){
8779 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8780 p->autoExplain = 0;
8781 }else if( val==99 ){
8782 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8783 p->autoExplain = 1;
8785 }else
8787 #ifndef SQLITE_OMIT_VIRTUALTABLE
8788 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8789 if( p->bSafeMode ){
8790 eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
8791 azArg[0]);
8792 rc = 1;
8793 }else{
8794 open_db(p, 0);
8795 expertDotCommand(p, azArg, nArg);
8797 }else
8798 #endif
8800 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8801 static const struct {
8802 const char *zCtrlName; /* Name of a test-control option */
8803 int ctrlCode; /* Integer code for that option */
8804 const char *zUsage; /* Usage notes */
8805 } aCtrl[] = {
8806 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
8807 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
8808 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
8809 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
8810 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
8811 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
8812 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
8813 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
8814 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
8815 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
8816 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
8818 int filectrl = -1;
8819 int iCtrl = -1;
8820 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
8821 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
8822 int n2, i;
8823 const char *zCmd = 0;
8824 const char *zSchema = 0;
8826 open_db(p, 0);
8827 zCmd = nArg>=2 ? azArg[1] : "help";
8829 if( zCmd[0]=='-'
8830 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8831 && nArg>=4
8833 zSchema = azArg[2];
8834 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8835 nArg -= 2;
8836 zCmd = azArg[1];
8839 /* The argument can optionally begin with "-" or "--" */
8840 if( zCmd[0]=='-' && zCmd[1] ){
8841 zCmd++;
8842 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8845 /* --help lists all file-controls */
8846 if( cli_strcmp(zCmd,"help")==0 ){
8847 oputz("Available file-controls:\n");
8848 for(i=0; i<ArraySize(aCtrl); i++){
8849 oputf(" .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8851 rc = 1;
8852 goto meta_command_exit;
8855 /* convert filectrl text option to value. allow any unique prefix
8856 ** of the option name, or a numerical value. */
8857 n2 = strlen30(zCmd);
8858 for(i=0; i<ArraySize(aCtrl); i++){
8859 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8860 if( filectrl<0 ){
8861 filectrl = aCtrl[i].ctrlCode;
8862 iCtrl = i;
8863 }else{
8864 eputf("Error: ambiguous file-control: \"%s\"\n"
8865 "Use \".filectrl --help\" for help\n", zCmd);
8866 rc = 1;
8867 goto meta_command_exit;
8871 if( filectrl<0 ){
8872 eputf("Error: unknown file-control: %s\n"
8873 "Use \".filectrl --help\" for help\n", zCmd);
8874 }else{
8875 switch(filectrl){
8876 case SQLITE_FCNTL_SIZE_LIMIT: {
8877 if( nArg!=2 && nArg!=3 ) break;
8878 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8879 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8880 isOk = 1;
8881 break;
8883 case SQLITE_FCNTL_LOCK_TIMEOUT:
8884 case SQLITE_FCNTL_CHUNK_SIZE: {
8885 int x;
8886 if( nArg!=3 ) break;
8887 x = (int)integerValue(azArg[2]);
8888 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8889 isOk = 2;
8890 break;
8892 case SQLITE_FCNTL_PERSIST_WAL:
8893 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8894 int x;
8895 if( nArg!=2 && nArg!=3 ) break;
8896 x = nArg==3 ? booleanValue(azArg[2]) : -1;
8897 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8898 iRes = x;
8899 isOk = 1;
8900 break;
8902 case SQLITE_FCNTL_DATA_VERSION:
8903 case SQLITE_FCNTL_HAS_MOVED: {
8904 int x;
8905 if( nArg!=2 ) break;
8906 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8907 iRes = x;
8908 isOk = 1;
8909 break;
8911 case SQLITE_FCNTL_TEMPFILENAME: {
8912 char *z = 0;
8913 if( nArg!=2 ) break;
8914 sqlite3_file_control(p->db, zSchema, filectrl, &z);
8915 if( z ){
8916 oputf("%s\n", z);
8917 sqlite3_free(z);
8919 isOk = 2;
8920 break;
8922 case SQLITE_FCNTL_RESERVE_BYTES: {
8923 int x;
8924 if( nArg>=3 ){
8925 x = atoi(azArg[2]);
8926 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8928 x = -1;
8929 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8930 oputf("%d\n", x);
8931 isOk = 2;
8932 break;
8936 if( isOk==0 && iCtrl>=0 ){
8937 oputf("Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
8938 rc = 1;
8939 }else if( isOk==1 ){
8940 char zBuf[100];
8941 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
8942 oputf("%s\n", zBuf);
8944 }else
8946 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
8947 ShellState data;
8948 int doStats = 0;
8949 memcpy(&data, p, sizeof(data));
8950 data.showHeader = 0;
8951 data.cMode = data.mode = MODE_Semi;
8952 if( nArg==2 && optionMatch(azArg[1], "indent") ){
8953 data.cMode = data.mode = MODE_Pretty;
8954 nArg = 1;
8956 if( nArg!=1 ){
8957 eputz("Usage: .fullschema ?--indent?\n");
8958 rc = 1;
8959 goto meta_command_exit;
8961 open_db(p, 0);
8962 rc = sqlite3_exec(p->db,
8963 "SELECT sql FROM"
8964 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8965 " FROM sqlite_schema UNION ALL"
8966 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8967 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8968 "ORDER BY x",
8969 callback, &data, 0
8971 if( rc==SQLITE_OK ){
8972 sqlite3_stmt *pStmt;
8973 rc = sqlite3_prepare_v2(p->db,
8974 "SELECT rowid FROM sqlite_schema"
8975 " WHERE name GLOB 'sqlite_stat[134]'",
8976 -1, &pStmt, 0);
8977 if( rc==SQLITE_OK ){
8978 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
8979 sqlite3_finalize(pStmt);
8982 if( doStats==0 ){
8983 oputz("/* No STAT tables available */\n");
8984 }else{
8985 oputz("ANALYZE sqlite_schema;\n");
8986 data.cMode = data.mode = MODE_Insert;
8987 data.zDestTable = "sqlite_stat1";
8988 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
8989 data.zDestTable = "sqlite_stat4";
8990 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
8991 oputz("ANALYZE sqlite_schema;\n");
8993 }else
8995 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
8996 if( nArg==2 ){
8997 p->showHeader = booleanValue(azArg[1]);
8998 p->shellFlgs |= SHFLG_HeaderSet;
8999 }else{
9000 eputz("Usage: .headers on|off\n");
9001 rc = 1;
9003 }else
9005 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
9006 if( nArg>=2 ){
9007 n = showHelp(p->out, azArg[1]);
9008 if( n==0 ){
9009 oputf("Nothing matches '%s'\n", azArg[1]);
9011 }else{
9012 showHelp(p->out, 0);
9014 }else
9016 #ifndef SQLITE_SHELL_FIDDLE
9017 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
9018 char *zTable = 0; /* Insert data into this table */
9019 char *zSchema = 0; /* Schema of zTable */
9020 char *zFile = 0; /* Name of file to extra content from */
9021 sqlite3_stmt *pStmt = NULL; /* A statement */
9022 int nCol; /* Number of columns in the table */
9023 i64 nByte; /* Number of bytes in an SQL string */
9024 int i, j; /* Loop counters */
9025 int needCommit; /* True to COMMIT or ROLLBACK at end */
9026 int nSep; /* Number of bytes in p->colSeparator[] */
9027 char *zSql = 0; /* An SQL statement */
9028 ImportCtx sCtx; /* Reader context */
9029 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
9030 int eVerbose = 0; /* Larger for more console output */
9031 int nSkip = 0; /* Initial lines to skip */
9032 int useOutputMode = 1; /* Use output mode to determine separators */
9033 char *zCreate = 0; /* CREATE TABLE statement text */
9035 failIfSafeMode(p, "cannot run .import in safe mode");
9036 memset(&sCtx, 0, sizeof(sCtx));
9037 if( p->mode==MODE_Ascii ){
9038 xRead = ascii_read_one_field;
9039 }else{
9040 xRead = csv_read_one_field;
9042 rc = 1;
9043 for(i=1; i<nArg; i++){
9044 char *z = azArg[i];
9045 if( z[0]=='-' && z[1]=='-' ) z++;
9046 if( z[0]!='-' ){
9047 if( zFile==0 ){
9048 zFile = z;
9049 }else if( zTable==0 ){
9050 zTable = z;
9051 }else{
9052 oputf("ERROR: extra argument: \"%s\". Usage:\n", z);
9053 showHelp(p->out, "import");
9054 goto meta_command_exit;
9056 }else if( cli_strcmp(z,"-v")==0 ){
9057 eVerbose++;
9058 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
9059 zSchema = azArg[++i];
9060 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
9061 nSkip = integerValue(azArg[++i]);
9062 }else if( cli_strcmp(z,"-ascii")==0 ){
9063 sCtx.cColSep = SEP_Unit[0];
9064 sCtx.cRowSep = SEP_Record[0];
9065 xRead = ascii_read_one_field;
9066 useOutputMode = 0;
9067 }else if( cli_strcmp(z,"-csv")==0 ){
9068 sCtx.cColSep = ',';
9069 sCtx.cRowSep = '\n';
9070 xRead = csv_read_one_field;
9071 useOutputMode = 0;
9072 }else{
9073 oputf("ERROR: unknown option: \"%s\". Usage:\n", z);
9074 showHelp(p->out, "import");
9075 goto meta_command_exit;
9078 if( zTable==0 ){
9079 oputf("ERROR: missing %s argument. Usage:\n",
9080 zFile==0 ? "FILE" : "TABLE");
9081 showHelp(p->out, "import");
9082 goto meta_command_exit;
9084 seenInterrupt = 0;
9085 open_db(p, 0);
9086 if( useOutputMode ){
9087 /* If neither the --csv or --ascii options are specified, then set
9088 ** the column and row separator characters from the output mode. */
9089 nSep = strlen30(p->colSeparator);
9090 if( nSep==0 ){
9091 eputz("Error: non-null column separator required for import\n");
9092 goto meta_command_exit;
9094 if( nSep>1 ){
9095 eputz("Error: multi-character column separators not allowed"
9096 " for import\n");
9097 goto meta_command_exit;
9099 nSep = strlen30(p->rowSeparator);
9100 if( nSep==0 ){
9101 eputz("Error: non-null row separator required for import\n");
9102 goto meta_command_exit;
9104 if( nSep==2 && p->mode==MODE_Csv
9105 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
9107 /* When importing CSV (only), if the row separator is set to the
9108 ** default output row separator, change it to the default input
9109 ** row separator. This avoids having to maintain different input
9110 ** and output row separators. */
9111 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9112 nSep = strlen30(p->rowSeparator);
9114 if( nSep>1 ){
9115 eputz("Error: multi-character row separators not allowed"
9116 " for import\n");
9117 goto meta_command_exit;
9119 sCtx.cColSep = (u8)p->colSeparator[0];
9120 sCtx.cRowSep = (u8)p->rowSeparator[0];
9122 sCtx.zFile = zFile;
9123 sCtx.nLine = 1;
9124 if( sCtx.zFile[0]=='|' ){
9125 #ifdef SQLITE_OMIT_POPEN
9126 eputz("Error: pipes are not supported in this OS\n");
9127 goto meta_command_exit;
9128 #else
9129 sCtx.in = popen(sCtx.zFile+1, "r");
9130 sCtx.zFile = "<pipe>";
9131 sCtx.xCloser = pclose;
9132 #endif
9133 }else{
9134 sCtx.in = fopen(sCtx.zFile, "rb");
9135 sCtx.xCloser = fclose;
9137 if( sCtx.in==0 ){
9138 eputf("Error: cannot open \"%s\"\n", zFile);
9139 goto meta_command_exit;
9141 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9142 char zSep[2];
9143 zSep[1] = 0;
9144 zSep[0] = sCtx.cColSep;
9145 oputz("Column separator ");
9146 output_c_string(zSep);
9147 oputz(", row separator ");
9148 zSep[0] = sCtx.cRowSep;
9149 output_c_string(zSep);
9150 oputz("\n");
9152 sCtx.z = sqlite3_malloc64(120);
9153 if( sCtx.z==0 ){
9154 import_cleanup(&sCtx);
9155 shell_out_of_memory();
9157 /* Below, resources must be freed before exit. */
9158 while( (nSkip--)>0 ){
9159 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9161 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
9162 if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
9163 /* Table does not exist. Create it. */
9164 sqlite3 *dbCols = 0;
9165 char *zRenames = 0;
9166 char *zColDefs;
9167 zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
9168 zSchema ? zSchema : "main", zTable);
9169 while( xRead(&sCtx) ){
9170 zAutoColumn(sCtx.z, &dbCols, 0);
9171 if( sCtx.cTerm!=sCtx.cColSep ) break;
9173 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9174 if( zRenames!=0 ){
9175 sputf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9176 "Columns renamed during .import %s due to duplicates:\n"
9177 "%s\n", sCtx.zFile, zRenames);
9178 sqlite3_free(zRenames);
9180 assert(dbCols==0);
9181 if( zColDefs==0 ){
9182 eputf("%s: empty file\n", sCtx.zFile);
9183 import_cleanup(&sCtx);
9184 rc = 1;
9185 sqlite3_free(zCreate);
9186 goto meta_command_exit;
9188 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9189 if( zCreate==0 ){
9190 import_cleanup(&sCtx);
9191 shell_out_of_memory();
9193 if( eVerbose>=1 ){
9194 oputf("%s\n", zCreate);
9196 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9197 sqlite3_free(zCreate);
9198 zCreate = 0;
9199 if( rc ){
9200 eputf("%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9201 import_cleanup(&sCtx);
9202 rc = 1;
9203 goto meta_command_exit;
9206 zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
9207 zTable, zSchema);
9208 if( zSql==0 ){
9209 import_cleanup(&sCtx);
9210 shell_out_of_memory();
9212 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9213 sqlite3_free(zSql);
9214 zSql = 0;
9215 if( rc ){
9216 if (pStmt) sqlite3_finalize(pStmt);
9217 shellDatabaseError(p->db);
9218 import_cleanup(&sCtx);
9219 rc = 1;
9220 goto meta_command_exit;
9222 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9223 nCol = sqlite3_column_int(pStmt, 0);
9224 }else{
9225 nCol = 0;
9227 sqlite3_finalize(pStmt);
9228 pStmt = 0;
9229 if( nCol==0 ) return 0; /* no columns, no error */
9231 nByte = 64 /* space for "INSERT INTO", "VALUES(", ")\0" */
9232 + (zSchema ? strlen(zSchema)*2 + 2: 0) /* Quoted schema name */
9233 + strlen(zTable)*2 + 2 /* Quoted table name */
9234 + nCol*2; /* Space for ",?" for each column */
9235 zSql = sqlite3_malloc64( nByte );
9236 if( zSql==0 ){
9237 import_cleanup(&sCtx);
9238 shell_out_of_memory();
9240 if( zSchema ){
9241 sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
9242 zSchema, zTable);
9243 }else{
9244 sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
9246 j = strlen30(zSql);
9247 for(i=1; i<nCol; i++){
9248 zSql[j++] = ',';
9249 zSql[j++] = '?';
9251 zSql[j++] = ')';
9252 zSql[j] = 0;
9253 assert( j<nByte );
9254 if( eVerbose>=2 ){
9255 oputf("Insert using: %s\n", zSql);
9257 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9258 sqlite3_free(zSql);
9259 zSql = 0;
9260 if( rc ){
9261 shellDatabaseError(p->db);
9262 if (pStmt) sqlite3_finalize(pStmt);
9263 import_cleanup(&sCtx);
9264 rc = 1;
9265 goto meta_command_exit;
9267 needCommit = sqlite3_get_autocommit(p->db);
9268 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9270 int startLine = sCtx.nLine;
9271 for(i=0; i<nCol; i++){
9272 char *z = xRead(&sCtx);
9274 ** Did we reach end-of-file before finding any columns?
9275 ** If so, stop instead of NULL filling the remaining columns.
9277 if( z==0 && i==0 ) break;
9279 ** Did we reach end-of-file OR end-of-line before finding any
9280 ** columns in ASCII mode? If so, stop instead of NULL filling
9281 ** the remaining columns.
9283 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9285 ** For CSV mode, per RFC 4180, accept EOF in lieu of final
9286 ** record terminator but only for last field of multi-field row.
9287 ** (If there are too few fields, it's not valid CSV anyway.)
9289 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
9290 z = "";
9292 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9293 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9294 eputf("%s:%d: expected %d columns but found %d"
9295 " - filling the rest with NULL\n",
9296 sCtx.zFile, startLine, nCol, i+1);
9297 i += 2;
9298 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9301 if( sCtx.cTerm==sCtx.cColSep ){
9303 xRead(&sCtx);
9304 i++;
9305 }while( sCtx.cTerm==sCtx.cColSep );
9306 eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
9307 sCtx.zFile, startLine, nCol, i);
9309 if( i>=nCol ){
9310 sqlite3_step(pStmt);
9311 rc = sqlite3_reset(pStmt);
9312 if( rc!=SQLITE_OK ){
9313 eputf("%s:%d: INSERT failed: %s\n",
9314 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
9315 sCtx.nErr++;
9316 }else{
9317 sCtx.nRow++;
9320 }while( sCtx.cTerm!=EOF );
9322 import_cleanup(&sCtx);
9323 sqlite3_finalize(pStmt);
9324 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9325 if( eVerbose>0 ){
9326 oputf("Added %d rows with %d errors using %d lines of input\n",
9327 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9329 }else
9330 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9332 #ifndef SQLITE_UNTESTABLE
9333 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
9334 char *zSql;
9335 char *zCollist = 0;
9336 sqlite3_stmt *pStmt;
9337 int tnum = 0;
9338 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
9339 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9340 int i;
9341 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
9342 eputf(".%s unavailable without --unsafe-testing\n",
9343 "imposter");
9344 rc = 1;
9345 goto meta_command_exit;
9347 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9348 eputz("Usage: .imposter INDEX IMPOSTER\n"
9349 " .imposter off\n");
9350 /* Also allowed, but not documented:
9352 ** .imposter TABLE IMPOSTER
9354 ** where TABLE is a WITHOUT ROWID table. In that case, the
9355 ** imposter is another WITHOUT ROWID table with the columns in
9356 ** storage order. */
9357 rc = 1;
9358 goto meta_command_exit;
9360 open_db(p, 0);
9361 if( nArg==2 ){
9362 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9363 goto meta_command_exit;
9365 zSql = sqlite3_mprintf(
9366 "SELECT rootpage, 0 FROM sqlite_schema"
9367 " WHERE name='%q' AND type='index'"
9368 "UNION ALL "
9369 "SELECT rootpage, 1 FROM sqlite_schema"
9370 " WHERE name='%q' AND type='table'"
9371 " AND sql LIKE '%%without%%rowid%%'",
9372 azArg[1], azArg[1]
9374 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9375 sqlite3_free(zSql);
9376 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9377 tnum = sqlite3_column_int(pStmt, 0);
9378 isWO = sqlite3_column_int(pStmt, 1);
9380 sqlite3_finalize(pStmt);
9381 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9382 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9383 sqlite3_free(zSql);
9384 i = 0;
9385 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9386 char zLabel[20];
9387 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9388 i++;
9389 if( zCol==0 ){
9390 if( sqlite3_column_int(pStmt,1)==-1 ){
9391 zCol = "_ROWID_";
9392 }else{
9393 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9394 zCol = zLabel;
9397 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9398 lenPK = (int)strlen(zCollist);
9400 if( zCollist==0 ){
9401 zCollist = sqlite3_mprintf("\"%w\"", zCol);
9402 }else{
9403 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9406 sqlite3_finalize(pStmt);
9407 if( i==0 || tnum==0 ){
9408 eputf("no such index: \"%s\"\n", azArg[1]);
9409 rc = 1;
9410 sqlite3_free(zCollist);
9411 goto meta_command_exit;
9413 if( lenPK==0 ) lenPK = 100000;
9414 zSql = sqlite3_mprintf(
9415 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9416 azArg[2], zCollist, lenPK, zCollist);
9417 sqlite3_free(zCollist);
9418 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9419 if( rc==SQLITE_OK ){
9420 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9421 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9422 if( rc ){
9423 eputf("Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9424 }else{
9425 sputf(stdout, "%s;\n", zSql);
9426 sputf(stdout, "WARNING: writing to an imposter table will corrupt"
9427 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
9429 }else{
9430 eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9431 rc = 1;
9433 sqlite3_free(zSql);
9434 }else
9435 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9437 if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){
9438 i64 iArg = 0;
9439 if( nArg==2 ){
9440 iArg = integerValue(azArg[1]);
9441 if( iArg==0 ) iArg = -1;
9443 if( (nArg!=1 && nArg!=2) || iArg<0 ){
9444 eputf("%s","Usage: .intck STEPS_PER_UNLOCK\n");
9445 rc = 1;
9446 goto meta_command_exit;
9448 open_db(p, 0);
9449 rc = intckDatabaseCmd(p, iArg);
9450 }else
9452 #ifdef SQLITE_ENABLE_IOTRACE
9453 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
9454 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9455 if( iotrace && iotrace!=stdout ) fclose(iotrace);
9456 iotrace = 0;
9457 if( nArg<2 ){
9458 sqlite3IoTrace = 0;
9459 }else if( cli_strcmp(azArg[1], "-")==0 ){
9460 sqlite3IoTrace = iotracePrintf;
9461 iotrace = stdout;
9462 }else{
9463 iotrace = fopen(azArg[1], "w");
9464 if( iotrace==0 ){
9465 eputf("Error: cannot open \"%s\"\n", azArg[1]);
9466 sqlite3IoTrace = 0;
9467 rc = 1;
9468 }else{
9469 sqlite3IoTrace = iotracePrintf;
9472 }else
9473 #endif
9475 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
9476 static const struct {
9477 const char *zLimitName; /* Name of a limit */
9478 int limitCode; /* Integer code for that limit */
9479 } aLimit[] = {
9480 { "length", SQLITE_LIMIT_LENGTH },
9481 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
9482 { "column", SQLITE_LIMIT_COLUMN },
9483 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
9484 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
9485 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
9486 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
9487 { "attached", SQLITE_LIMIT_ATTACHED },
9488 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
9489 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
9490 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
9491 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
9493 int i, n2;
9494 open_db(p, 0);
9495 if( nArg==1 ){
9496 for(i=0; i<ArraySize(aLimit); i++){
9497 sputf(stdout, "%20s %d\n", aLimit[i].zLimitName,
9498 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9500 }else if( nArg>3 ){
9501 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
9502 rc = 1;
9503 goto meta_command_exit;
9504 }else{
9505 int iLimit = -1;
9506 n2 = strlen30(azArg[1]);
9507 for(i=0; i<ArraySize(aLimit); i++){
9508 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9509 if( iLimit<0 ){
9510 iLimit = i;
9511 }else{
9512 eputf("ambiguous limit: \"%s\"\n", azArg[1]);
9513 rc = 1;
9514 goto meta_command_exit;
9518 if( iLimit<0 ){
9519 eputf("unknown limit: \"%s\"\n"
9520 "enter \".limits\" with no arguments for a list.\n",
9521 azArg[1]);
9522 rc = 1;
9523 goto meta_command_exit;
9525 if( nArg==3 ){
9526 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9527 (int)integerValue(azArg[2]));
9529 sputf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
9530 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9532 }else
9534 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
9535 open_db(p, 0);
9536 lintDotCommand(p, azArg, nArg);
9537 }else
9539 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9540 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
9541 const char *zFile, *zProc;
9542 char *zErrMsg = 0;
9543 failIfSafeMode(p, "cannot run .load in safe mode");
9544 if( nArg<2 || azArg[1][0]==0 ){
9545 /* Must have a non-empty FILE. (Will not load self.) */
9546 eputz("Usage: .load FILE ?ENTRYPOINT?\n");
9547 rc = 1;
9548 goto meta_command_exit;
9550 zFile = azArg[1];
9551 zProc = nArg>=3 ? azArg[2] : 0;
9552 open_db(p, 0);
9553 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9554 if( rc!=SQLITE_OK ){
9555 shellEmitError(zErrMsg);
9556 sqlite3_free(zErrMsg);
9557 rc = 1;
9559 }else
9560 #endif
9562 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
9563 if( nArg!=2 ){
9564 eputz("Usage: .log FILENAME\n");
9565 rc = 1;
9566 }else{
9567 const char *zFile = azArg[1];
9568 if( p->bSafeMode
9569 && cli_strcmp(zFile,"on")!=0
9570 && cli_strcmp(zFile,"off")!=0
9572 sputz(stdout, "cannot set .log to anything other"
9573 " than \"on\" or \"off\"\n");
9574 zFile = "off";
9576 output_file_close(p->pLog);
9577 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
9578 p->pLog = output_file_open(zFile, 0);
9580 }else
9582 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
9583 const char *zMode = 0;
9584 const char *zTabname = 0;
9585 int i, n2;
9586 ColModeOpts cmOpts = ColModeOpts_default;
9587 for(i=1; i<nArg; i++){
9588 const char *z = azArg[i];
9589 if( optionMatch(z,"wrap") && i+1<nArg ){
9590 cmOpts.iWrap = integerValue(azArg[++i]);
9591 }else if( optionMatch(z,"ww") ){
9592 cmOpts.bWordWrap = 1;
9593 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9594 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9595 }else if( optionMatch(z,"quote") ){
9596 cmOpts.bQuote = 1;
9597 }else if( optionMatch(z,"noquote") ){
9598 cmOpts.bQuote = 0;
9599 }else if( zMode==0 ){
9600 zMode = z;
9601 /* Apply defaults for qbox pseudo-mode. If that
9602 * overwrites already-set values, user was informed of this.
9604 if( cli_strcmp(z, "qbox")==0 ){
9605 ColModeOpts cmo = ColModeOpts_default_qbox;
9606 zMode = "box";
9607 cmOpts = cmo;
9609 }else if( zTabname==0 ){
9610 zTabname = z;
9611 }else if( z[0]=='-' ){
9612 eputf("unknown option: %s\n", z);
9613 eputz("options:\n"
9614 " --noquote\n"
9615 " --quote\n"
9616 " --wordwrap on/off\n"
9617 " --wrap N\n"
9618 " --ww\n");
9619 rc = 1;
9620 goto meta_command_exit;
9621 }else{
9622 eputf("extra argument: \"%s\"\n", z);
9623 rc = 1;
9624 goto meta_command_exit;
9627 if( zMode==0 ){
9628 if( p->mode==MODE_Column
9629 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9631 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9632 modeDescr[p->mode], p->cmOpts.iWrap,
9633 p->cmOpts.bWordWrap ? "on" : "off",
9634 p->cmOpts.bQuote ? "" : "no");
9635 }else{
9636 oputf("current output mode: %s\n", modeDescr[p->mode]);
9638 zMode = modeDescr[p->mode];
9640 n2 = strlen30(zMode);
9641 if( cli_strncmp(zMode,"lines",n2)==0 ){
9642 p->mode = MODE_Line;
9643 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9644 }else if( cli_strncmp(zMode,"columns",n2)==0 ){
9645 p->mode = MODE_Column;
9646 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9647 p->showHeader = 1;
9649 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9650 p->cmOpts = cmOpts;
9651 }else if( cli_strncmp(zMode,"list",n2)==0 ){
9652 p->mode = MODE_List;
9653 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9654 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9655 }else if( cli_strncmp(zMode,"html",n2)==0 ){
9656 p->mode = MODE_Html;
9657 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
9658 p->mode = MODE_Tcl;
9659 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9660 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9661 }else if( cli_strncmp(zMode,"csv",n2)==0 ){
9662 p->mode = MODE_Csv;
9663 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9664 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9665 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
9666 p->mode = MODE_List;
9667 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9668 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
9669 p->mode = MODE_Insert;
9670 set_table_name(p, zTabname ? zTabname : "table");
9671 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
9672 p->mode = MODE_Quote;
9673 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9674 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9675 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
9676 p->mode = MODE_Ascii;
9677 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9678 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9679 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
9680 p->mode = MODE_Markdown;
9681 p->cmOpts = cmOpts;
9682 }else if( cli_strncmp(zMode,"table",n2)==0 ){
9683 p->mode = MODE_Table;
9684 p->cmOpts = cmOpts;
9685 }else if( cli_strncmp(zMode,"box",n2)==0 ){
9686 p->mode = MODE_Box;
9687 p->cmOpts = cmOpts;
9688 }else if( cli_strncmp(zMode,"count",n2)==0 ){
9689 p->mode = MODE_Count;
9690 }else if( cli_strncmp(zMode,"off",n2)==0 ){
9691 p->mode = MODE_Off;
9692 }else if( cli_strncmp(zMode,"json",n2)==0 ){
9693 p->mode = MODE_Json;
9694 }else{
9695 eputz("Error: mode should be one of: "
9696 "ascii box column csv html insert json line list markdown "
9697 "qbox quote table tabs tcl\n");
9698 rc = 1;
9700 p->cMode = p->mode;
9701 }else
9703 #ifndef SQLITE_SHELL_FIDDLE
9704 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9705 if( nArg!=2 ){
9706 eputz("Usage: .nonce NONCE\n");
9707 rc = 1;
9708 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9709 eputf("line %d: incorrect nonce: \"%s\"\n",
9710 p->lineno, azArg[1]);
9711 exit(1);
9712 }else{
9713 p->bSafeMode = 0;
9714 return 0; /* Return immediately to bypass the safe mode reset
9715 ** at the end of this procedure */
9717 }else
9718 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9720 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9721 if( nArg==2 ){
9722 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9723 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9724 }else{
9725 eputz("Usage: .nullvalue STRING\n");
9726 rc = 1;
9728 }else
9730 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9731 const char *zFN = 0; /* Pointer to constant filename */
9732 char *zNewFilename = 0; /* Name of the database file to open */
9733 int iName = 1; /* Index in azArg[] of the filename */
9734 int newFlag = 0; /* True to delete file before opening */
9735 int openMode = SHELL_OPEN_UNSPEC;
9737 /* Check for command-line arguments */
9738 for(iName=1; iName<nArg; iName++){
9739 const char *z = azArg[iName];
9740 #ifndef SQLITE_SHELL_FIDDLE
9741 if( optionMatch(z,"new") ){
9742 newFlag = 1;
9743 #ifdef SQLITE_HAVE_ZLIB
9744 }else if( optionMatch(z, "zip") ){
9745 openMode = SHELL_OPEN_ZIPFILE;
9746 #endif
9747 }else if( optionMatch(z, "append") ){
9748 openMode = SHELL_OPEN_APPENDVFS;
9749 }else if( optionMatch(z, "readonly") ){
9750 openMode = SHELL_OPEN_READONLY;
9751 }else if( optionMatch(z, "nofollow") ){
9752 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9753 #ifndef SQLITE_OMIT_DESERIALIZE
9754 }else if( optionMatch(z, "deserialize") ){
9755 openMode = SHELL_OPEN_DESERIALIZE;
9756 }else if( optionMatch(z, "hexdb") ){
9757 openMode = SHELL_OPEN_HEXDB;
9758 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9759 p->szMax = integerValue(azArg[++iName]);
9760 #endif /* SQLITE_OMIT_DESERIALIZE */
9761 }else
9762 #endif /* !SQLITE_SHELL_FIDDLE */
9763 if( z[0]=='-' ){
9764 eputf("unknown option: %s\n", z);
9765 rc = 1;
9766 goto meta_command_exit;
9767 }else if( zFN ){
9768 eputf("extra argument: \"%s\"\n", z);
9769 rc = 1;
9770 goto meta_command_exit;
9771 }else{
9772 zFN = z;
9776 /* Close the existing database */
9777 session_close_all(p, -1);
9778 close_db(p->db);
9779 p->db = 0;
9780 p->pAuxDb->zDbFilename = 0;
9781 sqlite3_free(p->pAuxDb->zFreeOnClose);
9782 p->pAuxDb->zFreeOnClose = 0;
9783 p->openMode = openMode;
9784 p->openFlags = 0;
9785 p->szMax = 0;
9787 /* If a filename is specified, try to open it first */
9788 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9789 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9790 #ifndef SQLITE_SHELL_FIDDLE
9791 if( p->bSafeMode
9792 && p->openMode!=SHELL_OPEN_HEXDB
9793 && zFN
9794 && cli_strcmp(zFN,":memory:")!=0
9796 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9798 #else
9799 /* WASM mode has its own sandboxed pseudo-filesystem. */
9800 #endif
9801 if( zFN ){
9802 zNewFilename = sqlite3_mprintf("%s", zFN);
9803 shell_check_oom(zNewFilename);
9804 }else{
9805 zNewFilename = 0;
9807 p->pAuxDb->zDbFilename = zNewFilename;
9808 open_db(p, OPEN_DB_KEEPALIVE);
9809 if( p->db==0 ){
9810 eputf("Error: cannot open '%s'\n", zNewFilename);
9811 sqlite3_free(zNewFilename);
9812 }else{
9813 p->pAuxDb->zFreeOnClose = zNewFilename;
9816 if( p->db==0 ){
9817 /* As a fall-back open a TEMP database */
9818 p->pAuxDb->zDbFilename = 0;
9819 open_db(p, 0);
9821 }else
9823 #ifndef SQLITE_SHELL_FIDDLE
9824 if( (c=='o'
9825 && (cli_strncmp(azArg[0], "output", n)==0
9826 || cli_strncmp(azArg[0], "once", n)==0))
9827 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9829 char *zFile = 0;
9830 int bTxtMode = 0;
9831 int i;
9832 int eMode = 0;
9833 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
9834 static const char *zBomUtf8 = "\xef\xbb\xbf";
9835 const char *zBom = 0;
9837 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9838 if( c=='e' ){
9839 eMode = 'x';
9840 bOnce = 2;
9841 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9842 bOnce = 1;
9844 for(i=1; i<nArg; i++){
9845 char *z = azArg[i];
9846 if( z[0]=='-' ){
9847 if( z[1]=='-' ) z++;
9848 if( cli_strcmp(z,"-bom")==0 ){
9849 zBom = zBomUtf8;
9850 }else if( c!='e' && cli_strcmp(z,"-x")==0 ){
9851 eMode = 'x'; /* spreadsheet */
9852 }else if( c!='e' && cli_strcmp(z,"-e")==0 ){
9853 eMode = 'e'; /* text editor */
9854 }else{
9855 oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
9856 showHelp(p->out, azArg[0]);
9857 rc = 1;
9858 goto meta_command_exit;
9860 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
9861 zFile = sqlite3_mprintf("%s", z);
9862 if( zFile && zFile[0]=='|' ){
9863 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9864 break;
9866 }else{
9867 oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
9868 showHelp(p->out, azArg[0]);
9869 rc = 1;
9870 sqlite3_free(zFile);
9871 goto meta_command_exit;
9874 if( zFile==0 ){
9875 zFile = sqlite3_mprintf("stdout");
9877 if( bOnce ){
9878 p->outCount = 2;
9879 }else{
9880 p->outCount = 0;
9882 output_reset(p);
9883 #ifndef SQLITE_NOHAVE_SYSTEM
9884 if( eMode=='e' || eMode=='x' ){
9885 p->doXdgOpen = 1;
9886 outputModePush(p);
9887 if( eMode=='x' ){
9888 /* spreadsheet mode. Output as CSV. */
9889 newTempFile(p, "csv");
9890 ShellClearFlag(p, SHFLG_Echo);
9891 p->mode = MODE_Csv;
9892 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9893 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9894 }else{
9895 /* text editor mode */
9896 newTempFile(p, "txt");
9897 bTxtMode = 1;
9899 sqlite3_free(zFile);
9900 zFile = sqlite3_mprintf("%s", p->zTempFile);
9902 #endif /* SQLITE_NOHAVE_SYSTEM */
9903 shell_check_oom(zFile);
9904 if( zFile[0]=='|' ){
9905 #ifdef SQLITE_OMIT_POPEN
9906 eputz("Error: pipes are not supported in this OS\n");
9907 rc = 1;
9908 output_redir(p, stdout);
9909 #else
9910 FILE *pfPipe = popen(zFile + 1, "w");
9911 if( pfPipe==0 ){
9912 eputf("Error: cannot open pipe \"%s\"\n", zFile + 1);
9913 rc = 1;
9914 }else{
9915 output_redir(p, pfPipe);
9916 if( zBom ) oputz(zBom);
9917 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9919 #endif
9920 }else{
9921 FILE *pfFile = output_file_open(zFile, bTxtMode);
9922 if( pfFile==0 ){
9923 if( cli_strcmp(zFile,"off")!=0 ){
9924 eputf("Error: cannot write to \"%s\"\n", zFile);
9926 rc = 1;
9927 } else {
9928 output_redir(p, pfFile);
9929 if( zBom ) oputz(zBom);
9930 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
9933 sqlite3_free(zFile);
9934 }else
9935 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9937 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
9938 open_db(p,0);
9939 if( nArg<=1 ) goto parameter_syntax_error;
9941 /* .parameter clear
9942 ** Clear all bind parameters by dropping the TEMP table that holds them.
9944 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
9945 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9946 0, 0, 0);
9947 }else
9949 /* .parameter list
9950 ** List all bind parameters.
9952 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
9953 sqlite3_stmt *pStmt = 0;
9954 int rx;
9955 int len = 0;
9956 rx = sqlite3_prepare_v2(p->db,
9957 "SELECT max(length(key)) "
9958 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9959 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9960 len = sqlite3_column_int(pStmt, 0);
9961 if( len>40 ) len = 40;
9963 sqlite3_finalize(pStmt);
9964 pStmt = 0;
9965 if( len ){
9966 rx = sqlite3_prepare_v2(p->db,
9967 "SELECT key, quote(value) "
9968 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
9969 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9970 oputf("%-*s %s\n", len, sqlite3_column_text(pStmt,0),
9971 sqlite3_column_text(pStmt,1));
9973 sqlite3_finalize(pStmt);
9975 }else
9977 /* .parameter init
9978 ** Make sure the TEMP table used to hold bind parameters exists.
9979 ** Create it if necessary.
9981 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
9982 bind_table_init(p);
9983 }else
9985 /* .parameter set NAME VALUE
9986 ** Set or reset a bind parameter. NAME should be the full parameter
9987 ** name exactly as it appears in the query. (ex: $abc, @def). The
9988 ** VALUE can be in either SQL literal notation, or if not it will be
9989 ** understood to be a text string.
9991 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
9992 int rx;
9993 char *zSql;
9994 sqlite3_stmt *pStmt;
9995 const char *zKey = azArg[2];
9996 const char *zValue = azArg[3];
9997 bind_table_init(p);
9998 zSql = sqlite3_mprintf(
9999 "REPLACE INTO temp.sqlite_parameters(key,value)"
10000 "VALUES(%Q,%s);", zKey, zValue);
10001 shell_check_oom(zSql);
10002 pStmt = 0;
10003 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10004 sqlite3_free(zSql);
10005 if( rx!=SQLITE_OK ){
10006 sqlite3_finalize(pStmt);
10007 pStmt = 0;
10008 zSql = sqlite3_mprintf(
10009 "REPLACE INTO temp.sqlite_parameters(key,value)"
10010 "VALUES(%Q,%Q);", zKey, zValue);
10011 shell_check_oom(zSql);
10012 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10013 sqlite3_free(zSql);
10014 if( rx!=SQLITE_OK ){
10015 oputf("Error: %s\n", sqlite3_errmsg(p->db));
10016 sqlite3_finalize(pStmt);
10017 pStmt = 0;
10018 rc = 1;
10021 sqlite3_step(pStmt);
10022 sqlite3_finalize(pStmt);
10023 }else
10025 /* .parameter unset NAME
10026 ** Remove the NAME binding from the parameter binding table, if it
10027 ** exists.
10029 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
10030 char *zSql = sqlite3_mprintf(
10031 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
10032 shell_check_oom(zSql);
10033 sqlite3_exec(p->db, zSql, 0, 0, 0);
10034 sqlite3_free(zSql);
10035 }else
10036 /* If no command name matches, show a syntax error */
10037 parameter_syntax_error:
10038 showHelp(p->out, "parameter");
10039 }else
10041 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
10042 int i;
10043 for(i=1; i<nArg; i++){
10044 if( i>1 ) oputz(" ");
10045 oputz(azArg[i]);
10047 oputz("\n");
10048 }else
10050 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10051 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
10052 int i;
10053 int nn = 0;
10054 p->flgProgress = 0;
10055 p->mxProgress = 0;
10056 p->nProgress = 0;
10057 for(i=1; i<nArg; i++){
10058 const char *z = azArg[i];
10059 if( z[0]=='-' ){
10060 z++;
10061 if( z[0]=='-' ) z++;
10062 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
10063 p->flgProgress |= SHELL_PROGRESS_QUIET;
10064 continue;
10066 if( cli_strcmp(z,"reset")==0 ){
10067 p->flgProgress |= SHELL_PROGRESS_RESET;
10068 continue;
10070 if( cli_strcmp(z,"once")==0 ){
10071 p->flgProgress |= SHELL_PROGRESS_ONCE;
10072 continue;
10074 if( cli_strcmp(z,"limit")==0 ){
10075 if( i+1>=nArg ){
10076 eputz("Error: missing argument on --limit\n");
10077 rc = 1;
10078 goto meta_command_exit;
10079 }else{
10080 p->mxProgress = (int)integerValue(azArg[++i]);
10082 continue;
10084 eputf("Error: unknown option: \"%s\"\n", azArg[i]);
10085 rc = 1;
10086 goto meta_command_exit;
10087 }else{
10088 nn = (int)integerValue(z);
10091 open_db(p, 0);
10092 sqlite3_progress_handler(p->db, nn, progress_handler, p);
10093 }else
10094 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10096 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
10097 if( nArg >= 2) {
10098 shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
10100 if( nArg >= 3) {
10101 shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
10103 }else
10105 #ifndef SQLITE_SHELL_FIDDLE
10106 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
10107 rc = 2;
10108 }else
10109 #endif
10111 #ifndef SQLITE_SHELL_FIDDLE
10112 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
10113 FILE *inSaved = p->in;
10114 int savedLineno = p->lineno;
10115 failIfSafeMode(p, "cannot run .read in safe mode");
10116 if( nArg!=2 ){
10117 eputz("Usage: .read FILE\n");
10118 rc = 1;
10119 goto meta_command_exit;
10121 if( azArg[1][0]=='|' ){
10122 #ifdef SQLITE_OMIT_POPEN
10123 eputz("Error: pipes are not supported in this OS\n");
10124 rc = 1;
10125 p->out = stdout;
10126 #else
10127 p->in = popen(azArg[1]+1, "r");
10128 if( p->in==0 ){
10129 eputf("Error: cannot open \"%s\"\n", azArg[1]);
10130 rc = 1;
10131 }else{
10132 rc = process_input(p);
10133 pclose(p->in);
10135 #endif
10136 }else if( (p->in = openChrSource(azArg[1]))==0 ){
10137 eputf("Error: cannot open \"%s\"\n", azArg[1]);
10138 rc = 1;
10139 }else{
10140 rc = process_input(p);
10141 fclose(p->in);
10143 p->in = inSaved;
10144 p->lineno = savedLineno;
10145 }else
10146 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10148 #ifndef SQLITE_SHELL_FIDDLE
10149 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
10150 const char *zSrcFile;
10151 const char *zDb;
10152 sqlite3 *pSrc;
10153 sqlite3_backup *pBackup;
10154 int nTimeout = 0;
10156 failIfSafeMode(p, "cannot run .restore in safe mode");
10157 if( nArg==2 ){
10158 zSrcFile = azArg[1];
10159 zDb = "main";
10160 }else if( nArg==3 ){
10161 zSrcFile = azArg[2];
10162 zDb = azArg[1];
10163 }else{
10164 eputz("Usage: .restore ?DB? FILE\n");
10165 rc = 1;
10166 goto meta_command_exit;
10168 rc = sqlite3_open(zSrcFile, &pSrc);
10169 if( rc!=SQLITE_OK ){
10170 eputf("Error: cannot open \"%s\"\n", zSrcFile);
10171 close_db(pSrc);
10172 return 1;
10174 open_db(p, 0);
10175 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10176 if( pBackup==0 ){
10177 shellDatabaseError(p->db);
10178 close_db(pSrc);
10179 return 1;
10181 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10182 || rc==SQLITE_BUSY ){
10183 if( rc==SQLITE_BUSY ){
10184 if( nTimeout++ >= 3 ) break;
10185 sqlite3_sleep(100);
10188 sqlite3_backup_finish(pBackup);
10189 if( rc==SQLITE_DONE ){
10190 rc = 0;
10191 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10192 eputz("Error: source database is busy\n");
10193 rc = 1;
10194 }else{
10195 shellDatabaseError(p->db);
10196 rc = 1;
10198 close_db(pSrc);
10199 }else
10200 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10202 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
10203 if( nArg==2 ){
10204 if( cli_strcmp(azArg[1], "vm")==0 ){
10205 p->scanstatsOn = 3;
10206 }else
10207 if( cli_strcmp(azArg[1], "est")==0 ){
10208 p->scanstatsOn = 2;
10209 }else{
10210 p->scanstatsOn = (u8)booleanValue(azArg[1]);
10212 open_db(p, 0);
10213 sqlite3_db_config(
10214 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
10216 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
10217 eputz("Warning: .scanstats not available in this build.\n");
10218 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
10219 if( p->scanstatsOn==3 ){
10220 eputz("Warning: \".scanstats vm\" not available in this build.\n");
10222 #endif
10223 }else{
10224 eputz("Usage: .scanstats on|off|est\n");
10225 rc = 1;
10227 }else
10229 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
10230 ShellText sSelect;
10231 ShellState data;
10232 char *zErrMsg = 0;
10233 const char *zDiv = "(";
10234 const char *zName = 0;
10235 int iSchema = 0;
10236 int bDebug = 0;
10237 int bNoSystemTabs = 0;
10238 int ii;
10240 open_db(p, 0);
10241 memcpy(&data, p, sizeof(data));
10242 data.showHeader = 0;
10243 data.cMode = data.mode = MODE_Semi;
10244 initText(&sSelect);
10245 for(ii=1; ii<nArg; ii++){
10246 if( optionMatch(azArg[ii],"indent") ){
10247 data.cMode = data.mode = MODE_Pretty;
10248 }else if( optionMatch(azArg[ii],"debug") ){
10249 bDebug = 1;
10250 }else if( optionMatch(azArg[ii],"nosys") ){
10251 bNoSystemTabs = 1;
10252 }else if( azArg[ii][0]=='-' ){
10253 eputf("Unknown option: \"%s\"\n", azArg[ii]);
10254 rc = 1;
10255 goto meta_command_exit;
10256 }else if( zName==0 ){
10257 zName = azArg[ii];
10258 }else{
10259 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10260 rc = 1;
10261 goto meta_command_exit;
10264 if( zName!=0 ){
10265 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10266 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10267 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10268 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10269 if( isSchema ){
10270 char *new_argv[2], *new_colv[2];
10271 new_argv[0] = sqlite3_mprintf(
10272 "CREATE TABLE %s (\n"
10273 " type text,\n"
10274 " name text,\n"
10275 " tbl_name text,\n"
10276 " rootpage integer,\n"
10277 " sql text\n"
10278 ")", zName);
10279 shell_check_oom(new_argv[0]);
10280 new_argv[1] = 0;
10281 new_colv[0] = "sql";
10282 new_colv[1] = 0;
10283 callback(&data, 1, new_argv, new_colv);
10284 sqlite3_free(new_argv[0]);
10287 if( zDiv ){
10288 sqlite3_stmt *pStmt = 0;
10289 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10290 -1, &pStmt, 0);
10291 if( rc ){
10292 shellDatabaseError(p->db);
10293 sqlite3_finalize(pStmt);
10294 rc = 1;
10295 goto meta_command_exit;
10297 appendText(&sSelect, "SELECT sql FROM", 0);
10298 iSchema = 0;
10299 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10300 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10301 char zScNum[30];
10302 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10303 appendText(&sSelect, zDiv, 0);
10304 zDiv = " UNION ALL ";
10305 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10306 if( sqlite3_stricmp(zDb, "main")!=0 ){
10307 appendText(&sSelect, zDb, '\'');
10308 }else{
10309 appendText(&sSelect, "NULL", 0);
10311 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10312 appendText(&sSelect, zScNum, 0);
10313 appendText(&sSelect, " AS snum, ", 0);
10314 appendText(&sSelect, zDb, '\'');
10315 appendText(&sSelect, " AS sname FROM ", 0);
10316 appendText(&sSelect, zDb, quoteChar(zDb));
10317 appendText(&sSelect, ".sqlite_schema", 0);
10319 sqlite3_finalize(pStmt);
10320 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10321 if( zName ){
10322 appendText(&sSelect,
10323 " UNION ALL SELECT shell_module_schema(name),"
10324 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10327 #endif
10328 appendText(&sSelect, ") WHERE ", 0);
10329 if( zName ){
10330 char *zQarg = sqlite3_mprintf("%Q", zName);
10331 int bGlob;
10332 shell_check_oom(zQarg);
10333 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10334 strchr(zName, '[') != 0;
10335 if( strchr(zName, '.') ){
10336 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10337 }else{
10338 appendText(&sSelect, "lower(tbl_name)", 0);
10340 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10341 appendText(&sSelect, zQarg, 0);
10342 if( !bGlob ){
10343 appendText(&sSelect, " ESCAPE '\\' ", 0);
10345 appendText(&sSelect, " AND ", 0);
10346 sqlite3_free(zQarg);
10348 if( bNoSystemTabs ){
10349 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10351 appendText(&sSelect, "sql IS NOT NULL"
10352 " ORDER BY snum, rowid", 0);
10353 if( bDebug ){
10354 oputf("SQL: %s;\n", sSelect.z);
10355 }else{
10356 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10358 freeText(&sSelect);
10360 if( zErrMsg ){
10361 shellEmitError(zErrMsg);
10362 sqlite3_free(zErrMsg);
10363 rc = 1;
10364 }else if( rc != SQLITE_OK ){
10365 eputz("Error: querying schema information\n");
10366 rc = 1;
10367 }else{
10368 rc = 0;
10370 }else
10372 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
10373 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
10375 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10376 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10377 }else
10379 #if defined(SQLITE_ENABLE_SESSION)
10380 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
10381 struct AuxDb *pAuxDb = p->pAuxDb;
10382 OpenSession *pSession = &pAuxDb->aSession[0];
10383 char **azCmd = &azArg[1];
10384 int iSes = 0;
10385 int nCmd = nArg - 1;
10386 int i;
10387 if( nArg<=1 ) goto session_syntax_error;
10388 open_db(p, 0);
10389 if( nArg>=3 ){
10390 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10391 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10393 if( iSes<pAuxDb->nSession ){
10394 pSession = &pAuxDb->aSession[iSes];
10395 azCmd++;
10396 nCmd--;
10397 }else{
10398 pSession = &pAuxDb->aSession[0];
10399 iSes = 0;
10403 /* .session attach TABLE
10404 ** Invoke the sqlite3session_attach() interface to attach a particular
10405 ** table so that it is never filtered.
10407 if( cli_strcmp(azCmd[0],"attach")==0 ){
10408 if( nCmd!=2 ) goto session_syntax_error;
10409 if( pSession->p==0 ){
10410 session_not_open:
10411 eputz("ERROR: No sessions are open\n");
10412 }else{
10413 rc = sqlite3session_attach(pSession->p, azCmd[1]);
10414 if( rc ){
10415 eputf("ERROR: sqlite3session_attach() returns %d\n",rc);
10416 rc = 0;
10419 }else
10421 /* .session changeset FILE
10422 ** .session patchset FILE
10423 ** Write a changeset or patchset into a file. The file is overwritten.
10425 if( cli_strcmp(azCmd[0],"changeset")==0
10426 || cli_strcmp(azCmd[0],"patchset")==0
10428 FILE *out = 0;
10429 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10430 if( nCmd!=2 ) goto session_syntax_error;
10431 if( pSession->p==0 ) goto session_not_open;
10432 out = fopen(azCmd[1], "wb");
10433 if( out==0 ){
10434 eputf("ERROR: cannot open \"%s\" for writing\n",
10435 azCmd[1]);
10436 }else{
10437 int szChng;
10438 void *pChng;
10439 if( azCmd[0][0]=='c' ){
10440 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10441 }else{
10442 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10444 if( rc ){
10445 sputf(stdout, "Error: error code %d\n", rc);
10446 rc = 0;
10448 if( pChng
10449 && fwrite(pChng, szChng, 1, out)!=1 ){
10450 eputf("ERROR: Failed to write entire %d-byte output\n", szChng);
10452 sqlite3_free(pChng);
10453 fclose(out);
10455 }else
10457 /* .session close
10458 ** Close the identified session
10460 if( cli_strcmp(azCmd[0], "close")==0 ){
10461 if( nCmd!=1 ) goto session_syntax_error;
10462 if( pAuxDb->nSession ){
10463 session_close(pSession);
10464 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10466 }else
10468 /* .session enable ?BOOLEAN?
10469 ** Query or set the enable flag
10471 if( cli_strcmp(azCmd[0], "enable")==0 ){
10472 int ii;
10473 if( nCmd>2 ) goto session_syntax_error;
10474 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10475 if( pAuxDb->nSession ){
10476 ii = sqlite3session_enable(pSession->p, ii);
10477 oputf("session %s enable flag = %d\n", pSession->zName, ii);
10479 }else
10481 /* .session filter GLOB ....
10482 ** Set a list of GLOB patterns of table names to be excluded.
10484 if( cli_strcmp(azCmd[0], "filter")==0 ){
10485 int ii, nByte;
10486 if( nCmd<2 ) goto session_syntax_error;
10487 if( pAuxDb->nSession ){
10488 for(ii=0; ii<pSession->nFilter; ii++){
10489 sqlite3_free(pSession->azFilter[ii]);
10491 sqlite3_free(pSession->azFilter);
10492 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10493 pSession->azFilter = sqlite3_malloc( nByte );
10494 shell_check_oom( pSession->azFilter );
10495 for(ii=1; ii<nCmd; ii++){
10496 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10497 shell_check_oom(x);
10499 pSession->nFilter = ii-1;
10501 }else
10503 /* .session indirect ?BOOLEAN?
10504 ** Query or set the indirect flag
10506 if( cli_strcmp(azCmd[0], "indirect")==0 ){
10507 int ii;
10508 if( nCmd>2 ) goto session_syntax_error;
10509 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10510 if( pAuxDb->nSession ){
10511 ii = sqlite3session_indirect(pSession->p, ii);
10512 oputf("session %s indirect flag = %d\n", pSession->zName, ii);
10514 }else
10516 /* .session isempty
10517 ** Determine if the session is empty
10519 if( cli_strcmp(azCmd[0], "isempty")==0 ){
10520 int ii;
10521 if( nCmd!=1 ) goto session_syntax_error;
10522 if( pAuxDb->nSession ){
10523 ii = sqlite3session_isempty(pSession->p);
10524 oputf("session %s isempty flag = %d\n", pSession->zName, ii);
10526 }else
10528 /* .session list
10529 ** List all currently open sessions
10531 if( cli_strcmp(azCmd[0],"list")==0 ){
10532 for(i=0; i<pAuxDb->nSession; i++){
10533 oputf("%d %s\n", i, pAuxDb->aSession[i].zName);
10535 }else
10537 /* .session open DB NAME
10538 ** Open a new session called NAME on the attached database DB.
10539 ** DB is normally "main".
10541 if( cli_strcmp(azCmd[0],"open")==0 ){
10542 char *zName;
10543 if( nCmd!=3 ) goto session_syntax_error;
10544 zName = azCmd[2];
10545 if( zName[0]==0 ) goto session_syntax_error;
10546 for(i=0; i<pAuxDb->nSession; i++){
10547 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10548 eputf("Session \"%s\" already exists\n", zName);
10549 goto meta_command_exit;
10552 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10553 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10554 goto meta_command_exit;
10556 pSession = &pAuxDb->aSession[pAuxDb->nSession];
10557 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10558 if( rc ){
10559 eputf("Cannot open session: error code=%d\n", rc);
10560 rc = 0;
10561 goto meta_command_exit;
10563 pSession->nFilter = 0;
10564 sqlite3session_table_filter(pSession->p, session_filter, pSession);
10565 pAuxDb->nSession++;
10566 pSession->zName = sqlite3_mprintf("%s", zName);
10567 shell_check_oom(pSession->zName);
10568 }else
10569 /* If no command name matches, show a syntax error */
10570 session_syntax_error:
10571 showHelp(p->out, "session");
10572 }else
10573 #endif
10575 #ifdef SQLITE_DEBUG
10576 /* Undocumented commands for internal testing. Subject to change
10577 ** without notice. */
10578 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
10579 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10580 int i, v;
10581 for(i=1; i<nArg; i++){
10582 v = booleanValue(azArg[i]);
10583 oputf("%s: %d 0x%x\n", azArg[i], v, v);
10586 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
10587 int i; sqlite3_int64 v;
10588 for(i=1; i<nArg; i++){
10589 char zBuf[200];
10590 v = integerValue(azArg[i]);
10591 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10592 oputz(zBuf);
10595 }else
10596 #endif
10598 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
10599 int bIsInit = 0; /* True to initialize the SELFTEST table */
10600 int bVerbose = 0; /* Verbose output */
10601 int bSelftestExists; /* True if SELFTEST already exists */
10602 int i, k; /* Loop counters */
10603 int nTest = 0; /* Number of tests runs */
10604 int nErr = 0; /* Number of errors seen */
10605 ShellText str; /* Answer for a query */
10606 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10608 open_db(p,0);
10609 for(i=1; i<nArg; i++){
10610 const char *z = azArg[i];
10611 if( z[0]=='-' && z[1]=='-' ) z++;
10612 if( cli_strcmp(z,"-init")==0 ){
10613 bIsInit = 1;
10614 }else
10615 if( cli_strcmp(z,"-v")==0 ){
10616 bVerbose++;
10617 }else
10619 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
10620 eputz("Should be one of: --init -v\n");
10621 rc = 1;
10622 goto meta_command_exit;
10625 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10626 != SQLITE_OK ){
10627 bSelftestExists = 0;
10628 }else{
10629 bSelftestExists = 1;
10631 if( bIsInit ){
10632 createSelftestTable(p);
10633 bSelftestExists = 1;
10635 initText(&str);
10636 appendText(&str, "x", 0);
10637 for(k=bSelftestExists; k>=0; k--){
10638 if( k==1 ){
10639 rc = sqlite3_prepare_v2(p->db,
10640 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10641 -1, &pStmt, 0);
10642 }else{
10643 rc = sqlite3_prepare_v2(p->db,
10644 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10645 " (1,'run','PRAGMA integrity_check','ok')",
10646 -1, &pStmt, 0);
10648 if( rc ){
10649 eputz("Error querying the selftest table\n");
10650 rc = 1;
10651 sqlite3_finalize(pStmt);
10652 goto meta_command_exit;
10654 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10655 int tno = sqlite3_column_int(pStmt, 0);
10656 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10657 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10658 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10660 if( zOp==0 ) continue;
10661 if( zSql==0 ) continue;
10662 if( zAns==0 ) continue;
10663 k = 0;
10664 if( bVerbose>0 ){
10665 sputf(stdout, "%d: %s %s\n", tno, zOp, zSql);
10667 if( cli_strcmp(zOp,"memo")==0 ){
10668 oputf("%s\n", zSql);
10669 }else
10670 if( cli_strcmp(zOp,"run")==0 ){
10671 char *zErrMsg = 0;
10672 str.n = 0;
10673 str.z[0] = 0;
10674 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10675 nTest++;
10676 if( bVerbose ){
10677 oputf("Result: %s\n", str.z);
10679 if( rc || zErrMsg ){
10680 nErr++;
10681 rc = 1;
10682 oputf("%d: error-code-%d: %s\n", tno, rc, zErrMsg);
10683 sqlite3_free(zErrMsg);
10684 }else if( cli_strcmp(zAns,str.z)!=0 ){
10685 nErr++;
10686 rc = 1;
10687 oputf("%d: Expected: [%s]\n", tno, zAns);
10688 oputf("%d: Got: [%s]\n", tno, str.z);
10691 else{
10692 eputf("Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10693 rc = 1;
10694 break;
10696 } /* End loop over rows of content from SELFTEST */
10697 sqlite3_finalize(pStmt);
10698 } /* End loop over k */
10699 freeText(&str);
10700 oputf("%d errors out of %d tests\n", nErr, nTest);
10701 }else
10703 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10704 if( nArg<2 || nArg>3 ){
10705 eputz("Usage: .separator COL ?ROW?\n");
10706 rc = 1;
10708 if( nArg>=2 ){
10709 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10710 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10712 if( nArg>=3 ){
10713 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10714 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10716 }else
10718 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10719 const char *zLike = 0; /* Which table to checksum. 0 means everything */
10720 int i; /* Loop counter */
10721 int bSchema = 0; /* Also hash the schema */
10722 int bSeparate = 0; /* Hash each table separately */
10723 int iSize = 224; /* Hash algorithm to use */
10724 int bDebug = 0; /* Only show the query that would have run */
10725 sqlite3_stmt *pStmt; /* For querying tables names */
10726 char *zSql; /* SQL to be run */
10727 char *zSep; /* Separator */
10728 ShellText sSql; /* Complete SQL for the query to run the hash */
10729 ShellText sQuery; /* Set of queries used to read all content */
10730 open_db(p, 0);
10731 for(i=1; i<nArg; i++){
10732 const char *z = azArg[i];
10733 if( z[0]=='-' ){
10734 z++;
10735 if( z[0]=='-' ) z++;
10736 if( cli_strcmp(z,"schema")==0 ){
10737 bSchema = 1;
10738 }else
10739 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10740 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10742 iSize = atoi(&z[5]);
10743 }else
10744 if( cli_strcmp(z,"debug")==0 ){
10745 bDebug = 1;
10746 }else
10748 eputf("Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
10749 showHelp(p->out, azArg[0]);
10750 rc = 1;
10751 goto meta_command_exit;
10753 }else if( zLike ){
10754 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10755 rc = 1;
10756 goto meta_command_exit;
10757 }else{
10758 zLike = z;
10759 bSeparate = 1;
10760 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10763 if( bSchema ){
10764 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10765 " WHERE type='table' AND coalesce(rootpage,0)>1"
10766 " UNION ALL SELECT 'sqlite_schema'"
10767 " ORDER BY 1 collate nocase";
10768 }else{
10769 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10770 " WHERE type='table' AND coalesce(rootpage,0)>1"
10771 " AND name NOT LIKE 'sqlite_%'"
10772 " ORDER BY 1 collate nocase";
10774 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10775 initText(&sQuery);
10776 initText(&sSql);
10777 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10778 zSep = "VALUES(";
10779 while( SQLITE_ROW==sqlite3_step(pStmt) ){
10780 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10781 if( zTab==0 ) continue;
10782 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10783 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10784 appendText(&sQuery,"SELECT * FROM ", 0);
10785 appendText(&sQuery,zTab,'"');
10786 appendText(&sQuery," NOT INDEXED;", 0);
10787 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10788 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10789 " ORDER BY name;", 0);
10790 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10791 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10792 " ORDER BY name;", 0);
10793 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10794 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10795 " ORDER BY tbl,idx;", 0);
10796 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10797 appendText(&sQuery, "SELECT * FROM ", 0);
10798 appendText(&sQuery, zTab, 0);
10799 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10801 appendText(&sSql, zSep, 0);
10802 appendText(&sSql, sQuery.z, '\'');
10803 sQuery.n = 0;
10804 appendText(&sSql, ",", 0);
10805 appendText(&sSql, zTab, '\'');
10806 zSep = "),(";
10808 sqlite3_finalize(pStmt);
10809 if( bSeparate ){
10810 zSql = sqlite3_mprintf(
10811 "%s))"
10812 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10813 " FROM [sha3sum$query]",
10814 sSql.z, iSize);
10815 }else{
10816 zSql = sqlite3_mprintf(
10817 "%s))"
10818 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10819 " FROM [sha3sum$query]",
10820 sSql.z, iSize);
10822 shell_check_oom(zSql);
10823 freeText(&sQuery);
10824 freeText(&sSql);
10825 if( bDebug ){
10826 oputf("%s\n", zSql);
10827 }else{
10828 shell_exec(p, zSql, 0);
10830 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10832 int lrc;
10833 char *zRevText = /* Query for reversible to-blob-to-text check */
10834 "SELECT lower(name) as tname FROM sqlite_schema\n"
10835 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
10836 "AND name NOT LIKE 'sqlite_%%'%s\n"
10837 "ORDER BY 1 collate nocase";
10838 zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
10839 zRevText = sqlite3_mprintf(
10840 /* lower-case query is first run, producing upper-case query. */
10841 "with tabcols as materialized(\n"
10842 "select tname, cname\n"
10843 "from ("
10844 " select printf('\"%%w\"',ss.tname) as tname,"
10845 " printf('\"%%w\"',ti.name) as cname\n"
10846 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
10847 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
10848 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
10849 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
10850 "FROM '||tname||' WHERE '\n"
10851 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
10852 "|| ' AND typeof('||cname||')=''text'' ',\n"
10853 "' OR ') as query, tname from tabcols group by tname)"
10854 , zRevText);
10855 shell_check_oom(zRevText);
10856 if( bDebug ) oputf("%s\n", zRevText);
10857 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
10858 if( lrc!=SQLITE_OK ){
10859 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
10860 ** user does cruel and unnatural things like ".limit expr_depth 0". */
10861 rc = 1;
10862 }else{
10863 if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
10864 lrc = SQLITE_ROW==sqlite3_step(pStmt);
10865 if( lrc ){
10866 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
10867 sqlite3_stmt *pCheckStmt;
10868 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
10869 if( bDebug ) oputf("%s\n", zGenQuery);
10870 if( lrc!=SQLITE_OK ){
10871 rc = 1;
10872 }else{
10873 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
10874 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
10875 if( countIrreversible>0 ){
10876 int sz = (int)(countIrreversible + 0.5);
10877 eputf("Digest includes %d invalidly encoded text field%s.\n",
10878 sz, (sz>1)? "s": "");
10881 sqlite3_finalize(pCheckStmt);
10883 sqlite3_finalize(pStmt);
10886 if( rc ) eputz(".sha3sum failed.\n");
10887 sqlite3_free(zRevText);
10889 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
10890 sqlite3_free(zSql);
10891 }else
10893 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10894 if( c=='s'
10895 && (cli_strncmp(azArg[0], "shell", n)==0
10896 || cli_strncmp(azArg[0],"system",n)==0)
10898 char *zCmd;
10899 int i, x;
10900 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
10901 if( nArg<2 ){
10902 eputz("Usage: .system COMMAND\n");
10903 rc = 1;
10904 goto meta_command_exit;
10906 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
10907 for(i=2; i<nArg && zCmd!=0; i++){
10908 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
10909 zCmd, azArg[i]);
10911 consoleRestore();
10912 x = zCmd!=0 ? system(zCmd) : 1;
10913 consoleRenewSetup();
10914 sqlite3_free(zCmd);
10915 if( x ) eputf("System command returns %d\n", x);
10916 }else
10917 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10919 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
10920 static const char *azBool[] = { "off", "on", "trigger", "full"};
10921 const char *zOut;
10922 int i;
10923 if( nArg!=1 ){
10924 eputz("Usage: .show\n");
10925 rc = 1;
10926 goto meta_command_exit;
10928 oputf("%12.12s: %s\n","echo",
10929 azBool[ShellHasFlag(p, SHFLG_Echo)]);
10930 oputf("%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
10931 oputf("%12.12s: %s\n","explain",
10932 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
10933 oputf("%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
10934 if( p->mode==MODE_Column
10935 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
10937 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10938 modeDescr[p->mode], p->cmOpts.iWrap,
10939 p->cmOpts.bWordWrap ? "on" : "off",
10940 p->cmOpts.bQuote ? "" : "no");
10941 }else{
10942 oputf("%12.12s: %s\n","mode", modeDescr[p->mode]);
10944 oputf("%12.12s: ", "nullvalue");
10945 output_c_string(p->nullValue);
10946 oputz("\n");
10947 oputf("%12.12s: %s\n","output",
10948 strlen30(p->outfile) ? p->outfile : "stdout");
10949 oputf("%12.12s: ", "colseparator");
10950 output_c_string(p->colSeparator);
10951 oputz("\n");
10952 oputf("%12.12s: ", "rowseparator");
10953 output_c_string(p->rowSeparator);
10954 oputz("\n");
10955 switch( p->statsOn ){
10956 case 0: zOut = "off"; break;
10957 default: zOut = "on"; break;
10958 case 2: zOut = "stmt"; break;
10959 case 3: zOut = "vmstep"; break;
10961 oputf("%12.12s: %s\n","stats", zOut);
10962 oputf("%12.12s: ", "width");
10963 for (i=0;i<p->nWidth;i++) {
10964 oputf("%d ", p->colWidth[i]);
10966 oputz("\n");
10967 oputf("%12.12s: %s\n", "filename",
10968 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
10969 }else
10971 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
10972 if( nArg==2 ){
10973 if( cli_strcmp(azArg[1],"stmt")==0 ){
10974 p->statsOn = 2;
10975 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
10976 p->statsOn = 3;
10977 }else{
10978 p->statsOn = (u8)booleanValue(azArg[1]);
10980 }else if( nArg==1 ){
10981 display_stats(p->db, p, 0);
10982 }else{
10983 eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
10984 rc = 1;
10986 }else
10988 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
10989 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
10990 || cli_strncmp(azArg[0], "indexes", n)==0) )
10992 sqlite3_stmt *pStmt;
10993 char **azResult;
10994 int nRow, nAlloc;
10995 int ii;
10996 ShellText s;
10997 initText(&s);
10998 open_db(p, 0);
10999 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
11000 if( rc ){
11001 sqlite3_finalize(pStmt);
11002 return shellDatabaseError(p->db);
11005 if( nArg>2 && c=='i' ){
11006 /* It is an historical accident that the .indexes command shows an error
11007 ** when called with the wrong number of arguments whereas the .tables
11008 ** command does not. */
11009 eputz("Usage: .indexes ?LIKE-PATTERN?\n");
11010 rc = 1;
11011 sqlite3_finalize(pStmt);
11012 goto meta_command_exit;
11014 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
11015 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
11016 if( zDbName==0 ) continue;
11017 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
11018 if( sqlite3_stricmp(zDbName, "main")==0 ){
11019 appendText(&s, "SELECT name FROM ", 0);
11020 }else{
11021 appendText(&s, "SELECT ", 0);
11022 appendText(&s, zDbName, '\'');
11023 appendText(&s, "||'.'||name FROM ", 0);
11025 appendText(&s, zDbName, '"');
11026 appendText(&s, ".sqlite_schema ", 0);
11027 if( c=='t' ){
11028 appendText(&s," WHERE type IN ('table','view')"
11029 " AND name NOT LIKE 'sqlite_%'"
11030 " AND name LIKE ?1", 0);
11031 }else{
11032 appendText(&s," WHERE type='index'"
11033 " AND tbl_name LIKE ?1", 0);
11036 rc = sqlite3_finalize(pStmt);
11037 if( rc==SQLITE_OK ){
11038 appendText(&s, " ORDER BY 1", 0);
11039 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
11041 freeText(&s);
11042 if( rc ) return shellDatabaseError(p->db);
11044 /* Run the SQL statement prepared by the above block. Store the results
11045 ** as an array of nul-terminated strings in azResult[]. */
11046 nRow = nAlloc = 0;
11047 azResult = 0;
11048 if( nArg>1 ){
11049 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
11050 }else{
11051 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
11053 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11054 if( nRow>=nAlloc ){
11055 char **azNew;
11056 int n2 = nAlloc*2 + 10;
11057 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
11058 shell_check_oom(azNew);
11059 nAlloc = n2;
11060 azResult = azNew;
11062 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
11063 shell_check_oom(azResult[nRow]);
11064 nRow++;
11066 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
11067 rc = shellDatabaseError(p->db);
11070 /* Pretty-print the contents of array azResult[] to the output */
11071 if( rc==0 && nRow>0 ){
11072 int len, maxlen = 0;
11073 int i, j;
11074 int nPrintCol, nPrintRow;
11075 for(i=0; i<nRow; i++){
11076 len = strlen30(azResult[i]);
11077 if( len>maxlen ) maxlen = len;
11079 nPrintCol = 80/(maxlen+2);
11080 if( nPrintCol<1 ) nPrintCol = 1;
11081 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
11082 for(i=0; i<nPrintRow; i++){
11083 for(j=i; j<nRow; j+=nPrintRow){
11084 char *zSp = j<nPrintRow ? "" : " ";
11085 oputf("%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
11087 oputz("\n");
11091 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
11092 sqlite3_free(azResult);
11093 }else
11095 #ifndef SQLITE_SHELL_FIDDLE
11096 /* Begin redirecting output to the file "testcase-out.txt" */
11097 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
11098 output_reset(p);
11099 p->out = output_file_open("testcase-out.txt", 0);
11100 if( p->out==0 ){
11101 eputz("Error: cannot open 'testcase-out.txt'\n");
11103 if( nArg>=2 ){
11104 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
11105 }else{
11106 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
11108 }else
11109 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
11111 #ifndef SQLITE_UNTESTABLE
11112 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
11113 static const struct {
11114 const char *zCtrlName; /* Name of a test-control option */
11115 int ctrlCode; /* Integer code for that option */
11116 int unSafe; /* Not valid unless --unsafe-testing */
11117 const char *zUsage; /* Usage notes */
11118 } aCtrl[] = {
11119 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
11120 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
11121 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
11122 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
11123 {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" },
11124 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" },
11125 {"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..." },
11126 {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN" },
11127 {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
11128 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" },
11129 {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" },
11130 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" },
11131 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" },
11132 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."},
11133 #ifdef YYCOVERAGE
11134 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" },
11135 #endif
11136 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET " },
11137 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" },
11138 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
11139 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
11140 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
11141 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
11142 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
11143 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE,0,"?BOOLEAN|\"default\"?"},
11145 int testctrl = -1;
11146 int iCtrl = -1;
11147 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
11148 int isOk = 0;
11149 int i, n2;
11150 const char *zCmd = 0;
11152 open_db(p, 0);
11153 zCmd = nArg>=2 ? azArg[1] : "help";
11155 /* The argument can optionally begin with "-" or "--" */
11156 if( zCmd[0]=='-' && zCmd[1] ){
11157 zCmd++;
11158 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
11161 /* --help lists all test-controls */
11162 if( cli_strcmp(zCmd,"help")==0 ){
11163 oputz("Available test-controls:\n");
11164 for(i=0; i<ArraySize(aCtrl); i++){
11165 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
11166 oputf(" .testctrl %s %s\n",
11167 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
11169 rc = 1;
11170 goto meta_command_exit;
11173 /* convert testctrl text option to value. allow any unique prefix
11174 ** of the option name, or a numerical value. */
11175 n2 = strlen30(zCmd);
11176 for(i=0; i<ArraySize(aCtrl); i++){
11177 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
11178 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11179 if( testctrl<0 ){
11180 testctrl = aCtrl[i].ctrlCode;
11181 iCtrl = i;
11182 }else{
11183 eputf("Error: ambiguous test-control: \"%s\"\n"
11184 "Use \".testctrl --help\" for help\n", zCmd);
11185 rc = 1;
11186 goto meta_command_exit;
11190 if( testctrl<0 ){
11191 eputf("Error: unknown test-control: %s\n"
11192 "Use \".testctrl --help\" for help\n", zCmd);
11193 }else{
11194 switch(testctrl){
11196 /* Special processing for .testctrl opt MASK ...
11197 ** Each MASK argument can be one of:
11199 ** +LABEL Enable the named optimization
11201 ** -LABEL Disable the named optimization
11203 ** INTEGER Mask of optimizations to disable
11205 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
11206 static const struct {
11207 unsigned int mask; /* Mask for this optimization */
11208 unsigned int bDsply; /* Display this on output */
11209 const char *zLabel; /* Name of optimization */
11210 } aLabel[] = {
11211 { 0x00000001, 1, "QueryFlattener" },
11212 { 0x00000001, 0, "Flatten" },
11213 { 0x00000002, 1, "WindowFunc" },
11214 { 0x00000004, 1, "GroupByOrder" },
11215 { 0x00000008, 1, "FactorOutConst" },
11216 { 0x00000010, 1, "DistinctOpt" },
11217 { 0x00000020, 1, "CoverIdxScan" },
11218 { 0x00000040, 1, "OrderByIdxJoin" },
11219 { 0x00000080, 1, "Transitive" },
11220 { 0x00000100, 1, "OmitNoopJoin" },
11221 { 0x00000200, 1, "CountOfView" },
11222 { 0x00000400, 1, "CurosrHints" },
11223 { 0x00000800, 1, "Stat4" },
11224 { 0x00001000, 1, "PushDown" },
11225 { 0x00002000, 1, "SimplifyJoin" },
11226 { 0x00004000, 1, "SkipScan" },
11227 { 0x00008000, 1, "PropagateConst" },
11228 { 0x00010000, 1, "MinMaxOpt" },
11229 { 0x00020000, 1, "SeekScan" },
11230 { 0x00040000, 1, "OmitOrderBy" },
11231 { 0x00080000, 1, "BloomFilter" },
11232 { 0x00100000, 1, "BloomPulldown" },
11233 { 0x00200000, 1, "BalancedMerge" },
11234 { 0x00400000, 1, "ReleaseReg" },
11235 { 0x00800000, 1, "FlttnUnionAll" },
11236 { 0x01000000, 1, "IndexedEXpr" },
11237 { 0x02000000, 1, "Coroutines" },
11238 { 0x04000000, 1, "NullUnusedCols" },
11239 { 0x08000000, 1, "OnePass" },
11240 { 0x10000000, 1, "OrderBySubq" },
11241 { 0xffffffff, 0, "All" },
11243 unsigned int curOpt;
11244 unsigned int newOpt;
11245 int ii;
11246 sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
11247 newOpt = curOpt;
11248 for(ii=2; ii<nArg; ii++){
11249 const char *z = azArg[ii];
11250 int useLabel = 0;
11251 const char *zLabel = 0;
11252 if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){
11253 useLabel = z[0];
11254 zLabel = &z[1];
11255 }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){
11256 useLabel = '+';
11257 zLabel = z;
11258 }else{
11259 newOpt = (unsigned int)strtol(z,0,0);
11261 if( useLabel ){
11262 int jj;
11263 for(jj=0; jj<ArraySize(aLabel); jj++){
11264 if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
11266 if( jj>=ArraySize(aLabel) ){
11267 eputf("Error: no such optimization: \"%s\"\n", zLabel);
11268 eputz("Should be one of:");
11269 for(jj=0; jj<ArraySize(aLabel); jj++){
11270 eputf(" %s", aLabel[jj].zLabel);
11272 eputz("\n");
11273 rc = 1;
11274 goto meta_command_exit;
11276 if( useLabel=='+' ){
11277 newOpt &= ~aLabel[jj].mask;
11278 }else{
11279 newOpt |= aLabel[jj].mask;
11283 if( curOpt!=newOpt ){
11284 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
11285 }else if( nArg<3 ){
11286 curOpt = ~newOpt;
11288 if( newOpt==0 ){
11289 oputz("+All\n");
11290 }else if( newOpt==0xffffffff ){
11291 oputz("-All\n");
11292 }else{
11293 int jj;
11294 for(jj=0; jj<ArraySize(aLabel); jj++){
11295 unsigned int m = aLabel[jj].mask;
11296 if( !aLabel[jj].bDsply ) continue;
11297 if( (curOpt&m)!=(newOpt&m) ){
11298 oputf("%c%s\n", (newOpt & m)==0 ? '+' : '-',
11299 aLabel[jj].zLabel);
11303 rc2 = isOk = 3;
11304 break;
11307 /* sqlite3_test_control(int, db, int) */
11308 case SQLITE_TESTCTRL_FK_NO_ACTION:
11309 if( nArg==3 ){
11310 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11311 rc2 = sqlite3_test_control(testctrl, p->db, opt);
11312 isOk = 3;
11314 break;
11316 /* sqlite3_test_control(int) */
11317 case SQLITE_TESTCTRL_PRNG_SAVE:
11318 case SQLITE_TESTCTRL_PRNG_RESTORE:
11319 case SQLITE_TESTCTRL_BYTEORDER:
11320 if( nArg==2 ){
11321 rc2 = sqlite3_test_control(testctrl);
11322 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11324 break;
11326 /* sqlite3_test_control(int, uint) */
11327 case SQLITE_TESTCTRL_PENDING_BYTE:
11328 if( nArg==3 ){
11329 unsigned int opt = (unsigned int)integerValue(azArg[2]);
11330 rc2 = sqlite3_test_control(testctrl, opt);
11331 isOk = 3;
11333 break;
11335 /* sqlite3_test_control(int, int, sqlite3*) */
11336 case SQLITE_TESTCTRL_PRNG_SEED:
11337 if( nArg==3 || nArg==4 ){
11338 int ii = (int)integerValue(azArg[2]);
11339 sqlite3 *db;
11340 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
11341 sqlite3_randomness(sizeof(ii),&ii);
11342 sputf(stdout, "-- random seed: %d\n", ii);
11344 if( nArg==3 ){
11345 db = 0;
11346 }else{
11347 db = p->db;
11348 /* Make sure the schema has been loaded */
11349 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11351 rc2 = sqlite3_test_control(testctrl, ii, db);
11352 isOk = 3;
11354 break;
11356 /* sqlite3_test_control(int, int) */
11357 case SQLITE_TESTCTRL_ASSERT:
11358 case SQLITE_TESTCTRL_ALWAYS:
11359 if( nArg==3 ){
11360 int opt = booleanValue(azArg[2]);
11361 rc2 = sqlite3_test_control(testctrl, opt);
11362 isOk = 1;
11364 break;
11366 /* sqlite3_test_control(int, int) */
11367 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11368 case SQLITE_TESTCTRL_NEVER_CORRUPT:
11369 if( nArg==3 ){
11370 int opt = booleanValue(azArg[2]);
11371 rc2 = sqlite3_test_control(testctrl, opt);
11372 isOk = 3;
11374 break;
11376 /* sqlite3_test_control(int, int) */
11377 case SQLITE_TESTCTRL_USELONGDOUBLE: {
11378 int opt = -1;
11379 if( nArg==3 ){
11380 if( cli_strcmp(azArg[2],"default")==0 ){
11381 opt = 2;
11382 }else{
11383 opt = booleanValue(azArg[2]);
11386 rc2 = sqlite3_test_control(testctrl, opt);
11387 isOk = 1;
11388 break;
11391 /* sqlite3_test_control(sqlite3*) */
11392 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11393 rc2 = sqlite3_test_control(testctrl, p->db);
11394 isOk = 3;
11395 break;
11397 case SQLITE_TESTCTRL_IMPOSTER:
11398 if( nArg==5 ){
11399 rc2 = sqlite3_test_control(testctrl, p->db,
11400 azArg[2],
11401 integerValue(azArg[3]),
11402 integerValue(azArg[4]));
11403 isOk = 3;
11405 break;
11407 case SQLITE_TESTCTRL_SEEK_COUNT: {
11408 u64 x = 0;
11409 rc2 = sqlite3_test_control(testctrl, p->db, &x);
11410 oputf("%llu\n", x);
11411 isOk = 3;
11412 break;
11415 #ifdef YYCOVERAGE
11416 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11417 if( nArg==2 ){
11418 sqlite3_test_control(testctrl, p->out);
11419 isOk = 3;
11421 break;
11423 #endif
11424 #ifdef SQLITE_DEBUG
11425 case SQLITE_TESTCTRL_TUNE: {
11426 if( nArg==4 ){
11427 int id = (int)integerValue(azArg[2]);
11428 int val = (int)integerValue(azArg[3]);
11429 sqlite3_test_control(testctrl, id, &val);
11430 isOk = 3;
11431 }else if( nArg==3 ){
11432 int id = (int)integerValue(azArg[2]);
11433 sqlite3_test_control(testctrl, -id, &rc2);
11434 isOk = 1;
11435 }else if( nArg==2 ){
11436 int id = 1;
11437 while(1){
11438 int val = 0;
11439 rc2 = sqlite3_test_control(testctrl, -id, &val);
11440 if( rc2!=SQLITE_OK ) break;
11441 if( id>1 ) oputz(" ");
11442 oputf("%d: %d", id, val);
11443 id++;
11445 if( id>1 ) oputz("\n");
11446 isOk = 3;
11448 break;
11450 #endif
11451 case SQLITE_TESTCTRL_SORTER_MMAP:
11452 if( nArg==3 ){
11453 int opt = (unsigned int)integerValue(azArg[2]);
11454 rc2 = sqlite3_test_control(testctrl, p->db, opt);
11455 isOk = 3;
11457 break;
11458 case SQLITE_TESTCTRL_JSON_SELFCHECK:
11459 if( nArg==2 ){
11460 rc2 = -1;
11461 isOk = 1;
11462 }else{
11463 rc2 = booleanValue(azArg[2]);
11464 isOk = 3;
11466 sqlite3_test_control(testctrl, &rc2);
11467 break;
11468 case SQLITE_TESTCTRL_FAULT_INSTALL: {
11469 int kk;
11470 int bShowHelp = nArg<=2;
11471 isOk = 3;
11472 for(kk=2; kk<nArg; kk++){
11473 const char *z = azArg[kk];
11474 if( z[0]=='-' && z[1]=='-' ) z++;
11475 if( cli_strcmp(z,"off")==0 ){
11476 sqlite3_test_control(testctrl, 0);
11477 }else if( cli_strcmp(z,"on")==0 ){
11478 faultsim_state.iCnt = faultsim_state.nSkip;
11479 if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1;
11480 faultsim_state.nHit = 0;
11481 sqlite3_test_control(testctrl, faultsim_callback);
11482 }else if( cli_strcmp(z,"reset")==0 ){
11483 faultsim_state.iCnt = faultsim_state.nSkip;
11484 faultsim_state.nHit = 0;
11485 sqlite3_test_control(testctrl, faultsim_callback);
11486 }else if( cli_strcmp(z,"status")==0 ){
11487 oputf("faultsim.iId: %d\n", faultsim_state.iId);
11488 oputf("faultsim.iErr: %d\n", faultsim_state.iErr);
11489 oputf("faultsim.iCnt: %d\n", faultsim_state.iCnt);
11490 oputf("faultsim.nHit: %d\n", faultsim_state.nHit);
11491 oputf("faultsim.iInterval: %d\n", faultsim_state.iInterval);
11492 oputf("faultsim.eVerbose: %d\n", faultsim_state.eVerbose);
11493 oputf("faultsim.nRepeat: %d\n", faultsim_state.nRepeat);
11494 oputf("faultsim.nSkip: %d\n", faultsim_state.nSkip);
11495 }else if( cli_strcmp(z,"-v")==0 ){
11496 if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
11497 }else if( cli_strcmp(z,"-q")==0 ){
11498 if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
11499 }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
11500 faultsim_state.iId = atoi(azArg[++kk]);
11501 }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){
11502 faultsim_state.iErr = atoi(azArg[++kk]);
11503 }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){
11504 faultsim_state.iInterval = atoi(azArg[++kk]);
11505 }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){
11506 faultsim_state.nRepeat = atoi(azArg[++kk]);
11507 }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
11508 faultsim_state.nSkip = atoi(azArg[++kk]);
11509 }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
11510 bShowHelp = 1;
11511 }else{
11512 eputf("Unrecognized fault_install argument: \"%s\"\n",
11513 azArg[kk]);
11514 rc = 1;
11515 bShowHelp = 1;
11516 break;
11519 if( bShowHelp ){
11520 oputz(
11521 "Usage: .testctrl fault_install ARGS\n"
11522 "Possible arguments:\n"
11523 " off Disable faultsim\n"
11524 " on Activate faultsim\n"
11525 " reset Reset the trigger counter\n"
11526 " status Show current status\n"
11527 " -v Increase verbosity\n"
11528 " -q Decrease verbosity\n"
11529 " --errcode N When triggered, return N as error code\n"
11530 " --id ID Trigger only for the ID specified\n"
11531 " --interval N Trigger only after every N-th call\n"
11532 " --repeat N Turn off after N hits. 0 means never\n"
11533 " --skip N Skip the first N encounters\n"
11536 break;
11540 if( isOk==0 && iCtrl>=0 ){
11541 oputf("Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11542 rc = 1;
11543 }else if( isOk==1 ){
11544 oputf("%d\n", rc2);
11545 }else if( isOk==2 ){
11546 oputf("0x%08x\n", rc2);
11548 }else
11549 #endif /* !defined(SQLITE_UNTESTABLE) */
11551 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
11552 open_db(p, 0);
11553 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11554 }else
11556 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
11557 if( nArg==2 ){
11558 enableTimer = booleanValue(azArg[1]);
11559 if( enableTimer && !HAS_TIMER ){
11560 eputz("Error: timer not available on this system.\n");
11561 enableTimer = 0;
11563 }else{
11564 eputz("Usage: .timer on|off\n");
11565 rc = 1;
11567 }else
11569 #ifndef SQLITE_OMIT_TRACE
11570 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
11571 int mType = 0;
11572 int jj;
11573 open_db(p, 0);
11574 for(jj=1; jj<nArg; jj++){
11575 const char *z = azArg[jj];
11576 if( z[0]=='-' ){
11577 if( optionMatch(z, "expanded") ){
11578 p->eTraceType = SHELL_TRACE_EXPANDED;
11580 #ifdef SQLITE_ENABLE_NORMALIZE
11581 else if( optionMatch(z, "normalized") ){
11582 p->eTraceType = SHELL_TRACE_NORMALIZED;
11584 #endif
11585 else if( optionMatch(z, "plain") ){
11586 p->eTraceType = SHELL_TRACE_PLAIN;
11588 else if( optionMatch(z, "profile") ){
11589 mType |= SQLITE_TRACE_PROFILE;
11591 else if( optionMatch(z, "row") ){
11592 mType |= SQLITE_TRACE_ROW;
11594 else if( optionMatch(z, "stmt") ){
11595 mType |= SQLITE_TRACE_STMT;
11597 else if( optionMatch(z, "close") ){
11598 mType |= SQLITE_TRACE_CLOSE;
11600 else {
11601 eputf("Unknown option \"%s\" on \".trace\"\n", z);
11602 rc = 1;
11603 goto meta_command_exit;
11605 }else{
11606 output_file_close(p->traceOut);
11607 p->traceOut = output_file_open(z, 0);
11610 if( p->traceOut==0 ){
11611 sqlite3_trace_v2(p->db, 0, 0, 0);
11612 }else{
11613 if( mType==0 ) mType = SQLITE_TRACE_STMT;
11614 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11616 }else
11617 #endif /* !defined(SQLITE_OMIT_TRACE) */
11619 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11620 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
11621 int ii;
11622 int lenOpt;
11623 char *zOpt;
11624 if( nArg<2 ){
11625 eputz("Usage: .unmodule [--allexcept] NAME ...\n");
11626 rc = 1;
11627 goto meta_command_exit;
11629 open_db(p, 0);
11630 zOpt = azArg[1];
11631 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11632 lenOpt = (int)strlen(zOpt);
11633 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11634 assert( azArg[nArg]==0 );
11635 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11636 }else{
11637 for(ii=1; ii<nArg; ii++){
11638 sqlite3_create_module(p->db, azArg[ii], 0, 0);
11641 }else
11642 #endif
11644 #if SQLITE_USER_AUTHENTICATION
11645 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
11646 if( nArg<2 ){
11647 eputz("Usage: .user SUBCOMMAND ...\n");
11648 rc = 1;
11649 goto meta_command_exit;
11651 open_db(p, 0);
11652 if( cli_strcmp(azArg[1],"login")==0 ){
11653 if( nArg!=4 ){
11654 eputz("Usage: .user login USER PASSWORD\n");
11655 rc = 1;
11656 goto meta_command_exit;
11658 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11659 strlen30(azArg[3]));
11660 if( rc ){
11661 eputf("Authentication failed for user %s\n", azArg[2]);
11662 rc = 1;
11664 }else if( cli_strcmp(azArg[1],"add")==0 ){
11665 if( nArg!=5 ){
11666 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
11667 rc = 1;
11668 goto meta_command_exit;
11670 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11671 booleanValue(azArg[4]));
11672 if( rc ){
11673 eputf("User-Add failed: %d\n", rc);
11674 rc = 1;
11676 }else if( cli_strcmp(azArg[1],"edit")==0 ){
11677 if( nArg!=5 ){
11678 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
11679 rc = 1;
11680 goto meta_command_exit;
11682 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11683 booleanValue(azArg[4]));
11684 if( rc ){
11685 eputf("User-Edit failed: %d\n", rc);
11686 rc = 1;
11688 }else if( cli_strcmp(azArg[1],"delete")==0 ){
11689 if( nArg!=3 ){
11690 eputz("Usage: .user delete USER\n");
11691 rc = 1;
11692 goto meta_command_exit;
11694 rc = sqlite3_user_delete(p->db, azArg[2]);
11695 if( rc ){
11696 eputf("User-Delete failed: %d\n", rc);
11697 rc = 1;
11699 }else{
11700 eputz("Usage: .user login|add|edit|delete ...\n");
11701 rc = 1;
11702 goto meta_command_exit;
11704 }else
11705 #endif /* SQLITE_USER_AUTHENTICATION */
11707 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
11708 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
11709 oputf("SQLite %s %s\n" /*extra-version-info*/,
11710 sqlite3_libversion(), sqlite3_sourceid());
11711 #if SQLITE_HAVE_ZLIB
11712 oputf("zlib version %s\n", zlibVersion());
11713 #endif
11714 #define CTIMEOPT_VAL_(opt) #opt
11715 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11716 #if defined(__clang__) && defined(__clang_major__)
11717 oputf("clang-" CTIMEOPT_VAL(__clang_major__) "."
11718 CTIMEOPT_VAL(__clang_minor__) "."
11719 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
11720 #elif defined(_MSC_VER)
11721 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
11722 #elif defined(__GNUC__) && defined(__VERSION__)
11723 oputf("gcc-" __VERSION__ " (%s)\n", zPtrSz);
11724 #endif
11725 }else
11727 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
11728 const char *zDbName = nArg==2 ? azArg[1] : "main";
11729 sqlite3_vfs *pVfs = 0;
11730 if( p->db ){
11731 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11732 if( pVfs ){
11733 oputf("vfs.zName = \"%s\"\n", pVfs->zName);
11734 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
11735 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
11736 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
11739 }else
11741 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
11742 sqlite3_vfs *pVfs;
11743 sqlite3_vfs *pCurrent = 0;
11744 if( p->db ){
11745 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11747 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11748 oputf("vfs.zName = \"%s\"%s\n", pVfs->zName,
11749 pVfs==pCurrent ? " <--- CURRENT" : "");
11750 oputf("vfs.iVersion = %d\n", pVfs->iVersion);
11751 oputf("vfs.szOsFile = %d\n", pVfs->szOsFile);
11752 oputf("vfs.mxPathname = %d\n", pVfs->mxPathname);
11753 if( pVfs->pNext ){
11754 oputz("-----------------------------------\n");
11757 }else
11759 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
11760 const char *zDbName = nArg==2 ? azArg[1] : "main";
11761 char *zVfsName = 0;
11762 if( p->db ){
11763 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11764 if( zVfsName ){
11765 oputf("%s\n", zVfsName);
11766 sqlite3_free(zVfsName);
11769 }else
11771 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
11772 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11773 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11774 }else
11776 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
11777 int j;
11778 assert( nArg<=ArraySize(azArg) );
11779 p->nWidth = nArg-1;
11780 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11781 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11782 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11783 for(j=1; j<nArg; j++){
11784 p->colWidth[j-1] = (int)integerValue(azArg[j]);
11786 }else
11789 eputf("Error: unknown command or invalid arguments: "
11790 " \"%s\". Enter \".help\" for help\n", azArg[0]);
11791 rc = 1;
11794 meta_command_exit:
11795 if( p->outCount ){
11796 p->outCount--;
11797 if( p->outCount==0 ) output_reset(p);
11799 p->bSafeMode = p->bSafeModePersist;
11800 return rc;
11803 /* Line scan result and intermediate states (supporting scan resumption)
11805 #ifndef CHAR_BIT
11806 # define CHAR_BIT 8
11807 #endif
11808 typedef enum {
11809 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11810 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11811 QSS_Start = 0
11812 } QuickScanState;
11813 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11814 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11815 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11816 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11817 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11820 ** Scan line for classification to guide shell's handling.
11821 ** The scan is resumable for subsequent lines when prior
11822 ** return values are passed as the 2nd argument.
11824 static QuickScanState quickscan(char *zLine, QuickScanState qss,
11825 SCAN_TRACKER_REFTYPE pst){
11826 char cin;
11827 char cWait = (char)qss; /* intentional narrowing loss */
11828 if( cWait==0 ){
11829 PlainScan:
11830 assert( cWait==0 );
11831 while( (cin = *zLine++)!=0 ){
11832 if( IsSpace(cin) )
11833 continue;
11834 switch (cin){
11835 case '-':
11836 if( *zLine!='-' )
11837 break;
11838 while((cin = *++zLine)!=0 )
11839 if( cin=='\n')
11840 goto PlainScan;
11841 return qss;
11842 case ';':
11843 qss |= QSS_EndingSemi;
11844 continue;
11845 case '/':
11846 if( *zLine=='*' ){
11847 ++zLine;
11848 cWait = '*';
11849 CONTINUE_PROMPT_AWAITS(pst, "/*");
11850 qss = QSS_SETV(qss, cWait);
11851 goto TermScan;
11853 break;
11854 case '[':
11855 cin = ']';
11856 deliberate_fall_through;
11857 case '`': case '\'': case '"':
11858 cWait = cin;
11859 qss = QSS_HasDark | cWait;
11860 CONTINUE_PROMPT_AWAITC(pst, cin);
11861 goto TermScan;
11862 case '(':
11863 CONTINUE_PAREN_INCR(pst, 1);
11864 break;
11865 case ')':
11866 CONTINUE_PAREN_INCR(pst, -1);
11867 break;
11868 default:
11869 break;
11871 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11873 }else{
11874 TermScan:
11875 while( (cin = *zLine++)!=0 ){
11876 if( cin==cWait ){
11877 switch( cWait ){
11878 case '*':
11879 if( *zLine != '/' )
11880 continue;
11881 ++zLine;
11882 cWait = 0;
11883 CONTINUE_PROMPT_AWAITC(pst, 0);
11884 qss = QSS_SETV(qss, 0);
11885 goto PlainScan;
11886 case '`': case '\'': case '"':
11887 if(*zLine==cWait){
11888 /* Swallow doubled end-delimiter.*/
11889 ++zLine;
11890 continue;
11892 deliberate_fall_through;
11893 case ']':
11894 cWait = 0;
11895 CONTINUE_PROMPT_AWAITC(pst, 0);
11896 qss = QSS_SETV(qss, 0);
11897 goto PlainScan;
11898 default: assert(0);
11903 return qss;
11907 ** Return TRUE if the line typed in is an SQL command terminator other
11908 ** than a semi-colon. The SQL Server style "go" command is understood
11909 ** as is the Oracle "/".
11911 static int line_is_command_terminator(char *zLine){
11912 while( IsSpace(zLine[0]) ){ zLine++; };
11913 if( zLine[0]=='/' )
11914 zLine += 1; /* Oracle */
11915 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
11916 zLine += 2; /* SQL Server */
11917 else
11918 return 0;
11919 return quickscan(zLine, QSS_Start, 0)==QSS_Start;
11923 ** The CLI needs a working sqlite3_complete() to work properly. So error
11924 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
11926 #ifdef SQLITE_OMIT_COMPLETE
11927 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
11928 #endif
11931 ** Return true if zSql is a complete SQL statement. Return false if it
11932 ** ends in the middle of a string literal or C-style comment.
11934 static int line_is_complete(char *zSql, int nSql){
11935 int rc;
11936 if( zSql==0 ) return 1;
11937 zSql[nSql] = ';';
11938 zSql[nSql+1] = 0;
11939 rc = sqlite3_complete(zSql);
11940 zSql[nSql] = 0;
11941 return rc;
11945 ** This function is called after processing each line of SQL in the
11946 ** runOneSqlLine() function. Its purpose is to detect scenarios where
11947 ** defensive mode should be automatically turned off. Specifically, when
11949 ** 1. The first line of input is "PRAGMA foreign_keys=OFF;",
11950 ** 2. The second line of input is "BEGIN TRANSACTION;",
11951 ** 3. The database is empty, and
11952 ** 4. The shell is not running in --safe mode.
11954 ** The implementation uses the ShellState.eRestoreState to maintain state:
11956 ** 0: Have not seen any SQL.
11957 ** 1: Have seen "PRAGMA foreign_keys=OFF;".
11958 ** 2-6: Currently running .dump transaction. If the "2" bit is set,
11959 ** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
11960 ** 7: Nothing left to do. This function becomes a no-op.
11962 static int doAutoDetectRestore(ShellState *p, const char *zSql){
11963 int rc = SQLITE_OK;
11965 if( p->eRestoreState<7 ){
11966 switch( p->eRestoreState ){
11967 case 0: {
11968 const char *zExpect = "PRAGMA foreign_keys=OFF;";
11969 assert( strlen(zExpect)==24 );
11970 if( p->bSafeMode==0
11971 && strlen(zSql)>=24
11972 && memcmp(zSql, zExpect, 25)==0
11974 p->eRestoreState = 1;
11975 }else{
11976 p->eRestoreState = 7;
11978 break;
11981 case 1: {
11982 int bIsDump = 0;
11983 const char *zExpect = "BEGIN TRANSACTION;";
11984 assert( strlen(zExpect)==18 );
11985 if( memcmp(zSql, zExpect, 19)==0 ){
11986 /* Now check if the database is empty. */
11987 const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
11988 sqlite3_stmt *pStmt = 0;
11990 bIsDump = 1;
11991 shellPrepare(p->db, &rc, zQuery, &pStmt);
11992 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11993 bIsDump = 0;
11995 shellFinalize(&rc, pStmt);
11997 if( bIsDump && rc==SQLITE_OK ){
11998 int bDefense = 0;
11999 int bDqsDdl = 0;
12000 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
12001 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
12002 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
12003 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
12004 p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
12005 }else{
12006 p->eRestoreState = 7;
12008 break;
12011 default: {
12012 if( sqlite3_get_autocommit(p->db) ){
12013 if( (p->eRestoreState & 2) ){
12014 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
12016 if( (p->eRestoreState & 4) ){
12017 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
12019 p->eRestoreState = 7;
12021 break;
12026 return rc;
12030 ** Run a single line of SQL. Return the number of errors.
12032 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
12033 int rc;
12034 char *zErrMsg = 0;
12036 open_db(p, 0);
12037 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
12038 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12039 BEGIN_TIMER;
12040 rc = shell_exec(p, zSql, &zErrMsg);
12041 END_TIMER;
12042 if( rc || zErrMsg ){
12043 char zPrefix[100];
12044 const char *zErrorTail;
12045 const char *zErrorType;
12046 if( zErrMsg==0 ){
12047 zErrorType = "Error";
12048 zErrorTail = sqlite3_errmsg(p->db);
12049 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
12050 zErrorType = "Parse error";
12051 zErrorTail = &zErrMsg[12];
12052 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
12053 zErrorType = "Runtime error";
12054 zErrorTail = &zErrMsg[10];
12055 }else{
12056 zErrorType = "Error";
12057 zErrorTail = zErrMsg;
12059 if( in!=0 || !stdin_is_interactive ){
12060 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
12061 "%s near line %d:", zErrorType, startline);
12062 }else{
12063 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
12065 eputf("%s %s\n", zPrefix, zErrorTail);
12066 sqlite3_free(zErrMsg);
12067 zErrMsg = 0;
12068 return 1;
12069 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
12070 char zLineBuf[2000];
12071 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
12072 "changes: %lld total_changes: %lld",
12073 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
12074 oputf("%s\n", zLineBuf);
12077 if( doAutoDetectRestore(p, zSql) ) return 1;
12078 return 0;
12081 static void echo_group_input(ShellState *p, const char *zDo){
12082 if( ShellHasFlag(p, SHFLG_Echo) ) oputf("%s\n", zDo);
12085 #ifdef SQLITE_SHELL_FIDDLE
12087 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
12088 ** impl because we need the global shellState and cannot access it from that
12089 ** function without moving lots of code around (creating a larger/messier diff).
12091 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
12092 /* Parse the next line from shellState.wasm.zInput. */
12093 const char *zBegin = shellState.wasm.zPos;
12094 const char *z = zBegin;
12095 char *zLine = 0;
12096 i64 nZ = 0;
12098 UNUSED_PARAMETER(in);
12099 UNUSED_PARAMETER(isContinuation);
12100 if(!z || !*z){
12101 return 0;
12103 while(*z && isspace(*z)) ++z;
12104 zBegin = z;
12105 for(; *z && '\n'!=*z; ++nZ, ++z){}
12106 if(nZ>0 && '\r'==zBegin[nZ-1]){
12107 --nZ;
12109 shellState.wasm.zPos = z;
12110 zLine = realloc(zPrior, nZ+1);
12111 shell_check_oom(zLine);
12112 memcpy(zLine, zBegin, nZ);
12113 zLine[nZ] = 0;
12114 return zLine;
12116 #endif /* SQLITE_SHELL_FIDDLE */
12119 ** Read input from *in and process it. If *in==0 then input
12120 ** is interactive - the user is typing it it. Otherwise, input
12121 ** is coming from a file or device. A prompt is issued and history
12122 ** is saved only if input is interactive. An interrupt signal will
12123 ** cause this routine to exit immediately, unless input is interactive.
12125 ** Return the number of errors.
12127 static int process_input(ShellState *p){
12128 char *zLine = 0; /* A single input line */
12129 char *zSql = 0; /* Accumulated SQL text */
12130 i64 nLine; /* Length of current line */
12131 i64 nSql = 0; /* Bytes of zSql[] used */
12132 i64 nAlloc = 0; /* Allocated zSql[] space */
12133 int rc; /* Error code */
12134 int errCnt = 0; /* Number of errors seen */
12135 i64 startline = 0; /* Line number for start of current input */
12136 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
12138 if( p->inputNesting==MAX_INPUT_NESTING ){
12139 /* This will be more informative in a later version. */
12140 eputf("Input nesting limit (%d) reached at line %d."
12141 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
12142 return 1;
12144 ++p->inputNesting;
12145 p->lineno = 0;
12146 CONTINUE_PROMPT_RESET;
12147 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
12148 fflush(p->out);
12149 zLine = one_input_line(p->in, zLine, nSql>0);
12150 if( zLine==0 ){
12151 /* End of input */
12152 if( p->in==0 && stdin_is_interactive ) oputz("\n");
12153 break;
12155 if( seenInterrupt ){
12156 if( p->in!=0 ) break;
12157 seenInterrupt = 0;
12159 p->lineno++;
12160 if( QSS_INPLAIN(qss)
12161 && line_is_command_terminator(zLine)
12162 && line_is_complete(zSql, nSql) ){
12163 memcpy(zLine,";",2);
12165 qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
12166 if( QSS_PLAINWHITE(qss) && nSql==0 ){
12167 /* Just swallow single-line whitespace */
12168 echo_group_input(p, zLine);
12169 qss = QSS_Start;
12170 continue;
12172 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
12173 CONTINUE_PROMPT_RESET;
12174 echo_group_input(p, zLine);
12175 if( zLine[0]=='.' ){
12176 rc = do_meta_command(zLine, p);
12177 if( rc==2 ){ /* exit requested */
12178 break;
12179 }else if( rc ){
12180 errCnt++;
12183 qss = QSS_Start;
12184 continue;
12186 /* No single-line dispositions remain; accumulate line(s). */
12187 nLine = strlen(zLine);
12188 if( nSql+nLine+2>=nAlloc ){
12189 /* Grow buffer by half-again increments when big. */
12190 nAlloc = nSql+(nSql>>1)+nLine+100;
12191 zSql = realloc(zSql, nAlloc);
12192 shell_check_oom(zSql);
12194 if( nSql==0 ){
12195 i64 i;
12196 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
12197 assert( nAlloc>0 && zSql!=0 );
12198 memcpy(zSql, zLine+i, nLine+1-i);
12199 startline = p->lineno;
12200 nSql = nLine-i;
12201 }else{
12202 zSql[nSql++] = '\n';
12203 memcpy(zSql+nSql, zLine, nLine+1);
12204 nSql += nLine;
12206 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
12207 echo_group_input(p, zSql);
12208 errCnt += runOneSqlLine(p, zSql, p->in, startline);
12209 CONTINUE_PROMPT_RESET;
12210 nSql = 0;
12211 if( p->outCount ){
12212 output_reset(p);
12213 p->outCount = 0;
12214 }else{
12215 clearTempFile(p);
12217 p->bSafeMode = p->bSafeModePersist;
12218 qss = QSS_Start;
12219 }else if( nSql && QSS_PLAINWHITE(qss) ){
12220 echo_group_input(p, zSql);
12221 nSql = 0;
12222 qss = QSS_Start;
12225 if( nSql ){
12226 /* This may be incomplete. Let the SQL parser deal with that. */
12227 echo_group_input(p, zSql);
12228 errCnt += runOneSqlLine(p, zSql, p->in, startline);
12229 CONTINUE_PROMPT_RESET;
12231 free(zSql);
12232 free(zLine);
12233 --p->inputNesting;
12234 return errCnt>0;
12238 ** Return a pathname which is the user's home directory. A
12239 ** 0 return indicates an error of some kind.
12241 static char *find_home_dir(int clearFlag){
12242 static char *home_dir = NULL;
12243 if( clearFlag ){
12244 free(home_dir);
12245 home_dir = 0;
12246 return 0;
12248 if( home_dir ) return home_dir;
12250 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
12251 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
12253 struct passwd *pwent;
12254 uid_t uid = getuid();
12255 if( (pwent=getpwuid(uid)) != NULL) {
12256 home_dir = pwent->pw_dir;
12259 #endif
12261 #if defined(_WIN32_WCE)
12262 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
12264 home_dir = "/";
12265 #else
12267 #if defined(_WIN32) || defined(WIN32)
12268 if (!home_dir) {
12269 home_dir = getenv("USERPROFILE");
12271 #endif
12273 if (!home_dir) {
12274 home_dir = getenv("HOME");
12277 #if defined(_WIN32) || defined(WIN32)
12278 if (!home_dir) {
12279 char *zDrive, *zPath;
12280 int n;
12281 zDrive = getenv("HOMEDRIVE");
12282 zPath = getenv("HOMEPATH");
12283 if( zDrive && zPath ){
12284 n = strlen30(zDrive) + strlen30(zPath) + 1;
12285 home_dir = malloc( n );
12286 if( home_dir==0 ) return 0;
12287 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
12288 return home_dir;
12290 home_dir = "c:\\";
12292 #endif
12294 #endif /* !_WIN32_WCE */
12296 if( home_dir ){
12297 i64 n = strlen(home_dir) + 1;
12298 char *z = malloc( n );
12299 if( z ) memcpy(z, home_dir, n);
12300 home_dir = z;
12303 return home_dir;
12307 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
12308 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
12309 ** the path to it. If there is no $(XDG_CONFIG_HOME) then
12310 ** look for $(HOME)/.config/sqlite3/sqliterc and if found
12311 ** return that. If none of these are found, return 0.
12313 ** The string returned is obtained from sqlite3_malloc() and
12314 ** should be freed by the caller.
12316 static char *find_xdg_config(void){
12317 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
12318 || defined(__RTP__) || defined(_WRS_KERNEL)
12319 return 0;
12320 #else
12321 char *zConfig = 0;
12322 const char *zXdgHome;
12324 zXdgHome = getenv("XDG_CONFIG_HOME");
12325 if( zXdgHome==0 ){
12326 const char *zHome = getenv("HOME");
12327 if( zHome==0 ) return 0;
12328 zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome);
12329 }else{
12330 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
12332 shell_check_oom(zConfig);
12333 if( access(zConfig,0)!=0 ){
12334 sqlite3_free(zConfig);
12335 zConfig = 0;
12337 return zConfig;
12338 #endif
12342 ** Read input from the file given by sqliterc_override. Or if that
12343 ** parameter is NULL, take input from the first of find_xdg_config()
12344 ** or ~/.sqliterc which is found.
12346 ** Returns the number of errors.
12348 static void process_sqliterc(
12349 ShellState *p, /* Configuration data */
12350 const char *sqliterc_override /* Name of config file. NULL to use default */
12352 char *home_dir = NULL;
12353 const char *sqliterc = sqliterc_override;
12354 char *zBuf = 0;
12355 FILE *inSaved = p->in;
12356 int savedLineno = p->lineno;
12358 if( sqliterc == NULL ){
12359 sqliterc = zBuf = find_xdg_config();
12361 if( sqliterc == NULL ){
12362 home_dir = find_home_dir(0);
12363 if( home_dir==0 ){
12364 eputz("-- warning: cannot find home directory;"
12365 " cannot read ~/.sqliterc\n");
12366 return;
12368 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
12369 shell_check_oom(zBuf);
12370 sqliterc = zBuf;
12372 p->in = fopen(sqliterc,"rb");
12373 if( p->in ){
12374 if( stdin_is_interactive ){
12375 eputf("-- Loading resources from %s\n", sqliterc);
12377 if( process_input(p) && bail_on_error ) exit(1);
12378 fclose(p->in);
12379 }else if( sqliterc_override!=0 ){
12380 eputf("cannot open: \"%s\"\n", sqliterc);
12381 if( bail_on_error ) exit(1);
12383 p->in = inSaved;
12384 p->lineno = savedLineno;
12385 sqlite3_free(zBuf);
12389 ** Show available command line options
12391 static const char zOptions[] =
12392 " -- treat no subsequent arguments as options\n"
12393 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12394 " -A ARGS... run \".archive ARGS\" and exit\n"
12395 #endif
12396 " -append append the database to the end of the file\n"
12397 " -ascii set output mode to 'ascii'\n"
12398 " -bail stop after hitting an error\n"
12399 " -batch force batch I/O\n"
12400 " -box set output mode to 'box'\n"
12401 " -column set output mode to 'column'\n"
12402 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
12403 " -csv set output mode to 'csv'\n"
12404 #if !defined(SQLITE_OMIT_DESERIALIZE)
12405 " -deserialize open the database using sqlite3_deserialize()\n"
12406 #endif
12407 " -echo print inputs before execution\n"
12408 " -init FILENAME read/process named file\n"
12409 " -[no]header turn headers on or off\n"
12410 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12411 " -heap SIZE Size of heap for memsys3 or memsys5\n"
12412 #endif
12413 " -help show this message\n"
12414 " -html set output mode to HTML\n"
12415 " -interactive force interactive I/O\n"
12416 " -json set output mode to 'json'\n"
12417 " -line set output mode to 'line'\n"
12418 " -list set output mode to 'list'\n"
12419 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
12420 " -markdown set output mode to 'markdown'\n"
12421 #if !defined(SQLITE_OMIT_DESERIALIZE)
12422 " -maxsize N maximum size for a --deserialize database\n"
12423 #endif
12424 " -memtrace trace all memory allocations and deallocations\n"
12425 " -mmap N default mmap size set to N\n"
12426 #ifdef SQLITE_ENABLE_MULTIPLEX
12427 " -multiplex enable the multiplexor VFS\n"
12428 #endif
12429 " -newline SEP set output row separator. Default: '\\n'\n"
12430 " -nofollow refuse to open symbolic links to database files\n"
12431 " -nonce STRING set the safe-mode escape nonce\n"
12432 " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n"
12433 " -nullvalue TEXT set text string for NULL values. Default ''\n"
12434 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
12435 " -pcachetrace trace all page cache operations\n"
12436 " -quote set output mode to 'quote'\n"
12437 " -readonly open the database read-only\n"
12438 " -safe enable safe-mode\n"
12439 " -separator SEP set output column separator. Default: '|'\n"
12440 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
12441 " -sorterref SIZE sorter references threshold size\n"
12442 #endif
12443 " -stats print memory stats before each finalize\n"
12444 " -table set output mode to 'table'\n"
12445 " -tabs set output mode to 'tabs'\n"
12446 " -unsafe-testing allow unsafe commands and modes for testing\n"
12447 " -version show SQLite version\n"
12448 " -vfs NAME use NAME as the default VFS\n"
12449 " -vfstrace enable tracing of all VFS calls\n"
12450 #ifdef SQLITE_HAVE_ZLIB
12451 " -zip open the file as a ZIP Archive\n"
12452 #endif
12454 static void usage(int showDetail){
12455 eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
12456 "FILENAME is the name of an SQLite database. A new database is created\n"
12457 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
12458 if( showDetail ){
12459 eputf("OPTIONS include:\n%s", zOptions);
12460 }else{
12461 eputz("Use the -help option for additional information\n");
12463 exit(0);
12467 ** Internal check: Verify that the SQLite is uninitialized. Print a
12468 ** error message if it is initialized.
12470 static void verify_uninitialized(void){
12471 if( sqlite3_config(-1)==SQLITE_MISUSE ){
12472 sputz(stdout, "WARNING: attempt to configure SQLite after"
12473 " initialization.\n");
12478 ** Initialize the state information in data
12480 static void main_init(ShellState *data) {
12481 memset(data, 0, sizeof(*data));
12482 data->normalMode = data->cMode = data->mode = MODE_List;
12483 data->autoExplain = 1;
12484 data->pAuxDb = &data->aAuxDb[0];
12485 memcpy(data->colSeparator,SEP_Column, 2);
12486 memcpy(data->rowSeparator,SEP_Row, 2);
12487 data->showHeader = 0;
12488 data->shellFlgs = SHFLG_Lookaside;
12489 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
12490 #if !defined(SQLITE_SHELL_FIDDLE)
12491 verify_uninitialized();
12492 #endif
12493 sqlite3_config(SQLITE_CONFIG_URI, 1);
12494 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
12495 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
12496 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
12500 ** Output text to the console in a font that attracts extra attention.
12502 #if defined(_WIN32) || defined(WIN32)
12503 static void printBold(const char *zText){
12504 #if !SQLITE_OS_WINRT
12505 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
12506 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
12507 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
12508 SetConsoleTextAttribute(out,
12509 FOREGROUND_RED|FOREGROUND_INTENSITY
12511 #endif
12512 sputz(stdout, zText);
12513 #if !SQLITE_OS_WINRT
12514 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
12515 #endif
12517 #else
12518 static void printBold(const char *zText){
12519 sputf(stdout, "\033[1m%s\033[0m", zText);
12521 #endif
12524 ** Get the argument to an --option. Throw an error and die if no argument
12525 ** is available.
12527 static char *cmdline_option_value(int argc, char **argv, int i){
12528 if( i==argc ){
12529 eputf("%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
12530 exit(1);
12532 return argv[i];
12535 static void sayAbnormalExit(void){
12536 if( seenInterrupt ) eputz("Program interrupted.\n");
12539 #ifndef SQLITE_SHELL_IS_UTF8
12540 # if (defined(_WIN32) || defined(WIN32)) \
12541 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12542 # define SQLITE_SHELL_IS_UTF8 (0)
12543 # else
12544 # define SQLITE_SHELL_IS_UTF8 (1)
12545 # endif
12546 #endif
12548 #ifdef SQLITE_SHELL_FIDDLE
12549 # define main fiddle_main
12550 #endif
12552 #if SQLITE_SHELL_IS_UTF8
12553 int SQLITE_CDECL main(int argc, char **argv){
12554 #else
12555 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12556 char **argv;
12557 #endif
12558 #ifdef SQLITE_DEBUG
12559 sqlite3_int64 mem_main_enter = 0;
12560 #endif
12561 char *zErrMsg = 0;
12562 #ifdef SQLITE_SHELL_FIDDLE
12563 # define data shellState
12564 #else
12565 ShellState data;
12566 StreamsAreConsole consStreams = SAC_NoConsole;
12567 #endif
12568 const char *zInitFile = 0;
12569 int i;
12570 int rc = 0;
12571 int warnInmemoryDb = 0;
12572 int readStdin = 1;
12573 int nCmd = 0;
12574 int nOptsEnd = argc;
12575 int bEnableVfstrace = 0;
12576 char **azCmd = 0;
12577 const char *zVfs = 0; /* Value of -vfs command-line option */
12578 #if !SQLITE_SHELL_IS_UTF8
12579 char **argvToFree = 0;
12580 int argcToFree = 0;
12581 #endif
12582 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12584 #ifdef SQLITE_SHELL_FIDDLE
12585 stdin_is_interactive = 0;
12586 stdout_is_console = 1;
12587 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12588 #else
12589 consStreams = consoleClassifySetup(stdin, stdout, stderr);
12590 stdin_is_interactive = (consStreams & SAC_InConsole)!=0;
12591 stdout_is_console = (consStreams & SAC_OutConsole)!=0;
12592 atexit(consoleRestore);
12593 #endif
12594 atexit(sayAbnormalExit);
12595 #ifdef SQLITE_DEBUG
12596 mem_main_enter = sqlite3_memory_used();
12597 #endif
12598 #if !defined(_WIN32_WCE)
12599 if( getenv("SQLITE_DEBUG_BREAK") ){
12600 if( isatty(0) && isatty(2) ){
12601 eputf("attach debugger to process %d and press any key to continue.\n",
12602 GETPID());
12603 fgetc(stdin);
12604 }else{
12605 #if defined(_WIN32) || defined(WIN32)
12606 #if SQLITE_OS_WINRT
12607 __debugbreak();
12608 #else
12609 DebugBreak();
12610 #endif
12611 #elif defined(SIGTRAP)
12612 raise(SIGTRAP);
12613 #endif
12616 #endif
12617 /* Register a valid signal handler early, before much else is done. */
12618 #ifdef SIGINT
12619 signal(SIGINT, interrupt_handler);
12620 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12621 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
12622 eputz("No ^C handler.\n");
12624 #endif
12626 #if USE_SYSTEM_SQLITE+0!=1
12627 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12628 eputf("SQLite header and source version mismatch\n%s\n%s\n",
12629 sqlite3_sourceid(), SQLITE_SOURCE_ID);
12630 exit(1);
12632 #endif
12633 main_init(&data);
12635 /* On Windows, we must translate command-line arguments into UTF-8.
12636 ** The SQLite memory allocator subsystem has to be enabled in order to
12637 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
12638 ** subsequent sqlite3_config() calls will work. So copy all results into
12639 ** memory that does not come from the SQLite memory allocator.
12641 #if !SQLITE_SHELL_IS_UTF8
12642 sqlite3_initialize();
12643 argvToFree = malloc(sizeof(argv[0])*argc*2);
12644 shell_check_oom(argvToFree);
12645 argcToFree = argc;
12646 argv = argvToFree + argc;
12647 for(i=0; i<argc; i++){
12648 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12649 i64 n;
12650 shell_check_oom(z);
12651 n = strlen(z);
12652 argv[i] = malloc( n+1 );
12653 shell_check_oom(argv[i]);
12654 memcpy(argv[i], z, n+1);
12655 argvToFree[i] = argv[i];
12656 sqlite3_free(z);
12658 sqlite3_shutdown();
12659 #endif
12661 assert( argc>=1 && argv && argv[0] );
12662 Argv0 = argv[0];
12664 #ifdef SQLITE_SHELL_DBNAME_PROC
12666 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12667 ** of a C-function that will provide the name of the database file. Use
12668 ** this compile-time option to embed this shell program in larger
12669 ** applications. */
12670 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12671 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12672 warnInmemoryDb = 0;
12674 #endif
12676 /* Do an initial pass through the command-line argument to locate
12677 ** the name of the database file, the name of the initialization file,
12678 ** the size of the alternative malloc heap, options affecting commands
12679 ** or SQL run from the command line, and the first command to execute.
12681 #ifndef SQLITE_SHELL_FIDDLE
12682 verify_uninitialized();
12683 #endif
12684 for(i=1; i<argc; i++){
12685 char *z;
12686 z = argv[i];
12687 if( z[0]!='-' || i>nOptsEnd ){
12688 if( data.aAuxDb->zDbFilename==0 ){
12689 data.aAuxDb->zDbFilename = z;
12690 }else{
12691 /* Excess arguments are interpreted as SQL (or dot-commands) and
12692 ** mean that nothing is read from stdin */
12693 readStdin = 0;
12694 nCmd++;
12695 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12696 shell_check_oom(azCmd);
12697 azCmd[nCmd-1] = z;
12699 continue;
12701 if( z[1]=='-' ) z++;
12702 if( cli_strcmp(z, "-")==0 ){
12703 nOptsEnd = i;
12704 continue;
12705 }else if( cli_strcmp(z,"-separator")==0
12706 || cli_strcmp(z,"-nullvalue")==0
12707 || cli_strcmp(z,"-newline")==0
12708 || cli_strcmp(z,"-cmd")==0
12710 (void)cmdline_option_value(argc, argv, ++i);
12711 }else if( cli_strcmp(z,"-init")==0 ){
12712 zInitFile = cmdline_option_value(argc, argv, ++i);
12713 }else if( cli_strcmp(z,"-interactive")==0 ){
12714 }else if( cli_strcmp(z,"-batch")==0 ){
12715 /* Need to check for batch mode here to so we can avoid printing
12716 ** informational messages (like from process_sqliterc) before
12717 ** we do the actual processing of arguments later in a second pass.
12719 stdin_is_interactive = 0;
12720 }else if( cli_strcmp(z,"-utf8")==0 ){
12721 }else if( cli_strcmp(z,"-no-utf8")==0 ){
12722 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
12723 int val = 0;
12724 sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val);
12725 assert( val==0 );
12726 }else if( cli_strcmp(z,"-heap")==0 ){
12727 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12728 const char *zSize;
12729 sqlite3_int64 szHeap;
12731 zSize = cmdline_option_value(argc, argv, ++i);
12732 szHeap = integerValue(zSize);
12733 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12734 verify_uninitialized();
12735 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12736 #else
12737 (void)cmdline_option_value(argc, argv, ++i);
12738 #endif
12739 }else if( cli_strcmp(z,"-pagecache")==0 ){
12740 sqlite3_int64 n, sz;
12741 sz = integerValue(cmdline_option_value(argc,argv,++i));
12742 if( sz>70000 ) sz = 70000;
12743 if( sz<0 ) sz = 0;
12744 n = integerValue(cmdline_option_value(argc,argv,++i));
12745 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12746 n = 0xffffffffffffLL/sz;
12748 verify_uninitialized();
12749 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12750 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12751 data.shellFlgs |= SHFLG_Pagecache;
12752 }else if( cli_strcmp(z,"-lookaside")==0 ){
12753 int n, sz;
12754 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12755 if( sz<0 ) sz = 0;
12756 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12757 if( n<0 ) n = 0;
12758 verify_uninitialized();
12759 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12760 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12761 }else if( cli_strcmp(z,"-threadsafe")==0 ){
12762 int n;
12763 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12764 verify_uninitialized();
12765 switch( n ){
12766 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
12767 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
12768 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
12770 }else if( cli_strcmp(z,"-vfstrace")==0 ){
12771 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
12772 bEnableVfstrace = 1;
12773 #ifdef SQLITE_ENABLE_MULTIPLEX
12774 }else if( cli_strcmp(z,"-multiplex")==0 ){
12775 extern int sqlite3_multiplex_initialize(const char*,int);
12776 sqlite3_multiplex_initialize(0, 1);
12777 #endif
12778 }else if( cli_strcmp(z,"-mmap")==0 ){
12779 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12780 verify_uninitialized();
12781 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12782 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
12783 }else if( cli_strcmp(z,"-sorterref")==0 ){
12784 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12785 verify_uninitialized();
12786 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12787 #endif
12788 }else if( cli_strcmp(z,"-vfs")==0 ){
12789 zVfs = cmdline_option_value(argc, argv, ++i);
12790 #ifdef SQLITE_HAVE_ZLIB
12791 }else if( cli_strcmp(z,"-zip")==0 ){
12792 data.openMode = SHELL_OPEN_ZIPFILE;
12793 #endif
12794 }else if( cli_strcmp(z,"-append")==0 ){
12795 data.openMode = SHELL_OPEN_APPENDVFS;
12796 #ifndef SQLITE_OMIT_DESERIALIZE
12797 }else if( cli_strcmp(z,"-deserialize")==0 ){
12798 data.openMode = SHELL_OPEN_DESERIALIZE;
12799 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12800 data.szMax = integerValue(argv[++i]);
12801 #endif
12802 }else if( cli_strcmp(z,"-readonly")==0 ){
12803 data.openMode = SHELL_OPEN_READONLY;
12804 }else if( cli_strcmp(z,"-nofollow")==0 ){
12805 data.openFlags = SQLITE_OPEN_NOFOLLOW;
12806 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12807 }else if( cli_strncmp(z, "-A",2)==0 ){
12808 /* All remaining command-line arguments are passed to the ".archive"
12809 ** command, so ignore them */
12810 break;
12811 #endif
12812 }else if( cli_strcmp(z, "-memtrace")==0 ){
12813 sqlite3MemTraceActivate(stderr);
12814 }else if( cli_strcmp(z, "-pcachetrace")==0 ){
12815 sqlite3PcacheTraceActivate(stderr);
12816 }else if( cli_strcmp(z,"-bail")==0 ){
12817 bail_on_error = 1;
12818 }else if( cli_strcmp(z,"-nonce")==0 ){
12819 free(data.zNonce);
12820 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
12821 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
12822 ShellSetFlag(&data,SHFLG_TestingMode);
12823 }else if( cli_strcmp(z,"-safe")==0 ){
12824 /* no-op - catch this on the second pass */
12827 #ifndef SQLITE_SHELL_FIDDLE
12828 if( !bEnableVfstrace ) verify_uninitialized();
12829 #endif
12832 #ifdef SQLITE_SHELL_INIT_PROC
12834 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12835 ** of a C-function that will perform initialization actions on SQLite that
12836 ** occur just before or after sqlite3_initialize(). Use this compile-time
12837 ** option to embed this shell program in larger applications. */
12838 extern void SQLITE_SHELL_INIT_PROC(void);
12839 SQLITE_SHELL_INIT_PROC();
12841 #else
12842 /* All the sqlite3_config() calls have now been made. So it is safe
12843 ** to call sqlite3_initialize() and process any command line -vfs option. */
12844 sqlite3_initialize();
12845 #endif
12847 if( zVfs ){
12848 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12849 if( pVfs ){
12850 sqlite3_vfs_register(pVfs, 1);
12851 }else{
12852 eputf("no such VFS: \"%s\"\n", zVfs);
12853 exit(1);
12857 if( data.pAuxDb->zDbFilename==0 ){
12858 #ifndef SQLITE_OMIT_MEMORYDB
12859 data.pAuxDb->zDbFilename = ":memory:";
12860 warnInmemoryDb = argc==1;
12861 #else
12862 eputf("%s: Error: no database filename specified\n", Argv0);
12863 return 1;
12864 #endif
12866 data.out = stdout;
12867 #ifndef SQLITE_SHELL_FIDDLE
12868 sqlite3_appendvfs_init(0,0,0);
12869 #endif
12871 /* Go ahead and open the database file if it already exists. If the
12872 ** file does not exist, delay opening it. This prevents empty database
12873 ** files from being created if a user mistypes the database name argument
12874 ** to the sqlite command-line tool.
12876 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
12877 open_db(&data, 0);
12880 /* Process the initialization file if there is one. If no -init option
12881 ** is given on the command line, look for a file named ~/.sqliterc and
12882 ** try to process it.
12884 process_sqliterc(&data,zInitFile);
12886 /* Make a second pass through the command-line argument and set
12887 ** options. This second pass is delayed until after the initialization
12888 ** file is processed so that the command-line arguments will override
12889 ** settings in the initialization file.
12891 for(i=1; i<argc; i++){
12892 char *z = argv[i];
12893 if( z[0]!='-' || i>=nOptsEnd ) continue;
12894 if( z[1]=='-' ){ z++; }
12895 if( cli_strcmp(z,"-init")==0 ){
12896 i++;
12897 }else if( cli_strcmp(z,"-html")==0 ){
12898 data.mode = MODE_Html;
12899 }else if( cli_strcmp(z,"-list")==0 ){
12900 data.mode = MODE_List;
12901 }else if( cli_strcmp(z,"-quote")==0 ){
12902 data.mode = MODE_Quote;
12903 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
12904 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
12905 }else if( cli_strcmp(z,"-line")==0 ){
12906 data.mode = MODE_Line;
12907 }else if( cli_strcmp(z,"-column")==0 ){
12908 data.mode = MODE_Column;
12909 }else if( cli_strcmp(z,"-json")==0 ){
12910 data.mode = MODE_Json;
12911 }else if( cli_strcmp(z,"-markdown")==0 ){
12912 data.mode = MODE_Markdown;
12913 }else if( cli_strcmp(z,"-table")==0 ){
12914 data.mode = MODE_Table;
12915 }else if( cli_strcmp(z,"-box")==0 ){
12916 data.mode = MODE_Box;
12917 }else if( cli_strcmp(z,"-csv")==0 ){
12918 data.mode = MODE_Csv;
12919 memcpy(data.colSeparator,",",2);
12920 #ifdef SQLITE_HAVE_ZLIB
12921 }else if( cli_strcmp(z,"-zip")==0 ){
12922 data.openMode = SHELL_OPEN_ZIPFILE;
12923 #endif
12924 }else if( cli_strcmp(z,"-append")==0 ){
12925 data.openMode = SHELL_OPEN_APPENDVFS;
12926 #ifndef SQLITE_OMIT_DESERIALIZE
12927 }else if( cli_strcmp(z,"-deserialize")==0 ){
12928 data.openMode = SHELL_OPEN_DESERIALIZE;
12929 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12930 data.szMax = integerValue(argv[++i]);
12931 #endif
12932 }else if( cli_strcmp(z,"-readonly")==0 ){
12933 data.openMode = SHELL_OPEN_READONLY;
12934 }else if( cli_strcmp(z,"-nofollow")==0 ){
12935 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
12936 }else if( cli_strcmp(z,"-ascii")==0 ){
12937 data.mode = MODE_Ascii;
12938 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
12939 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
12940 }else if( cli_strcmp(z,"-tabs")==0 ){
12941 data.mode = MODE_List;
12942 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab);
12943 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row);
12944 }else if( cli_strcmp(z,"-separator")==0 ){
12945 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
12946 "%s",cmdline_option_value(argc,argv,++i));
12947 }else if( cli_strcmp(z,"-newline")==0 ){
12948 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
12949 "%s",cmdline_option_value(argc,argv,++i));
12950 }else if( cli_strcmp(z,"-nullvalue")==0 ){
12951 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
12952 "%s",cmdline_option_value(argc,argv,++i));
12953 }else if( cli_strcmp(z,"-header")==0 ){
12954 data.showHeader = 1;
12955 ShellSetFlag(&data, SHFLG_HeaderSet);
12956 }else if( cli_strcmp(z,"-noheader")==0 ){
12957 data.showHeader = 0;
12958 ShellSetFlag(&data, SHFLG_HeaderSet);
12959 }else if( cli_strcmp(z,"-echo")==0 ){
12960 ShellSetFlag(&data, SHFLG_Echo);
12961 }else if( cli_strcmp(z,"-eqp")==0 ){
12962 data.autoEQP = AUTOEQP_on;
12963 }else if( cli_strcmp(z,"-eqpfull")==0 ){
12964 data.autoEQP = AUTOEQP_full;
12965 }else if( cli_strcmp(z,"-stats")==0 ){
12966 data.statsOn = 1;
12967 }else if( cli_strcmp(z,"-scanstats")==0 ){
12968 data.scanstatsOn = 1;
12969 }else if( cli_strcmp(z,"-backslash")==0 ){
12970 /* Undocumented command-line option: -backslash
12971 ** Causes C-style backslash escapes to be evaluated in SQL statements
12972 ** prior to sending the SQL into SQLite. Useful for injecting
12973 ** crazy bytes in the middle of SQL statements for testing and debugging.
12975 ShellSetFlag(&data, SHFLG_Backslash);
12976 }else if( cli_strcmp(z,"-bail")==0 ){
12977 /* No-op. The bail_on_error flag should already be set. */
12978 }else if( cli_strcmp(z,"-version")==0 ){
12979 sputf(stdout, "%s %s (%d-bit)\n",
12980 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
12981 return 0;
12982 }else if( cli_strcmp(z,"-interactive")==0 ){
12983 /* Need to check for interactive override here to so that it can
12984 ** affect console setup (for Windows only) and testing thereof.
12986 stdin_is_interactive = 1;
12987 }else if( cli_strcmp(z,"-batch")==0 ){
12988 /* already handled */
12989 }else if( cli_strcmp(z,"-utf8")==0 ){
12990 /* already handled */
12991 }else if( cli_strcmp(z,"-no-utf8")==0 ){
12992 /* already handled */
12993 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
12994 /* already handled */
12995 }else if( cli_strcmp(z,"-heap")==0 ){
12996 i++;
12997 }else if( cli_strcmp(z,"-pagecache")==0 ){
12998 i+=2;
12999 }else if( cli_strcmp(z,"-lookaside")==0 ){
13000 i+=2;
13001 }else if( cli_strcmp(z,"-threadsafe")==0 ){
13002 i+=2;
13003 }else if( cli_strcmp(z,"-nonce")==0 ){
13004 i += 2;
13005 }else if( cli_strcmp(z,"-mmap")==0 ){
13006 i++;
13007 }else if( cli_strcmp(z,"-memtrace")==0 ){
13008 i++;
13009 }else if( cli_strcmp(z,"-pcachetrace")==0 ){
13010 i++;
13011 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
13012 }else if( cli_strcmp(z,"-sorterref")==0 ){
13013 i++;
13014 #endif
13015 }else if( cli_strcmp(z,"-vfs")==0 ){
13016 i++;
13017 }else if( cli_strcmp(z,"-vfstrace")==0 ){
13018 i++;
13019 #ifdef SQLITE_ENABLE_MULTIPLEX
13020 }else if( cli_strcmp(z,"-multiplex")==0 ){
13021 i++;
13022 #endif
13023 }else if( cli_strcmp(z,"-help")==0 ){
13024 usage(1);
13025 }else if( cli_strcmp(z,"-cmd")==0 ){
13026 /* Run commands that follow -cmd first and separately from commands
13027 ** that simply appear on the command-line. This seems goofy. It would
13028 ** be better if all commands ran in the order that they appear. But
13029 ** we retain the goofy behavior for historical compatibility. */
13030 if( i==argc-1 ) break;
13031 z = cmdline_option_value(argc,argv,++i);
13032 if( z[0]=='.' ){
13033 rc = do_meta_command(z, &data);
13034 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
13035 }else{
13036 open_db(&data, 0);
13037 rc = shell_exec(&data, z, &zErrMsg);
13038 if( zErrMsg!=0 ){
13039 shellEmitError(zErrMsg);
13040 if( bail_on_error ) return rc!=0 ? rc : 1;
13041 }else if( rc!=0 ){
13042 eputf("Error: unable to process SQL \"%s\"\n", z);
13043 if( bail_on_error ) return rc;
13046 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13047 }else if( cli_strncmp(z, "-A", 2)==0 ){
13048 if( nCmd>0 ){
13049 eputf("Error: cannot mix regular SQL or dot-commands"
13050 " with \"%s\"\n", z);
13051 return 1;
13053 open_db(&data, OPEN_DB_ZIPFILE);
13054 if( z[2] ){
13055 argv[i] = &z[2];
13056 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
13057 }else{
13058 arDotCommand(&data, 1, argv+i, argc-i);
13060 readStdin = 0;
13061 break;
13062 #endif
13063 }else if( cli_strcmp(z,"-safe")==0 ){
13064 data.bSafeMode = data.bSafeModePersist = 1;
13065 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
13066 /* Acted upon in first pass. */
13067 }else{
13068 eputf("%s: Error: unknown option: %s\n", Argv0, z);
13069 eputz("Use -help for a list of options.\n");
13070 return 1;
13072 data.cMode = data.mode;
13075 if( !readStdin ){
13076 /* Run all arguments that do not begin with '-' as if they were separate
13077 ** command-line inputs, except for the argToSkip argument which contains
13078 ** the database filename.
13080 for(i=0; i<nCmd; i++){
13081 if( azCmd[i][0]=='.' ){
13082 rc = do_meta_command(azCmd[i], &data);
13083 if( rc ){
13084 if( rc==2 ) rc = 0;
13085 goto shell_main_exit;
13087 }else{
13088 open_db(&data, 0);
13089 echo_group_input(&data, azCmd[i]);
13090 rc = shell_exec(&data, azCmd[i], &zErrMsg);
13091 if( zErrMsg || rc ){
13092 if( zErrMsg!=0 ){
13093 shellEmitError(zErrMsg);
13094 }else{
13095 eputf("Error: unable to process SQL: %s\n", azCmd[i]);
13097 sqlite3_free(zErrMsg);
13098 if( rc==0 ) rc = 1;
13099 goto shell_main_exit;
13103 }else{
13104 /* Run commands received from standard input
13106 if( stdin_is_interactive ){
13107 char *zHome;
13108 char *zHistory;
13109 int nHistory;
13110 #if CIO_WIN_WC_XLATE
13111 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
13112 #else
13113 # define SHELL_CIO_CHAR_SET ""
13114 #endif
13115 sputf(stdout, "SQLite version %s %.19s%s\n" /*extra-version-info*/
13116 "Enter \".help\" for usage hints.\n",
13117 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
13118 if( warnInmemoryDb ){
13119 sputz(stdout, "Connected to a ");
13120 printBold("transient in-memory database");
13121 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
13122 " persistent database.\n");
13124 zHistory = getenv("SQLITE_HISTORY");
13125 if( zHistory ){
13126 zHistory = strdup(zHistory);
13127 }else if( (zHome = find_home_dir(0))!=0 ){
13128 nHistory = strlen30(zHome) + 20;
13129 if( (zHistory = malloc(nHistory))!=0 ){
13130 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
13133 if( zHistory ){ shell_read_history(zHistory); }
13134 #if HAVE_READLINE || HAVE_EDITLINE
13135 rl_attempted_completion_function = readline_completion;
13136 #elif HAVE_LINENOISE
13137 linenoiseSetCompletionCallback(linenoise_completion, NULL);
13138 #endif
13139 data.in = 0;
13140 rc = process_input(&data);
13141 if( zHistory ){
13142 shell_stifle_history(2000);
13143 shell_write_history(zHistory);
13144 free(zHistory);
13146 }else{
13147 data.in = stdin;
13148 rc = process_input(&data);
13151 #ifndef SQLITE_SHELL_FIDDLE
13152 /* In WASM mode we have to leave the db state in place so that
13153 ** client code can "push" SQL into it after this call returns. */
13154 #ifndef SQLITE_OMIT_VIRTUALTABLE
13155 if( data.expert.pExpert ){
13156 expertFinish(&data, 1, 0);
13158 #endif
13159 shell_main_exit:
13160 free(azCmd);
13161 set_table_name(&data, 0);
13162 if( data.db ){
13163 session_close_all(&data, -1);
13164 close_db(data.db);
13166 for(i=0; i<ArraySize(data.aAuxDb); i++){
13167 sqlite3_free(data.aAuxDb[i].zFreeOnClose);
13168 if( data.aAuxDb[i].db ){
13169 session_close_all(&data, i);
13170 close_db(data.aAuxDb[i].db);
13173 find_home_dir(1);
13174 output_reset(&data);
13175 data.doXdgOpen = 0;
13176 clearTempFile(&data);
13177 #if !SQLITE_SHELL_IS_UTF8
13178 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
13179 free(argvToFree);
13180 #endif
13181 free(data.colWidth);
13182 free(data.zNonce);
13183 /* Clear the global data structure so that valgrind will detect memory
13184 ** leaks */
13185 memset(&data, 0, sizeof(data));
13186 if( bEnableVfstrace ){
13187 vfstrace_unregister("trace");
13189 #ifdef SQLITE_DEBUG
13190 if( sqlite3_memory_used()>mem_main_enter ){
13191 eputf("Memory leaked: %u bytes\n",
13192 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
13194 #endif
13195 #else /* SQLITE_SHELL_FIDDLE... */
13196 shell_main_exit:
13197 #endif
13198 return rc;
13202 #ifdef SQLITE_SHELL_FIDDLE
13203 /* Only for emcc experimentation purposes. */
13204 int fiddle_experiment(int a,int b){
13205 return a + b;
13209 ** Returns a pointer to the current DB handle.
13211 sqlite3 * fiddle_db_handle(){
13212 return globalDb;
13216 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
13217 ** "main" is assumed. Returns 0 if no db with the given name is
13218 ** open.
13220 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
13221 sqlite3_vfs * pVfs = 0;
13222 if(globalDb){
13223 sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
13224 SQLITE_FCNTL_VFS_POINTER, &pVfs);
13226 return pVfs;
13229 /* Only for emcc experimentation purposes. */
13230 sqlite3 * fiddle_db_arg(sqlite3 *arg){
13231 oputf("fiddle_db_arg(%p)\n", (const void*)arg);
13232 return arg;
13236 ** Intended to be called via a SharedWorker() while a separate
13237 ** SharedWorker() (which manages the wasm module) is performing work
13238 ** which should be interrupted. Unfortunately, SharedWorker is not
13239 ** portable enough to make real use of.
13241 void fiddle_interrupt(void){
13242 if( globalDb ) sqlite3_interrupt(globalDb);
13246 ** Returns the filename of the given db name, assuming "main" if
13247 ** zDbName is NULL. Returns NULL if globalDb is not opened.
13249 const char * fiddle_db_filename(const char * zDbName){
13250 return globalDb
13251 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
13252 : NULL;
13256 ** Completely wipes out the contents of the currently-opened database
13257 ** but leaves its storage intact for reuse. If any transactions are
13258 ** active, they are forcibly rolled back.
13260 void fiddle_reset_db(void){
13261 if( globalDb ){
13262 int rc;
13263 while( sqlite3_txn_state(globalDb,0)>0 ){
13265 ** Resolve problem reported in
13266 ** https://sqlite.org/forum/forumpost/0b41a25d65
13268 oputz("Rolling back in-progress transaction.\n");
13269 sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
13271 rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
13272 if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
13273 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
13278 ** Uses the current database's VFS xRead to stream the db file's
13279 ** contents out to the given callback. The callback gets a single
13280 ** chunk of size n (its 2nd argument) on each call and must return 0
13281 ** on success, non-0 on error. This function returns 0 on success,
13282 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
13283 ** code from the callback. Note that this is not thread-friendly: it
13284 ** expects that it will be the only thread reading the db file and
13285 ** takes no measures to ensure that is the case.
13287 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
13288 sqlite3_int64 nSize = 0;
13289 sqlite3_int64 nPos = 0;
13290 sqlite3_file * pFile = 0;
13291 unsigned char buf[1024 * 8];
13292 int nBuf = (int)sizeof(buf);
13293 int rc = shellState.db
13294 ? sqlite3_file_control(shellState.db, "main",
13295 SQLITE_FCNTL_FILE_POINTER, &pFile)
13296 : SQLITE_NOTFOUND;
13297 if( rc ) return rc;
13298 rc = pFile->pMethods->xFileSize(pFile, &nSize);
13299 if( rc ) return rc;
13300 if(nSize % nBuf){
13301 /* DB size is not an even multiple of the buffer size. Reduce
13302 ** buffer size so that we do not unduly inflate the db size when
13303 ** exporting. */
13304 if(0 == nSize % 4096) nBuf = 4096;
13305 else if(0 == nSize % 2048) nBuf = 2048;
13306 else if(0 == nSize % 1024) nBuf = 1024;
13307 else nBuf = 512;
13309 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
13310 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
13311 if(SQLITE_IOERR_SHORT_READ == rc){
13312 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
13314 if( 0==rc ) rc = xCallback(buf, nBuf);
13316 return rc;
13320 ** Trivial exportable function for emscripten. It processes zSql as if
13321 ** it were input to the sqlite3 shell and redirects all output to the
13322 ** wasm binding. fiddle_main() must have been called before this
13323 ** is called, or results are undefined.
13325 void fiddle_exec(const char * zSql){
13326 if(zSql && *zSql){
13327 if('.'==*zSql) puts(zSql);
13328 shellState.wasm.zInput = zSql;
13329 shellState.wasm.zPos = zSql;
13330 process_input(&shellState);
13331 shellState.wasm.zInput = shellState.wasm.zPos = 0;
13334 #endif /* SQLITE_SHELL_FIDDLE */