Fix harmless compiler warning in flockCheckReservedLock().
[sqlite.git] / src / shell.c.in
blobbbf08f8098ab7742b6b2aed1682f0516ca5d6101
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 pclose
196 # define pclose _pclose
197 # endif
198 #else
199 /* Make sure isatty() has a prototype. */
200 extern int isatty(int);
202 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
203 /* popen and pclose are not C89 functions and so are
204 ** sometimes omitted from the <stdio.h> header */
205 extern FILE *popen(const char*,const char*);
206 extern int pclose(FILE*);
207 # else
208 # define SQLITE_OMIT_POPEN 1
209 # endif
210 #endif
212 #if defined(_WIN32_WCE)
213 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
214 * thus we always assume that we have a console. That can be
215 * overridden with the -batch command line option.
217 #define isatty(x) 1
218 #endif
220 /* ctype macros that work with signed characters */
221 #define IsSpace(X) isspace((unsigned char)X)
222 #define IsDigit(X) isdigit((unsigned char)X)
223 #define ToLower(X) (char)tolower((unsigned char)X)
225 #if defined(_WIN32) || defined(WIN32)
226 #if SQLITE_OS_WINRT
227 #include <intrin.h>
228 #endif
229 #undef WIN32_LEAN_AND_MEAN
230 #define WIN32_LEAN_AND_MEAN
231 #include <windows.h>
233 /* string conversion routines only needed on Win32 */
234 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
235 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
236 #endif
238 INCLUDE ../ext/misc/sqlite3_stdio.h
239 INCLUDE ../ext/misc/sqlite3_stdio.c
241 /* Use console I/O package as a direct INCLUDE. */
242 #define SQLITE_INTERNAL_LINKAGE static
244 #ifdef SQLITE_SHELL_FIDDLE
245 /* Deselect most features from the console I/O package for Fiddle. */
246 # define SQLITE_CIO_NO_REDIRECT
247 # define SQLITE_CIO_NO_CLASSIFY
248 # define SQLITE_CIO_NO_TRANSLATE
249 # define SQLITE_CIO_NO_SETMODE
250 # define SQLITE_CIO_NO_FLUSH
251 #endif
253 #define eputz(z) sqlite3_fputs(z,stderr)
254 #define sputz(fp,z) sqlite3_fputs(z,fp)
256 /* True if the timer is enabled */
257 static int enableTimer = 0;
259 /* A version of strcmp() that works with NULL values */
260 static int cli_strcmp(const char *a, const char *b){
261 if( a==0 ) a = "";
262 if( b==0 ) b = "";
263 return strcmp(a,b);
265 static int cli_strncmp(const char *a, const char *b, size_t n){
266 if( a==0 ) a = "";
267 if( b==0 ) b = "";
268 return strncmp(a,b,n);
271 /* Return the current wall-clock time */
272 static sqlite3_int64 timeOfDay(void){
273 static sqlite3_vfs *clockVfs = 0;
274 sqlite3_int64 t;
275 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
276 if( clockVfs==0 ) return 0; /* Never actually happens */
277 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
278 clockVfs->xCurrentTimeInt64(clockVfs, &t);
279 }else{
280 double r;
281 clockVfs->xCurrentTime(clockVfs, &r);
282 t = (sqlite3_int64)(r*86400000.0);
284 return t;
287 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
288 #include <sys/time.h>
289 #include <sys/resource.h>
291 /* VxWorks does not support getrusage() as far as we can determine */
292 #if defined(_WRS_KERNEL) || defined(__RTP__)
293 struct rusage {
294 struct timeval ru_utime; /* user CPU time used */
295 struct timeval ru_stime; /* system CPU time used */
297 #define getrusage(A,B) memset(B,0,sizeof(*B))
298 #endif
301 /* Saved resource information for the beginning of an operation */
302 static struct rusage sBegin; /* CPU time at start */
303 static sqlite3_int64 iBegin; /* Wall-clock time at start */
306 ** Begin timing an operation
308 static void beginTimer(void){
309 if( enableTimer ){
310 getrusage(RUSAGE_SELF, &sBegin);
311 iBegin = timeOfDay();
315 /* Return the difference of two time_structs in seconds */
316 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
317 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
318 (double)(pEnd->tv_sec - pStart->tv_sec);
322 ** Print the timing results.
324 static void endTimer(FILE *out){
325 if( enableTimer ){
326 sqlite3_int64 iEnd = timeOfDay();
327 struct rusage sEnd;
328 getrusage(RUSAGE_SELF, &sEnd);
329 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
330 (iEnd - iBegin)*0.001,
331 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
332 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
336 #define BEGIN_TIMER beginTimer()
337 #define END_TIMER(X) endTimer(X)
338 #define HAS_TIMER 1
340 #elif (defined(_WIN32) || defined(WIN32))
342 /* Saved resource information for the beginning of an operation */
343 static HANDLE hProcess;
344 static FILETIME ftKernelBegin;
345 static FILETIME ftUserBegin;
346 static sqlite3_int64 ftWallBegin;
347 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
348 LPFILETIME, LPFILETIME);
349 static GETPROCTIMES getProcessTimesAddr = NULL;
352 ** Check to see if we have timer support. Return 1 if necessary
353 ** support found (or found previously).
355 static int hasTimer(void){
356 if( getProcessTimesAddr ){
357 return 1;
358 } else {
359 #if !SQLITE_OS_WINRT
360 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
361 ** versions. See if the version we are running on has it, and if it
362 ** does, save off a pointer to it and the current process handle.
364 hProcess = GetCurrentProcess();
365 if( hProcess ){
366 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
367 if( NULL != hinstLib ){
368 getProcessTimesAddr =
369 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
370 if( NULL != getProcessTimesAddr ){
371 return 1;
373 FreeLibrary(hinstLib);
376 #endif
378 return 0;
382 ** Begin timing an operation
384 static void beginTimer(void){
385 if( enableTimer && getProcessTimesAddr ){
386 FILETIME ftCreation, ftExit;
387 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
388 &ftKernelBegin,&ftUserBegin);
389 ftWallBegin = timeOfDay();
393 /* Return the difference of two FILETIME structs in seconds */
394 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
395 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
396 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
397 return (double) ((i64End - i64Start) / 10000000.0);
401 ** Print the timing results.
403 static void endTimer(FILE *out){
404 if( enableTimer && getProcessTimesAddr){
405 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
406 sqlite3_int64 ftWallEnd = timeOfDay();
407 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
408 sqlite3_fprintf(out, "Run Time: real %.3f user %f sys %f\n",
409 (ftWallEnd - ftWallBegin)*0.001,
410 timeDiff(&ftUserBegin, &ftUserEnd),
411 timeDiff(&ftKernelBegin, &ftKernelEnd));
415 #define BEGIN_TIMER beginTimer()
416 #define END_TIMER(X) endTimer(X)
417 #define HAS_TIMER hasTimer()
419 #else
420 #define BEGIN_TIMER
421 #define END_TIMER(X) /*no-op*/
422 #define HAS_TIMER 0
423 #endif
426 ** Used to prevent warnings about unused parameters
428 #define UNUSED_PARAMETER(x) (void)(x)
431 ** Number of elements in an array
433 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
436 ** If the following flag is set, then command execution stops
437 ** at an error if we are not interactive.
439 static int bail_on_error = 0;
442 ** Treat stdin as an interactive input if the following variable
443 ** is true. Otherwise, assume stdin is connected to a file or pipe.
445 static int stdin_is_interactive = 1;
448 ** On Windows systems we need to know if standard output is a console
449 ** in order to show that UTF-16 translation is done in the sign-on
450 ** banner. The following variable is true if it is the console.
452 static int stdout_is_console = 1;
455 ** The following is the open SQLite database. We make a pointer
456 ** to this database a static variable so that it can be accessed
457 ** by the SIGINT handler to interrupt database processing.
459 static sqlite3 *globalDb = 0;
462 ** True if an interrupt (Control-C) has been received.
464 static volatile int seenInterrupt = 0;
467 ** This is the name of our program. It is set in main(), used
468 ** in a number of other places, mostly for error messages.
470 static char *Argv0;
473 ** Prompt strings. Initialized in main. Settable with
474 ** .prompt main continue
476 #define PROMPT_LEN_MAX 20
477 /* First line prompt. default: "sqlite> " */
478 static char mainPrompt[PROMPT_LEN_MAX];
479 /* Continuation prompt. default: " ...> " */
480 static char continuePrompt[PROMPT_LEN_MAX];
482 /* This is variant of the standard-library strncpy() routine with the
483 ** one change that the destination string is always zero-terminated, even
484 ** if there is no zero-terminator in the first n-1 characters of the source
485 ** string.
487 static char *shell_strncpy(char *dest, const char *src, size_t n){
488 size_t i;
489 for(i=0; i<n-1 && src[i]!=0; i++) dest[i] = src[i];
490 dest[i] = 0;
491 return dest;
495 ** strcpy() workalike to squelch an unwarranted link-time warning
496 ** from OpenBSD.
498 static void shell_strcpy(char *dest, const char *src){
499 while( (*(dest++) = *(src++))!=0 ){}
503 ** Optionally disable dynamic continuation prompt.
504 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
505 ** or open parentheses level if non-zero, or continuation prompt as set.
506 ** This facility interacts with the scanner and process_input() where the
507 ** below 5 macros are used.
509 #ifdef SQLITE_OMIT_DYNAPROMPT
510 # define CONTINUATION_PROMPT continuePrompt
511 # define CONTINUE_PROMPT_RESET
512 # define CONTINUE_PROMPT_AWAITS(p,s)
513 # define CONTINUE_PROMPT_AWAITC(p,c)
514 # define CONTINUE_PAREN_INCR(p,n)
515 # define CONTINUE_PROMPT_PSTATE 0
516 typedef void *t_NoDynaPrompt;
517 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
518 #else
519 # define CONTINUATION_PROMPT dynamicContinuePrompt()
520 # define CONTINUE_PROMPT_RESET \
521 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
522 # define CONTINUE_PROMPT_AWAITS(p,s) \
523 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
524 # define CONTINUE_PROMPT_AWAITC(p,c) \
525 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
526 # define CONTINUE_PAREN_INCR(p,n) \
527 if(p && stdin_is_interactive) (trackParenLevel(p,n))
528 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
529 typedef struct DynaPrompt *t_DynaPromptRef;
530 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
532 static struct DynaPrompt {
533 char dynamicPrompt[PROMPT_LEN_MAX];
534 char acAwait[2];
535 int inParenLevel;
536 char *zScannerAwaits;
537 } dynPrompt = { {0}, {0}, 0, 0 };
539 /* Record parenthesis nesting level change, or force level to 0. */
540 static void trackParenLevel(struct DynaPrompt *p, int ni){
541 p->inParenLevel += ni;
542 if( ni==0 ) p->inParenLevel = 0;
543 p->zScannerAwaits = 0;
546 /* Record that a lexeme is opened, or closed with args==0. */
547 static void setLexemeOpen(struct DynaPrompt *p, char *s, char c){
548 if( s!=0 || c==0 ){
549 p->zScannerAwaits = s;
550 p->acAwait[0] = 0;
551 }else{
552 p->acAwait[0] = c;
553 p->zScannerAwaits = p->acAwait;
557 /* Upon demand, derive the continuation prompt to display. */
558 static char *dynamicContinuePrompt(void){
559 if( continuePrompt[0]==0
560 || (dynPrompt.zScannerAwaits==0 && dynPrompt.inParenLevel == 0) ){
561 return continuePrompt;
562 }else{
563 if( dynPrompt.zScannerAwaits ){
564 size_t ncp = strlen(continuePrompt);
565 size_t ndp = strlen(dynPrompt.zScannerAwaits);
566 if( ndp > ncp-3 ) return continuePrompt;
567 shell_strcpy(dynPrompt.dynamicPrompt, dynPrompt.zScannerAwaits);
568 while( ndp<3 ) dynPrompt.dynamicPrompt[ndp++] = ' ';
569 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
570 PROMPT_LEN_MAX-4);
571 }else{
572 if( dynPrompt.inParenLevel>9 ){
573 shell_strncpy(dynPrompt.dynamicPrompt, "(..", 4);
574 }else if( dynPrompt.inParenLevel<0 ){
575 shell_strncpy(dynPrompt.dynamicPrompt, ")x!", 4);
576 }else{
577 shell_strncpy(dynPrompt.dynamicPrompt, "(x.", 4);
578 dynPrompt.dynamicPrompt[2] = (char)('0'+dynPrompt.inParenLevel);
580 shell_strncpy(dynPrompt.dynamicPrompt+3, continuePrompt+3,
581 PROMPT_LEN_MAX-4);
584 return dynPrompt.dynamicPrompt;
586 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
588 /* Indicate out-of-memory and exit. */
589 static void shell_out_of_memory(void){
590 eputz("Error: out of memory\n");
591 exit(1);
594 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
595 ** out-of-memory error.
597 static void shell_check_oom(const void *p){
598 if( p==0 ) shell_out_of_memory();
602 ** Write I/O traces to the following stream.
604 #ifdef SQLITE_ENABLE_IOTRACE
605 static FILE *iotrace = 0;
606 #endif
609 ** This routine works like printf in that its first argument is a
610 ** format string and subsequent arguments are values to be substituted
611 ** in place of % fields. The result of formatting this string
612 ** is written to iotrace.
614 #ifdef SQLITE_ENABLE_IOTRACE
615 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
616 va_list ap;
617 char *z;
618 if( iotrace==0 ) return;
619 va_start(ap, zFormat);
620 z = sqlite3_vmprintf(zFormat, ap);
621 va_end(ap);
622 sqlite3_fprintf(iotrace, "%s", z);
623 sqlite3_free(z);
625 #endif
627 /* Lookup table to estimate the number of columns consumed by a Unicode
628 ** character.
630 static const struct {
631 unsigned char w; /* Width of the character in columns */
632 int iFirst; /* First character in a span having this width */
633 } aUWidth[] = {
634 /* {1, 0x00000}, */
635 {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
636 {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
637 {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
638 {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
639 {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
640 {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
641 {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
642 {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
643 {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
644 {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
645 {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
646 {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
647 {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
648 {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
649 {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
650 {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
651 {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
652 {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
653 {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
654 {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
655 {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
656 {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
657 {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
658 {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
659 {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
660 {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
661 {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
662 {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
663 {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
664 {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
665 {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
666 {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
667 {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
668 {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
669 {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
670 {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
671 {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
672 {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
673 {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
674 {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
675 {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
676 {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
677 {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
678 {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
679 {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
680 {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
681 {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
682 {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
683 {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
684 {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
685 {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
686 {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
687 {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
688 {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
689 {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
690 {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
691 {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
692 {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
693 {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
694 {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
695 {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
699 ** Return an estimate of the width, in columns, for the single Unicode
700 ** character c. For normal characters, the answer is always 1. But the
701 ** estimate might be 0 or 2 for zero-width and double-width characters.
703 ** Different display devices display unicode using different widths. So
704 ** it is impossible to know that true display width with 100% accuracy.
705 ** Inaccuracies in the width estimates might cause columns to be misaligned.
706 ** Unfortunately, there is nothing we can do about that.
708 int cli_wcwidth(int c){
709 int iFirst, iLast;
711 /* Fast path for common characters */
712 if( c<=0x300 ) return 1;
714 /* The general case */
715 iFirst = 0;
716 iLast = sizeof(aUWidth)/sizeof(aUWidth[0]) - 1;
717 while( iFirst<iLast-1 ){
718 int iMid = (iFirst+iLast)/2;
719 int cMid = aUWidth[iMid].iFirst;
720 if( cMid < c ){
721 iFirst = iMid;
722 }else if( cMid > c ){
723 iLast = iMid - 1;
724 }else{
725 return aUWidth[iMid].w;
728 if( aUWidth[iLast].iFirst > c ) return aUWidth[iFirst].w;
729 return aUWidth[iLast].w;
733 ** Compute the value and length of a multi-byte UTF-8 character that
734 ** begins at z[0]. Return the length. Write the Unicode value into *pU.
736 ** This routine only works for *multi-byte* UTF-8 characters.
738 static int decodeUtf8(const unsigned char *z, int *pU){
739 if( (z[0] & 0xe0)==0xc0 && (z[1] & 0xc0)==0x80 ){
740 *pU = ((z[0] & 0x1f)<<6) | (z[1] & 0x3f);
741 return 2;
743 if( (z[0] & 0xf0)==0xe0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80 ){
744 *pU = ((z[0] & 0x0f)<<12) | ((z[1] & 0x3f)<<6) | (z[2] & 0x3f);
745 return 3;
747 if( (z[0] & 0xf8)==0xf0 && (z[1] & 0xc0)==0x80 && (z[2] & 0xc0)==0x80
748 && (z[3] & 0xc0)==0x80
750 *pU = ((z[0] & 0x0f)<<18) | ((z[1] & 0x3f)<<12) | ((z[2] & 0x3f))<<6
751 | (z[4] & 0x3f);
752 return 4;
754 *pU = 0;
755 return 1;
759 #if 0 /* NOT USED */
761 ** Return the width, in display columns, of a UTF-8 string.
763 ** Each normal character counts as 1. Zero-width characters count
764 ** as zero, and double-width characters count as 2.
766 int cli_wcswidth(const char *z){
767 const unsigned char *a = (const unsigned char*)z;
768 int n = 0;
769 int i = 0;
770 unsigned char c;
771 while( (c = a[i])!=0 ){
772 if( c>=0xc0 ){
773 int u;
774 int len = decodeUtf8(&a[i], &u);
775 i += len;
776 n += cli_wcwidth(u);
777 }else if( c>=' ' ){
778 n++;
779 i++;
780 }else{
781 i++;
784 return n;
786 #endif
789 ** Output string zUtf to stdout as w characters. If w is negative,
790 ** then right-justify the text. W is the width in UTF-8 characters, not
791 ** in bytes. This is different from the %*.*s specification in printf
792 ** since with %*.*s the width is measured in bytes, not characters.
794 ** Take into account zero-width and double-width Unicode characters.
795 ** In other words, a zero-width character does not count toward the
796 ** the w limit. A double-width character counts as two.
798 static void utf8_width_print(FILE *out, int w, const char *zUtf){
799 const unsigned char *a = (const unsigned char*)zUtf;
800 unsigned char c;
801 int i = 0;
802 int n = 0;
803 int aw = w<0 ? -w : w;
804 if( zUtf==0 ) zUtf = "";
805 while( (c = a[i])!=0 ){
806 if( (c&0xc0)==0xc0 ){
807 int u;
808 int len = decodeUtf8(a+i, &u);
809 int x = cli_wcwidth(u);
810 if( x+n>aw ){
811 break;
813 i += len;
814 n += x;
815 }else if( n>=aw ){
816 break;
817 }else{
818 n++;
819 i++;
822 if( n>=aw ){
823 sqlite3_fprintf(out, "%.*s", i, zUtf);
824 }else if( w<0 ){
825 sqlite3_fprintf(out, "%*s%s", aw-n, "", zUtf);
826 }else{
827 sqlite3_fprintf(out, "%s%*s", zUtf, aw-n, "");
833 ** Determines if a string is a number of not.
835 static int isNumber(const char *z, int *realnum){
836 if( *z=='-' || *z=='+' ) z++;
837 if( !IsDigit(*z) ){
838 return 0;
840 z++;
841 if( realnum ) *realnum = 0;
842 while( IsDigit(*z) ){ z++; }
843 if( *z=='.' ){
844 z++;
845 if( !IsDigit(*z) ) return 0;
846 while( IsDigit(*z) ){ z++; }
847 if( realnum ) *realnum = 1;
849 if( *z=='e' || *z=='E' ){
850 z++;
851 if( *z=='+' || *z=='-' ) z++;
852 if( !IsDigit(*z) ) return 0;
853 while( IsDigit(*z) ){ z++; }
854 if( realnum ) *realnum = 1;
856 return *z==0;
860 ** Compute a string length that is limited to what can be stored in
861 ** lower 30 bits of a 32-bit signed integer.
863 static int strlen30(const char *z){
864 const char *z2 = z;
865 while( *z2 ){ z2++; }
866 return 0x3fffffff & (int)(z2 - z);
870 ** Return the length of a string in characters. Multibyte UTF8 characters
871 ** count as a single character.
873 static int strlenChar(const char *z){
874 int n = 0;
875 while( *z ){
876 if( (0xc0&*(z++))!=0x80 ) n++;
878 return n;
882 ** Return open FILE * if zFile exists, can be opened for read
883 ** and is an ordinary file or a character stream source.
884 ** Otherwise return 0.
886 static FILE * openChrSource(const char *zFile){
887 #if defined(_WIN32) || defined(WIN32)
888 struct __stat64 x = {0};
889 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
890 /* On Windows, open first, then check the stream nature. This order
891 ** is necessary because _stat() and sibs, when checking a named pipe,
892 ** effectively break the pipe as its supplier sees it. */
893 FILE *rv = sqlite3_fopen(zFile, "rb");
894 if( rv==0 ) return 0;
895 if( _fstat64(_fileno(rv), &x) != 0
896 || !STAT_CHR_SRC(x.st_mode)){
897 fclose(rv);
898 rv = 0;
900 return rv;
901 #else
902 struct stat x = {0};
903 int rc = stat(zFile, &x);
904 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
905 if( rc!=0 ) return 0;
906 if( STAT_CHR_SRC(x.st_mode) ){
907 return sqlite3_fopen(zFile, "rb");
908 }else{
909 return 0;
911 #endif
912 #undef STAT_CHR_SRC
916 ** This routine reads a line of text from FILE in, stores
917 ** the text in memory obtained from malloc() and returns a pointer
918 ** to the text. NULL is returned at end of file, or if malloc()
919 ** fails.
921 ** If zLine is not NULL then it is a malloced buffer returned from
922 ** a previous call to this routine that may be reused.
924 static char *local_getline(char *zLine, FILE *in){
925 int nLine = zLine==0 ? 0 : 100;
926 int n = 0;
928 while( 1 ){
929 if( n+100>nLine ){
930 nLine = nLine*2 + 100;
931 zLine = realloc(zLine, nLine);
932 shell_check_oom(zLine);
934 if( sqlite3_fgets(&zLine[n], nLine - n, in)==0 ){
935 if( n==0 ){
936 free(zLine);
937 return 0;
939 zLine[n] = 0;
940 break;
942 while( zLine[n] ) n++;
943 if( n>0 && zLine[n-1]=='\n' ){
944 n--;
945 if( n>0 && zLine[n-1]=='\r' ) n--;
946 zLine[n] = 0;
947 break;
950 return zLine;
954 ** Retrieve a single line of input text.
956 ** If in==0 then read from standard input and prompt before each line.
957 ** If isContinuation is true, then a continuation prompt is appropriate.
958 ** If isContinuation is zero, then the main prompt should be used.
960 ** If zPrior is not NULL then it is a buffer from a prior call to this
961 ** routine that can be reused.
963 ** The result is stored in space obtained from malloc() and must either
964 ** be freed by the caller or else passed back into this routine via the
965 ** zPrior argument for reuse.
967 #ifndef SQLITE_SHELL_FIDDLE
968 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
969 char *zPrompt;
970 char *zResult;
971 if( in!=0 ){
972 zResult = local_getline(zPrior, in);
973 }else{
974 zPrompt = isContinuation ? CONTINUATION_PROMPT : mainPrompt;
975 #if SHELL_USE_LOCAL_GETLINE
976 sputz(stdout, zPrompt);
977 fflush(stdout);
979 zResult = local_getline(zPrior, stdin);
980 zPrior = 0;
981 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
982 if( zResult==0 ) sqlite3_sleep(50);
983 }while( zResult==0 && seenInterrupt>0 );
984 #else
985 free(zPrior);
986 zResult = shell_readline(zPrompt);
987 while( zResult==0 ){
988 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
989 sqlite3_sleep(50);
990 if( seenInterrupt==0 ) break;
991 zResult = shell_readline("");
993 if( zResult && *zResult ) shell_add_history(zResult);
994 #endif
996 return zResult;
998 #endif /* !SQLITE_SHELL_FIDDLE */
1001 ** Return the value of a hexadecimal digit. Return -1 if the input
1002 ** is not a hex digit.
1004 static int hexDigitValue(char c){
1005 if( c>='0' && c<='9' ) return c - '0';
1006 if( c>='a' && c<='f' ) return c - 'a' + 10;
1007 if( c>='A' && c<='F' ) return c - 'A' + 10;
1008 return -1;
1012 ** Interpret zArg as an integer value, possibly with suffixes.
1014 static sqlite3_int64 integerValue(const char *zArg){
1015 sqlite3_int64 v = 0;
1016 static const struct { char *zSuffix; int iMult; } aMult[] = {
1017 { "KiB", 1024 },
1018 { "MiB", 1024*1024 },
1019 { "GiB", 1024*1024*1024 },
1020 { "KB", 1000 },
1021 { "MB", 1000000 },
1022 { "GB", 1000000000 },
1023 { "K", 1000 },
1024 { "M", 1000000 },
1025 { "G", 1000000000 },
1027 int i;
1028 int isNeg = 0;
1029 if( zArg[0]=='-' ){
1030 isNeg = 1;
1031 zArg++;
1032 }else if( zArg[0]=='+' ){
1033 zArg++;
1035 if( zArg[0]=='0' && zArg[1]=='x' ){
1036 int x;
1037 zArg += 2;
1038 while( (x = hexDigitValue(zArg[0]))>=0 ){
1039 v = (v<<4) + x;
1040 zArg++;
1042 }else{
1043 while( IsDigit(zArg[0]) ){
1044 v = v*10 + zArg[0] - '0';
1045 zArg++;
1048 for(i=0; i<ArraySize(aMult); i++){
1049 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
1050 v *= aMult[i].iMult;
1051 break;
1054 return isNeg? -v : v;
1058 ** A variable length string to which one can append text.
1060 typedef struct ShellText ShellText;
1061 struct ShellText {
1062 char *z;
1063 int n;
1064 int nAlloc;
1068 ** Initialize and destroy a ShellText object
1070 static void initText(ShellText *p){
1071 memset(p, 0, sizeof(*p));
1073 static void freeText(ShellText *p){
1074 free(p->z);
1075 initText(p);
1078 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1079 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1080 ** added to zIn, and the result returned in memory obtained from malloc().
1081 ** zIn, if it was not NULL, is freed.
1083 ** If the third argument, quote, is not '\0', then it is used as a
1084 ** quote character for zAppend.
1086 static void appendText(ShellText *p, const char *zAppend, char quote){
1087 i64 len;
1088 i64 i;
1089 i64 nAppend = strlen30(zAppend);
1091 len = nAppend+p->n+1;
1092 if( quote ){
1093 len += 2;
1094 for(i=0; i<nAppend; i++){
1095 if( zAppend[i]==quote ) len++;
1099 if( p->z==0 || p->n+len>=p->nAlloc ){
1100 p->nAlloc = p->nAlloc*2 + len + 20;
1101 p->z = realloc(p->z, p->nAlloc);
1102 shell_check_oom(p->z);
1105 if( quote ){
1106 char *zCsr = p->z+p->n;
1107 *zCsr++ = quote;
1108 for(i=0; i<nAppend; i++){
1109 *zCsr++ = zAppend[i];
1110 if( zAppend[i]==quote ) *zCsr++ = quote;
1112 *zCsr++ = quote;
1113 p->n = (int)(zCsr - p->z);
1114 *zCsr = '\0';
1115 }else{
1116 memcpy(p->z+p->n, zAppend, nAppend);
1117 p->n += nAppend;
1118 p->z[p->n] = '\0';
1123 ** Attempt to determine if identifier zName needs to be quoted, either
1124 ** because it contains non-alphanumeric characters, or because it is an
1125 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1126 ** that quoting is required.
1128 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1130 static char quoteChar(const char *zName){
1131 int i;
1132 if( zName==0 ) return '"';
1133 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
1134 for(i=0; zName[i]; i++){
1135 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
1137 return sqlite3_keyword_check(zName, i) ? '"' : 0;
1141 ** Construct a fake object name and column list to describe the structure
1142 ** of the view, virtual table, or table valued function zSchema.zName.
1144 static char *shellFakeSchema(
1145 sqlite3 *db, /* The database connection containing the vtab */
1146 const char *zSchema, /* Schema of the database holding the vtab */
1147 const char *zName /* The name of the virtual table */
1149 sqlite3_stmt *pStmt = 0;
1150 char *zSql;
1151 ShellText s;
1152 char cQuote;
1153 char *zDiv = "(";
1154 int nRow = 0;
1156 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1157 zSchema ? zSchema : "main", zName);
1158 shell_check_oom(zSql);
1159 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
1160 sqlite3_free(zSql);
1161 initText(&s);
1162 if( zSchema ){
1163 cQuote = quoteChar(zSchema);
1164 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
1165 appendText(&s, zSchema, cQuote);
1166 appendText(&s, ".", 0);
1168 cQuote = quoteChar(zName);
1169 appendText(&s, zName, cQuote);
1170 while( sqlite3_step(pStmt)==SQLITE_ROW ){
1171 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
1172 nRow++;
1173 appendText(&s, zDiv, 0);
1174 zDiv = ",";
1175 if( zCol==0 ) zCol = "";
1176 cQuote = quoteChar(zCol);
1177 appendText(&s, zCol, cQuote);
1179 appendText(&s, ")", 0);
1180 sqlite3_finalize(pStmt);
1181 if( nRow==0 ){
1182 freeText(&s);
1183 s.z = 0;
1185 return s.z;
1189 ** SQL function: strtod(X)
1191 ** Use the C-library strtod() function to convert string X into a double.
1192 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1193 ** routines against the C-library.
1195 static void shellStrtod(
1196 sqlite3_context *pCtx,
1197 int nVal,
1198 sqlite3_value **apVal
1200 char *z = (char*)sqlite3_value_text(apVal[0]);
1201 UNUSED_PARAMETER(nVal);
1202 if( z==0 ) return;
1203 sqlite3_result_double(pCtx, strtod(z,0));
1207 ** SQL function: dtostr(X)
1209 ** Use the C-library printf() function to convert real value X into a string.
1210 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1211 ** routines against the C-library.
1213 static void shellDtostr(
1214 sqlite3_context *pCtx,
1215 int nVal,
1216 sqlite3_value **apVal
1218 double r = sqlite3_value_double(apVal[0]);
1219 int n = nVal>=2 ? sqlite3_value_int(apVal[1]) : 26;
1220 char z[400];
1221 if( n<1 ) n = 1;
1222 if( n>350 ) n = 350;
1223 sqlite3_snprintf(sizeof(z), z, "%#+.*e", n, r);
1224 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
1229 ** SQL function: shell_module_schema(X)
1231 ** Return a fake schema for the table-valued function or eponymous virtual
1232 ** table X.
1234 static void shellModuleSchema(
1235 sqlite3_context *pCtx,
1236 int nVal,
1237 sqlite3_value **apVal
1239 const char *zName;
1240 char *zFake;
1241 UNUSED_PARAMETER(nVal);
1242 zName = (const char*)sqlite3_value_text(apVal[0]);
1243 zFake = zName? shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName) : 0;
1244 if( zFake ){
1245 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
1246 -1, sqlite3_free);
1247 free(zFake);
1252 ** SQL function: shell_add_schema(S,X)
1254 ** Add the schema name X to the CREATE statement in S and return the result.
1255 ** Examples:
1257 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1259 ** Also works on
1261 ** CREATE INDEX
1262 ** CREATE UNIQUE INDEX
1263 ** CREATE VIEW
1264 ** CREATE TRIGGER
1265 ** CREATE VIRTUAL TABLE
1267 ** This UDF is used by the .schema command to insert the schema name of
1268 ** attached databases into the middle of the sqlite_schema.sql field.
1270 static void shellAddSchemaName(
1271 sqlite3_context *pCtx,
1272 int nVal,
1273 sqlite3_value **apVal
1275 static const char *aPrefix[] = {
1276 "TABLE",
1277 "INDEX",
1278 "UNIQUE INDEX",
1279 "VIEW",
1280 "TRIGGER",
1281 "VIRTUAL TABLE"
1283 int i = 0;
1284 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1285 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1286 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1287 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1288 UNUSED_PARAMETER(nVal);
1289 if( zIn!=0 && cli_strncmp(zIn, "CREATE ", 7)==0 ){
1290 for(i=0; i<ArraySize(aPrefix); i++){
1291 int n = strlen30(aPrefix[i]);
1292 if( cli_strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1293 char *z = 0;
1294 char *zFake = 0;
1295 if( zSchema ){
1296 char cQuote = quoteChar(zSchema);
1297 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1298 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1299 }else{
1300 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1303 if( zName
1304 && aPrefix[i][0]=='V'
1305 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1307 if( z==0 ){
1308 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1309 }else{
1310 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1312 free(zFake);
1314 if( z ){
1315 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1316 return;
1321 sqlite3_result_value(pCtx, apVal[0]);
1325 ** The source code for several run-time loadable extensions is inserted
1326 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1327 ** code, we need to override some macros to make the included program code
1328 ** work here in the middle of this regular program.
1330 #define SQLITE_EXTENSION_INIT1
1331 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1333 #if defined(_WIN32) && defined(_MSC_VER)
1334 INCLUDE test_windirent.h
1335 INCLUDE test_windirent.c
1336 #define dirent DIRENT
1337 #endif
1338 INCLUDE ../ext/misc/memtrace.c
1339 INCLUDE ../ext/misc/pcachetrace.c
1340 INCLUDE ../ext/misc/shathree.c
1341 INCLUDE ../ext/misc/sha1.c
1342 INCLUDE ../ext/misc/uint.c
1343 INCLUDE ../ext/misc/decimal.c
1344 INCLUDE ../ext/misc/percentile.c
1345 #undef sqlite3_base_init
1346 #define sqlite3_base_init sqlite3_base64_init
1347 INCLUDE ../ext/misc/base64.c
1348 #undef sqlite3_base_init
1349 #define sqlite3_base_init sqlite3_base85_init
1350 #define OMIT_BASE85_CHECKER
1351 INCLUDE ../ext/misc/base85.c
1352 INCLUDE ../ext/misc/ieee754.c
1353 INCLUDE ../ext/misc/series.c
1354 INCLUDE ../ext/misc/regexp.c
1355 #ifndef SQLITE_SHELL_FIDDLE
1356 INCLUDE ../ext/misc/fileio.c
1357 INCLUDE ../ext/misc/completion.c
1358 INCLUDE ../ext/misc/appendvfs.c
1359 #endif
1360 #ifdef SQLITE_HAVE_ZLIB
1361 INCLUDE ../ext/misc/zipfile.c
1362 INCLUDE ../ext/misc/sqlar.c
1363 #endif
1364 INCLUDE ../ext/expert/sqlite3expert.h
1365 INCLUDE ../ext/expert/sqlite3expert.c
1366 INCLUDE ../ext/intck/sqlite3intck.h
1367 INCLUDE ../ext/intck/sqlite3intck.c
1368 INCLUDE ../ext/misc/stmtrand.c
1369 INCLUDE ../ext/misc/vfstrace.c
1371 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1372 #define SQLITE_SHELL_HAVE_RECOVER 1
1373 #else
1374 #define SQLITE_SHELL_HAVE_RECOVER 0
1375 #endif
1376 #if SQLITE_SHELL_HAVE_RECOVER
1377 INCLUDE ../ext/recover/sqlite3recover.h
1378 # ifndef SQLITE_HAVE_SQLITE3R
1379 INCLUDE ../ext/recover/dbdata.c
1380 INCLUDE ../ext/recover/sqlite3recover.c
1381 # endif /* SQLITE_HAVE_SQLITE3R */
1382 #endif
1383 #ifdef SQLITE_SHELL_EXTSRC
1384 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1385 #endif
1387 #if defined(SQLITE_ENABLE_SESSION)
1389 ** State information for a single open session
1391 typedef struct OpenSession OpenSession;
1392 struct OpenSession {
1393 char *zName; /* Symbolic name for this session */
1394 int nFilter; /* Number of xFilter rejection GLOB patterns */
1395 char **azFilter; /* Array of xFilter rejection GLOB patterns */
1396 sqlite3_session *p; /* The open session */
1398 #endif
1400 typedef struct ExpertInfo ExpertInfo;
1401 struct ExpertInfo {
1402 sqlite3expert *pExpert;
1403 int bVerbose;
1406 /* A single line in the EQP output */
1407 typedef struct EQPGraphRow EQPGraphRow;
1408 struct EQPGraphRow {
1409 int iEqpId; /* ID for this row */
1410 int iParentId; /* ID of the parent row */
1411 EQPGraphRow *pNext; /* Next row in sequence */
1412 char zText[1]; /* Text to display for this row */
1415 /* All EQP output is collected into an instance of the following */
1416 typedef struct EQPGraph EQPGraph;
1417 struct EQPGraph {
1418 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
1419 EQPGraphRow *pLast; /* Last element of the pRow list */
1420 char zPrefix[100]; /* Graph prefix */
1423 /* Parameters affecting columnar mode result display (defaulting together) */
1424 typedef struct ColModeOpts {
1425 int iWrap; /* In columnar modes, wrap lines reaching this limit */
1426 u8 bQuote; /* Quote results for .mode box and table */
1427 u8 bWordWrap; /* In columnar modes, wrap at word boundaries */
1428 } ColModeOpts;
1429 #define ColModeOpts_default { 60, 0, 0 }
1430 #define ColModeOpts_default_qbox { 60, 1, 0 }
1433 ** State information about the database connection is contained in an
1434 ** instance of the following structure.
1436 typedef struct ShellState ShellState;
1437 struct ShellState {
1438 sqlite3 *db; /* The database */
1439 u8 autoExplain; /* Automatically turn on .explain mode */
1440 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
1441 u8 autoEQPtest; /* autoEQP is in test mode */
1442 u8 autoEQPtrace; /* autoEQP is in trace mode */
1443 u8 scanstatsOn; /* True to display scan stats before each finalize */
1444 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1445 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
1446 u8 nEqpLevel; /* Depth of the EQP output graph */
1447 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
1448 u8 bSafeMode; /* True to prohibit unsafe operations */
1449 u8 bSafeModePersist; /* The long-term value of bSafeMode */
1450 u8 eRestoreState; /* See comments above doAutoDetectRestore() */
1451 u8 crlfMode; /* Do NL-to-CRLF translations when enabled (maybe) */
1452 ColModeOpts cmOpts; /* Option values affecting columnar mode output */
1453 unsigned statsOn; /* True to display memory stats before each finalize */
1454 unsigned mEqpLines; /* Mask of vertical lines in the EQP output graph */
1455 int inputNesting; /* Track nesting level of .read and other redirects */
1456 int outCount; /* Revert to stdout when reaching zero */
1457 int cnt; /* Number of records displayed so far */
1458 int lineno; /* Line number of last line read from in */
1459 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
1460 FILE *in; /* Read commands from this stream */
1461 FILE *out; /* Write results here */
1462 FILE *traceOut; /* Output for sqlite3_trace() */
1463 int nErr; /* Number of errors seen */
1464 int mode; /* An output mode setting */
1465 int modePrior; /* Saved mode */
1466 int cMode; /* temporary output mode for the current query */
1467 int normalMode; /* Output mode before ".explain on" */
1468 int writableSchema; /* True if PRAGMA writable_schema=ON */
1469 int showHeader; /* True to show column names in List or Column mode */
1470 int nCheck; /* Number of ".check" commands run */
1471 unsigned nProgress; /* Number of progress callbacks encountered */
1472 unsigned mxProgress; /* Maximum progress callbacks before failing */
1473 unsigned flgProgress; /* Flags for the progress callback */
1474 unsigned shellFlgs; /* Various flags */
1475 unsigned priorShFlgs; /* Saved copy of flags */
1476 sqlite3_int64 szMax; /* --maxsize argument to .open */
1477 char *zDestTable; /* Name of destination table when MODE_Insert */
1478 char *zTempFile; /* Temporary file that might need deleting */
1479 char zTestcase[30]; /* Name of current test case */
1480 char colSeparator[20]; /* Column separator character for several modes */
1481 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
1482 char colSepPrior[20]; /* Saved column separator */
1483 char rowSepPrior[20]; /* Saved row separator */
1484 int *colWidth; /* Requested width of each column in columnar modes */
1485 int *actualWidth; /* Actual width of each column */
1486 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
1487 char nullValue[20]; /* The text to print when a NULL comes back from
1488 ** the database */
1489 char outfile[FILENAME_MAX]; /* Filename for *out */
1490 sqlite3_stmt *pStmt; /* Current statement if any. */
1491 FILE *pLog; /* Write log output here */
1492 struct AuxDb { /* Storage space for auxiliary database connections */
1493 sqlite3 *db; /* Connection pointer */
1494 const char *zDbFilename; /* Filename used to open the connection */
1495 char *zFreeOnClose; /* Free this memory allocation on close */
1496 #if defined(SQLITE_ENABLE_SESSION)
1497 int nSession; /* Number of active sessions */
1498 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
1499 #endif
1500 } aAuxDb[5], /* Array of all database connections */
1501 *pAuxDb; /* Currently active database connection */
1502 int *aiIndent; /* Array of indents used in MODE_Explain */
1503 int nIndent; /* Size of array aiIndent[] */
1504 int iIndent; /* Index of current op in aiIndent[] */
1505 char *zNonce; /* Nonce for temporary safe-mode escapes */
1506 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
1507 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
1508 #ifdef SQLITE_SHELL_FIDDLE
1509 struct {
1510 const char * zInput; /* Input string from wasm/JS proxy */
1511 const char * zPos; /* Cursor pos into zInput */
1512 const char * zDefaultDbName; /* Default name for db file */
1513 } wasm;
1514 #endif
1517 #ifdef SQLITE_SHELL_FIDDLE
1518 static ShellState shellState;
1519 #endif
1522 /* Allowed values for ShellState.autoEQP
1524 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1525 #define AUTOEQP_on 1 /* Automatic EQP is on */
1526 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1527 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1529 /* Allowed values for ShellState.openMode
1531 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1532 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1533 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1534 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1535 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1536 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
1537 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
1539 /* Allowed values for ShellState.eTraceType
1541 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1542 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1543 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1545 /* Bits in the ShellState.flgProgress variable */
1546 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1547 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
1548 ** callback limit is reached, and for each
1549 ** top-level SQL statement */
1550 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
1553 ** These are the allowed shellFlgs values
1555 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1556 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1557 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1558 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1559 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1560 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1561 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
1562 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
1563 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
1564 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
1565 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
1568 ** Macros for testing and setting shellFlgs
1570 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1571 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1572 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1575 ** These are the allowed modes.
1577 #define MODE_Line 0 /* One column per line. Blank line between records */
1578 #define MODE_Column 1 /* One record per line in neat columns */
1579 #define MODE_List 2 /* One record per line with a separator */
1580 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1581 #define MODE_Html 4 /* Generate an XHTML table */
1582 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1583 #define MODE_Quote 6 /* Quote values as for SQL */
1584 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1585 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1586 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1587 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1588 #define MODE_Pretty 11 /* Pretty-print schemas */
1589 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1590 #define MODE_Json 13 /* Output JSON */
1591 #define MODE_Markdown 14 /* Markdown formatting */
1592 #define MODE_Table 15 /* MySQL-style table formatting */
1593 #define MODE_Box 16 /* Unicode box-drawing characters */
1594 #define MODE_Count 17 /* Output only a count of the rows of output */
1595 #define MODE_Off 18 /* No query output shown */
1596 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
1597 #define MODE_Www 20 /* Full web-page output */
1599 static const char *modeDescr[] = {
1600 "line",
1601 "column",
1602 "list",
1603 "semi",
1604 "html",
1605 "insert",
1606 "quote",
1607 "tcl",
1608 "csv",
1609 "explain",
1610 "ascii",
1611 "prettyprint",
1612 "eqp",
1613 "json",
1614 "markdown",
1615 "table",
1616 "box",
1617 "count",
1618 "off",
1619 "scanexp",
1620 "www",
1624 ** These are the column/row/line separators used by the various
1625 ** import/export modes.
1627 #define SEP_Column "|"
1628 #define SEP_Row "\n"
1629 #define SEP_Tab "\t"
1630 #define SEP_Space " "
1631 #define SEP_Comma ","
1632 #define SEP_CrLf "\r\n"
1633 #define SEP_Unit "\x1F"
1634 #define SEP_Record "\x1E"
1637 ** Limit input nesting via .read or any other input redirect.
1638 ** It's not too expensive, so a generous allowance can be made.
1640 #define MAX_INPUT_NESTING 25
1643 ** A callback for the sqlite3_log() interface.
1645 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1646 ShellState *p = (ShellState*)pArg;
1647 if( p->pLog==0 ) return;
1648 sqlite3_fprintf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1649 fflush(p->pLog);
1653 ** SQL function: shell_putsnl(X)
1655 ** Write the text X to the screen (or whatever output is being directed)
1656 ** adding a newline at the end, and then return X.
1658 static void shellPutsFunc(
1659 sqlite3_context *pCtx,
1660 int nVal,
1661 sqlite3_value **apVal
1663 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
1664 (void)nVal;
1665 sqlite3_fprintf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1666 sqlite3_result_value(pCtx, apVal[0]);
1670 ** If in safe mode, print an error message described by the arguments
1671 ** and exit immediately.
1673 static void failIfSafeMode(
1674 ShellState *p,
1675 const char *zErrMsg,
1678 if( p->bSafeMode ){
1679 va_list ap;
1680 char *zMsg;
1681 va_start(ap, zErrMsg);
1682 zMsg = sqlite3_vmprintf(zErrMsg, ap);
1683 va_end(ap);
1684 sqlite3_fprintf(stderr, "line %d: %s\n", p->lineno, zMsg);
1685 exit(1);
1690 ** SQL function: edit(VALUE)
1691 ** edit(VALUE,EDITOR)
1693 ** These steps:
1695 ** (1) Write VALUE into a temporary file.
1696 ** (2) Run program EDITOR on that temporary file.
1697 ** (3) Read the temporary file back and return its content as the result.
1698 ** (4) Delete the temporary file
1700 ** If the EDITOR argument is omitted, use the value in the VISUAL
1701 ** environment variable. If still there is no EDITOR, through an error.
1703 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1705 #ifndef SQLITE_NOHAVE_SYSTEM
1706 static void editFunc(
1707 sqlite3_context *context,
1708 int argc,
1709 sqlite3_value **argv
1711 const char *zEditor;
1712 char *zTempFile = 0;
1713 sqlite3 *db;
1714 char *zCmd = 0;
1715 int bBin;
1716 int rc;
1717 int hasCRLF = 0;
1718 FILE *f = 0;
1719 sqlite3_int64 sz;
1720 sqlite3_int64 x;
1721 unsigned char *p = 0;
1723 if( argc==2 ){
1724 zEditor = (const char*)sqlite3_value_text(argv[1]);
1725 }else{
1726 zEditor = getenv("VISUAL");
1728 if( zEditor==0 ){
1729 sqlite3_result_error(context, "no editor for edit()", -1);
1730 return;
1732 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1733 sqlite3_result_error(context, "NULL input to edit()", -1);
1734 return;
1736 db = sqlite3_context_db_handle(context);
1737 zTempFile = 0;
1738 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1739 if( zTempFile==0 ){
1740 sqlite3_uint64 r = 0;
1741 sqlite3_randomness(sizeof(r), &r);
1742 zTempFile = sqlite3_mprintf("temp%llx", r);
1743 if( zTempFile==0 ){
1744 sqlite3_result_error_nomem(context);
1745 return;
1748 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
1749 /* When writing the file to be edited, do \n to \r\n conversions on systems
1750 ** that want \r\n line endings */
1751 f = sqlite3_fopen(zTempFile, bBin ? "wb" : "w");
1752 if( f==0 ){
1753 sqlite3_result_error(context, "edit() cannot open temp file", -1);
1754 goto edit_func_end;
1756 sz = sqlite3_value_bytes(argv[0]);
1757 if( bBin ){
1758 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
1759 }else{
1760 const char *z = (const char*)sqlite3_value_text(argv[0]);
1761 /* Remember whether or not the value originally contained \r\n */
1762 if( z && strstr(z,"\r\n")!=0 ) hasCRLF = 1;
1763 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
1765 fclose(f);
1766 f = 0;
1767 if( x!=sz ){
1768 sqlite3_result_error(context, "edit() could not write the whole file", -1);
1769 goto edit_func_end;
1771 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1772 if( zCmd==0 ){
1773 sqlite3_result_error_nomem(context);
1774 goto edit_func_end;
1776 rc = system(zCmd);
1777 sqlite3_free(zCmd);
1778 if( rc ){
1779 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1780 goto edit_func_end;
1782 f = sqlite3_fopen(zTempFile, "rb");
1783 if( f==0 ){
1784 sqlite3_result_error(context,
1785 "edit() cannot reopen temp file after edit", -1);
1786 goto edit_func_end;
1788 fseek(f, 0, SEEK_END);
1789 sz = ftell(f);
1790 rewind(f);
1791 p = sqlite3_malloc64( sz+1 );
1792 if( p==0 ){
1793 sqlite3_result_error_nomem(context);
1794 goto edit_func_end;
1796 x = fread(p, 1, (size_t)sz, f);
1797 fclose(f);
1798 f = 0;
1799 if( x!=sz ){
1800 sqlite3_result_error(context, "could not read back the whole file", -1);
1801 goto edit_func_end;
1803 if( bBin ){
1804 sqlite3_result_blob64(context, p, sz, sqlite3_free);
1805 }else{
1806 sqlite3_int64 i, j;
1807 if( hasCRLF ){
1808 /* If the original contains \r\n then do no conversions back to \n */
1809 }else{
1810 /* If the file did not originally contain \r\n then convert any new
1811 ** \r\n back into \n */
1812 p[sz] = 0;
1813 for(i=j=0; i<sz; i++){
1814 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1815 p[j++] = p[i];
1817 sz = j;
1818 p[sz] = 0;
1820 sqlite3_result_text64(context, (const char*)p, sz,
1821 sqlite3_free, SQLITE_UTF8);
1823 p = 0;
1825 edit_func_end:
1826 if( f ) fclose(f);
1827 unlink(zTempFile);
1828 sqlite3_free(zTempFile);
1829 sqlite3_free(p);
1831 #endif /* SQLITE_NOHAVE_SYSTEM */
1834 ** Save or restore the current output mode
1836 static void outputModePush(ShellState *p){
1837 p->modePrior = p->mode;
1838 p->priorShFlgs = p->shellFlgs;
1839 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1840 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1842 static void outputModePop(ShellState *p){
1843 p->mode = p->modePrior;
1844 p->shellFlgs = p->priorShFlgs;
1845 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1846 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1850 ** Set output mode to text or binary for Windows.
1852 static void setCrlfMode(ShellState *p){
1853 #ifdef _WIN32
1854 if( p->crlfMode ){
1855 sqlite3_fsetmode(p->out, _O_TEXT);
1856 }else{
1857 sqlite3_fsetmode(p->out, _O_BINARY);
1859 #else
1860 UNUSED_PARAMETER(p);
1861 #endif
1865 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1867 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1868 int i;
1869 unsigned char *aBlob = (unsigned char*)pBlob;
1871 char *zStr = sqlite3_malloc(nBlob*2 + 1);
1872 shell_check_oom(zStr);
1874 for(i=0; i<nBlob; i++){
1875 static const char aHex[] = {
1876 '0', '1', '2', '3', '4', '5', '6', '7',
1877 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1879 zStr[i*2] = aHex[ (aBlob[i] >> 4) ];
1880 zStr[i*2+1] = aHex[ (aBlob[i] & 0x0F) ];
1882 zStr[i*2] = '\0';
1884 sqlite3_fprintf(out, "X'%s'", zStr);
1885 sqlite3_free(zStr);
1889 ** Find a string that is not found anywhere in z[]. Return a pointer
1890 ** to that string.
1892 ** Try to use zA and zB first. If both of those are already found in z[]
1893 ** then make up some string and store it in the buffer zBuf.
1895 static const char *unused_string(
1896 const char *z, /* Result must not appear anywhere in z */
1897 const char *zA, const char *zB, /* Try these first */
1898 char *zBuf /* Space to store a generated string */
1900 unsigned i = 0;
1901 if( strstr(z, zA)==0 ) return zA;
1902 if( strstr(z, zB)==0 ) return zB;
1904 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1905 }while( strstr(z,zBuf)!=0 );
1906 return zBuf;
1910 ** Output the given string as a quoted string using SQL quoting conventions.
1912 ** See also: output_quoted_escaped_string()
1914 static void output_quoted_string(ShellState *p, const char *z){
1915 int i;
1916 char c;
1917 FILE *out = p->out;
1918 sqlite3_fsetmode(out, _O_BINARY);
1919 if( z==0 ) return;
1920 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1921 if( c==0 ){
1922 sqlite3_fprintf(out, "'%s'",z);
1923 }else{
1924 sqlite3_fputs("'", out);
1925 while( *z ){
1926 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1927 if( c=='\'' ) i++;
1928 if( i ){
1929 sqlite3_fprintf(out, "%.*s", i, z);
1930 z += i;
1932 if( c=='\'' ){
1933 sqlite3_fputs("'", out);
1934 continue;
1936 if( c==0 ){
1937 break;
1939 z++;
1941 sqlite3_fputs("'", out);
1943 setCrlfMode(p);
1947 ** Output the given string as a quoted string using SQL quoting conventions.
1948 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1949 ** get corrupted by end-of-line translation facilities in some operating
1950 ** systems.
1952 ** This is like output_quoted_string() but with the addition of the \r\n
1953 ** escape mechanism.
1955 static void output_quoted_escaped_string(ShellState *p, const char *z){
1956 int i;
1957 char c;
1958 FILE *out = p->out;
1959 sqlite3_fsetmode(out, _O_BINARY);
1960 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1961 if( c==0 ){
1962 sqlite3_fprintf(out, "'%s'",z);
1963 }else{
1964 const char *zNL = 0;
1965 const char *zCR = 0;
1966 int nNL = 0;
1967 int nCR = 0;
1968 char zBuf1[20], zBuf2[20];
1969 for(i=0; z[i]; i++){
1970 if( z[i]=='\n' ) nNL++;
1971 if( z[i]=='\r' ) nCR++;
1973 if( nNL ){
1974 sqlite3_fputs("replace(", out);
1975 zNL = unused_string(z, "\\n", "\\012", zBuf1);
1977 if( nCR ){
1978 sqlite3_fputs("replace(", out);
1979 zCR = unused_string(z, "\\r", "\\015", zBuf2);
1981 sqlite3_fputs("'", out);
1982 while( *z ){
1983 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1984 if( c=='\'' ) i++;
1985 if( i ){
1986 sqlite3_fprintf(out, "%.*s", i, z);
1987 z += i;
1989 if( c=='\'' ){
1990 sqlite3_fputs("'", out);
1991 continue;
1993 if( c==0 ){
1994 break;
1996 z++;
1997 if( c=='\n' ){
1998 sqlite3_fputs(zNL, out);
1999 continue;
2001 sqlite3_fputs(zCR, out);
2003 sqlite3_fputs("'", out);
2004 if( nCR ){
2005 sqlite3_fprintf(out, ",'%s',char(13))", zCR);
2007 if( nNL ){
2008 sqlite3_fprintf(out, ",'%s',char(10))", zNL);
2011 setCrlfMode(p);
2015 ** Find earliest of chars within s specified in zAny.
2016 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
2018 static const char *anyOfInStr(const char *s, const char *zAny, size_t ns){
2019 const char *pcFirst = 0;
2020 if( ns == ~(size_t)0 ) ns = strlen(s);
2021 while(*zAny){
2022 const char *pc = (const char*)memchr(s, *zAny&0xff, ns);
2023 if( pc ){
2024 pcFirst = pc;
2025 ns = pcFirst - s;
2027 ++zAny;
2029 return pcFirst;
2032 /* Skip over as much z[] input char sequence as is valid UTF-8,
2033 ** limited per nAccept char's or whole characters and containing
2034 ** no char cn such that ((1<<cn) & ccm)!=0. On return, the
2035 ** sequence z:return (inclusive:exclusive) is validated UTF-8.
2036 ** Limit: nAccept>=0 => char count, nAccept<0 => character
2038 const char *zSkipValidUtf8(const char *z, int nAccept, long ccm){
2039 int ng = (nAccept<0)? -nAccept : 0;
2040 const char *pcLimit = (nAccept>=0)? z+nAccept : 0;
2041 assert(z!=0);
2042 while( (pcLimit)? (z<pcLimit) : (ng-- != 0) ){
2043 char c = *z;
2044 if( (c & 0x80) == 0 ){
2045 if( ccm != 0L && c < 0x20 && ((1L<<c) & ccm) != 0 ) return z;
2046 ++z; /* ASCII */
2047 }else if( (c & 0xC0) != 0xC0 ) return z; /* not a lead byte */
2048 else{
2049 const char *zt = z+1; /* Got lead byte, look at trail bytes.*/
2051 if( pcLimit && zt >= pcLimit ) return z;
2052 else{
2053 char ct = *zt++;
2054 if( ct==0 || (zt-z)>4 || (ct & 0xC0)!=0x80 ){
2055 /* Trailing bytes are too few, too many, or invalid. */
2056 return z;
2059 } while( ((c <<= 1) & 0x40) == 0x40 ); /* Eat lead byte's count. */
2060 z = zt;
2063 return z;
2068 ** Output the given string as a quoted according to C or TCL quoting rules.
2070 static void output_c_string(FILE *out, const char *z){
2071 char c;
2072 static const char *zq = "\"";
2073 static long ctrlMask = ~0L;
2074 static const char *zDQBSRO = "\"\\\x7f"; /* double-quote, backslash, rubout */
2075 char ace[3] = "\\?";
2076 char cbsSay;
2077 sqlite3_fputs(zq, out);
2078 while( *z!=0 ){
2079 const char *pcDQBSRO = anyOfInStr(z, zDQBSRO, ~(size_t)0);
2080 const char *pcPast = zSkipValidUtf8(z, INT_MAX, ctrlMask);
2081 const char *pcEnd = (pcDQBSRO && pcDQBSRO < pcPast)? pcDQBSRO : pcPast;
2082 if( pcEnd > z ){
2083 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
2085 if( (c = *pcEnd)==0 ) break;
2086 ++pcEnd;
2087 switch( c ){
2088 case '\\': case '"':
2089 cbsSay = (char)c;
2090 break;
2091 case '\t': cbsSay = 't'; break;
2092 case '\n': cbsSay = 'n'; break;
2093 case '\r': cbsSay = 'r'; break;
2094 case '\f': cbsSay = 'f'; break;
2095 default: cbsSay = 0; break;
2097 if( cbsSay ){
2098 ace[1] = cbsSay;
2099 sqlite3_fputs(ace, out);
2100 }else if( !isprint(c&0xff) ){
2101 sqlite3_fprintf(out, "\\%03o", c&0xff);
2102 }else{
2103 ace[1] = (char)c;
2104 sqlite3_fputs(ace+1, out);
2106 z = pcEnd;
2108 sqlite3_fputs(zq, out);
2112 ** Output the given string as a quoted according to JSON quoting rules.
2114 static void output_json_string(FILE *out, const char *z, i64 n){
2115 char c;
2116 static const char *zq = "\"";
2117 static long ctrlMask = ~0L;
2118 static const char *zDQBS = "\"\\";
2119 const char *pcLimit;
2120 char ace[3] = "\\?";
2121 char cbsSay;
2123 if( z==0 ) z = "";
2124 pcLimit = z + ((n<0)? strlen(z) : (size_t)n);
2125 sqlite3_fputs(zq, out);
2126 while( z < pcLimit ){
2127 const char *pcDQBS = anyOfInStr(z, zDQBS, pcLimit-z);
2128 const char *pcPast = zSkipValidUtf8(z, (int)(pcLimit-z), ctrlMask);
2129 const char *pcEnd = (pcDQBS && pcDQBS < pcPast)? pcDQBS : pcPast;
2130 if( pcEnd > z ){
2131 sqlite3_fprintf(out, "%.*s", (int)(pcEnd-z), z);
2132 z = pcEnd;
2134 if( z >= pcLimit ) break;
2135 c = *(z++);
2136 switch( c ){
2137 case '"': case '\\':
2138 cbsSay = (char)c;
2139 break;
2140 case '\b': cbsSay = 'b'; break;
2141 case '\f': cbsSay = 'f'; break;
2142 case '\n': cbsSay = 'n'; break;
2143 case '\r': cbsSay = 'r'; break;
2144 case '\t': cbsSay = 't'; break;
2145 default: cbsSay = 0; break;
2147 if( cbsSay ){
2148 ace[1] = cbsSay;
2149 sqlite3_fputs(ace, out);
2150 }else if( c<=0x1f ){
2151 sqlite3_fprintf(out, "u%04x", c);
2152 }else{
2153 ace[1] = (char)c;
2154 sqlite3_fputs(ace+1, out);
2157 sqlite3_fputs(zq, out);
2161 ** Output the given string with characters that are special to
2162 ** HTML escaped.
2164 static void output_html_string(FILE *out, const char *z){
2165 int i;
2166 if( z==0 ) z = "";
2167 while( *z ){
2168 for(i=0; z[i]
2169 && z[i]!='<'
2170 && z[i]!='&'
2171 && z[i]!='>'
2172 && z[i]!='\"'
2173 && z[i]!='\'';
2174 i++){}
2175 if( i>0 ){
2176 sqlite3_fprintf(out, "%.*s",i,z);
2178 if( z[i]=='<' ){
2179 sqlite3_fputs("&lt;", out);
2180 }else if( z[i]=='&' ){
2181 sqlite3_fputs("&amp;", out);
2182 }else if( z[i]=='>' ){
2183 sqlite3_fputs("&gt;", out);
2184 }else if( z[i]=='\"' ){
2185 sqlite3_fputs("&quot;", out);
2186 }else if( z[i]=='\'' ){
2187 sqlite3_fputs("&#39;", out);
2188 }else{
2189 break;
2191 z += i + 1;
2196 ** If a field contains any character identified by a 1 in the following
2197 ** array, then the string must be quoted for CSV.
2199 static const char needCsvQuote[] = {
2200 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2201 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2202 1, 0, 1, 0, 0, 0, 0, 1, 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, 0,
2205 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2206 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2207 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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,
2213 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2214 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2215 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2219 ** Output a single term of CSV. Actually, p->colSeparator is used for
2220 ** the separator, which may or may not be a comma. p->nullValue is
2221 ** the null value. Strings are quoted if necessary. The separator
2222 ** is only issued if bSep is true.
2224 static void output_csv(ShellState *p, const char *z, int bSep){
2225 if( z==0 ){
2226 sqlite3_fprintf(p->out, "%s",p->nullValue);
2227 }else{
2228 unsigned i;
2229 for(i=0; z[i]; i++){
2230 if( needCsvQuote[((unsigned char*)z)[i]] ){
2231 i = 0;
2232 break;
2235 if( i==0 || strstr(z, p->colSeparator)!=0 ){
2236 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
2237 shell_check_oom(zQuoted);
2238 sqlite3_fputs(zQuoted, p->out);
2239 sqlite3_free(zQuoted);
2240 }else{
2241 sqlite3_fputs(z, p->out);
2244 if( bSep ){
2245 sqlite3_fputs(p->colSeparator, p->out);
2250 ** This routine runs when the user presses Ctrl-C
2252 static void interrupt_handler(int NotUsed){
2253 UNUSED_PARAMETER(NotUsed);
2254 if( ++seenInterrupt>1 ) exit(1);
2255 if( globalDb ) sqlite3_interrupt(globalDb);
2258 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2260 ** This routine runs for console events (e.g. Ctrl-C) on Win32
2262 static BOOL WINAPI ConsoleCtrlHandler(
2263 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
2265 if( dwCtrlType==CTRL_C_EVENT ){
2266 interrupt_handler(0);
2267 return TRUE;
2269 return FALSE;
2271 #endif
2273 #ifndef SQLITE_OMIT_AUTHORIZATION
2275 ** This authorizer runs in safe mode.
2277 static int safeModeAuth(
2278 void *pClientData,
2279 int op,
2280 const char *zA1,
2281 const char *zA2,
2282 const char *zA3,
2283 const char *zA4
2285 ShellState *p = (ShellState*)pClientData;
2286 static const char *azProhibitedFunctions[] = {
2287 "edit",
2288 "fts3_tokenizer",
2289 "load_extension",
2290 "readfile",
2291 "writefile",
2292 "zipfile",
2293 "zipfile_cds",
2295 UNUSED_PARAMETER(zA1);
2296 UNUSED_PARAMETER(zA3);
2297 UNUSED_PARAMETER(zA4);
2298 switch( op ){
2299 case SQLITE_ATTACH: {
2300 #ifndef SQLITE_SHELL_FIDDLE
2301 /* In WASM builds the filesystem is a virtual sandbox, so
2302 ** there's no harm in using ATTACH. */
2303 failIfSafeMode(p, "cannot run ATTACH in safe mode");
2304 #endif
2305 break;
2307 case SQLITE_FUNCTION: {
2308 int i;
2309 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
2310 if( sqlite3_stricmp(zA2, azProhibitedFunctions[i])==0 ){
2311 failIfSafeMode(p, "cannot use the %s() function in safe mode",
2312 azProhibitedFunctions[i]);
2315 break;
2318 return SQLITE_OK;
2322 ** When the ".auth ON" is set, the following authorizer callback is
2323 ** invoked. It always returns SQLITE_OK.
2325 static int shellAuth(
2326 void *pClientData,
2327 int op,
2328 const char *zA1,
2329 const char *zA2,
2330 const char *zA3,
2331 const char *zA4
2333 ShellState *p = (ShellState*)pClientData;
2334 static const char *azAction[] = { 0,
2335 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2336 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2337 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2338 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2339 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2340 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2341 "PRAGMA", "READ", "SELECT",
2342 "TRANSACTION", "UPDATE", "ATTACH",
2343 "DETACH", "ALTER_TABLE", "REINDEX",
2344 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2345 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2347 int i;
2348 const char *az[4];
2349 az[0] = zA1;
2350 az[1] = zA2;
2351 az[2] = zA3;
2352 az[3] = zA4;
2353 sqlite3_fprintf(p->out, "authorizer: %s", azAction[op]);
2354 for(i=0; i<4; i++){
2355 sqlite3_fputs(" ", p->out);
2356 if( az[i] ){
2357 output_c_string(p->out, az[i]);
2358 }else{
2359 sqlite3_fputs("NULL", p->out);
2362 sqlite3_fputs("\n", p->out);
2363 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
2364 return SQLITE_OK;
2366 #endif
2369 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2371 ** This routine converts some CREATE TABLE statements for shadow tables
2372 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2374 ** If the schema statement in z[] contains a start-of-comment and if
2375 ** sqlite3_complete() returns false, try to terminate the comment before
2376 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2378 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
2379 char *zToFree = 0;
2380 if( z==0 ) return;
2381 if( zTail==0 ) return;
2382 if( zTail[0]==';' && (strstr(z, "/*")!=0 || strstr(z,"--")!=0) ){
2383 const char *zOrig = z;
2384 static const char *azTerm[] = { "", "*/", "\n" };
2385 int i;
2386 for(i=0; i<ArraySize(azTerm); i++){
2387 char *zNew = sqlite3_mprintf("%s%s;", zOrig, azTerm[i]);
2388 shell_check_oom(zNew);
2389 if( sqlite3_complete(zNew) ){
2390 size_t n = strlen(zNew);
2391 zNew[n-1] = 0;
2392 zToFree = zNew;
2393 z = zNew;
2394 break;
2396 sqlite3_free(zNew);
2399 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
2400 sqlite3_fprintf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
2401 }else{
2402 sqlite3_fprintf(out, "%s%s", z, zTail);
2404 sqlite3_free(zToFree);
2406 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
2407 char c = z[n];
2408 z[n] = 0;
2409 printSchemaLine(out, z, zTail);
2410 z[n] = c;
2414 ** Return true if string z[] has nothing but whitespace and comments to the
2415 ** end of the first line.
2417 static int wsToEol(const char *z){
2418 int i;
2419 for(i=0; z[i]; i++){
2420 if( z[i]=='\n' ) return 1;
2421 if( IsSpace(z[i]) ) continue;
2422 if( z[i]=='-' && z[i+1]=='-' ) return 1;
2423 return 0;
2425 return 1;
2429 ** Add a new entry to the EXPLAIN QUERY PLAN data
2431 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
2432 EQPGraphRow *pNew;
2433 i64 nText;
2434 if( zText==0 ) return;
2435 nText = strlen(zText);
2436 if( p->autoEQPtest ){
2437 sqlite3_fprintf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
2439 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
2440 shell_check_oom(pNew);
2441 pNew->iEqpId = iEqpId;
2442 pNew->iParentId = p2;
2443 memcpy(pNew->zText, zText, nText+1);
2444 pNew->pNext = 0;
2445 if( p->sGraph.pLast ){
2446 p->sGraph.pLast->pNext = pNew;
2447 }else{
2448 p->sGraph.pRow = pNew;
2450 p->sGraph.pLast = pNew;
2454 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2455 ** in p->sGraph.
2457 static void eqp_reset(ShellState *p){
2458 EQPGraphRow *pRow, *pNext;
2459 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
2460 pNext = pRow->pNext;
2461 sqlite3_free(pRow);
2463 memset(&p->sGraph, 0, sizeof(p->sGraph));
2466 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2467 ** pOld, or return the first such line if pOld is NULL
2469 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
2470 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
2471 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
2472 return pRow;
2475 /* Render a single level of the graph that has iEqpId as its parent. Called
2476 ** recursively to render sublevels.
2478 static void eqp_render_level(ShellState *p, int iEqpId){
2479 EQPGraphRow *pRow, *pNext;
2480 i64 n = strlen(p->sGraph.zPrefix);
2481 char *z;
2482 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
2483 pNext = eqp_next_row(p, iEqpId, pRow);
2484 z = pRow->zText;
2485 sqlite3_fprintf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
2486 pNext ? "|--" : "`--", z);
2487 if( n<(i64)sizeof(p->sGraph.zPrefix)-7 ){
2488 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
2489 eqp_render_level(p, pRow->iEqpId);
2490 p->sGraph.zPrefix[n] = 0;
2496 ** Display and reset the EXPLAIN QUERY PLAN data
2498 static void eqp_render(ShellState *p, i64 nCycle){
2499 EQPGraphRow *pRow = p->sGraph.pRow;
2500 if( pRow ){
2501 if( pRow->zText[0]=='-' ){
2502 if( pRow->pNext==0 ){
2503 eqp_reset(p);
2504 return;
2506 sqlite3_fprintf(p->out, "%s\n", pRow->zText+3);
2507 p->sGraph.pRow = pRow->pNext;
2508 sqlite3_free(pRow);
2509 }else if( nCycle>0 ){
2510 sqlite3_fprintf(p->out, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle);
2511 }else{
2512 sqlite3_fputs("QUERY PLAN\n", p->out);
2514 p->sGraph.zPrefix[0] = 0;
2515 eqp_render_level(p, 0);
2516 eqp_reset(p);
2520 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2522 ** Progress handler callback.
2524 static int progress_handler(void *pClientData) {
2525 ShellState *p = (ShellState*)pClientData;
2526 p->nProgress++;
2527 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
2528 sqlite3_fprintf(p->out, "Progress limit reached (%u)\n", p->nProgress);
2529 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
2530 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
2531 return 1;
2533 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
2534 sqlite3_fprintf(p->out, "Progress %u\n", p->nProgress);
2536 return 0;
2538 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2541 ** Print N dashes
2543 static void print_dashes(FILE *out, int N){
2544 const char zDash[] = "--------------------------------------------------";
2545 const int nDash = sizeof(zDash) - 1;
2546 while( N>nDash ){
2547 sqlite3_fputs(zDash, out);
2548 N -= nDash;
2550 sqlite3_fprintf(out, "%.*s", N, zDash);
2554 ** Print a markdown or table-style row separator using ascii-art
2556 static void print_row_separator(
2557 ShellState *p,
2558 int nArg,
2559 const char *zSep
2561 int i;
2562 if( nArg>0 ){
2563 sqlite3_fputs(zSep, p->out);
2564 print_dashes(p->out, p->actualWidth[0]+2);
2565 for(i=1; i<nArg; i++){
2566 sqlite3_fputs(zSep, p->out);
2567 print_dashes(p->out, p->actualWidth[i]+2);
2569 sqlite3_fputs(zSep, p->out);
2571 sqlite3_fputs("\n", p->out);
2575 ** This is the callback routine that the shell
2576 ** invokes for each row of a query result.
2578 static int shell_callback(
2579 void *pArg,
2580 int nArg, /* Number of result columns */
2581 char **azArg, /* Text of each result column */
2582 char **azCol, /* Column names */
2583 int *aiType /* Column types. Might be NULL */
2585 int i;
2586 ShellState *p = (ShellState*)pArg;
2588 if( azArg==0 ) return 0;
2589 switch( p->cMode ){
2590 case MODE_Count:
2591 case MODE_Off: {
2592 break;
2594 case MODE_Line: {
2595 int w = 5;
2596 if( azArg==0 ) break;
2597 for(i=0; i<nArg; i++){
2598 int len = strlen30(azCol[i] ? azCol[i] : "");
2599 if( len>w ) w = len;
2601 if( p->cnt++>0 ) sqlite3_fputs(p->rowSeparator, p->out);
2602 for(i=0; i<nArg; i++){
2603 sqlite3_fprintf(p->out, "%*s = %s%s", w, azCol[i],
2604 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
2606 break;
2608 case MODE_ScanExp:
2609 case MODE_Explain: {
2610 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
2611 static const int aExplainMap[] = {0, 1, 2, 3, 4, 5, 6, 7 };
2612 static const int aScanExpWidth[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13};
2613 static const int aScanExpMap[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
2615 const int *aWidth = aExplainWidth;
2616 const int *aMap = aExplainMap;
2617 int nWidth = ArraySize(aExplainWidth);
2618 int iIndent = 1;
2620 if( p->cMode==MODE_ScanExp ){
2621 aWidth = aScanExpWidth;
2622 aMap = aScanExpMap;
2623 nWidth = ArraySize(aScanExpWidth);
2624 iIndent = 3;
2626 if( nArg>nWidth ) nArg = nWidth;
2628 /* If this is the first row seen, print out the headers */
2629 if( p->cnt++==0 ){
2630 for(i=0; i<nArg; i++){
2631 utf8_width_print(p->out, aWidth[i], azCol[ aMap[i] ]);
2632 sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
2634 for(i=0; i<nArg; i++){
2635 print_dashes(p->out, aWidth[i]);
2636 sqlite3_fputs(i==nArg-1 ? "\n" : " ", p->out);
2640 /* If there is no data, exit early. */
2641 if( azArg==0 ) break;
2643 for(i=0; i<nArg; i++){
2644 const char *zSep = " ";
2645 int w = aWidth[i];
2646 const char *zVal = azArg[ aMap[i] ];
2647 if( i==nArg-1 ) w = 0;
2648 if( zVal && strlenChar(zVal)>w ){
2649 w = strlenChar(zVal);
2650 zSep = " ";
2652 if( i==iIndent && p->aiIndent && p->pStmt ){
2653 if( p->iIndent<p->nIndent ){
2654 sqlite3_fprintf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
2656 p->iIndent++;
2658 utf8_width_print(p->out, w, zVal ? zVal : p->nullValue);
2659 sqlite3_fputs(i==nArg-1 ? "\n" : zSep, p->out);
2661 break;
2663 case MODE_Semi: { /* .schema and .fullschema output */
2664 printSchemaLine(p->out, azArg[0], ";\n");
2665 break;
2667 case MODE_Pretty: { /* .schema and .fullschema with --indent */
2668 char *z;
2669 int j;
2670 int nParen = 0;
2671 char cEnd = 0;
2672 char c;
2673 int nLine = 0;
2674 assert( nArg==1 );
2675 if( azArg[0]==0 ) break;
2676 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2677 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2679 sqlite3_fprintf(p->out, "%s;\n", azArg[0]);
2680 break;
2682 z = sqlite3_mprintf("%s", azArg[0]);
2683 shell_check_oom(z);
2684 j = 0;
2685 for(i=0; IsSpace(z[i]); i++){}
2686 for(; (c = z[i])!=0; i++){
2687 if( IsSpace(c) ){
2688 if( z[j-1]=='\r' ) z[j-1] = '\n';
2689 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2690 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2691 j--;
2693 z[j++] = c;
2695 while( j>0 && IsSpace(z[j-1]) ){ j--; }
2696 z[j] = 0;
2697 if( strlen30(z)>=79 ){
2698 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
2699 if( c==cEnd ){
2700 cEnd = 0;
2701 }else if( c=='"' || c=='\'' || c=='`' ){
2702 cEnd = c;
2703 }else if( c=='[' ){
2704 cEnd = ']';
2705 }else if( c=='-' && z[i+1]=='-' ){
2706 cEnd = '\n';
2707 }else if( c=='(' ){
2708 nParen++;
2709 }else if( c==')' ){
2710 nParen--;
2711 if( nLine>0 && nParen==0 && j>0 ){
2712 printSchemaLineN(p->out, z, j, "\n");
2713 j = 0;
2716 z[j++] = c;
2717 if( nParen==1 && cEnd==0
2718 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2720 if( c=='\n' ) j--;
2721 printSchemaLineN(p->out, z, j, "\n ");
2722 j = 0;
2723 nLine++;
2724 while( IsSpace(z[i+1]) ){ i++; }
2727 z[j] = 0;
2729 printSchemaLine(p->out, z, ";\n");
2730 sqlite3_free(z);
2731 break;
2733 case MODE_List: {
2734 if( p->cnt++==0 && p->showHeader ){
2735 for(i=0; i<nArg; i++){
2736 sqlite3_fprintf(p->out, "%s%s", azCol[i],
2737 i==nArg-1 ? p->rowSeparator : p->colSeparator);
2740 if( azArg==0 ) break;
2741 for(i=0; i<nArg; i++){
2742 char *z = azArg[i];
2743 if( z==0 ) z = p->nullValue;
2744 sqlite3_fputs(z, p->out);
2745 sqlite3_fputs((i<nArg-1)? p->colSeparator : p->rowSeparator, p->out);
2747 break;
2749 case MODE_Www:
2750 case MODE_Html: {
2751 if( p->cnt==0 && p->cMode==MODE_Www ){
2752 sqlite3_fputs(
2753 "</PRE>\n"
2754 "<TABLE border='1' cellspacing='0' cellpadding='2'>\n"
2755 ,p->out
2758 if( p->cnt==0 && (p->showHeader || p->cMode==MODE_Www) ){
2759 sqlite3_fputs("<TR>", p->out);
2760 for(i=0; i<nArg; i++){
2761 sqlite3_fputs("<TH>", p->out);
2762 output_html_string(p->out, azCol[i]);
2763 sqlite3_fputs("</TH>\n", p->out);
2765 sqlite3_fputs("</TR>\n", p->out);
2767 p->cnt++;
2768 if( azArg==0 ) break;
2769 sqlite3_fputs("<TR>", p->out);
2770 for(i=0; i<nArg; i++){
2771 sqlite3_fputs("<TD>", p->out);
2772 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2773 sqlite3_fputs("</TD>\n", p->out);
2775 sqlite3_fputs("</TR>\n", p->out);
2776 break;
2778 case MODE_Tcl: {
2779 if( p->cnt++==0 && p->showHeader ){
2780 for(i=0; i<nArg; i++){
2781 output_c_string(p->out, azCol[i] ? azCol[i] : "");
2782 if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
2784 sqlite3_fputs(p->rowSeparator, p->out);
2786 if( azArg==0 ) break;
2787 for(i=0; i<nArg; i++){
2788 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2789 if(i<nArg-1) sqlite3_fputs(p->colSeparator, p->out);
2791 sqlite3_fputs(p->rowSeparator, p->out);
2792 break;
2794 case MODE_Csv: {
2795 sqlite3_fsetmode(p->out, _O_BINARY);
2796 if( p->cnt++==0 && p->showHeader ){
2797 for(i=0; i<nArg; i++){
2798 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2800 sqlite3_fputs(p->rowSeparator, p->out);
2802 if( nArg>0 ){
2803 for(i=0; i<nArg; i++){
2804 output_csv(p, azArg[i], i<nArg-1);
2806 sqlite3_fputs(p->rowSeparator, p->out);
2808 setCrlfMode(p);
2809 break;
2811 case MODE_Insert: {
2812 if( azArg==0 ) break;
2813 sqlite3_fprintf(p->out, "INSERT INTO %s",p->zDestTable);
2814 if( p->showHeader ){
2815 sqlite3_fputs("(", p->out);
2816 for(i=0; i<nArg; i++){
2817 if( i>0 ) sqlite3_fputs(",", p->out);
2818 if( quoteChar(azCol[i]) ){
2819 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2820 shell_check_oom(z);
2821 sqlite3_fputs(z, p->out);
2822 sqlite3_free(z);
2823 }else{
2824 sqlite3_fprintf(p->out, "%s", azCol[i]);
2827 sqlite3_fputs(")", p->out);
2829 p->cnt++;
2830 for(i=0; i<nArg; i++){
2831 sqlite3_fputs(i>0 ? "," : " VALUES(", p->out);
2832 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2833 sqlite3_fputs("NULL", p->out);
2834 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2835 if( ShellHasFlag(p, SHFLG_Newlines) ){
2836 output_quoted_string(p, azArg[i]);
2837 }else{
2838 output_quoted_escaped_string(p, azArg[i]);
2840 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2841 sqlite3_fputs(azArg[i], p->out);
2842 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2843 char z[50];
2844 double r = sqlite3_column_double(p->pStmt, i);
2845 sqlite3_uint64 ur;
2846 memcpy(&ur,&r,sizeof(r));
2847 if( ur==0x7ff0000000000000LL ){
2848 sqlite3_fputs("9.0e+999", p->out);
2849 }else if( ur==0xfff0000000000000LL ){
2850 sqlite3_fputs("-9.0e+999", p->out);
2851 }else{
2852 sqlite3_int64 ir = (sqlite3_int64)r;
2853 if( r==(double)ir ){
2854 sqlite3_snprintf(50,z,"%lld.0", ir);
2855 }else{
2856 sqlite3_snprintf(50,z,"%!.20g", r);
2858 sqlite3_fputs(z, p->out);
2860 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2861 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2862 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2863 output_hex_blob(p->out, pBlob, nBlob);
2864 }else if( isNumber(azArg[i], 0) ){
2865 sqlite3_fputs(azArg[i], p->out);
2866 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2867 output_quoted_string(p, azArg[i]);
2868 }else{
2869 output_quoted_escaped_string(p, azArg[i]);
2872 sqlite3_fputs(");\n", p->out);
2873 break;
2875 case MODE_Json: {
2876 if( azArg==0 ) break;
2877 if( p->cnt==0 ){
2878 sqlite3_fputs("[{", p->out);
2879 }else{
2880 sqlite3_fputs(",\n{", p->out);
2882 p->cnt++;
2883 for(i=0; i<nArg; i++){
2884 output_json_string(p->out, azCol[i], -1);
2885 sqlite3_fputs(":", p->out);
2886 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2887 sqlite3_fputs("null", p->out);
2888 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2889 char z[50];
2890 double r = sqlite3_column_double(p->pStmt, i);
2891 sqlite3_uint64 ur;
2892 memcpy(&ur,&r,sizeof(r));
2893 if( ur==0x7ff0000000000000LL ){
2894 sqlite3_fputs("9.0e+999", p->out);
2895 }else if( ur==0xfff0000000000000LL ){
2896 sqlite3_fputs("-9.0e+999", p->out);
2897 }else{
2898 sqlite3_snprintf(50,z,"%!.20g", r);
2899 sqlite3_fputs(z, p->out);
2901 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2902 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2903 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2904 output_json_string(p->out, pBlob, nBlob);
2905 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2906 output_json_string(p->out, azArg[i], -1);
2907 }else{
2908 sqlite3_fputs(azArg[i], p->out);
2910 if( i<nArg-1 ){
2911 sqlite3_fputs(",", p->out);
2914 sqlite3_fputs("}", p->out);
2915 break;
2917 case MODE_Quote: {
2918 if( azArg==0 ) break;
2919 if( p->cnt==0 && p->showHeader ){
2920 for(i=0; i<nArg; i++){
2921 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
2922 output_quoted_string(p, azCol[i]);
2924 sqlite3_fputs(p->rowSeparator, p->out);
2926 p->cnt++;
2927 for(i=0; i<nArg; i++){
2928 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
2929 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2930 sqlite3_fputs("NULL", p->out);
2931 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2932 output_quoted_string(p, azArg[i]);
2933 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2934 sqlite3_fputs(azArg[i], p->out);
2935 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2936 char z[50];
2937 double r = sqlite3_column_double(p->pStmt, i);
2938 sqlite3_snprintf(50,z,"%!.20g", r);
2939 sqlite3_fputs(z, p->out);
2940 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2941 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2942 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2943 output_hex_blob(p->out, pBlob, nBlob);
2944 }else if( isNumber(azArg[i], 0) ){
2945 sqlite3_fputs(azArg[i], p->out);
2946 }else{
2947 output_quoted_string(p, azArg[i]);
2950 sqlite3_fputs(p->rowSeparator, p->out);
2951 break;
2953 case MODE_Ascii: {
2954 if( p->cnt++==0 && p->showHeader ){
2955 for(i=0; i<nArg; i++){
2956 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
2957 sqlite3_fputs(azCol[i] ? azCol[i] : "", p->out);
2959 sqlite3_fputs(p->rowSeparator, p->out);
2961 if( azArg==0 ) break;
2962 for(i=0; i<nArg; i++){
2963 if( i>0 ) sqlite3_fputs(p->colSeparator, p->out);
2964 sqlite3_fputs(azArg[i] ? azArg[i] : p->nullValue, p->out);
2966 sqlite3_fputs(p->rowSeparator, p->out);
2967 break;
2969 case MODE_EQP: {
2970 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
2971 break;
2974 return 0;
2978 ** This is the callback routine that the SQLite library
2979 ** invokes for each row of a query result.
2981 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2982 /* since we don't have type info, call the shell_callback with a NULL value */
2983 return shell_callback(pArg, nArg, azArg, azCol, NULL);
2987 ** This is the callback routine from sqlite3_exec() that appends all
2988 ** output onto the end of a ShellText object.
2990 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2991 ShellText *p = (ShellText*)pArg;
2992 int i;
2993 UNUSED_PARAMETER(az);
2994 if( azArg==0 ) return 0;
2995 if( p->n ) appendText(p, "|", 0);
2996 for(i=0; i<nArg; i++){
2997 if( i ) appendText(p, ",", 0);
2998 if( azArg[i] ) appendText(p, azArg[i], 0);
3000 return 0;
3004 ** Generate an appropriate SELFTEST table in the main database.
3006 static void createSelftestTable(ShellState *p){
3007 char *zErrMsg = 0;
3008 sqlite3_exec(p->db,
3009 "SAVEPOINT selftest_init;\n"
3010 "CREATE TABLE IF NOT EXISTS selftest(\n"
3011 " tno INTEGER PRIMARY KEY,\n" /* Test number */
3012 " op TEXT,\n" /* Operator: memo run */
3013 " cmd TEXT,\n" /* Command text */
3014 " ans TEXT\n" /* Desired answer */
3015 ");"
3016 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
3017 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
3018 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
3019 " 'memo','Tests generated by --init');\n"
3020 "INSERT INTO [_shell$self]\n"
3021 " SELECT 'run',\n"
3022 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
3023 "FROM sqlite_schema ORDER BY 2'',224))',\n"
3024 " hex(sha3_query('SELECT type,name,tbl_name,sql "
3025 "FROM sqlite_schema ORDER BY 2',224));\n"
3026 "INSERT INTO [_shell$self]\n"
3027 " SELECT 'run',"
3028 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
3029 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
3030 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
3031 " FROM (\n"
3032 " SELECT name FROM sqlite_schema\n"
3033 " WHERE type='table'\n"
3034 " AND name<>'selftest'\n"
3035 " AND coalesce(rootpage,0)>0\n"
3036 " )\n"
3037 " ORDER BY name;\n"
3038 "INSERT INTO [_shell$self]\n"
3039 " VALUES('run','PRAGMA integrity_check','ok');\n"
3040 "INSERT INTO selftest(tno,op,cmd,ans)"
3041 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
3042 "DROP TABLE [_shell$self];"
3043 ,0,0,&zErrMsg);
3044 if( zErrMsg ){
3045 sqlite3_fprintf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
3046 sqlite3_free(zErrMsg);
3048 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
3053 ** Set the destination table field of the ShellState structure to
3054 ** the name of the table given. Escape any quote characters in the
3055 ** table name.
3057 static void set_table_name(ShellState *p, const char *zName){
3058 int i, n;
3059 char cQuote;
3060 char *z;
3062 if( p->zDestTable ){
3063 free(p->zDestTable);
3064 p->zDestTable = 0;
3066 if( zName==0 ) return;
3067 cQuote = quoteChar(zName);
3068 n = strlen30(zName);
3069 if( cQuote ) n += n+2;
3070 z = p->zDestTable = malloc( n+1 );
3071 shell_check_oom(z);
3072 n = 0;
3073 if( cQuote ) z[n++] = cQuote;
3074 for(i=0; zName[i]; i++){
3075 z[n++] = zName[i];
3076 if( zName[i]==cQuote ) z[n++] = cQuote;
3078 if( cQuote ) z[n++] = cQuote;
3079 z[n] = 0;
3083 ** Maybe construct two lines of text that point out the position of a
3084 ** syntax error. Return a pointer to the text, in memory obtained from
3085 ** sqlite3_malloc(). Or, if the most recent error does not involve a
3086 ** specific token that we can point to, return an empty string.
3088 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
3089 ** and should be released by the caller invoking sqlite3_free().
3091 static char *shell_error_context(const char *zSql, sqlite3 *db){
3092 int iOffset;
3093 size_t len;
3094 char *zCode;
3095 char *zMsg;
3096 int i;
3097 if( db==0
3098 || zSql==0
3099 || (iOffset = sqlite3_error_offset(db))<0
3100 || iOffset>=(int)strlen(zSql)
3102 return sqlite3_mprintf("");
3104 while( iOffset>50 ){
3105 iOffset--;
3106 zSql++;
3107 while( (zSql[0]&0xc0)==0x80 ){ zSql++; iOffset--; }
3109 len = strlen(zSql);
3110 if( len>78 ){
3111 len = 78;
3112 while( len>0 && (zSql[len]&0xc0)==0x80 ) len--;
3114 zCode = sqlite3_mprintf("%.*s", len, zSql);
3115 shell_check_oom(zCode);
3116 for(i=0; zCode[i]; i++){ if( IsSpace(zSql[i]) ) zCode[i] = ' '; }
3117 if( iOffset<25 ){
3118 zMsg = sqlite3_mprintf("\n %z\n %*s^--- error here", zCode,iOffset,"");
3119 }else{
3120 zMsg = sqlite3_mprintf("\n %z\n %*serror here ---^", zCode,iOffset-14,"");
3122 return zMsg;
3127 ** Execute a query statement that will generate SQL output. Print
3128 ** the result columns, comma-separated, on a line and then add a
3129 ** semicolon terminator to the end of that line.
3131 ** If the number of columns is 1 and that column contains text "--"
3132 ** then write the semicolon on a separate line. That way, if a
3133 ** "--" comment occurs at the end of the statement, the comment
3134 ** won't consume the semicolon terminator.
3136 static int run_table_dump_query(
3137 ShellState *p, /* Query context */
3138 const char *zSelect /* SELECT statement to extract content */
3140 sqlite3_stmt *pSelect;
3141 int rc;
3142 int nResult;
3143 int i;
3144 const char *z;
3145 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
3146 if( rc!=SQLITE_OK || !pSelect ){
3147 char *zContext = shell_error_context(zSelect, p->db);
3148 sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n%s",
3149 rc, sqlite3_errmsg(p->db), zContext);
3150 sqlite3_free(zContext);
3151 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3152 return rc;
3154 rc = sqlite3_step(pSelect);
3155 nResult = sqlite3_column_count(pSelect);
3156 while( rc==SQLITE_ROW ){
3157 z = (const char*)sqlite3_column_text(pSelect, 0);
3158 sqlite3_fprintf(p->out, "%s", z);
3159 for(i=1; i<nResult; i++){
3160 sqlite3_fprintf(p->out, ",%s", sqlite3_column_text(pSelect, i));
3162 if( z==0 ) z = "";
3163 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
3164 if( z[0] ){
3165 sqlite3_fputs("\n;\n", p->out);
3166 }else{
3167 sqlite3_fputs(";\n", p->out);
3169 rc = sqlite3_step(pSelect);
3171 rc = sqlite3_finalize(pSelect);
3172 if( rc!=SQLITE_OK ){
3173 sqlite3_fprintf(p->out, "/**** ERROR: (%d) %s *****/\n",
3174 rc, sqlite3_errmsg(p->db));
3175 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
3177 return rc;
3181 ** Allocate space and save off string indicating current error.
3183 static char *save_err_msg(
3184 sqlite3 *db, /* Database to query */
3185 const char *zPhase, /* When the error occurs */
3186 int rc, /* Error code returned from API */
3187 const char *zSql /* SQL string, or NULL */
3189 char *zErr;
3190 char *zContext;
3191 sqlite3_str *pStr = sqlite3_str_new(0);
3192 sqlite3_str_appendf(pStr, "%s, %s", zPhase, sqlite3_errmsg(db));
3193 if( rc>1 ){
3194 sqlite3_str_appendf(pStr, " (%d)", rc);
3196 zContext = shell_error_context(zSql, db);
3197 if( zContext ){
3198 sqlite3_str_appendall(pStr, zContext);
3199 sqlite3_free(zContext);
3201 zErr = sqlite3_str_finish(pStr);
3202 shell_check_oom(zErr);
3203 return zErr;
3206 #ifdef __linux__
3208 ** Attempt to display I/O stats on Linux using /proc/PID/io
3210 static void displayLinuxIoStats(FILE *out){
3211 FILE *in;
3212 char z[200];
3213 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
3214 in = sqlite3_fopen(z, "rb");
3215 if( in==0 ) return;
3216 while( sqlite3_fgets(z, sizeof(z), in)!=0 ){
3217 static const struct {
3218 const char *zPattern;
3219 const char *zDesc;
3220 } aTrans[] = {
3221 { "rchar: ", "Bytes received by read():" },
3222 { "wchar: ", "Bytes sent to write():" },
3223 { "syscr: ", "Read() system calls:" },
3224 { "syscw: ", "Write() system calls:" },
3225 { "read_bytes: ", "Bytes read from storage:" },
3226 { "write_bytes: ", "Bytes written to storage:" },
3227 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3229 int i;
3230 for(i=0; i<ArraySize(aTrans); i++){
3231 int n = strlen30(aTrans[i].zPattern);
3232 if( cli_strncmp(aTrans[i].zPattern, z, n)==0 ){
3233 sqlite3_fprintf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
3234 break;
3238 fclose(in);
3240 #endif
3243 ** Display a single line of status using 64-bit values.
3245 static void displayStatLine(
3246 FILE *out, /* Write to this channel */
3247 char *zLabel, /* Label for this one line */
3248 char *zFormat, /* Format for the result */
3249 int iStatusCtrl, /* Which status to display */
3250 int bReset /* True to reset the stats */
3252 sqlite3_int64 iCur = -1;
3253 sqlite3_int64 iHiwtr = -1;
3254 int i, nPercent;
3255 char zLine[200];
3256 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
3257 for(i=0, nPercent=0; zFormat[i]; i++){
3258 if( zFormat[i]=='%' ) nPercent++;
3260 if( nPercent>1 ){
3261 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
3262 }else{
3263 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
3265 sqlite3_fprintf(out, "%-36s %s\n", zLabel, zLine);
3269 ** Display memory stats.
3271 static int display_stats(
3272 sqlite3 *db, /* Database to query */
3273 ShellState *pArg, /* Pointer to ShellState */
3274 int bReset /* True to reset the stats */
3276 int iCur;
3277 int iHiwtr;
3278 FILE *out;
3279 if( pArg==0 || pArg->out==0 ) return 0;
3280 out = pArg->out;
3282 if( pArg->pStmt && pArg->statsOn==2 ){
3283 int nCol, i, x;
3284 sqlite3_stmt *pStmt = pArg->pStmt;
3285 char z[100];
3286 nCol = sqlite3_column_count(pStmt);
3287 sqlite3_fprintf(out, "%-36s %d\n", "Number of output columns:", nCol);
3288 for(i=0; i<nCol; i++){
3289 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
3290 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
3291 #ifndef SQLITE_OMIT_DECLTYPE
3292 sqlite3_snprintf(30, z+x, "declared type:");
3293 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
3294 #endif
3295 #ifdef SQLITE_ENABLE_COLUMN_METADATA
3296 sqlite3_snprintf(30, z+x, "database name:");
3297 sqlite3_fprintf(out, "%-36s %s\n", z,
3298 sqlite3_column_database_name(pStmt,i));
3299 sqlite3_snprintf(30, z+x, "table name:");
3300 sqlite3_fprintf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
3301 sqlite3_snprintf(30, z+x, "origin name:");
3302 sqlite3_fprintf(out, "%-36s %s\n", z,sqlite3_column_origin_name(pStmt,i));
3303 #endif
3307 if( pArg->statsOn==3 ){
3308 if( pArg->pStmt ){
3309 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP,bReset);
3310 sqlite3_fprintf(out, "VM-steps: %d\n", iCur);
3312 return 0;
3315 displayStatLine(out, "Memory Used:",
3316 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
3317 displayStatLine(out, "Number of Outstanding Allocations:",
3318 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
3319 if( pArg->shellFlgs & SHFLG_Pagecache ){
3320 displayStatLine(out, "Number of Pcache Pages Used:",
3321 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
3323 displayStatLine(out, "Number of Pcache Overflow Bytes:",
3324 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
3325 displayStatLine(out, "Largest Allocation:",
3326 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
3327 displayStatLine(out, "Largest Pcache Allocation:",
3328 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
3329 #ifdef YYTRACKMAXSTACKDEPTH
3330 displayStatLine(out, "Deepest Parser Stack:",
3331 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
3332 #endif
3334 if( db ){
3335 if( pArg->shellFlgs & SHFLG_Lookaside ){
3336 iHiwtr = iCur = -1;
3337 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
3338 &iCur, &iHiwtr, bReset);
3339 sqlite3_fprintf(out,
3340 "Lookaside Slots Used: %d (max %d)\n", iCur, iHiwtr);
3341 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
3342 &iCur, &iHiwtr, bReset);
3343 sqlite3_fprintf(out,
3344 "Successful lookaside attempts: %d\n", iHiwtr);
3345 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
3346 &iCur, &iHiwtr, bReset);
3347 sqlite3_fprintf(out,
3348 "Lookaside failures due to size: %d\n", iHiwtr);
3349 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
3350 &iCur, &iHiwtr, bReset);
3351 sqlite3_fprintf(out,
3352 "Lookaside failures due to OOM: %d\n", iHiwtr);
3354 iHiwtr = iCur = -1;
3355 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
3356 sqlite3_fprintf(out,
3357 "Pager Heap Usage: %d bytes\n", iCur);
3358 iHiwtr = iCur = -1;
3359 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
3360 sqlite3_fprintf(out,
3361 "Page cache hits: %d\n", iCur);
3362 iHiwtr = iCur = -1;
3363 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
3364 sqlite3_fprintf(out,
3365 "Page cache misses: %d\n", iCur);
3366 iHiwtr = iCur = -1;
3367 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
3368 sqlite3_fprintf(out,
3369 "Page cache writes: %d\n", iCur);
3370 iHiwtr = iCur = -1;
3371 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
3372 sqlite3_fprintf(out,
3373 "Page cache spills: %d\n", iCur);
3374 iHiwtr = iCur = -1;
3375 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
3376 sqlite3_fprintf(out,
3377 "Schema Heap Usage: %d bytes\n", iCur);
3378 iHiwtr = iCur = -1;
3379 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
3380 sqlite3_fprintf(out,
3381 "Statement Heap/Lookaside Usage: %d bytes\n", iCur);
3384 if( pArg->pStmt ){
3385 int iHit, iMiss;
3386 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
3387 bReset);
3388 sqlite3_fprintf(out,
3389 "Fullscan Steps: %d\n", iCur);
3390 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
3391 sqlite3_fprintf(out,
3392 "Sort Operations: %d\n", iCur);
3393 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
3394 sqlite3_fprintf(out,
3395 "Autoindex Inserts: %d\n", iCur);
3396 iHit = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_HIT,
3397 bReset);
3398 iMiss = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FILTER_MISS,
3399 bReset);
3400 if( iHit || iMiss ){
3401 sqlite3_fprintf(out,
3402 "Bloom filter bypass taken: %d/%d\n", iHit, iHit+iMiss);
3404 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
3405 sqlite3_fprintf(out,
3406 "Virtual Machine Steps: %d\n", iCur);
3407 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
3408 sqlite3_fprintf(out,
3409 "Reprepare operations: %d\n", iCur);
3410 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
3411 sqlite3_fprintf(out,
3412 "Number of times run: %d\n", iCur);
3413 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
3414 sqlite3_fprintf(out,
3415 "Memory used by prepared stmt: %d\n", iCur);
3418 #ifdef __linux__
3419 displayLinuxIoStats(pArg->out);
3420 #endif
3422 /* Do not remove this machine readable comment: extra-stats-output-here */
3424 return 0;
3428 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3429 static int scanStatsHeight(sqlite3_stmt *p, int iEntry){
3430 int iPid = 0;
3431 int ret = 1;
3432 sqlite3_stmt_scanstatus_v2(p, iEntry,
3433 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3435 while( iPid!=0 ){
3436 int ii;
3437 for(ii=0; 1; ii++){
3438 int iId;
3439 int res;
3440 res = sqlite3_stmt_scanstatus_v2(p, ii,
3441 SQLITE_SCANSTAT_SELECTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iId
3443 if( res ) break;
3444 if( iId==iPid ){
3445 sqlite3_stmt_scanstatus_v2(p, ii,
3446 SQLITE_SCANSTAT_PARENTID, SQLITE_SCANSTAT_COMPLEX, (void*)&iPid
3450 ret++;
3452 return ret;
3454 #endif
3456 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3457 static void display_explain_scanstats(
3458 sqlite3 *db, /* Database to query */
3459 ShellState *pArg /* Pointer to ShellState */
3461 static const int f = SQLITE_SCANSTAT_COMPLEX;
3462 sqlite3_stmt *p = pArg->pStmt;
3463 int ii = 0;
3464 i64 nTotal = 0;
3465 int nWidth = 0;
3466 eqp_reset(pArg);
3468 for(ii=0; 1; ii++){
3469 const char *z = 0;
3470 int n = 0;
3471 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&z) ){
3472 break;
3474 n = (int)strlen(z) + scanStatsHeight(p, ii)*3;
3475 if( n>nWidth ) nWidth = n;
3477 nWidth += 4;
3479 sqlite3_stmt_scanstatus_v2(p, -1, SQLITE_SCANSTAT_NCYCLE, f, (void*)&nTotal);
3480 for(ii=0; 1; ii++){
3481 i64 nLoop = 0;
3482 i64 nRow = 0;
3483 i64 nCycle = 0;
3484 int iId = 0;
3485 int iPid = 0;
3486 const char *zo = 0;
3487 const char *zName = 0;
3488 char *zText = 0;
3489 double rEst = 0.0;
3491 if( sqlite3_stmt_scanstatus_v2(p,ii,SQLITE_SCANSTAT_EXPLAIN,f,(void*)&zo) ){
3492 break;
3494 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_EST,f,(void*)&rEst);
3495 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NLOOP,f,(void*)&nLoop);
3496 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NVISIT,f,(void*)&nRow);
3497 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NCYCLE,f,(void*)&nCycle);
3498 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_SELECTID,f,(void*)&iId);
3499 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_PARENTID,f,(void*)&iPid);
3500 sqlite3_stmt_scanstatus_v2(p, ii, SQLITE_SCANSTAT_NAME,f,(void*)&zName);
3502 zText = sqlite3_mprintf("%s", zo);
3503 if( nCycle>=0 || nLoop>=0 || nRow>=0 ){
3504 char *z = 0;
3505 if( nCycle>=0 && nTotal>0 ){
3506 z = sqlite3_mprintf("%zcycles=%lld [%d%%]", z,
3507 nCycle, ((nCycle*100)+nTotal/2) / nTotal
3510 if( nLoop>=0 ){
3511 z = sqlite3_mprintf("%z%sloops=%lld", z, z ? " " : "", nLoop);
3513 if( nRow>=0 ){
3514 z = sqlite3_mprintf("%z%srows=%lld", z, z ? " " : "", nRow);
3517 if( zName && pArg->scanstatsOn>1 ){
3518 double rpl = (double)nRow / (double)nLoop;
3519 z = sqlite3_mprintf("%z rpl=%.1f est=%.1f", z, rpl, rEst);
3522 zText = sqlite3_mprintf(
3523 "% *z (%z)", -1*(nWidth-scanStatsHeight(p, ii)*3), zText, z
3527 eqp_append(pArg, iId, iPid, zText);
3528 sqlite3_free(zText);
3531 eqp_render(pArg, nTotal);
3533 #endif
3537 ** Parameter azArray points to a zero-terminated array of strings. zStr
3538 ** points to a single nul-terminated string. Return non-zero if zStr
3539 ** is equal, according to strcmp(), to any of the strings in the array.
3540 ** Otherwise, return zero.
3542 static int str_in_array(const char *zStr, const char **azArray){
3543 int i;
3544 for(i=0; azArray[i]; i++){
3545 if( 0==cli_strcmp(zStr, azArray[i]) ) return 1;
3547 return 0;
3551 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3552 ** and populate the ShellState.aiIndent[] array with the number of
3553 ** spaces each opcode should be indented before it is output.
3555 ** The indenting rules are:
3557 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3558 ** all opcodes that occur between the p2 jump destination and the opcode
3559 ** itself by 2 spaces.
3561 ** * Do the previous for "Return" instructions for when P2 is positive.
3562 ** See tag-20220407a in wherecode.c and vdbe.c.
3564 ** * For each "Goto", if the jump destination is earlier in the program
3565 ** and ends on one of:
3566 ** Yield SeekGt SeekLt RowSetRead Rewind
3567 ** or if the P1 parameter is one instead of zero,
3568 ** then indent all opcodes between the earlier instruction
3569 ** and "Goto" by 2 spaces.
3571 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
3572 int *abYield = 0; /* True if op is an OP_Yield */
3573 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
3574 int iOp; /* Index of operation in p->aiIndent[] */
3576 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3577 "Return", 0 };
3578 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3579 "Rewind", 0 };
3580 const char *azGoto[] = { "Goto", 0 };
3582 /* The caller guarantees that the leftmost 4 columns of the statement
3583 ** passed to this function are equivalent to the leftmost 4 columns
3584 ** of EXPLAIN statement output. In practice the statement may be
3585 ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
3586 assert( sqlite3_column_count(pSql)>=4 );
3587 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 0), "addr" ) );
3588 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 1), "opcode" ) );
3589 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 2), "p1" ) );
3590 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql, 3), "p2" ) );
3592 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
3593 int i;
3594 int iAddr = sqlite3_column_int(pSql, 0);
3595 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
3596 int p1 = sqlite3_column_int(pSql, 2);
3597 int p2 = sqlite3_column_int(pSql, 3);
3599 /* Assuming that p2 is an instruction address, set variable p2op to the
3600 ** index of that instruction in the aiIndent[] array. p2 and p2op may be
3601 ** different if the current instruction is part of a sub-program generated
3602 ** by an SQL trigger or foreign key. */
3603 int p2op = (p2 + (iOp-iAddr));
3605 /* Grow the p->aiIndent array as required */
3606 if( iOp>=nAlloc ){
3607 nAlloc += 100;
3608 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
3609 shell_check_oom(p->aiIndent);
3610 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
3611 shell_check_oom(abYield);
3614 abYield[iOp] = str_in_array(zOp, azYield);
3615 p->aiIndent[iOp] = 0;
3616 p->nIndent = iOp+1;
3617 if( str_in_array(zOp, azNext) && p2op>0 ){
3618 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3620 if( str_in_array(zOp, azGoto) && p2op<iOp && (abYield[p2op] || p1) ){
3621 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
3625 p->iIndent = 0;
3626 sqlite3_free(abYield);
3627 sqlite3_reset(pSql);
3631 ** Free the array allocated by explain_data_prepare().
3633 static void explain_data_delete(ShellState *p){
3634 sqlite3_free(p->aiIndent);
3635 p->aiIndent = 0;
3636 p->nIndent = 0;
3637 p->iIndent = 0;
3640 static void exec_prepared_stmt(ShellState*, sqlite3_stmt*);
3643 ** Display scan stats.
3645 static void display_scanstats(
3646 sqlite3 *db, /* Database to query */
3647 ShellState *pArg /* Pointer to ShellState */
3649 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3650 UNUSED_PARAMETER(db);
3651 UNUSED_PARAMETER(pArg);
3652 #else
3653 if( pArg->scanstatsOn==3 ){
3654 const char *zSql =
3655 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
3656 " format('% 6s (%.2f%%)',"
3657 " CASE WHEN ncycle<100_000 THEN ncycle || ' '"
3658 " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'"
3659 " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'"
3660 " ELSE (ncycle/1000_000_000) || 'G' END,"
3661 " ncycle*100.0/(sum(ncycle) OVER ())"
3662 " ) AS cycles"
3663 " FROM bytecode(?)";
3665 int rc = SQLITE_OK;
3666 sqlite3_stmt *pStmt = 0;
3667 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3668 if( rc==SQLITE_OK ){
3669 sqlite3_stmt *pSave = pArg->pStmt;
3670 pArg->pStmt = pStmt;
3671 sqlite3_bind_pointer(pStmt, 1, pSave, "stmt-pointer", 0);
3673 pArg->cnt = 0;
3674 pArg->cMode = MODE_ScanExp;
3675 explain_data_prepare(pArg, pStmt);
3676 exec_prepared_stmt(pArg, pStmt);
3677 explain_data_delete(pArg);
3679 sqlite3_finalize(pStmt);
3680 pArg->pStmt = pSave;
3682 }else{
3683 display_explain_scanstats(db, pArg);
3685 #endif
3689 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3691 static unsigned int savedSelectTrace;
3692 static unsigned int savedWhereTrace;
3693 static void disable_debug_trace_modes(void){
3694 unsigned int zero = 0;
3695 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
3696 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
3697 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
3698 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
3700 static void restore_debug_trace_modes(void){
3701 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
3702 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
3705 /* Create the TEMP table used to store parameter bindings */
3706 static void bind_table_init(ShellState *p){
3707 int wrSchema = 0;
3708 int defensiveMode = 0;
3709 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
3710 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
3711 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
3712 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
3713 sqlite3_exec(p->db,
3714 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3715 " key TEXT PRIMARY KEY,\n"
3716 " value\n"
3717 ") WITHOUT ROWID;",
3718 0, 0, 0);
3719 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
3720 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
3724 ** Bind parameters on a prepared statement.
3726 ** Parameter bindings are taken from a TEMP table of the form:
3728 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3729 ** WITHOUT ROWID;
3731 ** No bindings occur if this table does not exist. The name of the table
3732 ** begins with "sqlite_" so that it will not collide with ordinary application
3733 ** tables. The table must be in the TEMP schema.
3735 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
3736 int nVar;
3737 int i;
3738 int rc;
3739 sqlite3_stmt *pQ = 0;
3741 nVar = sqlite3_bind_parameter_count(pStmt);
3742 if( nVar==0 ) return; /* Nothing to do */
3743 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
3744 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
3745 rc = SQLITE_NOTFOUND;
3746 pQ = 0;
3747 }else{
3748 rc = sqlite3_prepare_v2(pArg->db,
3749 "SELECT value FROM temp.sqlite_parameters"
3750 " WHERE key=?1", -1, &pQ, 0);
3752 for(i=1; i<=nVar; i++){
3753 char zNum[30];
3754 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
3755 if( zVar==0 ){
3756 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
3757 zVar = zNum;
3759 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
3760 if( rc==SQLITE_OK && pQ && sqlite3_step(pQ)==SQLITE_ROW ){
3761 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
3762 #ifdef NAN
3763 }else if( sqlite3_strlike("_NAN", zVar, 0)==0 ){
3764 sqlite3_bind_double(pStmt, i, NAN);
3765 #endif
3766 #ifdef INFINITY
3767 }else if( sqlite3_strlike("_INF", zVar, 0)==0 ){
3768 sqlite3_bind_double(pStmt, i, INFINITY);
3769 #endif
3770 }else if( strncmp(zVar, "$int_", 5)==0 ){
3771 sqlite3_bind_int(pStmt, i, atoi(&zVar[5]));
3772 }else if( strncmp(zVar, "$text_", 6)==0 ){
3773 size_t szVar = strlen(zVar);
3774 char *zBuf = sqlite3_malloc64( szVar-5 );
3775 if( zBuf ){
3776 memcpy(zBuf, &zVar[6], szVar-5);
3777 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8);
3779 }else{
3780 sqlite3_bind_null(pStmt, i);
3782 sqlite3_reset(pQ);
3784 sqlite3_finalize(pQ);
3788 ** UTF8 box-drawing characters. Imagine box lines like this:
3790 ** 1
3791 ** |
3792 ** 4 --+-- 2
3793 ** |
3794 ** 3
3796 ** Each box characters has between 2 and 4 of the lines leading from
3797 ** the center. The characters are here identified by the numbers of
3798 ** their corresponding lines.
3800 #define BOX_24 "\342\224\200" /* U+2500 --- */
3801 #define BOX_13 "\342\224\202" /* U+2502 | */
3802 #define BOX_23 "\342\224\214" /* U+250c ,- */
3803 #define BOX_34 "\342\224\220" /* U+2510 -, */
3804 #define BOX_12 "\342\224\224" /* U+2514 '- */
3805 #define BOX_14 "\342\224\230" /* U+2518 -' */
3806 #define BOX_123 "\342\224\234" /* U+251c |- */
3807 #define BOX_134 "\342\224\244" /* U+2524 -| */
3808 #define BOX_234 "\342\224\254" /* U+252c -,- */
3809 #define BOX_124 "\342\224\264" /* U+2534 -'- */
3810 #define BOX_1234 "\342\224\274" /* U+253c -|- */
3812 /* Draw horizontal line N characters long using unicode box
3813 ** characters
3815 static void print_box_line(FILE *out, int N){
3816 const char zDash[] =
3817 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3818 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
3819 const int nDash = sizeof(zDash) - 1;
3820 N *= 3;
3821 while( N>nDash ){
3822 sqlite3_fputs(zDash, out);
3823 N -= nDash;
3825 sqlite3_fprintf(out, "%.*s", N, zDash);
3829 ** Draw a horizontal separator for a MODE_Box table.
3831 static void print_box_row_separator(
3832 ShellState *p,
3833 int nArg,
3834 const char *zSep1,
3835 const char *zSep2,
3836 const char *zSep3
3838 int i;
3839 if( nArg>0 ){
3840 sqlite3_fputs(zSep1, p->out);
3841 print_box_line(p->out, p->actualWidth[0]+2);
3842 for(i=1; i<nArg; i++){
3843 sqlite3_fputs(zSep2, p->out);
3844 print_box_line(p->out, p->actualWidth[i]+2);
3846 sqlite3_fputs(zSep3, p->out);
3848 sqlite3_fputs("\n", p->out);
3852 ** z[] is a line of text that is to be displayed the .mode box or table or
3853 ** similar tabular formats. z[] might contain control characters such
3854 ** as \n, \t, \f, or \r.
3856 ** Compute characters to display on the first line of z[]. Stop at the
3857 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
3858 ** from malloc()) of that first line, which caller should free sometime.
3859 ** Write anything to display on the next line into *pzTail. If this is
3860 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3862 static char *translateForDisplayAndDup(
3863 const unsigned char *z, /* Input text to be transformed */
3864 const unsigned char **pzTail, /* OUT: Tail of the input for next line */
3865 int mxWidth, /* Max width. 0 means no limit */
3866 u8 bWordWrap /* If true, avoid breaking mid-word */
3868 int i; /* Input bytes consumed */
3869 int j; /* Output bytes generated */
3870 int k; /* Input bytes to be displayed */
3871 int n; /* Output column number */
3872 unsigned char *zOut; /* Output text */
3874 if( z==0 ){
3875 *pzTail = 0;
3876 return 0;
3878 if( mxWidth<0 ) mxWidth = -mxWidth;
3879 if( mxWidth==0 ) mxWidth = 1000000;
3880 i = j = n = 0;
3881 while( n<mxWidth ){
3882 unsigned char c = z[i];
3883 if( c>=0xc0 ){
3884 int u;
3885 int len = decodeUtf8(&z[i], &u);
3886 i += len;
3887 j += len;
3888 n += cli_wcwidth(u);
3889 continue;
3891 if( c>=' ' ){
3892 n++;
3893 i++;
3894 j++;
3895 continue;
3897 if( c=='\t' ){
3899 n++;
3900 j++;
3901 }while( (n&7)!=0 && n<mxWidth );
3902 i++;
3903 continue;
3905 break;
3907 if( n>=mxWidth && bWordWrap ){
3908 /* Perhaps try to back up to a better place to break the line */
3909 for(k=i; k>i/2; k--){
3910 if( isspace(z[k-1]) ) break;
3912 if( k<=i/2 ){
3913 for(k=i; k>i/2; k--){
3914 if( isalnum(z[k-1])!=isalnum(z[k]) && (z[k]&0xc0)!=0x80 ) break;
3917 if( k<=i/2 ){
3918 k = i;
3919 }else{
3920 i = k;
3921 while( z[i]==' ' ) i++;
3923 }else{
3924 k = i;
3926 if( n>=mxWidth && z[i]>=' ' ){
3927 *pzTail = &z[i];
3928 }else if( z[i]=='\r' && z[i+1]=='\n' ){
3929 *pzTail = z[i+2] ? &z[i+2] : 0;
3930 }else if( z[i]==0 || z[i+1]==0 ){
3931 *pzTail = 0;
3932 }else{
3933 *pzTail = &z[i+1];
3935 zOut = malloc( j+1 );
3936 shell_check_oom(zOut);
3937 i = j = n = 0;
3938 while( i<k ){
3939 unsigned char c = z[i];
3940 if( c>=0xc0 ){
3941 int u;
3942 int len = decodeUtf8(&z[i], &u);
3943 do{ zOut[j++] = z[i++]; }while( (--len)>0 );
3944 n += cli_wcwidth(u);
3945 continue;
3947 if( c>=' ' ){
3948 n++;
3949 zOut[j++] = z[i++];
3950 continue;
3952 if( z[i]=='\t' ){
3954 n++;
3955 zOut[j++] = ' ';
3956 }while( (n&7)!=0 && n<mxWidth );
3957 i++;
3958 continue;
3960 break;
3962 zOut[j] = 0;
3963 return (char*)zOut;
3966 /* Extract the value of the i-th current column for pStmt as an SQL literal
3967 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
3968 ** the caller.
3970 static char *quoted_column(sqlite3_stmt *pStmt, int i){
3971 switch( sqlite3_column_type(pStmt, i) ){
3972 case SQLITE_NULL: {
3973 return sqlite3_mprintf("NULL");
3975 case SQLITE_INTEGER:
3976 case SQLITE_FLOAT: {
3977 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt,i));
3979 case SQLITE_TEXT: {
3980 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt,i));
3982 case SQLITE_BLOB: {
3983 int j;
3984 sqlite3_str *pStr = sqlite3_str_new(0);
3985 const unsigned char *a = sqlite3_column_blob(pStmt,i);
3986 int n = sqlite3_column_bytes(pStmt,i);
3987 sqlite3_str_append(pStr, "x'", 2);
3988 for(j=0; j<n; j++){
3989 sqlite3_str_appendf(pStr, "%02x", a[j]);
3991 sqlite3_str_append(pStr, "'", 1);
3992 return sqlite3_str_finish(pStr);
3995 return 0; /* Not reached */
3999 ** Run a prepared statement and output the result in one of the
4000 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
4001 ** or MODE_Box.
4003 ** This is different from ordinary exec_prepared_stmt() in that
4004 ** it has to run the entire query and gather the results into memory
4005 ** first, in order to determine column widths, before providing
4006 ** any output.
4008 static void exec_prepared_stmt_columnar(
4009 ShellState *p, /* Pointer to ShellState */
4010 sqlite3_stmt *pStmt /* Statement to run */
4012 sqlite3_int64 nRow = 0;
4013 int nColumn = 0;
4014 char **azData = 0;
4015 sqlite3_int64 nAlloc = 0;
4016 char *abRowDiv = 0;
4017 const unsigned char *uz;
4018 const char *z;
4019 char **azQuoted = 0;
4020 int rc;
4021 sqlite3_int64 i, nData;
4022 int j, nTotal, w, n;
4023 const char *colSep = 0;
4024 const char *rowSep = 0;
4025 const unsigned char **azNextLine = 0;
4026 int bNextLine = 0;
4027 int bMultiLineRowExists = 0;
4028 int bw = p->cmOpts.bWordWrap;
4029 const char *zEmpty = "";
4030 const char *zShowNull = p->nullValue;
4032 rc = sqlite3_step(pStmt);
4033 if( rc!=SQLITE_ROW ) return;
4034 nColumn = sqlite3_column_count(pStmt);
4035 if( nColumn==0 ) goto columnar_end;
4036 nAlloc = nColumn*4;
4037 if( nAlloc<=0 ) nAlloc = 1;
4038 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
4039 shell_check_oom(azData);
4040 azNextLine = sqlite3_malloc64( nColumn*sizeof(char*) );
4041 shell_check_oom(azNextLine);
4042 memset((void*)azNextLine, 0, nColumn*sizeof(char*) );
4043 if( p->cmOpts.bQuote ){
4044 azQuoted = sqlite3_malloc64( nColumn*sizeof(char*) );
4045 shell_check_oom(azQuoted);
4046 memset(azQuoted, 0, nColumn*sizeof(char*) );
4048 abRowDiv = sqlite3_malloc64( nAlloc/nColumn );
4049 shell_check_oom(abRowDiv);
4050 if( nColumn>p->nWidth ){
4051 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
4052 shell_check_oom(p->colWidth);
4053 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
4054 p->nWidth = nColumn;
4055 p->actualWidth = &p->colWidth[nColumn];
4057 memset(p->actualWidth, 0, nColumn*sizeof(int));
4058 for(i=0; i<nColumn; i++){
4059 w = p->colWidth[i];
4060 if( w<0 ) w = -w;
4061 p->actualWidth[i] = w;
4063 for(i=0; i<nColumn; i++){
4064 const unsigned char *zNotUsed;
4065 int wx = p->colWidth[i];
4066 if( wx==0 ){
4067 wx = p->cmOpts.iWrap;
4069 if( wx<0 ) wx = -wx;
4070 uz = (const unsigned char*)sqlite3_column_name(pStmt,i);
4071 if( uz==0 ) uz = (u8*)"";
4072 azData[i] = translateForDisplayAndDup(uz, &zNotUsed, wx, bw);
4075 int useNextLine = bNextLine;
4076 bNextLine = 0;
4077 if( (nRow+2)*nColumn >= nAlloc ){
4078 nAlloc *= 2;
4079 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
4080 shell_check_oom(azData);
4081 abRowDiv = sqlite3_realloc64(abRowDiv, nAlloc/nColumn);
4082 shell_check_oom(abRowDiv);
4084 abRowDiv[nRow] = 1;
4085 nRow++;
4086 for(i=0; i<nColumn; i++){
4087 int wx = p->colWidth[i];
4088 if( wx==0 ){
4089 wx = p->cmOpts.iWrap;
4091 if( wx<0 ) wx = -wx;
4092 if( useNextLine ){
4093 uz = azNextLine[i];
4094 if( uz==0 ) uz = (u8*)zEmpty;
4095 }else if( p->cmOpts.bQuote ){
4096 sqlite3_free(azQuoted[i]);
4097 azQuoted[i] = quoted_column(pStmt,i);
4098 uz = (const unsigned char*)azQuoted[i];
4099 }else{
4100 uz = (const unsigned char*)sqlite3_column_text(pStmt,i);
4101 if( uz==0 ) uz = (u8*)zShowNull;
4103 azData[nRow*nColumn + i]
4104 = translateForDisplayAndDup(uz, &azNextLine[i], wx, bw);
4105 if( azNextLine[i] ){
4106 bNextLine = 1;
4107 abRowDiv[nRow-1] = 0;
4108 bMultiLineRowExists = 1;
4111 }while( bNextLine || sqlite3_step(pStmt)==SQLITE_ROW );
4112 nTotal = nColumn*(nRow+1);
4113 for(i=0; i<nTotal; i++){
4114 z = azData[i];
4115 if( z==0 ) z = (char*)zEmpty;
4116 n = strlenChar(z);
4117 j = i%nColumn;
4118 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
4120 if( seenInterrupt ) goto columnar_end;
4121 switch( p->cMode ){
4122 case MODE_Column: {
4123 colSep = " ";
4124 rowSep = "\n";
4125 if( p->showHeader ){
4126 for(i=0; i<nColumn; i++){
4127 w = p->actualWidth[i];
4128 if( p->colWidth[i]<0 ) w = -w;
4129 utf8_width_print(p->out, w, azData[i]);
4130 sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
4132 for(i=0; i<nColumn; i++){
4133 print_dashes(p->out, p->actualWidth[i]);
4134 sqlite3_fputs(i==nColumn-1?"\n":" ", p->out);
4137 break;
4139 case MODE_Table: {
4140 colSep = " | ";
4141 rowSep = " |\n";
4142 print_row_separator(p, nColumn, "+");
4143 sqlite3_fputs("| ", p->out);
4144 for(i=0; i<nColumn; i++){
4145 w = p->actualWidth[i];
4146 n = strlenChar(azData[i]);
4147 sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
4148 azData[i], (w-n+1)/2, "");
4149 sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
4151 print_row_separator(p, nColumn, "+");
4152 break;
4154 case MODE_Markdown: {
4155 colSep = " | ";
4156 rowSep = " |\n";
4157 sqlite3_fputs("| ", p->out);
4158 for(i=0; i<nColumn; i++){
4159 w = p->actualWidth[i];
4160 n = strlenChar(azData[i]);
4161 sqlite3_fprintf(p->out, "%*s%s%*s", (w-n)/2, "",
4162 azData[i], (w-n+1)/2, "");
4163 sqlite3_fputs(i==nColumn-1?" |\n":" | ", p->out);
4165 print_row_separator(p, nColumn, "|");
4166 break;
4168 case MODE_Box: {
4169 colSep = " " BOX_13 " ";
4170 rowSep = " " BOX_13 "\n";
4171 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
4172 sqlite3_fputs(BOX_13 " ", p->out);
4173 for(i=0; i<nColumn; i++){
4174 w = p->actualWidth[i];
4175 n = strlenChar(azData[i]);
4176 sqlite3_fprintf(p->out, "%*s%s%*s%s",
4177 (w-n)/2, "", azData[i], (w-n+1)/2, "",
4178 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
4180 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
4181 break;
4184 for(i=nColumn, j=0; i<nTotal; i++, j++){
4185 if( j==0 && p->cMode!=MODE_Column ){
4186 sqlite3_fputs(p->cMode==MODE_Box?BOX_13" ":"| ", p->out);
4188 z = azData[i];
4189 if( z==0 ) z = p->nullValue;
4190 w = p->actualWidth[j];
4191 if( p->colWidth[j]<0 ) w = -w;
4192 utf8_width_print(p->out, w, z);
4193 if( j==nColumn-1 ){
4194 sqlite3_fputs(rowSep, p->out);
4195 if( bMultiLineRowExists && abRowDiv[i/nColumn-1] && i+1<nTotal ){
4196 if( p->cMode==MODE_Table ){
4197 print_row_separator(p, nColumn, "+");
4198 }else if( p->cMode==MODE_Box ){
4199 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
4200 }else if( p->cMode==MODE_Column ){
4201 sqlite3_fputs("\n", p->out);
4204 j = -1;
4205 if( seenInterrupt ) goto columnar_end;
4206 }else{
4207 sqlite3_fputs(colSep, p->out);
4210 if( p->cMode==MODE_Table ){
4211 print_row_separator(p, nColumn, "+");
4212 }else if( p->cMode==MODE_Box ){
4213 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
4215 columnar_end:
4216 if( seenInterrupt ){
4217 sqlite3_fputs("Interrupt\n", p->out);
4219 nData = (nRow+1)*nColumn;
4220 for(i=0; i<nData; i++){
4221 z = azData[i];
4222 if( z!=zEmpty && z!=zShowNull ) free(azData[i]);
4224 sqlite3_free(azData);
4225 sqlite3_free((void*)azNextLine);
4226 sqlite3_free(abRowDiv);
4227 if( azQuoted ){
4228 for(i=0; i<nColumn; i++) sqlite3_free(azQuoted[i]);
4229 sqlite3_free(azQuoted);
4234 ** Run a prepared statement
4236 static void exec_prepared_stmt(
4237 ShellState *pArg, /* Pointer to ShellState */
4238 sqlite3_stmt *pStmt /* Statement to run */
4240 int rc;
4241 sqlite3_uint64 nRow = 0;
4243 if( pArg->cMode==MODE_Column
4244 || pArg->cMode==MODE_Table
4245 || pArg->cMode==MODE_Box
4246 || pArg->cMode==MODE_Markdown
4248 exec_prepared_stmt_columnar(pArg, pStmt);
4249 return;
4252 /* perform the first step. this will tell us if we
4253 ** have a result set or not and how wide it is.
4255 rc = sqlite3_step(pStmt);
4256 /* if we have a result set... */
4257 if( SQLITE_ROW == rc ){
4258 /* allocate space for col name ptr, value ptr, and type */
4259 int nCol = sqlite3_column_count(pStmt);
4260 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
4261 if( !pData ){
4262 shell_out_of_memory();
4263 }else{
4264 char **azCols = (char **)pData; /* Names of result columns */
4265 char **azVals = &azCols[nCol]; /* Results */
4266 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
4267 int i, x;
4268 assert(sizeof(int) <= sizeof(char *));
4269 /* save off ptrs to column names */
4270 for(i=0; i<nCol; i++){
4271 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
4274 nRow++;
4275 /* extract the data and data types */
4276 for(i=0; i<nCol; i++){
4277 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
4278 if( x==SQLITE_BLOB
4279 && pArg
4280 && (pArg->cMode==MODE_Insert || pArg->cMode==MODE_Quote)
4282 azVals[i] = "";
4283 }else{
4284 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
4286 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
4287 rc = SQLITE_NOMEM;
4288 break; /* from for */
4290 } /* end for */
4292 /* if data and types extracted successfully... */
4293 if( SQLITE_ROW == rc ){
4294 /* call the supplied callback with the result row data */
4295 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
4296 rc = SQLITE_ABORT;
4297 }else{
4298 rc = sqlite3_step(pStmt);
4301 } while( SQLITE_ROW == rc );
4302 sqlite3_free(pData);
4303 if( pArg->cMode==MODE_Json ){
4304 sqlite3_fputs("]\n", pArg->out);
4305 }else if( pArg->cMode==MODE_Www ){
4306 sqlite3_fputs("</TABLE>\n<PRE>\n", pArg->out);
4307 }else if( pArg->cMode==MODE_Count ){
4308 char zBuf[200];
4309 sqlite3_snprintf(sizeof(zBuf), zBuf, "%llu row%s\n",
4310 nRow, nRow!=1 ? "s" : "");
4311 printf("%s", zBuf);
4317 #ifndef SQLITE_OMIT_VIRTUALTABLE
4319 ** This function is called to process SQL if the previous shell command
4320 ** was ".expert". It passes the SQL in the second argument directly to
4321 ** the sqlite3expert object.
4323 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4324 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4325 ** an English language error message. It is the responsibility of the
4326 ** caller to eventually free this buffer using sqlite3_free().
4328 static int expertHandleSQL(
4329 ShellState *pState,
4330 const char *zSql,
4331 char **pzErr
4333 assert( pState->expert.pExpert );
4334 assert( pzErr==0 || *pzErr==0 );
4335 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
4339 ** This function is called either to silently clean up the object
4340 ** created by the ".expert" command (if bCancel==1), or to generate a
4341 ** report from it and then clean it up (if bCancel==0).
4343 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4344 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4345 ** an English language error message. It is the responsibility of the
4346 ** caller to eventually free this buffer using sqlite3_free().
4348 static int expertFinish(
4349 ShellState *pState,
4350 int bCancel,
4351 char **pzErr
4353 int rc = SQLITE_OK;
4354 sqlite3expert *p = pState->expert.pExpert;
4355 FILE *out = pState->out;
4356 assert( p );
4357 assert( bCancel || pzErr==0 || *pzErr==0 );
4358 if( bCancel==0 ){
4359 int bVerbose = pState->expert.bVerbose;
4361 rc = sqlite3_expert_analyze(p, pzErr);
4362 if( rc==SQLITE_OK ){
4363 int nQuery = sqlite3_expert_count(p);
4364 int i;
4366 if( bVerbose ){
4367 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
4368 sqlite3_fputs("-- Candidates -----------------------------\n", out);
4369 sqlite3_fprintf(out, "%s\n", zCand);
4371 for(i=0; i<nQuery; i++){
4372 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
4373 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
4374 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
4375 if( zIdx==0 ) zIdx = "(no new indexes)\n";
4376 if( bVerbose ){
4377 sqlite3_fprintf(out,
4378 "-- Query %d --------------------------------\n"
4379 "%s\n\n"
4380 ,i+1, zSql);
4382 sqlite3_fprintf(out, "%s\n%s\n", zIdx, zEQP);
4386 sqlite3_expert_destroy(p);
4387 pState->expert.pExpert = 0;
4388 return rc;
4392 ** Implementation of ".expert" dot command.
4394 static int expertDotCommand(
4395 ShellState *pState, /* Current shell tool state */
4396 char **azArg, /* Array of arguments passed to dot command */
4397 int nArg /* Number of entries in azArg[] */
4399 int rc = SQLITE_OK;
4400 char *zErr = 0;
4401 int i;
4402 int iSample = 0;
4404 assert( pState->expert.pExpert==0 );
4405 memset(&pState->expert, 0, sizeof(ExpertInfo));
4407 for(i=1; rc==SQLITE_OK && i<nArg; i++){
4408 char *z = azArg[i];
4409 int n;
4410 if( z[0]=='-' && z[1]=='-' ) z++;
4411 n = strlen30(z);
4412 if( n>=2 && 0==cli_strncmp(z, "-verbose", n) ){
4413 pState->expert.bVerbose = 1;
4415 else if( n>=2 && 0==cli_strncmp(z, "-sample", n) ){
4416 if( i==(nArg-1) ){
4417 sqlite3_fprintf(stderr, "option requires an argument: %s\n", z);
4418 rc = SQLITE_ERROR;
4419 }else{
4420 iSample = (int)integerValue(azArg[++i]);
4421 if( iSample<0 || iSample>100 ){
4422 sqlite3_fprintf(stderr,"value out of range: %s\n", azArg[i]);
4423 rc = SQLITE_ERROR;
4427 else{
4428 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
4429 rc = SQLITE_ERROR;
4433 if( rc==SQLITE_OK ){
4434 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
4435 if( pState->expert.pExpert==0 ){
4436 sqlite3_fprintf(stderr,
4437 "sqlite3_expert_new: %s\n", zErr ? zErr : "out of memory");
4438 rc = SQLITE_ERROR;
4439 }else{
4440 sqlite3_expert_config(
4441 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
4445 sqlite3_free(zErr);
4447 return rc;
4449 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
4452 ** Execute a statement or set of statements. Print
4453 ** any result rows/columns depending on the current mode
4454 ** set via the supplied callback.
4456 ** This is very similar to SQLite's built-in sqlite3_exec()
4457 ** function except it takes a slightly different callback
4458 ** and callback data argument.
4460 static int shell_exec(
4461 ShellState *pArg, /* Pointer to ShellState */
4462 const char *zSql, /* SQL to be evaluated */
4463 char **pzErrMsg /* Error msg written here */
4465 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
4466 int rc = SQLITE_OK; /* Return Code */
4467 int rc2;
4468 const char *zLeftover; /* Tail of unprocessed SQL */
4469 sqlite3 *db = pArg->db;
4471 if( pzErrMsg ){
4472 *pzErrMsg = NULL;
4475 #ifndef SQLITE_OMIT_VIRTUALTABLE
4476 if( pArg->expert.pExpert ){
4477 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
4478 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
4480 #endif
4482 while( zSql[0] && (SQLITE_OK == rc) ){
4483 static const char *zStmtSql;
4484 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
4485 if( SQLITE_OK != rc ){
4486 if( pzErrMsg ){
4487 *pzErrMsg = save_err_msg(db, "in prepare", rc, zSql);
4489 }else{
4490 if( !pStmt ){
4491 /* this happens for a comment or white-space */
4492 zSql = zLeftover;
4493 while( IsSpace(zSql[0]) ) zSql++;
4494 continue;
4496 zStmtSql = sqlite3_sql(pStmt);
4497 if( zStmtSql==0 ) zStmtSql = "";
4498 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
4500 /* save off the prepared statement handle and reset row count */
4501 if( pArg ){
4502 pArg->pStmt = pStmt;
4503 pArg->cnt = 0;
4506 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
4507 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
4508 sqlite3_stmt *pExplain;
4509 int triggerEQP = 0;
4510 disable_debug_trace_modes();
4511 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
4512 if( pArg->autoEQP>=AUTOEQP_trigger ){
4513 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
4515 pExplain = pStmt;
4516 sqlite3_reset(pExplain);
4517 rc = sqlite3_stmt_explain(pExplain, 2);
4518 if( rc==SQLITE_OK ){
4519 bind_prepared_stmt(pArg, pExplain);
4520 while( sqlite3_step(pExplain)==SQLITE_ROW ){
4521 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
4522 int iEqpId = sqlite3_column_int(pExplain, 0);
4523 int iParentId = sqlite3_column_int(pExplain, 1);
4524 if( zEQPLine==0 ) zEQPLine = "";
4525 if( zEQPLine[0]=='-' ) eqp_render(pArg, 0);
4526 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
4528 eqp_render(pArg, 0);
4530 if( pArg->autoEQP>=AUTOEQP_full ){
4531 /* Also do an EXPLAIN for ".eqp full" mode */
4532 sqlite3_reset(pExplain);
4533 rc = sqlite3_stmt_explain(pExplain, 1);
4534 if( rc==SQLITE_OK ){
4535 pArg->cMode = MODE_Explain;
4536 assert( sqlite3_stmt_isexplain(pExplain)==1 );
4537 bind_prepared_stmt(pArg, pExplain);
4538 explain_data_prepare(pArg, pExplain);
4539 exec_prepared_stmt(pArg, pExplain);
4540 explain_data_delete(pArg);
4543 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
4544 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
4546 sqlite3_reset(pStmt);
4547 sqlite3_stmt_explain(pStmt, 0);
4548 restore_debug_trace_modes();
4551 if( pArg ){
4552 int bIsExplain = (sqlite3_stmt_isexplain(pStmt)==1);
4553 pArg->cMode = pArg->mode;
4554 if( pArg->autoExplain ){
4555 if( bIsExplain ){
4556 pArg->cMode = MODE_Explain;
4558 if( sqlite3_stmt_isexplain(pStmt)==2 ){
4559 pArg->cMode = MODE_EQP;
4563 /* If the shell is currently in ".explain" mode, gather the extra
4564 ** data required to add indents to the output.*/
4565 if( pArg->cMode==MODE_Explain && bIsExplain ){
4566 explain_data_prepare(pArg, pStmt);
4570 bind_prepared_stmt(pArg, pStmt);
4571 exec_prepared_stmt(pArg, pStmt);
4572 explain_data_delete(pArg);
4573 eqp_render(pArg, 0);
4575 /* print usage stats if stats on */
4576 if( pArg && pArg->statsOn ){
4577 display_stats(db, pArg, 0);
4580 /* print loop-counters if required */
4581 if( pArg && pArg->scanstatsOn ){
4582 display_scanstats(db, pArg);
4585 /* Finalize the statement just executed. If this fails, save a
4586 ** copy of the error message. Otherwise, set zSql to point to the
4587 ** next statement to execute. */
4588 rc2 = sqlite3_finalize(pStmt);
4589 if( rc!=SQLITE_NOMEM ) rc = rc2;
4590 if( rc==SQLITE_OK ){
4591 zSql = zLeftover;
4592 while( IsSpace(zSql[0]) ) zSql++;
4593 }else if( pzErrMsg ){
4594 *pzErrMsg = save_err_msg(db, "stepping", rc, 0);
4597 /* clear saved stmt handle */
4598 if( pArg ){
4599 pArg->pStmt = NULL;
4602 } /* end while */
4604 return rc;
4608 ** Release memory previously allocated by tableColumnList().
4610 static void freeColumnList(char **azCol){
4611 int i;
4612 for(i=1; azCol[i]; i++){
4613 sqlite3_free(azCol[i]);
4615 /* azCol[0] is a static string */
4616 sqlite3_free(azCol);
4620 ** Return a list of pointers to strings which are the names of all
4621 ** columns in table zTab. The memory to hold the names is dynamically
4622 ** allocated and must be released by the caller using a subsequent call
4623 ** to freeColumnList().
4625 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
4626 ** value that needs to be preserved, then azCol[0] is filled in with the
4627 ** name of the rowid column.
4629 ** The first regular column in the table is azCol[1]. The list is terminated
4630 ** by an entry with azCol[i]==0.
4632 static char **tableColumnList(ShellState *p, const char *zTab){
4633 char **azCol = 0;
4634 sqlite3_stmt *pStmt;
4635 char *zSql;
4636 int nCol = 0;
4637 int nAlloc = 0;
4638 int nPK = 0; /* Number of PRIMARY KEY columns seen */
4639 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
4640 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
4641 int rc;
4643 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
4644 shell_check_oom(zSql);
4645 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4646 sqlite3_free(zSql);
4647 if( rc ) return 0;
4648 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4649 if( nCol>=nAlloc-2 ){
4650 nAlloc = nAlloc*2 + nCol + 10;
4651 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
4652 shell_check_oom(azCol);
4654 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
4655 shell_check_oom(azCol[nCol]);
4656 if( sqlite3_column_int(pStmt, 5) ){
4657 nPK++;
4658 if( nPK==1
4659 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
4660 "INTEGER")==0
4662 isIPK = 1;
4663 }else{
4664 isIPK = 0;
4668 sqlite3_finalize(pStmt);
4669 if( azCol==0 ) return 0;
4670 azCol[0] = 0;
4671 azCol[nCol+1] = 0;
4673 /* The decision of whether or not a rowid really needs to be preserved
4674 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
4675 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
4676 ** rowids on tables where the rowid is inaccessible because there are other
4677 ** columns in the table named "rowid", "_rowid_", and "oid".
4679 if( preserveRowid && isIPK ){
4680 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4681 ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID
4682 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4683 ** ROWID aliases. To distinguish these cases, check to see if
4684 ** there is a "pk" entry in "PRAGMA index_list". There will be
4685 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4687 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4688 " WHERE origin='pk'", zTab);
4689 shell_check_oom(zSql);
4690 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4691 sqlite3_free(zSql);
4692 if( rc ){
4693 freeColumnList(azCol);
4694 return 0;
4696 rc = sqlite3_step(pStmt);
4697 sqlite3_finalize(pStmt);
4698 preserveRowid = rc==SQLITE_ROW;
4700 if( preserveRowid ){
4701 /* Only preserve the rowid if we can find a name to use for the
4702 ** rowid */
4703 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
4704 int i, j;
4705 for(j=0; j<3; j++){
4706 for(i=1; i<=nCol; i++){
4707 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
4709 if( i>nCol ){
4710 /* At this point, we know that azRowid[j] is not the name of any
4711 ** ordinary column in the table. Verify that azRowid[j] is a valid
4712 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
4713 ** tables will fail this last check */
4714 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
4715 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
4716 break;
4720 return azCol;
4724 ** Toggle the reverse_unordered_selects setting.
4726 static void toggleSelectOrder(sqlite3 *db){
4727 sqlite3_stmt *pStmt = 0;
4728 int iSetting = 0;
4729 char zStmt[100];
4730 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
4731 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4732 iSetting = sqlite3_column_int(pStmt, 0);
4734 sqlite3_finalize(pStmt);
4735 sqlite3_snprintf(sizeof(zStmt), zStmt,
4736 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
4737 sqlite3_exec(db, zStmt, 0, 0, 0);
4741 ** This is a different callback routine used for dumping the database.
4742 ** Each row received by this callback consists of a table name,
4743 ** the table type ("index" or "table") and SQL to create the table.
4744 ** This routine should print text sufficient to recreate the table.
4746 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
4747 int rc;
4748 const char *zTable;
4749 const char *zType;
4750 const char *zSql;
4751 ShellState *p = (ShellState *)pArg;
4752 int dataOnly;
4753 int noSys;
4755 UNUSED_PARAMETER(azNotUsed);
4756 if( nArg!=3 || azArg==0 ) return 0;
4757 zTable = azArg[0];
4758 zType = azArg[1];
4759 zSql = azArg[2];
4760 if( zTable==0 ) return 0;
4761 if( zType==0 ) return 0;
4762 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
4763 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
4765 if( cli_strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
4766 /* no-op */
4767 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
4768 if( !dataOnly ) sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
4769 }else if( cli_strncmp(zTable, "sqlite_", 7)==0 ){
4770 return 0;
4771 }else if( dataOnly ){
4772 /* no-op */
4773 }else if( cli_strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
4774 char *zIns;
4775 if( !p->writableSchema ){
4776 sqlite3_fputs("PRAGMA writable_schema=ON;\n", p->out);
4777 p->writableSchema = 1;
4779 zIns = sqlite3_mprintf(
4780 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4781 "VALUES('table','%q','%q',0,'%q');",
4782 zTable, zTable, zSql);
4783 shell_check_oom(zIns);
4784 sqlite3_fprintf(p->out, "%s\n", zIns);
4785 sqlite3_free(zIns);
4786 return 0;
4787 }else{
4788 printSchemaLine(p->out, zSql, ";\n");
4791 if( cli_strcmp(zType, "table")==0 ){
4792 ShellText sSelect;
4793 ShellText sTable;
4794 char **azCol;
4795 int i;
4796 char *savedDestTable;
4797 int savedMode;
4799 azCol = tableColumnList(p, zTable);
4800 if( azCol==0 ){
4801 p->nErr++;
4802 return 0;
4805 /* Always quote the table name, even if it appears to be pure ascii,
4806 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
4807 initText(&sTable);
4808 appendText(&sTable, zTable, quoteChar(zTable));
4809 /* If preserving the rowid, add a column list after the table name.
4810 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4811 ** instead of the usual "INSERT INTO tab VALUES(...)".
4813 if( azCol[0] ){
4814 appendText(&sTable, "(", 0);
4815 appendText(&sTable, azCol[0], 0);
4816 for(i=1; azCol[i]; i++){
4817 appendText(&sTable, ",", 0);
4818 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
4820 appendText(&sTable, ")", 0);
4823 /* Build an appropriate SELECT statement */
4824 initText(&sSelect);
4825 appendText(&sSelect, "SELECT ", 0);
4826 if( azCol[0] ){
4827 appendText(&sSelect, azCol[0], 0);
4828 appendText(&sSelect, ",", 0);
4830 for(i=1; azCol[i]; i++){
4831 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
4832 if( azCol[i+1] ){
4833 appendText(&sSelect, ",", 0);
4836 freeColumnList(azCol);
4837 appendText(&sSelect, " FROM ", 0);
4838 appendText(&sSelect, zTable, quoteChar(zTable));
4840 savedDestTable = p->zDestTable;
4841 savedMode = p->mode;
4842 p->zDestTable = sTable.z;
4843 p->mode = p->cMode = MODE_Insert;
4844 rc = shell_exec(p, sSelect.z, 0);
4845 if( (rc&0xff)==SQLITE_CORRUPT ){
4846 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
4847 toggleSelectOrder(p->db);
4848 shell_exec(p, sSelect.z, 0);
4849 toggleSelectOrder(p->db);
4851 p->zDestTable = savedDestTable;
4852 p->mode = savedMode;
4853 freeText(&sTable);
4854 freeText(&sSelect);
4855 if( rc ) p->nErr++;
4857 return 0;
4861 ** Run zQuery. Use dump_callback() as the callback routine so that
4862 ** the contents of the query are output as SQL statements.
4864 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
4865 ** "ORDER BY rowid DESC" to the end.
4867 static int run_schema_dump_query(
4868 ShellState *p,
4869 const char *zQuery
4871 int rc;
4872 char *zErr = 0;
4873 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
4874 if( rc==SQLITE_CORRUPT ){
4875 char *zQ2;
4876 int len = strlen30(zQuery);
4877 sqlite3_fputs("/****** CORRUPTION ERROR *******/\n", p->out);
4878 if( zErr ){
4879 sqlite3_fprintf(p->out, "/****** %s ******/\n", zErr);
4880 sqlite3_free(zErr);
4881 zErr = 0;
4883 zQ2 = malloc( len+100 );
4884 if( zQ2==0 ) return rc;
4885 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
4886 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
4887 if( rc ){
4888 sqlite3_fprintf(p->out, "/****** ERROR: %s ******/\n", zErr);
4889 }else{
4890 rc = SQLITE_CORRUPT;
4892 sqlite3_free(zErr);
4893 free(zQ2);
4895 return rc;
4899 ** Text of help messages.
4901 ** The help text for each individual command begins with a line that starts
4902 ** with ".". Subsequent lines are supplemental information.
4904 ** There must be two or more spaces between the end of the command and the
4905 ** start of the description of what that command does.
4907 static const char *(azHelp[]) = {
4908 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4909 && !defined(SQLITE_SHELL_FIDDLE)
4910 ".archive ... Manage SQL archives",
4911 " Each command must have exactly one of the following options:",
4912 " -c, --create Create a new archive",
4913 " -u, --update Add or update files with changed mtime",
4914 " -i, --insert Like -u but always add even if unchanged",
4915 " -r, --remove Remove files from archive",
4916 " -t, --list List contents of archive",
4917 " -x, --extract Extract files from archive",
4918 " Optional arguments:",
4919 " -v, --verbose Print each filename as it is processed",
4920 " -f FILE, --file FILE Use archive FILE (default is current db)",
4921 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
4922 " -C DIR, --directory DIR Read/extract files from directory DIR",
4923 " -g, --glob Use glob matching for names in archive",
4924 " -n, --dryrun Show the SQL that would have occurred",
4925 " Examples:",
4926 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
4927 " .ar -tf ARCHIVE # List members of ARCHIVE",
4928 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
4929 " See also:",
4930 " http://sqlite.org/cli.html#sqlite_archive_support",
4931 #endif
4932 #ifndef SQLITE_OMIT_AUTHORIZATION
4933 ".auth ON|OFF Show authorizer callbacks",
4934 #endif
4935 #ifndef SQLITE_SHELL_FIDDLE
4936 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
4937 " Options:",
4938 " --append Use the appendvfs",
4939 " --async Write to FILE without journal and fsync()",
4940 #endif
4941 ".bail on|off Stop after hitting an error. Default OFF",
4942 #ifndef SQLITE_SHELL_FIDDLE
4943 ".cd DIRECTORY Change the working directory to DIRECTORY",
4944 #endif
4945 ".changes on|off Show number of rows changed by SQL",
4946 #ifndef SQLITE_SHELL_FIDDLE
4947 ".check GLOB Fail if output since .testcase does not match",
4948 ".clone NEWDB Clone data into NEWDB from the existing database",
4949 #endif
4950 ".connection [close] [#] Open or close an auxiliary database connection",
4951 ".crlf ?on|off? Whether or not to use \\r\\n line endings",
4952 ".databases List names and files of attached databases",
4953 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
4954 #if SQLITE_SHELL_HAVE_RECOVER
4955 ".dbinfo ?DB? Show status information about the database",
4956 #endif
4957 ".dump ?OBJECTS? Render database content as SQL",
4958 " Options:",
4959 " --data-only Output only INSERT statements",
4960 " --newlines Allow unescaped newline characters in output",
4961 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
4962 " --preserve-rowids Include ROWID values in the output",
4963 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4964 " Additional LIKE patterns can be given in subsequent arguments",
4965 ".echo on|off Turn command echo on or off",
4966 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
4967 " Other Modes:",
4968 #ifdef SQLITE_DEBUG
4969 " test Show raw EXPLAIN QUERY PLAN output",
4970 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4971 #endif
4972 " trigger Like \"full\" but also show trigger bytecode",
4973 #ifndef SQLITE_SHELL_FIDDLE
4974 ".excel Display the output of next command in spreadsheet",
4975 " --bom Put a UTF8 byte-order mark on intermediate file",
4976 #endif
4977 #ifndef SQLITE_SHELL_FIDDLE
4978 ".exit ?CODE? Exit this program with return-code CODE",
4979 #endif
4980 ".expert EXPERIMENTAL. Suggest indexes for queries",
4981 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
4982 ".filectrl CMD ... Run various sqlite3_file_control() operations",
4983 " --schema SCHEMA Use SCHEMA instead of \"main\"",
4984 " --help Show CMD details",
4985 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
4986 ".headers on|off Turn display of headers on or off",
4987 ".help ?-all? ?PATTERN? Show help text for PATTERN",
4988 #ifndef SQLITE_SHELL_FIDDLE
4989 ".import FILE TABLE Import data from FILE into TABLE",
4990 " Options:",
4991 " --ascii Use \\037 and \\036 as column and row separators",
4992 " --csv Use , and \\n as column and row separators",
4993 " --skip N Skip the first N rows of input",
4994 " --schema S Target table to be S.TABLE",
4995 " -v \"Verbose\" - increase auxiliary output",
4996 " Notes:",
4997 " * If TABLE does not exist, it is created. The first row of input",
4998 " determines the column names.",
4999 " * If neither --csv or --ascii are used, the input mode is derived",
5000 " from the \".mode\" output mode",
5001 " * If FILE begins with \"|\" then it is a command that generates the",
5002 " input text.",
5003 #endif
5004 #ifndef SQLITE_OMIT_TEST_CONTROL
5005 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
5006 #endif
5007 ".indexes ?TABLE? Show names of indexes",
5008 " If TABLE is specified, only show indexes for",
5009 " tables matching TABLE using the LIKE operator.",
5010 ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
5011 #ifdef SQLITE_ENABLE_IOTRACE
5012 ",iotrace FILE Enable I/O diagnostic logging to FILE",
5013 #endif
5014 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
5015 ".lint OPTIONS Report potential schema issues.",
5016 " Options:",
5017 " fkey-indexes Find missing foreign key indexes",
5018 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
5019 ".load FILE ?ENTRY? Load an extension library",
5020 #endif
5021 #if !defined(SQLITE_SHELL_FIDDLE)
5022 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
5023 #else
5024 ".log on|off Turn logging on or off.",
5025 #endif
5026 ".mode MODE ?OPTIONS? Set output mode",
5027 " MODE is one of:",
5028 " ascii Columns/rows delimited by 0x1F and 0x1E",
5029 " box Tables using unicode box-drawing characters",
5030 " csv Comma-separated values",
5031 " column Output in columns. (See .width)",
5032 " html HTML <table> code",
5033 " insert SQL insert statements for TABLE",
5034 " json Results in a JSON array",
5035 " line One value per line",
5036 " list Values delimited by \"|\"",
5037 " markdown Markdown table format",
5038 " qbox Shorthand for \"box --wrap 60 --quote\"",
5039 " quote Escape answers as for SQL",
5040 " table ASCII-art table",
5041 " tabs Tab-separated values",
5042 " tcl TCL list elements",
5043 " OPTIONS: (for columnar modes or insert mode):",
5044 " --wrap N Wrap output lines to no longer than N characters",
5045 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
5046 " --ww Shorthand for \"--wordwrap 1\"",
5047 " --quote Quote output text as SQL literals",
5048 " --noquote Do not quote output text",
5049 " TABLE The name of SQL table used for \"insert\" mode",
5050 #ifndef SQLITE_SHELL_FIDDLE
5051 ".nonce STRING Suspend safe mode for one command if nonce matches",
5052 #endif
5053 ".nullvalue STRING Use STRING in place of NULL values",
5054 #ifndef SQLITE_SHELL_FIDDLE
5055 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
5056 " If FILE begins with '|' then open as a pipe",
5057 " --bom Put a UTF8 byte-order mark at the beginning",
5058 " -e Send output to the system text editor",
5059 " --plain Use text/plain output instead of HTML for -w option",
5060 " -w Send output as HTML to a web browser (same as \".www\")",
5061 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
5062 /* Note that .open is (partially) available in WASM builds but is
5063 ** currently only intended to be used by the fiddle tool, not
5064 ** end users, so is "undocumented." */
5065 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
5066 " Options:",
5067 " --append Use appendvfs to append database to the end of FILE",
5068 #endif
5069 #ifndef SQLITE_OMIT_DESERIALIZE
5070 " --deserialize Load into memory using sqlite3_deserialize()",
5071 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
5072 " --maxsize N Maximum size for --hexdb or --deserialized database",
5073 #endif
5074 " --new Initialize FILE to an empty database",
5075 " --nofollow Do not follow symbolic links",
5076 " --readonly Open FILE readonly",
5077 " --zip FILE is a ZIP archive",
5078 #ifndef SQLITE_SHELL_FIDDLE
5079 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
5080 " If FILE begins with '|' then open it as a pipe.",
5081 " Options:",
5082 " --bom Prefix output with a UTF8 byte-order mark",
5083 " -e Send output to the system text editor",
5084 " --plain Use text/plain for -w option",
5085 " -w Send output to a web browser",
5086 " -x Send output as CSV to a spreadsheet",
5087 #endif
5088 ".parameter CMD ... Manage SQL parameter bindings",
5089 " clear Erase all bindings",
5090 " init Initialize the TEMP table that holds bindings",
5091 " list List the current parameter bindings",
5092 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
5093 " PARAMETER should start with one of: $ : @ ?",
5094 " unset PARAMETER Remove PARAMETER from the binding table",
5095 ".print STRING... Print literal STRING",
5096 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
5097 ".progress N Invoke progress handler after every N opcodes",
5098 " --limit N Interrupt after N progress callbacks",
5099 " --once Do no more than one progress interrupt",
5100 " --quiet|-q No output except at interrupts",
5101 " --reset Reset the count for each input and interrupt",
5102 #endif
5103 ".prompt MAIN CONTINUE Replace the standard prompts",
5104 #ifndef SQLITE_SHELL_FIDDLE
5105 ".quit Stop interpreting input stream, exit if primary.",
5106 ".read FILE Read input from FILE or command output",
5107 " If FILE begins with \"|\", it is a command that generates the input.",
5108 #endif
5109 #if SQLITE_SHELL_HAVE_RECOVER
5110 ".recover Recover as much data as possible from corrupt db.",
5111 " --ignore-freelist Ignore pages that appear to be on db freelist",
5112 " --lost-and-found TABLE Alternative name for the lost-and-found table",
5113 " --no-rowids Do not attempt to recover rowid values",
5114 " that are not also INTEGER PRIMARY KEYs",
5115 #endif
5116 #ifndef SQLITE_SHELL_FIDDLE
5117 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
5118 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
5119 #endif
5120 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
5121 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
5122 " Options:",
5123 " --indent Try to pretty-print the schema",
5124 " --nosys Omit objects whose names start with \"sqlite_\"",
5125 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
5126 " Options:",
5127 " --init Create a new SELFTEST table",
5128 " -v Verbose output",
5129 ".separator COL ?ROW? Change the column and row separators",
5130 #if defined(SQLITE_ENABLE_SESSION)
5131 ".session ?NAME? CMD ... Create or control sessions",
5132 " Subcommands:",
5133 " attach TABLE Attach TABLE",
5134 " changeset FILE Write a changeset into FILE",
5135 " close Close one session",
5136 " enable ?BOOLEAN? Set or query the enable bit",
5137 " filter GLOB... Reject tables matching GLOBs",
5138 " indirect ?BOOLEAN? Mark or query the indirect status",
5139 " isempty Query whether the session is empty",
5140 " list List currently open session names",
5141 " open DB NAME Open a new session on DB",
5142 " patchset FILE Write a patchset into FILE",
5143 " If ?NAME? is omitted, the first defined session is used.",
5144 #endif
5145 ".sha3sum ... Compute a SHA3 hash of database content",
5146 " Options:",
5147 " --schema Also hash the sqlite_schema table",
5148 " --sha3-224 Use the sha3-224 algorithm",
5149 " --sha3-256 Use the sha3-256 algorithm (default)",
5150 " --sha3-384 Use the sha3-384 algorithm",
5151 " --sha3-512 Use the sha3-512 algorithm",
5152 " Any other argument is a LIKE pattern for tables to hash",
5153 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5154 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
5155 #endif
5156 ".show Show the current values for various settings",
5157 ".stats ?ARG? Show stats or turn stats on or off",
5158 " off Turn off automatic stat display",
5159 " on Turn on automatic stat display",
5160 " stmt Show statement stats",
5161 " vmstep Show the virtual machine step count only",
5162 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5163 ".system CMD ARGS... Run CMD ARGS... in a system shell",
5164 #endif
5165 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
5166 #ifndef SQLITE_SHELL_FIDDLE
5167 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
5168 #endif
5169 ",testctrl CMD ... Run various sqlite3_test_control() operations",
5170 " Run \".testctrl\" with no arguments for details",
5171 ".timeout MS Try opening locked tables for MS milliseconds",
5172 ".timer on|off Turn SQL timer on or off",
5173 #ifndef SQLITE_OMIT_TRACE
5174 ".trace ?OPTIONS? Output each SQL statement as it is run",
5175 " FILE Send output to FILE",
5176 " stdout Send output to stdout",
5177 " stderr Send output to stderr",
5178 " off Disable tracing",
5179 " --expanded Expand query parameters",
5180 #ifdef SQLITE_ENABLE_NORMALIZE
5181 " --normalized Normal the SQL statements",
5182 #endif
5183 " --plain Show SQL as it is input",
5184 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
5185 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
5186 " --row Trace each row (SQLITE_TRACE_ROW)",
5187 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
5188 #endif /* SQLITE_OMIT_TRACE */
5189 #ifdef SQLITE_DEBUG
5190 ".unmodule NAME ... Unregister virtual table modules",
5191 " --allexcept Unregister everything except those named",
5192 #endif
5193 ".version Show source, library and compiler versions",
5194 ".vfsinfo ?AUX? Information about the top-level VFS",
5195 ".vfslist List all available VFSes",
5196 ".vfsname ?AUX? Print the name of the VFS stack",
5197 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
5198 " Negative values right-justify",
5199 #ifndef SQLITE_SHELL_FIDDLE
5200 ".www Display output of the next command in web browser",
5201 " --plain Show results as text/plain, not as HTML",
5202 #endif
5206 ** Output help text.
5208 ** zPattern describes the set of commands for which help text is provided.
5209 ** If zPattern is NULL, then show all commands, but only give a one-line
5210 ** description of each.
5212 ** Return the number of matches.
5214 static int showHelp(FILE *out, const char *zPattern){
5215 int i = 0;
5216 int j = 0;
5217 int n = 0;
5218 char *zPat;
5219 if( zPattern==0
5220 || zPattern[0]=='0'
5221 || cli_strcmp(zPattern,"-a")==0
5222 || cli_strcmp(zPattern,"-all")==0
5223 || cli_strcmp(zPattern,"--all")==0
5225 enum HelpWanted { HW_NoCull = 0, HW_SummaryOnly = 1, HW_Undoc = 2 };
5226 enum HelpHave { HH_Undoc = 2, HH_Summary = 1, HH_More = 0 };
5227 /* Show all or most commands
5228 ** *zPattern==0 => summary of documented commands only
5229 ** *zPattern=='0' => whole help for undocumented commands
5230 ** Otherwise => whole help for documented commands
5232 enum HelpWanted hw = HW_SummaryOnly;
5233 enum HelpHave hh = HH_More;
5234 if( zPattern!=0 ){
5235 hw = (*zPattern=='0')? HW_NoCull|HW_Undoc : HW_NoCull;
5237 for(i=0; i<ArraySize(azHelp); i++){
5238 switch( azHelp[i][0] ){
5239 case ',':
5240 hh = HH_Summary|HH_Undoc;
5241 break;
5242 case '.':
5243 hh = HH_Summary;
5244 break;
5245 default:
5246 hh &= ~HH_Summary;
5247 break;
5249 if( ((hw^hh)&HH_Undoc)==0 ){
5250 if( (hh&HH_Summary)!=0 ){
5251 sqlite3_fprintf(out, ".%s\n", azHelp[i]+1);
5252 ++n;
5253 }else if( (hw&HW_SummaryOnly)==0 ){
5254 sqlite3_fprintf(out, "%s\n", azHelp[i]);
5258 }else{
5259 /* Seek documented commands for which zPattern is an exact prefix */
5260 zPat = sqlite3_mprintf(".%s*", zPattern);
5261 shell_check_oom(zPat);
5262 for(i=0; i<ArraySize(azHelp); i++){
5263 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
5264 sqlite3_fprintf(out, "%s\n", azHelp[i]);
5265 j = i+1;
5266 n++;
5269 sqlite3_free(zPat);
5270 if( n ){
5271 if( n==1 ){
5272 /* when zPattern is a prefix of exactly one command, then include
5273 ** the details of that command, which should begin at offset j */
5274 while( j<ArraySize(azHelp)-1 && azHelp[j][0]==' ' ){
5275 sqlite3_fprintf(out, "%s\n", azHelp[j]);
5276 j++;
5279 return n;
5281 /* Look for documented commands that contain zPattern anywhere.
5282 ** Show complete text of all documented commands that match. */
5283 zPat = sqlite3_mprintf("%%%s%%", zPattern);
5284 shell_check_oom(zPat);
5285 for(i=0; i<ArraySize(azHelp); i++){
5286 if( azHelp[i][0]==',' ){
5287 while( i<ArraySize(azHelp)-1 && azHelp[i+1][0]==' ' ) ++i;
5288 continue;
5290 if( azHelp[i][0]=='.' ) j = i;
5291 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
5292 sqlite3_fprintf(out, "%s\n", azHelp[j]);
5293 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]==' ' ){
5294 j++;
5295 sqlite3_fprintf(out, "%s\n", azHelp[j]);
5297 i = j;
5298 n++;
5301 sqlite3_free(zPat);
5303 return n;
5306 /* Forward reference */
5307 static int process_input(ShellState *p);
5310 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
5311 ** and return a pointer to the buffer. The caller is responsible for freeing
5312 ** the memory.
5314 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
5315 ** read.
5317 ** For convenience, a nul-terminator byte is always appended to the data read
5318 ** from the file before the buffer is returned. This byte is not included in
5319 ** the final value of (*pnByte), if applicable.
5321 ** NULL is returned if any error is encountered. The final value of *pnByte
5322 ** is undefined in this case.
5324 static char *readFile(const char *zName, int *pnByte){
5325 FILE *in = sqlite3_fopen(zName, "rb");
5326 long nIn;
5327 size_t nRead;
5328 char *pBuf;
5329 int rc;
5330 if( in==0 ) return 0;
5331 rc = fseek(in, 0, SEEK_END);
5332 if( rc!=0 ){
5333 sqlite3_fprintf(stderr,"Error: '%s' not seekable\n", zName);
5334 fclose(in);
5335 return 0;
5337 nIn = ftell(in);
5338 rewind(in);
5339 pBuf = sqlite3_malloc64( nIn+1 );
5340 if( pBuf==0 ){
5341 sqlite3_fputs("Error: out of memory\n", stderr);
5342 fclose(in);
5343 return 0;
5345 nRead = fread(pBuf, nIn, 1, in);
5346 fclose(in);
5347 if( nRead!=1 ){
5348 sqlite3_free(pBuf);
5349 sqlite3_fprintf(stderr,"Error: cannot read '%s'\n", zName);
5350 return 0;
5352 pBuf[nIn] = 0;
5353 if( pnByte ) *pnByte = nIn;
5354 return pBuf;
5357 #if defined(SQLITE_ENABLE_SESSION)
5359 ** Close a single OpenSession object and release all of its associated
5360 ** resources.
5362 static void session_close(OpenSession *pSession){
5363 int i;
5364 sqlite3session_delete(pSession->p);
5365 sqlite3_free(pSession->zName);
5366 for(i=0; i<pSession->nFilter; i++){
5367 sqlite3_free(pSession->azFilter[i]);
5369 sqlite3_free(pSession->azFilter);
5370 memset(pSession, 0, sizeof(OpenSession));
5372 #endif
5375 ** Close all OpenSession objects and release all associated resources.
5377 #if defined(SQLITE_ENABLE_SESSION)
5378 static void session_close_all(ShellState *p, int i){
5379 int j;
5380 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
5381 for(j=0; j<pAuxDb->nSession; j++){
5382 session_close(&pAuxDb->aSession[j]);
5384 pAuxDb->nSession = 0;
5386 #else
5387 # define session_close_all(X,Y)
5388 #endif
5391 ** Implementation of the xFilter function for an open session. Omit
5392 ** any tables named by ".session filter" but let all other table through.
5394 #if defined(SQLITE_ENABLE_SESSION)
5395 static int session_filter(void *pCtx, const char *zTab){
5396 OpenSession *pSession = (OpenSession*)pCtx;
5397 int i;
5398 for(i=0; i<pSession->nFilter; i++){
5399 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
5401 return 1;
5403 #endif
5406 ** Try to deduce the type of file for zName based on its content. Return
5407 ** one of the SHELL_OPEN_* constants.
5409 ** If the file does not exist or is empty but its name looks like a ZIP
5410 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
5411 ** Otherwise, assume an ordinary database regardless of the filename if
5412 ** the type cannot be determined from content.
5414 int deduceDatabaseType(const char *zName, int dfltZip){
5415 FILE *f = sqlite3_fopen(zName, "rb");
5416 size_t n;
5417 int rc = SHELL_OPEN_UNSPEC;
5418 char zBuf[100];
5419 if( f==0 ){
5420 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5421 return SHELL_OPEN_ZIPFILE;
5422 }else{
5423 return SHELL_OPEN_NORMAL;
5426 n = fread(zBuf, 16, 1, f);
5427 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
5428 fclose(f);
5429 return SHELL_OPEN_NORMAL;
5431 fseek(f, -25, SEEK_END);
5432 n = fread(zBuf, 25, 1, f);
5433 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
5434 rc = SHELL_OPEN_APPENDVFS;
5435 }else{
5436 fseek(f, -22, SEEK_END);
5437 n = fread(zBuf, 22, 1, f);
5438 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
5439 && zBuf[3]==0x06 ){
5440 rc = SHELL_OPEN_ZIPFILE;
5441 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
5442 rc = SHELL_OPEN_ZIPFILE;
5445 fclose(f);
5446 return rc;
5449 #ifndef SQLITE_OMIT_DESERIALIZE
5451 ** Reconstruct an in-memory database using the output from the "dbtotxt"
5452 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
5453 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
5455 static unsigned char *readHexDb(ShellState *p, int *pnData){
5456 unsigned char *a = 0;
5457 int nLine;
5458 int n = 0;
5459 int pgsz = 0;
5460 int iOffset = 0;
5461 int j, k;
5462 int rc;
5463 FILE *in;
5464 const char *zDbFilename = p->pAuxDb->zDbFilename;
5465 unsigned int x[16];
5466 char zLine[1000];
5467 if( zDbFilename ){
5468 in = sqlite3_fopen(zDbFilename, "r");
5469 if( in==0 ){
5470 sqlite3_fprintf(stderr,"cannot open \"%s\" for reading\n", zDbFilename);
5471 return 0;
5473 nLine = 0;
5474 }else{
5475 in = p->in;
5476 nLine = p->lineno;
5477 if( in==0 ) in = stdin;
5479 *pnData = 0;
5480 nLine++;
5481 if( sqlite3_fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
5482 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
5483 if( rc!=2 ) goto readHexDb_error;
5484 if( n<0 ) goto readHexDb_error;
5485 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
5486 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
5487 a = sqlite3_malloc( n ? n : 1 );
5488 shell_check_oom(a);
5489 memset(a, 0, n);
5490 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
5491 sqlite3_fputs("invalid pagesize\n", stderr);
5492 goto readHexDb_error;
5494 for(nLine++; sqlite3_fgets(zLine, sizeof(zLine), in)!=0; nLine++){
5495 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
5496 if( rc==2 ){
5497 iOffset = k;
5498 continue;
5500 if( cli_strncmp(zLine, "| end ", 6)==0 ){
5501 break;
5503 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5504 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
5505 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
5506 if( rc==17 ){
5507 k = iOffset+j;
5508 if( k+16<=n && k>=0 ){
5509 int ii;
5510 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
5514 *pnData = n;
5515 if( in!=p->in ){
5516 fclose(in);
5517 }else{
5518 p->lineno = nLine;
5520 return a;
5522 readHexDb_error:
5523 if( in!=p->in ){
5524 fclose(in);
5525 }else{
5526 while( sqlite3_fgets(zLine, sizeof(zLine), p->in)!=0 ){
5527 nLine++;
5528 if(cli_strncmp(zLine, "| end ", 6)==0 ) break;
5530 p->lineno = nLine;
5532 sqlite3_free(a);
5533 sqlite3_fprintf(stderr,"Error on line %d of --hexdb input\n", nLine);
5534 return 0;
5536 #endif /* SQLITE_OMIT_DESERIALIZE */
5539 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
5541 static void shellUSleepFunc(
5542 sqlite3_context *context,
5543 int argcUnused,
5544 sqlite3_value **argv
5546 int sleep = sqlite3_value_int(argv[0]);
5547 (void)argcUnused;
5548 sqlite3_sleep(sleep/1000);
5549 sqlite3_result_int(context, sleep);
5552 /* Flags for open_db().
5554 ** The default behavior of open_db() is to exit(1) if the database fails to
5555 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5556 ** but still returns without calling exit.
5558 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5559 ** ZIP archive if the file does not exist or is empty and its name matches
5560 ** the *.zip pattern.
5562 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
5563 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
5566 ** Make sure the database is open. If it is not, then open it. If
5567 ** the database fails to open, print an error message and exit.
5569 static void open_db(ShellState *p, int openFlags){
5570 if( p->db==0 ){
5571 const char *zDbFilename = p->pAuxDb->zDbFilename;
5572 if( p->openMode==SHELL_OPEN_UNSPEC ){
5573 if( zDbFilename==0 || zDbFilename[0]==0 ){
5574 p->openMode = SHELL_OPEN_NORMAL;
5575 }else{
5576 p->openMode = (u8)deduceDatabaseType(zDbFilename,
5577 (openFlags & OPEN_DB_ZIPFILE)!=0);
5580 switch( p->openMode ){
5581 case SHELL_OPEN_APPENDVFS: {
5582 sqlite3_open_v2(zDbFilename, &p->db,
5583 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
5584 break;
5586 case SHELL_OPEN_HEXDB:
5587 case SHELL_OPEN_DESERIALIZE: {
5588 sqlite3_open(0, &p->db);
5589 break;
5591 case SHELL_OPEN_ZIPFILE: {
5592 sqlite3_open(":memory:", &p->db);
5593 break;
5595 case SHELL_OPEN_READONLY: {
5596 sqlite3_open_v2(zDbFilename, &p->db,
5597 SQLITE_OPEN_READONLY|p->openFlags, 0);
5598 break;
5600 case SHELL_OPEN_UNSPEC:
5601 case SHELL_OPEN_NORMAL: {
5602 sqlite3_open_v2(zDbFilename, &p->db,
5603 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
5604 break;
5607 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5608 sqlite3_fprintf(stderr,"Error: unable to open database \"%s\": %s\n",
5609 zDbFilename, sqlite3_errmsg(p->db));
5610 if( (openFlags & OPEN_DB_KEEPALIVE)==0 ){
5611 exit(1);
5613 sqlite3_close(p->db);
5614 sqlite3_open(":memory:", &p->db);
5615 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
5616 sqlite3_fputs("Also: unable to open substitute in-memory database.\n",
5617 stderr);
5618 exit(1);
5619 }else{
5620 sqlite3_fprintf(stderr,
5621 "Notice: using substitute in-memory database instead of \"%s\"\n",
5622 zDbFilename);
5625 globalDb = p->db;
5626 sqlite3_db_config(p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, (int)0, (int*)0);
5628 /* Reflect the use or absence of --unsafe-testing invocation. */
5630 int testmode_on = ShellHasFlag(p,SHFLG_TestingMode);
5631 sqlite3_db_config(p->db, SQLITE_DBCONFIG_TRUSTED_SCHEMA, testmode_on,0);
5632 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, !testmode_on,0);
5635 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5636 sqlite3_enable_load_extension(p->db, 1);
5637 #endif
5638 sqlite3_sha_init(p->db, 0, 0);
5639 sqlite3_shathree_init(p->db, 0, 0);
5640 sqlite3_uint_init(p->db, 0, 0);
5641 sqlite3_stmtrand_init(p->db, 0, 0);
5642 sqlite3_decimal_init(p->db, 0, 0);
5643 sqlite3_percentile_init(p->db, 0, 0);
5644 sqlite3_base64_init(p->db, 0, 0);
5645 sqlite3_base85_init(p->db, 0, 0);
5646 sqlite3_regexp_init(p->db, 0, 0);
5647 sqlite3_ieee_init(p->db, 0, 0);
5648 sqlite3_series_init(p->db, 0, 0);
5649 #ifndef SQLITE_SHELL_FIDDLE
5650 sqlite3_fileio_init(p->db, 0, 0);
5651 sqlite3_completion_init(p->db, 0, 0);
5652 #endif
5653 #ifdef SQLITE_HAVE_ZLIB
5654 if( !p->bSafeModePersist ){
5655 sqlite3_zipfile_init(p->db, 0, 0);
5656 sqlite3_sqlar_init(p->db, 0, 0);
5658 #endif
5659 #ifdef SQLITE_SHELL_EXTFUNCS
5660 /* Create a preprocessing mechanism for extensions to make
5661 * their own provisions for being built into the shell.
5662 * This is a short-span macro. See further below for usage.
5664 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
5665 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
5666 /* Let custom-included extensions get their ..._init() called.
5667 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
5668 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
5669 * initialization routine to be called.
5672 int irc = SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, INIT)(p->db);
5673 /* Let custom-included extensions expose their functionality.
5674 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
5675 * the SQL functions, virtual tables, collating sequences or
5676 * VFS's implemented by the extension to be registered.
5678 if( irc==SQLITE_OK
5679 || irc==SQLITE_OK_LOAD_PERMANENTLY ){
5680 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS, EXPOSE)(p->db, 0);
5682 #undef SHELL_SUB_MACRO
5683 #undef SHELL_SUBMACRO
5685 #endif
5687 sqlite3_create_function(p->db, "strtod", 1, SQLITE_UTF8, 0,
5688 shellStrtod, 0, 0);
5689 sqlite3_create_function(p->db, "dtostr", 1, SQLITE_UTF8, 0,
5690 shellDtostr, 0, 0);
5691 sqlite3_create_function(p->db, "dtostr", 2, SQLITE_UTF8, 0,
5692 shellDtostr, 0, 0);
5693 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
5694 shellAddSchemaName, 0, 0);
5695 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
5696 shellModuleSchema, 0, 0);
5697 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
5698 shellPutsFunc, 0, 0);
5699 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
5700 shellUSleepFunc, 0, 0);
5701 #ifndef SQLITE_NOHAVE_SYSTEM
5702 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
5703 editFunc, 0, 0);
5704 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
5705 editFunc, 0, 0);
5706 #endif
5708 if( p->openMode==SHELL_OPEN_ZIPFILE ){
5709 char *zSql = sqlite3_mprintf(
5710 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
5711 shell_check_oom(zSql);
5712 sqlite3_exec(p->db, zSql, 0, 0, 0);
5713 sqlite3_free(zSql);
5715 #ifndef SQLITE_OMIT_DESERIALIZE
5716 else
5717 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
5718 int rc;
5719 int nData = 0;
5720 unsigned char *aData;
5721 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
5722 aData = (unsigned char*)readFile(zDbFilename, &nData);
5723 }else{
5724 aData = readHexDb(p, &nData);
5726 if( aData==0 ){
5727 return;
5729 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
5730 SQLITE_DESERIALIZE_RESIZEABLE |
5731 SQLITE_DESERIALIZE_FREEONCLOSE);
5732 if( rc ){
5733 sqlite3_fprintf(stderr,"Error: sqlite3_deserialize() returns %d\n", rc);
5735 if( p->szMax>0 ){
5736 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
5739 #endif
5741 if( p->db!=0 ){
5742 if( p->bSafeModePersist ){
5743 sqlite3_set_authorizer(p->db, safeModeAuth, p);
5745 sqlite3_db_config(
5746 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
5752 ** Attempt to close the database connection. Report errors.
5754 void close_db(sqlite3 *db){
5755 int rc = sqlite3_close(db);
5756 if( rc ){
5757 sqlite3_fprintf(stderr,
5758 "Error: sqlite3_close() returns %d: %s\n", rc, sqlite3_errmsg(db));
5762 #if HAVE_READLINE || HAVE_EDITLINE
5764 ** Readline completion callbacks
5766 static char *readline_completion_generator(const char *text, int state){
5767 static sqlite3_stmt *pStmt = 0;
5768 char *zRet;
5769 if( state==0 ){
5770 char *zSql;
5771 sqlite3_finalize(pStmt);
5772 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5773 " FROM completion(%Q) ORDER BY 1", text);
5774 shell_check_oom(zSql);
5775 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5776 sqlite3_free(zSql);
5778 if( sqlite3_step(pStmt)==SQLITE_ROW ){
5779 const char *z = (const char*)sqlite3_column_text(pStmt,0);
5780 zRet = z ? strdup(z) : 0;
5781 }else{
5782 sqlite3_finalize(pStmt);
5783 pStmt = 0;
5784 zRet = 0;
5786 return zRet;
5788 static char **readline_completion(const char *zText, int iStart, int iEnd){
5789 (void)iStart;
5790 (void)iEnd;
5791 rl_attempted_completion_over = 1;
5792 return rl_completion_matches(zText, readline_completion_generator);
5795 #elif HAVE_LINENOISE
5797 ** Linenoise completion callback. Note that the 3rd argument is from
5798 ** the "msteveb" version of linenoise, not the "antirez" version.
5800 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc,
5801 void *pUserData){
5802 i64 nLine = strlen(zLine);
5803 i64 i, iStart;
5804 sqlite3_stmt *pStmt = 0;
5805 char *zSql;
5806 char zBuf[1000];
5808 UNUSED_PARAMETER(pUserData);
5809 if( nLine>(i64)sizeof(zBuf)-30 ) return;
5810 if( zLine[0]=='.' || zLine[0]=='#') return;
5811 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
5812 if( i==nLine-1 ) return;
5813 iStart = i+1;
5814 memcpy(zBuf, zLine, iStart);
5815 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5816 " FROM completion(%Q,%Q) ORDER BY 1",
5817 &zLine[iStart], zLine);
5818 shell_check_oom(zSql);
5819 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
5820 sqlite3_free(zSql);
5821 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5822 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5823 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
5824 int nCompletion = sqlite3_column_bytes(pStmt, 0);
5825 if( iStart+nCompletion < (i64)sizeof(zBuf)-1 && zCompletion ){
5826 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
5827 linenoiseAddCompletion(lc, zBuf);
5830 sqlite3_finalize(pStmt);
5832 #endif
5835 ** Do C-language style dequoting.
5837 ** \a -> alarm
5838 ** \b -> backspace
5839 ** \t -> tab
5840 ** \n -> newline
5841 ** \v -> vertical tab
5842 ** \f -> form feed
5843 ** \r -> carriage return
5844 ** \s -> space
5845 ** \" -> "
5846 ** \' -> '
5847 ** \\ -> backslash
5848 ** \NNN -> ascii character NNN in octal
5849 ** \xHH -> ascii character HH in hexadecimal
5851 static void resolve_backslashes(char *z){
5852 int i, j;
5853 char c;
5854 while( *z && *z!='\\' ) z++;
5855 for(i=j=0; (c = z[i])!=0; i++, j++){
5856 if( c=='\\' && z[i+1]!=0 ){
5857 c = z[++i];
5858 if( c=='a' ){
5859 c = '\a';
5860 }else if( c=='b' ){
5861 c = '\b';
5862 }else if( c=='t' ){
5863 c = '\t';
5864 }else if( c=='n' ){
5865 c = '\n';
5866 }else if( c=='v' ){
5867 c = '\v';
5868 }else if( c=='f' ){
5869 c = '\f';
5870 }else if( c=='r' ){
5871 c = '\r';
5872 }else if( c=='"' ){
5873 c = '"';
5874 }else if( c=='\'' ){
5875 c = '\'';
5876 }else if( c=='\\' ){
5877 c = '\\';
5878 }else if( c=='x' ){
5879 int nhd = 0, hdv;
5880 u8 hv = 0;
5881 while( nhd<2 && (c=z[i+1+nhd])!=0 && (hdv=hexDigitValue(c))>=0 ){
5882 hv = (u8)((hv<<4)|hdv);
5883 ++nhd;
5885 i += nhd;
5886 c = (u8)hv;
5887 }else if( c>='0' && c<='7' ){
5888 c -= '0';
5889 if( z[i+1]>='0' && z[i+1]<='7' ){
5890 i++;
5891 c = (c<<3) + z[i] - '0';
5892 if( z[i+1]>='0' && z[i+1]<='7' ){
5893 i++;
5894 c = (c<<3) + z[i] - '0';
5899 z[j] = c;
5901 if( j<i ) z[j] = 0;
5905 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
5906 ** for TRUE and FALSE. Return the integer value if appropriate.
5908 static int booleanValue(const char *zArg){
5909 int i;
5910 if( zArg[0]=='0' && zArg[1]=='x' ){
5911 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
5912 }else{
5913 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
5915 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
5916 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
5917 return 1;
5919 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
5920 return 0;
5922 sqlite3_fprintf(stderr,
5923 "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg);
5924 return 0;
5928 ** Set or clear a shell flag according to a boolean value.
5930 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
5931 if( booleanValue(zArg) ){
5932 ShellSetFlag(p, mFlag);
5933 }else{
5934 ShellClearFlag(p, mFlag);
5939 ** Close an output file, assuming it is not stderr or stdout
5941 static void output_file_close(FILE *f){
5942 if( f && f!=stdout && f!=stderr ) fclose(f);
5946 ** Try to open an output file. The names "stdout" and "stderr" are
5947 ** recognized and do the right thing. NULL is returned if the output
5948 ** filename is "off".
5950 static FILE *output_file_open(const char *zFile){
5951 FILE *f;
5952 if( cli_strcmp(zFile,"stdout")==0 ){
5953 f = stdout;
5954 }else if( cli_strcmp(zFile, "stderr")==0 ){
5955 f = stderr;
5956 }else if( cli_strcmp(zFile, "off")==0 ){
5957 f = 0;
5958 }else{
5959 f = sqlite3_fopen(zFile, "w");
5960 if( f==0 ){
5961 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
5964 return f;
5967 #ifndef SQLITE_OMIT_TRACE
5969 ** A routine for handling output from sqlite3_trace().
5971 static int sql_trace_callback(
5972 unsigned mType, /* The trace type */
5973 void *pArg, /* The ShellState pointer */
5974 void *pP, /* Usually a pointer to sqlite_stmt */
5975 void *pX /* Auxiliary output */
5977 ShellState *p = (ShellState*)pArg;
5978 sqlite3_stmt *pStmt;
5979 const char *zSql;
5980 i64 nSql;
5981 if( p->traceOut==0 ) return 0;
5982 if( mType==SQLITE_TRACE_CLOSE ){
5983 sputz(p->traceOut, "-- closing database connection\n");
5984 return 0;
5986 if( mType!=SQLITE_TRACE_ROW && pX!=0 && ((const char*)pX)[0]=='-' ){
5987 zSql = (const char*)pX;
5988 }else{
5989 pStmt = (sqlite3_stmt*)pP;
5990 switch( p->eTraceType ){
5991 case SHELL_TRACE_EXPANDED: {
5992 zSql = sqlite3_expanded_sql(pStmt);
5993 break;
5995 #ifdef SQLITE_ENABLE_NORMALIZE
5996 case SHELL_TRACE_NORMALIZED: {
5997 zSql = sqlite3_normalized_sql(pStmt);
5998 break;
6000 #endif
6001 default: {
6002 zSql = sqlite3_sql(pStmt);
6003 break;
6007 if( zSql==0 ) return 0;
6008 nSql = strlen(zSql);
6009 if( nSql>1000000000 ) nSql = 1000000000;
6010 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
6011 switch( mType ){
6012 case SQLITE_TRACE_ROW:
6013 case SQLITE_TRACE_STMT: {
6014 sqlite3_fprintf(p->traceOut, "%.*s;\n", (int)nSql, zSql);
6015 break;
6017 case SQLITE_TRACE_PROFILE: {
6018 sqlite3_int64 nNanosec = pX ? *(sqlite3_int64*)pX : 0;
6019 sqlite3_fprintf(p->traceOut,
6020 "%.*s; -- %lld ns\n", (int)nSql, zSql, nNanosec);
6021 break;
6024 return 0;
6026 #endif
6029 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
6030 ** a useful spot to set a debugger breakpoint.
6032 ** This routine does not do anything practical. The code are there simply
6033 ** to prevent the compiler from optimizing this routine out.
6035 static void test_breakpoint(void){
6036 static unsigned int nCall = 0;
6037 if( (nCall++)==0xffffffff ) printf("Many .breakpoints have run\n");
6041 ** An object used to read a CSV and other files for import.
6043 typedef struct ImportCtx ImportCtx;
6044 struct ImportCtx {
6045 const char *zFile; /* Name of the input file */
6046 FILE *in; /* Read the CSV text from this input stream */
6047 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
6048 char *z; /* Accumulated text for a field */
6049 int n; /* Number of bytes in z */
6050 int nAlloc; /* Space allocated for z[] */
6051 int nLine; /* Current line number */
6052 int nRow; /* Number of rows imported */
6053 int nErr; /* Number of errors encountered */
6054 int bNotFirst; /* True if one or more bytes already read */
6055 int cTerm; /* Character that terminated the most recent field */
6056 int cColSep; /* The column separator character. (Usually ",") */
6057 int cRowSep; /* The row separator character. (Usually "\n") */
6060 /* Clean up resourced used by an ImportCtx */
6061 static void import_cleanup(ImportCtx *p){
6062 if( p->in!=0 && p->xCloser!=0 ){
6063 p->xCloser(p->in);
6064 p->in = 0;
6066 sqlite3_free(p->z);
6067 p->z = 0;
6070 /* Append a single byte to z[] */
6071 static void import_append_char(ImportCtx *p, int c){
6072 if( p->n+1>=p->nAlloc ){
6073 p->nAlloc += p->nAlloc + 100;
6074 p->z = sqlite3_realloc64(p->z, p->nAlloc);
6075 shell_check_oom(p->z);
6077 p->z[p->n++] = (char)c;
6080 /* Read a single field of CSV text. Compatible with rfc4180 and extended
6081 ** with the option of having a separator other than ",".
6083 ** + Input comes from p->in.
6084 ** + Store results in p->z of length p->n. Space to hold p->z comes
6085 ** from sqlite3_malloc64().
6086 ** + Use p->cSep as the column separator. The default is ",".
6087 ** + Use p->rSep as the row separator. The default is "\n".
6088 ** + Keep track of the line number in p->nLine.
6089 ** + Store the character that terminates the field in p->cTerm. Store
6090 ** EOF on end-of-file.
6091 ** + Report syntax errors on stderr
6093 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
6094 int c;
6095 int cSep = (u8)p->cColSep;
6096 int rSep = (u8)p->cRowSep;
6097 p->n = 0;
6098 c = fgetc(p->in);
6099 if( c==EOF || seenInterrupt ){
6100 p->cTerm = EOF;
6101 return 0;
6103 if( c=='"' ){
6104 int pc, ppc;
6105 int startLine = p->nLine;
6106 int cQuote = c;
6107 pc = ppc = 0;
6108 while( 1 ){
6109 c = fgetc(p->in);
6110 if( c==rSep ) p->nLine++;
6111 if( c==cQuote ){
6112 if( pc==cQuote ){
6113 pc = 0;
6114 continue;
6117 if( (c==cSep && pc==cQuote)
6118 || (c==rSep && pc==cQuote)
6119 || (c==rSep && pc=='\r' && ppc==cQuote)
6120 || (c==EOF && pc==cQuote)
6122 do{ p->n--; }while( p->z[p->n]!=cQuote );
6123 p->cTerm = c;
6124 break;
6126 if( pc==cQuote && c!='\r' ){
6127 sqlite3_fprintf(stderr,"%s:%d: unescaped %c character\n",
6128 p->zFile, p->nLine, cQuote);
6130 if( c==EOF ){
6131 sqlite3_fprintf(stderr,"%s:%d: unterminated %c-quoted field\n",
6132 p->zFile, startLine, cQuote);
6133 p->cTerm = c;
6134 break;
6136 import_append_char(p, c);
6137 ppc = pc;
6138 pc = c;
6140 }else{
6141 /* If this is the first field being parsed and it begins with the
6142 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
6143 if( (c&0xff)==0xef && p->bNotFirst==0 ){
6144 import_append_char(p, c);
6145 c = fgetc(p->in);
6146 if( (c&0xff)==0xbb ){
6147 import_append_char(p, c);
6148 c = fgetc(p->in);
6149 if( (c&0xff)==0xbf ){
6150 p->bNotFirst = 1;
6151 p->n = 0;
6152 return csv_read_one_field(p);
6156 while( c!=EOF && c!=cSep && c!=rSep ){
6157 import_append_char(p, c);
6158 c = fgetc(p->in);
6160 if( c==rSep ){
6161 p->nLine++;
6162 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
6164 p->cTerm = c;
6166 if( p->z ) p->z[p->n] = 0;
6167 p->bNotFirst = 1;
6168 return p->z;
6171 /* Read a single field of ASCII delimited text.
6173 ** + Input comes from p->in.
6174 ** + Store results in p->z of length p->n. Space to hold p->z comes
6175 ** from sqlite3_malloc64().
6176 ** + Use p->cSep as the column separator. The default is "\x1F".
6177 ** + Use p->rSep as the row separator. The default is "\x1E".
6178 ** + Keep track of the row number in p->nLine.
6179 ** + Store the character that terminates the field in p->cTerm. Store
6180 ** EOF on end-of-file.
6181 ** + Report syntax errors on stderr
6183 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
6184 int c;
6185 int cSep = (u8)p->cColSep;
6186 int rSep = (u8)p->cRowSep;
6187 p->n = 0;
6188 c = fgetc(p->in);
6189 if( c==EOF || seenInterrupt ){
6190 p->cTerm = EOF;
6191 return 0;
6193 while( c!=EOF && c!=cSep && c!=rSep ){
6194 import_append_char(p, c);
6195 c = fgetc(p->in);
6197 if( c==rSep ){
6198 p->nLine++;
6200 p->cTerm = c;
6201 if( p->z ) p->z[p->n] = 0;
6202 return p->z;
6206 ** Try to transfer data for table zTable. If an error is seen while
6207 ** moving forward, try to go backwards. The backwards movement won't
6208 ** work for WITHOUT ROWID tables.
6210 static void tryToCloneData(
6211 ShellState *p,
6212 sqlite3 *newDb,
6213 const char *zTable
6215 sqlite3_stmt *pQuery = 0;
6216 sqlite3_stmt *pInsert = 0;
6217 char *zQuery = 0;
6218 char *zInsert = 0;
6219 int rc;
6220 int i, j, n;
6221 int nTable = strlen30(zTable);
6222 int k = 0;
6223 int cnt = 0;
6224 const int spinRate = 10000;
6226 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
6227 shell_check_oom(zQuery);
6228 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6229 if( rc ){
6230 sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
6231 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
6232 goto end_data_xfer;
6234 n = sqlite3_column_count(pQuery);
6235 zInsert = sqlite3_malloc64(200 + nTable + n*3);
6236 shell_check_oom(zInsert);
6237 sqlite3_snprintf(200+nTable,zInsert,
6238 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
6239 i = strlen30(zInsert);
6240 for(j=1; j<n; j++){
6241 memcpy(zInsert+i, ",?", 2);
6242 i += 2;
6244 memcpy(zInsert+i, ");", 3);
6245 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
6246 if( rc ){
6247 sqlite3_fprintf(stderr,"Error %d: %s on [%s]\n",
6248 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb), zInsert);
6249 goto end_data_xfer;
6251 for(k=0; k<2; k++){
6252 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6253 for(i=0; i<n; i++){
6254 switch( sqlite3_column_type(pQuery, i) ){
6255 case SQLITE_NULL: {
6256 sqlite3_bind_null(pInsert, i+1);
6257 break;
6259 case SQLITE_INTEGER: {
6260 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
6261 break;
6263 case SQLITE_FLOAT: {
6264 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
6265 break;
6267 case SQLITE_TEXT: {
6268 sqlite3_bind_text(pInsert, i+1,
6269 (const char*)sqlite3_column_text(pQuery,i),
6270 -1, SQLITE_STATIC);
6271 break;
6273 case SQLITE_BLOB: {
6274 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
6275 sqlite3_column_bytes(pQuery,i),
6276 SQLITE_STATIC);
6277 break;
6280 } /* End for */
6281 rc = sqlite3_step(pInsert);
6282 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
6283 sqlite3_fprintf(stderr,"Error %d: %s\n",
6284 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb));
6286 sqlite3_reset(pInsert);
6287 cnt++;
6288 if( (cnt%spinRate)==0 ){
6289 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
6290 fflush(stdout);
6292 } /* End while */
6293 if( rc==SQLITE_DONE ) break;
6294 sqlite3_finalize(pQuery);
6295 sqlite3_free(zQuery);
6296 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
6297 zTable);
6298 shell_check_oom(zQuery);
6299 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6300 if( rc ){
6301 sqlite3_fprintf(stderr,"Warning: cannot step \"%s\" backwards", zTable);
6302 break;
6304 } /* End for(k=0...) */
6306 end_data_xfer:
6307 sqlite3_finalize(pQuery);
6308 sqlite3_finalize(pInsert);
6309 sqlite3_free(zQuery);
6310 sqlite3_free(zInsert);
6315 ** Try to transfer all rows of the schema that match zWhere. For
6316 ** each row, invoke xForEach() on the object defined by that row.
6317 ** If an error is encountered while moving forward through the
6318 ** sqlite_schema table, try again moving backwards.
6320 static void tryToCloneSchema(
6321 ShellState *p,
6322 sqlite3 *newDb,
6323 const char *zWhere,
6324 void (*xForEach)(ShellState*,sqlite3*,const char*)
6326 sqlite3_stmt *pQuery = 0;
6327 char *zQuery = 0;
6328 int rc;
6329 const unsigned char *zName;
6330 const unsigned char *zSql;
6331 char *zErrMsg = 0;
6333 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6334 " WHERE %s ORDER BY rowid ASC", zWhere);
6335 shell_check_oom(zQuery);
6336 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6337 if( rc ){
6338 sqlite3_fprintf(stderr,
6339 "Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p->db),
6340 sqlite3_errmsg(p->db), zQuery);
6341 goto end_schema_xfer;
6343 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
6344 zName = sqlite3_column_text(pQuery, 0);
6345 zSql = sqlite3_column_text(pQuery, 1);
6346 if( zName==0 || zSql==0 ) continue;
6347 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")!=0 ){
6348 sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
6349 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6350 if( zErrMsg ){
6351 sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6352 sqlite3_free(zErrMsg);
6353 zErrMsg = 0;
6356 if( xForEach ){
6357 xForEach(p, newDb, (const char*)zName);
6359 sputz(stdout, "done\n");
6361 if( rc!=SQLITE_DONE ){
6362 sqlite3_finalize(pQuery);
6363 sqlite3_free(zQuery);
6364 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6365 " WHERE %s ORDER BY rowid DESC", zWhere);
6366 shell_check_oom(zQuery);
6367 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
6368 if( rc ){
6369 sqlite3_fprintf(stderr,"Error: (%d) %s on [%s]\n",
6370 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db), zQuery);
6371 goto end_schema_xfer;
6373 while( sqlite3_step(pQuery)==SQLITE_ROW ){
6374 zName = sqlite3_column_text(pQuery, 0);
6375 zSql = sqlite3_column_text(pQuery, 1);
6376 if( zName==0 || zSql==0 ) continue;
6377 if( sqlite3_stricmp((char*)zName, "sqlite_sequence")==0 ) continue;
6378 sqlite3_fprintf(stdout, "%s... ", zName); fflush(stdout);
6379 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
6380 if( zErrMsg ){
6381 sqlite3_fprintf(stderr,"Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
6382 sqlite3_free(zErrMsg);
6383 zErrMsg = 0;
6385 if( xForEach ){
6386 xForEach(p, newDb, (const char*)zName);
6388 sputz(stdout, "done\n");
6391 end_schema_xfer:
6392 sqlite3_finalize(pQuery);
6393 sqlite3_free(zQuery);
6397 ** Open a new database file named "zNewDb". Try to recover as much information
6398 ** as possible out of the main database (which might be corrupt) and write it
6399 ** into zNewDb.
6401 static void tryToClone(ShellState *p, const char *zNewDb){
6402 int rc;
6403 sqlite3 *newDb = 0;
6404 if( access(zNewDb,0)==0 ){
6405 sqlite3_fprintf(stderr,"File \"%s\" already exists.\n", zNewDb);
6406 return;
6408 rc = sqlite3_open(zNewDb, &newDb);
6409 if( rc ){
6410 sqlite3_fprintf(stderr,
6411 "Cannot create output database: %s\n", sqlite3_errmsg(newDb));
6412 }else{
6413 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
6414 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
6415 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
6416 tryToCloneSchema(p, newDb, "type!='table'", 0);
6417 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
6418 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
6420 close_db(newDb);
6423 #ifndef SQLITE_SHELL_FIDDLE
6425 ** Change the output stream (file or pipe or console) to something else.
6427 static void output_redir(ShellState *p, FILE *pfNew){
6428 if( p->out != stdout ){
6429 sqlite3_fputs("Output already redirected.\n", stderr);
6430 }else{
6431 p->out = pfNew;
6432 setCrlfMode(p);
6433 if( p->mode==MODE_Www ){
6434 sqlite3_fputs(
6435 "<!DOCTYPE html>\n"
6436 "<HTML><BODY><PRE>\n",
6437 p->out
6444 ** Change the output file back to stdout.
6446 ** If the p->doXdgOpen flag is set, that means the output was being
6447 ** redirected to a temporary file named by p->zTempFile. In that case,
6448 ** launch start/open/xdg-open on that temporary file.
6450 static void output_reset(ShellState *p){
6451 if( p->outfile[0]=='|' ){
6452 #ifndef SQLITE_OMIT_POPEN
6453 pclose(p->out);
6454 #endif
6455 }else{
6456 if( p->mode==MODE_Www ){
6457 sqlite3_fputs("</PRE></BODY></HTML>\n", p->out);
6459 output_file_close(p->out);
6460 #ifndef SQLITE_NOHAVE_SYSTEM
6461 if( p->doXdgOpen ){
6462 const char *zXdgOpenCmd =
6463 #if defined(_WIN32)
6464 "start";
6465 #elif defined(__APPLE__)
6466 "open";
6467 #else
6468 "xdg-open";
6469 #endif
6470 char *zCmd;
6471 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
6472 if( system(zCmd) ){
6473 sqlite3_fprintf(stderr,"Failed: [%s]\n", zCmd);
6474 }else{
6475 /* Give the start/open/xdg-open command some time to get
6476 ** going before we continue, and potential delete the
6477 ** p->zTempFile data file out from under it */
6478 sqlite3_sleep(2000);
6480 sqlite3_free(zCmd);
6481 outputModePop(p);
6482 p->doXdgOpen = 0;
6484 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
6486 p->outfile[0] = 0;
6487 p->out = stdout;
6488 setCrlfMode(p);
6490 #else
6491 # define output_redir(SS,pfO)
6492 # define output_reset(SS)
6493 #endif
6496 ** Run an SQL command and return the single integer result.
6498 static int db_int(sqlite3 *db, const char *zSql){
6499 sqlite3_stmt *pStmt;
6500 int res = 0;
6501 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
6502 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
6503 res = sqlite3_column_int(pStmt,0);
6505 sqlite3_finalize(pStmt);
6506 return res;
6509 #if SQLITE_SHELL_HAVE_RECOVER
6511 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
6513 static unsigned int get2byteInt(unsigned char *a){
6514 return (a[0]<<8) + a[1];
6516 static unsigned int get4byteInt(unsigned char *a){
6517 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
6521 ** Implementation of the ".dbinfo" command.
6523 ** Return 1 on error, 2 to exit, and 0 otherwise.
6525 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
6526 static const struct { const char *zName; int ofst; } aField[] = {
6527 { "file change counter:", 24 },
6528 { "database page count:", 28 },
6529 { "freelist page count:", 36 },
6530 { "schema cookie:", 40 },
6531 { "schema format:", 44 },
6532 { "default cache size:", 48 },
6533 { "autovacuum top root:", 52 },
6534 { "incremental vacuum:", 64 },
6535 { "text encoding:", 56 },
6536 { "user version:", 60 },
6537 { "application id:", 68 },
6538 { "software version:", 96 },
6540 static const struct { const char *zName; const char *zSql; } aQuery[] = {
6541 { "number of tables:",
6542 "SELECT count(*) FROM %s WHERE type='table'" },
6543 { "number of indexes:",
6544 "SELECT count(*) FROM %s WHERE type='index'" },
6545 { "number of triggers:",
6546 "SELECT count(*) FROM %s WHERE type='trigger'" },
6547 { "number of views:",
6548 "SELECT count(*) FROM %s WHERE type='view'" },
6549 { "schema size:",
6550 "SELECT total(length(sql)) FROM %s" },
6552 int i, rc;
6553 unsigned iDataVersion;
6554 char *zSchemaTab;
6555 char *zDb = nArg>=2 ? azArg[1] : "main";
6556 sqlite3_stmt *pStmt = 0;
6557 unsigned char aHdr[100];
6558 open_db(p, 0);
6559 if( p->db==0 ) return 1;
6560 rc = sqlite3_prepare_v2(p->db,
6561 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
6562 -1, &pStmt, 0);
6563 if( rc ){
6564 sqlite3_fprintf(stderr,"error: %s\n", sqlite3_errmsg(p->db));
6565 sqlite3_finalize(pStmt);
6566 return 1;
6568 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
6569 if( sqlite3_step(pStmt)==SQLITE_ROW
6570 && sqlite3_column_bytes(pStmt,0)>100
6572 const u8 *pb = sqlite3_column_blob(pStmt,0);
6573 shell_check_oom(pb);
6574 memcpy(aHdr, pb, 100);
6575 sqlite3_finalize(pStmt);
6576 }else{
6577 sqlite3_fputs("unable to read database header\n", stderr);
6578 sqlite3_finalize(pStmt);
6579 return 1;
6581 i = get2byteInt(aHdr+16);
6582 if( i==1 ) i = 65536;
6583 sqlite3_fprintf(p->out, "%-20s %d\n", "database page size:", i);
6584 sqlite3_fprintf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
6585 sqlite3_fprintf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
6586 sqlite3_fprintf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
6587 for(i=0; i<ArraySize(aField); i++){
6588 int ofst = aField[i].ofst;
6589 unsigned int val = get4byteInt(aHdr + ofst);
6590 sqlite3_fprintf(p->out, "%-20s %u", aField[i].zName, val);
6591 switch( ofst ){
6592 case 56: {
6593 if( val==1 ) sqlite3_fputs(" (utf8)", p->out);
6594 if( val==2 ) sqlite3_fputs(" (utf16le)", p->out);
6595 if( val==3 ) sqlite3_fputs(" (utf16be)", p->out);
6598 sqlite3_fputs("\n", p->out);
6600 if( zDb==0 ){
6601 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
6602 }else if( cli_strcmp(zDb,"temp")==0 ){
6603 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
6604 }else{
6605 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
6607 for(i=0; i<ArraySize(aQuery); i++){
6608 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
6609 int val = db_int(p->db, zSql);
6610 sqlite3_free(zSql);
6611 sqlite3_fprintf(p->out, "%-20s %d\n", aQuery[i].zName, val);
6613 sqlite3_free(zSchemaTab);
6614 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
6615 sqlite3_fprintf(p->out, "%-20s %u\n", "data version", iDataVersion);
6616 return 0;
6618 #endif /* SQLITE_SHELL_HAVE_RECOVER */
6621 ** Print the given string as an error message.
6623 static void shellEmitError(const char *zErr){
6624 sqlite3_fprintf(stderr,"Error: %s\n", zErr);
6627 ** Print the current sqlite3_errmsg() value to stderr and return 1.
6629 static int shellDatabaseError(sqlite3 *db){
6630 shellEmitError(sqlite3_errmsg(db));
6631 return 1;
6635 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
6636 ** if they match and FALSE (0) if they do not match.
6638 ** Globbing rules:
6640 ** '*' Matches any sequence of zero or more characters.
6642 ** '?' Matches exactly one character.
6644 ** [...] Matches one character from the enclosed list of
6645 ** characters.
6647 ** [^...] Matches one character not in the enclosed list.
6649 ** '#' Matches any sequence of one or more digits with an
6650 ** optional + or - sign in front
6652 ** ' ' Any span of whitespace matches any other span of
6653 ** whitespace.
6655 ** Extra whitespace at the end of z[] is ignored.
6657 static int testcase_glob(const char *zGlob, const char *z){
6658 int c, c2;
6659 int invert;
6660 int seen;
6662 while( (c = (*(zGlob++)))!=0 ){
6663 if( IsSpace(c) ){
6664 if( !IsSpace(*z) ) return 0;
6665 while( IsSpace(*zGlob) ) zGlob++;
6666 while( IsSpace(*z) ) z++;
6667 }else if( c=='*' ){
6668 while( (c=(*(zGlob++))) == '*' || c=='?' ){
6669 if( c=='?' && (*(z++))==0 ) return 0;
6671 if( c==0 ){
6672 return 1;
6673 }else if( c=='[' ){
6674 while( *z && testcase_glob(zGlob-1,z)==0 ){
6675 z++;
6677 return (*z)!=0;
6679 while( (c2 = (*(z++)))!=0 ){
6680 while( c2!=c ){
6681 c2 = *(z++);
6682 if( c2==0 ) return 0;
6684 if( testcase_glob(zGlob,z) ) return 1;
6686 return 0;
6687 }else if( c=='?' ){
6688 if( (*(z++))==0 ) return 0;
6689 }else if( c=='[' ){
6690 int prior_c = 0;
6691 seen = 0;
6692 invert = 0;
6693 c = *(z++);
6694 if( c==0 ) return 0;
6695 c2 = *(zGlob++);
6696 if( c2=='^' ){
6697 invert = 1;
6698 c2 = *(zGlob++);
6700 if( c2==']' ){
6701 if( c==']' ) seen = 1;
6702 c2 = *(zGlob++);
6704 while( c2 && c2!=']' ){
6705 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
6706 c2 = *(zGlob++);
6707 if( c>=prior_c && c<=c2 ) seen = 1;
6708 prior_c = 0;
6709 }else{
6710 if( c==c2 ){
6711 seen = 1;
6713 prior_c = c2;
6715 c2 = *(zGlob++);
6717 if( c2==0 || (seen ^ invert)==0 ) return 0;
6718 }else if( c=='#' ){
6719 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
6720 if( !IsDigit(z[0]) ) return 0;
6721 z++;
6722 while( IsDigit(z[0]) ){ z++; }
6723 }else{
6724 if( c!=(*(z++)) ) return 0;
6727 while( IsSpace(*z) ){ z++; }
6728 return *z==0;
6733 ** Compare the string as a command-line option with either one or two
6734 ** initial "-" characters.
6736 static int optionMatch(const char *zStr, const char *zOpt){
6737 if( zStr[0]!='-' ) return 0;
6738 zStr++;
6739 if( zStr[0]=='-' ) zStr++;
6740 return cli_strcmp(zStr, zOpt)==0;
6744 ** Delete a file.
6746 int shellDeleteFile(const char *zFilename){
6747 int rc;
6748 #ifdef _WIN32
6749 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
6750 rc = _wunlink(z);
6751 sqlite3_free(z);
6752 #else
6753 rc = unlink(zFilename);
6754 #endif
6755 return rc;
6759 ** Try to delete the temporary file (if there is one) and free the
6760 ** memory used to hold the name of the temp file.
6762 static void clearTempFile(ShellState *p){
6763 if( p->zTempFile==0 ) return;
6764 if( p->doXdgOpen ) return;
6765 if( shellDeleteFile(p->zTempFile) ) return;
6766 sqlite3_free(p->zTempFile);
6767 p->zTempFile = 0;
6771 ** Create a new temp file name with the given suffix.
6773 static void newTempFile(ShellState *p, const char *zSuffix){
6774 clearTempFile(p);
6775 sqlite3_free(p->zTempFile);
6776 p->zTempFile = 0;
6777 if( p->db ){
6778 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
6780 if( p->zTempFile==0 ){
6781 /* If p->db is an in-memory database then the TEMPFILENAME file-control
6782 ** will not work and we will need to fallback to guessing */
6783 char *zTemp;
6784 sqlite3_uint64 r;
6785 sqlite3_randomness(sizeof(r), &r);
6786 zTemp = getenv("TEMP");
6787 if( zTemp==0 ) zTemp = getenv("TMP");
6788 if( zTemp==0 ){
6789 #ifdef _WIN32
6790 zTemp = "\\tmp";
6791 #else
6792 zTemp = "/tmp";
6793 #endif
6795 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
6796 }else{
6797 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
6799 shell_check_oom(p->zTempFile);
6804 ** The implementation of SQL scalar function fkey_collate_clause(), used
6805 ** by the ".lint fkey-indexes" command. This scalar function is always
6806 ** called with four arguments - the parent table name, the parent column name,
6807 ** the child table name and the child column name.
6809 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6811 ** If either of the named tables or columns do not exist, this function
6812 ** returns an empty string. An empty string is also returned if both tables
6813 ** and columns exist but have the same default collation sequence. Or,
6814 ** if both exist but the default collation sequences are different, this
6815 ** function returns the string " COLLATE <parent-collation>", where
6816 ** <parent-collation> is the default collation sequence of the parent column.
6818 static void shellFkeyCollateClause(
6819 sqlite3_context *pCtx,
6820 int nVal,
6821 sqlite3_value **apVal
6823 sqlite3 *db = sqlite3_context_db_handle(pCtx);
6824 const char *zParent;
6825 const char *zParentCol;
6826 const char *zParentSeq;
6827 const char *zChild;
6828 const char *zChildCol;
6829 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
6830 int rc;
6832 assert( nVal==4 );
6833 zParent = (const char*)sqlite3_value_text(apVal[0]);
6834 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
6835 zChild = (const char*)sqlite3_value_text(apVal[2]);
6836 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
6838 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
6839 rc = sqlite3_table_column_metadata(
6840 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
6842 if( rc==SQLITE_OK ){
6843 rc = sqlite3_table_column_metadata(
6844 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
6848 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
6849 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
6850 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
6851 sqlite3_free(z);
6857 ** The implementation of dot-command ".lint fkey-indexes".
6859 static int lintFkeyIndexes(
6860 ShellState *pState, /* Current shell tool state */
6861 char **azArg, /* Array of arguments passed to dot command */
6862 int nArg /* Number of entries in azArg[] */
6864 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
6865 int bVerbose = 0; /* If -verbose is present */
6866 int bGroupByParent = 0; /* If -groupbyparent is present */
6867 int i; /* To iterate through azArg[] */
6868 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
6869 int rc; /* Return code */
6870 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
6871 FILE *out = pState->out; /* Send output here */
6874 ** This SELECT statement returns one row for each foreign key constraint
6875 ** in the schema of the main database. The column values are:
6877 ** 0. The text of an SQL statement similar to:
6879 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6881 ** This SELECT is similar to the one that the foreign keys implementation
6882 ** needs to run internally on child tables. If there is an index that can
6883 ** be used to optimize this query, then it can also be used by the FK
6884 ** implementation to optimize DELETE or UPDATE statements on the parent
6885 ** table.
6887 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6888 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6889 ** contains an index that can be used to optimize the query.
6891 ** 2. Human readable text that describes the child table and columns. e.g.
6893 ** "child_table(child_key1, child_key2)"
6895 ** 3. Human readable text that describes the parent table and columns. e.g.
6897 ** "parent_table(parent_key1, parent_key2)"
6899 ** 4. A full CREATE INDEX statement for an index that could be used to
6900 ** optimize DELETE or UPDATE statements on the parent table. e.g.
6902 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
6904 ** 5. The name of the parent table.
6906 ** These six values are used by the C logic below to generate the report.
6908 const char *zSql =
6909 "SELECT "
6910 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6911 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6912 " || fkey_collate_clause("
6913 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6914 ", "
6915 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6916 " || group_concat('*=?', ' AND ') || ')'"
6917 ", "
6918 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
6919 ", "
6920 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6921 ", "
6922 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6923 " || ' ON ' || quote(s.name) || '('"
6924 " || group_concat(quote(f.[from]) ||"
6925 " fkey_collate_clause("
6926 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6927 " || ');'"
6928 ", "
6929 " f.[table] "
6930 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6931 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6932 "GROUP BY s.name, f.id "
6933 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6935 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6937 for(i=2; i<nArg; i++){
6938 int n = strlen30(azArg[i]);
6939 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
6940 bVerbose = 1;
6942 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
6943 bGroupByParent = 1;
6944 zIndent = " ";
6946 else{
6947 sqlite3_fprintf(stderr,
6948 "Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg[0], azArg[1]);
6949 return SQLITE_ERROR;
6953 /* Register the fkey_collate_clause() SQL function */
6954 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
6955 0, shellFkeyCollateClause, 0, 0
6959 if( rc==SQLITE_OK ){
6960 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
6962 if( rc==SQLITE_OK ){
6963 sqlite3_bind_int(pSql, 1, bGroupByParent);
6966 if( rc==SQLITE_OK ){
6967 int rc2;
6968 char *zPrev = 0;
6969 while( SQLITE_ROW==sqlite3_step(pSql) ){
6970 int res = -1;
6971 sqlite3_stmt *pExplain = 0;
6972 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
6973 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
6974 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
6975 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
6976 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
6977 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
6979 if( zEQP==0 ) continue;
6980 if( zGlob==0 ) continue;
6981 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
6982 if( rc!=SQLITE_OK ) break;
6983 if( SQLITE_ROW==sqlite3_step(pExplain) ){
6984 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
6985 res = zPlan!=0 && ( 0==sqlite3_strglob(zGlob, zPlan)
6986 || 0==sqlite3_strglob(zGlobIPK, zPlan));
6988 rc = sqlite3_finalize(pExplain);
6989 if( rc!=SQLITE_OK ) break;
6991 if( res<0 ){
6992 sqlite3_fputs("Error: internal error", stderr);
6993 break;
6994 }else{
6995 if( bGroupByParent
6996 && (bVerbose || res==0)
6997 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
6999 sqlite3_fprintf(out, "-- Parent table %s\n", zParent);
7000 sqlite3_free(zPrev);
7001 zPrev = sqlite3_mprintf("%s", zParent);
7004 if( res==0 ){
7005 sqlite3_fprintf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
7006 }else if( bVerbose ){
7007 sqlite3_fprintf(out,
7008 "%s/* no extra indexes required for %s -> %s */\n",
7009 zIndent, zFrom, zTarget
7014 sqlite3_free(zPrev);
7016 if( rc!=SQLITE_OK ){
7017 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
7020 rc2 = sqlite3_finalize(pSql);
7021 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
7022 rc = rc2;
7023 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
7025 }else{
7026 sqlite3_fprintf(stderr,"%s\n", sqlite3_errmsg(db));
7029 return rc;
7033 ** Implementation of ".lint" dot command.
7035 static int lintDotCommand(
7036 ShellState *pState, /* Current shell tool state */
7037 char **azArg, /* Array of arguments passed to dot command */
7038 int nArg /* Number of entries in azArg[] */
7040 int n;
7041 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
7042 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
7043 return lintFkeyIndexes(pState, azArg, nArg);
7045 usage:
7046 sqlite3_fprintf(stderr,"Usage %s sub-command ?switches...?\n", azArg[0]);
7047 sqlite3_fprintf(stderr, "Where sub-commands are:\n");
7048 sqlite3_fprintf(stderr, " fkey-indexes\n");
7049 return SQLITE_ERROR;
7052 static void shellPrepare(
7053 sqlite3 *db,
7054 int *pRc,
7055 const char *zSql,
7056 sqlite3_stmt **ppStmt
7058 *ppStmt = 0;
7059 if( *pRc==SQLITE_OK ){
7060 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
7061 if( rc!=SQLITE_OK ){
7062 sqlite3_fprintf(stderr,
7063 "sql error: %s (%d)\n", sqlite3_errmsg(db), sqlite3_errcode(db));
7064 *pRc = rc;
7070 ** Create a prepared statement using printf-style arguments for the SQL.
7072 static void shellPreparePrintf(
7073 sqlite3 *db,
7074 int *pRc,
7075 sqlite3_stmt **ppStmt,
7076 const char *zFmt,
7079 *ppStmt = 0;
7080 if( *pRc==SQLITE_OK ){
7081 va_list ap;
7082 char *z;
7083 va_start(ap, zFmt);
7084 z = sqlite3_vmprintf(zFmt, ap);
7085 va_end(ap);
7086 if( z==0 ){
7087 *pRc = SQLITE_NOMEM;
7088 }else{
7089 shellPrepare(db, pRc, z, ppStmt);
7090 sqlite3_free(z);
7096 ** Finalize the prepared statement created using shellPreparePrintf().
7098 static void shellFinalize(
7099 int *pRc,
7100 sqlite3_stmt *pStmt
7102 if( pStmt ){
7103 sqlite3 *db = sqlite3_db_handle(pStmt);
7104 int rc = sqlite3_finalize(pStmt);
7105 if( *pRc==SQLITE_OK ){
7106 if( rc!=SQLITE_OK ){
7107 sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
7109 *pRc = rc;
7114 #if !defined SQLITE_OMIT_VIRTUALTABLE
7115 /* Reset the prepared statement created using shellPreparePrintf().
7117 ** This routine is could be marked "static". But it is not always used,
7118 ** depending on compile-time options. By omitting the "static", we avoid
7119 ** nuisance compiler warnings about "defined but not used".
7121 void shellReset(
7122 int *pRc,
7123 sqlite3_stmt *pStmt
7125 int rc = sqlite3_reset(pStmt);
7126 if( *pRc==SQLITE_OK ){
7127 if( rc!=SQLITE_OK ){
7128 sqlite3 *db = sqlite3_db_handle(pStmt);
7129 sqlite3_fprintf(stderr,"SQL error: %s\n", sqlite3_errmsg(db));
7131 *pRc = rc;
7134 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
7136 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7137 /******************************************************************************
7138 ** The ".archive" or ".ar" command.
7141 ** Structure representing a single ".ar" command.
7143 typedef struct ArCommand ArCommand;
7144 struct ArCommand {
7145 u8 eCmd; /* An AR_CMD_* value */
7146 u8 bVerbose; /* True if --verbose */
7147 u8 bZip; /* True if the archive is a ZIP */
7148 u8 bDryRun; /* True if --dry-run */
7149 u8 bAppend; /* True if --append */
7150 u8 bGlob; /* True if --glob */
7151 u8 fromCmdLine; /* Run from -A instead of .archive */
7152 int nArg; /* Number of command arguments */
7153 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
7154 const char *zFile; /* --file argument, or NULL */
7155 const char *zDir; /* --directory argument, or NULL */
7156 char **azArg; /* Array of command arguments */
7157 ShellState *p; /* Shell state */
7158 FILE *out; /* Output to this stream */
7159 sqlite3 *db; /* Database containing the archive */
7163 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
7165 static int arUsage(FILE *f){
7166 showHelp(f,"archive");
7167 return SQLITE_ERROR;
7171 ** Print an error message for the .ar command to stderr and return
7172 ** SQLITE_ERROR.
7174 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
7175 va_list ap;
7176 char *z;
7177 va_start(ap, zFmt);
7178 z = sqlite3_vmprintf(zFmt, ap);
7179 va_end(ap);
7180 shellEmitError(z);
7181 if( pAr->fromCmdLine ){
7182 sqlite3_fputs("Use \"-A\" for more help\n", stderr);
7183 }else{
7184 sqlite3_fputs("Use \".archive --help\" for more help\n", stderr);
7186 sqlite3_free(z);
7187 return SQLITE_ERROR;
7191 ** Values for ArCommand.eCmd.
7193 #define AR_CMD_CREATE 1
7194 #define AR_CMD_UPDATE 2
7195 #define AR_CMD_INSERT 3
7196 #define AR_CMD_EXTRACT 4
7197 #define AR_CMD_LIST 5
7198 #define AR_CMD_HELP 6
7199 #define AR_CMD_REMOVE 7
7202 ** Other (non-command) switches.
7204 #define AR_SWITCH_VERBOSE 8
7205 #define AR_SWITCH_FILE 9
7206 #define AR_SWITCH_DIRECTORY 10
7207 #define AR_SWITCH_APPEND 11
7208 #define AR_SWITCH_DRYRUN 12
7209 #define AR_SWITCH_GLOB 13
7211 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
7212 switch( eSwitch ){
7213 case AR_CMD_CREATE:
7214 case AR_CMD_EXTRACT:
7215 case AR_CMD_LIST:
7216 case AR_CMD_REMOVE:
7217 case AR_CMD_UPDATE:
7218 case AR_CMD_INSERT:
7219 case AR_CMD_HELP:
7220 if( pAr->eCmd ){
7221 return arErrorMsg(pAr, "multiple command options");
7223 pAr->eCmd = eSwitch;
7224 break;
7226 case AR_SWITCH_DRYRUN:
7227 pAr->bDryRun = 1;
7228 break;
7229 case AR_SWITCH_GLOB:
7230 pAr->bGlob = 1;
7231 break;
7232 case AR_SWITCH_VERBOSE:
7233 pAr->bVerbose = 1;
7234 break;
7235 case AR_SWITCH_APPEND:
7236 pAr->bAppend = 1;
7237 deliberate_fall_through;
7238 case AR_SWITCH_FILE:
7239 pAr->zFile = zArg;
7240 break;
7241 case AR_SWITCH_DIRECTORY:
7242 pAr->zDir = zArg;
7243 break;
7246 return SQLITE_OK;
7250 ** Parse the command line for an ".ar" command. The results are written into
7251 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
7252 ** successfully, otherwise an error message is written to stderr and
7253 ** SQLITE_ERROR returned.
7255 static int arParseCommand(
7256 char **azArg, /* Array of arguments passed to dot command */
7257 int nArg, /* Number of entries in azArg[] */
7258 ArCommand *pAr /* Populate this object */
7260 struct ArSwitch {
7261 const char *zLong;
7262 char cShort;
7263 u8 eSwitch;
7264 u8 bArg;
7265 } aSwitch[] = {
7266 { "create", 'c', AR_CMD_CREATE, 0 },
7267 { "extract", 'x', AR_CMD_EXTRACT, 0 },
7268 { "insert", 'i', AR_CMD_INSERT, 0 },
7269 { "list", 't', AR_CMD_LIST, 0 },
7270 { "remove", 'r', AR_CMD_REMOVE, 0 },
7271 { "update", 'u', AR_CMD_UPDATE, 0 },
7272 { "help", 'h', AR_CMD_HELP, 0 },
7273 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
7274 { "file", 'f', AR_SWITCH_FILE, 1 },
7275 { "append", 'a', AR_SWITCH_APPEND, 1 },
7276 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
7277 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
7278 { "glob", 'g', AR_SWITCH_GLOB, 0 },
7280 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
7281 struct ArSwitch *pEnd = &aSwitch[nSwitch];
7283 if( nArg<=1 ){
7284 sqlite3_fprintf(stderr, "Wrong number of arguments. Usage:\n");
7285 return arUsage(stderr);
7286 }else{
7287 char *z = azArg[1];
7288 if( z[0]!='-' ){
7289 /* Traditional style [tar] invocation */
7290 int i;
7291 int iArg = 2;
7292 for(i=0; z[i]; i++){
7293 const char *zArg = 0;
7294 struct ArSwitch *pOpt;
7295 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7296 if( z[i]==pOpt->cShort ) break;
7298 if( pOpt==pEnd ){
7299 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7301 if( pOpt->bArg ){
7302 if( iArg>=nArg ){
7303 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
7305 zArg = azArg[iArg++];
7307 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7309 pAr->nArg = nArg-iArg;
7310 if( pAr->nArg>0 ){
7311 pAr->azArg = &azArg[iArg];
7313 }else{
7314 /* Non-traditional invocation */
7315 int iArg;
7316 for(iArg=1; iArg<nArg; iArg++){
7317 int n;
7318 z = azArg[iArg];
7319 if( z[0]!='-' ){
7320 /* All remaining command line words are command arguments. */
7321 pAr->azArg = &azArg[iArg];
7322 pAr->nArg = nArg-iArg;
7323 break;
7325 n = strlen30(z);
7327 if( z[1]!='-' ){
7328 int i;
7329 /* One or more short options */
7330 for(i=1; i<n; i++){
7331 const char *zArg = 0;
7332 struct ArSwitch *pOpt;
7333 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7334 if( z[i]==pOpt->cShort ) break;
7336 if( pOpt==pEnd ){
7337 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
7339 if( pOpt->bArg ){
7340 if( i<(n-1) ){
7341 zArg = &z[i+1];
7342 i = n;
7343 }else{
7344 if( iArg>=(nArg-1) ){
7345 return arErrorMsg(pAr, "option requires an argument: %c",
7346 z[i]);
7348 zArg = azArg[++iArg];
7351 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
7353 }else if( z[2]=='\0' ){
7354 /* A -- option, indicating that all remaining command line words
7355 ** are command arguments. */
7356 pAr->azArg = &azArg[iArg+1];
7357 pAr->nArg = nArg-iArg-1;
7358 break;
7359 }else{
7360 /* A long option */
7361 const char *zArg = 0; /* Argument for option, if any */
7362 struct ArSwitch *pMatch = 0; /* Matching option */
7363 struct ArSwitch *pOpt; /* Iterator */
7364 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
7365 const char *zLong = pOpt->zLong;
7366 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
7367 if( pMatch ){
7368 return arErrorMsg(pAr, "ambiguous option: %s",z);
7369 }else{
7370 pMatch = pOpt;
7375 if( pMatch==0 ){
7376 return arErrorMsg(pAr, "unrecognized option: %s", z);
7378 if( pMatch->bArg ){
7379 if( iArg>=(nArg-1) ){
7380 return arErrorMsg(pAr, "option requires an argument: %s", z);
7382 zArg = azArg[++iArg];
7384 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
7389 if( pAr->eCmd==0 ){
7390 sqlite3_fprintf(stderr, "Required argument missing. Usage:\n");
7391 return arUsage(stderr);
7393 return SQLITE_OK;
7397 ** This function assumes that all arguments within the ArCommand.azArg[]
7398 ** array refer to archive members, as for the --extract, --list or --remove
7399 ** commands. It checks that each of them are "present". If any specified
7400 ** file is not present in the archive, an error is printed to stderr and an
7401 ** error code returned. Otherwise, if all specified arguments are present
7402 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
7403 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
7404 ** when pAr->bGlob is true.
7406 ** This function strips any trailing '/' characters from each argument.
7407 ** This is consistent with the way the [tar] command seems to work on
7408 ** Linux.
7410 static int arCheckEntries(ArCommand *pAr){
7411 int rc = SQLITE_OK;
7412 if( pAr->nArg ){
7413 int i, j;
7414 sqlite3_stmt *pTest = 0;
7415 const char *zSel = (pAr->bGlob)
7416 ? "SELECT name FROM %s WHERE glob($name,name)"
7417 : "SELECT name FROM %s WHERE name=$name";
7419 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
7420 j = sqlite3_bind_parameter_index(pTest, "$name");
7421 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7422 char *z = pAr->azArg[i];
7423 int n = strlen30(z);
7424 int bOk = 0;
7425 while( n>0 && z[n-1]=='/' ) n--;
7426 z[n] = '\0';
7427 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
7428 if( SQLITE_ROW==sqlite3_step(pTest) ){
7429 bOk = 1;
7431 shellReset(&rc, pTest);
7432 if( rc==SQLITE_OK && bOk==0 ){
7433 sqlite3_fprintf(stderr,"not found in archive: %s\n", z);
7434 rc = SQLITE_ERROR;
7437 shellFinalize(&rc, pTest);
7439 return rc;
7443 ** Format a WHERE clause that can be used against the "sqlar" table to
7444 ** identify all archive members that match the command arguments held
7445 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
7446 ** The caller is responsible for eventually calling sqlite3_free() on
7447 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
7448 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
7450 static void arWhereClause(
7451 int *pRc,
7452 ArCommand *pAr,
7453 char **pzWhere /* OUT: New WHERE clause */
7455 char *zWhere = 0;
7456 const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
7457 if( *pRc==SQLITE_OK ){
7458 if( pAr->nArg==0 ){
7459 zWhere = sqlite3_mprintf("1");
7460 }else{
7461 int i;
7462 const char *zSep = "";
7463 for(i=0; i<pAr->nArg; i++){
7464 const char *z = pAr->azArg[i];
7465 zWhere = sqlite3_mprintf(
7466 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
7467 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
7469 if( zWhere==0 ){
7470 *pRc = SQLITE_NOMEM;
7471 break;
7473 zSep = " OR ";
7477 *pzWhere = zWhere;
7481 ** Implementation of .ar "lisT" command.
7483 static int arListCommand(ArCommand *pAr){
7484 const char *zSql = "SELECT %s FROM %s WHERE %s";
7485 const char *azCols[] = {
7486 "name",
7487 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
7490 char *zWhere = 0;
7491 sqlite3_stmt *pSql = 0;
7492 int rc;
7494 rc = arCheckEntries(pAr);
7495 arWhereClause(&rc, pAr, &zWhere);
7497 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
7498 pAr->zSrcTable, zWhere);
7499 if( pAr->bDryRun ){
7500 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
7501 }else{
7502 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7503 if( pAr->bVerbose ){
7504 sqlite3_fprintf(pAr->out, "%s % 10d %s %s\n",
7505 sqlite3_column_text(pSql, 0), sqlite3_column_int(pSql, 1),
7506 sqlite3_column_text(pSql, 2),sqlite3_column_text(pSql, 3));
7507 }else{
7508 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
7512 shellFinalize(&rc, pSql);
7513 sqlite3_free(zWhere);
7514 return rc;
7518 ** Implementation of .ar "Remove" command.
7520 static int arRemoveCommand(ArCommand *pAr){
7521 int rc = 0;
7522 char *zSql = 0;
7523 char *zWhere = 0;
7525 if( pAr->nArg ){
7526 /* Verify that args actually exist within the archive before proceeding.
7527 ** And formulate a WHERE clause to match them. */
7528 rc = arCheckEntries(pAr);
7529 arWhereClause(&rc, pAr, &zWhere);
7531 if( rc==SQLITE_OK ){
7532 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
7533 pAr->zSrcTable, zWhere);
7534 if( pAr->bDryRun ){
7535 sqlite3_fprintf(pAr->out, "%s\n", zSql);
7536 }else{
7537 char *zErr = 0;
7538 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
7539 if( rc==SQLITE_OK ){
7540 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7541 if( rc!=SQLITE_OK ){
7542 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7543 }else{
7544 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
7547 if( zErr ){
7548 sqlite3_fprintf(stdout, "ERROR: %s\n", zErr); /* stdout? */
7549 sqlite3_free(zErr);
7553 sqlite3_free(zWhere);
7554 sqlite3_free(zSql);
7555 return rc;
7559 ** Implementation of .ar "eXtract" command.
7561 static int arExtractCommand(ArCommand *pAr){
7562 const char *zSql1 =
7563 "SELECT "
7564 " ($dir || name),"
7565 " writefile(($dir || name), %s, mode, mtime) "
7566 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
7567 " AND name NOT GLOB '*..[/\\]*'";
7569 const char *azExtraArg[] = {
7570 "sqlar_uncompress(data, sz)",
7571 "data"
7574 sqlite3_stmt *pSql = 0;
7575 int rc = SQLITE_OK;
7576 char *zDir = 0;
7577 char *zWhere = 0;
7578 int i, j;
7580 /* If arguments are specified, check that they actually exist within
7581 ** the archive before proceeding. And formulate a WHERE clause to
7582 ** match them. */
7583 rc = arCheckEntries(pAr);
7584 arWhereClause(&rc, pAr, &zWhere);
7586 if( rc==SQLITE_OK ){
7587 if( pAr->zDir ){
7588 zDir = sqlite3_mprintf("%s/", pAr->zDir);
7589 }else{
7590 zDir = sqlite3_mprintf("");
7592 if( zDir==0 ) rc = SQLITE_NOMEM;
7595 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
7596 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
7599 if( rc==SQLITE_OK ){
7600 j = sqlite3_bind_parameter_index(pSql, "$dir");
7601 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
7603 /* Run the SELECT statement twice. The first time, writefile() is called
7604 ** for all archive members that should be extracted. The second time,
7605 ** only for the directories. This is because the timestamps for
7606 ** extracted directories must be reset after they are populated (as
7607 ** populating them changes the timestamp). */
7608 for(i=0; i<2; i++){
7609 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
7610 sqlite3_bind_int(pSql, j, i);
7611 if( pAr->bDryRun ){
7612 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_sql(pSql));
7613 }else{
7614 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7615 if( i==0 && pAr->bVerbose ){
7616 sqlite3_fprintf(pAr->out, "%s\n", sqlite3_column_text(pSql, 0));
7620 shellReset(&rc, pSql);
7622 shellFinalize(&rc, pSql);
7625 sqlite3_free(zDir);
7626 sqlite3_free(zWhere);
7627 return rc;
7631 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
7633 static int arExecSql(ArCommand *pAr, const char *zSql){
7634 int rc;
7635 if( pAr->bDryRun ){
7636 sqlite3_fprintf(pAr->out, "%s\n", zSql);
7637 rc = SQLITE_OK;
7638 }else{
7639 char *zErr = 0;
7640 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
7641 if( zErr ){
7642 sqlite3_fprintf(stdout, "ERROR: %s\n", zErr);
7643 sqlite3_free(zErr);
7646 return rc;
7651 ** Implementation of .ar "create", "insert", and "update" commands.
7653 ** create -> Create a new SQL archive
7654 ** insert -> Insert or reinsert all files listed
7655 ** update -> Insert files that have changed or that were not
7656 ** previously in the archive
7658 ** Create the "sqlar" table in the database if it does not already exist.
7659 ** Then add each file in the azFile[] array to the archive. Directories
7660 ** are added recursively. If argument bVerbose is non-zero, a message is
7661 ** printed on stdout for each file archived.
7663 ** The create command is the same as update, except that it drops
7664 ** any existing "sqlar" table before beginning. The "insert" command
7665 ** always overwrites every file named on the command-line, where as
7666 ** "update" only overwrites if the size or mtime or mode has changed.
7668 static int arCreateOrUpdateCommand(
7669 ArCommand *pAr, /* Command arguments and options */
7670 int bUpdate, /* true for a --create. */
7671 int bOnlyIfChanged /* Only update if file has changed */
7673 const char *zCreate =
7674 "CREATE TABLE IF NOT EXISTS sqlar(\n"
7675 " name TEXT PRIMARY KEY, -- name of the file\n"
7676 " mode INT, -- access permissions\n"
7677 " mtime INT, -- last modification time\n"
7678 " sz INT, -- original file size\n"
7679 " data BLOB -- compressed content\n"
7680 ")";
7681 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
7682 const char *zInsertFmt[2] = {
7683 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7684 " SELECT\n"
7685 " %s,\n"
7686 " mode,\n"
7687 " mtime,\n"
7688 " CASE substr(lsmode(mode),1,1)\n"
7689 " WHEN '-' THEN length(data)\n"
7690 " WHEN 'd' THEN 0\n"
7691 " ELSE -1 END,\n"
7692 " sqlar_compress(data)\n"
7693 " FROM fsdir(%Q,%Q) AS disk\n"
7694 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7696 "REPLACE INTO %s(name,mode,mtime,data)\n"
7697 " SELECT\n"
7698 " %s,\n"
7699 " mode,\n"
7700 " mtime,\n"
7701 " data\n"
7702 " FROM fsdir(%Q,%Q) AS disk\n"
7703 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7705 int i; /* For iterating through azFile[] */
7706 int rc; /* Return code */
7707 const char *zTab = 0; /* SQL table into which to insert */
7708 char *zSql;
7709 char zTemp[50];
7710 char *zExists = 0;
7712 arExecSql(pAr, "PRAGMA page_size=512");
7713 rc = arExecSql(pAr, "SAVEPOINT ar;");
7714 if( rc!=SQLITE_OK ) return rc;
7715 zTemp[0] = 0;
7716 if( pAr->bZip ){
7717 /* Initialize the zipfile virtual table, if necessary */
7718 if( pAr->zFile ){
7719 sqlite3_uint64 r;
7720 sqlite3_randomness(sizeof(r),&r);
7721 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
7722 zTab = zTemp;
7723 zSql = sqlite3_mprintf(
7724 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7725 zTab, pAr->zFile
7727 rc = arExecSql(pAr, zSql);
7728 sqlite3_free(zSql);
7729 }else{
7730 zTab = "zip";
7732 }else{
7733 /* Initialize the table for an SQLAR */
7734 zTab = "sqlar";
7735 if( bUpdate==0 ){
7736 rc = arExecSql(pAr, zDrop);
7737 if( rc!=SQLITE_OK ) goto end_ar_transaction;
7739 rc = arExecSql(pAr, zCreate);
7741 if( bOnlyIfChanged ){
7742 zExists = sqlite3_mprintf(
7743 " AND NOT EXISTS("
7744 "SELECT 1 FROM %s AS mem"
7745 " WHERE mem.name=disk.name"
7746 " AND mem.mtime=disk.mtime"
7747 " AND mem.mode=disk.mode)", zTab);
7748 }else{
7749 zExists = sqlite3_mprintf("");
7751 if( zExists==0 ) rc = SQLITE_NOMEM;
7752 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
7753 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
7754 pAr->bVerbose ? "shell_putsnl(name)" : "name",
7755 pAr->azArg[i], pAr->zDir, zExists);
7756 rc = arExecSql(pAr, zSql2);
7757 sqlite3_free(zSql2);
7759 end_ar_transaction:
7760 if( rc!=SQLITE_OK ){
7761 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7762 }else{
7763 rc = arExecSql(pAr, "RELEASE ar;");
7764 if( pAr->bZip && pAr->zFile ){
7765 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
7766 arExecSql(pAr, zSql);
7767 sqlite3_free(zSql);
7770 sqlite3_free(zExists);
7771 return rc;
7775 ** Implementation of ".ar" dot command.
7777 static int arDotCommand(
7778 ShellState *pState, /* Current shell tool state */
7779 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
7780 char **azArg, /* Array of arguments passed to dot command */
7781 int nArg /* Number of entries in azArg[] */
7783 ArCommand cmd;
7784 int rc;
7785 memset(&cmd, 0, sizeof(cmd));
7786 cmd.fromCmdLine = fromCmdLine;
7787 rc = arParseCommand(azArg, nArg, &cmd);
7788 if( rc==SQLITE_OK ){
7789 int eDbType = SHELL_OPEN_UNSPEC;
7790 cmd.p = pState;
7791 cmd.out = pState->out;
7792 cmd.db = pState->db;
7793 if( cmd.zFile ){
7794 eDbType = deduceDatabaseType(cmd.zFile, 1);
7795 }else{
7796 eDbType = pState->openMode;
7798 if( eDbType==SHELL_OPEN_ZIPFILE ){
7799 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
7800 if( cmd.zFile==0 ){
7801 cmd.zSrcTable = sqlite3_mprintf("zip");
7802 }else{
7803 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
7806 cmd.bZip = 1;
7807 }else if( cmd.zFile ){
7808 int flags;
7809 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
7810 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
7811 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
7812 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
7813 }else{
7814 flags = SQLITE_OPEN_READONLY;
7816 cmd.db = 0;
7817 if( cmd.bDryRun ){
7818 sqlite3_fprintf(cmd.out, "-- open database '%s'%s\n", cmd.zFile,
7819 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
7821 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
7822 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
7823 if( rc!=SQLITE_OK ){
7824 sqlite3_fprintf(stderr, "cannot open file: %s (%s)\n",
7825 cmd.zFile, sqlite3_errmsg(cmd.db));
7826 goto end_ar_command;
7828 sqlite3_fileio_init(cmd.db, 0, 0);
7829 sqlite3_sqlar_init(cmd.db, 0, 0);
7830 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
7831 shellPutsFunc, 0, 0);
7834 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
7835 if( cmd.eCmd!=AR_CMD_CREATE
7836 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
7838 sqlite3_fprintf(stderr, "database does not contain an 'sqlar' table\n");
7839 rc = SQLITE_ERROR;
7840 goto end_ar_command;
7842 cmd.zSrcTable = sqlite3_mprintf("sqlar");
7845 switch( cmd.eCmd ){
7846 case AR_CMD_CREATE:
7847 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
7848 break;
7850 case AR_CMD_EXTRACT:
7851 rc = arExtractCommand(&cmd);
7852 break;
7854 case AR_CMD_LIST:
7855 rc = arListCommand(&cmd);
7856 break;
7858 case AR_CMD_HELP:
7859 arUsage(pState->out);
7860 break;
7862 case AR_CMD_INSERT:
7863 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
7864 break;
7866 case AR_CMD_REMOVE:
7867 rc = arRemoveCommand(&cmd);
7868 break;
7870 default:
7871 assert( cmd.eCmd==AR_CMD_UPDATE );
7872 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
7873 break;
7876 end_ar_command:
7877 if( cmd.db!=pState->db ){
7878 close_db(cmd.db);
7880 sqlite3_free(cmd.zSrcTable);
7882 return rc;
7884 /* End of the ".archive" or ".ar" command logic
7885 *******************************************************************************/
7886 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7888 #if SQLITE_SHELL_HAVE_RECOVER
7891 ** This function is used as a callback by the recover extension. Simply
7892 ** print the supplied SQL statement to stdout.
7894 static int recoverSqlCb(void *pCtx, const char *zSql){
7895 ShellState *pState = (ShellState*)pCtx;
7896 sqlite3_fprintf(pState->out, "%s;\n", zSql);
7897 return SQLITE_OK;
7901 ** This function is called to recover data from the database. A script
7902 ** to construct a new database containing all recovered data is output
7903 ** on stream pState->out.
7905 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
7906 int rc = SQLITE_OK;
7907 const char *zRecoveryDb = ""; /* Name of "recovery" database. Debug only */
7908 const char *zLAF = "lost_and_found";
7909 int bFreelist = 1; /* 0 if --ignore-freelist is specified */
7910 int bRowids = 1; /* 0 if --no-rowids */
7911 sqlite3_recover *p = 0;
7912 int i = 0;
7914 for(i=1; i<nArg; i++){
7915 char *z = azArg[i];
7916 int n;
7917 if( z[0]=='-' && z[1]=='-' ) z++;
7918 n = strlen30(z);
7919 if( n<=17 && memcmp("-ignore-freelist", z, n)==0 ){
7920 bFreelist = 0;
7921 }else
7922 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
7923 /* This option determines the name of the ATTACH-ed database used
7924 ** internally by the recovery extension. The default is "" which
7925 ** means to use a temporary database that is automatically deleted
7926 ** when closed. This option is undocumented and might disappear at
7927 ** any moment. */
7928 i++;
7929 zRecoveryDb = azArg[i];
7930 }else
7931 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
7932 i++;
7933 zLAF = azArg[i];
7934 }else
7935 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
7936 bRowids = 0;
7938 else{
7939 sqlite3_fprintf(stderr,"unexpected option: %s\n", azArg[i]);
7940 showHelp(pState->out, azArg[0]);
7941 return 1;
7945 p = sqlite3_recover_init_sql(
7946 pState->db, "main", recoverSqlCb, (void*)pState
7949 sqlite3_recover_config(p, 789, (void*)zRecoveryDb); /* Debug use only */
7950 sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
7951 sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
7952 sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
7954 sqlite3_recover_run(p);
7955 if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
7956 const char *zErr = sqlite3_recover_errmsg(p);
7957 int errCode = sqlite3_recover_errcode(p);
7958 sqlite3_fprintf(stderr,"sql error: %s (%d)\n", zErr, errCode);
7960 rc = sqlite3_recover_finish(p);
7961 return rc;
7963 #endif /* SQLITE_SHELL_HAVE_RECOVER */
7966 ** Implementation of ".intck STEPS_PER_UNLOCK" command.
7968 static int intckDatabaseCmd(ShellState *pState, i64 nStepPerUnlock){
7969 sqlite3_intck *p = 0;
7970 int rc = SQLITE_OK;
7972 rc = sqlite3_intck_open(pState->db, "main", &p);
7973 if( rc==SQLITE_OK ){
7974 i64 nStep = 0;
7975 i64 nError = 0;
7976 const char *zErr = 0;
7977 while( SQLITE_OK==sqlite3_intck_step(p) ){
7978 const char *zMsg = sqlite3_intck_message(p);
7979 if( zMsg ){
7980 sqlite3_fprintf(pState->out, "%s\n", zMsg);
7981 nError++;
7983 nStep++;
7984 if( nStepPerUnlock && (nStep % nStepPerUnlock)==0 ){
7985 sqlite3_intck_unlock(p);
7988 rc = sqlite3_intck_error(p, &zErr);
7989 if( zErr ){
7990 sqlite3_fprintf(stderr,"%s\n", zErr);
7992 sqlite3_intck_close(p);
7994 sqlite3_fprintf(pState->out, "%lld steps, %lld errors\n", nStep, nError);
7997 return rc;
8001 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
8002 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
8003 * close db and set it to 0, and return the columns spec, to later
8004 * be sqlite3_free()'ed by the caller.
8005 * The return is 0 when either:
8006 * (a) The db was not initialized and zCol==0 (There are no columns.)
8007 * (b) zCol!=0 (Column was added, db initialized as needed.)
8008 * The 3rd argument, pRenamed, references an out parameter. If the
8009 * pointer is non-zero, its referent will be set to a summary of renames
8010 * done if renaming was necessary, or set to 0 if none was done. The out
8011 * string (if any) must be sqlite3_free()'ed by the caller.
8013 #ifdef SHELL_DEBUG
8014 #define rc_err_oom_die(rc) \
8015 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
8016 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
8017 sqlite3_fprintf(stderr,"E:%d\n",rc), assert(0)
8018 #else
8019 static void rc_err_oom_die(int rc){
8020 if( rc==SQLITE_NOMEM ) shell_check_oom(0);
8021 assert(rc==SQLITE_OK||rc==SQLITE_DONE);
8023 #endif
8025 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
8026 static char zCOL_DB[] = SHELL_STRINGIFY(SHELL_COLFIX_DB);
8027 #else /* Otherwise, memory is faster/better for the transient DB. */
8028 static const char *zCOL_DB = ":memory:";
8029 #endif
8031 /* Define character (as C string) to separate generated column ordinal
8032 * from protected part of incoming column names. This defaults to "_"
8033 * so that incoming column identifiers that did not need not be quoted
8034 * remain usable without being quoted. It must be one character.
8036 #ifndef SHELL_AUTOCOLUMN_SEP
8037 # define AUTOCOLUMN_SEP "_"
8038 #else
8039 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
8040 #endif
8042 static char *zAutoColumn(const char *zColNew, sqlite3 **pDb, char **pzRenamed){
8043 /* Queries and D{D,M}L used here */
8044 static const char * const zTabMake = "\
8045 CREATE TABLE ColNames(\
8046 cpos INTEGER PRIMARY KEY,\
8047 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
8048 CREATE VIEW RepeatedNames AS \
8049 SELECT DISTINCT t.name FROM ColNames t \
8050 WHERE t.name COLLATE NOCASE IN (\
8051 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
8054 static const char * const zTabFill = "\
8055 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
8056 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
8058 static const char * const zHasDupes = "\
8059 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
8060 <count(name) FROM ColNames\
8062 #ifdef SHELL_COLUMN_RENAME_CLEAN
8063 static const char * const zDedoctor = "\
8064 UPDATE ColNames SET chop=iif(\
8065 (substring(name,nlen,1) BETWEEN '0' AND '9')\
8066 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP"'),\
8067 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP"0123456789')),\
8071 #endif
8072 static const char * const zSetReps = "\
8073 UPDATE ColNames AS t SET reps=\
8074 (SELECT count(*) FROM ColNames d \
8075 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8076 COLLATE NOCASE\
8079 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8080 static const char * const zColDigits = "\
8081 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8083 #else
8084 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8085 static const char * const zColDigits = "\
8086 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8087 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8088 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8090 #endif
8091 static const char * const zRenameRank =
8092 #ifdef SHELL_COLUMN_RENAME_CLEAN
8093 "UPDATE ColNames AS t SET suff="
8094 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP"', $1, cpos), '')"
8095 #else /* ...RENAME_MINIMAL_ONE_PASS */
8096 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8097 " SELECT 0 AS nlz"
8098 " UNION"
8099 " SELECT nlz+1 AS nlz FROM Lzn"
8100 " WHERE EXISTS("
8101 " SELECT 1"
8102 " FROM ColNames t, ColNames o"
8103 " WHERE"
8104 " iif(t.name IN (SELECT * FROM RepeatedNames),"
8105 " printf('%s"AUTOCOLUMN_SEP"%s',"
8106 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8107 " t.name"
8108 " )"
8109 " ="
8110 " iif(o.name IN (SELECT * FROM RepeatedNames),"
8111 " printf('%s"AUTOCOLUMN_SEP"%s',"
8112 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8113 " o.name"
8114 " )"
8115 " COLLATE NOCASE"
8116 " AND o.cpos<>t.cpos"
8117 " GROUP BY t.cpos"
8118 " )"
8119 ") UPDATE Colnames AS t SET"
8120 " chop = 0," /* No chopping, never touch incoming names. */
8121 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
8122 " printf('"AUTOCOLUMN_SEP"%s', substring("
8123 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8124 " ''"
8125 " )"
8126 #endif
8128 static const char * const zCollectVar = "\
8129 SELECT\
8130 '('||x'0a'\
8131 || group_concat(\
8132 cname||' TEXT',\
8133 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8134 ||')' AS ColsSpec \
8135 FROM (\
8136 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8137 FROM ColNames ORDER BY cpos\
8139 static const char * const zRenamesDone =
8140 "SELECT group_concat("
8141 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8142 " ','||x'0a')"
8143 "FROM ColNames WHERE suff<>'' OR chop!=0"
8145 int rc;
8146 sqlite3_stmt *pStmt = 0;
8147 assert(pDb!=0);
8148 if( zColNew ){
8149 /* Add initial or additional column. Init db if necessary. */
8150 if( *pDb==0 ){
8151 if( SQLITE_OK!=sqlite3_open(zCOL_DB, pDb) ) return 0;
8152 #ifdef SHELL_COLFIX_DB
8153 if(*zCOL_DB!=':')
8154 sqlite3_exec(*pDb,"drop table if exists ColNames;"
8155 "drop view if exists RepeatedNames;",0,0,0);
8156 #endif
8157 #undef SHELL_COLFIX_DB
8158 rc = sqlite3_exec(*pDb, zTabMake, 0, 0, 0);
8159 rc_err_oom_die(rc);
8161 assert(*pDb!=0);
8162 rc = sqlite3_prepare_v2(*pDb, zTabFill, -1, &pStmt, 0);
8163 rc_err_oom_die(rc);
8164 rc = sqlite3_bind_text(pStmt, 1, zColNew, -1, 0);
8165 rc_err_oom_die(rc);
8166 rc = sqlite3_step(pStmt);
8167 rc_err_oom_die(rc);
8168 sqlite3_finalize(pStmt);
8169 return 0;
8170 }else if( *pDb==0 ){
8171 return 0;
8172 }else{
8173 /* Formulate the columns spec, close the DB, zero *pDb. */
8174 char *zColsSpec = 0;
8175 int hasDupes = db_int(*pDb, zHasDupes);
8176 int nDigits = (hasDupes)? db_int(*pDb, zColDigits) : 0;
8177 if( hasDupes ){
8178 #ifdef SHELL_COLUMN_RENAME_CLEAN
8179 rc = sqlite3_exec(*pDb, zDedoctor, 0, 0, 0);
8180 rc_err_oom_die(rc);
8181 #endif
8182 rc = sqlite3_exec(*pDb, zSetReps, 0, 0, 0);
8183 rc_err_oom_die(rc);
8184 rc = sqlite3_prepare_v2(*pDb, zRenameRank, -1, &pStmt, 0);
8185 rc_err_oom_die(rc);
8186 sqlite3_bind_int(pStmt, 1, nDigits);
8187 rc = sqlite3_step(pStmt);
8188 sqlite3_finalize(pStmt);
8189 if( rc!=SQLITE_DONE ) rc_err_oom_die(SQLITE_NOMEM);
8191 assert(db_int(*pDb, zHasDupes)==0); /* Consider: remove this */
8192 rc = sqlite3_prepare_v2(*pDb, zCollectVar, -1, &pStmt, 0);
8193 rc_err_oom_die(rc);
8194 rc = sqlite3_step(pStmt);
8195 if( rc==SQLITE_ROW ){
8196 zColsSpec = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8197 }else{
8198 zColsSpec = 0;
8200 if( pzRenamed!=0 ){
8201 if( !hasDupes ) *pzRenamed = 0;
8202 else{
8203 sqlite3_finalize(pStmt);
8204 if( SQLITE_OK==sqlite3_prepare_v2(*pDb, zRenamesDone, -1, &pStmt, 0)
8205 && SQLITE_ROW==sqlite3_step(pStmt) ){
8206 *pzRenamed = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
8207 }else
8208 *pzRenamed = 0;
8211 sqlite3_finalize(pStmt);
8212 sqlite3_close(*pDb);
8213 *pDb = 0;
8214 return zColsSpec;
8219 ** Check if the sqlite_schema table contains one or more virtual tables. If
8220 ** parameter zLike is not NULL, then it is an SQL expression that the
8221 ** sqlite_schema row must also match. If one or more such rows are found,
8222 ** print the following warning to the output:
8224 ** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
8226 static int outputDumpWarning(ShellState *p, const char *zLike){
8227 int rc = SQLITE_OK;
8228 sqlite3_stmt *pStmt = 0;
8229 shellPreparePrintf(p->db, &rc, &pStmt,
8230 "SELECT 1 FROM sqlite_schema o WHERE "
8231 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike ? zLike : "true"
8233 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8234 sqlite3_fputs("/* WARNING: "
8235 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n",
8236 p->out
8239 shellFinalize(&rc, pStmt);
8240 return rc;
8244 ** Fault-Simulator state and logic.
8246 static struct {
8247 int iId; /* ID that triggers a simulated fault. -1 means "any" */
8248 int iErr; /* The error code to return on a fault */
8249 int iCnt; /* Trigger the fault only if iCnt is already zero */
8250 int iInterval; /* Reset iCnt to this value after each fault */
8251 int eVerbose; /* When to print output */
8252 int nHit; /* Number of hits seen so far */
8253 int nRepeat; /* Turn off after this many hits. 0 for never */
8254 int nSkip; /* Skip this many before first fault */
8255 } faultsim_state = {-1, 0, 0, 0, 0, 0, 0, 0};
8258 ** This is the fault-sim callback
8260 static int faultsim_callback(int iArg){
8261 if( faultsim_state.iId>0 && faultsim_state.iId!=iArg ){
8262 return SQLITE_OK;
8264 if( faultsim_state.iCnt ){
8265 if( faultsim_state.iCnt>0 ) faultsim_state.iCnt--;
8266 if( faultsim_state.eVerbose>=2 ){
8267 sqlite3_fprintf(stdout,
8268 "FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg, faultsim_state.iCnt);
8270 return SQLITE_OK;
8272 if( faultsim_state.eVerbose>=1 ){
8273 sqlite3_fprintf(stdout,
8274 "FAULT-SIM id=%d returns %d\n", iArg, faultsim_state.iErr);
8276 faultsim_state.iCnt = faultsim_state.iInterval;
8277 faultsim_state.nHit++;
8278 if( faultsim_state.nRepeat>0 && faultsim_state.nRepeat<=faultsim_state.nHit ){
8279 faultsim_state.iCnt = -1;
8281 return faultsim_state.iErr;
8285 ** If an input line begins with "." then invoke this routine to
8286 ** process that line.
8288 ** Return 1 on error, 2 to exit, and 0 otherwise.
8290 static int do_meta_command(char *zLine, ShellState *p){
8291 int h = 1;
8292 int nArg = 0;
8293 int n, c;
8294 int rc = 0;
8295 char *azArg[52];
8297 #ifndef SQLITE_OMIT_VIRTUALTABLE
8298 if( p->expert.pExpert ){
8299 expertFinish(p, 1, 0);
8301 #endif
8303 /* Parse the input line into tokens.
8305 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
8306 while( IsSpace(zLine[h]) ){ h++; }
8307 if( zLine[h]==0 ) break;
8308 if( zLine[h]=='\'' || zLine[h]=='"' ){
8309 int delim = zLine[h++];
8310 azArg[nArg++] = &zLine[h];
8311 while( zLine[h] && zLine[h]!=delim ){
8312 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
8313 h++;
8315 if( zLine[h]==delim ){
8316 zLine[h++] = 0;
8318 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
8319 }else{
8320 azArg[nArg++] = &zLine[h];
8321 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
8322 if( zLine[h] ) zLine[h++] = 0;
8325 azArg[nArg] = 0;
8327 /* Process the input line.
8329 if( nArg==0 ) return 0; /* no tokens, no error */
8330 n = strlen30(azArg[0]);
8331 c = azArg[0][0];
8332 clearTempFile(p);
8334 #ifndef SQLITE_OMIT_AUTHORIZATION
8335 if( c=='a' && cli_strncmp(azArg[0], "auth", n)==0 ){
8336 if( nArg!=2 ){
8337 sqlite3_fprintf(stderr, "Usage: .auth ON|OFF\n");
8338 rc = 1;
8339 goto meta_command_exit;
8341 open_db(p, 0);
8342 if( booleanValue(azArg[1]) ){
8343 sqlite3_set_authorizer(p->db, shellAuth, p);
8344 }else if( p->bSafeModePersist ){
8345 sqlite3_set_authorizer(p->db, safeModeAuth, p);
8346 }else{
8347 sqlite3_set_authorizer(p->db, 0, 0);
8349 }else
8350 #endif
8352 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8353 && !defined(SQLITE_SHELL_FIDDLE)
8354 if( c=='a' && cli_strncmp(azArg[0], "archive", n)==0 ){
8355 open_db(p, 0);
8356 failIfSafeMode(p, "cannot run .archive in safe mode");
8357 rc = arDotCommand(p, 0, azArg, nArg);
8358 }else
8359 #endif
8361 #ifndef SQLITE_SHELL_FIDDLE
8362 if( (c=='b' && n>=3 && cli_strncmp(azArg[0], "backup", n)==0)
8363 || (c=='s' && n>=3 && cli_strncmp(azArg[0], "save", n)==0)
8365 const char *zDestFile = 0;
8366 const char *zDb = 0;
8367 sqlite3 *pDest;
8368 sqlite3_backup *pBackup;
8369 int j;
8370 int bAsync = 0;
8371 const char *zVfs = 0;
8372 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
8373 for(j=1; j<nArg; j++){
8374 const char *z = azArg[j];
8375 if( z[0]=='-' ){
8376 if( z[1]=='-' ) z++;
8377 if( cli_strcmp(z, "-append")==0 ){
8378 zVfs = "apndvfs";
8379 }else
8380 if( cli_strcmp(z, "-async")==0 ){
8381 bAsync = 1;
8382 }else
8384 sqlite3_fprintf(stderr,"unknown option: %s\n", azArg[j]);
8385 return 1;
8387 }else if( zDestFile==0 ){
8388 zDestFile = azArg[j];
8389 }else if( zDb==0 ){
8390 zDb = zDestFile;
8391 zDestFile = azArg[j];
8392 }else{
8393 sqlite3_fprintf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8394 return 1;
8397 if( zDestFile==0 ){
8398 sqlite3_fprintf(stderr, "missing FILENAME argument on .backup\n");
8399 return 1;
8401 if( zDb==0 ) zDb = "main";
8402 rc = sqlite3_open_v2(zDestFile, &pDest,
8403 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
8404 if( rc!=SQLITE_OK ){
8405 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zDestFile);
8406 close_db(pDest);
8407 return 1;
8409 if( bAsync ){
8410 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8411 0, 0, 0);
8413 open_db(p, 0);
8414 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
8415 if( pBackup==0 ){
8416 shellDatabaseError(pDest);
8417 close_db(pDest);
8418 return 1;
8420 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
8421 sqlite3_backup_finish(pBackup);
8422 if( rc==SQLITE_DONE ){
8423 rc = 0;
8424 }else{
8425 shellDatabaseError(pDest);
8426 rc = 1;
8428 close_db(pDest);
8429 }else
8430 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8432 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "bail", n)==0 ){
8433 if( nArg==2 ){
8434 bail_on_error = booleanValue(azArg[1]);
8435 }else{
8436 eputz("Usage: .bail on|off\n");
8437 rc = 1;
8439 }else
8441 /* Undocumented. Legacy only. See "crlf" below */
8442 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "binary", n)==0 ){
8443 eputz("The \".binary\" command is deprecated.\n");
8444 rc = 1;
8445 }else
8447 /* The undocumented ".breakpoint" command causes a call to the no-op
8448 ** routine named test_breakpoint().
8450 if( c=='b' && n>=3 && cli_strncmp(azArg[0], "breakpoint", n)==0 ){
8451 test_breakpoint();
8452 }else
8454 #ifndef SQLITE_SHELL_FIDDLE
8455 if( c=='c' && cli_strcmp(azArg[0],"cd")==0 ){
8456 failIfSafeMode(p, "cannot run .cd in safe mode");
8457 if( nArg==2 ){
8458 #if defined(_WIN32) || defined(WIN32)
8459 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
8460 rc = !SetCurrentDirectoryW(z);
8461 sqlite3_free(z);
8462 #else
8463 rc = chdir(azArg[1]);
8464 #endif
8465 if( rc ){
8466 sqlite3_fprintf(stderr,"Cannot change to directory \"%s\"\n", azArg[1]);
8467 rc = 1;
8469 }else{
8470 eputz("Usage: .cd DIRECTORY\n");
8471 rc = 1;
8473 }else
8474 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8476 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "changes", n)==0 ){
8477 if( nArg==2 ){
8478 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
8479 }else{
8480 eputz("Usage: .changes on|off\n");
8481 rc = 1;
8483 }else
8485 #ifndef SQLITE_SHELL_FIDDLE
8486 /* Cancel output redirection, if it is currently set (by .testcase)
8487 ** Then read the content of the testcase-out.txt file and compare against
8488 ** azArg[1]. If there are differences, report an error and exit.
8490 if( c=='c' && n>=3 && cli_strncmp(azArg[0], "check", n)==0 ){
8491 char *zRes = 0;
8492 output_reset(p);
8493 if( nArg!=2 ){
8494 eputz("Usage: .check GLOB-PATTERN\n");
8495 rc = 2;
8496 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
8497 rc = 2;
8498 }else if( testcase_glob(azArg[1],zRes)==0 ){
8499 sqlite3_fprintf(stderr,
8500 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
8501 p->zTestcase, azArg[1], zRes);
8502 rc = 1;
8503 }else{
8504 sqlite3_fprintf(p->out, "testcase-%s ok\n", p->zTestcase);
8505 p->nCheck++;
8507 sqlite3_free(zRes);
8508 }else
8509 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8511 #ifndef SQLITE_SHELL_FIDDLE
8512 if( c=='c' && cli_strncmp(azArg[0], "clone", n)==0 ){
8513 failIfSafeMode(p, "cannot run .clone in safe mode");
8514 if( nArg==2 ){
8515 tryToClone(p, azArg[1]);
8516 }else{
8517 eputz("Usage: .clone FILENAME\n");
8518 rc = 1;
8520 }else
8521 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8523 if( c=='c' && cli_strncmp(azArg[0], "connection", n)==0 ){
8524 if( nArg==1 ){
8525 /* List available connections */
8526 int i;
8527 for(i=0; i<ArraySize(p->aAuxDb); i++){
8528 const char *zFile = p->aAuxDb[i].zDbFilename;
8529 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
8530 zFile = "(not open)";
8531 }else if( zFile==0 ){
8532 zFile = "(memory)";
8533 }else if( zFile[0]==0 ){
8534 zFile = "(temporary-file)";
8536 if( p->pAuxDb == &p->aAuxDb[i] ){
8537 sqlite3_fprintf(stdout, "ACTIVE %d: %s\n", i, zFile);
8538 }else if( p->aAuxDb[i].db!=0 ){
8539 sqlite3_fprintf(stdout, " %d: %s\n", i, zFile);
8542 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
8543 int i = azArg[1][0] - '0';
8544 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
8545 p->pAuxDb->db = p->db;
8546 p->pAuxDb = &p->aAuxDb[i];
8547 globalDb = p->db = p->pAuxDb->db;
8548 p->pAuxDb->db = 0;
8550 }else if( nArg==3 && cli_strcmp(azArg[1], "close")==0
8551 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
8552 int i = azArg[2][0] - '0';
8553 if( i<0 || i>=ArraySize(p->aAuxDb) ){
8554 /* No-op */
8555 }else if( p->pAuxDb == &p->aAuxDb[i] ){
8556 eputz("cannot close the active database connection\n");
8557 rc = 1;
8558 }else if( p->aAuxDb[i].db ){
8559 session_close_all(p, i);
8560 close_db(p->aAuxDb[i].db);
8561 p->aAuxDb[i].db = 0;
8563 }else{
8564 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
8565 rc = 1;
8567 }else
8569 if( c=='c' && n==4
8570 && (cli_strncmp(azArg[0], "crlf", n)==0
8571 || cli_strncmp(azArg[0], "crnl",n)==0)
8573 if( nArg==2 ){
8574 #ifdef _WIN32
8575 p->crlfMode = booleanValue(azArg[1]);
8576 #else
8577 p->crlfMode = 0;
8578 #endif
8580 sqlite3_fprintf(stderr, "crlf is %s\n", p->crlfMode ? "ON" : "OFF");
8581 }else
8583 if( c=='d' && n>1 && cli_strncmp(azArg[0], "databases", n)==0 ){
8584 char **azName = 0;
8585 int nName = 0;
8586 sqlite3_stmt *pStmt;
8587 int i;
8588 open_db(p, 0);
8589 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
8590 if( rc ){
8591 shellDatabaseError(p->db);
8592 rc = 1;
8593 }else{
8594 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8595 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
8596 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
8597 if( zSchema==0 || zFile==0 ) continue;
8598 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
8599 shell_check_oom(azName);
8600 azName[nName*2] = strdup(zSchema);
8601 azName[nName*2+1] = strdup(zFile);
8602 nName++;
8605 sqlite3_finalize(pStmt);
8606 for(i=0; i<nName; i++){
8607 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
8608 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
8609 const char *z = azName[i*2+1];
8610 sqlite3_fprintf(p->out, "%s: %s %s%s\n",
8611 azName[i*2], z && z[0] ? z : "\"\"", bRdonly ? "r/o" : "r/w",
8612 eTxn==SQLITE_TXN_NONE ? "" :
8613 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
8614 free(azName[i*2]);
8615 free(azName[i*2+1]);
8617 sqlite3_free(azName);
8618 }else
8620 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbconfig", n)==0 ){
8621 static const struct DbConfigChoices {
8622 const char *zName;
8623 int op;
8624 } aDbConfig[] = {
8625 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
8626 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
8627 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
8628 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
8629 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
8630 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
8631 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
8632 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
8633 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
8634 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
8635 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
8636 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
8637 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
8638 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER },
8639 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS },
8640 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
8641 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
8642 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
8644 int ii, v;
8645 open_db(p, 0);
8646 for(ii=0; ii<ArraySize(aDbConfig); ii++){
8647 if( nArg>1 && cli_strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
8648 if( nArg>=3 ){
8649 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
8651 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
8652 sqlite3_fprintf(p->out, "%19s %s\n",
8653 aDbConfig[ii].zName, v ? "on" : "off");
8654 if( nArg>1 ) break;
8656 if( nArg>1 && ii==ArraySize(aDbConfig) ){
8657 sqlite3_fprintf(stderr,"Error: unknown dbconfig \"%s\"\n", azArg[1]);
8658 eputz("Enter \".dbconfig\" with no arguments for a list\n");
8660 }else
8662 #if SQLITE_SHELL_HAVE_RECOVER
8663 if( c=='d' && n>=3 && cli_strncmp(azArg[0], "dbinfo", n)==0 ){
8664 rc = shell_dbinfo_command(p, nArg, azArg);
8665 }else
8667 if( c=='r' && cli_strncmp(azArg[0], "recover", n)==0 ){
8668 open_db(p, 0);
8669 rc = recoverDatabaseCmd(p, nArg, azArg);
8670 }else
8671 #endif /* SQLITE_SHELL_HAVE_RECOVER */
8673 if( c=='d' && cli_strncmp(azArg[0], "dump", n)==0 ){
8674 char *zLike = 0;
8675 char *zSql;
8676 int i;
8677 int savedShowHeader = p->showHeader;
8678 int savedShellFlags = p->shellFlgs;
8679 ShellClearFlag(p,
8680 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
8681 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
8682 for(i=1; i<nArg; i++){
8683 if( azArg[i][0]=='-' ){
8684 const char *z = azArg[i]+1;
8685 if( z[0]=='-' ) z++;
8686 if( cli_strcmp(z,"preserve-rowids")==0 ){
8687 #ifdef SQLITE_OMIT_VIRTUALTABLE
8688 eputz("The --preserve-rowids option is not compatible"
8689 " with SQLITE_OMIT_VIRTUALTABLE\n");
8690 rc = 1;
8691 sqlite3_free(zLike);
8692 goto meta_command_exit;
8693 #else
8694 ShellSetFlag(p, SHFLG_PreserveRowid);
8695 #endif
8696 }else
8697 if( cli_strcmp(z,"newlines")==0 ){
8698 ShellSetFlag(p, SHFLG_Newlines);
8699 }else
8700 if( cli_strcmp(z,"data-only")==0 ){
8701 ShellSetFlag(p, SHFLG_DumpDataOnly);
8702 }else
8703 if( cli_strcmp(z,"nosys")==0 ){
8704 ShellSetFlag(p, SHFLG_DumpNoSys);
8705 }else
8707 sqlite3_fprintf(stderr,
8708 "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
8709 rc = 1;
8710 sqlite3_free(zLike);
8711 goto meta_command_exit;
8713 }else{
8714 /* azArg[i] contains a LIKE pattern. This ".dump" request should
8715 ** only dump data for tables for which either the table name matches
8716 ** the LIKE pattern, or the table appears to be a shadow table of
8717 ** a virtual table for which the name matches the LIKE pattern.
8719 char *zExpr = sqlite3_mprintf(
8720 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8721 " SELECT 1 FROM sqlite_schema WHERE "
8722 " name LIKE %Q ESCAPE '\\' AND"
8723 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8724 " substr(o.name, 1, length(name)+1) == (name||'_')"
8725 ")", azArg[i], azArg[i]
8728 if( zLike ){
8729 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
8730 }else{
8731 zLike = zExpr;
8736 open_db(p, 0);
8738 outputDumpWarning(p, zLike);
8739 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8740 /* When playing back a "dump", the content might appear in an order
8741 ** which causes immediate foreign key constraints to be violated.
8742 ** So disable foreign-key constraint enforcement to prevent problems. */
8743 sqlite3_fputs("PRAGMA foreign_keys=OFF;\n", p->out);
8744 sqlite3_fputs("BEGIN TRANSACTION;\n", p->out);
8746 p->writableSchema = 0;
8747 p->showHeader = 0;
8748 /* Set writable_schema=ON since doing so forces SQLite to initialize
8749 ** as much of the schema as it can even if the sqlite_schema table is
8750 ** corrupt. */
8751 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8752 p->nErr = 0;
8753 if( zLike==0 ) zLike = sqlite3_mprintf("true");
8754 zSql = sqlite3_mprintf(
8755 "SELECT name, type, sql FROM sqlite_schema AS o "
8756 "WHERE (%s) AND type=='table'"
8757 " AND sql NOT NULL"
8758 " ORDER BY tbl_name='sqlite_sequence', rowid",
8759 zLike
8761 run_schema_dump_query(p,zSql);
8762 sqlite3_free(zSql);
8763 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8764 zSql = sqlite3_mprintf(
8765 "SELECT sql FROM sqlite_schema AS o "
8766 "WHERE (%s) AND sql NOT NULL"
8767 " AND type IN ('index','trigger','view') "
8768 "ORDER BY type COLLATE NOCASE DESC",
8769 zLike
8771 run_table_dump_query(p, zSql);
8772 sqlite3_free(zSql);
8774 sqlite3_free(zLike);
8775 if( p->writableSchema ){
8776 sqlite3_fputs("PRAGMA writable_schema=OFF;\n", p->out);
8777 p->writableSchema = 0;
8779 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8780 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
8781 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
8782 sqlite3_fputs(p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n", p->out);
8784 p->showHeader = savedShowHeader;
8785 p->shellFlgs = savedShellFlags;
8786 }else
8788 if( c=='e' && cli_strncmp(azArg[0], "echo", n)==0 ){
8789 if( nArg==2 ){
8790 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
8791 }else{
8792 eputz("Usage: .echo on|off\n");
8793 rc = 1;
8795 }else
8797 if( c=='e' && cli_strncmp(azArg[0], "eqp", n)==0 ){
8798 if( nArg==2 ){
8799 p->autoEQPtest = 0;
8800 if( p->autoEQPtrace ){
8801 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8802 p->autoEQPtrace = 0;
8804 if( cli_strcmp(azArg[1],"full")==0 ){
8805 p->autoEQP = AUTOEQP_full;
8806 }else if( cli_strcmp(azArg[1],"trigger")==0 ){
8807 p->autoEQP = AUTOEQP_trigger;
8808 #ifdef SQLITE_DEBUG
8809 }else if( cli_strcmp(azArg[1],"test")==0 ){
8810 p->autoEQP = AUTOEQP_on;
8811 p->autoEQPtest = 1;
8812 }else if( cli_strcmp(azArg[1],"trace")==0 ){
8813 p->autoEQP = AUTOEQP_full;
8814 p->autoEQPtrace = 1;
8815 open_db(p, 0);
8816 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8817 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8818 #endif
8819 }else{
8820 p->autoEQP = (u8)booleanValue(azArg[1]);
8822 }else{
8823 eputz("Usage: .eqp off|on|trace|trigger|full\n");
8824 rc = 1;
8826 }else
8828 #ifndef SQLITE_SHELL_FIDDLE
8829 if( c=='e' && cli_strncmp(azArg[0], "exit", n)==0 ){
8830 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
8831 rc = 2;
8832 }else
8833 #endif
8835 /* The ".explain" command is automatic now. It is largely pointless. It
8836 ** retained purely for backwards compatibility */
8837 if( c=='e' && cli_strncmp(azArg[0], "explain", n)==0 ){
8838 int val = 1;
8839 if( nArg>=2 ){
8840 if( cli_strcmp(azArg[1],"auto")==0 ){
8841 val = 99;
8842 }else{
8843 val = booleanValue(azArg[1]);
8846 if( val==1 && p->mode!=MODE_Explain ){
8847 p->normalMode = p->mode;
8848 p->mode = MODE_Explain;
8849 p->autoExplain = 0;
8850 }else if( val==0 ){
8851 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8852 p->autoExplain = 0;
8853 }else if( val==99 ){
8854 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
8855 p->autoExplain = 1;
8857 }else
8859 #ifndef SQLITE_OMIT_VIRTUALTABLE
8860 if( c=='e' && cli_strncmp(azArg[0], "expert", n)==0 ){
8861 if( p->bSafeMode ){
8862 sqlite3_fprintf(stderr,
8863 "Cannot run experimental commands such as \"%s\" in safe mode\n",
8864 azArg[0]);
8865 rc = 1;
8866 }else{
8867 open_db(p, 0);
8868 expertDotCommand(p, azArg, nArg);
8870 }else
8871 #endif
8873 if( c=='f' && cli_strncmp(azArg[0], "filectrl", n)==0 ){
8874 static const struct {
8875 const char *zCtrlName; /* Name of a test-control option */
8876 int ctrlCode; /* Integer code for that option */
8877 const char *zUsage; /* Usage notes */
8878 } aCtrl[] = {
8879 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
8880 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
8881 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
8882 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
8883 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
8884 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
8885 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
8886 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
8887 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
8888 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
8889 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
8891 int filectrl = -1;
8892 int iCtrl = -1;
8893 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
8894 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
8895 int n2, i;
8896 const char *zCmd = 0;
8897 const char *zSchema = 0;
8899 open_db(p, 0);
8900 zCmd = nArg>=2 ? azArg[1] : "help";
8902 if( zCmd[0]=='-'
8903 && (cli_strcmp(zCmd,"--schema")==0 || cli_strcmp(zCmd,"-schema")==0)
8904 && nArg>=4
8906 zSchema = azArg[2];
8907 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
8908 nArg -= 2;
8909 zCmd = azArg[1];
8912 /* The argument can optionally begin with "-" or "--" */
8913 if( zCmd[0]=='-' && zCmd[1] ){
8914 zCmd++;
8915 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
8918 /* --help lists all file-controls */
8919 if( cli_strcmp(zCmd,"help")==0 ){
8920 sqlite3_fputs("Available file-controls:\n", p->out);
8921 for(i=0; i<ArraySize(aCtrl); i++){
8922 sqlite3_fprintf(p->out,
8923 " .filectrl %s %s\n", aCtrl[i].zCtrlName, aCtrl[i].zUsage);
8925 rc = 1;
8926 goto meta_command_exit;
8929 /* convert filectrl text option to value. allow any unique prefix
8930 ** of the option name, or a numerical value. */
8931 n2 = strlen30(zCmd);
8932 for(i=0; i<ArraySize(aCtrl); i++){
8933 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
8934 if( filectrl<0 ){
8935 filectrl = aCtrl[i].ctrlCode;
8936 iCtrl = i;
8937 }else{
8938 sqlite3_fprintf(stderr,"Error: ambiguous file-control: \"%s\"\n"
8939 "Use \".filectrl --help\" for help\n", zCmd);
8940 rc = 1;
8941 goto meta_command_exit;
8945 if( filectrl<0 ){
8946 sqlite3_fprintf(stderr,"Error: unknown file-control: %s\n"
8947 "Use \".filectrl --help\" for help\n", zCmd);
8948 }else{
8949 switch(filectrl){
8950 case SQLITE_FCNTL_SIZE_LIMIT: {
8951 if( nArg!=2 && nArg!=3 ) break;
8952 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
8953 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
8954 isOk = 1;
8955 break;
8957 case SQLITE_FCNTL_LOCK_TIMEOUT:
8958 case SQLITE_FCNTL_CHUNK_SIZE: {
8959 int x;
8960 if( nArg!=3 ) break;
8961 x = (int)integerValue(azArg[2]);
8962 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8963 isOk = 2;
8964 break;
8966 case SQLITE_FCNTL_PERSIST_WAL:
8967 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
8968 int x;
8969 if( nArg!=2 && nArg!=3 ) break;
8970 x = nArg==3 ? booleanValue(azArg[2]) : -1;
8971 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8972 iRes = x;
8973 isOk = 1;
8974 break;
8976 case SQLITE_FCNTL_DATA_VERSION:
8977 case SQLITE_FCNTL_HAS_MOVED: {
8978 int x;
8979 if( nArg!=2 ) break;
8980 sqlite3_file_control(p->db, zSchema, filectrl, &x);
8981 iRes = x;
8982 isOk = 1;
8983 break;
8985 case SQLITE_FCNTL_TEMPFILENAME: {
8986 char *z = 0;
8987 if( nArg!=2 ) break;
8988 sqlite3_file_control(p->db, zSchema, filectrl, &z);
8989 if( z ){
8990 sqlite3_fprintf(p->out, "%s\n", z);
8991 sqlite3_free(z);
8993 isOk = 2;
8994 break;
8996 case SQLITE_FCNTL_RESERVE_BYTES: {
8997 int x;
8998 if( nArg>=3 ){
8999 x = atoi(azArg[2]);
9000 sqlite3_file_control(p->db, zSchema, filectrl, &x);
9002 x = -1;
9003 sqlite3_file_control(p->db, zSchema, filectrl, &x);
9004 sqlite3_fprintf(p->out, "%d\n", x);
9005 isOk = 2;
9006 break;
9010 if( isOk==0 && iCtrl>=0 ){
9011 sqlite3_fprintf(p->out, "Usage: .filectrl %s %s\n",
9012 zCmd, aCtrl[iCtrl].zUsage);
9013 rc = 1;
9014 }else if( isOk==1 ){
9015 char zBuf[100];
9016 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
9017 sqlite3_fprintf(p->out, "%s\n", zBuf);
9019 }else
9021 if( c=='f' && cli_strncmp(azArg[0], "fullschema", n)==0 ){
9022 ShellState data;
9023 int doStats = 0;
9024 memcpy(&data, p, sizeof(data));
9025 data.showHeader = 0;
9026 data.cMode = data.mode = MODE_Semi;
9027 if( nArg==2 && optionMatch(azArg[1], "indent") ){
9028 data.cMode = data.mode = MODE_Pretty;
9029 nArg = 1;
9031 if( nArg!=1 ){
9032 eputz("Usage: .fullschema ?--indent?\n");
9033 rc = 1;
9034 goto meta_command_exit;
9036 open_db(p, 0);
9037 rc = sqlite3_exec(p->db,
9038 "SELECT sql FROM"
9039 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
9040 " FROM sqlite_schema UNION ALL"
9041 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
9042 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
9043 "ORDER BY x",
9044 callback, &data, 0
9046 if( rc==SQLITE_OK ){
9047 sqlite3_stmt *pStmt;
9048 rc = sqlite3_prepare_v2(p->db,
9049 "SELECT rowid FROM sqlite_schema"
9050 " WHERE name GLOB 'sqlite_stat[134]'",
9051 -1, &pStmt, 0);
9052 if( rc==SQLITE_OK ){
9053 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
9054 sqlite3_finalize(pStmt);
9057 if( doStats==0 ){
9058 sqlite3_fputs("/* No STAT tables available */\n", p->out);
9059 }else{
9060 sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
9061 data.cMode = data.mode = MODE_Insert;
9062 data.zDestTable = "sqlite_stat1";
9063 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
9064 data.zDestTable = "sqlite_stat4";
9065 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
9066 sqlite3_fputs("ANALYZE sqlite_schema;\n", p->out);
9068 }else
9070 if( c=='h' && cli_strncmp(azArg[0], "headers", n)==0 ){
9071 if( nArg==2 ){
9072 p->showHeader = booleanValue(azArg[1]);
9073 p->shellFlgs |= SHFLG_HeaderSet;
9074 }else{
9075 eputz("Usage: .headers on|off\n");
9076 rc = 1;
9078 }else
9080 if( c=='h' && cli_strncmp(azArg[0], "help", n)==0 ){
9081 if( nArg>=2 ){
9082 n = showHelp(p->out, azArg[1]);
9083 if( n==0 ){
9084 sqlite3_fprintf(p->out, "Nothing matches '%s'\n", azArg[1]);
9086 }else{
9087 showHelp(p->out, 0);
9089 }else
9091 #ifndef SQLITE_SHELL_FIDDLE
9092 if( c=='i' && cli_strncmp(azArg[0], "import", n)==0 ){
9093 char *zTable = 0; /* Insert data into this table */
9094 char *zSchema = 0; /* Schema of zTable */
9095 char *zFile = 0; /* Name of file to extra content from */
9096 sqlite3_stmt *pStmt = NULL; /* A statement */
9097 int nCol; /* Number of columns in the table */
9098 i64 nByte; /* Number of bytes in an SQL string */
9099 int i, j; /* Loop counters */
9100 int needCommit; /* True to COMMIT or ROLLBACK at end */
9101 int nSep; /* Number of bytes in p->colSeparator[] */
9102 char *zSql = 0; /* An SQL statement */
9103 ImportCtx sCtx; /* Reader context */
9104 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
9105 int eVerbose = 0; /* Larger for more console output */
9106 int nSkip = 0; /* Initial lines to skip */
9107 int useOutputMode = 1; /* Use output mode to determine separators */
9108 char *zCreate = 0; /* CREATE TABLE statement text */
9110 failIfSafeMode(p, "cannot run .import in safe mode");
9111 memset(&sCtx, 0, sizeof(sCtx));
9112 if( p->mode==MODE_Ascii ){
9113 xRead = ascii_read_one_field;
9114 }else{
9115 xRead = csv_read_one_field;
9117 rc = 1;
9118 for(i=1; i<nArg; i++){
9119 char *z = azArg[i];
9120 if( z[0]=='-' && z[1]=='-' ) z++;
9121 if( z[0]!='-' ){
9122 if( zFile==0 ){
9123 zFile = z;
9124 }else if( zTable==0 ){
9125 zTable = z;
9126 }else{
9127 sqlite3_fprintf(p->out, "ERROR: extra argument: \"%s\". Usage:\n",z);
9128 showHelp(p->out, "import");
9129 goto meta_command_exit;
9131 }else if( cli_strcmp(z,"-v")==0 ){
9132 eVerbose++;
9133 }else if( cli_strcmp(z,"-schema")==0 && i<nArg-1 ){
9134 zSchema = azArg[++i];
9135 }else if( cli_strcmp(z,"-skip")==0 && i<nArg-1 ){
9136 nSkip = integerValue(azArg[++i]);
9137 }else if( cli_strcmp(z,"-ascii")==0 ){
9138 sCtx.cColSep = SEP_Unit[0];
9139 sCtx.cRowSep = SEP_Record[0];
9140 xRead = ascii_read_one_field;
9141 useOutputMode = 0;
9142 }else if( cli_strcmp(z,"-csv")==0 ){
9143 sCtx.cColSep = ',';
9144 sCtx.cRowSep = '\n';
9145 xRead = csv_read_one_field;
9146 useOutputMode = 0;
9147 }else{
9148 sqlite3_fprintf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
9149 showHelp(p->out, "import");
9150 goto meta_command_exit;
9153 if( zTable==0 ){
9154 sqlite3_fprintf(p->out, "ERROR: missing %s argument. Usage:\n",
9155 zFile==0 ? "FILE" : "TABLE");
9156 showHelp(p->out, "import");
9157 goto meta_command_exit;
9159 seenInterrupt = 0;
9160 open_db(p, 0);
9161 if( useOutputMode ){
9162 /* If neither the --csv or --ascii options are specified, then set
9163 ** the column and row separator characters from the output mode. */
9164 nSep = strlen30(p->colSeparator);
9165 if( nSep==0 ){
9166 eputz("Error: non-null column separator required for import\n");
9167 goto meta_command_exit;
9169 if( nSep>1 ){
9170 eputz("Error: multi-character column separators not allowed"
9171 " for import\n");
9172 goto meta_command_exit;
9174 nSep = strlen30(p->rowSeparator);
9175 if( nSep==0 ){
9176 eputz("Error: non-null row separator required for import\n");
9177 goto meta_command_exit;
9179 if( nSep==2 && p->mode==MODE_Csv
9180 && cli_strcmp(p->rowSeparator,SEP_CrLf)==0
9182 /* When importing CSV (only), if the row separator is set to the
9183 ** default output row separator, change it to the default input
9184 ** row separator. This avoids having to maintain different input
9185 ** and output row separators. */
9186 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9187 nSep = strlen30(p->rowSeparator);
9189 if( nSep>1 ){
9190 eputz("Error: multi-character row separators not allowed"
9191 " for import\n");
9192 goto meta_command_exit;
9194 sCtx.cColSep = (u8)p->colSeparator[0];
9195 sCtx.cRowSep = (u8)p->rowSeparator[0];
9197 sCtx.zFile = zFile;
9198 sCtx.nLine = 1;
9199 if( sCtx.zFile[0]=='|' ){
9200 #ifdef SQLITE_OMIT_POPEN
9201 eputz("Error: pipes are not supported in this OS\n");
9202 goto meta_command_exit;
9203 #else
9204 sCtx.in = sqlite3_popen(sCtx.zFile+1, "r");
9205 sCtx.zFile = "<pipe>";
9206 sCtx.xCloser = pclose;
9207 #endif
9208 }else{
9209 sCtx.in = sqlite3_fopen(sCtx.zFile, "rb");
9210 sCtx.xCloser = fclose;
9212 if( sCtx.in==0 ){
9213 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zFile);
9214 goto meta_command_exit;
9216 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
9217 char zSep[2];
9218 zSep[1] = 0;
9219 zSep[0] = sCtx.cColSep;
9220 sqlite3_fputs("Column separator ", p->out);
9221 output_c_string(p->out, zSep);
9222 sqlite3_fputs(", row separator ", p->out);
9223 zSep[0] = sCtx.cRowSep;
9224 output_c_string(p->out, zSep);
9225 sqlite3_fputs("\n", p->out);
9227 sCtx.z = sqlite3_malloc64(120);
9228 if( sCtx.z==0 ){
9229 import_cleanup(&sCtx);
9230 shell_out_of_memory();
9232 /* Below, resources must be freed before exit. */
9233 while( (nSkip--)>0 ){
9234 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
9236 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
9237 if( sqlite3_table_column_metadata(p->db, zSchema, zTable,0,0,0,0,0,0) ){
9238 /* Table does not exist. Create it. */
9239 sqlite3 *dbCols = 0;
9240 char *zRenames = 0;
9241 char *zColDefs;
9242 zCreate = sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
9243 zSchema ? zSchema : "main", zTable);
9244 while( xRead(&sCtx) ){
9245 zAutoColumn(sCtx.z, &dbCols, 0);
9246 if( sCtx.cTerm!=sCtx.cColSep ) break;
9248 zColDefs = zAutoColumn(0, &dbCols, &zRenames);
9249 if( zRenames!=0 ){
9250 sqlite3_fprintf((stdin_is_interactive && p->in==stdin)? p->out : stderr,
9251 "Columns renamed during .import %s due to duplicates:\n"
9252 "%s\n", sCtx.zFile, zRenames);
9253 sqlite3_free(zRenames);
9255 assert(dbCols==0);
9256 if( zColDefs==0 ){
9257 sqlite3_fprintf(stderr,"%s: empty file\n", sCtx.zFile);
9258 import_cleanup(&sCtx);
9259 rc = 1;
9260 sqlite3_free(zCreate);
9261 goto meta_command_exit;
9263 zCreate = sqlite3_mprintf("%z%z\n", zCreate, zColDefs);
9264 if( zCreate==0 ){
9265 import_cleanup(&sCtx);
9266 shell_out_of_memory();
9268 if( eVerbose>=1 ){
9269 sqlite3_fprintf(p->out, "%s\n", zCreate);
9271 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
9272 if( rc ){
9273 sqlite3_fprintf(stderr,
9274 "%s failed:\n%s\n", zCreate, sqlite3_errmsg(p->db));
9276 sqlite3_free(zCreate);
9277 zCreate = 0;
9278 if( rc ){
9279 import_cleanup(&sCtx);
9280 rc = 1;
9281 goto meta_command_exit;
9284 zSql = sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
9285 zTable, zSchema);
9286 if( zSql==0 ){
9287 import_cleanup(&sCtx);
9288 shell_out_of_memory();
9290 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9291 sqlite3_free(zSql);
9292 zSql = 0;
9293 if( rc ){
9294 if (pStmt) sqlite3_finalize(pStmt);
9295 shellDatabaseError(p->db);
9296 import_cleanup(&sCtx);
9297 rc = 1;
9298 goto meta_command_exit;
9300 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9301 nCol = sqlite3_column_int(pStmt, 0);
9302 }else{
9303 nCol = 0;
9305 sqlite3_finalize(pStmt);
9306 pStmt = 0;
9307 if( nCol==0 ) return 0; /* no columns, no error */
9309 nByte = 64 /* space for "INSERT INTO", "VALUES(", ")\0" */
9310 + (zSchema ? strlen(zSchema)*2 + 2: 0) /* Quoted schema name */
9311 + strlen(zTable)*2 + 2 /* Quoted table name */
9312 + nCol*2; /* Space for ",?" for each column */
9313 zSql = sqlite3_malloc64( nByte );
9314 if( zSql==0 ){
9315 import_cleanup(&sCtx);
9316 shell_out_of_memory();
9318 if( zSchema ){
9319 sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\".\"%w\" VALUES(?",
9320 zSchema, zTable);
9321 }else{
9322 sqlite3_snprintf(nByte, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
9324 j = strlen30(zSql);
9325 for(i=1; i<nCol; i++){
9326 zSql[j++] = ',';
9327 zSql[j++] = '?';
9329 zSql[j++] = ')';
9330 zSql[j] = 0;
9331 assert( j<nByte );
9332 if( eVerbose>=2 ){
9333 sqlite3_fprintf(p->out, "Insert using: %s\n", zSql);
9335 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9336 sqlite3_free(zSql);
9337 zSql = 0;
9338 if( rc ){
9339 shellDatabaseError(p->db);
9340 if (pStmt) sqlite3_finalize(pStmt);
9341 import_cleanup(&sCtx);
9342 rc = 1;
9343 goto meta_command_exit;
9345 needCommit = sqlite3_get_autocommit(p->db);
9346 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
9348 int startLine = sCtx.nLine;
9349 for(i=0; i<nCol; i++){
9350 char *z = xRead(&sCtx);
9352 ** Did we reach end-of-file before finding any columns?
9353 ** If so, stop instead of NULL filling the remaining columns.
9355 if( z==0 && i==0 ) break;
9357 ** Did we reach end-of-file OR end-of-line before finding any
9358 ** columns in ASCII mode? If so, stop instead of NULL filling
9359 ** the remaining columns.
9361 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
9363 ** For CSV mode, per RFC 4180, accept EOF in lieu of final
9364 ** record terminator but only for last field of multi-field row.
9365 ** (If there are too few fields, it's not valid CSV anyway.)
9367 if( z==0 && (xRead==csv_read_one_field) && i==nCol-1 && i>0 ){
9368 z = "";
9370 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
9371 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
9372 sqlite3_fprintf(stderr,"%s:%d: expected %d columns but found %d"
9373 " - filling the rest with NULL\n",
9374 sCtx.zFile, startLine, nCol, i+1);
9375 i += 2;
9376 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
9379 if( sCtx.cTerm==sCtx.cColSep ){
9381 xRead(&sCtx);
9382 i++;
9383 }while( sCtx.cTerm==sCtx.cColSep );
9384 sqlite3_fprintf(stderr,
9385 "%s:%d: expected %d columns but found %d - extras ignored\n",
9386 sCtx.zFile, startLine, nCol, i);
9388 if( i>=nCol ){
9389 sqlite3_step(pStmt);
9390 rc = sqlite3_reset(pStmt);
9391 if( rc!=SQLITE_OK ){
9392 sqlite3_fprintf(stderr,"%s:%d: INSERT failed: %s\n",
9393 sCtx.zFile, startLine, sqlite3_errmsg(p->db));
9394 sCtx.nErr++;
9395 }else{
9396 sCtx.nRow++;
9399 }while( sCtx.cTerm!=EOF );
9401 import_cleanup(&sCtx);
9402 sqlite3_finalize(pStmt);
9403 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
9404 if( eVerbose>0 ){
9405 sqlite3_fprintf(p->out,
9406 "Added %d rows with %d errors using %d lines of input\n",
9407 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
9409 }else
9410 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9412 #ifndef SQLITE_UNTESTABLE
9413 if( c=='i' && cli_strncmp(azArg[0], "imposter", n)==0 ){
9414 char *zSql;
9415 char *zCollist = 0;
9416 sqlite3_stmt *pStmt;
9417 int tnum = 0;
9418 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
9419 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
9420 int i;
9421 if( !ShellHasFlag(p,SHFLG_TestingMode) ){
9422 sqlite3_fprintf(stderr,".%s unavailable without --unsafe-testing\n",
9423 "imposter");
9424 rc = 1;
9425 goto meta_command_exit;
9427 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
9428 eputz("Usage: .imposter INDEX IMPOSTER\n"
9429 " .imposter off\n");
9430 /* Also allowed, but not documented:
9432 ** .imposter TABLE IMPOSTER
9434 ** where TABLE is a WITHOUT ROWID table. In that case, the
9435 ** imposter is another WITHOUT ROWID table with the columns in
9436 ** storage order. */
9437 rc = 1;
9438 goto meta_command_exit;
9440 open_db(p, 0);
9441 if( nArg==2 ){
9442 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
9443 goto meta_command_exit;
9445 zSql = sqlite3_mprintf(
9446 "SELECT rootpage, 0 FROM sqlite_schema"
9447 " WHERE name='%q' AND type='index'"
9448 "UNION ALL "
9449 "SELECT rootpage, 1 FROM sqlite_schema"
9450 " WHERE name='%q' AND type='table'"
9451 " AND sql LIKE '%%without%%rowid%%'",
9452 azArg[1], azArg[1]
9454 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9455 sqlite3_free(zSql);
9456 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9457 tnum = sqlite3_column_int(pStmt, 0);
9458 isWO = sqlite3_column_int(pStmt, 1);
9460 sqlite3_finalize(pStmt);
9461 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
9462 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9463 sqlite3_free(zSql);
9464 i = 0;
9465 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9466 char zLabel[20];
9467 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
9468 i++;
9469 if( zCol==0 ){
9470 if( sqlite3_column_int(pStmt,1)==-1 ){
9471 zCol = "_ROWID_";
9472 }else{
9473 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
9474 zCol = zLabel;
9477 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
9478 lenPK = (int)strlen(zCollist);
9480 if( zCollist==0 ){
9481 zCollist = sqlite3_mprintf("\"%w\"", zCol);
9482 }else{
9483 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
9486 sqlite3_finalize(pStmt);
9487 if( i==0 || tnum==0 ){
9488 sqlite3_fprintf(stderr,"no such index: \"%s\"\n", azArg[1]);
9489 rc = 1;
9490 sqlite3_free(zCollist);
9491 goto meta_command_exit;
9493 if( lenPK==0 ) lenPK = 100000;
9494 zSql = sqlite3_mprintf(
9495 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9496 azArg[2], zCollist, lenPK, zCollist);
9497 sqlite3_free(zCollist);
9498 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
9499 if( rc==SQLITE_OK ){
9500 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
9501 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
9502 if( rc ){
9503 sqlite3_fprintf(stderr,
9504 "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
9505 }else{
9506 sqlite3_fprintf(stdout, "%s;\n", zSql);
9507 sqlite3_fprintf(stdout,
9508 "WARNING: writing to an imposter table will corrupt"
9509 " the \"%s\" %s!\n", azArg[1], isWO ? "table" : "index");
9511 }else{
9512 sqlite3_fprintf(stderr,"SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
9513 rc = 1;
9515 sqlite3_free(zSql);
9516 }else
9517 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9519 if( c=='i' && cli_strncmp(azArg[0], "intck", n)==0 ){
9520 i64 iArg = 0;
9521 if( nArg==2 ){
9522 iArg = integerValue(azArg[1]);
9523 if( iArg==0 ) iArg = -1;
9525 if( (nArg!=1 && nArg!=2) || iArg<0 ){
9526 sqlite3_fprintf(stderr,"%s","Usage: .intck STEPS_PER_UNLOCK\n");
9527 rc = 1;
9528 goto meta_command_exit;
9530 open_db(p, 0);
9531 rc = intckDatabaseCmd(p, iArg);
9532 }else
9534 #ifdef SQLITE_ENABLE_IOTRACE
9535 if( c=='i' && cli_strncmp(azArg[0], "iotrace", n)==0 ){
9536 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
9537 if( iotrace && iotrace!=stdout ) fclose(iotrace);
9538 iotrace = 0;
9539 if( nArg<2 ){
9540 sqlite3IoTrace = 0;
9541 }else if( cli_strcmp(azArg[1], "-")==0 ){
9542 sqlite3IoTrace = iotracePrintf;
9543 iotrace = stdout;
9544 }else{
9545 iotrace = sqlite3_fopen(azArg[1], "w");
9546 if( iotrace==0 ){
9547 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
9548 sqlite3IoTrace = 0;
9549 rc = 1;
9550 }else{
9551 sqlite3IoTrace = iotracePrintf;
9554 }else
9555 #endif
9557 if( c=='l' && n>=5 && cli_strncmp(azArg[0], "limits", n)==0 ){
9558 static const struct {
9559 const char *zLimitName; /* Name of a limit */
9560 int limitCode; /* Integer code for that limit */
9561 } aLimit[] = {
9562 { "length", SQLITE_LIMIT_LENGTH },
9563 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
9564 { "column", SQLITE_LIMIT_COLUMN },
9565 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
9566 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
9567 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
9568 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
9569 { "attached", SQLITE_LIMIT_ATTACHED },
9570 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
9571 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
9572 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
9573 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
9575 int i, n2;
9576 open_db(p, 0);
9577 if( nArg==1 ){
9578 for(i=0; i<ArraySize(aLimit); i++){
9579 sqlite3_fprintf(stdout, "%20s %d\n", aLimit[i].zLimitName,
9580 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
9582 }else if( nArg>3 ){
9583 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
9584 rc = 1;
9585 goto meta_command_exit;
9586 }else{
9587 int iLimit = -1;
9588 n2 = strlen30(azArg[1]);
9589 for(i=0; i<ArraySize(aLimit); i++){
9590 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
9591 if( iLimit<0 ){
9592 iLimit = i;
9593 }else{
9594 sqlite3_fprintf(stderr,"ambiguous limit: \"%s\"\n", azArg[1]);
9595 rc = 1;
9596 goto meta_command_exit;
9600 if( iLimit<0 ){
9601 sqlite3_fprintf(stderr,"unknown limit: \"%s\"\n"
9602 "enter \".limits\" with no arguments for a list.\n",
9603 azArg[1]);
9604 rc = 1;
9605 goto meta_command_exit;
9607 if( nArg==3 ){
9608 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
9609 (int)integerValue(azArg[2]));
9611 sqlite3_fprintf(stdout, "%20s %d\n", aLimit[iLimit].zLimitName,
9612 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
9614 }else
9616 if( c=='l' && n>2 && cli_strncmp(azArg[0], "lint", n)==0 ){
9617 open_db(p, 0);
9618 lintDotCommand(p, azArg, nArg);
9619 }else
9621 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9622 if( c=='l' && cli_strncmp(azArg[0], "load", n)==0 ){
9623 const char *zFile, *zProc;
9624 char *zErrMsg = 0;
9625 failIfSafeMode(p, "cannot run .load in safe mode");
9626 if( nArg<2 || azArg[1][0]==0 ){
9627 /* Must have a non-empty FILE. (Will not load self.) */
9628 eputz("Usage: .load FILE ?ENTRYPOINT?\n");
9629 rc = 1;
9630 goto meta_command_exit;
9632 zFile = azArg[1];
9633 zProc = nArg>=3 ? azArg[2] : 0;
9634 open_db(p, 0);
9635 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
9636 if( rc!=SQLITE_OK ){
9637 shellEmitError(zErrMsg);
9638 sqlite3_free(zErrMsg);
9639 rc = 1;
9641 }else
9642 #endif
9644 if( c=='l' && cli_strncmp(azArg[0], "log", n)==0 ){
9645 if( nArg!=2 ){
9646 eputz("Usage: .log FILENAME\n");
9647 rc = 1;
9648 }else{
9649 const char *zFile = azArg[1];
9650 if( p->bSafeMode
9651 && cli_strcmp(zFile,"on")!=0
9652 && cli_strcmp(zFile,"off")!=0
9654 sputz(stdout, "cannot set .log to anything other"
9655 " than \"on\" or \"off\"\n");
9656 zFile = "off";
9658 output_file_close(p->pLog);
9659 if( cli_strcmp(zFile,"on")==0 ) zFile = "stdout";
9660 p->pLog = output_file_open(zFile);
9662 }else
9664 if( c=='m' && cli_strncmp(azArg[0], "mode", n)==0 ){
9665 const char *zMode = 0;
9666 const char *zTabname = 0;
9667 int i, n2;
9668 ColModeOpts cmOpts = ColModeOpts_default;
9669 for(i=1; i<nArg; i++){
9670 const char *z = azArg[i];
9671 if( optionMatch(z,"wrap") && i+1<nArg ){
9672 cmOpts.iWrap = integerValue(azArg[++i]);
9673 }else if( optionMatch(z,"ww") ){
9674 cmOpts.bWordWrap = 1;
9675 }else if( optionMatch(z,"wordwrap") && i+1<nArg ){
9676 cmOpts.bWordWrap = (u8)booleanValue(azArg[++i]);
9677 }else if( optionMatch(z,"quote") ){
9678 cmOpts.bQuote = 1;
9679 }else if( optionMatch(z,"noquote") ){
9680 cmOpts.bQuote = 0;
9681 }else if( zMode==0 ){
9682 zMode = z;
9683 /* Apply defaults for qbox pseudo-mode. If that
9684 * overwrites already-set values, user was informed of this.
9686 if( cli_strcmp(z, "qbox")==0 ){
9687 ColModeOpts cmo = ColModeOpts_default_qbox;
9688 zMode = "box";
9689 cmOpts = cmo;
9691 }else if( zTabname==0 ){
9692 zTabname = z;
9693 }else if( z[0]=='-' ){
9694 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
9695 eputz("options:\n"
9696 " --noquote\n"
9697 " --quote\n"
9698 " --wordwrap on/off\n"
9699 " --wrap N\n"
9700 " --ww\n");
9701 rc = 1;
9702 goto meta_command_exit;
9703 }else{
9704 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
9705 rc = 1;
9706 goto meta_command_exit;
9709 if( zMode==0 ){
9710 if( p->mode==MODE_Column
9711 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
9713 sqlite3_fprintf(p->out,
9714 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9715 modeDescr[p->mode], p->cmOpts.iWrap,
9716 p->cmOpts.bWordWrap ? "on" : "off",
9717 p->cmOpts.bQuote ? "" : "no");
9718 }else{
9719 sqlite3_fprintf(p->out,
9720 "current output mode: %s\n", modeDescr[p->mode]);
9722 zMode = modeDescr[p->mode];
9724 n2 = strlen30(zMode);
9725 if( cli_strncmp(zMode,"lines",n2)==0 ){
9726 p->mode = MODE_Line;
9727 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9728 }else if( cli_strncmp(zMode,"columns",n2)==0 ){
9729 p->mode = MODE_Column;
9730 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
9731 p->showHeader = 1;
9733 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9734 p->cmOpts = cmOpts;
9735 }else if( cli_strncmp(zMode,"list",n2)==0 ){
9736 p->mode = MODE_List;
9737 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
9738 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9739 }else if( cli_strncmp(zMode,"html",n2)==0 ){
9740 p->mode = MODE_Html;
9741 }else if( cli_strncmp(zMode,"tcl",n2)==0 ){
9742 p->mode = MODE_Tcl;
9743 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
9744 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9745 }else if( cli_strncmp(zMode,"csv",n2)==0 ){
9746 p->mode = MODE_Csv;
9747 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9748 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9749 }else if( cli_strncmp(zMode,"tabs",n2)==0 ){
9750 p->mode = MODE_List;
9751 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
9752 }else if( cli_strncmp(zMode,"insert",n2)==0 ){
9753 p->mode = MODE_Insert;
9754 set_table_name(p, zTabname ? zTabname : "table");
9755 }else if( cli_strncmp(zMode,"quote",n2)==0 ){
9756 p->mode = MODE_Quote;
9757 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9758 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
9759 }else if( cli_strncmp(zMode,"ascii",n2)==0 ){
9760 p->mode = MODE_Ascii;
9761 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
9762 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
9763 }else if( cli_strncmp(zMode,"markdown",n2)==0 ){
9764 p->mode = MODE_Markdown;
9765 p->cmOpts = cmOpts;
9766 }else if( cli_strncmp(zMode,"table",n2)==0 ){
9767 p->mode = MODE_Table;
9768 p->cmOpts = cmOpts;
9769 }else if( cli_strncmp(zMode,"box",n2)==0 ){
9770 p->mode = MODE_Box;
9771 p->cmOpts = cmOpts;
9772 }else if( cli_strncmp(zMode,"count",n2)==0 ){
9773 p->mode = MODE_Count;
9774 }else if( cli_strncmp(zMode,"off",n2)==0 ){
9775 p->mode = MODE_Off;
9776 }else if( cli_strncmp(zMode,"json",n2)==0 ){
9777 p->mode = MODE_Json;
9778 }else{
9779 eputz("Error: mode should be one of: "
9780 "ascii box column csv html insert json line list markdown "
9781 "qbox quote table tabs tcl\n");
9782 rc = 1;
9784 p->cMode = p->mode;
9785 }else
9787 #ifndef SQLITE_SHELL_FIDDLE
9788 if( c=='n' && cli_strcmp(azArg[0], "nonce")==0 ){
9789 if( nArg!=2 ){
9790 eputz("Usage: .nonce NONCE\n");
9791 rc = 1;
9792 }else if( p->zNonce==0 || cli_strcmp(azArg[1],p->zNonce)!=0 ){
9793 sqlite3_fprintf(stderr,"line %d: incorrect nonce: \"%s\"\n",
9794 p->lineno, azArg[1]);
9795 exit(1);
9796 }else{
9797 p->bSafeMode = 0;
9798 return 0; /* Return immediately to bypass the safe mode reset
9799 ** at the end of this procedure */
9801 }else
9802 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9804 if( c=='n' && cli_strncmp(azArg[0], "nullvalue", n)==0 ){
9805 if( nArg==2 ){
9806 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
9807 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
9808 }else{
9809 eputz("Usage: .nullvalue STRING\n");
9810 rc = 1;
9812 }else
9814 if( c=='o' && cli_strncmp(azArg[0], "open", n)==0 && n>=2 ){
9815 const char *zFN = 0; /* Pointer to constant filename */
9816 char *zNewFilename = 0; /* Name of the database file to open */
9817 int iName = 1; /* Index in azArg[] of the filename */
9818 int newFlag = 0; /* True to delete file before opening */
9819 int openMode = SHELL_OPEN_UNSPEC;
9821 /* Check for command-line arguments */
9822 for(iName=1; iName<nArg; iName++){
9823 const char *z = azArg[iName];
9824 #ifndef SQLITE_SHELL_FIDDLE
9825 if( optionMatch(z,"new") ){
9826 newFlag = 1;
9827 #ifdef SQLITE_HAVE_ZLIB
9828 }else if( optionMatch(z, "zip") ){
9829 openMode = SHELL_OPEN_ZIPFILE;
9830 #endif
9831 }else if( optionMatch(z, "append") ){
9832 openMode = SHELL_OPEN_APPENDVFS;
9833 }else if( optionMatch(z, "readonly") ){
9834 openMode = SHELL_OPEN_READONLY;
9835 }else if( optionMatch(z, "nofollow") ){
9836 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
9837 #ifndef SQLITE_OMIT_DESERIALIZE
9838 }else if( optionMatch(z, "deserialize") ){
9839 openMode = SHELL_OPEN_DESERIALIZE;
9840 }else if( optionMatch(z, "hexdb") ){
9841 openMode = SHELL_OPEN_HEXDB;
9842 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
9843 p->szMax = integerValue(azArg[++iName]);
9844 #endif /* SQLITE_OMIT_DESERIALIZE */
9845 }else
9846 #endif /* !SQLITE_SHELL_FIDDLE */
9847 if( z[0]=='-' ){
9848 sqlite3_fprintf(stderr,"unknown option: %s\n", z);
9849 rc = 1;
9850 goto meta_command_exit;
9851 }else if( zFN ){
9852 sqlite3_fprintf(stderr,"extra argument: \"%s\"\n", z);
9853 rc = 1;
9854 goto meta_command_exit;
9855 }else{
9856 zFN = z;
9860 /* Close the existing database */
9861 session_close_all(p, -1);
9862 close_db(p->db);
9863 p->db = 0;
9864 p->pAuxDb->zDbFilename = 0;
9865 sqlite3_free(p->pAuxDb->zFreeOnClose);
9866 p->pAuxDb->zFreeOnClose = 0;
9867 p->openMode = openMode;
9868 p->openFlags = 0;
9869 p->szMax = 0;
9871 /* If a filename is specified, try to open it first */
9872 if( zFN || p->openMode==SHELL_OPEN_HEXDB ){
9873 if( newFlag && zFN && !p->bSafeMode ) shellDeleteFile(zFN);
9874 #ifndef SQLITE_SHELL_FIDDLE
9875 if( p->bSafeMode
9876 && p->openMode!=SHELL_OPEN_HEXDB
9877 && zFN
9878 && cli_strcmp(zFN,":memory:")!=0
9880 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
9882 #else
9883 /* WASM mode has its own sandboxed pseudo-filesystem. */
9884 #endif
9885 if( zFN ){
9886 zNewFilename = sqlite3_mprintf("%s", zFN);
9887 shell_check_oom(zNewFilename);
9888 }else{
9889 zNewFilename = 0;
9891 p->pAuxDb->zDbFilename = zNewFilename;
9892 open_db(p, OPEN_DB_KEEPALIVE);
9893 if( p->db==0 ){
9894 sqlite3_fprintf(stderr,"Error: cannot open '%s'\n", zNewFilename);
9895 sqlite3_free(zNewFilename);
9896 }else{
9897 p->pAuxDb->zFreeOnClose = zNewFilename;
9900 if( p->db==0 ){
9901 /* As a fall-back open a TEMP database */
9902 p->pAuxDb->zDbFilename = 0;
9903 open_db(p, 0);
9905 }else
9907 #ifndef SQLITE_SHELL_FIDDLE
9908 if( (c=='o'
9909 && (cli_strncmp(azArg[0], "output", n)==0
9910 || cli_strncmp(azArg[0], "once", n)==0))
9911 || (c=='e' && n==5 && cli_strcmp(azArg[0],"excel")==0)
9912 || (c=='w' && n==3 && cli_strcmp(azArg[0],"www")==0)
9914 char *zFile = 0;
9915 int i;
9916 int eMode = 0;
9917 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel/.www */
9918 int bPlain = 0; /* --plain option */
9919 static const char *zBomUtf8 = "\357\273\277";
9920 const char *zBom = 0;
9922 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
9923 if( c=='e' ){
9924 eMode = 'x';
9925 bOnce = 2;
9926 }else if( c=='w' ){
9927 eMode = 'w';
9928 bOnce = 2;
9929 }else if( cli_strncmp(azArg[0],"once",n)==0 ){
9930 bOnce = 1;
9932 for(i=1; i<nArg; i++){
9933 char *z = azArg[i];
9934 if( z[0]=='-' ){
9935 if( z[1]=='-' ) z++;
9936 if( cli_strcmp(z,"-bom")==0 ){
9937 zBom = zBomUtf8;
9938 }else if( cli_strcmp(z,"-plain")==0 ){
9939 bPlain = 1;
9940 }else if( c=='o' && cli_strcmp(z,"-x")==0 ){
9941 eMode = 'x'; /* spreadsheet */
9942 }else if( c=='o' && cli_strcmp(z,"-e")==0 ){
9943 eMode = 'e'; /* text editor */
9944 }else if( c=='o' && cli_strcmp(z,"-w")==0 ){
9945 eMode = 'w'; /* Web browser */
9946 }else{
9947 sqlite3_fprintf(p->out,
9948 "ERROR: unknown option: \"%s\". Usage:\n", azArg[i]);
9949 showHelp(p->out, azArg[0]);
9950 rc = 1;
9951 goto meta_command_exit;
9953 }else if( zFile==0 && eMode==0 ){
9954 zFile = sqlite3_mprintf("%s", z);
9955 if( zFile && zFile[0]=='|' ){
9956 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
9957 break;
9959 }else{
9960 sqlite3_fprintf(p->out,
9961 "ERROR: extra parameter: \"%s\". Usage:\n", azArg[i]);
9962 showHelp(p->out, azArg[0]);
9963 rc = 1;
9964 sqlite3_free(zFile);
9965 goto meta_command_exit;
9968 if( zFile==0 ){
9969 zFile = sqlite3_mprintf("stdout");
9971 if( bOnce ){
9972 p->outCount = 2;
9973 }else{
9974 p->outCount = 0;
9976 output_reset(p);
9977 #ifndef SQLITE_NOHAVE_SYSTEM
9978 if( eMode=='e' || eMode=='x' || eMode=='w' ){
9979 p->doXdgOpen = 1;
9980 outputModePush(p);
9981 if( eMode=='x' ){
9982 /* spreadsheet mode. Output as CSV. */
9983 newTempFile(p, "csv");
9984 ShellClearFlag(p, SHFLG_Echo);
9985 p->mode = MODE_Csv;
9986 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
9987 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
9988 #ifdef _WIN32
9989 zBom = zBomUtf8; /* Always include the BOM on Windows, as Excel does
9990 ** not work without it. */
9991 #endif
9992 }else if( eMode=='w' ){
9993 /* web-browser mode. */
9994 newTempFile(p, "html");
9995 if( !bPlain ) p->mode = MODE_Www;
9996 }else{
9997 /* text editor mode */
9998 newTempFile(p, "txt");
10000 sqlite3_free(zFile);
10001 zFile = sqlite3_mprintf("%s", p->zTempFile);
10003 #endif /* SQLITE_NOHAVE_SYSTEM */
10004 shell_check_oom(zFile);
10005 if( zFile[0]=='|' ){
10006 #ifdef SQLITE_OMIT_POPEN
10007 eputz("Error: pipes are not supported in this OS\n");
10008 rc = 1;
10009 output_redir(p, stdout);
10010 #else
10011 FILE *pfPipe = sqlite3_popen(zFile + 1, "w");
10012 if( pfPipe==0 ){
10013 sqlite3_fprintf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
10014 rc = 1;
10015 }else{
10016 output_redir(p, pfPipe);
10017 if( zBom ) sqlite3_fputs(zBom, pfPipe);
10018 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
10020 #endif
10021 }else{
10022 FILE *pfFile = output_file_open(zFile);
10023 if( pfFile==0 ){
10024 if( cli_strcmp(zFile,"off")!=0 ){
10025 sqlite3_fprintf(stderr,"Error: cannot write to \"%s\"\n", zFile);
10027 rc = 1;
10028 } else {
10029 output_redir(p, pfFile);
10030 if( zBom ) sqlite3_fputs(zBom, pfFile);
10031 if( bPlain && eMode=='w' ){
10032 sqlite3_fputs(
10033 "<!DOCTYPE html>\n<BODY>\n<PLAINTEXT>\n",
10034 pfFile
10037 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
10040 sqlite3_free(zFile);
10041 }else
10042 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10044 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "parameter", n)==0 ){
10045 open_db(p,0);
10046 if( nArg<=1 ) goto parameter_syntax_error;
10048 /* .parameter clear
10049 ** Clear all bind parameters by dropping the TEMP table that holds them.
10051 if( nArg==2 && cli_strcmp(azArg[1],"clear")==0 ){
10052 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
10053 0, 0, 0);
10054 }else
10056 /* .parameter list
10057 ** List all bind parameters.
10059 if( nArg==2 && cli_strcmp(azArg[1],"list")==0 ){
10060 sqlite3_stmt *pStmt = 0;
10061 int rx;
10062 int len = 0;
10063 rx = sqlite3_prepare_v2(p->db,
10064 "SELECT max(length(key)) "
10065 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
10066 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
10067 len = sqlite3_column_int(pStmt, 0);
10068 if( len>40 ) len = 40;
10070 sqlite3_finalize(pStmt);
10071 pStmt = 0;
10072 if( len ){
10073 rx = sqlite3_prepare_v2(p->db,
10074 "SELECT key, quote(value) "
10075 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
10076 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
10077 sqlite3_fprintf(p->out,
10078 "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
10079 sqlite3_column_text(pStmt,1));
10081 sqlite3_finalize(pStmt);
10083 }else
10085 /* .parameter init
10086 ** Make sure the TEMP table used to hold bind parameters exists.
10087 ** Create it if necessary.
10089 if( nArg==2 && cli_strcmp(azArg[1],"init")==0 ){
10090 bind_table_init(p);
10091 }else
10093 /* .parameter set NAME VALUE
10094 ** Set or reset a bind parameter. NAME should be the full parameter
10095 ** name exactly as it appears in the query. (ex: $abc, @def). The
10096 ** VALUE can be in either SQL literal notation, or if not it will be
10097 ** understood to be a text string.
10099 if( nArg==4 && cli_strcmp(azArg[1],"set")==0 ){
10100 int rx;
10101 char *zSql;
10102 sqlite3_stmt *pStmt;
10103 const char *zKey = azArg[2];
10104 const char *zValue = azArg[3];
10105 bind_table_init(p);
10106 zSql = sqlite3_mprintf(
10107 "REPLACE INTO temp.sqlite_parameters(key,value)"
10108 "VALUES(%Q,%s);", zKey, zValue);
10109 shell_check_oom(zSql);
10110 pStmt = 0;
10111 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10112 sqlite3_free(zSql);
10113 if( rx!=SQLITE_OK ){
10114 sqlite3_finalize(pStmt);
10115 pStmt = 0;
10116 zSql = sqlite3_mprintf(
10117 "REPLACE INTO temp.sqlite_parameters(key,value)"
10118 "VALUES(%Q,%Q);", zKey, zValue);
10119 shell_check_oom(zSql);
10120 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10121 sqlite3_free(zSql);
10122 if( rx!=SQLITE_OK ){
10123 sqlite3_fprintf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
10124 sqlite3_finalize(pStmt);
10125 pStmt = 0;
10126 rc = 1;
10129 sqlite3_step(pStmt);
10130 sqlite3_finalize(pStmt);
10131 }else
10133 /* .parameter unset NAME
10134 ** Remove the NAME binding from the parameter binding table, if it
10135 ** exists.
10137 if( nArg==3 && cli_strcmp(azArg[1],"unset")==0 ){
10138 char *zSql = sqlite3_mprintf(
10139 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
10140 shell_check_oom(zSql);
10141 sqlite3_exec(p->db, zSql, 0, 0, 0);
10142 sqlite3_free(zSql);
10143 }else
10144 /* If no command name matches, show a syntax error */
10145 parameter_syntax_error:
10146 showHelp(p->out, "parameter");
10147 }else
10149 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "print", n)==0 ){
10150 int i;
10151 for(i=1; i<nArg; i++){
10152 if( i>1 ) sqlite3_fputs(" ", p->out);
10153 sqlite3_fputs(azArg[i], p->out);
10155 sqlite3_fputs("\n", p->out);
10156 }else
10158 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10159 if( c=='p' && n>=3 && cli_strncmp(azArg[0], "progress", n)==0 ){
10160 int i;
10161 int nn = 0;
10162 p->flgProgress = 0;
10163 p->mxProgress = 0;
10164 p->nProgress = 0;
10165 for(i=1; i<nArg; i++){
10166 const char *z = azArg[i];
10167 if( z[0]=='-' ){
10168 z++;
10169 if( z[0]=='-' ) z++;
10170 if( cli_strcmp(z,"quiet")==0 || cli_strcmp(z,"q")==0 ){
10171 p->flgProgress |= SHELL_PROGRESS_QUIET;
10172 continue;
10174 if( cli_strcmp(z,"reset")==0 ){
10175 p->flgProgress |= SHELL_PROGRESS_RESET;
10176 continue;
10178 if( cli_strcmp(z,"once")==0 ){
10179 p->flgProgress |= SHELL_PROGRESS_ONCE;
10180 continue;
10182 if( cli_strcmp(z,"limit")==0 ){
10183 if( i+1>=nArg ){
10184 eputz("Error: missing argument on --limit\n");
10185 rc = 1;
10186 goto meta_command_exit;
10187 }else{
10188 p->mxProgress = (int)integerValue(azArg[++i]);
10190 continue;
10192 sqlite3_fprintf(stderr,"Error: unknown option: \"%s\"\n", azArg[i]);
10193 rc = 1;
10194 goto meta_command_exit;
10195 }else{
10196 nn = (int)integerValue(z);
10199 open_db(p, 0);
10200 sqlite3_progress_handler(p->db, nn, progress_handler, p);
10201 }else
10202 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10204 if( c=='p' && cli_strncmp(azArg[0], "prompt", n)==0 ){
10205 if( nArg >= 2) {
10206 shell_strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
10208 if( nArg >= 3) {
10209 shell_strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
10211 }else
10213 #ifndef SQLITE_SHELL_FIDDLE
10214 if( c=='q' && cli_strncmp(azArg[0], "quit", n)==0 ){
10215 rc = 2;
10216 }else
10217 #endif
10219 #ifndef SQLITE_SHELL_FIDDLE
10220 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "read", n)==0 ){
10221 FILE *inSaved = p->in;
10222 int savedLineno = p->lineno;
10223 failIfSafeMode(p, "cannot run .read in safe mode");
10224 if( nArg!=2 ){
10225 eputz("Usage: .read FILE\n");
10226 rc = 1;
10227 goto meta_command_exit;
10229 if( azArg[1][0]=='|' ){
10230 #ifdef SQLITE_OMIT_POPEN
10231 eputz("Error: pipes are not supported in this OS\n");
10232 rc = 1;
10233 #else
10234 p->in = sqlite3_popen(azArg[1]+1, "r");
10235 if( p->in==0 ){
10236 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10237 rc = 1;
10238 }else{
10239 rc = process_input(p);
10240 pclose(p->in);
10242 #endif
10243 }else if( (p->in = openChrSource(azArg[1]))==0 ){
10244 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
10245 rc = 1;
10246 }else{
10247 rc = process_input(p);
10248 fclose(p->in);
10250 p->in = inSaved;
10251 p->lineno = savedLineno;
10252 }else
10253 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10255 #ifndef SQLITE_SHELL_FIDDLE
10256 if( c=='r' && n>=3 && cli_strncmp(azArg[0], "restore", n)==0 ){
10257 const char *zSrcFile;
10258 const char *zDb;
10259 sqlite3 *pSrc;
10260 sqlite3_backup *pBackup;
10261 int nTimeout = 0;
10263 failIfSafeMode(p, "cannot run .restore in safe mode");
10264 if( nArg==2 ){
10265 zSrcFile = azArg[1];
10266 zDb = "main";
10267 }else if( nArg==3 ){
10268 zSrcFile = azArg[2];
10269 zDb = azArg[1];
10270 }else{
10271 eputz("Usage: .restore ?DB? FILE\n");
10272 rc = 1;
10273 goto meta_command_exit;
10275 rc = sqlite3_open(zSrcFile, &pSrc);
10276 if( rc!=SQLITE_OK ){
10277 sqlite3_fprintf(stderr,"Error: cannot open \"%s\"\n", zSrcFile);
10278 close_db(pSrc);
10279 return 1;
10281 open_db(p, 0);
10282 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
10283 if( pBackup==0 ){
10284 shellDatabaseError(p->db);
10285 close_db(pSrc);
10286 return 1;
10288 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
10289 || rc==SQLITE_BUSY ){
10290 if( rc==SQLITE_BUSY ){
10291 if( nTimeout++ >= 3 ) break;
10292 sqlite3_sleep(100);
10295 sqlite3_backup_finish(pBackup);
10296 if( rc==SQLITE_DONE ){
10297 rc = 0;
10298 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
10299 eputz("Error: source database is busy\n");
10300 rc = 1;
10301 }else{
10302 shellDatabaseError(p->db);
10303 rc = 1;
10305 close_db(pSrc);
10306 }else
10307 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10309 if( c=='s' && cli_strncmp(azArg[0], "scanstats", n)==0 ){
10310 if( nArg==2 ){
10311 if( cli_strcmp(azArg[1], "vm")==0 ){
10312 p->scanstatsOn = 3;
10313 }else
10314 if( cli_strcmp(azArg[1], "est")==0 ){
10315 p->scanstatsOn = 2;
10316 }else{
10317 p->scanstatsOn = (u8)booleanValue(azArg[1]);
10319 open_db(p, 0);
10320 sqlite3_db_config(
10321 p->db, SQLITE_DBCONFIG_STMT_SCANSTATUS, p->scanstatsOn, (int*)0
10323 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
10324 eputz("Warning: .scanstats not available in this build.\n");
10325 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
10326 if( p->scanstatsOn==3 ){
10327 eputz("Warning: \".scanstats vm\" not available in this build.\n");
10329 #endif
10330 }else{
10331 eputz("Usage: .scanstats on|off|est\n");
10332 rc = 1;
10334 }else
10336 if( c=='s' && cli_strncmp(azArg[0], "schema", n)==0 ){
10337 ShellText sSelect;
10338 ShellState data;
10339 char *zErrMsg = 0;
10340 const char *zDiv = "(";
10341 const char *zName = 0;
10342 int iSchema = 0;
10343 int bDebug = 0;
10344 int bNoSystemTabs = 0;
10345 int ii;
10347 open_db(p, 0);
10348 memcpy(&data, p, sizeof(data));
10349 data.showHeader = 0;
10350 data.cMode = data.mode = MODE_Semi;
10351 initText(&sSelect);
10352 for(ii=1; ii<nArg; ii++){
10353 if( optionMatch(azArg[ii],"indent") ){
10354 data.cMode = data.mode = MODE_Pretty;
10355 }else if( optionMatch(azArg[ii],"debug") ){
10356 bDebug = 1;
10357 }else if( optionMatch(azArg[ii],"nosys") ){
10358 bNoSystemTabs = 1;
10359 }else if( azArg[ii][0]=='-' ){
10360 sqlite3_fprintf(stderr,"Unknown option: \"%s\"\n", azArg[ii]);
10361 rc = 1;
10362 goto meta_command_exit;
10363 }else if( zName==0 ){
10364 zName = azArg[ii];
10365 }else{
10366 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10367 rc = 1;
10368 goto meta_command_exit;
10371 if( zName!=0 ){
10372 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
10373 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
10374 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
10375 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
10376 if( isSchema ){
10377 char *new_argv[2], *new_colv[2];
10378 new_argv[0] = sqlite3_mprintf(
10379 "CREATE TABLE %s (\n"
10380 " type text,\n"
10381 " name text,\n"
10382 " tbl_name text,\n"
10383 " rootpage integer,\n"
10384 " sql text\n"
10385 ")", zName);
10386 shell_check_oom(new_argv[0]);
10387 new_argv[1] = 0;
10388 new_colv[0] = "sql";
10389 new_colv[1] = 0;
10390 callback(&data, 1, new_argv, new_colv);
10391 sqlite3_free(new_argv[0]);
10394 if( zDiv ){
10395 sqlite3_stmt *pStmt = 0;
10396 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
10397 -1, &pStmt, 0);
10398 if( rc ){
10399 shellDatabaseError(p->db);
10400 sqlite3_finalize(pStmt);
10401 rc = 1;
10402 goto meta_command_exit;
10404 appendText(&sSelect, "SELECT sql FROM", 0);
10405 iSchema = 0;
10406 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10407 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
10408 char zScNum[30];
10409 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
10410 appendText(&sSelect, zDiv, 0);
10411 zDiv = " UNION ALL ";
10412 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
10413 if( sqlite3_stricmp(zDb, "main")!=0 ){
10414 appendText(&sSelect, zDb, '\'');
10415 }else{
10416 appendText(&sSelect, "NULL", 0);
10418 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10419 appendText(&sSelect, zScNum, 0);
10420 appendText(&sSelect, " AS snum, ", 0);
10421 appendText(&sSelect, zDb, '\'');
10422 appendText(&sSelect, " AS sname FROM ", 0);
10423 appendText(&sSelect, zDb, quoteChar(zDb));
10424 appendText(&sSelect, ".sqlite_schema", 0);
10426 sqlite3_finalize(pStmt);
10427 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10428 if( zName ){
10429 appendText(&sSelect,
10430 " UNION ALL SELECT shell_module_schema(name),"
10431 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10434 #endif
10435 appendText(&sSelect, ") WHERE ", 0);
10436 if( zName ){
10437 char *zQarg = sqlite3_mprintf("%Q", zName);
10438 int bGlob;
10439 shell_check_oom(zQarg);
10440 bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
10441 strchr(zName, '[') != 0;
10442 if( strchr(zName, '.') ){
10443 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
10444 }else{
10445 appendText(&sSelect, "lower(tbl_name)", 0);
10447 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
10448 appendText(&sSelect, zQarg, 0);
10449 if( !bGlob ){
10450 appendText(&sSelect, " ESCAPE '\\' ", 0);
10452 appendText(&sSelect, " AND ", 0);
10453 sqlite3_free(zQarg);
10455 if( bNoSystemTabs ){
10456 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
10458 appendText(&sSelect, "sql IS NOT NULL"
10459 " ORDER BY snum, rowid", 0);
10460 if( bDebug ){
10461 sqlite3_fprintf(p->out, "SQL: %s;\n", sSelect.z);
10462 }else{
10463 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
10465 freeText(&sSelect);
10467 if( zErrMsg ){
10468 shellEmitError(zErrMsg);
10469 sqlite3_free(zErrMsg);
10470 rc = 1;
10471 }else if( rc != SQLITE_OK ){
10472 eputz("Error: querying schema information\n");
10473 rc = 1;
10474 }else{
10475 rc = 0;
10477 }else
10479 if( (c=='s' && n==11 && cli_strncmp(azArg[0], "selecttrace", n)==0)
10480 || (c=='t' && n==9 && cli_strncmp(azArg[0], "treetrace", n)==0)
10482 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
10483 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
10484 }else
10486 #if defined(SQLITE_ENABLE_SESSION)
10487 if( c=='s' && cli_strncmp(azArg[0],"session",n)==0 && n>=3 ){
10488 struct AuxDb *pAuxDb = p->pAuxDb;
10489 OpenSession *pSession = &pAuxDb->aSession[0];
10490 char **azCmd = &azArg[1];
10491 int iSes = 0;
10492 int nCmd = nArg - 1;
10493 int i;
10494 if( nArg<=1 ) goto session_syntax_error;
10495 open_db(p, 0);
10496 if( nArg>=3 ){
10497 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
10498 if( cli_strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
10500 if( iSes<pAuxDb->nSession ){
10501 pSession = &pAuxDb->aSession[iSes];
10502 azCmd++;
10503 nCmd--;
10504 }else{
10505 pSession = &pAuxDb->aSession[0];
10506 iSes = 0;
10510 /* .session attach TABLE
10511 ** Invoke the sqlite3session_attach() interface to attach a particular
10512 ** table so that it is never filtered.
10514 if( cli_strcmp(azCmd[0],"attach")==0 ){
10515 if( nCmd!=2 ) goto session_syntax_error;
10516 if( pSession->p==0 ){
10517 session_not_open:
10518 eputz("ERROR: No sessions are open\n");
10519 }else{
10520 rc = sqlite3session_attach(pSession->p, azCmd[1]);
10521 if( rc ){
10522 sqlite3_fprintf(stderr,
10523 "ERROR: sqlite3session_attach() returns %d\n",rc);
10524 rc = 0;
10527 }else
10529 /* .session changeset FILE
10530 ** .session patchset FILE
10531 ** Write a changeset or patchset into a file. The file is overwritten.
10533 if( cli_strcmp(azCmd[0],"changeset")==0
10534 || cli_strcmp(azCmd[0],"patchset")==0
10536 FILE *out = 0;
10537 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
10538 if( nCmd!=2 ) goto session_syntax_error;
10539 if( pSession->p==0 ) goto session_not_open;
10540 out = sqlite3_fopen(azCmd[1], "wb");
10541 if( out==0 ){
10542 sqlite3_fprintf(stderr,"ERROR: cannot open \"%s\" for writing\n",
10543 azCmd[1]);
10544 }else{
10545 int szChng;
10546 void *pChng;
10547 if( azCmd[0][0]=='c' ){
10548 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
10549 }else{
10550 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
10552 if( rc ){
10553 sqlite3_fprintf(stdout, "Error: error code %d\n", rc);
10554 rc = 0;
10556 if( pChng
10557 && fwrite(pChng, szChng, 1, out)!=1 ){
10558 sqlite3_fprintf(stderr,
10559 "ERROR: Failed to write entire %d-byte output\n", szChng);
10561 sqlite3_free(pChng);
10562 fclose(out);
10564 }else
10566 /* .session close
10567 ** Close the identified session
10569 if( cli_strcmp(azCmd[0], "close")==0 ){
10570 if( nCmd!=1 ) goto session_syntax_error;
10571 if( pAuxDb->nSession ){
10572 session_close(pSession);
10573 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
10575 }else
10577 /* .session enable ?BOOLEAN?
10578 ** Query or set the enable flag
10580 if( cli_strcmp(azCmd[0], "enable")==0 ){
10581 int ii;
10582 if( nCmd>2 ) goto session_syntax_error;
10583 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10584 if( pAuxDb->nSession ){
10585 ii = sqlite3session_enable(pSession->p, ii);
10586 sqlite3_fprintf(p->out,
10587 "session %s enable flag = %d\n", pSession->zName, ii);
10589 }else
10591 /* .session filter GLOB ....
10592 ** Set a list of GLOB patterns of table names to be excluded.
10594 if( cli_strcmp(azCmd[0], "filter")==0 ){
10595 int ii, nByte;
10596 if( nCmd<2 ) goto session_syntax_error;
10597 if( pAuxDb->nSession ){
10598 for(ii=0; ii<pSession->nFilter; ii++){
10599 sqlite3_free(pSession->azFilter[ii]);
10601 sqlite3_free(pSession->azFilter);
10602 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
10603 pSession->azFilter = sqlite3_malloc( nByte );
10604 shell_check_oom( pSession->azFilter );
10605 for(ii=1; ii<nCmd; ii++){
10606 char *x = pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
10607 shell_check_oom(x);
10609 pSession->nFilter = ii-1;
10611 }else
10613 /* .session indirect ?BOOLEAN?
10614 ** Query or set the indirect flag
10616 if( cli_strcmp(azCmd[0], "indirect")==0 ){
10617 int ii;
10618 if( nCmd>2 ) goto session_syntax_error;
10619 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
10620 if( pAuxDb->nSession ){
10621 ii = sqlite3session_indirect(pSession->p, ii);
10622 sqlite3_fprintf(p->out,
10623 "session %s indirect flag = %d\n", pSession->zName, ii);
10625 }else
10627 /* .session isempty
10628 ** Determine if the session is empty
10630 if( cli_strcmp(azCmd[0], "isempty")==0 ){
10631 int ii;
10632 if( nCmd!=1 ) goto session_syntax_error;
10633 if( pAuxDb->nSession ){
10634 ii = sqlite3session_isempty(pSession->p);
10635 sqlite3_fprintf(p->out,
10636 "session %s isempty flag = %d\n", pSession->zName, ii);
10638 }else
10640 /* .session list
10641 ** List all currently open sessions
10643 if( cli_strcmp(azCmd[0],"list")==0 ){
10644 for(i=0; i<pAuxDb->nSession; i++){
10645 sqlite3_fprintf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
10647 }else
10649 /* .session open DB NAME
10650 ** Open a new session called NAME on the attached database DB.
10651 ** DB is normally "main".
10653 if( cli_strcmp(azCmd[0],"open")==0 ){
10654 char *zName;
10655 if( nCmd!=3 ) goto session_syntax_error;
10656 zName = azCmd[2];
10657 if( zName[0]==0 ) goto session_syntax_error;
10658 for(i=0; i<pAuxDb->nSession; i++){
10659 if( cli_strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
10660 sqlite3_fprintf(stderr,"Session \"%s\" already exists\n", zName);
10661 goto meta_command_exit;
10664 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
10665 sqlite3_fprintf(stderr,
10666 "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
10667 goto meta_command_exit;
10669 pSession = &pAuxDb->aSession[pAuxDb->nSession];
10670 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
10671 if( rc ){
10672 sqlite3_fprintf(stderr,"Cannot open session: error code=%d\n", rc);
10673 rc = 0;
10674 goto meta_command_exit;
10676 pSession->nFilter = 0;
10677 sqlite3session_table_filter(pSession->p, session_filter, pSession);
10678 pAuxDb->nSession++;
10679 pSession->zName = sqlite3_mprintf("%s", zName);
10680 shell_check_oom(pSession->zName);
10681 }else
10682 /* If no command name matches, show a syntax error */
10683 session_syntax_error:
10684 showHelp(p->out, "session");
10685 }else
10686 #endif
10688 #ifdef SQLITE_DEBUG
10689 /* Undocumented commands for internal testing. Subject to change
10690 ** without notice. */
10691 if( c=='s' && n>=10 && cli_strncmp(azArg[0], "selftest-", 9)==0 ){
10692 if( cli_strncmp(azArg[0]+9, "boolean", n-9)==0 ){
10693 int i, v;
10694 for(i=1; i<nArg; i++){
10695 v = booleanValue(azArg[i]);
10696 sqlite3_fprintf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
10699 if( cli_strncmp(azArg[0]+9, "integer", n-9)==0 ){
10700 int i; sqlite3_int64 v;
10701 for(i=1; i<nArg; i++){
10702 char zBuf[200];
10703 v = integerValue(azArg[i]);
10704 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
10705 sqlite3_fputs(zBuf, p->out);
10708 }else
10709 #endif
10711 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"selftest",n)==0 ){
10712 int bIsInit = 0; /* True to initialize the SELFTEST table */
10713 int bVerbose = 0; /* Verbose output */
10714 int bSelftestExists; /* True if SELFTEST already exists */
10715 int i, k; /* Loop counters */
10716 int nTest = 0; /* Number of tests runs */
10717 int nErr = 0; /* Number of errors seen */
10718 ShellText str; /* Answer for a query */
10719 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
10721 open_db(p,0);
10722 for(i=1; i<nArg; i++){
10723 const char *z = azArg[i];
10724 if( z[0]=='-' && z[1]=='-' ) z++;
10725 if( cli_strcmp(z,"-init")==0 ){
10726 bIsInit = 1;
10727 }else
10728 if( cli_strcmp(z,"-v")==0 ){
10729 bVerbose++;
10730 }else
10732 sqlite3_fprintf(stderr,
10733 "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
10734 sqlite3_fputs("Should be one of: --init -v\n", stderr);
10735 rc = 1;
10736 goto meta_command_exit;
10739 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
10740 != SQLITE_OK ){
10741 bSelftestExists = 0;
10742 }else{
10743 bSelftestExists = 1;
10745 if( bIsInit ){
10746 createSelftestTable(p);
10747 bSelftestExists = 1;
10749 initText(&str);
10750 appendText(&str, "x", 0);
10751 for(k=bSelftestExists; k>=0; k--){
10752 if( k==1 ){
10753 rc = sqlite3_prepare_v2(p->db,
10754 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10755 -1, &pStmt, 0);
10756 }else{
10757 rc = sqlite3_prepare_v2(p->db,
10758 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10759 " (1,'run','PRAGMA integrity_check','ok')",
10760 -1, &pStmt, 0);
10762 if( rc ){
10763 eputz("Error querying the selftest table\n");
10764 rc = 1;
10765 sqlite3_finalize(pStmt);
10766 goto meta_command_exit;
10768 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
10769 int tno = sqlite3_column_int(pStmt, 0);
10770 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
10771 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
10772 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
10774 if( zOp==0 ) continue;
10775 if( zSql==0 ) continue;
10776 if( zAns==0 ) continue;
10777 k = 0;
10778 if( bVerbose>0 ){
10779 sqlite3_fprintf(stdout, "%d: %s %s\n", tno, zOp, zSql);
10781 if( cli_strcmp(zOp,"memo")==0 ){
10782 sqlite3_fprintf(p->out, "%s\n", zSql);
10783 }else
10784 if( cli_strcmp(zOp,"run")==0 ){
10785 char *zErrMsg = 0;
10786 str.n = 0;
10787 str.z[0] = 0;
10788 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
10789 nTest++;
10790 if( bVerbose ){
10791 sqlite3_fprintf(p->out, "Result: %s\n", str.z);
10793 if( rc || zErrMsg ){
10794 nErr++;
10795 rc = 1;
10796 sqlite3_fprintf(p->out, "%d: error-code-%d: %s\n", tno, rc,zErrMsg);
10797 sqlite3_free(zErrMsg);
10798 }else if( cli_strcmp(zAns,str.z)!=0 ){
10799 nErr++;
10800 rc = 1;
10801 sqlite3_fprintf(p->out, "%d: Expected: [%s]\n", tno, zAns);
10802 sqlite3_fprintf(p->out, "%d: Got: [%s]\n", tno, str.z);
10805 else{
10806 sqlite3_fprintf(stderr,
10807 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
10808 rc = 1;
10809 break;
10811 } /* End loop over rows of content from SELFTEST */
10812 sqlite3_finalize(pStmt);
10813 } /* End loop over k */
10814 freeText(&str);
10815 sqlite3_fprintf(p->out, "%d errors out of %d tests\n", nErr, nTest);
10816 }else
10818 if( c=='s' && cli_strncmp(azArg[0], "separator", n)==0 ){
10819 if( nArg<2 || nArg>3 ){
10820 eputz("Usage: .separator COL ?ROW?\n");
10821 rc = 1;
10823 if( nArg>=2 ){
10824 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
10825 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
10827 if( nArg>=3 ){
10828 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
10829 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
10831 }else
10833 if( c=='s' && n>=4 && cli_strncmp(azArg[0],"sha3sum",n)==0 ){
10834 const char *zLike = 0; /* Which table to checksum. 0 means everything */
10835 int i; /* Loop counter */
10836 int bSchema = 0; /* Also hash the schema */
10837 int bSeparate = 0; /* Hash each table separately */
10838 int iSize = 224; /* Hash algorithm to use */
10839 int bDebug = 0; /* Only show the query that would have run */
10840 sqlite3_stmt *pStmt; /* For querying tables names */
10841 char *zSql; /* SQL to be run */
10842 char *zSep; /* Separator */
10843 ShellText sSql; /* Complete SQL for the query to run the hash */
10844 ShellText sQuery; /* Set of queries used to read all content */
10845 open_db(p, 0);
10846 for(i=1; i<nArg; i++){
10847 const char *z = azArg[i];
10848 if( z[0]=='-' ){
10849 z++;
10850 if( z[0]=='-' ) z++;
10851 if( cli_strcmp(z,"schema")==0 ){
10852 bSchema = 1;
10853 }else
10854 if( cli_strcmp(z,"sha3-224")==0 || cli_strcmp(z,"sha3-256")==0
10855 || cli_strcmp(z,"sha3-384")==0 || cli_strcmp(z,"sha3-512")==0
10857 iSize = atoi(&z[5]);
10858 }else
10859 if( cli_strcmp(z,"debug")==0 ){
10860 bDebug = 1;
10861 }else
10863 sqlite3_fprintf(stderr,
10864 "Unknown option \"%s\" on \"%s\"\n", azArg[i], azArg[0]);
10865 showHelp(p->out, azArg[0]);
10866 rc = 1;
10867 goto meta_command_exit;
10869 }else if( zLike ){
10870 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10871 rc = 1;
10872 goto meta_command_exit;
10873 }else{
10874 zLike = z;
10875 bSeparate = 1;
10876 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
10879 if( bSchema ){
10880 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10881 " WHERE type='table' AND coalesce(rootpage,0)>1"
10882 " UNION ALL SELECT 'sqlite_schema'"
10883 " ORDER BY 1 collate nocase";
10884 }else{
10885 zSql = "SELECT lower(name) as tname FROM sqlite_schema"
10886 " WHERE type='table' AND coalesce(rootpage,0)>1"
10887 " AND name NOT LIKE 'sqlite_%'"
10888 " ORDER BY 1 collate nocase";
10890 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10891 initText(&sQuery);
10892 initText(&sSql);
10893 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
10894 zSep = "VALUES(";
10895 while( SQLITE_ROW==sqlite3_step(pStmt) ){
10896 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
10897 if( zTab==0 ) continue;
10898 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
10899 if( cli_strncmp(zTab, "sqlite_",7)!=0 ){
10900 appendText(&sQuery,"SELECT * FROM ", 0);
10901 appendText(&sQuery,zTab,'"');
10902 appendText(&sQuery," NOT INDEXED;", 0);
10903 }else if( cli_strcmp(zTab, "sqlite_schema")==0 ){
10904 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10905 " ORDER BY name;", 0);
10906 }else if( cli_strcmp(zTab, "sqlite_sequence")==0 ){
10907 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
10908 " ORDER BY name;", 0);
10909 }else if( cli_strcmp(zTab, "sqlite_stat1")==0 ){
10910 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
10911 " ORDER BY tbl,idx;", 0);
10912 }else if( cli_strcmp(zTab, "sqlite_stat4")==0 ){
10913 appendText(&sQuery, "SELECT * FROM ", 0);
10914 appendText(&sQuery, zTab, 0);
10915 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
10917 appendText(&sSql, zSep, 0);
10918 appendText(&sSql, sQuery.z, '\'');
10919 sQuery.n = 0;
10920 appendText(&sSql, ",", 0);
10921 appendText(&sSql, zTab, '\'');
10922 zSep = "),(";
10924 sqlite3_finalize(pStmt);
10925 if( bSeparate ){
10926 zSql = sqlite3_mprintf(
10927 "%s))"
10928 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10929 " FROM [sha3sum$query]",
10930 sSql.z, iSize);
10931 }else{
10932 zSql = sqlite3_mprintf(
10933 "%s))"
10934 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10935 " FROM [sha3sum$query]",
10936 sSql.z, iSize);
10938 shell_check_oom(zSql);
10939 freeText(&sQuery);
10940 freeText(&sSql);
10941 if( bDebug ){
10942 sqlite3_fprintf(p->out, "%s\n", zSql);
10943 }else{
10944 shell_exec(p, zSql, 0);
10946 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10948 int lrc;
10949 char *zRevText = /* Query for reversible to-blob-to-text check */
10950 "SELECT lower(name) as tname FROM sqlite_schema\n"
10951 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
10952 "AND name NOT LIKE 'sqlite_%%'%s\n"
10953 "ORDER BY 1 collate nocase";
10954 zRevText = sqlite3_mprintf(zRevText, zLike? " AND name LIKE $tspec" : "");
10955 zRevText = sqlite3_mprintf(
10956 /* lower-case query is first run, producing upper-case query. */
10957 "with tabcols as materialized(\n"
10958 "select tname, cname\n"
10959 "from ("
10960 " select printf('\"%%w\"',ss.tname) as tname,"
10961 " printf('\"%%w\"',ti.name) as cname\n"
10962 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
10963 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
10964 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
10965 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
10966 "FROM '||tname||' WHERE '\n"
10967 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
10968 "|| ' AND typeof('||cname||')=''text'' ',\n"
10969 "' OR ') as query, tname from tabcols group by tname)"
10970 , zRevText);
10971 shell_check_oom(zRevText);
10972 if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zRevText);
10973 lrc = sqlite3_prepare_v2(p->db, zRevText, -1, &pStmt, 0);
10974 if( lrc!=SQLITE_OK ){
10975 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
10976 ** user does cruel and unnatural things like ".limit expr_depth 0". */
10977 rc = 1;
10978 }else{
10979 if( zLike ) sqlite3_bind_text(pStmt,1,zLike,-1,SQLITE_STATIC);
10980 lrc = SQLITE_ROW==sqlite3_step(pStmt);
10981 if( lrc ){
10982 const char *zGenQuery = (char*)sqlite3_column_text(pStmt,0);
10983 sqlite3_stmt *pCheckStmt;
10984 lrc = sqlite3_prepare_v2(p->db, zGenQuery, -1, &pCheckStmt, 0);
10985 if( bDebug ) sqlite3_fprintf(p->out, "%s\n", zGenQuery);
10986 if( lrc!=SQLITE_OK ){
10987 rc = 1;
10988 }else{
10989 if( SQLITE_ROW==sqlite3_step(pCheckStmt) ){
10990 double countIrreversible = sqlite3_column_double(pCheckStmt, 0);
10991 if( countIrreversible>0 ){
10992 int sz = (int)(countIrreversible + 0.5);
10993 sqlite3_fprintf(stderr,
10994 "Digest includes %d invalidly encoded text field%s.\n",
10995 sz, (sz>1)? "s": "");
10998 sqlite3_finalize(pCheckStmt);
11000 sqlite3_finalize(pStmt);
11003 if( rc ) eputz(".sha3sum failed.\n");
11004 sqlite3_free(zRevText);
11006 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
11007 sqlite3_free(zSql);
11008 }else
11010 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
11011 if( c=='s'
11012 && (cli_strncmp(azArg[0], "shell", n)==0
11013 || cli_strncmp(azArg[0],"system",n)==0)
11015 char *zCmd;
11016 int i, x;
11017 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
11018 if( nArg<2 ){
11019 eputz("Usage: .system COMMAND\n");
11020 rc = 1;
11021 goto meta_command_exit;
11023 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
11024 for(i=2; i<nArg && zCmd!=0; i++){
11025 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
11026 zCmd, azArg[i]);
11028 /*consoleRestore();*/
11029 x = zCmd!=0 ? system(zCmd) : 1;
11030 /*consoleRenewSetup();*/
11031 sqlite3_free(zCmd);
11032 if( x ) sqlite3_fprintf(stderr,"System command returns %d\n", x);
11033 }else
11034 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
11036 if( c=='s' && cli_strncmp(azArg[0], "show", n)==0 ){
11037 static const char *azBool[] = { "off", "on", "trigger", "full"};
11038 const char *zOut;
11039 int i;
11040 if( nArg!=1 ){
11041 eputz("Usage: .show\n");
11042 rc = 1;
11043 goto meta_command_exit;
11045 sqlite3_fprintf(p->out, "%12.12s: %s\n","echo",
11046 azBool[ShellHasFlag(p, SHFLG_Echo)]);
11047 sqlite3_fprintf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
11048 sqlite3_fprintf(p->out, "%12.12s: %s\n","explain",
11049 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
11050 sqlite3_fprintf(p->out, "%12.12s: %s\n","headers",
11051 azBool[p->showHeader!=0]);
11052 if( p->mode==MODE_Column
11053 || (p->mode>=MODE_Markdown && p->mode<=MODE_Box)
11055 sqlite3_fprintf(p->out,
11056 "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
11057 modeDescr[p->mode], p->cmOpts.iWrap,
11058 p->cmOpts.bWordWrap ? "on" : "off",
11059 p->cmOpts.bQuote ? "" : "no");
11060 }else{
11061 sqlite3_fprintf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
11063 sqlite3_fprintf(p->out, "%12.12s: ", "nullvalue");
11064 output_c_string(p->out, p->nullValue);
11065 sqlite3_fputs("\n", p->out);
11066 sqlite3_fprintf(p->out, "%12.12s: %s\n","output",
11067 strlen30(p->outfile) ? p->outfile : "stdout");
11068 sqlite3_fprintf(p->out, "%12.12s: ", "colseparator");
11069 output_c_string(p->out, p->colSeparator);
11070 sqlite3_fputs("\n", p->out);
11071 sqlite3_fprintf(p->out, "%12.12s: ", "rowseparator");
11072 output_c_string(p->out, p->rowSeparator);
11073 sqlite3_fputs("\n", p->out);
11074 switch( p->statsOn ){
11075 case 0: zOut = "off"; break;
11076 default: zOut = "on"; break;
11077 case 2: zOut = "stmt"; break;
11078 case 3: zOut = "vmstep"; break;
11080 sqlite3_fprintf(p->out, "%12.12s: %s\n","stats", zOut);
11081 sqlite3_fprintf(p->out, "%12.12s: ", "width");
11082 for (i=0;i<p->nWidth;i++) {
11083 sqlite3_fprintf(p->out, "%d ", p->colWidth[i]);
11085 sqlite3_fputs("\n", p->out);
11086 sqlite3_fprintf(p->out, "%12.12s: %s\n", "filename",
11087 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
11088 }else
11090 if( c=='s' && cli_strncmp(azArg[0], "stats", n)==0 ){
11091 if( nArg==2 ){
11092 if( cli_strcmp(azArg[1],"stmt")==0 ){
11093 p->statsOn = 2;
11094 }else if( cli_strcmp(azArg[1],"vmstep")==0 ){
11095 p->statsOn = 3;
11096 }else{
11097 p->statsOn = (u8)booleanValue(azArg[1]);
11099 }else if( nArg==1 ){
11100 display_stats(p->db, p, 0);
11101 }else{
11102 eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
11103 rc = 1;
11105 }else
11107 if( (c=='t' && n>1 && cli_strncmp(azArg[0], "tables", n)==0)
11108 || (c=='i' && (cli_strncmp(azArg[0], "indices", n)==0
11109 || cli_strncmp(azArg[0], "indexes", n)==0) )
11111 sqlite3_stmt *pStmt;
11112 char **azResult;
11113 int nRow, nAlloc;
11114 int ii;
11115 ShellText s;
11116 initText(&s);
11117 open_db(p, 0);
11118 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
11119 if( rc ){
11120 sqlite3_finalize(pStmt);
11121 return shellDatabaseError(p->db);
11124 if( nArg>2 && c=='i' ){
11125 /* It is an historical accident that the .indexes command shows an error
11126 ** when called with the wrong number of arguments whereas the .tables
11127 ** command does not. */
11128 eputz("Usage: .indexes ?LIKE-PATTERN?\n");
11129 rc = 1;
11130 sqlite3_finalize(pStmt);
11131 goto meta_command_exit;
11133 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
11134 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
11135 if( zDbName==0 ) continue;
11136 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
11137 if( sqlite3_stricmp(zDbName, "main")==0 ){
11138 appendText(&s, "SELECT name FROM ", 0);
11139 }else{
11140 appendText(&s, "SELECT ", 0);
11141 appendText(&s, zDbName, '\'');
11142 appendText(&s, "||'.'||name FROM ", 0);
11144 appendText(&s, zDbName, '"');
11145 appendText(&s, ".sqlite_schema ", 0);
11146 if( c=='t' ){
11147 appendText(&s," WHERE type IN ('table','view')"
11148 " AND name NOT LIKE 'sqlite_%'"
11149 " AND name LIKE ?1", 0);
11150 }else{
11151 appendText(&s," WHERE type='index'"
11152 " AND tbl_name LIKE ?1", 0);
11155 rc = sqlite3_finalize(pStmt);
11156 if( rc==SQLITE_OK ){
11157 appendText(&s, " ORDER BY 1", 0);
11158 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
11160 freeText(&s);
11161 if( rc ) return shellDatabaseError(p->db);
11163 /* Run the SQL statement prepared by the above block. Store the results
11164 ** as an array of nul-terminated strings in azResult[]. */
11165 nRow = nAlloc = 0;
11166 azResult = 0;
11167 if( nArg>1 ){
11168 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
11169 }else{
11170 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
11172 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11173 if( nRow>=nAlloc ){
11174 char **azNew;
11175 int n2 = nAlloc*2 + 10;
11176 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
11177 shell_check_oom(azNew);
11178 nAlloc = n2;
11179 azResult = azNew;
11181 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
11182 shell_check_oom(azResult[nRow]);
11183 nRow++;
11185 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
11186 rc = shellDatabaseError(p->db);
11189 /* Pretty-print the contents of array azResult[] to the output */
11190 if( rc==0 && nRow>0 ){
11191 int len, maxlen = 0;
11192 int i, j;
11193 int nPrintCol, nPrintRow;
11194 for(i=0; i<nRow; i++){
11195 len = strlen30(azResult[i]);
11196 if( len>maxlen ) maxlen = len;
11198 nPrintCol = 80/(maxlen+2);
11199 if( nPrintCol<1 ) nPrintCol = 1;
11200 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
11201 for(i=0; i<nPrintRow; i++){
11202 for(j=i; j<nRow; j+=nPrintRow){
11203 char *zSp = j<nPrintRow ? "" : " ";
11204 sqlite3_fprintf(p->out,
11205 "%s%-*s", zSp, maxlen, azResult[j] ? azResult[j]:"");
11207 sqlite3_fputs("\n", p->out);
11211 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
11212 sqlite3_free(azResult);
11213 }else
11215 #ifndef SQLITE_SHELL_FIDDLE
11216 /* Begin redirecting output to the file "testcase-out.txt" */
11217 if( c=='t' && cli_strcmp(azArg[0],"testcase")==0 ){
11218 output_reset(p);
11219 p->out = output_file_open("testcase-out.txt");
11220 if( p->out==0 ){
11221 eputz("Error: cannot open 'testcase-out.txt'\n");
11223 if( nArg>=2 ){
11224 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
11225 }else{
11226 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
11228 }else
11229 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
11231 #ifndef SQLITE_UNTESTABLE
11232 if( c=='t' && n>=8 && cli_strncmp(azArg[0], "testctrl", n)==0 ){
11233 static const struct {
11234 const char *zCtrlName; /* Name of a test-control option */
11235 int ctrlCode; /* Integer code for that option */
11236 int unSafe; /* Not valid unless --unsafe-testing */
11237 const char *zUsage; /* Usage notes */
11238 } aCtrl[] = {
11239 {"always", SQLITE_TESTCTRL_ALWAYS, 1, "BOOLEAN" },
11240 {"assert", SQLITE_TESTCTRL_ASSERT, 1, "BOOLEAN" },
11241 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
11242 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
11243 {"byteorder", SQLITE_TESTCTRL_BYTEORDER, 0, "" },
11244 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,0,"BOOLEAN" },
11245 {"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"args..." },
11246 {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION, 0, "BOOLEAN" },
11247 {"imposter", SQLITE_TESTCTRL_IMPOSTER,1,"SCHEMA ON/OFF ROOTPAGE"},
11248 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS,0,"" },
11249 {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK ,0,"BOOLEAN" },
11250 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,0,"BOOLEAN" },
11251 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT,1, "BOOLEAN" },
11252 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS,0,"DISABLE-MASK ..."},
11253 #ifdef YYCOVERAGE
11254 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE,0,"" },
11255 #endif
11256 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE,1, "OFFSET " },
11257 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE,0, "" },
11258 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE, 0, "" },
11259 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED, 0, "SEED ?db?" },
11260 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT, 0, "" },
11261 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, 0, "NMAX" },
11262 {"tune", SQLITE_TESTCTRL_TUNE, 1, "ID VALUE" },
11264 int testctrl = -1;
11265 int iCtrl = -1;
11266 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
11267 int isOk = 0;
11268 int i, n2;
11269 const char *zCmd = 0;
11271 open_db(p, 0);
11272 zCmd = nArg>=2 ? azArg[1] : "help";
11274 /* The argument can optionally begin with "-" or "--" */
11275 if( zCmd[0]=='-' && zCmd[1] ){
11276 zCmd++;
11277 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
11280 /* --help lists all test-controls */
11281 if( cli_strcmp(zCmd,"help")==0 ){
11282 sqlite3_fputs("Available test-controls:\n", p->out);
11283 for(i=0; i<ArraySize(aCtrl); i++){
11284 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
11285 sqlite3_fprintf(p->out, " .testctrl %s %s\n",
11286 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
11288 rc = 1;
11289 goto meta_command_exit;
11292 /* convert testctrl text option to value. allow any unique prefix
11293 ** of the option name, or a numerical value. */
11294 n2 = strlen30(zCmd);
11295 for(i=0; i<ArraySize(aCtrl); i++){
11296 if( aCtrl[i].unSafe && !ShellHasFlag(p,SHFLG_TestingMode) ) continue;
11297 if( cli_strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
11298 if( testctrl<0 ){
11299 testctrl = aCtrl[i].ctrlCode;
11300 iCtrl = i;
11301 }else{
11302 sqlite3_fprintf(stderr,"Error: ambiguous test-control: \"%s\"\n"
11303 "Use \".testctrl --help\" for help\n", zCmd);
11304 rc = 1;
11305 goto meta_command_exit;
11309 if( testctrl<0 ){
11310 sqlite3_fprintf(stderr,"Error: unknown test-control: %s\n"
11311 "Use \".testctrl --help\" for help\n", zCmd);
11312 }else{
11313 switch(testctrl){
11315 /* Special processing for .testctrl opt MASK ...
11316 ** Each MASK argument can be one of:
11318 ** +LABEL Enable the named optimization
11320 ** -LABEL Disable the named optimization
11322 ** INTEGER Mask of optimizations to disable
11324 case SQLITE_TESTCTRL_OPTIMIZATIONS: {
11325 static const struct {
11326 unsigned int mask; /* Mask for this optimization */
11327 unsigned int bDsply; /* Display this on output */
11328 const char *zLabel; /* Name of optimization */
11329 } aLabel[] = {
11330 { 0x00000001, 1, "QueryFlattener" },
11331 { 0x00000001, 0, "Flatten" },
11332 { 0x00000002, 1, "WindowFunc" },
11333 { 0x00000004, 1, "GroupByOrder" },
11334 { 0x00000008, 1, "FactorOutConst" },
11335 { 0x00000010, 1, "DistinctOpt" },
11336 { 0x00000020, 1, "CoverIdxScan" },
11337 { 0x00000040, 1, "OrderByIdxJoin" },
11338 { 0x00000080, 1, "Transitive" },
11339 { 0x00000100, 1, "OmitNoopJoin" },
11340 { 0x00000200, 1, "CountOfView" },
11341 { 0x00000400, 1, "CurosrHints" },
11342 { 0x00000800, 1, "Stat4" },
11343 { 0x00001000, 1, "PushDown" },
11344 { 0x00002000, 1, "SimplifyJoin" },
11345 { 0x00004000, 1, "SkipScan" },
11346 { 0x00008000, 1, "PropagateConst" },
11347 { 0x00010000, 1, "MinMaxOpt" },
11348 { 0x00020000, 1, "SeekScan" },
11349 { 0x00040000, 1, "OmitOrderBy" },
11350 { 0x00080000, 1, "BloomFilter" },
11351 { 0x00100000, 1, "BloomPulldown" },
11352 { 0x00200000, 1, "BalancedMerge" },
11353 { 0x00400000, 1, "ReleaseReg" },
11354 { 0x00800000, 1, "FlttnUnionAll" },
11355 { 0x01000000, 1, "IndexedEXpr" },
11356 { 0x02000000, 1, "Coroutines" },
11357 { 0x04000000, 1, "NullUnusedCols" },
11358 { 0x08000000, 1, "OnePass" },
11359 { 0x10000000, 1, "OrderBySubq" },
11360 { 0xffffffff, 0, "All" },
11362 unsigned int curOpt;
11363 unsigned int newOpt;
11364 int ii;
11365 sqlite3_test_control(SQLITE_TESTCTRL_GETOPT, p->db, &curOpt);
11366 newOpt = curOpt;
11367 for(ii=2; ii<nArg; ii++){
11368 const char *z = azArg[ii];
11369 int useLabel = 0;
11370 const char *zLabel = 0;
11371 if( (z[0]=='+'|| z[0]=='-') && !IsDigit(z[1]) ){
11372 useLabel = z[0];
11373 zLabel = &z[1];
11374 }else if( !IsDigit(z[0]) && z[0]!=0 && !IsDigit(z[1]) ){
11375 useLabel = '+';
11376 zLabel = z;
11377 }else{
11378 newOpt = (unsigned int)strtol(z,0,0);
11380 if( useLabel ){
11381 int jj;
11382 for(jj=0; jj<ArraySize(aLabel); jj++){
11383 if( sqlite3_stricmp(zLabel, aLabel[jj].zLabel)==0 ) break;
11385 if( jj>=ArraySize(aLabel) ){
11386 sqlite3_fprintf(stderr,
11387 "Error: no such optimization: \"%s\"\n", zLabel);
11388 sqlite3_fputs("Should be one of:", stderr);
11389 for(jj=0; jj<ArraySize(aLabel); jj++){
11390 sqlite3_fprintf(stderr," %s", aLabel[jj].zLabel);
11392 sqlite3_fputs("\n", stderr);
11393 rc = 1;
11394 goto meta_command_exit;
11396 if( useLabel=='+' ){
11397 newOpt &= ~aLabel[jj].mask;
11398 }else{
11399 newOpt |= aLabel[jj].mask;
11403 if( curOpt!=newOpt ){
11404 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,p->db,newOpt);
11405 }else if( nArg<3 ){
11406 curOpt = ~newOpt;
11408 if( newOpt==0 ){
11409 sqlite3_fputs("+All\n", p->out);
11410 }else if( newOpt==0xffffffff ){
11411 sqlite3_fputs("-All\n", p->out);
11412 }else{
11413 int jj;
11414 for(jj=0; jj<ArraySize(aLabel); jj++){
11415 unsigned int m = aLabel[jj].mask;
11416 if( !aLabel[jj].bDsply ) continue;
11417 if( (curOpt&m)!=(newOpt&m) ){
11418 sqlite3_fprintf(p->out, "%c%s\n", (newOpt & m)==0 ? '+' : '-',
11419 aLabel[jj].zLabel);
11423 rc2 = isOk = 3;
11424 break;
11427 /* sqlite3_test_control(int, db, int) */
11428 case SQLITE_TESTCTRL_FK_NO_ACTION:
11429 if( nArg==3 ){
11430 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
11431 rc2 = sqlite3_test_control(testctrl, p->db, opt);
11432 isOk = 3;
11434 break;
11436 /* sqlite3_test_control(int) */
11437 case SQLITE_TESTCTRL_PRNG_SAVE:
11438 case SQLITE_TESTCTRL_PRNG_RESTORE:
11439 case SQLITE_TESTCTRL_BYTEORDER:
11440 if( nArg==2 ){
11441 rc2 = sqlite3_test_control(testctrl);
11442 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
11444 break;
11446 /* sqlite3_test_control(int, uint) */
11447 case SQLITE_TESTCTRL_PENDING_BYTE:
11448 if( nArg==3 ){
11449 unsigned int opt = (unsigned int)integerValue(azArg[2]);
11450 rc2 = sqlite3_test_control(testctrl, opt);
11451 isOk = 3;
11453 break;
11455 /* sqlite3_test_control(int, int, sqlite3*) */
11456 case SQLITE_TESTCTRL_PRNG_SEED:
11457 if( nArg==3 || nArg==4 ){
11458 int ii = (int)integerValue(azArg[2]);
11459 sqlite3 *db;
11460 if( ii==0 && cli_strcmp(azArg[2],"random")==0 ){
11461 sqlite3_randomness(sizeof(ii),&ii);
11462 sqlite3_fprintf(stdout, "-- random seed: %d\n", ii);
11464 if( nArg==3 ){
11465 db = 0;
11466 }else{
11467 db = p->db;
11468 /* Make sure the schema has been loaded */
11469 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
11471 rc2 = sqlite3_test_control(testctrl, ii, db);
11472 isOk = 3;
11474 break;
11476 /* sqlite3_test_control(int, int) */
11477 case SQLITE_TESTCTRL_ASSERT:
11478 case SQLITE_TESTCTRL_ALWAYS:
11479 if( nArg==3 ){
11480 int opt = booleanValue(azArg[2]);
11481 rc2 = sqlite3_test_control(testctrl, opt);
11482 isOk = 1;
11484 break;
11486 /* sqlite3_test_control(int, int) */
11487 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
11488 case SQLITE_TESTCTRL_NEVER_CORRUPT:
11489 if( nArg==3 ){
11490 int opt = booleanValue(azArg[2]);
11491 rc2 = sqlite3_test_control(testctrl, opt);
11492 isOk = 3;
11494 break;
11496 /* sqlite3_test_control(sqlite3*) */
11497 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
11498 rc2 = sqlite3_test_control(testctrl, p->db);
11499 isOk = 3;
11500 break;
11502 case SQLITE_TESTCTRL_IMPOSTER:
11503 if( nArg==5 ){
11504 rc2 = sqlite3_test_control(testctrl, p->db,
11505 azArg[2],
11506 integerValue(azArg[3]),
11507 integerValue(azArg[4]));
11508 isOk = 3;
11510 break;
11512 case SQLITE_TESTCTRL_SEEK_COUNT: {
11513 u64 x = 0;
11514 rc2 = sqlite3_test_control(testctrl, p->db, &x);
11515 sqlite3_fprintf(p->out, "%llu\n", x);
11516 isOk = 3;
11517 break;
11520 #ifdef YYCOVERAGE
11521 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
11522 if( nArg==2 ){
11523 sqlite3_test_control(testctrl, p->out);
11524 isOk = 3;
11526 break;
11528 #endif
11529 #ifdef SQLITE_DEBUG
11530 case SQLITE_TESTCTRL_TUNE: {
11531 if( nArg==4 ){
11532 int id = (int)integerValue(azArg[2]);
11533 int val = (int)integerValue(azArg[3]);
11534 sqlite3_test_control(testctrl, id, &val);
11535 isOk = 3;
11536 }else if( nArg==3 ){
11537 int id = (int)integerValue(azArg[2]);
11538 sqlite3_test_control(testctrl, -id, &rc2);
11539 isOk = 1;
11540 }else if( nArg==2 ){
11541 int id = 1;
11542 while(1){
11543 int val = 0;
11544 rc2 = sqlite3_test_control(testctrl, -id, &val);
11545 if( rc2!=SQLITE_OK ) break;
11546 if( id>1 ) sqlite3_fputs(" ", p->out);
11547 sqlite3_fprintf(p->out, "%d: %d", id, val);
11548 id++;
11550 if( id>1 ) sqlite3_fputs("\n", p->out);
11551 isOk = 3;
11553 break;
11555 #endif
11556 case SQLITE_TESTCTRL_SORTER_MMAP:
11557 if( nArg==3 ){
11558 int opt = (unsigned int)integerValue(azArg[2]);
11559 rc2 = sqlite3_test_control(testctrl, p->db, opt);
11560 isOk = 3;
11562 break;
11563 case SQLITE_TESTCTRL_JSON_SELFCHECK:
11564 if( nArg==2 ){
11565 rc2 = -1;
11566 isOk = 1;
11567 }else{
11568 rc2 = booleanValue(azArg[2]);
11569 isOk = 3;
11571 sqlite3_test_control(testctrl, &rc2);
11572 break;
11573 case SQLITE_TESTCTRL_FAULT_INSTALL: {
11574 int kk;
11575 int bShowHelp = nArg<=2;
11576 isOk = 3;
11577 for(kk=2; kk<nArg; kk++){
11578 const char *z = azArg[kk];
11579 if( z[0]=='-' && z[1]=='-' ) z++;
11580 if( cli_strcmp(z,"off")==0 ){
11581 sqlite3_test_control(testctrl, 0);
11582 }else if( cli_strcmp(z,"on")==0 ){
11583 faultsim_state.iCnt = faultsim_state.nSkip;
11584 if( faultsim_state.iErr==0 ) faultsim_state.iErr = 1;
11585 faultsim_state.nHit = 0;
11586 sqlite3_test_control(testctrl, faultsim_callback);
11587 }else if( cli_strcmp(z,"reset")==0 ){
11588 faultsim_state.iCnt = faultsim_state.nSkip;
11589 faultsim_state.nHit = 0;
11590 sqlite3_test_control(testctrl, faultsim_callback);
11591 }else if( cli_strcmp(z,"status")==0 ){
11592 sqlite3_fprintf(p->out, "faultsim.iId: %d\n",
11593 faultsim_state.iId);
11594 sqlite3_fprintf(p->out, "faultsim.iErr: %d\n",
11595 faultsim_state.iErr);
11596 sqlite3_fprintf(p->out, "faultsim.iCnt: %d\n",
11597 faultsim_state.iCnt);
11598 sqlite3_fprintf(p->out, "faultsim.nHit: %d\n",
11599 faultsim_state.nHit);
11600 sqlite3_fprintf(p->out, "faultsim.iInterval: %d\n",
11601 faultsim_state.iInterval);
11602 sqlite3_fprintf(p->out, "faultsim.eVerbose: %d\n",
11603 faultsim_state.eVerbose);
11604 sqlite3_fprintf(p->out, "faultsim.nRepeat: %d\n",
11605 faultsim_state.nRepeat);
11606 sqlite3_fprintf(p->out, "faultsim.nSkip: %d\n",
11607 faultsim_state.nSkip);
11608 }else if( cli_strcmp(z,"-v")==0 ){
11609 if( faultsim_state.eVerbose<2 ) faultsim_state.eVerbose++;
11610 }else if( cli_strcmp(z,"-q")==0 ){
11611 if( faultsim_state.eVerbose>0 ) faultsim_state.eVerbose--;
11612 }else if( cli_strcmp(z,"-id")==0 && kk+1<nArg ){
11613 faultsim_state.iId = atoi(azArg[++kk]);
11614 }else if( cli_strcmp(z,"-errcode")==0 && kk+1<nArg ){
11615 faultsim_state.iErr = atoi(azArg[++kk]);
11616 }else if( cli_strcmp(z,"-interval")==0 && kk+1<nArg ){
11617 faultsim_state.iInterval = atoi(azArg[++kk]);
11618 }else if( cli_strcmp(z,"-repeat")==0 && kk+1<nArg ){
11619 faultsim_state.nRepeat = atoi(azArg[++kk]);
11620 }else if( cli_strcmp(z,"-skip")==0 && kk+1<nArg ){
11621 faultsim_state.nSkip = atoi(azArg[++kk]);
11622 }else if( cli_strcmp(z,"-?")==0 || sqlite3_strglob("*help*",z)==0){
11623 bShowHelp = 1;
11624 }else{
11625 sqlite3_fprintf(stderr,
11626 "Unrecognized fault_install argument: \"%s\"\n",
11627 azArg[kk]);
11628 rc = 1;
11629 bShowHelp = 1;
11630 break;
11633 if( bShowHelp ){
11634 sqlite3_fputs(
11635 "Usage: .testctrl fault_install ARGS\n"
11636 "Possible arguments:\n"
11637 " off Disable faultsim\n"
11638 " on Activate faultsim\n"
11639 " reset Reset the trigger counter\n"
11640 " status Show current status\n"
11641 " -v Increase verbosity\n"
11642 " -q Decrease verbosity\n"
11643 " --errcode N When triggered, return N as error code\n"
11644 " --id ID Trigger only for the ID specified\n"
11645 " --interval N Trigger only after every N-th call\n"
11646 " --repeat N Turn off after N hits. 0 means never\n"
11647 " --skip N Skip the first N encounters\n"
11648 ,p->out
11651 break;
11655 if( isOk==0 && iCtrl>=0 ){
11656 sqlite3_fprintf(p->out,
11657 "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
11658 rc = 1;
11659 }else if( isOk==1 ){
11660 sqlite3_fprintf(p->out, "%d\n", rc2);
11661 }else if( isOk==2 ){
11662 sqlite3_fprintf(p->out, "0x%08x\n", rc2);
11664 }else
11665 #endif /* !defined(SQLITE_UNTESTABLE) */
11667 if( c=='t' && n>4 && cli_strncmp(azArg[0], "timeout", n)==0 ){
11668 open_db(p, 0);
11669 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
11670 }else
11672 if( c=='t' && n>=5 && cli_strncmp(azArg[0], "timer", n)==0 ){
11673 if( nArg==2 ){
11674 enableTimer = booleanValue(azArg[1]);
11675 if( enableTimer && !HAS_TIMER ){
11676 eputz("Error: timer not available on this system.\n");
11677 enableTimer = 0;
11679 }else{
11680 eputz("Usage: .timer on|off\n");
11681 rc = 1;
11683 }else
11685 #ifndef SQLITE_OMIT_TRACE
11686 if( c=='t' && cli_strncmp(azArg[0], "trace", n)==0 ){
11687 int mType = 0;
11688 int jj;
11689 open_db(p, 0);
11690 for(jj=1; jj<nArg; jj++){
11691 const char *z = azArg[jj];
11692 if( z[0]=='-' ){
11693 if( optionMatch(z, "expanded") ){
11694 p->eTraceType = SHELL_TRACE_EXPANDED;
11696 #ifdef SQLITE_ENABLE_NORMALIZE
11697 else if( optionMatch(z, "normalized") ){
11698 p->eTraceType = SHELL_TRACE_NORMALIZED;
11700 #endif
11701 else if( optionMatch(z, "plain") ){
11702 p->eTraceType = SHELL_TRACE_PLAIN;
11704 else if( optionMatch(z, "profile") ){
11705 mType |= SQLITE_TRACE_PROFILE;
11707 else if( optionMatch(z, "row") ){
11708 mType |= SQLITE_TRACE_ROW;
11710 else if( optionMatch(z, "stmt") ){
11711 mType |= SQLITE_TRACE_STMT;
11713 else if( optionMatch(z, "close") ){
11714 mType |= SQLITE_TRACE_CLOSE;
11716 else {
11717 sqlite3_fprintf(stderr,"Unknown option \"%s\" on \".trace\"\n", z);
11718 rc = 1;
11719 goto meta_command_exit;
11721 }else{
11722 output_file_close(p->traceOut);
11723 p->traceOut = output_file_open(z);
11726 if( p->traceOut==0 ){
11727 sqlite3_trace_v2(p->db, 0, 0, 0);
11728 }else{
11729 if( mType==0 ) mType = SQLITE_TRACE_STMT;
11730 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
11732 }else
11733 #endif /* !defined(SQLITE_OMIT_TRACE) */
11735 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11736 if( c=='u' && cli_strncmp(azArg[0], "unmodule", n)==0 ){
11737 int ii;
11738 int lenOpt;
11739 char *zOpt;
11740 if( nArg<2 ){
11741 eputz("Usage: .unmodule [--allexcept] NAME ...\n");
11742 rc = 1;
11743 goto meta_command_exit;
11745 open_db(p, 0);
11746 zOpt = azArg[1];
11747 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
11748 lenOpt = (int)strlen(zOpt);
11749 if( lenOpt>=3 && cli_strncmp(zOpt, "-allexcept",lenOpt)==0 ){
11750 assert( azArg[nArg]==0 );
11751 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
11752 }else{
11753 for(ii=1; ii<nArg; ii++){
11754 sqlite3_create_module(p->db, azArg[ii], 0, 0);
11757 }else
11758 #endif
11760 #if SQLITE_USER_AUTHENTICATION
11761 if( c=='u' && cli_strncmp(azArg[0], "user", n)==0 ){
11762 if( nArg<2 ){
11763 eputz("Usage: .user SUBCOMMAND ...\n");
11764 rc = 1;
11765 goto meta_command_exit;
11767 open_db(p, 0);
11768 if( cli_strcmp(azArg[1],"login")==0 ){
11769 if( nArg!=4 ){
11770 eputz("Usage: .user login USER PASSWORD\n");
11771 rc = 1;
11772 goto meta_command_exit;
11774 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
11775 strlen30(azArg[3]));
11776 if( rc ){
11777 sqlite3_fprintf(stderr,"Authentication failed for user %s\n", azArg[2]);
11778 rc = 1;
11780 }else if( cli_strcmp(azArg[1],"add")==0 ){
11781 if( nArg!=5 ){
11782 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
11783 rc = 1;
11784 goto meta_command_exit;
11786 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11787 booleanValue(azArg[4]));
11788 if( rc ){
11789 sqlite3_fprintf(stderr,"User-Add failed: %d\n", rc);
11790 rc = 1;
11792 }else if( cli_strcmp(azArg[1],"edit")==0 ){
11793 if( nArg!=5 ){
11794 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
11795 rc = 1;
11796 goto meta_command_exit;
11798 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
11799 booleanValue(azArg[4]));
11800 if( rc ){
11801 sqlite3_fprintf(stderr,"User-Edit failed: %d\n", rc);
11802 rc = 1;
11804 }else if( cli_strcmp(azArg[1],"delete")==0 ){
11805 if( nArg!=3 ){
11806 eputz("Usage: .user delete USER\n");
11807 rc = 1;
11808 goto meta_command_exit;
11810 rc = sqlite3_user_delete(p->db, azArg[2]);
11811 if( rc ){
11812 sqlite3_fprintf(stderr,"User-Delete failed: %d\n", rc);
11813 rc = 1;
11815 }else{
11816 eputz("Usage: .user login|add|edit|delete ...\n");
11817 rc = 1;
11818 goto meta_command_exit;
11820 }else
11821 #endif /* SQLITE_USER_AUTHENTICATION */
11823 if( c=='v' && cli_strncmp(azArg[0], "version", n)==0 ){
11824 char *zPtrSz = sizeof(void*)==8 ? "64-bit" : "32-bit";
11825 sqlite3_fprintf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
11826 sqlite3_libversion(), sqlite3_sourceid());
11827 #if SQLITE_HAVE_ZLIB
11828 sqlite3_fprintf(p->out, "zlib version %s\n", zlibVersion());
11829 #endif
11830 #define CTIMEOPT_VAL_(opt) #opt
11831 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11832 #if defined(__clang__) && defined(__clang_major__)
11833 sqlite3_fprintf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
11834 CTIMEOPT_VAL(__clang_minor__) "."
11835 CTIMEOPT_VAL(__clang_patchlevel__) " (%s)\n", zPtrSz);
11836 #elif defined(_MSC_VER)
11837 sqlite3_fprintf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) " (%s)\n", zPtrSz);
11838 #elif defined(__GNUC__) && defined(__VERSION__)
11839 sqlite3_fprintf(p->out, "gcc-" __VERSION__ " (%s)\n", zPtrSz);
11840 #endif
11841 }else
11843 if( c=='v' && cli_strncmp(azArg[0], "vfsinfo", n)==0 ){
11844 const char *zDbName = nArg==2 ? azArg[1] : "main";
11845 sqlite3_vfs *pVfs = 0;
11846 if( p->db ){
11847 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
11848 if( pVfs ){
11849 sqlite3_fprintf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
11850 sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
11851 sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
11852 sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11855 }else
11857 if( c=='v' && cli_strncmp(azArg[0], "vfslist", n)==0 ){
11858 sqlite3_vfs *pVfs;
11859 sqlite3_vfs *pCurrent = 0;
11860 if( p->db ){
11861 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
11863 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
11864 sqlite3_fprintf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
11865 pVfs==pCurrent ? " <--- CURRENT" : "");
11866 sqlite3_fprintf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
11867 sqlite3_fprintf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
11868 sqlite3_fprintf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
11869 if( pVfs->pNext ){
11870 sqlite3_fputs("-----------------------------------\n", p->out);
11873 }else
11875 if( c=='v' && cli_strncmp(azArg[0], "vfsname", n)==0 ){
11876 const char *zDbName = nArg==2 ? azArg[1] : "main";
11877 char *zVfsName = 0;
11878 if( p->db ){
11879 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
11880 if( zVfsName ){
11881 sqlite3_fprintf(p->out, "%s\n", zVfsName);
11882 sqlite3_free(zVfsName);
11885 }else
11887 if( c=='w' && cli_strncmp(azArg[0], "wheretrace", n)==0 ){
11888 unsigned int x = nArg>=2? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
11889 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
11890 }else
11892 if( c=='w' && cli_strncmp(azArg[0], "width", n)==0 ){
11893 int j;
11894 assert( nArg<=ArraySize(azArg) );
11895 p->nWidth = nArg-1;
11896 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
11897 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
11898 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
11899 for(j=1; j<nArg; j++){
11900 p->colWidth[j-1] = (int)integerValue(azArg[j]);
11902 }else
11905 sqlite3_fprintf(stderr,"Error: unknown command or invalid arguments: "
11906 " \"%s\". Enter \".help\" for help\n", azArg[0]);
11907 rc = 1;
11910 meta_command_exit:
11911 if( p->outCount ){
11912 p->outCount--;
11913 if( p->outCount==0 ) output_reset(p);
11915 p->bSafeMode = p->bSafeModePersist;
11916 return rc;
11919 /* Line scan result and intermediate states (supporting scan resumption)
11921 #ifndef CHAR_BIT
11922 # define CHAR_BIT 8
11923 #endif
11924 typedef enum {
11925 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
11926 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
11927 QSS_Start = 0
11928 } QuickScanState;
11929 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11930 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11931 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11932 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11933 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11936 ** Scan line for classification to guide shell's handling.
11937 ** The scan is resumable for subsequent lines when prior
11938 ** return values are passed as the 2nd argument.
11940 static QuickScanState quickscan(char *zLine, QuickScanState qss,
11941 SCAN_TRACKER_REFTYPE pst){
11942 char cin;
11943 char cWait = (char)qss; /* intentional narrowing loss */
11944 if( cWait==0 ){
11945 PlainScan:
11946 assert( cWait==0 );
11947 while( (cin = *zLine++)!=0 ){
11948 if( IsSpace(cin) )
11949 continue;
11950 switch (cin){
11951 case '-':
11952 if( *zLine!='-' )
11953 break;
11954 while((cin = *++zLine)!=0 )
11955 if( cin=='\n')
11956 goto PlainScan;
11957 return qss;
11958 case ';':
11959 qss |= QSS_EndingSemi;
11960 continue;
11961 case '/':
11962 if( *zLine=='*' ){
11963 ++zLine;
11964 cWait = '*';
11965 CONTINUE_PROMPT_AWAITS(pst, "/*");
11966 qss = QSS_SETV(qss, cWait);
11967 goto TermScan;
11969 break;
11970 case '[':
11971 cin = ']';
11972 deliberate_fall_through;
11973 case '`': case '\'': case '"':
11974 cWait = cin;
11975 qss = QSS_HasDark | cWait;
11976 CONTINUE_PROMPT_AWAITC(pst, cin);
11977 goto TermScan;
11978 case '(':
11979 CONTINUE_PAREN_INCR(pst, 1);
11980 break;
11981 case ')':
11982 CONTINUE_PAREN_INCR(pst, -1);
11983 break;
11984 default:
11985 break;
11987 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
11989 }else{
11990 TermScan:
11991 while( (cin = *zLine++)!=0 ){
11992 if( cin==cWait ){
11993 switch( cWait ){
11994 case '*':
11995 if( *zLine != '/' )
11996 continue;
11997 ++zLine;
11998 cWait = 0;
11999 CONTINUE_PROMPT_AWAITC(pst, 0);
12000 qss = QSS_SETV(qss, 0);
12001 goto PlainScan;
12002 case '`': case '\'': case '"':
12003 if(*zLine==cWait){
12004 /* Swallow doubled end-delimiter.*/
12005 ++zLine;
12006 continue;
12008 deliberate_fall_through;
12009 case ']':
12010 cWait = 0;
12011 CONTINUE_PROMPT_AWAITC(pst, 0);
12012 qss = QSS_SETV(qss, 0);
12013 goto PlainScan;
12014 default: assert(0);
12019 return qss;
12023 ** Return TRUE if the line typed in is an SQL command terminator other
12024 ** than a semi-colon. The SQL Server style "go" command is understood
12025 ** as is the Oracle "/".
12027 static int line_is_command_terminator(char *zLine){
12028 while( IsSpace(zLine[0]) ){ zLine++; };
12029 if( zLine[0]=='/' )
12030 zLine += 1; /* Oracle */
12031 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
12032 zLine += 2; /* SQL Server */
12033 else
12034 return 0;
12035 return quickscan(zLine, QSS_Start, 0)==QSS_Start;
12039 ** The CLI needs a working sqlite3_complete() to work properly. So error
12040 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
12042 #ifdef SQLITE_OMIT_COMPLETE
12043 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
12044 #endif
12047 ** Return true if zSql is a complete SQL statement. Return false if it
12048 ** ends in the middle of a string literal or C-style comment.
12050 static int line_is_complete(char *zSql, int nSql){
12051 int rc;
12052 if( zSql==0 ) return 1;
12053 zSql[nSql] = ';';
12054 zSql[nSql+1] = 0;
12055 rc = sqlite3_complete(zSql);
12056 zSql[nSql] = 0;
12057 return rc;
12061 ** This function is called after processing each line of SQL in the
12062 ** runOneSqlLine() function. Its purpose is to detect scenarios where
12063 ** defensive mode should be automatically turned off. Specifically, when
12065 ** 1. The first line of input is "PRAGMA foreign_keys=OFF;",
12066 ** 2. The second line of input is "BEGIN TRANSACTION;",
12067 ** 3. The database is empty, and
12068 ** 4. The shell is not running in --safe mode.
12070 ** The implementation uses the ShellState.eRestoreState to maintain state:
12072 ** 0: Have not seen any SQL.
12073 ** 1: Have seen "PRAGMA foreign_keys=OFF;".
12074 ** 2-6: Currently running .dump transaction. If the "2" bit is set,
12075 ** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
12076 ** 7: Nothing left to do. This function becomes a no-op.
12078 static int doAutoDetectRestore(ShellState *p, const char *zSql){
12079 int rc = SQLITE_OK;
12081 if( p->eRestoreState<7 ){
12082 switch( p->eRestoreState ){
12083 case 0: {
12084 const char *zExpect = "PRAGMA foreign_keys=OFF;";
12085 assert( strlen(zExpect)==24 );
12086 if( p->bSafeMode==0
12087 && strlen(zSql)>=24
12088 && memcmp(zSql, zExpect, 25)==0
12090 p->eRestoreState = 1;
12091 }else{
12092 p->eRestoreState = 7;
12094 break;
12097 case 1: {
12098 int bIsDump = 0;
12099 const char *zExpect = "BEGIN TRANSACTION;";
12100 assert( strlen(zExpect)==18 );
12101 if( memcmp(zSql, zExpect, 19)==0 ){
12102 /* Now check if the database is empty. */
12103 const char *zQuery = "SELECT 1 FROM sqlite_schema LIMIT 1";
12104 sqlite3_stmt *pStmt = 0;
12106 bIsDump = 1;
12107 shellPrepare(p->db, &rc, zQuery, &pStmt);
12108 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
12109 bIsDump = 0;
12111 shellFinalize(&rc, pStmt);
12113 if( bIsDump && rc==SQLITE_OK ){
12114 int bDefense = 0;
12115 int bDqsDdl = 0;
12116 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &bDefense);
12117 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, -1, &bDqsDdl);
12118 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
12119 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 1, 0);
12120 p->eRestoreState = (bDefense ? 2 : 0) + (bDqsDdl ? 4 : 0);
12121 }else{
12122 p->eRestoreState = 7;
12124 break;
12127 default: {
12128 if( sqlite3_get_autocommit(p->db) ){
12129 if( (p->eRestoreState & 2) ){
12130 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 1, 0);
12132 if( (p->eRestoreState & 4) ){
12133 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DQS_DDL, 0, 0);
12135 p->eRestoreState = 7;
12137 break;
12142 return rc;
12146 ** Run a single line of SQL. Return the number of errors.
12148 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
12149 int rc;
12150 char *zErrMsg = 0;
12152 open_db(p, 0);
12153 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
12154 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12155 BEGIN_TIMER;
12156 rc = shell_exec(p, zSql, &zErrMsg);
12157 END_TIMER(p->out);
12158 if( rc || zErrMsg ){
12159 char zPrefix[100];
12160 const char *zErrorTail;
12161 const char *zErrorType;
12162 if( zErrMsg==0 ){
12163 zErrorType = "Error";
12164 zErrorTail = sqlite3_errmsg(p->db);
12165 }else if( cli_strncmp(zErrMsg, "in prepare, ",12)==0 ){
12166 zErrorType = "Parse error";
12167 zErrorTail = &zErrMsg[12];
12168 }else if( cli_strncmp(zErrMsg, "stepping, ", 10)==0 ){
12169 zErrorType = "Runtime error";
12170 zErrorTail = &zErrMsg[10];
12171 }else{
12172 zErrorType = "Error";
12173 zErrorTail = zErrMsg;
12175 if( in!=0 || !stdin_is_interactive ){
12176 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
12177 "%s near line %d:", zErrorType, startline);
12178 }else{
12179 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:", zErrorType);
12181 sqlite3_fprintf(stderr,"%s %s\n", zPrefix, zErrorTail);
12182 sqlite3_free(zErrMsg);
12183 zErrMsg = 0;
12184 return 1;
12185 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
12186 char zLineBuf[2000];
12187 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
12188 "changes: %lld total_changes: %lld",
12189 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
12190 sqlite3_fprintf(p->out, "%s\n", zLineBuf);
12193 if( doAutoDetectRestore(p, zSql) ) return 1;
12194 return 0;
12197 static void echo_group_input(ShellState *p, const char *zDo){
12198 if( ShellHasFlag(p, SHFLG_Echo) ) sqlite3_fprintf(p->out, "%s\n", zDo);
12201 #ifdef SQLITE_SHELL_FIDDLE
12203 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
12204 ** impl because we need the global shellState and cannot access it from that
12205 ** function without moving lots of code around (creating a larger/messier diff).
12207 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
12208 /* Parse the next line from shellState.wasm.zInput. */
12209 const char *zBegin = shellState.wasm.zPos;
12210 const char *z = zBegin;
12211 char *zLine = 0;
12212 i64 nZ = 0;
12214 UNUSED_PARAMETER(in);
12215 UNUSED_PARAMETER(isContinuation);
12216 if(!z || !*z){
12217 return 0;
12219 while(*z && isspace(*z)) ++z;
12220 zBegin = z;
12221 for(; *z && '\n'!=*z; ++nZ, ++z){}
12222 if(nZ>0 && '\r'==zBegin[nZ-1]){
12223 --nZ;
12225 shellState.wasm.zPos = z;
12226 zLine = realloc(zPrior, nZ+1);
12227 shell_check_oom(zLine);
12228 memcpy(zLine, zBegin, nZ);
12229 zLine[nZ] = 0;
12230 return zLine;
12232 #endif /* SQLITE_SHELL_FIDDLE */
12235 ** Read input from *in and process it. If *in==0 then input
12236 ** is interactive - the user is typing it it. Otherwise, input
12237 ** is coming from a file or device. A prompt is issued and history
12238 ** is saved only if input is interactive. An interrupt signal will
12239 ** cause this routine to exit immediately, unless input is interactive.
12241 ** Return the number of errors.
12243 static int process_input(ShellState *p){
12244 char *zLine = 0; /* A single input line */
12245 char *zSql = 0; /* Accumulated SQL text */
12246 i64 nLine; /* Length of current line */
12247 i64 nSql = 0; /* Bytes of zSql[] used */
12248 i64 nAlloc = 0; /* Allocated zSql[] space */
12249 int rc; /* Error code */
12250 int errCnt = 0; /* Number of errors seen */
12251 i64 startline = 0; /* Line number for start of current input */
12252 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
12254 if( p->inputNesting==MAX_INPUT_NESTING ){
12255 /* This will be more informative in a later version. */
12256 sqlite3_fprintf(stderr,"Input nesting limit (%d) reached at line %d."
12257 " Check recursion.\n", MAX_INPUT_NESTING, p->lineno);
12258 return 1;
12260 ++p->inputNesting;
12261 p->lineno = 0;
12262 CONTINUE_PROMPT_RESET;
12263 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
12264 fflush(p->out);
12265 zLine = one_input_line(p->in, zLine, nSql>0);
12266 if( zLine==0 ){
12267 /* End of input */
12268 if( p->in==0 && stdin_is_interactive ) sqlite3_fputs("\n", p->out);
12269 break;
12271 if( seenInterrupt ){
12272 if( p->in!=0 ) break;
12273 seenInterrupt = 0;
12275 p->lineno++;
12276 if( QSS_INPLAIN(qss)
12277 && line_is_command_terminator(zLine)
12278 && line_is_complete(zSql, nSql) ){
12279 memcpy(zLine,";",2);
12281 qss = quickscan(zLine, qss, CONTINUE_PROMPT_PSTATE);
12282 if( QSS_PLAINWHITE(qss) && nSql==0 ){
12283 /* Just swallow single-line whitespace */
12284 echo_group_input(p, zLine);
12285 qss = QSS_Start;
12286 continue;
12288 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
12289 CONTINUE_PROMPT_RESET;
12290 echo_group_input(p, zLine);
12291 if( zLine[0]=='.' ){
12292 rc = do_meta_command(zLine, p);
12293 if( rc==2 ){ /* exit requested */
12294 break;
12295 }else if( rc ){
12296 errCnt++;
12299 qss = QSS_Start;
12300 continue;
12302 /* No single-line dispositions remain; accumulate line(s). */
12303 nLine = strlen(zLine);
12304 if( nSql+nLine+2>=nAlloc ){
12305 /* Grow buffer by half-again increments when big. */
12306 nAlloc = nSql+(nSql>>1)+nLine+100;
12307 zSql = realloc(zSql, nAlloc);
12308 shell_check_oom(zSql);
12310 if( nSql==0 ){
12311 i64 i;
12312 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
12313 assert( nAlloc>0 && zSql!=0 );
12314 memcpy(zSql, zLine+i, nLine+1-i);
12315 startline = p->lineno;
12316 nSql = nLine-i;
12317 }else{
12318 zSql[nSql++] = '\n';
12319 memcpy(zSql+nSql, zLine, nLine+1);
12320 nSql += nLine;
12322 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
12323 echo_group_input(p, zSql);
12324 errCnt += runOneSqlLine(p, zSql, p->in, startline);
12325 CONTINUE_PROMPT_RESET;
12326 nSql = 0;
12327 if( p->outCount ){
12328 output_reset(p);
12329 p->outCount = 0;
12330 }else{
12331 clearTempFile(p);
12333 p->bSafeMode = p->bSafeModePersist;
12334 qss = QSS_Start;
12335 }else if( nSql && QSS_PLAINWHITE(qss) ){
12336 echo_group_input(p, zSql);
12337 nSql = 0;
12338 qss = QSS_Start;
12341 if( nSql ){
12342 /* This may be incomplete. Let the SQL parser deal with that. */
12343 echo_group_input(p, zSql);
12344 errCnt += runOneSqlLine(p, zSql, p->in, startline);
12345 CONTINUE_PROMPT_RESET;
12347 free(zSql);
12348 free(zLine);
12349 --p->inputNesting;
12350 return errCnt>0;
12354 ** Return a pathname which is the user's home directory. A
12355 ** 0 return indicates an error of some kind.
12357 static char *find_home_dir(int clearFlag){
12358 static char *home_dir = NULL;
12359 if( clearFlag ){
12360 free(home_dir);
12361 home_dir = 0;
12362 return 0;
12364 if( home_dir ) return home_dir;
12366 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
12367 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
12369 struct passwd *pwent;
12370 uid_t uid = getuid();
12371 if( (pwent=getpwuid(uid)) != NULL) {
12372 home_dir = pwent->pw_dir;
12375 #endif
12377 #if defined(_WIN32_WCE)
12378 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
12380 home_dir = "/";
12381 #else
12383 #if defined(_WIN32) || defined(WIN32)
12384 if (!home_dir) {
12385 home_dir = getenv("USERPROFILE");
12387 #endif
12389 if (!home_dir) {
12390 home_dir = getenv("HOME");
12393 #if defined(_WIN32) || defined(WIN32)
12394 if (!home_dir) {
12395 char *zDrive, *zPath;
12396 int n;
12397 zDrive = getenv("HOMEDRIVE");
12398 zPath = getenv("HOMEPATH");
12399 if( zDrive && zPath ){
12400 n = strlen30(zDrive) + strlen30(zPath) + 1;
12401 home_dir = malloc( n );
12402 if( home_dir==0 ) return 0;
12403 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
12404 return home_dir;
12406 home_dir = "c:\\";
12408 #endif
12410 #endif /* !_WIN32_WCE */
12412 if( home_dir ){
12413 i64 n = strlen(home_dir) + 1;
12414 char *z = malloc( n );
12415 if( z ) memcpy(z, home_dir, n);
12416 home_dir = z;
12419 return home_dir;
12423 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
12424 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
12425 ** the path to it. If there is no $(XDG_CONFIG_HOME) then
12426 ** look for $(HOME)/.config/sqlite3/sqliterc and if found
12427 ** return that. If none of these are found, return 0.
12429 ** The string returned is obtained from sqlite3_malloc() and
12430 ** should be freed by the caller.
12432 static char *find_xdg_config(void){
12433 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
12434 || defined(__RTP__) || defined(_WRS_KERNEL)
12435 return 0;
12436 #else
12437 char *zConfig = 0;
12438 const char *zXdgHome;
12440 zXdgHome = getenv("XDG_CONFIG_HOME");
12441 if( zXdgHome==0 ){
12442 const char *zHome = getenv("HOME");
12443 if( zHome==0 ) return 0;
12444 zConfig = sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome);
12445 }else{
12446 zConfig = sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome);
12448 shell_check_oom(zConfig);
12449 if( access(zConfig,0)!=0 ){
12450 sqlite3_free(zConfig);
12451 zConfig = 0;
12453 return zConfig;
12454 #endif
12458 ** Read input from the file given by sqliterc_override. Or if that
12459 ** parameter is NULL, take input from the first of find_xdg_config()
12460 ** or ~/.sqliterc which is found.
12462 ** Returns the number of errors.
12464 static void process_sqliterc(
12465 ShellState *p, /* Configuration data */
12466 const char *sqliterc_override /* Name of config file. NULL to use default */
12468 char *home_dir = NULL;
12469 const char *sqliterc = sqliterc_override;
12470 char *zBuf = 0;
12471 FILE *inSaved = p->in;
12472 int savedLineno = p->lineno;
12474 if( sqliterc == NULL ){
12475 sqliterc = zBuf = find_xdg_config();
12477 if( sqliterc == NULL ){
12478 home_dir = find_home_dir(0);
12479 if( home_dir==0 ){
12480 eputz("-- warning: cannot find home directory;"
12481 " cannot read ~/.sqliterc\n");
12482 return;
12484 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
12485 shell_check_oom(zBuf);
12486 sqliterc = zBuf;
12488 p->in = sqlite3_fopen(sqliterc,"rb");
12489 if( p->in ){
12490 if( stdin_is_interactive ){
12491 sqlite3_fprintf(stderr,"-- Loading resources from %s\n", sqliterc);
12493 if( process_input(p) && bail_on_error ) exit(1);
12494 fclose(p->in);
12495 }else if( sqliterc_override!=0 ){
12496 sqlite3_fprintf(stderr,"cannot open: \"%s\"\n", sqliterc);
12497 if( bail_on_error ) exit(1);
12499 p->in = inSaved;
12500 p->lineno = savedLineno;
12501 sqlite3_free(zBuf);
12505 ** Show available command line options
12507 static const char zOptions[] =
12508 " -- treat no subsequent arguments as options\n"
12509 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12510 " -A ARGS... run \".archive ARGS\" and exit\n"
12511 #endif
12512 " -append append the database to the end of the file\n"
12513 " -ascii set output mode to 'ascii'\n"
12514 " -bail stop after hitting an error\n"
12515 " -batch force batch I/O\n"
12516 " -box set output mode to 'box'\n"
12517 " -column set output mode to 'column'\n"
12518 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
12519 " -csv set output mode to 'csv'\n"
12520 #if !defined(SQLITE_OMIT_DESERIALIZE)
12521 " -deserialize open the database using sqlite3_deserialize()\n"
12522 #endif
12523 " -echo print inputs before execution\n"
12524 " -init FILENAME read/process named file\n"
12525 " -[no]header turn headers on or off\n"
12526 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12527 " -heap SIZE Size of heap for memsys3 or memsys5\n"
12528 #endif
12529 " -help show this message\n"
12530 " -html set output mode to HTML\n"
12531 " -interactive force interactive I/O\n"
12532 " -json set output mode to 'json'\n"
12533 " -line set output mode to 'line'\n"
12534 " -list set output mode to 'list'\n"
12535 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
12536 " -markdown set output mode to 'markdown'\n"
12537 #if !defined(SQLITE_OMIT_DESERIALIZE)
12538 " -maxsize N maximum size for a --deserialize database\n"
12539 #endif
12540 " -memtrace trace all memory allocations and deallocations\n"
12541 " -mmap N default mmap size set to N\n"
12542 #ifdef SQLITE_ENABLE_MULTIPLEX
12543 " -multiplex enable the multiplexor VFS\n"
12544 #endif
12545 " -newline SEP set output row separator. Default: '\\n'\n"
12546 " -nofollow refuse to open symbolic links to database files\n"
12547 " -nonce STRING set the safe-mode escape nonce\n"
12548 " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n"
12549 " -nullvalue TEXT set text string for NULL values. Default ''\n"
12550 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
12551 " -pcachetrace trace all page cache operations\n"
12552 " -quote set output mode to 'quote'\n"
12553 " -readonly open the database read-only\n"
12554 " -safe enable safe-mode\n"
12555 " -separator SEP set output column separator. Default: '|'\n"
12556 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
12557 " -sorterref SIZE sorter references threshold size\n"
12558 #endif
12559 " -stats print memory stats before each finalize\n"
12560 " -table set output mode to 'table'\n"
12561 " -tabs set output mode to 'tabs'\n"
12562 " -unsafe-testing allow unsafe commands and modes for testing\n"
12563 " -version show SQLite version\n"
12564 " -vfs NAME use NAME as the default VFS\n"
12565 " -vfstrace enable tracing of all VFS calls\n"
12566 #ifdef SQLITE_HAVE_ZLIB
12567 " -zip open the file as a ZIP Archive\n"
12568 #endif
12570 static void usage(int showDetail){
12571 sqlite3_fprintf(stderr,"Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
12572 "FILENAME is the name of an SQLite database. A new database is created\n"
12573 "if the file does not previously exist. Defaults to :memory:.\n", Argv0);
12574 if( showDetail ){
12575 sqlite3_fprintf(stderr,"OPTIONS include:\n%s", zOptions);
12576 }else{
12577 eputz("Use the -help option for additional information\n");
12579 exit(0);
12583 ** Internal check: Verify that the SQLite is uninitialized. Print a
12584 ** error message if it is initialized.
12586 static void verify_uninitialized(void){
12587 if( sqlite3_config(-1)==SQLITE_MISUSE ){
12588 sputz(stdout, "WARNING: attempt to configure SQLite after"
12589 " initialization.\n");
12594 ** Initialize the state information in data
12596 static void main_init(ShellState *data) {
12597 memset(data, 0, sizeof(*data));
12598 data->normalMode = data->cMode = data->mode = MODE_List;
12599 data->autoExplain = 1;
12600 #ifdef _WIN32
12601 data->crlfMode = 1;
12602 #endif
12603 data->pAuxDb = &data->aAuxDb[0];
12604 memcpy(data->colSeparator,SEP_Column, 2);
12605 memcpy(data->rowSeparator,SEP_Row, 2);
12606 data->showHeader = 0;
12607 data->shellFlgs = SHFLG_Lookaside;
12608 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
12609 #if !defined(SQLITE_SHELL_FIDDLE)
12610 verify_uninitialized();
12611 #endif
12612 sqlite3_config(SQLITE_CONFIG_URI, 1);
12613 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
12614 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
12615 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
12619 ** Output text to the console in a font that attracts extra attention.
12621 #if defined(_WIN32) || defined(WIN32)
12622 static void printBold(const char *zText){
12623 #if !SQLITE_OS_WINRT
12624 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
12625 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
12626 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
12627 SetConsoleTextAttribute(out,
12628 FOREGROUND_RED|FOREGROUND_INTENSITY
12630 #endif
12631 sputz(stdout, zText);
12632 #if !SQLITE_OS_WINRT
12633 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
12634 #endif
12636 #else
12637 static void printBold(const char *zText){
12638 sqlite3_fprintf(stdout, "\033[1m%s\033[0m", zText);
12640 #endif
12643 ** Get the argument to an --option. Throw an error and die if no argument
12644 ** is available.
12646 static char *cmdline_option_value(int argc, char **argv, int i){
12647 if( i==argc ){
12648 sqlite3_fprintf(stderr,
12649 "%s: Error: missing argument to %s\n", argv[0], argv[argc-1]);
12650 exit(1);
12652 return argv[i];
12655 static void sayAbnormalExit(void){
12656 if( seenInterrupt ) eputz("Program interrupted.\n");
12659 #ifndef SQLITE_SHELL_IS_UTF8
12660 # if (defined(_WIN32) || defined(WIN32)) \
12661 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12662 # define SQLITE_SHELL_IS_UTF8 (0)
12663 # else
12664 # define SQLITE_SHELL_IS_UTF8 (1)
12665 # endif
12666 #endif
12668 #ifdef SQLITE_SHELL_FIDDLE
12669 # define main fiddle_main
12670 #endif
12672 #if SQLITE_SHELL_IS_UTF8
12673 int SQLITE_CDECL main(int argc, char **argv){
12674 #else
12675 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
12676 char **argv;
12677 #endif
12678 #ifdef SQLITE_DEBUG
12679 sqlite3_int64 mem_main_enter = 0;
12680 #endif
12681 char *zErrMsg = 0;
12682 #ifdef SQLITE_SHELL_FIDDLE
12683 # define data shellState
12684 #else
12685 ShellState data;
12686 #endif
12687 const char *zInitFile = 0;
12688 int i;
12689 int rc = 0;
12690 int warnInmemoryDb = 0;
12691 int readStdin = 1;
12692 int nCmd = 0;
12693 int nOptsEnd = argc;
12694 int bEnableVfstrace = 0;
12695 char **azCmd = 0;
12696 const char *zVfs = 0; /* Value of -vfs command-line option */
12697 #if !SQLITE_SHELL_IS_UTF8
12698 char **argvToFree = 0;
12699 int argcToFree = 0;
12700 #endif
12701 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
12703 #ifdef SQLITE_SHELL_FIDDLE
12704 stdin_is_interactive = 0;
12705 stdout_is_console = 1;
12706 data.wasm.zDefaultDbName = "/fiddle.sqlite3";
12707 #else
12708 stdin_is_interactive = isatty(0);
12709 stdout_is_console = isatty(1);
12710 #endif
12711 atexit(sayAbnormalExit);
12712 #ifdef SQLITE_DEBUG
12713 mem_main_enter = sqlite3_memory_used();
12714 #endif
12715 #if !defined(_WIN32_WCE)
12716 if( getenv("SQLITE_DEBUG_BREAK") ){
12717 if( isatty(0) && isatty(2) ){
12718 char zLine[100];
12719 sqlite3_fprintf(stderr,
12720 "attach debugger to process %d and press ENTER to continue...",
12721 GETPID());
12722 if( sqlite3_fgets(zLine, sizeof(zLine), stdin)!=0
12723 && cli_strcmp(zLine,"stop")==0
12725 exit(1);
12727 }else{
12728 #if defined(_WIN32) || defined(WIN32)
12729 #if SQLITE_OS_WINRT
12730 __debugbreak();
12731 #else
12732 DebugBreak();
12733 #endif
12734 #elif defined(SIGTRAP)
12735 raise(SIGTRAP);
12736 #endif
12739 #endif
12740 /* Register a valid signal handler early, before much else is done. */
12741 #ifdef SIGINT
12742 signal(SIGINT, interrupt_handler);
12743 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12744 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE) ){
12745 eputz("No ^C handler.\n");
12747 #endif
12749 #if USE_SYSTEM_SQLITE+0!=1
12750 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
12751 sqlite3_fprintf(stderr,
12752 "SQLite header and source version mismatch\n%s\n%s\n",
12753 sqlite3_sourceid(), SQLITE_SOURCE_ID);
12754 exit(1);
12756 #endif
12757 main_init(&data);
12759 /* On Windows, we must translate command-line arguments into UTF-8.
12760 ** The SQLite memory allocator subsystem has to be enabled in order to
12761 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
12762 ** subsequent sqlite3_config() calls will work. So copy all results into
12763 ** memory that does not come from the SQLite memory allocator.
12765 #if !SQLITE_SHELL_IS_UTF8
12766 sqlite3_initialize();
12767 argvToFree = malloc(sizeof(argv[0])*argc*2);
12768 shell_check_oom(argvToFree);
12769 argcToFree = argc;
12770 argv = argvToFree + argc;
12771 for(i=0; i<argc; i++){
12772 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
12773 i64 n;
12774 shell_check_oom(z);
12775 n = strlen(z);
12776 argv[i] = malloc( n+1 );
12777 shell_check_oom(argv[i]);
12778 memcpy(argv[i], z, n+1);
12779 argvToFree[i] = argv[i];
12780 sqlite3_free(z);
12782 sqlite3_shutdown();
12783 #endif
12785 assert( argc>=1 && argv && argv[0] );
12786 Argv0 = argv[0];
12788 #ifdef SQLITE_SHELL_DBNAME_PROC
12790 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12791 ** of a C-function that will provide the name of the database file. Use
12792 ** this compile-time option to embed this shell program in larger
12793 ** applications. */
12794 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12795 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
12796 warnInmemoryDb = 0;
12798 #endif
12800 /* Do an initial pass through the command-line argument to locate
12801 ** the name of the database file, the name of the initialization file,
12802 ** the size of the alternative malloc heap, options affecting commands
12803 ** or SQL run from the command line, and the first command to execute.
12805 #ifndef SQLITE_SHELL_FIDDLE
12806 verify_uninitialized();
12807 #endif
12808 for(i=1; i<argc; i++){
12809 char *z;
12810 z = argv[i];
12811 if( z[0]!='-' || i>nOptsEnd ){
12812 if( data.aAuxDb->zDbFilename==0 ){
12813 data.aAuxDb->zDbFilename = z;
12814 }else{
12815 /* Excess arguments are interpreted as SQL (or dot-commands) and
12816 ** mean that nothing is read from stdin */
12817 readStdin = 0;
12818 nCmd++;
12819 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
12820 shell_check_oom(azCmd);
12821 azCmd[nCmd-1] = z;
12823 continue;
12825 if( z[1]=='-' ) z++;
12826 if( cli_strcmp(z, "-")==0 ){
12827 nOptsEnd = i;
12828 continue;
12829 }else if( cli_strcmp(z,"-separator")==0
12830 || cli_strcmp(z,"-nullvalue")==0
12831 || cli_strcmp(z,"-newline")==0
12832 || cli_strcmp(z,"-cmd")==0
12834 (void)cmdline_option_value(argc, argv, ++i);
12835 }else if( cli_strcmp(z,"-init")==0 ){
12836 zInitFile = cmdline_option_value(argc, argv, ++i);
12837 }else if( cli_strcmp(z,"-interactive")==0 ){
12838 }else if( cli_strcmp(z,"-batch")==0 ){
12839 /* Need to check for batch mode here to so we can avoid printing
12840 ** informational messages (like from process_sqliterc) before
12841 ** we do the actual processing of arguments later in a second pass.
12843 stdin_is_interactive = 0;
12844 }else if( cli_strcmp(z,"-utf8")==0 ){
12845 }else if( cli_strcmp(z,"-no-utf8")==0 ){
12846 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
12847 int val = 0;
12848 sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW, &val);
12849 assert( val==0 );
12850 }else if( cli_strcmp(z,"-heap")==0 ){
12851 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12852 const char *zSize;
12853 sqlite3_int64 szHeap;
12855 zSize = cmdline_option_value(argc, argv, ++i);
12856 szHeap = integerValue(zSize);
12857 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
12858 verify_uninitialized();
12859 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
12860 #else
12861 (void)cmdline_option_value(argc, argv, ++i);
12862 #endif
12863 }else if( cli_strcmp(z,"-pagecache")==0 ){
12864 sqlite3_int64 n, sz;
12865 sz = integerValue(cmdline_option_value(argc,argv,++i));
12866 if( sz>70000 ) sz = 70000;
12867 if( sz<0 ) sz = 0;
12868 n = integerValue(cmdline_option_value(argc,argv,++i));
12869 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
12870 n = 0xffffffffffffLL/sz;
12872 verify_uninitialized();
12873 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
12874 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
12875 data.shellFlgs |= SHFLG_Pagecache;
12876 }else if( cli_strcmp(z,"-lookaside")==0 ){
12877 int n, sz;
12878 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
12879 if( sz<0 ) sz = 0;
12880 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12881 if( n<0 ) n = 0;
12882 verify_uninitialized();
12883 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
12884 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
12885 }else if( cli_strcmp(z,"-threadsafe")==0 ){
12886 int n;
12887 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
12888 verify_uninitialized();
12889 switch( n ){
12890 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
12891 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
12892 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
12894 }else if( cli_strcmp(z,"-vfstrace")==0 ){
12895 vfstrace_register("trace",0,(int(*)(const char*,void*))sqlite3_fputs,
12896 stderr,1);
12897 bEnableVfstrace = 1;
12898 #ifdef SQLITE_ENABLE_MULTIPLEX
12899 }else if( cli_strcmp(z,"-multiplex")==0 ){
12900 extern int sqlite3_multiplex_initialize(const char*,int);
12901 sqlite3_multiplex_initialize(0, 1);
12902 #endif
12903 }else if( cli_strcmp(z,"-mmap")==0 ){
12904 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12905 verify_uninitialized();
12906 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
12907 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
12908 }else if( cli_strcmp(z,"-sorterref")==0 ){
12909 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
12910 verify_uninitialized();
12911 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
12912 #endif
12913 }else if( cli_strcmp(z,"-vfs")==0 ){
12914 zVfs = cmdline_option_value(argc, argv, ++i);
12915 #ifdef SQLITE_HAVE_ZLIB
12916 }else if( cli_strcmp(z,"-zip")==0 ){
12917 data.openMode = SHELL_OPEN_ZIPFILE;
12918 #endif
12919 }else if( cli_strcmp(z,"-append")==0 ){
12920 data.openMode = SHELL_OPEN_APPENDVFS;
12921 #ifndef SQLITE_OMIT_DESERIALIZE
12922 }else if( cli_strcmp(z,"-deserialize")==0 ){
12923 data.openMode = SHELL_OPEN_DESERIALIZE;
12924 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
12925 data.szMax = integerValue(argv[++i]);
12926 #endif
12927 }else if( cli_strcmp(z,"-readonly")==0 ){
12928 data.openMode = SHELL_OPEN_READONLY;
12929 }else if( cli_strcmp(z,"-nofollow")==0 ){
12930 data.openFlags = SQLITE_OPEN_NOFOLLOW;
12931 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12932 }else if( cli_strncmp(z, "-A",2)==0 ){
12933 /* All remaining command-line arguments are passed to the ".archive"
12934 ** command, so ignore them */
12935 break;
12936 #endif
12937 }else if( cli_strcmp(z, "-memtrace")==0 ){
12938 sqlite3MemTraceActivate(stderr);
12939 }else if( cli_strcmp(z, "-pcachetrace")==0 ){
12940 sqlite3PcacheTraceActivate(stderr);
12941 }else if( cli_strcmp(z,"-bail")==0 ){
12942 bail_on_error = 1;
12943 }else if( cli_strcmp(z,"-nonce")==0 ){
12944 free(data.zNonce);
12945 data.zNonce = strdup(cmdline_option_value(argc, argv, ++i));
12946 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
12947 ShellSetFlag(&data,SHFLG_TestingMode);
12948 }else if( cli_strcmp(z,"-safe")==0 ){
12949 /* no-op - catch this on the second pass */
12952 #ifndef SQLITE_SHELL_FIDDLE
12953 if( !bEnableVfstrace ) verify_uninitialized();
12954 #endif
12957 #ifdef SQLITE_SHELL_INIT_PROC
12959 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12960 ** of a C-function that will perform initialization actions on SQLite that
12961 ** occur just before or after sqlite3_initialize(). Use this compile-time
12962 ** option to embed this shell program in larger applications. */
12963 extern void SQLITE_SHELL_INIT_PROC(void);
12964 SQLITE_SHELL_INIT_PROC();
12966 #else
12967 /* All the sqlite3_config() calls have now been made. So it is safe
12968 ** to call sqlite3_initialize() and process any command line -vfs option. */
12969 sqlite3_initialize();
12970 #endif
12972 if( zVfs ){
12973 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
12974 if( pVfs ){
12975 sqlite3_vfs_register(pVfs, 1);
12976 }else{
12977 sqlite3_fprintf(stderr,"no such VFS: \"%s\"\n", zVfs);
12978 exit(1);
12982 if( data.pAuxDb->zDbFilename==0 ){
12983 #ifndef SQLITE_OMIT_MEMORYDB
12984 data.pAuxDb->zDbFilename = ":memory:";
12985 warnInmemoryDb = argc==1;
12986 #else
12987 sqlite3_fprintf(stderr,
12988 "%s: Error: no database filename specified\n", Argv0);
12989 return 1;
12990 #endif
12992 data.out = stdout;
12993 #ifndef SQLITE_SHELL_FIDDLE
12994 sqlite3_appendvfs_init(0,0,0);
12995 #endif
12997 /* Go ahead and open the database file if it already exists. If the
12998 ** file does not exist, delay opening it. This prevents empty database
12999 ** files from being created if a user mistypes the database name argument
13000 ** to the sqlite command-line tool.
13002 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
13003 open_db(&data, 0);
13006 /* Process the initialization file if there is one. If no -init option
13007 ** is given on the command line, look for a file named ~/.sqliterc and
13008 ** try to process it.
13010 process_sqliterc(&data,zInitFile);
13012 /* Make a second pass through the command-line argument and set
13013 ** options. This second pass is delayed until after the initialization
13014 ** file is processed so that the command-line arguments will override
13015 ** settings in the initialization file.
13017 for(i=1; i<argc; i++){
13018 char *z = argv[i];
13019 if( z[0]!='-' || i>=nOptsEnd ) continue;
13020 if( z[1]=='-' ){ z++; }
13021 if( cli_strcmp(z,"-init")==0 ){
13022 i++;
13023 }else if( cli_strcmp(z,"-html")==0 ){
13024 data.mode = MODE_Html;
13025 }else if( cli_strcmp(z,"-list")==0 ){
13026 data.mode = MODE_List;
13027 }else if( cli_strcmp(z,"-quote")==0 ){
13028 data.mode = MODE_Quote;
13029 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
13030 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
13031 }else if( cli_strcmp(z,"-line")==0 ){
13032 data.mode = MODE_Line;
13033 }else if( cli_strcmp(z,"-column")==0 ){
13034 data.mode = MODE_Column;
13035 }else if( cli_strcmp(z,"-json")==0 ){
13036 data.mode = MODE_Json;
13037 }else if( cli_strcmp(z,"-markdown")==0 ){
13038 data.mode = MODE_Markdown;
13039 }else if( cli_strcmp(z,"-table")==0 ){
13040 data.mode = MODE_Table;
13041 }else if( cli_strcmp(z,"-box")==0 ){
13042 data.mode = MODE_Box;
13043 }else if( cli_strcmp(z,"-csv")==0 ){
13044 data.mode = MODE_Csv;
13045 memcpy(data.colSeparator,",",2);
13046 #ifdef SQLITE_HAVE_ZLIB
13047 }else if( cli_strcmp(z,"-zip")==0 ){
13048 data.openMode = SHELL_OPEN_ZIPFILE;
13049 #endif
13050 }else if( cli_strcmp(z,"-append")==0 ){
13051 data.openMode = SHELL_OPEN_APPENDVFS;
13052 #ifndef SQLITE_OMIT_DESERIALIZE
13053 }else if( cli_strcmp(z,"-deserialize")==0 ){
13054 data.openMode = SHELL_OPEN_DESERIALIZE;
13055 }else if( cli_strcmp(z,"-maxsize")==0 && i+1<argc ){
13056 data.szMax = integerValue(argv[++i]);
13057 #endif
13058 }else if( cli_strcmp(z,"-readonly")==0 ){
13059 data.openMode = SHELL_OPEN_READONLY;
13060 }else if( cli_strcmp(z,"-nofollow")==0 ){
13061 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
13062 }else if( cli_strcmp(z,"-ascii")==0 ){
13063 data.mode = MODE_Ascii;
13064 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Unit);
13065 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Record);
13066 }else if( cli_strcmp(z,"-tabs")==0 ){
13067 data.mode = MODE_List;
13068 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,SEP_Tab);
13069 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,SEP_Row);
13070 }else if( cli_strcmp(z,"-separator")==0 ){
13071 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
13072 "%s",cmdline_option_value(argc,argv,++i));
13073 }else if( cli_strcmp(z,"-newline")==0 ){
13074 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
13075 "%s",cmdline_option_value(argc,argv,++i));
13076 }else if( cli_strcmp(z,"-nullvalue")==0 ){
13077 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
13078 "%s",cmdline_option_value(argc,argv,++i));
13079 }else if( cli_strcmp(z,"-header")==0 ){
13080 data.showHeader = 1;
13081 ShellSetFlag(&data, SHFLG_HeaderSet);
13082 }else if( cli_strcmp(z,"-noheader")==0 ){
13083 data.showHeader = 0;
13084 ShellSetFlag(&data, SHFLG_HeaderSet);
13085 }else if( cli_strcmp(z,"-echo")==0 ){
13086 ShellSetFlag(&data, SHFLG_Echo);
13087 }else if( cli_strcmp(z,"-eqp")==0 ){
13088 data.autoEQP = AUTOEQP_on;
13089 }else if( cli_strcmp(z,"-eqpfull")==0 ){
13090 data.autoEQP = AUTOEQP_full;
13091 }else if( cli_strcmp(z,"-stats")==0 ){
13092 data.statsOn = 1;
13093 }else if( cli_strcmp(z,"-scanstats")==0 ){
13094 data.scanstatsOn = 1;
13095 }else if( cli_strcmp(z,"-backslash")==0 ){
13096 /* Undocumented command-line option: -backslash
13097 ** Causes C-style backslash escapes to be evaluated in SQL statements
13098 ** prior to sending the SQL into SQLite. Useful for injecting
13099 ** crazy bytes in the middle of SQL statements for testing and debugging.
13101 ShellSetFlag(&data, SHFLG_Backslash);
13102 }else if( cli_strcmp(z,"-bail")==0 ){
13103 /* No-op. The bail_on_error flag should already be set. */
13104 }else if( cli_strcmp(z,"-version")==0 ){
13105 sqlite3_fprintf(stdout, "%s %s (%d-bit)\n",
13106 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
13107 return 0;
13108 }else if( cli_strcmp(z,"-interactive")==0 ){
13109 /* Need to check for interactive override here to so that it can
13110 ** affect console setup (for Windows only) and testing thereof.
13112 stdin_is_interactive = 1;
13113 }else if( cli_strcmp(z,"-batch")==0 ){
13114 /* already handled */
13115 }else if( cli_strcmp(z,"-utf8")==0 ){
13116 /* already handled */
13117 }else if( cli_strcmp(z,"-no-utf8")==0 ){
13118 /* already handled */
13119 }else if( cli_strcmp(z,"-no-rowid-in-view")==0 ){
13120 /* already handled */
13121 }else if( cli_strcmp(z,"-heap")==0 ){
13122 i++;
13123 }else if( cli_strcmp(z,"-pagecache")==0 ){
13124 i+=2;
13125 }else if( cli_strcmp(z,"-lookaside")==0 ){
13126 i+=2;
13127 }else if( cli_strcmp(z,"-threadsafe")==0 ){
13128 i+=2;
13129 }else if( cli_strcmp(z,"-nonce")==0 ){
13130 i += 2;
13131 }else if( cli_strcmp(z,"-mmap")==0 ){
13132 i++;
13133 }else if( cli_strcmp(z,"-memtrace")==0 ){
13134 i++;
13135 }else if( cli_strcmp(z,"-pcachetrace")==0 ){
13136 i++;
13137 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
13138 }else if( cli_strcmp(z,"-sorterref")==0 ){
13139 i++;
13140 #endif
13141 }else if( cli_strcmp(z,"-vfs")==0 ){
13142 i++;
13143 }else if( cli_strcmp(z,"-vfstrace")==0 ){
13144 i++;
13145 #ifdef SQLITE_ENABLE_MULTIPLEX
13146 }else if( cli_strcmp(z,"-multiplex")==0 ){
13147 i++;
13148 #endif
13149 }else if( cli_strcmp(z,"-help")==0 ){
13150 usage(1);
13151 }else if( cli_strcmp(z,"-cmd")==0 ){
13152 /* Run commands that follow -cmd first and separately from commands
13153 ** that simply appear on the command-line. This seems goofy. It would
13154 ** be better if all commands ran in the order that they appear. But
13155 ** we retain the goofy behavior for historical compatibility. */
13156 if( i==argc-1 ) break;
13157 z = cmdline_option_value(argc,argv,++i);
13158 if( z[0]=='.' ){
13159 rc = do_meta_command(z, &data);
13160 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
13161 }else{
13162 open_db(&data, 0);
13163 rc = shell_exec(&data, z, &zErrMsg);
13164 if( zErrMsg!=0 ){
13165 shellEmitError(zErrMsg);
13166 if( bail_on_error ) return rc!=0 ? rc : 1;
13167 }else if( rc!=0 ){
13168 sqlite3_fprintf(stderr,"Error: unable to process SQL \"%s\"\n", z);
13169 if( bail_on_error ) return rc;
13172 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13173 }else if( cli_strncmp(z, "-A", 2)==0 ){
13174 if( nCmd>0 ){
13175 sqlite3_fprintf(stderr,"Error: cannot mix regular SQL or dot-commands"
13176 " with \"%s\"\n", z);
13177 return 1;
13179 open_db(&data, OPEN_DB_ZIPFILE);
13180 if( z[2] ){
13181 argv[i] = &z[2];
13182 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
13183 }else{
13184 arDotCommand(&data, 1, argv+i, argc-i);
13186 readStdin = 0;
13187 break;
13188 #endif
13189 }else if( cli_strcmp(z,"-safe")==0 ){
13190 data.bSafeMode = data.bSafeModePersist = 1;
13191 }else if( cli_strcmp(z,"-unsafe-testing")==0 ){
13192 /* Acted upon in first pass. */
13193 }else{
13194 sqlite3_fprintf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
13195 eputz("Use -help for a list of options.\n");
13196 return 1;
13198 data.cMode = data.mode;
13201 if( !readStdin ){
13202 /* Run all arguments that do not begin with '-' as if they were separate
13203 ** command-line inputs, except for the argToSkip argument which contains
13204 ** the database filename.
13206 for(i=0; i<nCmd; i++){
13207 if( azCmd[i][0]=='.' ){
13208 rc = do_meta_command(azCmd[i], &data);
13209 if( rc ){
13210 if( rc==2 ) rc = 0;
13211 goto shell_main_exit;
13213 }else{
13214 open_db(&data, 0);
13215 echo_group_input(&data, azCmd[i]);
13216 rc = shell_exec(&data, azCmd[i], &zErrMsg);
13217 if( zErrMsg || rc ){
13218 if( zErrMsg!=0 ){
13219 shellEmitError(zErrMsg);
13220 }else{
13221 sqlite3_fprintf(stderr,
13222 "Error: unable to process SQL: %s\n", azCmd[i]);
13224 sqlite3_free(zErrMsg);
13225 if( rc==0 ) rc = 1;
13226 goto shell_main_exit;
13230 }else{
13231 /* Run commands received from standard input
13233 if( stdin_is_interactive ){
13234 char *zHome;
13235 char *zHistory;
13236 int nHistory;
13237 #if CIO_WIN_WC_XLATE
13238 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
13239 #else
13240 # define SHELL_CIO_CHAR_SET ""
13241 #endif
13242 sqlite3_fprintf(stdout,
13243 "SQLite version %s %.19s%s\n" /*extra-version-info*/
13244 "Enter \".help\" for usage hints.\n",
13245 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET);
13246 if( warnInmemoryDb ){
13247 sputz(stdout, "Connected to a ");
13248 printBold("transient in-memory database");
13249 sputz(stdout, ".\nUse \".open FILENAME\" to reopen on a"
13250 " persistent database.\n");
13252 zHistory = getenv("SQLITE_HISTORY");
13253 if( zHistory ){
13254 zHistory = strdup(zHistory);
13255 }else if( (zHome = find_home_dir(0))!=0 ){
13256 nHistory = strlen30(zHome) + 20;
13257 if( (zHistory = malloc(nHistory))!=0 ){
13258 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
13261 if( zHistory ){ shell_read_history(zHistory); }
13262 #if HAVE_READLINE || HAVE_EDITLINE
13263 rl_attempted_completion_function = readline_completion;
13264 #elif HAVE_LINENOISE
13265 linenoiseSetCompletionCallback(linenoise_completion, NULL);
13266 #endif
13267 data.in = 0;
13268 rc = process_input(&data);
13269 if( zHistory ){
13270 shell_stifle_history(2000);
13271 shell_write_history(zHistory);
13272 free(zHistory);
13274 }else{
13275 data.in = stdin;
13276 rc = process_input(&data);
13279 #ifndef SQLITE_SHELL_FIDDLE
13280 /* In WASM mode we have to leave the db state in place so that
13281 ** client code can "push" SQL into it after this call returns. */
13282 #ifndef SQLITE_OMIT_VIRTUALTABLE
13283 if( data.expert.pExpert ){
13284 expertFinish(&data, 1, 0);
13286 #endif
13287 shell_main_exit:
13288 free(azCmd);
13289 set_table_name(&data, 0);
13290 if( data.db ){
13291 session_close_all(&data, -1);
13292 close_db(data.db);
13294 for(i=0; i<ArraySize(data.aAuxDb); i++){
13295 sqlite3_free(data.aAuxDb[i].zFreeOnClose);
13296 if( data.aAuxDb[i].db ){
13297 session_close_all(&data, i);
13298 close_db(data.aAuxDb[i].db);
13301 find_home_dir(1);
13302 output_reset(&data);
13303 data.doXdgOpen = 0;
13304 clearTempFile(&data);
13305 #if !SQLITE_SHELL_IS_UTF8
13306 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
13307 free(argvToFree);
13308 #endif
13309 free(data.colWidth);
13310 free(data.zNonce);
13311 /* Clear the global data structure so that valgrind will detect memory
13312 ** leaks */
13313 memset(&data, 0, sizeof(data));
13314 if( bEnableVfstrace ){
13315 vfstrace_unregister("trace");
13317 #ifdef SQLITE_DEBUG
13318 if( sqlite3_memory_used()>mem_main_enter ){
13319 sqlite3_fprintf(stderr,"Memory leaked: %u bytes\n",
13320 (unsigned int)(sqlite3_memory_used()-mem_main_enter));
13322 #endif
13323 #else /* SQLITE_SHELL_FIDDLE... */
13324 shell_main_exit:
13325 #endif
13326 return rc;
13330 #ifdef SQLITE_SHELL_FIDDLE
13331 /* Only for emcc experimentation purposes. */
13332 int fiddle_experiment(int a,int b){
13333 return a + b;
13337 ** Returns a pointer to the current DB handle.
13339 sqlite3 * fiddle_db_handle(){
13340 return globalDb;
13344 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
13345 ** "main" is assumed. Returns 0 if no db with the given name is
13346 ** open.
13348 sqlite3_vfs * fiddle_db_vfs(const char *zDbName){
13349 sqlite3_vfs * pVfs = 0;
13350 if(globalDb){
13351 sqlite3_file_control(globalDb, zDbName ? zDbName : "main",
13352 SQLITE_FCNTL_VFS_POINTER, &pVfs);
13354 return pVfs;
13357 /* Only for emcc experimentation purposes. */
13358 sqlite3 * fiddle_db_arg(sqlite3 *arg){
13359 sqlite3_fprintf(stdout, "fiddle_db_arg(%p)\n", (const void*)arg);
13360 return arg;
13364 ** Intended to be called via a SharedWorker() while a separate
13365 ** SharedWorker() (which manages the wasm module) is performing work
13366 ** which should be interrupted. Unfortunately, SharedWorker is not
13367 ** portable enough to make real use of.
13369 void fiddle_interrupt(void){
13370 if( globalDb ) sqlite3_interrupt(globalDb);
13374 ** Returns the filename of the given db name, assuming "main" if
13375 ** zDbName is NULL. Returns NULL if globalDb is not opened.
13377 const char * fiddle_db_filename(const char * zDbName){
13378 return globalDb
13379 ? sqlite3_db_filename(globalDb, zDbName ? zDbName : "main")
13380 : NULL;
13384 ** Completely wipes out the contents of the currently-opened database
13385 ** but leaves its storage intact for reuse. If any transactions are
13386 ** active, they are forcibly rolled back.
13388 void fiddle_reset_db(void){
13389 if( globalDb ){
13390 int rc;
13391 while( sqlite3_txn_state(globalDb,0)>0 ){
13393 ** Resolve problem reported in
13394 ** https://sqlite.org/forum/forumpost/0b41a25d65
13396 sqlite3_fputs("Rolling back in-progress transaction.\n", stdout);
13397 sqlite3_exec(globalDb,"ROLLBACK", 0, 0, 0);
13399 rc = sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 1, 0);
13400 if( 0==rc ) sqlite3_exec(globalDb, "VACUUM", 0, 0, 0);
13401 sqlite3_db_config(globalDb, SQLITE_DBCONFIG_RESET_DATABASE, 0, 0);
13406 ** Uses the current database's VFS xRead to stream the db file's
13407 ** contents out to the given callback. The callback gets a single
13408 ** chunk of size n (its 2nd argument) on each call and must return 0
13409 ** on success, non-0 on error. This function returns 0 on success,
13410 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
13411 ** code from the callback. Note that this is not thread-friendly: it
13412 ** expects that it will be the only thread reading the db file and
13413 ** takes no measures to ensure that is the case.
13415 int fiddle_export_db( int (*xCallback)(unsigned const char *zOut, int n) ){
13416 sqlite3_int64 nSize = 0;
13417 sqlite3_int64 nPos = 0;
13418 sqlite3_file * pFile = 0;
13419 unsigned char buf[1024 * 8];
13420 int nBuf = (int)sizeof(buf);
13421 int rc = shellState.db
13422 ? sqlite3_file_control(shellState.db, "main",
13423 SQLITE_FCNTL_FILE_POINTER, &pFile)
13424 : SQLITE_NOTFOUND;
13425 if( rc ) return rc;
13426 rc = pFile->pMethods->xFileSize(pFile, &nSize);
13427 if( rc ) return rc;
13428 if(nSize % nBuf){
13429 /* DB size is not an even multiple of the buffer size. Reduce
13430 ** buffer size so that we do not unduly inflate the db size when
13431 ** exporting. */
13432 if(0 == nSize % 4096) nBuf = 4096;
13433 else if(0 == nSize % 2048) nBuf = 2048;
13434 else if(0 == nSize % 1024) nBuf = 1024;
13435 else nBuf = 512;
13437 for( ; 0==rc && nPos<nSize; nPos += nBuf ){
13438 rc = pFile->pMethods->xRead(pFile, buf, nBuf, nPos);
13439 if(SQLITE_IOERR_SHORT_READ == rc){
13440 rc = (nPos + nBuf) < nSize ? rc : 0/*assume EOF*/;
13442 if( 0==rc ) rc = xCallback(buf, nBuf);
13444 return rc;
13448 ** Trivial exportable function for emscripten. It processes zSql as if
13449 ** it were input to the sqlite3 shell and redirects all output to the
13450 ** wasm binding. fiddle_main() must have been called before this
13451 ** is called, or results are undefined.
13453 void fiddle_exec(const char * zSql){
13454 if(zSql && *zSql){
13455 if('.'==*zSql) puts(zSql);
13456 shellState.wasm.zInput = zSql;
13457 shellState.wasm.zPos = zSql;
13458 process_input(&shellState);
13459 shellState.wasm.zInput = shellState.wasm.zPos = 0;
13462 #endif /* SQLITE_SHELL_FIDDLE */