2 ** This file is automatically generated by the script in the canonical
3 ** SQLite source tree at tool/mkshellc.tcl. That script combines source
4 ** code from various constituent source files of SQLite into this single
5 ** "shell.c" file used to implement the SQLite command-line shell.
7 ** Most of the code found below comes from the "src/shell.c.in" file in
8 ** the canonical SQLite source tree. That main file contains "INCLUDE"
9 ** lines that specify other files in the canonical source tree that are
10 ** inserted to getnerate this complete program source file.
12 ** The code from multiple files is combined into this single "shell.c"
13 ** source file to help make the command-line program easier to compile.
15 ** To modify this program, get a copy of the canonical SQLite source tree,
16 ** edit the src/shell.c.in" and/or some of the other files that are included
17 ** by "src/shell.c.in", then rerun the tool/mkshellc.tcl script.
22 ** The author disclaims copyright to this source code. In place of
23 ** a legal notice, here is a blessing:
25 ** May you do good and not evil.
26 ** May you find forgiveness for yourself and forgive others.
27 ** May you share freely, never taking more than you give.
29 *************************************************************************
30 ** This file contains code to implement the "sqlite" command line
31 ** utility for accessing SQLite databases.
33 #if (defined(_WIN32) || defined(WIN32)) && !defined(_CRT_SECURE_NO_WARNINGS)
34 /* This needs to come before any includes for MSVC compiler */
35 #define _CRT_SECURE_NO_WARNINGS
39 ** Warning pragmas copied from msvc.h in the core.
42 #pragma warning(disable : 4054)
43 #pragma warning(disable : 4055)
44 #pragma warning(disable : 4100)
45 #pragma warning(disable : 4127)
46 #pragma warning(disable : 4130)
47 #pragma warning(disable : 4152)
48 #pragma warning(disable : 4189)
49 #pragma warning(disable : 4206)
50 #pragma warning(disable : 4210)
51 #pragma warning(disable : 4232)
52 #pragma warning(disable : 4244)
53 #pragma warning(disable : 4305)
54 #pragma warning(disable : 4306)
55 #pragma warning(disable : 4702)
56 #pragma warning(disable : 4706)
57 #endif /* defined(_MSC_VER) */
60 ** No support for loadable extensions in VxWorks.
62 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
63 # define SQLITE_OMIT_LOAD_EXTENSION 1
67 ** Enable large-file support for fopen() and friends on unix.
69 #ifndef SQLITE_DISABLE_LFS
70 # define _LARGE_FILE 1
71 # ifndef _FILE_OFFSET_BITS
72 # define _FILE_OFFSET_BITS 64
74 # define _LARGEFILE_SOURCE 1
82 typedef sqlite3_int64 i64;
83 typedef sqlite3_uint64 u64;
84 typedef unsigned char u8;
85 #if SQLITE_USER_AUTHENTICATION
86 # include "sqlite3userauth.h"
91 #if !defined(_WIN32) && !defined(WIN32)
93 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
97 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
100 # if defined(__MINGW32__)
101 # define DIRENT dirent
103 # define S_ISLNK(mode) (0)
107 #include <sys/types.h>
108 #include <sys/stat.h>
111 # include <readline/readline.h>
112 # include <readline/history.h>
116 # include <editline/readline.h>
119 #if HAVE_EDITLINE || HAVE_READLINE
121 # define shell_add_history(X) add_history(X)
122 # define shell_read_history(X) read_history(X)
123 # define shell_write_history(X) write_history(X)
124 # define shell_stifle_history(X) stifle_history(X)
125 # define shell_readline(X) readline(X)
129 # include "linenoise.h"
130 # define shell_add_history(X) linenoiseHistoryAdd(X)
131 # define shell_read_history(X) linenoiseHistoryLoad(X)
132 # define shell_write_history(X) linenoiseHistorySave(X)
133 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
134 # define shell_readline(X) linenoise(X)
138 # define shell_read_history(X)
139 # define shell_write_history(X)
140 # define shell_stifle_history(X)
142 # define SHELL_USE_LOCAL_GETLINE 1
146 #if defined(_WIN32) || defined(WIN32)
149 # define isatty(h) _isatty(h)
151 # define access(f,m) _access((f),(m))
154 # define unlink _unlink
157 # define popen _popen
159 # define pclose _pclose
161 /* Make sure isatty() has a prototype. */
162 extern int isatty(int);
164 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
165 /* popen and pclose are not C89 functions and so are
166 ** sometimes omitted from the <stdio.h> header */
167 extern FILE *popen(const char*,const char*);
168 extern int pclose(FILE*);
170 # define SQLITE_OMIT_POPEN 1
174 #if defined(_WIN32_WCE)
175 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
176 * thus we always assume that we have a console. That can be
177 * overridden with the -batch command line option.
182 /* ctype macros that work with signed characters */
183 #define IsSpace(X) isspace((unsigned char)X)
184 #define IsDigit(X) isdigit((unsigned char)X)
185 #define ToLower(X) (char)tolower((unsigned char)X)
187 #if defined(_WIN32) || defined(WIN32)
190 /* string conversion routines only needed on Win32 */
191 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
192 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
193 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
194 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
197 /* On Windows, we normally run with output mode of TEXT so that \n characters
198 ** are automatically translated into \r\n. However, this behavior needs
199 ** to be disabled in some cases (ex: when generating CSV output and when
200 ** rendering quoted strings that contain \n characters). The following
201 ** routines take care of that.
203 #if defined(_WIN32) || defined(WIN32)
204 static void setBinaryMode(FILE *file, int isOutput){
205 if( isOutput ) fflush(file);
206 _setmode(_fileno(file), _O_BINARY);
208 static void setTextMode(FILE *file, int isOutput){
209 if( isOutput ) fflush(file);
210 _setmode(_fileno(file), _O_TEXT);
213 # define setBinaryMode(X,Y)
214 # define setTextMode(X,Y)
218 /* True if the timer is enabled */
219 static int enableTimer = 0;
221 /* Return the current wall-clock time */
222 static sqlite3_int64 timeOfDay(void){
223 static sqlite3_vfs *clockVfs = 0;
225 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
226 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
227 clockVfs->xCurrentTimeInt64(clockVfs, &t);
230 clockVfs->xCurrentTime(clockVfs, &r);
231 t = (sqlite3_int64)(r*86400000.0);
236 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
237 #include <sys/time.h>
238 #include <sys/resource.h>
240 /* VxWorks does not support getrusage() as far as we can determine */
241 #if defined(_WRS_KERNEL) || defined(__RTP__)
243 struct timeval ru_utime; /* user CPU time used */
244 struct timeval ru_stime; /* system CPU time used */
246 #define getrusage(A,B) memset(B,0,sizeof(*B))
249 /* Saved resource information for the beginning of an operation */
250 static struct rusage sBegin; /* CPU time at start */
251 static sqlite3_int64 iBegin; /* Wall-clock time at start */
254 ** Begin timing an operation
256 static void beginTimer(void){
258 getrusage(RUSAGE_SELF, &sBegin);
259 iBegin = timeOfDay();
263 /* Return the difference of two time_structs in seconds */
264 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
265 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
266 (double)(pEnd->tv_sec - pStart->tv_sec);
270 ** Print the timing results.
272 static void endTimer(void){
274 sqlite3_int64 iEnd = timeOfDay();
276 getrusage(RUSAGE_SELF, &sEnd);
277 printf("Run Time: real %.3f user %f sys %f\n",
278 (iEnd - iBegin)*0.001,
279 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
280 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
284 #define BEGIN_TIMER beginTimer()
285 #define END_TIMER endTimer()
288 #elif (defined(_WIN32) || defined(WIN32))
290 /* Saved resource information for the beginning of an operation */
291 static HANDLE hProcess;
292 static FILETIME ftKernelBegin;
293 static FILETIME ftUserBegin;
294 static sqlite3_int64 ftWallBegin;
295 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
296 LPFILETIME, LPFILETIME);
297 static GETPROCTIMES getProcessTimesAddr = NULL;
300 ** Check to see if we have timer support. Return 1 if necessary
301 ** support found (or found previously).
303 static int hasTimer(void){
304 if( getProcessTimesAddr ){
307 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
308 ** versions. See if the version we are running on has it, and if it
309 ** does, save off a pointer to it and the current process handle.
311 hProcess = GetCurrentProcess();
313 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
314 if( NULL != hinstLib ){
315 getProcessTimesAddr =
316 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
317 if( NULL != getProcessTimesAddr ){
320 FreeLibrary(hinstLib);
328 ** Begin timing an operation
330 static void beginTimer(void){
331 if( enableTimer && getProcessTimesAddr ){
332 FILETIME ftCreation, ftExit;
333 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
334 &ftKernelBegin,&ftUserBegin);
335 ftWallBegin = timeOfDay();
339 /* Return the difference of two FILETIME structs in seconds */
340 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
341 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
342 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
343 return (double) ((i64End - i64Start) / 10000000.0);
347 ** Print the timing results.
349 static void endTimer(void){
350 if( enableTimer && getProcessTimesAddr){
351 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
352 sqlite3_int64 ftWallEnd = timeOfDay();
353 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
354 printf("Run Time: real %.3f user %f sys %f\n",
355 (ftWallEnd - ftWallBegin)*0.001,
356 timeDiff(&ftUserBegin, &ftUserEnd),
357 timeDiff(&ftKernelBegin, &ftKernelEnd));
361 #define BEGIN_TIMER beginTimer()
362 #define END_TIMER endTimer()
363 #define HAS_TIMER hasTimer()
372 ** Used to prevent warnings about unused parameters
374 #define UNUSED_PARAMETER(x) (void)(x)
377 ** Number of elements in an array
379 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
382 ** If the following flag is set, then command execution stops
383 ** at an error if we are not interactive.
385 static int bail_on_error = 0;
388 ** Threat stdin as an interactive input if the following variable
389 ** is true. Otherwise, assume stdin is connected to a file or pipe.
391 static int stdin_is_interactive = 1;
394 ** On Windows systems we have to know if standard output is a console
395 ** in order to translate UTF-8 into MBCS. The following variable is
396 ** true if translation is required.
398 static int stdout_is_console = 1;
401 ** The following is the open SQLite database. We make a pointer
402 ** to this database a static variable so that it can be accessed
403 ** by the SIGINT handler to interrupt database processing.
405 static sqlite3 *globalDb = 0;
408 ** True if an interrupt (Control-C) has been received.
410 static volatile int seenInterrupt = 0;
413 ** This is the name of our program. It is set in main(), used
414 ** in a number of other places, mostly for error messages.
419 ** Prompt strings. Initialized in main. Settable with
420 ** .prompt main continue
422 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
423 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
426 ** Render output like fprintf(). Except, if the output is going to the
427 ** console and if this is running on a Windows machine, translate the
428 ** output from UTF-8 into MBCS.
430 #if defined(_WIN32) || defined(WIN32)
431 void utf8_printf(FILE *out, const char *zFormat, ...){
433 va_start(ap, zFormat);
434 if( stdout_is_console && (out==stdout || out==stderr) ){
435 char *z1 = sqlite3_vmprintf(zFormat, ap);
436 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
441 vfprintf(out, zFormat, ap);
445 #elif !defined(utf8_printf)
446 # define utf8_printf fprintf
450 ** Render output like fprintf(). This should not be used on anything that
451 ** includes string formatting (e.g. "%s").
453 #if !defined(raw_printf)
454 # define raw_printf fprintf
457 /* Indicate out-of-memory and exit. */
458 static void shell_out_of_memory(void){
459 raw_printf(stderr,"Error: out of memory\n");
464 ** Write I/O traces to the following stream.
466 #ifdef SQLITE_ENABLE_IOTRACE
467 static FILE *iotrace = 0;
471 ** This routine works like printf in that its first argument is a
472 ** format string and subsequent arguments are values to be substituted
473 ** in place of % fields. The result of formatting this string
474 ** is written to iotrace.
476 #ifdef SQLITE_ENABLE_IOTRACE
477 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
480 if( iotrace==0 ) return;
481 va_start(ap, zFormat);
482 z = sqlite3_vmprintf(zFormat, ap);
484 utf8_printf(iotrace, "%s", z);
490 ** Output string zUtf to stream pOut as w characters. If w is negative,
491 ** then right-justify the text. W is the width in UTF-8 characters, not
492 ** in bytes. This is different from the %*.*s specification in printf
493 ** since with %*.*s the width is measured in bytes, not characters.
495 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
498 int aw = w<0 ? -w : w;
500 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
501 for(i=n=0; zUtf[i]; i++){
502 if( (zUtf[i]&0xc0)!=0x80 ){
505 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
511 utf8_printf(pOut, "%.*s", i, zUtf);
513 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
515 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
521 ** Determines if a string is a number of not.
523 static int isNumber(const char *z, int *realnum){
524 if( *z=='-' || *z=='+' ) z++;
529 if( realnum ) *realnum = 0;
530 while( IsDigit(*z) ){ z++; }
533 if( !IsDigit(*z) ) return 0;
534 while( IsDigit(*z) ){ z++; }
535 if( realnum ) *realnum = 1;
537 if( *z=='e' || *z=='E' ){
539 if( *z=='+' || *z=='-' ) z++;
540 if( !IsDigit(*z) ) return 0;
541 while( IsDigit(*z) ){ z++; }
542 if( realnum ) *realnum = 1;
548 ** Compute a string length that is limited to what can be stored in
549 ** lower 30 bits of a 32-bit signed integer.
551 static int strlen30(const char *z){
553 while( *z2 ){ z2++; }
554 return 0x3fffffff & (int)(z2 - z);
558 ** Return the length of a string in characters. Multibyte UTF8 characters
559 ** count as a single character.
561 static int strlenChar(const char *z){
564 if( (0xc0&*(z++))!=0x80 ) n++;
570 ** This routine reads a line of text from FILE in, stores
571 ** the text in memory obtained from malloc() and returns a pointer
572 ** to the text. NULL is returned at end of file, or if malloc()
575 ** If zLine is not NULL then it is a malloced buffer returned from
576 ** a previous call to this routine that may be reused.
578 static char *local_getline(char *zLine, FILE *in){
579 int nLine = zLine==0 ? 0 : 100;
584 nLine = nLine*2 + 100;
585 zLine = realloc(zLine, nLine);
586 if( zLine==0 ) return 0;
588 if( fgets(&zLine[n], nLine - n, in)==0 ){
596 while( zLine[n] ) n++;
597 if( n>0 && zLine[n-1]=='\n' ){
599 if( n>0 && zLine[n-1]=='\r' ) n--;
604 #if defined(_WIN32) || defined(WIN32)
605 /* For interactive input on Windows systems, translate the
606 ** multi-byte characterset characters into UTF-8. */
607 if( stdin_is_interactive && in==stdin ){
608 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
610 int nTrans = strlen30(zTrans)+1;
612 zLine = realloc(zLine, nTrans);
614 sqlite3_free(zTrans);
618 memcpy(zLine, zTrans, nTrans);
619 sqlite3_free(zTrans);
622 #endif /* defined(_WIN32) || defined(WIN32) */
627 ** Retrieve a single line of input text.
629 ** If in==0 then read from standard input and prompt before each line.
630 ** If isContinuation is true, then a continuation prompt is appropriate.
631 ** If isContinuation is zero, then the main prompt should be used.
633 ** If zPrior is not NULL then it is a buffer from a prior call to this
634 ** routine that can be reused.
636 ** The result is stored in space obtained from malloc() and must either
637 ** be freed by the caller or else passed back into this routine via the
638 ** zPrior argument for reuse.
640 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
644 zResult = local_getline(zPrior, in);
646 zPrompt = isContinuation ? continuePrompt : mainPrompt;
647 #if SHELL_USE_LOCAL_GETLINE
648 printf("%s", zPrompt);
650 zResult = local_getline(zPrior, stdin);
653 zResult = shell_readline(zPrompt);
654 if( zResult && *zResult ) shell_add_history(zResult);
662 ** Return the value of a hexadecimal digit. Return -1 if the input
663 ** is not a hex digit.
665 static int hexDigitValue(char c){
666 if( c>='0' && c<='9' ) return c - '0';
667 if( c>='a' && c<='f' ) return c - 'a' + 10;
668 if( c>='A' && c<='F' ) return c - 'A' + 10;
673 ** Interpret zArg as an integer value, possibly with suffixes.
675 static sqlite3_int64 integerValue(const char *zArg){
677 static const struct { char *zSuffix; int iMult; } aMult[] = {
679 { "MiB", 1024*1024 },
680 { "GiB", 1024*1024*1024 },
683 { "GB", 1000000000 },
693 }else if( zArg[0]=='+' ){
696 if( zArg[0]=='0' && zArg[1]=='x' ){
699 while( (x = hexDigitValue(zArg[0]))>=0 ){
704 while( IsDigit(zArg[0]) ){
705 v = v*10 + zArg[0] - '0';
709 for(i=0; i<ArraySize(aMult); i++){
710 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
715 return isNeg? -v : v;
719 ** A variable length string to which one can append text.
721 typedef struct ShellText ShellText;
729 ** Initialize and destroy a ShellText object
731 static void initText(ShellText *p){
732 memset(p, 0, sizeof(*p));
734 static void freeText(ShellText *p){
739 /* zIn is either a pointer to a NULL-terminated string in memory obtained
740 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
741 ** added to zIn, and the result returned in memory obtained from malloc().
742 ** zIn, if it was not NULL, is freed.
744 ** If the third argument, quote, is not '\0', then it is used as a
745 ** quote character for zAppend.
747 static void appendText(ShellText *p, char const *zAppend, char quote){
750 int nAppend = strlen30(zAppend);
752 len = nAppend+p->n+1;
755 for(i=0; i<nAppend; i++){
756 if( zAppend[i]==quote ) len++;
760 if( p->n+len>=p->nAlloc ){
761 p->nAlloc = p->nAlloc*2 + len + 20;
762 p->z = realloc(p->z, p->nAlloc);
764 memset(p, 0, sizeof(*p));
770 char *zCsr = p->z+p->n;
772 for(i=0; i<nAppend; i++){
773 *zCsr++ = zAppend[i];
774 if( zAppend[i]==quote ) *zCsr++ = quote;
777 p->n = (int)(zCsr - p->z);
780 memcpy(p->z+p->n, zAppend, nAppend);
787 ** Attempt to determine if identifier zName needs to be quoted, either
788 ** because it contains non-alphanumeric characters, or because it is an
789 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
790 ** that quoting is required.
792 ** Return '"' if quoting is required. Return 0 if no quoting is required.
794 static char quoteChar(const char *zName){
796 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
797 for(i=0; zName[i]; i++){
798 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
800 return sqlite3_keyword_check(zName, i) ? '"' : 0;
804 ** Construct a fake object name and column list to describe the structure
805 ** of the view, virtual table, or table valued function zSchema.zName.
807 static char *shellFakeSchema(
808 sqlite3 *db, /* The database connection containing the vtab */
809 const char *zSchema, /* Schema of the database holding the vtab */
810 const char *zName /* The name of the virtual table */
812 sqlite3_stmt *pStmt = 0;
819 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
820 zSchema ? zSchema : "main", zName);
821 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
825 cQuote = quoteChar(zSchema);
826 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
827 appendText(&s, zSchema, cQuote);
828 appendText(&s, ".", 0);
830 cQuote = quoteChar(zName);
831 appendText(&s, zName, cQuote);
832 while( sqlite3_step(pStmt)==SQLITE_ROW ){
833 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
835 appendText(&s, zDiv, 0);
837 cQuote = quoteChar(zCol);
838 appendText(&s, zCol, cQuote);
840 appendText(&s, ")", 0);
841 sqlite3_finalize(pStmt);
850 ** SQL function: shell_module_schema(X)
852 ** Return a fake schema for the table-valued function or eponymous virtual
855 static void shellModuleSchema(
856 sqlite3_context *pCtx,
858 sqlite3_value **apVal
860 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
861 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
862 UNUSED_PARAMETER(nVal);
864 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
871 ** SQL function: shell_add_schema(S,X)
873 ** Add the schema name X to the CREATE statement in S and return the result.
876 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
881 ** CREATE UNIQUE INDEX
884 ** CREATE VIRTUAL TABLE
886 ** This UDF is used by the .schema command to insert the schema name of
887 ** attached databases into the middle of the sqlite_master.sql field.
889 static void shellAddSchemaName(
890 sqlite3_context *pCtx,
892 sqlite3_value **apVal
894 static const char *aPrefix[] = {
903 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
904 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
905 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
906 sqlite3 *db = sqlite3_context_db_handle(pCtx);
907 UNUSED_PARAMETER(nVal);
908 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
909 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
910 int n = strlen30(aPrefix[i]);
911 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
915 char cQuote = quoteChar(zSchema);
916 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
917 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
919 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
923 && aPrefix[i][0]=='V'
924 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
927 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
929 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
934 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
940 sqlite3_result_value(pCtx, apVal[0]);
944 ** The source code for several run-time loadable extensions is inserted
945 ** below by the ../tool/mkshellc.tcl script. Before processing that included
946 ** code, we need to override some macros to make the included program code
947 ** work here in the middle of this regular program.
949 #define SQLITE_EXTENSION_INIT1
950 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
952 #if defined(_WIN32) && defined(_MSC_VER)
953 /************************* Begin test_windirent.h ******************/
957 ** The author disclaims copyright to this source code. In place of
958 ** a legal notice, here is a blessing:
960 ** May you do good and not evil.
961 ** May you find forgiveness for yourself and forgive others.
962 ** May you share freely, never taking more than you give.
964 *************************************************************************
965 ** This file contains declarations for most of the opendir() family of
966 ** POSIX functions on Win32 using the MSVCRT.
969 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
970 #define SQLITE_WINDIRENT_H
973 ** We need several data types from the Windows SDK header.
976 #ifndef WIN32_LEAN_AND_MEAN
977 #define WIN32_LEAN_AND_MEAN
983 ** We need several support functions from the SQLite core.
988 ** We need several things from the ANSI and MSVCRT headers.
996 #include <sys/types.h>
997 #include <sys/stat.h>
1000 ** We may need several defines that should have been in "sys/stat.h".
1004 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1008 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1012 #define S_ISLNK(mode) (0)
1016 ** We may need to provide the "mode_t" type.
1019 #ifndef MODE_T_DEFINED
1020 #define MODE_T_DEFINED
1021 typedef unsigned short mode_t;
1025 ** We may need to provide the "ino_t" type.
1028 #ifndef INO_T_DEFINED
1029 #define INO_T_DEFINED
1030 typedef unsigned short ino_t;
1034 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1038 # ifdef FILENAME_MAX
1039 # define NAME_MAX (FILENAME_MAX)
1041 # define NAME_MAX (260)
1046 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1049 #ifndef NULL_INTPTR_T
1050 # define NULL_INTPTR_T ((intptr_t)(0))
1053 #ifndef BAD_INTPTR_T
1054 # define BAD_INTPTR_T ((intptr_t)(-1))
1058 ** We need to provide the necessary structures and related types.
1061 #ifndef DIRENT_DEFINED
1062 #define DIRENT_DEFINED
1063 typedef struct DIRENT DIRENT;
1064 typedef DIRENT *LPDIRENT;
1066 ino_t d_ino; /* Sequence number, do not use. */
1067 unsigned d_attributes; /* Win32 file attributes. */
1068 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1074 typedef struct DIR DIR;
1077 intptr_t d_handle; /* Value returned by "_findfirst". */
1078 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1079 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1084 ** Provide a macro, for use by the implementation, to determine if a
1085 ** particular directory entry should be skipped over when searching for
1086 ** the next directory entry that should be returned by the readdir() or
1087 ** readdir_r() functions.
1091 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1095 ** Provide the function prototype for the POSIX compatiable getenv()
1096 ** function. This function is not thread-safe.
1099 extern const char *windirent_getenv(const char *name);
1102 ** Finally, we can provide the function prototypes for the opendir(),
1103 ** readdir(), readdir_r(), and closedir() POSIX functions.
1106 extern LPDIR opendir(const char *dirname);
1107 extern LPDIRENT readdir(LPDIR dirp);
1108 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1109 extern INT closedir(LPDIR dirp);
1111 #endif /* defined(WIN32) && defined(_MSC_VER) */
1113 /************************* End test_windirent.h ********************/
1114 /************************* Begin test_windirent.c ******************/
1118 ** The author disclaims copyright to this source code. In place of
1119 ** a legal notice, here is a blessing:
1121 ** May you do good and not evil.
1122 ** May you find forgiveness for yourself and forgive others.
1123 ** May you share freely, never taking more than you give.
1125 *************************************************************************
1126 ** This file contains code to implement most of the opendir() family of
1127 ** POSIX functions on Win32 using the MSVCRT.
1130 #if defined(_WIN32) && defined(_MSC_VER)
1131 /* #include "test_windirent.h" */
1134 ** Implementation of the POSIX getenv() function using the Win32 API.
1135 ** This function is not thread-safe.
1137 const char *windirent_getenv(
1140 static char value[32768]; /* Maximum length, per MSDN */
1141 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1142 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1144 memset(value, 0, sizeof(value));
1145 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1146 if( dwRet==0 || dwRet>dwSize ){
1148 ** The function call to GetEnvironmentVariableA() failed -OR-
1149 ** the buffer is not large enough. Either way, return NULL.
1154 ** The function call to GetEnvironmentVariableA() succeeded
1155 ** -AND- the buffer contains the entire value.
1162 ** Implementation of the POSIX opendir() function using the MSVCRT.
1167 struct _finddata_t data;
1168 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1169 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1171 if( dirp==NULL ) return NULL;
1172 memset(dirp, 0, sizeof(DIR));
1174 /* TODO: Remove this if Unix-style root paths are not used. */
1175 if( sqlite3_stricmp(dirname, "/")==0 ){
1176 dirname = windirent_getenv("SystemDrive");
1179 memset(&data, 0, sizeof(struct _finddata_t));
1180 _snprintf(data.name, namesize, "%s\\*", dirname);
1181 dirp->d_handle = _findfirst(data.name, &data);
1183 if( dirp->d_handle==BAD_INTPTR_T ){
1188 /* TODO: Remove this block to allow hidden and/or system files. */
1189 if( is_filtered(data) ){
1192 memset(&data, 0, sizeof(struct _finddata_t));
1193 if( _findnext(dirp->d_handle, &data)==-1 ){
1198 /* TODO: Remove this block to allow hidden and/or system files. */
1199 if( is_filtered(data) ) goto next;
1202 dirp->d_first.d_attributes = data.attrib;
1203 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1204 dirp->d_first.d_name[NAME_MAX] = '\0';
1210 ** Implementation of the POSIX readdir() function using the MSVCRT.
1215 struct _finddata_t data;
1217 if( dirp==NULL ) return NULL;
1219 if( dirp->d_first.d_ino==0 ){
1220 dirp->d_first.d_ino++;
1221 dirp->d_next.d_ino++;
1223 return &dirp->d_first;
1228 memset(&data, 0, sizeof(struct _finddata_t));
1229 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1231 /* TODO: Remove this block to allow hidden and/or system files. */
1232 if( is_filtered(data) ) goto next;
1234 dirp->d_next.d_ino++;
1235 dirp->d_next.d_attributes = data.attrib;
1236 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1237 dirp->d_next.d_name[NAME_MAX] = '\0';
1239 return &dirp->d_next;
1243 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1250 struct _finddata_t data;
1252 if( dirp==NULL ) return EBADF;
1254 if( dirp->d_first.d_ino==0 ){
1255 dirp->d_first.d_ino++;
1256 dirp->d_next.d_ino++;
1258 entry->d_ino = dirp->d_first.d_ino;
1259 entry->d_attributes = dirp->d_first.d_attributes;
1260 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1261 entry->d_name[NAME_MAX] = '\0';
1269 memset(&data, 0, sizeof(struct _finddata_t));
1270 if( _findnext(dirp->d_handle, &data)==-1 ){
1275 /* TODO: Remove this block to allow hidden and/or system files. */
1276 if( is_filtered(data) ) goto next;
1278 entry->d_ino = (ino_t)-1; /* not available */
1279 entry->d_attributes = data.attrib;
1280 strncpy(entry->d_name, data.name, NAME_MAX);
1281 entry->d_name[NAME_MAX] = '\0';
1288 ** Implementation of the POSIX closedir() function using the MSVCRT.
1295 if( dirp==NULL ) return EINVAL;
1297 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1298 result = _findclose(dirp->d_handle);
1305 #endif /* defined(WIN32) && defined(_MSC_VER) */
1307 /************************* End test_windirent.c ********************/
1308 #define dirent DIRENT
1310 /************************* Begin ../ext/misc/shathree.c ******************/
1314 ** The author disclaims copyright to this source code. In place of
1315 ** a legal notice, here is a blessing:
1317 ** May you do good and not evil.
1318 ** May you find forgiveness for yourself and forgive others.
1319 ** May you share freely, never taking more than you give.
1321 ******************************************************************************
1323 ** This SQLite extension implements a functions that compute SHA1 hashes.
1324 ** Two SQL functions are implemented:
1327 ** sha3_query(Y,SIZE)
1329 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1332 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1333 ** and returns a hash of their results.
1335 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1336 ** is used. If SIZE is included it must be one of the integers 224, 256,
1337 ** 384, or 512, to determine SHA3 hash variant that is computed.
1339 SQLITE_EXTENSION_INIT1
1343 /* typedef sqlite3_uint64 u64; */
1345 /******************************************************************************
1349 ** Macros to determine whether the machine is big or little endian,
1350 ** and whether or not that determination is run-time or compile-time.
1352 ** For best performance, an attempt is made to guess at the byte-order
1353 ** using C-preprocessor macros. If that is unsuccessful, or if
1354 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1357 #ifndef SHA3_BYTEORDER
1358 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1359 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1360 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1362 # define SHA3_BYTEORDER 1234
1363 # elif defined(sparc) || defined(__ppc__)
1364 # define SHA3_BYTEORDER 4321
1366 # define SHA3_BYTEORDER 0
1372 ** State structure for a SHA3 hash in progress
1374 typedef struct SHA3Context SHA3Context;
1375 struct SHA3Context {
1377 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1378 unsigned char x[1600]; /* ... or 1600 bytes */
1380 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1381 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1382 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1386 ** A single step of the Keccak mixing function for a 1600-bit state
1388 static void KeccakF1600Step(SHA3Context *p){
1390 u64 b0, b1, b2, b3, b4;
1391 u64 c0, c1, c2, c3, c4;
1392 u64 d0, d1, d2, d3, d4;
1393 static const u64 RC[] = {
1394 0x0000000000000001ULL, 0x0000000000008082ULL,
1395 0x800000000000808aULL, 0x8000000080008000ULL,
1396 0x000000000000808bULL, 0x0000000080000001ULL,
1397 0x8000000080008081ULL, 0x8000000000008009ULL,
1398 0x000000000000008aULL, 0x0000000000000088ULL,
1399 0x0000000080008009ULL, 0x000000008000000aULL,
1400 0x000000008000808bULL, 0x800000000000008bULL,
1401 0x8000000000008089ULL, 0x8000000000008003ULL,
1402 0x8000000000008002ULL, 0x8000000000000080ULL,
1403 0x000000000000800aULL, 0x800000008000000aULL,
1404 0x8000000080008081ULL, 0x8000000000008080ULL,
1405 0x0000000080000001ULL, 0x8000000080008008ULL
1407 # define a00 (p->u.s[0])
1408 # define a01 (p->u.s[1])
1409 # define a02 (p->u.s[2])
1410 # define a03 (p->u.s[3])
1411 # define a04 (p->u.s[4])
1412 # define a10 (p->u.s[5])
1413 # define a11 (p->u.s[6])
1414 # define a12 (p->u.s[7])
1415 # define a13 (p->u.s[8])
1416 # define a14 (p->u.s[9])
1417 # define a20 (p->u.s[10])
1418 # define a21 (p->u.s[11])
1419 # define a22 (p->u.s[12])
1420 # define a23 (p->u.s[13])
1421 # define a24 (p->u.s[14])
1422 # define a30 (p->u.s[15])
1423 # define a31 (p->u.s[16])
1424 # define a32 (p->u.s[17])
1425 # define a33 (p->u.s[18])
1426 # define a34 (p->u.s[19])
1427 # define a40 (p->u.s[20])
1428 # define a41 (p->u.s[21])
1429 # define a42 (p->u.s[22])
1430 # define a43 (p->u.s[23])
1431 # define a44 (p->u.s[24])
1432 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1434 for(i=0; i<24; i+=4){
1435 c0 = a00^a10^a20^a30^a40;
1436 c1 = a01^a11^a21^a31^a41;
1437 c2 = a02^a12^a22^a32^a42;
1438 c3 = a03^a13^a23^a33^a43;
1439 c4 = a04^a14^a24^a34^a44;
1440 d0 = c4^ROL64(c1, 1);
1441 d1 = c0^ROL64(c2, 1);
1442 d2 = c1^ROL64(c3, 1);
1443 d3 = c2^ROL64(c4, 1);
1444 d4 = c3^ROL64(c0, 1);
1447 b1 = ROL64((a11^d1), 44);
1448 b2 = ROL64((a22^d2), 43);
1449 b3 = ROL64((a33^d3), 21);
1450 b4 = ROL64((a44^d4), 14);
1451 a00 = b0 ^((~b1)& b2 );
1453 a11 = b1 ^((~b2)& b3 );
1454 a22 = b2 ^((~b3)& b4 );
1455 a33 = b3 ^((~b4)& b0 );
1456 a44 = b4 ^((~b0)& b1 );
1458 b2 = ROL64((a20^d0), 3);
1459 b3 = ROL64((a31^d1), 45);
1460 b4 = ROL64((a42^d2), 61);
1461 b0 = ROL64((a03^d3), 28);
1462 b1 = ROL64((a14^d4), 20);
1463 a20 = b0 ^((~b1)& b2 );
1464 a31 = b1 ^((~b2)& b3 );
1465 a42 = b2 ^((~b3)& b4 );
1466 a03 = b3 ^((~b4)& b0 );
1467 a14 = b4 ^((~b0)& b1 );
1469 b4 = ROL64((a40^d0), 18);
1470 b0 = ROL64((a01^d1), 1);
1471 b1 = ROL64((a12^d2), 6);
1472 b2 = ROL64((a23^d3), 25);
1473 b3 = ROL64((a34^d4), 8);
1474 a40 = b0 ^((~b1)& b2 );
1475 a01 = b1 ^((~b2)& b3 );
1476 a12 = b2 ^((~b3)& b4 );
1477 a23 = b3 ^((~b4)& b0 );
1478 a34 = b4 ^((~b0)& b1 );
1480 b1 = ROL64((a10^d0), 36);
1481 b2 = ROL64((a21^d1), 10);
1482 b3 = ROL64((a32^d2), 15);
1483 b4 = ROL64((a43^d3), 56);
1484 b0 = ROL64((a04^d4), 27);
1485 a10 = b0 ^((~b1)& b2 );
1486 a21 = b1 ^((~b2)& b3 );
1487 a32 = b2 ^((~b3)& b4 );
1488 a43 = b3 ^((~b4)& b0 );
1489 a04 = b4 ^((~b0)& b1 );
1491 b3 = ROL64((a30^d0), 41);
1492 b4 = ROL64((a41^d1), 2);
1493 b0 = ROL64((a02^d2), 62);
1494 b1 = ROL64((a13^d3), 55);
1495 b2 = ROL64((a24^d4), 39);
1496 a30 = b0 ^((~b1)& b2 );
1497 a41 = b1 ^((~b2)& b3 );
1498 a02 = b2 ^((~b3)& b4 );
1499 a13 = b3 ^((~b4)& b0 );
1500 a24 = b4 ^((~b0)& b1 );
1502 c0 = a00^a20^a40^a10^a30;
1503 c1 = a11^a31^a01^a21^a41;
1504 c2 = a22^a42^a12^a32^a02;
1505 c3 = a33^a03^a23^a43^a13;
1506 c4 = a44^a14^a34^a04^a24;
1507 d0 = c4^ROL64(c1, 1);
1508 d1 = c0^ROL64(c2, 1);
1509 d2 = c1^ROL64(c3, 1);
1510 d3 = c2^ROL64(c4, 1);
1511 d4 = c3^ROL64(c0, 1);
1514 b1 = ROL64((a31^d1), 44);
1515 b2 = ROL64((a12^d2), 43);
1516 b3 = ROL64((a43^d3), 21);
1517 b4 = ROL64((a24^d4), 14);
1518 a00 = b0 ^((~b1)& b2 );
1520 a31 = b1 ^((~b2)& b3 );
1521 a12 = b2 ^((~b3)& b4 );
1522 a43 = b3 ^((~b4)& b0 );
1523 a24 = b4 ^((~b0)& b1 );
1525 b2 = ROL64((a40^d0), 3);
1526 b3 = ROL64((a21^d1), 45);
1527 b4 = ROL64((a02^d2), 61);
1528 b0 = ROL64((a33^d3), 28);
1529 b1 = ROL64((a14^d4), 20);
1530 a40 = b0 ^((~b1)& b2 );
1531 a21 = b1 ^((~b2)& b3 );
1532 a02 = b2 ^((~b3)& b4 );
1533 a33 = b3 ^((~b4)& b0 );
1534 a14 = b4 ^((~b0)& b1 );
1536 b4 = ROL64((a30^d0), 18);
1537 b0 = ROL64((a11^d1), 1);
1538 b1 = ROL64((a42^d2), 6);
1539 b2 = ROL64((a23^d3), 25);
1540 b3 = ROL64((a04^d4), 8);
1541 a30 = b0 ^((~b1)& b2 );
1542 a11 = b1 ^((~b2)& b3 );
1543 a42 = b2 ^((~b3)& b4 );
1544 a23 = b3 ^((~b4)& b0 );
1545 a04 = b4 ^((~b0)& b1 );
1547 b1 = ROL64((a20^d0), 36);
1548 b2 = ROL64((a01^d1), 10);
1549 b3 = ROL64((a32^d2), 15);
1550 b4 = ROL64((a13^d3), 56);
1551 b0 = ROL64((a44^d4), 27);
1552 a20 = b0 ^((~b1)& b2 );
1553 a01 = b1 ^((~b2)& b3 );
1554 a32 = b2 ^((~b3)& b4 );
1555 a13 = b3 ^((~b4)& b0 );
1556 a44 = b4 ^((~b0)& b1 );
1558 b3 = ROL64((a10^d0), 41);
1559 b4 = ROL64((a41^d1), 2);
1560 b0 = ROL64((a22^d2), 62);
1561 b1 = ROL64((a03^d3), 55);
1562 b2 = ROL64((a34^d4), 39);
1563 a10 = b0 ^((~b1)& b2 );
1564 a41 = b1 ^((~b2)& b3 );
1565 a22 = b2 ^((~b3)& b4 );
1566 a03 = b3 ^((~b4)& b0 );
1567 a34 = b4 ^((~b0)& b1 );
1569 c0 = a00^a40^a30^a20^a10;
1570 c1 = a31^a21^a11^a01^a41;
1571 c2 = a12^a02^a42^a32^a22;
1572 c3 = a43^a33^a23^a13^a03;
1573 c4 = a24^a14^a04^a44^a34;
1574 d0 = c4^ROL64(c1, 1);
1575 d1 = c0^ROL64(c2, 1);
1576 d2 = c1^ROL64(c3, 1);
1577 d3 = c2^ROL64(c4, 1);
1578 d4 = c3^ROL64(c0, 1);
1581 b1 = ROL64((a21^d1), 44);
1582 b2 = ROL64((a42^d2), 43);
1583 b3 = ROL64((a13^d3), 21);
1584 b4 = ROL64((a34^d4), 14);
1585 a00 = b0 ^((~b1)& b2 );
1587 a21 = b1 ^((~b2)& b3 );
1588 a42 = b2 ^((~b3)& b4 );
1589 a13 = b3 ^((~b4)& b0 );
1590 a34 = b4 ^((~b0)& b1 );
1592 b2 = ROL64((a30^d0), 3);
1593 b3 = ROL64((a01^d1), 45);
1594 b4 = ROL64((a22^d2), 61);
1595 b0 = ROL64((a43^d3), 28);
1596 b1 = ROL64((a14^d4), 20);
1597 a30 = b0 ^((~b1)& b2 );
1598 a01 = b1 ^((~b2)& b3 );
1599 a22 = b2 ^((~b3)& b4 );
1600 a43 = b3 ^((~b4)& b0 );
1601 a14 = b4 ^((~b0)& b1 );
1603 b4 = ROL64((a10^d0), 18);
1604 b0 = ROL64((a31^d1), 1);
1605 b1 = ROL64((a02^d2), 6);
1606 b2 = ROL64((a23^d3), 25);
1607 b3 = ROL64((a44^d4), 8);
1608 a10 = b0 ^((~b1)& b2 );
1609 a31 = b1 ^((~b2)& b3 );
1610 a02 = b2 ^((~b3)& b4 );
1611 a23 = b3 ^((~b4)& b0 );
1612 a44 = b4 ^((~b0)& b1 );
1614 b1 = ROL64((a40^d0), 36);
1615 b2 = ROL64((a11^d1), 10);
1616 b3 = ROL64((a32^d2), 15);
1617 b4 = ROL64((a03^d3), 56);
1618 b0 = ROL64((a24^d4), 27);
1619 a40 = b0 ^((~b1)& b2 );
1620 a11 = b1 ^((~b2)& b3 );
1621 a32 = b2 ^((~b3)& b4 );
1622 a03 = b3 ^((~b4)& b0 );
1623 a24 = b4 ^((~b0)& b1 );
1625 b3 = ROL64((a20^d0), 41);
1626 b4 = ROL64((a41^d1), 2);
1627 b0 = ROL64((a12^d2), 62);
1628 b1 = ROL64((a33^d3), 55);
1629 b2 = ROL64((a04^d4), 39);
1630 a20 = b0 ^((~b1)& b2 );
1631 a41 = b1 ^((~b2)& b3 );
1632 a12 = b2 ^((~b3)& b4 );
1633 a33 = b3 ^((~b4)& b0 );
1634 a04 = b4 ^((~b0)& b1 );
1636 c0 = a00^a30^a10^a40^a20;
1637 c1 = a21^a01^a31^a11^a41;
1638 c2 = a42^a22^a02^a32^a12;
1639 c3 = a13^a43^a23^a03^a33;
1640 c4 = a34^a14^a44^a24^a04;
1641 d0 = c4^ROL64(c1, 1);
1642 d1 = c0^ROL64(c2, 1);
1643 d2 = c1^ROL64(c3, 1);
1644 d3 = c2^ROL64(c4, 1);
1645 d4 = c3^ROL64(c0, 1);
1648 b1 = ROL64((a01^d1), 44);
1649 b2 = ROL64((a02^d2), 43);
1650 b3 = ROL64((a03^d3), 21);
1651 b4 = ROL64((a04^d4), 14);
1652 a00 = b0 ^((~b1)& b2 );
1654 a01 = b1 ^((~b2)& b3 );
1655 a02 = b2 ^((~b3)& b4 );
1656 a03 = b3 ^((~b4)& b0 );
1657 a04 = b4 ^((~b0)& b1 );
1659 b2 = ROL64((a10^d0), 3);
1660 b3 = ROL64((a11^d1), 45);
1661 b4 = ROL64((a12^d2), 61);
1662 b0 = ROL64((a13^d3), 28);
1663 b1 = ROL64((a14^d4), 20);
1664 a10 = b0 ^((~b1)& b2 );
1665 a11 = b1 ^((~b2)& b3 );
1666 a12 = b2 ^((~b3)& b4 );
1667 a13 = b3 ^((~b4)& b0 );
1668 a14 = b4 ^((~b0)& b1 );
1670 b4 = ROL64((a20^d0), 18);
1671 b0 = ROL64((a21^d1), 1);
1672 b1 = ROL64((a22^d2), 6);
1673 b2 = ROL64((a23^d3), 25);
1674 b3 = ROL64((a24^d4), 8);
1675 a20 = b0 ^((~b1)& b2 );
1676 a21 = b1 ^((~b2)& b3 );
1677 a22 = b2 ^((~b3)& b4 );
1678 a23 = b3 ^((~b4)& b0 );
1679 a24 = b4 ^((~b0)& b1 );
1681 b1 = ROL64((a30^d0), 36);
1682 b2 = ROL64((a31^d1), 10);
1683 b3 = ROL64((a32^d2), 15);
1684 b4 = ROL64((a33^d3), 56);
1685 b0 = ROL64((a34^d4), 27);
1686 a30 = b0 ^((~b1)& b2 );
1687 a31 = b1 ^((~b2)& b3 );
1688 a32 = b2 ^((~b3)& b4 );
1689 a33 = b3 ^((~b4)& b0 );
1690 a34 = b4 ^((~b0)& b1 );
1692 b3 = ROL64((a40^d0), 41);
1693 b4 = ROL64((a41^d1), 2);
1694 b0 = ROL64((a42^d2), 62);
1695 b1 = ROL64((a43^d3), 55);
1696 b2 = ROL64((a44^d4), 39);
1697 a40 = b0 ^((~b1)& b2 );
1698 a41 = b1 ^((~b2)& b3 );
1699 a42 = b2 ^((~b3)& b4 );
1700 a43 = b3 ^((~b4)& b0 );
1701 a44 = b4 ^((~b0)& b1 );
1706 ** Initialize a new hash. iSize determines the size of the hash
1707 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1708 ** can be zero to use the default hash size of 256 bits.
1710 static void SHA3Init(SHA3Context *p, int iSize){
1711 memset(p, 0, sizeof(*p));
1712 if( iSize>=128 && iSize<=512 ){
1713 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1715 p->nRate = (1600 - 2*256)/8;
1717 #if SHA3_BYTEORDER==1234
1718 /* Known to be little-endian at compile-time. No-op */
1719 #elif SHA3_BYTEORDER==4321
1720 p->ixMask = 7; /* Big-endian */
1723 static unsigned int one = 1;
1724 if( 1==*(unsigned char*)&one ){
1725 /* Little endian. No byte swapping. */
1728 /* Big endian. Byte swap. */
1736 ** Make consecutive calls to the SHA3Update function to add new content
1739 static void SHA3Update(
1741 const unsigned char *aData,
1745 #if SHA3_BYTEORDER==1234
1746 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1747 for(; i+7<nData; i+=8){
1748 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1750 if( p->nLoaded>=p->nRate ){
1757 for(; i<nData; i++){
1758 #if SHA3_BYTEORDER==1234
1759 p->u.x[p->nLoaded] ^= aData[i];
1760 #elif SHA3_BYTEORDER==4321
1761 p->u.x[p->nLoaded^0x07] ^= aData[i];
1763 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1766 if( p->nLoaded==p->nRate ){
1774 ** After all content has been added, invoke SHA3Final() to compute
1775 ** the final hash. The function returns a pointer to the binary
1778 static unsigned char *SHA3Final(SHA3Context *p){
1780 if( p->nLoaded==p->nRate-1 ){
1781 const unsigned char c1 = 0x86;
1782 SHA3Update(p, &c1, 1);
1784 const unsigned char c2 = 0x06;
1785 const unsigned char c3 = 0x80;
1786 SHA3Update(p, &c2, 1);
1787 p->nLoaded = p->nRate - 1;
1788 SHA3Update(p, &c3, 1);
1790 for(i=0; i<p->nRate; i++){
1791 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1793 return &p->u.x[p->nRate];
1795 /* End of the hashing logic
1796 *****************************************************************************/
1799 ** Implementation of the sha3(X,SIZE) function.
1801 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1802 ** size is 256. If X is a BLOB, it is hashed as is.
1803 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1804 ** and the string is hashed without the trailing 0x00 terminator. The hash
1805 ** of a NULL value is NULL.
1807 static void sha3Func(
1808 sqlite3_context *context,
1810 sqlite3_value **argv
1813 int eType = sqlite3_value_type(argv[0]);
1814 int nByte = sqlite3_value_bytes(argv[0]);
1819 iSize = sqlite3_value_int(argv[1]);
1820 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1821 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1826 if( eType==SQLITE_NULL ) return;
1827 SHA3Init(&cx, iSize);
1828 if( eType==SQLITE_BLOB ){
1829 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1831 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1833 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1836 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1837 ** of 50 bytes and add it to the hash.
1839 static void hash_step_vformat(
1840 SHA3Context *p, /* Add content to this context */
1841 const char *zFormat,
1847 va_start(ap, zFormat);
1848 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1850 n = (int)strlen(zBuf);
1851 SHA3Update(p, (unsigned char*)zBuf, n);
1855 ** Implementation of the sha3_query(SQL,SIZE) function.
1857 ** This function compiles and runs the SQL statement(s) given in the
1858 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1861 ** The format of the byte stream that is hashed is summarized as follows:
1871 ** <sql> is the original SQL text for each statement run and <n> is
1872 ** the size of that text. The SQL text is UTF-8. A single R character
1873 ** occurs before the start of each row. N means a NULL value.
1874 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1875 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1876 ** B means blobs of <size> bytes. T means text rendered as <size>
1877 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1880 ** For each SQL statement in the X input, there is one S segment. Each
1881 ** S segment is followed by zero or more R segments, one for each row in the
1882 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1883 ** one for each column in the result set. Segments are concatentated directly
1884 ** with no delimiters of any kind.
1886 static void sha3QueryFunc(
1887 sqlite3_context *context,
1889 sqlite3_value **argv
1891 sqlite3 *db = sqlite3_context_db_handle(context);
1892 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1893 sqlite3_stmt *pStmt = 0;
1894 int nCol; /* Number of columns in the result set */
1895 int i; /* Loop counter */
1905 iSize = sqlite3_value_int(argv[1]);
1906 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1907 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1912 if( zSql==0 ) return;
1913 SHA3Init(&cx, iSize);
1915 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1917 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1918 zSql, sqlite3_errmsg(db));
1919 sqlite3_finalize(pStmt);
1920 sqlite3_result_error(context, zMsg, -1);
1924 if( !sqlite3_stmt_readonly(pStmt) ){
1925 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1926 sqlite3_finalize(pStmt);
1927 sqlite3_result_error(context, zMsg, -1);
1931 nCol = sqlite3_column_count(pStmt);
1932 z = sqlite3_sql(pStmt);
1934 hash_step_vformat(&cx,"S%d:",n);
1935 SHA3Update(&cx,(unsigned char*)z,n);
1937 /* Compute a hash over the result of the query */
1938 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1939 SHA3Update(&cx,(const unsigned char*)"R",1);
1940 for(i=0; i<nCol; i++){
1941 switch( sqlite3_column_type(pStmt,i) ){
1943 SHA3Update(&cx, (const unsigned char*)"N",1);
1946 case SQLITE_INTEGER: {
1950 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1952 for(j=8; j>=1; j--){
1957 SHA3Update(&cx, x, 9);
1960 case SQLITE_FLOAT: {
1964 double r = sqlite3_column_double(pStmt,i);
1966 for(j=8; j>=1; j--){
1971 SHA3Update(&cx,x,9);
1975 int n2 = sqlite3_column_bytes(pStmt, i);
1976 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1977 hash_step_vformat(&cx,"T%d:",n2);
1978 SHA3Update(&cx, z2, n2);
1982 int n2 = sqlite3_column_bytes(pStmt, i);
1983 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1984 hash_step_vformat(&cx,"B%d:",n2);
1985 SHA3Update(&cx, z2, n2);
1991 sqlite3_finalize(pStmt);
1993 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2000 int sqlite3_shathree_init(
2003 const sqlite3_api_routines *pApi
2006 SQLITE_EXTENSION_INIT2(pApi);
2007 (void)pzErrMsg; /* Unused parameter */
2008 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2010 if( rc==SQLITE_OK ){
2011 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2014 if( rc==SQLITE_OK ){
2015 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2016 sha3QueryFunc, 0, 0);
2018 if( rc==SQLITE_OK ){
2019 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2020 sha3QueryFunc, 0, 0);
2025 /************************* End ../ext/misc/shathree.c ********************/
2026 /************************* Begin ../ext/misc/fileio.c ******************/
2030 ** The author disclaims copyright to this source code. In place of
2031 ** a legal notice, here is a blessing:
2033 ** May you do good and not evil.
2034 ** May you find forgiveness for yourself and forgive others.
2035 ** May you share freely, never taking more than you give.
2037 ******************************************************************************
2039 ** This SQLite extension implements SQL functions readfile() and
2040 ** writefile(), and eponymous virtual type "fsdir".
2042 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2044 ** If neither of the optional arguments is present, then this UDF
2045 ** function writes blob DATA to file FILE. If successful, the number
2046 ** of bytes written is returned. If an error occurs, NULL is returned.
2048 ** If the first option argument - MODE - is present, then it must
2049 ** be passed an integer value that corresponds to a POSIX mode
2050 ** value (file type + permissions, as returned in the stat.st_mode
2051 ** field by the stat() system call). Three types of files may
2052 ** be written/created:
2054 ** regular files: (mode & 0170000)==0100000
2055 ** symbolic links: (mode & 0170000)==0120000
2056 ** directories: (mode & 0170000)==0040000
2058 ** For a directory, the DATA is ignored. For a symbolic link, it is
2059 ** interpreted as text and used as the target of the link. For a
2060 ** regular file, it is interpreted as a blob and written into the
2061 ** named file. Regardless of the type of file, its permissions are
2062 ** set to (mode & 0777) before returning.
2064 ** If the optional MTIME argument is present, then it is interpreted
2065 ** as an integer - the number of seconds since the unix epoch. The
2066 ** modification-time of the target file is set to this value before
2069 ** If three or more arguments are passed to this function and an
2070 ** error is encountered, an exception is raised.
2074 ** Read and return the contents of file FILE (type blob) from disk.
2080 ** SELECT * FROM fsdir($path [, $dir]);
2082 ** Parameter $path is an absolute or relative pathname. If the file that it
2083 ** refers to does not exist, it is an error. If the path refers to a regular
2084 ** file or symbolic link, it returns a single row. Or, if the path refers
2085 ** to a directory, it returns one row for the directory, and one row for each
2086 ** file within the hierarchy rooted at $path.
2088 ** Each row has the following columns:
2090 ** name: Path to file or directory (text value).
2091 ** mode: Value of stat.st_mode for directory entry (an integer).
2092 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2093 ** data: For a regular file, a blob containing the file data. For a
2094 ** symlink, a text value containing the text of the link. For a
2097 ** If a non-NULL value is specified for the optional $dir parameter and
2098 ** $path is a relative path, then $path is interpreted relative to $dir.
2099 ** And the paths returned in the "name" column of the table are also
2100 ** relative to directory $dir.
2102 SQLITE_EXTENSION_INIT1
2107 #include <sys/types.h>
2108 #include <sys/stat.h>
2110 #if !defined(_WIN32) && !defined(WIN32)
2111 # include <unistd.h>
2112 # include <dirent.h>
2114 # include <sys/time.h>
2116 # include "windows.h"
2118 # include <direct.h>
2119 /* # include "test_windirent.h" */
2120 # define dirent DIRENT
2122 # define chmod _chmod
2127 # define mkdir(path,mode) _mkdir(path)
2128 # define lstat(path,buf) stat(path,buf)
2134 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2137 ** Set the result stored by context ctx to a blob containing the
2138 ** contents of file zName.
2140 static void readFileContents(sqlite3_context *ctx, const char *zName){
2145 in = fopen(zName, "rb");
2147 fseek(in, 0, SEEK_END);
2150 pBuf = sqlite3_malloc( nIn );
2151 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
2152 sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
2160 ** Implementation of the "readfile(X)" SQL function. The entire content
2161 ** of the file named X is read and returned as a BLOB. NULL is returned
2162 ** if the file does not exist or is unreadable.
2164 static void readfileFunc(
2165 sqlite3_context *context,
2167 sqlite3_value **argv
2170 (void)(argc); /* Unused parameter */
2171 zName = (const char*)sqlite3_value_text(argv[0]);
2172 if( zName==0 ) return;
2173 readFileContents(context, zName);
2177 ** Set the error message contained in context ctx to the results of
2178 ** vprintf(zFmt, ...).
2180 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2184 zMsg = sqlite3_vmprintf(zFmt, ap);
2185 sqlite3_result_error(ctx, zMsg, -1);
2192 ** This function is designed to convert a Win32 FILETIME structure into the
2193 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2195 static sqlite3_uint64 fileTimeToUnixTime(
2196 LPFILETIME pFileTime
2198 SYSTEMTIME epochSystemTime;
2199 ULARGE_INTEGER epochIntervals;
2200 FILETIME epochFileTime;
2201 ULARGE_INTEGER fileIntervals;
2203 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2204 epochSystemTime.wYear = 1970;
2205 epochSystemTime.wMonth = 1;
2206 epochSystemTime.wDay = 1;
2207 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2208 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2209 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2211 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2212 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2214 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2218 ** This function attempts to normalize the time values found in the stat()
2219 ** buffer to UTC. This is necessary on Win32, where the runtime library
2220 ** appears to return these values as local times.
2222 static void statTimesToUtc(
2224 struct stat *pStatBuf
2227 WIN32_FIND_DATAW fd;
2228 LPWSTR zUnicodeName;
2229 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2230 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2232 memset(&fd, 0, sizeof(WIN32_FIND_DATA));
2233 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2234 if( hFindFile!=NULL ){
2235 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2236 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2237 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2238 FindClose(hFindFile);
2240 sqlite3_free(zUnicodeName);
2246 ** This function is used in place of stat(). On Windows, special handling
2247 ** is required in order for the included time to be returned as UTC. On all
2248 ** other systems, this function simply calls stat().
2250 static int fileStat(
2252 struct stat *pStatBuf
2255 int rc = stat(zPath, pStatBuf);
2256 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2259 return stat(zPath, pStatBuf);
2264 ** This function is used in place of lstat(). On Windows, special handling
2265 ** is required in order for the included time to be returned as UTC. On all
2266 ** other systems, this function simply calls lstat().
2268 static int fileLinkStat(
2270 struct stat *pStatBuf
2273 int rc = lstat(zPath, pStatBuf);
2274 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2277 return lstat(zPath, pStatBuf);
2282 ** Argument zFile is the name of a file that will be created and/or written
2283 ** by SQL function writefile(). This function ensures that the directory
2284 ** zFile will be written to exists, creating it if required. The permissions
2285 ** for any path components created by this function are set to (mode&0777).
2287 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2288 ** SQLITE_OK is returned if the directory is successfully created, or
2289 ** SQLITE_ERROR otherwise.
2291 static int makeDirectory(
2295 char *zCopy = sqlite3_mprintf("%s", zFile);
2301 int nCopy = (int)strlen(zCopy);
2304 while( rc==SQLITE_OK ){
2308 for(; zCopy[i]!='/' && i<nCopy; i++);
2309 if( i==nCopy ) break;
2312 rc2 = fileStat(zCopy, &sStat);
2314 if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2316 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2322 sqlite3_free(zCopy);
2329 ** This function does the work for the writefile() UDF. Refer to
2330 ** header comments at the top of this file for details.
2332 static int writeFile(
2333 sqlite3_context *pCtx, /* Context to return bytes written in */
2334 const char *zFile, /* File to write */
2335 sqlite3_value *pData, /* Data to write */
2336 mode_t mode, /* MODE parameter passed to writefile() */
2337 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2339 #if !defined(_WIN32) && !defined(WIN32)
2340 if( S_ISLNK(mode) ){
2341 const char *zTo = (const char*)sqlite3_value_text(pData);
2342 if( symlink(zTo, zFile)<0 ) return 1;
2346 if( S_ISDIR(mode) ){
2347 if( mkdir(zFile, mode) ){
2348 /* The mkdir() call to create the directory failed. This might not
2349 ** be an error though - if there is already a directory at the same
2350 ** path and either the permissions already match or can be changed
2351 ** to do so using chmod(), it is not an error. */
2354 || 0!=fileStat(zFile, &sStat)
2355 || !S_ISDIR(sStat.st_mode)
2356 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2362 sqlite3_int64 nWrite = 0;
2365 FILE *out = fopen(zFile, "wb");
2366 if( out==0 ) return 1;
2367 z = (const char*)sqlite3_value_blob(pData);
2369 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2370 nWrite = sqlite3_value_bytes(pData);
2376 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2380 sqlite3_result_int64(pCtx, nWrite);
2387 FILETIME lastAccess;
2389 SYSTEMTIME currentTime;
2392 LPWSTR zUnicodeName;
2393 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2395 GetSystemTime(¤tTime);
2396 SystemTimeToFileTime(¤tTime, &lastAccess);
2397 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2398 lastWrite.dwLowDateTime = (DWORD)intervals;
2399 lastWrite.dwHighDateTime = intervals >> 32;
2400 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2401 if( zUnicodeName==0 ){
2404 hFile = CreateFileW(
2405 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2406 FILE_FLAG_BACKUP_SEMANTICS, NULL
2408 sqlite3_free(zUnicodeName);
2409 if( hFile!=INVALID_HANDLE_VALUE ){
2410 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2416 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2418 struct timespec times[2];
2419 times[0].tv_nsec = times[1].tv_nsec = 0;
2420 times[0].tv_sec = time(0);
2421 times[1].tv_sec = mtime;
2422 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2427 struct timeval times[2];
2428 times[0].tv_usec = times[1].tv_usec = 0;
2429 times[0].tv_sec = time(0);
2430 times[1].tv_sec = mtime;
2431 if( utimes(zFile, times) ){
2441 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2442 ** Refer to header comments at the top of this file for details.
2444 static void writefileFunc(
2445 sqlite3_context *context,
2447 sqlite3_value **argv
2452 sqlite3_int64 mtime = -1;
2454 if( argc<2 || argc>4 ){
2455 sqlite3_result_error(context,
2456 "wrong number of arguments to function writefile()", -1
2461 zFile = (const char*)sqlite3_value_text(argv[0]);
2462 if( zFile==0 ) return;
2464 mode = (mode_t)sqlite3_value_int(argv[2]);
2467 mtime = sqlite3_value_int64(argv[3]);
2470 res = writeFile(context, zFile, argv[1], mode, mtime);
2471 if( res==1 && errno==ENOENT ){
2472 if( makeDirectory(zFile, mode)==SQLITE_OK ){
2473 res = writeFile(context, zFile, argv[1], mode, mtime);
2477 if( argc>2 && res!=0 ){
2478 if( S_ISLNK(mode) ){
2479 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2480 }else if( S_ISDIR(mode) ){
2481 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2483 ctxErrorMsg(context, "failed to write file: %s", zFile);
2489 ** SQL function: lsmode(MODE)
2491 ** Given a numberic st_mode from stat(), convert it into a human-readable
2492 ** text string in the style of "ls -l".
2494 static void lsModeFunc(
2495 sqlite3_context *context,
2497 sqlite3_value **argv
2500 int iMode = sqlite3_value_int(argv[0]);
2503 if( S_ISLNK(iMode) ){
2505 }else if( S_ISREG(iMode) ){
2507 }else if( S_ISDIR(iMode) ){
2513 int m = (iMode >> ((2-i)*3));
2514 char *a = &z[1 + i*3];
2515 a[0] = (m & 0x4) ? 'r' : '-';
2516 a[1] = (m & 0x2) ? 'w' : '-';
2517 a[2] = (m & 0x1) ? 'x' : '-';
2520 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2523 #ifndef SQLITE_OMIT_VIRTUALTABLE
2526 ** Cursor type for recursively iterating through a directory structure.
2528 typedef struct fsdir_cursor fsdir_cursor;
2529 typedef struct FsdirLevel FsdirLevel;
2532 DIR *pDir; /* From opendir() */
2533 char *zDir; /* Name of directory (nul-terminated) */
2536 struct fsdir_cursor {
2537 sqlite3_vtab_cursor base; /* Base class - must be first */
2539 int nLvl; /* Number of entries in aLvl[] array */
2540 int iLvl; /* Index of current entry */
2541 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2546 struct stat sStat; /* Current lstat() results */
2547 char *zPath; /* Path to current entry */
2548 sqlite3_int64 iRowid; /* Current rowid */
2551 typedef struct fsdir_tab fsdir_tab;
2553 sqlite3_vtab base; /* Base class - must be first */
2557 ** Construct a new fsdir virtual table object.
2559 static int fsdirConnect(
2562 int argc, const char *const*argv,
2563 sqlite3_vtab **ppVtab,
2566 fsdir_tab *pNew = 0;
2572 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2573 if( rc==SQLITE_OK ){
2574 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2575 if( pNew==0 ) return SQLITE_NOMEM;
2576 memset(pNew, 0, sizeof(*pNew));
2578 *ppVtab = (sqlite3_vtab*)pNew;
2583 ** This method is the destructor for fsdir vtab objects.
2585 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2586 sqlite3_free(pVtab);
2591 ** Constructor for a new fsdir_cursor object.
2593 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2596 pCur = sqlite3_malloc( sizeof(*pCur) );
2597 if( pCur==0 ) return SQLITE_NOMEM;
2598 memset(pCur, 0, sizeof(*pCur));
2600 *ppCursor = &pCur->base;
2605 ** Reset a cursor back to the state it was in when first returned
2608 static void fsdirResetCursor(fsdir_cursor *pCur){
2610 for(i=0; i<=pCur->iLvl; i++){
2611 FsdirLevel *pLvl = &pCur->aLvl[i];
2612 if( pLvl->pDir ) closedir(pLvl->pDir);
2613 sqlite3_free(pLvl->zDir);
2615 sqlite3_free(pCur->zPath);
2616 sqlite3_free(pCur->aLvl);
2627 ** Destructor for an fsdir_cursor.
2629 static int fsdirClose(sqlite3_vtab_cursor *cur){
2630 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2632 fsdirResetCursor(pCur);
2638 ** Set the error message for the virtual table associated with cursor
2639 ** pCur to the results of vprintf(zFmt, ...).
2641 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2644 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2650 ** Advance an fsdir_cursor to its next row of output.
2652 static int fsdirNext(sqlite3_vtab_cursor *cur){
2653 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2654 mode_t m = pCur->sStat.st_mode;
2658 /* Descend into this directory */
2659 int iNew = pCur->iLvl + 1;
2661 if( iNew>=pCur->nLvl ){
2663 int nByte = nNew*sizeof(FsdirLevel);
2664 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
2665 if( aNew==0 ) return SQLITE_NOMEM;
2666 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2671 pLvl = &pCur->aLvl[iNew];
2673 pLvl->zDir = pCur->zPath;
2675 pLvl->pDir = opendir(pLvl->zDir);
2676 if( pLvl->pDir==0 ){
2677 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2678 return SQLITE_ERROR;
2682 while( pCur->iLvl>=0 ){
2683 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2684 struct dirent *pEntry = readdir(pLvl->pDir);
2686 if( pEntry->d_name[0]=='.' ){
2687 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2688 if( pEntry->d_name[1]=='\0' ) continue;
2690 sqlite3_free(pCur->zPath);
2691 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2692 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2693 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2694 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2695 return SQLITE_ERROR;
2699 closedir(pLvl->pDir);
2700 sqlite3_free(pLvl->zDir);
2707 sqlite3_free(pCur->zPath);
2713 ** Return values of columns for the row at which the series_cursor
2714 ** is currently pointing.
2716 static int fsdirColumn(
2717 sqlite3_vtab_cursor *cur, /* The cursor */
2718 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2719 int i /* Which column to return */
2721 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2723 case 0: { /* name */
2724 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2729 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2733 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2736 case 3: { /* data */
2737 mode_t m = pCur->sStat.st_mode;
2739 sqlite3_result_null(ctx);
2740 #if !defined(_WIN32) && !defined(WIN32)
2741 }else if( S_ISLNK(m) ){
2743 char *aBuf = aStatic;
2748 n = readlink(pCur->zPath, aBuf, nBuf);
2750 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2752 aBuf = sqlite3_malloc(nBuf);
2754 sqlite3_result_error_nomem(ctx);
2755 return SQLITE_NOMEM;
2759 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2760 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2763 readFileContents(ctx, pCur->zPath);
2771 ** Return the rowid for the current row. In this implementation, the
2772 ** first row returned is assigned rowid value 1, and each subsequent
2773 ** row a value 1 more than that of the previous.
2775 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2776 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2777 *pRowid = pCur->iRowid;
2782 ** Return TRUE if the cursor has been moved off of the last
2785 static int fsdirEof(sqlite3_vtab_cursor *cur){
2786 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2787 return (pCur->zPath==0);
2791 ** xFilter callback.
2793 static int fsdirFilter(
2794 sqlite3_vtab_cursor *cur,
2795 int idxNum, const char *idxStr,
2796 int argc, sqlite3_value **argv
2798 const char *zDir = 0;
2799 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2801 fsdirResetCursor(pCur);
2804 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2805 return SQLITE_ERROR;
2808 assert( argc==idxNum && (argc==1 || argc==2) );
2809 zDir = (const char*)sqlite3_value_text(argv[0]);
2811 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2812 return SQLITE_ERROR;
2815 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2818 pCur->nBase = (int)strlen(pCur->zBase)+1;
2819 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2821 pCur->zPath = sqlite3_mprintf("%s", zDir);
2824 if( pCur->zPath==0 ){
2825 return SQLITE_NOMEM;
2827 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2828 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2829 return SQLITE_ERROR;
2836 ** SQLite will invoke this method one or more times while planning a query
2837 ** that uses the generate_series virtual table. This routine needs to create
2838 ** a query plan for each invocation and compute an estimated cost for that
2841 ** In this implementation idxNum is used to represent the
2842 ** query plan. idxStr is unused.
2844 ** The query plan is represented by bits in idxNum:
2846 ** (1) start = $value -- constraint exists
2847 ** (2) stop = $value -- constraint exists
2848 ** (4) step = $value -- constraint exists
2849 ** (8) output in descending order
2851 static int fsdirBestIndex(
2853 sqlite3_index_info *pIdxInfo
2855 int i; /* Loop over constraints */
2858 const struct sqlite3_index_constraint *pConstraint;
2861 pConstraint = pIdxInfo->aConstraint;
2862 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2863 if( pConstraint->usable==0 ) continue;
2864 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2865 if( pConstraint->iColumn==4 ) idx4 = i;
2866 if( pConstraint->iColumn==5 ) idx5 = i;
2870 pIdxInfo->idxNum = 0;
2871 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
2873 pIdxInfo->aConstraintUsage[idx4].omit = 1;
2874 pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
2876 pIdxInfo->aConstraintUsage[idx5].omit = 1;
2877 pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
2878 pIdxInfo->idxNum = 2;
2879 pIdxInfo->estimatedCost = 10.0;
2881 pIdxInfo->idxNum = 1;
2882 pIdxInfo->estimatedCost = 100.0;
2890 ** Register the "fsdir" virtual table.
2892 static int fsdirRegister(sqlite3 *db){
2893 static sqlite3_module fsdirModule = {
2896 fsdirConnect, /* xConnect */
2897 fsdirBestIndex, /* xBestIndex */
2898 fsdirDisconnect, /* xDisconnect */
2900 fsdirOpen, /* xOpen - open a cursor */
2901 fsdirClose, /* xClose - close a cursor */
2902 fsdirFilter, /* xFilter - configure scan constraints */
2903 fsdirNext, /* xNext - advance a cursor */
2904 fsdirEof, /* xEof - check for end of scan */
2905 fsdirColumn, /* xColumn - read data */
2906 fsdirRowid, /* xRowid - read data */
2912 0, /* xFindMethod */
2919 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2922 #else /* SQLITE_OMIT_VIRTUALTABLE */
2923 # define fsdirRegister(x) SQLITE_OK
2929 int sqlite3_fileio_init(
2932 const sqlite3_api_routines *pApi
2935 SQLITE_EXTENSION_INIT2(pApi);
2936 (void)pzErrMsg; /* Unused parameter */
2937 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
2938 readfileFunc, 0, 0);
2939 if( rc==SQLITE_OK ){
2940 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2941 writefileFunc, 0, 0);
2943 if( rc==SQLITE_OK ){
2944 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2947 if( rc==SQLITE_OK ){
2948 rc = fsdirRegister(db);
2953 /************************* End ../ext/misc/fileio.c ********************/
2954 /************************* Begin ../ext/misc/completion.c ******************/
2958 ** The author disclaims copyright to this source code. In place of
2959 ** a legal notice, here is a blessing:
2961 ** May you do good and not evil.
2962 ** May you find forgiveness for yourself and forgive others.
2963 ** May you share freely, never taking more than you give.
2965 *************************************************************************
2967 ** This file implements an eponymous virtual table that returns suggested
2968 ** completions for a partial SQL input.
2972 ** SELECT DISTINCT candidate COLLATE nocase
2973 ** FROM completion($prefix,$wholeline)
2976 ** The two query parameters are optional. $prefix is the text of the
2977 ** current word being typed and that is to be completed. $wholeline is
2978 ** the complete input line, used for context.
2980 ** The raw completion() table might return the same candidate multiple
2981 ** times, for example if the same column name is used to two or more
2982 ** tables. And the candidates are returned in an arbitrary order. Hence,
2983 ** the DISTINCT and ORDER BY are recommended.
2985 ** This virtual table operates at the speed of human typing, and so there
2986 ** is no attempt to make it fast. Even a slow implementation will be much
2987 ** faster than any human can type.
2990 SQLITE_EXTENSION_INIT1
2995 #ifndef SQLITE_OMIT_VIRTUALTABLE
2997 /* completion_vtab is a subclass of sqlite3_vtab which will
2998 ** serve as the underlying representation of a completion virtual table
3000 typedef struct completion_vtab completion_vtab;
3001 struct completion_vtab {
3002 sqlite3_vtab base; /* Base class - must be first */
3003 sqlite3 *db; /* Database connection for this completion vtab */
3006 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3007 ** serve as the underlying representation of a cursor that scans
3008 ** over rows of the result
3010 typedef struct completion_cursor completion_cursor;
3011 struct completion_cursor {
3012 sqlite3_vtab_cursor base; /* Base class - must be first */
3013 sqlite3 *db; /* Database connection for this cursor */
3014 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3015 char *zPrefix; /* The prefix for the word we want to complete */
3016 char *zLine; /* The whole that we want to complete */
3017 const char *zCurrentRow; /* Current output row */
3018 int szRow; /* Length of the zCurrentRow string */
3019 sqlite3_stmt *pStmt; /* Current statement */
3020 sqlite3_int64 iRowid; /* The rowid */
3021 int ePhase; /* Current phase */
3022 int j; /* inter-phase counter */
3025 /* Values for ePhase:
3027 #define COMPLETION_FIRST_PHASE 1
3028 #define COMPLETION_KEYWORDS 1
3029 #define COMPLETION_PRAGMAS 2
3030 #define COMPLETION_FUNCTIONS 3
3031 #define COMPLETION_COLLATIONS 4
3032 #define COMPLETION_INDEXES 5
3033 #define COMPLETION_TRIGGERS 6
3034 #define COMPLETION_DATABASES 7
3035 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3036 #define COMPLETION_COLUMNS 9
3037 #define COMPLETION_MODULES 10
3038 #define COMPLETION_EOF 11
3041 ** The completionConnect() method is invoked to create a new
3042 ** completion_vtab that describes the completion virtual table.
3044 ** Think of this routine as the constructor for completion_vtab objects.
3046 ** All this routine needs to do is:
3048 ** (1) Allocate the completion_vtab object and initialize all fields.
3050 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3051 ** result set of queries against completion will look like.
3053 static int completionConnect(
3056 int argc, const char *const*argv,
3057 sqlite3_vtab **ppVtab,
3060 completion_vtab *pNew;
3063 (void)(pAux); /* Unused parameter */
3064 (void)(argc); /* Unused parameter */
3065 (void)(argv); /* Unused parameter */
3066 (void)(pzErr); /* Unused parameter */
3068 /* Column numbers */
3069 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3070 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3071 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3072 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3074 rc = sqlite3_declare_vtab(db,
3077 " prefix TEXT HIDDEN,"
3078 " wholeline TEXT HIDDEN,"
3079 " phase INT HIDDEN" /* Used for debugging only */
3081 if( rc==SQLITE_OK ){
3082 pNew = sqlite3_malloc( sizeof(*pNew) );
3083 *ppVtab = (sqlite3_vtab*)pNew;
3084 if( pNew==0 ) return SQLITE_NOMEM;
3085 memset(pNew, 0, sizeof(*pNew));
3092 ** This method is the destructor for completion_cursor objects.
3094 static int completionDisconnect(sqlite3_vtab *pVtab){
3095 sqlite3_free(pVtab);
3100 ** Constructor for a new completion_cursor object.
3102 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3103 completion_cursor *pCur;
3104 pCur = sqlite3_malloc( sizeof(*pCur) );
3105 if( pCur==0 ) return SQLITE_NOMEM;
3106 memset(pCur, 0, sizeof(*pCur));
3107 pCur->db = ((completion_vtab*)p)->db;
3108 *ppCursor = &pCur->base;
3113 ** Reset the completion_cursor.
3115 static void completionCursorReset(completion_cursor *pCur){
3116 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3117 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3118 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3123 ** Destructor for a completion_cursor.
3125 static int completionClose(sqlite3_vtab_cursor *cur){
3126 completionCursorReset((completion_cursor*)cur);
3132 ** Advance a completion_cursor to its next row of output.
3134 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3135 ** record the current state of the scan. This routine sets ->zCurrentRow
3136 ** to the current row of output and then returns. If no more rows remain,
3137 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3138 ** table that has reached the end of its scan.
3140 ** The current implementation just lists potential identifiers and
3141 ** keywords and filters them by zPrefix. Future enhancements should
3142 ** take zLine into account to try to restrict the set of identifiers and
3143 ** keywords based on what would be legal at the current point of input.
3145 static int completionNext(sqlite3_vtab_cursor *cur){
3146 completion_cursor *pCur = (completion_cursor*)cur;
3147 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3148 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3150 while( pCur->ePhase!=COMPLETION_EOF ){
3151 switch( pCur->ePhase ){
3152 case COMPLETION_KEYWORDS: {
3153 if( pCur->j >= sqlite3_keyword_count() ){
3154 pCur->zCurrentRow = 0;
3155 pCur->ePhase = COMPLETION_DATABASES;
3157 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3162 case COMPLETION_DATABASES: {
3163 if( pCur->pStmt==0 ){
3164 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3168 eNextPhase = COMPLETION_TABLES;
3171 case COMPLETION_TABLES: {
3172 if( pCur->pStmt==0 ){
3175 const char *zSep = "";
3176 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3177 while( sqlite3_step(pS2)==SQLITE_ROW ){
3178 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3179 zSql = sqlite3_mprintf(
3181 "SELECT name FROM \"%w\".sqlite_master",
3184 if( zSql==0 ) return SQLITE_NOMEM;
3187 sqlite3_finalize(pS2);
3188 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3192 eNextPhase = COMPLETION_COLUMNS;
3195 case COMPLETION_COLUMNS: {
3196 if( pCur->pStmt==0 ){
3199 const char *zSep = "";
3200 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3201 while( sqlite3_step(pS2)==SQLITE_ROW ){
3202 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3203 zSql = sqlite3_mprintf(
3205 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3206 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3207 " WHERE sm.type='table'",
3208 zSql, zSep, zDb, zDb
3210 if( zSql==0 ) return SQLITE_NOMEM;
3213 sqlite3_finalize(pS2);
3214 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3218 eNextPhase = COMPLETION_EOF;
3223 /* This case is when the phase presets zCurrentRow */
3224 if( pCur->zCurrentRow==0 ) continue;
3226 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3227 /* Extract the next row of content */
3228 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3229 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3231 /* When all rows are finished, advance to the next phase */
3232 sqlite3_finalize(pCur->pStmt);
3234 pCur->ePhase = eNextPhase;
3238 if( pCur->nPrefix==0 ) break;
3239 if( pCur->nPrefix<=pCur->szRow
3240 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3250 ** Return values of columns for the row at which the completion_cursor
3251 ** is currently pointing.
3253 static int completionColumn(
3254 sqlite3_vtab_cursor *cur, /* The cursor */
3255 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3256 int i /* Which column to return */
3258 completion_cursor *pCur = (completion_cursor*)cur;
3260 case COMPLETION_COLUMN_CANDIDATE: {
3261 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3264 case COMPLETION_COLUMN_PREFIX: {
3265 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3268 case COMPLETION_COLUMN_WHOLELINE: {
3269 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3272 case COMPLETION_COLUMN_PHASE: {
3273 sqlite3_result_int(ctx, pCur->ePhase);
3281 ** Return the rowid for the current row. In this implementation, the
3282 ** rowid is the same as the output value.
3284 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3285 completion_cursor *pCur = (completion_cursor*)cur;
3286 *pRowid = pCur->iRowid;
3291 ** Return TRUE if the cursor has been moved off of the last
3294 static int completionEof(sqlite3_vtab_cursor *cur){
3295 completion_cursor *pCur = (completion_cursor*)cur;
3296 return pCur->ePhase >= COMPLETION_EOF;
3300 ** This method is called to "rewind" the completion_cursor object back
3301 ** to the first row of output. This method is always called at least
3302 ** once prior to any call to completionColumn() or completionRowid() or
3305 static int completionFilter(
3306 sqlite3_vtab_cursor *pVtabCursor,
3307 int idxNum, const char *idxStr,
3308 int argc, sqlite3_value **argv
3310 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3312 (void)(idxStr); /* Unused parameter */
3313 (void)(argc); /* Unused parameter */
3314 completionCursorReset(pCur);
3316 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3317 if( pCur->nPrefix>0 ){
3318 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3319 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3324 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3325 if( pCur->nLine>0 ){
3326 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3327 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3331 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3332 int i = pCur->nLine;
3333 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3336 pCur->nPrefix = pCur->nLine - i;
3337 if( pCur->nPrefix>0 ){
3338 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3339 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3343 pCur->ePhase = COMPLETION_FIRST_PHASE;
3344 return completionNext(pVtabCursor);
3348 ** SQLite will invoke this method one or more times while planning a query
3349 ** that uses the completion virtual table. This routine needs to create
3350 ** a query plan for each invocation and compute an estimated cost for that
3353 ** There are two hidden parameters that act as arguments to the table-valued
3354 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3355 ** is available and bit 1 is set if "wholeline" is available.
3357 static int completionBestIndex(
3359 sqlite3_index_info *pIdxInfo
3361 int i; /* Loop over constraints */
3362 int idxNum = 0; /* The query plan bitmask */
3363 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3364 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3365 int nArg = 0; /* Number of arguments that completeFilter() expects */
3366 const struct sqlite3_index_constraint *pConstraint;
3368 (void)(tab); /* Unused parameter */
3369 pConstraint = pIdxInfo->aConstraint;
3370 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3371 if( pConstraint->usable==0 ) continue;
3372 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3373 switch( pConstraint->iColumn ){
3374 case COMPLETION_COLUMN_PREFIX:
3378 case COMPLETION_COLUMN_WHOLELINE:
3385 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3386 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3388 if( wholelineIdx>=0 ){
3389 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3390 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3392 pIdxInfo->idxNum = idxNum;
3393 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3394 pIdxInfo->estimatedRows = 500 - 100*nArg;
3399 ** This following structure defines all the methods for the
3400 ** completion virtual table.
3402 static sqlite3_module completionModule = {
3405 completionConnect, /* xConnect */
3406 completionBestIndex, /* xBestIndex */
3407 completionDisconnect, /* xDisconnect */
3409 completionOpen, /* xOpen - open a cursor */
3410 completionClose, /* xClose - close a cursor */
3411 completionFilter, /* xFilter - configure scan constraints */
3412 completionNext, /* xNext - advance a cursor */
3413 completionEof, /* xEof - check for end of scan */
3414 completionColumn, /* xColumn - read data */
3415 completionRowid, /* xRowid - read data */
3421 0, /* xFindMethod */
3428 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3430 int sqlite3CompletionVtabInit(sqlite3 *db){
3432 #ifndef SQLITE_OMIT_VIRTUALTABLE
3433 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3441 int sqlite3_completion_init(
3444 const sqlite3_api_routines *pApi
3447 SQLITE_EXTENSION_INIT2(pApi);
3448 (void)(pzErrMsg); /* Unused parameter */
3449 #ifndef SQLITE_OMIT_VIRTUALTABLE
3450 rc = sqlite3CompletionVtabInit(db);
3455 /************************* End ../ext/misc/completion.c ********************/
3456 /************************* Begin ../ext/misc/appendvfs.c ******************/
3460 ** The author disclaims copyright to this source code. In place of
3461 ** a legal notice, here is a blessing:
3463 ** May you do good and not evil.
3464 ** May you find forgiveness for yourself and forgive others.
3465 ** May you share freely, never taking more than you give.
3467 ******************************************************************************
3469 ** This file implements a VFS shim that allows an SQLite database to be
3470 ** appended onto the end of some other file, such as an executable.
3472 ** A special record must appear at the end of the file that identifies the
3473 ** file as an appended database and provides an offset to page 1. For
3474 ** best performance page 1 should be located at a disk page boundary, though
3475 ** that is not required.
3477 ** When opening a database using this VFS, the connection might treat
3478 ** the file as an ordinary SQLite database, or it might treat is as a
3479 ** database appended onto some other file. Here are the rules:
3481 ** (1) When opening a new empty file, that file is treated as an ordinary
3484 ** (2) When opening a file that begins with the standard SQLite prefix
3485 ** string "SQLite format 3", that file is treated as an ordinary
3488 ** (3) When opening a file that ends with the appendvfs trailer string
3489 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3492 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3493 ** set, then a new database is appended to the already existing file.
3495 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3497 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3498 ** the file containing the database is limited to 1GB. This VFS will refuse
3499 ** to read or write past the 1GB mark. This restriction might be lifted in
3500 ** future versions. For now, if you need a large database, then keep the
3501 ** database in a separate file.
3503 ** If the file being opened is not an appended database, then this shim is
3504 ** a pass-through into the default underlying VFS.
3506 SQLITE_EXTENSION_INIT1
3510 /* The append mark at the end of the database is:
3512 ** Start-Of-SQLite3-NNNNNNNN
3513 ** 123456789 123456789 12345
3515 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3516 ** the offset to page 1.
3518 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3519 #define APND_MARK_PREFIX_SZ 17
3520 #define APND_MARK_SIZE 25
3523 ** Maximum size of the combined prefix + database + append-mark. This
3524 ** must be less than 0x40000000 to avoid locking issues on Windows.
3526 #define APND_MAX_SIZE (65536*15259)
3529 ** Forward declaration of objects used by this utility
3531 typedef struct sqlite3_vfs ApndVfs;
3532 typedef struct ApndFile ApndFile;
3534 /* Access to a lower-level VFS that (might) implement dynamic loading,
3535 ** access to randomness, etc.
3537 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3538 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3542 sqlite3_file base; /* IO methods */
3543 sqlite3_int64 iPgOne; /* File offset to page 1 */
3544 sqlite3_int64 iMark; /* Start of the append-mark */
3548 ** Methods for ApndFile
3550 static int apndClose(sqlite3_file*);
3551 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3552 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3553 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3554 static int apndSync(sqlite3_file*, int flags);
3555 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3556 static int apndLock(sqlite3_file*, int);
3557 static int apndUnlock(sqlite3_file*, int);
3558 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3559 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3560 static int apndSectorSize(sqlite3_file*);
3561 static int apndDeviceCharacteristics(sqlite3_file*);
3562 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3563 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3564 static void apndShmBarrier(sqlite3_file*);
3565 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3566 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3567 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3570 ** Methods for ApndVfs
3572 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3573 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3574 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3575 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3576 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3577 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3578 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3579 static void apndDlClose(sqlite3_vfs*, void*);
3580 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3581 static int apndSleep(sqlite3_vfs*, int microseconds);
3582 static int apndCurrentTime(sqlite3_vfs*, double*);
3583 static int apndGetLastError(sqlite3_vfs*, int, char *);
3584 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3585 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3586 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3587 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3589 static sqlite3_vfs apnd_vfs = {
3590 3, /* iVersion (set when registered) */
3591 0, /* szOsFile (set when registered) */
3592 1024, /* mxPathname */
3594 "apndvfs", /* zName */
3595 0, /* pAppData (set when registered) */
3596 apndOpen, /* xOpen */
3597 apndDelete, /* xDelete */
3598 apndAccess, /* xAccess */
3599 apndFullPathname, /* xFullPathname */
3600 apndDlOpen, /* xDlOpen */
3601 apndDlError, /* xDlError */
3602 apndDlSym, /* xDlSym */
3603 apndDlClose, /* xDlClose */
3604 apndRandomness, /* xRandomness */
3605 apndSleep, /* xSleep */
3606 apndCurrentTime, /* xCurrentTime */
3607 apndGetLastError, /* xGetLastError */
3608 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3609 apndSetSystemCall, /* xSetSystemCall */
3610 apndGetSystemCall, /* xGetSystemCall */
3611 apndNextSystemCall /* xNextSystemCall */
3614 static const sqlite3_io_methods apnd_io_methods = {
3616 apndClose, /* xClose */
3617 apndRead, /* xRead */
3618 apndWrite, /* xWrite */
3619 apndTruncate, /* xTruncate */
3620 apndSync, /* xSync */
3621 apndFileSize, /* xFileSize */
3622 apndLock, /* xLock */
3623 apndUnlock, /* xUnlock */
3624 apndCheckReservedLock, /* xCheckReservedLock */
3625 apndFileControl, /* xFileControl */
3626 apndSectorSize, /* xSectorSize */
3627 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3628 apndShmMap, /* xShmMap */
3629 apndShmLock, /* xShmLock */
3630 apndShmBarrier, /* xShmBarrier */
3631 apndShmUnmap, /* xShmUnmap */
3632 apndFetch, /* xFetch */
3633 apndUnfetch /* xUnfetch */
3639 ** Close an apnd-file.
3641 static int apndClose(sqlite3_file *pFile){
3642 pFile = ORIGFILE(pFile);
3643 return pFile->pMethods->xClose(pFile);
3647 ** Read data from an apnd-file.
3649 static int apndRead(
3650 sqlite3_file *pFile,
3655 ApndFile *p = (ApndFile *)pFile;
3656 pFile = ORIGFILE(pFile);
3657 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3661 ** Add the append-mark onto the end of the file.
3663 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3665 unsigned char a[APND_MARK_SIZE];
3666 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3668 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3670 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3674 ** Write data to an apnd-file.
3676 static int apndWrite(
3677 sqlite3_file *pFile,
3683 ApndFile *p = (ApndFile *)pFile;
3684 pFile = ORIGFILE(pFile);
3685 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3686 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3687 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3688 sqlite3_int64 sz = 0;
3689 rc = pFile->pMethods->xFileSize(pFile, &sz);
3690 if( rc==SQLITE_OK ){
3691 p->iMark = sz - APND_MARK_SIZE;
3692 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3693 p->iMark = p->iPgOne + iOfst + iAmt;
3694 rc = apndWriteMark(p, pFile);
3702 ** Truncate an apnd-file.
3704 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3706 ApndFile *p = (ApndFile *)pFile;
3707 pFile = ORIGFILE(pFile);
3708 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3709 if( rc==SQLITE_OK ){
3710 p->iMark = p->iPgOne+size;
3711 rc = apndWriteMark(p, pFile);
3717 ** Sync an apnd-file.
3719 static int apndSync(sqlite3_file *pFile, int flags){
3720 pFile = ORIGFILE(pFile);
3721 return pFile->pMethods->xSync(pFile, flags);
3725 ** Return the current file-size of an apnd-file.
3727 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3728 ApndFile *p = (ApndFile *)pFile;
3730 pFile = ORIGFILE(p);
3731 rc = pFile->pMethods->xFileSize(pFile, pSize);
3732 if( rc==SQLITE_OK && p->iPgOne ){
3733 *pSize -= p->iPgOne + APND_MARK_SIZE;
3739 ** Lock an apnd-file.
3741 static int apndLock(sqlite3_file *pFile, int eLock){
3742 pFile = ORIGFILE(pFile);
3743 return pFile->pMethods->xLock(pFile, eLock);
3747 ** Unlock an apnd-file.
3749 static int apndUnlock(sqlite3_file *pFile, int eLock){
3750 pFile = ORIGFILE(pFile);
3751 return pFile->pMethods->xUnlock(pFile, eLock);
3755 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3757 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3758 pFile = ORIGFILE(pFile);
3759 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3763 ** File control method. For custom operations on an apnd-file.
3765 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3766 ApndFile *p = (ApndFile *)pFile;
3768 pFile = ORIGFILE(pFile);
3769 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3770 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3771 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3777 ** Return the sector-size in bytes for an apnd-file.
3779 static int apndSectorSize(sqlite3_file *pFile){
3780 pFile = ORIGFILE(pFile);
3781 return pFile->pMethods->xSectorSize(pFile);
3785 ** Return the device characteristic flags supported by an apnd-file.
3787 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3788 pFile = ORIGFILE(pFile);
3789 return pFile->pMethods->xDeviceCharacteristics(pFile);
3792 /* Create a shared memory file mapping */
3793 static int apndShmMap(
3794 sqlite3_file *pFile,
3800 pFile = ORIGFILE(pFile);
3801 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3804 /* Perform locking on a shared-memory segment */
3805 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3806 pFile = ORIGFILE(pFile);
3807 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3810 /* Memory barrier operation on shared memory */
3811 static void apndShmBarrier(sqlite3_file *pFile){
3812 pFile = ORIGFILE(pFile);
3813 pFile->pMethods->xShmBarrier(pFile);
3816 /* Unmap a shared memory segment */
3817 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3818 pFile = ORIGFILE(pFile);
3819 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3822 /* Fetch a page of a memory-mapped file */
3823 static int apndFetch(
3824 sqlite3_file *pFile,
3825 sqlite3_int64 iOfst,
3829 ApndFile *p = (ApndFile *)pFile;
3830 pFile = ORIGFILE(pFile);
3831 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3834 /* Release a memory-mapped page */
3835 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3836 ApndFile *p = (ApndFile *)pFile;
3837 pFile = ORIGFILE(pFile);
3838 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3842 ** Check to see if the file is an ordinary SQLite database file.
3844 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3847 static const char aSqliteHdr[] = "SQLite format 3";
3848 if( sz<512 ) return 0;
3849 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3851 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3855 ** Try to read the append-mark off the end of a file. Return the
3856 ** start of the appended database if the append-mark is present. If
3857 ** there is no append-mark, return -1;
3859 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3861 sqlite3_int64 iMark;
3862 unsigned char a[APND_MARK_SIZE];
3864 if( sz<=APND_MARK_SIZE ) return -1;
3865 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3867 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3868 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3870 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3876 ** Open an apnd file handle.
3878 static int apndOpen(
3881 sqlite3_file *pFile,
3886 sqlite3_file *pSubFile;
3887 sqlite3_vfs *pSubVfs;
3890 pSubVfs = ORIGVFS(pVfs);
3891 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3892 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3894 p = (ApndFile*)pFile;
3895 memset(p, 0, sizeof(*p));
3896 pSubFile = ORIGFILE(pFile);
3897 p->base.pMethods = &apnd_io_methods;
3898 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3899 if( rc ) goto apnd_open_done;
3900 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3902 pSubFile->pMethods->xClose(pSubFile);
3903 goto apnd_open_done;
3905 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3906 memmove(pFile, pSubFile, pSubVfs->szOsFile);
3910 p->iPgOne = apndReadMark(sz, pFile);
3914 if( (flags & SQLITE_OPEN_CREATE)==0 ){
3915 pSubFile->pMethods->xClose(pSubFile);
3916 rc = SQLITE_CANTOPEN;
3918 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3920 if( rc ) pFile->pMethods = 0;
3925 ** All other VFS methods are pass-thrus.
3927 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3928 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3930 static int apndAccess(
3936 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
3938 static int apndFullPathname(
3944 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
3946 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3947 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
3949 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3950 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
3952 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
3953 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
3955 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
3956 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
3958 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
3959 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
3961 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
3962 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
3964 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
3965 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
3967 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
3968 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
3970 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
3971 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
3973 static int apndSetSystemCall(
3976 sqlite3_syscall_ptr pCall
3978 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
3980 static sqlite3_syscall_ptr apndGetSystemCall(
3984 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
3986 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
3987 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
3995 ** This routine is called when the extension is loaded.
3996 ** Register the new VFS.
3998 int sqlite3_appendvfs_init(
4001 const sqlite3_api_routines *pApi
4005 SQLITE_EXTENSION_INIT2(pApi);
4008 pOrig = sqlite3_vfs_find(0);
4009 apnd_vfs.iVersion = pOrig->iVersion;
4010 apnd_vfs.pAppData = pOrig;
4011 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4012 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4013 #ifdef APPENDVFS_TEST
4014 if( rc==SQLITE_OK ){
4015 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4018 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4022 /************************* End ../ext/misc/appendvfs.c ********************/
4023 #ifdef SQLITE_HAVE_ZLIB
4024 /************************* Begin ../ext/misc/zipfile.c ******************/
4028 ** The author disclaims copyright to this source code. In place of
4029 ** a legal notice, here is a blessing:
4031 ** May you do good and not evil.
4032 ** May you find forgiveness for yourself and forgive others.
4033 ** May you share freely, never taking more than you give.
4035 ******************************************************************************
4037 ** This file implements a virtual table for reading and writing ZIP archive
4042 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4044 ** Current limitations:
4046 ** * No support for encryption
4047 ** * No support for ZIP archives spanning multiple files
4048 ** * No support for zip64 extensions
4049 ** * Only the "inflate/deflate" (zlib) compression method is supported
4051 SQLITE_EXTENSION_INIT1
4058 #ifndef SQLITE_OMIT_VIRTUALTABLE
4060 #ifndef SQLITE_AMALGAMATION
4062 /* typedef sqlite3_int64 i64; */
4063 /* typedef unsigned char u8; */
4064 typedef unsigned short u16;
4065 typedef unsigned long u32;
4066 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4068 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4069 # define ALWAYS(X) (1)
4070 # define NEVER(X) (0)
4071 #elif !defined(NDEBUG)
4072 # define ALWAYS(X) ((X)?1:(assert(0),0))
4073 # define NEVER(X) ((X)?(assert(0),1):0)
4075 # define ALWAYS(X) (X)
4076 # define NEVER(X) (X)
4079 #endif /* SQLITE_AMALGAMATION */
4082 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4084 ** In some ways it would be better to obtain these values from system
4085 ** header files. But, the dependency is undesirable and (a) these
4086 ** have been stable for decades, (b) the values are part of POSIX and
4087 ** are also made explicit in [man stat], and (c) are part of the
4088 ** file format for zip archives.
4091 # define S_IFDIR 0040000
4094 # define S_IFREG 0100000
4097 # define S_IFLNK 0120000
4100 static const char ZIPFILE_SCHEMA[] =
4102 "name PRIMARY KEY," /* 0: Name of file in zip archive */
4103 "mode," /* 1: POSIX mode for file */
4104 "mtime," /* 2: Last modification time (secs since 1970)*/
4105 "sz," /* 3: Size of object */
4106 "rawdata," /* 4: Raw data */
4107 "data," /* 5: Uncompressed data */
4108 "method," /* 6: Compression method (integer) */
4109 "z HIDDEN" /* 7: Name of zip file */
4112 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
4113 #define ZIPFILE_BUFFER_SIZE (64*1024)
4117 ** Magic numbers used to read and write zip files.
4119 ** ZIPFILE_NEWENTRY_MADEBY:
4120 ** Use this value for the "version-made-by" field in new zip file
4121 ** entries. The upper byte indicates "unix", and the lower byte
4122 ** indicates that the zip file matches pkzip specification 3.0.
4123 ** This is what info-zip seems to do.
4125 ** ZIPFILE_NEWENTRY_REQUIRED:
4126 ** Value for "version-required-to-extract" field of new entries.
4127 ** Version 2.0 is required to support folders and deflate compression.
4129 ** ZIPFILE_NEWENTRY_FLAGS:
4130 ** Value for "general-purpose-bit-flags" field of new entries. Bit
4131 ** 11 means "utf-8 filename and comment".
4133 ** ZIPFILE_SIGNATURE_CDS:
4134 ** First 4 bytes of a valid CDS record.
4136 ** ZIPFILE_SIGNATURE_LFH:
4137 ** First 4 bytes of a valid LFH record.
4139 ** ZIPFILE_SIGNATURE_EOCD
4140 ** First 4 bytes of a valid EOCD record.
4142 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4143 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4144 #define ZIPFILE_NEWENTRY_REQUIRED 20
4145 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4146 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4147 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4148 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4151 ** The sizes of the fixed-size part of each of the three main data
4152 ** structures in a zip archive.
4154 #define ZIPFILE_LFH_FIXED_SZ 30
4155 #define ZIPFILE_EOCD_FIXED_SZ 22
4156 #define ZIPFILE_CDS_FIXED_SZ 46
4159 *** 4.3.16 End of central directory record:
4161 *** end of central dir signature 4 bytes (0x06054b50)
4162 *** number of this disk 2 bytes
4163 *** number of the disk with the
4164 *** start of the central directory 2 bytes
4165 *** total number of entries in the
4166 *** central directory on this disk 2 bytes
4167 *** total number of entries in
4168 *** the central directory 2 bytes
4169 *** size of the central directory 4 bytes
4170 *** offset of start of central
4171 *** directory with respect to
4172 *** the starting disk number 4 bytes
4173 *** .ZIP file comment length 2 bytes
4174 *** .ZIP file comment (variable size)
4176 typedef struct ZipfileEOCD ZipfileEOCD;
4177 struct ZipfileEOCD {
4187 *** 4.3.12 Central directory structure:
4191 *** central file header signature 4 bytes (0x02014b50)
4192 *** version made by 2 bytes
4193 *** version needed to extract 2 bytes
4194 *** general purpose bit flag 2 bytes
4195 *** compression method 2 bytes
4196 *** last mod file time 2 bytes
4197 *** last mod file date 2 bytes
4199 *** compressed size 4 bytes
4200 *** uncompressed size 4 bytes
4201 *** file name length 2 bytes
4202 *** extra field length 2 bytes
4203 *** file comment length 2 bytes
4204 *** disk number start 2 bytes
4205 *** internal file attributes 2 bytes
4206 *** external file attributes 4 bytes
4207 *** relative offset of local header 4 bytes
4209 typedef struct ZipfileCDS ZipfileCDS;
4212 u16 iVersionExtract;
4227 char *zFile; /* Filename (sqlite3_malloc()) */
4231 *** 4.3.7 Local file header:
4233 *** local file header signature 4 bytes (0x04034b50)
4234 *** version needed to extract 2 bytes
4235 *** general purpose bit flag 2 bytes
4236 *** compression method 2 bytes
4237 *** last mod file time 2 bytes
4238 *** last mod file date 2 bytes
4240 *** compressed size 4 bytes
4241 *** uncompressed size 4 bytes
4242 *** file name length 2 bytes
4243 *** extra field length 2 bytes
4246 typedef struct ZipfileLFH ZipfileLFH;
4248 u16 iVersionExtract;
4260 typedef struct ZipfileEntry ZipfileEntry;
4261 struct ZipfileEntry {
4262 ZipfileCDS cds; /* Parsed CDS record */
4263 u32 mUnixTime; /* Modification time, in UNIX format */
4264 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4265 i64 iDataOff; /* Offset to data in file (if aData==0) */
4266 u8 *aData; /* cds.szCompressed bytes of compressed data */
4267 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4271 ** Cursor type for zipfile tables.
4273 typedef struct ZipfileCsr ZipfileCsr;
4275 sqlite3_vtab_cursor base; /* Base class - must be first */
4276 i64 iId; /* Cursor ID */
4277 u8 bEof; /* True when at EOF */
4278 u8 bNoop; /* If next xNext() call is no-op */
4280 /* Used outside of write transactions */
4281 FILE *pFile; /* Zip file */
4282 i64 iNextOff; /* Offset of next record in central directory */
4283 ZipfileEOCD eocd; /* Parse of central directory record */
4285 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4286 ZipfileEntry *pCurrent; /* Current entry */
4287 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4290 typedef struct ZipfileTab ZipfileTab;
4292 sqlite3_vtab base; /* Base class - must be first */
4293 char *zFile; /* Zip file this table accesses (may be NULL) */
4294 sqlite3 *db; /* Host database connection */
4295 u8 *aBuffer; /* Temporary buffer used for various tasks */
4297 ZipfileCsr *pCsrList; /* List of cursors */
4300 /* The following are used by write transactions only */
4301 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4302 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
4303 FILE *pWriteFd; /* File handle open on zip archive */
4304 i64 szCurrent; /* Current size of zip archive */
4305 i64 szOrig; /* Size of archive at start of transaction */
4309 ** Set the error message contained in context ctx to the results of
4310 ** vprintf(zFmt, ...).
4312 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4316 zMsg = sqlite3_vmprintf(zFmt, ap);
4317 sqlite3_result_error(ctx, zMsg, -1);
4323 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4324 ** is not quoted, do nothing.
4326 static void zipfileDequote(char *zIn){
4328 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4331 if( q=='[' ) q = ']';
4332 while( ALWAYS(zIn[iIn]) ){
4333 char c = zIn[iIn++];
4334 if( c==q && zIn[iIn++]!=q ) break;
4342 ** Construct a new ZipfileTab virtual table object.
4344 ** argv[0] -> module name ("zipfile")
4345 ** argv[1] -> database name
4346 ** argv[2] -> table name
4347 ** argv[...] -> "column name" and other module argument fields.
4349 static int zipfileConnect(
4352 int argc, const char *const*argv,
4353 sqlite3_vtab **ppVtab,
4356 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4358 const char *zFile = 0;
4359 ZipfileTab *pNew = 0;
4362 /* If the table name is not "zipfile", require that the argument be
4363 ** specified. This stops zipfile tables from being created as:
4365 ** CREATE VIRTUAL TABLE zzz USING zipfile();
4367 ** It does not prevent:
4369 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4371 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4372 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4373 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4374 return SQLITE_ERROR;
4379 nFile = (int)strlen(zFile)+1;
4382 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4383 if( rc==SQLITE_OK ){
4384 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4385 if( pNew==0 ) return SQLITE_NOMEM;
4386 memset(pNew, 0, nByte+nFile);
4388 pNew->aBuffer = (u8*)&pNew[1];
4390 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4391 memcpy(pNew->zFile, zFile, nFile);
4392 zipfileDequote(pNew->zFile);
4395 *ppVtab = (sqlite3_vtab*)pNew;
4400 ** Free the ZipfileEntry structure indicated by the only argument.
4402 static void zipfileEntryFree(ZipfileEntry *p){
4404 sqlite3_free(p->cds.zFile);
4410 ** Release resources that should be freed at the end of a write
4413 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4414 ZipfileEntry *pEntry;
4415 ZipfileEntry *pNext;
4417 if( pTab->pWriteFd ){
4418 fclose(pTab->pWriteFd);
4421 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4422 pNext = pEntry->pNext;
4423 zipfileEntryFree(pEntry);
4425 pTab->pFirstEntry = 0;
4426 pTab->pLastEntry = 0;
4427 pTab->szCurrent = 0;
4432 ** This method is the destructor for zipfile vtab objects.
4434 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4435 zipfileCleanupTransaction((ZipfileTab*)pVtab);
4436 sqlite3_free(pVtab);
4441 ** Constructor for a new ZipfileCsr object.
4443 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4444 ZipfileTab *pTab = (ZipfileTab*)p;
4446 pCsr = sqlite3_malloc(sizeof(*pCsr));
4447 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4449 return SQLITE_NOMEM;
4451 memset(pCsr, 0, sizeof(*pCsr));
4452 pCsr->iId = ++pTab->iNextCsrid;
4453 pCsr->pCsrNext = pTab->pCsrList;
4454 pTab->pCsrList = pCsr;
4459 ** Reset a cursor back to the state it was in when first returned
4460 ** by zipfileOpen().
4462 static void zipfileResetCursor(ZipfileCsr *pCsr){
4464 ZipfileEntry *pNext;
4468 fclose(pCsr->pFile);
4470 zipfileEntryFree(pCsr->pCurrent);
4474 for(p=pCsr->pFreeEntry; p; p=pNext){
4476 zipfileEntryFree(p);
4481 ** Destructor for an ZipfileCsr.
4483 static int zipfileClose(sqlite3_vtab_cursor *cur){
4484 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4485 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4487 zipfileResetCursor(pCsr);
4489 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4490 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4491 *pp = pCsr->pCsrNext;
4498 ** Set the error message for the virtual table associated with cursor
4499 ** pCsr to the results of vprintf(zFmt, ...).
4501 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4504 sqlite3_free(pTab->base.zErrMsg);
4505 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4508 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4511 sqlite3_free(pCsr->base.pVtab->zErrMsg);
4512 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4517 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4518 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4521 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4522 ** to an English language error message. It is the responsibility of the
4523 ** caller to eventually free this buffer using
4526 static int zipfileReadData(
4527 FILE *pFile, /* Read from this file */
4528 u8 *aRead, /* Read into this buffer */
4529 int nRead, /* Number of bytes to read */
4530 i64 iOff, /* Offset to read from */
4531 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
4534 fseek(pFile, (long)iOff, SEEK_SET);
4535 n = fread(aRead, 1, nRead, pFile);
4536 if( (int)n!=nRead ){
4537 *pzErrmsg = sqlite3_mprintf("error in fread()");
4538 return SQLITE_ERROR;
4543 static int zipfileAppendData(
4549 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4550 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4551 if( (int)n!=nWrite ){
4552 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4553 return SQLITE_ERROR;
4555 pTab->szCurrent += nWrite;
4560 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4562 static u16 zipfileGetU16(const u8 *aBuf){
4563 return (aBuf[1] << 8) + aBuf[0];
4567 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4569 static u32 zipfileGetU32(const u8 *aBuf){
4570 return ((u32)(aBuf[3]) << 24)
4571 + ((u32)(aBuf[2]) << 16)
4572 + ((u32)(aBuf[1]) << 8)
4573 + ((u32)(aBuf[0]) << 0);
4577 ** Write a 16-bit little endiate integer into buffer aBuf.
4579 static void zipfilePutU16(u8 *aBuf, u16 val){
4580 aBuf[0] = val & 0xFF;
4581 aBuf[1] = (val>>8) & 0xFF;
4585 ** Write a 32-bit little endiate integer into buffer aBuf.
4587 static void zipfilePutU32(u8 *aBuf, u32 val){
4588 aBuf[0] = val & 0xFF;
4589 aBuf[1] = (val>>8) & 0xFF;
4590 aBuf[2] = (val>>16) & 0xFF;
4591 aBuf[3] = (val>>24) & 0xFF;
4594 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4595 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4597 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4598 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4601 ** Magic numbers used to read CDS records.
4603 #define ZIPFILE_CDS_NFILE_OFF 28
4604 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4607 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4608 ** if the record is not well-formed, or SQLITE_OK otherwise.
4610 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4612 u32 sig = zipfileRead32(aRead);
4614 if( sig!=ZIPFILE_SIGNATURE_CDS ){
4617 pCDS->iVersionMadeBy = zipfileRead16(aRead);
4618 pCDS->iVersionExtract = zipfileRead16(aRead);
4619 pCDS->flags = zipfileRead16(aRead);
4620 pCDS->iCompression = zipfileRead16(aRead);
4621 pCDS->mTime = zipfileRead16(aRead);
4622 pCDS->mDate = zipfileRead16(aRead);
4623 pCDS->crc32 = zipfileRead32(aRead);
4624 pCDS->szCompressed = zipfileRead32(aRead);
4625 pCDS->szUncompressed = zipfileRead32(aRead);
4626 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4627 pCDS->nFile = zipfileRead16(aRead);
4628 pCDS->nExtra = zipfileRead16(aRead);
4629 pCDS->nComment = zipfileRead16(aRead);
4630 pCDS->iDiskStart = zipfileRead16(aRead);
4631 pCDS->iInternalAttr = zipfileRead16(aRead);
4632 pCDS->iExternalAttr = zipfileRead32(aRead);
4633 pCDS->iOffset = zipfileRead32(aRead);
4634 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4641 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4642 ** if the record is not well-formed, or SQLITE_OK otherwise.
4644 static int zipfileReadLFH(
4648 u8 *aRead = aBuffer;
4651 u32 sig = zipfileRead32(aRead);
4652 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4655 pLFH->iVersionExtract = zipfileRead16(aRead);
4656 pLFH->flags = zipfileRead16(aRead);
4657 pLFH->iCompression = zipfileRead16(aRead);
4658 pLFH->mTime = zipfileRead16(aRead);
4659 pLFH->mDate = zipfileRead16(aRead);
4660 pLFH->crc32 = zipfileRead32(aRead);
4661 pLFH->szCompressed = zipfileRead32(aRead);
4662 pLFH->szUncompressed = zipfileRead32(aRead);
4663 pLFH->nFile = zipfileRead16(aRead);
4664 pLFH->nExtra = zipfileRead16(aRead);
4671 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4672 ** Scan through this buffer to find an "extra-timestamp" field. If one
4673 ** exists, extract the 32-bit modification-timestamp from it and store
4674 ** the value in output parameter *pmTime.
4676 ** Zero is returned if no extra-timestamp record could be found (and so
4677 ** *pmTime is left unchanged), or non-zero otherwise.
4679 ** The general format of an extra field is:
4681 ** Header ID 2 bytes
4682 ** Data Size 2 bytes
4685 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4688 u8 *pEnd = &aExtra[nExtra];
4691 u16 id = zipfileRead16(p);
4692 u16 nByte = zipfileRead16(p);
4695 case ZIPFILE_EXTRA_TIMESTAMP: {
4697 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4698 *pmTime = zipfileGetU32(&p[1]);
4711 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4712 ** fields of the CDS structure passed as the only argument to a 32-bit
4713 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4715 ** "Standard" MS-DOS time format:
4717 ** File modification time:
4718 ** Bits 00-04: seconds divided by 2
4719 ** Bits 05-10: minute
4721 ** File modification date:
4723 ** Bits 05-08: month (1-12)
4724 ** Bits 09-15: years from 1980
4726 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4728 static u32 zipfileMtime(ZipfileCDS *pCDS){
4729 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4730 int M = ((pCDS->mDate >> 5) & 0x0F);
4731 int D = (pCDS->mDate & 0x1F);
4734 int sec = (pCDS->mTime & 0x1F)*2;
4735 int min = (pCDS->mTime >> 5) & 0x3F;
4736 int hr = (pCDS->mTime >> 11) & 0x1F;
4739 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4741 /* Calculate the JD in seconds for noon on the day in question */
4746 JD = (i64)(24*60*60) * (
4747 (int)(365.25 * (Y + 4716))
4748 + (int)(30.6001 * (M + 1))
4752 /* Correct the JD for the time within the day */
4753 JD += (hr-12) * 3600 + min * 60 + sec;
4755 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4756 return (u32)(JD - (i64)(24405875) * 24*60*6);
4760 ** The opposite of zipfileMtime(). This function populates the mTime and
4761 ** mDate fields of the CDS structure passed as the first argument according
4762 ** to the UNIX timestamp value passed as the second.
4764 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4765 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4766 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4772 A = (int)((JD - 1867216.25)/36524.25);
4773 A = (int)(JD + 1 + A - (A/4));
4775 C = (int)((B - 122.1)/365.25);
4776 D = (36525*(C&32767))/100;
4777 E = (int)((B-D)/30.6001);
4779 day = B - D - (int)(30.6001*E);
4780 mon = (E<14 ? E-1 : E-13);
4781 yr = mon>2 ? C-4716 : C-4715;
4783 hr = (mUnixTime % (24*60*60)) / (60*60);
4784 min = (mUnixTime % (60*60)) / 60;
4785 sec = (mUnixTime % 60);
4788 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4789 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4791 pCds->mDate = pCds->mTime = 0;
4794 assert( mUnixTime<315507600
4795 || mUnixTime==zipfileMtime(pCds)
4796 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4797 /* || (mUnixTime % 2) */
4802 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4803 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4804 ** then pFile is a file-handle open on a zip file. In either case, this
4805 ** function creates a ZipfileEntry object based on the zip archive entry
4806 ** for which the CDS record is at offset iOff.
4808 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4809 ** the new object. Otherwise, an SQLite error code is returned and the
4810 ** final value of (*ppEntry) undefined.
4812 static int zipfileGetEntry(
4813 ZipfileTab *pTab, /* Store any error message here */
4814 const u8 *aBlob, /* Pointer to in-memory file image */
4815 int nBlob, /* Size of aBlob[] in bytes */
4816 FILE *pFile, /* If aBlob==0, read from this file */
4817 i64 iOff, /* Offset of CDS record */
4818 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
4821 char **pzErr = &pTab->base.zErrMsg;
4825 aRead = pTab->aBuffer;
4826 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
4828 aRead = (u8*)&aBlob[iOff];
4831 if( rc==SQLITE_OK ){
4835 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
4836 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
4837 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
4839 nAlloc = sizeof(ZipfileEntry) + nExtra;
4841 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
4844 pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
4848 memset(pNew, 0, sizeof(ZipfileEntry));
4849 rc = zipfileReadCDS(aRead, &pNew->cds);
4850 if( rc!=SQLITE_OK ){
4851 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
4852 }else if( aBlob==0 ){
4853 rc = zipfileReadData(
4854 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
4857 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
4861 if( rc==SQLITE_OK ){
4862 u32 *pt = &pNew->mUnixTime;
4863 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
4864 pNew->aExtra = (u8*)&pNew[1];
4865 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
4866 if( pNew->cds.zFile==0 ){
4868 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
4869 pNew->mUnixTime = zipfileMtime(&pNew->cds);
4873 if( rc==SQLITE_OK ){
4874 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4877 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
4879 aRead = (u8*)&aBlob[pNew->cds.iOffset];
4882 rc = zipfileReadLFH(aRead, &lfh);
4883 if( rc==SQLITE_OK ){
4884 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4885 pNew->iDataOff += lfh.nFile + lfh.nExtra;
4886 if( aBlob && pNew->cds.szCompressed ){
4887 pNew->aData = &pNew->aExtra[nExtra];
4888 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
4891 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
4892 (int)pNew->cds.iOffset
4897 if( rc!=SQLITE_OK ){
4898 zipfileEntryFree(pNew);
4908 ** Advance an ZipfileCsr to its next row of output.
4910 static int zipfileNext(sqlite3_vtab_cursor *cur){
4911 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4915 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4916 zipfileEntryFree(pCsr->pCurrent);
4918 if( pCsr->iNextOff>=iEof ){
4921 ZipfileEntry *p = 0;
4922 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
4923 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
4924 if( rc==SQLITE_OK ){
4925 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4926 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
4932 pCsr->pCurrent = pCsr->pCurrent->pNext;
4934 if( pCsr->pCurrent==0 ){
4943 static void zipfileFree(void *p) {
4948 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
4949 ** size is nOut bytes. This function uncompresses the data and sets the
4950 ** return value in context pCtx to the result (a blob).
4952 ** If an error occurs, an error code is left in pCtx instead.
4954 static void zipfileInflate(
4955 sqlite3_context *pCtx, /* Store result here */
4956 const u8 *aIn, /* Compressed data */
4957 int nIn, /* Size of buffer aIn[] in bytes */
4958 int nOut /* Expected output size */
4960 u8 *aRes = sqlite3_malloc(nOut);
4962 sqlite3_result_error_nomem(pCtx);
4966 memset(&str, 0, sizeof(str));
4968 str.next_in = (Byte*)aIn;
4970 str.next_out = (Byte*)aRes;
4971 str.avail_out = nOut;
4973 err = inflateInit2(&str, -15);
4975 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
4977 err = inflate(&str, Z_NO_FLUSH);
4978 if( err!=Z_STREAM_END ){
4979 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4981 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
4991 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
4992 ** compresses it and sets (*ppOut) to point to a buffer containing the
4993 ** compressed data. The caller is responsible for eventually calling
4994 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
4995 ** is set to the size of buffer (*ppOut) in bytes.
4997 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
4998 ** code is returned and an error message left in virtual-table handle
4999 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5002 static int zipfileDeflate(
5003 const u8 *aIn, int nIn, /* Input */
5004 u8 **ppOut, int *pnOut, /* Output */
5005 char **pzErr /* OUT: Error message */
5007 int nAlloc = (int)compressBound(nIn);
5011 aOut = (u8*)sqlite3_malloc(nAlloc);
5017 memset(&str, 0, sizeof(str));
5018 str.next_in = (Bytef*)aIn;
5020 str.next_out = aOut;
5021 str.avail_out = nAlloc;
5023 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5024 res = deflate(&str, Z_FINISH);
5026 if( res==Z_STREAM_END ){
5028 *pnOut = (int)str.total_out;
5031 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5042 ** Return values of columns for the row at which the series_cursor
5043 ** is currently pointing.
5045 static int zipfileColumn(
5046 sqlite3_vtab_cursor *cur, /* The cursor */
5047 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5048 int i /* Which column to return */
5050 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5051 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5055 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5058 /* TODO: Whether or not the following is correct surely depends on
5059 ** the platform on which the archive was created. */
5060 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5062 case 2: { /* mtime */
5063 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5067 if( sqlite3_vtab_nochange(ctx)==0 ){
5068 sqlite3_result_int64(ctx, pCDS->szUncompressed);
5072 case 4: /* rawdata */
5073 if( sqlite3_vtab_nochange(ctx) ) break;
5074 case 5: { /* data */
5075 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5076 int sz = pCDS->szCompressed;
5077 int szFinal = pCDS->szUncompressed;
5081 if( pCsr->pCurrent->aData ){
5082 aBuf = pCsr->pCurrent->aData;
5084 aBuf = aFree = sqlite3_malloc(sz);
5088 FILE *pFile = pCsr->pFile;
5090 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5092 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5093 &pCsr->base.pVtab->zErrMsg
5097 if( rc==SQLITE_OK ){
5098 if( i==5 && pCDS->iCompression ){
5099 zipfileInflate(ctx, aBuf, sz, szFinal);
5101 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5104 sqlite3_free(aFree);
5106 /* Figure out if this is a directory or a zero-sized file. Consider
5107 ** it to be a directory either if the mode suggests so, or if
5108 ** the final character in the name is '/'. */
5109 u32 mode = pCDS->iExternalAttr >> 16;
5110 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5111 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5117 case 6: /* method */
5118 sqlite3_result_int(ctx, pCDS->iCompression);
5122 sqlite3_result_int64(ctx, pCsr->iId);
5130 ** Return TRUE if the cursor is at EOF.
5132 static int zipfileEof(sqlite3_vtab_cursor *cur){
5133 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5138 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5139 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5140 ** is guaranteed to be a file-handle open on a zip file.
5142 ** This function attempts to locate the EOCD record within the zip archive
5143 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5144 ** returned if successful. Otherwise, an SQLite error code is returned and
5145 ** an English language error message may be left in virtual-table pTab.
5147 static int zipfileReadEOCD(
5148 ZipfileTab *pTab, /* Return errors here */
5149 const u8 *aBlob, /* Pointer to in-memory file image */
5150 int nBlob, /* Size of aBlob[] in bytes */
5151 FILE *pFile, /* Read from this file if aBlob==0 */
5152 ZipfileEOCD *pEOCD /* Object to populate */
5154 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
5155 int nRead; /* Bytes to read from file */
5159 i64 iOff; /* Offset to read from */
5160 i64 szFile; /* Total size of file in bytes */
5161 fseek(pFile, 0, SEEK_END);
5162 szFile = (i64)ftell(pFile);
5164 memset(pEOCD, 0, sizeof(ZipfileEOCD));
5167 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5168 iOff = szFile - nRead;
5169 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5171 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5172 aRead = (u8*)&aBlob[nBlob-nRead];
5175 if( rc==SQLITE_OK ){
5178 /* Scan backwards looking for the signature bytes */
5179 for(i=nRead-20; i>=0; i--){
5180 if( aRead[i]==0x50 && aRead[i+1]==0x4b
5181 && aRead[i+2]==0x05 && aRead[i+3]==0x06
5187 pTab->base.zErrMsg = sqlite3_mprintf(
5188 "cannot find end of central directory record"
5190 return SQLITE_ERROR;
5194 pEOCD->iDisk = zipfileRead16(aRead);
5195 pEOCD->iFirstDisk = zipfileRead16(aRead);
5196 pEOCD->nEntry = zipfileRead16(aRead);
5197 pEOCD->nEntryTotal = zipfileRead16(aRead);
5198 pEOCD->nSize = zipfileRead32(aRead);
5199 pEOCD->iOffset = zipfileRead32(aRead);
5206 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5207 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5208 ** to the end of the list. Otherwise, it is added to the list immediately
5209 ** before pBefore (which is guaranteed to be a part of said list).
5211 static void zipfileAddEntry(
5213 ZipfileEntry *pBefore,
5216 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5217 assert( pNew->pNext==0 );
5219 if( pTab->pFirstEntry==0 ){
5220 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5222 assert( pTab->pLastEntry->pNext==0 );
5223 pTab->pLastEntry->pNext = pNew;
5224 pTab->pLastEntry = pNew;
5228 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5229 pNew->pNext = pBefore;
5234 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5240 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5241 iOff = eocd.iOffset;
5242 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5243 ZipfileEntry *pNew = 0;
5244 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5246 if( rc==SQLITE_OK ){
5247 zipfileAddEntry(pTab, 0, pNew);
5248 iOff += ZIPFILE_CDS_FIXED_SZ;
5249 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5256 ** xFilter callback.
5258 static int zipfileFilter(
5259 sqlite3_vtab_cursor *cur,
5260 int idxNum, const char *idxStr,
5261 int argc, sqlite3_value **argv
5263 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5264 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5265 const char *zFile = 0; /* Zip file to scan */
5266 int rc = SQLITE_OK; /* Return Code */
5267 int bInMemory = 0; /* True for an in-memory zipfile */
5269 zipfileResetCursor(pCsr);
5272 zFile = pTab->zFile;
5273 }else if( idxNum==0 ){
5274 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5275 return SQLITE_ERROR;
5276 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5277 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5278 int nBlob = sqlite3_value_bytes(argv[0]);
5279 assert( pTab->pFirstEntry==0 );
5280 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5281 pCsr->pFreeEntry = pTab->pFirstEntry;
5282 pTab->pFirstEntry = pTab->pLastEntry = 0;
5283 if( rc!=SQLITE_OK ) return rc;
5286 zFile = (const char*)sqlite3_value_text(argv[0]);
5289 if( 0==pTab->pWriteFd && 0==bInMemory ){
5290 pCsr->pFile = fopen(zFile, "rb");
5291 if( pCsr->pFile==0 ){
5292 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5295 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5296 if( rc==SQLITE_OK ){
5297 if( pCsr->eocd.nEntry==0 ){
5300 pCsr->iNextOff = pCsr->eocd.iOffset;
5301 rc = zipfileNext(cur);
5307 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5308 rc = zipfileNext(cur);
5315 ** xBestIndex callback.
5317 static int zipfileBestIndex(
5319 sqlite3_index_info *pIdxInfo
5323 for(i=0; i<pIdxInfo->nConstraint; i++){
5324 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5325 if( pCons->usable==0 ) continue;
5326 if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
5327 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5331 if( i<pIdxInfo->nConstraint ){
5332 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
5333 pIdxInfo->aConstraintUsage[i].omit = 1;
5334 pIdxInfo->estimatedCost = 1000.0;
5335 pIdxInfo->idxNum = 1;
5337 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
5338 pIdxInfo->idxNum = 0;
5344 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5346 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5348 memset(pNew, 0, sizeof(ZipfileEntry));
5349 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5350 if( pNew->cds.zFile==0 ){
5358 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5359 ZipfileCDS *pCds = &pEntry->cds;
5364 /* Write the LFH itself */
5365 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5366 zipfileWrite16(a, pCds->iVersionExtract);
5367 zipfileWrite16(a, pCds->flags);
5368 zipfileWrite16(a, pCds->iCompression);
5369 zipfileWrite16(a, pCds->mTime);
5370 zipfileWrite16(a, pCds->mDate);
5371 zipfileWrite32(a, pCds->crc32);
5372 zipfileWrite32(a, pCds->szCompressed);
5373 zipfileWrite32(a, pCds->szUncompressed);
5374 zipfileWrite16(a, (u16)pCds->nFile);
5375 zipfileWrite16(a, pCds->nExtra);
5376 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5378 /* Add the file name */
5379 memcpy(a, pCds->zFile, (int)pCds->nFile);
5380 a += (int)pCds->nFile;
5382 /* The "extra" data */
5383 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5384 zipfileWrite16(a, 5);
5386 zipfileWrite32(a, pEntry->mUnixTime);
5391 static int zipfileAppendEntry(
5393 ZipfileEntry *pEntry,
5397 u8 *aBuf = pTab->aBuffer;
5401 nBuf = zipfileSerializeLFH(pEntry, aBuf);
5402 rc = zipfileAppendData(pTab, aBuf, nBuf);
5403 if( rc==SQLITE_OK ){
5404 pEntry->iDataOff = pTab->szCurrent;
5405 rc = zipfileAppendData(pTab, pData, nData);
5411 static int zipfileGetMode(
5412 sqlite3_value *pVal,
5413 int bIsDir, /* If true, default to directory */
5414 u32 *pMode, /* OUT: Mode value */
5415 char **pzErr /* OUT: Error message */
5417 const char *z = (const char*)sqlite3_value_text(pVal);
5420 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5421 }else if( z[0]>='0' && z[0]<='9' ){
5422 mode = (unsigned int)sqlite3_value_int(pVal);
5424 const char zTemplate[11] = "-rwxrwxrwx";
5426 if( strlen(z)!=10 ) goto parse_error;
5428 case '-': mode |= S_IFREG; break;
5429 case 'd': mode |= S_IFDIR; break;
5430 case 'l': mode |= S_IFLNK; break;
5431 default: goto parse_error;
5433 for(i=1; i<10; i++){
5434 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5435 else if( z[i]!='-' ) goto parse_error;
5438 if( ((mode & S_IFDIR)==0)==bIsDir ){
5439 /* The "mode" attribute is a directory, but data has been specified.
5440 ** Or vice-versa - no data but "mode" is a file or symlink. */
5441 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5442 return SQLITE_CONSTRAINT;
5448 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5449 return SQLITE_ERROR;
5453 ** Both (const char*) arguments point to nul-terminated strings. Argument
5454 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5455 ** identical, ignoring any trailing '/' character in either path. */
5456 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5457 int nA = (int)strlen(zA);
5458 if( zA[nA-1]=='/' ) nA--;
5459 if( zB[nB-1]=='/' ) nB--;
5460 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5464 static int zipfileBegin(sqlite3_vtab *pVtab){
5465 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5468 assert( pTab->pWriteFd==0 );
5470 /* Open a write fd on the file. Also load the entire central directory
5471 ** structure into memory. During the transaction any new file data is
5472 ** appended to the archive file, but the central directory is accumulated
5473 ** in main-memory until the transaction is committed. */
5474 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5475 if( pTab->pWriteFd==0 ){
5476 pTab->base.zErrMsg = sqlite3_mprintf(
5477 "zipfile: failed to open file %s for writing", pTab->zFile
5481 fseek(pTab->pWriteFd, 0, SEEK_END);
5482 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5483 rc = zipfileLoadDirectory(pTab, 0, 0);
5486 if( rc!=SQLITE_OK ){
5487 zipfileCleanupTransaction(pTab);
5494 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5497 static u32 zipfileTime(void){
5498 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5500 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5502 pVfs->xCurrentTimeInt64(pVfs, &ms);
5503 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5506 pVfs->xCurrentTime(pVfs, &day);
5507 ret = (u32)((day - 2440587.5) * 86400);
5513 ** Return a 32-bit timestamp in UNIX epoch format.
5515 ** If the value passed as the only argument is either NULL or an SQL NULL,
5516 ** return the current time. Otherwise, return the value stored in (*pVal)
5517 ** cast to a 32-bit unsigned integer.
5519 static u32 zipfileGetTime(sqlite3_value *pVal){
5520 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5521 return zipfileTime();
5523 return (u32)sqlite3_value_int64(pVal);
5527 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5528 ** linked list. Remove it from the list and free the object.
5530 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5533 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5535 zipfileEntryFree(pOld);
5542 static int zipfileUpdate(
5543 sqlite3_vtab *pVtab,
5545 sqlite3_value **apVal,
5546 sqlite_int64 *pRowid
5548 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5549 int rc = SQLITE_OK; /* Return Code */
5550 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5552 u32 mode = 0; /* Mode for new entry */
5553 u32 mTime = 0; /* Modification time for new entry */
5554 i64 sz = 0; /* Uncompressed size */
5555 const char *zPath = 0; /* Path for new entry */
5556 int nPath = 0; /* strlen(zPath) */
5557 const u8 *pData = 0; /* Pointer to buffer containing content */
5558 int nData = 0; /* Size of pData buffer in bytes */
5559 int iMethod = 0; /* Compression method for new entry */
5560 u8 *pFree = 0; /* Free this */
5561 char *zFree = 0; /* Also free this */
5562 ZipfileEntry *pOld = 0;
5563 ZipfileEntry *pOld2 = 0;
5564 int bUpdate = 0; /* True for an update that modifies "name" */
5568 if( pTab->pWriteFd==0 ){
5569 rc = zipfileBegin(pVtab);
5570 if( rc!=SQLITE_OK ) return rc;
5573 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5574 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5575 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5576 int nDelete = (int)strlen(zDelete);
5578 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5579 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5583 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5584 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5587 assert( pOld->pNext );
5592 /* Check that "sz" and "rawdata" are both NULL: */
5593 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5594 zipfileTableErr(pTab, "sz must be NULL");
5595 rc = SQLITE_CONSTRAINT;
5597 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5598 zipfileTableErr(pTab, "rawdata must be NULL");
5599 rc = SQLITE_CONSTRAINT;
5602 if( rc==SQLITE_OK ){
5603 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5604 /* data=NULL. A directory */
5607 /* Value specified for "data", and possibly "method". This must be
5608 ** a regular file or a symlink. */
5609 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5610 int nIn = sqlite3_value_bytes(apVal[7]);
5611 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5613 iMethod = sqlite3_value_int(apVal[8]);
5617 if( iMethod!=0 && iMethod!=8 ){
5618 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5619 rc = SQLITE_CONSTRAINT;
5621 if( bAuto || iMethod ){
5623 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5624 if( rc==SQLITE_OK ){
5625 if( iMethod || nCmp<nIn ){
5632 iCrc32 = crc32(0, aIn, nIn);
5637 if( rc==SQLITE_OK ){
5638 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5641 if( rc==SQLITE_OK ){
5642 zPath = (const char*)sqlite3_value_text(apVal[2]);
5643 nPath = (int)strlen(zPath);
5644 mTime = zipfileGetTime(apVal[4]);
5647 if( rc==SQLITE_OK && bIsDir ){
5648 /* For a directory, check that the last character in the path is a
5649 ** '/'. This appears to be required for compatibility with info-zip
5650 ** (the unzip command on unix). It does not create directories
5652 if( zPath[nPath-1]!='/' ){
5653 zFree = sqlite3_mprintf("%s/", zPath);
5654 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5655 zPath = (const char*)zFree;
5660 /* Check that we're not inserting a duplicate entry -OR- updating an
5661 ** entry with a path, thereby making it into a duplicate. */
5662 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5664 for(p=pTab->pFirstEntry; p; p=p->pNext){
5665 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5666 switch( sqlite3_vtab_on_conflict(pTab->db) ){
5667 case SQLITE_IGNORE: {
5668 goto zipfile_update_done;
5670 case SQLITE_REPLACE: {
5675 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5676 rc = SQLITE_CONSTRAINT;
5685 if( rc==SQLITE_OK ){
5686 /* Create the new CDS record. */
5687 pNew = zipfileNewEntry(zPath);
5691 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5692 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5693 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5694 pNew->cds.iCompression = (u16)iMethod;
5695 zipfileMtimeToDos(&pNew->cds, mTime);
5696 pNew->cds.crc32 = iCrc32;
5697 pNew->cds.szCompressed = nData;
5698 pNew->cds.szUncompressed = (u32)sz;
5699 pNew->cds.iExternalAttr = (mode<<16);
5700 pNew->cds.iOffset = (u32)pTab->szCurrent;
5701 pNew->cds.nFile = (u16)nPath;
5702 pNew->mUnixTime = (u32)mTime;
5703 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5704 zipfileAddEntry(pTab, pOld, pNew);
5709 if( rc==SQLITE_OK && (pOld || pOld2) ){
5711 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5712 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5713 pCsr->pCurrent = pCsr->pCurrent->pNext;
5718 zipfileRemoveEntryFromList(pTab, pOld);
5719 zipfileRemoveEntryFromList(pTab, pOld2);
5722 zipfile_update_done:
5723 sqlite3_free(pFree);
5724 sqlite3_free(zFree);
5728 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5730 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5731 zipfileWrite16(a, p->iDisk);
5732 zipfileWrite16(a, p->iFirstDisk);
5733 zipfileWrite16(a, p->nEntry);
5734 zipfileWrite16(a, p->nEntryTotal);
5735 zipfileWrite32(a, p->nSize);
5736 zipfileWrite32(a, p->iOffset);
5737 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
5742 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5743 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5744 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5745 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5749 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5750 ** of bytes written.
5752 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5754 ZipfileCDS *pCDS = &pEntry->cds;
5756 if( pEntry->aExtra==0 ){
5760 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5761 zipfileWrite16(a, pCDS->iVersionMadeBy);
5762 zipfileWrite16(a, pCDS->iVersionExtract);
5763 zipfileWrite16(a, pCDS->flags);
5764 zipfileWrite16(a, pCDS->iCompression);
5765 zipfileWrite16(a, pCDS->mTime);
5766 zipfileWrite16(a, pCDS->mDate);
5767 zipfileWrite32(a, pCDS->crc32);
5768 zipfileWrite32(a, pCDS->szCompressed);
5769 zipfileWrite32(a, pCDS->szUncompressed);
5770 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5771 zipfileWrite16(a, pCDS->nFile);
5772 zipfileWrite16(a, pCDS->nExtra);
5773 zipfileWrite16(a, pCDS->nComment);
5774 zipfileWrite16(a, pCDS->iDiskStart);
5775 zipfileWrite16(a, pCDS->iInternalAttr);
5776 zipfileWrite32(a, pCDS->iExternalAttr);
5777 zipfileWrite32(a, pCDS->iOffset);
5779 memcpy(a, pCDS->zFile, pCDS->nFile);
5782 if( pEntry->aExtra ){
5783 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5784 memcpy(a, pEntry->aExtra, n);
5787 assert( pCDS->nExtra==9 );
5788 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5789 zipfileWrite16(a, 5);
5791 zipfileWrite32(a, pEntry->mUnixTime);
5797 static int zipfileCommit(sqlite3_vtab *pVtab){
5798 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5800 if( pTab->pWriteFd ){
5801 i64 iOffset = pTab->szCurrent;
5806 /* Write out all entries */
5807 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5808 int n = zipfileSerializeCDS(p, pTab->aBuffer);
5809 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5813 /* Write out the EOCD record */
5815 eocd.iFirstDisk = 0;
5816 eocd.nEntry = (u16)nEntry;
5817 eocd.nEntryTotal = (u16)nEntry;
5818 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
5819 eocd.iOffset = (u32)iOffset;
5820 rc = zipfileAppendEOCD(pTab, &eocd);
5822 zipfileCleanupTransaction(pTab);
5827 static int zipfileRollback(sqlite3_vtab *pVtab){
5828 return zipfileCommit(pVtab);
5831 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
5833 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5834 if( iId==pCsr->iId ) break;
5839 static void zipfileFunctionCds(
5840 sqlite3_context *context,
5842 sqlite3_value **argv
5845 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5848 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5850 ZipfileCDS *p = &pCsr->pCurrent->cds;
5851 char *zRes = sqlite3_mprintf("{"
5852 "\"version-made-by\" : %u, "
5853 "\"version-to-extract\" : %u, "
5855 "\"compression\" : %u, "
5859 "\"compressed-size\" : %u, "
5860 "\"uncompressed-size\" : %u, "
5861 "\"file-name-length\" : %u, "
5862 "\"extra-field-length\" : %u, "
5863 "\"file-comment-length\" : %u, "
5864 "\"disk-number-start\" : %u, "
5865 "\"internal-attr\" : %u, "
5866 "\"external-attr\" : %u, "
5867 "\"offset\" : %u }",
5868 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
5869 (u32)p->flags, (u32)p->iCompression,
5870 (u32)p->mTime, (u32)p->mDate,
5871 (u32)p->crc32, (u32)p->szCompressed,
5872 (u32)p->szUncompressed, (u32)p->nFile,
5873 (u32)p->nExtra, (u32)p->nComment,
5874 (u32)p->iDiskStart, (u32)p->iInternalAttr,
5875 (u32)p->iExternalAttr, (u32)p->iOffset
5879 sqlite3_result_error_nomem(context);
5881 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
5888 ** xFindFunction method.
5890 static int zipfileFindFunction(
5891 sqlite3_vtab *pVtab, /* Virtual table handle */
5892 int nArg, /* Number of SQL function arguments */
5893 const char *zName, /* Name of SQL function */
5894 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5895 void **ppArg /* OUT: User data for *pxFunc */
5897 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5898 *pxFunc = zipfileFunctionCds;
5899 *ppArg = (void*)pVtab;
5905 typedef struct ZipfileBuffer ZipfileBuffer;
5906 struct ZipfileBuffer {
5907 u8 *a; /* Pointer to buffer */
5908 int n; /* Size of buffer in bytes */
5909 int nAlloc; /* Byte allocated at a[] */
5912 typedef struct ZipfileCtx ZipfileCtx;
5919 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
5920 if( pBuf->n+nByte>pBuf->nAlloc ){
5922 int nNew = pBuf->n ? pBuf->n*2 : 512;
5923 int nReq = pBuf->n + nByte;
5925 while( nNew<nReq ) nNew = nNew*2;
5926 aNew = sqlite3_realloc(pBuf->a, nNew);
5927 if( aNew==0 ) return SQLITE_NOMEM;
5929 pBuf->nAlloc = nNew;
5935 ** xStep() callback for the zipfile() aggregate. This can be called in
5936 ** any of the following ways:
5938 ** SELECT zipfile(name,data) ...
5939 ** SELECT zipfile(name,mode,mtime,data) ...
5940 ** SELECT zipfile(name,mode,mtime,data,method) ...
5942 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
5943 ZipfileCtx *p; /* Aggregate function context */
5944 ZipfileEntry e; /* New entry to add to zip archive */
5946 sqlite3_value *pName = 0;
5947 sqlite3_value *pMode = 0;
5948 sqlite3_value *pMtime = 0;
5949 sqlite3_value *pData = 0;
5950 sqlite3_value *pMethod = 0;
5957 int iMethod = -1; /* Compression method to use (0 or 8) */
5959 const u8 *aData = 0; /* Possibly compressed data for new entry */
5960 int nData = 0; /* Size of aData[] in bytes */
5961 int szUncompressed = 0; /* Size of data before compression */
5962 u8 *aFree = 0; /* Free this before returning */
5963 u32 iCrc32 = 0; /* crc32 of uncompressed data */
5965 char *zName = 0; /* Path (name) of new entry */
5966 int nName = 0; /* Size of zName in bytes */
5967 char *zFree = 0; /* Free this before returning */
5970 memset(&e, 0, sizeof(e));
5971 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
5974 /* Martial the arguments into stack variables */
5975 if( nVal!=2 && nVal!=4 && nVal!=5 ){
5976 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
5978 goto zipfile_step_out;
5992 /* Check that the 'name' parameter looks ok. */
5993 zName = (char*)sqlite3_value_text(pName);
5994 nName = sqlite3_value_bytes(pName);
5996 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
5998 goto zipfile_step_out;
6001 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6002 ** deflate compression) or NULL (choose automatically). */
6003 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6004 iMethod = (int)sqlite3_value_int64(pMethod);
6005 if( iMethod!=0 && iMethod!=8 ){
6006 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6008 goto zipfile_step_out;
6012 /* Now inspect the data. If this is NULL, then the new entry must be a
6013 ** directory. Otherwise, figure out whether or not the data should
6014 ** be deflated or simply stored in the zip archive. */
6015 if( sqlite3_value_type(pData)==SQLITE_NULL ){
6019 aData = sqlite3_value_blob(pData);
6020 szUncompressed = nData = sqlite3_value_bytes(pData);
6021 iCrc32 = crc32(0, aData, nData);
6022 if( iMethod<0 || iMethod==8 ){
6024 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6025 if( rc!=SQLITE_OK ){
6026 goto zipfile_step_out;
6028 if( iMethod==8 || nOut<nData ){
6038 /* Decode the "mode" argument. */
6039 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6040 if( rc ) goto zipfile_step_out;
6042 /* Decode the "mtime" argument. */
6043 e.mUnixTime = zipfileGetTime(pMtime);
6045 /* If this is a directory entry, ensure that there is exactly one '/'
6046 ** at the end of the path. Or, if this is not a directory and the path
6047 ** ends in '/' it is an error. */
6049 if( zName[nName-1]=='/' ){
6050 zErr = sqlite3_mprintf("non-directory name must not end with /");
6052 goto zipfile_step_out;
6055 if( zName[nName-1]!='/' ){
6056 zName = zFree = sqlite3_mprintf("%s/", zName);
6060 goto zipfile_step_out;
6063 while( nName>1 && zName[nName-2]=='/' ) nName--;
6067 /* Assemble the ZipfileEntry object for the new zip archive entry */
6068 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6069 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6070 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6071 e.cds.iCompression = (u16)iMethod;
6072 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6073 e.cds.crc32 = iCrc32;
6074 e.cds.szCompressed = nData;
6075 e.cds.szUncompressed = szUncompressed;
6076 e.cds.iExternalAttr = (mode<<16);
6077 e.cds.iOffset = p->body.n;
6078 e.cds.nFile = (u16)nName;
6079 e.cds.zFile = zName;
6081 /* Append the LFH to the body of the new archive */
6082 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6083 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6084 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6086 /* Append the data to the body of the new archive */
6088 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6089 memcpy(&p->body.a[p->body.n], aData, nData);
6093 /* Append the CDS record to the directory of the new archive */
6094 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6095 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6096 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6098 /* Increment the count of entries in the archive */
6102 sqlite3_free(aFree);
6103 sqlite3_free(zFree);
6106 sqlite3_result_error(pCtx, zErr, -1);
6108 sqlite3_result_error_code(pCtx, rc);
6115 ** xFinalize() callback for zipfile aggregate function.
6117 void zipfileFinal(sqlite3_context *pCtx){
6123 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6126 memset(&eocd, 0, sizeof(eocd));
6127 eocd.nEntry = (u16)p->nEntry;
6128 eocd.nEntryTotal = (u16)p->nEntry;
6129 eocd.nSize = p->cds.n;
6130 eocd.iOffset = p->body.n;
6132 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6133 aZip = (u8*)sqlite3_malloc(nZip);
6135 sqlite3_result_error_nomem(pCtx);
6137 memcpy(aZip, p->body.a, p->body.n);
6138 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6139 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6140 sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
6144 sqlite3_free(p->body.a);
6145 sqlite3_free(p->cds.a);
6150 ** Register the "zipfile" virtual table.
6152 static int zipfileRegister(sqlite3 *db){
6153 static sqlite3_module zipfileModule = {
6155 zipfileConnect, /* xCreate */
6156 zipfileConnect, /* xConnect */
6157 zipfileBestIndex, /* xBestIndex */
6158 zipfileDisconnect, /* xDisconnect */
6159 zipfileDisconnect, /* xDestroy */
6160 zipfileOpen, /* xOpen - open a cursor */
6161 zipfileClose, /* xClose - close a cursor */
6162 zipfileFilter, /* xFilter - configure scan constraints */
6163 zipfileNext, /* xNext - advance a cursor */
6164 zipfileEof, /* xEof - check for end of scan */
6165 zipfileColumn, /* xColumn - read data */
6166 0, /* xRowid - read data */
6167 zipfileUpdate, /* xUpdate */
6168 zipfileBegin, /* xBegin */
6170 zipfileCommit, /* xCommit */
6171 zipfileRollback, /* xRollback */
6172 zipfileFindFunction, /* xFindMethod */
6176 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6177 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6178 if( rc==SQLITE_OK ){
6179 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6180 zipfileStep, zipfileFinal
6185 #else /* SQLITE_OMIT_VIRTUALTABLE */
6186 # define zipfileRegister(x) SQLITE_OK
6192 int sqlite3_zipfile_init(
6195 const sqlite3_api_routines *pApi
6197 SQLITE_EXTENSION_INIT2(pApi);
6198 (void)pzErrMsg; /* Unused parameter */
6199 return zipfileRegister(db);
6202 /************************* End ../ext/misc/zipfile.c ********************/
6203 /************************* Begin ../ext/misc/sqlar.c ******************/
6207 ** The author disclaims copyright to this source code. In place of
6208 ** a legal notice, here is a blessing:
6210 ** May you do good and not evil.
6211 ** May you find forgiveness for yourself and forgive others.
6212 ** May you share freely, never taking more than you give.
6214 ******************************************************************************
6216 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6217 ** for working with sqlar archives and used by the shell tool's built-in
6220 SQLITE_EXTENSION_INIT1
6224 ** Implementation of the "sqlar_compress(X)" SQL function.
6226 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6227 ** zlib utility function compress() yields a smaller blob, return the
6228 ** compressed blob. Otherwise, return a copy of X.
6230 ** SQLar uses the "zlib format" for compressed content. The zlib format
6231 ** contains a two-byte identification header and a four-byte checksum at
6232 ** the end. This is different from ZIP which uses the raw deflate format.
6234 ** Future enhancements to SQLar might add support for new compression formats.
6235 ** If so, those new formats will be identified by alternative headers in the
6238 static void sqlarCompressFunc(
6239 sqlite3_context *context,
6241 sqlite3_value **argv
6244 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6245 const Bytef *pData = sqlite3_value_blob(argv[0]);
6246 uLong nData = sqlite3_value_bytes(argv[0]);
6247 uLongf nOut = compressBound(nData);
6250 pOut = (Bytef*)sqlite3_malloc(nOut);
6252 sqlite3_result_error_nomem(context);
6255 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6256 sqlite3_result_error(context, "error in compress()", -1);
6257 }else if( nOut<nData ){
6258 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6260 sqlite3_result_value(context, argv[0]);
6265 sqlite3_result_value(context, argv[0]);
6270 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6272 ** Parameter SZ is interpreted as an integer. If it is less than or
6273 ** equal to zero, then this function returns a copy of X. Or, if
6274 ** SZ is equal to the size of X when interpreted as a blob, also
6275 ** return a copy of X. Otherwise, decompress blob X using zlib
6276 ** utility function uncompress() and return the results (another
6279 static void sqlarUncompressFunc(
6280 sqlite3_context *context,
6282 sqlite3_value **argv
6288 sz = sqlite3_value_int(argv[1]);
6290 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6291 sqlite3_result_value(context, argv[0]);
6293 const Bytef *pData= sqlite3_value_blob(argv[0]);
6294 Bytef *pOut = sqlite3_malloc(sz);
6295 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6296 sqlite3_result_error(context, "error in uncompress()", -1);
6298 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6308 int sqlite3_sqlar_init(
6311 const sqlite3_api_routines *pApi
6314 SQLITE_EXTENSION_INIT2(pApi);
6315 (void)pzErrMsg; /* Unused parameter */
6316 rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6317 sqlarCompressFunc, 0, 0);
6318 if( rc==SQLITE_OK ){
6319 rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6320 sqlarUncompressFunc, 0, 0);
6325 /************************* End ../ext/misc/sqlar.c ********************/
6327 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6331 ** The author disclaims copyright to this source code. In place of
6332 ** a legal notice, here is a blessing:
6334 ** May you do good and not evil.
6335 ** May you find forgiveness for yourself and forgive others.
6336 ** May you share freely, never taking more than you give.
6338 *************************************************************************
6343 typedef struct sqlite3expert sqlite3expert;
6346 ** Create a new sqlite3expert object.
6348 ** If successful, a pointer to the new object is returned and (*pzErr) set
6349 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6350 ** an English-language error message. In this case it is the responsibility
6351 ** of the caller to eventually free the error message buffer using
6354 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6357 ** Configure an sqlite3expert object.
6359 ** EXPERT_CONFIG_SAMPLE:
6360 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6361 ** each candidate index. This involves scanning and sorting the entire
6362 ** contents of each user database table once for each candidate index
6363 ** associated with the table. For large databases, this can be
6364 ** prohibitively slow. This option allows the sqlite3expert object to
6365 ** be configured so that sqlite_stat1 data is instead generated based on a
6366 ** subset of each table, or so that no sqlite_stat1 data is used at all.
6368 ** A single integer argument is passed to this option. If the value is less
6369 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
6370 ** the analysis - indexes are recommended based on the database schema only.
6371 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
6372 ** generated for each candidate index (this is the default). Finally, if the
6373 ** value falls between 0 and 100, then it represents the percentage of user
6374 ** table rows that should be considered when generating sqlite_stat1 data.
6378 ** // Do not generate any sqlite_stat1 data
6379 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6381 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
6382 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6384 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6386 #define EXPERT_CONFIG_SAMPLE 1 /* int */
6389 ** Specify zero or more SQL statements to be included in the analysis.
6391 ** Buffer zSql must contain zero or more complete SQL statements. This
6392 ** function parses all statements contained in the buffer and adds them
6393 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6394 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6395 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6396 ** may be set to point to an English language error message. In this case
6397 ** the caller is responsible for eventually freeing the error message buffer
6398 ** using sqlite3_free().
6400 ** If an error does occur while processing one of the statements in the
6401 ** buffer passed as the second argument, none of the statements in the
6402 ** buffer are added to the analysis.
6404 ** This function must be called before sqlite3_expert_analyze(). If a call
6405 ** to this function is made on an sqlite3expert object that has already
6406 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6407 ** immediately and no statements are added to the analysis.
6409 int sqlite3_expert_sql(
6410 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
6411 const char *zSql, /* SQL statement(s) to add */
6412 char **pzErr /* OUT: Error message (if any) */
6417 ** This function is called after the sqlite3expert object has been configured
6418 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6419 ** the analysis. Once this function has been called, it is not possible to
6420 ** add further SQL statements to the analysis.
6422 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6423 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6424 ** point to a buffer containing an English language error message. In this
6425 ** case it is the responsibility of the caller to eventually free the buffer
6426 ** using sqlite3_free().
6428 ** If an error does occur within this function, the sqlite3expert object
6429 ** is no longer useful for any purpose. At that point it is no longer
6430 ** possible to add further SQL statements to the object or to re-attempt
6431 ** the analysis. The sqlite3expert object must still be freed using a call
6432 ** sqlite3_expert_destroy().
6434 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6437 ** Return the total number of statements loaded using sqlite3_expert_sql().
6438 ** The total number of SQL statements may be different from the total number
6439 ** to calls to sqlite3_expert_sql().
6441 int sqlite3_expert_count(sqlite3expert*);
6444 ** Return a component of the report.
6446 ** This function is called after sqlite3_expert_analyze() to extract the
6447 ** results of the analysis. Each call to this function returns either a
6448 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6449 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6450 ** #define constants defined below.
6452 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6453 ** information relating to a specific SQL statement. In these cases that
6454 ** SQL statement is identified by the value passed as the second argument.
6455 ** SQL statements are numbered from 0 in the order in which they are parsed.
6456 ** If an out-of-range value (less than zero or equal to or greater than the
6457 ** value returned by sqlite3_expert_count()) is passed as the second argument
6458 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6460 ** EXPERT_REPORT_SQL:
6461 ** Return the text of SQL statement iStmt.
6463 ** EXPERT_REPORT_INDEXES:
6464 ** Return a buffer containing the CREATE INDEX statements for all recommended
6465 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
6468 ** EXPERT_REPORT_PLAN:
6469 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6470 ** iStmt after the proposed indexes have been added to the database schema.
6472 ** EXPERT_REPORT_CANDIDATES:
6473 ** Return a pointer to a buffer containing the CREATE INDEX statements
6474 ** for all indexes that were tested (for all SQL statements). The iStmt
6475 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6477 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6480 ** Values for the third argument passed to sqlite3_expert_report().
6482 #define EXPERT_REPORT_SQL 1
6483 #define EXPERT_REPORT_INDEXES 2
6484 #define EXPERT_REPORT_PLAN 3
6485 #define EXPERT_REPORT_CANDIDATES 4
6488 ** Free an (sqlite3expert*) handle and all associated resources. There
6489 ** should be one call to this function for each successful call to
6490 ** sqlite3-expert_new().
6492 void sqlite3_expert_destroy(sqlite3expert*);
6496 /************************* End ../ext/expert/sqlite3expert.h ********************/
6497 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6501 ** The author disclaims copyright to this source code. In place of
6502 ** a legal notice, here is a blessing:
6504 ** May you do good and not evil.
6505 ** May you find forgiveness for yourself and forgive others.
6506 ** May you share freely, never taking more than you give.
6508 *************************************************************************
6514 #ifndef SQLITE_OMIT_VIRTUALTABLE
6516 /* typedef sqlite3_int64 i64; */
6517 /* typedef sqlite3_uint64 u64; */
6519 typedef struct IdxColumn IdxColumn;
6520 typedef struct IdxConstraint IdxConstraint;
6521 typedef struct IdxScan IdxScan;
6522 typedef struct IdxStatement IdxStatement;
6523 typedef struct IdxTable IdxTable;
6524 typedef struct IdxWrite IdxWrite;
6526 #define STRLEN (int)strlen
6529 ** A temp table name that we assume no user database will actually use.
6530 ** If this assumption proves incorrect triggers on the table with the
6531 ** conflicting name will be ignored.
6533 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6536 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6537 ** any other type of single-ended range constraint on a column).
6540 ** Used to temporarily link IdxConstraint objects into lists while
6541 ** creating candidate indexes.
6543 struct IdxConstraint {
6544 char *zColl; /* Collation sequence */
6545 int bRange; /* True for range, false for eq */
6546 int iCol; /* Constrained table column */
6547 int bFlag; /* Used by idxFindCompatible() */
6548 int bDesc; /* True if ORDER BY <expr> DESC */
6549 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
6550 IdxConstraint *pLink; /* See above */
6554 ** A single scan of a single table.
6557 IdxTable *pTab; /* Associated table object */
6558 int iDb; /* Database containing table zTable */
6559 i64 covering; /* Mask of columns required for cov. index */
6560 IdxConstraint *pOrder; /* ORDER BY columns */
6561 IdxConstraint *pEq; /* List of == constraints */
6562 IdxConstraint *pRange; /* List of < constraints */
6563 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
6567 ** Information regarding a single database table. Extracted from
6568 ** "PRAGMA table_info" by function idxGetTableInfo().
6577 char *zName; /* Table name */
6579 IdxTable *pNext; /* Next table in linked list of all tables */
6583 ** An object of the following type is created for each unique table/write-op
6584 ** seen. The objects are stored in a singly-linked list beginning at
6585 ** sqlite3expert.pWrite.
6589 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
6594 ** Each statement being analyzed is represented by an instance of this
6597 struct IdxStatement {
6598 int iId; /* Statement number */
6599 char *zSql; /* SQL statement */
6600 char *zIdx; /* Indexes */
6601 char *zEQP; /* Plan */
6602 IdxStatement *pNext;
6607 ** A hash table for storing strings. With space for a payload string
6608 ** with each entry. Methods are:
6615 #define IDX_HASH_SIZE 1023
6616 typedef struct IdxHashEntry IdxHashEntry;
6617 typedef struct IdxHash IdxHash;
6618 struct IdxHashEntry {
6619 char *zKey; /* nul-terminated key */
6620 char *zVal; /* nul-terminated value string */
6621 char *zVal2; /* nul-terminated value string 2 */
6622 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
6623 IdxHashEntry *pNext; /* Next entry in hash */
6626 IdxHashEntry *pFirst;
6627 IdxHashEntry *aHash[IDX_HASH_SIZE];
6631 ** sqlite3expert object.
6633 struct sqlite3expert {
6634 int iSample; /* Percentage of tables to sample for stat1 */
6635 sqlite3 *db; /* User database */
6636 sqlite3 *dbm; /* In-memory db for this analysis */
6637 sqlite3 *dbv; /* Vtab schema for this analysis */
6638 IdxTable *pTable; /* List of all IdxTable objects */
6639 IdxScan *pScan; /* List of scan objects */
6640 IdxWrite *pWrite; /* List of write objects */
6641 IdxStatement *pStatement; /* List of IdxStatement objects */
6642 int bRun; /* True once analysis has run */
6644 int rc; /* Error code from whereinfo hook */
6645 IdxHash hIdx; /* Hash containing all candidate indexes */
6646 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
6651 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6652 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6654 static void *idxMalloc(int *pRc, int nByte){
6656 assert( *pRc==SQLITE_OK );
6658 pRet = sqlite3_malloc(nByte);
6660 memset(pRet, 0, nByte);
6662 *pRc = SQLITE_NOMEM;
6668 ** Initialize an IdxHash hash table.
6670 static void idxHashInit(IdxHash *pHash){
6671 memset(pHash, 0, sizeof(IdxHash));
6675 ** Reset an IdxHash hash table.
6677 static void idxHashClear(IdxHash *pHash){
6679 for(i=0; i<IDX_HASH_SIZE; i++){
6680 IdxHashEntry *pEntry;
6681 IdxHashEntry *pNext;
6682 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6683 pNext = pEntry->pHashNext;
6684 sqlite3_free(pEntry->zVal2);
6685 sqlite3_free(pEntry);
6688 memset(pHash, 0, sizeof(IdxHash));
6692 ** Return the index of the hash bucket that the string specified by the
6693 ** arguments to this function belongs.
6695 static int idxHashString(const char *z, int n){
6696 unsigned int ret = 0;
6699 ret += (ret<<3) + (unsigned char)(z[i]);
6701 return (int)(ret % IDX_HASH_SIZE);
6705 ** If zKey is already present in the hash table, return non-zero and do
6706 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6707 ** the hash table passed as the second argument.
6709 static int idxHashAdd(
6715 int nKey = STRLEN(zKey);
6716 int iHash = idxHashString(zKey, nKey);
6717 int nVal = (zVal ? STRLEN(zVal) : 0);
6718 IdxHashEntry *pEntry;
6720 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6721 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6725 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6727 pEntry->zKey = (char*)&pEntry[1];
6728 memcpy(pEntry->zKey, zKey, nKey);
6730 pEntry->zVal = &pEntry->zKey[nKey+1];
6731 memcpy(pEntry->zVal, zVal, nVal);
6733 pEntry->pHashNext = pHash->aHash[iHash];
6734 pHash->aHash[iHash] = pEntry;
6736 pEntry->pNext = pHash->pFirst;
6737 pHash->pFirst = pEntry;
6743 ** If zKey/nKey is present in the hash table, return a pointer to the
6744 ** hash-entry object.
6746 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6748 IdxHashEntry *pEntry;
6749 if( nKey<0 ) nKey = STRLEN(zKey);
6750 iHash = idxHashString(zKey, nKey);
6752 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6753 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6761 ** If the hash table contains an entry with a key equal to the string
6762 ** passed as the final two arguments to this function, return a pointer
6763 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6764 ** hash table, return NULL.
6766 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6767 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6768 if( pEntry ) return pEntry->zVal;
6773 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6774 ** variable to point to a copy of nul-terminated string zColl.
6776 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6777 IdxConstraint *pNew;
6778 int nColl = STRLEN(zColl);
6780 assert( *pRc==SQLITE_OK );
6781 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6783 pNew->zColl = (char*)&pNew[1];
6784 memcpy(pNew->zColl, zColl, nColl+1);
6790 ** An error associated with database handle db has just occurred. Pass
6791 ** the error message to callback function xOut.
6793 static void idxDatabaseError(
6794 sqlite3 *db, /* Database handle */
6795 char **pzErrmsg /* Write error here */
6797 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6801 ** Prepare an SQL statement.
6803 static int idxPrepareStmt(
6804 sqlite3 *db, /* Database handle to compile against */
6805 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6806 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6807 const char *zSql /* SQL statement to compile */
6809 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6810 if( rc!=SQLITE_OK ){
6812 idxDatabaseError(db, pzErrmsg);
6818 ** Prepare an SQL statement using the results of a printf() formatting.
6820 static int idxPrintfPrepareStmt(
6821 sqlite3 *db, /* Database handle to compile against */
6822 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6823 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6824 const char *zFmt, /* printf() format of SQL statement */
6825 ... /* Trailing printf() arguments */
6831 zSql = sqlite3_vmprintf(zFmt, ap);
6835 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
6843 /*************************************************************************
6844 ** Beginning of virtual table implementation.
6846 typedef struct ExpertVtab ExpertVtab;
6850 sqlite3expert *pExpert;
6853 typedef struct ExpertCsr ExpertCsr;
6855 sqlite3_vtab_cursor base;
6856 sqlite3_stmt *pData;
6859 static char *expertDequote(const char *zIn){
6860 int n = STRLEN(zIn);
6861 char *zRet = sqlite3_malloc(n);
6863 assert( zIn[0]=='\'' );
6864 assert( zIn[n-1]=='\'' );
6869 for(iIn=1; iIn<(n-1); iIn++){
6870 if( zIn[iIn]=='\'' ){
6871 assert( zIn[iIn+1]=='\'' );
6874 zRet[iOut++] = zIn[iIn];
6883 ** This function is the implementation of both the xConnect and xCreate
6884 ** methods of the r-tree virtual table.
6886 ** argv[0] -> module name
6887 ** argv[1] -> database name
6888 ** argv[2] -> table name
6889 ** argv[...] -> column names...
6891 static int expertConnect(
6894 int argc, const char *const*argv,
6895 sqlite3_vtab **ppVtab,
6898 sqlite3expert *pExpert = (sqlite3expert*)pAux;
6903 *pzErr = sqlite3_mprintf("internal error!");
6906 char *zCreateTable = expertDequote(argv[3]);
6908 rc = sqlite3_declare_vtab(db, zCreateTable);
6909 if( rc==SQLITE_OK ){
6910 p = idxMalloc(&rc, sizeof(ExpertVtab));
6912 if( rc==SQLITE_OK ){
6913 p->pExpert = pExpert;
6914 p->pTab = pExpert->pTable;
6915 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
6917 sqlite3_free(zCreateTable);
6923 *ppVtab = (sqlite3_vtab*)p;
6927 static int expertDisconnect(sqlite3_vtab *pVtab){
6928 ExpertVtab *p = (ExpertVtab*)pVtab;
6933 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
6934 ExpertVtab *p = (ExpertVtab*)pVtab;
6939 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
6940 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
6941 SQLITE_INDEX_CONSTRAINT_LE;
6943 pScan = idxMalloc(&rc, sizeof(IdxScan));
6947 /* Link the new scan object into the list */
6948 pScan->pTab = p->pTab;
6949 pScan->pNextScan = p->pExpert->pScan;
6950 p->pExpert->pScan = pScan;
6952 /* Add the constraints to the IdxScan object */
6953 for(i=0; i<pIdxInfo->nConstraint; i++){
6954 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
6956 && pCons->iColumn>=0
6957 && p->pTab->aCol[pCons->iColumn].iPk==0
6958 && (pCons->op & opmask)
6960 IdxConstraint *pNew;
6961 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
6962 pNew = idxNewConstraint(&rc, zColl);
6964 pNew->iCol = pCons->iColumn;
6965 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6966 pNew->pNext = pScan->pEq;
6970 pNew->pNext = pScan->pRange;
6971 pScan->pRange = pNew;
6975 pIdxInfo->aConstraintUsage[i].argvIndex = n;
6979 /* Add the ORDER BY to the IdxScan object */
6980 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
6981 int iCol = pIdxInfo->aOrderBy[i].iColumn;
6983 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
6986 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
6987 pNew->pNext = pScan->pOrder;
6988 pNew->pLink = pScan->pOrder;
6989 pScan->pOrder = pNew;
6996 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7000 static int expertUpdate(
7001 sqlite3_vtab *pVtab,
7003 sqlite3_value **azData,
7004 sqlite_int64 *pRowid
7014 ** Virtual table module xOpen method.
7016 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7020 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7021 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7026 ** Virtual table module xClose method.
7028 static int expertClose(sqlite3_vtab_cursor *cur){
7029 ExpertCsr *pCsr = (ExpertCsr*)cur;
7030 sqlite3_finalize(pCsr->pData);
7036 ** Virtual table module xEof method.
7038 ** Return non-zero if the cursor does not currently point to a valid
7039 ** record (i.e if the scan has finished), or zero otherwise.
7041 static int expertEof(sqlite3_vtab_cursor *cur){
7042 ExpertCsr *pCsr = (ExpertCsr*)cur;
7043 return pCsr->pData==0;
7047 ** Virtual table module xNext method.
7049 static int expertNext(sqlite3_vtab_cursor *cur){
7050 ExpertCsr *pCsr = (ExpertCsr*)cur;
7053 assert( pCsr->pData );
7054 rc = sqlite3_step(pCsr->pData);
7055 if( rc!=SQLITE_ROW ){
7056 rc = sqlite3_finalize(pCsr->pData);
7066 ** Virtual table module xRowid method.
7068 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7075 ** Virtual table module xColumn method.
7077 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7078 ExpertCsr *pCsr = (ExpertCsr*)cur;
7079 sqlite3_value *pVal;
7080 pVal = sqlite3_column_value(pCsr->pData, i);
7082 sqlite3_result_value(ctx, pVal);
7088 ** Virtual table module xFilter method.
7090 static int expertFilter(
7091 sqlite3_vtab_cursor *cur,
7092 int idxNum, const char *idxStr,
7093 int argc, sqlite3_value **argv
7095 ExpertCsr *pCsr = (ExpertCsr*)cur;
7096 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7097 sqlite3expert *pExpert = pVtab->pExpert;
7104 rc = sqlite3_finalize(pCsr->pData);
7106 if( rc==SQLITE_OK ){
7107 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7108 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7112 if( rc==SQLITE_OK ){
7113 rc = expertNext(cur);
7118 static int idxRegisterVtab(sqlite3expert *p){
7119 static sqlite3_module expertModule = {
7121 expertConnect, /* xCreate - create a table */
7122 expertConnect, /* xConnect - connect to an existing table */
7123 expertBestIndex, /* xBestIndex - Determine search strategy */
7124 expertDisconnect, /* xDisconnect - Disconnect from a table */
7125 expertDisconnect, /* xDestroy - Drop a table */
7126 expertOpen, /* xOpen - open a cursor */
7127 expertClose, /* xClose - close a cursor */
7128 expertFilter, /* xFilter - configure scan constraints */
7129 expertNext, /* xNext - advance a cursor */
7130 expertEof, /* xEof */
7131 expertColumn, /* xColumn - read data */
7132 expertRowid, /* xRowid - read data */
7133 expertUpdate, /* xUpdate - write data */
7134 0, /* xBegin - begin transaction */
7135 0, /* xSync - sync transaction */
7136 0, /* xCommit - commit transaction */
7137 0, /* xRollback - rollback transaction */
7138 0, /* xFindFunction - function overloading */
7139 0, /* xRename - rename the table */
7142 0, /* xRollbackTo */
7145 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7148 ** End of virtual table implementation.
7149 *************************************************************************/
7151 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7152 ** is called, set it to the return value of sqlite3_finalize() before
7153 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7155 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7156 int rc = sqlite3_finalize(pStmt);
7157 if( *pRc==SQLITE_OK ) *pRc = rc;
7161 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7162 ** in the main database of connection db. If successful, set (*ppOut) to
7163 ** point to the new object and return SQLITE_OK. Otherwise, return an
7164 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7165 ** set to point to an error string.
7167 ** It is the responsibility of the caller to eventually free either the
7168 ** IdxTable object or error message using sqlite3_free().
7170 static int idxGetTableInfo(
7171 sqlite3 *db, /* Database connection to read details from */
7172 const char *zTab, /* Table name */
7173 IdxTable **ppOut, /* OUT: New object (if successful) */
7174 char **pzErrmsg /* OUT: Error message (if not) */
7176 sqlite3_stmt *p1 = 0;
7178 int nTab = STRLEN(zTab);
7179 int nByte = sizeof(IdxTable) + nTab + 1;
7184 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7185 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7186 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7187 nByte += 1 + STRLEN(zCol);
7188 rc = sqlite3_table_column_metadata(
7189 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7191 nByte += 1 + STRLEN(zCol);
7194 rc2 = sqlite3_reset(p1);
7195 if( rc==SQLITE_OK ) rc = rc2;
7197 nByte += sizeof(IdxColumn) * nCol;
7198 if( rc==SQLITE_OK ){
7199 pNew = idxMalloc(&rc, nByte);
7201 if( rc==SQLITE_OK ){
7202 pNew->aCol = (IdxColumn*)&pNew[1];
7204 pCsr = (char*)&pNew->aCol[nCol];
7208 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7209 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7210 int nCopy = STRLEN(zCol) + 1;
7211 pNew->aCol[nCol].zName = pCsr;
7212 pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7213 memcpy(pCsr, zCol, nCopy);
7216 rc = sqlite3_table_column_metadata(
7217 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7219 if( rc==SQLITE_OK ){
7220 nCopy = STRLEN(zCol) + 1;
7221 pNew->aCol[nCol].zColl = pCsr;
7222 memcpy(pCsr, zCol, nCopy);
7228 idxFinalize(&rc, p1);
7230 if( rc!=SQLITE_OK ){
7235 memcpy(pNew->zName, zTab, nTab+1);
7243 ** This function is a no-op if *pRc is set to anything other than
7244 ** SQLITE_OK when it is called.
7246 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7247 ** the printf() style arguments is appended to zIn and the result returned
7248 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7249 ** zIn before returning.
7251 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7255 int nIn = zIn ? STRLEN(zIn) : 0;
7258 if( *pRc==SQLITE_OK ){
7259 zAppend = sqlite3_vmprintf(zFmt, ap);
7261 nAppend = STRLEN(zAppend);
7262 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7264 if( zAppend && zRet ){
7265 if( nIn ) memcpy(zRet, zIn, nIn);
7266 memcpy(&zRet[nIn], zAppend, nAppend+1);
7270 *pRc = SQLITE_NOMEM;
7272 sqlite3_free(zAppend);
7280 ** Return true if zId must be quoted in order to use it as an SQL
7281 ** identifier, or false otherwise.
7283 static int idxIdentifierRequiresQuotes(const char *zId){
7285 for(i=0; zId[i]; i++){
7287 && !(zId[i]>='0' && zId[i]<='9')
7288 && !(zId[i]>='a' && zId[i]<='z')
7289 && !(zId[i]>='A' && zId[i]<='Z')
7298 ** This function appends an index column definition suitable for constraint
7299 ** pCons to the string passed as zIn and returns the result.
7301 static char *idxAppendColDefn(
7302 int *pRc, /* IN/OUT: Error code */
7303 char *zIn, /* Column defn accumulated so far */
7304 IdxTable *pTab, /* Table index will be created on */
7305 IdxConstraint *pCons
7308 IdxColumn *p = &pTab->aCol[pCons->iCol];
7309 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7311 if( idxIdentifierRequiresQuotes(p->zName) ){
7312 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7314 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7317 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7318 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7319 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7321 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7326 zRet = idxAppendText(pRc, zRet, " DESC");
7332 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7333 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7334 ** such an index is found, return non-zero. Or, if no such index is found,
7337 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7339 static int idxFindCompatible(
7340 int *pRc, /* OUT: Error code */
7341 sqlite3* dbm, /* Database to search */
7342 IdxScan *pScan, /* Scan for table to search for index on */
7343 IdxConstraint *pEq, /* List of == constraints */
7344 IdxConstraint *pTail /* List of range constraints */
7346 const char *zTbl = pScan->pTab->zName;
7347 sqlite3_stmt *pIdxList = 0;
7348 IdxConstraint *pIter;
7349 int nEq = 0; /* Number of elements in pEq */
7352 /* Count the elements in list pEq */
7353 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7355 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7356 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7358 IdxConstraint *pT = pTail;
7359 sqlite3_stmt *pInfo = 0;
7360 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7362 /* Zero the IdxConstraint.bFlag values in the pEq list */
7363 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7365 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7366 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7367 int iIdx = sqlite3_column_int(pInfo, 0);
7368 int iCol = sqlite3_column_int(pInfo, 1);
7369 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7372 for(pIter=pEq; pIter; pIter=pIter->pLink){
7373 if( pIter->bFlag ) continue;
7374 if( pIter->iCol!=iCol ) continue;
7375 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7385 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7393 idxFinalize(&rc, pInfo);
7395 if( rc==SQLITE_OK && bMatch ){
7396 sqlite3_finalize(pIdxList);
7400 idxFinalize(&rc, pIdxList);
7406 static int idxCreateFromCons(
7410 IdxConstraint *pTail
7412 sqlite3 *dbm = p->dbm;
7414 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7415 IdxTable *pTab = pScan->pTab;
7418 IdxConstraint *pCons;
7422 for(pCons=pEq; pCons; pCons=pCons->pLink){
7423 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7425 for(pCons=pTail; pCons; pCons=pCons->pLink){
7426 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7429 if( rc==SQLITE_OK ){
7430 /* Hash the list of columns to come up with a name for the index */
7431 const char *zTable = pScan->pTab->zName;
7432 char *zName; /* Index name */
7434 for(i=0; zCols[i]; i++){
7435 h += ((h<<3) + zCols[i]);
7437 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7441 if( idxIdentifierRequiresQuotes(zTable) ){
7442 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7444 zFmt = "CREATE INDEX %s ON %s(%s)";
7446 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7450 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7451 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7453 sqlite3_free(zName);
7458 sqlite3_free(zCols);
7464 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7465 ** a constraint compatible with *p. Otherwise return false.
7467 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7468 IdxConstraint *pCmp;
7469 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7470 if( p->iCol==pCmp->iCol ) return 1;
7475 static int idxCreateFromWhere(
7477 IdxScan *pScan, /* Create indexes for this scan */
7478 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
7480 IdxConstraint *p1 = 0;
7481 IdxConstraint *pCon;
7484 /* Gather up all the == constraints. */
7485 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7486 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7492 /* Create an index using the == constraints collected above. And the
7493 ** range constraint/ORDER BY terms passed in by the caller, if any. */
7494 rc = idxCreateFromCons(p, pScan, p1, pTail);
7496 /* If no range/ORDER BY passed by the caller, create a version of the
7497 ** index for each range constraint. */
7499 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7500 assert( pCon->pLink==0 );
7501 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7502 rc = idxCreateFromCons(p, pScan, p1, pCon);
7511 ** Create candidate indexes in database [dbm] based on the data in
7512 ** linked-list pScan.
7514 static int idxCreateCandidates(sqlite3expert *p){
7518 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7519 rc = idxCreateFromWhere(p, pIter, 0);
7520 if( rc==SQLITE_OK && pIter->pOrder ){
7521 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7529 ** Free all elements of the linked list starting at pConstraint.
7531 static void idxConstraintFree(IdxConstraint *pConstraint){
7532 IdxConstraint *pNext;
7535 for(p=pConstraint; p; p=pNext){
7542 ** Free all elements of the linked list starting from pScan up until pLast
7543 ** (pLast is not freed).
7545 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7548 for(p=pScan; p!=pLast; p=pNext){
7549 pNext = p->pNextScan;
7550 idxConstraintFree(p->pOrder);
7551 idxConstraintFree(p->pEq);
7552 idxConstraintFree(p->pRange);
7558 ** Free all elements of the linked list starting from pStatement up
7559 ** until pLast (pLast is not freed).
7561 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7563 IdxStatement *pNext;
7564 for(p=pStatement; p!=pLast; p=pNext){
7566 sqlite3_free(p->zEQP);
7567 sqlite3_free(p->zIdx);
7573 ** Free the linked list of IdxTable objects starting at pTab.
7575 static void idxTableFree(IdxTable *pTab){
7578 for(pIter=pTab; pIter; pIter=pNext){
7579 pNext = pIter->pNext;
7580 sqlite3_free(pIter);
7585 ** Free the linked list of IdxWrite objects starting at pTab.
7587 static void idxWriteFree(IdxWrite *pTab){
7590 for(pIter=pTab; pIter; pIter=pNext){
7591 pNext = pIter->pNext;
7592 sqlite3_free(pIter);
7599 ** This function is called after candidate indexes have been created. It
7600 ** runs all the queries to see which indexes they prefer, and populates
7601 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7605 char **pzErr /* OUT: Error message (sqlite3_malloc) */
7607 IdxStatement *pStmt;
7608 sqlite3 *dbm = p->dbm;
7614 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7615 IdxHashEntry *pEntry;
7616 sqlite3_stmt *pExplain = 0;
7617 idxHashClear(&hIdx);
7618 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7619 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7621 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7622 /* int iId = sqlite3_column_int(pExplain, 0); */
7623 /* int iParent = sqlite3_column_int(pExplain, 1); */
7624 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7625 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7626 int nDetail = STRLEN(zDetail);
7629 for(i=0; i<nDetail; i++){
7630 const char *zIdx = 0;
7631 if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7632 zIdx = &zDetail[i+13];
7633 }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7634 zIdx = &zDetail[i+22];
7639 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7642 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7644 idxHashAdd(&rc, &hIdx, zSql, 0);
7645 if( rc ) goto find_indexes_out;
7651 if( zDetail[0]!='-' ){
7652 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7656 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7657 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7660 idxFinalize(&rc, pExplain);
7664 idxHashClear(&hIdx);
7668 static int idxAuthCallback(
7674 const char *zTrigger
7679 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7680 if( sqlite3_stricmp(zDb, "main")==0 ){
7681 sqlite3expert *p = (sqlite3expert*)pCtx;
7683 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7684 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7688 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7689 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7692 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7693 if( rc==SQLITE_OK ){
7694 pWrite->pTab = pTab;
7696 pWrite->pNext = p->pWrite;
7706 static int idxProcessOneTrigger(
7711 static const char *zInt = UNIQUE_TABLE_NAME;
7712 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7713 IdxTable *pTab = pWrite->pTab;
7714 const char *zTab = pTab->zName;
7716 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7717 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7719 sqlite3_stmt *pSelect = 0;
7723 /* Create the table and its triggers in the temp schema */
7724 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7725 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7726 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7727 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7729 idxFinalize(&rc, pSelect);
7731 /* Rename the table in the temp schema to zInt */
7732 if( rc==SQLITE_OK ){
7733 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7737 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7742 switch( pWrite->eOp ){
7743 case SQLITE_INSERT: {
7745 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7746 for(i=0; i<pTab->nCol; i++){
7747 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7749 zWrite = idxAppendText(&rc, zWrite, ")");
7752 case SQLITE_UPDATE: {
7754 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7755 for(i=0; i<pTab->nCol; i++){
7756 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7763 assert( pWrite->eOp==SQLITE_DELETE );
7764 if( rc==SQLITE_OK ){
7765 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7766 if( zWrite==0 ) rc = SQLITE_NOMEM;
7771 if( rc==SQLITE_OK ){
7772 sqlite3_stmt *pX = 0;
7773 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7774 idxFinalize(&rc, pX);
7775 if( rc!=SQLITE_OK ){
7776 idxDatabaseError(p->dbv, pzErr);
7779 sqlite3_free(zWrite);
7781 if( rc==SQLITE_OK ){
7782 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7788 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7791 IdxWrite *pFirst = p->pWrite;
7793 while( rc==SQLITE_OK && pFirst!=pEnd ){
7795 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7796 rc = idxProcessOneTrigger(p, pIter, pzErr);
7806 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7807 int rc = idxRegisterVtab(p);
7808 sqlite3_stmt *pSchema = 0;
7810 /* For each table in the main db schema:
7812 ** 1) Add an entry to the p->pTable list, and
7813 ** 2) Create the equivalent virtual table in dbv.
7815 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7816 "SELECT type, name, sql, 1 FROM sqlite_master "
7817 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
7819 "SELECT type, name, sql, 2 FROM sqlite_master "
7820 "WHERE type = 'trigger'"
7821 " AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
7824 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
7825 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
7826 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
7827 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
7829 if( zType[0]=='v' || zType[1]=='r' ){
7830 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
7833 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
7834 if( rc==SQLITE_OK ){
7838 pTab->pNext = p->pTable;
7841 /* The statement the vtab will pass to sqlite3_declare_vtab() */
7842 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
7843 for(i=0; i<pTab->nCol; i++){
7844 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
7845 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
7848 zInner = idxAppendText(&rc, zInner, ")");
7850 /* The CVT statement to create the vtab */
7851 zOuter = idxAppendText(&rc, 0,
7852 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
7854 if( rc==SQLITE_OK ){
7855 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
7857 sqlite3_free(zInner);
7858 sqlite3_free(zOuter);
7862 idxFinalize(&rc, pSchema);
7866 struct IdxSampleCtx {
7868 double target; /* Target nRet/nRow value */
7869 double nRow; /* Number of rows seen */
7870 double nRet; /* Number of rows returned */
7873 static void idxSampleFunc(
7874 sqlite3_context *pCtx,
7876 sqlite3_value **argv
7878 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
7886 bRet = (p->nRet / p->nRow) <= p->target;
7889 sqlite3_randomness(2, (void*)&rnd);
7890 bRet = ((int)rnd % 100) <= p->iTarget;
7894 sqlite3_result_int(pCtx, bRet);
7896 p->nRet += (double)bRet;
7902 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
7903 i64 iVal; /* SQLITE_INTEGER value */
7904 double rVal; /* SQLITE_FLOAT value */
7905 int nByte; /* Bytes of space allocated at z */
7906 int n; /* Size of buffer z */
7907 char *z; /* SQLITE_TEXT/BLOB value */
7912 ** Implementation of scalar function rem().
7914 static void idxRemFunc(
7915 sqlite3_context *pCtx,
7917 sqlite3_value **argv
7919 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
7920 struct IdxRemSlot *pSlot;
7924 iSlot = sqlite3_value_int(argv[0]);
7925 assert( iSlot<=p->nSlot );
7926 pSlot = &p->aSlot[iSlot];
7928 switch( pSlot->eType ){
7933 case SQLITE_INTEGER:
7934 sqlite3_result_int64(pCtx, pSlot->iVal);
7938 sqlite3_result_double(pCtx, pSlot->rVal);
7942 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7946 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7950 pSlot->eType = sqlite3_value_type(argv[1]);
7951 switch( pSlot->eType ){
7956 case SQLITE_INTEGER:
7957 pSlot->iVal = sqlite3_value_int64(argv[1]);
7961 pSlot->rVal = sqlite3_value_double(argv[1]);
7966 int nByte = sqlite3_value_bytes(argv[1]);
7967 if( nByte>pSlot->nByte ){
7968 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
7970 sqlite3_result_error_nomem(pCtx);
7973 pSlot->nByte = nByte*2;
7977 if( pSlot->eType==SQLITE_BLOB ){
7978 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
7980 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
7987 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
7990 "SELECT max(i.seqno) FROM "
7991 " sqlite_master AS s, "
7992 " pragma_index_list(s.name) AS l, "
7993 " pragma_index_info(l.name) AS i "
7994 "WHERE s.type = 'table'";
7995 sqlite3_stmt *pMax = 0;
7998 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
7999 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8000 *pnMax = sqlite3_column_int(pMax, 0) + 1;
8002 idxFinalize(&rc, pMax);
8007 static int idxPopulateOneStat1(
8009 sqlite3_stmt *pIndexXInfo,
8010 sqlite3_stmt *pWriteStat,
8020 sqlite3_stmt *pQuery = 0;
8024 assert( p->iSample>0 );
8026 /* Formulate the query text */
8027 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8028 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8029 const char *zComma = zCols==0 ? "" : ", ";
8030 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8031 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8032 zCols = idxAppendText(&rc, zCols,
8033 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8035 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8037 sqlite3_reset(pIndexXInfo);
8038 if( rc==SQLITE_OK ){
8039 if( p->iSample==100 ){
8040 zQuery = sqlite3_mprintf(
8041 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8044 zQuery = sqlite3_mprintf(
8045 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8049 sqlite3_free(zCols);
8050 sqlite3_free(zOrder);
8052 /* Formulate the query text */
8053 if( rc==SQLITE_OK ){
8054 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8055 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8057 sqlite3_free(zQuery);
8059 if( rc==SQLITE_OK ){
8060 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8062 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8063 IdxHashEntry *pEntry;
8065 for(i=0; i<=nCol; i++) aStat[i] = 1;
8066 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8068 for(i=0; i<nCol; i++){
8069 if( sqlite3_column_int(pQuery, i)==0 ) break;
8071 for(/*no-op*/; i<nCol; i++){
8076 if( rc==SQLITE_OK ){
8078 zStat = sqlite3_mprintf("%d", s0);
8079 if( zStat==0 ) rc = SQLITE_NOMEM;
8080 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8081 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8085 if( rc==SQLITE_OK ){
8086 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8087 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8088 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8089 sqlite3_step(pWriteStat);
8090 rc = sqlite3_reset(pWriteStat);
8093 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8095 assert( pEntry->zVal2==0 );
8096 pEntry->zVal2 = zStat;
8098 sqlite3_free(zStat);
8101 sqlite3_free(aStat);
8102 idxFinalize(&rc, pQuery);
8107 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8111 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8112 if( rc!=SQLITE_OK ) return rc;
8114 zSql = sqlite3_mprintf(
8115 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8117 if( zSql==0 ) return SQLITE_NOMEM;
8118 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8125 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8126 ** indexes have already been created in database sqlite3expert.dbm, this
8127 ** function populates sqlite_stat1 table in the same database.
8129 ** The stat1 data is generated by querying the
8131 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8134 struct IdxRemCtx *pCtx = 0;
8135 struct IdxSampleCtx samplectx;
8137 i64 iPrev = -100000;
8138 sqlite3_stmt *pAllIndex = 0;
8139 sqlite3_stmt *pIndexXInfo = 0;
8140 sqlite3_stmt *pWrite = 0;
8142 const char *zAllIndex =
8143 "SELECT s.rowid, s.name, l.name FROM "
8144 " sqlite_master AS s, "
8145 " pragma_index_list(s.name) AS l "
8146 "WHERE s.type = 'table'";
8147 const char *zIndexXInfo =
8148 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8149 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8151 /* If iSample==0, no sqlite_stat1 data is required. */
8152 if( p->iSample==0 ) return SQLITE_OK;
8154 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8155 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8157 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8159 if( rc==SQLITE_OK ){
8160 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8161 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8164 if( rc==SQLITE_OK ){
8165 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8166 rc = sqlite3_create_function(
8167 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8170 if( rc==SQLITE_OK ){
8171 rc = sqlite3_create_function(
8172 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8176 if( rc==SQLITE_OK ){
8177 pCtx->nSlot = nMax+1;
8178 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8180 if( rc==SQLITE_OK ){
8181 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8183 if( rc==SQLITE_OK ){
8184 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8187 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8188 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8189 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8190 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8191 if( p->iSample<100 && iPrev!=iRowid ){
8192 samplectx.target = (double)p->iSample / 100.0;
8193 samplectx.iTarget = p->iSample;
8194 samplectx.nRow = 0.0;
8195 samplectx.nRet = 0.0;
8196 rc = idxBuildSampleTable(p, zTab);
8197 if( rc!=SQLITE_OK ) break;
8199 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8202 if( rc==SQLITE_OK && p->iSample<100 ){
8203 rc = sqlite3_exec(p->dbv,
8204 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8208 idxFinalize(&rc, pAllIndex);
8209 idxFinalize(&rc, pIndexXInfo);
8210 idxFinalize(&rc, pWrite);
8212 for(i=0; i<pCtx->nSlot; i++){
8213 sqlite3_free(pCtx->aSlot[i].z);
8217 if( rc==SQLITE_OK ){
8218 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8221 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8226 ** Allocate a new sqlite3expert object.
8228 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8230 sqlite3expert *pNew;
8232 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8234 /* Open two in-memory databases to work with. The "vtab database" (dbv)
8235 ** will contain a virtual table corresponding to each real table in
8236 ** the user database schema, and a copy of each view. It is used to
8237 ** collect information regarding the WHERE, ORDER BY and other clauses
8238 ** of the user's query.
8240 if( rc==SQLITE_OK ){
8242 pNew->iSample = 100;
8243 rc = sqlite3_open(":memory:", &pNew->dbv);
8245 if( rc==SQLITE_OK ){
8246 rc = sqlite3_open(":memory:", &pNew->dbm);
8247 if( rc==SQLITE_OK ){
8248 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8253 /* Copy the entire schema of database [db] into [dbm]. */
8254 if( rc==SQLITE_OK ){
8256 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8257 "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8258 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8260 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8261 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8262 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8264 idxFinalize(&rc, pSql);
8267 /* Create the vtab schema */
8268 if( rc==SQLITE_OK ){
8269 rc = idxCreateVtabSchema(pNew, pzErrmsg);
8272 /* Register the auth callback with dbv */
8273 if( rc==SQLITE_OK ){
8274 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8277 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8278 ** return the new sqlite3expert handle. */
8279 if( rc!=SQLITE_OK ){
8280 sqlite3_expert_destroy(pNew);
8287 ** Configure an sqlite3expert object.
8289 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8294 case EXPERT_CONFIG_SAMPLE: {
8295 int iVal = va_arg(ap, int);
8296 if( iVal<0 ) iVal = 0;
8297 if( iVal>100 ) iVal = 100;
8302 rc = SQLITE_NOTFOUND;
8311 ** Add an SQL statement to the analysis.
8313 int sqlite3_expert_sql(
8314 sqlite3expert *p, /* From sqlite3_expert_new() */
8315 const char *zSql, /* SQL statement to add */
8316 char **pzErr /* OUT: Error message (if any) */
8318 IdxScan *pScanOrig = p->pScan;
8319 IdxStatement *pStmtOrig = p->pStatement;
8321 const char *zStmt = zSql;
8323 if( p->bRun ) return SQLITE_MISUSE;
8325 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8326 sqlite3_stmt *pStmt = 0;
8327 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8328 if( rc==SQLITE_OK ){
8331 const char *z = sqlite3_sql(pStmt);
8333 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8334 if( rc==SQLITE_OK ){
8335 pNew->zSql = (char*)&pNew[1];
8336 memcpy(pNew->zSql, z, n+1);
8337 pNew->pNext = p->pStatement;
8338 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8339 p->pStatement = pNew;
8341 sqlite3_finalize(pStmt);
8344 idxDatabaseError(p->dbv, pzErr);
8348 if( rc!=SQLITE_OK ){
8349 idxScanFree(p->pScan, pScanOrig);
8350 idxStatementFree(p->pStatement, pStmtOrig);
8351 p->pScan = pScanOrig;
8352 p->pStatement = pStmtOrig;
8358 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8360 IdxHashEntry *pEntry;
8362 /* Do trigger processing to collect any extra IdxScan structures */
8363 rc = idxProcessTriggers(p, pzErr);
8365 /* Create candidate indexes within the in-memory database file */
8366 if( rc==SQLITE_OK ){
8367 rc = idxCreateCandidates(p);
8370 /* Generate the stat1 data */
8371 if( rc==SQLITE_OK ){
8372 rc = idxPopulateStat1(p, pzErr);
8375 /* Formulate the EXPERT_REPORT_CANDIDATES text */
8376 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8377 p->zCandidates = idxAppendText(&rc, p->zCandidates,
8378 "%s;%s%s\n", pEntry->zVal,
8379 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8383 /* Figure out which of the candidate indexes are preferred by the query
8384 ** planner and report the results to the user. */
8385 if( rc==SQLITE_OK ){
8386 rc = idxFindIndexes(p, pzErr);
8389 if( rc==SQLITE_OK ){
8396 ** Return the total number of statements that have been added to this
8397 ** sqlite3expert using sqlite3_expert_sql().
8399 int sqlite3_expert_count(sqlite3expert *p){
8401 if( p->pStatement ) nRet = p->pStatement->iId+1;
8406 ** Return a component of the report.
8408 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8409 const char *zRet = 0;
8410 IdxStatement *pStmt;
8412 if( p->bRun==0 ) return 0;
8413 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8415 case EXPERT_REPORT_SQL:
8416 if( pStmt ) zRet = pStmt->zSql;
8418 case EXPERT_REPORT_INDEXES:
8419 if( pStmt ) zRet = pStmt->zIdx;
8421 case EXPERT_REPORT_PLAN:
8422 if( pStmt ) zRet = pStmt->zEQP;
8424 case EXPERT_REPORT_CANDIDATES:
8425 zRet = p->zCandidates;
8432 ** Free an sqlite3expert object.
8434 void sqlite3_expert_destroy(sqlite3expert *p){
8436 sqlite3_close(p->dbm);
8437 sqlite3_close(p->dbv);
8438 idxScanFree(p->pScan, 0);
8439 idxStatementFree(p->pStatement, 0);
8440 idxTableFree(p->pTable);
8441 idxWriteFree(p->pWrite);
8442 idxHashClear(&p->hIdx);
8443 sqlite3_free(p->zCandidates);
8448 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8450 /************************* End ../ext/expert/sqlite3expert.c ********************/
8452 #if defined(SQLITE_ENABLE_SESSION)
8454 ** State information for a single open session
8456 typedef struct OpenSession OpenSession;
8457 struct OpenSession {
8458 char *zName; /* Symbolic name for this session */
8459 int nFilter; /* Number of xFilter rejection GLOB patterns */
8460 char **azFilter; /* Array of xFilter rejection GLOB patterns */
8461 sqlite3_session *p; /* The open session */
8466 ** Shell output mode information from before ".explain on",
8467 ** saved so that it can be restored by ".explain off"
8469 typedef struct SavedModeInfo SavedModeInfo;
8470 struct SavedModeInfo {
8471 int valid; /* Is there legit data in here? */
8472 int mode; /* Mode prior to ".explain on" */
8473 int showHeader; /* The ".header" setting prior to ".explain on" */
8474 int colWidth[100]; /* Column widths prior to ".explain on" */
8477 typedef struct ExpertInfo ExpertInfo;
8479 sqlite3expert *pExpert;
8483 /* A single line in the EQP output */
8484 typedef struct EQPGraphRow EQPGraphRow;
8485 struct EQPGraphRow {
8486 int iEqpId; /* ID for this row */
8487 int iParentId; /* ID of the parent row */
8488 EQPGraphRow *pNext; /* Next row in sequence */
8489 char zText[1]; /* Text to display for this row */
8492 /* All EQP output is collected into an instance of the following */
8493 typedef struct EQPGraph EQPGraph;
8495 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
8496 EQPGraphRow *pLast; /* Last element of the pRow list */
8497 char zPrefix[100]; /* Graph prefix */
8501 ** State information about the database connection is contained in an
8502 ** instance of the following structure.
8504 typedef struct ShellState ShellState;
8506 sqlite3 *db; /* The database */
8507 u8 autoExplain; /* Automatically turn on .explain mode */
8508 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8509 u8 autoEQPtest; /* autoEQP is in test mode */
8510 u8 statsOn; /* True to display memory stats before each finalize */
8511 u8 scanstatsOn; /* True to display scan stats before each finalize */
8512 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8513 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
8514 u8 nEqpLevel; /* Depth of the EQP output graph */
8515 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
8516 int outCount; /* Revert to stdout when reaching zero */
8517 int cnt; /* Number of records displayed so far */
8518 FILE *out; /* Write results here */
8519 FILE *traceOut; /* Output for sqlite3_trace() */
8520 int nErr; /* Number of errors seen */
8521 int mode; /* An output mode setting */
8522 int modePrior; /* Saved mode */
8523 int cMode; /* temporary output mode for the current query */
8524 int normalMode; /* Output mode before ".explain on" */
8525 int writableSchema; /* True if PRAGMA writable_schema=ON */
8526 int showHeader; /* True to show column names in List or Column mode */
8527 int nCheck; /* Number of ".check" commands run */
8528 unsigned shellFlgs; /* Various flags */
8529 char *zDestTable; /* Name of destination table when MODE_Insert */
8530 char *zTempFile; /* Temporary file that might need deleting */
8531 char zTestcase[30]; /* Name of current test case */
8532 char colSeparator[20]; /* Column separator character for several modes */
8533 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
8534 char colSepPrior[20]; /* Saved column separator */
8535 char rowSepPrior[20]; /* Saved row separator */
8536 int colWidth[100]; /* Requested width of each column when in column mode*/
8537 int actualWidth[100]; /* Actual width of each column */
8538 char nullValue[20]; /* The text to print when a NULL comes back from
8540 char outfile[FILENAME_MAX]; /* Filename for *out */
8541 const char *zDbFilename; /* name of the database file */
8542 char *zFreeOnClose; /* Filename to free when closing */
8543 const char *zVfs; /* Name of VFS to use */
8544 sqlite3_stmt *pStmt; /* Current statement if any. */
8545 FILE *pLog; /* Write log output here */
8546 int *aiIndent; /* Array of indents used in MODE_Explain */
8547 int nIndent; /* Size of array aiIndent[] */
8548 int iIndent; /* Index of current op in aiIndent[] */
8549 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
8550 #if defined(SQLITE_ENABLE_SESSION)
8551 int nSession; /* Number of active sessions */
8552 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
8554 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
8558 /* Allowed values for ShellState.autoEQP
8560 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
8561 #define AUTOEQP_on 1 /* Automatic EQP is on */
8562 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
8563 #define AUTOEQP_full 3 /* Show full EXPLAIN */
8565 /* Allowed values for ShellState.openMode
8567 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8568 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
8569 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
8570 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
8571 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
8574 ** These are the allowed shellFlgs values
8576 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
8577 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
8578 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
8579 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
8580 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
8581 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
8582 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
8585 ** Macros for testing and setting shellFlgs
8587 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
8588 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
8589 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
8592 ** These are the allowed modes.
8594 #define MODE_Line 0 /* One column per line. Blank line between records */
8595 #define MODE_Column 1 /* One record per line in neat columns */
8596 #define MODE_List 2 /* One record per line with a separator */
8597 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
8598 #define MODE_Html 4 /* Generate an XHTML table */
8599 #define MODE_Insert 5 /* Generate SQL "insert" statements */
8600 #define MODE_Quote 6 /* Quote values as for SQL */
8601 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
8602 #define MODE_Csv 8 /* Quote strings, numbers are plain */
8603 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
8604 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
8605 #define MODE_Pretty 11 /* Pretty-print schemas */
8606 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
8608 static const char *modeDescr[] = {
8625 ** These are the column/row/line separators used by the various
8626 ** import/export modes.
8628 #define SEP_Column "|"
8629 #define SEP_Row "\n"
8630 #define SEP_Tab "\t"
8631 #define SEP_Space " "
8632 #define SEP_Comma ","
8633 #define SEP_CrLf "\r\n"
8634 #define SEP_Unit "\x1F"
8635 #define SEP_Record "\x1E"
8638 ** A callback for the sqlite3_log() interface.
8640 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8641 ShellState *p = (ShellState*)pArg;
8642 if( p->pLog==0 ) return;
8643 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8648 ** SQL function: shell_putsnl(X)
8650 ** Write the text X to the screen (or whatever output is being directed)
8651 ** adding a newline at the end, and then return X.
8653 static void shellPutsFunc(
8654 sqlite3_context *pCtx,
8656 sqlite3_value **apVal
8658 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8660 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8661 sqlite3_result_value(pCtx, apVal[0]);
8665 ** SQL function: edit(VALUE)
8666 ** edit(VALUE,EDITOR)
8670 ** (1) Write VALUE into a temporary file.
8671 ** (2) Run program EDITOR on that temporary file.
8672 ** (3) Read the temporary file back and return its content as the result.
8673 ** (4) Delete the temporary file
8675 ** If the EDITOR argument is omitted, use the value in the VISUAL
8676 ** environment variable. If still there is no EDITOR, through an error.
8678 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8680 #ifndef SQLITE_NOHAVE_SYSTEM
8681 static void editFunc(
8682 sqlite3_context *context,
8684 sqlite3_value **argv
8686 const char *zEditor;
8687 char *zTempFile = 0;
8695 unsigned char *p = 0;
8698 zEditor = (const char*)sqlite3_value_text(argv[1]);
8700 zEditor = getenv("VISUAL");
8703 sqlite3_result_error(context, "no editor for edit()", -1);
8706 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8707 sqlite3_result_error(context, "NULL input to edit()", -1);
8710 db = sqlite3_context_db_handle(context);
8712 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8714 sqlite3_uint64 r = 0;
8715 sqlite3_randomness(sizeof(r), &r);
8716 zTempFile = sqlite3_mprintf("temp%llx", r);
8718 sqlite3_result_error_nomem(context);
8722 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8723 f = fopen(zTempFile, bBin ? "wb" : "w");
8725 sqlite3_result_error(context, "edit() cannot open temp file", -1);
8728 sz = sqlite3_value_bytes(argv[0]);
8730 x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8732 x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8737 sqlite3_result_error(context, "edit() could not write the whole file", -1);
8740 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8742 sqlite3_result_error_nomem(context);
8748 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8751 f = fopen(zTempFile, bBin ? "rb" : "r");
8753 sqlite3_result_error(context,
8754 "edit() cannot reopen temp file after edit", -1);
8757 fseek(f, 0, SEEK_END);
8760 p = sqlite3_malloc64( sz+(bBin==0) );
8762 sqlite3_result_error_nomem(context);
8766 x = fread(p, 1, sz, f);
8768 x = fread(p, 1, sz, f);
8774 sqlite3_result_error(context, "could not read back the whole file", -1);
8778 sqlite3_result_blob64(context, p, sz, sqlite3_free);
8780 sqlite3_result_text64(context, (const char*)p, sz,
8781 sqlite3_free, SQLITE_UTF8);
8788 sqlite3_free(zTempFile);
8791 #endif /* SQLITE_NOHAVE_SYSTEM */
8794 ** Save or restore the current output mode
8796 static void outputModePush(ShellState *p){
8797 p->modePrior = p->mode;
8798 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8799 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8801 static void outputModePop(ShellState *p){
8802 p->mode = p->modePrior;
8803 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8804 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8808 ** Output the given string as a hex-encoded blob (eg. X'1234' )
8810 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
8812 char *zBlob = (char *)pBlob;
8813 raw_printf(out,"X'");
8814 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
8815 raw_printf(out,"'");
8819 ** Find a string that is not found anywhere in z[]. Return a pointer
8822 ** Try to use zA and zB first. If both of those are already found in z[]
8823 ** then make up some string and store it in the buffer zBuf.
8825 static const char *unused_string(
8826 const char *z, /* Result must not appear anywhere in z */
8827 const char *zA, const char *zB, /* Try these first */
8828 char *zBuf /* Space to store a generated string */
8831 if( strstr(z, zA)==0 ) return zA;
8832 if( strstr(z, zB)==0 ) return zB;
8834 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
8835 }while( strstr(z,zBuf)!=0 );
8840 ** Output the given string as a quoted string using SQL quoting conventions.
8842 ** See also: output_quoted_escaped_string()
8844 static void output_quoted_string(FILE *out, const char *z){
8847 setBinaryMode(out, 1);
8848 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8850 utf8_printf(out,"'%s'",z);
8852 raw_printf(out, "'");
8854 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8857 utf8_printf(out, "%.*s", i, z);
8861 raw_printf(out, "'");
8869 raw_printf(out, "'");
8871 setTextMode(out, 1);
8875 ** Output the given string as a quoted string using SQL quoting conventions.
8876 ** Additionallly , escape the "\n" and "\r" characters so that they do not
8877 ** get corrupted by end-of-line translation facilities in some operating
8880 ** This is like output_quoted_string() but with the addition of the \r\n
8881 ** escape mechanism.
8883 static void output_quoted_escaped_string(FILE *out, const char *z){
8886 setBinaryMode(out, 1);
8887 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
8889 utf8_printf(out,"'%s'",z);
8891 const char *zNL = 0;
8892 const char *zCR = 0;
8895 char zBuf1[20], zBuf2[20];
8896 for(i=0; z[i]; i++){
8897 if( z[i]=='\n' ) nNL++;
8898 if( z[i]=='\r' ) nCR++;
8901 raw_printf(out, "replace(");
8902 zNL = unused_string(z, "\\n", "\\012", zBuf1);
8905 raw_printf(out, "replace(");
8906 zCR = unused_string(z, "\\r", "\\015", zBuf2);
8908 raw_printf(out, "'");
8910 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
8913 utf8_printf(out, "%.*s", i, z);
8917 raw_printf(out, "'");
8925 raw_printf(out, "%s", zNL);
8928 raw_printf(out, "%s", zCR);
8930 raw_printf(out, "'");
8932 raw_printf(out, ",'%s',char(13))", zCR);
8935 raw_printf(out, ",'%s',char(10))", zNL);
8938 setTextMode(out, 1);
8942 ** Output the given string as a quoted according to C or TCL quoting rules.
8944 static void output_c_string(FILE *out, const char *z){
8947 while( (c = *(z++))!=0 ){
8954 }else if( c=='\t' ){
8957 }else if( c=='\n' ){
8960 }else if( c=='\r' ){
8963 }else if( !isprint(c&0xff) ){
8964 raw_printf(out, "\\%03o", c&0xff);
8973 ** Output the given string with characters that are special to
8976 static void output_html_string(FILE *out, const char *z){
8988 utf8_printf(out,"%.*s",i,z);
8991 raw_printf(out,"<");
8992 }else if( z[i]=='&' ){
8993 raw_printf(out,"&");
8994 }else if( z[i]=='>' ){
8995 raw_printf(out,">");
8996 }else if( z[i]=='\"' ){
8997 raw_printf(out,""");
8998 }else if( z[i]=='\'' ){
8999 raw_printf(out,"'");
9008 ** If a field contains any character identified by a 1 in the following
9009 ** array, then the string must be quoted for CSV.
9011 static const char needCsvQuote[] = {
9012 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9013 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9014 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
9015 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9016 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9017 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9018 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9019 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
9020 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9021 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9022 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9023 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9024 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9025 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9026 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9027 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9031 ** Output a single term of CSV. Actually, p->colSeparator is used for
9032 ** the separator, which may or may not be a comma. p->nullValue is
9033 ** the null value. Strings are quoted if necessary. The separator
9034 ** is only issued if bSep is true.
9036 static void output_csv(ShellState *p, const char *z, int bSep){
9039 utf8_printf(out,"%s",p->nullValue);
9042 int nSep = strlen30(p->colSeparator);
9043 for(i=0; z[i]; i++){
9044 if( needCsvQuote[((unsigned char*)z)[i]]
9045 || (z[i]==p->colSeparator[0] &&
9046 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
9052 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
9053 utf8_printf(out, "%s", zQuoted);
9054 sqlite3_free(zQuoted);
9056 utf8_printf(out, "%s", z);
9060 utf8_printf(p->out, "%s", p->colSeparator);
9065 ** This routine runs when the user presses Ctrl-C
9067 static void interrupt_handler(int NotUsed){
9068 UNUSED_PARAMETER(NotUsed);
9070 if( seenInterrupt>2 ) exit(1);
9071 if( globalDb ) sqlite3_interrupt(globalDb);
9074 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
9076 ** This routine runs for console events (e.g. Ctrl-C) on Win32
9078 static BOOL WINAPI ConsoleCtrlHandler(
9079 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
9081 if( dwCtrlType==CTRL_C_EVENT ){
9082 interrupt_handler(0);
9089 #ifndef SQLITE_OMIT_AUTHORIZATION
9091 ** When the ".auth ON" is set, the following authorizer callback is
9092 ** invoked. It always returns SQLITE_OK.
9094 static int shellAuth(
9102 ShellState *p = (ShellState*)pClientData;
9103 static const char *azAction[] = { 0,
9104 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
9105 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
9106 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
9107 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
9108 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
9109 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
9110 "PRAGMA", "READ", "SELECT",
9111 "TRANSACTION", "UPDATE", "ATTACH",
9112 "DETACH", "ALTER_TABLE", "REINDEX",
9113 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
9114 "FUNCTION", "SAVEPOINT", "RECURSIVE"
9122 utf8_printf(p->out, "authorizer: %s", azAction[op]);
9124 raw_printf(p->out, " ");
9126 output_c_string(p->out, az[i]);
9128 raw_printf(p->out, "NULL");
9131 raw_printf(p->out, "\n");
9137 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
9139 ** This routine converts some CREATE TABLE statements for shadow tables
9140 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
9142 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
9143 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
9144 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
9146 utf8_printf(out, "%s%s", z, zTail);
9149 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
9152 printSchemaLine(out, z, zTail);
9157 ** Return true if string z[] has nothing but whitespace and comments to the
9158 ** end of the first line.
9160 static int wsToEol(const char *z){
9162 for(i=0; z[i]; i++){
9163 if( z[i]=='\n' ) return 1;
9164 if( IsSpace(z[i]) ) continue;
9165 if( z[i]=='-' && z[i+1]=='-' ) return 1;
9172 ** Add a new entry to the EXPLAIN QUERY PLAN data
9174 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9176 int nText = strlen30(zText);
9177 if( p->autoEQPtest ){
9178 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9180 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9181 if( pNew==0 ) shell_out_of_memory();
9182 pNew->iEqpId = iEqpId;
9183 pNew->iParentId = p2;
9184 memcpy(pNew->zText, zText, nText+1);
9186 if( p->sGraph.pLast ){
9187 p->sGraph.pLast->pNext = pNew;
9189 p->sGraph.pRow = pNew;
9191 p->sGraph.pLast = pNew;
9195 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
9198 static void eqp_reset(ShellState *p){
9199 EQPGraphRow *pRow, *pNext;
9200 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
9201 pNext = pRow->pNext;
9204 memset(&p->sGraph, 0, sizeof(p->sGraph));
9207 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9208 ** pOld, or return the first such line if pOld is NULL
9210 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9211 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9212 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9216 /* Render a single level of the graph that has iEqpId as its parent. Called
9217 ** recursively to render sublevels.
9219 static void eqp_render_level(ShellState *p, int iEqpId){
9220 EQPGraphRow *pRow, *pNext;
9221 int n = strlen30(p->sGraph.zPrefix);
9223 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9224 pNext = eqp_next_row(p, iEqpId, pRow);
9226 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9227 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
9228 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9229 eqp_render_level(p, pRow->iEqpId);
9230 p->sGraph.zPrefix[n] = 0;
9236 ** Display and reset the EXPLAIN QUERY PLAN data
9238 static void eqp_render(ShellState *p){
9239 EQPGraphRow *pRow = p->sGraph.pRow;
9241 if( pRow->zText[0]=='-' ){
9242 if( pRow->pNext==0 ){
9246 utf8_printf(p->out, "%s\n", pRow->zText+3);
9247 p->sGraph.pRow = pRow->pNext;
9250 utf8_printf(p->out, "QUERY PLAN\n");
9252 p->sGraph.zPrefix[0] = 0;
9253 eqp_render_level(p, 0);
9259 ** This is the callback routine that the shell
9260 ** invokes for each row of a query result.
9262 static int shell_callback(
9264 int nArg, /* Number of result columns */
9265 char **azArg, /* Text of each result column */
9266 char **azCol, /* Column names */
9267 int *aiType /* Column types */
9270 ShellState *p = (ShellState*)pArg;
9272 if( azArg==0 ) return 0;
9276 if( azArg==0 ) break;
9277 for(i=0; i<nArg; i++){
9278 int len = strlen30(azCol[i] ? azCol[i] : "");
9279 if( len>w ) w = len;
9281 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
9282 for(i=0; i<nArg; i++){
9283 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
9284 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
9290 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
9291 const int *colWidth;
9294 if( p->cMode==MODE_Column ){
9295 colWidth = p->colWidth;
9296 showHdr = p->showHeader;
9297 rowSep = p->rowSeparator;
9299 colWidth = aExplainWidths;
9304 for(i=0; i<nArg; i++){
9306 if( i<ArraySize(p->colWidth) ){
9312 w = strlenChar(azCol[i] ? azCol[i] : "");
9314 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
9317 if( i<ArraySize(p->actualWidth) ){
9318 p->actualWidth[i] = w;
9321 utf8_width_print(p->out, w, azCol[i]);
9322 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9326 for(i=0; i<nArg; i++){
9328 if( i<ArraySize(p->actualWidth) ){
9329 w = p->actualWidth[i];
9334 utf8_printf(p->out,"%-*.*s%s",w,w,
9335 "----------------------------------------------------------"
9336 "----------------------------------------------------------",
9337 i==nArg-1 ? rowSep : " ");
9341 if( azArg==0 ) break;
9342 for(i=0; i<nArg; i++){
9344 if( i<ArraySize(p->actualWidth) ){
9345 w = p->actualWidth[i];
9349 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
9350 w = strlenChar(azArg[i]);
9352 if( i==1 && p->aiIndent && p->pStmt ){
9353 if( p->iIndent<p->nIndent ){
9354 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
9358 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
9359 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9363 case MODE_Semi: { /* .schema and .fullschema output */
9364 printSchemaLine(p->out, azArg[0], ";\n");
9367 case MODE_Pretty: { /* .schema and .fullschema with --indent */
9375 if( azArg[0]==0 ) break;
9376 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
9377 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
9379 utf8_printf(p->out, "%s;\n", azArg[0]);
9382 z = sqlite3_mprintf("%s", azArg[0]);
9384 for(i=0; IsSpace(z[i]); i++){}
9385 for(; (c = z[i])!=0; i++){
9387 if( z[j-1]=='\r' ) z[j-1] = '\n';
9388 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
9389 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
9394 while( j>0 && IsSpace(z[j-1]) ){ j--; }
9396 if( strlen30(z)>=79 ){
9397 for(i=j=0; (c = z[i])!=0; i++){ /* Copy changes from z[i] back to z[j] */
9400 }else if( c=='"' || c=='\'' || c=='`' ){
9404 }else if( c=='-' && z[i+1]=='-' ){
9410 if( nLine>0 && nParen==0 && j>0 ){
9411 printSchemaLineN(p->out, z, j, "\n");
9416 if( nParen==1 && cEnd==0
9417 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
9420 printSchemaLineN(p->out, z, j, "\n ");
9423 while( IsSpace(z[i+1]) ){ i++; }
9428 printSchemaLine(p->out, z, ";\n");
9433 if( p->cnt++==0 && p->showHeader ){
9434 for(i=0; i<nArg; i++){
9435 utf8_printf(p->out,"%s%s",azCol[i],
9436 i==nArg-1 ? p->rowSeparator : p->colSeparator);
9439 if( azArg==0 ) break;
9440 for(i=0; i<nArg; i++){
9442 if( z==0 ) z = p->nullValue;
9443 utf8_printf(p->out, "%s", z);
9445 utf8_printf(p->out, "%s", p->colSeparator);
9447 utf8_printf(p->out, "%s", p->rowSeparator);
9453 if( p->cnt++==0 && p->showHeader ){
9454 raw_printf(p->out,"<TR>");
9455 for(i=0; i<nArg; i++){
9456 raw_printf(p->out,"<TH>");
9457 output_html_string(p->out, azCol[i]);
9458 raw_printf(p->out,"</TH>\n");
9460 raw_printf(p->out,"</TR>\n");
9462 if( azArg==0 ) break;
9463 raw_printf(p->out,"<TR>");
9464 for(i=0; i<nArg; i++){
9465 raw_printf(p->out,"<TD>");
9466 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9467 raw_printf(p->out,"</TD>\n");
9469 raw_printf(p->out,"</TR>\n");
9473 if( p->cnt++==0 && p->showHeader ){
9474 for(i=0; i<nArg; i++){
9475 output_c_string(p->out,azCol[i] ? azCol[i] : "");
9476 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9478 utf8_printf(p->out, "%s", p->rowSeparator);
9480 if( azArg==0 ) break;
9481 for(i=0; i<nArg; i++){
9482 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9483 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9485 utf8_printf(p->out, "%s", p->rowSeparator);
9489 setBinaryMode(p->out, 1);
9490 if( p->cnt++==0 && p->showHeader ){
9491 for(i=0; i<nArg; i++){
9492 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
9494 utf8_printf(p->out, "%s", p->rowSeparator);
9497 for(i=0; i<nArg; i++){
9498 output_csv(p, azArg[i], i<nArg-1);
9500 utf8_printf(p->out, "%s", p->rowSeparator);
9502 setTextMode(p->out, 1);
9506 if( azArg==0 ) break;
9507 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
9508 if( p->showHeader ){
9509 raw_printf(p->out,"(");
9510 for(i=0; i<nArg; i++){
9511 if( i>0 ) raw_printf(p->out, ",");
9512 if( quoteChar(azCol[i]) ){
9513 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
9514 utf8_printf(p->out, "%s", z);
9517 raw_printf(p->out, "%s", azCol[i]);
9520 raw_printf(p->out,")");
9523 for(i=0; i<nArg; i++){
9524 raw_printf(p->out, i>0 ? "," : " VALUES(");
9525 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9526 utf8_printf(p->out,"NULL");
9527 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9528 if( ShellHasFlag(p, SHFLG_Newlines) ){
9529 output_quoted_string(p->out, azArg[i]);
9531 output_quoted_escaped_string(p->out, azArg[i]);
9533 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9534 utf8_printf(p->out,"%s", azArg[i]);
9535 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9537 double r = sqlite3_column_double(p->pStmt, i);
9538 sqlite3_snprintf(50,z,"%!.20g", r);
9539 raw_printf(p->out, "%s", z);
9540 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9541 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9542 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9543 output_hex_blob(p->out, pBlob, nBlob);
9544 }else if( isNumber(azArg[i], 0) ){
9545 utf8_printf(p->out,"%s", azArg[i]);
9546 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
9547 output_quoted_string(p->out, azArg[i]);
9549 output_quoted_escaped_string(p->out, azArg[i]);
9552 raw_printf(p->out,");\n");
9556 if( azArg==0 ) break;
9557 if( p->cnt==0 && p->showHeader ){
9558 for(i=0; i<nArg; i++){
9559 if( i>0 ) raw_printf(p->out, ",");
9560 output_quoted_string(p->out, azCol[i]);
9562 raw_printf(p->out,"\n");
9565 for(i=0; i<nArg; i++){
9566 if( i>0 ) raw_printf(p->out, ",");
9567 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9568 utf8_printf(p->out,"NULL");
9569 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9570 output_quoted_string(p->out, azArg[i]);
9571 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9572 utf8_printf(p->out,"%s", azArg[i]);
9573 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9575 double r = sqlite3_column_double(p->pStmt, i);
9576 sqlite3_snprintf(50,z,"%!.20g", r);
9577 raw_printf(p->out, "%s", z);
9578 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9579 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9580 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9581 output_hex_blob(p->out, pBlob, nBlob);
9582 }else if( isNumber(azArg[i], 0) ){
9583 utf8_printf(p->out,"%s", azArg[i]);
9585 output_quoted_string(p->out, azArg[i]);
9588 raw_printf(p->out,"\n");
9592 if( p->cnt++==0 && p->showHeader ){
9593 for(i=0; i<nArg; i++){
9594 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9595 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
9597 utf8_printf(p->out, "%s", p->rowSeparator);
9599 if( azArg==0 ) break;
9600 for(i=0; i<nArg; i++){
9601 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9602 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
9604 utf8_printf(p->out, "%s", p->rowSeparator);
9608 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9616 ** This is the callback routine that the SQLite library
9617 ** invokes for each row of a query result.
9619 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
9620 /* since we don't have type info, call the shell_callback with a NULL value */
9621 return shell_callback(pArg, nArg, azArg, azCol, NULL);
9625 ** This is the callback routine from sqlite3_exec() that appends all
9626 ** output onto the end of a ShellText object.
9628 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
9629 ShellText *p = (ShellText*)pArg;
9631 UNUSED_PARAMETER(az);
9632 if( azArg==0 ) return 0;
9633 if( p->n ) appendText(p, "|", 0);
9634 for(i=0; i<nArg; i++){
9635 if( i ) appendText(p, ",", 0);
9636 if( azArg[i] ) appendText(p, azArg[i], 0);
9642 ** Generate an appropriate SELFTEST table in the main database.
9644 static void createSelftestTable(ShellState *p){
9647 "SAVEPOINT selftest_init;\n"
9648 "CREATE TABLE IF NOT EXISTS selftest(\n"
9649 " tno INTEGER PRIMARY KEY,\n" /* Test number */
9650 " op TEXT,\n" /* Operator: memo run */
9651 " cmd TEXT,\n" /* Command text */
9652 " ans TEXT\n" /* Desired answer */
9654 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
9655 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
9656 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
9657 " 'memo','Tests generated by --init');\n"
9658 "INSERT INTO [_shell$self]\n"
9660 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
9661 "FROM sqlite_master ORDER BY 2'',224))',\n"
9662 " hex(sha3_query('SELECT type,name,tbl_name,sql "
9663 "FROM sqlite_master ORDER BY 2',224));\n"
9664 "INSERT INTO [_shell$self]\n"
9666 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
9667 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
9668 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
9670 " SELECT name FROM sqlite_master\n"
9671 " WHERE type='table'\n"
9672 " AND name<>'selftest'\n"
9673 " AND coalesce(rootpage,0)>0\n"
9676 "INSERT INTO [_shell$self]\n"
9677 " VALUES('run','PRAGMA integrity_check','ok');\n"
9678 "INSERT INTO selftest(tno,op,cmd,ans)"
9679 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
9680 "DROP TABLE [_shell$self];"
9683 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
9684 sqlite3_free(zErrMsg);
9686 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9691 ** Set the destination table field of the ShellState structure to
9692 ** the name of the table given. Escape any quote characters in the
9695 static void set_table_name(ShellState *p, const char *zName){
9700 if( p->zDestTable ){
9701 free(p->zDestTable);
9704 if( zName==0 ) return;
9705 cQuote = quoteChar(zName);
9706 n = strlen30(zName);
9707 if( cQuote ) n += n+2;
9708 z = p->zDestTable = malloc( n+1 );
9709 if( z==0 ) shell_out_of_memory();
9711 if( cQuote ) z[n++] = cQuote;
9712 for(i=0; zName[i]; i++){
9714 if( zName[i]==cQuote ) z[n++] = cQuote;
9716 if( cQuote ) z[n++] = cQuote;
9722 ** Execute a query statement that will generate SQL output. Print
9723 ** the result columns, comma-separated, on a line and then add a
9724 ** semicolon terminator to the end of that line.
9726 ** If the number of columns is 1 and that column contains text "--"
9727 ** then write the semicolon on a separate line. That way, if a
9728 ** "--" comment occurs at the end of the statement, the comment
9729 ** won't consume the semicolon terminator.
9731 static int run_table_dump_query(
9732 ShellState *p, /* Query context */
9733 const char *zSelect, /* SELECT statement to extract content */
9734 const char *zFirstRow /* Print before first row, if not NULL */
9736 sqlite3_stmt *pSelect;
9741 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9742 if( rc!=SQLITE_OK || !pSelect ){
9743 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9744 sqlite3_errmsg(p->db));
9745 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9748 rc = sqlite3_step(pSelect);
9749 nResult = sqlite3_column_count(pSelect);
9750 while( rc==SQLITE_ROW ){
9752 utf8_printf(p->out, "%s", zFirstRow);
9755 z = (const char*)sqlite3_column_text(pSelect, 0);
9756 utf8_printf(p->out, "%s", z);
9757 for(i=1; i<nResult; i++){
9758 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
9761 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
9763 raw_printf(p->out, "\n;\n");
9765 raw_printf(p->out, ";\n");
9767 rc = sqlite3_step(pSelect);
9769 rc = sqlite3_finalize(pSelect);
9770 if( rc!=SQLITE_OK ){
9771 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9772 sqlite3_errmsg(p->db));
9773 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9779 ** Allocate space and save off current error string.
9781 static char *save_err_msg(
9782 sqlite3 *db /* Database to query */
9784 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
9785 char *zErrMsg = sqlite3_malloc64(nErrMsg);
9787 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
9794 ** Attempt to display I/O stats on Linux using /proc/PID/io
9796 static void displayLinuxIoStats(FILE *out){
9799 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
9800 in = fopen(z, "rb");
9802 while( fgets(z, sizeof(z), in)!=0 ){
9803 static const struct {
9804 const char *zPattern;
9807 { "rchar: ", "Bytes received by read():" },
9808 { "wchar: ", "Bytes sent to write():" },
9809 { "syscr: ", "Read() system calls:" },
9810 { "syscw: ", "Write() system calls:" },
9811 { "read_bytes: ", "Bytes read from storage:" },
9812 { "write_bytes: ", "Bytes written to storage:" },
9813 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
9816 for(i=0; i<ArraySize(aTrans); i++){
9817 int n = strlen30(aTrans[i].zPattern);
9818 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
9819 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
9829 ** Display a single line of status using 64-bit values.
9831 static void displayStatLine(
9832 ShellState *p, /* The shell context */
9833 char *zLabel, /* Label for this one line */
9834 char *zFormat, /* Format for the result */
9835 int iStatusCtrl, /* Which status to display */
9836 int bReset /* True to reset the stats */
9838 sqlite3_int64 iCur = -1;
9839 sqlite3_int64 iHiwtr = -1;
9842 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
9843 for(i=0, nPercent=0; zFormat[i]; i++){
9844 if( zFormat[i]=='%' ) nPercent++;
9847 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
9849 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
9851 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
9855 ** Display memory stats.
9857 static int display_stats(
9858 sqlite3 *db, /* Database to query */
9859 ShellState *pArg, /* Pointer to ShellState */
9860 int bReset /* True to reset the stats */
9865 if( pArg==0 || pArg->out==0 ) return 0;
9868 if( pArg->pStmt && (pArg->statsOn & 2) ){
9870 sqlite3_stmt *pStmt = pArg->pStmt;
9872 nCol = sqlite3_column_count(pStmt);
9873 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
9874 for(i=0; i<nCol; i++){
9875 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
9876 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
9877 #ifndef SQLITE_OMIT_DECLTYPE
9878 sqlite3_snprintf(30, z+x, "declared type:");
9879 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
9881 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9882 sqlite3_snprintf(30, z+x, "database name:");
9883 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
9884 sqlite3_snprintf(30, z+x, "table name:");
9885 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
9886 sqlite3_snprintf(30, z+x, "origin name:");
9887 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
9892 displayStatLine(pArg, "Memory Used:",
9893 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9894 displayStatLine(pArg, "Number of Outstanding Allocations:",
9895 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9896 if( pArg->shellFlgs & SHFLG_Pagecache ){
9897 displayStatLine(pArg, "Number of Pcache Pages Used:",
9898 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9900 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9901 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9902 displayStatLine(pArg, "Largest Allocation:",
9903 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9904 displayStatLine(pArg, "Largest Pcache Allocation:",
9905 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9906 #ifdef YYTRACKMAXSTACKDEPTH
9907 displayStatLine(pArg, "Deepest Parser Stack:",
9908 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9912 if( pArg->shellFlgs & SHFLG_Lookaside ){
9914 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9915 &iCur, &iHiwtr, bReset);
9916 raw_printf(pArg->out,
9917 "Lookaside Slots Used: %d (max %d)\n",
9919 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
9920 &iCur, &iHiwtr, bReset);
9921 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
9923 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
9924 &iCur, &iHiwtr, bReset);
9925 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
9927 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
9928 &iCur, &iHiwtr, bReset);
9929 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
9933 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
9934 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
9937 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
9938 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
9940 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
9941 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
9943 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9944 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
9946 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
9947 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
9949 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9950 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
9953 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9954 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
9959 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9961 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
9962 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9963 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
9964 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9965 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
9966 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9967 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
9968 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
9969 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
9970 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
9971 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
9972 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
9973 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
9977 displayLinuxIoStats(pArg->out);
9980 /* Do not remove this machine readable comment: extra-stats-output-here */
9986 ** Display scan stats.
9988 static void display_scanstats(
9989 sqlite3 *db, /* Database to query */
9990 ShellState *pArg /* Pointer to ShellState */
9992 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9993 UNUSED_PARAMETER(db);
9994 UNUSED_PARAMETER(pArg);
9997 raw_printf(pArg->out, "-------- scanstats --------\n");
9999 for(k=0; k<=mx; k++){
10000 double rEstLoop = 1.0;
10001 for(i=n=0; 1; i++){
10002 sqlite3_stmt *p = pArg->pStmt;
10003 sqlite3_int64 nLoop, nVisit;
10006 const char *zExplain;
10007 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
10010 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
10011 if( iSid>mx ) mx = iSid;
10012 if( iSid!=k ) continue;
10014 rEstLoop = (double)nLoop;
10015 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
10018 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
10019 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
10020 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
10021 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
10023 raw_printf(pArg->out,
10024 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
10025 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
10029 raw_printf(pArg->out, "---------------------------\n");
10034 ** Parameter azArray points to a zero-terminated array of strings. zStr
10035 ** points to a single nul-terminated string. Return non-zero if zStr
10036 ** is equal, according to strcmp(), to any of the strings in the array.
10037 ** Otherwise, return zero.
10039 static int str_in_array(const char *zStr, const char **azArray){
10041 for(i=0; azArray[i]; i++){
10042 if( 0==strcmp(zStr, azArray[i]) ) return 1;
10048 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
10049 ** and populate the ShellState.aiIndent[] array with the number of
10050 ** spaces each opcode should be indented before it is output.
10052 ** The indenting rules are:
10054 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
10055 ** all opcodes that occur between the p2 jump destination and the opcode
10056 ** itself by 2 spaces.
10058 ** * For each "Goto", if the jump destination is earlier in the program
10059 ** and ends on one of:
10060 ** Yield SeekGt SeekLt RowSetRead Rewind
10061 ** or if the P1 parameter is one instead of zero,
10062 ** then indent all opcodes between the earlier instruction
10063 ** and "Goto" by 2 spaces.
10065 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
10066 const char *zSql; /* The text of the SQL statement */
10067 const char *z; /* Used to check if this is an EXPLAIN */
10068 int *abYield = 0; /* True if op is an OP_Yield */
10069 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
10070 int iOp; /* Index of operation in p->aiIndent[] */
10072 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
10073 "NextIfOpen", "PrevIfOpen", 0 };
10074 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
10076 const char *azGoto[] = { "Goto", 0 };
10078 /* Try to figure out if this is really an EXPLAIN statement. If this
10079 ** cannot be verified, return early. */
10080 if( sqlite3_column_count(pSql)!=8 ){
10081 p->cMode = p->mode;
10084 zSql = sqlite3_sql(pSql);
10085 if( zSql==0 ) return;
10086 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
10087 if( sqlite3_strnicmp(z, "explain", 7) ){
10088 p->cMode = p->mode;
10092 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
10094 int iAddr = sqlite3_column_int(pSql, 0);
10095 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
10097 /* Set p2 to the P2 field of the current opcode. Then, assuming that
10098 ** p2 is an instruction address, set variable p2op to the index of that
10099 ** instruction in the aiIndent[] array. p2 and p2op may be different if
10100 ** the current instruction is part of a sub-program generated by an
10101 ** SQL trigger or foreign key. */
10102 int p2 = sqlite3_column_int(pSql, 3);
10103 int p2op = (p2 + (iOp-iAddr));
10105 /* Grow the p->aiIndent array as required */
10108 /* Do further verfication that this is explain output. Abort if
10110 static const char *explainCols[] = {
10111 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
10113 for(jj=0; jj<ArraySize(explainCols); jj++){
10114 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
10115 p->cMode = p->mode;
10116 sqlite3_reset(pSql);
10122 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
10123 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
10125 abYield[iOp] = str_in_array(zOp, azYield);
10126 p->aiIndent[iOp] = 0;
10127 p->nIndent = iOp+1;
10129 if( str_in_array(zOp, azNext) ){
10130 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10132 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
10133 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
10135 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10140 sqlite3_free(abYield);
10141 sqlite3_reset(pSql);
10145 ** Free the array allocated by explain_data_prepare().
10147 static void explain_data_delete(ShellState *p){
10148 sqlite3_free(p->aiIndent);
10155 ** Disable and restore .wheretrace and .selecttrace settings.
10157 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10158 extern int sqlite3SelectTrace;
10159 static int savedSelectTrace;
10161 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10162 extern int sqlite3WhereTrace;
10163 static int savedWhereTrace;
10165 static void disable_debug_trace_modes(void){
10166 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10167 savedSelectTrace = sqlite3SelectTrace;
10168 sqlite3SelectTrace = 0;
10170 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10171 savedWhereTrace = sqlite3WhereTrace;
10172 sqlite3WhereTrace = 0;
10175 static void restore_debug_trace_modes(void){
10176 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10177 sqlite3SelectTrace = savedSelectTrace;
10179 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10180 sqlite3WhereTrace = savedWhereTrace;
10185 ** Run a prepared statement
10187 static void exec_prepared_stmt(
10188 ShellState *pArg, /* Pointer to ShellState */
10189 sqlite3_stmt *pStmt /* Statment to run */
10193 /* perform the first step. this will tell us if we
10194 ** have a result set or not and how wide it is.
10196 rc = sqlite3_step(pStmt);
10197 /* if we have a result set... */
10198 if( SQLITE_ROW == rc ){
10199 /* allocate space for col name ptr, value ptr, and type */
10200 int nCol = sqlite3_column_count(pStmt);
10201 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10205 char **azCols = (char **)pData; /* Names of result columns */
10206 char **azVals = &azCols[nCol]; /* Results */
10207 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10209 assert(sizeof(int) <= sizeof(char *));
10210 /* save off ptrs to column names */
10211 for(i=0; i<nCol; i++){
10212 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10215 /* extract the data and data types */
10216 for(i=0; i<nCol; i++){
10217 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10218 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10221 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10223 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10225 break; /* from for */
10229 /* if data and types extracted successfully... */
10230 if( SQLITE_ROW == rc ){
10231 /* call the supplied callback with the result row data */
10232 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10235 rc = sqlite3_step(pStmt);
10238 } while( SQLITE_ROW == rc );
10239 sqlite3_free(pData);
10244 #ifndef SQLITE_OMIT_VIRTUALTABLE
10246 ** This function is called to process SQL if the previous shell command
10247 ** was ".expert". It passes the SQL in the second argument directly to
10248 ** the sqlite3expert object.
10250 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10251 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10252 ** an English language error message. It is the responsibility of the
10253 ** caller to eventually free this buffer using sqlite3_free().
10255 static int expertHandleSQL(
10256 ShellState *pState,
10260 assert( pState->expert.pExpert );
10261 assert( pzErr==0 || *pzErr==0 );
10262 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10266 ** This function is called either to silently clean up the object
10267 ** created by the ".expert" command (if bCancel==1), or to generate a
10268 ** report from it and then clean it up (if bCancel==0).
10270 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10271 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10272 ** an English language error message. It is the responsibility of the
10273 ** caller to eventually free this buffer using sqlite3_free().
10275 static int expertFinish(
10276 ShellState *pState,
10280 int rc = SQLITE_OK;
10281 sqlite3expert *p = pState->expert.pExpert;
10283 assert( bCancel || pzErr==0 || *pzErr==0 );
10285 FILE *out = pState->out;
10286 int bVerbose = pState->expert.bVerbose;
10288 rc = sqlite3_expert_analyze(p, pzErr);
10289 if( rc==SQLITE_OK ){
10290 int nQuery = sqlite3_expert_count(p);
10294 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10295 raw_printf(out, "-- Candidates -----------------------------\n");
10296 raw_printf(out, "%s\n", zCand);
10298 for(i=0; i<nQuery; i++){
10299 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10300 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10301 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10302 if( zIdx==0 ) zIdx = "(no new indexes)\n";
10304 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10305 raw_printf(out, "%s\n\n", zSql);
10307 raw_printf(out, "%s\n", zIdx);
10308 raw_printf(out, "%s\n", zEQP);
10312 sqlite3_expert_destroy(p);
10313 pState->expert.pExpert = 0;
10318 ** Implementation of ".expert" dot command.
10320 static int expertDotCommand(
10321 ShellState *pState, /* Current shell tool state */
10322 char **azArg, /* Array of arguments passed to dot command */
10323 int nArg /* Number of entries in azArg[] */
10325 int rc = SQLITE_OK;
10330 assert( pState->expert.pExpert==0 );
10331 memset(&pState->expert, 0, sizeof(ExpertInfo));
10333 for(i=1; rc==SQLITE_OK && i<nArg; i++){
10334 char *z = azArg[i];
10336 if( z[0]=='-' && z[1]=='-' ) z++;
10338 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10339 pState->expert.bVerbose = 1;
10341 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10343 raw_printf(stderr, "option requires an argument: %s\n", z);
10346 iSample = (int)integerValue(azArg[++i]);
10347 if( iSample<0 || iSample>100 ){
10348 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10354 raw_printf(stderr, "unknown option: %s\n", z);
10359 if( rc==SQLITE_OK ){
10360 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10361 if( pState->expert.pExpert==0 ){
10362 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10365 sqlite3_expert_config(
10366 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10373 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10376 ** Execute a statement or set of statements. Print
10377 ** any result rows/columns depending on the current mode
10378 ** set via the supplied callback.
10380 ** This is very similar to SQLite's built-in sqlite3_exec()
10381 ** function except it takes a slightly different callback
10382 ** and callback data argument.
10384 static int shell_exec(
10385 ShellState *pArg, /* Pointer to ShellState */
10386 const char *zSql, /* SQL to be evaluated */
10387 char **pzErrMsg /* Error msg written here */
10389 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10390 int rc = SQLITE_OK; /* Return Code */
10392 const char *zLeftover; /* Tail of unprocessed SQL */
10393 sqlite3 *db = pArg->db;
10399 #ifndef SQLITE_OMIT_VIRTUALTABLE
10400 if( pArg->expert.pExpert ){
10401 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10402 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10406 while( zSql[0] && (SQLITE_OK == rc) ){
10407 static const char *zStmtSql;
10408 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10409 if( SQLITE_OK != rc ){
10411 *pzErrMsg = save_err_msg(db);
10415 /* this happens for a comment or white-space */
10417 while( IsSpace(zSql[0]) ) zSql++;
10420 zStmtSql = sqlite3_sql(pStmt);
10421 if( zStmtSql==0 ) zStmtSql = "";
10422 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10424 /* save off the prepared statment handle and reset row count */
10426 pArg->pStmt = pStmt;
10430 /* echo the sql statement if echo on */
10431 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10432 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10435 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10436 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10437 sqlite3_stmt *pExplain;
10439 int triggerEQP = 0;
10440 disable_debug_trace_modes();
10441 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10442 if( pArg->autoEQP>=AUTOEQP_trigger ){
10443 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10445 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10446 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10447 if( rc==SQLITE_OK ){
10448 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10449 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10450 int iEqpId = sqlite3_column_int(pExplain, 0);
10451 int iParentId = sqlite3_column_int(pExplain, 1);
10452 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10453 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10457 sqlite3_finalize(pExplain);
10458 sqlite3_free(zEQP);
10459 if( pArg->autoEQP>=AUTOEQP_full ){
10460 /* Also do an EXPLAIN for ".eqp full" mode */
10461 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10462 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10463 if( rc==SQLITE_OK ){
10464 pArg->cMode = MODE_Explain;
10465 explain_data_prepare(pArg, pExplain);
10466 exec_prepared_stmt(pArg, pExplain);
10467 explain_data_delete(pArg);
10469 sqlite3_finalize(pExplain);
10470 sqlite3_free(zEQP);
10472 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10473 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10474 /* Reprepare pStmt before reactiving trace modes */
10475 sqlite3_finalize(pStmt);
10476 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10478 restore_debug_trace_modes();
10482 pArg->cMode = pArg->mode;
10483 if( pArg->autoExplain ){
10484 if( sqlite3_column_count(pStmt)==8
10485 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10487 pArg->cMode = MODE_Explain;
10489 if( sqlite3_column_count(pStmt)==4
10490 && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10491 pArg->cMode = MODE_EQP;
10495 /* If the shell is currently in ".explain" mode, gather the extra
10496 ** data required to add indents to the output.*/
10497 if( pArg->cMode==MODE_Explain ){
10498 explain_data_prepare(pArg, pStmt);
10502 exec_prepared_stmt(pArg, pStmt);
10503 explain_data_delete(pArg);
10506 /* print usage stats if stats on */
10507 if( pArg && pArg->statsOn ){
10508 display_stats(db, pArg, 0);
10511 /* print loop-counters if required */
10512 if( pArg && pArg->scanstatsOn ){
10513 display_scanstats(db, pArg);
10516 /* Finalize the statement just executed. If this fails, save a
10517 ** copy of the error message. Otherwise, set zSql to point to the
10518 ** next statement to execute. */
10519 rc2 = sqlite3_finalize(pStmt);
10520 if( rc!=SQLITE_NOMEM ) rc = rc2;
10521 if( rc==SQLITE_OK ){
10523 while( IsSpace(zSql[0]) ) zSql++;
10524 }else if( pzErrMsg ){
10525 *pzErrMsg = save_err_msg(db);
10528 /* clear saved stmt handle */
10530 pArg->pStmt = NULL;
10539 ** Release memory previously allocated by tableColumnList().
10541 static void freeColumnList(char **azCol){
10543 for(i=1; azCol[i]; i++){
10544 sqlite3_free(azCol[i]);
10546 /* azCol[0] is a static string */
10547 sqlite3_free(azCol);
10551 ** Return a list of pointers to strings which are the names of all
10552 ** columns in table zTab. The memory to hold the names is dynamically
10553 ** allocated and must be released by the caller using a subsequent call
10554 ** to freeColumnList().
10556 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
10557 ** value that needs to be preserved, then azCol[0] is filled in with the
10558 ** name of the rowid column.
10560 ** The first regular column in the table is azCol[1]. The list is terminated
10561 ** by an entry with azCol[i]==0.
10563 static char **tableColumnList(ShellState *p, const char *zTab){
10565 sqlite3_stmt *pStmt;
10569 int nPK = 0; /* Number of PRIMARY KEY columns seen */
10570 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
10571 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10574 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10575 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10576 sqlite3_free(zSql);
10578 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10579 if( nCol>=nAlloc-2 ){
10580 nAlloc = nAlloc*2 + nCol + 10;
10581 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10582 if( azCol==0 ) shell_out_of_memory();
10584 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10585 if( sqlite3_column_int(pStmt, 5) ){
10588 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10597 sqlite3_finalize(pStmt);
10598 if( azCol==0 ) return 0;
10602 /* The decision of whether or not a rowid really needs to be preserved
10603 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
10604 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
10605 ** rowids on tables where the rowid is inaccessible because there are other
10606 ** columns in the table named "rowid", "_rowid_", and "oid".
10608 if( preserveRowid && isIPK ){
10609 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10610 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
10611 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10612 ** ROWID aliases. To distinguish these cases, check to see if
10613 ** there is a "pk" entry in "PRAGMA index_list". There will be
10614 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10616 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10617 " WHERE origin='pk'", zTab);
10618 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10619 sqlite3_free(zSql);
10621 freeColumnList(azCol);
10624 rc = sqlite3_step(pStmt);
10625 sqlite3_finalize(pStmt);
10626 preserveRowid = rc==SQLITE_ROW;
10628 if( preserveRowid ){
10629 /* Only preserve the rowid if we can find a name to use for the
10631 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10633 for(j=0; j<3; j++){
10634 for(i=1; i<=nCol; i++){
10635 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10638 /* At this point, we know that azRowid[j] is not the name of any
10639 ** ordinary column in the table. Verify that azRowid[j] is a valid
10640 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
10641 ** tables will fail this last check */
10642 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10643 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10652 ** Toggle the reverse_unordered_selects setting.
10654 static void toggleSelectOrder(sqlite3 *db){
10655 sqlite3_stmt *pStmt = 0;
10658 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10659 if( sqlite3_step(pStmt)==SQLITE_ROW ){
10660 iSetting = sqlite3_column_int(pStmt, 0);
10662 sqlite3_finalize(pStmt);
10663 sqlite3_snprintf(sizeof(zStmt), zStmt,
10664 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10665 sqlite3_exec(db, zStmt, 0, 0, 0);
10669 ** This is a different callback routine used for dumping the database.
10670 ** Each row received by this callback consists of a table name,
10671 ** the table type ("index" or "table") and SQL to create the table.
10672 ** This routine should print text sufficient to recreate the table.
10674 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10676 const char *zTable;
10679 ShellState *p = (ShellState *)pArg;
10681 UNUSED_PARAMETER(azNotUsed);
10682 if( nArg!=3 || azArg==0 ) return 0;
10687 if( strcmp(zTable, "sqlite_sequence")==0 ){
10688 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10689 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10690 raw_printf(p->out, "ANALYZE sqlite_master;\n");
10691 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
10693 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
10695 if( !p->writableSchema ){
10696 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
10697 p->writableSchema = 1;
10699 zIns = sqlite3_mprintf(
10700 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
10701 "VALUES('table','%q','%q',0,'%q');",
10702 zTable, zTable, zSql);
10703 utf8_printf(p->out, "%s\n", zIns);
10704 sqlite3_free(zIns);
10707 printSchemaLine(p->out, zSql, ";\n");
10710 if( strcmp(zType, "table")==0 ){
10715 char *savedDestTable;
10718 azCol = tableColumnList(p, zTable);
10724 /* Always quote the table name, even if it appears to be pure ascii,
10725 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
10727 appendText(&sTable, zTable, quoteChar(zTable));
10728 /* If preserving the rowid, add a column list after the table name.
10729 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10730 ** instead of the usual "INSERT INTO tab VALUES(...)".
10733 appendText(&sTable, "(", 0);
10734 appendText(&sTable, azCol[0], 0);
10735 for(i=1; azCol[i]; i++){
10736 appendText(&sTable, ",", 0);
10737 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10739 appendText(&sTable, ")", 0);
10742 /* Build an appropriate SELECT statement */
10743 initText(&sSelect);
10744 appendText(&sSelect, "SELECT ", 0);
10746 appendText(&sSelect, azCol[0], 0);
10747 appendText(&sSelect, ",", 0);
10749 for(i=1; azCol[i]; i++){
10750 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
10752 appendText(&sSelect, ",", 0);
10755 freeColumnList(azCol);
10756 appendText(&sSelect, " FROM ", 0);
10757 appendText(&sSelect, zTable, quoteChar(zTable));
10759 savedDestTable = p->zDestTable;
10760 savedMode = p->mode;
10761 p->zDestTable = sTable.z;
10762 p->mode = p->cMode = MODE_Insert;
10763 rc = shell_exec(p, sSelect.z, 0);
10764 if( (rc&0xff)==SQLITE_CORRUPT ){
10765 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10766 toggleSelectOrder(p->db);
10767 shell_exec(p, sSelect.z, 0);
10768 toggleSelectOrder(p->db);
10770 p->zDestTable = savedDestTable;
10771 p->mode = savedMode;
10773 freeText(&sSelect);
10774 if( rc ) p->nErr++;
10780 ** Run zQuery. Use dump_callback() as the callback routine so that
10781 ** the contents of the query are output as SQL statements.
10783 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
10784 ** "ORDER BY rowid DESC" to the end.
10786 static int run_schema_dump_query(
10792 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
10793 if( rc==SQLITE_CORRUPT ){
10795 int len = strlen30(zQuery);
10796 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10798 utf8_printf(p->out, "/****** %s ******/\n", zErr);
10799 sqlite3_free(zErr);
10802 zQ2 = malloc( len+100 );
10803 if( zQ2==0 ) return rc;
10804 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
10805 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
10807 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
10809 rc = SQLITE_CORRUPT;
10811 sqlite3_free(zErr);
10818 ** Text of a help message
10820 static char zHelp[] =
10821 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10822 ".archive ... Manage SQL archives: \".archive --help\" for details\n"
10824 #ifndef SQLITE_OMIT_AUTHORIZATION
10825 ".auth ON|OFF Show authorizer callbacks\n"
10827 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
10828 " Add \"--append\" to open using appendvfs.\n"
10829 ".bail on|off Stop after hitting an error. Default OFF\n"
10830 ".binary on|off Turn binary output on or off. Default OFF\n"
10831 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
10832 ".changes on|off Show number of rows changed by SQL\n"
10833 ".check GLOB Fail if output since .testcase does not match\n"
10834 ".clone NEWDB Clone data into NEWDB from the existing database\n"
10835 ".databases List names and files of attached databases\n"
10836 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options\n"
10837 ".dbinfo ?DB? Show status information about the database\n"
10838 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
10839 " If TABLE specified, only dump tables matching\n"
10840 " LIKE pattern TABLE.\n"
10841 ".echo on|off Turn command echo on or off\n"
10842 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
10843 ".excel Display the output of next command in a spreadsheet\n"
10844 ".exit Exit this program\n"
10845 ".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
10846 /* Because explain mode comes on automatically now, the ".explain" mode
10847 ** is removed from the help screen. It is still supported for legacy, however */
10848 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
10849 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
10850 ".headers on|off Turn display of headers on or off\n"
10851 ".help Show this message\n"
10852 ".import FILE TABLE Import data from FILE into TABLE\n"
10853 #ifndef SQLITE_OMIT_TEST_CONTROL
10854 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
10856 ".indexes ?TABLE? Show names of all indexes\n"
10857 " If TABLE specified, only show indexes for tables\n"
10858 " matching LIKE pattern TABLE.\n"
10859 #ifdef SQLITE_ENABLE_IOTRACE
10860 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
10862 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
10863 ".lint OPTIONS Report potential schema issues. Options:\n"
10864 " fkey-indexes Find missing foreign key indexes\n"
10865 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10866 ".load FILE ?ENTRY? Load an extension library\n"
10868 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
10869 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
10870 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
10871 " csv Comma-separated values\n"
10872 " column Left-aligned columns. (See .width)\n"
10873 " html HTML <table> code\n"
10874 " insert SQL insert statements for TABLE\n"
10875 " line One value per line\n"
10876 " list Values delimited by \"|\"\n"
10877 " quote Escape answers as for SQL\n"
10878 " tabs Tab-separated values\n"
10879 " tcl TCL list elements\n"
10880 ".nullvalue STRING Use STRING in place of NULL values\n"
10881 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
10882 " or invoke system text editor (-e) or spreadsheet (-x)\n"
10883 " on the output.\n"
10884 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10885 " The --new option starts with an empty file\n"
10886 " Other options: --readonly --append --zip\n"
10887 ".output ?FILE? Send output to FILE or stdout\n"
10888 ".print STRING... Print literal STRING\n"
10889 ".prompt MAIN CONTINUE Replace the standard prompts\n"
10890 ".quit Exit this program\n"
10891 ".read FILENAME Execute SQL in FILENAME\n"
10892 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
10893 ".save FILE Write in-memory database into FILE\n"
10894 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
10895 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
10896 " Add --indent for pretty-printing\n"
10897 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
10898 ".separator COL ?ROW? Change the column separator and optionally the row\n"
10899 " separator for both the output mode and .import\n"
10900 #if defined(SQLITE_ENABLE_SESSION)
10901 ".session CMD ... Create or control sessions\n"
10903 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
10904 #ifndef SQLITE_NOHAVE_SYSTEM
10905 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
10907 ".show Show the current values for various settings\n"
10908 ".stats ?on|off? Show stats or turn stats on or off\n"
10909 #ifndef SQLITE_NOHAVE_SYSTEM
10910 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
10912 ".tables ?TABLE? List names of tables\n"
10913 " If TABLE specified, only list tables matching\n"
10914 " LIKE pattern TABLE.\n"
10915 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
10916 ".timeout MS Try opening locked tables for MS milliseconds\n"
10917 ".timer on|off Turn SQL timer on or off\n"
10918 ".trace FILE|off Output each SQL statement as it is run\n"
10919 ".vfsinfo ?AUX? Information about the top-level VFS\n"
10920 ".vfslist List all available VFSes\n"
10921 ".vfsname ?AUX? Print the name of the VFS stack\n"
10922 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
10923 " Negative values right-justify\n"
10926 #if defined(SQLITE_ENABLE_SESSION)
10928 ** Print help information for the ".sessions" command
10930 void session_help(ShellState *p){
10932 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
10933 "If ?NAME? is omitted, the first defined session is used.\n"
10935 " attach TABLE Attach TABLE\n"
10936 " changeset FILE Write a changeset into FILE\n"
10937 " close Close one session\n"
10938 " enable ?BOOLEAN? Set or query the enable bit\n"
10939 " filter GLOB... Reject tables matching GLOBs\n"
10940 " indirect ?BOOLEAN? Mark or query the indirect status\n"
10941 " isempty Query whether the session is empty\n"
10942 " list List currently open session names\n"
10943 " open DB NAME Open a new session on DB\n"
10944 " patchset FILE Write a patchset into FILE\n"
10950 /* Forward reference */
10951 static int process_input(ShellState *p, FILE *in);
10954 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
10955 ** and return a pointer to the buffer. The caller is responsible for freeing
10958 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
10961 ** For convenience, a nul-terminator byte is always appended to the data read
10962 ** from the file before the buffer is returned. This byte is not included in
10963 ** the final value of (*pnByte), if applicable.
10965 ** NULL is returned if any error is encountered. The final value of *pnByte
10966 ** is undefined in this case.
10968 static char *readFile(const char *zName, int *pnByte){
10969 FILE *in = fopen(zName, "rb");
10973 if( in==0 ) return 0;
10974 fseek(in, 0, SEEK_END);
10977 pBuf = sqlite3_malloc64( nIn+1 );
10978 if( pBuf==0 ) return 0;
10979 nRead = fread(pBuf, nIn, 1, in);
10982 sqlite3_free(pBuf);
10986 if( pnByte ) *pnByte = nIn;
10990 #if defined(SQLITE_ENABLE_SESSION)
10992 ** Close a single OpenSession object and release all of its associated
10995 static void session_close(OpenSession *pSession){
10997 sqlite3session_delete(pSession->p);
10998 sqlite3_free(pSession->zName);
10999 for(i=0; i<pSession->nFilter; i++){
11000 sqlite3_free(pSession->azFilter[i]);
11002 sqlite3_free(pSession->azFilter);
11003 memset(pSession, 0, sizeof(OpenSession));
11008 ** Close all OpenSession objects and release all associated resources.
11010 #if defined(SQLITE_ENABLE_SESSION)
11011 static void session_close_all(ShellState *p){
11013 for(i=0; i<p->nSession; i++){
11014 session_close(&p->aSession[i]);
11019 # define session_close_all(X)
11023 ** Implementation of the xFilter function for an open session. Omit
11024 ** any tables named by ".session filter" but let all other table through.
11026 #if defined(SQLITE_ENABLE_SESSION)
11027 static int session_filter(void *pCtx, const char *zTab){
11028 OpenSession *pSession = (OpenSession*)pCtx;
11030 for(i=0; i<pSession->nFilter; i++){
11031 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11038 ** Try to deduce the type of file for zName based on its content. Return
11039 ** one of the SHELL_OPEN_* constants.
11041 ** If the file does not exist or is empty but its name looks like a ZIP
11042 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11043 ** Otherwise, assume an ordinary database regardless of the filename if
11044 ** the type cannot be determined from content.
11046 int deduceDatabaseType(const char *zName, int dfltZip){
11047 FILE *f = fopen(zName, "rb");
11049 int rc = SHELL_OPEN_UNSPEC;
11052 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11053 return SHELL_OPEN_ZIPFILE;
11055 return SHELL_OPEN_NORMAL;
11058 fseek(f, -25, SEEK_END);
11059 n = fread(zBuf, 25, 1, f);
11060 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11061 rc = SHELL_OPEN_APPENDVFS;
11063 fseek(f, -22, SEEK_END);
11064 n = fread(zBuf, 22, 1, f);
11065 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11066 && zBuf[3]==0x06 ){
11067 rc = SHELL_OPEN_ZIPFILE;
11068 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11069 rc = SHELL_OPEN_ZIPFILE;
11076 /* Flags for open_db().
11078 ** The default behavior of open_db() is to exit(1) if the database fails to
11079 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11080 ** but still returns without calling exit.
11082 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11083 ** ZIP archive if the file does not exist or is empty and its name matches
11084 ** the *.zip pattern.
11086 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11087 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11090 ** Make sure the database is open. If it is not, then open it. If
11091 ** the database fails to open, print an error message and exit.
11093 static void open_db(ShellState *p, int openFlags){
11095 if( p->openMode==SHELL_OPEN_UNSPEC ){
11096 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11097 p->openMode = SHELL_OPEN_NORMAL;
11099 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11100 (openFlags & OPEN_DB_ZIPFILE)!=0);
11103 switch( p->openMode ){
11104 case SHELL_OPEN_APPENDVFS: {
11105 sqlite3_open_v2(p->zDbFilename, &p->db,
11106 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11109 case SHELL_OPEN_ZIPFILE: {
11110 sqlite3_open(":memory:", &p->db);
11113 case SHELL_OPEN_READONLY: {
11114 sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11117 case SHELL_OPEN_UNSPEC:
11118 case SHELL_OPEN_NORMAL: {
11119 sqlite3_open(p->zDbFilename, &p->db);
11124 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11125 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11126 p->zDbFilename, sqlite3_errmsg(p->db));
11127 if( openFlags & OPEN_DB_KEEPALIVE ) return;
11130 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11131 sqlite3_enable_load_extension(p->db, 1);
11133 sqlite3_fileio_init(p->db, 0, 0);
11134 sqlite3_shathree_init(p->db, 0, 0);
11135 sqlite3_completion_init(p->db, 0, 0);
11136 #ifdef SQLITE_HAVE_ZLIB
11137 sqlite3_zipfile_init(p->db, 0, 0);
11138 sqlite3_sqlar_init(p->db, 0, 0);
11140 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11141 shellAddSchemaName, 0, 0);
11142 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11143 shellModuleSchema, 0, 0);
11144 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11145 shellPutsFunc, 0, 0);
11146 #ifndef SQLITE_NOHAVE_SYSTEM
11147 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11149 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11152 if( p->openMode==SHELL_OPEN_ZIPFILE ){
11153 char *zSql = sqlite3_mprintf(
11154 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11155 sqlite3_exec(p->db, zSql, 0, 0, 0);
11156 sqlite3_free(zSql);
11162 ** Attempt to close the databaes connection. Report errors.
11164 void close_db(sqlite3 *db){
11165 int rc = sqlite3_close(db);
11167 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11168 rc, sqlite3_errmsg(db));
11172 #if HAVE_READLINE || HAVE_EDITLINE
11174 ** Readline completion callbacks
11176 static char *readline_completion_generator(const char *text, int state){
11177 static sqlite3_stmt *pStmt = 0;
11181 sqlite3_finalize(pStmt);
11182 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11183 " FROM completion(%Q) ORDER BY 1", text);
11184 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11185 sqlite3_free(zSql);
11187 if( sqlite3_step(pStmt)==SQLITE_ROW ){
11188 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11190 sqlite3_finalize(pStmt);
11196 static char **readline_completion(const char *zText, int iStart, int iEnd){
11197 rl_attempted_completion_over = 1;
11198 return rl_completion_matches(zText, readline_completion_generator);
11201 #elif HAVE_LINENOISE
11203 ** Linenoise completion callback
11205 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11206 int nLine = strlen30(zLine);
11208 sqlite3_stmt *pStmt = 0;
11212 if( nLine>sizeof(zBuf)-30 ) return;
11213 if( zLine[0]=='.' || zLine[0]=='#') return;
11214 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11215 if( i==nLine-1 ) return;
11217 memcpy(zBuf, zLine, iStart);
11218 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11219 " FROM completion(%Q,%Q) ORDER BY 1",
11220 &zLine[iStart], zLine);
11221 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11222 sqlite3_free(zSql);
11223 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11224 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11225 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11226 int nCompletion = sqlite3_column_bytes(pStmt, 0);
11227 if( iStart+nCompletion < sizeof(zBuf)-1 ){
11228 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11229 linenoiseAddCompletion(lc, zBuf);
11232 sqlite3_finalize(pStmt);
11237 ** Do C-language style dequoting.
11243 ** \v -> vertical tab
11245 ** \r -> carriage return
11250 ** \NNN -> ascii character NNN in octal
11252 static void resolve_backslashes(char *z){
11255 while( *z && *z!='\\' ) z++;
11256 for(i=j=0; (c = z[i])!=0; i++, j++){
11257 if( c=='\\' && z[i+1]!=0 ){
11261 }else if( c=='b' ){
11263 }else if( c=='t' ){
11265 }else if( c=='n' ){
11267 }else if( c=='v' ){
11269 }else if( c=='f' ){
11271 }else if( c=='r' ){
11273 }else if( c=='"' ){
11275 }else if( c=='\'' ){
11277 }else if( c=='\\' ){
11279 }else if( c>='0' && c<='7' ){
11281 if( z[i+1]>='0' && z[i+1]<='7' ){
11283 c = (c<<3) + z[i] - '0';
11284 if( z[i+1]>='0' && z[i+1]<='7' ){
11286 c = (c<<3) + z[i] - '0';
11293 if( j<i ) z[j] = 0;
11297 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
11298 ** for TRUE and FALSE. Return the integer value if appropriate.
11300 static int booleanValue(const char *zArg){
11302 if( zArg[0]=='0' && zArg[1]=='x' ){
11303 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11305 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11307 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11308 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11311 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11314 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11320 ** Set or clear a shell flag according to a boolean value.
11322 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11323 if( booleanValue(zArg) ){
11324 ShellSetFlag(p, mFlag);
11326 ShellClearFlag(p, mFlag);
11331 ** Close an output file, assuming it is not stderr or stdout
11333 static void output_file_close(FILE *f){
11334 if( f && f!=stdout && f!=stderr ) fclose(f);
11338 ** Try to open an output file. The names "stdout" and "stderr" are
11339 ** recognized and do the right thing. NULL is returned if the output
11340 ** filename is "off".
11342 static FILE *output_file_open(const char *zFile, int bTextMode){
11344 if( strcmp(zFile,"stdout")==0 ){
11346 }else if( strcmp(zFile, "stderr")==0 ){
11348 }else if( strcmp(zFile, "off")==0 ){
11351 f = fopen(zFile, bTextMode ? "w" : "wb");
11353 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11359 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
11361 ** A routine for handling output from sqlite3_trace().
11363 static int sql_trace_callback(
11369 FILE *f = (FILE*)pArg;
11370 UNUSED_PARAMETER(mType);
11371 UNUSED_PARAMETER(pP);
11373 const char *z = (const char*)pX;
11374 int i = strlen30(z);
11375 while( i>0 && z[i-1]==';' ){ i--; }
11376 utf8_printf(f, "%.*s;\n", i, z);
11383 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
11384 ** a useful spot to set a debugger breakpoint.
11386 static void test_breakpoint(void){
11387 static int nCall = 0;
11392 ** An object used to read a CSV and other files for import.
11394 typedef struct ImportCtx ImportCtx;
11396 const char *zFile; /* Name of the input file */
11397 FILE *in; /* Read the CSV text from this input stream */
11398 char *z; /* Accumulated text for a field */
11399 int n; /* Number of bytes in z */
11400 int nAlloc; /* Space allocated for z[] */
11401 int nLine; /* Current line number */
11402 int bNotFirst; /* True if one or more bytes already read */
11403 int cTerm; /* Character that terminated the most recent field */
11404 int cColSep; /* The column separator character. (Usually ",") */
11405 int cRowSep; /* The row separator character. (Usually "\n") */
11408 /* Append a single byte to z[] */
11409 static void import_append_char(ImportCtx *p, int c){
11410 if( p->n+1>=p->nAlloc ){
11411 p->nAlloc += p->nAlloc + 100;
11412 p->z = sqlite3_realloc64(p->z, p->nAlloc);
11413 if( p->z==0 ) shell_out_of_memory();
11415 p->z[p->n++] = (char)c;
11418 /* Read a single field of CSV text. Compatible with rfc4180 and extended
11419 ** with the option of having a separator other than ",".
11421 ** + Input comes from p->in.
11422 ** + Store results in p->z of length p->n. Space to hold p->z comes
11423 ** from sqlite3_malloc64().
11424 ** + Use p->cSep as the column separator. The default is ",".
11425 ** + Use p->rSep as the row separator. The default is "\n".
11426 ** + Keep track of the line number in p->nLine.
11427 ** + Store the character that terminates the field in p->cTerm. Store
11428 ** EOF on end-of-file.
11429 ** + Report syntax errors on stderr
11431 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
11433 int cSep = p->cColSep;
11434 int rSep = p->cRowSep;
11437 if( c==EOF || seenInterrupt ){
11443 int startLine = p->nLine;
11448 if( c==rSep ) p->nLine++;
11455 if( (c==cSep && pc==cQuote)
11456 || (c==rSep && pc==cQuote)
11457 || (c==rSep && pc=='\r' && ppc==cQuote)
11458 || (c==EOF && pc==cQuote)
11460 do{ p->n--; }while( p->z[p->n]!=cQuote );
11464 if( pc==cQuote && c!='\r' ){
11465 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
11466 p->zFile, p->nLine, cQuote);
11469 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
11470 p->zFile, startLine, cQuote);
11474 import_append_char(p, c);
11479 /* If this is the first field being parsed and it begins with the
11480 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
11481 if( (c&0xff)==0xef && p->bNotFirst==0 ){
11482 import_append_char(p, c);
11484 if( (c&0xff)==0xbb ){
11485 import_append_char(p, c);
11487 if( (c&0xff)==0xbf ){
11490 return csv_read_one_field(p);
11494 while( c!=EOF && c!=cSep && c!=rSep ){
11495 import_append_char(p, c);
11500 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
11504 if( p->z ) p->z[p->n] = 0;
11509 /* Read a single field of ASCII delimited text.
11511 ** + Input comes from p->in.
11512 ** + Store results in p->z of length p->n. Space to hold p->z comes
11513 ** from sqlite3_malloc64().
11514 ** + Use p->cSep as the column separator. The default is "\x1F".
11515 ** + Use p->rSep as the row separator. The default is "\x1E".
11516 ** + Keep track of the row number in p->nLine.
11517 ** + Store the character that terminates the field in p->cTerm. Store
11518 ** EOF on end-of-file.
11519 ** + Report syntax errors on stderr
11521 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
11523 int cSep = p->cColSep;
11524 int rSep = p->cRowSep;
11527 if( c==EOF || seenInterrupt ){
11531 while( c!=EOF && c!=cSep && c!=rSep ){
11532 import_append_char(p, c);
11539 if( p->z ) p->z[p->n] = 0;
11544 ** Try to transfer data for table zTable. If an error is seen while
11545 ** moving forward, try to go backwards. The backwards movement won't
11546 ** work for WITHOUT ROWID tables.
11548 static void tryToCloneData(
11553 sqlite3_stmt *pQuery = 0;
11554 sqlite3_stmt *pInsert = 0;
11559 int nTable = strlen30(zTable);
11562 const int spinRate = 10000;
11564 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
11565 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11567 utf8_printf(stderr, "Error %d: %s on [%s]\n",
11568 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11570 goto end_data_xfer;
11572 n = sqlite3_column_count(pQuery);
11573 zInsert = sqlite3_malloc64(200 + nTable + n*3);
11574 if( zInsert==0 ) shell_out_of_memory();
11575 sqlite3_snprintf(200+nTable,zInsert,
11576 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
11577 i = strlen30(zInsert);
11578 for(j=1; j<n; j++){
11579 memcpy(zInsert+i, ",?", 2);
11582 memcpy(zInsert+i, ");", 3);
11583 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
11585 utf8_printf(stderr, "Error %d: %s on [%s]\n",
11586 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
11588 goto end_data_xfer;
11590 for(k=0; k<2; k++){
11591 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11592 for(i=0; i<n; i++){
11593 switch( sqlite3_column_type(pQuery, i) ){
11594 case SQLITE_NULL: {
11595 sqlite3_bind_null(pInsert, i+1);
11598 case SQLITE_INTEGER: {
11599 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
11602 case SQLITE_FLOAT: {
11603 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
11606 case SQLITE_TEXT: {
11607 sqlite3_bind_text(pInsert, i+1,
11608 (const char*)sqlite3_column_text(pQuery,i),
11609 -1, SQLITE_STATIC);
11612 case SQLITE_BLOB: {
11613 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
11614 sqlite3_column_bytes(pQuery,i),
11620 rc = sqlite3_step(pInsert);
11621 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
11622 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
11623 sqlite3_errmsg(newDb));
11625 sqlite3_reset(pInsert);
11627 if( (cnt%spinRate)==0 ){
11628 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
11632 if( rc==SQLITE_DONE ) break;
11633 sqlite3_finalize(pQuery);
11634 sqlite3_free(zQuery);
11635 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
11637 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11639 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
11642 } /* End for(k=0...) */
11645 sqlite3_finalize(pQuery);
11646 sqlite3_finalize(pInsert);
11647 sqlite3_free(zQuery);
11648 sqlite3_free(zInsert);
11653 ** Try to transfer all rows of the schema that match zWhere. For
11654 ** each row, invoke xForEach() on the object defined by that row.
11655 ** If an error is encountered while moving forward through the
11656 ** sqlite_master table, try again moving backwards.
11658 static void tryToCloneSchema(
11661 const char *zWhere,
11662 void (*xForEach)(ShellState*,sqlite3*,const char*)
11664 sqlite3_stmt *pQuery = 0;
11667 const unsigned char *zName;
11668 const unsigned char *zSql;
11671 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11672 " WHERE %s", zWhere);
11673 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11675 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11676 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11678 goto end_schema_xfer;
11680 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11681 zName = sqlite3_column_text(pQuery, 0);
11682 zSql = sqlite3_column_text(pQuery, 1);
11683 printf("%s... ", zName); fflush(stdout);
11684 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11686 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11687 sqlite3_free(zErrMsg);
11691 xForEach(p, newDb, (const char*)zName);
11695 if( rc!=SQLITE_DONE ){
11696 sqlite3_finalize(pQuery);
11697 sqlite3_free(zQuery);
11698 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11699 " WHERE %s ORDER BY rowid DESC", zWhere);
11700 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11702 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11703 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11705 goto end_schema_xfer;
11707 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11708 zName = sqlite3_column_text(pQuery, 0);
11709 zSql = sqlite3_column_text(pQuery, 1);
11710 printf("%s... ", zName); fflush(stdout);
11711 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11713 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11714 sqlite3_free(zErrMsg);
11718 xForEach(p, newDb, (const char*)zName);
11724 sqlite3_finalize(pQuery);
11725 sqlite3_free(zQuery);
11729 ** Open a new database file named "zNewDb". Try to recover as much information
11730 ** as possible out of the main database (which might be corrupt) and write it
11733 static void tryToClone(ShellState *p, const char *zNewDb){
11735 sqlite3 *newDb = 0;
11736 if( access(zNewDb,0)==0 ){
11737 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
11740 rc = sqlite3_open(zNewDb, &newDb);
11742 utf8_printf(stderr, "Cannot create output database: %s\n",
11743 sqlite3_errmsg(newDb));
11745 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
11746 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
11747 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
11748 tryToCloneSchema(p, newDb, "type!='table'", 0);
11749 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
11750 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
11756 ** Change the output file back to stdout.
11758 ** If the p->doXdgOpen flag is set, that means the output was being
11759 ** redirected to a temporary file named by p->zTempFile. In that case,
11760 ** launch start/open/xdg-open on that temporary file.
11762 static void output_reset(ShellState *p){
11763 if( p->outfile[0]=='|' ){
11764 #ifndef SQLITE_OMIT_POPEN
11768 output_file_close(p->out);
11769 #ifndef SQLITE_NOHAVE_SYSTEM
11770 if( p->doXdgOpen ){
11771 const char *zXdgOpenCmd =
11772 #if defined(_WIN32)
11774 #elif defined(__APPLE__)
11780 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
11781 if( system(zCmd) ){
11782 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
11784 sqlite3_free(zCmd);
11788 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
11795 ** Run an SQL command and return the single integer result.
11797 static int db_int(ShellState *p, const char *zSql){
11798 sqlite3_stmt *pStmt;
11800 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11801 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
11802 res = sqlite3_column_int(pStmt,0);
11804 sqlite3_finalize(pStmt);
11809 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
11811 static unsigned int get2byteInt(unsigned char *a){
11812 return (a[0]<<8) + a[1];
11814 static unsigned int get4byteInt(unsigned char *a){
11815 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
11819 ** Implementation of the ".info" command.
11821 ** Return 1 on error, 2 to exit, and 0 otherwise.
11823 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
11824 static const struct { const char *zName; int ofst; } aField[] = {
11825 { "file change counter:", 24 },
11826 { "database page count:", 28 },
11827 { "freelist page count:", 36 },
11828 { "schema cookie:", 40 },
11829 { "schema format:", 44 },
11830 { "default cache size:", 48 },
11831 { "autovacuum top root:", 52 },
11832 { "incremental vacuum:", 64 },
11833 { "text encoding:", 56 },
11834 { "user version:", 60 },
11835 { "application id:", 68 },
11836 { "software version:", 96 },
11838 static const struct { const char *zName; const char *zSql; } aQuery[] = {
11839 { "number of tables:",
11840 "SELECT count(*) FROM %s WHERE type='table'" },
11841 { "number of indexes:",
11842 "SELECT count(*) FROM %s WHERE type='index'" },
11843 { "number of triggers:",
11844 "SELECT count(*) FROM %s WHERE type='trigger'" },
11845 { "number of views:",
11846 "SELECT count(*) FROM %s WHERE type='view'" },
11848 "SELECT total(length(sql)) FROM %s" },
11852 char *zDb = nArg>=2 ? azArg[1] : "main";
11853 sqlite3_stmt *pStmt = 0;
11854 unsigned char aHdr[100];
11856 if( p->db==0 ) return 1;
11857 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
11859 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
11860 if( sqlite3_step(pStmt)==SQLITE_ROW
11861 && sqlite3_column_bytes(pStmt,0)>100
11863 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
11864 sqlite3_finalize(pStmt);
11866 raw_printf(stderr, "unable to read database header\n");
11867 sqlite3_finalize(pStmt);
11870 i = get2byteInt(aHdr+16);
11871 if( i==1 ) i = 65536;
11872 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
11873 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
11874 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
11875 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
11876 for(i=0; i<ArraySize(aField); i++){
11877 int ofst = aField[i].ofst;
11878 unsigned int val = get4byteInt(aHdr + ofst);
11879 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
11882 if( val==1 ) raw_printf(p->out, " (utf8)");
11883 if( val==2 ) raw_printf(p->out, " (utf16le)");
11884 if( val==3 ) raw_printf(p->out, " (utf16be)");
11887 raw_printf(p->out, "\n");
11890 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
11891 }else if( strcmp(zDb,"temp")==0 ){
11892 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
11894 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
11896 for(i=0; i<ArraySize(aQuery); i++){
11897 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
11898 int val = db_int(p, zSql);
11899 sqlite3_free(zSql);
11900 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
11902 sqlite3_free(zSchemaTab);
11907 ** Print the current sqlite3_errmsg() value to stderr and return 1.
11909 static int shellDatabaseError(sqlite3 *db){
11910 const char *zErr = sqlite3_errmsg(db);
11911 utf8_printf(stderr, "Error: %s\n", zErr);
11916 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
11917 ** if they match and FALSE (0) if they do not match.
11921 ** '*' Matches any sequence of zero or more characters.
11923 ** '?' Matches exactly one character.
11925 ** [...] Matches one character from the enclosed list of
11928 ** [^...] Matches one character not in the enclosed list.
11930 ** '#' Matches any sequence of one or more digits with an
11931 ** optional + or - sign in front
11933 ** ' ' Any span of whitespace matches any other span of
11936 ** Extra whitespace at the end of z[] is ignored.
11938 static int testcase_glob(const char *zGlob, const char *z){
11943 while( (c = (*(zGlob++)))!=0 ){
11945 if( !IsSpace(*z) ) return 0;
11946 while( IsSpace(*zGlob) ) zGlob++;
11947 while( IsSpace(*z) ) z++;
11948 }else if( c=='*' ){
11949 while( (c=(*(zGlob++))) == '*' || c=='?' ){
11950 if( c=='?' && (*(z++))==0 ) return 0;
11954 }else if( c=='[' ){
11955 while( *z && testcase_glob(zGlob-1,z)==0 ){
11960 while( (c2 = (*(z++)))!=0 ){
11963 if( c2==0 ) return 0;
11965 if( testcase_glob(zGlob,z) ) return 1;
11968 }else if( c=='?' ){
11969 if( (*(z++))==0 ) return 0;
11970 }else if( c=='[' ){
11975 if( c==0 ) return 0;
11982 if( c==']' ) seen = 1;
11985 while( c2 && c2!=']' ){
11986 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
11988 if( c>=prior_c && c<=c2 ) seen = 1;
11998 if( c2==0 || (seen ^ invert)==0 ) return 0;
11999 }else if( c=='#' ){
12000 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12001 if( !IsDigit(z[0]) ) return 0;
12003 while( IsDigit(z[0]) ){ z++; }
12005 if( c!=(*(z++)) ) return 0;
12008 while( IsSpace(*z) ){ z++; }
12014 ** Compare the string as a command-line option with either one or two
12015 ** initial "-" characters.
12017 static int optionMatch(const char *zStr, const char *zOpt){
12018 if( zStr[0]!='-' ) return 0;
12020 if( zStr[0]=='-' ) zStr++;
12021 return strcmp(zStr, zOpt)==0;
12027 int shellDeleteFile(const char *zFilename){
12030 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12034 rc = unlink(zFilename);
12040 ** Try to delete the temporary file (if there is one) and free the
12041 ** memory used to hold the name of the temp file.
12043 static void clearTempFile(ShellState *p){
12044 if( p->zTempFile==0 ) return;
12045 if( p->doXdgOpen ) return;
12046 if( shellDeleteFile(p->zTempFile) ) return;
12047 sqlite3_free(p->zTempFile);
12052 ** Create a new temp file name with the given suffix.
12054 static void newTempFile(ShellState *p, const char *zSuffix){
12056 sqlite3_free(p->zTempFile);
12059 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12061 if( p->zTempFile==0 ){
12063 sqlite3_randomness(sizeof(r), &r);
12064 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12066 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12068 if( p->zTempFile==0 ){
12069 raw_printf(stderr, "out of memory\n");
12076 ** The implementation of SQL scalar function fkey_collate_clause(), used
12077 ** by the ".lint fkey-indexes" command. This scalar function is always
12078 ** called with four arguments - the parent table name, the parent column name,
12079 ** the child table name and the child column name.
12081 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12083 ** If either of the named tables or columns do not exist, this function
12084 ** returns an empty string. An empty string is also returned if both tables
12085 ** and columns exist but have the same default collation sequence. Or,
12086 ** if both exist but the default collation sequences are different, this
12087 ** function returns the string " COLLATE <parent-collation>", where
12088 ** <parent-collation> is the default collation sequence of the parent column.
12090 static void shellFkeyCollateClause(
12091 sqlite3_context *pCtx,
12093 sqlite3_value **apVal
12095 sqlite3 *db = sqlite3_context_db_handle(pCtx);
12096 const char *zParent;
12097 const char *zParentCol;
12098 const char *zParentSeq;
12099 const char *zChild;
12100 const char *zChildCol;
12101 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
12105 zParent = (const char*)sqlite3_value_text(apVal[0]);
12106 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12107 zChild = (const char*)sqlite3_value_text(apVal[2]);
12108 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12110 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12111 rc = sqlite3_table_column_metadata(
12112 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12114 if( rc==SQLITE_OK ){
12115 rc = sqlite3_table_column_metadata(
12116 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12120 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12121 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12122 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12129 ** The implementation of dot-command ".lint fkey-indexes".
12131 static int lintFkeyIndexes(
12132 ShellState *pState, /* Current shell tool state */
12133 char **azArg, /* Array of arguments passed to dot command */
12134 int nArg /* Number of entries in azArg[] */
12136 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
12137 FILE *out = pState->out; /* Stream to write non-error output to */
12138 int bVerbose = 0; /* If -verbose is present */
12139 int bGroupByParent = 0; /* If -groupbyparent is present */
12140 int i; /* To iterate through azArg[] */
12141 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
12142 int rc; /* Return code */
12143 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
12146 ** This SELECT statement returns one row for each foreign key constraint
12147 ** in the schema of the main database. The column values are:
12149 ** 0. The text of an SQL statement similar to:
12151 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12153 ** This SELECT is similar to the one that the foreign keys implementation
12154 ** needs to run internally on child tables. If there is an index that can
12155 ** be used to optimize this query, then it can also be used by the FK
12156 ** implementation to optimize DELETE or UPDATE statements on the parent
12159 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12160 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12161 ** contains an index that can be used to optimize the query.
12163 ** 2. Human readable text that describes the child table and columns. e.g.
12165 ** "child_table(child_key1, child_key2)"
12167 ** 3. Human readable text that describes the parent table and columns. e.g.
12169 ** "parent_table(parent_key1, parent_key2)"
12171 ** 4. A full CREATE INDEX statement for an index that could be used to
12172 ** optimize DELETE or UPDATE statements on the parent table. e.g.
12174 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
12176 ** 5. The name of the parent table.
12178 ** These six values are used by the C logic below to generate the report.
12182 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12183 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12184 " || fkey_collate_clause("
12185 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12187 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12188 " || group_concat('*=?', ' AND ') || ')'"
12190 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
12192 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12194 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12195 " || ' ON ' || quote(s.name) || '('"
12196 " || group_concat(quote(f.[from]) ||"
12197 " fkey_collate_clause("
12198 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12202 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12203 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12204 "GROUP BY s.name, f.id "
12205 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12207 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12209 for(i=2; i<nArg; i++){
12210 int n = strlen30(azArg[i]);
12211 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12214 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12215 bGroupByParent = 1;
12219 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12222 return SQLITE_ERROR;
12226 /* Register the fkey_collate_clause() SQL function */
12227 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12228 0, shellFkeyCollateClause, 0, 0
12232 if( rc==SQLITE_OK ){
12233 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12235 if( rc==SQLITE_OK ){
12236 sqlite3_bind_int(pSql, 1, bGroupByParent);
12239 if( rc==SQLITE_OK ){
12242 while( SQLITE_ROW==sqlite3_step(pSql) ){
12244 sqlite3_stmt *pExplain = 0;
12245 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12246 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12247 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12248 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12249 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12250 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12252 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12253 if( rc!=SQLITE_OK ) break;
12254 if( SQLITE_ROW==sqlite3_step(pExplain) ){
12255 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12257 0==sqlite3_strglob(zGlob, zPlan)
12258 || 0==sqlite3_strglob(zGlobIPK, zPlan)
12261 rc = sqlite3_finalize(pExplain);
12262 if( rc!=SQLITE_OK ) break;
12265 raw_printf(stderr, "Error: internal error");
12269 && (bVerbose || res==0)
12270 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12272 raw_printf(out, "-- Parent table %s\n", zParent);
12273 sqlite3_free(zPrev);
12274 zPrev = sqlite3_mprintf("%s", zParent);
12278 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12279 }else if( bVerbose ){
12280 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12281 zIndent, zFrom, zTarget
12286 sqlite3_free(zPrev);
12288 if( rc!=SQLITE_OK ){
12289 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12292 rc2 = sqlite3_finalize(pSql);
12293 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12295 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12298 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12305 ** Implementation of ".lint" dot command.
12307 static int lintDotCommand(
12308 ShellState *pState, /* Current shell tool state */
12309 char **azArg, /* Array of arguments passed to dot command */
12310 int nArg /* Number of entries in azArg[] */
12313 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12314 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12315 return lintFkeyIndexes(pState, azArg, nArg);
12318 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12319 raw_printf(stderr, "Where sub-commands are:\n");
12320 raw_printf(stderr, " fkey-indexes\n");
12321 return SQLITE_ERROR;
12324 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12325 /*********************************************************************************
12326 ** The ".archive" or ".ar" command.
12328 static void shellPrepare(
12332 sqlite3_stmt **ppStmt
12335 if( *pRc==SQLITE_OK ){
12336 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12337 if( rc!=SQLITE_OK ){
12338 raw_printf(stderr, "sql error: %s (%d)\n",
12339 sqlite3_errmsg(db), sqlite3_errcode(db)
12346 static void shellPreparePrintf(
12349 sqlite3_stmt **ppStmt,
12354 if( *pRc==SQLITE_OK ){
12357 va_start(ap, zFmt);
12358 z = sqlite3_vmprintf(zFmt, ap);
12360 *pRc = SQLITE_NOMEM;
12362 shellPrepare(db, pRc, z, ppStmt);
12368 static void shellFinalize(
12370 sqlite3_stmt *pStmt
12373 sqlite3 *db = sqlite3_db_handle(pStmt);
12374 int rc = sqlite3_finalize(pStmt);
12375 if( *pRc==SQLITE_OK ){
12376 if( rc!=SQLITE_OK ){
12377 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12384 static void shellReset(
12386 sqlite3_stmt *pStmt
12388 int rc = sqlite3_reset(pStmt);
12389 if( *pRc==SQLITE_OK ){
12390 if( rc!=SQLITE_OK ){
12391 sqlite3 *db = sqlite3_db_handle(pStmt);
12392 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12398 ** Structure representing a single ".ar" command.
12400 typedef struct ArCommand ArCommand;
12402 u8 eCmd; /* An AR_CMD_* value */
12403 u8 bVerbose; /* True if --verbose */
12404 u8 bZip; /* True if the archive is a ZIP */
12405 u8 bDryRun; /* True if --dry-run */
12406 u8 bAppend; /* True if --append */
12407 u8 fromCmdLine; /* Run from -A instead of .archive */
12408 int nArg; /* Number of command arguments */
12409 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
12410 const char *zFile; /* --file argument, or NULL */
12411 const char *zDir; /* --directory argument, or NULL */
12412 char **azArg; /* Array of command arguments */
12413 ShellState *p; /* Shell state */
12414 sqlite3 *db; /* Database containing the archive */
12418 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
12420 static int arUsage(FILE *f){
12423 "Usage: .ar [OPTION...] [FILE...]\n"
12424 "The .ar command manages sqlar archives.\n"
12427 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar\n"
12428 " .ar -tf archive.sar # List members of archive.sar\n"
12429 " .ar -xvf archive.sar # Verbosely extract files from archive.sar\n"
12431 "Each command line must feature exactly one command option:\n"
12432 " -c, --create Create a new archive\n"
12433 " -u, --update Update or add files to an existing archive\n"
12434 " -t, --list List contents of archive\n"
12435 " -x, --extract Extract files from archive\n"
12437 "And zero or more optional options:\n"
12438 " -v, --verbose Print each filename as it is processed\n"
12439 " -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
12440 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n"
12441 " -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
12442 " -n, --dryrun Show the SQL that would have occurred\n"
12444 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
12447 return SQLITE_ERROR;
12451 ** Print an error message for the .ar command to stderr and return
12454 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12457 va_start(ap, zFmt);
12458 z = sqlite3_vmprintf(zFmt, ap);
12460 utf8_printf(stderr, "Error: %s\n", z);
12461 if( pAr->fromCmdLine ){
12462 utf8_printf(stderr, "Use \"-A\" for more help\n");
12464 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12467 return SQLITE_ERROR;
12471 ** Values for ArCommand.eCmd.
12473 #define AR_CMD_CREATE 1
12474 #define AR_CMD_EXTRACT 2
12475 #define AR_CMD_LIST 3
12476 #define AR_CMD_UPDATE 4
12477 #define AR_CMD_HELP 5
12480 ** Other (non-command) switches.
12482 #define AR_SWITCH_VERBOSE 6
12483 #define AR_SWITCH_FILE 7
12484 #define AR_SWITCH_DIRECTORY 8
12485 #define AR_SWITCH_APPEND 9
12486 #define AR_SWITCH_DRYRUN 10
12488 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
12490 case AR_CMD_CREATE:
12491 case AR_CMD_EXTRACT:
12493 case AR_CMD_UPDATE:
12496 return arErrorMsg(pAr, "multiple command options");
12498 pAr->eCmd = eSwitch;
12501 case AR_SWITCH_DRYRUN:
12504 case AR_SWITCH_VERBOSE:
12507 case AR_SWITCH_APPEND:
12509 /* Fall thru into --file */
12510 case AR_SWITCH_FILE:
12513 case AR_SWITCH_DIRECTORY:
12522 ** Parse the command line for an ".ar" command. The results are written into
12523 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
12524 ** successfully, otherwise an error message is written to stderr and
12525 ** SQLITE_ERROR returned.
12527 static int arParseCommand(
12528 char **azArg, /* Array of arguments passed to dot command */
12529 int nArg, /* Number of entries in azArg[] */
12530 ArCommand *pAr /* Populate this object */
12538 { "create", 'c', AR_CMD_CREATE, 0 },
12539 { "extract", 'x', AR_CMD_EXTRACT, 0 },
12540 { "list", 't', AR_CMD_LIST, 0 },
12541 { "update", 'u', AR_CMD_UPDATE, 0 },
12542 { "help", 'h', AR_CMD_HELP, 0 },
12543 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
12544 { "file", 'f', AR_SWITCH_FILE, 1 },
12545 { "append", 'a', AR_SWITCH_APPEND, 1 },
12546 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
12547 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
12549 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
12550 struct ArSwitch *pEnd = &aSwitch[nSwitch];
12553 return arUsage(stderr);
12555 char *z = azArg[1];
12557 /* Traditional style [tar] invocation */
12560 for(i=0; z[i]; i++){
12561 const char *zArg = 0;
12562 struct ArSwitch *pOpt;
12563 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12564 if( z[i]==pOpt->cShort ) break;
12567 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12571 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12573 zArg = azArg[iArg++];
12575 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12577 pAr->nArg = nArg-iArg;
12579 pAr->azArg = &azArg[iArg];
12582 /* Non-traditional invocation */
12584 for(iArg=1; iArg<nArg; iArg++){
12588 /* All remaining command line words are command arguments. */
12589 pAr->azArg = &azArg[iArg];
12590 pAr->nArg = nArg-iArg;
12597 /* One or more short options */
12598 for(i=1; i<n; i++){
12599 const char *zArg = 0;
12600 struct ArSwitch *pOpt;
12601 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12602 if( z[i]==pOpt->cShort ) break;
12605 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12612 if( iArg>=(nArg-1) ){
12613 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12615 zArg = azArg[++iArg];
12618 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12620 }else if( z[2]=='\0' ){
12621 /* A -- option, indicating that all remaining command line words
12622 ** are command arguments. */
12623 pAr->azArg = &azArg[iArg+1];
12624 pAr->nArg = nArg-iArg-1;
12627 /* A long option */
12628 const char *zArg = 0; /* Argument for option, if any */
12629 struct ArSwitch *pMatch = 0; /* Matching option */
12630 struct ArSwitch *pOpt; /* Iterator */
12631 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12632 const char *zLong = pOpt->zLong;
12633 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
12635 return arErrorMsg(pAr, "ambiguous option: %s",z);
12643 return arErrorMsg(pAr, "unrecognized option: %s", z);
12645 if( pMatch->bArg ){
12646 if( iArg>=(nArg-1) ){
12647 return arErrorMsg(pAr, "option requires an argument: %s", z);
12649 zArg = azArg[++iArg];
12651 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
12661 ** This function assumes that all arguments within the ArCommand.azArg[]
12662 ** array refer to archive members, as for the --extract or --list commands.
12663 ** It checks that each of them are present. If any specified file is not
12664 ** present in the archive, an error is printed to stderr and an error
12665 ** code returned. Otherwise, if all specified arguments are present in
12666 ** the archive, SQLITE_OK is returned.
12668 ** This function strips any trailing '/' characters from each argument.
12669 ** This is consistent with the way the [tar] command seems to work on
12672 static int arCheckEntries(ArCommand *pAr){
12673 int rc = SQLITE_OK;
12676 sqlite3_stmt *pTest = 0;
12678 shellPreparePrintf(pAr->db, &rc, &pTest,
12679 "SELECT name FROM %s WHERE name=$name",
12682 j = sqlite3_bind_parameter_index(pTest, "$name");
12683 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12684 char *z = pAr->azArg[i];
12685 int n = strlen30(z);
12687 while( n>0 && z[n-1]=='/' ) n--;
12689 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
12690 if( SQLITE_ROW==sqlite3_step(pTest) ){
12693 shellReset(&rc, pTest);
12694 if( rc==SQLITE_OK && bOk==0 ){
12695 utf8_printf(stderr, "not found in archive: %s\n", z);
12699 shellFinalize(&rc, pTest);
12705 ** Format a WHERE clause that can be used against the "sqlar" table to
12706 ** identify all archive members that match the command arguments held
12707 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
12708 ** The caller is responsible for eventually calling sqlite3_free() on
12709 ** any non-NULL (*pzWhere) value.
12711 static void arWhereClause(
12714 char **pzWhere /* OUT: New WHERE clause */
12717 if( *pRc==SQLITE_OK ){
12718 if( pAr->nArg==0 ){
12719 zWhere = sqlite3_mprintf("1");
12722 const char *zSep = "";
12723 for(i=0; i<pAr->nArg; i++){
12724 const char *z = pAr->azArg[i];
12725 zWhere = sqlite3_mprintf(
12726 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
12727 zWhere, zSep, z, strlen30(z)+1, z
12730 *pRc = SQLITE_NOMEM;
12741 ** Implementation of .ar "lisT" command.
12743 static int arListCommand(ArCommand *pAr){
12744 const char *zSql = "SELECT %s FROM %s WHERE %s";
12745 const char *azCols[] = {
12747 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
12751 sqlite3_stmt *pSql = 0;
12754 rc = arCheckEntries(pAr);
12755 arWhereClause(&rc, pAr, &zWhere);
12757 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
12758 pAr->zSrcTable, zWhere);
12759 if( pAr->bDryRun ){
12760 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12762 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12763 if( pAr->bVerbose ){
12764 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
12765 sqlite3_column_text(pSql, 0),
12766 sqlite3_column_int(pSql, 1),
12767 sqlite3_column_text(pSql, 2),
12768 sqlite3_column_text(pSql, 3)
12771 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12775 shellFinalize(&rc, pSql);
12776 sqlite3_free(zWhere);
12782 ** Implementation of .ar "eXtract" command.
12784 static int arExtractCommand(ArCommand *pAr){
12785 const char *zSql1 =
12788 " writefile(($dir || name), %s, mode, mtime) "
12789 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
12791 const char *azExtraArg[] = {
12792 "sqlar_uncompress(data, sz)",
12796 sqlite3_stmt *pSql = 0;
12797 int rc = SQLITE_OK;
12802 /* If arguments are specified, check that they actually exist within
12803 ** the archive before proceeding. And formulate a WHERE clause to
12805 rc = arCheckEntries(pAr);
12806 arWhereClause(&rc, pAr, &zWhere);
12808 if( rc==SQLITE_OK ){
12810 zDir = sqlite3_mprintf("%s/", pAr->zDir);
12812 zDir = sqlite3_mprintf("");
12814 if( zDir==0 ) rc = SQLITE_NOMEM;
12817 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
12818 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
12821 if( rc==SQLITE_OK ){
12822 j = sqlite3_bind_parameter_index(pSql, "$dir");
12823 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
12825 /* Run the SELECT statement twice. The first time, writefile() is called
12826 ** for all archive members that should be extracted. The second time,
12827 ** only for the directories. This is because the timestamps for
12828 ** extracted directories must be reset after they are populated (as
12829 ** populating them changes the timestamp). */
12830 for(i=0; i<2; i++){
12831 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
12832 sqlite3_bind_int(pSql, j, i);
12833 if( pAr->bDryRun ){
12834 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12836 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12837 if( i==0 && pAr->bVerbose ){
12838 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12842 shellReset(&rc, pSql);
12844 shellFinalize(&rc, pSql);
12847 sqlite3_free(zDir);
12848 sqlite3_free(zWhere);
12853 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
12855 static int arExecSql(ArCommand *pAr, const char *zSql){
12857 if( pAr->bDryRun ){
12858 utf8_printf(pAr->p->out, "%s\n", zSql);
12862 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
12864 utf8_printf(stdout, "ERROR: %s\n", zErr);
12865 sqlite3_free(zErr);
12873 ** Implementation of .ar "create" and "update" commands.
12875 ** Create the "sqlar" table in the database if it does not already exist.
12876 ** Then add each file in the azFile[] array to the archive. Directories
12877 ** are added recursively. If argument bVerbose is non-zero, a message is
12878 ** printed on stdout for each file archived.
12880 ** The create command is the same as update, except that it drops
12881 ** any existing "sqlar" table before beginning.
12883 static int arCreateOrUpdateCommand(
12884 ArCommand *pAr, /* Command arguments and options */
12885 int bUpdate /* true for a --create. false for --update */
12887 const char *zCreate =
12888 "CREATE TABLE IF NOT EXISTS sqlar(\n"
12889 " name TEXT PRIMARY KEY, -- name of the file\n"
12890 " mode INT, -- access permissions\n"
12891 " mtime INT, -- last modification time\n"
12892 " sz INT, -- original file size\n"
12893 " data BLOB -- compressed content\n"
12895 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12896 const char *zInsertFmt[2] = {
12897 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
12902 " CASE substr(lsmode(mode),1,1)\n"
12903 " WHEN '-' THEN length(data)\n"
12904 " WHEN 'd' THEN 0\n"
12906 " sqlar_compress(data)\n"
12907 " FROM fsdir(%Q,%Q)\n"
12908 " WHERE lsmode(mode) NOT LIKE '?%%';",
12909 "REPLACE INTO %s(name,mode,mtime,data)\n"
12915 " FROM fsdir(%Q,%Q)\n"
12916 " WHERE lsmode(mode) NOT LIKE '?%%';"
12918 int i; /* For iterating through azFile[] */
12919 int rc; /* Return code */
12920 const char *zTab = 0; /* SQL table into which to insert */
12924 arExecSql(pAr, "PRAGMA page_size=512");
12925 rc = arExecSql(pAr, "SAVEPOINT ar;");
12926 if( rc!=SQLITE_OK ) return rc;
12929 /* Initialize the zipfile virtual table, if necessary */
12932 sqlite3_randomness(sizeof(r),&r);
12933 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
12935 zSql = sqlite3_mprintf(
12936 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
12939 rc = arExecSql(pAr, zSql);
12940 sqlite3_free(zSql);
12945 /* Initialize the table for an SQLAR */
12948 rc = arExecSql(pAr, zDrop);
12949 if( rc!=SQLITE_OK ) goto end_ar_transaction;
12951 rc = arExecSql(pAr, zCreate);
12953 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12954 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
12955 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12956 pAr->azArg[i], pAr->zDir);
12957 rc = arExecSql(pAr, zSql2);
12958 sqlite3_free(zSql2);
12960 end_ar_transaction:
12961 if( rc!=SQLITE_OK ){
12962 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12964 rc = arExecSql(pAr, "RELEASE ar;");
12965 if( pAr->bZip && pAr->zFile ){
12966 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
12967 arExecSql(pAr, zSql);
12968 sqlite3_free(zSql);
12975 ** Implementation of ".ar" dot command.
12977 static int arDotCommand(
12978 ShellState *pState, /* Current shell tool state */
12979 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
12980 char **azArg, /* Array of arguments passed to dot command */
12981 int nArg /* Number of entries in azArg[] */
12985 memset(&cmd, 0, sizeof(cmd));
12986 cmd.fromCmdLine = fromCmdLine;
12987 rc = arParseCommand(azArg, nArg, &cmd);
12988 if( rc==SQLITE_OK ){
12989 int eDbType = SHELL_OPEN_UNSPEC;
12991 cmd.db = pState->db;
12993 eDbType = deduceDatabaseType(cmd.zFile, 1);
12995 eDbType = pState->openMode;
12997 if( eDbType==SHELL_OPEN_ZIPFILE ){
12998 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
12999 if( cmd.zFile==0 ){
13000 cmd.zSrcTable = sqlite3_mprintf("zip");
13002 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13006 }else if( cmd.zFile ){
13008 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13009 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13010 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13012 flags = SQLITE_OPEN_READONLY;
13016 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13017 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13019 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
13020 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13021 if( rc!=SQLITE_OK ){
13022 utf8_printf(stderr, "cannot open file: %s (%s)\n",
13023 cmd.zFile, sqlite3_errmsg(cmd.db)
13025 goto end_ar_command;
13027 sqlite3_fileio_init(cmd.db, 0, 0);
13028 sqlite3_sqlar_init(cmd.db, 0, 0);
13029 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13030 shellPutsFunc, 0, 0);
13033 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13034 if( cmd.eCmd!=AR_CMD_CREATE
13035 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13037 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13039 goto end_ar_command;
13041 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13044 switch( cmd.eCmd ){
13045 case AR_CMD_CREATE:
13046 rc = arCreateOrUpdateCommand(&cmd, 0);
13049 case AR_CMD_EXTRACT:
13050 rc = arExtractCommand(&cmd);
13054 rc = arListCommand(&cmd);
13058 arUsage(pState->out);
13062 assert( cmd.eCmd==AR_CMD_UPDATE );
13063 rc = arCreateOrUpdateCommand(&cmd, 1);
13068 if( cmd.db!=pState->db ){
13071 sqlite3_free(cmd.zSrcTable);
13075 /* End of the ".archive" or ".ar" command logic
13076 **********************************************************************************/
13077 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13081 ** If an input line begins with "." then invoke this routine to
13082 ** process that line.
13084 ** Return 1 on error, 2 to exit, and 0 otherwise.
13086 static int do_meta_command(char *zLine, ShellState *p){
13093 #ifndef SQLITE_OMIT_VIRTUALTABLE
13094 if( p->expert.pExpert ){
13095 expertFinish(p, 1, 0);
13099 /* Parse the input line into tokens.
13101 while( zLine[h] && nArg<ArraySize(azArg) ){
13102 while( IsSpace(zLine[h]) ){ h++; }
13103 if( zLine[h]==0 ) break;
13104 if( zLine[h]=='\'' || zLine[h]=='"' ){
13105 int delim = zLine[h++];
13106 azArg[nArg++] = &zLine[h];
13107 while( zLine[h] && zLine[h]!=delim ){
13108 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13111 if( zLine[h]==delim ){
13114 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13116 azArg[nArg++] = &zLine[h];
13117 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13118 if( zLine[h] ) zLine[h++] = 0;
13119 resolve_backslashes(azArg[nArg-1]);
13123 /* Process the input line.
13125 if( nArg==0 ) return 0; /* no tokens, no error */
13126 n = strlen30(azArg[0]);
13130 #ifndef SQLITE_OMIT_AUTHORIZATION
13131 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13133 raw_printf(stderr, "Usage: .auth ON|OFF\n");
13135 goto meta_command_exit;
13138 if( booleanValue(azArg[1]) ){
13139 sqlite3_set_authorizer(p->db, shellAuth, p);
13141 sqlite3_set_authorizer(p->db, 0, 0);
13146 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13147 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13149 rc = arDotCommand(p, 0, azArg, nArg);
13153 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13154 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13156 const char *zDestFile = 0;
13157 const char *zDb = 0;
13159 sqlite3_backup *pBackup;
13161 const char *zVfs = 0;
13162 for(j=1; j<nArg; j++){
13163 const char *z = azArg[j];
13165 if( z[1]=='-' ) z++;
13166 if( strcmp(z, "-append")==0 ){
13170 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13173 }else if( zDestFile==0 ){
13174 zDestFile = azArg[j];
13175 }else if( zDb==0 ){
13177 zDestFile = azArg[j];
13179 raw_printf(stderr, "Usage: .backup ?DB? ?--append? FILENAME\n");
13183 if( zDestFile==0 ){
13184 raw_printf(stderr, "missing FILENAME argument on .backup\n");
13187 if( zDb==0 ) zDb = "main";
13188 rc = sqlite3_open_v2(zDestFile, &pDest,
13189 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13190 if( rc!=SQLITE_OK ){
13191 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13196 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13198 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13202 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13203 sqlite3_backup_finish(pBackup);
13204 if( rc==SQLITE_DONE ){
13207 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13213 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13215 bail_on_error = booleanValue(azArg[1]);
13217 raw_printf(stderr, "Usage: .bail on|off\n");
13222 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13224 if( booleanValue(azArg[1]) ){
13225 setBinaryMode(p->out, 1);
13227 setTextMode(p->out, 1);
13230 raw_printf(stderr, "Usage: .binary on|off\n");
13235 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13237 #if defined(_WIN32) || defined(WIN32)
13238 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13239 rc = !SetCurrentDirectoryW(z);
13242 rc = chdir(azArg[1]);
13245 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13249 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13254 /* The undocumented ".breakpoint" command causes a call to the no-op
13255 ** routine named test_breakpoint().
13257 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13261 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13263 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13265 raw_printf(stderr, "Usage: .changes on|off\n");
13270 /* Cancel output redirection, if it is currently set (by .testcase)
13271 ** Then read the content of the testcase-out.txt file and compare against
13272 ** azArg[1]. If there are differences, report an error and exit.
13274 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13278 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13280 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13281 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13283 }else if( testcase_glob(azArg[1],zRes)==0 ){
13284 utf8_printf(stderr,
13285 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
13286 p->zTestcase, azArg[1], zRes);
13289 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13292 sqlite3_free(zRes);
13295 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13297 tryToClone(p, azArg[1]);
13299 raw_printf(stderr, "Usage: .clone FILENAME\n");
13304 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13308 memcpy(&data, p, sizeof(data));
13309 data.showHeader = 0;
13310 data.cMode = data.mode = MODE_List;
13311 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13313 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13314 callback, &data, &zErrMsg);
13316 utf8_printf(stderr,"Error: %s\n", zErrMsg);
13317 sqlite3_free(zErrMsg);
13322 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13323 static const struct DbConfigChoices {const char *zName; int op;} aDbConfig[] = {
13324 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13325 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13326 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13327 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13328 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13329 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13330 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13331 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13335 for(ii=0; ii<ArraySize(aDbConfig); ii++){
13336 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13338 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13340 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13341 utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13342 if( nArg>1 ) break;
13344 if( nArg>1 && ii==ArraySize(aDbConfig) ){
13345 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13346 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13350 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13351 rc = shell_dbinfo_command(p, nArg, azArg);
13354 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13355 const char *zLike = 0;
13357 int savedShowHeader = p->showHeader;
13358 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
13359 for(i=1; i<nArg; i++){
13360 if( azArg[i][0]=='-' ){
13361 const char *z = azArg[i]+1;
13362 if( z[0]=='-' ) z++;
13363 if( strcmp(z,"preserve-rowids")==0 ){
13364 #ifdef SQLITE_OMIT_VIRTUALTABLE
13365 raw_printf(stderr, "The --preserve-rowids option is not compatible"
13366 " with SQLITE_OMIT_VIRTUALTABLE\n");
13368 goto meta_command_exit;
13370 ShellSetFlag(p, SHFLG_PreserveRowid);
13373 if( strcmp(z,"newlines")==0 ){
13374 ShellSetFlag(p, SHFLG_Newlines);
13377 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
13379 goto meta_command_exit;
13382 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
13383 "?--newlines? ?LIKE-PATTERN?\n");
13385 goto meta_command_exit;
13391 /* When playing back a "dump", the content might appear in an order
13392 ** which causes immediate foreign key constraints to be violated.
13393 ** So disable foreign-key constraint enforcement to prevent problems. */
13394 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
13395 raw_printf(p->out, "BEGIN TRANSACTION;\n");
13396 p->writableSchema = 0;
13398 /* Set writable_schema=ON since doing so forces SQLite to initialize
13399 ** as much of the schema as it can even if the sqlite_master table is
13401 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
13404 run_schema_dump_query(p,
13405 "SELECT name, type, sql FROM sqlite_master "
13406 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
13408 run_schema_dump_query(p,
13409 "SELECT name, type, sql FROM sqlite_master "
13410 "WHERE name=='sqlite_sequence'"
13412 run_table_dump_query(p,
13413 "SELECT sql FROM sqlite_master "
13414 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
13418 zSql = sqlite3_mprintf(
13419 "SELECT name, type, sql FROM sqlite_master "
13420 "WHERE tbl_name LIKE %Q AND type=='table'"
13421 " AND sql NOT NULL", zLike);
13422 run_schema_dump_query(p,zSql);
13423 sqlite3_free(zSql);
13424 zSql = sqlite3_mprintf(
13425 "SELECT sql FROM sqlite_master "
13426 "WHERE sql NOT NULL"
13427 " AND type IN ('index','trigger','view')"
13428 " AND tbl_name LIKE %Q", zLike);
13429 run_table_dump_query(p, zSql, 0);
13430 sqlite3_free(zSql);
13432 if( p->writableSchema ){
13433 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
13434 p->writableSchema = 0;
13436 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13437 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
13438 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
13439 p->showHeader = savedShowHeader;
13442 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
13444 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
13446 raw_printf(stderr, "Usage: .echo on|off\n");
13451 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13453 p->autoEQPtest = 0;
13454 if( strcmp(azArg[1],"full")==0 ){
13455 p->autoEQP = AUTOEQP_full;
13456 }else if( strcmp(azArg[1],"trigger")==0 ){
13457 p->autoEQP = AUTOEQP_trigger;
13458 }else if( strcmp(azArg[1],"test")==0 ){
13459 p->autoEQP = AUTOEQP_on;
13460 p->autoEQPtest = 1;
13462 p->autoEQP = (u8)booleanValue(azArg[1]);
13465 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13470 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
13471 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
13475 /* The ".explain" command is automatic now. It is largely pointless. It
13476 ** retained purely for backwards compatibility */
13477 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
13480 if( strcmp(azArg[1],"auto")==0 ){
13483 val = booleanValue(azArg[1]);
13486 if( val==1 && p->mode!=MODE_Explain ){
13487 p->normalMode = p->mode;
13488 p->mode = MODE_Explain;
13489 p->autoExplain = 0;
13490 }else if( val==0 ){
13491 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13492 p->autoExplain = 0;
13493 }else if( val==99 ){
13494 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13495 p->autoExplain = 1;
13499 #ifndef SQLITE_OMIT_VIRTUALTABLE
13500 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
13502 expertDotCommand(p, azArg, nArg);
13506 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
13510 memcpy(&data, p, sizeof(data));
13511 data.showHeader = 0;
13512 data.cMode = data.mode = MODE_Semi;
13513 if( nArg==2 && optionMatch(azArg[1], "indent") ){
13514 data.cMode = data.mode = MODE_Pretty;
13518 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
13520 goto meta_command_exit;
13523 rc = sqlite3_exec(p->db,
13525 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
13526 " FROM sqlite_master UNION ALL"
13527 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
13528 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
13530 callback, &data, &zErrMsg
13532 if( rc==SQLITE_OK ){
13533 sqlite3_stmt *pStmt;
13534 rc = sqlite3_prepare_v2(p->db,
13535 "SELECT rowid FROM sqlite_master"
13536 " WHERE name GLOB 'sqlite_stat[134]'",
13538 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
13539 sqlite3_finalize(pStmt);
13542 raw_printf(p->out, "/* No STAT tables available */\n");
13544 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13545 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
13546 callback, &data, &zErrMsg);
13547 data.cMode = data.mode = MODE_Insert;
13548 data.zDestTable = "sqlite_stat1";
13549 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
13550 data.zDestTable = "sqlite_stat3";
13551 shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
13552 data.zDestTable = "sqlite_stat4";
13553 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
13554 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13558 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
13560 p->showHeader = booleanValue(azArg[1]);
13562 raw_printf(stderr, "Usage: .headers on|off\n");
13567 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
13568 utf8_printf(p->out, "%s", zHelp);
13571 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
13572 char *zTable; /* Insert data into this table */
13573 char *zFile; /* Name of file to extra content from */
13574 sqlite3_stmt *pStmt = NULL; /* A statement */
13575 int nCol; /* Number of columns in the table */
13576 int nByte; /* Number of bytes in an SQL string */
13577 int i, j; /* Loop counters */
13578 int needCommit; /* True to COMMIT or ROLLBACK at end */
13579 int nSep; /* Number of bytes in p->colSeparator[] */
13580 char *zSql; /* An SQL statement */
13581 ImportCtx sCtx; /* Reader context */
13582 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
13583 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
13586 raw_printf(stderr, "Usage: .import FILE TABLE\n");
13587 goto meta_command_exit;
13592 memset(&sCtx, 0, sizeof(sCtx));
13594 nSep = strlen30(p->colSeparator);
13597 "Error: non-null column separator required for import\n");
13601 raw_printf(stderr, "Error: multi-character column separators not allowed"
13605 nSep = strlen30(p->rowSeparator);
13607 raw_printf(stderr, "Error: non-null row separator required for import\n");
13610 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
13611 /* When importing CSV (only), if the row separator is set to the
13612 ** default output row separator, change it to the default input
13613 ** row separator. This avoids having to maintain different input
13614 ** and output row separators. */
13615 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13616 nSep = strlen30(p->rowSeparator);
13619 raw_printf(stderr, "Error: multi-character row separators not allowed"
13623 sCtx.zFile = zFile;
13625 if( sCtx.zFile[0]=='|' ){
13626 #ifdef SQLITE_OMIT_POPEN
13627 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13630 sCtx.in = popen(sCtx.zFile+1, "r");
13631 sCtx.zFile = "<pipe>";
13635 sCtx.in = fopen(sCtx.zFile, "rb");
13638 if( p->mode==MODE_Ascii ){
13639 xRead = ascii_read_one_field;
13641 xRead = csv_read_one_field;
13644 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
13647 sCtx.cColSep = p->colSeparator[0];
13648 sCtx.cRowSep = p->rowSeparator[0];
13649 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
13652 shell_out_of_memory();
13654 nByte = strlen30(zSql);
13655 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13656 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
13657 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
13658 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
13660 while( xRead(&sCtx) ){
13661 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
13663 if( sCtx.cTerm!=sCtx.cColSep ) break;
13666 sqlite3_free(zCreate);
13667 sqlite3_free(sCtx.z);
13669 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
13672 zCreate = sqlite3_mprintf("%z\n)", zCreate);
13673 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
13674 sqlite3_free(zCreate);
13676 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
13677 sqlite3_errmsg(p->db));
13678 sqlite3_free(sCtx.z);
13682 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13684 sqlite3_free(zSql);
13686 if (pStmt) sqlite3_finalize(pStmt);
13687 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
13691 nCol = sqlite3_column_count(pStmt);
13692 sqlite3_finalize(pStmt);
13694 if( nCol==0 ) return 0; /* no columns, no error */
13695 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
13698 shell_out_of_memory();
13700 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
13701 j = strlen30(zSql);
13702 for(i=1; i<nCol; i++){
13708 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13709 sqlite3_free(zSql);
13711 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13712 if (pStmt) sqlite3_finalize(pStmt);
13716 needCommit = sqlite3_get_autocommit(p->db);
13717 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
13719 int startLine = sCtx.nLine;
13720 for(i=0; i<nCol; i++){
13721 char *z = xRead(&sCtx);
13723 ** Did we reach end-of-file before finding any columns?
13724 ** If so, stop instead of NULL filling the remaining columns.
13726 if( z==0 && i==0 ) break;
13728 ** Did we reach end-of-file OR end-of-line before finding any
13729 ** columns in ASCII mode? If so, stop instead of NULL filling
13730 ** the remaining columns.
13732 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
13733 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
13734 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
13735 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13736 "filling the rest with NULL\n",
13737 sCtx.zFile, startLine, nCol, i+1);
13739 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
13742 if( sCtx.cTerm==sCtx.cColSep ){
13746 }while( sCtx.cTerm==sCtx.cColSep );
13747 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13748 "extras ignored\n",
13749 sCtx.zFile, startLine, nCol, i);
13752 sqlite3_step(pStmt);
13753 rc = sqlite3_reset(pStmt);
13754 if( rc!=SQLITE_OK ){
13755 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
13756 startLine, sqlite3_errmsg(p->db));
13759 }while( sCtx.cTerm!=EOF );
13762 sqlite3_free(sCtx.z);
13763 sqlite3_finalize(pStmt);
13764 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
13767 #ifndef SQLITE_UNTESTABLE
13768 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
13770 char *zCollist = 0;
13771 sqlite3_stmt *pStmt;
13774 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
13775 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
13776 " .imposter off\n");
13778 goto meta_command_exit;
13782 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
13783 goto meta_command_exit;
13785 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
13786 " WHERE name='%q' AND type='index'", azArg[1]);
13787 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13788 sqlite3_free(zSql);
13789 if( sqlite3_step(pStmt)==SQLITE_ROW ){
13790 tnum = sqlite3_column_int(pStmt, 0);
13792 sqlite3_finalize(pStmt);
13794 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
13796 goto meta_command_exit;
13798 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
13799 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13800 sqlite3_free(zSql);
13802 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13804 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
13807 if( sqlite3_column_int(pStmt,1)==-1 ){
13810 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
13815 zCollist = sqlite3_mprintf("\"%w\"", zCol);
13817 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
13820 sqlite3_finalize(pStmt);
13821 zSql = sqlite3_mprintf(
13822 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
13823 azArg[2], zCollist, zCollist);
13824 sqlite3_free(zCollist);
13825 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
13826 if( rc==SQLITE_OK ){
13827 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
13828 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
13830 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
13832 utf8_printf(stdout, "%s;\n", zSql);
13834 "WARNING: writing to an imposter table will corrupt the index!\n"
13838 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
13841 sqlite3_free(zSql);
13843 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
13845 #ifdef SQLITE_ENABLE_IOTRACE
13846 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
13847 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
13848 if( iotrace && iotrace!=stdout ) fclose(iotrace);
13851 sqlite3IoTrace = 0;
13852 }else if( strcmp(azArg[1], "-")==0 ){
13853 sqlite3IoTrace = iotracePrintf;
13856 iotrace = fopen(azArg[1], "w");
13858 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
13859 sqlite3IoTrace = 0;
13862 sqlite3IoTrace = iotracePrintf;
13868 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
13869 static const struct {
13870 const char *zLimitName; /* Name of a limit */
13871 int limitCode; /* Integer code for that limit */
13873 { "length", SQLITE_LIMIT_LENGTH },
13874 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
13875 { "column", SQLITE_LIMIT_COLUMN },
13876 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
13877 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
13878 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
13879 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
13880 { "attached", SQLITE_LIMIT_ATTACHED },
13881 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
13882 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
13883 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
13884 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
13889 for(i=0; i<ArraySize(aLimit); i++){
13890 printf("%20s %d\n", aLimit[i].zLimitName,
13891 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
13893 }else if( nArg>3 ){
13894 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
13896 goto meta_command_exit;
13899 n2 = strlen30(azArg[1]);
13900 for(i=0; i<ArraySize(aLimit); i++){
13901 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
13905 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
13907 goto meta_command_exit;
13912 utf8_printf(stderr, "unknown limit: \"%s\"\n"
13913 "enter \".limits\" with no arguments for a list.\n",
13916 goto meta_command_exit;
13919 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
13920 (int)integerValue(azArg[2]));
13922 printf("%20s %d\n", aLimit[iLimit].zLimitName,
13923 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
13927 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
13929 lintDotCommand(p, azArg, nArg);
13932 #ifndef SQLITE_OMIT_LOAD_EXTENSION
13933 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
13934 const char *zFile, *zProc;
13937 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
13939 goto meta_command_exit;
13942 zProc = nArg>=3 ? azArg[2] : 0;
13944 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
13945 if( rc!=SQLITE_OK ){
13946 utf8_printf(stderr, "Error: %s\n", zErrMsg);
13947 sqlite3_free(zErrMsg);
13953 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
13955 raw_printf(stderr, "Usage: .log FILENAME\n");
13958 const char *zFile = azArg[1];
13959 output_file_close(p->pLog);
13960 p->pLog = output_file_open(zFile, 0);
13964 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
13965 const char *zMode = nArg>=2 ? azArg[1] : "";
13966 int n2 = strlen30(zMode);
13968 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
13969 p->mode = MODE_Line;
13970 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13971 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
13972 p->mode = MODE_Column;
13973 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13974 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
13975 p->mode = MODE_List;
13976 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
13977 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13978 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
13979 p->mode = MODE_Html;
13980 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
13981 p->mode = MODE_Tcl;
13982 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
13983 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13984 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
13985 p->mode = MODE_Csv;
13986 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13987 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13988 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
13989 p->mode = MODE_List;
13990 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
13991 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
13992 p->mode = MODE_Insert;
13993 set_table_name(p, nArg>=3 ? azArg[2] : "table");
13994 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
13995 p->mode = MODE_Quote;
13996 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
13997 p->mode = MODE_Ascii;
13998 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
13999 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14000 }else if( nArg==1 ){
14001 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14003 raw_printf(stderr, "Error: mode should be one of: "
14004 "ascii column csv html insert line list quote tabs tcl\n");
14007 p->cMode = p->mode;
14010 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14012 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14013 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14015 raw_printf(stderr, "Usage: .nullvalue STRING\n");
14020 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14021 char *zNewFilename; /* Name of the database file to open */
14022 int iName = 1; /* Index in azArg[] of the filename */
14023 int newFlag = 0; /* True to delete file before opening */
14024 /* Close the existing database */
14025 session_close_all(p);
14028 p->zDbFilename = 0;
14029 sqlite3_free(p->zFreeOnClose);
14030 p->zFreeOnClose = 0;
14031 p->openMode = SHELL_OPEN_UNSPEC;
14032 /* Check for command-line arguments */
14033 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14034 const char *z = azArg[iName];
14035 if( optionMatch(z,"new") ){
14037 #ifdef SQLITE_HAVE_ZLIB
14038 }else if( optionMatch(z, "zip") ){
14039 p->openMode = SHELL_OPEN_ZIPFILE;
14041 }else if( optionMatch(z, "append") ){
14042 p->openMode = SHELL_OPEN_APPENDVFS;
14043 }else if( optionMatch(z, "readonly") ){
14044 p->openMode = SHELL_OPEN_READONLY;
14045 }else if( z[0]=='-' ){
14046 utf8_printf(stderr, "unknown option: %s\n", z);
14048 goto meta_command_exit;
14051 /* If a filename is specified, try to open it first */
14052 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14053 if( zNewFilename ){
14054 if( newFlag ) shellDeleteFile(zNewFilename);
14055 p->zDbFilename = zNewFilename;
14056 open_db(p, OPEN_DB_KEEPALIVE);
14058 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14059 sqlite3_free(zNewFilename);
14061 p->zFreeOnClose = zNewFilename;
14065 /* As a fall-back open a TEMP database */
14066 p->zDbFilename = 0;
14072 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14073 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14075 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14077 if( azArg[0][0]=='e' ){
14078 /* Transform the ".excel" command into ".once -x" */
14081 zFile = azArg[1] = "-x";
14085 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14087 goto meta_command_exit;
14089 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14091 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14093 goto meta_command_exit;
14100 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14101 #ifndef SQLITE_NOHAVE_SYSTEM
14102 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14105 if( zFile[1]=='x' ){
14106 newTempFile(p, "csv");
14107 p->mode = MODE_Csv;
14108 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14109 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14111 newTempFile(p, "txt");
14114 zFile = p->zTempFile;
14116 #endif /* SQLITE_NOHAVE_SYSTEM */
14117 if( zFile[0]=='|' ){
14118 #ifdef SQLITE_OMIT_POPEN
14119 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14123 p->out = popen(zFile + 1, "w");
14125 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14129 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14133 p->out = output_file_open(zFile, bTxtMode);
14135 if( strcmp(zFile,"off")!=0 ){
14136 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14141 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14146 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14148 for(i=1; i<nArg; i++){
14149 if( i>1 ) raw_printf(p->out, " ");
14150 utf8_printf(p->out, "%s", azArg[i]);
14152 raw_printf(p->out, "\n");
14155 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14157 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14160 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14164 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14168 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14171 raw_printf(stderr, "Usage: .read FILE\n");
14173 goto meta_command_exit;
14175 alt = fopen(azArg[1], "rb");
14177 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
14180 rc = process_input(p, alt);
14185 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
14186 const char *zSrcFile;
14189 sqlite3_backup *pBackup;
14193 zSrcFile = azArg[1];
14195 }else if( nArg==3 ){
14196 zSrcFile = azArg[2];
14199 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
14201 goto meta_command_exit;
14203 rc = sqlite3_open(zSrcFile, &pSrc);
14204 if( rc!=SQLITE_OK ){
14205 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14210 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14212 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14216 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14217 || rc==SQLITE_BUSY ){
14218 if( rc==SQLITE_BUSY ){
14219 if( nTimeout++ >= 3 ) break;
14220 sqlite3_sleep(100);
14223 sqlite3_backup_finish(pBackup);
14224 if( rc==SQLITE_DONE ){
14226 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
14227 raw_printf(stderr, "Error: source database is busy\n");
14230 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14236 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14238 p->scanstatsOn = (u8)booleanValue(azArg[1]);
14239 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
14240 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
14243 raw_printf(stderr, "Usage: .scanstats on|off\n");
14248 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
14252 const char *zDiv = "(";
14253 const char *zName = 0;
14259 memcpy(&data, p, sizeof(data));
14260 data.showHeader = 0;
14261 data.cMode = data.mode = MODE_Semi;
14262 initText(&sSelect);
14263 for(ii=1; ii<nArg; ii++){
14264 if( optionMatch(azArg[ii],"indent") ){
14265 data.cMode = data.mode = MODE_Pretty;
14266 }else if( optionMatch(azArg[ii],"debug") ){
14268 }else if( zName==0 ){
14271 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
14273 goto meta_command_exit;
14277 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14278 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14279 char *new_argv[2], *new_colv[2];
14280 new_argv[0] = sqlite3_mprintf(
14281 "CREATE TABLE %s (\n"
14284 " tbl_name text,\n"
14285 " rootpage integer,\n"
14287 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
14289 new_colv[0] = "sql";
14291 callback(&data, 1, new_argv, new_colv);
14292 sqlite3_free(new_argv[0]);
14296 sqlite3_stmt *pStmt = 0;
14297 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
14300 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14301 sqlite3_finalize(pStmt);
14303 goto meta_command_exit;
14305 appendText(&sSelect, "SELECT sql FROM", 0);
14307 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14308 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
14310 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
14311 appendText(&sSelect, zDiv, 0);
14312 zDiv = " UNION ALL ";
14313 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
14314 if( sqlite3_stricmp(zDb, "main")!=0 ){
14315 appendText(&sSelect, zDb, '"');
14317 appendText(&sSelect, "NULL", 0);
14319 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
14320 appendText(&sSelect, zScNum, 0);
14321 appendText(&sSelect, " AS snum, ", 0);
14322 appendText(&sSelect, zDb, '\'');
14323 appendText(&sSelect, " AS sname FROM ", 0);
14324 appendText(&sSelect, zDb, '"');
14325 appendText(&sSelect, ".sqlite_master", 0);
14327 sqlite3_finalize(pStmt);
14328 #ifdef SQLITE_INTROSPECTION_PRAGMAS
14330 appendText(&sSelect,
14331 " UNION ALL SELECT shell_module_schema(name),"
14332 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
14335 appendText(&sSelect, ") WHERE ", 0);
14337 char *zQarg = sqlite3_mprintf("%Q", zName);
14338 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14339 strchr(zName, '[') != 0;
14340 if( strchr(zName, '.') ){
14341 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14343 appendText(&sSelect, "lower(tbl_name)", 0);
14345 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14346 appendText(&sSelect, zQarg, 0);
14348 appendText(&sSelect, " ESCAPE '\\' ", 0);
14350 appendText(&sSelect, " AND ", 0);
14351 sqlite3_free(zQarg);
14353 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14354 " ORDER BY snum, rowid", 0);
14356 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
14358 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
14360 freeText(&sSelect);
14363 utf8_printf(stderr,"Error: %s\n", zErrMsg);
14364 sqlite3_free(zErrMsg);
14366 }else if( rc != SQLITE_OK ){
14367 raw_printf(stderr,"Error: querying schema information\n");
14374 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
14375 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
14376 sqlite3SelectTrace = (int)integerValue(azArg[1]);
14380 #if defined(SQLITE_ENABLE_SESSION)
14381 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
14382 OpenSession *pSession = &p->aSession[0];
14383 char **azCmd = &azArg[1];
14385 int nCmd = nArg - 1;
14387 if( nArg<=1 ) goto session_syntax_error;
14390 for(iSes=0; iSes<p->nSession; iSes++){
14391 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
14393 if( iSes<p->nSession ){
14394 pSession = &p->aSession[iSes];
14398 pSession = &p->aSession[0];
14403 /* .session attach TABLE
14404 ** Invoke the sqlite3session_attach() interface to attach a particular
14405 ** table so that it is never filtered.
14407 if( strcmp(azCmd[0],"attach")==0 ){
14408 if( nCmd!=2 ) goto session_syntax_error;
14409 if( pSession->p==0 ){
14411 raw_printf(stderr, "ERROR: No sessions are open\n");
14413 rc = sqlite3session_attach(pSession->p, azCmd[1]);
14415 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
14421 /* .session changeset FILE
14422 ** .session patchset FILE
14423 ** Write a changeset or patchset into a file. The file is overwritten.
14425 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
14427 if( nCmd!=2 ) goto session_syntax_error;
14428 if( pSession->p==0 ) goto session_not_open;
14429 out = fopen(azCmd[1], "wb");
14431 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
14435 if( azCmd[0][0]=='c' ){
14436 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
14438 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
14441 printf("Error: error code %d\n", rc);
14445 && fwrite(pChng, szChng, 1, out)!=1 ){
14446 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
14449 sqlite3_free(pChng);
14455 ** Close the identified session
14457 if( strcmp(azCmd[0], "close")==0 ){
14458 if( nCmd!=1 ) goto session_syntax_error;
14460 session_close(pSession);
14461 p->aSession[iSes] = p->aSession[--p->nSession];
14465 /* .session enable ?BOOLEAN?
14466 ** Query or set the enable flag
14468 if( strcmp(azCmd[0], "enable")==0 ){
14470 if( nCmd>2 ) goto session_syntax_error;
14471 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14473 ii = sqlite3session_enable(pSession->p, ii);
14474 utf8_printf(p->out, "session %s enable flag = %d\n",
14475 pSession->zName, ii);
14479 /* .session filter GLOB ....
14480 ** Set a list of GLOB patterns of table names to be excluded.
14482 if( strcmp(azCmd[0], "filter")==0 ){
14484 if( nCmd<2 ) goto session_syntax_error;
14486 for(ii=0; ii<pSession->nFilter; ii++){
14487 sqlite3_free(pSession->azFilter[ii]);
14489 sqlite3_free(pSession->azFilter);
14490 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
14491 pSession->azFilter = sqlite3_malloc( nByte );
14492 if( pSession->azFilter==0 ){
14493 raw_printf(stderr, "Error: out or memory\n");
14496 for(ii=1; ii<nCmd; ii++){
14497 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
14499 pSession->nFilter = ii-1;
14503 /* .session indirect ?BOOLEAN?
14504 ** Query or set the indirect flag
14506 if( strcmp(azCmd[0], "indirect")==0 ){
14508 if( nCmd>2 ) goto session_syntax_error;
14509 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14511 ii = sqlite3session_indirect(pSession->p, ii);
14512 utf8_printf(p->out, "session %s indirect flag = %d\n",
14513 pSession->zName, ii);
14517 /* .session isempty
14518 ** Determine if the session is empty
14520 if( strcmp(azCmd[0], "isempty")==0 ){
14522 if( nCmd!=1 ) goto session_syntax_error;
14524 ii = sqlite3session_isempty(pSession->p);
14525 utf8_printf(p->out, "session %s isempty flag = %d\n",
14526 pSession->zName, ii);
14531 ** List all currently open sessions
14533 if( strcmp(azCmd[0],"list")==0 ){
14534 for(i=0; i<p->nSession; i++){
14535 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
14539 /* .session open DB NAME
14540 ** Open a new session called NAME on the attached database DB.
14541 ** DB is normally "main".
14543 if( strcmp(azCmd[0],"open")==0 ){
14545 if( nCmd!=3 ) goto session_syntax_error;
14547 if( zName[0]==0 ) goto session_syntax_error;
14548 for(i=0; i<p->nSession; i++){
14549 if( strcmp(p->aSession[i].zName,zName)==0 ){
14550 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
14551 goto meta_command_exit;
14554 if( p->nSession>=ArraySize(p->aSession) ){
14555 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
14556 goto meta_command_exit;
14558 pSession = &p->aSession[p->nSession];
14559 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
14561 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
14563 goto meta_command_exit;
14565 pSession->nFilter = 0;
14566 sqlite3session_table_filter(pSession->p, session_filter, pSession);
14568 pSession->zName = sqlite3_mprintf("%s", zName);
14570 /* If no command name matches, show a syntax error */
14571 session_syntax_error:
14576 #ifdef SQLITE_DEBUG
14577 /* Undocumented commands for internal testing. Subject to change
14578 ** without notice. */
14579 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
14580 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
14582 for(i=1; i<nArg; i++){
14583 v = booleanValue(azArg[i]);
14584 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
14587 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
14588 int i; sqlite3_int64 v;
14589 for(i=1; i<nArg; i++){
14591 v = integerValue(azArg[i]);
14592 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
14593 utf8_printf(p->out, "%s", zBuf);
14599 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
14600 int bIsInit = 0; /* True to initialize the SELFTEST table */
14601 int bVerbose = 0; /* Verbose output */
14602 int bSelftestExists; /* True if SELFTEST already exists */
14603 int i, k; /* Loop counters */
14604 int nTest = 0; /* Number of tests runs */
14605 int nErr = 0; /* Number of errors seen */
14606 ShellText str; /* Answer for a query */
14607 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
14610 for(i=1; i<nArg; i++){
14611 const char *z = azArg[i];
14612 if( z[0]=='-' && z[1]=='-' ) z++;
14613 if( strcmp(z,"-init")==0 ){
14616 if( strcmp(z,"-v")==0 ){
14620 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14621 azArg[i], azArg[0]);
14622 raw_printf(stderr, "Should be one of: --init -v\n");
14624 goto meta_command_exit;
14627 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
14629 bSelftestExists = 0;
14631 bSelftestExists = 1;
14634 createSelftestTable(p);
14635 bSelftestExists = 1;
14638 appendText(&str, "x", 0);
14639 for(k=bSelftestExists; k>=0; k--){
14641 rc = sqlite3_prepare_v2(p->db,
14642 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
14645 rc = sqlite3_prepare_v2(p->db,
14646 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
14647 " (1,'run','PRAGMA integrity_check','ok')",
14651 raw_printf(stderr, "Error querying the selftest table\n");
14653 sqlite3_finalize(pStmt);
14654 goto meta_command_exit;
14656 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
14657 int tno = sqlite3_column_int(pStmt, 0);
14658 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
14659 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
14660 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
14664 char *zQuote = sqlite3_mprintf("%q", zSql);
14665 printf("%d: %s %s\n", tno, zOp, zSql);
14666 sqlite3_free(zQuote);
14668 if( strcmp(zOp,"memo")==0 ){
14669 utf8_printf(p->out, "%s\n", zSql);
14671 if( strcmp(zOp,"run")==0 ){
14675 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
14678 utf8_printf(p->out, "Result: %s\n", str.z);
14680 if( rc || zErrMsg ){
14683 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
14684 sqlite3_free(zErrMsg);
14685 }else if( strcmp(zAns,str.z)!=0 ){
14688 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
14689 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
14693 utf8_printf(stderr,
14694 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
14698 } /* End loop over rows of content from SELFTEST */
14699 sqlite3_finalize(pStmt);
14700 } /* End loop over k */
14702 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
14705 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
14706 if( nArg<2 || nArg>3 ){
14707 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
14711 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
14712 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
14715 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
14716 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
14720 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
14721 const char *zLike = 0; /* Which table to checksum. 0 means everything */
14722 int i; /* Loop counter */
14723 int bSchema = 0; /* Also hash the schema */
14724 int bSeparate = 0; /* Hash each table separately */
14725 int iSize = 224; /* Hash algorithm to use */
14726 int bDebug = 0; /* Only show the query that would have run */
14727 sqlite3_stmt *pStmt; /* For querying tables names */
14728 char *zSql; /* SQL to be run */
14729 char *zSep; /* Separator */
14730 ShellText sSql; /* Complete SQL for the query to run the hash */
14731 ShellText sQuery; /* Set of queries used to read all content */
14733 for(i=1; i<nArg; i++){
14734 const char *z = azArg[i];
14737 if( z[0]=='-' ) z++;
14738 if( strcmp(z,"schema")==0 ){
14741 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
14742 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
14744 iSize = atoi(&z[5]);
14746 if( strcmp(z,"debug")==0 ){
14750 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14751 azArg[i], azArg[0]);
14752 raw_printf(stderr, "Should be one of: --schema"
14753 " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
14755 goto meta_command_exit;
14758 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
14760 goto meta_command_exit;
14764 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
14768 zSql = "SELECT lower(name) FROM sqlite_master"
14769 " WHERE type='table' AND coalesce(rootpage,0)>1"
14770 " UNION ALL SELECT 'sqlite_master'"
14771 " ORDER BY 1 collate nocase";
14773 zSql = "SELECT lower(name) FROM sqlite_master"
14774 " WHERE type='table' AND coalesce(rootpage,0)>1"
14775 " AND name NOT LIKE 'sqlite_%'"
14776 " ORDER BY 1 collate nocase";
14778 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14781 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
14783 while( SQLITE_ROW==sqlite3_step(pStmt) ){
14784 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
14785 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
14786 if( strncmp(zTab, "sqlite_",7)!=0 ){
14787 appendText(&sQuery,"SELECT * FROM ", 0);
14788 appendText(&sQuery,zTab,'"');
14789 appendText(&sQuery," NOT INDEXED;", 0);
14790 }else if( strcmp(zTab, "sqlite_master")==0 ){
14791 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
14792 " ORDER BY name;", 0);
14793 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
14794 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
14795 " ORDER BY name;", 0);
14796 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
14797 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
14798 " ORDER BY tbl,idx;", 0);
14799 }else if( strcmp(zTab, "sqlite_stat3")==0
14800 || strcmp(zTab, "sqlite_stat4")==0 ){
14801 appendText(&sQuery, "SELECT * FROM ", 0);
14802 appendText(&sQuery, zTab, 0);
14803 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
14805 appendText(&sSql, zSep, 0);
14806 appendText(&sSql, sQuery.z, '\'');
14808 appendText(&sSql, ",", 0);
14809 appendText(&sSql, zTab, '\'');
14812 sqlite3_finalize(pStmt);
14814 zSql = sqlite3_mprintf(
14816 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
14817 " FROM [sha3sum$query]",
14820 zSql = sqlite3_mprintf(
14822 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
14823 " FROM [sha3sum$query]",
14829 utf8_printf(p->out, "%s\n", zSql);
14831 shell_exec(p, zSql, 0);
14833 sqlite3_free(zSql);
14836 #ifndef SQLITE_NOHAVE_SYSTEM
14838 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
14843 raw_printf(stderr, "Usage: .system COMMAND\n");
14845 goto meta_command_exit;
14847 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
14848 for(i=2; i<nArg; i++){
14849 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
14853 sqlite3_free(zCmd);
14854 if( x ) raw_printf(stderr, "System command returns %d\n", x);
14856 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
14858 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14859 static const char *azBool[] = { "off", "on", "trigger", "full"};
14862 raw_printf(stderr, "Usage: .show\n");
14864 goto meta_command_exit;
14866 utf8_printf(p->out, "%12.12s: %s\n","echo",
14867 azBool[ShellHasFlag(p, SHFLG_Echo)]);
14868 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
14869 utf8_printf(p->out, "%12.12s: %s\n","explain",
14870 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
14871 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
14872 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
14873 utf8_printf(p->out, "%12.12s: ", "nullvalue");
14874 output_c_string(p->out, p->nullValue);
14875 raw_printf(p->out, "\n");
14876 utf8_printf(p->out,"%12.12s: %s\n","output",
14877 strlen30(p->outfile) ? p->outfile : "stdout");
14878 utf8_printf(p->out,"%12.12s: ", "colseparator");
14879 output_c_string(p->out, p->colSeparator);
14880 raw_printf(p->out, "\n");
14881 utf8_printf(p->out,"%12.12s: ", "rowseparator");
14882 output_c_string(p->out, p->rowSeparator);
14883 raw_printf(p->out, "\n");
14884 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
14885 utf8_printf(p->out, "%12.12s: ", "width");
14886 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
14887 raw_printf(p->out, "%d ", p->colWidth[i]);
14889 raw_printf(p->out, "\n");
14890 utf8_printf(p->out, "%12.12s: %s\n", "filename",
14891 p->zDbFilename ? p->zDbFilename : "");
14894 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14896 p->statsOn = (u8)booleanValue(azArg[1]);
14897 }else if( nArg==1 ){
14898 display_stats(p->db, p, 0);
14900 raw_printf(stderr, "Usage: .stats ?on|off?\n");
14905 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
14906 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
14907 || strncmp(azArg[0], "indexes", n)==0) )
14909 sqlite3_stmt *pStmt;
14916 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14918 sqlite3_finalize(pStmt);
14919 return shellDatabaseError(p->db);
14922 if( nArg>2 && c=='i' ){
14923 /* It is an historical accident that the .indexes command shows an error
14924 ** when called with the wrong number of arguments whereas the .tables
14925 ** command does not. */
14926 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14928 sqlite3_finalize(pStmt);
14929 goto meta_command_exit;
14931 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14932 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14933 if( zDbName==0 ) continue;
14934 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
14935 if( sqlite3_stricmp(zDbName, "main")==0 ){
14936 appendText(&s, "SELECT name FROM ", 0);
14938 appendText(&s, "SELECT ", 0);
14939 appendText(&s, zDbName, '\'');
14940 appendText(&s, "||'.'||name FROM ", 0);
14942 appendText(&s, zDbName, '"');
14943 appendText(&s, ".sqlite_master ", 0);
14945 appendText(&s," WHERE type IN ('table','view')"
14946 " AND name NOT LIKE 'sqlite_%'"
14947 " AND name LIKE ?1", 0);
14949 appendText(&s," WHERE type='index'"
14950 " AND tbl_name LIKE ?1", 0);
14953 rc = sqlite3_finalize(pStmt);
14954 appendText(&s, " ORDER BY 1", 0);
14955 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
14957 if( rc ) return shellDatabaseError(p->db);
14959 /* Run the SQL statement prepared by the above block. Store the results
14960 ** as an array of nul-terminated strings in azResult[]. */
14964 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
14966 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
14968 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14969 if( nRow>=nAlloc ){
14971 int n2 = nAlloc*2 + 10;
14972 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
14973 if( azNew==0 ) shell_out_of_memory();
14977 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
14978 if( 0==azResult[nRow] ) shell_out_of_memory();
14981 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
14982 rc = shellDatabaseError(p->db);
14985 /* Pretty-print the contents of array azResult[] to the output */
14986 if( rc==0 && nRow>0 ){
14987 int len, maxlen = 0;
14989 int nPrintCol, nPrintRow;
14990 for(i=0; i<nRow; i++){
14991 len = strlen30(azResult[i]);
14992 if( len>maxlen ) maxlen = len;
14994 nPrintCol = 80/(maxlen+2);
14995 if( nPrintCol<1 ) nPrintCol = 1;
14996 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
14997 for(i=0; i<nPrintRow; i++){
14998 for(j=i; j<nRow; j+=nPrintRow){
14999 char *zSp = j<nPrintRow ? "" : " ";
15000 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15001 azResult[j] ? azResult[j]:"");
15003 raw_printf(p->out, "\n");
15007 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15008 sqlite3_free(azResult);
15011 /* Begin redirecting output to the file "testcase-out.txt" */
15012 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15014 p->out = output_file_open("testcase-out.txt", 0);
15016 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15019 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15021 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15025 #ifndef SQLITE_UNTESTABLE
15026 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15027 static const struct {
15028 const char *zCtrlName; /* Name of a test-control option */
15029 int ctrlCode; /* Integer code for that option */
15030 const char *zUsage; /* Usage notes */
15032 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
15033 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
15034 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
15035 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
15036 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
15037 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
15038 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
15039 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
15040 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
15041 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
15043 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
15045 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
15046 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" },
15047 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
15048 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
15049 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" },
15053 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
15056 const char *zCmd = 0;
15059 zCmd = nArg>=2 ? azArg[1] : "help";
15061 /* The argument can optionally begin with "-" or "--" */
15062 if( zCmd[0]=='-' && zCmd[1] ){
15064 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15067 /* --help lists all test-controls */
15068 if( strcmp(zCmd,"help")==0 ){
15069 utf8_printf(p->out, "Available test-controls:\n");
15070 for(i=0; i<ArraySize(aCtrl); i++){
15071 utf8_printf(p->out, " .testctrl %s %s\n",
15072 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15075 goto meta_command_exit;
15078 /* convert testctrl text option to value. allow any unique prefix
15079 ** of the option name, or a numerical value. */
15080 n2 = strlen30(zCmd);
15081 for(i=0; i<ArraySize(aCtrl); i++){
15082 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15084 testctrl = aCtrl[i].ctrlCode;
15087 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15088 "Use \".testctrl --help\" for help\n", zCmd);
15090 goto meta_command_exit;
15095 utf8_printf(stderr,"Error: unknown test-control: %s\n"
15096 "Use \".testctrl --help\" for help\n", zCmd);
15100 /* sqlite3_test_control(int, db, int) */
15101 case SQLITE_TESTCTRL_OPTIMIZATIONS:
15102 case SQLITE_TESTCTRL_RESERVE:
15104 int opt = (int)strtol(azArg[2], 0, 0);
15105 rc2 = sqlite3_test_control(testctrl, p->db, opt);
15110 /* sqlite3_test_control(int) */
15111 case SQLITE_TESTCTRL_PRNG_SAVE:
15112 case SQLITE_TESTCTRL_PRNG_RESTORE:
15113 case SQLITE_TESTCTRL_PRNG_RESET:
15114 case SQLITE_TESTCTRL_BYTEORDER:
15116 rc2 = sqlite3_test_control(testctrl);
15117 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15121 /* sqlite3_test_control(int, uint) */
15122 case SQLITE_TESTCTRL_PENDING_BYTE:
15124 unsigned int opt = (unsigned int)integerValue(azArg[2]);
15125 rc2 = sqlite3_test_control(testctrl, opt);
15130 /* sqlite3_test_control(int, int) */
15131 case SQLITE_TESTCTRL_ASSERT:
15132 case SQLITE_TESTCTRL_ALWAYS:
15134 int opt = booleanValue(azArg[2]);
15135 rc2 = sqlite3_test_control(testctrl, opt);
15140 /* sqlite3_test_control(int, int) */
15141 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15142 case SQLITE_TESTCTRL_NEVER_CORRUPT:
15144 int opt = booleanValue(azArg[2]);
15145 rc2 = sqlite3_test_control(testctrl, opt);
15150 case SQLITE_TESTCTRL_IMPOSTER:
15152 rc2 = sqlite3_test_control(testctrl, p->db,
15154 integerValue(azArg[3]),
15155 integerValue(azArg[4]));
15161 case SQLITE_TESTCTRL_PARSER_COVERAGE:
15163 sqlite3_test_control(testctrl, p->out);
15169 if( isOk==0 && iCtrl>=0 ){
15170 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
15172 }else if( isOk==1 ){
15173 raw_printf(p->out, "%d\n", rc2);
15174 }else if( isOk==2 ){
15175 raw_printf(p->out, "0x%08x\n", rc2);
15178 #endif /* !defined(SQLITE_UNTESTABLE) */
15180 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
15182 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
15185 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
15187 enableTimer = booleanValue(azArg[1]);
15188 if( enableTimer && !HAS_TIMER ){
15189 raw_printf(stderr, "Error: timer not available on this system.\n");
15193 raw_printf(stderr, "Usage: .timer on|off\n");
15198 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
15201 raw_printf(stderr, "Usage: .trace FILE|off\n");
15203 goto meta_command_exit;
15205 output_file_close(p->traceOut);
15206 p->traceOut = output_file_open(azArg[1], 0);
15207 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
15208 if( p->traceOut==0 ){
15209 sqlite3_trace_v2(p->db, 0, 0, 0);
15211 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
15216 #if SQLITE_USER_AUTHENTICATION
15217 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
15219 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
15221 goto meta_command_exit;
15224 if( strcmp(azArg[1],"login")==0 ){
15226 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
15228 goto meta_command_exit;
15230 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
15232 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
15235 }else if( strcmp(azArg[1],"add")==0 ){
15237 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
15239 goto meta_command_exit;
15241 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15242 booleanValue(azArg[4]));
15244 raw_printf(stderr, "User-Add failed: %d\n", rc);
15247 }else if( strcmp(azArg[1],"edit")==0 ){
15249 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
15251 goto meta_command_exit;
15253 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15254 booleanValue(azArg[4]));
15256 raw_printf(stderr, "User-Edit failed: %d\n", rc);
15259 }else if( strcmp(azArg[1],"delete")==0 ){
15261 raw_printf(stderr, "Usage: .user delete USER\n");
15263 goto meta_command_exit;
15265 rc = sqlite3_user_delete(p->db, azArg[2]);
15267 raw_printf(stderr, "User-Delete failed: %d\n", rc);
15271 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
15273 goto meta_command_exit;
15276 #endif /* SQLITE_USER_AUTHENTICATION */
15278 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
15279 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
15280 sqlite3_libversion(), sqlite3_sourceid());
15281 #if SQLITE_HAVE_ZLIB
15282 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
15284 #define CTIMEOPT_VAL_(opt) #opt
15285 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
15286 #if defined(__clang__) && defined(__clang_major__)
15287 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
15288 CTIMEOPT_VAL(__clang_minor__) "."
15289 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
15290 #elif defined(_MSC_VER)
15291 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
15292 #elif defined(__GNUC__) && defined(__VERSION__)
15293 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
15297 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
15298 const char *zDbName = nArg==2 ? azArg[1] : "main";
15299 sqlite3_vfs *pVfs = 0;
15301 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
15303 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
15304 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15305 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15306 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15311 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
15313 sqlite3_vfs *pCurrent = 0;
15315 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
15317 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
15318 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
15319 pVfs==pCurrent ? " <--- CURRENT" : "");
15320 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15321 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15322 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15324 raw_printf(p->out, "-----------------------------------\n");
15329 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
15330 const char *zDbName = nArg==2 ? azArg[1] : "main";
15331 char *zVfsName = 0;
15333 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
15335 utf8_printf(p->out, "%s\n", zVfsName);
15336 sqlite3_free(zVfsName);
15341 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
15342 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
15343 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
15347 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
15349 assert( nArg<=ArraySize(azArg) );
15350 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
15351 p->colWidth[j-1] = (int)integerValue(azArg[j]);
15356 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
15357 " \"%s\". Enter \".help\" for help\n", azArg[0]);
15364 if( p->outCount==0 ) output_reset(p);
15370 ** Return TRUE if a semicolon occurs anywhere in the first N characters
15373 static int line_contains_semicolon(const char *z, int N){
15375 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
15380 ** Test to see if a line consists entirely of whitespace.
15382 static int _all_whitespace(const char *z){
15384 if( IsSpace(z[0]) ) continue;
15385 if( *z=='/' && z[1]=='*' ){
15387 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
15388 if( *z==0 ) return 0;
15392 if( *z=='-' && z[1]=='-' ){
15394 while( *z && *z!='\n' ){ z++; }
15395 if( *z==0 ) return 1;
15404 ** Return TRUE if the line typed in is an SQL command terminator other
15405 ** than a semi-colon. The SQL Server style "go" command is understood
15406 ** as is the Oracle "/".
15408 static int line_is_command_terminator(const char *zLine){
15409 while( IsSpace(zLine[0]) ){ zLine++; };
15410 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
15411 return 1; /* Oracle */
15413 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
15414 && _all_whitespace(&zLine[2]) ){
15415 return 1; /* SQL Server */
15421 ** We need a default sqlite3_complete() implementation to use in case
15422 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
15423 ** any arbitrary text is a complete SQL statement. This is not very
15424 ** user-friendly, but it does seem to work.
15426 #ifdef SQLITE_OMIT_COMPLETE
15427 int sqlite3_complete(const char *zSql){ return 1; }
15431 ** Return true if zSql is a complete SQL statement. Return false if it
15432 ** ends in the middle of a string literal or C-style comment.
15434 static int line_is_complete(char *zSql, int nSql){
15436 if( zSql==0 ) return 1;
15439 rc = sqlite3_complete(zSql);
15445 ** Run a single line of SQL. Return the number of errors.
15447 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
15452 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
15454 rc = shell_exec(p, zSql, &zErrMsg);
15456 if( rc || zErrMsg ){
15458 if( in!=0 || !stdin_is_interactive ){
15459 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
15460 "Error: near line %d:", startline);
15462 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
15465 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
15466 sqlite3_free(zErrMsg);
15469 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
15472 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
15473 raw_printf(p->out, "changes: %3d total_changes: %d\n",
15474 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
15481 ** Read input from *in and process it. If *in==0 then input
15482 ** is interactive - the user is typing it it. Otherwise, input
15483 ** is coming from a file or device. A prompt is issued and history
15484 ** is saved only if input is interactive. An interrupt signal will
15485 ** cause this routine to exit immediately, unless input is interactive.
15487 ** Return the number of errors.
15489 static int process_input(ShellState *p, FILE *in){
15490 char *zLine = 0; /* A single input line */
15491 char *zSql = 0; /* Accumulated SQL text */
15492 int nLine; /* Length of current line */
15493 int nSql = 0; /* Bytes of zSql[] used */
15494 int nAlloc = 0; /* Allocated zSql[] space */
15495 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
15496 int rc; /* Error code */
15497 int errCnt = 0; /* Number of errors seen */
15498 int lineno = 0; /* Current line number */
15499 int startline = 0; /* Line number for start of current input */
15501 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
15503 zLine = one_input_line(in, zLine, nSql>0);
15506 if( in==0 && stdin_is_interactive ) printf("\n");
15509 if( seenInterrupt ){
15514 if( nSql==0 && _all_whitespace(zLine) ){
15515 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15518 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
15519 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15520 if( zLine[0]=='.' ){
15521 rc = do_meta_command(zLine, p);
15522 if( rc==2 ){ /* exit requested */
15530 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
15531 memcpy(zLine,";",2);
15533 nLine = strlen30(zLine);
15534 if( nSql+nLine+2>=nAlloc ){
15535 nAlloc = nSql+nLine+100;
15536 zSql = realloc(zSql, nAlloc);
15537 if( zSql==0 ) shell_out_of_memory();
15542 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
15543 assert( nAlloc>0 && zSql!=0 );
15544 memcpy(zSql, zLine+i, nLine+1-i);
15545 startline = lineno;
15548 zSql[nSql++] = '\n';
15549 memcpy(zSql+nSql, zLine, nLine+1);
15552 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
15553 && sqlite3_complete(zSql) ){
15554 errCnt += runOneSqlLine(p, zSql, in, startline);
15562 }else if( nSql && _all_whitespace(zSql) ){
15563 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
15567 if( nSql && !_all_whitespace(zSql) ){
15568 errCnt += runOneSqlLine(p, zSql, in, startline);
15576 ** Return a pathname which is the user's home directory. A
15577 ** 0 return indicates an error of some kind.
15579 static char *find_home_dir(int clearFlag){
15580 static char *home_dir = NULL;
15586 if( home_dir ) return home_dir;
15588 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
15589 && !defined(__RTP__) && !defined(_WRS_KERNEL)
15591 struct passwd *pwent;
15592 uid_t uid = getuid();
15593 if( (pwent=getpwuid(uid)) != NULL) {
15594 home_dir = pwent->pw_dir;
15599 #if defined(_WIN32_WCE)
15600 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
15605 #if defined(_WIN32) || defined(WIN32)
15607 home_dir = getenv("USERPROFILE");
15612 home_dir = getenv("HOME");
15615 #if defined(_WIN32) || defined(WIN32)
15617 char *zDrive, *zPath;
15619 zDrive = getenv("HOMEDRIVE");
15620 zPath = getenv("HOMEPATH");
15621 if( zDrive && zPath ){
15622 n = strlen30(zDrive) + strlen30(zPath) + 1;
15623 home_dir = malloc( n );
15624 if( home_dir==0 ) return 0;
15625 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
15632 #endif /* !_WIN32_WCE */
15635 int n = strlen30(home_dir) + 1;
15636 char *z = malloc( n );
15637 if( z ) memcpy(z, home_dir, n);
15645 ** Read input from the file given by sqliterc_override. Or if that
15646 ** parameter is NULL, take input from ~/.sqliterc
15648 ** Returns the number of errors.
15650 static void process_sqliterc(
15651 ShellState *p, /* Configuration data */
15652 const char *sqliterc_override /* Name of config file. NULL to use default */
15654 char *home_dir = NULL;
15655 const char *sqliterc = sqliterc_override;
15659 if (sqliterc == NULL) {
15660 home_dir = find_home_dir(0);
15662 raw_printf(stderr, "-- warning: cannot find home directory;"
15663 " cannot read ~/.sqliterc\n");
15666 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
15669 in = fopen(sqliterc,"rb");
15671 if( stdin_is_interactive ){
15672 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
15674 process_input(p,in);
15677 sqlite3_free(zBuf);
15681 ** Show available command line options
15683 static const char zOptions[] =
15684 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15685 " -A ARGS... run \".archive ARGS\" and exit\n"
15687 " -append append the database to the end of the file\n"
15688 " -ascii set output mode to 'ascii'\n"
15689 " -bail stop after hitting an error\n"
15690 " -batch force batch I/O\n"
15691 " -column set output mode to 'column'\n"
15692 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
15693 " -csv set output mode to 'csv'\n"
15694 " -echo print commands before execution\n"
15695 " -init FILENAME read/process named file\n"
15696 " -[no]header turn headers on or off\n"
15697 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15698 " -heap SIZE Size of heap for memsys3 or memsys5\n"
15700 " -help show this message\n"
15701 " -html set output mode to HTML\n"
15702 " -interactive force interactive I/O\n"
15703 " -line set output mode to 'line'\n"
15704 " -list set output mode to 'list'\n"
15705 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
15706 " -mmap N default mmap size set to N\n"
15707 #ifdef SQLITE_ENABLE_MULTIPLEX
15708 " -multiplex enable the multiplexor VFS\n"
15710 " -newline SEP set output row separator. Default: '\\n'\n"
15711 " -nullvalue TEXT set text string for NULL values. Default ''\n"
15712 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
15713 " -quote set output mode to 'quote'\n"
15714 " -readonly open the database read-only\n"
15715 " -separator SEP set output column separator. Default: '|'\n"
15716 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
15717 " -sorterref SIZE sorter references threshold size\n"
15719 " -stats print memory stats before each finalize\n"
15720 " -version show SQLite version\n"
15721 " -vfs NAME use NAME as the default VFS\n"
15722 #ifdef SQLITE_ENABLE_VFSTRACE
15723 " -vfstrace enable tracing of all VFS calls\n"
15725 #ifdef SQLITE_HAVE_ZLIB
15726 " -zip open the file as a ZIP Archive\n"
15729 static void usage(int showDetail){
15730 utf8_printf(stderr,
15731 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
15732 "FILENAME is the name of an SQLite database. A new database is created\n"
15733 "if the file does not previously exist.\n", Argv0);
15735 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
15737 raw_printf(stderr, "Use the -help option for additional information\n");
15743 ** Internal check: Verify that the SQLite is uninitialized. Print a
15744 ** error message if it is initialized.
15746 static void verify_uninitialized(void){
15747 if( sqlite3_config(-1)==SQLITE_MISUSE ){
15748 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
15749 " initialization.\n");
15754 ** Initialize the state information in data
15756 static void main_init(ShellState *data) {
15757 memset(data, 0, sizeof(*data));
15758 data->normalMode = data->cMode = data->mode = MODE_List;
15759 data->autoExplain = 1;
15760 memcpy(data->colSeparator,SEP_Column, 2);
15761 memcpy(data->rowSeparator,SEP_Row, 2);
15762 data->showHeader = 0;
15763 data->shellFlgs = SHFLG_Lookaside;
15764 verify_uninitialized();
15765 sqlite3_config(SQLITE_CONFIG_URI, 1);
15766 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
15767 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
15768 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
15769 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
15773 ** Output text to the console in a font that attracts extra attention.
15776 static void printBold(const char *zText){
15777 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
15778 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
15779 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
15780 SetConsoleTextAttribute(out,
15781 FOREGROUND_RED|FOREGROUND_INTENSITY
15783 printf("%s", zText);
15784 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
15787 static void printBold(const char *zText){
15788 printf("\033[1m%s\033[0m", zText);
15793 ** Get the argument to an --option. Throw an error and die if no argument
15796 static char *cmdline_option_value(int argc, char **argv, int i){
15798 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
15799 argv[0], argv[argc-1]);
15805 #ifndef SQLITE_SHELL_IS_UTF8
15806 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
15807 # define SQLITE_SHELL_IS_UTF8 (0)
15809 # define SQLITE_SHELL_IS_UTF8 (1)
15813 #if SQLITE_SHELL_IS_UTF8
15814 int SQLITE_CDECL main(int argc, char **argv){
15816 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
15821 const char *zInitFile = 0;
15824 int warnInmemoryDb = 0;
15828 const char *zVfs = 0; /* Value of -vfs command-line option */
15829 #if !SQLITE_SHELL_IS_UTF8
15830 char **argvToFree = 0;
15831 int argcToFree = 0;
15834 setBinaryMode(stdin, 0);
15835 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
15836 stdin_is_interactive = isatty(0);
15837 stdout_is_console = isatty(1);
15839 #if USE_SYSTEM_SQLITE+0!=1
15840 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
15841 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
15842 sqlite3_sourceid(), SQLITE_SOURCE_ID);
15848 /* On Windows, we must translate command-line arguments into UTF-8.
15849 ** The SQLite memory allocator subsystem has to be enabled in order to
15850 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
15851 ** subsequent sqlite3_config() calls will work. So copy all results into
15852 ** memory that does not come from the SQLite memory allocator.
15854 #if !SQLITE_SHELL_IS_UTF8
15855 sqlite3_initialize();
15856 argvToFree = malloc(sizeof(argv[0])*argc*2);
15858 argv = argvToFree + argc;
15859 if( argv==0 ) shell_out_of_memory();
15860 for(i=0; i<argc; i++){
15861 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
15863 if( z==0 ) shell_out_of_memory();
15864 n = (int)strlen(z);
15865 argv[i] = malloc( n+1 );
15866 if( argv[i]==0 ) shell_out_of_memory();
15867 memcpy(argv[i], z, n+1);
15868 argvToFree[i] = argv[i];
15871 sqlite3_shutdown();
15874 assert( argc>=1 && argv && argv[0] );
15877 /* Make sure we have a valid signal handler early, before anything
15881 signal(SIGINT, interrupt_handler);
15882 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
15883 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
15886 #ifdef SQLITE_SHELL_DBNAME_PROC
15888 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
15889 ** of a C-function that will provide the name of the database file. Use
15890 ** this compile-time option to embed this shell program in larger
15891 ** applications. */
15892 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
15893 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
15894 warnInmemoryDb = 0;
15898 /* Do an initial pass through the command-line argument to locate
15899 ** the name of the database file, the name of the initialization file,
15900 ** the size of the alternative malloc heap,
15901 ** and the first command to execute.
15903 verify_uninitialized();
15904 for(i=1; i<argc; i++){
15908 if( data.zDbFilename==0 ){
15909 data.zDbFilename = z;
15911 /* Excesss arguments are interpreted as SQL (or dot-commands) and
15912 ** mean that nothing is read from stdin */
15915 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
15916 if( azCmd==0 ) shell_out_of_memory();
15920 if( z[1]=='-' ) z++;
15921 if( strcmp(z,"-separator")==0
15922 || strcmp(z,"-nullvalue")==0
15923 || strcmp(z,"-newline")==0
15924 || strcmp(z,"-cmd")==0
15926 (void)cmdline_option_value(argc, argv, ++i);
15927 }else if( strcmp(z,"-init")==0 ){
15928 zInitFile = cmdline_option_value(argc, argv, ++i);
15929 }else if( strcmp(z,"-batch")==0 ){
15930 /* Need to check for batch mode here to so we can avoid printing
15931 ** informational messages (like from process_sqliterc) before
15932 ** we do the actual processing of arguments later in a second pass.
15934 stdin_is_interactive = 0;
15935 }else if( strcmp(z,"-heap")==0 ){
15936 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15938 sqlite3_int64 szHeap;
15940 zSize = cmdline_option_value(argc, argv, ++i);
15941 szHeap = integerValue(zSize);
15942 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
15943 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
15945 (void)cmdline_option_value(argc, argv, ++i);
15947 }else if( strcmp(z,"-pagecache")==0 ){
15949 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15950 if( sz>70000 ) sz = 70000;
15952 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15953 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
15954 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
15955 data.shellFlgs |= SHFLG_Pagecache;
15956 }else if( strcmp(z,"-lookaside")==0 ){
15958 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15960 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15962 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
15963 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
15964 #ifdef SQLITE_ENABLE_VFSTRACE
15965 }else if( strcmp(z,"-vfstrace")==0 ){
15966 extern int vfstrace_register(
15967 const char *zTraceName,
15968 const char *zOldVfsName,
15969 int (*xOut)(const char*,void*),
15973 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
15975 #ifdef SQLITE_ENABLE_MULTIPLEX
15976 }else if( strcmp(z,"-multiplex")==0 ){
15977 extern int sqlite3_multiple_initialize(const char*,int);
15978 sqlite3_multiplex_initialize(0, 1);
15980 }else if( strcmp(z,"-mmap")==0 ){
15981 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15982 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
15983 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
15984 }else if( strcmp(z,"-sorterref")==0 ){
15985 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15986 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
15988 }else if( strcmp(z,"-vfs")==0 ){
15989 zVfs = cmdline_option_value(argc, argv, ++i);
15990 #ifdef SQLITE_HAVE_ZLIB
15991 }else if( strcmp(z,"-zip")==0 ){
15992 data.openMode = SHELL_OPEN_ZIPFILE;
15994 }else if( strcmp(z,"-append")==0 ){
15995 data.openMode = SHELL_OPEN_APPENDVFS;
15996 }else if( strcmp(z,"-readonly")==0 ){
15997 data.openMode = SHELL_OPEN_READONLY;
15998 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15999 }else if( strncmp(z, "-A",2)==0 ){
16000 /* All remaining command-line arguments are passed to the ".archive"
16001 ** command, so ignore them */
16006 verify_uninitialized();
16009 #ifdef SQLITE_SHELL_INIT_PROC
16011 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16012 ** of a C-function that will perform initialization actions on SQLite that
16013 ** occur just before or after sqlite3_initialize(). Use this compile-time
16014 ** option to embed this shell program in larger applications. */
16015 extern void SQLITE_SHELL_INIT_PROC(void);
16016 SQLITE_SHELL_INIT_PROC();
16019 /* All the sqlite3_config() calls have now been made. So it is safe
16020 ** to call sqlite3_initialize() and process any command line -vfs option. */
16021 sqlite3_initialize();
16025 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16027 sqlite3_vfs_register(pVfs, 1);
16029 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16034 if( data.zDbFilename==0 ){
16035 #ifndef SQLITE_OMIT_MEMORYDB
16036 data.zDbFilename = ":memory:";
16037 warnInmemoryDb = argc==1;
16039 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16044 sqlite3_appendvfs_init(0,0,0);
16046 /* Go ahead and open the database file if it already exists. If the
16047 ** file does not exist, delay opening it. This prevents empty database
16048 ** files from being created if a user mistypes the database name argument
16049 ** to the sqlite command-line tool.
16051 if( access(data.zDbFilename, 0)==0 ){
16055 /* Process the initialization file if there is one. If no -init option
16056 ** is given on the command line, look for a file named ~/.sqliterc and
16057 ** try to process it.
16059 process_sqliterc(&data,zInitFile);
16061 /* Make a second pass through the command-line argument and set
16062 ** options. This second pass is delayed until after the initialization
16063 ** file is processed so that the command-line arguments will override
16064 ** settings in the initialization file.
16066 for(i=1; i<argc; i++){
16068 if( z[0]!='-' ) continue;
16069 if( z[1]=='-' ){ z++; }
16070 if( strcmp(z,"-init")==0 ){
16072 }else if( strcmp(z,"-html")==0 ){
16073 data.mode = MODE_Html;
16074 }else if( strcmp(z,"-list")==0 ){
16075 data.mode = MODE_List;
16076 }else if( strcmp(z,"-quote")==0 ){
16077 data.mode = MODE_Quote;
16078 }else if( strcmp(z,"-line")==0 ){
16079 data.mode = MODE_Line;
16080 }else if( strcmp(z,"-column")==0 ){
16081 data.mode = MODE_Column;
16082 }else if( strcmp(z,"-csv")==0 ){
16083 data.mode = MODE_Csv;
16084 memcpy(data.colSeparator,",",2);
16085 #ifdef SQLITE_HAVE_ZLIB
16086 }else if( strcmp(z,"-zip")==0 ){
16087 data.openMode = SHELL_OPEN_ZIPFILE;
16089 }else if( strcmp(z,"-append")==0 ){
16090 data.openMode = SHELL_OPEN_APPENDVFS;
16091 }else if( strcmp(z,"-readonly")==0 ){
16092 data.openMode = SHELL_OPEN_READONLY;
16093 }else if( strcmp(z,"-ascii")==0 ){
16094 data.mode = MODE_Ascii;
16095 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16097 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16099 }else if( strcmp(z,"-separator")==0 ){
16100 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16101 "%s",cmdline_option_value(argc,argv,++i));
16102 }else if( strcmp(z,"-newline")==0 ){
16103 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16104 "%s",cmdline_option_value(argc,argv,++i));
16105 }else if( strcmp(z,"-nullvalue")==0 ){
16106 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
16107 "%s",cmdline_option_value(argc,argv,++i));
16108 }else if( strcmp(z,"-header")==0 ){
16109 data.showHeader = 1;
16110 }else if( strcmp(z,"-noheader")==0 ){
16111 data.showHeader = 0;
16112 }else if( strcmp(z,"-echo")==0 ){
16113 ShellSetFlag(&data, SHFLG_Echo);
16114 }else if( strcmp(z,"-eqp")==0 ){
16115 data.autoEQP = AUTOEQP_on;
16116 }else if( strcmp(z,"-eqpfull")==0 ){
16117 data.autoEQP = AUTOEQP_full;
16118 }else if( strcmp(z,"-stats")==0 ){
16120 }else if( strcmp(z,"-scanstats")==0 ){
16121 data.scanstatsOn = 1;
16122 }else if( strcmp(z,"-backslash")==0 ){
16123 /* Undocumented command-line option: -backslash
16124 ** Causes C-style backslash escapes to be evaluated in SQL statements
16125 ** prior to sending the SQL into SQLite. Useful for injecting
16126 ** crazy bytes in the middle of SQL statements for testing and debugging.
16128 ShellSetFlag(&data, SHFLG_Backslash);
16129 }else if( strcmp(z,"-bail")==0 ){
16131 }else if( strcmp(z,"-version")==0 ){
16132 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
16134 }else if( strcmp(z,"-interactive")==0 ){
16135 stdin_is_interactive = 1;
16136 }else if( strcmp(z,"-batch")==0 ){
16137 stdin_is_interactive = 0;
16138 }else if( strcmp(z,"-heap")==0 ){
16140 }else if( strcmp(z,"-pagecache")==0 ){
16142 }else if( strcmp(z,"-lookaside")==0 ){
16144 }else if( strcmp(z,"-mmap")==0 ){
16146 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16147 }else if( strcmp(z,"-sorterref")==0 ){
16150 }else if( strcmp(z,"-vfs")==0 ){
16152 #ifdef SQLITE_ENABLE_VFSTRACE
16153 }else if( strcmp(z,"-vfstrace")==0 ){
16156 #ifdef SQLITE_ENABLE_MULTIPLEX
16157 }else if( strcmp(z,"-multiplex")==0 ){
16160 }else if( strcmp(z,"-help")==0 ){
16162 }else if( strcmp(z,"-cmd")==0 ){
16163 /* Run commands that follow -cmd first and separately from commands
16164 ** that simply appear on the command-line. This seems goofy. It would
16165 ** be better if all commands ran in the order that they appear. But
16166 ** we retain the goofy behavior for historical compatibility. */
16167 if( i==argc-1 ) break;
16168 z = cmdline_option_value(argc,argv,++i);
16170 rc = do_meta_command(z, &data);
16171 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
16174 rc = shell_exec(&data, z, &zErrMsg);
16176 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16177 if( bail_on_error ) return rc!=0 ? rc : 1;
16179 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
16180 if( bail_on_error ) return rc;
16183 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16184 }else if( strncmp(z, "-A", 2)==0 ){
16186 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16187 " with \"%s\"\n", z);
16190 open_db(&data, OPEN_DB_ZIPFILE);
16193 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16195 arDotCommand(&data, 1, argv+i, argc-i);
16201 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
16202 raw_printf(stderr,"Use -help for a list of options.\n");
16205 data.cMode = data.mode;
16209 /* Run all arguments that do not begin with '-' as if they were separate
16210 ** command-line inputs, except for the argToSkip argument which contains
16211 ** the database filename.
16213 for(i=0; i<nCmd; i++){
16214 if( azCmd[i][0]=='.' ){
16215 rc = do_meta_command(azCmd[i], &data);
16216 if( rc ) return rc==2 ? 0 : rc;
16219 rc = shell_exec(&data, azCmd[i], &zErrMsg);
16221 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16222 return rc!=0 ? rc : 1;
16224 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
16231 /* Run commands received from standard input
16233 if( stdin_is_interactive ){
16235 char *zHistory = 0;
16238 "SQLite version %s %.19s\n" /*extra-version-info*/
16239 "Enter \".help\" for usage hints.\n",
16240 sqlite3_libversion(), sqlite3_sourceid()
16242 if( warnInmemoryDb ){
16243 printf("Connected to a ");
16244 printBold("transient in-memory database");
16245 printf(".\nUse \".open FILENAME\" to reopen on a "
16246 "persistent database.\n");
16248 zHome = find_home_dir(0);
16250 nHistory = strlen30(zHome) + 20;
16251 if( (zHistory = malloc(nHistory))!=0 ){
16252 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
16255 if( zHistory ){ shell_read_history(zHistory); }
16256 #if HAVE_READLINE || HAVE_EDITLINE
16257 rl_attempted_completion_function = readline_completion;
16258 #elif HAVE_LINENOISE
16259 linenoiseSetCompletionCallback(linenoise_completion);
16261 rc = process_input(&data, 0);
16263 shell_stifle_history(2000);
16264 shell_write_history(zHistory);
16268 rc = process_input(&data, stdin);
16271 set_table_name(&data, 0);
16273 session_close_all(&data);
16276 sqlite3_free(data.zFreeOnClose);
16278 output_reset(&data);
16279 data.doXdgOpen = 0;
16280 clearTempFile(&data);
16281 #if !SQLITE_SHELL_IS_UTF8
16282 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16285 /* Clear the global data structure so that valgrind will detect memory
16287 memset(&data, 0, sizeof(data));