blob: 5858296420599d1596ff4917180fdc03f5ca80ac [file] [log] [blame]
drh2ce15c32017-07-11 13:34:40 +00001/*
2** 2001 September 15
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains code to implement the "sqlite" command line
13** utility for accessing SQLite databases.
14*/
15#if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16/* This needs to come before any includes for MSVC compiler */
17#define _CRT_SECURE_NO_WARNINGS
18#endif
19
20/*
mistachkin43e86272020-04-09 15:31:22 +000021** Determine if we are dealing with WinRT, which provides only a subset of
22** the full Win32 API.
23*/
24#if !defined(SQLITE_OS_WINRT)
25# define SQLITE_OS_WINRT 0
26#endif
27
28/*
drh2ce15c32017-07-11 13:34:40 +000029** Warning pragmas copied from msvc.h in the core.
30*/
31#if defined(_MSC_VER)
32#pragma warning(disable : 4054)
33#pragma warning(disable : 4055)
34#pragma warning(disable : 4100)
35#pragma warning(disable : 4127)
36#pragma warning(disable : 4130)
37#pragma warning(disable : 4152)
38#pragma warning(disable : 4189)
39#pragma warning(disable : 4206)
40#pragma warning(disable : 4210)
41#pragma warning(disable : 4232)
42#pragma warning(disable : 4244)
43#pragma warning(disable : 4305)
44#pragma warning(disable : 4306)
45#pragma warning(disable : 4702)
46#pragma warning(disable : 4706)
47#endif /* defined(_MSC_VER) */
48
49/*
50** No support for loadable extensions in VxWorks.
51*/
52#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
53# define SQLITE_OMIT_LOAD_EXTENSION 1
54#endif
55
56/*
57** Enable large-file support for fopen() and friends on unix.
58*/
59#ifndef SQLITE_DISABLE_LFS
60# define _LARGE_FILE 1
61# ifndef _FILE_OFFSET_BITS
62# define _FILE_OFFSET_BITS 64
63# endif
64# define _LARGEFILE_SOURCE 1
65#endif
66
67#include <stdlib.h>
68#include <string.h>
69#include <stdio.h>
70#include <assert.h>
71#include "sqlite3.h"
drh1e506b52018-01-05 21:01:37 +000072typedef sqlite3_int64 i64;
73typedef sqlite3_uint64 u64;
drh1fa6d9f2018-01-06 21:46:01 +000074typedef unsigned char u8;
drh2ce15c32017-07-11 13:34:40 +000075#if SQLITE_USER_AUTHENTICATION
76# include "sqlite3userauth.h"
77#endif
78#include <ctype.h>
79#include <stdarg.h>
80
81#if !defined(_WIN32) && !defined(WIN32)
82# include <signal.h>
83# if !defined(__RTP__) && !defined(_WRS_KERNEL)
84# include <pwd.h>
85# endif
mistachkinacae8c32018-01-05 20:08:46 +000086#endif
mistachkin562f0c82018-01-09 00:28:24 +000087#if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
drh2ce15c32017-07-11 13:34:40 +000088# include <unistd.h>
mistachkinacae8c32018-01-05 20:08:46 +000089# include <dirent.h>
mistachkin1e8487d2018-07-22 06:25:35 +000090# define GETPID getpid
mistachkin562f0c82018-01-09 00:28:24 +000091# if defined(__MINGW32__)
mistachkinacae8c32018-01-05 20:08:46 +000092# define DIRENT dirent
mistachkin2f74b3c2018-01-05 20:26:06 +000093# ifndef S_ISLNK
94# define S_ISLNK(mode) (0)
95# endif
mistachkinacae8c32018-01-05 20:08:46 +000096# endif
mistachkin1e8487d2018-07-22 06:25:35 +000097#else
98# define GETPID (int)GetCurrentProcessId
drh2ce15c32017-07-11 13:34:40 +000099#endif
mistachkindfdfd8c2018-01-04 22:46:08 +0000100#include <sys/types.h>
101#include <sys/stat.h>
drh2ce15c32017-07-11 13:34:40 +0000102
103#if HAVE_READLINE
104# include <readline/readline.h>
105# include <readline/history.h>
106#endif
107
108#if HAVE_EDITLINE
109# include <editline/readline.h>
110#endif
111
112#if HAVE_EDITLINE || HAVE_READLINE
113
114# define shell_add_history(X) add_history(X)
115# define shell_read_history(X) read_history(X)
116# define shell_write_history(X) write_history(X)
117# define shell_stifle_history(X) stifle_history(X)
118# define shell_readline(X) readline(X)
119
120#elif HAVE_LINENOISE
121
122# include "linenoise.h"
123# define shell_add_history(X) linenoiseHistoryAdd(X)
124# define shell_read_history(X) linenoiseHistoryLoad(X)
125# define shell_write_history(X) linenoiseHistorySave(X)
126# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
127# define shell_readline(X) linenoise(X)
128
129#else
130
131# define shell_read_history(X)
132# define shell_write_history(X)
133# define shell_stifle_history(X)
134
135# define SHELL_USE_LOCAL_GETLINE 1
136#endif
137
138
139#if defined(_WIN32) || defined(WIN32)
mistachkin43e86272020-04-09 15:31:22 +0000140# if SQLITE_OS_WINRT
141# define SQLITE_OMIT_POPEN 1
142# else
143# include <io.h>
144# include <fcntl.h>
145# define isatty(h) _isatty(h)
146# ifndef access
147# define access(f,m) _access((f),(m))
148# endif
149# ifndef unlink
150# define unlink _unlink
151# endif
152# ifndef strdup
153# define strdup _strdup
154# endif
155# undef popen
156# define popen _popen
157# undef pclose
158# define pclose _pclose
drh2ce15c32017-07-11 13:34:40 +0000159# endif
drh2ce15c32017-07-11 13:34:40 +0000160#else
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
163
164# if !defined(__RTP__) && !defined(_WRS_KERNEL)
165 /* popen and pclose are not C89 functions and so are
166 ** sometimes omitted from the <stdio.h> header */
167 extern FILE *popen(const char*,const char*);
168 extern int pclose(FILE*);
169# else
170# define SQLITE_OMIT_POPEN 1
171# endif
172#endif
173
174#if defined(_WIN32_WCE)
175/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
178 */
179#define isatty(x) 1
180#endif
181
182/* ctype macros that work with signed characters */
183#define IsSpace(X) isspace((unsigned char)X)
184#define IsDigit(X) isdigit((unsigned char)X)
185#define ToLower(X) (char)tolower((unsigned char)X)
186
187#if defined(_WIN32) || defined(WIN32)
mistachkin43e86272020-04-09 15:31:22 +0000188#if SQLITE_OS_WINRT
189#include <intrin.h>
190#endif
drh2ce15c32017-07-11 13:34:40 +0000191#include <windows.h>
192
193/* string conversion routines only needed on Win32 */
194extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
198#endif
199
200/* On Windows, we normally run with output mode of TEXT so that \n characters
201** are automatically translated into \r\n. However, this behavior needs
202** to be disabled in some cases (ex: when generating CSV output and when
203** rendering quoted strings that contain \n characters). The following
204** routines take care of that.
205*/
mistachkin43e86272020-04-09 15:31:22 +0000206#if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
drh2ce15c32017-07-11 13:34:40 +0000207static void setBinaryMode(FILE *file, int isOutput){
208 if( isOutput ) fflush(file);
209 _setmode(_fileno(file), _O_BINARY);
210}
211static void setTextMode(FILE *file, int isOutput){
212 if( isOutput ) fflush(file);
213 _setmode(_fileno(file), _O_TEXT);
214}
215#else
216# define setBinaryMode(X,Y)
217# define setTextMode(X,Y)
218#endif
219
220
221/* True if the timer is enabled */
222static int enableTimer = 0;
223
224/* Return the current wall-clock time */
225static sqlite3_int64 timeOfDay(void){
226 static sqlite3_vfs *clockVfs = 0;
227 sqlite3_int64 t;
228 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230 clockVfs->xCurrentTimeInt64(clockVfs, &t);
231 }else{
232 double r;
233 clockVfs->xCurrentTime(clockVfs, &r);
234 t = (sqlite3_int64)(r*86400000.0);
235 }
236 return t;
237}
238
239#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240#include <sys/time.h>
241#include <sys/resource.h>
242
243/* VxWorks does not support getrusage() as far as we can determine */
244#if defined(_WRS_KERNEL) || defined(__RTP__)
245struct rusage {
246 struct timeval ru_utime; /* user CPU time used */
247 struct timeval ru_stime; /* system CPU time used */
248};
249#define getrusage(A,B) memset(B,0,sizeof(*B))
250#endif
251
252/* Saved resource information for the beginning of an operation */
253static struct rusage sBegin; /* CPU time at start */
254static sqlite3_int64 iBegin; /* Wall-clock time at start */
255
256/*
257** Begin timing an operation
258*/
259static void beginTimer(void){
260 if( enableTimer ){
261 getrusage(RUSAGE_SELF, &sBegin);
262 iBegin = timeOfDay();
263 }
264}
265
266/* Return the difference of two time_structs in seconds */
267static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269 (double)(pEnd->tv_sec - pStart->tv_sec);
270}
271
272/*
273** Print the timing results.
274*/
275static void endTimer(void){
276 if( enableTimer ){
277 sqlite3_int64 iEnd = timeOfDay();
278 struct rusage sEnd;
279 getrusage(RUSAGE_SELF, &sEnd);
280 printf("Run Time: real %.3f user %f sys %f\n",
281 (iEnd - iBegin)*0.001,
282 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284 }
285}
286
287#define BEGIN_TIMER beginTimer()
288#define END_TIMER endTimer()
289#define HAS_TIMER 1
290
291#elif (defined(_WIN32) || defined(WIN32))
292
293/* Saved resource information for the beginning of an operation */
294static HANDLE hProcess;
295static FILETIME ftKernelBegin;
296static FILETIME ftUserBegin;
297static sqlite3_int64 ftWallBegin;
298typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299 LPFILETIME, LPFILETIME);
300static GETPROCTIMES getProcessTimesAddr = NULL;
301
302/*
303** Check to see if we have timer support. Return 1 if necessary
304** support found (or found previously).
305*/
306static int hasTimer(void){
307 if( getProcessTimesAddr ){
308 return 1;
309 } else {
mistachkin43e86272020-04-09 15:31:22 +0000310#if !SQLITE_OS_WINRT
drh2ce15c32017-07-11 13:34:40 +0000311 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
312 ** versions. See if the version we are running on has it, and if it
313 ** does, save off a pointer to it and the current process handle.
314 */
315 hProcess = GetCurrentProcess();
316 if( hProcess ){
317 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
318 if( NULL != hinstLib ){
319 getProcessTimesAddr =
320 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
321 if( NULL != getProcessTimesAddr ){
322 return 1;
323 }
324 FreeLibrary(hinstLib);
325 }
326 }
mistachkin43e86272020-04-09 15:31:22 +0000327#endif
drh2ce15c32017-07-11 13:34:40 +0000328 }
329 return 0;
330}
331
332/*
333** Begin timing an operation
334*/
335static void beginTimer(void){
336 if( enableTimer && getProcessTimesAddr ){
337 FILETIME ftCreation, ftExit;
338 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
339 &ftKernelBegin,&ftUserBegin);
340 ftWallBegin = timeOfDay();
341 }
342}
343
344/* Return the difference of two FILETIME structs in seconds */
345static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
346 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
347 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
348 return (double) ((i64End - i64Start) / 10000000.0);
349}
350
351/*
352** Print the timing results.
353*/
354static void endTimer(void){
355 if( enableTimer && getProcessTimesAddr){
356 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
357 sqlite3_int64 ftWallEnd = timeOfDay();
358 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
359 printf("Run Time: real %.3f user %f sys %f\n",
360 (ftWallEnd - ftWallBegin)*0.001,
361 timeDiff(&ftUserBegin, &ftUserEnd),
362 timeDiff(&ftKernelBegin, &ftKernelEnd));
363 }
364}
365
366#define BEGIN_TIMER beginTimer()
367#define END_TIMER endTimer()
368#define HAS_TIMER hasTimer()
369
370#else
371#define BEGIN_TIMER
372#define END_TIMER
373#define HAS_TIMER 0
374#endif
375
376/*
377** Used to prevent warnings about unused parameters
378*/
379#define UNUSED_PARAMETER(x) (void)(x)
380
381/*
drh5af06982018-01-10 00:53:55 +0000382** Number of elements in an array
383*/
384#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
385
386/*
drh2ce15c32017-07-11 13:34:40 +0000387** If the following flag is set, then command execution stops
388** at an error if we are not interactive.
389*/
390static int bail_on_error = 0;
391
392/*
393** Threat stdin as an interactive input if the following variable
394** is true. Otherwise, assume stdin is connected to a file or pipe.
395*/
396static int stdin_is_interactive = 1;
397
398/*
399** On Windows systems we have to know if standard output is a console
400** in order to translate UTF-8 into MBCS. The following variable is
401** true if translation is required.
402*/
403static int stdout_is_console = 1;
404
405/*
406** The following is the open SQLite database. We make a pointer
407** to this database a static variable so that it can be accessed
408** by the SIGINT handler to interrupt database processing.
409*/
410static sqlite3 *globalDb = 0;
411
412/*
413** True if an interrupt (Control-C) has been received.
414*/
415static volatile int seenInterrupt = 0;
416
drh4a3a3eb2020-02-29 15:53:48 +0000417#ifdef SQLITE_DEBUG
418/*
419** Out-of-memory simulator variables
420*/
421static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
422static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
423static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
424#endif /* SQLITE_DEBUG */
425
drh2ce15c32017-07-11 13:34:40 +0000426/*
427** This is the name of our program. It is set in main(), used
428** in a number of other places, mostly for error messages.
429*/
430static char *Argv0;
431
432/*
433** Prompt strings. Initialized in main. Settable with
434** .prompt main continue
435*/
436static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
437static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
438
439/*
440** Render output like fprintf(). Except, if the output is going to the
441** console and if this is running on a Windows machine, translate the
442** output from UTF-8 into MBCS.
443*/
444#if defined(_WIN32) || defined(WIN32)
445void utf8_printf(FILE *out, const char *zFormat, ...){
446 va_list ap;
447 va_start(ap, zFormat);
448 if( stdout_is_console && (out==stdout || out==stderr) ){
449 char *z1 = sqlite3_vmprintf(zFormat, ap);
450 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
451 sqlite3_free(z1);
452 fputs(z2, out);
453 sqlite3_free(z2);
454 }else{
455 vfprintf(out, zFormat, ap);
456 }
457 va_end(ap);
458}
459#elif !defined(utf8_printf)
460# define utf8_printf fprintf
461#endif
462
463/*
464** Render output like fprintf(). This should not be used on anything that
465** includes string formatting (e.g. "%s").
466*/
467#if !defined(raw_printf)
468# define raw_printf fprintf
469#endif
470
drh4b5345c2018-04-24 13:07:40 +0000471/* Indicate out-of-memory and exit. */
472static void shell_out_of_memory(void){
473 raw_printf(stderr,"Error: out of memory\n");
474 exit(1);
475}
476
drh4a3a3eb2020-02-29 15:53:48 +0000477#ifdef SQLITE_DEBUG
478/* This routine is called when a simulated OOM occurs. It is broken
479** out as a separate routine to make it easy to set a breakpoint on
480** the OOM
481*/
482void shellOomFault(void){
483 if( oomRepeat>0 ){
484 oomRepeat--;
485 }else{
486 oomCounter--;
487 }
488}
489#endif /* SQLITE_DEBUG */
490
491#ifdef SQLITE_DEBUG
492/* This routine is a replacement malloc() that is used to simulate
493** Out-Of-Memory (OOM) errors for testing purposes.
494*/
495static void *oomMalloc(int nByte){
496 if( oomCounter ){
497 if( oomCounter==1 ){
498 shellOomFault();
499 return 0;
500 }else{
501 oomCounter--;
502 }
503 }
504 return defaultMalloc(nByte);
505}
506#endif /* SQLITE_DEBUG */
507
508#ifdef SQLITE_DEBUG
509/* Register the OOM simulator. This must occur before any memory
510** allocations */
511static void registerOomSimulator(void){
512 sqlite3_mem_methods mem;
513 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
514 defaultMalloc = mem.xMalloc;
515 mem.xMalloc = oomMalloc;
516 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
517}
518#endif
519
drh2ce15c32017-07-11 13:34:40 +0000520/*
521** Write I/O traces to the following stream.
522*/
523#ifdef SQLITE_ENABLE_IOTRACE
524static FILE *iotrace = 0;
525#endif
526
527/*
528** This routine works like printf in that its first argument is a
529** format string and subsequent arguments are values to be substituted
530** in place of % fields. The result of formatting this string
531** is written to iotrace.
532*/
533#ifdef SQLITE_ENABLE_IOTRACE
534static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
535 va_list ap;
536 char *z;
537 if( iotrace==0 ) return;
538 va_start(ap, zFormat);
539 z = sqlite3_vmprintf(zFormat, ap);
540 va_end(ap);
541 utf8_printf(iotrace, "%s", z);
542 sqlite3_free(z);
543}
544#endif
545
546/*
547** Output string zUtf to stream pOut as w characters. If w is negative,
548** then right-justify the text. W is the width in UTF-8 characters, not
549** in bytes. This is different from the %*.*s specification in printf
550** since with %*.*s the width is measured in bytes, not characters.
551*/
552static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
553 int i;
554 int n;
555 int aw = w<0 ? -w : w;
556 char zBuf[1000];
557 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
558 for(i=n=0; zUtf[i]; i++){
559 if( (zUtf[i]&0xc0)!=0x80 ){
560 n++;
561 if( n==aw ){
562 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
563 break;
564 }
565 }
566 }
567 if( n>=aw ){
568 utf8_printf(pOut, "%.*s", i, zUtf);
569 }else if( w<0 ){
570 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
571 }else{
572 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
573 }
574}
575
576
577/*
578** Determines if a string is a number of not.
579*/
580static int isNumber(const char *z, int *realnum){
581 if( *z=='-' || *z=='+' ) z++;
582 if( !IsDigit(*z) ){
583 return 0;
584 }
585 z++;
586 if( realnum ) *realnum = 0;
587 while( IsDigit(*z) ){ z++; }
588 if( *z=='.' ){
589 z++;
590 if( !IsDigit(*z) ) return 0;
591 while( IsDigit(*z) ){ z++; }
592 if( realnum ) *realnum = 1;
593 }
594 if( *z=='e' || *z=='E' ){
595 z++;
596 if( *z=='+' || *z=='-' ) z++;
597 if( !IsDigit(*z) ) return 0;
598 while( IsDigit(*z) ){ z++; }
599 if( realnum ) *realnum = 1;
600 }
601 return *z==0;
602}
603
604/*
605** Compute a string length that is limited to what can be stored in
606** lower 30 bits of a 32-bit signed integer.
607*/
608static int strlen30(const char *z){
609 const char *z2 = z;
610 while( *z2 ){ z2++; }
611 return 0x3fffffff & (int)(z2 - z);
612}
613
614/*
615** Return the length of a string in characters. Multibyte UTF8 characters
616** count as a single character.
617*/
618static int strlenChar(const char *z){
619 int n = 0;
620 while( *z ){
621 if( (0xc0&*(z++))!=0x80 ) n++;
622 }
623 return n;
624}
625
626/*
627** This routine reads a line of text from FILE in, stores
628** the text in memory obtained from malloc() and returns a pointer
629** to the text. NULL is returned at end of file, or if malloc()
630** fails.
631**
632** If zLine is not NULL then it is a malloced buffer returned from
633** a previous call to this routine that may be reused.
634*/
635static char *local_getline(char *zLine, FILE *in){
636 int nLine = zLine==0 ? 0 : 100;
637 int n = 0;
638
639 while( 1 ){
640 if( n+100>nLine ){
641 nLine = nLine*2 + 100;
642 zLine = realloc(zLine, nLine);
drh884406b2018-07-29 18:56:35 +0000643 if( zLine==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +0000644 }
645 if( fgets(&zLine[n], nLine - n, in)==0 ){
646 if( n==0 ){
647 free(zLine);
648 return 0;
649 }
650 zLine[n] = 0;
651 break;
652 }
653 while( zLine[n] ) n++;
654 if( n>0 && zLine[n-1]=='\n' ){
655 n--;
656 if( n>0 && zLine[n-1]=='\r' ) n--;
657 zLine[n] = 0;
658 break;
659 }
660 }
661#if defined(_WIN32) || defined(WIN32)
662 /* For interactive input on Windows systems, translate the
663 ** multi-byte characterset characters into UTF-8. */
664 if( stdin_is_interactive && in==stdin ){
665 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
666 if( zTrans ){
667 int nTrans = strlen30(zTrans)+1;
668 if( nTrans>nLine ){
669 zLine = realloc(zLine, nTrans);
drh884406b2018-07-29 18:56:35 +0000670 if( zLine==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +0000671 }
672 memcpy(zLine, zTrans, nTrans);
673 sqlite3_free(zTrans);
674 }
675 }
676#endif /* defined(_WIN32) || defined(WIN32) */
677 return zLine;
678}
679
680/*
681** Retrieve a single line of input text.
682**
683** If in==0 then read from standard input and prompt before each line.
684** If isContinuation is true, then a continuation prompt is appropriate.
685** If isContinuation is zero, then the main prompt should be used.
686**
687** If zPrior is not NULL then it is a buffer from a prior call to this
688** routine that can be reused.
689**
690** The result is stored in space obtained from malloc() and must either
691** be freed by the caller or else passed back into this routine via the
692** zPrior argument for reuse.
693*/
694static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
695 char *zPrompt;
696 char *zResult;
697 if( in!=0 ){
698 zResult = local_getline(zPrior, in);
699 }else{
700 zPrompt = isContinuation ? continuePrompt : mainPrompt;
701#if SHELL_USE_LOCAL_GETLINE
702 printf("%s", zPrompt);
703 fflush(stdout);
704 zResult = local_getline(zPrior, stdin);
705#else
706 free(zPrior);
707 zResult = shell_readline(zPrompt);
708 if( zResult && *zResult ) shell_add_history(zResult);
709#endif
710 }
711 return zResult;
712}
drh5af06982018-01-10 00:53:55 +0000713
714
715/*
716** Return the value of a hexadecimal digit. Return -1 if the input
717** is not a hex digit.
718*/
719static int hexDigitValue(char c){
720 if( c>='0' && c<='9' ) return c - '0';
721 if( c>='a' && c<='f' ) return c - 'a' + 10;
722 if( c>='A' && c<='F' ) return c - 'A' + 10;
723 return -1;
724}
725
726/*
727** Interpret zArg as an integer value, possibly with suffixes.
728*/
729static sqlite3_int64 integerValue(const char *zArg){
730 sqlite3_int64 v = 0;
731 static const struct { char *zSuffix; int iMult; } aMult[] = {
732 { "KiB", 1024 },
733 { "MiB", 1024*1024 },
734 { "GiB", 1024*1024*1024 },
735 { "KB", 1000 },
736 { "MB", 1000000 },
737 { "GB", 1000000000 },
738 { "K", 1000 },
739 { "M", 1000000 },
740 { "G", 1000000000 },
741 };
742 int i;
743 int isNeg = 0;
744 if( zArg[0]=='-' ){
745 isNeg = 1;
746 zArg++;
747 }else if( zArg[0]=='+' ){
748 zArg++;
749 }
750 if( zArg[0]=='0' && zArg[1]=='x' ){
751 int x;
752 zArg += 2;
753 while( (x = hexDigitValue(zArg[0]))>=0 ){
754 v = (v<<4) + x;
755 zArg++;
756 }
757 }else{
758 while( IsDigit(zArg[0]) ){
759 v = v*10 + zArg[0] - '0';
760 zArg++;
761 }
762 }
763 for(i=0; i<ArraySize(aMult); i++){
764 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
765 v *= aMult[i].iMult;
766 break;
767 }
768 }
769 return isNeg? -v : v;
770}
771
drh2ce15c32017-07-11 13:34:40 +0000772/*
773** A variable length string to which one can append text.
774*/
775typedef struct ShellText ShellText;
776struct ShellText {
777 char *z;
778 int n;
779 int nAlloc;
780};
781
782/*
783** Initialize and destroy a ShellText object
784*/
785static void initText(ShellText *p){
786 memset(p, 0, sizeof(*p));
787}
788static void freeText(ShellText *p){
789 free(p->z);
790 initText(p);
791}
792
793/* zIn is either a pointer to a NULL-terminated string in memory obtained
794** from malloc(), or a NULL pointer. The string pointed to by zAppend is
795** added to zIn, and the result returned in memory obtained from malloc().
796** zIn, if it was not NULL, is freed.
797**
798** If the third argument, quote, is not '\0', then it is used as a
799** quote character for zAppend.
800*/
801static void appendText(ShellText *p, char const *zAppend, char quote){
802 int len;
803 int i;
804 int nAppend = strlen30(zAppend);
805
806 len = nAppend+p->n+1;
807 if( quote ){
808 len += 2;
809 for(i=0; i<nAppend; i++){
810 if( zAppend[i]==quote ) len++;
811 }
812 }
813
814 if( p->n+len>=p->nAlloc ){
815 p->nAlloc = p->nAlloc*2 + len + 20;
816 p->z = realloc(p->z, p->nAlloc);
drh884406b2018-07-29 18:56:35 +0000817 if( p->z==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +0000818 }
819
820 if( quote ){
821 char *zCsr = p->z+p->n;
822 *zCsr++ = quote;
823 for(i=0; i<nAppend; i++){
824 *zCsr++ = zAppend[i];
825 if( zAppend[i]==quote ) *zCsr++ = quote;
826 }
827 *zCsr++ = quote;
828 p->n = (int)(zCsr - p->z);
829 *zCsr = '\0';
830 }else{
831 memcpy(p->z+p->n, zAppend, nAppend);
832 p->n += nAppend;
833 p->z[p->n] = '\0';
834 }
835}
836
837/*
838** Attempt to determine if identifier zName needs to be quoted, either
839** because it contains non-alphanumeric characters, or because it is an
840** SQLite keyword. Be conservative in this estimate: When in doubt assume
841** that quoting is required.
842**
843** Return '"' if quoting is required. Return 0 if no quoting is required.
844*/
845static char quoteChar(const char *zName){
drhfc0ec3e2018-04-25 19:02:48 +0000846 int i;
drh2ce15c32017-07-11 13:34:40 +0000847 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
848 for(i=0; zName[i]; i++){
849 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
850 }
drhfc0ec3e2018-04-25 19:02:48 +0000851 return sqlite3_keyword_check(zName, i) ? '"' : 0;
drh2ce15c32017-07-11 13:34:40 +0000852}
853
854/*
drh667a2a22018-01-02 00:04:37 +0000855** Construct a fake object name and column list to describe the structure
856** of the view, virtual table, or table valued function zSchema.zName.
drhceba7922018-01-01 21:28:25 +0000857*/
drh667a2a22018-01-02 00:04:37 +0000858static char *shellFakeSchema(
drhceba7922018-01-01 21:28:25 +0000859 sqlite3 *db, /* The database connection containing the vtab */
860 const char *zSchema, /* Schema of the database holding the vtab */
861 const char *zName /* The name of the virtual table */
862){
863 sqlite3_stmt *pStmt = 0;
864 char *zSql;
drh1d315cf2018-01-01 21:49:43 +0000865 ShellText s;
866 char cQuote;
867 char *zDiv = "(";
drh667a2a22018-01-02 00:04:37 +0000868 int nRow = 0;
drhceba7922018-01-01 21:28:25 +0000869
drh1d315cf2018-01-01 21:49:43 +0000870 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
871 zSchema ? zSchema : "main", zName);
drhceba7922018-01-01 21:28:25 +0000872 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
873 sqlite3_free(zSql);
drh1d315cf2018-01-01 21:49:43 +0000874 initText(&s);
875 if( zSchema ){
876 cQuote = quoteChar(zSchema);
877 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
878 appendText(&s, zSchema, cQuote);
879 appendText(&s, ".", 0);
drhceba7922018-01-01 21:28:25 +0000880 }
drh1d315cf2018-01-01 21:49:43 +0000881 cQuote = quoteChar(zName);
882 appendText(&s, zName, cQuote);
883 while( sqlite3_step(pStmt)==SQLITE_ROW ){
884 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
drh667a2a22018-01-02 00:04:37 +0000885 nRow++;
drh1d315cf2018-01-01 21:49:43 +0000886 appendText(&s, zDiv, 0);
887 zDiv = ",";
888 cQuote = quoteChar(zCol);
889 appendText(&s, zCol, cQuote);
890 }
891 appendText(&s, ")", 0);
drhceba7922018-01-01 21:28:25 +0000892 sqlite3_finalize(pStmt);
drh667a2a22018-01-02 00:04:37 +0000893 if( nRow==0 ){
894 freeText(&s);
895 s.z = 0;
896 }
drh1d315cf2018-01-01 21:49:43 +0000897 return s.z;
drhceba7922018-01-01 21:28:25 +0000898}
899
900/*
drh667a2a22018-01-02 00:04:37 +0000901** SQL function: shell_module_schema(X)
902**
903** Return a fake schema for the table-valued function or eponymous virtual
904** table X.
905*/
906static void shellModuleSchema(
907 sqlite3_context *pCtx,
908 int nVal,
909 sqlite3_value **apVal
910){
911 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
912 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
drhb9685182018-01-17 13:15:23 +0000913 UNUSED_PARAMETER(nVal);
drh667a2a22018-01-02 00:04:37 +0000914 if( zFake ){
dandcfbff92018-01-08 17:05:32 +0000915 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
drh667a2a22018-01-02 00:04:37 +0000916 -1, sqlite3_free);
dandcfbff92018-01-08 17:05:32 +0000917 free(zFake);
drh667a2a22018-01-02 00:04:37 +0000918 }
919}
920
921/*
drh2ce15c32017-07-11 13:34:40 +0000922** SQL function: shell_add_schema(S,X)
923**
924** Add the schema name X to the CREATE statement in S and return the result.
925** Examples:
926**
927** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
928**
929** Also works on
930**
931** CREATE INDEX
932** CREATE UNIQUE INDEX
933** CREATE VIEW
934** CREATE TRIGGER
935** CREATE VIRTUAL TABLE
936**
937** This UDF is used by the .schema command to insert the schema name of
938** attached databases into the middle of the sqlite_master.sql field.
939*/
940static void shellAddSchemaName(
941 sqlite3_context *pCtx,
942 int nVal,
943 sqlite3_value **apVal
944){
945 static const char *aPrefix[] = {
946 "TABLE",
947 "INDEX",
948 "UNIQUE INDEX",
949 "VIEW",
950 "TRIGGER",
951 "VIRTUAL TABLE"
952 };
953 int i = 0;
954 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
955 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
drh667a2a22018-01-02 00:04:37 +0000956 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
drhceba7922018-01-01 21:28:25 +0000957 sqlite3 *db = sqlite3_context_db_handle(pCtx);
drhb9685182018-01-17 13:15:23 +0000958 UNUSED_PARAMETER(nVal);
drh2ce15c32017-07-11 13:34:40 +0000959 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
drh89997982017-07-11 18:11:33 +0000960 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
drh2ce15c32017-07-11 13:34:40 +0000961 int n = strlen30(aPrefix[i]);
962 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
drhceba7922018-01-01 21:28:25 +0000963 char *z = 0;
drh667a2a22018-01-02 00:04:37 +0000964 char *zFake = 0;
drhceba7922018-01-01 21:28:25 +0000965 if( zSchema ){
966 char cQuote = quoteChar(zSchema);
967 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
968 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
969 }else{
970 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
971 }
drh2ce15c32017-07-11 13:34:40 +0000972 }
drh667a2a22018-01-02 00:04:37 +0000973 if( zName
974 && aPrefix[i][0]=='V'
975 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
976 ){
977 if( z==0 ){
dandcfbff92018-01-08 17:05:32 +0000978 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
drh667a2a22018-01-02 00:04:37 +0000979 }else{
dandcfbff92018-01-08 17:05:32 +0000980 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
drh667a2a22018-01-02 00:04:37 +0000981 }
dandcfbff92018-01-08 17:05:32 +0000982 free(zFake);
drhceba7922018-01-01 21:28:25 +0000983 }
984 if( z ){
985 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
986 return;
987 }
drh2ce15c32017-07-11 13:34:40 +0000988 }
989 }
990 }
991 sqlite3_result_value(pCtx, apVal[0]);
992}
993
994/*
995** The source code for several run-time loadable extensions is inserted
996** below by the ../tool/mkshellc.tcl script. Before processing that included
997** code, we need to override some macros to make the included program code
998** work here in the middle of this regular program.
999*/
1000#define SQLITE_EXTENSION_INIT1
drh89997982017-07-11 18:11:33 +00001001#define SQLITE_EXTENSION_INIT2(X) (void)(X)
drh2ce15c32017-07-11 13:34:40 +00001002
mistachkinacae8c32018-01-05 20:08:46 +00001003#if defined(_WIN32) && defined(_MSC_VER)
drh03491a12018-01-07 21:58:17 +00001004INCLUDE test_windirent.h
mistachkindfdfd8c2018-01-04 22:46:08 +00001005INCLUDE test_windirent.c
1006#define dirent DIRENT
mistachkindfdfd8c2018-01-04 22:46:08 +00001007#endif
drh2ce15c32017-07-11 13:34:40 +00001008INCLUDE ../ext/misc/shathree.c
1009INCLUDE ../ext/misc/fileio.c
drh56eb09b2017-07-11 13:59:07 +00001010INCLUDE ../ext/misc/completion.c
drh8682e122018-01-07 20:38:10 +00001011INCLUDE ../ext/misc/appendvfs.c
drh50b910a2019-01-21 14:55:03 +00001012INCLUDE ../ext/misc/memtrace.c
drhf05dd032020-04-14 15:53:58 +00001013INCLUDE ../ext/misc/uint.c
dan72afc3c2017-12-05 18:32:40 +00001014#ifdef SQLITE_HAVE_ZLIB
dan9ebfaad2017-12-26 20:39:58 +00001015INCLUDE ../ext/misc/zipfile.c
dand1b51d42017-12-16 19:11:26 +00001016INCLUDE ../ext/misc/sqlar.c
dan72afc3c2017-12-05 18:32:40 +00001017#endif
dan43efc182017-12-19 17:42:13 +00001018INCLUDE ../ext/expert/sqlite3expert.h
1019INCLUDE ../ext/expert/sqlite3expert.c
drh2ce15c32017-07-11 13:34:40 +00001020
dan1b162162019-04-27 20:15:15 +00001021#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
dan68cb86e2019-04-20 20:57:28 +00001022INCLUDE ../ext/misc/dbdata.c
dan1b162162019-04-27 20:15:15 +00001023#endif
dan68cb86e2019-04-20 20:57:28 +00001024
drh2ce15c32017-07-11 13:34:40 +00001025#if defined(SQLITE_ENABLE_SESSION)
1026/*
1027** State information for a single open session
1028*/
1029typedef struct OpenSession OpenSession;
1030struct OpenSession {
1031 char *zName; /* Symbolic name for this session */
1032 int nFilter; /* Number of xFilter rejection GLOB patterns */
1033 char **azFilter; /* Array of xFilter rejection GLOB patterns */
1034 sqlite3_session *p; /* The open session */
1035};
1036#endif
1037
1038/*
1039** Shell output mode information from before ".explain on",
1040** saved so that it can be restored by ".explain off"
1041*/
1042typedef struct SavedModeInfo SavedModeInfo;
1043struct SavedModeInfo {
1044 int valid; /* Is there legit data in here? */
1045 int mode; /* Mode prior to ".explain on" */
1046 int showHeader; /* The ".header" setting prior to ".explain on" */
1047 int colWidth[100]; /* Column widths prior to ".explain on" */
1048};
1049
dan43efc182017-12-19 17:42:13 +00001050typedef struct ExpertInfo ExpertInfo;
1051struct ExpertInfo {
1052 sqlite3expert *pExpert;
1053 int bVerbose;
1054};
1055
drh4b5345c2018-04-24 13:07:40 +00001056/* A single line in the EQP output */
1057typedef struct EQPGraphRow EQPGraphRow;
1058struct EQPGraphRow {
drhe2ca99c2018-05-02 00:33:43 +00001059 int iEqpId; /* ID for this row */
1060 int iParentId; /* ID of the parent row */
drh4b5345c2018-04-24 13:07:40 +00001061 EQPGraphRow *pNext; /* Next row in sequence */
1062 char zText[1]; /* Text to display for this row */
1063};
1064
1065/* All EQP output is collected into an instance of the following */
1066typedef struct EQPGraph EQPGraph;
1067struct EQPGraph {
1068 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
1069 EQPGraphRow *pLast; /* Last element of the pRow list */
1070 char zPrefix[100]; /* Graph prefix */
1071};
1072
drh2ce15c32017-07-11 13:34:40 +00001073/*
1074** State information about the database connection is contained in an
1075** instance of the following structure.
1076*/
1077typedef struct ShellState ShellState;
1078struct ShellState {
1079 sqlite3 *db; /* The database */
drh1fa6d9f2018-01-06 21:46:01 +00001080 u8 autoExplain; /* Automatically turn on .explain mode */
1081 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
drhe2ca99c2018-05-02 00:33:43 +00001082 u8 autoEQPtest; /* autoEQP is in test mode */
drhb4e50392019-01-26 15:40:04 +00001083 u8 autoEQPtrace; /* autoEQP is in trace mode */
drh1fa6d9f2018-01-06 21:46:01 +00001084 u8 statsOn; /* True to display memory stats before each finalize */
1085 u8 scanstatsOn; /* True to display scan stats before each finalize */
1086 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
drh13c20932018-01-10 21:41:55 +00001087 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
drh4b5345c2018-04-24 13:07:40 +00001088 u8 nEqpLevel; /* Depth of the EQP output graph */
drh707821f2018-12-05 13:39:06 +00001089 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
drh4b5345c2018-04-24 13:07:40 +00001090 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
drh2ce15c32017-07-11 13:34:40 +00001091 int outCount; /* Revert to stdout when reaching zero */
1092 int cnt; /* Number of records displayed so far */
drh2c8ee022018-12-13 18:59:30 +00001093 int lineno; /* Line number of last line read from in */
drh0933aad2019-11-18 17:46:38 +00001094 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
drh60379d42018-12-13 18:30:01 +00001095 FILE *in; /* Read commands from this stream */
drh2ce15c32017-07-11 13:34:40 +00001096 FILE *out; /* Write results here */
1097 FILE *traceOut; /* Output for sqlite3_trace() */
1098 int nErr; /* Number of errors seen */
1099 int mode; /* An output mode setting */
drh3c484e82018-01-10 22:27:21 +00001100 int modePrior; /* Saved mode */
drh2ce15c32017-07-11 13:34:40 +00001101 int cMode; /* temporary output mode for the current query */
1102 int normalMode; /* Output mode before ".explain on" */
1103 int writableSchema; /* True if PRAGMA writable_schema=ON */
1104 int showHeader; /* True to show column names in List or Column mode */
1105 int nCheck; /* Number of ".check" commands run */
drh3f83f592019-02-04 14:53:18 +00001106 unsigned nProgress; /* Number of progress callbacks encountered */
1107 unsigned mxProgress; /* Maximum progress callbacks before failing */
1108 unsigned flgProgress; /* Flags for the progress callback */
drh2ce15c32017-07-11 13:34:40 +00001109 unsigned shellFlgs; /* Various flags */
drh7a431002020-04-18 14:12:00 +00001110 unsigned priorShFlgs; /* Saved copy of flags */
drh6ca64482019-01-22 16:06:20 +00001111 sqlite3_int64 szMax; /* --maxsize argument to .open */
drh2ce15c32017-07-11 13:34:40 +00001112 char *zDestTable; /* Name of destination table when MODE_Insert */
drh13c20932018-01-10 21:41:55 +00001113 char *zTempFile; /* Temporary file that might need deleting */
drh2ce15c32017-07-11 13:34:40 +00001114 char zTestcase[30]; /* Name of current test case */
1115 char colSeparator[20]; /* Column separator character for several modes */
1116 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
drh3c484e82018-01-10 22:27:21 +00001117 char colSepPrior[20]; /* Saved column separator */
1118 char rowSepPrior[20]; /* Saved row separator */
drh2ce15c32017-07-11 13:34:40 +00001119 int colWidth[100]; /* Requested width of each column when in column mode*/
1120 int actualWidth[100]; /* Actual width of each column */
1121 char nullValue[20]; /* The text to print when a NULL comes back from
1122 ** the database */
1123 char outfile[FILENAME_MAX]; /* Filename for *out */
1124 const char *zDbFilename; /* name of the database file */
1125 char *zFreeOnClose; /* Filename to free when closing */
1126 const char *zVfs; /* Name of VFS to use */
1127 sqlite3_stmt *pStmt; /* Current statement if any. */
1128 FILE *pLog; /* Write log output here */
1129 int *aiIndent; /* Array of indents used in MODE_Explain */
1130 int nIndent; /* Size of array aiIndent[] */
1131 int iIndent; /* Index of current op in aiIndent[] */
drh4b5345c2018-04-24 13:07:40 +00001132 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
drh2ce15c32017-07-11 13:34:40 +00001133#if defined(SQLITE_ENABLE_SESSION)
1134 int nSession; /* Number of active sessions */
1135 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
1136#endif
dan43efc182017-12-19 17:42:13 +00001137 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
drh2ce15c32017-07-11 13:34:40 +00001138};
1139
drh1fa6d9f2018-01-06 21:46:01 +00001140
drhada70452017-12-21 21:02:27 +00001141/* Allowed values for ShellState.autoEQP
1142*/
drhe2ca99c2018-05-02 00:33:43 +00001143#define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1144#define AUTOEQP_on 1 /* Automatic EQP is on */
1145#define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1146#define AUTOEQP_full 3 /* Show full EXPLAIN */
drhada70452017-12-21 21:02:27 +00001147
drh1fa6d9f2018-01-06 21:46:01 +00001148/* Allowed values for ShellState.openMode
1149*/
drh60f34ae2018-10-30 13:19:49 +00001150#define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1151#define SHELL_OPEN_NORMAL 1 /* Normal database file */
1152#define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1153#define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1154#define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1155#define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
drh33746482018-12-13 15:06:26 +00001156#define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
drh1fa6d9f2018-01-06 21:46:01 +00001157
drh707821f2018-12-05 13:39:06 +00001158/* Allowed values for ShellState.eTraceType
1159*/
1160#define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
1161#define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
1162#define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
1163
drh3f83f592019-02-04 14:53:18 +00001164/* Bits in the ShellState.flgProgress variable */
drhfc4eeef2019-02-05 19:48:46 +00001165#define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
1166#define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
1167 ** callback limit is reached, and for each
1168 ** top-level SQL statement */
1169#define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
drh3f83f592019-02-04 14:53:18 +00001170
drh2ce15c32017-07-11 13:34:40 +00001171/*
1172** These are the allowed shellFlgs values
1173*/
drhb2a0f752017-08-28 15:51:35 +00001174#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1175#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1176#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1177#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1178#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1179#define SHFLG_CountChanges 0x00000020 /* .changes setting */
1180#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
drh2ce15c32017-07-11 13:34:40 +00001181
1182/*
1183** Macros for testing and setting shellFlgs
1184*/
1185#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1186#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1187#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1188
1189/*
1190** These are the allowed modes.
1191*/
1192#define MODE_Line 0 /* One column per line. Blank line between records */
1193#define MODE_Column 1 /* One record per line in neat columns */
1194#define MODE_List 2 /* One record per line with a separator */
1195#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1196#define MODE_Html 4 /* Generate an XHTML table */
1197#define MODE_Insert 5 /* Generate SQL "insert" statements */
1198#define MODE_Quote 6 /* Quote values as for SQL */
1199#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1200#define MODE_Csv 8 /* Quote strings, numbers are plain */
1201#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1202#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1203#define MODE_Pretty 11 /* Pretty-print schemas */
drh4b5345c2018-04-24 13:07:40 +00001204#define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
drh2ce15c32017-07-11 13:34:40 +00001205
1206static const char *modeDescr[] = {
1207 "line",
1208 "column",
1209 "list",
1210 "semi",
1211 "html",
1212 "insert",
1213 "quote",
1214 "tcl",
1215 "csv",
1216 "explain",
1217 "ascii",
1218 "prettyprint",
drh4b5345c2018-04-24 13:07:40 +00001219 "eqp"
drh2ce15c32017-07-11 13:34:40 +00001220};
1221
1222/*
1223** These are the column/row/line separators used by the various
1224** import/export modes.
1225*/
1226#define SEP_Column "|"
1227#define SEP_Row "\n"
1228#define SEP_Tab "\t"
1229#define SEP_Space " "
1230#define SEP_Comma ","
1231#define SEP_CrLf "\r\n"
1232#define SEP_Unit "\x1F"
1233#define SEP_Record "\x1E"
1234
1235/*
drh2ce15c32017-07-11 13:34:40 +00001236** A callback for the sqlite3_log() interface.
1237*/
1238static void shellLog(void *pArg, int iErrCode, const char *zMsg){
1239 ShellState *p = (ShellState*)pArg;
1240 if( p->pLog==0 ) return;
1241 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
1242 fflush(p->pLog);
1243}
1244
1245/*
drh634c70f2018-01-10 16:50:18 +00001246** SQL function: shell_putsnl(X)
1247**
1248** Write the text X to the screen (or whatever output is being directed)
1249** adding a newline at the end, and then return X.
1250*/
1251static void shellPutsFunc(
1252 sqlite3_context *pCtx,
1253 int nVal,
1254 sqlite3_value **apVal
1255){
1256 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
drhb9685182018-01-17 13:15:23 +00001257 (void)nVal;
drh634c70f2018-01-10 16:50:18 +00001258 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
1259 sqlite3_result_value(pCtx, apVal[0]);
1260}
1261
1262/*
drh97913132018-01-11 00:04:00 +00001263** SQL function: edit(VALUE)
1264** edit(VALUE,EDITOR)
1265**
1266** These steps:
1267**
1268** (1) Write VALUE into a temporary file.
1269** (2) Run program EDITOR on that temporary file.
1270** (3) Read the temporary file back and return its content as the result.
1271** (4) Delete the temporary file
1272**
1273** If the EDITOR argument is omitted, use the value in the VISUAL
1274** environment variable. If still there is no EDITOR, through an error.
1275**
1276** Also throw an error if the EDITOR program returns a non-zero exit code.
1277*/
drh04a28c32018-01-31 01:38:44 +00001278#ifndef SQLITE_NOHAVE_SYSTEM
drh97913132018-01-11 00:04:00 +00001279static void editFunc(
1280 sqlite3_context *context,
1281 int argc,
1282 sqlite3_value **argv
1283){
1284 const char *zEditor;
1285 char *zTempFile = 0;
1286 sqlite3 *db;
1287 char *zCmd = 0;
1288 int bBin;
1289 int rc;
drhf018fd52018-08-06 02:08:53 +00001290 int hasCRNL = 0;
drh97913132018-01-11 00:04:00 +00001291 FILE *f = 0;
1292 sqlite3_int64 sz;
1293 sqlite3_int64 x;
1294 unsigned char *p = 0;
1295
1296 if( argc==2 ){
1297 zEditor = (const char*)sqlite3_value_text(argv[1]);
1298 }else{
1299 zEditor = getenv("VISUAL");
1300 }
1301 if( zEditor==0 ){
1302 sqlite3_result_error(context, "no editor for edit()", -1);
1303 return;
1304 }
1305 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
1306 sqlite3_result_error(context, "NULL input to edit()", -1);
1307 return;
1308 }
1309 db = sqlite3_context_db_handle(context);
1310 zTempFile = 0;
1311 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
1312 if( zTempFile==0 ){
1313 sqlite3_uint64 r = 0;
1314 sqlite3_randomness(sizeof(r), &r);
1315 zTempFile = sqlite3_mprintf("temp%llx", r);
1316 if( zTempFile==0 ){
1317 sqlite3_result_error_nomem(context);
1318 return;
1319 }
1320 }
1321 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
drhf018fd52018-08-06 02:08:53 +00001322 /* When writing the file to be edited, do \n to \r\n conversions on systems
1323 ** that want \r\n line endings */
drh97913132018-01-11 00:04:00 +00001324 f = fopen(zTempFile, bBin ? "wb" : "w");
1325 if( f==0 ){
1326 sqlite3_result_error(context, "edit() cannot open temp file", -1);
1327 goto edit_func_end;
1328 }
1329 sz = sqlite3_value_bytes(argv[0]);
1330 if( bBin ){
dan4d02b5f2019-07-17 07:23:06 +00001331 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
drh97913132018-01-11 00:04:00 +00001332 }else{
drhf018fd52018-08-06 02:08:53 +00001333 const char *z = (const char*)sqlite3_value_text(argv[0]);
1334 /* Remember whether or not the value originally contained \r\n */
1335 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
dan4d02b5f2019-07-17 07:23:06 +00001336 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
drh97913132018-01-11 00:04:00 +00001337 }
1338 fclose(f);
1339 f = 0;
1340 if( x!=sz ){
1341 sqlite3_result_error(context, "edit() could not write the whole file", -1);
1342 goto edit_func_end;
1343 }
1344 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
1345 if( zCmd==0 ){
1346 sqlite3_result_error_nomem(context);
1347 goto edit_func_end;
1348 }
1349 rc = system(zCmd);
1350 sqlite3_free(zCmd);
1351 if( rc ){
1352 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
1353 goto edit_func_end;
1354 }
drhf018fd52018-08-06 02:08:53 +00001355 f = fopen(zTempFile, "rb");
drh97913132018-01-11 00:04:00 +00001356 if( f==0 ){
1357 sqlite3_result_error(context,
1358 "edit() cannot reopen temp file after edit", -1);
1359 goto edit_func_end;
1360 }
1361 fseek(f, 0, SEEK_END);
1362 sz = ftell(f);
1363 rewind(f);
drhee37f8b2019-08-23 23:05:32 +00001364 p = sqlite3_malloc64( sz+1 );
drh97913132018-01-11 00:04:00 +00001365 if( p==0 ){
1366 sqlite3_result_error_nomem(context);
1367 goto edit_func_end;
1368 }
dan4d02b5f2019-07-17 07:23:06 +00001369 x = fread(p, 1, (size_t)sz, f);
drh97913132018-01-11 00:04:00 +00001370 fclose(f);
1371 f = 0;
1372 if( x!=sz ){
1373 sqlite3_result_error(context, "could not read back the whole file", -1);
1374 goto edit_func_end;
1375 }
1376 if( bBin ){
mistachkinb71aa092018-01-23 00:05:18 +00001377 sqlite3_result_blob64(context, p, sz, sqlite3_free);
drh97913132018-01-11 00:04:00 +00001378 }else{
dan60bdcf52018-10-03 11:13:30 +00001379 sqlite3_int64 i, j;
drhf018fd52018-08-06 02:08:53 +00001380 if( hasCRNL ){
1381 /* If the original contains \r\n then do no conversions back to \n */
1382 j = sz;
1383 }else{
1384 /* If the file did not originally contain \r\n then convert any new
1385 ** \r\n back into \n */
1386 for(i=j=0; i<sz; i++){
1387 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
1388 p[j++] = p[i];
1389 }
1390 sz = j;
1391 p[sz] = 0;
1392 }
mistachkinb71aa092018-01-23 00:05:18 +00001393 sqlite3_result_text64(context, (const char*)p, sz,
1394 sqlite3_free, SQLITE_UTF8);
drh97913132018-01-11 00:04:00 +00001395 }
1396 p = 0;
1397
1398edit_func_end:
1399 if( f ) fclose(f);
1400 unlink(zTempFile);
1401 sqlite3_free(zTempFile);
1402 sqlite3_free(p);
1403}
drh04a28c32018-01-31 01:38:44 +00001404#endif /* SQLITE_NOHAVE_SYSTEM */
drh97913132018-01-11 00:04:00 +00001405
1406/*
drh3c484e82018-01-10 22:27:21 +00001407** Save or restore the current output mode
1408*/
1409static void outputModePush(ShellState *p){
1410 p->modePrior = p->mode;
drh7a431002020-04-18 14:12:00 +00001411 p->priorShFlgs = p->shellFlgs;
drh3c484e82018-01-10 22:27:21 +00001412 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
1413 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
1414}
1415static void outputModePop(ShellState *p){
1416 p->mode = p->modePrior;
drh7a431002020-04-18 14:12:00 +00001417 p->shellFlgs = p->priorShFlgs;
drh3c484e82018-01-10 22:27:21 +00001418 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
1419 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
1420}
1421
1422/*
drh2ce15c32017-07-11 13:34:40 +00001423** Output the given string as a hex-encoded blob (eg. X'1234' )
1424*/
1425static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
1426 int i;
1427 char *zBlob = (char *)pBlob;
1428 raw_printf(out,"X'");
1429 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
1430 raw_printf(out,"'");
1431}
1432
1433/*
1434** Find a string that is not found anywhere in z[]. Return a pointer
1435** to that string.
1436**
1437** Try to use zA and zB first. If both of those are already found in z[]
1438** then make up some string and store it in the buffer zBuf.
1439*/
1440static const char *unused_string(
1441 const char *z, /* Result must not appear anywhere in z */
1442 const char *zA, const char *zB, /* Try these first */
1443 char *zBuf /* Space to store a generated string */
1444){
1445 unsigned i = 0;
1446 if( strstr(z, zA)==0 ) return zA;
1447 if( strstr(z, zB)==0 ) return zB;
1448 do{
1449 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
1450 }while( strstr(z,zBuf)!=0 );
1451 return zBuf;
1452}
1453
1454/*
1455** Output the given string as a quoted string using SQL quoting conventions.
1456**
1457** See also: output_quoted_escaped_string()
1458*/
1459static void output_quoted_string(FILE *out, const char *z){
1460 int i;
1461 char c;
1462 setBinaryMode(out, 1);
1463 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1464 if( c==0 ){
1465 utf8_printf(out,"'%s'",z);
1466 }else{
1467 raw_printf(out, "'");
1468 while( *z ){
1469 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
1470 if( c=='\'' ) i++;
1471 if( i ){
1472 utf8_printf(out, "%.*s", i, z);
1473 z += i;
1474 }
1475 if( c=='\'' ){
1476 raw_printf(out, "'");
1477 continue;
1478 }
1479 if( c==0 ){
1480 break;
1481 }
1482 z++;
1483 }
1484 raw_printf(out, "'");
1485 }
1486 setTextMode(out, 1);
1487}
1488
1489/*
1490** Output the given string as a quoted string using SQL quoting conventions.
1491** Additionallly , escape the "\n" and "\r" characters so that they do not
1492** get corrupted by end-of-line translation facilities in some operating
1493** systems.
1494**
1495** This is like output_quoted_string() but with the addition of the \r\n
1496** escape mechanism.
1497*/
1498static void output_quoted_escaped_string(FILE *out, const char *z){
1499 int i;
1500 char c;
1501 setBinaryMode(out, 1);
1502 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1503 if( c==0 ){
1504 utf8_printf(out,"'%s'",z);
1505 }else{
1506 const char *zNL = 0;
1507 const char *zCR = 0;
1508 int nNL = 0;
1509 int nCR = 0;
1510 char zBuf1[20], zBuf2[20];
1511 for(i=0; z[i]; i++){
1512 if( z[i]=='\n' ) nNL++;
1513 if( z[i]=='\r' ) nCR++;
1514 }
1515 if( nNL ){
1516 raw_printf(out, "replace(");
1517 zNL = unused_string(z, "\\n", "\\012", zBuf1);
1518 }
1519 if( nCR ){
1520 raw_printf(out, "replace(");
1521 zCR = unused_string(z, "\\r", "\\015", zBuf2);
1522 }
1523 raw_printf(out, "'");
1524 while( *z ){
1525 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1526 if( c=='\'' ) i++;
1527 if( i ){
1528 utf8_printf(out, "%.*s", i, z);
1529 z += i;
1530 }
1531 if( c=='\'' ){
1532 raw_printf(out, "'");
1533 continue;
1534 }
1535 if( c==0 ){
1536 break;
1537 }
1538 z++;
1539 if( c=='\n' ){
1540 raw_printf(out, "%s", zNL);
1541 continue;
1542 }
1543 raw_printf(out, "%s", zCR);
1544 }
1545 raw_printf(out, "'");
1546 if( nCR ){
1547 raw_printf(out, ",'%s',char(13))", zCR);
1548 }
1549 if( nNL ){
1550 raw_printf(out, ",'%s',char(10))", zNL);
1551 }
1552 }
1553 setTextMode(out, 1);
1554}
1555
1556/*
1557** Output the given string as a quoted according to C or TCL quoting rules.
1558*/
1559static void output_c_string(FILE *out, const char *z){
1560 unsigned int c;
1561 fputc('"', out);
1562 while( (c = *(z++))!=0 ){
1563 if( c=='\\' ){
1564 fputc(c, out);
1565 fputc(c, out);
1566 }else if( c=='"' ){
1567 fputc('\\', out);
1568 fputc('"', out);
1569 }else if( c=='\t' ){
1570 fputc('\\', out);
1571 fputc('t', out);
1572 }else if( c=='\n' ){
1573 fputc('\\', out);
1574 fputc('n', out);
1575 }else if( c=='\r' ){
1576 fputc('\\', out);
1577 fputc('r', out);
1578 }else if( !isprint(c&0xff) ){
1579 raw_printf(out, "\\%03o", c&0xff);
1580 }else{
1581 fputc(c, out);
1582 }
1583 }
1584 fputc('"', out);
1585}
1586
1587/*
1588** Output the given string with characters that are special to
1589** HTML escaped.
1590*/
1591static void output_html_string(FILE *out, const char *z){
1592 int i;
1593 if( z==0 ) z = "";
1594 while( *z ){
1595 for(i=0; z[i]
1596 && z[i]!='<'
1597 && z[i]!='&'
1598 && z[i]!='>'
1599 && z[i]!='\"'
1600 && z[i]!='\'';
1601 i++){}
1602 if( i>0 ){
1603 utf8_printf(out,"%.*s",i,z);
1604 }
1605 if( z[i]=='<' ){
1606 raw_printf(out,"&lt;");
1607 }else if( z[i]=='&' ){
1608 raw_printf(out,"&amp;");
1609 }else if( z[i]=='>' ){
1610 raw_printf(out,"&gt;");
1611 }else if( z[i]=='\"' ){
1612 raw_printf(out,"&quot;");
1613 }else if( z[i]=='\'' ){
1614 raw_printf(out,"&#39;");
1615 }else{
1616 break;
1617 }
1618 z += i + 1;
1619 }
1620}
1621
1622/*
1623** If a field contains any character identified by a 1 in the following
1624** array, then the string must be quoted for CSV.
1625*/
1626static const char needCsvQuote[] = {
1627 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1628 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1629 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1630 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1631 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1632 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1633 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1634 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1635 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1636 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1637 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1638 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1639 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1640 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1641 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1642 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1643};
1644
1645/*
1646** Output a single term of CSV. Actually, p->colSeparator is used for
1647** the separator, which may or may not be a comma. p->nullValue is
1648** the null value. Strings are quoted if necessary. The separator
1649** is only issued if bSep is true.
1650*/
1651static void output_csv(ShellState *p, const char *z, int bSep){
1652 FILE *out = p->out;
1653 if( z==0 ){
1654 utf8_printf(out,"%s",p->nullValue);
1655 }else{
1656 int i;
1657 int nSep = strlen30(p->colSeparator);
1658 for(i=0; z[i]; i++){
1659 if( needCsvQuote[((unsigned char*)z)[i]]
1660 || (z[i]==p->colSeparator[0] &&
1661 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1662 i = 0;
1663 break;
1664 }
1665 }
1666 if( i==0 ){
drh9b7affc2017-11-26 02:14:18 +00001667 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
1668 utf8_printf(out, "%s", zQuoted);
1669 sqlite3_free(zQuoted);
drh2ce15c32017-07-11 13:34:40 +00001670 }else{
1671 utf8_printf(out, "%s", z);
1672 }
1673 }
1674 if( bSep ){
1675 utf8_printf(p->out, "%s", p->colSeparator);
1676 }
1677}
1678
drh2ce15c32017-07-11 13:34:40 +00001679/*
1680** This routine runs when the user presses Ctrl-C
1681*/
1682static void interrupt_handler(int NotUsed){
1683 UNUSED_PARAMETER(NotUsed);
1684 seenInterrupt++;
1685 if( seenInterrupt>2 ) exit(1);
1686 if( globalDb ) sqlite3_interrupt(globalDb);
1687}
mistachkinb4bab902017-10-27 17:09:44 +00001688
1689#if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1690/*
1691** This routine runs for console events (e.g. Ctrl-C) on Win32
1692*/
1693static BOOL WINAPI ConsoleCtrlHandler(
1694 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
1695){
1696 if( dwCtrlType==CTRL_C_EVENT ){
1697 interrupt_handler(0);
1698 return TRUE;
1699 }
1700 return FALSE;
1701}
drh2ce15c32017-07-11 13:34:40 +00001702#endif
1703
1704#ifndef SQLITE_OMIT_AUTHORIZATION
1705/*
1706** When the ".auth ON" is set, the following authorizer callback is
1707** invoked. It always returns SQLITE_OK.
1708*/
1709static int shellAuth(
1710 void *pClientData,
1711 int op,
1712 const char *zA1,
1713 const char *zA2,
1714 const char *zA3,
1715 const char *zA4
1716){
1717 ShellState *p = (ShellState*)pClientData;
1718 static const char *azAction[] = { 0,
1719 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
1720 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
1721 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
1722 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
1723 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
1724 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
1725 "PRAGMA", "READ", "SELECT",
1726 "TRANSACTION", "UPDATE", "ATTACH",
1727 "DETACH", "ALTER_TABLE", "REINDEX",
1728 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
1729 "FUNCTION", "SAVEPOINT", "RECURSIVE"
1730 };
1731 int i;
1732 const char *az[4];
1733 az[0] = zA1;
1734 az[1] = zA2;
1735 az[2] = zA3;
1736 az[3] = zA4;
1737 utf8_printf(p->out, "authorizer: %s", azAction[op]);
1738 for(i=0; i<4; i++){
1739 raw_printf(p->out, " ");
1740 if( az[i] ){
1741 output_c_string(p->out, az[i]);
1742 }else{
1743 raw_printf(p->out, "NULL");
1744 }
1745 }
1746 raw_printf(p->out, "\n");
1747 return SQLITE_OK;
1748}
1749#endif
1750
1751/*
1752** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
1753**
1754** This routine converts some CREATE TABLE statements for shadow tables
1755** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1756*/
1757static void printSchemaLine(FILE *out, const char *z, const char *zTail){
drh0a0536a2019-05-09 18:13:30 +00001758 if( z==0 ) return;
1759 if( zTail==0 ) return;
drh2ce15c32017-07-11 13:34:40 +00001760 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1761 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1762 }else{
1763 utf8_printf(out, "%s%s", z, zTail);
1764 }
1765}
1766static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1767 char c = z[n];
1768 z[n] = 0;
1769 printSchemaLine(out, z, zTail);
1770 z[n] = c;
1771}
1772
1773/*
drh11be81d2018-01-06 15:46:20 +00001774** Return true if string z[] has nothing but whitespace and comments to the
1775** end of the first line.
1776*/
1777static int wsToEol(const char *z){
1778 int i;
1779 for(i=0; z[i]; i++){
1780 if( z[i]=='\n' ) return 1;
1781 if( IsSpace(z[i]) ) continue;
1782 if( z[i]=='-' && z[i+1]=='-' ) return 1;
1783 return 0;
1784 }
1785 return 1;
1786}
drh4b5345c2018-04-24 13:07:40 +00001787
1788/*
1789** Add a new entry to the EXPLAIN QUERY PLAN data
1790*/
drhe2ca99c2018-05-02 00:33:43 +00001791static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
drh4b5345c2018-04-24 13:07:40 +00001792 EQPGraphRow *pNew;
1793 int nText = strlen30(zText);
drhe2ca99c2018-05-02 00:33:43 +00001794 if( p->autoEQPtest ){
1795 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
1796 }
drh4b5345c2018-04-24 13:07:40 +00001797 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
1798 if( pNew==0 ) shell_out_of_memory();
drhe2ca99c2018-05-02 00:33:43 +00001799 pNew->iEqpId = iEqpId;
1800 pNew->iParentId = p2;
drh4b5345c2018-04-24 13:07:40 +00001801 memcpy(pNew->zText, zText, nText+1);
1802 pNew->pNext = 0;
1803 if( p->sGraph.pLast ){
1804 p->sGraph.pLast->pNext = pNew;
1805 }else{
1806 p->sGraph.pRow = pNew;
1807 }
1808 p->sGraph.pLast = pNew;
1809}
1810
1811/*
1812** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1813** in p->sGraph.
1814*/
1815static void eqp_reset(ShellState *p){
1816 EQPGraphRow *pRow, *pNext;
1817 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
1818 pNext = pRow->pNext;
1819 sqlite3_free(pRow);
1820 }
1821 memset(&p->sGraph, 0, sizeof(p->sGraph));
1822}
1823
drhe2ca99c2018-05-02 00:33:43 +00001824/* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
drh4b5345c2018-04-24 13:07:40 +00001825** pOld, or return the first such line if pOld is NULL
1826*/
drhe2ca99c2018-05-02 00:33:43 +00001827static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
drh4b5345c2018-04-24 13:07:40 +00001828 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
drhe2ca99c2018-05-02 00:33:43 +00001829 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
drh4b5345c2018-04-24 13:07:40 +00001830 return pRow;
1831}
1832
drhe2ca99c2018-05-02 00:33:43 +00001833/* Render a single level of the graph that has iEqpId as its parent. Called
drh4b5345c2018-04-24 13:07:40 +00001834** recursively to render sublevels.
1835*/
drhe2ca99c2018-05-02 00:33:43 +00001836static void eqp_render_level(ShellState *p, int iEqpId){
drh4b5345c2018-04-24 13:07:40 +00001837 EQPGraphRow *pRow, *pNext;
drh4b5345c2018-04-24 13:07:40 +00001838 int n = strlen30(p->sGraph.zPrefix);
1839 char *z;
drhe2ca99c2018-05-02 00:33:43 +00001840 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
1841 pNext = eqp_next_row(p, iEqpId, pRow);
drh4b5345c2018-04-24 13:07:40 +00001842 z = pRow->zText;
drhe2754c12019-08-26 12:50:01 +00001843 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
1844 pNext ? "|--" : "`--", z);
drhe2188f02018-05-07 11:37:34 +00001845 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
drh4b5345c2018-04-24 13:07:40 +00001846 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
drhe2ca99c2018-05-02 00:33:43 +00001847 eqp_render_level(p, pRow->iEqpId);
drh4b5345c2018-04-24 13:07:40 +00001848 p->sGraph.zPrefix[n] = 0;
1849 }
1850 }
1851}
1852
1853/*
1854** Display and reset the EXPLAIN QUERY PLAN data
1855*/
1856static void eqp_render(ShellState *p){
1857 EQPGraphRow *pRow = p->sGraph.pRow;
1858 if( pRow ){
1859 if( pRow->zText[0]=='-' ){
1860 if( pRow->pNext==0 ){
1861 eqp_reset(p);
1862 return;
1863 }
1864 utf8_printf(p->out, "%s\n", pRow->zText+3);
1865 p->sGraph.pRow = pRow->pNext;
1866 sqlite3_free(pRow);
1867 }else{
1868 utf8_printf(p->out, "QUERY PLAN\n");
1869 }
1870 p->sGraph.zPrefix[0] = 0;
1871 eqp_render_level(p, 0);
1872 eqp_reset(p);
1873 }
1874}
drh11be81d2018-01-06 15:46:20 +00001875
drh569b1d92019-02-05 20:51:41 +00001876#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh11be81d2018-01-06 15:46:20 +00001877/*
drh3f83f592019-02-04 14:53:18 +00001878** Progress handler callback.
1879*/
1880static int progress_handler(void *pClientData) {
1881 ShellState *p = (ShellState*)pClientData;
1882 p->nProgress++;
1883 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
1884 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
drhfc4eeef2019-02-05 19:48:46 +00001885 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
1886 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
drh3f83f592019-02-04 14:53:18 +00001887 return 1;
1888 }
drhfc4eeef2019-02-05 19:48:46 +00001889 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
drh3f83f592019-02-04 14:53:18 +00001890 raw_printf(p->out, "Progress %u\n", p->nProgress);
1891 }
1892 return 0;
1893}
drh569b1d92019-02-05 20:51:41 +00001894#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
drh3f83f592019-02-04 14:53:18 +00001895
1896/*
drh2ce15c32017-07-11 13:34:40 +00001897** This is the callback routine that the shell
1898** invokes for each row of a query result.
1899*/
1900static int shell_callback(
1901 void *pArg,
1902 int nArg, /* Number of result columns */
1903 char **azArg, /* Text of each result column */
1904 char **azCol, /* Column names */
1905 int *aiType /* Column types */
1906){
1907 int i;
1908 ShellState *p = (ShellState*)pArg;
1909
drhb3c45232017-08-28 14:33:27 +00001910 if( azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00001911 switch( p->cMode ){
1912 case MODE_Line: {
1913 int w = 5;
1914 if( azArg==0 ) break;
1915 for(i=0; i<nArg; i++){
1916 int len = strlen30(azCol[i] ? azCol[i] : "");
1917 if( len>w ) w = len;
1918 }
1919 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1920 for(i=0; i<nArg; i++){
1921 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1922 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1923 }
1924 break;
1925 }
1926 case MODE_Explain:
1927 case MODE_Column: {
1928 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1929 const int *colWidth;
1930 int showHdr;
1931 char *rowSep;
drha015d3c2019-10-26 16:02:34 +00001932 int nWidth;
drh2ce15c32017-07-11 13:34:40 +00001933 if( p->cMode==MODE_Column ){
1934 colWidth = p->colWidth;
drha015d3c2019-10-26 16:02:34 +00001935 nWidth = ArraySize(p->colWidth);
drh2ce15c32017-07-11 13:34:40 +00001936 showHdr = p->showHeader;
1937 rowSep = p->rowSeparator;
1938 }else{
1939 colWidth = aExplainWidths;
drha015d3c2019-10-26 16:02:34 +00001940 nWidth = ArraySize(aExplainWidths);
drh2ce15c32017-07-11 13:34:40 +00001941 showHdr = 1;
1942 rowSep = SEP_Row;
1943 }
1944 if( p->cnt++==0 ){
1945 for(i=0; i<nArg; i++){
1946 int w, n;
drha015d3c2019-10-26 16:02:34 +00001947 if( i<nWidth ){
drh2ce15c32017-07-11 13:34:40 +00001948 w = colWidth[i];
1949 }else{
1950 w = 0;
1951 }
1952 if( w==0 ){
1953 w = strlenChar(azCol[i] ? azCol[i] : "");
1954 if( w<10 ) w = 10;
1955 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1956 if( w<n ) w = n;
1957 }
1958 if( i<ArraySize(p->actualWidth) ){
1959 p->actualWidth[i] = w;
1960 }
1961 if( showHdr ){
1962 utf8_width_print(p->out, w, azCol[i]);
1963 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
1964 }
1965 }
1966 if( showHdr ){
1967 for(i=0; i<nArg; i++){
1968 int w;
1969 if( i<ArraySize(p->actualWidth) ){
1970 w = p->actualWidth[i];
1971 if( w<0 ) w = -w;
1972 }else{
1973 w = 10;
1974 }
1975 utf8_printf(p->out,"%-*.*s%s",w,w,
1976 "----------------------------------------------------------"
1977 "----------------------------------------------------------",
1978 i==nArg-1 ? rowSep : " ");
1979 }
1980 }
1981 }
1982 if( azArg==0 ) break;
1983 for(i=0; i<nArg; i++){
1984 int w;
1985 if( i<ArraySize(p->actualWidth) ){
1986 w = p->actualWidth[i];
1987 }else{
1988 w = 10;
1989 }
1990 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1991 w = strlenChar(azArg[i]);
1992 }
1993 if( i==1 && p->aiIndent && p->pStmt ){
1994 if( p->iIndent<p->nIndent ){
1995 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1996 }
1997 p->iIndent++;
1998 }
1999 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
2000 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
2001 }
2002 break;
2003 }
2004 case MODE_Semi: { /* .schema and .fullschema output */
2005 printSchemaLine(p->out, azArg[0], ";\n");
2006 break;
2007 }
2008 case MODE_Pretty: { /* .schema and .fullschema with --indent */
2009 char *z;
2010 int j;
2011 int nParen = 0;
2012 char cEnd = 0;
2013 char c;
2014 int nLine = 0;
2015 assert( nArg==1 );
2016 if( azArg[0]==0 ) break;
2017 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
2018 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
2019 ){
2020 utf8_printf(p->out, "%s;\n", azArg[0]);
2021 break;
2022 }
2023 z = sqlite3_mprintf("%s", azArg[0]);
2024 j = 0;
2025 for(i=0; IsSpace(z[i]); i++){}
2026 for(; (c = z[i])!=0; i++){
2027 if( IsSpace(c) ){
drhc3cbd672017-10-05 19:12:10 +00002028 if( z[j-1]=='\r' ) z[j-1] = '\n';
drh2ce15c32017-07-11 13:34:40 +00002029 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
2030 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
2031 j--;
2032 }
2033 z[j++] = c;
2034 }
2035 while( j>0 && IsSpace(z[j-1]) ){ j--; }
2036 z[j] = 0;
2037 if( strlen30(z)>=79 ){
drhe2754c12019-08-26 12:50:01 +00002038 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
drh2ce15c32017-07-11 13:34:40 +00002039 if( c==cEnd ){
2040 cEnd = 0;
2041 }else if( c=='"' || c=='\'' || c=='`' ){
2042 cEnd = c;
2043 }else if( c=='[' ){
2044 cEnd = ']';
drh11be81d2018-01-06 15:46:20 +00002045 }else if( c=='-' && z[i+1]=='-' ){
2046 cEnd = '\n';
drh2ce15c32017-07-11 13:34:40 +00002047 }else if( c=='(' ){
2048 nParen++;
2049 }else if( c==')' ){
2050 nParen--;
2051 if( nLine>0 && nParen==0 && j>0 ){
2052 printSchemaLineN(p->out, z, j, "\n");
2053 j = 0;
2054 }
2055 }
2056 z[j++] = c;
drh11be81d2018-01-06 15:46:20 +00002057 if( nParen==1 && cEnd==0
2058 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
2059 ){
drh2ce15c32017-07-11 13:34:40 +00002060 if( c=='\n' ) j--;
2061 printSchemaLineN(p->out, z, j, "\n ");
2062 j = 0;
2063 nLine++;
2064 while( IsSpace(z[i+1]) ){ i++; }
2065 }
2066 }
2067 z[j] = 0;
2068 }
2069 printSchemaLine(p->out, z, ";\n");
2070 sqlite3_free(z);
2071 break;
2072 }
2073 case MODE_List: {
2074 if( p->cnt++==0 && p->showHeader ){
2075 for(i=0; i<nArg; i++){
2076 utf8_printf(p->out,"%s%s",azCol[i],
2077 i==nArg-1 ? p->rowSeparator : p->colSeparator);
2078 }
2079 }
2080 if( azArg==0 ) break;
2081 for(i=0; i<nArg; i++){
2082 char *z = azArg[i];
2083 if( z==0 ) z = p->nullValue;
2084 utf8_printf(p->out, "%s", z);
2085 if( i<nArg-1 ){
2086 utf8_printf(p->out, "%s", p->colSeparator);
2087 }else{
2088 utf8_printf(p->out, "%s", p->rowSeparator);
2089 }
2090 }
2091 break;
2092 }
2093 case MODE_Html: {
2094 if( p->cnt++==0 && p->showHeader ){
2095 raw_printf(p->out,"<TR>");
2096 for(i=0; i<nArg; i++){
2097 raw_printf(p->out,"<TH>");
2098 output_html_string(p->out, azCol[i]);
2099 raw_printf(p->out,"</TH>\n");
2100 }
2101 raw_printf(p->out,"</TR>\n");
2102 }
2103 if( azArg==0 ) break;
2104 raw_printf(p->out,"<TR>");
2105 for(i=0; i<nArg; i++){
2106 raw_printf(p->out,"<TD>");
2107 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2108 raw_printf(p->out,"</TD>\n");
2109 }
2110 raw_printf(p->out,"</TR>\n");
2111 break;
2112 }
2113 case MODE_Tcl: {
2114 if( p->cnt++==0 && p->showHeader ){
2115 for(i=0; i<nArg; i++){
2116 output_c_string(p->out,azCol[i] ? azCol[i] : "");
2117 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2118 }
2119 utf8_printf(p->out, "%s", p->rowSeparator);
2120 }
2121 if( azArg==0 ) break;
2122 for(i=0; i<nArg; i++){
2123 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
2124 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
2125 }
2126 utf8_printf(p->out, "%s", p->rowSeparator);
2127 break;
2128 }
2129 case MODE_Csv: {
2130 setBinaryMode(p->out, 1);
2131 if( p->cnt++==0 && p->showHeader ){
2132 for(i=0; i<nArg; i++){
2133 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
2134 }
2135 utf8_printf(p->out, "%s", p->rowSeparator);
2136 }
2137 if( nArg>0 ){
2138 for(i=0; i<nArg; i++){
2139 output_csv(p, azArg[i], i<nArg-1);
2140 }
2141 utf8_printf(p->out, "%s", p->rowSeparator);
2142 }
2143 setTextMode(p->out, 1);
2144 break;
2145 }
2146 case MODE_Insert: {
2147 if( azArg==0 ) break;
2148 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
2149 if( p->showHeader ){
2150 raw_printf(p->out,"(");
2151 for(i=0; i<nArg; i++){
2152 if( i>0 ) raw_printf(p->out, ",");
2153 if( quoteChar(azCol[i]) ){
2154 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
2155 utf8_printf(p->out, "%s", z);
2156 sqlite3_free(z);
2157 }else{
2158 raw_printf(p->out, "%s", azCol[i]);
2159 }
2160 }
2161 raw_printf(p->out,")");
2162 }
2163 p->cnt++;
2164 for(i=0; i<nArg; i++){
2165 raw_printf(p->out, i>0 ? "," : " VALUES(");
2166 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2167 utf8_printf(p->out,"NULL");
2168 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2169 if( ShellHasFlag(p, SHFLG_Newlines) ){
2170 output_quoted_string(p->out, azArg[i]);
2171 }else{
2172 output_quoted_escaped_string(p->out, azArg[i]);
2173 }
2174 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2175 utf8_printf(p->out,"%s", azArg[i]);
2176 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2177 char z[50];
2178 double r = sqlite3_column_double(p->pStmt, i);
drh2f1f8802018-06-13 17:19:20 +00002179 sqlite3_uint64 ur;
2180 memcpy(&ur,&r,sizeof(r));
2181 if( ur==0x7ff0000000000000LL ){
2182 raw_printf(p->out, "1e999");
2183 }else if( ur==0xfff0000000000000LL ){
2184 raw_printf(p->out, "-1e999");
2185 }else{
2186 sqlite3_snprintf(50,z,"%!.20g", r);
2187 raw_printf(p->out, "%s", z);
2188 }
drh2ce15c32017-07-11 13:34:40 +00002189 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2190 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2191 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2192 output_hex_blob(p->out, pBlob, nBlob);
2193 }else if( isNumber(azArg[i], 0) ){
2194 utf8_printf(p->out,"%s", azArg[i]);
2195 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
2196 output_quoted_string(p->out, azArg[i]);
2197 }else{
2198 output_quoted_escaped_string(p->out, azArg[i]);
2199 }
2200 }
2201 raw_printf(p->out,");\n");
2202 break;
2203 }
2204 case MODE_Quote: {
2205 if( azArg==0 ) break;
2206 if( p->cnt==0 && p->showHeader ){
2207 for(i=0; i<nArg; i++){
2208 if( i>0 ) raw_printf(p->out, ",");
2209 output_quoted_string(p->out, azCol[i]);
2210 }
2211 raw_printf(p->out,"\n");
2212 }
2213 p->cnt++;
2214 for(i=0; i<nArg; i++){
2215 if( i>0 ) raw_printf(p->out, ",");
2216 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
2217 utf8_printf(p->out,"NULL");
2218 }else if( aiType && aiType[i]==SQLITE_TEXT ){
2219 output_quoted_string(p->out, azArg[i]);
2220 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
2221 utf8_printf(p->out,"%s", azArg[i]);
2222 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
2223 char z[50];
2224 double r = sqlite3_column_double(p->pStmt, i);
2225 sqlite3_snprintf(50,z,"%!.20g", r);
2226 raw_printf(p->out, "%s", z);
2227 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
2228 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
2229 int nBlob = sqlite3_column_bytes(p->pStmt, i);
2230 output_hex_blob(p->out, pBlob, nBlob);
2231 }else if( isNumber(azArg[i], 0) ){
2232 utf8_printf(p->out,"%s", azArg[i]);
2233 }else{
2234 output_quoted_string(p->out, azArg[i]);
2235 }
2236 }
2237 raw_printf(p->out,"\n");
2238 break;
2239 }
2240 case MODE_Ascii: {
2241 if( p->cnt++==0 && p->showHeader ){
2242 for(i=0; i<nArg; i++){
2243 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2244 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
2245 }
2246 utf8_printf(p->out, "%s", p->rowSeparator);
2247 }
2248 if( azArg==0 ) break;
2249 for(i=0; i<nArg; i++){
2250 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
2251 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
2252 }
2253 utf8_printf(p->out, "%s", p->rowSeparator);
2254 break;
2255 }
drh4b5345c2018-04-24 13:07:40 +00002256 case MODE_EQP: {
drhe2ca99c2018-05-02 00:33:43 +00002257 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
drh4b5345c2018-04-24 13:07:40 +00002258 break;
2259 }
drh2ce15c32017-07-11 13:34:40 +00002260 }
2261 return 0;
2262}
2263
2264/*
2265** This is the callback routine that the SQLite library
2266** invokes for each row of a query result.
2267*/
2268static int callback(void *pArg, int nArg, char **azArg, char **azCol){
2269 /* since we don't have type info, call the shell_callback with a NULL value */
2270 return shell_callback(pArg, nArg, azArg, azCol, NULL);
2271}
2272
2273/*
2274** This is the callback routine from sqlite3_exec() that appends all
2275** output onto the end of a ShellText object.
2276*/
2277static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
2278 ShellText *p = (ShellText*)pArg;
2279 int i;
2280 UNUSED_PARAMETER(az);
drhb3c45232017-08-28 14:33:27 +00002281 if( azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00002282 if( p->n ) appendText(p, "|", 0);
2283 for(i=0; i<nArg; i++){
2284 if( i ) appendText(p, ",", 0);
2285 if( azArg[i] ) appendText(p, azArg[i], 0);
2286 }
2287 return 0;
2288}
2289
2290/*
2291** Generate an appropriate SELFTEST table in the main database.
2292*/
2293static void createSelftestTable(ShellState *p){
2294 char *zErrMsg = 0;
2295 sqlite3_exec(p->db,
2296 "SAVEPOINT selftest_init;\n"
2297 "CREATE TABLE IF NOT EXISTS selftest(\n"
2298 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2299 " op TEXT,\n" /* Operator: memo run */
2300 " cmd TEXT,\n" /* Command text */
2301 " ans TEXT\n" /* Desired answer */
2302 ");"
2303 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2304 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2305 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2306 " 'memo','Tests generated by --init');\n"
2307 "INSERT INTO [_shell$self]\n"
2308 " SELECT 'run',\n"
2309 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2310 "FROM sqlite_master ORDER BY 2'',224))',\n"
2311 " hex(sha3_query('SELECT type,name,tbl_name,sql "
2312 "FROM sqlite_master ORDER BY 2',224));\n"
2313 "INSERT INTO [_shell$self]\n"
2314 " SELECT 'run',"
2315 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2316 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2317 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2318 " FROM (\n"
2319 " SELECT name FROM sqlite_master\n"
2320 " WHERE type='table'\n"
2321 " AND name<>'selftest'\n"
2322 " AND coalesce(rootpage,0)>0\n"
2323 " )\n"
2324 " ORDER BY name;\n"
2325 "INSERT INTO [_shell$self]\n"
2326 " VALUES('run','PRAGMA integrity_check','ok');\n"
2327 "INSERT INTO selftest(tno,op,cmd,ans)"
2328 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2329 "DROP TABLE [_shell$self];"
2330 ,0,0,&zErrMsg);
2331 if( zErrMsg ){
2332 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
2333 sqlite3_free(zErrMsg);
2334 }
2335 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
2336}
2337
2338
2339/*
2340** Set the destination table field of the ShellState structure to
2341** the name of the table given. Escape any quote characters in the
2342** table name.
2343*/
2344static void set_table_name(ShellState *p, const char *zName){
2345 int i, n;
mistachkin2158a0c2017-09-09 00:51:36 +00002346 char cQuote;
drh2ce15c32017-07-11 13:34:40 +00002347 char *z;
2348
2349 if( p->zDestTable ){
2350 free(p->zDestTable);
2351 p->zDestTable = 0;
2352 }
2353 if( zName==0 ) return;
2354 cQuote = quoteChar(zName);
2355 n = strlen30(zName);
2356 if( cQuote ) n += n+2;
2357 z = p->zDestTable = malloc( n+1 );
drh4b5345c2018-04-24 13:07:40 +00002358 if( z==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00002359 n = 0;
2360 if( cQuote ) z[n++] = cQuote;
2361 for(i=0; zName[i]; i++){
2362 z[n++] = zName[i];
2363 if( zName[i]==cQuote ) z[n++] = cQuote;
2364 }
2365 if( cQuote ) z[n++] = cQuote;
2366 z[n] = 0;
2367}
2368
2369
2370/*
2371** Execute a query statement that will generate SQL output. Print
2372** the result columns, comma-separated, on a line and then add a
2373** semicolon terminator to the end of that line.
2374**
2375** If the number of columns is 1 and that column contains text "--"
2376** then write the semicolon on a separate line. That way, if a
2377** "--" comment occurs at the end of the statement, the comment
2378** won't consume the semicolon terminator.
2379*/
2380static int run_table_dump_query(
2381 ShellState *p, /* Query context */
drh8e9297f2020-03-25 12:50:13 +00002382 const char *zSelect /* SELECT statement to extract content */
drh2ce15c32017-07-11 13:34:40 +00002383){
2384 sqlite3_stmt *pSelect;
2385 int rc;
2386 int nResult;
2387 int i;
2388 const char *z;
2389 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
2390 if( rc!=SQLITE_OK || !pSelect ){
2391 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2392 sqlite3_errmsg(p->db));
2393 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2394 return rc;
2395 }
2396 rc = sqlite3_step(pSelect);
2397 nResult = sqlite3_column_count(pSelect);
2398 while( rc==SQLITE_ROW ){
drh2ce15c32017-07-11 13:34:40 +00002399 z = (const char*)sqlite3_column_text(pSelect, 0);
2400 utf8_printf(p->out, "%s", z);
2401 for(i=1; i<nResult; i++){
2402 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
2403 }
2404 if( z==0 ) z = "";
2405 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
2406 if( z[0] ){
2407 raw_printf(p->out, "\n;\n");
2408 }else{
2409 raw_printf(p->out, ";\n");
2410 }
2411 rc = sqlite3_step(pSelect);
2412 }
2413 rc = sqlite3_finalize(pSelect);
2414 if( rc!=SQLITE_OK ){
2415 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
2416 sqlite3_errmsg(p->db));
2417 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
2418 }
2419 return rc;
2420}
2421
2422/*
2423** Allocate space and save off current error string.
2424*/
2425static char *save_err_msg(
2426 sqlite3 *db /* Database to query */
2427){
2428 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
2429 char *zErrMsg = sqlite3_malloc64(nErrMsg);
2430 if( zErrMsg ){
2431 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
2432 }
2433 return zErrMsg;
2434}
2435
2436#ifdef __linux__
2437/*
2438** Attempt to display I/O stats on Linux using /proc/PID/io
2439*/
2440static void displayLinuxIoStats(FILE *out){
2441 FILE *in;
2442 char z[200];
2443 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
2444 in = fopen(z, "rb");
2445 if( in==0 ) return;
2446 while( fgets(z, sizeof(z), in)!=0 ){
2447 static const struct {
2448 const char *zPattern;
2449 const char *zDesc;
2450 } aTrans[] = {
2451 { "rchar: ", "Bytes received by read():" },
2452 { "wchar: ", "Bytes sent to write():" },
2453 { "syscr: ", "Read() system calls:" },
2454 { "syscw: ", "Write() system calls:" },
2455 { "read_bytes: ", "Bytes read from storage:" },
2456 { "write_bytes: ", "Bytes written to storage:" },
2457 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
2458 };
2459 int i;
2460 for(i=0; i<ArraySize(aTrans); i++){
drhaf2770f2018-01-05 14:55:43 +00002461 int n = strlen30(aTrans[i].zPattern);
drh2ce15c32017-07-11 13:34:40 +00002462 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
2463 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
2464 break;
2465 }
2466 }
2467 }
2468 fclose(in);
2469}
2470#endif
2471
2472/*
2473** Display a single line of status using 64-bit values.
2474*/
2475static void displayStatLine(
2476 ShellState *p, /* The shell context */
2477 char *zLabel, /* Label for this one line */
2478 char *zFormat, /* Format for the result */
2479 int iStatusCtrl, /* Which status to display */
2480 int bReset /* True to reset the stats */
2481){
2482 sqlite3_int64 iCur = -1;
2483 sqlite3_int64 iHiwtr = -1;
2484 int i, nPercent;
2485 char zLine[200];
2486 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
2487 for(i=0, nPercent=0; zFormat[i]; i++){
2488 if( zFormat[i]=='%' ) nPercent++;
2489 }
2490 if( nPercent>1 ){
2491 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
2492 }else{
2493 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
2494 }
2495 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
2496}
2497
2498/*
2499** Display memory stats.
2500*/
2501static int display_stats(
2502 sqlite3 *db, /* Database to query */
2503 ShellState *pArg, /* Pointer to ShellState */
2504 int bReset /* True to reset the stats */
2505){
2506 int iCur;
2507 int iHiwtr;
drh393344f2018-03-09 16:37:05 +00002508 FILE *out;
2509 if( pArg==0 || pArg->out==0 ) return 0;
2510 out = pArg->out;
drh2ce15c32017-07-11 13:34:40 +00002511
drh393344f2018-03-09 16:37:05 +00002512 if( pArg->pStmt && (pArg->statsOn & 2) ){
2513 int nCol, i, x;
2514 sqlite3_stmt *pStmt = pArg->pStmt;
2515 char z[100];
2516 nCol = sqlite3_column_count(pStmt);
2517 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
2518 for(i=0; i<nCol; i++){
2519 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
2520 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
drh929cce82018-03-17 16:26:36 +00002521#ifndef SQLITE_OMIT_DECLTYPE
drh393344f2018-03-09 16:37:05 +00002522 sqlite3_snprintf(30, z+x, "declared type:");
2523 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
drh929cce82018-03-17 16:26:36 +00002524#endif
2525#ifdef SQLITE_ENABLE_COLUMN_METADATA
drh393344f2018-03-09 16:37:05 +00002526 sqlite3_snprintf(30, z+x, "database name:");
2527 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
2528 sqlite3_snprintf(30, z+x, "table name:");
2529 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
2530 sqlite3_snprintf(30, z+x, "origin name:");
2531 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
drh929cce82018-03-17 16:26:36 +00002532#endif
drh2ce15c32017-07-11 13:34:40 +00002533 }
drh929cce82018-03-17 16:26:36 +00002534 }
drh2ce15c32017-07-11 13:34:40 +00002535
drh393344f2018-03-09 16:37:05 +00002536 displayStatLine(pArg, "Memory Used:",
2537 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
2538 displayStatLine(pArg, "Number of Outstanding Allocations:",
2539 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
2540 if( pArg->shellFlgs & SHFLG_Pagecache ){
2541 displayStatLine(pArg, "Number of Pcache Pages Used:",
2542 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
2543 }
2544 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
2545 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
2546 displayStatLine(pArg, "Largest Allocation:",
2547 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
2548 displayStatLine(pArg, "Largest Pcache Allocation:",
2549 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
2550#ifdef YYTRACKMAXSTACKDEPTH
2551 displayStatLine(pArg, "Deepest Parser Stack:",
2552 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
2553#endif
2554
2555 if( db ){
drh2ce15c32017-07-11 13:34:40 +00002556 if( pArg->shellFlgs & SHFLG_Lookaside ){
2557 iHiwtr = iCur = -1;
2558 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
2559 &iCur, &iHiwtr, bReset);
2560 raw_printf(pArg->out,
2561 "Lookaside Slots Used: %d (max %d)\n",
2562 iCur, iHiwtr);
2563 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
2564 &iCur, &iHiwtr, bReset);
2565 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
2566 iHiwtr);
2567 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
2568 &iCur, &iHiwtr, bReset);
2569 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
2570 iHiwtr);
2571 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
2572 &iCur, &iHiwtr, bReset);
2573 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
2574 iHiwtr);
2575 }
2576 iHiwtr = iCur = -1;
2577 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
2578 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
2579 iCur);
2580 iHiwtr = iCur = -1;
2581 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
2582 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
2583 iHiwtr = iCur = -1;
2584 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
2585 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
2586 iHiwtr = iCur = -1;
2587 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
2588 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
2589 iHiwtr = iCur = -1;
drhffc78a42018-03-14 14:53:50 +00002590 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
2591 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
2592 iHiwtr = iCur = -1;
drh2ce15c32017-07-11 13:34:40 +00002593 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
2594 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
2595 iCur);
2596 iHiwtr = iCur = -1;
2597 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
2598 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
2599 iCur);
2600 }
2601
drh393344f2018-03-09 16:37:05 +00002602 if( pArg->pStmt ){
drh2ce15c32017-07-11 13:34:40 +00002603 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
2604 bReset);
2605 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
2606 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
2607 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
2608 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
2609 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
2610 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
2611 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
drhe2754c12019-08-26 12:50:01 +00002612 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
drh393344f2018-03-09 16:37:05 +00002613 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
2614 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
2615 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
2616 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
2617 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
drh2ce15c32017-07-11 13:34:40 +00002618 }
2619
2620#ifdef __linux__
2621 displayLinuxIoStats(pArg->out);
2622#endif
2623
2624 /* Do not remove this machine readable comment: extra-stats-output-here */
2625
2626 return 0;
2627}
2628
2629/*
2630** Display scan stats.
2631*/
2632static void display_scanstats(
2633 sqlite3 *db, /* Database to query */
2634 ShellState *pArg /* Pointer to ShellState */
2635){
2636#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2637 UNUSED_PARAMETER(db);
2638 UNUSED_PARAMETER(pArg);
2639#else
2640 int i, k, n, mx;
2641 raw_printf(pArg->out, "-------- scanstats --------\n");
2642 mx = 0;
2643 for(k=0; k<=mx; k++){
2644 double rEstLoop = 1.0;
2645 for(i=n=0; 1; i++){
2646 sqlite3_stmt *p = pArg->pStmt;
2647 sqlite3_int64 nLoop, nVisit;
2648 double rEst;
2649 int iSid;
2650 const char *zExplain;
2651 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
2652 break;
2653 }
2654 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2655 if( iSid>mx ) mx = iSid;
2656 if( iSid!=k ) continue;
2657 if( n==0 ){
2658 rEstLoop = (double)nLoop;
2659 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2660 }
2661 n++;
2662 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2663 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2664 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2665 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2666 rEstLoop *= rEst;
2667 raw_printf(pArg->out,
2668 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2669 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2670 );
2671 }
2672 }
2673 raw_printf(pArg->out, "---------------------------\n");
2674#endif
2675}
2676
2677/*
2678** Parameter azArray points to a zero-terminated array of strings. zStr
2679** points to a single nul-terminated string. Return non-zero if zStr
2680** is equal, according to strcmp(), to any of the strings in the array.
2681** Otherwise, return zero.
2682*/
2683static int str_in_array(const char *zStr, const char **azArray){
2684 int i;
2685 for(i=0; azArray[i]; i++){
2686 if( 0==strcmp(zStr, azArray[i]) ) return 1;
2687 }
2688 return 0;
2689}
2690
2691/*
2692** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2693** and populate the ShellState.aiIndent[] array with the number of
2694** spaces each opcode should be indented before it is output.
2695**
2696** The indenting rules are:
2697**
2698** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2699** all opcodes that occur between the p2 jump destination and the opcode
2700** itself by 2 spaces.
2701**
2702** * For each "Goto", if the jump destination is earlier in the program
2703** and ends on one of:
2704** Yield SeekGt SeekLt RowSetRead Rewind
2705** or if the P1 parameter is one instead of zero,
2706** then indent all opcodes between the earlier instruction
2707** and "Goto" by 2 spaces.
2708*/
2709static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2710 const char *zSql; /* The text of the SQL statement */
2711 const char *z; /* Used to check if this is an EXPLAIN */
2712 int *abYield = 0; /* True if op is an OP_Yield */
2713 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
2714 int iOp; /* Index of operation in p->aiIndent[] */
2715
drhf1949b62018-06-07 17:32:59 +00002716 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
drh2ce15c32017-07-11 13:34:40 +00002717 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2718 "Rewind", 0 };
2719 const char *azGoto[] = { "Goto", 0 };
2720
2721 /* Try to figure out if this is really an EXPLAIN statement. If this
2722 ** cannot be verified, return early. */
2723 if( sqlite3_column_count(pSql)!=8 ){
2724 p->cMode = p->mode;
2725 return;
2726 }
2727 zSql = sqlite3_sql(pSql);
2728 if( zSql==0 ) return;
2729 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2730 if( sqlite3_strnicmp(z, "explain", 7) ){
2731 p->cMode = p->mode;
2732 return;
2733 }
2734
2735 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2736 int i;
2737 int iAddr = sqlite3_column_int(pSql, 0);
2738 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2739
2740 /* Set p2 to the P2 field of the current opcode. Then, assuming that
2741 ** p2 is an instruction address, set variable p2op to the index of that
2742 ** instruction in the aiIndent[] array. p2 and p2op may be different if
2743 ** the current instruction is part of a sub-program generated by an
2744 ** SQL trigger or foreign key. */
2745 int p2 = sqlite3_column_int(pSql, 3);
2746 int p2op = (p2 + (iOp-iAddr));
2747
2748 /* Grow the p->aiIndent array as required */
2749 if( iOp>=nAlloc ){
2750 if( iOp==0 ){
2751 /* Do further verfication that this is explain output. Abort if
2752 ** it is not */
2753 static const char *explainCols[] = {
2754 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2755 int jj;
2756 for(jj=0; jj<ArraySize(explainCols); jj++){
2757 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2758 p->cMode = p->mode;
2759 sqlite3_reset(pSql);
2760 return;
2761 }
2762 }
2763 }
2764 nAlloc += 100;
2765 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
drh884406b2018-07-29 18:56:35 +00002766 if( p->aiIndent==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00002767 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
drh884406b2018-07-29 18:56:35 +00002768 if( abYield==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00002769 }
2770 abYield[iOp] = str_in_array(zOp, azYield);
2771 p->aiIndent[iOp] = 0;
2772 p->nIndent = iOp+1;
2773
2774 if( str_in_array(zOp, azNext) ){
2775 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2776 }
2777 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2778 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2779 ){
2780 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2781 }
2782 }
2783
2784 p->iIndent = 0;
2785 sqlite3_free(abYield);
2786 sqlite3_reset(pSql);
2787}
2788
2789/*
2790** Free the array allocated by explain_data_prepare().
2791*/
2792static void explain_data_delete(ShellState *p){
2793 sqlite3_free(p->aiIndent);
2794 p->aiIndent = 0;
2795 p->nIndent = 0;
2796 p->iIndent = 0;
2797}
2798
2799/*
2800** Disable and restore .wheretrace and .selecttrace settings.
2801*/
2802#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2803extern int sqlite3SelectTrace;
2804static int savedSelectTrace;
2805#endif
2806#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2807extern int sqlite3WhereTrace;
2808static int savedWhereTrace;
2809#endif
2810static void disable_debug_trace_modes(void){
2811#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2812 savedSelectTrace = sqlite3SelectTrace;
2813 sqlite3SelectTrace = 0;
2814#endif
2815#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2816 savedWhereTrace = sqlite3WhereTrace;
2817 sqlite3WhereTrace = 0;
2818#endif
2819}
2820static void restore_debug_trace_modes(void){
2821#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2822 sqlite3SelectTrace = savedSelectTrace;
2823#endif
2824#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2825 sqlite3WhereTrace = savedWhereTrace;
2826#endif
2827}
2828
drh9cb02642019-02-28 20:10:52 +00002829/* Create the TEMP table used to store parameter bindings */
2830static void bind_table_init(ShellState *p){
drh346f4e22019-03-25 21:35:41 +00002831 int wrSchema = 0;
drh4b86e202020-01-19 20:37:26 +00002832 int defensiveMode = 0;
2833 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
2834 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
drh346f4e22019-03-25 21:35:41 +00002835 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
2836 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
drh9cb02642019-02-28 20:10:52 +00002837 sqlite3_exec(p->db,
drh65c29fd2019-03-25 21:56:26 +00002838 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
drh9cb02642019-02-28 20:10:52 +00002839 " key TEXT PRIMARY KEY,\n"
2840 " value ANY\n"
2841 ") WITHOUT ROWID;",
2842 0, 0, 0);
drh346f4e22019-03-25 21:35:41 +00002843 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
drh4b86e202020-01-19 20:37:26 +00002844 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
drh9cb02642019-02-28 20:10:52 +00002845}
2846
drh8b738d02019-02-25 18:43:54 +00002847/*
2848** Bind parameters on a prepared statement.
2849**
2850** Parameter bindings are taken from a TEMP table of the form:
2851**
drh1cb02632019-03-25 22:05:22 +00002852** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
drh8b738d02019-02-25 18:43:54 +00002853** WITHOUT ROWID;
2854**
drh91654b22020-04-02 13:21:10 +00002855** No bindings occur if this table does not exist. The name of the table
2856** begins with "sqlite_" so that it will not collide with ordinary application
2857** tables. The table must be in the TEMP schema.
drh8b738d02019-02-25 18:43:54 +00002858*/
2859static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
2860 int nVar;
2861 int i;
2862 int rc;
2863 sqlite3_stmt *pQ = 0;
2864
2865 nVar = sqlite3_bind_parameter_count(pStmt);
2866 if( nVar==0 ) return; /* Nothing to do */
drh65c29fd2019-03-25 21:56:26 +00002867 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
drh8b738d02019-02-25 18:43:54 +00002868 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
2869 return; /* Parameter table does not exist */
2870 }
2871 rc = sqlite3_prepare_v2(pArg->db,
drh65c29fd2019-03-25 21:56:26 +00002872 "SELECT value FROM temp.sqlite_parameters"
drh8b738d02019-02-25 18:43:54 +00002873 " WHERE key=?1", -1, &pQ, 0);
2874 if( rc || pQ==0 ) return;
2875 for(i=1; i<=nVar; i++){
2876 char zNum[30];
2877 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
2878 if( zVar==0 ){
2879 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
2880 zVar = zNum;
2881 }
2882 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
2883 if( sqlite3_step(pQ)==SQLITE_ROW ){
2884 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
2885 }else{
2886 sqlite3_bind_null(pStmt, i);
2887 }
2888 sqlite3_reset(pQ);
2889 }
2890 sqlite3_finalize(pQ);
2891}
2892
drh2ce15c32017-07-11 13:34:40 +00002893/*
2894** Run a prepared statement
2895*/
2896static void exec_prepared_stmt(
2897 ShellState *pArg, /* Pointer to ShellState */
drha10b9992018-03-09 15:24:33 +00002898 sqlite3_stmt *pStmt /* Statment to run */
drh2ce15c32017-07-11 13:34:40 +00002899){
2900 int rc;
2901
2902 /* perform the first step. this will tell us if we
2903 ** have a result set or not and how wide it is.
2904 */
2905 rc = sqlite3_step(pStmt);
2906 /* if we have a result set... */
2907 if( SQLITE_ROW == rc ){
drha10b9992018-03-09 15:24:33 +00002908 /* allocate space for col name ptr, value ptr, and type */
2909 int nCol = sqlite3_column_count(pStmt);
2910 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2911 if( !pData ){
2912 rc = SQLITE_NOMEM;
drh2ce15c32017-07-11 13:34:40 +00002913 }else{
drha10b9992018-03-09 15:24:33 +00002914 char **azCols = (char **)pData; /* Names of result columns */
2915 char **azVals = &azCols[nCol]; /* Results */
2916 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2917 int i, x;
2918 assert(sizeof(int) <= sizeof(char *));
2919 /* save off ptrs to column names */
2920 for(i=0; i<nCol; i++){
2921 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2922 }
drh2ce15c32017-07-11 13:34:40 +00002923 do{
drha10b9992018-03-09 15:24:33 +00002924 /* extract the data and data types */
2925 for(i=0; i<nCol; i++){
2926 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2927 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2928 azVals[i] = "";
2929 }else{
2930 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2931 }
2932 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2933 rc = SQLITE_NOMEM;
2934 break; /* from for */
2935 }
2936 } /* end for */
2937
2938 /* if data and types extracted successfully... */
2939 if( SQLITE_ROW == rc ){
2940 /* call the supplied callback with the result row data */
2941 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
2942 rc = SQLITE_ABORT;
2943 }else{
2944 rc = sqlite3_step(pStmt);
2945 }
2946 }
2947 } while( SQLITE_ROW == rc );
2948 sqlite3_free(pData);
drh2ce15c32017-07-11 13:34:40 +00002949 }
2950 }
2951}
2952
dan6b046be2018-01-09 15:25:55 +00002953#ifndef SQLITE_OMIT_VIRTUALTABLE
drh2ce15c32017-07-11 13:34:40 +00002954/*
dan43efc182017-12-19 17:42:13 +00002955** This function is called to process SQL if the previous shell command
2956** was ".expert". It passes the SQL in the second argument directly to
2957** the sqlite3expert object.
2958**
2959** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2960** code. In this case, (*pzErr) may be set to point to a buffer containing
2961** an English language error message. It is the responsibility of the
2962** caller to eventually free this buffer using sqlite3_free().
2963*/
2964static int expertHandleSQL(
2965 ShellState *pState,
2966 const char *zSql,
2967 char **pzErr
2968){
2969 assert( pState->expert.pExpert );
2970 assert( pzErr==0 || *pzErr==0 );
2971 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
2972}
2973
2974/*
2975** This function is called either to silently clean up the object
2976** created by the ".expert" command (if bCancel==1), or to generate a
2977** report from it and then clean it up (if bCancel==0).
2978**
2979** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2980** code. In this case, (*pzErr) may be set to point to a buffer containing
2981** an English language error message. It is the responsibility of the
2982** caller to eventually free this buffer using sqlite3_free().
2983*/
2984static int expertFinish(
2985 ShellState *pState,
2986 int bCancel,
2987 char **pzErr
2988){
2989 int rc = SQLITE_OK;
2990 sqlite3expert *p = pState->expert.pExpert;
2991 assert( p );
2992 assert( bCancel || pzErr==0 || *pzErr==0 );
2993 if( bCancel==0 ){
2994 FILE *out = pState->out;
2995 int bVerbose = pState->expert.bVerbose;
2996
2997 rc = sqlite3_expert_analyze(p, pzErr);
2998 if( rc==SQLITE_OK ){
2999 int nQuery = sqlite3_expert_count(p);
3000 int i;
3001
3002 if( bVerbose ){
3003 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
3004 raw_printf(out, "-- Candidates -----------------------------\n");
3005 raw_printf(out, "%s\n", zCand);
3006 }
3007 for(i=0; i<nQuery; i++){
3008 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
3009 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
3010 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
3011 if( zIdx==0 ) zIdx = "(no new indexes)\n";
3012 if( bVerbose ){
3013 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
3014 raw_printf(out, "%s\n\n", zSql);
3015 }
3016 raw_printf(out, "%s\n", zIdx);
3017 raw_printf(out, "%s\n", zEQP);
3018 }
3019 }
3020 }
3021 sqlite3_expert_destroy(p);
3022 pState->expert.pExpert = 0;
3023 return rc;
3024}
3025
dan6b046be2018-01-09 15:25:55 +00003026/*
3027** Implementation of ".expert" dot command.
3028*/
3029static int expertDotCommand(
3030 ShellState *pState, /* Current shell tool state */
3031 char **azArg, /* Array of arguments passed to dot command */
3032 int nArg /* Number of entries in azArg[] */
3033){
3034 int rc = SQLITE_OK;
3035 char *zErr = 0;
3036 int i;
3037 int iSample = 0;
3038
3039 assert( pState->expert.pExpert==0 );
3040 memset(&pState->expert, 0, sizeof(ExpertInfo));
3041
3042 for(i=1; rc==SQLITE_OK && i<nArg; i++){
3043 char *z = azArg[i];
3044 int n;
3045 if( z[0]=='-' && z[1]=='-' ) z++;
3046 n = strlen30(z);
3047 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
3048 pState->expert.bVerbose = 1;
3049 }
3050 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
3051 if( i==(nArg-1) ){
3052 raw_printf(stderr, "option requires an argument: %s\n", z);
3053 rc = SQLITE_ERROR;
3054 }else{
3055 iSample = (int)integerValue(azArg[++i]);
3056 if( iSample<0 || iSample>100 ){
3057 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
3058 rc = SQLITE_ERROR;
3059 }
3060 }
3061 }
3062 else{
3063 raw_printf(stderr, "unknown option: %s\n", z);
3064 rc = SQLITE_ERROR;
3065 }
3066 }
3067
3068 if( rc==SQLITE_OK ){
3069 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
3070 if( pState->expert.pExpert==0 ){
3071 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
3072 rc = SQLITE_ERROR;
3073 }else{
3074 sqlite3_expert_config(
3075 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
3076 );
3077 }
3078 }
3079
3080 return rc;
3081}
3082#endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
dan43efc182017-12-19 17:42:13 +00003083
3084/*
drh2ce15c32017-07-11 13:34:40 +00003085** Execute a statement or set of statements. Print
3086** any result rows/columns depending on the current mode
3087** set via the supplied callback.
3088**
3089** This is very similar to SQLite's built-in sqlite3_exec()
3090** function except it takes a slightly different callback
3091** and callback data argument.
3092*/
3093static int shell_exec(
drh2ce15c32017-07-11 13:34:40 +00003094 ShellState *pArg, /* Pointer to ShellState */
drha10b9992018-03-09 15:24:33 +00003095 const char *zSql, /* SQL to be evaluated */
drh2ce15c32017-07-11 13:34:40 +00003096 char **pzErrMsg /* Error msg written here */
3097){
3098 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
3099 int rc = SQLITE_OK; /* Return Code */
3100 int rc2;
3101 const char *zLeftover; /* Tail of unprocessed SQL */
drha10b9992018-03-09 15:24:33 +00003102 sqlite3 *db = pArg->db;
drh2ce15c32017-07-11 13:34:40 +00003103
3104 if( pzErrMsg ){
3105 *pzErrMsg = NULL;
3106 }
3107
dan6b046be2018-01-09 15:25:55 +00003108#ifndef SQLITE_OMIT_VIRTUALTABLE
dan43efc182017-12-19 17:42:13 +00003109 if( pArg->expert.pExpert ){
3110 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
3111 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
3112 }
dan6b046be2018-01-09 15:25:55 +00003113#endif
dan43efc182017-12-19 17:42:13 +00003114
drh2ce15c32017-07-11 13:34:40 +00003115 while( zSql[0] && (SQLITE_OK == rc) ){
3116 static const char *zStmtSql;
3117 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
3118 if( SQLITE_OK != rc ){
3119 if( pzErrMsg ){
3120 *pzErrMsg = save_err_msg(db);
3121 }
3122 }else{
3123 if( !pStmt ){
3124 /* this happens for a comment or white-space */
3125 zSql = zLeftover;
3126 while( IsSpace(zSql[0]) ) zSql++;
3127 continue;
3128 }
3129 zStmtSql = sqlite3_sql(pStmt);
3130 if( zStmtSql==0 ) zStmtSql = "";
3131 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
3132
3133 /* save off the prepared statment handle and reset row count */
3134 if( pArg ){
3135 pArg->pStmt = pStmt;
3136 pArg->cnt = 0;
3137 }
3138
3139 /* echo the sql statement if echo on */
3140 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
3141 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
3142 }
3143
3144 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
drh39c5c4a2019-03-06 14:53:27 +00003145 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
drh2ce15c32017-07-11 13:34:40 +00003146 sqlite3_stmt *pExplain;
3147 char *zEQP;
drhada70452017-12-21 21:02:27 +00003148 int triggerEQP = 0;
drh2ce15c32017-07-11 13:34:40 +00003149 disable_debug_trace_modes();
drhada70452017-12-21 21:02:27 +00003150 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
3151 if( pArg->autoEQP>=AUTOEQP_trigger ){
3152 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
3153 }
drh2ce15c32017-07-11 13:34:40 +00003154 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
3155 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3156 if( rc==SQLITE_OK ){
3157 while( sqlite3_step(pExplain)==SQLITE_ROW ){
drh4b5345c2018-04-24 13:07:40 +00003158 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
drhe2ca99c2018-05-02 00:33:43 +00003159 int iEqpId = sqlite3_column_int(pExplain, 0);
3160 int iParentId = sqlite3_column_int(pExplain, 1);
drh7e088a62020-05-02 00:01:39 +00003161 if( zEQPLine==0 ) zEQPLine = "";
drh4b5345c2018-04-24 13:07:40 +00003162 if( zEQPLine[0]=='-' ) eqp_render(pArg);
drhe2ca99c2018-05-02 00:33:43 +00003163 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
drh2ce15c32017-07-11 13:34:40 +00003164 }
drh4b5345c2018-04-24 13:07:40 +00003165 eqp_render(pArg);
drh2ce15c32017-07-11 13:34:40 +00003166 }
3167 sqlite3_finalize(pExplain);
3168 sqlite3_free(zEQP);
drhada70452017-12-21 21:02:27 +00003169 if( pArg->autoEQP>=AUTOEQP_full ){
drh2ce15c32017-07-11 13:34:40 +00003170 /* Also do an EXPLAIN for ".eqp full" mode */
3171 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
3172 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3173 if( rc==SQLITE_OK ){
3174 pArg->cMode = MODE_Explain;
3175 explain_data_prepare(pArg, pExplain);
drha10b9992018-03-09 15:24:33 +00003176 exec_prepared_stmt(pArg, pExplain);
drh2ce15c32017-07-11 13:34:40 +00003177 explain_data_delete(pArg);
3178 }
3179 sqlite3_finalize(pExplain);
3180 sqlite3_free(zEQP);
3181 }
drh51efe092018-03-20 12:04:38 +00003182 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
3183 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
3184 /* Reprepare pStmt before reactiving trace modes */
3185 sqlite3_finalize(pStmt);
3186 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
drh3c49eaf2018-06-07 15:23:43 +00003187 if( pArg ) pArg->pStmt = pStmt;
drh51efe092018-03-20 12:04:38 +00003188 }
drh2ce15c32017-07-11 13:34:40 +00003189 restore_debug_trace_modes();
3190 }
3191
3192 if( pArg ){
3193 pArg->cMode = pArg->mode;
drh4b5345c2018-04-24 13:07:40 +00003194 if( pArg->autoExplain ){
drh39c5c4a2019-03-06 14:53:27 +00003195 if( sqlite3_stmt_isexplain(pStmt)==1 ){
drh4b5345c2018-04-24 13:07:40 +00003196 pArg->cMode = MODE_Explain;
3197 }
drh39c5c4a2019-03-06 14:53:27 +00003198 if( sqlite3_stmt_isexplain(pStmt)==2 ){
drh4b5345c2018-04-24 13:07:40 +00003199 pArg->cMode = MODE_EQP;
3200 }
drh2ce15c32017-07-11 13:34:40 +00003201 }
3202
3203 /* If the shell is currently in ".explain" mode, gather the extra
3204 ** data required to add indents to the output.*/
3205 if( pArg->cMode==MODE_Explain ){
3206 explain_data_prepare(pArg, pStmt);
3207 }
3208 }
3209
drh8b738d02019-02-25 18:43:54 +00003210 bind_prepared_stmt(pArg, pStmt);
drha10b9992018-03-09 15:24:33 +00003211 exec_prepared_stmt(pArg, pStmt);
drh2ce15c32017-07-11 13:34:40 +00003212 explain_data_delete(pArg);
drh4b5345c2018-04-24 13:07:40 +00003213 eqp_render(pArg);
drh2ce15c32017-07-11 13:34:40 +00003214
3215 /* print usage stats if stats on */
3216 if( pArg && pArg->statsOn ){
3217 display_stats(db, pArg, 0);
3218 }
3219
3220 /* print loop-counters if required */
3221 if( pArg && pArg->scanstatsOn ){
3222 display_scanstats(db, pArg);
3223 }
3224
3225 /* Finalize the statement just executed. If this fails, save a
3226 ** copy of the error message. Otherwise, set zSql to point to the
3227 ** next statement to execute. */
3228 rc2 = sqlite3_finalize(pStmt);
3229 if( rc!=SQLITE_NOMEM ) rc = rc2;
3230 if( rc==SQLITE_OK ){
3231 zSql = zLeftover;
3232 while( IsSpace(zSql[0]) ) zSql++;
3233 }else if( pzErrMsg ){
3234 *pzErrMsg = save_err_msg(db);
3235 }
3236
3237 /* clear saved stmt handle */
3238 if( pArg ){
3239 pArg->pStmt = NULL;
3240 }
3241 }
3242 } /* end while */
3243
3244 return rc;
3245}
3246
3247/*
3248** Release memory previously allocated by tableColumnList().
3249*/
3250static void freeColumnList(char **azCol){
3251 int i;
3252 for(i=1; azCol[i]; i++){
3253 sqlite3_free(azCol[i]);
3254 }
3255 /* azCol[0] is a static string */
3256 sqlite3_free(azCol);
3257}
3258
3259/*
3260** Return a list of pointers to strings which are the names of all
3261** columns in table zTab. The memory to hold the names is dynamically
3262** allocated and must be released by the caller using a subsequent call
3263** to freeColumnList().
3264**
3265** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
3266** value that needs to be preserved, then azCol[0] is filled in with the
3267** name of the rowid column.
3268**
3269** The first regular column in the table is azCol[1]. The list is terminated
3270** by an entry with azCol[i]==0.
3271*/
3272static char **tableColumnList(ShellState *p, const char *zTab){
3273 char **azCol = 0;
3274 sqlite3_stmt *pStmt;
3275 char *zSql;
3276 int nCol = 0;
3277 int nAlloc = 0;
3278 int nPK = 0; /* Number of PRIMARY KEY columns seen */
3279 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
3280 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
3281 int rc;
3282
3283 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
3284 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3285 sqlite3_free(zSql);
3286 if( rc ) return 0;
3287 while( sqlite3_step(pStmt)==SQLITE_ROW ){
3288 if( nCol>=nAlloc-2 ){
3289 nAlloc = nAlloc*2 + nCol + 10;
3290 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
drh4b5345c2018-04-24 13:07:40 +00003291 if( azCol==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00003292 }
3293 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
3294 if( sqlite3_column_int(pStmt, 5) ){
3295 nPK++;
3296 if( nPK==1
3297 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
3298 "INTEGER")==0
3299 ){
3300 isIPK = 1;
3301 }else{
3302 isIPK = 0;
3303 }
3304 }
3305 }
3306 sqlite3_finalize(pStmt);
drh4c6cddc2017-10-12 10:28:30 +00003307 if( azCol==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00003308 azCol[0] = 0;
3309 azCol[nCol+1] = 0;
3310
3311 /* The decision of whether or not a rowid really needs to be preserved
3312 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
3313 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
3314 ** rowids on tables where the rowid is inaccessible because there are other
3315 ** columns in the table named "rowid", "_rowid_", and "oid".
3316 */
3317 if( preserveRowid && isIPK ){
3318 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3319 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
3320 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3321 ** ROWID aliases. To distinguish these cases, check to see if
3322 ** there is a "pk" entry in "PRAGMA index_list". There will be
3323 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3324 */
3325 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3326 " WHERE origin='pk'", zTab);
3327 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3328 sqlite3_free(zSql);
3329 if( rc ){
3330 freeColumnList(azCol);
3331 return 0;
3332 }
3333 rc = sqlite3_step(pStmt);
3334 sqlite3_finalize(pStmt);
3335 preserveRowid = rc==SQLITE_ROW;
3336 }
3337 if( preserveRowid ){
3338 /* Only preserve the rowid if we can find a name to use for the
3339 ** rowid */
3340 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
3341 int i, j;
3342 for(j=0; j<3; j++){
3343 for(i=1; i<=nCol; i++){
3344 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
3345 }
3346 if( i>nCol ){
3347 /* At this point, we know that azRowid[j] is not the name of any
3348 ** ordinary column in the table. Verify that azRowid[j] is a valid
3349 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
3350 ** tables will fail this last check */
3351 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
3352 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
3353 break;
3354 }
3355 }
3356 }
3357 return azCol;
3358}
3359
3360/*
3361** Toggle the reverse_unordered_selects setting.
3362*/
3363static void toggleSelectOrder(sqlite3 *db){
3364 sqlite3_stmt *pStmt = 0;
3365 int iSetting = 0;
3366 char zStmt[100];
3367 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
3368 if( sqlite3_step(pStmt)==SQLITE_ROW ){
3369 iSetting = sqlite3_column_int(pStmt, 0);
3370 }
3371 sqlite3_finalize(pStmt);
3372 sqlite3_snprintf(sizeof(zStmt), zStmt,
3373 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
3374 sqlite3_exec(db, zStmt, 0, 0, 0);
3375}
3376
3377/*
3378** This is a different callback routine used for dumping the database.
3379** Each row received by this callback consists of a table name,
3380** the table type ("index" or "table") and SQL to create the table.
3381** This routine should print text sufficient to recreate the table.
3382*/
3383static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
3384 int rc;
3385 const char *zTable;
3386 const char *zType;
3387 const char *zSql;
3388 ShellState *p = (ShellState *)pArg;
3389
3390 UNUSED_PARAMETER(azNotUsed);
drhb3c45232017-08-28 14:33:27 +00003391 if( nArg!=3 || azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00003392 zTable = azArg[0];
3393 zType = azArg[1];
3394 zSql = azArg[2];
3395
3396 if( strcmp(zTable, "sqlite_sequence")==0 ){
3397 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
3398 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
3399 raw_printf(p->out, "ANALYZE sqlite_master;\n");
3400 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
3401 return 0;
3402 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
3403 char *zIns;
3404 if( !p->writableSchema ){
3405 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
3406 p->writableSchema = 1;
3407 }
3408 zIns = sqlite3_mprintf(
3409 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3410 "VALUES('table','%q','%q',0,'%q');",
3411 zTable, zTable, zSql);
3412 utf8_printf(p->out, "%s\n", zIns);
3413 sqlite3_free(zIns);
3414 return 0;
3415 }else{
3416 printSchemaLine(p->out, zSql, ";\n");
3417 }
3418
3419 if( strcmp(zType, "table")==0 ){
3420 ShellText sSelect;
3421 ShellText sTable;
3422 char **azCol;
3423 int i;
3424 char *savedDestTable;
3425 int savedMode;
3426
3427 azCol = tableColumnList(p, zTable);
3428 if( azCol==0 ){
3429 p->nErr++;
3430 return 0;
3431 }
3432
3433 /* Always quote the table name, even if it appears to be pure ascii,
3434 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
3435 initText(&sTable);
3436 appendText(&sTable, zTable, quoteChar(zTable));
3437 /* If preserving the rowid, add a column list after the table name.
3438 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3439 ** instead of the usual "INSERT INTO tab VALUES(...)".
3440 */
3441 if( azCol[0] ){
3442 appendText(&sTable, "(", 0);
3443 appendText(&sTable, azCol[0], 0);
3444 for(i=1; azCol[i]; i++){
3445 appendText(&sTable, ",", 0);
3446 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
3447 }
3448 appendText(&sTable, ")", 0);
3449 }
3450
3451 /* Build an appropriate SELECT statement */
3452 initText(&sSelect);
3453 appendText(&sSelect, "SELECT ", 0);
3454 if( azCol[0] ){
3455 appendText(&sSelect, azCol[0], 0);
3456 appendText(&sSelect, ",", 0);
3457 }
3458 for(i=1; azCol[i]; i++){
3459 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
3460 if( azCol[i+1] ){
3461 appendText(&sSelect, ",", 0);
3462 }
3463 }
3464 freeColumnList(azCol);
3465 appendText(&sSelect, " FROM ", 0);
3466 appendText(&sSelect, zTable, quoteChar(zTable));
3467
3468 savedDestTable = p->zDestTable;
3469 savedMode = p->mode;
3470 p->zDestTable = sTable.z;
3471 p->mode = p->cMode = MODE_Insert;
drha10b9992018-03-09 15:24:33 +00003472 rc = shell_exec(p, sSelect.z, 0);
drh2ce15c32017-07-11 13:34:40 +00003473 if( (rc&0xff)==SQLITE_CORRUPT ){
3474 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3475 toggleSelectOrder(p->db);
drha10b9992018-03-09 15:24:33 +00003476 shell_exec(p, sSelect.z, 0);
drh2ce15c32017-07-11 13:34:40 +00003477 toggleSelectOrder(p->db);
3478 }
3479 p->zDestTable = savedDestTable;
3480 p->mode = savedMode;
3481 freeText(&sTable);
3482 freeText(&sSelect);
3483 if( rc ) p->nErr++;
3484 }
3485 return 0;
3486}
3487
3488/*
3489** Run zQuery. Use dump_callback() as the callback routine so that
3490** the contents of the query are output as SQL statements.
3491**
3492** If we get a SQLITE_CORRUPT error, rerun the query after appending
3493** "ORDER BY rowid DESC" to the end.
3494*/
3495static int run_schema_dump_query(
3496 ShellState *p,
3497 const char *zQuery
3498){
3499 int rc;
3500 char *zErr = 0;
3501 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
3502 if( rc==SQLITE_CORRUPT ){
3503 char *zQ2;
3504 int len = strlen30(zQuery);
3505 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
3506 if( zErr ){
3507 utf8_printf(p->out, "/****** %s ******/\n", zErr);
3508 sqlite3_free(zErr);
3509 zErr = 0;
3510 }
3511 zQ2 = malloc( len+100 );
3512 if( zQ2==0 ) return rc;
3513 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
3514 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
3515 if( rc ){
3516 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
3517 }else{
3518 rc = SQLITE_CORRUPT;
3519 }
3520 sqlite3_free(zErr);
3521 free(zQ2);
3522 }
3523 return rc;
3524}
3525
3526/*
drh98aa2ab2018-09-26 16:53:51 +00003527** Text of help messages.
3528**
3529** The help text for each individual command begins with a line that starts
3530** with ".". Subsequent lines are supplimental information.
3531**
3532** There must be two or more spaces between the end of the command and the
3533** start of the description of what that command does.
drh2ce15c32017-07-11 13:34:40 +00003534*/
drh98aa2ab2018-09-26 16:53:51 +00003535static const char *(azHelp[]) = {
drhe37c0e12018-01-06 19:19:50 +00003536#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
drh98aa2ab2018-09-26 16:53:51 +00003537 ".archive ... Manage SQL archives",
3538 " Each command must have exactly one of the following options:",
3539 " -c, --create Create a new archive",
drhe2754c12019-08-26 12:50:01 +00003540 " -u, --update Add or update files with changed mtime",
3541 " -i, --insert Like -u but always add even if unchanged",
drh98aa2ab2018-09-26 16:53:51 +00003542 " -t, --list List contents of archive",
3543 " -x, --extract Extract files from archive",
3544 " Optional arguments:",
3545 " -v, --verbose Print each filename as it is processed",
drhe2754c12019-08-26 12:50:01 +00003546 " -f FILE, --file FILE Use archive FILE (default is current db)",
3547 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
3548 " -C DIR, --directory DIR Read/extract files from directory DIR",
drh98aa2ab2018-09-26 16:53:51 +00003549 " -n, --dryrun Show the SQL that would have occurred",
3550 " Examples:",
drhe2754c12019-08-26 12:50:01 +00003551 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
3552 " .ar -tf ARCHIVE # List members of ARCHIVE",
3553 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
drh98aa2ab2018-09-26 16:53:51 +00003554 " See also:",
3555 " http://sqlite.org/cli.html#sqlar_archive_support",
drhe37c0e12018-01-06 19:19:50 +00003556#endif
drh2ce15c32017-07-11 13:34:40 +00003557#ifndef SQLITE_OMIT_AUTHORIZATION
drh98aa2ab2018-09-26 16:53:51 +00003558 ".auth ON|OFF Show authorizer callbacks",
drh2ce15c32017-07-11 13:34:40 +00003559#endif
drh98aa2ab2018-09-26 16:53:51 +00003560 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
3561 " --append Use the appendvfs",
drhe2754c12019-08-26 12:50:01 +00003562 " --async Write to FILE without journal and fsync()",
drh98aa2ab2018-09-26 16:53:51 +00003563 ".bail on|off Stop after hitting an error. Default OFF",
3564 ".binary on|off Turn binary output on or off. Default OFF",
3565 ".cd DIRECTORY Change the working directory to DIRECTORY",
3566 ".changes on|off Show number of rows changed by SQL",
3567 ".check GLOB Fail if output since .testcase does not match",
3568 ".clone NEWDB Clone data into NEWDB from the existing database",
3569 ".databases List names and files of attached databases",
3570 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
3571 ".dbinfo ?DB? Show status information about the database",
drh8e9297f2020-03-25 12:50:13 +00003572 ".dump ?TABLE? Render database content as SQL",
drheb7f2a02018-09-26 18:02:32 +00003573 " Options:",
3574 " --preserve-rowids Include ROWID values in the output",
3575 " --newlines Allow unescaped newline characters in output",
drhe551b512019-04-17 13:58:07 +00003576 " TABLE is a LIKE pattern for the tables to dump",
drh8e9297f2020-03-25 12:50:13 +00003577 " Additional LIKE patterns can be given in subsequent arguments",
drh98aa2ab2018-09-26 16:53:51 +00003578 ".echo on|off Turn command echo on or off",
drhb4e50392019-01-26 15:40:04 +00003579 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
3580 " Other Modes:",
3581#ifdef SQLITE_DEBUG
3582 " test Show raw EXPLAIN QUERY PLAN output",
drhe2754c12019-08-26 12:50:01 +00003583 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
drhb4e50392019-01-26 15:40:04 +00003584#endif
3585 " trigger Like \"full\" but also show trigger bytecode",
drhe2754c12019-08-26 12:50:01 +00003586 ".excel Display the output of next command in spreadsheet",
drh7a431002020-04-18 14:12:00 +00003587 " --bom Put a UTF8 byte-order mark on intermediate file",
drheb7f2a02018-09-26 18:02:32 +00003588 ".exit ?CODE? Exit this program with return-code CODE",
drhe2754c12019-08-26 12:50:01 +00003589 ".expert EXPERIMENTAL. Suggest indexes for queries",
drh978256f2019-11-02 00:00:14 +00003590 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
drhd985f722019-06-05 14:29:53 +00003591 ".filectrl CMD ... Run various sqlite3_file_control() operations",
drh541ef2c2020-04-20 16:21:30 +00003592 " --schema SCHEMA Use SCHEMA instead of \"main\"",
3593 " --help Show CMD details",
drh98aa2ab2018-09-26 16:53:51 +00003594 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
3595 ".headers on|off Turn display of headers on or off",
3596 ".help ?-all? ?PATTERN? Show help text for PATTERN",
3597 ".import FILE TABLE Import data from FILE into TABLE",
drhccb37812020-03-09 15:39:39 +00003598 " Options:",
3599 " --ascii Use \\037 and \\036 as column and row separators",
3600 " --csv Use , and \\n as column and row separators",
3601 " --skip N Skip the first N rows of input",
3602 " -v \"Verbose\" - increase auxiliary output",
3603 " Notes:",
3604 " * If TABLE does not exist, it is created. The first row of input",
3605 " determines the column names.",
3606 " * If neither --csv or --ascii are used, the input mode is derived",
3607 " from the \".mode\" output mode",
3608 " * If FILE begins with \"|\" then it is a command that generates the",
3609 " input text.",
drh2ce15c32017-07-11 13:34:40 +00003610#ifndef SQLITE_OMIT_TEST_CONTROL
drh98aa2ab2018-09-26 16:53:51 +00003611 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
drh2ce15c32017-07-11 13:34:40 +00003612#endif
drh98aa2ab2018-09-26 16:53:51 +00003613 ".indexes ?TABLE? Show names of indexes",
3614 " If TABLE is specified, only show indexes for",
3615 " tables matching TABLE using the LIKE operator.",
drh2ce15c32017-07-11 13:34:40 +00003616#ifdef SQLITE_ENABLE_IOTRACE
drh98aa2ab2018-09-26 16:53:51 +00003617 ".iotrace FILE Enable I/O diagnostic logging to FILE",
drh2ce15c32017-07-11 13:34:40 +00003618#endif
drh98aa2ab2018-09-26 16:53:51 +00003619 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
3620 ".lint OPTIONS Report potential schema issues.",
3621 " Options:",
3622 " fkey-indexes Find missing foreign key indexes",
drh2ce15c32017-07-11 13:34:40 +00003623#ifndef SQLITE_OMIT_LOAD_EXTENSION
drh98aa2ab2018-09-26 16:53:51 +00003624 ".load FILE ?ENTRY? Load an extension library",
drh2ce15c32017-07-11 13:34:40 +00003625#endif
drh98aa2ab2018-09-26 16:53:51 +00003626 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
3627 ".mode MODE ?TABLE? Set output mode",
3628 " MODE is one of:",
3629 " ascii Columns/rows delimited by 0x1F and 0x1E",
3630 " csv Comma-separated values",
3631 " column Left-aligned columns. (See .width)",
3632 " html HTML <table> code",
3633 " insert SQL insert statements for TABLE",
3634 " line One value per line",
3635 " list Values delimited by \"|\"",
3636 " quote Escape answers as for SQL",
3637 " tabs Tab-separated values",
3638 " tcl TCL list elements",
3639 ".nullvalue STRING Use STRING in place of NULL values",
drh7a431002020-04-18 14:12:00 +00003640 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
drh98aa2ab2018-09-26 16:53:51 +00003641 " If FILE begins with '|' then open as a pipe",
drh7a431002020-04-18 14:12:00 +00003642 " --bom Put a UTF8 byte-order mark at the beginning",
3643 " -e Send output to the system text editor",
3644 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
drh4a3a3eb2020-02-29 15:53:48 +00003645#ifdef SQLITE_DEBUG
3646 ".oom [--repeat M] [N] Simulate an OOM error on the N-th allocation",
3647#endif
drh98aa2ab2018-09-26 16:53:51 +00003648 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
3649 " Options:",
drh60f34ae2018-10-30 13:19:49 +00003650 " --append Use appendvfs to append database to the end of FILE",
drha751f392018-10-30 15:31:22 +00003651#ifdef SQLITE_ENABLE_DESERIALIZE
drh60f34ae2018-10-30 13:19:49 +00003652 " --deserialize Load into memory useing sqlite3_deserialize()",
drhe2754c12019-08-26 12:50:01 +00003653 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
drh6ca64482019-01-22 16:06:20 +00003654 " --maxsize N Maximum size for --hexdb or --deserialized database",
drha751f392018-10-30 15:31:22 +00003655#endif
drh60f34ae2018-10-30 13:19:49 +00003656 " --new Initialize FILE to an empty database",
drh0933aad2019-11-18 17:46:38 +00003657 " --nofollow Do not follow symbolic links",
drh60f34ae2018-10-30 13:19:49 +00003658 " --readonly Open FILE readonly",
3659 " --zip FILE is a ZIP archive",
drh98aa2ab2018-09-26 16:53:51 +00003660 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
drh7a431002020-04-18 14:12:00 +00003661 " If FILE begins with '|' then open it as a pipe.",
3662 " Options:",
3663 " --bom Prefix output with a UTF8 byte-order mark",
3664 " -e Send output to the system text editor",
3665 " -x Send output as CSV to a spreadsheet",
drh9cb02642019-02-28 20:10:52 +00003666 ".parameter CMD ... Manage SQL parameter bindings",
3667 " clear Erase all bindings",
3668 " init Initialize the TEMP table that holds bindings",
3669 " list List the current parameter bindings",
3670 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
drhe2754c12019-08-26 12:50:01 +00003671 " PARAMETER should start with one of: $ : @ ?",
drh9cb02642019-02-28 20:10:52 +00003672 " unset PARAMETER Remove PARAMETER from the binding table",
drh98aa2ab2018-09-26 16:53:51 +00003673 ".print STRING... Print literal STRING",
drh569b1d92019-02-05 20:51:41 +00003674#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh3f83f592019-02-04 14:53:18 +00003675 ".progress N Invoke progress handler after every N opcodes",
3676 " --limit N Interrupt after N progress callbacks",
3677 " --once Do no more than one progress interrupt",
3678 " --quiet|-q No output except at interrupts",
3679 " --reset Reset the count for each input and interrupt",
drh569b1d92019-02-05 20:51:41 +00003680#endif
drh98aa2ab2018-09-26 16:53:51 +00003681 ".prompt MAIN CONTINUE Replace the standard prompts",
3682 ".quit Exit this program",
3683 ".read FILE Read input from FILE",
dan1b162162019-04-27 20:15:15 +00003684#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
dan42ebb012019-04-27 18:47:03 +00003685 ".recover Recover as much data as possible from corrupt db.",
drhe2754c12019-08-26 12:50:01 +00003686 " --freelist-corrupt Assume the freelist is corrupt",
3687 " --recovery-db NAME Store recovery metadata in database file NAME",
3688 " --lost-and-found TABLE Alternative name for the lost-and-found table",
dan8cce6b82019-09-14 16:44:51 +00003689 " --no-rowids Do not attempt to recover rowid values",
3690 " that are not also INTEGER PRIMARY KEYs",
dan1b162162019-04-27 20:15:15 +00003691#endif
drh98aa2ab2018-09-26 16:53:51 +00003692 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
3693 ".save FILE Write in-memory database into FILE",
3694 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
3695 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
3696 " Options:",
3697 " --indent Try to pretty-print the schema",
drheb7f2a02018-09-26 18:02:32 +00003698 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
3699 " Options:",
3700 " --init Create a new SELFTEST table",
3701 " -v Verbose output",
drh98aa2ab2018-09-26 16:53:51 +00003702 ".separator COL ?ROW? Change the column and row separators",
drh2ce15c32017-07-11 13:34:40 +00003703#if defined(SQLITE_ENABLE_SESSION)
drheb7f2a02018-09-26 18:02:32 +00003704 ".session ?NAME? CMD ... Create or control sessions",
3705 " Subcommands:",
3706 " attach TABLE Attach TABLE",
3707 " changeset FILE Write a changeset into FILE",
3708 " close Close one session",
3709 " enable ?BOOLEAN? Set or query the enable bit",
3710 " filter GLOB... Reject tables matching GLOBs",
3711 " indirect ?BOOLEAN? Mark or query the indirect status",
3712 " isempty Query whether the session is empty",
3713 " list List currently open session names",
3714 " open DB NAME Open a new session on DB",
3715 " patchset FILE Write a patchset into FILE",
3716 " If ?NAME? is omitted, the first defined session is used.",
drh2ce15c32017-07-11 13:34:40 +00003717#endif
drheb7f2a02018-09-26 18:02:32 +00003718 ".sha3sum ... Compute a SHA3 hash of database content",
3719 " Options:",
3720 " --schema Also hash the sqlite_master table",
3721 " --sha3-224 Use the sha3-224 algorithm",
drhe2754c12019-08-26 12:50:01 +00003722 " --sha3-256 Use the sha3-256 algorithm (default)",
drheb7f2a02018-09-26 18:02:32 +00003723 " --sha3-384 Use the sha3-384 algorithm",
3724 " --sha3-512 Use the sha3-512 algorithm",
3725 " Any other argument is a LIKE pattern for tables to hash",
drh04a28c32018-01-31 01:38:44 +00003726#ifndef SQLITE_NOHAVE_SYSTEM
drh98aa2ab2018-09-26 16:53:51 +00003727 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
drh04a28c32018-01-31 01:38:44 +00003728#endif
drh98aa2ab2018-09-26 16:53:51 +00003729 ".show Show the current values for various settings",
3730 ".stats ?on|off? Show stats or turn stats on or off",
drh04a28c32018-01-31 01:38:44 +00003731#ifndef SQLITE_NOHAVE_SYSTEM
drh98aa2ab2018-09-26 16:53:51 +00003732 ".system CMD ARGS... Run CMD ARGS... in a system shell",
drh04a28c32018-01-31 01:38:44 +00003733#endif
drh98aa2ab2018-09-26 16:53:51 +00003734 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
3735 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
drhd985f722019-06-05 14:29:53 +00003736 ".testctrl CMD ... Run various sqlite3_test_control() operations",
3737 " Run \".testctrl\" with no arguments for details",
drh98aa2ab2018-09-26 16:53:51 +00003738 ".timeout MS Try opening locked tables for MS milliseconds",
3739 ".timer on|off Turn SQL timer on or off",
drh707821f2018-12-05 13:39:06 +00003740#ifndef SQLITE_OMIT_TRACE
3741 ".trace ?OPTIONS? Output each SQL statement as it is run",
3742 " FILE Send output to FILE",
3743 " stdout Send output to stdout",
3744 " stderr Send output to stderr",
3745 " off Disable tracing",
3746 " --expanded Expand query parameters",
3747#ifdef SQLITE_ENABLE_NORMALIZE
3748 " --normalized Normal the SQL statements",
3749#endif
3750 " --plain Show SQL as it is input",
3751 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
3752 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
3753 " --row Trace each row (SQLITE_TRACE_ROW)",
3754 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
3755#endif /* SQLITE_OMIT_TRACE */
drhcc5979d2019-08-16 22:58:29 +00003756#ifdef SQLITE_DEBUG
3757 ".unmodule NAME ... Unregister virtual table modules",
drh5df84282019-08-17 19:45:25 +00003758 " --allexcept Unregister everything except those named",
drhcc5979d2019-08-16 22:58:29 +00003759#endif
drh98aa2ab2018-09-26 16:53:51 +00003760 ".vfsinfo ?AUX? Information about the top-level VFS",
3761 ".vfslist List all available VFSes",
3762 ".vfsname ?AUX? Print the name of the VFS stack",
3763 ".width NUM1 NUM2 ... Set column widths for \"column\" mode",
3764 " Negative values right-justify",
3765};
3766
3767/*
3768** Output help text.
3769**
3770** zPattern describes the set of commands for which help text is provided.
3771** If zPattern is NULL, then show all commands, but only give a one-line
3772** description of each.
3773**
3774** Return the number of matches.
3775*/
3776static int showHelp(FILE *out, const char *zPattern){
drhe93f8262018-10-11 16:53:37 +00003777 int i = 0;
3778 int j = 0;
drh98aa2ab2018-09-26 16:53:51 +00003779 int n = 0;
3780 char *zPat;
drh488cddf2018-10-06 14:38:17 +00003781 if( zPattern==0
3782 || zPattern[0]=='0'
3783 || strcmp(zPattern,"-a")==0
3784 || strcmp(zPattern,"-all")==0
drh7a431002020-04-18 14:12:00 +00003785 || strcmp(zPattern,"--all")==0
drh488cddf2018-10-06 14:38:17 +00003786 ){
drh98aa2ab2018-09-26 16:53:51 +00003787 /* Show all commands, but only one line per command */
drh488cddf2018-10-06 14:38:17 +00003788 if( zPattern==0 ) zPattern = "";
drh98aa2ab2018-09-26 16:53:51 +00003789 for(i=0; i<ArraySize(azHelp); i++){
drh488cddf2018-10-06 14:38:17 +00003790 if( azHelp[i][0]=='.' || zPattern[0] ){
drh98aa2ab2018-09-26 16:53:51 +00003791 utf8_printf(out, "%s\n", azHelp[i]);
3792 n++;
3793 }
3794 }
3795 }else{
3796 /* Look for commands that for which zPattern is an exact prefix */
3797 zPat = sqlite3_mprintf(".%s*", zPattern);
3798 for(i=0; i<ArraySize(azHelp); i++){
3799 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
3800 utf8_printf(out, "%s\n", azHelp[i]);
drheb7f2a02018-09-26 18:02:32 +00003801 j = i+1;
drh98aa2ab2018-09-26 16:53:51 +00003802 n++;
3803 }
3804 }
3805 sqlite3_free(zPat);
drheb7f2a02018-09-26 18:02:32 +00003806 if( n ){
3807 if( n==1 ){
3808 /* when zPattern is a prefix of exactly one command, then include the
3809 ** details of that command, which should begin at offset j */
3810 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
3811 utf8_printf(out, "%s\n", azHelp[j]);
3812 j++;
3813 }
3814 }
3815 return n;
3816 }
3817 /* Look for commands that contain zPattern anywhere. Show the complete
3818 ** text of all commands that match. */
drh98aa2ab2018-09-26 16:53:51 +00003819 zPat = sqlite3_mprintf("%%%s%%", zPattern);
3820 for(i=0; i<ArraySize(azHelp); i++){
3821 if( azHelp[i][0]=='.' ) j = i;
3822 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
3823 utf8_printf(out, "%s\n", azHelp[j]);
3824 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
3825 j++;
3826 utf8_printf(out, "%s\n", azHelp[j]);
3827 }
3828 i = j;
3829 n++;
3830 }
3831 }
3832 sqlite3_free(zPat);
3833 }
3834 return n;
3835}
drh2ce15c32017-07-11 13:34:40 +00003836
drh2ce15c32017-07-11 13:34:40 +00003837/* Forward reference */
drh60379d42018-12-13 18:30:01 +00003838static int process_input(ShellState *p);
drh2ce15c32017-07-11 13:34:40 +00003839
3840/*
3841** Read the content of file zName into memory obtained from sqlite3_malloc64()
3842** and return a pointer to the buffer. The caller is responsible for freeing
3843** the memory.
3844**
3845** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
3846** read.
3847**
3848** For convenience, a nul-terminator byte is always appended to the data read
3849** from the file before the buffer is returned. This byte is not included in
3850** the final value of (*pnByte), if applicable.
3851**
3852** NULL is returned if any error is encountered. The final value of *pnByte
3853** is undefined in this case.
3854*/
3855static char *readFile(const char *zName, int *pnByte){
3856 FILE *in = fopen(zName, "rb");
3857 long nIn;
3858 size_t nRead;
3859 char *pBuf;
3860 if( in==0 ) return 0;
3861 fseek(in, 0, SEEK_END);
3862 nIn = ftell(in);
3863 rewind(in);
3864 pBuf = sqlite3_malloc64( nIn+1 );
drh1dbb1472018-10-11 10:37:24 +00003865 if( pBuf==0 ){ fclose(in); return 0; }
drh2ce15c32017-07-11 13:34:40 +00003866 nRead = fread(pBuf, nIn, 1, in);
3867 fclose(in);
3868 if( nRead!=1 ){
3869 sqlite3_free(pBuf);
3870 return 0;
3871 }
3872 pBuf[nIn] = 0;
3873 if( pnByte ) *pnByte = nIn;
3874 return pBuf;
3875}
3876
3877#if defined(SQLITE_ENABLE_SESSION)
3878/*
3879** Close a single OpenSession object and release all of its associated
3880** resources.
3881*/
3882static void session_close(OpenSession *pSession){
3883 int i;
3884 sqlite3session_delete(pSession->p);
3885 sqlite3_free(pSession->zName);
3886 for(i=0; i<pSession->nFilter; i++){
3887 sqlite3_free(pSession->azFilter[i]);
3888 }
3889 sqlite3_free(pSession->azFilter);
3890 memset(pSession, 0, sizeof(OpenSession));
3891}
3892#endif
3893
3894/*
3895** Close all OpenSession objects and release all associated resources.
3896*/
3897#if defined(SQLITE_ENABLE_SESSION)
3898static void session_close_all(ShellState *p){
3899 int i;
3900 for(i=0; i<p->nSession; i++){
3901 session_close(&p->aSession[i]);
3902 }
3903 p->nSession = 0;
3904}
3905#else
3906# define session_close_all(X)
3907#endif
3908
3909/*
3910** Implementation of the xFilter function for an open session. Omit
3911** any tables named by ".session filter" but let all other table through.
3912*/
3913#if defined(SQLITE_ENABLE_SESSION)
3914static int session_filter(void *pCtx, const char *zTab){
3915 OpenSession *pSession = (OpenSession*)pCtx;
3916 int i;
3917 for(i=0; i<pSession->nFilter; i++){
3918 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
3919 }
3920 return 1;
3921}
3922#endif
3923
3924/*
drh1fa6d9f2018-01-06 21:46:01 +00003925** Try to deduce the type of file for zName based on its content. Return
3926** one of the SHELL_OPEN_* constants.
drh1bf208c2018-03-09 21:54:01 +00003927**
3928** If the file does not exist or is empty but its name looks like a ZIP
3929** archive and the dfltZip flag is true, then assume it is a ZIP archive.
3930** Otherwise, assume an ordinary database regardless of the filename if
3931** the type cannot be determined from content.
drh1fa6d9f2018-01-06 21:46:01 +00003932*/
drhfc97c1c2018-05-14 00:41:12 +00003933int deduceDatabaseType(const char *zName, int dfltZip){
drh1fa6d9f2018-01-06 21:46:01 +00003934 FILE *f = fopen(zName, "rb");
3935 size_t n;
3936 int rc = SHELL_OPEN_UNSPEC;
3937 char zBuf[100];
drh1bf208c2018-03-09 21:54:01 +00003938 if( f==0 ){
drhbe4ccb22018-05-17 20:04:24 +00003939 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
3940 return SHELL_OPEN_ZIPFILE;
3941 }else{
3942 return SHELL_OPEN_NORMAL;
3943 }
drh1bf208c2018-03-09 21:54:01 +00003944 }
drh2b3c4af2018-10-30 14:36:21 +00003945 n = fread(zBuf, 16, 1, f);
3946 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
3947 fclose(f);
3948 return SHELL_OPEN_NORMAL;
3949 }
drh1fa6d9f2018-01-06 21:46:01 +00003950 fseek(f, -25, SEEK_END);
3951 n = fread(zBuf, 25, 1, f);
3952 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
3953 rc = SHELL_OPEN_APPENDVFS;
3954 }else{
3955 fseek(f, -22, SEEK_END);
3956 n = fread(zBuf, 22, 1, f);
3957 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
3958 && zBuf[3]==0x06 ){
3959 rc = SHELL_OPEN_ZIPFILE;
drh1bf208c2018-03-09 21:54:01 +00003960 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
mistachkina3926f42018-05-14 12:23:04 +00003961 rc = SHELL_OPEN_ZIPFILE;
drh1fa6d9f2018-01-06 21:46:01 +00003962 }
3963 }
3964 fclose(f);
3965 return rc;
3966}
3967
drh33746482018-12-13 15:06:26 +00003968#ifdef SQLITE_ENABLE_DESERIALIZE
3969/*
3970** Reconstruct an in-memory database using the output from the "dbtotxt"
3971** program. Read content from the file in p->zDbFilename. If p->zDbFilename
3972** is 0, then read from standard input.
3973*/
3974static unsigned char *readHexDb(ShellState *p, int *pnData){
3975 unsigned char *a = 0;
drh2c8ee022018-12-13 18:59:30 +00003976 int nLine;
drh33746482018-12-13 15:06:26 +00003977 int n = 0;
3978 int pgsz = 0;
3979 int iOffset = 0;
3980 int j, k;
3981 int rc;
3982 FILE *in;
drh3ea557e2019-04-23 15:30:58 +00003983 unsigned int x[16];
drh2c8ee022018-12-13 18:59:30 +00003984 char zLine[1000];
drh33746482018-12-13 15:06:26 +00003985 if( p->zDbFilename ){
3986 in = fopen(p->zDbFilename, "r");
3987 if( in==0 ){
3988 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
3989 return 0;
3990 }
drh2c8ee022018-12-13 18:59:30 +00003991 nLine = 0;
drh33746482018-12-13 15:06:26 +00003992 }else{
drh60379d42018-12-13 18:30:01 +00003993 in = p->in;
drh2c8ee022018-12-13 18:59:30 +00003994 nLine = p->lineno;
drh5bf46442019-05-03 02:41:36 +00003995 if( in==0 ) in = stdin;
drh33746482018-12-13 15:06:26 +00003996 }
3997 *pnData = 0;
drh2c8ee022018-12-13 18:59:30 +00003998 nLine++;
drh33746482018-12-13 15:06:26 +00003999 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
4000 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
4001 if( rc!=2 ) goto readHexDb_error;
drh68feae52019-05-09 11:18:41 +00004002 if( n<0 ) goto readHexDb_error;
drh09ea1252019-07-17 15:05:16 +00004003 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
4004 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
drh68feae52019-05-09 11:18:41 +00004005 a = sqlite3_malloc( n ? n : 1 );
drh33746482018-12-13 15:06:26 +00004006 if( a==0 ){
4007 utf8_printf(stderr, "Out of memory!\n");
4008 goto readHexDb_error;
4009 }
4010 memset(a, 0, n);
4011 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
4012 utf8_printf(stderr, "invalid pagesize\n");
4013 goto readHexDb_error;
4014 }
drh2c8ee022018-12-13 18:59:30 +00004015 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
drh33746482018-12-13 15:06:26 +00004016 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
4017 if( rc==2 ){
4018 iOffset = k;
4019 continue;
4020 }
4021 if( strncmp(zLine, "| end ", 6)==0 ){
4022 break;
4023 }
drh3ea557e2019-04-23 15:30:58 +00004024 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
drh33746482018-12-13 15:06:26 +00004025 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
4026 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
4027 if( rc==17 ){
4028 k = iOffset+j;
drhf354e772018-12-13 22:58:52 +00004029 if( k+16<=n ){
drh3ea557e2019-04-23 15:30:58 +00004030 int ii;
4031 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
drh33746482018-12-13 15:06:26 +00004032 }
drh33746482018-12-13 15:06:26 +00004033 }
4034 }
4035 *pnData = n;
drh2c8ee022018-12-13 18:59:30 +00004036 if( in!=p->in ){
4037 fclose(in);
4038 }else{
4039 p->lineno = nLine;
4040 }
drh33746482018-12-13 15:06:26 +00004041 return a;
4042
4043readHexDb_error:
drh68feae52019-05-09 11:18:41 +00004044 if( in!=p->in ){
drh33746482018-12-13 15:06:26 +00004045 fclose(in);
4046 }else{
drh60379d42018-12-13 18:30:01 +00004047 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
drh2c8ee022018-12-13 18:59:30 +00004048 nLine++;
drh33746482018-12-13 15:06:26 +00004049 if(strncmp(zLine, "| end ", 6)==0 ) break;
4050 }
drh2c8ee022018-12-13 18:59:30 +00004051 p->lineno = nLine;
drh33746482018-12-13 15:06:26 +00004052 }
4053 sqlite3_free(a);
4054 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
4055 return 0;
4056}
4057#endif /* SQLITE_ENABLE_DESERIALIZE */
4058
danb1825882019-04-23 20:48:32 +00004059/*
dan9c014f82019-04-25 19:23:15 +00004060** Scalar function "shell_int32". The first argument to this function
4061** must be a blob. The second a non-negative integer. This function
4062** reads and returns a 32-bit big-endian integer from byte
4063** offset (4*<arg2>) of the blob.
4064*/
4065static void shellInt32(
4066 sqlite3_context *context,
4067 int argc,
4068 sqlite3_value **argv
4069){
4070 const unsigned char *pBlob;
4071 int nBlob;
4072 int iInt;
drh9546c762019-05-10 17:50:33 +00004073
4074 UNUSED_PARAMETER(argc);
dan9c014f82019-04-25 19:23:15 +00004075 nBlob = sqlite3_value_bytes(argv[0]);
4076 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
4077 iInt = sqlite3_value_int(argv[1]);
4078
4079 if( iInt>=0 && (iInt+1)*4<=nBlob ){
4080 const unsigned char *a = &pBlob[iInt*4];
4081 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
4082 + ((sqlite3_int64)a[1]<<16)
4083 + ((sqlite3_int64)a[2]<< 8)
4084 + ((sqlite3_int64)a[3]<< 0);
4085 sqlite3_result_int64(context, iVal);
4086 }
4087}
4088
4089/*
drha2de66c2019-08-06 20:26:17 +00004090** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
4091** using "..." with internal double-quote characters doubled.
4092*/
4093static void shellIdQuote(
4094 sqlite3_context *context,
4095 int argc,
4096 sqlite3_value **argv
4097){
4098 const char *zName = (const char*)sqlite3_value_text(argv[0]);
drh51755a72019-08-08 19:40:29 +00004099 UNUSED_PARAMETER(argc);
drha2de66c2019-08-06 20:26:17 +00004100 if( zName ){
4101 char *z = sqlite3_mprintf("\"%w\"", zName);
4102 sqlite3_result_text(context, z, -1, sqlite3_free);
4103 }
4104}
4105
4106/*
danb1825882019-04-23 20:48:32 +00004107** Scalar function "shell_escape_crnl" used by the .recover command.
4108** The argument passed to this function is the output of built-in
4109** function quote(). If the first character of the input is "'",
4110** indicating that the value passed to quote() was a text value,
4111** then this function searches the input for "\n" and "\r" characters
4112** and adds a wrapper similar to the following:
4113**
4114** replace(replace(<input>, '\n', char(10), '\r', char(13));
4115**
4116** Or, if the first character of the input is not "'", then a copy
4117** of the input is returned.
4118*/
4119static void shellEscapeCrnl(
4120 sqlite3_context *context,
4121 int argc,
4122 sqlite3_value **argv
4123){
4124 const char *zText = (const char*)sqlite3_value_text(argv[0]);
drh9546c762019-05-10 17:50:33 +00004125 UNUSED_PARAMETER(argc);
danb1825882019-04-23 20:48:32 +00004126 if( zText[0]=='\'' ){
4127 int nText = sqlite3_value_bytes(argv[0]);
4128 int i;
4129 char zBuf1[20];
4130 char zBuf2[20];
4131 const char *zNL = 0;
4132 const char *zCR = 0;
4133 int nCR = 0;
4134 int nNL = 0;
4135
4136 for(i=0; zText[i]; i++){
4137 if( zNL==0 && zText[i]=='\n' ){
4138 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
4139 nNL = (int)strlen(zNL);
4140 }
4141 if( zCR==0 && zText[i]=='\r' ){
4142 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
4143 nCR = (int)strlen(zCR);
4144 }
4145 }
4146
4147 if( zNL || zCR ){
4148 int iOut = 0;
4149 i64 nMax = (nNL > nCR) ? nNL : nCR;
dan51f5ffa2019-04-29 11:41:46 +00004150 i64 nAlloc = nMax * nText + (nMax+64)*2;
danb1825882019-04-23 20:48:32 +00004151 char *zOut = (char*)sqlite3_malloc64(nAlloc);
4152 if( zOut==0 ){
4153 sqlite3_result_error_nomem(context);
4154 return;
4155 }
4156
4157 if( zNL && zCR ){
4158 memcpy(&zOut[iOut], "replace(replace(", 16);
4159 iOut += 16;
4160 }else{
4161 memcpy(&zOut[iOut], "replace(", 8);
4162 iOut += 8;
4163 }
4164 for(i=0; zText[i]; i++){
4165 if( zText[i]=='\n' ){
4166 memcpy(&zOut[iOut], zNL, nNL);
4167 iOut += nNL;
4168 }else if( zText[i]=='\r' ){
4169 memcpy(&zOut[iOut], zCR, nCR);
4170 iOut += nCR;
4171 }else{
4172 zOut[iOut] = zText[i];
4173 iOut++;
4174 }
4175 }
4176
4177 if( zNL ){
4178 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4179 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
4180 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
4181 }
4182 if( zCR ){
4183 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
4184 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
4185 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
4186 }
4187
4188 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
4189 sqlite3_free(zOut);
4190 return;
4191 }
4192 }
4193
4194 sqlite3_result_value(context, argv[0]);
4195}
4196
drhbe4ccb22018-05-17 20:04:24 +00004197/* Flags for open_db().
4198**
4199** The default behavior of open_db() is to exit(1) if the database fails to
4200** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
4201** but still returns without calling exit.
4202**
4203** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
4204** ZIP archive if the file does not exist or is empty and its name matches
4205** the *.zip pattern.
4206*/
4207#define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
4208#define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
4209
drh1fa6d9f2018-01-06 21:46:01 +00004210/*
drh2ce15c32017-07-11 13:34:40 +00004211** Make sure the database is open. If it is not, then open it. If
4212** the database fails to open, print an error message and exit.
4213*/
drhbe4ccb22018-05-17 20:04:24 +00004214static void open_db(ShellState *p, int openFlags){
drh2ce15c32017-07-11 13:34:40 +00004215 if( p->db==0 ){
drhf2072d12018-05-11 15:10:11 +00004216 if( p->openMode==SHELL_OPEN_UNSPEC ){
4217 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
4218 p->openMode = SHELL_OPEN_NORMAL;
drhbe4ccb22018-05-17 20:04:24 +00004219 }else{
4220 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
4221 (openFlags & OPEN_DB_ZIPFILE)!=0);
drhf2072d12018-05-11 15:10:11 +00004222 }
drh1fa6d9f2018-01-06 21:46:01 +00004223 }
4224 switch( p->openMode ){
4225 case SHELL_OPEN_APPENDVFS: {
4226 sqlite3_open_v2(p->zDbFilename, &p->db,
drh0933aad2019-11-18 17:46:38 +00004227 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
drh1fa6d9f2018-01-06 21:46:01 +00004228 break;
4229 }
drh33746482018-12-13 15:06:26 +00004230 case SHELL_OPEN_HEXDB:
drh60f34ae2018-10-30 13:19:49 +00004231 case SHELL_OPEN_DESERIALIZE: {
4232 sqlite3_open(0, &p->db);
4233 break;
4234 }
drh1fa6d9f2018-01-06 21:46:01 +00004235 case SHELL_OPEN_ZIPFILE: {
4236 sqlite3_open(":memory:", &p->db);
4237 break;
4238 }
drhee269a62018-02-14 23:27:43 +00004239 case SHELL_OPEN_READONLY: {
drh0933aad2019-11-18 17:46:38 +00004240 sqlite3_open_v2(p->zDbFilename, &p->db,
4241 SQLITE_OPEN_READONLY|p->openFlags, 0);
drhee269a62018-02-14 23:27:43 +00004242 break;
4243 }
drh1fa6d9f2018-01-06 21:46:01 +00004244 case SHELL_OPEN_UNSPEC:
4245 case SHELL_OPEN_NORMAL: {
drh0933aad2019-11-18 17:46:38 +00004246 sqlite3_open_v2(p->zDbFilename, &p->db,
4247 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
drh1fa6d9f2018-01-06 21:46:01 +00004248 break;
4249 }
4250 }
drh2ce15c32017-07-11 13:34:40 +00004251 globalDb = p->db;
4252 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
4253 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
4254 p->zDbFilename, sqlite3_errmsg(p->db));
drhf25cc4f2019-01-04 14:29:21 +00004255 if( openFlags & OPEN_DB_KEEPALIVE ){
4256 sqlite3_open(":memory:", &p->db);
4257 return;
4258 }
drh2ce15c32017-07-11 13:34:40 +00004259 exit(1);
4260 }
4261#ifndef SQLITE_OMIT_LOAD_EXTENSION
4262 sqlite3_enable_load_extension(p->db, 1);
4263#endif
4264 sqlite3_fileio_init(p->db, 0, 0);
4265 sqlite3_shathree_init(p->db, 0, 0);
drh56eb09b2017-07-11 13:59:07 +00004266 sqlite3_completion_init(p->db, 0, 0);
drhf05dd032020-04-14 15:53:58 +00004267 sqlite3_uint_init(p->db, 0, 0);
dan1b162162019-04-27 20:15:15 +00004268#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
dan68cb86e2019-04-20 20:57:28 +00004269 sqlite3_dbdata_init(p->db, 0, 0);
dan1b162162019-04-27 20:15:15 +00004270#endif
dan72afc3c2017-12-05 18:32:40 +00004271#ifdef SQLITE_HAVE_ZLIB
dan9ebfaad2017-12-26 20:39:58 +00004272 sqlite3_zipfile_init(p->db, 0, 0);
dand1b51d42017-12-16 19:11:26 +00004273 sqlite3_sqlar_init(p->db, 0, 0);
dan72afc3c2017-12-05 18:32:40 +00004274#endif
drhceba7922018-01-01 21:28:25 +00004275 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
drh2ce15c32017-07-11 13:34:40 +00004276 shellAddSchemaName, 0, 0);
drh667a2a22018-01-02 00:04:37 +00004277 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
4278 shellModuleSchema, 0, 0);
drh634c70f2018-01-10 16:50:18 +00004279 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
4280 shellPutsFunc, 0, 0);
danb1825882019-04-23 20:48:32 +00004281 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
4282 shellEscapeCrnl, 0, 0);
dan9c014f82019-04-25 19:23:15 +00004283 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
4284 shellInt32, 0, 0);
drha2de66c2019-08-06 20:26:17 +00004285 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
4286 shellIdQuote, 0, 0);
drh04a28c32018-01-31 01:38:44 +00004287#ifndef SQLITE_NOHAVE_SYSTEM
drh97913132018-01-11 00:04:00 +00004288 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
4289 editFunc, 0, 0);
4290 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
4291 editFunc, 0, 0);
drh04a28c32018-01-31 01:38:44 +00004292#endif
drh1fa6d9f2018-01-06 21:46:01 +00004293 if( p->openMode==SHELL_OPEN_ZIPFILE ){
4294 char *zSql = sqlite3_mprintf(
4295 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
4296 sqlite3_exec(p->db, zSql, 0, 0, 0);
4297 sqlite3_free(zSql);
drha751f392018-10-30 15:31:22 +00004298 }
4299#ifdef SQLITE_ENABLE_DESERIALIZE
drh33746482018-12-13 15:06:26 +00004300 else
4301 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
mistachkin99490932018-12-17 22:19:57 +00004302 int rc;
drh60f34ae2018-10-30 13:19:49 +00004303 int nData = 0;
drh33746482018-12-13 15:06:26 +00004304 unsigned char *aData;
4305 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
4306 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
4307 }else{
4308 aData = readHexDb(p, &nData);
4309 if( aData==0 ){
drh33746482018-12-13 15:06:26 +00004310 return;
4311 }
4312 }
mistachkin99490932018-12-17 22:19:57 +00004313 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
drh60f34ae2018-10-30 13:19:49 +00004314 SQLITE_DESERIALIZE_RESIZEABLE |
4315 SQLITE_DESERIALIZE_FREEONCLOSE);
4316 if( rc ){
4317 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
4318 }
drh6ca64482019-01-22 16:06:20 +00004319 if( p->szMax>0 ){
4320 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
4321 }
drh1fa6d9f2018-01-06 21:46:01 +00004322 }
drha751f392018-10-30 15:31:22 +00004323#endif
drh2ce15c32017-07-11 13:34:40 +00004324 }
4325}
4326
drh9e804032018-05-18 17:11:50 +00004327/*
4328** Attempt to close the databaes connection. Report errors.
4329*/
4330void close_db(sqlite3 *db){
4331 int rc = sqlite3_close(db);
4332 if( rc ){
4333 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
4334 rc, sqlite3_errmsg(db));
4335 }
4336}
4337
drh56eb09b2017-07-11 13:59:07 +00004338#if HAVE_READLINE || HAVE_EDITLINE
4339/*
4340** Readline completion callbacks
4341*/
4342static char *readline_completion_generator(const char *text, int state){
4343 static sqlite3_stmt *pStmt = 0;
4344 char *zRet;
4345 if( state==0 ){
4346 char *zSql;
drh56eb09b2017-07-11 13:59:07 +00004347 sqlite3_finalize(pStmt);
4348 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4349 " FROM completion(%Q) ORDER BY 1", text);
4350 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4351 sqlite3_free(zSql);
4352 }
4353 if( sqlite3_step(pStmt)==SQLITE_ROW ){
drh968d8712017-07-14 00:28:28 +00004354 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
drh56eb09b2017-07-11 13:59:07 +00004355 }else{
4356 sqlite3_finalize(pStmt);
4357 pStmt = 0;
4358 zRet = 0;
4359 }
4360 return zRet;
4361}
4362static char **readline_completion(const char *zText, int iStart, int iEnd){
4363 rl_attempted_completion_over = 1;
4364 return rl_completion_matches(zText, readline_completion_generator);
4365}
4366
4367#elif HAVE_LINENOISE
4368/*
4369** Linenoise completion callback
4370*/
4371static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
drhaf2770f2018-01-05 14:55:43 +00004372 int nLine = strlen30(zLine);
drh56eb09b2017-07-11 13:59:07 +00004373 int i, iStart;
4374 sqlite3_stmt *pStmt = 0;
4375 char *zSql;
4376 char zBuf[1000];
4377
4378 if( nLine>sizeof(zBuf)-30 ) return;
drh1615c372018-05-12 23:56:22 +00004379 if( zLine[0]=='.' || zLine[0]=='#') return;
drh56eb09b2017-07-11 13:59:07 +00004380 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
4381 if( i==nLine-1 ) return;
4382 iStart = i+1;
4383 memcpy(zBuf, zLine, iStart);
4384 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
4385 " FROM completion(%Q,%Q) ORDER BY 1",
4386 &zLine[iStart], zLine);
4387 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
4388 sqlite3_free(zSql);
4389 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
4390 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4391 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
4392 int nCompletion = sqlite3_column_bytes(pStmt, 0);
4393 if( iStart+nCompletion < sizeof(zBuf)-1 ){
4394 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
4395 linenoiseAddCompletion(lc, zBuf);
4396 }
4397 }
4398 sqlite3_finalize(pStmt);
4399}
4400#endif
4401
drh2ce15c32017-07-11 13:34:40 +00004402/*
4403** Do C-language style dequoting.
4404**
4405** \a -> alarm
4406** \b -> backspace
4407** \t -> tab
4408** \n -> newline
4409** \v -> vertical tab
4410** \f -> form feed
4411** \r -> carriage return
4412** \s -> space
4413** \" -> "
4414** \' -> '
4415** \\ -> backslash
4416** \NNN -> ascii character NNN in octal
4417*/
4418static void resolve_backslashes(char *z){
4419 int i, j;
4420 char c;
4421 while( *z && *z!='\\' ) z++;
4422 for(i=j=0; (c = z[i])!=0; i++, j++){
4423 if( c=='\\' && z[i+1]!=0 ){
4424 c = z[++i];
4425 if( c=='a' ){
4426 c = '\a';
4427 }else if( c=='b' ){
4428 c = '\b';
4429 }else if( c=='t' ){
4430 c = '\t';
4431 }else if( c=='n' ){
4432 c = '\n';
4433 }else if( c=='v' ){
4434 c = '\v';
4435 }else if( c=='f' ){
4436 c = '\f';
4437 }else if( c=='r' ){
4438 c = '\r';
4439 }else if( c=='"' ){
4440 c = '"';
4441 }else if( c=='\'' ){
4442 c = '\'';
4443 }else if( c=='\\' ){
4444 c = '\\';
4445 }else if( c>='0' && c<='7' ){
4446 c -= '0';
4447 if( z[i+1]>='0' && z[i+1]<='7' ){
4448 i++;
4449 c = (c<<3) + z[i] - '0';
4450 if( z[i+1]>='0' && z[i+1]<='7' ){
4451 i++;
4452 c = (c<<3) + z[i] - '0';
4453 }
4454 }
4455 }
4456 }
4457 z[j] = c;
4458 }
4459 if( j<i ) z[j] = 0;
4460}
4461
4462/*
drh2ce15c32017-07-11 13:34:40 +00004463** Interpret zArg as either an integer or a boolean value. Return 1 or 0
4464** for TRUE and FALSE. Return the integer value if appropriate.
4465*/
4466static int booleanValue(const char *zArg){
4467 int i;
4468 if( zArg[0]=='0' && zArg[1]=='x' ){
4469 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
4470 }else{
4471 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
4472 }
4473 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
4474 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
4475 return 1;
4476 }
4477 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
4478 return 0;
4479 }
4480 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
4481 zArg);
4482 return 0;
4483}
4484
4485/*
4486** Set or clear a shell flag according to a boolean value.
4487*/
4488static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
4489 if( booleanValue(zArg) ){
4490 ShellSetFlag(p, mFlag);
4491 }else{
4492 ShellClearFlag(p, mFlag);
4493 }
4494}
4495
4496/*
4497** Close an output file, assuming it is not stderr or stdout
4498*/
4499static void output_file_close(FILE *f){
4500 if( f && f!=stdout && f!=stderr ) fclose(f);
4501}
4502
4503/*
4504** Try to open an output file. The names "stdout" and "stderr" are
4505** recognized and do the right thing. NULL is returned if the output
4506** filename is "off".
4507*/
drha92a01a2018-01-10 22:15:37 +00004508static FILE *output_file_open(const char *zFile, int bTextMode){
drh2ce15c32017-07-11 13:34:40 +00004509 FILE *f;
4510 if( strcmp(zFile,"stdout")==0 ){
4511 f = stdout;
4512 }else if( strcmp(zFile, "stderr")==0 ){
4513 f = stderr;
4514 }else if( strcmp(zFile, "off")==0 ){
4515 f = 0;
4516 }else{
drha92a01a2018-01-10 22:15:37 +00004517 f = fopen(zFile, bTextMode ? "w" : "wb");
drh2ce15c32017-07-11 13:34:40 +00004518 if( f==0 ){
4519 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4520 }
4521 }
4522 return f;
4523}
4524
drh707821f2018-12-05 13:39:06 +00004525#ifndef SQLITE_OMIT_TRACE
drh2ce15c32017-07-11 13:34:40 +00004526/*
4527** A routine for handling output from sqlite3_trace().
4528*/
4529static int sql_trace_callback(
drh707821f2018-12-05 13:39:06 +00004530 unsigned mType, /* The trace type */
4531 void *pArg, /* The ShellState pointer */
4532 void *pP, /* Usually a pointer to sqlite_stmt */
4533 void *pX /* Auxiliary output */
drh2ce15c32017-07-11 13:34:40 +00004534){
drh707821f2018-12-05 13:39:06 +00004535 ShellState *p = (ShellState*)pArg;
4536 sqlite3_stmt *pStmt;
4537 const char *zSql;
4538 int nSql;
4539 if( p->traceOut==0 ) return 0;
4540 if( mType==SQLITE_TRACE_CLOSE ){
4541 utf8_printf(p->traceOut, "-- closing database connection\n");
4542 return 0;
4543 }
4544 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
4545 zSql = (const char*)pX;
4546 }else{
4547 pStmt = (sqlite3_stmt*)pP;
4548 switch( p->eTraceType ){
4549 case SHELL_TRACE_EXPANDED: {
4550 zSql = sqlite3_expanded_sql(pStmt);
4551 break;
4552 }
4553#ifdef SQLITE_ENABLE_NORMALIZE
4554 case SHELL_TRACE_NORMALIZED: {
4555 zSql = sqlite3_normalized_sql(pStmt);
4556 break;
4557 }
4558#endif
4559 default: {
4560 zSql = sqlite3_sql(pStmt);
4561 break;
4562 }
4563 }
4564 }
4565 if( zSql==0 ) return 0;
4566 nSql = strlen30(zSql);
4567 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
4568 switch( mType ){
4569 case SQLITE_TRACE_ROW:
4570 case SQLITE_TRACE_STMT: {
4571 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
4572 break;
4573 }
4574 case SQLITE_TRACE_PROFILE: {
4575 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
4576 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
4577 break;
4578 }
drh2ce15c32017-07-11 13:34:40 +00004579 }
4580 return 0;
4581}
4582#endif
drh2ce15c32017-07-11 13:34:40 +00004583
4584/*
4585** A no-op routine that runs with the ".breakpoint" doc-command. This is
4586** a useful spot to set a debugger breakpoint.
4587*/
4588static void test_breakpoint(void){
4589 static int nCall = 0;
4590 nCall++;
4591}
4592
4593/*
4594** An object used to read a CSV and other files for import.
4595*/
4596typedef struct ImportCtx ImportCtx;
4597struct ImportCtx {
4598 const char *zFile; /* Name of the input file */
4599 FILE *in; /* Read the CSV text from this input stream */
4600 char *z; /* Accumulated text for a field */
4601 int n; /* Number of bytes in z */
4602 int nAlloc; /* Space allocated for z[] */
4603 int nLine; /* Current line number */
drhccb37812020-03-09 15:39:39 +00004604 int nRow; /* Number of rows imported */
4605 int nErr; /* Number of errors encountered */
drh2ce15c32017-07-11 13:34:40 +00004606 int bNotFirst; /* True if one or more bytes already read */
4607 int cTerm; /* Character that terminated the most recent field */
4608 int cColSep; /* The column separator character. (Usually ",") */
4609 int cRowSep; /* The row separator character. (Usually "\n") */
4610};
4611
4612/* Append a single byte to z[] */
4613static void import_append_char(ImportCtx *p, int c){
4614 if( p->n+1>=p->nAlloc ){
4615 p->nAlloc += p->nAlloc + 100;
4616 p->z = sqlite3_realloc64(p->z, p->nAlloc);
drh4b5345c2018-04-24 13:07:40 +00004617 if( p->z==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00004618 }
4619 p->z[p->n++] = (char)c;
4620}
4621
4622/* Read a single field of CSV text. Compatible with rfc4180 and extended
4623** with the option of having a separator other than ",".
4624**
4625** + Input comes from p->in.
4626** + Store results in p->z of length p->n. Space to hold p->z comes
4627** from sqlite3_malloc64().
4628** + Use p->cSep as the column separator. The default is ",".
4629** + Use p->rSep as the row separator. The default is "\n".
4630** + Keep track of the line number in p->nLine.
4631** + Store the character that terminates the field in p->cTerm. Store
4632** EOF on end-of-file.
4633** + Report syntax errors on stderr
4634*/
4635static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
4636 int c;
4637 int cSep = p->cColSep;
4638 int rSep = p->cRowSep;
4639 p->n = 0;
4640 c = fgetc(p->in);
4641 if( c==EOF || seenInterrupt ){
4642 p->cTerm = EOF;
4643 return 0;
4644 }
4645 if( c=='"' ){
4646 int pc, ppc;
4647 int startLine = p->nLine;
4648 int cQuote = c;
4649 pc = ppc = 0;
4650 while( 1 ){
4651 c = fgetc(p->in);
4652 if( c==rSep ) p->nLine++;
4653 if( c==cQuote ){
4654 if( pc==cQuote ){
4655 pc = 0;
4656 continue;
4657 }
4658 }
4659 if( (c==cSep && pc==cQuote)
4660 || (c==rSep && pc==cQuote)
4661 || (c==rSep && pc=='\r' && ppc==cQuote)
4662 || (c==EOF && pc==cQuote)
4663 ){
4664 do{ p->n--; }while( p->z[p->n]!=cQuote );
4665 p->cTerm = c;
4666 break;
4667 }
4668 if( pc==cQuote && c!='\r' ){
4669 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
4670 p->zFile, p->nLine, cQuote);
4671 }
4672 if( c==EOF ){
4673 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
4674 p->zFile, startLine, cQuote);
4675 p->cTerm = c;
4676 break;
4677 }
4678 import_append_char(p, c);
4679 ppc = pc;
4680 pc = c;
4681 }
4682 }else{
4683 /* If this is the first field being parsed and it begins with the
4684 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
4685 if( (c&0xff)==0xef && p->bNotFirst==0 ){
4686 import_append_char(p, c);
4687 c = fgetc(p->in);
4688 if( (c&0xff)==0xbb ){
4689 import_append_char(p, c);
4690 c = fgetc(p->in);
4691 if( (c&0xff)==0xbf ){
4692 p->bNotFirst = 1;
4693 p->n = 0;
4694 return csv_read_one_field(p);
4695 }
4696 }
4697 }
4698 while( c!=EOF && c!=cSep && c!=rSep ){
4699 import_append_char(p, c);
4700 c = fgetc(p->in);
4701 }
4702 if( c==rSep ){
4703 p->nLine++;
4704 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
4705 }
4706 p->cTerm = c;
4707 }
4708 if( p->z ) p->z[p->n] = 0;
4709 p->bNotFirst = 1;
4710 return p->z;
4711}
4712
4713/* Read a single field of ASCII delimited text.
4714**
4715** + Input comes from p->in.
4716** + Store results in p->z of length p->n. Space to hold p->z comes
4717** from sqlite3_malloc64().
4718** + Use p->cSep as the column separator. The default is "\x1F".
4719** + Use p->rSep as the row separator. The default is "\x1E".
4720** + Keep track of the row number in p->nLine.
4721** + Store the character that terminates the field in p->cTerm. Store
4722** EOF on end-of-file.
4723** + Report syntax errors on stderr
4724*/
4725static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
4726 int c;
4727 int cSep = p->cColSep;
4728 int rSep = p->cRowSep;
4729 p->n = 0;
4730 c = fgetc(p->in);
4731 if( c==EOF || seenInterrupt ){
4732 p->cTerm = EOF;
4733 return 0;
4734 }
4735 while( c!=EOF && c!=cSep && c!=rSep ){
4736 import_append_char(p, c);
4737 c = fgetc(p->in);
4738 }
4739 if( c==rSep ){
4740 p->nLine++;
4741 }
4742 p->cTerm = c;
4743 if( p->z ) p->z[p->n] = 0;
4744 return p->z;
4745}
4746
4747/*
4748** Try to transfer data for table zTable. If an error is seen while
4749** moving forward, try to go backwards. The backwards movement won't
4750** work for WITHOUT ROWID tables.
4751*/
4752static void tryToCloneData(
4753 ShellState *p,
4754 sqlite3 *newDb,
4755 const char *zTable
4756){
4757 sqlite3_stmt *pQuery = 0;
4758 sqlite3_stmt *pInsert = 0;
4759 char *zQuery = 0;
4760 char *zInsert = 0;
4761 int rc;
4762 int i, j, n;
drhaf2770f2018-01-05 14:55:43 +00004763 int nTable = strlen30(zTable);
drh2ce15c32017-07-11 13:34:40 +00004764 int k = 0;
4765 int cnt = 0;
4766 const int spinRate = 10000;
4767
4768 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
4769 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4770 if( rc ){
4771 utf8_printf(stderr, "Error %d: %s on [%s]\n",
4772 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4773 zQuery);
4774 goto end_data_xfer;
4775 }
4776 n = sqlite3_column_count(pQuery);
4777 zInsert = sqlite3_malloc64(200 + nTable + n*3);
drh4b5345c2018-04-24 13:07:40 +00004778 if( zInsert==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00004779 sqlite3_snprintf(200+nTable,zInsert,
4780 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
drhaf2770f2018-01-05 14:55:43 +00004781 i = strlen30(zInsert);
drh2ce15c32017-07-11 13:34:40 +00004782 for(j=1; j<n; j++){
4783 memcpy(zInsert+i, ",?", 2);
4784 i += 2;
4785 }
4786 memcpy(zInsert+i, ");", 3);
4787 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
4788 if( rc ){
4789 utf8_printf(stderr, "Error %d: %s on [%s]\n",
4790 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
4791 zQuery);
4792 goto end_data_xfer;
4793 }
4794 for(k=0; k<2; k++){
4795 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4796 for(i=0; i<n; i++){
4797 switch( sqlite3_column_type(pQuery, i) ){
4798 case SQLITE_NULL: {
4799 sqlite3_bind_null(pInsert, i+1);
4800 break;
4801 }
4802 case SQLITE_INTEGER: {
4803 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
4804 break;
4805 }
4806 case SQLITE_FLOAT: {
4807 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
4808 break;
4809 }
4810 case SQLITE_TEXT: {
4811 sqlite3_bind_text(pInsert, i+1,
4812 (const char*)sqlite3_column_text(pQuery,i),
4813 -1, SQLITE_STATIC);
4814 break;
4815 }
4816 case SQLITE_BLOB: {
4817 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
4818 sqlite3_column_bytes(pQuery,i),
4819 SQLITE_STATIC);
4820 break;
4821 }
4822 }
4823 } /* End for */
4824 rc = sqlite3_step(pInsert);
4825 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
4826 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
4827 sqlite3_errmsg(newDb));
4828 }
4829 sqlite3_reset(pInsert);
4830 cnt++;
4831 if( (cnt%spinRate)==0 ){
4832 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
4833 fflush(stdout);
4834 }
4835 } /* End while */
4836 if( rc==SQLITE_DONE ) break;
4837 sqlite3_finalize(pQuery);
4838 sqlite3_free(zQuery);
4839 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4840 zTable);
4841 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4842 if( rc ){
4843 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
4844 break;
4845 }
4846 } /* End for(k=0...) */
4847
4848end_data_xfer:
4849 sqlite3_finalize(pQuery);
4850 sqlite3_finalize(pInsert);
4851 sqlite3_free(zQuery);
4852 sqlite3_free(zInsert);
4853}
4854
4855
4856/*
4857** Try to transfer all rows of the schema that match zWhere. For
4858** each row, invoke xForEach() on the object defined by that row.
4859** If an error is encountered while moving forward through the
4860** sqlite_master table, try again moving backwards.
4861*/
4862static void tryToCloneSchema(
4863 ShellState *p,
4864 sqlite3 *newDb,
4865 const char *zWhere,
4866 void (*xForEach)(ShellState*,sqlite3*,const char*)
4867){
4868 sqlite3_stmt *pQuery = 0;
4869 char *zQuery = 0;
4870 int rc;
4871 const unsigned char *zName;
4872 const unsigned char *zSql;
4873 char *zErrMsg = 0;
4874
4875 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4876 " WHERE %s", zWhere);
4877 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4878 if( rc ){
4879 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4880 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4881 zQuery);
4882 goto end_schema_xfer;
4883 }
4884 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4885 zName = sqlite3_column_text(pQuery, 0);
4886 zSql = sqlite3_column_text(pQuery, 1);
4887 printf("%s... ", zName); fflush(stdout);
4888 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4889 if( zErrMsg ){
4890 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4891 sqlite3_free(zErrMsg);
4892 zErrMsg = 0;
4893 }
4894 if( xForEach ){
4895 xForEach(p, newDb, (const char*)zName);
4896 }
4897 printf("done\n");
4898 }
4899 if( rc!=SQLITE_DONE ){
4900 sqlite3_finalize(pQuery);
4901 sqlite3_free(zQuery);
4902 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4903 " WHERE %s ORDER BY rowid DESC", zWhere);
4904 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
4905 if( rc ){
4906 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
4907 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
4908 zQuery);
4909 goto end_schema_xfer;
4910 }
4911 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
4912 zName = sqlite3_column_text(pQuery, 0);
4913 zSql = sqlite3_column_text(pQuery, 1);
4914 printf("%s... ", zName); fflush(stdout);
4915 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
4916 if( zErrMsg ){
4917 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
4918 sqlite3_free(zErrMsg);
4919 zErrMsg = 0;
4920 }
4921 if( xForEach ){
4922 xForEach(p, newDb, (const char*)zName);
4923 }
4924 printf("done\n");
4925 }
4926 }
4927end_schema_xfer:
4928 sqlite3_finalize(pQuery);
4929 sqlite3_free(zQuery);
4930}
4931
4932/*
4933** Open a new database file named "zNewDb". Try to recover as much information
4934** as possible out of the main database (which might be corrupt) and write it
4935** into zNewDb.
4936*/
4937static void tryToClone(ShellState *p, const char *zNewDb){
4938 int rc;
4939 sqlite3 *newDb = 0;
4940 if( access(zNewDb,0)==0 ){
4941 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
4942 return;
4943 }
4944 rc = sqlite3_open(zNewDb, &newDb);
4945 if( rc ){
4946 utf8_printf(stderr, "Cannot create output database: %s\n",
4947 sqlite3_errmsg(newDb));
4948 }else{
4949 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
4950 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
4951 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
4952 tryToCloneSchema(p, newDb, "type!='table'", 0);
4953 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
4954 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4955 }
drh9e804032018-05-18 17:11:50 +00004956 close_db(newDb);
drh2ce15c32017-07-11 13:34:40 +00004957}
4958
4959/*
drh13c20932018-01-10 21:41:55 +00004960** Change the output file back to stdout.
4961**
4962** If the p->doXdgOpen flag is set, that means the output was being
4963** redirected to a temporary file named by p->zTempFile. In that case,
4964** launch start/open/xdg-open on that temporary file.
drh2ce15c32017-07-11 13:34:40 +00004965*/
4966static void output_reset(ShellState *p){
4967 if( p->outfile[0]=='|' ){
4968#ifndef SQLITE_OMIT_POPEN
4969 pclose(p->out);
4970#endif
4971 }else{
4972 output_file_close(p->out);
drh04a28c32018-01-31 01:38:44 +00004973#ifndef SQLITE_NOHAVE_SYSTEM
drh13c20932018-01-10 21:41:55 +00004974 if( p->doXdgOpen ){
4975 const char *zXdgOpenCmd =
4976#if defined(_WIN32)
4977 "start";
4978#elif defined(__APPLE__)
4979 "open";
4980#else
4981 "xdg-open";
4982#endif
4983 char *zCmd;
4984 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
drha92a01a2018-01-10 22:15:37 +00004985 if( system(zCmd) ){
4986 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
drh1d9ea272020-04-17 23:46:54 +00004987 }else{
4988 /* Give the start/open/xdg-open command some time to get
4989 ** going before we continue, and potential delete the
4990 ** p->zTempFile data file out from under it */
4991 sqlite3_sleep(2000);
drha92a01a2018-01-10 22:15:37 +00004992 }
drh13c20932018-01-10 21:41:55 +00004993 sqlite3_free(zCmd);
drh3c484e82018-01-10 22:27:21 +00004994 outputModePop(p);
drh13c20932018-01-10 21:41:55 +00004995 p->doXdgOpen = 0;
4996 }
drh04a28c32018-01-31 01:38:44 +00004997#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
drh2ce15c32017-07-11 13:34:40 +00004998 }
4999 p->outfile[0] = 0;
5000 p->out = stdout;
5001}
5002
5003/*
5004** Run an SQL command and return the single integer result.
5005*/
5006static int db_int(ShellState *p, const char *zSql){
5007 sqlite3_stmt *pStmt;
5008 int res = 0;
5009 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5010 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
5011 res = sqlite3_column_int(pStmt,0);
5012 }
5013 sqlite3_finalize(pStmt);
5014 return res;
5015}
5016
5017/*
5018** Convert a 2-byte or 4-byte big-endian integer into a native integer
5019*/
5020static unsigned int get2byteInt(unsigned char *a){
5021 return (a[0]<<8) + a[1];
5022}
5023static unsigned int get4byteInt(unsigned char *a){
5024 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
5025}
5026
5027/*
drh76c12062020-01-14 13:13:19 +00005028** Implementation of the ".dbinfo" command.
drh2ce15c32017-07-11 13:34:40 +00005029**
5030** Return 1 on error, 2 to exit, and 0 otherwise.
5031*/
5032static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
5033 static const struct { const char *zName; int ofst; } aField[] = {
5034 { "file change counter:", 24 },
5035 { "database page count:", 28 },
5036 { "freelist page count:", 36 },
5037 { "schema cookie:", 40 },
5038 { "schema format:", 44 },
5039 { "default cache size:", 48 },
5040 { "autovacuum top root:", 52 },
5041 { "incremental vacuum:", 64 },
5042 { "text encoding:", 56 },
5043 { "user version:", 60 },
5044 { "application id:", 68 },
5045 { "software version:", 96 },
5046 };
5047 static const struct { const char *zName; const char *zSql; } aQuery[] = {
5048 { "number of tables:",
5049 "SELECT count(*) FROM %s WHERE type='table'" },
5050 { "number of indexes:",
5051 "SELECT count(*) FROM %s WHERE type='index'" },
5052 { "number of triggers:",
5053 "SELECT count(*) FROM %s WHERE type='trigger'" },
5054 { "number of views:",
5055 "SELECT count(*) FROM %s WHERE type='view'" },
5056 { "schema size:",
5057 "SELECT total(length(sql)) FROM %s" },
5058 };
drh87c889c2019-03-20 18:22:51 +00005059 int i, rc;
drhea99a312018-07-18 19:09:07 +00005060 unsigned iDataVersion;
drh2ce15c32017-07-11 13:34:40 +00005061 char *zSchemaTab;
5062 char *zDb = nArg>=2 ? azArg[1] : "main";
drh512e6c32017-10-11 17:51:08 +00005063 sqlite3_stmt *pStmt = 0;
drh2ce15c32017-07-11 13:34:40 +00005064 unsigned char aHdr[100];
5065 open_db(p, 0);
5066 if( p->db==0 ) return 1;
drh87c889c2019-03-20 18:22:51 +00005067 rc = sqlite3_prepare_v2(p->db,
5068 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
5069 -1, &pStmt, 0);
5070 if( rc ){
drh451f89a2020-04-28 23:09:56 +00005071 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
drh87c889c2019-03-20 18:22:51 +00005072 sqlite3_finalize(pStmt);
5073 return 1;
5074 }
drh512e6c32017-10-11 17:51:08 +00005075 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
5076 if( sqlite3_step(pStmt)==SQLITE_ROW
5077 && sqlite3_column_bytes(pStmt,0)>100
5078 ){
5079 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
5080 sqlite3_finalize(pStmt);
5081 }else{
drh2ce15c32017-07-11 13:34:40 +00005082 raw_printf(stderr, "unable to read database header\n");
drh512e6c32017-10-11 17:51:08 +00005083 sqlite3_finalize(pStmt);
drh2ce15c32017-07-11 13:34:40 +00005084 return 1;
5085 }
5086 i = get2byteInt(aHdr+16);
5087 if( i==1 ) i = 65536;
5088 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
5089 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
5090 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
5091 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
5092 for(i=0; i<ArraySize(aField); i++){
5093 int ofst = aField[i].ofst;
5094 unsigned int val = get4byteInt(aHdr + ofst);
5095 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
5096 switch( ofst ){
5097 case 56: {
5098 if( val==1 ) raw_printf(p->out, " (utf8)");
5099 if( val==2 ) raw_printf(p->out, " (utf16le)");
5100 if( val==3 ) raw_printf(p->out, " (utf16be)");
5101 }
5102 }
5103 raw_printf(p->out, "\n");
5104 }
5105 if( zDb==0 ){
5106 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
5107 }else if( strcmp(zDb,"temp")==0 ){
5108 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
5109 }else{
5110 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
5111 }
5112 for(i=0; i<ArraySize(aQuery); i++){
5113 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
5114 int val = db_int(p, zSql);
5115 sqlite3_free(zSql);
5116 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
5117 }
5118 sqlite3_free(zSchemaTab);
drhea99a312018-07-18 19:09:07 +00005119 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
5120 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
drh2ce15c32017-07-11 13:34:40 +00005121 return 0;
5122}
5123
5124/*
5125** Print the current sqlite3_errmsg() value to stderr and return 1.
5126*/
5127static int shellDatabaseError(sqlite3 *db){
5128 const char *zErr = sqlite3_errmsg(db);
5129 utf8_printf(stderr, "Error: %s\n", zErr);
5130 return 1;
5131}
5132
5133/*
drh2ce15c32017-07-11 13:34:40 +00005134** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
5135** if they match and FALSE (0) if they do not match.
5136**
5137** Globbing rules:
5138**
5139** '*' Matches any sequence of zero or more characters.
5140**
5141** '?' Matches exactly one character.
5142**
5143** [...] Matches one character from the enclosed list of
5144** characters.
5145**
5146** [^...] Matches one character not in the enclosed list.
5147**
5148** '#' Matches any sequence of one or more digits with an
5149** optional + or - sign in front
5150**
5151** ' ' Any span of whitespace matches any other span of
5152** whitespace.
5153**
5154** Extra whitespace at the end of z[] is ignored.
5155*/
5156static int testcase_glob(const char *zGlob, const char *z){
5157 int c, c2;
5158 int invert;
5159 int seen;
5160
5161 while( (c = (*(zGlob++)))!=0 ){
5162 if( IsSpace(c) ){
5163 if( !IsSpace(*z) ) return 0;
5164 while( IsSpace(*zGlob) ) zGlob++;
5165 while( IsSpace(*z) ) z++;
5166 }else if( c=='*' ){
5167 while( (c=(*(zGlob++))) == '*' || c=='?' ){
5168 if( c=='?' && (*(z++))==0 ) return 0;
5169 }
5170 if( c==0 ){
5171 return 1;
5172 }else if( c=='[' ){
5173 while( *z && testcase_glob(zGlob-1,z)==0 ){
5174 z++;
5175 }
5176 return (*z)!=0;
5177 }
5178 while( (c2 = (*(z++)))!=0 ){
5179 while( c2!=c ){
5180 c2 = *(z++);
5181 if( c2==0 ) return 0;
5182 }
5183 if( testcase_glob(zGlob,z) ) return 1;
5184 }
5185 return 0;
5186 }else if( c=='?' ){
5187 if( (*(z++))==0 ) return 0;
5188 }else if( c=='[' ){
5189 int prior_c = 0;
5190 seen = 0;
5191 invert = 0;
5192 c = *(z++);
5193 if( c==0 ) return 0;
5194 c2 = *(zGlob++);
5195 if( c2=='^' ){
5196 invert = 1;
5197 c2 = *(zGlob++);
5198 }
5199 if( c2==']' ){
5200 if( c==']' ) seen = 1;
5201 c2 = *(zGlob++);
5202 }
5203 while( c2 && c2!=']' ){
5204 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
5205 c2 = *(zGlob++);
5206 if( c>=prior_c && c<=c2 ) seen = 1;
5207 prior_c = 0;
5208 }else{
5209 if( c==c2 ){
5210 seen = 1;
5211 }
5212 prior_c = c2;
5213 }
5214 c2 = *(zGlob++);
5215 }
5216 if( c2==0 || (seen ^ invert)==0 ) return 0;
5217 }else if( c=='#' ){
5218 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
5219 if( !IsDigit(z[0]) ) return 0;
5220 z++;
5221 while( IsDigit(z[0]) ){ z++; }
5222 }else{
5223 if( c!=(*(z++)) ) return 0;
5224 }
5225 }
5226 while( IsSpace(*z) ){ z++; }
5227 return *z==0;
5228}
5229
5230
5231/*
5232** Compare the string as a command-line option with either one or two
5233** initial "-" characters.
5234*/
5235static int optionMatch(const char *zStr, const char *zOpt){
5236 if( zStr[0]!='-' ) return 0;
5237 zStr++;
5238 if( zStr[0]=='-' ) zStr++;
5239 return strcmp(zStr, zOpt)==0;
5240}
5241
5242/*
5243** Delete a file.
5244*/
5245int shellDeleteFile(const char *zFilename){
5246 int rc;
5247#ifdef _WIN32
5248 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
5249 rc = _wunlink(z);
5250 sqlite3_free(z);
5251#else
5252 rc = unlink(zFilename);
5253#endif
5254 return rc;
5255}
5256
drh13c20932018-01-10 21:41:55 +00005257/*
5258** Try to delete the temporary file (if there is one) and free the
5259** memory used to hold the name of the temp file.
5260*/
5261static void clearTempFile(ShellState *p){
5262 if( p->zTempFile==0 ) return;
drh536c3452018-01-11 00:38:39 +00005263 if( p->doXdgOpen ) return;
drh13c20932018-01-10 21:41:55 +00005264 if( shellDeleteFile(p->zTempFile) ) return;
5265 sqlite3_free(p->zTempFile);
5266 p->zTempFile = 0;
5267}
5268
5269/*
5270** Create a new temp file name with the given suffix.
5271*/
5272static void newTempFile(ShellState *p, const char *zSuffix){
5273 clearTempFile(p);
5274 sqlite3_free(p->zTempFile);
5275 p->zTempFile = 0;
drh7f3bf8a2018-01-10 21:50:08 +00005276 if( p->db ){
5277 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
5278 }
drh13c20932018-01-10 21:41:55 +00005279 if( p->zTempFile==0 ){
drh1d9ea272020-04-17 23:46:54 +00005280 /* If p->db is an in-memory database then the TEMPFILENAME file-control
5281 ** will not work and we will need to fallback to guessing */
5282 char *zTemp;
drh13c20932018-01-10 21:41:55 +00005283 sqlite3_uint64 r;
5284 sqlite3_randomness(sizeof(r), &r);
drh1d9ea272020-04-17 23:46:54 +00005285 zTemp = getenv("TEMP");
5286 if( zTemp==0 ) zTemp = getenv("TMP");
5287 if( zTemp==0 ){
5288#ifdef _WIN32
5289 zTemp = "\\tmp";
5290#else
5291 zTemp = "/tmp";
5292#endif
5293 }
5294 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
drh13c20932018-01-10 21:41:55 +00005295 }else{
5296 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
5297 }
5298 if( p->zTempFile==0 ){
5299 raw_printf(stderr, "out of memory\n");
5300 exit(1);
5301 }
5302}
5303
drh2ce15c32017-07-11 13:34:40 +00005304
5305/*
5306** The implementation of SQL scalar function fkey_collate_clause(), used
5307** by the ".lint fkey-indexes" command. This scalar function is always
5308** called with four arguments - the parent table name, the parent column name,
5309** the child table name and the child column name.
5310**
5311** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
5312**
5313** If either of the named tables or columns do not exist, this function
5314** returns an empty string. An empty string is also returned if both tables
5315** and columns exist but have the same default collation sequence. Or,
5316** if both exist but the default collation sequences are different, this
5317** function returns the string " COLLATE <parent-collation>", where
5318** <parent-collation> is the default collation sequence of the parent column.
5319*/
5320static void shellFkeyCollateClause(
5321 sqlite3_context *pCtx,
5322 int nVal,
5323 sqlite3_value **apVal
5324){
5325 sqlite3 *db = sqlite3_context_db_handle(pCtx);
5326 const char *zParent;
5327 const char *zParentCol;
5328 const char *zParentSeq;
5329 const char *zChild;
5330 const char *zChildCol;
5331 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
5332 int rc;
5333
5334 assert( nVal==4 );
5335 zParent = (const char*)sqlite3_value_text(apVal[0]);
5336 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
5337 zChild = (const char*)sqlite3_value_text(apVal[2]);
5338 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
5339
5340 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
5341 rc = sqlite3_table_column_metadata(
5342 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
5343 );
5344 if( rc==SQLITE_OK ){
5345 rc = sqlite3_table_column_metadata(
5346 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
5347 );
5348 }
5349
5350 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
5351 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
5352 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
5353 sqlite3_free(z);
5354 }
5355}
5356
5357
5358/*
5359** The implementation of dot-command ".lint fkey-indexes".
5360*/
5361static int lintFkeyIndexes(
5362 ShellState *pState, /* Current shell tool state */
5363 char **azArg, /* Array of arguments passed to dot command */
5364 int nArg /* Number of entries in azArg[] */
5365){
5366 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
5367 FILE *out = pState->out; /* Stream to write non-error output to */
5368 int bVerbose = 0; /* If -verbose is present */
5369 int bGroupByParent = 0; /* If -groupbyparent is present */
5370 int i; /* To iterate through azArg[] */
5371 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
5372 int rc; /* Return code */
5373 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
5374
5375 /*
5376 ** This SELECT statement returns one row for each foreign key constraint
5377 ** in the schema of the main database. The column values are:
5378 **
5379 ** 0. The text of an SQL statement similar to:
5380 **
danf9679312017-12-01 18:40:18 +00005381 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
drh2ce15c32017-07-11 13:34:40 +00005382 **
danf9679312017-12-01 18:40:18 +00005383 ** This SELECT is similar to the one that the foreign keys implementation
5384 ** needs to run internally on child tables. If there is an index that can
drh2ce15c32017-07-11 13:34:40 +00005385 ** be used to optimize this query, then it can also be used by the FK
5386 ** implementation to optimize DELETE or UPDATE statements on the parent
5387 ** table.
5388 **
5389 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
5390 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
5391 ** contains an index that can be used to optimize the query.
5392 **
5393 ** 2. Human readable text that describes the child table and columns. e.g.
5394 **
5395 ** "child_table(child_key1, child_key2)"
5396 **
5397 ** 3. Human readable text that describes the parent table and columns. e.g.
5398 **
5399 ** "parent_table(parent_key1, parent_key2)"
5400 **
5401 ** 4. A full CREATE INDEX statement for an index that could be used to
5402 ** optimize DELETE or UPDATE statements on the parent table. e.g.
5403 **
5404 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
5405 **
5406 ** 5. The name of the parent table.
5407 **
5408 ** These six values are used by the C logic below to generate the report.
5409 */
5410 const char *zSql =
5411 "SELECT "
danf9679312017-12-01 18:40:18 +00005412 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
drh2ce15c32017-07-11 13:34:40 +00005413 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
5414 " || fkey_collate_clause("
5415 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
5416 ", "
5417 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
5418 " || group_concat('*=?', ' AND ') || ')'"
5419 ", "
5420 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
5421 ", "
5422 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
5423 ", "
5424 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
5425 " || ' ON ' || quote(s.name) || '('"
5426 " || group_concat(quote(f.[from]) ||"
5427 " fkey_collate_clause("
5428 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
5429 " || ');'"
5430 ", "
5431 " f.[table] "
5432 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
5433 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
5434 "GROUP BY s.name, f.id "
5435 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
5436 ;
5437 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
5438
5439 for(i=2; i<nArg; i++){
drhaf2770f2018-01-05 14:55:43 +00005440 int n = strlen30(azArg[i]);
drh2ce15c32017-07-11 13:34:40 +00005441 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
5442 bVerbose = 1;
5443 }
5444 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
5445 bGroupByParent = 1;
5446 zIndent = " ";
5447 }
5448 else{
5449 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
5450 azArg[0], azArg[1]
5451 );
5452 return SQLITE_ERROR;
5453 }
5454 }
5455
5456 /* Register the fkey_collate_clause() SQL function */
5457 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
5458 0, shellFkeyCollateClause, 0, 0
5459 );
5460
5461
5462 if( rc==SQLITE_OK ){
5463 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
5464 }
5465 if( rc==SQLITE_OK ){
5466 sqlite3_bind_int(pSql, 1, bGroupByParent);
5467 }
5468
5469 if( rc==SQLITE_OK ){
5470 int rc2;
5471 char *zPrev = 0;
5472 while( SQLITE_ROW==sqlite3_step(pSql) ){
5473 int res = -1;
5474 sqlite3_stmt *pExplain = 0;
5475 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
5476 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
5477 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
5478 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
5479 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
5480 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
5481
5482 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
5483 if( rc!=SQLITE_OK ) break;
5484 if( SQLITE_ROW==sqlite3_step(pExplain) ){
5485 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
5486 res = (
5487 0==sqlite3_strglob(zGlob, zPlan)
5488 || 0==sqlite3_strglob(zGlobIPK, zPlan)
5489 );
5490 }
5491 rc = sqlite3_finalize(pExplain);
5492 if( rc!=SQLITE_OK ) break;
5493
5494 if( res<0 ){
5495 raw_printf(stderr, "Error: internal error");
5496 break;
5497 }else{
5498 if( bGroupByParent
5499 && (bVerbose || res==0)
5500 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
5501 ){
5502 raw_printf(out, "-- Parent table %s\n", zParent);
5503 sqlite3_free(zPrev);
5504 zPrev = sqlite3_mprintf("%s", zParent);
5505 }
5506
5507 if( res==0 ){
5508 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
5509 }else if( bVerbose ){
5510 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
5511 zIndent, zFrom, zTarget
5512 );
5513 }
5514 }
5515 }
5516 sqlite3_free(zPrev);
5517
5518 if( rc!=SQLITE_OK ){
5519 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5520 }
5521
5522 rc2 = sqlite3_finalize(pSql);
5523 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
5524 rc = rc2;
5525 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5526 }
5527 }else{
5528 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
5529 }
5530
5531 return rc;
5532}
5533
5534/*
5535** Implementation of ".lint" dot command.
5536*/
5537static int lintDotCommand(
5538 ShellState *pState, /* Current shell tool state */
5539 char **azArg, /* Array of arguments passed to dot command */
5540 int nArg /* Number of entries in azArg[] */
5541){
5542 int n;
drhaf2770f2018-01-05 14:55:43 +00005543 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
drh2ce15c32017-07-11 13:34:40 +00005544 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
5545 return lintFkeyIndexes(pState, azArg, nArg);
5546
5547 usage:
5548 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
5549 raw_printf(stderr, "Where sub-commands are:\n");
5550 raw_printf(stderr, " fkey-indexes\n");
5551 return SQLITE_ERROR;
5552}
5553
dan1b162162019-04-27 20:15:15 +00005554#if !defined SQLITE_OMIT_VIRTUALTABLE
danfd0245d2017-12-07 15:44:29 +00005555static void shellPrepare(
dand4b56e52017-12-12 20:04:59 +00005556 sqlite3 *db,
danfd0245d2017-12-07 15:44:29 +00005557 int *pRc,
5558 const char *zSql,
5559 sqlite3_stmt **ppStmt
5560){
5561 *ppStmt = 0;
5562 if( *pRc==SQLITE_OK ){
dand4b56e52017-12-12 20:04:59 +00005563 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
danfd0245d2017-12-07 15:44:29 +00005564 if( rc!=SQLITE_OK ){
5565 raw_printf(stderr, "sql error: %s (%d)\n",
dand4b56e52017-12-12 20:04:59 +00005566 sqlite3_errmsg(db), sqlite3_errcode(db)
danfd0245d2017-12-07 15:44:29 +00005567 );
5568 *pRc = rc;
5569 }
5570 }
5571}
5572
drh9546c762019-05-10 17:50:33 +00005573/*
5574** Create a prepared statement using printf-style arguments for the SQL.
5575**
5576** This routine is could be marked "static". But it is not always used,
5577** depending on compile-time options. By omitting the "static", we avoid
5578** nuisance compiler warnings about "defined but not used".
5579*/
5580void shellPreparePrintf(
dan3f67ddf2017-12-13 20:04:53 +00005581 sqlite3 *db,
5582 int *pRc,
danac15e2d2017-12-14 19:15:07 +00005583 sqlite3_stmt **ppStmt,
5584 const char *zFmt,
5585 ...
dan3f67ddf2017-12-13 20:04:53 +00005586){
danac15e2d2017-12-14 19:15:07 +00005587 *ppStmt = 0;
5588 if( *pRc==SQLITE_OK ){
5589 va_list ap;
5590 char *z;
5591 va_start(ap, zFmt);
5592 z = sqlite3_vmprintf(zFmt, ap);
drh1dbb1472018-10-11 10:37:24 +00005593 va_end(ap);
dan3f67ddf2017-12-13 20:04:53 +00005594 if( z==0 ){
5595 *pRc = SQLITE_NOMEM;
5596 }else{
5597 shellPrepare(db, pRc, z, ppStmt);
5598 sqlite3_free(z);
5599 }
dan3f67ddf2017-12-13 20:04:53 +00005600 }
5601}
5602
drh9546c762019-05-10 17:50:33 +00005603/* Finalize the prepared statement created using shellPreparePrintf().
5604**
5605** This routine is could be marked "static". But it is not always used,
5606** depending on compile-time options. By omitting the "static", we avoid
5607** nuisance compiler warnings about "defined but not used".
5608*/
5609void shellFinalize(
danfd0245d2017-12-07 15:44:29 +00005610 int *pRc,
5611 sqlite3_stmt *pStmt
5612){
dan25c12182017-12-07 21:03:33 +00005613 if( pStmt ){
5614 sqlite3 *db = sqlite3_db_handle(pStmt);
5615 int rc = sqlite3_finalize(pStmt);
5616 if( *pRc==SQLITE_OK ){
5617 if( rc!=SQLITE_OK ){
5618 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5619 }
5620 *pRc = rc;
5621 }
5622 }
danfd0245d2017-12-07 15:44:29 +00005623}
5624
drh9546c762019-05-10 17:50:33 +00005625/* Reset the prepared statement created using shellPreparePrintf().
5626**
5627** This routine is could be marked "static". But it is not always used,
5628** depending on compile-time options. By omitting the "static", we avoid
5629** nuisance compiler warnings about "defined but not used".
5630*/
5631void shellReset(
danfd0245d2017-12-07 15:44:29 +00005632 int *pRc,
5633 sqlite3_stmt *pStmt
5634){
5635 int rc = sqlite3_reset(pStmt);
dan5a78b812017-12-27 18:54:11 +00005636 if( *pRc==SQLITE_OK ){
5637 if( rc!=SQLITE_OK ){
5638 sqlite3 *db = sqlite3_db_handle(pStmt);
5639 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
5640 }
5641 *pRc = rc;
5642 }
danfd0245d2017-12-07 15:44:29 +00005643}
dan1b162162019-04-27 20:15:15 +00005644#endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
5645
5646#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
drhe2754c12019-08-26 12:50:01 +00005647/******************************************************************************
dan1b162162019-04-27 20:15:15 +00005648** The ".archive" or ".ar" command.
5649*/
drhe37c0e12018-01-06 19:19:50 +00005650/*
dan88be0202017-12-09 17:58:02 +00005651** Structure representing a single ".ar" command.
5652*/
5653typedef struct ArCommand ArCommand;
5654struct ArCommand {
drhb376b3d2018-01-10 13:11:51 +00005655 u8 eCmd; /* An AR_CMD_* value */
5656 u8 bVerbose; /* True if --verbose */
drha5676c42018-01-10 15:17:34 +00005657 u8 bZip; /* True if the archive is a ZIP */
drhb376b3d2018-01-10 13:11:51 +00005658 u8 bDryRun; /* True if --dry-run */
drha5676c42018-01-10 15:17:34 +00005659 u8 bAppend; /* True if --append */
drhd0f9cdc2018-05-17 14:09:06 +00005660 u8 fromCmdLine; /* Run from -A instead of .archive */
drhb376b3d2018-01-10 13:11:51 +00005661 int nArg; /* Number of command arguments */
drha5676c42018-01-10 15:17:34 +00005662 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
dan88be0202017-12-09 17:58:02 +00005663 const char *zFile; /* --file argument, or NULL */
5664 const char *zDir; /* --directory argument, or NULL */
dan88be0202017-12-09 17:58:02 +00005665 char **azArg; /* Array of command arguments */
drhb376b3d2018-01-10 13:11:51 +00005666 ShellState *p; /* Shell state */
5667 sqlite3 *db; /* Database containing the archive */
dan88be0202017-12-09 17:58:02 +00005668};
5669
5670/*
5671** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
5672*/
dan0d0547f2017-12-14 15:40:42 +00005673static int arUsage(FILE *f){
drh98aa2ab2018-09-26 16:53:51 +00005674 showHelp(f,"archive");
dan0d0547f2017-12-14 15:40:42 +00005675 return SQLITE_ERROR;
5676}
5677
5678/*
5679** Print an error message for the .ar command to stderr and return
5680** SQLITE_ERROR.
5681*/
drhd0f9cdc2018-05-17 14:09:06 +00005682static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
dan0d0547f2017-12-14 15:40:42 +00005683 va_list ap;
5684 char *z;
5685 va_start(ap, zFmt);
5686 z = sqlite3_vmprintf(zFmt, ap);
5687 va_end(ap);
drhd0f9cdc2018-05-17 14:09:06 +00005688 utf8_printf(stderr, "Error: %s\n", z);
5689 if( pAr->fromCmdLine ){
5690 utf8_printf(stderr, "Use \"-A\" for more help\n");
5691 }else{
5692 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
5693 }
dan0d0547f2017-12-14 15:40:42 +00005694 sqlite3_free(z);
dan88be0202017-12-09 17:58:02 +00005695 return SQLITE_ERROR;
5696}
5697
5698/*
5699** Values for ArCommand.eCmd.
5700*/
dand4b56e52017-12-12 20:04:59 +00005701#define AR_CMD_CREATE 1
drhb17ea912019-03-25 14:24:19 +00005702#define AR_CMD_UPDATE 2
5703#define AR_CMD_INSERT 3
5704#define AR_CMD_EXTRACT 4
5705#define AR_CMD_LIST 5
5706#define AR_CMD_HELP 6
dand4b56e52017-12-12 20:04:59 +00005707
5708/*
5709** Other (non-command) switches.
5710*/
drhb17ea912019-03-25 14:24:19 +00005711#define AR_SWITCH_VERBOSE 7
5712#define AR_SWITCH_FILE 8
5713#define AR_SWITCH_DIRECTORY 9
5714#define AR_SWITCH_APPEND 10
5715#define AR_SWITCH_DRYRUN 11
dand4b56e52017-12-12 20:04:59 +00005716
5717static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
5718 switch( eSwitch ){
5719 case AR_CMD_CREATE:
5720 case AR_CMD_EXTRACT:
5721 case AR_CMD_LIST:
5722 case AR_CMD_UPDATE:
drhb17ea912019-03-25 14:24:19 +00005723 case AR_CMD_INSERT:
dan0d0547f2017-12-14 15:40:42 +00005724 case AR_CMD_HELP:
5725 if( pAr->eCmd ){
drhd0f9cdc2018-05-17 14:09:06 +00005726 return arErrorMsg(pAr, "multiple command options");
dan0d0547f2017-12-14 15:40:42 +00005727 }
dand4b56e52017-12-12 20:04:59 +00005728 pAr->eCmd = eSwitch;
5729 break;
5730
drhb376b3d2018-01-10 13:11:51 +00005731 case AR_SWITCH_DRYRUN:
5732 pAr->bDryRun = 1;
5733 break;
dand4b56e52017-12-12 20:04:59 +00005734 case AR_SWITCH_VERBOSE:
5735 pAr->bVerbose = 1;
5736 break;
drha5676c42018-01-10 15:17:34 +00005737 case AR_SWITCH_APPEND:
5738 pAr->bAppend = 1;
drhca7733b2018-01-10 18:09:20 +00005739 /* Fall thru into --file */
dand4b56e52017-12-12 20:04:59 +00005740 case AR_SWITCH_FILE:
5741 pAr->zFile = zArg;
5742 break;
5743 case AR_SWITCH_DIRECTORY:
5744 pAr->zDir = zArg;
5745 break;
5746 }
5747
5748 return SQLITE_OK;
5749}
dan88be0202017-12-09 17:58:02 +00005750
5751/*
5752** Parse the command line for an ".ar" command. The results are written into
5753** structure (*pAr). SQLITE_OK is returned if the command line is parsed
5754** successfully, otherwise an error message is written to stderr and
5755** SQLITE_ERROR returned.
5756*/
5757static int arParseCommand(
5758 char **azArg, /* Array of arguments passed to dot command */
5759 int nArg, /* Number of entries in azArg[] */
5760 ArCommand *pAr /* Populate this object */
5761){
dand4b56e52017-12-12 20:04:59 +00005762 struct ArSwitch {
dand4b56e52017-12-12 20:04:59 +00005763 const char *zLong;
drhb376b3d2018-01-10 13:11:51 +00005764 char cShort;
5765 u8 eSwitch;
5766 u8 bArg;
dand4b56e52017-12-12 20:04:59 +00005767 } aSwitch[] = {
drhb376b3d2018-01-10 13:11:51 +00005768 { "create", 'c', AR_CMD_CREATE, 0 },
5769 { "extract", 'x', AR_CMD_EXTRACT, 0 },
drhb17ea912019-03-25 14:24:19 +00005770 { "insert", 'i', AR_CMD_INSERT, 0 },
drhb376b3d2018-01-10 13:11:51 +00005771 { "list", 't', AR_CMD_LIST, 0 },
5772 { "update", 'u', AR_CMD_UPDATE, 0 },
5773 { "help", 'h', AR_CMD_HELP, 0 },
5774 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
5775 { "file", 'f', AR_SWITCH_FILE, 1 },
drhca7733b2018-01-10 18:09:20 +00005776 { "append", 'a', AR_SWITCH_APPEND, 1 },
drhb376b3d2018-01-10 13:11:51 +00005777 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
drhb376b3d2018-01-10 13:11:51 +00005778 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
dand4b56e52017-12-12 20:04:59 +00005779 };
5780 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
5781 struct ArSwitch *pEnd = &aSwitch[nSwitch];
5782
dan88be0202017-12-09 17:58:02 +00005783 if( nArg<=1 ){
drh98aa2ab2018-09-26 16:53:51 +00005784 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
dan0d0547f2017-12-14 15:40:42 +00005785 return arUsage(stderr);
dan88be0202017-12-09 17:58:02 +00005786 }else{
5787 char *z = azArg[1];
dan88be0202017-12-09 17:58:02 +00005788 if( z[0]!='-' ){
5789 /* Traditional style [tar] invocation */
5790 int i;
5791 int iArg = 2;
5792 for(i=0; z[i]; i++){
dand4b56e52017-12-12 20:04:59 +00005793 const char *zArg = 0;
5794 struct ArSwitch *pOpt;
5795 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5796 if( z[i]==pOpt->cShort ) break;
dan88be0202017-12-09 17:58:02 +00005797 }
dan0d0547f2017-12-14 15:40:42 +00005798 if( pOpt==pEnd ){
drhd0f9cdc2018-05-17 14:09:06 +00005799 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
dan0d0547f2017-12-14 15:40:42 +00005800 }
dand4b56e52017-12-12 20:04:59 +00005801 if( pOpt->bArg ){
dan0d0547f2017-12-14 15:40:42 +00005802 if( iArg>=nArg ){
drhd0f9cdc2018-05-17 14:09:06 +00005803 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
dan0d0547f2017-12-14 15:40:42 +00005804 }
dand4b56e52017-12-12 20:04:59 +00005805 zArg = azArg[iArg++];
5806 }
5807 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
dan88be0202017-12-09 17:58:02 +00005808 }
dan88be0202017-12-09 17:58:02 +00005809 pAr->nArg = nArg-iArg;
5810 if( pAr->nArg>0 ){
5811 pAr->azArg = &azArg[iArg];
5812 }
dand4b56e52017-12-12 20:04:59 +00005813 }else{
5814 /* Non-traditional invocation */
5815 int iArg;
5816 for(iArg=1; iArg<nArg; iArg++){
5817 int n;
5818 z = azArg[iArg];
5819 if( z[0]!='-' ){
5820 /* All remaining command line words are command arguments. */
5821 pAr->azArg = &azArg[iArg];
5822 pAr->nArg = nArg-iArg;
5823 break;
5824 }
drhaf2770f2018-01-05 14:55:43 +00005825 n = strlen30(z);
dand4b56e52017-12-12 20:04:59 +00005826
5827 if( z[1]!='-' ){
5828 int i;
5829 /* One or more short options */
5830 for(i=1; i<n; i++){
5831 const char *zArg = 0;
5832 struct ArSwitch *pOpt;
5833 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5834 if( z[i]==pOpt->cShort ) break;
5835 }
dan0d0547f2017-12-14 15:40:42 +00005836 if( pOpt==pEnd ){
drhd0f9cdc2018-05-17 14:09:06 +00005837 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
dan0d0547f2017-12-14 15:40:42 +00005838 }
dand4b56e52017-12-12 20:04:59 +00005839 if( pOpt->bArg ){
5840 if( i<(n-1) ){
5841 zArg = &z[i+1];
5842 i = n;
5843 }else{
dan0d0547f2017-12-14 15:40:42 +00005844 if( iArg>=(nArg-1) ){
drhe2754c12019-08-26 12:50:01 +00005845 return arErrorMsg(pAr, "option requires an argument: %c",
5846 z[i]);
dan0d0547f2017-12-14 15:40:42 +00005847 }
dand4b56e52017-12-12 20:04:59 +00005848 zArg = azArg[++iArg];
5849 }
5850 }
5851 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
5852 }
5853 }else if( z[2]=='\0' ){
5854 /* A -- option, indicating that all remaining command line words
5855 ** are command arguments. */
5856 pAr->azArg = &azArg[iArg+1];
5857 pAr->nArg = nArg-iArg-1;
5858 break;
5859 }else{
5860 /* A long option */
5861 const char *zArg = 0; /* Argument for option, if any */
5862 struct ArSwitch *pMatch = 0; /* Matching option */
5863 struct ArSwitch *pOpt; /* Iterator */
5864 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
5865 const char *zLong = pOpt->zLong;
drhaf2770f2018-01-05 14:55:43 +00005866 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
dand4b56e52017-12-12 20:04:59 +00005867 if( pMatch ){
drhd0f9cdc2018-05-17 14:09:06 +00005868 return arErrorMsg(pAr, "ambiguous option: %s",z);
dand4b56e52017-12-12 20:04:59 +00005869 }else{
5870 pMatch = pOpt;
5871 }
5872 }
5873 }
5874
5875 if( pMatch==0 ){
drhd0f9cdc2018-05-17 14:09:06 +00005876 return arErrorMsg(pAr, "unrecognized option: %s", z);
dand4b56e52017-12-12 20:04:59 +00005877 }
5878 if( pMatch->bArg ){
dan0d0547f2017-12-14 15:40:42 +00005879 if( iArg>=(nArg-1) ){
drhd0f9cdc2018-05-17 14:09:06 +00005880 return arErrorMsg(pAr, "option requires an argument: %s", z);
dan0d0547f2017-12-14 15:40:42 +00005881 }
dand4b56e52017-12-12 20:04:59 +00005882 zArg = azArg[++iArg];
5883 }
5884 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
5885 }
5886 }
dan88be0202017-12-09 17:58:02 +00005887 }
5888 }
5889
5890 return SQLITE_OK;
5891}
5892
5893/*
dan3f67ddf2017-12-13 20:04:53 +00005894** This function assumes that all arguments within the ArCommand.azArg[]
5895** array refer to archive members, as for the --extract or --list commands.
5896** It checks that each of them are present. If any specified file is not
5897** present in the archive, an error is printed to stderr and an error
5898** code returned. Otherwise, if all specified arguments are present in
5899** the archive, SQLITE_OK is returned.
5900**
5901** This function strips any trailing '/' characters from each argument.
5902** This is consistent with the way the [tar] command seems to work on
5903** Linux.
5904*/
drhb376b3d2018-01-10 13:11:51 +00005905static int arCheckEntries(ArCommand *pAr){
dan3f67ddf2017-12-13 20:04:53 +00005906 int rc = SQLITE_OK;
5907 if( pAr->nArg ){
drhb376b3d2018-01-10 13:11:51 +00005908 int i, j;
dan3f67ddf2017-12-13 20:04:53 +00005909 sqlite3_stmt *pTest = 0;
5910
drhb376b3d2018-01-10 13:11:51 +00005911 shellPreparePrintf(pAr->db, &rc, &pTest,
5912 "SELECT name FROM %s WHERE name=$name",
5913 pAr->zSrcTable
dan5a78b812017-12-27 18:54:11 +00005914 );
drhb376b3d2018-01-10 13:11:51 +00005915 j = sqlite3_bind_parameter_index(pTest, "$name");
dan3f67ddf2017-12-13 20:04:53 +00005916 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
5917 char *z = pAr->azArg[i];
drhaf2770f2018-01-05 14:55:43 +00005918 int n = strlen30(z);
dan3f67ddf2017-12-13 20:04:53 +00005919 int bOk = 0;
5920 while( n>0 && z[n-1]=='/' ) n--;
5921 z[n] = '\0';
drhb376b3d2018-01-10 13:11:51 +00005922 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
dan3f67ddf2017-12-13 20:04:53 +00005923 if( SQLITE_ROW==sqlite3_step(pTest) ){
5924 bOk = 1;
5925 }
5926 shellReset(&rc, pTest);
5927 if( rc==SQLITE_OK && bOk==0 ){
drhb376b3d2018-01-10 13:11:51 +00005928 utf8_printf(stderr, "not found in archive: %s\n", z);
dan3f67ddf2017-12-13 20:04:53 +00005929 rc = SQLITE_ERROR;
5930 }
5931 }
5932 shellFinalize(&rc, pTest);
5933 }
dan3f67ddf2017-12-13 20:04:53 +00005934 return rc;
5935}
5936
5937/*
5938** Format a WHERE clause that can be used against the "sqlar" table to
5939** identify all archive members that match the command arguments held
5940** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5941** The caller is responsible for eventually calling sqlite3_free() on
5942** any non-NULL (*pzWhere) value.
5943*/
5944static void arWhereClause(
5945 int *pRc,
5946 ArCommand *pAr,
danac15e2d2017-12-14 19:15:07 +00005947 char **pzWhere /* OUT: New WHERE clause */
dan3f67ddf2017-12-13 20:04:53 +00005948){
5949 char *zWhere = 0;
5950 if( *pRc==SQLITE_OK ){
danac15e2d2017-12-14 19:15:07 +00005951 if( pAr->nArg==0 ){
5952 zWhere = sqlite3_mprintf("1");
5953 }else{
5954 int i;
5955 const char *zSep = "";
5956 for(i=0; i<pAr->nArg; i++){
5957 const char *z = pAr->azArg[i];
5958 zWhere = sqlite3_mprintf(
drhb376b3d2018-01-10 13:11:51 +00005959 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5960 zWhere, zSep, z, strlen30(z)+1, z
5961 );
danac15e2d2017-12-14 19:15:07 +00005962 if( zWhere==0 ){
5963 *pRc = SQLITE_NOMEM;
5964 break;
5965 }
5966 zSep = " OR ";
dan3f67ddf2017-12-13 20:04:53 +00005967 }
dan3f67ddf2017-12-13 20:04:53 +00005968 }
5969 }
5970 *pzWhere = zWhere;
5971}
5972
5973/*
dan88be0202017-12-09 17:58:02 +00005974** Implementation of .ar "lisT" command.
5975*/
drhb376b3d2018-01-10 13:11:51 +00005976static int arListCommand(ArCommand *pAr){
danb5090e42017-12-27 21:13:21 +00005977 const char *zSql = "SELECT %s FROM %s WHERE %s";
danb5090e42017-12-27 21:13:21 +00005978 const char *azCols[] = {
5979 "name",
drh410cad92018-01-10 17:19:16 +00005980 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
danb5090e42017-12-27 21:13:21 +00005981 };
dan5a78b812017-12-27 18:54:11 +00005982
dan3f67ddf2017-12-13 20:04:53 +00005983 char *zWhere = 0;
5984 sqlite3_stmt *pSql = 0;
5985 int rc;
5986
drhb376b3d2018-01-10 13:11:51 +00005987 rc = arCheckEntries(pAr);
dan3f67ddf2017-12-13 20:04:53 +00005988 arWhereClause(&rc, pAr, &zWhere);
5989
drhb376b3d2018-01-10 13:11:51 +00005990 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
5991 pAr->zSrcTable, zWhere);
drhb376b3d2018-01-10 13:11:51 +00005992 if( pAr->bDryRun ){
5993 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
5994 }else{
5995 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
5996 if( pAr->bVerbose ){
drh410cad92018-01-10 17:19:16 +00005997 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
5998 sqlite3_column_text(pSql, 0),
drhb376b3d2018-01-10 13:11:51 +00005999 sqlite3_column_int(pSql, 1),
6000 sqlite3_column_text(pSql, 2),
6001 sqlite3_column_text(pSql, 3)
6002 );
6003 }else{
6004 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6005 }
danb5090e42017-12-27 21:13:21 +00006006 }
dan3f67ddf2017-12-13 20:04:53 +00006007 }
dan5a78b812017-12-27 18:54:11 +00006008 shellFinalize(&rc, pSql);
drhd0f9cdc2018-05-17 14:09:06 +00006009 sqlite3_free(zWhere);
dan3f67ddf2017-12-13 20:04:53 +00006010 return rc;
dan88be0202017-12-09 17:58:02 +00006011}
6012
6013
danfd0245d2017-12-07 15:44:29 +00006014/*
6015** Implementation of .ar "eXtract" command.
6016*/
drhb376b3d2018-01-10 13:11:51 +00006017static int arExtractCommand(ArCommand *pAr){
dan25c12182017-12-07 21:03:33 +00006018 const char *zSql1 =
dand1b51d42017-12-16 19:11:26 +00006019 "SELECT "
drhb376b3d2018-01-10 13:11:51 +00006020 " ($dir || name),"
6021 " writefile(($dir || name), %s, mode, mtime) "
drh0cfd46a2018-06-06 01:18:01 +00006022 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
6023 " AND name NOT GLOB '*..[/\\]*'";
dan5a78b812017-12-27 18:54:11 +00006024
6025 const char *azExtraArg[] = {
6026 "sqlar_uncompress(data, sz)",
dan7c15ac12018-01-08 19:59:59 +00006027 "data"
dan5a78b812017-12-27 18:54:11 +00006028 };
dan5a78b812017-12-27 18:54:11 +00006029
danfd0245d2017-12-07 15:44:29 +00006030 sqlite3_stmt *pSql = 0;
6031 int rc = SQLITE_OK;
dan2ad09492017-12-09 18:28:22 +00006032 char *zDir = 0;
dan3f67ddf2017-12-13 20:04:53 +00006033 char *zWhere = 0;
drhb376b3d2018-01-10 13:11:51 +00006034 int i, j;
dan2ad09492017-12-09 18:28:22 +00006035
dan3f67ddf2017-12-13 20:04:53 +00006036 /* If arguments are specified, check that they actually exist within
6037 ** the archive before proceeding. And formulate a WHERE clause to
6038 ** match them. */
drhb376b3d2018-01-10 13:11:51 +00006039 rc = arCheckEntries(pAr);
dan3f67ddf2017-12-13 20:04:53 +00006040 arWhereClause(&rc, pAr, &zWhere);
6041
6042 if( rc==SQLITE_OK ){
6043 if( pAr->zDir ){
6044 zDir = sqlite3_mprintf("%s/", pAr->zDir);
6045 }else{
6046 zDir = sqlite3_mprintf("");
6047 }
6048 if( zDir==0 ) rc = SQLITE_NOMEM;
dan2ad09492017-12-09 18:28:22 +00006049 }
danfd0245d2017-12-07 15:44:29 +00006050
drhb376b3d2018-01-10 13:11:51 +00006051 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
6052 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
dan5a78b812017-12-27 18:54:11 +00006053 );
6054
dan2ad09492017-12-09 18:28:22 +00006055 if( rc==SQLITE_OK ){
drhb376b3d2018-01-10 13:11:51 +00006056 j = sqlite3_bind_parameter_index(pSql, "$dir");
6057 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
dan25c12182017-12-07 21:03:33 +00006058
danac15e2d2017-12-14 19:15:07 +00006059 /* Run the SELECT statement twice. The first time, writefile() is called
6060 ** for all archive members that should be extracted. The second time,
6061 ** only for the directories. This is because the timestamps for
6062 ** extracted directories must be reset after they are populated (as
6063 ** populating them changes the timestamp). */
6064 for(i=0; i<2; i++){
drhb376b3d2018-01-10 13:11:51 +00006065 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
6066 sqlite3_bind_int(pSql, j, i);
6067 if( pAr->bDryRun ){
6068 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
6069 }else{
6070 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
6071 if( i==0 && pAr->bVerbose ){
6072 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
6073 }
danac15e2d2017-12-14 19:15:07 +00006074 }
6075 }
6076 shellReset(&rc, pSql);
dan25c12182017-12-07 21:03:33 +00006077 }
danac15e2d2017-12-14 19:15:07 +00006078 shellFinalize(&rc, pSql);
dan25c12182017-12-07 21:03:33 +00006079 }
dan25c12182017-12-07 21:03:33 +00006080
dan2ad09492017-12-09 18:28:22 +00006081 sqlite3_free(zDir);
dan3f67ddf2017-12-13 20:04:53 +00006082 sqlite3_free(zWhere);
danfd0245d2017-12-07 15:44:29 +00006083 return rc;
6084}
6085
drhb376b3d2018-01-10 13:11:51 +00006086/*
6087** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
6088*/
6089static int arExecSql(ArCommand *pAr, const char *zSql){
6090 int rc;
6091 if( pAr->bDryRun ){
6092 utf8_printf(pAr->p->out, "%s\n", zSql);
6093 rc = SQLITE_OK;
6094 }else{
drh410cad92018-01-10 17:19:16 +00006095 char *zErr = 0;
6096 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
6097 if( zErr ){
6098 utf8_printf(stdout, "ERROR: %s\n", zErr);
6099 sqlite3_free(zErr);
6100 }
drhb376b3d2018-01-10 13:11:51 +00006101 }
6102 return rc;
6103}
6104
dan1ad3f612017-12-11 20:22:02 +00006105
danfd0245d2017-12-07 15:44:29 +00006106/*
drhb17ea912019-03-25 14:24:19 +00006107** Implementation of .ar "create", "insert", and "update" commands.
6108**
6109** create -> Create a new SQL archive
6110** insert -> Insert or reinsert all files listed
6111** update -> Insert files that have changed or that were not
6112** previously in the archive
danfd0245d2017-12-07 15:44:29 +00006113**
6114** Create the "sqlar" table in the database if it does not already exist.
6115** Then add each file in the azFile[] array to the archive. Directories
6116** are added recursively. If argument bVerbose is non-zero, a message is
6117** printed on stdout for each file archived.
dan06741a32017-12-13 20:17:18 +00006118**
6119** The create command is the same as update, except that it drops
drhb17ea912019-03-25 14:24:19 +00006120** any existing "sqlar" table before beginning. The "insert" command
6121** always overwrites every file named on the command-line, where as
6122** "update" only overwrites if the size or mtime or mode has changed.
danfd0245d2017-12-07 15:44:29 +00006123*/
drhb376b3d2018-01-10 13:11:51 +00006124static int arCreateOrUpdateCommand(
dan06741a32017-12-13 20:17:18 +00006125 ArCommand *pAr, /* Command arguments and options */
drhb17ea912019-03-25 14:24:19 +00006126 int bUpdate, /* true for a --create. */
6127 int bOnlyIfChanged /* Only update if file has changed */
danfd0245d2017-12-07 15:44:29 +00006128){
dand4b56e52017-12-12 20:04:59 +00006129 const char *zCreate =
drhafba1802018-01-06 15:49:57 +00006130 "CREATE TABLE IF NOT EXISTS sqlar(\n"
6131 " name TEXT PRIMARY KEY, -- name of the file\n"
6132 " mode INT, -- access permissions\n"
6133 " mtime INT, -- last modification time\n"
6134 " sz INT, -- original file size\n"
6135 " data BLOB -- compressed content\n"
6136 ")";
dand4b56e52017-12-12 20:04:59 +00006137 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
drh1bf208c2018-03-09 21:54:01 +00006138 const char *zInsertFmt[2] = {
6139 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
drh634c70f2018-01-10 16:50:18 +00006140 " SELECT\n"
6141 " %s,\n"
6142 " mode,\n"
6143 " mtime,\n"
drh410cad92018-01-10 17:19:16 +00006144 " CASE substr(lsmode(mode),1,1)\n"
6145 " WHEN '-' THEN length(data)\n"
6146 " WHEN 'd' THEN 0\n"
drh634c70f2018-01-10 16:50:18 +00006147 " ELSE -1 END,\n"
drh69d2d352018-03-09 22:18:53 +00006148 " sqlar_compress(data)\n"
drhb17ea912019-03-25 14:24:19 +00006149 " FROM fsdir(%Q,%Q) AS disk\n"
6150 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
6151 ,
drh1bf208c2018-03-09 21:54:01 +00006152 "REPLACE INTO %s(name,mode,mtime,data)\n"
6153 " SELECT\n"
6154 " %s,\n"
6155 " mode,\n"
6156 " mtime,\n"
6157 " data\n"
drhb17ea912019-03-25 14:24:19 +00006158 " FROM fsdir(%Q,%Q) AS disk\n"
6159 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
drh1bf208c2018-03-09 21:54:01 +00006160 };
danfd0245d2017-12-07 15:44:29 +00006161 int i; /* For iterating through azFile[] */
6162 int rc; /* Return code */
drh1bf208c2018-03-09 21:54:01 +00006163 const char *zTab = 0; /* SQL table into which to insert */
6164 char *zSql;
6165 char zTemp[50];
drhb17ea912019-03-25 14:24:19 +00006166 char *zExists = 0;
danfd0245d2017-12-07 15:44:29 +00006167
drh1bf208c2018-03-09 21:54:01 +00006168 arExecSql(pAr, "PRAGMA page_size=512");
drhb376b3d2018-01-10 13:11:51 +00006169 rc = arExecSql(pAr, "SAVEPOINT ar;");
danfd0245d2017-12-07 15:44:29 +00006170 if( rc!=SQLITE_OK ) return rc;
drh1bf208c2018-03-09 21:54:01 +00006171 zTemp[0] = 0;
6172 if( pAr->bZip ){
6173 /* Initialize the zipfile virtual table, if necessary */
6174 if( pAr->zFile ){
6175 sqlite3_uint64 r;
6176 sqlite3_randomness(sizeof(r),&r);
6177 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
6178 zTab = zTemp;
6179 zSql = sqlite3_mprintf(
6180 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
6181 zTab, pAr->zFile
6182 );
6183 rc = arExecSql(pAr, zSql);
6184 sqlite3_free(zSql);
6185 }else{
6186 zTab = "zip";
6187 }
6188 }else{
6189 /* Initialize the table for an SQLAR */
6190 zTab = "sqlar";
6191 if( bUpdate==0 ){
6192 rc = arExecSql(pAr, zDrop);
6193 if( rc!=SQLITE_OK ) goto end_ar_transaction;
6194 }
6195 rc = arExecSql(pAr, zCreate);
dan06741a32017-12-13 20:17:18 +00006196 }
drhb17ea912019-03-25 14:24:19 +00006197 if( bOnlyIfChanged ){
6198 zExists = sqlite3_mprintf(
6199 " AND NOT EXISTS("
6200 "SELECT 1 FROM %s AS mem"
6201 " WHERE mem.name=disk.name"
6202 " AND mem.mtime=disk.mtime"
6203 " AND mem.mode=disk.mode)", zTab);
6204 }else{
6205 zExists = sqlite3_mprintf("");
6206 }
6207 if( zExists==0 ) rc = SQLITE_NOMEM;
dan88be0202017-12-09 17:58:02 +00006208 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
mistachkince2052b2018-03-23 00:31:53 +00006209 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
drh634c70f2018-01-10 16:50:18 +00006210 pAr->bVerbose ? "shell_putsnl(name)" : "name",
drhb17ea912019-03-25 14:24:19 +00006211 pAr->azArg[i], pAr->zDir, zExists);
mistachkince2052b2018-03-23 00:31:53 +00006212 rc = arExecSql(pAr, zSql2);
6213 sqlite3_free(zSql2);
danfd0245d2017-12-07 15:44:29 +00006214 }
drh1bf208c2018-03-09 21:54:01 +00006215end_ar_transaction:
danfd0245d2017-12-07 15:44:29 +00006216 if( rc!=SQLITE_OK ){
drh2bd207f2019-01-11 17:19:59 +00006217 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
danfd0245d2017-12-07 15:44:29 +00006218 }else{
drhb376b3d2018-01-10 13:11:51 +00006219 rc = arExecSql(pAr, "RELEASE ar;");
drh1bf208c2018-03-09 21:54:01 +00006220 if( pAr->bZip && pAr->zFile ){
6221 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
6222 arExecSql(pAr, zSql);
6223 sqlite3_free(zSql);
6224 }
danfd0245d2017-12-07 15:44:29 +00006225 }
drhb17ea912019-03-25 14:24:19 +00006226 sqlite3_free(zExists);
danfd0245d2017-12-07 15:44:29 +00006227 return rc;
6228}
6229
6230/*
6231** Implementation of ".ar" dot command.
6232*/
6233static int arDotCommand(
drhe2754c12019-08-26 12:50:01 +00006234 ShellState *pState, /* Current shell tool state */
6235 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
6236 char **azArg, /* Array of arguments passed to dot command */
6237 int nArg /* Number of entries in azArg[] */
danfd0245d2017-12-07 15:44:29 +00006238){
dan88be0202017-12-09 17:58:02 +00006239 ArCommand cmd;
6240 int rc;
drh34660642018-01-10 17:39:54 +00006241 memset(&cmd, 0, sizeof(cmd));
drhd0f9cdc2018-05-17 14:09:06 +00006242 cmd.fromCmdLine = fromCmdLine;
dan88be0202017-12-09 17:58:02 +00006243 rc = arParseCommand(azArg, nArg, &cmd);
6244 if( rc==SQLITE_OK ){
drha5676c42018-01-10 15:17:34 +00006245 int eDbType = SHELL_OPEN_UNSPEC;
drhb376b3d2018-01-10 13:11:51 +00006246 cmd.p = pState;
6247 cmd.db = pState->db;
drha5676c42018-01-10 15:17:34 +00006248 if( cmd.zFile ){
drh1bf208c2018-03-09 21:54:01 +00006249 eDbType = deduceDatabaseType(cmd.zFile, 1);
drha5676c42018-01-10 15:17:34 +00006250 }else{
6251 eDbType = pState->openMode;
6252 }
6253 if( eDbType==SHELL_OPEN_ZIPFILE ){
drh1bf208c2018-03-09 21:54:01 +00006254 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
6255 if( cmd.zFile==0 ){
6256 cmd.zSrcTable = sqlite3_mprintf("zip");
6257 }else{
6258 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
6259 }
dan5a78b812017-12-27 18:54:11 +00006260 }
drha5676c42018-01-10 15:17:34 +00006261 cmd.bZip = 1;
dan5a78b812017-12-27 18:54:11 +00006262 }else if( cmd.zFile ){
dand4b56e52017-12-12 20:04:59 +00006263 int flags;
drha5676c42018-01-10 15:17:34 +00006264 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
drhb17ea912019-03-25 14:24:19 +00006265 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
6266 || cmd.eCmd==AR_CMD_UPDATE ){
dand4b56e52017-12-12 20:04:59 +00006267 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
6268 }else{
6269 flags = SQLITE_OPEN_READONLY;
6270 }
drha82c95b2018-01-10 14:00:00 +00006271 cmd.db = 0;
drha5676c42018-01-10 15:17:34 +00006272 if( cmd.bDryRun ){
6273 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
6274 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
6275 }
6276 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
6277 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
dand4b56e52017-12-12 20:04:59 +00006278 if( rc!=SQLITE_OK ){
drhb376b3d2018-01-10 13:11:51 +00006279 utf8_printf(stderr, "cannot open file: %s (%s)\n",
6280 cmd.zFile, sqlite3_errmsg(cmd.db)
dand4b56e52017-12-12 20:04:59 +00006281 );
drha5676c42018-01-10 15:17:34 +00006282 goto end_ar_command;
dand4b56e52017-12-12 20:04:59 +00006283 }
drhb376b3d2018-01-10 13:11:51 +00006284 sqlite3_fileio_init(cmd.db, 0, 0);
drhb376b3d2018-01-10 13:11:51 +00006285 sqlite3_sqlar_init(cmd.db, 0, 0);
drh34660642018-01-10 17:39:54 +00006286 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
6287 shellPutsFunc, 0, 0);
6288
dand4b56e52017-12-12 20:04:59 +00006289 }
drhd0f9cdc2018-05-17 14:09:06 +00006290 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
drh634c70f2018-01-10 16:50:18 +00006291 if( cmd.eCmd!=AR_CMD_CREATE
6292 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
6293 ){
drha5676c42018-01-10 15:17:34 +00006294 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
6295 rc = SQLITE_ERROR;
6296 goto end_ar_command;
6297 }
6298 cmd.zSrcTable = sqlite3_mprintf("sqlar");
6299 }
dand4b56e52017-12-12 20:04:59 +00006300
dan88be0202017-12-09 17:58:02 +00006301 switch( cmd.eCmd ){
6302 case AR_CMD_CREATE:
drhb17ea912019-03-25 14:24:19 +00006303 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
dan88be0202017-12-09 17:58:02 +00006304 break;
danfd0245d2017-12-07 15:44:29 +00006305
dan88be0202017-12-09 17:58:02 +00006306 case AR_CMD_EXTRACT:
drhb376b3d2018-01-10 13:11:51 +00006307 rc = arExtractCommand(&cmd);
dan88be0202017-12-09 17:58:02 +00006308 break;
6309
6310 case AR_CMD_LIST:
drhb376b3d2018-01-10 13:11:51 +00006311 rc = arListCommand(&cmd);
dan88be0202017-12-09 17:58:02 +00006312 break;
6313
dan0d0547f2017-12-14 15:40:42 +00006314 case AR_CMD_HELP:
6315 arUsage(pState->out);
6316 break;
6317
drhb17ea912019-03-25 14:24:19 +00006318 case AR_CMD_INSERT:
6319 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
6320 break;
6321
dan88be0202017-12-09 17:58:02 +00006322 default:
6323 assert( cmd.eCmd==AR_CMD_UPDATE );
drhb17ea912019-03-25 14:24:19 +00006324 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
dan88be0202017-12-09 17:58:02 +00006325 break;
danfd0245d2017-12-07 15:44:29 +00006326 }
6327 }
drha5676c42018-01-10 15:17:34 +00006328end_ar_command:
6329 if( cmd.db!=pState->db ){
drh9e804032018-05-18 17:11:50 +00006330 close_db(cmd.db);
drha5676c42018-01-10 15:17:34 +00006331 }
6332 sqlite3_free(cmd.zSrcTable);
danfd0245d2017-12-07 15:44:29 +00006333
dan88be0202017-12-09 17:58:02 +00006334 return rc;
danfd0245d2017-12-07 15:44:29 +00006335}
drhe37c0e12018-01-06 19:19:50 +00006336/* End of the ".archive" or ".ar" command logic
drhe2754c12019-08-26 12:50:01 +00006337*******************************************************************************/
drhe37c0e12018-01-06 19:19:50 +00006338#endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
danfd0245d2017-12-07 15:44:29 +00006339
dan1b162162019-04-27 20:15:15 +00006340#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
dan42ebb012019-04-27 18:47:03 +00006341/*
6342** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
6343** Otherwise, the SQL statement or statements in zSql are executed using
6344** database connection db and the error code written to *pRc before
6345** this function returns.
6346*/
dan68cb86e2019-04-20 20:57:28 +00006347static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
6348 int rc = *pRc;
6349 if( rc==SQLITE_OK ){
6350 char *zErr = 0;
6351 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
6352 if( rc!=SQLITE_OK ){
6353 raw_printf(stderr, "SQL error: %s\n", zErr);
6354 }
6355 *pRc = rc;
6356 }
6357}
6358
dan42ebb012019-04-27 18:47:03 +00006359/*
6360** Like shellExec(), except that zFmt is a printf() style format string.
6361*/
danc0b42432019-04-26 15:14:53 +00006362static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
6363 char *z = 0;
6364 if( *pRc==SQLITE_OK ){
6365 va_list ap;
6366 va_start(ap, zFmt);
6367 z = sqlite3_vmprintf(zFmt, ap);
6368 va_end(ap);
6369 if( z==0 ){
6370 *pRc = SQLITE_NOMEM;
6371 }else{
6372 shellExec(db, pRc, z);
6373 }
6374 sqlite3_free(z);
6375 }
6376}
6377
dan42ebb012019-04-27 18:47:03 +00006378/*
6379** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6380** Otherwise, an attempt is made to allocate, zero and return a pointer
6381** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
6382** to SQLITE_NOMEM and NULL returned.
6383*/
dan68cb86e2019-04-20 20:57:28 +00006384static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
6385 void *pRet = 0;
6386 if( *pRc==SQLITE_OK ){
6387 pRet = sqlite3_malloc64(nByte);
6388 if( pRet==0 ){
6389 *pRc = SQLITE_NOMEM;
6390 }else{
6391 memset(pRet, 0, nByte);
6392 }
6393 }
6394 return pRet;
6395}
6396
dan42ebb012019-04-27 18:47:03 +00006397/*
6398** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
6399** Otherwise, zFmt is treated as a printf() style string. The result of
6400** formatting it along with any trailing arguments is written into a
6401** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
6402** It is the responsibility of the caller to eventually free this buffer
6403** using a call to sqlite3_free().
6404**
6405** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
6406** pointer returned.
6407*/
dan68cb86e2019-04-20 20:57:28 +00006408static char *shellMPrintf(int *pRc, const char *zFmt, ...){
6409 char *z = 0;
6410 if( *pRc==SQLITE_OK ){
6411 va_list ap;
6412 va_start(ap, zFmt);
6413 z = sqlite3_vmprintf(zFmt, ap);
6414 va_end(ap);
6415 if( z==0 ){
6416 *pRc = SQLITE_NOMEM;
6417 }
6418 }
6419 return z;
6420}
6421
dan42ebb012019-04-27 18:47:03 +00006422/*
6423** When running the ".recover" command, each output table, and the special
6424** orphaned row table if it is required, is represented by an instance
6425** of the following struct.
6426*/
dan68cb86e2019-04-20 20:57:28 +00006427typedef struct RecoverTable RecoverTable;
6428struct RecoverTable {
dan42ebb012019-04-27 18:47:03 +00006429 char *zQuoted; /* Quoted version of table name */
dan68cb86e2019-04-20 20:57:28 +00006430 int nCol; /* Number of columns in table */
6431 char **azlCol; /* Array of column lists */
dan42ebb012019-04-27 18:47:03 +00006432 int iPk; /* Index of IPK column */
dan68cb86e2019-04-20 20:57:28 +00006433};
6434
6435/*
dan42ebb012019-04-27 18:47:03 +00006436** Free a RecoverTable object allocated by recoverFindTable() or
6437** recoverOrphanTable().
dan68cb86e2019-04-20 20:57:28 +00006438*/
6439static void recoverFreeTable(RecoverTable *pTab){
6440 if( pTab ){
dan68cb86e2019-04-20 20:57:28 +00006441 sqlite3_free(pTab->zQuoted);
dan68cb86e2019-04-20 20:57:28 +00006442 if( pTab->azlCol ){
6443 int i;
dan98c5ad32019-04-26 21:11:37 +00006444 for(i=0; i<=pTab->nCol; i++){
dan68cb86e2019-04-20 20:57:28 +00006445 sqlite3_free(pTab->azlCol[i]);
6446 }
6447 sqlite3_free(pTab->azlCol);
6448 }
6449 sqlite3_free(pTab);
6450 }
6451}
6452
dan42ebb012019-04-27 18:47:03 +00006453/*
6454** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
6455** Otherwise, it allocates and returns a RecoverTable object based on the
6456** final four arguments passed to this function. It is the responsibility
6457** of the caller to eventually free the returned object using
6458** recoverFreeTable().
6459*/
6460static RecoverTable *recoverNewTable(
danb40af492019-04-22 20:52:12 +00006461 int *pRc, /* IN/OUT: Error code */
danb40af492019-04-22 20:52:12 +00006462 const char *zName, /* Name of table */
6463 const char *zSql, /* CREATE TABLE statement */
6464 int bIntkey,
6465 int nCol
6466){
6467 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
6468 int rc = *pRc;
dan98c5ad32019-04-26 21:11:37 +00006469 RecoverTable *pTab = 0;
danb40af492019-04-22 20:52:12 +00006470
dan98c5ad32019-04-26 21:11:37 +00006471 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
danb40af492019-04-22 20:52:12 +00006472 if( rc==SQLITE_OK ){
6473 int nSqlCol = 0;
6474 int bSqlIntkey = 0;
6475 sqlite3_stmt *pStmt = 0;
dan98c5ad32019-04-26 21:11:37 +00006476
danb40af492019-04-22 20:52:12 +00006477 rc = sqlite3_open("", &dbtmp);
6478 if( rc==SQLITE_OK ){
drha2de66c2019-08-06 20:26:17 +00006479 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
6480 shellIdQuote, 0, 0);
6481 }
6482 if( rc==SQLITE_OK ){
dan38f9c712019-04-23 18:03:02 +00006483 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
6484 }
6485 if( rc==SQLITE_OK ){
danb40af492019-04-22 20:52:12 +00006486 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
6487 if( rc==SQLITE_ERROR ){
6488 rc = SQLITE_OK;
6489 goto finished;
6490 }
6491 }
6492 shellPreparePrintf(dbtmp, &rc, &pStmt,
6493 "SELECT count(*) FROM pragma_table_info(%Q)", zName
6494 );
6495 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6496 nSqlCol = sqlite3_column_int(pStmt, 0);
6497 }
6498 shellFinalize(&rc, pStmt);
6499
6500 if( rc!=SQLITE_OK || nSqlCol<nCol ){
6501 goto finished;
6502 }
6503
6504 shellPreparePrintf(dbtmp, &rc, &pStmt,
6505 "SELECT ("
6506 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
6507 ") FROM sqlite_master WHERE name = %Q", zName
6508 );
6509 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6510 bSqlIntkey = sqlite3_column_int(pStmt, 0);
6511 }
6512 shellFinalize(&rc, pStmt);
6513
6514 if( bIntkey==bSqlIntkey ){
dan98c5ad32019-04-26 21:11:37 +00006515 int i;
danb40af492019-04-22 20:52:12 +00006516 const char *zPk = "_rowid_";
6517 sqlite3_stmt *pPkFinder = 0;
6518
danf57bea32019-04-27 15:35:45 +00006519 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
6520 ** set zPk to the name of the PK column, and pTab->iPk to the index
6521 ** of the column, where columns are 0-numbered from left to right.
6522 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
6523 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
dan98c5ad32019-04-26 21:11:37 +00006524 pTab->iPk = -2;
6525 if( bIntkey ){
6526 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
danb40af492019-04-22 20:52:12 +00006527 "SELECT cid, name FROM pragma_table_info(%Q) "
6528 " WHERE pk=1 AND type='integer' COLLATE nocase"
dan98c5ad32019-04-26 21:11:37 +00006529 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
6530 , zName, zName
6531 );
6532 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
6533 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
6534 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
6535 }
danb40af492019-04-22 20:52:12 +00006536 }
6537
drha2de66c2019-08-06 20:26:17 +00006538 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
dan98c5ad32019-04-26 21:11:37 +00006539 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
danb40af492019-04-22 20:52:12 +00006540 pTab->nCol = nSqlCol;
6541
dan98c5ad32019-04-26 21:11:37 +00006542 if( bIntkey ){
drha2de66c2019-08-06 20:26:17 +00006543 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
danb40af492019-04-22 20:52:12 +00006544 }else{
dan98c5ad32019-04-26 21:11:37 +00006545 pTab->azlCol[0] = shellMPrintf(&rc, "");
danb40af492019-04-22 20:52:12 +00006546 }
dan98c5ad32019-04-26 21:11:37 +00006547 i = 1;
6548 shellPreparePrintf(dbtmp, &rc, &pStmt,
drha2de66c2019-08-06 20:26:17 +00006549 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
danf57bea32019-04-27 15:35:45 +00006550 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
dan98c5ad32019-04-26 21:11:37 +00006551 "FROM pragma_table_info(%Q)",
danf57bea32019-04-27 15:35:45 +00006552 bIntkey ? ", " : "", pTab->iPk,
6553 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
6554 zName
dan98c5ad32019-04-26 21:11:37 +00006555 );
6556 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6557 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
6558 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
6559 i++;
6560 }
6561 shellFinalize(&rc, pStmt);
6562
danb40af492019-04-22 20:52:12 +00006563 shellFinalize(&rc, pPkFinder);
6564 }
6565 }
6566
6567 finished:
6568 sqlite3_close(dbtmp);
6569 *pRc = rc;
dan98779652019-05-09 14:15:19 +00006570 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
dan98c5ad32019-04-26 21:11:37 +00006571 recoverFreeTable(pTab);
6572 pTab = 0;
6573 }
6574 return pTab;
danb40af492019-04-22 20:52:12 +00006575}
6576
dan0aa01ee2019-04-27 19:36:49 +00006577/*
6578** This function is called to search the schema recovered from the
6579** sqlite_master table of the (possibly) corrupt database as part
6580** of a ".recover" command. Specifically, for a table with root page
6581** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
6582** table must be a WITHOUT ROWID table, or if non-zero, not one of
6583** those.
6584**
6585** If a table is found, a (RecoverTable*) object is returned. Or, if
6586** no such table is found, but bIntkey is false and iRoot is the
6587** root page of an index in the recovered schema, then (*pbNoop) is
6588** set to true and NULL returned. Or, if there is no such table or
6589** index, NULL is returned and (*pbNoop) set to 0, indicating that
6590** the caller should write data to the orphans table.
6591*/
dan42ebb012019-04-27 18:47:03 +00006592static RecoverTable *recoverFindTable(
dan0aa01ee2019-04-27 19:36:49 +00006593 ShellState *pState, /* Shell state object */
6594 int *pRc, /* IN/OUT: Error code */
6595 int iRoot, /* Root page of table */
6596 int bIntkey, /* True for an intkey table */
6597 int nCol, /* Number of columns in table */
6598 int *pbNoop /* OUT: True if iRoot is root of index */
dan68cb86e2019-04-20 20:57:28 +00006599){
danb40af492019-04-22 20:52:12 +00006600 sqlite3_stmt *pStmt = 0;
dan68cb86e2019-04-20 20:57:28 +00006601 RecoverTable *pRet = 0;
danb40af492019-04-22 20:52:12 +00006602 int bNoop = 0;
6603 const char *zSql = 0;
6604 const char *zName = 0;
dan68cb86e2019-04-20 20:57:28 +00006605
danb40af492019-04-22 20:52:12 +00006606 /* Search the recovered schema for an object with root page iRoot. */
6607 shellPreparePrintf(pState->db, pRc, &pStmt,
6608 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
6609 );
6610 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6611 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
6612 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
6613 bNoop = 1;
6614 break;
6615 }
6616 if( sqlite3_stricmp(zType, "table")==0 ){
6617 zName = (const char*)sqlite3_column_text(pStmt, 1);
6618 zSql = (const char*)sqlite3_column_text(pStmt, 2);
dan42ebb012019-04-27 18:47:03 +00006619 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
danb40af492019-04-22 20:52:12 +00006620 break;
6621 }
6622 }
dan98c5ad32019-04-26 21:11:37 +00006623
danb40af492019-04-22 20:52:12 +00006624 shellFinalize(pRc, pStmt);
dan98c5ad32019-04-26 21:11:37 +00006625 *pbNoop = bNoop;
6626 return pRet;
6627}
danb40af492019-04-22 20:52:12 +00006628
dan0aa01ee2019-04-27 19:36:49 +00006629/*
6630** Return a RecoverTable object representing the orphans table.
6631*/
dan98c5ad32019-04-26 21:11:37 +00006632static RecoverTable *recoverOrphanTable(
dan0aa01ee2019-04-27 19:36:49 +00006633 ShellState *pState, /* Shell state object */
6634 int *pRc, /* IN/OUT: Error code */
6635 const char *zLostAndFound, /* Base name for orphans table */
6636 int nCol /* Number of user data columns */
dan98c5ad32019-04-26 21:11:37 +00006637){
6638 RecoverTable *pTab = 0;
6639 if( nCol>=0 && *pRc==SQLITE_OK ){
6640 int i;
dan42ebb012019-04-27 18:47:03 +00006641
6642 /* This block determines the name of the orphan table. The prefered
6643 ** name is zLostAndFound. But if that clashes with another name
6644 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
6645 ** and so on until a non-clashing name is found. */
6646 int iTab = 0;
6647 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
6648 sqlite3_stmt *pTest = 0;
6649 shellPrepare(pState->db, pRc,
6650 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
dan68cb86e2019-04-20 20:57:28 +00006651 );
dan42ebb012019-04-27 18:47:03 +00006652 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
6653 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
6654 shellReset(pRc, pTest);
6655 sqlite3_free(zTab);
6656 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
6657 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
dan68cb86e2019-04-20 20:57:28 +00006658 }
dan42ebb012019-04-27 18:47:03 +00006659 shellFinalize(pRc, pTest);
dan68cb86e2019-04-20 20:57:28 +00006660
dan98c5ad32019-04-26 21:11:37 +00006661 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
6662 if( pTab ){
drha2de66c2019-08-06 20:26:17 +00006663 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
dan98c5ad32019-04-26 21:11:37 +00006664 pTab->nCol = nCol;
6665 pTab->iPk = -2;
6666 if( nCol>0 ){
6667 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
6668 if( pTab->azlCol ){
6669 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
6670 for(i=nCol-1; i>=0; i--){
6671 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
6672 }
6673 }
danb40af492019-04-22 20:52:12 +00006674 }
dan68cb86e2019-04-20 20:57:28 +00006675
dan42ebb012019-04-27 18:47:03 +00006676 if( *pRc!=SQLITE_OK ){
6677 recoverFreeTable(pTab);
6678 pTab = 0;
6679 }else{
6680 raw_printf(pState->out,
6681 "CREATE TABLE %s(rootpgno INTEGER, "
6682 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
6683 );
6684 for(i=0; i<nCol; i++){
6685 raw_printf(pState->out, ", c%d", i);
6686 }
6687 raw_printf(pState->out, ");\n");
6688 }
dan98c5ad32019-04-26 21:11:37 +00006689 }
dan42ebb012019-04-27 18:47:03 +00006690 sqlite3_free(zTab);
dan68cb86e2019-04-20 20:57:28 +00006691 }
dan98c5ad32019-04-26 21:11:37 +00006692 return pTab;
dan68cb86e2019-04-20 20:57:28 +00006693}
6694
6695/*
6696** This function is called to recover data from the database. A script
6697** to construct a new database containing all recovered data is output
6698** on stream pState->out.
6699*/
danb9b71db2019-04-25 16:20:40 +00006700static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
dan68cb86e2019-04-20 20:57:28 +00006701 int rc = SQLITE_OK;
6702 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
danefa363b2019-04-24 20:48:55 +00006703 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
6704 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
danc0b42432019-04-26 15:14:53 +00006705 const char *zRecoveryDb = ""; /* Name of "recovery" database */
dan42ebb012019-04-27 18:47:03 +00006706 const char *zLostAndFound = "lost_and_found";
dan9c014f82019-04-25 19:23:15 +00006707 int i;
dan98c5ad32019-04-26 21:11:37 +00006708 int nOrphan = -1;
6709 RecoverTable *pOrphan = 0;
dan9c014f82019-04-25 19:23:15 +00006710
6711 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
dan8cce6b82019-09-14 16:44:51 +00006712 int bRowids = 1; /* 0 if --no-rowids */
dan9c014f82019-04-25 19:23:15 +00006713 for(i=1; i<nArg; i++){
6714 char *z = azArg[i];
6715 int n;
6716 if( z[0]=='-' && z[1]=='-' ) z++;
drh4245e042019-06-13 13:52:46 +00006717 n = strlen30(z);
dan9c014f82019-04-25 19:23:15 +00006718 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
6719 bFreelist = 0;
dan42ebb012019-04-27 18:47:03 +00006720 }else
danc0b42432019-04-26 15:14:53 +00006721 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
6722 i++;
6723 zRecoveryDb = azArg[i];
dan42ebb012019-04-27 18:47:03 +00006724 }else
6725 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
6726 i++;
6727 zLostAndFound = azArg[i];
dan8cce6b82019-09-14 16:44:51 +00006728 }else
6729 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
6730 bRowids = 0;
danc0b42432019-04-26 15:14:53 +00006731 }
dan9c014f82019-04-25 19:23:15 +00006732 else{
drhe2754c12019-08-26 12:50:01 +00006733 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
6734 showHelp(pState->out, azArg[0]);
dan9c014f82019-04-25 19:23:15 +00006735 return 1;
6736 }
6737 }
dan68cb86e2019-04-20 20:57:28 +00006738
danc0b42432019-04-26 15:14:53 +00006739 shellExecPrintf(pState->db, &rc,
dan68cb86e2019-04-20 20:57:28 +00006740 /* Attach an in-memory database named 'recovery'. Create an indexed
6741 ** cache of the sqlite_dbptr virtual table. */
dan01c08bc2019-07-24 19:20:30 +00006742 "PRAGMA writable_schema = on;"
danc0b42432019-04-26 15:14:53 +00006743 "ATTACH %Q AS recovery;"
6744 "DROP TABLE IF EXISTS recovery.dbptr;"
6745 "DROP TABLE IF EXISTS recovery.freelist;"
6746 "DROP TABLE IF EXISTS recovery.map;"
6747 "DROP TABLE IF EXISTS recovery.schema;"
danc0b42432019-04-26 15:14:53 +00006748 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
dan9c014f82019-04-25 19:23:15 +00006749 );
6750
6751 if( bFreelist ){
6752 shellExec(pState->db, &rc,
6753 "WITH trunk(pgno) AS ("
6754 " SELECT shell_int32("
6755 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
6756 " WHERE x>0"
6757 " UNION"
6758 " SELECT shell_int32("
6759 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
6760 " FROM trunk WHERE x>0"
6761 "),"
6762 "freelist(data, n, freepgno) AS ("
danf6099e92019-05-09 16:57:39 +00006763 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
dan9c014f82019-04-25 19:23:15 +00006764 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
6765 " UNION ALL"
6766 " SELECT data, n-1, shell_int32(data, 2+n) "
6767 " FROM freelist WHERE n>=0"
6768 ")"
6769 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
6770 );
6771 }
6772
dan95063c22019-07-24 08:15:09 +00006773 /* If this is an auto-vacuum database, add all pointer-map pages to
6774 ** the freelist table. Do this regardless of whether or not
6775 ** --freelist-corrupt was specified. */
6776 shellExec(pState->db, &rc,
6777 "WITH ptrmap(pgno) AS ("
6778 " SELECT 2 WHERE shell_int32("
6779 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
6780 " )"
6781 " UNION ALL "
6782 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
6783 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
6784 ")"
6785 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
6786 );
6787
dan9c014f82019-04-25 19:23:15 +00006788 shellExec(pState->db, &rc,
danca424382019-04-26 15:40:27 +00006789 "CREATE TABLE recovery.dbptr("
6790 " pgno, child, PRIMARY KEY(child, pgno)"
6791 ") WITHOUT ROWID;"
6792 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
6793 " SELECT * FROM sqlite_dbptr"
6794 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
6795
6796 /* Delete any pointer to page 1. This ensures that page 1 is considered
6797 ** a root page, regardless of how corrupt the db is. */
6798 "DELETE FROM recovery.dbptr WHERE child = 1;"
6799
6800 /* Delete all pointers to any pages that have more than one pointer
6801 ** to them. Such pages will be treated as root pages when recovering
6802 ** data. */
6803 "DELETE FROM recovery.dbptr WHERE child IN ("
6804 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
6805 ");"
6806
dan68cb86e2019-04-20 20:57:28 +00006807 /* Create the "map" table that will (eventually) contain instructions
6808 ** for dealing with each page in the db that contains one or more
6809 ** records. */
danb40af492019-04-22 20:52:12 +00006810 "CREATE TABLE recovery.map("
6811 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
6812 ");"
dan68cb86e2019-04-20 20:57:28 +00006813
6814 /* Populate table [map]. If there are circular loops of pages in the
6815 ** database, the following adds all pages in such a loop to the map
6816 ** as individual root pages. This could be handled better. */
6817 "WITH pages(i, maxlen) AS ("
danb9b71db2019-04-25 16:20:40 +00006818 " SELECT page_count, ("
6819 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
dan13b87672019-05-09 11:45:21 +00006820 " ) FROM pragma_page_count WHERE page_count>0"
dan68cb86e2019-04-20 20:57:28 +00006821 " UNION ALL"
danb40af492019-04-22 20:52:12 +00006822 " SELECT i-1, ("
6823 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
6824 " ) FROM pages WHERE i>=2"
dan68cb86e2019-04-20 20:57:28 +00006825 ")"
danb40af492019-04-22 20:52:12 +00006826 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
6827 " SELECT i, maxlen, NULL, ("
dan68cb86e2019-04-20 20:57:28 +00006828 " WITH p(orig, pgno, parent) AS ("
6829 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
dan39e04f82019-05-09 18:33:32 +00006830 " UNION "
dan68cb86e2019-04-20 20:57:28 +00006831 " SELECT i, p.parent, "
6832 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
6833 " )"
6834 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
6835 ") "
dand790c9a2019-08-26 14:57:58 +00006836 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
danb40af492019-04-22 20:52:12 +00006837 "UPDATE recovery.map AS o SET intkey = ("
6838 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
6839 ");"
dan68cb86e2019-04-20 20:57:28 +00006840
6841 /* Extract data from page 1 and any linked pages into table
6842 ** recovery.schema. With the same schema as an sqlite_master table. */
6843 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
6844 "INSERT INTO recovery.schema SELECT "
6845 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
6846 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
6847 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
6848 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
6849 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
6850 "FROM sqlite_dbdata WHERE pgno IN ("
6851 " SELECT pgno FROM recovery.map WHERE root=1"
6852 ")"
6853 "GROUP BY pgno, cell;"
dan98c5ad32019-04-26 21:11:37 +00006854 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
dan68cb86e2019-04-20 20:57:28 +00006855 );
6856
danb40af492019-04-22 20:52:12 +00006857 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
6858 ** CREATE TABLE statements that extracted from the existing schema. */
6859 if( rc==SQLITE_OK ){
6860 sqlite3_stmt *pStmt = 0;
danf3210572019-08-06 18:40:36 +00006861 /* ".recover" might output content in an order which causes immediate
6862 ** foreign key constraints to be violated. So disable foreign-key
6863 ** constraint enforcement to prevent problems when running the output
6864 ** script. */
6865 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
danb40af492019-04-22 20:52:12 +00006866 raw_printf(pState->out, "BEGIN;\n");
dan38f9c712019-04-23 18:03:02 +00006867 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
6868 shellPrepare(pState->db, &rc,
danb40af492019-04-22 20:52:12 +00006869 "SELECT sql FROM recovery.schema "
dan38f9c712019-04-23 18:03:02 +00006870 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
danb40af492019-04-22 20:52:12 +00006871 );
6872 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6873 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
dan38f9c712019-04-23 18:03:02 +00006874 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
6875 &zCreateTable[12]
6876 );
danb40af492019-04-22 20:52:12 +00006877 }
6878 shellFinalize(&rc, pStmt);
6879 }
6880
dan98c5ad32019-04-26 21:11:37 +00006881 /* Figure out if an orphan table will be required. And if so, how many
6882 ** user columns it should contain */
6883 shellPrepare(pState->db, &rc,
dan98779652019-05-09 14:15:19 +00006884 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
dan98c5ad32019-04-26 21:11:37 +00006885 , &pLoop
6886 );
6887 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6888 nOrphan = sqlite3_column_int(pLoop, 0);
6889 }
6890 shellFinalize(&rc, pLoop);
6891 pLoop = 0;
dan98c5ad32019-04-26 21:11:37 +00006892
danefa363b2019-04-24 20:48:55 +00006893 shellPrepare(pState->db, &rc,
6894 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
6895 );
dan8cce6b82019-09-14 16:44:51 +00006896
danefa363b2019-04-24 20:48:55 +00006897 shellPrepare(pState->db, &rc,
dan8cce6b82019-09-14 16:44:51 +00006898 "SELECT max(field), group_concat(shell_escape_crnl(quote"
6899 "(case when (? AND field<0) then NULL else value end)"
6900 "), ', ')"
dan9443dbc2019-07-24 20:10:27 +00006901 ", min(field) "
danefa363b2019-04-24 20:48:55 +00006902 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
6903 "GROUP BY cell", &pCells
6904 );
6905
dan68cb86e2019-04-20 20:57:28 +00006906 /* Loop through each root page. */
danb40af492019-04-22 20:52:12 +00006907 shellPrepare(pState->db, &rc,
6908 "SELECT root, intkey, max(maxlen) FROM recovery.map"
dan38f9c712019-04-23 18:03:02 +00006909 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
6910 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
6911 ")", &pLoop
danb40af492019-04-22 20:52:12 +00006912 );
dan68cb86e2019-04-20 20:57:28 +00006913 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
6914 int iRoot = sqlite3_column_int(pLoop, 0);
danb40af492019-04-22 20:52:12 +00006915 int bIntkey = sqlite3_column_int(pLoop, 1);
6916 int nCol = sqlite3_column_int(pLoop, 2);
dan98c5ad32019-04-26 21:11:37 +00006917 int bNoop = 0;
dan68cb86e2019-04-20 20:57:28 +00006918 RecoverTable *pTab;
6919
dan9443dbc2019-07-24 20:10:27 +00006920 assert( bIntkey==0 || bIntkey==1 );
dan42ebb012019-04-27 18:47:03 +00006921 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
dan98c5ad32019-04-26 21:11:37 +00006922 if( bNoop || rc ) continue;
dan98779652019-05-09 14:15:19 +00006923 if( pTab==0 ){
6924 if( pOrphan==0 ){
6925 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6926 }
6927 pTab = pOrphan;
6928 if( pTab==0 ) break;
6929 }
dan98c5ad32019-04-26 21:11:37 +00006930
drha2de66c2019-08-06 20:26:17 +00006931 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
dan98c5ad32019-04-26 21:11:37 +00006932 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
6933 }
6934 sqlite3_bind_int(pPages, 1, iRoot);
dan8cce6b82019-09-14 16:44:51 +00006935 if( bRowids==0 && pTab->iPk<0 ){
6936 sqlite3_bind_int(pCells, 1, 1);
6937 }else{
6938 sqlite3_bind_int(pCells, 1, 0);
6939 }
6940 sqlite3_bind_int(pCells, 3, pTab->iPk);
dan98c5ad32019-04-26 21:11:37 +00006941
6942 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
6943 int iPgno = sqlite3_column_int(pPages, 0);
dan8cce6b82019-09-14 16:44:51 +00006944 sqlite3_bind_int(pCells, 2, iPgno);
dan98c5ad32019-04-26 21:11:37 +00006945 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
6946 int nField = sqlite3_column_int(pCells, 0);
dan9443dbc2019-07-24 20:10:27 +00006947 int iMin = sqlite3_column_int(pCells, 2);
dan98c5ad32019-04-26 21:11:37 +00006948 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
6949
dan9443dbc2019-07-24 20:10:27 +00006950 RecoverTable *pTab2 = pTab;
6951 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
6952 if( pOrphan==0 ){
6953 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
6954 }
6955 pTab2 = pOrphan;
6956 if( pTab2==0 ) break;
6957 }
6958
dan98c5ad32019-04-26 21:11:37 +00006959 nField = nField+1;
dan9443dbc2019-07-24 20:10:27 +00006960 if( pTab2==pOrphan ){
dan98c5ad32019-04-26 21:11:37 +00006961 raw_printf(pState->out,
6962 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
dan9443dbc2019-07-24 20:10:27 +00006963 pTab2->zQuoted, iRoot, iPgno, nField,
6964 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
dan98c5ad32019-04-26 21:11:37 +00006965 );
6966 }else{
danefa363b2019-04-24 20:48:55 +00006967 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
dan9443dbc2019-07-24 20:10:27 +00006968 pTab2->zQuoted, pTab2->azlCol[nField], zVal
danefa363b2019-04-24 20:48:55 +00006969 );
6970 }
dan68cb86e2019-04-20 20:57:28 +00006971 }
dan98c5ad32019-04-26 21:11:37 +00006972 shellReset(&rc, pCells);
dan68cb86e2019-04-20 20:57:28 +00006973 }
dan98c5ad32019-04-26 21:11:37 +00006974 shellReset(&rc, pPages);
6975 if( pTab!=pOrphan ) recoverFreeTable(pTab);
dan68cb86e2019-04-20 20:57:28 +00006976 }
6977 shellFinalize(&rc, pLoop);
danefa363b2019-04-24 20:48:55 +00006978 shellFinalize(&rc, pPages);
6979 shellFinalize(&rc, pCells);
dan98c5ad32019-04-26 21:11:37 +00006980 recoverFreeTable(pOrphan);
dan68cb86e2019-04-20 20:57:28 +00006981
dan38f9c712019-04-23 18:03:02 +00006982 /* The rest of the schema */
danb40af492019-04-22 20:52:12 +00006983 if( rc==SQLITE_OK ){
dan38f9c712019-04-23 18:03:02 +00006984 sqlite3_stmt *pStmt = 0;
6985 shellPrepare(pState->db, &rc,
6986 "SELECT sql, name FROM recovery.schema "
danb1825882019-04-23 20:48:32 +00006987 "WHERE sql NOT LIKE 'create table%'", &pStmt
dan38f9c712019-04-23 18:03:02 +00006988 );
6989 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
6990 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
6991 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
6992 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
6993 char *zPrint = shellMPrintf(&rc,
6994 "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
6995 zName, zName, zSql
6996 );
6997 raw_printf(pState->out, "%s;\n", zPrint);
6998 sqlite3_free(zPrint);
6999 }else{
7000 raw_printf(pState->out, "%s;\n", zSql);
7001 }
7002 }
7003 shellFinalize(&rc, pStmt);
7004 }
7005
7006 if( rc==SQLITE_OK ){
7007 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
danb40af492019-04-22 20:52:12 +00007008 raw_printf(pState->out, "COMMIT;\n");
7009 }
dan68cb86e2019-04-20 20:57:28 +00007010 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
7011 return rc;
7012}
dan1b162162019-04-27 20:15:15 +00007013#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
dan68cb86e2019-04-20 20:57:28 +00007014
drh2ce15c32017-07-11 13:34:40 +00007015
7016/*
7017** If an input line begins with "." then invoke this routine to
7018** process that line.
7019**
7020** Return 1 on error, 2 to exit, and 0 otherwise.
7021*/
7022static int do_meta_command(char *zLine, ShellState *p){
7023 int h = 1;
7024 int nArg = 0;
7025 int n, c;
7026 int rc = 0;
drh5df84282019-08-17 19:45:25 +00007027 char *azArg[52];
drh2ce15c32017-07-11 13:34:40 +00007028
dan6b046be2018-01-09 15:25:55 +00007029#ifndef SQLITE_OMIT_VIRTUALTABLE
dan43efc182017-12-19 17:42:13 +00007030 if( p->expert.pExpert ){
7031 expertFinish(p, 1, 0);
7032 }
dan6b046be2018-01-09 15:25:55 +00007033#endif
dan43efc182017-12-19 17:42:13 +00007034
drh2ce15c32017-07-11 13:34:40 +00007035 /* Parse the input line into tokens.
7036 */
drh5df84282019-08-17 19:45:25 +00007037 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
drh2ce15c32017-07-11 13:34:40 +00007038 while( IsSpace(zLine[h]) ){ h++; }
7039 if( zLine[h]==0 ) break;
7040 if( zLine[h]=='\'' || zLine[h]=='"' ){
7041 int delim = zLine[h++];
7042 azArg[nArg++] = &zLine[h];
7043 while( zLine[h] && zLine[h]!=delim ){
7044 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
7045 h++;
7046 }
7047 if( zLine[h]==delim ){
7048 zLine[h++] = 0;
7049 }
7050 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
7051 }else{
7052 azArg[nArg++] = &zLine[h];
7053 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
7054 if( zLine[h] ) zLine[h++] = 0;
7055 resolve_backslashes(azArg[nArg-1]);
7056 }
7057 }
drh5df84282019-08-17 19:45:25 +00007058 azArg[nArg] = 0;
drh2ce15c32017-07-11 13:34:40 +00007059
7060 /* Process the input line.
7061 */
7062 if( nArg==0 ) return 0; /* no tokens, no error */
7063 n = strlen30(azArg[0]);
7064 c = azArg[0][0];
drh13c20932018-01-10 21:41:55 +00007065 clearTempFile(p);
drh2ce15c32017-07-11 13:34:40 +00007066
7067#ifndef SQLITE_OMIT_AUTHORIZATION
7068 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
7069 if( nArg!=2 ){
7070 raw_printf(stderr, "Usage: .auth ON|OFF\n");
7071 rc = 1;
7072 goto meta_command_exit;
7073 }
7074 open_db(p, 0);
7075 if( booleanValue(azArg[1]) ){
7076 sqlite3_set_authorizer(p->db, shellAuth, p);
7077 }else{
7078 sqlite3_set_authorizer(p->db, 0, 0);
7079 }
7080 }else
7081#endif
7082
drhe37c0e12018-01-06 19:19:50 +00007083#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
7084 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
danfd0245d2017-12-07 15:44:29 +00007085 open_db(p, 0);
drhd0f9cdc2018-05-17 14:09:06 +00007086 rc = arDotCommand(p, 0, azArg, nArg);
danfd0245d2017-12-07 15:44:29 +00007087 }else
7088#endif
7089
drh2ce15c32017-07-11 13:34:40 +00007090 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
7091 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
7092 ){
7093 const char *zDestFile = 0;
7094 const char *zDb = 0;
7095 sqlite3 *pDest;
7096 sqlite3_backup *pBackup;
7097 int j;
drha50bffb2018-12-08 01:09:14 +00007098 int bAsync = 0;
drh69ed38a2018-05-14 00:23:08 +00007099 const char *zVfs = 0;
drh2ce15c32017-07-11 13:34:40 +00007100 for(j=1; j<nArg; j++){
7101 const char *z = azArg[j];
7102 if( z[0]=='-' ){
drh69ed38a2018-05-14 00:23:08 +00007103 if( z[1]=='-' ) z++;
7104 if( strcmp(z, "-append")==0 ){
7105 zVfs = "apndvfs";
7106 }else
drha50bffb2018-12-08 01:09:14 +00007107 if( strcmp(z, "-async")==0 ){
7108 bAsync = 1;
7109 }else
drh2ce15c32017-07-11 13:34:40 +00007110 {
7111 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
7112 return 1;
7113 }
7114 }else if( zDestFile==0 ){
7115 zDestFile = azArg[j];
7116 }else if( zDb==0 ){
7117 zDb = zDestFile;
7118 zDestFile = azArg[j];
7119 }else{
drha50bffb2018-12-08 01:09:14 +00007120 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
drh2ce15c32017-07-11 13:34:40 +00007121 return 1;
7122 }
7123 }
7124 if( zDestFile==0 ){
7125 raw_printf(stderr, "missing FILENAME argument on .backup\n");
7126 return 1;
7127 }
7128 if( zDb==0 ) zDb = "main";
drh69ed38a2018-05-14 00:23:08 +00007129 rc = sqlite3_open_v2(zDestFile, &pDest,
7130 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
drh2ce15c32017-07-11 13:34:40 +00007131 if( rc!=SQLITE_OK ){
7132 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
drh9e804032018-05-18 17:11:50 +00007133 close_db(pDest);
drh2ce15c32017-07-11 13:34:40 +00007134 return 1;
7135 }
drha50bffb2018-12-08 01:09:14 +00007136 if( bAsync ){
7137 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
7138 0, 0, 0);
7139 }
drh2ce15c32017-07-11 13:34:40 +00007140 open_db(p, 0);
7141 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
7142 if( pBackup==0 ){
7143 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
drh9e804032018-05-18 17:11:50 +00007144 close_db(pDest);
drh2ce15c32017-07-11 13:34:40 +00007145 return 1;
7146 }
7147 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
7148 sqlite3_backup_finish(pBackup);
7149 if( rc==SQLITE_DONE ){
7150 rc = 0;
7151 }else{
7152 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
7153 rc = 1;
7154 }
drh9e804032018-05-18 17:11:50 +00007155 close_db(pDest);
drh2ce15c32017-07-11 13:34:40 +00007156 }else
7157
7158 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
7159 if( nArg==2 ){
7160 bail_on_error = booleanValue(azArg[1]);
7161 }else{
7162 raw_printf(stderr, "Usage: .bail on|off\n");
7163 rc = 1;
7164 }
7165 }else
7166
7167 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
7168 if( nArg==2 ){
7169 if( booleanValue(azArg[1]) ){
7170 setBinaryMode(p->out, 1);
7171 }else{
7172 setTextMode(p->out, 1);
7173 }
7174 }else{
7175 raw_printf(stderr, "Usage: .binary on|off\n");
7176 rc = 1;
7177 }
7178 }else
7179
7180 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
7181 if( nArg==2 ){
7182#if defined(_WIN32) || defined(WIN32)
7183 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
7184 rc = !SetCurrentDirectoryW(z);
7185 sqlite3_free(z);
7186#else
7187 rc = chdir(azArg[1]);
7188#endif
7189 if( rc ){
7190 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
7191 rc = 1;
7192 }
7193 }else{
7194 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
7195 rc = 1;
7196 }
7197 }else
7198
7199 /* The undocumented ".breakpoint" command causes a call to the no-op
7200 ** routine named test_breakpoint().
7201 */
7202 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
7203 test_breakpoint();
7204 }else
7205
7206 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
7207 if( nArg==2 ){
7208 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
7209 }else{
7210 raw_printf(stderr, "Usage: .changes on|off\n");
7211 rc = 1;
7212 }
7213 }else
7214
7215 /* Cancel output redirection, if it is currently set (by .testcase)
7216 ** Then read the content of the testcase-out.txt file and compare against
7217 ** azArg[1]. If there are differences, report an error and exit.
7218 */
7219 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
7220 char *zRes = 0;
7221 output_reset(p);
7222 if( nArg!=2 ){
7223 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
7224 rc = 2;
7225 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
7226 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
7227 rc = 2;
7228 }else if( testcase_glob(azArg[1],zRes)==0 ){
7229 utf8_printf(stderr,
7230 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
7231 p->zTestcase, azArg[1], zRes);
drhf30d3452017-10-17 13:44:46 +00007232 rc = 1;
drh2ce15c32017-07-11 13:34:40 +00007233 }else{
7234 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
7235 p->nCheck++;
7236 }
7237 sqlite3_free(zRes);
7238 }else
7239
7240 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
7241 if( nArg==2 ){
7242 tryToClone(p, azArg[1]);
7243 }else{
7244 raw_printf(stderr, "Usage: .clone FILENAME\n");
7245 rc = 1;
7246 }
7247 }else
7248
7249 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
7250 ShellState data;
7251 char *zErrMsg = 0;
7252 open_db(p, 0);
7253 memcpy(&data, p, sizeof(data));
7254 data.showHeader = 0;
7255 data.cMode = data.mode = MODE_List;
7256 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
7257 data.cnt = 0;
7258 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
7259 callback, &data, &zErrMsg);
7260 if( zErrMsg ){
7261 utf8_printf(stderr,"Error: %s\n", zErrMsg);
7262 sqlite3_free(zErrMsg);
7263 rc = 1;
7264 }
7265 }else
7266
drh7df01192018-04-28 12:43:16 +00007267 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
drheb7f2a02018-09-26 18:02:32 +00007268 static const struct DbConfigChoices {
7269 const char *zName;
7270 int op;
7271 } aDbConfig[] = {
drhb945bcd2019-12-31 22:52:10 +00007272 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
7273 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
7274 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
drh0a6873b2019-06-14 21:25:25 +00007275 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
drhb945bcd2019-12-31 22:52:10 +00007276 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
drh0a6873b2019-06-14 21:25:25 +00007277 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
drh11d88e62019-08-15 21:27:20 +00007278 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
drh0a6873b2019-06-14 21:25:25 +00007279 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
drhb945bcd2019-12-31 22:52:10 +00007280 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
7281 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
drh0a6873b2019-06-14 21:25:25 +00007282 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
7283 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
drh0a6873b2019-06-14 21:25:25 +00007284 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
drhb945bcd2019-12-31 22:52:10 +00007285 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
drhb77da372020-01-07 16:09:11 +00007286 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
dan07312a62019-06-21 14:05:27 +00007287 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
drh7df01192018-04-28 12:43:16 +00007288 };
7289 int ii, v;
7290 open_db(p, 0);
7291 for(ii=0; ii<ArraySize(aDbConfig); ii++){
7292 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
7293 if( nArg>=3 ){
7294 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
7295 }
7296 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
drhb945bcd2019-12-31 22:52:10 +00007297 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
drh7df01192018-04-28 12:43:16 +00007298 if( nArg>1 ) break;
7299 }
7300 if( nArg>1 && ii==ArraySize(aDbConfig) ){
7301 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
7302 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
7303 }
7304 }else
7305
7306 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
drh2ce15c32017-07-11 13:34:40 +00007307 rc = shell_dbinfo_command(p, nArg, azArg);
7308 }else
7309
dan1b162162019-04-27 20:15:15 +00007310#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
dan68cb86e2019-04-20 20:57:28 +00007311 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
7312 open_db(p, 0);
danb9b71db2019-04-25 16:20:40 +00007313 rc = recoverDatabaseCmd(p, nArg, azArg);
dan68cb86e2019-04-20 20:57:28 +00007314 }else
dan1b162162019-04-27 20:15:15 +00007315#endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
dan68cb86e2019-04-20 20:57:28 +00007316
drh2ce15c32017-07-11 13:34:40 +00007317 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
drh8e9297f2020-03-25 12:50:13 +00007318 char *zLike = 0;
7319 char *zSql;
drh2ce15c32017-07-11 13:34:40 +00007320 int i;
7321 int savedShowHeader = p->showHeader;
drhf213b332018-07-05 17:35:46 +00007322 int savedShellFlags = p->shellFlgs;
7323 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
drh2ce15c32017-07-11 13:34:40 +00007324 for(i=1; i<nArg; i++){
7325 if( azArg[i][0]=='-' ){
7326 const char *z = azArg[i]+1;
7327 if( z[0]=='-' ) z++;
7328 if( strcmp(z,"preserve-rowids")==0 ){
7329#ifdef SQLITE_OMIT_VIRTUALTABLE
7330 raw_printf(stderr, "The --preserve-rowids option is not compatible"
7331 " with SQLITE_OMIT_VIRTUALTABLE\n");
7332 rc = 1;
7333 goto meta_command_exit;
7334#else
7335 ShellSetFlag(p, SHFLG_PreserveRowid);
7336#endif
7337 }else
7338 if( strcmp(z,"newlines")==0 ){
7339 ShellSetFlag(p, SHFLG_Newlines);
7340 }else
7341 {
7342 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
7343 rc = 1;
7344 goto meta_command_exit;
7345 }
7346 }else if( zLike ){
drhcdbb2812020-03-25 20:31:45 +00007347 zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
7348 zLike, azArg[i]);
drh2ce15c32017-07-11 13:34:40 +00007349 }else{
drhcdbb2812020-03-25 20:31:45 +00007350 zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
drh2ce15c32017-07-11 13:34:40 +00007351 }
7352 }
dan68cb86e2019-04-20 20:57:28 +00007353
drh2ce15c32017-07-11 13:34:40 +00007354 open_db(p, 0);
dan68cb86e2019-04-20 20:57:28 +00007355
drh2ce15c32017-07-11 13:34:40 +00007356 /* When playing back a "dump", the content might appear in an order
7357 ** which causes immediate foreign key constraints to be violated.
7358 ** So disable foreign-key constraint enforcement to prevent problems. */
7359 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
7360 raw_printf(p->out, "BEGIN TRANSACTION;\n");
7361 p->writableSchema = 0;
7362 p->showHeader = 0;
7363 /* Set writable_schema=ON since doing so forces SQLite to initialize
7364 ** as much of the schema as it can even if the sqlite_master table is
7365 ** corrupt. */
7366 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
7367 p->nErr = 0;
drh8e9297f2020-03-25 12:50:13 +00007368 if( zLike==0 ) zLike = sqlite3_mprintf("true");
7369 zSql = sqlite3_mprintf(
7370 "SELECT name, type, sql FROM sqlite_master "
7371 "WHERE (%s) AND type=='table'"
7372 " AND sql NOT NULL"
7373 " ORDER BY tbl_name='sqlite_sequence', rowid",
7374 zLike
7375 );
7376 run_schema_dump_query(p,zSql);
7377 sqlite3_free(zSql);
7378 zSql = sqlite3_mprintf(
7379 "SELECT sql FROM sqlite_master "
7380 "WHERE (%s) AND sql NOT NULL"
7381 " AND type IN ('index','trigger','view')",
7382 zLike
7383 );
7384 run_table_dump_query(p, zSql);
7385 sqlite3_free(zSql);
7386 sqlite3_free(zLike);
drh2ce15c32017-07-11 13:34:40 +00007387 if( p->writableSchema ){
7388 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
7389 p->writableSchema = 0;
7390 }
7391 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
7392 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
dan68cb86e2019-04-20 20:57:28 +00007393 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
drh2ce15c32017-07-11 13:34:40 +00007394 p->showHeader = savedShowHeader;
drhf213b332018-07-05 17:35:46 +00007395 p->shellFlgs = savedShellFlags;
drh2ce15c32017-07-11 13:34:40 +00007396 }else
7397
7398 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
7399 if( nArg==2 ){
7400 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
7401 }else{
7402 raw_printf(stderr, "Usage: .echo on|off\n");
7403 rc = 1;
7404 }
7405 }else
7406
7407 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
7408 if( nArg==2 ){
drhe2ca99c2018-05-02 00:33:43 +00007409 p->autoEQPtest = 0;
drhb4e50392019-01-26 15:40:04 +00007410 if( p->autoEQPtrace ){
7411 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
7412 p->autoEQPtrace = 0;
7413 }
drh2ce15c32017-07-11 13:34:40 +00007414 if( strcmp(azArg[1],"full")==0 ){
drhada70452017-12-21 21:02:27 +00007415 p->autoEQP = AUTOEQP_full;
7416 }else if( strcmp(azArg[1],"trigger")==0 ){
7417 p->autoEQP = AUTOEQP_trigger;
drhb4e50392019-01-26 15:40:04 +00007418#ifdef SQLITE_DEBUG
drhe2ca99c2018-05-02 00:33:43 +00007419 }else if( strcmp(azArg[1],"test")==0 ){
7420 p->autoEQP = AUTOEQP_on;
7421 p->autoEQPtest = 1;
drhb4e50392019-01-26 15:40:04 +00007422 }else if( strcmp(azArg[1],"trace")==0 ){
7423 p->autoEQP = AUTOEQP_full;
7424 p->autoEQPtrace = 1;
7425 open_db(p, 0);
drhc07eee72019-01-27 19:50:56 +00007426 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
drhb4e50392019-01-26 15:40:04 +00007427 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
7428#endif
drh2ce15c32017-07-11 13:34:40 +00007429 }else{
mistachkinb71aa092018-01-23 00:05:18 +00007430 p->autoEQP = (u8)booleanValue(azArg[1]);
drh2ce15c32017-07-11 13:34:40 +00007431 }
7432 }else{
drhb4e50392019-01-26 15:40:04 +00007433 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
drh2ce15c32017-07-11 13:34:40 +00007434 rc = 1;
7435 }
7436 }else
7437
7438 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
7439 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
7440 rc = 2;
7441 }else
7442
7443 /* The ".explain" command is automatic now. It is largely pointless. It
7444 ** retained purely for backwards compatibility */
7445 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
7446 int val = 1;
7447 if( nArg>=2 ){
7448 if( strcmp(azArg[1],"auto")==0 ){
7449 val = 99;
7450 }else{
7451 val = booleanValue(azArg[1]);
7452 }
7453 }
7454 if( val==1 && p->mode!=MODE_Explain ){
7455 p->normalMode = p->mode;
7456 p->mode = MODE_Explain;
7457 p->autoExplain = 0;
7458 }else if( val==0 ){
7459 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7460 p->autoExplain = 0;
7461 }else if( val==99 ){
7462 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
7463 p->autoExplain = 1;
7464 }
7465 }else
7466
dan6b046be2018-01-09 15:25:55 +00007467#ifndef SQLITE_OMIT_VIRTUALTABLE
dan43efc182017-12-19 17:42:13 +00007468 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
7469 open_db(p, 0);
7470 expertDotCommand(p, azArg, nArg);
7471 }else
dan6b046be2018-01-09 15:25:55 +00007472#endif
dan43efc182017-12-19 17:42:13 +00007473
drhd985f722019-06-05 14:29:53 +00007474 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
7475 static const struct {
7476 const char *zCtrlName; /* Name of a test-control option */
7477 int ctrlCode; /* Integer code for that option */
7478 const char *zUsage; /* Usage notes */
7479 } aCtrl[] = {
7480 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
7481 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
7482 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
7483 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
7484 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
7485 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
7486 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
7487 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
7488 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
drh541ef2c2020-04-20 16:21:30 +00007489 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
drhd985f722019-06-05 14:29:53 +00007490 };
7491 int filectrl = -1;
7492 int iCtrl = -1;
drh4245e042019-06-13 13:52:46 +00007493 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
7494 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
drhd985f722019-06-05 14:29:53 +00007495 int n2, i;
7496 const char *zCmd = 0;
drh541ef2c2020-04-20 16:21:30 +00007497 const char *zSchema = 0;
drhd985f722019-06-05 14:29:53 +00007498
7499 open_db(p, 0);
7500 zCmd = nArg>=2 ? azArg[1] : "help";
7501
drh541ef2c2020-04-20 16:21:30 +00007502 if( zCmd[0]=='-'
7503 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
7504 && nArg>=4
7505 ){
7506 zSchema = azArg[2];
7507 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
7508 nArg -= 2;
7509 zCmd = azArg[1];
7510 }
7511
drhd985f722019-06-05 14:29:53 +00007512 /* The argument can optionally begin with "-" or "--" */
7513 if( zCmd[0]=='-' && zCmd[1] ){
7514 zCmd++;
7515 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
7516 }
7517
7518 /* --help lists all file-controls */
7519 if( strcmp(zCmd,"help")==0 ){
7520 utf8_printf(p->out, "Available file-controls:\n");
7521 for(i=0; i<ArraySize(aCtrl); i++){
7522 utf8_printf(p->out, " .filectrl %s %s\n",
7523 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
7524 }
7525 rc = 1;
7526 goto meta_command_exit;
7527 }
7528
7529 /* convert filectrl text option to value. allow any unique prefix
7530 ** of the option name, or a numerical value. */
7531 n2 = strlen30(zCmd);
7532 for(i=0; i<ArraySize(aCtrl); i++){
7533 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
7534 if( filectrl<0 ){
7535 filectrl = aCtrl[i].ctrlCode;
7536 iCtrl = i;
7537 }else{
7538 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
7539 "Use \".filectrl --help\" for help\n", zCmd);
7540 rc = 1;
7541 goto meta_command_exit;
7542 }
7543 }
7544 }
7545 if( filectrl<0 ){
7546 utf8_printf(stderr,"Error: unknown file-control: %s\n"
7547 "Use \".filectrl --help\" for help\n", zCmd);
7548 }else{
7549 switch(filectrl){
7550 case SQLITE_FCNTL_SIZE_LIMIT: {
7551 if( nArg!=2 && nArg!=3 ) break;
7552 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
drh541ef2c2020-04-20 16:21:30 +00007553 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
drhd985f722019-06-05 14:29:53 +00007554 isOk = 1;
7555 break;
7556 }
7557 case SQLITE_FCNTL_LOCK_TIMEOUT:
7558 case SQLITE_FCNTL_CHUNK_SIZE: {
7559 int x;
7560 if( nArg!=3 ) break;
7561 x = (int)integerValue(azArg[2]);
drh541ef2c2020-04-20 16:21:30 +00007562 sqlite3_file_control(p->db, zSchema, filectrl, &x);
drhd985f722019-06-05 14:29:53 +00007563 isOk = 2;
7564 break;
7565 }
7566 case SQLITE_FCNTL_PERSIST_WAL:
7567 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
7568 int x;
7569 if( nArg!=2 && nArg!=3 ) break;
7570 x = nArg==3 ? booleanValue(azArg[2]) : -1;
drh541ef2c2020-04-20 16:21:30 +00007571 sqlite3_file_control(p->db, zSchema, filectrl, &x);
drhd985f722019-06-05 14:29:53 +00007572 iRes = x;
7573 isOk = 1;
7574 break;
7575 }
7576 case SQLITE_FCNTL_HAS_MOVED: {
7577 int x;
7578 if( nArg!=2 ) break;
drh541ef2c2020-04-20 16:21:30 +00007579 sqlite3_file_control(p->db, zSchema, filectrl, &x);
drhd985f722019-06-05 14:29:53 +00007580 iRes = x;
7581 isOk = 1;
7582 break;
7583 }
7584 case SQLITE_FCNTL_TEMPFILENAME: {
7585 char *z = 0;
7586 if( nArg!=2 ) break;
drh541ef2c2020-04-20 16:21:30 +00007587 sqlite3_file_control(p->db, zSchema, filectrl, &z);
drhd985f722019-06-05 14:29:53 +00007588 if( z ){
7589 utf8_printf(p->out, "%s\n", z);
7590 sqlite3_free(z);
7591 }
7592 isOk = 2;
7593 break;
7594 }
drh541ef2c2020-04-20 16:21:30 +00007595 case SQLITE_FCNTL_RESERVE_BYTES: {
7596 int x;
7597 if( nArg>=3 ){
7598 x = atoi(azArg[2]);
7599 sqlite3_file_control(p->db, zSchema, filectrl, &x);
7600 }
7601 x = -1;
7602 sqlite3_file_control(p->db, zSchema, filectrl, &x);
7603 utf8_printf(p->out,"%d\n", x);
7604 isOk = 2;
7605 break;
7606 }
drhd985f722019-06-05 14:29:53 +00007607 }
7608 }
7609 if( isOk==0 && iCtrl>=0 ){
7610 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
7611 rc = 1;
7612 }else if( isOk==1 ){
drhe2500762019-06-13 14:07:41 +00007613 char zBuf[100];
7614 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
7615 raw_printf(p->out, "%s\n", zBuf);
drhd985f722019-06-05 14:29:53 +00007616 }
7617 }else
7618
drh2ce15c32017-07-11 13:34:40 +00007619 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
7620 ShellState data;
7621 char *zErrMsg = 0;
7622 int doStats = 0;
7623 memcpy(&data, p, sizeof(data));
7624 data.showHeader = 0;
7625 data.cMode = data.mode = MODE_Semi;
7626 if( nArg==2 && optionMatch(azArg[1], "indent") ){
7627 data.cMode = data.mode = MODE_Pretty;
7628 nArg = 1;
7629 }
7630 if( nArg!=1 ){
7631 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
7632 rc = 1;
7633 goto meta_command_exit;
7634 }
7635 open_db(p, 0);
7636 rc = sqlite3_exec(p->db,
7637 "SELECT sql FROM"
7638 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
7639 " FROM sqlite_master UNION ALL"
7640 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
7641 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
7642 "ORDER BY rowid",
7643 callback, &data, &zErrMsg
7644 );
7645 if( rc==SQLITE_OK ){
7646 sqlite3_stmt *pStmt;
7647 rc = sqlite3_prepare_v2(p->db,
7648 "SELECT rowid FROM sqlite_master"
7649 " WHERE name GLOB 'sqlite_stat[134]'",
7650 -1, &pStmt, 0);
7651 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
7652 sqlite3_finalize(pStmt);
7653 }
7654 if( doStats==0 ){
7655 raw_printf(p->out, "/* No STAT tables available */\n");
7656 }else{
7657 raw_printf(p->out, "ANALYZE sqlite_master;\n");
7658 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
7659 callback, &data, &zErrMsg);
7660 data.cMode = data.mode = MODE_Insert;
7661 data.zDestTable = "sqlite_stat1";
drh4c540452018-05-08 23:17:36 +00007662 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
drh2ce15c32017-07-11 13:34:40 +00007663 data.zDestTable = "sqlite_stat4";
drh4c540452018-05-08 23:17:36 +00007664 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
drh2ce15c32017-07-11 13:34:40 +00007665 raw_printf(p->out, "ANALYZE sqlite_master;\n");
7666 }
7667 }else
7668
7669 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
7670 if( nArg==2 ){
7671 p->showHeader = booleanValue(azArg[1]);
7672 }else{
7673 raw_printf(stderr, "Usage: .headers on|off\n");
7674 rc = 1;
7675 }
7676 }else
7677
7678 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
drh98aa2ab2018-09-26 16:53:51 +00007679 if( nArg>=2 ){
drhe93f8262018-10-11 16:53:37 +00007680 n = showHelp(p->out, azArg[1]);
drh98aa2ab2018-09-26 16:53:51 +00007681 if( n==0 ){
7682 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
7683 }
7684 }else{
7685 showHelp(p->out, 0);
7686 }
drh2ce15c32017-07-11 13:34:40 +00007687 }else
7688
7689 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
drhccb37812020-03-09 15:39:39 +00007690 char *zTable = 0; /* Insert data into this table */
7691 char *zFile = 0; /* Name of file to extra content from */
drh2ce15c32017-07-11 13:34:40 +00007692 sqlite3_stmt *pStmt = NULL; /* A statement */
7693 int nCol; /* Number of columns in the table */
7694 int nByte; /* Number of bytes in an SQL string */
7695 int i, j; /* Loop counters */
7696 int needCommit; /* True to COMMIT or ROLLBACK at end */
7697 int nSep; /* Number of bytes in p->colSeparator[] */
7698 char *zSql; /* An SQL statement */
7699 ImportCtx sCtx; /* Reader context */
7700 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
7701 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
drhccb37812020-03-09 15:39:39 +00007702 int eVerbose = 0; /* Larger for more console output */
7703 int nSkip = 0; /* Initial lines to skip */
7704 int useOutputMode = 1; /* Use output mode to determine separators */
drh2ce15c32017-07-11 13:34:40 +00007705
drhccb37812020-03-09 15:39:39 +00007706 memset(&sCtx, 0, sizeof(sCtx));
7707 if( p->mode==MODE_Ascii ){
7708 xRead = ascii_read_one_field;
7709 }else{
7710 xRead = csv_read_one_field;
7711 }
7712 for(i=1; i<nArg; i++){
7713 char *z = azArg[i];
7714 if( z[0]=='-' && z[1]=='-' ) z++;
7715 if( z[0]!='-' ){
7716 if( zFile==0 ){
7717 zFile = z;
7718 }else if( zTable==0 ){
7719 zTable = z;
7720 }else{
7721 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
7722 showHelp(p->out, "import");
7723 rc = 1;
7724 goto meta_command_exit;
7725 }
7726 }else if( strcmp(z,"-v")==0 ){
7727 eVerbose++;
7728 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
7729 nSkip = integerValue(azArg[++i]);
7730 }else if( strcmp(z,"-ascii")==0 ){
7731 sCtx.cColSep = SEP_Unit[0];
7732 sCtx.cRowSep = SEP_Record[0];
7733 xRead = ascii_read_one_field;
7734 useOutputMode = 0;
7735 }else if( strcmp(z,"-csv")==0 ){
7736 sCtx.cColSep = ',';
7737 sCtx.cRowSep = '\n';
7738 xRead = csv_read_one_field;
7739 useOutputMode = 0;
7740 }else{
7741 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
7742 showHelp(p->out, "import");
7743 rc = 1;
7744 goto meta_command_exit;
7745 }
7746 }
7747 if( zTable==0 ){
7748 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
7749 zFile==0 ? "FILE" : "TABLE");
7750 showHelp(p->out, "import");
7751 rc = 1;
drh2ce15c32017-07-11 13:34:40 +00007752 goto meta_command_exit;
7753 }
drh2ce15c32017-07-11 13:34:40 +00007754 seenInterrupt = 0;
drh2ce15c32017-07-11 13:34:40 +00007755 open_db(p, 0);
drhccb37812020-03-09 15:39:39 +00007756 if( useOutputMode ){
7757 /* If neither the --csv or --ascii options are specified, then set
7758 ** the column and row separator characters from the output mode. */
7759 nSep = strlen30(p->colSeparator);
7760 if( nSep==0 ){
7761 raw_printf(stderr,
7762 "Error: non-null column separator required for import\n");
7763 rc = 1;
7764 goto meta_command_exit;
7765 }
7766 if( nSep>1 ){
7767 raw_printf(stderr,
7768 "Error: multi-character column separators not allowed"
7769 " for import\n");
7770 rc = 1;
7771 goto meta_command_exit;
7772 }
drh2ce15c32017-07-11 13:34:40 +00007773 nSep = strlen30(p->rowSeparator);
drhccb37812020-03-09 15:39:39 +00007774 if( nSep==0 ){
7775 raw_printf(stderr,
7776 "Error: non-null row separator required for import\n");
7777 rc = 1;
7778 goto meta_command_exit;
7779 }
7780 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
7781 /* When importing CSV (only), if the row separator is set to the
7782 ** default output row separator, change it to the default input
7783 ** row separator. This avoids having to maintain different input
7784 ** and output row separators. */
7785 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
7786 nSep = strlen30(p->rowSeparator);
7787 }
7788 if( nSep>1 ){
7789 raw_printf(stderr, "Error: multi-character row separators not allowed"
7790 " for import\n");
7791 rc = 1;
7792 goto meta_command_exit;
7793 }
7794 sCtx.cColSep = p->colSeparator[0];
7795 sCtx.cRowSep = p->rowSeparator[0];
drh2ce15c32017-07-11 13:34:40 +00007796 }
7797 sCtx.zFile = zFile;
7798 sCtx.nLine = 1;
7799 if( sCtx.zFile[0]=='|' ){
7800#ifdef SQLITE_OMIT_POPEN
7801 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
drhccb37812020-03-09 15:39:39 +00007802 rc = 1;
7803 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007804#else
7805 sCtx.in = popen(sCtx.zFile+1, "r");
7806 sCtx.zFile = "<pipe>";
7807 xCloser = pclose;
7808#endif
7809 }else{
7810 sCtx.in = fopen(sCtx.zFile, "rb");
7811 xCloser = fclose;
7812 }
drh2ce15c32017-07-11 13:34:40 +00007813 if( sCtx.in==0 ){
7814 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
drhccb37812020-03-09 15:39:39 +00007815 rc = 1;
7816 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007817 }
drhccb37812020-03-09 15:39:39 +00007818 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
7819 char zSep[2];
7820 zSep[1] = 0;
7821 zSep[0] = sCtx.cColSep;
7822 utf8_printf(p->out, "Column separator ");
7823 output_c_string(p->out, zSep);
7824 utf8_printf(p->out, ", row separator ");
7825 zSep[0] = sCtx.cRowSep;
7826 output_c_string(p->out, zSep);
7827 utf8_printf(p->out, "\n");
7828 }
7829 while( (nSkip--)>0 ){
7830 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
7831 sCtx.nLine++;
7832 }
drh2ce15c32017-07-11 13:34:40 +00007833 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
7834 if( zSql==0 ){
drh2ce15c32017-07-11 13:34:40 +00007835 xCloser(sCtx.in);
drh4b5345c2018-04-24 13:07:40 +00007836 shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00007837 }
7838 nByte = strlen30(zSql);
7839 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7840 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
7841 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
7842 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
7843 char cSep = '(';
7844 while( xRead(&sCtx) ){
7845 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
7846 cSep = ',';
7847 if( sCtx.cTerm!=sCtx.cColSep ) break;
7848 }
7849 if( cSep=='(' ){
7850 sqlite3_free(zCreate);
7851 sqlite3_free(sCtx.z);
7852 xCloser(sCtx.in);
7853 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
drhccb37812020-03-09 15:39:39 +00007854 rc = 1;
7855 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007856 }
7857 zCreate = sqlite3_mprintf("%z\n)", zCreate);
drhccb37812020-03-09 15:39:39 +00007858 if( eVerbose>=1 ){
7859 utf8_printf(p->out, "%s\n", zCreate);
7860 }
drh2ce15c32017-07-11 13:34:40 +00007861 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
7862 sqlite3_free(zCreate);
7863 if( rc ){
7864 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
7865 sqlite3_errmsg(p->db));
7866 sqlite3_free(sCtx.z);
7867 xCloser(sCtx.in);
drhccb37812020-03-09 15:39:39 +00007868 rc = 1;
7869 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007870 }
7871 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7872 }
7873 sqlite3_free(zSql);
7874 if( rc ){
7875 if (pStmt) sqlite3_finalize(pStmt);
7876 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
7877 xCloser(sCtx.in);
drhccb37812020-03-09 15:39:39 +00007878 rc = 1;
7879 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007880 }
7881 nCol = sqlite3_column_count(pStmt);
7882 sqlite3_finalize(pStmt);
7883 pStmt = 0;
7884 if( nCol==0 ) return 0; /* no columns, no error */
7885 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
7886 if( zSql==0 ){
drh2ce15c32017-07-11 13:34:40 +00007887 xCloser(sCtx.in);
drh4b5345c2018-04-24 13:07:40 +00007888 shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00007889 }
7890 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
7891 j = strlen30(zSql);
7892 for(i=1; i<nCol; i++){
7893 zSql[j++] = ',';
7894 zSql[j++] = '?';
7895 }
7896 zSql[j++] = ')';
7897 zSql[j] = 0;
drhccb37812020-03-09 15:39:39 +00007898 if( eVerbose>=2 ){
7899 utf8_printf(p->out, "Insert using: %s\n", zSql);
7900 }
drh2ce15c32017-07-11 13:34:40 +00007901 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
7902 sqlite3_free(zSql);
7903 if( rc ){
7904 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
7905 if (pStmt) sqlite3_finalize(pStmt);
7906 xCloser(sCtx.in);
drhccb37812020-03-09 15:39:39 +00007907 rc = 1;
7908 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00007909 }
7910 needCommit = sqlite3_get_autocommit(p->db);
7911 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
7912 do{
7913 int startLine = sCtx.nLine;
7914 for(i=0; i<nCol; i++){
7915 char *z = xRead(&sCtx);
7916 /*
7917 ** Did we reach end-of-file before finding any columns?
7918 ** If so, stop instead of NULL filling the remaining columns.
7919 */
7920 if( z==0 && i==0 ) break;
7921 /*
7922 ** Did we reach end-of-file OR end-of-line before finding any
7923 ** columns in ASCII mode? If so, stop instead of NULL filling
7924 ** the remaining columns.
7925 */
7926 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
7927 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
7928 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
7929 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7930 "filling the rest with NULL\n",
7931 sCtx.zFile, startLine, nCol, i+1);
7932 i += 2;
7933 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
7934 }
7935 }
7936 if( sCtx.cTerm==sCtx.cColSep ){
7937 do{
7938 xRead(&sCtx);
7939 i++;
7940 }while( sCtx.cTerm==sCtx.cColSep );
7941 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
7942 "extras ignored\n",
7943 sCtx.zFile, startLine, nCol, i);
7944 }
7945 if( i>=nCol ){
7946 sqlite3_step(pStmt);
7947 rc = sqlite3_reset(pStmt);
7948 if( rc!=SQLITE_OK ){
7949 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
7950 startLine, sqlite3_errmsg(p->db));
drhccb37812020-03-09 15:39:39 +00007951 sCtx.nErr++;
7952 }else{
7953 sCtx.nRow++;
drh2ce15c32017-07-11 13:34:40 +00007954 }
7955 }
7956 }while( sCtx.cTerm!=EOF );
7957
7958 xCloser(sCtx.in);
7959 sqlite3_free(sCtx.z);
7960 sqlite3_finalize(pStmt);
7961 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
drhccb37812020-03-09 15:39:39 +00007962 if( eVerbose>0 ){
7963 utf8_printf(p->out,
7964 "Added %d rows with %d errors using %d lines of input\n",
7965 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
7966 }
drh2ce15c32017-07-11 13:34:40 +00007967 }else
7968
7969#ifndef SQLITE_UNTESTABLE
7970 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
7971 char *zSql;
7972 char *zCollist = 0;
7973 sqlite3_stmt *pStmt;
7974 int tnum = 0;
drh491c5be2019-10-18 15:58:50 +00007975 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
7976 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
drh2ce15c32017-07-11 13:34:40 +00007977 int i;
drh48d219a2018-04-23 18:38:48 +00007978 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
7979 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
7980 " .imposter off\n");
drh491c5be2019-10-18 15:58:50 +00007981 /* Also allowed, but not documented:
7982 **
7983 ** .imposter TABLE IMPOSTER
7984 **
7985 ** where TABLE is a WITHOUT ROWID table. In that case, the
7986 ** imposter is another WITHOUT ROWID table with the columns in
7987 ** storage order. */
drh2ce15c32017-07-11 13:34:40 +00007988 rc = 1;
7989 goto meta_command_exit;
7990 }
7991 open_db(p, 0);
drh48d219a2018-04-23 18:38:48 +00007992 if( nArg==2 ){
7993 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
7994 goto meta_command_exit;
7995 }
drh491c5be2019-10-18 15:58:50 +00007996 zSql = sqlite3_mprintf(
7997 "SELECT rootpage, 0 FROM sqlite_master"
7998 " WHERE name='%q' AND type='index'"
7999 "UNION ALL "
8000 "SELECT rootpage, 1 FROM sqlite_master"
8001 " WHERE name='%q' AND type='table'"
8002 " AND sql LIKE '%%without%%rowid%%'",
8003 azArg[1], azArg[1]
8004 );
drh2ce15c32017-07-11 13:34:40 +00008005 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8006 sqlite3_free(zSql);
8007 if( sqlite3_step(pStmt)==SQLITE_ROW ){
8008 tnum = sqlite3_column_int(pStmt, 0);
drh491c5be2019-10-18 15:58:50 +00008009 isWO = sqlite3_column_int(pStmt, 1);
drh2ce15c32017-07-11 13:34:40 +00008010 }
8011 sqlite3_finalize(pStmt);
drh2ce15c32017-07-11 13:34:40 +00008012 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
8013 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8014 sqlite3_free(zSql);
8015 i = 0;
8016 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8017 char zLabel[20];
8018 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
8019 i++;
8020 if( zCol==0 ){
8021 if( sqlite3_column_int(pStmt,1)==-1 ){
8022 zCol = "_ROWID_";
8023 }else{
8024 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
8025 zCol = zLabel;
8026 }
8027 }
drh491c5be2019-10-18 15:58:50 +00008028 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
8029 lenPK = (int)strlen(zCollist);
8030 }
drh2ce15c32017-07-11 13:34:40 +00008031 if( zCollist==0 ){
8032 zCollist = sqlite3_mprintf("\"%w\"", zCol);
8033 }else{
8034 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
8035 }
8036 }
8037 sqlite3_finalize(pStmt);
drh491c5be2019-10-18 15:58:50 +00008038 if( i==0 || tnum==0 ){
8039 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
8040 rc = 1;
8041 sqlite3_free(zCollist);
8042 goto meta_command_exit;
8043 }
8044 if( lenPK==0 ) lenPK = 100000;
drh2ce15c32017-07-11 13:34:40 +00008045 zSql = sqlite3_mprintf(
drh491c5be2019-10-18 15:58:50 +00008046 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
8047 azArg[2], zCollist, lenPK, zCollist);
drh2ce15c32017-07-11 13:34:40 +00008048 sqlite3_free(zCollist);
8049 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
8050 if( rc==SQLITE_OK ){
8051 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
8052 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
8053 if( rc ){
8054 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
8055 }else{
8056 utf8_printf(stdout, "%s;\n", zSql);
8057 raw_printf(stdout,
drh491c5be2019-10-18 15:58:50 +00008058 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
8059 azArg[1], isWO ? "table" : "index"
drh2ce15c32017-07-11 13:34:40 +00008060 );
8061 }
8062 }else{
8063 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
8064 rc = 1;
8065 }
8066 sqlite3_free(zSql);
8067 }else
8068#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
8069
8070#ifdef SQLITE_ENABLE_IOTRACE
8071 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
8072 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
8073 if( iotrace && iotrace!=stdout ) fclose(iotrace);
8074 iotrace = 0;
8075 if( nArg<2 ){
8076 sqlite3IoTrace = 0;
8077 }else if( strcmp(azArg[1], "-")==0 ){
8078 sqlite3IoTrace = iotracePrintf;
8079 iotrace = stdout;
8080 }else{
8081 iotrace = fopen(azArg[1], "w");
8082 if( iotrace==0 ){
8083 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
8084 sqlite3IoTrace = 0;
8085 rc = 1;
8086 }else{
8087 sqlite3IoTrace = iotracePrintf;
8088 }
8089 }
8090 }else
8091#endif
8092
8093 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
8094 static const struct {
8095 const char *zLimitName; /* Name of a limit */
8096 int limitCode; /* Integer code for that limit */
8097 } aLimit[] = {
8098 { "length", SQLITE_LIMIT_LENGTH },
8099 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
8100 { "column", SQLITE_LIMIT_COLUMN },
8101 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
8102 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
8103 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
8104 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
8105 { "attached", SQLITE_LIMIT_ATTACHED },
8106 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
8107 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
8108 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
8109 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
8110 };
8111 int i, n2;
8112 open_db(p, 0);
8113 if( nArg==1 ){
8114 for(i=0; i<ArraySize(aLimit); i++){
8115 printf("%20s %d\n", aLimit[i].zLimitName,
8116 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
8117 }
8118 }else if( nArg>3 ){
8119 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
8120 rc = 1;
8121 goto meta_command_exit;
8122 }else{
8123 int iLimit = -1;
8124 n2 = strlen30(azArg[1]);
8125 for(i=0; i<ArraySize(aLimit); i++){
8126 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
8127 if( iLimit<0 ){
8128 iLimit = i;
8129 }else{
8130 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
8131 rc = 1;
8132 goto meta_command_exit;
8133 }
8134 }
8135 }
8136 if( iLimit<0 ){
8137 utf8_printf(stderr, "unknown limit: \"%s\"\n"
8138 "enter \".limits\" with no arguments for a list.\n",
8139 azArg[1]);
8140 rc = 1;
8141 goto meta_command_exit;
8142 }
8143 if( nArg==3 ){
8144 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
8145 (int)integerValue(azArg[2]));
8146 }
8147 printf("%20s %d\n", aLimit[iLimit].zLimitName,
8148 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
8149 }
8150 }else
8151
8152 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
8153 open_db(p, 0);
8154 lintDotCommand(p, azArg, nArg);
8155 }else
8156
8157#ifndef SQLITE_OMIT_LOAD_EXTENSION
8158 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
8159 const char *zFile, *zProc;
8160 char *zErrMsg = 0;
8161 if( nArg<2 ){
8162 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
8163 rc = 1;
8164 goto meta_command_exit;
8165 }
8166 zFile = azArg[1];
8167 zProc = nArg>=3 ? azArg[2] : 0;
8168 open_db(p, 0);
8169 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
8170 if( rc!=SQLITE_OK ){
8171 utf8_printf(stderr, "Error: %s\n", zErrMsg);
8172 sqlite3_free(zErrMsg);
8173 rc = 1;
8174 }
8175 }else
8176#endif
8177
8178 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
8179 if( nArg!=2 ){
8180 raw_printf(stderr, "Usage: .log FILENAME\n");
8181 rc = 1;
8182 }else{
8183 const char *zFile = azArg[1];
8184 output_file_close(p->pLog);
drha92a01a2018-01-10 22:15:37 +00008185 p->pLog = output_file_open(zFile, 0);
drh2ce15c32017-07-11 13:34:40 +00008186 }
8187 }else
8188
8189 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
8190 const char *zMode = nArg>=2 ? azArg[1] : "";
drhaf2770f2018-01-05 14:55:43 +00008191 int n2 = strlen30(zMode);
drh2ce15c32017-07-11 13:34:40 +00008192 int c2 = zMode[0];
8193 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
8194 p->mode = MODE_Line;
8195 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8196 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
8197 p->mode = MODE_Column;
8198 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8199 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
8200 p->mode = MODE_List;
8201 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
8202 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8203 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
8204 p->mode = MODE_Html;
8205 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
8206 p->mode = MODE_Tcl;
8207 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
8208 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
8209 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
8210 p->mode = MODE_Csv;
8211 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8212 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8213 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
8214 p->mode = MODE_List;
8215 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
8216 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
8217 p->mode = MODE_Insert;
8218 set_table_name(p, nArg>=3 ? azArg[2] : "table");
8219 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
8220 p->mode = MODE_Quote;
8221 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
8222 p->mode = MODE_Ascii;
8223 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
8224 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
8225 }else if( nArg==1 ){
8226 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
8227 }else{
8228 raw_printf(stderr, "Error: mode should be one of: "
8229 "ascii column csv html insert line list quote tabs tcl\n");
8230 rc = 1;
8231 }
8232 p->cMode = p->mode;
8233 }else
8234
8235 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
8236 if( nArg==2 ){
8237 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
8238 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
8239 }else{
8240 raw_printf(stderr, "Usage: .nullvalue STRING\n");
8241 rc = 1;
8242 }
8243 }else
8244
drh4a3a3eb2020-02-29 15:53:48 +00008245#ifdef SQLITE_DEBUG
8246 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
8247 int i;
8248 for(i=1; i<nArg; i++){
8249 const char *z = azArg[i];
8250 if( z[0]=='-' && z[1]=='-' ) z++;
8251 if( strcmp(z,"-repeat")==0 ){
8252 if( i==nArg-1 ){
8253 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
8254 rc = 1;
8255 }else{
8256 oomRepeat = (int)integerValue(azArg[++i]);
8257 }
8258 }else if( IsDigit(z[0]) ){
8259 oomCounter = (int)integerValue(azArg[i]);
8260 }else{
8261 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
8262 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
8263 rc = 1;
8264 }
8265 }
8266 if( rc==0 ){
8267 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
8268 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
8269 }
8270 }else
8271#endif /* SQLITE_DEBUG */
8272
drh2ce15c32017-07-11 13:34:40 +00008273 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
8274 char *zNewFilename; /* Name of the database file to open */
8275 int iName = 1; /* Index in azArg[] of the filename */
8276 int newFlag = 0; /* True to delete file before opening */
8277 /* Close the existing database */
8278 session_close_all(p);
drh9e804032018-05-18 17:11:50 +00008279 close_db(p->db);
drh2ce15c32017-07-11 13:34:40 +00008280 p->db = 0;
8281 p->zDbFilename = 0;
8282 sqlite3_free(p->zFreeOnClose);
8283 p->zFreeOnClose = 0;
drh1fa6d9f2018-01-06 21:46:01 +00008284 p->openMode = SHELL_OPEN_UNSPEC;
drh0933aad2019-11-18 17:46:38 +00008285 p->openFlags = 0;
drh6ca64482019-01-22 16:06:20 +00008286 p->szMax = 0;
drh2ce15c32017-07-11 13:34:40 +00008287 /* Check for command-line arguments */
8288 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
8289 const char *z = azArg[iName];
8290 if( optionMatch(z,"new") ){
8291 newFlag = 1;
drh3baed312018-03-08 18:14:41 +00008292#ifdef SQLITE_HAVE_ZLIB
drh1fa6d9f2018-01-06 21:46:01 +00008293 }else if( optionMatch(z, "zip") ){
8294 p->openMode = SHELL_OPEN_ZIPFILE;
8295#endif
8296 }else if( optionMatch(z, "append") ){
8297 p->openMode = SHELL_OPEN_APPENDVFS;
drhee269a62018-02-14 23:27:43 +00008298 }else if( optionMatch(z, "readonly") ){
8299 p->openMode = SHELL_OPEN_READONLY;
drh0933aad2019-11-18 17:46:38 +00008300 }else if( optionMatch(z, "nofollow") ){
8301 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
drha751f392018-10-30 15:31:22 +00008302#ifdef SQLITE_ENABLE_DESERIALIZE
drh60f34ae2018-10-30 13:19:49 +00008303 }else if( optionMatch(z, "deserialize") ){
8304 p->openMode = SHELL_OPEN_DESERIALIZE;
drh33746482018-12-13 15:06:26 +00008305 }else if( optionMatch(z, "hexdb") ){
8306 p->openMode = SHELL_OPEN_HEXDB;
drh6ca64482019-01-22 16:06:20 +00008307 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
8308 p->szMax = integerValue(azArg[++iName]);
drh33746482018-12-13 15:06:26 +00008309#endif /* SQLITE_ENABLE_DESERIALIZE */
drh2ce15c32017-07-11 13:34:40 +00008310 }else if( z[0]=='-' ){
8311 utf8_printf(stderr, "unknown option: %s\n", z);
8312 rc = 1;
8313 goto meta_command_exit;
8314 }
8315 }
8316 /* If a filename is specified, try to open it first */
8317 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
drh33746482018-12-13 15:06:26 +00008318 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
drh2ce15c32017-07-11 13:34:40 +00008319 if( newFlag ) shellDeleteFile(zNewFilename);
8320 p->zDbFilename = zNewFilename;
drhbe4ccb22018-05-17 20:04:24 +00008321 open_db(p, OPEN_DB_KEEPALIVE);
drh2ce15c32017-07-11 13:34:40 +00008322 if( p->db==0 ){
8323 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
8324 sqlite3_free(zNewFilename);
8325 }else{
8326 p->zFreeOnClose = zNewFilename;
8327 }
8328 }
8329 if( p->db==0 ){
8330 /* As a fall-back open a TEMP database */
8331 p->zDbFilename = 0;
8332 open_db(p, 0);
8333 }
8334 }else
8335
drh13c20932018-01-10 21:41:55 +00008336 if( (c=='o'
8337 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
8338 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
drh2ce15c32017-07-11 13:34:40 +00008339 ){
drh7a431002020-04-18 14:12:00 +00008340 const char *zFile = 0;
drha92a01a2018-01-10 22:15:37 +00008341 int bTxtMode = 0;
drh7a431002020-04-18 14:12:00 +00008342 int i;
8343 int eMode = 0;
8344 int bBOM = 0;
drh5415ab42020-04-23 20:45:46 +00008345 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
drh7a431002020-04-18 14:12:00 +00008346
8347 if( c=='e' ){
8348 eMode = 'x';
8349 bOnce = 2;
8350 }else if( strncmp(azArg[0],"once",n)==0 ){
8351 bOnce = 1;
drh13c20932018-01-10 21:41:55 +00008352 }
drh7a431002020-04-18 14:12:00 +00008353 for(i=1; i<nArg; i++){
8354 char *z = azArg[i];
8355 if( z[0]=='-' ){
8356 if( z[1]=='-' ) z++;
8357 if( strcmp(z,"-bom")==0 ){
8358 bBOM = 1;
8359 }else if( c!='e' && strcmp(z,"-x")==0 ){
8360 eMode = 'x'; /* spreadsheet */
8361 }else if( c!='e' && strcmp(z,"-e")==0 ){
8362 eMode = 'e'; /* text editor */
8363 }else{
8364 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
8365 azArg[i]);
8366 showHelp(p->out, azArg[0]);
8367 rc = 1;
8368 goto meta_command_exit;
8369 }
8370 }else if( zFile==0 ){
8371 zFile = z;
8372 }else{
8373 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
8374 azArg[i]);
8375 showHelp(p->out, azArg[0]);
drh2ce15c32017-07-11 13:34:40 +00008376 rc = 1;
8377 goto meta_command_exit;
8378 }
drh7a431002020-04-18 14:12:00 +00008379 }
8380 if( zFile==0 ) zFile = "stdout";
8381 if( bOnce ){
drh2ce15c32017-07-11 13:34:40 +00008382 p->outCount = 2;
8383 }else{
8384 p->outCount = 0;
8385 }
8386 output_reset(p);
drh04a28c32018-01-31 01:38:44 +00008387#ifndef SQLITE_NOHAVE_SYSTEM
drh7a431002020-04-18 14:12:00 +00008388 if( eMode=='e' || eMode=='x' ){
drh3c484e82018-01-10 22:27:21 +00008389 p->doXdgOpen = 1;
8390 outputModePush(p);
drh7a431002020-04-18 14:12:00 +00008391 if( eMode=='x' ){
8392 /* spreadsheet mode. Output as CSV. */
drh13c20932018-01-10 21:41:55 +00008393 newTempFile(p, "csv");
drh7a431002020-04-18 14:12:00 +00008394 ShellClearFlag(p, SHFLG_Echo);
drh13c20932018-01-10 21:41:55 +00008395 p->mode = MODE_Csv;
8396 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
8397 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
8398 }else{
drh7a431002020-04-18 14:12:00 +00008399 /* text editor mode */
drh13c20932018-01-10 21:41:55 +00008400 newTempFile(p, "txt");
drha92a01a2018-01-10 22:15:37 +00008401 bTxtMode = 1;
drh13c20932018-01-10 21:41:55 +00008402 }
8403 zFile = p->zTempFile;
8404 }
drh04a28c32018-01-31 01:38:44 +00008405#endif /* SQLITE_NOHAVE_SYSTEM */
drh2ce15c32017-07-11 13:34:40 +00008406 if( zFile[0]=='|' ){
8407#ifdef SQLITE_OMIT_POPEN
8408 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
8409 rc = 1;
8410 p->out = stdout;
8411#else
8412 p->out = popen(zFile + 1, "w");
8413 if( p->out==0 ){
8414 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
8415 p->out = stdout;
8416 rc = 1;
8417 }else{
drh7a431002020-04-18 14:12:00 +00008418 if( bBOM ) fprintf(p->out,"\357\273\277");
drh2ce15c32017-07-11 13:34:40 +00008419 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8420 }
8421#endif
8422 }else{
drha92a01a2018-01-10 22:15:37 +00008423 p->out = output_file_open(zFile, bTxtMode);
drh2ce15c32017-07-11 13:34:40 +00008424 if( p->out==0 ){
8425 if( strcmp(zFile,"off")!=0 ){
8426 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
8427 }
8428 p->out = stdout;
8429 rc = 1;
8430 } else {
drh7a431002020-04-18 14:12:00 +00008431 if( bBOM ) fprintf(p->out,"\357\273\277");
drh2ce15c32017-07-11 13:34:40 +00008432 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
8433 }
8434 }
8435 }else
8436
drh9cb02642019-02-28 20:10:52 +00008437 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
8438 open_db(p,0);
8439 if( nArg<=1 ) goto parameter_syntax_error;
8440
8441 /* .parameter clear
8442 ** Clear all bind parameters by dropping the TEMP table that holds them.
8443 */
8444 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
drh65c29fd2019-03-25 21:56:26 +00008445 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
drh9cb02642019-02-28 20:10:52 +00008446 0, 0, 0);
8447 }else
8448
8449 /* .parameter list
8450 ** List all bind parameters.
8451 */
8452 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
8453 sqlite3_stmt *pStmt = 0;
8454 int rx;
8455 int len = 0;
8456 rx = sqlite3_prepare_v2(p->db,
8457 "SELECT max(length(key)) "
drh65c29fd2019-03-25 21:56:26 +00008458 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
drh9cb02642019-02-28 20:10:52 +00008459 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
8460 len = sqlite3_column_int(pStmt, 0);
8461 if( len>40 ) len = 40;
8462 }
8463 sqlite3_finalize(pStmt);
8464 pStmt = 0;
8465 if( len ){
8466 rx = sqlite3_prepare_v2(p->db,
8467 "SELECT key, quote(value) "
drh65c29fd2019-03-25 21:56:26 +00008468 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
drh9cb02642019-02-28 20:10:52 +00008469 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8470 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
8471 sqlite3_column_text(pStmt,1));
8472 }
8473 sqlite3_finalize(pStmt);
8474 }
8475 }else
8476
8477 /* .parameter init
8478 ** Make sure the TEMP table used to hold bind parameters exists.
8479 ** Create it if necessary.
8480 */
8481 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
8482 bind_table_init(p);
8483 }else
8484
8485 /* .parameter set NAME VALUE
8486 ** Set or reset a bind parameter. NAME should be the full parameter
8487 ** name exactly as it appears in the query. (ex: $abc, @def). The
8488 ** VALUE can be in either SQL literal notation, or if not it will be
8489 ** understood to be a text string.
8490 */
8491 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
8492 int rx;
8493 char *zSql;
8494 sqlite3_stmt *pStmt;
8495 const char *zKey = azArg[2];
8496 const char *zValue = azArg[3];
8497 bind_table_init(p);
8498 zSql = sqlite3_mprintf(
drh65c29fd2019-03-25 21:56:26 +00008499 "REPLACE INTO temp.sqlite_parameters(key,value)"
drh9cb02642019-02-28 20:10:52 +00008500 "VALUES(%Q,%s);", zKey, zValue);
8501 if( zSql==0 ) shell_out_of_memory();
8502 pStmt = 0;
8503 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8504 sqlite3_free(zSql);
8505 if( rx!=SQLITE_OK ){
8506 sqlite3_finalize(pStmt);
8507 pStmt = 0;
8508 zSql = sqlite3_mprintf(
drh65c29fd2019-03-25 21:56:26 +00008509 "REPLACE INTO temp.sqlite_parameters(key,value)"
drh9cb02642019-02-28 20:10:52 +00008510 "VALUES(%Q,%Q);", zKey, zValue);
8511 if( zSql==0 ) shell_out_of_memory();
8512 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
8513 sqlite3_free(zSql);
8514 if( rx!=SQLITE_OK ){
8515 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
8516 sqlite3_finalize(pStmt);
8517 pStmt = 0;
8518 rc = 1;
8519 }
8520 }
8521 sqlite3_step(pStmt);
8522 sqlite3_finalize(pStmt);
8523 }else
8524
8525 /* .parameter unset NAME
8526 ** Remove the NAME binding from the parameter binding table, if it
8527 ** exists.
8528 */
8529 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
8530 char *zSql = sqlite3_mprintf(
drh65c29fd2019-03-25 21:56:26 +00008531 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
drh9cb02642019-02-28 20:10:52 +00008532 if( zSql==0 ) shell_out_of_memory();
8533 sqlite3_exec(p->db, zSql, 0, 0, 0);
8534 sqlite3_free(zSql);
8535 }else
8536 /* If no command name matches, show a syntax error */
8537 parameter_syntax_error:
8538 showHelp(p->out, "parameter");
8539 }else
8540
drh2ce15c32017-07-11 13:34:40 +00008541 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
8542 int i;
8543 for(i=1; i<nArg; i++){
8544 if( i>1 ) raw_printf(p->out, " ");
8545 utf8_printf(p->out, "%s", azArg[i]);
8546 }
8547 raw_printf(p->out, "\n");
8548 }else
8549
drh569b1d92019-02-05 20:51:41 +00008550#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
drh3f83f592019-02-04 14:53:18 +00008551 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
8552 int i;
drhfc4eeef2019-02-05 19:48:46 +00008553 int nn = 0;
drh3f83f592019-02-04 14:53:18 +00008554 p->flgProgress = 0;
8555 p->mxProgress = 0;
8556 p->nProgress = 0;
8557 for(i=1; i<nArg; i++){
8558 const char *z = azArg[i];
8559 if( z[0]=='-' ){
8560 z++;
8561 if( z[0]=='-' ) z++;
8562 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
drhfc4eeef2019-02-05 19:48:46 +00008563 p->flgProgress |= SHELL_PROGRESS_QUIET;
drh3f83f592019-02-04 14:53:18 +00008564 continue;
8565 }
8566 if( strcmp(z,"reset")==0 ){
drhfc4eeef2019-02-05 19:48:46 +00008567 p->flgProgress |= SHELL_PROGRESS_RESET;
drh3f83f592019-02-04 14:53:18 +00008568 continue;
8569 }
8570 if( strcmp(z,"once")==0 ){
drhfc4eeef2019-02-05 19:48:46 +00008571 p->flgProgress |= SHELL_PROGRESS_ONCE;
drh3f83f592019-02-04 14:53:18 +00008572 continue;
8573 }
8574 if( strcmp(z,"limit")==0 ){
8575 if( i+1>=nArg ){
8576 utf8_printf(stderr, "Error: missing argument on --limit\n");
8577 rc = 1;
8578 goto meta_command_exit;
8579 }else{
8580 p->mxProgress = (int)integerValue(azArg[++i]);
8581 }
8582 continue;
8583 }
8584 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
8585 rc = 1;
8586 goto meta_command_exit;
8587 }else{
drhfc4eeef2019-02-05 19:48:46 +00008588 nn = (int)integerValue(z);
drh3f83f592019-02-04 14:53:18 +00008589 }
8590 }
8591 open_db(p, 0);
drhfc4eeef2019-02-05 19:48:46 +00008592 sqlite3_progress_handler(p->db, nn, progress_handler, p);
drh3f83f592019-02-04 14:53:18 +00008593 }else
drh569b1d92019-02-05 20:51:41 +00008594#endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
drh3f83f592019-02-04 14:53:18 +00008595
drh2ce15c32017-07-11 13:34:40 +00008596 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
8597 if( nArg >= 2) {
8598 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
8599 }
8600 if( nArg >= 3) {
8601 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
8602 }
8603 }else
8604
8605 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
8606 rc = 2;
8607 }else
8608
8609 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
drh60379d42018-12-13 18:30:01 +00008610 FILE *inSaved = p->in;
drh2c8ee022018-12-13 18:59:30 +00008611 int savedLineno = p->lineno;
drh2ce15c32017-07-11 13:34:40 +00008612 if( nArg!=2 ){
8613 raw_printf(stderr, "Usage: .read FILE\n");
8614 rc = 1;
8615 goto meta_command_exit;
8616 }
drh60379d42018-12-13 18:30:01 +00008617 p->in = fopen(azArg[1], "rb");
8618 if( p->in==0 ){
drh2ce15c32017-07-11 13:34:40 +00008619 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
8620 rc = 1;
8621 }else{
drh60379d42018-12-13 18:30:01 +00008622 rc = process_input(p);
8623 fclose(p->in);
drh2ce15c32017-07-11 13:34:40 +00008624 }
drh60379d42018-12-13 18:30:01 +00008625 p->in = inSaved;
drh2c8ee022018-12-13 18:59:30 +00008626 p->lineno = savedLineno;
drh2ce15c32017-07-11 13:34:40 +00008627 }else
8628
8629 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
8630 const char *zSrcFile;
8631 const char *zDb;
8632 sqlite3 *pSrc;
8633 sqlite3_backup *pBackup;
8634 int nTimeout = 0;
8635
8636 if( nArg==2 ){
8637 zSrcFile = azArg[1];
8638 zDb = "main";
8639 }else if( nArg==3 ){
8640 zSrcFile = azArg[2];
8641 zDb = azArg[1];
8642 }else{
8643 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
8644 rc = 1;
8645 goto meta_command_exit;
8646 }
8647 rc = sqlite3_open(zSrcFile, &pSrc);
8648 if( rc!=SQLITE_OK ){
8649 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
drh9e804032018-05-18 17:11:50 +00008650 close_db(pSrc);
drh2ce15c32017-07-11 13:34:40 +00008651 return 1;
8652 }
8653 open_db(p, 0);
8654 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
8655 if( pBackup==0 ){
8656 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
drh9e804032018-05-18 17:11:50 +00008657 close_db(pSrc);
drh2ce15c32017-07-11 13:34:40 +00008658 return 1;
8659 }
8660 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
8661 || rc==SQLITE_BUSY ){
8662 if( rc==SQLITE_BUSY ){
8663 if( nTimeout++ >= 3 ) break;
8664 sqlite3_sleep(100);
8665 }
8666 }
8667 sqlite3_backup_finish(pBackup);
8668 if( rc==SQLITE_DONE ){
8669 rc = 0;
8670 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
8671 raw_printf(stderr, "Error: source database is busy\n");
8672 rc = 1;
8673 }else{
8674 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8675 rc = 1;
8676 }
drh9e804032018-05-18 17:11:50 +00008677 close_db(pSrc);
drh2ce15c32017-07-11 13:34:40 +00008678 }else
8679
drh2ce15c32017-07-11 13:34:40 +00008680 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
8681 if( nArg==2 ){
mistachkinb71aa092018-01-23 00:05:18 +00008682 p->scanstatsOn = (u8)booleanValue(azArg[1]);
drh2ce15c32017-07-11 13:34:40 +00008683#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
8684 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
8685#endif
8686 }else{
8687 raw_printf(stderr, "Usage: .scanstats on|off\n");
8688 rc = 1;
8689 }
8690 }else
8691
8692 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
8693 ShellText sSelect;
8694 ShellState data;
8695 char *zErrMsg = 0;
drh667a2a22018-01-02 00:04:37 +00008696 const char *zDiv = "(";
drhceba7922018-01-01 21:28:25 +00008697 const char *zName = 0;
drh2ce15c32017-07-11 13:34:40 +00008698 int iSchema = 0;
drhceba7922018-01-01 21:28:25 +00008699 int bDebug = 0;
8700 int ii;
drh2ce15c32017-07-11 13:34:40 +00008701
8702 open_db(p, 0);
8703 memcpy(&data, p, sizeof(data));
8704 data.showHeader = 0;
8705 data.cMode = data.mode = MODE_Semi;
8706 initText(&sSelect);
drhceba7922018-01-01 21:28:25 +00008707 for(ii=1; ii<nArg; ii++){
8708 if( optionMatch(azArg[ii],"indent") ){
8709 data.cMode = data.mode = MODE_Pretty;
8710 }else if( optionMatch(azArg[ii],"debug") ){
8711 bDebug = 1;
8712 }else if( zName==0 ){
8713 zName = azArg[ii];
drh2ce15c32017-07-11 13:34:40 +00008714 }else{
drhceba7922018-01-01 21:28:25 +00008715 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
8716 rc = 1;
8717 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00008718 }
drh2ce15c32017-07-11 13:34:40 +00008719 }
drhceba7922018-01-01 21:28:25 +00008720 if( zName!=0 ){
mistachkin9d107262018-03-23 14:24:34 +00008721 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
8722 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
drh2ce15c32017-07-11 13:34:40 +00008723 char *new_argv[2], *new_colv[2];
drhc22b7162018-01-01 20:11:23 +00008724 new_argv[0] = sqlite3_mprintf(
8725 "CREATE TABLE %s (\n"
drh2ce15c32017-07-11 13:34:40 +00008726 " type text,\n"
8727 " name text,\n"
8728 " tbl_name text,\n"
8729 " rootpage integer,\n"
8730 " sql text\n"
drh667a2a22018-01-02 00:04:37 +00008731 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
drh2ce15c32017-07-11 13:34:40 +00008732 new_argv[1] = 0;
8733 new_colv[0] = "sql";
8734 new_colv[1] = 0;
8735 callback(&data, 1, new_argv, new_colv);
drhc22b7162018-01-01 20:11:23 +00008736 sqlite3_free(new_argv[0]);
drh2ce15c32017-07-11 13:34:40 +00008737 }
drh2ce15c32017-07-11 13:34:40 +00008738 }
8739 if( zDiv ){
8740 sqlite3_stmt *pStmt = 0;
8741 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
8742 -1, &pStmt, 0);
8743 if( rc ){
8744 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
8745 sqlite3_finalize(pStmt);
8746 rc = 1;
8747 goto meta_command_exit;
8748 }
8749 appendText(&sSelect, "SELECT sql FROM", 0);
8750 iSchema = 0;
8751 while( sqlite3_step(pStmt)==SQLITE_ROW ){
8752 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
8753 char zScNum[30];
8754 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
8755 appendText(&sSelect, zDiv, 0);
8756 zDiv = " UNION ALL ";
drhceba7922018-01-01 21:28:25 +00008757 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
8758 if( sqlite3_stricmp(zDb, "main")!=0 ){
drhea38f4f2019-07-13 17:21:47 +00008759 appendText(&sSelect, zDb, '\'');
drh2ce15c32017-07-11 13:34:40 +00008760 }else{
drhceba7922018-01-01 21:28:25 +00008761 appendText(&sSelect, "NULL", 0);
drh2ce15c32017-07-11 13:34:40 +00008762 }
drhceba7922018-01-01 21:28:25 +00008763 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
8764 appendText(&sSelect, zScNum, 0);
8765 appendText(&sSelect, " AS snum, ", 0);
8766 appendText(&sSelect, zDb, '\'');
8767 appendText(&sSelect, " AS sname FROM ", 0);
drhea38f4f2019-07-13 17:21:47 +00008768 appendText(&sSelect, zDb, quoteChar(zDb));
drhceba7922018-01-01 21:28:25 +00008769 appendText(&sSelect, ".sqlite_master", 0);
drh2ce15c32017-07-11 13:34:40 +00008770 }
8771 sqlite3_finalize(pStmt);
drhcc3f3d12019-08-17 15:27:58 +00008772#ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
drh667a2a22018-01-02 00:04:37 +00008773 if( zName ){
8774 appendText(&sSelect,
8775 " UNION ALL SELECT shell_module_schema(name),"
drhe2754c12019-08-26 12:50:01 +00008776 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
8777 0);
drh667a2a22018-01-02 00:04:37 +00008778 }
drhcde7b772018-01-02 12:50:40 +00008779#endif
drh2ce15c32017-07-11 13:34:40 +00008780 appendText(&sSelect, ") WHERE ", 0);
drhceba7922018-01-01 21:28:25 +00008781 if( zName ){
8782 char *zQarg = sqlite3_mprintf("%Q", zName);
mistachkin9d107262018-03-23 14:24:34 +00008783 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
8784 strchr(zName, '[') != 0;
drhceba7922018-01-01 21:28:25 +00008785 if( strchr(zName, '.') ){
drh2ce15c32017-07-11 13:34:40 +00008786 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
8787 }else{
8788 appendText(&sSelect, "lower(tbl_name)", 0);
8789 }
mistachkin9d107262018-03-23 14:24:34 +00008790 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
drh2ce15c32017-07-11 13:34:40 +00008791 appendText(&sSelect, zQarg, 0);
mistachkin9d107262018-03-23 14:24:34 +00008792 if( !bGlob ){
8793 appendText(&sSelect, " ESCAPE '\\' ", 0);
8794 }
drh2ce15c32017-07-11 13:34:40 +00008795 appendText(&sSelect, " AND ", 0);
8796 sqlite3_free(zQarg);
8797 }
8798 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
8799 " ORDER BY snum, rowid", 0);
drhceba7922018-01-01 21:28:25 +00008800 if( bDebug ){
8801 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
8802 }else{
8803 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
8804 }
drh2ce15c32017-07-11 13:34:40 +00008805 freeText(&sSelect);
8806 }
8807 if( zErrMsg ){
8808 utf8_printf(stderr,"Error: %s\n", zErrMsg);
8809 sqlite3_free(zErrMsg);
8810 rc = 1;
8811 }else if( rc != SQLITE_OK ){
8812 raw_printf(stderr,"Error: querying schema information\n");
8813 rc = 1;
8814 }else{
8815 rc = 0;
8816 }
8817 }else
8818
8819#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
8820 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
8821 sqlite3SelectTrace = (int)integerValue(azArg[1]);
8822 }else
8823#endif
8824
8825#if defined(SQLITE_ENABLE_SESSION)
8826 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
8827 OpenSession *pSession = &p->aSession[0];
8828 char **azCmd = &azArg[1];
8829 int iSes = 0;
8830 int nCmd = nArg - 1;
8831 int i;
8832 if( nArg<=1 ) goto session_syntax_error;
8833 open_db(p, 0);
8834 if( nArg>=3 ){
8835 for(iSes=0; iSes<p->nSession; iSes++){
8836 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
8837 }
8838 if( iSes<p->nSession ){
8839 pSession = &p->aSession[iSes];
8840 azCmd++;
8841 nCmd--;
8842 }else{
8843 pSession = &p->aSession[0];
8844 iSes = 0;
8845 }
8846 }
8847
8848 /* .session attach TABLE
8849 ** Invoke the sqlite3session_attach() interface to attach a particular
8850 ** table so that it is never filtered.
8851 */
8852 if( strcmp(azCmd[0],"attach")==0 ){
8853 if( nCmd!=2 ) goto session_syntax_error;
8854 if( pSession->p==0 ){
8855 session_not_open:
8856 raw_printf(stderr, "ERROR: No sessions are open\n");
8857 }else{
8858 rc = sqlite3session_attach(pSession->p, azCmd[1]);
8859 if( rc ){
8860 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
8861 rc = 0;
8862 }
8863 }
8864 }else
8865
8866 /* .session changeset FILE
8867 ** .session patchset FILE
8868 ** Write a changeset or patchset into a file. The file is overwritten.
8869 */
8870 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
8871 FILE *out = 0;
8872 if( nCmd!=2 ) goto session_syntax_error;
8873 if( pSession->p==0 ) goto session_not_open;
8874 out = fopen(azCmd[1], "wb");
8875 if( out==0 ){
drhe2754c12019-08-26 12:50:01 +00008876 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
8877 azCmd[1]);
drh2ce15c32017-07-11 13:34:40 +00008878 }else{
8879 int szChng;
8880 void *pChng;
8881 if( azCmd[0][0]=='c' ){
8882 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
8883 }else{
8884 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
8885 }
8886 if( rc ){
8887 printf("Error: error code %d\n", rc);
8888 rc = 0;
8889 }
8890 if( pChng
8891 && fwrite(pChng, szChng, 1, out)!=1 ){
8892 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
8893 szChng);
8894 }
8895 sqlite3_free(pChng);
8896 fclose(out);
8897 }
8898 }else
8899
8900 /* .session close
8901 ** Close the identified session
8902 */
8903 if( strcmp(azCmd[0], "close")==0 ){
8904 if( nCmd!=1 ) goto session_syntax_error;
8905 if( p->nSession ){
8906 session_close(pSession);
8907 p->aSession[iSes] = p->aSession[--p->nSession];
8908 }
8909 }else
8910
8911 /* .session enable ?BOOLEAN?
8912 ** Query or set the enable flag
8913 */
8914 if( strcmp(azCmd[0], "enable")==0 ){
8915 int ii;
8916 if( nCmd>2 ) goto session_syntax_error;
8917 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8918 if( p->nSession ){
8919 ii = sqlite3session_enable(pSession->p, ii);
8920 utf8_printf(p->out, "session %s enable flag = %d\n",
8921 pSession->zName, ii);
8922 }
8923 }else
8924
8925 /* .session filter GLOB ....
8926 ** Set a list of GLOB patterns of table names to be excluded.
8927 */
8928 if( strcmp(azCmd[0], "filter")==0 ){
8929 int ii, nByte;
8930 if( nCmd<2 ) goto session_syntax_error;
8931 if( p->nSession ){
8932 for(ii=0; ii<pSession->nFilter; ii++){
8933 sqlite3_free(pSession->azFilter[ii]);
8934 }
8935 sqlite3_free(pSession->azFilter);
8936 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
8937 pSession->azFilter = sqlite3_malloc( nByte );
8938 if( pSession->azFilter==0 ){
8939 raw_printf(stderr, "Error: out or memory\n");
8940 exit(1);
8941 }
8942 for(ii=1; ii<nCmd; ii++){
8943 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
8944 }
8945 pSession->nFilter = ii-1;
8946 }
8947 }else
8948
8949 /* .session indirect ?BOOLEAN?
8950 ** Query or set the indirect flag
8951 */
8952 if( strcmp(azCmd[0], "indirect")==0 ){
8953 int ii;
8954 if( nCmd>2 ) goto session_syntax_error;
8955 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
8956 if( p->nSession ){
8957 ii = sqlite3session_indirect(pSession->p, ii);
8958 utf8_printf(p->out, "session %s indirect flag = %d\n",
8959 pSession->zName, ii);
8960 }
8961 }else
8962
8963 /* .session isempty
8964 ** Determine if the session is empty
8965 */
8966 if( strcmp(azCmd[0], "isempty")==0 ){
8967 int ii;
8968 if( nCmd!=1 ) goto session_syntax_error;
8969 if( p->nSession ){
8970 ii = sqlite3session_isempty(pSession->p);
8971 utf8_printf(p->out, "session %s isempty flag = %d\n",
8972 pSession->zName, ii);
8973 }
8974 }else
8975
8976 /* .session list
8977 ** List all currently open sessions
8978 */
8979 if( strcmp(azCmd[0],"list")==0 ){
8980 for(i=0; i<p->nSession; i++){
8981 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
8982 }
8983 }else
8984
8985 /* .session open DB NAME
8986 ** Open a new session called NAME on the attached database DB.
8987 ** DB is normally "main".
8988 */
8989 if( strcmp(azCmd[0],"open")==0 ){
8990 char *zName;
8991 if( nCmd!=3 ) goto session_syntax_error;
8992 zName = azCmd[2];
8993 if( zName[0]==0 ) goto session_syntax_error;
8994 for(i=0; i<p->nSession; i++){
8995 if( strcmp(p->aSession[i].zName,zName)==0 ){
8996 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
8997 goto meta_command_exit;
8998 }
8999 }
9000 if( p->nSession>=ArraySize(p->aSession) ){
9001 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
9002 goto meta_command_exit;
9003 }
9004 pSession = &p->aSession[p->nSession];
9005 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
9006 if( rc ){
9007 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
9008 rc = 0;
9009 goto meta_command_exit;
9010 }
9011 pSession->nFilter = 0;
9012 sqlite3session_table_filter(pSession->p, session_filter, pSession);
9013 p->nSession++;
9014 pSession->zName = sqlite3_mprintf("%s", zName);
9015 }else
9016 /* If no command name matches, show a syntax error */
9017 session_syntax_error:
drheb7f2a02018-09-26 18:02:32 +00009018 showHelp(p->out, "session");
drh2ce15c32017-07-11 13:34:40 +00009019 }else
9020#endif
9021
9022#ifdef SQLITE_DEBUG
9023 /* Undocumented commands for internal testing. Subject to change
9024 ** without notice. */
9025 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
9026 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
9027 int i, v;
9028 for(i=1; i<nArg; i++){
9029 v = booleanValue(azArg[i]);
9030 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
9031 }
9032 }
9033 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
9034 int i; sqlite3_int64 v;
9035 for(i=1; i<nArg; i++){
9036 char zBuf[200];
9037 v = integerValue(azArg[i]);
9038 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
9039 utf8_printf(p->out, "%s", zBuf);
9040 }
9041 }
9042 }else
9043#endif
9044
9045 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
9046 int bIsInit = 0; /* True to initialize the SELFTEST table */
9047 int bVerbose = 0; /* Verbose output */
9048 int bSelftestExists; /* True if SELFTEST already exists */
9049 int i, k; /* Loop counters */
9050 int nTest = 0; /* Number of tests runs */
9051 int nErr = 0; /* Number of errors seen */
9052 ShellText str; /* Answer for a query */
9053 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
9054
9055 open_db(p,0);
9056 for(i=1; i<nArg; i++){
9057 const char *z = azArg[i];
9058 if( z[0]=='-' && z[1]=='-' ) z++;
9059 if( strcmp(z,"-init")==0 ){
9060 bIsInit = 1;
9061 }else
9062 if( strcmp(z,"-v")==0 ){
9063 bVerbose++;
9064 }else
9065 {
9066 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9067 azArg[i], azArg[0]);
9068 raw_printf(stderr, "Should be one of: --init -v\n");
9069 rc = 1;
9070 goto meta_command_exit;
9071 }
9072 }
9073 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
9074 != SQLITE_OK ){
9075 bSelftestExists = 0;
9076 }else{
9077 bSelftestExists = 1;
9078 }
9079 if( bIsInit ){
9080 createSelftestTable(p);
9081 bSelftestExists = 1;
9082 }
9083 initText(&str);
9084 appendText(&str, "x", 0);
9085 for(k=bSelftestExists; k>=0; k--){
9086 if( k==1 ){
9087 rc = sqlite3_prepare_v2(p->db,
9088 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
9089 -1, &pStmt, 0);
9090 }else{
9091 rc = sqlite3_prepare_v2(p->db,
9092 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
9093 " (1,'run','PRAGMA integrity_check','ok')",
9094 -1, &pStmt, 0);
9095 }
9096 if( rc ){
9097 raw_printf(stderr, "Error querying the selftest table\n");
9098 rc = 1;
9099 sqlite3_finalize(pStmt);
9100 goto meta_command_exit;
9101 }
9102 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
9103 int tno = sqlite3_column_int(pStmt, 0);
9104 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
9105 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
9106 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
9107
9108 k = 0;
9109 if( bVerbose>0 ){
9110 char *zQuote = sqlite3_mprintf("%q", zSql);
9111 printf("%d: %s %s\n", tno, zOp, zSql);
9112 sqlite3_free(zQuote);
9113 }
9114 if( strcmp(zOp,"memo")==0 ){
9115 utf8_printf(p->out, "%s\n", zSql);
9116 }else
9117 if( strcmp(zOp,"run")==0 ){
9118 char *zErrMsg = 0;
9119 str.n = 0;
9120 str.z[0] = 0;
9121 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
9122 nTest++;
9123 if( bVerbose ){
9124 utf8_printf(p->out, "Result: %s\n", str.z);
9125 }
9126 if( rc || zErrMsg ){
9127 nErr++;
9128 rc = 1;
9129 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
9130 sqlite3_free(zErrMsg);
9131 }else if( strcmp(zAns,str.z)!=0 ){
9132 nErr++;
9133 rc = 1;
9134 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
9135 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
9136 }
9137 }else
9138 {
9139 utf8_printf(stderr,
9140 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
9141 rc = 1;
9142 break;
9143 }
9144 } /* End loop over rows of content from SELFTEST */
9145 sqlite3_finalize(pStmt);
9146 } /* End loop over k */
9147 freeText(&str);
9148 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
9149 }else
9150
9151 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
9152 if( nArg<2 || nArg>3 ){
9153 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
9154 rc = 1;
9155 }
9156 if( nArg>=2 ){
9157 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
9158 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
9159 }
9160 if( nArg>=3 ){
9161 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
9162 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
9163 }
9164 }else
9165
9166 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
9167 const char *zLike = 0; /* Which table to checksum. 0 means everything */
9168 int i; /* Loop counter */
9169 int bSchema = 0; /* Also hash the schema */
9170 int bSeparate = 0; /* Hash each table separately */
9171 int iSize = 224; /* Hash algorithm to use */
9172 int bDebug = 0; /* Only show the query that would have run */
9173 sqlite3_stmt *pStmt; /* For querying tables names */
9174 char *zSql; /* SQL to be run */
9175 char *zSep; /* Separator */
9176 ShellText sSql; /* Complete SQL for the query to run the hash */
9177 ShellText sQuery; /* Set of queries used to read all content */
9178 open_db(p, 0);
9179 for(i=1; i<nArg; i++){
9180 const char *z = azArg[i];
9181 if( z[0]=='-' ){
9182 z++;
9183 if( z[0]=='-' ) z++;
9184 if( strcmp(z,"schema")==0 ){
9185 bSchema = 1;
9186 }else
9187 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
9188 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
9189 ){
9190 iSize = atoi(&z[5]);
9191 }else
9192 if( strcmp(z,"debug")==0 ){
9193 bDebug = 1;
9194 }else
9195 {
9196 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
9197 azArg[i], azArg[0]);
drhe2754c12019-08-26 12:50:01 +00009198 showHelp(p->out, azArg[0]);
drh2ce15c32017-07-11 13:34:40 +00009199 rc = 1;
9200 goto meta_command_exit;
9201 }
9202 }else if( zLike ){
9203 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
9204 rc = 1;
9205 goto meta_command_exit;
9206 }else{
9207 zLike = z;
9208 bSeparate = 1;
drhcedfecf2018-03-23 12:59:10 +00009209 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
drh2ce15c32017-07-11 13:34:40 +00009210 }
9211 }
9212 if( bSchema ){
9213 zSql = "SELECT lower(name) FROM sqlite_master"
9214 " WHERE type='table' AND coalesce(rootpage,0)>1"
9215 " UNION ALL SELECT 'sqlite_master'"
9216 " ORDER BY 1 collate nocase";
9217 }else{
9218 zSql = "SELECT lower(name) FROM sqlite_master"
9219 " WHERE type='table' AND coalesce(rootpage,0)>1"
9220 " AND name NOT LIKE 'sqlite_%'"
9221 " ORDER BY 1 collate nocase";
9222 }
9223 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9224 initText(&sQuery);
9225 initText(&sSql);
9226 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
9227 zSep = "VALUES(";
9228 while( SQLITE_ROW==sqlite3_step(pStmt) ){
9229 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
9230 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
9231 if( strncmp(zTab, "sqlite_",7)!=0 ){
9232 appendText(&sQuery,"SELECT * FROM ", 0);
9233 appendText(&sQuery,zTab,'"');
9234 appendText(&sQuery," NOT INDEXED;", 0);
9235 }else if( strcmp(zTab, "sqlite_master")==0 ){
9236 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
9237 " ORDER BY name;", 0);
9238 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
9239 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
9240 " ORDER BY name;", 0);
9241 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
9242 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
9243 " ORDER BY tbl,idx;", 0);
drh175b8f02019-08-08 15:24:17 +00009244 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
drh2ce15c32017-07-11 13:34:40 +00009245 appendText(&sQuery, "SELECT * FROM ", 0);
9246 appendText(&sQuery, zTab, 0);
9247 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
9248 }
9249 appendText(&sSql, zSep, 0);
9250 appendText(&sSql, sQuery.z, '\'');
9251 sQuery.n = 0;
9252 appendText(&sSql, ",", 0);
9253 appendText(&sSql, zTab, '\'');
9254 zSep = "),(";
9255 }
9256 sqlite3_finalize(pStmt);
9257 if( bSeparate ){
9258 zSql = sqlite3_mprintf(
9259 "%s))"
9260 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
9261 " FROM [sha3sum$query]",
9262 sSql.z, iSize);
9263 }else{
9264 zSql = sqlite3_mprintf(
9265 "%s))"
9266 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
9267 " FROM [sha3sum$query]",
9268 sSql.z, iSize);
9269 }
9270 freeText(&sQuery);
9271 freeText(&sSql);
9272 if( bDebug ){
9273 utf8_printf(p->out, "%s\n", zSql);
9274 }else{
drha10b9992018-03-09 15:24:33 +00009275 shell_exec(p, zSql, 0);
drh2ce15c32017-07-11 13:34:40 +00009276 }
9277 sqlite3_free(zSql);
9278 }else
9279
drh04a28c32018-01-31 01:38:44 +00009280#ifndef SQLITE_NOHAVE_SYSTEM
drh2ce15c32017-07-11 13:34:40 +00009281 if( c=='s'
9282 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
9283 ){
9284 char *zCmd;
9285 int i, x;
9286 if( nArg<2 ){
9287 raw_printf(stderr, "Usage: .system COMMAND\n");
9288 rc = 1;
9289 goto meta_command_exit;
9290 }
9291 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
9292 for(i=2; i<nArg; i++){
9293 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
9294 zCmd, azArg[i]);
9295 }
9296 x = system(zCmd);
9297 sqlite3_free(zCmd);
9298 if( x ) raw_printf(stderr, "System command returns %d\n", x);
9299 }else
drh04a28c32018-01-31 01:38:44 +00009300#endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
drh2ce15c32017-07-11 13:34:40 +00009301
9302 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
drhada70452017-12-21 21:02:27 +00009303 static const char *azBool[] = { "off", "on", "trigger", "full"};
drh2ce15c32017-07-11 13:34:40 +00009304 int i;
9305 if( nArg!=1 ){
9306 raw_printf(stderr, "Usage: .show\n");
9307 rc = 1;
9308 goto meta_command_exit;
9309 }
9310 utf8_printf(p->out, "%12.12s: %s\n","echo",
9311 azBool[ShellHasFlag(p, SHFLG_Echo)]);
9312 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
9313 utf8_printf(p->out, "%12.12s: %s\n","explain",
9314 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
9315 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
9316 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
9317 utf8_printf(p->out, "%12.12s: ", "nullvalue");
9318 output_c_string(p->out, p->nullValue);
9319 raw_printf(p->out, "\n");
9320 utf8_printf(p->out,"%12.12s: %s\n","output",
9321 strlen30(p->outfile) ? p->outfile : "stdout");
9322 utf8_printf(p->out,"%12.12s: ", "colseparator");
9323 output_c_string(p->out, p->colSeparator);
9324 raw_printf(p->out, "\n");
9325 utf8_printf(p->out,"%12.12s: ", "rowseparator");
9326 output_c_string(p->out, p->rowSeparator);
9327 raw_printf(p->out, "\n");
9328 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
9329 utf8_printf(p->out, "%12.12s: ", "width");
9330 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
9331 raw_printf(p->out, "%d ", p->colWidth[i]);
9332 }
9333 raw_printf(p->out, "\n");
9334 utf8_printf(p->out, "%12.12s: %s\n", "filename",
9335 p->zDbFilename ? p->zDbFilename : "");
9336 }else
9337
9338 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
9339 if( nArg==2 ){
mistachkinb71aa092018-01-23 00:05:18 +00009340 p->statsOn = (u8)booleanValue(azArg[1]);
drh2ce15c32017-07-11 13:34:40 +00009341 }else if( nArg==1 ){
9342 display_stats(p->db, p, 0);
9343 }else{
9344 raw_printf(stderr, "Usage: .stats ?on|off?\n");
9345 rc = 1;
9346 }
9347 }else
9348
9349 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
9350 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
9351 || strncmp(azArg[0], "indexes", n)==0) )
9352 ){
9353 sqlite3_stmt *pStmt;
9354 char **azResult;
9355 int nRow, nAlloc;
9356 int ii;
9357 ShellText s;
9358 initText(&s);
9359 open_db(p, 0);
9360 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
drh9e804032018-05-18 17:11:50 +00009361 if( rc ){
9362 sqlite3_finalize(pStmt);
9363 return shellDatabaseError(p->db);
9364 }
drh2ce15c32017-07-11 13:34:40 +00009365
9366 if( nArg>2 && c=='i' ){
9367 /* It is an historical accident that the .indexes command shows an error
9368 ** when called with the wrong number of arguments whereas the .tables
9369 ** command does not. */
9370 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
9371 rc = 1;
drh9e804032018-05-18 17:11:50 +00009372 sqlite3_finalize(pStmt);
drh2ce15c32017-07-11 13:34:40 +00009373 goto meta_command_exit;
9374 }
9375 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
9376 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
9377 if( zDbName==0 ) continue;
9378 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
9379 if( sqlite3_stricmp(zDbName, "main")==0 ){
9380 appendText(&s, "SELECT name FROM ", 0);
9381 }else{
9382 appendText(&s, "SELECT ", 0);
9383 appendText(&s, zDbName, '\'');
9384 appendText(&s, "||'.'||name FROM ", 0);
9385 }
9386 appendText(&s, zDbName, '"');
9387 appendText(&s, ".sqlite_master ", 0);
9388 if( c=='t' ){
9389 appendText(&s," WHERE type IN ('table','view')"
9390 " AND name NOT LIKE 'sqlite_%'"
9391 " AND name LIKE ?1", 0);
9392 }else{
9393 appendText(&s," WHERE type='index'"
9394 " AND tbl_name LIKE ?1", 0);
9395 }
9396 }
9397 rc = sqlite3_finalize(pStmt);
9398 appendText(&s, " ORDER BY 1", 0);
9399 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
9400 freeText(&s);
9401 if( rc ) return shellDatabaseError(p->db);
9402
9403 /* Run the SQL statement prepared by the above block. Store the results
9404 ** as an array of nul-terminated strings in azResult[]. */
9405 nRow = nAlloc = 0;
9406 azResult = 0;
9407 if( nArg>1 ){
9408 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
9409 }else{
9410 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
9411 }
9412 while( sqlite3_step(pStmt)==SQLITE_ROW ){
9413 if( nRow>=nAlloc ){
9414 char **azNew;
9415 int n2 = nAlloc*2 + 10;
9416 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
drh4b5345c2018-04-24 13:07:40 +00009417 if( azNew==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00009418 nAlloc = n2;
9419 azResult = azNew;
9420 }
9421 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
drh4b5345c2018-04-24 13:07:40 +00009422 if( 0==azResult[nRow] ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +00009423 nRow++;
9424 }
9425 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
9426 rc = shellDatabaseError(p->db);
9427 }
9428
9429 /* Pretty-print the contents of array azResult[] to the output */
9430 if( rc==0 && nRow>0 ){
9431 int len, maxlen = 0;
9432 int i, j;
9433 int nPrintCol, nPrintRow;
9434 for(i=0; i<nRow; i++){
9435 len = strlen30(azResult[i]);
9436 if( len>maxlen ) maxlen = len;
9437 }
9438 nPrintCol = 80/(maxlen+2);
9439 if( nPrintCol<1 ) nPrintCol = 1;
9440 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
9441 for(i=0; i<nPrintRow; i++){
9442 for(j=i; j<nRow; j+=nPrintRow){
9443 char *zSp = j<nPrintRow ? "" : " ";
9444 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
9445 azResult[j] ? azResult[j]:"");
9446 }
9447 raw_printf(p->out, "\n");
9448 }
9449 }
9450
9451 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
9452 sqlite3_free(azResult);
9453 }else
9454
9455 /* Begin redirecting output to the file "testcase-out.txt" */
9456 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
9457 output_reset(p);
drha92a01a2018-01-10 22:15:37 +00009458 p->out = output_file_open("testcase-out.txt", 0);
drh2ce15c32017-07-11 13:34:40 +00009459 if( p->out==0 ){
9460 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
9461 }
9462 if( nArg>=2 ){
9463 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
9464 }else{
9465 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
9466 }
9467 }else
9468
9469#ifndef SQLITE_UNTESTABLE
drh35f51a42017-11-15 17:07:22 +00009470 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
drh2ce15c32017-07-11 13:34:40 +00009471 static const struct {
9472 const char *zCtrlName; /* Name of a test-control option */
9473 int ctrlCode; /* Integer code for that option */
drhef302e82017-11-15 19:14:08 +00009474 const char *zUsage; /* Usage notes */
drh2ce15c32017-07-11 13:34:40 +00009475 } aCtrl[] = {
drhe2754c12019-08-26 12:50:01 +00009476 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
9477 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
9478 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
9479 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
9480 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
9481 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
9482 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
9483 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
drh171c50e2020-01-01 15:43:30 +00009484 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
drhe2754c12019-08-26 12:50:01 +00009485 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
9486 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
9487 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
drh0d9de992017-12-26 18:04:23 +00009488#ifdef YYCOVERAGE
drhe2754c12019-08-26 12:50:01 +00009489 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
drh0d9de992017-12-26 18:04:23 +00009490#endif
drhe2754c12019-08-26 12:50:01 +00009491 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
9492 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
9493 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
9494 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
drh2ce15c32017-07-11 13:34:40 +00009495 };
9496 int testctrl = -1;
drhef302e82017-11-15 19:14:08 +00009497 int iCtrl = -1;
9498 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
9499 int isOk = 0;
drh2ce15c32017-07-11 13:34:40 +00009500 int i, n2;
mistachkinc6bc15a2017-11-21 21:14:32 +00009501 const char *zCmd = 0;
9502
drh2ce15c32017-07-11 13:34:40 +00009503 open_db(p, 0);
mistachkinc6bc15a2017-11-21 21:14:32 +00009504 zCmd = nArg>=2 ? azArg[1] : "help";
drh35f51a42017-11-15 17:07:22 +00009505
9506 /* The argument can optionally begin with "-" or "--" */
9507 if( zCmd[0]=='-' && zCmd[1] ){
9508 zCmd++;
9509 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
9510 }
9511
9512 /* --help lists all test-controls */
9513 if( strcmp(zCmd,"help")==0 ){
9514 utf8_printf(p->out, "Available test-controls:\n");
9515 for(i=0; i<ArraySize(aCtrl); i++){
drhef302e82017-11-15 19:14:08 +00009516 utf8_printf(p->out, " .testctrl %s %s\n",
9517 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
drh35f51a42017-11-15 17:07:22 +00009518 }
9519 rc = 1;
9520 goto meta_command_exit;
9521 }
drh2ce15c32017-07-11 13:34:40 +00009522
9523 /* convert testctrl text option to value. allow any unique prefix
9524 ** of the option name, or a numerical value. */
drh35f51a42017-11-15 17:07:22 +00009525 n2 = strlen30(zCmd);
drh2ce15c32017-07-11 13:34:40 +00009526 for(i=0; i<ArraySize(aCtrl); i++){
drh35f51a42017-11-15 17:07:22 +00009527 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
drh2ce15c32017-07-11 13:34:40 +00009528 if( testctrl<0 ){
9529 testctrl = aCtrl[i].ctrlCode;
drhef302e82017-11-15 19:14:08 +00009530 iCtrl = i;
drh2ce15c32017-07-11 13:34:40 +00009531 }else{
drh35f51a42017-11-15 17:07:22 +00009532 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
9533 "Use \".testctrl --help\" for help\n", zCmd);
9534 rc = 1;
9535 goto meta_command_exit;
drh2ce15c32017-07-11 13:34:40 +00009536 }
9537 }
9538 }
drhef302e82017-11-15 19:14:08 +00009539 if( testctrl<0 ){
drh35f51a42017-11-15 17:07:22 +00009540 utf8_printf(stderr,"Error: unknown test-control: %s\n"
9541 "Use \".testctrl --help\" for help\n", zCmd);
drh2ce15c32017-07-11 13:34:40 +00009542 }else{
9543 switch(testctrl){
9544
9545 /* sqlite3_test_control(int, db, int) */
9546 case SQLITE_TESTCTRL_OPTIMIZATIONS:
drh2ce15c32017-07-11 13:34:40 +00009547 if( nArg==3 ){
9548 int opt = (int)strtol(azArg[2], 0, 0);
9549 rc2 = sqlite3_test_control(testctrl, p->db, opt);
drhef302e82017-11-15 19:14:08 +00009550 isOk = 3;
drh2ce15c32017-07-11 13:34:40 +00009551 }
9552 break;
9553
9554 /* sqlite3_test_control(int) */
9555 case SQLITE_TESTCTRL_PRNG_SAVE:
9556 case SQLITE_TESTCTRL_PRNG_RESTORE:
9557 case SQLITE_TESTCTRL_PRNG_RESET:
9558 case SQLITE_TESTCTRL_BYTEORDER:
9559 if( nArg==2 ){
9560 rc2 = sqlite3_test_control(testctrl);
drhef302e82017-11-15 19:14:08 +00009561 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
drh2ce15c32017-07-11 13:34:40 +00009562 }
9563 break;
9564
9565 /* sqlite3_test_control(int, uint) */
9566 case SQLITE_TESTCTRL_PENDING_BYTE:
9567 if( nArg==3 ){
9568 unsigned int opt = (unsigned int)integerValue(azArg[2]);
9569 rc2 = sqlite3_test_control(testctrl, opt);
drhef302e82017-11-15 19:14:08 +00009570 isOk = 3;
drh2ce15c32017-07-11 13:34:40 +00009571 }
9572 break;
9573
drh2e6d83b2019-08-03 01:39:20 +00009574 /* sqlite3_test_control(int, int, sqlite3*) */
9575 case SQLITE_TESTCTRL_PRNG_SEED:
9576 if( nArg==3 || nArg==4 ){
drh51755a72019-08-08 19:40:29 +00009577 int ii = (int)integerValue(azArg[2]);
drh2e6d83b2019-08-03 01:39:20 +00009578 sqlite3 *db;
drh41428a92019-08-12 16:25:11 +00009579 if( ii==0 && strcmp(azArg[2],"random")==0 ){
9580 sqlite3_randomness(sizeof(ii),&ii);
9581 printf("-- random seed: %d\n", ii);
9582 }
drh2e6d83b2019-08-03 01:39:20 +00009583 if( nArg==3 ){
9584 db = 0;
9585 }else{
9586 db = p->db;
9587 /* Make sure the schema has been loaded */
9588 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
9589 }
drh51755a72019-08-08 19:40:29 +00009590 rc2 = sqlite3_test_control(testctrl, ii, db);
drh2e6d83b2019-08-03 01:39:20 +00009591 isOk = 3;
9592 }
9593 break;
9594
drh2ce15c32017-07-11 13:34:40 +00009595 /* sqlite3_test_control(int, int) */
9596 case SQLITE_TESTCTRL_ASSERT:
9597 case SQLITE_TESTCTRL_ALWAYS:
drhef302e82017-11-15 19:14:08 +00009598 if( nArg==3 ){
9599 int opt = booleanValue(azArg[2]);
9600 rc2 = sqlite3_test_control(testctrl, opt);
9601 isOk = 1;
9602 }
9603 break;
9604
9605 /* sqlite3_test_control(int, int) */
9606 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
drh2ce15c32017-07-11 13:34:40 +00009607 case SQLITE_TESTCTRL_NEVER_CORRUPT:
9608 if( nArg==3 ){
9609 int opt = booleanValue(azArg[2]);
9610 rc2 = sqlite3_test_control(testctrl, opt);
drhef302e82017-11-15 19:14:08 +00009611 isOk = 3;
drh2ce15c32017-07-11 13:34:40 +00009612 }
9613 break;
9614
drh171c50e2020-01-01 15:43:30 +00009615 /* sqlite3_test_control(sqlite3*) */
9616 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
9617 rc2 = sqlite3_test_control(testctrl, p->db);
drh2a83c102020-01-01 23:02:35 +00009618 isOk = 3;
drh171c50e2020-01-01 15:43:30 +00009619 break;
9620
drh2ce15c32017-07-11 13:34:40 +00009621 case SQLITE_TESTCTRL_IMPOSTER:
9622 if( nArg==5 ){
9623 rc2 = sqlite3_test_control(testctrl, p->db,
9624 azArg[2],
9625 integerValue(azArg[3]),
9626 integerValue(azArg[4]));
drhef302e82017-11-15 19:14:08 +00009627 isOk = 3;
drh2ce15c32017-07-11 13:34:40 +00009628 }
9629 break;
drh0d9de992017-12-26 18:04:23 +00009630
9631#ifdef YYCOVERAGE
9632 case SQLITE_TESTCTRL_PARSER_COVERAGE:
9633 if( nArg==2 ){
9634 sqlite3_test_control(testctrl, p->out);
9635 isOk = 3;
9636 }
9637#endif
drh2ce15c32017-07-11 13:34:40 +00009638 }
9639 }
drhef302e82017-11-15 19:14:08 +00009640 if( isOk==0 && iCtrl>=0 ){
drhe2754c12019-08-26 12:50:01 +00009641 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
drhef302e82017-11-15 19:14:08 +00009642 rc = 1;
9643 }else if( isOk==1 ){
9644 raw_printf(p->out, "%d\n", rc2);
9645 }else if( isOk==2 ){
9646 raw_printf(p->out, "0x%08x\n", rc2);
9647 }
drh2ce15c32017-07-11 13:34:40 +00009648 }else
9649#endif /* !defined(SQLITE_UNTESTABLE) */
9650
9651 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
9652 open_db(p, 0);
9653 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
9654 }else
9655
9656 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
9657 if( nArg==2 ){
9658 enableTimer = booleanValue(azArg[1]);
9659 if( enableTimer && !HAS_TIMER ){
9660 raw_printf(stderr, "Error: timer not available on this system.\n");
9661 enableTimer = 0;
9662 }
9663 }else{
9664 raw_printf(stderr, "Usage: .timer on|off\n");
9665 rc = 1;
9666 }
9667 }else
9668
drh707821f2018-12-05 13:39:06 +00009669#ifndef SQLITE_OMIT_TRACE
drh2ce15c32017-07-11 13:34:40 +00009670 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
drh707821f2018-12-05 13:39:06 +00009671 int mType = 0;
9672 int jj;
drh2ce15c32017-07-11 13:34:40 +00009673 open_db(p, 0);
drh707821f2018-12-05 13:39:06 +00009674 for(jj=1; jj<nArg; jj++){
9675 const char *z = azArg[jj];
9676 if( z[0]=='-' ){
9677 if( optionMatch(z, "expanded") ){
9678 p->eTraceType = SHELL_TRACE_EXPANDED;
9679 }
9680#ifdef SQLITE_ENABLE_NORMALIZE
9681 else if( optionMatch(z, "normalized") ){
9682 p->eTraceType = SHELL_TRACE_NORMALIZED;
9683 }
9684#endif
9685 else if( optionMatch(z, "plain") ){
9686 p->eTraceType = SHELL_TRACE_PLAIN;
9687 }
9688 else if( optionMatch(z, "profile") ){
9689 mType |= SQLITE_TRACE_PROFILE;
9690 }
9691 else if( optionMatch(z, "row") ){
9692 mType |= SQLITE_TRACE_ROW;
9693 }
9694 else if( optionMatch(z, "stmt") ){
9695 mType |= SQLITE_TRACE_STMT;
9696 }
9697 else if( optionMatch(z, "close") ){
9698 mType |= SQLITE_TRACE_CLOSE;
9699 }
9700 else {
9701 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
9702 rc = 1;
9703 goto meta_command_exit;
9704 }
9705 }else{
9706 output_file_close(p->traceOut);
9707 p->traceOut = output_file_open(azArg[1], 0);
9708 }
drh2ce15c32017-07-11 13:34:40 +00009709 }
drh2ce15c32017-07-11 13:34:40 +00009710 if( p->traceOut==0 ){
9711 sqlite3_trace_v2(p->db, 0, 0, 0);
9712 }else{
drh707821f2018-12-05 13:39:06 +00009713 if( mType==0 ) mType = SQLITE_TRACE_STMT;
9714 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
drh2ce15c32017-07-11 13:34:40 +00009715 }
drh2ce15c32017-07-11 13:34:40 +00009716 }else
drh707821f2018-12-05 13:39:06 +00009717#endif /* !defined(SQLITE_OMIT_TRACE) */
drh2ce15c32017-07-11 13:34:40 +00009718
drhe2b7a762019-10-02 00:25:08 +00009719#if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
drhcc5979d2019-08-16 22:58:29 +00009720 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
9721 int ii;
drh8c754a32019-08-19 20:35:30 +00009722 int lenOpt;
drh5df84282019-08-17 19:45:25 +00009723 char *zOpt;
drhcc5979d2019-08-16 22:58:29 +00009724 if( nArg<2 ){
drh5df84282019-08-17 19:45:25 +00009725 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
drhcc5979d2019-08-16 22:58:29 +00009726 rc = 1;
9727 goto meta_command_exit;
9728 }
9729 open_db(p, 0);
drh5df84282019-08-17 19:45:25 +00009730 zOpt = azArg[1];
9731 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
drh8c754a32019-08-19 20:35:30 +00009732 lenOpt = (int)strlen(zOpt);
9733 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
drh5df84282019-08-17 19:45:25 +00009734 assert( azArg[nArg]==0 );
drh8c754a32019-08-19 20:35:30 +00009735 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
drh5df84282019-08-17 19:45:25 +00009736 }else{
9737 for(ii=1; ii<nArg; ii++){
9738 sqlite3_create_module(p->db, azArg[ii], 0, 0);
9739 }
drhcc5979d2019-08-16 22:58:29 +00009740 }
9741 }else
9742#endif
9743
drh2ce15c32017-07-11 13:34:40 +00009744#if SQLITE_USER_AUTHENTICATION
9745 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
9746 if( nArg<2 ){
9747 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
9748 rc = 1;
9749 goto meta_command_exit;
9750 }
9751 open_db(p, 0);
9752 if( strcmp(azArg[1],"login")==0 ){
9753 if( nArg!=4 ){
9754 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
9755 rc = 1;
9756 goto meta_command_exit;
9757 }
drhe2754c12019-08-26 12:50:01 +00009758 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
9759 strlen30(azArg[3]));
drh2ce15c32017-07-11 13:34:40 +00009760 if( rc ){
9761 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
9762 rc = 1;
9763 }
9764 }else if( strcmp(azArg[1],"add")==0 ){
9765 if( nArg!=5 ){
9766 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
9767 rc = 1;
9768 goto meta_command_exit;
9769 }
drhaf2770f2018-01-05 14:55:43 +00009770 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
drh2ce15c32017-07-11 13:34:40 +00009771 booleanValue(azArg[4]));
9772 if( rc ){
9773 raw_printf(stderr, "User-Add failed: %d\n", rc);
9774 rc = 1;
9775 }
9776 }else if( strcmp(azArg[1],"edit")==0 ){
9777 if( nArg!=5 ){
9778 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
9779 rc = 1;
9780 goto meta_command_exit;
9781 }
drhaf2770f2018-01-05 14:55:43 +00009782 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
drh2ce15c32017-07-11 13:34:40 +00009783 booleanValue(azArg[4]));
9784 if( rc ){
9785 raw_printf(stderr, "User-Edit failed: %d\n", rc);
9786 rc = 1;
9787 }
9788 }else if( strcmp(azArg[1],"delete")==0 ){
9789 if( nArg!=3 ){
9790 raw_printf(stderr, "Usage: .user delete USER\n");
9791 rc = 1;
9792 goto meta_command_exit;
9793 }
9794 rc = sqlite3_user_delete(p->db, azArg[2]);
9795 if( rc ){
9796 raw_printf(stderr, "User-Delete failed: %d\n", rc);
9797 rc = 1;
9798 }
9799 }else{
9800 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
9801 rc = 1;
9802 goto meta_command_exit;
9803 }
9804 }else
9805#endif /* SQLITE_USER_AUTHENTICATION */
9806
9807 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
9808 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
9809 sqlite3_libversion(), sqlite3_sourceid());
drh0ed2fd82018-01-16 20:05:27 +00009810#if SQLITE_HAVE_ZLIB
9811 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
9812#endif
9813#define CTIMEOPT_VAL_(opt) #opt
9814#define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
9815#if defined(__clang__) && defined(__clang_major__)
9816 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
9817 CTIMEOPT_VAL(__clang_minor__) "."
9818 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
9819#elif defined(_MSC_VER)
9820 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
9821#elif defined(__GNUC__) && defined(__VERSION__)
9822 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
9823#endif
drh2ce15c32017-07-11 13:34:40 +00009824 }else
9825
9826 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
9827 const char *zDbName = nArg==2 ? azArg[1] : "main";
9828 sqlite3_vfs *pVfs = 0;
9829 if( p->db ){
9830 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
9831 if( pVfs ){
9832 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
9833 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
9834 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
9835 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9836 }
9837 }
9838 }else
9839
9840 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
9841 sqlite3_vfs *pVfs;
9842 sqlite3_vfs *pCurrent = 0;
9843 if( p->db ){
9844 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
9845 }
9846 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
9847 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
9848 pVfs==pCurrent ? " <--- CURRENT" : "");
9849 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
9850 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
9851 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
9852 if( pVfs->pNext ){
9853 raw_printf(p->out, "-----------------------------------\n");
9854 }
9855 }
9856 }else
9857
9858 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
9859 const char *zDbName = nArg==2 ? azArg[1] : "main";
9860 char *zVfsName = 0;
9861 if( p->db ){
9862 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
9863 if( zVfsName ){
9864 utf8_printf(p->out, "%s\n", zVfsName);
9865 sqlite3_free(zVfsName);
9866 }
9867 }
9868 }else
9869
9870#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9871 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
9872 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
9873 }else
9874#endif
9875
9876 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
9877 int j;
9878 assert( nArg<=ArraySize(azArg) );
9879 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
9880 p->colWidth[j-1] = (int)integerValue(azArg[j]);
9881 }
9882 }else
9883
9884 {
9885 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
9886 " \"%s\". Enter \".help\" for help\n", azArg[0]);
9887 rc = 1;
9888 }
9889
9890meta_command_exit:
9891 if( p->outCount ){
9892 p->outCount--;
9893 if( p->outCount==0 ) output_reset(p);
9894 }
9895 return rc;
9896}
9897
9898/*
9899** Return TRUE if a semicolon occurs anywhere in the first N characters
9900** of string z[].
9901*/
9902static int line_contains_semicolon(const char *z, int N){
9903 int i;
9904 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
9905 return 0;
9906}
9907
9908/*
9909** Test to see if a line consists entirely of whitespace.
9910*/
9911static int _all_whitespace(const char *z){
9912 for(; *z; z++){
9913 if( IsSpace(z[0]) ) continue;
9914 if( *z=='/' && z[1]=='*' ){
9915 z += 2;
9916 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
9917 if( *z==0 ) return 0;
9918 z++;
9919 continue;
9920 }
9921 if( *z=='-' && z[1]=='-' ){
9922 z += 2;
9923 while( *z && *z!='\n' ){ z++; }
9924 if( *z==0 ) return 1;
9925 continue;
9926 }
9927 return 0;
9928 }
9929 return 1;
9930}
9931
9932/*
9933** Return TRUE if the line typed in is an SQL command terminator other
9934** than a semi-colon. The SQL Server style "go" command is understood
9935** as is the Oracle "/".
9936*/
9937static int line_is_command_terminator(const char *zLine){
9938 while( IsSpace(zLine[0]) ){ zLine++; };
9939 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
9940 return 1; /* Oracle */
9941 }
9942 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
9943 && _all_whitespace(&zLine[2]) ){
9944 return 1; /* SQL Server */
9945 }
9946 return 0;
9947}
9948
9949/*
drh56f17742018-01-24 01:58:49 +00009950** We need a default sqlite3_complete() implementation to use in case
9951** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
9952** any arbitrary text is a complete SQL statement. This is not very
9953** user-friendly, but it does seem to work.
9954*/
9955#ifdef SQLITE_OMIT_COMPLETE
danc86b23b2018-11-16 14:36:42 +00009956#define sqlite3_complete(x) 1
drh56f17742018-01-24 01:58:49 +00009957#endif
9958
9959/*
drh2ce15c32017-07-11 13:34:40 +00009960** Return true if zSql is a complete SQL statement. Return false if it
9961** ends in the middle of a string literal or C-style comment.
9962*/
9963static int line_is_complete(char *zSql, int nSql){
9964 int rc;
9965 if( zSql==0 ) return 1;
9966 zSql[nSql] = ';';
9967 zSql[nSql+1] = 0;
9968 rc = sqlite3_complete(zSql);
9969 zSql[nSql] = 0;
9970 return rc;
9971}
9972
9973/*
drhfc29a862018-05-11 19:11:18 +00009974** Run a single line of SQL. Return the number of errors.
drh2ce15c32017-07-11 13:34:40 +00009975*/
9976static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
9977 int rc;
9978 char *zErrMsg = 0;
9979
9980 open_db(p, 0);
9981 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
drhfc4eeef2019-02-05 19:48:46 +00009982 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
drh2ce15c32017-07-11 13:34:40 +00009983 BEGIN_TIMER;
drha10b9992018-03-09 15:24:33 +00009984 rc = shell_exec(p, zSql, &zErrMsg);
drh2ce15c32017-07-11 13:34:40 +00009985 END_TIMER;
9986 if( rc || zErrMsg ){
9987 char zPrefix[100];
9988 if( in!=0 || !stdin_is_interactive ){
9989 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
9990 "Error: near line %d:", startline);
9991 }else{
9992 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
9993 }
9994 if( zErrMsg!=0 ){
9995 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
9996 sqlite3_free(zErrMsg);
9997 zErrMsg = 0;
9998 }else{
9999 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
10000 }
10001 return 1;
10002 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
10003 raw_printf(p->out, "changes: %3d total_changes: %d\n",
10004 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
10005 }
10006 return 0;
10007}
10008
10009
10010/*
10011** Read input from *in and process it. If *in==0 then input
10012** is interactive - the user is typing it it. Otherwise, input
10013** is coming from a file or device. A prompt is issued and history
10014** is saved only if input is interactive. An interrupt signal will
10015** cause this routine to exit immediately, unless input is interactive.
10016**
10017** Return the number of errors.
10018*/
drh60379d42018-12-13 18:30:01 +000010019static int process_input(ShellState *p){
drh2ce15c32017-07-11 13:34:40 +000010020 char *zLine = 0; /* A single input line */
10021 char *zSql = 0; /* Accumulated SQL text */
10022 int nLine; /* Length of current line */
10023 int nSql = 0; /* Bytes of zSql[] used */
10024 int nAlloc = 0; /* Allocated zSql[] space */
10025 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
10026 int rc; /* Error code */
10027 int errCnt = 0; /* Number of errors seen */
drh2ce15c32017-07-11 13:34:40 +000010028 int startline = 0; /* Line number for start of current input */
10029
drh2c8ee022018-12-13 18:59:30 +000010030 p->lineno = 0;
drh60379d42018-12-13 18:30:01 +000010031 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
drh2ce15c32017-07-11 13:34:40 +000010032 fflush(p->out);
drh60379d42018-12-13 18:30:01 +000010033 zLine = one_input_line(p->in, zLine, nSql>0);
drh2ce15c32017-07-11 13:34:40 +000010034 if( zLine==0 ){
10035 /* End of input */
drh60379d42018-12-13 18:30:01 +000010036 if( p->in==0 && stdin_is_interactive ) printf("\n");
drh2ce15c32017-07-11 13:34:40 +000010037 break;
10038 }
10039 if( seenInterrupt ){
drh60379d42018-12-13 18:30:01 +000010040 if( p->in!=0 ) break;
drh2ce15c32017-07-11 13:34:40 +000010041 seenInterrupt = 0;
10042 }
drh2c8ee022018-12-13 18:59:30 +000010043 p->lineno++;
drh2ce15c32017-07-11 13:34:40 +000010044 if( nSql==0 && _all_whitespace(zLine) ){
10045 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
10046 continue;
10047 }
drh1615c372018-05-12 23:56:22 +000010048 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
drh2ce15c32017-07-11 13:34:40 +000010049 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
drh1615c372018-05-12 23:56:22 +000010050 if( zLine[0]=='.' ){
10051 rc = do_meta_command(zLine, p);
10052 if( rc==2 ){ /* exit requested */
10053 break;
10054 }else if( rc ){
10055 errCnt++;
10056 }
drh2ce15c32017-07-11 13:34:40 +000010057 }
10058 continue;
10059 }
10060 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
10061 memcpy(zLine,";",2);
10062 }
10063 nLine = strlen30(zLine);
10064 if( nSql+nLine+2>=nAlloc ){
10065 nAlloc = nSql+nLine+100;
10066 zSql = realloc(zSql, nAlloc);
drh4b5345c2018-04-24 13:07:40 +000010067 if( zSql==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +000010068 }
10069 nSqlPrior = nSql;
10070 if( nSql==0 ){
10071 int i;
10072 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
10073 assert( nAlloc>0 && zSql!=0 );
10074 memcpy(zSql, zLine+i, nLine+1-i);
drh2c8ee022018-12-13 18:59:30 +000010075 startline = p->lineno;
drh2ce15c32017-07-11 13:34:40 +000010076 nSql = nLine-i;
10077 }else{
10078 zSql[nSql++] = '\n';
10079 memcpy(zSql+nSql, zLine, nLine+1);
10080 nSql += nLine;
10081 }
10082 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
10083 && sqlite3_complete(zSql) ){
drh60379d42018-12-13 18:30:01 +000010084 errCnt += runOneSqlLine(p, zSql, p->in, startline);
drh2ce15c32017-07-11 13:34:40 +000010085 nSql = 0;
10086 if( p->outCount ){
10087 output_reset(p);
10088 p->outCount = 0;
drh13c20932018-01-10 21:41:55 +000010089 }else{
10090 clearTempFile(p);
drh2ce15c32017-07-11 13:34:40 +000010091 }
10092 }else if( nSql && _all_whitespace(zSql) ){
10093 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
10094 nSql = 0;
10095 }
10096 }
10097 if( nSql && !_all_whitespace(zSql) ){
drh60379d42018-12-13 18:30:01 +000010098 errCnt += runOneSqlLine(p, zSql, p->in, startline);
drh2ce15c32017-07-11 13:34:40 +000010099 }
10100 free(zSql);
10101 free(zLine);
10102 return errCnt>0;
10103}
10104
10105/*
10106** Return a pathname which is the user's home directory. A
10107** 0 return indicates an error of some kind.
10108*/
10109static char *find_home_dir(int clearFlag){
10110 static char *home_dir = NULL;
10111 if( clearFlag ){
10112 free(home_dir);
10113 home_dir = 0;
10114 return 0;
10115 }
10116 if( home_dir ) return home_dir;
10117
10118#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
10119 && !defined(__RTP__) && !defined(_WRS_KERNEL)
10120 {
10121 struct passwd *pwent;
10122 uid_t uid = getuid();
10123 if( (pwent=getpwuid(uid)) != NULL) {
10124 home_dir = pwent->pw_dir;
10125 }
10126 }
10127#endif
10128
10129#if defined(_WIN32_WCE)
10130 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
10131 */
10132 home_dir = "/";
10133#else
10134
10135#if defined(_WIN32) || defined(WIN32)
10136 if (!home_dir) {
10137 home_dir = getenv("USERPROFILE");
10138 }
10139#endif
10140
10141 if (!home_dir) {
10142 home_dir = getenv("HOME");
10143 }
10144
10145#if defined(_WIN32) || defined(WIN32)
10146 if (!home_dir) {
10147 char *zDrive, *zPath;
10148 int n;
10149 zDrive = getenv("HOMEDRIVE");
10150 zPath = getenv("HOMEPATH");
10151 if( zDrive && zPath ){
10152 n = strlen30(zDrive) + strlen30(zPath) + 1;
10153 home_dir = malloc( n );
10154 if( home_dir==0 ) return 0;
10155 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
10156 return home_dir;
10157 }
10158 home_dir = "c:\\";
10159 }
10160#endif
10161
10162#endif /* !_WIN32_WCE */
10163
10164 if( home_dir ){
10165 int n = strlen30(home_dir) + 1;
10166 char *z = malloc( n );
10167 if( z ) memcpy(z, home_dir, n);
10168 home_dir = z;
10169 }
10170
10171 return home_dir;
10172}
10173
10174/*
10175** Read input from the file given by sqliterc_override. Or if that
10176** parameter is NULL, take input from ~/.sqliterc
10177**
10178** Returns the number of errors.
10179*/
10180static void process_sqliterc(
10181 ShellState *p, /* Configuration data */
10182 const char *sqliterc_override /* Name of config file. NULL to use default */
10183){
10184 char *home_dir = NULL;
10185 const char *sqliterc = sqliterc_override;
10186 char *zBuf = 0;
drh60379d42018-12-13 18:30:01 +000010187 FILE *inSaved = p->in;
drh2c8ee022018-12-13 18:59:30 +000010188 int savedLineno = p->lineno;
drh2ce15c32017-07-11 13:34:40 +000010189
10190 if (sqliterc == NULL) {
10191 home_dir = find_home_dir(0);
10192 if( home_dir==0 ){
10193 raw_printf(stderr, "-- warning: cannot find home directory;"
10194 " cannot read ~/.sqliterc\n");
10195 return;
10196 }
drh2ce15c32017-07-11 13:34:40 +000010197 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
10198 sqliterc = zBuf;
10199 }
drh60379d42018-12-13 18:30:01 +000010200 p->in = fopen(sqliterc,"rb");
10201 if( p->in ){
drh2ce15c32017-07-11 13:34:40 +000010202 if( stdin_is_interactive ){
10203 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
10204 }
drh60379d42018-12-13 18:30:01 +000010205 process_input(p);
10206 fclose(p->in);
drh2ce15c32017-07-11 13:34:40 +000010207 }
drh60379d42018-12-13 18:30:01 +000010208 p->in = inSaved;
drh2c8ee022018-12-13 18:59:30 +000010209 p->lineno = savedLineno;
drh2ce15c32017-07-11 13:34:40 +000010210 sqlite3_free(zBuf);
10211}
10212
10213/*
10214** Show available command line options
10215*/
10216static const char zOptions[] =
drhda57d962018-03-05 19:34:05 +000010217#if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
drhad7fd5d2018-03-05 20:21:50 +000010218 " -A ARGS... run \".archive ARGS\" and exit\n"
drhda57d962018-03-05 19:34:05 +000010219#endif
drh3baed312018-03-08 18:14:41 +000010220 " -append append the database to the end of the file\n"
drh2ce15c32017-07-11 13:34:40 +000010221 " -ascii set output mode to 'ascii'\n"
10222 " -bail stop after hitting an error\n"
10223 " -batch force batch I/O\n"
10224 " -column set output mode to 'column'\n"
10225 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
10226 " -csv set output mode to 'csv'\n"
drh6ca64482019-01-22 16:06:20 +000010227#if defined(SQLITE_ENABLE_DESERIALIZE)
10228 " -deserialize open the database using sqlite3_deserialize()\n"
10229#endif
drh2ce15c32017-07-11 13:34:40 +000010230 " -echo print commands before execution\n"
10231 " -init FILENAME read/process named file\n"
10232 " -[no]header turn headers on or off\n"
10233#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10234 " -heap SIZE Size of heap for memsys3 or memsys5\n"
10235#endif
10236 " -help show this message\n"
10237 " -html set output mode to HTML\n"
10238 " -interactive force interactive I/O\n"
10239 " -line set output mode to 'line'\n"
10240 " -list set output mode to 'list'\n"
10241 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
drh6ca64482019-01-22 16:06:20 +000010242#if defined(SQLITE_ENABLE_DESERIALIZE)
10243 " -maxsize N maximum size for a --deserialize database\n"
10244#endif
drhaf482572019-02-04 19:52:39 +000010245 " -memtrace trace all memory allocations and deallocations\n"
drh2ce15c32017-07-11 13:34:40 +000010246 " -mmap N default mmap size set to N\n"
10247#ifdef SQLITE_ENABLE_MULTIPLEX
10248 " -multiplex enable the multiplexor VFS\n"
10249#endif
10250 " -newline SEP set output row separator. Default: '\\n'\n"
drh0933aad2019-11-18 17:46:38 +000010251 " -nofollow refuse to open symbolic links to database files\n"
drh2ce15c32017-07-11 13:34:40 +000010252 " -nullvalue TEXT set text string for NULL values. Default ''\n"
10253 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
10254 " -quote set output mode to 'quote'\n"
drhee269a62018-02-14 23:27:43 +000010255 " -readonly open the database read-only\n"
drh2ce15c32017-07-11 13:34:40 +000010256 " -separator SEP set output column separator. Default: '|'\n"
drha90d84f2018-04-18 15:21:13 +000010257#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10258 " -sorterref SIZE sorter references threshold size\n"
10259#endif
drh2ce15c32017-07-11 13:34:40 +000010260 " -stats print memory stats before each finalize\n"
10261 " -version show SQLite version\n"
10262 " -vfs NAME use NAME as the default VFS\n"
10263#ifdef SQLITE_ENABLE_VFSTRACE
10264 " -vfstrace enable tracing of all VFS calls\n"
10265#endif
drh3baed312018-03-08 18:14:41 +000010266#ifdef SQLITE_HAVE_ZLIB
10267 " -zip open the file as a ZIP Archive\n"
10268#endif
drh2ce15c32017-07-11 13:34:40 +000010269;
10270static void usage(int showDetail){
10271 utf8_printf(stderr,
10272 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
10273 "FILENAME is the name of an SQLite database. A new database is created\n"
10274 "if the file does not previously exist.\n", Argv0);
10275 if( showDetail ){
10276 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
10277 }else{
10278 raw_printf(stderr, "Use the -help option for additional information\n");
10279 }
10280 exit(1);
10281}
10282
10283/*
drhe7df8922018-04-18 10:44:58 +000010284** Internal check: Verify that the SQLite is uninitialized. Print a
10285** error message if it is initialized.
10286*/
10287static void verify_uninitialized(void){
10288 if( sqlite3_config(-1)==SQLITE_MISUSE ){
drh8e02a182018-05-30 07:24:41 +000010289 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
drhe7df8922018-04-18 10:44:58 +000010290 " initialization.\n");
10291 }
10292}
10293
10294/*
drh2ce15c32017-07-11 13:34:40 +000010295** Initialize the state information in data
10296*/
10297static void main_init(ShellState *data) {
10298 memset(data, 0, sizeof(*data));
10299 data->normalMode = data->cMode = data->mode = MODE_List;
10300 data->autoExplain = 1;
10301 memcpy(data->colSeparator,SEP_Column, 2);
10302 memcpy(data->rowSeparator,SEP_Row, 2);
10303 data->showHeader = 0;
10304 data->shellFlgs = SHFLG_Lookaside;
drhe7df8922018-04-18 10:44:58 +000010305 verify_uninitialized();
drh2ce15c32017-07-11 13:34:40 +000010306 sqlite3_config(SQLITE_CONFIG_URI, 1);
10307 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
10308 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
10309 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
10310 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
10311}
10312
10313/*
10314** Output text to the console in a font that attracts extra attention.
10315*/
10316#ifdef _WIN32
10317static void printBold(const char *zText){
mistachkin43e86272020-04-09 15:31:22 +000010318#if !SQLITE_OS_WINRT
drh2ce15c32017-07-11 13:34:40 +000010319 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
10320 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
10321 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
10322 SetConsoleTextAttribute(out,
10323 FOREGROUND_RED|FOREGROUND_INTENSITY
10324 );
mistachkin43e86272020-04-09 15:31:22 +000010325#endif
drh2ce15c32017-07-11 13:34:40 +000010326 printf("%s", zText);
mistachkin43e86272020-04-09 15:31:22 +000010327#if !SQLITE_OS_WINRT
drh2ce15c32017-07-11 13:34:40 +000010328 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
mistachkin43e86272020-04-09 15:31:22 +000010329#endif
drh2ce15c32017-07-11 13:34:40 +000010330}
10331#else
10332static void printBold(const char *zText){
10333 printf("\033[1m%s\033[0m", zText);
10334}
10335#endif
10336
10337/*
10338** Get the argument to an --option. Throw an error and die if no argument
10339** is available.
10340*/
10341static char *cmdline_option_value(int argc, char **argv, int i){
10342 if( i==argc ){
10343 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
10344 argv[0], argv[argc-1]);
10345 exit(1);
10346 }
10347 return argv[i];
10348}
10349
10350#ifndef SQLITE_SHELL_IS_UTF8
10351# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
10352# define SQLITE_SHELL_IS_UTF8 (0)
10353# else
10354# define SQLITE_SHELL_IS_UTF8 (1)
10355# endif
10356#endif
10357
10358#if SQLITE_SHELL_IS_UTF8
10359int SQLITE_CDECL main(int argc, char **argv){
10360#else
10361int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
10362 char **argv;
10363#endif
10364 char *zErrMsg = 0;
10365 ShellState data;
10366 const char *zInitFile = 0;
10367 int i;
10368 int rc = 0;
10369 int warnInmemoryDb = 0;
10370 int readStdin = 1;
10371 int nCmd = 0;
10372 char **azCmd = 0;
dan16a47422018-04-18 09:16:11 +000010373 const char *zVfs = 0; /* Value of -vfs command-line option */
drh1f22f622018-05-17 13:29:14 +000010374#if !SQLITE_SHELL_IS_UTF8
10375 char **argvToFree = 0;
10376 int argcToFree = 0;
10377#endif
drh2ce15c32017-07-11 13:34:40 +000010378
10379 setBinaryMode(stdin, 0);
10380 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
10381 stdin_is_interactive = isatty(0);
10382 stdout_is_console = isatty(1);
10383
drh4a3a3eb2020-02-29 15:53:48 +000010384#ifdef SQLITE_DEBUG
10385 registerOomSimulator();
10386#endif
10387
mistachkin1e8487d2018-07-22 06:25:35 +000010388#if !defined(_WIN32_WCE)
10389 if( getenv("SQLITE_DEBUG_BREAK") ){
10390 if( isatty(0) && isatty(2) ){
10391 fprintf(stderr,
10392 "attach debugger to process %d and press any key to continue.\n",
10393 GETPID());
10394 fgetc(stdin);
10395 }else{
10396#if defined(_WIN32) || defined(WIN32)
mistachkin43e86272020-04-09 15:31:22 +000010397#if SQLITE_OS_WINRT
10398 __debugbreak();
10399#else
mistachkin1e8487d2018-07-22 06:25:35 +000010400 DebugBreak();
mistachkin43e86272020-04-09 15:31:22 +000010401#endif
mistachkin1e8487d2018-07-22 06:25:35 +000010402#elif defined(SIGTRAP)
10403 raise(SIGTRAP);
10404#endif
10405 }
10406 }
10407#endif
10408
drh2ce15c32017-07-11 13:34:40 +000010409#if USE_SYSTEM_SQLITE+0!=1
drhb3c45232017-08-28 14:33:27 +000010410 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
drh2ce15c32017-07-11 13:34:40 +000010411 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
10412 sqlite3_sourceid(), SQLITE_SOURCE_ID);
10413 exit(1);
10414 }
10415#endif
10416 main_init(&data);
drh501ea052018-02-15 01:03:37 +000010417
10418 /* On Windows, we must translate command-line arguments into UTF-8.
10419 ** The SQLite memory allocator subsystem has to be enabled in order to
10420 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
10421 ** subsequent sqlite3_config() calls will work. So copy all results into
10422 ** memory that does not come from the SQLite memory allocator.
10423 */
drh4b18c1d2018-02-04 20:33:13 +000010424#if !SQLITE_SHELL_IS_UTF8
drh501ea052018-02-15 01:03:37 +000010425 sqlite3_initialize();
drh1f22f622018-05-17 13:29:14 +000010426 argvToFree = malloc(sizeof(argv[0])*argc*2);
10427 argcToFree = argc;
10428 argv = argvToFree + argc;
drh4b5345c2018-04-24 13:07:40 +000010429 if( argv==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +000010430 for(i=0; i<argc; i++){
drh501ea052018-02-15 01:03:37 +000010431 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
10432 int n;
drh4b5345c2018-04-24 13:07:40 +000010433 if( z==0 ) shell_out_of_memory();
drh501ea052018-02-15 01:03:37 +000010434 n = (int)strlen(z);
10435 argv[i] = malloc( n+1 );
drh4b5345c2018-04-24 13:07:40 +000010436 if( argv[i]==0 ) shell_out_of_memory();
drh501ea052018-02-15 01:03:37 +000010437 memcpy(argv[i], z, n+1);
drh1f22f622018-05-17 13:29:14 +000010438 argvToFree[i] = argv[i];
drh501ea052018-02-15 01:03:37 +000010439 sqlite3_free(z);
drh2ce15c32017-07-11 13:34:40 +000010440 }
drh501ea052018-02-15 01:03:37 +000010441 sqlite3_shutdown();
drh2ce15c32017-07-11 13:34:40 +000010442#endif
drh501ea052018-02-15 01:03:37 +000010443
drh2ce15c32017-07-11 13:34:40 +000010444 assert( argc>=1 && argv && argv[0] );
10445 Argv0 = argv[0];
10446
10447 /* Make sure we have a valid signal handler early, before anything
10448 ** else is done.
10449 */
10450#ifdef SIGINT
10451 signal(SIGINT, interrupt_handler);
mistachkinb4bab902017-10-27 17:09:44 +000010452#elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10453 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
drh2ce15c32017-07-11 13:34:40 +000010454#endif
10455
10456#ifdef SQLITE_SHELL_DBNAME_PROC
10457 {
10458 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
10459 ** of a C-function that will provide the name of the database file. Use
10460 ** this compile-time option to embed this shell program in larger
10461 ** applications. */
10462 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
10463 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
10464 warnInmemoryDb = 0;
10465 }
10466#endif
10467
10468 /* Do an initial pass through the command-line argument to locate
10469 ** the name of the database file, the name of the initialization file,
10470 ** the size of the alternative malloc heap,
10471 ** and the first command to execute.
10472 */
drhe7df8922018-04-18 10:44:58 +000010473 verify_uninitialized();
drh2ce15c32017-07-11 13:34:40 +000010474 for(i=1; i<argc; i++){
10475 char *z;
10476 z = argv[i];
10477 if( z[0]!='-' ){
10478 if( data.zDbFilename==0 ){
10479 data.zDbFilename = z;
10480 }else{
10481 /* Excesss arguments are interpreted as SQL (or dot-commands) and
10482 ** mean that nothing is read from stdin */
10483 readStdin = 0;
10484 nCmd++;
10485 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
drh4b5345c2018-04-24 13:07:40 +000010486 if( azCmd==0 ) shell_out_of_memory();
drh2ce15c32017-07-11 13:34:40 +000010487 azCmd[nCmd-1] = z;
10488 }
10489 }
10490 if( z[1]=='-' ) z++;
10491 if( strcmp(z,"-separator")==0
10492 || strcmp(z,"-nullvalue")==0
10493 || strcmp(z,"-newline")==0
10494 || strcmp(z,"-cmd")==0
10495 ){
10496 (void)cmdline_option_value(argc, argv, ++i);
10497 }else if( strcmp(z,"-init")==0 ){
10498 zInitFile = cmdline_option_value(argc, argv, ++i);
10499 }else if( strcmp(z,"-batch")==0 ){
10500 /* Need to check for batch mode here to so we can avoid printing
10501 ** informational messages (like from process_sqliterc) before
10502 ** we do the actual processing of arguments later in a second pass.
10503 */
10504 stdin_is_interactive = 0;
10505 }else if( strcmp(z,"-heap")==0 ){
10506#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
10507 const char *zSize;
10508 sqlite3_int64 szHeap;
10509
10510 zSize = cmdline_option_value(argc, argv, ++i);
10511 szHeap = integerValue(zSize);
10512 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
10513 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
10514#else
10515 (void)cmdline_option_value(argc, argv, ++i);
10516#endif
drh2ce15c32017-07-11 13:34:40 +000010517 }else if( strcmp(z,"-pagecache")==0 ){
10518 int n, sz;
10519 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10520 if( sz>70000 ) sz = 70000;
10521 if( sz<0 ) sz = 0;
10522 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10523 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
10524 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
10525 data.shellFlgs |= SHFLG_Pagecache;
10526 }else if( strcmp(z,"-lookaside")==0 ){
10527 int n, sz;
10528 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
10529 if( sz<0 ) sz = 0;
10530 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
10531 if( n<0 ) n = 0;
10532 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
10533 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
10534#ifdef SQLITE_ENABLE_VFSTRACE
10535 }else if( strcmp(z,"-vfstrace")==0 ){
10536 extern int vfstrace_register(
10537 const char *zTraceName,
10538 const char *zOldVfsName,
10539 int (*xOut)(const char*,void*),
10540 void *pOutArg,
10541 int makeDefault
10542 );
10543 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
10544#endif
10545#ifdef SQLITE_ENABLE_MULTIPLEX
10546 }else if( strcmp(z,"-multiplex")==0 ){
10547 extern int sqlite3_multiple_initialize(const char*,int);
10548 sqlite3_multiplex_initialize(0, 1);
10549#endif
10550 }else if( strcmp(z,"-mmap")==0 ){
10551 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10552 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
drha90d84f2018-04-18 15:21:13 +000010553#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10554 }else if( strcmp(z,"-sorterref")==0 ){
10555 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
10556 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
10557#endif
drh2ce15c32017-07-11 13:34:40 +000010558 }else if( strcmp(z,"-vfs")==0 ){
dan16a47422018-04-18 09:16:11 +000010559 zVfs = cmdline_option_value(argc, argv, ++i);
drh3baed312018-03-08 18:14:41 +000010560#ifdef SQLITE_HAVE_ZLIB
drh8682e122018-01-07 20:38:10 +000010561 }else if( strcmp(z,"-zip")==0 ){
10562 data.openMode = SHELL_OPEN_ZIPFILE;
10563#endif
10564 }else if( strcmp(z,"-append")==0 ){
10565 data.openMode = SHELL_OPEN_APPENDVFS;
drha751f392018-10-30 15:31:22 +000010566#ifdef SQLITE_ENABLE_DESERIALIZE
drh60f34ae2018-10-30 13:19:49 +000010567 }else if( strcmp(z,"-deserialize")==0 ){
10568 data.openMode = SHELL_OPEN_DESERIALIZE;
drh6ca64482019-01-22 16:06:20 +000010569 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10570 data.szMax = integerValue(argv[++i]);
drha751f392018-10-30 15:31:22 +000010571#endif
drhee269a62018-02-14 23:27:43 +000010572 }else if( strcmp(z,"-readonly")==0 ){
10573 data.openMode = SHELL_OPEN_READONLY;
drh0933aad2019-11-18 17:46:38 +000010574 }else if( strcmp(z,"-nofollow")==0 ){
10575 data.openFlags = SQLITE_OPEN_NOFOLLOW;
drhda57d962018-03-05 19:34:05 +000010576#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
drh93b77312018-03-05 20:20:22 +000010577 }else if( strncmp(z, "-A",2)==0 ){
drhda57d962018-03-05 19:34:05 +000010578 /* All remaining command-line arguments are passed to the ".archive"
10579 ** command, so ignore them */
10580 break;
10581#endif
drh50b910a2019-01-21 14:55:03 +000010582 }else if( strcmp(z, "-memtrace")==0 ){
10583 sqlite3MemTraceActivate(stderr);
drh2ce15c32017-07-11 13:34:40 +000010584 }
10585 }
drhe7df8922018-04-18 10:44:58 +000010586 verify_uninitialized();
10587
dan16a47422018-04-18 09:16:11 +000010588
drhd11b8f62018-04-25 13:27:07 +000010589#ifdef SQLITE_SHELL_INIT_PROC
10590 {
10591 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
10592 ** of a C-function that will perform initialization actions on SQLite that
10593 ** occur just before or after sqlite3_initialize(). Use this compile-time
10594 ** option to embed this shell program in larger applications. */
10595 extern void SQLITE_SHELL_INIT_PROC(void);
10596 SQLITE_SHELL_INIT_PROC();
10597 }
10598#else
dan16a47422018-04-18 09:16:11 +000010599 /* All the sqlite3_config() calls have now been made. So it is safe
10600 ** to call sqlite3_initialize() and process any command line -vfs option. */
10601 sqlite3_initialize();
drhd11b8f62018-04-25 13:27:07 +000010602#endif
10603
dan16a47422018-04-18 09:16:11 +000010604 if( zVfs ){
10605 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
10606 if( pVfs ){
10607 sqlite3_vfs_register(pVfs, 1);
10608 }else{
10609 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
10610 exit(1);
10611 }
10612 }
10613
drh2ce15c32017-07-11 13:34:40 +000010614 if( data.zDbFilename==0 ){
10615#ifndef SQLITE_OMIT_MEMORYDB
10616 data.zDbFilename = ":memory:";
10617 warnInmemoryDb = argc==1;
10618#else
10619 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
10620 return 1;
10621#endif
10622 }
10623 data.out = stdout;
drh8682e122018-01-07 20:38:10 +000010624 sqlite3_appendvfs_init(0,0,0);
drh2ce15c32017-07-11 13:34:40 +000010625
10626 /* Go ahead and open the database file if it already exists. If the
10627 ** file does not exist, delay opening it. This prevents empty database
10628 ** files from being created if a user mistypes the database name argument
10629 ** to the sqlite command-line tool.
10630 */
10631 if( access(data.zDbFilename, 0)==0 ){
10632 open_db(&data, 0);
10633 }
10634
10635 /* Process the initialization file if there is one. If no -init option
10636 ** is given on the command line, look for a file named ~/.sqliterc and
10637 ** try to process it.
10638 */
10639 process_sqliterc(&data,zInitFile);
10640
10641 /* Make a second pass through the command-line argument and set
10642 ** options. This second pass is delayed until after the initialization
10643 ** file is processed so that the command-line arguments will override
10644 ** settings in the initialization file.
10645 */
10646 for(i=1; i<argc; i++){
10647 char *z = argv[i];
10648 if( z[0]!='-' ) continue;
10649 if( z[1]=='-' ){ z++; }
10650 if( strcmp(z,"-init")==0 ){
10651 i++;
10652 }else if( strcmp(z,"-html")==0 ){
10653 data.mode = MODE_Html;
10654 }else if( strcmp(z,"-list")==0 ){
10655 data.mode = MODE_List;
10656 }else if( strcmp(z,"-quote")==0 ){
10657 data.mode = MODE_Quote;
10658 }else if( strcmp(z,"-line")==0 ){
10659 data.mode = MODE_Line;
10660 }else if( strcmp(z,"-column")==0 ){
10661 data.mode = MODE_Column;
10662 }else if( strcmp(z,"-csv")==0 ){
10663 data.mode = MODE_Csv;
10664 memcpy(data.colSeparator,",",2);
drh3baed312018-03-08 18:14:41 +000010665#ifdef SQLITE_HAVE_ZLIB
drh1fa6d9f2018-01-06 21:46:01 +000010666 }else if( strcmp(z,"-zip")==0 ){
10667 data.openMode = SHELL_OPEN_ZIPFILE;
10668#endif
10669 }else if( strcmp(z,"-append")==0 ){
10670 data.openMode = SHELL_OPEN_APPENDVFS;
drha751f392018-10-30 15:31:22 +000010671#ifdef SQLITE_ENABLE_DESERIALIZE
drh60f34ae2018-10-30 13:19:49 +000010672 }else if( strcmp(z,"-deserialize")==0 ){
10673 data.openMode = SHELL_OPEN_DESERIALIZE;
drh6ca64482019-01-22 16:06:20 +000010674 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
10675 data.szMax = integerValue(argv[++i]);
drha751f392018-10-30 15:31:22 +000010676#endif
drh4aafe592018-03-23 16:08:30 +000010677 }else if( strcmp(z,"-readonly")==0 ){
10678 data.openMode = SHELL_OPEN_READONLY;
drh0933aad2019-11-18 17:46:38 +000010679 }else if( strcmp(z,"-nofollow")==0 ){
10680 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
drh2ce15c32017-07-11 13:34:40 +000010681 }else if( strcmp(z,"-ascii")==0 ){
10682 data.mode = MODE_Ascii;
10683 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10684 SEP_Unit);
10685 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10686 SEP_Record);
10687 }else if( strcmp(z,"-separator")==0 ){
10688 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
10689 "%s",cmdline_option_value(argc,argv,++i));
10690 }else if( strcmp(z,"-newline")==0 ){
10691 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
10692 "%s",cmdline_option_value(argc,argv,++i));
10693 }else if( strcmp(z,"-nullvalue")==0 ){
10694 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
10695 "%s",cmdline_option_value(argc,argv,++i));
10696 }else if( strcmp(z,"-header")==0 ){
10697 data.showHeader = 1;
10698 }else if( strcmp(z,"-noheader")==0 ){
10699 data.showHeader = 0;
10700 }else if( strcmp(z,"-echo")==0 ){
10701 ShellSetFlag(&data, SHFLG_Echo);
10702 }else if( strcmp(z,"-eqp")==0 ){
drhada70452017-12-21 21:02:27 +000010703 data.autoEQP = AUTOEQP_on;
drh2ce15c32017-07-11 13:34:40 +000010704 }else if( strcmp(z,"-eqpfull")==0 ){
drhada70452017-12-21 21:02:27 +000010705 data.autoEQP = AUTOEQP_full;
drh2ce15c32017-07-11 13:34:40 +000010706 }else if( strcmp(z,"-stats")==0 ){
10707 data.statsOn = 1;
10708 }else if( strcmp(z,"-scanstats")==0 ){
10709 data.scanstatsOn = 1;
10710 }else if( strcmp(z,"-backslash")==0 ){
10711 /* Undocumented command-line option: -backslash
10712 ** Causes C-style backslash escapes to be evaluated in SQL statements
10713 ** prior to sending the SQL into SQLite. Useful for injecting
10714 ** crazy bytes in the middle of SQL statements for testing and debugging.
10715 */
10716 ShellSetFlag(&data, SHFLG_Backslash);
10717 }else if( strcmp(z,"-bail")==0 ){
10718 bail_on_error = 1;
10719 }else if( strcmp(z,"-version")==0 ){
10720 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
10721 return 0;
10722 }else if( strcmp(z,"-interactive")==0 ){
10723 stdin_is_interactive = 1;
10724 }else if( strcmp(z,"-batch")==0 ){
10725 stdin_is_interactive = 0;
10726 }else if( strcmp(z,"-heap")==0 ){
10727 i++;
drh2ce15c32017-07-11 13:34:40 +000010728 }else if( strcmp(z,"-pagecache")==0 ){
10729 i+=2;
10730 }else if( strcmp(z,"-lookaside")==0 ){
10731 i+=2;
10732 }else if( strcmp(z,"-mmap")==0 ){
10733 i++;
drh50b910a2019-01-21 14:55:03 +000010734 }else if( strcmp(z,"-memtrace")==0 ){
10735 i++;
drha90d84f2018-04-18 15:21:13 +000010736#ifdef SQLITE_ENABLE_SORTER_REFERENCES
10737 }else if( strcmp(z,"-sorterref")==0 ){
10738 i++;
10739#endif
drh2ce15c32017-07-11 13:34:40 +000010740 }else if( strcmp(z,"-vfs")==0 ){
10741 i++;
10742#ifdef SQLITE_ENABLE_VFSTRACE
10743 }else if( strcmp(z,"-vfstrace")==0 ){
10744 i++;
10745#endif
10746#ifdef SQLITE_ENABLE_MULTIPLEX
10747 }else if( strcmp(z,"-multiplex")==0 ){
10748 i++;
10749#endif
10750 }else if( strcmp(z,"-help")==0 ){
10751 usage(1);
10752 }else if( strcmp(z,"-cmd")==0 ){
10753 /* Run commands that follow -cmd first and separately from commands
10754 ** that simply appear on the command-line. This seems goofy. It would
10755 ** be better if all commands ran in the order that they appear. But
10756 ** we retain the goofy behavior for historical compatibility. */
10757 if( i==argc-1 ) break;
10758 z = cmdline_option_value(argc,argv,++i);
10759 if( z[0]=='.' ){
10760 rc = do_meta_command(z, &data);
10761 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
10762 }else{
10763 open_db(&data, 0);
drha10b9992018-03-09 15:24:33 +000010764 rc = shell_exec(&data, z, &zErrMsg);
drh2ce15c32017-07-11 13:34:40 +000010765 if( zErrMsg!=0 ){
10766 utf8_printf(stderr,"Error: %s\n", zErrMsg);
10767 if( bail_on_error ) return rc!=0 ? rc : 1;
10768 }else if( rc!=0 ){
10769 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
10770 if( bail_on_error ) return rc;
10771 }
10772 }
drhda57d962018-03-05 19:34:05 +000010773#if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
drh93b77312018-03-05 20:20:22 +000010774 }else if( strncmp(z, "-A", 2)==0 ){
drhda57d962018-03-05 19:34:05 +000010775 if( nCmd>0 ){
10776 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
10777 " with \"%s\"\n", z);
10778 return 1;
10779 }
drhbe4ccb22018-05-17 20:04:24 +000010780 open_db(&data, OPEN_DB_ZIPFILE);
drh93b77312018-03-05 20:20:22 +000010781 if( z[2] ){
10782 argv[i] = &z[2];
drhd0f9cdc2018-05-17 14:09:06 +000010783 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
drh93b77312018-03-05 20:20:22 +000010784 }else{
drhd0f9cdc2018-05-17 14:09:06 +000010785 arDotCommand(&data, 1, argv+i, argc-i);
drh93b77312018-03-05 20:20:22 +000010786 }
drhda57d962018-03-05 19:34:05 +000010787 readStdin = 0;
10788 break;
10789#endif
drh2ce15c32017-07-11 13:34:40 +000010790 }else{
10791 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
10792 raw_printf(stderr,"Use -help for a list of options.\n");
10793 return 1;
10794 }
10795 data.cMode = data.mode;
10796 }
10797
10798 if( !readStdin ){
10799 /* Run all arguments that do not begin with '-' as if they were separate
10800 ** command-line inputs, except for the argToSkip argument which contains
10801 ** the database filename.
10802 */
10803 for(i=0; i<nCmd; i++){
10804 if( azCmd[i][0]=='.' ){
10805 rc = do_meta_command(azCmd[i], &data);
10806 if( rc ) return rc==2 ? 0 : rc;
10807 }else{
10808 open_db(&data, 0);
drha10b9992018-03-09 15:24:33 +000010809 rc = shell_exec(&data, azCmd[i], &zErrMsg);
drh2ce15c32017-07-11 13:34:40 +000010810 if( zErrMsg!=0 ){
10811 utf8_printf(stderr,"Error: %s\n", zErrMsg);
10812 return rc!=0 ? rc : 1;
10813 }else if( rc!=0 ){
10814 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
10815 return rc;
10816 }
10817 }
10818 }
10819 free(azCmd);
10820 }else{
10821 /* Run commands received from standard input
10822 */
10823 if( stdin_is_interactive ){
10824 char *zHome;
drha9e4be32018-10-10 18:56:40 +000010825 char *zHistory;
drh2ce15c32017-07-11 13:34:40 +000010826 int nHistory;
10827 printf(
10828 "SQLite version %s %.19s\n" /*extra-version-info*/
10829 "Enter \".help\" for usage hints.\n",
10830 sqlite3_libversion(), sqlite3_sourceid()
10831 );
10832 if( warnInmemoryDb ){
10833 printf("Connected to a ");
10834 printBold("transient in-memory database");
10835 printf(".\nUse \".open FILENAME\" to reopen on a "
10836 "persistent database.\n");
10837 }
drha9e4be32018-10-10 18:56:40 +000010838 zHistory = getenv("SQLITE_HISTORY");
10839 if( zHistory ){
10840 zHistory = strdup(zHistory);
10841 }else if( (zHome = find_home_dir(0))!=0 ){
drh2ce15c32017-07-11 13:34:40 +000010842 nHistory = strlen30(zHome) + 20;
10843 if( (zHistory = malloc(nHistory))!=0 ){
10844 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
10845 }
10846 }
10847 if( zHistory ){ shell_read_history(zHistory); }
drh56eb09b2017-07-11 13:59:07 +000010848#if HAVE_READLINE || HAVE_EDITLINE
10849 rl_attempted_completion_function = readline_completion;
10850#elif HAVE_LINENOISE
10851 linenoiseSetCompletionCallback(linenoise_completion);
10852#endif
drh60379d42018-12-13 18:30:01 +000010853 data.in = 0;
10854 rc = process_input(&data);
drh2ce15c32017-07-11 13:34:40 +000010855 if( zHistory ){
drh5a75dd82017-07-18 20:59:40 +000010856 shell_stifle_history(2000);
drh2ce15c32017-07-11 13:34:40 +000010857 shell_write_history(zHistory);
10858 free(zHistory);
10859 }
10860 }else{
drh60379d42018-12-13 18:30:01 +000010861 data.in = stdin;
10862 rc = process_input(&data);
drh2ce15c32017-07-11 13:34:40 +000010863 }
10864 }
10865 set_table_name(&data, 0);
10866 if( data.db ){
10867 session_close_all(&data);
drh9e804032018-05-18 17:11:50 +000010868 close_db(data.db);
drh2ce15c32017-07-11 13:34:40 +000010869 }
10870 sqlite3_free(data.zFreeOnClose);
10871 find_home_dir(1);
drh536c3452018-01-11 00:38:39 +000010872 output_reset(&data);
10873 data.doXdgOpen = 0;
drh13c20932018-01-10 21:41:55 +000010874 clearTempFile(&data);
drh2ce15c32017-07-11 13:34:40 +000010875#if !SQLITE_SHELL_IS_UTF8
drh1f22f622018-05-17 13:29:14 +000010876 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
10877 free(argvToFree);
drh2ce15c32017-07-11 13:34:40 +000010878#endif
drh9e804032018-05-18 17:11:50 +000010879 /* Clear the global data structure so that valgrind will detect memory
10880 ** leaks */
10881 memset(&data, 0, sizeof(data));
drh2ce15c32017-07-11 13:34:40 +000010882 return rc;
10883}