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 #undef WIN32_LEAN_AND_MEAN
232 #define WIN32_LEAN_AND_MEAN
235 /* string conversion routines only needed on Win32 */
236 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR
);
237 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char *zText
);
240 /* Use console I/O package as a direct INCLUDE. */
241 #define SQLITE_INTERNAL_LINKAGE static
243 #ifdef SQLITE_SHELL_FIDDLE
244 /* Deselect most features from the console I/O package for Fiddle. */
245 # define SQLITE_CIO_NO_REDIRECT
246 # define SQLITE_CIO_NO_CLASSIFY
247 # define SQLITE_CIO_NO_TRANSLATE
248 # define SQLITE_CIO_NO_SETMODE
249 # define SQLITE_CIO_NO_FLUSH
251 INCLUDE
../ext
/consio
/console_io
.h
252 INCLUDE
../ext
/consio
/console_io
.c
254 #ifndef SQLITE_SHELL_FIDDLE
256 /* From here onward, fgets() is redirected to the console_io library. */
257 # define fgets(b,n,f) fGetsUtf8(b,n,f)
259 * Define macros for emitting output text in various ways:
260 * sputz(s, z) => emit 0-terminated string z to given stream s
261 * sputf(s, f, ...) => emit varargs per format f to given stream s
262 * oputz(z) => emit 0-terminated string z to default stream
263 * oputf(f, ...) => emit varargs per format f to default stream
264 * eputz(z) => emit 0-terminated string z to error stream
265 * eputf(f, ...) => emit varargs per format f to error stream
266 * oputb(b, n) => emit char buffer b[0..n-1] to default stream
268 * Note that the default stream is whatever has been last set via:
269 * setOutputStream(FILE *pf)
270 * This is normally the stream that CLI normal output goes to.
271 * For the stand-alone CLI, it is stdout with no .output redirect.
273 * The ?putz(z) forms are required for the Fiddle builds for string literal
274 * output, in aid of enforcing format string to argument correspondence.
276 # define sputz(s,z) fPutsUtf8(z,s)
277 # define sputf fPrintfUtf8
278 # define oputz(z) oPutsUtf8(z)
279 # define oputf oPrintfUtf8
280 # define eputz(z) ePutsUtf8(z)
281 # define eputf ePrintfUtf8
282 # define oputb(buf,na) oPutbUtf8(buf,na)
283 # define fflush(s) fFlushBuffer(s);
286 /* For Fiddle, all console handling and emit redirection is omitted. */
287 /* These next 3 macros are for emitting formatted output. When complaints
288 * from the WASM build are issued for non-formatted output, when a mere
289 * string literal is to be emitted, the ?putz(z) forms should be used.
290 * (This permits compile-time checking of format string / argument mismatch.)
292 # define oputf(fmt, ...) printf(fmt,__VA_ARGS__)
293 # define eputf(fmt, ...) fprintf(stderr,fmt,__VA_ARGS__)
294 # define sputf(fp,fmt, ...) fprintf(fp,fmt,__VA_ARGS__)
295 /* These next 3 macros are for emitting simple string literals. */
296 # define oputz(z) fputs(z,stdout)
297 # define eputz(z) fputs(z,stderr)
298 # define sputz(fp,z) fputs(z,fp)
299 # define oputb(buf,na) fwrite(buf,1,na,stdout)
303 /* True if the timer is enabled */
304 static int enableTimer
= 0;
306 /* A version of strcmp() that works with NULL values */
307 static int cli_strcmp(const char *a
, const char *b
){
312 static int cli_strncmp(const char *a
, const char *b
, size_t n
){
315 return strncmp(a
,b
,n
);
318 /* Return the current wall-clock time */
319 static sqlite3_int64
timeOfDay(void){
320 static sqlite3_vfs
*clockVfs
= 0;
322 if( clockVfs
==0 ) clockVfs
= sqlite3_vfs_find(0);
323 if( clockVfs
==0 ) return 0; /* Never actually happens */
324 if( clockVfs
->iVersion
>=2 && clockVfs
->xCurrentTimeInt64
!=0 ){
325 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
328 clockVfs
->xCurrentTime(clockVfs
, &r
);
329 t
= (sqlite3_int64
)(r
*86400000.0);
334 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
335 #include <sys/time.h>
336 #include <sys/resource.h>
338 /* VxWorks does not support getrusage() as far as we can determine */
339 #if defined(_WRS_KERNEL) || defined(__RTP__)
341 struct timeval ru_utime
; /* user CPU time used */
342 struct timeval ru_stime
; /* system CPU time used */
344 #define getrusage(A,B) memset(B,0,sizeof(*B))
347 /* Saved resource information for the beginning of an operation */
348 static struct rusage sBegin
; /* CPU time at start */
349 static sqlite3_int64 iBegin
; /* Wall-clock time at start */
352 ** Begin timing an operation
354 static void beginTimer(void){
356 getrusage(RUSAGE_SELF
, &sBegin
);
357 iBegin
= timeOfDay();
361 /* Return the difference of two time_structs in seconds */
362 static double timeDiff(struct timeval
*pStart
, struct timeval
*pEnd
){
363 return (pEnd
->tv_usec
- pStart
->tv_usec
)*0.000001 +
364 (double)(pEnd
->tv_sec
- pStart
->tv_sec
);
368 ** Print the timing results.
370 static void endTimer(void){
372 sqlite3_int64 iEnd
= timeOfDay();
374 getrusage(RUSAGE_SELF
, &sEnd
);
375 sputf(stdout
, "Run Time: real %.3f user %f sys %f\n",
376 (iEnd
- iBegin
)*0.001,
377 timeDiff(&sBegin
.ru_utime
, &sEnd
.ru_utime
),
378 timeDiff(&sBegin
.ru_stime
, &sEnd
.ru_stime
));
382 #define BEGIN_TIMER beginTimer()
383 #define END_TIMER endTimer()
386 #elif (defined(_WIN32) || defined(WIN32))
388 /* Saved resource information for the beginning of an operation */
389 static HANDLE hProcess
;
390 static FILETIME ftKernelBegin
;
391 static FILETIME ftUserBegin
;
392 static sqlite3_int64 ftWallBegin
;
393 typedef BOOL (WINAPI
*GETPROCTIMES
)(HANDLE
, LPFILETIME
, LPFILETIME
,
394 LPFILETIME
, LPFILETIME
);
395 static GETPROCTIMES getProcessTimesAddr
= NULL
;
398 ** Check to see if we have timer support. Return 1 if necessary
399 ** support found (or found previously).
401 static int hasTimer(void){
402 if( getProcessTimesAddr
){
406 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
407 ** versions. See if the version we are running on has it, and if it
408 ** does, save off a pointer to it and the current process handle.
410 hProcess
= GetCurrentProcess();
412 HINSTANCE hinstLib
= LoadLibrary(TEXT("Kernel32.dll"));
413 if( NULL
!= hinstLib
){
414 getProcessTimesAddr
=
415 (GETPROCTIMES
) GetProcAddress(hinstLib
, "GetProcessTimes");
416 if( NULL
!= getProcessTimesAddr
){
419 FreeLibrary(hinstLib
);
428 ** Begin timing an operation
430 static void beginTimer(void){
431 if( enableTimer
&& getProcessTimesAddr
){
432 FILETIME ftCreation
, ftExit
;
433 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,
434 &ftKernelBegin
,&ftUserBegin
);
435 ftWallBegin
= timeOfDay();
439 /* Return the difference of two FILETIME structs in seconds */
440 static double timeDiff(FILETIME
*pStart
, FILETIME
*pEnd
){
441 sqlite_int64 i64Start
= *((sqlite_int64
*) pStart
);
442 sqlite_int64 i64End
= *((sqlite_int64
*) pEnd
);
443 return (double) ((i64End
- i64Start
) / 10000000.0);
447 ** Print the timing results.
449 static void endTimer(void){
450 if( enableTimer
&& getProcessTimesAddr
){
451 FILETIME ftCreation
, ftExit
, ftKernelEnd
, ftUserEnd
;
452 sqlite3_int64 ftWallEnd
= timeOfDay();
453 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,&ftKernelEnd
,&ftUserEnd
);
454 sputf(stdout
, "Run Time: real %.3f user %f sys %f\n",
455 (ftWallEnd
- ftWallBegin
)*0.001,
456 timeDiff(&ftUserBegin
, &ftUserEnd
),
457 timeDiff(&ftKernelBegin
, &ftKernelEnd
));
461 #define BEGIN_TIMER beginTimer()
462 #define END_TIMER endTimer()
463 #define HAS_TIMER hasTimer()
472 ** Used to prevent warnings about unused parameters
474 #define UNUSED_PARAMETER(x) (void)(x)
477 ** Number of elements in an array
479 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
482 ** If the following flag is set, then command execution stops
483 ** at an error if we are not interactive.
485 static int bail_on_error
= 0;
488 ** Treat stdin as an interactive input if the following variable
489 ** is true. Otherwise, assume stdin is connected to a file or pipe.
491 static int stdin_is_interactive
= 1;
494 ** On Windows systems we need to know if standard output is a console
495 ** in order to show that UTF-16 translation is done in the sign-on
496 ** banner. The following variable is true if it is the console.
498 static int stdout_is_console
= 1;
501 ** The following is the open SQLite database. We make a pointer
502 ** to this database a static variable so that it can be accessed
503 ** by the SIGINT handler to interrupt database processing.
505 static sqlite3
*globalDb
= 0;
508 ** True if an interrupt (Control-C) has been received.
510 static volatile int seenInterrupt
= 0;
513 ** This is the name of our program. It is set in main(), used
514 ** in a number of other places, mostly for error messages.
519 ** Prompt strings. Initialized in main. Settable with
520 ** .prompt main continue
522 #define PROMPT_LEN_MAX 20
523 /* First line prompt. default: "sqlite> " */
524 static char mainPrompt
[PROMPT_LEN_MAX
];
525 /* Continuation prompt. default: " ...> " */
526 static char continuePrompt
[PROMPT_LEN_MAX
];
528 /* This is variant of the standard-library strncpy() routine with the
529 ** one change that the destination string is always zero-terminated, even
530 ** if there is no zero-terminator in the first n-1 characters of the source
533 static char *shell_strncpy(char *dest
, const char *src
, size_t n
){
535 for(i
=0; i
<n
-1 && src
[i
]!=0; i
++) dest
[i
] = src
[i
];
541 ** strcpy() workalike to squelch an unwarranted link-time warning
544 static void shell_strcpy(char *dest
, const char *src
){
545 while( (*(dest
++) = *(src
++))!=0 ){}
549 ** Optionally disable dynamic continuation prompt.
550 ** Unless disabled, the continuation prompt shows open SQL lexemes if any,
551 ** or open parentheses level if non-zero, or continuation prompt as set.
552 ** This facility interacts with the scanner and process_input() where the
553 ** below 5 macros are used.
555 #ifdef SQLITE_OMIT_DYNAPROMPT
556 # define CONTINUATION_PROMPT continuePrompt
557 # define CONTINUE_PROMPT_RESET
558 # define CONTINUE_PROMPT_AWAITS(p,s)
559 # define CONTINUE_PROMPT_AWAITC(p,c)
560 # define CONTINUE_PAREN_INCR(p,n)
561 # define CONTINUE_PROMPT_PSTATE 0
562 typedef void *t_NoDynaPrompt
;
563 # define SCAN_TRACKER_REFTYPE t_NoDynaPrompt
565 # define CONTINUATION_PROMPT dynamicContinuePrompt()
566 # define CONTINUE_PROMPT_RESET \
567 do {setLexemeOpen(&dynPrompt,0,0); trackParenLevel(&dynPrompt,0);} while(0)
568 # define CONTINUE_PROMPT_AWAITS(p,s) \
569 if(p && stdin_is_interactive) setLexemeOpen(p, s, 0)
570 # define CONTINUE_PROMPT_AWAITC(p,c) \
571 if(p && stdin_is_interactive) setLexemeOpen(p, 0, c)
572 # define CONTINUE_PAREN_INCR(p,n) \
573 if(p && stdin_is_interactive) (trackParenLevel(p,n))
574 # define CONTINUE_PROMPT_PSTATE (&dynPrompt)
575 typedef struct DynaPrompt
*t_DynaPromptRef
;
576 # define SCAN_TRACKER_REFTYPE t_DynaPromptRef
578 static struct DynaPrompt
{
579 char dynamicPrompt
[PROMPT_LEN_MAX
];
582 char *zScannerAwaits
;
583 } dynPrompt
= { {0}, {0}, 0, 0 };
585 /* Record parenthesis nesting level change, or force level to 0. */
586 static void trackParenLevel(struct DynaPrompt
*p
, int ni
){
587 p
->inParenLevel
+= ni
;
588 if( ni
==0 ) p
->inParenLevel
= 0;
589 p
->zScannerAwaits
= 0;
592 /* Record that a lexeme is opened, or closed with args==0. */
593 static void setLexemeOpen(struct DynaPrompt
*p
, char *s
, char c
){
595 p
->zScannerAwaits
= s
;
599 p
->zScannerAwaits
= p
->acAwait
;
603 /* Upon demand, derive the continuation prompt to display. */
604 static char *dynamicContinuePrompt(void){
605 if( continuePrompt
[0]==0
606 || (dynPrompt
.zScannerAwaits
==0 && dynPrompt
.inParenLevel
== 0) ){
607 return continuePrompt
;
609 if( dynPrompt
.zScannerAwaits
){
610 size_t ncp
= strlen(continuePrompt
);
611 size_t ndp
= strlen(dynPrompt
.zScannerAwaits
);
612 if( ndp
> ncp
-3 ) return continuePrompt
;
613 shell_strcpy(dynPrompt
.dynamicPrompt
, dynPrompt
.zScannerAwaits
);
614 while( ndp
<3 ) dynPrompt
.dynamicPrompt
[ndp
++] = ' ';
615 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3,
618 if( dynPrompt
.inParenLevel
>9 ){
619 shell_strncpy(dynPrompt
.dynamicPrompt
, "(..", 4);
620 }else if( dynPrompt
.inParenLevel
<0 ){
621 shell_strncpy(dynPrompt
.dynamicPrompt
, ")x!", 4);
623 shell_strncpy(dynPrompt
.dynamicPrompt
, "(x.", 4);
624 dynPrompt
.dynamicPrompt
[2] = (char)('0'+dynPrompt
.inParenLevel
);
626 shell_strncpy(dynPrompt
.dynamicPrompt
+3, continuePrompt
+3,
630 return dynPrompt
.dynamicPrompt
;
632 #endif /* !defined(SQLITE_OMIT_DYNAPROMPT) */
634 /* Indicate out-of-memory and exit. */
635 static void shell_out_of_memory(void){
636 eputz("Error: out of memory\n");
640 /* Check a pointer to see if it is NULL. If it is NULL, exit with an
641 ** out-of-memory error.
643 static void shell_check_oom(const void *p
){
644 if( p
==0 ) shell_out_of_memory();
648 ** Write I/O traces to the following stream.
650 #ifdef SQLITE_ENABLE_IOTRACE
651 static FILE *iotrace
= 0;
655 ** This routine works like printf in that its first argument is a
656 ** format string and subsequent arguments are values to be substituted
657 ** in place of % fields. The result of formatting this string
658 ** is written to iotrace.
660 #ifdef SQLITE_ENABLE_IOTRACE
661 static void SQLITE_CDECL
iotracePrintf(const char *zFormat
, ...){
664 if( iotrace
==0 ) return;
665 va_start(ap
, zFormat
);
666 z
= sqlite3_vmprintf(zFormat
, ap
);
668 sputf(iotrace
, "%s", z
);
673 /* Lookup table to determine the number of columns consumed by a Unicode
676 static const struct {
677 unsigned char w
; /* Width of the character in columns */
678 int iFirst
; /* First character in a span having this width */
680 /* {0, 0x00000}, {1, 0x00020}, {0, 0x0007f}, {1, 0x000a0}, */
681 {0, 0x00300}, {1, 0x00370}, {0, 0x00483}, {1, 0x00487}, {0, 0x00488},
682 {1, 0x0048a}, {0, 0x00591}, {1, 0x005be}, {0, 0x005bf}, {1, 0x005c0},
683 {0, 0x005c1}, {1, 0x005c3}, {0, 0x005c4}, {1, 0x005c6}, {0, 0x005c7},
684 {1, 0x005c8}, {0, 0x00600}, {1, 0x00604}, {0, 0x00610}, {1, 0x00616},
685 {0, 0x0064b}, {1, 0x0065f}, {0, 0x00670}, {1, 0x00671}, {0, 0x006d6},
686 {1, 0x006e5}, {0, 0x006e7}, {1, 0x006e9}, {0, 0x006ea}, {1, 0x006ee},
687 {0, 0x0070f}, {1, 0x00710}, {0, 0x00711}, {1, 0x00712}, {0, 0x00730},
688 {1, 0x0074b}, {0, 0x007a6}, {1, 0x007b1}, {0, 0x007eb}, {1, 0x007f4},
689 {0, 0x00901}, {1, 0x00903}, {0, 0x0093c}, {1, 0x0093d}, {0, 0x00941},
690 {1, 0x00949}, {0, 0x0094d}, {1, 0x0094e}, {0, 0x00951}, {1, 0x00955},
691 {0, 0x00962}, {1, 0x00964}, {0, 0x00981}, {1, 0x00982}, {0, 0x009bc},
692 {1, 0x009bd}, {0, 0x009c1}, {1, 0x009c5}, {0, 0x009cd}, {1, 0x009ce},
693 {0, 0x009e2}, {1, 0x009e4}, {0, 0x00a01}, {1, 0x00a03}, {0, 0x00a3c},
694 {1, 0x00a3d}, {0, 0x00a41}, {1, 0x00a43}, {0, 0x00a47}, {1, 0x00a49},
695 {0, 0x00a4b}, {1, 0x00a4e}, {0, 0x00a70}, {1, 0x00a72}, {0, 0x00a81},
696 {1, 0x00a83}, {0, 0x00abc}, {1, 0x00abd}, {0, 0x00ac1}, {1, 0x00ac6},
697 {0, 0x00ac7}, {1, 0x00ac9}, {0, 0x00acd}, {1, 0x00ace}, {0, 0x00ae2},
698 {1, 0x00ae4}, {0, 0x00b01}, {1, 0x00b02}, {0, 0x00b3c}, {1, 0x00b3d},
699 {0, 0x00b3f}, {1, 0x00b40}, {0, 0x00b41}, {1, 0x00b44}, {0, 0x00b4d},
700 {1, 0x00b4e}, {0, 0x00b56}, {1, 0x00b57}, {0, 0x00b82}, {1, 0x00b83},
701 {0, 0x00bc0}, {1, 0x00bc1}, {0, 0x00bcd}, {1, 0x00bce}, {0, 0x00c3e},
702 {1, 0x00c41}, {0, 0x00c46}, {1, 0x00c49}, {0, 0x00c4a}, {1, 0x00c4e},
703 {0, 0x00c55}, {1, 0x00c57}, {0, 0x00cbc}, {1, 0x00cbd}, {0, 0x00cbf},
704 {1, 0x00cc0}, {0, 0x00cc6}, {1, 0x00cc7}, {0, 0x00ccc}, {1, 0x00cce},
705 {0, 0x00ce2}, {1, 0x00ce4}, {0, 0x00d41}, {1, 0x00d44}, {0, 0x00d4d},
706 {1, 0x00d4e}, {0, 0x00dca}, {1, 0x00dcb}, {0, 0x00dd2}, {1, 0x00dd5},
707 {0, 0x00dd6}, {1, 0x00dd7}, {0, 0x00e31}, {1, 0x00e32}, {0, 0x00e34},
708 {1, 0x00e3b}, {0, 0x00e47}, {1, 0x00e4f}, {0, 0x00eb1}, {1, 0x00eb2},
709 {0, 0x00eb4}, {1, 0x00eba}, {0, 0x00ebb}, {1, 0x00ebd}, {0, 0x00ec8},
710 {1, 0x00ece}, {0, 0x00f18}, {1, 0x00f1a}, {0, 0x00f35}, {1, 0x00f36},
711 {0, 0x00f37}, {1, 0x00f38}, {0, 0x00f39}, {1, 0x00f3a}, {0, 0x00f71},
712 {1, 0x00f7f}, {0, 0x00f80}, {1, 0x00f85}, {0, 0x00f86}, {1, 0x00f88},
713 {0, 0x00f90}, {1, 0x00f98}, {0, 0x00f99}, {1, 0x00fbd}, {0, 0x00fc6},
714 {1, 0x00fc7}, {0, 0x0102d}, {1, 0x01031}, {0, 0x01032}, {1, 0x01033},
715 {0, 0x01036}, {1, 0x01038}, {0, 0x01039}, {1, 0x0103a}, {0, 0x01058},
716 {1, 0x0105a}, {2, 0x01100}, {0, 0x01160}, {1, 0x01200}, {0, 0x0135f},
717 {1, 0x01360}, {0, 0x01712}, {1, 0x01715}, {0, 0x01732}, {1, 0x01735},
718 {0, 0x01752}, {1, 0x01754}, {0, 0x01772}, {1, 0x01774}, {0, 0x017b4},
719 {1, 0x017b6}, {0, 0x017b7}, {1, 0x017be}, {0, 0x017c6}, {1, 0x017c7},
720 {0, 0x017c9}, {1, 0x017d4}, {0, 0x017dd}, {1, 0x017de}, {0, 0x0180b},
721 {1, 0x0180e}, {0, 0x018a9}, {1, 0x018aa}, {0, 0x01920}, {1, 0x01923},
722 {0, 0x01927}, {1, 0x01929}, {0, 0x01932}, {1, 0x01933}, {0, 0x01939},
723 {1, 0x0193c}, {0, 0x01a17}, {1, 0x01a19}, {0, 0x01b00}, {1, 0x01b04},
724 {0, 0x01b34}, {1, 0x01b35}, {0, 0x01b36}, {1, 0x01b3b}, {0, 0x01b3c},
725 {1, 0x01b3d}, {0, 0x01b42}, {1, 0x01b43}, {0, 0x01b6b}, {1, 0x01b74},
726 {0, 0x01dc0}, {1, 0x01dcb}, {0, 0x01dfe}, {1, 0x01e00}, {0, 0x0200b},
727 {1, 0x02010}, {0, 0x0202a}, {1, 0x0202f}, {0, 0x02060}, {1, 0x02064},
728 {0, 0x0206a}, {1, 0x02070}, {0, 0x020d0}, {1, 0x020f0}, {2, 0x02329},
729 {1, 0x0232b}, {2, 0x02e80}, {0, 0x0302a}, {2, 0x03030}, {1, 0x0303f},
730 {2, 0x03040}, {0, 0x03099}, {2, 0x0309b}, {1, 0x0a4d0}, {0, 0x0a806},
731 {1, 0x0a807}, {0, 0x0a80b}, {1, 0x0a80c}, {0, 0x0a825}, {1, 0x0a827},
732 {2, 0x0ac00}, {1, 0x0d7a4}, {2, 0x0f900}, {1, 0x0fb00}, {0, 0x0fb1e},
733 {1, 0x0fb1f}, {0, 0x0fe00}, {2, 0x0fe10}, {1, 0x0fe1a}, {0, 0x0fe20},
734 {1, 0x0fe24}, {2, 0x0fe30}, {1, 0x0fe70}, {0, 0x0feff}, {2, 0x0ff00},
735 {1, 0x0ff61}, {2, 0x0ffe0}, {1, 0x0ffe7}, {0, 0x0fff9}, {1, 0x0fffc},
736 {0, 0x10a01}, {1, 0x10a04}, {0, 0x10a05}, {1, 0x10a07}, {0, 0x10a0c},
737 {1, 0x10a10}, {0, 0x10a38}, {1, 0x10a3b}, {0, 0x10a3f}, {1, 0x10a40},
738 {0, 0x1d167}, {1, 0x1d16a}, {0, 0x1d173}, {1, 0x1d183}, {0, 0x1d185},
739 {1, 0x1d18c}, {0, 0x1d1aa}, {1, 0x1d1ae}, {0, 0x1d242}, {1, 0x1d245},
740 {2, 0x20000}, {1, 0x2fffe}, {2, 0x30000}, {1, 0x3fffe}, {0, 0xe0001},
741 {1, 0xe0002}, {0, 0xe0020}, {1, 0xe0080}, {0, 0xe0100}, {1, 0xe01f0}
745 ** Return the width, in columns, of the single Unicode character c.
746 ** For normal characters, the answer is always 1. But it might be 0 or 2
747 ** for zero-width and double-width characters.
749 int cli_wcwidth(int c
){
752 /* Fast path for common characters */
753 if( c
<0x20 ) return 0;
754 if( c
<0x7f ) return 1;
755 if( c
<0xa0 ) return 0;
756 if( c
<=0x300 ) return 1;
758 /* The general case */
760 iLast
= sizeof(aUWidth
)/sizeof(aUWidth
[0]) - 1;
761 while( iFirst
<iLast
-1 ){
762 int iMid
= (iFirst
+iLast
)/2;
763 int cMid
= aUWidth
[iMid
].iFirst
;
766 }else if( cMid
> c
){
769 return aUWidth
[iMid
].w
;
772 if( aUWidth
[iLast
].iFirst
> c
) return aUWidth
[iFirst
].w
;
773 return aUWidth
[iLast
].w
;
777 ** Compute the value and length of a multi-byte UTF-8 character that
778 ** begins at z[0]. Return the length. Write the Unicode value into *pU.
780 static int decodeUtf8(const unsigned char *z
, int *pU
){
781 if( (z
[0] & 0xe0)==0xc0 && (z
[1] & 0xc0)==0x80 ){
782 *pU
= ((z
[0] & 0x1f)<<6) | (z
[1] & 0x3f);
785 if( (z
[0] & 0xf0)==0xe0 && (z
[1] & 0xc0)==0x80 && (z
[2] & 0xc0)==0x80 ){
786 *pU
= ((z
[0] & 0x0f)<<12) | ((z
[1] & 0x3f)<<6) | (z
[2] & 0x3f);
789 if( (z
[0] & 0xf8)==0xf0 && (z
[1] & 0xc0)==0x80 && (z
[2] & 0xc0)==0x80
790 && (z
[3] & 0xc0)==0x80
792 *pU
= ((z
[0] & 0x0f)<<18) | ((z
[1] & 0x3f)<<12) | ((z
[2] & 0x3f))<<6
803 ** Return the width, in display columns, of a UTF-8 string.
805 ** Each normal character counts as 1. Zero-width characters count
806 ** as zero, and double-width characters count as 2.
808 int cli_wcswidth(const char *z
){
809 const unsigned char *a
= (const unsigned char*)z
;
813 while( (c
= a
[i
])!=0 ){
816 int len
= decodeUtf8(&a
[i
], &u
);
831 ** Output string zUtf to stdout as w characters. If w is negative,
832 ** then right-justify the text. W is the width in UTF-8 characters, not
833 ** in bytes. This is different from the %*.*s specification in printf
834 ** since with %*.*s the width is measured in bytes, not characters.
836 ** Take into account zero-width and double-width Unicode characters.
837 ** In other words, a zero-width character does not count toward the
838 ** the w limit. A double-width character counts as two.
840 static void utf8_width_print(int w
, const char *zUtf
){
841 const unsigned char *a
= (const unsigned char*)zUtf
;
845 int aw
= w
<0 ? -w
: w
;
846 if( zUtf
==0 ) zUtf
= "";
847 while( (c
= a
[i
])!=0 ){
848 if( (c
&0xc0)==0xc0 ){
850 int len
= decodeUtf8(a
+i
, &u
);
851 int x
= cli_wcwidth(u
);
865 oputf("%.*s", i
, zUtf
);
867 oputf("%*s%s", aw
-n
, "", zUtf
);
869 oputf("%s%*s", zUtf
, aw
-n
, "");
875 ** Determines if a string is a number of not.
877 static int isNumber(const char *z
, int *realnum
){
878 if( *z
=='-' || *z
=='+' ) z
++;
883 if( realnum
) *realnum
= 0;
884 while( IsDigit(*z
) ){ z
++; }
887 if( !IsDigit(*z
) ) return 0;
888 while( IsDigit(*z
) ){ z
++; }
889 if( realnum
) *realnum
= 1;
891 if( *z
=='e' || *z
=='E' ){
893 if( *z
=='+' || *z
=='-' ) z
++;
894 if( !IsDigit(*z
) ) return 0;
895 while( IsDigit(*z
) ){ z
++; }
896 if( realnum
) *realnum
= 1;
902 ** Compute a string length that is limited to what can be stored in
903 ** lower 30 bits of a 32-bit signed integer.
905 static int strlen30(const char *z
){
907 while( *z2
){ z2
++; }
908 return 0x3fffffff & (int)(z2
- z
);
912 ** Return the length of a string in characters. Multibyte UTF8 characters
913 ** count as a single character.
915 static int strlenChar(const char *z
){
918 if( (0xc0&*(z
++))!=0x80 ) n
++;
924 ** Return open FILE * if zFile exists, can be opened for read
925 ** and is an ordinary file or a character stream source.
926 ** Otherwise return 0.
928 static FILE * openChrSource(const char *zFile
){
929 #if defined(_WIN32) || defined(WIN32)
930 struct __stat64 x
= {0};
931 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
932 /* On Windows, open first, then check the stream nature. This order
933 ** is necessary because _stat() and sibs, when checking a named pipe,
934 ** effectively break the pipe as its supplier sees it. */
935 FILE *rv
= fopen(zFile
, "rb");
936 if( rv
==0 ) return 0;
937 if( _fstat64(_fileno(rv
), &x
) != 0
938 || !STAT_CHR_SRC(x
.st_mode
)){
945 int rc
= stat(zFile
, &x
);
946 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
947 if( rc
!=0 ) return 0;
948 if( STAT_CHR_SRC(x
.st_mode
) ){
949 return fopen(zFile
, "rb");
958 ** This routine reads a line of text from FILE in, stores
959 ** the text in memory obtained from malloc() and returns a pointer
960 ** to the text. NULL is returned at end of file, or if malloc()
963 ** If zLine is not NULL then it is a malloced buffer returned from
964 ** a previous call to this routine that may be reused.
966 static char *local_getline(char *zLine
, FILE *in
){
967 int nLine
= zLine
==0 ? 0 : 100;
972 nLine
= nLine
*2 + 100;
973 zLine
= realloc(zLine
, nLine
);
974 shell_check_oom(zLine
);
976 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
984 while( zLine
[n
] ) n
++;
985 if( n
>0 && zLine
[n
-1]=='\n' ){
987 if( n
>0 && zLine
[n
-1]=='\r' ) n
--;
996 ** Retrieve a single line of input text.
998 ** If in==0 then read from standard input and prompt before each line.
999 ** If isContinuation is true, then a continuation prompt is appropriate.
1000 ** If isContinuation is zero, then the main prompt should be used.
1002 ** If zPrior is not NULL then it is a buffer from a prior call to this
1003 ** routine that can be reused.
1005 ** The result is stored in space obtained from malloc() and must either
1006 ** be freed by the caller or else passed back into this routine via the
1007 ** zPrior argument for reuse.
1009 #ifndef SQLITE_SHELL_FIDDLE
1010 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
1014 zResult
= local_getline(zPrior
, in
);
1016 zPrompt
= isContinuation
? CONTINUATION_PROMPT
: mainPrompt
;
1017 #if SHELL_USE_LOCAL_GETLINE
1018 sputz(stdout
, zPrompt
);
1021 zResult
= local_getline(zPrior
, stdin
);
1023 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1024 if( zResult
==0 ) sqlite3_sleep(50);
1025 }while( zResult
==0 && seenInterrupt
>0 );
1028 zResult
= shell_readline(zPrompt
);
1029 while( zResult
==0 ){
1030 /* ^C trap creates a false EOF, so let "interrupt" thread catch up. */
1032 if( seenInterrupt
==0 ) break;
1033 zResult
= shell_readline("");
1035 if( zResult
&& *zResult
) shell_add_history(zResult
);
1040 #endif /* !SQLITE_SHELL_FIDDLE */
1043 ** Return the value of a hexadecimal digit. Return -1 if the input
1044 ** is not a hex digit.
1046 static int hexDigitValue(char c
){
1047 if( c
>='0' && c
<='9' ) return c
- '0';
1048 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
1049 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
1054 ** Interpret zArg as an integer value, possibly with suffixes.
1056 static sqlite3_int64
integerValue(const char *zArg
){
1057 sqlite3_int64 v
= 0;
1058 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
1060 { "MiB", 1024*1024 },
1061 { "GiB", 1024*1024*1024 },
1064 { "GB", 1000000000 },
1067 { "G", 1000000000 },
1074 }else if( zArg
[0]=='+' ){
1077 if( zArg
[0]=='0' && zArg
[1]=='x' ){
1080 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
1085 while( IsDigit(zArg
[0]) ){
1086 v
= v
*10 + zArg
[0] - '0';
1090 for(i
=0; i
<ArraySize(aMult
); i
++){
1091 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
1092 v
*= aMult
[i
].iMult
;
1096 return isNeg
? -v
: v
;
1100 ** A variable length string to which one can append text.
1102 typedef struct ShellText ShellText
;
1110 ** Initialize and destroy a ShellText object
1112 static void initText(ShellText
*p
){
1113 memset(p
, 0, sizeof(*p
));
1115 static void freeText(ShellText
*p
){
1120 /* zIn is either a pointer to a NULL-terminated string in memory obtained
1121 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
1122 ** added to zIn, and the result returned in memory obtained from malloc().
1123 ** zIn, if it was not NULL, is freed.
1125 ** If the third argument, quote, is not '\0', then it is used as a
1126 ** quote character for zAppend.
1128 static void appendText(ShellText
*p
, const char *zAppend
, char quote
){
1131 i64 nAppend
= strlen30(zAppend
);
1133 len
= nAppend
+p
->n
+1;
1136 for(i
=0; i
<nAppend
; i
++){
1137 if( zAppend
[i
]==quote
) len
++;
1141 if( p
->z
==0 || p
->n
+len
>=p
->nAlloc
){
1142 p
->nAlloc
= p
->nAlloc
*2 + len
+ 20;
1143 p
->z
= realloc(p
->z
, p
->nAlloc
);
1144 shell_check_oom(p
->z
);
1148 char *zCsr
= p
->z
+p
->n
;
1150 for(i
=0; i
<nAppend
; i
++){
1151 *zCsr
++ = zAppend
[i
];
1152 if( zAppend
[i
]==quote
) *zCsr
++ = quote
;
1155 p
->n
= (int)(zCsr
- p
->z
);
1158 memcpy(p
->z
+p
->n
, zAppend
, nAppend
);
1165 ** Attempt to determine if identifier zName needs to be quoted, either
1166 ** because it contains non-alphanumeric characters, or because it is an
1167 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
1168 ** that quoting is required.
1170 ** Return '"' if quoting is required. Return 0 if no quoting is required.
1172 static char quoteChar(const char *zName
){
1174 if( zName
==0 ) return '"';
1175 if( !isalpha((unsigned char)zName
[0]) && zName
[0]!='_' ) return '"';
1176 for(i
=0; zName
[i
]; i
++){
1177 if( !isalnum((unsigned char)zName
[i
]) && zName
[i
]!='_' ) return '"';
1179 return sqlite3_keyword_check(zName
, i
) ? '"' : 0;
1183 ** Construct a fake object name and column list to describe the structure
1184 ** of the view, virtual table, or table valued function zSchema.zName.
1186 static char *shellFakeSchema(
1187 sqlite3
*db
, /* The database connection containing the vtab */
1188 const char *zSchema
, /* Schema of the database holding the vtab */
1189 const char *zName
/* The name of the virtual table */
1191 sqlite3_stmt
*pStmt
= 0;
1198 zSql
= sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
1199 zSchema
? zSchema
: "main", zName
);
1200 shell_check_oom(zSql
);
1201 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
1205 cQuote
= quoteChar(zSchema
);
1206 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")==0 ) cQuote
= 0;
1207 appendText(&s
, zSchema
, cQuote
);
1208 appendText(&s
, ".", 0);
1210 cQuote
= quoteChar(zName
);
1211 appendText(&s
, zName
, cQuote
);
1212 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
1213 const char *zCol
= (const char*)sqlite3_column_text(pStmt
, 1);
1215 appendText(&s
, zDiv
, 0);
1217 if( zCol
==0 ) zCol
= "";
1218 cQuote
= quoteChar(zCol
);
1219 appendText(&s
, zCol
, cQuote
);
1221 appendText(&s
, ")", 0);
1222 sqlite3_finalize(pStmt
);
1231 ** SQL function: strtod(X)
1233 ** Use the C-library strtod() function to convert string X into a double.
1234 ** Used for comparing the accuracy of SQLite's internal text-to-float conversion
1235 ** routines against the C-library.
1237 static void shellStrtod(
1238 sqlite3_context
*pCtx
,
1240 sqlite3_value
**apVal
1242 char *z
= (char*)sqlite3_value_text(apVal
[0]);
1243 UNUSED_PARAMETER(nVal
);
1245 sqlite3_result_double(pCtx
, strtod(z
,0));
1249 ** SQL function: dtostr(X)
1251 ** Use the C-library printf() function to convert real value X into a string.
1252 ** Used for comparing the accuracy of SQLite's internal float-to-text conversion
1253 ** routines against the C-library.
1255 static void shellDtostr(
1256 sqlite3_context
*pCtx
,
1258 sqlite3_value
**apVal
1260 double r
= sqlite3_value_double(apVal
[0]);
1261 int n
= nVal
>=2 ? sqlite3_value_int(apVal
[1]) : 26;
1264 if( n
>350 ) n
= 350;
1265 sqlite3_snprintf(sizeof(z
), z
, "%#+.*e", n
, r
);
1266 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
1271 ** SQL function: shell_module_schema(X)
1273 ** Return a fake schema for the table-valued function or eponymous virtual
1276 static void shellModuleSchema(
1277 sqlite3_context
*pCtx
,
1279 sqlite3_value
**apVal
1283 UNUSED_PARAMETER(nVal
);
1284 zName
= (const char*)sqlite3_value_text(apVal
[0]);
1285 zFake
= zName
? shellFakeSchema(sqlite3_context_db_handle(pCtx
), 0, zName
) : 0;
1287 sqlite3_result_text(pCtx
, sqlite3_mprintf("/* %s */", zFake
),
1294 ** SQL function: shell_add_schema(S,X)
1296 ** Add the schema name X to the CREATE statement in S and return the result.
1299 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
1304 ** CREATE UNIQUE INDEX
1307 ** CREATE VIRTUAL TABLE
1309 ** This UDF is used by the .schema command to insert the schema name of
1310 ** attached databases into the middle of the sqlite_schema.sql field.
1312 static void shellAddSchemaName(
1313 sqlite3_context
*pCtx
,
1315 sqlite3_value
**apVal
1317 static const char *aPrefix
[] = {
1326 const char *zIn
= (const char*)sqlite3_value_text(apVal
[0]);
1327 const char *zSchema
= (const char*)sqlite3_value_text(apVal
[1]);
1328 const char *zName
= (const char*)sqlite3_value_text(apVal
[2]);
1329 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
1330 UNUSED_PARAMETER(nVal
);
1331 if( zIn
!=0 && cli_strncmp(zIn
, "CREATE ", 7)==0 ){
1332 for(i
=0; i
<ArraySize(aPrefix
); i
++){
1333 int n
= strlen30(aPrefix
[i
]);
1334 if( cli_strncmp(zIn
+7, aPrefix
[i
], n
)==0 && zIn
[n
+7]==' ' ){
1338 char cQuote
= quoteChar(zSchema
);
1339 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")!=0 ){
1340 z
= sqlite3_mprintf("%.*s \"%w\".%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1342 z
= sqlite3_mprintf("%.*s %s.%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
1346 && aPrefix
[i
][0]=='V'
1347 && (zFake
= shellFakeSchema(db
, zSchema
, zName
))!=0
1350 z
= sqlite3_mprintf("%s\n/* %s */", zIn
, zFake
);
1352 z
= sqlite3_mprintf("%z\n/* %s */", z
, zFake
);
1357 sqlite3_result_text(pCtx
, z
, -1, sqlite3_free
);
1363 sqlite3_result_value(pCtx
, apVal
[0]);
1367 ** The source code for several run-time loadable extensions is inserted
1368 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1369 ** code, we need to override some macros to make the included program code
1370 ** work here in the middle of this regular program.
1372 #define SQLITE_EXTENSION_INIT1
1373 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1375 #if defined(_WIN32) && defined(_MSC_VER)
1376 INCLUDE test_windirent
.h
1377 INCLUDE test_windirent
.c
1378 #define dirent DIRENT
1380 INCLUDE
../ext
/misc
/memtrace
.c
1381 INCLUDE
../ext
/misc
/pcachetrace
.c
1382 INCLUDE
../ext
/misc
/shathree
.c
1383 INCLUDE
../ext
/misc
/sha1
.c
1384 INCLUDE
../ext
/misc
/uint
.c
1385 INCLUDE
../ext
/misc
/decimal
.c
1386 INCLUDE
../ext
/misc
/percentile
.c
1387 #undef sqlite3_base_init
1388 #define sqlite3_base_init sqlite3_base64_init
1389 INCLUDE
../ext
/misc
/base64
.c
1390 #undef sqlite3_base_init
1391 #define sqlite3_base_init sqlite3_base85_init
1392 #define OMIT_BASE85_CHECKER
1393 INCLUDE
../ext
/misc
/base85
.c
1394 INCLUDE
../ext
/misc
/ieee754
.c
1395 INCLUDE
../ext
/misc
/series
.c
1396 INCLUDE
../ext
/misc
/regexp
.c
1397 #ifndef SQLITE_SHELL_FIDDLE
1398 INCLUDE
../ext
/misc
/fileio
.c
1399 INCLUDE
../ext
/misc
/completion
.c
1400 INCLUDE
../ext
/misc
/appendvfs
.c
1402 #ifdef SQLITE_HAVE_ZLIB
1403 INCLUDE
../ext
/misc
/zipfile
.c
1404 INCLUDE
../ext
/misc
/sqlar
.c
1406 INCLUDE
../ext
/expert
/sqlite3expert
.h
1407 INCLUDE
../ext
/expert
/sqlite3expert
.c
1408 INCLUDE
../ext
/intck
/sqlite3intck
.h
1409 INCLUDE
../ext
/intck
/sqlite3intck
.c
1410 INCLUDE
../ext
/misc
/stmtrand
.c
1411 INCLUDE
../ext
/misc
/vfstrace
.c
1413 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
1414 #define SQLITE_SHELL_HAVE_RECOVER 1
1416 #define SQLITE_SHELL_HAVE_RECOVER 0
1418 #if SQLITE_SHELL_HAVE_RECOVER
1419 INCLUDE
../ext
/recover
/sqlite3recover
.h
1420 # ifndef SQLITE_HAVE_SQLITE3R
1421 INCLUDE
../ext
/recover
/dbdata
.c
1422 INCLUDE
../ext
/recover
/sqlite3recover
.c
1423 # endif /* SQLITE_HAVE_SQLITE3R */
1425 #ifdef SQLITE_SHELL_EXTSRC
1426 # include SHELL_STRINGIFY(SQLITE_SHELL_EXTSRC)
1429 #if defined(SQLITE_ENABLE_SESSION)
1431 ** State information for a single open session
1433 typedef struct OpenSession OpenSession
;
1434 struct OpenSession
{
1435 char *zName
; /* Symbolic name for this session */
1436 int nFilter
; /* Number of xFilter rejection GLOB patterns */
1437 char **azFilter
; /* Array of xFilter rejection GLOB patterns */
1438 sqlite3_session
*p
; /* The open session */
1442 typedef struct ExpertInfo ExpertInfo
;
1444 sqlite3expert
*pExpert
;
1448 /* A single line in the EQP output */
1449 typedef struct EQPGraphRow EQPGraphRow
;
1450 struct EQPGraphRow
{
1451 int iEqpId
; /* ID for this row */
1452 int iParentId
; /* ID of the parent row */
1453 EQPGraphRow
*pNext
; /* Next row in sequence */
1454 char zText
[1]; /* Text to display for this row */
1457 /* All EQP output is collected into an instance of the following */
1458 typedef struct EQPGraph EQPGraph
;
1460 EQPGraphRow
*pRow
; /* Linked list of all rows of the EQP output */
1461 EQPGraphRow
*pLast
; /* Last element of the pRow list */
1462 char zPrefix
[100]; /* Graph prefix */
1465 /* Parameters affecting columnar mode result display (defaulting together) */
1466 typedef struct ColModeOpts
{
1467 int iWrap
; /* In columnar modes, wrap lines reaching this limit */
1468 u8 bQuote
; /* Quote results for .mode box and table */
1469 u8 bWordWrap
; /* In columnar modes, wrap at word boundaries */
1471 #define ColModeOpts_default { 60, 0, 0 }
1472 #define ColModeOpts_default_qbox { 60, 1, 0 }
1475 ** State information about the database connection is contained in an
1476 ** instance of the following structure.
1478 typedef struct ShellState ShellState
;
1480 sqlite3
*db
; /* The database */
1481 u8 autoExplain
; /* Automatically turn on .explain mode */
1482 u8 autoEQP
; /* Run EXPLAIN QUERY PLAN prior to each SQL stmt */
1483 u8 autoEQPtest
; /* autoEQP is in test mode */
1484 u8 autoEQPtrace
; /* autoEQP is in trace mode */
1485 u8 scanstatsOn
; /* True to display scan stats before each finalize */
1486 u8 openMode
; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1487 u8 doXdgOpen
; /* Invoke start/open/xdg-open in output_reset() */
1488 u8 nEqpLevel
; /* Depth of the EQP output graph */
1489 u8 eTraceType
; /* SHELL_TRACE_* value for type of trace */
1490 u8 bSafeMode
; /* True to prohibit unsafe operations */
1491 u8 bSafeModePersist
; /* The long-term value of bSafeMode */
1492 u8 eRestoreState
; /* See comments above doAutoDetectRestore() */
1493 ColModeOpts cmOpts
; /* Option values affecting columnar mode output */
1494 unsigned statsOn
; /* True to display memory stats before each finalize */
1495 unsigned mEqpLines
; /* Mask of vertical lines in the EQP output graph */
1496 int inputNesting
; /* Track nesting level of .read and other redirects */
1497 int outCount
; /* Revert to stdout when reaching zero */
1498 int cnt
; /* Number of records displayed so far */
1499 int lineno
; /* Line number of last line read from in */
1500 int openFlags
; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
1501 FILE *in
; /* Read commands from this stream */
1502 FILE *out
; /* Write results here */
1503 FILE *traceOut
; /* Output for sqlite3_trace() */
1504 int nErr
; /* Number of errors seen */
1505 int mode
; /* An output mode setting */
1506 int modePrior
; /* Saved mode */
1507 int cMode
; /* temporary output mode for the current query */
1508 int normalMode
; /* Output mode before ".explain on" */
1509 int writableSchema
; /* True if PRAGMA writable_schema=ON */
1510 int showHeader
; /* True to show column names in List or Column mode */
1511 int nCheck
; /* Number of ".check" commands run */
1512 unsigned nProgress
; /* Number of progress callbacks encountered */
1513 unsigned mxProgress
; /* Maximum progress callbacks before failing */
1514 unsigned flgProgress
; /* Flags for the progress callback */
1515 unsigned shellFlgs
; /* Various flags */
1516 unsigned priorShFlgs
; /* Saved copy of flags */
1517 sqlite3_int64 szMax
; /* --maxsize argument to .open */
1518 char *zDestTable
; /* Name of destination table when MODE_Insert */
1519 char *zTempFile
; /* Temporary file that might need deleting */
1520 char zTestcase
[30]; /* Name of current test case */
1521 char colSeparator
[20]; /* Column separator character for several modes */
1522 char rowSeparator
[20]; /* Row separator character for MODE_Ascii */
1523 char colSepPrior
[20]; /* Saved column separator */
1524 char rowSepPrior
[20]; /* Saved row separator */
1525 int *colWidth
; /* Requested width of each column in columnar modes */
1526 int *actualWidth
; /* Actual width of each column */
1527 int nWidth
; /* Number of slots in colWidth[] and actualWidth[] */
1528 char nullValue
[20]; /* The text to print when a NULL comes back from
1530 char outfile
[FILENAME_MAX
]; /* Filename for *out */
1531 sqlite3_stmt
*pStmt
; /* Current statement if any. */
1532 FILE *pLog
; /* Write log output here */
1533 struct AuxDb
{ /* Storage space for auxiliary database connections */
1534 sqlite3
*db
; /* Connection pointer */
1535 const char *zDbFilename
; /* Filename used to open the connection */
1536 char *zFreeOnClose
; /* Free this memory allocation on close */
1537 #if defined(SQLITE_ENABLE_SESSION)
1538 int nSession
; /* Number of active sessions */
1539 OpenSession aSession
[4]; /* Array of sessions. [0] is in focus. */
1541 } aAuxDb
[5], /* Array of all database connections */
1542 *pAuxDb
; /* Currently active database connection */
1543 int *aiIndent
; /* Array of indents used in MODE_Explain */
1544 int nIndent
; /* Size of array aiIndent[] */
1545 int iIndent
; /* Index of current op in aiIndent[] */
1546 char *zNonce
; /* Nonce for temporary safe-mode escapes */
1547 EQPGraph sGraph
; /* Information for the graphical EXPLAIN QUERY PLAN */
1548 ExpertInfo expert
; /* Valid if previous command was ".expert OPT..." */
1549 #ifdef SQLITE_SHELL_FIDDLE
1551 const char * zInput
; /* Input string from wasm/JS proxy */
1552 const char * zPos
; /* Cursor pos into zInput */
1553 const char * zDefaultDbName
; /* Default name for db file */
1558 #ifdef SQLITE_SHELL_FIDDLE
1559 static ShellState shellState
;
1563 /* Allowed values for ShellState.autoEQP
1565 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1566 #define AUTOEQP_on 1 /* Automatic EQP is on */
1567 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1568 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1570 /* Allowed values for ShellState.openMode
1572 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1573 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1574 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1575 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1576 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1577 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
1578 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
1580 /* Allowed values for ShellState.eTraceType
1582 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1583 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1584 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1586 /* Bits in the ShellState.flgProgress variable */
1587 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1588 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progress
1589 ** callback limit is reached, and for each
1590 ** top-level SQL statement */
1591 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
1594 ** These are the allowed shellFlgs values
1596 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1597 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1598 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1599 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1600 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1601 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1602 #define SHFLG_Echo 0x00000040 /* .echo on/off, or --echo setting */
1603 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
1604 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
1605 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
1606 #define SHFLG_TestingMode 0x00000400 /* allow unsafe testing features */
1609 ** Macros for testing and setting shellFlgs
1611 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1612 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1613 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1616 ** These are the allowed modes.
1618 #define MODE_Line 0 /* One column per line. Blank line between records */
1619 #define MODE_Column 1 /* One record per line in neat columns */
1620 #define MODE_List 2 /* One record per line with a separator */
1621 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1622 #define MODE_Html 4 /* Generate an XHTML table */
1623 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1624 #define MODE_Quote 6 /* Quote values as for SQL */
1625 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1626 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1627 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1628 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1629 #define MODE_Pretty 11 /* Pretty-print schemas */
1630 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1631 #define MODE_Json 13 /* Output JSON */
1632 #define MODE_Markdown 14 /* Markdown formatting */
1633 #define MODE_Table 15 /* MySQL-style table formatting */
1634 #define MODE_Box 16 /* Unicode box-drawing characters */
1635 #define MODE_Count 17 /* Output only a count of the rows of output */
1636 #define MODE_Off 18 /* No query output shown */
1637 #define MODE_ScanExp 19 /* Like MODE_Explain, but for ".scanstats vm" */
1639 static const char *modeDescr
[] = {
1662 ** These are the column/row/line separators used by the various
1663 ** import/export modes.
1665 #define SEP_Column "|"
1666 #define SEP_Row "\n"
1667 #define SEP_Tab "\t"
1668 #define SEP_Space " "
1669 #define SEP_Comma ","
1670 #define SEP_CrLf "\r\n"
1671 #define SEP_Unit "\x1F"
1672 #define SEP_Record "\x1E"
1675 ** Limit input nesting via .read or any other input redirect.
1676 ** It's not too expensive, so a generous allowance can be made.
1678 #define MAX_INPUT_NESTING 25
1681 ** A callback for the sqlite3_log() interface.
1683 static void shellLog(void *pArg
, int iErrCode
, const char *zMsg
){
1684 ShellState
*p
= (ShellState
*)pArg
;
1685 if( p
->pLog
==0 ) return;
1686 sputf(p
->pLog
, "(%d) %s\n", iErrCode
, zMsg
);
1691 ** SQL function: shell_putsnl(X)
1693 ** Write the text X to the screen (or whatever output is being directed)
1694 ** adding a newline at the end, and then return X.
1696 static void shellPutsFunc(
1697 sqlite3_context
*pCtx
,
1699 sqlite3_value
**apVal
1701 /* Unused: (ShellState*)sqlite3_user_data(pCtx); */
1703 oputf("%s\n", sqlite3_value_text(apVal
[0]));
1704 sqlite3_result_value(pCtx
, apVal
[0]);
1708 ** If in safe mode, print an error message described by the arguments
1709 ** and exit immediately.
1711 static void failIfSafeMode(
1713 const char *zErrMsg
,
1719 va_start(ap
, zErrMsg
);
1720 zMsg
= sqlite3_vmprintf(zErrMsg
, ap
);
1722 eputf("line %d: %s\n", p
->lineno
, zMsg
);
1728 ** SQL function: edit(VALUE)
1729 ** edit(VALUE,EDITOR)
1733 ** (1) Write VALUE into a temporary file.
1734 ** (2) Run program EDITOR on that temporary file.
1735 ** (3) Read the temporary file back and return its content as the result.
1736 ** (4) Delete the temporary file
1738 ** If the EDITOR argument is omitted, use the value in the VISUAL
1739 ** environment variable. If still there is no EDITOR, through an error.
1741 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1743 #ifndef SQLITE_NOHAVE_SYSTEM
1744 static void editFunc(
1745 sqlite3_context
*context
,
1747 sqlite3_value
**argv
1749 const char *zEditor
;
1750 char *zTempFile
= 0;
1759 unsigned char *p
= 0;
1762 zEditor
= (const char*)sqlite3_value_text(argv
[1]);
1764 zEditor
= getenv("VISUAL");
1767 sqlite3_result_error(context
, "no editor for edit()", -1);
1770 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
){
1771 sqlite3_result_error(context
, "NULL input to edit()", -1);
1774 db
= sqlite3_context_db_handle(context
);
1776 sqlite3_file_control(db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &zTempFile
);
1778 sqlite3_uint64 r
= 0;
1779 sqlite3_randomness(sizeof(r
), &r
);
1780 zTempFile
= sqlite3_mprintf("temp%llx", r
);
1782 sqlite3_result_error_nomem(context
);
1786 bBin
= sqlite3_value_type(argv
[0])==SQLITE_BLOB
;
1787 /* When writing the file to be edited, do \n to \r\n conversions on systems
1788 ** that want \r\n line endings */
1789 f
= fopen(zTempFile
, bBin
? "wb" : "w");
1791 sqlite3_result_error(context
, "edit() cannot open temp file", -1);
1794 sz
= sqlite3_value_bytes(argv
[0]);
1796 x
= fwrite(sqlite3_value_blob(argv
[0]), 1, (size_t)sz
, f
);
1798 const char *z
= (const char*)sqlite3_value_text(argv
[0]);
1799 /* Remember whether or not the value originally contained \r\n */
1800 if( z
&& strstr(z
,"\r\n")!=0 ) hasCRNL
= 1;
1801 x
= fwrite(sqlite3_value_text(argv
[0]), 1, (size_t)sz
, f
);
1806 sqlite3_result_error(context
, "edit() could not write the whole file", -1);
1809 zCmd
= sqlite3_mprintf("%s \"%s\"", zEditor
, zTempFile
);
1811 sqlite3_result_error_nomem(context
);
1817 sqlite3_result_error(context
, "EDITOR returned non-zero", -1);
1820 f
= fopen(zTempFile
, "rb");
1822 sqlite3_result_error(context
,
1823 "edit() cannot reopen temp file after edit", -1);
1826 fseek(f
, 0, SEEK_END
);
1829 p
= sqlite3_malloc64( sz
+1 );
1831 sqlite3_result_error_nomem(context
);
1834 x
= fread(p
, 1, (size_t)sz
, f
);
1838 sqlite3_result_error(context
, "could not read back the whole file", -1);
1842 sqlite3_result_blob64(context
, p
, sz
, sqlite3_free
);
1846 /* If the original contains \r\n then do no conversions back to \n */
1848 /* If the file did not originally contain \r\n then convert any new
1849 ** \r\n back into \n */
1851 for(i
=j
=0; i
<sz
; i
++){
1852 if( p
[i
]=='\r' && p
[i
+1]=='\n' ) i
++;
1858 sqlite3_result_text64(context
, (const char*)p
, sz
,
1859 sqlite3_free
, SQLITE_UTF8
);
1866 sqlite3_free(zTempFile
);
1869 #endif /* SQLITE_NOHAVE_SYSTEM */
1872 ** Save or restore the current output mode
1874 static void outputModePush(ShellState
*p
){
1875 p
->modePrior
= p
->mode
;
1876 p
->priorShFlgs
= p
->shellFlgs
;
1877 memcpy(p
->colSepPrior
, p
->colSeparator
, sizeof(p
->colSeparator
));
1878 memcpy(p
->rowSepPrior
, p
->rowSeparator
, sizeof(p
->rowSeparator
));
1880 static void outputModePop(ShellState
*p
){
1881 p
->mode
= p
->modePrior
;
1882 p
->shellFlgs
= p
->priorShFlgs
;
1883 memcpy(p
->colSeparator
, p
->colSepPrior
, sizeof(p
->colSeparator
));
1884 memcpy(p
->rowSeparator
, p
->rowSepPrior
, sizeof(p
->rowSeparator
));
1888 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1890 static void output_hex_blob(const void *pBlob
, int nBlob
){
1892 unsigned char *aBlob
= (unsigned char*)pBlob
;
1894 char *zStr
= sqlite3_malloc(nBlob
*2 + 1);
1895 shell_check_oom(zStr
);
1897 for(i
=0; i
<nBlob
; i
++){
1898 static const char aHex
[] = {
1899 '0', '1', '2', '3', '4', '5', '6', '7',
1900 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
1902 zStr
[i
*2] = aHex
[ (aBlob
[i
] >> 4) ];
1903 zStr
[i
*2+1] = aHex
[ (aBlob
[i
] & 0x0F) ];
1907 oputf("X'%s'", zStr
);
1912 ** Find a string that is not found anywhere in z[]. Return a pointer
1915 ** Try to use zA and zB first. If both of those are already found in z[]
1916 ** then make up some string and store it in the buffer zBuf.
1918 static const char *unused_string(
1919 const char *z
, /* Result must not appear anywhere in z */
1920 const char *zA
, const char *zB
, /* Try these first */
1921 char *zBuf
/* Space to store a generated string */
1924 if( strstr(z
, zA
)==0 ) return zA
;
1925 if( strstr(z
, zB
)==0 ) return zB
;
1927 sqlite3_snprintf(20,zBuf
,"(%s%u)", zA
, i
++);
1928 }while( strstr(z
,zBuf
)!=0 );
1933 ** Output the given string as a quoted string using SQL quoting conventions.
1935 ** See also: output_quoted_escaped_string()
1937 static void output_quoted_string(const char *z
){
1940 #ifndef SQLITE_SHELL_FIDDLE
1941 FILE *pfO
= setOutputStream(invalidFileStream
);
1942 setBinaryMode(pfO
, 1);
1945 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1951 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1954 oputf("%.*s", i
, z
);
1968 #ifndef SQLITE_SHELL_FIDDLE
1969 setTextMode(pfO
, 1);
1971 setTextMode(stdout
, 1);
1976 ** Output the given string as a quoted string using SQL quoting conventions.
1977 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1978 ** get corrupted by end-of-line translation facilities in some operating
1981 ** This is like output_quoted_string() but with the addition of the \r\n
1982 ** escape mechanism.
1984 static void output_quoted_escaped_string(const char *z
){
1987 #ifndef SQLITE_SHELL_FIDDLE
1988 FILE *pfO
= setOutputStream(invalidFileStream
);
1989 setBinaryMode(pfO
, 1);
1991 for(i
=0; (c
= z
[i
])!=0 && c
!='\'' && c
!='\n' && c
!='\r'; i
++){}
1995 const char *zNL
= 0;
1996 const char *zCR
= 0;
1999 char zBuf1
[20], zBuf2
[20];
2000 for(i
=0; z
[i
]; i
++){
2001 if( z
[i
]=='\n' ) nNL
++;
2002 if( z
[i
]=='\r' ) nCR
++;
2006 zNL
= unused_string(z
, "\\n", "\\012", zBuf1
);
2010 zCR
= unused_string(z
, "\\r", "\\015", zBuf2
);
2014 for(i
=0; (c
= z
[i
])!=0 && c
!='\n' && c
!='\r' && c
!='\''; i
++){}
2017 oputf("%.*s", i
, z
);
2036 oputf(",'%s',char(13))", zCR
);
2039 oputf(",'%s',char(10))", zNL
);
2042 #ifndef SQLITE_SHELL_FIDDLE
2043 setTextMode(pfO
, 1);
2045 setTextMode(stdout
, 1);
2050 ** Find earliest of chars within s specified in zAny.
2051 ** With ns == ~0, is like strpbrk(s,zAny) and s must be 0-terminated.
2053 static const char *anyOfInStr(const char *s
, const char *zAny
, size_t ns
){
2054 const char *pcFirst
= 0;
2055 if( ns
== ~(size_t)0 ) ns
= strlen(s
);
2057 const char *pc
= (const char*)memchr(s
, *zAny
&0xff, ns
);
2067 ** Output the given string as a quoted according to C or TCL quoting rules.
2069 static void output_c_string(const char *z
){
2071 static const char *zq
= "\"";
2072 static long ctrlMask
= ~0L;
2073 static const char *zDQBSRO
= "\"\\\x7f"; /* double-quote, backslash, rubout */
2074 char ace
[3] = "\\?";
2078 const char *pcDQBSRO
= anyOfInStr(z
, zDQBSRO
, ~(size_t)0);
2079 const char *pcPast
= zSkipValidUtf8(z
, INT_MAX
, ctrlMask
);
2080 const char *pcEnd
= (pcDQBSRO
&& pcDQBSRO
< pcPast
)? pcDQBSRO
: pcPast
;
2081 if( pcEnd
> z
) oputb(z
, (int)(pcEnd
-z
));
2082 if( (c
= *pcEnd
)==0 ) break;
2085 case '\\': case '"':
2088 case '\t': cbsSay
= 't'; break;
2089 case '\n': cbsSay
= 'n'; break;
2090 case '\r': cbsSay
= 'r'; break;
2091 case '\f': cbsSay
= 'f'; break;
2092 default: cbsSay
= 0; break;
2097 }else if( !isprint(c
&0xff) ){
2098 oputf("\\%03o", c
&0xff);
2109 ** Output the given string as a quoted according to JSON quoting rules.
2111 static void output_json_string(const char *z
, i64 n
){
2113 static const char *zq
= "\"";
2114 static long ctrlMask
= ~0L;
2115 static const char *zDQBS
= "\"\\";
2116 const char *pcLimit
;
2117 char ace
[3] = "\\?";
2121 pcLimit
= z
+ ((n
<0)? strlen(z
) : (size_t)n
);
2123 while( z
< pcLimit
){
2124 const char *pcDQBS
= anyOfInStr(z
, zDQBS
, pcLimit
-z
);
2125 const char *pcPast
= zSkipValidUtf8(z
, (int)(pcLimit
-z
), ctrlMask
);
2126 const char *pcEnd
= (pcDQBS
&& pcDQBS
< pcPast
)? pcDQBS
: pcPast
;
2128 oputb(z
, (int)(pcEnd
-z
));
2131 if( z
>= pcLimit
) break;
2134 case '"': case '\\':
2137 case '\b': cbsSay
= 'b'; break;
2138 case '\f': cbsSay
= 'f'; break;
2139 case '\n': cbsSay
= 'n'; break;
2140 case '\r': cbsSay
= 'r'; break;
2141 case '\t': cbsSay
= 't'; break;
2142 default: cbsSay
= 0; break;
2147 }else if( c
<=0x1f ){
2158 ** Output the given string with characters that are special to
2161 static void output_html_string(const char *z
){
2177 }else if( z
[i
]=='&' ){
2179 }else if( z
[i
]=='>' ){
2181 }else if( z
[i
]=='\"' ){
2183 }else if( z
[i
]=='\'' ){
2193 ** If a field contains any character identified by a 1 in the following
2194 ** array, then the string must be quoted for CSV.
2196 static const char needCsvQuote
[] = {
2197 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2198 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2199 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
2200 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2201 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2202 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2203 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2204 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
2205 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2206 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2207 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2208 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2209 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2210 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2211 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2212 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2216 ** Output a single term of CSV. Actually, p->colSeparator is used for
2217 ** the separator, which may or may not be a comma. p->nullValue is
2218 ** the null value. Strings are quoted if necessary. The separator
2219 ** is only issued if bSep is true.
2221 static void output_csv(ShellState
*p
, const char *z
, int bSep
){
2223 oputf("%s",p
->nullValue
);
2226 for(i
=0; z
[i
]; i
++){
2227 if( needCsvQuote
[((unsigned char*)z
)[i
]] ){
2232 if( i
==0 || strstr(z
, p
->colSeparator
)!=0 ){
2233 char *zQuoted
= sqlite3_mprintf("\"%w\"", z
);
2234 shell_check_oom(zQuoted
);
2236 sqlite3_free(zQuoted
);
2242 oputz(p
->colSeparator
);
2247 ** This routine runs when the user presses Ctrl-C
2249 static void interrupt_handler(int NotUsed
){
2250 UNUSED_PARAMETER(NotUsed
);
2251 if( ++seenInterrupt
>1 ) exit(1);
2252 if( globalDb
) sqlite3_interrupt(globalDb
);
2255 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
2257 ** This routine runs for console events (e.g. Ctrl-C) on Win32
2259 static BOOL WINAPI
ConsoleCtrlHandler(
2260 DWORD dwCtrlType
/* One of the CTRL_*_EVENT constants */
2262 if( dwCtrlType
==CTRL_C_EVENT
){
2263 interrupt_handler(0);
2270 #ifndef SQLITE_OMIT_AUTHORIZATION
2272 ** This authorizer runs in safe mode.
2274 static int safeModeAuth(
2282 ShellState
*p
= (ShellState
*)pClientData
;
2283 static const char *azProhibitedFunctions
[] = {
2292 UNUSED_PARAMETER(zA1
);
2293 UNUSED_PARAMETER(zA3
);
2294 UNUSED_PARAMETER(zA4
);
2296 case SQLITE_ATTACH
: {
2297 #ifndef SQLITE_SHELL_FIDDLE
2298 /* In WASM builds the filesystem is a virtual sandbox, so
2299 ** there's no harm in using ATTACH. */
2300 failIfSafeMode(p
, "cannot run ATTACH in safe mode");
2304 case SQLITE_FUNCTION
: {
2306 for(i
=0; i
<ArraySize(azProhibitedFunctions
); i
++){
2307 if( sqlite3_stricmp(zA2
, azProhibitedFunctions
[i
])==0 ){
2308 failIfSafeMode(p
, "cannot use the %s() function in safe mode",
2309 azProhibitedFunctions
[i
]);
2319 ** When the ".auth ON" is set, the following authorizer callback is
2320 ** invoked. It always returns SQLITE_OK.
2322 static int shellAuth(
2330 ShellState
*p
= (ShellState
*)pClientData
;
2331 static const char *azAction
[] = { 0,
2332 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
2333 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
2334 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
2335 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
2336 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
2337 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
2338 "PRAGMA", "READ", "SELECT",
2339 "TRANSACTION", "UPDATE", "ATTACH",
2340 "DETACH", "ALTER_TABLE", "REINDEX",
2341 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
2342 "FUNCTION", "SAVEPOINT", "RECURSIVE"
2350 oputf("authorizer: %s", azAction
[op
]);
2354 output_c_string(az
[i
]);
2360 if( p
->bSafeMode
) (void)safeModeAuth(pClientData
, op
, zA1
, zA2
, zA3
, zA4
);
2366 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
2368 ** This routine converts some CREATE TABLE statements for shadow tables
2369 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
2371 ** If the schema statement in z[] contains a start-of-comment and if
2372 ** sqlite3_complete() returns false, try to terminate the comment before
2373 ** printing the result. https://sqlite.org/forum/forumpost/d7be961c5c
2375 static void printSchemaLine(const char *z
, const char *zTail
){
2378 if( zTail
==0 ) return;
2379 if( zTail
[0]==';' && (strstr(z
, "/*")!=0 || strstr(z
,"--")!=0) ){
2380 const char *zOrig
= z
;
2381 static const char *azTerm
[] = { "", "*/", "\n" };
2383 for(i
=0; i
<ArraySize(azTerm
); i
++){
2384 char *zNew
= sqlite3_mprintf("%s%s;", zOrig
, azTerm
[i
]);
2385 shell_check_oom(zNew
);
2386 if( sqlite3_complete(zNew
) ){
2387 size_t n
= strlen(zNew
);
2396 if( sqlite3_strglob("CREATE TABLE ['\"]*", z
)==0 ){
2397 oputf("CREATE TABLE IF NOT EXISTS %s%s", z
+13, zTail
);
2399 oputf("%s%s", z
, zTail
);
2401 sqlite3_free(zToFree
);
2403 static void printSchemaLineN(char *z
, int n
, const char *zTail
){
2406 printSchemaLine(z
, zTail
);
2411 ** Return true if string z[] has nothing but whitespace and comments to the
2412 ** end of the first line.
2414 static int wsToEol(const char *z
){
2416 for(i
=0; z
[i
]; i
++){
2417 if( z
[i
]=='\n' ) return 1;
2418 if( IsSpace(z
[i
]) ) continue;
2419 if( z
[i
]=='-' && z
[i
+1]=='-' ) return 1;
2426 ** Add a new entry to the EXPLAIN QUERY PLAN data
2428 static void eqp_append(ShellState
*p
, int iEqpId
, int p2
, const char *zText
){
2431 if( zText
==0 ) return;
2432 nText
= strlen(zText
);
2433 if( p
->autoEQPtest
){
2434 oputf("%d,%d,%s\n", iEqpId
, p2
, zText
);
2436 pNew
= sqlite3_malloc64( sizeof(*pNew
) + nText
);
2437 shell_check_oom(pNew
);
2438 pNew
->iEqpId
= iEqpId
;
2439 pNew
->iParentId
= p2
;
2440 memcpy(pNew
->zText
, zText
, nText
+1);
2442 if( p
->sGraph
.pLast
){
2443 p
->sGraph
.pLast
->pNext
= pNew
;
2445 p
->sGraph
.pRow
= pNew
;
2447 p
->sGraph
.pLast
= pNew
;
2451 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
2454 static void eqp_reset(ShellState
*p
){
2455 EQPGraphRow
*pRow
, *pNext
;
2456 for(pRow
= p
->sGraph
.pRow
; pRow
; pRow
= pNext
){
2457 pNext
= pRow
->pNext
;
2460 memset(&p
->sGraph
, 0, sizeof(p
->sGraph
));
2463 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
2464 ** pOld, or return the first such line if pOld is NULL
2466 static EQPGraphRow
*eqp_next_row(ShellState
*p
, int iEqpId
, EQPGraphRow
*pOld
){
2467 EQPGraphRow
*pRow
= pOld
? pOld
->pNext
: p
->sGraph
.pRow
;
2468 while( pRow
&& pRow
->iParentId
!=iEqpId
) pRow
= pRow
->pNext
;
2472 /* Render a single level of the graph that has iEqpId as its parent. Called
2473 ** recursively to render sublevels.
2475 static void eqp_render_level(ShellState
*p
, int iEqpId
){
2476 EQPGraphRow
*pRow
, *pNext
;
2477 i64 n
= strlen(p
->sGraph
.zPrefix
);
2479 for(pRow
= eqp_next_row(p
, iEqpId
, 0); pRow
; pRow
= pNext
){
2480 pNext
= eqp_next_row(p
, iEqpId
, pRow
);
2482 oputf("%s%s%s\n", p
->sGraph
.zPrefix
, pNext
? "|--" : "`--", z
);
2483 if( n
<(i64
)sizeof(p
->sGraph
.zPrefix
)-7 ){
2484 memcpy(&p
->sGraph
.zPrefix
[n
], pNext
? "| " : " ", 4);
2485 eqp_render_level(p
, pRow
->iEqpId
);
2486 p
->sGraph
.zPrefix
[n
] = 0;
2492 ** Display and reset the EXPLAIN QUERY PLAN data
2494 static void eqp_render(ShellState
*p
, i64 nCycle
){
2495 EQPGraphRow
*pRow
= p
->sGraph
.pRow
;
2497 if( pRow
->zText
[0]=='-' ){
2498 if( pRow
->pNext
==0 ){
2502 oputf("%s\n", pRow
->zText
+3);
2503 p
->sGraph
.pRow
= pRow
->pNext
;
2505 }else if( nCycle
>0 ){
2506 oputf("QUERY PLAN (cycles=%lld [100%%])\n", nCycle
);
2508 oputz("QUERY PLAN\n");
2510 p
->sGraph
.zPrefix
[0] = 0;
2511 eqp_render_level(p
, 0);
2516 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
2518 ** Progress handler callback.
2520 static int progress_handler(void *pClientData
) {
2521 ShellState
*p
= (ShellState
*)pClientData
;
2523 if( p
->nProgress
>=p
->mxProgress
&& p
->mxProgress
>0 ){
2524 oputf("Progress limit reached (%u)\n", p
->nProgress
);
2525 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
2526 if( p
->flgProgress
& SHELL_PROGRESS_ONCE
) p
->mxProgress
= 0;
2529 if( (p
->flgProgress
& SHELL_PROGRESS_QUIET
)==0 ){
2530 oputf("Progress %u\n", p
->nProgress
);
2534 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
2539 static void print_dashes(int N
){
2540 const char zDash
[] = "--------------------------------------------------";
2541 const int nDash
= sizeof(zDash
) - 1;
2546 oputf("%.*s", N
, zDash
);
2550 ** Print a markdown or table-style row separator using ascii-art
2552 static void print_row_separator(
2560 print_dashes(p
->actualWidth
[0]+2);
2561 for(i
=1; i
<nArg
; i
++){
2563 print_dashes(p
->actualWidth
[i
]+2);
2571 ** This is the callback routine that the shell
2572 ** invokes for each row of a query result.
2574 static int shell_callback(
2576 int nArg
, /* Number of result columns */
2577 char **azArg
, /* Text of each result column */
2578 char **azCol
, /* Column names */
2579 int *aiType
/* Column types. Might be NULL */
2582 ShellState
*p
= (ShellState
*)pArg
;
2584 if( azArg
==0 ) return 0;
2592 if( azArg
==0 ) break;
2593 for(i
=0; i
<nArg
; i
++){
2594 int len
= strlen30(azCol
[i
] ? azCol
[i
] : "");
2595 if( len
>w
) w
= len
;
2597 if( p
->cnt
++>0 ) oputz(p
->rowSeparator
);
2598 for(i
=0; i
<nArg
; i
++){
2599 oputf("%*s = %s%s", w
, azCol
[i
],
2600 azArg
[i
] ? azArg
[i
] : p
->nullValue
, p
->rowSeparator
);
2605 case MODE_Explain
: {
2606 static const int aExplainWidth
[] = {4, 13, 4, 4, 4, 13, 2, 13};
2607 static const int aExplainMap
[] = {0, 1, 2, 3, 4, 5, 6, 7 };
2608 static const int aScanExpWidth
[] = {4, 15, 6, 13, 4, 4, 4, 13, 2, 13};
2609 static const int aScanExpMap
[] = {0, 9, 8, 1, 2, 3, 4, 5, 6, 7 };
2611 const int *aWidth
= aExplainWidth
;
2612 const int *aMap
= aExplainMap
;
2613 int nWidth
= ArraySize(aExplainWidth
);
2616 if( p
->cMode
==MODE_ScanExp
){
2617 aWidth
= aScanExpWidth
;
2619 nWidth
= ArraySize(aScanExpWidth
);
2622 if( nArg
>nWidth
) nArg
= nWidth
;
2624 /* If this is the first row seen, print out the headers */
2626 for(i
=0; i
<nArg
; i
++){
2627 utf8_width_print(aWidth
[i
], azCol
[ aMap
[i
] ]);
2628 oputz(i
==nArg
-1 ? "\n" : " ");
2630 for(i
=0; i
<nArg
; i
++){
2631 print_dashes(aWidth
[i
]);
2632 oputz(i
==nArg
-1 ? "\n" : " ");
2636 /* If there is no data, exit early. */
2637 if( azArg
==0 ) break;
2639 for(i
=0; i
<nArg
; i
++){
2640 const char *zSep
= " ";
2642 const char *zVal
= azArg
[ aMap
[i
] ];
2643 if( i
==nArg
-1 ) w
= 0;
2644 if( zVal
&& strlenChar(zVal
)>w
){
2645 w
= strlenChar(zVal
);
2648 if( i
==iIndent
&& p
->aiIndent
&& p
->pStmt
){
2649 if( p
->iIndent
<p
->nIndent
){
2650 oputf("%*.s", p
->aiIndent
[p
->iIndent
], "");
2654 utf8_width_print(w
, zVal
? zVal
: p
->nullValue
);
2655 oputz(i
==nArg
-1 ? "\n" : zSep
);
2659 case MODE_Semi
: { /* .schema and .fullschema output */
2660 printSchemaLine(azArg
[0], ";\n");
2663 case MODE_Pretty
: { /* .schema and .fullschema with --indent */
2671 if( azArg
[0]==0 ) break;
2672 if( sqlite3_strlike("CREATE VIEW%", azArg
[0], 0)==0
2673 || sqlite3_strlike("CREATE TRIG%", azArg
[0], 0)==0
2675 oputf("%s;\n", azArg
[0]);
2678 z
= sqlite3_mprintf("%s", azArg
[0]);
2681 for(i
=0; IsSpace(z
[i
]); i
++){}
2682 for(; (c
= z
[i
])!=0; i
++){
2684 if( z
[j
-1]=='\r' ) z
[j
-1] = '\n';
2685 if( IsSpace(z
[j
-1]) || z
[j
-1]=='(' ) continue;
2686 }else if( (c
=='(' || c
==')') && j
>0 && IsSpace(z
[j
-1]) ){
2691 while( j
>0 && IsSpace(z
[j
-1]) ){ j
--; }
2693 if( strlen30(z
)>=79 ){
2694 for(i
=j
=0; (c
= z
[i
])!=0; i
++){ /* Copy from z[i] back to z[j] */
2697 }else if( c
=='"' || c
=='\'' || c
=='`' ){
2701 }else if( c
=='-' && z
[i
+1]=='-' ){
2707 if( nLine
>0 && nParen
==0 && j
>0 ){
2708 printSchemaLineN(z
, j
, "\n");
2713 if( nParen
==1 && cEnd
==0
2714 && (c
=='(' || c
=='\n' || (c
==',' && !wsToEol(z
+i
+1)))
2717 printSchemaLineN(z
, j
, "\n ");
2720 while( IsSpace(z
[i
+1]) ){ i
++; }
2725 printSchemaLine(z
, ";\n");
2730 if( p
->cnt
++==0 && p
->showHeader
){
2731 for(i
=0; i
<nArg
; i
++){
2732 oputf("%s%s",azCol
[i
], i
==nArg
-1 ? p
->rowSeparator
: p
->colSeparator
);
2735 if( azArg
==0 ) break;
2736 for(i
=0; i
<nArg
; i
++){
2738 if( z
==0 ) z
= p
->nullValue
;
2740 oputz((i
<nArg
-1)? p
->colSeparator
: p
->rowSeparator
);
2745 if( p
->cnt
++==0 && p
->showHeader
){
2747 for(i
=0; i
<nArg
; i
++){
2749 output_html_string(azCol
[i
]);
2754 if( azArg
==0 ) break;
2756 for(i
=0; i
<nArg
; i
++){
2758 output_html_string(azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2765 if( p
->cnt
++==0 && p
->showHeader
){
2766 for(i
=0; i
<nArg
; i
++){
2767 output_c_string(azCol
[i
] ? azCol
[i
] : "");
2768 if(i
<nArg
-1) oputz(p
->colSeparator
);
2770 oputz(p
->rowSeparator
);
2772 if( azArg
==0 ) break;
2773 for(i
=0; i
<nArg
; i
++){
2774 output_c_string(azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2775 if(i
<nArg
-1) oputz(p
->colSeparator
);
2777 oputz(p
->rowSeparator
);
2781 setBinaryMode(p
->out
, 1);
2782 if( p
->cnt
++==0 && p
->showHeader
){
2783 for(i
=0; i
<nArg
; i
++){
2784 output_csv(p
, azCol
[i
] ? azCol
[i
] : "", i
<nArg
-1);
2786 oputz(p
->rowSeparator
);
2789 for(i
=0; i
<nArg
; i
++){
2790 output_csv(p
, azArg
[i
], i
<nArg
-1);
2792 oputz(p
->rowSeparator
);
2794 setTextMode(p
->out
, 1);
2798 if( azArg
==0 ) break;
2799 oputf("INSERT INTO %s",p
->zDestTable
);
2800 if( p
->showHeader
){
2802 for(i
=0; i
<nArg
; i
++){
2803 if( i
>0 ) oputz(",");
2804 if( quoteChar(azCol
[i
]) ){
2805 char *z
= sqlite3_mprintf("\"%w\"", azCol
[i
]);
2810 oputf("%s", azCol
[i
]);
2816 for(i
=0; i
<nArg
; i
++){
2817 oputz(i
>0 ? "," : " VALUES(");
2818 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2820 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2821 if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2822 output_quoted_string(azArg
[i
]);
2824 output_quoted_escaped_string(azArg
[i
]);
2826 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2828 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2830 double r
= sqlite3_column_double(p
->pStmt
, i
);
2832 memcpy(&ur
,&r
,sizeof(r
));
2833 if( ur
==0x7ff0000000000000LL
){
2835 }else if( ur
==0xfff0000000000000LL
){
2838 sqlite3_int64 ir
= (sqlite3_int64
)r
;
2839 if( r
==(double)ir
){
2840 sqlite3_snprintf(50,z
,"%lld.0", ir
);
2842 sqlite3_snprintf(50,z
,"%!.20g", r
);
2846 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2847 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2848 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2849 output_hex_blob(pBlob
, nBlob
);
2850 }else if( isNumber(azArg
[i
], 0) ){
2852 }else if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2853 output_quoted_string(azArg
[i
]);
2855 output_quoted_escaped_string(azArg
[i
]);
2862 if( azArg
==0 ) break;
2864 fputs("[{", p
->out
);
2866 fputs(",\n{", p
->out
);
2869 for(i
=0; i
<nArg
; i
++){
2870 output_json_string(azCol
[i
], -1);
2872 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2874 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2876 double r
= sqlite3_column_double(p
->pStmt
, i
);
2878 memcpy(&ur
,&r
,sizeof(r
));
2879 if( ur
==0x7ff0000000000000LL
){
2881 }else if( ur
==0xfff0000000000000LL
){
2884 sqlite3_snprintf(50,z
,"%!.20g", r
);
2887 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2888 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2889 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2890 output_json_string(pBlob
, nBlob
);
2891 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2892 output_json_string(azArg
[i
], -1);
2904 if( azArg
==0 ) break;
2905 if( p
->cnt
==0 && p
->showHeader
){
2906 for(i
=0; i
<nArg
; i
++){
2907 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
2908 output_quoted_string(azCol
[i
]);
2910 fputs(p
->rowSeparator
, p
->out
);
2913 for(i
=0; i
<nArg
; i
++){
2914 if( i
>0 ) fputs(p
->colSeparator
, p
->out
);
2915 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2917 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2918 output_quoted_string(azArg
[i
]);
2919 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2921 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2923 double r
= sqlite3_column_double(p
->pStmt
, i
);
2924 sqlite3_snprintf(50,z
,"%!.20g", r
);
2926 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2927 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2928 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2929 output_hex_blob(pBlob
, nBlob
);
2930 }else if( isNumber(azArg
[i
], 0) ){
2933 output_quoted_string(azArg
[i
]);
2936 fputs(p
->rowSeparator
, p
->out
);
2940 if( p
->cnt
++==0 && p
->showHeader
){
2941 for(i
=0; i
<nArg
; i
++){
2942 if( i
>0 ) oputz(p
->colSeparator
);
2943 oputz(azCol
[i
] ? azCol
[i
] : "");
2945 oputz(p
->rowSeparator
);
2947 if( azArg
==0 ) break;
2948 for(i
=0; i
<nArg
; i
++){
2949 if( i
>0 ) oputz(p
->colSeparator
);
2950 oputz(azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2952 oputz(p
->rowSeparator
);
2956 eqp_append(p
, atoi(azArg
[0]), atoi(azArg
[1]), azArg
[3]);
2964 ** This is the callback routine that the SQLite library
2965 ** invokes for each row of a query result.
2967 static int callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
2968 /* since we don't have type info, call the shell_callback with a NULL value */
2969 return shell_callback(pArg
, nArg
, azArg
, azCol
, NULL
);
2973 ** This is the callback routine from sqlite3_exec() that appends all
2974 ** output onto the end of a ShellText object.
2976 static int captureOutputCallback(void *pArg
, int nArg
, char **azArg
, char **az
){
2977 ShellText
*p
= (ShellText
*)pArg
;
2979 UNUSED_PARAMETER(az
);
2980 if( azArg
==0 ) return 0;
2981 if( p
->n
) appendText(p
, "|", 0);
2982 for(i
=0; i
<nArg
; i
++){
2983 if( i
) appendText(p
, ",", 0);
2984 if( azArg
[i
] ) appendText(p
, azArg
[i
], 0);
2990 ** Generate an appropriate SELFTEST table in the main database.
2992 static void createSelftestTable(ShellState
*p
){
2995 "SAVEPOINT selftest_init;\n"
2996 "CREATE TABLE IF NOT EXISTS selftest(\n"
2997 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2998 " op TEXT,\n" /* Operator: memo run */
2999 " cmd TEXT,\n" /* Command text */
3000 " ans TEXT\n" /* Desired answer */
3002 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
3003 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
3004 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
3005 " 'memo','Tests generated by --init');\n"
3006 "INSERT INTO [_shell$self]\n"
3008 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
3009 "FROM sqlite_schema ORDER BY 2'',224))',\n"
3010 " hex(sha3_query('SELECT type,name,tbl_name,sql "
3011 "FROM sqlite_schema ORDER BY 2',224));\n"
3012 "INSERT INTO [_shell$self]\n"
3014 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
3015 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
3016 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
3018 " SELECT name FROM sqlite_schema\n"
3019 " WHERE type='table'\n"
3020 " AND name<>'selftest'\n"
3021 " AND coalesce(rootpage,0)>0\n"
3024 "INSERT INTO [_shell$self]\n"
3025 " VALUES('run','PRAGMA integrity_check','ok');\n"
3026 "INSERT INTO selftest(tno,op,cmd,ans)"
3027 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
3028 "DROP TABLE [_shell$self];"
3031 eputf("SELFTEST initialization failure: %s\n", zErrMsg
);
3032 sqlite3_free(zErrMsg
);
3034 sqlite3_exec(p
->db
, "RELEASE selftest_init",0,0,0);
3039 ** Set the destination table field of the ShellState structure to
3040 ** the name of the table given. Escape any quote characters in the
3043 static void set_table_name(ShellState
*p
, const char *zName
){
3048 if( p
->zDestTable
){
3049 free(p
->zDestTable
);
3052 if( zName
==0 ) return;
3053 cQuote
= quoteChar(zName
);
3054 n
= strlen30(zName
);
3055 if( cQuote
) n
+= n
+2;
3056 z
= p
->zDestTable
= malloc( n
+1 );
3059 if( cQuote
) z
[n
++] = cQuote
;
3060 for(i
=0; zName
[i
]; i
++){
3062 if( zName
[i
]==cQuote
) z
[n
++] = cQuote
;
3064 if( cQuote
) z
[n
++] = cQuote
;
3069 ** Maybe construct two lines of text that point out the position of a
3070 ** syntax error. Return a pointer to the text, in memory obtained from
3071 ** sqlite3_malloc(). Or, if the most recent error does not involve a
3072 ** specific token that we can point to, return an empty string.
3074 ** In all cases, the memory returned is obtained from sqlite3_malloc64()
3075 ** and should be released by the caller invoking sqlite3_free().
3077 static char *shell_error_context(const char *zSql
, sqlite3
*db
){
3085 || (iOffset
= sqlite3_error_offset(db
))<0
3086 || iOffset
>=(int)strlen(zSql
)
3088 return sqlite3_mprintf("");
3090 while( iOffset
>50 ){
3093 while( (zSql
[0]&0xc0)==0x80 ){ zSql
++; iOffset
--; }
3098 while( len
>0 && (zSql
[len
]&0xc0)==0x80 ) len
--;
3100 zCode
= sqlite3_mprintf("%.*s", len
, zSql
);
3101 shell_check_oom(zCode
);
3102 for(i
=0; zCode
[i
]; i
++){ if( IsSpace(zSql
[i
]) ) zCode
[i
] = ' '; }
3104 zMsg
= sqlite3_mprintf("\n %z\n %*s^--- error here", zCode
,iOffset
,"");
3106 zMsg
= sqlite3_mprintf("\n %z\n %*serror here ---^", zCode
,iOffset
-14,"");
3113 ** Execute a query statement that will generate SQL output. Print
3114 ** the result columns, comma-separated, on a line and then add a
3115 ** semicolon terminator to the end of that line.
3117 ** If the number of columns is 1 and that column contains text "--"
3118 ** then write the semicolon on a separate line. That way, if a
3119 ** "--" comment occurs at the end of the statement, the comment
3120 ** won't consume the semicolon terminator.
3122 static int run_table_dump_query(
3123 ShellState
*p
, /* Query context */
3124 const char *zSelect
/* SELECT statement to extract content */
3126 sqlite3_stmt
*pSelect
;
3131 rc
= sqlite3_prepare_v2(p
->db
, zSelect
, -1, &pSelect
, 0);
3132 if( rc
!=SQLITE_OK
|| !pSelect
){
3133 char *zContext
= shell_error_context(zSelect
, p
->db
);
3134 oputf("/**** ERROR: (%d) %s *****/\n%s",
3135 rc
, sqlite3_errmsg(p
->db
), zContext
);
3136 sqlite3_free(zContext
);
3137 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
3140 rc
= sqlite3_step(pSelect
);
3141 nResult
= sqlite3_column_count(pSelect
);
3142 while( rc
==SQLITE_ROW
){
3143 z
= (const char*)sqlite3_column_text(pSelect
, 0);
3145 for(i
=1; i
<nResult
; i
++){
3146 oputf(",%s", sqlite3_column_text(pSelect
, i
));
3149 while( z
[0] && (z
[0]!='-' || z
[1]!='-') ) z
++;
3155 rc
= sqlite3_step(pSelect
);
3157 rc
= sqlite3_finalize(pSelect
);
3158 if( rc
!=SQLITE_OK
){
3159 oputf("/**** ERROR: (%d) %s *****/\n", rc
, sqlite3_errmsg(p
->db
));
3160 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
3166 ** Allocate space and save off string indicating current error.
3168 static char *save_err_msg(
3169 sqlite3
*db
, /* Database to query */
3170 const char *zPhase
, /* When the error occurs */
3171 int rc
, /* Error code returned from API */
3172 const char *zSql
/* SQL string, or NULL */
3176 sqlite3_str
*pStr
= sqlite3_str_new(0);
3177 sqlite3_str_appendf(pStr
, "%s, %s", zPhase
, sqlite3_errmsg(db
));
3179 sqlite3_str_appendf(pStr
, " (%d)", rc
);
3181 zContext
= shell_error_context(zSql
, db
);
3183 sqlite3_str_appendall(pStr
, zContext
);
3184 sqlite3_free(zContext
);
3186 zErr
= sqlite3_str_finish(pStr
);
3187 shell_check_oom(zErr
);
3193 ** Attempt to display I/O stats on Linux using /proc/PID/io
3195 static void displayLinuxIoStats(void){
3198 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
3199 in
= fopen(z
, "rb");
3201 while( fgets(z
, sizeof(z
), in
)!=0 ){
3202 static const struct {
3203 const char *zPattern
;
3206 { "rchar: ", "Bytes received by read():" },
3207 { "wchar: ", "Bytes sent to write():" },
3208 { "syscr: ", "Read() system calls:" },
3209 { "syscw: ", "Write() system calls:" },
3210 { "read_bytes: ", "Bytes read from storage:" },
3211 { "write_bytes: ", "Bytes written to storage:" },
3212 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
3215 for(i
=0; i
<ArraySize(aTrans
); i
++){
3216 int n
= strlen30(aTrans
[i
].zPattern
);
3217 if( cli_strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
3218 oputf("%-36s %s", aTrans
[i
].zDesc
, &z
[n
]);
3228 ** Display a single line of status using 64-bit values.
3230 static void displayStatLine(
3231 char *zLabel
, /* Label for this one line */
3232 char *zFormat
, /* Format for the result */
3233 int iStatusCtrl
, /* Which status to display */
3234 int bReset
/* True to reset the stats */
3236 sqlite3_int64 iCur
= -1;
3237 sqlite3_int64 iHiwtr
= -1;
3240 sqlite3_status64(iStatusCtrl
, &iCur
, &iHiwtr
, bReset
);
3241 for(i
=0, nPercent
=0; zFormat
[i
]; i
++){
3242 if( zFormat
[i
]=='%' ) nPercent
++;
3245 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iCur
, iHiwtr
);
3247 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iHiwtr
);
3249 oputf("%-36s %s\n", zLabel
, zLine
);
3253 ** Display memory stats.
3255 static int display_stats(
3256 sqlite3
*db
, /* Database to query */
3257 ShellState
*pArg
, /* Pointer to ShellState */
3258 int bReset
/* True to reset the stats */
3262 if( pArg
==0 || pArg
->out
==0 ) return 0;
3264 if( pArg
->pStmt
&& pArg
->statsOn
==2 ){
3266 sqlite3_stmt
*pStmt
= pArg
->pStmt
;
3268 nCol
= sqlite3_column_count(pStmt
);
3269 oputf("%-36s %d\n", "Number of output columns:", nCol
);
3270 for(i
=0; i
<nCol
; i
++){
3271 sqlite3_snprintf(sizeof(z
),z
,"Column %d %nname:", i
, &x
);
3272 oputf("%-36s %s\n", z
, sqlite3_column_name(pStmt
,i
));
3273 #ifndef SQLITE_OMIT_DECLTYPE
3274 sqlite3_snprintf(30, z
+x
, "declared type:");
3275 oputf("%-36s %s\n", z
, sqlite3_column_decltype(pStmt
, i
));
3277 #ifdef SQLITE_ENABLE_COLUMN_METADATA
3278 sqlite3_snprintf(30, z
+x
, "database name:");
3279 oputf("%-36s %s\n", z
, sqlite3_column_database_name(pStmt
,i
));
3280 sqlite3_snprintf(30, z
+x
, "table name:");
3281 oputf("%-36s %s\n", z
, sqlite3_column_table_name(pStmt
,i
));
3282 sqlite3_snprintf(30, z
+x
, "origin name:");
3283 oputf("%-36s %s\n", z
, sqlite3_column_origin_name(pStmt
,i
));
3288 if( pArg
->statsOn
==3 ){
3290 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
,bReset
);
3291 oputf("VM-steps: %d\n", iCur
);
3296 displayStatLine("Memory Used:",
3297 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED
, bReset
);
3298 displayStatLine("Number of Outstanding Allocations:",
3299 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT
, bReset
);
3300 if( pArg
->shellFlgs
& SHFLG_Pagecache
){
3301 displayStatLine("Number of Pcache Pages Used:",
3302 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED
, bReset
);
3304 displayStatLine("Number of Pcache Overflow Bytes:",
3305 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW
, bReset
);
3306 displayStatLine("Largest Allocation:",
3307 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE
, bReset
);
3308 displayStatLine("Largest Pcache Allocation:",
3309 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE
, bReset
);
3310 #ifdef YYTRACKMAXSTACKDEPTH
3311 displayStatLine("Deepest Parser Stack:",
3312 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK
, bReset
);
3316 if( pArg
->shellFlgs
& SHFLG_Lookaside
){
3318 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_USED
,
3319 &iCur
, &iHiwtr
, bReset
);
3320 oputf("Lookaside Slots Used: %d (max %d)\n", iCur
, iHiwtr
);
3321 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
,
3322 &iCur
, &iHiwtr
, bReset
);
3323 oputf("Successful lookaside attempts: %d\n", iHiwtr
);
3324 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
,
3325 &iCur
, &iHiwtr
, bReset
);
3326 oputf("Lookaside failures due to size: %d\n", iHiwtr
);
3327 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
,
3328 &iCur
, &iHiwtr
, bReset
);
3329 oputf("Lookaside failures due to OOM: %d\n", iHiwtr
);
3332 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHiwtr
, bReset
);
3333 oputf("Pager Heap Usage: %d bytes\n", iCur
);
3335 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHiwtr
, 1);
3336 oputf("Page cache hits: %d\n", iCur
);
3338 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHiwtr
, 1);
3339 oputf("Page cache misses: %d\n", iCur
);
3341 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHiwtr
, 1);
3342 oputf("Page cache writes: %d\n", iCur
);
3344 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_SPILL
, &iCur
, &iHiwtr
, 1);
3345 oputf("Page cache spills: %d\n", iCur
);
3347 sqlite3_db_status(db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHiwtr
, bReset
);
3348 oputf("Schema Heap Usage: %d bytes\n", iCur
);
3350 sqlite3_db_status(db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHiwtr
, bReset
);
3351 oputf("Statement Heap/Lookaside Usage: %d bytes\n", iCur
);
3356 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FULLSCAN_STEP
,
3358 oputf("Fullscan Steps: %d\n", iCur
);
3359 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_SORT
, bReset
);
3360 oputf("Sort Operations: %d\n", iCur
);
3361 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_AUTOINDEX
,bReset
);
3362 oputf("Autoindex Inserts: %d\n", iCur
);
3363 iHit
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_HIT
,
3365 iMiss
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FILTER_MISS
,
3367 if( iHit
|| iMiss
){
3368 oputf("Bloom filter bypass taken: %d/%d\n", iHit
, iHit
+iMiss
);
3370 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
, bReset
);
3371 oputf("Virtual Machine Steps: %d\n", iCur
);
3372 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_REPREPARE
,bReset
);
3373 oputf("Reprepare operations: %d\n", iCur
);
3374 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_RUN
, bReset
);
3375 oputf("Number of times run: %d\n", iCur
);
3376 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_MEMUSED
, bReset
);
3377 oputf("Memory used by prepared stmt: %d\n", iCur
);
3381 displayLinuxIoStats();
3384 /* Do not remove this machine readable comment: extra-stats-output-here */
3390 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3391 static int scanStatsHeight(sqlite3_stmt
*p
, int iEntry
){
3394 sqlite3_stmt_scanstatus_v2(p
, iEntry
,
3395 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
3402 res
= sqlite3_stmt_scanstatus_v2(p
, ii
,
3403 SQLITE_SCANSTAT_SELECTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iId
3407 sqlite3_stmt_scanstatus_v2(p
, ii
,
3408 SQLITE_SCANSTAT_PARENTID
, SQLITE_SCANSTAT_COMPLEX
, (void*)&iPid
3418 #ifdef SQLITE_ENABLE_STMT_SCANSTATUS
3419 static void display_explain_scanstats(
3420 sqlite3
*db
, /* Database to query */
3421 ShellState
*pArg
/* Pointer to ShellState */
3423 static const int f
= SQLITE_SCANSTAT_COMPLEX
;
3424 sqlite3_stmt
*p
= pArg
->pStmt
;
3433 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&z
) ){
3436 n
= (int)strlen(z
) + scanStatsHeight(p
, ii
)*3;
3437 if( n
>nWidth
) nWidth
= n
;
3441 sqlite3_stmt_scanstatus_v2(p
, -1, SQLITE_SCANSTAT_NCYCLE
, f
, (void*)&nTotal
);
3449 const char *zName
= 0;
3453 if( sqlite3_stmt_scanstatus_v2(p
,ii
,SQLITE_SCANSTAT_EXPLAIN
,f
,(void*)&zo
) ){
3456 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_EST
,f
,(void*)&rEst
);
3457 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NLOOP
,f
,(void*)&nLoop
);
3458 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NVISIT
,f
,(void*)&nRow
);
3459 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NCYCLE
,f
,(void*)&nCycle
);
3460 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_SELECTID
,f
,(void*)&iId
);
3461 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_PARENTID
,f
,(void*)&iPid
);
3462 sqlite3_stmt_scanstatus_v2(p
, ii
, SQLITE_SCANSTAT_NAME
,f
,(void*)&zName
);
3464 zText
= sqlite3_mprintf("%s", zo
);
3465 if( nCycle
>=0 || nLoop
>=0 || nRow
>=0 ){
3467 if( nCycle
>=0 && nTotal
>0 ){
3468 z
= sqlite3_mprintf("%zcycles=%lld [%d%%]", z
,
3469 nCycle
, ((nCycle
*100)+nTotal
/2) / nTotal
3473 z
= sqlite3_mprintf("%z%sloops=%lld", z
, z
? " " : "", nLoop
);
3476 z
= sqlite3_mprintf("%z%srows=%lld", z
, z
? " " : "", nRow
);
3479 if( zName
&& pArg
->scanstatsOn
>1 ){
3480 double rpl
= (double)nRow
/ (double)nLoop
;
3481 z
= sqlite3_mprintf("%z rpl=%.1f est=%.1f", z
, rpl
, rEst
);
3484 zText
= sqlite3_mprintf(
3485 "% *z (%z)", -1*(nWidth
-scanStatsHeight(p
, ii
)*3), zText
, z
3489 eqp_append(pArg
, iId
, iPid
, zText
);
3490 sqlite3_free(zText
);
3493 eqp_render(pArg
, nTotal
);
3499 ** Parameter azArray points to a zero-terminated array of strings. zStr
3500 ** points to a single nul-terminated string. Return non-zero if zStr
3501 ** is equal, according to strcmp(), to any of the strings in the array.
3502 ** Otherwise, return zero.
3504 static int str_in_array(const char *zStr
, const char **azArray
){
3506 for(i
=0; azArray
[i
]; i
++){
3507 if( 0==cli_strcmp(zStr
, azArray
[i
]) ) return 1;
3513 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
3514 ** and populate the ShellState.aiIndent[] array with the number of
3515 ** spaces each opcode should be indented before it is output.
3517 ** The indenting rules are:
3519 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
3520 ** all opcodes that occur between the p2 jump destination and the opcode
3521 ** itself by 2 spaces.
3523 ** * Do the previous for "Return" instructions for when P2 is positive.
3524 ** See tag-20220407a in wherecode.c and vdbe.c.
3526 ** * For each "Goto", if the jump destination is earlier in the program
3527 ** and ends on one of:
3528 ** Yield SeekGt SeekLt RowSetRead Rewind
3529 ** or if the P1 parameter is one instead of zero,
3530 ** then indent all opcodes between the earlier instruction
3531 ** and "Goto" by 2 spaces.
3533 static void explain_data_prepare(ShellState
*p
, sqlite3_stmt
*pSql
){
3534 int *abYield
= 0; /* True if op is an OP_Yield */
3535 int nAlloc
= 0; /* Allocated size of p->aiIndent[], abYield */
3536 int iOp
; /* Index of operation in p->aiIndent[] */
3538 const char *azNext
[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
3540 const char *azYield
[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
3542 const char *azGoto
[] = { "Goto", 0 };
3544 /* The caller guarantees that the leftmost 4 columns of the statement
3545 ** passed to this function are equivalent to the leftmost 4 columns
3546 ** of EXPLAIN statement output. In practice the statement may be
3547 ** an EXPLAIN, or it may be a query on the bytecode() virtual table. */
3548 assert( sqlite3_column_count(pSql
)>=4 );
3549 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 0), "addr" ) );
3550 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 1), "opcode" ) );
3551 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 2), "p1" ) );
3552 assert( 0==sqlite3_stricmp( sqlite3_column_name(pSql
, 3), "p2" ) );
3554 for(iOp
=0; SQLITE_ROW
==sqlite3_step(pSql
); iOp
++){
3556 int iAddr
= sqlite3_column_int(pSql
, 0);
3557 const char *zOp
= (const char*)sqlite3_column_text(pSql
, 1);
3558 int p1
= sqlite3_column_int(pSql
, 2);
3559 int p2
= sqlite3_column_int(pSql
, 3);
3561 /* Assuming that p2 is an instruction address, set variable p2op to the
3562 ** index of that instruction in the aiIndent[] array. p2 and p2op may be
3563 ** different if the current instruction is part of a sub-program generated
3564 ** by an SQL trigger or foreign key. */
3565 int p2op
= (p2
+ (iOp
-iAddr
));
3567 /* Grow the p->aiIndent array as required */
3570 p
->aiIndent
= (int*)sqlite3_realloc64(p
->aiIndent
, nAlloc
*sizeof(int));
3571 shell_check_oom(p
->aiIndent
);
3572 abYield
= (int*)sqlite3_realloc64(abYield
, nAlloc
*sizeof(int));
3573 shell_check_oom(abYield
);
3576 abYield
[iOp
] = str_in_array(zOp
, azYield
);
3577 p
->aiIndent
[iOp
] = 0;
3579 if( str_in_array(zOp
, azNext
) && p2op
>0 ){
3580 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
3582 if( str_in_array(zOp
, azGoto
) && p2op
<iOp
&& (abYield
[p2op
] || p1
) ){
3583 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
3588 sqlite3_free(abYield
);
3589 sqlite3_reset(pSql
);
3593 ** Free the array allocated by explain_data_prepare().
3595 static void explain_data_delete(ShellState
*p
){
3596 sqlite3_free(p
->aiIndent
);
3602 static void exec_prepared_stmt(ShellState
*, sqlite3_stmt
*);
3605 ** Display scan stats.
3607 static void display_scanstats(
3608 sqlite3
*db
, /* Database to query */
3609 ShellState
*pArg
/* Pointer to ShellState */
3611 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
3612 UNUSED_PARAMETER(db
);
3613 UNUSED_PARAMETER(pArg
);
3615 if( pArg
->scanstatsOn
==3 ){
3617 " SELECT addr, opcode, p1, p2, p3, p4, p5, comment, nexec,"
3618 " format('% 6s (%.2f%%)',"
3619 " CASE WHEN ncycle<100_000 THEN ncycle || ' '"
3620 " WHEN ncycle<100_000_000 THEN (ncycle/1_000) || 'K'"
3621 " WHEN ncycle<100_000_000_000 THEN (ncycle/1_000_000) || 'M'"
3622 " ELSE (ncycle/1000_000_000) || 'G' END,"
3623 " ncycle*100.0/(sum(ncycle) OVER ())"
3625 " FROM bytecode(?)";
3628 sqlite3_stmt
*pStmt
= 0;
3629 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
3630 if( rc
==SQLITE_OK
){
3631 sqlite3_stmt
*pSave
= pArg
->pStmt
;
3632 pArg
->pStmt
= pStmt
;
3633 sqlite3_bind_pointer(pStmt
, 1, pSave
, "stmt-pointer", 0);
3636 pArg
->cMode
= MODE_ScanExp
;
3637 explain_data_prepare(pArg
, pStmt
);
3638 exec_prepared_stmt(pArg
, pStmt
);
3639 explain_data_delete(pArg
);
3641 sqlite3_finalize(pStmt
);
3642 pArg
->pStmt
= pSave
;
3645 display_explain_scanstats(db
, pArg
);
3651 ** Disable and restore .wheretrace and .treetrace/.selecttrace settings.
3653 static unsigned int savedSelectTrace
;
3654 static unsigned int savedWhereTrace
;
3655 static void disable_debug_trace_modes(void){
3656 unsigned int zero
= 0;
3657 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 0, &savedSelectTrace
);
3658 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &zero
);
3659 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 2, &savedWhereTrace
);
3660 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &zero
);
3662 static void restore_debug_trace_modes(void){
3663 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &savedSelectTrace
);
3664 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &savedWhereTrace
);
3667 /* Create the TEMP table used to store parameter bindings */
3668 static void bind_table_init(ShellState
*p
){
3670 int defensiveMode
= 0;
3671 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, -1, &defensiveMode
);
3672 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, 0, 0);
3673 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, -1, &wrSchema
);
3674 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, 1, 0);
3676 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
3677 " key TEXT PRIMARY KEY,\n"
3681 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_WRITABLE_SCHEMA
, wrSchema
, 0);
3682 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, defensiveMode
, 0);
3686 ** Bind parameters on a prepared statement.
3688 ** Parameter bindings are taken from a TEMP table of the form:
3690 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
3693 ** No bindings occur if this table does not exist. The name of the table
3694 ** begins with "sqlite_" so that it will not collide with ordinary application
3695 ** tables. The table must be in the TEMP schema.
3697 static void bind_prepared_stmt(ShellState
*pArg
, sqlite3_stmt
*pStmt
){
3701 sqlite3_stmt
*pQ
= 0;
3703 nVar
= sqlite3_bind_parameter_count(pStmt
);
3704 if( nVar
==0 ) return; /* Nothing to do */
3705 if( sqlite3_table_column_metadata(pArg
->db
, "TEMP", "sqlite_parameters",
3706 "key", 0, 0, 0, 0, 0)!=SQLITE_OK
){
3707 rc
= SQLITE_NOTFOUND
;
3710 rc
= sqlite3_prepare_v2(pArg
->db
,
3711 "SELECT value FROM temp.sqlite_parameters"
3712 " WHERE key=?1", -1, &pQ
, 0);
3714 for(i
=1; i
<=nVar
; i
++){
3716 const char *zVar
= sqlite3_bind_parameter_name(pStmt
, i
);
3718 sqlite3_snprintf(sizeof(zNum
),zNum
,"?%d",i
);
3721 sqlite3_bind_text(pQ
, 1, zVar
, -1, SQLITE_STATIC
);
3722 if( rc
==SQLITE_OK
&& pQ
&& sqlite3_step(pQ
)==SQLITE_ROW
){
3723 sqlite3_bind_value(pStmt
, i
, sqlite3_column_value(pQ
, 0));
3725 }else if( sqlite3_strlike("_NAN", zVar
, 0)==0 ){
3726 sqlite3_bind_double(pStmt
, i
, NAN
);
3729 }else if( sqlite3_strlike("_INF", zVar
, 0)==0 ){
3730 sqlite3_bind_double(pStmt
, i
, INFINITY
);
3732 }else if( strncmp(zVar
, "$int_", 5)==0 ){
3733 sqlite3_bind_int(pStmt
, i
, atoi(&zVar
[5]));
3734 }else if( strncmp(zVar
, "$text_", 6)==0 ){
3735 size_t szVar
= strlen(zVar
);
3736 char *zBuf
= sqlite3_malloc64( szVar
-5 );
3738 memcpy(zBuf
, &zVar
[6], szVar
-5);
3739 sqlite3_bind_text64(pStmt
, i
, zBuf
, szVar
-6, sqlite3_free
, SQLITE_UTF8
);
3742 sqlite3_bind_null(pStmt
, i
);
3746 sqlite3_finalize(pQ
);
3750 ** UTF8 box-drawing characters. Imagine box lines like this:
3758 ** Each box characters has between 2 and 4 of the lines leading from
3759 ** the center. The characters are here identified by the numbers of
3760 ** their corresponding lines.
3762 #define BOX_24 "\342\224\200" /* U+2500 --- */
3763 #define BOX_13 "\342\224\202" /* U+2502 | */
3764 #define BOX_23 "\342\224\214" /* U+250c ,- */
3765 #define BOX_34 "\342\224\220" /* U+2510 -, */
3766 #define BOX_12 "\342\224\224" /* U+2514 '- */
3767 #define BOX_14 "\342\224\230" /* U+2518 -' */
3768 #define BOX_123 "\342\224\234" /* U+251c |- */
3769 #define BOX_134 "\342\224\244" /* U+2524 -| */
3770 #define BOX_234 "\342\224\254" /* U+252c -,- */
3771 #define BOX_124 "\342\224\264" /* U+2534 -'- */
3772 #define BOX_1234 "\342\224\274" /* U+253c -|- */
3774 /* Draw horizontal line N characters long using unicode box
3777 static void print_box_line(int N
){
3778 const char zDash
[] =
3779 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
3780 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
;
3781 const int nDash
= sizeof(zDash
) - 1;
3787 oputf("%.*s", N
, zDash
);
3791 ** Draw a horizontal separator for a MODE_Box table.
3793 static void print_box_row_separator(
3803 print_box_line(p
->actualWidth
[0]+2);
3804 for(i
=1; i
<nArg
; i
++){
3806 print_box_line(p
->actualWidth
[i
]+2);
3814 ** z[] is a line of text that is to be displayed the .mode box or table or
3815 ** similar tabular formats. z[] might contain control characters such
3816 ** as \n, \t, \f, or \r.
3818 ** Compute characters to display on the first line of z[]. Stop at the
3819 ** first \r, \n, or \f. Expand \t into spaces. Return a copy (obtained
3820 ** from malloc()) of that first line, which caller should free sometime.
3821 ** Write anything to display on the next line into *pzTail. If this is
3822 ** the last line, write a NULL into *pzTail. (*pzTail is not allocated.)
3824 static char *translateForDisplayAndDup(
3825 const unsigned char *z
, /* Input text to be transformed */
3826 const unsigned char **pzTail
, /* OUT: Tail of the input for next line */
3827 int mxWidth
, /* Max width. 0 means no limit */
3828 u8 bWordWrap
/* If true, avoid breaking mid-word */
3830 int i
; /* Input bytes consumed */
3831 int j
; /* Output bytes generated */
3832 int k
; /* Input bytes to be displayed */
3833 int n
; /* Output column number */
3834 unsigned char *zOut
; /* Output text */
3840 if( mxWidth
<0 ) mxWidth
= -mxWidth
;
3841 if( mxWidth
==0 ) mxWidth
= 1000000;
3844 unsigned char c
= z
[i
];
3847 int len
= decodeUtf8(&z
[i
], &u
);
3850 n
+= cli_wcwidth(u
);
3863 }while( (n
&7)!=0 && n
<mxWidth
);
3869 if( n
>=mxWidth
&& bWordWrap
){
3870 /* Perhaps try to back up to a better place to break the line */
3871 for(k
=i
; k
>i
/2; k
--){
3872 if( isspace(z
[k
-1]) ) break;
3875 for(k
=i
; k
>i
/2; k
--){
3876 if( isalnum(z
[k
-1])!=isalnum(z
[k
]) && (z
[k
]&0xc0)!=0x80 ) break;
3883 while( z
[i
]==' ' ) i
++;
3888 if( n
>=mxWidth
&& z
[i
]>=' ' ){
3890 }else if( z
[i
]=='\r' && z
[i
+1]=='\n' ){
3891 *pzTail
= z
[i
+2] ? &z
[i
+2] : 0;
3892 }else if( z
[i
]==0 || z
[i
+1]==0 ){
3897 zOut
= malloc( j
+1 );
3898 shell_check_oom(zOut
);
3901 unsigned char c
= z
[i
];
3904 int len
= decodeUtf8(&z
[i
], &u
);
3905 do{ zOut
[j
++] = z
[i
++]; }while( (--len
)>0 );
3906 n
+= cli_wcwidth(u
);
3918 }while( (n
&7)!=0 && n
<mxWidth
);
3928 /* Extract the value of the i-th current column for pStmt as an SQL literal
3929 ** value. Memory is obtained from sqlite3_malloc64() and must be freed by
3932 static char *quoted_column(sqlite3_stmt
*pStmt
, int i
){
3933 switch( sqlite3_column_type(pStmt
, i
) ){
3935 return sqlite3_mprintf("NULL");
3937 case SQLITE_INTEGER
:
3938 case SQLITE_FLOAT
: {
3939 return sqlite3_mprintf("%s",sqlite3_column_text(pStmt
,i
));
3942 return sqlite3_mprintf("%Q",sqlite3_column_text(pStmt
,i
));
3946 sqlite3_str
*pStr
= sqlite3_str_new(0);
3947 const unsigned char *a
= sqlite3_column_blob(pStmt
,i
);
3948 int n
= sqlite3_column_bytes(pStmt
,i
);
3949 sqlite3_str_append(pStr
, "x'", 2);
3951 sqlite3_str_appendf(pStr
, "%02x", a
[j
]);
3953 sqlite3_str_append(pStr
, "'", 1);
3954 return sqlite3_str_finish(pStr
);
3957 return 0; /* Not reached */
3961 ** Run a prepared statement and output the result in one of the
3962 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
3965 ** This is different from ordinary exec_prepared_stmt() in that
3966 ** it has to run the entire query and gather the results into memory
3967 ** first, in order to determine column widths, before providing
3970 static void exec_prepared_stmt_columnar(
3971 ShellState
*p
, /* Pointer to ShellState */
3972 sqlite3_stmt
*pStmt
/* Statement to run */
3974 sqlite3_int64 nRow
= 0;
3977 sqlite3_int64 nAlloc
= 0;
3979 const unsigned char *uz
;
3981 char **azQuoted
= 0;
3983 sqlite3_int64 i
, nData
;
3984 int j
, nTotal
, w
, n
;
3985 const char *colSep
= 0;
3986 const char *rowSep
= 0;
3987 const unsigned char **azNextLine
= 0;
3989 int bMultiLineRowExists
= 0;
3990 int bw
= p
->cmOpts
.bWordWrap
;
3991 const char *zEmpty
= "";
3992 const char *zShowNull
= p
->nullValue
;
3994 rc
= sqlite3_step(pStmt
);
3995 if( rc
!=SQLITE_ROW
) return;
3996 nColumn
= sqlite3_column_count(pStmt
);
3997 if( nColumn
==0 ) goto columnar_end
;
3999 if( nAlloc
<=0 ) nAlloc
= 1;
4000 azData
= sqlite3_malloc64( nAlloc
*sizeof(char*) );
4001 shell_check_oom(azData
);
4002 azNextLine
= sqlite3_malloc64( nColumn
*sizeof(char*) );
4003 shell_check_oom(azNextLine
);
4004 memset((void*)azNextLine
, 0, nColumn
*sizeof(char*) );
4005 if( p
->cmOpts
.bQuote
){
4006 azQuoted
= sqlite3_malloc64( nColumn
*sizeof(char*) );
4007 shell_check_oom(azQuoted
);
4008 memset(azQuoted
, 0, nColumn
*sizeof(char*) );
4010 abRowDiv
= sqlite3_malloc64( nAlloc
/nColumn
);
4011 shell_check_oom(abRowDiv
);
4012 if( nColumn
>p
->nWidth
){
4013 p
->colWidth
= realloc(p
->colWidth
, (nColumn
+1)*2*sizeof(int));
4014 shell_check_oom(p
->colWidth
);
4015 for(i
=p
->nWidth
; i
<nColumn
; i
++) p
->colWidth
[i
] = 0;
4016 p
->nWidth
= nColumn
;
4017 p
->actualWidth
= &p
->colWidth
[nColumn
];
4019 memset(p
->actualWidth
, 0, nColumn
*sizeof(int));
4020 for(i
=0; i
<nColumn
; i
++){
4023 p
->actualWidth
[i
] = w
;
4025 for(i
=0; i
<nColumn
; i
++){
4026 const unsigned char *zNotUsed
;
4027 int wx
= p
->colWidth
[i
];
4029 wx
= p
->cmOpts
.iWrap
;
4031 if( wx
<0 ) wx
= -wx
;
4032 uz
= (const unsigned char*)sqlite3_column_name(pStmt
,i
);
4033 if( uz
==0 ) uz
= (u8
*)"";
4034 azData
[i
] = translateForDisplayAndDup(uz
, &zNotUsed
, wx
, bw
);
4037 int useNextLine
= bNextLine
;
4039 if( (nRow
+2)*nColumn
>= nAlloc
){
4041 azData
= sqlite3_realloc64(azData
, nAlloc
*sizeof(char*));
4042 shell_check_oom(azData
);
4043 abRowDiv
= sqlite3_realloc64(abRowDiv
, nAlloc
/nColumn
);
4044 shell_check_oom(abRowDiv
);
4048 for(i
=0; i
<nColumn
; i
++){
4049 int wx
= p
->colWidth
[i
];
4051 wx
= p
->cmOpts
.iWrap
;
4053 if( wx
<0 ) wx
= -wx
;
4056 if( uz
==0 ) uz
= (u8
*)zEmpty
;
4057 }else if( p
->cmOpts
.bQuote
){
4058 sqlite3_free(azQuoted
[i
]);
4059 azQuoted
[i
] = quoted_column(pStmt
,i
);
4060 uz
= (const unsigned char*)azQuoted
[i
];
4062 uz
= (const unsigned char*)sqlite3_column_text(pStmt
,i
);
4063 if( uz
==0 ) uz
= (u8
*)zShowNull
;
4065 azData
[nRow
*nColumn
+ i
]
4066 = translateForDisplayAndDup(uz
, &azNextLine
[i
], wx
, bw
);
4067 if( azNextLine
[i
] ){
4069 abRowDiv
[nRow
-1] = 0;
4070 bMultiLineRowExists
= 1;
4073 }while( bNextLine
|| sqlite3_step(pStmt
)==SQLITE_ROW
);
4074 nTotal
= nColumn
*(nRow
+1);
4075 for(i
=0; i
<nTotal
; i
++){
4077 if( z
==0 ) z
= (char*)zEmpty
;
4080 if( n
>p
->actualWidth
[j
] ) p
->actualWidth
[j
] = n
;
4082 if( seenInterrupt
) goto columnar_end
;
4087 if( p
->showHeader
){
4088 for(i
=0; i
<nColumn
; i
++){
4089 w
= p
->actualWidth
[i
];
4090 if( p
->colWidth
[i
]<0 ) w
= -w
;
4091 utf8_width_print(w
, azData
[i
]);
4092 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
4094 for(i
=0; i
<nColumn
; i
++){
4095 print_dashes(p
->actualWidth
[i
]);
4096 fputs(i
==nColumn
-1?"\n":" ", p
->out
);
4104 print_row_separator(p
, nColumn
, "+");
4105 fputs("| ", p
->out
);
4106 for(i
=0; i
<nColumn
; i
++){
4107 w
= p
->actualWidth
[i
];
4108 n
= strlenChar(azData
[i
]);
4109 oputf("%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
4110 oputz(i
==nColumn
-1?" |\n":" | ");
4112 print_row_separator(p
, nColumn
, "+");
4115 case MODE_Markdown
: {
4118 fputs("| ", p
->out
);
4119 for(i
=0; i
<nColumn
; i
++){
4120 w
= p
->actualWidth
[i
];
4121 n
= strlenChar(azData
[i
]);
4122 oputf("%*s%s%*s", (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "");
4123 oputz(i
==nColumn
-1?" |\n":" | ");
4125 print_row_separator(p
, nColumn
, "|");
4129 colSep
= " " BOX_13
" ";
4130 rowSep
= " " BOX_13
"\n";
4131 print_box_row_separator(p
, nColumn
, BOX_23
, BOX_234
, BOX_34
);
4133 for(i
=0; i
<nColumn
; i
++){
4134 w
= p
->actualWidth
[i
];
4135 n
= strlenChar(azData
[i
]);
4137 (w
-n
)/2, "", azData
[i
], (w
-n
+1)/2, "",
4138 i
==nColumn
-1?" "BOX_13
"\n":" "BOX_13
" ");
4140 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
4144 for(i
=nColumn
, j
=0; i
<nTotal
; i
++, j
++){
4145 if( j
==0 && p
->cMode
!=MODE_Column
){
4146 oputz(p
->cMode
==MODE_Box
?BOX_13
" ":"| ");
4149 if( z
==0 ) z
= p
->nullValue
;
4150 w
= p
->actualWidth
[j
];
4151 if( p
->colWidth
[j
]<0 ) w
= -w
;
4152 utf8_width_print(w
, z
);
4155 if( bMultiLineRowExists
&& abRowDiv
[i
/nColumn
-1] && i
+1<nTotal
){
4156 if( p
->cMode
==MODE_Table
){
4157 print_row_separator(p
, nColumn
, "+");
4158 }else if( p
->cMode
==MODE_Box
){
4159 print_box_row_separator(p
, nColumn
, BOX_123
, BOX_1234
, BOX_134
);
4160 }else if( p
->cMode
==MODE_Column
){
4165 if( seenInterrupt
) goto columnar_end
;
4170 if( p
->cMode
==MODE_Table
){
4171 print_row_separator(p
, nColumn
, "+");
4172 }else if( p
->cMode
==MODE_Box
){
4173 print_box_row_separator(p
, nColumn
, BOX_12
, BOX_124
, BOX_14
);
4176 if( seenInterrupt
){
4177 oputz("Interrupt\n");
4179 nData
= (nRow
+1)*nColumn
;
4180 for(i
=0; i
<nData
; i
++){
4182 if( z
!=zEmpty
&& z
!=zShowNull
) free(azData
[i
]);
4184 sqlite3_free(azData
);
4185 sqlite3_free((void*)azNextLine
);
4186 sqlite3_free(abRowDiv
);
4188 for(i
=0; i
<nColumn
; i
++) sqlite3_free(azQuoted
[i
]);
4189 sqlite3_free(azQuoted
);
4194 ** Run a prepared statement
4196 static void exec_prepared_stmt(
4197 ShellState
*pArg
, /* Pointer to ShellState */
4198 sqlite3_stmt
*pStmt
/* Statement to run */
4201 sqlite3_uint64 nRow
= 0;
4203 if( pArg
->cMode
==MODE_Column
4204 || pArg
->cMode
==MODE_Table
4205 || pArg
->cMode
==MODE_Box
4206 || pArg
->cMode
==MODE_Markdown
4208 exec_prepared_stmt_columnar(pArg
, pStmt
);
4212 /* perform the first step. this will tell us if we
4213 ** have a result set or not and how wide it is.
4215 rc
= sqlite3_step(pStmt
);
4216 /* if we have a result set... */
4217 if( SQLITE_ROW
== rc
){
4218 /* allocate space for col name ptr, value ptr, and type */
4219 int nCol
= sqlite3_column_count(pStmt
);
4220 void *pData
= sqlite3_malloc64(3*nCol
*sizeof(const char*) + 1);
4222 shell_out_of_memory();
4224 char **azCols
= (char **)pData
; /* Names of result columns */
4225 char **azVals
= &azCols
[nCol
]; /* Results */
4226 int *aiTypes
= (int *)&azVals
[nCol
]; /* Result types */
4228 assert(sizeof(int) <= sizeof(char *));
4229 /* save off ptrs to column names */
4230 for(i
=0; i
<nCol
; i
++){
4231 azCols
[i
] = (char *)sqlite3_column_name(pStmt
, i
);
4235 /* extract the data and data types */
4236 for(i
=0; i
<nCol
; i
++){
4237 aiTypes
[i
] = x
= sqlite3_column_type(pStmt
, i
);
4240 && (pArg
->cMode
==MODE_Insert
|| pArg
->cMode
==MODE_Quote
)
4244 azVals
[i
] = (char*)sqlite3_column_text(pStmt
, i
);
4246 if( !azVals
[i
] && (aiTypes
[i
]!=SQLITE_NULL
) ){
4248 break; /* from for */
4252 /* if data and types extracted successfully... */
4253 if( SQLITE_ROW
== rc
){
4254 /* call the supplied callback with the result row data */
4255 if( shell_callback(pArg
, nCol
, azVals
, azCols
, aiTypes
) ){
4258 rc
= sqlite3_step(pStmt
);
4261 } while( SQLITE_ROW
== rc
);
4262 sqlite3_free(pData
);
4263 if( pArg
->cMode
==MODE_Json
){
4264 fputs("]\n", pArg
->out
);
4265 }else if( pArg
->cMode
==MODE_Count
){
4267 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%llu row%s\n",
4268 nRow
, nRow
!=1 ? "s" : "");
4275 #ifndef SQLITE_OMIT_VIRTUALTABLE
4277 ** This function is called to process SQL if the previous shell command
4278 ** was ".expert". It passes the SQL in the second argument directly to
4279 ** the sqlite3expert object.
4281 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4282 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4283 ** an English language error message. It is the responsibility of the
4284 ** caller to eventually free this buffer using sqlite3_free().
4286 static int expertHandleSQL(
4291 assert( pState
->expert
.pExpert
);
4292 assert( pzErr
==0 || *pzErr
==0 );
4293 return sqlite3_expert_sql(pState
->expert
.pExpert
, zSql
, pzErr
);
4297 ** This function is called either to silently clean up the object
4298 ** created by the ".expert" command (if bCancel==1), or to generate a
4299 ** report from it and then clean it up (if bCancel==0).
4301 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
4302 ** code. In this case, (*pzErr) may be set to point to a buffer containing
4303 ** an English language error message. It is the responsibility of the
4304 ** caller to eventually free this buffer using sqlite3_free().
4306 static int expertFinish(
4312 sqlite3expert
*p
= pState
->expert
.pExpert
;
4314 assert( bCancel
|| pzErr
==0 || *pzErr
==0 );
4316 int bVerbose
= pState
->expert
.bVerbose
;
4318 rc
= sqlite3_expert_analyze(p
, pzErr
);
4319 if( rc
==SQLITE_OK
){
4320 int nQuery
= sqlite3_expert_count(p
);
4324 const char *zCand
= sqlite3_expert_report(p
,0,EXPERT_REPORT_CANDIDATES
);
4325 oputz("-- Candidates -----------------------------\n");
4326 oputf("%s\n", zCand
);
4328 for(i
=0; i
<nQuery
; i
++){
4329 const char *zSql
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_SQL
);
4330 const char *zIdx
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_INDEXES
);
4331 const char *zEQP
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_PLAN
);
4332 if( zIdx
==0 ) zIdx
= "(no new indexes)\n";
4334 oputf("-- Query %d --------------------------------\n",i
+1);
4335 oputf("%s\n\n", zSql
);
4337 oputf("%s\n", zIdx
);
4338 oputf("%s\n", zEQP
);
4342 sqlite3_expert_destroy(p
);
4343 pState
->expert
.pExpert
= 0;
4348 ** Implementation of ".expert" dot command.
4350 static int expertDotCommand(
4351 ShellState
*pState
, /* Current shell tool state */
4352 char **azArg
, /* Array of arguments passed to dot command */
4353 int nArg
/* Number of entries in azArg[] */
4360 assert( pState
->expert
.pExpert
==0 );
4361 memset(&pState
->expert
, 0, sizeof(ExpertInfo
));
4363 for(i
=1; rc
==SQLITE_OK
&& i
<nArg
; i
++){
4366 if( z
[0]=='-' && z
[1]=='-' ) z
++;
4368 if( n
>=2 && 0==cli_strncmp(z
, "-verbose", n
) ){
4369 pState
->expert
.bVerbose
= 1;
4371 else if( n
>=2 && 0==cli_strncmp(z
, "-sample", n
) ){
4373 eputf("option requires an argument: %s\n", z
);
4376 iSample
= (int)integerValue(azArg
[++i
]);
4377 if( iSample
<0 || iSample
>100 ){
4378 eputf("value out of range: %s\n", azArg
[i
]);
4384 eputf("unknown option: %s\n", z
);
4389 if( rc
==SQLITE_OK
){
4390 pState
->expert
.pExpert
= sqlite3_expert_new(pState
->db
, &zErr
);
4391 if( pState
->expert
.pExpert
==0 ){
4392 eputf("sqlite3_expert_new: %s\n", zErr
? zErr
: "out of memory");
4395 sqlite3_expert_config(
4396 pState
->expert
.pExpert
, EXPERT_CONFIG_SAMPLE
, iSample
4404 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
4407 ** Execute a statement or set of statements. Print
4408 ** any result rows/columns depending on the current mode
4409 ** set via the supplied callback.
4411 ** This is very similar to SQLite's built-in sqlite3_exec()
4412 ** function except it takes a slightly different callback
4413 ** and callback data argument.
4415 static int shell_exec(
4416 ShellState
*pArg
, /* Pointer to ShellState */
4417 const char *zSql
, /* SQL to be evaluated */
4418 char **pzErrMsg
/* Error msg written here */
4420 sqlite3_stmt
*pStmt
= NULL
; /* Statement to execute. */
4421 int rc
= SQLITE_OK
; /* Return Code */
4423 const char *zLeftover
; /* Tail of unprocessed SQL */
4424 sqlite3
*db
= pArg
->db
;
4430 #ifndef SQLITE_OMIT_VIRTUALTABLE
4431 if( pArg
->expert
.pExpert
){
4432 rc
= expertHandleSQL(pArg
, zSql
, pzErrMsg
);
4433 return expertFinish(pArg
, (rc
!=SQLITE_OK
), pzErrMsg
);
4437 while( zSql
[0] && (SQLITE_OK
== rc
) ){
4438 static const char *zStmtSql
;
4439 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zLeftover
);
4440 if( SQLITE_OK
!= rc
){
4442 *pzErrMsg
= save_err_msg(db
, "in prepare", rc
, zSql
);
4446 /* this happens for a comment or white-space */
4448 while( IsSpace(zSql
[0]) ) zSql
++;
4451 zStmtSql
= sqlite3_sql(pStmt
);
4452 if( zStmtSql
==0 ) zStmtSql
= "";
4453 while( IsSpace(zStmtSql
[0]) ) zStmtSql
++;
4455 /* save off the prepared statement handle and reset row count */
4457 pArg
->pStmt
= pStmt
;
4461 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
4462 if( pArg
&& pArg
->autoEQP
&& sqlite3_stmt_isexplain(pStmt
)==0 ){
4463 sqlite3_stmt
*pExplain
;
4465 disable_debug_trace_modes();
4466 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, -1, &triggerEQP
);
4467 if( pArg
->autoEQP
>=AUTOEQP_trigger
){
4468 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, 0);
4471 sqlite3_reset(pExplain
);
4472 rc
= sqlite3_stmt_explain(pExplain
, 2);
4473 if( rc
==SQLITE_OK
){
4474 bind_prepared_stmt(pArg
, pExplain
);
4475 while( sqlite3_step(pExplain
)==SQLITE_ROW
){
4476 const char *zEQPLine
= (const char*)sqlite3_column_text(pExplain
,3);
4477 int iEqpId
= sqlite3_column_int(pExplain
, 0);
4478 int iParentId
= sqlite3_column_int(pExplain
, 1);
4479 if( zEQPLine
==0 ) zEQPLine
= "";
4480 if( zEQPLine
[0]=='-' ) eqp_render(pArg
, 0);
4481 eqp_append(pArg
, iEqpId
, iParentId
, zEQPLine
);
4483 eqp_render(pArg
, 0);
4485 if( pArg
->autoEQP
>=AUTOEQP_full
){
4486 /* Also do an EXPLAIN for ".eqp full" mode */
4487 sqlite3_reset(pExplain
);
4488 rc
= sqlite3_stmt_explain(pExplain
, 1);
4489 if( rc
==SQLITE_OK
){
4490 pArg
->cMode
= MODE_Explain
;
4491 assert( sqlite3_stmt_isexplain(pExplain
)==1 );
4492 bind_prepared_stmt(pArg
, pExplain
);
4493 explain_data_prepare(pArg
, pExplain
);
4494 exec_prepared_stmt(pArg
, pExplain
);
4495 explain_data_delete(pArg
);
4498 if( pArg
->autoEQP
>=AUTOEQP_trigger
&& triggerEQP
==0 ){
4499 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 0, 0);
4501 sqlite3_reset(pStmt
);
4502 sqlite3_stmt_explain(pStmt
, 0);
4503 restore_debug_trace_modes();
4507 int bIsExplain
= (sqlite3_stmt_isexplain(pStmt
)==1);
4508 pArg
->cMode
= pArg
->mode
;
4509 if( pArg
->autoExplain
){
4511 pArg
->cMode
= MODE_Explain
;
4513 if( sqlite3_stmt_isexplain(pStmt
)==2 ){
4514 pArg
->cMode
= MODE_EQP
;
4518 /* If the shell is currently in ".explain" mode, gather the extra
4519 ** data required to add indents to the output.*/
4520 if( pArg
->cMode
==MODE_Explain
&& bIsExplain
){
4521 explain_data_prepare(pArg
, pStmt
);
4525 bind_prepared_stmt(pArg
, pStmt
);
4526 exec_prepared_stmt(pArg
, pStmt
);
4527 explain_data_delete(pArg
);
4528 eqp_render(pArg
, 0);
4530 /* print usage stats if stats on */
4531 if( pArg
&& pArg
->statsOn
){
4532 display_stats(db
, pArg
, 0);
4535 /* print loop-counters if required */
4536 if( pArg
&& pArg
->scanstatsOn
){
4537 display_scanstats(db
, pArg
);
4540 /* Finalize the statement just executed. If this fails, save a
4541 ** copy of the error message. Otherwise, set zSql to point to the
4542 ** next statement to execute. */
4543 rc2
= sqlite3_finalize(pStmt
);
4544 if( rc
!=SQLITE_NOMEM
) rc
= rc2
;
4545 if( rc
==SQLITE_OK
){
4547 while( IsSpace(zSql
[0]) ) zSql
++;
4548 }else if( pzErrMsg
){
4549 *pzErrMsg
= save_err_msg(db
, "stepping", rc
, 0);
4552 /* clear saved stmt handle */
4563 ** Release memory previously allocated by tableColumnList().
4565 static void freeColumnList(char **azCol
){
4567 for(i
=1; azCol
[i
]; i
++){
4568 sqlite3_free(azCol
[i
]);
4570 /* azCol[0] is a static string */
4571 sqlite3_free(azCol
);
4575 ** Return a list of pointers to strings which are the names of all
4576 ** columns in table zTab. The memory to hold the names is dynamically
4577 ** allocated and must be released by the caller using a subsequent call
4578 ** to freeColumnList().
4580 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
4581 ** value that needs to be preserved, then azCol[0] is filled in with the
4582 ** name of the rowid column.
4584 ** The first regular column in the table is azCol[1]. The list is terminated
4585 ** by an entry with azCol[i]==0.
4587 static char **tableColumnList(ShellState
*p
, const char *zTab
){
4589 sqlite3_stmt
*pStmt
;
4593 int nPK
= 0; /* Number of PRIMARY KEY columns seen */
4594 int isIPK
= 0; /* True if one PRIMARY KEY column of type INTEGER */
4595 int preserveRowid
= ShellHasFlag(p
, SHFLG_PreserveRowid
);
4598 zSql
= sqlite3_mprintf("PRAGMA table_info=%Q", zTab
);
4599 shell_check_oom(zSql
);
4600 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
4603 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
4604 if( nCol
>=nAlloc
-2 ){
4605 nAlloc
= nAlloc
*2 + nCol
+ 10;
4606 azCol
= sqlite3_realloc(azCol
, nAlloc
*sizeof(azCol
[0]));
4607 shell_check_oom(azCol
);
4609 azCol
[++nCol
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 1));
4610 shell_check_oom(azCol
[nCol
]);
4611 if( sqlite3_column_int(pStmt
, 5) ){
4614 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt
,2),
4623 sqlite3_finalize(pStmt
);
4624 if( azCol
==0 ) return 0;
4628 /* The decision of whether or not a rowid really needs to be preserved
4629 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
4630 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
4631 ** rowids on tables where the rowid is inaccessible because there are other
4632 ** columns in the table named "rowid", "_rowid_", and "oid".
4634 if( preserveRowid
&& isIPK
){
4635 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
4636 ** might be an alias for the ROWID. But it might also be a WITHOUT ROWID
4637 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
4638 ** ROWID aliases. To distinguish these cases, check to see if
4639 ** there is a "pk" entry in "PRAGMA index_list". There will be
4640 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
4642 zSql
= sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
4643 " WHERE origin='pk'", zTab
);
4644 shell_check_oom(zSql
);
4645 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
4648 freeColumnList(azCol
);
4651 rc
= sqlite3_step(pStmt
);
4652 sqlite3_finalize(pStmt
);
4653 preserveRowid
= rc
==SQLITE_ROW
;
4655 if( preserveRowid
){
4656 /* Only preserve the rowid if we can find a name to use for the
4658 static char *azRowid
[] = { "rowid", "_rowid_", "oid" };
4661 for(i
=1; i
<=nCol
; i
++){
4662 if( sqlite3_stricmp(azRowid
[j
],azCol
[i
])==0 ) break;
4665 /* At this point, we know that azRowid[j] is not the name of any
4666 ** ordinary column in the table. Verify that azRowid[j] is a valid
4667 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
4668 ** tables will fail this last check */
4669 rc
= sqlite3_table_column_metadata(p
->db
,0,zTab
,azRowid
[j
],0,0,0,0,0);
4670 if( rc
==SQLITE_OK
) azCol
[0] = azRowid
[j
];
4679 ** Toggle the reverse_unordered_selects setting.
4681 static void toggleSelectOrder(sqlite3
*db
){
4682 sqlite3_stmt
*pStmt
= 0;
4685 sqlite3_prepare_v2(db
, "PRAGMA reverse_unordered_selects", -1, &pStmt
, 0);
4686 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
4687 iSetting
= sqlite3_column_int(pStmt
, 0);
4689 sqlite3_finalize(pStmt
);
4690 sqlite3_snprintf(sizeof(zStmt
), zStmt
,
4691 "PRAGMA reverse_unordered_selects(%d)", !iSetting
);
4692 sqlite3_exec(db
, zStmt
, 0, 0, 0);
4696 ** This is a different callback routine used for dumping the database.
4697 ** Each row received by this callback consists of a table name,
4698 ** the table type ("index" or "table") and SQL to create the table.
4699 ** This routine should print text sufficient to recreate the table.
4701 static int dump_callback(void *pArg
, int nArg
, char **azArg
, char **azNotUsed
){
4706 ShellState
*p
= (ShellState
*)pArg
;
4710 UNUSED_PARAMETER(azNotUsed
);
4711 if( nArg
!=3 || azArg
==0 ) return 0;
4715 if( zTable
==0 ) return 0;
4716 if( zType
==0 ) return 0;
4717 dataOnly
= (p
->shellFlgs
& SHFLG_DumpDataOnly
)!=0;
4718 noSys
= (p
->shellFlgs
& SHFLG_DumpNoSys
)!=0;
4720 if( cli_strcmp(zTable
, "sqlite_sequence")==0 && !noSys
){
4721 if( !dataOnly
) oputz("DELETE FROM sqlite_sequence;\n");
4722 }else if( sqlite3_strglob("sqlite_stat?", zTable
)==0 && !noSys
){
4723 if( !dataOnly
) oputz("ANALYZE sqlite_schema;\n");
4724 }else if( cli_strncmp(zTable
, "sqlite_", 7)==0 ){
4726 }else if( dataOnly
){
4728 }else if( cli_strncmp(zSql
, "CREATE VIRTUAL TABLE", 20)==0 ){
4730 if( !p
->writableSchema
){
4731 oputz("PRAGMA writable_schema=ON;\n");
4732 p
->writableSchema
= 1;
4734 zIns
= sqlite3_mprintf(
4735 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
4736 "VALUES('table','%q','%q',0,'%q');",
4737 zTable
, zTable
, zSql
);
4738 shell_check_oom(zIns
);
4739 oputf("%s\n", zIns
);
4743 printSchemaLine(zSql
, ";\n");
4746 if( cli_strcmp(zType
, "table")==0 ){
4751 char *savedDestTable
;
4754 azCol
= tableColumnList(p
, zTable
);
4760 /* Always quote the table name, even if it appears to be pure ascii,
4761 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
4763 appendText(&sTable
, zTable
, quoteChar(zTable
));
4764 /* If preserving the rowid, add a column list after the table name.
4765 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
4766 ** instead of the usual "INSERT INTO tab VALUES(...)".
4769 appendText(&sTable
, "(", 0);
4770 appendText(&sTable
, azCol
[0], 0);
4771 for(i
=1; azCol
[i
]; i
++){
4772 appendText(&sTable
, ",", 0);
4773 appendText(&sTable
, azCol
[i
], quoteChar(azCol
[i
]));
4775 appendText(&sTable
, ")", 0);
4778 /* Build an appropriate SELECT statement */
4780 appendText(&sSelect
, "SELECT ", 0);
4782 appendText(&sSelect
, azCol
[0], 0);
4783 appendText(&sSelect
, ",", 0);
4785 for(i
=1; azCol
[i
]; i
++){
4786 appendText(&sSelect
, azCol
[i
], quoteChar(azCol
[i
]));
4788 appendText(&sSelect
, ",", 0);
4791 freeColumnList(azCol
);
4792 appendText(&sSelect
, " FROM ", 0);
4793 appendText(&sSelect
, zTable
, quoteChar(zTable
));
4795 savedDestTable
= p
->zDestTable
;
4796 savedMode
= p
->mode
;
4797 p
->zDestTable
= sTable
.z
;
4798 p
->mode
= p
->cMode
= MODE_Insert
;
4799 rc
= shell_exec(p
, sSelect
.z
, 0);
4800 if( (rc
&0xff)==SQLITE_CORRUPT
){
4801 oputz("/****** CORRUPTION ERROR *******/\n");
4802 toggleSelectOrder(p
->db
);
4803 shell_exec(p
, sSelect
.z
, 0);
4804 toggleSelectOrder(p
->db
);
4806 p
->zDestTable
= savedDestTable
;
4807 p
->mode
= savedMode
;
4816 ** Run zQuery. Use dump_callback() as the callback routine so that
4817 ** the contents of the query are output as SQL statements.
4819 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
4820 ** "ORDER BY rowid DESC" to the end.
4822 static int run_schema_dump_query(
4828 rc
= sqlite3_exec(p
->db
, zQuery
, dump_callback
, p
, &zErr
);
4829 if( rc
==SQLITE_CORRUPT
){
4831 int len
= strlen30(zQuery
);
4832 oputz("/****** CORRUPTION ERROR *******/\n");
4834 oputf("/****** %s ******/\n", zErr
);
4838 zQ2
= malloc( len
+100 );
4839 if( zQ2
==0 ) return rc
;
4840 sqlite3_snprintf(len
+100, zQ2
, "%s ORDER BY rowid DESC", zQuery
);
4841 rc
= sqlite3_exec(p
->db
, zQ2
, dump_callback
, p
, &zErr
);
4843 oputf("/****** ERROR: %s ******/\n", zErr
);
4845 rc
= SQLITE_CORRUPT
;
4854 ** Text of help messages.
4856 ** The help text for each individual command begins with a line that starts
4857 ** with ".". Subsequent lines are supplemental information.
4859 ** There must be two or more spaces between the end of the command and the
4860 ** start of the description of what that command does.
4862 static const char *(azHelp
[]) = {
4863 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE) \
4864 && !defined(SQLITE_SHELL_FIDDLE)
4865 ".archive ... Manage SQL archives",
4866 " Each command must have exactly one of the following options:",
4867 " -c, --create Create a new archive",
4868 " -u, --update Add or update files with changed mtime",
4869 " -i, --insert Like -u but always add even if unchanged",
4870 " -r, --remove Remove files from archive",
4871 " -t, --list List contents of archive",
4872 " -x, --extract Extract files from archive",
4873 " Optional arguments:",
4874 " -v, --verbose Print each filename as it is processed",
4875 " -f FILE, --file FILE Use archive FILE (default is current db)",
4876 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
4877 " -C DIR, --directory DIR Read/extract files from directory DIR",
4878 " -g, --glob Use glob matching for names in archive",
4879 " -n, --dryrun Show the SQL that would have occurred",
4881 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
4882 " .ar -tf ARCHIVE # List members of ARCHIVE",
4883 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
4885 " http://sqlite.org/cli.html#sqlite_archive_support",
4887 #ifndef SQLITE_OMIT_AUTHORIZATION
4888 ".auth ON|OFF Show authorizer callbacks",
4890 #ifndef SQLITE_SHELL_FIDDLE
4891 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
4893 " --append Use the appendvfs",
4894 " --async Write to FILE without journal and fsync()",
4896 ".bail on|off Stop after hitting an error. Default OFF",
4897 #ifndef SQLITE_SHELL_FIDDLE
4898 ".cd DIRECTORY Change the working directory to DIRECTORY",
4900 ".changes on|off Show number of rows changed by SQL",
4901 #ifndef SQLITE_SHELL_FIDDLE
4902 ".check GLOB Fail if output since .testcase does not match",
4903 ".clone NEWDB Clone data into NEWDB from the existing database",
4905 ".connection [close] [#] Open or close an auxiliary database connection",
4906 #if defined(_WIN32) || defined(WIN32)
4907 ".crnl on|off Translate \\n to \\r\\n. Default ON",
4909 ".databases List names and files of attached databases",
4910 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
4911 #if SQLITE_SHELL_HAVE_RECOVER
4912 ".dbinfo ?DB? Show status information about the database",
4914 ".dump ?OBJECTS? Render database content as SQL",
4916 " --data-only Output only INSERT statements",
4917 " --newlines Allow unescaped newline characters in output",
4918 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
4919 " --preserve-rowids Include ROWID values in the output",
4920 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
4921 " Additional LIKE patterns can be given in subsequent arguments",
4922 ".echo on|off Turn command echo on or off",
4923 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
4926 " test Show raw EXPLAIN QUERY PLAN output",
4927 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
4929 " trigger Like \"full\" but also show trigger bytecode",
4930 #ifndef SQLITE_SHELL_FIDDLE
4931 ".excel Display the output of next command in spreadsheet",
4932 " --bom Put a UTF8 byte-order mark on intermediate file",
4934 #ifndef SQLITE_SHELL_FIDDLE
4935 ".exit ?CODE? Exit this program with return-code CODE",
4937 ".expert EXPERIMENTAL. Suggest indexes for queries",
4938 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
4939 ".filectrl CMD ... Run various sqlite3_file_control() operations",
4940 " --schema SCHEMA Use SCHEMA instead of \"main\"",
4941 " --help Show CMD details",
4942 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
4943 ".headers on|off Turn display of headers on or off",
4944 ".help ?-all? ?PATTERN? Show help text for PATTERN",
4945 #ifndef SQLITE_SHELL_FIDDLE
4946 ".import FILE TABLE Import data from FILE into TABLE",
4948 " --ascii Use \\037 and \\036 as column and row separators",
4949 " --csv Use , and \\n as column and row separators",
4950 " --skip N Skip the first N rows of input",
4951 " --schema S Target table to be S.TABLE",
4952 " -v \"Verbose\" - increase auxiliary output",
4954 " * If TABLE does not exist, it is created. The first row of input",
4955 " determines the column names.",
4956 " * If neither --csv or --ascii are used, the input mode is derived",
4957 " from the \".mode\" output mode",
4958 " * If FILE begins with \"|\" then it is a command that generates the",
4961 #ifndef SQLITE_OMIT_TEST_CONTROL
4962 ",imposter INDEX TABLE Create imposter table TABLE on index INDEX",
4964 ".indexes ?TABLE? Show names of indexes",
4965 " If TABLE is specified, only show indexes for",
4966 " tables matching TABLE using the LIKE operator.",
4967 ".intck ?STEPS_PER_UNLOCK? Run an incremental integrity check on the db",
4968 #ifdef SQLITE_ENABLE_IOTRACE
4969 ",iotrace FILE Enable I/O diagnostic logging to FILE",
4971 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
4972 ".lint OPTIONS Report potential schema issues.",
4974 " fkey-indexes Find missing foreign key indexes",
4975 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
4976 ".load FILE ?ENTRY? Load an extension library",
4978 #if !defined(SQLITE_SHELL_FIDDLE)
4979 ".log FILE|on|off Turn logging on or off. FILE can be stderr/stdout",
4981 ".log on|off Turn logging on or off.",
4983 ".mode MODE ?OPTIONS? Set output mode",
4985 " ascii Columns/rows delimited by 0x1F and 0x1E",
4986 " box Tables using unicode box-drawing characters",
4987 " csv Comma-separated values",
4988 " column Output in columns. (See .width)",
4989 " html HTML <table> code",
4990 " insert SQL insert statements for TABLE",
4991 " json Results in a JSON array",
4992 " line One value per line",
4993 " list Values delimited by \"|\"",
4994 " markdown Markdown table format",
4995 " qbox Shorthand for \"box --wrap 60 --quote\"",
4996 " quote Escape answers as for SQL",
4997 " table ASCII-art table",
4998 " tabs Tab-separated values",
4999 " tcl TCL list elements",
5000 " OPTIONS: (for columnar modes or insert mode):",
5001 " --wrap N Wrap output lines to no longer than N characters",
5002 " --wordwrap B Wrap or not at word boundaries per B (on/off)",
5003 " --ww Shorthand for \"--wordwrap 1\"",
5004 " --quote Quote output text as SQL literals",
5005 " --noquote Do not quote output text",
5006 " TABLE The name of SQL table used for \"insert\" mode",
5007 #ifndef SQLITE_SHELL_FIDDLE
5008 ".nonce STRING Suspend safe mode for one command if nonce matches",
5010 ".nullvalue STRING Use STRING in place of NULL values",
5011 #ifndef SQLITE_SHELL_FIDDLE
5012 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
5013 " If FILE begins with '|' then open as a pipe",
5014 " --bom Put a UTF8 byte-order mark at the beginning",
5015 " -e Send output to the system text editor",
5016 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
5017 /* Note that .open is (partially) available in WASM builds but is
5018 ** currently only intended to be used by the fiddle tool, not
5019 ** end users, so is "undocumented." */
5020 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
5022 " --append Use appendvfs to append database to the end of FILE",
5024 #ifndef SQLITE_OMIT_DESERIALIZE
5025 " --deserialize Load into memory using sqlite3_deserialize()",
5026 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
5027 " --maxsize N Maximum size for --hexdb or --deserialized database",
5029 " --new Initialize FILE to an empty database",
5030 " --nofollow Do not follow symbolic links",
5031 " --readonly Open FILE readonly",
5032 " --zip FILE is a ZIP archive",
5033 #ifndef SQLITE_SHELL_FIDDLE
5034 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
5035 " If FILE begins with '|' then open it as a pipe.",
5037 " --bom Prefix output with a UTF8 byte-order mark",
5038 " -e Send output to the system text editor",
5039 " -x Send output as CSV to a spreadsheet",
5041 ".parameter CMD ... Manage SQL parameter bindings",
5042 " clear Erase all bindings",
5043 " init Initialize the TEMP table that holds bindings",
5044 " list List the current parameter bindings",
5045 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
5046 " PARAMETER should start with one of: $ : @ ?",
5047 " unset PARAMETER Remove PARAMETER from the binding table",
5048 ".print STRING... Print literal STRING",
5049 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
5050 ".progress N Invoke progress handler after every N opcodes",
5051 " --limit N Interrupt after N progress callbacks",
5052 " --once Do no more than one progress interrupt",
5053 " --quiet|-q No output except at interrupts",
5054 " --reset Reset the count for each input and interrupt",
5056 ".prompt MAIN CONTINUE Replace the standard prompts",
5057 #ifndef SQLITE_SHELL_FIDDLE
5058 ".quit Stop interpreting input stream, exit if primary.",
5059 ".read FILE Read input from FILE or command output",
5060 " If FILE begins with \"|\", it is a command that generates the input.",
5062 #if SQLITE_SHELL_HAVE_RECOVER
5063 ".recover Recover as much data as possible from corrupt db.",
5064 " --ignore-freelist Ignore pages that appear to be on db freelist",
5065 " --lost-and-found TABLE Alternative name for the lost-and-found table",
5066 " --no-rowids Do not attempt to recover rowid values",
5067 " that are not also INTEGER PRIMARY KEYs",
5069 #ifndef SQLITE_SHELL_FIDDLE
5070 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
5071 ".save ?OPTIONS? FILE Write database to FILE (an alias for .backup ...)",
5073 ".scanstats on|off|est Turn sqlite3_stmt_scanstatus() metrics on or off",
5074 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
5076 " --indent Try to pretty-print the schema",
5077 " --nosys Omit objects whose names start with \"sqlite_\"",
5078 ",selftest ?OPTIONS? Run tests defined in the SELFTEST table",
5080 " --init Create a new SELFTEST table",
5081 " -v Verbose output",
5082 ".separator COL ?ROW? Change the column and row separators",
5083 #if defined(SQLITE_ENABLE_SESSION)
5084 ".session ?NAME? CMD ... Create or control sessions",
5086 " attach TABLE Attach TABLE",
5087 " changeset FILE Write a changeset into FILE",
5088 " close Close one session",
5089 " enable ?BOOLEAN? Set or query the enable bit",
5090 " filter GLOB... Reject tables matching GLOBs",
5091 " indirect ?BOOLEAN? Mark or query the indirect status",
5092 " isempty Query whether the session is empty",
5093 " list List currently open session names",
5094 " open DB NAME Open a new session on DB",
5095 " patchset FILE Write a patchset into FILE",
5096 " If ?NAME? is omitted, the first defined session is used.",
5098 ".sha3sum ... Compute a SHA3 hash of database content",
5100 " --schema Also hash the sqlite_schema table",
5101 " --sha3-224 Use the sha3-224 algorithm",
5102 " --sha3-256 Use the sha3-256 algorithm (default)",
5103 " --sha3-384 Use the sha3-384 algorithm",
5104 " --sha3-512 Use the sha3-512 algorithm",
5105 " Any other argument is a LIKE pattern for tables to hash",
5106 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5107 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
5109 ".show Show the current values for various settings",
5110 ".stats ?ARG? Show stats or turn stats on or off",
5111 " off Turn off automatic stat display",
5112 " on Turn on automatic stat display",
5113 " stmt Show statement stats",
5114 " vmstep Show the virtual machine step count only",
5115 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
5116 ".system CMD ARGS... Run CMD ARGS... in a system shell",
5118 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
5119 #ifndef SQLITE_SHELL_FIDDLE
5120 ",testcase NAME Begin redirecting output to 'testcase-out.txt'",
5122 ",testctrl CMD ... Run various sqlite3_test_control() operations",
5123 " Run \".testctrl\" with no arguments for details",
5124 ".timeout MS Try opening locked tables for MS milliseconds",
5125 ".timer on|off Turn SQL timer on or off",
5126 #ifndef SQLITE_OMIT_TRACE
5127 ".trace ?OPTIONS? Output each SQL statement as it is run",
5128 " FILE Send output to FILE",
5129 " stdout Send output to stdout",
5130 " stderr Send output to stderr",
5131 " off Disable tracing",
5132 " --expanded Expand query parameters",
5133 #ifdef SQLITE_ENABLE_NORMALIZE
5134 " --normalized Normal the SQL statements",
5136 " --plain Show SQL as it is input",
5137 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
5138 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
5139 " --row Trace each row (SQLITE_TRACE_ROW)",
5140 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
5141 #endif /* SQLITE_OMIT_TRACE */
5143 ".unmodule NAME ... Unregister virtual table modules",
5144 " --allexcept Unregister everything except those named",
5146 ".version Show source, library and compiler versions",
5147 ".vfsinfo ?AUX? Information about the top-level VFS",
5148 ".vfslist List all available VFSes",
5149 ".vfsname ?AUX? Print the name of the VFS stack",
5150 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
5151 " Negative values right-justify",
5155 ** Output help text.
5157 ** zPattern describes the set of commands for which help text is provided.
5158 ** If zPattern is NULL, then show all commands, but only give a one-line
5159 ** description of each.
5161 ** Return the number of matches.
5163 static int showHelp(FILE *out
, const char *zPattern
){
5170 || cli_strcmp(zPattern
,"-a")==0
5171 || cli_strcmp(zPattern
,"-all")==0
5172 || cli_strcmp(zPattern
,"--all")==0
5174 enum HelpWanted
{ HW_NoCull
= 0, HW_SummaryOnly
= 1, HW_Undoc
= 2 };
5175 enum HelpHave
{ HH_Undoc
= 2, HH_Summary
= 1, HH_More
= 0 };
5176 /* Show all or most commands
5177 ** *zPattern==0 => summary of documented commands only
5178 ** *zPattern=='0' => whole help for undocumented commands
5179 ** Otherwise => whole help for documented commands
5181 enum HelpWanted hw
= HW_SummaryOnly
;
5182 enum HelpHave hh
= HH_More
;
5184 hw
= (*zPattern
=='0')? HW_NoCull
|HW_Undoc
: HW_NoCull
;
5186 for(i
=0; i
<ArraySize(azHelp
); i
++){
5187 switch( azHelp
[i
][0] ){
5189 hh
= HH_Summary
|HH_Undoc
;
5198 if( ((hw
^hh
)&HH_Undoc
)==0 ){
5199 if( (hh
&HH_Summary
)!=0 ){
5200 sputf(out
, ".%s\n", azHelp
[i
]+1);
5202 }else if( (hw
&HW_SummaryOnly
)==0 ){
5203 sputf(out
, "%s\n", azHelp
[i
]);
5208 /* Seek documented commands for which zPattern is an exact prefix */
5209 zPat
= sqlite3_mprintf(".%s*", zPattern
);
5210 shell_check_oom(zPat
);
5211 for(i
=0; i
<ArraySize(azHelp
); i
++){
5212 if( sqlite3_strglob(zPat
, azHelp
[i
])==0 ){
5213 sputf(out
, "%s\n", azHelp
[i
]);
5221 /* when zPattern is a prefix of exactly one command, then include
5222 ** the details of that command, which should begin at offset j */
5223 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
][0]==' ' ){
5224 sputf(out
, "%s\n", azHelp
[j
]);
5230 /* Look for documented commands that contain zPattern anywhere.
5231 ** Show complete text of all documented commands that match. */
5232 zPat
= sqlite3_mprintf("%%%s%%", zPattern
);
5233 shell_check_oom(zPat
);
5234 for(i
=0; i
<ArraySize(azHelp
); i
++){
5235 if( azHelp
[i
][0]==',' ){
5236 while( i
<ArraySize(azHelp
)-1 && azHelp
[i
+1][0]==' ' ) ++i
;
5239 if( azHelp
[i
][0]=='.' ) j
= i
;
5240 if( sqlite3_strlike(zPat
, azHelp
[i
], 0)==0 ){
5241 sputf(out
, "%s\n", azHelp
[j
]);
5242 while( j
<ArraySize(azHelp
)-1 && azHelp
[j
+1][0]==' ' ){
5244 sputf(out
, "%s\n", azHelp
[j
]);
5255 /* Forward reference */
5256 static int process_input(ShellState
*p
);
5259 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
5260 ** and return a pointer to the buffer. The caller is responsible for freeing
5263 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
5266 ** For convenience, a nul-terminator byte is always appended to the data read
5267 ** from the file before the buffer is returned. This byte is not included in
5268 ** the final value of (*pnByte), if applicable.
5270 ** NULL is returned if any error is encountered. The final value of *pnByte
5271 ** is undefined in this case.
5273 static char *readFile(const char *zName
, int *pnByte
){
5274 FILE *in
= fopen(zName
, "rb");
5279 if( in
==0 ) return 0;
5280 rc
= fseek(in
, 0, SEEK_END
);
5282 eputf("Error: '%s' not seekable\n", zName
);
5288 pBuf
= sqlite3_malloc64( nIn
+1 );
5290 eputz("Error: out of memory\n");
5294 nRead
= fread(pBuf
, nIn
, 1, in
);
5298 eputf("Error: cannot read '%s'\n", zName
);
5302 if( pnByte
) *pnByte
= nIn
;
5306 #if defined(SQLITE_ENABLE_SESSION)
5308 ** Close a single OpenSession object and release all of its associated
5311 static void session_close(OpenSession
*pSession
){
5313 sqlite3session_delete(pSession
->p
);
5314 sqlite3_free(pSession
->zName
);
5315 for(i
=0; i
<pSession
->nFilter
; i
++){
5316 sqlite3_free(pSession
->azFilter
[i
]);
5318 sqlite3_free(pSession
->azFilter
);
5319 memset(pSession
, 0, sizeof(OpenSession
));
5324 ** Close all OpenSession objects and release all associated resources.
5326 #if defined(SQLITE_ENABLE_SESSION)
5327 static void session_close_all(ShellState
*p
, int i
){
5329 struct AuxDb
*pAuxDb
= i
<0 ? p
->pAuxDb
: &p
->aAuxDb
[i
];
5330 for(j
=0; j
<pAuxDb
->nSession
; j
++){
5331 session_close(&pAuxDb
->aSession
[j
]);
5333 pAuxDb
->nSession
= 0;
5336 # define session_close_all(X,Y)
5340 ** Implementation of the xFilter function for an open session. Omit
5341 ** any tables named by ".session filter" but let all other table through.
5343 #if defined(SQLITE_ENABLE_SESSION)
5344 static int session_filter(void *pCtx
, const char *zTab
){
5345 OpenSession
*pSession
= (OpenSession
*)pCtx
;
5347 for(i
=0; i
<pSession
->nFilter
; i
++){
5348 if( sqlite3_strglob(pSession
->azFilter
[i
], zTab
)==0 ) return 0;
5355 ** Try to deduce the type of file for zName based on its content. Return
5356 ** one of the SHELL_OPEN_* constants.
5358 ** If the file does not exist or is empty but its name looks like a ZIP
5359 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
5360 ** Otherwise, assume an ordinary database regardless of the filename if
5361 ** the type cannot be determined from content.
5363 int deduceDatabaseType(const char *zName
, int dfltZip
){
5364 FILE *f
= fopen(zName
, "rb");
5366 int rc
= SHELL_OPEN_UNSPEC
;
5369 if( dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
5370 return SHELL_OPEN_ZIPFILE
;
5372 return SHELL_OPEN_NORMAL
;
5375 n
= fread(zBuf
, 16, 1, f
);
5376 if( n
==1 && memcmp(zBuf
, "SQLite format 3", 16)==0 ){
5378 return SHELL_OPEN_NORMAL
;
5380 fseek(f
, -25, SEEK_END
);
5381 n
= fread(zBuf
, 25, 1, f
);
5382 if( n
==1 && memcmp(zBuf
, "Start-Of-SQLite3-", 17)==0 ){
5383 rc
= SHELL_OPEN_APPENDVFS
;
5385 fseek(f
, -22, SEEK_END
);
5386 n
= fread(zBuf
, 22, 1, f
);
5387 if( n
==1 && zBuf
[0]==0x50 && zBuf
[1]==0x4b && zBuf
[2]==0x05
5389 rc
= SHELL_OPEN_ZIPFILE
;
5390 }else if( n
==0 && dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
5391 rc
= SHELL_OPEN_ZIPFILE
;
5398 #ifndef SQLITE_OMIT_DESERIALIZE
5400 ** Reconstruct an in-memory database using the output from the "dbtotxt"
5401 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
5402 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
5404 static unsigned char *readHexDb(ShellState
*p
, int *pnData
){
5405 unsigned char *a
= 0;
5413 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
5417 in
= fopen(zDbFilename
, "r");
5419 eputf("cannot open \"%s\" for reading\n", zDbFilename
);
5426 if( in
==0 ) in
= stdin
;
5430 if( fgets(zLine
, sizeof(zLine
), in
)==0 ) goto readHexDb_error
;
5431 rc
= sscanf(zLine
, "| size %d pagesize %d", &n
, &pgsz
);
5432 if( rc
!=2 ) goto readHexDb_error
;
5433 if( n
<0 ) goto readHexDb_error
;
5434 if( pgsz
<512 || pgsz
>65536 || (pgsz
&(pgsz
-1))!=0 ) goto readHexDb_error
;
5435 n
= (n
+pgsz
-1)&~(pgsz
-1); /* Round n up to the next multiple of pgsz */
5436 a
= sqlite3_malloc( n
? n
: 1 );
5439 if( pgsz
<512 || pgsz
>65536 || (pgsz
& (pgsz
-1))!=0 ){
5440 eputz("invalid pagesize\n");
5441 goto readHexDb_error
;
5443 for(nLine
++; fgets(zLine
, sizeof(zLine
), in
)!=0; nLine
++){
5444 rc
= sscanf(zLine
, "| page %d offset %d", &j
, &k
);
5449 if( cli_strncmp(zLine
, "| end ", 6)==0 ){
5452 rc
= sscanf(zLine
,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
5453 &j
, &x
[0], &x
[1], &x
[2], &x
[3], &x
[4], &x
[5], &x
[6], &x
[7],
5454 &x
[8], &x
[9], &x
[10], &x
[11], &x
[12], &x
[13], &x
[14], &x
[15]);
5457 if( k
+16<=n
&& k
>=0 ){
5459 for(ii
=0; ii
<16; ii
++) a
[k
+ii
] = x
[ii
]&0xff;
5475 while( fgets(zLine
, sizeof(zLine
), p
->in
)!=0 ){
5477 if(cli_strncmp(zLine
, "| end ", 6)==0 ) break;
5482 eputf("Error on line %d of --hexdb input\n", nLine
);
5485 #endif /* SQLITE_OMIT_DESERIALIZE */
5488 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
5490 static void shellUSleepFunc(
5491 sqlite3_context
*context
,
5493 sqlite3_value
**argv
5495 int sleep
= sqlite3_value_int(argv
[0]);
5497 sqlite3_sleep(sleep
/1000);
5498 sqlite3_result_int(context
, sleep
);
5501 /* Flags for open_db().
5503 ** The default behavior of open_db() is to exit(1) if the database fails to
5504 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
5505 ** but still returns without calling exit.
5507 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
5508 ** ZIP archive if the file does not exist or is empty and its name matches
5509 ** the *.zip pattern.
5511 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
5512 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
5515 ** Make sure the database is open. If it is not, then open it. If
5516 ** the database fails to open, print an error message and exit.
5518 static void open_db(ShellState
*p
, int openFlags
){
5520 const char *zDbFilename
= p
->pAuxDb
->zDbFilename
;
5521 if( p
->openMode
==SHELL_OPEN_UNSPEC
){
5522 if( zDbFilename
==0 || zDbFilename
[0]==0 ){
5523 p
->openMode
= SHELL_OPEN_NORMAL
;
5525 p
->openMode
= (u8
)deduceDatabaseType(zDbFilename
,
5526 (openFlags
& OPEN_DB_ZIPFILE
)!=0);
5529 switch( p
->openMode
){
5530 case SHELL_OPEN_APPENDVFS
: {
5531 sqlite3_open_v2(zDbFilename
, &p
->db
,
5532 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, "apndvfs");
5535 case SHELL_OPEN_HEXDB
:
5536 case SHELL_OPEN_DESERIALIZE
: {
5537 sqlite3_open(0, &p
->db
);
5540 case SHELL_OPEN_ZIPFILE
: {
5541 sqlite3_open(":memory:", &p
->db
);
5544 case SHELL_OPEN_READONLY
: {
5545 sqlite3_open_v2(zDbFilename
, &p
->db
,
5546 SQLITE_OPEN_READONLY
|p
->openFlags
, 0);
5549 case SHELL_OPEN_UNSPEC
:
5550 case SHELL_OPEN_NORMAL
: {
5551 sqlite3_open_v2(zDbFilename
, &p
->db
,
5552 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
|p
->openFlags
, 0);
5556 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
5557 eputf("Error: unable to open database \"%s\": %s\n",
5558 zDbFilename
, sqlite3_errmsg(p
->db
));
5559 if( (openFlags
& OPEN_DB_KEEPALIVE
)==0 ){
5562 sqlite3_close(p
->db
);
5563 sqlite3_open(":memory:", &p
->db
);
5564 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
5565 eputz("Also: unable to open substitute in-memory database.\n");
5568 eputf("Notice: using substitute in-memory database instead of \"%s\"\n",
5573 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, (int)0, (int*)0);
5575 /* Reflect the use or absence of --unsafe-testing invocation. */
5577 int testmode_on
= ShellHasFlag(p
,SHFLG_TestingMode
);
5578 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_TRUSTED_SCHEMA
, testmode_on
,0);
5579 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, !testmode_on
,0);
5582 #ifndef SQLITE_OMIT_LOAD_EXTENSION
5583 sqlite3_enable_load_extension(p
->db
, 1);
5585 sqlite3_sha_init(p
->db
, 0, 0);
5586 sqlite3_shathree_init(p
->db
, 0, 0);
5587 sqlite3_uint_init(p
->db
, 0, 0);
5588 sqlite3_stmtrand_init(p
->db
, 0, 0);
5589 sqlite3_decimal_init(p
->db
, 0, 0);
5590 sqlite3_percentile_init(p
->db
, 0, 0);
5591 sqlite3_base64_init(p
->db
, 0, 0);
5592 sqlite3_base85_init(p
->db
, 0, 0);
5593 sqlite3_regexp_init(p
->db
, 0, 0);
5594 sqlite3_ieee_init(p
->db
, 0, 0);
5595 sqlite3_series_init(p
->db
, 0, 0);
5596 #ifndef SQLITE_SHELL_FIDDLE
5597 sqlite3_fileio_init(p
->db
, 0, 0);
5598 sqlite3_completion_init(p
->db
, 0, 0);
5600 #ifdef SQLITE_HAVE_ZLIB
5601 if( !p
->bSafeModePersist
){
5602 sqlite3_zipfile_init(p
->db
, 0, 0);
5603 sqlite3_sqlar_init(p
->db
, 0, 0);
5606 #ifdef SQLITE_SHELL_EXTFUNCS
5607 /* Create a preprocessing mechanism for extensions to make
5608 * their own provisions for being built into the shell.
5609 * This is a short-span macro. See further below for usage.
5611 #define SHELL_SUB_MACRO(base, variant) base ## _ ## variant
5612 #define SHELL_SUBMACRO(base, variant) SHELL_SUB_MACRO(base, variant)
5613 /* Let custom-included extensions get their ..._init() called.
5614 * The WHATEVER_INIT( db, pzErrorMsg, pApi ) macro should cause
5615 * the extension's sqlite3_*_init( db, pzErrorMsg, pApi )
5616 * initialization routine to be called.
5619 int irc
= SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, INIT
)(p
->db
);
5620 /* Let custom-included extensions expose their functionality.
5621 * The WHATEVER_EXPOSE( db, pzErrorMsg ) macro should cause
5622 * the SQL functions, virtual tables, collating sequences or
5623 * VFS's implemented by the extension to be registered.
5626 || irc
==SQLITE_OK_LOAD_PERMANENTLY
){
5627 SHELL_SUBMACRO(SQLITE_SHELL_EXTFUNCS
, EXPOSE
)(p
->db
, 0);
5629 #undef SHELL_SUB_MACRO
5630 #undef SHELL_SUBMACRO
5634 sqlite3_create_function(p
->db
, "strtod", 1, SQLITE_UTF8
, 0,
5636 sqlite3_create_function(p
->db
, "dtostr", 1, SQLITE_UTF8
, 0,
5638 sqlite3_create_function(p
->db
, "dtostr", 2, SQLITE_UTF8
, 0,
5640 sqlite3_create_function(p
->db
, "shell_add_schema", 3, SQLITE_UTF8
, 0,
5641 shellAddSchemaName
, 0, 0);
5642 sqlite3_create_function(p
->db
, "shell_module_schema", 1, SQLITE_UTF8
, 0,
5643 shellModuleSchema
, 0, 0);
5644 sqlite3_create_function(p
->db
, "shell_putsnl", 1, SQLITE_UTF8
, p
,
5645 shellPutsFunc
, 0, 0);
5646 sqlite3_create_function(p
->db
, "usleep",1,SQLITE_UTF8
,0,
5647 shellUSleepFunc
, 0, 0);
5648 #ifndef SQLITE_NOHAVE_SYSTEM
5649 sqlite3_create_function(p
->db
, "edit", 1, SQLITE_UTF8
, 0,
5651 sqlite3_create_function(p
->db
, "edit", 2, SQLITE_UTF8
, 0,
5655 if( p
->openMode
==SHELL_OPEN_ZIPFILE
){
5656 char *zSql
= sqlite3_mprintf(
5657 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename
);
5658 shell_check_oom(zSql
);
5659 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
5662 #ifndef SQLITE_OMIT_DESERIALIZE
5664 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
|| p
->openMode
==SHELL_OPEN_HEXDB
){
5667 unsigned char *aData
;
5668 if( p
->openMode
==SHELL_OPEN_DESERIALIZE
){
5669 aData
= (unsigned char*)readFile(zDbFilename
, &nData
);
5671 aData
= readHexDb(p
, &nData
);
5676 rc
= sqlite3_deserialize(p
->db
, "main", aData
, nData
, nData
,
5677 SQLITE_DESERIALIZE_RESIZEABLE
|
5678 SQLITE_DESERIALIZE_FREEONCLOSE
);
5680 eputf("Error: sqlite3_deserialize() returns %d\n", rc
);
5683 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_SIZE_LIMIT
, &p
->szMax
);
5689 if( p
->bSafeModePersist
){
5690 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
5693 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
5699 ** Attempt to close the database connection. Report errors.
5701 void close_db(sqlite3
*db
){
5702 int rc
= sqlite3_close(db
);
5704 eputf("Error: sqlite3_close() returns %d: %s\n", rc
, sqlite3_errmsg(db
));
5708 #if HAVE_READLINE || HAVE_EDITLINE
5710 ** Readline completion callbacks
5712 static char *readline_completion_generator(const char *text
, int state
){
5713 static sqlite3_stmt
*pStmt
= 0;
5717 sqlite3_finalize(pStmt
);
5718 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5719 " FROM completion(%Q) ORDER BY 1", text
);
5720 shell_check_oom(zSql
);
5721 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
5724 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
5725 const char *z
= (const char*)sqlite3_column_text(pStmt
,0);
5726 zRet
= z
? strdup(z
) : 0;
5728 sqlite3_finalize(pStmt
);
5734 static char **readline_completion(const char *zText
, int iStart
, int iEnd
){
5737 rl_attempted_completion_over
= 1;
5738 return rl_completion_matches(zText
, readline_completion_generator
);
5741 #elif HAVE_LINENOISE
5743 ** Linenoise completion callback. Note that the 3rd argument is from
5744 ** the "msteveb" version of linenoise, not the "antirez" version.
5746 static void linenoise_completion(const char *zLine
, linenoiseCompletions
*lc
,
5748 i64 nLine
= strlen(zLine
);
5750 sqlite3_stmt
*pStmt
= 0;
5754 UNUSED_PARAMETER(pUserData
);
5755 if( nLine
>(i64
)sizeof(zBuf
)-30 ) return;
5756 if( zLine
[0]=='.' || zLine
[0]=='#') return;
5757 for(i
=nLine
-1; i
>=0 && (isalnum(zLine
[i
]) || zLine
[i
]=='_'); i
--){}
5758 if( i
==nLine
-1 ) return;
5760 memcpy(zBuf
, zLine
, iStart
);
5761 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
5762 " FROM completion(%Q,%Q) ORDER BY 1",
5763 &zLine
[iStart
], zLine
);
5764 shell_check_oom(zSql
);
5765 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
5767 sqlite3_exec(globalDb
, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
5768 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
5769 const char *zCompletion
= (const char*)sqlite3_column_text(pStmt
, 0);
5770 int nCompletion
= sqlite3_column_bytes(pStmt
, 0);
5771 if( iStart
+nCompletion
< (i64
)sizeof(zBuf
)-1 && zCompletion
){
5772 memcpy(zBuf
+iStart
, zCompletion
, nCompletion
+1);
5773 linenoiseAddCompletion(lc
, zBuf
);
5776 sqlite3_finalize(pStmt
);
5781 ** Do C-language style dequoting.
5787 ** \v -> vertical tab
5789 ** \r -> carriage return
5794 ** \NNN -> ascii character NNN in octal
5795 ** \xHH -> ascii character HH in hexadecimal
5797 static void resolve_backslashes(char *z
){
5800 while( *z
&& *z
!='\\' ) z
++;
5801 for(i
=j
=0; (c
= z
[i
])!=0; i
++, j
++){
5802 if( c
=='\\' && z
[i
+1]!=0 ){
5820 }else if( c
=='\'' ){
5822 }else if( c
=='\\' ){
5827 while( nhd
<2 && (c
=z
[i
+1+nhd
])!=0 && (hdv
=hexDigitValue(c
))>=0 ){
5828 hv
= (u8
)((hv
<<4)|hdv
);
5833 }else if( c
>='0' && c
<='7' ){
5835 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
5837 c
= (c
<<3) + z
[i
] - '0';
5838 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
5840 c
= (c
<<3) + z
[i
] - '0';
5851 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
5852 ** for TRUE and FALSE. Return the integer value if appropriate.
5854 static int booleanValue(const char *zArg
){
5856 if( zArg
[0]=='0' && zArg
[1]=='x' ){
5857 for(i
=2; hexDigitValue(zArg
[i
])>=0; i
++){}
5859 for(i
=0; zArg
[i
]>='0' && zArg
[i
]<='9'; i
++){}
5861 if( i
>0 && zArg
[i
]==0 ) return (int)(integerValue(zArg
) & 0xffffffff);
5862 if( sqlite3_stricmp(zArg
, "on")==0 || sqlite3_stricmp(zArg
,"yes")==0 ){
5865 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
5868 eputf("ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n", zArg
);
5873 ** Set or clear a shell flag according to a boolean value.
5875 static void setOrClearFlag(ShellState
*p
, unsigned mFlag
, const char *zArg
){
5876 if( booleanValue(zArg
) ){
5877 ShellSetFlag(p
, mFlag
);
5879 ShellClearFlag(p
, mFlag
);
5884 ** Close an output file, assuming it is not stderr or stdout
5886 static void output_file_close(FILE *f
){
5887 if( f
&& f
!=stdout
&& f
!=stderr
) fclose(f
);
5891 ** Try to open an output file. The names "stdout" and "stderr" are
5892 ** recognized and do the right thing. NULL is returned if the output
5893 ** filename is "off".
5895 static FILE *output_file_open(const char *zFile
, int bTextMode
){
5897 if( cli_strcmp(zFile
,"stdout")==0 ){
5899 }else if( cli_strcmp(zFile
, "stderr")==0 ){
5901 }else if( cli_strcmp(zFile
, "off")==0 ){
5904 f
= fopen(zFile
, bTextMode
? "w" : "wb");
5906 eputf("Error: cannot open \"%s\"\n", zFile
);
5912 #ifndef SQLITE_OMIT_TRACE
5914 ** A routine for handling output from sqlite3_trace().
5916 static int sql_trace_callback(
5917 unsigned mType
, /* The trace type */
5918 void *pArg
, /* The ShellState pointer */
5919 void *pP
, /* Usually a pointer to sqlite_stmt */
5920 void *pX
/* Auxiliary output */
5922 ShellState
*p
= (ShellState
*)pArg
;
5923 sqlite3_stmt
*pStmt
;
5926 if( p
->traceOut
==0 ) return 0;
5927 if( mType
==SQLITE_TRACE_CLOSE
){
5928 sputz(p
->traceOut
, "-- closing database connection\n");
5931 if( mType
!=SQLITE_TRACE_ROW
&& pX
!=0 && ((const char*)pX
)[0]=='-' ){
5932 zSql
= (const char*)pX
;
5934 pStmt
= (sqlite3_stmt
*)pP
;
5935 switch( p
->eTraceType
){
5936 case SHELL_TRACE_EXPANDED
: {
5937 zSql
= sqlite3_expanded_sql(pStmt
);
5940 #ifdef SQLITE_ENABLE_NORMALIZE
5941 case SHELL_TRACE_NORMALIZED
: {
5942 zSql
= sqlite3_normalized_sql(pStmt
);
5947 zSql
= sqlite3_sql(pStmt
);
5952 if( zSql
==0 ) return 0;
5953 nSql
= strlen(zSql
);
5954 if( nSql
>1000000000 ) nSql
= 1000000000;
5955 while( nSql
>0 && zSql
[nSql
-1]==';' ){ nSql
--; }
5957 case SQLITE_TRACE_ROW
:
5958 case SQLITE_TRACE_STMT
: {
5959 sputf(p
->traceOut
, "%.*s;\n", (int)nSql
, zSql
);
5962 case SQLITE_TRACE_PROFILE
: {
5963 sqlite3_int64 nNanosec
= pX
? *(sqlite3_int64
*)pX
: 0;
5964 sputf(p
->traceOut
, "%.*s; -- %lld ns\n", (int)nSql
, zSql
, nNanosec
);
5973 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
5974 ** a useful spot to set a debugger breakpoint.
5976 ** This routine does not do anything practical. The code are there simply
5977 ** to prevent the compiler from optimizing this routine out.
5979 static void test_breakpoint(void){
5980 static unsigned int nCall
= 0;
5981 if( (nCall
++)==0xffffffff ) printf("Many .breakpoints have run\n");
5985 ** An object used to read a CSV and other files for import.
5987 typedef struct ImportCtx ImportCtx
;
5989 const char *zFile
; /* Name of the input file */
5990 FILE *in
; /* Read the CSV text from this input stream */
5991 int (SQLITE_CDECL
*xCloser
)(FILE*); /* Func to close in */
5992 char *z
; /* Accumulated text for a field */
5993 int n
; /* Number of bytes in z */
5994 int nAlloc
; /* Space allocated for z[] */
5995 int nLine
; /* Current line number */
5996 int nRow
; /* Number of rows imported */
5997 int nErr
; /* Number of errors encountered */
5998 int bNotFirst
; /* True if one or more bytes already read */
5999 int cTerm
; /* Character that terminated the most recent field */
6000 int cColSep
; /* The column separator character. (Usually ",") */
6001 int cRowSep
; /* The row separator character. (Usually "\n") */
6004 /* Clean up resourced used by an ImportCtx */
6005 static void import_cleanup(ImportCtx
*p
){
6006 if( p
->in
!=0 && p
->xCloser
!=0 ){
6014 /* Append a single byte to z[] */
6015 static void import_append_char(ImportCtx
*p
, int c
){
6016 if( p
->n
+1>=p
->nAlloc
){
6017 p
->nAlloc
+= p
->nAlloc
+ 100;
6018 p
->z
= sqlite3_realloc64(p
->z
, p
->nAlloc
);
6019 shell_check_oom(p
->z
);
6021 p
->z
[p
->n
++] = (char)c
;
6024 /* Read a single field of CSV text. Compatible with rfc4180 and extended
6025 ** with the option of having a separator other than ",".
6027 ** + Input comes from p->in.
6028 ** + Store results in p->z of length p->n. Space to hold p->z comes
6029 ** from sqlite3_malloc64().
6030 ** + Use p->cSep as the column separator. The default is ",".
6031 ** + Use p->rSep as the row separator. The default is "\n".
6032 ** + Keep track of the line number in p->nLine.
6033 ** + Store the character that terminates the field in p->cTerm. Store
6034 ** EOF on end-of-file.
6035 ** + Report syntax errors on stderr
6037 static char *SQLITE_CDECL
csv_read_one_field(ImportCtx
*p
){
6039 int cSep
= (u8
)p
->cColSep
;
6040 int rSep
= (u8
)p
->cRowSep
;
6043 if( c
==EOF
|| seenInterrupt
){
6049 int startLine
= p
->nLine
;
6054 if( c
==rSep
) p
->nLine
++;
6061 if( (c
==cSep
&& pc
==cQuote
)
6062 || (c
==rSep
&& pc
==cQuote
)
6063 || (c
==rSep
&& pc
=='\r' && ppc
==cQuote
)
6064 || (c
==EOF
&& pc
==cQuote
)
6066 do{ p
->n
--; }while( p
->z
[p
->n
]!=cQuote
);
6070 if( pc
==cQuote
&& c
!='\r' ){
6071 eputf("%s:%d: unescaped %c character\n", p
->zFile
, p
->nLine
, cQuote
);
6074 eputf("%s:%d: unterminated %c-quoted field\n",
6075 p
->zFile
, startLine
, cQuote
);
6079 import_append_char(p
, c
);
6084 /* If this is the first field being parsed and it begins with the
6085 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
6086 if( (c
&0xff)==0xef && p
->bNotFirst
==0 ){
6087 import_append_char(p
, c
);
6089 if( (c
&0xff)==0xbb ){
6090 import_append_char(p
, c
);
6092 if( (c
&0xff)==0xbf ){
6095 return csv_read_one_field(p
);
6099 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
6100 import_append_char(p
, c
);
6105 if( p
->n
>0 && p
->z
[p
->n
-1]=='\r' ) p
->n
--;
6109 if( p
->z
) p
->z
[p
->n
] = 0;
6114 /* Read a single field of ASCII delimited text.
6116 ** + Input comes from p->in.
6117 ** + Store results in p->z of length p->n. Space to hold p->z comes
6118 ** from sqlite3_malloc64().
6119 ** + Use p->cSep as the column separator. The default is "\x1F".
6120 ** + Use p->rSep as the row separator. The default is "\x1E".
6121 ** + Keep track of the row number in p->nLine.
6122 ** + Store the character that terminates the field in p->cTerm. Store
6123 ** EOF on end-of-file.
6124 ** + Report syntax errors on stderr
6126 static char *SQLITE_CDECL
ascii_read_one_field(ImportCtx
*p
){
6128 int cSep
= (u8
)p
->cColSep
;
6129 int rSep
= (u8
)p
->cRowSep
;
6132 if( c
==EOF
|| seenInterrupt
){
6136 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
6137 import_append_char(p
, c
);
6144 if( p
->z
) p
->z
[p
->n
] = 0;
6149 ** Try to transfer data for table zTable. If an error is seen while
6150 ** moving forward, try to go backwards. The backwards movement won't
6151 ** work for WITHOUT ROWID tables.
6153 static void tryToCloneData(
6158 sqlite3_stmt
*pQuery
= 0;
6159 sqlite3_stmt
*pInsert
= 0;
6164 int nTable
= strlen30(zTable
);
6167 const int spinRate
= 10000;
6169 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\"", zTable
);
6170 shell_check_oom(zQuery
);
6171 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6173 eputf("Error %d: %s on [%s]\n",
6174 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
), zQuery
);
6177 n
= sqlite3_column_count(pQuery
);
6178 zInsert
= sqlite3_malloc64(200 + nTable
+ n
*3);
6179 shell_check_oom(zInsert
);
6180 sqlite3_snprintf(200+nTable
,zInsert
,
6181 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable
);
6182 i
= strlen30(zInsert
);
6184 memcpy(zInsert
+i
, ",?", 2);
6187 memcpy(zInsert
+i
, ");", 3);
6188 rc
= sqlite3_prepare_v2(newDb
, zInsert
, -1, &pInsert
, 0);
6190 eputf("Error %d: %s on [%s]\n",
6191 sqlite3_extended_errcode(newDb
), sqlite3_errmsg(newDb
), zInsert
);
6195 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
6197 switch( sqlite3_column_type(pQuery
, i
) ){
6199 sqlite3_bind_null(pInsert
, i
+1);
6202 case SQLITE_INTEGER
: {
6203 sqlite3_bind_int64(pInsert
, i
+1, sqlite3_column_int64(pQuery
,i
));
6206 case SQLITE_FLOAT
: {
6207 sqlite3_bind_double(pInsert
, i
+1, sqlite3_column_double(pQuery
,i
));
6211 sqlite3_bind_text(pInsert
, i
+1,
6212 (const char*)sqlite3_column_text(pQuery
,i
),
6217 sqlite3_bind_blob(pInsert
, i
+1, sqlite3_column_blob(pQuery
,i
),
6218 sqlite3_column_bytes(pQuery
,i
),
6224 rc
= sqlite3_step(pInsert
);
6225 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_ROW
&& rc
!=SQLITE_DONE
){
6226 eputf("Error %d: %s\n",
6227 sqlite3_extended_errcode(newDb
), sqlite3_errmsg(newDb
));
6229 sqlite3_reset(pInsert
);
6231 if( (cnt
%spinRate
)==0 ){
6232 printf("%c\b", "|/-\\"[(cnt
/spinRate
)%4]);
6236 if( rc
==SQLITE_DONE
) break;
6237 sqlite3_finalize(pQuery
);
6238 sqlite3_free(zQuery
);
6239 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
6241 shell_check_oom(zQuery
);
6242 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6244 eputf("Warning: cannot step \"%s\" backwards", zTable
);
6247 } /* End for(k=0...) */
6250 sqlite3_finalize(pQuery
);
6251 sqlite3_finalize(pInsert
);
6252 sqlite3_free(zQuery
);
6253 sqlite3_free(zInsert
);
6258 ** Try to transfer all rows of the schema that match zWhere. For
6259 ** each row, invoke xForEach() on the object defined by that row.
6260 ** If an error is encountered while moving forward through the
6261 ** sqlite_schema table, try again moving backwards.
6263 static void tryToCloneSchema(
6267 void (*xForEach
)(ShellState
*,sqlite3
*,const char*)
6269 sqlite3_stmt
*pQuery
= 0;
6272 const unsigned char *zName
;
6273 const unsigned char *zSql
;
6276 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6277 " WHERE %s ORDER BY rowid ASC", zWhere
);
6278 shell_check_oom(zQuery
);
6279 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6281 eputf("Error: (%d) %s on [%s]\n", sqlite3_extended_errcode(p
->db
),
6282 sqlite3_errmsg(p
->db
), zQuery
);
6283 goto end_schema_xfer
;
6285 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
6286 zName
= sqlite3_column_text(pQuery
, 0);
6287 zSql
= sqlite3_column_text(pQuery
, 1);
6288 if( zName
==0 || zSql
==0 ) continue;
6289 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")!=0 ){
6290 sputf(stdout
, "%s... ", zName
); fflush(stdout
);
6291 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
6293 eputf("Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
6294 sqlite3_free(zErrMsg
);
6299 xForEach(p
, newDb
, (const char*)zName
);
6301 sputz(stdout
, "done\n");
6303 if( rc
!=SQLITE_DONE
){
6304 sqlite3_finalize(pQuery
);
6305 sqlite3_free(zQuery
);
6306 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
6307 " WHERE %s ORDER BY rowid DESC", zWhere
);
6308 shell_check_oom(zQuery
);
6309 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
6311 eputf("Error: (%d) %s on [%s]\n",
6312 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
), zQuery
);
6313 goto end_schema_xfer
;
6315 while( sqlite3_step(pQuery
)==SQLITE_ROW
){
6316 zName
= sqlite3_column_text(pQuery
, 0);
6317 zSql
= sqlite3_column_text(pQuery
, 1);
6318 if( zName
==0 || zSql
==0 ) continue;
6319 if( sqlite3_stricmp((char*)zName
, "sqlite_sequence")==0 ) continue;
6320 sputf(stdout
, "%s... ", zName
); fflush(stdout
);
6321 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
6323 eputf("Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
6324 sqlite3_free(zErrMsg
);
6328 xForEach(p
, newDb
, (const char*)zName
);
6330 sputz(stdout
, "done\n");
6334 sqlite3_finalize(pQuery
);
6335 sqlite3_free(zQuery
);
6339 ** Open a new database file named "zNewDb". Try to recover as much information
6340 ** as possible out of the main database (which might be corrupt) and write it
6343 static void tryToClone(ShellState
*p
, const char *zNewDb
){
6346 if( access(zNewDb
,0)==0 ){
6347 eputf("File \"%s\" already exists.\n", zNewDb
);
6350 rc
= sqlite3_open(zNewDb
, &newDb
);
6352 eputf("Cannot create output database: %s\n", sqlite3_errmsg(newDb
));
6354 sqlite3_exec(p
->db
, "PRAGMA writable_schema=ON;", 0, 0, 0);
6355 sqlite3_exec(newDb
, "BEGIN EXCLUSIVE;", 0, 0, 0);
6356 tryToCloneSchema(p
, newDb
, "type='table'", tryToCloneData
);
6357 tryToCloneSchema(p
, newDb
, "type!='table'", 0);
6358 sqlite3_exec(newDb
, "COMMIT;", 0, 0, 0);
6359 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
6364 #ifndef SQLITE_SHELL_FIDDLE
6366 ** Change the output stream (file or pipe or console) to something else.
6368 static void output_redir(ShellState
*p
, FILE *pfNew
){
6369 if( p
->out
!= stdout
) eputz("Output already redirected.\n");
6372 setOutputStream(pfNew
);
6377 ** Change the output file back to stdout.
6379 ** If the p->doXdgOpen flag is set, that means the output was being
6380 ** redirected to a temporary file named by p->zTempFile. In that case,
6381 ** launch start/open/xdg-open on that temporary file.
6383 static void output_reset(ShellState
*p
){
6384 if( p
->outfile
[0]=='|' ){
6385 #ifndef SQLITE_OMIT_POPEN
6389 output_file_close(p
->out
);
6390 #ifndef SQLITE_NOHAVE_SYSTEM
6392 const char *zXdgOpenCmd
=
6395 #elif defined(__APPLE__)
6401 zCmd
= sqlite3_mprintf("%s %s", zXdgOpenCmd
, p
->zTempFile
);
6403 eputf("Failed: [%s]\n", zCmd
);
6405 /* Give the start/open/xdg-open command some time to get
6406 ** going before we continue, and potential delete the
6407 ** p->zTempFile data file out from under it */
6408 sqlite3_sleep(2000);
6414 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
6418 setOutputStream(stdout
);
6421 # define output_redir(SS,pfO)
6422 # define output_reset(SS)
6426 ** Run an SQL command and return the single integer result.
6428 static int db_int(sqlite3
*db
, const char *zSql
){
6429 sqlite3_stmt
*pStmt
;
6431 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
6432 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
6433 res
= sqlite3_column_int(pStmt
,0);
6435 sqlite3_finalize(pStmt
);
6439 #if SQLITE_SHELL_HAVE_RECOVER
6441 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
6443 static unsigned int get2byteInt(unsigned char *a
){
6444 return (a
[0]<<8) + a
[1];
6446 static unsigned int get4byteInt(unsigned char *a
){
6447 return (a
[0]<<24) + (a
[1]<<16) + (a
[2]<<8) + a
[3];
6451 ** Implementation of the ".dbinfo" command.
6453 ** Return 1 on error, 2 to exit, and 0 otherwise.
6455 static int shell_dbinfo_command(ShellState
*p
, int nArg
, char **azArg
){
6456 static const struct { const char *zName
; int ofst
; } aField
[] = {
6457 { "file change counter:", 24 },
6458 { "database page count:", 28 },
6459 { "freelist page count:", 36 },
6460 { "schema cookie:", 40 },
6461 { "schema format:", 44 },
6462 { "default cache size:", 48 },
6463 { "autovacuum top root:", 52 },
6464 { "incremental vacuum:", 64 },
6465 { "text encoding:", 56 },
6466 { "user version:", 60 },
6467 { "application id:", 68 },
6468 { "software version:", 96 },
6470 static const struct { const char *zName
; const char *zSql
; } aQuery
[] = {
6471 { "number of tables:",
6472 "SELECT count(*) FROM %s WHERE type='table'" },
6473 { "number of indexes:",
6474 "SELECT count(*) FROM %s WHERE type='index'" },
6475 { "number of triggers:",
6476 "SELECT count(*) FROM %s WHERE type='trigger'" },
6477 { "number of views:",
6478 "SELECT count(*) FROM %s WHERE type='view'" },
6480 "SELECT total(length(sql)) FROM %s" },
6483 unsigned iDataVersion
;
6485 char *zDb
= nArg
>=2 ? azArg
[1] : "main";
6486 sqlite3_stmt
*pStmt
= 0;
6487 unsigned char aHdr
[100];
6489 if( p
->db
==0 ) return 1;
6490 rc
= sqlite3_prepare_v2(p
->db
,
6491 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
6494 eputf("error: %s\n", sqlite3_errmsg(p
->db
));
6495 sqlite3_finalize(pStmt
);
6498 sqlite3_bind_text(pStmt
, 1, zDb
, -1, SQLITE_STATIC
);
6499 if( sqlite3_step(pStmt
)==SQLITE_ROW
6500 && sqlite3_column_bytes(pStmt
,0)>100
6502 const u8
*pb
= sqlite3_column_blob(pStmt
,0);
6503 shell_check_oom(pb
);
6504 memcpy(aHdr
, pb
, 100);
6505 sqlite3_finalize(pStmt
);
6507 eputz("unable to read database header\n");
6508 sqlite3_finalize(pStmt
);
6511 i
= get2byteInt(aHdr
+16);
6512 if( i
==1 ) i
= 65536;
6513 oputf("%-20s %d\n", "database page size:", i
);
6514 oputf("%-20s %d\n", "write format:", aHdr
[18]);
6515 oputf("%-20s %d\n", "read format:", aHdr
[19]);
6516 oputf("%-20s %d\n", "reserved bytes:", aHdr
[20]);
6517 for(i
=0; i
<ArraySize(aField
); i
++){
6518 int ofst
= aField
[i
].ofst
;
6519 unsigned int val
= get4byteInt(aHdr
+ ofst
);
6520 oputf("%-20s %u", aField
[i
].zName
, val
);
6523 if( val
==1 ) oputz(" (utf8)");
6524 if( val
==2 ) oputz(" (utf16le)");
6525 if( val
==3 ) oputz(" (utf16be)");
6531 zSchemaTab
= sqlite3_mprintf("main.sqlite_schema");
6532 }else if( cli_strcmp(zDb
,"temp")==0 ){
6533 zSchemaTab
= sqlite3_mprintf("%s", "sqlite_temp_schema");
6535 zSchemaTab
= sqlite3_mprintf("\"%w\".sqlite_schema", zDb
);
6537 for(i
=0; i
<ArraySize(aQuery
); i
++){
6538 char *zSql
= sqlite3_mprintf(aQuery
[i
].zSql
, zSchemaTab
);
6539 int val
= db_int(p
->db
, zSql
);
6541 oputf("%-20s %d\n", aQuery
[i
].zName
, val
);
6543 sqlite3_free(zSchemaTab
);
6544 sqlite3_file_control(p
->db
, zDb
, SQLITE_FCNTL_DATA_VERSION
, &iDataVersion
);
6545 oputf("%-20s %u\n", "data version", iDataVersion
);
6548 #endif /* SQLITE_SHELL_HAVE_RECOVER */
6551 ** Print the given string as an error message.
6553 static void shellEmitError(const char *zErr
){
6554 eputf("Error: %s\n", zErr
);
6557 ** Print the current sqlite3_errmsg() value to stderr and return 1.
6559 static int shellDatabaseError(sqlite3
*db
){
6560 shellEmitError(sqlite3_errmsg(db
));
6565 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
6566 ** if they match and FALSE (0) if they do not match.
6570 ** '*' Matches any sequence of zero or more characters.
6572 ** '?' Matches exactly one character.
6574 ** [...] Matches one character from the enclosed list of
6577 ** [^...] Matches one character not in the enclosed list.
6579 ** '#' Matches any sequence of one or more digits with an
6580 ** optional + or - sign in front
6582 ** ' ' Any span of whitespace matches any other span of
6585 ** Extra whitespace at the end of z[] is ignored.
6587 static int testcase_glob(const char *zGlob
, const char *z
){
6592 while( (c
= (*(zGlob
++)))!=0 ){
6594 if( !IsSpace(*z
) ) return 0;
6595 while( IsSpace(*zGlob
) ) zGlob
++;
6596 while( IsSpace(*z
) ) z
++;
6598 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
6599 if( c
=='?' && (*(z
++))==0 ) return 0;
6604 while( *z
&& testcase_glob(zGlob
-1,z
)==0 ){
6609 while( (c2
= (*(z
++)))!=0 ){
6612 if( c2
==0 ) return 0;
6614 if( testcase_glob(zGlob
,z
) ) return 1;
6618 if( (*(z
++))==0 ) return 0;
6624 if( c
==0 ) return 0;
6631 if( c
==']' ) seen
= 1;
6634 while( c2
&& c2
!=']' ){
6635 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
6637 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
6647 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
6649 if( (z
[0]=='-' || z
[0]=='+') && IsDigit(z
[1]) ) z
++;
6650 if( !IsDigit(z
[0]) ) return 0;
6652 while( IsDigit(z
[0]) ){ z
++; }
6654 if( c
!=(*(z
++)) ) return 0;
6657 while( IsSpace(*z
) ){ z
++; }
6663 ** Compare the string as a command-line option with either one or two
6664 ** initial "-" characters.
6666 static int optionMatch(const char *zStr
, const char *zOpt
){
6667 if( zStr
[0]!='-' ) return 0;
6669 if( zStr
[0]=='-' ) zStr
++;
6670 return cli_strcmp(zStr
, zOpt
)==0;
6676 int shellDeleteFile(const char *zFilename
){
6679 wchar_t *z
= sqlite3_win32_utf8_to_unicode(zFilename
);
6683 rc
= unlink(zFilename
);
6689 ** Try to delete the temporary file (if there is one) and free the
6690 ** memory used to hold the name of the temp file.
6692 static void clearTempFile(ShellState
*p
){
6693 if( p
->zTempFile
==0 ) return;
6694 if( p
->doXdgOpen
) return;
6695 if( shellDeleteFile(p
->zTempFile
) ) return;
6696 sqlite3_free(p
->zTempFile
);
6701 ** Create a new temp file name with the given suffix.
6703 static void newTempFile(ShellState
*p
, const char *zSuffix
){
6705 sqlite3_free(p
->zTempFile
);
6708 sqlite3_file_control(p
->db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &p
->zTempFile
);
6710 if( p
->zTempFile
==0 ){
6711 /* If p->db is an in-memory database then the TEMPFILENAME file-control
6712 ** will not work and we will need to fallback to guessing */
6715 sqlite3_randomness(sizeof(r
), &r
);
6716 zTemp
= getenv("TEMP");
6717 if( zTemp
==0 ) zTemp
= getenv("TMP");
6725 p
->zTempFile
= sqlite3_mprintf("%s/temp%llx.%s", zTemp
, r
, zSuffix
);
6727 p
->zTempFile
= sqlite3_mprintf("%z.%s", p
->zTempFile
, zSuffix
);
6729 shell_check_oom(p
->zTempFile
);
6734 ** The implementation of SQL scalar function fkey_collate_clause(), used
6735 ** by the ".lint fkey-indexes" command. This scalar function is always
6736 ** called with four arguments - the parent table name, the parent column name,
6737 ** the child table name and the child column name.
6739 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
6741 ** If either of the named tables or columns do not exist, this function
6742 ** returns an empty string. An empty string is also returned if both tables
6743 ** and columns exist but have the same default collation sequence. Or,
6744 ** if both exist but the default collation sequences are different, this
6745 ** function returns the string " COLLATE <parent-collation>", where
6746 ** <parent-collation> is the default collation sequence of the parent column.
6748 static void shellFkeyCollateClause(
6749 sqlite3_context
*pCtx
,
6751 sqlite3_value
**apVal
6753 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
6754 const char *zParent
;
6755 const char *zParentCol
;
6756 const char *zParentSeq
;
6758 const char *zChildCol
;
6759 const char *zChildSeq
= 0; /* Initialize to avoid false-positive warning */
6763 zParent
= (const char*)sqlite3_value_text(apVal
[0]);
6764 zParentCol
= (const char*)sqlite3_value_text(apVal
[1]);
6765 zChild
= (const char*)sqlite3_value_text(apVal
[2]);
6766 zChildCol
= (const char*)sqlite3_value_text(apVal
[3]);
6768 sqlite3_result_text(pCtx
, "", -1, SQLITE_STATIC
);
6769 rc
= sqlite3_table_column_metadata(
6770 db
, "main", zParent
, zParentCol
, 0, &zParentSeq
, 0, 0, 0
6772 if( rc
==SQLITE_OK
){
6773 rc
= sqlite3_table_column_metadata(
6774 db
, "main", zChild
, zChildCol
, 0, &zChildSeq
, 0, 0, 0
6778 if( rc
==SQLITE_OK
&& sqlite3_stricmp(zParentSeq
, zChildSeq
) ){
6779 char *z
= sqlite3_mprintf(" COLLATE %s", zParentSeq
);
6780 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
6787 ** The implementation of dot-command ".lint fkey-indexes".
6789 static int lintFkeyIndexes(
6790 ShellState
*pState
, /* Current shell tool state */
6791 char **azArg
, /* Array of arguments passed to dot command */
6792 int nArg
/* Number of entries in azArg[] */
6794 sqlite3
*db
= pState
->db
; /* Database handle to query "main" db of */
6795 int bVerbose
= 0; /* If -verbose is present */
6796 int bGroupByParent
= 0; /* If -groupbyparent is present */
6797 int i
; /* To iterate through azArg[] */
6798 const char *zIndent
= ""; /* How much to indent CREATE INDEX by */
6799 int rc
; /* Return code */
6800 sqlite3_stmt
*pSql
= 0; /* Compiled version of SQL statement below */
6803 ** This SELECT statement returns one row for each foreign key constraint
6804 ** in the schema of the main database. The column values are:
6806 ** 0. The text of an SQL statement similar to:
6808 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
6810 ** This SELECT is similar to the one that the foreign keys implementation
6811 ** needs to run internally on child tables. If there is an index that can
6812 ** be used to optimize this query, then it can also be used by the FK
6813 ** implementation to optimize DELETE or UPDATE statements on the parent
6816 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
6817 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
6818 ** contains an index that can be used to optimize the query.
6820 ** 2. Human readable text that describes the child table and columns. e.g.
6822 ** "child_table(child_key1, child_key2)"
6824 ** 3. Human readable text that describes the parent table and columns. e.g.
6826 ** "parent_table(parent_key1, parent_key2)"
6828 ** 4. A full CREATE INDEX statement for an index that could be used to
6829 ** optimize DELETE or UPDATE statements on the parent table. e.g.
6831 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
6833 ** 5. The name of the parent table.
6835 ** These six values are used by the C logic below to generate the report.
6839 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
6840 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
6841 " || fkey_collate_clause("
6842 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
6844 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
6845 " || group_concat('*=?', ' AND ') || ')'"
6847 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
6849 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
6851 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
6852 " || ' ON ' || quote(s.name) || '('"
6853 " || group_concat(quote(f.[from]) ||"
6854 " fkey_collate_clause("
6855 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
6859 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
6860 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
6861 "GROUP BY s.name, f.id "
6862 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
6864 const char *zGlobIPK
= "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
6866 for(i
=2; i
<nArg
; i
++){
6867 int n
= strlen30(azArg
[i
]);
6868 if( n
>1 && sqlite3_strnicmp("-verbose", azArg
[i
], n
)==0 ){
6871 else if( n
>1 && sqlite3_strnicmp("-groupbyparent", azArg
[i
], n
)==0 ){
6876 eputf("Usage: %s %s ?-verbose? ?-groupbyparent?\n", azArg
[0], azArg
[1]);
6877 return SQLITE_ERROR
;
6881 /* Register the fkey_collate_clause() SQL function */
6882 rc
= sqlite3_create_function(db
, "fkey_collate_clause", 4, SQLITE_UTF8
,
6883 0, shellFkeyCollateClause
, 0, 0
6887 if( rc
==SQLITE_OK
){
6888 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pSql
, 0);
6890 if( rc
==SQLITE_OK
){
6891 sqlite3_bind_int(pSql
, 1, bGroupByParent
);
6894 if( rc
==SQLITE_OK
){
6897 while( SQLITE_ROW
==sqlite3_step(pSql
) ){
6899 sqlite3_stmt
*pExplain
= 0;
6900 const char *zEQP
= (const char*)sqlite3_column_text(pSql
, 0);
6901 const char *zGlob
= (const char*)sqlite3_column_text(pSql
, 1);
6902 const char *zFrom
= (const char*)sqlite3_column_text(pSql
, 2);
6903 const char *zTarget
= (const char*)sqlite3_column_text(pSql
, 3);
6904 const char *zCI
= (const char*)sqlite3_column_text(pSql
, 4);
6905 const char *zParent
= (const char*)sqlite3_column_text(pSql
, 5);
6907 if( zEQP
==0 ) continue;
6908 if( zGlob
==0 ) continue;
6909 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
6910 if( rc
!=SQLITE_OK
) break;
6911 if( SQLITE_ROW
==sqlite3_step(pExplain
) ){
6912 const char *zPlan
= (const char*)sqlite3_column_text(pExplain
, 3);
6913 res
= zPlan
!=0 && ( 0==sqlite3_strglob(zGlob
, zPlan
)
6914 || 0==sqlite3_strglob(zGlobIPK
, zPlan
));
6916 rc
= sqlite3_finalize(pExplain
);
6917 if( rc
!=SQLITE_OK
) break;
6920 eputz("Error: internal error");
6924 && (bVerbose
|| res
==0)
6925 && (zPrev
==0 || sqlite3_stricmp(zParent
, zPrev
))
6927 oputf("-- Parent table %s\n", zParent
);
6928 sqlite3_free(zPrev
);
6929 zPrev
= sqlite3_mprintf("%s", zParent
);
6933 oputf("%s%s --> %s\n", zIndent
, zCI
, zTarget
);
6934 }else if( bVerbose
){
6935 oputf("%s/* no extra indexes required for %s -> %s */\n",
6936 zIndent
, zFrom
, zTarget
6941 sqlite3_free(zPrev
);
6943 if( rc
!=SQLITE_OK
){
6944 eputf("%s\n", sqlite3_errmsg(db
));
6947 rc2
= sqlite3_finalize(pSql
);
6948 if( rc
==SQLITE_OK
&& rc2
!=SQLITE_OK
){
6950 eputf("%s\n", sqlite3_errmsg(db
));
6953 eputf("%s\n", sqlite3_errmsg(db
));
6960 ** Implementation of ".lint" dot command.
6962 static int lintDotCommand(
6963 ShellState
*pState
, /* Current shell tool state */
6964 char **azArg
, /* Array of arguments passed to dot command */
6965 int nArg
/* Number of entries in azArg[] */
6968 n
= (nArg
>=2 ? strlen30(azArg
[1]) : 0);
6969 if( n
<1 || sqlite3_strnicmp(azArg
[1], "fkey-indexes", n
) ) goto usage
;
6970 return lintFkeyIndexes(pState
, azArg
, nArg
);
6973 eputf("Usage %s sub-command ?switches...?\n", azArg
[0]);
6974 eputz("Where sub-commands are:\n");
6975 eputz(" fkey-indexes\n");
6976 return SQLITE_ERROR
;
6979 static void shellPrepare(
6983 sqlite3_stmt
**ppStmt
6986 if( *pRc
==SQLITE_OK
){
6987 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
6988 if( rc
!=SQLITE_OK
){
6989 eputf("sql error: %s (%d)\n", sqlite3_errmsg(db
), sqlite3_errcode(db
));
6996 ** Create a prepared statement using printf-style arguments for the SQL.
6998 static void shellPreparePrintf(
7001 sqlite3_stmt
**ppStmt
,
7006 if( *pRc
==SQLITE_OK
){
7010 z
= sqlite3_vmprintf(zFmt
, ap
);
7013 *pRc
= SQLITE_NOMEM
;
7015 shellPrepare(db
, pRc
, z
, ppStmt
);
7022 ** Finalize the prepared statement created using shellPreparePrintf().
7024 static void shellFinalize(
7029 sqlite3
*db
= sqlite3_db_handle(pStmt
);
7030 int rc
= sqlite3_finalize(pStmt
);
7031 if( *pRc
==SQLITE_OK
){
7032 if( rc
!=SQLITE_OK
){
7033 eputf("SQL error: %s\n", sqlite3_errmsg(db
));
7040 #if !defined SQLITE_OMIT_VIRTUALTABLE
7041 /* Reset the prepared statement created using shellPreparePrintf().
7043 ** This routine is could be marked "static". But it is not always used,
7044 ** depending on compile-time options. By omitting the "static", we avoid
7045 ** nuisance compiler warnings about "defined but not used".
7051 int rc
= sqlite3_reset(pStmt
);
7052 if( *pRc
==SQLITE_OK
){
7053 if( rc
!=SQLITE_OK
){
7054 sqlite3
*db
= sqlite3_db_handle(pStmt
);
7055 eputf("SQL error: %s\n", sqlite3_errmsg(db
));
7060 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
7062 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7063 /******************************************************************************
7064 ** The ".archive" or ".ar" command.
7067 ** Structure representing a single ".ar" command.
7069 typedef struct ArCommand ArCommand
;
7071 u8 eCmd
; /* An AR_CMD_* value */
7072 u8 bVerbose
; /* True if --verbose */
7073 u8 bZip
; /* True if the archive is a ZIP */
7074 u8 bDryRun
; /* True if --dry-run */
7075 u8 bAppend
; /* True if --append */
7076 u8 bGlob
; /* True if --glob */
7077 u8 fromCmdLine
; /* Run from -A instead of .archive */
7078 int nArg
; /* Number of command arguments */
7079 char *zSrcTable
; /* "sqlar", "zipfile($file)" or "zip" */
7080 const char *zFile
; /* --file argument, or NULL */
7081 const char *zDir
; /* --directory argument, or NULL */
7082 char **azArg
; /* Array of command arguments */
7083 ShellState
*p
; /* Shell state */
7084 sqlite3
*db
; /* Database containing the archive */
7088 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
7090 static int arUsage(FILE *f
){
7091 showHelp(f
,"archive");
7092 return SQLITE_ERROR
;
7096 ** Print an error message for the .ar command to stderr and return
7099 static int arErrorMsg(ArCommand
*pAr
, const char *zFmt
, ...){
7103 z
= sqlite3_vmprintf(zFmt
, ap
);
7106 if( pAr
->fromCmdLine
){
7107 eputz("Use \"-A\" for more help\n");
7109 eputz("Use \".archive --help\" for more help\n");
7112 return SQLITE_ERROR
;
7116 ** Values for ArCommand.eCmd.
7118 #define AR_CMD_CREATE 1
7119 #define AR_CMD_UPDATE 2
7120 #define AR_CMD_INSERT 3
7121 #define AR_CMD_EXTRACT 4
7122 #define AR_CMD_LIST 5
7123 #define AR_CMD_HELP 6
7124 #define AR_CMD_REMOVE 7
7127 ** Other (non-command) switches.
7129 #define AR_SWITCH_VERBOSE 8
7130 #define AR_SWITCH_FILE 9
7131 #define AR_SWITCH_DIRECTORY 10
7132 #define AR_SWITCH_APPEND 11
7133 #define AR_SWITCH_DRYRUN 12
7134 #define AR_SWITCH_GLOB 13
7136 static int arProcessSwitch(ArCommand
*pAr
, int eSwitch
, const char *zArg
){
7139 case AR_CMD_EXTRACT
:
7146 return arErrorMsg(pAr
, "multiple command options");
7148 pAr
->eCmd
= eSwitch
;
7151 case AR_SWITCH_DRYRUN
:
7154 case AR_SWITCH_GLOB
:
7157 case AR_SWITCH_VERBOSE
:
7160 case AR_SWITCH_APPEND
:
7162 deliberate_fall_through
;
7163 case AR_SWITCH_FILE
:
7166 case AR_SWITCH_DIRECTORY
:
7175 ** Parse the command line for an ".ar" command. The results are written into
7176 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
7177 ** successfully, otherwise an error message is written to stderr and
7178 ** SQLITE_ERROR returned.
7180 static int arParseCommand(
7181 char **azArg
, /* Array of arguments passed to dot command */
7182 int nArg
, /* Number of entries in azArg[] */
7183 ArCommand
*pAr
/* Populate this object */
7191 { "create", 'c', AR_CMD_CREATE
, 0 },
7192 { "extract", 'x', AR_CMD_EXTRACT
, 0 },
7193 { "insert", 'i', AR_CMD_INSERT
, 0 },
7194 { "list", 't', AR_CMD_LIST
, 0 },
7195 { "remove", 'r', AR_CMD_REMOVE
, 0 },
7196 { "update", 'u', AR_CMD_UPDATE
, 0 },
7197 { "help", 'h', AR_CMD_HELP
, 0 },
7198 { "verbose", 'v', AR_SWITCH_VERBOSE
, 0 },
7199 { "file", 'f', AR_SWITCH_FILE
, 1 },
7200 { "append", 'a', AR_SWITCH_APPEND
, 1 },
7201 { "directory", 'C', AR_SWITCH_DIRECTORY
, 1 },
7202 { "dryrun", 'n', AR_SWITCH_DRYRUN
, 0 },
7203 { "glob", 'g', AR_SWITCH_GLOB
, 0 },
7205 int nSwitch
= sizeof(aSwitch
) / sizeof(struct ArSwitch
);
7206 struct ArSwitch
*pEnd
= &aSwitch
[nSwitch
];
7209 eputz("Wrong number of arguments. Usage:\n");
7210 return arUsage(stderr
);
7214 /* Traditional style [tar] invocation */
7217 for(i
=0; z
[i
]; i
++){
7218 const char *zArg
= 0;
7219 struct ArSwitch
*pOpt
;
7220 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7221 if( z
[i
]==pOpt
->cShort
) break;
7224 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
7228 return arErrorMsg(pAr
, "option requires an argument: %c",z
[i
]);
7230 zArg
= azArg
[iArg
++];
7232 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7234 pAr
->nArg
= nArg
-iArg
;
7236 pAr
->azArg
= &azArg
[iArg
];
7239 /* Non-traditional invocation */
7241 for(iArg
=1; iArg
<nArg
; iArg
++){
7245 /* All remaining command line words are command arguments. */
7246 pAr
->azArg
= &azArg
[iArg
];
7247 pAr
->nArg
= nArg
-iArg
;
7254 /* One or more short options */
7256 const char *zArg
= 0;
7257 struct ArSwitch
*pOpt
;
7258 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7259 if( z
[i
]==pOpt
->cShort
) break;
7262 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
7269 if( iArg
>=(nArg
-1) ){
7270 return arErrorMsg(pAr
, "option requires an argument: %c",
7273 zArg
= azArg
[++iArg
];
7276 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7278 }else if( z
[2]=='\0' ){
7279 /* A -- option, indicating that all remaining command line words
7280 ** are command arguments. */
7281 pAr
->azArg
= &azArg
[iArg
+1];
7282 pAr
->nArg
= nArg
-iArg
-1;
7286 const char *zArg
= 0; /* Argument for option, if any */
7287 struct ArSwitch
*pMatch
= 0; /* Matching option */
7288 struct ArSwitch
*pOpt
; /* Iterator */
7289 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
7290 const char *zLong
= pOpt
->zLong
;
7291 if( (n
-2)<=strlen30(zLong
) && 0==memcmp(&z
[2], zLong
, n
-2) ){
7293 return arErrorMsg(pAr
, "ambiguous option: %s",z
);
7301 return arErrorMsg(pAr
, "unrecognized option: %s", z
);
7304 if( iArg
>=(nArg
-1) ){
7305 return arErrorMsg(pAr
, "option requires an argument: %s", z
);
7307 zArg
= azArg
[++iArg
];
7309 if( arProcessSwitch(pAr
, pMatch
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
7315 eputz("Required argument missing. Usage:\n");
7316 return arUsage(stderr
);
7322 ** This function assumes that all arguments within the ArCommand.azArg[]
7323 ** array refer to archive members, as for the --extract, --list or --remove
7324 ** commands. It checks that each of them are "present". If any specified
7325 ** file is not present in the archive, an error is printed to stderr and an
7326 ** error code returned. Otherwise, if all specified arguments are present
7327 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
7328 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
7329 ** when pAr->bGlob is true.
7331 ** This function strips any trailing '/' characters from each argument.
7332 ** This is consistent with the way the [tar] command seems to work on
7335 static int arCheckEntries(ArCommand
*pAr
){
7339 sqlite3_stmt
*pTest
= 0;
7340 const char *zSel
= (pAr
->bGlob
)
7341 ? "SELECT name FROM %s WHERE glob($name,name)"
7342 : "SELECT name FROM %s WHERE name=$name";
7344 shellPreparePrintf(pAr
->db
, &rc
, &pTest
, zSel
, pAr
->zSrcTable
);
7345 j
= sqlite3_bind_parameter_index(pTest
, "$name");
7346 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
7347 char *z
= pAr
->azArg
[i
];
7348 int n
= strlen30(z
);
7350 while( n
>0 && z
[n
-1]=='/' ) n
--;
7352 sqlite3_bind_text(pTest
, j
, z
, -1, SQLITE_STATIC
);
7353 if( SQLITE_ROW
==sqlite3_step(pTest
) ){
7356 shellReset(&rc
, pTest
);
7357 if( rc
==SQLITE_OK
&& bOk
==0 ){
7358 eputf("not found in archive: %s\n", z
);
7362 shellFinalize(&rc
, pTest
);
7368 ** Format a WHERE clause that can be used against the "sqlar" table to
7369 ** identify all archive members that match the command arguments held
7370 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
7371 ** The caller is responsible for eventually calling sqlite3_free() on
7372 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
7373 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
7375 static void arWhereClause(
7378 char **pzWhere
/* OUT: New WHERE clause */
7381 const char *zSameOp
= (pAr
->bGlob
)? "GLOB" : "=";
7382 if( *pRc
==SQLITE_OK
){
7384 zWhere
= sqlite3_mprintf("1");
7387 const char *zSep
= "";
7388 for(i
=0; i
<pAr
->nArg
; i
++){
7389 const char *z
= pAr
->azArg
[i
];
7390 zWhere
= sqlite3_mprintf(
7391 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
7392 zWhere
, zSep
, zSameOp
, z
, strlen30(z
)+1, zSameOp
, z
7395 *pRc
= SQLITE_NOMEM
;
7406 ** Implementation of .ar "lisT" command.
7408 static int arListCommand(ArCommand
*pAr
){
7409 const char *zSql
= "SELECT %s FROM %s WHERE %s";
7410 const char *azCols
[] = {
7412 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
7416 sqlite3_stmt
*pSql
= 0;
7419 rc
= arCheckEntries(pAr
);
7420 arWhereClause(&rc
, pAr
, &zWhere
);
7422 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql
, azCols
[pAr
->bVerbose
],
7423 pAr
->zSrcTable
, zWhere
);
7425 oputf("%s\n", sqlite3_sql(pSql
));
7427 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
7428 if( pAr
->bVerbose
){
7429 oputf("%s % 10d %s %s\n",
7430 sqlite3_column_text(pSql
, 0), sqlite3_column_int(pSql
, 1),
7431 sqlite3_column_text(pSql
, 2),sqlite3_column_text(pSql
, 3));
7433 oputf("%s\n", sqlite3_column_text(pSql
, 0));
7437 shellFinalize(&rc
, pSql
);
7438 sqlite3_free(zWhere
);
7443 ** Implementation of .ar "Remove" command.
7445 static int arRemoveCommand(ArCommand
*pAr
){
7451 /* Verify that args actually exist within the archive before proceeding.
7452 ** And formulate a WHERE clause to match them. */
7453 rc
= arCheckEntries(pAr
);
7454 arWhereClause(&rc
, pAr
, &zWhere
);
7456 if( rc
==SQLITE_OK
){
7457 zSql
= sqlite3_mprintf("DELETE FROM %s WHERE %s;",
7458 pAr
->zSrcTable
, zWhere
);
7460 oputf("%s\n", zSql
);
7463 rc
= sqlite3_exec(pAr
->db
, "SAVEPOINT ar;", 0, 0, 0);
7464 if( rc
==SQLITE_OK
){
7465 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
7466 if( rc
!=SQLITE_OK
){
7467 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7469 rc
= sqlite3_exec(pAr
->db
, "RELEASE ar;", 0, 0, 0);
7473 sputf(stdout
, "ERROR: %s\n", zErr
); /* stdout? */
7478 sqlite3_free(zWhere
);
7484 ** Implementation of .ar "eXtract" command.
7486 static int arExtractCommand(ArCommand
*pAr
){
7490 " writefile(($dir || name), %s, mode, mtime) "
7491 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
7492 " AND name NOT GLOB '*..[/\\]*'";
7494 const char *azExtraArg
[] = {
7495 "sqlar_uncompress(data, sz)",
7499 sqlite3_stmt
*pSql
= 0;
7505 /* If arguments are specified, check that they actually exist within
7506 ** the archive before proceeding. And formulate a WHERE clause to
7508 rc
= arCheckEntries(pAr
);
7509 arWhereClause(&rc
, pAr
, &zWhere
);
7511 if( rc
==SQLITE_OK
){
7513 zDir
= sqlite3_mprintf("%s/", pAr
->zDir
);
7515 zDir
= sqlite3_mprintf("");
7517 if( zDir
==0 ) rc
= SQLITE_NOMEM
;
7520 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql1
,
7521 azExtraArg
[pAr
->bZip
], pAr
->zSrcTable
, zWhere
7524 if( rc
==SQLITE_OK
){
7525 j
= sqlite3_bind_parameter_index(pSql
, "$dir");
7526 sqlite3_bind_text(pSql
, j
, zDir
, -1, SQLITE_STATIC
);
7528 /* Run the SELECT statement twice. The first time, writefile() is called
7529 ** for all archive members that should be extracted. The second time,
7530 ** only for the directories. This is because the timestamps for
7531 ** extracted directories must be reset after they are populated (as
7532 ** populating them changes the timestamp). */
7534 j
= sqlite3_bind_parameter_index(pSql
, "$dirOnly");
7535 sqlite3_bind_int(pSql
, j
, i
);
7537 oputf("%s\n", sqlite3_sql(pSql
));
7539 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
7540 if( i
==0 && pAr
->bVerbose
){
7541 oputf("%s\n", sqlite3_column_text(pSql
, 0));
7545 shellReset(&rc
, pSql
);
7547 shellFinalize(&rc
, pSql
);
7551 sqlite3_free(zWhere
);
7556 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
7558 static int arExecSql(ArCommand
*pAr
, const char *zSql
){
7561 oputf("%s\n", zSql
);
7565 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
7567 sputf(stdout
, "ERROR: %s\n", zErr
);
7576 ** Implementation of .ar "create", "insert", and "update" commands.
7578 ** create -> Create a new SQL archive
7579 ** insert -> Insert or reinsert all files listed
7580 ** update -> Insert files that have changed or that were not
7581 ** previously in the archive
7583 ** Create the "sqlar" table in the database if it does not already exist.
7584 ** Then add each file in the azFile[] array to the archive. Directories
7585 ** are added recursively. If argument bVerbose is non-zero, a message is
7586 ** printed on stdout for each file archived.
7588 ** The create command is the same as update, except that it drops
7589 ** any existing "sqlar" table before beginning. The "insert" command
7590 ** always overwrites every file named on the command-line, where as
7591 ** "update" only overwrites if the size or mtime or mode has changed.
7593 static int arCreateOrUpdateCommand(
7594 ArCommand
*pAr
, /* Command arguments and options */
7595 int bUpdate
, /* true for a --create. */
7596 int bOnlyIfChanged
/* Only update if file has changed */
7598 const char *zCreate
=
7599 "CREATE TABLE IF NOT EXISTS sqlar(\n"
7600 " name TEXT PRIMARY KEY, -- name of the file\n"
7601 " mode INT, -- access permissions\n"
7602 " mtime INT, -- last modification time\n"
7603 " sz INT, -- original file size\n"
7604 " data BLOB -- compressed content\n"
7606 const char *zDrop
= "DROP TABLE IF EXISTS sqlar";
7607 const char *zInsertFmt
[2] = {
7608 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
7613 " CASE substr(lsmode(mode),1,1)\n"
7614 " WHEN '-' THEN length(data)\n"
7615 " WHEN 'd' THEN 0\n"
7617 " sqlar_compress(data)\n"
7618 " FROM fsdir(%Q,%Q) AS disk\n"
7619 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7621 "REPLACE INTO %s(name,mode,mtime,data)\n"
7627 " FROM fsdir(%Q,%Q) AS disk\n"
7628 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
7630 int i
; /* For iterating through azFile[] */
7631 int rc
; /* Return code */
7632 const char *zTab
= 0; /* SQL table into which to insert */
7637 arExecSql(pAr
, "PRAGMA page_size=512");
7638 rc
= arExecSql(pAr
, "SAVEPOINT ar;");
7639 if( rc
!=SQLITE_OK
) return rc
;
7642 /* Initialize the zipfile virtual table, if necessary */
7645 sqlite3_randomness(sizeof(r
),&r
);
7646 sqlite3_snprintf(sizeof(zTemp
),zTemp
,"zip%016llx",r
);
7648 zSql
= sqlite3_mprintf(
7649 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
7652 rc
= arExecSql(pAr
, zSql
);
7658 /* Initialize the table for an SQLAR */
7661 rc
= arExecSql(pAr
, zDrop
);
7662 if( rc
!=SQLITE_OK
) goto end_ar_transaction
;
7664 rc
= arExecSql(pAr
, zCreate
);
7666 if( bOnlyIfChanged
){
7667 zExists
= sqlite3_mprintf(
7669 "SELECT 1 FROM %s AS mem"
7670 " WHERE mem.name=disk.name"
7671 " AND mem.mtime=disk.mtime"
7672 " AND mem.mode=disk.mode)", zTab
);
7674 zExists
= sqlite3_mprintf("");
7676 if( zExists
==0 ) rc
= SQLITE_NOMEM
;
7677 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
7678 char *zSql2
= sqlite3_mprintf(zInsertFmt
[pAr
->bZip
], zTab
,
7679 pAr
->bVerbose
? "shell_putsnl(name)" : "name",
7680 pAr
->azArg
[i
], pAr
->zDir
, zExists
);
7681 rc
= arExecSql(pAr
, zSql2
);
7682 sqlite3_free(zSql2
);
7685 if( rc
!=SQLITE_OK
){
7686 sqlite3_exec(pAr
->db
, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
7688 rc
= arExecSql(pAr
, "RELEASE ar;");
7689 if( pAr
->bZip
&& pAr
->zFile
){
7690 zSql
= sqlite3_mprintf("DROP TABLE %s", zTemp
);
7691 arExecSql(pAr
, zSql
);
7695 sqlite3_free(zExists
);
7700 ** Implementation of ".ar" dot command.
7702 static int arDotCommand(
7703 ShellState
*pState
, /* Current shell tool state */
7704 int fromCmdLine
, /* True if -A command-line option, not .ar cmd */
7705 char **azArg
, /* Array of arguments passed to dot command */
7706 int nArg
/* Number of entries in azArg[] */
7710 memset(&cmd
, 0, sizeof(cmd
));
7711 cmd
.fromCmdLine
= fromCmdLine
;
7712 rc
= arParseCommand(azArg
, nArg
, &cmd
);
7713 if( rc
==SQLITE_OK
){
7714 int eDbType
= SHELL_OPEN_UNSPEC
;
7716 cmd
.db
= pState
->db
;
7718 eDbType
= deduceDatabaseType(cmd
.zFile
, 1);
7720 eDbType
= pState
->openMode
;
7722 if( eDbType
==SHELL_OPEN_ZIPFILE
){
7723 if( cmd
.eCmd
==AR_CMD_EXTRACT
|| cmd
.eCmd
==AR_CMD_LIST
){
7725 cmd
.zSrcTable
= sqlite3_mprintf("zip");
7727 cmd
.zSrcTable
= sqlite3_mprintf("zipfile(%Q)", cmd
.zFile
);
7731 }else if( cmd
.zFile
){
7733 if( cmd
.bAppend
) eDbType
= SHELL_OPEN_APPENDVFS
;
7734 if( cmd
.eCmd
==AR_CMD_CREATE
|| cmd
.eCmd
==AR_CMD_INSERT
7735 || cmd
.eCmd
==AR_CMD_REMOVE
|| cmd
.eCmd
==AR_CMD_UPDATE
){
7736 flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
7738 flags
= SQLITE_OPEN_READONLY
;
7742 oputf("-- open database '%s'%s\n", cmd
.zFile
,
7743 eDbType
==SHELL_OPEN_APPENDVFS
? " using 'apndvfs'" : "");
7745 rc
= sqlite3_open_v2(cmd
.zFile
, &cmd
.db
, flags
,
7746 eDbType
==SHELL_OPEN_APPENDVFS
? "apndvfs" : 0);
7747 if( rc
!=SQLITE_OK
){
7748 eputf("cannot open file: %s (%s)\n", cmd
.zFile
, sqlite3_errmsg(cmd
.db
));
7749 goto end_ar_command
;
7751 sqlite3_fileio_init(cmd
.db
, 0, 0);
7752 sqlite3_sqlar_init(cmd
.db
, 0, 0);
7753 sqlite3_create_function(cmd
.db
, "shell_putsnl", 1, SQLITE_UTF8
, cmd
.p
,
7754 shellPutsFunc
, 0, 0);
7757 if( cmd
.zSrcTable
==0 && cmd
.bZip
==0 && cmd
.eCmd
!=AR_CMD_HELP
){
7758 if( cmd
.eCmd
!=AR_CMD_CREATE
7759 && sqlite3_table_column_metadata(cmd
.db
,0,"sqlar","name",0,0,0,0,0)
7761 eputz("database does not contain an 'sqlar' table\n");
7763 goto end_ar_command
;
7765 cmd
.zSrcTable
= sqlite3_mprintf("sqlar");
7770 rc
= arCreateOrUpdateCommand(&cmd
, 0, 0);
7773 case AR_CMD_EXTRACT
:
7774 rc
= arExtractCommand(&cmd
);
7778 rc
= arListCommand(&cmd
);
7782 arUsage(pState
->out
);
7786 rc
= arCreateOrUpdateCommand(&cmd
, 1, 0);
7790 rc
= arRemoveCommand(&cmd
);
7794 assert( cmd
.eCmd
==AR_CMD_UPDATE
);
7795 rc
= arCreateOrUpdateCommand(&cmd
, 1, 1);
7800 if( cmd
.db
!=pState
->db
){
7803 sqlite3_free(cmd
.zSrcTable
);
7807 /* End of the ".archive" or ".ar" command logic
7808 *******************************************************************************/
7809 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
7811 #if SQLITE_SHELL_HAVE_RECOVER
7814 ** This function is used as a callback by the recover extension. Simply
7815 ** print the supplied SQL statement to stdout.
7817 static int recoverSqlCb(void *pCtx
, const char *zSql
){
7818 ShellState
*pState
= (ShellState
*)pCtx
;
7819 sputf(pState
->out
, "%s;\n", zSql
);
7824 ** This function is called to recover data from the database. A script
7825 ** to construct a new database containing all recovered data is output
7826 ** on stream pState->out.
7828 static int recoverDatabaseCmd(ShellState
*pState
, int nArg
, char **azArg
){
7830 const char *zRecoveryDb
= ""; /* Name of "recovery" database. Debug only */
7831 const char *zLAF
= "lost_and_found";
7832 int bFreelist
= 1; /* 0 if --ignore-freelist is specified */
7833 int bRowids
= 1; /* 0 if --no-rowids */
7834 sqlite3_recover
*p
= 0;
7837 for(i
=1; i
<nArg
; i
++){
7840 if( z
[0]=='-' && z
[1]=='-' ) z
++;
7842 if( n
<=17 && memcmp("-ignore-freelist", z
, n
)==0 ){
7845 if( n
<=12 && memcmp("-recovery-db", z
, n
)==0 && i
<(nArg
-1) ){
7846 /* This option determines the name of the ATTACH-ed database used
7847 ** internally by the recovery extension. The default is "" which
7848 ** means to use a temporary database that is automatically deleted
7849 ** when closed. This option is undocumented and might disappear at
7852 zRecoveryDb
= azArg
[i
];
7854 if( n
<=15 && memcmp("-lost-and-found", z
, n
)==0 && i
<(nArg
-1) ){
7858 if( n
<=10 && memcmp("-no-rowids", z
, n
)==0 ){
7862 eputf("unexpected option: %s\n", azArg
[i
]);
7863 showHelp(pState
->out
, azArg
[0]);
7868 p
= sqlite3_recover_init_sql(
7869 pState
->db
, "main", recoverSqlCb
, (void*)pState
7872 sqlite3_recover_config(p
, 789, (void*)zRecoveryDb
); /* Debug use only */
7873 sqlite3_recover_config(p
, SQLITE_RECOVER_LOST_AND_FOUND
, (void*)zLAF
);
7874 sqlite3_recover_config(p
, SQLITE_RECOVER_ROWIDS
, (void*)&bRowids
);
7875 sqlite3_recover_config(p
, SQLITE_RECOVER_FREELIST_CORRUPT
,(void*)&bFreelist
);
7877 sqlite3_recover_run(p
);
7878 if( sqlite3_recover_errcode(p
)!=SQLITE_OK
){
7879 const char *zErr
= sqlite3_recover_errmsg(p
);
7880 int errCode
= sqlite3_recover_errcode(p
);
7881 eputf("sql error: %s (%d)\n", zErr
, errCode
);
7883 rc
= sqlite3_recover_finish(p
);
7886 #endif /* SQLITE_SHELL_HAVE_RECOVER */
7889 ** Implementation of ".intck STEPS_PER_UNLOCK" command.
7891 static int intckDatabaseCmd(ShellState
*pState
, i64 nStepPerUnlock
){
7892 sqlite3_intck
*p
= 0;
7895 rc
= sqlite3_intck_open(pState
->db
, "main", &p
);
7896 if( rc
==SQLITE_OK
){
7899 const char *zErr
= 0;
7900 while( SQLITE_OK
==sqlite3_intck_step(p
) ){
7901 const char *zMsg
= sqlite3_intck_message(p
);
7903 oputf("%s\n", zMsg
);
7907 if( nStepPerUnlock
&& (nStep
% nStepPerUnlock
)==0 ){
7908 sqlite3_intck_unlock(p
);
7911 rc
= sqlite3_intck_error(p
, &zErr
);
7913 eputf("%s\n", zErr
);
7915 sqlite3_intck_close(p
);
7917 oputf("%lld steps, %lld errors\n", nStep
, nError
);
7924 * zAutoColumn(zCol, &db, ?) => Maybe init db, add column zCol to it.
7925 * zAutoColumn(0, &db, ?) => (db!=0) Form columns spec for CREATE TABLE,
7926 * close db and set it to 0, and return the columns spec, to later
7927 * be sqlite3_free()'ed by the caller.
7928 * The return is 0 when either:
7929 * (a) The db was not initialized and zCol==0 (There are no columns.)
7930 * (b) zCol!=0 (Column was added, db initialized as needed.)
7931 * The 3rd argument, pRenamed, references an out parameter. If the
7932 * pointer is non-zero, its referent will be set to a summary of renames
7933 * done if renaming was necessary, or set to 0 if none was done. The out
7934 * string (if any) must be sqlite3_free()'ed by the caller.
7937 #define rc_err_oom_die(rc) \
7938 if( rc==SQLITE_NOMEM ) shell_check_oom(0); \
7939 else if(!(rc==SQLITE_OK||rc==SQLITE_DONE)) \
7940 eputf("E:%d\n",rc), assert(0)
7942 static void rc_err_oom_die(int rc
){
7943 if( rc
==SQLITE_NOMEM
) shell_check_oom(0);
7944 assert(rc
==SQLITE_OK
||rc
==SQLITE_DONE
);
7948 #ifdef SHELL_COLFIX_DB /* If this is set, the DB can be in a file. */
7949 static char zCOL_DB
[] = SHELL_STRINGIFY(SHELL_COLFIX_DB
);
7950 #else /* Otherwise, memory is faster/better for the transient DB. */
7951 static const char *zCOL_DB
= ":memory:";
7954 /* Define character (as C string) to separate generated column ordinal
7955 * from protected part of incoming column names. This defaults to "_"
7956 * so that incoming column identifiers that did not need not be quoted
7957 * remain usable without being quoted. It must be one character.
7959 #ifndef SHELL_AUTOCOLUMN_SEP
7960 # define AUTOCOLUMN_SEP "_"
7962 # define AUTOCOLUMN_SEP SHELL_STRINGIFY(SHELL_AUTOCOLUMN_SEP)
7965 static char *zAutoColumn(const char *zColNew
, sqlite3
**pDb
, char **pzRenamed
){
7966 /* Queries and D{D,M}L used here */
7967 static const char * const zTabMake
= "\
7968 CREATE TABLE ColNames(\
7969 cpos INTEGER PRIMARY KEY,\
7970 name TEXT, nlen INT, chop INT, reps INT, suff TEXT);\
7971 CREATE VIEW RepeatedNames AS \
7972 SELECT DISTINCT t.name FROM ColNames t \
7973 WHERE t.name COLLATE NOCASE IN (\
7974 SELECT o.name FROM ColNames o WHERE o.cpos<>t.cpos\
7977 static const char * const zTabFill
= "\
7978 INSERT INTO ColNames(name,nlen,chop,reps,suff)\
7979 VALUES(iif(length(?1)>0,?1,'?'),max(length(?1),1),0,0,'')\
7981 static const char * const zHasDupes
= "\
7982 SELECT count(DISTINCT (substring(name,1,nlen-chop)||suff) COLLATE NOCASE)\
7983 <count(name) FROM ColNames\
7985 #ifdef SHELL_COLUMN_RENAME_CLEAN
7986 static const char * const zDedoctor
= "\
7987 UPDATE ColNames SET chop=iif(\
7988 (substring(name,nlen,1) BETWEEN '0' AND '9')\
7989 AND (rtrim(name,'0123456790') glob '*"AUTOCOLUMN_SEP
"'),\
7990 nlen-length(rtrim(name, '"AUTOCOLUMN_SEP
"0123456789')),\
7995 static const char * const zSetReps
= "\
7996 UPDATE ColNames AS t SET reps=\
7997 (SELECT count(*) FROM ColNames d \
7998 WHERE substring(t.name,1,t.nlen-t.chop)=substring(d.name,1,d.nlen-d.chop)\
8002 #ifdef SQLITE_ENABLE_MATH_FUNCTIONS
8003 static const char * const zColDigits
= "\
8004 SELECT CAST(ceil(log(count(*)+0.5)) AS INT) FROM ColNames \
8007 /* Counting on SQLITE_MAX_COLUMN < 100,000 here. (32767 is the hard limit.) */
8008 static const char * const zColDigits
= "\
8009 SELECT CASE WHEN (nc < 10) THEN 1 WHEN (nc < 100) THEN 2 \
8010 WHEN (nc < 1000) THEN 3 WHEN (nc < 10000) THEN 4 \
8011 ELSE 5 FROM (SELECT count(*) AS nc FROM ColNames) \
8014 static const char * const zRenameRank
=
8015 #ifdef SHELL_COLUMN_RENAME_CLEAN
8016 "UPDATE ColNames AS t SET suff="
8017 "iif(reps>1, printf('%c%0*d', '"AUTOCOLUMN_SEP
"', $1, cpos), '')"
8018 #else /* ...RENAME_MINIMAL_ONE_PASS */
8019 "WITH Lzn(nlz) AS (" /* Find minimum extraneous leading 0's for uniqueness */
8022 " SELECT nlz+1 AS nlz FROM Lzn"
8025 " FROM ColNames t, ColNames o"
8027 " iif(t.name IN (SELECT * FROM RepeatedNames),"
8028 " printf('%s"AUTOCOLUMN_SEP
"%s',"
8029 " t.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,t.cpos),2)),"
8033 " iif(o.name IN (SELECT * FROM RepeatedNames),"
8034 " printf('%s"AUTOCOLUMN_SEP
"%s',"
8035 " o.name, substring(printf('%.*c%0.*d',nlz+1,'0',$1,o.cpos),2)),"
8039 " AND o.cpos<>t.cpos"
8042 ") UPDATE Colnames AS t SET"
8043 " chop = 0," /* No chopping, never touch incoming names. */
8044 " suff = iif(name IN (SELECT * FROM RepeatedNames),"
8045 " printf('"AUTOCOLUMN_SEP
"%s', substring("
8046 " printf('%.*c%0.*d',(SELECT max(nlz) FROM Lzn)+1,'0',1,t.cpos),2)),"
8051 static const char * const zCollectVar
= "\
8056 ','||iif((cpos-1)%4>0, ' ', x'0a'||' '))\
8059 SELECT cpos, printf('\"%w\"',printf('%!.*s%s', nlen-chop,name,suff)) AS cname \
8060 FROM ColNames ORDER BY cpos\
8062 static const char * const zRenamesDone
=
8063 "SELECT group_concat("
8064 " printf('\"%w\" to \"%w\"',name,printf('%!.*s%s', nlen-chop, name, suff)),"
8066 "FROM ColNames WHERE suff<>'' OR chop!=0"
8069 sqlite3_stmt
*pStmt
= 0;
8072 /* Add initial or additional column. Init db if necessary. */
8074 if( SQLITE_OK
!=sqlite3_open(zCOL_DB
, pDb
) ) return 0;
8075 #ifdef SHELL_COLFIX_DB
8077 sqlite3_exec(*pDb
,"drop table if exists ColNames;"
8078 "drop view if exists RepeatedNames;",0,0,0);
8080 #undef SHELL_COLFIX_DB
8081 rc
= sqlite3_exec(*pDb
, zTabMake
, 0, 0, 0);
8085 rc
= sqlite3_prepare_v2(*pDb
, zTabFill
, -1, &pStmt
, 0);
8087 rc
= sqlite3_bind_text(pStmt
, 1, zColNew
, -1, 0);
8089 rc
= sqlite3_step(pStmt
);
8091 sqlite3_finalize(pStmt
);
8093 }else if( *pDb
==0 ){
8096 /* Formulate the columns spec, close the DB, zero *pDb. */
8097 char *zColsSpec
= 0;
8098 int hasDupes
= db_int(*pDb
, zHasDupes
);
8099 int nDigits
= (hasDupes
)? db_int(*pDb
, zColDigits
) : 0;
8101 #ifdef SHELL_COLUMN_RENAME_CLEAN
8102 rc
= sqlite3_exec(*pDb
, zDedoctor
, 0, 0, 0);
8105 rc
= sqlite3_exec(*pDb
, zSetReps
, 0, 0, 0);
8107 rc
= sqlite3_prepare_v2(*pDb
, zRenameRank
, -1, &pStmt
, 0);
8109 sqlite3_bind_int(pStmt
, 1, nDigits
);
8110 rc
= sqlite3_step(pStmt
);
8111 sqlite3_finalize(pStmt
);
8112 if( rc
!=SQLITE_DONE
) rc_err_oom_die(SQLITE_NOMEM
);
8114 assert(db_int(*pDb
, zHasDupes
)==0); /* Consider: remove this */
8115 rc
= sqlite3_prepare_v2(*pDb
, zCollectVar
, -1, &pStmt
, 0);
8117 rc
= sqlite3_step(pStmt
);
8118 if( rc
==SQLITE_ROW
){
8119 zColsSpec
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
8124 if( !hasDupes
) *pzRenamed
= 0;
8126 sqlite3_finalize(pStmt
);
8127 if( SQLITE_OK
==sqlite3_prepare_v2(*pDb
, zRenamesDone
, -1, &pStmt
, 0)
8128 && SQLITE_ROW
==sqlite3_step(pStmt
) ){
8129 *pzRenamed
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
8134 sqlite3_finalize(pStmt
);
8135 sqlite3_close(*pDb
);
8142 ** Check if the sqlite_schema table contains one or more virtual tables. If
8143 ** parameter zLike is not NULL, then it is an SQL expression that the
8144 ** sqlite_schema row must also match. If one or more such rows are found,
8145 ** print the following warning to the output:
8147 ** WARNING: Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled
8149 static int outputDumpWarning(ShellState
*p
, const char *zLike
){
8151 sqlite3_stmt
*pStmt
= 0;
8152 shellPreparePrintf(p
->db
, &rc
, &pStmt
,
8153 "SELECT 1 FROM sqlite_schema o WHERE "
8154 "sql LIKE 'CREATE VIRTUAL TABLE%%' AND %s", zLike
? zLike
: "true"
8156 if( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
8157 oputz("/* WARNING: "
8158 "Script requires that SQLITE_DBCONFIG_DEFENSIVE be disabled */\n"
8161 shellFinalize(&rc
, pStmt
);
8166 ** Fault-Simulator state and logic.
8169 int iId
; /* ID that triggers a simulated fault. -1 means "any" */
8170 int iErr
; /* The error code to return on a fault */
8171 int iCnt
; /* Trigger the fault only if iCnt is already zero */
8172 int iInterval
; /* Reset iCnt to this value after each fault */
8173 int eVerbose
; /* When to print output */
8174 int nHit
; /* Number of hits seen so far */
8175 int nRepeat
; /* Turn off after this many hits. 0 for never */
8176 int nSkip
; /* Skip this many before first fault */
8177 } faultsim_state
= {-1, 0, 0, 0, 0, 0, 0, 0};
8180 ** This is the fault-sim callback
8182 static int faultsim_callback(int iArg
){
8183 if( faultsim_state
.iId
>0 && faultsim_state
.iId
!=iArg
){
8186 if( faultsim_state
.iCnt
){
8187 if( faultsim_state
.iCnt
>0 ) faultsim_state
.iCnt
--;
8188 if( faultsim_state
.eVerbose
>=2 ){
8189 oputf("FAULT-SIM id=%d no-fault (cnt=%d)\n", iArg
, faultsim_state
.iCnt
);
8193 if( faultsim_state
.eVerbose
>=1 ){
8194 oputf("FAULT-SIM id=%d returns %d\n", iArg
, faultsim_state
.iErr
);
8196 faultsim_state
.iCnt
= faultsim_state
.iInterval
;
8197 faultsim_state
.nHit
++;
8198 if( faultsim_state
.nRepeat
>0 && faultsim_state
.nRepeat
<=faultsim_state
.nHit
){
8199 faultsim_state
.iCnt
= -1;
8201 return faultsim_state
.iErr
;
8205 ** If an input line begins with "." then invoke this routine to
8206 ** process that line.
8208 ** Return 1 on error, 2 to exit, and 0 otherwise.
8210 static int do_meta_command(char *zLine
, ShellState
*p
){
8217 #ifndef SQLITE_OMIT_VIRTUALTABLE
8218 if( p
->expert
.pExpert
){
8219 expertFinish(p
, 1, 0);
8223 /* Parse the input line into tokens.
8225 while( zLine
[h
] && nArg
<ArraySize(azArg
)-1 ){
8226 while( IsSpace(zLine
[h
]) ){ h
++; }
8227 if( zLine
[h
]==0 ) break;
8228 if( zLine
[h
]=='\'' || zLine
[h
]=='"' ){
8229 int delim
= zLine
[h
++];
8230 azArg
[nArg
++] = &zLine
[h
];
8231 while( zLine
[h
] && zLine
[h
]!=delim
){
8232 if( zLine
[h
]=='\\' && delim
=='"' && zLine
[h
+1]!=0 ) h
++;
8235 if( zLine
[h
]==delim
){
8238 if( delim
=='"' ) resolve_backslashes(azArg
[nArg
-1]);
8240 azArg
[nArg
++] = &zLine
[h
];
8241 while( zLine
[h
] && !IsSpace(zLine
[h
]) ){ h
++; }
8242 if( zLine
[h
] ) zLine
[h
++] = 0;
8247 /* Process the input line.
8249 if( nArg
==0 ) return 0; /* no tokens, no error */
8250 n
= strlen30(azArg
[0]);
8254 #ifndef SQLITE_OMIT_AUTHORIZATION
8255 if( c
=='a' && cli_strncmp(azArg
[0], "auth", n
)==0 ){
8257 eputz("Usage: .auth ON|OFF\n");
8259 goto meta_command_exit
;
8262 if( booleanValue(azArg
[1]) ){
8263 sqlite3_set_authorizer(p
->db
, shellAuth
, p
);
8264 }else if( p
->bSafeModePersist
){
8265 sqlite3_set_authorizer(p
->db
, safeModeAuth
, p
);
8267 sqlite3_set_authorizer(p
->db
, 0, 0);
8272 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) \
8273 && !defined(SQLITE_SHELL_FIDDLE)
8274 if( c
=='a' && cli_strncmp(azArg
[0], "archive", n
)==0 ){
8276 failIfSafeMode(p
, "cannot run .archive in safe mode");
8277 rc
= arDotCommand(p
, 0, azArg
, nArg
);
8281 #ifndef SQLITE_SHELL_FIDDLE
8282 if( (c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "backup", n
)==0)
8283 || (c
=='s' && n
>=3 && cli_strncmp(azArg
[0], "save", n
)==0)
8285 const char *zDestFile
= 0;
8286 const char *zDb
= 0;
8288 sqlite3_backup
*pBackup
;
8291 const char *zVfs
= 0;
8292 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
8293 for(j
=1; j
<nArg
; j
++){
8294 const char *z
= azArg
[j
];
8296 if( z
[1]=='-' ) z
++;
8297 if( cli_strcmp(z
, "-append")==0 ){
8300 if( cli_strcmp(z
, "-async")==0 ){
8304 eputf("unknown option: %s\n", azArg
[j
]);
8307 }else if( zDestFile
==0 ){
8308 zDestFile
= azArg
[j
];
8311 zDestFile
= azArg
[j
];
8313 eputz("Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
8318 eputz("missing FILENAME argument on .backup\n");
8321 if( zDb
==0 ) zDb
= "main";
8322 rc
= sqlite3_open_v2(zDestFile
, &pDest
,
8323 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
, zVfs
);
8324 if( rc
!=SQLITE_OK
){
8325 eputf("Error: cannot open \"%s\"\n", zDestFile
);
8330 sqlite3_exec(pDest
, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
8334 pBackup
= sqlite3_backup_init(pDest
, "main", p
->db
, zDb
);
8336 shellDatabaseError(pDest
);
8340 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
){}
8341 sqlite3_backup_finish(pBackup
);
8342 if( rc
==SQLITE_DONE
){
8345 shellDatabaseError(pDest
);
8350 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8352 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "bail", n
)==0 ){
8354 bail_on_error
= booleanValue(azArg
[1]);
8356 eputz("Usage: .bail on|off\n");
8361 /* Undocumented. Legacy only. See "crnl" below */
8362 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "binary", n
)==0 ){
8364 if( booleanValue(azArg
[1]) ){
8365 setBinaryMode(p
->out
, 1);
8367 setTextMode(p
->out
, 1);
8370 eputz("The \".binary\" command is deprecated. Use \".crnl\" instead.\n"
8371 "Usage: .binary on|off\n");
8376 /* The undocumented ".breakpoint" command causes a call to the no-op
8377 ** routine named test_breakpoint().
8379 if( c
=='b' && n
>=3 && cli_strncmp(azArg
[0], "breakpoint", n
)==0 ){
8383 #ifndef SQLITE_SHELL_FIDDLE
8384 if( c
=='c' && cli_strcmp(azArg
[0],"cd")==0 ){
8385 failIfSafeMode(p
, "cannot run .cd in safe mode");
8387 #if defined(_WIN32) || defined(WIN32)
8388 wchar_t *z
= sqlite3_win32_utf8_to_unicode(azArg
[1]);
8389 rc
= !SetCurrentDirectoryW(z
);
8392 rc
= chdir(azArg
[1]);
8395 eputf("Cannot change to directory \"%s\"\n", azArg
[1]);
8399 eputz("Usage: .cd DIRECTORY\n");
8403 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8405 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "changes", n
)==0 ){
8407 setOrClearFlag(p
, SHFLG_CountChanges
, azArg
[1]);
8409 eputz("Usage: .changes on|off\n");
8414 #ifndef SQLITE_SHELL_FIDDLE
8415 /* Cancel output redirection, if it is currently set (by .testcase)
8416 ** Then read the content of the testcase-out.txt file and compare against
8417 ** azArg[1]. If there are differences, report an error and exit.
8419 if( c
=='c' && n
>=3 && cli_strncmp(azArg
[0], "check", n
)==0 ){
8423 eputz("Usage: .check GLOB-PATTERN\n");
8425 }else if( (zRes
= readFile("testcase-out.txt", 0))==0 ){
8427 }else if( testcase_glob(azArg
[1],zRes
)==0 ){
8428 eputf("testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
8429 p
->zTestcase
, azArg
[1], zRes
);
8432 oputf("testcase-%s ok\n", p
->zTestcase
);
8437 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8439 #ifndef SQLITE_SHELL_FIDDLE
8440 if( c
=='c' && cli_strncmp(azArg
[0], "clone", n
)==0 ){
8441 failIfSafeMode(p
, "cannot run .clone in safe mode");
8443 tryToClone(p
, azArg
[1]);
8445 eputz("Usage: .clone FILENAME\n");
8449 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
8451 if( c
=='c' && cli_strncmp(azArg
[0], "connection", n
)==0 ){
8453 /* List available connections */
8455 for(i
=0; i
<ArraySize(p
->aAuxDb
); i
++){
8456 const char *zFile
= p
->aAuxDb
[i
].zDbFilename
;
8457 if( p
->aAuxDb
[i
].db
==0 && p
->pAuxDb
!=&p
->aAuxDb
[i
] ){
8458 zFile
= "(not open)";
8459 }else if( zFile
==0 ){
8461 }else if( zFile
[0]==0 ){
8462 zFile
= "(temporary-file)";
8464 if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
8465 sputf(stdout
, "ACTIVE %d: %s\n", i
, zFile
);
8466 }else if( p
->aAuxDb
[i
].db
!=0 ){
8467 sputf(stdout
, " %d: %s\n", i
, zFile
);
8470 }else if( nArg
==2 && IsDigit(azArg
[1][0]) && azArg
[1][1]==0 ){
8471 int i
= azArg
[1][0] - '0';
8472 if( p
->pAuxDb
!= &p
->aAuxDb
[i
] && i
>=0 && i
<ArraySize(p
->aAuxDb
) ){
8473 p
->pAuxDb
->db
= p
->db
;
8474 p
->pAuxDb
= &p
->aAuxDb
[i
];
8475 globalDb
= p
->db
= p
->pAuxDb
->db
;
8478 }else if( nArg
==3 && cli_strcmp(azArg
[1], "close")==0
8479 && IsDigit(azArg
[2][0]) && azArg
[2][1]==0 ){
8480 int i
= azArg
[2][0] - '0';
8481 if( i
<0 || i
>=ArraySize(p
->aAuxDb
) ){
8483 }else if( p
->pAuxDb
== &p
->aAuxDb
[i
] ){
8484 eputz("cannot close the active database connection\n");
8486 }else if( p
->aAuxDb
[i
].db
){
8487 session_close_all(p
, i
);
8488 close_db(p
->aAuxDb
[i
].db
);
8489 p
->aAuxDb
[i
].db
= 0;
8492 eputz("Usage: .connection [close] [CONNECTION-NUMBER]\n");
8497 if( c
=='c' && n
==4 && cli_strncmp(azArg
[0], "crnl", n
)==0 ){
8499 if( booleanValue(azArg
[1]) ){
8500 setTextMode(p
->out
, 1);
8502 setBinaryMode(p
->out
, 1);
8505 #if !defined(_WIN32) && !defined(WIN32)
8506 eputz("The \".crnl\" is a no-op on non-Windows machines.\n");
8508 eputz("Usage: .crnl on|off\n");
8513 if( c
=='d' && n
>1 && cli_strncmp(azArg
[0], "databases", n
)==0 ){
8516 sqlite3_stmt
*pStmt
;
8519 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
8521 shellDatabaseError(p
->db
);
8524 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
8525 const char *zSchema
= (const char *)sqlite3_column_text(pStmt
,1);
8526 const char *zFile
= (const char*)sqlite3_column_text(pStmt
,2);
8527 if( zSchema
==0 || zFile
==0 ) continue;
8528 azName
= sqlite3_realloc(azName
, (nName
+1)*2*sizeof(char*));
8529 shell_check_oom(azName
);
8530 azName
[nName
*2] = strdup(zSchema
);
8531 azName
[nName
*2+1] = strdup(zFile
);
8535 sqlite3_finalize(pStmt
);
8536 for(i
=0; i
<nName
; i
++){
8537 int eTxn
= sqlite3_txn_state(p
->db
, azName
[i
*2]);
8538 int bRdonly
= sqlite3_db_readonly(p
->db
, azName
[i
*2]);
8539 const char *z
= azName
[i
*2+1];
8540 oputf("%s: %s %s%s\n",
8541 azName
[i
*2], z
&& z
[0] ? z
: "\"\"", bRdonly
? "r/o" : "r/w",
8542 eTxn
==SQLITE_TXN_NONE
? "" :
8543 eTxn
==SQLITE_TXN_READ
? " read-txn" : " write-txn");
8545 free(azName
[i
*2+1]);
8547 sqlite3_free(azName
);
8550 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbconfig", n
)==0 ){
8551 static const struct DbConfigChoices
{
8555 { "defensive", SQLITE_DBCONFIG_DEFENSIVE
},
8556 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL
},
8557 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML
},
8558 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY
},
8559 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG
},
8560 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER
},
8561 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW
},
8562 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
},
8563 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE
},
8564 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT
},
8565 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
},
8566 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
},
8567 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE
},
8568 { "reverse_scanorder", SQLITE_DBCONFIG_REVERSE_SCANORDER
},
8569 { "stmt_scanstatus", SQLITE_DBCONFIG_STMT_SCANSTATUS
},
8570 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP
},
8571 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA
},
8572 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA
},
8576 for(ii
=0; ii
<ArraySize(aDbConfig
); ii
++){
8577 if( nArg
>1 && cli_strcmp(azArg
[1], aDbConfig
[ii
].zName
)!=0 ) continue;
8579 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, booleanValue(azArg
[2]), 0);
8581 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, -1, &v
);
8582 oputf("%19s %s\n", aDbConfig
[ii
].zName
, v
? "on" : "off");
8585 if( nArg
>1 && ii
==ArraySize(aDbConfig
) ){
8586 eputf("Error: unknown dbconfig \"%s\"\n", azArg
[1]);
8587 eputz("Enter \".dbconfig\" with no arguments for a list\n");
8591 #if SQLITE_SHELL_HAVE_RECOVER
8592 if( c
=='d' && n
>=3 && cli_strncmp(azArg
[0], "dbinfo", n
)==0 ){
8593 rc
= shell_dbinfo_command(p
, nArg
, azArg
);
8596 if( c
=='r' && cli_strncmp(azArg
[0], "recover", n
)==0 ){
8598 rc
= recoverDatabaseCmd(p
, nArg
, azArg
);
8600 #endif /* SQLITE_SHELL_HAVE_RECOVER */
8602 if( c
=='d' && cli_strncmp(azArg
[0], "dump", n
)==0 ){
8606 int savedShowHeader
= p
->showHeader
;
8607 int savedShellFlags
= p
->shellFlgs
;
8609 SHFLG_PreserveRowid
|SHFLG_Newlines
|SHFLG_Echo
8610 |SHFLG_DumpDataOnly
|SHFLG_DumpNoSys
);
8611 for(i
=1; i
<nArg
; i
++){
8612 if( azArg
[i
][0]=='-' ){
8613 const char *z
= azArg
[i
]+1;
8614 if( z
[0]=='-' ) z
++;
8615 if( cli_strcmp(z
,"preserve-rowids")==0 ){
8616 #ifdef SQLITE_OMIT_VIRTUALTABLE
8617 eputz("The --preserve-rowids option is not compatible"
8618 " with SQLITE_OMIT_VIRTUALTABLE\n");
8620 sqlite3_free(zLike
);
8621 goto meta_command_exit
;
8623 ShellSetFlag(p
, SHFLG_PreserveRowid
);
8626 if( cli_strcmp(z
,"newlines")==0 ){
8627 ShellSetFlag(p
, SHFLG_Newlines
);
8629 if( cli_strcmp(z
,"data-only")==0 ){
8630 ShellSetFlag(p
, SHFLG_DumpDataOnly
);
8632 if( cli_strcmp(z
,"nosys")==0 ){
8633 ShellSetFlag(p
, SHFLG_DumpNoSys
);
8636 eputf("Unknown option \"%s\" on \".dump\"\n", azArg
[i
]);
8638 sqlite3_free(zLike
);
8639 goto meta_command_exit
;
8642 /* azArg[i] contains a LIKE pattern. This ".dump" request should
8643 ** only dump data for tables for which either the table name matches
8644 ** the LIKE pattern, or the table appears to be a shadow table of
8645 ** a virtual table for which the name matches the LIKE pattern.
8647 char *zExpr
= sqlite3_mprintf(
8648 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
8649 " SELECT 1 FROM sqlite_schema WHERE "
8650 " name LIKE %Q ESCAPE '\\' AND"
8651 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
8652 " substr(o.name, 1, length(name)+1) == (name||'_')"
8653 ")", azArg
[i
], azArg
[i
]
8657 zLike
= sqlite3_mprintf("%z OR %z", zLike
, zExpr
);
8666 outputDumpWarning(p
, zLike
);
8667 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8668 /* When playing back a "dump", the content might appear in an order
8669 ** which causes immediate foreign key constraints to be violated.
8670 ** So disable foreign-key constraint enforcement to prevent problems. */
8671 oputz("PRAGMA foreign_keys=OFF;\n");
8672 oputz("BEGIN TRANSACTION;\n");
8674 p
->writableSchema
= 0;
8676 /* Set writable_schema=ON since doing so forces SQLite to initialize
8677 ** as much of the schema as it can even if the sqlite_schema table is
8679 sqlite3_exec(p
->db
, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
8681 if( zLike
==0 ) zLike
= sqlite3_mprintf("true");
8682 zSql
= sqlite3_mprintf(
8683 "SELECT name, type, sql FROM sqlite_schema AS o "
8684 "WHERE (%s) AND type=='table'"
8686 " ORDER BY tbl_name='sqlite_sequence', rowid",
8689 run_schema_dump_query(p
,zSql
);
8691 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8692 zSql
= sqlite3_mprintf(
8693 "SELECT sql FROM sqlite_schema AS o "
8694 "WHERE (%s) AND sql NOT NULL"
8695 " AND type IN ('index','trigger','view') "
8696 "ORDER BY type COLLATE NOCASE DESC",
8699 run_table_dump_query(p
, zSql
);
8702 sqlite3_free(zLike
);
8703 if( p
->writableSchema
){
8704 oputz("PRAGMA writable_schema=OFF;\n");
8705 p
->writableSchema
= 0;
8707 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
8708 sqlite3_exec(p
->db
, "RELEASE dump;", 0, 0, 0);
8709 if( (p
->shellFlgs
& SHFLG_DumpDataOnly
)==0 ){
8710 oputz(p
->nErr
?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
8712 p
->showHeader
= savedShowHeader
;
8713 p
->shellFlgs
= savedShellFlags
;
8716 if( c
=='e' && cli_strncmp(azArg
[0], "echo", n
)==0 ){
8718 setOrClearFlag(p
, SHFLG_Echo
, azArg
[1]);
8720 eputz("Usage: .echo on|off\n");
8725 if( c
=='e' && cli_strncmp(azArg
[0], "eqp", n
)==0 ){
8728 if( p
->autoEQPtrace
){
8729 if( p
->db
) sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
8730 p
->autoEQPtrace
= 0;
8732 if( cli_strcmp(azArg
[1],"full")==0 ){
8733 p
->autoEQP
= AUTOEQP_full
;
8734 }else if( cli_strcmp(azArg
[1],"trigger")==0 ){
8735 p
->autoEQP
= AUTOEQP_trigger
;
8737 }else if( cli_strcmp(azArg
[1],"test")==0 ){
8738 p
->autoEQP
= AUTOEQP_on
;
8740 }else if( cli_strcmp(azArg
[1],"trace")==0 ){
8741 p
->autoEQP
= AUTOEQP_full
;
8742 p
->autoEQPtrace
= 1;
8744 sqlite3_exec(p
->db
, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
8745 sqlite3_exec(p
->db
, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
8748 p
->autoEQP
= (u8
)booleanValue(azArg
[1]);
8751 eputz("Usage: .eqp off|on|trace|trigger|full\n");
8756 #ifndef SQLITE_SHELL_FIDDLE
8757 if( c
=='e' && cli_strncmp(azArg
[0], "exit", n
)==0 ){
8758 if( nArg
>1 && (rc
= (int)integerValue(azArg
[1]))!=0 ) exit(rc
);
8763 /* The ".explain" command is automatic now. It is largely pointless. It
8764 ** retained purely for backwards compatibility */
8765 if( c
=='e' && cli_strncmp(azArg
[0], "explain", n
)==0 ){
8768 if( cli_strcmp(azArg
[1],"auto")==0 ){
8771 val
= booleanValue(azArg
[1]);
8774 if( val
==1 && p
->mode
!=MODE_Explain
){
8775 p
->normalMode
= p
->mode
;
8776 p
->mode
= MODE_Explain
;
8779 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
8781 }else if( val
==99 ){
8782 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
8787 #ifndef SQLITE_OMIT_VIRTUALTABLE
8788 if( c
=='e' && cli_strncmp(azArg
[0], "expert", n
)==0 ){
8790 eputf("Cannot run experimental commands such as \"%s\" in safe mode\n",
8795 expertDotCommand(p
, azArg
, nArg
);
8800 if( c
=='f' && cli_strncmp(azArg
[0], "filectrl", n
)==0 ){
8801 static const struct {
8802 const char *zCtrlName
; /* Name of a test-control option */
8803 int ctrlCode
; /* Integer code for that option */
8804 const char *zUsage
; /* Usage notes */
8806 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE
, "SIZE" },
8807 { "data_version", SQLITE_FCNTL_DATA_VERSION
, "" },
8808 { "has_moved", SQLITE_FCNTL_HAS_MOVED
, "" },
8809 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT
, "MILLISEC" },
8810 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL
, "[BOOLEAN]" },
8811 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
8812 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE
, "[BOOLEAN]" },
8813 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES
, "[N]" },
8814 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT
, "[LIMIT]" },
8815 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME
, "" },
8816 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
8820 sqlite3_int64 iRes
= 0; /* Integer result to display if rc2==1 */
8821 int isOk
= 0; /* 0: usage 1: %lld 2: no-result */
8823 const char *zCmd
= 0;
8824 const char *zSchema
= 0;
8827 zCmd
= nArg
>=2 ? azArg
[1] : "help";
8830 && (cli_strcmp(zCmd
,"--schema")==0 || cli_strcmp(zCmd
,"-schema")==0)
8834 for(i
=3; i
<nArg
; i
++) azArg
[i
-2] = azArg
[i
];
8839 /* The argument can optionally begin with "-" or "--" */
8840 if( zCmd
[0]=='-' && zCmd
[1] ){
8842 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
8845 /* --help lists all file-controls */
8846 if( cli_strcmp(zCmd
,"help")==0 ){
8847 oputz("Available file-controls:\n");
8848 for(i
=0; i
<ArraySize(aCtrl
); i
++){
8849 oputf(" .filectrl %s %s\n", aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
8852 goto meta_command_exit
;
8855 /* convert filectrl text option to value. allow any unique prefix
8856 ** of the option name, or a numerical value. */
8857 n2
= strlen30(zCmd
);
8858 for(i
=0; i
<ArraySize(aCtrl
); i
++){
8859 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
8861 filectrl
= aCtrl
[i
].ctrlCode
;
8864 eputf("Error: ambiguous file-control: \"%s\"\n"
8865 "Use \".filectrl --help\" for help\n", zCmd
);
8867 goto meta_command_exit
;
8872 eputf("Error: unknown file-control: %s\n"
8873 "Use \".filectrl --help\" for help\n", zCmd
);
8876 case SQLITE_FCNTL_SIZE_LIMIT
: {
8877 if( nArg
!=2 && nArg
!=3 ) break;
8878 iRes
= nArg
==3 ? integerValue(azArg
[2]) : -1;
8879 sqlite3_file_control(p
->db
, zSchema
, SQLITE_FCNTL_SIZE_LIMIT
, &iRes
);
8883 case SQLITE_FCNTL_LOCK_TIMEOUT
:
8884 case SQLITE_FCNTL_CHUNK_SIZE
: {
8886 if( nArg
!=3 ) break;
8887 x
= (int)integerValue(azArg
[2]);
8888 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8892 case SQLITE_FCNTL_PERSIST_WAL
:
8893 case SQLITE_FCNTL_POWERSAFE_OVERWRITE
: {
8895 if( nArg
!=2 && nArg
!=3 ) break;
8896 x
= nArg
==3 ? booleanValue(azArg
[2]) : -1;
8897 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8902 case SQLITE_FCNTL_DATA_VERSION
:
8903 case SQLITE_FCNTL_HAS_MOVED
: {
8905 if( nArg
!=2 ) break;
8906 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8911 case SQLITE_FCNTL_TEMPFILENAME
: {
8913 if( nArg
!=2 ) break;
8914 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &z
);
8922 case SQLITE_FCNTL_RESERVE_BYTES
: {
8926 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8929 sqlite3_file_control(p
->db
, zSchema
, filectrl
, &x
);
8936 if( isOk
==0 && iCtrl
>=0 ){
8937 oputf("Usage: .filectrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
8939 }else if( isOk
==1 ){
8941 sqlite3_snprintf(sizeof(zBuf
), zBuf
, "%lld", iRes
);
8942 oputf("%s\n", zBuf
);
8946 if( c
=='f' && cli_strncmp(azArg
[0], "fullschema", n
)==0 ){
8949 memcpy(&data
, p
, sizeof(data
));
8950 data
.showHeader
= 0;
8951 data
.cMode
= data
.mode
= MODE_Semi
;
8952 if( nArg
==2 && optionMatch(azArg
[1], "indent") ){
8953 data
.cMode
= data
.mode
= MODE_Pretty
;
8957 eputz("Usage: .fullschema ?--indent?\n");
8959 goto meta_command_exit
;
8962 rc
= sqlite3_exec(p
->db
,
8964 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
8965 " FROM sqlite_schema UNION ALL"
8966 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
8967 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
8971 if( rc
==SQLITE_OK
){
8972 sqlite3_stmt
*pStmt
;
8973 rc
= sqlite3_prepare_v2(p
->db
,
8974 "SELECT rowid FROM sqlite_schema"
8975 " WHERE name GLOB 'sqlite_stat[134]'",
8977 if( rc
==SQLITE_OK
){
8978 doStats
= sqlite3_step(pStmt
)==SQLITE_ROW
;
8979 sqlite3_finalize(pStmt
);
8983 oputz("/* No STAT tables available */\n");
8985 oputz("ANALYZE sqlite_schema;\n");
8986 data
.cMode
= data
.mode
= MODE_Insert
;
8987 data
.zDestTable
= "sqlite_stat1";
8988 shell_exec(&data
, "SELECT * FROM sqlite_stat1", 0);
8989 data
.zDestTable
= "sqlite_stat4";
8990 shell_exec(&data
, "SELECT * FROM sqlite_stat4", 0);
8991 oputz("ANALYZE sqlite_schema;\n");
8995 if( c
=='h' && cli_strncmp(azArg
[0], "headers", n
)==0 ){
8997 p
->showHeader
= booleanValue(azArg
[1]);
8998 p
->shellFlgs
|= SHFLG_HeaderSet
;
9000 eputz("Usage: .headers on|off\n");
9005 if( c
=='h' && cli_strncmp(azArg
[0], "help", n
)==0 ){
9007 n
= showHelp(p
->out
, azArg
[1]);
9009 oputf("Nothing matches '%s'\n", azArg
[1]);
9012 showHelp(p
->out
, 0);
9016 #ifndef SQLITE_SHELL_FIDDLE
9017 if( c
=='i' && cli_strncmp(azArg
[0], "import", n
)==0 ){
9018 char *zTable
= 0; /* Insert data into this table */
9019 char *zSchema
= 0; /* Schema of zTable */
9020 char *zFile
= 0; /* Name of file to extra content from */
9021 sqlite3_stmt
*pStmt
= NULL
; /* A statement */
9022 int nCol
; /* Number of columns in the table */
9023 i64 nByte
; /* Number of bytes in an SQL string */
9024 int i
, j
; /* Loop counters */
9025 int needCommit
; /* True to COMMIT or ROLLBACK at end */
9026 int nSep
; /* Number of bytes in p->colSeparator[] */
9027 char *zSql
= 0; /* An SQL statement */
9028 ImportCtx sCtx
; /* Reader context */
9029 char *(SQLITE_CDECL
*xRead
)(ImportCtx
*); /* Func to read one value */
9030 int eVerbose
= 0; /* Larger for more console output */
9031 int nSkip
= 0; /* Initial lines to skip */
9032 int useOutputMode
= 1; /* Use output mode to determine separators */
9033 char *zCreate
= 0; /* CREATE TABLE statement text */
9035 failIfSafeMode(p
, "cannot run .import in safe mode");
9036 memset(&sCtx
, 0, sizeof(sCtx
));
9037 if( p
->mode
==MODE_Ascii
){
9038 xRead
= ascii_read_one_field
;
9040 xRead
= csv_read_one_field
;
9043 for(i
=1; i
<nArg
; i
++){
9045 if( z
[0]=='-' && z
[1]=='-' ) z
++;
9049 }else if( zTable
==0 ){
9052 oputf("ERROR: extra argument: \"%s\". Usage:\n", z
);
9053 showHelp(p
->out
, "import");
9054 goto meta_command_exit
;
9056 }else if( cli_strcmp(z
,"-v")==0 ){
9058 }else if( cli_strcmp(z
,"-schema")==0 && i
<nArg
-1 ){
9059 zSchema
= azArg
[++i
];
9060 }else if( cli_strcmp(z
,"-skip")==0 && i
<nArg
-1 ){
9061 nSkip
= integerValue(azArg
[++i
]);
9062 }else if( cli_strcmp(z
,"-ascii")==0 ){
9063 sCtx
.cColSep
= SEP_Unit
[0];
9064 sCtx
.cRowSep
= SEP_Record
[0];
9065 xRead
= ascii_read_one_field
;
9067 }else if( cli_strcmp(z
,"-csv")==0 ){
9069 sCtx
.cRowSep
= '\n';
9070 xRead
= csv_read_one_field
;
9073 oputf("ERROR: unknown option: \"%s\". Usage:\n", z
);
9074 showHelp(p
->out
, "import");
9075 goto meta_command_exit
;
9079 oputf("ERROR: missing %s argument. Usage:\n",
9080 zFile
==0 ? "FILE" : "TABLE");
9081 showHelp(p
->out
, "import");
9082 goto meta_command_exit
;
9086 if( useOutputMode
){
9087 /* If neither the --csv or --ascii options are specified, then set
9088 ** the column and row separator characters from the output mode. */
9089 nSep
= strlen30(p
->colSeparator
);
9091 eputz("Error: non-null column separator required for import\n");
9092 goto meta_command_exit
;
9095 eputz("Error: multi-character column separators not allowed"
9097 goto meta_command_exit
;
9099 nSep
= strlen30(p
->rowSeparator
);
9101 eputz("Error: non-null row separator required for import\n");
9102 goto meta_command_exit
;
9104 if( nSep
==2 && p
->mode
==MODE_Csv
9105 && cli_strcmp(p
->rowSeparator
,SEP_CrLf
)==0
9107 /* When importing CSV (only), if the row separator is set to the
9108 ** default output row separator, change it to the default input
9109 ** row separator. This avoids having to maintain different input
9110 ** and output row separators. */
9111 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9112 nSep
= strlen30(p
->rowSeparator
);
9115 eputz("Error: multi-character row separators not allowed"
9117 goto meta_command_exit
;
9119 sCtx
.cColSep
= (u8
)p
->colSeparator
[0];
9120 sCtx
.cRowSep
= (u8
)p
->rowSeparator
[0];
9124 if( sCtx
.zFile
[0]=='|' ){
9125 #ifdef SQLITE_OMIT_POPEN
9126 eputz("Error: pipes are not supported in this OS\n");
9127 goto meta_command_exit
;
9129 sCtx
.in
= popen(sCtx
.zFile
+1, "r");
9130 sCtx
.zFile
= "<pipe>";
9131 sCtx
.xCloser
= pclose
;
9134 sCtx
.in
= fopen(sCtx
.zFile
, "rb");
9135 sCtx
.xCloser
= fclose
;
9138 eputf("Error: cannot open \"%s\"\n", zFile
);
9139 goto meta_command_exit
;
9141 if( eVerbose
>=2 || (eVerbose
>=1 && useOutputMode
) ){
9144 zSep
[0] = sCtx
.cColSep
;
9145 oputz("Column separator ");
9146 output_c_string(zSep
);
9147 oputz(", row separator ");
9148 zSep
[0] = sCtx
.cRowSep
;
9149 output_c_string(zSep
);
9152 sCtx
.z
= sqlite3_malloc64(120);
9154 import_cleanup(&sCtx
);
9155 shell_out_of_memory();
9157 /* Below, resources must be freed before exit. */
9158 while( (nSkip
--)>0 ){
9159 while( xRead(&sCtx
) && sCtx
.cTerm
==sCtx
.cColSep
){}
9161 import_append_char(&sCtx
, 0); /* To ensure sCtx.z is allocated */
9162 if( sqlite3_table_column_metadata(p
->db
, zSchema
, zTable
,0,0,0,0,0,0) ){
9163 /* Table does not exist. Create it. */
9164 sqlite3
*dbCols
= 0;
9167 zCreate
= sqlite3_mprintf("CREATE TABLE \"%w\".\"%w\"",
9168 zSchema
? zSchema
: "main", zTable
);
9169 while( xRead(&sCtx
) ){
9170 zAutoColumn(sCtx
.z
, &dbCols
, 0);
9171 if( sCtx
.cTerm
!=sCtx
.cColSep
) break;
9173 zColDefs
= zAutoColumn(0, &dbCols
, &zRenames
);
9175 sputf((stdin_is_interactive
&& p
->in
==stdin
)? p
->out
: stderr
,
9176 "Columns renamed during .import %s due to duplicates:\n"
9177 "%s\n", sCtx
.zFile
, zRenames
);
9178 sqlite3_free(zRenames
);
9182 eputf("%s: empty file\n", sCtx
.zFile
);
9183 import_cleanup(&sCtx
);
9185 sqlite3_free(zCreate
);
9186 goto meta_command_exit
;
9188 zCreate
= sqlite3_mprintf("%z%z\n", zCreate
, zColDefs
);
9190 import_cleanup(&sCtx
);
9191 shell_out_of_memory();
9194 oputf("%s\n", zCreate
);
9196 rc
= sqlite3_exec(p
->db
, zCreate
, 0, 0, 0);
9197 sqlite3_free(zCreate
);
9200 eputf("%s failed:\n%s\n", zCreate
, sqlite3_errmsg(p
->db
));
9201 import_cleanup(&sCtx
);
9203 goto meta_command_exit
;
9206 zSql
= sqlite3_mprintf("SELECT count(*) FROM pragma_table_info(%Q,%Q);",
9209 import_cleanup(&sCtx
);
9210 shell_out_of_memory();
9212 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9216 if (pStmt
) sqlite3_finalize(pStmt
);
9217 shellDatabaseError(p
->db
);
9218 import_cleanup(&sCtx
);
9220 goto meta_command_exit
;
9222 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
9223 nCol
= sqlite3_column_int(pStmt
, 0);
9227 sqlite3_finalize(pStmt
);
9229 if( nCol
==0 ) return 0; /* no columns, no error */
9231 nByte
= 64 /* space for "INSERT INTO", "VALUES(", ")\0" */
9232 + (zSchema
? strlen(zSchema
)*2 + 2: 0) /* Quoted schema name */
9233 + strlen(zTable
)*2 + 2 /* Quoted table name */
9234 + nCol
*2; /* Space for ",?" for each column */
9235 zSql
= sqlite3_malloc64( nByte
);
9237 import_cleanup(&sCtx
);
9238 shell_out_of_memory();
9241 sqlite3_snprintf(nByte
, zSql
, "INSERT INTO \"%w\".\"%w\" VALUES(?",
9244 sqlite3_snprintf(nByte
, zSql
, "INSERT INTO \"%w\" VALUES(?", zTable
);
9247 for(i
=1; i
<nCol
; i
++){
9255 oputf("Insert using: %s\n", zSql
);
9257 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9261 shellDatabaseError(p
->db
);
9262 if (pStmt
) sqlite3_finalize(pStmt
);
9263 import_cleanup(&sCtx
);
9265 goto meta_command_exit
;
9267 needCommit
= sqlite3_get_autocommit(p
->db
);
9268 if( needCommit
) sqlite3_exec(p
->db
, "BEGIN", 0, 0, 0);
9270 int startLine
= sCtx
.nLine
;
9271 for(i
=0; i
<nCol
; i
++){
9272 char *z
= xRead(&sCtx
);
9274 ** Did we reach end-of-file before finding any columns?
9275 ** If so, stop instead of NULL filling the remaining columns.
9277 if( z
==0 && i
==0 ) break;
9279 ** Did we reach end-of-file OR end-of-line before finding any
9280 ** columns in ASCII mode? If so, stop instead of NULL filling
9281 ** the remaining columns.
9283 if( p
->mode
==MODE_Ascii
&& (z
==0 || z
[0]==0) && i
==0 ) break;
9285 ** For CSV mode, per RFC 4180, accept EOF in lieu of final
9286 ** record terminator but only for last field of multi-field row.
9287 ** (If there are too few fields, it's not valid CSV anyway.)
9289 if( z
==0 && (xRead
==csv_read_one_field
) && i
==nCol
-1 && i
>0 ){
9292 sqlite3_bind_text(pStmt
, i
+1, z
, -1, SQLITE_TRANSIENT
);
9293 if( i
<nCol
-1 && sCtx
.cTerm
!=sCtx
.cColSep
){
9294 eputf("%s:%d: expected %d columns but found %d"
9295 " - filling the rest with NULL\n",
9296 sCtx
.zFile
, startLine
, nCol
, i
+1);
9298 while( i
<=nCol
){ sqlite3_bind_null(pStmt
, i
); i
++; }
9301 if( sCtx
.cTerm
==sCtx
.cColSep
){
9305 }while( sCtx
.cTerm
==sCtx
.cColSep
);
9306 eputf("%s:%d: expected %d columns but found %d - extras ignored\n",
9307 sCtx
.zFile
, startLine
, nCol
, i
);
9310 sqlite3_step(pStmt
);
9311 rc
= sqlite3_reset(pStmt
);
9312 if( rc
!=SQLITE_OK
){
9313 eputf("%s:%d: INSERT failed: %s\n",
9314 sCtx
.zFile
, startLine
, sqlite3_errmsg(p
->db
));
9320 }while( sCtx
.cTerm
!=EOF
);
9322 import_cleanup(&sCtx
);
9323 sqlite3_finalize(pStmt
);
9324 if( needCommit
) sqlite3_exec(p
->db
, "COMMIT", 0, 0, 0);
9326 oputf("Added %d rows with %d errors using %d lines of input\n",
9327 sCtx
.nRow
, sCtx
.nErr
, sCtx
.nLine
-1);
9330 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9332 #ifndef SQLITE_UNTESTABLE
9333 if( c
=='i' && cli_strncmp(azArg
[0], "imposter", n
)==0 ){
9336 sqlite3_stmt
*pStmt
;
9338 int isWO
= 0; /* True if making an imposter of a WITHOUT ROWID table */
9339 int lenPK
= 0; /* Length of the PRIMARY KEY string for isWO tables */
9341 if( !ShellHasFlag(p
,SHFLG_TestingMode
) ){
9342 eputf(".%s unavailable without --unsafe-testing\n",
9345 goto meta_command_exit
;
9347 if( !(nArg
==3 || (nArg
==2 && sqlite3_stricmp(azArg
[1],"off")==0)) ){
9348 eputz("Usage: .imposter INDEX IMPOSTER\n"
9349 " .imposter off\n");
9350 /* Also allowed, but not documented:
9352 ** .imposter TABLE IMPOSTER
9354 ** where TABLE is a WITHOUT ROWID table. In that case, the
9355 ** imposter is another WITHOUT ROWID table with the columns in
9356 ** storage order. */
9358 goto meta_command_exit
;
9362 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 1);
9363 goto meta_command_exit
;
9365 zSql
= sqlite3_mprintf(
9366 "SELECT rootpage, 0 FROM sqlite_schema"
9367 " WHERE name='%q' AND type='index'"
9369 "SELECT rootpage, 1 FROM sqlite_schema"
9370 " WHERE name='%q' AND type='table'"
9371 " AND sql LIKE '%%without%%rowid%%'",
9374 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9376 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
9377 tnum
= sqlite3_column_int(pStmt
, 0);
9378 isWO
= sqlite3_column_int(pStmt
, 1);
9380 sqlite3_finalize(pStmt
);
9381 zSql
= sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg
[1]);
9382 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
9385 while( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9387 const char *zCol
= (const char*)sqlite3_column_text(pStmt
,2);
9390 if( sqlite3_column_int(pStmt
,1)==-1 ){
9393 sqlite3_snprintf(sizeof(zLabel
),zLabel
,"expr%d",i
);
9397 if( isWO
&& lenPK
==0 && sqlite3_column_int(pStmt
,5)==0 && zCollist
){
9398 lenPK
= (int)strlen(zCollist
);
9401 zCollist
= sqlite3_mprintf("\"%w\"", zCol
);
9403 zCollist
= sqlite3_mprintf("%z,\"%w\"", zCollist
, zCol
);
9406 sqlite3_finalize(pStmt
);
9407 if( i
==0 || tnum
==0 ){
9408 eputf("no such index: \"%s\"\n", azArg
[1]);
9410 sqlite3_free(zCollist
);
9411 goto meta_command_exit
;
9413 if( lenPK
==0 ) lenPK
= 100000;
9414 zSql
= sqlite3_mprintf(
9415 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
9416 azArg
[2], zCollist
, lenPK
, zCollist
);
9417 sqlite3_free(zCollist
);
9418 rc
= sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 1, tnum
);
9419 if( rc
==SQLITE_OK
){
9420 rc
= sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
9421 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 0);
9423 eputf("Error in [%s]: %s\n", zSql
, sqlite3_errmsg(p
->db
));
9425 sputf(stdout
, "%s;\n", zSql
);
9426 sputf(stdout
, "WARNING: writing to an imposter table will corrupt"
9427 " the \"%s\" %s!\n", azArg
[1], isWO
? "table" : "index");
9430 eputf("SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc
);
9435 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
9437 if( c
=='i' && cli_strncmp(azArg
[0], "intck", n
)==0 ){
9440 iArg
= integerValue(azArg
[1]);
9441 if( iArg
==0 ) iArg
= -1;
9443 if( (nArg
!=1 && nArg
!=2) || iArg
<0 ){
9444 eputf("%s","Usage: .intck STEPS_PER_UNLOCK\n");
9446 goto meta_command_exit
;
9449 rc
= intckDatabaseCmd(p
, iArg
);
9452 #ifdef SQLITE_ENABLE_IOTRACE
9453 if( c
=='i' && cli_strncmp(azArg
[0], "iotrace", n
)==0 ){
9454 SQLITE_API
extern void (SQLITE_CDECL
*sqlite3IoTrace
)(const char*, ...);
9455 if( iotrace
&& iotrace
!=stdout
) fclose(iotrace
);
9459 }else if( cli_strcmp(azArg
[1], "-")==0 ){
9460 sqlite3IoTrace
= iotracePrintf
;
9463 iotrace
= fopen(azArg
[1], "w");
9465 eputf("Error: cannot open \"%s\"\n", azArg
[1]);
9469 sqlite3IoTrace
= iotracePrintf
;
9475 if( c
=='l' && n
>=5 && cli_strncmp(azArg
[0], "limits", n
)==0 ){
9476 static const struct {
9477 const char *zLimitName
; /* Name of a limit */
9478 int limitCode
; /* Integer code for that limit */
9480 { "length", SQLITE_LIMIT_LENGTH
},
9481 { "sql_length", SQLITE_LIMIT_SQL_LENGTH
},
9482 { "column", SQLITE_LIMIT_COLUMN
},
9483 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH
},
9484 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT
},
9485 { "vdbe_op", SQLITE_LIMIT_VDBE_OP
},
9486 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG
},
9487 { "attached", SQLITE_LIMIT_ATTACHED
},
9488 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH
},
9489 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER
},
9490 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH
},
9491 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS
},
9496 for(i
=0; i
<ArraySize(aLimit
); i
++){
9497 sputf(stdout
, "%20s %d\n", aLimit
[i
].zLimitName
,
9498 sqlite3_limit(p
->db
, aLimit
[i
].limitCode
, -1));
9501 eputz("Usage: .limit NAME ?NEW-VALUE?\n");
9503 goto meta_command_exit
;
9506 n2
= strlen30(azArg
[1]);
9507 for(i
=0; i
<ArraySize(aLimit
); i
++){
9508 if( sqlite3_strnicmp(aLimit
[i
].zLimitName
, azArg
[1], n2
)==0 ){
9512 eputf("ambiguous limit: \"%s\"\n", azArg
[1]);
9514 goto meta_command_exit
;
9519 eputf("unknown limit: \"%s\"\n"
9520 "enter \".limits\" with no arguments for a list.\n",
9523 goto meta_command_exit
;
9526 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
,
9527 (int)integerValue(azArg
[2]));
9529 sputf(stdout
, "%20s %d\n", aLimit
[iLimit
].zLimitName
,
9530 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
, -1));
9534 if( c
=='l' && n
>2 && cli_strncmp(azArg
[0], "lint", n
)==0 ){
9536 lintDotCommand(p
, azArg
, nArg
);
9539 #if !defined(SQLITE_OMIT_LOAD_EXTENSION) && !defined(SQLITE_SHELL_FIDDLE)
9540 if( c
=='l' && cli_strncmp(azArg
[0], "load", n
)==0 ){
9541 const char *zFile
, *zProc
;
9543 failIfSafeMode(p
, "cannot run .load in safe mode");
9544 if( nArg
<2 || azArg
[1][0]==0 ){
9545 /* Must have a non-empty FILE. (Will not load self.) */
9546 eputz("Usage: .load FILE ?ENTRYPOINT?\n");
9548 goto meta_command_exit
;
9551 zProc
= nArg
>=3 ? azArg
[2] : 0;
9553 rc
= sqlite3_load_extension(p
->db
, zFile
, zProc
, &zErrMsg
);
9554 if( rc
!=SQLITE_OK
){
9555 shellEmitError(zErrMsg
);
9556 sqlite3_free(zErrMsg
);
9562 if( c
=='l' && cli_strncmp(azArg
[0], "log", n
)==0 ){
9564 eputz("Usage: .log FILENAME\n");
9567 const char *zFile
= azArg
[1];
9569 && cli_strcmp(zFile
,"on")!=0
9570 && cli_strcmp(zFile
,"off")!=0
9572 sputz(stdout
, "cannot set .log to anything other"
9573 " than \"on\" or \"off\"\n");
9576 output_file_close(p
->pLog
);
9577 if( cli_strcmp(zFile
,"on")==0 ) zFile
= "stdout";
9578 p
->pLog
= output_file_open(zFile
, 0);
9582 if( c
=='m' && cli_strncmp(azArg
[0], "mode", n
)==0 ){
9583 const char *zMode
= 0;
9584 const char *zTabname
= 0;
9586 ColModeOpts cmOpts
= ColModeOpts_default
;
9587 for(i
=1; i
<nArg
; i
++){
9588 const char *z
= azArg
[i
];
9589 if( optionMatch(z
,"wrap") && i
+1<nArg
){
9590 cmOpts
.iWrap
= integerValue(azArg
[++i
]);
9591 }else if( optionMatch(z
,"ww") ){
9592 cmOpts
.bWordWrap
= 1;
9593 }else if( optionMatch(z
,"wordwrap") && i
+1<nArg
){
9594 cmOpts
.bWordWrap
= (u8
)booleanValue(azArg
[++i
]);
9595 }else if( optionMatch(z
,"quote") ){
9597 }else if( optionMatch(z
,"noquote") ){
9599 }else if( zMode
==0 ){
9601 /* Apply defaults for qbox pseudo-mode. If that
9602 * overwrites already-set values, user was informed of this.
9604 if( cli_strcmp(z
, "qbox")==0 ){
9605 ColModeOpts cmo
= ColModeOpts_default_qbox
;
9609 }else if( zTabname
==0 ){
9611 }else if( z
[0]=='-' ){
9612 eputf("unknown option: %s\n", z
);
9616 " --wordwrap on/off\n"
9620 goto meta_command_exit
;
9622 eputf("extra argument: \"%s\"\n", z
);
9624 goto meta_command_exit
;
9628 if( p
->mode
==MODE_Column
9629 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
9631 oputf("current output mode: %s --wrap %d --wordwrap %s --%squote\n",
9632 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
9633 p
->cmOpts
.bWordWrap
? "on" : "off",
9634 p
->cmOpts
.bQuote
? "" : "no");
9636 oputf("current output mode: %s\n", modeDescr
[p
->mode
]);
9638 zMode
= modeDescr
[p
->mode
];
9640 n2
= strlen30(zMode
);
9641 if( cli_strncmp(zMode
,"lines",n2
)==0 ){
9642 p
->mode
= MODE_Line
;
9643 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9644 }else if( cli_strncmp(zMode
,"columns",n2
)==0 ){
9645 p
->mode
= MODE_Column
;
9646 if( (p
->shellFlgs
& SHFLG_HeaderSet
)==0 ){
9649 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9651 }else if( cli_strncmp(zMode
,"list",n2
)==0 ){
9652 p
->mode
= MODE_List
;
9653 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Column
);
9654 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9655 }else if( cli_strncmp(zMode
,"html",n2
)==0 ){
9656 p
->mode
= MODE_Html
;
9657 }else if( cli_strncmp(zMode
,"tcl",n2
)==0 ){
9659 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Space
);
9660 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9661 }else if( cli_strncmp(zMode
,"csv",n2
)==0 ){
9663 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9664 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
9665 }else if( cli_strncmp(zMode
,"tabs",n2
)==0 ){
9666 p
->mode
= MODE_List
;
9667 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Tab
);
9668 }else if( cli_strncmp(zMode
,"insert",n2
)==0 ){
9669 p
->mode
= MODE_Insert
;
9670 set_table_name(p
, zTabname
? zTabname
: "table");
9671 }else if( cli_strncmp(zMode
,"quote",n2
)==0 ){
9672 p
->mode
= MODE_Quote
;
9673 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9674 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
9675 }else if( cli_strncmp(zMode
,"ascii",n2
)==0 ){
9676 p
->mode
= MODE_Ascii
;
9677 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Unit
);
9678 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Record
);
9679 }else if( cli_strncmp(zMode
,"markdown",n2
)==0 ){
9680 p
->mode
= MODE_Markdown
;
9682 }else if( cli_strncmp(zMode
,"table",n2
)==0 ){
9683 p
->mode
= MODE_Table
;
9685 }else if( cli_strncmp(zMode
,"box",n2
)==0 ){
9688 }else if( cli_strncmp(zMode
,"count",n2
)==0 ){
9689 p
->mode
= MODE_Count
;
9690 }else if( cli_strncmp(zMode
,"off",n2
)==0 ){
9692 }else if( cli_strncmp(zMode
,"json",n2
)==0 ){
9693 p
->mode
= MODE_Json
;
9695 eputz("Error: mode should be one of: "
9696 "ascii box column csv html insert json line list markdown "
9697 "qbox quote table tabs tcl\n");
9703 #ifndef SQLITE_SHELL_FIDDLE
9704 if( c
=='n' && cli_strcmp(azArg
[0], "nonce")==0 ){
9706 eputz("Usage: .nonce NONCE\n");
9708 }else if( p
->zNonce
==0 || cli_strcmp(azArg
[1],p
->zNonce
)!=0 ){
9709 eputf("line %d: incorrect nonce: \"%s\"\n",
9710 p
->lineno
, azArg
[1]);
9714 return 0; /* Return immediately to bypass the safe mode reset
9715 ** at the end of this procedure */
9718 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9720 if( c
=='n' && cli_strncmp(azArg
[0], "nullvalue", n
)==0 ){
9722 sqlite3_snprintf(sizeof(p
->nullValue
), p
->nullValue
,
9723 "%.*s", (int)ArraySize(p
->nullValue
)-1, azArg
[1]);
9725 eputz("Usage: .nullvalue STRING\n");
9730 if( c
=='o' && cli_strncmp(azArg
[0], "open", n
)==0 && n
>=2 ){
9731 const char *zFN
= 0; /* Pointer to constant filename */
9732 char *zNewFilename
= 0; /* Name of the database file to open */
9733 int iName
= 1; /* Index in azArg[] of the filename */
9734 int newFlag
= 0; /* True to delete file before opening */
9735 int openMode
= SHELL_OPEN_UNSPEC
;
9737 /* Check for command-line arguments */
9738 for(iName
=1; iName
<nArg
; iName
++){
9739 const char *z
= azArg
[iName
];
9740 #ifndef SQLITE_SHELL_FIDDLE
9741 if( optionMatch(z
,"new") ){
9743 #ifdef SQLITE_HAVE_ZLIB
9744 }else if( optionMatch(z
, "zip") ){
9745 openMode
= SHELL_OPEN_ZIPFILE
;
9747 }else if( optionMatch(z
, "append") ){
9748 openMode
= SHELL_OPEN_APPENDVFS
;
9749 }else if( optionMatch(z
, "readonly") ){
9750 openMode
= SHELL_OPEN_READONLY
;
9751 }else if( optionMatch(z
, "nofollow") ){
9752 p
->openFlags
|= SQLITE_OPEN_NOFOLLOW
;
9753 #ifndef SQLITE_OMIT_DESERIALIZE
9754 }else if( optionMatch(z
, "deserialize") ){
9755 openMode
= SHELL_OPEN_DESERIALIZE
;
9756 }else if( optionMatch(z
, "hexdb") ){
9757 openMode
= SHELL_OPEN_HEXDB
;
9758 }else if( optionMatch(z
, "maxsize") && iName
+1<nArg
){
9759 p
->szMax
= integerValue(azArg
[++iName
]);
9760 #endif /* SQLITE_OMIT_DESERIALIZE */
9762 #endif /* !SQLITE_SHELL_FIDDLE */
9764 eputf("unknown option: %s\n", z
);
9766 goto meta_command_exit
;
9768 eputf("extra argument: \"%s\"\n", z
);
9770 goto meta_command_exit
;
9776 /* Close the existing database */
9777 session_close_all(p
, -1);
9780 p
->pAuxDb
->zDbFilename
= 0;
9781 sqlite3_free(p
->pAuxDb
->zFreeOnClose
);
9782 p
->pAuxDb
->zFreeOnClose
= 0;
9783 p
->openMode
= openMode
;
9787 /* If a filename is specified, try to open it first */
9788 if( zFN
|| p
->openMode
==SHELL_OPEN_HEXDB
){
9789 if( newFlag
&& zFN
&& !p
->bSafeMode
) shellDeleteFile(zFN
);
9790 #ifndef SQLITE_SHELL_FIDDLE
9792 && p
->openMode
!=SHELL_OPEN_HEXDB
9794 && cli_strcmp(zFN
,":memory:")!=0
9796 failIfSafeMode(p
, "cannot open disk-based database files in safe mode");
9799 /* WASM mode has its own sandboxed pseudo-filesystem. */
9802 zNewFilename
= sqlite3_mprintf("%s", zFN
);
9803 shell_check_oom(zNewFilename
);
9807 p
->pAuxDb
->zDbFilename
= zNewFilename
;
9808 open_db(p
, OPEN_DB_KEEPALIVE
);
9810 eputf("Error: cannot open '%s'\n", zNewFilename
);
9811 sqlite3_free(zNewFilename
);
9813 p
->pAuxDb
->zFreeOnClose
= zNewFilename
;
9817 /* As a fall-back open a TEMP database */
9818 p
->pAuxDb
->zDbFilename
= 0;
9823 #ifndef SQLITE_SHELL_FIDDLE
9825 && (cli_strncmp(azArg
[0], "output", n
)==0
9826 || cli_strncmp(azArg
[0], "once", n
)==0))
9827 || (c
=='e' && n
==5 && cli_strcmp(azArg
[0],"excel")==0)
9833 int bOnce
= 0; /* 0: .output, 1: .once, 2: .excel */
9834 static const char *zBomUtf8
= "\xef\xbb\xbf";
9835 const char *zBom
= 0;
9837 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
9841 }else if( cli_strncmp(azArg
[0],"once",n
)==0 ){
9844 for(i
=1; i
<nArg
; i
++){
9847 if( z
[1]=='-' ) z
++;
9848 if( cli_strcmp(z
,"-bom")==0 ){
9850 }else if( c
!='e' && cli_strcmp(z
,"-x")==0 ){
9851 eMode
= 'x'; /* spreadsheet */
9852 }else if( c
!='e' && cli_strcmp(z
,"-e")==0 ){
9853 eMode
= 'e'; /* text editor */
9855 oputf("ERROR: unknown option: \"%s\". Usage:\n", azArg
[i
]);
9856 showHelp(p
->out
, azArg
[0]);
9858 goto meta_command_exit
;
9860 }else if( zFile
==0 && eMode
!='e' && eMode
!='x' ){
9861 zFile
= sqlite3_mprintf("%s", z
);
9862 if( zFile
&& zFile
[0]=='|' ){
9863 while( i
+1<nArg
) zFile
= sqlite3_mprintf("%z %s", zFile
, azArg
[++i
]);
9867 oputf("ERROR: extra parameter: \"%s\". Usage:\n", azArg
[i
]);
9868 showHelp(p
->out
, azArg
[0]);
9870 sqlite3_free(zFile
);
9871 goto meta_command_exit
;
9875 zFile
= sqlite3_mprintf("stdout");
9883 #ifndef SQLITE_NOHAVE_SYSTEM
9884 if( eMode
=='e' || eMode
=='x' ){
9888 /* spreadsheet mode. Output as CSV. */
9889 newTempFile(p
, "csv");
9890 ShellClearFlag(p
, SHFLG_Echo
);
9892 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
9893 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
9895 /* text editor mode */
9896 newTempFile(p
, "txt");
9899 sqlite3_free(zFile
);
9900 zFile
= sqlite3_mprintf("%s", p
->zTempFile
);
9902 #endif /* SQLITE_NOHAVE_SYSTEM */
9903 shell_check_oom(zFile
);
9904 if( zFile
[0]=='|' ){
9905 #ifdef SQLITE_OMIT_POPEN
9906 eputz("Error: pipes are not supported in this OS\n");
9908 output_redir(p
, stdout
);
9910 FILE *pfPipe
= popen(zFile
+ 1, "w");
9912 eputf("Error: cannot open pipe \"%s\"\n", zFile
+ 1);
9915 output_redir(p
, pfPipe
);
9916 if( zBom
) oputz(zBom
);
9917 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
9921 FILE *pfFile
= output_file_open(zFile
, bTxtMode
);
9923 if( cli_strcmp(zFile
,"off")!=0 ){
9924 eputf("Error: cannot write to \"%s\"\n", zFile
);
9928 output_redir(p
, pfFile
);
9929 if( zBom
) oputz(zBom
);
9930 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
9933 sqlite3_free(zFile
);
9935 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
9937 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "parameter", n
)==0 ){
9939 if( nArg
<=1 ) goto parameter_syntax_error
;
9942 ** Clear all bind parameters by dropping the TEMP table that holds them.
9944 if( nArg
==2 && cli_strcmp(azArg
[1],"clear")==0 ){
9945 sqlite3_exec(p
->db
, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
9950 ** List all bind parameters.
9952 if( nArg
==2 && cli_strcmp(azArg
[1],"list")==0 ){
9953 sqlite3_stmt
*pStmt
= 0;
9956 rx
= sqlite3_prepare_v2(p
->db
,
9957 "SELECT max(length(key)) "
9958 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
9959 if( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9960 len
= sqlite3_column_int(pStmt
, 0);
9961 if( len
>40 ) len
= 40;
9963 sqlite3_finalize(pStmt
);
9966 rx
= sqlite3_prepare_v2(p
->db
,
9967 "SELECT key, quote(value) "
9968 "FROM temp.sqlite_parameters;", -1, &pStmt
, 0);
9969 while( rx
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
9970 oputf("%-*s %s\n", len
, sqlite3_column_text(pStmt
,0),
9971 sqlite3_column_text(pStmt
,1));
9973 sqlite3_finalize(pStmt
);
9978 ** Make sure the TEMP table used to hold bind parameters exists.
9979 ** Create it if necessary.
9981 if( nArg
==2 && cli_strcmp(azArg
[1],"init")==0 ){
9985 /* .parameter set NAME VALUE
9986 ** Set or reset a bind parameter. NAME should be the full parameter
9987 ** name exactly as it appears in the query. (ex: $abc, @def). The
9988 ** VALUE can be in either SQL literal notation, or if not it will be
9989 ** understood to be a text string.
9991 if( nArg
==4 && cli_strcmp(azArg
[1],"set")==0 ){
9994 sqlite3_stmt
*pStmt
;
9995 const char *zKey
= azArg
[2];
9996 const char *zValue
= azArg
[3];
9998 zSql
= sqlite3_mprintf(
9999 "REPLACE INTO temp.sqlite_parameters(key,value)"
10000 "VALUES(%Q,%s);", zKey
, zValue
);
10001 shell_check_oom(zSql
);
10003 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
10004 sqlite3_free(zSql
);
10005 if( rx
!=SQLITE_OK
){
10006 sqlite3_finalize(pStmt
);
10008 zSql
= sqlite3_mprintf(
10009 "REPLACE INTO temp.sqlite_parameters(key,value)"
10010 "VALUES(%Q,%Q);", zKey
, zValue
);
10011 shell_check_oom(zSql
);
10012 rx
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
10013 sqlite3_free(zSql
);
10014 if( rx
!=SQLITE_OK
){
10015 oputf("Error: %s\n", sqlite3_errmsg(p
->db
));
10016 sqlite3_finalize(pStmt
);
10021 sqlite3_step(pStmt
);
10022 sqlite3_finalize(pStmt
);
10025 /* .parameter unset NAME
10026 ** Remove the NAME binding from the parameter binding table, if it
10029 if( nArg
==3 && cli_strcmp(azArg
[1],"unset")==0 ){
10030 char *zSql
= sqlite3_mprintf(
10031 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg
[2]);
10032 shell_check_oom(zSql
);
10033 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
10034 sqlite3_free(zSql
);
10036 /* If no command name matches, show a syntax error */
10037 parameter_syntax_error
:
10038 showHelp(p
->out
, "parameter");
10041 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "print", n
)==0 ){
10043 for(i
=1; i
<nArg
; i
++){
10044 if( i
>1 ) oputz(" ");
10050 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10051 if( c
=='p' && n
>=3 && cli_strncmp(azArg
[0], "progress", n
)==0 ){
10054 p
->flgProgress
= 0;
10057 for(i
=1; i
<nArg
; i
++){
10058 const char *z
= azArg
[i
];
10061 if( z
[0]=='-' ) z
++;
10062 if( cli_strcmp(z
,"quiet")==0 || cli_strcmp(z
,"q")==0 ){
10063 p
->flgProgress
|= SHELL_PROGRESS_QUIET
;
10066 if( cli_strcmp(z
,"reset")==0 ){
10067 p
->flgProgress
|= SHELL_PROGRESS_RESET
;
10070 if( cli_strcmp(z
,"once")==0 ){
10071 p
->flgProgress
|= SHELL_PROGRESS_ONCE
;
10074 if( cli_strcmp(z
,"limit")==0 ){
10076 eputz("Error: missing argument on --limit\n");
10078 goto meta_command_exit
;
10080 p
->mxProgress
= (int)integerValue(azArg
[++i
]);
10084 eputf("Error: unknown option: \"%s\"\n", azArg
[i
]);
10086 goto meta_command_exit
;
10088 nn
= (int)integerValue(z
);
10092 sqlite3_progress_handler(p
->db
, nn
, progress_handler
, p
);
10094 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10096 if( c
=='p' && cli_strncmp(azArg
[0], "prompt", n
)==0 ){
10098 shell_strncpy(mainPrompt
,azArg
[1],(int)ArraySize(mainPrompt
)-1);
10101 shell_strncpy(continuePrompt
,azArg
[2],(int)ArraySize(continuePrompt
)-1);
10105 #ifndef SQLITE_SHELL_FIDDLE
10106 if( c
=='q' && cli_strncmp(azArg
[0], "quit", n
)==0 ){
10111 #ifndef SQLITE_SHELL_FIDDLE
10112 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "read", n
)==0 ){
10113 FILE *inSaved
= p
->in
;
10114 int savedLineno
= p
->lineno
;
10115 failIfSafeMode(p
, "cannot run .read in safe mode");
10117 eputz("Usage: .read FILE\n");
10119 goto meta_command_exit
;
10121 if( azArg
[1][0]=='|' ){
10122 #ifdef SQLITE_OMIT_POPEN
10123 eputz("Error: pipes are not supported in this OS\n");
10127 p
->in
= popen(azArg
[1]+1, "r");
10129 eputf("Error: cannot open \"%s\"\n", azArg
[1]);
10132 rc
= process_input(p
);
10136 }else if( (p
->in
= openChrSource(azArg
[1]))==0 ){
10137 eputf("Error: cannot open \"%s\"\n", azArg
[1]);
10140 rc
= process_input(p
);
10144 p
->lineno
= savedLineno
;
10146 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10148 #ifndef SQLITE_SHELL_FIDDLE
10149 if( c
=='r' && n
>=3 && cli_strncmp(azArg
[0], "restore", n
)==0 ){
10150 const char *zSrcFile
;
10153 sqlite3_backup
*pBackup
;
10156 failIfSafeMode(p
, "cannot run .restore in safe mode");
10158 zSrcFile
= azArg
[1];
10160 }else if( nArg
==3 ){
10161 zSrcFile
= azArg
[2];
10164 eputz("Usage: .restore ?DB? FILE\n");
10166 goto meta_command_exit
;
10168 rc
= sqlite3_open(zSrcFile
, &pSrc
);
10169 if( rc
!=SQLITE_OK
){
10170 eputf("Error: cannot open \"%s\"\n", zSrcFile
);
10175 pBackup
= sqlite3_backup_init(p
->db
, zDb
, pSrc
, "main");
10177 shellDatabaseError(p
->db
);
10181 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
10182 || rc
==SQLITE_BUSY
){
10183 if( rc
==SQLITE_BUSY
){
10184 if( nTimeout
++ >= 3 ) break;
10185 sqlite3_sleep(100);
10188 sqlite3_backup_finish(pBackup
);
10189 if( rc
==SQLITE_DONE
){
10191 }else if( rc
==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
10192 eputz("Error: source database is busy\n");
10195 shellDatabaseError(p
->db
);
10200 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
10202 if( c
=='s' && cli_strncmp(azArg
[0], "scanstats", n
)==0 ){
10204 if( cli_strcmp(azArg
[1], "vm")==0 ){
10205 p
->scanstatsOn
= 3;
10207 if( cli_strcmp(azArg
[1], "est")==0 ){
10208 p
->scanstatsOn
= 2;
10210 p
->scanstatsOn
= (u8
)booleanValue(azArg
[1]);
10214 p
->db
, SQLITE_DBCONFIG_STMT_SCANSTATUS
, p
->scanstatsOn
, (int*)0
10216 #if !defined(SQLITE_ENABLE_STMT_SCANSTATUS)
10217 eputz("Warning: .scanstats not available in this build.\n");
10218 #elif !defined(SQLITE_ENABLE_BYTECODE_VTAB)
10219 if( p
->scanstatsOn
==3 ){
10220 eputz("Warning: \".scanstats vm\" not available in this build.\n");
10224 eputz("Usage: .scanstats on|off|est\n");
10229 if( c
=='s' && cli_strncmp(azArg
[0], "schema", n
)==0 ){
10233 const char *zDiv
= "(";
10234 const char *zName
= 0;
10237 int bNoSystemTabs
= 0;
10241 memcpy(&data
, p
, sizeof(data
));
10242 data
.showHeader
= 0;
10243 data
.cMode
= data
.mode
= MODE_Semi
;
10244 initText(&sSelect
);
10245 for(ii
=1; ii
<nArg
; ii
++){
10246 if( optionMatch(azArg
[ii
],"indent") ){
10247 data
.cMode
= data
.mode
= MODE_Pretty
;
10248 }else if( optionMatch(azArg
[ii
],"debug") ){
10250 }else if( optionMatch(azArg
[ii
],"nosys") ){
10252 }else if( azArg
[ii
][0]=='-' ){
10253 eputf("Unknown option: \"%s\"\n", azArg
[ii
]);
10255 goto meta_command_exit
;
10256 }else if( zName
==0 ){
10259 eputz("Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
10261 goto meta_command_exit
;
10265 int isSchema
= sqlite3_strlike(zName
, "sqlite_master", '\\')==0
10266 || sqlite3_strlike(zName
, "sqlite_schema", '\\')==0
10267 || sqlite3_strlike(zName
,"sqlite_temp_master", '\\')==0
10268 || sqlite3_strlike(zName
,"sqlite_temp_schema", '\\')==0;
10270 char *new_argv
[2], *new_colv
[2];
10271 new_argv
[0] = sqlite3_mprintf(
10272 "CREATE TABLE %s (\n"
10275 " tbl_name text,\n"
10276 " rootpage integer,\n"
10279 shell_check_oom(new_argv
[0]);
10281 new_colv
[0] = "sql";
10283 callback(&data
, 1, new_argv
, new_colv
);
10284 sqlite3_free(new_argv
[0]);
10288 sqlite3_stmt
*pStmt
= 0;
10289 rc
= sqlite3_prepare_v2(p
->db
, "SELECT name FROM pragma_database_list",
10292 shellDatabaseError(p
->db
);
10293 sqlite3_finalize(pStmt
);
10295 goto meta_command_exit
;
10297 appendText(&sSelect
, "SELECT sql FROM", 0);
10299 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
10300 const char *zDb
= (const char*)sqlite3_column_text(pStmt
, 0);
10302 sqlite3_snprintf(sizeof(zScNum
), zScNum
, "%d", ++iSchema
);
10303 appendText(&sSelect
, zDiv
, 0);
10304 zDiv
= " UNION ALL ";
10305 appendText(&sSelect
, "SELECT shell_add_schema(sql,", 0);
10306 if( sqlite3_stricmp(zDb
, "main")!=0 ){
10307 appendText(&sSelect
, zDb
, '\'');
10309 appendText(&sSelect
, "NULL", 0);
10311 appendText(&sSelect
, ",name) AS sql, type, tbl_name, name, rowid,", 0);
10312 appendText(&sSelect
, zScNum
, 0);
10313 appendText(&sSelect
, " AS snum, ", 0);
10314 appendText(&sSelect
, zDb
, '\'');
10315 appendText(&sSelect
, " AS sname FROM ", 0);
10316 appendText(&sSelect
, zDb
, quoteChar(zDb
));
10317 appendText(&sSelect
, ".sqlite_schema", 0);
10319 sqlite3_finalize(pStmt
);
10320 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
10322 appendText(&sSelect
,
10323 " UNION ALL SELECT shell_module_schema(name),"
10324 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
10328 appendText(&sSelect
, ") WHERE ", 0);
10330 char *zQarg
= sqlite3_mprintf("%Q", zName
);
10332 shell_check_oom(zQarg
);
10333 bGlob
= strchr(zName
, '*') != 0 || strchr(zName
, '?') != 0 ||
10334 strchr(zName
, '[') != 0;
10335 if( strchr(zName
, '.') ){
10336 appendText(&sSelect
, "lower(printf('%s.%s',sname,tbl_name))", 0);
10338 appendText(&sSelect
, "lower(tbl_name)", 0);
10340 appendText(&sSelect
, bGlob
? " GLOB " : " LIKE ", 0);
10341 appendText(&sSelect
, zQarg
, 0);
10343 appendText(&sSelect
, " ESCAPE '\\' ", 0);
10345 appendText(&sSelect
, " AND ", 0);
10346 sqlite3_free(zQarg
);
10348 if( bNoSystemTabs
){
10349 appendText(&sSelect
, "name NOT LIKE 'sqlite_%%' AND ", 0);
10351 appendText(&sSelect
, "sql IS NOT NULL"
10352 " ORDER BY snum, rowid", 0);
10354 oputf("SQL: %s;\n", sSelect
.z
);
10356 rc
= sqlite3_exec(p
->db
, sSelect
.z
, callback
, &data
, &zErrMsg
);
10358 freeText(&sSelect
);
10361 shellEmitError(zErrMsg
);
10362 sqlite3_free(zErrMsg
);
10364 }else if( rc
!= SQLITE_OK
){
10365 eputz("Error: querying schema information\n");
10372 if( (c
=='s' && n
==11 && cli_strncmp(azArg
[0], "selecttrace", n
)==0)
10373 || (c
=='t' && n
==9 && cli_strncmp(azArg
[0], "treetrace", n
)==0)
10375 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
10376 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 1, &x
);
10379 #if defined(SQLITE_ENABLE_SESSION)
10380 if( c
=='s' && cli_strncmp(azArg
[0],"session",n
)==0 && n
>=3 ){
10381 struct AuxDb
*pAuxDb
= p
->pAuxDb
;
10382 OpenSession
*pSession
= &pAuxDb
->aSession
[0];
10383 char **azCmd
= &azArg
[1];
10385 int nCmd
= nArg
- 1;
10387 if( nArg
<=1 ) goto session_syntax_error
;
10390 for(iSes
=0; iSes
<pAuxDb
->nSession
; iSes
++){
10391 if( cli_strcmp(pAuxDb
->aSession
[iSes
].zName
, azArg
[1])==0 ) break;
10393 if( iSes
<pAuxDb
->nSession
){
10394 pSession
= &pAuxDb
->aSession
[iSes
];
10398 pSession
= &pAuxDb
->aSession
[0];
10403 /* .session attach TABLE
10404 ** Invoke the sqlite3session_attach() interface to attach a particular
10405 ** table so that it is never filtered.
10407 if( cli_strcmp(azCmd
[0],"attach")==0 ){
10408 if( nCmd
!=2 ) goto session_syntax_error
;
10409 if( pSession
->p
==0 ){
10411 eputz("ERROR: No sessions are open\n");
10413 rc
= sqlite3session_attach(pSession
->p
, azCmd
[1]);
10415 eputf("ERROR: sqlite3session_attach() returns %d\n",rc
);
10421 /* .session changeset FILE
10422 ** .session patchset FILE
10423 ** Write a changeset or patchset into a file. The file is overwritten.
10425 if( cli_strcmp(azCmd
[0],"changeset")==0
10426 || cli_strcmp(azCmd
[0],"patchset")==0
10429 failIfSafeMode(p
, "cannot run \".session %s\" in safe mode", azCmd
[0]);
10430 if( nCmd
!=2 ) goto session_syntax_error
;
10431 if( pSession
->p
==0 ) goto session_not_open
;
10432 out
= fopen(azCmd
[1], "wb");
10434 eputf("ERROR: cannot open \"%s\" for writing\n",
10439 if( azCmd
[0][0]=='c' ){
10440 rc
= sqlite3session_changeset(pSession
->p
, &szChng
, &pChng
);
10442 rc
= sqlite3session_patchset(pSession
->p
, &szChng
, &pChng
);
10445 sputf(stdout
, "Error: error code %d\n", rc
);
10449 && fwrite(pChng
, szChng
, 1, out
)!=1 ){
10450 eputf("ERROR: Failed to write entire %d-byte output\n", szChng
);
10452 sqlite3_free(pChng
);
10458 ** Close the identified session
10460 if( cli_strcmp(azCmd
[0], "close")==0 ){
10461 if( nCmd
!=1 ) goto session_syntax_error
;
10462 if( pAuxDb
->nSession
){
10463 session_close(pSession
);
10464 pAuxDb
->aSession
[iSes
] = pAuxDb
->aSession
[--pAuxDb
->nSession
];
10468 /* .session enable ?BOOLEAN?
10469 ** Query or set the enable flag
10471 if( cli_strcmp(azCmd
[0], "enable")==0 ){
10473 if( nCmd
>2 ) goto session_syntax_error
;
10474 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
10475 if( pAuxDb
->nSession
){
10476 ii
= sqlite3session_enable(pSession
->p
, ii
);
10477 oputf("session %s enable flag = %d\n", pSession
->zName
, ii
);
10481 /* .session filter GLOB ....
10482 ** Set a list of GLOB patterns of table names to be excluded.
10484 if( cli_strcmp(azCmd
[0], "filter")==0 ){
10486 if( nCmd
<2 ) goto session_syntax_error
;
10487 if( pAuxDb
->nSession
){
10488 for(ii
=0; ii
<pSession
->nFilter
; ii
++){
10489 sqlite3_free(pSession
->azFilter
[ii
]);
10491 sqlite3_free(pSession
->azFilter
);
10492 nByte
= sizeof(pSession
->azFilter
[0])*(nCmd
-1);
10493 pSession
->azFilter
= sqlite3_malloc( nByte
);
10494 shell_check_oom( pSession
->azFilter
);
10495 for(ii
=1; ii
<nCmd
; ii
++){
10496 char *x
= pSession
->azFilter
[ii
-1] = sqlite3_mprintf("%s", azCmd
[ii
]);
10497 shell_check_oom(x
);
10499 pSession
->nFilter
= ii
-1;
10503 /* .session indirect ?BOOLEAN?
10504 ** Query or set the indirect flag
10506 if( cli_strcmp(azCmd
[0], "indirect")==0 ){
10508 if( nCmd
>2 ) goto session_syntax_error
;
10509 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
10510 if( pAuxDb
->nSession
){
10511 ii
= sqlite3session_indirect(pSession
->p
, ii
);
10512 oputf("session %s indirect flag = %d\n", pSession
->zName
, ii
);
10516 /* .session isempty
10517 ** Determine if the session is empty
10519 if( cli_strcmp(azCmd
[0], "isempty")==0 ){
10521 if( nCmd
!=1 ) goto session_syntax_error
;
10522 if( pAuxDb
->nSession
){
10523 ii
= sqlite3session_isempty(pSession
->p
);
10524 oputf("session %s isempty flag = %d\n", pSession
->zName
, ii
);
10529 ** List all currently open sessions
10531 if( cli_strcmp(azCmd
[0],"list")==0 ){
10532 for(i
=0; i
<pAuxDb
->nSession
; i
++){
10533 oputf("%d %s\n", i
, pAuxDb
->aSession
[i
].zName
);
10537 /* .session open DB NAME
10538 ** Open a new session called NAME on the attached database DB.
10539 ** DB is normally "main".
10541 if( cli_strcmp(azCmd
[0],"open")==0 ){
10543 if( nCmd
!=3 ) goto session_syntax_error
;
10545 if( zName
[0]==0 ) goto session_syntax_error
;
10546 for(i
=0; i
<pAuxDb
->nSession
; i
++){
10547 if( cli_strcmp(pAuxDb
->aSession
[i
].zName
,zName
)==0 ){
10548 eputf("Session \"%s\" already exists\n", zName
);
10549 goto meta_command_exit
;
10552 if( pAuxDb
->nSession
>=ArraySize(pAuxDb
->aSession
) ){
10553 eputf("Maximum of %d sessions\n", ArraySize(pAuxDb
->aSession
));
10554 goto meta_command_exit
;
10556 pSession
= &pAuxDb
->aSession
[pAuxDb
->nSession
];
10557 rc
= sqlite3session_create(p
->db
, azCmd
[1], &pSession
->p
);
10559 eputf("Cannot open session: error code=%d\n", rc
);
10561 goto meta_command_exit
;
10563 pSession
->nFilter
= 0;
10564 sqlite3session_table_filter(pSession
->p
, session_filter
, pSession
);
10565 pAuxDb
->nSession
++;
10566 pSession
->zName
= sqlite3_mprintf("%s", zName
);
10567 shell_check_oom(pSession
->zName
);
10569 /* If no command name matches, show a syntax error */
10570 session_syntax_error
:
10571 showHelp(p
->out
, "session");
10575 #ifdef SQLITE_DEBUG
10576 /* Undocumented commands for internal testing. Subject to change
10577 ** without notice. */
10578 if( c
=='s' && n
>=10 && cli_strncmp(azArg
[0], "selftest-", 9)==0 ){
10579 if( cli_strncmp(azArg
[0]+9, "boolean", n
-9)==0 ){
10581 for(i
=1; i
<nArg
; i
++){
10582 v
= booleanValue(azArg
[i
]);
10583 oputf("%s: %d 0x%x\n", azArg
[i
], v
, v
);
10586 if( cli_strncmp(azArg
[0]+9, "integer", n
-9)==0 ){
10587 int i
; sqlite3_int64 v
;
10588 for(i
=1; i
<nArg
; i
++){
10590 v
= integerValue(azArg
[i
]);
10591 sqlite3_snprintf(sizeof(zBuf
),zBuf
,"%s: %lld 0x%llx\n", azArg
[i
],v
,v
);
10598 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"selftest",n
)==0 ){
10599 int bIsInit
= 0; /* True to initialize the SELFTEST table */
10600 int bVerbose
= 0; /* Verbose output */
10601 int bSelftestExists
; /* True if SELFTEST already exists */
10602 int i
, k
; /* Loop counters */
10603 int nTest
= 0; /* Number of tests runs */
10604 int nErr
= 0; /* Number of errors seen */
10605 ShellText str
; /* Answer for a query */
10606 sqlite3_stmt
*pStmt
= 0; /* Query against the SELFTEST table */
10609 for(i
=1; i
<nArg
; i
++){
10610 const char *z
= azArg
[i
];
10611 if( z
[0]=='-' && z
[1]=='-' ) z
++;
10612 if( cli_strcmp(z
,"-init")==0 ){
10615 if( cli_strcmp(z
,"-v")==0 ){
10619 eputf("Unknown option \"%s\" on \"%s\"\n", azArg
[i
], azArg
[0]);
10620 eputz("Should be one of: --init -v\n");
10622 goto meta_command_exit
;
10625 if( sqlite3_table_column_metadata(p
->db
,"main","selftest",0,0,0,0,0,0)
10627 bSelftestExists
= 0;
10629 bSelftestExists
= 1;
10632 createSelftestTable(p
);
10633 bSelftestExists
= 1;
10636 appendText(&str
, "x", 0);
10637 for(k
=bSelftestExists
; k
>=0; k
--){
10639 rc
= sqlite3_prepare_v2(p
->db
,
10640 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
10643 rc
= sqlite3_prepare_v2(p
->db
,
10644 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
10645 " (1,'run','PRAGMA integrity_check','ok')",
10649 eputz("Error querying the selftest table\n");
10651 sqlite3_finalize(pStmt
);
10652 goto meta_command_exit
;
10654 for(i
=1; sqlite3_step(pStmt
)==SQLITE_ROW
; i
++){
10655 int tno
= sqlite3_column_int(pStmt
, 0);
10656 const char *zOp
= (const char*)sqlite3_column_text(pStmt
, 1);
10657 const char *zSql
= (const char*)sqlite3_column_text(pStmt
, 2);
10658 const char *zAns
= (const char*)sqlite3_column_text(pStmt
, 3);
10660 if( zOp
==0 ) continue;
10661 if( zSql
==0 ) continue;
10662 if( zAns
==0 ) continue;
10665 sputf(stdout
, "%d: %s %s\n", tno
, zOp
, zSql
);
10667 if( cli_strcmp(zOp
,"memo")==0 ){
10668 oputf("%s\n", zSql
);
10670 if( cli_strcmp(zOp
,"run")==0 ){
10674 rc
= sqlite3_exec(p
->db
, zSql
, captureOutputCallback
, &str
, &zErrMsg
);
10677 oputf("Result: %s\n", str
.z
);
10679 if( rc
|| zErrMsg
){
10682 oputf("%d: error-code-%d: %s\n", tno
, rc
, zErrMsg
);
10683 sqlite3_free(zErrMsg
);
10684 }else if( cli_strcmp(zAns
,str
.z
)!=0 ){
10687 oputf("%d: Expected: [%s]\n", tno
, zAns
);
10688 oputf("%d: Got: [%s]\n", tno
, str
.z
);
10692 eputf("Unknown operation \"%s\" on selftest line %d\n", zOp
, tno
);
10696 } /* End loop over rows of content from SELFTEST */
10697 sqlite3_finalize(pStmt
);
10698 } /* End loop over k */
10700 oputf("%d errors out of %d tests\n", nErr
, nTest
);
10703 if( c
=='s' && cli_strncmp(azArg
[0], "separator", n
)==0 ){
10704 if( nArg
<2 || nArg
>3 ){
10705 eputz("Usage: .separator COL ?ROW?\n");
10709 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
,
10710 "%.*s", (int)ArraySize(p
->colSeparator
)-1, azArg
[1]);
10713 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
,
10714 "%.*s", (int)ArraySize(p
->rowSeparator
)-1, azArg
[2]);
10718 if( c
=='s' && n
>=4 && cli_strncmp(azArg
[0],"sha3sum",n
)==0 ){
10719 const char *zLike
= 0; /* Which table to checksum. 0 means everything */
10720 int i
; /* Loop counter */
10721 int bSchema
= 0; /* Also hash the schema */
10722 int bSeparate
= 0; /* Hash each table separately */
10723 int iSize
= 224; /* Hash algorithm to use */
10724 int bDebug
= 0; /* Only show the query that would have run */
10725 sqlite3_stmt
*pStmt
; /* For querying tables names */
10726 char *zSql
; /* SQL to be run */
10727 char *zSep
; /* Separator */
10728 ShellText sSql
; /* Complete SQL for the query to run the hash */
10729 ShellText sQuery
; /* Set of queries used to read all content */
10731 for(i
=1; i
<nArg
; i
++){
10732 const char *z
= azArg
[i
];
10735 if( z
[0]=='-' ) z
++;
10736 if( cli_strcmp(z
,"schema")==0 ){
10739 if( cli_strcmp(z
,"sha3-224")==0 || cli_strcmp(z
,"sha3-256")==0
10740 || cli_strcmp(z
,"sha3-384")==0 || cli_strcmp(z
,"sha3-512")==0
10742 iSize
= atoi(&z
[5]);
10744 if( cli_strcmp(z
,"debug")==0 ){
10748 eputf("Unknown option \"%s\" on \"%s\"\n", azArg
[i
], azArg
[0]);
10749 showHelp(p
->out
, azArg
[0]);
10751 goto meta_command_exit
;
10754 eputz("Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
10756 goto meta_command_exit
;
10760 if( sqlite3_strlike("sqlite\\_%", zLike
, '\\')==0 ) bSchema
= 1;
10764 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
10765 " WHERE type='table' AND coalesce(rootpage,0)>1"
10766 " UNION ALL SELECT 'sqlite_schema'"
10767 " ORDER BY 1 collate nocase";
10769 zSql
= "SELECT lower(name) as tname FROM sqlite_schema"
10770 " WHERE type='table' AND coalesce(rootpage,0)>1"
10771 " AND name NOT LIKE 'sqlite_%'"
10772 " ORDER BY 1 collate nocase";
10774 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
10777 appendText(&sSql
, "WITH [sha3sum$query](a,b) AS(",0);
10779 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
10780 const char *zTab
= (const char*)sqlite3_column_text(pStmt
,0);
10781 if( zTab
==0 ) continue;
10782 if( zLike
&& sqlite3_strlike(zLike
, zTab
, 0)!=0 ) continue;
10783 if( cli_strncmp(zTab
, "sqlite_",7)!=0 ){
10784 appendText(&sQuery
,"SELECT * FROM ", 0);
10785 appendText(&sQuery
,zTab
,'"');
10786 appendText(&sQuery
," NOT INDEXED;", 0);
10787 }else if( cli_strcmp(zTab
, "sqlite_schema")==0 ){
10788 appendText(&sQuery
,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
10789 " ORDER BY name;", 0);
10790 }else if( cli_strcmp(zTab
, "sqlite_sequence")==0 ){
10791 appendText(&sQuery
,"SELECT name,seq FROM sqlite_sequence"
10792 " ORDER BY name;", 0);
10793 }else if( cli_strcmp(zTab
, "sqlite_stat1")==0 ){
10794 appendText(&sQuery
,"SELECT tbl,idx,stat FROM sqlite_stat1"
10795 " ORDER BY tbl,idx;", 0);
10796 }else if( cli_strcmp(zTab
, "sqlite_stat4")==0 ){
10797 appendText(&sQuery
, "SELECT * FROM ", 0);
10798 appendText(&sQuery
, zTab
, 0);
10799 appendText(&sQuery
, " ORDER BY tbl, idx, rowid;\n", 0);
10801 appendText(&sSql
, zSep
, 0);
10802 appendText(&sSql
, sQuery
.z
, '\'');
10804 appendText(&sSql
, ",", 0);
10805 appendText(&sSql
, zTab
, '\'');
10808 sqlite3_finalize(pStmt
);
10810 zSql
= sqlite3_mprintf(
10812 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
10813 " FROM [sha3sum$query]",
10816 zSql
= sqlite3_mprintf(
10818 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
10819 " FROM [sha3sum$query]",
10822 shell_check_oom(zSql
);
10826 oputf("%s\n", zSql
);
10828 shell_exec(p
, zSql
, 0);
10830 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10833 char *zRevText
= /* Query for reversible to-blob-to-text check */
10834 "SELECT lower(name) as tname FROM sqlite_schema\n"
10835 "WHERE type='table' AND coalesce(rootpage,0)>1\n"
10836 "AND name NOT LIKE 'sqlite_%%'%s\n"
10837 "ORDER BY 1 collate nocase";
10838 zRevText
= sqlite3_mprintf(zRevText
, zLike
? " AND name LIKE $tspec" : "");
10839 zRevText
= sqlite3_mprintf(
10840 /* lower-case query is first run, producing upper-case query. */
10841 "with tabcols as materialized(\n"
10842 "select tname, cname\n"
10844 " select printf('\"%%w\"',ss.tname) as tname,"
10845 " printf('\"%%w\"',ti.name) as cname\n"
10846 " from (%z) ss\n inner join pragma_table_info(tname) ti))\n"
10847 "select 'SELECT total(bad_text_count) AS bad_text_count\n"
10848 "FROM ('||group_concat(query, ' UNION ALL ')||')' as btc_query\n"
10849 " from (select 'SELECT COUNT(*) AS bad_text_count\n"
10850 "FROM '||tname||' WHERE '\n"
10851 "||group_concat('CAST(CAST('||cname||' AS BLOB) AS TEXT)<>'||cname\n"
10852 "|| ' AND typeof('||cname||')=''text'' ',\n"
10853 "' OR ') as query, tname from tabcols group by tname)"
10855 shell_check_oom(zRevText
);
10856 if( bDebug
) oputf("%s\n", zRevText
);
10857 lrc
= sqlite3_prepare_v2(p
->db
, zRevText
, -1, &pStmt
, 0);
10858 if( lrc
!=SQLITE_OK
){
10859 /* assert(lrc==SQLITE_NOMEM); // might also be SQLITE_ERROR if the
10860 ** user does cruel and unnatural things like ".limit expr_depth 0". */
10863 if( zLike
) sqlite3_bind_text(pStmt
,1,zLike
,-1,SQLITE_STATIC
);
10864 lrc
= SQLITE_ROW
==sqlite3_step(pStmt
);
10866 const char *zGenQuery
= (char*)sqlite3_column_text(pStmt
,0);
10867 sqlite3_stmt
*pCheckStmt
;
10868 lrc
= sqlite3_prepare_v2(p
->db
, zGenQuery
, -1, &pCheckStmt
, 0);
10869 if( bDebug
) oputf("%s\n", zGenQuery
);
10870 if( lrc
!=SQLITE_OK
){
10873 if( SQLITE_ROW
==sqlite3_step(pCheckStmt
) ){
10874 double countIrreversible
= sqlite3_column_double(pCheckStmt
, 0);
10875 if( countIrreversible
>0 ){
10876 int sz
= (int)(countIrreversible
+ 0.5);
10877 eputf("Digest includes %d invalidly encoded text field%s.\n",
10878 sz
, (sz
>1)? "s": "");
10881 sqlite3_finalize(pCheckStmt
);
10883 sqlite3_finalize(pStmt
);
10886 if( rc
) eputz(".sha3sum failed.\n");
10887 sqlite3_free(zRevText
);
10889 #endif /* !defined(*_OMIT_SCHEMA_PRAGMAS) && !defined(*_OMIT_VIRTUALTABLE) */
10890 sqlite3_free(zSql
);
10893 #if !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE)
10895 && (cli_strncmp(azArg
[0], "shell", n
)==0
10896 || cli_strncmp(azArg
[0],"system",n
)==0)
10900 failIfSafeMode(p
, "cannot run .%s in safe mode", azArg
[0]);
10902 eputz("Usage: .system COMMAND\n");
10904 goto meta_command_exit
;
10906 zCmd
= sqlite3_mprintf(strchr(azArg
[1],' ')==0?"%s":"\"%s\"", azArg
[1]);
10907 for(i
=2; i
<nArg
&& zCmd
!=0; i
++){
10908 zCmd
= sqlite3_mprintf(strchr(azArg
[i
],' ')==0?"%z %s":"%z \"%s\"",
10912 x
= zCmd
!=0 ? system(zCmd
) : 1;
10913 consoleRenewSetup();
10914 sqlite3_free(zCmd
);
10915 if( x
) eputf("System command returns %d\n", x
);
10917 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) && !defined(SQLITE_SHELL_FIDDLE) */
10919 if( c
=='s' && cli_strncmp(azArg
[0], "show", n
)==0 ){
10920 static const char *azBool
[] = { "off", "on", "trigger", "full"};
10924 eputz("Usage: .show\n");
10926 goto meta_command_exit
;
10928 oputf("%12.12s: %s\n","echo",
10929 azBool
[ShellHasFlag(p
, SHFLG_Echo
)]);
10930 oputf("%12.12s: %s\n","eqp", azBool
[p
->autoEQP
&3]);
10931 oputf("%12.12s: %s\n","explain",
10932 p
->mode
==MODE_Explain
? "on" : p
->autoExplain
? "auto" : "off");
10933 oputf("%12.12s: %s\n","headers", azBool
[p
->showHeader
!=0]);
10934 if( p
->mode
==MODE_Column
10935 || (p
->mode
>=MODE_Markdown
&& p
->mode
<=MODE_Box
)
10937 oputf("%12.12s: %s --wrap %d --wordwrap %s --%squote\n", "mode",
10938 modeDescr
[p
->mode
], p
->cmOpts
.iWrap
,
10939 p
->cmOpts
.bWordWrap
? "on" : "off",
10940 p
->cmOpts
.bQuote
? "" : "no");
10942 oputf("%12.12s: %s\n","mode", modeDescr
[p
->mode
]);
10944 oputf("%12.12s: ", "nullvalue");
10945 output_c_string(p
->nullValue
);
10947 oputf("%12.12s: %s\n","output",
10948 strlen30(p
->outfile
) ? p
->outfile
: "stdout");
10949 oputf("%12.12s: ", "colseparator");
10950 output_c_string(p
->colSeparator
);
10952 oputf("%12.12s: ", "rowseparator");
10953 output_c_string(p
->rowSeparator
);
10955 switch( p
->statsOn
){
10956 case 0: zOut
= "off"; break;
10957 default: zOut
= "on"; break;
10958 case 2: zOut
= "stmt"; break;
10959 case 3: zOut
= "vmstep"; break;
10961 oputf("%12.12s: %s\n","stats", zOut
);
10962 oputf("%12.12s: ", "width");
10963 for (i
=0;i
<p
->nWidth
;i
++) {
10964 oputf("%d ", p
->colWidth
[i
]);
10967 oputf("%12.12s: %s\n", "filename",
10968 p
->pAuxDb
->zDbFilename
? p
->pAuxDb
->zDbFilename
: "");
10971 if( c
=='s' && cli_strncmp(azArg
[0], "stats", n
)==0 ){
10973 if( cli_strcmp(azArg
[1],"stmt")==0 ){
10975 }else if( cli_strcmp(azArg
[1],"vmstep")==0 ){
10978 p
->statsOn
= (u8
)booleanValue(azArg
[1]);
10980 }else if( nArg
==1 ){
10981 display_stats(p
->db
, p
, 0);
10983 eputz("Usage: .stats ?on|off|stmt|vmstep?\n");
10988 if( (c
=='t' && n
>1 && cli_strncmp(azArg
[0], "tables", n
)==0)
10989 || (c
=='i' && (cli_strncmp(azArg
[0], "indices", n
)==0
10990 || cli_strncmp(azArg
[0], "indexes", n
)==0) )
10992 sqlite3_stmt
*pStmt
;
10999 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
11001 sqlite3_finalize(pStmt
);
11002 return shellDatabaseError(p
->db
);
11005 if( nArg
>2 && c
=='i' ){
11006 /* It is an historical accident that the .indexes command shows an error
11007 ** when called with the wrong number of arguments whereas the .tables
11008 ** command does not. */
11009 eputz("Usage: .indexes ?LIKE-PATTERN?\n");
11011 sqlite3_finalize(pStmt
);
11012 goto meta_command_exit
;
11014 for(ii
=0; sqlite3_step(pStmt
)==SQLITE_ROW
; ii
++){
11015 const char *zDbName
= (const char*)sqlite3_column_text(pStmt
, 1);
11016 if( zDbName
==0 ) continue;
11017 if( s
.z
&& s
.z
[0] ) appendText(&s
, " UNION ALL ", 0);
11018 if( sqlite3_stricmp(zDbName
, "main")==0 ){
11019 appendText(&s
, "SELECT name FROM ", 0);
11021 appendText(&s
, "SELECT ", 0);
11022 appendText(&s
, zDbName
, '\'');
11023 appendText(&s
, "||'.'||name FROM ", 0);
11025 appendText(&s
, zDbName
, '"');
11026 appendText(&s
, ".sqlite_schema ", 0);
11028 appendText(&s
," WHERE type IN ('table','view')"
11029 " AND name NOT LIKE 'sqlite_%'"
11030 " AND name LIKE ?1", 0);
11032 appendText(&s
," WHERE type='index'"
11033 " AND tbl_name LIKE ?1", 0);
11036 rc
= sqlite3_finalize(pStmt
);
11037 if( rc
==SQLITE_OK
){
11038 appendText(&s
, " ORDER BY 1", 0);
11039 rc
= sqlite3_prepare_v2(p
->db
, s
.z
, -1, &pStmt
, 0);
11042 if( rc
) return shellDatabaseError(p
->db
);
11044 /* Run the SQL statement prepared by the above block. Store the results
11045 ** as an array of nul-terminated strings in azResult[]. */
11049 sqlite3_bind_text(pStmt
, 1, azArg
[1], -1, SQLITE_TRANSIENT
);
11051 sqlite3_bind_text(pStmt
, 1, "%", -1, SQLITE_STATIC
);
11053 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
11054 if( nRow
>=nAlloc
){
11056 int n2
= nAlloc
*2 + 10;
11057 azNew
= sqlite3_realloc64(azResult
, sizeof(azResult
[0])*n2
);
11058 shell_check_oom(azNew
);
11062 azResult
[nRow
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
11063 shell_check_oom(azResult
[nRow
]);
11066 if( sqlite3_finalize(pStmt
)!=SQLITE_OK
){
11067 rc
= shellDatabaseError(p
->db
);
11070 /* Pretty-print the contents of array azResult[] to the output */
11071 if( rc
==0 && nRow
>0 ){
11072 int len
, maxlen
= 0;
11074 int nPrintCol
, nPrintRow
;
11075 for(i
=0; i
<nRow
; i
++){
11076 len
= strlen30(azResult
[i
]);
11077 if( len
>maxlen
) maxlen
= len
;
11079 nPrintCol
= 80/(maxlen
+2);
11080 if( nPrintCol
<1 ) nPrintCol
= 1;
11081 nPrintRow
= (nRow
+ nPrintCol
- 1)/nPrintCol
;
11082 for(i
=0; i
<nPrintRow
; i
++){
11083 for(j
=i
; j
<nRow
; j
+=nPrintRow
){
11084 char *zSp
= j
<nPrintRow
? "" : " ";
11085 oputf("%s%-*s", zSp
, maxlen
, azResult
[j
] ? azResult
[j
]:"");
11091 for(ii
=0; ii
<nRow
; ii
++) sqlite3_free(azResult
[ii
]);
11092 sqlite3_free(azResult
);
11095 #ifndef SQLITE_SHELL_FIDDLE
11096 /* Begin redirecting output to the file "testcase-out.txt" */
11097 if( c
=='t' && cli_strcmp(azArg
[0],"testcase")==0 ){
11099 p
->out
= output_file_open("testcase-out.txt", 0);
11101 eputz("Error: cannot open 'testcase-out.txt'\n");
11104 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "%s", azArg
[1]);
11106 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "?");
11109 #endif /* !defined(SQLITE_SHELL_FIDDLE) */
11111 #ifndef SQLITE_UNTESTABLE
11112 if( c
=='t' && n
>=8 && cli_strncmp(azArg
[0], "testctrl", n
)==0 ){
11113 static const struct {
11114 const char *zCtrlName
; /* Name of a test-control option */
11115 int ctrlCode
; /* Integer code for that option */
11116 int unSafe
; /* Not valid unless --unsafe-testing */
11117 const char *zUsage
; /* Usage notes */
11119 {"always", SQLITE_TESTCTRL_ALWAYS
, 1, "BOOLEAN" },
11120 {"assert", SQLITE_TESTCTRL_ASSERT
, 1, "BOOLEAN" },
11121 /*{"benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS,1, "" },*/
11122 /*{"bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, 1, "" },*/
11123 {"byteorder", SQLITE_TESTCTRL_BYTEORDER
, 0, "" },
11124 {"extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS
,0,"BOOLEAN" },
11125 {"fault_install", SQLITE_TESTCTRL_FAULT_INSTALL
, 1,"args..." },
11126 {"fk_no_action", SQLITE_TESTCTRL_FK_NO_ACTION
, 0, "BOOLEAN" },
11127 {"imposter", SQLITE_TESTCTRL_IMPOSTER
,1,"SCHEMA ON/OFF ROOTPAGE"},
11128 {"internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
,0,"" },
11129 {"json_selfcheck", SQLITE_TESTCTRL_JSON_SELFCHECK
,0,"BOOLEAN" },
11130 {"localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT
,0,"BOOLEAN" },
11131 {"never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT
,1, "BOOLEAN" },
11132 {"optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS
,0,"DISABLE-MASK ..."},
11134 {"parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE
,0,"" },
11136 {"pending_byte", SQLITE_TESTCTRL_PENDING_BYTE
,1, "OFFSET " },
11137 {"prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE
,0, "" },
11138 {"prng_save", SQLITE_TESTCTRL_PRNG_SAVE
, 0, "" },
11139 {"prng_seed", SQLITE_TESTCTRL_PRNG_SEED
, 0, "SEED ?db?" },
11140 {"seek_count", SQLITE_TESTCTRL_SEEK_COUNT
, 0, "" },
11141 {"sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP
, 0, "NMAX" },
11142 {"tune", SQLITE_TESTCTRL_TUNE
, 1, "ID VALUE" },
11143 {"uselongdouble", SQLITE_TESTCTRL_USELONGDOUBLE
,0,"?BOOLEAN|\"default\"?"},
11147 int rc2
= 0; /* 0: usage. 1: %d 2: %x 3: no-output */
11150 const char *zCmd
= 0;
11153 zCmd
= nArg
>=2 ? azArg
[1] : "help";
11155 /* The argument can optionally begin with "-" or "--" */
11156 if( zCmd
[0]=='-' && zCmd
[1] ){
11158 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
11161 /* --help lists all test-controls */
11162 if( cli_strcmp(zCmd
,"help")==0 ){
11163 oputz("Available test-controls:\n");
11164 for(i
=0; i
<ArraySize(aCtrl
); i
++){
11165 if( aCtrl
[i
].unSafe
&& !ShellHasFlag(p
,SHFLG_TestingMode
) ) continue;
11166 oputf(" .testctrl %s %s\n",
11167 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
11170 goto meta_command_exit
;
11173 /* convert testctrl text option to value. allow any unique prefix
11174 ** of the option name, or a numerical value. */
11175 n2
= strlen30(zCmd
);
11176 for(i
=0; i
<ArraySize(aCtrl
); i
++){
11177 if( aCtrl
[i
].unSafe
&& !ShellHasFlag(p
,SHFLG_TestingMode
) ) continue;
11178 if( cli_strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
11180 testctrl
= aCtrl
[i
].ctrlCode
;
11183 eputf("Error: ambiguous test-control: \"%s\"\n"
11184 "Use \".testctrl --help\" for help\n", zCmd
);
11186 goto meta_command_exit
;
11191 eputf("Error: unknown test-control: %s\n"
11192 "Use \".testctrl --help\" for help\n", zCmd
);
11196 /* Special processing for .testctrl opt MASK ...
11197 ** Each MASK argument can be one of:
11199 ** +LABEL Enable the named optimization
11201 ** -LABEL Disable the named optimization
11203 ** INTEGER Mask of optimizations to disable
11205 case SQLITE_TESTCTRL_OPTIMIZATIONS
: {
11206 static const struct {
11207 unsigned int mask
; /* Mask for this optimization */
11208 unsigned int bDsply
; /* Display this on output */
11209 const char *zLabel
; /* Name of optimization */
11211 { 0x00000001, 1, "QueryFlattener" },
11212 { 0x00000001, 0, "Flatten" },
11213 { 0x00000002, 1, "WindowFunc" },
11214 { 0x00000004, 1, "GroupByOrder" },
11215 { 0x00000008, 1, "FactorOutConst" },
11216 { 0x00000010, 1, "DistinctOpt" },
11217 { 0x00000020, 1, "CoverIdxScan" },
11218 { 0x00000040, 1, "OrderByIdxJoin" },
11219 { 0x00000080, 1, "Transitive" },
11220 { 0x00000100, 1, "OmitNoopJoin" },
11221 { 0x00000200, 1, "CountOfView" },
11222 { 0x00000400, 1, "CurosrHints" },
11223 { 0x00000800, 1, "Stat4" },
11224 { 0x00001000, 1, "PushDown" },
11225 { 0x00002000, 1, "SimplifyJoin" },
11226 { 0x00004000, 1, "SkipScan" },
11227 { 0x00008000, 1, "PropagateConst" },
11228 { 0x00010000, 1, "MinMaxOpt" },
11229 { 0x00020000, 1, "SeekScan" },
11230 { 0x00040000, 1, "OmitOrderBy" },
11231 { 0x00080000, 1, "BloomFilter" },
11232 { 0x00100000, 1, "BloomPulldown" },
11233 { 0x00200000, 1, "BalancedMerge" },
11234 { 0x00400000, 1, "ReleaseReg" },
11235 { 0x00800000, 1, "FlttnUnionAll" },
11236 { 0x01000000, 1, "IndexedEXpr" },
11237 { 0x02000000, 1, "Coroutines" },
11238 { 0x04000000, 1, "NullUnusedCols" },
11239 { 0x08000000, 1, "OnePass" },
11240 { 0x10000000, 1, "OrderBySubq" },
11241 { 0xffffffff, 0, "All" },
11243 unsigned int curOpt
;
11244 unsigned int newOpt
;
11246 sqlite3_test_control(SQLITE_TESTCTRL_GETOPT
, p
->db
, &curOpt
);
11248 for(ii
=2; ii
<nArg
; ii
++){
11249 const char *z
= azArg
[ii
];
11251 const char *zLabel
= 0;
11252 if( (z
[0]=='+'|| z
[0]=='-') && !IsDigit(z
[1]) ){
11255 }else if( !IsDigit(z
[0]) && z
[0]!=0 && !IsDigit(z
[1]) ){
11259 newOpt
= (unsigned int)strtol(z
,0,0);
11263 for(jj
=0; jj
<ArraySize(aLabel
); jj
++){
11264 if( sqlite3_stricmp(zLabel
, aLabel
[jj
].zLabel
)==0 ) break;
11266 if( jj
>=ArraySize(aLabel
) ){
11267 eputf("Error: no such optimization: \"%s\"\n", zLabel
);
11268 eputz("Should be one of:");
11269 for(jj
=0; jj
<ArraySize(aLabel
); jj
++){
11270 eputf(" %s", aLabel
[jj
].zLabel
);
11274 goto meta_command_exit
;
11276 if( useLabel
=='+' ){
11277 newOpt
&= ~aLabel
[jj
].mask
;
11279 newOpt
|= aLabel
[jj
].mask
;
11283 if( curOpt
!=newOpt
){
11284 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS
,p
->db
,newOpt
);
11285 }else if( nArg
<3 ){
11290 }else if( newOpt
==0xffffffff ){
11294 for(jj
=0; jj
<ArraySize(aLabel
); jj
++){
11295 unsigned int m
= aLabel
[jj
].mask
;
11296 if( !aLabel
[jj
].bDsply
) continue;
11297 if( (curOpt
&m
)!=(newOpt
&m
) ){
11298 oputf("%c%s\n", (newOpt
& m
)==0 ? '+' : '-',
11299 aLabel
[jj
].zLabel
);
11307 /* sqlite3_test_control(int, db, int) */
11308 case SQLITE_TESTCTRL_FK_NO_ACTION
:
11310 unsigned int opt
= (unsigned int)strtol(azArg
[2], 0, 0);
11311 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
11316 /* sqlite3_test_control(int) */
11317 case SQLITE_TESTCTRL_PRNG_SAVE
:
11318 case SQLITE_TESTCTRL_PRNG_RESTORE
:
11319 case SQLITE_TESTCTRL_BYTEORDER
:
11321 rc2
= sqlite3_test_control(testctrl
);
11322 isOk
= testctrl
==SQLITE_TESTCTRL_BYTEORDER
? 1 : 3;
11326 /* sqlite3_test_control(int, uint) */
11327 case SQLITE_TESTCTRL_PENDING_BYTE
:
11329 unsigned int opt
= (unsigned int)integerValue(azArg
[2]);
11330 rc2
= sqlite3_test_control(testctrl
, opt
);
11335 /* sqlite3_test_control(int, int, sqlite3*) */
11336 case SQLITE_TESTCTRL_PRNG_SEED
:
11337 if( nArg
==3 || nArg
==4 ){
11338 int ii
= (int)integerValue(azArg
[2]);
11340 if( ii
==0 && cli_strcmp(azArg
[2],"random")==0 ){
11341 sqlite3_randomness(sizeof(ii
),&ii
);
11342 sputf(stdout
, "-- random seed: %d\n", ii
);
11348 /* Make sure the schema has been loaded */
11349 sqlite3_table_column_metadata(db
, 0, "x", 0, 0, 0, 0, 0, 0);
11351 rc2
= sqlite3_test_control(testctrl
, ii
, db
);
11356 /* sqlite3_test_control(int, int) */
11357 case SQLITE_TESTCTRL_ASSERT
:
11358 case SQLITE_TESTCTRL_ALWAYS
:
11360 int opt
= booleanValue(azArg
[2]);
11361 rc2
= sqlite3_test_control(testctrl
, opt
);
11366 /* sqlite3_test_control(int, int) */
11367 case SQLITE_TESTCTRL_LOCALTIME_FAULT
:
11368 case SQLITE_TESTCTRL_NEVER_CORRUPT
:
11370 int opt
= booleanValue(azArg
[2]);
11371 rc2
= sqlite3_test_control(testctrl
, opt
);
11376 /* sqlite3_test_control(int, int) */
11377 case SQLITE_TESTCTRL_USELONGDOUBLE
: {
11380 if( cli_strcmp(azArg
[2],"default")==0 ){
11383 opt
= booleanValue(azArg
[2]);
11386 rc2
= sqlite3_test_control(testctrl
, opt
);
11391 /* sqlite3_test_control(sqlite3*) */
11392 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS
:
11393 rc2
= sqlite3_test_control(testctrl
, p
->db
);
11397 case SQLITE_TESTCTRL_IMPOSTER
:
11399 rc2
= sqlite3_test_control(testctrl
, p
->db
,
11401 integerValue(azArg
[3]),
11402 integerValue(azArg
[4]));
11407 case SQLITE_TESTCTRL_SEEK_COUNT
: {
11409 rc2
= sqlite3_test_control(testctrl
, p
->db
, &x
);
11410 oputf("%llu\n", x
);
11416 case SQLITE_TESTCTRL_PARSER_COVERAGE
: {
11418 sqlite3_test_control(testctrl
, p
->out
);
11424 #ifdef SQLITE_DEBUG
11425 case SQLITE_TESTCTRL_TUNE
: {
11427 int id
= (int)integerValue(azArg
[2]);
11428 int val
= (int)integerValue(azArg
[3]);
11429 sqlite3_test_control(testctrl
, id
, &val
);
11431 }else if( nArg
==3 ){
11432 int id
= (int)integerValue(azArg
[2]);
11433 sqlite3_test_control(testctrl
, -id
, &rc2
);
11435 }else if( nArg
==2 ){
11439 rc2
= sqlite3_test_control(testctrl
, -id
, &val
);
11440 if( rc2
!=SQLITE_OK
) break;
11441 if( id
>1 ) oputz(" ");
11442 oputf("%d: %d", id
, val
);
11445 if( id
>1 ) oputz("\n");
11451 case SQLITE_TESTCTRL_SORTER_MMAP
:
11453 int opt
= (unsigned int)integerValue(azArg
[2]);
11454 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
11458 case SQLITE_TESTCTRL_JSON_SELFCHECK
:
11463 rc2
= booleanValue(azArg
[2]);
11466 sqlite3_test_control(testctrl
, &rc2
);
11468 case SQLITE_TESTCTRL_FAULT_INSTALL
: {
11470 int bShowHelp
= nArg
<=2;
11472 for(kk
=2; kk
<nArg
; kk
++){
11473 const char *z
= azArg
[kk
];
11474 if( z
[0]=='-' && z
[1]=='-' ) z
++;
11475 if( cli_strcmp(z
,"off")==0 ){
11476 sqlite3_test_control(testctrl
, 0);
11477 }else if( cli_strcmp(z
,"on")==0 ){
11478 faultsim_state
.iCnt
= faultsim_state
.nSkip
;
11479 if( faultsim_state
.iErr
==0 ) faultsim_state
.iErr
= 1;
11480 faultsim_state
.nHit
= 0;
11481 sqlite3_test_control(testctrl
, faultsim_callback
);
11482 }else if( cli_strcmp(z
,"reset")==0 ){
11483 faultsim_state
.iCnt
= faultsim_state
.nSkip
;
11484 faultsim_state
.nHit
= 0;
11485 sqlite3_test_control(testctrl
, faultsim_callback
);
11486 }else if( cli_strcmp(z
,"status")==0 ){
11487 oputf("faultsim.iId: %d\n", faultsim_state
.iId
);
11488 oputf("faultsim.iErr: %d\n", faultsim_state
.iErr
);
11489 oputf("faultsim.iCnt: %d\n", faultsim_state
.iCnt
);
11490 oputf("faultsim.nHit: %d\n", faultsim_state
.nHit
);
11491 oputf("faultsim.iInterval: %d\n", faultsim_state
.iInterval
);
11492 oputf("faultsim.eVerbose: %d\n", faultsim_state
.eVerbose
);
11493 oputf("faultsim.nRepeat: %d\n", faultsim_state
.nRepeat
);
11494 oputf("faultsim.nSkip: %d\n", faultsim_state
.nSkip
);
11495 }else if( cli_strcmp(z
,"-v")==0 ){
11496 if( faultsim_state
.eVerbose
<2 ) faultsim_state
.eVerbose
++;
11497 }else if( cli_strcmp(z
,"-q")==0 ){
11498 if( faultsim_state
.eVerbose
>0 ) faultsim_state
.eVerbose
--;
11499 }else if( cli_strcmp(z
,"-id")==0 && kk
+1<nArg
){
11500 faultsim_state
.iId
= atoi(azArg
[++kk
]);
11501 }else if( cli_strcmp(z
,"-errcode")==0 && kk
+1<nArg
){
11502 faultsim_state
.iErr
= atoi(azArg
[++kk
]);
11503 }else if( cli_strcmp(z
,"-interval")==0 && kk
+1<nArg
){
11504 faultsim_state
.iInterval
= atoi(azArg
[++kk
]);
11505 }else if( cli_strcmp(z
,"-repeat")==0 && kk
+1<nArg
){
11506 faultsim_state
.nRepeat
= atoi(azArg
[++kk
]);
11507 }else if( cli_strcmp(z
,"-skip")==0 && kk
+1<nArg
){
11508 faultsim_state
.nSkip
= atoi(azArg
[++kk
]);
11509 }else if( cli_strcmp(z
,"-?")==0 || sqlite3_strglob("*help*",z
)==0){
11512 eputf("Unrecognized fault_install argument: \"%s\"\n",
11521 "Usage: .testctrl fault_install ARGS\n"
11522 "Possible arguments:\n"
11523 " off Disable faultsim\n"
11524 " on Activate faultsim\n"
11525 " reset Reset the trigger counter\n"
11526 " status Show current status\n"
11527 " -v Increase verbosity\n"
11528 " -q Decrease verbosity\n"
11529 " --errcode N When triggered, return N as error code\n"
11530 " --id ID Trigger only for the ID specified\n"
11531 " --interval N Trigger only after every N-th call\n"
11532 " --repeat N Turn off after N hits. 0 means never\n"
11533 " --skip N Skip the first N encounters\n"
11540 if( isOk
==0 && iCtrl
>=0 ){
11541 oputf("Usage: .testctrl %s %s\n", zCmd
,aCtrl
[iCtrl
].zUsage
);
11543 }else if( isOk
==1 ){
11544 oputf("%d\n", rc2
);
11545 }else if( isOk
==2 ){
11546 oputf("0x%08x\n", rc2
);
11549 #endif /* !defined(SQLITE_UNTESTABLE) */
11551 if( c
=='t' && n
>4 && cli_strncmp(azArg
[0], "timeout", n
)==0 ){
11553 sqlite3_busy_timeout(p
->db
, nArg
>=2 ? (int)integerValue(azArg
[1]) : 0);
11556 if( c
=='t' && n
>=5 && cli_strncmp(azArg
[0], "timer", n
)==0 ){
11558 enableTimer
= booleanValue(azArg
[1]);
11559 if( enableTimer
&& !HAS_TIMER
){
11560 eputz("Error: timer not available on this system.\n");
11564 eputz("Usage: .timer on|off\n");
11569 #ifndef SQLITE_OMIT_TRACE
11570 if( c
=='t' && cli_strncmp(azArg
[0], "trace", n
)==0 ){
11574 for(jj
=1; jj
<nArg
; jj
++){
11575 const char *z
= azArg
[jj
];
11577 if( optionMatch(z
, "expanded") ){
11578 p
->eTraceType
= SHELL_TRACE_EXPANDED
;
11580 #ifdef SQLITE_ENABLE_NORMALIZE
11581 else if( optionMatch(z
, "normalized") ){
11582 p
->eTraceType
= SHELL_TRACE_NORMALIZED
;
11585 else if( optionMatch(z
, "plain") ){
11586 p
->eTraceType
= SHELL_TRACE_PLAIN
;
11588 else if( optionMatch(z
, "profile") ){
11589 mType
|= SQLITE_TRACE_PROFILE
;
11591 else if( optionMatch(z
, "row") ){
11592 mType
|= SQLITE_TRACE_ROW
;
11594 else if( optionMatch(z
, "stmt") ){
11595 mType
|= SQLITE_TRACE_STMT
;
11597 else if( optionMatch(z
, "close") ){
11598 mType
|= SQLITE_TRACE_CLOSE
;
11601 eputf("Unknown option \"%s\" on \".trace\"\n", z
);
11603 goto meta_command_exit
;
11606 output_file_close(p
->traceOut
);
11607 p
->traceOut
= output_file_open(z
, 0);
11610 if( p
->traceOut
==0 ){
11611 sqlite3_trace_v2(p
->db
, 0, 0, 0);
11613 if( mType
==0 ) mType
= SQLITE_TRACE_STMT
;
11614 sqlite3_trace_v2(p
->db
, mType
, sql_trace_callback
, p
);
11617 #endif /* !defined(SQLITE_OMIT_TRACE) */
11619 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11620 if( c
=='u' && cli_strncmp(azArg
[0], "unmodule", n
)==0 ){
11625 eputz("Usage: .unmodule [--allexcept] NAME ...\n");
11627 goto meta_command_exit
;
11631 if( zOpt
[0]=='-' && zOpt
[1]=='-' && zOpt
[2]!=0 ) zOpt
++;
11632 lenOpt
= (int)strlen(zOpt
);
11633 if( lenOpt
>=3 && cli_strncmp(zOpt
, "-allexcept",lenOpt
)==0 ){
11634 assert( azArg
[nArg
]==0 );
11635 sqlite3_drop_modules(p
->db
, nArg
>2 ? (const char**)(azArg
+2) : 0);
11637 for(ii
=1; ii
<nArg
; ii
++){
11638 sqlite3_create_module(p
->db
, azArg
[ii
], 0, 0);
11644 #if SQLITE_USER_AUTHENTICATION
11645 if( c
=='u' && cli_strncmp(azArg
[0], "user", n
)==0 ){
11647 eputz("Usage: .user SUBCOMMAND ...\n");
11649 goto meta_command_exit
;
11652 if( cli_strcmp(azArg
[1],"login")==0 ){
11654 eputz("Usage: .user login USER PASSWORD\n");
11656 goto meta_command_exit
;
11658 rc
= sqlite3_user_authenticate(p
->db
, azArg
[2], azArg
[3],
11659 strlen30(azArg
[3]));
11661 eputf("Authentication failed for user %s\n", azArg
[2]);
11664 }else if( cli_strcmp(azArg
[1],"add")==0 ){
11666 eputz("Usage: .user add USER PASSWORD ISADMIN\n");
11668 goto meta_command_exit
;
11670 rc
= sqlite3_user_add(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
11671 booleanValue(azArg
[4]));
11673 eputf("User-Add failed: %d\n", rc
);
11676 }else if( cli_strcmp(azArg
[1],"edit")==0 ){
11678 eputz("Usage: .user edit USER PASSWORD ISADMIN\n");
11680 goto meta_command_exit
;
11682 rc
= sqlite3_user_change(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
11683 booleanValue(azArg
[4]));
11685 eputf("User-Edit failed: %d\n", rc
);
11688 }else if( cli_strcmp(azArg
[1],"delete")==0 ){
11690 eputz("Usage: .user delete USER\n");
11692 goto meta_command_exit
;
11694 rc
= sqlite3_user_delete(p
->db
, azArg
[2]);
11696 eputf("User-Delete failed: %d\n", rc
);
11700 eputz("Usage: .user login|add|edit|delete ...\n");
11702 goto meta_command_exit
;
11705 #endif /* SQLITE_USER_AUTHENTICATION */
11707 if( c
=='v' && cli_strncmp(azArg
[0], "version", n
)==0 ){
11708 char *zPtrSz
= sizeof(void*)==8 ? "64-bit" : "32-bit";
11709 oputf("SQLite %s %s\n" /*extra-version-info*/,
11710 sqlite3_libversion(), sqlite3_sourceid());
11711 #if SQLITE_HAVE_ZLIB
11712 oputf("zlib version %s\n", zlibVersion());
11714 #define CTIMEOPT_VAL_(opt) #opt
11715 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
11716 #if defined(__clang__) && defined(__clang_major__)
11717 oputf("clang-" CTIMEOPT_VAL(__clang_major__
) "."
11718 CTIMEOPT_VAL(__clang_minor__
) "."
11719 CTIMEOPT_VAL(__clang_patchlevel__
) " (%s)\n", zPtrSz
);
11720 #elif defined(_MSC_VER)
11721 oputf("msvc-" CTIMEOPT_VAL(_MSC_VER
) " (%s)\n", zPtrSz
);
11722 #elif defined(__GNUC__) && defined(__VERSION__)
11723 oputf("gcc-" __VERSION__
" (%s)\n", zPtrSz
);
11727 if( c
=='v' && cli_strncmp(azArg
[0], "vfsinfo", n
)==0 ){
11728 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
11729 sqlite3_vfs
*pVfs
= 0;
11731 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
11733 oputf("vfs.zName = \"%s\"\n", pVfs
->zName
);
11734 oputf("vfs.iVersion = %d\n", pVfs
->iVersion
);
11735 oputf("vfs.szOsFile = %d\n", pVfs
->szOsFile
);
11736 oputf("vfs.mxPathname = %d\n", pVfs
->mxPathname
);
11741 if( c
=='v' && cli_strncmp(azArg
[0], "vfslist", n
)==0 ){
11743 sqlite3_vfs
*pCurrent
= 0;
11745 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_VFS_POINTER
, &pCurrent
);
11747 for(pVfs
=sqlite3_vfs_find(0); pVfs
; pVfs
=pVfs
->pNext
){
11748 oputf("vfs.zName = \"%s\"%s\n", pVfs
->zName
,
11749 pVfs
==pCurrent
? " <--- CURRENT" : "");
11750 oputf("vfs.iVersion = %d\n", pVfs
->iVersion
);
11751 oputf("vfs.szOsFile = %d\n", pVfs
->szOsFile
);
11752 oputf("vfs.mxPathname = %d\n", pVfs
->mxPathname
);
11754 oputz("-----------------------------------\n");
11759 if( c
=='v' && cli_strncmp(azArg
[0], "vfsname", n
)==0 ){
11760 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
11761 char *zVfsName
= 0;
11763 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFSNAME
, &zVfsName
);
11765 oputf("%s\n", zVfsName
);
11766 sqlite3_free(zVfsName
);
11771 if( c
=='w' && cli_strncmp(azArg
[0], "wheretrace", n
)==0 ){
11772 unsigned int x
= nArg
>=2? (unsigned int)integerValue(azArg
[1]) : 0xffffffff;
11773 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS
, 3, &x
);
11776 if( c
=='w' && cli_strncmp(azArg
[0], "width", n
)==0 ){
11778 assert( nArg
<=ArraySize(azArg
) );
11779 p
->nWidth
= nArg
-1;
11780 p
->colWidth
= realloc(p
->colWidth
, (p
->nWidth
+1)*sizeof(int)*2);
11781 if( p
->colWidth
==0 && p
->nWidth
>0 ) shell_out_of_memory();
11782 if( p
->nWidth
) p
->actualWidth
= &p
->colWidth
[p
->nWidth
];
11783 for(j
=1; j
<nArg
; j
++){
11784 p
->colWidth
[j
-1] = (int)integerValue(azArg
[j
]);
11789 eputf("Error: unknown command or invalid arguments: "
11790 " \"%s\". Enter \".help\" for help\n", azArg
[0]);
11797 if( p
->outCount
==0 ) output_reset(p
);
11799 p
->bSafeMode
= p
->bSafeModePersist
;
11803 /* Line scan result and intermediate states (supporting scan resumption)
11806 # define CHAR_BIT 8
11809 QSS_HasDark
= 1<<CHAR_BIT
, QSS_EndingSemi
= 2<<CHAR_BIT
,
11810 QSS_CharMask
= (1<<CHAR_BIT
)-1, QSS_ScanMask
= 3<<CHAR_BIT
,
11813 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
11814 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
11815 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
11816 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
11817 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
11820 ** Scan line for classification to guide shell's handling.
11821 ** The scan is resumable for subsequent lines when prior
11822 ** return values are passed as the 2nd argument.
11824 static QuickScanState
quickscan(char *zLine
, QuickScanState qss
,
11825 SCAN_TRACKER_REFTYPE pst
){
11827 char cWait
= (char)qss
; /* intentional narrowing loss */
11830 assert( cWait
==0 );
11831 while( (cin
= *zLine
++)!=0 ){
11838 while((cin
= *++zLine
)!=0 )
11843 qss
|= QSS_EndingSemi
;
11849 CONTINUE_PROMPT_AWAITS(pst
, "/*");
11850 qss
= QSS_SETV(qss
, cWait
);
11856 deliberate_fall_through
;
11857 case '`': case '\'': case '"':
11859 qss
= QSS_HasDark
| cWait
;
11860 CONTINUE_PROMPT_AWAITC(pst
, cin
);
11863 CONTINUE_PAREN_INCR(pst
, 1);
11866 CONTINUE_PAREN_INCR(pst
, -1);
11871 qss
= (qss
& ~QSS_EndingSemi
) | QSS_HasDark
;
11875 while( (cin
= *zLine
++)!=0 ){
11879 if( *zLine
!= '/' )
11883 CONTINUE_PROMPT_AWAITC(pst
, 0);
11884 qss
= QSS_SETV(qss
, 0);
11886 case '`': case '\'': case '"':
11888 /* Swallow doubled end-delimiter.*/
11892 deliberate_fall_through
;
11895 CONTINUE_PROMPT_AWAITC(pst
, 0);
11896 qss
= QSS_SETV(qss
, 0);
11898 default: assert(0);
11907 ** Return TRUE if the line typed in is an SQL command terminator other
11908 ** than a semi-colon. The SQL Server style "go" command is understood
11909 ** as is the Oracle "/".
11911 static int line_is_command_terminator(char *zLine
){
11912 while( IsSpace(zLine
[0]) ){ zLine
++; };
11913 if( zLine
[0]=='/' )
11914 zLine
+= 1; /* Oracle */
11915 else if ( ToLower(zLine
[0])=='g' && ToLower(zLine
[1])=='o' )
11916 zLine
+= 2; /* SQL Server */
11919 return quickscan(zLine
, QSS_Start
, 0)==QSS_Start
;
11923 ** The CLI needs a working sqlite3_complete() to work properly. So error
11924 ** out of the build if compiling with SQLITE_OMIT_COMPLETE.
11926 #ifdef SQLITE_OMIT_COMPLETE
11927 # error the CLI application is imcompatable with SQLITE_OMIT_COMPLETE.
11931 ** Return true if zSql is a complete SQL statement. Return false if it
11932 ** ends in the middle of a string literal or C-style comment.
11934 static int line_is_complete(char *zSql
, int nSql
){
11936 if( zSql
==0 ) return 1;
11939 rc
= sqlite3_complete(zSql
);
11945 ** This function is called after processing each line of SQL in the
11946 ** runOneSqlLine() function. Its purpose is to detect scenarios where
11947 ** defensive mode should be automatically turned off. Specifically, when
11949 ** 1. The first line of input is "PRAGMA foreign_keys=OFF;",
11950 ** 2. The second line of input is "BEGIN TRANSACTION;",
11951 ** 3. The database is empty, and
11952 ** 4. The shell is not running in --safe mode.
11954 ** The implementation uses the ShellState.eRestoreState to maintain state:
11956 ** 0: Have not seen any SQL.
11957 ** 1: Have seen "PRAGMA foreign_keys=OFF;".
11958 ** 2-6: Currently running .dump transaction. If the "2" bit is set,
11959 ** disable DEFENSIVE when done. If "4" is set, disable DQS_DDL.
11960 ** 7: Nothing left to do. This function becomes a no-op.
11962 static int doAutoDetectRestore(ShellState
*p
, const char *zSql
){
11963 int rc
= SQLITE_OK
;
11965 if( p
->eRestoreState
<7 ){
11966 switch( p
->eRestoreState
){
11968 const char *zExpect
= "PRAGMA foreign_keys=OFF;";
11969 assert( strlen(zExpect
)==24 );
11970 if( p
->bSafeMode
==0
11971 && strlen(zSql
)>=24
11972 && memcmp(zSql
, zExpect
, 25)==0
11974 p
->eRestoreState
= 1;
11976 p
->eRestoreState
= 7;
11983 const char *zExpect
= "BEGIN TRANSACTION;";
11984 assert( strlen(zExpect
)==18 );
11985 if( memcmp(zSql
, zExpect
, 19)==0 ){
11986 /* Now check if the database is empty. */
11987 const char *zQuery
= "SELECT 1 FROM sqlite_schema LIMIT 1";
11988 sqlite3_stmt
*pStmt
= 0;
11991 shellPrepare(p
->db
, &rc
, zQuery
, &pStmt
);
11992 if( rc
==SQLITE_OK
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
11995 shellFinalize(&rc
, pStmt
);
11997 if( bIsDump
&& rc
==SQLITE_OK
){
12000 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, -1, &bDefense
);
12001 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DQS_DDL
, -1, &bDqsDdl
);
12002 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, 0, 0);
12003 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DQS_DDL
, 1, 0);
12004 p
->eRestoreState
= (bDefense
? 2 : 0) + (bDqsDdl
? 4 : 0);
12006 p
->eRestoreState
= 7;
12012 if( sqlite3_get_autocommit(p
->db
) ){
12013 if( (p
->eRestoreState
& 2) ){
12014 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DEFENSIVE
, 1, 0);
12016 if( (p
->eRestoreState
& 4) ){
12017 sqlite3_db_config(p
->db
, SQLITE_DBCONFIG_DQS_DDL
, 0, 0);
12019 p
->eRestoreState
= 7;
12030 ** Run a single line of SQL. Return the number of errors.
12032 static int runOneSqlLine(ShellState
*p
, char *zSql
, FILE *in
, int startline
){
12037 if( ShellHasFlag(p
,SHFLG_Backslash
) ) resolve_backslashes(zSql
);
12038 if( p
->flgProgress
& SHELL_PROGRESS_RESET
) p
->nProgress
= 0;
12040 rc
= shell_exec(p
, zSql
, &zErrMsg
);
12042 if( rc
|| zErrMsg
){
12044 const char *zErrorTail
;
12045 const char *zErrorType
;
12047 zErrorType
= "Error";
12048 zErrorTail
= sqlite3_errmsg(p
->db
);
12049 }else if( cli_strncmp(zErrMsg
, "in prepare, ",12)==0 ){
12050 zErrorType
= "Parse error";
12051 zErrorTail
= &zErrMsg
[12];
12052 }else if( cli_strncmp(zErrMsg
, "stepping, ", 10)==0 ){
12053 zErrorType
= "Runtime error";
12054 zErrorTail
= &zErrMsg
[10];
12056 zErrorType
= "Error";
12057 zErrorTail
= zErrMsg
;
12059 if( in
!=0 || !stdin_is_interactive
){
12060 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
,
12061 "%s near line %d:", zErrorType
, startline
);
12063 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:", zErrorType
);
12065 eputf("%s %s\n", zPrefix
, zErrorTail
);
12066 sqlite3_free(zErrMsg
);
12069 }else if( ShellHasFlag(p
, SHFLG_CountChanges
) ){
12070 char zLineBuf
[2000];
12071 sqlite3_snprintf(sizeof(zLineBuf
), zLineBuf
,
12072 "changes: %lld total_changes: %lld",
12073 sqlite3_changes64(p
->db
), sqlite3_total_changes64(p
->db
));
12074 oputf("%s\n", zLineBuf
);
12077 if( doAutoDetectRestore(p
, zSql
) ) return 1;
12081 static void echo_group_input(ShellState
*p
, const char *zDo
){
12082 if( ShellHasFlag(p
, SHFLG_Echo
) ) oputf("%s\n", zDo
);
12085 #ifdef SQLITE_SHELL_FIDDLE
12087 ** Alternate one_input_line() impl for wasm mode. This is not in the primary
12088 ** impl because we need the global shellState and cannot access it from that
12089 ** function without moving lots of code around (creating a larger/messier diff).
12091 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
12092 /* Parse the next line from shellState.wasm.zInput. */
12093 const char *zBegin
= shellState
.wasm
.zPos
;
12094 const char *z
= zBegin
;
12098 UNUSED_PARAMETER(in
);
12099 UNUSED_PARAMETER(isContinuation
);
12103 while(*z
&& isspace(*z
)) ++z
;
12105 for(; *z
&& '\n'!=*z
; ++nZ
, ++z
){}
12106 if(nZ
>0 && '\r'==zBegin
[nZ
-1]){
12109 shellState
.wasm
.zPos
= z
;
12110 zLine
= realloc(zPrior
, nZ
+1);
12111 shell_check_oom(zLine
);
12112 memcpy(zLine
, zBegin
, nZ
);
12116 #endif /* SQLITE_SHELL_FIDDLE */
12119 ** Read input from *in and process it. If *in==0 then input
12120 ** is interactive - the user is typing it it. Otherwise, input
12121 ** is coming from a file or device. A prompt is issued and history
12122 ** is saved only if input is interactive. An interrupt signal will
12123 ** cause this routine to exit immediately, unless input is interactive.
12125 ** Return the number of errors.
12127 static int process_input(ShellState
*p
){
12128 char *zLine
= 0; /* A single input line */
12129 char *zSql
= 0; /* Accumulated SQL text */
12130 i64 nLine
; /* Length of current line */
12131 i64 nSql
= 0; /* Bytes of zSql[] used */
12132 i64 nAlloc
= 0; /* Allocated zSql[] space */
12133 int rc
; /* Error code */
12134 int errCnt
= 0; /* Number of errors seen */
12135 i64 startline
= 0; /* Line number for start of current input */
12136 QuickScanState qss
= QSS_Start
; /* Accumulated line status (so far) */
12138 if( p
->inputNesting
==MAX_INPUT_NESTING
){
12139 /* This will be more informative in a later version. */
12140 eputf("Input nesting limit (%d) reached at line %d."
12141 " Check recursion.\n", MAX_INPUT_NESTING
, p
->lineno
);
12146 CONTINUE_PROMPT_RESET
;
12147 while( errCnt
==0 || !bail_on_error
|| (p
->in
==0 && stdin_is_interactive
) ){
12149 zLine
= one_input_line(p
->in
, zLine
, nSql
>0);
12152 if( p
->in
==0 && stdin_is_interactive
) oputz("\n");
12155 if( seenInterrupt
){
12156 if( p
->in
!=0 ) break;
12160 if( QSS_INPLAIN(qss
)
12161 && line_is_command_terminator(zLine
)
12162 && line_is_complete(zSql
, nSql
) ){
12163 memcpy(zLine
,";",2);
12165 qss
= quickscan(zLine
, qss
, CONTINUE_PROMPT_PSTATE
);
12166 if( QSS_PLAINWHITE(qss
) && nSql
==0 ){
12167 /* Just swallow single-line whitespace */
12168 echo_group_input(p
, zLine
);
12172 if( zLine
&& (zLine
[0]=='.' || zLine
[0]=='#') && nSql
==0 ){
12173 CONTINUE_PROMPT_RESET
;
12174 echo_group_input(p
, zLine
);
12175 if( zLine
[0]=='.' ){
12176 rc
= do_meta_command(zLine
, p
);
12177 if( rc
==2 ){ /* exit requested */
12186 /* No single-line dispositions remain; accumulate line(s). */
12187 nLine
= strlen(zLine
);
12188 if( nSql
+nLine
+2>=nAlloc
){
12189 /* Grow buffer by half-again increments when big. */
12190 nAlloc
= nSql
+(nSql
>>1)+nLine
+100;
12191 zSql
= realloc(zSql
, nAlloc
);
12192 shell_check_oom(zSql
);
12196 for(i
=0; zLine
[i
] && IsSpace(zLine
[i
]); i
++){}
12197 assert( nAlloc
>0 && zSql
!=0 );
12198 memcpy(zSql
, zLine
+i
, nLine
+1-i
);
12199 startline
= p
->lineno
;
12202 zSql
[nSql
++] = '\n';
12203 memcpy(zSql
+nSql
, zLine
, nLine
+1);
12206 if( nSql
&& QSS_SEMITERM(qss
) && sqlite3_complete(zSql
) ){
12207 echo_group_input(p
, zSql
);
12208 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
12209 CONTINUE_PROMPT_RESET
;
12217 p
->bSafeMode
= p
->bSafeModePersist
;
12219 }else if( nSql
&& QSS_PLAINWHITE(qss
) ){
12220 echo_group_input(p
, zSql
);
12226 /* This may be incomplete. Let the SQL parser deal with that. */
12227 echo_group_input(p
, zSql
);
12228 errCnt
+= runOneSqlLine(p
, zSql
, p
->in
, startline
);
12229 CONTINUE_PROMPT_RESET
;
12238 ** Return a pathname which is the user's home directory. A
12239 ** 0 return indicates an error of some kind.
12241 static char *find_home_dir(int clearFlag
){
12242 static char *home_dir
= NULL
;
12248 if( home_dir
) return home_dir
;
12250 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
12251 && !defined(__RTP__) && !defined(_WRS_KERNEL) && !defined(SQLITE_WASI)
12253 struct passwd
*pwent
;
12254 uid_t uid
= getuid();
12255 if( (pwent
=getpwuid(uid
)) != NULL
) {
12256 home_dir
= pwent
->pw_dir
;
12261 #if defined(_WIN32_WCE)
12262 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
12267 #if defined(_WIN32) || defined(WIN32)
12269 home_dir
= getenv("USERPROFILE");
12274 home_dir
= getenv("HOME");
12277 #if defined(_WIN32) || defined(WIN32)
12279 char *zDrive
, *zPath
;
12281 zDrive
= getenv("HOMEDRIVE");
12282 zPath
= getenv("HOMEPATH");
12283 if( zDrive
&& zPath
){
12284 n
= strlen30(zDrive
) + strlen30(zPath
) + 1;
12285 home_dir
= malloc( n
);
12286 if( home_dir
==0 ) return 0;
12287 sqlite3_snprintf(n
, home_dir
, "%s%s", zDrive
, zPath
);
12294 #endif /* !_WIN32_WCE */
12297 i64 n
= strlen(home_dir
) + 1;
12298 char *z
= malloc( n
);
12299 if( z
) memcpy(z
, home_dir
, n
);
12307 ** On non-Windows platforms, look for $XDG_CONFIG_HOME.
12308 ** If ${XDG_CONFIG_HOME}/sqlite3/sqliterc is found, return
12309 ** the path to it. If there is no $(XDG_CONFIG_HOME) then
12310 ** look for $(HOME)/.config/sqlite3/sqliterc and if found
12311 ** return that. If none of these are found, return 0.
12313 ** The string returned is obtained from sqlite3_malloc() and
12314 ** should be freed by the caller.
12316 static char *find_xdg_config(void){
12317 #if defined(_WIN32) || defined(WIN32) || defined(_WIN32_WCE) \
12318 || defined(__RTP__) || defined(_WRS_KERNEL)
12322 const char *zXdgHome
;
12324 zXdgHome
= getenv("XDG_CONFIG_HOME");
12326 const char *zHome
= getenv("HOME");
12327 if( zHome
==0 ) return 0;
12328 zConfig
= sqlite3_mprintf("%s/.config/sqlite3/sqliterc", zHome
);
12330 zConfig
= sqlite3_mprintf("%s/sqlite3/sqliterc", zXdgHome
);
12332 shell_check_oom(zConfig
);
12333 if( access(zConfig
,0)!=0 ){
12334 sqlite3_free(zConfig
);
12342 ** Read input from the file given by sqliterc_override. Or if that
12343 ** parameter is NULL, take input from the first of find_xdg_config()
12344 ** or ~/.sqliterc which is found.
12346 ** Returns the number of errors.
12348 static void process_sqliterc(
12349 ShellState
*p
, /* Configuration data */
12350 const char *sqliterc_override
/* Name of config file. NULL to use default */
12352 char *home_dir
= NULL
;
12353 const char *sqliterc
= sqliterc_override
;
12355 FILE *inSaved
= p
->in
;
12356 int savedLineno
= p
->lineno
;
12358 if( sqliterc
== NULL
){
12359 sqliterc
= zBuf
= find_xdg_config();
12361 if( sqliterc
== NULL
){
12362 home_dir
= find_home_dir(0);
12364 eputz("-- warning: cannot find home directory;"
12365 " cannot read ~/.sqliterc\n");
12368 zBuf
= sqlite3_mprintf("%s/.sqliterc",home_dir
);
12369 shell_check_oom(zBuf
);
12372 p
->in
= fopen(sqliterc
,"rb");
12374 if( stdin_is_interactive
){
12375 eputf("-- Loading resources from %s\n", sqliterc
);
12377 if( process_input(p
) && bail_on_error
) exit(1);
12379 }else if( sqliterc_override
!=0 ){
12380 eputf("cannot open: \"%s\"\n", sqliterc
);
12381 if( bail_on_error
) exit(1);
12384 p
->lineno
= savedLineno
;
12385 sqlite3_free(zBuf
);
12389 ** Show available command line options
12391 static const char zOptions
[] =
12392 " -- treat no subsequent arguments as options\n"
12393 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12394 " -A ARGS... run \".archive ARGS\" and exit\n"
12396 " -append append the database to the end of the file\n"
12397 " -ascii set output mode to 'ascii'\n"
12398 " -bail stop after hitting an error\n"
12399 " -batch force batch I/O\n"
12400 " -box set output mode to 'box'\n"
12401 " -column set output mode to 'column'\n"
12402 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
12403 " -csv set output mode to 'csv'\n"
12404 #if !defined(SQLITE_OMIT_DESERIALIZE)
12405 " -deserialize open the database using sqlite3_deserialize()\n"
12407 " -echo print inputs before execution\n"
12408 " -init FILENAME read/process named file\n"
12409 " -[no]header turn headers on or off\n"
12410 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12411 " -heap SIZE Size of heap for memsys3 or memsys5\n"
12413 " -help show this message\n"
12414 " -html set output mode to HTML\n"
12415 " -interactive force interactive I/O\n"
12416 " -json set output mode to 'json'\n"
12417 " -line set output mode to 'line'\n"
12418 " -list set output mode to 'list'\n"
12419 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
12420 " -markdown set output mode to 'markdown'\n"
12421 #if !defined(SQLITE_OMIT_DESERIALIZE)
12422 " -maxsize N maximum size for a --deserialize database\n"
12424 " -memtrace trace all memory allocations and deallocations\n"
12425 " -mmap N default mmap size set to N\n"
12426 #ifdef SQLITE_ENABLE_MULTIPLEX
12427 " -multiplex enable the multiplexor VFS\n"
12429 " -newline SEP set output row separator. Default: '\\n'\n"
12430 " -nofollow refuse to open symbolic links to database files\n"
12431 " -nonce STRING set the safe-mode escape nonce\n"
12432 " -no-rowid-in-view Disable rowid-in-view using sqlite3_config()\n"
12433 " -nullvalue TEXT set text string for NULL values. Default ''\n"
12434 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
12435 " -pcachetrace trace all page cache operations\n"
12436 " -quote set output mode to 'quote'\n"
12437 " -readonly open the database read-only\n"
12438 " -safe enable safe-mode\n"
12439 " -separator SEP set output column separator. Default: '|'\n"
12440 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
12441 " -sorterref SIZE sorter references threshold size\n"
12443 " -stats print memory stats before each finalize\n"
12444 " -table set output mode to 'table'\n"
12445 " -tabs set output mode to 'tabs'\n"
12446 " -unsafe-testing allow unsafe commands and modes for testing\n"
12447 " -version show SQLite version\n"
12448 " -vfs NAME use NAME as the default VFS\n"
12449 " -vfstrace enable tracing of all VFS calls\n"
12450 #ifdef SQLITE_HAVE_ZLIB
12451 " -zip open the file as a ZIP Archive\n"
12454 static void usage(int showDetail
){
12455 eputf("Usage: %s [OPTIONS] [FILENAME [SQL]]\n"
12456 "FILENAME is the name of an SQLite database. A new database is created\n"
12457 "if the file does not previously exist. Defaults to :memory:.\n", Argv0
);
12459 eputf("OPTIONS include:\n%s", zOptions
);
12461 eputz("Use the -help option for additional information\n");
12467 ** Internal check: Verify that the SQLite is uninitialized. Print a
12468 ** error message if it is initialized.
12470 static void verify_uninitialized(void){
12471 if( sqlite3_config(-1)==SQLITE_MISUSE
){
12472 sputz(stdout
, "WARNING: attempt to configure SQLite after"
12473 " initialization.\n");
12478 ** Initialize the state information in data
12480 static void main_init(ShellState
*data
) {
12481 memset(data
, 0, sizeof(*data
));
12482 data
->normalMode
= data
->cMode
= data
->mode
= MODE_List
;
12483 data
->autoExplain
= 1;
12484 data
->pAuxDb
= &data
->aAuxDb
[0];
12485 memcpy(data
->colSeparator
,SEP_Column
, 2);
12486 memcpy(data
->rowSeparator
,SEP_Row
, 2);
12487 data
->showHeader
= 0;
12488 data
->shellFlgs
= SHFLG_Lookaside
;
12489 sqlite3_config(SQLITE_CONFIG_LOG
, shellLog
, data
);
12490 #if !defined(SQLITE_SHELL_FIDDLE)
12491 verify_uninitialized();
12493 sqlite3_config(SQLITE_CONFIG_URI
, 1);
12494 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
12495 sqlite3_snprintf(sizeof(mainPrompt
), mainPrompt
,"sqlite> ");
12496 sqlite3_snprintf(sizeof(continuePrompt
), continuePrompt
," ...> ");
12500 ** Output text to the console in a font that attracts extra attention.
12502 #if defined(_WIN32) || defined(WIN32)
12503 static void printBold(const char *zText
){
12504 #if !SQLITE_OS_WINRT
12505 HANDLE out
= GetStdHandle(STD_OUTPUT_HANDLE
);
12506 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo
;
12507 GetConsoleScreenBufferInfo(out
, &defaultScreenInfo
);
12508 SetConsoleTextAttribute(out
,
12509 FOREGROUND_RED
|FOREGROUND_INTENSITY
12512 sputz(stdout
, zText
);
12513 #if !SQLITE_OS_WINRT
12514 SetConsoleTextAttribute(out
, defaultScreenInfo
.wAttributes
);
12518 static void printBold(const char *zText
){
12519 sputf(stdout
, "\033[1m%s\033[0m", zText
);
12524 ** Get the argument to an --option. Throw an error and die if no argument
12527 static char *cmdline_option_value(int argc
, char **argv
, int i
){
12529 eputf("%s: Error: missing argument to %s\n", argv
[0], argv
[argc
-1]);
12535 static void sayAbnormalExit(void){
12536 if( seenInterrupt
) eputz("Program interrupted.\n");
12539 #ifndef SQLITE_SHELL_IS_UTF8
12540 # if (defined(_WIN32) || defined(WIN32)) \
12541 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
12542 # define SQLITE_SHELL_IS_UTF8 (0)
12544 # define SQLITE_SHELL_IS_UTF8 (1)
12548 #ifdef SQLITE_SHELL_FIDDLE
12549 # define main fiddle_main
12552 #if SQLITE_SHELL_IS_UTF8
12553 int SQLITE_CDECL
main(int argc
, char **argv
){
12555 int SQLITE_CDECL
wmain(int argc
, wchar_t **wargv
){
12558 #ifdef SQLITE_DEBUG
12559 sqlite3_int64 mem_main_enter
= 0;
12562 #ifdef SQLITE_SHELL_FIDDLE
12563 # define data shellState
12566 StreamsAreConsole consStreams
= SAC_NoConsole
;
12568 const char *zInitFile
= 0;
12571 int warnInmemoryDb
= 0;
12574 int nOptsEnd
= argc
;
12575 int bEnableVfstrace
= 0;
12577 const char *zVfs
= 0; /* Value of -vfs command-line option */
12578 #if !SQLITE_SHELL_IS_UTF8
12579 char **argvToFree
= 0;
12580 int argcToFree
= 0;
12582 setvbuf(stderr
, 0, _IONBF
, 0); /* Make sure stderr is unbuffered */
12584 #ifdef SQLITE_SHELL_FIDDLE
12585 stdin_is_interactive
= 0;
12586 stdout_is_console
= 1;
12587 data
.wasm
.zDefaultDbName
= "/fiddle.sqlite3";
12589 consStreams
= consoleClassifySetup(stdin
, stdout
, stderr
);
12590 stdin_is_interactive
= (consStreams
& SAC_InConsole
)!=0;
12591 stdout_is_console
= (consStreams
& SAC_OutConsole
)!=0;
12592 atexit(consoleRestore
);
12594 atexit(sayAbnormalExit
);
12595 #ifdef SQLITE_DEBUG
12596 mem_main_enter
= sqlite3_memory_used();
12598 #if !defined(_WIN32_WCE)
12599 if( getenv("SQLITE_DEBUG_BREAK") ){
12600 if( isatty(0) && isatty(2) ){
12601 eputf("attach debugger to process %d and press any key to continue.\n",
12605 #if defined(_WIN32) || defined(WIN32)
12606 #if SQLITE_OS_WINRT
12611 #elif defined(SIGTRAP)
12617 /* Register a valid signal handler early, before much else is done. */
12619 signal(SIGINT
, interrupt_handler
);
12620 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12621 if( !SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
) ){
12622 eputz("No ^C handler.\n");
12626 #if USE_SYSTEM_SQLITE+0!=1
12627 if( cli_strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID
,60)!=0 ){
12628 eputf("SQLite header and source version mismatch\n%s\n%s\n",
12629 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
12635 /* On Windows, we must translate command-line arguments into UTF-8.
12636 ** The SQLite memory allocator subsystem has to be enabled in order to
12637 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
12638 ** subsequent sqlite3_config() calls will work. So copy all results into
12639 ** memory that does not come from the SQLite memory allocator.
12641 #if !SQLITE_SHELL_IS_UTF8
12642 sqlite3_initialize();
12643 argvToFree
= malloc(sizeof(argv
[0])*argc
*2);
12644 shell_check_oom(argvToFree
);
12646 argv
= argvToFree
+ argc
;
12647 for(i
=0; i
<argc
; i
++){
12648 char *z
= sqlite3_win32_unicode_to_utf8(wargv
[i
]);
12650 shell_check_oom(z
);
12652 argv
[i
] = malloc( n
+1 );
12653 shell_check_oom(argv
[i
]);
12654 memcpy(argv
[i
], z
, n
+1);
12655 argvToFree
[i
] = argv
[i
];
12658 sqlite3_shutdown();
12661 assert( argc
>=1 && argv
&& argv
[0] );
12664 #ifdef SQLITE_SHELL_DBNAME_PROC
12666 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
12667 ** of a C-function that will provide the name of the database file. Use
12668 ** this compile-time option to embed this shell program in larger
12669 ** applications. */
12670 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
12671 SQLITE_SHELL_DBNAME_PROC(&data
.pAuxDb
->zDbFilename
);
12672 warnInmemoryDb
= 0;
12676 /* Do an initial pass through the command-line argument to locate
12677 ** the name of the database file, the name of the initialization file,
12678 ** the size of the alternative malloc heap, options affecting commands
12679 ** or SQL run from the command line, and the first command to execute.
12681 #ifndef SQLITE_SHELL_FIDDLE
12682 verify_uninitialized();
12684 for(i
=1; i
<argc
; i
++){
12687 if( z
[0]!='-' || i
>nOptsEnd
){
12688 if( data
.aAuxDb
->zDbFilename
==0 ){
12689 data
.aAuxDb
->zDbFilename
= z
;
12691 /* Excess arguments are interpreted as SQL (or dot-commands) and
12692 ** mean that nothing is read from stdin */
12695 azCmd
= realloc(azCmd
, sizeof(azCmd
[0])*nCmd
);
12696 shell_check_oom(azCmd
);
12701 if( z
[1]=='-' ) z
++;
12702 if( cli_strcmp(z
, "-")==0 ){
12705 }else if( cli_strcmp(z
,"-separator")==0
12706 || cli_strcmp(z
,"-nullvalue")==0
12707 || cli_strcmp(z
,"-newline")==0
12708 || cli_strcmp(z
,"-cmd")==0
12710 (void)cmdline_option_value(argc
, argv
, ++i
);
12711 }else if( cli_strcmp(z
,"-init")==0 ){
12712 zInitFile
= cmdline_option_value(argc
, argv
, ++i
);
12713 }else if( cli_strcmp(z
,"-interactive")==0 ){
12714 }else if( cli_strcmp(z
,"-batch")==0 ){
12715 /* Need to check for batch mode here to so we can avoid printing
12716 ** informational messages (like from process_sqliterc) before
12717 ** we do the actual processing of arguments later in a second pass.
12719 stdin_is_interactive
= 0;
12720 }else if( cli_strcmp(z
,"-utf8")==0 ){
12721 }else if( cli_strcmp(z
,"-no-utf8")==0 ){
12722 }else if( cli_strcmp(z
,"-no-rowid-in-view")==0 ){
12724 sqlite3_config(SQLITE_CONFIG_ROWID_IN_VIEW
, &val
);
12726 }else if( cli_strcmp(z
,"-heap")==0 ){
12727 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
12729 sqlite3_int64 szHeap
;
12731 zSize
= cmdline_option_value(argc
, argv
, ++i
);
12732 szHeap
= integerValue(zSize
);
12733 if( szHeap
>0x7fff0000 ) szHeap
= 0x7fff0000;
12734 verify_uninitialized();
12735 sqlite3_config(SQLITE_CONFIG_HEAP
, malloc((int)szHeap
), (int)szHeap
, 64);
12737 (void)cmdline_option_value(argc
, argv
, ++i
);
12739 }else if( cli_strcmp(z
,"-pagecache")==0 ){
12740 sqlite3_int64 n
, sz
;
12741 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12742 if( sz
>70000 ) sz
= 70000;
12744 n
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12745 if( sz
>0 && n
>0 && 0xffffffffffffLL
/sz
<n
){
12746 n
= 0xffffffffffffLL
/sz
;
12748 verify_uninitialized();
12749 sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
12750 (n
>0 && sz
>0) ? malloc(n
*sz
) : 0, sz
, n
);
12751 data
.shellFlgs
|= SHFLG_Pagecache
;
12752 }else if( cli_strcmp(z
,"-lookaside")==0 ){
12754 sz
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12756 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12758 verify_uninitialized();
12759 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, n
);
12760 if( sz
*n
==0 ) data
.shellFlgs
&= ~SHFLG_Lookaside
;
12761 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
12763 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
12764 verify_uninitialized();
12766 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD
); break;
12767 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD
); break;
12768 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED
); break;
12770 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
12771 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs
,stderr
,1);
12772 bEnableVfstrace
= 1;
12773 #ifdef SQLITE_ENABLE_MULTIPLEX
12774 }else if( cli_strcmp(z
,"-multiplex")==0 ){
12775 extern int sqlite3_multiplex_initialize(const char*,int);
12776 sqlite3_multiplex_initialize(0, 1);
12778 }else if( cli_strcmp(z
,"-mmap")==0 ){
12779 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12780 verify_uninitialized();
12781 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE
, sz
, sz
);
12782 #if defined(SQLITE_ENABLE_SORTER_REFERENCES)
12783 }else if( cli_strcmp(z
,"-sorterref")==0 ){
12784 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
12785 verify_uninitialized();
12786 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE
, (int)sz
);
12788 }else if( cli_strcmp(z
,"-vfs")==0 ){
12789 zVfs
= cmdline_option_value(argc
, argv
, ++i
);
12790 #ifdef SQLITE_HAVE_ZLIB
12791 }else if( cli_strcmp(z
,"-zip")==0 ){
12792 data
.openMode
= SHELL_OPEN_ZIPFILE
;
12794 }else if( cli_strcmp(z
,"-append")==0 ){
12795 data
.openMode
= SHELL_OPEN_APPENDVFS
;
12796 #ifndef SQLITE_OMIT_DESERIALIZE
12797 }else if( cli_strcmp(z
,"-deserialize")==0 ){
12798 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
12799 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
12800 data
.szMax
= integerValue(argv
[++i
]);
12802 }else if( cli_strcmp(z
,"-readonly")==0 ){
12803 data
.openMode
= SHELL_OPEN_READONLY
;
12804 }else if( cli_strcmp(z
,"-nofollow")==0 ){
12805 data
.openFlags
= SQLITE_OPEN_NOFOLLOW
;
12806 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12807 }else if( cli_strncmp(z
, "-A",2)==0 ){
12808 /* All remaining command-line arguments are passed to the ".archive"
12809 ** command, so ignore them */
12812 }else if( cli_strcmp(z
, "-memtrace")==0 ){
12813 sqlite3MemTraceActivate(stderr
);
12814 }else if( cli_strcmp(z
, "-pcachetrace")==0 ){
12815 sqlite3PcacheTraceActivate(stderr
);
12816 }else if( cli_strcmp(z
,"-bail")==0 ){
12818 }else if( cli_strcmp(z
,"-nonce")==0 ){
12820 data
.zNonce
= strdup(cmdline_option_value(argc
, argv
, ++i
));
12821 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
12822 ShellSetFlag(&data
,SHFLG_TestingMode
);
12823 }else if( cli_strcmp(z
,"-safe")==0 ){
12824 /* no-op - catch this on the second pass */
12827 #ifndef SQLITE_SHELL_FIDDLE
12828 if( !bEnableVfstrace
) verify_uninitialized();
12832 #ifdef SQLITE_SHELL_INIT_PROC
12834 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
12835 ** of a C-function that will perform initialization actions on SQLite that
12836 ** occur just before or after sqlite3_initialize(). Use this compile-time
12837 ** option to embed this shell program in larger applications. */
12838 extern void SQLITE_SHELL_INIT_PROC(void);
12839 SQLITE_SHELL_INIT_PROC();
12842 /* All the sqlite3_config() calls have now been made. So it is safe
12843 ** to call sqlite3_initialize() and process any command line -vfs option. */
12844 sqlite3_initialize();
12848 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zVfs
);
12850 sqlite3_vfs_register(pVfs
, 1);
12852 eputf("no such VFS: \"%s\"\n", zVfs
);
12857 if( data
.pAuxDb
->zDbFilename
==0 ){
12858 #ifndef SQLITE_OMIT_MEMORYDB
12859 data
.pAuxDb
->zDbFilename
= ":memory:";
12860 warnInmemoryDb
= argc
==1;
12862 eputf("%s: Error: no database filename specified\n", Argv0
);
12867 #ifndef SQLITE_SHELL_FIDDLE
12868 sqlite3_appendvfs_init(0,0,0);
12871 /* Go ahead and open the database file if it already exists. If the
12872 ** file does not exist, delay opening it. This prevents empty database
12873 ** files from being created if a user mistypes the database name argument
12874 ** to the sqlite command-line tool.
12876 if( access(data
.pAuxDb
->zDbFilename
, 0)==0 ){
12880 /* Process the initialization file if there is one. If no -init option
12881 ** is given on the command line, look for a file named ~/.sqliterc and
12882 ** try to process it.
12884 process_sqliterc(&data
,zInitFile
);
12886 /* Make a second pass through the command-line argument and set
12887 ** options. This second pass is delayed until after the initialization
12888 ** file is processed so that the command-line arguments will override
12889 ** settings in the initialization file.
12891 for(i
=1; i
<argc
; i
++){
12893 if( z
[0]!='-' || i
>=nOptsEnd
) continue;
12894 if( z
[1]=='-' ){ z
++; }
12895 if( cli_strcmp(z
,"-init")==0 ){
12897 }else if( cli_strcmp(z
,"-html")==0 ){
12898 data
.mode
= MODE_Html
;
12899 }else if( cli_strcmp(z
,"-list")==0 ){
12900 data
.mode
= MODE_List
;
12901 }else if( cli_strcmp(z
,"-quote")==0 ){
12902 data
.mode
= MODE_Quote
;
12903 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
, SEP_Comma
);
12904 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
, SEP_Row
);
12905 }else if( cli_strcmp(z
,"-line")==0 ){
12906 data
.mode
= MODE_Line
;
12907 }else if( cli_strcmp(z
,"-column")==0 ){
12908 data
.mode
= MODE_Column
;
12909 }else if( cli_strcmp(z
,"-json")==0 ){
12910 data
.mode
= MODE_Json
;
12911 }else if( cli_strcmp(z
,"-markdown")==0 ){
12912 data
.mode
= MODE_Markdown
;
12913 }else if( cli_strcmp(z
,"-table")==0 ){
12914 data
.mode
= MODE_Table
;
12915 }else if( cli_strcmp(z
,"-box")==0 ){
12916 data
.mode
= MODE_Box
;
12917 }else if( cli_strcmp(z
,"-csv")==0 ){
12918 data
.mode
= MODE_Csv
;
12919 memcpy(data
.colSeparator
,",",2);
12920 #ifdef SQLITE_HAVE_ZLIB
12921 }else if( cli_strcmp(z
,"-zip")==0 ){
12922 data
.openMode
= SHELL_OPEN_ZIPFILE
;
12924 }else if( cli_strcmp(z
,"-append")==0 ){
12925 data
.openMode
= SHELL_OPEN_APPENDVFS
;
12926 #ifndef SQLITE_OMIT_DESERIALIZE
12927 }else if( cli_strcmp(z
,"-deserialize")==0 ){
12928 data
.openMode
= SHELL_OPEN_DESERIALIZE
;
12929 }else if( cli_strcmp(z
,"-maxsize")==0 && i
+1<argc
){
12930 data
.szMax
= integerValue(argv
[++i
]);
12932 }else if( cli_strcmp(z
,"-readonly")==0 ){
12933 data
.openMode
= SHELL_OPEN_READONLY
;
12934 }else if( cli_strcmp(z
,"-nofollow")==0 ){
12935 data
.openFlags
|= SQLITE_OPEN_NOFOLLOW
;
12936 }else if( cli_strcmp(z
,"-ascii")==0 ){
12937 data
.mode
= MODE_Ascii
;
12938 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Unit
);
12939 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Record
);
12940 }else if( cli_strcmp(z
,"-tabs")==0 ){
12941 data
.mode
= MODE_List
;
12942 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,SEP_Tab
);
12943 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,SEP_Row
);
12944 }else if( cli_strcmp(z
,"-separator")==0 ){
12945 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,
12946 "%s",cmdline_option_value(argc
,argv
,++i
));
12947 }else if( cli_strcmp(z
,"-newline")==0 ){
12948 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,
12949 "%s",cmdline_option_value(argc
,argv
,++i
));
12950 }else if( cli_strcmp(z
,"-nullvalue")==0 ){
12951 sqlite3_snprintf(sizeof(data
.nullValue
), data
.nullValue
,
12952 "%s",cmdline_option_value(argc
,argv
,++i
));
12953 }else if( cli_strcmp(z
,"-header")==0 ){
12954 data
.showHeader
= 1;
12955 ShellSetFlag(&data
, SHFLG_HeaderSet
);
12956 }else if( cli_strcmp(z
,"-noheader")==0 ){
12957 data
.showHeader
= 0;
12958 ShellSetFlag(&data
, SHFLG_HeaderSet
);
12959 }else if( cli_strcmp(z
,"-echo")==0 ){
12960 ShellSetFlag(&data
, SHFLG_Echo
);
12961 }else if( cli_strcmp(z
,"-eqp")==0 ){
12962 data
.autoEQP
= AUTOEQP_on
;
12963 }else if( cli_strcmp(z
,"-eqpfull")==0 ){
12964 data
.autoEQP
= AUTOEQP_full
;
12965 }else if( cli_strcmp(z
,"-stats")==0 ){
12967 }else if( cli_strcmp(z
,"-scanstats")==0 ){
12968 data
.scanstatsOn
= 1;
12969 }else if( cli_strcmp(z
,"-backslash")==0 ){
12970 /* Undocumented command-line option: -backslash
12971 ** Causes C-style backslash escapes to be evaluated in SQL statements
12972 ** prior to sending the SQL into SQLite. Useful for injecting
12973 ** crazy bytes in the middle of SQL statements for testing and debugging.
12975 ShellSetFlag(&data
, SHFLG_Backslash
);
12976 }else if( cli_strcmp(z
,"-bail")==0 ){
12977 /* No-op. The bail_on_error flag should already be set. */
12978 }else if( cli_strcmp(z
,"-version")==0 ){
12979 sputf(stdout
, "%s %s (%d-bit)\n",
12980 sqlite3_libversion(), sqlite3_sourceid(), 8*(int)sizeof(char*));
12982 }else if( cli_strcmp(z
,"-interactive")==0 ){
12983 /* Need to check for interactive override here to so that it can
12984 ** affect console setup (for Windows only) and testing thereof.
12986 stdin_is_interactive
= 1;
12987 }else if( cli_strcmp(z
,"-batch")==0 ){
12988 /* already handled */
12989 }else if( cli_strcmp(z
,"-utf8")==0 ){
12990 /* already handled */
12991 }else if( cli_strcmp(z
,"-no-utf8")==0 ){
12992 /* already handled */
12993 }else if( cli_strcmp(z
,"-no-rowid-in-view")==0 ){
12994 /* already handled */
12995 }else if( cli_strcmp(z
,"-heap")==0 ){
12997 }else if( cli_strcmp(z
,"-pagecache")==0 ){
12999 }else if( cli_strcmp(z
,"-lookaside")==0 ){
13001 }else if( cli_strcmp(z
,"-threadsafe")==0 ){
13003 }else if( cli_strcmp(z
,"-nonce")==0 ){
13005 }else if( cli_strcmp(z
,"-mmap")==0 ){
13007 }else if( cli_strcmp(z
,"-memtrace")==0 ){
13009 }else if( cli_strcmp(z
,"-pcachetrace")==0 ){
13011 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
13012 }else if( cli_strcmp(z
,"-sorterref")==0 ){
13015 }else if( cli_strcmp(z
,"-vfs")==0 ){
13017 }else if( cli_strcmp(z
,"-vfstrace")==0 ){
13019 #ifdef SQLITE_ENABLE_MULTIPLEX
13020 }else if( cli_strcmp(z
,"-multiplex")==0 ){
13023 }else if( cli_strcmp(z
,"-help")==0 ){
13025 }else if( cli_strcmp(z
,"-cmd")==0 ){
13026 /* Run commands that follow -cmd first and separately from commands
13027 ** that simply appear on the command-line. This seems goofy. It would
13028 ** be better if all commands ran in the order that they appear. But
13029 ** we retain the goofy behavior for historical compatibility. */
13030 if( i
==argc
-1 ) break;
13031 z
= cmdline_option_value(argc
,argv
,++i
);
13033 rc
= do_meta_command(z
, &data
);
13034 if( rc
&& bail_on_error
) return rc
==2 ? 0 : rc
;
13037 rc
= shell_exec(&data
, z
, &zErrMsg
);
13039 shellEmitError(zErrMsg
);
13040 if( bail_on_error
) return rc
!=0 ? rc
: 1;
13042 eputf("Error: unable to process SQL \"%s\"\n", z
);
13043 if( bail_on_error
) return rc
;
13046 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13047 }else if( cli_strncmp(z
, "-A", 2)==0 ){
13049 eputf("Error: cannot mix regular SQL or dot-commands"
13050 " with \"%s\"\n", z
);
13053 open_db(&data
, OPEN_DB_ZIPFILE
);
13056 arDotCommand(&data
, 1, argv
+(i
-1), argc
-(i
-1));
13058 arDotCommand(&data
, 1, argv
+i
, argc
-i
);
13063 }else if( cli_strcmp(z
,"-safe")==0 ){
13064 data
.bSafeMode
= data
.bSafeModePersist
= 1;
13065 }else if( cli_strcmp(z
,"-unsafe-testing")==0 ){
13066 /* Acted upon in first pass. */
13068 eputf("%s: Error: unknown option: %s\n", Argv0
, z
);
13069 eputz("Use -help for a list of options.\n");
13072 data
.cMode
= data
.mode
;
13076 /* Run all arguments that do not begin with '-' as if they were separate
13077 ** command-line inputs, except for the argToSkip argument which contains
13078 ** the database filename.
13080 for(i
=0; i
<nCmd
; i
++){
13081 if( azCmd
[i
][0]=='.' ){
13082 rc
= do_meta_command(azCmd
[i
], &data
);
13084 if( rc
==2 ) rc
= 0;
13085 goto shell_main_exit
;
13089 echo_group_input(&data
, azCmd
[i
]);
13090 rc
= shell_exec(&data
, azCmd
[i
], &zErrMsg
);
13091 if( zErrMsg
|| rc
){
13093 shellEmitError(zErrMsg
);
13095 eputf("Error: unable to process SQL: %s\n", azCmd
[i
]);
13097 sqlite3_free(zErrMsg
);
13098 if( rc
==0 ) rc
= 1;
13099 goto shell_main_exit
;
13104 /* Run commands received from standard input
13106 if( stdin_is_interactive
){
13110 #if CIO_WIN_WC_XLATE
13111 # define SHELL_CIO_CHAR_SET (stdout_is_console? " (UTF-16 console I/O)" : "")
13113 # define SHELL_CIO_CHAR_SET ""
13115 sputf(stdout
, "SQLite version %s %.19s%s\n" /*extra-version-info*/
13116 "Enter \".help\" for usage hints.\n",
13117 sqlite3_libversion(), sqlite3_sourceid(), SHELL_CIO_CHAR_SET
);
13118 if( warnInmemoryDb
){
13119 sputz(stdout
, "Connected to a ");
13120 printBold("transient in-memory database");
13121 sputz(stdout
, ".\nUse \".open FILENAME\" to reopen on a"
13122 " persistent database.\n");
13124 zHistory
= getenv("SQLITE_HISTORY");
13126 zHistory
= strdup(zHistory
);
13127 }else if( (zHome
= find_home_dir(0))!=0 ){
13128 nHistory
= strlen30(zHome
) + 20;
13129 if( (zHistory
= malloc(nHistory
))!=0 ){
13130 sqlite3_snprintf(nHistory
, zHistory
,"%s/.sqlite_history", zHome
);
13133 if( zHistory
){ shell_read_history(zHistory
); }
13134 #if HAVE_READLINE || HAVE_EDITLINE
13135 rl_attempted_completion_function
= readline_completion
;
13136 #elif HAVE_LINENOISE
13137 linenoiseSetCompletionCallback(linenoise_completion
, NULL
);
13140 rc
= process_input(&data
);
13142 shell_stifle_history(2000);
13143 shell_write_history(zHistory
);
13148 rc
= process_input(&data
);
13151 #ifndef SQLITE_SHELL_FIDDLE
13152 /* In WASM mode we have to leave the db state in place so that
13153 ** client code can "push" SQL into it after this call returns. */
13154 #ifndef SQLITE_OMIT_VIRTUALTABLE
13155 if( data
.expert
.pExpert
){
13156 expertFinish(&data
, 1, 0);
13161 set_table_name(&data
, 0);
13163 session_close_all(&data
, -1);
13166 for(i
=0; i
<ArraySize(data
.aAuxDb
); i
++){
13167 sqlite3_free(data
.aAuxDb
[i
].zFreeOnClose
);
13168 if( data
.aAuxDb
[i
].db
){
13169 session_close_all(&data
, i
);
13170 close_db(data
.aAuxDb
[i
].db
);
13174 output_reset(&data
);
13175 data
.doXdgOpen
= 0;
13176 clearTempFile(&data
);
13177 #if !SQLITE_SHELL_IS_UTF8
13178 for(i
=0; i
<argcToFree
; i
++) free(argvToFree
[i
]);
13181 free(data
.colWidth
);
13183 /* Clear the global data structure so that valgrind will detect memory
13185 memset(&data
, 0, sizeof(data
));
13186 if( bEnableVfstrace
){
13187 vfstrace_unregister("trace");
13189 #ifdef SQLITE_DEBUG
13190 if( sqlite3_memory_used()>mem_main_enter
){
13191 eputf("Memory leaked: %u bytes\n",
13192 (unsigned int)(sqlite3_memory_used()-mem_main_enter
));
13195 #else /* SQLITE_SHELL_FIDDLE... */
13202 #ifdef SQLITE_SHELL_FIDDLE
13203 /* Only for emcc experimentation purposes. */
13204 int fiddle_experiment(int a
,int b
){
13209 ** Returns a pointer to the current DB handle.
13211 sqlite3
* fiddle_db_handle(){
13216 ** Returns a pointer to the given DB name's VFS. If zDbName is 0 then
13217 ** "main" is assumed. Returns 0 if no db with the given name is
13220 sqlite3_vfs
* fiddle_db_vfs(const char *zDbName
){
13221 sqlite3_vfs
* pVfs
= 0;
13223 sqlite3_file_control(globalDb
, zDbName
? zDbName
: "main",
13224 SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
13229 /* Only for emcc experimentation purposes. */
13230 sqlite3
* fiddle_db_arg(sqlite3
*arg
){
13231 oputf("fiddle_db_arg(%p)\n", (const void*)arg
);
13236 ** Intended to be called via a SharedWorker() while a separate
13237 ** SharedWorker() (which manages the wasm module) is performing work
13238 ** which should be interrupted. Unfortunately, SharedWorker is not
13239 ** portable enough to make real use of.
13241 void fiddle_interrupt(void){
13242 if( globalDb
) sqlite3_interrupt(globalDb
);
13246 ** Returns the filename of the given db name, assuming "main" if
13247 ** zDbName is NULL. Returns NULL if globalDb is not opened.
13249 const char * fiddle_db_filename(const char * zDbName
){
13251 ? sqlite3_db_filename(globalDb
, zDbName
? zDbName
: "main")
13256 ** Completely wipes out the contents of the currently-opened database
13257 ** but leaves its storage intact for reuse. If any transactions are
13258 ** active, they are forcibly rolled back.
13260 void fiddle_reset_db(void){
13263 while( sqlite3_txn_state(globalDb
,0)>0 ){
13265 ** Resolve problem reported in
13266 ** https://sqlite.org/forum/forumpost/0b41a25d65
13268 oputz("Rolling back in-progress transaction.\n");
13269 sqlite3_exec(globalDb
,"ROLLBACK", 0, 0, 0);
13271 rc
= sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 1, 0);
13272 if( 0==rc
) sqlite3_exec(globalDb
, "VACUUM", 0, 0, 0);
13273 sqlite3_db_config(globalDb
, SQLITE_DBCONFIG_RESET_DATABASE
, 0, 0);
13278 ** Uses the current database's VFS xRead to stream the db file's
13279 ** contents out to the given callback. The callback gets a single
13280 ** chunk of size n (its 2nd argument) on each call and must return 0
13281 ** on success, non-0 on error. This function returns 0 on success,
13282 ** SQLITE_NOTFOUND if no db is open, or propagates any other non-0
13283 ** code from the callback. Note that this is not thread-friendly: it
13284 ** expects that it will be the only thread reading the db file and
13285 ** takes no measures to ensure that is the case.
13287 int fiddle_export_db( int (*xCallback
)(unsigned const char *zOut
, int n
) ){
13288 sqlite3_int64 nSize
= 0;
13289 sqlite3_int64 nPos
= 0;
13290 sqlite3_file
* pFile
= 0;
13291 unsigned char buf
[1024 * 8];
13292 int nBuf
= (int)sizeof(buf
);
13293 int rc
= shellState
.db
13294 ? sqlite3_file_control(shellState
.db
, "main",
13295 SQLITE_FCNTL_FILE_POINTER
, &pFile
)
13297 if( rc
) return rc
;
13298 rc
= pFile
->pMethods
->xFileSize(pFile
, &nSize
);
13299 if( rc
) return rc
;
13301 /* DB size is not an even multiple of the buffer size. Reduce
13302 ** buffer size so that we do not unduly inflate the db size when
13304 if(0 == nSize
% 4096) nBuf
= 4096;
13305 else if(0 == nSize
% 2048) nBuf
= 2048;
13306 else if(0 == nSize
% 1024) nBuf
= 1024;
13309 for( ; 0==rc
&& nPos
<nSize
; nPos
+= nBuf
){
13310 rc
= pFile
->pMethods
->xRead(pFile
, buf
, nBuf
, nPos
);
13311 if(SQLITE_IOERR_SHORT_READ
== rc
){
13312 rc
= (nPos
+ nBuf
) < nSize
? rc
: 0/*assume EOF*/;
13314 if( 0==rc
) rc
= xCallback(buf
, nBuf
);
13320 ** Trivial exportable function for emscripten. It processes zSql as if
13321 ** it were input to the sqlite3 shell and redirects all output to the
13322 ** wasm binding. fiddle_main() must have been called before this
13323 ** is called, or results are undefined.
13325 void fiddle_exec(const char * zSql
){
13327 if('.'==*zSql
) puts(zSql
);
13328 shellState
.wasm
.zInput
= zSql
;
13329 shellState
.wasm
.zPos
= zSql
;
13330 process_input(&shellState
);
13331 shellState
.wasm
.zInput
= shellState
.wasm
.zPos
= 0;
13334 #endif /* SQLITE_SHELL_FIDDLE */