4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
12 ** This file contains code to implement the "sqlite" command line
13 ** utility for accessing SQLite databases.
15 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
16 /* This needs to come before any includes for MSVC compiler */
17 #define _CRT_SECURE_NO_WARNINGS
21 ** Warning pragmas copied from msvc.h in the core.
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) */
42 ** No support for loadable extensions in VxWorks.
44 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
45 # define SQLITE_OMIT_LOAD_EXTENSION 1
49 ** Enable large-file support for fopen() and friends on unix.
51 #ifndef SQLITE_DISABLE_LFS
52 # define _LARGE_FILE 1
53 # ifndef _FILE_OFFSET_BITS
54 # define _FILE_OFFSET_BITS 64
56 # define _LARGEFILE_SOURCE 1
64 typedef sqlite3_int64 i64
;
65 typedef sqlite3_uint64 u64
;
66 typedef unsigned char u8
;
67 #if SQLITE_USER_AUTHENTICATION
68 # include "sqlite3userauth.h"
73 #if !defined(_WIN32) && !defined(WIN32)
75 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
79 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
82 # if defined(__MINGW32__)
83 # define DIRENT dirent
85 # define S_ISLNK(mode) (0)
89 #include <sys/types.h>
93 # include <readline/readline.h>
94 # include <readline/history.h>
98 # include <editline/readline.h>
101 #if HAVE_EDITLINE || HAVE_READLINE
103 # define shell_add_history(X) add_history(X)
104 # define shell_read_history(X) read_history(X)
105 # define shell_write_history(X) write_history(X)
106 # define shell_stifle_history(X) stifle_history(X)
107 # define shell_readline(X) readline(X)
111 # include "linenoise.h"
112 # define shell_add_history(X) linenoiseHistoryAdd(X)
113 # define shell_read_history(X) linenoiseHistoryLoad(X)
114 # define shell_write_history(X) linenoiseHistorySave(X)
115 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
116 # define shell_readline(X) linenoise(X)
120 # define shell_read_history(X)
121 # define shell_write_history(X)
122 # define shell_stifle_history(X)
124 # define SHELL_USE_LOCAL_GETLINE 1
128 #if defined(_WIN32) || defined(WIN32)
131 # define isatty(h) _isatty(h)
133 # define access(f,m) _access((f),(m))
136 # define unlink _unlink
139 # define popen _popen
141 # define pclose _pclose
143 /* Make sure isatty() has a prototype. */
144 extern int isatty(int);
146 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
147 /* popen and pclose are not C89 functions and so are
148 ** sometimes omitted from the <stdio.h> header */
149 extern FILE *popen(const char*,const char*);
150 extern int pclose(FILE*);
152 # define SQLITE_OMIT_POPEN 1
156 #if defined(_WIN32_WCE)
157 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
158 * thus we always assume that we have a console. That can be
159 * overridden with the -batch command line option.
164 /* ctype macros that work with signed characters */
165 #define IsSpace(X) isspace((unsigned char)X)
166 #define IsDigit(X) isdigit((unsigned char)X)
167 #define ToLower(X) (char)tolower((unsigned char)X)
169 #if defined(_WIN32) || defined(WIN32)
172 /* string conversion routines only needed on Win32 */
173 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR
);
174 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
175 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
176 extern LPWSTR
sqlite3_win32_utf8_to_unicode(const char *zText
);
179 /* On Windows, we normally run with output mode of TEXT so that \n characters
180 ** are automatically translated into \r\n. However, this behavior needs
181 ** to be disabled in some cases (ex: when generating CSV output and when
182 ** rendering quoted strings that contain \n characters). The following
183 ** routines take care of that.
185 #if defined(_WIN32) || defined(WIN32)
186 static void setBinaryMode(FILE *file
, int isOutput
){
187 if( isOutput
) fflush(file
);
188 _setmode(_fileno(file
), _O_BINARY
);
190 static void setTextMode(FILE *file
, int isOutput
){
191 if( isOutput
) fflush(file
);
192 _setmode(_fileno(file
), _O_TEXT
);
195 # define setBinaryMode(X,Y)
196 # define setTextMode(X,Y)
200 /* True if the timer is enabled */
201 static int enableTimer
= 0;
203 /* Return the current wall-clock time */
204 static sqlite3_int64
timeOfDay(void){
205 static sqlite3_vfs
*clockVfs
= 0;
207 if( clockVfs
==0 ) clockVfs
= sqlite3_vfs_find(0);
208 if( clockVfs
->iVersion
>=2 && clockVfs
->xCurrentTimeInt64
!=0 ){
209 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
212 clockVfs
->xCurrentTime(clockVfs
, &r
);
213 t
= (sqlite3_int64
)(r
*86400000.0);
218 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
219 #include <sys/time.h>
220 #include <sys/resource.h>
222 /* VxWorks does not support getrusage() as far as we can determine */
223 #if defined(_WRS_KERNEL) || defined(__RTP__)
225 struct timeval ru_utime
; /* user CPU time used */
226 struct timeval ru_stime
; /* system CPU time used */
228 #define getrusage(A,B) memset(B,0,sizeof(*B))
231 /* Saved resource information for the beginning of an operation */
232 static struct rusage sBegin
; /* CPU time at start */
233 static sqlite3_int64 iBegin
; /* Wall-clock time at start */
236 ** Begin timing an operation
238 static void beginTimer(void){
240 getrusage(RUSAGE_SELF
, &sBegin
);
241 iBegin
= timeOfDay();
245 /* Return the difference of two time_structs in seconds */
246 static double timeDiff(struct timeval
*pStart
, struct timeval
*pEnd
){
247 return (pEnd
->tv_usec
- pStart
->tv_usec
)*0.000001 +
248 (double)(pEnd
->tv_sec
- pStart
->tv_sec
);
252 ** Print the timing results.
254 static void endTimer(void){
256 sqlite3_int64 iEnd
= timeOfDay();
258 getrusage(RUSAGE_SELF
, &sEnd
);
259 printf("Run Time: real %.3f user %f sys %f\n",
260 (iEnd
- iBegin
)*0.001,
261 timeDiff(&sBegin
.ru_utime
, &sEnd
.ru_utime
),
262 timeDiff(&sBegin
.ru_stime
, &sEnd
.ru_stime
));
266 #define BEGIN_TIMER beginTimer()
267 #define END_TIMER endTimer()
270 #elif (defined(_WIN32) || defined(WIN32))
272 /* Saved resource information for the beginning of an operation */
273 static HANDLE hProcess
;
274 static FILETIME ftKernelBegin
;
275 static FILETIME ftUserBegin
;
276 static sqlite3_int64 ftWallBegin
;
277 typedef BOOL (WINAPI
*GETPROCTIMES
)(HANDLE
, LPFILETIME
, LPFILETIME
,
278 LPFILETIME
, LPFILETIME
);
279 static GETPROCTIMES getProcessTimesAddr
= NULL
;
282 ** Check to see if we have timer support. Return 1 if necessary
283 ** support found (or found previously).
285 static int hasTimer(void){
286 if( getProcessTimesAddr
){
289 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
290 ** versions. See if the version we are running on has it, and if it
291 ** does, save off a pointer to it and the current process handle.
293 hProcess
= GetCurrentProcess();
295 HINSTANCE hinstLib
= LoadLibrary(TEXT("Kernel32.dll"));
296 if( NULL
!= hinstLib
){
297 getProcessTimesAddr
=
298 (GETPROCTIMES
) GetProcAddress(hinstLib
, "GetProcessTimes");
299 if( NULL
!= getProcessTimesAddr
){
302 FreeLibrary(hinstLib
);
310 ** Begin timing an operation
312 static void beginTimer(void){
313 if( enableTimer
&& getProcessTimesAddr
){
314 FILETIME ftCreation
, ftExit
;
315 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,
316 &ftKernelBegin
,&ftUserBegin
);
317 ftWallBegin
= timeOfDay();
321 /* Return the difference of two FILETIME structs in seconds */
322 static double timeDiff(FILETIME
*pStart
, FILETIME
*pEnd
){
323 sqlite_int64 i64Start
= *((sqlite_int64
*) pStart
);
324 sqlite_int64 i64End
= *((sqlite_int64
*) pEnd
);
325 return (double) ((i64End
- i64Start
) / 10000000.0);
329 ** Print the timing results.
331 static void endTimer(void){
332 if( enableTimer
&& getProcessTimesAddr
){
333 FILETIME ftCreation
, ftExit
, ftKernelEnd
, ftUserEnd
;
334 sqlite3_int64 ftWallEnd
= timeOfDay();
335 getProcessTimesAddr(hProcess
,&ftCreation
,&ftExit
,&ftKernelEnd
,&ftUserEnd
);
336 printf("Run Time: real %.3f user %f sys %f\n",
337 (ftWallEnd
- ftWallBegin
)*0.001,
338 timeDiff(&ftUserBegin
, &ftUserEnd
),
339 timeDiff(&ftKernelBegin
, &ftKernelEnd
));
343 #define BEGIN_TIMER beginTimer()
344 #define END_TIMER endTimer()
345 #define HAS_TIMER hasTimer()
354 ** Used to prevent warnings about unused parameters
356 #define UNUSED_PARAMETER(x) (void)(x)
359 ** Number of elements in an array
361 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
364 ** If the following flag is set, then command execution stops
365 ** at an error if we are not interactive.
367 static int bail_on_error
= 0;
370 ** Threat stdin as an interactive input if the following variable
371 ** is true. Otherwise, assume stdin is connected to a file or pipe.
373 static int stdin_is_interactive
= 1;
376 ** On Windows systems we have to know if standard output is a console
377 ** in order to translate UTF-8 into MBCS. The following variable is
378 ** true if translation is required.
380 static int stdout_is_console
= 1;
383 ** The following is the open SQLite database. We make a pointer
384 ** to this database a static variable so that it can be accessed
385 ** by the SIGINT handler to interrupt database processing.
387 static sqlite3
*globalDb
= 0;
390 ** True if an interrupt (Control-C) has been received.
392 static volatile int seenInterrupt
= 0;
395 ** This is the name of our program. It is set in main(), used
396 ** in a number of other places, mostly for error messages.
401 ** Prompt strings. Initialized in main. Settable with
402 ** .prompt main continue
404 static char mainPrompt
[20]; /* First line prompt. default: "sqlite> "*/
405 static char continuePrompt
[20]; /* Continuation prompt. default: " ...> " */
408 ** Render output like fprintf(). Except, if the output is going to the
409 ** console and if this is running on a Windows machine, translate the
410 ** output from UTF-8 into MBCS.
412 #if defined(_WIN32) || defined(WIN32)
413 void utf8_printf(FILE *out
, const char *zFormat
, ...){
415 va_start(ap
, zFormat
);
416 if( stdout_is_console
&& (out
==stdout
|| out
==stderr
) ){
417 char *z1
= sqlite3_vmprintf(zFormat
, ap
);
418 char *z2
= sqlite3_win32_utf8_to_mbcs_v2(z1
, 0);
423 vfprintf(out
, zFormat
, ap
);
427 #elif !defined(utf8_printf)
428 # define utf8_printf fprintf
432 ** Render output like fprintf(). This should not be used on anything that
433 ** includes string formatting (e.g. "%s").
435 #if !defined(raw_printf)
436 # define raw_printf fprintf
439 /* Indicate out-of-memory and exit. */
440 static void shell_out_of_memory(void){
441 raw_printf(stderr
,"Error: out of memory\n");
446 ** Write I/O traces to the following stream.
448 #ifdef SQLITE_ENABLE_IOTRACE
449 static FILE *iotrace
= 0;
453 ** This routine works like printf in that its first argument is a
454 ** format string and subsequent arguments are values to be substituted
455 ** in place of % fields. The result of formatting this string
456 ** is written to iotrace.
458 #ifdef SQLITE_ENABLE_IOTRACE
459 static void SQLITE_CDECL
iotracePrintf(const char *zFormat
, ...){
462 if( iotrace
==0 ) return;
463 va_start(ap
, zFormat
);
464 z
= sqlite3_vmprintf(zFormat
, ap
);
466 utf8_printf(iotrace
, "%s", z
);
472 ** Output string zUtf to stream pOut as w characters. If w is negative,
473 ** then right-justify the text. W is the width in UTF-8 characters, not
474 ** in bytes. This is different from the %*.*s specification in printf
475 ** since with %*.*s the width is measured in bytes, not characters.
477 static void utf8_width_print(FILE *pOut
, int w
, const char *zUtf
){
480 int aw
= w
<0 ? -w
: w
;
482 if( aw
>(int)sizeof(zBuf
)/3 ) aw
= (int)sizeof(zBuf
)/3;
483 for(i
=n
=0; zUtf
[i
]; i
++){
484 if( (zUtf
[i
]&0xc0)!=0x80 ){
487 do{ i
++; }while( (zUtf
[i
]&0xc0)==0x80 );
493 utf8_printf(pOut
, "%.*s", i
, zUtf
);
495 utf8_printf(pOut
, "%*s%s", aw
-n
, "", zUtf
);
497 utf8_printf(pOut
, "%s%*s", zUtf
, aw
-n
, "");
503 ** Determines if a string is a number of not.
505 static int isNumber(const char *z
, int *realnum
){
506 if( *z
=='-' || *z
=='+' ) z
++;
511 if( realnum
) *realnum
= 0;
512 while( IsDigit(*z
) ){ z
++; }
515 if( !IsDigit(*z
) ) return 0;
516 while( IsDigit(*z
) ){ z
++; }
517 if( realnum
) *realnum
= 1;
519 if( *z
=='e' || *z
=='E' ){
521 if( *z
=='+' || *z
=='-' ) z
++;
522 if( !IsDigit(*z
) ) return 0;
523 while( IsDigit(*z
) ){ z
++; }
524 if( realnum
) *realnum
= 1;
530 ** Compute a string length that is limited to what can be stored in
531 ** lower 30 bits of a 32-bit signed integer.
533 static int strlen30(const char *z
){
535 while( *z2
){ z2
++; }
536 return 0x3fffffff & (int)(z2
- z
);
540 ** Return the length of a string in characters. Multibyte UTF8 characters
541 ** count as a single character.
543 static int strlenChar(const char *z
){
546 if( (0xc0&*(z
++))!=0x80 ) n
++;
552 ** This routine reads a line of text from FILE in, stores
553 ** the text in memory obtained from malloc() and returns a pointer
554 ** to the text. NULL is returned at end of file, or if malloc()
557 ** If zLine is not NULL then it is a malloced buffer returned from
558 ** a previous call to this routine that may be reused.
560 static char *local_getline(char *zLine
, FILE *in
){
561 int nLine
= zLine
==0 ? 0 : 100;
566 nLine
= nLine
*2 + 100;
567 zLine
= realloc(zLine
, nLine
);
568 if( zLine
==0 ) return 0;
570 if( fgets(&zLine
[n
], nLine
- n
, in
)==0 ){
578 while( zLine
[n
] ) n
++;
579 if( n
>0 && zLine
[n
-1]=='\n' ){
581 if( n
>0 && zLine
[n
-1]=='\r' ) n
--;
586 #if defined(_WIN32) || defined(WIN32)
587 /* For interactive input on Windows systems, translate the
588 ** multi-byte characterset characters into UTF-8. */
589 if( stdin_is_interactive
&& in
==stdin
){
590 char *zTrans
= sqlite3_win32_mbcs_to_utf8_v2(zLine
, 0);
592 int nTrans
= strlen30(zTrans
)+1;
594 zLine
= realloc(zLine
, nTrans
);
596 sqlite3_free(zTrans
);
600 memcpy(zLine
, zTrans
, nTrans
);
601 sqlite3_free(zTrans
);
604 #endif /* defined(_WIN32) || defined(WIN32) */
609 ** Retrieve a single line of input text.
611 ** If in==0 then read from standard input and prompt before each line.
612 ** If isContinuation is true, then a continuation prompt is appropriate.
613 ** If isContinuation is zero, then the main prompt should be used.
615 ** If zPrior is not NULL then it is a buffer from a prior call to this
616 ** routine that can be reused.
618 ** The result is stored in space obtained from malloc() and must either
619 ** be freed by the caller or else passed back into this routine via the
620 ** zPrior argument for reuse.
622 static char *one_input_line(FILE *in
, char *zPrior
, int isContinuation
){
626 zResult
= local_getline(zPrior
, in
);
628 zPrompt
= isContinuation
? continuePrompt
: mainPrompt
;
629 #if SHELL_USE_LOCAL_GETLINE
630 printf("%s", zPrompt
);
632 zResult
= local_getline(zPrior
, stdin
);
635 zResult
= shell_readline(zPrompt
);
636 if( zResult
&& *zResult
) shell_add_history(zResult
);
644 ** Return the value of a hexadecimal digit. Return -1 if the input
645 ** is not a hex digit.
647 static int hexDigitValue(char c
){
648 if( c
>='0' && c
<='9' ) return c
- '0';
649 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
650 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
655 ** Interpret zArg as an integer value, possibly with suffixes.
657 static sqlite3_int64
integerValue(const char *zArg
){
659 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
661 { "MiB", 1024*1024 },
662 { "GiB", 1024*1024*1024 },
665 { "GB", 1000000000 },
675 }else if( zArg
[0]=='+' ){
678 if( zArg
[0]=='0' && zArg
[1]=='x' ){
681 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
686 while( IsDigit(zArg
[0]) ){
687 v
= v
*10 + zArg
[0] - '0';
691 for(i
=0; i
<ArraySize(aMult
); i
++){
692 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
697 return isNeg
? -v
: v
;
701 ** A variable length string to which one can append text.
703 typedef struct ShellText ShellText
;
711 ** Initialize and destroy a ShellText object
713 static void initText(ShellText
*p
){
714 memset(p
, 0, sizeof(*p
));
716 static void freeText(ShellText
*p
){
721 /* zIn is either a pointer to a NULL-terminated string in memory obtained
722 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
723 ** added to zIn, and the result returned in memory obtained from malloc().
724 ** zIn, if it was not NULL, is freed.
726 ** If the third argument, quote, is not '\0', then it is used as a
727 ** quote character for zAppend.
729 static void appendText(ShellText
*p
, char const *zAppend
, char quote
){
732 int nAppend
= strlen30(zAppend
);
734 len
= nAppend
+p
->n
+1;
737 for(i
=0; i
<nAppend
; i
++){
738 if( zAppend
[i
]==quote
) len
++;
742 if( p
->n
+len
>=p
->nAlloc
){
743 p
->nAlloc
= p
->nAlloc
*2 + len
+ 20;
744 p
->z
= realloc(p
->z
, p
->nAlloc
);
746 memset(p
, 0, sizeof(*p
));
752 char *zCsr
= p
->z
+p
->n
;
754 for(i
=0; i
<nAppend
; i
++){
755 *zCsr
++ = zAppend
[i
];
756 if( zAppend
[i
]==quote
) *zCsr
++ = quote
;
759 p
->n
= (int)(zCsr
- p
->z
);
762 memcpy(p
->z
+p
->n
, zAppend
, nAppend
);
769 ** Attempt to determine if identifier zName needs to be quoted, either
770 ** because it contains non-alphanumeric characters, or because it is an
771 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
772 ** that quoting is required.
774 ** Return '"' if quoting is required. Return 0 if no quoting is required.
776 static char quoteChar(const char *zName
){
778 if( !isalpha((unsigned char)zName
[0]) && zName
[0]!='_' ) return '"';
779 for(i
=0; zName
[i
]; i
++){
780 if( !isalnum((unsigned char)zName
[i
]) && zName
[i
]!='_' ) return '"';
782 return sqlite3_keyword_check(zName
, i
) ? '"' : 0;
786 ** Construct a fake object name and column list to describe the structure
787 ** of the view, virtual table, or table valued function zSchema.zName.
789 static char *shellFakeSchema(
790 sqlite3
*db
, /* The database connection containing the vtab */
791 const char *zSchema
, /* Schema of the database holding the vtab */
792 const char *zName
/* The name of the virtual table */
794 sqlite3_stmt
*pStmt
= 0;
801 zSql
= sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
802 zSchema
? zSchema
: "main", zName
);
803 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
807 cQuote
= quoteChar(zSchema
);
808 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")==0 ) cQuote
= 0;
809 appendText(&s
, zSchema
, cQuote
);
810 appendText(&s
, ".", 0);
812 cQuote
= quoteChar(zName
);
813 appendText(&s
, zName
, cQuote
);
814 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
815 const char *zCol
= (const char*)sqlite3_column_text(pStmt
, 1);
817 appendText(&s
, zDiv
, 0);
819 cQuote
= quoteChar(zCol
);
820 appendText(&s
, zCol
, cQuote
);
822 appendText(&s
, ")", 0);
823 sqlite3_finalize(pStmt
);
832 ** SQL function: shell_module_schema(X)
834 ** Return a fake schema for the table-valued function or eponymous virtual
837 static void shellModuleSchema(
838 sqlite3_context
*pCtx
,
840 sqlite3_value
**apVal
842 const char *zName
= (const char*)sqlite3_value_text(apVal
[0]);
843 char *zFake
= shellFakeSchema(sqlite3_context_db_handle(pCtx
), 0, zName
);
844 UNUSED_PARAMETER(nVal
);
846 sqlite3_result_text(pCtx
, sqlite3_mprintf("/* %s */", zFake
),
853 ** SQL function: shell_add_schema(S,X)
855 ** Add the schema name X to the CREATE statement in S and return the result.
858 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
863 ** CREATE UNIQUE INDEX
866 ** CREATE VIRTUAL TABLE
868 ** This UDF is used by the .schema command to insert the schema name of
869 ** attached databases into the middle of the sqlite_master.sql field.
871 static void shellAddSchemaName(
872 sqlite3_context
*pCtx
,
874 sqlite3_value
**apVal
876 static const char *aPrefix
[] = {
885 const char *zIn
= (const char*)sqlite3_value_text(apVal
[0]);
886 const char *zSchema
= (const char*)sqlite3_value_text(apVal
[1]);
887 const char *zName
= (const char*)sqlite3_value_text(apVal
[2]);
888 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
889 UNUSED_PARAMETER(nVal
);
890 if( zIn
!=0 && strncmp(zIn
, "CREATE ", 7)==0 ){
891 for(i
=0; i
<(int)(sizeof(aPrefix
)/sizeof(aPrefix
[0])); i
++){
892 int n
= strlen30(aPrefix
[i
]);
893 if( strncmp(zIn
+7, aPrefix
[i
], n
)==0 && zIn
[n
+7]==' ' ){
897 char cQuote
= quoteChar(zSchema
);
898 if( cQuote
&& sqlite3_stricmp(zSchema
,"temp")!=0 ){
899 z
= sqlite3_mprintf("%.*s \"%w\".%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
901 z
= sqlite3_mprintf("%.*s %s.%s", n
+7, zIn
, zSchema
, zIn
+n
+8);
905 && aPrefix
[i
][0]=='V'
906 && (zFake
= shellFakeSchema(db
, zSchema
, zName
))!=0
909 z
= sqlite3_mprintf("%s\n/* %s */", zIn
, zFake
);
911 z
= sqlite3_mprintf("%z\n/* %s */", z
, zFake
);
916 sqlite3_result_text(pCtx
, z
, -1, sqlite3_free
);
922 sqlite3_result_value(pCtx
, apVal
[0]);
926 ** The source code for several run-time loadable extensions is inserted
927 ** below by the ../tool/mkshellc.tcl script. Before processing that included
928 ** code, we need to override some macros to make the included program code
929 ** work here in the middle of this regular program.
931 #define SQLITE_EXTENSION_INIT1
932 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
934 #if defined(_WIN32) && defined(_MSC_VER)
935 INCLUDE test_windirent
.h
936 INCLUDE test_windirent
.c
937 #define dirent DIRENT
939 INCLUDE
../ext
/misc
/shathree
.c
940 INCLUDE
../ext
/misc
/fileio
.c
941 INCLUDE
../ext
/misc
/completion
.c
942 INCLUDE
../ext
/misc
/appendvfs
.c
943 #ifdef SQLITE_HAVE_ZLIB
944 INCLUDE
../ext
/misc
/zipfile
.c
945 INCLUDE
../ext
/misc
/sqlar
.c
947 INCLUDE
../ext
/expert
/sqlite3expert
.h
948 INCLUDE
../ext
/expert
/sqlite3expert
.c
950 #if defined(SQLITE_ENABLE_SESSION)
952 ** State information for a single open session
954 typedef struct OpenSession OpenSession
;
956 char *zName
; /* Symbolic name for this session */
957 int nFilter
; /* Number of xFilter rejection GLOB patterns */
958 char **azFilter
; /* Array of xFilter rejection GLOB patterns */
959 sqlite3_session
*p
; /* The open session */
964 ** Shell output mode information from before ".explain on",
965 ** saved so that it can be restored by ".explain off"
967 typedef struct SavedModeInfo SavedModeInfo
;
968 struct SavedModeInfo
{
969 int valid
; /* Is there legit data in here? */
970 int mode
; /* Mode prior to ".explain on" */
971 int showHeader
; /* The ".header" setting prior to ".explain on" */
972 int colWidth
[100]; /* Column widths prior to ".explain on" */
975 typedef struct ExpertInfo ExpertInfo
;
977 sqlite3expert
*pExpert
;
981 /* A single line in the EQP output */
982 typedef struct EQPGraphRow EQPGraphRow
;
984 int iEqpId
; /* ID for this row */
985 int iParentId
; /* ID of the parent row */
986 EQPGraphRow
*pNext
; /* Next row in sequence */
987 char zText
[1]; /* Text to display for this row */
990 /* All EQP output is collected into an instance of the following */
991 typedef struct EQPGraph EQPGraph
;
993 EQPGraphRow
*pRow
; /* Linked list of all rows of the EQP output */
994 EQPGraphRow
*pLast
; /* Last element of the pRow list */
995 char zPrefix
[100]; /* Graph prefix */
999 ** State information about the database connection is contained in an
1000 ** instance of the following structure.
1002 typedef struct ShellState ShellState
;
1004 sqlite3
*db
; /* The database */
1005 u8 autoExplain
; /* Automatically turn on .explain mode */
1006 u8 autoEQP
; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
1007 u8 autoEQPtest
; /* autoEQP is in test mode */
1008 u8 statsOn
; /* True to display memory stats before each finalize */
1009 u8 scanstatsOn
; /* True to display scan stats before each finalize */
1010 u8 openMode
; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
1011 u8 doXdgOpen
; /* Invoke start/open/xdg-open in output_reset() */
1012 u8 nEqpLevel
; /* Depth of the EQP output graph */
1013 unsigned mEqpLines
; /* Mask of veritical lines in the EQP output graph */
1014 int outCount
; /* Revert to stdout when reaching zero */
1015 int cnt
; /* Number of records displayed so far */
1016 FILE *out
; /* Write results here */
1017 FILE *traceOut
; /* Output for sqlite3_trace() */
1018 int nErr
; /* Number of errors seen */
1019 int mode
; /* An output mode setting */
1020 int modePrior
; /* Saved mode */
1021 int cMode
; /* temporary output mode for the current query */
1022 int normalMode
; /* Output mode before ".explain on" */
1023 int writableSchema
; /* True if PRAGMA writable_schema=ON */
1024 int showHeader
; /* True to show column names in List or Column mode */
1025 int nCheck
; /* Number of ".check" commands run */
1026 unsigned shellFlgs
; /* Various flags */
1027 char *zDestTable
; /* Name of destination table when MODE_Insert */
1028 char *zTempFile
; /* Temporary file that might need deleting */
1029 char zTestcase
[30]; /* Name of current test case */
1030 char colSeparator
[20]; /* Column separator character for several modes */
1031 char rowSeparator
[20]; /* Row separator character for MODE_Ascii */
1032 char colSepPrior
[20]; /* Saved column separator */
1033 char rowSepPrior
[20]; /* Saved row separator */
1034 int colWidth
[100]; /* Requested width of each column when in column mode*/
1035 int actualWidth
[100]; /* Actual width of each column */
1036 char nullValue
[20]; /* The text to print when a NULL comes back from
1038 char outfile
[FILENAME_MAX
]; /* Filename for *out */
1039 const char *zDbFilename
; /* name of the database file */
1040 char *zFreeOnClose
; /* Filename to free when closing */
1041 const char *zVfs
; /* Name of VFS to use */
1042 sqlite3_stmt
*pStmt
; /* Current statement if any. */
1043 FILE *pLog
; /* Write log output here */
1044 int *aiIndent
; /* Array of indents used in MODE_Explain */
1045 int nIndent
; /* Size of array aiIndent[] */
1046 int iIndent
; /* Index of current op in aiIndent[] */
1047 EQPGraph sGraph
; /* Information for the graphical EXPLAIN QUERY PLAN */
1048 #if defined(SQLITE_ENABLE_SESSION)
1049 int nSession
; /* Number of active sessions */
1050 OpenSession aSession
[4]; /* Array of sessions. [0] is in focus. */
1052 ExpertInfo expert
; /* Valid if previous command was ".expert OPT..." */
1056 /* Allowed values for ShellState.autoEQP
1058 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
1059 #define AUTOEQP_on 1 /* Automatic EQP is on */
1060 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
1061 #define AUTOEQP_full 3 /* Show full EXPLAIN */
1063 /* Allowed values for ShellState.openMode
1065 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
1066 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
1067 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
1068 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
1069 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
1072 ** These are the allowed shellFlgs values
1074 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
1075 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
1076 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
1077 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
1078 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
1079 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
1080 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
1083 ** Macros for testing and setting shellFlgs
1085 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
1086 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
1087 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
1090 ** These are the allowed modes.
1092 #define MODE_Line 0 /* One column per line. Blank line between records */
1093 #define MODE_Column 1 /* One record per line in neat columns */
1094 #define MODE_List 2 /* One record per line with a separator */
1095 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
1096 #define MODE_Html 4 /* Generate an XHTML table */
1097 #define MODE_Insert 5 /* Generate SQL "insert" statements */
1098 #define MODE_Quote 6 /* Quote values as for SQL */
1099 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
1100 #define MODE_Csv 8 /* Quote strings, numbers are plain */
1101 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
1102 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
1103 #define MODE_Pretty 11 /* Pretty-print schemas */
1104 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
1106 static const char *modeDescr
[] = {
1123 ** These are the column/row/line separators used by the various
1124 ** import/export modes.
1126 #define SEP_Column "|"
1127 #define SEP_Row "\n"
1128 #define SEP_Tab "\t"
1129 #define SEP_Space " "
1130 #define SEP_Comma ","
1131 #define SEP_CrLf "\r\n"
1132 #define SEP_Unit "\x1F"
1133 #define SEP_Record "\x1E"
1136 ** A callback for the sqlite3_log() interface.
1138 static void shellLog(void *pArg
, int iErrCode
, const char *zMsg
){
1139 ShellState
*p
= (ShellState
*)pArg
;
1140 if( p
->pLog
==0 ) return;
1141 utf8_printf(p
->pLog
, "(%d) %s\n", iErrCode
, zMsg
);
1146 ** SQL function: shell_putsnl(X)
1148 ** Write the text X to the screen (or whatever output is being directed)
1149 ** adding a newline at the end, and then return X.
1151 static void shellPutsFunc(
1152 sqlite3_context
*pCtx
,
1154 sqlite3_value
**apVal
1156 ShellState
*p
= (ShellState
*)sqlite3_user_data(pCtx
);
1158 utf8_printf(p
->out
, "%s\n", sqlite3_value_text(apVal
[0]));
1159 sqlite3_result_value(pCtx
, apVal
[0]);
1163 ** SQL function: edit(VALUE)
1164 ** edit(VALUE,EDITOR)
1168 ** (1) Write VALUE into a temporary file.
1169 ** (2) Run program EDITOR on that temporary file.
1170 ** (3) Read the temporary file back and return its content as the result.
1171 ** (4) Delete the temporary file
1173 ** If the EDITOR argument is omitted, use the value in the VISUAL
1174 ** environment variable. If still there is no EDITOR, through an error.
1176 ** Also throw an error if the EDITOR program returns a non-zero exit code.
1178 #ifndef SQLITE_NOHAVE_SYSTEM
1179 static void editFunc(
1180 sqlite3_context
*context
,
1182 sqlite3_value
**argv
1184 const char *zEditor
;
1185 char *zTempFile
= 0;
1193 unsigned char *p
= 0;
1196 zEditor
= (const char*)sqlite3_value_text(argv
[1]);
1198 zEditor
= getenv("VISUAL");
1201 sqlite3_result_error(context
, "no editor for edit()", -1);
1204 if( sqlite3_value_type(argv
[0])==SQLITE_NULL
){
1205 sqlite3_result_error(context
, "NULL input to edit()", -1);
1208 db
= sqlite3_context_db_handle(context
);
1210 sqlite3_file_control(db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &zTempFile
);
1212 sqlite3_uint64 r
= 0;
1213 sqlite3_randomness(sizeof(r
), &r
);
1214 zTempFile
= sqlite3_mprintf("temp%llx", r
);
1216 sqlite3_result_error_nomem(context
);
1220 bBin
= sqlite3_value_type(argv
[0])==SQLITE_BLOB
;
1221 f
= fopen(zTempFile
, bBin
? "wb" : "w");
1223 sqlite3_result_error(context
, "edit() cannot open temp file", -1);
1226 sz
= sqlite3_value_bytes(argv
[0]);
1228 x
= fwrite(sqlite3_value_blob(argv
[0]), 1, sz
, f
);
1230 x
= fwrite(sqlite3_value_text(argv
[0]), 1, sz
, f
);
1235 sqlite3_result_error(context
, "edit() could not write the whole file", -1);
1238 zCmd
= sqlite3_mprintf("%s \"%s\"", zEditor
, zTempFile
);
1240 sqlite3_result_error_nomem(context
);
1246 sqlite3_result_error(context
, "EDITOR returned non-zero", -1);
1249 f
= fopen(zTempFile
, bBin
? "rb" : "r");
1251 sqlite3_result_error(context
,
1252 "edit() cannot reopen temp file after edit", -1);
1255 fseek(f
, 0, SEEK_END
);
1258 p
= sqlite3_malloc64( sz
+(bBin
==0) );
1260 sqlite3_result_error_nomem(context
);
1264 x
= fread(p
, 1, sz
, f
);
1266 x
= fread(p
, 1, sz
, f
);
1272 sqlite3_result_error(context
, "could not read back the whole file", -1);
1276 sqlite3_result_blob64(context
, p
, sz
, sqlite3_free
);
1278 sqlite3_result_text64(context
, (const char*)p
, sz
,
1279 sqlite3_free
, SQLITE_UTF8
);
1286 sqlite3_free(zTempFile
);
1289 #endif /* SQLITE_NOHAVE_SYSTEM */
1292 ** Save or restore the current output mode
1294 static void outputModePush(ShellState
*p
){
1295 p
->modePrior
= p
->mode
;
1296 memcpy(p
->colSepPrior
, p
->colSeparator
, sizeof(p
->colSeparator
));
1297 memcpy(p
->rowSepPrior
, p
->rowSeparator
, sizeof(p
->rowSeparator
));
1299 static void outputModePop(ShellState
*p
){
1300 p
->mode
= p
->modePrior
;
1301 memcpy(p
->colSeparator
, p
->colSepPrior
, sizeof(p
->colSeparator
));
1302 memcpy(p
->rowSeparator
, p
->rowSepPrior
, sizeof(p
->rowSeparator
));
1306 ** Output the given string as a hex-encoded blob (eg. X'1234' )
1308 static void output_hex_blob(FILE *out
, const void *pBlob
, int nBlob
){
1310 char *zBlob
= (char *)pBlob
;
1311 raw_printf(out
,"X'");
1312 for(i
=0; i
<nBlob
; i
++){ raw_printf(out
,"%02x",zBlob
[i
]&0xff); }
1313 raw_printf(out
,"'");
1317 ** Find a string that is not found anywhere in z[]. Return a pointer
1320 ** Try to use zA and zB first. If both of those are already found in z[]
1321 ** then make up some string and store it in the buffer zBuf.
1323 static const char *unused_string(
1324 const char *z
, /* Result must not appear anywhere in z */
1325 const char *zA
, const char *zB
, /* Try these first */
1326 char *zBuf
/* Space to store a generated string */
1329 if( strstr(z
, zA
)==0 ) return zA
;
1330 if( strstr(z
, zB
)==0 ) return zB
;
1332 sqlite3_snprintf(20,zBuf
,"(%s%u)", zA
, i
++);
1333 }while( strstr(z
,zBuf
)!=0 );
1338 ** Output the given string as a quoted string using SQL quoting conventions.
1340 ** See also: output_quoted_escaped_string()
1342 static void output_quoted_string(FILE *out
, const char *z
){
1345 setBinaryMode(out
, 1);
1346 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1348 utf8_printf(out
,"'%s'",z
);
1350 raw_printf(out
, "'");
1352 for(i
=0; (c
= z
[i
])!=0 && c
!='\''; i
++){}
1355 utf8_printf(out
, "%.*s", i
, z
);
1359 raw_printf(out
, "'");
1367 raw_printf(out
, "'");
1369 setTextMode(out
, 1);
1373 ** Output the given string as a quoted string using SQL quoting conventions.
1374 ** Additionallly , escape the "\n" and "\r" characters so that they do not
1375 ** get corrupted by end-of-line translation facilities in some operating
1378 ** This is like output_quoted_string() but with the addition of the \r\n
1379 ** escape mechanism.
1381 static void output_quoted_escaped_string(FILE *out
, const char *z
){
1384 setBinaryMode(out
, 1);
1385 for(i
=0; (c
= z
[i
])!=0 && c
!='\'' && c
!='\n' && c
!='\r'; i
++){}
1387 utf8_printf(out
,"'%s'",z
);
1389 const char *zNL
= 0;
1390 const char *zCR
= 0;
1393 char zBuf1
[20], zBuf2
[20];
1394 for(i
=0; z
[i
]; i
++){
1395 if( z
[i
]=='\n' ) nNL
++;
1396 if( z
[i
]=='\r' ) nCR
++;
1399 raw_printf(out
, "replace(");
1400 zNL
= unused_string(z
, "\\n", "\\012", zBuf1
);
1403 raw_printf(out
, "replace(");
1404 zCR
= unused_string(z
, "\\r", "\\015", zBuf2
);
1406 raw_printf(out
, "'");
1408 for(i
=0; (c
= z
[i
])!=0 && c
!='\n' && c
!='\r' && c
!='\''; i
++){}
1411 utf8_printf(out
, "%.*s", i
, z
);
1415 raw_printf(out
, "'");
1423 raw_printf(out
, "%s", zNL
);
1426 raw_printf(out
, "%s", zCR
);
1428 raw_printf(out
, "'");
1430 raw_printf(out
, ",'%s',char(13))", zCR
);
1433 raw_printf(out
, ",'%s',char(10))", zNL
);
1436 setTextMode(out
, 1);
1440 ** Output the given string as a quoted according to C or TCL quoting rules.
1442 static void output_c_string(FILE *out
, const char *z
){
1445 while( (c
= *(z
++))!=0 ){
1452 }else if( c
=='\t' ){
1455 }else if( c
=='\n' ){
1458 }else if( c
=='\r' ){
1461 }else if( !isprint(c
&0xff) ){
1462 raw_printf(out
, "\\%03o", c
&0xff);
1471 ** Output the given string with characters that are special to
1474 static void output_html_string(FILE *out
, const char *z
){
1486 utf8_printf(out
,"%.*s",i
,z
);
1489 raw_printf(out
,"<");
1490 }else if( z
[i
]=='&' ){
1491 raw_printf(out
,"&");
1492 }else if( z
[i
]=='>' ){
1493 raw_printf(out
,">");
1494 }else if( z
[i
]=='\"' ){
1495 raw_printf(out
,""");
1496 }else if( z
[i
]=='\'' ){
1497 raw_printf(out
,"'");
1506 ** If a field contains any character identified by a 1 in the following
1507 ** array, then the string must be quoted for CSV.
1509 static const char needCsvQuote
[] = {
1510 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1511 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1512 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1513 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1514 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1515 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1516 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1517 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
1518 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1519 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1520 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1521 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1522 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1523 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1524 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1525 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1529 ** Output a single term of CSV. Actually, p->colSeparator is used for
1530 ** the separator, which may or may not be a comma. p->nullValue is
1531 ** the null value. Strings are quoted if necessary. The separator
1532 ** is only issued if bSep is true.
1534 static void output_csv(ShellState
*p
, const char *z
, int bSep
){
1537 utf8_printf(out
,"%s",p
->nullValue
);
1540 int nSep
= strlen30(p
->colSeparator
);
1541 for(i
=0; z
[i
]; i
++){
1542 if( needCsvQuote
[((unsigned char*)z
)[i
]]
1543 || (z
[i
]==p
->colSeparator
[0] &&
1544 (nSep
==1 || memcmp(z
, p
->colSeparator
, nSep
)==0)) ){
1550 char *zQuoted
= sqlite3_mprintf("\"%w\"", z
);
1551 utf8_printf(out
, "%s", zQuoted
);
1552 sqlite3_free(zQuoted
);
1554 utf8_printf(out
, "%s", z
);
1558 utf8_printf(p
->out
, "%s", p
->colSeparator
);
1563 ** This routine runs when the user presses Ctrl-C
1565 static void interrupt_handler(int NotUsed
){
1566 UNUSED_PARAMETER(NotUsed
);
1568 if( seenInterrupt
>2 ) exit(1);
1569 if( globalDb
) sqlite3_interrupt(globalDb
);
1572 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
1574 ** This routine runs for console events (e.g. Ctrl-C) on Win32
1576 static BOOL WINAPI
ConsoleCtrlHandler(
1577 DWORD dwCtrlType
/* One of the CTRL_*_EVENT constants */
1579 if( dwCtrlType
==CTRL_C_EVENT
){
1580 interrupt_handler(0);
1587 #ifndef SQLITE_OMIT_AUTHORIZATION
1589 ** When the ".auth ON" is set, the following authorizer callback is
1590 ** invoked. It always returns SQLITE_OK.
1592 static int shellAuth(
1600 ShellState
*p
= (ShellState
*)pClientData
;
1601 static const char *azAction
[] = { 0,
1602 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
1603 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
1604 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
1605 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
1606 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
1607 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
1608 "PRAGMA", "READ", "SELECT",
1609 "TRANSACTION", "UPDATE", "ATTACH",
1610 "DETACH", "ALTER_TABLE", "REINDEX",
1611 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
1612 "FUNCTION", "SAVEPOINT", "RECURSIVE"
1620 utf8_printf(p
->out
, "authorizer: %s", azAction
[op
]);
1622 raw_printf(p
->out
, " ");
1624 output_c_string(p
->out
, az
[i
]);
1626 raw_printf(p
->out
, "NULL");
1629 raw_printf(p
->out
, "\n");
1635 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
1637 ** This routine converts some CREATE TABLE statements for shadow tables
1638 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
1640 static void printSchemaLine(FILE *out
, const char *z
, const char *zTail
){
1641 if( sqlite3_strglob("CREATE TABLE ['\"]*", z
)==0 ){
1642 utf8_printf(out
, "CREATE TABLE IF NOT EXISTS %s%s", z
+13, zTail
);
1644 utf8_printf(out
, "%s%s", z
, zTail
);
1647 static void printSchemaLineN(FILE *out
, char *z
, int n
, const char *zTail
){
1650 printSchemaLine(out
, z
, zTail
);
1655 ** Return true if string z[] has nothing but whitespace and comments to the
1656 ** end of the first line.
1658 static int wsToEol(const char *z
){
1660 for(i
=0; z
[i
]; i
++){
1661 if( z
[i
]=='\n' ) return 1;
1662 if( IsSpace(z
[i
]) ) continue;
1663 if( z
[i
]=='-' && z
[i
+1]=='-' ) return 1;
1670 ** Add a new entry to the EXPLAIN QUERY PLAN data
1672 static void eqp_append(ShellState
*p
, int iEqpId
, int p2
, const char *zText
){
1674 int nText
= strlen30(zText
);
1675 if( p
->autoEQPtest
){
1676 utf8_printf(p
->out
, "%d,%d,%s\n", iEqpId
, p2
, zText
);
1678 pNew
= sqlite3_malloc64( sizeof(*pNew
) + nText
);
1679 if( pNew
==0 ) shell_out_of_memory();
1680 pNew
->iEqpId
= iEqpId
;
1681 pNew
->iParentId
= p2
;
1682 memcpy(pNew
->zText
, zText
, nText
+1);
1684 if( p
->sGraph
.pLast
){
1685 p
->sGraph
.pLast
->pNext
= pNew
;
1687 p
->sGraph
.pRow
= pNew
;
1689 p
->sGraph
.pLast
= pNew
;
1693 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
1696 static void eqp_reset(ShellState
*p
){
1697 EQPGraphRow
*pRow
, *pNext
;
1698 for(pRow
= p
->sGraph
.pRow
; pRow
; pRow
= pNext
){
1699 pNext
= pRow
->pNext
;
1702 memset(&p
->sGraph
, 0, sizeof(p
->sGraph
));
1705 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
1706 ** pOld, or return the first such line if pOld is NULL
1708 static EQPGraphRow
*eqp_next_row(ShellState
*p
, int iEqpId
, EQPGraphRow
*pOld
){
1709 EQPGraphRow
*pRow
= pOld
? pOld
->pNext
: p
->sGraph
.pRow
;
1710 while( pRow
&& pRow
->iParentId
!=iEqpId
) pRow
= pRow
->pNext
;
1714 /* Render a single level of the graph that has iEqpId as its parent. Called
1715 ** recursively to render sublevels.
1717 static void eqp_render_level(ShellState
*p
, int iEqpId
){
1718 EQPGraphRow
*pRow
, *pNext
;
1719 int n
= strlen30(p
->sGraph
.zPrefix
);
1721 for(pRow
= eqp_next_row(p
, iEqpId
, 0); pRow
; pRow
= pNext
){
1722 pNext
= eqp_next_row(p
, iEqpId
, pRow
);
1724 utf8_printf(p
->out
, "%s%s%s\n", p
->sGraph
.zPrefix
, pNext
? "|--" : "`--", z
);
1725 if( n
<(int)sizeof(p
->sGraph
.zPrefix
)-7 ){
1726 memcpy(&p
->sGraph
.zPrefix
[n
], pNext
? "| " : " ", 4);
1727 eqp_render_level(p
, pRow
->iEqpId
);
1728 p
->sGraph
.zPrefix
[n
] = 0;
1734 ** Display and reset the EXPLAIN QUERY PLAN data
1736 static void eqp_render(ShellState
*p
){
1737 EQPGraphRow
*pRow
= p
->sGraph
.pRow
;
1739 if( pRow
->zText
[0]=='-' ){
1740 if( pRow
->pNext
==0 ){
1744 utf8_printf(p
->out
, "%s\n", pRow
->zText
+3);
1745 p
->sGraph
.pRow
= pRow
->pNext
;
1748 utf8_printf(p
->out
, "QUERY PLAN\n");
1750 p
->sGraph
.zPrefix
[0] = 0;
1751 eqp_render_level(p
, 0);
1757 ** This is the callback routine that the shell
1758 ** invokes for each row of a query result.
1760 static int shell_callback(
1762 int nArg
, /* Number of result columns */
1763 char **azArg
, /* Text of each result column */
1764 char **azCol
, /* Column names */
1765 int *aiType
/* Column types */
1768 ShellState
*p
= (ShellState
*)pArg
;
1770 if( azArg
==0 ) return 0;
1774 if( azArg
==0 ) break;
1775 for(i
=0; i
<nArg
; i
++){
1776 int len
= strlen30(azCol
[i
] ? azCol
[i
] : "");
1777 if( len
>w
) w
= len
;
1779 if( p
->cnt
++>0 ) utf8_printf(p
->out
, "%s", p
->rowSeparator
);
1780 for(i
=0; i
<nArg
; i
++){
1781 utf8_printf(p
->out
,"%*s = %s%s", w
, azCol
[i
],
1782 azArg
[i
] ? azArg
[i
] : p
->nullValue
, p
->rowSeparator
);
1788 static const int aExplainWidths
[] = {4, 13, 4, 4, 4, 13, 2, 13};
1789 const int *colWidth
;
1792 if( p
->cMode
==MODE_Column
){
1793 colWidth
= p
->colWidth
;
1794 showHdr
= p
->showHeader
;
1795 rowSep
= p
->rowSeparator
;
1797 colWidth
= aExplainWidths
;
1802 for(i
=0; i
<nArg
; i
++){
1804 if( i
<ArraySize(p
->colWidth
) ){
1810 w
= strlenChar(azCol
[i
] ? azCol
[i
] : "");
1812 n
= strlenChar(azArg
&& azArg
[i
] ? azArg
[i
] : p
->nullValue
);
1815 if( i
<ArraySize(p
->actualWidth
) ){
1816 p
->actualWidth
[i
] = w
;
1819 utf8_width_print(p
->out
, w
, azCol
[i
]);
1820 utf8_printf(p
->out
, "%s", i
==nArg
-1 ? rowSep
: " ");
1824 for(i
=0; i
<nArg
; i
++){
1826 if( i
<ArraySize(p
->actualWidth
) ){
1827 w
= p
->actualWidth
[i
];
1832 utf8_printf(p
->out
,"%-*.*s%s",w
,w
,
1833 "----------------------------------------------------------"
1834 "----------------------------------------------------------",
1835 i
==nArg
-1 ? rowSep
: " ");
1839 if( azArg
==0 ) break;
1840 for(i
=0; i
<nArg
; i
++){
1842 if( i
<ArraySize(p
->actualWidth
) ){
1843 w
= p
->actualWidth
[i
];
1847 if( p
->cMode
==MODE_Explain
&& azArg
[i
] && strlenChar(azArg
[i
])>w
){
1848 w
= strlenChar(azArg
[i
]);
1850 if( i
==1 && p
->aiIndent
&& p
->pStmt
){
1851 if( p
->iIndent
<p
->nIndent
){
1852 utf8_printf(p
->out
, "%*.s", p
->aiIndent
[p
->iIndent
], "");
1856 utf8_width_print(p
->out
, w
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
1857 utf8_printf(p
->out
, "%s", i
==nArg
-1 ? rowSep
: " ");
1861 case MODE_Semi
: { /* .schema and .fullschema output */
1862 printSchemaLine(p
->out
, azArg
[0], ";\n");
1865 case MODE_Pretty
: { /* .schema and .fullschema with --indent */
1873 if( azArg
[0]==0 ) break;
1874 if( sqlite3_strlike("CREATE VIEW%", azArg
[0], 0)==0
1875 || sqlite3_strlike("CREATE TRIG%", azArg
[0], 0)==0
1877 utf8_printf(p
->out
, "%s;\n", azArg
[0]);
1880 z
= sqlite3_mprintf("%s", azArg
[0]);
1882 for(i
=0; IsSpace(z
[i
]); i
++){}
1883 for(; (c
= z
[i
])!=0; i
++){
1885 if( z
[j
-1]=='\r' ) z
[j
-1] = '\n';
1886 if( IsSpace(z
[j
-1]) || z
[j
-1]=='(' ) continue;
1887 }else if( (c
=='(' || c
==')') && j
>0 && IsSpace(z
[j
-1]) ){
1892 while( j
>0 && IsSpace(z
[j
-1]) ){ j
--; }
1894 if( strlen30(z
)>=79 ){
1895 for(i
=j
=0; (c
= z
[i
])!=0; i
++){ /* Copy changes from z[i] back to z[j] */
1898 }else if( c
=='"' || c
=='\'' || c
=='`' ){
1902 }else if( c
=='-' && z
[i
+1]=='-' ){
1908 if( nLine
>0 && nParen
==0 && j
>0 ){
1909 printSchemaLineN(p
->out
, z
, j
, "\n");
1914 if( nParen
==1 && cEnd
==0
1915 && (c
=='(' || c
=='\n' || (c
==',' && !wsToEol(z
+i
+1)))
1918 printSchemaLineN(p
->out
, z
, j
, "\n ");
1921 while( IsSpace(z
[i
+1]) ){ i
++; }
1926 printSchemaLine(p
->out
, z
, ";\n");
1931 if( p
->cnt
++==0 && p
->showHeader
){
1932 for(i
=0; i
<nArg
; i
++){
1933 utf8_printf(p
->out
,"%s%s",azCol
[i
],
1934 i
==nArg
-1 ? p
->rowSeparator
: p
->colSeparator
);
1937 if( azArg
==0 ) break;
1938 for(i
=0; i
<nArg
; i
++){
1940 if( z
==0 ) z
= p
->nullValue
;
1941 utf8_printf(p
->out
, "%s", z
);
1943 utf8_printf(p
->out
, "%s", p
->colSeparator
);
1945 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
1951 if( p
->cnt
++==0 && p
->showHeader
){
1952 raw_printf(p
->out
,"<TR>");
1953 for(i
=0; i
<nArg
; i
++){
1954 raw_printf(p
->out
,"<TH>");
1955 output_html_string(p
->out
, azCol
[i
]);
1956 raw_printf(p
->out
,"</TH>\n");
1958 raw_printf(p
->out
,"</TR>\n");
1960 if( azArg
==0 ) break;
1961 raw_printf(p
->out
,"<TR>");
1962 for(i
=0; i
<nArg
; i
++){
1963 raw_printf(p
->out
,"<TD>");
1964 output_html_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
1965 raw_printf(p
->out
,"</TD>\n");
1967 raw_printf(p
->out
,"</TR>\n");
1971 if( p
->cnt
++==0 && p
->showHeader
){
1972 for(i
=0; i
<nArg
; i
++){
1973 output_c_string(p
->out
,azCol
[i
] ? azCol
[i
] : "");
1974 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
1976 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
1978 if( azArg
==0 ) break;
1979 for(i
=0; i
<nArg
; i
++){
1980 output_c_string(p
->out
, azArg
[i
] ? azArg
[i
] : p
->nullValue
);
1981 if(i
<nArg
-1) utf8_printf(p
->out
, "%s", p
->colSeparator
);
1983 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
1987 setBinaryMode(p
->out
, 1);
1988 if( p
->cnt
++==0 && p
->showHeader
){
1989 for(i
=0; i
<nArg
; i
++){
1990 output_csv(p
, azCol
[i
] ? azCol
[i
] : "", i
<nArg
-1);
1992 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
1995 for(i
=0; i
<nArg
; i
++){
1996 output_csv(p
, azArg
[i
], i
<nArg
-1);
1998 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2000 setTextMode(p
->out
, 1);
2004 if( azArg
==0 ) break;
2005 utf8_printf(p
->out
,"INSERT INTO %s",p
->zDestTable
);
2006 if( p
->showHeader
){
2007 raw_printf(p
->out
,"(");
2008 for(i
=0; i
<nArg
; i
++){
2009 if( i
>0 ) raw_printf(p
->out
, ",");
2010 if( quoteChar(azCol
[i
]) ){
2011 char *z
= sqlite3_mprintf("\"%w\"", azCol
[i
]);
2012 utf8_printf(p
->out
, "%s", z
);
2015 raw_printf(p
->out
, "%s", azCol
[i
]);
2018 raw_printf(p
->out
,")");
2021 for(i
=0; i
<nArg
; i
++){
2022 raw_printf(p
->out
, i
>0 ? "," : " VALUES(");
2023 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2024 utf8_printf(p
->out
,"NULL");
2025 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2026 if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2027 output_quoted_string(p
->out
, azArg
[i
]);
2029 output_quoted_escaped_string(p
->out
, azArg
[i
]);
2031 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2032 utf8_printf(p
->out
,"%s", azArg
[i
]);
2033 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2035 double r
= sqlite3_column_double(p
->pStmt
, i
);
2036 sqlite3_snprintf(50,z
,"%!.20g", r
);
2037 raw_printf(p
->out
, "%s", z
);
2038 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2039 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2040 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2041 output_hex_blob(p
->out
, pBlob
, nBlob
);
2042 }else if( isNumber(azArg
[i
], 0) ){
2043 utf8_printf(p
->out
,"%s", azArg
[i
]);
2044 }else if( ShellHasFlag(p
, SHFLG_Newlines
) ){
2045 output_quoted_string(p
->out
, azArg
[i
]);
2047 output_quoted_escaped_string(p
->out
, azArg
[i
]);
2050 raw_printf(p
->out
,");\n");
2054 if( azArg
==0 ) break;
2055 if( p
->cnt
==0 && p
->showHeader
){
2056 for(i
=0; i
<nArg
; i
++){
2057 if( i
>0 ) raw_printf(p
->out
, ",");
2058 output_quoted_string(p
->out
, azCol
[i
]);
2060 raw_printf(p
->out
,"\n");
2063 for(i
=0; i
<nArg
; i
++){
2064 if( i
>0 ) raw_printf(p
->out
, ",");
2065 if( (azArg
[i
]==0) || (aiType
&& aiType
[i
]==SQLITE_NULL
) ){
2066 utf8_printf(p
->out
,"NULL");
2067 }else if( aiType
&& aiType
[i
]==SQLITE_TEXT
){
2068 output_quoted_string(p
->out
, azArg
[i
]);
2069 }else if( aiType
&& aiType
[i
]==SQLITE_INTEGER
){
2070 utf8_printf(p
->out
,"%s", azArg
[i
]);
2071 }else if( aiType
&& aiType
[i
]==SQLITE_FLOAT
){
2073 double r
= sqlite3_column_double(p
->pStmt
, i
);
2074 sqlite3_snprintf(50,z
,"%!.20g", r
);
2075 raw_printf(p
->out
, "%s", z
);
2076 }else if( aiType
&& aiType
[i
]==SQLITE_BLOB
&& p
->pStmt
){
2077 const void *pBlob
= sqlite3_column_blob(p
->pStmt
, i
);
2078 int nBlob
= sqlite3_column_bytes(p
->pStmt
, i
);
2079 output_hex_blob(p
->out
, pBlob
, nBlob
);
2080 }else if( isNumber(azArg
[i
], 0) ){
2081 utf8_printf(p
->out
,"%s", azArg
[i
]);
2083 output_quoted_string(p
->out
, azArg
[i
]);
2086 raw_printf(p
->out
,"\n");
2090 if( p
->cnt
++==0 && p
->showHeader
){
2091 for(i
=0; i
<nArg
; i
++){
2092 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2093 utf8_printf(p
->out
,"%s",azCol
[i
] ? azCol
[i
] : "");
2095 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2097 if( azArg
==0 ) break;
2098 for(i
=0; i
<nArg
; i
++){
2099 if( i
>0 ) utf8_printf(p
->out
, "%s", p
->colSeparator
);
2100 utf8_printf(p
->out
,"%s",azArg
[i
] ? azArg
[i
] : p
->nullValue
);
2102 utf8_printf(p
->out
, "%s", p
->rowSeparator
);
2106 eqp_append(p
, atoi(azArg
[0]), atoi(azArg
[1]), azArg
[3]);
2114 ** This is the callback routine that the SQLite library
2115 ** invokes for each row of a query result.
2117 static int callback(void *pArg
, int nArg
, char **azArg
, char **azCol
){
2118 /* since we don't have type info, call the shell_callback with a NULL value */
2119 return shell_callback(pArg
, nArg
, azArg
, azCol
, NULL
);
2123 ** This is the callback routine from sqlite3_exec() that appends all
2124 ** output onto the end of a ShellText object.
2126 static int captureOutputCallback(void *pArg
, int nArg
, char **azArg
, char **az
){
2127 ShellText
*p
= (ShellText
*)pArg
;
2129 UNUSED_PARAMETER(az
);
2130 if( azArg
==0 ) return 0;
2131 if( p
->n
) appendText(p
, "|", 0);
2132 for(i
=0; i
<nArg
; i
++){
2133 if( i
) appendText(p
, ",", 0);
2134 if( azArg
[i
] ) appendText(p
, azArg
[i
], 0);
2140 ** Generate an appropriate SELFTEST table in the main database.
2142 static void createSelftestTable(ShellState
*p
){
2145 "SAVEPOINT selftest_init;\n"
2146 "CREATE TABLE IF NOT EXISTS selftest(\n"
2147 " tno INTEGER PRIMARY KEY,\n" /* Test number */
2148 " op TEXT,\n" /* Operator: memo run */
2149 " cmd TEXT,\n" /* Command text */
2150 " ans TEXT\n" /* Desired answer */
2152 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
2153 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
2154 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
2155 " 'memo','Tests generated by --init');\n"
2156 "INSERT INTO [_shell$self]\n"
2158 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
2159 "FROM sqlite_master ORDER BY 2'',224))',\n"
2160 " hex(sha3_query('SELECT type,name,tbl_name,sql "
2161 "FROM sqlite_master ORDER BY 2',224));\n"
2162 "INSERT INTO [_shell$self]\n"
2164 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
2165 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
2166 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
2168 " SELECT name FROM sqlite_master\n"
2169 " WHERE type='table'\n"
2170 " AND name<>'selftest'\n"
2171 " AND coalesce(rootpage,0)>0\n"
2174 "INSERT INTO [_shell$self]\n"
2175 " VALUES('run','PRAGMA integrity_check','ok');\n"
2176 "INSERT INTO selftest(tno,op,cmd,ans)"
2177 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
2178 "DROP TABLE [_shell$self];"
2181 utf8_printf(stderr
, "SELFTEST initialization failure: %s\n", zErrMsg
);
2182 sqlite3_free(zErrMsg
);
2184 sqlite3_exec(p
->db
, "RELEASE selftest_init",0,0,0);
2189 ** Set the destination table field of the ShellState structure to
2190 ** the name of the table given. Escape any quote characters in the
2193 static void set_table_name(ShellState
*p
, const char *zName
){
2198 if( p
->zDestTable
){
2199 free(p
->zDestTable
);
2202 if( zName
==0 ) return;
2203 cQuote
= quoteChar(zName
);
2204 n
= strlen30(zName
);
2205 if( cQuote
) n
+= n
+2;
2206 z
= p
->zDestTable
= malloc( n
+1 );
2207 if( z
==0 ) shell_out_of_memory();
2209 if( cQuote
) z
[n
++] = cQuote
;
2210 for(i
=0; zName
[i
]; i
++){
2212 if( zName
[i
]==cQuote
) z
[n
++] = cQuote
;
2214 if( cQuote
) z
[n
++] = cQuote
;
2220 ** Execute a query statement that will generate SQL output. Print
2221 ** the result columns, comma-separated, on a line and then add a
2222 ** semicolon terminator to the end of that line.
2224 ** If the number of columns is 1 and that column contains text "--"
2225 ** then write the semicolon on a separate line. That way, if a
2226 ** "--" comment occurs at the end of the statement, the comment
2227 ** won't consume the semicolon terminator.
2229 static int run_table_dump_query(
2230 ShellState
*p
, /* Query context */
2231 const char *zSelect
, /* SELECT statement to extract content */
2232 const char *zFirstRow
/* Print before first row, if not NULL */
2234 sqlite3_stmt
*pSelect
;
2239 rc
= sqlite3_prepare_v2(p
->db
, zSelect
, -1, &pSelect
, 0);
2240 if( rc
!=SQLITE_OK
|| !pSelect
){
2241 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n", rc
,
2242 sqlite3_errmsg(p
->db
));
2243 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
2246 rc
= sqlite3_step(pSelect
);
2247 nResult
= sqlite3_column_count(pSelect
);
2248 while( rc
==SQLITE_ROW
){
2250 utf8_printf(p
->out
, "%s", zFirstRow
);
2253 z
= (const char*)sqlite3_column_text(pSelect
, 0);
2254 utf8_printf(p
->out
, "%s", z
);
2255 for(i
=1; i
<nResult
; i
++){
2256 utf8_printf(p
->out
, ",%s", sqlite3_column_text(pSelect
, i
));
2259 while( z
[0] && (z
[0]!='-' || z
[1]!='-') ) z
++;
2261 raw_printf(p
->out
, "\n;\n");
2263 raw_printf(p
->out
, ";\n");
2265 rc
= sqlite3_step(pSelect
);
2267 rc
= sqlite3_finalize(pSelect
);
2268 if( rc
!=SQLITE_OK
){
2269 utf8_printf(p
->out
, "/**** ERROR: (%d) %s *****/\n", rc
,
2270 sqlite3_errmsg(p
->db
));
2271 if( (rc
&0xff)!=SQLITE_CORRUPT
) p
->nErr
++;
2277 ** Allocate space and save off current error string.
2279 static char *save_err_msg(
2280 sqlite3
*db
/* Database to query */
2282 int nErrMsg
= 1+strlen30(sqlite3_errmsg(db
));
2283 char *zErrMsg
= sqlite3_malloc64(nErrMsg
);
2285 memcpy(zErrMsg
, sqlite3_errmsg(db
), nErrMsg
);
2292 ** Attempt to display I/O stats on Linux using /proc/PID/io
2294 static void displayLinuxIoStats(FILE *out
){
2297 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
2298 in
= fopen(z
, "rb");
2300 while( fgets(z
, sizeof(z
), in
)!=0 ){
2301 static const struct {
2302 const char *zPattern
;
2305 { "rchar: ", "Bytes received by read():" },
2306 { "wchar: ", "Bytes sent to write():" },
2307 { "syscr: ", "Read() system calls:" },
2308 { "syscw: ", "Write() system calls:" },
2309 { "read_bytes: ", "Bytes read from storage:" },
2310 { "write_bytes: ", "Bytes written to storage:" },
2311 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
2314 for(i
=0; i
<ArraySize(aTrans
); i
++){
2315 int n
= strlen30(aTrans
[i
].zPattern
);
2316 if( strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
2317 utf8_printf(out
, "%-36s %s", aTrans
[i
].zDesc
, &z
[n
]);
2327 ** Display a single line of status using 64-bit values.
2329 static void displayStatLine(
2330 ShellState
*p
, /* The shell context */
2331 char *zLabel
, /* Label for this one line */
2332 char *zFormat
, /* Format for the result */
2333 int iStatusCtrl
, /* Which status to display */
2334 int bReset
/* True to reset the stats */
2336 sqlite3_int64 iCur
= -1;
2337 sqlite3_int64 iHiwtr
= -1;
2340 sqlite3_status64(iStatusCtrl
, &iCur
, &iHiwtr
, bReset
);
2341 for(i
=0, nPercent
=0; zFormat
[i
]; i
++){
2342 if( zFormat
[i
]=='%' ) nPercent
++;
2345 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iCur
, iHiwtr
);
2347 sqlite3_snprintf(sizeof(zLine
), zLine
, zFormat
, iHiwtr
);
2349 raw_printf(p
->out
, "%-36s %s\n", zLabel
, zLine
);
2353 ** Display memory stats.
2355 static int display_stats(
2356 sqlite3
*db
, /* Database to query */
2357 ShellState
*pArg
, /* Pointer to ShellState */
2358 int bReset
/* True to reset the stats */
2363 if( pArg
==0 || pArg
->out
==0 ) return 0;
2366 if( pArg
->pStmt
&& (pArg
->statsOn
& 2) ){
2368 sqlite3_stmt
*pStmt
= pArg
->pStmt
;
2370 nCol
= sqlite3_column_count(pStmt
);
2371 raw_printf(out
, "%-36s %d\n", "Number of output columns:", nCol
);
2372 for(i
=0; i
<nCol
; i
++){
2373 sqlite3_snprintf(sizeof(z
),z
,"Column %d %nname:", i
, &x
);
2374 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_name(pStmt
,i
));
2375 #ifndef SQLITE_OMIT_DECLTYPE
2376 sqlite3_snprintf(30, z
+x
, "declared type:");
2377 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_decltype(pStmt
, i
));
2379 #ifdef SQLITE_ENABLE_COLUMN_METADATA
2380 sqlite3_snprintf(30, z
+x
, "database name:");
2381 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_database_name(pStmt
,i
));
2382 sqlite3_snprintf(30, z
+x
, "table name:");
2383 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_table_name(pStmt
,i
));
2384 sqlite3_snprintf(30, z
+x
, "origin name:");
2385 utf8_printf(out
, "%-36s %s\n", z
, sqlite3_column_origin_name(pStmt
,i
));
2390 displayStatLine(pArg
, "Memory Used:",
2391 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED
, bReset
);
2392 displayStatLine(pArg
, "Number of Outstanding Allocations:",
2393 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT
, bReset
);
2394 if( pArg
->shellFlgs
& SHFLG_Pagecache
){
2395 displayStatLine(pArg
, "Number of Pcache Pages Used:",
2396 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED
, bReset
);
2398 displayStatLine(pArg
, "Number of Pcache Overflow Bytes:",
2399 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW
, bReset
);
2400 displayStatLine(pArg
, "Largest Allocation:",
2401 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE
, bReset
);
2402 displayStatLine(pArg
, "Largest Pcache Allocation:",
2403 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE
, bReset
);
2404 #ifdef YYTRACKMAXSTACKDEPTH
2405 displayStatLine(pArg
, "Deepest Parser Stack:",
2406 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK
, bReset
);
2410 if( pArg
->shellFlgs
& SHFLG_Lookaside
){
2412 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_USED
,
2413 &iCur
, &iHiwtr
, bReset
);
2414 raw_printf(pArg
->out
,
2415 "Lookaside Slots Used: %d (max %d)\n",
2417 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
,
2418 &iCur
, &iHiwtr
, bReset
);
2419 raw_printf(pArg
->out
, "Successful lookaside attempts: %d\n",
2421 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
,
2422 &iCur
, &iHiwtr
, bReset
);
2423 raw_printf(pArg
->out
, "Lookaside failures due to size: %d\n",
2425 sqlite3_db_status(db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
,
2426 &iCur
, &iHiwtr
, bReset
);
2427 raw_printf(pArg
->out
, "Lookaside failures due to OOM: %d\n",
2431 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHiwtr
, bReset
);
2432 raw_printf(pArg
->out
, "Pager Heap Usage: %d bytes\n",
2435 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHiwtr
, 1);
2436 raw_printf(pArg
->out
, "Page cache hits: %d\n", iCur
);
2438 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHiwtr
, 1);
2439 raw_printf(pArg
->out
, "Page cache misses: %d\n", iCur
);
2441 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHiwtr
, 1);
2442 raw_printf(pArg
->out
, "Page cache writes: %d\n", iCur
);
2444 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_SPILL
, &iCur
, &iHiwtr
, 1);
2445 raw_printf(pArg
->out
, "Page cache spills: %d\n", iCur
);
2447 sqlite3_db_status(db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHiwtr
, bReset
);
2448 raw_printf(pArg
->out
, "Schema Heap Usage: %d bytes\n",
2451 sqlite3_db_status(db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHiwtr
, bReset
);
2452 raw_printf(pArg
->out
, "Statement Heap/Lookaside Usage: %d bytes\n",
2457 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_FULLSCAN_STEP
,
2459 raw_printf(pArg
->out
, "Fullscan Steps: %d\n", iCur
);
2460 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_SORT
, bReset
);
2461 raw_printf(pArg
->out
, "Sort Operations: %d\n", iCur
);
2462 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_AUTOINDEX
,bReset
);
2463 raw_printf(pArg
->out
, "Autoindex Inserts: %d\n", iCur
);
2464 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_VM_STEP
, bReset
);
2465 raw_printf(pArg
->out
, "Virtual Machine Steps: %d\n", iCur
);
2466 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_REPREPARE
, bReset
);
2467 raw_printf(pArg
->out
, "Reprepare operations: %d\n", iCur
);
2468 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_RUN
, bReset
);
2469 raw_printf(pArg
->out
, "Number of times run: %d\n", iCur
);
2470 iCur
= sqlite3_stmt_status(pArg
->pStmt
, SQLITE_STMTSTATUS_MEMUSED
, bReset
);
2471 raw_printf(pArg
->out
, "Memory used by prepared stmt: %d\n", iCur
);
2475 displayLinuxIoStats(pArg
->out
);
2478 /* Do not remove this machine readable comment: extra-stats-output-here */
2484 ** Display scan stats.
2486 static void display_scanstats(
2487 sqlite3
*db
, /* Database to query */
2488 ShellState
*pArg
/* Pointer to ShellState */
2490 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
2491 UNUSED_PARAMETER(db
);
2492 UNUSED_PARAMETER(pArg
);
2495 raw_printf(pArg
->out
, "-------- scanstats --------\n");
2497 for(k
=0; k
<=mx
; k
++){
2498 double rEstLoop
= 1.0;
2500 sqlite3_stmt
*p
= pArg
->pStmt
;
2501 sqlite3_int64 nLoop
, nVisit
;
2504 const char *zExplain
;
2505 if( sqlite3_stmt_scanstatus(p
, i
, SQLITE_SCANSTAT_NLOOP
, (void*)&nLoop
) ){
2508 sqlite3_stmt_scanstatus(p
, i
, SQLITE_SCANSTAT_SELECTID
, (void*)&iSid
);
2509 if( iSid
>mx
) mx
= iSid
;
2510 if( iSid
!=k
) continue;
2512 rEstLoop
= (double)nLoop
;
2513 if( k
>0 ) raw_printf(pArg
->out
, "-------- subquery %d -------\n", k
);
2516 sqlite3_stmt_scanstatus(p
, i
, SQLITE_SCANSTAT_NVISIT
, (void*)&nVisit
);
2517 sqlite3_stmt_scanstatus(p
, i
, SQLITE_SCANSTAT_EST
, (void*)&rEst
);
2518 sqlite3_stmt_scanstatus(p
, i
, SQLITE_SCANSTAT_EXPLAIN
, (void*)&zExplain
);
2519 utf8_printf(pArg
->out
, "Loop %2d: %s\n", n
, zExplain
);
2521 raw_printf(pArg
->out
,
2522 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
2523 nLoop
, nVisit
, (sqlite3_int64
)(rEstLoop
+0.5), rEst
2527 raw_printf(pArg
->out
, "---------------------------\n");
2532 ** Parameter azArray points to a zero-terminated array of strings. zStr
2533 ** points to a single nul-terminated string. Return non-zero if zStr
2534 ** is equal, according to strcmp(), to any of the strings in the array.
2535 ** Otherwise, return zero.
2537 static int str_in_array(const char *zStr
, const char **azArray
){
2539 for(i
=0; azArray
[i
]; i
++){
2540 if( 0==strcmp(zStr
, azArray
[i
]) ) return 1;
2546 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
2547 ** and populate the ShellState.aiIndent[] array with the number of
2548 ** spaces each opcode should be indented before it is output.
2550 ** The indenting rules are:
2552 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
2553 ** all opcodes that occur between the p2 jump destination and the opcode
2554 ** itself by 2 spaces.
2556 ** * For each "Goto", if the jump destination is earlier in the program
2557 ** and ends on one of:
2558 ** Yield SeekGt SeekLt RowSetRead Rewind
2559 ** or if the P1 parameter is one instead of zero,
2560 ** then indent all opcodes between the earlier instruction
2561 ** and "Goto" by 2 spaces.
2563 static void explain_data_prepare(ShellState
*p
, sqlite3_stmt
*pSql
){
2564 const char *zSql
; /* The text of the SQL statement */
2565 const char *z
; /* Used to check if this is an EXPLAIN */
2566 int *abYield
= 0; /* True if op is an OP_Yield */
2567 int nAlloc
= 0; /* Allocated size of p->aiIndent[], abYield */
2568 int iOp
; /* Index of operation in p->aiIndent[] */
2570 const char *azNext
[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
2571 "NextIfOpen", "PrevIfOpen", 0 };
2572 const char *azYield
[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
2574 const char *azGoto
[] = { "Goto", 0 };
2576 /* Try to figure out if this is really an EXPLAIN statement. If this
2577 ** cannot be verified, return early. */
2578 if( sqlite3_column_count(pSql
)!=8 ){
2582 zSql
= sqlite3_sql(pSql
);
2583 if( zSql
==0 ) return;
2584 for(z
=zSql
; *z
==' ' || *z
=='\t' || *z
=='\n' || *z
=='\f' || *z
=='\r'; z
++);
2585 if( sqlite3_strnicmp(z
, "explain", 7) ){
2590 for(iOp
=0; SQLITE_ROW
==sqlite3_step(pSql
); iOp
++){
2592 int iAddr
= sqlite3_column_int(pSql
, 0);
2593 const char *zOp
= (const char*)sqlite3_column_text(pSql
, 1);
2595 /* Set p2 to the P2 field of the current opcode. Then, assuming that
2596 ** p2 is an instruction address, set variable p2op to the index of that
2597 ** instruction in the aiIndent[] array. p2 and p2op may be different if
2598 ** the current instruction is part of a sub-program generated by an
2599 ** SQL trigger or foreign key. */
2600 int p2
= sqlite3_column_int(pSql
, 3);
2601 int p2op
= (p2
+ (iOp
-iAddr
));
2603 /* Grow the p->aiIndent array as required */
2606 /* Do further verfication that this is explain output. Abort if
2608 static const char *explainCols
[] = {
2609 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
2611 for(jj
=0; jj
<ArraySize(explainCols
); jj
++){
2612 if( strcmp(sqlite3_column_name(pSql
,jj
),explainCols
[jj
])!=0 ){
2614 sqlite3_reset(pSql
);
2620 p
->aiIndent
= (int*)sqlite3_realloc64(p
->aiIndent
, nAlloc
*sizeof(int));
2621 abYield
= (int*)sqlite3_realloc64(abYield
, nAlloc
*sizeof(int));
2623 abYield
[iOp
] = str_in_array(zOp
, azYield
);
2624 p
->aiIndent
[iOp
] = 0;
2627 if( str_in_array(zOp
, azNext
) ){
2628 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
2630 if( str_in_array(zOp
, azGoto
) && p2op
<p
->nIndent
2631 && (abYield
[p2op
] || sqlite3_column_int(pSql
, 2))
2633 for(i
=p2op
; i
<iOp
; i
++) p
->aiIndent
[i
] += 2;
2638 sqlite3_free(abYield
);
2639 sqlite3_reset(pSql
);
2643 ** Free the array allocated by explain_data_prepare().
2645 static void explain_data_delete(ShellState
*p
){
2646 sqlite3_free(p
->aiIndent
);
2653 ** Disable and restore .wheretrace and .selecttrace settings.
2655 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2656 extern int sqlite3SelectTrace
;
2657 static int savedSelectTrace
;
2659 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2660 extern int sqlite3WhereTrace
;
2661 static int savedWhereTrace
;
2663 static void disable_debug_trace_modes(void){
2664 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2665 savedSelectTrace
= sqlite3SelectTrace
;
2666 sqlite3SelectTrace
= 0;
2668 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2669 savedWhereTrace
= sqlite3WhereTrace
;
2670 sqlite3WhereTrace
= 0;
2673 static void restore_debug_trace_modes(void){
2674 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
2675 sqlite3SelectTrace
= savedSelectTrace
;
2677 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
2678 sqlite3WhereTrace
= savedWhereTrace
;
2683 ** Run a prepared statement
2685 static void exec_prepared_stmt(
2686 ShellState
*pArg
, /* Pointer to ShellState */
2687 sqlite3_stmt
*pStmt
/* Statment to run */
2691 /* perform the first step. this will tell us if we
2692 ** have a result set or not and how wide it is.
2694 rc
= sqlite3_step(pStmt
);
2695 /* if we have a result set... */
2696 if( SQLITE_ROW
== rc
){
2697 /* allocate space for col name ptr, value ptr, and type */
2698 int nCol
= sqlite3_column_count(pStmt
);
2699 void *pData
= sqlite3_malloc64(3*nCol
*sizeof(const char*) + 1);
2703 char **azCols
= (char **)pData
; /* Names of result columns */
2704 char **azVals
= &azCols
[nCol
]; /* Results */
2705 int *aiTypes
= (int *)&azVals
[nCol
]; /* Result types */
2707 assert(sizeof(int) <= sizeof(char *));
2708 /* save off ptrs to column names */
2709 for(i
=0; i
<nCol
; i
++){
2710 azCols
[i
] = (char *)sqlite3_column_name(pStmt
, i
);
2713 /* extract the data and data types */
2714 for(i
=0; i
<nCol
; i
++){
2715 aiTypes
[i
] = x
= sqlite3_column_type(pStmt
, i
);
2716 if( x
==SQLITE_BLOB
&& pArg
&& pArg
->cMode
==MODE_Insert
){
2719 azVals
[i
] = (char*)sqlite3_column_text(pStmt
, i
);
2721 if( !azVals
[i
] && (aiTypes
[i
]!=SQLITE_NULL
) ){
2723 break; /* from for */
2727 /* if data and types extracted successfully... */
2728 if( SQLITE_ROW
== rc
){
2729 /* call the supplied callback with the result row data */
2730 if( shell_callback(pArg
, nCol
, azVals
, azCols
, aiTypes
) ){
2733 rc
= sqlite3_step(pStmt
);
2736 } while( SQLITE_ROW
== rc
);
2737 sqlite3_free(pData
);
2742 #ifndef SQLITE_OMIT_VIRTUALTABLE
2744 ** This function is called to process SQL if the previous shell command
2745 ** was ".expert". It passes the SQL in the second argument directly to
2746 ** the sqlite3expert object.
2748 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2749 ** code. In this case, (*pzErr) may be set to point to a buffer containing
2750 ** an English language error message. It is the responsibility of the
2751 ** caller to eventually free this buffer using sqlite3_free().
2753 static int expertHandleSQL(
2758 assert( pState
->expert
.pExpert
);
2759 assert( pzErr
==0 || *pzErr
==0 );
2760 return sqlite3_expert_sql(pState
->expert
.pExpert
, zSql
, pzErr
);
2764 ** This function is called either to silently clean up the object
2765 ** created by the ".expert" command (if bCancel==1), or to generate a
2766 ** report from it and then clean it up (if bCancel==0).
2768 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
2769 ** code. In this case, (*pzErr) may be set to point to a buffer containing
2770 ** an English language error message. It is the responsibility of the
2771 ** caller to eventually free this buffer using sqlite3_free().
2773 static int expertFinish(
2779 sqlite3expert
*p
= pState
->expert
.pExpert
;
2781 assert( bCancel
|| pzErr
==0 || *pzErr
==0 );
2783 FILE *out
= pState
->out
;
2784 int bVerbose
= pState
->expert
.bVerbose
;
2786 rc
= sqlite3_expert_analyze(p
, pzErr
);
2787 if( rc
==SQLITE_OK
){
2788 int nQuery
= sqlite3_expert_count(p
);
2792 const char *zCand
= sqlite3_expert_report(p
,0,EXPERT_REPORT_CANDIDATES
);
2793 raw_printf(out
, "-- Candidates -----------------------------\n");
2794 raw_printf(out
, "%s\n", zCand
);
2796 for(i
=0; i
<nQuery
; i
++){
2797 const char *zSql
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_SQL
);
2798 const char *zIdx
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_INDEXES
);
2799 const char *zEQP
= sqlite3_expert_report(p
, i
, EXPERT_REPORT_PLAN
);
2800 if( zIdx
==0 ) zIdx
= "(no new indexes)\n";
2802 raw_printf(out
, "-- Query %d --------------------------------\n",i
+1);
2803 raw_printf(out
, "%s\n\n", zSql
);
2805 raw_printf(out
, "%s\n", zIdx
);
2806 raw_printf(out
, "%s\n", zEQP
);
2810 sqlite3_expert_destroy(p
);
2811 pState
->expert
.pExpert
= 0;
2816 ** Implementation of ".expert" dot command.
2818 static int expertDotCommand(
2819 ShellState
*pState
, /* Current shell tool state */
2820 char **azArg
, /* Array of arguments passed to dot command */
2821 int nArg
/* Number of entries in azArg[] */
2828 assert( pState
->expert
.pExpert
==0 );
2829 memset(&pState
->expert
, 0, sizeof(ExpertInfo
));
2831 for(i
=1; rc
==SQLITE_OK
&& i
<nArg
; i
++){
2834 if( z
[0]=='-' && z
[1]=='-' ) z
++;
2836 if( n
>=2 && 0==strncmp(z
, "-verbose", n
) ){
2837 pState
->expert
.bVerbose
= 1;
2839 else if( n
>=2 && 0==strncmp(z
, "-sample", n
) ){
2841 raw_printf(stderr
, "option requires an argument: %s\n", z
);
2844 iSample
= (int)integerValue(azArg
[++i
]);
2845 if( iSample
<0 || iSample
>100 ){
2846 raw_printf(stderr
, "value out of range: %s\n", azArg
[i
]);
2852 raw_printf(stderr
, "unknown option: %s\n", z
);
2857 if( rc
==SQLITE_OK
){
2858 pState
->expert
.pExpert
= sqlite3_expert_new(pState
->db
, &zErr
);
2859 if( pState
->expert
.pExpert
==0 ){
2860 raw_printf(stderr
, "sqlite3_expert_new: %s\n", zErr
);
2863 sqlite3_expert_config(
2864 pState
->expert
.pExpert
, EXPERT_CONFIG_SAMPLE
, iSample
2871 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
2874 ** Execute a statement or set of statements. Print
2875 ** any result rows/columns depending on the current mode
2876 ** set via the supplied callback.
2878 ** This is very similar to SQLite's built-in sqlite3_exec()
2879 ** function except it takes a slightly different callback
2880 ** and callback data argument.
2882 static int shell_exec(
2883 ShellState
*pArg
, /* Pointer to ShellState */
2884 const char *zSql
, /* SQL to be evaluated */
2885 char **pzErrMsg
/* Error msg written here */
2887 sqlite3_stmt
*pStmt
= NULL
; /* Statement to execute. */
2888 int rc
= SQLITE_OK
; /* Return Code */
2890 const char *zLeftover
; /* Tail of unprocessed SQL */
2891 sqlite3
*db
= pArg
->db
;
2897 #ifndef SQLITE_OMIT_VIRTUALTABLE
2898 if( pArg
->expert
.pExpert
){
2899 rc
= expertHandleSQL(pArg
, zSql
, pzErrMsg
);
2900 return expertFinish(pArg
, (rc
!=SQLITE_OK
), pzErrMsg
);
2904 while( zSql
[0] && (SQLITE_OK
== rc
) ){
2905 static const char *zStmtSql
;
2906 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, &zLeftover
);
2907 if( SQLITE_OK
!= rc
){
2909 *pzErrMsg
= save_err_msg(db
);
2913 /* this happens for a comment or white-space */
2915 while( IsSpace(zSql
[0]) ) zSql
++;
2918 zStmtSql
= sqlite3_sql(pStmt
);
2919 if( zStmtSql
==0 ) zStmtSql
= "";
2920 while( IsSpace(zStmtSql
[0]) ) zStmtSql
++;
2922 /* save off the prepared statment handle and reset row count */
2924 pArg
->pStmt
= pStmt
;
2928 /* echo the sql statement if echo on */
2929 if( pArg
&& ShellHasFlag(pArg
, SHFLG_Echo
) ){
2930 utf8_printf(pArg
->out
, "%s\n", zStmtSql
? zStmtSql
: zSql
);
2933 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
2934 if( pArg
&& pArg
->autoEQP
&& sqlite3_strlike("EXPLAIN%",zStmtSql
,0)!=0 ){
2935 sqlite3_stmt
*pExplain
;
2938 disable_debug_trace_modes();
2939 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, -1, &triggerEQP
);
2940 if( pArg
->autoEQP
>=AUTOEQP_trigger
){
2941 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 1, 0);
2943 zEQP
= sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql
);
2944 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
2945 if( rc
==SQLITE_OK
){
2946 while( sqlite3_step(pExplain
)==SQLITE_ROW
){
2947 const char *zEQPLine
= (const char*)sqlite3_column_text(pExplain
,3);
2948 int iEqpId
= sqlite3_column_int(pExplain
, 0);
2949 int iParentId
= sqlite3_column_int(pExplain
, 1);
2950 if( zEQPLine
[0]=='-' ) eqp_render(pArg
);
2951 eqp_append(pArg
, iEqpId
, iParentId
, zEQPLine
);
2955 sqlite3_finalize(pExplain
);
2957 if( pArg
->autoEQP
>=AUTOEQP_full
){
2958 /* Also do an EXPLAIN for ".eqp full" mode */
2959 zEQP
= sqlite3_mprintf("EXPLAIN %s", zStmtSql
);
2960 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
2961 if( rc
==SQLITE_OK
){
2962 pArg
->cMode
= MODE_Explain
;
2963 explain_data_prepare(pArg
, pExplain
);
2964 exec_prepared_stmt(pArg
, pExplain
);
2965 explain_data_delete(pArg
);
2967 sqlite3_finalize(pExplain
);
2970 if( pArg
->autoEQP
>=AUTOEQP_trigger
&& triggerEQP
==0 ){
2971 sqlite3_db_config(db
, SQLITE_DBCONFIG_TRIGGER_EQP
, 0, 0);
2972 /* Reprepare pStmt before reactiving trace modes */
2973 sqlite3_finalize(pStmt
);
2974 sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
2976 restore_debug_trace_modes();
2980 pArg
->cMode
= pArg
->mode
;
2981 if( pArg
->autoExplain
){
2982 if( sqlite3_column_count(pStmt
)==8
2983 && sqlite3_strlike("EXPLAIN%", zStmtSql
,0)==0
2985 pArg
->cMode
= MODE_Explain
;
2987 if( sqlite3_column_count(pStmt
)==4
2988 && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql
,0)==0 ){
2989 pArg
->cMode
= MODE_EQP
;
2993 /* If the shell is currently in ".explain" mode, gather the extra
2994 ** data required to add indents to the output.*/
2995 if( pArg
->cMode
==MODE_Explain
){
2996 explain_data_prepare(pArg
, pStmt
);
3000 exec_prepared_stmt(pArg
, pStmt
);
3001 explain_data_delete(pArg
);
3004 /* print usage stats if stats on */
3005 if( pArg
&& pArg
->statsOn
){
3006 display_stats(db
, pArg
, 0);
3009 /* print loop-counters if required */
3010 if( pArg
&& pArg
->scanstatsOn
){
3011 display_scanstats(db
, pArg
);
3014 /* Finalize the statement just executed. If this fails, save a
3015 ** copy of the error message. Otherwise, set zSql to point to the
3016 ** next statement to execute. */
3017 rc2
= sqlite3_finalize(pStmt
);
3018 if( rc
!=SQLITE_NOMEM
) rc
= rc2
;
3019 if( rc
==SQLITE_OK
){
3021 while( IsSpace(zSql
[0]) ) zSql
++;
3022 }else if( pzErrMsg
){
3023 *pzErrMsg
= save_err_msg(db
);
3026 /* clear saved stmt handle */
3037 ** Release memory previously allocated by tableColumnList().
3039 static void freeColumnList(char **azCol
){
3041 for(i
=1; azCol
[i
]; i
++){
3042 sqlite3_free(azCol
[i
]);
3044 /* azCol[0] is a static string */
3045 sqlite3_free(azCol
);
3049 ** Return a list of pointers to strings which are the names of all
3050 ** columns in table zTab. The memory to hold the names is dynamically
3051 ** allocated and must be released by the caller using a subsequent call
3052 ** to freeColumnList().
3054 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
3055 ** value that needs to be preserved, then azCol[0] is filled in with the
3056 ** name of the rowid column.
3058 ** The first regular column in the table is azCol[1]. The list is terminated
3059 ** by an entry with azCol[i]==0.
3061 static char **tableColumnList(ShellState
*p
, const char *zTab
){
3063 sqlite3_stmt
*pStmt
;
3067 int nPK
= 0; /* Number of PRIMARY KEY columns seen */
3068 int isIPK
= 0; /* True if one PRIMARY KEY column of type INTEGER */
3069 int preserveRowid
= ShellHasFlag(p
, SHFLG_PreserveRowid
);
3072 zSql
= sqlite3_mprintf("PRAGMA table_info=%Q", zTab
);
3073 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
3076 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
3077 if( nCol
>=nAlloc
-2 ){
3078 nAlloc
= nAlloc
*2 + nCol
+ 10;
3079 azCol
= sqlite3_realloc(azCol
, nAlloc
*sizeof(azCol
[0]));
3080 if( azCol
==0 ) shell_out_of_memory();
3082 azCol
[++nCol
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 1));
3083 if( sqlite3_column_int(pStmt
, 5) ){
3086 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt
,2),
3095 sqlite3_finalize(pStmt
);
3096 if( azCol
==0 ) return 0;
3100 /* The decision of whether or not a rowid really needs to be preserved
3101 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
3102 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
3103 ** rowids on tables where the rowid is inaccessible because there are other
3104 ** columns in the table named "rowid", "_rowid_", and "oid".
3106 if( preserveRowid
&& isIPK
){
3107 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
3108 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
3109 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
3110 ** ROWID aliases. To distinguish these cases, check to see if
3111 ** there is a "pk" entry in "PRAGMA index_list". There will be
3112 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
3114 zSql
= sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
3115 " WHERE origin='pk'", zTab
);
3116 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
3119 freeColumnList(azCol
);
3122 rc
= sqlite3_step(pStmt
);
3123 sqlite3_finalize(pStmt
);
3124 preserveRowid
= rc
==SQLITE_ROW
;
3126 if( preserveRowid
){
3127 /* Only preserve the rowid if we can find a name to use for the
3129 static char *azRowid
[] = { "rowid", "_rowid_", "oid" };
3132 for(i
=1; i
<=nCol
; i
++){
3133 if( sqlite3_stricmp(azRowid
[j
],azCol
[i
])==0 ) break;
3136 /* At this point, we know that azRowid[j] is not the name of any
3137 ** ordinary column in the table. Verify that azRowid[j] is a valid
3138 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
3139 ** tables will fail this last check */
3140 rc
= sqlite3_table_column_metadata(p
->db
,0,zTab
,azRowid
[j
],0,0,0,0,0);
3141 if( rc
==SQLITE_OK
) azCol
[0] = azRowid
[j
];
3150 ** Toggle the reverse_unordered_selects setting.
3152 static void toggleSelectOrder(sqlite3
*db
){
3153 sqlite3_stmt
*pStmt
= 0;
3156 sqlite3_prepare_v2(db
, "PRAGMA reverse_unordered_selects", -1, &pStmt
, 0);
3157 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
3158 iSetting
= sqlite3_column_int(pStmt
, 0);
3160 sqlite3_finalize(pStmt
);
3161 sqlite3_snprintf(sizeof(zStmt
), zStmt
,
3162 "PRAGMA reverse_unordered_selects(%d)", !iSetting
);
3163 sqlite3_exec(db
, zStmt
, 0, 0, 0);
3167 ** This is a different callback routine used for dumping the database.
3168 ** Each row received by this callback consists of a table name,
3169 ** the table type ("index" or "table") and SQL to create the table.
3170 ** This routine should print text sufficient to recreate the table.
3172 static int dump_callback(void *pArg
, int nArg
, char **azArg
, char **azNotUsed
){
3177 ShellState
*p
= (ShellState
*)pArg
;
3179 UNUSED_PARAMETER(azNotUsed
);
3180 if( nArg
!=3 || azArg
==0 ) return 0;
3185 if( strcmp(zTable
, "sqlite_sequence")==0 ){
3186 raw_printf(p
->out
, "DELETE FROM sqlite_sequence;\n");
3187 }else if( sqlite3_strglob("sqlite_stat?", zTable
)==0 ){
3188 raw_printf(p
->out
, "ANALYZE sqlite_master;\n");
3189 }else if( strncmp(zTable
, "sqlite_", 7)==0 ){
3191 }else if( strncmp(zSql
, "CREATE VIRTUAL TABLE", 20)==0 ){
3193 if( !p
->writableSchema
){
3194 raw_printf(p
->out
, "PRAGMA writable_schema=ON;\n");
3195 p
->writableSchema
= 1;
3197 zIns
= sqlite3_mprintf(
3198 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
3199 "VALUES('table','%q','%q',0,'%q');",
3200 zTable
, zTable
, zSql
);
3201 utf8_printf(p
->out
, "%s\n", zIns
);
3205 printSchemaLine(p
->out
, zSql
, ";\n");
3208 if( strcmp(zType
, "table")==0 ){
3213 char *savedDestTable
;
3216 azCol
= tableColumnList(p
, zTable
);
3222 /* Always quote the table name, even if it appears to be pure ascii,
3223 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
3225 appendText(&sTable
, zTable
, quoteChar(zTable
));
3226 /* If preserving the rowid, add a column list after the table name.
3227 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
3228 ** instead of the usual "INSERT INTO tab VALUES(...)".
3231 appendText(&sTable
, "(", 0);
3232 appendText(&sTable
, azCol
[0], 0);
3233 for(i
=1; azCol
[i
]; i
++){
3234 appendText(&sTable
, ",", 0);
3235 appendText(&sTable
, azCol
[i
], quoteChar(azCol
[i
]));
3237 appendText(&sTable
, ")", 0);
3240 /* Build an appropriate SELECT statement */
3242 appendText(&sSelect
, "SELECT ", 0);
3244 appendText(&sSelect
, azCol
[0], 0);
3245 appendText(&sSelect
, ",", 0);
3247 for(i
=1; azCol
[i
]; i
++){
3248 appendText(&sSelect
, azCol
[i
], quoteChar(azCol
[i
]));
3250 appendText(&sSelect
, ",", 0);
3253 freeColumnList(azCol
);
3254 appendText(&sSelect
, " FROM ", 0);
3255 appendText(&sSelect
, zTable
, quoteChar(zTable
));
3257 savedDestTable
= p
->zDestTable
;
3258 savedMode
= p
->mode
;
3259 p
->zDestTable
= sTable
.z
;
3260 p
->mode
= p
->cMode
= MODE_Insert
;
3261 rc
= shell_exec(p
, sSelect
.z
, 0);
3262 if( (rc
&0xff)==SQLITE_CORRUPT
){
3263 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
3264 toggleSelectOrder(p
->db
);
3265 shell_exec(p
, sSelect
.z
, 0);
3266 toggleSelectOrder(p
->db
);
3268 p
->zDestTable
= savedDestTable
;
3269 p
->mode
= savedMode
;
3278 ** Run zQuery. Use dump_callback() as the callback routine so that
3279 ** the contents of the query are output as SQL statements.
3281 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
3282 ** "ORDER BY rowid DESC" to the end.
3284 static int run_schema_dump_query(
3290 rc
= sqlite3_exec(p
->db
, zQuery
, dump_callback
, p
, &zErr
);
3291 if( rc
==SQLITE_CORRUPT
){
3293 int len
= strlen30(zQuery
);
3294 raw_printf(p
->out
, "/****** CORRUPTION ERROR *******/\n");
3296 utf8_printf(p
->out
, "/****** %s ******/\n", zErr
);
3300 zQ2
= malloc( len
+100 );
3301 if( zQ2
==0 ) return rc
;
3302 sqlite3_snprintf(len
+100, zQ2
, "%s ORDER BY rowid DESC", zQuery
);
3303 rc
= sqlite3_exec(p
->db
, zQ2
, dump_callback
, p
, &zErr
);
3305 utf8_printf(p
->out
, "/****** ERROR: %s ******/\n", zErr
);
3307 rc
= SQLITE_CORRUPT
;
3316 ** Text of a help message
3318 static char zHelp
[] =
3319 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
3320 ".archive ... Manage SQL archives: \".archive --help\" for details\n"
3322 #ifndef SQLITE_OMIT_AUTHORIZATION
3323 ".auth ON|OFF Show authorizer callbacks\n"
3325 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
3326 " Add \"--append\" to open using appendvfs.\n"
3327 ".bail on|off Stop after hitting an error. Default OFF\n"
3328 ".binary on|off Turn binary output on or off. Default OFF\n"
3329 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
3330 ".changes on|off Show number of rows changed by SQL\n"
3331 ".check GLOB Fail if output since .testcase does not match\n"
3332 ".clone NEWDB Clone data into NEWDB from the existing database\n"
3333 ".databases List names and files of attached databases\n"
3334 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options\n"
3335 ".dbinfo ?DB? Show status information about the database\n"
3336 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
3337 " If TABLE specified, only dump tables matching\n"
3338 " LIKE pattern TABLE.\n"
3339 ".echo on|off Turn command echo on or off\n"
3340 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
3341 ".excel Display the output of next command in a spreadsheet\n"
3342 ".exit Exit this program\n"
3343 ".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
3344 /* Because explain mode comes on automatically now, the ".explain" mode
3345 ** is removed from the help screen. It is still supported for legacy, however */
3346 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
3347 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
3348 ".headers on|off Turn display of headers on or off\n"
3349 ".help Show this message\n"
3350 ".import FILE TABLE Import data from FILE into TABLE\n"
3351 #ifndef SQLITE_OMIT_TEST_CONTROL
3352 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
3354 ".indexes ?TABLE? Show names of all indexes\n"
3355 " If TABLE specified, only show indexes for tables\n"
3356 " matching LIKE pattern TABLE.\n"
3357 #ifdef SQLITE_ENABLE_IOTRACE
3358 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
3360 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
3361 ".lint OPTIONS Report potential schema issues. Options:\n"
3362 " fkey-indexes Find missing foreign key indexes\n"
3363 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3364 ".load FILE ?ENTRY? Load an extension library\n"
3366 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
3367 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
3368 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
3369 " csv Comma-separated values\n"
3370 " column Left-aligned columns. (See .width)\n"
3371 " html HTML <table> code\n"
3372 " insert SQL insert statements for TABLE\n"
3373 " line One value per line\n"
3374 " list Values delimited by \"|\"\n"
3375 " quote Escape answers as for SQL\n"
3376 " tabs Tab-separated values\n"
3377 " tcl TCL list elements\n"
3378 ".nullvalue STRING Use STRING in place of NULL values\n"
3379 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
3380 " or invoke system text editor (-e) or spreadsheet (-x)\n"
3382 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
3383 " The --new option starts with an empty file\n"
3384 " Other options: --readonly --append --zip\n"
3385 ".output ?FILE? Send output to FILE or stdout\n"
3386 ".print STRING... Print literal STRING\n"
3387 ".prompt MAIN CONTINUE Replace the standard prompts\n"
3388 ".quit Exit this program\n"
3389 ".read FILENAME Execute SQL in FILENAME\n"
3390 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
3391 ".save FILE Write in-memory database into FILE\n"
3392 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
3393 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
3394 " Add --indent for pretty-printing\n"
3395 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
3396 ".separator COL ?ROW? Change the column separator and optionally the row\n"
3397 " separator for both the output mode and .import\n"
3398 #if defined(SQLITE_ENABLE_SESSION)
3399 ".session CMD ... Create or control sessions\n"
3401 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
3402 #ifndef SQLITE_NOHAVE_SYSTEM
3403 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
3405 ".show Show the current values for various settings\n"
3406 ".stats ?on|off? Show stats or turn stats on or off\n"
3407 #ifndef SQLITE_NOHAVE_SYSTEM
3408 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
3410 ".tables ?TABLE? List names of tables\n"
3411 " If TABLE specified, only list tables matching\n"
3412 " LIKE pattern TABLE.\n"
3413 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
3414 ".timeout MS Try opening locked tables for MS milliseconds\n"
3415 ".timer on|off Turn SQL timer on or off\n"
3416 ".trace FILE|off Output each SQL statement as it is run\n"
3417 ".vfsinfo ?AUX? Information about the top-level VFS\n"
3418 ".vfslist List all available VFSes\n"
3419 ".vfsname ?AUX? Print the name of the VFS stack\n"
3420 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
3421 " Negative values right-justify\n"
3424 #if defined(SQLITE_ENABLE_SESSION)
3426 ** Print help information for the ".sessions" command
3428 void session_help(ShellState
*p
){
3430 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
3431 "If ?NAME? is omitted, the first defined session is used.\n"
3433 " attach TABLE Attach TABLE\n"
3434 " changeset FILE Write a changeset into FILE\n"
3435 " close Close one session\n"
3436 " enable ?BOOLEAN? Set or query the enable bit\n"
3437 " filter GLOB... Reject tables matching GLOBs\n"
3438 " indirect ?BOOLEAN? Mark or query the indirect status\n"
3439 " isempty Query whether the session is empty\n"
3440 " list List currently open session names\n"
3441 " open DB NAME Open a new session on DB\n"
3442 " patchset FILE Write a patchset into FILE\n"
3448 /* Forward reference */
3449 static int process_input(ShellState
*p
, FILE *in
);
3452 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
3453 ** and return a pointer to the buffer. The caller is responsible for freeing
3456 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
3459 ** For convenience, a nul-terminator byte is always appended to the data read
3460 ** from the file before the buffer is returned. This byte is not included in
3461 ** the final value of (*pnByte), if applicable.
3463 ** NULL is returned if any error is encountered. The final value of *pnByte
3464 ** is undefined in this case.
3466 static char *readFile(const char *zName
, int *pnByte
){
3467 FILE *in
= fopen(zName
, "rb");
3471 if( in
==0 ) return 0;
3472 fseek(in
, 0, SEEK_END
);
3475 pBuf
= sqlite3_malloc64( nIn
+1 );
3476 if( pBuf
==0 ) return 0;
3477 nRead
= fread(pBuf
, nIn
, 1, in
);
3484 if( pnByte
) *pnByte
= nIn
;
3488 #if defined(SQLITE_ENABLE_SESSION)
3490 ** Close a single OpenSession object and release all of its associated
3493 static void session_close(OpenSession
*pSession
){
3495 sqlite3session_delete(pSession
->p
);
3496 sqlite3_free(pSession
->zName
);
3497 for(i
=0; i
<pSession
->nFilter
; i
++){
3498 sqlite3_free(pSession
->azFilter
[i
]);
3500 sqlite3_free(pSession
->azFilter
);
3501 memset(pSession
, 0, sizeof(OpenSession
));
3506 ** Close all OpenSession objects and release all associated resources.
3508 #if defined(SQLITE_ENABLE_SESSION)
3509 static void session_close_all(ShellState
*p
){
3511 for(i
=0; i
<p
->nSession
; i
++){
3512 session_close(&p
->aSession
[i
]);
3517 # define session_close_all(X)
3521 ** Implementation of the xFilter function for an open session. Omit
3522 ** any tables named by ".session filter" but let all other table through.
3524 #if defined(SQLITE_ENABLE_SESSION)
3525 static int session_filter(void *pCtx
, const char *zTab
){
3526 OpenSession
*pSession
= (OpenSession
*)pCtx
;
3528 for(i
=0; i
<pSession
->nFilter
; i
++){
3529 if( sqlite3_strglob(pSession
->azFilter
[i
], zTab
)==0 ) return 0;
3536 ** Try to deduce the type of file for zName based on its content. Return
3537 ** one of the SHELL_OPEN_* constants.
3539 ** If the file does not exist or is empty but its name looks like a ZIP
3540 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
3541 ** Otherwise, assume an ordinary database regardless of the filename if
3542 ** the type cannot be determined from content.
3544 int deduceDatabaseType(const char *zName
, int dfltZip
){
3545 FILE *f
= fopen(zName
, "rb");
3547 int rc
= SHELL_OPEN_UNSPEC
;
3550 if( dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
3551 return SHELL_OPEN_ZIPFILE
;
3553 return SHELL_OPEN_NORMAL
;
3556 fseek(f
, -25, SEEK_END
);
3557 n
= fread(zBuf
, 25, 1, f
);
3558 if( n
==1 && memcmp(zBuf
, "Start-Of-SQLite3-", 17)==0 ){
3559 rc
= SHELL_OPEN_APPENDVFS
;
3561 fseek(f
, -22, SEEK_END
);
3562 n
= fread(zBuf
, 22, 1, f
);
3563 if( n
==1 && zBuf
[0]==0x50 && zBuf
[1]==0x4b && zBuf
[2]==0x05
3565 rc
= SHELL_OPEN_ZIPFILE
;
3566 }else if( n
==0 && dfltZip
&& sqlite3_strlike("%.zip",zName
,0)==0 ){
3567 rc
= SHELL_OPEN_ZIPFILE
;
3574 /* Flags for open_db().
3576 ** The default behavior of open_db() is to exit(1) if the database fails to
3577 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
3578 ** but still returns without calling exit.
3580 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
3581 ** ZIP archive if the file does not exist or is empty and its name matches
3582 ** the *.zip pattern.
3584 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
3585 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
3588 ** Make sure the database is open. If it is not, then open it. If
3589 ** the database fails to open, print an error message and exit.
3591 static void open_db(ShellState
*p
, int openFlags
){
3593 if( p
->openMode
==SHELL_OPEN_UNSPEC
){
3594 if( p
->zDbFilename
==0 || p
->zDbFilename
[0]==0 ){
3595 p
->openMode
= SHELL_OPEN_NORMAL
;
3597 p
->openMode
= (u8
)deduceDatabaseType(p
->zDbFilename
,
3598 (openFlags
& OPEN_DB_ZIPFILE
)!=0);
3601 switch( p
->openMode
){
3602 case SHELL_OPEN_APPENDVFS
: {
3603 sqlite3_open_v2(p
->zDbFilename
, &p
->db
,
3604 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
, "apndvfs");
3607 case SHELL_OPEN_ZIPFILE
: {
3608 sqlite3_open(":memory:", &p
->db
);
3611 case SHELL_OPEN_READONLY
: {
3612 sqlite3_open_v2(p
->zDbFilename
, &p
->db
, SQLITE_OPEN_READONLY
, 0);
3615 case SHELL_OPEN_UNSPEC
:
3616 case SHELL_OPEN_NORMAL
: {
3617 sqlite3_open(p
->zDbFilename
, &p
->db
);
3622 if( p
->db
==0 || SQLITE_OK
!=sqlite3_errcode(p
->db
) ){
3623 utf8_printf(stderr
,"Error: unable to open database \"%s\": %s\n",
3624 p
->zDbFilename
, sqlite3_errmsg(p
->db
));
3625 if( openFlags
& OPEN_DB_KEEPALIVE
) return;
3628 #ifndef SQLITE_OMIT_LOAD_EXTENSION
3629 sqlite3_enable_load_extension(p
->db
, 1);
3631 sqlite3_fileio_init(p
->db
, 0, 0);
3632 sqlite3_shathree_init(p
->db
, 0, 0);
3633 sqlite3_completion_init(p
->db
, 0, 0);
3634 #ifdef SQLITE_HAVE_ZLIB
3635 sqlite3_zipfile_init(p
->db
, 0, 0);
3636 sqlite3_sqlar_init(p
->db
, 0, 0);
3638 sqlite3_create_function(p
->db
, "shell_add_schema", 3, SQLITE_UTF8
, 0,
3639 shellAddSchemaName
, 0, 0);
3640 sqlite3_create_function(p
->db
, "shell_module_schema", 1, SQLITE_UTF8
, 0,
3641 shellModuleSchema
, 0, 0);
3642 sqlite3_create_function(p
->db
, "shell_putsnl", 1, SQLITE_UTF8
, p
,
3643 shellPutsFunc
, 0, 0);
3644 #ifndef SQLITE_NOHAVE_SYSTEM
3645 sqlite3_create_function(p
->db
, "edit", 1, SQLITE_UTF8
, 0,
3647 sqlite3_create_function(p
->db
, "edit", 2, SQLITE_UTF8
, 0,
3650 if( p
->openMode
==SHELL_OPEN_ZIPFILE
){
3651 char *zSql
= sqlite3_mprintf(
3652 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p
->zDbFilename
);
3653 sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
3660 ** Attempt to close the databaes connection. Report errors.
3662 void close_db(sqlite3
*db
){
3663 int rc
= sqlite3_close(db
);
3665 utf8_printf(stderr
, "Error: sqlite3_close() returns %d: %s\n",
3666 rc
, sqlite3_errmsg(db
));
3670 #if HAVE_READLINE || HAVE_EDITLINE
3672 ** Readline completion callbacks
3674 static char *readline_completion_generator(const char *text
, int state
){
3675 static sqlite3_stmt
*pStmt
= 0;
3679 sqlite3_finalize(pStmt
);
3680 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3681 " FROM completion(%Q) ORDER BY 1", text
);
3682 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
3685 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
3686 zRet
= strdup((const char*)sqlite3_column_text(pStmt
, 0));
3688 sqlite3_finalize(pStmt
);
3694 static char **readline_completion(const char *zText
, int iStart
, int iEnd
){
3695 rl_attempted_completion_over
= 1;
3696 return rl_completion_matches(zText
, readline_completion_generator
);
3699 #elif HAVE_LINENOISE
3701 ** Linenoise completion callback
3703 static void linenoise_completion(const char *zLine
, linenoiseCompletions
*lc
){
3704 int nLine
= strlen30(zLine
);
3706 sqlite3_stmt
*pStmt
= 0;
3710 if( nLine
>sizeof(zBuf
)-30 ) return;
3711 if( zLine
[0]=='.' || zLine
[0]=='#') return;
3712 for(i
=nLine
-1; i
>=0 && (isalnum(zLine
[i
]) || zLine
[i
]=='_'); i
--){}
3713 if( i
==nLine
-1 ) return;
3715 memcpy(zBuf
, zLine
, iStart
);
3716 zSql
= sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
3717 " FROM completion(%Q,%Q) ORDER BY 1",
3718 &zLine
[iStart
], zLine
);
3719 sqlite3_prepare_v2(globalDb
, zSql
, -1, &pStmt
, 0);
3721 sqlite3_exec(globalDb
, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
3722 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
3723 const char *zCompletion
= (const char*)sqlite3_column_text(pStmt
, 0);
3724 int nCompletion
= sqlite3_column_bytes(pStmt
, 0);
3725 if( iStart
+nCompletion
< sizeof(zBuf
)-1 ){
3726 memcpy(zBuf
+iStart
, zCompletion
, nCompletion
+1);
3727 linenoiseAddCompletion(lc
, zBuf
);
3730 sqlite3_finalize(pStmt
);
3735 ** Do C-language style dequoting.
3741 ** \v -> vertical tab
3743 ** \r -> carriage return
3748 ** \NNN -> ascii character NNN in octal
3750 static void resolve_backslashes(char *z
){
3753 while( *z
&& *z
!='\\' ) z
++;
3754 for(i
=j
=0; (c
= z
[i
])!=0; i
++, j
++){
3755 if( c
=='\\' && z
[i
+1]!=0 ){
3773 }else if( c
=='\'' ){
3775 }else if( c
=='\\' ){
3777 }else if( c
>='0' && c
<='7' ){
3779 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
3781 c
= (c
<<3) + z
[i
] - '0';
3782 if( z
[i
+1]>='0' && z
[i
+1]<='7' ){
3784 c
= (c
<<3) + z
[i
] - '0';
3795 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
3796 ** for TRUE and FALSE. Return the integer value if appropriate.
3798 static int booleanValue(const char *zArg
){
3800 if( zArg
[0]=='0' && zArg
[1]=='x' ){
3801 for(i
=2; hexDigitValue(zArg
[i
])>=0; i
++){}
3803 for(i
=0; zArg
[i
]>='0' && zArg
[i
]<='9'; i
++){}
3805 if( i
>0 && zArg
[i
]==0 ) return (int)(integerValue(zArg
) & 0xffffffff);
3806 if( sqlite3_stricmp(zArg
, "on")==0 || sqlite3_stricmp(zArg
,"yes")==0 ){
3809 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
3812 utf8_printf(stderr
, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
3818 ** Set or clear a shell flag according to a boolean value.
3820 static void setOrClearFlag(ShellState
*p
, unsigned mFlag
, const char *zArg
){
3821 if( booleanValue(zArg
) ){
3822 ShellSetFlag(p
, mFlag
);
3824 ShellClearFlag(p
, mFlag
);
3829 ** Close an output file, assuming it is not stderr or stdout
3831 static void output_file_close(FILE *f
){
3832 if( f
&& f
!=stdout
&& f
!=stderr
) fclose(f
);
3836 ** Try to open an output file. The names "stdout" and "stderr" are
3837 ** recognized and do the right thing. NULL is returned if the output
3838 ** filename is "off".
3840 static FILE *output_file_open(const char *zFile
, int bTextMode
){
3842 if( strcmp(zFile
,"stdout")==0 ){
3844 }else if( strcmp(zFile
, "stderr")==0 ){
3846 }else if( strcmp(zFile
, "off")==0 ){
3849 f
= fopen(zFile
, bTextMode
? "w" : "wb");
3851 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
3857 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
3859 ** A routine for handling output from sqlite3_trace().
3861 static int sql_trace_callback(
3867 FILE *f
= (FILE*)pArg
;
3868 UNUSED_PARAMETER(mType
);
3869 UNUSED_PARAMETER(pP
);
3871 const char *z
= (const char*)pX
;
3872 int i
= strlen30(z
);
3873 while( i
>0 && z
[i
-1]==';' ){ i
--; }
3874 utf8_printf(f
, "%.*s;\n", i
, z
);
3881 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
3882 ** a useful spot to set a debugger breakpoint.
3884 static void test_breakpoint(void){
3885 static int nCall
= 0;
3890 ** An object used to read a CSV and other files for import.
3892 typedef struct ImportCtx ImportCtx
;
3894 const char *zFile
; /* Name of the input file */
3895 FILE *in
; /* Read the CSV text from this input stream */
3896 char *z
; /* Accumulated text for a field */
3897 int n
; /* Number of bytes in z */
3898 int nAlloc
; /* Space allocated for z[] */
3899 int nLine
; /* Current line number */
3900 int bNotFirst
; /* True if one or more bytes already read */
3901 int cTerm
; /* Character that terminated the most recent field */
3902 int cColSep
; /* The column separator character. (Usually ",") */
3903 int cRowSep
; /* The row separator character. (Usually "\n") */
3906 /* Append a single byte to z[] */
3907 static void import_append_char(ImportCtx
*p
, int c
){
3908 if( p
->n
+1>=p
->nAlloc
){
3909 p
->nAlloc
+= p
->nAlloc
+ 100;
3910 p
->z
= sqlite3_realloc64(p
->z
, p
->nAlloc
);
3911 if( p
->z
==0 ) shell_out_of_memory();
3913 p
->z
[p
->n
++] = (char)c
;
3916 /* Read a single field of CSV text. Compatible with rfc4180 and extended
3917 ** with the option of having a separator other than ",".
3919 ** + Input comes from p->in.
3920 ** + Store results in p->z of length p->n. Space to hold p->z comes
3921 ** from sqlite3_malloc64().
3922 ** + Use p->cSep as the column separator. The default is ",".
3923 ** + Use p->rSep as the row separator. The default is "\n".
3924 ** + Keep track of the line number in p->nLine.
3925 ** + Store the character that terminates the field in p->cTerm. Store
3926 ** EOF on end-of-file.
3927 ** + Report syntax errors on stderr
3929 static char *SQLITE_CDECL
csv_read_one_field(ImportCtx
*p
){
3931 int cSep
= p
->cColSep
;
3932 int rSep
= p
->cRowSep
;
3935 if( c
==EOF
|| seenInterrupt
){
3941 int startLine
= p
->nLine
;
3946 if( c
==rSep
) p
->nLine
++;
3953 if( (c
==cSep
&& pc
==cQuote
)
3954 || (c
==rSep
&& pc
==cQuote
)
3955 || (c
==rSep
&& pc
=='\r' && ppc
==cQuote
)
3956 || (c
==EOF
&& pc
==cQuote
)
3958 do{ p
->n
--; }while( p
->z
[p
->n
]!=cQuote
);
3962 if( pc
==cQuote
&& c
!='\r' ){
3963 utf8_printf(stderr
, "%s:%d: unescaped %c character\n",
3964 p
->zFile
, p
->nLine
, cQuote
);
3967 utf8_printf(stderr
, "%s:%d: unterminated %c-quoted field\n",
3968 p
->zFile
, startLine
, cQuote
);
3972 import_append_char(p
, c
);
3977 /* If this is the first field being parsed and it begins with the
3978 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
3979 if( (c
&0xff)==0xef && p
->bNotFirst
==0 ){
3980 import_append_char(p
, c
);
3982 if( (c
&0xff)==0xbb ){
3983 import_append_char(p
, c
);
3985 if( (c
&0xff)==0xbf ){
3988 return csv_read_one_field(p
);
3992 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
3993 import_append_char(p
, c
);
3998 if( p
->n
>0 && p
->z
[p
->n
-1]=='\r' ) p
->n
--;
4002 if( p
->z
) p
->z
[p
->n
] = 0;
4007 /* Read a single field of ASCII delimited text.
4009 ** + Input comes from p->in.
4010 ** + Store results in p->z of length p->n. Space to hold p->z comes
4011 ** from sqlite3_malloc64().
4012 ** + Use p->cSep as the column separator. The default is "\x1F".
4013 ** + Use p->rSep as the row separator. The default is "\x1E".
4014 ** + Keep track of the row number in p->nLine.
4015 ** + Store the character that terminates the field in p->cTerm. Store
4016 ** EOF on end-of-file.
4017 ** + Report syntax errors on stderr
4019 static char *SQLITE_CDECL
ascii_read_one_field(ImportCtx
*p
){
4021 int cSep
= p
->cColSep
;
4022 int rSep
= p
->cRowSep
;
4025 if( c
==EOF
|| seenInterrupt
){
4029 while( c
!=EOF
&& c
!=cSep
&& c
!=rSep
){
4030 import_append_char(p
, c
);
4037 if( p
->z
) p
->z
[p
->n
] = 0;
4042 ** Try to transfer data for table zTable. If an error is seen while
4043 ** moving forward, try to go backwards. The backwards movement won't
4044 ** work for WITHOUT ROWID tables.
4046 static void tryToCloneData(
4051 sqlite3_stmt
*pQuery
= 0;
4052 sqlite3_stmt
*pInsert
= 0;
4057 int nTable
= strlen30(zTable
);
4060 const int spinRate
= 10000;
4062 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\"", zTable
);
4063 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
4065 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
4066 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
4070 n
= sqlite3_column_count(pQuery
);
4071 zInsert
= sqlite3_malloc64(200 + nTable
+ n
*3);
4072 if( zInsert
==0 ) shell_out_of_memory();
4073 sqlite3_snprintf(200+nTable
,zInsert
,
4074 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable
);
4075 i
= strlen30(zInsert
);
4077 memcpy(zInsert
+i
, ",?", 2);
4080 memcpy(zInsert
+i
, ");", 3);
4081 rc
= sqlite3_prepare_v2(newDb
, zInsert
, -1, &pInsert
, 0);
4083 utf8_printf(stderr
, "Error %d: %s on [%s]\n",
4084 sqlite3_extended_errcode(newDb
), sqlite3_errmsg(newDb
),
4089 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
4091 switch( sqlite3_column_type(pQuery
, i
) ){
4093 sqlite3_bind_null(pInsert
, i
+1);
4096 case SQLITE_INTEGER
: {
4097 sqlite3_bind_int64(pInsert
, i
+1, sqlite3_column_int64(pQuery
,i
));
4100 case SQLITE_FLOAT
: {
4101 sqlite3_bind_double(pInsert
, i
+1, sqlite3_column_double(pQuery
,i
));
4105 sqlite3_bind_text(pInsert
, i
+1,
4106 (const char*)sqlite3_column_text(pQuery
,i
),
4111 sqlite3_bind_blob(pInsert
, i
+1, sqlite3_column_blob(pQuery
,i
),
4112 sqlite3_column_bytes(pQuery
,i
),
4118 rc
= sqlite3_step(pInsert
);
4119 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_ROW
&& rc
!=SQLITE_DONE
){
4120 utf8_printf(stderr
, "Error %d: %s\n", sqlite3_extended_errcode(newDb
),
4121 sqlite3_errmsg(newDb
));
4123 sqlite3_reset(pInsert
);
4125 if( (cnt
%spinRate
)==0 ){
4126 printf("%c\b", "|/-\\"[(cnt
/spinRate
)%4]);
4130 if( rc
==SQLITE_DONE
) break;
4131 sqlite3_finalize(pQuery
);
4132 sqlite3_free(zQuery
);
4133 zQuery
= sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
4135 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
4137 utf8_printf(stderr
, "Warning: cannot step \"%s\" backwards", zTable
);
4140 } /* End for(k=0...) */
4143 sqlite3_finalize(pQuery
);
4144 sqlite3_finalize(pInsert
);
4145 sqlite3_free(zQuery
);
4146 sqlite3_free(zInsert
);
4151 ** Try to transfer all rows of the schema that match zWhere. For
4152 ** each row, invoke xForEach() on the object defined by that row.
4153 ** If an error is encountered while moving forward through the
4154 ** sqlite_master table, try again moving backwards.
4156 static void tryToCloneSchema(
4160 void (*xForEach
)(ShellState
*,sqlite3
*,const char*)
4162 sqlite3_stmt
*pQuery
= 0;
4165 const unsigned char *zName
;
4166 const unsigned char *zSql
;
4169 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4170 " WHERE %s", zWhere
);
4171 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
4173 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
4174 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
4176 goto end_schema_xfer
;
4178 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
4179 zName
= sqlite3_column_text(pQuery
, 0);
4180 zSql
= sqlite3_column_text(pQuery
, 1);
4181 printf("%s... ", zName
); fflush(stdout
);
4182 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
4184 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
4185 sqlite3_free(zErrMsg
);
4189 xForEach(p
, newDb
, (const char*)zName
);
4193 if( rc
!=SQLITE_DONE
){
4194 sqlite3_finalize(pQuery
);
4195 sqlite3_free(zQuery
);
4196 zQuery
= sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
4197 " WHERE %s ORDER BY rowid DESC", zWhere
);
4198 rc
= sqlite3_prepare_v2(p
->db
, zQuery
, -1, &pQuery
, 0);
4200 utf8_printf(stderr
, "Error: (%d) %s on [%s]\n",
4201 sqlite3_extended_errcode(p
->db
), sqlite3_errmsg(p
->db
),
4203 goto end_schema_xfer
;
4205 while( (rc
= sqlite3_step(pQuery
))==SQLITE_ROW
){
4206 zName
= sqlite3_column_text(pQuery
, 0);
4207 zSql
= sqlite3_column_text(pQuery
, 1);
4208 printf("%s... ", zName
); fflush(stdout
);
4209 sqlite3_exec(newDb
, (const char*)zSql
, 0, 0, &zErrMsg
);
4211 utf8_printf(stderr
, "Error: %s\nSQL: [%s]\n", zErrMsg
, zSql
);
4212 sqlite3_free(zErrMsg
);
4216 xForEach(p
, newDb
, (const char*)zName
);
4222 sqlite3_finalize(pQuery
);
4223 sqlite3_free(zQuery
);
4227 ** Open a new database file named "zNewDb". Try to recover as much information
4228 ** as possible out of the main database (which might be corrupt) and write it
4231 static void tryToClone(ShellState
*p
, const char *zNewDb
){
4234 if( access(zNewDb
,0)==0 ){
4235 utf8_printf(stderr
, "File \"%s\" already exists.\n", zNewDb
);
4238 rc
= sqlite3_open(zNewDb
, &newDb
);
4240 utf8_printf(stderr
, "Cannot create output database: %s\n",
4241 sqlite3_errmsg(newDb
));
4243 sqlite3_exec(p
->db
, "PRAGMA writable_schema=ON;", 0, 0, 0);
4244 sqlite3_exec(newDb
, "BEGIN EXCLUSIVE;", 0, 0, 0);
4245 tryToCloneSchema(p
, newDb
, "type='table'", tryToCloneData
);
4246 tryToCloneSchema(p
, newDb
, "type!='table'", 0);
4247 sqlite3_exec(newDb
, "COMMIT;", 0, 0, 0);
4248 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
4254 ** Change the output file back to stdout.
4256 ** If the p->doXdgOpen flag is set, that means the output was being
4257 ** redirected to a temporary file named by p->zTempFile. In that case,
4258 ** launch start/open/xdg-open on that temporary file.
4260 static void output_reset(ShellState
*p
){
4261 if( p
->outfile
[0]=='|' ){
4262 #ifndef SQLITE_OMIT_POPEN
4266 output_file_close(p
->out
);
4267 #ifndef SQLITE_NOHAVE_SYSTEM
4269 const char *zXdgOpenCmd
=
4272 #elif defined(__APPLE__)
4278 zCmd
= sqlite3_mprintf("%s %s", zXdgOpenCmd
, p
->zTempFile
);
4280 utf8_printf(stderr
, "Failed: [%s]\n", zCmd
);
4286 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
4293 ** Run an SQL command and return the single integer result.
4295 static int db_int(ShellState
*p
, const char *zSql
){
4296 sqlite3_stmt
*pStmt
;
4298 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
4299 if( pStmt
&& sqlite3_step(pStmt
)==SQLITE_ROW
){
4300 res
= sqlite3_column_int(pStmt
,0);
4302 sqlite3_finalize(pStmt
);
4307 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
4309 static unsigned int get2byteInt(unsigned char *a
){
4310 return (a
[0]<<8) + a
[1];
4312 static unsigned int get4byteInt(unsigned char *a
){
4313 return (a
[0]<<24) + (a
[1]<<16) + (a
[2]<<8) + a
[3];
4317 ** Implementation of the ".info" command.
4319 ** Return 1 on error, 2 to exit, and 0 otherwise.
4321 static int shell_dbinfo_command(ShellState
*p
, int nArg
, char **azArg
){
4322 static const struct { const char *zName
; int ofst
; } aField
[] = {
4323 { "file change counter:", 24 },
4324 { "database page count:", 28 },
4325 { "freelist page count:", 36 },
4326 { "schema cookie:", 40 },
4327 { "schema format:", 44 },
4328 { "default cache size:", 48 },
4329 { "autovacuum top root:", 52 },
4330 { "incremental vacuum:", 64 },
4331 { "text encoding:", 56 },
4332 { "user version:", 60 },
4333 { "application id:", 68 },
4334 { "software version:", 96 },
4336 static const struct { const char *zName
; const char *zSql
; } aQuery
[] = {
4337 { "number of tables:",
4338 "SELECT count(*) FROM %s WHERE type='table'" },
4339 { "number of indexes:",
4340 "SELECT count(*) FROM %s WHERE type='index'" },
4341 { "number of triggers:",
4342 "SELECT count(*) FROM %s WHERE type='trigger'" },
4343 { "number of views:",
4344 "SELECT count(*) FROM %s WHERE type='view'" },
4346 "SELECT total(length(sql)) FROM %s" },
4350 char *zDb
= nArg
>=2 ? azArg
[1] : "main";
4351 sqlite3_stmt
*pStmt
= 0;
4352 unsigned char aHdr
[100];
4354 if( p
->db
==0 ) return 1;
4355 sqlite3_prepare_v2(p
->db
,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
4357 sqlite3_bind_text(pStmt
, 1, zDb
, -1, SQLITE_STATIC
);
4358 if( sqlite3_step(pStmt
)==SQLITE_ROW
4359 && sqlite3_column_bytes(pStmt
,0)>100
4361 memcpy(aHdr
, sqlite3_column_blob(pStmt
,0), 100);
4362 sqlite3_finalize(pStmt
);
4364 raw_printf(stderr
, "unable to read database header\n");
4365 sqlite3_finalize(pStmt
);
4368 i
= get2byteInt(aHdr
+16);
4369 if( i
==1 ) i
= 65536;
4370 utf8_printf(p
->out
, "%-20s %d\n", "database page size:", i
);
4371 utf8_printf(p
->out
, "%-20s %d\n", "write format:", aHdr
[18]);
4372 utf8_printf(p
->out
, "%-20s %d\n", "read format:", aHdr
[19]);
4373 utf8_printf(p
->out
, "%-20s %d\n", "reserved bytes:", aHdr
[20]);
4374 for(i
=0; i
<ArraySize(aField
); i
++){
4375 int ofst
= aField
[i
].ofst
;
4376 unsigned int val
= get4byteInt(aHdr
+ ofst
);
4377 utf8_printf(p
->out
, "%-20s %u", aField
[i
].zName
, val
);
4380 if( val
==1 ) raw_printf(p
->out
, " (utf8)");
4381 if( val
==2 ) raw_printf(p
->out
, " (utf16le)");
4382 if( val
==3 ) raw_printf(p
->out
, " (utf16be)");
4385 raw_printf(p
->out
, "\n");
4388 zSchemaTab
= sqlite3_mprintf("main.sqlite_master");
4389 }else if( strcmp(zDb
,"temp")==0 ){
4390 zSchemaTab
= sqlite3_mprintf("%s", "sqlite_temp_master");
4392 zSchemaTab
= sqlite3_mprintf("\"%w\".sqlite_master", zDb
);
4394 for(i
=0; i
<ArraySize(aQuery
); i
++){
4395 char *zSql
= sqlite3_mprintf(aQuery
[i
].zSql
, zSchemaTab
);
4396 int val
= db_int(p
, zSql
);
4398 utf8_printf(p
->out
, "%-20s %d\n", aQuery
[i
].zName
, val
);
4400 sqlite3_free(zSchemaTab
);
4405 ** Print the current sqlite3_errmsg() value to stderr and return 1.
4407 static int shellDatabaseError(sqlite3
*db
){
4408 const char *zErr
= sqlite3_errmsg(db
);
4409 utf8_printf(stderr
, "Error: %s\n", zErr
);
4414 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
4415 ** if they match and FALSE (0) if they do not match.
4419 ** '*' Matches any sequence of zero or more characters.
4421 ** '?' Matches exactly one character.
4423 ** [...] Matches one character from the enclosed list of
4426 ** [^...] Matches one character not in the enclosed list.
4428 ** '#' Matches any sequence of one or more digits with an
4429 ** optional + or - sign in front
4431 ** ' ' Any span of whitespace matches any other span of
4434 ** Extra whitespace at the end of z[] is ignored.
4436 static int testcase_glob(const char *zGlob
, const char *z
){
4441 while( (c
= (*(zGlob
++)))!=0 ){
4443 if( !IsSpace(*z
) ) return 0;
4444 while( IsSpace(*zGlob
) ) zGlob
++;
4445 while( IsSpace(*z
) ) z
++;
4447 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
4448 if( c
=='?' && (*(z
++))==0 ) return 0;
4453 while( *z
&& testcase_glob(zGlob
-1,z
)==0 ){
4458 while( (c2
= (*(z
++)))!=0 ){
4461 if( c2
==0 ) return 0;
4463 if( testcase_glob(zGlob
,z
) ) return 1;
4467 if( (*(z
++))==0 ) return 0;
4473 if( c
==0 ) return 0;
4480 if( c
==']' ) seen
= 1;
4483 while( c2
&& c2
!=']' ){
4484 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
4486 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
4496 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
4498 if( (z
[0]=='-' || z
[0]=='+') && IsDigit(z
[1]) ) z
++;
4499 if( !IsDigit(z
[0]) ) return 0;
4501 while( IsDigit(z
[0]) ){ z
++; }
4503 if( c
!=(*(z
++)) ) return 0;
4506 while( IsSpace(*z
) ){ z
++; }
4512 ** Compare the string as a command-line option with either one or two
4513 ** initial "-" characters.
4515 static int optionMatch(const char *zStr
, const char *zOpt
){
4516 if( zStr
[0]!='-' ) return 0;
4518 if( zStr
[0]=='-' ) zStr
++;
4519 return strcmp(zStr
, zOpt
)==0;
4525 int shellDeleteFile(const char *zFilename
){
4528 wchar_t *z
= sqlite3_win32_utf8_to_unicode(zFilename
);
4532 rc
= unlink(zFilename
);
4538 ** Try to delete the temporary file (if there is one) and free the
4539 ** memory used to hold the name of the temp file.
4541 static void clearTempFile(ShellState
*p
){
4542 if( p
->zTempFile
==0 ) return;
4543 if( p
->doXdgOpen
) return;
4544 if( shellDeleteFile(p
->zTempFile
) ) return;
4545 sqlite3_free(p
->zTempFile
);
4550 ** Create a new temp file name with the given suffix.
4552 static void newTempFile(ShellState
*p
, const char *zSuffix
){
4554 sqlite3_free(p
->zTempFile
);
4557 sqlite3_file_control(p
->db
, 0, SQLITE_FCNTL_TEMPFILENAME
, &p
->zTempFile
);
4559 if( p
->zTempFile
==0 ){
4561 sqlite3_randomness(sizeof(r
), &r
);
4562 p
->zTempFile
= sqlite3_mprintf("temp%llx.%s", r
, zSuffix
);
4564 p
->zTempFile
= sqlite3_mprintf("%z.%s", p
->zTempFile
, zSuffix
);
4566 if( p
->zTempFile
==0 ){
4567 raw_printf(stderr
, "out of memory\n");
4574 ** The implementation of SQL scalar function fkey_collate_clause(), used
4575 ** by the ".lint fkey-indexes" command. This scalar function is always
4576 ** called with four arguments - the parent table name, the parent column name,
4577 ** the child table name and the child column name.
4579 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
4581 ** If either of the named tables or columns do not exist, this function
4582 ** returns an empty string. An empty string is also returned if both tables
4583 ** and columns exist but have the same default collation sequence. Or,
4584 ** if both exist but the default collation sequences are different, this
4585 ** function returns the string " COLLATE <parent-collation>", where
4586 ** <parent-collation> is the default collation sequence of the parent column.
4588 static void shellFkeyCollateClause(
4589 sqlite3_context
*pCtx
,
4591 sqlite3_value
**apVal
4593 sqlite3
*db
= sqlite3_context_db_handle(pCtx
);
4594 const char *zParent
;
4595 const char *zParentCol
;
4596 const char *zParentSeq
;
4598 const char *zChildCol
;
4599 const char *zChildSeq
= 0; /* Initialize to avoid false-positive warning */
4603 zParent
= (const char*)sqlite3_value_text(apVal
[0]);
4604 zParentCol
= (const char*)sqlite3_value_text(apVal
[1]);
4605 zChild
= (const char*)sqlite3_value_text(apVal
[2]);
4606 zChildCol
= (const char*)sqlite3_value_text(apVal
[3]);
4608 sqlite3_result_text(pCtx
, "", -1, SQLITE_STATIC
);
4609 rc
= sqlite3_table_column_metadata(
4610 db
, "main", zParent
, zParentCol
, 0, &zParentSeq
, 0, 0, 0
4612 if( rc
==SQLITE_OK
){
4613 rc
= sqlite3_table_column_metadata(
4614 db
, "main", zChild
, zChildCol
, 0, &zChildSeq
, 0, 0, 0
4618 if( rc
==SQLITE_OK
&& sqlite3_stricmp(zParentSeq
, zChildSeq
) ){
4619 char *z
= sqlite3_mprintf(" COLLATE %s", zParentSeq
);
4620 sqlite3_result_text(pCtx
, z
, -1, SQLITE_TRANSIENT
);
4627 ** The implementation of dot-command ".lint fkey-indexes".
4629 static int lintFkeyIndexes(
4630 ShellState
*pState
, /* Current shell tool state */
4631 char **azArg
, /* Array of arguments passed to dot command */
4632 int nArg
/* Number of entries in azArg[] */
4634 sqlite3
*db
= pState
->db
; /* Database handle to query "main" db of */
4635 FILE *out
= pState
->out
; /* Stream to write non-error output to */
4636 int bVerbose
= 0; /* If -verbose is present */
4637 int bGroupByParent
= 0; /* If -groupbyparent is present */
4638 int i
; /* To iterate through azArg[] */
4639 const char *zIndent
= ""; /* How much to indent CREATE INDEX by */
4640 int rc
; /* Return code */
4641 sqlite3_stmt
*pSql
= 0; /* Compiled version of SQL statement below */
4644 ** This SELECT statement returns one row for each foreign key constraint
4645 ** in the schema of the main database. The column values are:
4647 ** 0. The text of an SQL statement similar to:
4649 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
4651 ** This SELECT is similar to the one that the foreign keys implementation
4652 ** needs to run internally on child tables. If there is an index that can
4653 ** be used to optimize this query, then it can also be used by the FK
4654 ** implementation to optimize DELETE or UPDATE statements on the parent
4657 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
4658 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
4659 ** contains an index that can be used to optimize the query.
4661 ** 2. Human readable text that describes the child table and columns. e.g.
4663 ** "child_table(child_key1, child_key2)"
4665 ** 3. Human readable text that describes the parent table and columns. e.g.
4667 ** "parent_table(parent_key1, parent_key2)"
4669 ** 4. A full CREATE INDEX statement for an index that could be used to
4670 ** optimize DELETE or UPDATE statements on the parent table. e.g.
4672 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
4674 ** 5. The name of the parent table.
4676 ** These six values are used by the C logic below to generate the report.
4680 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
4681 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
4682 " || fkey_collate_clause("
4683 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
4685 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
4686 " || group_concat('*=?', ' AND ') || ')'"
4688 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
4690 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
4692 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
4693 " || ' ON ' || quote(s.name) || '('"
4694 " || group_concat(quote(f.[from]) ||"
4695 " fkey_collate_clause("
4696 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
4700 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
4701 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
4702 "GROUP BY s.name, f.id "
4703 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
4705 const char *zGlobIPK
= "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
4707 for(i
=2; i
<nArg
; i
++){
4708 int n
= strlen30(azArg
[i
]);
4709 if( n
>1 && sqlite3_strnicmp("-verbose", azArg
[i
], n
)==0 ){
4712 else if( n
>1 && sqlite3_strnicmp("-groupbyparent", azArg
[i
], n
)==0 ){
4717 raw_printf(stderr
, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
4720 return SQLITE_ERROR
;
4724 /* Register the fkey_collate_clause() SQL function */
4725 rc
= sqlite3_create_function(db
, "fkey_collate_clause", 4, SQLITE_UTF8
,
4726 0, shellFkeyCollateClause
, 0, 0
4730 if( rc
==SQLITE_OK
){
4731 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pSql
, 0);
4733 if( rc
==SQLITE_OK
){
4734 sqlite3_bind_int(pSql
, 1, bGroupByParent
);
4737 if( rc
==SQLITE_OK
){
4740 while( SQLITE_ROW
==sqlite3_step(pSql
) ){
4742 sqlite3_stmt
*pExplain
= 0;
4743 const char *zEQP
= (const char*)sqlite3_column_text(pSql
, 0);
4744 const char *zGlob
= (const char*)sqlite3_column_text(pSql
, 1);
4745 const char *zFrom
= (const char*)sqlite3_column_text(pSql
, 2);
4746 const char *zTarget
= (const char*)sqlite3_column_text(pSql
, 3);
4747 const char *zCI
= (const char*)sqlite3_column_text(pSql
, 4);
4748 const char *zParent
= (const char*)sqlite3_column_text(pSql
, 5);
4750 rc
= sqlite3_prepare_v2(db
, zEQP
, -1, &pExplain
, 0);
4751 if( rc
!=SQLITE_OK
) break;
4752 if( SQLITE_ROW
==sqlite3_step(pExplain
) ){
4753 const char *zPlan
= (const char*)sqlite3_column_text(pExplain
, 3);
4755 0==sqlite3_strglob(zGlob
, zPlan
)
4756 || 0==sqlite3_strglob(zGlobIPK
, zPlan
)
4759 rc
= sqlite3_finalize(pExplain
);
4760 if( rc
!=SQLITE_OK
) break;
4763 raw_printf(stderr
, "Error: internal error");
4767 && (bVerbose
|| res
==0)
4768 && (zPrev
==0 || sqlite3_stricmp(zParent
, zPrev
))
4770 raw_printf(out
, "-- Parent table %s\n", zParent
);
4771 sqlite3_free(zPrev
);
4772 zPrev
= sqlite3_mprintf("%s", zParent
);
4776 raw_printf(out
, "%s%s --> %s\n", zIndent
, zCI
, zTarget
);
4777 }else if( bVerbose
){
4778 raw_printf(out
, "%s/* no extra indexes required for %s -> %s */\n",
4779 zIndent
, zFrom
, zTarget
4784 sqlite3_free(zPrev
);
4786 if( rc
!=SQLITE_OK
){
4787 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
4790 rc2
= sqlite3_finalize(pSql
);
4791 if( rc
==SQLITE_OK
&& rc2
!=SQLITE_OK
){
4793 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
4796 raw_printf(stderr
, "%s\n", sqlite3_errmsg(db
));
4803 ** Implementation of ".lint" dot command.
4805 static int lintDotCommand(
4806 ShellState
*pState
, /* Current shell tool state */
4807 char **azArg
, /* Array of arguments passed to dot command */
4808 int nArg
/* Number of entries in azArg[] */
4811 n
= (nArg
>=2 ? strlen30(azArg
[1]) : 0);
4812 if( n
<1 || sqlite3_strnicmp(azArg
[1], "fkey-indexes", n
) ) goto usage
;
4813 return lintFkeyIndexes(pState
, azArg
, nArg
);
4816 raw_printf(stderr
, "Usage %s sub-command ?switches...?\n", azArg
[0]);
4817 raw_printf(stderr
, "Where sub-commands are:\n");
4818 raw_printf(stderr
, " fkey-indexes\n");
4819 return SQLITE_ERROR
;
4822 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
4823 /*********************************************************************************
4824 ** The ".archive" or ".ar" command.
4826 static void shellPrepare(
4830 sqlite3_stmt
**ppStmt
4833 if( *pRc
==SQLITE_OK
){
4834 int rc
= sqlite3_prepare_v2(db
, zSql
, -1, ppStmt
, 0);
4835 if( rc
!=SQLITE_OK
){
4836 raw_printf(stderr
, "sql error: %s (%d)\n",
4837 sqlite3_errmsg(db
), sqlite3_errcode(db
)
4844 static void shellPreparePrintf(
4847 sqlite3_stmt
**ppStmt
,
4852 if( *pRc
==SQLITE_OK
){
4856 z
= sqlite3_vmprintf(zFmt
, ap
);
4858 *pRc
= SQLITE_NOMEM
;
4860 shellPrepare(db
, pRc
, z
, ppStmt
);
4866 static void shellFinalize(
4871 sqlite3
*db
= sqlite3_db_handle(pStmt
);
4872 int rc
= sqlite3_finalize(pStmt
);
4873 if( *pRc
==SQLITE_OK
){
4874 if( rc
!=SQLITE_OK
){
4875 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
4882 static void shellReset(
4886 int rc
= sqlite3_reset(pStmt
);
4887 if( *pRc
==SQLITE_OK
){
4888 if( rc
!=SQLITE_OK
){
4889 sqlite3
*db
= sqlite3_db_handle(pStmt
);
4890 raw_printf(stderr
, "SQL error: %s\n", sqlite3_errmsg(db
));
4896 ** Structure representing a single ".ar" command.
4898 typedef struct ArCommand ArCommand
;
4900 u8 eCmd
; /* An AR_CMD_* value */
4901 u8 bVerbose
; /* True if --verbose */
4902 u8 bZip
; /* True if the archive is a ZIP */
4903 u8 bDryRun
; /* True if --dry-run */
4904 u8 bAppend
; /* True if --append */
4905 u8 fromCmdLine
; /* Run from -A instead of .archive */
4906 int nArg
; /* Number of command arguments */
4907 char *zSrcTable
; /* "sqlar", "zipfile($file)" or "zip" */
4908 const char *zFile
; /* --file argument, or NULL */
4909 const char *zDir
; /* --directory argument, or NULL */
4910 char **azArg
; /* Array of command arguments */
4911 ShellState
*p
; /* Shell state */
4912 sqlite3
*db
; /* Database containing the archive */
4916 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
4918 static int arUsage(FILE *f
){
4921 "Usage: .ar [OPTION...] [FILE...]\n"
4922 "The .ar command manages sqlar archives.\n"
4925 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar\n"
4926 " .ar -tf archive.sar # List members of archive.sar\n"
4927 " .ar -xvf archive.sar # Verbosely extract files from archive.sar\n"
4929 "Each command line must feature exactly one command option:\n"
4930 " -c, --create Create a new archive\n"
4931 " -u, --update Update or add files to an existing archive\n"
4932 " -t, --list List contents of archive\n"
4933 " -x, --extract Extract files from archive\n"
4935 "And zero or more optional options:\n"
4936 " -v, --verbose Print each filename as it is processed\n"
4937 " -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
4938 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n"
4939 " -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
4940 " -n, --dryrun Show the SQL that would have occurred\n"
4942 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
4945 return SQLITE_ERROR
;
4949 ** Print an error message for the .ar command to stderr and return
4952 static int arErrorMsg(ArCommand
*pAr
, const char *zFmt
, ...){
4956 z
= sqlite3_vmprintf(zFmt
, ap
);
4958 utf8_printf(stderr
, "Error: %s\n", z
);
4959 if( pAr
->fromCmdLine
){
4960 utf8_printf(stderr
, "Use \"-A\" for more help\n");
4962 utf8_printf(stderr
, "Use \".archive --help\" for more help\n");
4965 return SQLITE_ERROR
;
4969 ** Values for ArCommand.eCmd.
4971 #define AR_CMD_CREATE 1
4972 #define AR_CMD_EXTRACT 2
4973 #define AR_CMD_LIST 3
4974 #define AR_CMD_UPDATE 4
4975 #define AR_CMD_HELP 5
4978 ** Other (non-command) switches.
4980 #define AR_SWITCH_VERBOSE 6
4981 #define AR_SWITCH_FILE 7
4982 #define AR_SWITCH_DIRECTORY 8
4983 #define AR_SWITCH_APPEND 9
4984 #define AR_SWITCH_DRYRUN 10
4986 static int arProcessSwitch(ArCommand
*pAr
, int eSwitch
, const char *zArg
){
4989 case AR_CMD_EXTRACT
:
4994 return arErrorMsg(pAr
, "multiple command options");
4996 pAr
->eCmd
= eSwitch
;
4999 case AR_SWITCH_DRYRUN
:
5002 case AR_SWITCH_VERBOSE
:
5005 case AR_SWITCH_APPEND
:
5007 /* Fall thru into --file */
5008 case AR_SWITCH_FILE
:
5011 case AR_SWITCH_DIRECTORY
:
5020 ** Parse the command line for an ".ar" command. The results are written into
5021 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
5022 ** successfully, otherwise an error message is written to stderr and
5023 ** SQLITE_ERROR returned.
5025 static int arParseCommand(
5026 char **azArg
, /* Array of arguments passed to dot command */
5027 int nArg
, /* Number of entries in azArg[] */
5028 ArCommand
*pAr
/* Populate this object */
5036 { "create", 'c', AR_CMD_CREATE
, 0 },
5037 { "extract", 'x', AR_CMD_EXTRACT
, 0 },
5038 { "list", 't', AR_CMD_LIST
, 0 },
5039 { "update", 'u', AR_CMD_UPDATE
, 0 },
5040 { "help", 'h', AR_CMD_HELP
, 0 },
5041 { "verbose", 'v', AR_SWITCH_VERBOSE
, 0 },
5042 { "file", 'f', AR_SWITCH_FILE
, 1 },
5043 { "append", 'a', AR_SWITCH_APPEND
, 1 },
5044 { "directory", 'C', AR_SWITCH_DIRECTORY
, 1 },
5045 { "dryrun", 'n', AR_SWITCH_DRYRUN
, 0 },
5047 int nSwitch
= sizeof(aSwitch
) / sizeof(struct ArSwitch
);
5048 struct ArSwitch
*pEnd
= &aSwitch
[nSwitch
];
5051 return arUsage(stderr
);
5055 /* Traditional style [tar] invocation */
5058 for(i
=0; z
[i
]; i
++){
5059 const char *zArg
= 0;
5060 struct ArSwitch
*pOpt
;
5061 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
5062 if( z
[i
]==pOpt
->cShort
) break;
5065 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
5069 return arErrorMsg(pAr
, "option requires an argument: %c",z
[i
]);
5071 zArg
= azArg
[iArg
++];
5073 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
5075 pAr
->nArg
= nArg
-iArg
;
5077 pAr
->azArg
= &azArg
[iArg
];
5080 /* Non-traditional invocation */
5082 for(iArg
=1; iArg
<nArg
; iArg
++){
5086 /* All remaining command line words are command arguments. */
5087 pAr
->azArg
= &azArg
[iArg
];
5088 pAr
->nArg
= nArg
-iArg
;
5095 /* One or more short options */
5097 const char *zArg
= 0;
5098 struct ArSwitch
*pOpt
;
5099 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
5100 if( z
[i
]==pOpt
->cShort
) break;
5103 return arErrorMsg(pAr
, "unrecognized option: %c", z
[i
]);
5110 if( iArg
>=(nArg
-1) ){
5111 return arErrorMsg(pAr
, "option requires an argument: %c",z
[i
]);
5113 zArg
= azArg
[++iArg
];
5116 if( arProcessSwitch(pAr
, pOpt
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
5118 }else if( z
[2]=='\0' ){
5119 /* A -- option, indicating that all remaining command line words
5120 ** are command arguments. */
5121 pAr
->azArg
= &azArg
[iArg
+1];
5122 pAr
->nArg
= nArg
-iArg
-1;
5126 const char *zArg
= 0; /* Argument for option, if any */
5127 struct ArSwitch
*pMatch
= 0; /* Matching option */
5128 struct ArSwitch
*pOpt
; /* Iterator */
5129 for(pOpt
=&aSwitch
[0]; pOpt
<pEnd
; pOpt
++){
5130 const char *zLong
= pOpt
->zLong
;
5131 if( (n
-2)<=strlen30(zLong
) && 0==memcmp(&z
[2], zLong
, n
-2) ){
5133 return arErrorMsg(pAr
, "ambiguous option: %s",z
);
5141 return arErrorMsg(pAr
, "unrecognized option: %s", z
);
5144 if( iArg
>=(nArg
-1) ){
5145 return arErrorMsg(pAr
, "option requires an argument: %s", z
);
5147 zArg
= azArg
[++iArg
];
5149 if( arProcessSwitch(pAr
, pMatch
->eSwitch
, zArg
) ) return SQLITE_ERROR
;
5159 ** This function assumes that all arguments within the ArCommand.azArg[]
5160 ** array refer to archive members, as for the --extract or --list commands.
5161 ** It checks that each of them are present. If any specified file is not
5162 ** present in the archive, an error is printed to stderr and an error
5163 ** code returned. Otherwise, if all specified arguments are present in
5164 ** the archive, SQLITE_OK is returned.
5166 ** This function strips any trailing '/' characters from each argument.
5167 ** This is consistent with the way the [tar] command seems to work on
5170 static int arCheckEntries(ArCommand
*pAr
){
5174 sqlite3_stmt
*pTest
= 0;
5176 shellPreparePrintf(pAr
->db
, &rc
, &pTest
,
5177 "SELECT name FROM %s WHERE name=$name",
5180 j
= sqlite3_bind_parameter_index(pTest
, "$name");
5181 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
5182 char *z
= pAr
->azArg
[i
];
5183 int n
= strlen30(z
);
5185 while( n
>0 && z
[n
-1]=='/' ) n
--;
5187 sqlite3_bind_text(pTest
, j
, z
, -1, SQLITE_STATIC
);
5188 if( SQLITE_ROW
==sqlite3_step(pTest
) ){
5191 shellReset(&rc
, pTest
);
5192 if( rc
==SQLITE_OK
&& bOk
==0 ){
5193 utf8_printf(stderr
, "not found in archive: %s\n", z
);
5197 shellFinalize(&rc
, pTest
);
5203 ** Format a WHERE clause that can be used against the "sqlar" table to
5204 ** identify all archive members that match the command arguments held
5205 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
5206 ** The caller is responsible for eventually calling sqlite3_free() on
5207 ** any non-NULL (*pzWhere) value.
5209 static void arWhereClause(
5212 char **pzWhere
/* OUT: New WHERE clause */
5215 if( *pRc
==SQLITE_OK
){
5217 zWhere
= sqlite3_mprintf("1");
5220 const char *zSep
= "";
5221 for(i
=0; i
<pAr
->nArg
; i
++){
5222 const char *z
= pAr
->azArg
[i
];
5223 zWhere
= sqlite3_mprintf(
5224 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
5225 zWhere
, zSep
, z
, strlen30(z
)+1, z
5228 *pRc
= SQLITE_NOMEM
;
5239 ** Implementation of .ar "lisT" command.
5241 static int arListCommand(ArCommand
*pAr
){
5242 const char *zSql
= "SELECT %s FROM %s WHERE %s";
5243 const char *azCols
[] = {
5245 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
5249 sqlite3_stmt
*pSql
= 0;
5252 rc
= arCheckEntries(pAr
);
5253 arWhereClause(&rc
, pAr
, &zWhere
);
5255 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql
, azCols
[pAr
->bVerbose
],
5256 pAr
->zSrcTable
, zWhere
);
5258 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
5260 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
5261 if( pAr
->bVerbose
){
5262 utf8_printf(pAr
->p
->out
, "%s % 10d %s %s\n",
5263 sqlite3_column_text(pSql
, 0),
5264 sqlite3_column_int(pSql
, 1),
5265 sqlite3_column_text(pSql
, 2),
5266 sqlite3_column_text(pSql
, 3)
5269 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
5273 shellFinalize(&rc
, pSql
);
5274 sqlite3_free(zWhere
);
5280 ** Implementation of .ar "eXtract" command.
5282 static int arExtractCommand(ArCommand
*pAr
){
5286 " writefile(($dir || name), %s, mode, mtime) "
5287 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
5289 const char *azExtraArg
[] = {
5290 "sqlar_uncompress(data, sz)",
5294 sqlite3_stmt
*pSql
= 0;
5300 /* If arguments are specified, check that they actually exist within
5301 ** the archive before proceeding. And formulate a WHERE clause to
5303 rc
= arCheckEntries(pAr
);
5304 arWhereClause(&rc
, pAr
, &zWhere
);
5306 if( rc
==SQLITE_OK
){
5308 zDir
= sqlite3_mprintf("%s/", pAr
->zDir
);
5310 zDir
= sqlite3_mprintf("");
5312 if( zDir
==0 ) rc
= SQLITE_NOMEM
;
5315 shellPreparePrintf(pAr
->db
, &rc
, &pSql
, zSql1
,
5316 azExtraArg
[pAr
->bZip
], pAr
->zSrcTable
, zWhere
5319 if( rc
==SQLITE_OK
){
5320 j
= sqlite3_bind_parameter_index(pSql
, "$dir");
5321 sqlite3_bind_text(pSql
, j
, zDir
, -1, SQLITE_STATIC
);
5323 /* Run the SELECT statement twice. The first time, writefile() is called
5324 ** for all archive members that should be extracted. The second time,
5325 ** only for the directories. This is because the timestamps for
5326 ** extracted directories must be reset after they are populated (as
5327 ** populating them changes the timestamp). */
5329 j
= sqlite3_bind_parameter_index(pSql
, "$dirOnly");
5330 sqlite3_bind_int(pSql
, j
, i
);
5332 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_sql(pSql
));
5334 while( rc
==SQLITE_OK
&& SQLITE_ROW
==sqlite3_step(pSql
) ){
5335 if( i
==0 && pAr
->bVerbose
){
5336 utf8_printf(pAr
->p
->out
, "%s\n", sqlite3_column_text(pSql
, 0));
5340 shellReset(&rc
, pSql
);
5342 shellFinalize(&rc
, pSql
);
5346 sqlite3_free(zWhere
);
5351 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
5353 static int arExecSql(ArCommand
*pAr
, const char *zSql
){
5356 utf8_printf(pAr
->p
->out
, "%s\n", zSql
);
5360 rc
= sqlite3_exec(pAr
->db
, zSql
, 0, 0, &zErr
);
5362 utf8_printf(stdout
, "ERROR: %s\n", zErr
);
5371 ** Implementation of .ar "create" and "update" commands.
5373 ** Create the "sqlar" table in the database if it does not already exist.
5374 ** Then add each file in the azFile[] array to the archive. Directories
5375 ** are added recursively. If argument bVerbose is non-zero, a message is
5376 ** printed on stdout for each file archived.
5378 ** The create command is the same as update, except that it drops
5379 ** any existing "sqlar" table before beginning.
5381 static int arCreateOrUpdateCommand(
5382 ArCommand
*pAr
, /* Command arguments and options */
5383 int bUpdate
/* true for a --create. false for --update */
5385 const char *zCreate
=
5386 "CREATE TABLE IF NOT EXISTS sqlar(\n"
5387 " name TEXT PRIMARY KEY, -- name of the file\n"
5388 " mode INT, -- access permissions\n"
5389 " mtime INT, -- last modification time\n"
5390 " sz INT, -- original file size\n"
5391 " data BLOB -- compressed content\n"
5393 const char *zDrop
= "DROP TABLE IF EXISTS sqlar";
5394 const char *zInsertFmt
[2] = {
5395 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
5400 " CASE substr(lsmode(mode),1,1)\n"
5401 " WHEN '-' THEN length(data)\n"
5402 " WHEN 'd' THEN 0\n"
5404 " sqlar_compress(data)\n"
5405 " FROM fsdir(%Q,%Q)\n"
5406 " WHERE lsmode(mode) NOT LIKE '?%%';",
5407 "REPLACE INTO %s(name,mode,mtime,data)\n"
5413 " FROM fsdir(%Q,%Q)\n"
5414 " WHERE lsmode(mode) NOT LIKE '?%%';"
5416 int i
; /* For iterating through azFile[] */
5417 int rc
; /* Return code */
5418 const char *zTab
= 0; /* SQL table into which to insert */
5422 arExecSql(pAr
, "PRAGMA page_size=512");
5423 rc
= arExecSql(pAr
, "SAVEPOINT ar;");
5424 if( rc
!=SQLITE_OK
) return rc
;
5427 /* Initialize the zipfile virtual table, if necessary */
5430 sqlite3_randomness(sizeof(r
),&r
);
5431 sqlite3_snprintf(sizeof(zTemp
),zTemp
,"zip%016llx",r
);
5433 zSql
= sqlite3_mprintf(
5434 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
5437 rc
= arExecSql(pAr
, zSql
);
5443 /* Initialize the table for an SQLAR */
5446 rc
= arExecSql(pAr
, zDrop
);
5447 if( rc
!=SQLITE_OK
) goto end_ar_transaction
;
5449 rc
= arExecSql(pAr
, zCreate
);
5451 for(i
=0; i
<pAr
->nArg
&& rc
==SQLITE_OK
; i
++){
5452 char *zSql2
= sqlite3_mprintf(zInsertFmt
[pAr
->bZip
], zTab
,
5453 pAr
->bVerbose
? "shell_putsnl(name)" : "name",
5454 pAr
->azArg
[i
], pAr
->zDir
);
5455 rc
= arExecSql(pAr
, zSql2
);
5456 sqlite3_free(zSql2
);
5459 if( rc
!=SQLITE_OK
){
5460 arExecSql(pAr
, "ROLLBACK TO ar; RELEASE ar;");
5462 rc
= arExecSql(pAr
, "RELEASE ar;");
5463 if( pAr
->bZip
&& pAr
->zFile
){
5464 zSql
= sqlite3_mprintf("DROP TABLE %s", zTemp
);
5465 arExecSql(pAr
, zSql
);
5473 ** Implementation of ".ar" dot command.
5475 static int arDotCommand(
5476 ShellState
*pState
, /* Current shell tool state */
5477 int fromCmdLine
, /* True if -A command-line option, not .ar cmd */
5478 char **azArg
, /* Array of arguments passed to dot command */
5479 int nArg
/* Number of entries in azArg[] */
5483 memset(&cmd
, 0, sizeof(cmd
));
5484 cmd
.fromCmdLine
= fromCmdLine
;
5485 rc
= arParseCommand(azArg
, nArg
, &cmd
);
5486 if( rc
==SQLITE_OK
){
5487 int eDbType
= SHELL_OPEN_UNSPEC
;
5489 cmd
.db
= pState
->db
;
5491 eDbType
= deduceDatabaseType(cmd
.zFile
, 1);
5493 eDbType
= pState
->openMode
;
5495 if( eDbType
==SHELL_OPEN_ZIPFILE
){
5496 if( cmd
.eCmd
==AR_CMD_EXTRACT
|| cmd
.eCmd
==AR_CMD_LIST
){
5498 cmd
.zSrcTable
= sqlite3_mprintf("zip");
5500 cmd
.zSrcTable
= sqlite3_mprintf("zipfile(%Q)", cmd
.zFile
);
5504 }else if( cmd
.zFile
){
5506 if( cmd
.bAppend
) eDbType
= SHELL_OPEN_APPENDVFS
;
5507 if( cmd
.eCmd
==AR_CMD_CREATE
|| cmd
.eCmd
==AR_CMD_UPDATE
){
5508 flags
= SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
;
5510 flags
= SQLITE_OPEN_READONLY
;
5514 utf8_printf(pState
->out
, "-- open database '%s'%s\n", cmd
.zFile
,
5515 eDbType
==SHELL_OPEN_APPENDVFS
? " using 'apndvfs'" : "");
5517 rc
= sqlite3_open_v2(cmd
.zFile
, &cmd
.db
, flags
,
5518 eDbType
==SHELL_OPEN_APPENDVFS
? "apndvfs" : 0);
5519 if( rc
!=SQLITE_OK
){
5520 utf8_printf(stderr
, "cannot open file: %s (%s)\n",
5521 cmd
.zFile
, sqlite3_errmsg(cmd
.db
)
5523 goto end_ar_command
;
5525 sqlite3_fileio_init(cmd
.db
, 0, 0);
5526 sqlite3_sqlar_init(cmd
.db
, 0, 0);
5527 sqlite3_create_function(cmd
.db
, "shell_putsnl", 1, SQLITE_UTF8
, cmd
.p
,
5528 shellPutsFunc
, 0, 0);
5531 if( cmd
.zSrcTable
==0 && cmd
.bZip
==0 && cmd
.eCmd
!=AR_CMD_HELP
){
5532 if( cmd
.eCmd
!=AR_CMD_CREATE
5533 && sqlite3_table_column_metadata(cmd
.db
,0,"sqlar","name",0,0,0,0,0)
5535 utf8_printf(stderr
, "database does not contain an 'sqlar' table\n");
5537 goto end_ar_command
;
5539 cmd
.zSrcTable
= sqlite3_mprintf("sqlar");
5544 rc
= arCreateOrUpdateCommand(&cmd
, 0);
5547 case AR_CMD_EXTRACT
:
5548 rc
= arExtractCommand(&cmd
);
5552 rc
= arListCommand(&cmd
);
5556 arUsage(pState
->out
);
5560 assert( cmd
.eCmd
==AR_CMD_UPDATE
);
5561 rc
= arCreateOrUpdateCommand(&cmd
, 1);
5566 if( cmd
.db
!=pState
->db
){
5569 sqlite3_free(cmd
.zSrcTable
);
5573 /* End of the ".archive" or ".ar" command logic
5574 **********************************************************************************/
5575 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
5579 ** If an input line begins with "." then invoke this routine to
5580 ** process that line.
5582 ** Return 1 on error, 2 to exit, and 0 otherwise.
5584 static int do_meta_command(char *zLine
, ShellState
*p
){
5591 #ifndef SQLITE_OMIT_VIRTUALTABLE
5592 if( p
->expert
.pExpert
){
5593 expertFinish(p
, 1, 0);
5597 /* Parse the input line into tokens.
5599 while( zLine
[h
] && nArg
<ArraySize(azArg
) ){
5600 while( IsSpace(zLine
[h
]) ){ h
++; }
5601 if( zLine
[h
]==0 ) break;
5602 if( zLine
[h
]=='\'' || zLine
[h
]=='"' ){
5603 int delim
= zLine
[h
++];
5604 azArg
[nArg
++] = &zLine
[h
];
5605 while( zLine
[h
] && zLine
[h
]!=delim
){
5606 if( zLine
[h
]=='\\' && delim
=='"' && zLine
[h
+1]!=0 ) h
++;
5609 if( zLine
[h
]==delim
){
5612 if( delim
=='"' ) resolve_backslashes(azArg
[nArg
-1]);
5614 azArg
[nArg
++] = &zLine
[h
];
5615 while( zLine
[h
] && !IsSpace(zLine
[h
]) ){ h
++; }
5616 if( zLine
[h
] ) zLine
[h
++] = 0;
5617 resolve_backslashes(azArg
[nArg
-1]);
5621 /* Process the input line.
5623 if( nArg
==0 ) return 0; /* no tokens, no error */
5624 n
= strlen30(azArg
[0]);
5628 #ifndef SQLITE_OMIT_AUTHORIZATION
5629 if( c
=='a' && strncmp(azArg
[0], "auth", n
)==0 ){
5631 raw_printf(stderr
, "Usage: .auth ON|OFF\n");
5633 goto meta_command_exit
;
5636 if( booleanValue(azArg
[1]) ){
5637 sqlite3_set_authorizer(p
->db
, shellAuth
, p
);
5639 sqlite3_set_authorizer(p
->db
, 0, 0);
5644 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
5645 if( c
=='a' && strncmp(azArg
[0], "archive", n
)==0 ){
5647 rc
= arDotCommand(p
, 0, azArg
, nArg
);
5651 if( (c
=='b' && n
>=3 && strncmp(azArg
[0], "backup", n
)==0)
5652 || (c
=='s' && n
>=3 && strncmp(azArg
[0], "save", n
)==0)
5654 const char *zDestFile
= 0;
5655 const char *zDb
= 0;
5657 sqlite3_backup
*pBackup
;
5659 const char *zVfs
= 0;
5660 for(j
=1; j
<nArg
; j
++){
5661 const char *z
= azArg
[j
];
5663 if( z
[1]=='-' ) z
++;
5664 if( strcmp(z
, "-append")==0 ){
5668 utf8_printf(stderr
, "unknown option: %s\n", azArg
[j
]);
5671 }else if( zDestFile
==0 ){
5672 zDestFile
= azArg
[j
];
5675 zDestFile
= azArg
[j
];
5677 raw_printf(stderr
, "Usage: .backup ?DB? ?--append? FILENAME\n");
5682 raw_printf(stderr
, "missing FILENAME argument on .backup\n");
5685 if( zDb
==0 ) zDb
= "main";
5686 rc
= sqlite3_open_v2(zDestFile
, &pDest
,
5687 SQLITE_OPEN_READWRITE
|SQLITE_OPEN_CREATE
, zVfs
);
5688 if( rc
!=SQLITE_OK
){
5689 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zDestFile
);
5694 pBackup
= sqlite3_backup_init(pDest
, "main", p
->db
, zDb
);
5696 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
5700 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
){}
5701 sqlite3_backup_finish(pBackup
);
5702 if( rc
==SQLITE_DONE
){
5705 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(pDest
));
5711 if( c
=='b' && n
>=3 && strncmp(azArg
[0], "bail", n
)==0 ){
5713 bail_on_error
= booleanValue(azArg
[1]);
5715 raw_printf(stderr
, "Usage: .bail on|off\n");
5720 if( c
=='b' && n
>=3 && strncmp(azArg
[0], "binary", n
)==0 ){
5722 if( booleanValue(azArg
[1]) ){
5723 setBinaryMode(p
->out
, 1);
5725 setTextMode(p
->out
, 1);
5728 raw_printf(stderr
, "Usage: .binary on|off\n");
5733 if( c
=='c' && strcmp(azArg
[0],"cd")==0 ){
5735 #if defined(_WIN32) || defined(WIN32)
5736 wchar_t *z
= sqlite3_win32_utf8_to_unicode(azArg
[1]);
5737 rc
= !SetCurrentDirectoryW(z
);
5740 rc
= chdir(azArg
[1]);
5743 utf8_printf(stderr
, "Cannot change to directory \"%s\"\n", azArg
[1]);
5747 raw_printf(stderr
, "Usage: .cd DIRECTORY\n");
5752 /* The undocumented ".breakpoint" command causes a call to the no-op
5753 ** routine named test_breakpoint().
5755 if( c
=='b' && n
>=3 && strncmp(azArg
[0], "breakpoint", n
)==0 ){
5759 if( c
=='c' && n
>=3 && strncmp(azArg
[0], "changes", n
)==0 ){
5761 setOrClearFlag(p
, SHFLG_CountChanges
, azArg
[1]);
5763 raw_printf(stderr
, "Usage: .changes on|off\n");
5768 /* Cancel output redirection, if it is currently set (by .testcase)
5769 ** Then read the content of the testcase-out.txt file and compare against
5770 ** azArg[1]. If there are differences, report an error and exit.
5772 if( c
=='c' && n
>=3 && strncmp(azArg
[0], "check", n
)==0 ){
5776 raw_printf(stderr
, "Usage: .check GLOB-PATTERN\n");
5778 }else if( (zRes
= readFile("testcase-out.txt", 0))==0 ){
5779 raw_printf(stderr
, "Error: cannot read 'testcase-out.txt'\n");
5781 }else if( testcase_glob(azArg
[1],zRes
)==0 ){
5783 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
5784 p
->zTestcase
, azArg
[1], zRes
);
5787 utf8_printf(stdout
, "testcase-%s ok\n", p
->zTestcase
);
5793 if( c
=='c' && strncmp(azArg
[0], "clone", n
)==0 ){
5795 tryToClone(p
, azArg
[1]);
5797 raw_printf(stderr
, "Usage: .clone FILENAME\n");
5802 if( c
=='d' && n
>1 && strncmp(azArg
[0], "databases", n
)==0 ){
5806 memcpy(&data
, p
, sizeof(data
));
5807 data
.showHeader
= 0;
5808 data
.cMode
= data
.mode
= MODE_List
;
5809 sqlite3_snprintf(sizeof(data
.colSeparator
),data
.colSeparator
,": ");
5811 sqlite3_exec(p
->db
, "SELECT name, file FROM pragma_database_list",
5812 callback
, &data
, &zErrMsg
);
5814 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
5815 sqlite3_free(zErrMsg
);
5820 if( c
=='d' && n
>=3 && strncmp(azArg
[0], "dbconfig", n
)==0 ){
5821 static const struct DbConfigChoices
{const char *zName
; int op
;} aDbConfig
[] = {
5822 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY
},
5823 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER
},
5824 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
},
5825 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
},
5826 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE
},
5827 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG
},
5828 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP
},
5829 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE
},
5833 for(ii
=0; ii
<ArraySize(aDbConfig
); ii
++){
5834 if( nArg
>1 && strcmp(azArg
[1], aDbConfig
[ii
].zName
)!=0 ) continue;
5836 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, booleanValue(azArg
[2]), 0);
5838 sqlite3_db_config(p
->db
, aDbConfig
[ii
].op
, -1, &v
);
5839 utf8_printf(p
->out
, "%18s %s\n", aDbConfig
[ii
].zName
, v
? "on" : "off");
5842 if( nArg
>1 && ii
==ArraySize(aDbConfig
) ){
5843 utf8_printf(stderr
, "Error: unknown dbconfig \"%s\"\n", azArg
[1]);
5844 utf8_printf(stderr
, "Enter \".dbconfig\" with no arguments for a list\n");
5848 if( c
=='d' && n
>=3 && strncmp(azArg
[0], "dbinfo", n
)==0 ){
5849 rc
= shell_dbinfo_command(p
, nArg
, azArg
);
5852 if( c
=='d' && strncmp(azArg
[0], "dump", n
)==0 ){
5853 const char *zLike
= 0;
5855 int savedShowHeader
= p
->showHeader
;
5856 ShellClearFlag(p
, SHFLG_PreserveRowid
|SHFLG_Newlines
);
5857 for(i
=1; i
<nArg
; i
++){
5858 if( azArg
[i
][0]=='-' ){
5859 const char *z
= azArg
[i
]+1;
5860 if( z
[0]=='-' ) z
++;
5861 if( strcmp(z
,"preserve-rowids")==0 ){
5862 #ifdef SQLITE_OMIT_VIRTUALTABLE
5863 raw_printf(stderr
, "The --preserve-rowids option is not compatible"
5864 " with SQLITE_OMIT_VIRTUALTABLE\n");
5866 goto meta_command_exit
;
5868 ShellSetFlag(p
, SHFLG_PreserveRowid
);
5871 if( strcmp(z
,"newlines")==0 ){
5872 ShellSetFlag(p
, SHFLG_Newlines
);
5875 raw_printf(stderr
, "Unknown option \"%s\" on \".dump\"\n", azArg
[i
]);
5877 goto meta_command_exit
;
5880 raw_printf(stderr
, "Usage: .dump ?--preserve-rowids? "
5881 "?--newlines? ?LIKE-PATTERN?\n");
5883 goto meta_command_exit
;
5889 /* When playing back a "dump", the content might appear in an order
5890 ** which causes immediate foreign key constraints to be violated.
5891 ** So disable foreign-key constraint enforcement to prevent problems. */
5892 raw_printf(p
->out
, "PRAGMA foreign_keys=OFF;\n");
5893 raw_printf(p
->out
, "BEGIN TRANSACTION;\n");
5894 p
->writableSchema
= 0;
5896 /* Set writable_schema=ON since doing so forces SQLite to initialize
5897 ** as much of the schema as it can even if the sqlite_master table is
5899 sqlite3_exec(p
->db
, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
5902 run_schema_dump_query(p
,
5903 "SELECT name, type, sql FROM sqlite_master "
5904 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
5906 run_schema_dump_query(p
,
5907 "SELECT name, type, sql FROM sqlite_master "
5908 "WHERE name=='sqlite_sequence'"
5910 run_table_dump_query(p
,
5911 "SELECT sql FROM sqlite_master "
5912 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
5916 zSql
= sqlite3_mprintf(
5917 "SELECT name, type, sql FROM sqlite_master "
5918 "WHERE tbl_name LIKE %Q AND type=='table'"
5919 " AND sql NOT NULL", zLike
);
5920 run_schema_dump_query(p
,zSql
);
5922 zSql
= sqlite3_mprintf(
5923 "SELECT sql FROM sqlite_master "
5924 "WHERE sql NOT NULL"
5925 " AND type IN ('index','trigger','view')"
5926 " AND tbl_name LIKE %Q", zLike
);
5927 run_table_dump_query(p
, zSql
, 0);
5930 if( p
->writableSchema
){
5931 raw_printf(p
->out
, "PRAGMA writable_schema=OFF;\n");
5932 p
->writableSchema
= 0;
5934 sqlite3_exec(p
->db
, "PRAGMA writable_schema=OFF;", 0, 0, 0);
5935 sqlite3_exec(p
->db
, "RELEASE dump;", 0, 0, 0);
5936 raw_printf(p
->out
, p
->nErr
? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
5937 p
->showHeader
= savedShowHeader
;
5940 if( c
=='e' && strncmp(azArg
[0], "echo", n
)==0 ){
5942 setOrClearFlag(p
, SHFLG_Echo
, azArg
[1]);
5944 raw_printf(stderr
, "Usage: .echo on|off\n");
5949 if( c
=='e' && strncmp(azArg
[0], "eqp", n
)==0 ){
5952 if( strcmp(azArg
[1],"full")==0 ){
5953 p
->autoEQP
= AUTOEQP_full
;
5954 }else if( strcmp(azArg
[1],"trigger")==0 ){
5955 p
->autoEQP
= AUTOEQP_trigger
;
5956 }else if( strcmp(azArg
[1],"test")==0 ){
5957 p
->autoEQP
= AUTOEQP_on
;
5960 p
->autoEQP
= (u8
)booleanValue(azArg
[1]);
5963 raw_printf(stderr
, "Usage: .eqp off|on|trigger|full\n");
5968 if( c
=='e' && strncmp(azArg
[0], "exit", n
)==0 ){
5969 if( nArg
>1 && (rc
= (int)integerValue(azArg
[1]))!=0 ) exit(rc
);
5973 /* The ".explain" command is automatic now. It is largely pointless. It
5974 ** retained purely for backwards compatibility */
5975 if( c
=='e' && strncmp(azArg
[0], "explain", n
)==0 ){
5978 if( strcmp(azArg
[1],"auto")==0 ){
5981 val
= booleanValue(azArg
[1]);
5984 if( val
==1 && p
->mode
!=MODE_Explain
){
5985 p
->normalMode
= p
->mode
;
5986 p
->mode
= MODE_Explain
;
5989 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
5991 }else if( val
==99 ){
5992 if( p
->mode
==MODE_Explain
) p
->mode
= p
->normalMode
;
5997 #ifndef SQLITE_OMIT_VIRTUALTABLE
5998 if( c
=='e' && strncmp(azArg
[0], "expert", n
)==0 ){
6000 expertDotCommand(p
, azArg
, nArg
);
6004 if( c
=='f' && strncmp(azArg
[0], "fullschema", n
)==0 ){
6008 memcpy(&data
, p
, sizeof(data
));
6009 data
.showHeader
= 0;
6010 data
.cMode
= data
.mode
= MODE_Semi
;
6011 if( nArg
==2 && optionMatch(azArg
[1], "indent") ){
6012 data
.cMode
= data
.mode
= MODE_Pretty
;
6016 raw_printf(stderr
, "Usage: .fullschema ?--indent?\n");
6018 goto meta_command_exit
;
6021 rc
= sqlite3_exec(p
->db
,
6023 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
6024 " FROM sqlite_master UNION ALL"
6025 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
6026 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
6028 callback
, &data
, &zErrMsg
6030 if( rc
==SQLITE_OK
){
6031 sqlite3_stmt
*pStmt
;
6032 rc
= sqlite3_prepare_v2(p
->db
,
6033 "SELECT rowid FROM sqlite_master"
6034 " WHERE name GLOB 'sqlite_stat[134]'",
6036 doStats
= sqlite3_step(pStmt
)==SQLITE_ROW
;
6037 sqlite3_finalize(pStmt
);
6040 raw_printf(p
->out
, "/* No STAT tables available */\n");
6042 raw_printf(p
->out
, "ANALYZE sqlite_master;\n");
6043 sqlite3_exec(p
->db
, "SELECT 'ANALYZE sqlite_master'",
6044 callback
, &data
, &zErrMsg
);
6045 data
.cMode
= data
.mode
= MODE_Insert
;
6046 data
.zDestTable
= "sqlite_stat1";
6047 shell_exec(&data
, "SELECT * FROM sqlite_stat1", &zErrMsg
);
6048 data
.zDestTable
= "sqlite_stat3";
6049 shell_exec(&data
, "SELECT * FROM sqlite_stat3", &zErrMsg
);
6050 data
.zDestTable
= "sqlite_stat4";
6051 shell_exec(&data
, "SELECT * FROM sqlite_stat4", &zErrMsg
);
6052 raw_printf(p
->out
, "ANALYZE sqlite_master;\n");
6056 if( c
=='h' && strncmp(azArg
[0], "headers", n
)==0 ){
6058 p
->showHeader
= booleanValue(azArg
[1]);
6060 raw_printf(stderr
, "Usage: .headers on|off\n");
6065 if( c
=='h' && strncmp(azArg
[0], "help", n
)==0 ){
6066 utf8_printf(p
->out
, "%s", zHelp
);
6069 if( c
=='i' && strncmp(azArg
[0], "import", n
)==0 ){
6070 char *zTable
; /* Insert data into this table */
6071 char *zFile
; /* Name of file to extra content from */
6072 sqlite3_stmt
*pStmt
= NULL
; /* A statement */
6073 int nCol
; /* Number of columns in the table */
6074 int nByte
; /* Number of bytes in an SQL string */
6075 int i
, j
; /* Loop counters */
6076 int needCommit
; /* True to COMMIT or ROLLBACK at end */
6077 int nSep
; /* Number of bytes in p->colSeparator[] */
6078 char *zSql
; /* An SQL statement */
6079 ImportCtx sCtx
; /* Reader context */
6080 char *(SQLITE_CDECL
*xRead
)(ImportCtx
*); /* Func to read one value */
6081 int (SQLITE_CDECL
*xCloser
)(FILE*); /* Func to close file */
6084 raw_printf(stderr
, "Usage: .import FILE TABLE\n");
6085 goto meta_command_exit
;
6090 memset(&sCtx
, 0, sizeof(sCtx
));
6092 nSep
= strlen30(p
->colSeparator
);
6095 "Error: non-null column separator required for import\n");
6099 raw_printf(stderr
, "Error: multi-character column separators not allowed"
6103 nSep
= strlen30(p
->rowSeparator
);
6105 raw_printf(stderr
, "Error: non-null row separator required for import\n");
6108 if( nSep
==2 && p
->mode
==MODE_Csv
&& strcmp(p
->rowSeparator
, SEP_CrLf
)==0 ){
6109 /* When importing CSV (only), if the row separator is set to the
6110 ** default output row separator, change it to the default input
6111 ** row separator. This avoids having to maintain different input
6112 ** and output row separators. */
6113 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
6114 nSep
= strlen30(p
->rowSeparator
);
6117 raw_printf(stderr
, "Error: multi-character row separators not allowed"
6123 if( sCtx
.zFile
[0]=='|' ){
6124 #ifdef SQLITE_OMIT_POPEN
6125 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
6128 sCtx
.in
= popen(sCtx
.zFile
+1, "r");
6129 sCtx
.zFile
= "<pipe>";
6133 sCtx
.in
= fopen(sCtx
.zFile
, "rb");
6136 if( p
->mode
==MODE_Ascii
){
6137 xRead
= ascii_read_one_field
;
6139 xRead
= csv_read_one_field
;
6142 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zFile
);
6145 sCtx
.cColSep
= p
->colSeparator
[0];
6146 sCtx
.cRowSep
= p
->rowSeparator
[0];
6147 zSql
= sqlite3_mprintf("SELECT * FROM %s", zTable
);
6150 shell_out_of_memory();
6152 nByte
= strlen30(zSql
);
6153 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
6154 import_append_char(&sCtx
, 0); /* To ensure sCtx.z is allocated */
6155 if( rc
&& sqlite3_strglob("no such table: *", sqlite3_errmsg(p
->db
))==0 ){
6156 char *zCreate
= sqlite3_mprintf("CREATE TABLE %s", zTable
);
6158 while( xRead(&sCtx
) ){
6159 zCreate
= sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate
, cSep
, sCtx
.z
);
6161 if( sCtx
.cTerm
!=sCtx
.cColSep
) break;
6164 sqlite3_free(zCreate
);
6165 sqlite3_free(sCtx
.z
);
6167 utf8_printf(stderr
,"%s: empty file\n", sCtx
.zFile
);
6170 zCreate
= sqlite3_mprintf("%z\n)", zCreate
);
6171 rc
= sqlite3_exec(p
->db
, zCreate
, 0, 0, 0);
6172 sqlite3_free(zCreate
);
6174 utf8_printf(stderr
, "CREATE TABLE %s(...) failed: %s\n", zTable
,
6175 sqlite3_errmsg(p
->db
));
6176 sqlite3_free(sCtx
.z
);
6180 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
6184 if (pStmt
) sqlite3_finalize(pStmt
);
6185 utf8_printf(stderr
,"Error: %s\n", sqlite3_errmsg(p
->db
));
6189 nCol
= sqlite3_column_count(pStmt
);
6190 sqlite3_finalize(pStmt
);
6192 if( nCol
==0 ) return 0; /* no columns, no error */
6193 zSql
= sqlite3_malloc64( nByte
*2 + 20 + nCol
*2 );
6196 shell_out_of_memory();
6198 sqlite3_snprintf(nByte
+20, zSql
, "INSERT INTO \"%w\" VALUES(?", zTable
);
6200 for(i
=1; i
<nCol
; i
++){
6206 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
6209 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
6210 if (pStmt
) sqlite3_finalize(pStmt
);
6214 needCommit
= sqlite3_get_autocommit(p
->db
);
6215 if( needCommit
) sqlite3_exec(p
->db
, "BEGIN", 0, 0, 0);
6217 int startLine
= sCtx
.nLine
;
6218 for(i
=0; i
<nCol
; i
++){
6219 char *z
= xRead(&sCtx
);
6221 ** Did we reach end-of-file before finding any columns?
6222 ** If so, stop instead of NULL filling the remaining columns.
6224 if( z
==0 && i
==0 ) break;
6226 ** Did we reach end-of-file OR end-of-line before finding any
6227 ** columns in ASCII mode? If so, stop instead of NULL filling
6228 ** the remaining columns.
6230 if( p
->mode
==MODE_Ascii
&& (z
==0 || z
[0]==0) && i
==0 ) break;
6231 sqlite3_bind_text(pStmt
, i
+1, z
, -1, SQLITE_TRANSIENT
);
6232 if( i
<nCol
-1 && sCtx
.cTerm
!=sCtx
.cColSep
){
6233 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
6234 "filling the rest with NULL\n",
6235 sCtx
.zFile
, startLine
, nCol
, i
+1);
6237 while( i
<=nCol
){ sqlite3_bind_null(pStmt
, i
); i
++; }
6240 if( sCtx
.cTerm
==sCtx
.cColSep
){
6244 }while( sCtx
.cTerm
==sCtx
.cColSep
);
6245 utf8_printf(stderr
, "%s:%d: expected %d columns but found %d - "
6247 sCtx
.zFile
, startLine
, nCol
, i
);
6250 sqlite3_step(pStmt
);
6251 rc
= sqlite3_reset(pStmt
);
6252 if( rc
!=SQLITE_OK
){
6253 utf8_printf(stderr
, "%s:%d: INSERT failed: %s\n", sCtx
.zFile
,
6254 startLine
, sqlite3_errmsg(p
->db
));
6257 }while( sCtx
.cTerm
!=EOF
);
6260 sqlite3_free(sCtx
.z
);
6261 sqlite3_finalize(pStmt
);
6262 if( needCommit
) sqlite3_exec(p
->db
, "COMMIT", 0, 0, 0);
6265 #ifndef SQLITE_UNTESTABLE
6266 if( c
=='i' && strncmp(azArg
[0], "imposter", n
)==0 ){
6269 sqlite3_stmt
*pStmt
;
6272 if( !(nArg
==3 || (nArg
==2 && sqlite3_stricmp(azArg
[1],"off")==0)) ){
6273 utf8_printf(stderr
, "Usage: .imposter INDEX IMPOSTER\n"
6274 " .imposter off\n");
6276 goto meta_command_exit
;
6280 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 1);
6281 goto meta_command_exit
;
6283 zSql
= sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
6284 " WHERE name='%q' AND type='index'", azArg
[1]);
6285 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
6287 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
6288 tnum
= sqlite3_column_int(pStmt
, 0);
6290 sqlite3_finalize(pStmt
);
6292 utf8_printf(stderr
, "no such index: \"%s\"\n", azArg
[1]);
6294 goto meta_command_exit
;
6296 zSql
= sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg
[1]);
6297 rc
= sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
6300 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
6302 const char *zCol
= (const char*)sqlite3_column_text(pStmt
,2);
6305 if( sqlite3_column_int(pStmt
,1)==-1 ){
6308 sqlite3_snprintf(sizeof(zLabel
),zLabel
,"expr%d",i
);
6313 zCollist
= sqlite3_mprintf("\"%w\"", zCol
);
6315 zCollist
= sqlite3_mprintf("%z,\"%w\"", zCollist
, zCol
);
6318 sqlite3_finalize(pStmt
);
6319 zSql
= sqlite3_mprintf(
6320 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
6321 azArg
[2], zCollist
, zCollist
);
6322 sqlite3_free(zCollist
);
6323 rc
= sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 1, tnum
);
6324 if( rc
==SQLITE_OK
){
6325 rc
= sqlite3_exec(p
->db
, zSql
, 0, 0, 0);
6326 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER
, p
->db
, "main", 0, 0);
6328 utf8_printf(stderr
, "Error in [%s]: %s\n", zSql
, sqlite3_errmsg(p
->db
));
6330 utf8_printf(stdout
, "%s;\n", zSql
);
6332 "WARNING: writing to an imposter table will corrupt the index!\n"
6336 raw_printf(stderr
, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc
);
6341 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
6343 #ifdef SQLITE_ENABLE_IOTRACE
6344 if( c
=='i' && strncmp(azArg
[0], "iotrace", n
)==0 ){
6345 SQLITE_API
extern void (SQLITE_CDECL
*sqlite3IoTrace
)(const char*, ...);
6346 if( iotrace
&& iotrace
!=stdout
) fclose(iotrace
);
6350 }else if( strcmp(azArg
[1], "-")==0 ){
6351 sqlite3IoTrace
= iotracePrintf
;
6354 iotrace
= fopen(azArg
[1], "w");
6356 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", azArg
[1]);
6360 sqlite3IoTrace
= iotracePrintf
;
6366 if( c
=='l' && n
>=5 && strncmp(azArg
[0], "limits", n
)==0 ){
6367 static const struct {
6368 const char *zLimitName
; /* Name of a limit */
6369 int limitCode
; /* Integer code for that limit */
6371 { "length", SQLITE_LIMIT_LENGTH
},
6372 { "sql_length", SQLITE_LIMIT_SQL_LENGTH
},
6373 { "column", SQLITE_LIMIT_COLUMN
},
6374 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH
},
6375 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT
},
6376 { "vdbe_op", SQLITE_LIMIT_VDBE_OP
},
6377 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG
},
6378 { "attached", SQLITE_LIMIT_ATTACHED
},
6379 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH
},
6380 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER
},
6381 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH
},
6382 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS
},
6387 for(i
=0; i
<ArraySize(aLimit
); i
++){
6388 printf("%20s %d\n", aLimit
[i
].zLimitName
,
6389 sqlite3_limit(p
->db
, aLimit
[i
].limitCode
, -1));
6392 raw_printf(stderr
, "Usage: .limit NAME ?NEW-VALUE?\n");
6394 goto meta_command_exit
;
6397 n2
= strlen30(azArg
[1]);
6398 for(i
=0; i
<ArraySize(aLimit
); i
++){
6399 if( sqlite3_strnicmp(aLimit
[i
].zLimitName
, azArg
[1], n2
)==0 ){
6403 utf8_printf(stderr
, "ambiguous limit: \"%s\"\n", azArg
[1]);
6405 goto meta_command_exit
;
6410 utf8_printf(stderr
, "unknown limit: \"%s\"\n"
6411 "enter \".limits\" with no arguments for a list.\n",
6414 goto meta_command_exit
;
6417 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
,
6418 (int)integerValue(azArg
[2]));
6420 printf("%20s %d\n", aLimit
[iLimit
].zLimitName
,
6421 sqlite3_limit(p
->db
, aLimit
[iLimit
].limitCode
, -1));
6425 if( c
=='l' && n
>2 && strncmp(azArg
[0], "lint", n
)==0 ){
6427 lintDotCommand(p
, azArg
, nArg
);
6430 #ifndef SQLITE_OMIT_LOAD_EXTENSION
6431 if( c
=='l' && strncmp(azArg
[0], "load", n
)==0 ){
6432 const char *zFile
, *zProc
;
6435 raw_printf(stderr
, "Usage: .load FILE ?ENTRYPOINT?\n");
6437 goto meta_command_exit
;
6440 zProc
= nArg
>=3 ? azArg
[2] : 0;
6442 rc
= sqlite3_load_extension(p
->db
, zFile
, zProc
, &zErrMsg
);
6443 if( rc
!=SQLITE_OK
){
6444 utf8_printf(stderr
, "Error: %s\n", zErrMsg
);
6445 sqlite3_free(zErrMsg
);
6451 if( c
=='l' && strncmp(azArg
[0], "log", n
)==0 ){
6453 raw_printf(stderr
, "Usage: .log FILENAME\n");
6456 const char *zFile
= azArg
[1];
6457 output_file_close(p
->pLog
);
6458 p
->pLog
= output_file_open(zFile
, 0);
6462 if( c
=='m' && strncmp(azArg
[0], "mode", n
)==0 ){
6463 const char *zMode
= nArg
>=2 ? azArg
[1] : "";
6464 int n2
= strlen30(zMode
);
6466 if( c2
=='l' && n2
>2 && strncmp(azArg
[1],"lines",n2
)==0 ){
6467 p
->mode
= MODE_Line
;
6468 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
6469 }else if( c2
=='c' && strncmp(azArg
[1],"columns",n2
)==0 ){
6470 p
->mode
= MODE_Column
;
6471 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
6472 }else if( c2
=='l' && n2
>2 && strncmp(azArg
[1],"list",n2
)==0 ){
6473 p
->mode
= MODE_List
;
6474 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Column
);
6475 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
6476 }else if( c2
=='h' && strncmp(azArg
[1],"html",n2
)==0 ){
6477 p
->mode
= MODE_Html
;
6478 }else if( c2
=='t' && strncmp(azArg
[1],"tcl",n2
)==0 ){
6480 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Space
);
6481 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Row
);
6482 }else if( c2
=='c' && strncmp(azArg
[1],"csv",n2
)==0 ){
6484 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
6485 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
6486 }else if( c2
=='t' && strncmp(azArg
[1],"tabs",n2
)==0 ){
6487 p
->mode
= MODE_List
;
6488 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Tab
);
6489 }else if( c2
=='i' && strncmp(azArg
[1],"insert",n2
)==0 ){
6490 p
->mode
= MODE_Insert
;
6491 set_table_name(p
, nArg
>=3 ? azArg
[2] : "table");
6492 }else if( c2
=='q' && strncmp(azArg
[1],"quote",n2
)==0 ){
6493 p
->mode
= MODE_Quote
;
6494 }else if( c2
=='a' && strncmp(azArg
[1],"ascii",n2
)==0 ){
6495 p
->mode
= MODE_Ascii
;
6496 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Unit
);
6497 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_Record
);
6498 }else if( nArg
==1 ){
6499 raw_printf(p
->out
, "current output mode: %s\n", modeDescr
[p
->mode
]);
6501 raw_printf(stderr
, "Error: mode should be one of: "
6502 "ascii column csv html insert line list quote tabs tcl\n");
6508 if( c
=='n' && strncmp(azArg
[0], "nullvalue", n
)==0 ){
6510 sqlite3_snprintf(sizeof(p
->nullValue
), p
->nullValue
,
6511 "%.*s", (int)ArraySize(p
->nullValue
)-1, azArg
[1]);
6513 raw_printf(stderr
, "Usage: .nullvalue STRING\n");
6518 if( c
=='o' && strncmp(azArg
[0], "open", n
)==0 && n
>=2 ){
6519 char *zNewFilename
; /* Name of the database file to open */
6520 int iName
= 1; /* Index in azArg[] of the filename */
6521 int newFlag
= 0; /* True to delete file before opening */
6522 /* Close the existing database */
6523 session_close_all(p
);
6527 sqlite3_free(p
->zFreeOnClose
);
6528 p
->zFreeOnClose
= 0;
6529 p
->openMode
= SHELL_OPEN_UNSPEC
;
6530 /* Check for command-line arguments */
6531 for(iName
=1; iName
<nArg
&& azArg
[iName
][0]=='-'; iName
++){
6532 const char *z
= azArg
[iName
];
6533 if( optionMatch(z
,"new") ){
6535 #ifdef SQLITE_HAVE_ZLIB
6536 }else if( optionMatch(z
, "zip") ){
6537 p
->openMode
= SHELL_OPEN_ZIPFILE
;
6539 }else if( optionMatch(z
, "append") ){
6540 p
->openMode
= SHELL_OPEN_APPENDVFS
;
6541 }else if( optionMatch(z
, "readonly") ){
6542 p
->openMode
= SHELL_OPEN_READONLY
;
6543 }else if( z
[0]=='-' ){
6544 utf8_printf(stderr
, "unknown option: %s\n", z
);
6546 goto meta_command_exit
;
6549 /* If a filename is specified, try to open it first */
6550 zNewFilename
= nArg
>iName
? sqlite3_mprintf("%s", azArg
[iName
]) : 0;
6552 if( newFlag
) shellDeleteFile(zNewFilename
);
6553 p
->zDbFilename
= zNewFilename
;
6554 open_db(p
, OPEN_DB_KEEPALIVE
);
6556 utf8_printf(stderr
, "Error: cannot open '%s'\n", zNewFilename
);
6557 sqlite3_free(zNewFilename
);
6559 p
->zFreeOnClose
= zNewFilename
;
6563 /* As a fall-back open a TEMP database */
6570 && (strncmp(azArg
[0], "output", n
)==0||strncmp(azArg
[0], "once", n
)==0))
6571 || (c
=='e' && n
==5 && strcmp(azArg
[0],"excel")==0)
6573 const char *zFile
= nArg
>=2 ? azArg
[1] : "stdout";
6575 if( azArg
[0][0]=='e' ){
6576 /* Transform the ".excel" command into ".once -x" */
6579 zFile
= azArg
[1] = "-x";
6583 utf8_printf(stderr
, "Usage: .%s [-e|-x|FILE]\n", azArg
[0]);
6585 goto meta_command_exit
;
6587 if( n
>1 && strncmp(azArg
[0], "once", n
)==0 ){
6589 raw_printf(stderr
, "Usage: .once (-e|-x|FILE)\n");
6591 goto meta_command_exit
;
6598 if( zFile
[0]=='-' && zFile
[1]=='-' ) zFile
++;
6599 #ifndef SQLITE_NOHAVE_SYSTEM
6600 if( strcmp(zFile
, "-e")==0 || strcmp(zFile
, "-x")==0 ){
6603 if( zFile
[1]=='x' ){
6604 newTempFile(p
, "csv");
6606 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
, SEP_Comma
);
6607 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
, SEP_CrLf
);
6609 newTempFile(p
, "txt");
6612 zFile
= p
->zTempFile
;
6614 #endif /* SQLITE_NOHAVE_SYSTEM */
6615 if( zFile
[0]=='|' ){
6616 #ifdef SQLITE_OMIT_POPEN
6617 raw_printf(stderr
, "Error: pipes are not supported in this OS\n");
6621 p
->out
= popen(zFile
+ 1, "w");
6623 utf8_printf(stderr
,"Error: cannot open pipe \"%s\"\n", zFile
+ 1);
6627 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
6631 p
->out
= output_file_open(zFile
, bTxtMode
);
6633 if( strcmp(zFile
,"off")!=0 ){
6634 utf8_printf(stderr
,"Error: cannot write to \"%s\"\n", zFile
);
6639 sqlite3_snprintf(sizeof(p
->outfile
), p
->outfile
, "%s", zFile
);
6644 if( c
=='p' && n
>=3 && strncmp(azArg
[0], "print", n
)==0 ){
6646 for(i
=1; i
<nArg
; i
++){
6647 if( i
>1 ) raw_printf(p
->out
, " ");
6648 utf8_printf(p
->out
, "%s", azArg
[i
]);
6650 raw_printf(p
->out
, "\n");
6653 if( c
=='p' && strncmp(azArg
[0], "prompt", n
)==0 ){
6655 strncpy(mainPrompt
,azArg
[1],(int)ArraySize(mainPrompt
)-1);
6658 strncpy(continuePrompt
,azArg
[2],(int)ArraySize(continuePrompt
)-1);
6662 if( c
=='q' && strncmp(azArg
[0], "quit", n
)==0 ){
6666 if( c
=='r' && n
>=3 && strncmp(azArg
[0], "read", n
)==0 ){
6669 raw_printf(stderr
, "Usage: .read FILE\n");
6671 goto meta_command_exit
;
6673 alt
= fopen(azArg
[1], "rb");
6675 utf8_printf(stderr
,"Error: cannot open \"%s\"\n", azArg
[1]);
6678 rc
= process_input(p
, alt
);
6683 if( c
=='r' && n
>=3 && strncmp(azArg
[0], "restore", n
)==0 ){
6684 const char *zSrcFile
;
6687 sqlite3_backup
*pBackup
;
6691 zSrcFile
= azArg
[1];
6693 }else if( nArg
==3 ){
6694 zSrcFile
= azArg
[2];
6697 raw_printf(stderr
, "Usage: .restore ?DB? FILE\n");
6699 goto meta_command_exit
;
6701 rc
= sqlite3_open(zSrcFile
, &pSrc
);
6702 if( rc
!=SQLITE_OK
){
6703 utf8_printf(stderr
, "Error: cannot open \"%s\"\n", zSrcFile
);
6708 pBackup
= sqlite3_backup_init(p
->db
, zDb
, pSrc
, "main");
6710 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
6714 while( (rc
= sqlite3_backup_step(pBackup
,100))==SQLITE_OK
6715 || rc
==SQLITE_BUSY
){
6716 if( rc
==SQLITE_BUSY
){
6717 if( nTimeout
++ >= 3 ) break;
6721 sqlite3_backup_finish(pBackup
);
6722 if( rc
==SQLITE_DONE
){
6724 }else if( rc
==SQLITE_BUSY
|| rc
==SQLITE_LOCKED
){
6725 raw_printf(stderr
, "Error: source database is busy\n");
6728 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
6734 if( c
=='s' && strncmp(azArg
[0], "scanstats", n
)==0 ){
6736 p
->scanstatsOn
= (u8
)booleanValue(azArg
[1]);
6737 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
6738 raw_printf(stderr
, "Warning: .scanstats not available in this build.\n");
6741 raw_printf(stderr
, "Usage: .scanstats on|off\n");
6746 if( c
=='s' && strncmp(azArg
[0], "schema", n
)==0 ){
6750 const char *zDiv
= "(";
6751 const char *zName
= 0;
6757 memcpy(&data
, p
, sizeof(data
));
6758 data
.showHeader
= 0;
6759 data
.cMode
= data
.mode
= MODE_Semi
;
6761 for(ii
=1; ii
<nArg
; ii
++){
6762 if( optionMatch(azArg
[ii
],"indent") ){
6763 data
.cMode
= data
.mode
= MODE_Pretty
;
6764 }else if( optionMatch(azArg
[ii
],"debug") ){
6766 }else if( zName
==0 ){
6769 raw_printf(stderr
, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
6771 goto meta_command_exit
;
6775 int isMaster
= sqlite3_strlike(zName
, "sqlite_master", '\\')==0;
6776 if( isMaster
|| sqlite3_strlike(zName
,"sqlite_temp_master", '\\')==0 ){
6777 char *new_argv
[2], *new_colv
[2];
6778 new_argv
[0] = sqlite3_mprintf(
6779 "CREATE TABLE %s (\n"
6783 " rootpage integer,\n"
6785 ")", isMaster
? "sqlite_master" : "sqlite_temp_master");
6787 new_colv
[0] = "sql";
6789 callback(&data
, 1, new_argv
, new_colv
);
6790 sqlite3_free(new_argv
[0]);
6794 sqlite3_stmt
*pStmt
= 0;
6795 rc
= sqlite3_prepare_v2(p
->db
, "SELECT name FROM pragma_database_list",
6798 utf8_printf(stderr
, "Error: %s\n", sqlite3_errmsg(p
->db
));
6799 sqlite3_finalize(pStmt
);
6801 goto meta_command_exit
;
6803 appendText(&sSelect
, "SELECT sql FROM", 0);
6805 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
6806 const char *zDb
= (const char*)sqlite3_column_text(pStmt
, 0);
6808 sqlite3_snprintf(sizeof(zScNum
), zScNum
, "%d", ++iSchema
);
6809 appendText(&sSelect
, zDiv
, 0);
6810 zDiv
= " UNION ALL ";
6811 appendText(&sSelect
, "SELECT shell_add_schema(sql,", 0);
6812 if( sqlite3_stricmp(zDb
, "main")!=0 ){
6813 appendText(&sSelect
, zDb
, '"');
6815 appendText(&sSelect
, "NULL", 0);
6817 appendText(&sSelect
, ",name) AS sql, type, tbl_name, name, rowid,", 0);
6818 appendText(&sSelect
, zScNum
, 0);
6819 appendText(&sSelect
, " AS snum, ", 0);
6820 appendText(&sSelect
, zDb
, '\'');
6821 appendText(&sSelect
, " AS sname FROM ", 0);
6822 appendText(&sSelect
, zDb
, '"');
6823 appendText(&sSelect
, ".sqlite_master", 0);
6825 sqlite3_finalize(pStmt
);
6826 #ifdef SQLITE_INTROSPECTION_PRAGMAS
6828 appendText(&sSelect
,
6829 " UNION ALL SELECT shell_module_schema(name),"
6830 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
6833 appendText(&sSelect
, ") WHERE ", 0);
6835 char *zQarg
= sqlite3_mprintf("%Q", zName
);
6836 int bGlob
= strchr(zName
, '*') != 0 || strchr(zName
, '?') != 0 ||
6837 strchr(zName
, '[') != 0;
6838 if( strchr(zName
, '.') ){
6839 appendText(&sSelect
, "lower(printf('%s.%s',sname,tbl_name))", 0);
6841 appendText(&sSelect
, "lower(tbl_name)", 0);
6843 appendText(&sSelect
, bGlob
? " GLOB " : " LIKE ", 0);
6844 appendText(&sSelect
, zQarg
, 0);
6846 appendText(&sSelect
, " ESCAPE '\\' ", 0);
6848 appendText(&sSelect
, " AND ", 0);
6849 sqlite3_free(zQarg
);
6851 appendText(&sSelect
, "type!='meta' AND sql IS NOT NULL"
6852 " ORDER BY snum, rowid", 0);
6854 utf8_printf(p
->out
, "SQL: %s;\n", sSelect
.z
);
6856 rc
= sqlite3_exec(p
->db
, sSelect
.z
, callback
, &data
, &zErrMsg
);
6861 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
6862 sqlite3_free(zErrMsg
);
6864 }else if( rc
!= SQLITE_OK
){
6865 raw_printf(stderr
,"Error: querying schema information\n");
6872 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
6873 if( c
=='s' && n
==11 && strncmp(azArg
[0], "selecttrace", n
)==0 ){
6874 sqlite3SelectTrace
= (int)integerValue(azArg
[1]);
6878 #if defined(SQLITE_ENABLE_SESSION)
6879 if( c
=='s' && strncmp(azArg
[0],"session",n
)==0 && n
>=3 ){
6880 OpenSession
*pSession
= &p
->aSession
[0];
6881 char **azCmd
= &azArg
[1];
6883 int nCmd
= nArg
- 1;
6885 if( nArg
<=1 ) goto session_syntax_error
;
6888 for(iSes
=0; iSes
<p
->nSession
; iSes
++){
6889 if( strcmp(p
->aSession
[iSes
].zName
, azArg
[1])==0 ) break;
6891 if( iSes
<p
->nSession
){
6892 pSession
= &p
->aSession
[iSes
];
6896 pSession
= &p
->aSession
[0];
6901 /* .session attach TABLE
6902 ** Invoke the sqlite3session_attach() interface to attach a particular
6903 ** table so that it is never filtered.
6905 if( strcmp(azCmd
[0],"attach")==0 ){
6906 if( nCmd
!=2 ) goto session_syntax_error
;
6907 if( pSession
->p
==0 ){
6909 raw_printf(stderr
, "ERROR: No sessions are open\n");
6911 rc
= sqlite3session_attach(pSession
->p
, azCmd
[1]);
6913 raw_printf(stderr
, "ERROR: sqlite3session_attach() returns %d\n", rc
);
6919 /* .session changeset FILE
6920 ** .session patchset FILE
6921 ** Write a changeset or patchset into a file. The file is overwritten.
6923 if( strcmp(azCmd
[0],"changeset")==0 || strcmp(azCmd
[0],"patchset")==0 ){
6925 if( nCmd
!=2 ) goto session_syntax_error
;
6926 if( pSession
->p
==0 ) goto session_not_open
;
6927 out
= fopen(azCmd
[1], "wb");
6929 utf8_printf(stderr
, "ERROR: cannot open \"%s\" for writing\n", azCmd
[1]);
6933 if( azCmd
[0][0]=='c' ){
6934 rc
= sqlite3session_changeset(pSession
->p
, &szChng
, &pChng
);
6936 rc
= sqlite3session_patchset(pSession
->p
, &szChng
, &pChng
);
6939 printf("Error: error code %d\n", rc
);
6943 && fwrite(pChng
, szChng
, 1, out
)!=1 ){
6944 raw_printf(stderr
, "ERROR: Failed to write entire %d-byte output\n",
6947 sqlite3_free(pChng
);
6953 ** Close the identified session
6955 if( strcmp(azCmd
[0], "close")==0 ){
6956 if( nCmd
!=1 ) goto session_syntax_error
;
6958 session_close(pSession
);
6959 p
->aSession
[iSes
] = p
->aSession
[--p
->nSession
];
6963 /* .session enable ?BOOLEAN?
6964 ** Query or set the enable flag
6966 if( strcmp(azCmd
[0], "enable")==0 ){
6968 if( nCmd
>2 ) goto session_syntax_error
;
6969 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
6971 ii
= sqlite3session_enable(pSession
->p
, ii
);
6972 utf8_printf(p
->out
, "session %s enable flag = %d\n",
6973 pSession
->zName
, ii
);
6977 /* .session filter GLOB ....
6978 ** Set a list of GLOB patterns of table names to be excluded.
6980 if( strcmp(azCmd
[0], "filter")==0 ){
6982 if( nCmd
<2 ) goto session_syntax_error
;
6984 for(ii
=0; ii
<pSession
->nFilter
; ii
++){
6985 sqlite3_free(pSession
->azFilter
[ii
]);
6987 sqlite3_free(pSession
->azFilter
);
6988 nByte
= sizeof(pSession
->azFilter
[0])*(nCmd
-1);
6989 pSession
->azFilter
= sqlite3_malloc( nByte
);
6990 if( pSession
->azFilter
==0 ){
6991 raw_printf(stderr
, "Error: out or memory\n");
6994 for(ii
=1; ii
<nCmd
; ii
++){
6995 pSession
->azFilter
[ii
-1] = sqlite3_mprintf("%s", azCmd
[ii
]);
6997 pSession
->nFilter
= ii
-1;
7001 /* .session indirect ?BOOLEAN?
7002 ** Query or set the indirect flag
7004 if( strcmp(azCmd
[0], "indirect")==0 ){
7006 if( nCmd
>2 ) goto session_syntax_error
;
7007 ii
= nCmd
==1 ? -1 : booleanValue(azCmd
[1]);
7009 ii
= sqlite3session_indirect(pSession
->p
, ii
);
7010 utf8_printf(p
->out
, "session %s indirect flag = %d\n",
7011 pSession
->zName
, ii
);
7016 ** Determine if the session is empty
7018 if( strcmp(azCmd
[0], "isempty")==0 ){
7020 if( nCmd
!=1 ) goto session_syntax_error
;
7022 ii
= sqlite3session_isempty(pSession
->p
);
7023 utf8_printf(p
->out
, "session %s isempty flag = %d\n",
7024 pSession
->zName
, ii
);
7029 ** List all currently open sessions
7031 if( strcmp(azCmd
[0],"list")==0 ){
7032 for(i
=0; i
<p
->nSession
; i
++){
7033 utf8_printf(p
->out
, "%d %s\n", i
, p
->aSession
[i
].zName
);
7037 /* .session open DB NAME
7038 ** Open a new session called NAME on the attached database DB.
7039 ** DB is normally "main".
7041 if( strcmp(azCmd
[0],"open")==0 ){
7043 if( nCmd
!=3 ) goto session_syntax_error
;
7045 if( zName
[0]==0 ) goto session_syntax_error
;
7046 for(i
=0; i
<p
->nSession
; i
++){
7047 if( strcmp(p
->aSession
[i
].zName
,zName
)==0 ){
7048 utf8_printf(stderr
, "Session \"%s\" already exists\n", zName
);
7049 goto meta_command_exit
;
7052 if( p
->nSession
>=ArraySize(p
->aSession
) ){
7053 raw_printf(stderr
, "Maximum of %d sessions\n", ArraySize(p
->aSession
));
7054 goto meta_command_exit
;
7056 pSession
= &p
->aSession
[p
->nSession
];
7057 rc
= sqlite3session_create(p
->db
, azCmd
[1], &pSession
->p
);
7059 raw_printf(stderr
, "Cannot open session: error code=%d\n", rc
);
7061 goto meta_command_exit
;
7063 pSession
->nFilter
= 0;
7064 sqlite3session_table_filter(pSession
->p
, session_filter
, pSession
);
7066 pSession
->zName
= sqlite3_mprintf("%s", zName
);
7068 /* If no command name matches, show a syntax error */
7069 session_syntax_error
:
7075 /* Undocumented commands for internal testing. Subject to change
7076 ** without notice. */
7077 if( c
=='s' && n
>=10 && strncmp(azArg
[0], "selftest-", 9)==0 ){
7078 if( strncmp(azArg
[0]+9, "boolean", n
-9)==0 ){
7080 for(i
=1; i
<nArg
; i
++){
7081 v
= booleanValue(azArg
[i
]);
7082 utf8_printf(p
->out
, "%s: %d 0x%x\n", azArg
[i
], v
, v
);
7085 if( strncmp(azArg
[0]+9, "integer", n
-9)==0 ){
7086 int i
; sqlite3_int64 v
;
7087 for(i
=1; i
<nArg
; i
++){
7089 v
= integerValue(azArg
[i
]);
7090 sqlite3_snprintf(sizeof(zBuf
),zBuf
,"%s: %lld 0x%llx\n", azArg
[i
],v
,v
);
7091 utf8_printf(p
->out
, "%s", zBuf
);
7097 if( c
=='s' && n
>=4 && strncmp(azArg
[0],"selftest",n
)==0 ){
7098 int bIsInit
= 0; /* True to initialize the SELFTEST table */
7099 int bVerbose
= 0; /* Verbose output */
7100 int bSelftestExists
; /* True if SELFTEST already exists */
7101 int i
, k
; /* Loop counters */
7102 int nTest
= 0; /* Number of tests runs */
7103 int nErr
= 0; /* Number of errors seen */
7104 ShellText str
; /* Answer for a query */
7105 sqlite3_stmt
*pStmt
= 0; /* Query against the SELFTEST table */
7108 for(i
=1; i
<nArg
; i
++){
7109 const char *z
= azArg
[i
];
7110 if( z
[0]=='-' && z
[1]=='-' ) z
++;
7111 if( strcmp(z
,"-init")==0 ){
7114 if( strcmp(z
,"-v")==0 ){
7118 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
7119 azArg
[i
], azArg
[0]);
7120 raw_printf(stderr
, "Should be one of: --init -v\n");
7122 goto meta_command_exit
;
7125 if( sqlite3_table_column_metadata(p
->db
,"main","selftest",0,0,0,0,0,0)
7127 bSelftestExists
= 0;
7129 bSelftestExists
= 1;
7132 createSelftestTable(p
);
7133 bSelftestExists
= 1;
7136 appendText(&str
, "x", 0);
7137 for(k
=bSelftestExists
; k
>=0; k
--){
7139 rc
= sqlite3_prepare_v2(p
->db
,
7140 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
7143 rc
= sqlite3_prepare_v2(p
->db
,
7144 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
7145 " (1,'run','PRAGMA integrity_check','ok')",
7149 raw_printf(stderr
, "Error querying the selftest table\n");
7151 sqlite3_finalize(pStmt
);
7152 goto meta_command_exit
;
7154 for(i
=1; sqlite3_step(pStmt
)==SQLITE_ROW
; i
++){
7155 int tno
= sqlite3_column_int(pStmt
, 0);
7156 const char *zOp
= (const char*)sqlite3_column_text(pStmt
, 1);
7157 const char *zSql
= (const char*)sqlite3_column_text(pStmt
, 2);
7158 const char *zAns
= (const char*)sqlite3_column_text(pStmt
, 3);
7162 char *zQuote
= sqlite3_mprintf("%q", zSql
);
7163 printf("%d: %s %s\n", tno
, zOp
, zSql
);
7164 sqlite3_free(zQuote
);
7166 if( strcmp(zOp
,"memo")==0 ){
7167 utf8_printf(p
->out
, "%s\n", zSql
);
7169 if( strcmp(zOp
,"run")==0 ){
7173 rc
= sqlite3_exec(p
->db
, zSql
, captureOutputCallback
, &str
, &zErrMsg
);
7176 utf8_printf(p
->out
, "Result: %s\n", str
.z
);
7178 if( rc
|| zErrMsg
){
7181 utf8_printf(p
->out
, "%d: error-code-%d: %s\n", tno
, rc
, zErrMsg
);
7182 sqlite3_free(zErrMsg
);
7183 }else if( strcmp(zAns
,str
.z
)!=0 ){
7186 utf8_printf(p
->out
, "%d: Expected: [%s]\n", tno
, zAns
);
7187 utf8_printf(p
->out
, "%d: Got: [%s]\n", tno
, str
.z
);
7192 "Unknown operation \"%s\" on selftest line %d\n", zOp
, tno
);
7196 } /* End loop over rows of content from SELFTEST */
7197 sqlite3_finalize(pStmt
);
7198 } /* End loop over k */
7200 utf8_printf(p
->out
, "%d errors out of %d tests\n", nErr
, nTest
);
7203 if( c
=='s' && strncmp(azArg
[0], "separator", n
)==0 ){
7204 if( nArg
<2 || nArg
>3 ){
7205 raw_printf(stderr
, "Usage: .separator COL ?ROW?\n");
7209 sqlite3_snprintf(sizeof(p
->colSeparator
), p
->colSeparator
,
7210 "%.*s", (int)ArraySize(p
->colSeparator
)-1, azArg
[1]);
7213 sqlite3_snprintf(sizeof(p
->rowSeparator
), p
->rowSeparator
,
7214 "%.*s", (int)ArraySize(p
->rowSeparator
)-1, azArg
[2]);
7218 if( c
=='s' && n
>=4 && strncmp(azArg
[0],"sha3sum",n
)==0 ){
7219 const char *zLike
= 0; /* Which table to checksum. 0 means everything */
7220 int i
; /* Loop counter */
7221 int bSchema
= 0; /* Also hash the schema */
7222 int bSeparate
= 0; /* Hash each table separately */
7223 int iSize
= 224; /* Hash algorithm to use */
7224 int bDebug
= 0; /* Only show the query that would have run */
7225 sqlite3_stmt
*pStmt
; /* For querying tables names */
7226 char *zSql
; /* SQL to be run */
7227 char *zSep
; /* Separator */
7228 ShellText sSql
; /* Complete SQL for the query to run the hash */
7229 ShellText sQuery
; /* Set of queries used to read all content */
7231 for(i
=1; i
<nArg
; i
++){
7232 const char *z
= azArg
[i
];
7235 if( z
[0]=='-' ) z
++;
7236 if( strcmp(z
,"schema")==0 ){
7239 if( strcmp(z
,"sha3-224")==0 || strcmp(z
,"sha3-256")==0
7240 || strcmp(z
,"sha3-384")==0 || strcmp(z
,"sha3-512")==0
7242 iSize
= atoi(&z
[5]);
7244 if( strcmp(z
,"debug")==0 ){
7248 utf8_printf(stderr
, "Unknown option \"%s\" on \"%s\"\n",
7249 azArg
[i
], azArg
[0]);
7250 raw_printf(stderr
, "Should be one of: --schema"
7251 " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
7253 goto meta_command_exit
;
7256 raw_printf(stderr
, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
7258 goto meta_command_exit
;
7262 if( sqlite3_strlike("sqlite\\_%", zLike
, '\\')==0 ) bSchema
= 1;
7266 zSql
= "SELECT lower(name) FROM sqlite_master"
7267 " WHERE type='table' AND coalesce(rootpage,0)>1"
7268 " UNION ALL SELECT 'sqlite_master'"
7269 " ORDER BY 1 collate nocase";
7271 zSql
= "SELECT lower(name) FROM sqlite_master"
7272 " WHERE type='table' AND coalesce(rootpage,0)>1"
7273 " AND name NOT LIKE 'sqlite_%'"
7274 " ORDER BY 1 collate nocase";
7276 sqlite3_prepare_v2(p
->db
, zSql
, -1, &pStmt
, 0);
7279 appendText(&sSql
, "WITH [sha3sum$query](a,b) AS(",0);
7281 while( SQLITE_ROW
==sqlite3_step(pStmt
) ){
7282 const char *zTab
= (const char*)sqlite3_column_text(pStmt
,0);
7283 if( zLike
&& sqlite3_strlike(zLike
, zTab
, 0)!=0 ) continue;
7284 if( strncmp(zTab
, "sqlite_",7)!=0 ){
7285 appendText(&sQuery
,"SELECT * FROM ", 0);
7286 appendText(&sQuery
,zTab
,'"');
7287 appendText(&sQuery
," NOT INDEXED;", 0);
7288 }else if( strcmp(zTab
, "sqlite_master")==0 ){
7289 appendText(&sQuery
,"SELECT type,name,tbl_name,sql FROM sqlite_master"
7290 " ORDER BY name;", 0);
7291 }else if( strcmp(zTab
, "sqlite_sequence")==0 ){
7292 appendText(&sQuery
,"SELECT name,seq FROM sqlite_sequence"
7293 " ORDER BY name;", 0);
7294 }else if( strcmp(zTab
, "sqlite_stat1")==0 ){
7295 appendText(&sQuery
,"SELECT tbl,idx,stat FROM sqlite_stat1"
7296 " ORDER BY tbl,idx;", 0);
7297 }else if( strcmp(zTab
, "sqlite_stat3")==0
7298 || strcmp(zTab
, "sqlite_stat4")==0 ){
7299 appendText(&sQuery
, "SELECT * FROM ", 0);
7300 appendText(&sQuery
, zTab
, 0);
7301 appendText(&sQuery
, " ORDER BY tbl, idx, rowid;\n", 0);
7303 appendText(&sSql
, zSep
, 0);
7304 appendText(&sSql
, sQuery
.z
, '\'');
7306 appendText(&sSql
, ",", 0);
7307 appendText(&sSql
, zTab
, '\'');
7310 sqlite3_finalize(pStmt
);
7312 zSql
= sqlite3_mprintf(
7314 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
7315 " FROM [sha3sum$query]",
7318 zSql
= sqlite3_mprintf(
7320 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
7321 " FROM [sha3sum$query]",
7327 utf8_printf(p
->out
, "%s\n", zSql
);
7329 shell_exec(p
, zSql
, 0);
7334 #ifndef SQLITE_NOHAVE_SYSTEM
7336 && (strncmp(azArg
[0], "shell", n
)==0 || strncmp(azArg
[0],"system",n
)==0)
7341 raw_printf(stderr
, "Usage: .system COMMAND\n");
7343 goto meta_command_exit
;
7345 zCmd
= sqlite3_mprintf(strchr(azArg
[1],' ')==0?"%s":"\"%s\"", azArg
[1]);
7346 for(i
=2; i
<nArg
; i
++){
7347 zCmd
= sqlite3_mprintf(strchr(azArg
[i
],' ')==0?"%z %s":"%z \"%s\"",
7352 if( x
) raw_printf(stderr
, "System command returns %d\n", x
);
7354 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
7356 if( c
=='s' && strncmp(azArg
[0], "show", n
)==0 ){
7357 static const char *azBool
[] = { "off", "on", "trigger", "full"};
7360 raw_printf(stderr
, "Usage: .show\n");
7362 goto meta_command_exit
;
7364 utf8_printf(p
->out
, "%12.12s: %s\n","echo",
7365 azBool
[ShellHasFlag(p
, SHFLG_Echo
)]);
7366 utf8_printf(p
->out
, "%12.12s: %s\n","eqp", azBool
[p
->autoEQP
&3]);
7367 utf8_printf(p
->out
, "%12.12s: %s\n","explain",
7368 p
->mode
==MODE_Explain
? "on" : p
->autoExplain
? "auto" : "off");
7369 utf8_printf(p
->out
,"%12.12s: %s\n","headers", azBool
[p
->showHeader
!=0]);
7370 utf8_printf(p
->out
, "%12.12s: %s\n","mode", modeDescr
[p
->mode
]);
7371 utf8_printf(p
->out
, "%12.12s: ", "nullvalue");
7372 output_c_string(p
->out
, p
->nullValue
);
7373 raw_printf(p
->out
, "\n");
7374 utf8_printf(p
->out
,"%12.12s: %s\n","output",
7375 strlen30(p
->outfile
) ? p
->outfile
: "stdout");
7376 utf8_printf(p
->out
,"%12.12s: ", "colseparator");
7377 output_c_string(p
->out
, p
->colSeparator
);
7378 raw_printf(p
->out
, "\n");
7379 utf8_printf(p
->out
,"%12.12s: ", "rowseparator");
7380 output_c_string(p
->out
, p
->rowSeparator
);
7381 raw_printf(p
->out
, "\n");
7382 utf8_printf(p
->out
, "%12.12s: %s\n","stats", azBool
[p
->statsOn
!=0]);
7383 utf8_printf(p
->out
, "%12.12s: ", "width");
7384 for (i
=0;i
<(int)ArraySize(p
->colWidth
) && p
->colWidth
[i
] != 0;i
++) {
7385 raw_printf(p
->out
, "%d ", p
->colWidth
[i
]);
7387 raw_printf(p
->out
, "\n");
7388 utf8_printf(p
->out
, "%12.12s: %s\n", "filename",
7389 p
->zDbFilename
? p
->zDbFilename
: "");
7392 if( c
=='s' && strncmp(azArg
[0], "stats", n
)==0 ){
7394 p
->statsOn
= (u8
)booleanValue(azArg
[1]);
7395 }else if( nArg
==1 ){
7396 display_stats(p
->db
, p
, 0);
7398 raw_printf(stderr
, "Usage: .stats ?on|off?\n");
7403 if( (c
=='t' && n
>1 && strncmp(azArg
[0], "tables", n
)==0)
7404 || (c
=='i' && (strncmp(azArg
[0], "indices", n
)==0
7405 || strncmp(azArg
[0], "indexes", n
)==0) )
7407 sqlite3_stmt
*pStmt
;
7414 rc
= sqlite3_prepare_v2(p
->db
, "PRAGMA database_list", -1, &pStmt
, 0);
7416 sqlite3_finalize(pStmt
);
7417 return shellDatabaseError(p
->db
);
7420 if( nArg
>2 && c
=='i' ){
7421 /* It is an historical accident that the .indexes command shows an error
7422 ** when called with the wrong number of arguments whereas the .tables
7423 ** command does not. */
7424 raw_printf(stderr
, "Usage: .indexes ?LIKE-PATTERN?\n");
7426 sqlite3_finalize(pStmt
);
7427 goto meta_command_exit
;
7429 for(ii
=0; sqlite3_step(pStmt
)==SQLITE_ROW
; ii
++){
7430 const char *zDbName
= (const char*)sqlite3_column_text(pStmt
, 1);
7431 if( zDbName
==0 ) continue;
7432 if( s
.z
&& s
.z
[0] ) appendText(&s
, " UNION ALL ", 0);
7433 if( sqlite3_stricmp(zDbName
, "main")==0 ){
7434 appendText(&s
, "SELECT name FROM ", 0);
7436 appendText(&s
, "SELECT ", 0);
7437 appendText(&s
, zDbName
, '\'');
7438 appendText(&s
, "||'.'||name FROM ", 0);
7440 appendText(&s
, zDbName
, '"');
7441 appendText(&s
, ".sqlite_master ", 0);
7443 appendText(&s
," WHERE type IN ('table','view')"
7444 " AND name NOT LIKE 'sqlite_%'"
7445 " AND name LIKE ?1", 0);
7447 appendText(&s
," WHERE type='index'"
7448 " AND tbl_name LIKE ?1", 0);
7451 rc
= sqlite3_finalize(pStmt
);
7452 appendText(&s
, " ORDER BY 1", 0);
7453 rc
= sqlite3_prepare_v2(p
->db
, s
.z
, -1, &pStmt
, 0);
7455 if( rc
) return shellDatabaseError(p
->db
);
7457 /* Run the SQL statement prepared by the above block. Store the results
7458 ** as an array of nul-terminated strings in azResult[]. */
7462 sqlite3_bind_text(pStmt
, 1, azArg
[1], -1, SQLITE_TRANSIENT
);
7464 sqlite3_bind_text(pStmt
, 1, "%", -1, SQLITE_STATIC
);
7466 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
7469 int n2
= nAlloc
*2 + 10;
7470 azNew
= sqlite3_realloc64(azResult
, sizeof(azResult
[0])*n2
);
7471 if( azNew
==0 ) shell_out_of_memory();
7475 azResult
[nRow
] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
7476 if( 0==azResult
[nRow
] ) shell_out_of_memory();
7479 if( sqlite3_finalize(pStmt
)!=SQLITE_OK
){
7480 rc
= shellDatabaseError(p
->db
);
7483 /* Pretty-print the contents of array azResult[] to the output */
7484 if( rc
==0 && nRow
>0 ){
7485 int len
, maxlen
= 0;
7487 int nPrintCol
, nPrintRow
;
7488 for(i
=0; i
<nRow
; i
++){
7489 len
= strlen30(azResult
[i
]);
7490 if( len
>maxlen
) maxlen
= len
;
7492 nPrintCol
= 80/(maxlen
+2);
7493 if( nPrintCol
<1 ) nPrintCol
= 1;
7494 nPrintRow
= (nRow
+ nPrintCol
- 1)/nPrintCol
;
7495 for(i
=0; i
<nPrintRow
; i
++){
7496 for(j
=i
; j
<nRow
; j
+=nPrintRow
){
7497 char *zSp
= j
<nPrintRow
? "" : " ";
7498 utf8_printf(p
->out
, "%s%-*s", zSp
, maxlen
,
7499 azResult
[j
] ? azResult
[j
]:"");
7501 raw_printf(p
->out
, "\n");
7505 for(ii
=0; ii
<nRow
; ii
++) sqlite3_free(azResult
[ii
]);
7506 sqlite3_free(azResult
);
7509 /* Begin redirecting output to the file "testcase-out.txt" */
7510 if( c
=='t' && strcmp(azArg
[0],"testcase")==0 ){
7512 p
->out
= output_file_open("testcase-out.txt", 0);
7514 raw_printf(stderr
, "Error: cannot open 'testcase-out.txt'\n");
7517 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "%s", azArg
[1]);
7519 sqlite3_snprintf(sizeof(p
->zTestcase
), p
->zTestcase
, "?");
7523 #ifndef SQLITE_UNTESTABLE
7524 if( c
=='t' && n
>=8 && strncmp(azArg
[0], "testctrl", n
)==0 ){
7525 static const struct {
7526 const char *zCtrlName
; /* Name of a test-control option */
7527 int ctrlCode
; /* Integer code for that option */
7528 const char *zUsage
; /* Usage notes */
7530 { "always", SQLITE_TESTCTRL_ALWAYS
, "BOOLEAN" },
7531 { "assert", SQLITE_TESTCTRL_ASSERT
, "BOOLEAN" },
7532 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
7533 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
7534 { "byteorder", SQLITE_TESTCTRL_BYTEORDER
, "" },
7535 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
7536 { "imposter", SQLITE_TESTCTRL_IMPOSTER
, "SCHEMA ON/OFF ROOTPAGE"},
7537 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT
,"BOOLEAN" },
7538 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT
, "BOOLEAN" },
7539 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS
, "DISABLE-MASK" },
7541 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE
, "" },
7543 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE
, "OFFSET " },
7544 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET
, "" },
7545 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE
, "" },
7546 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE
, "" },
7547 { "reserve", SQLITE_TESTCTRL_RESERVE
, "BYTES-OF-RESERVE" },
7551 int rc2
= 0; /* 0: usage. 1: %d 2: %x 3: no-output */
7554 const char *zCmd
= 0;
7557 zCmd
= nArg
>=2 ? azArg
[1] : "help";
7559 /* The argument can optionally begin with "-" or "--" */
7560 if( zCmd
[0]=='-' && zCmd
[1] ){
7562 if( zCmd
[0]=='-' && zCmd
[1] ) zCmd
++;
7565 /* --help lists all test-controls */
7566 if( strcmp(zCmd
,"help")==0 ){
7567 utf8_printf(p
->out
, "Available test-controls:\n");
7568 for(i
=0; i
<ArraySize(aCtrl
); i
++){
7569 utf8_printf(p
->out
, " .testctrl %s %s\n",
7570 aCtrl
[i
].zCtrlName
, aCtrl
[i
].zUsage
);
7573 goto meta_command_exit
;
7576 /* convert testctrl text option to value. allow any unique prefix
7577 ** of the option name, or a numerical value. */
7578 n2
= strlen30(zCmd
);
7579 for(i
=0; i
<ArraySize(aCtrl
); i
++){
7580 if( strncmp(zCmd
, aCtrl
[i
].zCtrlName
, n2
)==0 ){
7582 testctrl
= aCtrl
[i
].ctrlCode
;
7585 utf8_printf(stderr
, "Error: ambiguous test-control: \"%s\"\n"
7586 "Use \".testctrl --help\" for help\n", zCmd
);
7588 goto meta_command_exit
;
7593 utf8_printf(stderr
,"Error: unknown test-control: %s\n"
7594 "Use \".testctrl --help\" for help\n", zCmd
);
7598 /* sqlite3_test_control(int, db, int) */
7599 case SQLITE_TESTCTRL_OPTIMIZATIONS
:
7600 case SQLITE_TESTCTRL_RESERVE
:
7602 int opt
= (int)strtol(azArg
[2], 0, 0);
7603 rc2
= sqlite3_test_control(testctrl
, p
->db
, opt
);
7608 /* sqlite3_test_control(int) */
7609 case SQLITE_TESTCTRL_PRNG_SAVE
:
7610 case SQLITE_TESTCTRL_PRNG_RESTORE
:
7611 case SQLITE_TESTCTRL_PRNG_RESET
:
7612 case SQLITE_TESTCTRL_BYTEORDER
:
7614 rc2
= sqlite3_test_control(testctrl
);
7615 isOk
= testctrl
==SQLITE_TESTCTRL_BYTEORDER
? 1 : 3;
7619 /* sqlite3_test_control(int, uint) */
7620 case SQLITE_TESTCTRL_PENDING_BYTE
:
7622 unsigned int opt
= (unsigned int)integerValue(azArg
[2]);
7623 rc2
= sqlite3_test_control(testctrl
, opt
);
7628 /* sqlite3_test_control(int, int) */
7629 case SQLITE_TESTCTRL_ASSERT
:
7630 case SQLITE_TESTCTRL_ALWAYS
:
7632 int opt
= booleanValue(azArg
[2]);
7633 rc2
= sqlite3_test_control(testctrl
, opt
);
7638 /* sqlite3_test_control(int, int) */
7639 case SQLITE_TESTCTRL_LOCALTIME_FAULT
:
7640 case SQLITE_TESTCTRL_NEVER_CORRUPT
:
7642 int opt
= booleanValue(azArg
[2]);
7643 rc2
= sqlite3_test_control(testctrl
, opt
);
7648 case SQLITE_TESTCTRL_IMPOSTER
:
7650 rc2
= sqlite3_test_control(testctrl
, p
->db
,
7652 integerValue(azArg
[3]),
7653 integerValue(azArg
[4]));
7659 case SQLITE_TESTCTRL_PARSER_COVERAGE
:
7661 sqlite3_test_control(testctrl
, p
->out
);
7667 if( isOk
==0 && iCtrl
>=0 ){
7668 utf8_printf(p
->out
, "Usage: .testctrl %s %s\n", zCmd
, aCtrl
[iCtrl
].zUsage
);
7670 }else if( isOk
==1 ){
7671 raw_printf(p
->out
, "%d\n", rc2
);
7672 }else if( isOk
==2 ){
7673 raw_printf(p
->out
, "0x%08x\n", rc2
);
7676 #endif /* !defined(SQLITE_UNTESTABLE) */
7678 if( c
=='t' && n
>4 && strncmp(azArg
[0], "timeout", n
)==0 ){
7680 sqlite3_busy_timeout(p
->db
, nArg
>=2 ? (int)integerValue(azArg
[1]) : 0);
7683 if( c
=='t' && n
>=5 && strncmp(azArg
[0], "timer", n
)==0 ){
7685 enableTimer
= booleanValue(azArg
[1]);
7686 if( enableTimer
&& !HAS_TIMER
){
7687 raw_printf(stderr
, "Error: timer not available on this system.\n");
7691 raw_printf(stderr
, "Usage: .timer on|off\n");
7696 if( c
=='t' && strncmp(azArg
[0], "trace", n
)==0 ){
7699 raw_printf(stderr
, "Usage: .trace FILE|off\n");
7701 goto meta_command_exit
;
7703 output_file_close(p
->traceOut
);
7704 p
->traceOut
= output_file_open(azArg
[1], 0);
7705 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
7706 if( p
->traceOut
==0 ){
7707 sqlite3_trace_v2(p
->db
, 0, 0, 0);
7709 sqlite3_trace_v2(p
->db
, SQLITE_TRACE_STMT
, sql_trace_callback
,p
->traceOut
);
7714 #if SQLITE_USER_AUTHENTICATION
7715 if( c
=='u' && strncmp(azArg
[0], "user", n
)==0 ){
7717 raw_printf(stderr
, "Usage: .user SUBCOMMAND ...\n");
7719 goto meta_command_exit
;
7722 if( strcmp(azArg
[1],"login")==0 ){
7724 raw_printf(stderr
, "Usage: .user login USER PASSWORD\n");
7726 goto meta_command_exit
;
7728 rc
= sqlite3_user_authenticate(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]));
7730 utf8_printf(stderr
, "Authentication failed for user %s\n", azArg
[2]);
7733 }else if( strcmp(azArg
[1],"add")==0 ){
7735 raw_printf(stderr
, "Usage: .user add USER PASSWORD ISADMIN\n");
7737 goto meta_command_exit
;
7739 rc
= sqlite3_user_add(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
7740 booleanValue(azArg
[4]));
7742 raw_printf(stderr
, "User-Add failed: %d\n", rc
);
7745 }else if( strcmp(azArg
[1],"edit")==0 ){
7747 raw_printf(stderr
, "Usage: .user edit USER PASSWORD ISADMIN\n");
7749 goto meta_command_exit
;
7751 rc
= sqlite3_user_change(p
->db
, azArg
[2], azArg
[3], strlen30(azArg
[3]),
7752 booleanValue(azArg
[4]));
7754 raw_printf(stderr
, "User-Edit failed: %d\n", rc
);
7757 }else if( strcmp(azArg
[1],"delete")==0 ){
7759 raw_printf(stderr
, "Usage: .user delete USER\n");
7761 goto meta_command_exit
;
7763 rc
= sqlite3_user_delete(p
->db
, azArg
[2]);
7765 raw_printf(stderr
, "User-Delete failed: %d\n", rc
);
7769 raw_printf(stderr
, "Usage: .user login|add|edit|delete ...\n");
7771 goto meta_command_exit
;
7774 #endif /* SQLITE_USER_AUTHENTICATION */
7776 if( c
=='v' && strncmp(azArg
[0], "version", n
)==0 ){
7777 utf8_printf(p
->out
, "SQLite %s %s\n" /*extra-version-info*/,
7778 sqlite3_libversion(), sqlite3_sourceid());
7779 #if SQLITE_HAVE_ZLIB
7780 utf8_printf(p
->out
, "zlib version %s\n", zlibVersion());
7782 #define CTIMEOPT_VAL_(opt) #opt
7783 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
7784 #if defined(__clang__) && defined(__clang_major__)
7785 utf8_printf(p
->out
, "clang-" CTIMEOPT_VAL(__clang_major__
) "."
7786 CTIMEOPT_VAL(__clang_minor__
) "."
7787 CTIMEOPT_VAL(__clang_patchlevel__
) "\n");
7788 #elif defined(_MSC_VER)
7789 utf8_printf(p
->out
, "msvc-" CTIMEOPT_VAL(_MSC_VER
) "\n");
7790 #elif defined(__GNUC__) && defined(__VERSION__)
7791 utf8_printf(p
->out
, "gcc-" __VERSION__
"\n");
7795 if( c
=='v' && strncmp(azArg
[0], "vfsinfo", n
)==0 ){
7796 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
7797 sqlite3_vfs
*pVfs
= 0;
7799 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFS_POINTER
, &pVfs
);
7801 utf8_printf(p
->out
, "vfs.zName = \"%s\"\n", pVfs
->zName
);
7802 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
7803 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
7804 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
7809 if( c
=='v' && strncmp(azArg
[0], "vfslist", n
)==0 ){
7811 sqlite3_vfs
*pCurrent
= 0;
7813 sqlite3_file_control(p
->db
, "main", SQLITE_FCNTL_VFS_POINTER
, &pCurrent
);
7815 for(pVfs
=sqlite3_vfs_find(0); pVfs
; pVfs
=pVfs
->pNext
){
7816 utf8_printf(p
->out
, "vfs.zName = \"%s\"%s\n", pVfs
->zName
,
7817 pVfs
==pCurrent
? " <--- CURRENT" : "");
7818 raw_printf(p
->out
, "vfs.iVersion = %d\n", pVfs
->iVersion
);
7819 raw_printf(p
->out
, "vfs.szOsFile = %d\n", pVfs
->szOsFile
);
7820 raw_printf(p
->out
, "vfs.mxPathname = %d\n", pVfs
->mxPathname
);
7822 raw_printf(p
->out
, "-----------------------------------\n");
7827 if( c
=='v' && strncmp(azArg
[0], "vfsname", n
)==0 ){
7828 const char *zDbName
= nArg
==2 ? azArg
[1] : "main";
7831 sqlite3_file_control(p
->db
, zDbName
, SQLITE_FCNTL_VFSNAME
, &zVfsName
);
7833 utf8_printf(p
->out
, "%s\n", zVfsName
);
7834 sqlite3_free(zVfsName
);
7839 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
7840 if( c
=='w' && strncmp(azArg
[0], "wheretrace", n
)==0 ){
7841 sqlite3WhereTrace
= nArg
>=2 ? booleanValue(azArg
[1]) : 0xff;
7845 if( c
=='w' && strncmp(azArg
[0], "width", n
)==0 ){
7847 assert( nArg
<=ArraySize(azArg
) );
7848 for(j
=1; j
<nArg
&& j
<ArraySize(p
->colWidth
); j
++){
7849 p
->colWidth
[j
-1] = (int)integerValue(azArg
[j
]);
7854 utf8_printf(stderr
, "Error: unknown command or invalid arguments: "
7855 " \"%s\". Enter \".help\" for help\n", azArg
[0]);
7862 if( p
->outCount
==0 ) output_reset(p
);
7868 ** Return TRUE if a semicolon occurs anywhere in the first N characters
7871 static int line_contains_semicolon(const char *z
, int N
){
7873 for(i
=0; i
<N
; i
++){ if( z
[i
]==';' ) return 1; }
7878 ** Test to see if a line consists entirely of whitespace.
7880 static int _all_whitespace(const char *z
){
7882 if( IsSpace(z
[0]) ) continue;
7883 if( *z
=='/' && z
[1]=='*' ){
7885 while( *z
&& (*z
!='*' || z
[1]!='/') ){ z
++; }
7886 if( *z
==0 ) return 0;
7890 if( *z
=='-' && z
[1]=='-' ){
7892 while( *z
&& *z
!='\n' ){ z
++; }
7893 if( *z
==0 ) return 1;
7902 ** Return TRUE if the line typed in is an SQL command terminator other
7903 ** than a semi-colon. The SQL Server style "go" command is understood
7904 ** as is the Oracle "/".
7906 static int line_is_command_terminator(const char *zLine
){
7907 while( IsSpace(zLine
[0]) ){ zLine
++; };
7908 if( zLine
[0]=='/' && _all_whitespace(&zLine
[1]) ){
7909 return 1; /* Oracle */
7911 if( ToLower(zLine
[0])=='g' && ToLower(zLine
[1])=='o'
7912 && _all_whitespace(&zLine
[2]) ){
7913 return 1; /* SQL Server */
7919 ** We need a default sqlite3_complete() implementation to use in case
7920 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
7921 ** any arbitrary text is a complete SQL statement. This is not very
7922 ** user-friendly, but it does seem to work.
7924 #ifdef SQLITE_OMIT_COMPLETE
7925 int sqlite3_complete(const char *zSql
){ return 1; }
7929 ** Return true if zSql is a complete SQL statement. Return false if it
7930 ** ends in the middle of a string literal or C-style comment.
7932 static int line_is_complete(char *zSql
, int nSql
){
7934 if( zSql
==0 ) return 1;
7937 rc
= sqlite3_complete(zSql
);
7943 ** Run a single line of SQL. Return the number of errors.
7945 static int runOneSqlLine(ShellState
*p
, char *zSql
, FILE *in
, int startline
){
7950 if( ShellHasFlag(p
,SHFLG_Backslash
) ) resolve_backslashes(zSql
);
7952 rc
= shell_exec(p
, zSql
, &zErrMsg
);
7954 if( rc
|| zErrMsg
){
7956 if( in
!=0 || !stdin_is_interactive
){
7957 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
,
7958 "Error: near line %d:", startline
);
7960 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "Error:");
7963 utf8_printf(stderr
, "%s %s\n", zPrefix
, zErrMsg
);
7964 sqlite3_free(zErrMsg
);
7967 utf8_printf(stderr
, "%s %s\n", zPrefix
, sqlite3_errmsg(p
->db
));
7970 }else if( ShellHasFlag(p
, SHFLG_CountChanges
) ){
7971 raw_printf(p
->out
, "changes: %3d total_changes: %d\n",
7972 sqlite3_changes(p
->db
), sqlite3_total_changes(p
->db
));
7979 ** Read input from *in and process it. If *in==0 then input
7980 ** is interactive - the user is typing it it. Otherwise, input
7981 ** is coming from a file or device. A prompt is issued and history
7982 ** is saved only if input is interactive. An interrupt signal will
7983 ** cause this routine to exit immediately, unless input is interactive.
7985 ** Return the number of errors.
7987 static int process_input(ShellState
*p
, FILE *in
){
7988 char *zLine
= 0; /* A single input line */
7989 char *zSql
= 0; /* Accumulated SQL text */
7990 int nLine
; /* Length of current line */
7991 int nSql
= 0; /* Bytes of zSql[] used */
7992 int nAlloc
= 0; /* Allocated zSql[] space */
7993 int nSqlPrior
= 0; /* Bytes of zSql[] used by prior line */
7994 int rc
; /* Error code */
7995 int errCnt
= 0; /* Number of errors seen */
7996 int lineno
= 0; /* Current line number */
7997 int startline
= 0; /* Line number for start of current input */
7999 while( errCnt
==0 || !bail_on_error
|| (in
==0 && stdin_is_interactive
) ){
8001 zLine
= one_input_line(in
, zLine
, nSql
>0);
8004 if( in
==0 && stdin_is_interactive
) printf("\n");
8007 if( seenInterrupt
){
8012 if( nSql
==0 && _all_whitespace(zLine
) ){
8013 if( ShellHasFlag(p
, SHFLG_Echo
) ) printf("%s\n", zLine
);
8016 if( zLine
&& (zLine
[0]=='.' || zLine
[0]=='#') && nSql
==0 ){
8017 if( ShellHasFlag(p
, SHFLG_Echo
) ) printf("%s\n", zLine
);
8018 if( zLine
[0]=='.' ){
8019 rc
= do_meta_command(zLine
, p
);
8020 if( rc
==2 ){ /* exit requested */
8028 if( line_is_command_terminator(zLine
) && line_is_complete(zSql
, nSql
) ){
8029 memcpy(zLine
,";",2);
8031 nLine
= strlen30(zLine
);
8032 if( nSql
+nLine
+2>=nAlloc
){
8033 nAlloc
= nSql
+nLine
+100;
8034 zSql
= realloc(zSql
, nAlloc
);
8035 if( zSql
==0 ) shell_out_of_memory();
8040 for(i
=0; zLine
[i
] && IsSpace(zLine
[i
]); i
++){}
8041 assert( nAlloc
>0 && zSql
!=0 );
8042 memcpy(zSql
, zLine
+i
, nLine
+1-i
);
8046 zSql
[nSql
++] = '\n';
8047 memcpy(zSql
+nSql
, zLine
, nLine
+1);
8050 if( nSql
&& line_contains_semicolon(&zSql
[nSqlPrior
], nSql
-nSqlPrior
)
8051 && sqlite3_complete(zSql
) ){
8052 errCnt
+= runOneSqlLine(p
, zSql
, in
, startline
);
8060 }else if( nSql
&& _all_whitespace(zSql
) ){
8061 if( ShellHasFlag(p
, SHFLG_Echo
) ) printf("%s\n", zSql
);
8065 if( nSql
&& !_all_whitespace(zSql
) ){
8066 errCnt
+= runOneSqlLine(p
, zSql
, in
, startline
);
8074 ** Return a pathname which is the user's home directory. A
8075 ** 0 return indicates an error of some kind.
8077 static char *find_home_dir(int clearFlag
){
8078 static char *home_dir
= NULL
;
8084 if( home_dir
) return home_dir
;
8086 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
8087 && !defined(__RTP__) && !defined(_WRS_KERNEL)
8089 struct passwd
*pwent
;
8090 uid_t uid
= getuid();
8091 if( (pwent
=getpwuid(uid
)) != NULL
) {
8092 home_dir
= pwent
->pw_dir
;
8097 #if defined(_WIN32_WCE)
8098 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
8103 #if defined(_WIN32) || defined(WIN32)
8105 home_dir
= getenv("USERPROFILE");
8110 home_dir
= getenv("HOME");
8113 #if defined(_WIN32) || defined(WIN32)
8115 char *zDrive
, *zPath
;
8117 zDrive
= getenv("HOMEDRIVE");
8118 zPath
= getenv("HOMEPATH");
8119 if( zDrive
&& zPath
){
8120 n
= strlen30(zDrive
) + strlen30(zPath
) + 1;
8121 home_dir
= malloc( n
);
8122 if( home_dir
==0 ) return 0;
8123 sqlite3_snprintf(n
, home_dir
, "%s%s", zDrive
, zPath
);
8130 #endif /* !_WIN32_WCE */
8133 int n
= strlen30(home_dir
) + 1;
8134 char *z
= malloc( n
);
8135 if( z
) memcpy(z
, home_dir
, n
);
8143 ** Read input from the file given by sqliterc_override. Or if that
8144 ** parameter is NULL, take input from ~/.sqliterc
8146 ** Returns the number of errors.
8148 static void process_sqliterc(
8149 ShellState
*p
, /* Configuration data */
8150 const char *sqliterc_override
/* Name of config file. NULL to use default */
8152 char *home_dir
= NULL
;
8153 const char *sqliterc
= sqliterc_override
;
8157 if (sqliterc
== NULL
) {
8158 home_dir
= find_home_dir(0);
8160 raw_printf(stderr
, "-- warning: cannot find home directory;"
8161 " cannot read ~/.sqliterc\n");
8164 zBuf
= sqlite3_mprintf("%s/.sqliterc",home_dir
);
8167 in
= fopen(sqliterc
,"rb");
8169 if( stdin_is_interactive
){
8170 utf8_printf(stderr
,"-- Loading resources from %s\n",sqliterc
);
8172 process_input(p
,in
);
8179 ** Show available command line options
8181 static const char zOptions
[] =
8182 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
8183 " -A ARGS... run \".archive ARGS\" and exit\n"
8185 " -append append the database to the end of the file\n"
8186 " -ascii set output mode to 'ascii'\n"
8187 " -bail stop after hitting an error\n"
8188 " -batch force batch I/O\n"
8189 " -column set output mode to 'column'\n"
8190 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
8191 " -csv set output mode to 'csv'\n"
8192 " -echo print commands before execution\n"
8193 " -init FILENAME read/process named file\n"
8194 " -[no]header turn headers on or off\n"
8195 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8196 " -heap SIZE Size of heap for memsys3 or memsys5\n"
8198 " -help show this message\n"
8199 " -html set output mode to HTML\n"
8200 " -interactive force interactive I/O\n"
8201 " -line set output mode to 'line'\n"
8202 " -list set output mode to 'list'\n"
8203 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
8204 " -mmap N default mmap size set to N\n"
8205 #ifdef SQLITE_ENABLE_MULTIPLEX
8206 " -multiplex enable the multiplexor VFS\n"
8208 " -newline SEP set output row separator. Default: '\\n'\n"
8209 " -nullvalue TEXT set text string for NULL values. Default ''\n"
8210 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
8211 " -quote set output mode to 'quote'\n"
8212 " -readonly open the database read-only\n"
8213 " -separator SEP set output column separator. Default: '|'\n"
8214 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
8215 " -sorterref SIZE sorter references threshold size\n"
8217 " -stats print memory stats before each finalize\n"
8218 " -version show SQLite version\n"
8219 " -vfs NAME use NAME as the default VFS\n"
8220 #ifdef SQLITE_ENABLE_VFSTRACE
8221 " -vfstrace enable tracing of all VFS calls\n"
8223 #ifdef SQLITE_HAVE_ZLIB
8224 " -zip open the file as a ZIP Archive\n"
8227 static void usage(int showDetail
){
8229 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
8230 "FILENAME is the name of an SQLite database. A new database is created\n"
8231 "if the file does not previously exist.\n", Argv0
);
8233 utf8_printf(stderr
, "OPTIONS include:\n%s", zOptions
);
8235 raw_printf(stderr
, "Use the -help option for additional information\n");
8241 ** Internal check: Verify that the SQLite is uninitialized. Print a
8242 ** error message if it is initialized.
8244 static void verify_uninitialized(void){
8245 if( sqlite3_config(-1)==SQLITE_MISUSE
){
8246 utf8_printf(stdout
, "WARNING: attempt to configure SQLite after"
8247 " initialization.\n");
8252 ** Initialize the state information in data
8254 static void main_init(ShellState
*data
) {
8255 memset(data
, 0, sizeof(*data
));
8256 data
->normalMode
= data
->cMode
= data
->mode
= MODE_List
;
8257 data
->autoExplain
= 1;
8258 memcpy(data
->colSeparator
,SEP_Column
, 2);
8259 memcpy(data
->rowSeparator
,SEP_Row
, 2);
8260 data
->showHeader
= 0;
8261 data
->shellFlgs
= SHFLG_Lookaside
;
8262 verify_uninitialized();
8263 sqlite3_config(SQLITE_CONFIG_URI
, 1);
8264 sqlite3_config(SQLITE_CONFIG_LOG
, shellLog
, data
);
8265 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
8266 sqlite3_snprintf(sizeof(mainPrompt
), mainPrompt
,"sqlite> ");
8267 sqlite3_snprintf(sizeof(continuePrompt
), continuePrompt
," ...> ");
8271 ** Output text to the console in a font that attracts extra attention.
8274 static void printBold(const char *zText
){
8275 HANDLE out
= GetStdHandle(STD_OUTPUT_HANDLE
);
8276 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo
;
8277 GetConsoleScreenBufferInfo(out
, &defaultScreenInfo
);
8278 SetConsoleTextAttribute(out
,
8279 FOREGROUND_RED
|FOREGROUND_INTENSITY
8281 printf("%s", zText
);
8282 SetConsoleTextAttribute(out
, defaultScreenInfo
.wAttributes
);
8285 static void printBold(const char *zText
){
8286 printf("\033[1m%s\033[0m", zText
);
8291 ** Get the argument to an --option. Throw an error and die if no argument
8294 static char *cmdline_option_value(int argc
, char **argv
, int i
){
8296 utf8_printf(stderr
, "%s: Error: missing argument to %s\n",
8297 argv
[0], argv
[argc
-1]);
8303 #ifndef SQLITE_SHELL_IS_UTF8
8304 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
8305 # define SQLITE_SHELL_IS_UTF8 (0)
8307 # define SQLITE_SHELL_IS_UTF8 (1)
8311 #if SQLITE_SHELL_IS_UTF8
8312 int SQLITE_CDECL
main(int argc
, char **argv
){
8314 int SQLITE_CDECL
wmain(int argc
, wchar_t **wargv
){
8319 const char *zInitFile
= 0;
8322 int warnInmemoryDb
= 0;
8326 const char *zVfs
= 0; /* Value of -vfs command-line option */
8327 #if !SQLITE_SHELL_IS_UTF8
8328 char **argvToFree
= 0;
8332 setBinaryMode(stdin
, 0);
8333 setvbuf(stderr
, 0, _IONBF
, 0); /* Make sure stderr is unbuffered */
8334 stdin_is_interactive
= isatty(0);
8335 stdout_is_console
= isatty(1);
8337 #if USE_SYSTEM_SQLITE+0!=1
8338 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID
,60)!=0 ){
8339 utf8_printf(stderr
, "SQLite header and source version mismatch\n%s\n%s\n",
8340 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
8346 /* On Windows, we must translate command-line arguments into UTF-8.
8347 ** The SQLite memory allocator subsystem has to be enabled in order to
8348 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
8349 ** subsequent sqlite3_config() calls will work. So copy all results into
8350 ** memory that does not come from the SQLite memory allocator.
8352 #if !SQLITE_SHELL_IS_UTF8
8353 sqlite3_initialize();
8354 argvToFree
= malloc(sizeof(argv
[0])*argc
*2);
8356 argv
= argvToFree
+ argc
;
8357 if( argv
==0 ) shell_out_of_memory();
8358 for(i
=0; i
<argc
; i
++){
8359 char *z
= sqlite3_win32_unicode_to_utf8(wargv
[i
]);
8361 if( z
==0 ) shell_out_of_memory();
8363 argv
[i
] = malloc( n
+1 );
8364 if( argv
[i
]==0 ) shell_out_of_memory();
8365 memcpy(argv
[i
], z
, n
+1);
8366 argvToFree
[i
] = argv
[i
];
8372 assert( argc
>=1 && argv
&& argv
[0] );
8375 /* Make sure we have a valid signal handler early, before anything
8379 signal(SIGINT
, interrupt_handler
);
8380 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
8381 SetConsoleCtrlHandler(ConsoleCtrlHandler
, TRUE
);
8384 #ifdef SQLITE_SHELL_DBNAME_PROC
8386 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
8387 ** of a C-function that will provide the name of the database file. Use
8388 ** this compile-time option to embed this shell program in larger
8390 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
8391 SQLITE_SHELL_DBNAME_PROC(&data
.zDbFilename
);
8396 /* Do an initial pass through the command-line argument to locate
8397 ** the name of the database file, the name of the initialization file,
8398 ** the size of the alternative malloc heap,
8399 ** and the first command to execute.
8401 verify_uninitialized();
8402 for(i
=1; i
<argc
; i
++){
8406 if( data
.zDbFilename
==0 ){
8407 data
.zDbFilename
= z
;
8409 /* Excesss arguments are interpreted as SQL (or dot-commands) and
8410 ** mean that nothing is read from stdin */
8413 azCmd
= realloc(azCmd
, sizeof(azCmd
[0])*nCmd
);
8414 if( azCmd
==0 ) shell_out_of_memory();
8418 if( z
[1]=='-' ) z
++;
8419 if( strcmp(z
,"-separator")==0
8420 || strcmp(z
,"-nullvalue")==0
8421 || strcmp(z
,"-newline")==0
8422 || strcmp(z
,"-cmd")==0
8424 (void)cmdline_option_value(argc
, argv
, ++i
);
8425 }else if( strcmp(z
,"-init")==0 ){
8426 zInitFile
= cmdline_option_value(argc
, argv
, ++i
);
8427 }else if( strcmp(z
,"-batch")==0 ){
8428 /* Need to check for batch mode here to so we can avoid printing
8429 ** informational messages (like from process_sqliterc) before
8430 ** we do the actual processing of arguments later in a second pass.
8432 stdin_is_interactive
= 0;
8433 }else if( strcmp(z
,"-heap")==0 ){
8434 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
8436 sqlite3_int64 szHeap
;
8438 zSize
= cmdline_option_value(argc
, argv
, ++i
);
8439 szHeap
= integerValue(zSize
);
8440 if( szHeap
>0x7fff0000 ) szHeap
= 0x7fff0000;
8441 sqlite3_config(SQLITE_CONFIG_HEAP
, malloc((int)szHeap
), (int)szHeap
, 64);
8443 (void)cmdline_option_value(argc
, argv
, ++i
);
8445 }else if( strcmp(z
,"-pagecache")==0 ){
8447 sz
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
8448 if( sz
>70000 ) sz
= 70000;
8450 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
8451 sqlite3_config(SQLITE_CONFIG_PAGECACHE
,
8452 (n
>0 && sz
>0) ? malloc(n
*sz
) : 0, sz
, n
);
8453 data
.shellFlgs
|= SHFLG_Pagecache
;
8454 }else if( strcmp(z
,"-lookaside")==0 ){
8456 sz
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
8458 n
= (int)integerValue(cmdline_option_value(argc
,argv
,++i
));
8460 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, sz
, n
);
8461 if( sz
*n
==0 ) data
.shellFlgs
&= ~SHFLG_Lookaside
;
8462 #ifdef SQLITE_ENABLE_VFSTRACE
8463 }else if( strcmp(z
,"-vfstrace")==0 ){
8464 extern int vfstrace_register(
8465 const char *zTraceName
,
8466 const char *zOldVfsName
,
8467 int (*xOut
)(const char*,void*),
8471 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs
,stderr
,1);
8473 #ifdef SQLITE_ENABLE_MULTIPLEX
8474 }else if( strcmp(z
,"-multiplex")==0 ){
8475 extern int sqlite3_multiple_initialize(const char*,int);
8476 sqlite3_multiplex_initialize(0, 1);
8478 }else if( strcmp(z
,"-mmap")==0 ){
8479 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
8480 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE
, sz
, sz
);
8481 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
8482 }else if( strcmp(z
,"-sorterref")==0 ){
8483 sqlite3_int64 sz
= integerValue(cmdline_option_value(argc
,argv
,++i
));
8484 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE
, (int)sz
);
8486 }else if( strcmp(z
,"-vfs")==0 ){
8487 zVfs
= cmdline_option_value(argc
, argv
, ++i
);
8488 #ifdef SQLITE_HAVE_ZLIB
8489 }else if( strcmp(z
,"-zip")==0 ){
8490 data
.openMode
= SHELL_OPEN_ZIPFILE
;
8492 }else if( strcmp(z
,"-append")==0 ){
8493 data
.openMode
= SHELL_OPEN_APPENDVFS
;
8494 }else if( strcmp(z
,"-readonly")==0 ){
8495 data
.openMode
= SHELL_OPEN_READONLY
;
8496 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8497 }else if( strncmp(z
, "-A",2)==0 ){
8498 /* All remaining command-line arguments are passed to the ".archive"
8499 ** command, so ignore them */
8504 verify_uninitialized();
8507 #ifdef SQLITE_SHELL_INIT_PROC
8509 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
8510 ** of a C-function that will perform initialization actions on SQLite that
8511 ** occur just before or after sqlite3_initialize(). Use this compile-time
8512 ** option to embed this shell program in larger applications. */
8513 extern void SQLITE_SHELL_INIT_PROC(void);
8514 SQLITE_SHELL_INIT_PROC();
8517 /* All the sqlite3_config() calls have now been made. So it is safe
8518 ** to call sqlite3_initialize() and process any command line -vfs option. */
8519 sqlite3_initialize();
8523 sqlite3_vfs
*pVfs
= sqlite3_vfs_find(zVfs
);
8525 sqlite3_vfs_register(pVfs
, 1);
8527 utf8_printf(stderr
, "no such VFS: \"%s\"\n", argv
[i
]);
8532 if( data
.zDbFilename
==0 ){
8533 #ifndef SQLITE_OMIT_MEMORYDB
8534 data
.zDbFilename
= ":memory:";
8535 warnInmemoryDb
= argc
==1;
8537 utf8_printf(stderr
,"%s: Error: no database filename specified\n", Argv0
);
8542 sqlite3_appendvfs_init(0,0,0);
8544 /* Go ahead and open the database file if it already exists. If the
8545 ** file does not exist, delay opening it. This prevents empty database
8546 ** files from being created if a user mistypes the database name argument
8547 ** to the sqlite command-line tool.
8549 if( access(data
.zDbFilename
, 0)==0 ){
8553 /* Process the initialization file if there is one. If no -init option
8554 ** is given on the command line, look for a file named ~/.sqliterc and
8555 ** try to process it.
8557 process_sqliterc(&data
,zInitFile
);
8559 /* Make a second pass through the command-line argument and set
8560 ** options. This second pass is delayed until after the initialization
8561 ** file is processed so that the command-line arguments will override
8562 ** settings in the initialization file.
8564 for(i
=1; i
<argc
; i
++){
8566 if( z
[0]!='-' ) continue;
8567 if( z
[1]=='-' ){ z
++; }
8568 if( strcmp(z
,"-init")==0 ){
8570 }else if( strcmp(z
,"-html")==0 ){
8571 data
.mode
= MODE_Html
;
8572 }else if( strcmp(z
,"-list")==0 ){
8573 data
.mode
= MODE_List
;
8574 }else if( strcmp(z
,"-quote")==0 ){
8575 data
.mode
= MODE_Quote
;
8576 }else if( strcmp(z
,"-line")==0 ){
8577 data
.mode
= MODE_Line
;
8578 }else if( strcmp(z
,"-column")==0 ){
8579 data
.mode
= MODE_Column
;
8580 }else if( strcmp(z
,"-csv")==0 ){
8581 data
.mode
= MODE_Csv
;
8582 memcpy(data
.colSeparator
,",",2);
8583 #ifdef SQLITE_HAVE_ZLIB
8584 }else if( strcmp(z
,"-zip")==0 ){
8585 data
.openMode
= SHELL_OPEN_ZIPFILE
;
8587 }else if( strcmp(z
,"-append")==0 ){
8588 data
.openMode
= SHELL_OPEN_APPENDVFS
;
8589 }else if( strcmp(z
,"-readonly")==0 ){
8590 data
.openMode
= SHELL_OPEN_READONLY
;
8591 }else if( strcmp(z
,"-ascii")==0 ){
8592 data
.mode
= MODE_Ascii
;
8593 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,
8595 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,
8597 }else if( strcmp(z
,"-separator")==0 ){
8598 sqlite3_snprintf(sizeof(data
.colSeparator
), data
.colSeparator
,
8599 "%s",cmdline_option_value(argc
,argv
,++i
));
8600 }else if( strcmp(z
,"-newline")==0 ){
8601 sqlite3_snprintf(sizeof(data
.rowSeparator
), data
.rowSeparator
,
8602 "%s",cmdline_option_value(argc
,argv
,++i
));
8603 }else if( strcmp(z
,"-nullvalue")==0 ){
8604 sqlite3_snprintf(sizeof(data
.nullValue
), data
.nullValue
,
8605 "%s",cmdline_option_value(argc
,argv
,++i
));
8606 }else if( strcmp(z
,"-header")==0 ){
8607 data
.showHeader
= 1;
8608 }else if( strcmp(z
,"-noheader")==0 ){
8609 data
.showHeader
= 0;
8610 }else if( strcmp(z
,"-echo")==0 ){
8611 ShellSetFlag(&data
, SHFLG_Echo
);
8612 }else if( strcmp(z
,"-eqp")==0 ){
8613 data
.autoEQP
= AUTOEQP_on
;
8614 }else if( strcmp(z
,"-eqpfull")==0 ){
8615 data
.autoEQP
= AUTOEQP_full
;
8616 }else if( strcmp(z
,"-stats")==0 ){
8618 }else if( strcmp(z
,"-scanstats")==0 ){
8619 data
.scanstatsOn
= 1;
8620 }else if( strcmp(z
,"-backslash")==0 ){
8621 /* Undocumented command-line option: -backslash
8622 ** Causes C-style backslash escapes to be evaluated in SQL statements
8623 ** prior to sending the SQL into SQLite. Useful for injecting
8624 ** crazy bytes in the middle of SQL statements for testing and debugging.
8626 ShellSetFlag(&data
, SHFLG_Backslash
);
8627 }else if( strcmp(z
,"-bail")==0 ){
8629 }else if( strcmp(z
,"-version")==0 ){
8630 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
8632 }else if( strcmp(z
,"-interactive")==0 ){
8633 stdin_is_interactive
= 1;
8634 }else if( strcmp(z
,"-batch")==0 ){
8635 stdin_is_interactive
= 0;
8636 }else if( strcmp(z
,"-heap")==0 ){
8638 }else if( strcmp(z
,"-pagecache")==0 ){
8640 }else if( strcmp(z
,"-lookaside")==0 ){
8642 }else if( strcmp(z
,"-mmap")==0 ){
8644 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
8645 }else if( strcmp(z
,"-sorterref")==0 ){
8648 }else if( strcmp(z
,"-vfs")==0 ){
8650 #ifdef SQLITE_ENABLE_VFSTRACE
8651 }else if( strcmp(z
,"-vfstrace")==0 ){
8654 #ifdef SQLITE_ENABLE_MULTIPLEX
8655 }else if( strcmp(z
,"-multiplex")==0 ){
8658 }else if( strcmp(z
,"-help")==0 ){
8660 }else if( strcmp(z
,"-cmd")==0 ){
8661 /* Run commands that follow -cmd first and separately from commands
8662 ** that simply appear on the command-line. This seems goofy. It would
8663 ** be better if all commands ran in the order that they appear. But
8664 ** we retain the goofy behavior for historical compatibility. */
8665 if( i
==argc
-1 ) break;
8666 z
= cmdline_option_value(argc
,argv
,++i
);
8668 rc
= do_meta_command(z
, &data
);
8669 if( rc
&& bail_on_error
) return rc
==2 ? 0 : rc
;
8672 rc
= shell_exec(&data
, z
, &zErrMsg
);
8674 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
8675 if( bail_on_error
) return rc
!=0 ? rc
: 1;
8677 utf8_printf(stderr
,"Error: unable to process SQL \"%s\"\n", z
);
8678 if( bail_on_error
) return rc
;
8681 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
8682 }else if( strncmp(z
, "-A", 2)==0 ){
8684 utf8_printf(stderr
, "Error: cannot mix regular SQL or dot-commands"
8685 " with \"%s\"\n", z
);
8688 open_db(&data
, OPEN_DB_ZIPFILE
);
8691 arDotCommand(&data
, 1, argv
+(i
-1), argc
-(i
-1));
8693 arDotCommand(&data
, 1, argv
+i
, argc
-i
);
8699 utf8_printf(stderr
,"%s: Error: unknown option: %s\n", Argv0
, z
);
8700 raw_printf(stderr
,"Use -help for a list of options.\n");
8703 data
.cMode
= data
.mode
;
8707 /* Run all arguments that do not begin with '-' as if they were separate
8708 ** command-line inputs, except for the argToSkip argument which contains
8709 ** the database filename.
8711 for(i
=0; i
<nCmd
; i
++){
8712 if( azCmd
[i
][0]=='.' ){
8713 rc
= do_meta_command(azCmd
[i
], &data
);
8714 if( rc
) return rc
==2 ? 0 : rc
;
8717 rc
= shell_exec(&data
, azCmd
[i
], &zErrMsg
);
8719 utf8_printf(stderr
,"Error: %s\n", zErrMsg
);
8720 return rc
!=0 ? rc
: 1;
8722 utf8_printf(stderr
,"Error: unable to process SQL: %s\n", azCmd
[i
]);
8729 /* Run commands received from standard input
8731 if( stdin_is_interactive
){
8736 "SQLite version %s %.19s\n" /*extra-version-info*/
8737 "Enter \".help\" for usage hints.\n",
8738 sqlite3_libversion(), sqlite3_sourceid()
8740 if( warnInmemoryDb
){
8741 printf("Connected to a ");
8742 printBold("transient in-memory database");
8743 printf(".\nUse \".open FILENAME\" to reopen on a "
8744 "persistent database.\n");
8746 zHome
= find_home_dir(0);
8748 nHistory
= strlen30(zHome
) + 20;
8749 if( (zHistory
= malloc(nHistory
))!=0 ){
8750 sqlite3_snprintf(nHistory
, zHistory
,"%s/.sqlite_history", zHome
);
8753 if( zHistory
){ shell_read_history(zHistory
); }
8754 #if HAVE_READLINE || HAVE_EDITLINE
8755 rl_attempted_completion_function
= readline_completion
;
8756 #elif HAVE_LINENOISE
8757 linenoiseSetCompletionCallback(linenoise_completion
);
8759 rc
= process_input(&data
, 0);
8761 shell_stifle_history(2000);
8762 shell_write_history(zHistory
);
8766 rc
= process_input(&data
, stdin
);
8769 set_table_name(&data
, 0);
8771 session_close_all(&data
);
8774 sqlite3_free(data
.zFreeOnClose
);
8776 output_reset(&data
);
8778 clearTempFile(&data
);
8779 #if !SQLITE_SHELL_IS_UTF8
8780 for(i
=0; i
<argcToFree
; i
++) free(argvToFree
[i
]);
8783 /* Clear the global data structure so that valgrind will detect memory
8785 memset(&data
, 0, sizeof(data
));