4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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
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)
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
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
52 ** Warning pragmas copied from msvc.h in the core.
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
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
87 # define _LARGEFILE_SOURCE 1
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
104 typedef sqlite3_int64 i64
;
105 typedef sqlite3_uint64 u64
;
106 typedef unsigned char u8
;
107 #if SQLITE_USER_AUTHENTICATION
108 # include "sqlite3userauth.h"
113 #if !defined(_WIN32) && !defined(WIN32)
115 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
119 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
122 # define GETPID getpid
123 # if defined(__MINGW32__)
124 # define DIRENT dirent
126 # define S_ISLNK(mode) (0)
130 # define GETPID (int)GetCurrentProcessId
132 #include <sys/types.h>
133 #include <sys/stat.h>
136 # include <readline/readline.h>
137 # include <readline/history.h>
141 # include <editline/readline.h>
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)
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)
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
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));
175 # define deliberate_fall_through
179 #if defined(_WIN32) || defined(WIN32)
181 # define SQLITE_OMIT_POPEN 1
185 # define isatty(h) _isatty(h)
187 # define access(f,m) _access((f),(m))
190 # define unlink _unlink
193 # define strdup _strdup
196 # define popen _popen
198 # define pclose _pclose
201 /* Make sure isatty() has a prototype. */
202 extern int isatty(int);
204 # if !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
205 /* popen and pclose are not C89 functions and so are
206 ** sometimes omitted from the <stdio.h> header */
207 extern FILE *popen(const char*,const char*);
208 extern int pclose(FILE*);
210 # define SQLITE_OMIT_POPEN 1
214 #if defined(_WIN32_WCE)
215 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
216 * thus we always assume that we have a console. That can be
217 * overridden with the -batch command line option.
222 /* ctype macros that work with signed characters */
223 #define IsSpace(X) isspace((unsigned char)X)
224 #define IsDigit(X) isdigit((unsigned char)X)
225 #define ToLower(X) (char)tolower((unsigned char)X)
227 #if defined(_WIN32) || defined(WIN32)
231 #define WIN32_LEAN_AND_MEAN
234 /* string conversion routines only needed on Win32 */
235 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR
);
236 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
237 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
238 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char *zText
);
241 /* On Windows, we normally run with output mode of TEXT so that \n characters
242 ** are automatically translated into \r\n. However, this behavior needs
243 ** to be disabled in some cases (ex: when generating CSV output and when
244 ** rendering quoted strings that contain \n characters). The following
245 ** routines take care of that.
247 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
248 static void setBinaryMode(FILE *file
, int isOutput
){
249 if( isOutput
) fflush(file
);
250 _setmode(_fileno(file
), _O_BINARY
);
252 static void setTextMode(FILE *file
, int isOutput
){
253 if( isOutput
) fflush(file
);
254 _setmode(_fileno(file
), _O_TEXT
);
257 # define setBinaryMode(X,Y)
258 # define setTextMode(X,Y)
261 /* True if the timer is enabled */
262 static int enableTimer
= 0;
264 /* A version of strcmp() that works with NULL values */
265 static int cli_strcmp(const char *a
, const char *b
){
270 static int cli_strncmp(const char *a
, const char *b
, size_t n
){
273 return strncmp(a
,b
,n
);
276 /* Return the current wall-clock time */
277 static sqlite3_int64
timeOfDay(void){
278 static sqlite3_vfs
*clockVfs
= 0;
280 if( clockVfs
==0 ) clockVfs
= sqlite3_vfs_find(0);
281 if( clockVfs
==0 ) return 0; /* Never actually happens */
282 if( clockVfs
->iVersion
>=2 && clockVfs
->xCurrentTimeInt64
!=0 ){
283 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
286 clockVfs
->xCurrentTime(clockVfs
, &r
);
287 t
= (sqlite3_int64
)(r
*86400000.0);
292 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
293 #include <sys/time.h>
294 #include <sys/resource.h>
296 /* VxWorks does not support getrusage() as far as we can determine */
297 #if defined(_WRS_KERNEL) || defined(__RTP__)
299 struct timeval ru_utime
; /* user CPU time used */
300 struct timeval ru_stime
; /* system CPU time used */
302 #define getrusage(A,B) memset(B,0,sizeof(*B))
305 /* Saved resource information for the beginning of an operation */
306 static struct rusage sBegin
; /* CPU time at start */
307 static sqlite3_int64 iBegin
; /* Wall-clock time at start */
310 ** Begin timing an operation
312 static void beginTimer(void){
314 getrusage(RUSAGE_SELF
, &sBegin
);
315 iBegin
= timeOfDay();
319 /* Return the difference of two time_structs in seconds */
320 static double timeDiff(struct timeval
*pStart
, struct timeval
*pEnd
){
321 return (pEnd
->tv_usec
- pStart
->tv_usec
)*0.000001 +
322 (double)(pEnd
->tv_sec
- pStart
->tv_sec
);
326 ** Print the timing results.
328 static void endTimer(void){
330 sqlite3_int64 iEnd
= timeOfDay();
332 getrusage(RUSAGE_SELF
, &sEnd
);
333 printf("Run Time: real %.3f user %f sys %f\n",
334 (iEnd
- iBegin
)*0.001,
335 timeDiff(&sBegin
.ru_utime
, &sEnd
.ru_utime
),
336 timeDiff(&sBegin
.ru_stime
, &sEnd
.ru_stime
));
340 #define BEGIN_TIMER beginTimer()
341 #define END_TIMER endTimer()
344 #elif (defined(_WIN32) || defined(WIN32))
346 /* Saved resource information for the beginning of an operation */
347 static HANDLE hProcess
;
348 static FILETIME ftKernelBegin
;
349 static FILETIME ftUserBegin
;
350 static sqlite3_int64 ftWallBegin
;
351 typedef BOOL (WINAPI
*GETPROCTIMES
)(HANDLE
, LPFILETIME
, LPFILETIME
,
352 LPFILETIME
, LPFILETIME
);
353 static GETPROCTIMES getProcessTimesAddr
= NULL
;
356 ** Check to see if we have timer support. Return 1 if necessary
357 ** support found (or found previously).
359 static int hasTimer(void){
360 if( getProcessTimesAddr
){
364 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
365 ** versions. See if the version we are running on has it, and if it
366 ** does, save off a pointer to it and the current process handle.
368 hProcess
= GetCurrentProcess();
370 HINSTANCE hinstLib
= LoadLibrary(TEXT("Kernel32.dll"));
371 if( NULL
!= hinstLib
){
372 getProcessTimesAddr
=
373 (GETPROCTIMES
) GetProcAddress(hinstLib
, "GetProcessTimes");
374 if( NULL
!= getProcessTimesAddr
){
377 FreeLibrary(hinstLib
);
386 ** Begin timing an operation
388 static void beginTimer(void){
389 if( enableTimer
&& getProcessTimesAddr
){
390 FILETIME ftCreation
, ftExit
;
391 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,
392 &ftKernelBegin
,&ftUserBegin
);
393 ftWallBegin
= timeOfDay();
397 /* Return the difference of two FILETIME structs in seconds */
398 static double timeDiff(FILETIME
*pStart
, FILETIME
*pEnd
){
399 sqlite_int64 i64Start
= *((sqlite_int64
*) pStart
);
400 sqlite_int64 i64End
= *((sqlite_int64
*) pEnd
);
401 return (double) ((i64End
- i64Start
) / 10000000.0);
405 ** Print the timing results.
407 static void endTimer(void){
408 if( enableTimer
&& getProcessTimesAddr
){
409 FILETIME ftCreation
, ftExit
, ftKernelEnd
, ftUserEnd
;
410 sqlite3_int64 ftWallEnd
= timeOfDay();
411 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,&ftKernelEnd
,&ftUserEnd
);
412 printf("Run Time: real %.3f user %f sys %f\n",
413 (ftWallEnd
- ftWallBegin
)*0.001,
414 timeDiff(&ftUserBegin
, &ftUserEnd
),
415 timeDiff(&ftKernelBegin
, &ftKernelEnd
));
419 #define BEGIN_TIMER beginTimer()
420 #define END_TIMER endTimer()
421 #define HAS_TIMER hasTimer()
430 ** Used to prevent warnings about unused parameters
432 #define UNUSED_PARAMETER(x) (void)(x)
435 ** Number of elements in an array
437 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
440 ** If the following flag is set, then command execution stops
441 ** at an error if we are not interactive.
443 static int bail_on_error
= 0;
446 ** Treat stdin as an interactive input if the following variable
447 ** is true. Otherwise, assume stdin is connected to a file or pipe.
449 static int stdin_is_interactive
= 1;
451 #if (defined(_WIN32) || defined(WIN32)) && SHELL_USE_LOCAL_GETLINE \
452 && !defined(SHELL_OMIT_WIN_UTF8)
453 # define SHELL_WIN_UTF8_OPT 1
455 # define SHELL_WIN_UTF8_OPT 0
458 #if SHELL_WIN_UTF8_OPT
460 ** Setup console for UTF-8 input/output when following variable true.
462 static int console_utf8
= 0;
466 ** On Windows systems we have to know if standard output is a console
467 ** in order to translate UTF-8 into MBCS. The following variable is
468 ** true if translation is required.
470 static int stdout_is_console
= 1;
473 ** The following is the open SQLite database. We make a pointer
474 ** to this database a static variable so that it can be accessed
475 ** by the SIGINT handler to interrupt database processing.
477 static sqlite3
*globalDb
= 0;
480 ** True if an interrupt (Control-C) has been received.
482 static volatile int seenInterrupt
= 0;
485 ** This is the name of our program. It is set in main(), used
486 ** in a number of other places, mostly for error messages.
491 ** Prompt strings. Initialized in main. Settable with
492 ** .prompt main continue
494 #define PROMPT_LEN_MAX 20
495 /* First line prompt. default: "sqlite> " */
496 static char mainPrompt
[PROMPT_LEN_MAX
];
497 /* Continuation prompt. default: " ...> " */
498 static char continuePrompt
[PROMPT_LEN_MAX
];
500 /* This is variant of the standard-library strncpy() routine with the
501 ** one change that the destination string is always zero-terminated, even
502 ** if there is no zero-terminator in the first n-1 characters of the source
505 static char *shell_strncpy(char *dest
, const char *src
, size_t n
){
507 for(i
=0; i
<n
-1 && src
[i
]!=0; i
++) dest
[i
] = src
[i
];
513 ** Optionally disable dynamic continuation prompt.
514 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
515 ** or open parentheses level if non-zero, or continuation prompt as set.
516 ** This facility interacts with the scanner and process_input() where the
517 ** below 5 macros are used.
519 #ifdef SQLITE_OMIT_DYNAPROMPT
520 # define CONTINUATION_PROMPT continuePrompt
521 # define CONTINUE_PROMPT_RESET
522 # define CONTINUE_PROMPT_AWAITS(p,s)
523 # define CONTINUE_PROMPT_AWAITC(p,c)
524 # define CONTINUE_PAREN_INCR(p,n)
525 # define CONTINUE_PROMPT_PSTATE 0
526 typedef void *t_NoDynaPrompt
;
527 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
529 # define CONTINUATION_PROMPT dynamicContinuePrompt()
530 # define CONTINUE_PROMPT_RESET \
531 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
532 # define CONTINUE_PROMPT_AWAITS(p,s) \
533 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
534 # define CONTINUE_PROMPT_AWAITC(p,c) \
535 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
536 # define CONTINUE_PAREN_INCR(p,n) \
537 if(p && stdin_is_interactive) (trackParenLevel(p,n))
538 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
539 typedef struct DynaPrompt
*t_DynaPromptRef
;
540 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
542 static struct DynaPrompt
{
543 char dynamicPrompt
[PROMPT_LEN_MAX
];
546 char *zScannerAwaits
;
547 } dynPrompt
= { {0}, {0}, 0, 0 };
549 /* Record parenthesis nesting level change, or force level to 0. */
550 static void trackParenLevel(struct DynaPrompt
*p
, int ni
){
551 p
->inParenLevel
+= ni
;
552 if( ni
==0 ) p
->inParenLevel
= 0;
553 p
->zScannerAwaits
= 0;
556 /* Record that a lexeme is opened, or closed with args==0. */
557 static void setLexemeOpen(struct DynaPrompt
*p
, char *s
, char c
){
559 p
->zScannerAwaits
= s
;
563 p
->zScannerAwaits
= p
->acAwait
;
567 /* Upon demand, derive the continuation prompt to display. */
568 static char *dynamicContinuePrompt(void){
569 if( continuePrompt
[0]==0
570 || (dynPrompt
.zScannerAwaits
==0 && dynPrompt
.inParenLevel
== 0) ){
571 return continuePrompt
;
573 if( dynPrompt
.zScannerAwaits
){
574 size_t ncp
= strlen(continuePrompt
);
575 size_t ndp
= strlen(dynPrompt
.zScannerAwaits
);
576 if( ndp
> ncp
-3 ) return continuePrompt
;
577 strcpy(dynPrompt
.dynamicPrompt
, dynPrompt
.zScannerAwaits
);
578 while( ndp
<3 ) dynPrompt
.dynamicPrompt
[ndp
++] = ' ';
579 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3,
582 if( dynPrompt
.inParenLevel
>9 ){
583 shell_strncpy(dynPrompt
.dynamicPrompt
, "(..", 4);
584 }else if( dynPrompt
.inParenLevel
<0 ){
585 shell_strncpy(dynPrompt
.dynamicPrompt
, ")x!", 4);
587 shell_strncpy(dynPrompt
.dynamicPrompt
, "(x.", 4);
588 dynPrompt
.dynamicPrompt
[2] = (char)('0'+dynPrompt
.inParenLevel
);
590 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3, PROMPT_LEN_MAX
-4);
593 return dynPrompt
.dynamicPrompt
;
595 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
597 #if SHELL_WIN_UTF8_OPT
598 /* Following struct is used for -utf8 operation. */
599 static struct ConsoleState
{
600 int stdinEof
; /* EOF has been seen on console input */
601 int infsMode
; /* Input file stream mode upon shell start */
602 UINT inCodePage
; /* Input code page upon shell start */
603 UINT outCodePage
; /* Output code page upon shell start */
604 HANDLE hConsoleIn
; /* Console input handle */
605 DWORD consoleMode
; /* Console mode upon shell start */
606 } conState
= { 0, 0, 0, 0, INVALID_HANDLE_VALUE
, 0 };
608 #ifndef _O_U16TEXT /* For build environments lacking this constant: */
609 # define _O_U16TEXT 0x20000
613 ** Prepare console, (if known to be a WIN32 console), for UTF-8
614 ** input (from either typing or suitable paste operations) and for
615 ** UTF-8 rendering. This may "fail" with a message to stderr, where
616 ** the preparation is not done and common "code page" issues occur.
618 static void console_prepare(void){
619 HANDLE hCI
= GetStdHandle(STD_INPUT_HANDLE
);
620 DWORD consoleMode
= 0;
621 if( isatty(0) && GetFileType(hCI
)==FILE_TYPE_CHAR
622 && GetConsoleMode( hCI
, &consoleMode
) ){
623 if( !IsValidCodePage(CP_UTF8
) ){
624 fprintf(stderr
, "Cannot use UTF-8 code page.\n");
628 conState
.hConsoleIn
= hCI
;
629 conState
.consoleMode
= consoleMode
;
630 conState
.inCodePage
= GetConsoleCP();
631 conState
.outCodePage
= GetConsoleOutputCP();
632 SetConsoleCP(CP_UTF8
);
633 SetConsoleOutputCP(CP_UTF8
);
634 consoleMode
|= ENABLE_LINE_INPUT
| ENABLE_PROCESSED_INPUT
;
635 SetConsoleMode(conState
.hConsoleIn
, consoleMode
);
636 conState
.infsMode
= _setmode(_fileno(stdin
), _O_U16TEXT
);
644 ** Undo the effects of console_prepare(), if any.
646 static void SQLITE_CDECL
console_restore(void){
647 if( console_utf8
&& conState
.inCodePage
!=0
648 && conState
.hConsoleIn
!=INVALID_HANDLE_VALUE
){
649 _setmode(_fileno(stdin
), conState
.infsMode
);
650 SetConsoleCP(conState
.inCodePage
);
651 SetConsoleOutputCP(conState
.outCodePage
);
652 SetConsoleMode(conState
.hConsoleIn
, conState
.consoleMode
);
653 /* Avoid multiple calls. */
654 conState
.hConsoleIn
= INVALID_HANDLE_VALUE
;
655 conState
.consoleMode
= 0;
661 ** Collect input like fgets(...) with special provisions for input
662 ** from the Windows console to get around its strange coding issues.
663 ** Defers to plain fgets() when input is not interactive or when the
664 ** startup option, -utf8, has not been provided or taken effect.
666 static char* utf8_fgets(char *buf
, int ncmax
, FILE *fin
){
667 if( fin
==0 ) fin
= stdin
;
668 if( fin
==stdin
&& stdin_is_interactive
&& console_utf8
){
669 # define SQLITE_IALIM 150
670 wchar_t wbuf
[SQLITE_IALIM
];
673 if( ncmax
==0 || conState
.stdinEof
) return 0;
675 while( noc
<ncmax
-7-1 && !lend
){
676 /* There is room for at least 2 more characters and a 0-terminator. */
677 int na
= (ncmax
> SQLITE_IALIM
*4+1 + noc
)
678 ? SQLITE_IALIM
: (ncmax
-1 - noc
)/4;
681 BOOL bRC
= ReadConsoleW(conState
.hConsoleIn
, wbuf
, na
, &nbr
, 0);
682 if( !bRC
|| (noc
==0 && nbr
==0) ) return 0;
684 int nmb
= WideCharToMultiByte(CP_UTF8
,WC_COMPOSITECHECK
|WC_DEFAULTCHAR
,
686 if( nmb
!=0 && noc
+nmb
<= ncmax
){
688 nmb
= WideCharToMultiByte(CP_UTF8
,WC_COMPOSITECHECK
|WC_DEFAULTCHAR
,
689 wbuf
,nbr
,buf
+noc
,nmb
,0,0);
691 /* Fixup line-ends as coded by Windows for CR (or "Enter".)*/
693 if( buf
[noc
-1]=='\n' ){
695 if( noc
> 1 && buf
[noc
-2]=='\r' ){
701 /* Check for ^Z (anywhere in line) too. */
703 if( buf
[iseg
]==0x1a ){
704 conState
.stdinEof
= 1;
705 noc
= iseg
; /* Chop ^Z and anything following. */
710 }else break; /* Drop apparent garbage in. (Could assert.) */
713 /* If got nothing, (after ^Z chop), must be at end-of-file. */
714 if( noc
== 0 ) return 0;
718 return fgets(buf
, ncmax
, fin
);
722 # define fgets(b,n,f) utf8_fgets(b,n,f)
723 #endif /* SHELL_WIN_UTF8_OPT */
726 ** Render output like fprintf(). Except, if the output is going to the
727 ** console and if this is running on a Windows machine, and if the -utf8
728 ** option is unavailable or (available and inactive), translate the
729 ** output from UTF-8 into MBCS for output through 8-bit stdout stream.
730 ** (With -utf8 active, no translation is needed and must not be done.)
732 #if defined(_WIN32) || defined(WIN32)
733 void utf8_printf(FILE *out
, const char *zFormat
, ...){
735 va_start(ap
, zFormat
);
736 if( stdout_is_console
&& (out
==stdout
|| out
==stderr
)
737 # if SHELL_WIN_UTF8_OPT
741 char *z1
= sqlite3_vmprintf(zFormat
, ap
);
742 char *z2
= sqlite3_win32_utf8_to_mbcs_v2(z1
, 0);
747 vfprintf(out
, zFormat
, ap
);
751 #elif !defined(utf8_printf)
752 # define utf8_printf fprintf
756 ** Render output like fprintf(). This should not be used on anything that
757 ** includes string formatting (e.g. "%s").
759 #if !defined(raw_printf)
760 # define raw_printf fprintf
763 /* Indicate out-of-memory and exit. */
764 static void shell_out_of_memory(void){
765 raw_printf(stderr
,"Error: out of memory\n");
769 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
770 ** out-of-memory error.
772 static void shell_check_oom(const void *p
){
773 if( p
==0 ) shell_out_of_memory();
777 ** Write I/O traces to the following stream.
779 #ifdef SQLITE_ENABLE_IOTRACE
780 static FILE *iotrace
= 0;
784 ** This routine works like printf in that its first argument is a
785 ** format string and subsequent arguments are values to be substituted
786 ** in place of % fields. The result of formatting this string
787 ** is written to iotrace.
789 #ifdef SQLITE_ENABLE_IOTRACE
790 static void SQLITE_CDECL
iotracePrintf(const char *zFormat
, ...){
793 if( iotrace
==0 ) return;
794 va_start(ap
, zFormat
);
795 z
= sqlite3_vmprintf(zFormat
, ap
);
797 utf8_printf(iotrace
, "%s", z
);
803 ** Output string zUtf to stream pOut as w characters. If w is negative,
804 ** then right-justify the text. W is the width in UTF-8 characters, not
805 ** in bytes. This is different from the %*.*s specification in printf
806 ** since with %*.*s the width is measured in bytes, not characters.
808 static void utf8_width_print(FILE *pOut
, int w
, const char *zUtf
){
811 int aw
= w
<0 ? -w
: w
;
812 if( zUtf
==0 ) zUtf
= "";
813 for(i
=n
=0; zUtf
[i
]; i
++){
814 if( (zUtf
[i
]&0xc0)!=0x80 ){
817 do{ i
++; }while( (zUtf
[i
]&0xc0)==0x80 );
823 utf8_printf(pOut
, "%.*s", i
, zUtf
);
825 utf8_printf(pOut
, "%*s%s", aw
-n
, "", zUtf
);
827 utf8_printf(pOut
, "%s%*s", zUtf
, aw
-n
, "");
833 ** Determines if a string is a number of not.
835 static int isNumber(const char *z
, int *realnum
){
836 if( *z
=='-' || *z
=='+' ) z
++;
841 if( realnum
) *realnum
= 0;
842 while( IsDigit(*z
) ){ z
++; }
845 if( !IsDigit(*z
) ) return 0;
846 while( IsDigit(*z
) ){ z
++; }
847 if( realnum
) *realnum
= 1;
849 if( *z
=='e' || *z
=='E' ){
851 if( *z
=='+' || *z
=='-' ) z
++;
852 if( !IsDigit(*z
) ) return 0;
853 while( IsDigit(*z
) ){ z
++; }
854 if( realnum
) *realnum
= 1;
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
){
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
){
876 if( (0xc0&*(z
++))!=0x80 ) 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
){
888 struct _stat x
= {0};
889 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
890 /* On Windows, open first, then check the stream nature. This order
891 ** is necessary because _stat() and sibs, when checking a named pipe,
892 ** effectively break the pipe as its supplier sees it. */
893 FILE *rv
= fopen(zFile
, "rb");
894 if( rv
==0 ) return 0;
895 if( _fstat(_fileno(rv
), &x
) != 0
896 || !STAT_CHR_SRC(x
.st_mode
)){
903 int rc
= stat(zFile
, &x
);
904 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
905 if( rc
!=0 ) return 0;
906 if( STAT_CHR_SRC(x
.st_mode
) ){
907 return fopen(zFile
, "rb");
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()
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;
930 nLine
= nLine
*2 + 100;
931 zLine
= realloc(zLine
, nLine
);
932 shell_check_oom(zLine
);
934 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
942 while( zLine
[n
] ) n
++;
943 if( n
>0 && zLine
[n
-1]=='\n' ){
945 if( n
>0 && zLine
[n
-1]=='\r' ) n
--;
950 #if defined(_WIN32) || defined(WIN32)
951 /* For interactive input on Windows systems, without -utf8,
952 ** translate the multi-byte characterset characters into UTF-8.
953 ** This is the translation that predates the -utf8 option. */
954 if( stdin_is_interactive
&& in
==stdin
955 # if SHELL_WIN_UTF8_OPT
957 # endif /* SHELL_WIN_UTF8_OPT */
959 char *zTrans
= sqlite3_win32_mbcs_to_utf8_v2(zLine
, 0);
961 i64 nTrans
= strlen(zTrans
)+1;
963 zLine
= realloc(zLine
, nTrans
);
964 shell_check_oom(zLine
);
966 memcpy(zLine
, zTrans
, nTrans
);
967 sqlite3_free(zTrans
);
970 #endif /* defined(_WIN32) || defined(WIN32) */
975 ** Retrieve a single line of input text.
977 ** If in==0 then read from standard input and prompt before each line.
978 ** If isContinuation is true, then a continuation prompt is appropriate.
979 ** If isContinuation is zero, then the main prompt should be used.
981 ** If zPrior is not NULL then it is a buffer from a prior call to this
982 ** routine that can be reused.
984 ** The result is stored in space obtained from malloc() and must either
985 ** be freed by the caller or else passed back into this routine via the
986 ** zPrior argument for reuse.
988 #ifndef SQLITE_SHELL_FIDDLE
989 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
993 zResult
= local_getline(zPrior
, in
);
995 zPrompt
= isContinuation
? CONTINUATION_PROMPT
: mainPrompt
;
996 #if SHELL_USE_LOCAL_GETLINE
997 printf("%s", zPrompt
);
1000 zResult
= local_getline(zPrior
, stdin
);
1002 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1003 if( zResult
==0 ) sqlite3_sleep(50);
1004 }while( zResult
==0 && seenInterrupt
>0 );
1007 zResult
= shell_readline(zPrompt
);
1008 while( zResult
==0 ){
1009 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1011 if( seenInterrupt
==0 ) break;
1012 zResult
= shell_readline("");
1014 /* BEGIN SQLCIPHER */
1015 #ifdef SQLITE_HAS_CODEC
1016 /* Simplistic filtering of input lines to prevent PRAGKA key and
1017 PRAGMA rekey statements from being stored in readline history.
1018 Note that this will only prevent single line statements, but that
1019 will be sufficient for common cases. */
1020 if(zResult
&& *zResult
&& (
1021 sqlite3_strlike("%pragma%key%=%", zResult
, 0)==0 ||
1022 sqlite3_strlike("%attach%database%as%key%", zResult
, 0)==0
1027 if( zResult
&& *zResult
) shell_add_history(zResult
);
1032 #endif /* !SQLITE_SHELL_FIDDLE */
1035 ** Return the value of a hexadecimal digit. Return -1 if the input
1036 ** is not a hex digit.
1038 static int hexDigitValue(char c
){
1039 if( c
>='0' && c
<='9' ) return c
- '0';
1040 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
1041 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
1046 ** Interpret zArg as an integer value, possibly with suffixes.
1048 static sqlite3_int64
integerValue(const char *zArg
){
1049 sqlite3_int64 v
= 0;
1050 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
1052 { "MiB", 1024*1024 },
1053 { "GiB", 1024*1024*1024 },
1056 { "GB", 1000000000 },
1059 { "G", 1000000000 },
1066 }else if( zArg
[0]=='+' ){
1069 if( zArg
[0]=='0' && zArg
[1]=='x' ){
1072 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
1077 while( IsDigit(zArg
[0]) ){
1078 v
= v
*10 + zArg
[0] - '0';
1082 for(i
=0; i
<ArraySize(aMult
); i
++){
1083 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
1084 v
*= aMult
[i
].iMult
;
1088 return isNeg
? -v
: v
;
1092 ** A variable length string to which one can append text.
1094 typedef struct ShellText ShellText
;
1102 ** Initialize and destroy a ShellText object
1104 static void initText(ShellText
*p
){
1105 memset(p
, 0, sizeof(*p
));
1107 static void freeText(ShellText
*p
){
1112 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1113 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1114 ** added to zIn, and the result returned in memory obtained from malloc().
1115 ** zIn, if it was not NULL, is freed.
1117 ** If the third argument, quote, is not '\0', then it is used as a
1118 ** quote character for zAppend.
1120 static void appendText(ShellText
*p
, const char *zAppend
, char quote
){
1123 i64 nAppend
= strlen30(zAppend
);
1125 len
= nAppend
+p
->n
+1;
1128 for(i
=0; i
<nAppend
; i
++){
1129 if( zAppend
[i
]==quote
) len
++;
1133 if( p
->z
==0 || p
->n
+len
>=p
->nAlloc
){
1134 p
->nAlloc
= p
->nAlloc
*2 + len
+ 20;
1135 p
->z
= realloc(p
->z
, p
->nAlloc
);
1136 shell_check_oom(p
->z
);
1140 char *zCsr
= p
->z
+p
->n
;
1142 for(i
=0; i
<nAppend
; i
++){
1143 *zCsr
++ = zAppend
[i
];
1144 if( zAppend
[i
]==quote
) *zCsr
++ = quote
;
1147 p
->n
= (int)(zCsr
- p
->z
);
1150 memcpy(p
->z
+p
->n
, zAppend
, nAppend
);
1157 ** Attempt to determine if identifier zName needs to be quoted, either
1158 ** because it contains non-alphanumeric characters, or because it is an
1159 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1160 ** that quoting is required.
1162 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1164 static char quoteChar(const char *zName
){
1166 if( zName
==0 ) return '"';
1167 if( !isalpha((unsigned char)zName
[0]) && zName
[0]!='_' ) return '"';
1168 for(i
=0; zName
[i
]; i
++){
1169 if( !isalnum((unsigned char)zName
[i
]) && zName
[i
]!='_' ) return '"';
1171 return sqlite3_keyword_check(zName
, i
) ? '"' : 0;
1175 ** Construct a fake object name and column list to describe the structure
1176 ** of the view, virtual table, or table valued function zSchema.zName.
1178 static char *shellFakeSchema(
1179 sqlite3
*db
, /* The database connection containing the vtab */
1180 const char *zSchema
, /* Schema of the database holding the vtab */
1181 const char *zName
/* The name of the virtual table */
1183 sqlite3_stmt
*pStmt
= 0;
1190 zSql
= sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1191 zSchema
? zSchema
: "main", zName
);
1192 shell_check_oom(zSql
);
1193 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
1197 cQuote
= quoteChar(zSchema
);
1198 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")==0 ) cQuote
= 0;
1199 appendText(&s
, zSchema
, cQuote
);
1200 appendText(&s
, ".", 0);
1202 cQuote
= quoteChar(zName
);
1203 appendText(&s
, zName
, cQuote
);
1204 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
1205 const char *zCol
= (const char*)sqlite3_column_text(pStmt
, 1);
1207 appendText(&s
, zDiv
, 0);
1209 if( zCol
==0 ) zCol
= "";
1210 cQuote
= quoteChar(zCol
);
1211 appendText(&s
, zCol
, cQuote
);
1213 appendText(&s
, ")", 0);
1214 sqlite3_finalize(pStmt
);
1223 ** SQL function: shell_module_schema(X)
1225 ** Return a fake schema for the table-valued function or eponymous virtual
1228 static void shellModuleSchema(
1229 sqlite3_context
*pCtx
,
1231 sqlite3_value
**apVal
1235 UNUSED_PARAMETER(nVal
);
1236 zName
= (const char*)sqlite3_value_text(apVal
[0]);
1237 zFake
= zName
? shellFakeSchema(sqlite3_context_db_handle(pCtx
), 0, zName
) : 0;
1239 sqlite3_result_text(pCtx
, sqlite3_mprintf("/* %s */", zFake
),
1246 ** SQL function: shell_add_schema(S,X)
1248 ** Add the schema name X to the CREATE statement in S and return the result.
1251 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1256 ** CREATE UNIQUE INDEX
1259 ** CREATE VIRTUAL TABLE
1261 ** This UDF is used by the .schema command to insert the schema name of
1262 ** attached databases into the middle of the sqlite_schema.sql field.
1264 static void shellAddSchemaName(
1265 sqlite3_context
*pCtx
,
1267 sqlite3_value
**apVal
1269 static const char *aPrefix
[] = {
1278 const char *zIn
= (const char*)sqlite3_value_text(apVal
[0]);
1279 const char *zSchema
= (const char*)sqlite3_value_text(apVal
[1]);
1280 const char *zName
= (const char*)sqlite3_value_text(apVal
[2]);
1281 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
1282 UNUSED_PARAMETER(nVal
);
1283 if( zIn
!=0 && cli_strncmp(zIn
, "CREATE ", 7)==0 ){
1284 for(i
=0; i
<ArraySize(aPrefix
); i
++){
1285 int n
= strlen30(aPrefix
[i
]);
1286 if( cli_strncmp(zIn
+7, aPrefix
[i
], n
)==0 && zIn
[n
+7]==' ' ){
1290 char cQuote
= quoteChar(zSchema
);
1291 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")!=0 ){
1292 z
= sqlite3_mprintf("%.*s \"%w\".%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1294 z
= sqlite3_mprintf("%.*s %s.%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1298 && aPrefix
[i
][0]=='V'
1299 && (zFake
= shellFakeSchema(db
, zSchema
, zName
))!=0
1302 z
= sqlite3_mprintf("%s\n/* %s */", zIn
, zFake
);
1304 z
= sqlite3_mprintf("%z\n/* %s */", z
, zFake
);
1309 sqlite3_result_text(pCtx
, z
, -1, sqlite3_free
);
1315 sqlite3_result_value(pCtx
, apVal
[0]);
1319 ** The source code for several run-time loadable extensions is inserted
1320 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1321 ** code, we need to override some macros to make the included program code
1322 ** work here in the middle of this regular program.
1324 #define SQLITE_EXTENSION_INIT1
1325 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1327 #if defined(_WIN32) && defined(_MSC_VER)
1328 INCLUDE test_windirent
.h
1329 INCLUDE test_windirent
.c
1330 #define dirent DIRENT
1332 INCLUDE
../ext
/misc
/memtrace
.c
1333 INCLUDE
../ext
/misc
/shathree
.c
1334 INCLUDE
../ext
/misc
/uint
.c
1335 INCLUDE
../ext
/misc
/decimal
.c
1336 #undef sqlite3_base_init
1337 #define sqlite3_base_init sqlite3_base64_init
1338 INCLUDE
../ext
/misc
/base64
.c
1339 #undef sqlite3_base_init
1340 #define sqlite3_base_init sqlite3_base85_init
1341 #define OMIT_BASE85_CHECKER
1342 INCLUDE
../ext
/misc
/base85
.c
1343 INCLUDE
../ext
/misc
/ieee754
.c
1344 INCLUDE
../ext
/misc
/series
.c
1345 INCLUDE
../ext
/misc
/regexp
.c
1346 #ifndef SQLITE_SHELL_FIDDLE
1347 INCLUDE
../ext
/misc
/fileio
.c
1348 INCLUDE
../ext
/misc
/completion
.c
1349 INCLUDE
../ext
/misc
/appendvfs
.c
1351 #ifdef SQLITE_HAVE_ZLIB
1352 INCLUDE
../ext
/misc
/zipfile
.c
1353 INCLUDE
../ext
/misc
/sqlar
.c
1355 INCLUDE
../ext
/expert
/sqlite3expert
.h
1356 INCLUDE
../ext
/expert
/sqlite3expert
.c
1358 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1359 #define SQLITE_SHELL_HAVE_RECOVER 1
1361 #define SQLITE_SHELL_HAVE_RECOVER 0
1363 #if SQLITE_SHELL_HAVE_RECOVER
1364 INCLUDE
../ext
/recover
/sqlite3recover
.h
1365 # ifndef SQLITE_HAVE_SQLITE3R
1366 INCLUDE
../ext
/recover
/dbdata
.c
1367 INCLUDE
../ext
/recover
/sqlite3recover
.c
1368 # endif /* SQLITE_HAVE_SQLITE3R */
1370 #ifdef SQLITE_SHELL_EXTSRC
1371 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1374 #if defined(SQLITE_ENABLE_SESSION)
1376 ** State information for a single open session
1378 typedef struct OpenSession OpenSession
;
1379 struct OpenSession
{
1380 char *zName
; /* Symbolic name for this session */
1381 int nFilter
; /* Number of xFilter rejection GLOB patterns */
1382 char **azFilter
; /* Array of xFilter rejection GLOB patterns */
1383 sqlite3_session
*p
; /* The open session */
1387 typedef struct ExpertInfo ExpertInfo
;
1389 sqlite3expert
*pExpert
;
1393 /* A single line in the EQP output */
1394 typedef struct EQPGraphRow EQPGraphRow
;
1395 struct EQPGraphRow
{
1396 int iEqpId
; /* ID for this row */
1397 int iParentId
; /* ID of the parent row */
1398 EQPGraphRow
*pNext
; /* Next row in sequence */
1399 char zText
[1]; /* Text to display for this row */
1402 /* All EQP output is collected into an instance of the following */
1403 typedef struct EQPGraph EQPGraph
;
1405 EQPGraphRow
*pRow
; /* Linked list of all rows of the EQP output */
1406 EQPGraphRow
*pLast
; /* Last element of the pRow list */
1407 char zPrefix
[100]; /* Graph prefix */
1410 /* Parameters affecting columnar mode result display (defaulting together) */
1411 typedef struct ColModeOpts
{
1412 int iWrap
; /* In columnar modes, wrap lines reaching this limit */
1413 u8 bQuote
; /* Quote results for .mode box and table */
1414 u8 bWordWrap
; /* In columnar modes, wrap at word boundaries */
1416 #define ColModeOpts_default { 60, 0, 0 }
1417 #define ColModeOpts_default_qbox { 60, 1, 0 }
1420 ** State information about the database connection is contained in an
1421 ** instance of the following structure.
1423 typedef struct ShellState ShellState
;
1425 sqlite3
*db
; /* The database */
1426 u8 autoExplain
; /* Automatically turn on .explain mode */
1427 u8 autoEQP
; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1428 u8 autoEQPtest
; /* autoEQP is in test mode */
1429 u8 autoEQPtrace
; /* autoEQP is in trace mode */
1430 u8 scanstatsOn
; /* True to display scan stats before each finalize */
1431 u8 openMode
; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1432 u8 doXdgOpen
; /* Invoke start/open/xdg-open in output_reset() */
1433 u8 nEqpLevel
; /* Depth of the EQP output graph */
1434 u8 eTraceType
; /* SHELL_TRACE_* value for type of trace */
1435 u8 bSafeMode
; /* True to prohibit unsafe operations */
1436 u8 bSafeModePersist
; /* The long-term value of bSafeMode */
1437 ColModeOpts cmOpts
; /* Option values affecting columnar mode output */
1438 unsigned statsOn
; /* True to display memory stats before each finalize */
1439 unsigned mEqpLines
; /* Mask of veritical lines in the EQP output graph */
1440 int inputNesting
; /* Track nesting level of .read and other redirects */
1441 int outCount
; /* Revert to stdout when reaching zero */
1442 int cnt
; /* Number of records displayed so far */
1443 int lineno
; /* Line number of last line read from in */
1444 int openFlags
; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
1445 FILE *in
; /* Read commands from this stream */
1446 FILE *out
; /* Write results here */
1447 FILE *traceOut
; /* Output for sqlite3_trace() */
1448 int nErr
; /* Number of errors seen */
1449 int mode
; /* An output mode setting */
1450 int modePrior
; /* Saved mode */
1451 int cMode
; /* temporary output mode for the current query */
1452 int normalMode
; /* Output mode before ".explain on" */
1453 int writableSchema
; /* True if PRAGMA writable_schema=ON */
1454 int showHeader
; /* True to show column names in List or Column mode */
1455 int nCheck
; /* Number of ".check" commands run */
1456 unsigned nProgress
; /* Number of progress callbacks encountered */
1457 unsigned mxProgress
; /* Maximum progress callbacks before failing */
1458 unsigned flgProgress
; /* Flags for the progress callback */
1459 unsigned shellFlgs
; /* Various flags */
1460 unsigned priorShFlgs
; /* Saved copy of flags */
1461 sqlite3_int64 szMax
; /* --maxsize argument to .open */
1462 char *zDestTable
; /* Name of destination table when MODE_Insert */
1463 char *zTempFile
; /* Temporary file that might need deleting */
1464 char zTestcase
[30]; /* Name of current test case */
1465 char colSeparator
[20]; /* Column separator character for several modes */
1466 char rowSeparator
[20]; /* Row separator character for MODE_Ascii */
1467 char colSepPrior
[20]; /* Saved column separator */
1468 char rowSepPrior
[20]; /* Saved row separator */
1469 int *colWidth
; /* Requested width of each column in columnar modes */
1470 int *actualWidth
; /* Actual width of each column */
1471 int nWidth
; /* Number of slots in colWidth[] and actualWidth[] */
1472 char nullValue
[20]; /* The text to print when a NULL comes back from
1474 char outfile
[FILENAME_MAX
]; /* Filename for *out */
1475 sqlite3_stmt
*pStmt
; /* Current statement if any. */
1476 FILE *pLog
; /* Write log output here */
1477 struct AuxDb
{ /* Storage space for auxiliary database connections */
1478 sqlite3
*db
; /* Connection pointer */
1479 const char *zDbFilename
; /* Filename used to open the connection */
1480 char *zFreeOnClose
; /* Free this memory allocation on close */
1481 #if defined(SQLITE_ENABLE_SESSION)
1482 int nSession
; /* Number of active sessions */
1483 OpenSession aSession
[4]; /* Array of sessions. [0] is in focus. */
1485 } aAuxDb
[5], /* Array of all database connections */
1486 *pAuxDb
; /* Currently active database connection */
1487 int *aiIndent
; /* Array of indents used in MODE_Explain */
1488 int nIndent
; /* Size of array aiIndent[] */
1489 int iIndent
; /* Index of current op in aiIndent[] */
1490 char *zNonce
; /* Nonce for temporary safe-mode excapes */
1491 EQPGraph sGraph
; /* Information for the graphical EXPLAIN QUERY PLAN */
1492 ExpertInfo expert
; /* Valid if previous command was ".expert OPT..." */
1493 #ifdef SQLITE_SHELL_FIDDLE
1495 const char * zInput
; /* Input string from wasm/JS proxy */
1496 const char * zPos
; /* Cursor pos into zInput */
1497 const char * zDefaultDbName
; /* Default name for db file */
1502 #ifdef SQLITE_SHELL_FIDDLE
1503 static ShellState shellState
;
1507 /* Allowed values for ShellState.autoEQP
1509 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1510 #define AUTOEQP_on 1 /* Automatic EQP is on */
1511 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1512 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1514 /* Allowed values for ShellState.openMode
1516 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1517 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1518 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1519 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1520 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1521 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
1522 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
1524 /* Allowed values for ShellState.eTraceType
1526 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1527 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1528 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1530 /* Bits in the ShellState.flgProgress variable */
1531 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1532 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
1533 ** callback limit is reached, and for each
1534 ** top-level SQL statement */
1535 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
1538 ** These are the allowed shellFlgs values
1540 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1541 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1542 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1543 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1544 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1545 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1546 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
1547 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
1548 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
1549 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
1550 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
1553 ** Macros for testing and setting shellFlgs
1555 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1556 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1557 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1560 ** These are the allowed modes.
1562 #define MODE_Line 0 /* One column per line. Blank line between records */
1563 #define MODE_Column 1 /* One record per line in neat columns */
1564 #define MODE_List 2 /* One record per line with a separator */
1565 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1566 #define MODE_Html 4 /* Generate an XHTML table */
1567 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1568 #define MODE_Quote 6 /* Quote values as for SQL */
1569 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1570 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1571 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1572 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1573 #define MODE_Pretty 11 /* Pretty-print schemas */
1574 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1575 #define MODE_Json 13 /* Output JSON */
1576 #define MODE_Markdown 14 /* Markdown formatting */
1577 #define MODE_Table 15 /* MySQL-style table formatting */
1578 #define MODE_Box 16 /* Unicode box-drawing characters */
1579 #define MODE_Count 17 /* Output only a count of the rows of output */
1580 #define MODE_Off 18 /* No query output shown */
1582 static const char *modeDescr
[] = {
1605 ** These are the column/row/line separators used by the various
1606 ** import/export modes.
1608 #define SEP_Column "|"
1609 #define SEP_Row "\n"
1610 #define SEP_Tab "\t"
1611 #define SEP_Space " "
1612 #define SEP_Comma ","
1613 #define SEP_CrLf "\r\n"
1614 #define SEP_Unit "\x1F"
1615 #define SEP_Record "\x1E"
1618 ** Limit input nesting via .read or any other input redirect.
1619 ** It's not too expensive, so a generous allowance can be made.
1621 #define MAX_INPUT_NESTING 25
1624 ** A callback for the sqlite3_log() interface.
1626 static void shellLog(void *pArg
, int iErrCode
, const char *zMsg
){
1627 ShellState
*p
= (ShellState
*)pArg
;
1628 if( p
->pLog
==0 ) return;
1629 utf8_printf(p
->pLog
, "(%d) %s\n", iErrCode
, zMsg
);
1634 ** SQL function: shell_putsnl(X)
1636 ** Write the text X to the screen (or whatever output is being directed)
1637 ** adding a newline at the end, and then return X.
1639 static void shellPutsFunc(
1640 sqlite3_context
*pCtx
,
1642 sqlite3_value
**apVal
1644 ShellState
*p
= (ShellState
*)sqlite3_user_data(pCtx
);
1646 utf8_printf(p
->out
, "%s\n", sqlite3_value_text(apVal
[0]));
1647 sqlite3_result_value(pCtx
, apVal
[0]);
1651 ** If in safe mode, print an error message described by the arguments
1652 ** and exit immediately.
1654 static void failIfSafeMode(
1656 const char *zErrMsg
,
1662 va_start(ap
, zErrMsg
);
1663 zMsg
= sqlite3_vmprintf(zErrMsg
, ap
);
1665 raw_printf(stderr
, "line %d: ", p
->lineno
);
1666 utf8_printf(stderr
, "%s\n", zMsg
);
1672 ** SQL function: edit(VALUE)
1673 ** edit(VALUE,EDITOR)
1677 ** (1) Write VALUE into a temporary file.
1678 ** (2) Run program EDITOR on that temporary file.
1679 ** (3) Read the temporary file back and return its content as the result.
1680 ** (4) Delete the temporary file
1682 ** If the EDITOR argument is omitted, use the value in the VISUAL
1683 ** environment variable. If still there is no EDITOR, through an error.
1685 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1687 #ifndef SQLITE_NOHAVE_SYSTEM
1688 static void editFunc(
1689 sqlite3_context
*context
,
1691 sqlite3_value
**argv
1693 const char *zEditor
;
1694 char *zTempFile
= 0;
1703 unsigned char *p
= 0;
1706 zEditor
= (const char*)sqlite3_value_text(argv
[1]);
1708 zEditor
= getenv("VISUAL");
1711 sqlite3_result_error(context
, "no editor for edit()", -1);
1714 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
){
1715 sqlite3_result_error(context
, "NULL input to edit()", -1);
1718 db
= sqlite3_context_db_handle(context
);
1720 sqlite3_file_control(db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &zTempFile
);
1722 sqlite3_uint64 r
= 0;
1723 sqlite3_randomness(sizeof(r
), &r
);
1724 zTempFile
= sqlite3_mprintf("temp%llx", r
);
1726 sqlite3_result_error_nomem(context
);
1730 bBin
= sqlite3_value_type(argv
[0])==SQLITE_BLOB
;
1731 /* When writing the file to be edited, do \n to \r\n conversions on systems
1732 ** that want \r\n line endings */
1733 f
= fopen(zTempFile
, bBin
? "wb" : "w");
1735 sqlite3_result_error(context
, "edit() cannot open temp file", -1);
1738 sz
= sqlite3_value_bytes(argv
[0]);
1740 x
= fwrite(sqlite3_value_blob(argv
[0]), 1, (size_t)sz
, f
);
1742 const char *z
= (const char*)sqlite3_value_text(argv
[0]);
1743 /* Remember whether or not the value originally contained \r\n */
1744 if( z
&& strstr(z
,"\r\n")!=0 ) hasCRNL
= 1;
1745 x
= fwrite(sqlite3_value_text(argv
[0]), 1, (size_t)sz
, f
);
1750 sqlite3_result_error(context
, "edit() could not write the whole file", -1);
1753 zCmd
= sqlite3_mprintf("%s \"%s\"", zEditor
, zTempFile
);
1755 sqlite3_result_error_nomem(context
);
1761 sqlite3_result_error(context
, "EDITOR returned non-zero", -1);
1764 f
= fopen(zTempFile
, "rb");
1766 sqlite3_result_error(context
,
1767 "edit() cannot reopen temp file after edit", -1);
1770 fseek(f
, 0, SEEK_END
);
1773 p
= sqlite3_malloc64( sz
+1 );
1775 sqlite3_result_error_nomem(context
);
1778 x
= fread(p
, 1, (size_t)sz
, f
);
1782 sqlite3_result_error(context
, "could not read back the whole file", -1);
1786 sqlite3_result_blob64(context
, p
, sz
, sqlite3_free
);
1790 /* If the original contains \r\n then do no conversions back to \n */
1792 /* If the file did not originally contain \r\n then convert any new
1793 ** \r\n back into \n */
1795 for(i
=j
=0; i
<sz
; i
++){
1796 if( p
[i
]=='\r' && p
[i
+1]=='\n' ) i
++;
1802 sqlite3_result_text64(context
, (const char*)p
, sz
,
1803 sqlite3_free
, SQLITE_UTF8
);
1810 sqlite3_free(zTempFile
);
1813 #endif /* SQLITE_NOHAVE_SYSTEM */
1816 ** Save or restore the current output mode
1818 static void outputModePush(ShellState
*p
){
1819 p
->modePrior
= p
->mode
;
1820 p
->priorShFlgs
= p
->shellFlgs
;
1821 memcpy(p
->colSepPrior
, p
->colSeparator
, sizeof(p
->colSeparator
));
1822 memcpy(p
->rowSepPrior
, p
->rowSeparator
, sizeof(p
->rowSeparator
));
1824 static void outputModePop(ShellState
*p
){
1825 p
->mode
= p
->modePrior
;
1826 p
->shellFlgs
= p
->priorShFlgs
;
1827 memcpy(p
->colSeparator
, p
->colSepPrior
, sizeof(p
->colSeparator
));
1828 memcpy(p
->rowSeparator
, p
->rowSepPrior
, sizeof(p
->rowSeparator
));
1832 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1834 static void output_hex_blob(FILE *out
, const void *pBlob
, int nBlob
){
1836 unsigned char *aBlob
= (unsigned char*)pBlob
;
1838 char *zStr
= sqlite3_malloc(nBlob
*2 + 1);
1839 shell_check_oom(zStr
);
1841 for(i
=0; i
<nBlob
; i
++){
1842 static const char aHex
[] = {
1843 '0', '1', '2', '3', '4', '5', '6', '7',
1844 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1846 zStr
[i
*2] = aHex
[ (aBlob
[i
] >> 4) ];
1847 zStr
[i
*2+1] = aHex
[ (aBlob
[i
] & 0x0F) ];
1851 raw_printf(out
,"X'%s'", zStr
);
1856 ** Find a string that is not found anywhere in z[]. Return a pointer
1859 ** Try to use zA and zB first. If both of those are already found in z[]
1860 ** then make up some string and store it in the buffer zBuf.
1862 static const char *unused_string(
1863 const char *z
, /* Result must not appear anywhere in z */
1864 const char *zA
, const char *zB
, /* Try these first */
1865 char *zBuf
/* Space to store a generated string */
1868 if( strstr(z
, zA
)==0 ) return zA
;
1869 if( strstr(z
, zB
)==0 ) return zB
;
1871 sqlite3_snprintf(20,zBuf
,"(%s%u)", zA
, i
++);
1872 }while( strstr(z
,zBuf
)!=0 );
1877 ** Output the given string as a quoted string using SQL quoting conventions.
1879 ** See also: output_quoted_escaped_string()
1881 static void output_quoted_string(FILE *out
, const char *z
){
1884 setBinaryMode(out
, 1);
1886 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1888 utf8_printf(out
,"'%s'",z
);
1890 raw_printf(out
, "'");
1892 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1895 utf8_printf(out
, "%.*s", i
, z
);
1899 raw_printf(out
, "'");
1907 raw_printf(out
, "'");
1909 setTextMode(out
, 1);
1913 ** Output the given string as a quoted string using SQL quoting conventions.
1914 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1915 ** get corrupted by end-of-line translation facilities in some operating
1918 ** This is like output_quoted_string() but with the addition of the \r\n
1919 ** escape mechanism.
1921 static void output_quoted_escaped_string(FILE *out
, const char *z
){
1924 setBinaryMode(out
, 1);
1925 for(i
=0; (c
= z
[i
])!=0 && c
!='\'' && c
!='\n' && c
!='\r'; i
++){}
1927 utf8_printf(out
,"'%s'",z
);
1929 const char *zNL
= 0;
1930 const char *zCR
= 0;
1933 char zBuf1
[20], zBuf2
[20];
1934 for(i
=0; z
[i
]; i
++){
1935 if( z
[i
]=='\n' ) nNL
++;
1936 if( z
[i
]=='\r' ) nCR
++;
1939 raw_printf(out
, "replace(");
1940 zNL
= unused_string(z
, "\\n", "\\012", zBuf1
);
1943 raw_printf(out
, "replace(");
1944 zCR
= unused_string(z
, "\\r", "\\015", zBuf2
);
1946 raw_printf(out
, "'");
1948 for(i
=0; (c
= z
[i
])!=0 && c
!='\n' && c
!='\r' && c
!='\''; i
++){}
1951 utf8_printf(out
, "%.*s", i
, z
);
1955 raw_printf(out
, "'");
1963 raw_printf(out
, "%s", zNL
);
1966 raw_printf(out
, "%s", zCR
);
1968 raw_printf(out
, "'");
1970 raw_printf(out
, ",'%s',char(13))", zCR
);
1973 raw_printf(out
, ",'%s',char(10))", zNL
);
1976 setTextMode(out
, 1);
1980 ** Output the given string as a quoted according to C or TCL quoting rules.
1982 static void output_c_string(FILE *out
, const char *z
){
1985 while( (c
= *(z
++))!=0 ){
1992 }else if( c
=='\t' ){
1995 }else if( c
=='\n' ){
1998 }else if( c
=='\r' ){
2001 }else if( !isprint(c
&0xff) ){
2002 raw_printf(out
, "\\%03o", c
&0xff);
2011 ** Output the given string as a quoted according to JSON quoting rules.
2013 static void output_json_string(FILE *out
, const char *z
, i64 n
){
2016 if( n
<0 ) n
= strlen(z
);
2020 if( c
=='\\' || c
=='"' ){
2023 }else if( c
<=0x1f ){
2027 }else if( c
=='\f' ){
2029 }else if( c
=='\n' ){
2031 }else if( c
=='\r' ){
2033 }else if( c
=='\t' ){
2036 raw_printf(out
, "u%04x",c
);
2046 ** Output the given string with characters that are special to
2049 static void output_html_string(FILE *out
, const char *z
){
2061 utf8_printf(out
,"%.*s",i
,z
);
2064 raw_printf(out
,"<");
2065 }else if( z
[i
]=='&' ){
2066 raw_printf(out
,"&");
2067 }else if( z
[i
]=='>' ){
2068 raw_printf(out
,">");
2069 }else if( z
[i
]=='\"' ){
2070 raw_printf(out
,""");
2071 }else if( z
[i
]=='\'' ){
2072 raw_printf(out
,"'");
2081 ** If a field contains any character identified by a 1 in the following
2082 ** array, then the string must be quoted for CSV.
2084 static const char needCsvQuote
[] = {
2085 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2086 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2087 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
2088 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2089 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2090 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2091 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2092 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2093 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2094 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2095 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2096 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2097 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2098 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2099 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2100 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2104 ** Output a single term of CSV. Actually, p->colSeparator is used for
2105 ** the separator, which may or may not be a comma. p->nullValue is
2106 ** the null value. Strings are quoted if necessary. The separator
2107 ** is only issued if bSep is true.
2109 static void output_csv(ShellState
*p
, const char *z
, int bSep
){
2112 utf8_printf(out
,"%s",p
->nullValue
);
2115 for(i
=0; z
[i
]; i
++){
2116 if( needCsvQuote
[((unsigned char*)z
)[i
]] ){
2121 if( i
==0 || strstr(z
, p
->colSeparator
)!=0 ){
2122 char *zQuoted
= sqlite3_mprintf("\"%w\"", z
);
2123 shell_check_oom(zQuoted
);
2124 utf8_printf(out
, "%s", zQuoted
);
2125 sqlite3_free(zQuoted
);
2127 utf8_printf(out
, "%s", z
);
2131 utf8_printf(p
->out
, "%s", p
->colSeparator
);
2136 ** This routine runs when the user presses Ctrl-C
2138 static void interrupt_handler(int NotUsed
){
2139 UNUSED_PARAMETER(NotUsed
);
2140 if( ++seenInterrupt
>1 ) exit(1);
2141 if( globalDb
) sqlite3_interrupt(globalDb
);
2144 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2146 ** This routine runs for console events (e.g. Ctrl-C) on Win32
2148 static BOOL WINAPI
ConsoleCtrlHandler(
2149 DWORD dwCtrlType
/* One of the CTRL_*_EVENT constants */
2151 if( dwCtrlType
==CTRL_C_EVENT
){
2152 interrupt_handler(0);
2159 #ifndef SQLITE_OMIT_AUTHORIZATION
2161 ** This authorizer runs in safe mode.
2163 static int safeModeAuth(
2171 ShellState
*p
= (ShellState
*)pClientData
;
2172 static const char *azProhibitedFunctions
[] = {
2181 UNUSED_PARAMETER(zA1
);
2182 UNUSED_PARAMETER(zA3
);
2183 UNUSED_PARAMETER(zA4
);
2185 case SQLITE_ATTACH
: {
2186 #ifndef SQLITE_SHELL_FIDDLE
2187 /* In WASM builds the filesystem is a virtual sandbox, so
2188 ** there's no harm in using ATTACH. */
2189 failIfSafeMode(p
, "cannot run ATTACH in safe mode");
2193 case SQLITE_FUNCTION
: {
2195 for(i
=0; i
<ArraySize(azProhibitedFunctions
); i
++){
2196 if( sqlite3_stricmp(zA2
, azProhibitedFunctions
[i
])==0 ){
2197 failIfSafeMode(p
, "cannot use the %s() function in safe mode",
2198 azProhibitedFunctions
[i
]);
2208 ** When the ".auth ON" is set, the following authorizer callback is
2209 ** invoked. It always returns SQLITE_OK.
2211 static int shellAuth(
2219 ShellState
*p
= (ShellState
*)pClientData
;
2220 static const char *azAction
[] = { 0,
2221 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2222 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2223 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2224 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2225 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2226 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2227 "PRAGMA", "READ", "SELECT",
2228 "TRANSACTION", "UPDATE", "ATTACH",
2229 "DETACH", "ALTER_TABLE", "REINDEX",
2230 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2231 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2239 utf8_printf(p
->out
, "authorizer: %s", azAction
[op
]);
2241 raw_printf(p
->out
, " ");
2243 output_c_string(p
->out
, az
[i
]);
2245 raw_printf(p
->out
, "NULL");
2248 raw_printf(p
->out
, "\n");
2249 if( p
->bSafeMode
) (void)safeModeAuth(pClientData
, op
, zA1
, zA2
, zA3
, zA4
);
2255 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2257 ** This routine converts some CREATE TABLE statements for shadow tables
2258 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2260 ** If the schema statement in z[] contains a start-of-comment and if
2261 ** sqlite3_complete() returns false, try to terminate the comment before
2262 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2264 static void printSchemaLine(FILE *out
, const char *z
, const char *zTail
){
2267 if( zTail
==0 ) return;
2268 if( zTail
[0]==';' && (strstr(z
, "/*")!=0 || strstr(z
,"--")!=0) ){
2269 const char *zOrig
= z
;
2270 static const char *azTerm
[] = { "", "*/", "\n" };
2272 for(i
=0; i
<ArraySize(azTerm
); i
++){
2273 char *zNew
= sqlite3_mprintf("%s%s;", zOrig
, azTerm
[i
]);
2274 shell_check_oom(zNew
);
2275 if( sqlite3_complete(zNew
) ){
2276 size_t n
= strlen(zNew
);
2285 if( sqlite3_strglob("CREATE TABLE ['\"]*", z
)==0 ){
2286 utf8_printf(out
, "CREATE TABLE IF NOT EXISTS %s%s", z
+13, zTail
);
2288 utf8_printf(out
, "%s%s", z
, zTail
);
2290 sqlite3_free(zToFree
);
2292 static void printSchemaLineN(FILE *out
, char *z
, int n
, const char *zTail
){
2295 printSchemaLine(out
, z
, zTail
);
2300 ** Return true if string z[] has nothing but whitespace and comments to the
2301 ** end of the first line.
2303 static int wsToEol(const char *z
){
2305 for(i
=0; z
[i
]; i
++){
2306 if( z
[i
]=='\n' ) return 1;
2307 if( IsSpace(z
[i
]) ) continue;
2308 if( z
[i
]=='-' && z
[i
+1]=='-' ) return 1;
2315 ** Add a new entry to the EXPLAIN QUERY PLAN data
2317 static void eqp_append(ShellState
*p
, int iEqpId
, int p2
, const char *zText
){
2320 if( zText
==0 ) return;
2321 nText
= strlen(zText
);
2322 if( p
->autoEQPtest
){
2323 utf8_printf(p
->out
, "%d,%d,%s\n", iEqpId
, p2
, zText
);
2325 pNew
= sqlite3_malloc64( sizeof(*pNew
) + nText
);
2326 shell_check_oom(pNew
);
2327 pNew
->iEqpId
= iEqpId
;
2328 pNew
->iParentId
= p2
;
2329 memcpy(pNew
->zText
, zText
, nText
+1);
2331 if( p
->sGraph
.pLast
){
2332 p
->sGraph
.pLast
->pNext
= pNew
;
2334 p
->sGraph
.pRow
= pNew
;
2336 p
->sGraph
.pLast
= pNew
;
2340 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2343 static void eqp_reset(ShellState
*p
){
2344 EQPGraphRow
*pRow
, *pNext
;
2345 for(pRow
= p
->sGraph
.pRow
; pRow
; pRow
= pNext
){
2346 pNext
= pRow
->pNext
;
2349 memset(&p
->sGraph
, 0, sizeof(p
->sGraph
));
2352 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2353 ** pOld, or return the first such line if pOld is NULL
2355 static EQPGraphRow
*eqp_next_row(ShellState
*p
, int iEqpId
, EQPGraphRow
*pOld
){
2356 EQPGraphRow
*pRow
= pOld
? pOld
->pNext
: p
->sGraph
.pRow
;
2357 while( pRow
&& pRow
->iParentId
!=iEqpId
) pRow
= pRow
->pNext
;
2361 /* Render a single level of the graph that has iEqpId as its parent. Called
2362 ** recursively to render sublevels.
2364 static void eqp_render_level(ShellState
*p
, int iEqpId
){
2365 EQPGraphRow
*pRow
, *pNext
;
2366 i64 n
= strlen(p
->sGraph
.zPrefix
);
2368 for(pRow
= eqp_next_row(p
, iEqpId
, 0); pRow
; pRow
= pNext
){
2369 pNext
= eqp_next_row(p
, iEqpId
, pRow
);
2371 utf8_printf(p
->out
, "%s%s%s\n", p
->sGraph
.zPrefix
,
2372 pNext
? "|--" : "`--", z
);
2373 if( n
<(i64
)sizeof(p
->sGraph
.zPrefix
)-7 ){
2374 memcpy(&p
->sGraph
.zPrefix
[n
], pNext
? "| " : " ", 4);
2375 eqp_render_level(p
, pRow
->iEqpId
);
2376 p
->sGraph
.zPrefix
[n
] = 0;
2382 ** Display and reset the EXPLAIN QUERY PLAN data
2384 static void eqp_render(ShellState
*p
, i64 nCycle
){
2385 EQPGraphRow
*pRow
= p
->sGraph
.pRow
;
2387 if( pRow
->zText
[0]=='-' ){
2388 if( pRow
->pNext
==0 ){
2392 utf8_printf(p
->out
, "%s\n", pRow
->zText
+3);
2393 p
->sGraph
.pRow
= pRow
->pNext
;
2395 }else if( nCycle
>0 ){
2396 utf8_printf(p
->out
, "QUERY PLAN (cycles=%lld [100%%])\n", nCycle
);
2398 utf8_printf(p
->out
, "QUERY PLAN\n");
2400 p
->sGraph
.zPrefix
[0] = 0;
2401 eqp_render_level(p
, 0);
2406 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2408 ** Progress handler callback.
2410 static int progress_handler(void *pClientData
) {
2411 ShellState
*p
= (ShellState
*)pClientData
;
2413 if( p
->nProgress
>=p
->mxProgress
&& p
->mxProgress
>0 ){
2414 raw_printf(p
->out
, "Progress limit reached (%u)\n", p
->nProgress
);
2415 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
2416 if( p
->flgProgress
& SHELL_PROGRESS_ONCE
) p
->mxProgress
= 0;
2419 if( (p
->flgProgress
& SHELL_PROGRESS_QUIET
)==0 ){
2420 raw_printf(p
->out
, "Progress %u\n", p
->nProgress
);
2424 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2429 static void print_dashes(FILE *out
, int N
){
2430 const char zDash
[] = "--------------------------------------------------";
2431 const int nDash
= sizeof(zDash
) - 1;
2436 raw_printf(out
, "%.*s", N
, zDash
);
2440 ** Print a markdown or table-style row separator using ascii-art
2442 static void print_row_separator(
2449 fputs(zSep
, p
->out
);
2450 print_dashes(p
->out
, p
->actualWidth
[0]+2);
2451 for(i
=1; i
<nArg
; i
++){
2452 fputs(zSep
, p
->out
);
2453 print_dashes(p
->out
, p
->actualWidth
[i
]+2);
2455 fputs(zSep
, p
->out
);
2457 fputs("\n", p
->out
);
2461 ** This is the callback routine that the shell
2462 ** invokes for each row of a query result.
2464 static int shell_callback(
2466 int nArg
, /* Number of result columns */
2467 char **azArg
, /* Text of each result column */
2468 char **azCol
, /* Column names */
2469 int *aiType
/* Column types. Might be NULL */
2472 ShellState
*p
= (ShellState
*)pArg
;
2474 if( azArg
==0 ) return 0;
2482 if( azArg
==0 ) break;
2483 for(i
=0; i
<nArg
; i
++){
2484 int len
= strlen30(azCol
[i
] ? azCol
[i
] : "");
2485 if( len
>w
) w
= len
;
2487 if( p
->cnt
++>0 ) utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2488 for(i
=0; i
<nArg
; i
++){
2489 utf8_printf(p
->out
,"%*s = %s%s", w
, azCol
[i
],
2490 azArg
[i
] ? azArg
[i
] : p
->nullValue
, p
->rowSeparator
);
2494 case MODE_Explain
: {
2495 static const int aExplainWidth
[] = {4, 13, 4, 4, 4, 13, 2, 13};
2496 if( nArg
>ArraySize(aExplainWidth
) ){
2497 nArg
= ArraySize(aExplainWidth
);
2500 for(i
=0; i
<nArg
; i
++){
2501 int w
= aExplainWidth
[i
];
2502 utf8_width_print(p
->out
, w
, azCol
[i
]);
2503 fputs(i
==nArg
-1 ? "\n" : " ", p
->out
);
2505 for(i
=0; i
<nArg
; i
++){
2506 int w
= aExplainWidth
[i
];
2507 print_dashes(p
->out
, w
);
2508 fputs(i
==nArg
-1 ? "\n" : " ", p
->out
);
2511 if( azArg
==0 ) break;
2512 for(i
=0; i
<nArg
; i
++){
2513 int w
= aExplainWidth
[i
];
2514 if( i
==nArg
-1 ) w
= 0;
2515 if( azArg
[i
] && strlenChar(azArg
[i
])>w
){
2516 w
= strlenChar(azArg
[i
]);
2518 if( i
==1 && p
->aiIndent
&& p
->pStmt
){
2519 if( p
->iIndent
<p
->nIndent
){
2520 utf8_printf(p
->out
, "%*.s", p
->aiIndent
[p
->iIndent
], "");
2524 utf8_width_print(p
->out
, w
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2525 fputs(i
==nArg
-1 ? "\n" : " ", p
->out
);
2529 case MODE_Semi
: { /* .schema and .fullschema output */
2530 printSchemaLine(p
->out
, azArg
[0], ";\n");
2533 case MODE_Pretty
: { /* .schema and .fullschema with --indent */
2541 if( azArg
[0]==0 ) break;
2542 if( sqlite3_strlike("CREATE VIEW%", azArg
[0], 0)==0
2543 || sqlite3_strlike("CREATE TRIG%", azArg
[0], 0)==0
2545 utf8_printf(p
->out
, "%s;\n", azArg
[0]);
2548 z
= sqlite3_mprintf("%s", azArg
[0]);
2551 for(i
=0; IsSpace(z
[i
]); i
++){}
2552 for(; (c
= z
[i
])!=0; i
++){
2554 if( z
[j
-1]=='\r' ) z
[j
-1] = '\n';
2555 if( IsSpace(z
[j
-1]) || z
[j
-1]=='(' ) continue;
2556 }else if( (c
=='(' || c
==')') && j
>0 && IsSpace(z
[j
-1]) ){
2561 while( j
>0 && IsSpace(z
[j
-1]) ){ j
--; }
2563 if( strlen30(z
)>=79 ){
2564 for(i
=j
=0; (c
= z
[i
])!=0; i
++){ /* Copy from z[i] back to z[j] */
2567 }else if( c
=='"' || c
=='\'' || c
=='`' ){
2571 }else if( c
=='-' && z
[i
+1]=='-' ){
2577 if( nLine
>0 && nParen
==0 && j
>0 ){
2578 printSchemaLineN(p
->out
, z
, j
, "\n");
2583 if( nParen
==1 && cEnd
==0
2584 && (c
=='(' || c
=='\n' || (c
==',' && !wsToEol(z
+i
+1)))
2587 printSchemaLineN(p
->out
, z
, j
, "\n ");
2590 while( IsSpace(z
[i
+1]) ){ i
++; }
2595 printSchemaLine(p
->out
, z
, ";\n");
2600 if( p
->cnt
++==0 && p
->showHeader
){
2601 for(i
=0; i
<nArg
; i
++){
2602 utf8_printf(p
->out
,"%s%s",azCol
[i
],
2603 i
==nArg
-1 ? p
->rowSeparator
: p
->colSeparator
);
2606 if( azArg
==0 ) break;
2607 for(i
=0; i
<nArg
; i
++){
2609 if( z
==0 ) z
= p
->nullValue
;
2610 utf8_printf(p
->out
, "%s", z
);
2612 utf8_printf(p
->out
, "%s", p
->colSeparator
);
2614 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2620 if( p
->cnt
++==0 && p
->showHeader
){
2621 raw_printf(p
->out
,"<TR>");
2622 for(i
=0; i
<nArg
; i
++){
2623 raw_printf(p
->out
,"<TH>");
2624 output_html_string(p
->out
, azCol
[i
]);
2625 raw_printf(p
->out
,"</TH>\n");
2627 raw_printf(p
->out
,"</TR>\n");
2629 if( azArg
==0 ) break;
2630 raw_printf(p
->out
,"<TR>");
2631 for(i
=0; i
<nArg
; i
++){
2632 raw_printf(p
->out
,"<TD>");
2633 output_html_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2634 raw_printf(p
->out
,"</TD>\n");
2636 raw_printf(p
->out
,"</TR>\n");
2640 if( p
->cnt
++==0 && p
->showHeader
){
2641 for(i
=0; i
<nArg
; i
++){
2642 output_c_string(p
->out
,azCol
[i
] ? azCol
[i
] : "");
2643 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2645 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2647 if( azArg
==0 ) break;
2648 for(i
=0; i
<nArg
; i
++){
2649 output_c_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2650 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2652 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2656 setBinaryMode(p
->out
, 1);
2657 if( p
->cnt
++==0 && p
->showHeader
){
2658 for(i
=0; i
<nArg
; i
++){
2659 output_csv(p
, azCol
[i
] ? azCol
[i
] : "", i
<nArg
-1);
2661 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2664 for(i
=0; i
<nArg
; i
++){
2665 output_csv(p
, azArg
[i
], i
<nArg
-1);
2667 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2669 setTextMode(p
->out
, 1);
2673 if( azArg
==0 ) break;
2674 utf8_printf(p
->out
,"INSERT INTO %s",p
->zDestTable
);
2675 if( p
->showHeader
){
2676 raw_printf(p
->out
,"(");
2677 for(i
=0; i
<nArg
; i
++){
2678 if( i
>0 ) raw_printf(p
->out
, ",");
2679 if( quoteChar(azCol
[i
]) ){
2680 char *z
= sqlite3_mprintf("\"%w\"", azCol
[i
]);
2682 utf8_printf(p
->out
, "%s", z
);
2685 raw_printf(p
->out
, "%s", azCol
[i
]);
2688 raw_printf(p
->out
,")");
2691 for(i
=0; i
<nArg
; i
++){
2692 raw_printf(p
->out
, i
>0 ? "," : " VALUES(");
2693 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2694 utf8_printf(p
->out
,"NULL");
2695 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2696 if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2697 output_quoted_string(p
->out
, azArg
[i
]);
2699 output_quoted_escaped_string(p
->out
, azArg
[i
]);
2701 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2702 utf8_printf(p
->out
,"%s", azArg
[i
]);
2703 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2705 double r
= sqlite3_column_double(p
->pStmt
, i
);
2707 memcpy(&ur
,&r
,sizeof(r
));
2708 if( ur
==0x7ff0000000000000LL
){
2709 raw_printf(p
->out
, "9.0e+999");
2710 }else if( ur
==0xfff0000000000000LL
){
2711 raw_printf(p
->out
, "-9.0e+999");
2713 sqlite3_int64 ir
= (sqlite3_int64
)r
;
2714 if( r
==(double)ir
){
2715 sqlite3_snprintf(50,z
,"%lld.0", ir
);
2717 sqlite3_snprintf(50,z
,"%!.20g", r
);
2719 raw_printf(p
->out
, "%s", z
);
2721 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2722 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2723 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2724 output_hex_blob(p
->out
, pBlob
, nBlob
);
2725 }else if( isNumber(azArg
[i
], 0) ){
2726 utf8_printf(p
->out
,"%s", azArg
[i
]);
2727 }else if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2728 output_quoted_string(p
->out
, azArg
[i
]);
2730 output_quoted_escaped_string(p
->out
, azArg
[i
]);
2733 raw_printf(p
->out
,");\n");
2737 if( azArg
==0 ) break;
2739 fputs("[{", p
->out
);
2741 fputs(",\n{", p
->out
);
2744 for(i
=0; i
<nArg
; i
++){
2745 output_json_string(p
->out
, azCol
[i
], -1);
2747 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2748 fputs("null",p
->out
);
2749 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2751 double r
= sqlite3_column_double(p
->pStmt
, i
);
2753 memcpy(&ur
,&r
,sizeof(r
));
2754 if( ur
==0x7ff0000000000000LL
){
2755 raw_printf(p
->out
, "9.0e+999");
2756 }else if( ur
==0xfff0000000000000LL
){
2757 raw_printf(p
->out
, "-9.0e+999");
2759 sqlite3_snprintf(50,z
,"%!.20g", r
);
2760 raw_printf(p
->out
, "%s", z
);
2762 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2763 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2764 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2765 output_json_string(p
->out
, pBlob
, nBlob
);
2766 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2767 output_json_string(p
->out
, azArg
[i
], -1);
2769 utf8_printf(p
->out
,"%s", azArg
[i
]);
2779 if( azArg
==0 ) break;
2780 if( p
->cnt
==0 && p
->showHeader
){
2781 for(i
=0; i
<nArg
; i
++){
2782 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
2783 output_quoted_string(p
->out
, azCol
[i
]);
2785 fputs(p
->rowSeparator
, p
->out
);
2788 for(i
=0; i
<nArg
; i
++){
2789 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
2790 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2791 utf8_printf(p
->out
,"NULL");
2792 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2793 output_quoted_string(p
->out
, azArg
[i
]);
2794 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2795 utf8_printf(p
->out
,"%s", azArg
[i
]);
2796 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2798 double r
= sqlite3_column_double(p
->pStmt
, i
);
2799 sqlite3_snprintf(50,z
,"%!.20g", r
);
2800 raw_printf(p
->out
, "%s", z
);
2801 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2802 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2803 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2804 output_hex_blob(p
->out
, pBlob
, nBlob
);
2805 }else if( isNumber(azArg
[i
], 0) ){
2806 utf8_printf(p
->out
,"%s", azArg
[i
]);
2808 output_quoted_string(p
->out
, azArg
[i
]);
2811 fputs(p
->rowSeparator
, p
->out
);
2815 if( p
->cnt
++==0 && p
->showHeader
){
2816 for(i
=0; i
<nArg
; i
++){
2817 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2818 utf8_printf(p
->out
,"%s",azCol
[i
] ? azCol
[i
] : "");
2820 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2822 if( azArg
==0 ) break;
2823 for(i
=0; i
<nArg
; i
++){
2824 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2825 utf8_printf(p
->out
,"%s",azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2827 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2831 eqp_append(p
, atoi(azArg
[0]), atoi(azArg
[1]), azArg
[3]);
2839 ** This is the callback routine that the SQLite library
2840 ** invokes for each row of a query result.
2842 static int callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
2843 /* since we don't have type info, call the shell_callback with a NULL value */
2844 return shell_callback(pArg
, nArg
, azArg
, azCol
, NULL
);
2848 ** This is the callback routine from sqlite3_exec() that appends all
2849 ** output onto the end of a ShellText object.
2851 static int captureOutputCallback(void *pArg
, int nArg
, char **azArg
, char **az
){
2852 ShellText
*p
= (ShellText
*)pArg
;
2854 UNUSED_PARAMETER(az
);
2855 if( azArg
==0 ) return 0;
2856 if( p
->n
) appendText(p
, "|", 0);
2857 for(i
=0; i
<nArg
; i
++){
2858 if( i
) appendText(p
, ",", 0);
2859 if( azArg
[i
] ) appendText(p
, azArg
[i
], 0);
2865 ** Generate an appropriate SELFTEST table in the main database.
2867 static void createSelftestTable(ShellState
*p
){
2870 "SAVEPOINT selftest_init;\n"
2871 "CREATE TABLE IF NOT EXISTS selftest(\n"
2872 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2873 " op TEXT,\n" /* Operator: memo run */
2874 " cmd TEXT,\n" /* Command text */
2875 " ans TEXT\n" /* Desired answer */
2877 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2878 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2879 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2880 " 'memo','Tests generated by --init');\n"
2881 "INSERT INTO [_shell$self]\n"
2883 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2884 "FROM sqlite_schema ORDER BY 2'',224))',\n"
2885 " hex(sha3_query('SELECT type,name,tbl_name,sql "
2886 "FROM sqlite_schema ORDER BY 2',224));\n"
2887 "INSERT INTO [_shell$self]\n"
2889 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2890 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2891 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2893 " SELECT name FROM sqlite_schema\n"
2894 " WHERE type='table'\n"
2895 " AND name<>'selftest'\n"
2896 " AND coalesce(rootpage,0)>0\n"
2899 "INSERT INTO [_shell$self]\n"
2900 " VALUES('run','PRAGMA integrity_check','ok');\n"
2901 "INSERT INTO selftest(tno,op,cmd,ans)"
2902 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2903 "DROP TABLE [_shell$self];"
2906 utf8_printf(stderr
, "SELFTEST initialization failure: %s\n", zErrMsg
);
2907 sqlite3_free(zErrMsg
);
2909 sqlite3_exec(p
->db
, "RELEASE selftest_init",0,0,0);
2914 ** Set the destination table field of the ShellState structure to
2915 ** the name of the table given. Escape any quote characters in the
2918 static void set_table_name(ShellState
*p
, const char *zName
){
2923 if( p
->zDestTable
){
2924 free(p
->zDestTable
);
2927 if( zName
==0 ) return;
2928 cQuote
= quoteChar(zName
);
2929 n
= strlen30(zName
);
2930 if( cQuote
) n
+= n
+2;
2931 z
= p
->zDestTable
= malloc( n
+1 );
2934 if( cQuote
) z
[n
++] = cQuote
;
2935 for(i
=0; zName
[i
]; i
++){
2937 if( zName
[i
]==cQuote
) z
[n
++] = cQuote
;
2939 if( cQuote
) z
[n
++] = cQuote
;
2944 ** Maybe construct two lines of text that point out the position of a
2945 ** syntax error. Return a pointer to the text, in memory obtained from
2946 ** sqlite3_malloc(). Or, if the most recent error does not involve a
2947 ** specific token that we can point to, return an empty string.
2949 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
2950 ** and should be released by the caller invoking sqlite3_free().
2952 static char *shell_error_context(const char *zSql
, sqlite3
*db
){
2960 || (iOffset
= sqlite3_error_offset(db
))<0
2961 || iOffset
>=(int)strlen(zSql
)
2963 return sqlite3_mprintf("");
2965 while( iOffset
>50 ){
2968 while( (zSql
[0]&0xc0)==0x80 ){ zSql
++; iOffset
--; }
2973 while( len
>0 && (zSql
[len
]&0xc0)==0x80 ) len
--;
2975 zCode
= sqlite3_mprintf("%.*s", len
, zSql
);
2976 shell_check_oom(zCode
);
2977 for(i
=0; zCode
[i
]; i
++){ if( IsSpace(zSql
[i
]) ) zCode
[i
] = ' '; }
2979 zMsg
= sqlite3_mprintf("\n %z\n %*s^--- error here", zCode
,iOffset
,"");
2981 zMsg
= sqlite3_mprintf("\n %z\n %*serror here ---^", zCode
,iOffset
-14,"");
2988 ** Execute a query statement that will generate SQL output. Print
2989 ** the result columns, comma-separated, on a line and then add a
2990 ** semicolon terminator to the end of that line.
2992 ** If the number of columns is 1 and that column contains text "--"
2993 ** then write the semicolon on a separate line. That way, if a
2994 ** "--" comment occurs at the end of the statement, the comment
2995 ** won't consume the semicolon terminator.
2997 static int run_table_dump_query(
2998 ShellState
*p
, /* Query context */
2999 const char *zSelect
/* SELECT statement to extract content */
3001 sqlite3_stmt
*pSelect
;
3006 rc
= sqlite3_prepare_v2(p
->db
, zSelect
, -1, &pSelect
, 0);
3007 if( rc
!=SQLITE_OK
|| !pSelect
){
3008 char *zContext
= shell_error_context(zSelect
, p
->db
);
3009 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n%s", rc
,
3010 sqlite3_errmsg(p
->db
), zContext
);
3011 sqlite3_free(zContext
);
3012 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
3015 rc
= sqlite3_step(pSelect
);
3016 nResult
= sqlite3_column_count(pSelect
);
3017 while( rc
==SQLITE_ROW
){
3018 z
= (const char*)sqlite3_column_text(pSelect
, 0);
3019 utf8_printf(p
->out
, "%s", z
);
3020 for(i
=1; i
<nResult
; i
++){
3021 utf8_printf(p
->out
, ",%s", sqlite3_column_text(pSelect
, i
));
3024 while( z
[0] && (z
[0]!='-' || z
[1]!='-') ) z
++;
3026 raw_printf(p
->out
, "\n;\n");
3028 raw_printf(p
->out
, ";\n");
3030 rc
= sqlite3_step(pSelect
);
3032 rc
= sqlite3_finalize(pSelect
);
3033 if( rc
!=SQLITE_OK
){
3034 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n", rc
,
3035 sqlite3_errmsg(p
->db
));
3036 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
3042 ** Allocate space and save off string indicating current error.
3044 static char *save_err_msg(
3045 sqlite3
*db
, /* Database to query */
3046 const char *zPhase
, /* When the error occcurs */
3047 int rc
, /* Error code returned from API */
3048 const char *zSql
/* SQL string, or NULL */
3052 sqlite3_str
*pStr
= sqlite3_str_new(0);
3053 sqlite3_str_appendf(pStr
, "%s, %s", zPhase
, sqlite3_errmsg(db
));
3055 sqlite3_str_appendf(pStr
, " (%d)", rc
);
3057 zContext
= shell_error_context(zSql
, db
);
3059 sqlite3_str_appendall(pStr
, zContext
);
3060 sqlite3_free(zContext
);
3062 zErr
= sqlite3_str_finish(pStr
);
3063 shell_check_oom(zErr
);
3069 ** Attempt to display I/O stats on Linux using /proc/PID/io
3071 static void displayLinuxIoStats(FILE *out
){
3074 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
3075 in
= fopen(z
, "rb");
3077 while( fgets(z
, sizeof(z
), in
)!=0 ){
3078 static const struct {
3079 const char *zPattern
;
3082 { "rchar: ", "Bytes received by read():" },
3083 { "wchar: ", "Bytes sent to write():" },
3084 { "syscr: ", "Read() system calls:" },
3085 { "syscw: ", "Write() system calls:" },
3086 { "read_bytes: ", "Bytes read from storage:" },
3087 { "write_bytes: ", "Bytes written to storage:" },
3088 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3091 for(i
=0; i
<ArraySize(aTrans
); i
++){
3092 int n
= strlen30(aTrans
[i
].zPattern
);
3093 if( cli_strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
3094 utf8_printf(out
, "%-36s %s", aTrans
[i
].zDesc
, &z
[n
]);
3104 ** Display a single line of status using 64-bit values.
3106 static void displayStatLine(
3107 ShellState
*p
, /* The shell context */
3108 char *zLabel
, /* Label for this one line */
3109 char *zFormat
, /* Format for the result */
3110 int iStatusCtrl
, /* Which status to display */
3111 int bReset
/* True to reset the stats */
3113 sqlite3_int64 iCur
= -1;
3114 sqlite3_int64 iHiwtr
= -1;
3117 sqlite3_status64(iStatusCtrl
, &iCur
, &iHiwtr
, bReset
);
3118 for(i
=0, nPercent
=0; zFormat
[i
]; i
++){
3119 if( zFormat
[i
]=='%' ) nPercent
++;
3122 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iCur
, iHiwtr
);
3124 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iHiwtr
);
3126 raw_printf(p
->out
, "%-36s %s\n", zLabel
, zLine
);
3130 ** Display memory stats.
3132 static int display_stats(
3133 sqlite3
*db
, /* Database to query */
3134 ShellState
*pArg
, /* Pointer to ShellState */
3135 int bReset
/* True to reset the stats */
3140 if( pArg
==0 || pArg
->out
==0 ) return 0;
3143 if( pArg
->pStmt
&& pArg
->statsOn
==2 ){
3145 sqlite3_stmt
*pStmt
= pArg
->pStmt
;
3147 nCol
= sqlite3_column_count(pStmt
);
3148 raw_printf(out
, "%-36s %d\n", "Number of output columns:", nCol
);
3149 for(i
=0; i
<nCol
; i
++){
3150 sqlite3_snprintf(sizeof(z
),z
,"Column %d %nname:", i
, &x
);
3151 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_name(pStmt
,i
));
3152 #ifndef SQLITE_OMIT_DECLTYPE
3153 sqlite3_snprintf(30, z
+x
, "declared type:");
3154 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_decltype(pStmt
, i
));
3156 #ifdef SQLITE_ENABLE_COLUMN_METADATA
3157 sqlite3_snprintf(30, z
+x
, "database name:");
3158 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_database_name(pStmt
,i
));
3159 sqlite3_snprintf(30, z
+x
, "table name:");
3160 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_table_name(pStmt
,i
));
3161 sqlite3_snprintf(30, z
+x
, "origin name:");
3162 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_origin_name(pStmt
,i
));
3167 if( pArg
->statsOn
==3 ){
3169 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
,bReset
);
3170 raw_printf(pArg
->out
, "VM-steps: %d\n", iCur
);
3175 displayStatLine(pArg
, "Memory Used:",
3176 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED
, bReset
);
3177 displayStatLine(pArg
, "Number of Outstanding Allocations:",
3178 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT
, bReset
);
3179 if( pArg
->shellFlgs
& SHFLG_Pagecache
){
3180 displayStatLine(pArg
, "Number of Pcache Pages Used:",
3181 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED
, bReset
);
3183 displayStatLine(pArg
, "Number of Pcache Overflow Bytes:",
3184 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW
, bReset
);
3185 displayStatLine(pArg
, "Largest Allocation:",
3186 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE
, bReset
);
3187 displayStatLine(pArg
, "Largest Pcache Allocation:",
3188 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE
, bReset
);
3189 #ifdef YYTRACKMAXSTACKDEPTH
3190 displayStatLine(pArg
, "Deepest Parser Stack:",
3191 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK
, bReset
);
3195 if( pArg
->shellFlgs
& SHFLG_Lookaside
){
3197 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_USED
,
3198 &iCur
, &iHiwtr
, bReset
);
3199 raw_printf(pArg
->out
,
3200 "Lookaside Slots Used: %d (max %d)\n",
3202 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
,
3203 &iCur
, &iHiwtr
, bReset
);
3204 raw_printf(pArg
->out
, "Successful lookaside attempts: %d\n",
3206 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
,
3207 &iCur
, &iHiwtr
, bReset
);
3208 raw_printf(pArg
->out
, "Lookaside failures due to size: %d\n",
3210 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
,
3211 &iCur
, &iHiwtr
, bReset
);
3212 raw_printf(pArg
->out
, "Lookaside failures due to OOM: %d\n",
3216 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHiwtr
, bReset
);
3217 raw_printf(pArg
->out
, "Pager Heap Usage: %d bytes\n",
3220 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHiwtr
, 1);
3221 raw_printf(pArg
->out
, "Page cache hits: %d\n", iCur
);
3223 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHiwtr
, 1);
3224 raw_printf(pArg
->out
, "Page cache misses: %d\n", iCur
);
3226 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHiwtr
, 1);
3227 raw_printf(pArg
->out
, "Page cache writes: %d\n", iCur
);
3229 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_SPILL
, &iCur
, &iHiwtr
, 1);
3230 raw_printf(pArg
->out
, "Page cache spills: %d\n", iCur
);
3232 sqlite3_db_status(db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHiwtr
, bReset
);
3233 raw_printf(pArg
->out
, "Schema Heap Usage: %d bytes\n",
3236 sqlite3_db_status(db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHiwtr
, bReset
);
3237 raw_printf(pArg
->out
, "Statement Heap/Lookaside Usage: %d bytes\n",
3243 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FULLSCAN_STEP
,
3245 raw_printf(pArg
->out
, "Fullscan Steps: %d\n", iCur
);
3246 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_SORT
, bReset
);
3247 raw_printf(pArg
->out
, "Sort Operations: %d\n", iCur
);
3248 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_AUTOINDEX
,bReset
);
3249 raw_printf(pArg
->out
, "Autoindex Inserts: %d\n", iCur
);
3250 iHit
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_HIT
,
3252 iMiss
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_MISS
,
3254 if( iHit
|| iMiss
){
3255 raw_printf(pArg
->out
, "Bloom filter bypass taken: %d/%d\n",
3258 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
, bReset
);
3259 raw_printf(pArg
->out
, "Virtual Machine Steps: %d\n", iCur
);
3260 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_REPREPARE
,bReset
);
3261 raw_printf(pArg
->out
, "Reprepare operations: %d\n", iCur
);
3262 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_RUN
, bReset
);
3263 raw_printf(pArg
->out
, "Number of times run: %d\n", iCur
);
3264 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_MEMUSED
, bReset
);
3265 raw_printf(pArg
->out
, "Memory used by prepared stmt: %d\n", iCur
);
3269 displayLinuxIoStats(pArg
->out
);
3272 /* Do not remove this machine readable comment: extra-stats-output-here */
3278 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3279 static int scanStatsHeight(sqlite3_stmt
*p
, int iEntry
){
3282 sqlite3_stmt_scanstatus_v2(p
, iEntry
,
3283 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
3290 res
= sqlite3_stmt_scanstatus_v2(p
, ii
,
3291 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iId
3295 sqlite3_stmt_scanstatus_v2(p
, ii
,
3296 SQLITE_SCANSTAT_PARENTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
3307 ** Display scan stats.
3309 static void display_scanstats(
3310 sqlite3
*db
, /* Database to query */
3311 ShellState
*pArg
/* Pointer to ShellState */
3313 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3314 UNUSED_PARAMETER(db
);
3315 UNUSED_PARAMETER(pArg
);
3317 static const int f
= SQLITE_SCANSTAT_COMPLEX
;
3318 sqlite3_stmt
*p
= pArg
->pStmt
;
3327 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&z
) ){
3330 n
= strlen(z
) + scanStatsHeight(p
, ii
)*3;
3331 if( n
>nWidth
) nWidth
= n
;
3335 sqlite3_stmt_scanstatus_v2(p
, -1, SQLITE_SCANSTAT_NCYCLE
, f
, (void*)&nTotal
);
3343 const char *zName
= 0;
3347 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&z
) ){
3350 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_EST
,f
,(void*)&rEst
);
3351 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NLOOP
,f
,(void*)&nLoop
);
3352 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NVISIT
,f
,(void*)&nRow
);
3353 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NCYCLE
,f
,(void*)&nCycle
);
3354 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_SELECTID
,f
,(void*)&iId
);
3355 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_PARENTID
,f
,(void*)&iPid
);
3356 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NAME
,f
,(void*)&zName
);
3358 zText
= sqlite3_mprintf("%s", z
);
3359 if( nCycle
>=0 || nLoop
>=0 || nRow
>=0 ){
3361 if( nCycle
>=0 && nTotal
>0 ){
3362 z
= sqlite3_mprintf("%zcycles=%lld [%d%%]", z
,
3363 nCycle
, ((nCycle
*100)+nTotal
/2) / nTotal
3367 z
= sqlite3_mprintf("%z%sloops=%lld", z
, z
? " " : "", nLoop
);
3370 z
= sqlite3_mprintf("%z%srows=%lld", z
, z
? " " : "", nRow
);
3373 if( zName
&& pArg
->scanstatsOn
>1 ){
3374 double rpl
= (double)nRow
/ (double)nLoop
;
3375 z
= sqlite3_mprintf("%z rpl=%.1f est=%.1f", z
, rpl
, rEst
);
3378 zText
= sqlite3_mprintf(
3379 "% *z (%z)", -1*(nWidth
-scanStatsHeight(p
, ii
)*3), zText
, z
3383 eqp_append(pArg
, iId
, iPid
, zText
);
3384 sqlite3_free(zText
);
3387 eqp_render(pArg
, nTotal
);
3392 ** Parameter azArray points to a zero-terminated array of strings. zStr
3393 ** points to a single nul-terminated string. Return non-zero if zStr
3394 ** is equal, according to strcmp(), to any of the strings in the array.
3395 ** Otherwise, return zero.
3397 static int str_in_array(const char *zStr
, const char **azArray
){
3399 for(i
=0; azArray
[i
]; i
++){
3400 if( 0==cli_strcmp(zStr
, azArray
[i
]) ) return 1;
3406 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3407 ** and populate the ShellState.aiIndent[] array with the number of
3408 ** spaces each opcode should be indented before it is output.
3410 ** The indenting rules are:
3412 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3413 ** all opcodes that occur between the p2 jump destination and the opcode
3414 ** itself by 2 spaces.
3416 ** * Do the previous for "Return" instructions for when P2 is positive.
3417 ** See tag-20220407a in wherecode.c and vdbe.c.
3419 ** * For each "Goto", if the jump destination is earlier in the program
3420 ** and ends on one of:
3421 ** Yield SeekGt SeekLt RowSetRead Rewind
3422 ** or if the P1 parameter is one instead of zero,
3423 ** then indent all opcodes between the earlier instruction
3424 ** and "Goto" by 2 spaces.
3426 static void explain_data_prepare(ShellState
*p
, sqlite3_stmt
*pSql
){
3427 const char *zSql
; /* The text of the SQL statement */
3428 const char *z
; /* Used to check if this is an EXPLAIN */
3429 int *abYield
= 0; /* True if op is an OP_Yield */
3430 int nAlloc
= 0; /* Allocated size of p->aiIndent[], abYield */
3431 int iOp
; /* Index of operation in p->aiIndent[] */
3433 const char *azNext
[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3435 const char *azYield
[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3437 const char *azGoto
[] = { "Goto", 0 };
3439 /* Try to figure out if this is really an EXPLAIN statement. If this
3440 ** cannot be verified, return early. */
3441 if( sqlite3_column_count(pSql
)!=8 ){
3445 zSql
= sqlite3_sql(pSql
);
3446 if( zSql
==0 ) return;
3447 for(z
=zSql
; *z
==' ' || *z
=='\t' || *z
=='\n' || *z
=='\f' || *z
=='\r'; z
++);
3448 if( sqlite3_strnicmp(z
, "explain", 7) ){
3453 for(iOp
=0; SQLITE_ROW
==sqlite3_step(pSql
); iOp
++){
3455 int iAddr
= sqlite3_column_int(pSql
, 0);
3456 const char *zOp
= (const char*)sqlite3_column_text(pSql
, 1);
3458 /* Set p2 to the P2 field of the current opcode. Then, assuming that
3459 ** p2 is an instruction address, set variable p2op to the index of that
3460 ** instruction in the aiIndent[] array. p2 and p2op may be different if
3461 ** the current instruction is part of a sub-program generated by an
3462 ** SQL trigger or foreign key. */
3463 int p2
= sqlite3_column_int(pSql
, 3);
3464 int p2op
= (p2
+ (iOp
-iAddr
));
3466 /* Grow the p->aiIndent array as required */
3469 /* Do further verfication that this is explain output. Abort if
3471 static const char *explainCols
[] = {
3472 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
3474 for(jj
=0; jj
<ArraySize(explainCols
); jj
++){
3475 if( cli_strcmp(sqlite3_column_name(pSql
,jj
),explainCols
[jj
])!=0 ){
3477 sqlite3_reset(pSql
);
3483 p
->aiIndent
= (int*)sqlite3_realloc64(p
->aiIndent
, nAlloc
*sizeof(int));
3484 shell_check_oom(p
->aiIndent
);
3485 abYield
= (int*)sqlite3_realloc64(abYield
, nAlloc
*sizeof(int));
3486 shell_check_oom(abYield
);
3488 abYield
[iOp
] = str_in_array(zOp
, azYield
);
3489 p
->aiIndent
[iOp
] = 0;
3492 if( str_in_array(zOp
, azNext
) && p2op
>0 ){
3493 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
3495 if( str_in_array(zOp
, azGoto
) && p2op
<p
->nIndent
3496 && (abYield
[p2op
] || sqlite3_column_int(pSql
, 2))
3498 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
3503 sqlite3_free(abYield
);
3504 sqlite3_reset(pSql
);
3508 ** Free the array allocated by explain_data_prepare().
3510 static void explain_data_delete(ShellState
*p
){
3511 sqlite3_free(p
->aiIndent
);
3518 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3520 static unsigned int savedSelectTrace
;
3521 static unsigned int savedWhereTrace
;
3522 static void disable_debug_trace_modes(void){
3523 unsigned int zero
= 0;
3524 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 0, &savedSelectTrace
);
3525 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &zero
);
3526 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 2, &savedWhereTrace
);
3527 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &zero
);
3529 static void restore_debug_trace_modes(void){
3530 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &savedSelectTrace
);
3531 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &savedWhereTrace
);
3534 /* Create the TEMP table used to store parameter bindings */
3535 static void bind_table_init(ShellState
*p
){
3537 int defensiveMode
= 0;
3538 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, -1, &defensiveMode
);
3539 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, 0, 0);
3540 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, -1, &wrSchema
);
3541 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, 1, 0);
3543 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3544 " key TEXT PRIMARY KEY,\n"
3548 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, wrSchema
, 0);
3549 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, defensiveMode
, 0);
3553 ** Bind parameters on a prepared statement.
3555 ** Parameter bindings are taken from a TEMP table of the form:
3557 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3560 ** No bindings occur if this table does not exist. The name of the table
3561 ** begins with "sqlite_" so that it will not collide with ordinary application
3562 ** tables. The table must be in the TEMP schema.
3564 static void bind_prepared_stmt(ShellState
*pArg
, sqlite3_stmt
*pStmt
){
3568 sqlite3_stmt
*pQ
= 0;
3570 nVar
= sqlite3_bind_parameter_count(pStmt
);
3571 if( nVar
==0 ) return; /* Nothing to do */
3572 if( sqlite3_table_column_metadata(pArg
->db
, "TEMP", "sqlite_parameters",
3573 "key", 0, 0, 0, 0, 0)!=SQLITE_OK
){
3574 rc
= SQLITE_NOTFOUND
;
3577 rc
= sqlite3_prepare_v2(pArg
->db
,
3578 "SELECT value FROM temp.sqlite_parameters"
3579 " WHERE key=?1", -1, &pQ
, 0);
3581 for(i
=1; i
<=nVar
; i
++){
3583 const char *zVar
= sqlite3_bind_parameter_name(pStmt
, i
);
3585 sqlite3_snprintf(sizeof(zNum
),zNum
,"?%d",i
);
3588 sqlite3_bind_text(pQ
, 1, zVar
, -1, SQLITE_STATIC
);
3589 if( rc
==SQLITE_OK
&& pQ
&& sqlite3_step(pQ
)==SQLITE_ROW
){
3590 sqlite3_bind_value(pStmt
, i
, sqlite3_column_value(pQ
, 0));
3592 }else if( sqlite3_strlike("_NAN", zVar
, 0)==0 ){
3593 sqlite3_bind_double(pStmt
, i
, NAN
);
3596 }else if( sqlite3_strlike("_INF", zVar
, 0)==0 ){
3597 sqlite3_bind_double(pStmt
, i
, INFINITY
);
3600 sqlite3_bind_null(pStmt
, i
);
3604 sqlite3_finalize(pQ
);
3608 ** UTF8 box-drawing characters. Imagine box lines like this:
3616 ** Each box characters has between 2 and 4 of the lines leading from
3617 ** the center. The characters are here identified by the numbers of
3618 ** their corresponding lines.
3620 #define BOX_24 "\342\224\200" /* U+2500 --- */
3621 #define BOX_13 "\342\224\202" /* U+2502 | */
3622 #define BOX_23 "\342\224\214" /* U+250c ,- */
3623 #define BOX_34 "\342\224\220" /* U+2510 -, */
3624 #define BOX_12 "\342\224\224" /* U+2514 '- */
3625 #define BOX_14 "\342\224\230" /* U+2518 -' */
3626 #define BOX_123 "\342\224\234" /* U+251c |- */
3627 #define BOX_134 "\342\224\244" /* U+2524 -| */
3628 #define BOX_234 "\342\224\254" /* U+252c -,- */
3629 #define BOX_124 "\342\224\264" /* U+2534 -'- */
3630 #define BOX_1234 "\342\224\274" /* U+253c -|- */
3632 /* Draw horizontal line N characters long using unicode box
3635 static void print_box_line(FILE *out
, int N
){
3636 const char zDash
[] =
3637 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3638 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
;
3639 const int nDash
= sizeof(zDash
) - 1;
3642 utf8_printf(out
, zDash
);
3645 utf8_printf(out
, "%.*s", N
, zDash
);
3649 ** Draw a horizontal separator for a MODE_Box table.
3651 static void print_box_row_separator(
3660 utf8_printf(p
->out
, "%s", zSep1
);
3661 print_box_line(p
->out
, p
->actualWidth
[0]+2);
3662 for(i
=1; i
<nArg
; i
++){
3663 utf8_printf(p
->out
, "%s", zSep2
);
3664 print_box_line(p
->out
, p
->actualWidth
[i
]+2);
3666 utf8_printf(p
->out
, "%s", zSep3
);
3668 fputs("\n", p
->out
);
3672 ** z[] is a line of text that is to be displayed the .mode box or table or
3673 ** similar tabular formats. z[] might contain control characters such
3674 ** as \n, \t, \f, or \r.
3676 ** Compute characters to display on the first line of z[]. Stop at the
3677 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
3678 ** from malloc()) of that first line, which caller should free sometime.
3679 ** Write anything to display on the next line into *pzTail. If this is
3680 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3682 static char *translateForDisplayAndDup(
3683 const unsigned char *z
, /* Input text to be transformed */
3684 const unsigned char **pzTail
, /* OUT: Tail of the input for next line */
3685 int mxWidth
, /* Max width. 0 means no limit */
3686 u8 bWordWrap
/* If true, avoid breaking mid-word */
3688 int i
; /* Input bytes consumed */
3689 int j
; /* Output bytes generated */
3690 int k
; /* Input bytes to be displayed */
3691 int n
; /* Output column number */
3692 unsigned char *zOut
; /* Output text */
3698 if( mxWidth
<0 ) mxWidth
= -mxWidth
;
3699 if( mxWidth
==0 ) mxWidth
= 1000000;
3704 do{ i
++; j
++; }while( (z
[i
]&0xc0)==0x80 );
3711 }while( (n
&7)!=0 && n
<mxWidth
);
3717 if( n
>=mxWidth
&& bWordWrap
){
3718 /* Perhaps try to back up to a better place to break the line */
3719 for(k
=i
; k
>i
/2; k
--){
3720 if( isspace(z
[k
-1]) ) break;
3723 for(k
=i
; k
>i
/2; k
--){
3724 if( isalnum(z
[k
-1])!=isalnum(z
[k
]) && (z
[k
]&0xc0)!=0x80 ) break;
3731 while( z
[i
]==' ' ) i
++;
3736 if( n
>=mxWidth
&& z
[i
]>=' ' ){
3738 }else if( z
[i
]=='\r' && z
[i
+1]=='\n' ){
3739 *pzTail
= z
[i
+2] ? &z
[i
+2] : 0;
3740 }else if( z
[i
]==0 || z
[i
+1]==0 ){
3745 zOut
= malloc( j
+1 );
3746 shell_check_oom(zOut
);
3751 do{ zOut
[j
++] = z
[i
++]; }while( (z
[i
]&0xc0)==0x80 );
3758 }while( (n
&7)!=0 && n
<mxWidth
);
3768 /* Extract the value of the i-th current column for pStmt as an SQL literal
3769 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
3772 static char *quoted_column(sqlite3_stmt
*pStmt
, int i
){
3773 switch( sqlite3_column_type(pStmt
, i
) ){
3775 return sqlite3_mprintf("NULL");
3777 case SQLITE_INTEGER
:
3778 case SQLITE_FLOAT
: {
3779 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt
,i
));
3782 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt
,i
));
3786 sqlite3_str
*pStr
= sqlite3_str_new(0);
3787 const unsigned char *a
= sqlite3_column_blob(pStmt
,i
);
3788 int n
= sqlite3_column_bytes(pStmt
,i
);
3789 sqlite3_str_append(pStr
, "x'", 2);
3791 sqlite3_str_appendf(pStr
, "%02x", a
[j
]);
3793 sqlite3_str_append(pStr
, "'", 1);
3794 return sqlite3_str_finish(pStr
);
3797 return 0; /* Not reached */
3801 ** Run a prepared statement and output the result in one of the
3802 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3805 ** This is different from ordinary exec_prepared_stmt() in that
3806 ** it has to run the entire query and gather the results into memory
3807 ** first, in order to determine column widths, before providing
3810 static void exec_prepared_stmt_columnar(
3811 ShellState
*p
, /* Pointer to ShellState */
3812 sqlite3_stmt
*pStmt
/* Statment to run */
3814 sqlite3_int64 nRow
= 0;
3817 sqlite3_int64 nAlloc
= 0;
3819 const unsigned char *uz
;
3821 char **azQuoted
= 0;
3823 sqlite3_int64 i
, nData
;
3824 int j
, nTotal
, w
, n
;
3825 const char *colSep
= 0;
3826 const char *rowSep
= 0;
3827 const unsigned char **azNextLine
= 0;
3829 int bMultiLineRowExists
= 0;
3830 int bw
= p
->cmOpts
.bWordWrap
;
3831 const char *zEmpty
= "";
3832 const char *zShowNull
= p
->nullValue
;
3834 rc
= sqlite3_step(pStmt
);
3835 if( rc
!=SQLITE_ROW
) return;
3836 nColumn
= sqlite3_column_count(pStmt
);
3838 if( nAlloc
<=0 ) nAlloc
= 1;
3839 azData
= sqlite3_malloc64( nAlloc
*sizeof(char*) );
3840 shell_check_oom(azData
);
3841 azNextLine
= sqlite3_malloc64( nColumn
*sizeof(char*) );
3842 shell_check_oom(azNextLine
);
3843 memset((void*)azNextLine
, 0, nColumn
*sizeof(char*) );
3844 if( p
->cmOpts
.bQuote
){
3845 azQuoted
= sqlite3_malloc64( nColumn
*sizeof(char*) );
3846 shell_check_oom(azQuoted
);
3847 memset(azQuoted
, 0, nColumn
*sizeof(char*) );
3849 abRowDiv
= sqlite3_malloc64( nAlloc
/nColumn
);
3850 shell_check_oom(abRowDiv
);
3851 if( nColumn
>p
->nWidth
){
3852 p
->colWidth
= realloc(p
->colWidth
, (nColumn
+1)*2*sizeof(int));
3853 shell_check_oom(p
->colWidth
);
3854 for(i
=p
->nWidth
; i
<nColumn
; i
++) p
->colWidth
[i
] = 0;
3855 p
->nWidth
= nColumn
;
3856 p
->actualWidth
= &p
->colWidth
[nColumn
];
3858 memset(p
->actualWidth
, 0, nColumn
*sizeof(int));
3859 for(i
=0; i
<nColumn
; i
++){
3862 p
->actualWidth
[i
] = w
;
3864 for(i
=0; i
<nColumn
; i
++){
3865 const unsigned char *zNotUsed
;
3866 int wx
= p
->colWidth
[i
];
3868 wx
= p
->cmOpts
.iWrap
;
3870 if( wx
<0 ) wx
= -wx
;
3871 uz
= (const unsigned char*)sqlite3_column_name(pStmt
,i
);
3872 if( uz
==0 ) uz
= (u8
*)"";
3873 azData
[i
] = translateForDisplayAndDup(uz
, &zNotUsed
, wx
, bw
);
3876 int useNextLine
= bNextLine
;
3878 if( (nRow
+2)*nColumn
>= nAlloc
){
3880 azData
= sqlite3_realloc64(azData
, nAlloc
*sizeof(char*));
3881 shell_check_oom(azData
);
3882 abRowDiv
= sqlite3_realloc64(abRowDiv
, nAlloc
/nColumn
);
3883 shell_check_oom(abRowDiv
);
3887 for(i
=0; i
<nColumn
; i
++){
3888 int wx
= p
->colWidth
[i
];
3890 wx
= p
->cmOpts
.iWrap
;
3892 if( wx
<0 ) wx
= -wx
;
3895 if( uz
==0 ) uz
= (u8
*)zEmpty
;
3896 }else if( p
->cmOpts
.bQuote
){
3897 sqlite3_free(azQuoted
[i
]);
3898 azQuoted
[i
] = quoted_column(pStmt
,i
);
3899 uz
= (const unsigned char*)azQuoted
[i
];
3901 uz
= (const unsigned char*)sqlite3_column_text(pStmt
,i
);
3902 if( uz
==0 ) uz
= (u8
*)zShowNull
;
3904 azData
[nRow
*nColumn
+ i
]
3905 = translateForDisplayAndDup(uz
, &azNextLine
[i
], wx
, bw
);
3906 if( azNextLine
[i
] ){
3908 abRowDiv
[nRow
-1] = 0;
3909 bMultiLineRowExists
= 1;
3912 }while( bNextLine
|| sqlite3_step(pStmt
)==SQLITE_ROW
);
3913 nTotal
= nColumn
*(nRow
+1);
3914 for(i
=0; i
<nTotal
; i
++){
3916 if( z
==0 ) z
= (char*)zEmpty
;
3919 if( n
>p
->actualWidth
[j
] ) p
->actualWidth
[j
] = n
;
3921 if( seenInterrupt
) goto columnar_end
;
3922 if( nColumn
==0 ) goto columnar_end
;
3927 if( p
->showHeader
){
3928 for(i
=0; i
<nColumn
; i
++){
3929 w
= p
->actualWidth
[i
];
3930 if( p
->colWidth
[i
]<0 ) w
= -w
;
3931 utf8_width_print(p
->out
, w
, azData
[i
]);
3932 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
3934 for(i
=0; i
<nColumn
; i
++){
3935 print_dashes(p
->out
, p
->actualWidth
[i
]);
3936 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
3944 print_row_separator(p
, nColumn
, "+");
3945 fputs("| ", p
->out
);
3946 for(i
=0; i
<nColumn
; i
++){
3947 w
= p
->actualWidth
[i
];
3948 n
= strlenChar(azData
[i
]);
3949 utf8_printf(p
->out
, "%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
3950 fputs(i
==nColumn
-1?" |\n":" | ", p
->out
);
3952 print_row_separator(p
, nColumn
, "+");
3955 case MODE_Markdown
: {
3958 fputs("| ", p
->out
);
3959 for(i
=0; i
<nColumn
; i
++){
3960 w
= p
->actualWidth
[i
];
3961 n
= strlenChar(azData
[i
]);
3962 utf8_printf(p
->out
, "%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
3963 fputs(i
==nColumn
-1?" |\n":" | ", p
->out
);
3965 print_row_separator(p
, nColumn
, "|");
3969 colSep
= " " BOX_13
" ";
3970 rowSep
= " " BOX_13
"\n";
3971 print_box_row_separator(p
, nColumn
, BOX_23
, BOX_234
, BOX_34
);
3972 utf8_printf(p
->out
, BOX_13
" ");
3973 for(i
=0; i
<nColumn
; i
++){
3974 w
= p
->actualWidth
[i
];
3975 n
= strlenChar(azData
[i
]);
3976 utf8_printf(p
->out
, "%*s%s%*s%s",
3977 (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "",
3978 i
==nColumn
-1?" "BOX_13
"\n":" "BOX_13
" ");
3980 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
3984 for(i
=nColumn
, j
=0; i
<nTotal
; i
++, j
++){
3985 if( j
==0 && p
->cMode
!=MODE_Column
){
3986 utf8_printf(p
->out
, "%s", p
->cMode
==MODE_Box
?BOX_13
" ":"| ");
3989 if( z
==0 ) z
= p
->nullValue
;
3990 w
= p
->actualWidth
[j
];
3991 if( p
->colWidth
[j
]<0 ) w
= -w
;
3992 utf8_width_print(p
->out
, w
, z
);
3994 utf8_printf(p
->out
, "%s", rowSep
);
3995 if( bMultiLineRowExists
&& abRowDiv
[i
/nColumn
-1] && i
+1<nTotal
){
3996 if( p
->cMode
==MODE_Table
){
3997 print_row_separator(p
, nColumn
, "+");
3998 }else if( p
->cMode
==MODE_Box
){
3999 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
4000 }else if( p
->cMode
==MODE_Column
){
4001 raw_printf(p
->out
, "\n");
4005 if( seenInterrupt
) goto columnar_end
;
4007 utf8_printf(p
->out
, "%s", colSep
);
4010 if( p
->cMode
==MODE_Table
){
4011 print_row_separator(p
, nColumn
, "+");
4012 }else if( p
->cMode
==MODE_Box
){
4013 print_box_row_separator(p
, nColumn
, BOX_12
, BOX_124
, BOX_14
);
4016 if( seenInterrupt
){
4017 utf8_printf(p
->out
, "Interrupt\n");
4019 nData
= (nRow
+1)*nColumn
;
4020 for(i
=0; i
<nData
; i
++){
4022 if( z
!=zEmpty
&& z
!=zShowNull
) free(azData
[i
]);
4024 sqlite3_free(azData
);
4025 sqlite3_free((void*)azNextLine
);
4026 sqlite3_free(abRowDiv
);
4028 for(i
=0; i
<nColumn
; i
++) sqlite3_free(azQuoted
[i
]);
4029 sqlite3_free(azQuoted
);
4034 ** Run a prepared statement
4036 static void exec_prepared_stmt(
4037 ShellState
*pArg
, /* Pointer to ShellState */
4038 sqlite3_stmt
*pStmt
/* Statment to run */
4041 sqlite3_uint64 nRow
= 0;
4043 if( pArg
->cMode
==MODE_Column
4044 || pArg
->cMode
==MODE_Table
4045 || pArg
->cMode
==MODE_Box
4046 || pArg
->cMode
==MODE_Markdown
4048 exec_prepared_stmt_columnar(pArg
, pStmt
);
4052 /* perform the first step. this will tell us if we
4053 ** have a result set or not and how wide it is.
4055 rc
= sqlite3_step(pStmt
);
4056 /* if we have a result set... */
4057 if( SQLITE_ROW
== rc
){
4058 /* allocate space for col name ptr, value ptr, and type */
4059 int nCol
= sqlite3_column_count(pStmt
);
4060 void *pData
= sqlite3_malloc64(3*nCol
*sizeof(const char*) + 1);
4062 shell_out_of_memory();
4064 char **azCols
= (char **)pData
; /* Names of result columns */
4065 char **azVals
= &azCols
[nCol
]; /* Results */
4066 int *aiTypes
= (int *)&azVals
[nCol
]; /* Result types */
4068 assert(sizeof(int) <= sizeof(char *));
4069 /* save off ptrs to column names */
4070 for(i
=0; i
<nCol
; i
++){
4071 azCols
[i
] = (char *)sqlite3_column_name(pStmt
, i
);
4075 /* extract the data and data types */
4076 for(i
=0; i
<nCol
; i
++){
4077 aiTypes
[i
] = x
= sqlite3_column_type(pStmt
, i
);
4080 && (pArg
->cMode
==MODE_Insert
|| pArg
->cMode
==MODE_Quote
)
4084 azVals
[i
] = (char*)sqlite3_column_text(pStmt
, i
);
4086 if( !azVals
[i
] && (aiTypes
[i
]!=SQLITE_NULL
) ){
4088 break; /* from for */
4092 /* if data and types extracted successfully... */
4093 if( SQLITE_ROW
== rc
){
4094 /* call the supplied callback with the result row data */
4095 if( shell_callback(pArg
, nCol
, azVals
, azCols
, aiTypes
) ){
4098 rc
= sqlite3_step(pStmt
);
4101 } while( SQLITE_ROW
== rc
);
4102 sqlite3_free(pData
);
4103 if( pArg
->cMode
==MODE_Json
){
4104 fputs("]\n", pArg
->out
);
4105 }else if( pArg
->cMode
==MODE_Count
){
4107 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%llu row%s\n",
4108 nRow
, nRow
!=1 ? "s" : "");
4115 #ifndef SQLITE_OMIT_VIRTUALTABLE
4117 ** This function is called to process SQL if the previous shell command
4118 ** was ".expert". It passes the SQL in the second argument directly to
4119 ** the sqlite3expert object.
4121 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4122 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4123 ** an English language error message. It is the responsibility of the
4124 ** caller to eventually free this buffer using sqlite3_free().
4126 static int expertHandleSQL(
4131 assert( pState
->expert
.pExpert
);
4132 assert( pzErr
==0 || *pzErr
==0 );
4133 return sqlite3_expert_sql(pState
->expert
.pExpert
, zSql
, pzErr
);
4137 ** This function is called either to silently clean up the object
4138 ** created by the ".expert" command (if bCancel==1), or to generate a
4139 ** report from it and then clean it up (if bCancel==0).
4141 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4142 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4143 ** an English language error message. It is the responsibility of the
4144 ** caller to eventually free this buffer using sqlite3_free().
4146 static int expertFinish(
4152 sqlite3expert
*p
= pState
->expert
.pExpert
;
4154 assert( bCancel
|| pzErr
==0 || *pzErr
==0 );
4156 FILE *out
= pState
->out
;
4157 int bVerbose
= pState
->expert
.bVerbose
;
4159 rc
= sqlite3_expert_analyze(p
, pzErr
);
4160 if( rc
==SQLITE_OK
){
4161 int nQuery
= sqlite3_expert_count(p
);
4165 const char *zCand
= sqlite3_expert_report(p
,0,EXPERT_REPORT_CANDIDATES
);
4166 raw_printf(out
, "-- Candidates -----------------------------\n");
4167 raw_printf(out
, "%s\n", zCand
);
4169 for(i
=0; i
<nQuery
; i
++){
4170 const char *zSql
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_SQL
);
4171 const char *zIdx
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_INDEXES
);
4172 const char *zEQP
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_PLAN
);
4173 if( zIdx
==0 ) zIdx
= "(no new indexes)\n";
4175 raw_printf(out
, "-- Query %d --------------------------------\n",i
+1);
4176 raw_printf(out
, "%s\n\n", zSql
);
4178 raw_printf(out
, "%s\n", zIdx
);
4179 raw_printf(out
, "%s\n", zEQP
);
4183 sqlite3_expert_destroy(p
);
4184 pState
->expert
.pExpert
= 0;
4189 ** Implementation of ".expert" dot command.
4191 static int expertDotCommand(
4192 ShellState
*pState
, /* Current shell tool state */
4193 char **azArg
, /* Array of arguments passed to dot command */
4194 int nArg
/* Number of entries in azArg[] */
4201 assert( pState
->expert
.pExpert
==0 );
4202 memset(&pState
->expert
, 0, sizeof(ExpertInfo
));
4204 for(i
=1; rc
==SQLITE_OK
&& i
<nArg
; i
++){
4207 if( z
[0]=='-' && z
[1]=='-' ) z
++;
4209 if( n
>=2 && 0==cli_strncmp(z
, "-verbose", n
) ){
4210 pState
->expert
.bVerbose
= 1;
4212 else if( n
>=2 && 0==cli_strncmp(z
, "-sample", n
) ){
4214 raw_printf(stderr
, "option requires an argument: %s\n", z
);
4217 iSample
= (int)integerValue(azArg
[++i
]);
4218 if( iSample
<0 || iSample
>100 ){
4219 raw_printf(stderr
, "value out of range: %s\n", azArg
[i
]);
4225 raw_printf(stderr
, "unknown option: %s\n", z
);
4230 if( rc
==SQLITE_OK
){
4231 pState
->expert
.pExpert
= sqlite3_expert_new(pState
->db
, &zErr
);
4232 if( pState
->expert
.pExpert
==0 ){
4233 raw_printf(stderr
, "sqlite3_expert_new: %s\n",
4234 zErr
? zErr
: "out of memory");
4237 sqlite3_expert_config(
4238 pState
->expert
.pExpert
, EXPERT_CONFIG_SAMPLE
, iSample
4246 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
4249 ** Execute a statement or set of statements. Print
4250 ** any result rows/columns depending on the current mode
4251 ** set via the supplied callback.
4253 ** This is very similar to SQLite's built-in sqlite3_exec()
4254 ** function except it takes a slightly different callback
4255 ** and callback data argument.
4257 static int shell_exec(
4258 ShellState
*pArg
, /* Pointer to ShellState */
4259 const char *zSql
, /* SQL to be evaluated */
4260 char **pzErrMsg
/* Error msg written here */
4262 sqlite3_stmt
*pStmt
= NULL
; /* Statement to execute. */
4263 int rc
= SQLITE_OK
; /* Return Code */
4265 const char *zLeftover
; /* Tail of unprocessed SQL */
4266 sqlite3
*db
= pArg
->db
;
4272 #ifndef SQLITE_OMIT_VIRTUALTABLE
4273 if( pArg
->expert
.pExpert
){
4274 rc
= expertHandleSQL(pArg
, zSql
, pzErrMsg
);
4275 return expertFinish(pArg
, (rc
!=SQLITE_OK
), pzErrMsg
);
4279 while( zSql
[0] && (SQLITE_OK
== rc
) ){
4280 static const char *zStmtSql
;
4281 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zLeftover
);
4282 if( SQLITE_OK
!= rc
){
4284 *pzErrMsg
= save_err_msg(db
, "in prepare", rc
, zSql
);
4288 /* this happens for a comment or white-space */
4290 while( IsSpace(zSql
[0]) ) zSql
++;
4293 zStmtSql
= sqlite3_sql(pStmt
);
4294 if( zStmtSql
==0 ) zStmtSql
= "";
4295 while( IsSpace(zStmtSql
[0]) ) zStmtSql
++;
4297 /* save off the prepared statment handle and reset row count */
4299 pArg
->pStmt
= pStmt
;
4303 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
4304 if( pArg
&& pArg
->autoEQP
&& sqlite3_stmt_isexplain(pStmt
)==0 ){
4305 sqlite3_stmt
*pExplain
;
4308 disable_debug_trace_modes();
4309 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, -1, &triggerEQP
);
4310 if( pArg
->autoEQP
>=AUTOEQP_trigger
){
4311 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, 0);
4313 zEQP
= sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql
);
4314 shell_check_oom(zEQP
);
4315 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
4316 if( rc
==SQLITE_OK
){
4317 while( sqlite3_step(pExplain
)==SQLITE_ROW
){
4318 const char *zEQPLine
= (const char*)sqlite3_column_text(pExplain
,3);
4319 int iEqpId
= sqlite3_column_int(pExplain
, 0);
4320 int iParentId
= sqlite3_column_int(pExplain
, 1);
4321 if( zEQPLine
==0 ) zEQPLine
= "";
4322 if( zEQPLine
[0]=='-' ) eqp_render(pArg
, 0);
4323 eqp_append(pArg
, iEqpId
, iParentId
, zEQPLine
);
4325 eqp_render(pArg
, 0);
4327 sqlite3_finalize(pExplain
);
4329 if( pArg
->autoEQP
>=AUTOEQP_full
){
4330 /* Also do an EXPLAIN for ".eqp full" mode */
4331 zEQP
= sqlite3_mprintf("EXPLAIN %s", zStmtSql
);
4332 shell_check_oom(zEQP
);
4333 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
4334 if( rc
==SQLITE_OK
){
4335 pArg
->cMode
= MODE_Explain
;
4336 explain_data_prepare(pArg
, pExplain
);
4337 exec_prepared_stmt(pArg
, pExplain
);
4338 explain_data_delete(pArg
);
4340 sqlite3_finalize(pExplain
);
4343 if( pArg
->autoEQP
>=AUTOEQP_trigger
&& triggerEQP
==0 ){
4344 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 0, 0);
4345 /* Reprepare pStmt before reactiving trace modes */
4346 sqlite3_finalize(pStmt
);
4347 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
4348 if( pArg
) pArg
->pStmt
= pStmt
;
4350 restore_debug_trace_modes();
4354 pArg
->cMode
= pArg
->mode
;
4355 if( pArg
->autoExplain
){
4356 if( sqlite3_stmt_isexplain(pStmt
)==1 ){
4357 pArg
->cMode
= MODE_Explain
;
4359 if( sqlite3_stmt_isexplain(pStmt
)==2 ){
4360 pArg
->cMode
= MODE_EQP
;
4364 /* If the shell is currently in ".explain" mode, gather the extra
4365 ** data required to add indents to the output.*/
4366 if( pArg
->cMode
==MODE_Explain
){
4367 explain_data_prepare(pArg
, pStmt
);
4371 bind_prepared_stmt(pArg
, pStmt
);
4372 exec_prepared_stmt(pArg
, pStmt
);
4373 explain_data_delete(pArg
);
4374 eqp_render(pArg
, 0);
4376 /* print usage stats if stats on */
4377 if( pArg
&& pArg
->statsOn
){
4378 display_stats(db
, pArg
, 0);
4381 /* print loop-counters if required */
4382 if( pArg
&& pArg
->scanstatsOn
){
4383 display_scanstats(db
, pArg
);
4386 /* Finalize the statement just executed. If this fails, save a
4387 ** copy of the error message. Otherwise, set zSql to point to the
4388 ** next statement to execute. */
4389 rc2
= sqlite3_finalize(pStmt
);
4390 if( rc
!=SQLITE_NOMEM
) rc
= rc2
;
4391 if( rc
==SQLITE_OK
){
4393 while( IsSpace(zSql
[0]) ) zSql
++;
4394 }else if( pzErrMsg
){
4395 *pzErrMsg
= save_err_msg(db
, "stepping", rc
, 0);
4398 /* clear saved stmt handle */
4409 ** Release memory previously allocated by tableColumnList().
4411 static void freeColumnList(char **azCol
){
4413 for(i
=1; azCol
[i
]; i
++){
4414 sqlite3_free(azCol
[i
]);
4416 /* azCol[0] is a static string */
4417 sqlite3_free(azCol
);
4421 ** Return a list of pointers to strings which are the names of all
4422 ** columns in table zTab. The memory to hold the names is dynamically
4423 ** allocated and must be released by the caller using a subsequent call
4424 ** to freeColumnList().
4426 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
4427 ** value that needs to be preserved, then azCol[0] is filled in with the
4428 ** name of the rowid column.
4430 ** The first regular column in the table is azCol[1]. The list is terminated
4431 ** by an entry with azCol[i]==0.
4433 static char **tableColumnList(ShellState
*p
, const char *zTab
){
4435 sqlite3_stmt
*pStmt
;
4439 int nPK
= 0; /* Number of PRIMARY KEY columns seen */
4440 int isIPK
= 0; /* True if one PRIMARY KEY column of type INTEGER */
4441 int preserveRowid
= ShellHasFlag(p
, SHFLG_PreserveRowid
);
4444 zSql
= sqlite3_mprintf("PRAGMA table_info=%Q", zTab
);
4445 shell_check_oom(zSql
);
4446 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
4449 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
4450 if( nCol
>=nAlloc
-2 ){
4451 nAlloc
= nAlloc
*2 + nCol
+ 10;
4452 azCol
= sqlite3_realloc(azCol
, nAlloc
*sizeof(azCol
[0]));
4453 shell_check_oom(azCol
);
4455 azCol
[++nCol
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 1));
4456 shell_check_oom(azCol
[nCol
]);
4457 if( sqlite3_column_int(pStmt
, 5) ){
4460 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt
,2),
4469 sqlite3_finalize(pStmt
);
4470 if( azCol
==0 ) return 0;
4474 /* The decision of whether or not a rowid really needs to be preserved
4475 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
4476 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
4477 ** rowids on tables where the rowid is inaccessible because there are other
4478 ** columns in the table named "rowid", "_rowid_", and "oid".
4480 if( preserveRowid
&& isIPK
){
4481 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4482 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
4483 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4484 ** ROWID aliases. To distinguish these cases, check to see if
4485 ** there is a "pk" entry in "PRAGMA index_list". There will be
4486 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4488 zSql
= sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4489 " WHERE origin='pk'", zTab
);
4490 shell_check_oom(zSql
);
4491 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
4494 freeColumnList(azCol
);
4497 rc
= sqlite3_step(pStmt
);
4498 sqlite3_finalize(pStmt
);
4499 preserveRowid
= rc
==SQLITE_ROW
;
4501 if( preserveRowid
){
4502 /* Only preserve the rowid if we can find a name to use for the
4504 static char *azRowid
[] = { "rowid", "_rowid_", "oid" };
4507 for(i
=1; i
<=nCol
; i
++){
4508 if( sqlite3_stricmp(azRowid
[j
],azCol
[i
])==0 ) break;
4511 /* At this point, we know that azRowid[j] is not the name of any
4512 ** ordinary column in the table. Verify that azRowid[j] is a valid
4513 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
4514 ** tables will fail this last check */
4515 rc
= sqlite3_table_column_metadata(p
->db
,0,zTab
,azRowid
[j
],0,0,0,0,0);
4516 if( rc
==SQLITE_OK
) azCol
[0] = azRowid
[j
];
4525 ** Toggle the reverse_unordered_selects setting.
4527 static void toggleSelectOrder(sqlite3
*db
){
4528 sqlite3_stmt
*pStmt
= 0;
4531 sqlite3_prepare_v2(db
, "PRAGMA reverse_unordered_selects", -1, &pStmt
, 0);
4532 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
4533 iSetting
= sqlite3_column_int(pStmt
, 0);
4535 sqlite3_finalize(pStmt
);
4536 sqlite3_snprintf(sizeof(zStmt
), zStmt
,
4537 "PRAGMA reverse_unordered_selects(%d)", !iSetting
);
4538 sqlite3_exec(db
, zStmt
, 0, 0, 0);
4542 ** This is a different callback routine used for dumping the database.
4543 ** Each row received by this callback consists of a table name,
4544 ** the table type ("index" or "table") and SQL to create the table.
4545 ** This routine should print text sufficient to recreate the table.
4547 static int dump_callback(void *pArg
, int nArg
, char **azArg
, char **azNotUsed
){
4552 ShellState
*p
= (ShellState
*)pArg
;
4556 UNUSED_PARAMETER(azNotUsed
);
4557 if( nArg
!=3 || azArg
==0 ) return 0;
4561 if( zTable
==0 ) return 0;
4562 if( zType
==0 ) return 0;
4563 dataOnly
= (p
->shellFlgs
& SHFLG_DumpDataOnly
)!=0;
4564 noSys
= (p
->shellFlgs
& SHFLG_DumpNoSys
)!=0;
4566 if( cli_strcmp(zTable
, "sqlite_sequence")==0 && !noSys
){
4567 if( !dataOnly
) raw_printf(p
->out
, "DELETE FROM sqlite_sequence;\n");
4568 }else if( sqlite3_strglob("sqlite_stat?", zTable
)==0 && !noSys
){
4569 if( !dataOnly
) raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
4570 }else if( cli_strncmp(zTable
, "sqlite_", 7)==0 ){
4572 }else if( dataOnly
){
4574 }else if( cli_strncmp(zSql
, "CREATE VIRTUAL TABLE", 20)==0 ){
4576 if( !p
->writableSchema
){
4577 raw_printf(p
->out
, "PRAGMA writable_schema=ON;\n");
4578 p
->writableSchema
= 1;
4580 zIns
= sqlite3_mprintf(
4581 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4582 "VALUES('table','%q','%q',0,'%q');",
4583 zTable
, zTable
, zSql
);
4584 shell_check_oom(zIns
);
4585 utf8_printf(p
->out
, "%s\n", zIns
);
4589 printSchemaLine(p
->out
, zSql
, ";\n");
4592 if( cli_strcmp(zType
, "table")==0 ){
4597 char *savedDestTable
;
4600 azCol
= tableColumnList(p
, zTable
);
4606 /* Always quote the table name, even if it appears to be pure ascii,
4607 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
4609 appendText(&sTable
, zTable
, quoteChar(zTable
));
4610 /* If preserving the rowid, add a column list after the table name.
4611 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4612 ** instead of the usual "INSERT INTO tab VALUES(...)".
4615 appendText(&sTable
, "(", 0);
4616 appendText(&sTable
, azCol
[0], 0);
4617 for(i
=1; azCol
[i
]; i
++){
4618 appendText(&sTable
, ",", 0);
4619 appendText(&sTable
, azCol
[i
], quoteChar(azCol
[i
]));
4621 appendText(&sTable
, ")", 0);
4624 /* Build an appropriate SELECT statement */
4626 appendText(&sSelect
, "SELECT ", 0);
4628 appendText(&sSelect
, azCol
[0], 0);
4629 appendText(&sSelect
, ",", 0);
4631 for(i
=1; azCol
[i
]; i
++){
4632 appendText(&sSelect
, azCol
[i
], quoteChar(azCol
[i
]));
4634 appendText(&sSelect
, ",", 0);
4637 freeColumnList(azCol
);
4638 appendText(&sSelect
, " FROM ", 0);
4639 appendText(&sSelect
, zTable
, quoteChar(zTable
));
4641 savedDestTable
= p
->zDestTable
;
4642 savedMode
= p
->mode
;
4643 p
->zDestTable
= sTable
.z
;
4644 p
->mode
= p
->cMode
= MODE_Insert
;
4645 rc
= shell_exec(p
, sSelect
.z
, 0);
4646 if( (rc
&0xff)==SQLITE_CORRUPT
){
4647 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
4648 toggleSelectOrder(p
->db
);
4649 shell_exec(p
, sSelect
.z
, 0);
4650 toggleSelectOrder(p
->db
);
4652 p
->zDestTable
= savedDestTable
;
4653 p
->mode
= savedMode
;
4662 ** Run zQuery. Use dump_callback() as the callback routine so that
4663 ** the contents of the query are output as SQL statements.
4665 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
4666 ** "ORDER BY rowid DESC" to the end.
4668 static int run_schema_dump_query(
4674 rc
= sqlite3_exec(p
->db
, zQuery
, dump_callback
, p
, &zErr
);
4675 if( rc
==SQLITE_CORRUPT
){
4677 int len
= strlen30(zQuery
);
4678 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
4680 utf8_printf(p
->out
, "/****** %s ******/\n", zErr
);
4684 zQ2
= malloc( len
+100 );
4685 if( zQ2
==0 ) return rc
;
4686 sqlite3_snprintf(len
+100, zQ2
, "%s ORDER BY rowid DESC", zQuery
);
4687 rc
= sqlite3_exec(p
->db
, zQ2
, dump_callback
, p
, &zErr
);
4689 utf8_printf(p
->out
, "/****** ERROR: %s ******/\n", zErr
);
4691 rc
= SQLITE_CORRUPT
;
4700 ** Text of help messages.
4702 ** The help text for each individual command begins with a line that starts
4703 ** with ".". Subsequent lines are supplemental information.
4705 ** There must be two or more spaces between the end of the command and the
4706 ** start of the description of what that command does.
4708 static const char *(azHelp
[]) = {
4709 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4710 && !defined(SQLITE_SHELL_FIDDLE)
4711 ".archive ... Manage SQL archives",
4712 " Each command must have exactly one of the following options:",
4713 " -c, --create Create a new archive",
4714 " -u, --update Add or update files with changed mtime",
4715 " -i, --insert Like -u but always add even if unchanged",
4716 " -r, --remove Remove files from archive",
4717 " -t, --list List contents of archive",
4718 " -x, --extract Extract files from archive",
4719 " Optional arguments:",
4720 " -v, --verbose Print each filename as it is processed",
4721 " -f FILE, --file FILE Use archive FILE (default is current db)",
4722 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
4723 " -C DIR, --directory DIR Read/extract files from directory DIR",
4724 " -g, --glob Use glob matching for names in archive",
4725 " -n, --dryrun Show the SQL that would have occurred",
4727 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
4728 " .ar -tf ARCHIVE # List members of ARCHIVE",
4729 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
4731 " http://sqlite.org/cli.html#sqlite_archive_support",
4733 #ifndef SQLITE_OMIT_AUTHORIZATION
4734 ".auth ON|OFF Show authorizer callbacks",
4736 #ifndef SQLITE_SHELL_FIDDLE
4737 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
4739 " --append Use the appendvfs",
4740 " --async Write to FILE without journal and fsync()",
4742 ".bail on|off Stop after hitting an error. Default OFF",
4743 ".binary on|off Turn binary output on or off. Default OFF",
4744 #ifndef SQLITE_SHELL_FIDDLE
4745 ".cd DIRECTORY Change the working directory to DIRECTORY",
4747 ".changes on|off Show number of rows changed by SQL",
4748 #ifndef SQLITE_SHELL_FIDDLE
4749 ".check GLOB Fail if output since .testcase does not match",
4750 ".clone NEWDB Clone data into NEWDB from the existing database",
4752 ".connection [close] [#] Open or close an auxiliary database connection",
4753 ".databases List names and files of attached databases",
4754 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
4755 #if SQLITE_SHELL_HAVE_RECOVER
4756 ".dbinfo ?DB? Show status information about the database",
4758 ".dump ?OBJECTS? Render database content as SQL",
4760 " --data-only Output only INSERT statements",
4761 " --newlines Allow unescaped newline characters in output",
4762 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
4763 " --preserve-rowids Include ROWID values in the output",
4764 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4765 " Additional LIKE patterns can be given in subsequent arguments",
4766 ".echo on|off Turn command echo on or off",
4767 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
4770 " test Show raw EXPLAIN QUERY PLAN output",
4771 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4773 " trigger Like \"full\" but also show trigger bytecode",
4774 #ifndef SQLITE_SHELL_FIDDLE
4775 ".excel Display the output of next command in spreadsheet",
4776 " --bom Put a UTF8 byte-order mark on intermediate file",
4778 #ifndef SQLITE_SHELL_FIDDLE
4779 ".exit ?CODE? Exit this program with return-code CODE",
4781 ".expert EXPERIMENTAL. Suggest indexes for queries",
4782 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
4783 ".filectrl CMD ... Run various sqlite3_file_control() operations",
4784 " --schema SCHEMA Use SCHEMA instead of \"main\"",
4785 " --help Show CMD details",
4786 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
4787 ".headers on|off Turn display of headers on or off",
4788 ".help ?-all? ?PATTERN? Show help text for PATTERN",
4789 #ifndef SQLITE_SHELL_FIDDLE
4790 ".import FILE TABLE Import data from FILE into TABLE",
4792 " --ascii Use \\037 and \\036 as column and row separators",
4793 " --csv Use , and \\n as column and row separators",
4794 " --skip N Skip the first N rows of input",
4795 " --schema S Target table to be S.TABLE",
4796 " -v \"Verbose\" - increase auxiliary output",
4798 " * If TABLE does not exist, it is created. The first row of input",
4799 " determines the column names.",
4800 " * If neither --csv or --ascii are used, the input mode is derived",
4801 " from the \".mode\" output mode",
4802 " * If FILE begins with \"|\" then it is a command that generates the",
4805 #ifndef SQLITE_OMIT_TEST_CONTROL
4806 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
4808 ".indexes ?TABLE? Show names of indexes",
4809 " If TABLE is specified, only show indexes for",
4810 " tables matching TABLE using the LIKE operator.",
4811 #ifdef SQLITE_ENABLE_IOTRACE
4812 ",iotrace FILE Enable I/O diagnostic logging to FILE",
4814 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
4815 ".lint OPTIONS Report potential schema issues.",
4817 " fkey-indexes Find missing foreign key indexes",
4818 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4819 ".load FILE ?ENTRY? Load an extension library",
4821 #if !defined(SQLITE_SHELL_FIDDLE)
4822 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
4824 ".log on|off Turn logging on or off.",
4826 ".mode MODE ?OPTIONS? Set output mode",
4828 " ascii Columns/rows delimited by 0x1F and 0x1E",
4829 " box Tables using unicode box-drawing characters",
4830 " csv Comma-separated values",
4831 " column Output in columns. (See .width)",
4832 " html HTML <table> code",
4833 " insert SQL insert statements for TABLE",
4834 " json Results in a JSON array",
4835 " line One value per line",
4836 " list Values delimited by \"|\"",
4837 " markdown Markdown table format",
4838 " qbox Shorthand for \"box --wrap 60 --quote\"",
4839 " quote Escape answers as for SQL",
4840 " table ASCII-art table",
4841 " tabs Tab-separated values",
4842 " tcl TCL list elements",
4843 " OPTIONS: (for columnar modes or insert mode):",
4844 " --wrap N Wrap output lines to no longer than N characters",
4845 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
4846 " --ww Shorthand for \"--wordwrap 1\"",
4847 " --quote Quote output text as SQL literals",
4848 " --noquote Do not quote output text",
4849 " TABLE The name of SQL table used for \"insert\" mode",
4850 #ifndef SQLITE_SHELL_FIDDLE
4851 ".nonce STRING Suspend safe mode for one command if nonce matches",
4853 ".nullvalue STRING Use STRING in place of NULL values",
4854 #ifndef SQLITE_SHELL_FIDDLE
4855 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
4856 " If FILE begins with '|' then open as a pipe",
4857 " --bom Put a UTF8 byte-order mark at the beginning",
4858 " -e Send output to the system text editor",
4859 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
4860 /* Note that .open is (partially) available in WASM builds but is
4861 ** currently only intended to be used by the fiddle tool, not
4862 ** end users, so is "undocumented." */
4863 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
4865 " --append Use appendvfs to append database to the end of FILE",
4867 #ifndef SQLITE_OMIT_DESERIALIZE
4868 " --deserialize Load into memory using sqlite3_deserialize()",
4869 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
4870 " --maxsize N Maximum size for --hexdb or --deserialized database",
4872 " --new Initialize FILE to an empty database",
4873 " --nofollow Do not follow symbolic links",
4874 " --readonly Open FILE readonly",
4875 " --zip FILE is a ZIP archive",
4876 #ifndef SQLITE_SHELL_FIDDLE
4877 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
4878 " If FILE begins with '|' then open it as a pipe.",
4880 " --bom Prefix output with a UTF8 byte-order mark",
4881 " -e Send output to the system text editor",
4882 " -x Send output as CSV to a spreadsheet",
4884 ".parameter CMD ... Manage SQL parameter bindings",
4885 " clear Erase all bindings",
4886 " init Initialize the TEMP table that holds bindings",
4887 " list List the current parameter bindings",
4888 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
4889 " PARAMETER should start with one of: $ : @ ?",
4890 " unset PARAMETER Remove PARAMETER from the binding table",
4891 ".print STRING... Print literal STRING",
4892 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
4893 ".progress N Invoke progress handler after every N opcodes",
4894 " --limit N Interrupt after N progress callbacks",
4895 " --once Do no more than one progress interrupt",
4896 " --quiet|-q No output except at interrupts",
4897 " --reset Reset the count for each input and interrupt",
4899 ".prompt MAIN CONTINUE Replace the standard prompts",
4900 #ifndef SQLITE_SHELL_FIDDLE
4901 ".quit Stop interpreting input stream, exit if primary.",
4902 ".read FILE Read input from FILE or command output",
4903 " If FILE begins with \"|\", it is a command that generates the input.",
4905 #if SQLITE_SHELL_HAVE_RECOVER
4906 ".recover Recover as much data as possible from corrupt db.",
4907 " --ignore-freelist Ignore pages that appear to be on db freelist",
4908 " --lost-and-found TABLE Alternative name for the lost-and-found table",
4909 " --no-rowids Do not attempt to recover rowid values",
4910 " that are not also INTEGER PRIMARY KEYs",
4912 #ifndef SQLITE_SHELL_FIDDLE
4913 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
4914 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
4916 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
4917 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
4919 " --indent Try to pretty-print the schema",
4920 " --nosys Omit objects whose names start with \"sqlite_\"",
4921 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
4923 " --init Create a new SELFTEST table",
4924 " -v Verbose output",
4925 ".separator COL ?ROW? Change the column and row separators",
4926 #if defined(SQLITE_ENABLE_SESSION)
4927 ".session ?NAME? CMD ... Create or control sessions",
4929 " attach TABLE Attach TABLE",
4930 " changeset FILE Write a changeset into FILE",
4931 " close Close one session",
4932 " enable ?BOOLEAN? Set or query the enable bit",
4933 " filter GLOB... Reject tables matching GLOBs",
4934 " indirect ?BOOLEAN? Mark or query the indirect status",
4935 " isempty Query whether the session is empty",
4936 " list List currently open session names",
4937 " open DB NAME Open a new session on DB",
4938 " patchset FILE Write a patchset into FILE",
4939 " If ?NAME? is omitted, the first defined session is used.",
4941 ".sha3sum ... Compute a SHA3 hash of database content",
4943 " --schema Also hash the sqlite_schema table",
4944 " --sha3-224 Use the sha3-224 algorithm",
4945 " --sha3-256 Use the sha3-256 algorithm (default)",
4946 " --sha3-384 Use the sha3-384 algorithm",
4947 " --sha3-512 Use the sha3-512 algorithm",
4948 " Any other argument is a LIKE pattern for tables to hash",
4949 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4950 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
4952 ".show Show the current values for various settings",
4953 ".stats ?ARG? Show stats or turn stats on or off",
4954 " off Turn off automatic stat display",
4955 " on Turn on automatic stat display",
4956 " stmt Show statement stats",
4957 " vmstep Show the virtual machine step count only",
4958 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
4959 ".system CMD ARGS... Run CMD ARGS... in a system shell",
4961 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
4962 #ifndef SQLITE_SHELL_FIDDLE
4963 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
4965 ",testctrl CMD ... Run various sqlite3_test_control() operations",
4966 " Run \".testctrl\" with no arguments for details",
4967 ".timeout MS Try opening locked tables for MS milliseconds",
4968 ".timer on|off Turn SQL timer on or off",
4969 #ifndef SQLITE_OMIT_TRACE
4970 ".trace ?OPTIONS? Output each SQL statement as it is run",
4971 " FILE Send output to FILE",
4972 " stdout Send output to stdout",
4973 " stderr Send output to stderr",
4974 " off Disable tracing",
4975 " --expanded Expand query parameters",
4976 #ifdef SQLITE_ENABLE_NORMALIZE
4977 " --normalized Normal the SQL statements",
4979 " --plain Show SQL as it is input",
4980 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
4981 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
4982 " --row Trace each row (SQLITE_TRACE_ROW)",
4983 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
4984 #endif /* SQLITE_OMIT_TRACE */
4986 ".unmodule NAME ... Unregister virtual table modules",
4987 " --allexcept Unregister everything except those named",
4989 ".version Show source, library and compiler versions",
4990 ".vfsinfo ?AUX? Information about the top-level VFS",
4991 ".vfslist List all available VFSes",
4992 ".vfsname ?AUX? Print the name of the VFS stack",
4993 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
4994 " Negative values right-justify",
4998 ** Output help text.
5000 ** zPattern describes the set of commands for which help text is provided.
5001 ** If zPattern is NULL, then show all commands, but only give a one-line
5002 ** description of each.
5004 ** Return the number of matches.
5006 static int showHelp(FILE *out
, const char *zPattern
){
5013 || cli_strcmp(zPattern
,"-a")==0
5014 || cli_strcmp(zPattern
,"-all")==0
5015 || cli_strcmp(zPattern
,"--all")==0
5017 enum HelpWanted
{ HW_NoCull
= 0, HW_SummaryOnly
= 1, HW_Undoc
= 2 };
5018 enum HelpHave
{ HH_Undoc
= 2, HH_Summary
= 1, HH_More
= 0 };
5019 /* Show all or most commands
5020 ** *zPattern==0 => summary of documented commands only
5021 ** *zPattern=='0' => whole help for undocumented commands
5022 ** Otherwise => whole help for documented commands
5024 enum HelpWanted hw
= HW_SummaryOnly
;
5025 enum HelpHave hh
= HH_More
;
5027 hw
= (*zPattern
=='0')? HW_NoCull
|HW_Undoc
: HW_NoCull
;
5029 for(i
=0; i
<ArraySize(azHelp
); i
++){
5030 switch( azHelp
[i
][0] ){
5032 hh
= HH_Summary
|HH_Undoc
;
5041 if( ((hw
^hh
)&HH_Undoc
)==0 ){
5042 if( (hh
&HH_Summary
)!=0 ){
5043 utf8_printf(out
, ".%s\n", azHelp
[i
]+1);
5045 }else if( (hw
&HW_SummaryOnly
)==0 ){
5046 utf8_printf(out
, "%s\n", azHelp
[i
]);
5051 /* Seek documented commands for which zPattern is an exact prefix */
5052 zPat
= sqlite3_mprintf(".%s*", zPattern
);
5053 shell_check_oom(zPat
);
5054 for(i
=0; i
<ArraySize(azHelp
); i
++){
5055 if( sqlite3_strglob(zPat
, azHelp
[i
])==0 ){
5056 utf8_printf(out
, "%s\n", azHelp
[i
]);
5064 /* when zPattern is a prefix of exactly one command, then include
5065 ** the details of that command, which should begin at offset j */
5066 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
][0]==' ' ){
5067 utf8_printf(out
, "%s\n", azHelp
[j
]);
5073 /* Look for documented commands that contain zPattern anywhere.
5074 ** Show complete text of all documented commands that match. */
5075 zPat
= sqlite3_mprintf("%%%s%%", zPattern
);
5076 shell_check_oom(zPat
);
5077 for(i
=0; i
<ArraySize(azHelp
); i
++){
5078 if( azHelp
[i
][0]==',' ){
5079 while( i
<ArraySize(azHelp
)-1 && azHelp
[i
+1][0]==' ' ) ++i
;
5082 if( azHelp
[i
][0]=='.' ) j
= i
;
5083 if( sqlite3_strlike(zPat
, azHelp
[i
], 0)==0 ){
5084 utf8_printf(out
, "%s\n", azHelp
[j
]);
5085 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
+1][0]==' ' ){
5087 utf8_printf(out
, "%s\n", azHelp
[j
]);
5098 /* Forward reference */
5099 static int process_input(ShellState
*p
);
5102 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
5103 ** and return a pointer to the buffer. The caller is responsible for freeing
5106 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
5109 ** For convenience, a nul-terminator byte is always appended to the data read
5110 ** from the file before the buffer is returned. This byte is not included in
5111 ** the final value of (*pnByte), if applicable.
5113 ** NULL is returned if any error is encountered. The final value of *pnByte
5114 ** is undefined in this case.
5116 static char *readFile(const char *zName
, int *pnByte
){
5117 FILE *in
= fopen(zName
, "rb");
5122 if( in
==0 ) return 0;
5123 rc
= fseek(in
, 0, SEEK_END
);
5125 raw_printf(stderr
, "Error: '%s' not seekable\n", zName
);
5131 pBuf
= sqlite3_malloc64( nIn
+1 );
5133 raw_printf(stderr
, "Error: out of memory\n");
5137 nRead
= fread(pBuf
, nIn
, 1, in
);
5141 raw_printf(stderr
, "Error: cannot read '%s'\n", zName
);
5145 if( pnByte
) *pnByte
= nIn
;
5149 #if defined(SQLITE_ENABLE_SESSION)
5151 ** Close a single OpenSession object and release all of its associated
5154 static void session_close(OpenSession
*pSession
){
5156 sqlite3session_delete(pSession
->p
);
5157 sqlite3_free(pSession
->zName
);
5158 for(i
=0; i
<pSession
->nFilter
; i
++){
5159 sqlite3_free(pSession
->azFilter
[i
]);
5161 sqlite3_free(pSession
->azFilter
);
5162 memset(pSession
, 0, sizeof(OpenSession
));
5167 ** Close all OpenSession objects and release all associated resources.
5169 #if defined(SQLITE_ENABLE_SESSION)
5170 static void session_close_all(ShellState
*p
, int i
){
5172 struct AuxDb
*pAuxDb
= i
<0 ? p
->pAuxDb
: &p
->aAuxDb
[i
];
5173 for(j
=0; j
<pAuxDb
->nSession
; j
++){
5174 session_close(&pAuxDb
->aSession
[j
]);
5176 pAuxDb
->nSession
= 0;
5179 # define session_close_all(X,Y)
5183 ** Implementation of the xFilter function for an open session. Omit
5184 ** any tables named by ".session filter" but let all other table through.
5186 #if defined(SQLITE_ENABLE_SESSION)
5187 static int session_filter(void *pCtx
, const char *zTab
){
5188 OpenSession
*pSession
= (OpenSession
*)pCtx
;
5190 for(i
=0; i
<pSession
->nFilter
; i
++){
5191 if( sqlite3_strglob(pSession
->azFilter
[i
], zTab
)==0 ) return 0;
5198 ** Try to deduce the type of file for zName based on its content. Return
5199 ** one of the SHELL_OPEN_* constants.
5201 ** If the file does not exist or is empty but its name looks like a ZIP
5202 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
5203 ** Otherwise, assume an ordinary database regardless of the filename if
5204 ** the type cannot be determined from content.
5206 int deduceDatabaseType(const char *zName
, int dfltZip
){
5207 FILE *f
= fopen(zName
, "rb");
5209 int rc
= SHELL_OPEN_UNSPEC
;
5212 if( dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
5213 return SHELL_OPEN_ZIPFILE
;
5215 return SHELL_OPEN_NORMAL
;
5218 n
= fread(zBuf
, 16, 1, f
);
5219 if( n
==1 && memcmp(zBuf
, "SQLite format 3", 16)==0 ){
5221 return SHELL_OPEN_NORMAL
;
5223 fseek(f
, -25, SEEK_END
);
5224 n
= fread(zBuf
, 25, 1, f
);
5225 if( n
==1 && memcmp(zBuf
, "Start-Of-SQLite3-", 17)==0 ){
5226 rc
= SHELL_OPEN_APPENDVFS
;
5228 fseek(f
, -22, SEEK_END
);
5229 n
= fread(zBuf
, 22, 1, f
);
5230 if( n
==1 && zBuf
[0]==0x50 && zBuf
[1]==0x4b && zBuf
[2]==0x05
5232 rc
= SHELL_OPEN_ZIPFILE
;
5233 }else if( n
==0 && dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
5234 rc
= SHELL_OPEN_ZIPFILE
;
5241 #ifndef SQLITE_OMIT_DESERIALIZE
5243 ** Reconstruct an in-memory database using the output from the "dbtotxt"
5244 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
5245 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
5247 static unsigned char *readHexDb(ShellState
*p
, int *pnData
){
5248 unsigned char *a
= 0;
5256 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
5260 in
= fopen(zDbFilename
, "r");
5262 utf8_printf(stderr
, "cannot open \"%s\" for reading\n", zDbFilename
);
5269 if( in
==0 ) in
= stdin
;
5273 if( fgets(zLine
, sizeof(zLine
), in
)==0 ) goto readHexDb_error
;
5274 rc
= sscanf(zLine
, "| size %d pagesize %d", &n
, &pgsz
);
5275 if( rc
!=2 ) goto readHexDb_error
;
5276 if( n
<0 ) goto readHexDb_error
;
5277 if( pgsz
<512 || pgsz
>65536 || (pgsz
&(pgsz
-1))!=0 ) goto readHexDb_error
;
5278 n
= (n
+pgsz
-1)&~(pgsz
-1); /* Round n up to the next multiple of pgsz */
5279 a
= sqlite3_malloc( n
? n
: 1 );
5282 if( pgsz
<512 || pgsz
>65536 || (pgsz
& (pgsz
-1))!=0 ){
5283 utf8_printf(stderr
, "invalid pagesize\n");
5284 goto readHexDb_error
;
5286 for(nLine
++; fgets(zLine
, sizeof(zLine
), in
)!=0; nLine
++){
5287 rc
= sscanf(zLine
, "| page %d offset %d", &j
, &k
);
5292 if( cli_strncmp(zLine
, "| end ", 6)==0 ){
5295 rc
= sscanf(zLine
,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5296 &j
, &x
[0], &x
[1], &x
[2], &x
[3], &x
[4], &x
[5], &x
[6], &x
[7],
5297 &x
[8], &x
[9], &x
[10], &x
[11], &x
[12], &x
[13], &x
[14], &x
[15]);
5300 if( k
+16<=n
&& k
>=0 ){
5302 for(ii
=0; ii
<16; ii
++) a
[k
+ii
] = x
[ii
]&0xff;
5318 while( fgets(zLine
, sizeof(zLine
), p
->in
)!=0 ){
5320 if(cli_strncmp(zLine
, "| end ", 6)==0 ) break;
5325 utf8_printf(stderr
,"Error on line %d of --hexdb input\n", nLine
);
5328 #endif /* SQLITE_OMIT_DESERIALIZE */
5331 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
5333 static void shellUSleepFunc(
5334 sqlite3_context
*context
,
5336 sqlite3_value
**argv
5338 int sleep
= sqlite3_value_int(argv
[0]);
5340 sqlite3_sleep(sleep
/1000);
5341 sqlite3_result_int(context
, sleep
);
5344 /* Flags for open_db().
5346 ** The default behavior of open_db() is to exit(1) if the database fails to
5347 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5348 ** but still returns without calling exit.
5350 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5351 ** ZIP archive if the file does not exist or is empty and its name matches
5352 ** the *.zip pattern.
5354 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
5355 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
5358 ** Make sure the database is open. If it is not, then open it. If
5359 ** the database fails to open, print an error message and exit.
5361 static void open_db(ShellState
*p
, int openFlags
){
5363 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
5364 if( p
->openMode
==SHELL_OPEN_UNSPEC
){
5365 if( zDbFilename
==0 || zDbFilename
[0]==0 ){
5366 p
->openMode
= SHELL_OPEN_NORMAL
;
5368 p
->openMode
= (u8
)deduceDatabaseType(zDbFilename
,
5369 (openFlags
& OPEN_DB_ZIPFILE
)!=0);
5372 switch( p
->openMode
){
5373 case SHELL_OPEN_APPENDVFS
: {
5374 sqlite3_open_v2(zDbFilename
, &p
->db
,
5375 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, "apndvfs");
5378 case SHELL_OPEN_HEXDB
:
5379 case SHELL_OPEN_DESERIALIZE
: {
5380 sqlite3_open(0, &p
->db
);
5383 case SHELL_OPEN_ZIPFILE
: {
5384 sqlite3_open(":memory:", &p
->db
);
5387 case SHELL_OPEN_READONLY
: {
5388 sqlite3_open_v2(zDbFilename
, &p
->db
,
5389 SQLITE_OPEN_READONLY
|p
->openFlags
, 0);
5392 case SHELL_OPEN_UNSPEC
:
5393 case SHELL_OPEN_NORMAL
: {
5394 sqlite3_open_v2(zDbFilename
, &p
->db
,
5395 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, 0);
5400 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
5401 utf8_printf(stderr
,"Error: unable to open database \"%s\": %s\n",
5402 zDbFilename
, sqlite3_errmsg(p
->db
));
5403 if( (openFlags
& OPEN_DB_KEEPALIVE
)==0 ){
5406 sqlite3_close(p
->db
);
5407 sqlite3_open(":memory:", &p
->db
);
5408 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
5410 "Also: unable to open substitute in-memory database.\n"
5415 "Notice: using substitute in-memory database instead of \"%s\"\n",
5419 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, (int)0, (int*)0);
5421 /* Reflect the use or absence of --unsafe-testing invocation. */
5423 int testmode_on
= ShellHasFlag(p
,SHFLG_TestingMode
);
5424 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_TRUSTED_SCHEMA
, testmode_on
,0);
5425 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, !testmode_on
,0);
5428 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5429 sqlite3_enable_load_extension(p
->db
, 1);
5431 sqlite3_shathree_init(p
->db
, 0, 0);
5432 sqlite3_uint_init(p
->db
, 0, 0);
5433 sqlite3_decimal_init(p
->db
, 0, 0);
5434 sqlite3_base64_init(p
->db
, 0, 0);
5435 sqlite3_base85_init(p
->db
, 0, 0);
5436 sqlite3_regexp_init(p
->db
, 0, 0);
5437 sqlite3_ieee_init(p
->db
, 0, 0);
5438 sqlite3_series_init(p
->db
, 0, 0);
5439 #ifndef SQLITE_SHELL_FIDDLE
5440 sqlite3_fileio_init(p
->db
, 0, 0);
5441 sqlite3_completion_init(p
->db
, 0, 0);
5443 #ifdef SQLITE_HAVE_ZLIB
5444 if( !p
->bSafeModePersist
){
5445 sqlite3_zipfile_init(p
->db
, 0, 0);
5446 sqlite3_sqlar_init(p
->db
, 0, 0);
5449 #ifdef SQLITE_SHELL_EXTFUNCS
5450 /* Create a preprocessing mechanism for extensions to make
5451 * their own provisions for being built into the shell.
5452 * This is a short-span macro. See further below for usage.
5454 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
5455 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
5456 /* Let custom-included extensions get their ..._init() called.
5457 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
5458 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
5459 * inititialization routine to be called.
5462 int irc
= SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, INIT
)(p
->db
);
5463 /* Let custom-included extensions expose their functionality.
5464 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
5465 * the SQL functions, virtual tables, collating sequences or
5466 * VFS's implemented by the extension to be registered.
5469 || irc
==SQLITE_OK_LOAD_PERMANENTLY
){
5470 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, EXPOSE
)(p
->db
, 0);
5472 #undef SHELL_SUB_MACRO
5473 #undef SHELL_SUBMACRO
5477 sqlite3_create_function(p
->db
, "shell_add_schema", 3, SQLITE_UTF8
, 0,
5478 shellAddSchemaName
, 0, 0);
5479 sqlite3_create_function(p
->db
, "shell_module_schema", 1, SQLITE_UTF8
, 0,
5480 shellModuleSchema
, 0, 0);
5481 sqlite3_create_function(p
->db
, "shell_putsnl", 1, SQLITE_UTF8
, p
,
5482 shellPutsFunc
, 0, 0);
5483 sqlite3_create_function(p
->db
, "usleep",1,SQLITE_UTF8
,0,
5484 shellUSleepFunc
, 0, 0);
5485 #ifndef SQLITE_NOHAVE_SYSTEM
5486 sqlite3_create_function(p
->db
, "edit", 1, SQLITE_UTF8
, 0,
5488 sqlite3_create_function(p
->db
, "edit", 2, SQLITE_UTF8
, 0,
5492 if( p
->openMode
==SHELL_OPEN_ZIPFILE
){
5493 char *zSql
= sqlite3_mprintf(
5494 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename
);
5495 shell_check_oom(zSql
);
5496 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
5499 #ifndef SQLITE_OMIT_DESERIALIZE
5501 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
|| p
->openMode
==SHELL_OPEN_HEXDB
){
5504 unsigned char *aData
;
5505 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
){
5506 aData
= (unsigned char*)readFile(zDbFilename
, &nData
);
5508 aData
= readHexDb(p
, &nData
);
5513 rc
= sqlite3_deserialize(p
->db
, "main", aData
, nData
, nData
,
5514 SQLITE_DESERIALIZE_RESIZEABLE
|
5515 SQLITE_DESERIALIZE_FREEONCLOSE
);
5517 utf8_printf(stderr
, "Error: sqlite3_deserialize() returns %d\n", rc
);
5520 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_SIZE_LIMIT
, &p
->szMax
);
5526 if( p
->bSafeModePersist
){
5527 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
5530 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
5536 ** Attempt to close the databaes connection. Report errors.
5538 void close_db(sqlite3
*db
){
5539 int rc
= sqlite3_close(db
);
5541 utf8_printf(stderr
, "Error: sqlite3_close() returns %d: %s\n",
5542 rc
, sqlite3_errmsg(db
));
5546 #if HAVE_READLINE || HAVE_EDITLINE
5548 ** Readline completion callbacks
5550 static char *readline_completion_generator(const char *text
, int state
){
5551 static sqlite3_stmt
*pStmt
= 0;
5555 sqlite3_finalize(pStmt
);
5556 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5557 " FROM completion(%Q) ORDER BY 1", text
);
5558 shell_check_oom(zSql
);
5559 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
5562 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
5563 const char *z
= (const char*)sqlite3_column_text(pStmt
,0);
5564 zRet
= z
? strdup(z
) : 0;
5566 sqlite3_finalize(pStmt
);
5572 static char **readline_completion(const char *zText
, int iStart
, int iEnd
){
5575 rl_attempted_completion_over
= 1;
5576 return rl_completion_matches(zText
, readline_completion_generator
);
5579 #elif HAVE_LINENOISE
5581 ** Linenoise completion callback
5583 static void linenoise_completion(const char *zLine
, linenoiseCompletions
*lc
){
5584 i64 nLine
= strlen(zLine
);
5586 sqlite3_stmt
*pStmt
= 0;
5590 if( nLine
>(i64
)sizeof(zBuf
)-30 ) return;
5591 if( zLine
[0]=='.' || zLine
[0]=='#') return;
5592 for(i
=nLine
-1; i
>=0 && (isalnum(zLine
[i
]) || zLine
[i
]=='_'); i
--){}
5593 if( i
==nLine
-1 ) return;
5595 memcpy(zBuf
, zLine
, iStart
);
5596 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5597 " FROM completion(%Q,%Q) ORDER BY 1",
5598 &zLine
[iStart
], zLine
);
5599 shell_check_oom(zSql
);
5600 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
5602 sqlite3_exec(globalDb
, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5603 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
5604 const char *zCompletion
= (const char*)sqlite3_column_text(pStmt
, 0);
5605 int nCompletion
= sqlite3_column_bytes(pStmt
, 0);
5606 if( iStart
+nCompletion
< (i64
)sizeof(zBuf
)-1 && zCompletion
){
5607 memcpy(zBuf
+iStart
, zCompletion
, nCompletion
+1);
5608 linenoiseAddCompletion(lc
, zBuf
);
5611 sqlite3_finalize(pStmt
);
5616 ** Do C-language style dequoting.
5622 ** \v -> vertical tab
5624 ** \r -> carriage return
5629 ** \NNN -> ascii character NNN in octal
5630 ** \xHH -> ascii character HH in hexadecimal
5632 static void resolve_backslashes(char *z
){
5635 while( *z
&& *z
!='\\' ) z
++;
5636 for(i
=j
=0; (c
= z
[i
])!=0; i
++, j
++){
5637 if( c
=='\\' && z
[i
+1]!=0 ){
5655 }else if( c
=='\'' ){
5657 }else if( c
=='\\' ){
5662 while( nhd
<2 && (c
=z
[i
+1+nhd
])!=0 && (hdv
=hexDigitValue(c
))>=0 ){
5663 hv
= (u8
)((hv
<<4)|hdv
);
5668 }else if( c
>='0' && c
<='7' ){
5670 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
5672 c
= (c
<<3) + z
[i
] - '0';
5673 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
5675 c
= (c
<<3) + z
[i
] - '0';
5686 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
5687 ** for TRUE and FALSE. Return the integer value if appropriate.
5689 static int booleanValue(const char *zArg
){
5691 if( zArg
[0]=='0' && zArg
[1]=='x' ){
5692 for(i
=2; hexDigitValue(zArg
[i
])>=0; i
++){}
5694 for(i
=0; zArg
[i
]>='0' && zArg
[i
]<='9'; i
++){}
5696 if( i
>0 && zArg
[i
]==0 ) return (int)(integerValue(zArg
) & 0xffffffff);
5697 if( sqlite3_stricmp(zArg
, "on")==0 || sqlite3_stricmp(zArg
,"yes")==0 ){
5700 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
5703 utf8_printf(stderr
, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
5709 ** Set or clear a shell flag according to a boolean value.
5711 static void setOrClearFlag(ShellState
*p
, unsigned mFlag
, const char *zArg
){
5712 if( booleanValue(zArg
) ){
5713 ShellSetFlag(p
, mFlag
);
5715 ShellClearFlag(p
, mFlag
);
5720 ** Close an output file, assuming it is not stderr or stdout
5722 static void output_file_close(FILE *f
){
5723 if( f
&& f
!=stdout
&& f
!=stderr
) fclose(f
);
5727 ** Try to open an output file. The names "stdout" and "stderr" are
5728 ** recognized and do the right thing. NULL is returned if the output
5729 ** filename is "off".
5731 static FILE *output_file_open(const char *zFile
, int bTextMode
){
5733 if( cli_strcmp(zFile
,"stdout")==0 ){
5735 }else if( cli_strcmp(zFile
, "stderr")==0 ){
5737 }else if( cli_strcmp(zFile
, "off")==0 ){
5740 f
= fopen(zFile
, bTextMode
? "w" : "wb");
5742 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
5748 #ifndef SQLITE_OMIT_TRACE
5750 ** A routine for handling output from sqlite3_trace().
5752 static int sql_trace_callback(
5753 unsigned mType
, /* The trace type */
5754 void *pArg
, /* The ShellState pointer */
5755 void *pP
, /* Usually a pointer to sqlite_stmt */
5756 void *pX
/* Auxiliary output */
5758 ShellState
*p
= (ShellState
*)pArg
;
5759 sqlite3_stmt
*pStmt
;
5762 if( p
->traceOut
==0 ) return 0;
5763 if( mType
==SQLITE_TRACE_CLOSE
){
5764 utf8_printf(p
->traceOut
, "-- closing database connection\n");
5767 if( mType
!=SQLITE_TRACE_ROW
&& pX
!=0 && ((const char*)pX
)[0]=='-' ){
5768 zSql
= (const char*)pX
;
5770 pStmt
= (sqlite3_stmt
*)pP
;
5771 switch( p
->eTraceType
){
5772 case SHELL_TRACE_EXPANDED
: {
5773 zSql
= sqlite3_expanded_sql(pStmt
);
5776 #ifdef SQLITE_ENABLE_NORMALIZE
5777 case SHELL_TRACE_NORMALIZED
: {
5778 zSql
= sqlite3_normalized_sql(pStmt
);
5783 zSql
= sqlite3_sql(pStmt
);
5788 if( zSql
==0 ) return 0;
5789 nSql
= strlen(zSql
);
5790 if( nSql
>1000000000 ) nSql
= 1000000000;
5791 while( nSql
>0 && zSql
[nSql
-1]==';' ){ nSql
--; }
5793 case SQLITE_TRACE_ROW
:
5794 case SQLITE_TRACE_STMT
: {
5795 utf8_printf(p
->traceOut
, "%.*s;\n", (int)nSql
, zSql
);
5798 case SQLITE_TRACE_PROFILE
: {
5799 sqlite3_int64 nNanosec
= pX
? *(sqlite3_int64
*)pX
: 0;
5800 utf8_printf(p
->traceOut
, "%.*s; -- %lld ns\n", (int)nSql
, zSql
, nNanosec
);
5809 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
5810 ** a useful spot to set a debugger breakpoint.
5812 ** This routine does not do anything practical. The code are there simply
5813 ** to prevent the compiler from optimizing this routine out.
5815 static void test_breakpoint(void){
5816 static unsigned int nCall
= 0;
5817 if( (nCall
++)==0xffffffff ) printf("Many .breakpoints have run\n");
5821 ** An object used to read a CSV and other files for import.
5823 typedef struct ImportCtx ImportCtx
;
5825 const char *zFile
; /* Name of the input file */
5826 FILE *in
; /* Read the CSV text from this input stream */
5827 int (SQLITE_CDECL
*xCloser
)(FILE*); /* Func to close in */
5828 char *z
; /* Accumulated text for a field */
5829 int n
; /* Number of bytes in z */
5830 int nAlloc
; /* Space allocated for z[] */
5831 int nLine
; /* Current line number */
5832 int nRow
; /* Number of rows imported */
5833 int nErr
; /* Number of errors encountered */
5834 int bNotFirst
; /* True if one or more bytes already read */
5835 int cTerm
; /* Character that terminated the most recent field */
5836 int cColSep
; /* The column separator character. (Usually ",") */
5837 int cRowSep
; /* The row separator character. (Usually "\n") */
5840 /* Clean up resourced used by an ImportCtx */
5841 static void import_cleanup(ImportCtx
*p
){
5842 if( p
->in
!=0 && p
->xCloser
!=0 ){
5850 /* Append a single byte to z[] */
5851 static void import_append_char(ImportCtx
*p
, int c
){
5852 if( p
->n
+1>=p
->nAlloc
){
5853 p
->nAlloc
+= p
->nAlloc
+ 100;
5854 p
->z
= sqlite3_realloc64(p
->z
, p
->nAlloc
);
5855 shell_check_oom(p
->z
);
5857 p
->z
[p
->n
++] = (char)c
;
5860 /* Read a single field of CSV text. Compatible with rfc4180 and extended
5861 ** with the option of having a separator other than ",".
5863 ** + Input comes from p->in.
5864 ** + Store results in p->z of length p->n. Space to hold p->z comes
5865 ** from sqlite3_malloc64().
5866 ** + Use p->cSep as the column separator. The default is ",".
5867 ** + Use p->rSep as the row separator. The default is "\n".
5868 ** + Keep track of the line number in p->nLine.
5869 ** + Store the character that terminates the field in p->cTerm. Store
5870 ** EOF on end-of-file.
5871 ** + Report syntax errors on stderr
5873 static char *SQLITE_CDECL
csv_read_one_field(ImportCtx
*p
){
5875 int cSep
= (u8
)p
->cColSep
;
5876 int rSep
= (u8
)p
->cRowSep
;
5879 if( c
==EOF
|| seenInterrupt
){
5885 int startLine
= p
->nLine
;
5890 if( c
==rSep
) p
->nLine
++;
5897 if( (c
==cSep
&& pc
==cQuote
)
5898 || (c
==rSep
&& pc
==cQuote
)
5899 || (c
==rSep
&& pc
=='\r' && ppc
==cQuote
)
5900 || (c
==EOF
&& pc
==cQuote
)
5902 do{ p
->n
--; }while( p
->z
[p
->n
]!=cQuote
);
5906 if( pc
==cQuote
&& c
!='\r' ){
5907 utf8_printf(stderr
, "%s:%d: unescaped %c character\n",
5908 p
->zFile
, p
->nLine
, cQuote
);
5911 utf8_printf(stderr
, "%s:%d: unterminated %c-quoted field\n",
5912 p
->zFile
, startLine
, cQuote
);
5916 import_append_char(p
, c
);
5921 /* If this is the first field being parsed and it begins with the
5922 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
5923 if( (c
&0xff)==0xef && p
->bNotFirst
==0 ){
5924 import_append_char(p
, c
);
5926 if( (c
&0xff)==0xbb ){
5927 import_append_char(p
, c
);
5929 if( (c
&0xff)==0xbf ){
5932 return csv_read_one_field(p
);
5936 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
5937 import_append_char(p
, c
);
5942 if( p
->n
>0 && p
->z
[p
->n
-1]=='\r' ) p
->n
--;
5946 if( p
->z
) p
->z
[p
->n
] = 0;
5951 /* Read a single field of ASCII delimited text.
5953 ** + Input comes from p->in.
5954 ** + Store results in p->z of length p->n. Space to hold p->z comes
5955 ** from sqlite3_malloc64().
5956 ** + Use p->cSep as the column separator. The default is "\x1F".
5957 ** + Use p->rSep as the row separator. The default is "\x1E".
5958 ** + Keep track of the row number in p->nLine.
5959 ** + Store the character that terminates the field in p->cTerm. Store
5960 ** EOF on end-of-file.
5961 ** + Report syntax errors on stderr
5963 static char *SQLITE_CDECL
ascii_read_one_field(ImportCtx
*p
){
5965 int cSep
= (u8
)p
->cColSep
;
5966 int rSep
= (u8
)p
->cRowSep
;
5969 if( c
==EOF
|| seenInterrupt
){
5973 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
5974 import_append_char(p
, c
);
5981 if( p
->z
) p
->z
[p
->n
] = 0;
5986 ** Try to transfer data for table zTable. If an error is seen while
5987 ** moving forward, try to go backwards. The backwards movement won't
5988 ** work for WITHOUT ROWID tables.
5990 static void tryToCloneData(
5995 sqlite3_stmt
*pQuery
= 0;
5996 sqlite3_stmt
*pInsert
= 0;
6001 int nTable
= strlen30(zTable
);
6004 const int spinRate
= 10000;
6006 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\"", zTable
);
6007 shell_check_oom(zQuery
);
6008 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6010 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
6011 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
6015 n
= sqlite3_column_count(pQuery
);
6016 zInsert
= sqlite3_malloc64(200 + nTable
+ n
*3);
6017 shell_check_oom(zInsert
);
6018 sqlite3_snprintf(200+nTable
,zInsert
,
6019 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable
);
6020 i
= strlen30(zInsert
);
6022 memcpy(zInsert
+i
, ",?", 2);
6025 memcpy(zInsert
+i
, ");", 3);
6026 rc
= sqlite3_prepare_v2(newDb
, zInsert
, -1, &pInsert
, 0);
6028 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
6029 sqlite3_extended_errcode(newDb
), sqlite3_errmsg(newDb
),
6034 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
6036 switch( sqlite3_column_type(pQuery
, i
) ){
6038 sqlite3_bind_null(pInsert
, i
+1);
6041 case SQLITE_INTEGER
: {
6042 sqlite3_bind_int64(pInsert
, i
+1, sqlite3_column_int64(pQuery
,i
));
6045 case SQLITE_FLOAT
: {
6046 sqlite3_bind_double(pInsert
, i
+1, sqlite3_column_double(pQuery
,i
));
6050 sqlite3_bind_text(pInsert
, i
+1,
6051 (const char*)sqlite3_column_text(pQuery
,i
),
6056 sqlite3_bind_blob(pInsert
, i
+1, sqlite3_column_blob(pQuery
,i
),
6057 sqlite3_column_bytes(pQuery
,i
),
6063 rc
= sqlite3_step(pInsert
);
6064 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_ROW
&& rc
!=SQLITE_DONE
){
6065 utf8_printf(stderr
, "Error %d: %s\n", sqlite3_extended_errcode(newDb
),
6066 sqlite3_errmsg(newDb
));
6068 sqlite3_reset(pInsert
);
6070 if( (cnt
%spinRate
)==0 ){
6071 printf("%c\b", "|/-\\"[(cnt
/spinRate
)%4]);
6075 if( rc
==SQLITE_DONE
) break;
6076 sqlite3_finalize(pQuery
);
6077 sqlite3_free(zQuery
);
6078 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
6080 shell_check_oom(zQuery
);
6081 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6083 utf8_printf(stderr
, "Warning: cannot step \"%s\" backwards", zTable
);
6086 } /* End for(k=0...) */
6089 sqlite3_finalize(pQuery
);
6090 sqlite3_finalize(pInsert
);
6091 sqlite3_free(zQuery
);
6092 sqlite3_free(zInsert
);
6097 ** Try to transfer all rows of the schema that match zWhere. For
6098 ** each row, invoke xForEach() on the object defined by that row.
6099 ** If an error is encountered while moving forward through the
6100 ** sqlite_schema table, try again moving backwards.
6102 static void tryToCloneSchema(
6106 void (*xForEach
)(ShellState
*,sqlite3
*,const char*)
6108 sqlite3_stmt
*pQuery
= 0;
6111 const unsigned char *zName
;
6112 const unsigned char *zSql
;
6115 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6116 " WHERE %s ORDER BY rowid ASC", zWhere
);
6117 shell_check_oom(zQuery
);
6118 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6120 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
6121 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
6123 goto end_schema_xfer
;
6125 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
6126 zName
= sqlite3_column_text(pQuery
, 0);
6127 zSql
= sqlite3_column_text(pQuery
, 1);
6128 if( zName
==0 || zSql
==0 ) continue;
6129 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")!=0 ){
6130 printf("%s... ", zName
); fflush(stdout
);
6131 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
6133 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
6134 sqlite3_free(zErrMsg
);
6139 xForEach(p
, newDb
, (const char*)zName
);
6143 if( rc
!=SQLITE_DONE
){
6144 sqlite3_finalize(pQuery
);
6145 sqlite3_free(zQuery
);
6146 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6147 " WHERE %s ORDER BY rowid DESC", zWhere
);
6148 shell_check_oom(zQuery
);
6149 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6151 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
6152 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
6154 goto end_schema_xfer
;
6156 while( sqlite3_step(pQuery
)==SQLITE_ROW
){
6157 zName
= sqlite3_column_text(pQuery
, 0);
6158 zSql
= sqlite3_column_text(pQuery
, 1);
6159 if( zName
==0 || zSql
==0 ) continue;
6160 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")==0 ) continue;
6161 printf("%s... ", zName
); fflush(stdout
);
6162 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
6164 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
6165 sqlite3_free(zErrMsg
);
6169 xForEach(p
, newDb
, (const char*)zName
);
6175 sqlite3_finalize(pQuery
);
6176 sqlite3_free(zQuery
);
6180 ** Open a new database file named "zNewDb". Try to recover as much information
6181 ** as possible out of the main database (which might be corrupt) and write it
6184 static void tryToClone(ShellState
*p
, const char *zNewDb
){
6187 if( access(zNewDb
,0)==0 ){
6188 utf8_printf(stderr
, "File \"%s\" already exists.\n", zNewDb
);
6191 rc
= sqlite3_open(zNewDb
, &newDb
);
6193 utf8_printf(stderr
, "Cannot create output database: %s\n",
6194 sqlite3_errmsg(newDb
));
6196 sqlite3_exec(p
->db
, "PRAGMA writable_schema=ON;", 0, 0, 0);
6197 sqlite3_exec(newDb
, "BEGIN EXCLUSIVE;", 0, 0, 0);
6198 tryToCloneSchema(p
, newDb
, "type='table'", tryToCloneData
);
6199 tryToCloneSchema(p
, newDb
, "type!='table'", 0);
6200 sqlite3_exec(newDb
, "COMMIT;", 0, 0, 0);
6201 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
6207 ** Change the output file back to stdout.
6209 ** If the p->doXdgOpen flag is set, that means the output was being
6210 ** redirected to a temporary file named by p->zTempFile. In that case,
6211 ** launch start/open/xdg-open on that temporary file.
6213 static void output_reset(ShellState
*p
){
6214 if( p
->outfile
[0]=='|' ){
6215 #ifndef SQLITE_OMIT_POPEN
6219 output_file_close(p
->out
);
6220 #ifndef SQLITE_NOHAVE_SYSTEM
6222 const char *zXdgOpenCmd
=
6225 #elif defined(__APPLE__)
6231 zCmd
= sqlite3_mprintf("%s %s", zXdgOpenCmd
, p
->zTempFile
);
6233 utf8_printf(stderr
, "Failed: [%s]\n", zCmd
);
6235 /* Give the start/open/xdg-open command some time to get
6236 ** going before we continue, and potential delete the
6237 ** p->zTempFile data file out from under it */
6238 sqlite3_sleep(2000);
6244 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
6251 ** Run an SQL command and return the single integer result.
6253 static int db_int(sqlite3
*db
, const char *zSql
){
6254 sqlite3_stmt
*pStmt
;
6256 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
6257 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
6258 res
= sqlite3_column_int(pStmt
,0);
6260 sqlite3_finalize(pStmt
);
6264 #if SQLITE_SHELL_HAVE_RECOVER
6266 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
6268 static unsigned int get2byteInt(unsigned char *a
){
6269 return (a
[0]<<8) + a
[1];
6271 static unsigned int get4byteInt(unsigned char *a
){
6272 return (a
[0]<<24) + (a
[1]<<16) + (a
[2]<<8) + a
[3];
6276 ** Implementation of the ".dbinfo" command.
6278 ** Return 1 on error, 2 to exit, and 0 otherwise.
6280 static int shell_dbinfo_command(ShellState
*p
, int nArg
, char **azArg
){
6281 static const struct { const char *zName
; int ofst
; } aField
[] = {
6282 { "file change counter:", 24 },
6283 { "database page count:", 28 },
6284 { "freelist page count:", 36 },
6285 { "schema cookie:", 40 },
6286 { "schema format:", 44 },
6287 { "default cache size:", 48 },
6288 { "autovacuum top root:", 52 },
6289 { "incremental vacuum:", 64 },
6290 { "text encoding:", 56 },
6291 { "user version:", 60 },
6292 { "application id:", 68 },
6293 { "software version:", 96 },
6295 static const struct { const char *zName
; const char *zSql
; } aQuery
[] = {
6296 { "number of tables:",
6297 "SELECT count(*) FROM %s WHERE type='table'" },
6298 { "number of indexes:",
6299 "SELECT count(*) FROM %s WHERE type='index'" },
6300 { "number of triggers:",
6301 "SELECT count(*) FROM %s WHERE type='trigger'" },
6302 { "number of views:",
6303 "SELECT count(*) FROM %s WHERE type='view'" },
6305 "SELECT total(length(sql)) FROM %s" },
6308 unsigned iDataVersion
;
6310 char *zDb
= nArg
>=2 ? azArg
[1] : "main";
6311 sqlite3_stmt
*pStmt
= 0;
6312 unsigned char aHdr
[100];
6314 if( p
->db
==0 ) return 1;
6315 rc
= sqlite3_prepare_v2(p
->db
,
6316 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
6319 utf8_printf(stderr
, "error: %s\n", sqlite3_errmsg(p
->db
));
6320 sqlite3_finalize(pStmt
);
6323 sqlite3_bind_text(pStmt
, 1, zDb
, -1, SQLITE_STATIC
);
6324 if( sqlite3_step(pStmt
)==SQLITE_ROW
6325 && sqlite3_column_bytes(pStmt
,0)>100
6327 const u8
*pb
= sqlite3_column_blob(pStmt
,0);
6328 shell_check_oom(pb
);
6329 memcpy(aHdr
, pb
, 100);
6330 sqlite3_finalize(pStmt
);
6332 raw_printf(stderr
, "unable to read database header\n");
6333 sqlite3_finalize(pStmt
);
6336 i
= get2byteInt(aHdr
+16);
6337 if( i
==1 ) i
= 65536;
6338 utf8_printf(p
->out
, "%-20s %d\n", "database page size:", i
);
6339 utf8_printf(p
->out
, "%-20s %d\n", "write format:", aHdr
[18]);
6340 utf8_printf(p
->out
, "%-20s %d\n", "read format:", aHdr
[19]);
6341 utf8_printf(p
->out
, "%-20s %d\n", "reserved bytes:", aHdr
[20]);
6342 for(i
=0; i
<ArraySize(aField
); i
++){
6343 int ofst
= aField
[i
].ofst
;
6344 unsigned int val
= get4byteInt(aHdr
+ ofst
);
6345 utf8_printf(p
->out
, "%-20s %u", aField
[i
].zName
, val
);
6348 if( val
==1 ) raw_printf(p
->out
, " (utf8)");
6349 if( val
==2 ) raw_printf(p
->out
, " (utf16le)");
6350 if( val
==3 ) raw_printf(p
->out
, " (utf16be)");
6353 raw_printf(p
->out
, "\n");
6356 zSchemaTab
= sqlite3_mprintf("main.sqlite_schema");
6357 }else if( cli_strcmp(zDb
,"temp")==0 ){
6358 zSchemaTab
= sqlite3_mprintf("%s", "sqlite_temp_schema");
6360 zSchemaTab
= sqlite3_mprintf("\"%w\".sqlite_schema", zDb
);
6362 for(i
=0; i
<ArraySize(aQuery
); i
++){
6363 char *zSql
= sqlite3_mprintf(aQuery
[i
].zSql
, zSchemaTab
);
6364 int val
= db_int(p
->db
, zSql
);
6366 utf8_printf(p
->out
, "%-20s %d\n", aQuery
[i
].zName
, val
);
6368 sqlite3_free(zSchemaTab
);
6369 sqlite3_file_control(p
->db
, zDb
, SQLITE_FCNTL_DATA_VERSION
, &iDataVersion
);
6370 utf8_printf(p
->out
, "%-20s %u\n", "data version", iDataVersion
);
6373 #endif /* SQLITE_SHELL_HAVE_RECOVER */
6376 ** Print the current sqlite3_errmsg() value to stderr and return 1.
6378 static int shellDatabaseError(sqlite3
*db
){
6379 const char *zErr
= sqlite3_errmsg(db
);
6380 utf8_printf(stderr
, "Error: %s\n", zErr
);
6385 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
6386 ** if they match and FALSE (0) if they do not match.
6390 ** '*' Matches any sequence of zero or more characters.
6392 ** '?' Matches exactly one character.
6394 ** [...] Matches one character from the enclosed list of
6397 ** [^...] Matches one character not in the enclosed list.
6399 ** '#' Matches any sequence of one or more digits with an
6400 ** optional + or - sign in front
6402 ** ' ' Any span of whitespace matches any other span of
6405 ** Extra whitespace at the end of z[] is ignored.
6407 static int testcase_glob(const char *zGlob
, const char *z
){
6412 while( (c
= (*(zGlob
++)))!=0 ){
6414 if( !IsSpace(*z
) ) return 0;
6415 while( IsSpace(*zGlob
) ) zGlob
++;
6416 while( IsSpace(*z
) ) z
++;
6418 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
6419 if( c
=='?' && (*(z
++))==0 ) return 0;
6424 while( *z
&& testcase_glob(zGlob
-1,z
)==0 ){
6429 while( (c2
= (*(z
++)))!=0 ){
6432 if( c2
==0 ) return 0;
6434 if( testcase_glob(zGlob
,z
) ) return 1;
6438 if( (*(z
++))==0 ) return 0;
6444 if( c
==0 ) return 0;
6451 if( c
==']' ) seen
= 1;
6454 while( c2
&& c2
!=']' ){
6455 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
6457 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
6467 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
6469 if( (z
[0]=='-' || z
[0]=='+') && IsDigit(z
[1]) ) z
++;
6470 if( !IsDigit(z
[0]) ) return 0;
6472 while( IsDigit(z
[0]) ){ z
++; }
6474 if( c
!=(*(z
++)) ) return 0;
6477 while( IsSpace(*z
) ){ z
++; }
6483 ** Compare the string as a command-line option with either one or two
6484 ** initial "-" characters.
6486 static int optionMatch(const char *zStr
, const char *zOpt
){
6487 if( zStr
[0]!='-' ) return 0;
6489 if( zStr
[0]=='-' ) zStr
++;
6490 return cli_strcmp(zStr
, zOpt
)==0;
6496 int shellDeleteFile(const char *zFilename
){
6499 wchar_t *z
= sqlite3_win32_utf8_to_unicode(zFilename
);
6503 rc
= unlink(zFilename
);
6509 ** Try to delete the temporary file (if there is one) and free the
6510 ** memory used to hold the name of the temp file.
6512 static void clearTempFile(ShellState
*p
){
6513 if( p
->zTempFile
==0 ) return;
6514 if( p
->doXdgOpen
) return;
6515 if( shellDeleteFile(p
->zTempFile
) ) return;
6516 sqlite3_free(p
->zTempFile
);
6521 ** Create a new temp file name with the given suffix.
6523 static void newTempFile(ShellState
*p
, const char *zSuffix
){
6525 sqlite3_free(p
->zTempFile
);
6528 sqlite3_file_control(p
->db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &p
->zTempFile
);
6530 if( p
->zTempFile
==0 ){
6531 /* If p->db is an in-memory database then the TEMPFILENAME file-control
6532 ** will not work and we will need to fallback to guessing */
6535 sqlite3_randomness(sizeof(r
), &r
);
6536 zTemp
= getenv("TEMP");
6537 if( zTemp
==0 ) zTemp
= getenv("TMP");
6545 p
->zTempFile
= sqlite3_mprintf("%s/temp%llx.%s", zTemp
, r
, zSuffix
);
6547 p
->zTempFile
= sqlite3_mprintf("%z.%s", p
->zTempFile
, zSuffix
);
6549 shell_check_oom(p
->zTempFile
);
6554 ** The implementation of SQL scalar function fkey_collate_clause(), used
6555 ** by the ".lint fkey-indexes" command. This scalar function is always
6556 ** called with four arguments - the parent table name, the parent column name,
6557 ** the child table name and the child column name.
6559 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6561 ** If either of the named tables or columns do not exist, this function
6562 ** returns an empty string. An empty string is also returned if both tables
6563 ** and columns exist but have the same default collation sequence. Or,
6564 ** if both exist but the default collation sequences are different, this
6565 ** function returns the string " COLLATE <parent-collation>", where
6566 ** <parent-collation> is the default collation sequence of the parent column.
6568 static void shellFkeyCollateClause(
6569 sqlite3_context
*pCtx
,
6571 sqlite3_value
**apVal
6573 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
6574 const char *zParent
;
6575 const char *zParentCol
;
6576 const char *zParentSeq
;
6578 const char *zChildCol
;
6579 const char *zChildSeq
= 0; /* Initialize to avoid false-positive warning */
6583 zParent
= (const char*)sqlite3_value_text(apVal
[0]);
6584 zParentCol
= (const char*)sqlite3_value_text(apVal
[1]);
6585 zChild
= (const char*)sqlite3_value_text(apVal
[2]);
6586 zChildCol
= (const char*)sqlite3_value_text(apVal
[3]);
6588 sqlite3_result_text(pCtx
, "", -1, SQLITE_STATIC
);
6589 rc
= sqlite3_table_column_metadata(
6590 db
, "main", zParent
, zParentCol
, 0, &zParentSeq
, 0, 0, 0
6592 if( rc
==SQLITE_OK
){
6593 rc
= sqlite3_table_column_metadata(
6594 db
, "main", zChild
, zChildCol
, 0, &zChildSeq
, 0, 0, 0
6598 if( rc
==SQLITE_OK
&& sqlite3_stricmp(zParentSeq
, zChildSeq
) ){
6599 char *z
= sqlite3_mprintf(" COLLATE %s", zParentSeq
);
6600 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
6607 ** The implementation of dot-command ".lint fkey-indexes".
6609 static int lintFkeyIndexes(
6610 ShellState
*pState
, /* Current shell tool state */
6611 char **azArg
, /* Array of arguments passed to dot command */
6612 int nArg
/* Number of entries in azArg[] */
6614 sqlite3
*db
= pState
->db
; /* Database handle to query "main" db of */
6615 FILE *out
= pState
->out
; /* Stream to write non-error output to */
6616 int bVerbose
= 0; /* If -verbose is present */
6617 int bGroupByParent
= 0; /* If -groupbyparent is present */
6618 int i
; /* To iterate through azArg[] */
6619 const char *zIndent
= ""; /* How much to indent CREATE INDEX by */
6620 int rc
; /* Return code */
6621 sqlite3_stmt
*pSql
= 0; /* Compiled version of SQL statement below */
6624 ** This SELECT statement returns one row for each foreign key constraint
6625 ** in the schema of the main database. The column values are:
6627 ** 0. The text of an SQL statement similar to:
6629 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6631 ** This SELECT is similar to the one that the foreign keys implementation
6632 ** needs to run internally on child tables. If there is an index that can
6633 ** be used to optimize this query, then it can also be used by the FK
6634 ** implementation to optimize DELETE or UPDATE statements on the parent
6637 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6638 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6639 ** contains an index that can be used to optimize the query.
6641 ** 2. Human readable text that describes the child table and columns. e.g.
6643 ** "child_table(child_key1, child_key2)"
6645 ** 3. Human readable text that describes the parent table and columns. e.g.
6647 ** "parent_table(parent_key1, parent_key2)"
6649 ** 4. A full CREATE INDEX statement for an index that could be used to
6650 ** optimize DELETE or UPDATE statements on the parent table. e.g.
6652 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
6654 ** 5. The name of the parent table.
6656 ** These six values are used by the C logic below to generate the report.
6660 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6661 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6662 " || fkey_collate_clause("
6663 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6665 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6666 " || group_concat('*=?', ' AND ') || ')'"
6668 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
6670 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6672 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6673 " || ' ON ' || quote(s.name) || '('"
6674 " || group_concat(quote(f.[from]) ||"
6675 " fkey_collate_clause("
6676 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6680 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6681 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6682 "GROUP BY s.name, f.id "
6683 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6685 const char *zGlobIPK
= "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6687 for(i
=2; i
<nArg
; i
++){
6688 int n
= strlen30(azArg
[i
]);
6689 if( n
>1 && sqlite3_strnicmp("-verbose", azArg
[i
], n
)==0 ){
6692 else if( n
>1 && sqlite3_strnicmp("-groupbyparent", azArg
[i
], n
)==0 ){
6697 raw_printf(stderr
, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
6700 return SQLITE_ERROR
;
6704 /* Register the fkey_collate_clause() SQL function */
6705 rc
= sqlite3_create_function(db
, "fkey_collate_clause", 4, SQLITE_UTF8
,
6706 0, shellFkeyCollateClause
, 0, 0
6710 if( rc
==SQLITE_OK
){
6711 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pSql
, 0);
6713 if( rc
==SQLITE_OK
){
6714 sqlite3_bind_int(pSql
, 1, bGroupByParent
);
6717 if( rc
==SQLITE_OK
){
6720 while( SQLITE_ROW
==sqlite3_step(pSql
) ){
6722 sqlite3_stmt
*pExplain
= 0;
6723 const char *zEQP
= (const char*)sqlite3_column_text(pSql
, 0);
6724 const char *zGlob
= (const char*)sqlite3_column_text(pSql
, 1);
6725 const char *zFrom
= (const char*)sqlite3_column_text(pSql
, 2);
6726 const char *zTarget
= (const char*)sqlite3_column_text(pSql
, 3);
6727 const char *zCI
= (const char*)sqlite3_column_text(pSql
, 4);
6728 const char *zParent
= (const char*)sqlite3_column_text(pSql
, 5);
6730 if( zEQP
==0 ) continue;
6731 if( zGlob
==0 ) continue;
6732 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
6733 if( rc
!=SQLITE_OK
) break;
6734 if( SQLITE_ROW
==sqlite3_step(pExplain
) ){
6735 const char *zPlan
= (const char*)sqlite3_column_text(pExplain
, 3);
6736 res
= zPlan
!=0 && ( 0==sqlite3_strglob(zGlob
, zPlan
)
6737 || 0==sqlite3_strglob(zGlobIPK
, zPlan
));
6739 rc
= sqlite3_finalize(pExplain
);
6740 if( rc
!=SQLITE_OK
) break;
6743 raw_printf(stderr
, "Error: internal error");
6747 && (bVerbose
|| res
==0)
6748 && (zPrev
==0 || sqlite3_stricmp(zParent
, zPrev
))
6750 raw_printf(out
, "-- Parent table %s\n", zParent
);
6751 sqlite3_free(zPrev
);
6752 zPrev
= sqlite3_mprintf("%s", zParent
);
6756 raw_printf(out
, "%s%s --> %s\n", zIndent
, zCI
, zTarget
);
6757 }else if( bVerbose
){
6758 raw_printf(out
, "%s/* no extra indexes required for %s -> %s */\n",
6759 zIndent
, zFrom
, zTarget
6764 sqlite3_free(zPrev
);
6766 if( rc
!=SQLITE_OK
){
6767 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
6770 rc2
= sqlite3_finalize(pSql
);
6771 if( rc
==SQLITE_OK
&& rc2
!=SQLITE_OK
){
6773 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
6776 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
6783 ** Implementation of ".lint" dot command.
6785 static int lintDotCommand(
6786 ShellState
*pState
, /* Current shell tool state */
6787 char **azArg
, /* Array of arguments passed to dot command */
6788 int nArg
/* Number of entries in azArg[] */
6791 n
= (nArg
>=2 ? strlen30(azArg
[1]) : 0);
6792 if( n
<1 || sqlite3_strnicmp(azArg
[1], "fkey-indexes", n
) ) goto usage
;
6793 return lintFkeyIndexes(pState
, azArg
, nArg
);
6796 raw_printf(stderr
, "Usage %s sub-command ?switches...?\n", azArg
[0]);
6797 raw_printf(stderr
, "Where sub-commands are:\n");
6798 raw_printf(stderr
, " fkey-indexes\n");
6799 return SQLITE_ERROR
;
6802 #if !defined SQLITE_OMIT_VIRTUALTABLE
6803 static void shellPrepare(
6807 sqlite3_stmt
**ppStmt
6810 if( *pRc
==SQLITE_OK
){
6811 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
6812 if( rc
!=SQLITE_OK
){
6813 raw_printf(stderr
, "sql error: %s (%d)\n",
6814 sqlite3_errmsg(db
), sqlite3_errcode(db
)
6822 ** Create a prepared statement using printf-style arguments for the SQL.
6824 ** This routine is could be marked "static". But it is not always used,
6825 ** depending on compile-time options. By omitting the "static", we avoid
6826 ** nuisance compiler warnings about "defined but not used".
6828 void shellPreparePrintf(
6831 sqlite3_stmt
**ppStmt
,
6836 if( *pRc
==SQLITE_OK
){
6840 z
= sqlite3_vmprintf(zFmt
, ap
);
6843 *pRc
= SQLITE_NOMEM
;
6845 shellPrepare(db
, pRc
, z
, ppStmt
);
6851 /* Finalize the prepared statement created using shellPreparePrintf().
6853 ** This routine is could be marked "static". But it is not always used,
6854 ** depending on compile-time options. By omitting the "static", we avoid
6855 ** nuisance compiler warnings about "defined but not used".
6862 sqlite3
*db
= sqlite3_db_handle(pStmt
);
6863 int rc
= sqlite3_finalize(pStmt
);
6864 if( *pRc
==SQLITE_OK
){
6865 if( rc
!=SQLITE_OK
){
6866 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
6873 /* Reset the prepared statement created using shellPreparePrintf().
6875 ** This routine is could be marked "static". But it is not always used,
6876 ** depending on compile-time options. By omitting the "static", we avoid
6877 ** nuisance compiler warnings about "defined but not used".
6883 int rc
= sqlite3_reset(pStmt
);
6884 if( *pRc
==SQLITE_OK
){
6885 if( rc
!=SQLITE_OK
){
6886 sqlite3
*db
= sqlite3_db_handle(pStmt
);
6887 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
6892 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
6894 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
6895 /******************************************************************************
6896 ** The ".archive" or ".ar" command.
6899 ** Structure representing a single ".ar" command.
6901 typedef struct ArCommand ArCommand
;
6903 u8 eCmd
; /* An AR_CMD_* value */
6904 u8 bVerbose
; /* True if --verbose */
6905 u8 bZip
; /* True if the archive is a ZIP */
6906 u8 bDryRun
; /* True if --dry-run */
6907 u8 bAppend
; /* True if --append */
6908 u8 bGlob
; /* True if --glob */
6909 u8 fromCmdLine
; /* Run from -A instead of .archive */
6910 int nArg
; /* Number of command arguments */
6911 char *zSrcTable
; /* "sqlar", "zipfile($file)" or "zip" */
6912 const char *zFile
; /* --file argument, or NULL */
6913 const char *zDir
; /* --directory argument, or NULL */
6914 char **azArg
; /* Array of command arguments */
6915 ShellState
*p
; /* Shell state */
6916 sqlite3
*db
; /* Database containing the archive */
6920 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
6922 static int arUsage(FILE *f
){
6923 showHelp(f
,"archive");
6924 return SQLITE_ERROR
;
6928 ** Print an error message for the .ar command to stderr and return
6931 static int arErrorMsg(ArCommand
*pAr
, const char *zFmt
, ...){
6935 z
= sqlite3_vmprintf(zFmt
, ap
);
6937 utf8_printf(stderr
, "Error: %s\n", z
);
6938 if( pAr
->fromCmdLine
){
6939 utf8_printf(stderr
, "Use \"-A\" for more help\n");
6941 utf8_printf(stderr
, "Use \".archive --help\" for more help\n");
6944 return SQLITE_ERROR
;
6948 ** Values for ArCommand.eCmd.
6950 #define AR_CMD_CREATE 1
6951 #define AR_CMD_UPDATE 2
6952 #define AR_CMD_INSERT 3
6953 #define AR_CMD_EXTRACT 4
6954 #define AR_CMD_LIST 5
6955 #define AR_CMD_HELP 6
6956 #define AR_CMD_REMOVE 7
6959 ** Other (non-command) switches.
6961 #define AR_SWITCH_VERBOSE 8
6962 #define AR_SWITCH_FILE 9
6963 #define AR_SWITCH_DIRECTORY 10
6964 #define AR_SWITCH_APPEND 11
6965 #define AR_SWITCH_DRYRUN 12
6966 #define AR_SWITCH_GLOB 13
6968 static int arProcessSwitch(ArCommand
*pAr
, int eSwitch
, const char *zArg
){
6971 case AR_CMD_EXTRACT
:
6978 return arErrorMsg(pAr
, "multiple command options");
6980 pAr
->eCmd
= eSwitch
;
6983 case AR_SWITCH_DRYRUN
:
6986 case AR_SWITCH_GLOB
:
6989 case AR_SWITCH_VERBOSE
:
6992 case AR_SWITCH_APPEND
:
6994 deliberate_fall_through
;
6995 case AR_SWITCH_FILE
:
6998 case AR_SWITCH_DIRECTORY
:
7007 ** Parse the command line for an ".ar" command. The results are written into
7008 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
7009 ** successfully, otherwise an error message is written to stderr and
7010 ** SQLITE_ERROR returned.
7012 static int arParseCommand(
7013 char **azArg
, /* Array of arguments passed to dot command */
7014 int nArg
, /* Number of entries in azArg[] */
7015 ArCommand
*pAr
/* Populate this object */
7023 { "create", 'c', AR_CMD_CREATE
, 0 },
7024 { "extract", 'x', AR_CMD_EXTRACT
, 0 },
7025 { "insert", 'i', AR_CMD_INSERT
, 0 },
7026 { "list", 't', AR_CMD_LIST
, 0 },
7027 { "remove", 'r', AR_CMD_REMOVE
, 0 },
7028 { "update", 'u', AR_CMD_UPDATE
, 0 },
7029 { "help", 'h', AR_CMD_HELP
, 0 },
7030 { "verbose", 'v', AR_SWITCH_VERBOSE
, 0 },
7031 { "file", 'f', AR_SWITCH_FILE
, 1 },
7032 { "append", 'a', AR_SWITCH_APPEND
, 1 },
7033 { "directory", 'C', AR_SWITCH_DIRECTORY
, 1 },
7034 { "dryrun", 'n', AR_SWITCH_DRYRUN
, 0 },
7035 { "glob", 'g', AR_SWITCH_GLOB
, 0 },
7037 int nSwitch
= sizeof(aSwitch
) / sizeof(struct ArSwitch
);
7038 struct ArSwitch
*pEnd
= &aSwitch
[nSwitch
];
7041 utf8_printf(stderr
, "Wrong number of arguments. Usage:\n");
7042 return arUsage(stderr
);
7046 /* Traditional style [tar] invocation */
7049 for(i
=0; z
[i
]; i
++){
7050 const char *zArg
= 0;
7051 struct ArSwitch
*pOpt
;
7052 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7053 if( z
[i
]==pOpt
->cShort
) break;
7056 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
7060 return arErrorMsg(pAr
, "option requires an argument: %c",z
[i
]);
7062 zArg
= azArg
[iArg
++];
7064 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7066 pAr
->nArg
= nArg
-iArg
;
7068 pAr
->azArg
= &azArg
[iArg
];
7071 /* Non-traditional invocation */
7073 for(iArg
=1; iArg
<nArg
; iArg
++){
7077 /* All remaining command line words are command arguments. */
7078 pAr
->azArg
= &azArg
[iArg
];
7079 pAr
->nArg
= nArg
-iArg
;
7086 /* One or more short options */
7088 const char *zArg
= 0;
7089 struct ArSwitch
*pOpt
;
7090 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7091 if( z
[i
]==pOpt
->cShort
) break;
7094 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
7101 if( iArg
>=(nArg
-1) ){
7102 return arErrorMsg(pAr
, "option requires an argument: %c",
7105 zArg
= azArg
[++iArg
];
7108 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7110 }else if( z
[2]=='\0' ){
7111 /* A -- option, indicating that all remaining command line words
7112 ** are command arguments. */
7113 pAr
->azArg
= &azArg
[iArg
+1];
7114 pAr
->nArg
= nArg
-iArg
-1;
7118 const char *zArg
= 0; /* Argument for option, if any */
7119 struct ArSwitch
*pMatch
= 0; /* Matching option */
7120 struct ArSwitch
*pOpt
; /* Iterator */
7121 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7122 const char *zLong
= pOpt
->zLong
;
7123 if( (n
-2)<=strlen30(zLong
) && 0==memcmp(&z
[2], zLong
, n
-2) ){
7125 return arErrorMsg(pAr
, "ambiguous option: %s",z
);
7133 return arErrorMsg(pAr
, "unrecognized option: %s", z
);
7136 if( iArg
>=(nArg
-1) ){
7137 return arErrorMsg(pAr
, "option requires an argument: %s", z
);
7139 zArg
= azArg
[++iArg
];
7141 if( arProcessSwitch(pAr
, pMatch
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7147 utf8_printf(stderr
, "Required argument missing. Usage:\n");
7148 return arUsage(stderr
);
7154 ** This function assumes that all arguments within the ArCommand.azArg[]
7155 ** array refer to archive members, as for the --extract, --list or --remove
7156 ** commands. It checks that each of them are "present". If any specified
7157 ** file is not present in the archive, an error is printed to stderr and an
7158 ** error code returned. Otherwise, if all specified arguments are present
7159 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
7160 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
7161 ** when pAr->bGlob is true.
7163 ** This function strips any trailing '/' characters from each argument.
7164 ** This is consistent with the way the [tar] command seems to work on
7167 static int arCheckEntries(ArCommand
*pAr
){
7171 sqlite3_stmt
*pTest
= 0;
7172 const char *zSel
= (pAr
->bGlob
)
7173 ? "SELECT name FROM %s WHERE glob($name,name)"
7174 : "SELECT name FROM %s WHERE name=$name";
7176 shellPreparePrintf(pAr
->db
, &rc
, &pTest
, zSel
, pAr
->zSrcTable
);
7177 j
= sqlite3_bind_parameter_index(pTest
, "$name");
7178 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
7179 char *z
= pAr
->azArg
[i
];
7180 int n
= strlen30(z
);
7182 while( n
>0 && z
[n
-1]=='/' ) n
--;
7184 sqlite3_bind_text(pTest
, j
, z
, -1, SQLITE_STATIC
);
7185 if( SQLITE_ROW
==sqlite3_step(pTest
) ){
7188 shellReset(&rc
, pTest
);
7189 if( rc
==SQLITE_OK
&& bOk
==0 ){
7190 utf8_printf(stderr
, "not found in archive: %s\n", z
);
7194 shellFinalize(&rc
, pTest
);
7200 ** Format a WHERE clause that can be used against the "sqlar" table to
7201 ** identify all archive members that match the command arguments held
7202 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
7203 ** The caller is responsible for eventually calling sqlite3_free() on
7204 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
7205 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
7207 static void arWhereClause(
7210 char **pzWhere
/* OUT: New WHERE clause */
7213 const char *zSameOp
= (pAr
->bGlob
)? "GLOB" : "=";
7214 if( *pRc
==SQLITE_OK
){
7216 zWhere
= sqlite3_mprintf("1");
7219 const char *zSep
= "";
7220 for(i
=0; i
<pAr
->nArg
; i
++){
7221 const char *z
= pAr
->azArg
[i
];
7222 zWhere
= sqlite3_mprintf(
7223 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
7224 zWhere
, zSep
, zSameOp
, z
, strlen30(z
)+1, zSameOp
, z
7227 *pRc
= SQLITE_NOMEM
;
7238 ** Implementation of .ar "lisT" command.
7240 static int arListCommand(ArCommand
*pAr
){
7241 const char *zSql
= "SELECT %s FROM %s WHERE %s";
7242 const char *azCols
[] = {
7244 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
7248 sqlite3_stmt
*pSql
= 0;
7251 rc
= arCheckEntries(pAr
);
7252 arWhereClause(&rc
, pAr
, &zWhere
);
7254 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql
, azCols
[pAr
->bVerbose
],
7255 pAr
->zSrcTable
, zWhere
);
7257 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
7259 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
7260 if( pAr
->bVerbose
){
7261 utf8_printf(pAr
->p
->out
, "%s % 10d %s %s\n",
7262 sqlite3_column_text(pSql
, 0),
7263 sqlite3_column_int(pSql
, 1),
7264 sqlite3_column_text(pSql
, 2),
7265 sqlite3_column_text(pSql
, 3)
7268 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
7272 shellFinalize(&rc
, pSql
);
7273 sqlite3_free(zWhere
);
7279 ** Implementation of .ar "Remove" command.
7281 static int arRemoveCommand(ArCommand
*pAr
){
7287 /* Verify that args actually exist within the archive before proceeding.
7288 ** And formulate a WHERE clause to match them. */
7289 rc
= arCheckEntries(pAr
);
7290 arWhereClause(&rc
, pAr
, &zWhere
);
7292 if( rc
==SQLITE_OK
){
7293 zSql
= sqlite3_mprintf("DELETE FROM %s WHERE %s;",
7294 pAr
->zSrcTable
, zWhere
);
7296 utf8_printf(pAr
->p
->out
, "%s\n", zSql
);
7299 rc
= sqlite3_exec(pAr
->db
, "SAVEPOINT ar;", 0, 0, 0);
7300 if( rc
==SQLITE_OK
){
7301 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
7302 if( rc
!=SQLITE_OK
){
7303 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7305 rc
= sqlite3_exec(pAr
->db
, "RELEASE ar;", 0, 0, 0);
7309 utf8_printf(stdout
, "ERROR: %s\n", zErr
);
7314 sqlite3_free(zWhere
);
7320 ** Implementation of .ar "eXtract" command.
7322 static int arExtractCommand(ArCommand
*pAr
){
7326 " writefile(($dir || name), %s, mode, mtime) "
7327 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
7328 " AND name NOT GLOB '*..[/\\]*'";
7330 const char *azExtraArg
[] = {
7331 "sqlar_uncompress(data, sz)",
7335 sqlite3_stmt
*pSql
= 0;
7341 /* If arguments are specified, check that they actually exist within
7342 ** the archive before proceeding. And formulate a WHERE clause to
7344 rc
= arCheckEntries(pAr
);
7345 arWhereClause(&rc
, pAr
, &zWhere
);
7347 if( rc
==SQLITE_OK
){
7349 zDir
= sqlite3_mprintf("%s/", pAr
->zDir
);
7351 zDir
= sqlite3_mprintf("");
7353 if( zDir
==0 ) rc
= SQLITE_NOMEM
;
7356 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql1
,
7357 azExtraArg
[pAr
->bZip
], pAr
->zSrcTable
, zWhere
7360 if( rc
==SQLITE_OK
){
7361 j
= sqlite3_bind_parameter_index(pSql
, "$dir");
7362 sqlite3_bind_text(pSql
, j
, zDir
, -1, SQLITE_STATIC
);
7364 /* Run the SELECT statement twice. The first time, writefile() is called
7365 ** for all archive members that should be extracted. The second time,
7366 ** only for the directories. This is because the timestamps for
7367 ** extracted directories must be reset after they are populated (as
7368 ** populating them changes the timestamp). */
7370 j
= sqlite3_bind_parameter_index(pSql
, "$dirOnly");
7371 sqlite3_bind_int(pSql
, j
, i
);
7373 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
7375 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
7376 if( i
==0 && pAr
->bVerbose
){
7377 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
7381 shellReset(&rc
, pSql
);
7383 shellFinalize(&rc
, pSql
);
7387 sqlite3_free(zWhere
);
7392 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
7394 static int arExecSql(ArCommand
*pAr
, const char *zSql
){
7397 utf8_printf(pAr
->p
->out
, "%s\n", zSql
);
7401 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
7403 utf8_printf(stdout
, "ERROR: %s\n", zErr
);
7412 ** Implementation of .ar "create", "insert", and "update" commands.
7414 ** create -> Create a new SQL archive
7415 ** insert -> Insert or reinsert all files listed
7416 ** update -> Insert files that have changed or that were not
7417 ** previously in the archive
7419 ** Create the "sqlar" table in the database if it does not already exist.
7420 ** Then add each file in the azFile[] array to the archive. Directories
7421 ** are added recursively. If argument bVerbose is non-zero, a message is
7422 ** printed on stdout for each file archived.
7424 ** The create command is the same as update, except that it drops
7425 ** any existing "sqlar" table before beginning. The "insert" command
7426 ** always overwrites every file named on the command-line, where as
7427 ** "update" only overwrites if the size or mtime or mode has changed.
7429 static int arCreateOrUpdateCommand(
7430 ArCommand
*pAr
, /* Command arguments and options */
7431 int bUpdate
, /* true for a --create. */
7432 int bOnlyIfChanged
/* Only update if file has changed */
7434 const char *zCreate
=
7435 "CREATE TABLE IF NOT EXISTS sqlar(\n"
7436 " name TEXT PRIMARY KEY, -- name of the file\n"
7437 " mode INT, -- access permissions\n"
7438 " mtime INT, -- last modification time\n"
7439 " sz INT, -- original file size\n"
7440 " data BLOB -- compressed content\n"
7442 const char *zDrop
= "DROP TABLE IF EXISTS sqlar";
7443 const char *zInsertFmt
[2] = {
7444 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7449 " CASE substr(lsmode(mode),1,1)\n"
7450 " WHEN '-' THEN length(data)\n"
7451 " WHEN 'd' THEN 0\n"
7453 " sqlar_compress(data)\n"
7454 " FROM fsdir(%Q,%Q) AS disk\n"
7455 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7457 "REPLACE INTO %s(name,mode,mtime,data)\n"
7463 " FROM fsdir(%Q,%Q) AS disk\n"
7464 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7466 int i
; /* For iterating through azFile[] */
7467 int rc
; /* Return code */
7468 const char *zTab
= 0; /* SQL table into which to insert */
7473 arExecSql(pAr
, "PRAGMA page_size=512");
7474 rc
= arExecSql(pAr
, "SAVEPOINT ar;");
7475 if( rc
!=SQLITE_OK
) return rc
;
7478 /* Initialize the zipfile virtual table, if necessary */
7481 sqlite3_randomness(sizeof(r
),&r
);
7482 sqlite3_snprintf(sizeof(zTemp
),zTemp
,"zip%016llx",r
);
7484 zSql
= sqlite3_mprintf(
7485 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7488 rc
= arExecSql(pAr
, zSql
);
7494 /* Initialize the table for an SQLAR */
7497 rc
= arExecSql(pAr
, zDrop
);
7498 if( rc
!=SQLITE_OK
) goto end_ar_transaction
;
7500 rc
= arExecSql(pAr
, zCreate
);
7502 if( bOnlyIfChanged
){
7503 zExists
= sqlite3_mprintf(
7505 "SELECT 1 FROM %s AS mem"
7506 " WHERE mem.name=disk.name"
7507 " AND mem.mtime=disk.mtime"
7508 " AND mem.mode=disk.mode)", zTab
);
7510 zExists
= sqlite3_mprintf("");
7512 if( zExists
==0 ) rc
= SQLITE_NOMEM
;
7513 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
7514 char *zSql2
= sqlite3_mprintf(zInsertFmt
[pAr
->bZip
], zTab
,
7515 pAr
->bVerbose
? "shell_putsnl(name)" : "name",
7516 pAr
->azArg
[i
], pAr
->zDir
, zExists
);
7517 rc
= arExecSql(pAr
, zSql2
);
7518 sqlite3_free(zSql2
);
7521 if( rc
!=SQLITE_OK
){
7522 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7524 rc
= arExecSql(pAr
, "RELEASE ar;");
7525 if( pAr
->bZip
&& pAr
->zFile
){
7526 zSql
= sqlite3_mprintf("DROP TABLE %s", zTemp
);
7527 arExecSql(pAr
, zSql
);
7531 sqlite3_free(zExists
);
7536 ** Implementation of ".ar" dot command.
7538 static int arDotCommand(
7539 ShellState
*pState
, /* Current shell tool state */
7540 int fromCmdLine
, /* True if -A command-line option, not .ar cmd */
7541 char **azArg
, /* Array of arguments passed to dot command */
7542 int nArg
/* Number of entries in azArg[] */
7546 memset(&cmd
, 0, sizeof(cmd
));
7547 cmd
.fromCmdLine
= fromCmdLine
;
7548 rc
= arParseCommand(azArg
, nArg
, &cmd
);
7549 if( rc
==SQLITE_OK
){
7550 int eDbType
= SHELL_OPEN_UNSPEC
;
7552 cmd
.db
= pState
->db
;
7554 eDbType
= deduceDatabaseType(cmd
.zFile
, 1);
7556 eDbType
= pState
->openMode
;
7558 if( eDbType
==SHELL_OPEN_ZIPFILE
){
7559 if( cmd
.eCmd
==AR_CMD_EXTRACT
|| cmd
.eCmd
==AR_CMD_LIST
){
7561 cmd
.zSrcTable
= sqlite3_mprintf("zip");
7563 cmd
.zSrcTable
= sqlite3_mprintf("zipfile(%Q)", cmd
.zFile
);
7567 }else if( cmd
.zFile
){
7569 if( cmd
.bAppend
) eDbType
= SHELL_OPEN_APPENDVFS
;
7570 if( cmd
.eCmd
==AR_CMD_CREATE
|| cmd
.eCmd
==AR_CMD_INSERT
7571 || cmd
.eCmd
==AR_CMD_REMOVE
|| cmd
.eCmd
==AR_CMD_UPDATE
){
7572 flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
7574 flags
= SQLITE_OPEN_READONLY
;
7578 utf8_printf(pState
->out
, "-- open database '%s'%s\n", cmd
.zFile
,
7579 eDbType
==SHELL_OPEN_APPENDVFS
? " using 'apndvfs'" : "");
7581 rc
= sqlite3_open_v2(cmd
.zFile
, &cmd
.db
, flags
,
7582 eDbType
==SHELL_OPEN_APPENDVFS
? "apndvfs" : 0);
7583 if( rc
!=SQLITE_OK
){
7584 utf8_printf(stderr
, "cannot open file: %s (%s)\n",
7585 cmd
.zFile
, sqlite3_errmsg(cmd
.db
)
7587 goto end_ar_command
;
7589 sqlite3_fileio_init(cmd
.db
, 0, 0);
7590 sqlite3_sqlar_init(cmd
.db
, 0, 0);
7591 sqlite3_create_function(cmd
.db
, "shell_putsnl", 1, SQLITE_UTF8
, cmd
.p
,
7592 shellPutsFunc
, 0, 0);
7595 if( cmd
.zSrcTable
==0 && cmd
.bZip
==0 && cmd
.eCmd
!=AR_CMD_HELP
){
7596 if( cmd
.eCmd
!=AR_CMD_CREATE
7597 && sqlite3_table_column_metadata(cmd
.db
,0,"sqlar","name",0,0,0,0,0)
7599 utf8_printf(stderr
, "database does not contain an 'sqlar' table\n");
7601 goto end_ar_command
;
7603 cmd
.zSrcTable
= sqlite3_mprintf("sqlar");
7608 rc
= arCreateOrUpdateCommand(&cmd
, 0, 0);
7611 case AR_CMD_EXTRACT
:
7612 rc
= arExtractCommand(&cmd
);
7616 rc
= arListCommand(&cmd
);
7620 arUsage(pState
->out
);
7624 rc
= arCreateOrUpdateCommand(&cmd
, 1, 0);
7628 rc
= arRemoveCommand(&cmd
);
7632 assert( cmd
.eCmd
==AR_CMD_UPDATE
);
7633 rc
= arCreateOrUpdateCommand(&cmd
, 1, 1);
7638 if( cmd
.db
!=pState
->db
){
7641 sqlite3_free(cmd
.zSrcTable
);
7645 /* End of the ".archive" or ".ar" command logic
7646 *******************************************************************************/
7647 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7649 #if SQLITE_SHELL_HAVE_RECOVER
7652 ** This function is used as a callback by the recover extension. Simply
7653 ** print the supplied SQL statement to stdout.
7655 static int recoverSqlCb(void *pCtx
, const char *zSql
){
7656 ShellState
*pState
= (ShellState
*)pCtx
;
7657 utf8_printf(pState
->out
, "%s;\n", zSql
);
7662 ** This function is called to recover data from the database. A script
7663 ** to construct a new database containing all recovered data is output
7664 ** on stream pState->out.
7666 static int recoverDatabaseCmd(ShellState
*pState
, int nArg
, char **azArg
){
7668 const char *zRecoveryDb
= ""; /* Name of "recovery" database. Debug only */
7669 const char *zLAF
= "lost_and_found";
7670 int bFreelist
= 1; /* 0 if --ignore-freelist is specified */
7671 int bRowids
= 1; /* 0 if --no-rowids */
7672 sqlite3_recover
*p
= 0;
7675 for(i
=1; i
<nArg
; i
++){
7678 if( z
[0]=='-' && z
[1]=='-' ) z
++;
7680 if( n
<=17 && memcmp("-ignore-freelist", z
, n
)==0 ){
7683 if( n
<=12 && memcmp("-recovery-db", z
, n
)==0 && i
<(nArg
-1) ){
7684 /* This option determines the name of the ATTACH-ed database used
7685 ** internally by the recovery extension. The default is "" which
7686 ** means to use a temporary database that is automatically deleted
7687 ** when closed. This option is undocumented and might disappear at
7690 zRecoveryDb
= azArg
[i
];
7692 if( n
<=15 && memcmp("-lost-and-found", z
, n
)==0 && i
<(nArg
-1) ){
7696 if( n
<=10 && memcmp("-no-rowids", z
, n
)==0 ){
7700 utf8_printf(stderr
, "unexpected option: %s\n", azArg
[i
]);
7701 showHelp(pState
->out
, azArg
[0]);
7706 p
= sqlite3_recover_init_sql(
7707 pState
->db
, "main", recoverSqlCb
, (void*)pState
7710 sqlite3_recover_config(p
, 789, (void*)zRecoveryDb
); /* Debug use only */
7711 sqlite3_recover_config(p
, SQLITE_RECOVER_LOST_AND_FOUND
, (void*)zLAF
);
7712 sqlite3_recover_config(p
, SQLITE_RECOVER_ROWIDS
, (void*)&bRowids
);
7713 sqlite3_recover_config(p
, SQLITE_RECOVER_FREELIST_CORRUPT
,(void*)&bFreelist
);
7715 sqlite3_recover_run(p
);
7716 if( sqlite3_recover_errcode(p
)!=SQLITE_OK
){
7717 const char *zErr
= sqlite3_recover_errmsg(p
);
7718 int errCode
= sqlite3_recover_errcode(p
);
7719 raw_printf(stderr
, "sql error: %s (%d)\n", zErr
, errCode
);
7721 rc
= sqlite3_recover_finish(p
);
7724 #endif /* SQLITE_SHELL_HAVE_RECOVER */
7728 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7729 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7730 * close db and set it to 0, and return the columns spec, to later
7731 * be sqlite3_free()'ed by the caller.
7732 * The return is 0 when either:
7733 * (a) The db was not initialized and zCol==0 (There are no columns.)
7734 * (b) zCol!=0 (Column was added, db initialized as needed.)
7735 * The 3rd argument, pRenamed, references an out parameter. If the
7736 * pointer is non-zero, its referent will be set to a summary of renames
7737 * done if renaming was necessary, or set to 0 if none was done. The out
7738 * string (if any) must be sqlite3_free()'ed by the caller.
7741 #define rc_err_oom_die(rc) \
7742 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7743 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7744 fprintf(stderr,"E:%d\n",rc), assert(0)
7746 static void rc_err_oom_die(int rc
){
7747 if( rc
==SQLITE_NOMEM
) shell_check_oom(0);
7748 assert(rc
==SQLITE_OK
||rc
==SQLITE_DONE
);
7752 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7753 static char zCOL_DB
[] = SHELL_STRINGIFY(SHELL_COLFIX_DB
);
7754 #else /* Otherwise, memory is faster/better for the transient DB. */
7755 static const char *zCOL_DB
= ":memory:";
7758 /* Define character (as C string) to separate generated column ordinal
7759 * from protected part of incoming column names. This defaults to "_"
7760 * so that incoming column identifiers that did not need not be quoted
7761 * remain usable without being quoted. It must be one character.
7763 #ifndef SHELL_AUTOCOLUMN_SEP
7764 # define AUTOCOLUMN_SEP "_"
7766 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7769 static char *zAutoColumn(const char *zColNew
, sqlite3
**pDb
, char **pzRenamed
){
7770 /* Queries and D{D,M}L used here */
7771 static const char * const zTabMake
= "\
7772 CREATE TABLE ColNames(\
7773 cpos INTEGER PRIMARY KEY,\
7774 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7775 CREATE VIEW RepeatedNames AS \
7776 SELECT DISTINCT t.name FROM ColNames t \
7777 WHERE t.name COLLATE NOCASE IN (\
7778 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7781 static const char * const zTabFill
= "\
7782 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7783 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7785 static const char * const zHasDupes
= "\
7786 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7787 <count(name) FROM ColNames\
7789 #ifdef SHELL_COLUMN_RENAME_CLEAN
7790 static const char * const zDedoctor
= "\
7791 UPDATE ColNames SET chop=iif(\
7792 (substring(name,nlen,1) BETWEEN '0' AND '9')\
7793 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP
"'),\
7794 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP
"0123456789')),\
7799 static const char * const zSetReps
= "\
7800 UPDATE ColNames AS t SET reps=\
7801 (SELECT count(*) FROM ColNames d \
7802 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
7806 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
7807 static const char * const zColDigits
= "\
7808 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
7811 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
7812 static const char * const zColDigits
= "\
7813 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
7814 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
7815 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
7818 static const char * const zRenameRank
=
7819 #ifdef SHELL_COLUMN_RENAME_CLEAN
7820 "UPDATE ColNames AS t SET suff="
7821 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP
"', $1, cpos), '')"
7822 #else /* ...RENAME_MINIMAL_ONE_PASS */
7823 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
7826 " SELECT nlz+1 AS nlz FROM Lzn"
7829 " FROM ColNames t, ColNames o"
7831 " iif(t.name IN (SELECT * FROM RepeatedNames),"
7832 " printf('%s"AUTOCOLUMN_SEP
"%s',"
7833 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
7837 " iif(o.name IN (SELECT * FROM RepeatedNames),"
7838 " printf('%s"AUTOCOLUMN_SEP
"%s',"
7839 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
7843 " AND o.cpos<>t.cpos"
7846 ") UPDATE Colnames AS t SET"
7847 " chop = 0," /* No chopping, never touch incoming names. */
7848 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
7849 " printf('"AUTOCOLUMN_SEP
"%s', substring("
7850 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
7855 static const char * const zCollectVar
= "\
7860 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
7863 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
7864 FROM ColNames ORDER BY cpos\
7866 static const char * const zRenamesDone
=
7867 "SELECT group_concat("
7868 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
7870 "FROM ColNames WHERE suff<>'' OR chop!=0"
7873 sqlite3_stmt
*pStmt
= 0;
7876 /* Add initial or additional column. Init db if necessary. */
7878 if( SQLITE_OK
!=sqlite3_open(zCOL_DB
, pDb
) ) return 0;
7879 #ifdef SHELL_COLFIX_DB
7881 sqlite3_exec(*pDb
,"drop table if exists ColNames;"
7882 "drop view if exists RepeatedNames;",0,0,0);
7884 rc
= sqlite3_exec(*pDb
, zTabMake
, 0, 0, 0);
7888 rc
= sqlite3_prepare_v2(*pDb
, zTabFill
, -1, &pStmt
, 0);
7890 rc
= sqlite3_bind_text(pStmt
, 1, zColNew
, -1, 0);
7892 rc
= sqlite3_step(pStmt
);
7894 sqlite3_finalize(pStmt
);
7896 }else if( *pDb
==0 ){
7899 /* Formulate the columns spec, close the DB, zero *pDb. */
7900 char *zColsSpec
= 0;
7901 int hasDupes
= db_int(*pDb
, zHasDupes
);
7902 int nDigits
= (hasDupes
)? db_int(*pDb
, zColDigits
) : 0;
7904 #ifdef SHELL_COLUMN_RENAME_CLEAN
7905 rc
= sqlite3_exec(*pDb
, zDedoctor
, 0, 0, 0);
7908 rc
= sqlite3_exec(*pDb
, zSetReps
, 0, 0, 0);
7910 rc
= sqlite3_prepare_v2(*pDb
, zRenameRank
, -1, &pStmt
, 0);
7912 sqlite3_bind_int(pStmt
, 1, nDigits
);
7913 rc
= sqlite3_step(pStmt
);
7914 sqlite3_finalize(pStmt
);
7915 if( rc
!=SQLITE_DONE
) rc_err_oom_die(SQLITE_NOMEM
);
7917 assert(db_int(*pDb
, zHasDupes
)==0); /* Consider: remove this */
7918 rc
= sqlite3_prepare_v2(*pDb
, zCollectVar
, -1, &pStmt
, 0);
7920 rc
= sqlite3_step(pStmt
);
7921 if( rc
==SQLITE_ROW
){
7922 zColsSpec
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
7927 if( !hasDupes
) *pzRenamed
= 0;
7929 sqlite3_finalize(pStmt
);
7930 if( SQLITE_OK
==sqlite3_prepare_v2(*pDb
, zRenamesDone
, -1, &pStmt
, 0)
7931 && SQLITE_ROW
==sqlite3_step(pStmt
) ){
7932 *pzRenamed
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
7937 sqlite3_finalize(pStmt
);
7938 sqlite3_close(*pDb
);
7945 ** If an input line begins with "." then invoke this routine to
7946 ** process that line.
7948 ** Return 1 on error, 2 to exit, and 0 otherwise.
7950 static int do_meta_command(char *zLine
, ShellState
*p
){
7957 #ifndef SQLITE_OMIT_VIRTUALTABLE
7958 if( p
->expert
.pExpert
){
7959 expertFinish(p
, 1, 0);
7963 /* Parse the input line into tokens.
7965 while( zLine
[h
] && nArg
<ArraySize(azArg
)-1 ){
7966 while( IsSpace(zLine
[h
]) ){ h
++; }
7967 if( zLine
[h
]==0 ) break;
7968 if( zLine
[h
]=='\'' || zLine
[h
]=='"' ){
7969 int delim
= zLine
[h
++];
7970 azArg
[nArg
++] = &zLine
[h
];
7971 while( zLine
[h
] && zLine
[h
]!=delim
){
7972 if( zLine
[h
]=='\\' && delim
=='"' && zLine
[h
+1]!=0 ) h
++;
7975 if( zLine
[h
]==delim
){
7978 if( delim
=='"' ) resolve_backslashes(azArg
[nArg
-1]);
7980 azArg
[nArg
++] = &zLine
[h
];
7981 while( zLine
[h
] && !IsSpace(zLine
[h
]) ){ h
++; }
7982 if( zLine
[h
] ) zLine
[h
++] = 0;
7983 resolve_backslashes(azArg
[nArg
-1]);
7988 /* Process the input line.
7990 if( nArg
==0 ) return 0; /* no tokens, no error */
7991 n
= strlen30(azArg
[0]);
7995 #ifndef SQLITE_OMIT_AUTHORIZATION
7996 if( c
=='a' && cli_strncmp(azArg
[0], "auth", n
)==0 ){
7998 raw_printf(stderr
, "Usage: .auth ON|OFF\n");
8000 goto meta_command_exit
;
8003 if( booleanValue(azArg
[1]) ){
8004 sqlite3_set_authorizer(p
->db
, shellAuth
, p
);
8005 }else if( p
->bSafeModePersist
){
8006 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
8008 sqlite3_set_authorizer(p
->db
, 0, 0);
8013 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8014 && !defined(SQLITE_SHELL_FIDDLE)
8015 if( c
=='a' && cli_strncmp(azArg
[0], "archive", n
)==0 ){
8017 failIfSafeMode(p
, "cannot run .archive in safe mode");
8018 rc
= arDotCommand(p
, 0, azArg
, nArg
);
8022 #ifndef SQLITE_SHELL_FIDDLE
8023 if( (c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "backup", n
)==0)
8024 || (c
=='s' && n
>=3 && cli_strncmp(azArg
[0], "save", n
)==0)
8026 const char *zDestFile
= 0;
8027 const char *zDb
= 0;
8029 sqlite3_backup
*pBackup
;
8032 const char *zVfs
= 0;
8033 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
8034 for(j
=1; j
<nArg
; j
++){
8035 const char *z
= azArg
[j
];
8037 if( z
[1]=='-' ) z
++;
8038 if( cli_strcmp(z
, "-append")==0 ){
8041 if( cli_strcmp(z
, "-async")==0 ){
8045 utf8_printf(stderr
, "unknown option: %s\n", azArg
[j
]);
8048 }else if( zDestFile
==0 ){
8049 zDestFile
= azArg
[j
];
8052 zDestFile
= azArg
[j
];
8054 raw_printf(stderr
, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8059 raw_printf(stderr
, "missing FILENAME argument on .backup\n");
8062 if( zDb
==0 ) zDb
= "main";
8063 rc
= sqlite3_open_v2(zDestFile
, &pDest
,
8064 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
, zVfs
);
8065 if( rc
!=SQLITE_OK
){
8066 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zDestFile
);
8071 sqlite3_exec(pDest
, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8075 pBackup
= sqlite3_backup_init(pDest
, "main", p
->db
, zDb
);
8077 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
8081 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
){}
8082 sqlite3_backup_finish(pBackup
);
8083 if( rc
==SQLITE_DONE
){
8086 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
8091 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8093 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "bail", n
)==0 ){
8095 bail_on_error
= booleanValue(azArg
[1]);
8097 raw_printf(stderr
, "Usage: .bail on|off\n");
8102 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "binary", n
)==0 ){
8104 if( booleanValue(azArg
[1]) ){
8105 setBinaryMode(p
->out
, 1);
8107 setTextMode(p
->out
, 1);
8110 raw_printf(stderr
, "Usage: .binary on|off\n");
8115 /* The undocumented ".breakpoint" command causes a call to the no-op
8116 ** routine named test_breakpoint().
8118 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "breakpoint", n
)==0 ){
8122 #ifndef SQLITE_SHELL_FIDDLE
8123 if( c
=='c' && cli_strcmp(azArg
[0],"cd")==0 ){
8124 failIfSafeMode(p
, "cannot run .cd in safe mode");
8126 #if defined(_WIN32) || defined(WIN32)
8127 wchar_t *z
= sqlite3_win32_utf8_to_unicode(azArg
[1]);
8128 rc
= !SetCurrentDirectoryW(z
);
8131 rc
= chdir(azArg
[1]);
8134 utf8_printf(stderr
, "Cannot change to directory \"%s\"\n", azArg
[1]);
8138 raw_printf(stderr
, "Usage: .cd DIRECTORY\n");
8142 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8144 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "changes", n
)==0 ){
8146 setOrClearFlag(p
, SHFLG_CountChanges
, azArg
[1]);
8148 raw_printf(stderr
, "Usage: .changes on|off\n");
8153 #ifndef SQLITE_SHELL_FIDDLE
8154 /* Cancel output redirection, if it is currently set (by .testcase)
8155 ** Then read the content of the testcase-out.txt file and compare against
8156 ** azArg[1]. If there are differences, report an error and exit.
8158 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "check", n
)==0 ){
8162 raw_printf(stderr
, "Usage: .check GLOB-PATTERN\n");
8164 }else if( (zRes
= readFile("testcase-out.txt", 0))==0 ){
8166 }else if( testcase_glob(azArg
[1],zRes
)==0 ){
8168 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
8169 p
->zTestcase
, azArg
[1], zRes
);
8172 utf8_printf(stdout
, "testcase-%s ok\n", p
->zTestcase
);
8177 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8179 #ifndef SQLITE_SHELL_FIDDLE
8180 if( c
=='c' && cli_strncmp(azArg
[0], "clone", n
)==0 ){
8181 failIfSafeMode(p
, "cannot run .clone in safe mode");
8183 tryToClone(p
, azArg
[1]);
8185 raw_printf(stderr
, "Usage: .clone FILENAME\n");
8189 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8191 if( c
=='c' && cli_strncmp(azArg
[0], "connection", n
)==0 ){
8193 /* List available connections */
8195 for(i
=0; i
<ArraySize(p
->aAuxDb
); i
++){
8196 const char *zFile
= p
->aAuxDb
[i
].zDbFilename
;
8197 if( p
->aAuxDb
[i
].db
==0 && p
->pAuxDb
!=&p
->aAuxDb
[i
] ){
8198 zFile
= "(not open)";
8199 }else if( zFile
==0 ){
8201 }else if( zFile
[0]==0 ){
8202 zFile
= "(temporary-file)";
8204 if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
8205 utf8_printf(stdout
, "ACTIVE %d: %s\n", i
, zFile
);
8206 }else if( p
->aAuxDb
[i
].db
!=0 ){
8207 utf8_printf(stdout
, " %d: %s\n", i
, zFile
);
8210 }else if( nArg
==2 && IsDigit(azArg
[1][0]) && azArg
[1][1]==0 ){
8211 int i
= azArg
[1][0] - '0';
8212 if( p
->pAuxDb
!= &p
->aAuxDb
[i
] && i
>=0 && i
<ArraySize(p
->aAuxDb
) ){
8213 p
->pAuxDb
->db
= p
->db
;
8214 p
->pAuxDb
= &p
->aAuxDb
[i
];
8215 globalDb
= p
->db
= p
->pAuxDb
->db
;
8218 }else if( nArg
==3 && cli_strcmp(azArg
[1], "close")==0
8219 && IsDigit(azArg
[2][0]) && azArg
[2][1]==0 ){
8220 int i
= azArg
[2][0] - '0';
8221 if( i
<0 || i
>=ArraySize(p
->aAuxDb
) ){
8223 }else if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
8224 raw_printf(stderr
, "cannot close the active database connection\n");
8226 }else if( p
->aAuxDb
[i
].db
){
8227 session_close_all(p
, i
);
8228 close_db(p
->aAuxDb
[i
].db
);
8229 p
->aAuxDb
[i
].db
= 0;
8232 raw_printf(stderr
, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
8237 if( c
=='d' && n
>1 && cli_strncmp(azArg
[0], "databases", n
)==0 ){
8240 sqlite3_stmt
*pStmt
;
8243 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
8245 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
8248 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
8249 const char *zSchema
= (const char *)sqlite3_column_text(pStmt
,1);
8250 const char *zFile
= (const char*)sqlite3_column_text(pStmt
,2);
8251 if( zSchema
==0 || zFile
==0 ) continue;
8252 azName
= sqlite3_realloc(azName
, (nName
+1)*2*sizeof(char*));
8253 shell_check_oom(azName
);
8254 azName
[nName
*2] = strdup(zSchema
);
8255 azName
[nName
*2+1] = strdup(zFile
);
8259 sqlite3_finalize(pStmt
);
8260 for(i
=0; i
<nName
; i
++){
8261 int eTxn
= sqlite3_txn_state(p
->db
, azName
[i
*2]);
8262 int bRdonly
= sqlite3_db_readonly(p
->db
, azName
[i
*2]);
8263 const char *z
= azName
[i
*2+1];
8264 utf8_printf(p
->out
, "%s: %s %s%s\n",
8266 z
&& z
[0] ? z
: "\"\"",
8267 bRdonly
? "r/o" : "r/w",
8268 eTxn
==SQLITE_TXN_NONE
? "" :
8269 eTxn
==SQLITE_TXN_READ
? " read-txn" : " write-txn");
8271 free(azName
[i
*2+1]);
8273 sqlite3_free(azName
);
8276 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbconfig", n
)==0 ){
8277 static const struct DbConfigChoices
{
8281 { "defensive", SQLITE_DBCONFIG_DEFENSIVE
},
8282 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL
},
8283 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML
},
8284 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY
},
8285 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG
},
8286 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER
},
8287 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW
},
8288 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
},
8289 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE
},
8290 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT
},
8291 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
},
8292 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
},
8293 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE
},
8294 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER
},
8295 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS
},
8296 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP
},
8297 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA
},
8298 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA
},
8302 for(ii
=0; ii
<ArraySize(aDbConfig
); ii
++){
8303 if( nArg
>1 && cli_strcmp(azArg
[1], aDbConfig
[ii
].zName
)!=0 ) continue;
8305 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, booleanValue(azArg
[2]), 0);
8307 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, -1, &v
);
8308 utf8_printf(p
->out
, "%19s %s\n", aDbConfig
[ii
].zName
, v
? "on" : "off");
8311 if( nArg
>1 && ii
==ArraySize(aDbConfig
) ){
8312 utf8_printf(stderr
, "Error: unknown dbconfig \"%s\"\n", azArg
[1]);
8313 utf8_printf(stderr
, "Enter \".dbconfig\" with no arguments for a list\n");
8317 #if SQLITE_SHELL_HAVE_RECOVER
8318 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbinfo", n
)==0 ){
8319 rc
= shell_dbinfo_command(p
, nArg
, azArg
);
8322 if( c
=='r' && cli_strncmp(azArg
[0], "recover", n
)==0 ){
8324 rc
= recoverDatabaseCmd(p
, nArg
, azArg
);
8326 #endif /* SQLITE_SHELL_HAVE_RECOVER */
8328 if( c
=='d' && cli_strncmp(azArg
[0], "dump", n
)==0 ){
8332 int savedShowHeader
= p
->showHeader
;
8333 int savedShellFlags
= p
->shellFlgs
;
8335 SHFLG_PreserveRowid
|SHFLG_Newlines
|SHFLG_Echo
8336 |SHFLG_DumpDataOnly
|SHFLG_DumpNoSys
);
8337 for(i
=1; i
<nArg
; i
++){
8338 if( azArg
[i
][0]=='-' ){
8339 const char *z
= azArg
[i
]+1;
8340 if( z
[0]=='-' ) z
++;
8341 if( cli_strcmp(z
,"preserve-rowids")==0 ){
8342 #ifdef SQLITE_OMIT_VIRTUALTABLE
8343 raw_printf(stderr
, "The --preserve-rowids option is not compatible"
8344 " with SQLITE_OMIT_VIRTUALTABLE\n");
8346 sqlite3_free(zLike
);
8347 goto meta_command_exit
;
8349 ShellSetFlag(p
, SHFLG_PreserveRowid
);
8352 if( cli_strcmp(z
,"newlines")==0 ){
8353 ShellSetFlag(p
, SHFLG_Newlines
);
8355 if( cli_strcmp(z
,"data-only")==0 ){
8356 ShellSetFlag(p
, SHFLG_DumpDataOnly
);
8358 if( cli_strcmp(z
,"nosys")==0 ){
8359 ShellSetFlag(p
, SHFLG_DumpNoSys
);
8362 raw_printf(stderr
, "Unknown option \"%s\" on \".dump\"\n", azArg
[i
]);
8364 sqlite3_free(zLike
);
8365 goto meta_command_exit
;
8368 /* azArg[i] contains a LIKE pattern. This ".dump" request should
8369 ** only dump data for tables for which either the table name matches
8370 ** the LIKE pattern, or the table appears to be a shadow table of
8371 ** a virtual table for which the name matches the LIKE pattern.
8373 char *zExpr
= sqlite3_mprintf(
8374 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8375 " SELECT 1 FROM sqlite_schema WHERE "
8376 " name LIKE %Q ESCAPE '\\' AND"
8377 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8378 " substr(o.name, 1, length(name)+1) == (name||'_')"
8379 ")", azArg
[i
], azArg
[i
]
8383 zLike
= sqlite3_mprintf("%z OR %z", zLike
, zExpr
);
8392 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8393 /* When playing back a "dump", the content might appear in an order
8394 ** which causes immediate foreign key constraints to be violated.
8395 ** So disable foreign-key constraint enforcement to prevent problems. */
8396 raw_printf(p
->out
, "PRAGMA foreign_keys=OFF;\n");
8397 raw_printf(p
->out
, "BEGIN TRANSACTION;\n");
8399 p
->writableSchema
= 0;
8401 /* Set writable_schema=ON since doing so forces SQLite to initialize
8402 ** as much of the schema as it can even if the sqlite_schema table is
8404 sqlite3_exec(p
->db
, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8406 if( zLike
==0 ) zLike
= sqlite3_mprintf("true");
8407 zSql
= sqlite3_mprintf(
8408 "SELECT name, type, sql FROM sqlite_schema AS o "
8409 "WHERE (%s) AND type=='table'"
8411 " ORDER BY tbl_name='sqlite_sequence', rowid",
8414 run_schema_dump_query(p
,zSql
);
8416 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8417 zSql
= sqlite3_mprintf(
8418 "SELECT sql FROM sqlite_schema AS o "
8419 "WHERE (%s) AND sql NOT NULL"
8420 " AND type IN ('index','trigger','view')",
8423 run_table_dump_query(p
, zSql
);
8426 sqlite3_free(zLike
);
8427 if( p
->writableSchema
){
8428 raw_printf(p
->out
, "PRAGMA writable_schema=OFF;\n");
8429 p
->writableSchema
= 0;
8431 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8432 sqlite3_exec(p
->db
, "RELEASE dump;", 0, 0, 0);
8433 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8434 raw_printf(p
->out
, p
->nErr
?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8436 p
->showHeader
= savedShowHeader
;
8437 p
->shellFlgs
= savedShellFlags
;
8440 if( c
=='e' && cli_strncmp(azArg
[0], "echo", n
)==0 ){
8442 setOrClearFlag(p
, SHFLG_Echo
, azArg
[1]);
8444 raw_printf(stderr
, "Usage: .echo on|off\n");
8449 if( c
=='e' && cli_strncmp(azArg
[0], "eqp", n
)==0 ){
8452 if( p
->autoEQPtrace
){
8453 if( p
->db
) sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8454 p
->autoEQPtrace
= 0;
8456 if( cli_strcmp(azArg
[1],"full")==0 ){
8457 p
->autoEQP
= AUTOEQP_full
;
8458 }else if( cli_strcmp(azArg
[1],"trigger")==0 ){
8459 p
->autoEQP
= AUTOEQP_trigger
;
8461 }else if( cli_strcmp(azArg
[1],"test")==0 ){
8462 p
->autoEQP
= AUTOEQP_on
;
8464 }else if( cli_strcmp(azArg
[1],"trace")==0 ){
8465 p
->autoEQP
= AUTOEQP_full
;
8466 p
->autoEQPtrace
= 1;
8468 sqlite3_exec(p
->db
, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8469 sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8472 p
->autoEQP
= (u8
)booleanValue(azArg
[1]);
8475 raw_printf(stderr
, "Usage: .eqp off|on|trace|trigger|full\n");
8480 #ifndef SQLITE_SHELL_FIDDLE
8481 if( c
=='e' && cli_strncmp(azArg
[0], "exit", n
)==0 ){
8482 if( nArg
>1 && (rc
= (int)integerValue(azArg
[1]))!=0 ) exit(rc
);
8487 /* The ".explain" command is automatic now. It is largely pointless. It
8488 ** retained purely for backwards compatibility */
8489 if( c
=='e' && cli_strncmp(azArg
[0], "explain", n
)==0 ){
8492 if( cli_strcmp(azArg
[1],"auto")==0 ){
8495 val
= booleanValue(azArg
[1]);
8498 if( val
==1 && p
->mode
!=MODE_Explain
){
8499 p
->normalMode
= p
->mode
;
8500 p
->mode
= MODE_Explain
;
8503 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
8505 }else if( val
==99 ){
8506 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
8511 #ifndef SQLITE_OMIT_VIRTUALTABLE
8512 if( c
=='e' && cli_strncmp(azArg
[0], "expert", n
)==0 ){
8515 "Cannot run experimental commands such as \"%s\" in safe mode\n",
8520 expertDotCommand(p
, azArg
, nArg
);
8525 if( c
=='f' && cli_strncmp(azArg
[0], "filectrl", n
)==0 ){
8526 static const struct {
8527 const char *zCtrlName
; /* Name of a test-control option */
8528 int ctrlCode
; /* Integer code for that option */
8529 const char *zUsage
; /* Usage notes */
8531 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE
, "SIZE" },
8532 { "data_version", SQLITE_FCNTL_DATA_VERSION
, "" },
8533 { "has_moved", SQLITE_FCNTL_HAS_MOVED
, "" },
8534 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT
, "MILLISEC" },
8535 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL
, "[BOOLEAN]" },
8536 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
8537 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE
, "[BOOLEAN]" },
8538 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES
, "[N]" },
8539 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT
, "[LIMIT]" },
8540 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME
, "" },
8541 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
8545 sqlite3_int64 iRes
= 0; /* Integer result to display if rc2==1 */
8546 int isOk
= 0; /* 0: usage 1: %lld 2: no-result */
8548 const char *zCmd
= 0;
8549 const char *zSchema
= 0;
8552 zCmd
= nArg
>=2 ? azArg
[1] : "help";
8555 && (cli_strcmp(zCmd
,"--schema")==0 || cli_strcmp(zCmd
,"-schema")==0)
8559 for(i
=3; i
<nArg
; i
++) azArg
[i
-2] = azArg
[i
];
8564 /* The argument can optionally begin with "-" or "--" */
8565 if( zCmd
[0]=='-' && zCmd
[1] ){
8567 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
8570 /* --help lists all file-controls */
8571 if( cli_strcmp(zCmd
,"help")==0 ){
8572 utf8_printf(p
->out
, "Available file-controls:\n");
8573 for(i
=0; i
<ArraySize(aCtrl
); i
++){
8574 utf8_printf(p
->out
, " .filectrl %s %s\n",
8575 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
8578 goto meta_command_exit
;
8581 /* convert filectrl text option to value. allow any unique prefix
8582 ** of the option name, or a numerical value. */
8583 n2
= strlen30(zCmd
);
8584 for(i
=0; i
<ArraySize(aCtrl
); i
++){
8585 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
8587 filectrl
= aCtrl
[i
].ctrlCode
;
8590 utf8_printf(stderr
, "Error: ambiguous file-control: \"%s\"\n"
8591 "Use \".filectrl --help\" for help\n", zCmd
);
8593 goto meta_command_exit
;
8598 utf8_printf(stderr
,"Error: unknown file-control: %s\n"
8599 "Use \".filectrl --help\" for help\n", zCmd
);
8602 case SQLITE_FCNTL_SIZE_LIMIT
: {
8603 if( nArg
!=2 && nArg
!=3 ) break;
8604 iRes
= nArg
==3 ? integerValue(azArg
[2]) : -1;
8605 sqlite3_file_control(p
->db
, zSchema
, SQLITE_FCNTL_SIZE_LIMIT
, &iRes
);
8609 case SQLITE_FCNTL_LOCK_TIMEOUT
:
8610 case SQLITE_FCNTL_CHUNK_SIZE
: {
8612 if( nArg
!=3 ) break;
8613 x
= (int)integerValue(azArg
[2]);
8614 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8618 case SQLITE_FCNTL_PERSIST_WAL
:
8619 case SQLITE_FCNTL_POWERSAFE_OVERWRITE
: {
8621 if( nArg
!=2 && nArg
!=3 ) break;
8622 x
= nArg
==3 ? booleanValue(azArg
[2]) : -1;
8623 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8628 case SQLITE_FCNTL_DATA_VERSION
:
8629 case SQLITE_FCNTL_HAS_MOVED
: {
8631 if( nArg
!=2 ) break;
8632 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8637 case SQLITE_FCNTL_TEMPFILENAME
: {
8639 if( nArg
!=2 ) break;
8640 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &z
);
8642 utf8_printf(p
->out
, "%s\n", z
);
8648 case SQLITE_FCNTL_RESERVE_BYTES
: {
8652 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8655 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8656 utf8_printf(p
->out
,"%d\n", x
);
8662 if( isOk
==0 && iCtrl
>=0 ){
8663 utf8_printf(p
->out
, "Usage: .filectrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
8665 }else if( isOk
==1 ){
8667 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%lld", iRes
);
8668 raw_printf(p
->out
, "%s\n", zBuf
);
8672 if( c
=='f' && cli_strncmp(azArg
[0], "fullschema", n
)==0 ){
8675 memcpy(&data
, p
, sizeof(data
));
8676 data
.showHeader
= 0;
8677 data
.cMode
= data
.mode
= MODE_Semi
;
8678 if( nArg
==2 && optionMatch(azArg
[1], "indent") ){
8679 data
.cMode
= data
.mode
= MODE_Pretty
;
8683 raw_printf(stderr
, "Usage: .fullschema ?--indent?\n");
8685 goto meta_command_exit
;
8688 rc
= sqlite3_exec(p
->db
,
8690 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8691 " FROM sqlite_schema UNION ALL"
8692 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8693 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8697 if( rc
==SQLITE_OK
){
8698 sqlite3_stmt
*pStmt
;
8699 rc
= sqlite3_prepare_v2(p
->db
,
8700 "SELECT rowid FROM sqlite_schema"
8701 " WHERE name GLOB 'sqlite_stat[134]'",
8703 doStats
= sqlite3_step(pStmt
)==SQLITE_ROW
;
8704 sqlite3_finalize(pStmt
);
8707 raw_printf(p
->out
, "/* No STAT tables available */\n");
8709 raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
8710 data
.cMode
= data
.mode
= MODE_Insert
;
8711 data
.zDestTable
= "sqlite_stat1";
8712 shell_exec(&data
, "SELECT * FROM sqlite_stat1", 0);
8713 data
.zDestTable
= "sqlite_stat4";
8714 shell_exec(&data
, "SELECT * FROM sqlite_stat4", 0);
8715 raw_printf(p
->out
, "ANALYZE sqlite_schema;\n");
8719 if( c
=='h' && cli_strncmp(azArg
[0], "headers", n
)==0 ){
8721 p
->showHeader
= booleanValue(azArg
[1]);
8722 p
->shellFlgs
|= SHFLG_HeaderSet
;
8724 raw_printf(stderr
, "Usage: .headers on|off\n");
8729 if( c
=='h' && cli_strncmp(azArg
[0], "help", n
)==0 ){
8731 n
= showHelp(p
->out
, azArg
[1]);
8733 utf8_printf(p
->out
, "Nothing matches '%s'\n", azArg
[1]);
8736 showHelp(p
->out
, 0);
8740 #ifndef SQLITE_SHELL_FIDDLE
8741 if( c
=='i' && cli_strncmp(azArg
[0], "import", n
)==0 ){
8742 char *zTable
= 0; /* Insert data into this table */
8743 char *zSchema
= 0; /* within this schema (may default to "main") */
8744 char *zFile
= 0; /* Name of file to extra content from */
8745 sqlite3_stmt
*pStmt
= NULL
; /* A statement */
8746 int nCol
; /* Number of columns in the table */
8747 int nByte
; /* Number of bytes in an SQL string */
8748 int i
, j
; /* Loop counters */
8749 int needCommit
; /* True to COMMIT or ROLLBACK at end */
8750 int nSep
; /* Number of bytes in p->colSeparator[] */
8751 char *zSql
; /* An SQL statement */
8752 char *zFullTabName
; /* Table name with schema if applicable */
8753 ImportCtx sCtx
; /* Reader context */
8754 char *(SQLITE_CDECL
*xRead
)(ImportCtx
*); /* Func to read one value */
8755 int eVerbose
= 0; /* Larger for more console output */
8756 int nSkip
= 0; /* Initial lines to skip */
8757 int useOutputMode
= 1; /* Use output mode to determine separators */
8758 char *zCreate
= 0; /* CREATE TABLE statement text */
8760 failIfSafeMode(p
, "cannot run .import in safe mode");
8761 memset(&sCtx
, 0, sizeof(sCtx
));
8762 if( p
->mode
==MODE_Ascii
){
8763 xRead
= ascii_read_one_field
;
8765 xRead
= csv_read_one_field
;
8768 for(i
=1; i
<nArg
; i
++){
8770 if( z
[0]=='-' && z
[1]=='-' ) z
++;
8774 }else if( zTable
==0 ){
8777 utf8_printf(p
->out
, "ERROR: extra argument: \"%s\". Usage:\n", z
);
8778 showHelp(p
->out
, "import");
8779 goto meta_command_exit
;
8781 }else if( cli_strcmp(z
,"-v")==0 ){
8783 }else if( cli_strcmp(z
,"-schema")==0 && i
<nArg
-1 ){
8784 zSchema
= azArg
[++i
];
8785 }else if( cli_strcmp(z
,"-skip")==0 && i
<nArg
-1 ){
8786 nSkip
= integerValue(azArg
[++i
]);
8787 }else if( cli_strcmp(z
,"-ascii")==0 ){
8788 sCtx
.cColSep
= SEP_Unit
[0];
8789 sCtx
.cRowSep
= SEP_Record
[0];
8790 xRead
= ascii_read_one_field
;
8792 }else if( cli_strcmp(z
,"-csv")==0 ){
8794 sCtx
.cRowSep
= '\n';
8795 xRead
= csv_read_one_field
;
8798 utf8_printf(p
->out
, "ERROR: unknown option: \"%s\". Usage:\n", z
);
8799 showHelp(p
->out
, "import");
8800 goto meta_command_exit
;
8804 utf8_printf(p
->out
, "ERROR: missing %s argument. Usage:\n",
8805 zFile
==0 ? "FILE" : "TABLE");
8806 showHelp(p
->out
, "import");
8807 goto meta_command_exit
;
8811 if( useOutputMode
){
8812 /* If neither the --csv or --ascii options are specified, then set
8813 ** the column and row separator characters from the output mode. */
8814 nSep
= strlen30(p
->colSeparator
);
8817 "Error: non-null column separator required for import\n");
8818 goto meta_command_exit
;
8822 "Error: multi-character column separators not allowed"
8824 goto meta_command_exit
;
8826 nSep
= strlen30(p
->rowSeparator
);
8829 "Error: non-null row separator required for import\n");
8830 goto meta_command_exit
;
8832 if( nSep
==2 && p
->mode
==MODE_Csv
8833 && cli_strcmp(p
->rowSeparator
,SEP_CrLf
)==0
8835 /* When importing CSV (only), if the row separator is set to the
8836 ** default output row separator, change it to the default input
8837 ** row separator. This avoids having to maintain different input
8838 ** and output row separators. */
8839 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
8840 nSep
= strlen30(p
->rowSeparator
);
8843 raw_printf(stderr
, "Error: multi-character row separators not allowed"
8845 goto meta_command_exit
;
8847 sCtx
.cColSep
= (u8
)p
->colSeparator
[0];
8848 sCtx
.cRowSep
= (u8
)p
->rowSeparator
[0];
8852 if( sCtx
.zFile
[0]=='|' ){
8853 #ifdef SQLITE_OMIT_POPEN
8854 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
8855 goto meta_command_exit
;
8857 sCtx
.in
= popen(sCtx
.zFile
+1, "r");
8858 sCtx
.zFile
= "<pipe>";
8859 sCtx
.xCloser
= pclose
;
8862 sCtx
.in
= fopen(sCtx
.zFile
, "rb");
8863 sCtx
.xCloser
= fclose
;
8866 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
8867 goto meta_command_exit
;
8869 if( eVerbose
>=2 || (eVerbose
>=1 && useOutputMode
) ){
8872 zSep
[0] = sCtx
.cColSep
;
8873 utf8_printf(p
->out
, "Column separator ");
8874 output_c_string(p
->out
, zSep
);
8875 utf8_printf(p
->out
, ", row separator ");
8876 zSep
[0] = sCtx
.cRowSep
;
8877 output_c_string(p
->out
, zSep
);
8878 utf8_printf(p
->out
, "\n");
8880 sCtx
.z
= sqlite3_malloc64(120);
8882 import_cleanup(&sCtx
);
8883 shell_out_of_memory();
8885 /* Below, resources must be freed before exit. */
8886 while( (nSkip
--)>0 ){
8887 while( xRead(&sCtx
) && sCtx
.cTerm
==sCtx
.cColSep
){}
8890 zFullTabName
= sqlite3_mprintf("\"%w\".\"%w\"", zSchema
, zTable
);
8892 zFullTabName
= sqlite3_mprintf("\"%w\"", zTable
);
8894 zSql
= sqlite3_mprintf("SELECT * FROM %s", zFullTabName
);
8895 if( zSql
==0 || zFullTabName
==0 ){
8896 import_cleanup(&sCtx
);
8897 shell_out_of_memory();
8899 nByte
= strlen30(zSql
);
8900 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
8901 import_append_char(&sCtx
, 0); /* To ensure sCtx.z is allocated */
8902 if( rc
&& sqlite3_strglob("no such table: *", sqlite3_errmsg(p
->db
))==0 ){
8903 sqlite3
*dbCols
= 0;
8906 zCreate
= sqlite3_mprintf("CREATE TABLE %s", zFullTabName
);
8907 while( xRead(&sCtx
) ){
8908 zAutoColumn(sCtx
.z
, &dbCols
, 0);
8909 if( sCtx
.cTerm
!=sCtx
.cColSep
) break;
8911 zColDefs
= zAutoColumn(0, &dbCols
, &zRenames
);
8913 utf8_printf((stdin_is_interactive
&& p
->in
==stdin
)? p
->out
: stderr
,
8914 "Columns renamed during .import %s due to duplicates:\n"
8915 "%s\n", sCtx
.zFile
, zRenames
);
8916 sqlite3_free(zRenames
);
8920 utf8_printf(stderr
,"%s: empty file\n", sCtx
.zFile
);
8922 sqlite3_free(zCreate
);
8924 sqlite3_free(zFullTabName
);
8925 import_cleanup(&sCtx
);
8927 goto meta_command_exit
;
8929 zCreate
= sqlite3_mprintf("%z%z\n", zCreate
, zColDefs
);
8931 utf8_printf(p
->out
, "%s\n", zCreate
);
8933 rc
= sqlite3_exec(p
->db
, zCreate
, 0, 0, 0);
8935 utf8_printf(stderr
, "%s failed:\n%s\n", zCreate
, sqlite3_errmsg(p
->db
));
8938 sqlite3_free(zCreate
);
8940 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
8943 if (pStmt
) sqlite3_finalize(pStmt
);
8944 utf8_printf(stderr
,"Error: %s\n", sqlite3_errmsg(p
->db
));
8948 nCol
= sqlite3_column_count(pStmt
);
8949 sqlite3_finalize(pStmt
);
8951 if( nCol
==0 ) return 0; /* no columns, no error */
8952 zSql
= sqlite3_malloc64( nByte
*2 + 20 + nCol
*2 );
8954 import_cleanup(&sCtx
);
8955 shell_out_of_memory();
8957 sqlite3_snprintf(nByte
+20, zSql
, "INSERT INTO %s VALUES(?", zFullTabName
);
8959 for(i
=1; i
<nCol
; i
++){
8966 utf8_printf(p
->out
, "Insert using: %s\n", zSql
);
8968 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
8970 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
8971 if (pStmt
) sqlite3_finalize(pStmt
);
8975 sqlite3_free(zFullTabName
);
8976 needCommit
= sqlite3_get_autocommit(p
->db
);
8977 if( needCommit
) sqlite3_exec(p
->db
, "BEGIN", 0, 0, 0);
8979 int startLine
= sCtx
.nLine
;
8980 for(i
=0; i
<nCol
; i
++){
8981 char *z
= xRead(&sCtx
);
8983 ** Did we reach end-of-file before finding any columns?
8984 ** If so, stop instead of NULL filling the remaining columns.
8986 if( z
==0 && i
==0 ) break;
8988 ** Did we reach end-of-file OR end-of-line before finding any
8989 ** columns in ASCII mode? If so, stop instead of NULL filling
8990 ** the remaining columns.
8992 if( p
->mode
==MODE_Ascii
&& (z
==0 || z
[0]==0) && i
==0 ) break;
8993 sqlite3_bind_text(pStmt
, i
+1, z
, -1, SQLITE_TRANSIENT
);
8994 if( i
<nCol
-1 && sCtx
.cTerm
!=sCtx
.cColSep
){
8995 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
8996 "filling the rest with NULL\n",
8997 sCtx
.zFile
, startLine
, nCol
, i
+1);
8999 while( i
<=nCol
){ sqlite3_bind_null(pStmt
, i
); i
++; }
9002 if( sCtx
.cTerm
==sCtx
.cColSep
){
9006 }while( sCtx
.cTerm
==sCtx
.cColSep
);
9007 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
9009 sCtx
.zFile
, startLine
, nCol
, i
);
9012 sqlite3_step(pStmt
);
9013 rc
= sqlite3_reset(pStmt
);
9014 if( rc
!=SQLITE_OK
){
9015 utf8_printf(stderr
, "%s:%d: INSERT failed: %s\n", sCtx
.zFile
,
9016 startLine
, sqlite3_errmsg(p
->db
));
9022 }while( sCtx
.cTerm
!=EOF
);
9024 import_cleanup(&sCtx
);
9025 sqlite3_finalize(pStmt
);
9026 if( needCommit
) sqlite3_exec(p
->db
, "COMMIT", 0, 0, 0);
9029 "Added %d rows with %d errors using %d lines of input\n",
9030 sCtx
.nRow
, sCtx
.nErr
, sCtx
.nLine
-1);
9033 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9035 #ifndef SQLITE_UNTESTABLE
9036 if( c
=='i' && cli_strncmp(azArg
[0], "imposter", n
)==0 ){
9039 sqlite3_stmt
*pStmt
;
9041 int isWO
= 0; /* True if making an imposter of a WITHOUT ROWID table */
9042 int lenPK
= 0; /* Length of the PRIMARY KEY string for isWO tables */
9044 if( !ShellHasFlag(p
,SHFLG_TestingMode
) ){
9045 utf8_printf(stderr
, ".%s unavailable without --unsafe-testing\n",
9048 goto meta_command_exit
;
9050 if( !(nArg
==3 || (nArg
==2 && sqlite3_stricmp(azArg
[1],"off")==0)) ){
9051 utf8_printf(stderr
, "Usage: .imposter INDEX IMPOSTER\n"
9052 " .imposter off\n");
9053 /* Also allowed, but not documented:
9055 ** .imposter TABLE IMPOSTER
9057 ** where TABLE is a WITHOUT ROWID table. In that case, the
9058 ** imposter is another WITHOUT ROWID table with the columns in
9059 ** storage order. */
9061 goto meta_command_exit
;
9065 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 1);
9066 goto meta_command_exit
;
9068 zSql
= sqlite3_mprintf(
9069 "SELECT rootpage, 0 FROM sqlite_schema"
9070 " WHERE name='%q' AND type='index'"
9072 "SELECT rootpage, 1 FROM sqlite_schema"
9073 " WHERE name='%q' AND type='table'"
9074 " AND sql LIKE '%%without%%rowid%%'",
9077 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9079 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
9080 tnum
= sqlite3_column_int(pStmt
, 0);
9081 isWO
= sqlite3_column_int(pStmt
, 1);
9083 sqlite3_finalize(pStmt
);
9084 zSql
= sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg
[1]);
9085 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9088 while( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9090 const char *zCol
= (const char*)sqlite3_column_text(pStmt
,2);
9093 if( sqlite3_column_int(pStmt
,1)==-1 ){
9096 sqlite3_snprintf(sizeof(zLabel
),zLabel
,"expr%d",i
);
9100 if( isWO
&& lenPK
==0 && sqlite3_column_int(pStmt
,5)==0 && zCollist
){
9101 lenPK
= (int)strlen(zCollist
);
9104 zCollist
= sqlite3_mprintf("\"%w\"", zCol
);
9106 zCollist
= sqlite3_mprintf("%z,\"%w\"", zCollist
, zCol
);
9109 sqlite3_finalize(pStmt
);
9110 if( i
==0 || tnum
==0 ){
9111 utf8_printf(stderr
, "no such index: \"%s\"\n", azArg
[1]);
9113 sqlite3_free(zCollist
);
9114 goto meta_command_exit
;
9116 if( lenPK
==0 ) lenPK
= 100000;
9117 zSql
= sqlite3_mprintf(
9118 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9119 azArg
[2], zCollist
, lenPK
, zCollist
);
9120 sqlite3_free(zCollist
);
9121 rc
= sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 1, tnum
);
9122 if( rc
==SQLITE_OK
){
9123 rc
= sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
9124 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 0);
9126 utf8_printf(stderr
, "Error in [%s]: %s\n", zSql
, sqlite3_errmsg(p
->db
));
9128 utf8_printf(stdout
, "%s;\n", zSql
);
9130 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
9131 azArg
[1], isWO
? "table" : "index"
9135 raw_printf(stderr
, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc
);
9140 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9142 #ifdef SQLITE_ENABLE_IOTRACE
9143 if( c
=='i' && cli_strncmp(azArg
[0], "iotrace", n
)==0 ){
9144 SQLITE_API
extern void (SQLITE_CDECL
*sqlite3IoTrace
)(const char*, ...);
9145 if( iotrace
&& iotrace
!=stdout
) fclose(iotrace
);
9149 }else if( cli_strcmp(azArg
[1], "-")==0 ){
9150 sqlite3IoTrace
= iotracePrintf
;
9153 iotrace
= fopen(azArg
[1], "w");
9155 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", azArg
[1]);
9159 sqlite3IoTrace
= iotracePrintf
;
9165 if( c
=='l' && n
>=5 && cli_strncmp(azArg
[0], "limits", n
)==0 ){
9166 static const struct {
9167 const char *zLimitName
; /* Name of a limit */
9168 int limitCode
; /* Integer code for that limit */
9170 { "length", SQLITE_LIMIT_LENGTH
},
9171 { "sql_length", SQLITE_LIMIT_SQL_LENGTH
},
9172 { "column", SQLITE_LIMIT_COLUMN
},
9173 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH
},
9174 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT
},
9175 { "vdbe_op", SQLITE_LIMIT_VDBE_OP
},
9176 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG
},
9177 { "attached", SQLITE_LIMIT_ATTACHED
},
9178 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH
},
9179 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER
},
9180 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH
},
9181 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS
},
9186 for(i
=0; i
<ArraySize(aLimit
); i
++){
9187 printf("%20s %d\n", aLimit
[i
].zLimitName
,
9188 sqlite3_limit(p
->db
, aLimit
[i
].limitCode
, -1));
9191 raw_printf(stderr
, "Usage: .limit NAME ?NEW-VALUE?\n");
9193 goto meta_command_exit
;
9196 n2
= strlen30(azArg
[1]);
9197 for(i
=0; i
<ArraySize(aLimit
); i
++){
9198 if( sqlite3_strnicmp(aLimit
[i
].zLimitName
, azArg
[1], n2
)==0 ){
9202 utf8_printf(stderr
, "ambiguous limit: \"%s\"\n", azArg
[1]);
9204 goto meta_command_exit
;
9209 utf8_printf(stderr
, "unknown limit: \"%s\"\n"
9210 "enter \".limits\" with no arguments for a list.\n",
9213 goto meta_command_exit
;
9216 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
,
9217 (int)integerValue(azArg
[2]));
9219 printf("%20s %d\n", aLimit
[iLimit
].zLimitName
,
9220 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
, -1));
9224 if( c
=='l' && n
>2 && cli_strncmp(azArg
[0], "lint", n
)==0 ){
9226 lintDotCommand(p
, azArg
, nArg
);
9229 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9230 if( c
=='l' && cli_strncmp(azArg
[0], "load", n
)==0 ){
9231 const char *zFile
, *zProc
;
9233 failIfSafeMode(p
, "cannot run .load in safe mode");
9234 if( nArg
<2 || azArg
[1][0]==0 ){
9235 /* Must have a non-empty FILE. (Will not load self.) */
9236 raw_printf(stderr
, "Usage: .load FILE ?ENTRYPOINT?\n");
9238 goto meta_command_exit
;
9241 zProc
= nArg
>=3 ? azArg
[2] : 0;
9243 rc
= sqlite3_load_extension(p
->db
, zFile
, zProc
, &zErrMsg
);
9244 if( rc
!=SQLITE_OK
){
9245 utf8_printf(stderr
, "Error: %s\n", zErrMsg
);
9246 sqlite3_free(zErrMsg
);
9252 if( c
=='l' && cli_strncmp(azArg
[0], "log", n
)==0 ){
9254 raw_printf(stderr
, "Usage: .log FILENAME\n");
9257 const char *zFile
= azArg
[1];
9259 && cli_strcmp(zFile
,"on")!=0
9260 && cli_strcmp(zFile
,"off")!=0
9262 raw_printf(stdout
, "cannot set .log to anything other "
9263 "than \"on\" or \"off\"\n");
9266 output_file_close(p
->pLog
);
9267 if( cli_strcmp(zFile
,"on")==0 ) zFile
= "stdout";
9268 p
->pLog
= output_file_open(zFile
, 0);
9272 if( c
=='m' && cli_strncmp(azArg
[0], "mode", n
)==0 ){
9273 const char *zMode
= 0;
9274 const char *zTabname
= 0;
9276 ColModeOpts cmOpts
= ColModeOpts_default
;
9277 for(i
=1; i
<nArg
; i
++){
9278 const char *z
= azArg
[i
];
9279 if( optionMatch(z
,"wrap") && i
+1<nArg
){
9280 cmOpts
.iWrap
= integerValue(azArg
[++i
]);
9281 }else if( optionMatch(z
,"ww") ){
9282 cmOpts
.bWordWrap
= 1;
9283 }else if( optionMatch(z
,"wordwrap") && i
+1<nArg
){
9284 cmOpts
.bWordWrap
= (u8
)booleanValue(azArg
[++i
]);
9285 }else if( optionMatch(z
,"quote") ){
9287 }else if( optionMatch(z
,"noquote") ){
9289 }else if( zMode
==0 ){
9291 /* Apply defaults for qbox pseudo-mode. If that
9292 * overwrites already-set values, user was informed of this.
9294 if( cli_strcmp(z
, "qbox")==0 ){
9295 ColModeOpts cmo
= ColModeOpts_default_qbox
;
9299 }else if( zTabname
==0 ){
9301 }else if( z
[0]=='-' ){
9302 utf8_printf(stderr
, "unknown option: %s\n", z
);
9303 utf8_printf(stderr
, "options:\n"
9306 " --wordwrap on/off\n"
9310 goto meta_command_exit
;
9312 utf8_printf(stderr
, "extra argument: \"%s\"\n", z
);
9314 goto meta_command_exit
;
9318 if( p
->mode
==MODE_Column
9319 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
9323 "current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9324 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
9325 p
->cmOpts
.bWordWrap
? "on" : "off",
9326 p
->cmOpts
.bQuote
? "" : "no");
9328 raw_printf(p
->out
, "current output mode: %s\n", modeDescr
[p
->mode
]);
9330 zMode
= modeDescr
[p
->mode
];
9332 n2
= strlen30(zMode
);
9333 if( cli_strncmp(zMode
,"lines",n2
)==0 ){
9334 p
->mode
= MODE_Line
;
9335 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9336 }else if( cli_strncmp(zMode
,"columns",n2
)==0 ){
9337 p
->mode
= MODE_Column
;
9338 if( (p
->shellFlgs
& SHFLG_HeaderSet
)==0 ){
9341 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9343 }else if( cli_strncmp(zMode
,"list",n2
)==0 ){
9344 p
->mode
= MODE_List
;
9345 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Column
);
9346 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9347 }else if( cli_strncmp(zMode
,"html",n2
)==0 ){
9348 p
->mode
= MODE_Html
;
9349 }else if( cli_strncmp(zMode
,"tcl",n2
)==0 ){
9351 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Space
);
9352 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9353 }else if( cli_strncmp(zMode
,"csv",n2
)==0 ){
9355 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9356 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
9357 }else if( cli_strncmp(zMode
,"tabs",n2
)==0 ){
9358 p
->mode
= MODE_List
;
9359 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Tab
);
9360 }else if( cli_strncmp(zMode
,"insert",n2
)==0 ){
9361 p
->mode
= MODE_Insert
;
9362 set_table_name(p
, zTabname
? zTabname
: "table");
9363 }else if( cli_strncmp(zMode
,"quote",n2
)==0 ){
9364 p
->mode
= MODE_Quote
;
9365 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9366 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9367 }else if( cli_strncmp(zMode
,"ascii",n2
)==0 ){
9368 p
->mode
= MODE_Ascii
;
9369 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Unit
);
9370 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Record
);
9371 }else if( cli_strncmp(zMode
,"markdown",n2
)==0 ){
9372 p
->mode
= MODE_Markdown
;
9374 }else if( cli_strncmp(zMode
,"table",n2
)==0 ){
9375 p
->mode
= MODE_Table
;
9377 }else if( cli_strncmp(zMode
,"box",n2
)==0 ){
9380 }else if( cli_strncmp(zMode
,"count",n2
)==0 ){
9381 p
->mode
= MODE_Count
;
9382 }else if( cli_strncmp(zMode
,"off",n2
)==0 ){
9384 }else if( cli_strncmp(zMode
,"json",n2
)==0 ){
9385 p
->mode
= MODE_Json
;
9387 raw_printf(stderr
, "Error: mode should be one of: "
9388 "ascii box column csv html insert json line list markdown "
9389 "qbox quote table tabs tcl\n");
9395 #ifndef SQLITE_SHELL_FIDDLE
9396 if( c
=='n' && cli_strcmp(azArg
[0], "nonce")==0 ){
9398 raw_printf(stderr
, "Usage: .nonce NONCE\n");
9400 }else if( p
->zNonce
==0 || cli_strcmp(azArg
[1],p
->zNonce
)!=0 ){
9401 raw_printf(stderr
, "line %d: incorrect nonce: \"%s\"\n",
9402 p
->lineno
, azArg
[1]);
9406 return 0; /* Return immediately to bypass the safe mode reset
9407 ** at the end of this procedure */
9410 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9412 if( c
=='n' && cli_strncmp(azArg
[0], "nullvalue", n
)==0 ){
9414 sqlite3_snprintf(sizeof(p
->nullValue
), p
->nullValue
,
9415 "%.*s", (int)ArraySize(p
->nullValue
)-1, azArg
[1]);
9417 raw_printf(stderr
, "Usage: .nullvalue STRING\n");
9422 if( c
=='o' && cli_strncmp(azArg
[0], "open", n
)==0 && n
>=2 ){
9423 const char *zFN
= 0; /* Pointer to constant filename */
9424 char *zNewFilename
= 0; /* Name of the database file to open */
9425 int iName
= 1; /* Index in azArg[] of the filename */
9426 int newFlag
= 0; /* True to delete file before opening */
9427 int openMode
= SHELL_OPEN_UNSPEC
;
9429 /* Check for command-line arguments */
9430 for(iName
=1; iName
<nArg
; iName
++){
9431 const char *z
= azArg
[iName
];
9432 #ifndef SQLITE_SHELL_FIDDLE
9433 if( optionMatch(z
,"new") ){
9435 #ifdef SQLITE_HAVE_ZLIB
9436 }else if( optionMatch(z
, "zip") ){
9437 openMode
= SHELL_OPEN_ZIPFILE
;
9439 }else if( optionMatch(z
, "append") ){
9440 openMode
= SHELL_OPEN_APPENDVFS
;
9441 }else if( optionMatch(z
, "readonly") ){
9442 openMode
= SHELL_OPEN_READONLY
;
9443 }else if( optionMatch(z
, "nofollow") ){
9444 p
->openFlags
|= SQLITE_OPEN_NOFOLLOW
;
9445 #ifndef SQLITE_OMIT_DESERIALIZE
9446 }else if( optionMatch(z
, "deserialize") ){
9447 openMode
= SHELL_OPEN_DESERIALIZE
;
9448 }else if( optionMatch(z
, "hexdb") ){
9449 openMode
= SHELL_OPEN_HEXDB
;
9450 }else if( optionMatch(z
, "maxsize") && iName
+1<nArg
){
9451 p
->szMax
= integerValue(azArg
[++iName
]);
9452 #endif /* SQLITE_OMIT_DESERIALIZE */
9454 #endif /* !SQLITE_SHELL_FIDDLE */
9456 utf8_printf(stderr
, "unknown option: %s\n", z
);
9458 goto meta_command_exit
;
9460 utf8_printf(stderr
, "extra argument: \"%s\"\n", z
);
9462 goto meta_command_exit
;
9468 /* Close the existing database */
9469 session_close_all(p
, -1);
9472 p
->pAuxDb
->zDbFilename
= 0;
9473 sqlite3_free(p
->pAuxDb
->zFreeOnClose
);
9474 p
->pAuxDb
->zFreeOnClose
= 0;
9475 p
->openMode
= openMode
;
9479 /* If a filename is specified, try to open it first */
9480 if( zFN
|| p
->openMode
==SHELL_OPEN_HEXDB
){
9481 if( newFlag
&& zFN
&& !p
->bSafeMode
) shellDeleteFile(zFN
);
9482 #ifndef SQLITE_SHELL_FIDDLE
9484 && p
->openMode
!=SHELL_OPEN_HEXDB
9486 && cli_strcmp(zFN
,":memory:")!=0
9488 failIfSafeMode(p
, "cannot open disk-based database files in safe mode");
9491 /* WASM mode has its own sandboxed pseudo-filesystem. */
9494 zNewFilename
= sqlite3_mprintf("%s", zFN
);
9495 shell_check_oom(zNewFilename
);
9499 p
->pAuxDb
->zDbFilename
= zNewFilename
;
9500 open_db(p
, OPEN_DB_KEEPALIVE
);
9502 utf8_printf(stderr
, "Error: cannot open '%s'\n", zNewFilename
);
9503 sqlite3_free(zNewFilename
);
9505 p
->pAuxDb
->zFreeOnClose
= zNewFilename
;
9509 /* As a fall-back open a TEMP database */
9510 p
->pAuxDb
->zDbFilename
= 0;
9515 #ifndef SQLITE_SHELL_FIDDLE
9517 && (cli_strncmp(azArg
[0], "output", n
)==0
9518 || cli_strncmp(azArg
[0], "once", n
)==0))
9519 || (c
=='e' && n
==5 && cli_strcmp(azArg
[0],"excel")==0)
9525 int bOnce
= 0; /* 0: .output, 1: .once, 2: .excel */
9526 unsigned char zBOM
[4]; /* Byte-order mark to using if --bom is present */
9529 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
9533 }else if( cli_strncmp(azArg
[0],"once",n
)==0 ){
9536 for(i
=1; i
<nArg
; i
++){
9539 if( z
[1]=='-' ) z
++;
9540 if( cli_strcmp(z
,"-bom")==0 ){
9545 }else if( c
!='e' && cli_strcmp(z
,"-x")==0 ){
9546 eMode
= 'x'; /* spreadsheet */
9547 }else if( c
!='e' && cli_strcmp(z
,"-e")==0 ){
9548 eMode
= 'e'; /* text editor */
9550 utf8_printf(p
->out
, "ERROR: unknown option: \"%s\". Usage:\n",
9552 showHelp(p
->out
, azArg
[0]);
9554 goto meta_command_exit
;
9556 }else if( zFile
==0 && eMode
!='e' && eMode
!='x' ){
9557 zFile
= sqlite3_mprintf("%s", z
);
9558 if( zFile
&& zFile
[0]=='|' ){
9559 while( i
+1<nArg
) zFile
= sqlite3_mprintf("%z %s", zFile
, azArg
[++i
]);
9563 utf8_printf(p
->out
,"ERROR: extra parameter: \"%s\". Usage:\n",
9565 showHelp(p
->out
, azArg
[0]);
9567 sqlite3_free(zFile
);
9568 goto meta_command_exit
;
9572 zFile
= sqlite3_mprintf("stdout");
9580 #ifndef SQLITE_NOHAVE_SYSTEM
9581 if( eMode
=='e' || eMode
=='x' ){
9585 /* spreadsheet mode. Output as CSV. */
9586 newTempFile(p
, "csv");
9587 ShellClearFlag(p
, SHFLG_Echo
);
9589 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9590 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
9592 /* text editor mode */
9593 newTempFile(p
, "txt");
9596 sqlite3_free(zFile
);
9597 zFile
= sqlite3_mprintf("%s", p
->zTempFile
);
9599 #endif /* SQLITE_NOHAVE_SYSTEM */
9600 shell_check_oom(zFile
);
9601 if( zFile
[0]=='|' ){
9602 #ifdef SQLITE_OMIT_POPEN
9603 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
9607 p
->out
= popen(zFile
+ 1, "w");
9609 utf8_printf(stderr
,"Error: cannot open pipe \"%s\"\n", zFile
+ 1);
9613 if( zBOM
[0] ) fwrite(zBOM
, 1, 3, p
->out
);
9614 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
9618 p
->out
= output_file_open(zFile
, bTxtMode
);
9620 if( cli_strcmp(zFile
,"off")!=0 ){
9621 utf8_printf(stderr
,"Error: cannot write to \"%s\"\n", zFile
);
9626 if( zBOM
[0] ) fwrite(zBOM
, 1, 3, p
->out
);
9627 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
9630 sqlite3_free(zFile
);
9632 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9634 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "parameter", n
)==0 ){
9636 if( nArg
<=1 ) goto parameter_syntax_error
;
9639 ** Clear all bind parameters by dropping the TEMP table that holds them.
9641 if( nArg
==2 && cli_strcmp(azArg
[1],"clear")==0 ){
9642 sqlite3_exec(p
->db
, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9647 ** List all bind parameters.
9649 if( nArg
==2 && cli_strcmp(azArg
[1],"list")==0 ){
9650 sqlite3_stmt
*pStmt
= 0;
9653 rx
= sqlite3_prepare_v2(p
->db
,
9654 "SELECT max(length(key)) "
9655 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
9656 if( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9657 len
= sqlite3_column_int(pStmt
, 0);
9658 if( len
>40 ) len
= 40;
9660 sqlite3_finalize(pStmt
);
9663 rx
= sqlite3_prepare_v2(p
->db
,
9664 "SELECT key, quote(value) "
9665 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
9666 while( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9667 utf8_printf(p
->out
, "%-*s %s\n", len
, sqlite3_column_text(pStmt
,0),
9668 sqlite3_column_text(pStmt
,1));
9670 sqlite3_finalize(pStmt
);
9675 ** Make sure the TEMP table used to hold bind parameters exists.
9676 ** Create it if necessary.
9678 if( nArg
==2 && cli_strcmp(azArg
[1],"init")==0 ){
9682 /* .parameter set NAME VALUE
9683 ** Set or reset a bind parameter. NAME should be the full parameter
9684 ** name exactly as it appears in the query. (ex: $abc, @def). The
9685 ** VALUE can be in either SQL literal notation, or if not it will be
9686 ** understood to be a text string.
9688 if( nArg
==4 && cli_strcmp(azArg
[1],"set")==0 ){
9691 sqlite3_stmt
*pStmt
;
9692 const char *zKey
= azArg
[2];
9693 const char *zValue
= azArg
[3];
9695 zSql
= sqlite3_mprintf(
9696 "REPLACE INTO temp.sqlite_parameters(key,value)"
9697 "VALUES(%Q,%s);", zKey
, zValue
);
9698 shell_check_oom(zSql
);
9700 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9702 if( rx
!=SQLITE_OK
){
9703 sqlite3_finalize(pStmt
);
9705 zSql
= sqlite3_mprintf(
9706 "REPLACE INTO temp.sqlite_parameters(key,value)"
9707 "VALUES(%Q,%Q);", zKey
, zValue
);
9708 shell_check_oom(zSql
);
9709 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9711 if( rx
!=SQLITE_OK
){
9712 utf8_printf(p
->out
, "Error: %s\n", sqlite3_errmsg(p
->db
));
9713 sqlite3_finalize(pStmt
);
9718 sqlite3_step(pStmt
);
9719 sqlite3_finalize(pStmt
);
9722 /* .parameter unset NAME
9723 ** Remove the NAME binding from the parameter binding table, if it
9726 if( nArg
==3 && cli_strcmp(azArg
[1],"unset")==0 ){
9727 char *zSql
= sqlite3_mprintf(
9728 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg
[2]);
9729 shell_check_oom(zSql
);
9730 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
9733 /* If no command name matches, show a syntax error */
9734 parameter_syntax_error
:
9735 showHelp(p
->out
, "parameter");
9738 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "print", n
)==0 ){
9740 for(i
=1; i
<nArg
; i
++){
9741 if( i
>1 ) raw_printf(p
->out
, " ");
9742 utf8_printf(p
->out
, "%s", azArg
[i
]);
9744 raw_printf(p
->out
, "\n");
9747 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9748 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "progress", n
)==0 ){
9754 for(i
=1; i
<nArg
; i
++){
9755 const char *z
= azArg
[i
];
9758 if( z
[0]=='-' ) z
++;
9759 if( cli_strcmp(z
,"quiet")==0 || cli_strcmp(z
,"q")==0 ){
9760 p
->flgProgress
|= SHELL_PROGRESS_QUIET
;
9763 if( cli_strcmp(z
,"reset")==0 ){
9764 p
->flgProgress
|= SHELL_PROGRESS_RESET
;
9767 if( cli_strcmp(z
,"once")==0 ){
9768 p
->flgProgress
|= SHELL_PROGRESS_ONCE
;
9771 if( cli_strcmp(z
,"limit")==0 ){
9773 utf8_printf(stderr
, "Error: missing argument on --limit\n");
9775 goto meta_command_exit
;
9777 p
->mxProgress
= (int)integerValue(azArg
[++i
]);
9781 utf8_printf(stderr
, "Error: unknown option: \"%s\"\n", azArg
[i
]);
9783 goto meta_command_exit
;
9785 nn
= (int)integerValue(z
);
9789 sqlite3_progress_handler(p
->db
, nn
, progress_handler
, p
);
9791 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9793 if( c
=='p' && cli_strncmp(azArg
[0], "prompt", n
)==0 ){
9795 shell_strncpy(mainPrompt
,azArg
[1],(int)ArraySize(mainPrompt
)-1);
9798 shell_strncpy(continuePrompt
,azArg
[2],(int)ArraySize(continuePrompt
)-1);
9802 #ifndef SQLITE_SHELL_FIDDLE
9803 if( c
=='q' && cli_strncmp(azArg
[0], "quit", n
)==0 ){
9808 #ifndef SQLITE_SHELL_FIDDLE
9809 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "read", n
)==0 ){
9810 FILE *inSaved
= p
->in
;
9811 int savedLineno
= p
->lineno
;
9812 failIfSafeMode(p
, "cannot run .read in safe mode");
9814 raw_printf(stderr
, "Usage: .read FILE\n");
9816 goto meta_command_exit
;
9818 if( azArg
[1][0]=='|' ){
9819 #ifdef SQLITE_OMIT_POPEN
9820 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
9824 p
->in
= popen(azArg
[1]+1, "r");
9826 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", azArg
[1]);
9829 rc
= process_input(p
);
9833 }else if( (p
->in
= openChrSource(azArg
[1]))==0 ){
9834 utf8_printf(stderr
,"Error: cannot open \"%s\"\n", azArg
[1]);
9837 rc
= process_input(p
);
9841 p
->lineno
= savedLineno
;
9843 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9845 #ifndef SQLITE_SHELL_FIDDLE
9846 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "restore", n
)==0 ){
9847 const char *zSrcFile
;
9850 sqlite3_backup
*pBackup
;
9853 failIfSafeMode(p
, "cannot run .restore in safe mode");
9855 zSrcFile
= azArg
[1];
9857 }else if( nArg
==3 ){
9858 zSrcFile
= azArg
[2];
9861 raw_printf(stderr
, "Usage: .restore ?DB? FILE\n");
9863 goto meta_command_exit
;
9865 rc
= sqlite3_open(zSrcFile
, &pSrc
);
9866 if( rc
!=SQLITE_OK
){
9867 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zSrcFile
);
9872 pBackup
= sqlite3_backup_init(p
->db
, zDb
, pSrc
, "main");
9874 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
9878 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
9879 || rc
==SQLITE_BUSY
){
9880 if( rc
==SQLITE_BUSY
){
9881 if( nTimeout
++ >= 3 ) break;
9885 sqlite3_backup_finish(pBackup
);
9886 if( rc
==SQLITE_DONE
){
9888 }else if( rc
==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
9889 raw_printf(stderr
, "Error: source database is busy\n");
9892 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
9897 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9899 if( c
=='s' && cli_strncmp(azArg
[0], "scanstats", n
)==0 ){
9901 if( cli_strcmp(azArg
[1], "est")==0 ){
9904 p
->scanstatsOn
= (u8
)booleanValue(azArg
[1]);
9908 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
9910 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9911 raw_printf(stderr
, "Warning: .scanstats not available in this build.\n");
9914 raw_printf(stderr
, "Usage: .scanstats on|off|est\n");
9919 if( c
=='s' && cli_strncmp(azArg
[0], "schema", n
)==0 ){
9923 const char *zDiv
= "(";
9924 const char *zName
= 0;
9927 int bNoSystemTabs
= 0;
9931 memcpy(&data
, p
, sizeof(data
));
9932 data
.showHeader
= 0;
9933 data
.cMode
= data
.mode
= MODE_Semi
;
9935 for(ii
=1; ii
<nArg
; ii
++){
9936 if( optionMatch(azArg
[ii
],"indent") ){
9937 data
.cMode
= data
.mode
= MODE_Pretty
;
9938 }else if( optionMatch(azArg
[ii
],"debug") ){
9940 }else if( optionMatch(azArg
[ii
],"nosys") ){
9942 }else if( azArg
[ii
][0]=='-' ){
9943 utf8_printf(stderr
, "Unknown option: \"%s\"\n", azArg
[ii
]);
9945 goto meta_command_exit
;
9946 }else if( zName
==0 ){
9950 "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
9952 goto meta_command_exit
;
9956 int isSchema
= sqlite3_strlike(zName
, "sqlite_master", '\\')==0
9957 || sqlite3_strlike(zName
, "sqlite_schema", '\\')==0
9958 || sqlite3_strlike(zName
,"sqlite_temp_master", '\\')==0
9959 || sqlite3_strlike(zName
,"sqlite_temp_schema", '\\')==0;
9961 char *new_argv
[2], *new_colv
[2];
9962 new_argv
[0] = sqlite3_mprintf(
9963 "CREATE TABLE %s (\n"
9967 " rootpage integer,\n"
9970 shell_check_oom(new_argv
[0]);
9972 new_colv
[0] = "sql";
9974 callback(&data
, 1, new_argv
, new_colv
);
9975 sqlite3_free(new_argv
[0]);
9979 sqlite3_stmt
*pStmt
= 0;
9980 rc
= sqlite3_prepare_v2(p
->db
, "SELECT name FROM pragma_database_list",
9983 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
9984 sqlite3_finalize(pStmt
);
9986 goto meta_command_exit
;
9988 appendText(&sSelect
, "SELECT sql FROM", 0);
9990 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
9991 const char *zDb
= (const char*)sqlite3_column_text(pStmt
, 0);
9993 sqlite3_snprintf(sizeof(zScNum
), zScNum
, "%d", ++iSchema
);
9994 appendText(&sSelect
, zDiv
, 0);
9995 zDiv
= " UNION ALL ";
9996 appendText(&sSelect
, "SELECT shell_add_schema(sql,", 0);
9997 if( sqlite3_stricmp(zDb
, "main")!=0 ){
9998 appendText(&sSelect
, zDb
, '\'');
10000 appendText(&sSelect
, "NULL", 0);
10002 appendText(&sSelect
, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10003 appendText(&sSelect
, zScNum
, 0);
10004 appendText(&sSelect
, " AS snum, ", 0);
10005 appendText(&sSelect
, zDb
, '\'');
10006 appendText(&sSelect
, " AS sname FROM ", 0);
10007 appendText(&sSelect
, zDb
, quoteChar(zDb
));
10008 appendText(&sSelect
, ".sqlite_schema", 0);
10010 sqlite3_finalize(pStmt
);
10011 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10013 appendText(&sSelect
,
10014 " UNION ALL SELECT shell_module_schema(name),"
10015 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10019 appendText(&sSelect
, ") WHERE ", 0);
10021 char *zQarg
= sqlite3_mprintf("%Q", zName
);
10023 shell_check_oom(zQarg
);
10024 bGlob
= strchr(zName
, '*') != 0 || strchr(zName
, '?') != 0 ||
10025 strchr(zName
, '[') != 0;
10026 if( strchr(zName
, '.') ){
10027 appendText(&sSelect
, "lower(printf('%s.%s',sname,tbl_name))", 0);
10029 appendText(&sSelect
, "lower(tbl_name)", 0);
10031 appendText(&sSelect
, bGlob
? " GLOB " : " LIKE ", 0);
10032 appendText(&sSelect
, zQarg
, 0);
10034 appendText(&sSelect
, " ESCAPE '\\' ", 0);
10036 appendText(&sSelect
, " AND ", 0);
10037 sqlite3_free(zQarg
);
10039 if( bNoSystemTabs
){
10040 appendText(&sSelect
, "name NOT LIKE 'sqlite_%%' AND ", 0);
10042 appendText(&sSelect
, "sql IS NOT NULL"
10043 " ORDER BY snum, rowid", 0);
10045 utf8_printf(p
->out
, "SQL: %s;\n", sSelect
.z
);
10047 rc
= sqlite3_exec(p
->db
, sSelect
.z
, callback
, &data
, &zErrMsg
);
10049 freeText(&sSelect
);
10052 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
10053 sqlite3_free(zErrMsg
);
10055 }else if( rc
!= SQLITE_OK
){
10056 raw_printf(stderr
,"Error: querying schema information\n");
10063 if( (c
=='s' && n
==11 && cli_strncmp(azArg
[0], "selecttrace", n
)==0)
10064 || (c
=='t' && n
==9 && cli_strncmp(azArg
[0], "treetrace", n
)==0)
10066 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
10067 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &x
);
10070 #if defined(SQLITE_ENABLE_SESSION)
10071 if( c
=='s' && cli_strncmp(azArg
[0],"session",n
)==0 && n
>=3 ){
10072 struct AuxDb
*pAuxDb
= p
->pAuxDb
;
10073 OpenSession
*pSession
= &pAuxDb
->aSession
[0];
10074 char **azCmd
= &azArg
[1];
10076 int nCmd
= nArg
- 1;
10078 if( nArg
<=1 ) goto session_syntax_error
;
10081 for(iSes
=0; iSes
<pAuxDb
->nSession
; iSes
++){
10082 if( cli_strcmp(pAuxDb
->aSession
[iSes
].zName
, azArg
[1])==0 ) break;
10084 if( iSes
<pAuxDb
->nSession
){
10085 pSession
= &pAuxDb
->aSession
[iSes
];
10089 pSession
= &pAuxDb
->aSession
[0];
10094 /* .session attach TABLE
10095 ** Invoke the sqlite3session_attach() interface to attach a particular
10096 ** table so that it is never filtered.
10098 if( cli_strcmp(azCmd
[0],"attach")==0 ){
10099 if( nCmd
!=2 ) goto session_syntax_error
;
10100 if( pSession
->p
==0 ){
10102 raw_printf(stderr
, "ERROR: No sessions are open\n");
10104 rc
= sqlite3session_attach(pSession
->p
, azCmd
[1]);
10106 raw_printf(stderr
, "ERROR: sqlite3session_attach() returns %d\n", rc
);
10112 /* .session changeset FILE
10113 ** .session patchset FILE
10114 ** Write a changeset or patchset into a file. The file is overwritten.
10116 if( cli_strcmp(azCmd
[0],"changeset")==0
10117 || cli_strcmp(azCmd
[0],"patchset")==0
10120 failIfSafeMode(p
, "cannot run \".session %s\" in safe mode", azCmd
[0]);
10121 if( nCmd
!=2 ) goto session_syntax_error
;
10122 if( pSession
->p
==0 ) goto session_not_open
;
10123 out
= fopen(azCmd
[1], "wb");
10125 utf8_printf(stderr
, "ERROR: cannot open \"%s\" for writing\n",
10130 if( azCmd
[0][0]=='c' ){
10131 rc
= sqlite3session_changeset(pSession
->p
, &szChng
, &pChng
);
10133 rc
= sqlite3session_patchset(pSession
->p
, &szChng
, &pChng
);
10136 printf("Error: error code %d\n", rc
);
10140 && fwrite(pChng
, szChng
, 1, out
)!=1 ){
10141 raw_printf(stderr
, "ERROR: Failed to write entire %d-byte output\n",
10144 sqlite3_free(pChng
);
10150 ** Close the identified session
10152 if( cli_strcmp(azCmd
[0], "close")==0 ){
10153 if( nCmd
!=1 ) goto session_syntax_error
;
10154 if( pAuxDb
->nSession
){
10155 session_close(pSession
);
10156 pAuxDb
->aSession
[iSes
] = pAuxDb
->aSession
[--pAuxDb
->nSession
];
10160 /* .session enable ?BOOLEAN?
10161 ** Query or set the enable flag
10163 if( cli_strcmp(azCmd
[0], "enable")==0 ){
10165 if( nCmd
>2 ) goto session_syntax_error
;
10166 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
10167 if( pAuxDb
->nSession
){
10168 ii
= sqlite3session_enable(pSession
->p
, ii
);
10169 utf8_printf(p
->out
, "session %s enable flag = %d\n",
10170 pSession
->zName
, ii
);
10174 /* .session filter GLOB ....
10175 ** Set a list of GLOB patterns of table names to be excluded.
10177 if( cli_strcmp(azCmd
[0], "filter")==0 ){
10179 if( nCmd
<2 ) goto session_syntax_error
;
10180 if( pAuxDb
->nSession
){
10181 for(ii
=0; ii
<pSession
->nFilter
; ii
++){
10182 sqlite3_free(pSession
->azFilter
[ii
]);
10184 sqlite3_free(pSession
->azFilter
);
10185 nByte
= sizeof(pSession
->azFilter
[0])*(nCmd
-1);
10186 pSession
->azFilter
= sqlite3_malloc( nByte
);
10187 if( pSession
->azFilter
==0 ){
10188 raw_printf(stderr
, "Error: out or memory\n");
10191 for(ii
=1; ii
<nCmd
; ii
++){
10192 char *x
= pSession
->azFilter
[ii
-1] = sqlite3_mprintf("%s", azCmd
[ii
]);
10193 shell_check_oom(x
);
10195 pSession
->nFilter
= ii
-1;
10199 /* .session indirect ?BOOLEAN?
10200 ** Query or set the indirect flag
10202 if( cli_strcmp(azCmd
[0], "indirect")==0 ){
10204 if( nCmd
>2 ) goto session_syntax_error
;
10205 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
10206 if( pAuxDb
->nSession
){
10207 ii
= sqlite3session_indirect(pSession
->p
, ii
);
10208 utf8_printf(p
->out
, "session %s indirect flag = %d\n",
10209 pSession
->zName
, ii
);
10213 /* .session isempty
10214 ** Determine if the session is empty
10216 if( cli_strcmp(azCmd
[0], "isempty")==0 ){
10218 if( nCmd
!=1 ) goto session_syntax_error
;
10219 if( pAuxDb
->nSession
){
10220 ii
= sqlite3session_isempty(pSession
->p
);
10221 utf8_printf(p
->out
, "session %s isempty flag = %d\n",
10222 pSession
->zName
, ii
);
10227 ** List all currently open sessions
10229 if( cli_strcmp(azCmd
[0],"list")==0 ){
10230 for(i
=0; i
<pAuxDb
->nSession
; i
++){
10231 utf8_printf(p
->out
, "%d %s\n", i
, pAuxDb
->aSession
[i
].zName
);
10235 /* .session open DB NAME
10236 ** Open a new session called NAME on the attached database DB.
10237 ** DB is normally "main".
10239 if( cli_strcmp(azCmd
[0],"open")==0 ){
10241 if( nCmd
!=3 ) goto session_syntax_error
;
10243 if( zName
[0]==0 ) goto session_syntax_error
;
10244 for(i
=0; i
<pAuxDb
->nSession
; i
++){
10245 if( cli_strcmp(pAuxDb
->aSession
[i
].zName
,zName
)==0 ){
10246 utf8_printf(stderr
, "Session \"%s\" already exists\n", zName
);
10247 goto meta_command_exit
;
10250 if( pAuxDb
->nSession
>=ArraySize(pAuxDb
->aSession
) ){
10252 "Maximum of %d sessions\n", ArraySize(pAuxDb
->aSession
));
10253 goto meta_command_exit
;
10255 pSession
= &pAuxDb
->aSession
[pAuxDb
->nSession
];
10256 rc
= sqlite3session_create(p
->db
, azCmd
[1], &pSession
->p
);
10258 raw_printf(stderr
, "Cannot open session: error code=%d\n", rc
);
10260 goto meta_command_exit
;
10262 pSession
->nFilter
= 0;
10263 sqlite3session_table_filter(pSession
->p
, session_filter
, pSession
);
10264 pAuxDb
->nSession
++;
10265 pSession
->zName
= sqlite3_mprintf("%s", zName
);
10266 shell_check_oom(pSession
->zName
);
10268 /* If no command name matches, show a syntax error */
10269 session_syntax_error
:
10270 showHelp(p
->out
, "session");
10274 #ifdef SQLITE_DEBUG
10275 /* Undocumented commands for internal testing. Subject to change
10276 ** without notice. */
10277 if( c
=='s' && n
>=10 && cli_strncmp(azArg
[0], "selftest-", 9)==0 ){
10278 if( cli_strncmp(azArg
[0]+9, "boolean", n
-9)==0 ){
10280 for(i
=1; i
<nArg
; i
++){
10281 v
= booleanValue(azArg
[i
]);
10282 utf8_printf(p
->out
, "%s: %d 0x%x\n", azArg
[i
], v
, v
);
10285 if( cli_strncmp(azArg
[0]+9, "integer", n
-9)==0 ){
10286 int i
; sqlite3_int64 v
;
10287 for(i
=1; i
<nArg
; i
++){
10289 v
= integerValue(azArg
[i
]);
10290 sqlite3_snprintf(sizeof(zBuf
),zBuf
,"%s: %lld 0x%llx\n", azArg
[i
],v
,v
);
10291 utf8_printf(p
->out
, "%s", zBuf
);
10297 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"selftest",n
)==0 ){
10298 int bIsInit
= 0; /* True to initialize the SELFTEST table */
10299 int bVerbose
= 0; /* Verbose output */
10300 int bSelftestExists
; /* True if SELFTEST already exists */
10301 int i
, k
; /* Loop counters */
10302 int nTest
= 0; /* Number of tests runs */
10303 int nErr
= 0; /* Number of errors seen */
10304 ShellText str
; /* Answer for a query */
10305 sqlite3_stmt
*pStmt
= 0; /* Query against the SELFTEST table */
10308 for(i
=1; i
<nArg
; i
++){
10309 const char *z
= azArg
[i
];
10310 if( z
[0]=='-' && z
[1]=='-' ) z
++;
10311 if( cli_strcmp(z
,"-init")==0 ){
10314 if( cli_strcmp(z
,"-v")==0 ){
10318 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
10319 azArg
[i
], azArg
[0]);
10320 raw_printf(stderr
, "Should be one of: --init -v\n");
10322 goto meta_command_exit
;
10325 if( sqlite3_table_column_metadata(p
->db
,"main","selftest",0,0,0,0,0,0)
10327 bSelftestExists
= 0;
10329 bSelftestExists
= 1;
10332 createSelftestTable(p
);
10333 bSelftestExists
= 1;
10336 appendText(&str
, "x", 0);
10337 for(k
=bSelftestExists
; k
>=0; k
--){
10339 rc
= sqlite3_prepare_v2(p
->db
,
10340 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10343 rc
= sqlite3_prepare_v2(p
->db
,
10344 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10345 " (1,'run','PRAGMA integrity_check','ok')",
10349 raw_printf(stderr
, "Error querying the selftest table\n");
10351 sqlite3_finalize(pStmt
);
10352 goto meta_command_exit
;
10354 for(i
=1; sqlite3_step(pStmt
)==SQLITE_ROW
; i
++){
10355 int tno
= sqlite3_column_int(pStmt
, 0);
10356 const char *zOp
= (const char*)sqlite3_column_text(pStmt
, 1);
10357 const char *zSql
= (const char*)sqlite3_column_text(pStmt
, 2);
10358 const char *zAns
= (const char*)sqlite3_column_text(pStmt
, 3);
10360 if( zOp
==0 ) continue;
10361 if( zSql
==0 ) continue;
10362 if( zAns
==0 ) continue;
10365 printf("%d: %s %s\n", tno
, zOp
, zSql
);
10367 if( cli_strcmp(zOp
,"memo")==0 ){
10368 utf8_printf(p
->out
, "%s\n", zSql
);
10370 if( cli_strcmp(zOp
,"run")==0 ){
10374 rc
= sqlite3_exec(p
->db
, zSql
, captureOutputCallback
, &str
, &zErrMsg
);
10377 utf8_printf(p
->out
, "Result: %s\n", str
.z
);
10379 if( rc
|| zErrMsg
){
10382 utf8_printf(p
->out
, "%d: error-code-%d: %s\n", tno
, rc
, zErrMsg
);
10383 sqlite3_free(zErrMsg
);
10384 }else if( cli_strcmp(zAns
,str
.z
)!=0 ){
10387 utf8_printf(p
->out
, "%d: Expected: [%s]\n", tno
, zAns
);
10388 utf8_printf(p
->out
, "%d: Got: [%s]\n", tno
, str
.z
);
10392 utf8_printf(stderr
,
10393 "Unknown operation \"%s\" on selftest line %d\n", zOp
, tno
);
10397 } /* End loop over rows of content from SELFTEST */
10398 sqlite3_finalize(pStmt
);
10399 } /* End loop over k */
10401 utf8_printf(p
->out
, "%d errors out of %d tests\n", nErr
, nTest
);
10404 if( c
=='s' && cli_strncmp(azArg
[0], "separator", n
)==0 ){
10405 if( nArg
<2 || nArg
>3 ){
10406 raw_printf(stderr
, "Usage: .separator COL ?ROW?\n");
10410 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
,
10411 "%.*s", (int)ArraySize(p
->colSeparator
)-1, azArg
[1]);
10414 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
,
10415 "%.*s", (int)ArraySize(p
->rowSeparator
)-1, azArg
[2]);
10419 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"sha3sum",n
)==0 ){
10420 const char *zLike
= 0; /* Which table to checksum. 0 means everything */
10421 int i
; /* Loop counter */
10422 int bSchema
= 0; /* Also hash the schema */
10423 int bSeparate
= 0; /* Hash each table separately */
10424 int iSize
= 224; /* Hash algorithm to use */
10425 int bDebug
= 0; /* Only show the query that would have run */
10426 sqlite3_stmt
*pStmt
; /* For querying tables names */
10427 char *zSql
; /* SQL to be run */
10428 char *zSep
; /* Separator */
10429 ShellText sSql
; /* Complete SQL for the query to run the hash */
10430 ShellText sQuery
; /* Set of queries used to read all content */
10432 for(i
=1; i
<nArg
; i
++){
10433 const char *z
= azArg
[i
];
10436 if( z
[0]=='-' ) z
++;
10437 if( cli_strcmp(z
,"schema")==0 ){
10440 if( cli_strcmp(z
,"sha3-224")==0 || cli_strcmp(z
,"sha3-256")==0
10441 || cli_strcmp(z
,"sha3-384")==0 || cli_strcmp(z
,"sha3-512")==0
10443 iSize
= atoi(&z
[5]);
10445 if( cli_strcmp(z
,"debug")==0 ){
10449 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
10450 azArg
[i
], azArg
[0]);
10451 showHelp(p
->out
, azArg
[0]);
10453 goto meta_command_exit
;
10456 raw_printf(stderr
, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10458 goto meta_command_exit
;
10462 if( sqlite3_strlike("sqlite\\_%", zLike
, '\\')==0 ) bSchema
= 1;
10466 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
10467 " WHERE type='table' AND coalesce(rootpage,0)>1"
10468 " UNION ALL SELECT 'sqlite_schema'"
10469 " ORDER BY 1 collate nocase";
10471 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
10472 " WHERE type='table' AND coalesce(rootpage,0)>1"
10473 " AND name NOT LIKE 'sqlite_%'"
10474 " ORDER BY 1 collate nocase";
10476 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
10479 appendText(&sSql
, "WITH [sha3sum$query](a,b) AS(",0);
10481 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
10482 const char *zTab
= (const char*)sqlite3_column_text(pStmt
,0);
10483 if( zTab
==0 ) continue;
10484 if( zLike
&& sqlite3_strlike(zLike
, zTab
, 0)!=0 ) continue;
10485 if( cli_strncmp(zTab
, "sqlite_",7)!=0 ){
10486 appendText(&sQuery
,"SELECT * FROM ", 0);
10487 appendText(&sQuery
,zTab
,'"');
10488 appendText(&sQuery
," NOT INDEXED;", 0);
10489 }else if( cli_strcmp(zTab
, "sqlite_schema")==0 ){
10490 appendText(&sQuery
,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10491 " ORDER BY name;", 0);
10492 }else if( cli_strcmp(zTab
, "sqlite_sequence")==0 ){
10493 appendText(&sQuery
,"SELECT name,seq FROM sqlite_sequence"
10494 " ORDER BY name;", 0);
10495 }else if( cli_strcmp(zTab
, "sqlite_stat1")==0 ){
10496 appendText(&sQuery
,"SELECT tbl,idx,stat FROM sqlite_stat1"
10497 " ORDER BY tbl,idx;", 0);
10498 }else if( cli_strcmp(zTab
, "sqlite_stat4")==0 ){
10499 appendText(&sQuery
, "SELECT * FROM ", 0);
10500 appendText(&sQuery
, zTab
, 0);
10501 appendText(&sQuery
, " ORDER BY tbl, idx, rowid;\n", 0);
10503 appendText(&sSql
, zSep
, 0);
10504 appendText(&sSql
, sQuery
.z
, '\'');
10506 appendText(&sSql
, ",", 0);
10507 appendText(&sSql
, zTab
, '\'');
10510 sqlite3_finalize(pStmt
);
10512 zSql
= sqlite3_mprintf(
10514 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10515 " FROM [sha3sum$query]",
10518 zSql
= sqlite3_mprintf(
10520 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10521 " FROM [sha3sum$query]",
10524 shell_check_oom(zSql
);
10528 utf8_printf(p
->out
, "%s\n", zSql
);
10530 shell_exec(p
, zSql
, 0);
10532 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10535 char *zRevText
= /* Query for reversible to-blob-to-text check */
10536 "SELECT lower(name) as tname FROM sqlite_schema\n"
10537 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
10538 "AND name NOT LIKE 'sqlite_%%'%s\n"
10539 "ORDER BY 1 collate nocase";
10540 zRevText
= sqlite3_mprintf(zRevText
, zLike
? " AND name LIKE $tspec" : "");
10541 zRevText
= sqlite3_mprintf(
10542 /* lower-case query is first run, producing upper-case query. */
10543 "with tabcols as materialized(\n"
10544 "select tname, cname\n"
10546 " select ss.tname as tname, ti.name as cname\n"
10547 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
10548 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
10549 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
10550 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
10551 "FROM '||tname||' WHERE '\n"
10552 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
10553 "|| ' AND typeof('||cname||')=''text'' ',\n"
10554 "' OR ') as query, tname from tabcols group by tname)"
10556 shell_check_oom(zRevText
);
10557 if( bDebug
) utf8_printf(p
->out
, "%s\n", zRevText
);
10558 lrc
= sqlite3_prepare_v2(p
->db
, zRevText
, -1, &pStmt
, 0);
10559 if( lrc
!=SQLITE_OK
){
10560 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
10561 ** user does cruel and unnatural things like ".limit expr_depth 0". */
10564 if( zLike
) sqlite3_bind_text(pStmt
,1,zLike
,-1,SQLITE_STATIC
);
10565 lrc
= SQLITE_ROW
==sqlite3_step(pStmt
);
10567 const char *zGenQuery
= (char*)sqlite3_column_text(pStmt
,0);
10568 sqlite3_stmt
*pCheckStmt
;
10569 lrc
= sqlite3_prepare_v2(p
->db
, zGenQuery
, -1, &pCheckStmt
, 0);
10570 if( bDebug
) utf8_printf(p
->out
, "%s\n", zGenQuery
);
10571 if( lrc
!=SQLITE_OK
){
10574 if( SQLITE_ROW
==sqlite3_step(pCheckStmt
) ){
10575 double countIrreversible
= sqlite3_column_double(pCheckStmt
, 0);
10576 if( countIrreversible
>0 ){
10577 int sz
= (int)(countIrreversible
+ 0.5);
10578 utf8_printf(stderr
,
10579 "Digest includes %d invalidly encoded text field%s.\n",
10580 sz
, (sz
>1)? "s": "");
10583 sqlite3_finalize(pCheckStmt
);
10585 sqlite3_finalize(pStmt
);
10588 if( rc
) utf8_printf(stderr
, ".sha3sum failed.\n");
10589 sqlite3_free(zRevText
);
10591 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
10592 sqlite3_free(zSql
);
10595 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10597 && (cli_strncmp(azArg
[0], "shell", n
)==0
10598 || cli_strncmp(azArg
[0],"system",n
)==0)
10602 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
10604 raw_printf(stderr
, "Usage: .system COMMAND\n");
10606 goto meta_command_exit
;
10608 zCmd
= sqlite3_mprintf(strchr(azArg
[1],' ')==0?"%s":"\"%s\"", azArg
[1]);
10609 for(i
=2; i
<nArg
&& zCmd
!=0; i
++){
10610 zCmd
= sqlite3_mprintf(strchr(azArg
[i
],' ')==0?"%z %s":"%z \"%s\"",
10613 x
= zCmd
!=0 ? system(zCmd
) : 1;
10614 sqlite3_free(zCmd
);
10615 if( x
) raw_printf(stderr
, "System command returns %d\n", x
);
10617 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10619 if( c
=='s' && cli_strncmp(azArg
[0], "show", n
)==0 ){
10620 static const char *azBool
[] = { "off", "on", "trigger", "full"};
10624 raw_printf(stderr
, "Usage: .show\n");
10626 goto meta_command_exit
;
10628 utf8_printf(p
->out
, "%12.12s: %s\n","echo",
10629 azBool
[ShellHasFlag(p
, SHFLG_Echo
)]);
10630 utf8_printf(p
->out
, "%12.12s: %s\n","eqp", azBool
[p
->autoEQP
&3]);
10631 utf8_printf(p
->out
, "%12.12s: %s\n","explain",
10632 p
->mode
==MODE_Explain
? "on" : p
->autoExplain
? "auto" : "off");
10633 utf8_printf(p
->out
,"%12.12s: %s\n","headers", azBool
[p
->showHeader
!=0]);
10634 if( p
->mode
==MODE_Column
10635 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
10638 (p
->out
, "%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10639 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
10640 p
->cmOpts
.bWordWrap
? "on" : "off",
10641 p
->cmOpts
.bQuote
? "" : "no");
10643 utf8_printf(p
->out
, "%12.12s: %s\n","mode", modeDescr
[p
->mode
]);
10645 utf8_printf(p
->out
, "%12.12s: ", "nullvalue");
10646 output_c_string(p
->out
, p
->nullValue
);
10647 raw_printf(p
->out
, "\n");
10648 utf8_printf(p
->out
,"%12.12s: %s\n","output",
10649 strlen30(p
->outfile
) ? p
->outfile
: "stdout");
10650 utf8_printf(p
->out
,"%12.12s: ", "colseparator");
10651 output_c_string(p
->out
, p
->colSeparator
);
10652 raw_printf(p
->out
, "\n");
10653 utf8_printf(p
->out
,"%12.12s: ", "rowseparator");
10654 output_c_string(p
->out
, p
->rowSeparator
);
10655 raw_printf(p
->out
, "\n");
10656 switch( p
->statsOn
){
10657 case 0: zOut
= "off"; break;
10658 default: zOut
= "on"; break;
10659 case 2: zOut
= "stmt"; break;
10660 case 3: zOut
= "vmstep"; break;
10662 utf8_printf(p
->out
, "%12.12s: %s\n","stats", zOut
);
10663 utf8_printf(p
->out
, "%12.12s: ", "width");
10664 for (i
=0;i
<p
->nWidth
;i
++) {
10665 raw_printf(p
->out
, "%d ", p
->colWidth
[i
]);
10667 raw_printf(p
->out
, "\n");
10668 utf8_printf(p
->out
, "%12.12s: %s\n", "filename",
10669 p
->pAuxDb
->zDbFilename
? p
->pAuxDb
->zDbFilename
: "");
10672 if( c
=='s' && cli_strncmp(azArg
[0], "stats", n
)==0 ){
10674 if( cli_strcmp(azArg
[1],"stmt")==0 ){
10676 }else if( cli_strcmp(azArg
[1],"vmstep")==0 ){
10679 p
->statsOn
= (u8
)booleanValue(azArg
[1]);
10681 }else if( nArg
==1 ){
10682 display_stats(p
->db
, p
, 0);
10684 raw_printf(stderr
, "Usage: .stats ?on|off|stmt|vmstep?\n");
10689 if( (c
=='t' && n
>1 && cli_strncmp(azArg
[0], "tables", n
)==0)
10690 || (c
=='i' && (cli_strncmp(azArg
[0], "indices", n
)==0
10691 || cli_strncmp(azArg
[0], "indexes", n
)==0) )
10693 sqlite3_stmt
*pStmt
;
10700 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
10702 sqlite3_finalize(pStmt
);
10703 return shellDatabaseError(p
->db
);
10706 if( nArg
>2 && c
=='i' ){
10707 /* It is an historical accident that the .indexes command shows an error
10708 ** when called with the wrong number of arguments whereas the .tables
10709 ** command does not. */
10710 raw_printf(stderr
, "Usage: .indexes ?LIKE-PATTERN?\n");
10712 sqlite3_finalize(pStmt
);
10713 goto meta_command_exit
;
10715 for(ii
=0; sqlite3_step(pStmt
)==SQLITE_ROW
; ii
++){
10716 const char *zDbName
= (const char*)sqlite3_column_text(pStmt
, 1);
10717 if( zDbName
==0 ) continue;
10718 if( s
.z
&& s
.z
[0] ) appendText(&s
, " UNION ALL ", 0);
10719 if( sqlite3_stricmp(zDbName
, "main")==0 ){
10720 appendText(&s
, "SELECT name FROM ", 0);
10722 appendText(&s
, "SELECT ", 0);
10723 appendText(&s
, zDbName
, '\'');
10724 appendText(&s
, "||'.'||name FROM ", 0);
10726 appendText(&s
, zDbName
, '"');
10727 appendText(&s
, ".sqlite_schema ", 0);
10729 appendText(&s
," WHERE type IN ('table','view')"
10730 " AND name NOT LIKE 'sqlite_%'"
10731 " AND name LIKE ?1", 0);
10733 appendText(&s
," WHERE type='index'"
10734 " AND tbl_name LIKE ?1", 0);
10737 rc
= sqlite3_finalize(pStmt
);
10738 if( rc
==SQLITE_OK
){
10739 appendText(&s
, " ORDER BY 1", 0);
10740 rc
= sqlite3_prepare_v2(p
->db
, s
.z
, -1, &pStmt
, 0);
10743 if( rc
) return shellDatabaseError(p
->db
);
10745 /* Run the SQL statement prepared by the above block. Store the results
10746 ** as an array of nul-terminated strings in azResult[]. */
10750 sqlite3_bind_text(pStmt
, 1, azArg
[1], -1, SQLITE_TRANSIENT
);
10752 sqlite3_bind_text(pStmt
, 1, "%", -1, SQLITE_STATIC
);
10754 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
10755 if( nRow
>=nAlloc
){
10757 int n2
= nAlloc
*2 + 10;
10758 azNew
= sqlite3_realloc64(azResult
, sizeof(azResult
[0])*n2
);
10759 shell_check_oom(azNew
);
10763 azResult
[nRow
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
10764 shell_check_oom(azResult
[nRow
]);
10767 if( sqlite3_finalize(pStmt
)!=SQLITE_OK
){
10768 rc
= shellDatabaseError(p
->db
);
10771 /* Pretty-print the contents of array azResult[] to the output */
10772 if( rc
==0 && nRow
>0 ){
10773 int len
, maxlen
= 0;
10775 int nPrintCol
, nPrintRow
;
10776 for(i
=0; i
<nRow
; i
++){
10777 len
= strlen30(azResult
[i
]);
10778 if( len
>maxlen
) maxlen
= len
;
10780 nPrintCol
= 80/(maxlen
+2);
10781 if( nPrintCol
<1 ) nPrintCol
= 1;
10782 nPrintRow
= (nRow
+ nPrintCol
- 1)/nPrintCol
;
10783 for(i
=0; i
<nPrintRow
; i
++){
10784 for(j
=i
; j
<nRow
; j
+=nPrintRow
){
10785 char *zSp
= j
<nPrintRow
? "" : " ";
10786 utf8_printf(p
->out
, "%s%-*s", zSp
, maxlen
,
10787 azResult
[j
] ? azResult
[j
]:"");
10789 raw_printf(p
->out
, "\n");
10793 for(ii
=0; ii
<nRow
; ii
++) sqlite3_free(azResult
[ii
]);
10794 sqlite3_free(azResult
);
10797 #ifndef SQLITE_SHELL_FIDDLE
10798 /* Begin redirecting output to the file "testcase-out.txt" */
10799 if( c
=='t' && cli_strcmp(azArg
[0],"testcase")==0 ){
10801 p
->out
= output_file_open("testcase-out.txt", 0);
10803 raw_printf(stderr
, "Error: cannot open 'testcase-out.txt'\n");
10806 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "%s", azArg
[1]);
10808 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "?");
10811 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10813 #ifndef SQLITE_UNTESTABLE
10814 if( c
=='t' && n
>=8 && cli_strncmp(azArg
[0], "testctrl", n
)==0 ){
10815 static const struct {
10816 const char *zCtrlName
; /* Name of a test-control option */
10817 int ctrlCode
; /* Integer code for that option */
10818 int unSafe
; /* Not valid for --safe mode */
10819 const char *zUsage
; /* Usage notes */
10821 {"always", SQLITE_TESTCTRL_ALWAYS
, 1, "BOOLEAN" },
10822 {"assert", SQLITE_TESTCTRL_ASSERT
, 1, "BOOLEAN" },
10823 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
10824 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
10825 {"byteorder", SQLITE_TESTCTRL_BYTEORDER
, 0, "" },
10826 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS
,0,"BOOLEAN" },
10827 /*{"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, 1,"" },*/
10828 {"imposter", SQLITE_TESTCTRL_IMPOSTER
,1,"SCHEMA ON/OFF ROOTPAGE"},
10829 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
,0,"" },
10830 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT
,0,"BOOLEAN" },
10831 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT
,1, "BOOLEAN" },
10832 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS
,0,"DISABLE-MASK" },
10834 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE
,0,"" },
10836 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE
,0, "OFFSET " },
10837 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE
,0, "" },
10838 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE
, 0, "" },
10839 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED
, 0, "SEED ?db?" },
10840 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT
, 0, "" },
10841 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP
, 0, "NMAX" },
10842 {"tune", SQLITE_TESTCTRL_TUNE
, 1, "ID VALUE" },
10846 int rc2
= 0; /* 0: usage. 1: %d 2: %x 3: no-output */
10849 const char *zCmd
= 0;
10851 if( !ShellHasFlag(p
,SHFLG_TestingMode
) ){
10852 utf8_printf(stderr
, ".%s unavailable without --unsafe-testing\n",
10855 goto meta_command_exit
;
10858 zCmd
= nArg
>=2 ? azArg
[1] : "help";
10860 /* The argument can optionally begin with "-" or "--" */
10861 if( zCmd
[0]=='-' && zCmd
[1] ){
10863 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
10866 /* --help lists all test-controls */
10867 if( cli_strcmp(zCmd
,"help")==0 ){
10868 utf8_printf(p
->out
, "Available test-controls:\n");
10869 for(i
=0; i
<ArraySize(aCtrl
); i
++){
10870 utf8_printf(p
->out
, " .testctrl %s %s\n",
10871 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
10874 goto meta_command_exit
;
10877 /* convert testctrl text option to value. allow any unique prefix
10878 ** of the option name, or a numerical value. */
10879 n2
= strlen30(zCmd
);
10880 for(i
=0; i
<ArraySize(aCtrl
); i
++){
10881 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
10883 testctrl
= aCtrl
[i
].ctrlCode
;
10886 utf8_printf(stderr
, "Error: ambiguous test-control: \"%s\"\n"
10887 "Use \".testctrl --help\" for help\n", zCmd
);
10889 goto meta_command_exit
;
10894 utf8_printf(stderr
,"Error: unknown test-control: %s\n"
10895 "Use \".testctrl --help\" for help\n", zCmd
);
10896 }else if( aCtrl
[iCtrl
].unSafe
&& p
->bSafeMode
){
10897 utf8_printf(stderr
,
10898 "line %d: \".testctrl %s\" may not be used in safe mode\n",
10899 p
->lineno
, aCtrl
[iCtrl
].zCtrlName
);
10904 /* sqlite3_test_control(int, db, int) */
10905 case SQLITE_TESTCTRL_OPTIMIZATIONS
:
10907 unsigned int opt
= (unsigned int)strtol(azArg
[2], 0, 0);
10908 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
10913 /* sqlite3_test_control(int) */
10914 case SQLITE_TESTCTRL_PRNG_SAVE
:
10915 case SQLITE_TESTCTRL_PRNG_RESTORE
:
10916 case SQLITE_TESTCTRL_BYTEORDER
:
10918 rc2
= sqlite3_test_control(testctrl
);
10919 isOk
= testctrl
==SQLITE_TESTCTRL_BYTEORDER
? 1 : 3;
10923 /* sqlite3_test_control(int, uint) */
10924 case SQLITE_TESTCTRL_PENDING_BYTE
:
10926 unsigned int opt
= (unsigned int)integerValue(azArg
[2]);
10927 rc2
= sqlite3_test_control(testctrl
, opt
);
10932 /* sqlite3_test_control(int, int, sqlite3*) */
10933 case SQLITE_TESTCTRL_PRNG_SEED
:
10934 if( nArg
==3 || nArg
==4 ){
10935 int ii
= (int)integerValue(azArg
[2]);
10937 if( ii
==0 && cli_strcmp(azArg
[2],"random")==0 ){
10938 sqlite3_randomness(sizeof(ii
),&ii
);
10939 printf("-- random seed: %d\n", ii
);
10945 /* Make sure the schema has been loaded */
10946 sqlite3_table_column_metadata(db
, 0, "x", 0, 0, 0, 0, 0, 0);
10948 rc2
= sqlite3_test_control(testctrl
, ii
, db
);
10953 /* sqlite3_test_control(int, int) */
10954 case SQLITE_TESTCTRL_ASSERT
:
10955 case SQLITE_TESTCTRL_ALWAYS
:
10957 int opt
= booleanValue(azArg
[2]);
10958 rc2
= sqlite3_test_control(testctrl
, opt
);
10963 /* sqlite3_test_control(int, int) */
10964 case SQLITE_TESTCTRL_LOCALTIME_FAULT
:
10965 case SQLITE_TESTCTRL_NEVER_CORRUPT
:
10967 int opt
= booleanValue(azArg
[2]);
10968 rc2
= sqlite3_test_control(testctrl
, opt
);
10973 /* sqlite3_test_control(sqlite3*) */
10974 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
:
10975 rc2
= sqlite3_test_control(testctrl
, p
->db
);
10979 case SQLITE_TESTCTRL_IMPOSTER
:
10981 rc2
= sqlite3_test_control(testctrl
, p
->db
,
10983 integerValue(azArg
[3]),
10984 integerValue(azArg
[4]));
10989 case SQLITE_TESTCTRL_SEEK_COUNT
: {
10991 rc2
= sqlite3_test_control(testctrl
, p
->db
, &x
);
10992 utf8_printf(p
->out
, "%llu\n", x
);
10998 case SQLITE_TESTCTRL_PARSER_COVERAGE
: {
11000 sqlite3_test_control(testctrl
, p
->out
);
11006 #ifdef SQLITE_DEBUG
11007 case SQLITE_TESTCTRL_TUNE
: {
11009 int id
= (int)integerValue(azArg
[2]);
11010 int val
= (int)integerValue(azArg
[3]);
11011 sqlite3_test_control(testctrl
, id
, &val
);
11013 }else if( nArg
==3 ){
11014 int id
= (int)integerValue(azArg
[2]);
11015 sqlite3_test_control(testctrl
, -id
, &rc2
);
11017 }else if( nArg
==2 ){
11021 rc2
= sqlite3_test_control(testctrl
, -id
, &val
);
11022 if( rc2
!=SQLITE_OK
) break;
11023 if( id
>1 ) utf8_printf(p
->out
, " ");
11024 utf8_printf(p
->out
, "%d: %d", id
, val
);
11027 if( id
>1 ) utf8_printf(p
->out
, "\n");
11033 case SQLITE_TESTCTRL_SORTER_MMAP
:
11035 int opt
= (unsigned int)integerValue(azArg
[2]);
11036 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
11042 if( isOk
==0 && iCtrl
>=0 ){
11043 utf8_printf(p
->out
, "Usage: .testctrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
11045 }else if( isOk
==1 ){
11046 raw_printf(p
->out
, "%d\n", rc2
);
11047 }else if( isOk
==2 ){
11048 raw_printf(p
->out
, "0x%08x\n", rc2
);
11051 #endif /* !defined(SQLITE_UNTESTABLE) */
11053 if( c
=='t' && n
>4 && cli_strncmp(azArg
[0], "timeout", n
)==0 ){
11055 sqlite3_busy_timeout(p
->db
, nArg
>=2 ? (int)integerValue(azArg
[1]) : 0);
11058 if( c
=='t' && n
>=5 && cli_strncmp(azArg
[0], "timer", n
)==0 ){
11060 enableTimer
= booleanValue(azArg
[1]);
11061 if( enableTimer
&& !HAS_TIMER
){
11062 raw_printf(stderr
, "Error: timer not available on this system.\n");
11066 raw_printf(stderr
, "Usage: .timer on|off\n");
11071 #ifndef SQLITE_OMIT_TRACE
11072 if( c
=='t' && cli_strncmp(azArg
[0], "trace", n
)==0 ){
11076 for(jj
=1; jj
<nArg
; jj
++){
11077 const char *z
= azArg
[jj
];
11079 if( optionMatch(z
, "expanded") ){
11080 p
->eTraceType
= SHELL_TRACE_EXPANDED
;
11082 #ifdef SQLITE_ENABLE_NORMALIZE
11083 else if( optionMatch(z
, "normalized") ){
11084 p
->eTraceType
= SHELL_TRACE_NORMALIZED
;
11087 else if( optionMatch(z
, "plain") ){
11088 p
->eTraceType
= SHELL_TRACE_PLAIN
;
11090 else if( optionMatch(z
, "profile") ){
11091 mType
|= SQLITE_TRACE_PROFILE
;
11093 else if( optionMatch(z
, "row") ){
11094 mType
|= SQLITE_TRACE_ROW
;
11096 else if( optionMatch(z
, "stmt") ){
11097 mType
|= SQLITE_TRACE_STMT
;
11099 else if( optionMatch(z
, "close") ){
11100 mType
|= SQLITE_TRACE_CLOSE
;
11103 raw_printf(stderr
, "Unknown option \"%s\" on \".trace\"\n", z
);
11105 goto meta_command_exit
;
11108 output_file_close(p
->traceOut
);
11109 p
->traceOut
= output_file_open(z
, 0);
11112 if( p
->traceOut
==0 ){
11113 sqlite3_trace_v2(p
->db
, 0, 0, 0);
11115 if( mType
==0 ) mType
= SQLITE_TRACE_STMT
;
11116 sqlite3_trace_v2(p
->db
, mType
, sql_trace_callback
, p
);
11119 #endif /* !defined(SQLITE_OMIT_TRACE) */
11121 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11122 if( c
=='u' && cli_strncmp(azArg
[0], "unmodule", n
)==0 ){
11127 raw_printf(stderr
, "Usage: .unmodule [--allexcept] NAME ...\n");
11129 goto meta_command_exit
;
11133 if( zOpt
[0]=='-' && zOpt
[1]=='-' && zOpt
[2]!=0 ) zOpt
++;
11134 lenOpt
= (int)strlen(zOpt
);
11135 if( lenOpt
>=3 && cli_strncmp(zOpt
, "-allexcept",lenOpt
)==0 ){
11136 assert( azArg
[nArg
]==0 );
11137 sqlite3_drop_modules(p
->db
, nArg
>2 ? (const char**)(azArg
+2) : 0);
11139 for(ii
=1; ii
<nArg
; ii
++){
11140 sqlite3_create_module(p
->db
, azArg
[ii
], 0, 0);
11146 #if SQLITE_USER_AUTHENTICATION
11147 if( c
=='u' && cli_strncmp(azArg
[0], "user", n
)==0 ){
11149 raw_printf(stderr
, "Usage: .user SUBCOMMAND ...\n");
11151 goto meta_command_exit
;
11154 if( cli_strcmp(azArg
[1],"login")==0 ){
11156 raw_printf(stderr
, "Usage: .user login USER PASSWORD\n");
11158 goto meta_command_exit
;
11160 rc
= sqlite3_user_authenticate(p
->db
, azArg
[2], azArg
[3],
11161 strlen30(azArg
[3]));
11163 utf8_printf(stderr
, "Authentication failed for user %s\n", azArg
[2]);
11166 }else if( cli_strcmp(azArg
[1],"add")==0 ){
11168 raw_printf(stderr
, "Usage: .user add USER PASSWORD ISADMIN\n");
11170 goto meta_command_exit
;
11172 rc
= sqlite3_user_add(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
11173 booleanValue(azArg
[4]));
11175 raw_printf(stderr
, "User-Add failed: %d\n", rc
);
11178 }else if( cli_strcmp(azArg
[1],"edit")==0 ){
11180 raw_printf(stderr
, "Usage: .user edit USER PASSWORD ISADMIN\n");
11182 goto meta_command_exit
;
11184 rc
= sqlite3_user_change(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
11185 booleanValue(azArg
[4]));
11187 raw_printf(stderr
, "User-Edit failed: %d\n", rc
);
11190 }else if( cli_strcmp(azArg
[1],"delete")==0 ){
11192 raw_printf(stderr
, "Usage: .user delete USER\n");
11194 goto meta_command_exit
;
11196 rc
= sqlite3_user_delete(p
->db
, azArg
[2]);
11198 raw_printf(stderr
, "User-Delete failed: %d\n", rc
);
11202 raw_printf(stderr
, "Usage: .user login|add|edit|delete ...\n");
11204 goto meta_command_exit
;
11207 #endif /* SQLITE_USER_AUTHENTICATION */
11209 if( c
=='v' && cli_strncmp(azArg
[0], "version", n
)==0 ){
11210 utf8_printf(p
->out
, "SQLite %s %s\n" /*extra-version-info*/,
11211 sqlite3_libversion(), sqlite3_sourceid());
11212 /* BEGIN SQLCIPHER */
11213 #ifdef SQLITE_HAS_CODEC
11215 extern char* sqlcipher_version();
11216 char *sqlcipher_ver
= sqlcipher_version();
11217 utf8_printf(p
->out
, "SQLCipher %s\n", sqlcipher_ver
);
11218 sqlite3_free(sqlcipher_ver
);
11221 /* END SQLCIPHER */
11222 #if SQLITE_HAVE_ZLIB
11223 utf8_printf(p
->out
, "zlib version %s\n", zlibVersion());
11225 #define CTIMEOPT_VAL_(opt) #opt
11226 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11227 #if defined(__clang__) && defined(__clang_major__)
11228 utf8_printf(p
->out
, "clang-" CTIMEOPT_VAL(__clang_major__
) "."
11229 CTIMEOPT_VAL(__clang_minor__
) "."
11230 CTIMEOPT_VAL(__clang_patchlevel__
) "\n");
11231 #elif defined(_MSC_VER)
11232 utf8_printf(p
->out
, "msvc-" CTIMEOPT_VAL(_MSC_VER
) "\n");
11233 #elif defined(__GNUC__) && defined(__VERSION__)
11234 utf8_printf(p
->out
, "gcc-" __VERSION__
"\n");
11238 if( c
=='v' && cli_strncmp(azArg
[0], "vfsinfo", n
)==0 ){
11239 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
11240 sqlite3_vfs
*pVfs
= 0;
11242 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
11244 utf8_printf(p
->out
, "vfs.zName = \"%s\"\n", pVfs
->zName
);
11245 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
11246 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
11247 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
11252 if( c
=='v' && cli_strncmp(azArg
[0], "vfslist", n
)==0 ){
11254 sqlite3_vfs
*pCurrent
= 0;
11256 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_VFS_POINTER
, &pCurrent
);
11258 for(pVfs
=sqlite3_vfs_find(0); pVfs
; pVfs
=pVfs
->pNext
){
11259 utf8_printf(p
->out
, "vfs.zName = \"%s\"%s\n", pVfs
->zName
,
11260 pVfs
==pCurrent
? " <--- CURRENT" : "");
11261 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
11262 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
11263 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
11265 raw_printf(p
->out
, "-----------------------------------\n");
11270 if( c
=='v' && cli_strncmp(azArg
[0], "vfsname", n
)==0 ){
11271 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
11272 char *zVfsName
= 0;
11274 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFSNAME
, &zVfsName
);
11276 utf8_printf(p
->out
, "%s\n", zVfsName
);
11277 sqlite3_free(zVfsName
);
11282 if( c
=='w' && cli_strncmp(azArg
[0], "wheretrace", n
)==0 ){
11283 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
11284 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &x
);
11287 if( c
=='w' && cli_strncmp(azArg
[0], "width", n
)==0 ){
11289 assert( nArg
<=ArraySize(azArg
) );
11290 p
->nWidth
= nArg
-1;
11291 p
->colWidth
= realloc(p
->colWidth
, (p
->nWidth
+1)*sizeof(int)*2);
11292 if( p
->colWidth
==0 && p
->nWidth
>0 ) shell_out_of_memory();
11293 if( p
->nWidth
) p
->actualWidth
= &p
->colWidth
[p
->nWidth
];
11294 for(j
=1; j
<nArg
; j
++){
11295 p
->colWidth
[j
-1] = (int)integerValue(azArg
[j
]);
11300 utf8_printf(stderr
, "Error: unknown command or invalid arguments: "
11301 " \"%s\". Enter \".help\" for help\n", azArg
[0]);
11308 if( p
->outCount
==0 ) output_reset(p
);
11310 p
->bSafeMode
= p
->bSafeModePersist
;
11314 /* Line scan result and intermediate states (supporting scan resumption)
11317 # define CHAR_BIT 8
11320 QSS_HasDark
= 1<<CHAR_BIT
, QSS_EndingSemi
= 2<<CHAR_BIT
,
11321 QSS_CharMask
= (1<<CHAR_BIT
)-1, QSS_ScanMask
= 3<<CHAR_BIT
,
11324 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11325 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11326 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11327 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11328 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11331 ** Scan line for classification to guide shell's handling.
11332 ** The scan is resumable for subsequent lines when prior
11333 ** return values are passed as the 2nd argument.
11335 static QuickScanState
quickscan(char *zLine
, QuickScanState qss
,
11336 SCAN_TRACKER_REFTYPE pst
){
11338 char cWait
= (char)qss
; /* intentional narrowing loss */
11341 assert( cWait
==0 );
11342 while( (cin
= *zLine
++)!=0 ){
11349 while((cin
= *++zLine
)!=0 )
11354 qss
|= QSS_EndingSemi
;
11360 CONTINUE_PROMPT_AWAITS(pst
, "/*");
11361 qss
= QSS_SETV(qss
, cWait
);
11367 deliberate_fall_through
;
11368 case '`': case '\'': case '"':
11370 qss
= QSS_HasDark
| cWait
;
11371 CONTINUE_PROMPT_AWAITC(pst
, cin
);
11374 CONTINUE_PAREN_INCR(pst
, 1);
11377 CONTINUE_PAREN_INCR(pst
, -1);
11382 qss
= (qss
& ~QSS_EndingSemi
) | QSS_HasDark
;
11386 while( (cin
= *zLine
++)!=0 ){
11390 if( *zLine
!= '/' )
11394 CONTINUE_PROMPT_AWAITC(pst
, 0);
11395 qss
= QSS_SETV(qss
, 0);
11397 case '`': case '\'': case '"':
11399 /* Swallow doubled end-delimiter.*/
11403 deliberate_fall_through
;
11406 CONTINUE_PROMPT_AWAITC(pst
, 0);
11407 qss
= QSS_SETV(qss
, 0);
11409 default: assert(0);
11418 ** Return TRUE if the line typed in is an SQL command terminator other
11419 ** than a semi-colon. The SQL Server style "go" command is understood
11420 ** as is the Oracle "/".
11422 static int line_is_command_terminator(char *zLine
){
11423 while( IsSpace(zLine
[0]) ){ zLine
++; };
11424 if( zLine
[0]=='/' )
11425 zLine
+= 1; /* Oracle */
11426 else if ( ToLower(zLine
[0])=='g' && ToLower(zLine
[1])=='o' )
11427 zLine
+= 2; /* SQL Server */
11430 return quickscan(zLine
, QSS_Start
, 0)==QSS_Start
;
11434 ** The CLI needs a working sqlite3_complete() to work properly. So error
11435 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
11437 #ifdef SQLITE_OMIT_COMPLETE
11438 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
11442 ** Return true if zSql is a complete SQL statement. Return false if it
11443 ** ends in the middle of a string literal or C-style comment.
11445 static int line_is_complete(char *zSql
, int nSql
){
11447 if( zSql
==0 ) return 1;
11450 rc
= sqlite3_complete(zSql
);
11456 ** Run a single line of SQL. Return the number of errors.
11458 static int runOneSqlLine(ShellState
*p
, char *zSql
, FILE *in
, int startline
){
11463 if( ShellHasFlag(p
,SHFLG_Backslash
) ) resolve_backslashes(zSql
);
11464 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
11466 rc
= shell_exec(p
, zSql
, &zErrMsg
);
11468 if( rc
|| zErrMsg
){
11470 const char *zErrorTail
;
11471 const char *zErrorType
;
11473 zErrorType
= "Error";
11474 zErrorTail
= sqlite3_errmsg(p
->db
);
11475 }else if( cli_strncmp(zErrMsg
, "in prepare, ",12)==0 ){
11476 zErrorType
= "Parse error";
11477 zErrorTail
= &zErrMsg
[12];
11478 }else if( cli_strncmp(zErrMsg
, "stepping, ", 10)==0 ){
11479 zErrorType
= "Runtime error";
11480 zErrorTail
= &zErrMsg
[10];
11482 zErrorType
= "Error";
11483 zErrorTail
= zErrMsg
;
11485 if( in
!=0 || !stdin_is_interactive
){
11486 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
,
11487 "%s near line %d:", zErrorType
, startline
);
11489 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:", zErrorType
);
11491 utf8_printf(stderr
, "%s %s\n", zPrefix
, zErrorTail
);
11492 sqlite3_free(zErrMsg
);
11495 }else if( ShellHasFlag(p
, SHFLG_CountChanges
) ){
11496 char zLineBuf
[2000];
11497 sqlite3_snprintf(sizeof(zLineBuf
), zLineBuf
,
11498 "changes: %lld total_changes: %lld",
11499 sqlite3_changes64(p
->db
), sqlite3_total_changes64(p
->db
));
11500 raw_printf(p
->out
, "%s\n", zLineBuf
);
11505 static void echo_group_input(ShellState
*p
, const char *zDo
){
11506 if( ShellHasFlag(p
, SHFLG_Echo
) ) utf8_printf(p
->out
, "%s\n", zDo
);
11509 #ifdef SQLITE_SHELL_FIDDLE
11511 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
11512 ** impl because we need the global shellState and cannot access it from that
11513 ** function without moving lots of code around (creating a larger/messier diff).
11515 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
11516 /* Parse the next line from shellState.wasm.zInput. */
11517 const char *zBegin
= shellState
.wasm
.zPos
;
11518 const char *z
= zBegin
;
11522 UNUSED_PARAMETER(in
);
11523 UNUSED_PARAMETER(isContinuation
);
11527 while(*z
&& isspace(*z
)) ++z
;
11529 for(; *z
&& '\n'!=*z
; ++nZ
, ++z
){}
11530 if(nZ
>0 && '\r'==zBegin
[nZ
-1]){
11533 shellState
.wasm
.zPos
= z
;
11534 zLine
= realloc(zPrior
, nZ
+1);
11535 shell_check_oom(zLine
);
11536 memcpy(zLine
, zBegin
, nZ
);
11540 #endif /* SQLITE_SHELL_FIDDLE */
11543 ** Read input from *in and process it. If *in==0 then input
11544 ** is interactive - the user is typing it it. Otherwise, input
11545 ** is coming from a file or device. A prompt is issued and history
11546 ** is saved only if input is interactive. An interrupt signal will
11547 ** cause this routine to exit immediately, unless input is interactive.
11549 ** Return the number of errors.
11551 static int process_input(ShellState
*p
){
11552 char *zLine
= 0; /* A single input line */
11553 char *zSql
= 0; /* Accumulated SQL text */
11554 i64 nLine
; /* Length of current line */
11555 i64 nSql
= 0; /* Bytes of zSql[] used */
11556 i64 nAlloc
= 0; /* Allocated zSql[] space */
11557 int rc
; /* Error code */
11558 int errCnt
= 0; /* Number of errors seen */
11559 i64 startline
= 0; /* Line number for start of current input */
11560 QuickScanState qss
= QSS_Start
; /* Accumulated line status (so far) */
11562 if( p
->inputNesting
==MAX_INPUT_NESTING
){
11563 /* This will be more informative in a later version. */
11564 utf8_printf(stderr
,"Input nesting limit (%d) reached at line %d."
11565 " Check recursion.\n", MAX_INPUT_NESTING
, p
->lineno
);
11570 CONTINUE_PROMPT_RESET
;
11571 while( errCnt
==0 || !bail_on_error
|| (p
->in
==0 && stdin_is_interactive
) ){
11573 zLine
= one_input_line(p
->in
, zLine
, nSql
>0);
11576 if( p
->in
==0 && stdin_is_interactive
) printf("\n");
11579 if( seenInterrupt
){
11580 if( p
->in
!=0 ) break;
11584 if( QSS_INPLAIN(qss
)
11585 && line_is_command_terminator(zLine
)
11586 && line_is_complete(zSql
, nSql
) ){
11587 memcpy(zLine
,";",2);
11589 qss
= quickscan(zLine
, qss
, CONTINUE_PROMPT_PSTATE
);
11590 if( QSS_PLAINWHITE(qss
) && nSql
==0 ){
11591 /* Just swallow single-line whitespace */
11592 echo_group_input(p
, zLine
);
11596 if( zLine
&& (zLine
[0]=='.' || zLine
[0]=='#') && nSql
==0 ){
11597 CONTINUE_PROMPT_RESET
;
11598 echo_group_input(p
, zLine
);
11599 if( zLine
[0]=='.' ){
11600 rc
= do_meta_command(zLine
, p
);
11601 if( rc
==2 ){ /* exit requested */
11610 /* No single-line dispositions remain; accumulate line(s). */
11611 nLine
= strlen(zLine
);
11612 if( nSql
+nLine
+2>=nAlloc
){
11613 /* Grow buffer by half-again increments when big. */
11614 nAlloc
= nSql
+(nSql
>>1)+nLine
+100;
11615 zSql
= realloc(zSql
, nAlloc
);
11616 shell_check_oom(zSql
);
11620 for(i
=0; zLine
[i
] && IsSpace(zLine
[i
]); i
++){}
11621 assert( nAlloc
>0 && zSql
!=0 );
11622 memcpy(zSql
, zLine
+i
, nLine
+1-i
);
11623 startline
= p
->lineno
;
11626 zSql
[nSql
++] = '\n';
11627 memcpy(zSql
+nSql
, zLine
, nLine
+1);
11630 if( nSql
&& QSS_SEMITERM(qss
) && sqlite3_complete(zSql
) ){
11631 echo_group_input(p
, zSql
);
11632 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
11633 CONTINUE_PROMPT_RESET
;
11641 p
->bSafeMode
= p
->bSafeModePersist
;
11643 }else if( nSql
&& QSS_PLAINWHITE(qss
) ){
11644 echo_group_input(p
, zSql
);
11650 /* This may be incomplete. Let the SQL parser deal with that. */
11651 echo_group_input(p
, zSql
);
11652 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
11653 CONTINUE_PROMPT_RESET
;
11662 ** Return a pathname which is the user's home directory. A
11663 ** 0 return indicates an error of some kind.
11665 static char *find_home_dir(int clearFlag
){
11666 static char *home_dir
= NULL
;
11672 if( home_dir
) return home_dir
;
11674 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
11675 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
11677 struct passwd
*pwent
;
11678 uid_t uid
= getuid();
11679 if( (pwent
=getpwuid(uid
)) != NULL
) {
11680 home_dir
= pwent
->pw_dir
;
11685 #if defined(_WIN32_WCE)
11686 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
11691 #if defined(_WIN32) || defined(WIN32)
11693 home_dir
= getenv("USERPROFILE");
11698 home_dir
= getenv("HOME");
11701 #if defined(_WIN32) || defined(WIN32)
11703 char *zDrive
, *zPath
;
11705 zDrive
= getenv("HOMEDRIVE");
11706 zPath
= getenv("HOMEPATH");
11707 if( zDrive
&& zPath
){
11708 n
= strlen30(zDrive
) + strlen30(zPath
) + 1;
11709 home_dir
= malloc( n
);
11710 if( home_dir
==0 ) return 0;
11711 sqlite3_snprintf(n
, home_dir
, "%s%s", zDrive
, zPath
);
11718 #endif /* !_WIN32_WCE */
11721 i64 n
= strlen(home_dir
) + 1;
11722 char *z
= malloc( n
);
11723 if( z
) memcpy(z
, home_dir
, n
);
11731 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
11732 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
11733 ** the path to it, else return 0. The result is cached for
11734 ** subsequent calls.
11736 static const char *find_xdg_config(void){
11737 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
11738 || defined(__RTP__) || defined(_WRS_KERNEL)
11741 static int alreadyTried
= 0;
11742 static char *zConfig
= 0;
11743 const char *zXdgHome
;
11745 if( alreadyTried
!=0 ){
11749 zXdgHome
= getenv("XDG_CONFIG_HOME");
11753 zConfig
= sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome
);
11754 shell_check_oom(zConfig
);
11755 if( access(zConfig
,0)!=0 ){
11756 sqlite3_free(zConfig
);
11764 ** Read input from the file given by sqliterc_override. Or if that
11765 ** parameter is NULL, take input from the first of find_xdg_config()
11766 ** or ~/.sqliterc which is found.
11768 ** Returns the number of errors.
11770 static void process_sqliterc(
11771 ShellState
*p
, /* Configuration data */
11772 const char *sqliterc_override
/* Name of config file. NULL to use default */
11774 char *home_dir
= NULL
;
11775 const char *sqliterc
= sqliterc_override
;
11777 FILE *inSaved
= p
->in
;
11778 int savedLineno
= p
->lineno
;
11780 if( sqliterc
== NULL
){
11781 sqliterc
= find_xdg_config();
11783 if( sqliterc
== NULL
){
11784 home_dir
= find_home_dir(0);
11786 raw_printf(stderr
, "-- warning: cannot find home directory;"
11787 " cannot read ~/.sqliterc\n");
11790 zBuf
= sqlite3_mprintf("%s/.sqliterc",home_dir
);
11791 shell_check_oom(zBuf
);
11794 p
->in
= fopen(sqliterc
,"rb");
11796 if( stdin_is_interactive
){
11797 utf8_printf(stderr
,"-- Loading resources from %s\n",sqliterc
);
11799 if( process_input(p
) && bail_on_error
) exit(1);
11801 }else if( sqliterc_override
!=0 ){
11802 utf8_printf(stderr
,"cannot open: \"%s\"\n", sqliterc
);
11803 if( bail_on_error
) exit(1);
11806 p
->lineno
= savedLineno
;
11807 sqlite3_free(zBuf
);
11811 ** Show available command line options
11813 static const char zOptions
[] =
11814 " -- treat no subsequent arguments as options\n"
11815 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11816 " -A ARGS... run \".archive ARGS\" and exit\n"
11818 " -append append the database to the end of the file\n"
11819 " -ascii set output mode to 'ascii'\n"
11820 " -bail stop after hitting an error\n"
11821 " -batch force batch I/O\n"
11822 " -box set output mode to 'box'\n"
11823 " -column set output mode to 'column'\n"
11824 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
11825 " -csv set output mode to 'csv'\n"
11826 #if !defined(SQLITE_OMIT_DESERIALIZE)
11827 " -deserialize open the database using sqlite3_deserialize()\n"
11829 " -echo print inputs before execution\n"
11830 " -init FILENAME read/process named file\n"
11831 " -[no]header turn headers on or off\n"
11832 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
11833 " -heap SIZE Size of heap for memsys3 or memsys5\n"
11835 " -help show this message\n"
11836 " -html set output mode to HTML\n"
11837 " -interactive force interactive I/O\n"
11838 " -json set output mode to 'json'\n"
11839 " -line set output mode to 'line'\n"
11840 " -list set output mode to 'list'\n"
11841 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
11842 " -markdown set output mode to 'markdown'\n"
11843 #if !defined(SQLITE_OMIT_DESERIALIZE)
11844 " -maxsize N maximum size for a --deserialize database\n"
11846 " -memtrace trace all memory allocations and deallocations\n"
11847 " -mmap N default mmap size set to N\n"
11848 #ifdef SQLITE_ENABLE_MULTIPLEX
11849 " -multiplex enable the multiplexor VFS\n"
11851 " -newline SEP set output row separator. Default: '\\n'\n"
11852 " -nofollow refuse to open symbolic links to database files\n"
11853 " -nonce STRING set the safe-mode escape nonce\n"
11854 " -nullvalue TEXT set text string for NULL values. Default ''\n"
11855 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
11856 " -quote set output mode to 'quote'\n"
11857 " -readonly open the database read-only\n"
11858 " -safe enable safe-mode\n"
11859 " -separator SEP set output column separator. Default: '|'\n"
11860 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
11861 " -sorterref SIZE sorter references threshold size\n"
11863 " -stats print memory stats before each finalize\n"
11864 " -table set output mode to 'table'\n"
11865 " -tabs set output mode to 'tabs'\n"
11866 " -unsafe-testing allow unsafe commands and modes for testing\n"
11867 #if SHELL_WIN_UTF8_OPT
11868 " -utf8 setup interactive console code page for UTF-8\n"
11870 " -version show SQLite version\n"
11871 " -vfs NAME use NAME as the default VFS\n"
11872 #ifdef SQLITE_ENABLE_VFSTRACE
11873 " -vfstrace enable tracing of all VFS calls\n"
11875 #ifdef SQLITE_HAVE_ZLIB
11876 " -zip open the file as a ZIP Archive\n"
11879 static void usage(int showDetail
){
11880 utf8_printf(stderr
,
11881 "Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
11882 "FILENAME is the name of an SQLite database. A new database is created\n"
11883 "if the file does not previously exist. Defaults to :memory:.\n", Argv0
);
11885 utf8_printf(stderr
, "OPTIONS include:\n%s", zOptions
);
11887 raw_printf(stderr
, "Use the -help option for additional information\n");
11893 ** Internal check: Verify that the SQLite is uninitialized. Print a
11894 ** error message if it is initialized.
11896 static void verify_uninitialized(void){
11897 if( sqlite3_config(-1)==SQLITE_MISUSE
){
11898 utf8_printf(stdout
, "WARNING: attempt to configure SQLite after"
11899 " initialization.\n");
11904 ** Initialize the state information in data
11906 static void main_init(ShellState
*data
) {
11907 memset(data
, 0, sizeof(*data
));
11908 data
->normalMode
= data
->cMode
= data
->mode
= MODE_List
;
11909 data
->autoExplain
= 1;
11910 data
->pAuxDb
= &data
->aAuxDb
[0];
11911 memcpy(data
->colSeparator
,SEP_Column
, 2);
11912 memcpy(data
->rowSeparator
,SEP_Row
, 2);
11913 data
->showHeader
= 0;
11914 data
->shellFlgs
= SHFLG_Lookaside
;
11915 sqlite3_config(SQLITE_CONFIG_LOG
, shellLog
, data
);
11916 #if !defined(SQLITE_SHELL_FIDDLE)
11917 verify_uninitialized();
11919 sqlite3_config(SQLITE_CONFIG_URI
, 1);
11920 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
11921 sqlite3_snprintf(sizeof(mainPrompt
), mainPrompt
,"sqlite> ");
11922 sqlite3_snprintf(sizeof(continuePrompt
), continuePrompt
," ...> ");
11926 ** Output text to the console in a font that attracts extra attention.
11929 static void printBold(const char *zText
){
11930 #if !SQLITE_OS_WINRT
11931 HANDLE out
= GetStdHandle(STD_OUTPUT_HANDLE
);
11932 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo
;
11933 GetConsoleScreenBufferInfo(out
, &defaultScreenInfo
);
11934 SetConsoleTextAttribute(out
,
11935 FOREGROUND_RED
|FOREGROUND_INTENSITY
11938 printf("%s", zText
);
11939 #if !SQLITE_OS_WINRT
11940 SetConsoleTextAttribute(out
, defaultScreenInfo
.wAttributes
);
11944 static void printBold(const char *zText
){
11945 printf("\033[1m%s\033[0m", zText
);
11950 ** Get the argument to an --option. Throw an error and die if no argument
11953 static char *cmdline_option_value(int argc
, char **argv
, int i
){
11955 utf8_printf(stderr
, "%s: Error: missing argument to %s\n",
11956 argv
[0], argv
[argc
-1]);
11962 static void sayAbnormalExit(void){
11963 if( seenInterrupt
) fprintf(stderr
, "Program interrupted.\n");
11966 #ifndef SQLITE_SHELL_IS_UTF8
11967 # if (defined(_WIN32) || defined(WIN32)) \
11968 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
11969 # define SQLITE_SHELL_IS_UTF8 (0)
11971 # define SQLITE_SHELL_IS_UTF8 (1)
11975 #ifdef SQLITE_SHELL_FIDDLE
11976 # define main fiddle_main
11979 #if SQLITE_SHELL_IS_UTF8
11980 int SQLITE_CDECL
main(int argc
, char **argv
){
11982 int SQLITE_CDECL
wmain(int argc
, wchar_t **wargv
){
11985 #ifdef SQLITE_DEBUG
11986 sqlite3_int64 mem_main_enter
= 0;
11989 #ifdef SQLITE_SHELL_FIDDLE
11990 # define data shellState
11994 const char *zInitFile
= 0;
11997 int warnInmemoryDb
= 0;
12000 int nOptsEnd
= argc
;
12002 const char *zVfs
= 0; /* Value of -vfs command-line option */
12003 #if !SQLITE_SHELL_IS_UTF8
12004 char **argvToFree
= 0;
12005 int argcToFree
= 0;
12007 setvbuf(stderr
, 0, _IONBF
, 0); /* Make sure stderr is unbuffered */
12009 #ifdef SQLITE_SHELL_FIDDLE
12010 stdin_is_interactive
= 0;
12011 stdout_is_console
= 1;
12012 data
.wasm
.zDefaultDbName
= "/fiddle.sqlite3";
12014 stdin_is_interactive
= isatty(0);
12015 stdout_is_console
= isatty(1);
12017 #if SHELL_WIN_UTF8_OPT
12018 atexit(console_restore
); /* Needs revision for CLI as library call */
12020 atexit(sayAbnormalExit
);
12021 #ifdef SQLITE_DEBUG
12022 mem_main_enter
= sqlite3_memory_used();
12024 #if !defined(_WIN32_WCE)
12025 if( getenv("SQLITE_DEBUG_BREAK") ){
12026 if( isatty(0) && isatty(2) ){
12028 "attach debugger to process %d and press any key to continue.\n",
12032 #if defined(_WIN32) || defined(WIN32)
12033 #if SQLITE_OS_WINRT
12038 #elif defined(SIGTRAP)
12044 /* Register a valid signal handler early, before much else is done. */
12046 signal(SIGINT
, interrupt_handler
);
12047 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12048 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
) ){
12049 fprintf(stderr
, "No ^C handler.\n");
12053 #if USE_SYSTEM_SQLITE+0!=1
12054 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID
,60)!=0 ){
12055 utf8_printf(stderr
, "SQLite header and source version mismatch\n%s\n%s\n",
12056 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
12062 /* On Windows, we must translate command-line arguments into UTF-8.
12063 ** The SQLite memory allocator subsystem has to be enabled in order to
12064 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
12065 ** subsequent sqlite3_config() calls will work. So copy all results into
12066 ** memory that does not come from the SQLite memory allocator.
12068 #if !SQLITE_SHELL_IS_UTF8
12069 sqlite3_initialize();
12070 argvToFree
= malloc(sizeof(argv
[0])*argc
*2);
12071 shell_check_oom(argvToFree
);
12073 argv
= argvToFree
+ argc
;
12074 for(i
=0; i
<argc
; i
++){
12075 char *z
= sqlite3_win32_unicode_to_utf8(wargv
[i
]);
12077 shell_check_oom(z
);
12079 argv
[i
] = malloc( n
+1 );
12080 shell_check_oom(argv
[i
]);
12081 memcpy(argv
[i
], z
, n
+1);
12082 argvToFree
[i
] = argv
[i
];
12085 sqlite3_shutdown();
12088 assert( argc
>=1 && argv
&& argv
[0] );
12091 #ifdef SQLITE_SHELL_DBNAME_PROC
12093 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12094 ** of a C-function that will provide the name of the database file. Use
12095 ** this compile-time option to embed this shell program in larger
12096 ** applications. */
12097 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12098 SQLITE_SHELL_DBNAME_PROC(&data
.pAuxDb
->zDbFilename
);
12099 warnInmemoryDb
= 0;
12103 /* Do an initial pass through the command-line argument to locate
12104 ** the name of the database file, the name of the initialization file,
12105 ** the size of the alternative malloc heap,
12106 ** and the first command to execute.
12108 #ifndef SQLITE_SHELL_FIDDLE
12109 verify_uninitialized();
12111 for(i
=1; i
<argc
; i
++){
12114 if( z
[0]!='-' || i
>nOptsEnd
){
12115 if( data
.aAuxDb
->zDbFilename
==0 ){
12116 data
.aAuxDb
->zDbFilename
= z
;
12118 /* Excesss arguments are interpreted as SQL (or dot-commands) and
12119 ** mean that nothing is read from stdin */
12122 azCmd
= realloc(azCmd
, sizeof(azCmd
[0])*nCmd
);
12123 shell_check_oom(azCmd
);
12128 if( z
[1]=='-' ) z
++;
12129 if( cli_strcmp(z
, "-")==0 ){
12132 }else if( cli_strcmp(z
,"-separator")==0
12133 || cli_strcmp(z
,"-nullvalue")==0
12134 || cli_strcmp(z
,"-newline")==0
12135 || cli_strcmp(z
,"-cmd")==0
12137 (void)cmdline_option_value(argc
, argv
, ++i
);
12138 }else if( cli_strcmp(z
,"-init")==0 ){
12139 zInitFile
= cmdline_option_value(argc
, argv
, ++i
);
12140 }else if( cli_strcmp(z
,"-batch")==0 ){
12141 /* Need to check for batch mode here to so we can avoid printing
12142 ** informational messages (like from process_sqliterc) before
12143 ** we do the actual processing of arguments later in a second pass.
12145 stdin_is_interactive
= 0;
12146 }else if( cli_strcmp(z
,"-heap")==0 ){
12147 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12149 sqlite3_int64 szHeap
;
12151 zSize
= cmdline_option_value(argc
, argv
, ++i
);
12152 szHeap
= integerValue(zSize
);
12153 if( szHeap
>0x7fff0000 ) szHeap
= 0x7fff0000;
12154 verify_uninitialized();
12155 sqlite3_config(SQLITE_CONFIG_HEAP
, malloc((int)szHeap
), (int)szHeap
, 64);
12157 (void)cmdline_option_value(argc
, argv
, ++i
);
12159 }else if( cli_strcmp(z
,"-pagecache")==0 ){
12160 sqlite3_int64 n
, sz
;
12161 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12162 if( sz
>70000 ) sz
= 70000;
12164 n
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12165 if( sz
>0 && n
>0 && 0xffffffffffffLL
/sz
<n
){
12166 n
= 0xffffffffffffLL
/sz
;
12168 verify_uninitialized();
12169 sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
12170 (n
>0 && sz
>0) ? malloc(n
*sz
) : 0, sz
, n
);
12171 data
.shellFlgs
|= SHFLG_Pagecache
;
12172 }else if( cli_strcmp(z
,"-lookaside")==0 ){
12174 sz
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12176 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12178 verify_uninitialized();
12179 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, n
);
12180 if( sz
*n
==0 ) data
.shellFlgs
&= ~SHFLG_Lookaside
;
12181 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
12183 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12184 verify_uninitialized();
12186 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD
); break;
12187 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD
); break;
12188 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED
); break;
12190 #ifdef SQLITE_ENABLE_VFSTRACE
12191 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
12192 extern int vfstrace_register(
12193 const char *zTraceName
,
12194 const char *zOldVfsName
,
12195 int (*xOut
)(const char*,void*),
12199 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs
,stderr
,1);
12201 #ifdef SQLITE_ENABLE_MULTIPLEX
12202 }else if( cli_strcmp(z
,"-multiplex")==0 ){
12203 extern int sqlite3_multiple_initialize(const char*,int);
12204 sqlite3_multiplex_initialize(0, 1);
12206 }else if( cli_strcmp(z
,"-mmap")==0 ){
12207 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12208 verify_uninitialized();
12209 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE
, sz
, sz
);
12210 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
12211 }else if( cli_strcmp(z
,"-sorterref")==0 ){
12212 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12213 verify_uninitialized();
12214 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE
, (int)sz
);
12216 }else if( cli_strcmp(z
,"-vfs")==0 ){
12217 zVfs
= cmdline_option_value(argc
, argv
, ++i
);
12218 #ifdef SQLITE_HAVE_ZLIB
12219 }else if( cli_strcmp(z
,"-zip")==0 ){
12220 data
.openMode
= SHELL_OPEN_ZIPFILE
;
12222 }else if( cli_strcmp(z
,"-append")==0 ){
12223 data
.openMode
= SHELL_OPEN_APPENDVFS
;
12224 #ifndef SQLITE_OMIT_DESERIALIZE
12225 }else if( cli_strcmp(z
,"-deserialize")==0 ){
12226 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
12227 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
12228 data
.szMax
= integerValue(argv
[++i
]);
12230 }else if( cli_strcmp(z
,"-readonly")==0 ){
12231 data
.openMode
= SHELL_OPEN_READONLY
;
12232 }else if( cli_strcmp(z
,"-nofollow")==0 ){
12233 data
.openFlags
= SQLITE_OPEN_NOFOLLOW
;
12234 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12235 }else if( cli_strncmp(z
, "-A",2)==0 ){
12236 /* All remaining command-line arguments are passed to the ".archive"
12237 ** command, so ignore them */
12240 }else if( cli_strcmp(z
, "-memtrace")==0 ){
12241 sqlite3MemTraceActivate(stderr
);
12242 }else if( cli_strcmp(z
,"-bail")==0 ){
12244 }else if( cli_strcmp(z
,"-nonce")==0 ){
12246 data
.zNonce
= strdup(argv
[++i
]);
12247 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
12248 ShellSetFlag(&data
,SHFLG_TestingMode
);
12249 }else if( cli_strcmp(z
,"-safe")==0 ){
12250 /* no-op - catch this on the second pass */
12253 #ifndef SQLITE_SHELL_FIDDLE
12254 verify_uninitialized();
12258 #ifdef SQLITE_SHELL_INIT_PROC
12260 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12261 ** of a C-function that will perform initialization actions on SQLite that
12262 ** occur just before or after sqlite3_initialize(). Use this compile-time
12263 ** option to embed this shell program in larger applications. */
12264 extern void SQLITE_SHELL_INIT_PROC(void);
12265 SQLITE_SHELL_INIT_PROC();
12268 /* All the sqlite3_config() calls have now been made. So it is safe
12269 ** to call sqlite3_initialize() and process any command line -vfs option. */
12270 sqlite3_initialize();
12274 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zVfs
);
12276 sqlite3_vfs_register(pVfs
, 1);
12278 utf8_printf(stderr
, "no such VFS: \"%s\"\n", zVfs
);
12283 if( data
.pAuxDb
->zDbFilename
==0 ){
12284 #ifndef SQLITE_OMIT_MEMORYDB
12285 data
.pAuxDb
->zDbFilename
= ":memory:";
12286 warnInmemoryDb
= argc
==1;
12288 utf8_printf(stderr
,"%s: Error: no database filename specified\n", Argv0
);
12293 #ifndef SQLITE_SHELL_FIDDLE
12294 sqlite3_appendvfs_init(0,0,0);
12297 /* Go ahead and open the database file if it already exists. If the
12298 ** file does not exist, delay opening it. This prevents empty database
12299 ** files from being created if a user mistypes the database name argument
12300 ** to the sqlite command-line tool.
12302 if( access(data
.pAuxDb
->zDbFilename
, 0)==0 ){
12306 /* Process the initialization file if there is one. If no -init option
12307 ** is given on the command line, look for a file named ~/.sqliterc and
12308 ** try to process it.
12310 process_sqliterc(&data
,zInitFile
);
12312 /* Make a second pass through the command-line argument and set
12313 ** options. This second pass is delayed until after the initialization
12314 ** file is processed so that the command-line arguments will override
12315 ** settings in the initialization file.
12317 for(i
=1; i
<argc
; i
++){
12319 if( z
[0]!='-' || i
>=nOptsEnd
) continue;
12320 if( z
[1]=='-' ){ z
++; }
12321 if( cli_strcmp(z
,"-init")==0 ){
12323 }else if( cli_strcmp(z
,"-html")==0 ){
12324 data
.mode
= MODE_Html
;
12325 }else if( cli_strcmp(z
,"-list")==0 ){
12326 data
.mode
= MODE_List
;
12327 }else if( cli_strcmp(z
,"-quote")==0 ){
12328 data
.mode
= MODE_Quote
;
12329 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
, SEP_Comma
);
12330 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
, SEP_Row
);
12331 }else if( cli_strcmp(z
,"-line")==0 ){
12332 data
.mode
= MODE_Line
;
12333 }else if( cli_strcmp(z
,"-column")==0 ){
12334 data
.mode
= MODE_Column
;
12335 }else if( cli_strcmp(z
,"-json")==0 ){
12336 data
.mode
= MODE_Json
;
12337 }else if( cli_strcmp(z
,"-markdown")==0 ){
12338 data
.mode
= MODE_Markdown
;
12339 }else if( cli_strcmp(z
,"-table")==0 ){
12340 data
.mode
= MODE_Table
;
12341 }else if( cli_strcmp(z
,"-box")==0 ){
12342 data
.mode
= MODE_Box
;
12343 }else if( cli_strcmp(z
,"-csv")==0 ){
12344 data
.mode
= MODE_Csv
;
12345 memcpy(data
.colSeparator
,",",2);
12346 #ifdef SQLITE_HAVE_ZLIB
12347 }else if( cli_strcmp(z
,"-zip")==0 ){
12348 data
.openMode
= SHELL_OPEN_ZIPFILE
;
12350 }else if( cli_strcmp(z
,"-append")==0 ){
12351 data
.openMode
= SHELL_OPEN_APPENDVFS
;
12352 #ifndef SQLITE_OMIT_DESERIALIZE
12353 }else if( cli_strcmp(z
,"-deserialize")==0 ){
12354 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
12355 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
12356 data
.szMax
= integerValue(argv
[++i
]);
12358 }else if( cli_strcmp(z
,"-readonly")==0 ){
12359 data
.openMode
= SHELL_OPEN_READONLY
;
12360 }else if( cli_strcmp(z
,"-nofollow")==0 ){
12361 data
.openFlags
|= SQLITE_OPEN_NOFOLLOW
;
12362 }else if( cli_strcmp(z
,"-ascii")==0 ){
12363 data
.mode
= MODE_Ascii
;
12364 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Unit
);
12365 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Record
);
12366 }else if( cli_strcmp(z
,"-tabs")==0 ){
12367 data
.mode
= MODE_List
;
12368 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Tab
);
12369 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Row
);
12370 }else if( cli_strcmp(z
,"-separator")==0 ){
12371 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,
12372 "%s",cmdline_option_value(argc
,argv
,++i
));
12373 }else if( cli_strcmp(z
,"-newline")==0 ){
12374 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,
12375 "%s",cmdline_option_value(argc
,argv
,++i
));
12376 }else if( cli_strcmp(z
,"-nullvalue")==0 ){
12377 sqlite3_snprintf(sizeof(data
.nullValue
), data
.nullValue
,
12378 "%s",cmdline_option_value(argc
,argv
,++i
));
12379 }else if( cli_strcmp(z
,"-header")==0 ){
12380 data
.showHeader
= 1;
12381 ShellSetFlag(&data
, SHFLG_HeaderSet
);
12382 }else if( cli_strcmp(z
,"-noheader")==0 ){
12383 data
.showHeader
= 0;
12384 ShellSetFlag(&data
, SHFLG_HeaderSet
);
12385 }else if( cli_strcmp(z
,"-echo")==0 ){
12386 ShellSetFlag(&data
, SHFLG_Echo
);
12387 }else if( cli_strcmp(z
,"-eqp")==0 ){
12388 data
.autoEQP
= AUTOEQP_on
;
12389 }else if( cli_strcmp(z
,"-eqpfull")==0 ){
12390 data
.autoEQP
= AUTOEQP_full
;
12391 }else if( cli_strcmp(z
,"-stats")==0 ){
12393 }else if( cli_strcmp(z
,"-scanstats")==0 ){
12394 data
.scanstatsOn
= 1;
12395 }else if( cli_strcmp(z
,"-backslash")==0 ){
12396 /* Undocumented command-line option: -backslash
12397 ** Causes C-style backslash escapes to be evaluated in SQL statements
12398 ** prior to sending the SQL into SQLite. Useful for injecting
12399 ** crazy bytes in the middle of SQL statements for testing and debugging.
12401 ShellSetFlag(&data
, SHFLG_Backslash
);
12402 }else if( cli_strcmp(z
,"-bail")==0 ){
12403 /* No-op. The bail_on_error flag should already be set. */
12404 }else if( cli_strcmp(z
,"-version")==0 ){
12405 /* BEGIN SQLCIPHER */
12406 #ifdef SQLITE_HAS_CODEC
12407 extern char* sqlcipher_version();
12408 char *sqlcipher_ver
= sqlcipher_version();
12409 printf("%s %s", sqlite3_libversion(), sqlite3_sourceid());
12410 printf(" (SQLCipher %s)\n", sqlcipher_ver
);
12411 sqlite3_free(sqlcipher_ver
);
12413 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
12415 /* END SQLCIPHER */
12417 }else if( cli_strcmp(z
,"-interactive")==0 ){
12418 stdin_is_interactive
= 1;
12419 }else if( cli_strcmp(z
,"-batch")==0 ){
12420 stdin_is_interactive
= 0;
12421 }else if( cli_strcmp(z
,"-utf8")==0 ){
12422 #if SHELL_WIN_UTF8_OPT
12424 #endif /* SHELL_WIN_UTF8_OPT */
12425 }else if( cli_strcmp(z
,"-heap")==0 ){
12427 }else if( cli_strcmp(z
,"-pagecache")==0 ){
12429 }else if( cli_strcmp(z
,"-lookaside")==0 ){
12431 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
12433 }else if( cli_strcmp(z
,"-nonce")==0 ){
12435 }else if( cli_strcmp(z
,"-mmap")==0 ){
12437 }else if( cli_strcmp(z
,"-memtrace")==0 ){
12439 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
12440 }else if( cli_strcmp(z
,"-sorterref")==0 ){
12443 }else if( cli_strcmp(z
,"-vfs")==0 ){
12445 #ifdef SQLITE_ENABLE_VFSTRACE
12446 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
12449 #ifdef SQLITE_ENABLE_MULTIPLEX
12450 }else if( cli_strcmp(z
,"-multiplex")==0 ){
12453 }else if( cli_strcmp(z
,"-help")==0 ){
12455 }else if( cli_strcmp(z
,"-cmd")==0 ){
12456 /* Run commands that follow -cmd first and separately from commands
12457 ** that simply appear on the command-line. This seems goofy. It would
12458 ** be better if all commands ran in the order that they appear. But
12459 ** we retain the goofy behavior for historical compatibility. */
12460 if( i
==argc
-1 ) break;
12461 z
= cmdline_option_value(argc
,argv
,++i
);
12463 rc
= do_meta_command(z
, &data
);
12464 if( rc
&& bail_on_error
) return rc
==2 ? 0 : rc
;
12467 rc
= shell_exec(&data
, z
, &zErrMsg
);
12469 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
12470 if( bail_on_error
) return rc
!=0 ? rc
: 1;
12472 utf8_printf(stderr
,"Error: unable to process SQL \"%s\"\n", z
);
12473 if( bail_on_error
) return rc
;
12476 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12477 }else if( cli_strncmp(z
, "-A", 2)==0 ){
12479 utf8_printf(stderr
, "Error: cannot mix regular SQL or dot-commands"
12480 " with \"%s\"\n", z
);
12483 open_db(&data
, OPEN_DB_ZIPFILE
);
12486 arDotCommand(&data
, 1, argv
+(i
-1), argc
-(i
-1));
12488 arDotCommand(&data
, 1, argv
+i
, argc
-i
);
12493 }else if( cli_strcmp(z
,"-safe")==0 ){
12494 data
.bSafeMode
= data
.bSafeModePersist
= 1;
12495 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
12496 /* Acted upon in first pass. */
12498 utf8_printf(stderr
,"%s: Error: unknown option: %s\n", Argv0
, z
);
12499 raw_printf(stderr
,"Use -help for a list of options.\n");
12502 data
.cMode
= data
.mode
;
12504 #if SHELL_WIN_UTF8_OPT
12505 if( console_utf8
&& stdin_is_interactive
){
12508 setBinaryMode(stdin
, 0);
12514 /* Run all arguments that do not begin with '-' as if they were separate
12515 ** command-line inputs, except for the argToSkip argument which contains
12516 ** the database filename.
12518 for(i
=0; i
<nCmd
; i
++){
12519 if( azCmd
[i
][0]=='.' ){
12520 rc
= do_meta_command(azCmd
[i
], &data
);
12523 return rc
==2 ? 0 : rc
;
12527 echo_group_input(&data
, azCmd
[i
]);
12528 rc
= shell_exec(&data
, azCmd
[i
], &zErrMsg
);
12529 if( zErrMsg
|| rc
){
12531 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
12533 utf8_printf(stderr
,"Error: unable to process SQL: %s\n", azCmd
[i
]);
12535 sqlite3_free(zErrMsg
);
12537 return rc
!=0 ? rc
: 1;
12542 /* Run commands received from standard input
12544 if( stdin_is_interactive
){
12548 /* BEGIN SQLCIPHER */
12549 #ifdef SQLITE_HAS_CODEC
12550 extern char* sqlcipher_version();
12551 char *sqlcipher_ver
= sqlcipher_version();
12553 "SQLite version %s %.19s" /*extra-version-info*/
12554 " (SQLCipher %s)\n" /*sqlcipher version info*/
12555 "Enter \".help\" for usage hints.\n",
12556 sqlite3_libversion(), sqlite3_sourceid(), sqlcipher_ver
12558 sqlite3_free(sqlcipher_ver
);
12561 "SQLite version %s %.19s\n" /*extra-version-info*/
12562 "Enter \".help\" for usage hints.\n",
12563 sqlite3_libversion(), sqlite3_sourceid()
12566 /* END SQLCIPHER */
12567 if( warnInmemoryDb
){
12568 printf("Connected to a ");
12569 printBold("transient in-memory database");
12570 printf(".\nUse \".open FILENAME\" to reopen on a "
12571 "persistent database.\n");
12573 zHistory
= getenv("SQLITE_HISTORY");
12575 zHistory
= strdup(zHistory
);
12576 }else if( (zHome
= find_home_dir(0))!=0 ){
12577 nHistory
= strlen30(zHome
) + 20;
12578 if( (zHistory
= malloc(nHistory
))!=0 ){
12579 sqlite3_snprintf(nHistory
, zHistory
,"%s/.sqlite_history", zHome
);
12582 if( zHistory
){ shell_read_history(zHistory
); }
12583 #if HAVE_READLINE || HAVE_EDITLINE
12584 rl_attempted_completion_function
= readline_completion
;
12585 #elif HAVE_LINENOISE
12586 linenoiseSetCompletionCallback(linenoise_completion
);
12589 rc
= process_input(&data
);
12591 shell_stifle_history(2000);
12592 shell_write_history(zHistory
);
12597 rc
= process_input(&data
);
12600 #ifndef SQLITE_SHELL_FIDDLE
12601 /* In WASM mode we have to leave the db state in place so that
12602 ** client code can "push" SQL into it after this call returns. */
12604 set_table_name(&data
, 0);
12606 session_close_all(&data
, -1);
12609 for(i
=0; i
<ArraySize(data
.aAuxDb
); i
++){
12610 sqlite3_free(data
.aAuxDb
[i
].zFreeOnClose
);
12611 if( data
.aAuxDb
[i
].db
){
12612 session_close_all(&data
, i
);
12613 close_db(data
.aAuxDb
[i
].db
);
12617 output_reset(&data
);
12618 data
.doXdgOpen
= 0;
12619 clearTempFile(&data
);
12620 #if !SQLITE_SHELL_IS_UTF8
12621 for(i
=0; i
<argcToFree
; i
++) free(argvToFree
[i
]);
12624 free(data
.colWidth
);
12626 /* Clear the global data structure so that valgrind will detect memory
12628 memset(&data
, 0, sizeof(data
));
12629 #ifdef SQLITE_DEBUG
12630 if( sqlite3_memory_used()>mem_main_enter
){
12631 utf8_printf(stderr
, "Memory leaked: %u bytes\n",
12632 (unsigned int)(sqlite3_memory_used()-mem_main_enter
));
12635 #endif /* !SQLITE_SHELL_FIDDLE */
12640 #ifdef SQLITE_SHELL_FIDDLE
12641 /* Only for emcc experimentation purposes. */
12642 int fiddle_experiment(int a
,int b
){
12647 ** Returns a pointer to the current DB handle.
12649 sqlite3
* fiddle_db_handle(){
12654 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
12655 ** "main" is assumed. Returns 0 if no db with the given name is
12658 sqlite3_vfs
* fiddle_db_vfs(const char *zDbName
){
12659 sqlite3_vfs
* pVfs
= 0;
12661 sqlite3_file_control(globalDb
, zDbName
? zDbName
: "main",
12662 SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
12667 /* Only for emcc experimentation purposes. */
12668 sqlite3
* fiddle_db_arg(sqlite3
*arg
){
12669 printf("fiddle_db_arg(%p)\n", (const void*)arg
);
12674 ** Intended to be called via a SharedWorker() while a separate
12675 ** SharedWorker() (which manages the wasm module) is performing work
12676 ** which should be interrupted. Unfortunately, SharedWorker is not
12677 ** portable enough to make real use of.
12679 void fiddle_interrupt(void){
12680 if( globalDb
) sqlite3_interrupt(globalDb
);
12684 ** Returns the filename of the given db name, assuming "main" if
12685 ** zDbName is NULL. Returns NULL if globalDb is not opened.
12687 const char * fiddle_db_filename(const char * zDbName
){
12689 ? sqlite3_db_filename(globalDb
, zDbName
? zDbName
: "main")
12694 ** Completely wipes out the contents of the currently-opened database
12695 ** but leaves its storage intact for reuse.
12697 void fiddle_reset_db(void){
12699 int rc
= sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 1, 0);
12700 if( 0==rc
) rc
= sqlite3_exec(globalDb
, "VACUUM", 0, 0, 0);
12701 sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 0, 0);
12706 ** Uses the current database's VFS xRead to stream the db file's
12707 ** contents out to the given callback. The callback gets a single
12708 ** chunk of size n (its 2nd argument) on each call and must return 0
12709 ** on success, non-0 on error. This function returns 0 on success,
12710 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
12711 ** code from the callback. Note that this is not thread-friendly: it
12712 ** expects that it will be the only thread reading the db file and
12713 ** takes no measures to ensure that is the case.
12715 int fiddle_export_db( int (*xCallback
)(unsigned const char *zOut
, int n
) ){
12716 sqlite3_int64 nSize
= 0;
12717 sqlite3_int64 nPos
= 0;
12718 sqlite3_file
* pFile
= 0;
12719 unsigned char buf
[1024 * 8];
12720 int nBuf
= (int)sizeof(buf
);
12721 int rc
= shellState
.db
12722 ? sqlite3_file_control(shellState
.db
, "main",
12723 SQLITE_FCNTL_FILE_POINTER
, &pFile
)
12725 if( rc
) return rc
;
12726 rc
= pFile
->pMethods
->xFileSize(pFile
, &nSize
);
12727 if( rc
) return rc
;
12729 /* DB size is not an even multiple of the buffer size. Reduce
12730 ** buffer size so that we do not unduly inflate the db size when
12732 if(0 == nSize
% 4096) nBuf
= 4096;
12733 else if(0 == nSize
% 2048) nBuf
= 2048;
12734 else if(0 == nSize
% 1024) nBuf
= 1024;
12737 for( ; 0==rc
&& nPos
<nSize
; nPos
+= nBuf
){
12738 rc
= pFile
->pMethods
->xRead(pFile
, buf
, nBuf
, nPos
);
12739 if(SQLITE_IOERR_SHORT_READ
== rc
){
12740 rc
= (nPos
+ nBuf
) < nSize
? rc
: 0/*assume EOF*/;
12742 if( 0==rc
) rc
= xCallback(buf
, nBuf
);
12748 ** Trivial exportable function for emscripten. It processes zSql as if
12749 ** it were input to the sqlite3 shell and redirects all output to the
12750 ** wasm binding. fiddle_main() must have been called before this
12751 ** is called, or results are undefined.
12753 void fiddle_exec(const char * zSql
){
12755 if('.'==*zSql
) puts(zSql
);
12756 shellState
.wasm
.zInput
= zSql
;
12757 shellState
.wasm
.zPos
= zSql
;
12758 process_input(&shellState
);
12759 shellState
.wasm
.zInput
= shellState
.wasm
.zPos
= 0;
12762 #endif /* SQLITE_SHELL_FIDDLE */