blob: 8f5ed59e9304666a22c1b18d690407db106a2f06 [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/*
21** Warning pragmas copied from msvc.h in the core.
22*/
23#if defined(_MSC_VER)
24#pragma warning(disable : 4054)
25#pragma warning(disable : 4055)
26#pragma warning(disable : 4100)
27#pragma warning(disable : 4127)
28#pragma warning(disable : 4130)
29#pragma warning(disable : 4152)
30#pragma warning(disable : 4189)
31#pragma warning(disable : 4206)
32#pragma warning(disable : 4210)
33#pragma warning(disable : 4232)
34#pragma warning(disable : 4244)
35#pragma warning(disable : 4305)
36#pragma warning(disable : 4306)
37#pragma warning(disable : 4702)
38#pragma warning(disable : 4706)
39#endif /* defined(_MSC_VER) */
40
41/*
42** No support for loadable extensions in VxWorks.
43*/
44#if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
45# define SQLITE_OMIT_LOAD_EXTENSION 1
46#endif
47
48/*
49** Enable large-file support for fopen() and friends on unix.
50*/
51#ifndef SQLITE_DISABLE_LFS
52# define _LARGE_FILE 1
53# ifndef _FILE_OFFSET_BITS
54# define _FILE_OFFSET_BITS 64
55# endif
56# define _LARGEFILE_SOURCE 1
57#endif
58
59#include <stdlib.h>
60#include <string.h>
61#include <stdio.h>
62#include <assert.h>
63#include "sqlite3.h"
64#if SQLITE_USER_AUTHENTICATION
65# include "sqlite3userauth.h"
66#endif
67#include <ctype.h>
68#include <stdarg.h>
69
70#if !defined(_WIN32) && !defined(WIN32)
71# include <signal.h>
72# if !defined(__RTP__) && !defined(_WRS_KERNEL)
73# include <pwd.h>
74# endif
75# include <unistd.h>
76# include <sys/types.h>
77#endif
78
79#if HAVE_READLINE
80# include <readline/readline.h>
81# include <readline/history.h>
82#endif
83
84#if HAVE_EDITLINE
85# include <editline/readline.h>
86#endif
87
88#if HAVE_EDITLINE || HAVE_READLINE
89
90# define shell_add_history(X) add_history(X)
91# define shell_read_history(X) read_history(X)
92# define shell_write_history(X) write_history(X)
93# define shell_stifle_history(X) stifle_history(X)
94# define shell_readline(X) readline(X)
95
96#elif HAVE_LINENOISE
97
98# include "linenoise.h"
99# define shell_add_history(X) linenoiseHistoryAdd(X)
100# define shell_read_history(X) linenoiseHistoryLoad(X)
101# define shell_write_history(X) linenoiseHistorySave(X)
102# define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
103# define shell_readline(X) linenoise(X)
104
105#else
106
107# define shell_read_history(X)
108# define shell_write_history(X)
109# define shell_stifle_history(X)
110
111# define SHELL_USE_LOCAL_GETLINE 1
112#endif
113
114
115#if defined(_WIN32) || defined(WIN32)
116# include <io.h>
117# include <fcntl.h>
118# define isatty(h) _isatty(h)
119# ifndef access
120# define access(f,m) _access((f),(m))
121# endif
122# undef popen
123# define popen _popen
124# undef pclose
125# define pclose _pclose
126#else
127 /* Make sure isatty() has a prototype. */
128 extern int isatty(int);
129
130# if !defined(__RTP__) && !defined(_WRS_KERNEL)
131 /* popen and pclose are not C89 functions and so are
132 ** sometimes omitted from the <stdio.h> header */
133 extern FILE *popen(const char*,const char*);
134 extern int pclose(FILE*);
135# else
136# define SQLITE_OMIT_POPEN 1
137# endif
138#endif
139
140#if defined(_WIN32_WCE)
141/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
142 * thus we always assume that we have a console. That can be
143 * overridden with the -batch command line option.
144 */
145#define isatty(x) 1
146#endif
147
148/* ctype macros that work with signed characters */
149#define IsSpace(X) isspace((unsigned char)X)
150#define IsDigit(X) isdigit((unsigned char)X)
151#define ToLower(X) (char)tolower((unsigned char)X)
152
153#if defined(_WIN32) || defined(WIN32)
154#include <windows.h>
155
156/* string conversion routines only needed on Win32 */
157extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
158extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
159extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
160extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
161#endif
162
163/* On Windows, we normally run with output mode of TEXT so that \n characters
164** are automatically translated into \r\n. However, this behavior needs
165** to be disabled in some cases (ex: when generating CSV output and when
166** rendering quoted strings that contain \n characters). The following
167** routines take care of that.
168*/
169#if defined(_WIN32) || defined(WIN32)
170static void setBinaryMode(FILE *file, int isOutput){
171 if( isOutput ) fflush(file);
172 _setmode(_fileno(file), _O_BINARY);
173}
174static void setTextMode(FILE *file, int isOutput){
175 if( isOutput ) fflush(file);
176 _setmode(_fileno(file), _O_TEXT);
177}
178#else
179# define setBinaryMode(X,Y)
180# define setTextMode(X,Y)
181#endif
182
183
184/* True if the timer is enabled */
185static int enableTimer = 0;
186
187/* Return the current wall-clock time */
188static sqlite3_int64 timeOfDay(void){
189 static sqlite3_vfs *clockVfs = 0;
190 sqlite3_int64 t;
191 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
192 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
193 clockVfs->xCurrentTimeInt64(clockVfs, &t);
194 }else{
195 double r;
196 clockVfs->xCurrentTime(clockVfs, &r);
197 t = (sqlite3_int64)(r*86400000.0);
198 }
199 return t;
200}
201
202#if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
203#include <sys/time.h>
204#include <sys/resource.h>
205
206/* VxWorks does not support getrusage() as far as we can determine */
207#if defined(_WRS_KERNEL) || defined(__RTP__)
208struct rusage {
209 struct timeval ru_utime; /* user CPU time used */
210 struct timeval ru_stime; /* system CPU time used */
211};
212#define getrusage(A,B) memset(B,0,sizeof(*B))
213#endif
214
215/* Saved resource information for the beginning of an operation */
216static struct rusage sBegin; /* CPU time at start */
217static sqlite3_int64 iBegin; /* Wall-clock time at start */
218
219/*
220** Begin timing an operation
221*/
222static void beginTimer(void){
223 if( enableTimer ){
224 getrusage(RUSAGE_SELF, &sBegin);
225 iBegin = timeOfDay();
226 }
227}
228
229/* Return the difference of two time_structs in seconds */
230static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
231 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
232 (double)(pEnd->tv_sec - pStart->tv_sec);
233}
234
235/*
236** Print the timing results.
237*/
238static void endTimer(void){
239 if( enableTimer ){
240 sqlite3_int64 iEnd = timeOfDay();
241 struct rusage sEnd;
242 getrusage(RUSAGE_SELF, &sEnd);
243 printf("Run Time: real %.3f user %f sys %f\n",
244 (iEnd - iBegin)*0.001,
245 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
246 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
247 }
248}
249
250#define BEGIN_TIMER beginTimer()
251#define END_TIMER endTimer()
252#define HAS_TIMER 1
253
254#elif (defined(_WIN32) || defined(WIN32))
255
256/* Saved resource information for the beginning of an operation */
257static HANDLE hProcess;
258static FILETIME ftKernelBegin;
259static FILETIME ftUserBegin;
260static sqlite3_int64 ftWallBegin;
261typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
262 LPFILETIME, LPFILETIME);
263static GETPROCTIMES getProcessTimesAddr = NULL;
264
265/*
266** Check to see if we have timer support. Return 1 if necessary
267** support found (or found previously).
268*/
269static int hasTimer(void){
270 if( getProcessTimesAddr ){
271 return 1;
272 } else {
273 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
274 ** versions. See if the version we are running on has it, and if it
275 ** does, save off a pointer to it and the current process handle.
276 */
277 hProcess = GetCurrentProcess();
278 if( hProcess ){
279 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
280 if( NULL != hinstLib ){
281 getProcessTimesAddr =
282 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
283 if( NULL != getProcessTimesAddr ){
284 return 1;
285 }
286 FreeLibrary(hinstLib);
287 }
288 }
289 }
290 return 0;
291}
292
293/*
294** Begin timing an operation
295*/
296static void beginTimer(void){
297 if( enableTimer && getProcessTimesAddr ){
298 FILETIME ftCreation, ftExit;
299 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
300 &ftKernelBegin,&ftUserBegin);
301 ftWallBegin = timeOfDay();
302 }
303}
304
305/* Return the difference of two FILETIME structs in seconds */
306static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
307 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
308 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
309 return (double) ((i64End - i64Start) / 10000000.0);
310}
311
312/*
313** Print the timing results.
314*/
315static void endTimer(void){
316 if( enableTimer && getProcessTimesAddr){
317 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
318 sqlite3_int64 ftWallEnd = timeOfDay();
319 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
320 printf("Run Time: real %.3f user %f sys %f\n",
321 (ftWallEnd - ftWallBegin)*0.001,
322 timeDiff(&ftUserBegin, &ftUserEnd),
323 timeDiff(&ftKernelBegin, &ftKernelEnd));
324 }
325}
326
327#define BEGIN_TIMER beginTimer()
328#define END_TIMER endTimer()
329#define HAS_TIMER hasTimer()
330
331#else
332#define BEGIN_TIMER
333#define END_TIMER
334#define HAS_TIMER 0
335#endif
336
337/*
338** Used to prevent warnings about unused parameters
339*/
340#define UNUSED_PARAMETER(x) (void)(x)
341
342/*
343** If the following flag is set, then command execution stops
344** at an error if we are not interactive.
345*/
346static int bail_on_error = 0;
347
348/*
349** Threat stdin as an interactive input if the following variable
350** is true. Otherwise, assume stdin is connected to a file or pipe.
351*/
352static int stdin_is_interactive = 1;
353
354/*
355** On Windows systems we have to know if standard output is a console
356** in order to translate UTF-8 into MBCS. The following variable is
357** true if translation is required.
358*/
359static int stdout_is_console = 1;
360
361/*
362** The following is the open SQLite database. We make a pointer
363** to this database a static variable so that it can be accessed
364** by the SIGINT handler to interrupt database processing.
365*/
366static sqlite3 *globalDb = 0;
367
368/*
369** True if an interrupt (Control-C) has been received.
370*/
371static volatile int seenInterrupt = 0;
372
373/*
374** This is the name of our program. It is set in main(), used
375** in a number of other places, mostly for error messages.
376*/
377static char *Argv0;
378
379/*
380** Prompt strings. Initialized in main. Settable with
381** .prompt main continue
382*/
383static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
384static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
385
386/*
387** Render output like fprintf(). Except, if the output is going to the
388** console and if this is running on a Windows machine, translate the
389** output from UTF-8 into MBCS.
390*/
391#if defined(_WIN32) || defined(WIN32)
392void utf8_printf(FILE *out, const char *zFormat, ...){
393 va_list ap;
394 va_start(ap, zFormat);
395 if( stdout_is_console && (out==stdout || out==stderr) ){
396 char *z1 = sqlite3_vmprintf(zFormat, ap);
397 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
398 sqlite3_free(z1);
399 fputs(z2, out);
400 sqlite3_free(z2);
401 }else{
402 vfprintf(out, zFormat, ap);
403 }
404 va_end(ap);
405}
406#elif !defined(utf8_printf)
407# define utf8_printf fprintf
408#endif
409
410/*
411** Render output like fprintf(). This should not be used on anything that
412** includes string formatting (e.g. "%s").
413*/
414#if !defined(raw_printf)
415# define raw_printf fprintf
416#endif
417
418/*
419** Write I/O traces to the following stream.
420*/
421#ifdef SQLITE_ENABLE_IOTRACE
422static FILE *iotrace = 0;
423#endif
424
425/*
426** This routine works like printf in that its first argument is a
427** format string and subsequent arguments are values to be substituted
428** in place of % fields. The result of formatting this string
429** is written to iotrace.
430*/
431#ifdef SQLITE_ENABLE_IOTRACE
432static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
433 va_list ap;
434 char *z;
435 if( iotrace==0 ) return;
436 va_start(ap, zFormat);
437 z = sqlite3_vmprintf(zFormat, ap);
438 va_end(ap);
439 utf8_printf(iotrace, "%s", z);
440 sqlite3_free(z);
441}
442#endif
443
444/*
445** Output string zUtf to stream pOut as w characters. If w is negative,
446** then right-justify the text. W is the width in UTF-8 characters, not
447** in bytes. This is different from the %*.*s specification in printf
448** since with %*.*s the width is measured in bytes, not characters.
449*/
450static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
451 int i;
452 int n;
453 int aw = w<0 ? -w : w;
454 char zBuf[1000];
455 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
456 for(i=n=0; zUtf[i]; i++){
457 if( (zUtf[i]&0xc0)!=0x80 ){
458 n++;
459 if( n==aw ){
460 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
461 break;
462 }
463 }
464 }
465 if( n>=aw ){
466 utf8_printf(pOut, "%.*s", i, zUtf);
467 }else if( w<0 ){
468 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
469 }else{
470 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
471 }
472}
473
474
475/*
476** Determines if a string is a number of not.
477*/
478static int isNumber(const char *z, int *realnum){
479 if( *z=='-' || *z=='+' ) z++;
480 if( !IsDigit(*z) ){
481 return 0;
482 }
483 z++;
484 if( realnum ) *realnum = 0;
485 while( IsDigit(*z) ){ z++; }
486 if( *z=='.' ){
487 z++;
488 if( !IsDigit(*z) ) return 0;
489 while( IsDigit(*z) ){ z++; }
490 if( realnum ) *realnum = 1;
491 }
492 if( *z=='e' || *z=='E' ){
493 z++;
494 if( *z=='+' || *z=='-' ) z++;
495 if( !IsDigit(*z) ) return 0;
496 while( IsDigit(*z) ){ z++; }
497 if( realnum ) *realnum = 1;
498 }
499 return *z==0;
500}
501
502/*
503** Compute a string length that is limited to what can be stored in
504** lower 30 bits of a 32-bit signed integer.
505*/
506static int strlen30(const char *z){
507 const char *z2 = z;
508 while( *z2 ){ z2++; }
509 return 0x3fffffff & (int)(z2 - z);
510}
511
512/*
513** Return the length of a string in characters. Multibyte UTF8 characters
514** count as a single character.
515*/
516static int strlenChar(const char *z){
517 int n = 0;
518 while( *z ){
519 if( (0xc0&*(z++))!=0x80 ) n++;
520 }
521 return n;
522}
523
524/*
525** This routine reads a line of text from FILE in, stores
526** the text in memory obtained from malloc() and returns a pointer
527** to the text. NULL is returned at end of file, or if malloc()
528** fails.
529**
530** If zLine is not NULL then it is a malloced buffer returned from
531** a previous call to this routine that may be reused.
532*/
533static char *local_getline(char *zLine, FILE *in){
534 int nLine = zLine==0 ? 0 : 100;
535 int n = 0;
536
537 while( 1 ){
538 if( n+100>nLine ){
539 nLine = nLine*2 + 100;
540 zLine = realloc(zLine, nLine);
541 if( zLine==0 ) return 0;
542 }
543 if( fgets(&zLine[n], nLine - n, in)==0 ){
544 if( n==0 ){
545 free(zLine);
546 return 0;
547 }
548 zLine[n] = 0;
549 break;
550 }
551 while( zLine[n] ) n++;
552 if( n>0 && zLine[n-1]=='\n' ){
553 n--;
554 if( n>0 && zLine[n-1]=='\r' ) n--;
555 zLine[n] = 0;
556 break;
557 }
558 }
559#if defined(_WIN32) || defined(WIN32)
560 /* For interactive input on Windows systems, translate the
561 ** multi-byte characterset characters into UTF-8. */
562 if( stdin_is_interactive && in==stdin ){
563 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
564 if( zTrans ){
565 int nTrans = strlen30(zTrans)+1;
566 if( nTrans>nLine ){
567 zLine = realloc(zLine, nTrans);
568 if( zLine==0 ){
569 sqlite3_free(zTrans);
570 return 0;
571 }
572 }
573 memcpy(zLine, zTrans, nTrans);
574 sqlite3_free(zTrans);
575 }
576 }
577#endif /* defined(_WIN32) || defined(WIN32) */
578 return zLine;
579}
580
581/*
582** Retrieve a single line of input text.
583**
584** If in==0 then read from standard input and prompt before each line.
585** If isContinuation is true, then a continuation prompt is appropriate.
586** If isContinuation is zero, then the main prompt should be used.
587**
588** If zPrior is not NULL then it is a buffer from a prior call to this
589** routine that can be reused.
590**
591** The result is stored in space obtained from malloc() and must either
592** be freed by the caller or else passed back into this routine via the
593** zPrior argument for reuse.
594*/
595static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
596 char *zPrompt;
597 char *zResult;
598 if( in!=0 ){
599 zResult = local_getline(zPrior, in);
600 }else{
601 zPrompt = isContinuation ? continuePrompt : mainPrompt;
602#if SHELL_USE_LOCAL_GETLINE
603 printf("%s", zPrompt);
604 fflush(stdout);
605 zResult = local_getline(zPrior, stdin);
606#else
607 free(zPrior);
608 zResult = shell_readline(zPrompt);
609 if( zResult && *zResult ) shell_add_history(zResult);
610#endif
611 }
612 return zResult;
613}
614/*
615** A variable length string to which one can append text.
616*/
617typedef struct ShellText ShellText;
618struct ShellText {
619 char *z;
620 int n;
621 int nAlloc;
622};
623
624/*
625** Initialize and destroy a ShellText object
626*/
627static void initText(ShellText *p){
628 memset(p, 0, sizeof(*p));
629}
630static void freeText(ShellText *p){
631 free(p->z);
632 initText(p);
633}
634
635/* zIn is either a pointer to a NULL-terminated string in memory obtained
636** from malloc(), or a NULL pointer. The string pointed to by zAppend is
637** added to zIn, and the result returned in memory obtained from malloc().
638** zIn, if it was not NULL, is freed.
639**
640** If the third argument, quote, is not '\0', then it is used as a
641** quote character for zAppend.
642*/
643static void appendText(ShellText *p, char const *zAppend, char quote){
644 int len;
645 int i;
646 int nAppend = strlen30(zAppend);
647
648 len = nAppend+p->n+1;
649 if( quote ){
650 len += 2;
651 for(i=0; i<nAppend; i++){
652 if( zAppend[i]==quote ) len++;
653 }
654 }
655
656 if( p->n+len>=p->nAlloc ){
657 p->nAlloc = p->nAlloc*2 + len + 20;
658 p->z = realloc(p->z, p->nAlloc);
659 if( p->z==0 ){
660 memset(p, 0, sizeof(*p));
661 return;
662 }
663 }
664
665 if( quote ){
666 char *zCsr = p->z+p->n;
667 *zCsr++ = quote;
668 for(i=0; i<nAppend; i++){
669 *zCsr++ = zAppend[i];
670 if( zAppend[i]==quote ) *zCsr++ = quote;
671 }
672 *zCsr++ = quote;
673 p->n = (int)(zCsr - p->z);
674 *zCsr = '\0';
675 }else{
676 memcpy(p->z+p->n, zAppend, nAppend);
677 p->n += nAppend;
678 p->z[p->n] = '\0';
679 }
680}
681
682/*
683** Attempt to determine if identifier zName needs to be quoted, either
684** because it contains non-alphanumeric characters, or because it is an
685** SQLite keyword. Be conservative in this estimate: When in doubt assume
686** that quoting is required.
687**
688** Return '"' if quoting is required. Return 0 if no quoting is required.
689*/
690static char quoteChar(const char *zName){
691 /* All SQLite keywords, in alphabetical order */
692 static const char *azKeywords[] = {
693 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
694 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
695 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
696 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
697 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
698 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
699 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
700 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
701 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
702 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
703 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
704 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
705 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
706 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
707 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
708 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
709 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
710 "WITH", "WITHOUT",
711 };
712 int i, lwr, upr, mid, c;
713 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
714 for(i=0; zName[i]; i++){
715 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
716 }
717 lwr = 0;
718 upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
719 while( lwr<=upr ){
720 mid = (lwr+upr)/2;
721 c = sqlite3_stricmp(azKeywords[mid], zName);
722 if( c==0 ) return '"';
723 if( c<0 ){
724 lwr = mid+1;
725 }else{
726 upr = mid-1;
727 }
728 }
729 return 0;
730}
731
732/*
733** SQL function: shell_add_schema(S,X)
734**
735** Add the schema name X to the CREATE statement in S and return the result.
736** Examples:
737**
738** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
739**
740** Also works on
741**
742** CREATE INDEX
743** CREATE UNIQUE INDEX
744** CREATE VIEW
745** CREATE TRIGGER
746** CREATE VIRTUAL TABLE
747**
748** This UDF is used by the .schema command to insert the schema name of
749** attached databases into the middle of the sqlite_master.sql field.
750*/
751static void shellAddSchemaName(
752 sqlite3_context *pCtx,
753 int nVal,
754 sqlite3_value **apVal
755){
756 static const char *aPrefix[] = {
757 "TABLE",
758 "INDEX",
759 "UNIQUE INDEX",
760 "VIEW",
761 "TRIGGER",
762 "VIRTUAL TABLE"
763 };
764 int i = 0;
765 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
766 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
767 assert( nVal==2 );
768 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
drh89997982017-07-11 18:11:33 +0000769 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
drh2ce15c32017-07-11 13:34:40 +0000770 int n = strlen30(aPrefix[i]);
771 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
772 char cQuote = quoteChar(zSchema);
773 char *z;
774 if( cQuote ){
775 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
776 }else{
777 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
778 }
779 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
780 return;
781 }
782 }
783 }
784 sqlite3_result_value(pCtx, apVal[0]);
785}
786
787/*
788** The source code for several run-time loadable extensions is inserted
789** below by the ../tool/mkshellc.tcl script. Before processing that included
790** code, we need to override some macros to make the included program code
791** work here in the middle of this regular program.
792*/
793#define SQLITE_EXTENSION_INIT1
drh89997982017-07-11 18:11:33 +0000794#define SQLITE_EXTENSION_INIT2(X) (void)(X)
drh2ce15c32017-07-11 13:34:40 +0000795
796INCLUDE ../ext/misc/shathree.c
797INCLUDE ../ext/misc/fileio.c
drh56eb09b2017-07-11 13:59:07 +0000798INCLUDE ../ext/misc/completion.c
drh2ce15c32017-07-11 13:34:40 +0000799
800#if defined(SQLITE_ENABLE_SESSION)
801/*
802** State information for a single open session
803*/
804typedef struct OpenSession OpenSession;
805struct OpenSession {
806 char *zName; /* Symbolic name for this session */
807 int nFilter; /* Number of xFilter rejection GLOB patterns */
808 char **azFilter; /* Array of xFilter rejection GLOB patterns */
809 sqlite3_session *p; /* The open session */
810};
811#endif
812
813/*
814** Shell output mode information from before ".explain on",
815** saved so that it can be restored by ".explain off"
816*/
817typedef struct SavedModeInfo SavedModeInfo;
818struct SavedModeInfo {
819 int valid; /* Is there legit data in here? */
820 int mode; /* Mode prior to ".explain on" */
821 int showHeader; /* The ".header" setting prior to ".explain on" */
822 int colWidth[100]; /* Column widths prior to ".explain on" */
823};
824
825/*
826** State information about the database connection is contained in an
827** instance of the following structure.
828*/
829typedef struct ShellState ShellState;
830struct ShellState {
831 sqlite3 *db; /* The database */
832 int autoExplain; /* Automatically turn on .explain mode */
833 int autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
834 int statsOn; /* True to display memory stats before each finalize */
835 int scanstatsOn; /* True to display scan stats before each finalize */
836 int outCount; /* Revert to stdout when reaching zero */
837 int cnt; /* Number of records displayed so far */
838 FILE *out; /* Write results here */
839 FILE *traceOut; /* Output for sqlite3_trace() */
840 int nErr; /* Number of errors seen */
841 int mode; /* An output mode setting */
842 int cMode; /* temporary output mode for the current query */
843 int normalMode; /* Output mode before ".explain on" */
844 int writableSchema; /* True if PRAGMA writable_schema=ON */
845 int showHeader; /* True to show column names in List or Column mode */
846 int nCheck; /* Number of ".check" commands run */
847 unsigned shellFlgs; /* Various flags */
848 char *zDestTable; /* Name of destination table when MODE_Insert */
849 char zTestcase[30]; /* Name of current test case */
850 char colSeparator[20]; /* Column separator character for several modes */
851 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
852 int colWidth[100]; /* Requested width of each column when in column mode*/
853 int actualWidth[100]; /* Actual width of each column */
854 char nullValue[20]; /* The text to print when a NULL comes back from
855 ** the database */
856 char outfile[FILENAME_MAX]; /* Filename for *out */
857 const char *zDbFilename; /* name of the database file */
858 char *zFreeOnClose; /* Filename to free when closing */
859 const char *zVfs; /* Name of VFS to use */
860 sqlite3_stmt *pStmt; /* Current statement if any. */
861 FILE *pLog; /* Write log output here */
862 int *aiIndent; /* Array of indents used in MODE_Explain */
863 int nIndent; /* Size of array aiIndent[] */
864 int iIndent; /* Index of current op in aiIndent[] */
865#if defined(SQLITE_ENABLE_SESSION)
866 int nSession; /* Number of active sessions */
867 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
868#endif
869};
870
871/*
872** These are the allowed shellFlgs values
873*/
drhb2a0f752017-08-28 15:51:35 +0000874#define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
875#define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
876#define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
877#define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
878#define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
879#define SHFLG_CountChanges 0x00000020 /* .changes setting */
880#define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
drh2ce15c32017-07-11 13:34:40 +0000881
882/*
883** Macros for testing and setting shellFlgs
884*/
885#define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
886#define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
887#define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
888
889/*
890** These are the allowed modes.
891*/
892#define MODE_Line 0 /* One column per line. Blank line between records */
893#define MODE_Column 1 /* One record per line in neat columns */
894#define MODE_List 2 /* One record per line with a separator */
895#define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
896#define MODE_Html 4 /* Generate an XHTML table */
897#define MODE_Insert 5 /* Generate SQL "insert" statements */
898#define MODE_Quote 6 /* Quote values as for SQL */
899#define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
900#define MODE_Csv 8 /* Quote strings, numbers are plain */
901#define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
902#define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
903#define MODE_Pretty 11 /* Pretty-print schemas */
904
905static const char *modeDescr[] = {
906 "line",
907 "column",
908 "list",
909 "semi",
910 "html",
911 "insert",
912 "quote",
913 "tcl",
914 "csv",
915 "explain",
916 "ascii",
917 "prettyprint",
918};
919
920/*
921** These are the column/row/line separators used by the various
922** import/export modes.
923*/
924#define SEP_Column "|"
925#define SEP_Row "\n"
926#define SEP_Tab "\t"
927#define SEP_Space " "
928#define SEP_Comma ","
929#define SEP_CrLf "\r\n"
930#define SEP_Unit "\x1F"
931#define SEP_Record "\x1E"
932
933/*
934** Number of elements in an array
935*/
936#define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
937
938/*
939** A callback for the sqlite3_log() interface.
940*/
941static void shellLog(void *pArg, int iErrCode, const char *zMsg){
942 ShellState *p = (ShellState*)pArg;
943 if( p->pLog==0 ) return;
944 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
945 fflush(p->pLog);
946}
947
948/*
949** Output the given string as a hex-encoded blob (eg. X'1234' )
950*/
951static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
952 int i;
953 char *zBlob = (char *)pBlob;
954 raw_printf(out,"X'");
955 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
956 raw_printf(out,"'");
957}
958
959/*
960** Find a string that is not found anywhere in z[]. Return a pointer
961** to that string.
962**
963** Try to use zA and zB first. If both of those are already found in z[]
964** then make up some string and store it in the buffer zBuf.
965*/
966static const char *unused_string(
967 const char *z, /* Result must not appear anywhere in z */
968 const char *zA, const char *zB, /* Try these first */
969 char *zBuf /* Space to store a generated string */
970){
971 unsigned i = 0;
972 if( strstr(z, zA)==0 ) return zA;
973 if( strstr(z, zB)==0 ) return zB;
974 do{
975 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
976 }while( strstr(z,zBuf)!=0 );
977 return zBuf;
978}
979
980/*
981** Output the given string as a quoted string using SQL quoting conventions.
982**
983** See also: output_quoted_escaped_string()
984*/
985static void output_quoted_string(FILE *out, const char *z){
986 int i;
987 char c;
988 setBinaryMode(out, 1);
989 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
990 if( c==0 ){
991 utf8_printf(out,"'%s'",z);
992 }else{
993 raw_printf(out, "'");
994 while( *z ){
995 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
996 if( c=='\'' ) i++;
997 if( i ){
998 utf8_printf(out, "%.*s", i, z);
999 z += i;
1000 }
1001 if( c=='\'' ){
1002 raw_printf(out, "'");
1003 continue;
1004 }
1005 if( c==0 ){
1006 break;
1007 }
1008 z++;
1009 }
1010 raw_printf(out, "'");
1011 }
1012 setTextMode(out, 1);
1013}
1014
1015/*
1016** Output the given string as a quoted string using SQL quoting conventions.
1017** Additionallly , escape the "\n" and "\r" characters so that they do not
1018** get corrupted by end-of-line translation facilities in some operating
1019** systems.
1020**
1021** This is like output_quoted_string() but with the addition of the \r\n
1022** escape mechanism.
1023*/
1024static void output_quoted_escaped_string(FILE *out, const char *z){
1025 int i;
1026 char c;
1027 setBinaryMode(out, 1);
1028 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
1029 if( c==0 ){
1030 utf8_printf(out,"'%s'",z);
1031 }else{
1032 const char *zNL = 0;
1033 const char *zCR = 0;
1034 int nNL = 0;
1035 int nCR = 0;
1036 char zBuf1[20], zBuf2[20];
1037 for(i=0; z[i]; i++){
1038 if( z[i]=='\n' ) nNL++;
1039 if( z[i]=='\r' ) nCR++;
1040 }
1041 if( nNL ){
1042 raw_printf(out, "replace(");
1043 zNL = unused_string(z, "\\n", "\\012", zBuf1);
1044 }
1045 if( nCR ){
1046 raw_printf(out, "replace(");
1047 zCR = unused_string(z, "\\r", "\\015", zBuf2);
1048 }
1049 raw_printf(out, "'");
1050 while( *z ){
1051 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
1052 if( c=='\'' ) i++;
1053 if( i ){
1054 utf8_printf(out, "%.*s", i, z);
1055 z += i;
1056 }
1057 if( c=='\'' ){
1058 raw_printf(out, "'");
1059 continue;
1060 }
1061 if( c==0 ){
1062 break;
1063 }
1064 z++;
1065 if( c=='\n' ){
1066 raw_printf(out, "%s", zNL);
1067 continue;
1068 }
1069 raw_printf(out, "%s", zCR);
1070 }
1071 raw_printf(out, "'");
1072 if( nCR ){
1073 raw_printf(out, ",'%s',char(13))", zCR);
1074 }
1075 if( nNL ){
1076 raw_printf(out, ",'%s',char(10))", zNL);
1077 }
1078 }
1079 setTextMode(out, 1);
1080}
1081
1082/*
1083** Output the given string as a quoted according to C or TCL quoting rules.
1084*/
1085static void output_c_string(FILE *out, const char *z){
1086 unsigned int c;
1087 fputc('"', out);
1088 while( (c = *(z++))!=0 ){
1089 if( c=='\\' ){
1090 fputc(c, out);
1091 fputc(c, out);
1092 }else if( c=='"' ){
1093 fputc('\\', out);
1094 fputc('"', out);
1095 }else if( c=='\t' ){
1096 fputc('\\', out);
1097 fputc('t', out);
1098 }else if( c=='\n' ){
1099 fputc('\\', out);
1100 fputc('n', out);
1101 }else if( c=='\r' ){
1102 fputc('\\', out);
1103 fputc('r', out);
1104 }else if( !isprint(c&0xff) ){
1105 raw_printf(out, "\\%03o", c&0xff);
1106 }else{
1107 fputc(c, out);
1108 }
1109 }
1110 fputc('"', out);
1111}
1112
1113/*
1114** Output the given string with characters that are special to
1115** HTML escaped.
1116*/
1117static void output_html_string(FILE *out, const char *z){
1118 int i;
1119 if( z==0 ) z = "";
1120 while( *z ){
1121 for(i=0; z[i]
1122 && z[i]!='<'
1123 && z[i]!='&'
1124 && z[i]!='>'
1125 && z[i]!='\"'
1126 && z[i]!='\'';
1127 i++){}
1128 if( i>0 ){
1129 utf8_printf(out,"%.*s",i,z);
1130 }
1131 if( z[i]=='<' ){
1132 raw_printf(out,"&lt;");
1133 }else if( z[i]=='&' ){
1134 raw_printf(out,"&amp;");
1135 }else if( z[i]=='>' ){
1136 raw_printf(out,"&gt;");
1137 }else if( z[i]=='\"' ){
1138 raw_printf(out,"&quot;");
1139 }else if( z[i]=='\'' ){
1140 raw_printf(out,"&#39;");
1141 }else{
1142 break;
1143 }
1144 z += i + 1;
1145 }
1146}
1147
1148/*
1149** If a field contains any character identified by a 1 in the following
1150** array, then the string must be quoted for CSV.
1151*/
1152static const char needCsvQuote[] = {
1153 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1154 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1155 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1156 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1157 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1161 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1162 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1163 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1164 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1165 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1166 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1167 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1168 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1169};
1170
1171/*
1172** Output a single term of CSV. Actually, p->colSeparator is used for
1173** the separator, which may or may not be a comma. p->nullValue is
1174** the null value. Strings are quoted if necessary. The separator
1175** is only issued if bSep is true.
1176*/
1177static void output_csv(ShellState *p, const char *z, int bSep){
1178 FILE *out = p->out;
1179 if( z==0 ){
1180 utf8_printf(out,"%s",p->nullValue);
1181 }else{
1182 int i;
1183 int nSep = strlen30(p->colSeparator);
1184 for(i=0; z[i]; i++){
1185 if( needCsvQuote[((unsigned char*)z)[i]]
1186 || (z[i]==p->colSeparator[0] &&
1187 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
1188 i = 0;
1189 break;
1190 }
1191 }
1192 if( i==0 ){
1193 putc('"', out);
1194 for(i=0; z[i]; i++){
1195 if( z[i]=='"' ) putc('"', out);
1196 putc(z[i], out);
1197 }
1198 putc('"', out);
1199 }else{
1200 utf8_printf(out, "%s", z);
1201 }
1202 }
1203 if( bSep ){
1204 utf8_printf(p->out, "%s", p->colSeparator);
1205 }
1206}
1207
1208#ifdef SIGINT
1209/*
1210** This routine runs when the user presses Ctrl-C
1211*/
1212static void interrupt_handler(int NotUsed){
1213 UNUSED_PARAMETER(NotUsed);
1214 seenInterrupt++;
1215 if( seenInterrupt>2 ) exit(1);
1216 if( globalDb ) sqlite3_interrupt(globalDb);
1217}
1218#endif
1219
1220#ifndef SQLITE_OMIT_AUTHORIZATION
1221/*
1222** When the ".auth ON" is set, the following authorizer callback is
1223** invoked. It always returns SQLITE_OK.
1224*/
1225static int shellAuth(
1226 void *pClientData,
1227 int op,
1228 const char *zA1,
1229 const char *zA2,
1230 const char *zA3,
1231 const char *zA4
1232){
1233 ShellState *p = (ShellState*)pClientData;
1234 static const char *azAction[] = { 0,
1235 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
1236 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
1237 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
1238 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
1239 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
1240 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
1241 "PRAGMA", "READ", "SELECT",
1242 "TRANSACTION", "UPDATE", "ATTACH",
1243 "DETACH", "ALTER_TABLE", "REINDEX",
1244 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
1245 "FUNCTION", "SAVEPOINT", "RECURSIVE"
1246 };
1247 int i;
1248 const char *az[4];
1249 az[0] = zA1;
1250 az[1] = zA2;
1251 az[2] = zA3;
1252 az[3] = zA4;
1253 utf8_printf(p->out, "authorizer: %s", azAction[op]);
1254 for(i=0; i<4; i++){
1255 raw_printf(p->out, " ");
1256 if( az[i] ){
1257 output_c_string(p->out, az[i]);
1258 }else{
1259 raw_printf(p->out, "NULL");
1260 }
1261 }
1262 raw_printf(p->out, "\n");
1263 return SQLITE_OK;
1264}
1265#endif
1266
1267/*
1268** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
1269**
1270** This routine converts some CREATE TABLE statements for shadow tables
1271** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1272*/
1273static void printSchemaLine(FILE *out, const char *z, const char *zTail){
1274 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
1275 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
1276 }else{
1277 utf8_printf(out, "%s%s", z, zTail);
1278 }
1279}
1280static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
1281 char c = z[n];
1282 z[n] = 0;
1283 printSchemaLine(out, z, zTail);
1284 z[n] = c;
1285}
1286
1287/*
1288** This is the callback routine that the shell
1289** invokes for each row of a query result.
1290*/
1291static int shell_callback(
1292 void *pArg,
1293 int nArg, /* Number of result columns */
1294 char **azArg, /* Text of each result column */
1295 char **azCol, /* Column names */
1296 int *aiType /* Column types */
1297){
1298 int i;
1299 ShellState *p = (ShellState*)pArg;
1300
drhb3c45232017-08-28 14:33:27 +00001301 if( azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00001302 switch( p->cMode ){
1303 case MODE_Line: {
1304 int w = 5;
1305 if( azArg==0 ) break;
1306 for(i=0; i<nArg; i++){
1307 int len = strlen30(azCol[i] ? azCol[i] : "");
1308 if( len>w ) w = len;
1309 }
1310 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
1311 for(i=0; i<nArg; i++){
1312 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
1313 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
1314 }
1315 break;
1316 }
1317 case MODE_Explain:
1318 case MODE_Column: {
1319 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
1320 const int *colWidth;
1321 int showHdr;
1322 char *rowSep;
1323 if( p->cMode==MODE_Column ){
1324 colWidth = p->colWidth;
1325 showHdr = p->showHeader;
1326 rowSep = p->rowSeparator;
1327 }else{
1328 colWidth = aExplainWidths;
1329 showHdr = 1;
1330 rowSep = SEP_Row;
1331 }
1332 if( p->cnt++==0 ){
1333 for(i=0; i<nArg; i++){
1334 int w, n;
1335 if( i<ArraySize(p->colWidth) ){
1336 w = colWidth[i];
1337 }else{
1338 w = 0;
1339 }
1340 if( w==0 ){
1341 w = strlenChar(azCol[i] ? azCol[i] : "");
1342 if( w<10 ) w = 10;
1343 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
1344 if( w<n ) w = n;
1345 }
1346 if( i<ArraySize(p->actualWidth) ){
1347 p->actualWidth[i] = w;
1348 }
1349 if( showHdr ){
1350 utf8_width_print(p->out, w, azCol[i]);
1351 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
1352 }
1353 }
1354 if( showHdr ){
1355 for(i=0; i<nArg; i++){
1356 int w;
1357 if( i<ArraySize(p->actualWidth) ){
1358 w = p->actualWidth[i];
1359 if( w<0 ) w = -w;
1360 }else{
1361 w = 10;
1362 }
1363 utf8_printf(p->out,"%-*.*s%s",w,w,
1364 "----------------------------------------------------------"
1365 "----------------------------------------------------------",
1366 i==nArg-1 ? rowSep : " ");
1367 }
1368 }
1369 }
1370 if( azArg==0 ) break;
1371 for(i=0; i<nArg; i++){
1372 int w;
1373 if( i<ArraySize(p->actualWidth) ){
1374 w = p->actualWidth[i];
1375 }else{
1376 w = 10;
1377 }
1378 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
1379 w = strlenChar(azArg[i]);
1380 }
1381 if( i==1 && p->aiIndent && p->pStmt ){
1382 if( p->iIndent<p->nIndent ){
1383 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
1384 }
1385 p->iIndent++;
1386 }
1387 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
1388 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
1389 }
1390 break;
1391 }
1392 case MODE_Semi: { /* .schema and .fullschema output */
1393 printSchemaLine(p->out, azArg[0], ";\n");
1394 break;
1395 }
1396 case MODE_Pretty: { /* .schema and .fullschema with --indent */
1397 char *z;
1398 int j;
1399 int nParen = 0;
1400 char cEnd = 0;
1401 char c;
1402 int nLine = 0;
1403 assert( nArg==1 );
1404 if( azArg[0]==0 ) break;
1405 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
1406 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
1407 ){
1408 utf8_printf(p->out, "%s;\n", azArg[0]);
1409 break;
1410 }
1411 z = sqlite3_mprintf("%s", azArg[0]);
1412 j = 0;
1413 for(i=0; IsSpace(z[i]); i++){}
1414 for(; (c = z[i])!=0; i++){
1415 if( IsSpace(c) ){
drhc3cbd672017-10-05 19:12:10 +00001416 if( z[j-1]=='\r' ) z[j-1] = '\n';
drh2ce15c32017-07-11 13:34:40 +00001417 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
1418 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
1419 j--;
1420 }
1421 z[j++] = c;
1422 }
1423 while( j>0 && IsSpace(z[j-1]) ){ j--; }
1424 z[j] = 0;
1425 if( strlen30(z)>=79 ){
1426 for(i=j=0; (c = z[i])!=0; i++){
1427 if( c==cEnd ){
1428 cEnd = 0;
1429 }else if( c=='"' || c=='\'' || c=='`' ){
1430 cEnd = c;
1431 }else if( c=='[' ){
1432 cEnd = ']';
1433 }else if( c=='(' ){
1434 nParen++;
1435 }else if( c==')' ){
1436 nParen--;
1437 if( nLine>0 && nParen==0 && j>0 ){
1438 printSchemaLineN(p->out, z, j, "\n");
1439 j = 0;
1440 }
1441 }
1442 z[j++] = c;
1443 if( nParen==1 && (c=='(' || c==',' || c=='\n') ){
1444 if( c=='\n' ) j--;
1445 printSchemaLineN(p->out, z, j, "\n ");
1446 j = 0;
1447 nLine++;
1448 while( IsSpace(z[i+1]) ){ i++; }
1449 }
1450 }
1451 z[j] = 0;
1452 }
1453 printSchemaLine(p->out, z, ";\n");
1454 sqlite3_free(z);
1455 break;
1456 }
1457 case MODE_List: {
1458 if( p->cnt++==0 && p->showHeader ){
1459 for(i=0; i<nArg; i++){
1460 utf8_printf(p->out,"%s%s",azCol[i],
1461 i==nArg-1 ? p->rowSeparator : p->colSeparator);
1462 }
1463 }
1464 if( azArg==0 ) break;
1465 for(i=0; i<nArg; i++){
1466 char *z = azArg[i];
1467 if( z==0 ) z = p->nullValue;
1468 utf8_printf(p->out, "%s", z);
1469 if( i<nArg-1 ){
1470 utf8_printf(p->out, "%s", p->colSeparator);
1471 }else{
1472 utf8_printf(p->out, "%s", p->rowSeparator);
1473 }
1474 }
1475 break;
1476 }
1477 case MODE_Html: {
1478 if( p->cnt++==0 && p->showHeader ){
1479 raw_printf(p->out,"<TR>");
1480 for(i=0; i<nArg; i++){
1481 raw_printf(p->out,"<TH>");
1482 output_html_string(p->out, azCol[i]);
1483 raw_printf(p->out,"</TH>\n");
1484 }
1485 raw_printf(p->out,"</TR>\n");
1486 }
1487 if( azArg==0 ) break;
1488 raw_printf(p->out,"<TR>");
1489 for(i=0; i<nArg; i++){
1490 raw_printf(p->out,"<TD>");
1491 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1492 raw_printf(p->out,"</TD>\n");
1493 }
1494 raw_printf(p->out,"</TR>\n");
1495 break;
1496 }
1497 case MODE_Tcl: {
1498 if( p->cnt++==0 && p->showHeader ){
1499 for(i=0; i<nArg; i++){
1500 output_c_string(p->out,azCol[i] ? azCol[i] : "");
1501 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1502 }
1503 utf8_printf(p->out, "%s", p->rowSeparator);
1504 }
1505 if( azArg==0 ) break;
1506 for(i=0; i<nArg; i++){
1507 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
1508 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
1509 }
1510 utf8_printf(p->out, "%s", p->rowSeparator);
1511 break;
1512 }
1513 case MODE_Csv: {
1514 setBinaryMode(p->out, 1);
1515 if( p->cnt++==0 && p->showHeader ){
1516 for(i=0; i<nArg; i++){
1517 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
1518 }
1519 utf8_printf(p->out, "%s", p->rowSeparator);
1520 }
1521 if( nArg>0 ){
1522 for(i=0; i<nArg; i++){
1523 output_csv(p, azArg[i], i<nArg-1);
1524 }
1525 utf8_printf(p->out, "%s", p->rowSeparator);
1526 }
1527 setTextMode(p->out, 1);
1528 break;
1529 }
1530 case MODE_Insert: {
1531 if( azArg==0 ) break;
1532 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
1533 if( p->showHeader ){
1534 raw_printf(p->out,"(");
1535 for(i=0; i<nArg; i++){
1536 if( i>0 ) raw_printf(p->out, ",");
1537 if( quoteChar(azCol[i]) ){
1538 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
1539 utf8_printf(p->out, "%s", z);
1540 sqlite3_free(z);
1541 }else{
1542 raw_printf(p->out, "%s", azCol[i]);
1543 }
1544 }
1545 raw_printf(p->out,")");
1546 }
1547 p->cnt++;
1548 for(i=0; i<nArg; i++){
1549 raw_printf(p->out, i>0 ? "," : " VALUES(");
1550 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1551 utf8_printf(p->out,"NULL");
1552 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1553 if( ShellHasFlag(p, SHFLG_Newlines) ){
1554 output_quoted_string(p->out, azArg[i]);
1555 }else{
1556 output_quoted_escaped_string(p->out, azArg[i]);
1557 }
1558 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1559 utf8_printf(p->out,"%s", azArg[i]);
1560 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1561 char z[50];
1562 double r = sqlite3_column_double(p->pStmt, i);
1563 sqlite3_snprintf(50,z,"%!.20g", r);
1564 raw_printf(p->out, "%s", z);
1565 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1566 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1567 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1568 output_hex_blob(p->out, pBlob, nBlob);
1569 }else if( isNumber(azArg[i], 0) ){
1570 utf8_printf(p->out,"%s", azArg[i]);
1571 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
1572 output_quoted_string(p->out, azArg[i]);
1573 }else{
1574 output_quoted_escaped_string(p->out, azArg[i]);
1575 }
1576 }
1577 raw_printf(p->out,");\n");
1578 break;
1579 }
1580 case MODE_Quote: {
1581 if( azArg==0 ) break;
1582 if( p->cnt==0 && p->showHeader ){
1583 for(i=0; i<nArg; i++){
1584 if( i>0 ) raw_printf(p->out, ",");
1585 output_quoted_string(p->out, azCol[i]);
1586 }
1587 raw_printf(p->out,"\n");
1588 }
1589 p->cnt++;
1590 for(i=0; i<nArg; i++){
1591 if( i>0 ) raw_printf(p->out, ",");
1592 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
1593 utf8_printf(p->out,"NULL");
1594 }else if( aiType && aiType[i]==SQLITE_TEXT ){
1595 output_quoted_string(p->out, azArg[i]);
1596 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
1597 utf8_printf(p->out,"%s", azArg[i]);
1598 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
1599 char z[50];
1600 double r = sqlite3_column_double(p->pStmt, i);
1601 sqlite3_snprintf(50,z,"%!.20g", r);
1602 raw_printf(p->out, "%s", z);
1603 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
1604 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
1605 int nBlob = sqlite3_column_bytes(p->pStmt, i);
1606 output_hex_blob(p->out, pBlob, nBlob);
1607 }else if( isNumber(azArg[i], 0) ){
1608 utf8_printf(p->out,"%s", azArg[i]);
1609 }else{
1610 output_quoted_string(p->out, azArg[i]);
1611 }
1612 }
1613 raw_printf(p->out,"\n");
1614 break;
1615 }
1616 case MODE_Ascii: {
1617 if( p->cnt++==0 && p->showHeader ){
1618 for(i=0; i<nArg; i++){
1619 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1620 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
1621 }
1622 utf8_printf(p->out, "%s", p->rowSeparator);
1623 }
1624 if( azArg==0 ) break;
1625 for(i=0; i<nArg; i++){
1626 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
1627 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
1628 }
1629 utf8_printf(p->out, "%s", p->rowSeparator);
1630 break;
1631 }
1632 }
1633 return 0;
1634}
1635
1636/*
1637** This is the callback routine that the SQLite library
1638** invokes for each row of a query result.
1639*/
1640static int callback(void *pArg, int nArg, char **azArg, char **azCol){
1641 /* since we don't have type info, call the shell_callback with a NULL value */
1642 return shell_callback(pArg, nArg, azArg, azCol, NULL);
1643}
1644
1645/*
1646** This is the callback routine from sqlite3_exec() that appends all
1647** output onto the end of a ShellText object.
1648*/
1649static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
1650 ShellText *p = (ShellText*)pArg;
1651 int i;
1652 UNUSED_PARAMETER(az);
drhb3c45232017-08-28 14:33:27 +00001653 if( azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00001654 if( p->n ) appendText(p, "|", 0);
1655 for(i=0; i<nArg; i++){
1656 if( i ) appendText(p, ",", 0);
1657 if( azArg[i] ) appendText(p, azArg[i], 0);
1658 }
1659 return 0;
1660}
1661
1662/*
1663** Generate an appropriate SELFTEST table in the main database.
1664*/
1665static void createSelftestTable(ShellState *p){
1666 char *zErrMsg = 0;
1667 sqlite3_exec(p->db,
1668 "SAVEPOINT selftest_init;\n"
1669 "CREATE TABLE IF NOT EXISTS selftest(\n"
1670 " tno INTEGER PRIMARY KEY,\n" /* Test number */
1671 " op TEXT,\n" /* Operator: memo run */
1672 " cmd TEXT,\n" /* Command text */
1673 " ans TEXT\n" /* Desired answer */
1674 ");"
1675 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
1676 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
1677 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
1678 " 'memo','Tests generated by --init');\n"
1679 "INSERT INTO [_shell$self]\n"
1680 " SELECT 'run',\n"
1681 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
1682 "FROM sqlite_master ORDER BY 2'',224))',\n"
1683 " hex(sha3_query('SELECT type,name,tbl_name,sql "
1684 "FROM sqlite_master ORDER BY 2',224));\n"
1685 "INSERT INTO [_shell$self]\n"
1686 " SELECT 'run',"
1687 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
1688 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
1689 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
1690 " FROM (\n"
1691 " SELECT name FROM sqlite_master\n"
1692 " WHERE type='table'\n"
1693 " AND name<>'selftest'\n"
1694 " AND coalesce(rootpage,0)>0\n"
1695 " )\n"
1696 " ORDER BY name;\n"
1697 "INSERT INTO [_shell$self]\n"
1698 " VALUES('run','PRAGMA integrity_check','ok');\n"
1699 "INSERT INTO selftest(tno,op,cmd,ans)"
1700 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
1701 "DROP TABLE [_shell$self];"
1702 ,0,0,&zErrMsg);
1703 if( zErrMsg ){
1704 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
1705 sqlite3_free(zErrMsg);
1706 }
1707 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
1708}
1709
1710
1711/*
1712** Set the destination table field of the ShellState structure to
1713** the name of the table given. Escape any quote characters in the
1714** table name.
1715*/
1716static void set_table_name(ShellState *p, const char *zName){
1717 int i, n;
mistachkin2158a0c2017-09-09 00:51:36 +00001718 char cQuote;
drh2ce15c32017-07-11 13:34:40 +00001719 char *z;
1720
1721 if( p->zDestTable ){
1722 free(p->zDestTable);
1723 p->zDestTable = 0;
1724 }
1725 if( zName==0 ) return;
1726 cQuote = quoteChar(zName);
1727 n = strlen30(zName);
1728 if( cQuote ) n += n+2;
1729 z = p->zDestTable = malloc( n+1 );
1730 if( z==0 ){
1731 raw_printf(stderr,"Error: out of memory\n");
1732 exit(1);
1733 }
1734 n = 0;
1735 if( cQuote ) z[n++] = cQuote;
1736 for(i=0; zName[i]; i++){
1737 z[n++] = zName[i];
1738 if( zName[i]==cQuote ) z[n++] = cQuote;
1739 }
1740 if( cQuote ) z[n++] = cQuote;
1741 z[n] = 0;
1742}
1743
1744
1745/*
1746** Execute a query statement that will generate SQL output. Print
1747** the result columns, comma-separated, on a line and then add a
1748** semicolon terminator to the end of that line.
1749**
1750** If the number of columns is 1 and that column contains text "--"
1751** then write the semicolon on a separate line. That way, if a
1752** "--" comment occurs at the end of the statement, the comment
1753** won't consume the semicolon terminator.
1754*/
1755static int run_table_dump_query(
1756 ShellState *p, /* Query context */
1757 const char *zSelect, /* SELECT statement to extract content */
1758 const char *zFirstRow /* Print before first row, if not NULL */
1759){
1760 sqlite3_stmt *pSelect;
1761 int rc;
1762 int nResult;
1763 int i;
1764 const char *z;
1765 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
1766 if( rc!=SQLITE_OK || !pSelect ){
1767 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1768 sqlite3_errmsg(p->db));
1769 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1770 return rc;
1771 }
1772 rc = sqlite3_step(pSelect);
1773 nResult = sqlite3_column_count(pSelect);
1774 while( rc==SQLITE_ROW ){
1775 if( zFirstRow ){
1776 utf8_printf(p->out, "%s", zFirstRow);
1777 zFirstRow = 0;
1778 }
1779 z = (const char*)sqlite3_column_text(pSelect, 0);
1780 utf8_printf(p->out, "%s", z);
1781 for(i=1; i<nResult; i++){
1782 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
1783 }
1784 if( z==0 ) z = "";
1785 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
1786 if( z[0] ){
1787 raw_printf(p->out, "\n;\n");
1788 }else{
1789 raw_printf(p->out, ";\n");
1790 }
1791 rc = sqlite3_step(pSelect);
1792 }
1793 rc = sqlite3_finalize(pSelect);
1794 if( rc!=SQLITE_OK ){
1795 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
1796 sqlite3_errmsg(p->db));
1797 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
1798 }
1799 return rc;
1800}
1801
1802/*
1803** Allocate space and save off current error string.
1804*/
1805static char *save_err_msg(
1806 sqlite3 *db /* Database to query */
1807){
1808 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
1809 char *zErrMsg = sqlite3_malloc64(nErrMsg);
1810 if( zErrMsg ){
1811 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
1812 }
1813 return zErrMsg;
1814}
1815
1816#ifdef __linux__
1817/*
1818** Attempt to display I/O stats on Linux using /proc/PID/io
1819*/
1820static void displayLinuxIoStats(FILE *out){
1821 FILE *in;
1822 char z[200];
1823 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1824 in = fopen(z, "rb");
1825 if( in==0 ) return;
1826 while( fgets(z, sizeof(z), in)!=0 ){
1827 static const struct {
1828 const char *zPattern;
1829 const char *zDesc;
1830 } aTrans[] = {
1831 { "rchar: ", "Bytes received by read():" },
1832 { "wchar: ", "Bytes sent to write():" },
1833 { "syscr: ", "Read() system calls:" },
1834 { "syscw: ", "Write() system calls:" },
1835 { "read_bytes: ", "Bytes read from storage:" },
1836 { "write_bytes: ", "Bytes written to storage:" },
1837 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
1838 };
1839 int i;
1840 for(i=0; i<ArraySize(aTrans); i++){
1841 int n = (int)strlen(aTrans[i].zPattern);
1842 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1843 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
1844 break;
1845 }
1846 }
1847 }
1848 fclose(in);
1849}
1850#endif
1851
1852/*
1853** Display a single line of status using 64-bit values.
1854*/
1855static void displayStatLine(
1856 ShellState *p, /* The shell context */
1857 char *zLabel, /* Label for this one line */
1858 char *zFormat, /* Format for the result */
1859 int iStatusCtrl, /* Which status to display */
1860 int bReset /* True to reset the stats */
1861){
1862 sqlite3_int64 iCur = -1;
1863 sqlite3_int64 iHiwtr = -1;
1864 int i, nPercent;
1865 char zLine[200];
1866 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
1867 for(i=0, nPercent=0; zFormat[i]; i++){
1868 if( zFormat[i]=='%' ) nPercent++;
1869 }
1870 if( nPercent>1 ){
1871 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
1872 }else{
1873 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
1874 }
1875 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
1876}
1877
1878/*
1879** Display memory stats.
1880*/
1881static int display_stats(
1882 sqlite3 *db, /* Database to query */
1883 ShellState *pArg, /* Pointer to ShellState */
1884 int bReset /* True to reset the stats */
1885){
1886 int iCur;
1887 int iHiwtr;
1888
1889 if( pArg && pArg->out ){
1890 displayStatLine(pArg, "Memory Used:",
1891 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
1892 displayStatLine(pArg, "Number of Outstanding Allocations:",
1893 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
1894 if( pArg->shellFlgs & SHFLG_Pagecache ){
1895 displayStatLine(pArg, "Number of Pcache Pages Used:",
1896 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
1897 }
1898 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
1899 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
drh2ce15c32017-07-11 13:34:40 +00001900 displayStatLine(pArg, "Largest Allocation:",
1901 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
1902 displayStatLine(pArg, "Largest Pcache Allocation:",
1903 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
drh2ce15c32017-07-11 13:34:40 +00001904#ifdef YYTRACKMAXSTACKDEPTH
1905 displayStatLine(pArg, "Deepest Parser Stack:",
1906 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
1907#endif
1908 }
1909
1910 if( pArg && pArg->out && db ){
1911 if( pArg->shellFlgs & SHFLG_Lookaside ){
1912 iHiwtr = iCur = -1;
1913 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
1914 &iCur, &iHiwtr, bReset);
1915 raw_printf(pArg->out,
1916 "Lookaside Slots Used: %d (max %d)\n",
1917 iCur, iHiwtr);
1918 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
1919 &iCur, &iHiwtr, bReset);
1920 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
1921 iHiwtr);
1922 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
1923 &iCur, &iHiwtr, bReset);
1924 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
1925 iHiwtr);
1926 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
1927 &iCur, &iHiwtr, bReset);
1928 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
1929 iHiwtr);
1930 }
1931 iHiwtr = iCur = -1;
1932 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
1933 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
1934 iCur);
1935 iHiwtr = iCur = -1;
1936 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
1937 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
1938 iHiwtr = iCur = -1;
1939 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
1940 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
1941 iHiwtr = iCur = -1;
1942 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
1943 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
1944 iHiwtr = iCur = -1;
1945 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
1946 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
1947 iCur);
1948 iHiwtr = iCur = -1;
1949 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
1950 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
1951 iCur);
1952 }
1953
1954 if( pArg && pArg->out && db && pArg->pStmt ){
1955 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
1956 bReset);
1957 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
1958 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
1959 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
1960 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
1961 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
1962 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
1963 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
1964 }
1965
1966#ifdef __linux__
1967 displayLinuxIoStats(pArg->out);
1968#endif
1969
1970 /* Do not remove this machine readable comment: extra-stats-output-here */
1971
1972 return 0;
1973}
1974
1975/*
1976** Display scan stats.
1977*/
1978static void display_scanstats(
1979 sqlite3 *db, /* Database to query */
1980 ShellState *pArg /* Pointer to ShellState */
1981){
1982#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
1983 UNUSED_PARAMETER(db);
1984 UNUSED_PARAMETER(pArg);
1985#else
1986 int i, k, n, mx;
1987 raw_printf(pArg->out, "-------- scanstats --------\n");
1988 mx = 0;
1989 for(k=0; k<=mx; k++){
1990 double rEstLoop = 1.0;
1991 for(i=n=0; 1; i++){
1992 sqlite3_stmt *p = pArg->pStmt;
1993 sqlite3_int64 nLoop, nVisit;
1994 double rEst;
1995 int iSid;
1996 const char *zExplain;
1997 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
1998 break;
1999 }
2000 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
2001 if( iSid>mx ) mx = iSid;
2002 if( iSid!=k ) continue;
2003 if( n==0 ){
2004 rEstLoop = (double)nLoop;
2005 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
2006 }
2007 n++;
2008 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
2009 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
2010 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
2011 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
2012 rEstLoop *= rEst;
2013 raw_printf(pArg->out,
2014 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2015 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
2016 );
2017 }
2018 }
2019 raw_printf(pArg->out, "---------------------------\n");
2020#endif
2021}
2022
2023/*
2024** Parameter azArray points to a zero-terminated array of strings. zStr
2025** points to a single nul-terminated string. Return non-zero if zStr
2026** is equal, according to strcmp(), to any of the strings in the array.
2027** Otherwise, return zero.
2028*/
2029static int str_in_array(const char *zStr, const char **azArray){
2030 int i;
2031 for(i=0; azArray[i]; i++){
2032 if( 0==strcmp(zStr, azArray[i]) ) return 1;
2033 }
2034 return 0;
2035}
2036
2037/*
2038** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2039** and populate the ShellState.aiIndent[] array with the number of
2040** spaces each opcode should be indented before it is output.
2041**
2042** The indenting rules are:
2043**
2044** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2045** all opcodes that occur between the p2 jump destination and the opcode
2046** itself by 2 spaces.
2047**
2048** * For each "Goto", if the jump destination is earlier in the program
2049** and ends on one of:
2050** Yield SeekGt SeekLt RowSetRead Rewind
2051** or if the P1 parameter is one instead of zero,
2052** then indent all opcodes between the earlier instruction
2053** and "Goto" by 2 spaces.
2054*/
2055static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
2056 const char *zSql; /* The text of the SQL statement */
2057 const char *z; /* Used to check if this is an EXPLAIN */
2058 int *abYield = 0; /* True if op is an OP_Yield */
2059 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
2060 int iOp; /* Index of operation in p->aiIndent[] */
2061
2062 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
2063 "NextIfOpen", "PrevIfOpen", 0 };
2064 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2065 "Rewind", 0 };
2066 const char *azGoto[] = { "Goto", 0 };
2067
2068 /* Try to figure out if this is really an EXPLAIN statement. If this
2069 ** cannot be verified, return early. */
2070 if( sqlite3_column_count(pSql)!=8 ){
2071 p->cMode = p->mode;
2072 return;
2073 }
2074 zSql = sqlite3_sql(pSql);
2075 if( zSql==0 ) return;
2076 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
2077 if( sqlite3_strnicmp(z, "explain", 7) ){
2078 p->cMode = p->mode;
2079 return;
2080 }
2081
2082 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
2083 int i;
2084 int iAddr = sqlite3_column_int(pSql, 0);
2085 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
2086
2087 /* Set p2 to the P2 field of the current opcode. Then, assuming that
2088 ** p2 is an instruction address, set variable p2op to the index of that
2089 ** instruction in the aiIndent[] array. p2 and p2op may be different if
2090 ** the current instruction is part of a sub-program generated by an
2091 ** SQL trigger or foreign key. */
2092 int p2 = sqlite3_column_int(pSql, 3);
2093 int p2op = (p2 + (iOp-iAddr));
2094
2095 /* Grow the p->aiIndent array as required */
2096 if( iOp>=nAlloc ){
2097 if( iOp==0 ){
2098 /* Do further verfication that this is explain output. Abort if
2099 ** it is not */
2100 static const char *explainCols[] = {
2101 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2102 int jj;
2103 for(jj=0; jj<ArraySize(explainCols); jj++){
2104 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
2105 p->cMode = p->mode;
2106 sqlite3_reset(pSql);
2107 return;
2108 }
2109 }
2110 }
2111 nAlloc += 100;
2112 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
2113 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
2114 }
2115 abYield[iOp] = str_in_array(zOp, azYield);
2116 p->aiIndent[iOp] = 0;
2117 p->nIndent = iOp+1;
2118
2119 if( str_in_array(zOp, azNext) ){
2120 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2121 }
2122 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
2123 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
2124 ){
2125 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
2126 }
2127 }
2128
2129 p->iIndent = 0;
2130 sqlite3_free(abYield);
2131 sqlite3_reset(pSql);
2132}
2133
2134/*
2135** Free the array allocated by explain_data_prepare().
2136*/
2137static void explain_data_delete(ShellState *p){
2138 sqlite3_free(p->aiIndent);
2139 p->aiIndent = 0;
2140 p->nIndent = 0;
2141 p->iIndent = 0;
2142}
2143
2144/*
2145** Disable and restore .wheretrace and .selecttrace settings.
2146*/
2147#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2148extern int sqlite3SelectTrace;
2149static int savedSelectTrace;
2150#endif
2151#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2152extern int sqlite3WhereTrace;
2153static int savedWhereTrace;
2154#endif
2155static void disable_debug_trace_modes(void){
2156#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2157 savedSelectTrace = sqlite3SelectTrace;
2158 sqlite3SelectTrace = 0;
2159#endif
2160#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2161 savedWhereTrace = sqlite3WhereTrace;
2162 sqlite3WhereTrace = 0;
2163#endif
2164}
2165static void restore_debug_trace_modes(void){
2166#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2167 sqlite3SelectTrace = savedSelectTrace;
2168#endif
2169#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2170 sqlite3WhereTrace = savedWhereTrace;
2171#endif
2172}
2173
2174/*
2175** Run a prepared statement
2176*/
2177static void exec_prepared_stmt(
2178 ShellState *pArg, /* Pointer to ShellState */
2179 sqlite3_stmt *pStmt, /* Statment to run */
2180 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
2181){
2182 int rc;
2183
2184 /* perform the first step. this will tell us if we
2185 ** have a result set or not and how wide it is.
2186 */
2187 rc = sqlite3_step(pStmt);
2188 /* if we have a result set... */
2189 if( SQLITE_ROW == rc ){
2190 /* if we have a callback... */
2191 if( xCallback ){
2192 /* allocate space for col name ptr, value ptr, and type */
2193 int nCol = sqlite3_column_count(pStmt);
2194 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
2195 if( !pData ){
2196 rc = SQLITE_NOMEM;
2197 }else{
2198 char **azCols = (char **)pData; /* Names of result columns */
2199 char **azVals = &azCols[nCol]; /* Results */
2200 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
2201 int i, x;
2202 assert(sizeof(int) <= sizeof(char *));
2203 /* save off ptrs to column names */
2204 for(i=0; i<nCol; i++){
2205 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
2206 }
2207 do{
2208 /* extract the data and data types */
2209 for(i=0; i<nCol; i++){
2210 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
2211 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
2212 azVals[i] = "";
2213 }else{
2214 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
2215 }
2216 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
2217 rc = SQLITE_NOMEM;
2218 break; /* from for */
2219 }
2220 } /* end for */
2221
2222 /* if data and types extracted successfully... */
2223 if( SQLITE_ROW == rc ){
2224 /* call the supplied callback with the result row data */
2225 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
2226 rc = SQLITE_ABORT;
2227 }else{
2228 rc = sqlite3_step(pStmt);
2229 }
2230 }
2231 } while( SQLITE_ROW == rc );
2232 sqlite3_free(pData);
2233 }
2234 }else{
2235 do{
2236 rc = sqlite3_step(pStmt);
2237 } while( rc == SQLITE_ROW );
2238 }
2239 }
2240}
2241
2242/*
2243** Execute a statement or set of statements. Print
2244** any result rows/columns depending on the current mode
2245** set via the supplied callback.
2246**
2247** This is very similar to SQLite's built-in sqlite3_exec()
2248** function except it takes a slightly different callback
2249** and callback data argument.
2250*/
2251static int shell_exec(
2252 sqlite3 *db, /* An open database */
2253 const char *zSql, /* SQL to be evaluated */
2254 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
2255 /* (not the same as sqlite3_exec) */
2256 ShellState *pArg, /* Pointer to ShellState */
2257 char **pzErrMsg /* Error msg written here */
2258){
2259 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
2260 int rc = SQLITE_OK; /* Return Code */
2261 int rc2;
2262 const char *zLeftover; /* Tail of unprocessed SQL */
2263
2264 if( pzErrMsg ){
2265 *pzErrMsg = NULL;
2266 }
2267
2268 while( zSql[0] && (SQLITE_OK == rc) ){
2269 static const char *zStmtSql;
2270 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
2271 if( SQLITE_OK != rc ){
2272 if( pzErrMsg ){
2273 *pzErrMsg = save_err_msg(db);
2274 }
2275 }else{
2276 if( !pStmt ){
2277 /* this happens for a comment or white-space */
2278 zSql = zLeftover;
2279 while( IsSpace(zSql[0]) ) zSql++;
2280 continue;
2281 }
2282 zStmtSql = sqlite3_sql(pStmt);
2283 if( zStmtSql==0 ) zStmtSql = "";
2284 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
2285
2286 /* save off the prepared statment handle and reset row count */
2287 if( pArg ){
2288 pArg->pStmt = pStmt;
2289 pArg->cnt = 0;
2290 }
2291
2292 /* echo the sql statement if echo on */
2293 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
2294 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
2295 }
2296
2297 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2298 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
2299 sqlite3_stmt *pExplain;
2300 char *zEQP;
2301 disable_debug_trace_modes();
2302 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
2303 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2304 if( rc==SQLITE_OK ){
2305 while( sqlite3_step(pExplain)==SQLITE_ROW ){
2306 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
2307 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
2308 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
2309 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
2310 }
2311 }
2312 sqlite3_finalize(pExplain);
2313 sqlite3_free(zEQP);
2314 if( pArg->autoEQP>=2 ){
2315 /* Also do an EXPLAIN for ".eqp full" mode */
2316 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
2317 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
2318 if( rc==SQLITE_OK ){
2319 pArg->cMode = MODE_Explain;
2320 explain_data_prepare(pArg, pExplain);
2321 exec_prepared_stmt(pArg, pExplain, xCallback);
2322 explain_data_delete(pArg);
2323 }
2324 sqlite3_finalize(pExplain);
2325 sqlite3_free(zEQP);
2326 }
2327 restore_debug_trace_modes();
2328 }
2329
2330 if( pArg ){
2331 pArg->cMode = pArg->mode;
2332 if( pArg->autoExplain
2333 && sqlite3_column_count(pStmt)==8
2334 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
2335 ){
2336 pArg->cMode = MODE_Explain;
2337 }
2338
2339 /* If the shell is currently in ".explain" mode, gather the extra
2340 ** data required to add indents to the output.*/
2341 if( pArg->cMode==MODE_Explain ){
2342 explain_data_prepare(pArg, pStmt);
2343 }
2344 }
2345
2346 exec_prepared_stmt(pArg, pStmt, xCallback);
2347 explain_data_delete(pArg);
2348
2349 /* print usage stats if stats on */
2350 if( pArg && pArg->statsOn ){
2351 display_stats(db, pArg, 0);
2352 }
2353
2354 /* print loop-counters if required */
2355 if( pArg && pArg->scanstatsOn ){
2356 display_scanstats(db, pArg);
2357 }
2358
2359 /* Finalize the statement just executed. If this fails, save a
2360 ** copy of the error message. Otherwise, set zSql to point to the
2361 ** next statement to execute. */
2362 rc2 = sqlite3_finalize(pStmt);
2363 if( rc!=SQLITE_NOMEM ) rc = rc2;
2364 if( rc==SQLITE_OK ){
2365 zSql = zLeftover;
2366 while( IsSpace(zSql[0]) ) zSql++;
2367 }else if( pzErrMsg ){
2368 *pzErrMsg = save_err_msg(db);
2369 }
2370
2371 /* clear saved stmt handle */
2372 if( pArg ){
2373 pArg->pStmt = NULL;
2374 }
2375 }
2376 } /* end while */
2377
2378 return rc;
2379}
2380
2381/*
2382** Release memory previously allocated by tableColumnList().
2383*/
2384static void freeColumnList(char **azCol){
2385 int i;
2386 for(i=1; azCol[i]; i++){
2387 sqlite3_free(azCol[i]);
2388 }
2389 /* azCol[0] is a static string */
2390 sqlite3_free(azCol);
2391}
2392
2393/*
2394** Return a list of pointers to strings which are the names of all
2395** columns in table zTab. The memory to hold the names is dynamically
2396** allocated and must be released by the caller using a subsequent call
2397** to freeColumnList().
2398**
2399** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
2400** value that needs to be preserved, then azCol[0] is filled in with the
2401** name of the rowid column.
2402**
2403** The first regular column in the table is azCol[1]. The list is terminated
2404** by an entry with azCol[i]==0.
2405*/
2406static char **tableColumnList(ShellState *p, const char *zTab){
2407 char **azCol = 0;
2408 sqlite3_stmt *pStmt;
2409 char *zSql;
2410 int nCol = 0;
2411 int nAlloc = 0;
2412 int nPK = 0; /* Number of PRIMARY KEY columns seen */
2413 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
2414 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
2415 int rc;
2416
2417 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
2418 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2419 sqlite3_free(zSql);
2420 if( rc ) return 0;
2421 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2422 if( nCol>=nAlloc-2 ){
2423 nAlloc = nAlloc*2 + nCol + 10;
2424 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
2425 if( azCol==0 ){
2426 raw_printf(stderr, "Error: out of memory\n");
2427 exit(1);
2428 }
2429 }
2430 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
2431 if( sqlite3_column_int(pStmt, 5) ){
2432 nPK++;
2433 if( nPK==1
2434 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
2435 "INTEGER")==0
2436 ){
2437 isIPK = 1;
2438 }else{
2439 isIPK = 0;
2440 }
2441 }
2442 }
2443 sqlite3_finalize(pStmt);
drh4c6cddc2017-10-12 10:28:30 +00002444 if( azCol==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00002445 azCol[0] = 0;
2446 azCol[nCol+1] = 0;
2447
2448 /* The decision of whether or not a rowid really needs to be preserved
2449 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
2450 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
2451 ** rowids on tables where the rowid is inaccessible because there are other
2452 ** columns in the table named "rowid", "_rowid_", and "oid".
2453 */
2454 if( preserveRowid && isIPK ){
2455 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
2456 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
2457 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
2458 ** ROWID aliases. To distinguish these cases, check to see if
2459 ** there is a "pk" entry in "PRAGMA index_list". There will be
2460 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
2461 */
2462 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
2463 " WHERE origin='pk'", zTab);
2464 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2465 sqlite3_free(zSql);
2466 if( rc ){
2467 freeColumnList(azCol);
2468 return 0;
2469 }
2470 rc = sqlite3_step(pStmt);
2471 sqlite3_finalize(pStmt);
2472 preserveRowid = rc==SQLITE_ROW;
2473 }
2474 if( preserveRowid ){
2475 /* Only preserve the rowid if we can find a name to use for the
2476 ** rowid */
2477 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
2478 int i, j;
2479 for(j=0; j<3; j++){
2480 for(i=1; i<=nCol; i++){
2481 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
2482 }
2483 if( i>nCol ){
2484 /* At this point, we know that azRowid[j] is not the name of any
2485 ** ordinary column in the table. Verify that azRowid[j] is a valid
2486 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
2487 ** tables will fail this last check */
2488 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
2489 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
2490 break;
2491 }
2492 }
2493 }
2494 return azCol;
2495}
2496
2497/*
2498** Toggle the reverse_unordered_selects setting.
2499*/
2500static void toggleSelectOrder(sqlite3 *db){
2501 sqlite3_stmt *pStmt = 0;
2502 int iSetting = 0;
2503 char zStmt[100];
2504 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
2505 if( sqlite3_step(pStmt)==SQLITE_ROW ){
2506 iSetting = sqlite3_column_int(pStmt, 0);
2507 }
2508 sqlite3_finalize(pStmt);
2509 sqlite3_snprintf(sizeof(zStmt), zStmt,
2510 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
2511 sqlite3_exec(db, zStmt, 0, 0, 0);
2512}
2513
2514/*
2515** This is a different callback routine used for dumping the database.
2516** Each row received by this callback consists of a table name,
2517** the table type ("index" or "table") and SQL to create the table.
2518** This routine should print text sufficient to recreate the table.
2519*/
2520static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
2521 int rc;
2522 const char *zTable;
2523 const char *zType;
2524 const char *zSql;
2525 ShellState *p = (ShellState *)pArg;
2526
2527 UNUSED_PARAMETER(azNotUsed);
drhb3c45232017-08-28 14:33:27 +00002528 if( nArg!=3 || azArg==0 ) return 0;
drh2ce15c32017-07-11 13:34:40 +00002529 zTable = azArg[0];
2530 zType = azArg[1];
2531 zSql = azArg[2];
2532
2533 if( strcmp(zTable, "sqlite_sequence")==0 ){
2534 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
2535 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
2536 raw_printf(p->out, "ANALYZE sqlite_master;\n");
2537 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
2538 return 0;
2539 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
2540 char *zIns;
2541 if( !p->writableSchema ){
2542 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
2543 p->writableSchema = 1;
2544 }
2545 zIns = sqlite3_mprintf(
2546 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
2547 "VALUES('table','%q','%q',0,'%q');",
2548 zTable, zTable, zSql);
2549 utf8_printf(p->out, "%s\n", zIns);
2550 sqlite3_free(zIns);
2551 return 0;
2552 }else{
2553 printSchemaLine(p->out, zSql, ";\n");
2554 }
2555
2556 if( strcmp(zType, "table")==0 ){
2557 ShellText sSelect;
2558 ShellText sTable;
2559 char **azCol;
2560 int i;
2561 char *savedDestTable;
2562 int savedMode;
2563
2564 azCol = tableColumnList(p, zTable);
2565 if( azCol==0 ){
2566 p->nErr++;
2567 return 0;
2568 }
2569
2570 /* Always quote the table name, even if it appears to be pure ascii,
2571 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
2572 initText(&sTable);
2573 appendText(&sTable, zTable, quoteChar(zTable));
2574 /* If preserving the rowid, add a column list after the table name.
2575 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
2576 ** instead of the usual "INSERT INTO tab VALUES(...)".
2577 */
2578 if( azCol[0] ){
2579 appendText(&sTable, "(", 0);
2580 appendText(&sTable, azCol[0], 0);
2581 for(i=1; azCol[i]; i++){
2582 appendText(&sTable, ",", 0);
2583 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
2584 }
2585 appendText(&sTable, ")", 0);
2586 }
2587
2588 /* Build an appropriate SELECT statement */
2589 initText(&sSelect);
2590 appendText(&sSelect, "SELECT ", 0);
2591 if( azCol[0] ){
2592 appendText(&sSelect, azCol[0], 0);
2593 appendText(&sSelect, ",", 0);
2594 }
2595 for(i=1; azCol[i]; i++){
2596 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
2597 if( azCol[i+1] ){
2598 appendText(&sSelect, ",", 0);
2599 }
2600 }
2601 freeColumnList(azCol);
2602 appendText(&sSelect, " FROM ", 0);
2603 appendText(&sSelect, zTable, quoteChar(zTable));
2604
2605 savedDestTable = p->zDestTable;
2606 savedMode = p->mode;
2607 p->zDestTable = sTable.z;
2608 p->mode = p->cMode = MODE_Insert;
2609 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2610 if( (rc&0xff)==SQLITE_CORRUPT ){
2611 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2612 toggleSelectOrder(p->db);
2613 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
2614 toggleSelectOrder(p->db);
2615 }
2616 p->zDestTable = savedDestTable;
2617 p->mode = savedMode;
2618 freeText(&sTable);
2619 freeText(&sSelect);
2620 if( rc ) p->nErr++;
2621 }
2622 return 0;
2623}
2624
2625/*
2626** Run zQuery. Use dump_callback() as the callback routine so that
2627** the contents of the query are output as SQL statements.
2628**
2629** If we get a SQLITE_CORRUPT error, rerun the query after appending
2630** "ORDER BY rowid DESC" to the end.
2631*/
2632static int run_schema_dump_query(
2633 ShellState *p,
2634 const char *zQuery
2635){
2636 int rc;
2637 char *zErr = 0;
2638 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
2639 if( rc==SQLITE_CORRUPT ){
2640 char *zQ2;
2641 int len = strlen30(zQuery);
2642 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
2643 if( zErr ){
2644 utf8_printf(p->out, "/****** %s ******/\n", zErr);
2645 sqlite3_free(zErr);
2646 zErr = 0;
2647 }
2648 zQ2 = malloc( len+100 );
2649 if( zQ2==0 ) return rc;
2650 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
2651 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
2652 if( rc ){
2653 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
2654 }else{
2655 rc = SQLITE_CORRUPT;
2656 }
2657 sqlite3_free(zErr);
2658 free(zQ2);
2659 }
2660 return rc;
2661}
2662
2663/*
2664** Text of a help message
2665*/
2666static char zHelp[] =
2667#ifndef SQLITE_OMIT_AUTHORIZATION
2668 ".auth ON|OFF Show authorizer callbacks\n"
2669#endif
2670 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
2671 ".bail on|off Stop after hitting an error. Default OFF\n"
2672 ".binary on|off Turn binary output on or off. Default OFF\n"
2673 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
2674 ".changes on|off Show number of rows changed by SQL\n"
2675 ".check GLOB Fail if output since .testcase does not match\n"
2676 ".clone NEWDB Clone data into NEWDB from the existing database\n"
2677 ".databases List names and files of attached databases\n"
2678 ".dbinfo ?DB? Show status information about the database\n"
2679 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
2680 " If TABLE specified, only dump tables matching\n"
2681 " LIKE pattern TABLE.\n"
2682 ".echo on|off Turn command echo on or off\n"
2683 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
2684 ".exit Exit this program\n"
2685/* Because explain mode comes on automatically now, the ".explain" mode
2686** is removed from the help screen. It is still supported for legacy, however */
2687/*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
2688 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
2689 ".headers on|off Turn display of headers on or off\n"
2690 ".help Show this message\n"
2691 ".import FILE TABLE Import data from FILE into TABLE\n"
2692#ifndef SQLITE_OMIT_TEST_CONTROL
2693 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
2694#endif
2695 ".indexes ?TABLE? Show names of all indexes\n"
2696 " If TABLE specified, only show indexes for tables\n"
2697 " matching LIKE pattern TABLE.\n"
2698#ifdef SQLITE_ENABLE_IOTRACE
2699 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
2700#endif
2701 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
2702 ".lint OPTIONS Report potential schema issues. Options:\n"
2703 " fkey-indexes Find missing foreign key indexes\n"
2704#ifndef SQLITE_OMIT_LOAD_EXTENSION
2705 ".load FILE ?ENTRY? Load an extension library\n"
2706#endif
2707 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
2708 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
2709 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
2710 " csv Comma-separated values\n"
2711 " column Left-aligned columns. (See .width)\n"
2712 " html HTML <table> code\n"
2713 " insert SQL insert statements for TABLE\n"
2714 " line One value per line\n"
2715 " list Values delimited by \"|\"\n"
2716 " quote Escape answers as for SQL\n"
2717 " tabs Tab-separated values\n"
2718 " tcl TCL list elements\n"
2719 ".nullvalue STRING Use STRING in place of NULL values\n"
2720 ".once FILENAME Output for the next SQL command only to FILENAME\n"
2721 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
2722 " The --new option starts with an empty file\n"
2723 ".output ?FILENAME? Send output to FILENAME or stdout\n"
2724 ".print STRING... Print literal STRING\n"
2725 ".prompt MAIN CONTINUE Replace the standard prompts\n"
2726 ".quit Exit this program\n"
2727 ".read FILENAME Execute SQL in FILENAME\n"
2728 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
2729 ".save FILE Write in-memory database into FILE\n"
2730 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
2731 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
2732 " Add --indent for pretty-printing\n"
2733 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
2734 ".separator COL ?ROW? Change the column separator and optionally the row\n"
2735 " separator for both the output mode and .import\n"
2736#if defined(SQLITE_ENABLE_SESSION)
2737 ".session CMD ... Create or control sessions\n"
2738#endif
2739 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
2740 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
2741 ".show Show the current values for various settings\n"
2742 ".stats ?on|off? Show stats or turn stats on or off\n"
2743 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
2744 ".tables ?TABLE? List names of tables\n"
2745 " If TABLE specified, only list tables matching\n"
2746 " LIKE pattern TABLE.\n"
2747 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
2748 ".timeout MS Try opening locked tables for MS milliseconds\n"
2749 ".timer on|off Turn SQL timer on or off\n"
2750 ".trace FILE|off Output each SQL statement as it is run\n"
2751 ".vfsinfo ?AUX? Information about the top-level VFS\n"
2752 ".vfslist List all available VFSes\n"
2753 ".vfsname ?AUX? Print the name of the VFS stack\n"
2754 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
2755 " Negative values right-justify\n"
2756;
2757
2758#if defined(SQLITE_ENABLE_SESSION)
2759/*
2760** Print help information for the ".sessions" command
2761*/
2762void session_help(ShellState *p){
2763 raw_printf(p->out,
2764 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
2765 "If ?NAME? is omitted, the first defined session is used.\n"
2766 "Subcommands:\n"
2767 " attach TABLE Attach TABLE\n"
2768 " changeset FILE Write a changeset into FILE\n"
2769 " close Close one session\n"
2770 " enable ?BOOLEAN? Set or query the enable bit\n"
2771 " filter GLOB... Reject tables matching GLOBs\n"
2772 " indirect ?BOOLEAN? Mark or query the indirect status\n"
2773 " isempty Query whether the session is empty\n"
2774 " list List currently open session names\n"
2775 " open DB NAME Open a new session on DB\n"
2776 " patchset FILE Write a patchset into FILE\n"
2777 );
2778}
2779#endif
2780
2781
2782/* Forward reference */
2783static int process_input(ShellState *p, FILE *in);
2784
2785/*
2786** Read the content of file zName into memory obtained from sqlite3_malloc64()
2787** and return a pointer to the buffer. The caller is responsible for freeing
2788** the memory.
2789**
2790** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
2791** read.
2792**
2793** For convenience, a nul-terminator byte is always appended to the data read
2794** from the file before the buffer is returned. This byte is not included in
2795** the final value of (*pnByte), if applicable.
2796**
2797** NULL is returned if any error is encountered. The final value of *pnByte
2798** is undefined in this case.
2799*/
2800static char *readFile(const char *zName, int *pnByte){
2801 FILE *in = fopen(zName, "rb");
2802 long nIn;
2803 size_t nRead;
2804 char *pBuf;
2805 if( in==0 ) return 0;
2806 fseek(in, 0, SEEK_END);
2807 nIn = ftell(in);
2808 rewind(in);
2809 pBuf = sqlite3_malloc64( nIn+1 );
2810 if( pBuf==0 ) return 0;
2811 nRead = fread(pBuf, nIn, 1, in);
2812 fclose(in);
2813 if( nRead!=1 ){
2814 sqlite3_free(pBuf);
2815 return 0;
2816 }
2817 pBuf[nIn] = 0;
2818 if( pnByte ) *pnByte = nIn;
2819 return pBuf;
2820}
2821
2822#if defined(SQLITE_ENABLE_SESSION)
2823/*
2824** Close a single OpenSession object and release all of its associated
2825** resources.
2826*/
2827static void session_close(OpenSession *pSession){
2828 int i;
2829 sqlite3session_delete(pSession->p);
2830 sqlite3_free(pSession->zName);
2831 for(i=0; i<pSession->nFilter; i++){
2832 sqlite3_free(pSession->azFilter[i]);
2833 }
2834 sqlite3_free(pSession->azFilter);
2835 memset(pSession, 0, sizeof(OpenSession));
2836}
2837#endif
2838
2839/*
2840** Close all OpenSession objects and release all associated resources.
2841*/
2842#if defined(SQLITE_ENABLE_SESSION)
2843static void session_close_all(ShellState *p){
2844 int i;
2845 for(i=0; i<p->nSession; i++){
2846 session_close(&p->aSession[i]);
2847 }
2848 p->nSession = 0;
2849}
2850#else
2851# define session_close_all(X)
2852#endif
2853
2854/*
2855** Implementation of the xFilter function for an open session. Omit
2856** any tables named by ".session filter" but let all other table through.
2857*/
2858#if defined(SQLITE_ENABLE_SESSION)
2859static int session_filter(void *pCtx, const char *zTab){
2860 OpenSession *pSession = (OpenSession*)pCtx;
2861 int i;
2862 for(i=0; i<pSession->nFilter; i++){
2863 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
2864 }
2865 return 1;
2866}
2867#endif
2868
2869/*
2870** Make sure the database is open. If it is not, then open it. If
2871** the database fails to open, print an error message and exit.
2872*/
2873static void open_db(ShellState *p, int keepAlive){
2874 if( p->db==0 ){
2875 sqlite3_initialize();
2876 sqlite3_open(p->zDbFilename, &p->db);
2877 globalDb = p->db;
2878 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
2879 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
2880 p->zDbFilename, sqlite3_errmsg(p->db));
2881 if( keepAlive ) return;
2882 exit(1);
2883 }
2884#ifndef SQLITE_OMIT_LOAD_EXTENSION
2885 sqlite3_enable_load_extension(p->db, 1);
2886#endif
2887 sqlite3_fileio_init(p->db, 0, 0);
2888 sqlite3_shathree_init(p->db, 0, 0);
drh56eb09b2017-07-11 13:59:07 +00002889 sqlite3_completion_init(p->db, 0, 0);
drh2ce15c32017-07-11 13:34:40 +00002890 sqlite3_create_function(p->db, "shell_add_schema", 2, SQLITE_UTF8, 0,
2891 shellAddSchemaName, 0, 0);
drh2ce15c32017-07-11 13:34:40 +00002892 }
2893}
2894
drh56eb09b2017-07-11 13:59:07 +00002895#if HAVE_READLINE || HAVE_EDITLINE
2896/*
2897** Readline completion callbacks
2898*/
2899static char *readline_completion_generator(const char *text, int state){
2900 static sqlite3_stmt *pStmt = 0;
2901 char *zRet;
2902 if( state==0 ){
2903 char *zSql;
drh56eb09b2017-07-11 13:59:07 +00002904 sqlite3_finalize(pStmt);
2905 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2906 " FROM completion(%Q) ORDER BY 1", text);
2907 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2908 sqlite3_free(zSql);
2909 }
2910 if( sqlite3_step(pStmt)==SQLITE_ROW ){
drh968d8712017-07-14 00:28:28 +00002911 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
drh56eb09b2017-07-11 13:59:07 +00002912 }else{
2913 sqlite3_finalize(pStmt);
2914 pStmt = 0;
2915 zRet = 0;
2916 }
2917 return zRet;
2918}
2919static char **readline_completion(const char *zText, int iStart, int iEnd){
2920 rl_attempted_completion_over = 1;
2921 return rl_completion_matches(zText, readline_completion_generator);
2922}
2923
2924#elif HAVE_LINENOISE
2925/*
2926** Linenoise completion callback
2927*/
2928static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
2929 int nLine = (int)strlen(zLine);
2930 int i, iStart;
2931 sqlite3_stmt *pStmt = 0;
2932 char *zSql;
2933 char zBuf[1000];
2934
2935 if( nLine>sizeof(zBuf)-30 ) return;
2936 if( zLine[0]=='.' ) return;
2937 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
2938 if( i==nLine-1 ) return;
2939 iStart = i+1;
2940 memcpy(zBuf, zLine, iStart);
2941 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
2942 " FROM completion(%Q,%Q) ORDER BY 1",
2943 &zLine[iStart], zLine);
2944 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
2945 sqlite3_free(zSql);
2946 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
2947 while( sqlite3_step(pStmt)==SQLITE_ROW ){
2948 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
2949 int nCompletion = sqlite3_column_bytes(pStmt, 0);
2950 if( iStart+nCompletion < sizeof(zBuf)-1 ){
2951 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
2952 linenoiseAddCompletion(lc, zBuf);
2953 }
2954 }
2955 sqlite3_finalize(pStmt);
2956}
2957#endif
2958
drh2ce15c32017-07-11 13:34:40 +00002959/*
2960** Do C-language style dequoting.
2961**
2962** \a -> alarm
2963** \b -> backspace
2964** \t -> tab
2965** \n -> newline
2966** \v -> vertical tab
2967** \f -> form feed
2968** \r -> carriage return
2969** \s -> space
2970** \" -> "
2971** \' -> '
2972** \\ -> backslash
2973** \NNN -> ascii character NNN in octal
2974*/
2975static void resolve_backslashes(char *z){
2976 int i, j;
2977 char c;
2978 while( *z && *z!='\\' ) z++;
2979 for(i=j=0; (c = z[i])!=0; i++, j++){
2980 if( c=='\\' && z[i+1]!=0 ){
2981 c = z[++i];
2982 if( c=='a' ){
2983 c = '\a';
2984 }else if( c=='b' ){
2985 c = '\b';
2986 }else if( c=='t' ){
2987 c = '\t';
2988 }else if( c=='n' ){
2989 c = '\n';
2990 }else if( c=='v' ){
2991 c = '\v';
2992 }else if( c=='f' ){
2993 c = '\f';
2994 }else if( c=='r' ){
2995 c = '\r';
2996 }else if( c=='"' ){
2997 c = '"';
2998 }else if( c=='\'' ){
2999 c = '\'';
3000 }else if( c=='\\' ){
3001 c = '\\';
3002 }else if( c>='0' && c<='7' ){
3003 c -= '0';
3004 if( z[i+1]>='0' && z[i+1]<='7' ){
3005 i++;
3006 c = (c<<3) + z[i] - '0';
3007 if( z[i+1]>='0' && z[i+1]<='7' ){
3008 i++;
3009 c = (c<<3) + z[i] - '0';
3010 }
3011 }
3012 }
3013 }
3014 z[j] = c;
3015 }
3016 if( j<i ) z[j] = 0;
3017}
3018
3019/*
3020** Return the value of a hexadecimal digit. Return -1 if the input
3021** is not a hex digit.
3022*/
3023static int hexDigitValue(char c){
3024 if( c>='0' && c<='9' ) return c - '0';
3025 if( c>='a' && c<='f' ) return c - 'a' + 10;
3026 if( c>='A' && c<='F' ) return c - 'A' + 10;
3027 return -1;
3028}
3029
3030/*
3031** Interpret zArg as an integer value, possibly with suffixes.
3032*/
3033static sqlite3_int64 integerValue(const char *zArg){
3034 sqlite3_int64 v = 0;
3035 static const struct { char *zSuffix; int iMult; } aMult[] = {
3036 { "KiB", 1024 },
3037 { "MiB", 1024*1024 },
3038 { "GiB", 1024*1024*1024 },
3039 { "KB", 1000 },
3040 { "MB", 1000000 },
3041 { "GB", 1000000000 },
3042 { "K", 1000 },
3043 { "M", 1000000 },
3044 { "G", 1000000000 },
3045 };
3046 int i;
3047 int isNeg = 0;
3048 if( zArg[0]=='-' ){
3049 isNeg = 1;
3050 zArg++;
3051 }else if( zArg[0]=='+' ){
3052 zArg++;
3053 }
3054 if( zArg[0]=='0' && zArg[1]=='x' ){
3055 int x;
3056 zArg += 2;
3057 while( (x = hexDigitValue(zArg[0]))>=0 ){
3058 v = (v<<4) + x;
3059 zArg++;
3060 }
3061 }else{
3062 while( IsDigit(zArg[0]) ){
3063 v = v*10 + zArg[0] - '0';
3064 zArg++;
3065 }
3066 }
3067 for(i=0; i<ArraySize(aMult); i++){
3068 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
3069 v *= aMult[i].iMult;
3070 break;
3071 }
3072 }
3073 return isNeg? -v : v;
3074}
3075
3076/*
3077** Interpret zArg as either an integer or a boolean value. Return 1 or 0
3078** for TRUE and FALSE. Return the integer value if appropriate.
3079*/
3080static int booleanValue(const char *zArg){
3081 int i;
3082 if( zArg[0]=='0' && zArg[1]=='x' ){
3083 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
3084 }else{
3085 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
3086 }
3087 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
3088 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
3089 return 1;
3090 }
3091 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
3092 return 0;
3093 }
3094 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3095 zArg);
3096 return 0;
3097}
3098
3099/*
3100** Set or clear a shell flag according to a boolean value.
3101*/
3102static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
3103 if( booleanValue(zArg) ){
3104 ShellSetFlag(p, mFlag);
3105 }else{
3106 ShellClearFlag(p, mFlag);
3107 }
3108}
3109
3110/*
3111** Close an output file, assuming it is not stderr or stdout
3112*/
3113static void output_file_close(FILE *f){
3114 if( f && f!=stdout && f!=stderr ) fclose(f);
3115}
3116
3117/*
3118** Try to open an output file. The names "stdout" and "stderr" are
3119** recognized and do the right thing. NULL is returned if the output
3120** filename is "off".
3121*/
3122static FILE *output_file_open(const char *zFile){
3123 FILE *f;
3124 if( strcmp(zFile,"stdout")==0 ){
3125 f = stdout;
3126 }else if( strcmp(zFile, "stderr")==0 ){
3127 f = stderr;
3128 }else if( strcmp(zFile, "off")==0 ){
3129 f = 0;
3130 }else{
3131 f = fopen(zFile, "wb");
3132 if( f==0 ){
3133 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
3134 }
3135 }
3136 return f;
3137}
3138
3139#if !defined(SQLITE_UNTESTABLE)
3140#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3141/*
3142** A routine for handling output from sqlite3_trace().
3143*/
3144static int sql_trace_callback(
3145 unsigned mType,
3146 void *pArg,
3147 void *pP,
3148 void *pX
3149){
3150 FILE *f = (FILE*)pArg;
3151 UNUSED_PARAMETER(mType);
3152 UNUSED_PARAMETER(pP);
3153 if( f ){
3154 const char *z = (const char*)pX;
3155 int i = (int)strlen(z);
3156 while( i>0 && z[i-1]==';' ){ i--; }
3157 utf8_printf(f, "%.*s;\n", i, z);
3158 }
3159 return 0;
3160}
3161#endif
3162#endif
3163
3164/*
3165** A no-op routine that runs with the ".breakpoint" doc-command. This is
3166** a useful spot to set a debugger breakpoint.
3167*/
3168static void test_breakpoint(void){
3169 static int nCall = 0;
3170 nCall++;
3171}
3172
3173/*
3174** An object used to read a CSV and other files for import.
3175*/
3176typedef struct ImportCtx ImportCtx;
3177struct ImportCtx {
3178 const char *zFile; /* Name of the input file */
3179 FILE *in; /* Read the CSV text from this input stream */
3180 char *z; /* Accumulated text for a field */
3181 int n; /* Number of bytes in z */
3182 int nAlloc; /* Space allocated for z[] */
3183 int nLine; /* Current line number */
3184 int bNotFirst; /* True if one or more bytes already read */
3185 int cTerm; /* Character that terminated the most recent field */
3186 int cColSep; /* The column separator character. (Usually ",") */
3187 int cRowSep; /* The row separator character. (Usually "\n") */
3188};
3189
3190/* Append a single byte to z[] */
3191static void import_append_char(ImportCtx *p, int c){
3192 if( p->n+1>=p->nAlloc ){
3193 p->nAlloc += p->nAlloc + 100;
3194 p->z = sqlite3_realloc64(p->z, p->nAlloc);
3195 if( p->z==0 ){
3196 raw_printf(stderr, "out of memory\n");
3197 exit(1);
3198 }
3199 }
3200 p->z[p->n++] = (char)c;
3201}
3202
3203/* Read a single field of CSV text. Compatible with rfc4180 and extended
3204** with the option of having a separator other than ",".
3205**
3206** + Input comes from p->in.
3207** + Store results in p->z of length p->n. Space to hold p->z comes
3208** from sqlite3_malloc64().
3209** + Use p->cSep as the column separator. The default is ",".
3210** + Use p->rSep as the row separator. The default is "\n".
3211** + Keep track of the line number in p->nLine.
3212** + Store the character that terminates the field in p->cTerm. Store
3213** EOF on end-of-file.
3214** + Report syntax errors on stderr
3215*/
3216static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
3217 int c;
3218 int cSep = p->cColSep;
3219 int rSep = p->cRowSep;
3220 p->n = 0;
3221 c = fgetc(p->in);
3222 if( c==EOF || seenInterrupt ){
3223 p->cTerm = EOF;
3224 return 0;
3225 }
3226 if( c=='"' ){
3227 int pc, ppc;
3228 int startLine = p->nLine;
3229 int cQuote = c;
3230 pc = ppc = 0;
3231 while( 1 ){
3232 c = fgetc(p->in);
3233 if( c==rSep ) p->nLine++;
3234 if( c==cQuote ){
3235 if( pc==cQuote ){
3236 pc = 0;
3237 continue;
3238 }
3239 }
3240 if( (c==cSep && pc==cQuote)
3241 || (c==rSep && pc==cQuote)
3242 || (c==rSep && pc=='\r' && ppc==cQuote)
3243 || (c==EOF && pc==cQuote)
3244 ){
3245 do{ p->n--; }while( p->z[p->n]!=cQuote );
3246 p->cTerm = c;
3247 break;
3248 }
3249 if( pc==cQuote && c!='\r' ){
3250 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
3251 p->zFile, p->nLine, cQuote);
3252 }
3253 if( c==EOF ){
3254 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
3255 p->zFile, startLine, cQuote);
3256 p->cTerm = c;
3257 break;
3258 }
3259 import_append_char(p, c);
3260 ppc = pc;
3261 pc = c;
3262 }
3263 }else{
3264 /* If this is the first field being parsed and it begins with the
3265 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
3266 if( (c&0xff)==0xef && p->bNotFirst==0 ){
3267 import_append_char(p, c);
3268 c = fgetc(p->in);
3269 if( (c&0xff)==0xbb ){
3270 import_append_char(p, c);
3271 c = fgetc(p->in);
3272 if( (c&0xff)==0xbf ){
3273 p->bNotFirst = 1;
3274 p->n = 0;
3275 return csv_read_one_field(p);
3276 }
3277 }
3278 }
3279 while( c!=EOF && c!=cSep && c!=rSep ){
3280 import_append_char(p, c);
3281 c = fgetc(p->in);
3282 }
3283 if( c==rSep ){
3284 p->nLine++;
3285 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
3286 }
3287 p->cTerm = c;
3288 }
3289 if( p->z ) p->z[p->n] = 0;
3290 p->bNotFirst = 1;
3291 return p->z;
3292}
3293
3294/* Read a single field of ASCII delimited text.
3295**
3296** + Input comes from p->in.
3297** + Store results in p->z of length p->n. Space to hold p->z comes
3298** from sqlite3_malloc64().
3299** + Use p->cSep as the column separator. The default is "\x1F".
3300** + Use p->rSep as the row separator. The default is "\x1E".
3301** + Keep track of the row number in p->nLine.
3302** + Store the character that terminates the field in p->cTerm. Store
3303** EOF on end-of-file.
3304** + Report syntax errors on stderr
3305*/
3306static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
3307 int c;
3308 int cSep = p->cColSep;
3309 int rSep = p->cRowSep;
3310 p->n = 0;
3311 c = fgetc(p->in);
3312 if( c==EOF || seenInterrupt ){
3313 p->cTerm = EOF;
3314 return 0;
3315 }
3316 while( c!=EOF && c!=cSep && c!=rSep ){
3317 import_append_char(p, c);
3318 c = fgetc(p->in);
3319 }
3320 if( c==rSep ){
3321 p->nLine++;
3322 }
3323 p->cTerm = c;
3324 if( p->z ) p->z[p->n] = 0;
3325 return p->z;
3326}
3327
3328/*
3329** Try to transfer data for table zTable. If an error is seen while
3330** moving forward, try to go backwards. The backwards movement won't
3331** work for WITHOUT ROWID tables.
3332*/
3333static void tryToCloneData(
3334 ShellState *p,
3335 sqlite3 *newDb,
3336 const char *zTable
3337){
3338 sqlite3_stmt *pQuery = 0;
3339 sqlite3_stmt *pInsert = 0;
3340 char *zQuery = 0;
3341 char *zInsert = 0;
3342 int rc;
3343 int i, j, n;
3344 int nTable = (int)strlen(zTable);
3345 int k = 0;
3346 int cnt = 0;
3347 const int spinRate = 10000;
3348
3349 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
3350 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3351 if( rc ){
3352 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3353 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3354 zQuery);
3355 goto end_data_xfer;
3356 }
3357 n = sqlite3_column_count(pQuery);
3358 zInsert = sqlite3_malloc64(200 + nTable + n*3);
3359 if( zInsert==0 ){
3360 raw_printf(stderr, "out of memory\n");
3361 goto end_data_xfer;
3362 }
3363 sqlite3_snprintf(200+nTable,zInsert,
3364 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
3365 i = (int)strlen(zInsert);
3366 for(j=1; j<n; j++){
3367 memcpy(zInsert+i, ",?", 2);
3368 i += 2;
3369 }
3370 memcpy(zInsert+i, ");", 3);
3371 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
3372 if( rc ){
3373 utf8_printf(stderr, "Error %d: %s on [%s]\n",
3374 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
3375 zQuery);
3376 goto end_data_xfer;
3377 }
3378 for(k=0; k<2; k++){
3379 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3380 for(i=0; i<n; i++){
3381 switch( sqlite3_column_type(pQuery, i) ){
3382 case SQLITE_NULL: {
3383 sqlite3_bind_null(pInsert, i+1);
3384 break;
3385 }
3386 case SQLITE_INTEGER: {
3387 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
3388 break;
3389 }
3390 case SQLITE_FLOAT: {
3391 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
3392 break;
3393 }
3394 case SQLITE_TEXT: {
3395 sqlite3_bind_text(pInsert, i+1,
3396 (const char*)sqlite3_column_text(pQuery,i),
3397 -1, SQLITE_STATIC);
3398 break;
3399 }
3400 case SQLITE_BLOB: {
3401 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
3402 sqlite3_column_bytes(pQuery,i),
3403 SQLITE_STATIC);
3404 break;
3405 }
3406 }
3407 } /* End for */
3408 rc = sqlite3_step(pInsert);
3409 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
3410 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
3411 sqlite3_errmsg(newDb));
3412 }
3413 sqlite3_reset(pInsert);
3414 cnt++;
3415 if( (cnt%spinRate)==0 ){
3416 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
3417 fflush(stdout);
3418 }
3419 } /* End while */
3420 if( rc==SQLITE_DONE ) break;
3421 sqlite3_finalize(pQuery);
3422 sqlite3_free(zQuery);
3423 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
3424 zTable);
3425 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3426 if( rc ){
3427 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
3428 break;
3429 }
3430 } /* End for(k=0...) */
3431
3432end_data_xfer:
3433 sqlite3_finalize(pQuery);
3434 sqlite3_finalize(pInsert);
3435 sqlite3_free(zQuery);
3436 sqlite3_free(zInsert);
3437}
3438
3439
3440/*
3441** Try to transfer all rows of the schema that match zWhere. For
3442** each row, invoke xForEach() on the object defined by that row.
3443** If an error is encountered while moving forward through the
3444** sqlite_master table, try again moving backwards.
3445*/
3446static void tryToCloneSchema(
3447 ShellState *p,
3448 sqlite3 *newDb,
3449 const char *zWhere,
3450 void (*xForEach)(ShellState*,sqlite3*,const char*)
3451){
3452 sqlite3_stmt *pQuery = 0;
3453 char *zQuery = 0;
3454 int rc;
3455 const unsigned char *zName;
3456 const unsigned char *zSql;
3457 char *zErrMsg = 0;
3458
3459 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3460 " WHERE %s", zWhere);
3461 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3462 if( rc ){
3463 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3464 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3465 zQuery);
3466 goto end_schema_xfer;
3467 }
3468 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3469 zName = sqlite3_column_text(pQuery, 0);
3470 zSql = sqlite3_column_text(pQuery, 1);
3471 printf("%s... ", zName); fflush(stdout);
3472 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3473 if( zErrMsg ){
3474 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3475 sqlite3_free(zErrMsg);
3476 zErrMsg = 0;
3477 }
3478 if( xForEach ){
3479 xForEach(p, newDb, (const char*)zName);
3480 }
3481 printf("done\n");
3482 }
3483 if( rc!=SQLITE_DONE ){
3484 sqlite3_finalize(pQuery);
3485 sqlite3_free(zQuery);
3486 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
3487 " WHERE %s ORDER BY rowid DESC", zWhere);
3488 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
3489 if( rc ){
3490 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
3491 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
3492 zQuery);
3493 goto end_schema_xfer;
3494 }
3495 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
3496 zName = sqlite3_column_text(pQuery, 0);
3497 zSql = sqlite3_column_text(pQuery, 1);
3498 printf("%s... ", zName); fflush(stdout);
3499 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
3500 if( zErrMsg ){
3501 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
3502 sqlite3_free(zErrMsg);
3503 zErrMsg = 0;
3504 }
3505 if( xForEach ){
3506 xForEach(p, newDb, (const char*)zName);
3507 }
3508 printf("done\n");
3509 }
3510 }
3511end_schema_xfer:
3512 sqlite3_finalize(pQuery);
3513 sqlite3_free(zQuery);
3514}
3515
3516/*
3517** Open a new database file named "zNewDb". Try to recover as much information
3518** as possible out of the main database (which might be corrupt) and write it
3519** into zNewDb.
3520*/
3521static void tryToClone(ShellState *p, const char *zNewDb){
3522 int rc;
3523 sqlite3 *newDb = 0;
3524 if( access(zNewDb,0)==0 ){
3525 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
3526 return;
3527 }
3528 rc = sqlite3_open(zNewDb, &newDb);
3529 if( rc ){
3530 utf8_printf(stderr, "Cannot create output database: %s\n",
3531 sqlite3_errmsg(newDb));
3532 }else{
3533 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
3534 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
3535 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
3536 tryToCloneSchema(p, newDb, "type!='table'", 0);
3537 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
3538 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
3539 }
3540 sqlite3_close(newDb);
3541}
3542
3543/*
3544** Change the output file back to stdout
3545*/
3546static void output_reset(ShellState *p){
3547 if( p->outfile[0]=='|' ){
3548#ifndef SQLITE_OMIT_POPEN
3549 pclose(p->out);
3550#endif
3551 }else{
3552 output_file_close(p->out);
3553 }
3554 p->outfile[0] = 0;
3555 p->out = stdout;
3556}
3557
3558/*
3559** Run an SQL command and return the single integer result.
3560*/
3561static int db_int(ShellState *p, const char *zSql){
3562 sqlite3_stmt *pStmt;
3563 int res = 0;
3564 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
3565 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
3566 res = sqlite3_column_int(pStmt,0);
3567 }
3568 sqlite3_finalize(pStmt);
3569 return res;
3570}
3571
3572/*
3573** Convert a 2-byte or 4-byte big-endian integer into a native integer
3574*/
3575static unsigned int get2byteInt(unsigned char *a){
3576 return (a[0]<<8) + a[1];
3577}
3578static unsigned int get4byteInt(unsigned char *a){
3579 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
3580}
3581
3582/*
3583** Implementation of the ".info" command.
3584**
3585** Return 1 on error, 2 to exit, and 0 otherwise.
3586*/
3587static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
3588 static const struct { const char *zName; int ofst; } aField[] = {
3589 { "file change counter:", 24 },
3590 { "database page count:", 28 },
3591 { "freelist page count:", 36 },
3592 { "schema cookie:", 40 },
3593 { "schema format:", 44 },
3594 { "default cache size:", 48 },
3595 { "autovacuum top root:", 52 },
3596 { "incremental vacuum:", 64 },
3597 { "text encoding:", 56 },
3598 { "user version:", 60 },
3599 { "application id:", 68 },
3600 { "software version:", 96 },
3601 };
3602 static const struct { const char *zName; const char *zSql; } aQuery[] = {
3603 { "number of tables:",
3604 "SELECT count(*) FROM %s WHERE type='table'" },
3605 { "number of indexes:",
3606 "SELECT count(*) FROM %s WHERE type='index'" },
3607 { "number of triggers:",
3608 "SELECT count(*) FROM %s WHERE type='trigger'" },
3609 { "number of views:",
3610 "SELECT count(*) FROM %s WHERE type='view'" },
3611 { "schema size:",
3612 "SELECT total(length(sql)) FROM %s" },
3613 };
drh2ce15c32017-07-11 13:34:40 +00003614 int i;
3615 char *zSchemaTab;
3616 char *zDb = nArg>=2 ? azArg[1] : "main";
drh512e6c32017-10-11 17:51:08 +00003617 sqlite3_stmt *pStmt = 0;
drh2ce15c32017-07-11 13:34:40 +00003618 unsigned char aHdr[100];
3619 open_db(p, 0);
3620 if( p->db==0 ) return 1;
drh512e6c32017-10-11 17:51:08 +00003621 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
3622 -1, &pStmt, 0);
3623 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
3624 if( sqlite3_step(pStmt)==SQLITE_ROW
3625 && sqlite3_column_bytes(pStmt,0)>100
3626 ){
3627 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
3628 sqlite3_finalize(pStmt);
3629 }else{
drh2ce15c32017-07-11 13:34:40 +00003630 raw_printf(stderr, "unable to read database header\n");
drh512e6c32017-10-11 17:51:08 +00003631 sqlite3_finalize(pStmt);
drh2ce15c32017-07-11 13:34:40 +00003632 return 1;
3633 }
3634 i = get2byteInt(aHdr+16);
3635 if( i==1 ) i = 65536;
3636 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
3637 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
3638 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
3639 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
3640 for(i=0; i<ArraySize(aField); i++){
3641 int ofst = aField[i].ofst;
3642 unsigned int val = get4byteInt(aHdr + ofst);
3643 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
3644 switch( ofst ){
3645 case 56: {
3646 if( val==1 ) raw_printf(p->out, " (utf8)");
3647 if( val==2 ) raw_printf(p->out, " (utf16le)");
3648 if( val==3 ) raw_printf(p->out, " (utf16be)");
3649 }
3650 }
3651 raw_printf(p->out, "\n");
3652 }
3653 if( zDb==0 ){
3654 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
3655 }else if( strcmp(zDb,"temp")==0 ){
3656 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
3657 }else{
3658 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
3659 }
3660 for(i=0; i<ArraySize(aQuery); i++){
3661 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
3662 int val = db_int(p, zSql);
3663 sqlite3_free(zSql);
3664 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
3665 }
3666 sqlite3_free(zSchemaTab);
3667 return 0;
3668}
3669
3670/*
3671** Print the current sqlite3_errmsg() value to stderr and return 1.
3672*/
3673static int shellDatabaseError(sqlite3 *db){
3674 const char *zErr = sqlite3_errmsg(db);
3675 utf8_printf(stderr, "Error: %s\n", zErr);
3676 return 1;
3677}
3678
3679/*
3680** Print an out-of-memory message to stderr and return 1.
3681*/
3682static int shellNomemError(void){
3683 raw_printf(stderr, "Error: out of memory\n");
3684 return 1;
3685}
3686
3687/*
3688** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
3689** if they match and FALSE (0) if they do not match.
3690**
3691** Globbing rules:
3692**
3693** '*' Matches any sequence of zero or more characters.
3694**
3695** '?' Matches exactly one character.
3696**
3697** [...] Matches one character from the enclosed list of
3698** characters.
3699**
3700** [^...] Matches one character not in the enclosed list.
3701**
3702** '#' Matches any sequence of one or more digits with an
3703** optional + or - sign in front
3704**
3705** ' ' Any span of whitespace matches any other span of
3706** whitespace.
3707**
3708** Extra whitespace at the end of z[] is ignored.
3709*/
3710static int testcase_glob(const char *zGlob, const char *z){
3711 int c, c2;
3712 int invert;
3713 int seen;
3714
3715 while( (c = (*(zGlob++)))!=0 ){
3716 if( IsSpace(c) ){
3717 if( !IsSpace(*z) ) return 0;
3718 while( IsSpace(*zGlob) ) zGlob++;
3719 while( IsSpace(*z) ) z++;
3720 }else if( c=='*' ){
3721 while( (c=(*(zGlob++))) == '*' || c=='?' ){
3722 if( c=='?' && (*(z++))==0 ) return 0;
3723 }
3724 if( c==0 ){
3725 return 1;
3726 }else if( c=='[' ){
3727 while( *z && testcase_glob(zGlob-1,z)==0 ){
3728 z++;
3729 }
3730 return (*z)!=0;
3731 }
3732 while( (c2 = (*(z++)))!=0 ){
3733 while( c2!=c ){
3734 c2 = *(z++);
3735 if( c2==0 ) return 0;
3736 }
3737 if( testcase_glob(zGlob,z) ) return 1;
3738 }
3739 return 0;
3740 }else if( c=='?' ){
3741 if( (*(z++))==0 ) return 0;
3742 }else if( c=='[' ){
3743 int prior_c = 0;
3744 seen = 0;
3745 invert = 0;
3746 c = *(z++);
3747 if( c==0 ) return 0;
3748 c2 = *(zGlob++);
3749 if( c2=='^' ){
3750 invert = 1;
3751 c2 = *(zGlob++);
3752 }
3753 if( c2==']' ){
3754 if( c==']' ) seen = 1;
3755 c2 = *(zGlob++);
3756 }
3757 while( c2 && c2!=']' ){
3758 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
3759 c2 = *(zGlob++);
3760 if( c>=prior_c && c<=c2 ) seen = 1;
3761 prior_c = 0;
3762 }else{
3763 if( c==c2 ){
3764 seen = 1;
3765 }
3766 prior_c = c2;
3767 }
3768 c2 = *(zGlob++);
3769 }
3770 if( c2==0 || (seen ^ invert)==0 ) return 0;
3771 }else if( c=='#' ){
3772 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
3773 if( !IsDigit(z[0]) ) return 0;
3774 z++;
3775 while( IsDigit(z[0]) ){ z++; }
3776 }else{
3777 if( c!=(*(z++)) ) return 0;
3778 }
3779 }
3780 while( IsSpace(*z) ){ z++; }
3781 return *z==0;
3782}
3783
3784
3785/*
3786** Compare the string as a command-line option with either one or two
3787** initial "-" characters.
3788*/
3789static int optionMatch(const char *zStr, const char *zOpt){
3790 if( zStr[0]!='-' ) return 0;
3791 zStr++;
3792 if( zStr[0]=='-' ) zStr++;
3793 return strcmp(zStr, zOpt)==0;
3794}
3795
3796/*
3797** Delete a file.
3798*/
3799int shellDeleteFile(const char *zFilename){
3800 int rc;
3801#ifdef _WIN32
3802 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
3803 rc = _wunlink(z);
3804 sqlite3_free(z);
3805#else
3806 rc = unlink(zFilename);
3807#endif
3808 return rc;
3809}
3810
3811
3812/*
3813** The implementation of SQL scalar function fkey_collate_clause(), used
3814** by the ".lint fkey-indexes" command. This scalar function is always
3815** called with four arguments - the parent table name, the parent column name,
3816** the child table name and the child column name.
3817**
3818** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
3819**
3820** If either of the named tables or columns do not exist, this function
3821** returns an empty string. An empty string is also returned if both tables
3822** and columns exist but have the same default collation sequence. Or,
3823** if both exist but the default collation sequences are different, this
3824** function returns the string " COLLATE <parent-collation>", where
3825** <parent-collation> is the default collation sequence of the parent column.
3826*/
3827static void shellFkeyCollateClause(
3828 sqlite3_context *pCtx,
3829 int nVal,
3830 sqlite3_value **apVal
3831){
3832 sqlite3 *db = sqlite3_context_db_handle(pCtx);
3833 const char *zParent;
3834 const char *zParentCol;
3835 const char *zParentSeq;
3836 const char *zChild;
3837 const char *zChildCol;
3838 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
3839 int rc;
3840
3841 assert( nVal==4 );
3842 zParent = (const char*)sqlite3_value_text(apVal[0]);
3843 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
3844 zChild = (const char*)sqlite3_value_text(apVal[2]);
3845 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
3846
3847 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
3848 rc = sqlite3_table_column_metadata(
3849 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
3850 );
3851 if( rc==SQLITE_OK ){
3852 rc = sqlite3_table_column_metadata(
3853 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
3854 );
3855 }
3856
3857 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
3858 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
3859 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
3860 sqlite3_free(z);
3861 }
3862}
3863
3864
3865/*
3866** The implementation of dot-command ".lint fkey-indexes".
3867*/
3868static int lintFkeyIndexes(
3869 ShellState *pState, /* Current shell tool state */
3870 char **azArg, /* Array of arguments passed to dot command */
3871 int nArg /* Number of entries in azArg[] */
3872){
3873 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
3874 FILE *out = pState->out; /* Stream to write non-error output to */
3875 int bVerbose = 0; /* If -verbose is present */
3876 int bGroupByParent = 0; /* If -groupbyparent is present */
3877 int i; /* To iterate through azArg[] */
3878 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
3879 int rc; /* Return code */
3880 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
3881
3882 /*
3883 ** This SELECT statement returns one row for each foreign key constraint
3884 ** in the schema of the main database. The column values are:
3885 **
3886 ** 0. The text of an SQL statement similar to:
3887 **
3888 ** "EXPLAIN QUERY PLAN SELECT rowid FROM child_table WHERE child_key=?"
3889 **
3890 ** This is the same SELECT that the foreign keys implementation needs
3891 ** to run internally on child tables. If there is an index that can
3892 ** be used to optimize this query, then it can also be used by the FK
3893 ** implementation to optimize DELETE or UPDATE statements on the parent
3894 ** table.
3895 **
3896 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
3897 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
3898 ** contains an index that can be used to optimize the query.
3899 **
3900 ** 2. Human readable text that describes the child table and columns. e.g.
3901 **
3902 ** "child_table(child_key1, child_key2)"
3903 **
3904 ** 3. Human readable text that describes the parent table and columns. e.g.
3905 **
3906 ** "parent_table(parent_key1, parent_key2)"
3907 **
3908 ** 4. A full CREATE INDEX statement for an index that could be used to
3909 ** optimize DELETE or UPDATE statements on the parent table. e.g.
3910 **
3911 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
3912 **
3913 ** 5. The name of the parent table.
3914 **
3915 ** These six values are used by the C logic below to generate the report.
3916 */
3917 const char *zSql =
3918 "SELECT "
3919 " 'EXPLAIN QUERY PLAN SELECT rowid FROM ' || quote(s.name) || ' WHERE '"
3920 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
3921 " || fkey_collate_clause("
3922 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
3923 ", "
3924 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
3925 " || group_concat('*=?', ' AND ') || ')'"
3926 ", "
3927 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
3928 ", "
3929 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
3930 ", "
3931 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
3932 " || ' ON ' || quote(s.name) || '('"
3933 " || group_concat(quote(f.[from]) ||"
3934 " fkey_collate_clause("
3935 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
3936 " || ');'"
3937 ", "
3938 " f.[table] "
3939 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
3940 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
3941 "GROUP BY s.name, f.id "
3942 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
3943 ;
3944 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
3945
3946 for(i=2; i<nArg; i++){
3947 int n = (int)strlen(azArg[i]);
3948 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
3949 bVerbose = 1;
3950 }
3951 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
3952 bGroupByParent = 1;
3953 zIndent = " ";
3954 }
3955 else{
3956 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
3957 azArg[0], azArg[1]
3958 );
3959 return SQLITE_ERROR;
3960 }
3961 }
3962
3963 /* Register the fkey_collate_clause() SQL function */
3964 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
3965 0, shellFkeyCollateClause, 0, 0
3966 );
3967
3968
3969 if( rc==SQLITE_OK ){
3970 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
3971 }
3972 if( rc==SQLITE_OK ){
3973 sqlite3_bind_int(pSql, 1, bGroupByParent);
3974 }
3975
3976 if( rc==SQLITE_OK ){
3977 int rc2;
3978 char *zPrev = 0;
3979 while( SQLITE_ROW==sqlite3_step(pSql) ){
3980 int res = -1;
3981 sqlite3_stmt *pExplain = 0;
3982 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
3983 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
3984 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
3985 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
3986 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
3987 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
3988
3989 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
3990 if( rc!=SQLITE_OK ) break;
3991 if( SQLITE_ROW==sqlite3_step(pExplain) ){
3992 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
3993 res = (
3994 0==sqlite3_strglob(zGlob, zPlan)
3995 || 0==sqlite3_strglob(zGlobIPK, zPlan)
3996 );
3997 }
3998 rc = sqlite3_finalize(pExplain);
3999 if( rc!=SQLITE_OK ) break;
4000
4001 if( res<0 ){
4002 raw_printf(stderr, "Error: internal error");
4003 break;
4004 }else{
4005 if( bGroupByParent
4006 && (bVerbose || res==0)
4007 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
4008 ){
4009 raw_printf(out, "-- Parent table %s\n", zParent);
4010 sqlite3_free(zPrev);
4011 zPrev = sqlite3_mprintf("%s", zParent);
4012 }
4013
4014 if( res==0 ){
4015 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
4016 }else if( bVerbose ){
4017 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
4018 zIndent, zFrom, zTarget
4019 );
4020 }
4021 }
4022 }
4023 sqlite3_free(zPrev);
4024
4025 if( rc!=SQLITE_OK ){
4026 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4027 }
4028
4029 rc2 = sqlite3_finalize(pSql);
4030 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
4031 rc = rc2;
4032 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4033 }
4034 }else{
4035 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
4036 }
4037
4038 return rc;
4039}
4040
4041/*
4042** Implementation of ".lint" dot command.
4043*/
4044static int lintDotCommand(
4045 ShellState *pState, /* Current shell tool state */
4046 char **azArg, /* Array of arguments passed to dot command */
4047 int nArg /* Number of entries in azArg[] */
4048){
4049 int n;
4050 n = (nArg>=2 ? (int)strlen(azArg[1]) : 0);
4051 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
4052 return lintFkeyIndexes(pState, azArg, nArg);
4053
4054 usage:
4055 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
4056 raw_printf(stderr, "Where sub-commands are:\n");
4057 raw_printf(stderr, " fkey-indexes\n");
4058 return SQLITE_ERROR;
4059}
4060
4061
4062/*
4063** If an input line begins with "." then invoke this routine to
4064** process that line.
4065**
4066** Return 1 on error, 2 to exit, and 0 otherwise.
4067*/
4068static int do_meta_command(char *zLine, ShellState *p){
4069 int h = 1;
4070 int nArg = 0;
4071 int n, c;
4072 int rc = 0;
4073 char *azArg[50];
4074
4075 /* Parse the input line into tokens.
4076 */
4077 while( zLine[h] && nArg<ArraySize(azArg) ){
4078 while( IsSpace(zLine[h]) ){ h++; }
4079 if( zLine[h]==0 ) break;
4080 if( zLine[h]=='\'' || zLine[h]=='"' ){
4081 int delim = zLine[h++];
4082 azArg[nArg++] = &zLine[h];
4083 while( zLine[h] && zLine[h]!=delim ){
4084 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
4085 h++;
4086 }
4087 if( zLine[h]==delim ){
4088 zLine[h++] = 0;
4089 }
4090 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
4091 }else{
4092 azArg[nArg++] = &zLine[h];
4093 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
4094 if( zLine[h] ) zLine[h++] = 0;
4095 resolve_backslashes(azArg[nArg-1]);
4096 }
4097 }
4098
4099 /* Process the input line.
4100 */
4101 if( nArg==0 ) return 0; /* no tokens, no error */
4102 n = strlen30(azArg[0]);
4103 c = azArg[0][0];
4104
4105#ifndef SQLITE_OMIT_AUTHORIZATION
4106 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
4107 if( nArg!=2 ){
4108 raw_printf(stderr, "Usage: .auth ON|OFF\n");
4109 rc = 1;
4110 goto meta_command_exit;
4111 }
4112 open_db(p, 0);
4113 if( booleanValue(azArg[1]) ){
4114 sqlite3_set_authorizer(p->db, shellAuth, p);
4115 }else{
4116 sqlite3_set_authorizer(p->db, 0, 0);
4117 }
4118 }else
4119#endif
4120
4121 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
4122 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
4123 ){
4124 const char *zDestFile = 0;
4125 const char *zDb = 0;
4126 sqlite3 *pDest;
4127 sqlite3_backup *pBackup;
4128 int j;
4129 for(j=1; j<nArg; j++){
4130 const char *z = azArg[j];
4131 if( z[0]=='-' ){
4132 while( z[0]=='-' ) z++;
4133 /* No options to process at this time */
4134 {
4135 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
4136 return 1;
4137 }
4138 }else if( zDestFile==0 ){
4139 zDestFile = azArg[j];
4140 }else if( zDb==0 ){
4141 zDb = zDestFile;
4142 zDestFile = azArg[j];
4143 }else{
4144 raw_printf(stderr, "too many arguments to .backup\n");
4145 return 1;
4146 }
4147 }
4148 if( zDestFile==0 ){
4149 raw_printf(stderr, "missing FILENAME argument on .backup\n");
4150 return 1;
4151 }
4152 if( zDb==0 ) zDb = "main";
4153 rc = sqlite3_open(zDestFile, &pDest);
4154 if( rc!=SQLITE_OK ){
4155 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
4156 sqlite3_close(pDest);
4157 return 1;
4158 }
4159 open_db(p, 0);
4160 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
4161 if( pBackup==0 ){
4162 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4163 sqlite3_close(pDest);
4164 return 1;
4165 }
4166 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
4167 sqlite3_backup_finish(pBackup);
4168 if( rc==SQLITE_DONE ){
4169 rc = 0;
4170 }else{
4171 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
4172 rc = 1;
4173 }
4174 sqlite3_close(pDest);
4175 }else
4176
4177 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
4178 if( nArg==2 ){
4179 bail_on_error = booleanValue(azArg[1]);
4180 }else{
4181 raw_printf(stderr, "Usage: .bail on|off\n");
4182 rc = 1;
4183 }
4184 }else
4185
4186 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
4187 if( nArg==2 ){
4188 if( booleanValue(azArg[1]) ){
4189 setBinaryMode(p->out, 1);
4190 }else{
4191 setTextMode(p->out, 1);
4192 }
4193 }else{
4194 raw_printf(stderr, "Usage: .binary on|off\n");
4195 rc = 1;
4196 }
4197 }else
4198
4199 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
4200 if( nArg==2 ){
4201#if defined(_WIN32) || defined(WIN32)
4202 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
4203 rc = !SetCurrentDirectoryW(z);
4204 sqlite3_free(z);
4205#else
4206 rc = chdir(azArg[1]);
4207#endif
4208 if( rc ){
4209 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
4210 rc = 1;
4211 }
4212 }else{
4213 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
4214 rc = 1;
4215 }
4216 }else
4217
4218 /* The undocumented ".breakpoint" command causes a call to the no-op
4219 ** routine named test_breakpoint().
4220 */
4221 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
4222 test_breakpoint();
4223 }else
4224
4225 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
4226 if( nArg==2 ){
4227 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
4228 }else{
4229 raw_printf(stderr, "Usage: .changes on|off\n");
4230 rc = 1;
4231 }
4232 }else
4233
4234 /* Cancel output redirection, if it is currently set (by .testcase)
4235 ** Then read the content of the testcase-out.txt file and compare against
4236 ** azArg[1]. If there are differences, report an error and exit.
4237 */
4238 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
4239 char *zRes = 0;
4240 output_reset(p);
4241 if( nArg!=2 ){
4242 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
4243 rc = 2;
4244 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
4245 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
4246 rc = 2;
4247 }else if( testcase_glob(azArg[1],zRes)==0 ){
4248 utf8_printf(stderr,
4249 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
4250 p->zTestcase, azArg[1], zRes);
drhf30d3452017-10-17 13:44:46 +00004251 rc = 1;
drh2ce15c32017-07-11 13:34:40 +00004252 }else{
4253 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
4254 p->nCheck++;
4255 }
4256 sqlite3_free(zRes);
4257 }else
4258
4259 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
4260 if( nArg==2 ){
4261 tryToClone(p, azArg[1]);
4262 }else{
4263 raw_printf(stderr, "Usage: .clone FILENAME\n");
4264 rc = 1;
4265 }
4266 }else
4267
4268 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
4269 ShellState data;
4270 char *zErrMsg = 0;
4271 open_db(p, 0);
4272 memcpy(&data, p, sizeof(data));
4273 data.showHeader = 0;
4274 data.cMode = data.mode = MODE_List;
4275 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
4276 data.cnt = 0;
4277 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
4278 callback, &data, &zErrMsg);
4279 if( zErrMsg ){
4280 utf8_printf(stderr,"Error: %s\n", zErrMsg);
4281 sqlite3_free(zErrMsg);
4282 rc = 1;
4283 }
4284 }else
4285
4286 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
4287 rc = shell_dbinfo_command(p, nArg, azArg);
4288 }else
4289
4290 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
4291 const char *zLike = 0;
4292 int i;
4293 int savedShowHeader = p->showHeader;
4294 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
4295 for(i=1; i<nArg; i++){
4296 if( azArg[i][0]=='-' ){
4297 const char *z = azArg[i]+1;
4298 if( z[0]=='-' ) z++;
4299 if( strcmp(z,"preserve-rowids")==0 ){
4300#ifdef SQLITE_OMIT_VIRTUALTABLE
4301 raw_printf(stderr, "The --preserve-rowids option is not compatible"
4302 " with SQLITE_OMIT_VIRTUALTABLE\n");
4303 rc = 1;
4304 goto meta_command_exit;
4305#else
4306 ShellSetFlag(p, SHFLG_PreserveRowid);
4307#endif
4308 }else
4309 if( strcmp(z,"newlines")==0 ){
4310 ShellSetFlag(p, SHFLG_Newlines);
4311 }else
4312 {
4313 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
4314 rc = 1;
4315 goto meta_command_exit;
4316 }
4317 }else if( zLike ){
4318 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
4319 "?--newlines? ?LIKE-PATTERN?\n");
4320 rc = 1;
4321 goto meta_command_exit;
4322 }else{
4323 zLike = azArg[i];
4324 }
4325 }
4326 open_db(p, 0);
4327 /* When playing back a "dump", the content might appear in an order
4328 ** which causes immediate foreign key constraints to be violated.
4329 ** So disable foreign-key constraint enforcement to prevent problems. */
4330 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
4331 raw_printf(p->out, "BEGIN TRANSACTION;\n");
4332 p->writableSchema = 0;
4333 p->showHeader = 0;
4334 /* Set writable_schema=ON since doing so forces SQLite to initialize
4335 ** as much of the schema as it can even if the sqlite_master table is
4336 ** corrupt. */
4337 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
4338 p->nErr = 0;
4339 if( zLike==0 ){
4340 run_schema_dump_query(p,
4341 "SELECT name, type, sql FROM sqlite_master "
4342 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
4343 );
4344 run_schema_dump_query(p,
4345 "SELECT name, type, sql FROM sqlite_master "
4346 "WHERE name=='sqlite_sequence'"
4347 );
4348 run_table_dump_query(p,
4349 "SELECT sql FROM sqlite_master "
4350 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
4351 );
4352 }else{
4353 char *zSql;
4354 zSql = sqlite3_mprintf(
4355 "SELECT name, type, sql FROM sqlite_master "
4356 "WHERE tbl_name LIKE %Q AND type=='table'"
4357 " AND sql NOT NULL", zLike);
4358 run_schema_dump_query(p,zSql);
4359 sqlite3_free(zSql);
4360 zSql = sqlite3_mprintf(
4361 "SELECT sql FROM sqlite_master "
4362 "WHERE sql NOT NULL"
4363 " AND type IN ('index','trigger','view')"
4364 " AND tbl_name LIKE %Q", zLike);
4365 run_table_dump_query(p, zSql, 0);
4366 sqlite3_free(zSql);
4367 }
4368 if( p->writableSchema ){
4369 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
4370 p->writableSchema = 0;
4371 }
4372 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4373 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
4374 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
4375 p->showHeader = savedShowHeader;
4376 }else
4377
4378 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
4379 if( nArg==2 ){
4380 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
4381 }else{
4382 raw_printf(stderr, "Usage: .echo on|off\n");
4383 rc = 1;
4384 }
4385 }else
4386
4387 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
4388 if( nArg==2 ){
4389 if( strcmp(azArg[1],"full")==0 ){
4390 p->autoEQP = 2;
4391 }else{
4392 p->autoEQP = booleanValue(azArg[1]);
4393 }
4394 }else{
4395 raw_printf(stderr, "Usage: .eqp on|off|full\n");
4396 rc = 1;
4397 }
4398 }else
4399
4400 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
4401 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
4402 rc = 2;
4403 }else
4404
4405 /* The ".explain" command is automatic now. It is largely pointless. It
4406 ** retained purely for backwards compatibility */
4407 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
4408 int val = 1;
4409 if( nArg>=2 ){
4410 if( strcmp(azArg[1],"auto")==0 ){
4411 val = 99;
4412 }else{
4413 val = booleanValue(azArg[1]);
4414 }
4415 }
4416 if( val==1 && p->mode!=MODE_Explain ){
4417 p->normalMode = p->mode;
4418 p->mode = MODE_Explain;
4419 p->autoExplain = 0;
4420 }else if( val==0 ){
4421 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4422 p->autoExplain = 0;
4423 }else if( val==99 ){
4424 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
4425 p->autoExplain = 1;
4426 }
4427 }else
4428
4429 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
4430 ShellState data;
4431 char *zErrMsg = 0;
4432 int doStats = 0;
4433 memcpy(&data, p, sizeof(data));
4434 data.showHeader = 0;
4435 data.cMode = data.mode = MODE_Semi;
4436 if( nArg==2 && optionMatch(azArg[1], "indent") ){
4437 data.cMode = data.mode = MODE_Pretty;
4438 nArg = 1;
4439 }
4440 if( nArg!=1 ){
4441 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
4442 rc = 1;
4443 goto meta_command_exit;
4444 }
4445 open_db(p, 0);
4446 rc = sqlite3_exec(p->db,
4447 "SELECT sql FROM"
4448 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
4449 " FROM sqlite_master UNION ALL"
4450 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
4451 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
4452 "ORDER BY rowid",
4453 callback, &data, &zErrMsg
4454 );
4455 if( rc==SQLITE_OK ){
4456 sqlite3_stmt *pStmt;
4457 rc = sqlite3_prepare_v2(p->db,
4458 "SELECT rowid FROM sqlite_master"
4459 " WHERE name GLOB 'sqlite_stat[134]'",
4460 -1, &pStmt, 0);
4461 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
4462 sqlite3_finalize(pStmt);
4463 }
4464 if( doStats==0 ){
4465 raw_printf(p->out, "/* No STAT tables available */\n");
4466 }else{
4467 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4468 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
4469 callback, &data, &zErrMsg);
4470 data.cMode = data.mode = MODE_Insert;
4471 data.zDestTable = "sqlite_stat1";
4472 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
4473 shell_callback, &data,&zErrMsg);
4474 data.zDestTable = "sqlite_stat3";
4475 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
4476 shell_callback, &data,&zErrMsg);
4477 data.zDestTable = "sqlite_stat4";
4478 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
4479 shell_callback, &data, &zErrMsg);
4480 raw_printf(p->out, "ANALYZE sqlite_master;\n");
4481 }
4482 }else
4483
4484 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
4485 if( nArg==2 ){
4486 p->showHeader = booleanValue(azArg[1]);
4487 }else{
4488 raw_printf(stderr, "Usage: .headers on|off\n");
4489 rc = 1;
4490 }
4491 }else
4492
4493 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
4494 utf8_printf(p->out, "%s", zHelp);
4495 }else
4496
4497 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
4498 char *zTable; /* Insert data into this table */
4499 char *zFile; /* Name of file to extra content from */
4500 sqlite3_stmt *pStmt = NULL; /* A statement */
4501 int nCol; /* Number of columns in the table */
4502 int nByte; /* Number of bytes in an SQL string */
4503 int i, j; /* Loop counters */
4504 int needCommit; /* True to COMMIT or ROLLBACK at end */
4505 int nSep; /* Number of bytes in p->colSeparator[] */
4506 char *zSql; /* An SQL statement */
4507 ImportCtx sCtx; /* Reader context */
4508 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
4509 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
4510
4511 if( nArg!=3 ){
4512 raw_printf(stderr, "Usage: .import FILE TABLE\n");
4513 goto meta_command_exit;
4514 }
4515 zFile = azArg[1];
4516 zTable = azArg[2];
4517 seenInterrupt = 0;
4518 memset(&sCtx, 0, sizeof(sCtx));
4519 open_db(p, 0);
4520 nSep = strlen30(p->colSeparator);
4521 if( nSep==0 ){
4522 raw_printf(stderr,
4523 "Error: non-null column separator required for import\n");
4524 return 1;
4525 }
4526 if( nSep>1 ){
4527 raw_printf(stderr, "Error: multi-character column separators not allowed"
4528 " for import\n");
4529 return 1;
4530 }
4531 nSep = strlen30(p->rowSeparator);
4532 if( nSep==0 ){
4533 raw_printf(stderr, "Error: non-null row separator required for import\n");
4534 return 1;
4535 }
4536 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
4537 /* When importing CSV (only), if the row separator is set to the
4538 ** default output row separator, change it to the default input
4539 ** row separator. This avoids having to maintain different input
4540 ** and output row separators. */
4541 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4542 nSep = strlen30(p->rowSeparator);
4543 }
4544 if( nSep>1 ){
4545 raw_printf(stderr, "Error: multi-character row separators not allowed"
4546 " for import\n");
4547 return 1;
4548 }
4549 sCtx.zFile = zFile;
4550 sCtx.nLine = 1;
4551 if( sCtx.zFile[0]=='|' ){
4552#ifdef SQLITE_OMIT_POPEN
4553 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
4554 return 1;
4555#else
4556 sCtx.in = popen(sCtx.zFile+1, "r");
4557 sCtx.zFile = "<pipe>";
4558 xCloser = pclose;
4559#endif
4560 }else{
4561 sCtx.in = fopen(sCtx.zFile, "rb");
4562 xCloser = fclose;
4563 }
4564 if( p->mode==MODE_Ascii ){
4565 xRead = ascii_read_one_field;
4566 }else{
4567 xRead = csv_read_one_field;
4568 }
4569 if( sCtx.in==0 ){
4570 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
4571 return 1;
4572 }
4573 sCtx.cColSep = p->colSeparator[0];
4574 sCtx.cRowSep = p->rowSeparator[0];
4575 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
4576 if( zSql==0 ){
4577 raw_printf(stderr, "Error: out of memory\n");
4578 xCloser(sCtx.in);
4579 return 1;
4580 }
4581 nByte = strlen30(zSql);
4582 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4583 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
4584 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
4585 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
4586 char cSep = '(';
4587 while( xRead(&sCtx) ){
4588 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
4589 cSep = ',';
4590 if( sCtx.cTerm!=sCtx.cColSep ) break;
4591 }
4592 if( cSep=='(' ){
4593 sqlite3_free(zCreate);
4594 sqlite3_free(sCtx.z);
4595 xCloser(sCtx.in);
4596 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
4597 return 1;
4598 }
4599 zCreate = sqlite3_mprintf("%z\n)", zCreate);
4600 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
4601 sqlite3_free(zCreate);
4602 if( rc ){
4603 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
4604 sqlite3_errmsg(p->db));
4605 sqlite3_free(sCtx.z);
4606 xCloser(sCtx.in);
4607 return 1;
4608 }
4609 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4610 }
4611 sqlite3_free(zSql);
4612 if( rc ){
4613 if (pStmt) sqlite3_finalize(pStmt);
4614 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
4615 xCloser(sCtx.in);
4616 return 1;
4617 }
4618 nCol = sqlite3_column_count(pStmt);
4619 sqlite3_finalize(pStmt);
4620 pStmt = 0;
4621 if( nCol==0 ) return 0; /* no columns, no error */
4622 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
4623 if( zSql==0 ){
4624 raw_printf(stderr, "Error: out of memory\n");
4625 xCloser(sCtx.in);
4626 return 1;
4627 }
4628 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
4629 j = strlen30(zSql);
4630 for(i=1; i<nCol; i++){
4631 zSql[j++] = ',';
4632 zSql[j++] = '?';
4633 }
4634 zSql[j++] = ')';
4635 zSql[j] = 0;
4636 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4637 sqlite3_free(zSql);
4638 if( rc ){
4639 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
4640 if (pStmt) sqlite3_finalize(pStmt);
4641 xCloser(sCtx.in);
4642 return 1;
4643 }
4644 needCommit = sqlite3_get_autocommit(p->db);
4645 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
4646 do{
4647 int startLine = sCtx.nLine;
4648 for(i=0; i<nCol; i++){
4649 char *z = xRead(&sCtx);
4650 /*
4651 ** Did we reach end-of-file before finding any columns?
4652 ** If so, stop instead of NULL filling the remaining columns.
4653 */
4654 if( z==0 && i==0 ) break;
4655 /*
4656 ** Did we reach end-of-file OR end-of-line before finding any
4657 ** columns in ASCII mode? If so, stop instead of NULL filling
4658 ** the remaining columns.
4659 */
4660 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
4661 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
4662 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
4663 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4664 "filling the rest with NULL\n",
4665 sCtx.zFile, startLine, nCol, i+1);
4666 i += 2;
4667 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
4668 }
4669 }
4670 if( sCtx.cTerm==sCtx.cColSep ){
4671 do{
4672 xRead(&sCtx);
4673 i++;
4674 }while( sCtx.cTerm==sCtx.cColSep );
4675 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
4676 "extras ignored\n",
4677 sCtx.zFile, startLine, nCol, i);
4678 }
4679 if( i>=nCol ){
4680 sqlite3_step(pStmt);
4681 rc = sqlite3_reset(pStmt);
4682 if( rc!=SQLITE_OK ){
4683 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
4684 startLine, sqlite3_errmsg(p->db));
4685 }
4686 }
4687 }while( sCtx.cTerm!=EOF );
4688
4689 xCloser(sCtx.in);
4690 sqlite3_free(sCtx.z);
4691 sqlite3_finalize(pStmt);
4692 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
4693 }else
4694
4695#ifndef SQLITE_UNTESTABLE
4696 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
4697 char *zSql;
4698 char *zCollist = 0;
4699 sqlite3_stmt *pStmt;
4700 int tnum = 0;
4701 int i;
4702 if( nArg!=3 ){
4703 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
4704 rc = 1;
4705 goto meta_command_exit;
4706 }
4707 open_db(p, 0);
4708 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
4709 " WHERE name='%q' AND type='index'", azArg[1]);
4710 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4711 sqlite3_free(zSql);
4712 if( sqlite3_step(pStmt)==SQLITE_ROW ){
4713 tnum = sqlite3_column_int(pStmt, 0);
4714 }
4715 sqlite3_finalize(pStmt);
4716 if( tnum==0 ){
4717 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
4718 rc = 1;
4719 goto meta_command_exit;
4720 }
4721 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
4722 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
4723 sqlite3_free(zSql);
4724 i = 0;
4725 while( sqlite3_step(pStmt)==SQLITE_ROW ){
4726 char zLabel[20];
4727 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
4728 i++;
4729 if( zCol==0 ){
4730 if( sqlite3_column_int(pStmt,1)==-1 ){
4731 zCol = "_ROWID_";
4732 }else{
4733 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
4734 zCol = zLabel;
4735 }
4736 }
4737 if( zCollist==0 ){
4738 zCollist = sqlite3_mprintf("\"%w\"", zCol);
4739 }else{
4740 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
4741 }
4742 }
4743 sqlite3_finalize(pStmt);
4744 zSql = sqlite3_mprintf(
4745 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
4746 azArg[2], zCollist, zCollist);
4747 sqlite3_free(zCollist);
4748 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
4749 if( rc==SQLITE_OK ){
4750 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
4751 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
4752 if( rc ){
4753 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
4754 }else{
4755 utf8_printf(stdout, "%s;\n", zSql);
4756 raw_printf(stdout,
4757 "WARNING: writing to an imposter table will corrupt the index!\n"
4758 );
4759 }
4760 }else{
4761 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
4762 rc = 1;
4763 }
4764 sqlite3_free(zSql);
4765 }else
4766#endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
4767
4768#ifdef SQLITE_ENABLE_IOTRACE
4769 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
4770 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
4771 if( iotrace && iotrace!=stdout ) fclose(iotrace);
4772 iotrace = 0;
4773 if( nArg<2 ){
4774 sqlite3IoTrace = 0;
4775 }else if( strcmp(azArg[1], "-")==0 ){
4776 sqlite3IoTrace = iotracePrintf;
4777 iotrace = stdout;
4778 }else{
4779 iotrace = fopen(azArg[1], "w");
4780 if( iotrace==0 ){
4781 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
4782 sqlite3IoTrace = 0;
4783 rc = 1;
4784 }else{
4785 sqlite3IoTrace = iotracePrintf;
4786 }
4787 }
4788 }else
4789#endif
4790
4791 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
4792 static const struct {
4793 const char *zLimitName; /* Name of a limit */
4794 int limitCode; /* Integer code for that limit */
4795 } aLimit[] = {
4796 { "length", SQLITE_LIMIT_LENGTH },
4797 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
4798 { "column", SQLITE_LIMIT_COLUMN },
4799 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
4800 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
4801 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
4802 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
4803 { "attached", SQLITE_LIMIT_ATTACHED },
4804 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
4805 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
4806 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
4807 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
4808 };
4809 int i, n2;
4810 open_db(p, 0);
4811 if( nArg==1 ){
4812 for(i=0; i<ArraySize(aLimit); i++){
4813 printf("%20s %d\n", aLimit[i].zLimitName,
4814 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
4815 }
4816 }else if( nArg>3 ){
4817 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
4818 rc = 1;
4819 goto meta_command_exit;
4820 }else{
4821 int iLimit = -1;
4822 n2 = strlen30(azArg[1]);
4823 for(i=0; i<ArraySize(aLimit); i++){
4824 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
4825 if( iLimit<0 ){
4826 iLimit = i;
4827 }else{
4828 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
4829 rc = 1;
4830 goto meta_command_exit;
4831 }
4832 }
4833 }
4834 if( iLimit<0 ){
4835 utf8_printf(stderr, "unknown limit: \"%s\"\n"
4836 "enter \".limits\" with no arguments for a list.\n",
4837 azArg[1]);
4838 rc = 1;
4839 goto meta_command_exit;
4840 }
4841 if( nArg==3 ){
4842 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
4843 (int)integerValue(azArg[2]));
4844 }
4845 printf("%20s %d\n", aLimit[iLimit].zLimitName,
4846 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
4847 }
4848 }else
4849
4850 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
4851 open_db(p, 0);
4852 lintDotCommand(p, azArg, nArg);
4853 }else
4854
4855#ifndef SQLITE_OMIT_LOAD_EXTENSION
4856 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
4857 const char *zFile, *zProc;
4858 char *zErrMsg = 0;
4859 if( nArg<2 ){
4860 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
4861 rc = 1;
4862 goto meta_command_exit;
4863 }
4864 zFile = azArg[1];
4865 zProc = nArg>=3 ? azArg[2] : 0;
4866 open_db(p, 0);
4867 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
4868 if( rc!=SQLITE_OK ){
4869 utf8_printf(stderr, "Error: %s\n", zErrMsg);
4870 sqlite3_free(zErrMsg);
4871 rc = 1;
4872 }
4873 }else
4874#endif
4875
4876 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
4877 if( nArg!=2 ){
4878 raw_printf(stderr, "Usage: .log FILENAME\n");
4879 rc = 1;
4880 }else{
4881 const char *zFile = azArg[1];
4882 output_file_close(p->pLog);
4883 p->pLog = output_file_open(zFile);
4884 }
4885 }else
4886
4887 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
4888 const char *zMode = nArg>=2 ? azArg[1] : "";
4889 int n2 = (int)strlen(zMode);
4890 int c2 = zMode[0];
4891 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
4892 p->mode = MODE_Line;
4893 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4894 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
4895 p->mode = MODE_Column;
4896 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4897 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
4898 p->mode = MODE_List;
4899 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
4900 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4901 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
4902 p->mode = MODE_Html;
4903 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
4904 p->mode = MODE_Tcl;
4905 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
4906 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
4907 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
4908 p->mode = MODE_Csv;
4909 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
4910 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
4911 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
4912 p->mode = MODE_List;
4913 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
4914 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
4915 p->mode = MODE_Insert;
4916 set_table_name(p, nArg>=3 ? azArg[2] : "table");
4917 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
4918 p->mode = MODE_Quote;
4919 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
4920 p->mode = MODE_Ascii;
4921 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
4922 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
4923 }else if( nArg==1 ){
4924 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
4925 }else{
4926 raw_printf(stderr, "Error: mode should be one of: "
4927 "ascii column csv html insert line list quote tabs tcl\n");
4928 rc = 1;
4929 }
4930 p->cMode = p->mode;
4931 }else
4932
4933 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
4934 if( nArg==2 ){
4935 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
4936 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
4937 }else{
4938 raw_printf(stderr, "Usage: .nullvalue STRING\n");
4939 rc = 1;
4940 }
4941 }else
4942
4943 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
4944 char *zNewFilename; /* Name of the database file to open */
4945 int iName = 1; /* Index in azArg[] of the filename */
4946 int newFlag = 0; /* True to delete file before opening */
4947 /* Close the existing database */
4948 session_close_all(p);
4949 sqlite3_close(p->db);
4950 p->db = 0;
4951 p->zDbFilename = 0;
4952 sqlite3_free(p->zFreeOnClose);
4953 p->zFreeOnClose = 0;
4954 /* Check for command-line arguments */
4955 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
4956 const char *z = azArg[iName];
4957 if( optionMatch(z,"new") ){
4958 newFlag = 1;
4959 }else if( z[0]=='-' ){
4960 utf8_printf(stderr, "unknown option: %s\n", z);
4961 rc = 1;
4962 goto meta_command_exit;
4963 }
4964 }
4965 /* If a filename is specified, try to open it first */
4966 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
4967 if( zNewFilename ){
4968 if( newFlag ) shellDeleteFile(zNewFilename);
4969 p->zDbFilename = zNewFilename;
4970 open_db(p, 1);
4971 if( p->db==0 ){
4972 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
4973 sqlite3_free(zNewFilename);
4974 }else{
4975 p->zFreeOnClose = zNewFilename;
4976 }
4977 }
4978 if( p->db==0 ){
4979 /* As a fall-back open a TEMP database */
4980 p->zDbFilename = 0;
4981 open_db(p, 0);
4982 }
4983 }else
4984
4985 if( c=='o'
4986 && (strncmp(azArg[0], "output", n)==0 || strncmp(azArg[0], "once", n)==0)
4987 ){
4988 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
4989 if( nArg>2 ){
4990 utf8_printf(stderr, "Usage: .%s FILE\n", azArg[0]);
4991 rc = 1;
4992 goto meta_command_exit;
4993 }
4994 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
4995 if( nArg<2 ){
4996 raw_printf(stderr, "Usage: .once FILE\n");
4997 rc = 1;
4998 goto meta_command_exit;
4999 }
5000 p->outCount = 2;
5001 }else{
5002 p->outCount = 0;
5003 }
5004 output_reset(p);
5005 if( zFile[0]=='|' ){
5006#ifdef SQLITE_OMIT_POPEN
5007 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
5008 rc = 1;
5009 p->out = stdout;
5010#else
5011 p->out = popen(zFile + 1, "w");
5012 if( p->out==0 ){
5013 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
5014 p->out = stdout;
5015 rc = 1;
5016 }else{
5017 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5018 }
5019#endif
5020 }else{
5021 p->out = output_file_open(zFile);
5022 if( p->out==0 ){
5023 if( strcmp(zFile,"off")!=0 ){
5024 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
5025 }
5026 p->out = stdout;
5027 rc = 1;
5028 } else {
5029 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
5030 }
5031 }
5032 }else
5033
5034 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
5035 int i;
5036 for(i=1; i<nArg; i++){
5037 if( i>1 ) raw_printf(p->out, " ");
5038 utf8_printf(p->out, "%s", azArg[i]);
5039 }
5040 raw_printf(p->out, "\n");
5041 }else
5042
5043 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
5044 if( nArg >= 2) {
5045 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
5046 }
5047 if( nArg >= 3) {
5048 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
5049 }
5050 }else
5051
5052 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
5053 rc = 2;
5054 }else
5055
5056 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
5057 FILE *alt;
5058 if( nArg!=2 ){
5059 raw_printf(stderr, "Usage: .read FILE\n");
5060 rc = 1;
5061 goto meta_command_exit;
5062 }
5063 alt = fopen(azArg[1], "rb");
5064 if( alt==0 ){
5065 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
5066 rc = 1;
5067 }else{
5068 rc = process_input(p, alt);
5069 fclose(alt);
5070 }
5071 }else
5072
5073 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
5074 const char *zSrcFile;
5075 const char *zDb;
5076 sqlite3 *pSrc;
5077 sqlite3_backup *pBackup;
5078 int nTimeout = 0;
5079
5080 if( nArg==2 ){
5081 zSrcFile = azArg[1];
5082 zDb = "main";
5083 }else if( nArg==3 ){
5084 zSrcFile = azArg[2];
5085 zDb = azArg[1];
5086 }else{
5087 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
5088 rc = 1;
5089 goto meta_command_exit;
5090 }
5091 rc = sqlite3_open(zSrcFile, &pSrc);
5092 if( rc!=SQLITE_OK ){
5093 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
5094 sqlite3_close(pSrc);
5095 return 1;
5096 }
5097 open_db(p, 0);
5098 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
5099 if( pBackup==0 ){
5100 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5101 sqlite3_close(pSrc);
5102 return 1;
5103 }
5104 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
5105 || rc==SQLITE_BUSY ){
5106 if( rc==SQLITE_BUSY ){
5107 if( nTimeout++ >= 3 ) break;
5108 sqlite3_sleep(100);
5109 }
5110 }
5111 sqlite3_backup_finish(pBackup);
5112 if( rc==SQLITE_DONE ){
5113 rc = 0;
5114 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
5115 raw_printf(stderr, "Error: source database is busy\n");
5116 rc = 1;
5117 }else{
5118 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5119 rc = 1;
5120 }
5121 sqlite3_close(pSrc);
5122 }else
5123
5124
5125 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
5126 if( nArg==2 ){
5127 p->scanstatsOn = booleanValue(azArg[1]);
5128#ifndef SQLITE_ENABLE_STMT_SCANSTATUS
5129 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
5130#endif
5131 }else{
5132 raw_printf(stderr, "Usage: .scanstats on|off\n");
5133 rc = 1;
5134 }
5135 }else
5136
5137 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
5138 ShellText sSelect;
5139 ShellState data;
5140 char *zErrMsg = 0;
5141 const char *zDiv = 0;
5142 int iSchema = 0;
5143
5144 open_db(p, 0);
5145 memcpy(&data, p, sizeof(data));
5146 data.showHeader = 0;
5147 data.cMode = data.mode = MODE_Semi;
5148 initText(&sSelect);
5149 if( nArg>=2 && optionMatch(azArg[1], "indent") ){
5150 data.cMode = data.mode = MODE_Pretty;
5151 nArg--;
5152 if( nArg==2 ) azArg[1] = azArg[2];
5153 }
5154 if( nArg==2 && azArg[1][0]!='-' ){
5155 int i;
5156 for(i=0; azArg[1][i]; i++) azArg[1][i] = ToLower(azArg[1][i]);
5157 if( strcmp(azArg[1],"sqlite_master")==0 ){
5158 char *new_argv[2], *new_colv[2];
5159 new_argv[0] = "CREATE TABLE sqlite_master (\n"
5160 " type text,\n"
5161 " name text,\n"
5162 " tbl_name text,\n"
5163 " rootpage integer,\n"
5164 " sql text\n"
5165 ")";
5166 new_argv[1] = 0;
5167 new_colv[0] = "sql";
5168 new_colv[1] = 0;
5169 callback(&data, 1, new_argv, new_colv);
5170 rc = SQLITE_OK;
5171 }else if( strcmp(azArg[1],"sqlite_temp_master")==0 ){
5172 char *new_argv[2], *new_colv[2];
5173 new_argv[0] = "CREATE TEMP TABLE sqlite_temp_master (\n"
5174 " type text,\n"
5175 " name text,\n"
5176 " tbl_name text,\n"
5177 " rootpage integer,\n"
5178 " sql text\n"
5179 ")";
5180 new_argv[1] = 0;
5181 new_colv[0] = "sql";
5182 new_colv[1] = 0;
5183 callback(&data, 1, new_argv, new_colv);
5184 rc = SQLITE_OK;
5185 }else{
5186 zDiv = "(";
5187 }
5188 }else if( nArg==1 ){
5189 zDiv = "(";
5190 }else{
5191 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
5192 rc = 1;
5193 goto meta_command_exit;
5194 }
5195 if( zDiv ){
5196 sqlite3_stmt *pStmt = 0;
5197 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
5198 -1, &pStmt, 0);
5199 if( rc ){
5200 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
5201 sqlite3_finalize(pStmt);
5202 rc = 1;
5203 goto meta_command_exit;
5204 }
5205 appendText(&sSelect, "SELECT sql FROM", 0);
5206 iSchema = 0;
5207 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5208 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
5209 char zScNum[30];
5210 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
5211 appendText(&sSelect, zDiv, 0);
5212 zDiv = " UNION ALL ";
5213 if( strcmp(zDb, "main")!=0 ){
5214 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
5215 appendText(&sSelect, zDb, '"');
5216 appendText(&sSelect, ") AS sql, type, tbl_name, name, rowid,", 0);
5217 appendText(&sSelect, zScNum, 0);
5218 appendText(&sSelect, " AS snum, ", 0);
5219 appendText(&sSelect, zDb, '\'');
5220 appendText(&sSelect, " AS sname FROM ", 0);
5221 appendText(&sSelect, zDb, '"');
5222 appendText(&sSelect, ".sqlite_master", 0);
5223 }else{
5224 appendText(&sSelect, "SELECT sql, type, tbl_name, name, rowid, ", 0);
5225 appendText(&sSelect, zScNum, 0);
5226 appendText(&sSelect, " AS snum, 'main' AS sname FROM sqlite_master",0);
5227 }
5228 }
5229 sqlite3_finalize(pStmt);
5230 appendText(&sSelect, ") WHERE ", 0);
5231 if( nArg>1 ){
5232 char *zQarg = sqlite3_mprintf("%Q", azArg[1]);
5233 if( strchr(azArg[1], '.') ){
5234 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
5235 }else{
5236 appendText(&sSelect, "lower(tbl_name)", 0);
5237 }
5238 appendText(&sSelect, strchr(azArg[1], '*') ? " GLOB " : " LIKE ", 0);
5239 appendText(&sSelect, zQarg, 0);
5240 appendText(&sSelect, " AND ", 0);
5241 sqlite3_free(zQarg);
5242 }
5243 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
5244 " ORDER BY snum, rowid", 0);
5245 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
5246 freeText(&sSelect);
5247 }
5248 if( zErrMsg ){
5249 utf8_printf(stderr,"Error: %s\n", zErrMsg);
5250 sqlite3_free(zErrMsg);
5251 rc = 1;
5252 }else if( rc != SQLITE_OK ){
5253 raw_printf(stderr,"Error: querying schema information\n");
5254 rc = 1;
5255 }else{
5256 rc = 0;
5257 }
5258 }else
5259
5260#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
5261 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
5262 sqlite3SelectTrace = (int)integerValue(azArg[1]);
5263 }else
5264#endif
5265
5266#if defined(SQLITE_ENABLE_SESSION)
5267 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
5268 OpenSession *pSession = &p->aSession[0];
5269 char **azCmd = &azArg[1];
5270 int iSes = 0;
5271 int nCmd = nArg - 1;
5272 int i;
5273 if( nArg<=1 ) goto session_syntax_error;
5274 open_db(p, 0);
5275 if( nArg>=3 ){
5276 for(iSes=0; iSes<p->nSession; iSes++){
5277 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
5278 }
5279 if( iSes<p->nSession ){
5280 pSession = &p->aSession[iSes];
5281 azCmd++;
5282 nCmd--;
5283 }else{
5284 pSession = &p->aSession[0];
5285 iSes = 0;
5286 }
5287 }
5288
5289 /* .session attach TABLE
5290 ** Invoke the sqlite3session_attach() interface to attach a particular
5291 ** table so that it is never filtered.
5292 */
5293 if( strcmp(azCmd[0],"attach")==0 ){
5294 if( nCmd!=2 ) goto session_syntax_error;
5295 if( pSession->p==0 ){
5296 session_not_open:
5297 raw_printf(stderr, "ERROR: No sessions are open\n");
5298 }else{
5299 rc = sqlite3session_attach(pSession->p, azCmd[1]);
5300 if( rc ){
5301 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
5302 rc = 0;
5303 }
5304 }
5305 }else
5306
5307 /* .session changeset FILE
5308 ** .session patchset FILE
5309 ** Write a changeset or patchset into a file. The file is overwritten.
5310 */
5311 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
5312 FILE *out = 0;
5313 if( nCmd!=2 ) goto session_syntax_error;
5314 if( pSession->p==0 ) goto session_not_open;
5315 out = fopen(azCmd[1], "wb");
5316 if( out==0 ){
5317 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
5318 }else{
5319 int szChng;
5320 void *pChng;
5321 if( azCmd[0][0]=='c' ){
5322 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
5323 }else{
5324 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
5325 }
5326 if( rc ){
5327 printf("Error: error code %d\n", rc);
5328 rc = 0;
5329 }
5330 if( pChng
5331 && fwrite(pChng, szChng, 1, out)!=1 ){
5332 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
5333 szChng);
5334 }
5335 sqlite3_free(pChng);
5336 fclose(out);
5337 }
5338 }else
5339
5340 /* .session close
5341 ** Close the identified session
5342 */
5343 if( strcmp(azCmd[0], "close")==0 ){
5344 if( nCmd!=1 ) goto session_syntax_error;
5345 if( p->nSession ){
5346 session_close(pSession);
5347 p->aSession[iSes] = p->aSession[--p->nSession];
5348 }
5349 }else
5350
5351 /* .session enable ?BOOLEAN?
5352 ** Query or set the enable flag
5353 */
5354 if( strcmp(azCmd[0], "enable")==0 ){
5355 int ii;
5356 if( nCmd>2 ) goto session_syntax_error;
5357 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5358 if( p->nSession ){
5359 ii = sqlite3session_enable(pSession->p, ii);
5360 utf8_printf(p->out, "session %s enable flag = %d\n",
5361 pSession->zName, ii);
5362 }
5363 }else
5364
5365 /* .session filter GLOB ....
5366 ** Set a list of GLOB patterns of table names to be excluded.
5367 */
5368 if( strcmp(azCmd[0], "filter")==0 ){
5369 int ii, nByte;
5370 if( nCmd<2 ) goto session_syntax_error;
5371 if( p->nSession ){
5372 for(ii=0; ii<pSession->nFilter; ii++){
5373 sqlite3_free(pSession->azFilter[ii]);
5374 }
5375 sqlite3_free(pSession->azFilter);
5376 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
5377 pSession->azFilter = sqlite3_malloc( nByte );
5378 if( pSession->azFilter==0 ){
5379 raw_printf(stderr, "Error: out or memory\n");
5380 exit(1);
5381 }
5382 for(ii=1; ii<nCmd; ii++){
5383 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
5384 }
5385 pSession->nFilter = ii-1;
5386 }
5387 }else
5388
5389 /* .session indirect ?BOOLEAN?
5390 ** Query or set the indirect flag
5391 */
5392 if( strcmp(azCmd[0], "indirect")==0 ){
5393 int ii;
5394 if( nCmd>2 ) goto session_syntax_error;
5395 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
5396 if( p->nSession ){
5397 ii = sqlite3session_indirect(pSession->p, ii);
5398 utf8_printf(p->out, "session %s indirect flag = %d\n",
5399 pSession->zName, ii);
5400 }
5401 }else
5402
5403 /* .session isempty
5404 ** Determine if the session is empty
5405 */
5406 if( strcmp(azCmd[0], "isempty")==0 ){
5407 int ii;
5408 if( nCmd!=1 ) goto session_syntax_error;
5409 if( p->nSession ){
5410 ii = sqlite3session_isempty(pSession->p);
5411 utf8_printf(p->out, "session %s isempty flag = %d\n",
5412 pSession->zName, ii);
5413 }
5414 }else
5415
5416 /* .session list
5417 ** List all currently open sessions
5418 */
5419 if( strcmp(azCmd[0],"list")==0 ){
5420 for(i=0; i<p->nSession; i++){
5421 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
5422 }
5423 }else
5424
5425 /* .session open DB NAME
5426 ** Open a new session called NAME on the attached database DB.
5427 ** DB is normally "main".
5428 */
5429 if( strcmp(azCmd[0],"open")==0 ){
5430 char *zName;
5431 if( nCmd!=3 ) goto session_syntax_error;
5432 zName = azCmd[2];
5433 if( zName[0]==0 ) goto session_syntax_error;
5434 for(i=0; i<p->nSession; i++){
5435 if( strcmp(p->aSession[i].zName,zName)==0 ){
5436 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
5437 goto meta_command_exit;
5438 }
5439 }
5440 if( p->nSession>=ArraySize(p->aSession) ){
5441 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
5442 goto meta_command_exit;
5443 }
5444 pSession = &p->aSession[p->nSession];
5445 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
5446 if( rc ){
5447 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
5448 rc = 0;
5449 goto meta_command_exit;
5450 }
5451 pSession->nFilter = 0;
5452 sqlite3session_table_filter(pSession->p, session_filter, pSession);
5453 p->nSession++;
5454 pSession->zName = sqlite3_mprintf("%s", zName);
5455 }else
5456 /* If no command name matches, show a syntax error */
5457 session_syntax_error:
5458 session_help(p);
5459 }else
5460#endif
5461
5462#ifdef SQLITE_DEBUG
5463 /* Undocumented commands for internal testing. Subject to change
5464 ** without notice. */
5465 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
5466 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
5467 int i, v;
5468 for(i=1; i<nArg; i++){
5469 v = booleanValue(azArg[i]);
5470 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
5471 }
5472 }
5473 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
5474 int i; sqlite3_int64 v;
5475 for(i=1; i<nArg; i++){
5476 char zBuf[200];
5477 v = integerValue(azArg[i]);
5478 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
5479 utf8_printf(p->out, "%s", zBuf);
5480 }
5481 }
5482 }else
5483#endif
5484
5485 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
5486 int bIsInit = 0; /* True to initialize the SELFTEST table */
5487 int bVerbose = 0; /* Verbose output */
5488 int bSelftestExists; /* True if SELFTEST already exists */
5489 int i, k; /* Loop counters */
5490 int nTest = 0; /* Number of tests runs */
5491 int nErr = 0; /* Number of errors seen */
5492 ShellText str; /* Answer for a query */
5493 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
5494
5495 open_db(p,0);
5496 for(i=1; i<nArg; i++){
5497 const char *z = azArg[i];
5498 if( z[0]=='-' && z[1]=='-' ) z++;
5499 if( strcmp(z,"-init")==0 ){
5500 bIsInit = 1;
5501 }else
5502 if( strcmp(z,"-v")==0 ){
5503 bVerbose++;
5504 }else
5505 {
5506 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5507 azArg[i], azArg[0]);
5508 raw_printf(stderr, "Should be one of: --init -v\n");
5509 rc = 1;
5510 goto meta_command_exit;
5511 }
5512 }
5513 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
5514 != SQLITE_OK ){
5515 bSelftestExists = 0;
5516 }else{
5517 bSelftestExists = 1;
5518 }
5519 if( bIsInit ){
5520 createSelftestTable(p);
5521 bSelftestExists = 1;
5522 }
5523 initText(&str);
5524 appendText(&str, "x", 0);
5525 for(k=bSelftestExists; k>=0; k--){
5526 if( k==1 ){
5527 rc = sqlite3_prepare_v2(p->db,
5528 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
5529 -1, &pStmt, 0);
5530 }else{
5531 rc = sqlite3_prepare_v2(p->db,
5532 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
5533 " (1,'run','PRAGMA integrity_check','ok')",
5534 -1, &pStmt, 0);
5535 }
5536 if( rc ){
5537 raw_printf(stderr, "Error querying the selftest table\n");
5538 rc = 1;
5539 sqlite3_finalize(pStmt);
5540 goto meta_command_exit;
5541 }
5542 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
5543 int tno = sqlite3_column_int(pStmt, 0);
5544 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
5545 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
5546 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
5547
5548 k = 0;
5549 if( bVerbose>0 ){
5550 char *zQuote = sqlite3_mprintf("%q", zSql);
5551 printf("%d: %s %s\n", tno, zOp, zSql);
5552 sqlite3_free(zQuote);
5553 }
5554 if( strcmp(zOp,"memo")==0 ){
5555 utf8_printf(p->out, "%s\n", zSql);
5556 }else
5557 if( strcmp(zOp,"run")==0 ){
5558 char *zErrMsg = 0;
5559 str.n = 0;
5560 str.z[0] = 0;
5561 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
5562 nTest++;
5563 if( bVerbose ){
5564 utf8_printf(p->out, "Result: %s\n", str.z);
5565 }
5566 if( rc || zErrMsg ){
5567 nErr++;
5568 rc = 1;
5569 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
5570 sqlite3_free(zErrMsg);
5571 }else if( strcmp(zAns,str.z)!=0 ){
5572 nErr++;
5573 rc = 1;
5574 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
5575 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
5576 }
5577 }else
5578 {
5579 utf8_printf(stderr,
5580 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
5581 rc = 1;
5582 break;
5583 }
5584 } /* End loop over rows of content from SELFTEST */
5585 sqlite3_finalize(pStmt);
5586 } /* End loop over k */
5587 freeText(&str);
5588 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
5589 }else
5590
5591 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
5592 if( nArg<2 || nArg>3 ){
5593 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
5594 rc = 1;
5595 }
5596 if( nArg>=2 ){
5597 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
5598 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
5599 }
5600 if( nArg>=3 ){
5601 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
5602 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
5603 }
5604 }else
5605
5606 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
5607 const char *zLike = 0; /* Which table to checksum. 0 means everything */
5608 int i; /* Loop counter */
5609 int bSchema = 0; /* Also hash the schema */
5610 int bSeparate = 0; /* Hash each table separately */
5611 int iSize = 224; /* Hash algorithm to use */
5612 int bDebug = 0; /* Only show the query that would have run */
5613 sqlite3_stmt *pStmt; /* For querying tables names */
5614 char *zSql; /* SQL to be run */
5615 char *zSep; /* Separator */
5616 ShellText sSql; /* Complete SQL for the query to run the hash */
5617 ShellText sQuery; /* Set of queries used to read all content */
5618 open_db(p, 0);
5619 for(i=1; i<nArg; i++){
5620 const char *z = azArg[i];
5621 if( z[0]=='-' ){
5622 z++;
5623 if( z[0]=='-' ) z++;
5624 if( strcmp(z,"schema")==0 ){
5625 bSchema = 1;
5626 }else
5627 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
5628 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
5629 ){
5630 iSize = atoi(&z[5]);
5631 }else
5632 if( strcmp(z,"debug")==0 ){
5633 bDebug = 1;
5634 }else
5635 {
5636 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
5637 azArg[i], azArg[0]);
5638 raw_printf(stderr, "Should be one of: --schema"
5639 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
5640 rc = 1;
5641 goto meta_command_exit;
5642 }
5643 }else if( zLike ){
5644 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
5645 rc = 1;
5646 goto meta_command_exit;
5647 }else{
5648 zLike = z;
5649 bSeparate = 1;
5650 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
5651 }
5652 }
5653 if( bSchema ){
5654 zSql = "SELECT lower(name) FROM sqlite_master"
5655 " WHERE type='table' AND coalesce(rootpage,0)>1"
5656 " UNION ALL SELECT 'sqlite_master'"
5657 " ORDER BY 1 collate nocase";
5658 }else{
5659 zSql = "SELECT lower(name) FROM sqlite_master"
5660 " WHERE type='table' AND coalesce(rootpage,0)>1"
5661 " AND name NOT LIKE 'sqlite_%'"
5662 " ORDER BY 1 collate nocase";
5663 }
5664 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
5665 initText(&sQuery);
5666 initText(&sSql);
5667 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
5668 zSep = "VALUES(";
5669 while( SQLITE_ROW==sqlite3_step(pStmt) ){
5670 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
5671 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
5672 if( strncmp(zTab, "sqlite_",7)!=0 ){
5673 appendText(&sQuery,"SELECT * FROM ", 0);
5674 appendText(&sQuery,zTab,'"');
5675 appendText(&sQuery," NOT INDEXED;", 0);
5676 }else if( strcmp(zTab, "sqlite_master")==0 ){
5677 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
5678 " ORDER BY name;", 0);
5679 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
5680 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
5681 " ORDER BY name;", 0);
5682 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
5683 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
5684 " ORDER BY tbl,idx;", 0);
5685 }else if( strcmp(zTab, "sqlite_stat3")==0
5686 || strcmp(zTab, "sqlite_stat4")==0 ){
5687 appendText(&sQuery, "SELECT * FROM ", 0);
5688 appendText(&sQuery, zTab, 0);
5689 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
5690 }
5691 appendText(&sSql, zSep, 0);
5692 appendText(&sSql, sQuery.z, '\'');
5693 sQuery.n = 0;
5694 appendText(&sSql, ",", 0);
5695 appendText(&sSql, zTab, '\'');
5696 zSep = "),(";
5697 }
5698 sqlite3_finalize(pStmt);
5699 if( bSeparate ){
5700 zSql = sqlite3_mprintf(
5701 "%s))"
5702 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
5703 " FROM [sha3sum$query]",
5704 sSql.z, iSize);
5705 }else{
5706 zSql = sqlite3_mprintf(
5707 "%s))"
5708 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
5709 " FROM [sha3sum$query]",
5710 sSql.z, iSize);
5711 }
5712 freeText(&sQuery);
5713 freeText(&sSql);
5714 if( bDebug ){
5715 utf8_printf(p->out, "%s\n", zSql);
5716 }else{
5717 shell_exec(p->db, zSql, shell_callback, p, 0);
5718 }
5719 sqlite3_free(zSql);
5720 }else
5721
5722 if( c=='s'
5723 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
5724 ){
5725 char *zCmd;
5726 int i, x;
5727 if( nArg<2 ){
5728 raw_printf(stderr, "Usage: .system COMMAND\n");
5729 rc = 1;
5730 goto meta_command_exit;
5731 }
5732 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
5733 for(i=2; i<nArg; i++){
5734 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
5735 zCmd, azArg[i]);
5736 }
5737 x = system(zCmd);
5738 sqlite3_free(zCmd);
5739 if( x ) raw_printf(stderr, "System command returns %d\n", x);
5740 }else
5741
5742 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
5743 static const char *azBool[] = { "off", "on", "full", "unk" };
5744 int i;
5745 if( nArg!=1 ){
5746 raw_printf(stderr, "Usage: .show\n");
5747 rc = 1;
5748 goto meta_command_exit;
5749 }
5750 utf8_printf(p->out, "%12.12s: %s\n","echo",
5751 azBool[ShellHasFlag(p, SHFLG_Echo)]);
5752 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
5753 utf8_printf(p->out, "%12.12s: %s\n","explain",
5754 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
5755 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
5756 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
5757 utf8_printf(p->out, "%12.12s: ", "nullvalue");
5758 output_c_string(p->out, p->nullValue);
5759 raw_printf(p->out, "\n");
5760 utf8_printf(p->out,"%12.12s: %s\n","output",
5761 strlen30(p->outfile) ? p->outfile : "stdout");
5762 utf8_printf(p->out,"%12.12s: ", "colseparator");
5763 output_c_string(p->out, p->colSeparator);
5764 raw_printf(p->out, "\n");
5765 utf8_printf(p->out,"%12.12s: ", "rowseparator");
5766 output_c_string(p->out, p->rowSeparator);
5767 raw_printf(p->out, "\n");
5768 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
5769 utf8_printf(p->out, "%12.12s: ", "width");
5770 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
5771 raw_printf(p->out, "%d ", p->colWidth[i]);
5772 }
5773 raw_printf(p->out, "\n");
5774 utf8_printf(p->out, "%12.12s: %s\n", "filename",
5775 p->zDbFilename ? p->zDbFilename : "");
5776 }else
5777
5778 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
5779 if( nArg==2 ){
5780 p->statsOn = booleanValue(azArg[1]);
5781 }else if( nArg==1 ){
5782 display_stats(p->db, p, 0);
5783 }else{
5784 raw_printf(stderr, "Usage: .stats ?on|off?\n");
5785 rc = 1;
5786 }
5787 }else
5788
5789 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
5790 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
5791 || strncmp(azArg[0], "indexes", n)==0) )
5792 ){
5793 sqlite3_stmt *pStmt;
5794 char **azResult;
5795 int nRow, nAlloc;
5796 int ii;
5797 ShellText s;
5798 initText(&s);
5799 open_db(p, 0);
5800 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
5801 if( rc ) return shellDatabaseError(p->db);
5802
5803 if( nArg>2 && c=='i' ){
5804 /* It is an historical accident that the .indexes command shows an error
5805 ** when called with the wrong number of arguments whereas the .tables
5806 ** command does not. */
5807 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
5808 rc = 1;
5809 goto meta_command_exit;
5810 }
5811 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
5812 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
5813 if( zDbName==0 ) continue;
5814 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
5815 if( sqlite3_stricmp(zDbName, "main")==0 ){
5816 appendText(&s, "SELECT name FROM ", 0);
5817 }else{
5818 appendText(&s, "SELECT ", 0);
5819 appendText(&s, zDbName, '\'');
5820 appendText(&s, "||'.'||name FROM ", 0);
5821 }
5822 appendText(&s, zDbName, '"');
5823 appendText(&s, ".sqlite_master ", 0);
5824 if( c=='t' ){
5825 appendText(&s," WHERE type IN ('table','view')"
5826 " AND name NOT LIKE 'sqlite_%'"
5827 " AND name LIKE ?1", 0);
5828 }else{
5829 appendText(&s," WHERE type='index'"
5830 " AND tbl_name LIKE ?1", 0);
5831 }
5832 }
5833 rc = sqlite3_finalize(pStmt);
5834 appendText(&s, " ORDER BY 1", 0);
5835 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
5836 freeText(&s);
5837 if( rc ) return shellDatabaseError(p->db);
5838
5839 /* Run the SQL statement prepared by the above block. Store the results
5840 ** as an array of nul-terminated strings in azResult[]. */
5841 nRow = nAlloc = 0;
5842 azResult = 0;
5843 if( nArg>1 ){
5844 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
5845 }else{
5846 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
5847 }
5848 while( sqlite3_step(pStmt)==SQLITE_ROW ){
5849 if( nRow>=nAlloc ){
5850 char **azNew;
5851 int n2 = nAlloc*2 + 10;
5852 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
5853 if( azNew==0 ){
5854 rc = shellNomemError();
5855 break;
5856 }
5857 nAlloc = n2;
5858 azResult = azNew;
5859 }
5860 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
5861 if( 0==azResult[nRow] ){
5862 rc = shellNomemError();
5863 break;
5864 }
5865 nRow++;
5866 }
5867 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
5868 rc = shellDatabaseError(p->db);
5869 }
5870
5871 /* Pretty-print the contents of array azResult[] to the output */
5872 if( rc==0 && nRow>0 ){
5873 int len, maxlen = 0;
5874 int i, j;
5875 int nPrintCol, nPrintRow;
5876 for(i=0; i<nRow; i++){
5877 len = strlen30(azResult[i]);
5878 if( len>maxlen ) maxlen = len;
5879 }
5880 nPrintCol = 80/(maxlen+2);
5881 if( nPrintCol<1 ) nPrintCol = 1;
5882 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
5883 for(i=0; i<nPrintRow; i++){
5884 for(j=i; j<nRow; j+=nPrintRow){
5885 char *zSp = j<nPrintRow ? "" : " ";
5886 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
5887 azResult[j] ? azResult[j]:"");
5888 }
5889 raw_printf(p->out, "\n");
5890 }
5891 }
5892
5893 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
5894 sqlite3_free(azResult);
5895 }else
5896
5897 /* Begin redirecting output to the file "testcase-out.txt" */
5898 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
5899 output_reset(p);
5900 p->out = output_file_open("testcase-out.txt");
5901 if( p->out==0 ){
5902 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
5903 }
5904 if( nArg>=2 ){
5905 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
5906 }else{
5907 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
5908 }
5909 }else
5910
5911#ifndef SQLITE_UNTESTABLE
5912 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 && nArg>=2 ){
5913 static const struct {
5914 const char *zCtrlName; /* Name of a test-control option */
5915 int ctrlCode; /* Integer code for that option */
5916 } aCtrl[] = {
5917 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE },
5918 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE },
5919 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET },
5920 { "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST },
5921 { "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL },
5922 { "benign_malloc_hooks", SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS },
5923 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE },
5924 { "assert", SQLITE_TESTCTRL_ASSERT },
5925 { "always", SQLITE_TESTCTRL_ALWAYS },
5926 { "reserve", SQLITE_TESTCTRL_RESERVE },
5927 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS },
5928 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD },
drh2ce15c32017-07-11 13:34:40 +00005929 { "byteorder", SQLITE_TESTCTRL_BYTEORDER },
5930 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT },
5931 { "imposter", SQLITE_TESTCTRL_IMPOSTER },
5932 };
5933 int testctrl = -1;
5934 int rc2 = 0;
5935 int i, n2;
5936 open_db(p, 0);
5937
5938 /* convert testctrl text option to value. allow any unique prefix
5939 ** of the option name, or a numerical value. */
5940 n2 = strlen30(azArg[1]);
5941 for(i=0; i<ArraySize(aCtrl); i++){
5942 if( strncmp(azArg[1], aCtrl[i].zCtrlName, n2)==0 ){
5943 if( testctrl<0 ){
5944 testctrl = aCtrl[i].ctrlCode;
5945 }else{
5946 utf8_printf(stderr, "ambiguous option name: \"%s\"\n", azArg[1]);
5947 testctrl = -1;
5948 break;
5949 }
5950 }
5951 }
5952 if( testctrl<0 ) testctrl = (int)integerValue(azArg[1]);
5953 if( (testctrl<SQLITE_TESTCTRL_FIRST) || (testctrl>SQLITE_TESTCTRL_LAST) ){
5954 utf8_printf(stderr,"Error: invalid testctrl option: %s\n", azArg[1]);
5955 }else{
5956 switch(testctrl){
5957
5958 /* sqlite3_test_control(int, db, int) */
5959 case SQLITE_TESTCTRL_OPTIMIZATIONS:
5960 case SQLITE_TESTCTRL_RESERVE:
5961 if( nArg==3 ){
5962 int opt = (int)strtol(azArg[2], 0, 0);
5963 rc2 = sqlite3_test_control(testctrl, p->db, opt);
5964 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5965 } else {
5966 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
5967 azArg[1]);
5968 }
5969 break;
5970
5971 /* sqlite3_test_control(int) */
5972 case SQLITE_TESTCTRL_PRNG_SAVE:
5973 case SQLITE_TESTCTRL_PRNG_RESTORE:
5974 case SQLITE_TESTCTRL_PRNG_RESET:
5975 case SQLITE_TESTCTRL_BYTEORDER:
5976 if( nArg==2 ){
5977 rc2 = sqlite3_test_control(testctrl);
5978 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5979 } else {
5980 utf8_printf(stderr,"Error: testctrl %s takes no options\n",
5981 azArg[1]);
5982 }
5983 break;
5984
5985 /* sqlite3_test_control(int, uint) */
5986 case SQLITE_TESTCTRL_PENDING_BYTE:
5987 if( nArg==3 ){
5988 unsigned int opt = (unsigned int)integerValue(azArg[2]);
5989 rc2 = sqlite3_test_control(testctrl, opt);
5990 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
5991 } else {
5992 utf8_printf(stderr,"Error: testctrl %s takes a single unsigned"
5993 " int option\n", azArg[1]);
5994 }
5995 break;
5996
5997 /* sqlite3_test_control(int, int) */
5998 case SQLITE_TESTCTRL_ASSERT:
5999 case SQLITE_TESTCTRL_ALWAYS:
6000 case SQLITE_TESTCTRL_NEVER_CORRUPT:
6001 if( nArg==3 ){
6002 int opt = booleanValue(azArg[2]);
6003 rc2 = sqlite3_test_control(testctrl, opt);
6004 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6005 } else {
6006 utf8_printf(stderr,"Error: testctrl %s takes a single int option\n",
6007 azArg[1]);
6008 }
6009 break;
6010
6011 /* sqlite3_test_control(int, char *) */
6012#ifdef SQLITE_N_KEYWORD
6013 case SQLITE_TESTCTRL_ISKEYWORD:
6014 if( nArg==3 ){
6015 const char *opt = azArg[2];
6016 rc2 = sqlite3_test_control(testctrl, opt);
6017 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6018 } else {
6019 utf8_printf(stderr,
6020 "Error: testctrl %s takes a single char * option\n",
6021 azArg[1]);
6022 }
6023 break;
6024#endif
6025
6026 case SQLITE_TESTCTRL_IMPOSTER:
6027 if( nArg==5 ){
6028 rc2 = sqlite3_test_control(testctrl, p->db,
6029 azArg[2],
6030 integerValue(azArg[3]),
6031 integerValue(azArg[4]));
6032 raw_printf(p->out, "%d (0x%08x)\n", rc2, rc2);
6033 }else{
6034 raw_printf(stderr,"Usage: .testctrl imposter dbName onoff tnum\n");
6035 }
6036 break;
6037
6038 case SQLITE_TESTCTRL_BITVEC_TEST:
6039 case SQLITE_TESTCTRL_FAULT_INSTALL:
6040 case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS:
drh2ce15c32017-07-11 13:34:40 +00006041 default:
6042 utf8_printf(stderr,
6043 "Error: CLI support for testctrl %s not implemented\n",
6044 azArg[1]);
6045 break;
6046 }
6047 }
6048 }else
6049#endif /* !defined(SQLITE_UNTESTABLE) */
6050
6051 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
6052 open_db(p, 0);
6053 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
6054 }else
6055
6056 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
6057 if( nArg==2 ){
6058 enableTimer = booleanValue(azArg[1]);
6059 if( enableTimer && !HAS_TIMER ){
6060 raw_printf(stderr, "Error: timer not available on this system.\n");
6061 enableTimer = 0;
6062 }
6063 }else{
6064 raw_printf(stderr, "Usage: .timer on|off\n");
6065 rc = 1;
6066 }
6067 }else
6068
6069 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
6070 open_db(p, 0);
6071 if( nArg!=2 ){
6072 raw_printf(stderr, "Usage: .trace FILE|off\n");
6073 rc = 1;
6074 goto meta_command_exit;
6075 }
6076 output_file_close(p->traceOut);
6077 p->traceOut = output_file_open(azArg[1]);
6078#if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
6079 if( p->traceOut==0 ){
6080 sqlite3_trace_v2(p->db, 0, 0, 0);
6081 }else{
6082 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
6083 }
6084#endif
6085 }else
6086
6087#if SQLITE_USER_AUTHENTICATION
6088 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
6089 if( nArg<2 ){
6090 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
6091 rc = 1;
6092 goto meta_command_exit;
6093 }
6094 open_db(p, 0);
6095 if( strcmp(azArg[1],"login")==0 ){
6096 if( nArg!=4 ){
6097 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
6098 rc = 1;
6099 goto meta_command_exit;
6100 }
6101 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
6102 (int)strlen(azArg[3]));
6103 if( rc ){
6104 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
6105 rc = 1;
6106 }
6107 }else if( strcmp(azArg[1],"add")==0 ){
6108 if( nArg!=5 ){
6109 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
6110 rc = 1;
6111 goto meta_command_exit;
6112 }
6113 rc = sqlite3_user_add(p->db, azArg[2],
6114 azArg[3], (int)strlen(azArg[3]),
6115 booleanValue(azArg[4]));
6116 if( rc ){
6117 raw_printf(stderr, "User-Add failed: %d\n", rc);
6118 rc = 1;
6119 }
6120 }else if( strcmp(azArg[1],"edit")==0 ){
6121 if( nArg!=5 ){
6122 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
6123 rc = 1;
6124 goto meta_command_exit;
6125 }
6126 rc = sqlite3_user_change(p->db, azArg[2],
6127 azArg[3], (int)strlen(azArg[3]),
6128 booleanValue(azArg[4]));
6129 if( rc ){
6130 raw_printf(stderr, "User-Edit failed: %d\n", rc);
6131 rc = 1;
6132 }
6133 }else if( strcmp(azArg[1],"delete")==0 ){
6134 if( nArg!=3 ){
6135 raw_printf(stderr, "Usage: .user delete USER\n");
6136 rc = 1;
6137 goto meta_command_exit;
6138 }
6139 rc = sqlite3_user_delete(p->db, azArg[2]);
6140 if( rc ){
6141 raw_printf(stderr, "User-Delete failed: %d\n", rc);
6142 rc = 1;
6143 }
6144 }else{
6145 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
6146 rc = 1;
6147 goto meta_command_exit;
6148 }
6149 }else
6150#endif /* SQLITE_USER_AUTHENTICATION */
6151
6152 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
6153 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
6154 sqlite3_libversion(), sqlite3_sourceid());
6155 }else
6156
6157 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
6158 const char *zDbName = nArg==2 ? azArg[1] : "main";
6159 sqlite3_vfs *pVfs = 0;
6160 if( p->db ){
6161 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
6162 if( pVfs ){
6163 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
6164 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6165 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6166 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6167 }
6168 }
6169 }else
6170
6171 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
6172 sqlite3_vfs *pVfs;
6173 sqlite3_vfs *pCurrent = 0;
6174 if( p->db ){
6175 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
6176 }
6177 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
6178 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
6179 pVfs==pCurrent ? " <--- CURRENT" : "");
6180 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
6181 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
6182 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
6183 if( pVfs->pNext ){
6184 raw_printf(p->out, "-----------------------------------\n");
6185 }
6186 }
6187 }else
6188
6189 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
6190 const char *zDbName = nArg==2 ? azArg[1] : "main";
6191 char *zVfsName = 0;
6192 if( p->db ){
6193 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
6194 if( zVfsName ){
6195 utf8_printf(p->out, "%s\n", zVfsName);
6196 sqlite3_free(zVfsName);
6197 }
6198 }
6199 }else
6200
6201#if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
6202 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
6203 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
6204 }else
6205#endif
6206
6207 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
6208 int j;
6209 assert( nArg<=ArraySize(azArg) );
6210 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
6211 p->colWidth[j-1] = (int)integerValue(azArg[j]);
6212 }
6213 }else
6214
6215 {
6216 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
6217 " \"%s\". Enter \".help\" for help\n", azArg[0]);
6218 rc = 1;
6219 }
6220
6221meta_command_exit:
6222 if( p->outCount ){
6223 p->outCount--;
6224 if( p->outCount==0 ) output_reset(p);
6225 }
6226 return rc;
6227}
6228
6229/*
6230** Return TRUE if a semicolon occurs anywhere in the first N characters
6231** of string z[].
6232*/
6233static int line_contains_semicolon(const char *z, int N){
6234 int i;
6235 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
6236 return 0;
6237}
6238
6239/*
6240** Test to see if a line consists entirely of whitespace.
6241*/
6242static int _all_whitespace(const char *z){
6243 for(; *z; z++){
6244 if( IsSpace(z[0]) ) continue;
6245 if( *z=='/' && z[1]=='*' ){
6246 z += 2;
6247 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
6248 if( *z==0 ) return 0;
6249 z++;
6250 continue;
6251 }
6252 if( *z=='-' && z[1]=='-' ){
6253 z += 2;
6254 while( *z && *z!='\n' ){ z++; }
6255 if( *z==0 ) return 1;
6256 continue;
6257 }
6258 return 0;
6259 }
6260 return 1;
6261}
6262
6263/*
6264** Return TRUE if the line typed in is an SQL command terminator other
6265** than a semi-colon. The SQL Server style "go" command is understood
6266** as is the Oracle "/".
6267*/
6268static int line_is_command_terminator(const char *zLine){
6269 while( IsSpace(zLine[0]) ){ zLine++; };
6270 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
6271 return 1; /* Oracle */
6272 }
6273 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
6274 && _all_whitespace(&zLine[2]) ){
6275 return 1; /* SQL Server */
6276 }
6277 return 0;
6278}
6279
6280/*
6281** Return true if zSql is a complete SQL statement. Return false if it
6282** ends in the middle of a string literal or C-style comment.
6283*/
6284static int line_is_complete(char *zSql, int nSql){
6285 int rc;
6286 if( zSql==0 ) return 1;
6287 zSql[nSql] = ';';
6288 zSql[nSql+1] = 0;
6289 rc = sqlite3_complete(zSql);
6290 zSql[nSql] = 0;
6291 return rc;
6292}
6293
6294/*
6295** Run a single line of SQL
6296*/
6297static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
6298 int rc;
6299 char *zErrMsg = 0;
6300
6301 open_db(p, 0);
6302 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
6303 BEGIN_TIMER;
6304 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
6305 END_TIMER;
6306 if( rc || zErrMsg ){
6307 char zPrefix[100];
6308 if( in!=0 || !stdin_is_interactive ){
6309 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
6310 "Error: near line %d:", startline);
6311 }else{
6312 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
6313 }
6314 if( zErrMsg!=0 ){
6315 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
6316 sqlite3_free(zErrMsg);
6317 zErrMsg = 0;
6318 }else{
6319 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
6320 }
6321 return 1;
6322 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
6323 raw_printf(p->out, "changes: %3d total_changes: %d\n",
6324 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
6325 }
6326 return 0;
6327}
6328
6329
6330/*
6331** Read input from *in and process it. If *in==0 then input
6332** is interactive - the user is typing it it. Otherwise, input
6333** is coming from a file or device. A prompt is issued and history
6334** is saved only if input is interactive. An interrupt signal will
6335** cause this routine to exit immediately, unless input is interactive.
6336**
6337** Return the number of errors.
6338*/
6339static int process_input(ShellState *p, FILE *in){
6340 char *zLine = 0; /* A single input line */
6341 char *zSql = 0; /* Accumulated SQL text */
6342 int nLine; /* Length of current line */
6343 int nSql = 0; /* Bytes of zSql[] used */
6344 int nAlloc = 0; /* Allocated zSql[] space */
6345 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
6346 int rc; /* Error code */
6347 int errCnt = 0; /* Number of errors seen */
6348 int lineno = 0; /* Current line number */
6349 int startline = 0; /* Line number for start of current input */
6350
6351 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
6352 fflush(p->out);
6353 zLine = one_input_line(in, zLine, nSql>0);
6354 if( zLine==0 ){
6355 /* End of input */
6356 if( in==0 && stdin_is_interactive ) printf("\n");
6357 break;
6358 }
6359 if( seenInterrupt ){
6360 if( in!=0 ) break;
6361 seenInterrupt = 0;
6362 }
6363 lineno++;
6364 if( nSql==0 && _all_whitespace(zLine) ){
6365 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6366 continue;
6367 }
6368 if( zLine && zLine[0]=='.' && nSql==0 ){
6369 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
6370 rc = do_meta_command(zLine, p);
6371 if( rc==2 ){ /* exit requested */
6372 break;
6373 }else if( rc ){
6374 errCnt++;
6375 }
6376 continue;
6377 }
6378 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
6379 memcpy(zLine,";",2);
6380 }
6381 nLine = strlen30(zLine);
6382 if( nSql+nLine+2>=nAlloc ){
6383 nAlloc = nSql+nLine+100;
6384 zSql = realloc(zSql, nAlloc);
6385 if( zSql==0 ){
6386 raw_printf(stderr, "Error: out of memory\n");
6387 exit(1);
6388 }
6389 }
6390 nSqlPrior = nSql;
6391 if( nSql==0 ){
6392 int i;
6393 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
6394 assert( nAlloc>0 && zSql!=0 );
6395 memcpy(zSql, zLine+i, nLine+1-i);
6396 startline = lineno;
6397 nSql = nLine-i;
6398 }else{
6399 zSql[nSql++] = '\n';
6400 memcpy(zSql+nSql, zLine, nLine+1);
6401 nSql += nLine;
6402 }
6403 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
6404 && sqlite3_complete(zSql) ){
6405 errCnt += runOneSqlLine(p, zSql, in, startline);
6406 nSql = 0;
6407 if( p->outCount ){
6408 output_reset(p);
6409 p->outCount = 0;
6410 }
6411 }else if( nSql && _all_whitespace(zSql) ){
6412 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
6413 nSql = 0;
6414 }
6415 }
6416 if( nSql && !_all_whitespace(zSql) ){
6417 runOneSqlLine(p, zSql, in, startline);
6418 }
6419 free(zSql);
6420 free(zLine);
6421 return errCnt>0;
6422}
6423
6424/*
6425** Return a pathname which is the user's home directory. A
6426** 0 return indicates an error of some kind.
6427*/
6428static char *find_home_dir(int clearFlag){
6429 static char *home_dir = NULL;
6430 if( clearFlag ){
6431 free(home_dir);
6432 home_dir = 0;
6433 return 0;
6434 }
6435 if( home_dir ) return home_dir;
6436
6437#if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
6438 && !defined(__RTP__) && !defined(_WRS_KERNEL)
6439 {
6440 struct passwd *pwent;
6441 uid_t uid = getuid();
6442 if( (pwent=getpwuid(uid)) != NULL) {
6443 home_dir = pwent->pw_dir;
6444 }
6445 }
6446#endif
6447
6448#if defined(_WIN32_WCE)
6449 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
6450 */
6451 home_dir = "/";
6452#else
6453
6454#if defined(_WIN32) || defined(WIN32)
6455 if (!home_dir) {
6456 home_dir = getenv("USERPROFILE");
6457 }
6458#endif
6459
6460 if (!home_dir) {
6461 home_dir = getenv("HOME");
6462 }
6463
6464#if defined(_WIN32) || defined(WIN32)
6465 if (!home_dir) {
6466 char *zDrive, *zPath;
6467 int n;
6468 zDrive = getenv("HOMEDRIVE");
6469 zPath = getenv("HOMEPATH");
6470 if( zDrive && zPath ){
6471 n = strlen30(zDrive) + strlen30(zPath) + 1;
6472 home_dir = malloc( n );
6473 if( home_dir==0 ) return 0;
6474 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
6475 return home_dir;
6476 }
6477 home_dir = "c:\\";
6478 }
6479#endif
6480
6481#endif /* !_WIN32_WCE */
6482
6483 if( home_dir ){
6484 int n = strlen30(home_dir) + 1;
6485 char *z = malloc( n );
6486 if( z ) memcpy(z, home_dir, n);
6487 home_dir = z;
6488 }
6489
6490 return home_dir;
6491}
6492
6493/*
6494** Read input from the file given by sqliterc_override. Or if that
6495** parameter is NULL, take input from ~/.sqliterc
6496**
6497** Returns the number of errors.
6498*/
6499static void process_sqliterc(
6500 ShellState *p, /* Configuration data */
6501 const char *sqliterc_override /* Name of config file. NULL to use default */
6502){
6503 char *home_dir = NULL;
6504 const char *sqliterc = sqliterc_override;
6505 char *zBuf = 0;
6506 FILE *in = NULL;
6507
6508 if (sqliterc == NULL) {
6509 home_dir = find_home_dir(0);
6510 if( home_dir==0 ){
6511 raw_printf(stderr, "-- warning: cannot find home directory;"
6512 " cannot read ~/.sqliterc\n");
6513 return;
6514 }
6515 sqlite3_initialize();
6516 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
6517 sqliterc = zBuf;
6518 }
6519 in = fopen(sqliterc,"rb");
6520 if( in ){
6521 if( stdin_is_interactive ){
6522 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
6523 }
6524 process_input(p,in);
6525 fclose(in);
6526 }
6527 sqlite3_free(zBuf);
6528}
6529
6530/*
6531** Show available command line options
6532*/
6533static const char zOptions[] =
6534 " -ascii set output mode to 'ascii'\n"
6535 " -bail stop after hitting an error\n"
6536 " -batch force batch I/O\n"
6537 " -column set output mode to 'column'\n"
6538 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
6539 " -csv set output mode to 'csv'\n"
6540 " -echo print commands before execution\n"
6541 " -init FILENAME read/process named file\n"
6542 " -[no]header turn headers on or off\n"
6543#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6544 " -heap SIZE Size of heap for memsys3 or memsys5\n"
6545#endif
6546 " -help show this message\n"
6547 " -html set output mode to HTML\n"
6548 " -interactive force interactive I/O\n"
6549 " -line set output mode to 'line'\n"
6550 " -list set output mode to 'list'\n"
6551 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
6552 " -mmap N default mmap size set to N\n"
6553#ifdef SQLITE_ENABLE_MULTIPLEX
6554 " -multiplex enable the multiplexor VFS\n"
6555#endif
6556 " -newline SEP set output row separator. Default: '\\n'\n"
6557 " -nullvalue TEXT set text string for NULL values. Default ''\n"
6558 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
6559 " -quote set output mode to 'quote'\n"
drh2ce15c32017-07-11 13:34:40 +00006560 " -separator SEP set output column separator. Default: '|'\n"
6561 " -stats print memory stats before each finalize\n"
6562 " -version show SQLite version\n"
6563 " -vfs NAME use NAME as the default VFS\n"
6564#ifdef SQLITE_ENABLE_VFSTRACE
6565 " -vfstrace enable tracing of all VFS calls\n"
6566#endif
6567;
6568static void usage(int showDetail){
6569 utf8_printf(stderr,
6570 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
6571 "FILENAME is the name of an SQLite database. A new database is created\n"
6572 "if the file does not previously exist.\n", Argv0);
6573 if( showDetail ){
6574 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
6575 }else{
6576 raw_printf(stderr, "Use the -help option for additional information\n");
6577 }
6578 exit(1);
6579}
6580
6581/*
6582** Initialize the state information in data
6583*/
6584static void main_init(ShellState *data) {
6585 memset(data, 0, sizeof(*data));
6586 data->normalMode = data->cMode = data->mode = MODE_List;
6587 data->autoExplain = 1;
6588 memcpy(data->colSeparator,SEP_Column, 2);
6589 memcpy(data->rowSeparator,SEP_Row, 2);
6590 data->showHeader = 0;
6591 data->shellFlgs = SHFLG_Lookaside;
6592 sqlite3_config(SQLITE_CONFIG_URI, 1);
6593 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
6594 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
6595 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
6596 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
6597}
6598
6599/*
6600** Output text to the console in a font that attracts extra attention.
6601*/
6602#ifdef _WIN32
6603static void printBold(const char *zText){
6604 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
6605 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
6606 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
6607 SetConsoleTextAttribute(out,
6608 FOREGROUND_RED|FOREGROUND_INTENSITY
6609 );
6610 printf("%s", zText);
6611 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
6612}
6613#else
6614static void printBold(const char *zText){
6615 printf("\033[1m%s\033[0m", zText);
6616}
6617#endif
6618
6619/*
6620** Get the argument to an --option. Throw an error and die if no argument
6621** is available.
6622*/
6623static char *cmdline_option_value(int argc, char **argv, int i){
6624 if( i==argc ){
6625 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
6626 argv[0], argv[argc-1]);
6627 exit(1);
6628 }
6629 return argv[i];
6630}
6631
6632#ifndef SQLITE_SHELL_IS_UTF8
6633# if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
6634# define SQLITE_SHELL_IS_UTF8 (0)
6635# else
6636# define SQLITE_SHELL_IS_UTF8 (1)
6637# endif
6638#endif
6639
6640#if SQLITE_SHELL_IS_UTF8
6641int SQLITE_CDECL main(int argc, char **argv){
6642#else
6643int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
6644 char **argv;
6645#endif
6646 char *zErrMsg = 0;
6647 ShellState data;
6648 const char *zInitFile = 0;
6649 int i;
6650 int rc = 0;
6651 int warnInmemoryDb = 0;
6652 int readStdin = 1;
6653 int nCmd = 0;
6654 char **azCmd = 0;
6655
6656 setBinaryMode(stdin, 0);
6657 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
6658 stdin_is_interactive = isatty(0);
6659 stdout_is_console = isatty(1);
6660
6661#if USE_SYSTEM_SQLITE+0!=1
drhb3c45232017-08-28 14:33:27 +00006662 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
drh2ce15c32017-07-11 13:34:40 +00006663 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
6664 sqlite3_sourceid(), SQLITE_SOURCE_ID);
6665 exit(1);
6666 }
6667#endif
6668 main_init(&data);
6669#if !SQLITE_SHELL_IS_UTF8
6670 sqlite3_initialize();
6671 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
6672 if( argv==0 ){
6673 raw_printf(stderr, "out of memory\n");
6674 exit(1);
6675 }
6676 for(i=0; i<argc; i++){
6677 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
6678 if( argv[i]==0 ){
6679 raw_printf(stderr, "out of memory\n");
6680 exit(1);
6681 }
6682 }
6683#endif
6684 assert( argc>=1 && argv && argv[0] );
6685 Argv0 = argv[0];
6686
6687 /* Make sure we have a valid signal handler early, before anything
6688 ** else is done.
6689 */
6690#ifdef SIGINT
6691 signal(SIGINT, interrupt_handler);
6692#endif
6693
6694#ifdef SQLITE_SHELL_DBNAME_PROC
6695 {
6696 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
6697 ** of a C-function that will provide the name of the database file. Use
6698 ** this compile-time option to embed this shell program in larger
6699 ** applications. */
6700 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
6701 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
6702 warnInmemoryDb = 0;
6703 }
6704#endif
6705
6706 /* Do an initial pass through the command-line argument to locate
6707 ** the name of the database file, the name of the initialization file,
6708 ** the size of the alternative malloc heap,
6709 ** and the first command to execute.
6710 */
6711 for(i=1; i<argc; i++){
6712 char *z;
6713 z = argv[i];
6714 if( z[0]!='-' ){
6715 if( data.zDbFilename==0 ){
6716 data.zDbFilename = z;
6717 }else{
6718 /* Excesss arguments are interpreted as SQL (or dot-commands) and
6719 ** mean that nothing is read from stdin */
6720 readStdin = 0;
6721 nCmd++;
6722 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
6723 if( azCmd==0 ){
6724 raw_printf(stderr, "out of memory\n");
6725 exit(1);
6726 }
6727 azCmd[nCmd-1] = z;
6728 }
6729 }
6730 if( z[1]=='-' ) z++;
6731 if( strcmp(z,"-separator")==0
6732 || strcmp(z,"-nullvalue")==0
6733 || strcmp(z,"-newline")==0
6734 || strcmp(z,"-cmd")==0
6735 ){
6736 (void)cmdline_option_value(argc, argv, ++i);
6737 }else if( strcmp(z,"-init")==0 ){
6738 zInitFile = cmdline_option_value(argc, argv, ++i);
6739 }else if( strcmp(z,"-batch")==0 ){
6740 /* Need to check for batch mode here to so we can avoid printing
6741 ** informational messages (like from process_sqliterc) before
6742 ** we do the actual processing of arguments later in a second pass.
6743 */
6744 stdin_is_interactive = 0;
6745 }else if( strcmp(z,"-heap")==0 ){
6746#if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
6747 const char *zSize;
6748 sqlite3_int64 szHeap;
6749
6750 zSize = cmdline_option_value(argc, argv, ++i);
6751 szHeap = integerValue(zSize);
6752 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
6753 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
6754#else
6755 (void)cmdline_option_value(argc, argv, ++i);
6756#endif
drh2ce15c32017-07-11 13:34:40 +00006757 }else if( strcmp(z,"-pagecache")==0 ){
6758 int n, sz;
6759 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6760 if( sz>70000 ) sz = 70000;
6761 if( sz<0 ) sz = 0;
6762 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6763 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
6764 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
6765 data.shellFlgs |= SHFLG_Pagecache;
6766 }else if( strcmp(z,"-lookaside")==0 ){
6767 int n, sz;
6768 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
6769 if( sz<0 ) sz = 0;
6770 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
6771 if( n<0 ) n = 0;
6772 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
6773 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
6774#ifdef SQLITE_ENABLE_VFSTRACE
6775 }else if( strcmp(z,"-vfstrace")==0 ){
6776 extern int vfstrace_register(
6777 const char *zTraceName,
6778 const char *zOldVfsName,
6779 int (*xOut)(const char*,void*),
6780 void *pOutArg,
6781 int makeDefault
6782 );
6783 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
6784#endif
6785#ifdef SQLITE_ENABLE_MULTIPLEX
6786 }else if( strcmp(z,"-multiplex")==0 ){
6787 extern int sqlite3_multiple_initialize(const char*,int);
6788 sqlite3_multiplex_initialize(0, 1);
6789#endif
6790 }else if( strcmp(z,"-mmap")==0 ){
6791 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
6792 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
6793 }else if( strcmp(z,"-vfs")==0 ){
6794 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
6795 if( pVfs ){
6796 sqlite3_vfs_register(pVfs, 1);
6797 }else{
6798 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
6799 exit(1);
6800 }
6801 }
6802 }
6803 if( data.zDbFilename==0 ){
6804#ifndef SQLITE_OMIT_MEMORYDB
6805 data.zDbFilename = ":memory:";
6806 warnInmemoryDb = argc==1;
6807#else
6808 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
6809 return 1;
6810#endif
6811 }
6812 data.out = stdout;
6813
6814 /* Go ahead and open the database file if it already exists. If the
6815 ** file does not exist, delay opening it. This prevents empty database
6816 ** files from being created if a user mistypes the database name argument
6817 ** to the sqlite command-line tool.
6818 */
6819 if( access(data.zDbFilename, 0)==0 ){
6820 open_db(&data, 0);
6821 }
6822
6823 /* Process the initialization file if there is one. If no -init option
6824 ** is given on the command line, look for a file named ~/.sqliterc and
6825 ** try to process it.
6826 */
6827 process_sqliterc(&data,zInitFile);
6828
6829 /* Make a second pass through the command-line argument and set
6830 ** options. This second pass is delayed until after the initialization
6831 ** file is processed so that the command-line arguments will override
6832 ** settings in the initialization file.
6833 */
6834 for(i=1; i<argc; i++){
6835 char *z = argv[i];
6836 if( z[0]!='-' ) continue;
6837 if( z[1]=='-' ){ z++; }
6838 if( strcmp(z,"-init")==0 ){
6839 i++;
6840 }else if( strcmp(z,"-html")==0 ){
6841 data.mode = MODE_Html;
6842 }else if( strcmp(z,"-list")==0 ){
6843 data.mode = MODE_List;
6844 }else if( strcmp(z,"-quote")==0 ){
6845 data.mode = MODE_Quote;
6846 }else if( strcmp(z,"-line")==0 ){
6847 data.mode = MODE_Line;
6848 }else if( strcmp(z,"-column")==0 ){
6849 data.mode = MODE_Column;
6850 }else if( strcmp(z,"-csv")==0 ){
6851 data.mode = MODE_Csv;
6852 memcpy(data.colSeparator,",",2);
6853 }else if( strcmp(z,"-ascii")==0 ){
6854 data.mode = MODE_Ascii;
6855 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6856 SEP_Unit);
6857 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6858 SEP_Record);
6859 }else if( strcmp(z,"-separator")==0 ){
6860 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
6861 "%s",cmdline_option_value(argc,argv,++i));
6862 }else if( strcmp(z,"-newline")==0 ){
6863 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
6864 "%s",cmdline_option_value(argc,argv,++i));
6865 }else if( strcmp(z,"-nullvalue")==0 ){
6866 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
6867 "%s",cmdline_option_value(argc,argv,++i));
6868 }else if( strcmp(z,"-header")==0 ){
6869 data.showHeader = 1;
6870 }else if( strcmp(z,"-noheader")==0 ){
6871 data.showHeader = 0;
6872 }else if( strcmp(z,"-echo")==0 ){
6873 ShellSetFlag(&data, SHFLG_Echo);
6874 }else if( strcmp(z,"-eqp")==0 ){
6875 data.autoEQP = 1;
6876 }else if( strcmp(z,"-eqpfull")==0 ){
6877 data.autoEQP = 2;
6878 }else if( strcmp(z,"-stats")==0 ){
6879 data.statsOn = 1;
6880 }else if( strcmp(z,"-scanstats")==0 ){
6881 data.scanstatsOn = 1;
6882 }else if( strcmp(z,"-backslash")==0 ){
6883 /* Undocumented command-line option: -backslash
6884 ** Causes C-style backslash escapes to be evaluated in SQL statements
6885 ** prior to sending the SQL into SQLite. Useful for injecting
6886 ** crazy bytes in the middle of SQL statements for testing and debugging.
6887 */
6888 ShellSetFlag(&data, SHFLG_Backslash);
6889 }else if( strcmp(z,"-bail")==0 ){
6890 bail_on_error = 1;
6891 }else if( strcmp(z,"-version")==0 ){
6892 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
6893 return 0;
6894 }else if( strcmp(z,"-interactive")==0 ){
6895 stdin_is_interactive = 1;
6896 }else if( strcmp(z,"-batch")==0 ){
6897 stdin_is_interactive = 0;
6898 }else if( strcmp(z,"-heap")==0 ){
6899 i++;
drh2ce15c32017-07-11 13:34:40 +00006900 }else if( strcmp(z,"-pagecache")==0 ){
6901 i+=2;
6902 }else if( strcmp(z,"-lookaside")==0 ){
6903 i+=2;
6904 }else if( strcmp(z,"-mmap")==0 ){
6905 i++;
6906 }else if( strcmp(z,"-vfs")==0 ){
6907 i++;
6908#ifdef SQLITE_ENABLE_VFSTRACE
6909 }else if( strcmp(z,"-vfstrace")==0 ){
6910 i++;
6911#endif
6912#ifdef SQLITE_ENABLE_MULTIPLEX
6913 }else if( strcmp(z,"-multiplex")==0 ){
6914 i++;
6915#endif
6916 }else if( strcmp(z,"-help")==0 ){
6917 usage(1);
6918 }else if( strcmp(z,"-cmd")==0 ){
6919 /* Run commands that follow -cmd first and separately from commands
6920 ** that simply appear on the command-line. This seems goofy. It would
6921 ** be better if all commands ran in the order that they appear. But
6922 ** we retain the goofy behavior for historical compatibility. */
6923 if( i==argc-1 ) break;
6924 z = cmdline_option_value(argc,argv,++i);
6925 if( z[0]=='.' ){
6926 rc = do_meta_command(z, &data);
6927 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
6928 }else{
6929 open_db(&data, 0);
6930 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
6931 if( zErrMsg!=0 ){
6932 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6933 if( bail_on_error ) return rc!=0 ? rc : 1;
6934 }else if( rc!=0 ){
6935 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
6936 if( bail_on_error ) return rc;
6937 }
6938 }
6939 }else{
6940 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
6941 raw_printf(stderr,"Use -help for a list of options.\n");
6942 return 1;
6943 }
6944 data.cMode = data.mode;
6945 }
6946
6947 if( !readStdin ){
6948 /* Run all arguments that do not begin with '-' as if they were separate
6949 ** command-line inputs, except for the argToSkip argument which contains
6950 ** the database filename.
6951 */
6952 for(i=0; i<nCmd; i++){
6953 if( azCmd[i][0]=='.' ){
6954 rc = do_meta_command(azCmd[i], &data);
6955 if( rc ) return rc==2 ? 0 : rc;
6956 }else{
6957 open_db(&data, 0);
6958 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
6959 if( zErrMsg!=0 ){
6960 utf8_printf(stderr,"Error: %s\n", zErrMsg);
6961 return rc!=0 ? rc : 1;
6962 }else if( rc!=0 ){
6963 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
6964 return rc;
6965 }
6966 }
6967 }
6968 free(azCmd);
6969 }else{
6970 /* Run commands received from standard input
6971 */
6972 if( stdin_is_interactive ){
6973 char *zHome;
6974 char *zHistory = 0;
6975 int nHistory;
6976 printf(
6977 "SQLite version %s %.19s\n" /*extra-version-info*/
6978 "Enter \".help\" for usage hints.\n",
6979 sqlite3_libversion(), sqlite3_sourceid()
6980 );
6981 if( warnInmemoryDb ){
6982 printf("Connected to a ");
6983 printBold("transient in-memory database");
6984 printf(".\nUse \".open FILENAME\" to reopen on a "
6985 "persistent database.\n");
6986 }
6987 zHome = find_home_dir(0);
6988 if( zHome ){
6989 nHistory = strlen30(zHome) + 20;
6990 if( (zHistory = malloc(nHistory))!=0 ){
6991 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
6992 }
6993 }
6994 if( zHistory ){ shell_read_history(zHistory); }
drh56eb09b2017-07-11 13:59:07 +00006995#if HAVE_READLINE || HAVE_EDITLINE
6996 rl_attempted_completion_function = readline_completion;
6997#elif HAVE_LINENOISE
6998 linenoiseSetCompletionCallback(linenoise_completion);
6999#endif
drh2ce15c32017-07-11 13:34:40 +00007000 rc = process_input(&data, 0);
7001 if( zHistory ){
drh5a75dd82017-07-18 20:59:40 +00007002 shell_stifle_history(2000);
drh2ce15c32017-07-11 13:34:40 +00007003 shell_write_history(zHistory);
7004 free(zHistory);
7005 }
7006 }else{
7007 rc = process_input(&data, stdin);
7008 }
7009 }
7010 set_table_name(&data, 0);
7011 if( data.db ){
7012 session_close_all(&data);
7013 sqlite3_close(data.db);
7014 }
7015 sqlite3_free(data.zFreeOnClose);
7016 find_home_dir(1);
7017#if !SQLITE_SHELL_IS_UTF8
7018 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
7019 sqlite3_free(argv);
7020#endif
7021 return rc;
7022}