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 # define GETPID getpid
101 # if defined(__MINGW32__)
102 # define DIRENT dirent
104 # define S_ISLNK(mode) (0)
108 # define GETPID (int)GetCurrentProcessId
110 #include <sys/types.h>
111 #include <sys/stat.h>
114 # include <readline/readline.h>
115 # include <readline/history.h>
119 # include <editline/readline.h>
122 #if HAVE_EDITLINE || HAVE_READLINE
124 # define shell_add_history(X) add_history(X)
125 # define shell_read_history(X) read_history(X)
126 # define shell_write_history(X) write_history(X)
127 # define shell_stifle_history(X) stifle_history(X)
128 # define shell_readline(X) readline(X)
132 # include "linenoise.h"
133 # define shell_add_history(X) linenoiseHistoryAdd(X)
134 # define shell_read_history(X) linenoiseHistoryLoad(X)
135 # define shell_write_history(X) linenoiseHistorySave(X)
136 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
137 # define shell_readline(X) linenoise(X)
141 # define shell_read_history(X)
142 # define shell_write_history(X)
143 # define shell_stifle_history(X)
145 # define SHELL_USE_LOCAL_GETLINE 1
149 #if defined(_WIN32) || defined(WIN32)
152 # define isatty(h) _isatty(h)
154 # define access(f,m) _access((f),(m))
157 # define unlink _unlink
160 # define popen _popen
162 # define pclose _pclose
164 /* Make sure isatty() has a prototype. */
165 extern int isatty(int);
167 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
168 /* popen and pclose are not C89 functions and so are
169 ** sometimes omitted from the <stdio.h> header */
170 extern FILE *popen(const char*,const char*);
171 extern int pclose(FILE*);
173 # define SQLITE_OMIT_POPEN 1
177 #if defined(_WIN32_WCE)
178 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
179 * thus we always assume that we have a console. That can be
180 * overridden with the -batch command line option.
185 /* ctype macros that work with signed characters */
186 #define IsSpace(X) isspace((unsigned char)X)
187 #define IsDigit(X) isdigit((unsigned char)X)
188 #define ToLower(X) (char)tolower((unsigned char)X)
190 #if defined(_WIN32) || defined(WIN32)
193 /* string conversion routines only needed on Win32 */
194 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
195 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
196 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
197 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
200 /* On Windows, we normally run with output mode of TEXT so that \n characters
201 ** are automatically translated into \r\n. However, this behavior needs
202 ** to be disabled in some cases (ex: when generating CSV output and when
203 ** rendering quoted strings that contain \n characters). The following
204 ** routines take care of that.
206 #if defined(_WIN32) || defined(WIN32)
207 static void setBinaryMode(FILE *file, int isOutput){
208 if( isOutput ) fflush(file);
209 _setmode(_fileno(file), _O_BINARY);
211 static void setTextMode(FILE *file, int isOutput){
212 if( isOutput ) fflush(file);
213 _setmode(_fileno(file), _O_TEXT);
216 # define setBinaryMode(X,Y)
217 # define setTextMode(X,Y)
221 /* True if the timer is enabled */
222 static int enableTimer = 0;
224 /* Return the current wall-clock time */
225 static sqlite3_int64 timeOfDay(void){
226 static sqlite3_vfs *clockVfs = 0;
228 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
229 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
230 clockVfs->xCurrentTimeInt64(clockVfs, &t);
233 clockVfs->xCurrentTime(clockVfs, &r);
234 t = (sqlite3_int64)(r*86400000.0);
239 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
240 #include <sys/time.h>
241 #include <sys/resource.h>
243 /* VxWorks does not support getrusage() as far as we can determine */
244 #if defined(_WRS_KERNEL) || defined(__RTP__)
246 struct timeval ru_utime; /* user CPU time used */
247 struct timeval ru_stime; /* system CPU time used */
249 #define getrusage(A,B) memset(B,0,sizeof(*B))
252 /* Saved resource information for the beginning of an operation */
253 static struct rusage sBegin; /* CPU time at start */
254 static sqlite3_int64 iBegin; /* Wall-clock time at start */
257 ** Begin timing an operation
259 static void beginTimer(void){
261 getrusage(RUSAGE_SELF, &sBegin);
262 iBegin = timeOfDay();
266 /* Return the difference of two time_structs in seconds */
267 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
268 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
269 (double)(pEnd->tv_sec - pStart->tv_sec);
273 ** Print the timing results.
275 static void endTimer(void){
277 sqlite3_int64 iEnd = timeOfDay();
279 getrusage(RUSAGE_SELF, &sEnd);
280 printf("Run Time: real %.3f user %f sys %f\n",
281 (iEnd - iBegin)*0.001,
282 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
283 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
287 #define BEGIN_TIMER beginTimer()
288 #define END_TIMER endTimer()
291 #elif (defined(_WIN32) || defined(WIN32))
293 /* Saved resource information for the beginning of an operation */
294 static HANDLE hProcess;
295 static FILETIME ftKernelBegin;
296 static FILETIME ftUserBegin;
297 static sqlite3_int64 ftWallBegin;
298 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
299 LPFILETIME, LPFILETIME);
300 static GETPROCTIMES getProcessTimesAddr = NULL;
303 ** Check to see if we have timer support. Return 1 if necessary
304 ** support found (or found previously).
306 static int hasTimer(void){
307 if( getProcessTimesAddr ){
310 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
311 ** versions. See if the version we are running on has it, and if it
312 ** does, save off a pointer to it and the current process handle.
314 hProcess = GetCurrentProcess();
316 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
317 if( NULL != hinstLib ){
318 getProcessTimesAddr =
319 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
320 if( NULL != getProcessTimesAddr ){
323 FreeLibrary(hinstLib);
331 ** Begin timing an operation
333 static void beginTimer(void){
334 if( enableTimer && getProcessTimesAddr ){
335 FILETIME ftCreation, ftExit;
336 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
337 &ftKernelBegin,&ftUserBegin);
338 ftWallBegin = timeOfDay();
342 /* Return the difference of two FILETIME structs in seconds */
343 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
344 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
345 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
346 return (double) ((i64End - i64Start) / 10000000.0);
350 ** Print the timing results.
352 static void endTimer(void){
353 if( enableTimer && getProcessTimesAddr){
354 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
355 sqlite3_int64 ftWallEnd = timeOfDay();
356 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
357 printf("Run Time: real %.3f user %f sys %f\n",
358 (ftWallEnd - ftWallBegin)*0.001,
359 timeDiff(&ftUserBegin, &ftUserEnd),
360 timeDiff(&ftKernelBegin, &ftKernelEnd));
364 #define BEGIN_TIMER beginTimer()
365 #define END_TIMER endTimer()
366 #define HAS_TIMER hasTimer()
375 ** Used to prevent warnings about unused parameters
377 #define UNUSED_PARAMETER(x) (void)(x)
380 ** Number of elements in an array
382 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
385 ** If the following flag is set, then command execution stops
386 ** at an error if we are not interactive.
388 static int bail_on_error = 0;
391 ** Threat stdin as an interactive input if the following variable
392 ** is true. Otherwise, assume stdin is connected to a file or pipe.
394 static int stdin_is_interactive = 1;
397 ** On Windows systems we have to know if standard output is a console
398 ** in order to translate UTF-8 into MBCS. The following variable is
399 ** true if translation is required.
401 static int stdout_is_console = 1;
404 ** The following is the open SQLite database. We make a pointer
405 ** to this database a static variable so that it can be accessed
406 ** by the SIGINT handler to interrupt database processing.
408 static sqlite3 *globalDb = 0;
411 ** True if an interrupt (Control-C) has been received.
413 static volatile int seenInterrupt = 0;
416 ** This is the name of our program. It is set in main(), used
417 ** in a number of other places, mostly for error messages.
422 ** Prompt strings. Initialized in main. Settable with
423 ** .prompt main continue
425 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
426 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
429 ** Render output like fprintf(). Except, if the output is going to the
430 ** console and if this is running on a Windows machine, translate the
431 ** output from UTF-8 into MBCS.
433 #if defined(_WIN32) || defined(WIN32)
434 void utf8_printf(FILE *out, const char *zFormat, ...){
436 va_start(ap, zFormat);
437 if( stdout_is_console && (out==stdout || out==stderr) ){
438 char *z1 = sqlite3_vmprintf(zFormat, ap);
439 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
444 vfprintf(out, zFormat, ap);
448 #elif !defined(utf8_printf)
449 # define utf8_printf fprintf
453 ** Render output like fprintf(). This should not be used on anything that
454 ** includes string formatting (e.g. "%s").
456 #if !defined(raw_printf)
457 # define raw_printf fprintf
460 /* Indicate out-of-memory and exit. */
461 static void shell_out_of_memory(void){
462 raw_printf(stderr,"Error: out of memory\n");
467 ** Write I/O traces to the following stream.
469 #ifdef SQLITE_ENABLE_IOTRACE
470 static FILE *iotrace = 0;
474 ** This routine works like printf in that its first argument is a
475 ** format string and subsequent arguments are values to be substituted
476 ** in place of % fields. The result of formatting this string
477 ** is written to iotrace.
479 #ifdef SQLITE_ENABLE_IOTRACE
480 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
483 if( iotrace==0 ) return;
484 va_start(ap, zFormat);
485 z = sqlite3_vmprintf(zFormat, ap);
487 utf8_printf(iotrace, "%s", z);
493 ** Output string zUtf to stream pOut as w characters. If w is negative,
494 ** then right-justify the text. W is the width in UTF-8 characters, not
495 ** in bytes. This is different from the %*.*s specification in printf
496 ** since with %*.*s the width is measured in bytes, not characters.
498 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
501 int aw = w<0 ? -w : w;
503 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
504 for(i=n=0; zUtf[i]; i++){
505 if( (zUtf[i]&0xc0)!=0x80 ){
508 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
514 utf8_printf(pOut, "%.*s", i, zUtf);
516 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
518 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
524 ** Determines if a string is a number of not.
526 static int isNumber(const char *z, int *realnum){
527 if( *z=='-' || *z=='+' ) z++;
532 if( realnum ) *realnum = 0;
533 while( IsDigit(*z) ){ z++; }
536 if( !IsDigit(*z) ) return 0;
537 while( IsDigit(*z) ){ z++; }
538 if( realnum ) *realnum = 1;
540 if( *z=='e' || *z=='E' ){
542 if( *z=='+' || *z=='-' ) z++;
543 if( !IsDigit(*z) ) return 0;
544 while( IsDigit(*z) ){ z++; }
545 if( realnum ) *realnum = 1;
551 ** Compute a string length that is limited to what can be stored in
552 ** lower 30 bits of a 32-bit signed integer.
554 static int strlen30(const char *z){
556 while( *z2 ){ z2++; }
557 return 0x3fffffff & (int)(z2 - z);
561 ** Return the length of a string in characters. Multibyte UTF8 characters
562 ** count as a single character.
564 static int strlenChar(const char *z){
567 if( (0xc0&*(z++))!=0x80 ) n++;
573 ** This routine reads a line of text from FILE in, stores
574 ** the text in memory obtained from malloc() and returns a pointer
575 ** to the text. NULL is returned at end of file, or if malloc()
578 ** If zLine is not NULL then it is a malloced buffer returned from
579 ** a previous call to this routine that may be reused.
581 static char *local_getline(char *zLine, FILE *in){
582 int nLine = zLine==0 ? 0 : 100;
587 nLine = nLine*2 + 100;
588 zLine = realloc(zLine, nLine);
589 if( zLine==0 ) shell_out_of_memory();
591 if( fgets(&zLine[n], nLine - n, in)==0 ){
599 while( zLine[n] ) n++;
600 if( n>0 && zLine[n-1]=='\n' ){
602 if( n>0 && zLine[n-1]=='\r' ) n--;
607 #if defined(_WIN32) || defined(WIN32)
608 /* For interactive input on Windows systems, translate the
609 ** multi-byte characterset characters into UTF-8. */
610 if( stdin_is_interactive && in==stdin ){
611 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
613 int nTrans = strlen30(zTrans)+1;
615 zLine = realloc(zLine, nTrans);
616 if( zLine==0 ) shell_out_of_memory();
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);
763 if( p->z==0 ) shell_out_of_memory();
767 char *zCsr = p->z+p->n;
769 for(i=0; i<nAppend; i++){
770 *zCsr++ = zAppend[i];
771 if( zAppend[i]==quote ) *zCsr++ = quote;
774 p->n = (int)(zCsr - p->z);
777 memcpy(p->z+p->n, zAppend, nAppend);
784 ** Attempt to determine if identifier zName needs to be quoted, either
785 ** because it contains non-alphanumeric characters, or because it is an
786 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
787 ** that quoting is required.
789 ** Return '"' if quoting is required. Return 0 if no quoting is required.
791 static char quoteChar(const char *zName){
793 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
794 for(i=0; zName[i]; i++){
795 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
797 return sqlite3_keyword_check(zName, i) ? '"' : 0;
801 ** Construct a fake object name and column list to describe the structure
802 ** of the view, virtual table, or table valued function zSchema.zName.
804 static char *shellFakeSchema(
805 sqlite3 *db, /* The database connection containing the vtab */
806 const char *zSchema, /* Schema of the database holding the vtab */
807 const char *zName /* The name of the virtual table */
809 sqlite3_stmt *pStmt = 0;
816 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
817 zSchema ? zSchema : "main", zName);
818 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
822 cQuote = quoteChar(zSchema);
823 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
824 appendText(&s, zSchema, cQuote);
825 appendText(&s, ".", 0);
827 cQuote = quoteChar(zName);
828 appendText(&s, zName, cQuote);
829 while( sqlite3_step(pStmt)==SQLITE_ROW ){
830 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
832 appendText(&s, zDiv, 0);
834 cQuote = quoteChar(zCol);
835 appendText(&s, zCol, cQuote);
837 appendText(&s, ")", 0);
838 sqlite3_finalize(pStmt);
847 ** SQL function: shell_module_schema(X)
849 ** Return a fake schema for the table-valued function or eponymous virtual
852 static void shellModuleSchema(
853 sqlite3_context *pCtx,
855 sqlite3_value **apVal
857 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
858 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
859 UNUSED_PARAMETER(nVal);
861 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
868 ** SQL function: shell_add_schema(S,X)
870 ** Add the schema name X to the CREATE statement in S and return the result.
873 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
878 ** CREATE UNIQUE INDEX
881 ** CREATE VIRTUAL TABLE
883 ** This UDF is used by the .schema command to insert the schema name of
884 ** attached databases into the middle of the sqlite_master.sql field.
886 static void shellAddSchemaName(
887 sqlite3_context *pCtx,
889 sqlite3_value **apVal
891 static const char *aPrefix[] = {
900 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
901 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
902 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
903 sqlite3 *db = sqlite3_context_db_handle(pCtx);
904 UNUSED_PARAMETER(nVal);
905 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
906 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
907 int n = strlen30(aPrefix[i]);
908 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
912 char cQuote = quoteChar(zSchema);
913 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
914 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
916 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
920 && aPrefix[i][0]=='V'
921 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
924 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
926 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
931 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
937 sqlite3_result_value(pCtx, apVal[0]);
941 ** The source code for several run-time loadable extensions is inserted
942 ** below by the ../tool/mkshellc.tcl script. Before processing that included
943 ** code, we need to override some macros to make the included program code
944 ** work here in the middle of this regular program.
946 #define SQLITE_EXTENSION_INIT1
947 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
949 #if defined(_WIN32) && defined(_MSC_VER)
950 /************************* Begin test_windirent.h ******************/
954 ** The author disclaims copyright to this source code. In place of
955 ** a legal notice, here is a blessing:
957 ** May you do good and not evil.
958 ** May you find forgiveness for yourself and forgive others.
959 ** May you share freely, never taking more than you give.
961 *************************************************************************
962 ** This file contains declarations for most of the opendir() family of
963 ** POSIX functions on Win32 using the MSVCRT.
966 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
967 #define SQLITE_WINDIRENT_H
970 ** We need several data types from the Windows SDK header.
973 #ifndef WIN32_LEAN_AND_MEAN
974 #define WIN32_LEAN_AND_MEAN
980 ** We need several support functions from the SQLite core.
985 ** We need several things from the ANSI and MSVCRT headers.
993 #include <sys/types.h>
994 #include <sys/stat.h>
997 ** We may need several defines that should have been in "sys/stat.h".
1001 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1005 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1009 #define S_ISLNK(mode) (0)
1013 ** We may need to provide the "mode_t" type.
1016 #ifndef MODE_T_DEFINED
1017 #define MODE_T_DEFINED
1018 typedef unsigned short mode_t;
1022 ** We may need to provide the "ino_t" type.
1025 #ifndef INO_T_DEFINED
1026 #define INO_T_DEFINED
1027 typedef unsigned short ino_t;
1031 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1035 # ifdef FILENAME_MAX
1036 # define NAME_MAX (FILENAME_MAX)
1038 # define NAME_MAX (260)
1043 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1046 #ifndef NULL_INTPTR_T
1047 # define NULL_INTPTR_T ((intptr_t)(0))
1050 #ifndef BAD_INTPTR_T
1051 # define BAD_INTPTR_T ((intptr_t)(-1))
1055 ** We need to provide the necessary structures and related types.
1058 #ifndef DIRENT_DEFINED
1059 #define DIRENT_DEFINED
1060 typedef struct DIRENT DIRENT;
1061 typedef DIRENT *LPDIRENT;
1063 ino_t d_ino; /* Sequence number, do not use. */
1064 unsigned d_attributes; /* Win32 file attributes. */
1065 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1071 typedef struct DIR DIR;
1074 intptr_t d_handle; /* Value returned by "_findfirst". */
1075 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1076 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1081 ** Provide a macro, for use by the implementation, to determine if a
1082 ** particular directory entry should be skipped over when searching for
1083 ** the next directory entry that should be returned by the readdir() or
1084 ** readdir_r() functions.
1088 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1092 ** Provide the function prototype for the POSIX compatiable getenv()
1093 ** function. This function is not thread-safe.
1096 extern const char *windirent_getenv(const char *name);
1099 ** Finally, we can provide the function prototypes for the opendir(),
1100 ** readdir(), readdir_r(), and closedir() POSIX functions.
1103 extern LPDIR opendir(const char *dirname);
1104 extern LPDIRENT readdir(LPDIR dirp);
1105 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1106 extern INT closedir(LPDIR dirp);
1108 #endif /* defined(WIN32) && defined(_MSC_VER) */
1110 /************************* End test_windirent.h ********************/
1111 /************************* Begin test_windirent.c ******************/
1115 ** The author disclaims copyright to this source code. In place of
1116 ** a legal notice, here is a blessing:
1118 ** May you do good and not evil.
1119 ** May you find forgiveness for yourself and forgive others.
1120 ** May you share freely, never taking more than you give.
1122 *************************************************************************
1123 ** This file contains code to implement most of the opendir() family of
1124 ** POSIX functions on Win32 using the MSVCRT.
1127 #if defined(_WIN32) && defined(_MSC_VER)
1128 /* #include "test_windirent.h" */
1131 ** Implementation of the POSIX getenv() function using the Win32 API.
1132 ** This function is not thread-safe.
1134 const char *windirent_getenv(
1137 static char value[32768]; /* Maximum length, per MSDN */
1138 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1139 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1141 memset(value, 0, sizeof(value));
1142 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1143 if( dwRet==0 || dwRet>dwSize ){
1145 ** The function call to GetEnvironmentVariableA() failed -OR-
1146 ** the buffer is not large enough. Either way, return NULL.
1151 ** The function call to GetEnvironmentVariableA() succeeded
1152 ** -AND- the buffer contains the entire value.
1159 ** Implementation of the POSIX opendir() function using the MSVCRT.
1164 struct _finddata_t data;
1165 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1166 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1168 if( dirp==NULL ) return NULL;
1169 memset(dirp, 0, sizeof(DIR));
1171 /* TODO: Remove this if Unix-style root paths are not used. */
1172 if( sqlite3_stricmp(dirname, "/")==0 ){
1173 dirname = windirent_getenv("SystemDrive");
1176 memset(&data, 0, sizeof(struct _finddata_t));
1177 _snprintf(data.name, namesize, "%s\\*", dirname);
1178 dirp->d_handle = _findfirst(data.name, &data);
1180 if( dirp->d_handle==BAD_INTPTR_T ){
1185 /* TODO: Remove this block to allow hidden and/or system files. */
1186 if( is_filtered(data) ){
1189 memset(&data, 0, sizeof(struct _finddata_t));
1190 if( _findnext(dirp->d_handle, &data)==-1 ){
1195 /* TODO: Remove this block to allow hidden and/or system files. */
1196 if( is_filtered(data) ) goto next;
1199 dirp->d_first.d_attributes = data.attrib;
1200 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1201 dirp->d_first.d_name[NAME_MAX] = '\0';
1207 ** Implementation of the POSIX readdir() function using the MSVCRT.
1212 struct _finddata_t data;
1214 if( dirp==NULL ) return NULL;
1216 if( dirp->d_first.d_ino==0 ){
1217 dirp->d_first.d_ino++;
1218 dirp->d_next.d_ino++;
1220 return &dirp->d_first;
1225 memset(&data, 0, sizeof(struct _finddata_t));
1226 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1228 /* TODO: Remove this block to allow hidden and/or system files. */
1229 if( is_filtered(data) ) goto next;
1231 dirp->d_next.d_ino++;
1232 dirp->d_next.d_attributes = data.attrib;
1233 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1234 dirp->d_next.d_name[NAME_MAX] = '\0';
1236 return &dirp->d_next;
1240 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1247 struct _finddata_t data;
1249 if( dirp==NULL ) return EBADF;
1251 if( dirp->d_first.d_ino==0 ){
1252 dirp->d_first.d_ino++;
1253 dirp->d_next.d_ino++;
1255 entry->d_ino = dirp->d_first.d_ino;
1256 entry->d_attributes = dirp->d_first.d_attributes;
1257 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1258 entry->d_name[NAME_MAX] = '\0';
1266 memset(&data, 0, sizeof(struct _finddata_t));
1267 if( _findnext(dirp->d_handle, &data)==-1 ){
1272 /* TODO: Remove this block to allow hidden and/or system files. */
1273 if( is_filtered(data) ) goto next;
1275 entry->d_ino = (ino_t)-1; /* not available */
1276 entry->d_attributes = data.attrib;
1277 strncpy(entry->d_name, data.name, NAME_MAX);
1278 entry->d_name[NAME_MAX] = '\0';
1285 ** Implementation of the POSIX closedir() function using the MSVCRT.
1292 if( dirp==NULL ) return EINVAL;
1294 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1295 result = _findclose(dirp->d_handle);
1302 #endif /* defined(WIN32) && defined(_MSC_VER) */
1304 /************************* End test_windirent.c ********************/
1305 #define dirent DIRENT
1307 /************************* Begin ../ext/misc/shathree.c ******************/
1311 ** The author disclaims copyright to this source code. In place of
1312 ** a legal notice, here is a blessing:
1314 ** May you do good and not evil.
1315 ** May you find forgiveness for yourself and forgive others.
1316 ** May you share freely, never taking more than you give.
1318 ******************************************************************************
1320 ** This SQLite extension implements functions that compute SHA3 hashes.
1321 ** Two SQL functions are implemented:
1324 ** sha3_query(Y,SIZE)
1326 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1329 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1330 ** and returns a hash of their results.
1332 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1333 ** is used. If SIZE is included it must be one of the integers 224, 256,
1334 ** 384, or 512, to determine SHA3 hash variant that is computed.
1336 SQLITE_EXTENSION_INIT1
1340 /* typedef sqlite3_uint64 u64; */
1342 /******************************************************************************
1346 ** Macros to determine whether the machine is big or little endian,
1347 ** and whether or not that determination is run-time or compile-time.
1349 ** For best performance, an attempt is made to guess at the byte-order
1350 ** using C-preprocessor macros. If that is unsuccessful, or if
1351 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1354 #ifndef SHA3_BYTEORDER
1355 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1356 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1357 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1359 # define SHA3_BYTEORDER 1234
1360 # elif defined(sparc) || defined(__ppc__)
1361 # define SHA3_BYTEORDER 4321
1363 # define SHA3_BYTEORDER 0
1369 ** State structure for a SHA3 hash in progress
1371 typedef struct SHA3Context SHA3Context;
1372 struct SHA3Context {
1374 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1375 unsigned char x[1600]; /* ... or 1600 bytes */
1377 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1378 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1379 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1383 ** A single step of the Keccak mixing function for a 1600-bit state
1385 static void KeccakF1600Step(SHA3Context *p){
1387 u64 b0, b1, b2, b3, b4;
1388 u64 c0, c1, c2, c3, c4;
1389 u64 d0, d1, d2, d3, d4;
1390 static const u64 RC[] = {
1391 0x0000000000000001ULL, 0x0000000000008082ULL,
1392 0x800000000000808aULL, 0x8000000080008000ULL,
1393 0x000000000000808bULL, 0x0000000080000001ULL,
1394 0x8000000080008081ULL, 0x8000000000008009ULL,
1395 0x000000000000008aULL, 0x0000000000000088ULL,
1396 0x0000000080008009ULL, 0x000000008000000aULL,
1397 0x000000008000808bULL, 0x800000000000008bULL,
1398 0x8000000000008089ULL, 0x8000000000008003ULL,
1399 0x8000000000008002ULL, 0x8000000000000080ULL,
1400 0x000000000000800aULL, 0x800000008000000aULL,
1401 0x8000000080008081ULL, 0x8000000000008080ULL,
1402 0x0000000080000001ULL, 0x8000000080008008ULL
1404 # define a00 (p->u.s[0])
1405 # define a01 (p->u.s[1])
1406 # define a02 (p->u.s[2])
1407 # define a03 (p->u.s[3])
1408 # define a04 (p->u.s[4])
1409 # define a10 (p->u.s[5])
1410 # define a11 (p->u.s[6])
1411 # define a12 (p->u.s[7])
1412 # define a13 (p->u.s[8])
1413 # define a14 (p->u.s[9])
1414 # define a20 (p->u.s[10])
1415 # define a21 (p->u.s[11])
1416 # define a22 (p->u.s[12])
1417 # define a23 (p->u.s[13])
1418 # define a24 (p->u.s[14])
1419 # define a30 (p->u.s[15])
1420 # define a31 (p->u.s[16])
1421 # define a32 (p->u.s[17])
1422 # define a33 (p->u.s[18])
1423 # define a34 (p->u.s[19])
1424 # define a40 (p->u.s[20])
1425 # define a41 (p->u.s[21])
1426 # define a42 (p->u.s[22])
1427 # define a43 (p->u.s[23])
1428 # define a44 (p->u.s[24])
1429 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1431 for(i=0; i<24; i+=4){
1432 c0 = a00^a10^a20^a30^a40;
1433 c1 = a01^a11^a21^a31^a41;
1434 c2 = a02^a12^a22^a32^a42;
1435 c3 = a03^a13^a23^a33^a43;
1436 c4 = a04^a14^a24^a34^a44;
1437 d0 = c4^ROL64(c1, 1);
1438 d1 = c0^ROL64(c2, 1);
1439 d2 = c1^ROL64(c3, 1);
1440 d3 = c2^ROL64(c4, 1);
1441 d4 = c3^ROL64(c0, 1);
1444 b1 = ROL64((a11^d1), 44);
1445 b2 = ROL64((a22^d2), 43);
1446 b3 = ROL64((a33^d3), 21);
1447 b4 = ROL64((a44^d4), 14);
1448 a00 = b0 ^((~b1)& b2 );
1450 a11 = b1 ^((~b2)& b3 );
1451 a22 = b2 ^((~b3)& b4 );
1452 a33 = b3 ^((~b4)& b0 );
1453 a44 = b4 ^((~b0)& b1 );
1455 b2 = ROL64((a20^d0), 3);
1456 b3 = ROL64((a31^d1), 45);
1457 b4 = ROL64((a42^d2), 61);
1458 b0 = ROL64((a03^d3), 28);
1459 b1 = ROL64((a14^d4), 20);
1460 a20 = b0 ^((~b1)& b2 );
1461 a31 = b1 ^((~b2)& b3 );
1462 a42 = b2 ^((~b3)& b4 );
1463 a03 = b3 ^((~b4)& b0 );
1464 a14 = b4 ^((~b0)& b1 );
1466 b4 = ROL64((a40^d0), 18);
1467 b0 = ROL64((a01^d1), 1);
1468 b1 = ROL64((a12^d2), 6);
1469 b2 = ROL64((a23^d3), 25);
1470 b3 = ROL64((a34^d4), 8);
1471 a40 = b0 ^((~b1)& b2 );
1472 a01 = b1 ^((~b2)& b3 );
1473 a12 = b2 ^((~b3)& b4 );
1474 a23 = b3 ^((~b4)& b0 );
1475 a34 = b4 ^((~b0)& b1 );
1477 b1 = ROL64((a10^d0), 36);
1478 b2 = ROL64((a21^d1), 10);
1479 b3 = ROL64((a32^d2), 15);
1480 b4 = ROL64((a43^d3), 56);
1481 b0 = ROL64((a04^d4), 27);
1482 a10 = b0 ^((~b1)& b2 );
1483 a21 = b1 ^((~b2)& b3 );
1484 a32 = b2 ^((~b3)& b4 );
1485 a43 = b3 ^((~b4)& b0 );
1486 a04 = b4 ^((~b0)& b1 );
1488 b3 = ROL64((a30^d0), 41);
1489 b4 = ROL64((a41^d1), 2);
1490 b0 = ROL64((a02^d2), 62);
1491 b1 = ROL64((a13^d3), 55);
1492 b2 = ROL64((a24^d4), 39);
1493 a30 = b0 ^((~b1)& b2 );
1494 a41 = b1 ^((~b2)& b3 );
1495 a02 = b2 ^((~b3)& b4 );
1496 a13 = b3 ^((~b4)& b0 );
1497 a24 = b4 ^((~b0)& b1 );
1499 c0 = a00^a20^a40^a10^a30;
1500 c1 = a11^a31^a01^a21^a41;
1501 c2 = a22^a42^a12^a32^a02;
1502 c3 = a33^a03^a23^a43^a13;
1503 c4 = a44^a14^a34^a04^a24;
1504 d0 = c4^ROL64(c1, 1);
1505 d1 = c0^ROL64(c2, 1);
1506 d2 = c1^ROL64(c3, 1);
1507 d3 = c2^ROL64(c4, 1);
1508 d4 = c3^ROL64(c0, 1);
1511 b1 = ROL64((a31^d1), 44);
1512 b2 = ROL64((a12^d2), 43);
1513 b3 = ROL64((a43^d3), 21);
1514 b4 = ROL64((a24^d4), 14);
1515 a00 = b0 ^((~b1)& b2 );
1517 a31 = b1 ^((~b2)& b3 );
1518 a12 = b2 ^((~b3)& b4 );
1519 a43 = b3 ^((~b4)& b0 );
1520 a24 = b4 ^((~b0)& b1 );
1522 b2 = ROL64((a40^d0), 3);
1523 b3 = ROL64((a21^d1), 45);
1524 b4 = ROL64((a02^d2), 61);
1525 b0 = ROL64((a33^d3), 28);
1526 b1 = ROL64((a14^d4), 20);
1527 a40 = b0 ^((~b1)& b2 );
1528 a21 = b1 ^((~b2)& b3 );
1529 a02 = b2 ^((~b3)& b4 );
1530 a33 = b3 ^((~b4)& b0 );
1531 a14 = b4 ^((~b0)& b1 );
1533 b4 = ROL64((a30^d0), 18);
1534 b0 = ROL64((a11^d1), 1);
1535 b1 = ROL64((a42^d2), 6);
1536 b2 = ROL64((a23^d3), 25);
1537 b3 = ROL64((a04^d4), 8);
1538 a30 = b0 ^((~b1)& b2 );
1539 a11 = b1 ^((~b2)& b3 );
1540 a42 = b2 ^((~b3)& b4 );
1541 a23 = b3 ^((~b4)& b0 );
1542 a04 = b4 ^((~b0)& b1 );
1544 b1 = ROL64((a20^d0), 36);
1545 b2 = ROL64((a01^d1), 10);
1546 b3 = ROL64((a32^d2), 15);
1547 b4 = ROL64((a13^d3), 56);
1548 b0 = ROL64((a44^d4), 27);
1549 a20 = b0 ^((~b1)& b2 );
1550 a01 = b1 ^((~b2)& b3 );
1551 a32 = b2 ^((~b3)& b4 );
1552 a13 = b3 ^((~b4)& b0 );
1553 a44 = b4 ^((~b0)& b1 );
1555 b3 = ROL64((a10^d0), 41);
1556 b4 = ROL64((a41^d1), 2);
1557 b0 = ROL64((a22^d2), 62);
1558 b1 = ROL64((a03^d3), 55);
1559 b2 = ROL64((a34^d4), 39);
1560 a10 = b0 ^((~b1)& b2 );
1561 a41 = b1 ^((~b2)& b3 );
1562 a22 = b2 ^((~b3)& b4 );
1563 a03 = b3 ^((~b4)& b0 );
1564 a34 = b4 ^((~b0)& b1 );
1566 c0 = a00^a40^a30^a20^a10;
1567 c1 = a31^a21^a11^a01^a41;
1568 c2 = a12^a02^a42^a32^a22;
1569 c3 = a43^a33^a23^a13^a03;
1570 c4 = a24^a14^a04^a44^a34;
1571 d0 = c4^ROL64(c1, 1);
1572 d1 = c0^ROL64(c2, 1);
1573 d2 = c1^ROL64(c3, 1);
1574 d3 = c2^ROL64(c4, 1);
1575 d4 = c3^ROL64(c0, 1);
1578 b1 = ROL64((a21^d1), 44);
1579 b2 = ROL64((a42^d2), 43);
1580 b3 = ROL64((a13^d3), 21);
1581 b4 = ROL64((a34^d4), 14);
1582 a00 = b0 ^((~b1)& b2 );
1584 a21 = b1 ^((~b2)& b3 );
1585 a42 = b2 ^((~b3)& b4 );
1586 a13 = b3 ^((~b4)& b0 );
1587 a34 = b4 ^((~b0)& b1 );
1589 b2 = ROL64((a30^d0), 3);
1590 b3 = ROL64((a01^d1), 45);
1591 b4 = ROL64((a22^d2), 61);
1592 b0 = ROL64((a43^d3), 28);
1593 b1 = ROL64((a14^d4), 20);
1594 a30 = b0 ^((~b1)& b2 );
1595 a01 = b1 ^((~b2)& b3 );
1596 a22 = b2 ^((~b3)& b4 );
1597 a43 = b3 ^((~b4)& b0 );
1598 a14 = b4 ^((~b0)& b1 );
1600 b4 = ROL64((a10^d0), 18);
1601 b0 = ROL64((a31^d1), 1);
1602 b1 = ROL64((a02^d2), 6);
1603 b2 = ROL64((a23^d3), 25);
1604 b3 = ROL64((a44^d4), 8);
1605 a10 = b0 ^((~b1)& b2 );
1606 a31 = b1 ^((~b2)& b3 );
1607 a02 = b2 ^((~b3)& b4 );
1608 a23 = b3 ^((~b4)& b0 );
1609 a44 = b4 ^((~b0)& b1 );
1611 b1 = ROL64((a40^d0), 36);
1612 b2 = ROL64((a11^d1), 10);
1613 b3 = ROL64((a32^d2), 15);
1614 b4 = ROL64((a03^d3), 56);
1615 b0 = ROL64((a24^d4), 27);
1616 a40 = b0 ^((~b1)& b2 );
1617 a11 = b1 ^((~b2)& b3 );
1618 a32 = b2 ^((~b3)& b4 );
1619 a03 = b3 ^((~b4)& b0 );
1620 a24 = b4 ^((~b0)& b1 );
1622 b3 = ROL64((a20^d0), 41);
1623 b4 = ROL64((a41^d1), 2);
1624 b0 = ROL64((a12^d2), 62);
1625 b1 = ROL64((a33^d3), 55);
1626 b2 = ROL64((a04^d4), 39);
1627 a20 = b0 ^((~b1)& b2 );
1628 a41 = b1 ^((~b2)& b3 );
1629 a12 = b2 ^((~b3)& b4 );
1630 a33 = b3 ^((~b4)& b0 );
1631 a04 = b4 ^((~b0)& b1 );
1633 c0 = a00^a30^a10^a40^a20;
1634 c1 = a21^a01^a31^a11^a41;
1635 c2 = a42^a22^a02^a32^a12;
1636 c3 = a13^a43^a23^a03^a33;
1637 c4 = a34^a14^a44^a24^a04;
1638 d0 = c4^ROL64(c1, 1);
1639 d1 = c0^ROL64(c2, 1);
1640 d2 = c1^ROL64(c3, 1);
1641 d3 = c2^ROL64(c4, 1);
1642 d4 = c3^ROL64(c0, 1);
1645 b1 = ROL64((a01^d1), 44);
1646 b2 = ROL64((a02^d2), 43);
1647 b3 = ROL64((a03^d3), 21);
1648 b4 = ROL64((a04^d4), 14);
1649 a00 = b0 ^((~b1)& b2 );
1651 a01 = b1 ^((~b2)& b3 );
1652 a02 = b2 ^((~b3)& b4 );
1653 a03 = b3 ^((~b4)& b0 );
1654 a04 = b4 ^((~b0)& b1 );
1656 b2 = ROL64((a10^d0), 3);
1657 b3 = ROL64((a11^d1), 45);
1658 b4 = ROL64((a12^d2), 61);
1659 b0 = ROL64((a13^d3), 28);
1660 b1 = ROL64((a14^d4), 20);
1661 a10 = b0 ^((~b1)& b2 );
1662 a11 = b1 ^((~b2)& b3 );
1663 a12 = b2 ^((~b3)& b4 );
1664 a13 = b3 ^((~b4)& b0 );
1665 a14 = b4 ^((~b0)& b1 );
1667 b4 = ROL64((a20^d0), 18);
1668 b0 = ROL64((a21^d1), 1);
1669 b1 = ROL64((a22^d2), 6);
1670 b2 = ROL64((a23^d3), 25);
1671 b3 = ROL64((a24^d4), 8);
1672 a20 = b0 ^((~b1)& b2 );
1673 a21 = b1 ^((~b2)& b3 );
1674 a22 = b2 ^((~b3)& b4 );
1675 a23 = b3 ^((~b4)& b0 );
1676 a24 = b4 ^((~b0)& b1 );
1678 b1 = ROL64((a30^d0), 36);
1679 b2 = ROL64((a31^d1), 10);
1680 b3 = ROL64((a32^d2), 15);
1681 b4 = ROL64((a33^d3), 56);
1682 b0 = ROL64((a34^d4), 27);
1683 a30 = b0 ^((~b1)& b2 );
1684 a31 = b1 ^((~b2)& b3 );
1685 a32 = b2 ^((~b3)& b4 );
1686 a33 = b3 ^((~b4)& b0 );
1687 a34 = b4 ^((~b0)& b1 );
1689 b3 = ROL64((a40^d0), 41);
1690 b4 = ROL64((a41^d1), 2);
1691 b0 = ROL64((a42^d2), 62);
1692 b1 = ROL64((a43^d3), 55);
1693 b2 = ROL64((a44^d4), 39);
1694 a40 = b0 ^((~b1)& b2 );
1695 a41 = b1 ^((~b2)& b3 );
1696 a42 = b2 ^((~b3)& b4 );
1697 a43 = b3 ^((~b4)& b0 );
1698 a44 = b4 ^((~b0)& b1 );
1703 ** Initialize a new hash. iSize determines the size of the hash
1704 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1705 ** can be zero to use the default hash size of 256 bits.
1707 static void SHA3Init(SHA3Context *p, int iSize){
1708 memset(p, 0, sizeof(*p));
1709 if( iSize>=128 && iSize<=512 ){
1710 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1712 p->nRate = (1600 - 2*256)/8;
1714 #if SHA3_BYTEORDER==1234
1715 /* Known to be little-endian at compile-time. No-op */
1716 #elif SHA3_BYTEORDER==4321
1717 p->ixMask = 7; /* Big-endian */
1720 static unsigned int one = 1;
1721 if( 1==*(unsigned char*)&one ){
1722 /* Little endian. No byte swapping. */
1725 /* Big endian. Byte swap. */
1733 ** Make consecutive calls to the SHA3Update function to add new content
1736 static void SHA3Update(
1738 const unsigned char *aData,
1742 #if SHA3_BYTEORDER==1234
1743 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1744 for(; i+7<nData; i+=8){
1745 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1747 if( p->nLoaded>=p->nRate ){
1754 for(; i<nData; i++){
1755 #if SHA3_BYTEORDER==1234
1756 p->u.x[p->nLoaded] ^= aData[i];
1757 #elif SHA3_BYTEORDER==4321
1758 p->u.x[p->nLoaded^0x07] ^= aData[i];
1760 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1763 if( p->nLoaded==p->nRate ){
1771 ** After all content has been added, invoke SHA3Final() to compute
1772 ** the final hash. The function returns a pointer to the binary
1775 static unsigned char *SHA3Final(SHA3Context *p){
1777 if( p->nLoaded==p->nRate-1 ){
1778 const unsigned char c1 = 0x86;
1779 SHA3Update(p, &c1, 1);
1781 const unsigned char c2 = 0x06;
1782 const unsigned char c3 = 0x80;
1783 SHA3Update(p, &c2, 1);
1784 p->nLoaded = p->nRate - 1;
1785 SHA3Update(p, &c3, 1);
1787 for(i=0; i<p->nRate; i++){
1788 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1790 return &p->u.x[p->nRate];
1792 /* End of the hashing logic
1793 *****************************************************************************/
1796 ** Implementation of the sha3(X,SIZE) function.
1798 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1799 ** size is 256. If X is a BLOB, it is hashed as is.
1800 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1801 ** and the string is hashed without the trailing 0x00 terminator. The hash
1802 ** of a NULL value is NULL.
1804 static void sha3Func(
1805 sqlite3_context *context,
1807 sqlite3_value **argv
1810 int eType = sqlite3_value_type(argv[0]);
1811 int nByte = sqlite3_value_bytes(argv[0]);
1816 iSize = sqlite3_value_int(argv[1]);
1817 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1818 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1823 if( eType==SQLITE_NULL ) return;
1824 SHA3Init(&cx, iSize);
1825 if( eType==SQLITE_BLOB ){
1826 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1828 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1830 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1833 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1834 ** of 50 bytes and add it to the hash.
1836 static void hash_step_vformat(
1837 SHA3Context *p, /* Add content to this context */
1838 const char *zFormat,
1844 va_start(ap, zFormat);
1845 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1847 n = (int)strlen(zBuf);
1848 SHA3Update(p, (unsigned char*)zBuf, n);
1852 ** Implementation of the sha3_query(SQL,SIZE) function.
1854 ** This function compiles and runs the SQL statement(s) given in the
1855 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1858 ** The format of the byte stream that is hashed is summarized as follows:
1868 ** <sql> is the original SQL text for each statement run and <n> is
1869 ** the size of that text. The SQL text is UTF-8. A single R character
1870 ** occurs before the start of each row. N means a NULL value.
1871 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1872 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1873 ** B means blobs of <size> bytes. T means text rendered as <size>
1874 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1877 ** For each SQL statement in the X input, there is one S segment. Each
1878 ** S segment is followed by zero or more R segments, one for each row in the
1879 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1880 ** one for each column in the result set. Segments are concatentated directly
1881 ** with no delimiters of any kind.
1883 static void sha3QueryFunc(
1884 sqlite3_context *context,
1886 sqlite3_value **argv
1888 sqlite3 *db = sqlite3_context_db_handle(context);
1889 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1890 sqlite3_stmt *pStmt = 0;
1891 int nCol; /* Number of columns in the result set */
1892 int i; /* Loop counter */
1902 iSize = sqlite3_value_int(argv[1]);
1903 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1904 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1909 if( zSql==0 ) return;
1910 SHA3Init(&cx, iSize);
1912 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1914 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1915 zSql, sqlite3_errmsg(db));
1916 sqlite3_finalize(pStmt);
1917 sqlite3_result_error(context, zMsg, -1);
1921 if( !sqlite3_stmt_readonly(pStmt) ){
1922 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1923 sqlite3_finalize(pStmt);
1924 sqlite3_result_error(context, zMsg, -1);
1928 nCol = sqlite3_column_count(pStmt);
1929 z = sqlite3_sql(pStmt);
1931 hash_step_vformat(&cx,"S%d:",n);
1932 SHA3Update(&cx,(unsigned char*)z,n);
1934 /* Compute a hash over the result of the query */
1935 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1936 SHA3Update(&cx,(const unsigned char*)"R",1);
1937 for(i=0; i<nCol; i++){
1938 switch( sqlite3_column_type(pStmt,i) ){
1940 SHA3Update(&cx, (const unsigned char*)"N",1);
1943 case SQLITE_INTEGER: {
1947 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1949 for(j=8; j>=1; j--){
1954 SHA3Update(&cx, x, 9);
1957 case SQLITE_FLOAT: {
1961 double r = sqlite3_column_double(pStmt,i);
1963 for(j=8; j>=1; j--){
1968 SHA3Update(&cx,x,9);
1972 int n2 = sqlite3_column_bytes(pStmt, i);
1973 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1974 hash_step_vformat(&cx,"T%d:",n2);
1975 SHA3Update(&cx, z2, n2);
1979 int n2 = sqlite3_column_bytes(pStmt, i);
1980 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1981 hash_step_vformat(&cx,"B%d:",n2);
1982 SHA3Update(&cx, z2, n2);
1988 sqlite3_finalize(pStmt);
1990 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1997 int sqlite3_shathree_init(
2000 const sqlite3_api_routines *pApi
2003 SQLITE_EXTENSION_INIT2(pApi);
2004 (void)pzErrMsg; /* Unused parameter */
2005 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2007 if( rc==SQLITE_OK ){
2008 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2011 if( rc==SQLITE_OK ){
2012 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2013 sha3QueryFunc, 0, 0);
2015 if( rc==SQLITE_OK ){
2016 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2017 sha3QueryFunc, 0, 0);
2022 /************************* End ../ext/misc/shathree.c ********************/
2023 /************************* Begin ../ext/misc/fileio.c ******************/
2027 ** The author disclaims copyright to this source code. In place of
2028 ** a legal notice, here is a blessing:
2030 ** May you do good and not evil.
2031 ** May you find forgiveness for yourself and forgive others.
2032 ** May you share freely, never taking more than you give.
2034 ******************************************************************************
2036 ** This SQLite extension implements SQL functions readfile() and
2037 ** writefile(), and eponymous virtual type "fsdir".
2039 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2041 ** If neither of the optional arguments is present, then this UDF
2042 ** function writes blob DATA to file FILE. If successful, the number
2043 ** of bytes written is returned. If an error occurs, NULL is returned.
2045 ** If the first option argument - MODE - is present, then it must
2046 ** be passed an integer value that corresponds to a POSIX mode
2047 ** value (file type + permissions, as returned in the stat.st_mode
2048 ** field by the stat() system call). Three types of files may
2049 ** be written/created:
2051 ** regular files: (mode & 0170000)==0100000
2052 ** symbolic links: (mode & 0170000)==0120000
2053 ** directories: (mode & 0170000)==0040000
2055 ** For a directory, the DATA is ignored. For a symbolic link, it is
2056 ** interpreted as text and used as the target of the link. For a
2057 ** regular file, it is interpreted as a blob and written into the
2058 ** named file. Regardless of the type of file, its permissions are
2059 ** set to (mode & 0777) before returning.
2061 ** If the optional MTIME argument is present, then it is interpreted
2062 ** as an integer - the number of seconds since the unix epoch. The
2063 ** modification-time of the target file is set to this value before
2066 ** If three or more arguments are passed to this function and an
2067 ** error is encountered, an exception is raised.
2071 ** Read and return the contents of file FILE (type blob) from disk.
2077 ** SELECT * FROM fsdir($path [, $dir]);
2079 ** Parameter $path is an absolute or relative pathname. If the file that it
2080 ** refers to does not exist, it is an error. If the path refers to a regular
2081 ** file or symbolic link, it returns a single row. Or, if the path refers
2082 ** to a directory, it returns one row for the directory, and one row for each
2083 ** file within the hierarchy rooted at $path.
2085 ** Each row has the following columns:
2087 ** name: Path to file or directory (text value).
2088 ** mode: Value of stat.st_mode for directory entry (an integer).
2089 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2090 ** data: For a regular file, a blob containing the file data. For a
2091 ** symlink, a text value containing the text of the link. For a
2094 ** If a non-NULL value is specified for the optional $dir parameter and
2095 ** $path is a relative path, then $path is interpreted relative to $dir.
2096 ** And the paths returned in the "name" column of the table are also
2097 ** relative to directory $dir.
2099 SQLITE_EXTENSION_INIT1
2104 #include <sys/types.h>
2105 #include <sys/stat.h>
2107 #if !defined(_WIN32) && !defined(WIN32)
2108 # include <unistd.h>
2109 # include <dirent.h>
2111 # include <sys/time.h>
2113 # include "windows.h"
2115 # include <direct.h>
2116 /* # include "test_windirent.h" */
2117 # define dirent DIRENT
2119 # define chmod _chmod
2124 # define mkdir(path,mode) _mkdir(path)
2125 # define lstat(path,buf) stat(path,buf)
2132 ** Structure of the fsdir() table-valued function
2135 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2136 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2137 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2138 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2139 #define FSDIR_COLUMN_DATA 3 /* File content */
2140 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2141 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2145 ** Set the result stored by context ctx to a blob containing the
2146 ** contents of file zName.
2148 static void readFileContents(sqlite3_context *ctx, const char *zName){
2153 in = fopen(zName, "rb");
2155 fseek(in, 0, SEEK_END);
2158 pBuf = sqlite3_malloc( nIn );
2159 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
2160 sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
2168 ** Implementation of the "readfile(X)" SQL function. The entire content
2169 ** of the file named X is read and returned as a BLOB. NULL is returned
2170 ** if the file does not exist or is unreadable.
2172 static void readfileFunc(
2173 sqlite3_context *context,
2175 sqlite3_value **argv
2178 (void)(argc); /* Unused parameter */
2179 zName = (const char*)sqlite3_value_text(argv[0]);
2180 if( zName==0 ) return;
2181 readFileContents(context, zName);
2185 ** Set the error message contained in context ctx to the results of
2186 ** vprintf(zFmt, ...).
2188 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2192 zMsg = sqlite3_vmprintf(zFmt, ap);
2193 sqlite3_result_error(ctx, zMsg, -1);
2200 ** This function is designed to convert a Win32 FILETIME structure into the
2201 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2203 static sqlite3_uint64 fileTimeToUnixTime(
2204 LPFILETIME pFileTime
2206 SYSTEMTIME epochSystemTime;
2207 ULARGE_INTEGER epochIntervals;
2208 FILETIME epochFileTime;
2209 ULARGE_INTEGER fileIntervals;
2211 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2212 epochSystemTime.wYear = 1970;
2213 epochSystemTime.wMonth = 1;
2214 epochSystemTime.wDay = 1;
2215 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2216 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2217 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2219 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2220 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2222 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2226 ** This function attempts to normalize the time values found in the stat()
2227 ** buffer to UTC. This is necessary on Win32, where the runtime library
2228 ** appears to return these values as local times.
2230 static void statTimesToUtc(
2232 struct stat *pStatBuf
2235 WIN32_FIND_DATAW fd;
2236 LPWSTR zUnicodeName;
2237 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2238 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2240 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2241 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2242 if( hFindFile!=NULL ){
2243 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2244 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2245 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2246 FindClose(hFindFile);
2248 sqlite3_free(zUnicodeName);
2254 ** This function is used in place of stat(). On Windows, special handling
2255 ** is required in order for the included time to be returned as UTC. On all
2256 ** other systems, this function simply calls stat().
2258 static int fileStat(
2260 struct stat *pStatBuf
2263 int rc = stat(zPath, pStatBuf);
2264 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2267 return stat(zPath, pStatBuf);
2272 ** This function is used in place of lstat(). On Windows, special handling
2273 ** is required in order for the included time to be returned as UTC. On all
2274 ** other systems, this function simply calls lstat().
2276 static int fileLinkStat(
2278 struct stat *pStatBuf
2281 int rc = lstat(zPath, pStatBuf);
2282 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2285 return lstat(zPath, pStatBuf);
2290 ** Argument zFile is the name of a file that will be created and/or written
2291 ** by SQL function writefile(). This function ensures that the directory
2292 ** zFile will be written to exists, creating it if required. The permissions
2293 ** for any path components created by this function are set to (mode&0777).
2295 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2296 ** SQLITE_OK is returned if the directory is successfully created, or
2297 ** SQLITE_ERROR otherwise.
2299 static int makeDirectory(
2303 char *zCopy = sqlite3_mprintf("%s", zFile);
2309 int nCopy = (int)strlen(zCopy);
2312 while( rc==SQLITE_OK ){
2316 for(; zCopy[i]!='/' && i<nCopy; i++);
2317 if( i==nCopy ) break;
2320 rc2 = fileStat(zCopy, &sStat);
2322 if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2324 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2330 sqlite3_free(zCopy);
2337 ** This function does the work for the writefile() UDF. Refer to
2338 ** header comments at the top of this file for details.
2340 static int writeFile(
2341 sqlite3_context *pCtx, /* Context to return bytes written in */
2342 const char *zFile, /* File to write */
2343 sqlite3_value *pData, /* Data to write */
2344 mode_t mode, /* MODE parameter passed to writefile() */
2345 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2347 #if !defined(_WIN32) && !defined(WIN32)
2348 if( S_ISLNK(mode) ){
2349 const char *zTo = (const char*)sqlite3_value_text(pData);
2350 if( symlink(zTo, zFile)<0 ) return 1;
2354 if( S_ISDIR(mode) ){
2355 if( mkdir(zFile, mode) ){
2356 /* The mkdir() call to create the directory failed. This might not
2357 ** be an error though - if there is already a directory at the same
2358 ** path and either the permissions already match or can be changed
2359 ** to do so using chmod(), it is not an error. */
2362 || 0!=fileStat(zFile, &sStat)
2363 || !S_ISDIR(sStat.st_mode)
2364 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2370 sqlite3_int64 nWrite = 0;
2373 FILE *out = fopen(zFile, "wb");
2374 if( out==0 ) return 1;
2375 z = (const char*)sqlite3_value_blob(pData);
2377 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2378 nWrite = sqlite3_value_bytes(pData);
2384 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2388 sqlite3_result_int64(pCtx, nWrite);
2395 FILETIME lastAccess;
2397 SYSTEMTIME currentTime;
2400 LPWSTR zUnicodeName;
2401 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2403 GetSystemTime(¤tTime);
2404 SystemTimeToFileTime(¤tTime, &lastAccess);
2405 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2406 lastWrite.dwLowDateTime = (DWORD)intervals;
2407 lastWrite.dwHighDateTime = intervals >> 32;
2408 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2409 if( zUnicodeName==0 ){
2412 hFile = CreateFileW(
2413 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2414 FILE_FLAG_BACKUP_SEMANTICS, NULL
2416 sqlite3_free(zUnicodeName);
2417 if( hFile!=INVALID_HANDLE_VALUE ){
2418 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2424 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2426 struct timespec times[2];
2427 times[0].tv_nsec = times[1].tv_nsec = 0;
2428 times[0].tv_sec = time(0);
2429 times[1].tv_sec = mtime;
2430 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2435 struct timeval times[2];
2436 times[0].tv_usec = times[1].tv_usec = 0;
2437 times[0].tv_sec = time(0);
2438 times[1].tv_sec = mtime;
2439 if( utimes(zFile, times) ){
2449 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2450 ** Refer to header comments at the top of this file for details.
2452 static void writefileFunc(
2453 sqlite3_context *context,
2455 sqlite3_value **argv
2460 sqlite3_int64 mtime = -1;
2462 if( argc<2 || argc>4 ){
2463 sqlite3_result_error(context,
2464 "wrong number of arguments to function writefile()", -1
2469 zFile = (const char*)sqlite3_value_text(argv[0]);
2470 if( zFile==0 ) return;
2472 mode = (mode_t)sqlite3_value_int(argv[2]);
2475 mtime = sqlite3_value_int64(argv[3]);
2478 res = writeFile(context, zFile, argv[1], mode, mtime);
2479 if( res==1 && errno==ENOENT ){
2480 if( makeDirectory(zFile, mode)==SQLITE_OK ){
2481 res = writeFile(context, zFile, argv[1], mode, mtime);
2485 if( argc>2 && res!=0 ){
2486 if( S_ISLNK(mode) ){
2487 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2488 }else if( S_ISDIR(mode) ){
2489 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2491 ctxErrorMsg(context, "failed to write file: %s", zFile);
2497 ** SQL function: lsmode(MODE)
2499 ** Given a numberic st_mode from stat(), convert it into a human-readable
2500 ** text string in the style of "ls -l".
2502 static void lsModeFunc(
2503 sqlite3_context *context,
2505 sqlite3_value **argv
2508 int iMode = sqlite3_value_int(argv[0]);
2511 if( S_ISLNK(iMode) ){
2513 }else if( S_ISREG(iMode) ){
2515 }else if( S_ISDIR(iMode) ){
2521 int m = (iMode >> ((2-i)*3));
2522 char *a = &z[1 + i*3];
2523 a[0] = (m & 0x4) ? 'r' : '-';
2524 a[1] = (m & 0x2) ? 'w' : '-';
2525 a[2] = (m & 0x1) ? 'x' : '-';
2528 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2531 #ifndef SQLITE_OMIT_VIRTUALTABLE
2534 ** Cursor type for recursively iterating through a directory structure.
2536 typedef struct fsdir_cursor fsdir_cursor;
2537 typedef struct FsdirLevel FsdirLevel;
2540 DIR *pDir; /* From opendir() */
2541 char *zDir; /* Name of directory (nul-terminated) */
2544 struct fsdir_cursor {
2545 sqlite3_vtab_cursor base; /* Base class - must be first */
2547 int nLvl; /* Number of entries in aLvl[] array */
2548 int iLvl; /* Index of current entry */
2549 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2554 struct stat sStat; /* Current lstat() results */
2555 char *zPath; /* Path to current entry */
2556 sqlite3_int64 iRowid; /* Current rowid */
2559 typedef struct fsdir_tab fsdir_tab;
2561 sqlite3_vtab base; /* Base class - must be first */
2565 ** Construct a new fsdir virtual table object.
2567 static int fsdirConnect(
2570 int argc, const char *const*argv,
2571 sqlite3_vtab **ppVtab,
2574 fsdir_tab *pNew = 0;
2580 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2581 if( rc==SQLITE_OK ){
2582 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2583 if( pNew==0 ) return SQLITE_NOMEM;
2584 memset(pNew, 0, sizeof(*pNew));
2586 *ppVtab = (sqlite3_vtab*)pNew;
2591 ** This method is the destructor for fsdir vtab objects.
2593 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2594 sqlite3_free(pVtab);
2599 ** Constructor for a new fsdir_cursor object.
2601 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2604 pCur = sqlite3_malloc( sizeof(*pCur) );
2605 if( pCur==0 ) return SQLITE_NOMEM;
2606 memset(pCur, 0, sizeof(*pCur));
2608 *ppCursor = &pCur->base;
2613 ** Reset a cursor back to the state it was in when first returned
2616 static void fsdirResetCursor(fsdir_cursor *pCur){
2618 for(i=0; i<=pCur->iLvl; i++){
2619 FsdirLevel *pLvl = &pCur->aLvl[i];
2620 if( pLvl->pDir ) closedir(pLvl->pDir);
2621 sqlite3_free(pLvl->zDir);
2623 sqlite3_free(pCur->zPath);
2624 sqlite3_free(pCur->aLvl);
2635 ** Destructor for an fsdir_cursor.
2637 static int fsdirClose(sqlite3_vtab_cursor *cur){
2638 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2640 fsdirResetCursor(pCur);
2646 ** Set the error message for the virtual table associated with cursor
2647 ** pCur to the results of vprintf(zFmt, ...).
2649 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2652 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2658 ** Advance an fsdir_cursor to its next row of output.
2660 static int fsdirNext(sqlite3_vtab_cursor *cur){
2661 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2662 mode_t m = pCur->sStat.st_mode;
2666 /* Descend into this directory */
2667 int iNew = pCur->iLvl + 1;
2669 if( iNew>=pCur->nLvl ){
2671 int nByte = nNew*sizeof(FsdirLevel);
2672 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
2673 if( aNew==0 ) return SQLITE_NOMEM;
2674 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2679 pLvl = &pCur->aLvl[iNew];
2681 pLvl->zDir = pCur->zPath;
2683 pLvl->pDir = opendir(pLvl->zDir);
2684 if( pLvl->pDir==0 ){
2685 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2686 return SQLITE_ERROR;
2690 while( pCur->iLvl>=0 ){
2691 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2692 struct dirent *pEntry = readdir(pLvl->pDir);
2694 if( pEntry->d_name[0]=='.' ){
2695 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2696 if( pEntry->d_name[1]=='\0' ) continue;
2698 sqlite3_free(pCur->zPath);
2699 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2700 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2701 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2702 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2703 return SQLITE_ERROR;
2707 closedir(pLvl->pDir);
2708 sqlite3_free(pLvl->zDir);
2715 sqlite3_free(pCur->zPath);
2721 ** Return values of columns for the row at which the series_cursor
2722 ** is currently pointing.
2724 static int fsdirColumn(
2725 sqlite3_vtab_cursor *cur, /* The cursor */
2726 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2727 int i /* Which column to return */
2729 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2731 case FSDIR_COLUMN_NAME: {
2732 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2736 case FSDIR_COLUMN_MODE:
2737 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2740 case FSDIR_COLUMN_MTIME:
2741 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2744 case FSDIR_COLUMN_DATA: {
2745 mode_t m = pCur->sStat.st_mode;
2747 sqlite3_result_null(ctx);
2748 #if !defined(_WIN32) && !defined(WIN32)
2749 }else if( S_ISLNK(m) ){
2751 char *aBuf = aStatic;
2756 n = readlink(pCur->zPath, aBuf, nBuf);
2758 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2760 aBuf = sqlite3_malloc(nBuf);
2762 sqlite3_result_error_nomem(ctx);
2763 return SQLITE_NOMEM;
2767 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2768 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2771 readFileContents(ctx, pCur->zPath);
2774 case FSDIR_COLUMN_PATH:
2776 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2777 ** always return their values as NULL */
2785 ** Return the rowid for the current row. In this implementation, the
2786 ** first row returned is assigned rowid value 1, and each subsequent
2787 ** row a value 1 more than that of the previous.
2789 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2790 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2791 *pRowid = pCur->iRowid;
2796 ** Return TRUE if the cursor has been moved off of the last
2799 static int fsdirEof(sqlite3_vtab_cursor *cur){
2800 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2801 return (pCur->zPath==0);
2805 ** xFilter callback.
2807 ** idxNum==1 PATH parameter only
2808 ** idxNum==2 Both PATH and DIR supplied
2810 static int fsdirFilter(
2811 sqlite3_vtab_cursor *cur,
2812 int idxNum, const char *idxStr,
2813 int argc, sqlite3_value **argv
2815 const char *zDir = 0;
2816 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2818 fsdirResetCursor(pCur);
2821 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2822 return SQLITE_ERROR;
2825 assert( argc==idxNum && (argc==1 || argc==2) );
2826 zDir = (const char*)sqlite3_value_text(argv[0]);
2828 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2829 return SQLITE_ERROR;
2832 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2835 pCur->nBase = (int)strlen(pCur->zBase)+1;
2836 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2838 pCur->zPath = sqlite3_mprintf("%s", zDir);
2841 if( pCur->zPath==0 ){
2842 return SQLITE_NOMEM;
2844 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2845 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2846 return SQLITE_ERROR;
2853 ** SQLite will invoke this method one or more times while planning a query
2854 ** that uses the generate_series virtual table. This routine needs to create
2855 ** a query plan for each invocation and compute an estimated cost for that
2858 ** In this implementation idxNum is used to represent the
2859 ** query plan. idxStr is unused.
2861 ** The query plan is represented by values of idxNum:
2863 ** (1) The path value is supplied by argv[0]
2864 ** (2) Path is in argv[0] and dir is in argv[1]
2866 static int fsdirBestIndex(
2868 sqlite3_index_info *pIdxInfo
2870 int i; /* Loop over constraints */
2871 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2872 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2873 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2874 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
2875 const struct sqlite3_index_constraint *pConstraint;
2878 pConstraint = pIdxInfo->aConstraint;
2879 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2880 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2881 switch( pConstraint->iColumn ){
2882 case FSDIR_COLUMN_PATH: {
2883 if( pConstraint->usable ){
2886 }else if( idxPath<0 ){
2891 case FSDIR_COLUMN_DIR: {
2892 if( pConstraint->usable ){
2895 }else if( idxDir<0 ){
2902 if( seenPath || seenDir ){
2903 /* If input parameters are unusable, disallow this plan */
2904 return SQLITE_CONSTRAINT;
2908 pIdxInfo->idxNum = 0;
2909 /* The pIdxInfo->estimatedCost should have been initialized to a huge
2910 ** number. Leave it unchanged. */
2911 pIdxInfo->estimatedRows = 0x7fffffff;
2913 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
2914 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
2916 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
2917 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
2918 pIdxInfo->idxNum = 2;
2919 pIdxInfo->estimatedCost = 10.0;
2921 pIdxInfo->idxNum = 1;
2922 pIdxInfo->estimatedCost = 100.0;
2930 ** Register the "fsdir" virtual table.
2932 static int fsdirRegister(sqlite3 *db){
2933 static sqlite3_module fsdirModule = {
2936 fsdirConnect, /* xConnect */
2937 fsdirBestIndex, /* xBestIndex */
2938 fsdirDisconnect, /* xDisconnect */
2940 fsdirOpen, /* xOpen - open a cursor */
2941 fsdirClose, /* xClose - close a cursor */
2942 fsdirFilter, /* xFilter - configure scan constraints */
2943 fsdirNext, /* xNext - advance a cursor */
2944 fsdirEof, /* xEof - check for end of scan */
2945 fsdirColumn, /* xColumn - read data */
2946 fsdirRowid, /* xRowid - read data */
2952 0, /* xFindMethod */
2956 0, /* xRollbackTo */
2957 0, /* xShadowName */
2960 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2963 #else /* SQLITE_OMIT_VIRTUALTABLE */
2964 # define fsdirRegister(x) SQLITE_OK
2970 int sqlite3_fileio_init(
2973 const sqlite3_api_routines *pApi
2976 SQLITE_EXTENSION_INIT2(pApi);
2977 (void)pzErrMsg; /* Unused parameter */
2978 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
2979 readfileFunc, 0, 0);
2980 if( rc==SQLITE_OK ){
2981 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2982 writefileFunc, 0, 0);
2984 if( rc==SQLITE_OK ){
2985 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2988 if( rc==SQLITE_OK ){
2989 rc = fsdirRegister(db);
2994 /************************* End ../ext/misc/fileio.c ********************/
2995 /************************* Begin ../ext/misc/completion.c ******************/
2999 ** The author disclaims copyright to this source code. In place of
3000 ** a legal notice, here is a blessing:
3002 ** May you do good and not evil.
3003 ** May you find forgiveness for yourself and forgive others.
3004 ** May you share freely, never taking more than you give.
3006 *************************************************************************
3008 ** This file implements an eponymous virtual table that returns suggested
3009 ** completions for a partial SQL input.
3013 ** SELECT DISTINCT candidate COLLATE nocase
3014 ** FROM completion($prefix,$wholeline)
3017 ** The two query parameters are optional. $prefix is the text of the
3018 ** current word being typed and that is to be completed. $wholeline is
3019 ** the complete input line, used for context.
3021 ** The raw completion() table might return the same candidate multiple
3022 ** times, for example if the same column name is used to two or more
3023 ** tables. And the candidates are returned in an arbitrary order. Hence,
3024 ** the DISTINCT and ORDER BY are recommended.
3026 ** This virtual table operates at the speed of human typing, and so there
3027 ** is no attempt to make it fast. Even a slow implementation will be much
3028 ** faster than any human can type.
3031 SQLITE_EXTENSION_INIT1
3036 #ifndef SQLITE_OMIT_VIRTUALTABLE
3038 /* completion_vtab is a subclass of sqlite3_vtab which will
3039 ** serve as the underlying representation of a completion virtual table
3041 typedef struct completion_vtab completion_vtab;
3042 struct completion_vtab {
3043 sqlite3_vtab base; /* Base class - must be first */
3044 sqlite3 *db; /* Database connection for this completion vtab */
3047 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3048 ** serve as the underlying representation of a cursor that scans
3049 ** over rows of the result
3051 typedef struct completion_cursor completion_cursor;
3052 struct completion_cursor {
3053 sqlite3_vtab_cursor base; /* Base class - must be first */
3054 sqlite3 *db; /* Database connection for this cursor */
3055 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3056 char *zPrefix; /* The prefix for the word we want to complete */
3057 char *zLine; /* The whole that we want to complete */
3058 const char *zCurrentRow; /* Current output row */
3059 int szRow; /* Length of the zCurrentRow string */
3060 sqlite3_stmt *pStmt; /* Current statement */
3061 sqlite3_int64 iRowid; /* The rowid */
3062 int ePhase; /* Current phase */
3063 int j; /* inter-phase counter */
3066 /* Values for ePhase:
3068 #define COMPLETION_FIRST_PHASE 1
3069 #define COMPLETION_KEYWORDS 1
3070 #define COMPLETION_PRAGMAS 2
3071 #define COMPLETION_FUNCTIONS 3
3072 #define COMPLETION_COLLATIONS 4
3073 #define COMPLETION_INDEXES 5
3074 #define COMPLETION_TRIGGERS 6
3075 #define COMPLETION_DATABASES 7
3076 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3077 #define COMPLETION_COLUMNS 9
3078 #define COMPLETION_MODULES 10
3079 #define COMPLETION_EOF 11
3082 ** The completionConnect() method is invoked to create a new
3083 ** completion_vtab that describes the completion virtual table.
3085 ** Think of this routine as the constructor for completion_vtab objects.
3087 ** All this routine needs to do is:
3089 ** (1) Allocate the completion_vtab object and initialize all fields.
3091 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3092 ** result set of queries against completion will look like.
3094 static int completionConnect(
3097 int argc, const char *const*argv,
3098 sqlite3_vtab **ppVtab,
3101 completion_vtab *pNew;
3104 (void)(pAux); /* Unused parameter */
3105 (void)(argc); /* Unused parameter */
3106 (void)(argv); /* Unused parameter */
3107 (void)(pzErr); /* Unused parameter */
3109 /* Column numbers */
3110 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3111 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3112 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3113 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3115 rc = sqlite3_declare_vtab(db,
3118 " prefix TEXT HIDDEN,"
3119 " wholeline TEXT HIDDEN,"
3120 " phase INT HIDDEN" /* Used for debugging only */
3122 if( rc==SQLITE_OK ){
3123 pNew = sqlite3_malloc( sizeof(*pNew) );
3124 *ppVtab = (sqlite3_vtab*)pNew;
3125 if( pNew==0 ) return SQLITE_NOMEM;
3126 memset(pNew, 0, sizeof(*pNew));
3133 ** This method is the destructor for completion_cursor objects.
3135 static int completionDisconnect(sqlite3_vtab *pVtab){
3136 sqlite3_free(pVtab);
3141 ** Constructor for a new completion_cursor object.
3143 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3144 completion_cursor *pCur;
3145 pCur = sqlite3_malloc( sizeof(*pCur) );
3146 if( pCur==0 ) return SQLITE_NOMEM;
3147 memset(pCur, 0, sizeof(*pCur));
3148 pCur->db = ((completion_vtab*)p)->db;
3149 *ppCursor = &pCur->base;
3154 ** Reset the completion_cursor.
3156 static void completionCursorReset(completion_cursor *pCur){
3157 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3158 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3159 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3164 ** Destructor for a completion_cursor.
3166 static int completionClose(sqlite3_vtab_cursor *cur){
3167 completionCursorReset((completion_cursor*)cur);
3173 ** Advance a completion_cursor to its next row of output.
3175 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3176 ** record the current state of the scan. This routine sets ->zCurrentRow
3177 ** to the current row of output and then returns. If no more rows remain,
3178 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3179 ** table that has reached the end of its scan.
3181 ** The current implementation just lists potential identifiers and
3182 ** keywords and filters them by zPrefix. Future enhancements should
3183 ** take zLine into account to try to restrict the set of identifiers and
3184 ** keywords based on what would be legal at the current point of input.
3186 static int completionNext(sqlite3_vtab_cursor *cur){
3187 completion_cursor *pCur = (completion_cursor*)cur;
3188 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3189 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3191 while( pCur->ePhase!=COMPLETION_EOF ){
3192 switch( pCur->ePhase ){
3193 case COMPLETION_KEYWORDS: {
3194 if( pCur->j >= sqlite3_keyword_count() ){
3195 pCur->zCurrentRow = 0;
3196 pCur->ePhase = COMPLETION_DATABASES;
3198 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3203 case COMPLETION_DATABASES: {
3204 if( pCur->pStmt==0 ){
3205 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3209 eNextPhase = COMPLETION_TABLES;
3212 case COMPLETION_TABLES: {
3213 if( pCur->pStmt==0 ){
3216 const char *zSep = "";
3217 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3218 while( sqlite3_step(pS2)==SQLITE_ROW ){
3219 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3220 zSql = sqlite3_mprintf(
3222 "SELECT name FROM \"%w\".sqlite_master",
3225 if( zSql==0 ) return SQLITE_NOMEM;
3228 sqlite3_finalize(pS2);
3229 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3233 eNextPhase = COMPLETION_COLUMNS;
3236 case COMPLETION_COLUMNS: {
3237 if( pCur->pStmt==0 ){
3240 const char *zSep = "";
3241 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3242 while( sqlite3_step(pS2)==SQLITE_ROW ){
3243 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3244 zSql = sqlite3_mprintf(
3246 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3247 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3248 " WHERE sm.type='table'",
3249 zSql, zSep, zDb, zDb
3251 if( zSql==0 ) return SQLITE_NOMEM;
3254 sqlite3_finalize(pS2);
3255 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3259 eNextPhase = COMPLETION_EOF;
3264 /* This case is when the phase presets zCurrentRow */
3265 if( pCur->zCurrentRow==0 ) continue;
3267 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3268 /* Extract the next row of content */
3269 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3270 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3272 /* When all rows are finished, advance to the next phase */
3273 sqlite3_finalize(pCur->pStmt);
3275 pCur->ePhase = eNextPhase;
3279 if( pCur->nPrefix==0 ) break;
3280 if( pCur->nPrefix<=pCur->szRow
3281 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3291 ** Return values of columns for the row at which the completion_cursor
3292 ** is currently pointing.
3294 static int completionColumn(
3295 sqlite3_vtab_cursor *cur, /* The cursor */
3296 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3297 int i /* Which column to return */
3299 completion_cursor *pCur = (completion_cursor*)cur;
3301 case COMPLETION_COLUMN_CANDIDATE: {
3302 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3305 case COMPLETION_COLUMN_PREFIX: {
3306 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3309 case COMPLETION_COLUMN_WHOLELINE: {
3310 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3313 case COMPLETION_COLUMN_PHASE: {
3314 sqlite3_result_int(ctx, pCur->ePhase);
3322 ** Return the rowid for the current row. In this implementation, the
3323 ** rowid is the same as the output value.
3325 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3326 completion_cursor *pCur = (completion_cursor*)cur;
3327 *pRowid = pCur->iRowid;
3332 ** Return TRUE if the cursor has been moved off of the last
3335 static int completionEof(sqlite3_vtab_cursor *cur){
3336 completion_cursor *pCur = (completion_cursor*)cur;
3337 return pCur->ePhase >= COMPLETION_EOF;
3341 ** This method is called to "rewind" the completion_cursor object back
3342 ** to the first row of output. This method is always called at least
3343 ** once prior to any call to completionColumn() or completionRowid() or
3346 static int completionFilter(
3347 sqlite3_vtab_cursor *pVtabCursor,
3348 int idxNum, const char *idxStr,
3349 int argc, sqlite3_value **argv
3351 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3353 (void)(idxStr); /* Unused parameter */
3354 (void)(argc); /* Unused parameter */
3355 completionCursorReset(pCur);
3357 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3358 if( pCur->nPrefix>0 ){
3359 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3360 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3365 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3366 if( pCur->nLine>0 ){
3367 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3368 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3371 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3372 int i = pCur->nLine;
3373 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3376 pCur->nPrefix = pCur->nLine - i;
3377 if( pCur->nPrefix>0 ){
3378 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3379 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3383 pCur->ePhase = COMPLETION_FIRST_PHASE;
3384 return completionNext(pVtabCursor);
3388 ** SQLite will invoke this method one or more times while planning a query
3389 ** that uses the completion virtual table. This routine needs to create
3390 ** a query plan for each invocation and compute an estimated cost for that
3393 ** There are two hidden parameters that act as arguments to the table-valued
3394 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3395 ** is available and bit 1 is set if "wholeline" is available.
3397 static int completionBestIndex(
3399 sqlite3_index_info *pIdxInfo
3401 int i; /* Loop over constraints */
3402 int idxNum = 0; /* The query plan bitmask */
3403 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3404 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3405 int nArg = 0; /* Number of arguments that completeFilter() expects */
3406 const struct sqlite3_index_constraint *pConstraint;
3408 (void)(tab); /* Unused parameter */
3409 pConstraint = pIdxInfo->aConstraint;
3410 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3411 if( pConstraint->usable==0 ) continue;
3412 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3413 switch( pConstraint->iColumn ){
3414 case COMPLETION_COLUMN_PREFIX:
3418 case COMPLETION_COLUMN_WHOLELINE:
3425 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3426 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3428 if( wholelineIdx>=0 ){
3429 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3430 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3432 pIdxInfo->idxNum = idxNum;
3433 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3434 pIdxInfo->estimatedRows = 500 - 100*nArg;
3439 ** This following structure defines all the methods for the
3440 ** completion virtual table.
3442 static sqlite3_module completionModule = {
3445 completionConnect, /* xConnect */
3446 completionBestIndex, /* xBestIndex */
3447 completionDisconnect, /* xDisconnect */
3449 completionOpen, /* xOpen - open a cursor */
3450 completionClose, /* xClose - close a cursor */
3451 completionFilter, /* xFilter - configure scan constraints */
3452 completionNext, /* xNext - advance a cursor */
3453 completionEof, /* xEof - check for end of scan */
3454 completionColumn, /* xColumn - read data */
3455 completionRowid, /* xRowid - read data */
3461 0, /* xFindMethod */
3465 0, /* xRollbackTo */
3469 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3471 int sqlite3CompletionVtabInit(sqlite3 *db){
3473 #ifndef SQLITE_OMIT_VIRTUALTABLE
3474 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3482 int sqlite3_completion_init(
3485 const sqlite3_api_routines *pApi
3488 SQLITE_EXTENSION_INIT2(pApi);
3489 (void)(pzErrMsg); /* Unused parameter */
3490 #ifndef SQLITE_OMIT_VIRTUALTABLE
3491 rc = sqlite3CompletionVtabInit(db);
3496 /************************* End ../ext/misc/completion.c ********************/
3497 /************************* Begin ../ext/misc/appendvfs.c ******************/
3501 ** The author disclaims copyright to this source code. In place of
3502 ** a legal notice, here is a blessing:
3504 ** May you do good and not evil.
3505 ** May you find forgiveness for yourself and forgive others.
3506 ** May you share freely, never taking more than you give.
3508 ******************************************************************************
3510 ** This file implements a VFS shim that allows an SQLite database to be
3511 ** appended onto the end of some other file, such as an executable.
3513 ** A special record must appear at the end of the file that identifies the
3514 ** file as an appended database and provides an offset to page 1. For
3515 ** best performance page 1 should be located at a disk page boundary, though
3516 ** that is not required.
3518 ** When opening a database using this VFS, the connection might treat
3519 ** the file as an ordinary SQLite database, or it might treat is as a
3520 ** database appended onto some other file. Here are the rules:
3522 ** (1) When opening a new empty file, that file is treated as an ordinary
3525 ** (2) When opening a file that begins with the standard SQLite prefix
3526 ** string "SQLite format 3", that file is treated as an ordinary
3529 ** (3) When opening a file that ends with the appendvfs trailer string
3530 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3533 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3534 ** set, then a new database is appended to the already existing file.
3536 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3538 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3539 ** the file containing the database is limited to 1GB. This VFS will refuse
3540 ** to read or write past the 1GB mark. This restriction might be lifted in
3541 ** future versions. For now, if you need a large database, then keep the
3542 ** database in a separate file.
3544 ** If the file being opened is not an appended database, then this shim is
3545 ** a pass-through into the default underlying VFS.
3547 SQLITE_EXTENSION_INIT1
3551 /* The append mark at the end of the database is:
3553 ** Start-Of-SQLite3-NNNNNNNN
3554 ** 123456789 123456789 12345
3556 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3557 ** the offset to page 1.
3559 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3560 #define APND_MARK_PREFIX_SZ 17
3561 #define APND_MARK_SIZE 25
3564 ** Maximum size of the combined prefix + database + append-mark. This
3565 ** must be less than 0x40000000 to avoid locking issues on Windows.
3567 #define APND_MAX_SIZE (65536*15259)
3570 ** Forward declaration of objects used by this utility
3572 typedef struct sqlite3_vfs ApndVfs;
3573 typedef struct ApndFile ApndFile;
3575 /* Access to a lower-level VFS that (might) implement dynamic loading,
3576 ** access to randomness, etc.
3578 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3579 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3583 sqlite3_file base; /* IO methods */
3584 sqlite3_int64 iPgOne; /* File offset to page 1 */
3585 sqlite3_int64 iMark; /* Start of the append-mark */
3589 ** Methods for ApndFile
3591 static int apndClose(sqlite3_file*);
3592 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3593 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3594 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3595 static int apndSync(sqlite3_file*, int flags);
3596 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3597 static int apndLock(sqlite3_file*, int);
3598 static int apndUnlock(sqlite3_file*, int);
3599 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3600 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3601 static int apndSectorSize(sqlite3_file*);
3602 static int apndDeviceCharacteristics(sqlite3_file*);
3603 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3604 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3605 static void apndShmBarrier(sqlite3_file*);
3606 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3607 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3608 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3611 ** Methods for ApndVfs
3613 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3614 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3615 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3616 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3617 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3618 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3619 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3620 static void apndDlClose(sqlite3_vfs*, void*);
3621 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3622 static int apndSleep(sqlite3_vfs*, int microseconds);
3623 static int apndCurrentTime(sqlite3_vfs*, double*);
3624 static int apndGetLastError(sqlite3_vfs*, int, char *);
3625 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3626 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3627 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3628 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3630 static sqlite3_vfs apnd_vfs = {
3631 3, /* iVersion (set when registered) */
3632 0, /* szOsFile (set when registered) */
3633 1024, /* mxPathname */
3635 "apndvfs", /* zName */
3636 0, /* pAppData (set when registered) */
3637 apndOpen, /* xOpen */
3638 apndDelete, /* xDelete */
3639 apndAccess, /* xAccess */
3640 apndFullPathname, /* xFullPathname */
3641 apndDlOpen, /* xDlOpen */
3642 apndDlError, /* xDlError */
3643 apndDlSym, /* xDlSym */
3644 apndDlClose, /* xDlClose */
3645 apndRandomness, /* xRandomness */
3646 apndSleep, /* xSleep */
3647 apndCurrentTime, /* xCurrentTime */
3648 apndGetLastError, /* xGetLastError */
3649 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3650 apndSetSystemCall, /* xSetSystemCall */
3651 apndGetSystemCall, /* xGetSystemCall */
3652 apndNextSystemCall /* xNextSystemCall */
3655 static const sqlite3_io_methods apnd_io_methods = {
3657 apndClose, /* xClose */
3658 apndRead, /* xRead */
3659 apndWrite, /* xWrite */
3660 apndTruncate, /* xTruncate */
3661 apndSync, /* xSync */
3662 apndFileSize, /* xFileSize */
3663 apndLock, /* xLock */
3664 apndUnlock, /* xUnlock */
3665 apndCheckReservedLock, /* xCheckReservedLock */
3666 apndFileControl, /* xFileControl */
3667 apndSectorSize, /* xSectorSize */
3668 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3669 apndShmMap, /* xShmMap */
3670 apndShmLock, /* xShmLock */
3671 apndShmBarrier, /* xShmBarrier */
3672 apndShmUnmap, /* xShmUnmap */
3673 apndFetch, /* xFetch */
3674 apndUnfetch /* xUnfetch */
3680 ** Close an apnd-file.
3682 static int apndClose(sqlite3_file *pFile){
3683 pFile = ORIGFILE(pFile);
3684 return pFile->pMethods->xClose(pFile);
3688 ** Read data from an apnd-file.
3690 static int apndRead(
3691 sqlite3_file *pFile,
3696 ApndFile *p = (ApndFile *)pFile;
3697 pFile = ORIGFILE(pFile);
3698 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3702 ** Add the append-mark onto the end of the file.
3704 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3706 unsigned char a[APND_MARK_SIZE];
3707 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3709 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3711 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3715 ** Write data to an apnd-file.
3717 static int apndWrite(
3718 sqlite3_file *pFile,
3724 ApndFile *p = (ApndFile *)pFile;
3725 pFile = ORIGFILE(pFile);
3726 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3727 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3728 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3729 sqlite3_int64 sz = 0;
3730 rc = pFile->pMethods->xFileSize(pFile, &sz);
3731 if( rc==SQLITE_OK ){
3732 p->iMark = sz - APND_MARK_SIZE;
3733 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3734 p->iMark = p->iPgOne + iOfst + iAmt;
3735 rc = apndWriteMark(p, pFile);
3743 ** Truncate an apnd-file.
3745 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3747 ApndFile *p = (ApndFile *)pFile;
3748 pFile = ORIGFILE(pFile);
3749 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3750 if( rc==SQLITE_OK ){
3751 p->iMark = p->iPgOne+size;
3752 rc = apndWriteMark(p, pFile);
3758 ** Sync an apnd-file.
3760 static int apndSync(sqlite3_file *pFile, int flags){
3761 pFile = ORIGFILE(pFile);
3762 return pFile->pMethods->xSync(pFile, flags);
3766 ** Return the current file-size of an apnd-file.
3768 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3769 ApndFile *p = (ApndFile *)pFile;
3771 pFile = ORIGFILE(p);
3772 rc = pFile->pMethods->xFileSize(pFile, pSize);
3773 if( rc==SQLITE_OK && p->iPgOne ){
3774 *pSize -= p->iPgOne + APND_MARK_SIZE;
3780 ** Lock an apnd-file.
3782 static int apndLock(sqlite3_file *pFile, int eLock){
3783 pFile = ORIGFILE(pFile);
3784 return pFile->pMethods->xLock(pFile, eLock);
3788 ** Unlock an apnd-file.
3790 static int apndUnlock(sqlite3_file *pFile, int eLock){
3791 pFile = ORIGFILE(pFile);
3792 return pFile->pMethods->xUnlock(pFile, eLock);
3796 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3798 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3799 pFile = ORIGFILE(pFile);
3800 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3804 ** File control method. For custom operations on an apnd-file.
3806 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3807 ApndFile *p = (ApndFile *)pFile;
3809 pFile = ORIGFILE(pFile);
3810 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3811 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3812 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3818 ** Return the sector-size in bytes for an apnd-file.
3820 static int apndSectorSize(sqlite3_file *pFile){
3821 pFile = ORIGFILE(pFile);
3822 return pFile->pMethods->xSectorSize(pFile);
3826 ** Return the device characteristic flags supported by an apnd-file.
3828 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3829 pFile = ORIGFILE(pFile);
3830 return pFile->pMethods->xDeviceCharacteristics(pFile);
3833 /* Create a shared memory file mapping */
3834 static int apndShmMap(
3835 sqlite3_file *pFile,
3841 pFile = ORIGFILE(pFile);
3842 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3845 /* Perform locking on a shared-memory segment */
3846 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3847 pFile = ORIGFILE(pFile);
3848 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3851 /* Memory barrier operation on shared memory */
3852 static void apndShmBarrier(sqlite3_file *pFile){
3853 pFile = ORIGFILE(pFile);
3854 pFile->pMethods->xShmBarrier(pFile);
3857 /* Unmap a shared memory segment */
3858 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3859 pFile = ORIGFILE(pFile);
3860 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3863 /* Fetch a page of a memory-mapped file */
3864 static int apndFetch(
3865 sqlite3_file *pFile,
3866 sqlite3_int64 iOfst,
3870 ApndFile *p = (ApndFile *)pFile;
3871 pFile = ORIGFILE(pFile);
3872 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3875 /* Release a memory-mapped page */
3876 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3877 ApndFile *p = (ApndFile *)pFile;
3878 pFile = ORIGFILE(pFile);
3879 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3883 ** Check to see if the file is an ordinary SQLite database file.
3885 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3888 static const char aSqliteHdr[] = "SQLite format 3";
3889 if( sz<512 ) return 0;
3890 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3892 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3896 ** Try to read the append-mark off the end of a file. Return the
3897 ** start of the appended database if the append-mark is present. If
3898 ** there is no append-mark, return -1;
3900 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3902 sqlite3_int64 iMark;
3903 unsigned char a[APND_MARK_SIZE];
3905 if( sz<=APND_MARK_SIZE ) return -1;
3906 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3908 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3909 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3911 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3917 ** Open an apnd file handle.
3919 static int apndOpen(
3922 sqlite3_file *pFile,
3927 sqlite3_file *pSubFile;
3928 sqlite3_vfs *pSubVfs;
3931 pSubVfs = ORIGVFS(pVfs);
3932 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3933 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3935 p = (ApndFile*)pFile;
3936 memset(p, 0, sizeof(*p));
3937 pSubFile = ORIGFILE(pFile);
3938 p->base.pMethods = &apnd_io_methods;
3939 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3940 if( rc ) goto apnd_open_done;
3941 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3943 pSubFile->pMethods->xClose(pSubFile);
3944 goto apnd_open_done;
3946 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3947 memmove(pFile, pSubFile, pSubVfs->szOsFile);
3951 p->iPgOne = apndReadMark(sz, pFile);
3955 if( (flags & SQLITE_OPEN_CREATE)==0 ){
3956 pSubFile->pMethods->xClose(pSubFile);
3957 rc = SQLITE_CANTOPEN;
3959 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3961 if( rc ) pFile->pMethods = 0;
3966 ** All other VFS methods are pass-thrus.
3968 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3969 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3971 static int apndAccess(
3977 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
3979 static int apndFullPathname(
3985 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
3987 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3988 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
3990 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3991 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
3993 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
3994 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
3996 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
3997 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
3999 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4000 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4002 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4003 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4005 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4006 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4008 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4009 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4011 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4012 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4014 static int apndSetSystemCall(
4017 sqlite3_syscall_ptr pCall
4019 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4021 static sqlite3_syscall_ptr apndGetSystemCall(
4025 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4027 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4028 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4036 ** This routine is called when the extension is loaded.
4037 ** Register the new VFS.
4039 int sqlite3_appendvfs_init(
4042 const sqlite3_api_routines *pApi
4046 SQLITE_EXTENSION_INIT2(pApi);
4049 pOrig = sqlite3_vfs_find(0);
4050 apnd_vfs.iVersion = pOrig->iVersion;
4051 apnd_vfs.pAppData = pOrig;
4052 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4053 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4054 #ifdef APPENDVFS_TEST
4055 if( rc==SQLITE_OK ){
4056 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4059 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4063 /************************* End ../ext/misc/appendvfs.c ********************/
4064 #ifdef SQLITE_HAVE_ZLIB
4065 /************************* Begin ../ext/misc/zipfile.c ******************/
4069 ** The author disclaims copyright to this source code. In place of
4070 ** a legal notice, here is a blessing:
4072 ** May you do good and not evil.
4073 ** May you find forgiveness for yourself and forgive others.
4074 ** May you share freely, never taking more than you give.
4076 ******************************************************************************
4078 ** This file implements a virtual table for reading and writing ZIP archive
4083 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4085 ** Current limitations:
4087 ** * No support for encryption
4088 ** * No support for ZIP archives spanning multiple files
4089 ** * No support for zip64 extensions
4090 ** * Only the "inflate/deflate" (zlib) compression method is supported
4092 SQLITE_EXTENSION_INIT1
4099 #ifndef SQLITE_OMIT_VIRTUALTABLE
4101 #ifndef SQLITE_AMALGAMATION
4103 /* typedef sqlite3_int64 i64; */
4104 /* typedef unsigned char u8; */
4105 typedef unsigned short u16;
4106 typedef unsigned long u32;
4107 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4109 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4110 # define ALWAYS(X) (1)
4111 # define NEVER(X) (0)
4112 #elif !defined(NDEBUG)
4113 # define ALWAYS(X) ((X)?1:(assert(0),0))
4114 # define NEVER(X) ((X)?(assert(0),1):0)
4116 # define ALWAYS(X) (X)
4117 # define NEVER(X) (X)
4120 #endif /* SQLITE_AMALGAMATION */
4123 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4125 ** In some ways it would be better to obtain these values from system
4126 ** header files. But, the dependency is undesirable and (a) these
4127 ** have been stable for decades, (b) the values are part of POSIX and
4128 ** are also made explicit in [man stat], and (c) are part of the
4129 ** file format for zip archives.
4132 # define S_IFDIR 0040000
4135 # define S_IFREG 0100000
4138 # define S_IFLNK 0120000
4141 static const char ZIPFILE_SCHEMA[] =
4143 "name PRIMARY KEY," /* 0: Name of file in zip archive */
4144 "mode," /* 1: POSIX mode for file */
4145 "mtime," /* 2: Last modification time (secs since 1970)*/
4146 "sz," /* 3: Size of object */
4147 "rawdata," /* 4: Raw data */
4148 "data," /* 5: Uncompressed data */
4149 "method," /* 6: Compression method (integer) */
4150 "z HIDDEN" /* 7: Name of zip file */
4153 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
4154 #define ZIPFILE_BUFFER_SIZE (64*1024)
4158 ** Magic numbers used to read and write zip files.
4160 ** ZIPFILE_NEWENTRY_MADEBY:
4161 ** Use this value for the "version-made-by" field in new zip file
4162 ** entries. The upper byte indicates "unix", and the lower byte
4163 ** indicates that the zip file matches pkzip specification 3.0.
4164 ** This is what info-zip seems to do.
4166 ** ZIPFILE_NEWENTRY_REQUIRED:
4167 ** Value for "version-required-to-extract" field of new entries.
4168 ** Version 2.0 is required to support folders and deflate compression.
4170 ** ZIPFILE_NEWENTRY_FLAGS:
4171 ** Value for "general-purpose-bit-flags" field of new entries. Bit
4172 ** 11 means "utf-8 filename and comment".
4174 ** ZIPFILE_SIGNATURE_CDS:
4175 ** First 4 bytes of a valid CDS record.
4177 ** ZIPFILE_SIGNATURE_LFH:
4178 ** First 4 bytes of a valid LFH record.
4180 ** ZIPFILE_SIGNATURE_EOCD
4181 ** First 4 bytes of a valid EOCD record.
4183 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4184 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4185 #define ZIPFILE_NEWENTRY_REQUIRED 20
4186 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4187 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4188 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4189 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4192 ** The sizes of the fixed-size part of each of the three main data
4193 ** structures in a zip archive.
4195 #define ZIPFILE_LFH_FIXED_SZ 30
4196 #define ZIPFILE_EOCD_FIXED_SZ 22
4197 #define ZIPFILE_CDS_FIXED_SZ 46
4200 *** 4.3.16 End of central directory record:
4202 *** end of central dir signature 4 bytes (0x06054b50)
4203 *** number of this disk 2 bytes
4204 *** number of the disk with the
4205 *** start of the central directory 2 bytes
4206 *** total number of entries in the
4207 *** central directory on this disk 2 bytes
4208 *** total number of entries in
4209 *** the central directory 2 bytes
4210 *** size of the central directory 4 bytes
4211 *** offset of start of central
4212 *** directory with respect to
4213 *** the starting disk number 4 bytes
4214 *** .ZIP file comment length 2 bytes
4215 *** .ZIP file comment (variable size)
4217 typedef struct ZipfileEOCD ZipfileEOCD;
4218 struct ZipfileEOCD {
4228 *** 4.3.12 Central directory structure:
4232 *** central file header signature 4 bytes (0x02014b50)
4233 *** version made by 2 bytes
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
4244 *** file comment length 2 bytes
4245 *** disk number start 2 bytes
4246 *** internal file attributes 2 bytes
4247 *** external file attributes 4 bytes
4248 *** relative offset of local header 4 bytes
4250 typedef struct ZipfileCDS ZipfileCDS;
4253 u16 iVersionExtract;
4268 char *zFile; /* Filename (sqlite3_malloc()) */
4272 *** 4.3.7 Local file header:
4274 *** local file header signature 4 bytes (0x04034b50)
4275 *** version needed to extract 2 bytes
4276 *** general purpose bit flag 2 bytes
4277 *** compression method 2 bytes
4278 *** last mod file time 2 bytes
4279 *** last mod file date 2 bytes
4281 *** compressed size 4 bytes
4282 *** uncompressed size 4 bytes
4283 *** file name length 2 bytes
4284 *** extra field length 2 bytes
4287 typedef struct ZipfileLFH ZipfileLFH;
4289 u16 iVersionExtract;
4301 typedef struct ZipfileEntry ZipfileEntry;
4302 struct ZipfileEntry {
4303 ZipfileCDS cds; /* Parsed CDS record */
4304 u32 mUnixTime; /* Modification time, in UNIX format */
4305 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4306 i64 iDataOff; /* Offset to data in file (if aData==0) */
4307 u8 *aData; /* cds.szCompressed bytes of compressed data */
4308 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4312 ** Cursor type for zipfile tables.
4314 typedef struct ZipfileCsr ZipfileCsr;
4316 sqlite3_vtab_cursor base; /* Base class - must be first */
4317 i64 iId; /* Cursor ID */
4318 u8 bEof; /* True when at EOF */
4319 u8 bNoop; /* If next xNext() call is no-op */
4321 /* Used outside of write transactions */
4322 FILE *pFile; /* Zip file */
4323 i64 iNextOff; /* Offset of next record in central directory */
4324 ZipfileEOCD eocd; /* Parse of central directory record */
4326 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4327 ZipfileEntry *pCurrent; /* Current entry */
4328 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4331 typedef struct ZipfileTab ZipfileTab;
4333 sqlite3_vtab base; /* Base class - must be first */
4334 char *zFile; /* Zip file this table accesses (may be NULL) */
4335 sqlite3 *db; /* Host database connection */
4336 u8 *aBuffer; /* Temporary buffer used for various tasks */
4338 ZipfileCsr *pCsrList; /* List of cursors */
4341 /* The following are used by write transactions only */
4342 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4343 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
4344 FILE *pWriteFd; /* File handle open on zip archive */
4345 i64 szCurrent; /* Current size of zip archive */
4346 i64 szOrig; /* Size of archive at start of transaction */
4350 ** Set the error message contained in context ctx to the results of
4351 ** vprintf(zFmt, ...).
4353 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4357 zMsg = sqlite3_vmprintf(zFmt, ap);
4358 sqlite3_result_error(ctx, zMsg, -1);
4364 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4365 ** is not quoted, do nothing.
4367 static void zipfileDequote(char *zIn){
4369 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4372 if( q=='[' ) q = ']';
4373 while( ALWAYS(zIn[iIn]) ){
4374 char c = zIn[iIn++];
4375 if( c==q && zIn[iIn++]!=q ) break;
4383 ** Construct a new ZipfileTab virtual table object.
4385 ** argv[0] -> module name ("zipfile")
4386 ** argv[1] -> database name
4387 ** argv[2] -> table name
4388 ** argv[...] -> "column name" and other module argument fields.
4390 static int zipfileConnect(
4393 int argc, const char *const*argv,
4394 sqlite3_vtab **ppVtab,
4397 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4399 const char *zFile = 0;
4400 ZipfileTab *pNew = 0;
4403 /* If the table name is not "zipfile", require that the argument be
4404 ** specified. This stops zipfile tables from being created as:
4406 ** CREATE VIRTUAL TABLE zzz USING zipfile();
4408 ** It does not prevent:
4410 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4412 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4413 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4414 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4415 return SQLITE_ERROR;
4420 nFile = (int)strlen(zFile)+1;
4423 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4424 if( rc==SQLITE_OK ){
4425 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4426 if( pNew==0 ) return SQLITE_NOMEM;
4427 memset(pNew, 0, nByte+nFile);
4429 pNew->aBuffer = (u8*)&pNew[1];
4431 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4432 memcpy(pNew->zFile, zFile, nFile);
4433 zipfileDequote(pNew->zFile);
4436 *ppVtab = (sqlite3_vtab*)pNew;
4441 ** Free the ZipfileEntry structure indicated by the only argument.
4443 static void zipfileEntryFree(ZipfileEntry *p){
4445 sqlite3_free(p->cds.zFile);
4451 ** Release resources that should be freed at the end of a write
4454 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4455 ZipfileEntry *pEntry;
4456 ZipfileEntry *pNext;
4458 if( pTab->pWriteFd ){
4459 fclose(pTab->pWriteFd);
4462 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4463 pNext = pEntry->pNext;
4464 zipfileEntryFree(pEntry);
4466 pTab->pFirstEntry = 0;
4467 pTab->pLastEntry = 0;
4468 pTab->szCurrent = 0;
4473 ** This method is the destructor for zipfile vtab objects.
4475 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4476 zipfileCleanupTransaction((ZipfileTab*)pVtab);
4477 sqlite3_free(pVtab);
4482 ** Constructor for a new ZipfileCsr object.
4484 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4485 ZipfileTab *pTab = (ZipfileTab*)p;
4487 pCsr = sqlite3_malloc(sizeof(*pCsr));
4488 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4490 return SQLITE_NOMEM;
4492 memset(pCsr, 0, sizeof(*pCsr));
4493 pCsr->iId = ++pTab->iNextCsrid;
4494 pCsr->pCsrNext = pTab->pCsrList;
4495 pTab->pCsrList = pCsr;
4500 ** Reset a cursor back to the state it was in when first returned
4501 ** by zipfileOpen().
4503 static void zipfileResetCursor(ZipfileCsr *pCsr){
4505 ZipfileEntry *pNext;
4509 fclose(pCsr->pFile);
4511 zipfileEntryFree(pCsr->pCurrent);
4515 for(p=pCsr->pFreeEntry; p; p=pNext){
4517 zipfileEntryFree(p);
4522 ** Destructor for an ZipfileCsr.
4524 static int zipfileClose(sqlite3_vtab_cursor *cur){
4525 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4526 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4528 zipfileResetCursor(pCsr);
4530 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4531 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4532 *pp = pCsr->pCsrNext;
4539 ** Set the error message for the virtual table associated with cursor
4540 ** pCsr to the results of vprintf(zFmt, ...).
4542 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4545 sqlite3_free(pTab->base.zErrMsg);
4546 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4549 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4552 sqlite3_free(pCsr->base.pVtab->zErrMsg);
4553 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4558 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4559 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4562 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4563 ** to an English language error message. It is the responsibility of the
4564 ** caller to eventually free this buffer using
4567 static int zipfileReadData(
4568 FILE *pFile, /* Read from this file */
4569 u8 *aRead, /* Read into this buffer */
4570 int nRead, /* Number of bytes to read */
4571 i64 iOff, /* Offset to read from */
4572 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
4575 fseek(pFile, (long)iOff, SEEK_SET);
4576 n = fread(aRead, 1, nRead, pFile);
4577 if( (int)n!=nRead ){
4578 *pzErrmsg = sqlite3_mprintf("error in fread()");
4579 return SQLITE_ERROR;
4584 static int zipfileAppendData(
4590 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4591 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4592 if( (int)n!=nWrite ){
4593 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4594 return SQLITE_ERROR;
4596 pTab->szCurrent += nWrite;
4601 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4603 static u16 zipfileGetU16(const u8 *aBuf){
4604 return (aBuf[1] << 8) + aBuf[0];
4608 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4610 static u32 zipfileGetU32(const u8 *aBuf){
4611 return ((u32)(aBuf[3]) << 24)
4612 + ((u32)(aBuf[2]) << 16)
4613 + ((u32)(aBuf[1]) << 8)
4614 + ((u32)(aBuf[0]) << 0);
4618 ** Write a 16-bit little endiate integer into buffer aBuf.
4620 static void zipfilePutU16(u8 *aBuf, u16 val){
4621 aBuf[0] = val & 0xFF;
4622 aBuf[1] = (val>>8) & 0xFF;
4626 ** Write a 32-bit little endiate integer into buffer aBuf.
4628 static void zipfilePutU32(u8 *aBuf, u32 val){
4629 aBuf[0] = val & 0xFF;
4630 aBuf[1] = (val>>8) & 0xFF;
4631 aBuf[2] = (val>>16) & 0xFF;
4632 aBuf[3] = (val>>24) & 0xFF;
4635 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4636 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4638 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4639 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4642 ** Magic numbers used to read CDS records.
4644 #define ZIPFILE_CDS_NFILE_OFF 28
4645 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4648 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4649 ** if the record is not well-formed, or SQLITE_OK otherwise.
4651 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4653 u32 sig = zipfileRead32(aRead);
4655 if( sig!=ZIPFILE_SIGNATURE_CDS ){
4658 pCDS->iVersionMadeBy = zipfileRead16(aRead);
4659 pCDS->iVersionExtract = zipfileRead16(aRead);
4660 pCDS->flags = zipfileRead16(aRead);
4661 pCDS->iCompression = zipfileRead16(aRead);
4662 pCDS->mTime = zipfileRead16(aRead);
4663 pCDS->mDate = zipfileRead16(aRead);
4664 pCDS->crc32 = zipfileRead32(aRead);
4665 pCDS->szCompressed = zipfileRead32(aRead);
4666 pCDS->szUncompressed = zipfileRead32(aRead);
4667 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4668 pCDS->nFile = zipfileRead16(aRead);
4669 pCDS->nExtra = zipfileRead16(aRead);
4670 pCDS->nComment = zipfileRead16(aRead);
4671 pCDS->iDiskStart = zipfileRead16(aRead);
4672 pCDS->iInternalAttr = zipfileRead16(aRead);
4673 pCDS->iExternalAttr = zipfileRead32(aRead);
4674 pCDS->iOffset = zipfileRead32(aRead);
4675 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4682 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4683 ** if the record is not well-formed, or SQLITE_OK otherwise.
4685 static int zipfileReadLFH(
4689 u8 *aRead = aBuffer;
4692 u32 sig = zipfileRead32(aRead);
4693 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4696 pLFH->iVersionExtract = zipfileRead16(aRead);
4697 pLFH->flags = zipfileRead16(aRead);
4698 pLFH->iCompression = zipfileRead16(aRead);
4699 pLFH->mTime = zipfileRead16(aRead);
4700 pLFH->mDate = zipfileRead16(aRead);
4701 pLFH->crc32 = zipfileRead32(aRead);
4702 pLFH->szCompressed = zipfileRead32(aRead);
4703 pLFH->szUncompressed = zipfileRead32(aRead);
4704 pLFH->nFile = zipfileRead16(aRead);
4705 pLFH->nExtra = zipfileRead16(aRead);
4712 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4713 ** Scan through this buffer to find an "extra-timestamp" field. If one
4714 ** exists, extract the 32-bit modification-timestamp from it and store
4715 ** the value in output parameter *pmTime.
4717 ** Zero is returned if no extra-timestamp record could be found (and so
4718 ** *pmTime is left unchanged), or non-zero otherwise.
4720 ** The general format of an extra field is:
4722 ** Header ID 2 bytes
4723 ** Data Size 2 bytes
4726 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4729 u8 *pEnd = &aExtra[nExtra];
4732 u16 id = zipfileRead16(p);
4733 u16 nByte = zipfileRead16(p);
4736 case ZIPFILE_EXTRA_TIMESTAMP: {
4738 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4739 *pmTime = zipfileGetU32(&p[1]);
4752 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4753 ** fields of the CDS structure passed as the only argument to a 32-bit
4754 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4756 ** "Standard" MS-DOS time format:
4758 ** File modification time:
4759 ** Bits 00-04: seconds divided by 2
4760 ** Bits 05-10: minute
4762 ** File modification date:
4764 ** Bits 05-08: month (1-12)
4765 ** Bits 09-15: years from 1980
4767 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4769 static u32 zipfileMtime(ZipfileCDS *pCDS){
4770 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4771 int M = ((pCDS->mDate >> 5) & 0x0F);
4772 int D = (pCDS->mDate & 0x1F);
4775 int sec = (pCDS->mTime & 0x1F)*2;
4776 int min = (pCDS->mTime >> 5) & 0x3F;
4777 int hr = (pCDS->mTime >> 11) & 0x1F;
4780 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4782 /* Calculate the JD in seconds for noon on the day in question */
4787 JD = (i64)(24*60*60) * (
4788 (int)(365.25 * (Y + 4716))
4789 + (int)(30.6001 * (M + 1))
4793 /* Correct the JD for the time within the day */
4794 JD += (hr-12) * 3600 + min * 60 + sec;
4796 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4797 return (u32)(JD - (i64)(24405875) * 24*60*6);
4801 ** The opposite of zipfileMtime(). This function populates the mTime and
4802 ** mDate fields of the CDS structure passed as the first argument according
4803 ** to the UNIX timestamp value passed as the second.
4805 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4806 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4807 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4813 A = (int)((JD - 1867216.25)/36524.25);
4814 A = (int)(JD + 1 + A - (A/4));
4816 C = (int)((B - 122.1)/365.25);
4817 D = (36525*(C&32767))/100;
4818 E = (int)((B-D)/30.6001);
4820 day = B - D - (int)(30.6001*E);
4821 mon = (E<14 ? E-1 : E-13);
4822 yr = mon>2 ? C-4716 : C-4715;
4824 hr = (mUnixTime % (24*60*60)) / (60*60);
4825 min = (mUnixTime % (60*60)) / 60;
4826 sec = (mUnixTime % 60);
4829 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4830 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4832 pCds->mDate = pCds->mTime = 0;
4835 assert( mUnixTime<315507600
4836 || mUnixTime==zipfileMtime(pCds)
4837 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4838 /* || (mUnixTime % 2) */
4843 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4844 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4845 ** then pFile is a file-handle open on a zip file. In either case, this
4846 ** function creates a ZipfileEntry object based on the zip archive entry
4847 ** for which the CDS record is at offset iOff.
4849 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4850 ** the new object. Otherwise, an SQLite error code is returned and the
4851 ** final value of (*ppEntry) undefined.
4853 static int zipfileGetEntry(
4854 ZipfileTab *pTab, /* Store any error message here */
4855 const u8 *aBlob, /* Pointer to in-memory file image */
4856 int nBlob, /* Size of aBlob[] in bytes */
4857 FILE *pFile, /* If aBlob==0, read from this file */
4858 i64 iOff, /* Offset of CDS record */
4859 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
4862 char **pzErr = &pTab->base.zErrMsg;
4866 aRead = pTab->aBuffer;
4867 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
4869 aRead = (u8*)&aBlob[iOff];
4872 if( rc==SQLITE_OK ){
4876 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
4877 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
4878 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
4880 nAlloc = sizeof(ZipfileEntry) + nExtra;
4882 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
4885 pNew = (ZipfileEntry*)sqlite3_malloc(nAlloc);
4889 memset(pNew, 0, sizeof(ZipfileEntry));
4890 rc = zipfileReadCDS(aRead, &pNew->cds);
4891 if( rc!=SQLITE_OK ){
4892 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
4893 }else if( aBlob==0 ){
4894 rc = zipfileReadData(
4895 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
4898 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
4902 if( rc==SQLITE_OK ){
4903 u32 *pt = &pNew->mUnixTime;
4904 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
4905 pNew->aExtra = (u8*)&pNew[1];
4906 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
4907 if( pNew->cds.zFile==0 ){
4909 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
4910 pNew->mUnixTime = zipfileMtime(&pNew->cds);
4914 if( rc==SQLITE_OK ){
4915 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4918 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
4920 aRead = (u8*)&aBlob[pNew->cds.iOffset];
4923 rc = zipfileReadLFH(aRead, &lfh);
4924 if( rc==SQLITE_OK ){
4925 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4926 pNew->iDataOff += lfh.nFile + lfh.nExtra;
4927 if( aBlob && pNew->cds.szCompressed ){
4928 pNew->aData = &pNew->aExtra[nExtra];
4929 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
4932 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
4933 (int)pNew->cds.iOffset
4938 if( rc!=SQLITE_OK ){
4939 zipfileEntryFree(pNew);
4949 ** Advance an ZipfileCsr to its next row of output.
4951 static int zipfileNext(sqlite3_vtab_cursor *cur){
4952 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4956 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4957 zipfileEntryFree(pCsr->pCurrent);
4959 if( pCsr->iNextOff>=iEof ){
4962 ZipfileEntry *p = 0;
4963 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
4964 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
4965 if( rc==SQLITE_OK ){
4966 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4967 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
4973 pCsr->pCurrent = pCsr->pCurrent->pNext;
4975 if( pCsr->pCurrent==0 ){
4984 static void zipfileFree(void *p) {
4989 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
4990 ** size is nOut bytes. This function uncompresses the data and sets the
4991 ** return value in context pCtx to the result (a blob).
4993 ** If an error occurs, an error code is left in pCtx instead.
4995 static void zipfileInflate(
4996 sqlite3_context *pCtx, /* Store result here */
4997 const u8 *aIn, /* Compressed data */
4998 int nIn, /* Size of buffer aIn[] in bytes */
4999 int nOut /* Expected output size */
5001 u8 *aRes = sqlite3_malloc(nOut);
5003 sqlite3_result_error_nomem(pCtx);
5007 memset(&str, 0, sizeof(str));
5009 str.next_in = (Byte*)aIn;
5011 str.next_out = (Byte*)aRes;
5012 str.avail_out = nOut;
5014 err = inflateInit2(&str, -15);
5016 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5018 err = inflate(&str, Z_NO_FLUSH);
5019 if( err!=Z_STREAM_END ){
5020 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5022 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5032 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5033 ** compresses it and sets (*ppOut) to point to a buffer containing the
5034 ** compressed data. The caller is responsible for eventually calling
5035 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
5036 ** is set to the size of buffer (*ppOut) in bytes.
5038 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5039 ** code is returned and an error message left in virtual-table handle
5040 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5043 static int zipfileDeflate(
5044 const u8 *aIn, int nIn, /* Input */
5045 u8 **ppOut, int *pnOut, /* Output */
5046 char **pzErr /* OUT: Error message */
5048 int nAlloc = (int)compressBound(nIn);
5052 aOut = (u8*)sqlite3_malloc(nAlloc);
5058 memset(&str, 0, sizeof(str));
5059 str.next_in = (Bytef*)aIn;
5061 str.next_out = aOut;
5062 str.avail_out = nAlloc;
5064 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5065 res = deflate(&str, Z_FINISH);
5067 if( res==Z_STREAM_END ){
5069 *pnOut = (int)str.total_out;
5072 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5083 ** Return values of columns for the row at which the series_cursor
5084 ** is currently pointing.
5086 static int zipfileColumn(
5087 sqlite3_vtab_cursor *cur, /* The cursor */
5088 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5089 int i /* Which column to return */
5091 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5092 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5096 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5099 /* TODO: Whether or not the following is correct surely depends on
5100 ** the platform on which the archive was created. */
5101 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5103 case 2: { /* mtime */
5104 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5108 if( sqlite3_vtab_nochange(ctx)==0 ){
5109 sqlite3_result_int64(ctx, pCDS->szUncompressed);
5113 case 4: /* rawdata */
5114 if( sqlite3_vtab_nochange(ctx) ) break;
5115 case 5: { /* data */
5116 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5117 int sz = pCDS->szCompressed;
5118 int szFinal = pCDS->szUncompressed;
5122 if( pCsr->pCurrent->aData ){
5123 aBuf = pCsr->pCurrent->aData;
5125 aBuf = aFree = sqlite3_malloc(sz);
5129 FILE *pFile = pCsr->pFile;
5131 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5133 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5134 &pCsr->base.pVtab->zErrMsg
5138 if( rc==SQLITE_OK ){
5139 if( i==5 && pCDS->iCompression ){
5140 zipfileInflate(ctx, aBuf, sz, szFinal);
5142 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5145 sqlite3_free(aFree);
5147 /* Figure out if this is a directory or a zero-sized file. Consider
5148 ** it to be a directory either if the mode suggests so, or if
5149 ** the final character in the name is '/'. */
5150 u32 mode = pCDS->iExternalAttr >> 16;
5151 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5152 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5158 case 6: /* method */
5159 sqlite3_result_int(ctx, pCDS->iCompression);
5163 sqlite3_result_int64(ctx, pCsr->iId);
5171 ** Return TRUE if the cursor is at EOF.
5173 static int zipfileEof(sqlite3_vtab_cursor *cur){
5174 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5179 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5180 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5181 ** is guaranteed to be a file-handle open on a zip file.
5183 ** This function attempts to locate the EOCD record within the zip archive
5184 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5185 ** returned if successful. Otherwise, an SQLite error code is returned and
5186 ** an English language error message may be left in virtual-table pTab.
5188 static int zipfileReadEOCD(
5189 ZipfileTab *pTab, /* Return errors here */
5190 const u8 *aBlob, /* Pointer to in-memory file image */
5191 int nBlob, /* Size of aBlob[] in bytes */
5192 FILE *pFile, /* Read from this file if aBlob==0 */
5193 ZipfileEOCD *pEOCD /* Object to populate */
5195 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
5196 int nRead; /* Bytes to read from file */
5200 i64 iOff; /* Offset to read from */
5201 i64 szFile; /* Total size of file in bytes */
5202 fseek(pFile, 0, SEEK_END);
5203 szFile = (i64)ftell(pFile);
5205 memset(pEOCD, 0, sizeof(ZipfileEOCD));
5208 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5209 iOff = szFile - nRead;
5210 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5212 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5213 aRead = (u8*)&aBlob[nBlob-nRead];
5216 if( rc==SQLITE_OK ){
5219 /* Scan backwards looking for the signature bytes */
5220 for(i=nRead-20; i>=0; i--){
5221 if( aRead[i]==0x50 && aRead[i+1]==0x4b
5222 && aRead[i+2]==0x05 && aRead[i+3]==0x06
5228 pTab->base.zErrMsg = sqlite3_mprintf(
5229 "cannot find end of central directory record"
5231 return SQLITE_ERROR;
5235 pEOCD->iDisk = zipfileRead16(aRead);
5236 pEOCD->iFirstDisk = zipfileRead16(aRead);
5237 pEOCD->nEntry = zipfileRead16(aRead);
5238 pEOCD->nEntryTotal = zipfileRead16(aRead);
5239 pEOCD->nSize = zipfileRead32(aRead);
5240 pEOCD->iOffset = zipfileRead32(aRead);
5247 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5248 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5249 ** to the end of the list. Otherwise, it is added to the list immediately
5250 ** before pBefore (which is guaranteed to be a part of said list).
5252 static void zipfileAddEntry(
5254 ZipfileEntry *pBefore,
5257 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5258 assert( pNew->pNext==0 );
5260 if( pTab->pFirstEntry==0 ){
5261 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5263 assert( pTab->pLastEntry->pNext==0 );
5264 pTab->pLastEntry->pNext = pNew;
5265 pTab->pLastEntry = pNew;
5269 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5270 pNew->pNext = pBefore;
5275 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5281 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5282 iOff = eocd.iOffset;
5283 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5284 ZipfileEntry *pNew = 0;
5285 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5287 if( rc==SQLITE_OK ){
5288 zipfileAddEntry(pTab, 0, pNew);
5289 iOff += ZIPFILE_CDS_FIXED_SZ;
5290 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5297 ** xFilter callback.
5299 static int zipfileFilter(
5300 sqlite3_vtab_cursor *cur,
5301 int idxNum, const char *idxStr,
5302 int argc, sqlite3_value **argv
5304 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5305 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5306 const char *zFile = 0; /* Zip file to scan */
5307 int rc = SQLITE_OK; /* Return Code */
5308 int bInMemory = 0; /* True for an in-memory zipfile */
5310 zipfileResetCursor(pCsr);
5313 zFile = pTab->zFile;
5314 }else if( idxNum==0 ){
5315 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5316 return SQLITE_ERROR;
5317 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5318 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5319 int nBlob = sqlite3_value_bytes(argv[0]);
5320 assert( pTab->pFirstEntry==0 );
5321 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5322 pCsr->pFreeEntry = pTab->pFirstEntry;
5323 pTab->pFirstEntry = pTab->pLastEntry = 0;
5324 if( rc!=SQLITE_OK ) return rc;
5327 zFile = (const char*)sqlite3_value_text(argv[0]);
5330 if( 0==pTab->pWriteFd && 0==bInMemory ){
5331 pCsr->pFile = fopen(zFile, "rb");
5332 if( pCsr->pFile==0 ){
5333 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5336 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5337 if( rc==SQLITE_OK ){
5338 if( pCsr->eocd.nEntry==0 ){
5341 pCsr->iNextOff = pCsr->eocd.iOffset;
5342 rc = zipfileNext(cur);
5348 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5349 rc = zipfileNext(cur);
5356 ** xBestIndex callback.
5358 static int zipfileBestIndex(
5360 sqlite3_index_info *pIdxInfo
5366 for(i=0; i<pIdxInfo->nConstraint; i++){
5367 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5368 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5369 if( pCons->usable==0 ){
5371 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5376 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5377 pIdxInfo->aConstraintUsage[idx].omit = 1;
5378 pIdxInfo->estimatedCost = 1000.0;
5379 pIdxInfo->idxNum = 1;
5380 }else if( unusable ){
5381 return SQLITE_CONSTRAINT;
5386 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5388 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5390 memset(pNew, 0, sizeof(ZipfileEntry));
5391 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5392 if( pNew->cds.zFile==0 ){
5400 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5401 ZipfileCDS *pCds = &pEntry->cds;
5406 /* Write the LFH itself */
5407 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5408 zipfileWrite16(a, pCds->iVersionExtract);
5409 zipfileWrite16(a, pCds->flags);
5410 zipfileWrite16(a, pCds->iCompression);
5411 zipfileWrite16(a, pCds->mTime);
5412 zipfileWrite16(a, pCds->mDate);
5413 zipfileWrite32(a, pCds->crc32);
5414 zipfileWrite32(a, pCds->szCompressed);
5415 zipfileWrite32(a, pCds->szUncompressed);
5416 zipfileWrite16(a, (u16)pCds->nFile);
5417 zipfileWrite16(a, pCds->nExtra);
5418 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5420 /* Add the file name */
5421 memcpy(a, pCds->zFile, (int)pCds->nFile);
5422 a += (int)pCds->nFile;
5424 /* The "extra" data */
5425 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5426 zipfileWrite16(a, 5);
5428 zipfileWrite32(a, pEntry->mUnixTime);
5433 static int zipfileAppendEntry(
5435 ZipfileEntry *pEntry,
5439 u8 *aBuf = pTab->aBuffer;
5443 nBuf = zipfileSerializeLFH(pEntry, aBuf);
5444 rc = zipfileAppendData(pTab, aBuf, nBuf);
5445 if( rc==SQLITE_OK ){
5446 pEntry->iDataOff = pTab->szCurrent;
5447 rc = zipfileAppendData(pTab, pData, nData);
5453 static int zipfileGetMode(
5454 sqlite3_value *pVal,
5455 int bIsDir, /* If true, default to directory */
5456 u32 *pMode, /* OUT: Mode value */
5457 char **pzErr /* OUT: Error message */
5459 const char *z = (const char*)sqlite3_value_text(pVal);
5462 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5463 }else if( z[0]>='0' && z[0]<='9' ){
5464 mode = (unsigned int)sqlite3_value_int(pVal);
5466 const char zTemplate[11] = "-rwxrwxrwx";
5468 if( strlen(z)!=10 ) goto parse_error;
5470 case '-': mode |= S_IFREG; break;
5471 case 'd': mode |= S_IFDIR; break;
5472 case 'l': mode |= S_IFLNK; break;
5473 default: goto parse_error;
5475 for(i=1; i<10; i++){
5476 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5477 else if( z[i]!='-' ) goto parse_error;
5480 if( ((mode & S_IFDIR)==0)==bIsDir ){
5481 /* The "mode" attribute is a directory, but data has been specified.
5482 ** Or vice-versa - no data but "mode" is a file or symlink. */
5483 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5484 return SQLITE_CONSTRAINT;
5490 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5491 return SQLITE_ERROR;
5495 ** Both (const char*) arguments point to nul-terminated strings. Argument
5496 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5497 ** identical, ignoring any trailing '/' character in either path. */
5498 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5499 int nA = (int)strlen(zA);
5500 if( zA[nA-1]=='/' ) nA--;
5501 if( zB[nB-1]=='/' ) nB--;
5502 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5506 static int zipfileBegin(sqlite3_vtab *pVtab){
5507 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5510 assert( pTab->pWriteFd==0 );
5512 /* Open a write fd on the file. Also load the entire central directory
5513 ** structure into memory. During the transaction any new file data is
5514 ** appended to the archive file, but the central directory is accumulated
5515 ** in main-memory until the transaction is committed. */
5516 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5517 if( pTab->pWriteFd==0 ){
5518 pTab->base.zErrMsg = sqlite3_mprintf(
5519 "zipfile: failed to open file %s for writing", pTab->zFile
5523 fseek(pTab->pWriteFd, 0, SEEK_END);
5524 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5525 rc = zipfileLoadDirectory(pTab, 0, 0);
5528 if( rc!=SQLITE_OK ){
5529 zipfileCleanupTransaction(pTab);
5536 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5539 static u32 zipfileTime(void){
5540 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5542 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5544 pVfs->xCurrentTimeInt64(pVfs, &ms);
5545 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5548 pVfs->xCurrentTime(pVfs, &day);
5549 ret = (u32)((day - 2440587.5) * 86400);
5555 ** Return a 32-bit timestamp in UNIX epoch format.
5557 ** If the value passed as the only argument is either NULL or an SQL NULL,
5558 ** return the current time. Otherwise, return the value stored in (*pVal)
5559 ** cast to a 32-bit unsigned integer.
5561 static u32 zipfileGetTime(sqlite3_value *pVal){
5562 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5563 return zipfileTime();
5565 return (u32)sqlite3_value_int64(pVal);
5569 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5570 ** linked list. Remove it from the list and free the object.
5572 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5575 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5577 zipfileEntryFree(pOld);
5584 static int zipfileUpdate(
5585 sqlite3_vtab *pVtab,
5587 sqlite3_value **apVal,
5588 sqlite_int64 *pRowid
5590 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5591 int rc = SQLITE_OK; /* Return Code */
5592 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5594 u32 mode = 0; /* Mode for new entry */
5595 u32 mTime = 0; /* Modification time for new entry */
5596 i64 sz = 0; /* Uncompressed size */
5597 const char *zPath = 0; /* Path for new entry */
5598 int nPath = 0; /* strlen(zPath) */
5599 const u8 *pData = 0; /* Pointer to buffer containing content */
5600 int nData = 0; /* Size of pData buffer in bytes */
5601 int iMethod = 0; /* Compression method for new entry */
5602 u8 *pFree = 0; /* Free this */
5603 char *zFree = 0; /* Also free this */
5604 ZipfileEntry *pOld = 0;
5605 ZipfileEntry *pOld2 = 0;
5606 int bUpdate = 0; /* True for an update that modifies "name" */
5610 if( pTab->pWriteFd==0 ){
5611 rc = zipfileBegin(pVtab);
5612 if( rc!=SQLITE_OK ) return rc;
5615 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5616 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5617 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5618 int nDelete = (int)strlen(zDelete);
5620 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5621 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5625 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5626 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5629 assert( pOld->pNext );
5634 /* Check that "sz" and "rawdata" are both NULL: */
5635 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5636 zipfileTableErr(pTab, "sz must be NULL");
5637 rc = SQLITE_CONSTRAINT;
5639 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5640 zipfileTableErr(pTab, "rawdata must be NULL");
5641 rc = SQLITE_CONSTRAINT;
5644 if( rc==SQLITE_OK ){
5645 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5646 /* data=NULL. A directory */
5649 /* Value specified for "data", and possibly "method". This must be
5650 ** a regular file or a symlink. */
5651 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5652 int nIn = sqlite3_value_bytes(apVal[7]);
5653 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5655 iMethod = sqlite3_value_int(apVal[8]);
5659 if( iMethod!=0 && iMethod!=8 ){
5660 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5661 rc = SQLITE_CONSTRAINT;
5663 if( bAuto || iMethod ){
5665 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5666 if( rc==SQLITE_OK ){
5667 if( iMethod || nCmp<nIn ){
5674 iCrc32 = crc32(0, aIn, nIn);
5679 if( rc==SQLITE_OK ){
5680 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5683 if( rc==SQLITE_OK ){
5684 zPath = (const char*)sqlite3_value_text(apVal[2]);
5685 nPath = (int)strlen(zPath);
5686 mTime = zipfileGetTime(apVal[4]);
5689 if( rc==SQLITE_OK && bIsDir ){
5690 /* For a directory, check that the last character in the path is a
5691 ** '/'. This appears to be required for compatibility with info-zip
5692 ** (the unzip command on unix). It does not create directories
5694 if( zPath[nPath-1]!='/' ){
5695 zFree = sqlite3_mprintf("%s/", zPath);
5696 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5697 zPath = (const char*)zFree;
5702 /* Check that we're not inserting a duplicate entry -OR- updating an
5703 ** entry with a path, thereby making it into a duplicate. */
5704 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5706 for(p=pTab->pFirstEntry; p; p=p->pNext){
5707 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5708 switch( sqlite3_vtab_on_conflict(pTab->db) ){
5709 case SQLITE_IGNORE: {
5710 goto zipfile_update_done;
5712 case SQLITE_REPLACE: {
5717 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5718 rc = SQLITE_CONSTRAINT;
5727 if( rc==SQLITE_OK ){
5728 /* Create the new CDS record. */
5729 pNew = zipfileNewEntry(zPath);
5733 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5734 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5735 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5736 pNew->cds.iCompression = (u16)iMethod;
5737 zipfileMtimeToDos(&pNew->cds, mTime);
5738 pNew->cds.crc32 = iCrc32;
5739 pNew->cds.szCompressed = nData;
5740 pNew->cds.szUncompressed = (u32)sz;
5741 pNew->cds.iExternalAttr = (mode<<16);
5742 pNew->cds.iOffset = (u32)pTab->szCurrent;
5743 pNew->cds.nFile = (u16)nPath;
5744 pNew->mUnixTime = (u32)mTime;
5745 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5746 zipfileAddEntry(pTab, pOld, pNew);
5751 if( rc==SQLITE_OK && (pOld || pOld2) ){
5753 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5754 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5755 pCsr->pCurrent = pCsr->pCurrent->pNext;
5760 zipfileRemoveEntryFromList(pTab, pOld);
5761 zipfileRemoveEntryFromList(pTab, pOld2);
5764 zipfile_update_done:
5765 sqlite3_free(pFree);
5766 sqlite3_free(zFree);
5770 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5772 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5773 zipfileWrite16(a, p->iDisk);
5774 zipfileWrite16(a, p->iFirstDisk);
5775 zipfileWrite16(a, p->nEntry);
5776 zipfileWrite16(a, p->nEntryTotal);
5777 zipfileWrite32(a, p->nSize);
5778 zipfileWrite32(a, p->iOffset);
5779 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
5784 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5785 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5786 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5787 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5791 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5792 ** of bytes written.
5794 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5796 ZipfileCDS *pCDS = &pEntry->cds;
5798 if( pEntry->aExtra==0 ){
5802 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5803 zipfileWrite16(a, pCDS->iVersionMadeBy);
5804 zipfileWrite16(a, pCDS->iVersionExtract);
5805 zipfileWrite16(a, pCDS->flags);
5806 zipfileWrite16(a, pCDS->iCompression);
5807 zipfileWrite16(a, pCDS->mTime);
5808 zipfileWrite16(a, pCDS->mDate);
5809 zipfileWrite32(a, pCDS->crc32);
5810 zipfileWrite32(a, pCDS->szCompressed);
5811 zipfileWrite32(a, pCDS->szUncompressed);
5812 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5813 zipfileWrite16(a, pCDS->nFile);
5814 zipfileWrite16(a, pCDS->nExtra);
5815 zipfileWrite16(a, pCDS->nComment);
5816 zipfileWrite16(a, pCDS->iDiskStart);
5817 zipfileWrite16(a, pCDS->iInternalAttr);
5818 zipfileWrite32(a, pCDS->iExternalAttr);
5819 zipfileWrite32(a, pCDS->iOffset);
5821 memcpy(a, pCDS->zFile, pCDS->nFile);
5824 if( pEntry->aExtra ){
5825 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5826 memcpy(a, pEntry->aExtra, n);
5829 assert( pCDS->nExtra==9 );
5830 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5831 zipfileWrite16(a, 5);
5833 zipfileWrite32(a, pEntry->mUnixTime);
5839 static int zipfileCommit(sqlite3_vtab *pVtab){
5840 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5842 if( pTab->pWriteFd ){
5843 i64 iOffset = pTab->szCurrent;
5848 /* Write out all entries */
5849 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5850 int n = zipfileSerializeCDS(p, pTab->aBuffer);
5851 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5855 /* Write out the EOCD record */
5857 eocd.iFirstDisk = 0;
5858 eocd.nEntry = (u16)nEntry;
5859 eocd.nEntryTotal = (u16)nEntry;
5860 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
5861 eocd.iOffset = (u32)iOffset;
5862 rc = zipfileAppendEOCD(pTab, &eocd);
5864 zipfileCleanupTransaction(pTab);
5869 static int zipfileRollback(sqlite3_vtab *pVtab){
5870 return zipfileCommit(pVtab);
5873 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
5875 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5876 if( iId==pCsr->iId ) break;
5881 static void zipfileFunctionCds(
5882 sqlite3_context *context,
5884 sqlite3_value **argv
5887 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5890 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5892 ZipfileCDS *p = &pCsr->pCurrent->cds;
5893 char *zRes = sqlite3_mprintf("{"
5894 "\"version-made-by\" : %u, "
5895 "\"version-to-extract\" : %u, "
5897 "\"compression\" : %u, "
5901 "\"compressed-size\" : %u, "
5902 "\"uncompressed-size\" : %u, "
5903 "\"file-name-length\" : %u, "
5904 "\"extra-field-length\" : %u, "
5905 "\"file-comment-length\" : %u, "
5906 "\"disk-number-start\" : %u, "
5907 "\"internal-attr\" : %u, "
5908 "\"external-attr\" : %u, "
5909 "\"offset\" : %u }",
5910 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
5911 (u32)p->flags, (u32)p->iCompression,
5912 (u32)p->mTime, (u32)p->mDate,
5913 (u32)p->crc32, (u32)p->szCompressed,
5914 (u32)p->szUncompressed, (u32)p->nFile,
5915 (u32)p->nExtra, (u32)p->nComment,
5916 (u32)p->iDiskStart, (u32)p->iInternalAttr,
5917 (u32)p->iExternalAttr, (u32)p->iOffset
5921 sqlite3_result_error_nomem(context);
5923 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
5930 ** xFindFunction method.
5932 static int zipfileFindFunction(
5933 sqlite3_vtab *pVtab, /* Virtual table handle */
5934 int nArg, /* Number of SQL function arguments */
5935 const char *zName, /* Name of SQL function */
5936 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5937 void **ppArg /* OUT: User data for *pxFunc */
5939 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5940 *pxFunc = zipfileFunctionCds;
5941 *ppArg = (void*)pVtab;
5947 typedef struct ZipfileBuffer ZipfileBuffer;
5948 struct ZipfileBuffer {
5949 u8 *a; /* Pointer to buffer */
5950 int n; /* Size of buffer in bytes */
5951 int nAlloc; /* Byte allocated at a[] */
5954 typedef struct ZipfileCtx ZipfileCtx;
5961 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
5962 if( pBuf->n+nByte>pBuf->nAlloc ){
5964 int nNew = pBuf->n ? pBuf->n*2 : 512;
5965 int nReq = pBuf->n + nByte;
5967 while( nNew<nReq ) nNew = nNew*2;
5968 aNew = sqlite3_realloc(pBuf->a, nNew);
5969 if( aNew==0 ) return SQLITE_NOMEM;
5971 pBuf->nAlloc = nNew;
5977 ** xStep() callback for the zipfile() aggregate. This can be called in
5978 ** any of the following ways:
5980 ** SELECT zipfile(name,data) ...
5981 ** SELECT zipfile(name,mode,mtime,data) ...
5982 ** SELECT zipfile(name,mode,mtime,data,method) ...
5984 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
5985 ZipfileCtx *p; /* Aggregate function context */
5986 ZipfileEntry e; /* New entry to add to zip archive */
5988 sqlite3_value *pName = 0;
5989 sqlite3_value *pMode = 0;
5990 sqlite3_value *pMtime = 0;
5991 sqlite3_value *pData = 0;
5992 sqlite3_value *pMethod = 0;
5999 int iMethod = -1; /* Compression method to use (0 or 8) */
6001 const u8 *aData = 0; /* Possibly compressed data for new entry */
6002 int nData = 0; /* Size of aData[] in bytes */
6003 int szUncompressed = 0; /* Size of data before compression */
6004 u8 *aFree = 0; /* Free this before returning */
6005 u32 iCrc32 = 0; /* crc32 of uncompressed data */
6007 char *zName = 0; /* Path (name) of new entry */
6008 int nName = 0; /* Size of zName in bytes */
6009 char *zFree = 0; /* Free this before returning */
6012 memset(&e, 0, sizeof(e));
6013 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6016 /* Martial the arguments into stack variables */
6017 if( nVal!=2 && nVal!=4 && nVal!=5 ){
6018 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6020 goto zipfile_step_out;
6034 /* Check that the 'name' parameter looks ok. */
6035 zName = (char*)sqlite3_value_text(pName);
6036 nName = sqlite3_value_bytes(pName);
6038 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6040 goto zipfile_step_out;
6043 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6044 ** deflate compression) or NULL (choose automatically). */
6045 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6046 iMethod = (int)sqlite3_value_int64(pMethod);
6047 if( iMethod!=0 && iMethod!=8 ){
6048 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6050 goto zipfile_step_out;
6054 /* Now inspect the data. If this is NULL, then the new entry must be a
6055 ** directory. Otherwise, figure out whether or not the data should
6056 ** be deflated or simply stored in the zip archive. */
6057 if( sqlite3_value_type(pData)==SQLITE_NULL ){
6061 aData = sqlite3_value_blob(pData);
6062 szUncompressed = nData = sqlite3_value_bytes(pData);
6063 iCrc32 = crc32(0, aData, nData);
6064 if( iMethod<0 || iMethod==8 ){
6066 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6067 if( rc!=SQLITE_OK ){
6068 goto zipfile_step_out;
6070 if( iMethod==8 || nOut<nData ){
6080 /* Decode the "mode" argument. */
6081 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6082 if( rc ) goto zipfile_step_out;
6084 /* Decode the "mtime" argument. */
6085 e.mUnixTime = zipfileGetTime(pMtime);
6087 /* If this is a directory entry, ensure that there is exactly one '/'
6088 ** at the end of the path. Or, if this is not a directory and the path
6089 ** ends in '/' it is an error. */
6091 if( zName[nName-1]=='/' ){
6092 zErr = sqlite3_mprintf("non-directory name must not end with /");
6094 goto zipfile_step_out;
6097 if( zName[nName-1]!='/' ){
6098 zName = zFree = sqlite3_mprintf("%s/", zName);
6102 goto zipfile_step_out;
6105 while( nName>1 && zName[nName-2]=='/' ) nName--;
6109 /* Assemble the ZipfileEntry object for the new zip archive entry */
6110 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6111 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6112 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6113 e.cds.iCompression = (u16)iMethod;
6114 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6115 e.cds.crc32 = iCrc32;
6116 e.cds.szCompressed = nData;
6117 e.cds.szUncompressed = szUncompressed;
6118 e.cds.iExternalAttr = (mode<<16);
6119 e.cds.iOffset = p->body.n;
6120 e.cds.nFile = (u16)nName;
6121 e.cds.zFile = zName;
6123 /* Append the LFH to the body of the new archive */
6124 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6125 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6126 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6128 /* Append the data to the body of the new archive */
6130 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6131 memcpy(&p->body.a[p->body.n], aData, nData);
6135 /* Append the CDS record to the directory of the new archive */
6136 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6137 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6138 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6140 /* Increment the count of entries in the archive */
6144 sqlite3_free(aFree);
6145 sqlite3_free(zFree);
6148 sqlite3_result_error(pCtx, zErr, -1);
6150 sqlite3_result_error_code(pCtx, rc);
6157 ** xFinalize() callback for zipfile aggregate function.
6159 void zipfileFinal(sqlite3_context *pCtx){
6165 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6168 memset(&eocd, 0, sizeof(eocd));
6169 eocd.nEntry = (u16)p->nEntry;
6170 eocd.nEntryTotal = (u16)p->nEntry;
6171 eocd.nSize = p->cds.n;
6172 eocd.iOffset = p->body.n;
6174 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6175 aZip = (u8*)sqlite3_malloc(nZip);
6177 sqlite3_result_error_nomem(pCtx);
6179 memcpy(aZip, p->body.a, p->body.n);
6180 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6181 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6182 sqlite3_result_blob(pCtx, aZip, nZip, zipfileFree);
6186 sqlite3_free(p->body.a);
6187 sqlite3_free(p->cds.a);
6192 ** Register the "zipfile" virtual table.
6194 static int zipfileRegister(sqlite3 *db){
6195 static sqlite3_module zipfileModule = {
6197 zipfileConnect, /* xCreate */
6198 zipfileConnect, /* xConnect */
6199 zipfileBestIndex, /* xBestIndex */
6200 zipfileDisconnect, /* xDisconnect */
6201 zipfileDisconnect, /* xDestroy */
6202 zipfileOpen, /* xOpen - open a cursor */
6203 zipfileClose, /* xClose - close a cursor */
6204 zipfileFilter, /* xFilter - configure scan constraints */
6205 zipfileNext, /* xNext - advance a cursor */
6206 zipfileEof, /* xEof - check for end of scan */
6207 zipfileColumn, /* xColumn - read data */
6208 0, /* xRowid - read data */
6209 zipfileUpdate, /* xUpdate */
6210 zipfileBegin, /* xBegin */
6212 zipfileCommit, /* xCommit */
6213 zipfileRollback, /* xRollback */
6214 zipfileFindFunction, /* xFindMethod */
6218 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6219 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6220 if( rc==SQLITE_OK ){
6221 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6222 zipfileStep, zipfileFinal
6227 #else /* SQLITE_OMIT_VIRTUALTABLE */
6228 # define zipfileRegister(x) SQLITE_OK
6234 int sqlite3_zipfile_init(
6237 const sqlite3_api_routines *pApi
6239 SQLITE_EXTENSION_INIT2(pApi);
6240 (void)pzErrMsg; /* Unused parameter */
6241 return zipfileRegister(db);
6244 /************************* End ../ext/misc/zipfile.c ********************/
6245 /************************* Begin ../ext/misc/sqlar.c ******************/
6249 ** The author disclaims copyright to this source code. In place of
6250 ** a legal notice, here is a blessing:
6252 ** May you do good and not evil.
6253 ** May you find forgiveness for yourself and forgive others.
6254 ** May you share freely, never taking more than you give.
6256 ******************************************************************************
6258 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6259 ** for working with sqlar archives and used by the shell tool's built-in
6262 SQLITE_EXTENSION_INIT1
6266 ** Implementation of the "sqlar_compress(X)" SQL function.
6268 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6269 ** zlib utility function compress() yields a smaller blob, return the
6270 ** compressed blob. Otherwise, return a copy of X.
6272 ** SQLar uses the "zlib format" for compressed content. The zlib format
6273 ** contains a two-byte identification header and a four-byte checksum at
6274 ** the end. This is different from ZIP which uses the raw deflate format.
6276 ** Future enhancements to SQLar might add support for new compression formats.
6277 ** If so, those new formats will be identified by alternative headers in the
6280 static void sqlarCompressFunc(
6281 sqlite3_context *context,
6283 sqlite3_value **argv
6286 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6287 const Bytef *pData = sqlite3_value_blob(argv[0]);
6288 uLong nData = sqlite3_value_bytes(argv[0]);
6289 uLongf nOut = compressBound(nData);
6292 pOut = (Bytef*)sqlite3_malloc(nOut);
6294 sqlite3_result_error_nomem(context);
6297 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6298 sqlite3_result_error(context, "error in compress()", -1);
6299 }else if( nOut<nData ){
6300 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6302 sqlite3_result_value(context, argv[0]);
6307 sqlite3_result_value(context, argv[0]);
6312 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6314 ** Parameter SZ is interpreted as an integer. If it is less than or
6315 ** equal to zero, then this function returns a copy of X. Or, if
6316 ** SZ is equal to the size of X when interpreted as a blob, also
6317 ** return a copy of X. Otherwise, decompress blob X using zlib
6318 ** utility function uncompress() and return the results (another
6321 static void sqlarUncompressFunc(
6322 sqlite3_context *context,
6324 sqlite3_value **argv
6330 sz = sqlite3_value_int(argv[1]);
6332 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6333 sqlite3_result_value(context, argv[0]);
6335 const Bytef *pData= sqlite3_value_blob(argv[0]);
6336 Bytef *pOut = sqlite3_malloc(sz);
6337 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6338 sqlite3_result_error(context, "error in uncompress()", -1);
6340 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6350 int sqlite3_sqlar_init(
6353 const sqlite3_api_routines *pApi
6356 SQLITE_EXTENSION_INIT2(pApi);
6357 (void)pzErrMsg; /* Unused parameter */
6358 rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6359 sqlarCompressFunc, 0, 0);
6360 if( rc==SQLITE_OK ){
6361 rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6362 sqlarUncompressFunc, 0, 0);
6367 /************************* End ../ext/misc/sqlar.c ********************/
6369 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6373 ** The author disclaims copyright to this source code. In place of
6374 ** a legal notice, here is a blessing:
6376 ** May you do good and not evil.
6377 ** May you find forgiveness for yourself and forgive others.
6378 ** May you share freely, never taking more than you give.
6380 *************************************************************************
6385 typedef struct sqlite3expert sqlite3expert;
6388 ** Create a new sqlite3expert object.
6390 ** If successful, a pointer to the new object is returned and (*pzErr) set
6391 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6392 ** an English-language error message. In this case it is the responsibility
6393 ** of the caller to eventually free the error message buffer using
6396 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6399 ** Configure an sqlite3expert object.
6401 ** EXPERT_CONFIG_SAMPLE:
6402 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6403 ** each candidate index. This involves scanning and sorting the entire
6404 ** contents of each user database table once for each candidate index
6405 ** associated with the table. For large databases, this can be
6406 ** prohibitively slow. This option allows the sqlite3expert object to
6407 ** be configured so that sqlite_stat1 data is instead generated based on a
6408 ** subset of each table, or so that no sqlite_stat1 data is used at all.
6410 ** A single integer argument is passed to this option. If the value is less
6411 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
6412 ** the analysis - indexes are recommended based on the database schema only.
6413 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
6414 ** generated for each candidate index (this is the default). Finally, if the
6415 ** value falls between 0 and 100, then it represents the percentage of user
6416 ** table rows that should be considered when generating sqlite_stat1 data.
6420 ** // Do not generate any sqlite_stat1 data
6421 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6423 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
6424 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6426 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6428 #define EXPERT_CONFIG_SAMPLE 1 /* int */
6431 ** Specify zero or more SQL statements to be included in the analysis.
6433 ** Buffer zSql must contain zero or more complete SQL statements. This
6434 ** function parses all statements contained in the buffer and adds them
6435 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6436 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6437 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6438 ** may be set to point to an English language error message. In this case
6439 ** the caller is responsible for eventually freeing the error message buffer
6440 ** using sqlite3_free().
6442 ** If an error does occur while processing one of the statements in the
6443 ** buffer passed as the second argument, none of the statements in the
6444 ** buffer are added to the analysis.
6446 ** This function must be called before sqlite3_expert_analyze(). If a call
6447 ** to this function is made on an sqlite3expert object that has already
6448 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6449 ** immediately and no statements are added to the analysis.
6451 int sqlite3_expert_sql(
6452 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
6453 const char *zSql, /* SQL statement(s) to add */
6454 char **pzErr /* OUT: Error message (if any) */
6459 ** This function is called after the sqlite3expert object has been configured
6460 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6461 ** the analysis. Once this function has been called, it is not possible to
6462 ** add further SQL statements to the analysis.
6464 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6465 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6466 ** point to a buffer containing an English language error message. In this
6467 ** case it is the responsibility of the caller to eventually free the buffer
6468 ** using sqlite3_free().
6470 ** If an error does occur within this function, the sqlite3expert object
6471 ** is no longer useful for any purpose. At that point it is no longer
6472 ** possible to add further SQL statements to the object or to re-attempt
6473 ** the analysis. The sqlite3expert object must still be freed using a call
6474 ** sqlite3_expert_destroy().
6476 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6479 ** Return the total number of statements loaded using sqlite3_expert_sql().
6480 ** The total number of SQL statements may be different from the total number
6481 ** to calls to sqlite3_expert_sql().
6483 int sqlite3_expert_count(sqlite3expert*);
6486 ** Return a component of the report.
6488 ** This function is called after sqlite3_expert_analyze() to extract the
6489 ** results of the analysis. Each call to this function returns either a
6490 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6491 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6492 ** #define constants defined below.
6494 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6495 ** information relating to a specific SQL statement. In these cases that
6496 ** SQL statement is identified by the value passed as the second argument.
6497 ** SQL statements are numbered from 0 in the order in which they are parsed.
6498 ** If an out-of-range value (less than zero or equal to or greater than the
6499 ** value returned by sqlite3_expert_count()) is passed as the second argument
6500 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6502 ** EXPERT_REPORT_SQL:
6503 ** Return the text of SQL statement iStmt.
6505 ** EXPERT_REPORT_INDEXES:
6506 ** Return a buffer containing the CREATE INDEX statements for all recommended
6507 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
6510 ** EXPERT_REPORT_PLAN:
6511 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6512 ** iStmt after the proposed indexes have been added to the database schema.
6514 ** EXPERT_REPORT_CANDIDATES:
6515 ** Return a pointer to a buffer containing the CREATE INDEX statements
6516 ** for all indexes that were tested (for all SQL statements). The iStmt
6517 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6519 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6522 ** Values for the third argument passed to sqlite3_expert_report().
6524 #define EXPERT_REPORT_SQL 1
6525 #define EXPERT_REPORT_INDEXES 2
6526 #define EXPERT_REPORT_PLAN 3
6527 #define EXPERT_REPORT_CANDIDATES 4
6530 ** Free an (sqlite3expert*) handle and all associated resources. There
6531 ** should be one call to this function for each successful call to
6532 ** sqlite3-expert_new().
6534 void sqlite3_expert_destroy(sqlite3expert*);
6538 /************************* End ../ext/expert/sqlite3expert.h ********************/
6539 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6543 ** The author disclaims copyright to this source code. In place of
6544 ** a legal notice, here is a blessing:
6546 ** May you do good and not evil.
6547 ** May you find forgiveness for yourself and forgive others.
6548 ** May you share freely, never taking more than you give.
6550 *************************************************************************
6556 #ifndef SQLITE_OMIT_VIRTUALTABLE
6558 /* typedef sqlite3_int64 i64; */
6559 /* typedef sqlite3_uint64 u64; */
6561 typedef struct IdxColumn IdxColumn;
6562 typedef struct IdxConstraint IdxConstraint;
6563 typedef struct IdxScan IdxScan;
6564 typedef struct IdxStatement IdxStatement;
6565 typedef struct IdxTable IdxTable;
6566 typedef struct IdxWrite IdxWrite;
6568 #define STRLEN (int)strlen
6571 ** A temp table name that we assume no user database will actually use.
6572 ** If this assumption proves incorrect triggers on the table with the
6573 ** conflicting name will be ignored.
6575 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6578 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6579 ** any other type of single-ended range constraint on a column).
6582 ** Used to temporarily link IdxConstraint objects into lists while
6583 ** creating candidate indexes.
6585 struct IdxConstraint {
6586 char *zColl; /* Collation sequence */
6587 int bRange; /* True for range, false for eq */
6588 int iCol; /* Constrained table column */
6589 int bFlag; /* Used by idxFindCompatible() */
6590 int bDesc; /* True if ORDER BY <expr> DESC */
6591 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
6592 IdxConstraint *pLink; /* See above */
6596 ** A single scan of a single table.
6599 IdxTable *pTab; /* Associated table object */
6600 int iDb; /* Database containing table zTable */
6601 i64 covering; /* Mask of columns required for cov. index */
6602 IdxConstraint *pOrder; /* ORDER BY columns */
6603 IdxConstraint *pEq; /* List of == constraints */
6604 IdxConstraint *pRange; /* List of < constraints */
6605 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
6609 ** Information regarding a single database table. Extracted from
6610 ** "PRAGMA table_info" by function idxGetTableInfo().
6619 char *zName; /* Table name */
6621 IdxTable *pNext; /* Next table in linked list of all tables */
6625 ** An object of the following type is created for each unique table/write-op
6626 ** seen. The objects are stored in a singly-linked list beginning at
6627 ** sqlite3expert.pWrite.
6631 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
6636 ** Each statement being analyzed is represented by an instance of this
6639 struct IdxStatement {
6640 int iId; /* Statement number */
6641 char *zSql; /* SQL statement */
6642 char *zIdx; /* Indexes */
6643 char *zEQP; /* Plan */
6644 IdxStatement *pNext;
6649 ** A hash table for storing strings. With space for a payload string
6650 ** with each entry. Methods are:
6657 #define IDX_HASH_SIZE 1023
6658 typedef struct IdxHashEntry IdxHashEntry;
6659 typedef struct IdxHash IdxHash;
6660 struct IdxHashEntry {
6661 char *zKey; /* nul-terminated key */
6662 char *zVal; /* nul-terminated value string */
6663 char *zVal2; /* nul-terminated value string 2 */
6664 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
6665 IdxHashEntry *pNext; /* Next entry in hash */
6668 IdxHashEntry *pFirst;
6669 IdxHashEntry *aHash[IDX_HASH_SIZE];
6673 ** sqlite3expert object.
6675 struct sqlite3expert {
6676 int iSample; /* Percentage of tables to sample for stat1 */
6677 sqlite3 *db; /* User database */
6678 sqlite3 *dbm; /* In-memory db for this analysis */
6679 sqlite3 *dbv; /* Vtab schema for this analysis */
6680 IdxTable *pTable; /* List of all IdxTable objects */
6681 IdxScan *pScan; /* List of scan objects */
6682 IdxWrite *pWrite; /* List of write objects */
6683 IdxStatement *pStatement; /* List of IdxStatement objects */
6684 int bRun; /* True once analysis has run */
6686 int rc; /* Error code from whereinfo hook */
6687 IdxHash hIdx; /* Hash containing all candidate indexes */
6688 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
6693 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6694 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6696 static void *idxMalloc(int *pRc, int nByte){
6698 assert( *pRc==SQLITE_OK );
6700 pRet = sqlite3_malloc(nByte);
6702 memset(pRet, 0, nByte);
6704 *pRc = SQLITE_NOMEM;
6710 ** Initialize an IdxHash hash table.
6712 static void idxHashInit(IdxHash *pHash){
6713 memset(pHash, 0, sizeof(IdxHash));
6717 ** Reset an IdxHash hash table.
6719 static void idxHashClear(IdxHash *pHash){
6721 for(i=0; i<IDX_HASH_SIZE; i++){
6722 IdxHashEntry *pEntry;
6723 IdxHashEntry *pNext;
6724 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6725 pNext = pEntry->pHashNext;
6726 sqlite3_free(pEntry->zVal2);
6727 sqlite3_free(pEntry);
6730 memset(pHash, 0, sizeof(IdxHash));
6734 ** Return the index of the hash bucket that the string specified by the
6735 ** arguments to this function belongs.
6737 static int idxHashString(const char *z, int n){
6738 unsigned int ret = 0;
6741 ret += (ret<<3) + (unsigned char)(z[i]);
6743 return (int)(ret % IDX_HASH_SIZE);
6747 ** If zKey is already present in the hash table, return non-zero and do
6748 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6749 ** the hash table passed as the second argument.
6751 static int idxHashAdd(
6757 int nKey = STRLEN(zKey);
6758 int iHash = idxHashString(zKey, nKey);
6759 int nVal = (zVal ? STRLEN(zVal) : 0);
6760 IdxHashEntry *pEntry;
6762 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6763 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6767 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6769 pEntry->zKey = (char*)&pEntry[1];
6770 memcpy(pEntry->zKey, zKey, nKey);
6772 pEntry->zVal = &pEntry->zKey[nKey+1];
6773 memcpy(pEntry->zVal, zVal, nVal);
6775 pEntry->pHashNext = pHash->aHash[iHash];
6776 pHash->aHash[iHash] = pEntry;
6778 pEntry->pNext = pHash->pFirst;
6779 pHash->pFirst = pEntry;
6785 ** If zKey/nKey is present in the hash table, return a pointer to the
6786 ** hash-entry object.
6788 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6790 IdxHashEntry *pEntry;
6791 if( nKey<0 ) nKey = STRLEN(zKey);
6792 iHash = idxHashString(zKey, nKey);
6794 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6795 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6803 ** If the hash table contains an entry with a key equal to the string
6804 ** passed as the final two arguments to this function, return a pointer
6805 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6806 ** hash table, return NULL.
6808 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6809 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6810 if( pEntry ) return pEntry->zVal;
6815 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6816 ** variable to point to a copy of nul-terminated string zColl.
6818 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6819 IdxConstraint *pNew;
6820 int nColl = STRLEN(zColl);
6822 assert( *pRc==SQLITE_OK );
6823 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6825 pNew->zColl = (char*)&pNew[1];
6826 memcpy(pNew->zColl, zColl, nColl+1);
6832 ** An error associated with database handle db has just occurred. Pass
6833 ** the error message to callback function xOut.
6835 static void idxDatabaseError(
6836 sqlite3 *db, /* Database handle */
6837 char **pzErrmsg /* Write error here */
6839 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6843 ** Prepare an SQL statement.
6845 static int idxPrepareStmt(
6846 sqlite3 *db, /* Database handle to compile against */
6847 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6848 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6849 const char *zSql /* SQL statement to compile */
6851 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6852 if( rc!=SQLITE_OK ){
6854 idxDatabaseError(db, pzErrmsg);
6860 ** Prepare an SQL statement using the results of a printf() formatting.
6862 static int idxPrintfPrepareStmt(
6863 sqlite3 *db, /* Database handle to compile against */
6864 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6865 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6866 const char *zFmt, /* printf() format of SQL statement */
6867 ... /* Trailing printf() arguments */
6873 zSql = sqlite3_vmprintf(zFmt, ap);
6877 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
6885 /*************************************************************************
6886 ** Beginning of virtual table implementation.
6888 typedef struct ExpertVtab ExpertVtab;
6892 sqlite3expert *pExpert;
6895 typedef struct ExpertCsr ExpertCsr;
6897 sqlite3_vtab_cursor base;
6898 sqlite3_stmt *pData;
6901 static char *expertDequote(const char *zIn){
6902 int n = STRLEN(zIn);
6903 char *zRet = sqlite3_malloc(n);
6905 assert( zIn[0]=='\'' );
6906 assert( zIn[n-1]=='\'' );
6911 for(iIn=1; iIn<(n-1); iIn++){
6912 if( zIn[iIn]=='\'' ){
6913 assert( zIn[iIn+1]=='\'' );
6916 zRet[iOut++] = zIn[iIn];
6925 ** This function is the implementation of both the xConnect and xCreate
6926 ** methods of the r-tree virtual table.
6928 ** argv[0] -> module name
6929 ** argv[1] -> database name
6930 ** argv[2] -> table name
6931 ** argv[...] -> column names...
6933 static int expertConnect(
6936 int argc, const char *const*argv,
6937 sqlite3_vtab **ppVtab,
6940 sqlite3expert *pExpert = (sqlite3expert*)pAux;
6945 *pzErr = sqlite3_mprintf("internal error!");
6948 char *zCreateTable = expertDequote(argv[3]);
6950 rc = sqlite3_declare_vtab(db, zCreateTable);
6951 if( rc==SQLITE_OK ){
6952 p = idxMalloc(&rc, sizeof(ExpertVtab));
6954 if( rc==SQLITE_OK ){
6955 p->pExpert = pExpert;
6956 p->pTab = pExpert->pTable;
6957 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
6959 sqlite3_free(zCreateTable);
6965 *ppVtab = (sqlite3_vtab*)p;
6969 static int expertDisconnect(sqlite3_vtab *pVtab){
6970 ExpertVtab *p = (ExpertVtab*)pVtab;
6975 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
6976 ExpertVtab *p = (ExpertVtab*)pVtab;
6981 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
6982 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
6983 SQLITE_INDEX_CONSTRAINT_LE;
6985 pScan = idxMalloc(&rc, sizeof(IdxScan));
6989 /* Link the new scan object into the list */
6990 pScan->pTab = p->pTab;
6991 pScan->pNextScan = p->pExpert->pScan;
6992 p->pExpert->pScan = pScan;
6994 /* Add the constraints to the IdxScan object */
6995 for(i=0; i<pIdxInfo->nConstraint; i++){
6996 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
6998 && pCons->iColumn>=0
6999 && p->pTab->aCol[pCons->iColumn].iPk==0
7000 && (pCons->op & opmask)
7002 IdxConstraint *pNew;
7003 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7004 pNew = idxNewConstraint(&rc, zColl);
7006 pNew->iCol = pCons->iColumn;
7007 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7008 pNew->pNext = pScan->pEq;
7012 pNew->pNext = pScan->pRange;
7013 pScan->pRange = pNew;
7017 pIdxInfo->aConstraintUsage[i].argvIndex = n;
7021 /* Add the ORDER BY to the IdxScan object */
7022 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7023 int iCol = pIdxInfo->aOrderBy[i].iColumn;
7025 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7028 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7029 pNew->pNext = pScan->pOrder;
7030 pNew->pLink = pScan->pOrder;
7031 pScan->pOrder = pNew;
7038 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7042 static int expertUpdate(
7043 sqlite3_vtab *pVtab,
7045 sqlite3_value **azData,
7046 sqlite_int64 *pRowid
7056 ** Virtual table module xOpen method.
7058 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7062 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7063 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7068 ** Virtual table module xClose method.
7070 static int expertClose(sqlite3_vtab_cursor *cur){
7071 ExpertCsr *pCsr = (ExpertCsr*)cur;
7072 sqlite3_finalize(pCsr->pData);
7078 ** Virtual table module xEof method.
7080 ** Return non-zero if the cursor does not currently point to a valid
7081 ** record (i.e if the scan has finished), or zero otherwise.
7083 static int expertEof(sqlite3_vtab_cursor *cur){
7084 ExpertCsr *pCsr = (ExpertCsr*)cur;
7085 return pCsr->pData==0;
7089 ** Virtual table module xNext method.
7091 static int expertNext(sqlite3_vtab_cursor *cur){
7092 ExpertCsr *pCsr = (ExpertCsr*)cur;
7095 assert( pCsr->pData );
7096 rc = sqlite3_step(pCsr->pData);
7097 if( rc!=SQLITE_ROW ){
7098 rc = sqlite3_finalize(pCsr->pData);
7108 ** Virtual table module xRowid method.
7110 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7117 ** Virtual table module xColumn method.
7119 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7120 ExpertCsr *pCsr = (ExpertCsr*)cur;
7121 sqlite3_value *pVal;
7122 pVal = sqlite3_column_value(pCsr->pData, i);
7124 sqlite3_result_value(ctx, pVal);
7130 ** Virtual table module xFilter method.
7132 static int expertFilter(
7133 sqlite3_vtab_cursor *cur,
7134 int idxNum, const char *idxStr,
7135 int argc, sqlite3_value **argv
7137 ExpertCsr *pCsr = (ExpertCsr*)cur;
7138 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7139 sqlite3expert *pExpert = pVtab->pExpert;
7146 rc = sqlite3_finalize(pCsr->pData);
7148 if( rc==SQLITE_OK ){
7149 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7150 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7154 if( rc==SQLITE_OK ){
7155 rc = expertNext(cur);
7160 static int idxRegisterVtab(sqlite3expert *p){
7161 static sqlite3_module expertModule = {
7163 expertConnect, /* xCreate - create a table */
7164 expertConnect, /* xConnect - connect to an existing table */
7165 expertBestIndex, /* xBestIndex - Determine search strategy */
7166 expertDisconnect, /* xDisconnect - Disconnect from a table */
7167 expertDisconnect, /* xDestroy - Drop a table */
7168 expertOpen, /* xOpen - open a cursor */
7169 expertClose, /* xClose - close a cursor */
7170 expertFilter, /* xFilter - configure scan constraints */
7171 expertNext, /* xNext - advance a cursor */
7172 expertEof, /* xEof */
7173 expertColumn, /* xColumn - read data */
7174 expertRowid, /* xRowid - read data */
7175 expertUpdate, /* xUpdate - write data */
7176 0, /* xBegin - begin transaction */
7177 0, /* xSync - sync transaction */
7178 0, /* xCommit - commit transaction */
7179 0, /* xRollback - rollback transaction */
7180 0, /* xFindFunction - function overloading */
7181 0, /* xRename - rename the table */
7184 0, /* xRollbackTo */
7185 0, /* xShadowName */
7188 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7191 ** End of virtual table implementation.
7192 *************************************************************************/
7194 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7195 ** is called, set it to the return value of sqlite3_finalize() before
7196 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7198 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7199 int rc = sqlite3_finalize(pStmt);
7200 if( *pRc==SQLITE_OK ) *pRc = rc;
7204 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7205 ** in the main database of connection db. If successful, set (*ppOut) to
7206 ** point to the new object and return SQLITE_OK. Otherwise, return an
7207 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7208 ** set to point to an error string.
7210 ** It is the responsibility of the caller to eventually free either the
7211 ** IdxTable object or error message using sqlite3_free().
7213 static int idxGetTableInfo(
7214 sqlite3 *db, /* Database connection to read details from */
7215 const char *zTab, /* Table name */
7216 IdxTable **ppOut, /* OUT: New object (if successful) */
7217 char **pzErrmsg /* OUT: Error message (if not) */
7219 sqlite3_stmt *p1 = 0;
7221 int nTab = STRLEN(zTab);
7222 int nByte = sizeof(IdxTable) + nTab + 1;
7227 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7228 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7229 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7230 nByte += 1 + STRLEN(zCol);
7231 rc = sqlite3_table_column_metadata(
7232 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7234 nByte += 1 + STRLEN(zCol);
7237 rc2 = sqlite3_reset(p1);
7238 if( rc==SQLITE_OK ) rc = rc2;
7240 nByte += sizeof(IdxColumn) * nCol;
7241 if( rc==SQLITE_OK ){
7242 pNew = idxMalloc(&rc, nByte);
7244 if( rc==SQLITE_OK ){
7245 pNew->aCol = (IdxColumn*)&pNew[1];
7247 pCsr = (char*)&pNew->aCol[nCol];
7251 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7252 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7253 int nCopy = STRLEN(zCol) + 1;
7254 pNew->aCol[nCol].zName = pCsr;
7255 pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7256 memcpy(pCsr, zCol, nCopy);
7259 rc = sqlite3_table_column_metadata(
7260 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7262 if( rc==SQLITE_OK ){
7263 nCopy = STRLEN(zCol) + 1;
7264 pNew->aCol[nCol].zColl = pCsr;
7265 memcpy(pCsr, zCol, nCopy);
7271 idxFinalize(&rc, p1);
7273 if( rc!=SQLITE_OK ){
7278 memcpy(pNew->zName, zTab, nTab+1);
7286 ** This function is a no-op if *pRc is set to anything other than
7287 ** SQLITE_OK when it is called.
7289 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7290 ** the printf() style arguments is appended to zIn and the result returned
7291 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7292 ** zIn before returning.
7294 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7298 int nIn = zIn ? STRLEN(zIn) : 0;
7301 if( *pRc==SQLITE_OK ){
7302 zAppend = sqlite3_vmprintf(zFmt, ap);
7304 nAppend = STRLEN(zAppend);
7305 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7307 if( zAppend && zRet ){
7308 if( nIn ) memcpy(zRet, zIn, nIn);
7309 memcpy(&zRet[nIn], zAppend, nAppend+1);
7313 *pRc = SQLITE_NOMEM;
7315 sqlite3_free(zAppend);
7323 ** Return true if zId must be quoted in order to use it as an SQL
7324 ** identifier, or false otherwise.
7326 static int idxIdentifierRequiresQuotes(const char *zId){
7328 for(i=0; zId[i]; i++){
7330 && !(zId[i]>='0' && zId[i]<='9')
7331 && !(zId[i]>='a' && zId[i]<='z')
7332 && !(zId[i]>='A' && zId[i]<='Z')
7341 ** This function appends an index column definition suitable for constraint
7342 ** pCons to the string passed as zIn and returns the result.
7344 static char *idxAppendColDefn(
7345 int *pRc, /* IN/OUT: Error code */
7346 char *zIn, /* Column defn accumulated so far */
7347 IdxTable *pTab, /* Table index will be created on */
7348 IdxConstraint *pCons
7351 IdxColumn *p = &pTab->aCol[pCons->iCol];
7352 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7354 if( idxIdentifierRequiresQuotes(p->zName) ){
7355 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7357 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7360 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7361 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7362 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7364 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7369 zRet = idxAppendText(pRc, zRet, " DESC");
7375 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7376 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7377 ** such an index is found, return non-zero. Or, if no such index is found,
7380 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7382 static int idxFindCompatible(
7383 int *pRc, /* OUT: Error code */
7384 sqlite3* dbm, /* Database to search */
7385 IdxScan *pScan, /* Scan for table to search for index on */
7386 IdxConstraint *pEq, /* List of == constraints */
7387 IdxConstraint *pTail /* List of range constraints */
7389 const char *zTbl = pScan->pTab->zName;
7390 sqlite3_stmt *pIdxList = 0;
7391 IdxConstraint *pIter;
7392 int nEq = 0; /* Number of elements in pEq */
7395 /* Count the elements in list pEq */
7396 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7398 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7399 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7401 IdxConstraint *pT = pTail;
7402 sqlite3_stmt *pInfo = 0;
7403 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7405 /* Zero the IdxConstraint.bFlag values in the pEq list */
7406 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7408 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7409 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7410 int iIdx = sqlite3_column_int(pInfo, 0);
7411 int iCol = sqlite3_column_int(pInfo, 1);
7412 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7415 for(pIter=pEq; pIter; pIter=pIter->pLink){
7416 if( pIter->bFlag ) continue;
7417 if( pIter->iCol!=iCol ) continue;
7418 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7428 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7436 idxFinalize(&rc, pInfo);
7438 if( rc==SQLITE_OK && bMatch ){
7439 sqlite3_finalize(pIdxList);
7443 idxFinalize(&rc, pIdxList);
7449 static int idxCreateFromCons(
7453 IdxConstraint *pTail
7455 sqlite3 *dbm = p->dbm;
7457 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7458 IdxTable *pTab = pScan->pTab;
7461 IdxConstraint *pCons;
7465 for(pCons=pEq; pCons; pCons=pCons->pLink){
7466 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7468 for(pCons=pTail; pCons; pCons=pCons->pLink){
7469 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7472 if( rc==SQLITE_OK ){
7473 /* Hash the list of columns to come up with a name for the index */
7474 const char *zTable = pScan->pTab->zName;
7475 char *zName; /* Index name */
7477 for(i=0; zCols[i]; i++){
7478 h += ((h<<3) + zCols[i]);
7480 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7484 if( idxIdentifierRequiresQuotes(zTable) ){
7485 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7487 zFmt = "CREATE INDEX %s ON %s(%s)";
7489 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7493 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7494 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7496 sqlite3_free(zName);
7501 sqlite3_free(zCols);
7507 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7508 ** a constraint compatible with *p. Otherwise return false.
7510 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7511 IdxConstraint *pCmp;
7512 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7513 if( p->iCol==pCmp->iCol ) return 1;
7518 static int idxCreateFromWhere(
7520 IdxScan *pScan, /* Create indexes for this scan */
7521 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
7523 IdxConstraint *p1 = 0;
7524 IdxConstraint *pCon;
7527 /* Gather up all the == constraints. */
7528 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7529 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7535 /* Create an index using the == constraints collected above. And the
7536 ** range constraint/ORDER BY terms passed in by the caller, if any. */
7537 rc = idxCreateFromCons(p, pScan, p1, pTail);
7539 /* If no range/ORDER BY passed by the caller, create a version of the
7540 ** index for each range constraint. */
7542 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7543 assert( pCon->pLink==0 );
7544 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7545 rc = idxCreateFromCons(p, pScan, p1, pCon);
7554 ** Create candidate indexes in database [dbm] based on the data in
7555 ** linked-list pScan.
7557 static int idxCreateCandidates(sqlite3expert *p){
7561 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7562 rc = idxCreateFromWhere(p, pIter, 0);
7563 if( rc==SQLITE_OK && pIter->pOrder ){
7564 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7572 ** Free all elements of the linked list starting at pConstraint.
7574 static void idxConstraintFree(IdxConstraint *pConstraint){
7575 IdxConstraint *pNext;
7578 for(p=pConstraint; p; p=pNext){
7585 ** Free all elements of the linked list starting from pScan up until pLast
7586 ** (pLast is not freed).
7588 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7591 for(p=pScan; p!=pLast; p=pNext){
7592 pNext = p->pNextScan;
7593 idxConstraintFree(p->pOrder);
7594 idxConstraintFree(p->pEq);
7595 idxConstraintFree(p->pRange);
7601 ** Free all elements of the linked list starting from pStatement up
7602 ** until pLast (pLast is not freed).
7604 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7606 IdxStatement *pNext;
7607 for(p=pStatement; p!=pLast; p=pNext){
7609 sqlite3_free(p->zEQP);
7610 sqlite3_free(p->zIdx);
7616 ** Free the linked list of IdxTable objects starting at pTab.
7618 static void idxTableFree(IdxTable *pTab){
7621 for(pIter=pTab; pIter; pIter=pNext){
7622 pNext = pIter->pNext;
7623 sqlite3_free(pIter);
7628 ** Free the linked list of IdxWrite objects starting at pTab.
7630 static void idxWriteFree(IdxWrite *pTab){
7633 for(pIter=pTab; pIter; pIter=pNext){
7634 pNext = pIter->pNext;
7635 sqlite3_free(pIter);
7642 ** This function is called after candidate indexes have been created. It
7643 ** runs all the queries to see which indexes they prefer, and populates
7644 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7648 char **pzErr /* OUT: Error message (sqlite3_malloc) */
7650 IdxStatement *pStmt;
7651 sqlite3 *dbm = p->dbm;
7657 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7658 IdxHashEntry *pEntry;
7659 sqlite3_stmt *pExplain = 0;
7660 idxHashClear(&hIdx);
7661 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7662 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7664 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7665 /* int iId = sqlite3_column_int(pExplain, 0); */
7666 /* int iParent = sqlite3_column_int(pExplain, 1); */
7667 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7668 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7669 int nDetail = STRLEN(zDetail);
7672 for(i=0; i<nDetail; i++){
7673 const char *zIdx = 0;
7674 if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7675 zIdx = &zDetail[i+13];
7676 }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7677 zIdx = &zDetail[i+22];
7682 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7685 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7687 idxHashAdd(&rc, &hIdx, zSql, 0);
7688 if( rc ) goto find_indexes_out;
7694 if( zDetail[0]!='-' ){
7695 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7699 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7700 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7703 idxFinalize(&rc, pExplain);
7707 idxHashClear(&hIdx);
7711 static int idxAuthCallback(
7717 const char *zTrigger
7722 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7723 if( sqlite3_stricmp(zDb, "main")==0 ){
7724 sqlite3expert *p = (sqlite3expert*)pCtx;
7726 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7727 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7731 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7732 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7735 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7736 if( rc==SQLITE_OK ){
7737 pWrite->pTab = pTab;
7739 pWrite->pNext = p->pWrite;
7749 static int idxProcessOneTrigger(
7754 static const char *zInt = UNIQUE_TABLE_NAME;
7755 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7756 IdxTable *pTab = pWrite->pTab;
7757 const char *zTab = pTab->zName;
7759 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7760 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7762 sqlite3_stmt *pSelect = 0;
7766 /* Create the table and its triggers in the temp schema */
7767 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7768 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7769 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7770 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7772 idxFinalize(&rc, pSelect);
7774 /* Rename the table in the temp schema to zInt */
7775 if( rc==SQLITE_OK ){
7776 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7780 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7785 switch( pWrite->eOp ){
7786 case SQLITE_INSERT: {
7788 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7789 for(i=0; i<pTab->nCol; i++){
7790 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7792 zWrite = idxAppendText(&rc, zWrite, ")");
7795 case SQLITE_UPDATE: {
7797 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7798 for(i=0; i<pTab->nCol; i++){
7799 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7806 assert( pWrite->eOp==SQLITE_DELETE );
7807 if( rc==SQLITE_OK ){
7808 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7809 if( zWrite==0 ) rc = SQLITE_NOMEM;
7814 if( rc==SQLITE_OK ){
7815 sqlite3_stmt *pX = 0;
7816 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7817 idxFinalize(&rc, pX);
7818 if( rc!=SQLITE_OK ){
7819 idxDatabaseError(p->dbv, pzErr);
7822 sqlite3_free(zWrite);
7824 if( rc==SQLITE_OK ){
7825 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7831 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7834 IdxWrite *pFirst = p->pWrite;
7836 while( rc==SQLITE_OK && pFirst!=pEnd ){
7838 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7839 rc = idxProcessOneTrigger(p, pIter, pzErr);
7849 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7850 int rc = idxRegisterVtab(p);
7851 sqlite3_stmt *pSchema = 0;
7853 /* For each table in the main db schema:
7855 ** 1) Add an entry to the p->pTable list, and
7856 ** 2) Create the equivalent virtual table in dbv.
7858 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7859 "SELECT type, name, sql, 1 FROM sqlite_master "
7860 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
7862 "SELECT type, name, sql, 2 FROM sqlite_master "
7863 "WHERE type = 'trigger'"
7864 " AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
7867 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
7868 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
7869 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
7870 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
7872 if( zType[0]=='v' || zType[1]=='r' ){
7873 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
7876 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
7877 if( rc==SQLITE_OK ){
7881 pTab->pNext = p->pTable;
7884 /* The statement the vtab will pass to sqlite3_declare_vtab() */
7885 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
7886 for(i=0; i<pTab->nCol; i++){
7887 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
7888 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
7891 zInner = idxAppendText(&rc, zInner, ")");
7893 /* The CVT statement to create the vtab */
7894 zOuter = idxAppendText(&rc, 0,
7895 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
7897 if( rc==SQLITE_OK ){
7898 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
7900 sqlite3_free(zInner);
7901 sqlite3_free(zOuter);
7905 idxFinalize(&rc, pSchema);
7909 struct IdxSampleCtx {
7911 double target; /* Target nRet/nRow value */
7912 double nRow; /* Number of rows seen */
7913 double nRet; /* Number of rows returned */
7916 static void idxSampleFunc(
7917 sqlite3_context *pCtx,
7919 sqlite3_value **argv
7921 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
7929 bRet = (p->nRet / p->nRow) <= p->target;
7932 sqlite3_randomness(2, (void*)&rnd);
7933 bRet = ((int)rnd % 100) <= p->iTarget;
7937 sqlite3_result_int(pCtx, bRet);
7939 p->nRet += (double)bRet;
7945 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
7946 i64 iVal; /* SQLITE_INTEGER value */
7947 double rVal; /* SQLITE_FLOAT value */
7948 int nByte; /* Bytes of space allocated at z */
7949 int n; /* Size of buffer z */
7950 char *z; /* SQLITE_TEXT/BLOB value */
7955 ** Implementation of scalar function rem().
7957 static void idxRemFunc(
7958 sqlite3_context *pCtx,
7960 sqlite3_value **argv
7962 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
7963 struct IdxRemSlot *pSlot;
7967 iSlot = sqlite3_value_int(argv[0]);
7968 assert( iSlot<=p->nSlot );
7969 pSlot = &p->aSlot[iSlot];
7971 switch( pSlot->eType ){
7976 case SQLITE_INTEGER:
7977 sqlite3_result_int64(pCtx, pSlot->iVal);
7981 sqlite3_result_double(pCtx, pSlot->rVal);
7985 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7989 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7993 pSlot->eType = sqlite3_value_type(argv[1]);
7994 switch( pSlot->eType ){
7999 case SQLITE_INTEGER:
8000 pSlot->iVal = sqlite3_value_int64(argv[1]);
8004 pSlot->rVal = sqlite3_value_double(argv[1]);
8009 int nByte = sqlite3_value_bytes(argv[1]);
8010 if( nByte>pSlot->nByte ){
8011 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8013 sqlite3_result_error_nomem(pCtx);
8016 pSlot->nByte = nByte*2;
8020 if( pSlot->eType==SQLITE_BLOB ){
8021 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8023 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8030 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8033 "SELECT max(i.seqno) FROM "
8034 " sqlite_master AS s, "
8035 " pragma_index_list(s.name) AS l, "
8036 " pragma_index_info(l.name) AS i "
8037 "WHERE s.type = 'table'";
8038 sqlite3_stmt *pMax = 0;
8041 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8042 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8043 *pnMax = sqlite3_column_int(pMax, 0) + 1;
8045 idxFinalize(&rc, pMax);
8050 static int idxPopulateOneStat1(
8052 sqlite3_stmt *pIndexXInfo,
8053 sqlite3_stmt *pWriteStat,
8063 sqlite3_stmt *pQuery = 0;
8067 assert( p->iSample>0 );
8069 /* Formulate the query text */
8070 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8071 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8072 const char *zComma = zCols==0 ? "" : ", ";
8073 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8074 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8075 zCols = idxAppendText(&rc, zCols,
8076 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8078 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8080 sqlite3_reset(pIndexXInfo);
8081 if( rc==SQLITE_OK ){
8082 if( p->iSample==100 ){
8083 zQuery = sqlite3_mprintf(
8084 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8087 zQuery = sqlite3_mprintf(
8088 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8092 sqlite3_free(zCols);
8093 sqlite3_free(zOrder);
8095 /* Formulate the query text */
8096 if( rc==SQLITE_OK ){
8097 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8098 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8100 sqlite3_free(zQuery);
8102 if( rc==SQLITE_OK ){
8103 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8105 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8106 IdxHashEntry *pEntry;
8108 for(i=0; i<=nCol; i++) aStat[i] = 1;
8109 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8111 for(i=0; i<nCol; i++){
8112 if( sqlite3_column_int(pQuery, i)==0 ) break;
8114 for(/*no-op*/; i<nCol; i++){
8119 if( rc==SQLITE_OK ){
8121 zStat = sqlite3_mprintf("%d", s0);
8122 if( zStat==0 ) rc = SQLITE_NOMEM;
8123 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8124 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8128 if( rc==SQLITE_OK ){
8129 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8130 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8131 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8132 sqlite3_step(pWriteStat);
8133 rc = sqlite3_reset(pWriteStat);
8136 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8138 assert( pEntry->zVal2==0 );
8139 pEntry->zVal2 = zStat;
8141 sqlite3_free(zStat);
8144 sqlite3_free(aStat);
8145 idxFinalize(&rc, pQuery);
8150 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8154 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8155 if( rc!=SQLITE_OK ) return rc;
8157 zSql = sqlite3_mprintf(
8158 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8160 if( zSql==0 ) return SQLITE_NOMEM;
8161 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8168 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8169 ** indexes have already been created in database sqlite3expert.dbm, this
8170 ** function populates sqlite_stat1 table in the same database.
8172 ** The stat1 data is generated by querying the
8174 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8177 struct IdxRemCtx *pCtx = 0;
8178 struct IdxSampleCtx samplectx;
8180 i64 iPrev = -100000;
8181 sqlite3_stmt *pAllIndex = 0;
8182 sqlite3_stmt *pIndexXInfo = 0;
8183 sqlite3_stmt *pWrite = 0;
8185 const char *zAllIndex =
8186 "SELECT s.rowid, s.name, l.name FROM "
8187 " sqlite_master AS s, "
8188 " pragma_index_list(s.name) AS l "
8189 "WHERE s.type = 'table'";
8190 const char *zIndexXInfo =
8191 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8192 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8194 /* If iSample==0, no sqlite_stat1 data is required. */
8195 if( p->iSample==0 ) return SQLITE_OK;
8197 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8198 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8200 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8202 if( rc==SQLITE_OK ){
8203 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8204 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8207 if( rc==SQLITE_OK ){
8208 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8209 rc = sqlite3_create_function(
8210 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8213 if( rc==SQLITE_OK ){
8214 rc = sqlite3_create_function(
8215 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8219 if( rc==SQLITE_OK ){
8220 pCtx->nSlot = nMax+1;
8221 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8223 if( rc==SQLITE_OK ){
8224 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8226 if( rc==SQLITE_OK ){
8227 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8230 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8231 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8232 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8233 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8234 if( p->iSample<100 && iPrev!=iRowid ){
8235 samplectx.target = (double)p->iSample / 100.0;
8236 samplectx.iTarget = p->iSample;
8237 samplectx.nRow = 0.0;
8238 samplectx.nRet = 0.0;
8239 rc = idxBuildSampleTable(p, zTab);
8240 if( rc!=SQLITE_OK ) break;
8242 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8245 if( rc==SQLITE_OK && p->iSample<100 ){
8246 rc = sqlite3_exec(p->dbv,
8247 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8251 idxFinalize(&rc, pAllIndex);
8252 idxFinalize(&rc, pIndexXInfo);
8253 idxFinalize(&rc, pWrite);
8255 for(i=0; i<pCtx->nSlot; i++){
8256 sqlite3_free(pCtx->aSlot[i].z);
8260 if( rc==SQLITE_OK ){
8261 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8264 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8269 ** Allocate a new sqlite3expert object.
8271 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8273 sqlite3expert *pNew;
8275 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8277 /* Open two in-memory databases to work with. The "vtab database" (dbv)
8278 ** will contain a virtual table corresponding to each real table in
8279 ** the user database schema, and a copy of each view. It is used to
8280 ** collect information regarding the WHERE, ORDER BY and other clauses
8281 ** of the user's query.
8283 if( rc==SQLITE_OK ){
8285 pNew->iSample = 100;
8286 rc = sqlite3_open(":memory:", &pNew->dbv);
8288 if( rc==SQLITE_OK ){
8289 rc = sqlite3_open(":memory:", &pNew->dbm);
8290 if( rc==SQLITE_OK ){
8291 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8296 /* Copy the entire schema of database [db] into [dbm]. */
8297 if( rc==SQLITE_OK ){
8299 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8300 "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8301 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8303 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8304 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8305 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8307 idxFinalize(&rc, pSql);
8310 /* Create the vtab schema */
8311 if( rc==SQLITE_OK ){
8312 rc = idxCreateVtabSchema(pNew, pzErrmsg);
8315 /* Register the auth callback with dbv */
8316 if( rc==SQLITE_OK ){
8317 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8320 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8321 ** return the new sqlite3expert handle. */
8322 if( rc!=SQLITE_OK ){
8323 sqlite3_expert_destroy(pNew);
8330 ** Configure an sqlite3expert object.
8332 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8337 case EXPERT_CONFIG_SAMPLE: {
8338 int iVal = va_arg(ap, int);
8339 if( iVal<0 ) iVal = 0;
8340 if( iVal>100 ) iVal = 100;
8345 rc = SQLITE_NOTFOUND;
8354 ** Add an SQL statement to the analysis.
8356 int sqlite3_expert_sql(
8357 sqlite3expert *p, /* From sqlite3_expert_new() */
8358 const char *zSql, /* SQL statement to add */
8359 char **pzErr /* OUT: Error message (if any) */
8361 IdxScan *pScanOrig = p->pScan;
8362 IdxStatement *pStmtOrig = p->pStatement;
8364 const char *zStmt = zSql;
8366 if( p->bRun ) return SQLITE_MISUSE;
8368 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8369 sqlite3_stmt *pStmt = 0;
8370 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8371 if( rc==SQLITE_OK ){
8374 const char *z = sqlite3_sql(pStmt);
8376 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8377 if( rc==SQLITE_OK ){
8378 pNew->zSql = (char*)&pNew[1];
8379 memcpy(pNew->zSql, z, n+1);
8380 pNew->pNext = p->pStatement;
8381 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8382 p->pStatement = pNew;
8384 sqlite3_finalize(pStmt);
8387 idxDatabaseError(p->dbv, pzErr);
8391 if( rc!=SQLITE_OK ){
8392 idxScanFree(p->pScan, pScanOrig);
8393 idxStatementFree(p->pStatement, pStmtOrig);
8394 p->pScan = pScanOrig;
8395 p->pStatement = pStmtOrig;
8401 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8403 IdxHashEntry *pEntry;
8405 /* Do trigger processing to collect any extra IdxScan structures */
8406 rc = idxProcessTriggers(p, pzErr);
8408 /* Create candidate indexes within the in-memory database file */
8409 if( rc==SQLITE_OK ){
8410 rc = idxCreateCandidates(p);
8413 /* Generate the stat1 data */
8414 if( rc==SQLITE_OK ){
8415 rc = idxPopulateStat1(p, pzErr);
8418 /* Formulate the EXPERT_REPORT_CANDIDATES text */
8419 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8420 p->zCandidates = idxAppendText(&rc, p->zCandidates,
8421 "%s;%s%s\n", pEntry->zVal,
8422 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8426 /* Figure out which of the candidate indexes are preferred by the query
8427 ** planner and report the results to the user. */
8428 if( rc==SQLITE_OK ){
8429 rc = idxFindIndexes(p, pzErr);
8432 if( rc==SQLITE_OK ){
8439 ** Return the total number of statements that have been added to this
8440 ** sqlite3expert using sqlite3_expert_sql().
8442 int sqlite3_expert_count(sqlite3expert *p){
8444 if( p->pStatement ) nRet = p->pStatement->iId+1;
8449 ** Return a component of the report.
8451 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8452 const char *zRet = 0;
8453 IdxStatement *pStmt;
8455 if( p->bRun==0 ) return 0;
8456 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8458 case EXPERT_REPORT_SQL:
8459 if( pStmt ) zRet = pStmt->zSql;
8461 case EXPERT_REPORT_INDEXES:
8462 if( pStmt ) zRet = pStmt->zIdx;
8464 case EXPERT_REPORT_PLAN:
8465 if( pStmt ) zRet = pStmt->zEQP;
8467 case EXPERT_REPORT_CANDIDATES:
8468 zRet = p->zCandidates;
8475 ** Free an sqlite3expert object.
8477 void sqlite3_expert_destroy(sqlite3expert *p){
8479 sqlite3_close(p->dbm);
8480 sqlite3_close(p->dbv);
8481 idxScanFree(p->pScan, 0);
8482 idxStatementFree(p->pStatement, 0);
8483 idxTableFree(p->pTable);
8484 idxWriteFree(p->pWrite);
8485 idxHashClear(&p->hIdx);
8486 sqlite3_free(p->zCandidates);
8491 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8493 /************************* End ../ext/expert/sqlite3expert.c ********************/
8495 #if defined(SQLITE_ENABLE_SESSION)
8497 ** State information for a single open session
8499 typedef struct OpenSession OpenSession;
8500 struct OpenSession {
8501 char *zName; /* Symbolic name for this session */
8502 int nFilter; /* Number of xFilter rejection GLOB patterns */
8503 char **azFilter; /* Array of xFilter rejection GLOB patterns */
8504 sqlite3_session *p; /* The open session */
8509 ** Shell output mode information from before ".explain on",
8510 ** saved so that it can be restored by ".explain off"
8512 typedef struct SavedModeInfo SavedModeInfo;
8513 struct SavedModeInfo {
8514 int valid; /* Is there legit data in here? */
8515 int mode; /* Mode prior to ".explain on" */
8516 int showHeader; /* The ".header" setting prior to ".explain on" */
8517 int colWidth[100]; /* Column widths prior to ".explain on" */
8520 typedef struct ExpertInfo ExpertInfo;
8522 sqlite3expert *pExpert;
8526 /* A single line in the EQP output */
8527 typedef struct EQPGraphRow EQPGraphRow;
8528 struct EQPGraphRow {
8529 int iEqpId; /* ID for this row */
8530 int iParentId; /* ID of the parent row */
8531 EQPGraphRow *pNext; /* Next row in sequence */
8532 char zText[1]; /* Text to display for this row */
8535 /* All EQP output is collected into an instance of the following */
8536 typedef struct EQPGraph EQPGraph;
8538 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
8539 EQPGraphRow *pLast; /* Last element of the pRow list */
8540 char zPrefix[100]; /* Graph prefix */
8544 ** State information about the database connection is contained in an
8545 ** instance of the following structure.
8547 typedef struct ShellState ShellState;
8549 sqlite3 *db; /* The database */
8550 u8 autoExplain; /* Automatically turn on .explain mode */
8551 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8552 u8 autoEQPtest; /* autoEQP is in test mode */
8553 u8 statsOn; /* True to display memory stats before each finalize */
8554 u8 scanstatsOn; /* True to display scan stats before each finalize */
8555 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8556 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
8557 u8 nEqpLevel; /* Depth of the EQP output graph */
8558 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
8559 int outCount; /* Revert to stdout when reaching zero */
8560 int cnt; /* Number of records displayed so far */
8561 FILE *out; /* Write results here */
8562 FILE *traceOut; /* Output for sqlite3_trace() */
8563 int nErr; /* Number of errors seen */
8564 int mode; /* An output mode setting */
8565 int modePrior; /* Saved mode */
8566 int cMode; /* temporary output mode for the current query */
8567 int normalMode; /* Output mode before ".explain on" */
8568 int writableSchema; /* True if PRAGMA writable_schema=ON */
8569 int showHeader; /* True to show column names in List or Column mode */
8570 int nCheck; /* Number of ".check" commands run */
8571 unsigned shellFlgs; /* Various flags */
8572 char *zDestTable; /* Name of destination table when MODE_Insert */
8573 char *zTempFile; /* Temporary file that might need deleting */
8574 char zTestcase[30]; /* Name of current test case */
8575 char colSeparator[20]; /* Column separator character for several modes */
8576 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
8577 char colSepPrior[20]; /* Saved column separator */
8578 char rowSepPrior[20]; /* Saved row separator */
8579 int colWidth[100]; /* Requested width of each column when in column mode*/
8580 int actualWidth[100]; /* Actual width of each column */
8581 char nullValue[20]; /* The text to print when a NULL comes back from
8583 char outfile[FILENAME_MAX]; /* Filename for *out */
8584 const char *zDbFilename; /* name of the database file */
8585 char *zFreeOnClose; /* Filename to free when closing */
8586 const char *zVfs; /* Name of VFS to use */
8587 sqlite3_stmt *pStmt; /* Current statement if any. */
8588 FILE *pLog; /* Write log output here */
8589 int *aiIndent; /* Array of indents used in MODE_Explain */
8590 int nIndent; /* Size of array aiIndent[] */
8591 int iIndent; /* Index of current op in aiIndent[] */
8592 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
8593 #if defined(SQLITE_ENABLE_SESSION)
8594 int nSession; /* Number of active sessions */
8595 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
8597 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
8601 /* Allowed values for ShellState.autoEQP
8603 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
8604 #define AUTOEQP_on 1 /* Automatic EQP is on */
8605 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
8606 #define AUTOEQP_full 3 /* Show full EXPLAIN */
8608 /* Allowed values for ShellState.openMode
8610 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8611 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
8612 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
8613 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
8614 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
8615 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
8618 ** These are the allowed shellFlgs values
8620 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
8621 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
8622 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
8623 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
8624 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
8625 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
8626 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
8629 ** Macros for testing and setting shellFlgs
8631 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
8632 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
8633 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
8636 ** These are the allowed modes.
8638 #define MODE_Line 0 /* One column per line. Blank line between records */
8639 #define MODE_Column 1 /* One record per line in neat columns */
8640 #define MODE_List 2 /* One record per line with a separator */
8641 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
8642 #define MODE_Html 4 /* Generate an XHTML table */
8643 #define MODE_Insert 5 /* Generate SQL "insert" statements */
8644 #define MODE_Quote 6 /* Quote values as for SQL */
8645 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
8646 #define MODE_Csv 8 /* Quote strings, numbers are plain */
8647 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
8648 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
8649 #define MODE_Pretty 11 /* Pretty-print schemas */
8650 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
8652 static const char *modeDescr[] = {
8669 ** These are the column/row/line separators used by the various
8670 ** import/export modes.
8672 #define SEP_Column "|"
8673 #define SEP_Row "\n"
8674 #define SEP_Tab "\t"
8675 #define SEP_Space " "
8676 #define SEP_Comma ","
8677 #define SEP_CrLf "\r\n"
8678 #define SEP_Unit "\x1F"
8679 #define SEP_Record "\x1E"
8682 ** A callback for the sqlite3_log() interface.
8684 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8685 ShellState *p = (ShellState*)pArg;
8686 if( p->pLog==0 ) return;
8687 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8692 ** SQL function: shell_putsnl(X)
8694 ** Write the text X to the screen (or whatever output is being directed)
8695 ** adding a newline at the end, and then return X.
8697 static void shellPutsFunc(
8698 sqlite3_context *pCtx,
8700 sqlite3_value **apVal
8702 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8704 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8705 sqlite3_result_value(pCtx, apVal[0]);
8709 ** SQL function: edit(VALUE)
8710 ** edit(VALUE,EDITOR)
8714 ** (1) Write VALUE into a temporary file.
8715 ** (2) Run program EDITOR on that temporary file.
8716 ** (3) Read the temporary file back and return its content as the result.
8717 ** (4) Delete the temporary file
8719 ** If the EDITOR argument is omitted, use the value in the VISUAL
8720 ** environment variable. If still there is no EDITOR, through an error.
8722 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8724 #ifndef SQLITE_NOHAVE_SYSTEM
8725 static void editFunc(
8726 sqlite3_context *context,
8728 sqlite3_value **argv
8730 const char *zEditor;
8731 char *zTempFile = 0;
8740 unsigned char *p = 0;
8743 zEditor = (const char*)sqlite3_value_text(argv[1]);
8745 zEditor = getenv("VISUAL");
8748 sqlite3_result_error(context, "no editor for edit()", -1);
8751 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8752 sqlite3_result_error(context, "NULL input to edit()", -1);
8755 db = sqlite3_context_db_handle(context);
8757 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8759 sqlite3_uint64 r = 0;
8760 sqlite3_randomness(sizeof(r), &r);
8761 zTempFile = sqlite3_mprintf("temp%llx", r);
8763 sqlite3_result_error_nomem(context);
8767 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8768 /* When writing the file to be edited, do \n to \r\n conversions on systems
8769 ** that want \r\n line endings */
8770 f = fopen(zTempFile, bBin ? "wb" : "w");
8772 sqlite3_result_error(context, "edit() cannot open temp file", -1);
8775 sz = sqlite3_value_bytes(argv[0]);
8777 x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8779 const char *z = (const char*)sqlite3_value_text(argv[0]);
8780 /* Remember whether or not the value originally contained \r\n */
8781 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
8782 x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8787 sqlite3_result_error(context, "edit() could not write the whole file", -1);
8790 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8792 sqlite3_result_error_nomem(context);
8798 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8801 f = fopen(zTempFile, "rb");
8803 sqlite3_result_error(context,
8804 "edit() cannot reopen temp file after edit", -1);
8807 fseek(f, 0, SEEK_END);
8810 p = sqlite3_malloc64( sz+(bBin==0) );
8812 sqlite3_result_error_nomem(context);
8815 x = fread(p, 1, sz, f);
8819 sqlite3_result_error(context, "could not read back the whole file", -1);
8823 sqlite3_result_blob64(context, p, sz, sqlite3_free);
8827 /* If the original contains \r\n then do no conversions back to \n */
8830 /* If the file did not originally contain \r\n then convert any new
8831 ** \r\n back into \n */
8832 for(i=j=0; i<sz; i++){
8833 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
8839 sqlite3_result_text64(context, (const char*)p, sz,
8840 sqlite3_free, SQLITE_UTF8);
8847 sqlite3_free(zTempFile);
8850 #endif /* SQLITE_NOHAVE_SYSTEM */
8853 ** Save or restore the current output mode
8855 static void outputModePush(ShellState *p){
8856 p->modePrior = p->mode;
8857 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8858 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8860 static void outputModePop(ShellState *p){
8861 p->mode = p->modePrior;
8862 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8863 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8867 ** Output the given string as a hex-encoded blob (eg. X'1234' )
8869 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
8871 char *zBlob = (char *)pBlob;
8872 raw_printf(out,"X'");
8873 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
8874 raw_printf(out,"'");
8878 ** Find a string that is not found anywhere in z[]. Return a pointer
8881 ** Try to use zA and zB first. If both of those are already found in z[]
8882 ** then make up some string and store it in the buffer zBuf.
8884 static const char *unused_string(
8885 const char *z, /* Result must not appear anywhere in z */
8886 const char *zA, const char *zB, /* Try these first */
8887 char *zBuf /* Space to store a generated string */
8890 if( strstr(z, zA)==0 ) return zA;
8891 if( strstr(z, zB)==0 ) return zB;
8893 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
8894 }while( strstr(z,zBuf)!=0 );
8899 ** Output the given string as a quoted string using SQL quoting conventions.
8901 ** See also: output_quoted_escaped_string()
8903 static void output_quoted_string(FILE *out, const char *z){
8906 setBinaryMode(out, 1);
8907 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8909 utf8_printf(out,"'%s'",z);
8911 raw_printf(out, "'");
8913 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8916 utf8_printf(out, "%.*s", i, z);
8920 raw_printf(out, "'");
8928 raw_printf(out, "'");
8930 setTextMode(out, 1);
8934 ** Output the given string as a quoted string using SQL quoting conventions.
8935 ** Additionallly , escape the "\n" and "\r" characters so that they do not
8936 ** get corrupted by end-of-line translation facilities in some operating
8939 ** This is like output_quoted_string() but with the addition of the \r\n
8940 ** escape mechanism.
8942 static void output_quoted_escaped_string(FILE *out, const char *z){
8945 setBinaryMode(out, 1);
8946 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
8948 utf8_printf(out,"'%s'",z);
8950 const char *zNL = 0;
8951 const char *zCR = 0;
8954 char zBuf1[20], zBuf2[20];
8955 for(i=0; z[i]; i++){
8956 if( z[i]=='\n' ) nNL++;
8957 if( z[i]=='\r' ) nCR++;
8960 raw_printf(out, "replace(");
8961 zNL = unused_string(z, "\\n", "\\012", zBuf1);
8964 raw_printf(out, "replace(");
8965 zCR = unused_string(z, "\\r", "\\015", zBuf2);
8967 raw_printf(out, "'");
8969 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
8972 utf8_printf(out, "%.*s", i, z);
8976 raw_printf(out, "'");
8984 raw_printf(out, "%s", zNL);
8987 raw_printf(out, "%s", zCR);
8989 raw_printf(out, "'");
8991 raw_printf(out, ",'%s',char(13))", zCR);
8994 raw_printf(out, ",'%s',char(10))", zNL);
8997 setTextMode(out, 1);
9001 ** Output the given string as a quoted according to C or TCL quoting rules.
9003 static void output_c_string(FILE *out, const char *z){
9006 while( (c = *(z++))!=0 ){
9013 }else if( c=='\t' ){
9016 }else if( c=='\n' ){
9019 }else if( c=='\r' ){
9022 }else if( !isprint(c&0xff) ){
9023 raw_printf(out, "\\%03o", c&0xff);
9032 ** Output the given string with characters that are special to
9035 static void output_html_string(FILE *out, const char *z){
9047 utf8_printf(out,"%.*s",i,z);
9050 raw_printf(out,"<");
9051 }else if( z[i]=='&' ){
9052 raw_printf(out,"&");
9053 }else if( z[i]=='>' ){
9054 raw_printf(out,">");
9055 }else if( z[i]=='\"' ){
9056 raw_printf(out,""");
9057 }else if( z[i]=='\'' ){
9058 raw_printf(out,"'");
9067 ** If a field contains any character identified by a 1 in the following
9068 ** array, then the string must be quoted for CSV.
9070 static const char needCsvQuote[] = {
9071 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9072 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9073 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
9074 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9075 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9076 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9077 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
9079 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9080 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9081 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9082 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9083 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9084 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9085 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9086 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9090 ** Output a single term of CSV. Actually, p->colSeparator is used for
9091 ** the separator, which may or may not be a comma. p->nullValue is
9092 ** the null value. Strings are quoted if necessary. The separator
9093 ** is only issued if bSep is true.
9095 static void output_csv(ShellState *p, const char *z, int bSep){
9098 utf8_printf(out,"%s",p->nullValue);
9101 int nSep = strlen30(p->colSeparator);
9102 for(i=0; z[i]; i++){
9103 if( needCsvQuote[((unsigned char*)z)[i]]
9104 || (z[i]==p->colSeparator[0] &&
9105 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
9111 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
9112 utf8_printf(out, "%s", zQuoted);
9113 sqlite3_free(zQuoted);
9115 utf8_printf(out, "%s", z);
9119 utf8_printf(p->out, "%s", p->colSeparator);
9124 ** This routine runs when the user presses Ctrl-C
9126 static void interrupt_handler(int NotUsed){
9127 UNUSED_PARAMETER(NotUsed);
9129 if( seenInterrupt>2 ) exit(1);
9130 if( globalDb ) sqlite3_interrupt(globalDb);
9133 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
9135 ** This routine runs for console events (e.g. Ctrl-C) on Win32
9137 static BOOL WINAPI ConsoleCtrlHandler(
9138 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
9140 if( dwCtrlType==CTRL_C_EVENT ){
9141 interrupt_handler(0);
9148 #ifndef SQLITE_OMIT_AUTHORIZATION
9150 ** When the ".auth ON" is set, the following authorizer callback is
9151 ** invoked. It always returns SQLITE_OK.
9153 static int shellAuth(
9161 ShellState *p = (ShellState*)pClientData;
9162 static const char *azAction[] = { 0,
9163 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
9164 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
9165 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
9166 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
9167 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
9168 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
9169 "PRAGMA", "READ", "SELECT",
9170 "TRANSACTION", "UPDATE", "ATTACH",
9171 "DETACH", "ALTER_TABLE", "REINDEX",
9172 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
9173 "FUNCTION", "SAVEPOINT", "RECURSIVE"
9181 utf8_printf(p->out, "authorizer: %s", azAction[op]);
9183 raw_printf(p->out, " ");
9185 output_c_string(p->out, az[i]);
9187 raw_printf(p->out, "NULL");
9190 raw_printf(p->out, "\n");
9196 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
9198 ** This routine converts some CREATE TABLE statements for shadow tables
9199 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
9201 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
9202 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
9203 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
9205 utf8_printf(out, "%s%s", z, zTail);
9208 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
9211 printSchemaLine(out, z, zTail);
9216 ** Return true if string z[] has nothing but whitespace and comments to the
9217 ** end of the first line.
9219 static int wsToEol(const char *z){
9221 for(i=0; z[i]; i++){
9222 if( z[i]=='\n' ) return 1;
9223 if( IsSpace(z[i]) ) continue;
9224 if( z[i]=='-' && z[i+1]=='-' ) return 1;
9231 ** Add a new entry to the EXPLAIN QUERY PLAN data
9233 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9235 int nText = strlen30(zText);
9236 if( p->autoEQPtest ){
9237 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9239 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9240 if( pNew==0 ) shell_out_of_memory();
9241 pNew->iEqpId = iEqpId;
9242 pNew->iParentId = p2;
9243 memcpy(pNew->zText, zText, nText+1);
9245 if( p->sGraph.pLast ){
9246 p->sGraph.pLast->pNext = pNew;
9248 p->sGraph.pRow = pNew;
9250 p->sGraph.pLast = pNew;
9254 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
9257 static void eqp_reset(ShellState *p){
9258 EQPGraphRow *pRow, *pNext;
9259 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
9260 pNext = pRow->pNext;
9263 memset(&p->sGraph, 0, sizeof(p->sGraph));
9266 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9267 ** pOld, or return the first such line if pOld is NULL
9269 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9270 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9271 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9275 /* Render a single level of the graph that has iEqpId as its parent. Called
9276 ** recursively to render sublevels.
9278 static void eqp_render_level(ShellState *p, int iEqpId){
9279 EQPGraphRow *pRow, *pNext;
9280 int n = strlen30(p->sGraph.zPrefix);
9282 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9283 pNext = eqp_next_row(p, iEqpId, pRow);
9285 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9286 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
9287 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9288 eqp_render_level(p, pRow->iEqpId);
9289 p->sGraph.zPrefix[n] = 0;
9295 ** Display and reset the EXPLAIN QUERY PLAN data
9297 static void eqp_render(ShellState *p){
9298 EQPGraphRow *pRow = p->sGraph.pRow;
9300 if( pRow->zText[0]=='-' ){
9301 if( pRow->pNext==0 ){
9305 utf8_printf(p->out, "%s\n", pRow->zText+3);
9306 p->sGraph.pRow = pRow->pNext;
9309 utf8_printf(p->out, "QUERY PLAN\n");
9311 p->sGraph.zPrefix[0] = 0;
9312 eqp_render_level(p, 0);
9318 ** This is the callback routine that the shell
9319 ** invokes for each row of a query result.
9321 static int shell_callback(
9323 int nArg, /* Number of result columns */
9324 char **azArg, /* Text of each result column */
9325 char **azCol, /* Column names */
9326 int *aiType /* Column types */
9329 ShellState *p = (ShellState*)pArg;
9331 if( azArg==0 ) return 0;
9335 if( azArg==0 ) break;
9336 for(i=0; i<nArg; i++){
9337 int len = strlen30(azCol[i] ? azCol[i] : "");
9338 if( len>w ) w = len;
9340 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
9341 for(i=0; i<nArg; i++){
9342 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
9343 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
9349 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
9350 const int *colWidth;
9353 if( p->cMode==MODE_Column ){
9354 colWidth = p->colWidth;
9355 showHdr = p->showHeader;
9356 rowSep = p->rowSeparator;
9358 colWidth = aExplainWidths;
9363 for(i=0; i<nArg; i++){
9365 if( i<ArraySize(p->colWidth) ){
9371 w = strlenChar(azCol[i] ? azCol[i] : "");
9373 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
9376 if( i<ArraySize(p->actualWidth) ){
9377 p->actualWidth[i] = w;
9380 utf8_width_print(p->out, w, azCol[i]);
9381 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9385 for(i=0; i<nArg; i++){
9387 if( i<ArraySize(p->actualWidth) ){
9388 w = p->actualWidth[i];
9393 utf8_printf(p->out,"%-*.*s%s",w,w,
9394 "----------------------------------------------------------"
9395 "----------------------------------------------------------",
9396 i==nArg-1 ? rowSep : " ");
9400 if( azArg==0 ) break;
9401 for(i=0; i<nArg; i++){
9403 if( i<ArraySize(p->actualWidth) ){
9404 w = p->actualWidth[i];
9408 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
9409 w = strlenChar(azArg[i]);
9411 if( i==1 && p->aiIndent && p->pStmt ){
9412 if( p->iIndent<p->nIndent ){
9413 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
9417 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
9418 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9422 case MODE_Semi: { /* .schema and .fullschema output */
9423 printSchemaLine(p->out, azArg[0], ";\n");
9426 case MODE_Pretty: { /* .schema and .fullschema with --indent */
9434 if( azArg[0]==0 ) break;
9435 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
9436 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
9438 utf8_printf(p->out, "%s;\n", azArg[0]);
9441 z = sqlite3_mprintf("%s", azArg[0]);
9443 for(i=0; IsSpace(z[i]); i++){}
9444 for(; (c = z[i])!=0; i++){
9446 if( z[j-1]=='\r' ) z[j-1] = '\n';
9447 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
9448 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
9453 while( j>0 && IsSpace(z[j-1]) ){ j--; }
9455 if( strlen30(z)>=79 ){
9456 for(i=j=0; (c = z[i])!=0; i++){ /* Copy changes from z[i] back to z[j] */
9459 }else if( c=='"' || c=='\'' || c=='`' ){
9463 }else if( c=='-' && z[i+1]=='-' ){
9469 if( nLine>0 && nParen==0 && j>0 ){
9470 printSchemaLineN(p->out, z, j, "\n");
9475 if( nParen==1 && cEnd==0
9476 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
9479 printSchemaLineN(p->out, z, j, "\n ");
9482 while( IsSpace(z[i+1]) ){ i++; }
9487 printSchemaLine(p->out, z, ";\n");
9492 if( p->cnt++==0 && p->showHeader ){
9493 for(i=0; i<nArg; i++){
9494 utf8_printf(p->out,"%s%s",azCol[i],
9495 i==nArg-1 ? p->rowSeparator : p->colSeparator);
9498 if( azArg==0 ) break;
9499 for(i=0; i<nArg; i++){
9501 if( z==0 ) z = p->nullValue;
9502 utf8_printf(p->out, "%s", z);
9504 utf8_printf(p->out, "%s", p->colSeparator);
9506 utf8_printf(p->out, "%s", p->rowSeparator);
9512 if( p->cnt++==0 && p->showHeader ){
9513 raw_printf(p->out,"<TR>");
9514 for(i=0; i<nArg; i++){
9515 raw_printf(p->out,"<TH>");
9516 output_html_string(p->out, azCol[i]);
9517 raw_printf(p->out,"</TH>\n");
9519 raw_printf(p->out,"</TR>\n");
9521 if( azArg==0 ) break;
9522 raw_printf(p->out,"<TR>");
9523 for(i=0; i<nArg; i++){
9524 raw_printf(p->out,"<TD>");
9525 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9526 raw_printf(p->out,"</TD>\n");
9528 raw_printf(p->out,"</TR>\n");
9532 if( p->cnt++==0 && p->showHeader ){
9533 for(i=0; i<nArg; i++){
9534 output_c_string(p->out,azCol[i] ? azCol[i] : "");
9535 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9537 utf8_printf(p->out, "%s", p->rowSeparator);
9539 if( azArg==0 ) break;
9540 for(i=0; i<nArg; i++){
9541 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9542 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9544 utf8_printf(p->out, "%s", p->rowSeparator);
9548 setBinaryMode(p->out, 1);
9549 if( p->cnt++==0 && p->showHeader ){
9550 for(i=0; i<nArg; i++){
9551 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
9553 utf8_printf(p->out, "%s", p->rowSeparator);
9556 for(i=0; i<nArg; i++){
9557 output_csv(p, azArg[i], i<nArg-1);
9559 utf8_printf(p->out, "%s", p->rowSeparator);
9561 setTextMode(p->out, 1);
9565 if( azArg==0 ) break;
9566 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
9567 if( p->showHeader ){
9568 raw_printf(p->out,"(");
9569 for(i=0; i<nArg; i++){
9570 if( i>0 ) raw_printf(p->out, ",");
9571 if( quoteChar(azCol[i]) ){
9572 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
9573 utf8_printf(p->out, "%s", z);
9576 raw_printf(p->out, "%s", azCol[i]);
9579 raw_printf(p->out,")");
9582 for(i=0; i<nArg; i++){
9583 raw_printf(p->out, i>0 ? "," : " VALUES(");
9584 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9585 utf8_printf(p->out,"NULL");
9586 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9587 if( ShellHasFlag(p, SHFLG_Newlines) ){
9588 output_quoted_string(p->out, azArg[i]);
9590 output_quoted_escaped_string(p->out, azArg[i]);
9592 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9593 utf8_printf(p->out,"%s", azArg[i]);
9594 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9596 double r = sqlite3_column_double(p->pStmt, i);
9598 memcpy(&ur,&r,sizeof(r));
9599 if( ur==0x7ff0000000000000LL ){
9600 raw_printf(p->out, "1e999");
9601 }else if( ur==0xfff0000000000000LL ){
9602 raw_printf(p->out, "-1e999");
9604 sqlite3_snprintf(50,z,"%!.20g", r);
9605 raw_printf(p->out, "%s", z);
9607 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9608 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9609 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9610 output_hex_blob(p->out, pBlob, nBlob);
9611 }else if( isNumber(azArg[i], 0) ){
9612 utf8_printf(p->out,"%s", azArg[i]);
9613 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
9614 output_quoted_string(p->out, azArg[i]);
9616 output_quoted_escaped_string(p->out, azArg[i]);
9619 raw_printf(p->out,");\n");
9623 if( azArg==0 ) break;
9624 if( p->cnt==0 && p->showHeader ){
9625 for(i=0; i<nArg; i++){
9626 if( i>0 ) raw_printf(p->out, ",");
9627 output_quoted_string(p->out, azCol[i]);
9629 raw_printf(p->out,"\n");
9632 for(i=0; i<nArg; i++){
9633 if( i>0 ) raw_printf(p->out, ",");
9634 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9635 utf8_printf(p->out,"NULL");
9636 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9637 output_quoted_string(p->out, azArg[i]);
9638 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9639 utf8_printf(p->out,"%s", azArg[i]);
9640 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9642 double r = sqlite3_column_double(p->pStmt, i);
9643 sqlite3_snprintf(50,z,"%!.20g", r);
9644 raw_printf(p->out, "%s", z);
9645 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9646 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9647 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9648 output_hex_blob(p->out, pBlob, nBlob);
9649 }else if( isNumber(azArg[i], 0) ){
9650 utf8_printf(p->out,"%s", azArg[i]);
9652 output_quoted_string(p->out, azArg[i]);
9655 raw_printf(p->out,"\n");
9659 if( p->cnt++==0 && p->showHeader ){
9660 for(i=0; i<nArg; i++){
9661 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9662 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
9664 utf8_printf(p->out, "%s", p->rowSeparator);
9666 if( azArg==0 ) break;
9667 for(i=0; i<nArg; i++){
9668 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9669 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
9671 utf8_printf(p->out, "%s", p->rowSeparator);
9675 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9683 ** This is the callback routine that the SQLite library
9684 ** invokes for each row of a query result.
9686 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
9687 /* since we don't have type info, call the shell_callback with a NULL value */
9688 return shell_callback(pArg, nArg, azArg, azCol, NULL);
9692 ** This is the callback routine from sqlite3_exec() that appends all
9693 ** output onto the end of a ShellText object.
9695 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
9696 ShellText *p = (ShellText*)pArg;
9698 UNUSED_PARAMETER(az);
9699 if( azArg==0 ) return 0;
9700 if( p->n ) appendText(p, "|", 0);
9701 for(i=0; i<nArg; i++){
9702 if( i ) appendText(p, ",", 0);
9703 if( azArg[i] ) appendText(p, azArg[i], 0);
9709 ** Generate an appropriate SELFTEST table in the main database.
9711 static void createSelftestTable(ShellState *p){
9714 "SAVEPOINT selftest_init;\n"
9715 "CREATE TABLE IF NOT EXISTS selftest(\n"
9716 " tno INTEGER PRIMARY KEY,\n" /* Test number */
9717 " op TEXT,\n" /* Operator: memo run */
9718 " cmd TEXT,\n" /* Command text */
9719 " ans TEXT\n" /* Desired answer */
9721 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
9722 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
9723 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
9724 " 'memo','Tests generated by --init');\n"
9725 "INSERT INTO [_shell$self]\n"
9727 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
9728 "FROM sqlite_master ORDER BY 2'',224))',\n"
9729 " hex(sha3_query('SELECT type,name,tbl_name,sql "
9730 "FROM sqlite_master ORDER BY 2',224));\n"
9731 "INSERT INTO [_shell$self]\n"
9733 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
9734 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
9735 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
9737 " SELECT name FROM sqlite_master\n"
9738 " WHERE type='table'\n"
9739 " AND name<>'selftest'\n"
9740 " AND coalesce(rootpage,0)>0\n"
9743 "INSERT INTO [_shell$self]\n"
9744 " VALUES('run','PRAGMA integrity_check','ok');\n"
9745 "INSERT INTO selftest(tno,op,cmd,ans)"
9746 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
9747 "DROP TABLE [_shell$self];"
9750 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
9751 sqlite3_free(zErrMsg);
9753 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9758 ** Set the destination table field of the ShellState structure to
9759 ** the name of the table given. Escape any quote characters in the
9762 static void set_table_name(ShellState *p, const char *zName){
9767 if( p->zDestTable ){
9768 free(p->zDestTable);
9771 if( zName==0 ) return;
9772 cQuote = quoteChar(zName);
9773 n = strlen30(zName);
9774 if( cQuote ) n += n+2;
9775 z = p->zDestTable = malloc( n+1 );
9776 if( z==0 ) shell_out_of_memory();
9778 if( cQuote ) z[n++] = cQuote;
9779 for(i=0; zName[i]; i++){
9781 if( zName[i]==cQuote ) z[n++] = cQuote;
9783 if( cQuote ) z[n++] = cQuote;
9789 ** Execute a query statement that will generate SQL output. Print
9790 ** the result columns, comma-separated, on a line and then add a
9791 ** semicolon terminator to the end of that line.
9793 ** If the number of columns is 1 and that column contains text "--"
9794 ** then write the semicolon on a separate line. That way, if a
9795 ** "--" comment occurs at the end of the statement, the comment
9796 ** won't consume the semicolon terminator.
9798 static int run_table_dump_query(
9799 ShellState *p, /* Query context */
9800 const char *zSelect, /* SELECT statement to extract content */
9801 const char *zFirstRow /* Print before first row, if not NULL */
9803 sqlite3_stmt *pSelect;
9808 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9809 if( rc!=SQLITE_OK || !pSelect ){
9810 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9811 sqlite3_errmsg(p->db));
9812 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9815 rc = sqlite3_step(pSelect);
9816 nResult = sqlite3_column_count(pSelect);
9817 while( rc==SQLITE_ROW ){
9819 utf8_printf(p->out, "%s", zFirstRow);
9822 z = (const char*)sqlite3_column_text(pSelect, 0);
9823 utf8_printf(p->out, "%s", z);
9824 for(i=1; i<nResult; i++){
9825 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
9828 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
9830 raw_printf(p->out, "\n;\n");
9832 raw_printf(p->out, ";\n");
9834 rc = sqlite3_step(pSelect);
9836 rc = sqlite3_finalize(pSelect);
9837 if( rc!=SQLITE_OK ){
9838 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9839 sqlite3_errmsg(p->db));
9840 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9846 ** Allocate space and save off current error string.
9848 static char *save_err_msg(
9849 sqlite3 *db /* Database to query */
9851 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
9852 char *zErrMsg = sqlite3_malloc64(nErrMsg);
9854 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
9861 ** Attempt to display I/O stats on Linux using /proc/PID/io
9863 static void displayLinuxIoStats(FILE *out){
9866 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
9867 in = fopen(z, "rb");
9869 while( fgets(z, sizeof(z), in)!=0 ){
9870 static const struct {
9871 const char *zPattern;
9874 { "rchar: ", "Bytes received by read():" },
9875 { "wchar: ", "Bytes sent to write():" },
9876 { "syscr: ", "Read() system calls:" },
9877 { "syscw: ", "Write() system calls:" },
9878 { "read_bytes: ", "Bytes read from storage:" },
9879 { "write_bytes: ", "Bytes written to storage:" },
9880 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
9883 for(i=0; i<ArraySize(aTrans); i++){
9884 int n = strlen30(aTrans[i].zPattern);
9885 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
9886 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
9896 ** Display a single line of status using 64-bit values.
9898 static void displayStatLine(
9899 ShellState *p, /* The shell context */
9900 char *zLabel, /* Label for this one line */
9901 char *zFormat, /* Format for the result */
9902 int iStatusCtrl, /* Which status to display */
9903 int bReset /* True to reset the stats */
9905 sqlite3_int64 iCur = -1;
9906 sqlite3_int64 iHiwtr = -1;
9909 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
9910 for(i=0, nPercent=0; zFormat[i]; i++){
9911 if( zFormat[i]=='%' ) nPercent++;
9914 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
9916 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
9918 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
9922 ** Display memory stats.
9924 static int display_stats(
9925 sqlite3 *db, /* Database to query */
9926 ShellState *pArg, /* Pointer to ShellState */
9927 int bReset /* True to reset the stats */
9932 if( pArg==0 || pArg->out==0 ) return 0;
9935 if( pArg->pStmt && (pArg->statsOn & 2) ){
9937 sqlite3_stmt *pStmt = pArg->pStmt;
9939 nCol = sqlite3_column_count(pStmt);
9940 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
9941 for(i=0; i<nCol; i++){
9942 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
9943 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
9944 #ifndef SQLITE_OMIT_DECLTYPE
9945 sqlite3_snprintf(30, z+x, "declared type:");
9946 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
9948 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9949 sqlite3_snprintf(30, z+x, "database name:");
9950 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
9951 sqlite3_snprintf(30, z+x, "table name:");
9952 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
9953 sqlite3_snprintf(30, z+x, "origin name:");
9954 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
9959 displayStatLine(pArg, "Memory Used:",
9960 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9961 displayStatLine(pArg, "Number of Outstanding Allocations:",
9962 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9963 if( pArg->shellFlgs & SHFLG_Pagecache ){
9964 displayStatLine(pArg, "Number of Pcache Pages Used:",
9965 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9967 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9968 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9969 displayStatLine(pArg, "Largest Allocation:",
9970 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9971 displayStatLine(pArg, "Largest Pcache Allocation:",
9972 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9973 #ifdef YYTRACKMAXSTACKDEPTH
9974 displayStatLine(pArg, "Deepest Parser Stack:",
9975 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9979 if( pArg->shellFlgs & SHFLG_Lookaside ){
9981 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9982 &iCur, &iHiwtr, bReset);
9983 raw_printf(pArg->out,
9984 "Lookaside Slots Used: %d (max %d)\n",
9986 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
9987 &iCur, &iHiwtr, bReset);
9988 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
9990 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
9991 &iCur, &iHiwtr, bReset);
9992 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
9994 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
9995 &iCur, &iHiwtr, bReset);
9996 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
10000 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
10001 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
10003 iHiwtr = iCur = -1;
10004 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
10005 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
10006 iHiwtr = iCur = -1;
10007 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
10008 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
10009 iHiwtr = iCur = -1;
10010 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
10011 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
10012 iHiwtr = iCur = -1;
10013 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
10014 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
10015 iHiwtr = iCur = -1;
10016 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
10017 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
10019 iHiwtr = iCur = -1;
10020 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
10021 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
10026 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
10028 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
10029 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
10030 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
10031 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
10032 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
10033 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
10034 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
10035 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
10036 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
10037 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
10038 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
10039 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
10040 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
10044 displayLinuxIoStats(pArg->out);
10047 /* Do not remove this machine readable comment: extra-stats-output-here */
10053 ** Display scan stats.
10055 static void display_scanstats(
10056 sqlite3 *db, /* Database to query */
10057 ShellState *pArg /* Pointer to ShellState */
10059 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10060 UNUSED_PARAMETER(db);
10061 UNUSED_PARAMETER(pArg);
10064 raw_printf(pArg->out, "-------- scanstats --------\n");
10066 for(k=0; k<=mx; k++){
10067 double rEstLoop = 1.0;
10068 for(i=n=0; 1; i++){
10069 sqlite3_stmt *p = pArg->pStmt;
10070 sqlite3_int64 nLoop, nVisit;
10073 const char *zExplain;
10074 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
10077 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
10078 if( iSid>mx ) mx = iSid;
10079 if( iSid!=k ) continue;
10081 rEstLoop = (double)nLoop;
10082 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
10085 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
10086 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
10087 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
10088 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
10090 raw_printf(pArg->out,
10091 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
10092 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
10096 raw_printf(pArg->out, "---------------------------\n");
10101 ** Parameter azArray points to a zero-terminated array of strings. zStr
10102 ** points to a single nul-terminated string. Return non-zero if zStr
10103 ** is equal, according to strcmp(), to any of the strings in the array.
10104 ** Otherwise, return zero.
10106 static int str_in_array(const char *zStr, const char **azArray){
10108 for(i=0; azArray[i]; i++){
10109 if( 0==strcmp(zStr, azArray[i]) ) return 1;
10115 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
10116 ** and populate the ShellState.aiIndent[] array with the number of
10117 ** spaces each opcode should be indented before it is output.
10119 ** The indenting rules are:
10121 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
10122 ** all opcodes that occur between the p2 jump destination and the opcode
10123 ** itself by 2 spaces.
10125 ** * For each "Goto", if the jump destination is earlier in the program
10126 ** and ends on one of:
10127 ** Yield SeekGt SeekLt RowSetRead Rewind
10128 ** or if the P1 parameter is one instead of zero,
10129 ** then indent all opcodes between the earlier instruction
10130 ** and "Goto" by 2 spaces.
10132 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
10133 const char *zSql; /* The text of the SQL statement */
10134 const char *z; /* Used to check if this is an EXPLAIN */
10135 int *abYield = 0; /* True if op is an OP_Yield */
10136 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
10137 int iOp; /* Index of operation in p->aiIndent[] */
10139 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
10140 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
10142 const char *azGoto[] = { "Goto", 0 };
10144 /* Try to figure out if this is really an EXPLAIN statement. If this
10145 ** cannot be verified, return early. */
10146 if( sqlite3_column_count(pSql)!=8 ){
10147 p->cMode = p->mode;
10150 zSql = sqlite3_sql(pSql);
10151 if( zSql==0 ) return;
10152 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
10153 if( sqlite3_strnicmp(z, "explain", 7) ){
10154 p->cMode = p->mode;
10158 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
10160 int iAddr = sqlite3_column_int(pSql, 0);
10161 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
10163 /* Set p2 to the P2 field of the current opcode. Then, assuming that
10164 ** p2 is an instruction address, set variable p2op to the index of that
10165 ** instruction in the aiIndent[] array. p2 and p2op may be different if
10166 ** the current instruction is part of a sub-program generated by an
10167 ** SQL trigger or foreign key. */
10168 int p2 = sqlite3_column_int(pSql, 3);
10169 int p2op = (p2 + (iOp-iAddr));
10171 /* Grow the p->aiIndent array as required */
10174 /* Do further verfication that this is explain output. Abort if
10176 static const char *explainCols[] = {
10177 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
10179 for(jj=0; jj<ArraySize(explainCols); jj++){
10180 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
10181 p->cMode = p->mode;
10182 sqlite3_reset(pSql);
10188 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
10189 if( p->aiIndent==0 ) shell_out_of_memory();
10190 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
10191 if( abYield==0 ) shell_out_of_memory();
10193 abYield[iOp] = str_in_array(zOp, azYield);
10194 p->aiIndent[iOp] = 0;
10195 p->nIndent = iOp+1;
10197 if( str_in_array(zOp, azNext) ){
10198 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10200 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
10201 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
10203 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10208 sqlite3_free(abYield);
10209 sqlite3_reset(pSql);
10213 ** Free the array allocated by explain_data_prepare().
10215 static void explain_data_delete(ShellState *p){
10216 sqlite3_free(p->aiIndent);
10223 ** Disable and restore .wheretrace and .selecttrace settings.
10225 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10226 extern int sqlite3SelectTrace;
10227 static int savedSelectTrace;
10229 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10230 extern int sqlite3WhereTrace;
10231 static int savedWhereTrace;
10233 static void disable_debug_trace_modes(void){
10234 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10235 savedSelectTrace = sqlite3SelectTrace;
10236 sqlite3SelectTrace = 0;
10238 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10239 savedWhereTrace = sqlite3WhereTrace;
10240 sqlite3WhereTrace = 0;
10243 static void restore_debug_trace_modes(void){
10244 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10245 sqlite3SelectTrace = savedSelectTrace;
10247 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10248 sqlite3WhereTrace = savedWhereTrace;
10253 ** Run a prepared statement
10255 static void exec_prepared_stmt(
10256 ShellState *pArg, /* Pointer to ShellState */
10257 sqlite3_stmt *pStmt /* Statment to run */
10261 /* perform the first step. this will tell us if we
10262 ** have a result set or not and how wide it is.
10264 rc = sqlite3_step(pStmt);
10265 /* if we have a result set... */
10266 if( SQLITE_ROW == rc ){
10267 /* allocate space for col name ptr, value ptr, and type */
10268 int nCol = sqlite3_column_count(pStmt);
10269 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10273 char **azCols = (char **)pData; /* Names of result columns */
10274 char **azVals = &azCols[nCol]; /* Results */
10275 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10277 assert(sizeof(int) <= sizeof(char *));
10278 /* save off ptrs to column names */
10279 for(i=0; i<nCol; i++){
10280 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10283 /* extract the data and data types */
10284 for(i=0; i<nCol; i++){
10285 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10286 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10289 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10291 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10293 break; /* from for */
10297 /* if data and types extracted successfully... */
10298 if( SQLITE_ROW == rc ){
10299 /* call the supplied callback with the result row data */
10300 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10303 rc = sqlite3_step(pStmt);
10306 } while( SQLITE_ROW == rc );
10307 sqlite3_free(pData);
10312 #ifndef SQLITE_OMIT_VIRTUALTABLE
10314 ** This function is called to process SQL if the previous shell command
10315 ** was ".expert". It passes the SQL in the second argument directly to
10316 ** the sqlite3expert object.
10318 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10319 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10320 ** an English language error message. It is the responsibility of the
10321 ** caller to eventually free this buffer using sqlite3_free().
10323 static int expertHandleSQL(
10324 ShellState *pState,
10328 assert( pState->expert.pExpert );
10329 assert( pzErr==0 || *pzErr==0 );
10330 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10334 ** This function is called either to silently clean up the object
10335 ** created by the ".expert" command (if bCancel==1), or to generate a
10336 ** report from it and then clean it up (if bCancel==0).
10338 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10339 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10340 ** an English language error message. It is the responsibility of the
10341 ** caller to eventually free this buffer using sqlite3_free().
10343 static int expertFinish(
10344 ShellState *pState,
10348 int rc = SQLITE_OK;
10349 sqlite3expert *p = pState->expert.pExpert;
10351 assert( bCancel || pzErr==0 || *pzErr==0 );
10353 FILE *out = pState->out;
10354 int bVerbose = pState->expert.bVerbose;
10356 rc = sqlite3_expert_analyze(p, pzErr);
10357 if( rc==SQLITE_OK ){
10358 int nQuery = sqlite3_expert_count(p);
10362 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10363 raw_printf(out, "-- Candidates -----------------------------\n");
10364 raw_printf(out, "%s\n", zCand);
10366 for(i=0; i<nQuery; i++){
10367 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10368 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10369 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10370 if( zIdx==0 ) zIdx = "(no new indexes)\n";
10372 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10373 raw_printf(out, "%s\n\n", zSql);
10375 raw_printf(out, "%s\n", zIdx);
10376 raw_printf(out, "%s\n", zEQP);
10380 sqlite3_expert_destroy(p);
10381 pState->expert.pExpert = 0;
10386 ** Implementation of ".expert" dot command.
10388 static int expertDotCommand(
10389 ShellState *pState, /* Current shell tool state */
10390 char **azArg, /* Array of arguments passed to dot command */
10391 int nArg /* Number of entries in azArg[] */
10393 int rc = SQLITE_OK;
10398 assert( pState->expert.pExpert==0 );
10399 memset(&pState->expert, 0, sizeof(ExpertInfo));
10401 for(i=1; rc==SQLITE_OK && i<nArg; i++){
10402 char *z = azArg[i];
10404 if( z[0]=='-' && z[1]=='-' ) z++;
10406 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10407 pState->expert.bVerbose = 1;
10409 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10411 raw_printf(stderr, "option requires an argument: %s\n", z);
10414 iSample = (int)integerValue(azArg[++i]);
10415 if( iSample<0 || iSample>100 ){
10416 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10422 raw_printf(stderr, "unknown option: %s\n", z);
10427 if( rc==SQLITE_OK ){
10428 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10429 if( pState->expert.pExpert==0 ){
10430 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10433 sqlite3_expert_config(
10434 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10441 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10444 ** Execute a statement or set of statements. Print
10445 ** any result rows/columns depending on the current mode
10446 ** set via the supplied callback.
10448 ** This is very similar to SQLite's built-in sqlite3_exec()
10449 ** function except it takes a slightly different callback
10450 ** and callback data argument.
10452 static int shell_exec(
10453 ShellState *pArg, /* Pointer to ShellState */
10454 const char *zSql, /* SQL to be evaluated */
10455 char **pzErrMsg /* Error msg written here */
10457 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10458 int rc = SQLITE_OK; /* Return Code */
10460 const char *zLeftover; /* Tail of unprocessed SQL */
10461 sqlite3 *db = pArg->db;
10467 #ifndef SQLITE_OMIT_VIRTUALTABLE
10468 if( pArg->expert.pExpert ){
10469 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10470 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10474 while( zSql[0] && (SQLITE_OK == rc) ){
10475 static const char *zStmtSql;
10476 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10477 if( SQLITE_OK != rc ){
10479 *pzErrMsg = save_err_msg(db);
10483 /* this happens for a comment or white-space */
10485 while( IsSpace(zSql[0]) ) zSql++;
10488 zStmtSql = sqlite3_sql(pStmt);
10489 if( zStmtSql==0 ) zStmtSql = "";
10490 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10492 /* save off the prepared statment handle and reset row count */
10494 pArg->pStmt = pStmt;
10498 /* echo the sql statement if echo on */
10499 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10500 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10503 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10504 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10505 sqlite3_stmt *pExplain;
10507 int triggerEQP = 0;
10508 disable_debug_trace_modes();
10509 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10510 if( pArg->autoEQP>=AUTOEQP_trigger ){
10511 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10513 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10514 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10515 if( rc==SQLITE_OK ){
10516 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10517 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10518 int iEqpId = sqlite3_column_int(pExplain, 0);
10519 int iParentId = sqlite3_column_int(pExplain, 1);
10520 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10521 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10525 sqlite3_finalize(pExplain);
10526 sqlite3_free(zEQP);
10527 if( pArg->autoEQP>=AUTOEQP_full ){
10528 /* Also do an EXPLAIN for ".eqp full" mode */
10529 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10530 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10531 if( rc==SQLITE_OK ){
10532 pArg->cMode = MODE_Explain;
10533 explain_data_prepare(pArg, pExplain);
10534 exec_prepared_stmt(pArg, pExplain);
10535 explain_data_delete(pArg);
10537 sqlite3_finalize(pExplain);
10538 sqlite3_free(zEQP);
10540 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10541 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10542 /* Reprepare pStmt before reactiving trace modes */
10543 sqlite3_finalize(pStmt);
10544 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10545 if( pArg ) pArg->pStmt = pStmt;
10547 restore_debug_trace_modes();
10551 pArg->cMode = pArg->mode;
10552 if( pArg->autoExplain ){
10553 if( sqlite3_column_count(pStmt)==8
10554 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10556 pArg->cMode = MODE_Explain;
10558 if( sqlite3_column_count(pStmt)==4
10559 && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10560 pArg->cMode = MODE_EQP;
10564 /* If the shell is currently in ".explain" mode, gather the extra
10565 ** data required to add indents to the output.*/
10566 if( pArg->cMode==MODE_Explain ){
10567 explain_data_prepare(pArg, pStmt);
10571 exec_prepared_stmt(pArg, pStmt);
10572 explain_data_delete(pArg);
10575 /* print usage stats if stats on */
10576 if( pArg && pArg->statsOn ){
10577 display_stats(db, pArg, 0);
10580 /* print loop-counters if required */
10581 if( pArg && pArg->scanstatsOn ){
10582 display_scanstats(db, pArg);
10585 /* Finalize the statement just executed. If this fails, save a
10586 ** copy of the error message. Otherwise, set zSql to point to the
10587 ** next statement to execute. */
10588 rc2 = sqlite3_finalize(pStmt);
10589 if( rc!=SQLITE_NOMEM ) rc = rc2;
10590 if( rc==SQLITE_OK ){
10592 while( IsSpace(zSql[0]) ) zSql++;
10593 }else if( pzErrMsg ){
10594 *pzErrMsg = save_err_msg(db);
10597 /* clear saved stmt handle */
10599 pArg->pStmt = NULL;
10608 ** Release memory previously allocated by tableColumnList().
10610 static void freeColumnList(char **azCol){
10612 for(i=1; azCol[i]; i++){
10613 sqlite3_free(azCol[i]);
10615 /* azCol[0] is a static string */
10616 sqlite3_free(azCol);
10620 ** Return a list of pointers to strings which are the names of all
10621 ** columns in table zTab. The memory to hold the names is dynamically
10622 ** allocated and must be released by the caller using a subsequent call
10623 ** to freeColumnList().
10625 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
10626 ** value that needs to be preserved, then azCol[0] is filled in with the
10627 ** name of the rowid column.
10629 ** The first regular column in the table is azCol[1]. The list is terminated
10630 ** by an entry with azCol[i]==0.
10632 static char **tableColumnList(ShellState *p, const char *zTab){
10634 sqlite3_stmt *pStmt;
10638 int nPK = 0; /* Number of PRIMARY KEY columns seen */
10639 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
10640 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10643 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10644 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10645 sqlite3_free(zSql);
10647 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10648 if( nCol>=nAlloc-2 ){
10649 nAlloc = nAlloc*2 + nCol + 10;
10650 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10651 if( azCol==0 ) shell_out_of_memory();
10653 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10654 if( sqlite3_column_int(pStmt, 5) ){
10657 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10666 sqlite3_finalize(pStmt);
10667 if( azCol==0 ) return 0;
10671 /* The decision of whether or not a rowid really needs to be preserved
10672 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
10673 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
10674 ** rowids on tables where the rowid is inaccessible because there are other
10675 ** columns in the table named "rowid", "_rowid_", and "oid".
10677 if( preserveRowid && isIPK ){
10678 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10679 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
10680 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10681 ** ROWID aliases. To distinguish these cases, check to see if
10682 ** there is a "pk" entry in "PRAGMA index_list". There will be
10683 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10685 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10686 " WHERE origin='pk'", zTab);
10687 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10688 sqlite3_free(zSql);
10690 freeColumnList(azCol);
10693 rc = sqlite3_step(pStmt);
10694 sqlite3_finalize(pStmt);
10695 preserveRowid = rc==SQLITE_ROW;
10697 if( preserveRowid ){
10698 /* Only preserve the rowid if we can find a name to use for the
10700 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10702 for(j=0; j<3; j++){
10703 for(i=1; i<=nCol; i++){
10704 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10707 /* At this point, we know that azRowid[j] is not the name of any
10708 ** ordinary column in the table. Verify that azRowid[j] is a valid
10709 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
10710 ** tables will fail this last check */
10711 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10712 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10721 ** Toggle the reverse_unordered_selects setting.
10723 static void toggleSelectOrder(sqlite3 *db){
10724 sqlite3_stmt *pStmt = 0;
10727 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10728 if( sqlite3_step(pStmt)==SQLITE_ROW ){
10729 iSetting = sqlite3_column_int(pStmt, 0);
10731 sqlite3_finalize(pStmt);
10732 sqlite3_snprintf(sizeof(zStmt), zStmt,
10733 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10734 sqlite3_exec(db, zStmt, 0, 0, 0);
10738 ** This is a different callback routine used for dumping the database.
10739 ** Each row received by this callback consists of a table name,
10740 ** the table type ("index" or "table") and SQL to create the table.
10741 ** This routine should print text sufficient to recreate the table.
10743 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10745 const char *zTable;
10748 ShellState *p = (ShellState *)pArg;
10750 UNUSED_PARAMETER(azNotUsed);
10751 if( nArg!=3 || azArg==0 ) return 0;
10756 if( strcmp(zTable, "sqlite_sequence")==0 ){
10757 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10758 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10759 raw_printf(p->out, "ANALYZE sqlite_master;\n");
10760 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
10762 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
10764 if( !p->writableSchema ){
10765 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
10766 p->writableSchema = 1;
10768 zIns = sqlite3_mprintf(
10769 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
10770 "VALUES('table','%q','%q',0,'%q');",
10771 zTable, zTable, zSql);
10772 utf8_printf(p->out, "%s\n", zIns);
10773 sqlite3_free(zIns);
10776 printSchemaLine(p->out, zSql, ";\n");
10779 if( strcmp(zType, "table")==0 ){
10784 char *savedDestTable;
10787 azCol = tableColumnList(p, zTable);
10793 /* Always quote the table name, even if it appears to be pure ascii,
10794 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
10796 appendText(&sTable, zTable, quoteChar(zTable));
10797 /* If preserving the rowid, add a column list after the table name.
10798 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10799 ** instead of the usual "INSERT INTO tab VALUES(...)".
10802 appendText(&sTable, "(", 0);
10803 appendText(&sTable, azCol[0], 0);
10804 for(i=1; azCol[i]; i++){
10805 appendText(&sTable, ",", 0);
10806 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10808 appendText(&sTable, ")", 0);
10811 /* Build an appropriate SELECT statement */
10812 initText(&sSelect);
10813 appendText(&sSelect, "SELECT ", 0);
10815 appendText(&sSelect, azCol[0], 0);
10816 appendText(&sSelect, ",", 0);
10818 for(i=1; azCol[i]; i++){
10819 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
10821 appendText(&sSelect, ",", 0);
10824 freeColumnList(azCol);
10825 appendText(&sSelect, " FROM ", 0);
10826 appendText(&sSelect, zTable, quoteChar(zTable));
10828 savedDestTable = p->zDestTable;
10829 savedMode = p->mode;
10830 p->zDestTable = sTable.z;
10831 p->mode = p->cMode = MODE_Insert;
10832 rc = shell_exec(p, sSelect.z, 0);
10833 if( (rc&0xff)==SQLITE_CORRUPT ){
10834 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10835 toggleSelectOrder(p->db);
10836 shell_exec(p, sSelect.z, 0);
10837 toggleSelectOrder(p->db);
10839 p->zDestTable = savedDestTable;
10840 p->mode = savedMode;
10842 freeText(&sSelect);
10843 if( rc ) p->nErr++;
10849 ** Run zQuery. Use dump_callback() as the callback routine so that
10850 ** the contents of the query are output as SQL statements.
10852 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
10853 ** "ORDER BY rowid DESC" to the end.
10855 static int run_schema_dump_query(
10861 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
10862 if( rc==SQLITE_CORRUPT ){
10864 int len = strlen30(zQuery);
10865 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10867 utf8_printf(p->out, "/****** %s ******/\n", zErr);
10868 sqlite3_free(zErr);
10871 zQ2 = malloc( len+100 );
10872 if( zQ2==0 ) return rc;
10873 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
10874 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
10876 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
10878 rc = SQLITE_CORRUPT;
10880 sqlite3_free(zErr);
10887 ** Text of help messages.
10889 ** The help text for each individual command begins with a line that starts
10890 ** with ".". Subsequent lines are supplimental information.
10892 ** There must be two or more spaces between the end of the command and the
10893 ** start of the description of what that command does.
10895 static const char *(azHelp[]) = {
10896 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10897 ".archive ... Manage SQL archives",
10898 " Each command must have exactly one of the following options:",
10899 " -c, --create Create a new archive",
10900 " -u, --update Update or add files to an existing archive",
10901 " -t, --list List contents of archive",
10902 " -x, --extract Extract files from archive",
10903 " Optional arguments:",
10904 " -v, --verbose Print each filename as it is processed",
10905 " -f FILE, --file FILE Operate on archive FILE (default is current db)",
10906 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS",
10907 " -C DIR, --directory DIR Change to directory DIR to read/extract files",
10908 " -n, --dryrun Show the SQL that would have occurred",
10910 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar",
10911 " .ar -tf archive.sar # List members of archive.sar",
10912 " .ar -xvf archive.sar # Verbosely extract files from archive.sar",
10914 " http://sqlite.org/cli.html#sqlar_archive_support",
10916 #ifndef SQLITE_OMIT_AUTHORIZATION
10917 ".auth ON|OFF Show authorizer callbacks",
10919 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
10920 " --append Use the appendvfs",
10921 ".bail on|off Stop after hitting an error. Default OFF",
10922 ".binary on|off Turn binary output on or off. Default OFF",
10923 ".cd DIRECTORY Change the working directory to DIRECTORY",
10924 ".changes on|off Show number of rows changed by SQL",
10925 ".check GLOB Fail if output since .testcase does not match",
10926 ".clone NEWDB Clone data into NEWDB from the existing database",
10927 ".databases List names and files of attached databases",
10928 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
10929 ".dbinfo ?DB? Show status information about the database",
10930 ".dump ?TABLE? ... Render all database content as SQL",
10932 " --preserve-rowids Include ROWID values in the output",
10933 " --newlines Allow unescaped newline characters in output",
10934 " TABLE is LIKE pattern for the tables to dump",
10935 ".echo on|off Turn command echo on or off",
10936 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN",
10937 ".excel Display the output of next command in a spreadsheet",
10938 ".exit ?CODE? Exit this program with return-code CODE",
10939 ".expert EXPERIMENTAL. Suggest indexes for specified queries",
10940 /* Because explain mode comes on automatically now, the ".explain" mode
10941 ** is removed from the help screen. It is still supported for legacy, however */
10942 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic",*/
10943 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
10944 ".headers on|off Turn display of headers on or off",
10945 ".help ?-all? ?PATTERN? Show help text for PATTERN",
10946 ".import FILE TABLE Import data from FILE into TABLE",
10947 #ifndef SQLITE_OMIT_TEST_CONTROL
10948 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
10950 ".indexes ?TABLE? Show names of indexes",
10951 " If TABLE is specified, only show indexes for",
10952 " tables matching TABLE using the LIKE operator.",
10953 #ifdef SQLITE_ENABLE_IOTRACE
10954 ".iotrace FILE Enable I/O diagnostic logging to FILE",
10956 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
10957 ".lint OPTIONS Report potential schema issues.",
10959 " fkey-indexes Find missing foreign key indexes",
10960 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10961 ".load FILE ?ENTRY? Load an extension library",
10963 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
10964 ".mode MODE ?TABLE? Set output mode",
10965 " MODE is one of:",
10966 " ascii Columns/rows delimited by 0x1F and 0x1E",
10967 " csv Comma-separated values",
10968 " column Left-aligned columns. (See .width)",
10969 " html HTML <table> code",
10970 " insert SQL insert statements for TABLE",
10971 " line One value per line",
10972 " list Values delimited by \"|\"",
10973 " quote Escape answers as for SQL",
10974 " tabs Tab-separated values",
10975 " tcl TCL list elements",
10976 ".nullvalue STRING Use STRING in place of NULL values",
10977 ".once (-e|-x|FILE) Output for the next SQL command only to FILE",
10978 " If FILE begins with '|' then open as a pipe",
10980 " -e Invoke system text editor",
10981 " -x Open in a spreadsheet",
10982 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
10984 " --append Use appendvfs to append database to the end of FILE",
10985 #ifdef SQLITE_ENABLE_DESERIALIZE
10986 " --deserialize Load into memory useing sqlite3_deserialize()",
10988 " --new Initialize FILE to an empty database",
10989 " --readonly Open FILE readonly",
10990 " --zip FILE is a ZIP archive",
10991 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
10992 " If FILE begins with '|' then open it as a pipe.",
10993 ".print STRING... Print literal STRING",
10994 ".prompt MAIN CONTINUE Replace the standard prompts",
10995 ".quit Exit this program",
10996 ".read FILE Read input from FILE",
10997 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
10998 ".save FILE Write in-memory database into FILE",
10999 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
11000 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
11002 " --indent Try to pretty-print the schema",
11003 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
11005 " --init Create a new SELFTEST table",
11006 " -v Verbose output",
11007 ".separator COL ?ROW? Change the column and row separators",
11008 #if defined(SQLITE_ENABLE_SESSION)
11009 ".session ?NAME? CMD ... Create or control sessions",
11011 " attach TABLE Attach TABLE",
11012 " changeset FILE Write a changeset into FILE",
11013 " close Close one session",
11014 " enable ?BOOLEAN? Set or query the enable bit",
11015 " filter GLOB... Reject tables matching GLOBs",
11016 " indirect ?BOOLEAN? Mark or query the indirect status",
11017 " isempty Query whether the session is empty",
11018 " list List currently open session names",
11019 " open DB NAME Open a new session on DB",
11020 " patchset FILE Write a patchset into FILE",
11021 " If ?NAME? is omitted, the first defined session is used.",
11023 ".sha3sum ... Compute a SHA3 hash of database content",
11025 " --schema Also hash the sqlite_master table",
11026 " --sha3-224 Use the sha3-224 algorithm",
11027 " --sha3-256 Use the sha3-256 algorithm. This is the default.",
11028 " --sha3-384 Use the sha3-384 algorithm",
11029 " --sha3-512 Use the sha3-512 algorithm",
11030 " Any other argument is a LIKE pattern for tables to hash",
11031 #ifndef SQLITE_NOHAVE_SYSTEM
11032 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
11034 ".show Show the current values for various settings",
11035 ".stats ?on|off? Show stats or turn stats on or off",
11036 #ifndef SQLITE_NOHAVE_SYSTEM
11037 ".system CMD ARGS... Run CMD ARGS... in a system shell",
11039 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
11040 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
11041 ".timeout MS Try opening locked tables for MS milliseconds",
11042 ".timer on|off Turn SQL timer on or off",
11043 ".trace FILE|off Output each SQL statement as it is run",
11044 ".vfsinfo ?AUX? Information about the top-level VFS",
11045 ".vfslist List all available VFSes",
11046 ".vfsname ?AUX? Print the name of the VFS stack",
11047 ".width NUM1 NUM2 ... Set column widths for \"column\" mode",
11048 " Negative values right-justify",
11052 ** Output help text.
11054 ** zPattern describes the set of commands for which help text is provided.
11055 ** If zPattern is NULL, then show all commands, but only give a one-line
11056 ** description of each.
11058 ** Return the number of matches.
11060 static int showHelp(FILE *out, const char *zPattern){
11066 || zPattern[0]=='0'
11067 || strcmp(zPattern,"-a")==0
11068 || strcmp(zPattern,"-all")==0
11070 /* Show all commands, but only one line per command */
11071 if( zPattern==0 ) zPattern = "";
11072 for(i=0; i<ArraySize(azHelp); i++){
11073 if( azHelp[i][0]=='.' || zPattern[0] ){
11074 utf8_printf(out, "%s\n", azHelp[i]);
11079 /* Look for commands that for which zPattern is an exact prefix */
11080 zPat = sqlite3_mprintf(".%s*", zPattern);
11081 for(i=0; i<ArraySize(azHelp); i++){
11082 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
11083 utf8_printf(out, "%s\n", azHelp[i]);
11088 sqlite3_free(zPat);
11091 /* when zPattern is a prefix of exactly one command, then include the
11092 ** details of that command, which should begin at offset j */
11093 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
11094 utf8_printf(out, "%s\n", azHelp[j]);
11100 /* Look for commands that contain zPattern anywhere. Show the complete
11101 ** text of all commands that match. */
11102 zPat = sqlite3_mprintf("%%%s%%", zPattern);
11103 for(i=0; i<ArraySize(azHelp); i++){
11104 if( azHelp[i][0]=='.' ) j = i;
11105 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
11106 utf8_printf(out, "%s\n", azHelp[j]);
11107 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
11109 utf8_printf(out, "%s\n", azHelp[j]);
11115 sqlite3_free(zPat);
11120 /* Forward reference */
11121 static int process_input(ShellState *p, FILE *in);
11124 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
11125 ** and return a pointer to the buffer. The caller is responsible for freeing
11128 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
11131 ** For convenience, a nul-terminator byte is always appended to the data read
11132 ** from the file before the buffer is returned. This byte is not included in
11133 ** the final value of (*pnByte), if applicable.
11135 ** NULL is returned if any error is encountered. The final value of *pnByte
11136 ** is undefined in this case.
11138 static char *readFile(const char *zName, int *pnByte){
11139 FILE *in = fopen(zName, "rb");
11143 if( in==0 ) return 0;
11144 fseek(in, 0, SEEK_END);
11147 pBuf = sqlite3_malloc64( nIn+1 );
11148 if( pBuf==0 ){ fclose(in); return 0; }
11149 nRead = fread(pBuf, nIn, 1, in);
11152 sqlite3_free(pBuf);
11156 if( pnByte ) *pnByte = nIn;
11160 #if defined(SQLITE_ENABLE_SESSION)
11162 ** Close a single OpenSession object and release all of its associated
11165 static void session_close(OpenSession *pSession){
11167 sqlite3session_delete(pSession->p);
11168 sqlite3_free(pSession->zName);
11169 for(i=0; i<pSession->nFilter; i++){
11170 sqlite3_free(pSession->azFilter[i]);
11172 sqlite3_free(pSession->azFilter);
11173 memset(pSession, 0, sizeof(OpenSession));
11178 ** Close all OpenSession objects and release all associated resources.
11180 #if defined(SQLITE_ENABLE_SESSION)
11181 static void session_close_all(ShellState *p){
11183 for(i=0; i<p->nSession; i++){
11184 session_close(&p->aSession[i]);
11189 # define session_close_all(X)
11193 ** Implementation of the xFilter function for an open session. Omit
11194 ** any tables named by ".session filter" but let all other table through.
11196 #if defined(SQLITE_ENABLE_SESSION)
11197 static int session_filter(void *pCtx, const char *zTab){
11198 OpenSession *pSession = (OpenSession*)pCtx;
11200 for(i=0; i<pSession->nFilter; i++){
11201 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11208 ** Try to deduce the type of file for zName based on its content. Return
11209 ** one of the SHELL_OPEN_* constants.
11211 ** If the file does not exist or is empty but its name looks like a ZIP
11212 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11213 ** Otherwise, assume an ordinary database regardless of the filename if
11214 ** the type cannot be determined from content.
11216 int deduceDatabaseType(const char *zName, int dfltZip){
11217 FILE *f = fopen(zName, "rb");
11219 int rc = SHELL_OPEN_UNSPEC;
11222 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11223 return SHELL_OPEN_ZIPFILE;
11225 return SHELL_OPEN_NORMAL;
11228 n = fread(zBuf, 16, 1, f);
11229 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
11231 return SHELL_OPEN_NORMAL;
11233 fseek(f, -25, SEEK_END);
11234 n = fread(zBuf, 25, 1, f);
11235 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11236 rc = SHELL_OPEN_APPENDVFS;
11238 fseek(f, -22, SEEK_END);
11239 n = fread(zBuf, 22, 1, f);
11240 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11241 && zBuf[3]==0x06 ){
11242 rc = SHELL_OPEN_ZIPFILE;
11243 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11244 rc = SHELL_OPEN_ZIPFILE;
11251 /* Flags for open_db().
11253 ** The default behavior of open_db() is to exit(1) if the database fails to
11254 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11255 ** but still returns without calling exit.
11257 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11258 ** ZIP archive if the file does not exist or is empty and its name matches
11259 ** the *.zip pattern.
11261 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11262 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11265 ** Make sure the database is open. If it is not, then open it. If
11266 ** the database fails to open, print an error message and exit.
11268 static void open_db(ShellState *p, int openFlags){
11270 if( p->openMode==SHELL_OPEN_UNSPEC ){
11271 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11272 p->openMode = SHELL_OPEN_NORMAL;
11274 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11275 (openFlags & OPEN_DB_ZIPFILE)!=0);
11278 switch( p->openMode ){
11279 case SHELL_OPEN_APPENDVFS: {
11280 sqlite3_open_v2(p->zDbFilename, &p->db,
11281 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11284 case SHELL_OPEN_DESERIALIZE: {
11285 sqlite3_open(0, &p->db);
11288 case SHELL_OPEN_ZIPFILE: {
11289 sqlite3_open(":memory:", &p->db);
11292 case SHELL_OPEN_READONLY: {
11293 sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11296 case SHELL_OPEN_UNSPEC:
11297 case SHELL_OPEN_NORMAL: {
11298 sqlite3_open(p->zDbFilename, &p->db);
11303 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11304 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11305 p->zDbFilename, sqlite3_errmsg(p->db));
11306 if( openFlags & OPEN_DB_KEEPALIVE ) return;
11309 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11310 sqlite3_enable_load_extension(p->db, 1);
11312 sqlite3_fileio_init(p->db, 0, 0);
11313 sqlite3_shathree_init(p->db, 0, 0);
11314 sqlite3_completion_init(p->db, 0, 0);
11315 #ifdef SQLITE_HAVE_ZLIB
11316 sqlite3_zipfile_init(p->db, 0, 0);
11317 sqlite3_sqlar_init(p->db, 0, 0);
11319 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11320 shellAddSchemaName, 0, 0);
11321 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11322 shellModuleSchema, 0, 0);
11323 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11324 shellPutsFunc, 0, 0);
11325 #ifndef SQLITE_NOHAVE_SYSTEM
11326 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11328 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11331 if( p->openMode==SHELL_OPEN_ZIPFILE ){
11332 char *zSql = sqlite3_mprintf(
11333 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11334 sqlite3_exec(p->db, zSql, 0, 0, 0);
11335 sqlite3_free(zSql);
11337 #ifdef SQLITE_ENABLE_DESERIALIZE
11338 else if( p->openMode==SHELL_OPEN_DESERIALIZE ){
11340 unsigned char *aData = (unsigned char*)readFile(p->zDbFilename, &nData);
11341 int rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
11342 SQLITE_DESERIALIZE_RESIZEABLE |
11343 SQLITE_DESERIALIZE_FREEONCLOSE);
11345 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
11353 ** Attempt to close the databaes connection. Report errors.
11355 void close_db(sqlite3 *db){
11356 int rc = sqlite3_close(db);
11358 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11359 rc, sqlite3_errmsg(db));
11363 #if HAVE_READLINE || HAVE_EDITLINE
11365 ** Readline completion callbacks
11367 static char *readline_completion_generator(const char *text, int state){
11368 static sqlite3_stmt *pStmt = 0;
11372 sqlite3_finalize(pStmt);
11373 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11374 " FROM completion(%Q) ORDER BY 1", text);
11375 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11376 sqlite3_free(zSql);
11378 if( sqlite3_step(pStmt)==SQLITE_ROW ){
11379 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11381 sqlite3_finalize(pStmt);
11387 static char **readline_completion(const char *zText, int iStart, int iEnd){
11388 rl_attempted_completion_over = 1;
11389 return rl_completion_matches(zText, readline_completion_generator);
11392 #elif HAVE_LINENOISE
11394 ** Linenoise completion callback
11396 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11397 int nLine = strlen30(zLine);
11399 sqlite3_stmt *pStmt = 0;
11403 if( nLine>sizeof(zBuf)-30 ) return;
11404 if( zLine[0]=='.' || zLine[0]=='#') return;
11405 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11406 if( i==nLine-1 ) return;
11408 memcpy(zBuf, zLine, iStart);
11409 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11410 " FROM completion(%Q,%Q) ORDER BY 1",
11411 &zLine[iStart], zLine);
11412 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11413 sqlite3_free(zSql);
11414 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11415 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11416 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11417 int nCompletion = sqlite3_column_bytes(pStmt, 0);
11418 if( iStart+nCompletion < sizeof(zBuf)-1 ){
11419 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11420 linenoiseAddCompletion(lc, zBuf);
11423 sqlite3_finalize(pStmt);
11428 ** Do C-language style dequoting.
11434 ** \v -> vertical tab
11436 ** \r -> carriage return
11441 ** \NNN -> ascii character NNN in octal
11443 static void resolve_backslashes(char *z){
11446 while( *z && *z!='\\' ) z++;
11447 for(i=j=0; (c = z[i])!=0; i++, j++){
11448 if( c=='\\' && z[i+1]!=0 ){
11452 }else if( c=='b' ){
11454 }else if( c=='t' ){
11456 }else if( c=='n' ){
11458 }else if( c=='v' ){
11460 }else if( c=='f' ){
11462 }else if( c=='r' ){
11464 }else if( c=='"' ){
11466 }else if( c=='\'' ){
11468 }else if( c=='\\' ){
11470 }else if( c>='0' && c<='7' ){
11472 if( z[i+1]>='0' && z[i+1]<='7' ){
11474 c = (c<<3) + z[i] - '0';
11475 if( z[i+1]>='0' && z[i+1]<='7' ){
11477 c = (c<<3) + z[i] - '0';
11484 if( j<i ) z[j] = 0;
11488 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
11489 ** for TRUE and FALSE. Return the integer value if appropriate.
11491 static int booleanValue(const char *zArg){
11493 if( zArg[0]=='0' && zArg[1]=='x' ){
11494 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11496 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11498 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11499 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11502 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11505 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11511 ** Set or clear a shell flag according to a boolean value.
11513 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11514 if( booleanValue(zArg) ){
11515 ShellSetFlag(p, mFlag);
11517 ShellClearFlag(p, mFlag);
11522 ** Close an output file, assuming it is not stderr or stdout
11524 static void output_file_close(FILE *f){
11525 if( f && f!=stdout && f!=stderr ) fclose(f);
11529 ** Try to open an output file. The names "stdout" and "stderr" are
11530 ** recognized and do the right thing. NULL is returned if the output
11531 ** filename is "off".
11533 static FILE *output_file_open(const char *zFile, int bTextMode){
11535 if( strcmp(zFile,"stdout")==0 ){
11537 }else if( strcmp(zFile, "stderr")==0 ){
11539 }else if( strcmp(zFile, "off")==0 ){
11542 f = fopen(zFile, bTextMode ? "w" : "wb");
11544 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11550 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
11552 ** A routine for handling output from sqlite3_trace().
11554 static int sql_trace_callback(
11560 FILE *f = (FILE*)pArg;
11561 UNUSED_PARAMETER(mType);
11562 UNUSED_PARAMETER(pP);
11564 const char *z = (const char*)pX;
11565 int i = strlen30(z);
11566 while( i>0 && z[i-1]==';' ){ i--; }
11567 utf8_printf(f, "%.*s;\n", i, z);
11574 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
11575 ** a useful spot to set a debugger breakpoint.
11577 static void test_breakpoint(void){
11578 static int nCall = 0;
11583 ** An object used to read a CSV and other files for import.
11585 typedef struct ImportCtx ImportCtx;
11587 const char *zFile; /* Name of the input file */
11588 FILE *in; /* Read the CSV text from this input stream */
11589 char *z; /* Accumulated text for a field */
11590 int n; /* Number of bytes in z */
11591 int nAlloc; /* Space allocated for z[] */
11592 int nLine; /* Current line number */
11593 int bNotFirst; /* True if one or more bytes already read */
11594 int cTerm; /* Character that terminated the most recent field */
11595 int cColSep; /* The column separator character. (Usually ",") */
11596 int cRowSep; /* The row separator character. (Usually "\n") */
11599 /* Append a single byte to z[] */
11600 static void import_append_char(ImportCtx *p, int c){
11601 if( p->n+1>=p->nAlloc ){
11602 p->nAlloc += p->nAlloc + 100;
11603 p->z = sqlite3_realloc64(p->z, p->nAlloc);
11604 if( p->z==0 ) shell_out_of_memory();
11606 p->z[p->n++] = (char)c;
11609 /* Read a single field of CSV text. Compatible with rfc4180 and extended
11610 ** with the option of having a separator other than ",".
11612 ** + Input comes from p->in.
11613 ** + Store results in p->z of length p->n. Space to hold p->z comes
11614 ** from sqlite3_malloc64().
11615 ** + Use p->cSep as the column separator. The default is ",".
11616 ** + Use p->rSep as the row separator. The default is "\n".
11617 ** + Keep track of the line number in p->nLine.
11618 ** + Store the character that terminates the field in p->cTerm. Store
11619 ** EOF on end-of-file.
11620 ** + Report syntax errors on stderr
11622 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
11624 int cSep = p->cColSep;
11625 int rSep = p->cRowSep;
11628 if( c==EOF || seenInterrupt ){
11634 int startLine = p->nLine;
11639 if( c==rSep ) p->nLine++;
11646 if( (c==cSep && pc==cQuote)
11647 || (c==rSep && pc==cQuote)
11648 || (c==rSep && pc=='\r' && ppc==cQuote)
11649 || (c==EOF && pc==cQuote)
11651 do{ p->n--; }while( p->z[p->n]!=cQuote );
11655 if( pc==cQuote && c!='\r' ){
11656 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
11657 p->zFile, p->nLine, cQuote);
11660 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
11661 p->zFile, startLine, cQuote);
11665 import_append_char(p, c);
11670 /* If this is the first field being parsed and it begins with the
11671 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
11672 if( (c&0xff)==0xef && p->bNotFirst==0 ){
11673 import_append_char(p, c);
11675 if( (c&0xff)==0xbb ){
11676 import_append_char(p, c);
11678 if( (c&0xff)==0xbf ){
11681 return csv_read_one_field(p);
11685 while( c!=EOF && c!=cSep && c!=rSep ){
11686 import_append_char(p, c);
11691 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
11695 if( p->z ) p->z[p->n] = 0;
11700 /* Read a single field of ASCII delimited text.
11702 ** + Input comes from p->in.
11703 ** + Store results in p->z of length p->n. Space to hold p->z comes
11704 ** from sqlite3_malloc64().
11705 ** + Use p->cSep as the column separator. The default is "\x1F".
11706 ** + Use p->rSep as the row separator. The default is "\x1E".
11707 ** + Keep track of the row number in p->nLine.
11708 ** + Store the character that terminates the field in p->cTerm. Store
11709 ** EOF on end-of-file.
11710 ** + Report syntax errors on stderr
11712 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
11714 int cSep = p->cColSep;
11715 int rSep = p->cRowSep;
11718 if( c==EOF || seenInterrupt ){
11722 while( c!=EOF && c!=cSep && c!=rSep ){
11723 import_append_char(p, c);
11730 if( p->z ) p->z[p->n] = 0;
11735 ** Try to transfer data for table zTable. If an error is seen while
11736 ** moving forward, try to go backwards. The backwards movement won't
11737 ** work for WITHOUT ROWID tables.
11739 static void tryToCloneData(
11744 sqlite3_stmt *pQuery = 0;
11745 sqlite3_stmt *pInsert = 0;
11750 int nTable = strlen30(zTable);
11753 const int spinRate = 10000;
11755 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
11756 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11758 utf8_printf(stderr, "Error %d: %s on [%s]\n",
11759 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11761 goto end_data_xfer;
11763 n = sqlite3_column_count(pQuery);
11764 zInsert = sqlite3_malloc64(200 + nTable + n*3);
11765 if( zInsert==0 ) shell_out_of_memory();
11766 sqlite3_snprintf(200+nTable,zInsert,
11767 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
11768 i = strlen30(zInsert);
11769 for(j=1; j<n; j++){
11770 memcpy(zInsert+i, ",?", 2);
11773 memcpy(zInsert+i, ");", 3);
11774 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
11776 utf8_printf(stderr, "Error %d: %s on [%s]\n",
11777 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
11779 goto end_data_xfer;
11781 for(k=0; k<2; k++){
11782 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11783 for(i=0; i<n; i++){
11784 switch( sqlite3_column_type(pQuery, i) ){
11785 case SQLITE_NULL: {
11786 sqlite3_bind_null(pInsert, i+1);
11789 case SQLITE_INTEGER: {
11790 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
11793 case SQLITE_FLOAT: {
11794 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
11797 case SQLITE_TEXT: {
11798 sqlite3_bind_text(pInsert, i+1,
11799 (const char*)sqlite3_column_text(pQuery,i),
11800 -1, SQLITE_STATIC);
11803 case SQLITE_BLOB: {
11804 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
11805 sqlite3_column_bytes(pQuery,i),
11811 rc = sqlite3_step(pInsert);
11812 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
11813 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
11814 sqlite3_errmsg(newDb));
11816 sqlite3_reset(pInsert);
11818 if( (cnt%spinRate)==0 ){
11819 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
11823 if( rc==SQLITE_DONE ) break;
11824 sqlite3_finalize(pQuery);
11825 sqlite3_free(zQuery);
11826 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
11828 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11830 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
11833 } /* End for(k=0...) */
11836 sqlite3_finalize(pQuery);
11837 sqlite3_finalize(pInsert);
11838 sqlite3_free(zQuery);
11839 sqlite3_free(zInsert);
11844 ** Try to transfer all rows of the schema that match zWhere. For
11845 ** each row, invoke xForEach() on the object defined by that row.
11846 ** If an error is encountered while moving forward through the
11847 ** sqlite_master table, try again moving backwards.
11849 static void tryToCloneSchema(
11852 const char *zWhere,
11853 void (*xForEach)(ShellState*,sqlite3*,const char*)
11855 sqlite3_stmt *pQuery = 0;
11858 const unsigned char *zName;
11859 const unsigned char *zSql;
11862 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11863 " WHERE %s", zWhere);
11864 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11866 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11867 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11869 goto end_schema_xfer;
11871 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11872 zName = sqlite3_column_text(pQuery, 0);
11873 zSql = sqlite3_column_text(pQuery, 1);
11874 printf("%s... ", zName); fflush(stdout);
11875 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11877 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11878 sqlite3_free(zErrMsg);
11882 xForEach(p, newDb, (const char*)zName);
11886 if( rc!=SQLITE_DONE ){
11887 sqlite3_finalize(pQuery);
11888 sqlite3_free(zQuery);
11889 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
11890 " WHERE %s ORDER BY rowid DESC", zWhere);
11891 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
11893 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
11894 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
11896 goto end_schema_xfer;
11898 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
11899 zName = sqlite3_column_text(pQuery, 0);
11900 zSql = sqlite3_column_text(pQuery, 1);
11901 printf("%s... ", zName); fflush(stdout);
11902 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
11904 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
11905 sqlite3_free(zErrMsg);
11909 xForEach(p, newDb, (const char*)zName);
11915 sqlite3_finalize(pQuery);
11916 sqlite3_free(zQuery);
11920 ** Open a new database file named "zNewDb". Try to recover as much information
11921 ** as possible out of the main database (which might be corrupt) and write it
11924 static void tryToClone(ShellState *p, const char *zNewDb){
11926 sqlite3 *newDb = 0;
11927 if( access(zNewDb,0)==0 ){
11928 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
11931 rc = sqlite3_open(zNewDb, &newDb);
11933 utf8_printf(stderr, "Cannot create output database: %s\n",
11934 sqlite3_errmsg(newDb));
11936 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
11937 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
11938 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
11939 tryToCloneSchema(p, newDb, "type!='table'", 0);
11940 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
11941 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
11947 ** Change the output file back to stdout.
11949 ** If the p->doXdgOpen flag is set, that means the output was being
11950 ** redirected to a temporary file named by p->zTempFile. In that case,
11951 ** launch start/open/xdg-open on that temporary file.
11953 static void output_reset(ShellState *p){
11954 if( p->outfile[0]=='|' ){
11955 #ifndef SQLITE_OMIT_POPEN
11959 output_file_close(p->out);
11960 #ifndef SQLITE_NOHAVE_SYSTEM
11961 if( p->doXdgOpen ){
11962 const char *zXdgOpenCmd =
11963 #if defined(_WIN32)
11965 #elif defined(__APPLE__)
11971 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
11972 if( system(zCmd) ){
11973 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
11975 sqlite3_free(zCmd);
11979 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
11986 ** Run an SQL command and return the single integer result.
11988 static int db_int(ShellState *p, const char *zSql){
11989 sqlite3_stmt *pStmt;
11991 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11992 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
11993 res = sqlite3_column_int(pStmt,0);
11995 sqlite3_finalize(pStmt);
12000 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
12002 static unsigned int get2byteInt(unsigned char *a){
12003 return (a[0]<<8) + a[1];
12005 static unsigned int get4byteInt(unsigned char *a){
12006 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
12010 ** Implementation of the ".info" command.
12012 ** Return 1 on error, 2 to exit, and 0 otherwise.
12014 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
12015 static const struct { const char *zName; int ofst; } aField[] = {
12016 { "file change counter:", 24 },
12017 { "database page count:", 28 },
12018 { "freelist page count:", 36 },
12019 { "schema cookie:", 40 },
12020 { "schema format:", 44 },
12021 { "default cache size:", 48 },
12022 { "autovacuum top root:", 52 },
12023 { "incremental vacuum:", 64 },
12024 { "text encoding:", 56 },
12025 { "user version:", 60 },
12026 { "application id:", 68 },
12027 { "software version:", 96 },
12029 static const struct { const char *zName; const char *zSql; } aQuery[] = {
12030 { "number of tables:",
12031 "SELECT count(*) FROM %s WHERE type='table'" },
12032 { "number of indexes:",
12033 "SELECT count(*) FROM %s WHERE type='index'" },
12034 { "number of triggers:",
12035 "SELECT count(*) FROM %s WHERE type='trigger'" },
12036 { "number of views:",
12037 "SELECT count(*) FROM %s WHERE type='view'" },
12039 "SELECT total(length(sql)) FROM %s" },
12042 unsigned iDataVersion;
12044 char *zDb = nArg>=2 ? azArg[1] : "main";
12045 sqlite3_stmt *pStmt = 0;
12046 unsigned char aHdr[100];
12048 if( p->db==0 ) return 1;
12049 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12051 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12052 if( sqlite3_step(pStmt)==SQLITE_ROW
12053 && sqlite3_column_bytes(pStmt,0)>100
12055 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
12056 sqlite3_finalize(pStmt);
12058 raw_printf(stderr, "unable to read database header\n");
12059 sqlite3_finalize(pStmt);
12062 i = get2byteInt(aHdr+16);
12063 if( i==1 ) i = 65536;
12064 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
12065 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
12066 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
12067 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
12068 for(i=0; i<ArraySize(aField); i++){
12069 int ofst = aField[i].ofst;
12070 unsigned int val = get4byteInt(aHdr + ofst);
12071 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
12074 if( val==1 ) raw_printf(p->out, " (utf8)");
12075 if( val==2 ) raw_printf(p->out, " (utf16le)");
12076 if( val==3 ) raw_printf(p->out, " (utf16be)");
12079 raw_printf(p->out, "\n");
12082 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
12083 }else if( strcmp(zDb,"temp")==0 ){
12084 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
12086 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
12088 for(i=0; i<ArraySize(aQuery); i++){
12089 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
12090 int val = db_int(p, zSql);
12091 sqlite3_free(zSql);
12092 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
12094 sqlite3_free(zSchemaTab);
12095 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
12096 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
12101 ** Print the current sqlite3_errmsg() value to stderr and return 1.
12103 static int shellDatabaseError(sqlite3 *db){
12104 const char *zErr = sqlite3_errmsg(db);
12105 utf8_printf(stderr, "Error: %s\n", zErr);
12110 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
12111 ** if they match and FALSE (0) if they do not match.
12115 ** '*' Matches any sequence of zero or more characters.
12117 ** '?' Matches exactly one character.
12119 ** [...] Matches one character from the enclosed list of
12122 ** [^...] Matches one character not in the enclosed list.
12124 ** '#' Matches any sequence of one or more digits with an
12125 ** optional + or - sign in front
12127 ** ' ' Any span of whitespace matches any other span of
12130 ** Extra whitespace at the end of z[] is ignored.
12132 static int testcase_glob(const char *zGlob, const char *z){
12137 while( (c = (*(zGlob++)))!=0 ){
12139 if( !IsSpace(*z) ) return 0;
12140 while( IsSpace(*zGlob) ) zGlob++;
12141 while( IsSpace(*z) ) z++;
12142 }else if( c=='*' ){
12143 while( (c=(*(zGlob++))) == '*' || c=='?' ){
12144 if( c=='?' && (*(z++))==0 ) return 0;
12148 }else if( c=='[' ){
12149 while( *z && testcase_glob(zGlob-1,z)==0 ){
12154 while( (c2 = (*(z++)))!=0 ){
12157 if( c2==0 ) return 0;
12159 if( testcase_glob(zGlob,z) ) return 1;
12162 }else if( c=='?' ){
12163 if( (*(z++))==0 ) return 0;
12164 }else if( c=='[' ){
12169 if( c==0 ) return 0;
12176 if( c==']' ) seen = 1;
12179 while( c2 && c2!=']' ){
12180 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
12182 if( c>=prior_c && c<=c2 ) seen = 1;
12192 if( c2==0 || (seen ^ invert)==0 ) return 0;
12193 }else if( c=='#' ){
12194 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12195 if( !IsDigit(z[0]) ) return 0;
12197 while( IsDigit(z[0]) ){ z++; }
12199 if( c!=(*(z++)) ) return 0;
12202 while( IsSpace(*z) ){ z++; }
12208 ** Compare the string as a command-line option with either one or two
12209 ** initial "-" characters.
12211 static int optionMatch(const char *zStr, const char *zOpt){
12212 if( zStr[0]!='-' ) return 0;
12214 if( zStr[0]=='-' ) zStr++;
12215 return strcmp(zStr, zOpt)==0;
12221 int shellDeleteFile(const char *zFilename){
12224 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12228 rc = unlink(zFilename);
12234 ** Try to delete the temporary file (if there is one) and free the
12235 ** memory used to hold the name of the temp file.
12237 static void clearTempFile(ShellState *p){
12238 if( p->zTempFile==0 ) return;
12239 if( p->doXdgOpen ) return;
12240 if( shellDeleteFile(p->zTempFile) ) return;
12241 sqlite3_free(p->zTempFile);
12246 ** Create a new temp file name with the given suffix.
12248 static void newTempFile(ShellState *p, const char *zSuffix){
12250 sqlite3_free(p->zTempFile);
12253 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12255 if( p->zTempFile==0 ){
12257 sqlite3_randomness(sizeof(r), &r);
12258 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12260 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12262 if( p->zTempFile==0 ){
12263 raw_printf(stderr, "out of memory\n");
12270 ** The implementation of SQL scalar function fkey_collate_clause(), used
12271 ** by the ".lint fkey-indexes" command. This scalar function is always
12272 ** called with four arguments - the parent table name, the parent column name,
12273 ** the child table name and the child column name.
12275 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12277 ** If either of the named tables or columns do not exist, this function
12278 ** returns an empty string. An empty string is also returned if both tables
12279 ** and columns exist but have the same default collation sequence. Or,
12280 ** if both exist but the default collation sequences are different, this
12281 ** function returns the string " COLLATE <parent-collation>", where
12282 ** <parent-collation> is the default collation sequence of the parent column.
12284 static void shellFkeyCollateClause(
12285 sqlite3_context *pCtx,
12287 sqlite3_value **apVal
12289 sqlite3 *db = sqlite3_context_db_handle(pCtx);
12290 const char *zParent;
12291 const char *zParentCol;
12292 const char *zParentSeq;
12293 const char *zChild;
12294 const char *zChildCol;
12295 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
12299 zParent = (const char*)sqlite3_value_text(apVal[0]);
12300 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12301 zChild = (const char*)sqlite3_value_text(apVal[2]);
12302 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12304 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12305 rc = sqlite3_table_column_metadata(
12306 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12308 if( rc==SQLITE_OK ){
12309 rc = sqlite3_table_column_metadata(
12310 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12314 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12315 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12316 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12323 ** The implementation of dot-command ".lint fkey-indexes".
12325 static int lintFkeyIndexes(
12326 ShellState *pState, /* Current shell tool state */
12327 char **azArg, /* Array of arguments passed to dot command */
12328 int nArg /* Number of entries in azArg[] */
12330 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
12331 FILE *out = pState->out; /* Stream to write non-error output to */
12332 int bVerbose = 0; /* If -verbose is present */
12333 int bGroupByParent = 0; /* If -groupbyparent is present */
12334 int i; /* To iterate through azArg[] */
12335 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
12336 int rc; /* Return code */
12337 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
12340 ** This SELECT statement returns one row for each foreign key constraint
12341 ** in the schema of the main database. The column values are:
12343 ** 0. The text of an SQL statement similar to:
12345 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12347 ** This SELECT is similar to the one that the foreign keys implementation
12348 ** needs to run internally on child tables. If there is an index that can
12349 ** be used to optimize this query, then it can also be used by the FK
12350 ** implementation to optimize DELETE or UPDATE statements on the parent
12353 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12354 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12355 ** contains an index that can be used to optimize the query.
12357 ** 2. Human readable text that describes the child table and columns. e.g.
12359 ** "child_table(child_key1, child_key2)"
12361 ** 3. Human readable text that describes the parent table and columns. e.g.
12363 ** "parent_table(parent_key1, parent_key2)"
12365 ** 4. A full CREATE INDEX statement for an index that could be used to
12366 ** optimize DELETE or UPDATE statements on the parent table. e.g.
12368 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
12370 ** 5. The name of the parent table.
12372 ** These six values are used by the C logic below to generate the report.
12376 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12377 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12378 " || fkey_collate_clause("
12379 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12381 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12382 " || group_concat('*=?', ' AND ') || ')'"
12384 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
12386 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12388 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12389 " || ' ON ' || quote(s.name) || '('"
12390 " || group_concat(quote(f.[from]) ||"
12391 " fkey_collate_clause("
12392 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12396 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12397 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12398 "GROUP BY s.name, f.id "
12399 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12401 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12403 for(i=2; i<nArg; i++){
12404 int n = strlen30(azArg[i]);
12405 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12408 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12409 bGroupByParent = 1;
12413 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12416 return SQLITE_ERROR;
12420 /* Register the fkey_collate_clause() SQL function */
12421 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12422 0, shellFkeyCollateClause, 0, 0
12426 if( rc==SQLITE_OK ){
12427 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12429 if( rc==SQLITE_OK ){
12430 sqlite3_bind_int(pSql, 1, bGroupByParent);
12433 if( rc==SQLITE_OK ){
12436 while( SQLITE_ROW==sqlite3_step(pSql) ){
12438 sqlite3_stmt *pExplain = 0;
12439 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12440 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12441 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12442 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12443 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12444 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12446 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12447 if( rc!=SQLITE_OK ) break;
12448 if( SQLITE_ROW==sqlite3_step(pExplain) ){
12449 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12451 0==sqlite3_strglob(zGlob, zPlan)
12452 || 0==sqlite3_strglob(zGlobIPK, zPlan)
12455 rc = sqlite3_finalize(pExplain);
12456 if( rc!=SQLITE_OK ) break;
12459 raw_printf(stderr, "Error: internal error");
12463 && (bVerbose || res==0)
12464 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12466 raw_printf(out, "-- Parent table %s\n", zParent);
12467 sqlite3_free(zPrev);
12468 zPrev = sqlite3_mprintf("%s", zParent);
12472 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12473 }else if( bVerbose ){
12474 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12475 zIndent, zFrom, zTarget
12480 sqlite3_free(zPrev);
12482 if( rc!=SQLITE_OK ){
12483 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12486 rc2 = sqlite3_finalize(pSql);
12487 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12489 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12492 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12499 ** Implementation of ".lint" dot command.
12501 static int lintDotCommand(
12502 ShellState *pState, /* Current shell tool state */
12503 char **azArg, /* Array of arguments passed to dot command */
12504 int nArg /* Number of entries in azArg[] */
12507 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12508 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12509 return lintFkeyIndexes(pState, azArg, nArg);
12512 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12513 raw_printf(stderr, "Where sub-commands are:\n");
12514 raw_printf(stderr, " fkey-indexes\n");
12515 return SQLITE_ERROR;
12518 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12519 /*********************************************************************************
12520 ** The ".archive" or ".ar" command.
12522 static void shellPrepare(
12526 sqlite3_stmt **ppStmt
12529 if( *pRc==SQLITE_OK ){
12530 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12531 if( rc!=SQLITE_OK ){
12532 raw_printf(stderr, "sql error: %s (%d)\n",
12533 sqlite3_errmsg(db), sqlite3_errcode(db)
12540 static void shellPreparePrintf(
12543 sqlite3_stmt **ppStmt,
12548 if( *pRc==SQLITE_OK ){
12551 va_start(ap, zFmt);
12552 z = sqlite3_vmprintf(zFmt, ap);
12555 *pRc = SQLITE_NOMEM;
12557 shellPrepare(db, pRc, z, ppStmt);
12563 static void shellFinalize(
12565 sqlite3_stmt *pStmt
12568 sqlite3 *db = sqlite3_db_handle(pStmt);
12569 int rc = sqlite3_finalize(pStmt);
12570 if( *pRc==SQLITE_OK ){
12571 if( rc!=SQLITE_OK ){
12572 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12579 static void shellReset(
12581 sqlite3_stmt *pStmt
12583 int rc = sqlite3_reset(pStmt);
12584 if( *pRc==SQLITE_OK ){
12585 if( rc!=SQLITE_OK ){
12586 sqlite3 *db = sqlite3_db_handle(pStmt);
12587 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12593 ** Structure representing a single ".ar" command.
12595 typedef struct ArCommand ArCommand;
12597 u8 eCmd; /* An AR_CMD_* value */
12598 u8 bVerbose; /* True if --verbose */
12599 u8 bZip; /* True if the archive is a ZIP */
12600 u8 bDryRun; /* True if --dry-run */
12601 u8 bAppend; /* True if --append */
12602 u8 fromCmdLine; /* Run from -A instead of .archive */
12603 int nArg; /* Number of command arguments */
12604 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
12605 const char *zFile; /* --file argument, or NULL */
12606 const char *zDir; /* --directory argument, or NULL */
12607 char **azArg; /* Array of command arguments */
12608 ShellState *p; /* Shell state */
12609 sqlite3 *db; /* Database containing the archive */
12613 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
12615 static int arUsage(FILE *f){
12616 showHelp(f,"archive");
12617 return SQLITE_ERROR;
12621 ** Print an error message for the .ar command to stderr and return
12624 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12627 va_start(ap, zFmt);
12628 z = sqlite3_vmprintf(zFmt, ap);
12630 utf8_printf(stderr, "Error: %s\n", z);
12631 if( pAr->fromCmdLine ){
12632 utf8_printf(stderr, "Use \"-A\" for more help\n");
12634 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12637 return SQLITE_ERROR;
12641 ** Values for ArCommand.eCmd.
12643 #define AR_CMD_CREATE 1
12644 #define AR_CMD_EXTRACT 2
12645 #define AR_CMD_LIST 3
12646 #define AR_CMD_UPDATE 4
12647 #define AR_CMD_HELP 5
12650 ** Other (non-command) switches.
12652 #define AR_SWITCH_VERBOSE 6
12653 #define AR_SWITCH_FILE 7
12654 #define AR_SWITCH_DIRECTORY 8
12655 #define AR_SWITCH_APPEND 9
12656 #define AR_SWITCH_DRYRUN 10
12658 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
12660 case AR_CMD_CREATE:
12661 case AR_CMD_EXTRACT:
12663 case AR_CMD_UPDATE:
12666 return arErrorMsg(pAr, "multiple command options");
12668 pAr->eCmd = eSwitch;
12671 case AR_SWITCH_DRYRUN:
12674 case AR_SWITCH_VERBOSE:
12677 case AR_SWITCH_APPEND:
12679 /* Fall thru into --file */
12680 case AR_SWITCH_FILE:
12683 case AR_SWITCH_DIRECTORY:
12692 ** Parse the command line for an ".ar" command. The results are written into
12693 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
12694 ** successfully, otherwise an error message is written to stderr and
12695 ** SQLITE_ERROR returned.
12697 static int arParseCommand(
12698 char **azArg, /* Array of arguments passed to dot command */
12699 int nArg, /* Number of entries in azArg[] */
12700 ArCommand *pAr /* Populate this object */
12708 { "create", 'c', AR_CMD_CREATE, 0 },
12709 { "extract", 'x', AR_CMD_EXTRACT, 0 },
12710 { "list", 't', AR_CMD_LIST, 0 },
12711 { "update", 'u', AR_CMD_UPDATE, 0 },
12712 { "help", 'h', AR_CMD_HELP, 0 },
12713 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
12714 { "file", 'f', AR_SWITCH_FILE, 1 },
12715 { "append", 'a', AR_SWITCH_APPEND, 1 },
12716 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
12717 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
12719 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
12720 struct ArSwitch *pEnd = &aSwitch[nSwitch];
12723 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
12724 return arUsage(stderr);
12726 char *z = azArg[1];
12728 /* Traditional style [tar] invocation */
12731 for(i=0; z[i]; i++){
12732 const char *zArg = 0;
12733 struct ArSwitch *pOpt;
12734 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12735 if( z[i]==pOpt->cShort ) break;
12738 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12742 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12744 zArg = azArg[iArg++];
12746 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12748 pAr->nArg = nArg-iArg;
12750 pAr->azArg = &azArg[iArg];
12753 /* Non-traditional invocation */
12755 for(iArg=1; iArg<nArg; iArg++){
12759 /* All remaining command line words are command arguments. */
12760 pAr->azArg = &azArg[iArg];
12761 pAr->nArg = nArg-iArg;
12768 /* One or more short options */
12769 for(i=1; i<n; i++){
12770 const char *zArg = 0;
12771 struct ArSwitch *pOpt;
12772 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12773 if( z[i]==pOpt->cShort ) break;
12776 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
12783 if( iArg>=(nArg-1) ){
12784 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
12786 zArg = azArg[++iArg];
12789 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
12791 }else if( z[2]=='\0' ){
12792 /* A -- option, indicating that all remaining command line words
12793 ** are command arguments. */
12794 pAr->azArg = &azArg[iArg+1];
12795 pAr->nArg = nArg-iArg-1;
12798 /* A long option */
12799 const char *zArg = 0; /* Argument for option, if any */
12800 struct ArSwitch *pMatch = 0; /* Matching option */
12801 struct ArSwitch *pOpt; /* Iterator */
12802 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
12803 const char *zLong = pOpt->zLong;
12804 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
12806 return arErrorMsg(pAr, "ambiguous option: %s",z);
12814 return arErrorMsg(pAr, "unrecognized option: %s", z);
12816 if( pMatch->bArg ){
12817 if( iArg>=(nArg-1) ){
12818 return arErrorMsg(pAr, "option requires an argument: %s", z);
12820 zArg = azArg[++iArg];
12822 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
12832 ** This function assumes that all arguments within the ArCommand.azArg[]
12833 ** array refer to archive members, as for the --extract or --list commands.
12834 ** It checks that each of them are present. If any specified file is not
12835 ** present in the archive, an error is printed to stderr and an error
12836 ** code returned. Otherwise, if all specified arguments are present in
12837 ** the archive, SQLITE_OK is returned.
12839 ** This function strips any trailing '/' characters from each argument.
12840 ** This is consistent with the way the [tar] command seems to work on
12843 static int arCheckEntries(ArCommand *pAr){
12844 int rc = SQLITE_OK;
12847 sqlite3_stmt *pTest = 0;
12849 shellPreparePrintf(pAr->db, &rc, &pTest,
12850 "SELECT name FROM %s WHERE name=$name",
12853 j = sqlite3_bind_parameter_index(pTest, "$name");
12854 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12855 char *z = pAr->azArg[i];
12856 int n = strlen30(z);
12858 while( n>0 && z[n-1]=='/' ) n--;
12860 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
12861 if( SQLITE_ROW==sqlite3_step(pTest) ){
12864 shellReset(&rc, pTest);
12865 if( rc==SQLITE_OK && bOk==0 ){
12866 utf8_printf(stderr, "not found in archive: %s\n", z);
12870 shellFinalize(&rc, pTest);
12876 ** Format a WHERE clause that can be used against the "sqlar" table to
12877 ** identify all archive members that match the command arguments held
12878 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
12879 ** The caller is responsible for eventually calling sqlite3_free() on
12880 ** any non-NULL (*pzWhere) value.
12882 static void arWhereClause(
12885 char **pzWhere /* OUT: New WHERE clause */
12888 if( *pRc==SQLITE_OK ){
12889 if( pAr->nArg==0 ){
12890 zWhere = sqlite3_mprintf("1");
12893 const char *zSep = "";
12894 for(i=0; i<pAr->nArg; i++){
12895 const char *z = pAr->azArg[i];
12896 zWhere = sqlite3_mprintf(
12897 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
12898 zWhere, zSep, z, strlen30(z)+1, z
12901 *pRc = SQLITE_NOMEM;
12912 ** Implementation of .ar "lisT" command.
12914 static int arListCommand(ArCommand *pAr){
12915 const char *zSql = "SELECT %s FROM %s WHERE %s";
12916 const char *azCols[] = {
12918 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
12922 sqlite3_stmt *pSql = 0;
12925 rc = arCheckEntries(pAr);
12926 arWhereClause(&rc, pAr, &zWhere);
12928 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
12929 pAr->zSrcTable, zWhere);
12930 if( pAr->bDryRun ){
12931 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12933 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12934 if( pAr->bVerbose ){
12935 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
12936 sqlite3_column_text(pSql, 0),
12937 sqlite3_column_int(pSql, 1),
12938 sqlite3_column_text(pSql, 2),
12939 sqlite3_column_text(pSql, 3)
12942 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12946 shellFinalize(&rc, pSql);
12947 sqlite3_free(zWhere);
12953 ** Implementation of .ar "eXtract" command.
12955 static int arExtractCommand(ArCommand *pAr){
12956 const char *zSql1 =
12959 " writefile(($dir || name), %s, mode, mtime) "
12960 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
12961 " AND name NOT GLOB '*..[/\\]*'";
12963 const char *azExtraArg[] = {
12964 "sqlar_uncompress(data, sz)",
12968 sqlite3_stmt *pSql = 0;
12969 int rc = SQLITE_OK;
12974 /* If arguments are specified, check that they actually exist within
12975 ** the archive before proceeding. And formulate a WHERE clause to
12977 rc = arCheckEntries(pAr);
12978 arWhereClause(&rc, pAr, &zWhere);
12980 if( rc==SQLITE_OK ){
12982 zDir = sqlite3_mprintf("%s/", pAr->zDir);
12984 zDir = sqlite3_mprintf("");
12986 if( zDir==0 ) rc = SQLITE_NOMEM;
12989 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
12990 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
12993 if( rc==SQLITE_OK ){
12994 j = sqlite3_bind_parameter_index(pSql, "$dir");
12995 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
12997 /* Run the SELECT statement twice. The first time, writefile() is called
12998 ** for all archive members that should be extracted. The second time,
12999 ** only for the directories. This is because the timestamps for
13000 ** extracted directories must be reset after they are populated (as
13001 ** populating them changes the timestamp). */
13002 for(i=0; i<2; i++){
13003 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
13004 sqlite3_bind_int(pSql, j, i);
13005 if( pAr->bDryRun ){
13006 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13008 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13009 if( i==0 && pAr->bVerbose ){
13010 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13014 shellReset(&rc, pSql);
13016 shellFinalize(&rc, pSql);
13019 sqlite3_free(zDir);
13020 sqlite3_free(zWhere);
13025 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
13027 static int arExecSql(ArCommand *pAr, const char *zSql){
13029 if( pAr->bDryRun ){
13030 utf8_printf(pAr->p->out, "%s\n", zSql);
13034 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
13036 utf8_printf(stdout, "ERROR: %s\n", zErr);
13037 sqlite3_free(zErr);
13045 ** Implementation of .ar "create" and "update" commands.
13047 ** Create the "sqlar" table in the database if it does not already exist.
13048 ** Then add each file in the azFile[] array to the archive. Directories
13049 ** are added recursively. If argument bVerbose is non-zero, a message is
13050 ** printed on stdout for each file archived.
13052 ** The create command is the same as update, except that it drops
13053 ** any existing "sqlar" table before beginning.
13055 static int arCreateOrUpdateCommand(
13056 ArCommand *pAr, /* Command arguments and options */
13057 int bUpdate /* true for a --create. false for --update */
13059 const char *zCreate =
13060 "CREATE TABLE IF NOT EXISTS sqlar(\n"
13061 " name TEXT PRIMARY KEY, -- name of the file\n"
13062 " mode INT, -- access permissions\n"
13063 " mtime INT, -- last modification time\n"
13064 " sz INT, -- original file size\n"
13065 " data BLOB -- compressed content\n"
13067 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
13068 const char *zInsertFmt[2] = {
13069 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
13074 " CASE substr(lsmode(mode),1,1)\n"
13075 " WHEN '-' THEN length(data)\n"
13076 " WHEN 'd' THEN 0\n"
13078 " sqlar_compress(data)\n"
13079 " FROM fsdir(%Q,%Q)\n"
13080 " WHERE lsmode(mode) NOT LIKE '?%%';",
13081 "REPLACE INTO %s(name,mode,mtime,data)\n"
13087 " FROM fsdir(%Q,%Q)\n"
13088 " WHERE lsmode(mode) NOT LIKE '?%%';"
13090 int i; /* For iterating through azFile[] */
13091 int rc; /* Return code */
13092 const char *zTab = 0; /* SQL table into which to insert */
13096 arExecSql(pAr, "PRAGMA page_size=512");
13097 rc = arExecSql(pAr, "SAVEPOINT ar;");
13098 if( rc!=SQLITE_OK ) return rc;
13101 /* Initialize the zipfile virtual table, if necessary */
13104 sqlite3_randomness(sizeof(r),&r);
13105 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
13107 zSql = sqlite3_mprintf(
13108 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
13111 rc = arExecSql(pAr, zSql);
13112 sqlite3_free(zSql);
13117 /* Initialize the table for an SQLAR */
13120 rc = arExecSql(pAr, zDrop);
13121 if( rc!=SQLITE_OK ) goto end_ar_transaction;
13123 rc = arExecSql(pAr, zCreate);
13125 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13126 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13127 pAr->bVerbose ? "shell_putsnl(name)" : "name",
13128 pAr->azArg[i], pAr->zDir);
13129 rc = arExecSql(pAr, zSql2);
13130 sqlite3_free(zSql2);
13132 end_ar_transaction:
13133 if( rc!=SQLITE_OK ){
13134 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
13136 rc = arExecSql(pAr, "RELEASE ar;");
13137 if( pAr->bZip && pAr->zFile ){
13138 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13139 arExecSql(pAr, zSql);
13140 sqlite3_free(zSql);
13147 ** Implementation of ".ar" dot command.
13149 static int arDotCommand(
13150 ShellState *pState, /* Current shell tool state */
13151 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
13152 char **azArg, /* Array of arguments passed to dot command */
13153 int nArg /* Number of entries in azArg[] */
13157 memset(&cmd, 0, sizeof(cmd));
13158 cmd.fromCmdLine = fromCmdLine;
13159 rc = arParseCommand(azArg, nArg, &cmd);
13160 if( rc==SQLITE_OK ){
13161 int eDbType = SHELL_OPEN_UNSPEC;
13163 cmd.db = pState->db;
13165 eDbType = deduceDatabaseType(cmd.zFile, 1);
13167 eDbType = pState->openMode;
13169 if( eDbType==SHELL_OPEN_ZIPFILE ){
13170 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
13171 if( cmd.zFile==0 ){
13172 cmd.zSrcTable = sqlite3_mprintf("zip");
13174 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13178 }else if( cmd.zFile ){
13180 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13181 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13182 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13184 flags = SQLITE_OPEN_READONLY;
13188 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13189 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13191 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
13192 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13193 if( rc!=SQLITE_OK ){
13194 utf8_printf(stderr, "cannot open file: %s (%s)\n",
13195 cmd.zFile, sqlite3_errmsg(cmd.db)
13197 goto end_ar_command;
13199 sqlite3_fileio_init(cmd.db, 0, 0);
13200 sqlite3_sqlar_init(cmd.db, 0, 0);
13201 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13202 shellPutsFunc, 0, 0);
13205 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13206 if( cmd.eCmd!=AR_CMD_CREATE
13207 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13209 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13211 goto end_ar_command;
13213 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13216 switch( cmd.eCmd ){
13217 case AR_CMD_CREATE:
13218 rc = arCreateOrUpdateCommand(&cmd, 0);
13221 case AR_CMD_EXTRACT:
13222 rc = arExtractCommand(&cmd);
13226 rc = arListCommand(&cmd);
13230 arUsage(pState->out);
13234 assert( cmd.eCmd==AR_CMD_UPDATE );
13235 rc = arCreateOrUpdateCommand(&cmd, 1);
13240 if( cmd.db!=pState->db ){
13243 sqlite3_free(cmd.zSrcTable);
13247 /* End of the ".archive" or ".ar" command logic
13248 **********************************************************************************/
13249 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13253 ** If an input line begins with "." then invoke this routine to
13254 ** process that line.
13256 ** Return 1 on error, 2 to exit, and 0 otherwise.
13258 static int do_meta_command(char *zLine, ShellState *p){
13265 #ifndef SQLITE_OMIT_VIRTUALTABLE
13266 if( p->expert.pExpert ){
13267 expertFinish(p, 1, 0);
13271 /* Parse the input line into tokens.
13273 while( zLine[h] && nArg<ArraySize(azArg) ){
13274 while( IsSpace(zLine[h]) ){ h++; }
13275 if( zLine[h]==0 ) break;
13276 if( zLine[h]=='\'' || zLine[h]=='"' ){
13277 int delim = zLine[h++];
13278 azArg[nArg++] = &zLine[h];
13279 while( zLine[h] && zLine[h]!=delim ){
13280 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13283 if( zLine[h]==delim ){
13286 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13288 azArg[nArg++] = &zLine[h];
13289 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13290 if( zLine[h] ) zLine[h++] = 0;
13291 resolve_backslashes(azArg[nArg-1]);
13295 /* Process the input line.
13297 if( nArg==0 ) return 0; /* no tokens, no error */
13298 n = strlen30(azArg[0]);
13302 #ifndef SQLITE_OMIT_AUTHORIZATION
13303 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13305 raw_printf(stderr, "Usage: .auth ON|OFF\n");
13307 goto meta_command_exit;
13310 if( booleanValue(azArg[1]) ){
13311 sqlite3_set_authorizer(p->db, shellAuth, p);
13313 sqlite3_set_authorizer(p->db, 0, 0);
13318 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13319 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13321 rc = arDotCommand(p, 0, azArg, nArg);
13325 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13326 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13328 const char *zDestFile = 0;
13329 const char *zDb = 0;
13331 sqlite3_backup *pBackup;
13333 const char *zVfs = 0;
13334 for(j=1; j<nArg; j++){
13335 const char *z = azArg[j];
13337 if( z[1]=='-' ) z++;
13338 if( strcmp(z, "-append")==0 ){
13342 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13345 }else if( zDestFile==0 ){
13346 zDestFile = azArg[j];
13347 }else if( zDb==0 ){
13349 zDestFile = azArg[j];
13351 raw_printf(stderr, "Usage: .backup ?DB? ?--append? FILENAME\n");
13355 if( zDestFile==0 ){
13356 raw_printf(stderr, "missing FILENAME argument on .backup\n");
13359 if( zDb==0 ) zDb = "main";
13360 rc = sqlite3_open_v2(zDestFile, &pDest,
13361 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13362 if( rc!=SQLITE_OK ){
13363 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13368 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13370 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13374 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13375 sqlite3_backup_finish(pBackup);
13376 if( rc==SQLITE_DONE ){
13379 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13385 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13387 bail_on_error = booleanValue(azArg[1]);
13389 raw_printf(stderr, "Usage: .bail on|off\n");
13394 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13396 if( booleanValue(azArg[1]) ){
13397 setBinaryMode(p->out, 1);
13399 setTextMode(p->out, 1);
13402 raw_printf(stderr, "Usage: .binary on|off\n");
13407 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13409 #if defined(_WIN32) || defined(WIN32)
13410 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13411 rc = !SetCurrentDirectoryW(z);
13414 rc = chdir(azArg[1]);
13417 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13421 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13426 /* The undocumented ".breakpoint" command causes a call to the no-op
13427 ** routine named test_breakpoint().
13429 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13433 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13435 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13437 raw_printf(stderr, "Usage: .changes on|off\n");
13442 /* Cancel output redirection, if it is currently set (by .testcase)
13443 ** Then read the content of the testcase-out.txt file and compare against
13444 ** azArg[1]. If there are differences, report an error and exit.
13446 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13450 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13452 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13453 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13455 }else if( testcase_glob(azArg[1],zRes)==0 ){
13456 utf8_printf(stderr,
13457 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
13458 p->zTestcase, azArg[1], zRes);
13461 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13464 sqlite3_free(zRes);
13467 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13469 tryToClone(p, azArg[1]);
13471 raw_printf(stderr, "Usage: .clone FILENAME\n");
13476 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13480 memcpy(&data, p, sizeof(data));
13481 data.showHeader = 0;
13482 data.cMode = data.mode = MODE_List;
13483 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13485 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13486 callback, &data, &zErrMsg);
13488 utf8_printf(stderr,"Error: %s\n", zErrMsg);
13489 sqlite3_free(zErrMsg);
13494 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13495 static const struct DbConfigChoices {
13499 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13500 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13501 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13502 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13503 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13504 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13505 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13506 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13507 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
13511 for(ii=0; ii<ArraySize(aDbConfig); ii++){
13512 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13514 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13516 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13517 utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13518 if( nArg>1 ) break;
13520 if( nArg>1 && ii==ArraySize(aDbConfig) ){
13521 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13522 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13526 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13527 rc = shell_dbinfo_command(p, nArg, azArg);
13530 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13531 const char *zLike = 0;
13533 int savedShowHeader = p->showHeader;
13534 int savedShellFlags = p->shellFlgs;
13535 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
13536 for(i=1; i<nArg; i++){
13537 if( azArg[i][0]=='-' ){
13538 const char *z = azArg[i]+1;
13539 if( z[0]=='-' ) z++;
13540 if( strcmp(z,"preserve-rowids")==0 ){
13541 #ifdef SQLITE_OMIT_VIRTUALTABLE
13542 raw_printf(stderr, "The --preserve-rowids option is not compatible"
13543 " with SQLITE_OMIT_VIRTUALTABLE\n");
13545 goto meta_command_exit;
13547 ShellSetFlag(p, SHFLG_PreserveRowid);
13550 if( strcmp(z,"newlines")==0 ){
13551 ShellSetFlag(p, SHFLG_Newlines);
13554 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
13556 goto meta_command_exit;
13559 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
13560 "?--newlines? ?LIKE-PATTERN?\n");
13562 goto meta_command_exit;
13568 /* When playing back a "dump", the content might appear in an order
13569 ** which causes immediate foreign key constraints to be violated.
13570 ** So disable foreign-key constraint enforcement to prevent problems. */
13571 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
13572 raw_printf(p->out, "BEGIN TRANSACTION;\n");
13573 p->writableSchema = 0;
13575 /* Set writable_schema=ON since doing so forces SQLite to initialize
13576 ** as much of the schema as it can even if the sqlite_master table is
13578 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
13581 run_schema_dump_query(p,
13582 "SELECT name, type, sql FROM sqlite_master "
13583 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
13585 run_schema_dump_query(p,
13586 "SELECT name, type, sql FROM sqlite_master "
13587 "WHERE name=='sqlite_sequence'"
13589 run_table_dump_query(p,
13590 "SELECT sql FROM sqlite_master "
13591 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
13595 zSql = sqlite3_mprintf(
13596 "SELECT name, type, sql FROM sqlite_master "
13597 "WHERE tbl_name LIKE %Q AND type=='table'"
13598 " AND sql NOT NULL", zLike);
13599 run_schema_dump_query(p,zSql);
13600 sqlite3_free(zSql);
13601 zSql = sqlite3_mprintf(
13602 "SELECT sql FROM sqlite_master "
13603 "WHERE sql NOT NULL"
13604 " AND type IN ('index','trigger','view')"
13605 " AND tbl_name LIKE %Q", zLike);
13606 run_table_dump_query(p, zSql, 0);
13607 sqlite3_free(zSql);
13609 if( p->writableSchema ){
13610 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
13611 p->writableSchema = 0;
13613 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13614 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
13615 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
13616 p->showHeader = savedShowHeader;
13617 p->shellFlgs = savedShellFlags;
13620 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
13622 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
13624 raw_printf(stderr, "Usage: .echo on|off\n");
13629 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13631 p->autoEQPtest = 0;
13632 if( strcmp(azArg[1],"full")==0 ){
13633 p->autoEQP = AUTOEQP_full;
13634 }else if( strcmp(azArg[1],"trigger")==0 ){
13635 p->autoEQP = AUTOEQP_trigger;
13636 }else if( strcmp(azArg[1],"test")==0 ){
13637 p->autoEQP = AUTOEQP_on;
13638 p->autoEQPtest = 1;
13640 p->autoEQP = (u8)booleanValue(azArg[1]);
13643 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
13648 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
13649 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
13653 /* The ".explain" command is automatic now. It is largely pointless. It
13654 ** retained purely for backwards compatibility */
13655 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
13658 if( strcmp(azArg[1],"auto")==0 ){
13661 val = booleanValue(azArg[1]);
13664 if( val==1 && p->mode!=MODE_Explain ){
13665 p->normalMode = p->mode;
13666 p->mode = MODE_Explain;
13667 p->autoExplain = 0;
13668 }else if( val==0 ){
13669 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13670 p->autoExplain = 0;
13671 }else if( val==99 ){
13672 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
13673 p->autoExplain = 1;
13677 #ifndef SQLITE_OMIT_VIRTUALTABLE
13678 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
13680 expertDotCommand(p, azArg, nArg);
13684 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
13688 memcpy(&data, p, sizeof(data));
13689 data.showHeader = 0;
13690 data.cMode = data.mode = MODE_Semi;
13691 if( nArg==2 && optionMatch(azArg[1], "indent") ){
13692 data.cMode = data.mode = MODE_Pretty;
13696 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
13698 goto meta_command_exit;
13701 rc = sqlite3_exec(p->db,
13703 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
13704 " FROM sqlite_master UNION ALL"
13705 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
13706 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
13708 callback, &data, &zErrMsg
13710 if( rc==SQLITE_OK ){
13711 sqlite3_stmt *pStmt;
13712 rc = sqlite3_prepare_v2(p->db,
13713 "SELECT rowid FROM sqlite_master"
13714 " WHERE name GLOB 'sqlite_stat[134]'",
13716 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
13717 sqlite3_finalize(pStmt);
13720 raw_printf(p->out, "/* No STAT tables available */\n");
13722 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13723 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
13724 callback, &data, &zErrMsg);
13725 data.cMode = data.mode = MODE_Insert;
13726 data.zDestTable = "sqlite_stat1";
13727 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
13728 data.zDestTable = "sqlite_stat3";
13729 shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
13730 data.zDestTable = "sqlite_stat4";
13731 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
13732 raw_printf(p->out, "ANALYZE sqlite_master;\n");
13736 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
13738 p->showHeader = booleanValue(azArg[1]);
13740 raw_printf(stderr, "Usage: .headers on|off\n");
13745 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
13747 n = showHelp(p->out, azArg[1]);
13749 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
13752 showHelp(p->out, 0);
13756 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
13757 char *zTable; /* Insert data into this table */
13758 char *zFile; /* Name of file to extra content from */
13759 sqlite3_stmt *pStmt = NULL; /* A statement */
13760 int nCol; /* Number of columns in the table */
13761 int nByte; /* Number of bytes in an SQL string */
13762 int i, j; /* Loop counters */
13763 int needCommit; /* True to COMMIT or ROLLBACK at end */
13764 int nSep; /* Number of bytes in p->colSeparator[] */
13765 char *zSql; /* An SQL statement */
13766 ImportCtx sCtx; /* Reader context */
13767 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
13768 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
13771 raw_printf(stderr, "Usage: .import FILE TABLE\n");
13772 goto meta_command_exit;
13777 memset(&sCtx, 0, sizeof(sCtx));
13779 nSep = strlen30(p->colSeparator);
13782 "Error: non-null column separator required for import\n");
13786 raw_printf(stderr, "Error: multi-character column separators not allowed"
13790 nSep = strlen30(p->rowSeparator);
13792 raw_printf(stderr, "Error: non-null row separator required for import\n");
13795 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
13796 /* When importing CSV (only), if the row separator is set to the
13797 ** default output row separator, change it to the default input
13798 ** row separator. This avoids having to maintain different input
13799 ** and output row separators. */
13800 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13801 nSep = strlen30(p->rowSeparator);
13804 raw_printf(stderr, "Error: multi-character row separators not allowed"
13808 sCtx.zFile = zFile;
13810 if( sCtx.zFile[0]=='|' ){
13811 #ifdef SQLITE_OMIT_POPEN
13812 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13815 sCtx.in = popen(sCtx.zFile+1, "r");
13816 sCtx.zFile = "<pipe>";
13820 sCtx.in = fopen(sCtx.zFile, "rb");
13823 if( p->mode==MODE_Ascii ){
13824 xRead = ascii_read_one_field;
13826 xRead = csv_read_one_field;
13829 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
13832 sCtx.cColSep = p->colSeparator[0];
13833 sCtx.cRowSep = p->rowSeparator[0];
13834 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
13837 shell_out_of_memory();
13839 nByte = strlen30(zSql);
13840 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13841 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
13842 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
13843 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
13845 while( xRead(&sCtx) ){
13846 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
13848 if( sCtx.cTerm!=sCtx.cColSep ) break;
13851 sqlite3_free(zCreate);
13852 sqlite3_free(sCtx.z);
13854 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
13857 zCreate = sqlite3_mprintf("%z\n)", zCreate);
13858 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
13859 sqlite3_free(zCreate);
13861 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
13862 sqlite3_errmsg(p->db));
13863 sqlite3_free(sCtx.z);
13867 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13869 sqlite3_free(zSql);
13871 if (pStmt) sqlite3_finalize(pStmt);
13872 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
13876 nCol = sqlite3_column_count(pStmt);
13877 sqlite3_finalize(pStmt);
13879 if( nCol==0 ) return 0; /* no columns, no error */
13880 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
13883 shell_out_of_memory();
13885 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
13886 j = strlen30(zSql);
13887 for(i=1; i<nCol; i++){
13893 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13894 sqlite3_free(zSql);
13896 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13897 if (pStmt) sqlite3_finalize(pStmt);
13901 needCommit = sqlite3_get_autocommit(p->db);
13902 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
13904 int startLine = sCtx.nLine;
13905 for(i=0; i<nCol; i++){
13906 char *z = xRead(&sCtx);
13908 ** Did we reach end-of-file before finding any columns?
13909 ** If so, stop instead of NULL filling the remaining columns.
13911 if( z==0 && i==0 ) break;
13913 ** Did we reach end-of-file OR end-of-line before finding any
13914 ** columns in ASCII mode? If so, stop instead of NULL filling
13915 ** the remaining columns.
13917 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
13918 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
13919 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
13920 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13921 "filling the rest with NULL\n",
13922 sCtx.zFile, startLine, nCol, i+1);
13924 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
13927 if( sCtx.cTerm==sCtx.cColSep ){
13931 }while( sCtx.cTerm==sCtx.cColSep );
13932 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
13933 "extras ignored\n",
13934 sCtx.zFile, startLine, nCol, i);
13937 sqlite3_step(pStmt);
13938 rc = sqlite3_reset(pStmt);
13939 if( rc!=SQLITE_OK ){
13940 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
13941 startLine, sqlite3_errmsg(p->db));
13944 }while( sCtx.cTerm!=EOF );
13947 sqlite3_free(sCtx.z);
13948 sqlite3_finalize(pStmt);
13949 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
13952 #ifndef SQLITE_UNTESTABLE
13953 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
13955 char *zCollist = 0;
13956 sqlite3_stmt *pStmt;
13959 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
13960 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
13961 " .imposter off\n");
13963 goto meta_command_exit;
13967 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
13968 goto meta_command_exit;
13970 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
13971 " WHERE name='%q' AND type='index'", azArg[1]);
13972 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13973 sqlite3_free(zSql);
13974 if( sqlite3_step(pStmt)==SQLITE_ROW ){
13975 tnum = sqlite3_column_int(pStmt, 0);
13977 sqlite3_finalize(pStmt);
13979 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
13981 goto meta_command_exit;
13983 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
13984 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13985 sqlite3_free(zSql);
13987 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13989 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
13992 if( sqlite3_column_int(pStmt,1)==-1 ){
13995 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
14000 zCollist = sqlite3_mprintf("\"%w\"", zCol);
14002 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
14005 sqlite3_finalize(pStmt);
14006 zSql = sqlite3_mprintf(
14007 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
14008 azArg[2], zCollist, zCollist);
14009 sqlite3_free(zCollist);
14010 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
14011 if( rc==SQLITE_OK ){
14012 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
14013 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
14015 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
14017 utf8_printf(stdout, "%s;\n", zSql);
14019 "WARNING: writing to an imposter table will corrupt the index!\n"
14023 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
14026 sqlite3_free(zSql);
14028 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
14030 #ifdef SQLITE_ENABLE_IOTRACE
14031 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
14032 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
14033 if( iotrace && iotrace!=stdout ) fclose(iotrace);
14036 sqlite3IoTrace = 0;
14037 }else if( strcmp(azArg[1], "-")==0 ){
14038 sqlite3IoTrace = iotracePrintf;
14041 iotrace = fopen(azArg[1], "w");
14043 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
14044 sqlite3IoTrace = 0;
14047 sqlite3IoTrace = iotracePrintf;
14053 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
14054 static const struct {
14055 const char *zLimitName; /* Name of a limit */
14056 int limitCode; /* Integer code for that limit */
14058 { "length", SQLITE_LIMIT_LENGTH },
14059 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
14060 { "column", SQLITE_LIMIT_COLUMN },
14061 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
14062 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
14063 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
14064 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
14065 { "attached", SQLITE_LIMIT_ATTACHED },
14066 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
14067 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
14068 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
14069 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
14074 for(i=0; i<ArraySize(aLimit); i++){
14075 printf("%20s %d\n", aLimit[i].zLimitName,
14076 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
14078 }else if( nArg>3 ){
14079 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
14081 goto meta_command_exit;
14084 n2 = strlen30(azArg[1]);
14085 for(i=0; i<ArraySize(aLimit); i++){
14086 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
14090 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
14092 goto meta_command_exit;
14097 utf8_printf(stderr, "unknown limit: \"%s\"\n"
14098 "enter \".limits\" with no arguments for a list.\n",
14101 goto meta_command_exit;
14104 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
14105 (int)integerValue(azArg[2]));
14107 printf("%20s %d\n", aLimit[iLimit].zLimitName,
14108 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
14112 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
14114 lintDotCommand(p, azArg, nArg);
14117 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14118 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
14119 const char *zFile, *zProc;
14122 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
14124 goto meta_command_exit;
14127 zProc = nArg>=3 ? azArg[2] : 0;
14129 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
14130 if( rc!=SQLITE_OK ){
14131 utf8_printf(stderr, "Error: %s\n", zErrMsg);
14132 sqlite3_free(zErrMsg);
14138 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
14140 raw_printf(stderr, "Usage: .log FILENAME\n");
14143 const char *zFile = azArg[1];
14144 output_file_close(p->pLog);
14145 p->pLog = output_file_open(zFile, 0);
14149 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
14150 const char *zMode = nArg>=2 ? azArg[1] : "";
14151 int n2 = strlen30(zMode);
14153 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
14154 p->mode = MODE_Line;
14155 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14156 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
14157 p->mode = MODE_Column;
14158 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14159 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
14160 p->mode = MODE_List;
14161 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
14162 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14163 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
14164 p->mode = MODE_Html;
14165 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
14166 p->mode = MODE_Tcl;
14167 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
14168 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14169 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
14170 p->mode = MODE_Csv;
14171 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14172 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14173 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
14174 p->mode = MODE_List;
14175 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
14176 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
14177 p->mode = MODE_Insert;
14178 set_table_name(p, nArg>=3 ? azArg[2] : "table");
14179 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
14180 p->mode = MODE_Quote;
14181 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
14182 p->mode = MODE_Ascii;
14183 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
14184 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14185 }else if( nArg==1 ){
14186 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14188 raw_printf(stderr, "Error: mode should be one of: "
14189 "ascii column csv html insert line list quote tabs tcl\n");
14192 p->cMode = p->mode;
14195 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14197 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14198 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14200 raw_printf(stderr, "Usage: .nullvalue STRING\n");
14205 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14206 char *zNewFilename; /* Name of the database file to open */
14207 int iName = 1; /* Index in azArg[] of the filename */
14208 int newFlag = 0; /* True to delete file before opening */
14209 /* Close the existing database */
14210 session_close_all(p);
14213 p->zDbFilename = 0;
14214 sqlite3_free(p->zFreeOnClose);
14215 p->zFreeOnClose = 0;
14216 p->openMode = SHELL_OPEN_UNSPEC;
14217 /* Check for command-line arguments */
14218 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14219 const char *z = azArg[iName];
14220 if( optionMatch(z,"new") ){
14222 #ifdef SQLITE_HAVE_ZLIB
14223 }else if( optionMatch(z, "zip") ){
14224 p->openMode = SHELL_OPEN_ZIPFILE;
14226 }else if( optionMatch(z, "append") ){
14227 p->openMode = SHELL_OPEN_APPENDVFS;
14228 }else if( optionMatch(z, "readonly") ){
14229 p->openMode = SHELL_OPEN_READONLY;
14230 #ifdef SQLITE_ENABLE_DESERIALIZE
14231 }else if( optionMatch(z, "deserialize") ){
14232 p->openMode = SHELL_OPEN_DESERIALIZE;
14234 }else if( z[0]=='-' ){
14235 utf8_printf(stderr, "unknown option: %s\n", z);
14237 goto meta_command_exit;
14240 /* If a filename is specified, try to open it first */
14241 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14242 if( zNewFilename ){
14243 if( newFlag ) shellDeleteFile(zNewFilename);
14244 p->zDbFilename = zNewFilename;
14245 open_db(p, OPEN_DB_KEEPALIVE);
14247 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14248 sqlite3_free(zNewFilename);
14250 p->zFreeOnClose = zNewFilename;
14254 /* As a fall-back open a TEMP database */
14255 p->zDbFilename = 0;
14261 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14262 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14264 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14266 if( azArg[0][0]=='e' ){
14267 /* Transform the ".excel" command into ".once -x" */
14270 zFile = azArg[1] = "-x";
14274 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14276 goto meta_command_exit;
14278 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14280 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14282 goto meta_command_exit;
14289 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14290 #ifndef SQLITE_NOHAVE_SYSTEM
14291 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14294 if( zFile[1]=='x' ){
14295 newTempFile(p, "csv");
14296 p->mode = MODE_Csv;
14297 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14298 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14300 newTempFile(p, "txt");
14303 zFile = p->zTempFile;
14305 #endif /* SQLITE_NOHAVE_SYSTEM */
14306 if( zFile[0]=='|' ){
14307 #ifdef SQLITE_OMIT_POPEN
14308 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14312 p->out = popen(zFile + 1, "w");
14314 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14318 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14322 p->out = output_file_open(zFile, bTxtMode);
14324 if( strcmp(zFile,"off")!=0 ){
14325 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14330 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14335 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14337 for(i=1; i<nArg; i++){
14338 if( i>1 ) raw_printf(p->out, " ");
14339 utf8_printf(p->out, "%s", azArg[i]);
14341 raw_printf(p->out, "\n");
14344 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14346 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14349 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14353 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14357 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14360 raw_printf(stderr, "Usage: .read FILE\n");
14362 goto meta_command_exit;
14364 alt = fopen(azArg[1], "rb");
14366 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
14369 rc = process_input(p, alt);
14374 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
14375 const char *zSrcFile;
14378 sqlite3_backup *pBackup;
14382 zSrcFile = azArg[1];
14384 }else if( nArg==3 ){
14385 zSrcFile = azArg[2];
14388 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
14390 goto meta_command_exit;
14392 rc = sqlite3_open(zSrcFile, &pSrc);
14393 if( rc!=SQLITE_OK ){
14394 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14399 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14401 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14405 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14406 || rc==SQLITE_BUSY ){
14407 if( rc==SQLITE_BUSY ){
14408 if( nTimeout++ >= 3 ) break;
14409 sqlite3_sleep(100);
14412 sqlite3_backup_finish(pBackup);
14413 if( rc==SQLITE_DONE ){
14415 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
14416 raw_printf(stderr, "Error: source database is busy\n");
14419 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14425 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14427 p->scanstatsOn = (u8)booleanValue(azArg[1]);
14428 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
14429 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
14432 raw_printf(stderr, "Usage: .scanstats on|off\n");
14437 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
14441 const char *zDiv = "(";
14442 const char *zName = 0;
14448 memcpy(&data, p, sizeof(data));
14449 data.showHeader = 0;
14450 data.cMode = data.mode = MODE_Semi;
14451 initText(&sSelect);
14452 for(ii=1; ii<nArg; ii++){
14453 if( optionMatch(azArg[ii],"indent") ){
14454 data.cMode = data.mode = MODE_Pretty;
14455 }else if( optionMatch(azArg[ii],"debug") ){
14457 }else if( zName==0 ){
14460 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
14462 goto meta_command_exit;
14466 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14467 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14468 char *new_argv[2], *new_colv[2];
14469 new_argv[0] = sqlite3_mprintf(
14470 "CREATE TABLE %s (\n"
14473 " tbl_name text,\n"
14474 " rootpage integer,\n"
14476 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
14478 new_colv[0] = "sql";
14480 callback(&data, 1, new_argv, new_colv);
14481 sqlite3_free(new_argv[0]);
14485 sqlite3_stmt *pStmt = 0;
14486 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
14489 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14490 sqlite3_finalize(pStmt);
14492 goto meta_command_exit;
14494 appendText(&sSelect, "SELECT sql FROM", 0);
14496 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14497 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
14499 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
14500 appendText(&sSelect, zDiv, 0);
14501 zDiv = " UNION ALL ";
14502 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
14503 if( sqlite3_stricmp(zDb, "main")!=0 ){
14504 appendText(&sSelect, zDb, '"');
14506 appendText(&sSelect, "NULL", 0);
14508 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
14509 appendText(&sSelect, zScNum, 0);
14510 appendText(&sSelect, " AS snum, ", 0);
14511 appendText(&sSelect, zDb, '\'');
14512 appendText(&sSelect, " AS sname FROM ", 0);
14513 appendText(&sSelect, zDb, '"');
14514 appendText(&sSelect, ".sqlite_master", 0);
14516 sqlite3_finalize(pStmt);
14517 #ifdef SQLITE_INTROSPECTION_PRAGMAS
14519 appendText(&sSelect,
14520 " UNION ALL SELECT shell_module_schema(name),"
14521 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
14524 appendText(&sSelect, ") WHERE ", 0);
14526 char *zQarg = sqlite3_mprintf("%Q", zName);
14527 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14528 strchr(zName, '[') != 0;
14529 if( strchr(zName, '.') ){
14530 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14532 appendText(&sSelect, "lower(tbl_name)", 0);
14534 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14535 appendText(&sSelect, zQarg, 0);
14537 appendText(&sSelect, " ESCAPE '\\' ", 0);
14539 appendText(&sSelect, " AND ", 0);
14540 sqlite3_free(zQarg);
14542 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14543 " ORDER BY snum, rowid", 0);
14545 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
14547 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
14549 freeText(&sSelect);
14552 utf8_printf(stderr,"Error: %s\n", zErrMsg);
14553 sqlite3_free(zErrMsg);
14555 }else if( rc != SQLITE_OK ){
14556 raw_printf(stderr,"Error: querying schema information\n");
14563 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
14564 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
14565 sqlite3SelectTrace = (int)integerValue(azArg[1]);
14569 #if defined(SQLITE_ENABLE_SESSION)
14570 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
14571 OpenSession *pSession = &p->aSession[0];
14572 char **azCmd = &azArg[1];
14574 int nCmd = nArg - 1;
14576 if( nArg<=1 ) goto session_syntax_error;
14579 for(iSes=0; iSes<p->nSession; iSes++){
14580 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
14582 if( iSes<p->nSession ){
14583 pSession = &p->aSession[iSes];
14587 pSession = &p->aSession[0];
14592 /* .session attach TABLE
14593 ** Invoke the sqlite3session_attach() interface to attach a particular
14594 ** table so that it is never filtered.
14596 if( strcmp(azCmd[0],"attach")==0 ){
14597 if( nCmd!=2 ) goto session_syntax_error;
14598 if( pSession->p==0 ){
14600 raw_printf(stderr, "ERROR: No sessions are open\n");
14602 rc = sqlite3session_attach(pSession->p, azCmd[1]);
14604 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
14610 /* .session changeset FILE
14611 ** .session patchset FILE
14612 ** Write a changeset or patchset into a file. The file is overwritten.
14614 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
14616 if( nCmd!=2 ) goto session_syntax_error;
14617 if( pSession->p==0 ) goto session_not_open;
14618 out = fopen(azCmd[1], "wb");
14620 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
14624 if( azCmd[0][0]=='c' ){
14625 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
14627 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
14630 printf("Error: error code %d\n", rc);
14634 && fwrite(pChng, szChng, 1, out)!=1 ){
14635 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
14638 sqlite3_free(pChng);
14644 ** Close the identified session
14646 if( strcmp(azCmd[0], "close")==0 ){
14647 if( nCmd!=1 ) goto session_syntax_error;
14649 session_close(pSession);
14650 p->aSession[iSes] = p->aSession[--p->nSession];
14654 /* .session enable ?BOOLEAN?
14655 ** Query or set the enable flag
14657 if( strcmp(azCmd[0], "enable")==0 ){
14659 if( nCmd>2 ) goto session_syntax_error;
14660 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14662 ii = sqlite3session_enable(pSession->p, ii);
14663 utf8_printf(p->out, "session %s enable flag = %d\n",
14664 pSession->zName, ii);
14668 /* .session filter GLOB ....
14669 ** Set a list of GLOB patterns of table names to be excluded.
14671 if( strcmp(azCmd[0], "filter")==0 ){
14673 if( nCmd<2 ) goto session_syntax_error;
14675 for(ii=0; ii<pSession->nFilter; ii++){
14676 sqlite3_free(pSession->azFilter[ii]);
14678 sqlite3_free(pSession->azFilter);
14679 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
14680 pSession->azFilter = sqlite3_malloc( nByte );
14681 if( pSession->azFilter==0 ){
14682 raw_printf(stderr, "Error: out or memory\n");
14685 for(ii=1; ii<nCmd; ii++){
14686 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
14688 pSession->nFilter = ii-1;
14692 /* .session indirect ?BOOLEAN?
14693 ** Query or set the indirect flag
14695 if( strcmp(azCmd[0], "indirect")==0 ){
14697 if( nCmd>2 ) goto session_syntax_error;
14698 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
14700 ii = sqlite3session_indirect(pSession->p, ii);
14701 utf8_printf(p->out, "session %s indirect flag = %d\n",
14702 pSession->zName, ii);
14706 /* .session isempty
14707 ** Determine if the session is empty
14709 if( strcmp(azCmd[0], "isempty")==0 ){
14711 if( nCmd!=1 ) goto session_syntax_error;
14713 ii = sqlite3session_isempty(pSession->p);
14714 utf8_printf(p->out, "session %s isempty flag = %d\n",
14715 pSession->zName, ii);
14720 ** List all currently open sessions
14722 if( strcmp(azCmd[0],"list")==0 ){
14723 for(i=0; i<p->nSession; i++){
14724 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
14728 /* .session open DB NAME
14729 ** Open a new session called NAME on the attached database DB.
14730 ** DB is normally "main".
14732 if( strcmp(azCmd[0],"open")==0 ){
14734 if( nCmd!=3 ) goto session_syntax_error;
14736 if( zName[0]==0 ) goto session_syntax_error;
14737 for(i=0; i<p->nSession; i++){
14738 if( strcmp(p->aSession[i].zName,zName)==0 ){
14739 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
14740 goto meta_command_exit;
14743 if( p->nSession>=ArraySize(p->aSession) ){
14744 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
14745 goto meta_command_exit;
14747 pSession = &p->aSession[p->nSession];
14748 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
14750 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
14752 goto meta_command_exit;
14754 pSession->nFilter = 0;
14755 sqlite3session_table_filter(pSession->p, session_filter, pSession);
14757 pSession->zName = sqlite3_mprintf("%s", zName);
14759 /* If no command name matches, show a syntax error */
14760 session_syntax_error:
14761 showHelp(p->out, "session");
14765 #ifdef SQLITE_DEBUG
14766 /* Undocumented commands for internal testing. Subject to change
14767 ** without notice. */
14768 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
14769 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
14771 for(i=1; i<nArg; i++){
14772 v = booleanValue(azArg[i]);
14773 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
14776 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
14777 int i; sqlite3_int64 v;
14778 for(i=1; i<nArg; i++){
14780 v = integerValue(azArg[i]);
14781 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
14782 utf8_printf(p->out, "%s", zBuf);
14788 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
14789 int bIsInit = 0; /* True to initialize the SELFTEST table */
14790 int bVerbose = 0; /* Verbose output */
14791 int bSelftestExists; /* True if SELFTEST already exists */
14792 int i, k; /* Loop counters */
14793 int nTest = 0; /* Number of tests runs */
14794 int nErr = 0; /* Number of errors seen */
14795 ShellText str; /* Answer for a query */
14796 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
14799 for(i=1; i<nArg; i++){
14800 const char *z = azArg[i];
14801 if( z[0]=='-' && z[1]=='-' ) z++;
14802 if( strcmp(z,"-init")==0 ){
14805 if( strcmp(z,"-v")==0 ){
14809 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14810 azArg[i], azArg[0]);
14811 raw_printf(stderr, "Should be one of: --init -v\n");
14813 goto meta_command_exit;
14816 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
14818 bSelftestExists = 0;
14820 bSelftestExists = 1;
14823 createSelftestTable(p);
14824 bSelftestExists = 1;
14827 appendText(&str, "x", 0);
14828 for(k=bSelftestExists; k>=0; k--){
14830 rc = sqlite3_prepare_v2(p->db,
14831 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
14834 rc = sqlite3_prepare_v2(p->db,
14835 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
14836 " (1,'run','PRAGMA integrity_check','ok')",
14840 raw_printf(stderr, "Error querying the selftest table\n");
14842 sqlite3_finalize(pStmt);
14843 goto meta_command_exit;
14845 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
14846 int tno = sqlite3_column_int(pStmt, 0);
14847 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
14848 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
14849 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
14853 char *zQuote = sqlite3_mprintf("%q", zSql);
14854 printf("%d: %s %s\n", tno, zOp, zSql);
14855 sqlite3_free(zQuote);
14857 if( strcmp(zOp,"memo")==0 ){
14858 utf8_printf(p->out, "%s\n", zSql);
14860 if( strcmp(zOp,"run")==0 ){
14864 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
14867 utf8_printf(p->out, "Result: %s\n", str.z);
14869 if( rc || zErrMsg ){
14872 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
14873 sqlite3_free(zErrMsg);
14874 }else if( strcmp(zAns,str.z)!=0 ){
14877 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
14878 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
14882 utf8_printf(stderr,
14883 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
14887 } /* End loop over rows of content from SELFTEST */
14888 sqlite3_finalize(pStmt);
14889 } /* End loop over k */
14891 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
14894 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
14895 if( nArg<2 || nArg>3 ){
14896 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
14900 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
14901 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
14904 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
14905 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
14909 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
14910 const char *zLike = 0; /* Which table to checksum. 0 means everything */
14911 int i; /* Loop counter */
14912 int bSchema = 0; /* Also hash the schema */
14913 int bSeparate = 0; /* Hash each table separately */
14914 int iSize = 224; /* Hash algorithm to use */
14915 int bDebug = 0; /* Only show the query that would have run */
14916 sqlite3_stmt *pStmt; /* For querying tables names */
14917 char *zSql; /* SQL to be run */
14918 char *zSep; /* Separator */
14919 ShellText sSql; /* Complete SQL for the query to run the hash */
14920 ShellText sQuery; /* Set of queries used to read all content */
14922 for(i=1; i<nArg; i++){
14923 const char *z = azArg[i];
14926 if( z[0]=='-' ) z++;
14927 if( strcmp(z,"schema")==0 ){
14930 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
14931 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
14933 iSize = atoi(&z[5]);
14935 if( strcmp(z,"debug")==0 ){
14939 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
14940 azArg[i], azArg[0]);
14941 raw_printf(stderr, "Should be one of: --schema"
14942 " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
14944 goto meta_command_exit;
14947 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
14949 goto meta_command_exit;
14953 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
14957 zSql = "SELECT lower(name) FROM sqlite_master"
14958 " WHERE type='table' AND coalesce(rootpage,0)>1"
14959 " UNION ALL SELECT 'sqlite_master'"
14960 " ORDER BY 1 collate nocase";
14962 zSql = "SELECT lower(name) FROM sqlite_master"
14963 " WHERE type='table' AND coalesce(rootpage,0)>1"
14964 " AND name NOT LIKE 'sqlite_%'"
14965 " ORDER BY 1 collate nocase";
14967 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14970 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
14972 while( SQLITE_ROW==sqlite3_step(pStmt) ){
14973 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
14974 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
14975 if( strncmp(zTab, "sqlite_",7)!=0 ){
14976 appendText(&sQuery,"SELECT * FROM ", 0);
14977 appendText(&sQuery,zTab,'"');
14978 appendText(&sQuery," NOT INDEXED;", 0);
14979 }else if( strcmp(zTab, "sqlite_master")==0 ){
14980 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
14981 " ORDER BY name;", 0);
14982 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
14983 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
14984 " ORDER BY name;", 0);
14985 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
14986 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
14987 " ORDER BY tbl,idx;", 0);
14988 }else if( strcmp(zTab, "sqlite_stat3")==0
14989 || strcmp(zTab, "sqlite_stat4")==0 ){
14990 appendText(&sQuery, "SELECT * FROM ", 0);
14991 appendText(&sQuery, zTab, 0);
14992 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
14994 appendText(&sSql, zSep, 0);
14995 appendText(&sSql, sQuery.z, '\'');
14997 appendText(&sSql, ",", 0);
14998 appendText(&sSql, zTab, '\'');
15001 sqlite3_finalize(pStmt);
15003 zSql = sqlite3_mprintf(
15005 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
15006 " FROM [sha3sum$query]",
15009 zSql = sqlite3_mprintf(
15011 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
15012 " FROM [sha3sum$query]",
15018 utf8_printf(p->out, "%s\n", zSql);
15020 shell_exec(p, zSql, 0);
15022 sqlite3_free(zSql);
15025 #ifndef SQLITE_NOHAVE_SYSTEM
15027 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
15032 raw_printf(stderr, "Usage: .system COMMAND\n");
15034 goto meta_command_exit;
15036 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
15037 for(i=2; i<nArg; i++){
15038 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
15042 sqlite3_free(zCmd);
15043 if( x ) raw_printf(stderr, "System command returns %d\n", x);
15045 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15047 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
15048 static const char *azBool[] = { "off", "on", "trigger", "full"};
15051 raw_printf(stderr, "Usage: .show\n");
15053 goto meta_command_exit;
15055 utf8_printf(p->out, "%12.12s: %s\n","echo",
15056 azBool[ShellHasFlag(p, SHFLG_Echo)]);
15057 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
15058 utf8_printf(p->out, "%12.12s: %s\n","explain",
15059 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
15060 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
15061 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
15062 utf8_printf(p->out, "%12.12s: ", "nullvalue");
15063 output_c_string(p->out, p->nullValue);
15064 raw_printf(p->out, "\n");
15065 utf8_printf(p->out,"%12.12s: %s\n","output",
15066 strlen30(p->outfile) ? p->outfile : "stdout");
15067 utf8_printf(p->out,"%12.12s: ", "colseparator");
15068 output_c_string(p->out, p->colSeparator);
15069 raw_printf(p->out, "\n");
15070 utf8_printf(p->out,"%12.12s: ", "rowseparator");
15071 output_c_string(p->out, p->rowSeparator);
15072 raw_printf(p->out, "\n");
15073 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
15074 utf8_printf(p->out, "%12.12s: ", "width");
15075 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
15076 raw_printf(p->out, "%d ", p->colWidth[i]);
15078 raw_printf(p->out, "\n");
15079 utf8_printf(p->out, "%12.12s: %s\n", "filename",
15080 p->zDbFilename ? p->zDbFilename : "");
15083 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
15085 p->statsOn = (u8)booleanValue(azArg[1]);
15086 }else if( nArg==1 ){
15087 display_stats(p->db, p, 0);
15089 raw_printf(stderr, "Usage: .stats ?on|off?\n");
15094 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
15095 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
15096 || strncmp(azArg[0], "indexes", n)==0) )
15098 sqlite3_stmt *pStmt;
15105 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
15107 sqlite3_finalize(pStmt);
15108 return shellDatabaseError(p->db);
15111 if( nArg>2 && c=='i' ){
15112 /* It is an historical accident that the .indexes command shows an error
15113 ** when called with the wrong number of arguments whereas the .tables
15114 ** command does not. */
15115 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
15117 sqlite3_finalize(pStmt);
15118 goto meta_command_exit;
15120 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
15121 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
15122 if( zDbName==0 ) continue;
15123 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
15124 if( sqlite3_stricmp(zDbName, "main")==0 ){
15125 appendText(&s, "SELECT name FROM ", 0);
15127 appendText(&s, "SELECT ", 0);
15128 appendText(&s, zDbName, '\'');
15129 appendText(&s, "||'.'||name FROM ", 0);
15131 appendText(&s, zDbName, '"');
15132 appendText(&s, ".sqlite_master ", 0);
15134 appendText(&s," WHERE type IN ('table','view')"
15135 " AND name NOT LIKE 'sqlite_%'"
15136 " AND name LIKE ?1", 0);
15138 appendText(&s," WHERE type='index'"
15139 " AND tbl_name LIKE ?1", 0);
15142 rc = sqlite3_finalize(pStmt);
15143 appendText(&s, " ORDER BY 1", 0);
15144 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
15146 if( rc ) return shellDatabaseError(p->db);
15148 /* Run the SQL statement prepared by the above block. Store the results
15149 ** as an array of nul-terminated strings in azResult[]. */
15153 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
15155 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
15157 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15158 if( nRow>=nAlloc ){
15160 int n2 = nAlloc*2 + 10;
15161 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
15162 if( azNew==0 ) shell_out_of_memory();
15166 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
15167 if( 0==azResult[nRow] ) shell_out_of_memory();
15170 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
15171 rc = shellDatabaseError(p->db);
15174 /* Pretty-print the contents of array azResult[] to the output */
15175 if( rc==0 && nRow>0 ){
15176 int len, maxlen = 0;
15178 int nPrintCol, nPrintRow;
15179 for(i=0; i<nRow; i++){
15180 len = strlen30(azResult[i]);
15181 if( len>maxlen ) maxlen = len;
15183 nPrintCol = 80/(maxlen+2);
15184 if( nPrintCol<1 ) nPrintCol = 1;
15185 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
15186 for(i=0; i<nPrintRow; i++){
15187 for(j=i; j<nRow; j+=nPrintRow){
15188 char *zSp = j<nPrintRow ? "" : " ";
15189 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15190 azResult[j] ? azResult[j]:"");
15192 raw_printf(p->out, "\n");
15196 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15197 sqlite3_free(azResult);
15200 /* Begin redirecting output to the file "testcase-out.txt" */
15201 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15203 p->out = output_file_open("testcase-out.txt", 0);
15205 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15208 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15210 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15214 #ifndef SQLITE_UNTESTABLE
15215 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15216 static const struct {
15217 const char *zCtrlName; /* Name of a test-control option */
15218 int ctrlCode; /* Integer code for that option */
15219 const char *zUsage; /* Usage notes */
15221 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
15222 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
15223 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
15224 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
15225 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
15226 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
15227 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
15228 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN" },
15229 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
15230 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
15231 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
15233 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
15235 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
15236 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" },
15237 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
15238 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
15239 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" },
15243 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
15246 const char *zCmd = 0;
15249 zCmd = nArg>=2 ? azArg[1] : "help";
15251 /* The argument can optionally begin with "-" or "--" */
15252 if( zCmd[0]=='-' && zCmd[1] ){
15254 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15257 /* --help lists all test-controls */
15258 if( strcmp(zCmd,"help")==0 ){
15259 utf8_printf(p->out, "Available test-controls:\n");
15260 for(i=0; i<ArraySize(aCtrl); i++){
15261 utf8_printf(p->out, " .testctrl %s %s\n",
15262 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15265 goto meta_command_exit;
15268 /* convert testctrl text option to value. allow any unique prefix
15269 ** of the option name, or a numerical value. */
15270 n2 = strlen30(zCmd);
15271 for(i=0; i<ArraySize(aCtrl); i++){
15272 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15274 testctrl = aCtrl[i].ctrlCode;
15277 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15278 "Use \".testctrl --help\" for help\n", zCmd);
15280 goto meta_command_exit;
15285 utf8_printf(stderr,"Error: unknown test-control: %s\n"
15286 "Use \".testctrl --help\" for help\n", zCmd);
15290 /* sqlite3_test_control(int, db, int) */
15291 case SQLITE_TESTCTRL_OPTIMIZATIONS:
15292 case SQLITE_TESTCTRL_RESERVE:
15294 int opt = (int)strtol(azArg[2], 0, 0);
15295 rc2 = sqlite3_test_control(testctrl, p->db, opt);
15300 /* sqlite3_test_control(int) */
15301 case SQLITE_TESTCTRL_PRNG_SAVE:
15302 case SQLITE_TESTCTRL_PRNG_RESTORE:
15303 case SQLITE_TESTCTRL_PRNG_RESET:
15304 case SQLITE_TESTCTRL_BYTEORDER:
15306 rc2 = sqlite3_test_control(testctrl);
15307 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15311 /* sqlite3_test_control(int, uint) */
15312 case SQLITE_TESTCTRL_PENDING_BYTE:
15314 unsigned int opt = (unsigned int)integerValue(azArg[2]);
15315 rc2 = sqlite3_test_control(testctrl, opt);
15320 /* sqlite3_test_control(int, int) */
15321 case SQLITE_TESTCTRL_ASSERT:
15322 case SQLITE_TESTCTRL_ALWAYS:
15323 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
15325 int opt = booleanValue(azArg[2]);
15326 rc2 = sqlite3_test_control(testctrl, opt);
15331 /* sqlite3_test_control(int, int) */
15332 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15333 case SQLITE_TESTCTRL_NEVER_CORRUPT:
15335 int opt = booleanValue(azArg[2]);
15336 rc2 = sqlite3_test_control(testctrl, opt);
15341 case SQLITE_TESTCTRL_IMPOSTER:
15343 rc2 = sqlite3_test_control(testctrl, p->db,
15345 integerValue(azArg[3]),
15346 integerValue(azArg[4]));
15352 case SQLITE_TESTCTRL_PARSER_COVERAGE:
15354 sqlite3_test_control(testctrl, p->out);
15360 if( isOk==0 && iCtrl>=0 ){
15361 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
15363 }else if( isOk==1 ){
15364 raw_printf(p->out, "%d\n", rc2);
15365 }else if( isOk==2 ){
15366 raw_printf(p->out, "0x%08x\n", rc2);
15369 #endif /* !defined(SQLITE_UNTESTABLE) */
15371 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
15373 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
15376 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
15378 enableTimer = booleanValue(azArg[1]);
15379 if( enableTimer && !HAS_TIMER ){
15380 raw_printf(stderr, "Error: timer not available on this system.\n");
15384 raw_printf(stderr, "Usage: .timer on|off\n");
15389 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
15392 raw_printf(stderr, "Usage: .trace FILE|off\n");
15394 goto meta_command_exit;
15396 output_file_close(p->traceOut);
15397 p->traceOut = output_file_open(azArg[1], 0);
15398 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
15399 if( p->traceOut==0 ){
15400 sqlite3_trace_v2(p->db, 0, 0, 0);
15402 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
15407 #if SQLITE_USER_AUTHENTICATION
15408 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
15410 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
15412 goto meta_command_exit;
15415 if( strcmp(azArg[1],"login")==0 ){
15417 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
15419 goto meta_command_exit;
15421 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
15423 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
15426 }else if( strcmp(azArg[1],"add")==0 ){
15428 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
15430 goto meta_command_exit;
15432 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15433 booleanValue(azArg[4]));
15435 raw_printf(stderr, "User-Add failed: %d\n", rc);
15438 }else if( strcmp(azArg[1],"edit")==0 ){
15440 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
15442 goto meta_command_exit;
15444 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15445 booleanValue(azArg[4]));
15447 raw_printf(stderr, "User-Edit failed: %d\n", rc);
15450 }else if( strcmp(azArg[1],"delete")==0 ){
15452 raw_printf(stderr, "Usage: .user delete USER\n");
15454 goto meta_command_exit;
15456 rc = sqlite3_user_delete(p->db, azArg[2]);
15458 raw_printf(stderr, "User-Delete failed: %d\n", rc);
15462 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
15464 goto meta_command_exit;
15467 #endif /* SQLITE_USER_AUTHENTICATION */
15469 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
15470 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
15471 sqlite3_libversion(), sqlite3_sourceid());
15472 #if SQLITE_HAVE_ZLIB
15473 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
15475 #define CTIMEOPT_VAL_(opt) #opt
15476 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
15477 #if defined(__clang__) && defined(__clang_major__)
15478 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
15479 CTIMEOPT_VAL(__clang_minor__) "."
15480 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
15481 #elif defined(_MSC_VER)
15482 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
15483 #elif defined(__GNUC__) && defined(__VERSION__)
15484 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
15488 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
15489 const char *zDbName = nArg==2 ? azArg[1] : "main";
15490 sqlite3_vfs *pVfs = 0;
15492 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
15494 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
15495 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15496 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15497 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15502 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
15504 sqlite3_vfs *pCurrent = 0;
15506 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
15508 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
15509 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
15510 pVfs==pCurrent ? " <--- CURRENT" : "");
15511 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15512 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15513 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15515 raw_printf(p->out, "-----------------------------------\n");
15520 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
15521 const char *zDbName = nArg==2 ? azArg[1] : "main";
15522 char *zVfsName = 0;
15524 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
15526 utf8_printf(p->out, "%s\n", zVfsName);
15527 sqlite3_free(zVfsName);
15532 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
15533 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
15534 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
15538 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
15540 assert( nArg<=ArraySize(azArg) );
15541 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
15542 p->colWidth[j-1] = (int)integerValue(azArg[j]);
15547 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
15548 " \"%s\". Enter \".help\" for help\n", azArg[0]);
15555 if( p->outCount==0 ) output_reset(p);
15561 ** Return TRUE if a semicolon occurs anywhere in the first N characters
15564 static int line_contains_semicolon(const char *z, int N){
15566 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
15571 ** Test to see if a line consists entirely of whitespace.
15573 static int _all_whitespace(const char *z){
15575 if( IsSpace(z[0]) ) continue;
15576 if( *z=='/' && z[1]=='*' ){
15578 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
15579 if( *z==0 ) return 0;
15583 if( *z=='-' && z[1]=='-' ){
15585 while( *z && *z!='\n' ){ z++; }
15586 if( *z==0 ) return 1;
15595 ** Return TRUE if the line typed in is an SQL command terminator other
15596 ** than a semi-colon. The SQL Server style "go" command is understood
15597 ** as is the Oracle "/".
15599 static int line_is_command_terminator(const char *zLine){
15600 while( IsSpace(zLine[0]) ){ zLine++; };
15601 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
15602 return 1; /* Oracle */
15604 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
15605 && _all_whitespace(&zLine[2]) ){
15606 return 1; /* SQL Server */
15612 ** We need a default sqlite3_complete() implementation to use in case
15613 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
15614 ** any arbitrary text is a complete SQL statement. This is not very
15615 ** user-friendly, but it does seem to work.
15617 #ifdef SQLITE_OMIT_COMPLETE
15618 #define sqlite3_complete(x) 1
15622 ** Return true if zSql is a complete SQL statement. Return false if it
15623 ** ends in the middle of a string literal or C-style comment.
15625 static int line_is_complete(char *zSql, int nSql){
15627 if( zSql==0 ) return 1;
15630 rc = sqlite3_complete(zSql);
15636 ** Run a single line of SQL. Return the number of errors.
15638 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
15643 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
15645 rc = shell_exec(p, zSql, &zErrMsg);
15647 if( rc || zErrMsg ){
15649 if( in!=0 || !stdin_is_interactive ){
15650 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
15651 "Error: near line %d:", startline);
15653 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
15656 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
15657 sqlite3_free(zErrMsg);
15660 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
15663 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
15664 raw_printf(p->out, "changes: %3d total_changes: %d\n",
15665 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
15672 ** Read input from *in and process it. If *in==0 then input
15673 ** is interactive - the user is typing it it. Otherwise, input
15674 ** is coming from a file or device. A prompt is issued and history
15675 ** is saved only if input is interactive. An interrupt signal will
15676 ** cause this routine to exit immediately, unless input is interactive.
15678 ** Return the number of errors.
15680 static int process_input(ShellState *p, FILE *in){
15681 char *zLine = 0; /* A single input line */
15682 char *zSql = 0; /* Accumulated SQL text */
15683 int nLine; /* Length of current line */
15684 int nSql = 0; /* Bytes of zSql[] used */
15685 int nAlloc = 0; /* Allocated zSql[] space */
15686 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
15687 int rc; /* Error code */
15688 int errCnt = 0; /* Number of errors seen */
15689 int lineno = 0; /* Current line number */
15690 int startline = 0; /* Line number for start of current input */
15692 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
15694 zLine = one_input_line(in, zLine, nSql>0);
15697 if( in==0 && stdin_is_interactive ) printf("\n");
15700 if( seenInterrupt ){
15705 if( nSql==0 && _all_whitespace(zLine) ){
15706 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15709 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
15710 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
15711 if( zLine[0]=='.' ){
15712 rc = do_meta_command(zLine, p);
15713 if( rc==2 ){ /* exit requested */
15721 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
15722 memcpy(zLine,";",2);
15724 nLine = strlen30(zLine);
15725 if( nSql+nLine+2>=nAlloc ){
15726 nAlloc = nSql+nLine+100;
15727 zSql = realloc(zSql, nAlloc);
15728 if( zSql==0 ) shell_out_of_memory();
15733 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
15734 assert( nAlloc>0 && zSql!=0 );
15735 memcpy(zSql, zLine+i, nLine+1-i);
15736 startline = lineno;
15739 zSql[nSql++] = '\n';
15740 memcpy(zSql+nSql, zLine, nLine+1);
15743 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
15744 && sqlite3_complete(zSql) ){
15745 errCnt += runOneSqlLine(p, zSql, in, startline);
15753 }else if( nSql && _all_whitespace(zSql) ){
15754 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
15758 if( nSql && !_all_whitespace(zSql) ){
15759 errCnt += runOneSqlLine(p, zSql, in, startline);
15767 ** Return a pathname which is the user's home directory. A
15768 ** 0 return indicates an error of some kind.
15770 static char *find_home_dir(int clearFlag){
15771 static char *home_dir = NULL;
15777 if( home_dir ) return home_dir;
15779 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
15780 && !defined(__RTP__) && !defined(_WRS_KERNEL)
15782 struct passwd *pwent;
15783 uid_t uid = getuid();
15784 if( (pwent=getpwuid(uid)) != NULL) {
15785 home_dir = pwent->pw_dir;
15790 #if defined(_WIN32_WCE)
15791 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
15796 #if defined(_WIN32) || defined(WIN32)
15798 home_dir = getenv("USERPROFILE");
15803 home_dir = getenv("HOME");
15806 #if defined(_WIN32) || defined(WIN32)
15808 char *zDrive, *zPath;
15810 zDrive = getenv("HOMEDRIVE");
15811 zPath = getenv("HOMEPATH");
15812 if( zDrive && zPath ){
15813 n = strlen30(zDrive) + strlen30(zPath) + 1;
15814 home_dir = malloc( n );
15815 if( home_dir==0 ) return 0;
15816 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
15823 #endif /* !_WIN32_WCE */
15826 int n = strlen30(home_dir) + 1;
15827 char *z = malloc( n );
15828 if( z ) memcpy(z, home_dir, n);
15836 ** Read input from the file given by sqliterc_override. Or if that
15837 ** parameter is NULL, take input from ~/.sqliterc
15839 ** Returns the number of errors.
15841 static void process_sqliterc(
15842 ShellState *p, /* Configuration data */
15843 const char *sqliterc_override /* Name of config file. NULL to use default */
15845 char *home_dir = NULL;
15846 const char *sqliterc = sqliterc_override;
15850 if (sqliterc == NULL) {
15851 home_dir = find_home_dir(0);
15853 raw_printf(stderr, "-- warning: cannot find home directory;"
15854 " cannot read ~/.sqliterc\n");
15857 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
15860 in = fopen(sqliterc,"rb");
15862 if( stdin_is_interactive ){
15863 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
15865 process_input(p,in);
15868 sqlite3_free(zBuf);
15872 ** Show available command line options
15874 static const char zOptions[] =
15875 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15876 " -A ARGS... run \".archive ARGS\" and exit\n"
15878 " -append append the database to the end of the file\n"
15879 " -ascii set output mode to 'ascii'\n"
15880 " -bail stop after hitting an error\n"
15881 " -batch force batch I/O\n"
15882 " -column set output mode to 'column'\n"
15883 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
15884 " -csv set output mode to 'csv'\n"
15885 " -echo print commands before execution\n"
15886 " -init FILENAME read/process named file\n"
15887 " -[no]header turn headers on or off\n"
15888 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15889 " -heap SIZE Size of heap for memsys3 or memsys5\n"
15891 " -help show this message\n"
15892 " -html set output mode to HTML\n"
15893 " -interactive force interactive I/O\n"
15894 " -line set output mode to 'line'\n"
15895 " -list set output mode to 'list'\n"
15896 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
15897 " -mmap N default mmap size set to N\n"
15898 #ifdef SQLITE_ENABLE_MULTIPLEX
15899 " -multiplex enable the multiplexor VFS\n"
15901 " -newline SEP set output row separator. Default: '\\n'\n"
15902 " -nullvalue TEXT set text string for NULL values. Default ''\n"
15903 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
15904 " -quote set output mode to 'quote'\n"
15905 " -readonly open the database read-only\n"
15906 " -separator SEP set output column separator. Default: '|'\n"
15907 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
15908 " -sorterref SIZE sorter references threshold size\n"
15910 " -stats print memory stats before each finalize\n"
15911 " -version show SQLite version\n"
15912 " -vfs NAME use NAME as the default VFS\n"
15913 #ifdef SQLITE_ENABLE_VFSTRACE
15914 " -vfstrace enable tracing of all VFS calls\n"
15916 #ifdef SQLITE_HAVE_ZLIB
15917 " -zip open the file as a ZIP Archive\n"
15920 static void usage(int showDetail){
15921 utf8_printf(stderr,
15922 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
15923 "FILENAME is the name of an SQLite database. A new database is created\n"
15924 "if the file does not previously exist.\n", Argv0);
15926 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
15928 raw_printf(stderr, "Use the -help option for additional information\n");
15934 ** Internal check: Verify that the SQLite is uninitialized. Print a
15935 ** error message if it is initialized.
15937 static void verify_uninitialized(void){
15938 if( sqlite3_config(-1)==SQLITE_MISUSE ){
15939 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
15940 " initialization.\n");
15945 ** Initialize the state information in data
15947 static void main_init(ShellState *data) {
15948 memset(data, 0, sizeof(*data));
15949 data->normalMode = data->cMode = data->mode = MODE_List;
15950 data->autoExplain = 1;
15951 memcpy(data->colSeparator,SEP_Column, 2);
15952 memcpy(data->rowSeparator,SEP_Row, 2);
15953 data->showHeader = 0;
15954 data->shellFlgs = SHFLG_Lookaside;
15955 verify_uninitialized();
15956 sqlite3_config(SQLITE_CONFIG_URI, 1);
15957 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
15958 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
15959 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
15960 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
15964 ** Output text to the console in a font that attracts extra attention.
15967 static void printBold(const char *zText){
15968 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
15969 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
15970 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
15971 SetConsoleTextAttribute(out,
15972 FOREGROUND_RED|FOREGROUND_INTENSITY
15974 printf("%s", zText);
15975 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
15978 static void printBold(const char *zText){
15979 printf("\033[1m%s\033[0m", zText);
15984 ** Get the argument to an --option. Throw an error and die if no argument
15987 static char *cmdline_option_value(int argc, char **argv, int i){
15989 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
15990 argv[0], argv[argc-1]);
15996 #ifndef SQLITE_SHELL_IS_UTF8
15997 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
15998 # define SQLITE_SHELL_IS_UTF8 (0)
16000 # define SQLITE_SHELL_IS_UTF8 (1)
16004 #if SQLITE_SHELL_IS_UTF8
16005 int SQLITE_CDECL main(int argc, char **argv){
16007 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
16012 const char *zInitFile = 0;
16015 int warnInmemoryDb = 0;
16019 const char *zVfs = 0; /* Value of -vfs command-line option */
16020 #if !SQLITE_SHELL_IS_UTF8
16021 char **argvToFree = 0;
16022 int argcToFree = 0;
16025 setBinaryMode(stdin, 0);
16026 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
16027 stdin_is_interactive = isatty(0);
16028 stdout_is_console = isatty(1);
16030 #if !defined(_WIN32_WCE)
16031 if( getenv("SQLITE_DEBUG_BREAK") ){
16032 if( isatty(0) && isatty(2) ){
16034 "attach debugger to process %d and press any key to continue.\n",
16038 #if defined(_WIN32) || defined(WIN32)
16040 #elif defined(SIGTRAP)
16047 #if USE_SYSTEM_SQLITE+0!=1
16048 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
16049 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
16050 sqlite3_sourceid(), SQLITE_SOURCE_ID);
16056 /* On Windows, we must translate command-line arguments into UTF-8.
16057 ** The SQLite memory allocator subsystem has to be enabled in order to
16058 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
16059 ** subsequent sqlite3_config() calls will work. So copy all results into
16060 ** memory that does not come from the SQLite memory allocator.
16062 #if !SQLITE_SHELL_IS_UTF8
16063 sqlite3_initialize();
16064 argvToFree = malloc(sizeof(argv[0])*argc*2);
16066 argv = argvToFree + argc;
16067 if( argv==0 ) shell_out_of_memory();
16068 for(i=0; i<argc; i++){
16069 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
16071 if( z==0 ) shell_out_of_memory();
16072 n = (int)strlen(z);
16073 argv[i] = malloc( n+1 );
16074 if( argv[i]==0 ) shell_out_of_memory();
16075 memcpy(argv[i], z, n+1);
16076 argvToFree[i] = argv[i];
16079 sqlite3_shutdown();
16082 assert( argc>=1 && argv && argv[0] );
16085 /* Make sure we have a valid signal handler early, before anything
16089 signal(SIGINT, interrupt_handler);
16090 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
16091 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
16094 #ifdef SQLITE_SHELL_DBNAME_PROC
16096 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
16097 ** of a C-function that will provide the name of the database file. Use
16098 ** this compile-time option to embed this shell program in larger
16099 ** applications. */
16100 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
16101 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
16102 warnInmemoryDb = 0;
16106 /* Do an initial pass through the command-line argument to locate
16107 ** the name of the database file, the name of the initialization file,
16108 ** the size of the alternative malloc heap,
16109 ** and the first command to execute.
16111 verify_uninitialized();
16112 for(i=1; i<argc; i++){
16116 if( data.zDbFilename==0 ){
16117 data.zDbFilename = z;
16119 /* Excesss arguments are interpreted as SQL (or dot-commands) and
16120 ** mean that nothing is read from stdin */
16123 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
16124 if( azCmd==0 ) shell_out_of_memory();
16128 if( z[1]=='-' ) z++;
16129 if( strcmp(z,"-separator")==0
16130 || strcmp(z,"-nullvalue")==0
16131 || strcmp(z,"-newline")==0
16132 || strcmp(z,"-cmd")==0
16134 (void)cmdline_option_value(argc, argv, ++i);
16135 }else if( strcmp(z,"-init")==0 ){
16136 zInitFile = cmdline_option_value(argc, argv, ++i);
16137 }else if( strcmp(z,"-batch")==0 ){
16138 /* Need to check for batch mode here to so we can avoid printing
16139 ** informational messages (like from process_sqliterc) before
16140 ** we do the actual processing of arguments later in a second pass.
16142 stdin_is_interactive = 0;
16143 }else if( strcmp(z,"-heap")==0 ){
16144 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16146 sqlite3_int64 szHeap;
16148 zSize = cmdline_option_value(argc, argv, ++i);
16149 szHeap = integerValue(zSize);
16150 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
16151 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
16153 (void)cmdline_option_value(argc, argv, ++i);
16155 }else if( strcmp(z,"-pagecache")==0 ){
16157 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16158 if( sz>70000 ) sz = 70000;
16160 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16161 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
16162 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
16163 data.shellFlgs |= SHFLG_Pagecache;
16164 }else if( strcmp(z,"-lookaside")==0 ){
16166 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16168 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16170 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
16171 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
16172 #ifdef SQLITE_ENABLE_VFSTRACE
16173 }else if( strcmp(z,"-vfstrace")==0 ){
16174 extern int vfstrace_register(
16175 const char *zTraceName,
16176 const char *zOldVfsName,
16177 int (*xOut)(const char*,void*),
16181 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
16183 #ifdef SQLITE_ENABLE_MULTIPLEX
16184 }else if( strcmp(z,"-multiplex")==0 ){
16185 extern int sqlite3_multiple_initialize(const char*,int);
16186 sqlite3_multiplex_initialize(0, 1);
16188 }else if( strcmp(z,"-mmap")==0 ){
16189 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16190 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
16191 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16192 }else if( strcmp(z,"-sorterref")==0 ){
16193 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16194 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
16196 }else if( strcmp(z,"-vfs")==0 ){
16197 zVfs = cmdline_option_value(argc, argv, ++i);
16198 #ifdef SQLITE_HAVE_ZLIB
16199 }else if( strcmp(z,"-zip")==0 ){
16200 data.openMode = SHELL_OPEN_ZIPFILE;
16202 }else if( strcmp(z,"-append")==0 ){
16203 data.openMode = SHELL_OPEN_APPENDVFS;
16204 #ifdef SQLITE_ENABLE_DESERIALIZE
16205 }else if( strcmp(z,"-deserialize")==0 ){
16206 data.openMode = SHELL_OPEN_DESERIALIZE;
16208 }else if( strcmp(z,"-readonly")==0 ){
16209 data.openMode = SHELL_OPEN_READONLY;
16210 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16211 }else if( strncmp(z, "-A",2)==0 ){
16212 /* All remaining command-line arguments are passed to the ".archive"
16213 ** command, so ignore them */
16218 verify_uninitialized();
16221 #ifdef SQLITE_SHELL_INIT_PROC
16223 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16224 ** of a C-function that will perform initialization actions on SQLite that
16225 ** occur just before or after sqlite3_initialize(). Use this compile-time
16226 ** option to embed this shell program in larger applications. */
16227 extern void SQLITE_SHELL_INIT_PROC(void);
16228 SQLITE_SHELL_INIT_PROC();
16231 /* All the sqlite3_config() calls have now been made. So it is safe
16232 ** to call sqlite3_initialize() and process any command line -vfs option. */
16233 sqlite3_initialize();
16237 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16239 sqlite3_vfs_register(pVfs, 1);
16241 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16246 if( data.zDbFilename==0 ){
16247 #ifndef SQLITE_OMIT_MEMORYDB
16248 data.zDbFilename = ":memory:";
16249 warnInmemoryDb = argc==1;
16251 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16256 sqlite3_appendvfs_init(0,0,0);
16258 /* Go ahead and open the database file if it already exists. If the
16259 ** file does not exist, delay opening it. This prevents empty database
16260 ** files from being created if a user mistypes the database name argument
16261 ** to the sqlite command-line tool.
16263 if( access(data.zDbFilename, 0)==0 ){
16267 /* Process the initialization file if there is one. If no -init option
16268 ** is given on the command line, look for a file named ~/.sqliterc and
16269 ** try to process it.
16271 process_sqliterc(&data,zInitFile);
16273 /* Make a second pass through the command-line argument and set
16274 ** options. This second pass is delayed until after the initialization
16275 ** file is processed so that the command-line arguments will override
16276 ** settings in the initialization file.
16278 for(i=1; i<argc; i++){
16280 if( z[0]!='-' ) continue;
16281 if( z[1]=='-' ){ z++; }
16282 if( strcmp(z,"-init")==0 ){
16284 }else if( strcmp(z,"-html")==0 ){
16285 data.mode = MODE_Html;
16286 }else if( strcmp(z,"-list")==0 ){
16287 data.mode = MODE_List;
16288 }else if( strcmp(z,"-quote")==0 ){
16289 data.mode = MODE_Quote;
16290 }else if( strcmp(z,"-line")==0 ){
16291 data.mode = MODE_Line;
16292 }else if( strcmp(z,"-column")==0 ){
16293 data.mode = MODE_Column;
16294 }else if( strcmp(z,"-csv")==0 ){
16295 data.mode = MODE_Csv;
16296 memcpy(data.colSeparator,",",2);
16297 #ifdef SQLITE_HAVE_ZLIB
16298 }else if( strcmp(z,"-zip")==0 ){
16299 data.openMode = SHELL_OPEN_ZIPFILE;
16301 }else if( strcmp(z,"-append")==0 ){
16302 data.openMode = SHELL_OPEN_APPENDVFS;
16303 #ifdef SQLITE_ENABLE_DESERIALIZE
16304 }else if( strcmp(z,"-deserialize")==0 ){
16305 data.openMode = SHELL_OPEN_DESERIALIZE;
16307 }else if( strcmp(z,"-readonly")==0 ){
16308 data.openMode = SHELL_OPEN_READONLY;
16309 }else if( strcmp(z,"-ascii")==0 ){
16310 data.mode = MODE_Ascii;
16311 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16313 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16315 }else if( strcmp(z,"-separator")==0 ){
16316 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16317 "%s",cmdline_option_value(argc,argv,++i));
16318 }else if( strcmp(z,"-newline")==0 ){
16319 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16320 "%s",cmdline_option_value(argc,argv,++i));
16321 }else if( strcmp(z,"-nullvalue")==0 ){
16322 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
16323 "%s",cmdline_option_value(argc,argv,++i));
16324 }else if( strcmp(z,"-header")==0 ){
16325 data.showHeader = 1;
16326 }else if( strcmp(z,"-noheader")==0 ){
16327 data.showHeader = 0;
16328 }else if( strcmp(z,"-echo")==0 ){
16329 ShellSetFlag(&data, SHFLG_Echo);
16330 }else if( strcmp(z,"-eqp")==0 ){
16331 data.autoEQP = AUTOEQP_on;
16332 }else if( strcmp(z,"-eqpfull")==0 ){
16333 data.autoEQP = AUTOEQP_full;
16334 }else if( strcmp(z,"-stats")==0 ){
16336 }else if( strcmp(z,"-scanstats")==0 ){
16337 data.scanstatsOn = 1;
16338 }else if( strcmp(z,"-backslash")==0 ){
16339 /* Undocumented command-line option: -backslash
16340 ** Causes C-style backslash escapes to be evaluated in SQL statements
16341 ** prior to sending the SQL into SQLite. Useful for injecting
16342 ** crazy bytes in the middle of SQL statements for testing and debugging.
16344 ShellSetFlag(&data, SHFLG_Backslash);
16345 }else if( strcmp(z,"-bail")==0 ){
16347 }else if( strcmp(z,"-version")==0 ){
16348 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
16350 }else if( strcmp(z,"-interactive")==0 ){
16351 stdin_is_interactive = 1;
16352 }else if( strcmp(z,"-batch")==0 ){
16353 stdin_is_interactive = 0;
16354 }else if( strcmp(z,"-heap")==0 ){
16356 }else if( strcmp(z,"-pagecache")==0 ){
16358 }else if( strcmp(z,"-lookaside")==0 ){
16360 }else if( strcmp(z,"-mmap")==0 ){
16362 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16363 }else if( strcmp(z,"-sorterref")==0 ){
16366 }else if( strcmp(z,"-vfs")==0 ){
16368 #ifdef SQLITE_ENABLE_VFSTRACE
16369 }else if( strcmp(z,"-vfstrace")==0 ){
16372 #ifdef SQLITE_ENABLE_MULTIPLEX
16373 }else if( strcmp(z,"-multiplex")==0 ){
16376 }else if( strcmp(z,"-help")==0 ){
16378 }else if( strcmp(z,"-cmd")==0 ){
16379 /* Run commands that follow -cmd first and separately from commands
16380 ** that simply appear on the command-line. This seems goofy. It would
16381 ** be better if all commands ran in the order that they appear. But
16382 ** we retain the goofy behavior for historical compatibility. */
16383 if( i==argc-1 ) break;
16384 z = cmdline_option_value(argc,argv,++i);
16386 rc = do_meta_command(z, &data);
16387 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
16390 rc = shell_exec(&data, z, &zErrMsg);
16392 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16393 if( bail_on_error ) return rc!=0 ? rc : 1;
16395 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
16396 if( bail_on_error ) return rc;
16399 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16400 }else if( strncmp(z, "-A", 2)==0 ){
16402 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16403 " with \"%s\"\n", z);
16406 open_db(&data, OPEN_DB_ZIPFILE);
16409 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16411 arDotCommand(&data, 1, argv+i, argc-i);
16417 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
16418 raw_printf(stderr,"Use -help for a list of options.\n");
16421 data.cMode = data.mode;
16425 /* Run all arguments that do not begin with '-' as if they were separate
16426 ** command-line inputs, except for the argToSkip argument which contains
16427 ** the database filename.
16429 for(i=0; i<nCmd; i++){
16430 if( azCmd[i][0]=='.' ){
16431 rc = do_meta_command(azCmd[i], &data);
16432 if( rc ) return rc==2 ? 0 : rc;
16435 rc = shell_exec(&data, azCmd[i], &zErrMsg);
16437 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16438 return rc!=0 ? rc : 1;
16440 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
16447 /* Run commands received from standard input
16449 if( stdin_is_interactive ){
16454 "SQLite version %s %.19s\n" /*extra-version-info*/
16455 "Enter \".help\" for usage hints.\n",
16456 sqlite3_libversion(), sqlite3_sourceid()
16458 if( warnInmemoryDb ){
16459 printf("Connected to a ");
16460 printBold("transient in-memory database");
16461 printf(".\nUse \".open FILENAME\" to reopen on a "
16462 "persistent database.\n");
16464 zHistory = getenv("SQLITE_HISTORY");
16466 zHistory = strdup(zHistory);
16467 }else if( (zHome = find_home_dir(0))!=0 ){
16468 nHistory = strlen30(zHome) + 20;
16469 if( (zHistory = malloc(nHistory))!=0 ){
16470 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
16473 if( zHistory ){ shell_read_history(zHistory); }
16474 #if HAVE_READLINE || HAVE_EDITLINE
16475 rl_attempted_completion_function = readline_completion;
16476 #elif HAVE_LINENOISE
16477 linenoiseSetCompletionCallback(linenoise_completion);
16479 rc = process_input(&data, 0);
16481 shell_stifle_history(2000);
16482 shell_write_history(zHistory);
16486 rc = process_input(&data, stdin);
16489 set_table_name(&data, 0);
16491 session_close_all(&data);
16494 sqlite3_free(data.zFreeOnClose);
16496 output_reset(&data);
16497 data.doXdgOpen = 0;
16498 clearTempFile(&data);
16499 #if !SQLITE_SHELL_IS_UTF8
16500 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16503 /* Clear the global data structure so that valgrind will detect memory
16505 memset(&data, 0, sizeof(data));