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 strdup _strdup
163 # define popen _popen
165 # define pclose _pclose
167 /* Make sure isatty() has a prototype. */
168 extern int isatty(int);
170 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
171 /* popen and pclose are not C89 functions and so are
172 ** sometimes omitted from the <stdio.h> header */
173 extern FILE *popen(const char*,const char*);
174 extern int pclose(FILE*);
176 # define SQLITE_OMIT_POPEN 1
180 #if defined(_WIN32_WCE)
181 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
182 * thus we always assume that we have a console. That can be
183 * overridden with the -batch command line option.
188 /* ctype macros that work with signed characters */
189 #define IsSpace(X) isspace((unsigned char)X)
190 #define IsDigit(X) isdigit((unsigned char)X)
191 #define ToLower(X) (char)tolower((unsigned char)X)
193 #if defined(_WIN32) || defined(WIN32)
196 /* string conversion routines only needed on Win32 */
197 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
198 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
199 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
200 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
203 /* On Windows, we normally run with output mode of TEXT so that \n characters
204 ** are automatically translated into \r\n. However, this behavior needs
205 ** to be disabled in some cases (ex: when generating CSV output and when
206 ** rendering quoted strings that contain \n characters). The following
207 ** routines take care of that.
209 #if defined(_WIN32) || defined(WIN32)
210 static void setBinaryMode(FILE *file, int isOutput){
211 if( isOutput ) fflush(file);
212 _setmode(_fileno(file), _O_BINARY);
214 static void setTextMode(FILE *file, int isOutput){
215 if( isOutput ) fflush(file);
216 _setmode(_fileno(file), _O_TEXT);
219 # define setBinaryMode(X,Y)
220 # define setTextMode(X,Y)
224 /* True if the timer is enabled */
225 static int enableTimer = 0;
227 /* Return the current wall-clock time */
228 static sqlite3_int64 timeOfDay(void){
229 static sqlite3_vfs *clockVfs = 0;
231 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
232 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
233 clockVfs->xCurrentTimeInt64(clockVfs, &t);
236 clockVfs->xCurrentTime(clockVfs, &r);
237 t = (sqlite3_int64)(r*86400000.0);
242 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
243 #include <sys/time.h>
244 #include <sys/resource.h>
246 /* VxWorks does not support getrusage() as far as we can determine */
247 #if defined(_WRS_KERNEL) || defined(__RTP__)
249 struct timeval ru_utime; /* user CPU time used */
250 struct timeval ru_stime; /* system CPU time used */
252 #define getrusage(A,B) memset(B,0,sizeof(*B))
255 /* Saved resource information for the beginning of an operation */
256 static struct rusage sBegin; /* CPU time at start */
257 static sqlite3_int64 iBegin; /* Wall-clock time at start */
260 ** Begin timing an operation
262 static void beginTimer(void){
264 getrusage(RUSAGE_SELF, &sBegin);
265 iBegin = timeOfDay();
269 /* Return the difference of two time_structs in seconds */
270 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
271 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
272 (double)(pEnd->tv_sec - pStart->tv_sec);
276 ** Print the timing results.
278 static void endTimer(void){
280 sqlite3_int64 iEnd = timeOfDay();
282 getrusage(RUSAGE_SELF, &sEnd);
283 printf("Run Time: real %.3f user %f sys %f\n",
284 (iEnd - iBegin)*0.001,
285 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
286 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
290 #define BEGIN_TIMER beginTimer()
291 #define END_TIMER endTimer()
294 #elif (defined(_WIN32) || defined(WIN32))
296 /* Saved resource information for the beginning of an operation */
297 static HANDLE hProcess;
298 static FILETIME ftKernelBegin;
299 static FILETIME ftUserBegin;
300 static sqlite3_int64 ftWallBegin;
301 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
302 LPFILETIME, LPFILETIME);
303 static GETPROCTIMES getProcessTimesAddr = NULL;
306 ** Check to see if we have timer support. Return 1 if necessary
307 ** support found (or found previously).
309 static int hasTimer(void){
310 if( getProcessTimesAddr ){
313 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
314 ** versions. See if the version we are running on has it, and if it
315 ** does, save off a pointer to it and the current process handle.
317 hProcess = GetCurrentProcess();
319 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
320 if( NULL != hinstLib ){
321 getProcessTimesAddr =
322 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
323 if( NULL != getProcessTimesAddr ){
326 FreeLibrary(hinstLib);
334 ** Begin timing an operation
336 static void beginTimer(void){
337 if( enableTimer && getProcessTimesAddr ){
338 FILETIME ftCreation, ftExit;
339 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
340 &ftKernelBegin,&ftUserBegin);
341 ftWallBegin = timeOfDay();
345 /* Return the difference of two FILETIME structs in seconds */
346 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
347 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
348 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
349 return (double) ((i64End - i64Start) / 10000000.0);
353 ** Print the timing results.
355 static void endTimer(void){
356 if( enableTimer && getProcessTimesAddr){
357 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
358 sqlite3_int64 ftWallEnd = timeOfDay();
359 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
360 printf("Run Time: real %.3f user %f sys %f\n",
361 (ftWallEnd - ftWallBegin)*0.001,
362 timeDiff(&ftUserBegin, &ftUserEnd),
363 timeDiff(&ftKernelBegin, &ftKernelEnd));
367 #define BEGIN_TIMER beginTimer()
368 #define END_TIMER endTimer()
369 #define HAS_TIMER hasTimer()
378 ** Used to prevent warnings about unused parameters
380 #define UNUSED_PARAMETER(x) (void)(x)
383 ** Number of elements in an array
385 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
388 ** If the following flag is set, then command execution stops
389 ** at an error if we are not interactive.
391 static int bail_on_error = 0;
394 ** Threat stdin as an interactive input if the following variable
395 ** is true. Otherwise, assume stdin is connected to a file or pipe.
397 static int stdin_is_interactive = 1;
400 ** On Windows systems we have to know if standard output is a console
401 ** in order to translate UTF-8 into MBCS. The following variable is
402 ** true if translation is required.
404 static int stdout_is_console = 1;
407 ** The following is the open SQLite database. We make a pointer
408 ** to this database a static variable so that it can be accessed
409 ** by the SIGINT handler to interrupt database processing.
411 static sqlite3 *globalDb = 0;
414 ** True if an interrupt (Control-C) has been received.
416 static volatile int seenInterrupt = 0;
419 ** This is the name of our program. It is set in main(), used
420 ** in a number of other places, mostly for error messages.
425 ** Prompt strings. Initialized in main. Settable with
426 ** .prompt main continue
428 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
429 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
432 ** Render output like fprintf(). Except, if the output is going to the
433 ** console and if this is running on a Windows machine, translate the
434 ** output from UTF-8 into MBCS.
436 #if defined(_WIN32) || defined(WIN32)
437 void utf8_printf(FILE *out, const char *zFormat, ...){
439 va_start(ap, zFormat);
440 if( stdout_is_console && (out==stdout || out==stderr) ){
441 char *z1 = sqlite3_vmprintf(zFormat, ap);
442 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
447 vfprintf(out, zFormat, ap);
451 #elif !defined(utf8_printf)
452 # define utf8_printf fprintf
456 ** Render output like fprintf(). This should not be used on anything that
457 ** includes string formatting (e.g. "%s").
459 #if !defined(raw_printf)
460 # define raw_printf fprintf
463 /* Indicate out-of-memory and exit. */
464 static void shell_out_of_memory(void){
465 raw_printf(stderr,"Error: out of memory\n");
470 ** Write I/O traces to the following stream.
472 #ifdef SQLITE_ENABLE_IOTRACE
473 static FILE *iotrace = 0;
477 ** This routine works like printf in that its first argument is a
478 ** format string and subsequent arguments are values to be substituted
479 ** in place of % fields. The result of formatting this string
480 ** is written to iotrace.
482 #ifdef SQLITE_ENABLE_IOTRACE
483 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
486 if( iotrace==0 ) return;
487 va_start(ap, zFormat);
488 z = sqlite3_vmprintf(zFormat, ap);
490 utf8_printf(iotrace, "%s", z);
496 ** Output string zUtf to stream pOut as w characters. If w is negative,
497 ** then right-justify the text. W is the width in UTF-8 characters, not
498 ** in bytes. This is different from the %*.*s specification in printf
499 ** since with %*.*s the width is measured in bytes, not characters.
501 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
504 int aw = w<0 ? -w : w;
506 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
507 for(i=n=0; zUtf[i]; i++){
508 if( (zUtf[i]&0xc0)!=0x80 ){
511 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
517 utf8_printf(pOut, "%.*s", i, zUtf);
519 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
521 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
527 ** Determines if a string is a number of not.
529 static int isNumber(const char *z, int *realnum){
530 if( *z=='-' || *z=='+' ) z++;
535 if( realnum ) *realnum = 0;
536 while( IsDigit(*z) ){ z++; }
539 if( !IsDigit(*z) ) return 0;
540 while( IsDigit(*z) ){ z++; }
541 if( realnum ) *realnum = 1;
543 if( *z=='e' || *z=='E' ){
545 if( *z=='+' || *z=='-' ) z++;
546 if( !IsDigit(*z) ) return 0;
547 while( IsDigit(*z) ){ z++; }
548 if( realnum ) *realnum = 1;
554 ** Compute a string length that is limited to what can be stored in
555 ** lower 30 bits of a 32-bit signed integer.
557 static int strlen30(const char *z){
559 while( *z2 ){ z2++; }
560 return 0x3fffffff & (int)(z2 - z);
564 ** Return the length of a string in characters. Multibyte UTF8 characters
565 ** count as a single character.
567 static int strlenChar(const char *z){
570 if( (0xc0&*(z++))!=0x80 ) n++;
576 ** This routine reads a line of text from FILE in, stores
577 ** the text in memory obtained from malloc() and returns a pointer
578 ** to the text. NULL is returned at end of file, or if malloc()
581 ** If zLine is not NULL then it is a malloced buffer returned from
582 ** a previous call to this routine that may be reused.
584 static char *local_getline(char *zLine, FILE *in){
585 int nLine = zLine==0 ? 0 : 100;
590 nLine = nLine*2 + 100;
591 zLine = realloc(zLine, nLine);
592 if( zLine==0 ) shell_out_of_memory();
594 if( fgets(&zLine[n], nLine - n, in)==0 ){
602 while( zLine[n] ) n++;
603 if( n>0 && zLine[n-1]=='\n' ){
605 if( n>0 && zLine[n-1]=='\r' ) n--;
610 #if defined(_WIN32) || defined(WIN32)
611 /* For interactive input on Windows systems, translate the
612 ** multi-byte characterset characters into UTF-8. */
613 if( stdin_is_interactive && in==stdin ){
614 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
616 int nTrans = strlen30(zTrans)+1;
618 zLine = realloc(zLine, nTrans);
619 if( zLine==0 ) shell_out_of_memory();
621 memcpy(zLine, zTrans, nTrans);
622 sqlite3_free(zTrans);
625 #endif /* defined(_WIN32) || defined(WIN32) */
630 ** Retrieve a single line of input text.
632 ** If in==0 then read from standard input and prompt before each line.
633 ** If isContinuation is true, then a continuation prompt is appropriate.
634 ** If isContinuation is zero, then the main prompt should be used.
636 ** If zPrior is not NULL then it is a buffer from a prior call to this
637 ** routine that can be reused.
639 ** The result is stored in space obtained from malloc() and must either
640 ** be freed by the caller or else passed back into this routine via the
641 ** zPrior argument for reuse.
643 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
647 zResult = local_getline(zPrior, in);
649 zPrompt = isContinuation ? continuePrompt : mainPrompt;
650 #if SHELL_USE_LOCAL_GETLINE
651 printf("%s", zPrompt);
653 zResult = local_getline(zPrior, stdin);
656 zResult = shell_readline(zPrompt);
657 if( zResult && *zResult ) shell_add_history(zResult);
665 ** Return the value of a hexadecimal digit. Return -1 if the input
666 ** is not a hex digit.
668 static int hexDigitValue(char c){
669 if( c>='0' && c<='9' ) return c - '0';
670 if( c>='a' && c<='f' ) return c - 'a' + 10;
671 if( c>='A' && c<='F' ) return c - 'A' + 10;
676 ** Interpret zArg as an integer value, possibly with suffixes.
678 static sqlite3_int64 integerValue(const char *zArg){
680 static const struct { char *zSuffix; int iMult; } aMult[] = {
682 { "MiB", 1024*1024 },
683 { "GiB", 1024*1024*1024 },
686 { "GB", 1000000000 },
696 }else if( zArg[0]=='+' ){
699 if( zArg[0]=='0' && zArg[1]=='x' ){
702 while( (x = hexDigitValue(zArg[0]))>=0 ){
707 while( IsDigit(zArg[0]) ){
708 v = v*10 + zArg[0] - '0';
712 for(i=0; i<ArraySize(aMult); i++){
713 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
718 return isNeg? -v : v;
722 ** A variable length string to which one can append text.
724 typedef struct ShellText ShellText;
732 ** Initialize and destroy a ShellText object
734 static void initText(ShellText *p){
735 memset(p, 0, sizeof(*p));
737 static void freeText(ShellText *p){
742 /* zIn is either a pointer to a NULL-terminated string in memory obtained
743 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
744 ** added to zIn, and the result returned in memory obtained from malloc().
745 ** zIn, if it was not NULL, is freed.
747 ** If the third argument, quote, is not '\0', then it is used as a
748 ** quote character for zAppend.
750 static void appendText(ShellText *p, char const *zAppend, char quote){
753 int nAppend = strlen30(zAppend);
755 len = nAppend+p->n+1;
758 for(i=0; i<nAppend; i++){
759 if( zAppend[i]==quote ) len++;
763 if( p->n+len>=p->nAlloc ){
764 p->nAlloc = p->nAlloc*2 + len + 20;
765 p->z = realloc(p->z, p->nAlloc);
766 if( p->z==0 ) shell_out_of_memory();
770 char *zCsr = p->z+p->n;
772 for(i=0; i<nAppend; i++){
773 *zCsr++ = zAppend[i];
774 if( zAppend[i]==quote ) *zCsr++ = quote;
777 p->n = (int)(zCsr - p->z);
780 memcpy(p->z+p->n, zAppend, nAppend);
787 ** Attempt to determine if identifier zName needs to be quoted, either
788 ** because it contains non-alphanumeric characters, or because it is an
789 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
790 ** that quoting is required.
792 ** Return '"' if quoting is required. Return 0 if no quoting is required.
794 static char quoteChar(const char *zName){
796 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
797 for(i=0; zName[i]; i++){
798 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
800 return sqlite3_keyword_check(zName, i) ? '"' : 0;
804 ** Construct a fake object name and column list to describe the structure
805 ** of the view, virtual table, or table valued function zSchema.zName.
807 static char *shellFakeSchema(
808 sqlite3 *db, /* The database connection containing the vtab */
809 const char *zSchema, /* Schema of the database holding the vtab */
810 const char *zName /* The name of the virtual table */
812 sqlite3_stmt *pStmt = 0;
819 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
820 zSchema ? zSchema : "main", zName);
821 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
825 cQuote = quoteChar(zSchema);
826 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
827 appendText(&s, zSchema, cQuote);
828 appendText(&s, ".", 0);
830 cQuote = quoteChar(zName);
831 appendText(&s, zName, cQuote);
832 while( sqlite3_step(pStmt)==SQLITE_ROW ){
833 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
835 appendText(&s, zDiv, 0);
837 cQuote = quoteChar(zCol);
838 appendText(&s, zCol, cQuote);
840 appendText(&s, ")", 0);
841 sqlite3_finalize(pStmt);
850 ** SQL function: shell_module_schema(X)
852 ** Return a fake schema for the table-valued function or eponymous virtual
855 static void shellModuleSchema(
856 sqlite3_context *pCtx,
858 sqlite3_value **apVal
860 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
861 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
862 UNUSED_PARAMETER(nVal);
864 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
871 ** SQL function: shell_add_schema(S,X)
873 ** Add the schema name X to the CREATE statement in S and return the result.
876 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
881 ** CREATE UNIQUE INDEX
884 ** CREATE VIRTUAL TABLE
886 ** This UDF is used by the .schema command to insert the schema name of
887 ** attached databases into the middle of the sqlite_master.sql field.
889 static void shellAddSchemaName(
890 sqlite3_context *pCtx,
892 sqlite3_value **apVal
894 static const char *aPrefix[] = {
903 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
904 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
905 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
906 sqlite3 *db = sqlite3_context_db_handle(pCtx);
907 UNUSED_PARAMETER(nVal);
908 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
909 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
910 int n = strlen30(aPrefix[i]);
911 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
915 char cQuote = quoteChar(zSchema);
916 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
917 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
919 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
923 && aPrefix[i][0]=='V'
924 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
927 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
929 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
934 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
940 sqlite3_result_value(pCtx, apVal[0]);
944 ** The source code for several run-time loadable extensions is inserted
945 ** below by the ../tool/mkshellc.tcl script. Before processing that included
946 ** code, we need to override some macros to make the included program code
947 ** work here in the middle of this regular program.
949 #define SQLITE_EXTENSION_INIT1
950 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
952 #if defined(_WIN32) && defined(_MSC_VER)
953 /************************* Begin test_windirent.h ******************/
957 ** The author disclaims copyright to this source code. In place of
958 ** a legal notice, here is a blessing:
960 ** May you do good and not evil.
961 ** May you find forgiveness for yourself and forgive others.
962 ** May you share freely, never taking more than you give.
964 *************************************************************************
965 ** This file contains declarations for most of the opendir() family of
966 ** POSIX functions on Win32 using the MSVCRT.
969 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
970 #define SQLITE_WINDIRENT_H
973 ** We need several data types from the Windows SDK header.
976 #ifndef WIN32_LEAN_AND_MEAN
977 #define WIN32_LEAN_AND_MEAN
983 ** We need several support functions from the SQLite core.
988 ** We need several things from the ANSI and MSVCRT headers.
996 #include <sys/types.h>
997 #include <sys/stat.h>
1000 ** We may need several defines that should have been in "sys/stat.h".
1004 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1008 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1012 #define S_ISLNK(mode) (0)
1016 ** We may need to provide the "mode_t" type.
1019 #ifndef MODE_T_DEFINED
1020 #define MODE_T_DEFINED
1021 typedef unsigned short mode_t;
1025 ** We may need to provide the "ino_t" type.
1028 #ifndef INO_T_DEFINED
1029 #define INO_T_DEFINED
1030 typedef unsigned short ino_t;
1034 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1038 # ifdef FILENAME_MAX
1039 # define NAME_MAX (FILENAME_MAX)
1041 # define NAME_MAX (260)
1046 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1049 #ifndef NULL_INTPTR_T
1050 # define NULL_INTPTR_T ((intptr_t)(0))
1053 #ifndef BAD_INTPTR_T
1054 # define BAD_INTPTR_T ((intptr_t)(-1))
1058 ** We need to provide the necessary structures and related types.
1061 #ifndef DIRENT_DEFINED
1062 #define DIRENT_DEFINED
1063 typedef struct DIRENT DIRENT;
1064 typedef DIRENT *LPDIRENT;
1066 ino_t d_ino; /* Sequence number, do not use. */
1067 unsigned d_attributes; /* Win32 file attributes. */
1068 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1074 typedef struct DIR DIR;
1077 intptr_t d_handle; /* Value returned by "_findfirst". */
1078 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1079 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1084 ** Provide a macro, for use by the implementation, to determine if a
1085 ** particular directory entry should be skipped over when searching for
1086 ** the next directory entry that should be returned by the readdir() or
1087 ** readdir_r() functions.
1091 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1095 ** Provide the function prototype for the POSIX compatiable getenv()
1096 ** function. This function is not thread-safe.
1099 extern const char *windirent_getenv(const char *name);
1102 ** Finally, we can provide the function prototypes for the opendir(),
1103 ** readdir(), readdir_r(), and closedir() POSIX functions.
1106 extern LPDIR opendir(const char *dirname);
1107 extern LPDIRENT readdir(LPDIR dirp);
1108 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1109 extern INT closedir(LPDIR dirp);
1111 #endif /* defined(WIN32) && defined(_MSC_VER) */
1113 /************************* End test_windirent.h ********************/
1114 /************************* Begin test_windirent.c ******************/
1118 ** The author disclaims copyright to this source code. In place of
1119 ** a legal notice, here is a blessing:
1121 ** May you do good and not evil.
1122 ** May you find forgiveness for yourself and forgive others.
1123 ** May you share freely, never taking more than you give.
1125 *************************************************************************
1126 ** This file contains code to implement most of the opendir() family of
1127 ** POSIX functions on Win32 using the MSVCRT.
1130 #if defined(_WIN32) && defined(_MSC_VER)
1131 /* #include "test_windirent.h" */
1134 ** Implementation of the POSIX getenv() function using the Win32 API.
1135 ** This function is not thread-safe.
1137 const char *windirent_getenv(
1140 static char value[32768]; /* Maximum length, per MSDN */
1141 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1142 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1144 memset(value, 0, sizeof(value));
1145 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1146 if( dwRet==0 || dwRet>dwSize ){
1148 ** The function call to GetEnvironmentVariableA() failed -OR-
1149 ** the buffer is not large enough. Either way, return NULL.
1154 ** The function call to GetEnvironmentVariableA() succeeded
1155 ** -AND- the buffer contains the entire value.
1162 ** Implementation of the POSIX opendir() function using the MSVCRT.
1167 struct _finddata_t data;
1168 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1169 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1171 if( dirp==NULL ) return NULL;
1172 memset(dirp, 0, sizeof(DIR));
1174 /* TODO: Remove this if Unix-style root paths are not used. */
1175 if( sqlite3_stricmp(dirname, "/")==0 ){
1176 dirname = windirent_getenv("SystemDrive");
1179 memset(&data, 0, sizeof(struct _finddata_t));
1180 _snprintf(data.name, namesize, "%s\\*", dirname);
1181 dirp->d_handle = _findfirst(data.name, &data);
1183 if( dirp->d_handle==BAD_INTPTR_T ){
1188 /* TODO: Remove this block to allow hidden and/or system files. */
1189 if( is_filtered(data) ){
1192 memset(&data, 0, sizeof(struct _finddata_t));
1193 if( _findnext(dirp->d_handle, &data)==-1 ){
1198 /* TODO: Remove this block to allow hidden and/or system files. */
1199 if( is_filtered(data) ) goto next;
1202 dirp->d_first.d_attributes = data.attrib;
1203 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1204 dirp->d_first.d_name[NAME_MAX] = '\0';
1210 ** Implementation of the POSIX readdir() function using the MSVCRT.
1215 struct _finddata_t data;
1217 if( dirp==NULL ) return NULL;
1219 if( dirp->d_first.d_ino==0 ){
1220 dirp->d_first.d_ino++;
1221 dirp->d_next.d_ino++;
1223 return &dirp->d_first;
1228 memset(&data, 0, sizeof(struct _finddata_t));
1229 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1231 /* TODO: Remove this block to allow hidden and/or system files. */
1232 if( is_filtered(data) ) goto next;
1234 dirp->d_next.d_ino++;
1235 dirp->d_next.d_attributes = data.attrib;
1236 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1237 dirp->d_next.d_name[NAME_MAX] = '\0';
1239 return &dirp->d_next;
1243 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1250 struct _finddata_t data;
1252 if( dirp==NULL ) return EBADF;
1254 if( dirp->d_first.d_ino==0 ){
1255 dirp->d_first.d_ino++;
1256 dirp->d_next.d_ino++;
1258 entry->d_ino = dirp->d_first.d_ino;
1259 entry->d_attributes = dirp->d_first.d_attributes;
1260 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1261 entry->d_name[NAME_MAX] = '\0';
1269 memset(&data, 0, sizeof(struct _finddata_t));
1270 if( _findnext(dirp->d_handle, &data)==-1 ){
1275 /* TODO: Remove this block to allow hidden and/or system files. */
1276 if( is_filtered(data) ) goto next;
1278 entry->d_ino = (ino_t)-1; /* not available */
1279 entry->d_attributes = data.attrib;
1280 strncpy(entry->d_name, data.name, NAME_MAX);
1281 entry->d_name[NAME_MAX] = '\0';
1288 ** Implementation of the POSIX closedir() function using the MSVCRT.
1295 if( dirp==NULL ) return EINVAL;
1297 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1298 result = _findclose(dirp->d_handle);
1305 #endif /* defined(WIN32) && defined(_MSC_VER) */
1307 /************************* End test_windirent.c ********************/
1308 #define dirent DIRENT
1310 /************************* Begin ../ext/misc/shathree.c ******************/
1314 ** The author disclaims copyright to this source code. In place of
1315 ** a legal notice, here is a blessing:
1317 ** May you do good and not evil.
1318 ** May you find forgiveness for yourself and forgive others.
1319 ** May you share freely, never taking more than you give.
1321 ******************************************************************************
1323 ** This SQLite extension implements functions that compute SHA3 hashes.
1324 ** Two SQL functions are implemented:
1327 ** sha3_query(Y,SIZE)
1329 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1332 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1333 ** and returns a hash of their results.
1335 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1336 ** is used. If SIZE is included it must be one of the integers 224, 256,
1337 ** 384, or 512, to determine SHA3 hash variant that is computed.
1339 SQLITE_EXTENSION_INIT1
1343 /* typedef sqlite3_uint64 u64; */
1345 /******************************************************************************
1349 ** Macros to determine whether the machine is big or little endian,
1350 ** and whether or not that determination is run-time or compile-time.
1352 ** For best performance, an attempt is made to guess at the byte-order
1353 ** using C-preprocessor macros. If that is unsuccessful, or if
1354 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1357 #ifndef SHA3_BYTEORDER
1358 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1359 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1360 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1362 # define SHA3_BYTEORDER 1234
1363 # elif defined(sparc) || defined(__ppc__)
1364 # define SHA3_BYTEORDER 4321
1366 # define SHA3_BYTEORDER 0
1372 ** State structure for a SHA3 hash in progress
1374 typedef struct SHA3Context SHA3Context;
1375 struct SHA3Context {
1377 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1378 unsigned char x[1600]; /* ... or 1600 bytes */
1380 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1381 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1382 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1386 ** A single step of the Keccak mixing function for a 1600-bit state
1388 static void KeccakF1600Step(SHA3Context *p){
1390 u64 b0, b1, b2, b3, b4;
1391 u64 c0, c1, c2, c3, c4;
1392 u64 d0, d1, d2, d3, d4;
1393 static const u64 RC[] = {
1394 0x0000000000000001ULL, 0x0000000000008082ULL,
1395 0x800000000000808aULL, 0x8000000080008000ULL,
1396 0x000000000000808bULL, 0x0000000080000001ULL,
1397 0x8000000080008081ULL, 0x8000000000008009ULL,
1398 0x000000000000008aULL, 0x0000000000000088ULL,
1399 0x0000000080008009ULL, 0x000000008000000aULL,
1400 0x000000008000808bULL, 0x800000000000008bULL,
1401 0x8000000000008089ULL, 0x8000000000008003ULL,
1402 0x8000000000008002ULL, 0x8000000000000080ULL,
1403 0x000000000000800aULL, 0x800000008000000aULL,
1404 0x8000000080008081ULL, 0x8000000000008080ULL,
1405 0x0000000080000001ULL, 0x8000000080008008ULL
1407 # define a00 (p->u.s[0])
1408 # define a01 (p->u.s[1])
1409 # define a02 (p->u.s[2])
1410 # define a03 (p->u.s[3])
1411 # define a04 (p->u.s[4])
1412 # define a10 (p->u.s[5])
1413 # define a11 (p->u.s[6])
1414 # define a12 (p->u.s[7])
1415 # define a13 (p->u.s[8])
1416 # define a14 (p->u.s[9])
1417 # define a20 (p->u.s[10])
1418 # define a21 (p->u.s[11])
1419 # define a22 (p->u.s[12])
1420 # define a23 (p->u.s[13])
1421 # define a24 (p->u.s[14])
1422 # define a30 (p->u.s[15])
1423 # define a31 (p->u.s[16])
1424 # define a32 (p->u.s[17])
1425 # define a33 (p->u.s[18])
1426 # define a34 (p->u.s[19])
1427 # define a40 (p->u.s[20])
1428 # define a41 (p->u.s[21])
1429 # define a42 (p->u.s[22])
1430 # define a43 (p->u.s[23])
1431 # define a44 (p->u.s[24])
1432 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1434 for(i=0; i<24; i+=4){
1435 c0 = a00^a10^a20^a30^a40;
1436 c1 = a01^a11^a21^a31^a41;
1437 c2 = a02^a12^a22^a32^a42;
1438 c3 = a03^a13^a23^a33^a43;
1439 c4 = a04^a14^a24^a34^a44;
1440 d0 = c4^ROL64(c1, 1);
1441 d1 = c0^ROL64(c2, 1);
1442 d2 = c1^ROL64(c3, 1);
1443 d3 = c2^ROL64(c4, 1);
1444 d4 = c3^ROL64(c0, 1);
1447 b1 = ROL64((a11^d1), 44);
1448 b2 = ROL64((a22^d2), 43);
1449 b3 = ROL64((a33^d3), 21);
1450 b4 = ROL64((a44^d4), 14);
1451 a00 = b0 ^((~b1)& b2 );
1453 a11 = b1 ^((~b2)& b3 );
1454 a22 = b2 ^((~b3)& b4 );
1455 a33 = b3 ^((~b4)& b0 );
1456 a44 = b4 ^((~b0)& b1 );
1458 b2 = ROL64((a20^d0), 3);
1459 b3 = ROL64((a31^d1), 45);
1460 b4 = ROL64((a42^d2), 61);
1461 b0 = ROL64((a03^d3), 28);
1462 b1 = ROL64((a14^d4), 20);
1463 a20 = b0 ^((~b1)& b2 );
1464 a31 = b1 ^((~b2)& b3 );
1465 a42 = b2 ^((~b3)& b4 );
1466 a03 = b3 ^((~b4)& b0 );
1467 a14 = b4 ^((~b0)& b1 );
1469 b4 = ROL64((a40^d0), 18);
1470 b0 = ROL64((a01^d1), 1);
1471 b1 = ROL64((a12^d2), 6);
1472 b2 = ROL64((a23^d3), 25);
1473 b3 = ROL64((a34^d4), 8);
1474 a40 = b0 ^((~b1)& b2 );
1475 a01 = b1 ^((~b2)& b3 );
1476 a12 = b2 ^((~b3)& b4 );
1477 a23 = b3 ^((~b4)& b0 );
1478 a34 = b4 ^((~b0)& b1 );
1480 b1 = ROL64((a10^d0), 36);
1481 b2 = ROL64((a21^d1), 10);
1482 b3 = ROL64((a32^d2), 15);
1483 b4 = ROL64((a43^d3), 56);
1484 b0 = ROL64((a04^d4), 27);
1485 a10 = b0 ^((~b1)& b2 );
1486 a21 = b1 ^((~b2)& b3 );
1487 a32 = b2 ^((~b3)& b4 );
1488 a43 = b3 ^((~b4)& b0 );
1489 a04 = b4 ^((~b0)& b1 );
1491 b3 = ROL64((a30^d0), 41);
1492 b4 = ROL64((a41^d1), 2);
1493 b0 = ROL64((a02^d2), 62);
1494 b1 = ROL64((a13^d3), 55);
1495 b2 = ROL64((a24^d4), 39);
1496 a30 = b0 ^((~b1)& b2 );
1497 a41 = b1 ^((~b2)& b3 );
1498 a02 = b2 ^((~b3)& b4 );
1499 a13 = b3 ^((~b4)& b0 );
1500 a24 = b4 ^((~b0)& b1 );
1502 c0 = a00^a20^a40^a10^a30;
1503 c1 = a11^a31^a01^a21^a41;
1504 c2 = a22^a42^a12^a32^a02;
1505 c3 = a33^a03^a23^a43^a13;
1506 c4 = a44^a14^a34^a04^a24;
1507 d0 = c4^ROL64(c1, 1);
1508 d1 = c0^ROL64(c2, 1);
1509 d2 = c1^ROL64(c3, 1);
1510 d3 = c2^ROL64(c4, 1);
1511 d4 = c3^ROL64(c0, 1);
1514 b1 = ROL64((a31^d1), 44);
1515 b2 = ROL64((a12^d2), 43);
1516 b3 = ROL64((a43^d3), 21);
1517 b4 = ROL64((a24^d4), 14);
1518 a00 = b0 ^((~b1)& b2 );
1520 a31 = b1 ^((~b2)& b3 );
1521 a12 = b2 ^((~b3)& b4 );
1522 a43 = b3 ^((~b4)& b0 );
1523 a24 = b4 ^((~b0)& b1 );
1525 b2 = ROL64((a40^d0), 3);
1526 b3 = ROL64((a21^d1), 45);
1527 b4 = ROL64((a02^d2), 61);
1528 b0 = ROL64((a33^d3), 28);
1529 b1 = ROL64((a14^d4), 20);
1530 a40 = b0 ^((~b1)& b2 );
1531 a21 = b1 ^((~b2)& b3 );
1532 a02 = b2 ^((~b3)& b4 );
1533 a33 = b3 ^((~b4)& b0 );
1534 a14 = b4 ^((~b0)& b1 );
1536 b4 = ROL64((a30^d0), 18);
1537 b0 = ROL64((a11^d1), 1);
1538 b1 = ROL64((a42^d2), 6);
1539 b2 = ROL64((a23^d3), 25);
1540 b3 = ROL64((a04^d4), 8);
1541 a30 = b0 ^((~b1)& b2 );
1542 a11 = b1 ^((~b2)& b3 );
1543 a42 = b2 ^((~b3)& b4 );
1544 a23 = b3 ^((~b4)& b0 );
1545 a04 = b4 ^((~b0)& b1 );
1547 b1 = ROL64((a20^d0), 36);
1548 b2 = ROL64((a01^d1), 10);
1549 b3 = ROL64((a32^d2), 15);
1550 b4 = ROL64((a13^d3), 56);
1551 b0 = ROL64((a44^d4), 27);
1552 a20 = b0 ^((~b1)& b2 );
1553 a01 = b1 ^((~b2)& b3 );
1554 a32 = b2 ^((~b3)& b4 );
1555 a13 = b3 ^((~b4)& b0 );
1556 a44 = b4 ^((~b0)& b1 );
1558 b3 = ROL64((a10^d0), 41);
1559 b4 = ROL64((a41^d1), 2);
1560 b0 = ROL64((a22^d2), 62);
1561 b1 = ROL64((a03^d3), 55);
1562 b2 = ROL64((a34^d4), 39);
1563 a10 = b0 ^((~b1)& b2 );
1564 a41 = b1 ^((~b2)& b3 );
1565 a22 = b2 ^((~b3)& b4 );
1566 a03 = b3 ^((~b4)& b0 );
1567 a34 = b4 ^((~b0)& b1 );
1569 c0 = a00^a40^a30^a20^a10;
1570 c1 = a31^a21^a11^a01^a41;
1571 c2 = a12^a02^a42^a32^a22;
1572 c3 = a43^a33^a23^a13^a03;
1573 c4 = a24^a14^a04^a44^a34;
1574 d0 = c4^ROL64(c1, 1);
1575 d1 = c0^ROL64(c2, 1);
1576 d2 = c1^ROL64(c3, 1);
1577 d3 = c2^ROL64(c4, 1);
1578 d4 = c3^ROL64(c0, 1);
1581 b1 = ROL64((a21^d1), 44);
1582 b2 = ROL64((a42^d2), 43);
1583 b3 = ROL64((a13^d3), 21);
1584 b4 = ROL64((a34^d4), 14);
1585 a00 = b0 ^((~b1)& b2 );
1587 a21 = b1 ^((~b2)& b3 );
1588 a42 = b2 ^((~b3)& b4 );
1589 a13 = b3 ^((~b4)& b0 );
1590 a34 = b4 ^((~b0)& b1 );
1592 b2 = ROL64((a30^d0), 3);
1593 b3 = ROL64((a01^d1), 45);
1594 b4 = ROL64((a22^d2), 61);
1595 b0 = ROL64((a43^d3), 28);
1596 b1 = ROL64((a14^d4), 20);
1597 a30 = b0 ^((~b1)& b2 );
1598 a01 = b1 ^((~b2)& b3 );
1599 a22 = b2 ^((~b3)& b4 );
1600 a43 = b3 ^((~b4)& b0 );
1601 a14 = b4 ^((~b0)& b1 );
1603 b4 = ROL64((a10^d0), 18);
1604 b0 = ROL64((a31^d1), 1);
1605 b1 = ROL64((a02^d2), 6);
1606 b2 = ROL64((a23^d3), 25);
1607 b3 = ROL64((a44^d4), 8);
1608 a10 = b0 ^((~b1)& b2 );
1609 a31 = b1 ^((~b2)& b3 );
1610 a02 = b2 ^((~b3)& b4 );
1611 a23 = b3 ^((~b4)& b0 );
1612 a44 = b4 ^((~b0)& b1 );
1614 b1 = ROL64((a40^d0), 36);
1615 b2 = ROL64((a11^d1), 10);
1616 b3 = ROL64((a32^d2), 15);
1617 b4 = ROL64((a03^d3), 56);
1618 b0 = ROL64((a24^d4), 27);
1619 a40 = b0 ^((~b1)& b2 );
1620 a11 = b1 ^((~b2)& b3 );
1621 a32 = b2 ^((~b3)& b4 );
1622 a03 = b3 ^((~b4)& b0 );
1623 a24 = b4 ^((~b0)& b1 );
1625 b3 = ROL64((a20^d0), 41);
1626 b4 = ROL64((a41^d1), 2);
1627 b0 = ROL64((a12^d2), 62);
1628 b1 = ROL64((a33^d3), 55);
1629 b2 = ROL64((a04^d4), 39);
1630 a20 = b0 ^((~b1)& b2 );
1631 a41 = b1 ^((~b2)& b3 );
1632 a12 = b2 ^((~b3)& b4 );
1633 a33 = b3 ^((~b4)& b0 );
1634 a04 = b4 ^((~b0)& b1 );
1636 c0 = a00^a30^a10^a40^a20;
1637 c1 = a21^a01^a31^a11^a41;
1638 c2 = a42^a22^a02^a32^a12;
1639 c3 = a13^a43^a23^a03^a33;
1640 c4 = a34^a14^a44^a24^a04;
1641 d0 = c4^ROL64(c1, 1);
1642 d1 = c0^ROL64(c2, 1);
1643 d2 = c1^ROL64(c3, 1);
1644 d3 = c2^ROL64(c4, 1);
1645 d4 = c3^ROL64(c0, 1);
1648 b1 = ROL64((a01^d1), 44);
1649 b2 = ROL64((a02^d2), 43);
1650 b3 = ROL64((a03^d3), 21);
1651 b4 = ROL64((a04^d4), 14);
1652 a00 = b0 ^((~b1)& b2 );
1654 a01 = b1 ^((~b2)& b3 );
1655 a02 = b2 ^((~b3)& b4 );
1656 a03 = b3 ^((~b4)& b0 );
1657 a04 = b4 ^((~b0)& b1 );
1659 b2 = ROL64((a10^d0), 3);
1660 b3 = ROL64((a11^d1), 45);
1661 b4 = ROL64((a12^d2), 61);
1662 b0 = ROL64((a13^d3), 28);
1663 b1 = ROL64((a14^d4), 20);
1664 a10 = b0 ^((~b1)& b2 );
1665 a11 = b1 ^((~b2)& b3 );
1666 a12 = b2 ^((~b3)& b4 );
1667 a13 = b3 ^((~b4)& b0 );
1668 a14 = b4 ^((~b0)& b1 );
1670 b4 = ROL64((a20^d0), 18);
1671 b0 = ROL64((a21^d1), 1);
1672 b1 = ROL64((a22^d2), 6);
1673 b2 = ROL64((a23^d3), 25);
1674 b3 = ROL64((a24^d4), 8);
1675 a20 = b0 ^((~b1)& b2 );
1676 a21 = b1 ^((~b2)& b3 );
1677 a22 = b2 ^((~b3)& b4 );
1678 a23 = b3 ^((~b4)& b0 );
1679 a24 = b4 ^((~b0)& b1 );
1681 b1 = ROL64((a30^d0), 36);
1682 b2 = ROL64((a31^d1), 10);
1683 b3 = ROL64((a32^d2), 15);
1684 b4 = ROL64((a33^d3), 56);
1685 b0 = ROL64((a34^d4), 27);
1686 a30 = b0 ^((~b1)& b2 );
1687 a31 = b1 ^((~b2)& b3 );
1688 a32 = b2 ^((~b3)& b4 );
1689 a33 = b3 ^((~b4)& b0 );
1690 a34 = b4 ^((~b0)& b1 );
1692 b3 = ROL64((a40^d0), 41);
1693 b4 = ROL64((a41^d1), 2);
1694 b0 = ROL64((a42^d2), 62);
1695 b1 = ROL64((a43^d3), 55);
1696 b2 = ROL64((a44^d4), 39);
1697 a40 = b0 ^((~b1)& b2 );
1698 a41 = b1 ^((~b2)& b3 );
1699 a42 = b2 ^((~b3)& b4 );
1700 a43 = b3 ^((~b4)& b0 );
1701 a44 = b4 ^((~b0)& b1 );
1706 ** Initialize a new hash. iSize determines the size of the hash
1707 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1708 ** can be zero to use the default hash size of 256 bits.
1710 static void SHA3Init(SHA3Context *p, int iSize){
1711 memset(p, 0, sizeof(*p));
1712 if( iSize>=128 && iSize<=512 ){
1713 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1715 p->nRate = (1600 - 2*256)/8;
1717 #if SHA3_BYTEORDER==1234
1718 /* Known to be little-endian at compile-time. No-op */
1719 #elif SHA3_BYTEORDER==4321
1720 p->ixMask = 7; /* Big-endian */
1723 static unsigned int one = 1;
1724 if( 1==*(unsigned char*)&one ){
1725 /* Little endian. No byte swapping. */
1728 /* Big endian. Byte swap. */
1736 ** Make consecutive calls to the SHA3Update function to add new content
1739 static void SHA3Update(
1741 const unsigned char *aData,
1745 #if SHA3_BYTEORDER==1234
1746 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1747 for(; i+7<nData; i+=8){
1748 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1750 if( p->nLoaded>=p->nRate ){
1757 for(; i<nData; i++){
1758 #if SHA3_BYTEORDER==1234
1759 p->u.x[p->nLoaded] ^= aData[i];
1760 #elif SHA3_BYTEORDER==4321
1761 p->u.x[p->nLoaded^0x07] ^= aData[i];
1763 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1766 if( p->nLoaded==p->nRate ){
1774 ** After all content has been added, invoke SHA3Final() to compute
1775 ** the final hash. The function returns a pointer to the binary
1778 static unsigned char *SHA3Final(SHA3Context *p){
1780 if( p->nLoaded==p->nRate-1 ){
1781 const unsigned char c1 = 0x86;
1782 SHA3Update(p, &c1, 1);
1784 const unsigned char c2 = 0x06;
1785 const unsigned char c3 = 0x80;
1786 SHA3Update(p, &c2, 1);
1787 p->nLoaded = p->nRate - 1;
1788 SHA3Update(p, &c3, 1);
1790 for(i=0; i<p->nRate; i++){
1791 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1793 return &p->u.x[p->nRate];
1795 /* End of the hashing logic
1796 *****************************************************************************/
1799 ** Implementation of the sha3(X,SIZE) function.
1801 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1802 ** size is 256. If X is a BLOB, it is hashed as is.
1803 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1804 ** and the string is hashed without the trailing 0x00 terminator. The hash
1805 ** of a NULL value is NULL.
1807 static void sha3Func(
1808 sqlite3_context *context,
1810 sqlite3_value **argv
1813 int eType = sqlite3_value_type(argv[0]);
1814 int nByte = sqlite3_value_bytes(argv[0]);
1819 iSize = sqlite3_value_int(argv[1]);
1820 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1821 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1826 if( eType==SQLITE_NULL ) return;
1827 SHA3Init(&cx, iSize);
1828 if( eType==SQLITE_BLOB ){
1829 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1831 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1833 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1836 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1837 ** of 50 bytes and add it to the hash.
1839 static void hash_step_vformat(
1840 SHA3Context *p, /* Add content to this context */
1841 const char *zFormat,
1847 va_start(ap, zFormat);
1848 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1850 n = (int)strlen(zBuf);
1851 SHA3Update(p, (unsigned char*)zBuf, n);
1855 ** Implementation of the sha3_query(SQL,SIZE) function.
1857 ** This function compiles and runs the SQL statement(s) given in the
1858 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1861 ** The format of the byte stream that is hashed is summarized as follows:
1871 ** <sql> is the original SQL text for each statement run and <n> is
1872 ** the size of that text. The SQL text is UTF-8. A single R character
1873 ** occurs before the start of each row. N means a NULL value.
1874 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1875 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1876 ** B means blobs of <size> bytes. T means text rendered as <size>
1877 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1880 ** For each SQL statement in the X input, there is one S segment. Each
1881 ** S segment is followed by zero or more R segments, one for each row in the
1882 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1883 ** one for each column in the result set. Segments are concatentated directly
1884 ** with no delimiters of any kind.
1886 static void sha3QueryFunc(
1887 sqlite3_context *context,
1889 sqlite3_value **argv
1891 sqlite3 *db = sqlite3_context_db_handle(context);
1892 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1893 sqlite3_stmt *pStmt = 0;
1894 int nCol; /* Number of columns in the result set */
1895 int i; /* Loop counter */
1905 iSize = sqlite3_value_int(argv[1]);
1906 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1907 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1912 if( zSql==0 ) return;
1913 SHA3Init(&cx, iSize);
1915 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1917 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1918 zSql, sqlite3_errmsg(db));
1919 sqlite3_finalize(pStmt);
1920 sqlite3_result_error(context, zMsg, -1);
1924 if( !sqlite3_stmt_readonly(pStmt) ){
1925 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1926 sqlite3_finalize(pStmt);
1927 sqlite3_result_error(context, zMsg, -1);
1931 nCol = sqlite3_column_count(pStmt);
1932 z = sqlite3_sql(pStmt);
1934 hash_step_vformat(&cx,"S%d:",n);
1935 SHA3Update(&cx,(unsigned char*)z,n);
1937 /* Compute a hash over the result of the query */
1938 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1939 SHA3Update(&cx,(const unsigned char*)"R",1);
1940 for(i=0; i<nCol; i++){
1941 switch( sqlite3_column_type(pStmt,i) ){
1943 SHA3Update(&cx, (const unsigned char*)"N",1);
1946 case SQLITE_INTEGER: {
1950 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1952 for(j=8; j>=1; j--){
1957 SHA3Update(&cx, x, 9);
1960 case SQLITE_FLOAT: {
1964 double r = sqlite3_column_double(pStmt,i);
1966 for(j=8; j>=1; j--){
1971 SHA3Update(&cx,x,9);
1975 int n2 = sqlite3_column_bytes(pStmt, i);
1976 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1977 hash_step_vformat(&cx,"T%d:",n2);
1978 SHA3Update(&cx, z2, n2);
1982 int n2 = sqlite3_column_bytes(pStmt, i);
1983 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
1984 hash_step_vformat(&cx,"B%d:",n2);
1985 SHA3Update(&cx, z2, n2);
1991 sqlite3_finalize(pStmt);
1993 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2000 int sqlite3_shathree_init(
2003 const sqlite3_api_routines *pApi
2006 SQLITE_EXTENSION_INIT2(pApi);
2007 (void)pzErrMsg; /* Unused parameter */
2008 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2010 if( rc==SQLITE_OK ){
2011 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2014 if( rc==SQLITE_OK ){
2015 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2016 sha3QueryFunc, 0, 0);
2018 if( rc==SQLITE_OK ){
2019 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2020 sha3QueryFunc, 0, 0);
2025 /************************* End ../ext/misc/shathree.c ********************/
2026 /************************* Begin ../ext/misc/fileio.c ******************/
2030 ** The author disclaims copyright to this source code. In place of
2031 ** a legal notice, here is a blessing:
2033 ** May you do good and not evil.
2034 ** May you find forgiveness for yourself and forgive others.
2035 ** May you share freely, never taking more than you give.
2037 ******************************************************************************
2039 ** This SQLite extension implements SQL functions readfile() and
2040 ** writefile(), and eponymous virtual type "fsdir".
2042 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2044 ** If neither of the optional arguments is present, then this UDF
2045 ** function writes blob DATA to file FILE. If successful, the number
2046 ** of bytes written is returned. If an error occurs, NULL is returned.
2048 ** If the first option argument - MODE - is present, then it must
2049 ** be passed an integer value that corresponds to a POSIX mode
2050 ** value (file type + permissions, as returned in the stat.st_mode
2051 ** field by the stat() system call). Three types of files may
2052 ** be written/created:
2054 ** regular files: (mode & 0170000)==0100000
2055 ** symbolic links: (mode & 0170000)==0120000
2056 ** directories: (mode & 0170000)==0040000
2058 ** For a directory, the DATA is ignored. For a symbolic link, it is
2059 ** interpreted as text and used as the target of the link. For a
2060 ** regular file, it is interpreted as a blob and written into the
2061 ** named file. Regardless of the type of file, its permissions are
2062 ** set to (mode & 0777) before returning.
2064 ** If the optional MTIME argument is present, then it is interpreted
2065 ** as an integer - the number of seconds since the unix epoch. The
2066 ** modification-time of the target file is set to this value before
2069 ** If three or more arguments are passed to this function and an
2070 ** error is encountered, an exception is raised.
2074 ** Read and return the contents of file FILE (type blob) from disk.
2080 ** SELECT * FROM fsdir($path [, $dir]);
2082 ** Parameter $path is an absolute or relative pathname. If the file that it
2083 ** refers to does not exist, it is an error. If the path refers to a regular
2084 ** file or symbolic link, it returns a single row. Or, if the path refers
2085 ** to a directory, it returns one row for the directory, and one row for each
2086 ** file within the hierarchy rooted at $path.
2088 ** Each row has the following columns:
2090 ** name: Path to file or directory (text value).
2091 ** mode: Value of stat.st_mode for directory entry (an integer).
2092 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2093 ** data: For a regular file, a blob containing the file data. For a
2094 ** symlink, a text value containing the text of the link. For a
2097 ** If a non-NULL value is specified for the optional $dir parameter and
2098 ** $path is a relative path, then $path is interpreted relative to $dir.
2099 ** And the paths returned in the "name" column of the table are also
2100 ** relative to directory $dir.
2102 SQLITE_EXTENSION_INIT1
2107 #include <sys/types.h>
2108 #include <sys/stat.h>
2110 #if !defined(_WIN32) && !defined(WIN32)
2111 # include <unistd.h>
2112 # include <dirent.h>
2114 # include <sys/time.h>
2116 # include "windows.h"
2118 # include <direct.h>
2119 /* # include "test_windirent.h" */
2120 # define dirent DIRENT
2122 # define chmod _chmod
2127 # define mkdir(path,mode) _mkdir(path)
2128 # define lstat(path,buf) stat(path,buf)
2135 ** Structure of the fsdir() table-valued function
2138 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2139 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2140 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2141 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2142 #define FSDIR_COLUMN_DATA 3 /* File content */
2143 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2144 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2148 ** Set the result stored by context ctx to a blob containing the
2149 ** contents of file zName. Or, leave the result unchanged (NULL)
2150 ** if the file does not exist or is unreadable.
2152 ** If the file exceeds the SQLite blob size limit, through an
2153 ** SQLITE_TOOBIG error.
2155 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2158 static void readFileContents(sqlite3_context *ctx, const char *zName){
2165 in = fopen(zName, "rb");
2167 /* File does not exist or is unreadable. Leave the result set to NULL. */
2170 fseek(in, 0, SEEK_END);
2173 db = sqlite3_context_db_handle(ctx);
2174 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2176 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2180 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2182 sqlite3_result_error_nomem(ctx);
2186 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2187 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2189 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2196 ** Implementation of the "readfile(X)" SQL function. The entire content
2197 ** of the file named X is read and returned as a BLOB. NULL is returned
2198 ** if the file does not exist or is unreadable.
2200 static void readfileFunc(
2201 sqlite3_context *context,
2203 sqlite3_value **argv
2206 (void)(argc); /* Unused parameter */
2207 zName = (const char*)sqlite3_value_text(argv[0]);
2208 if( zName==0 ) return;
2209 readFileContents(context, zName);
2213 ** Set the error message contained in context ctx to the results of
2214 ** vprintf(zFmt, ...).
2216 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2220 zMsg = sqlite3_vmprintf(zFmt, ap);
2221 sqlite3_result_error(ctx, zMsg, -1);
2228 ** This function is designed to convert a Win32 FILETIME structure into the
2229 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2231 static sqlite3_uint64 fileTimeToUnixTime(
2232 LPFILETIME pFileTime
2234 SYSTEMTIME epochSystemTime;
2235 ULARGE_INTEGER epochIntervals;
2236 FILETIME epochFileTime;
2237 ULARGE_INTEGER fileIntervals;
2239 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2240 epochSystemTime.wYear = 1970;
2241 epochSystemTime.wMonth = 1;
2242 epochSystemTime.wDay = 1;
2243 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2244 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2245 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2247 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2248 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2250 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2254 ** This function attempts to normalize the time values found in the stat()
2255 ** buffer to UTC. This is necessary on Win32, where the runtime library
2256 ** appears to return these values as local times.
2258 static void statTimesToUtc(
2260 struct stat *pStatBuf
2263 WIN32_FIND_DATAW fd;
2264 LPWSTR zUnicodeName;
2265 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2266 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2268 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2269 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2270 if( hFindFile!=NULL ){
2271 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2272 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2273 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2274 FindClose(hFindFile);
2276 sqlite3_free(zUnicodeName);
2282 ** This function is used in place of stat(). On Windows, special handling
2283 ** is required in order for the included time to be returned as UTC. On all
2284 ** other systems, this function simply calls stat().
2286 static int fileStat(
2288 struct stat *pStatBuf
2291 int rc = stat(zPath, pStatBuf);
2292 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2295 return stat(zPath, pStatBuf);
2300 ** This function is used in place of lstat(). On Windows, special handling
2301 ** is required in order for the included time to be returned as UTC. On all
2302 ** other systems, this function simply calls lstat().
2304 static int fileLinkStat(
2306 struct stat *pStatBuf
2309 int rc = lstat(zPath, pStatBuf);
2310 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2313 return lstat(zPath, pStatBuf);
2318 ** Argument zFile is the name of a file that will be created and/or written
2319 ** by SQL function writefile(). This function ensures that the directory
2320 ** zFile will be written to exists, creating it if required. The permissions
2321 ** for any path components created by this function are set in accordance
2322 ** with the current umask.
2324 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2325 ** SQLITE_OK is returned if the directory is successfully created, or
2326 ** SQLITE_ERROR otherwise.
2328 static int makeDirectory(
2331 char *zCopy = sqlite3_mprintf("%s", zFile);
2337 int nCopy = (int)strlen(zCopy);
2340 while( rc==SQLITE_OK ){
2344 for(; zCopy[i]!='/' && i<nCopy; i++);
2345 if( i==nCopy ) break;
2348 rc2 = fileStat(zCopy, &sStat);
2350 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2352 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2358 sqlite3_free(zCopy);
2365 ** This function does the work for the writefile() UDF. Refer to
2366 ** header comments at the top of this file for details.
2368 static int writeFile(
2369 sqlite3_context *pCtx, /* Context to return bytes written in */
2370 const char *zFile, /* File to write */
2371 sqlite3_value *pData, /* Data to write */
2372 mode_t mode, /* MODE parameter passed to writefile() */
2373 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2375 #if !defined(_WIN32) && !defined(WIN32)
2376 if( S_ISLNK(mode) ){
2377 const char *zTo = (const char*)sqlite3_value_text(pData);
2378 if( symlink(zTo, zFile)<0 ) return 1;
2382 if( S_ISDIR(mode) ){
2383 if( mkdir(zFile, mode) ){
2384 /* The mkdir() call to create the directory failed. This might not
2385 ** be an error though - if there is already a directory at the same
2386 ** path and either the permissions already match or can be changed
2387 ** to do so using chmod(), it is not an error. */
2390 || 0!=fileStat(zFile, &sStat)
2391 || !S_ISDIR(sStat.st_mode)
2392 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2398 sqlite3_int64 nWrite = 0;
2401 FILE *out = fopen(zFile, "wb");
2402 if( out==0 ) return 1;
2403 z = (const char*)sqlite3_value_blob(pData);
2405 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2406 nWrite = sqlite3_value_bytes(pData);
2412 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2416 sqlite3_result_int64(pCtx, nWrite);
2423 FILETIME lastAccess;
2425 SYSTEMTIME currentTime;
2428 LPWSTR zUnicodeName;
2429 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2431 GetSystemTime(¤tTime);
2432 SystemTimeToFileTime(¤tTime, &lastAccess);
2433 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2434 lastWrite.dwLowDateTime = (DWORD)intervals;
2435 lastWrite.dwHighDateTime = intervals >> 32;
2436 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2437 if( zUnicodeName==0 ){
2440 hFile = CreateFileW(
2441 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2442 FILE_FLAG_BACKUP_SEMANTICS, NULL
2444 sqlite3_free(zUnicodeName);
2445 if( hFile!=INVALID_HANDLE_VALUE ){
2446 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2452 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2454 struct timespec times[2];
2455 times[0].tv_nsec = times[1].tv_nsec = 0;
2456 times[0].tv_sec = time(0);
2457 times[1].tv_sec = mtime;
2458 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2463 struct timeval times[2];
2464 times[0].tv_usec = times[1].tv_usec = 0;
2465 times[0].tv_sec = time(0);
2466 times[1].tv_sec = mtime;
2467 if( utimes(zFile, times) ){
2477 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2478 ** Refer to header comments at the top of this file for details.
2480 static void writefileFunc(
2481 sqlite3_context *context,
2483 sqlite3_value **argv
2488 sqlite3_int64 mtime = -1;
2490 if( argc<2 || argc>4 ){
2491 sqlite3_result_error(context,
2492 "wrong number of arguments to function writefile()", -1
2497 zFile = (const char*)sqlite3_value_text(argv[0]);
2498 if( zFile==0 ) return;
2500 mode = (mode_t)sqlite3_value_int(argv[2]);
2503 mtime = sqlite3_value_int64(argv[3]);
2506 res = writeFile(context, zFile, argv[1], mode, mtime);
2507 if( res==1 && errno==ENOENT ){
2508 if( makeDirectory(zFile)==SQLITE_OK ){
2509 res = writeFile(context, zFile, argv[1], mode, mtime);
2513 if( argc>2 && res!=0 ){
2514 if( S_ISLNK(mode) ){
2515 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2516 }else if( S_ISDIR(mode) ){
2517 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2519 ctxErrorMsg(context, "failed to write file: %s", zFile);
2525 ** SQL function: lsmode(MODE)
2527 ** Given a numberic st_mode from stat(), convert it into a human-readable
2528 ** text string in the style of "ls -l".
2530 static void lsModeFunc(
2531 sqlite3_context *context,
2533 sqlite3_value **argv
2536 int iMode = sqlite3_value_int(argv[0]);
2539 if( S_ISLNK(iMode) ){
2541 }else if( S_ISREG(iMode) ){
2543 }else if( S_ISDIR(iMode) ){
2549 int m = (iMode >> ((2-i)*3));
2550 char *a = &z[1 + i*3];
2551 a[0] = (m & 0x4) ? 'r' : '-';
2552 a[1] = (m & 0x2) ? 'w' : '-';
2553 a[2] = (m & 0x1) ? 'x' : '-';
2556 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2559 #ifndef SQLITE_OMIT_VIRTUALTABLE
2562 ** Cursor type for recursively iterating through a directory structure.
2564 typedef struct fsdir_cursor fsdir_cursor;
2565 typedef struct FsdirLevel FsdirLevel;
2568 DIR *pDir; /* From opendir() */
2569 char *zDir; /* Name of directory (nul-terminated) */
2572 struct fsdir_cursor {
2573 sqlite3_vtab_cursor base; /* Base class - must be first */
2575 int nLvl; /* Number of entries in aLvl[] array */
2576 int iLvl; /* Index of current entry */
2577 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2582 struct stat sStat; /* Current lstat() results */
2583 char *zPath; /* Path to current entry */
2584 sqlite3_int64 iRowid; /* Current rowid */
2587 typedef struct fsdir_tab fsdir_tab;
2589 sqlite3_vtab base; /* Base class - must be first */
2593 ** Construct a new fsdir virtual table object.
2595 static int fsdirConnect(
2598 int argc, const char *const*argv,
2599 sqlite3_vtab **ppVtab,
2602 fsdir_tab *pNew = 0;
2608 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2609 if( rc==SQLITE_OK ){
2610 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2611 if( pNew==0 ) return SQLITE_NOMEM;
2612 memset(pNew, 0, sizeof(*pNew));
2614 *ppVtab = (sqlite3_vtab*)pNew;
2619 ** This method is the destructor for fsdir vtab objects.
2621 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2622 sqlite3_free(pVtab);
2627 ** Constructor for a new fsdir_cursor object.
2629 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2632 pCur = sqlite3_malloc( sizeof(*pCur) );
2633 if( pCur==0 ) return SQLITE_NOMEM;
2634 memset(pCur, 0, sizeof(*pCur));
2636 *ppCursor = &pCur->base;
2641 ** Reset a cursor back to the state it was in when first returned
2644 static void fsdirResetCursor(fsdir_cursor *pCur){
2646 for(i=0; i<=pCur->iLvl; i++){
2647 FsdirLevel *pLvl = &pCur->aLvl[i];
2648 if( pLvl->pDir ) closedir(pLvl->pDir);
2649 sqlite3_free(pLvl->zDir);
2651 sqlite3_free(pCur->zPath);
2652 sqlite3_free(pCur->aLvl);
2663 ** Destructor for an fsdir_cursor.
2665 static int fsdirClose(sqlite3_vtab_cursor *cur){
2666 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2668 fsdirResetCursor(pCur);
2674 ** Set the error message for the virtual table associated with cursor
2675 ** pCur to the results of vprintf(zFmt, ...).
2677 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2680 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2686 ** Advance an fsdir_cursor to its next row of output.
2688 static int fsdirNext(sqlite3_vtab_cursor *cur){
2689 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2690 mode_t m = pCur->sStat.st_mode;
2694 /* Descend into this directory */
2695 int iNew = pCur->iLvl + 1;
2697 if( iNew>=pCur->nLvl ){
2699 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2700 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2701 if( aNew==0 ) return SQLITE_NOMEM;
2702 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2707 pLvl = &pCur->aLvl[iNew];
2709 pLvl->zDir = pCur->zPath;
2711 pLvl->pDir = opendir(pLvl->zDir);
2712 if( pLvl->pDir==0 ){
2713 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2714 return SQLITE_ERROR;
2718 while( pCur->iLvl>=0 ){
2719 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2720 struct dirent *pEntry = readdir(pLvl->pDir);
2722 if( pEntry->d_name[0]=='.' ){
2723 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2724 if( pEntry->d_name[1]=='\0' ) continue;
2726 sqlite3_free(pCur->zPath);
2727 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2728 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2729 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2730 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2731 return SQLITE_ERROR;
2735 closedir(pLvl->pDir);
2736 sqlite3_free(pLvl->zDir);
2743 sqlite3_free(pCur->zPath);
2749 ** Return values of columns for the row at which the series_cursor
2750 ** is currently pointing.
2752 static int fsdirColumn(
2753 sqlite3_vtab_cursor *cur, /* The cursor */
2754 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2755 int i /* Which column to return */
2757 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2759 case FSDIR_COLUMN_NAME: {
2760 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2764 case FSDIR_COLUMN_MODE:
2765 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2768 case FSDIR_COLUMN_MTIME:
2769 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2772 case FSDIR_COLUMN_DATA: {
2773 mode_t m = pCur->sStat.st_mode;
2775 sqlite3_result_null(ctx);
2776 #if !defined(_WIN32) && !defined(WIN32)
2777 }else if( S_ISLNK(m) ){
2779 char *aBuf = aStatic;
2780 sqlite3_int64 nBuf = 64;
2784 n = readlink(pCur->zPath, aBuf, nBuf);
2786 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2788 aBuf = sqlite3_malloc64(nBuf);
2790 sqlite3_result_error_nomem(ctx);
2791 return SQLITE_NOMEM;
2795 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2796 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2799 readFileContents(ctx, pCur->zPath);
2802 case FSDIR_COLUMN_PATH:
2804 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2805 ** always return their values as NULL */
2813 ** Return the rowid for the current row. In this implementation, the
2814 ** first row returned is assigned rowid value 1, and each subsequent
2815 ** row a value 1 more than that of the previous.
2817 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2818 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2819 *pRowid = pCur->iRowid;
2824 ** Return TRUE if the cursor has been moved off of the last
2827 static int fsdirEof(sqlite3_vtab_cursor *cur){
2828 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2829 return (pCur->zPath==0);
2833 ** xFilter callback.
2835 ** idxNum==1 PATH parameter only
2836 ** idxNum==2 Both PATH and DIR supplied
2838 static int fsdirFilter(
2839 sqlite3_vtab_cursor *cur,
2840 int idxNum, const char *idxStr,
2841 int argc, sqlite3_value **argv
2843 const char *zDir = 0;
2844 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2846 fsdirResetCursor(pCur);
2849 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2850 return SQLITE_ERROR;
2853 assert( argc==idxNum && (argc==1 || argc==2) );
2854 zDir = (const char*)sqlite3_value_text(argv[0]);
2856 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2857 return SQLITE_ERROR;
2860 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2863 pCur->nBase = (int)strlen(pCur->zBase)+1;
2864 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2866 pCur->zPath = sqlite3_mprintf("%s", zDir);
2869 if( pCur->zPath==0 ){
2870 return SQLITE_NOMEM;
2872 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2873 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2874 return SQLITE_ERROR;
2881 ** SQLite will invoke this method one or more times while planning a query
2882 ** that uses the generate_series virtual table. This routine needs to create
2883 ** a query plan for each invocation and compute an estimated cost for that
2886 ** In this implementation idxNum is used to represent the
2887 ** query plan. idxStr is unused.
2889 ** The query plan is represented by values of idxNum:
2891 ** (1) The path value is supplied by argv[0]
2892 ** (2) Path is in argv[0] and dir is in argv[1]
2894 static int fsdirBestIndex(
2896 sqlite3_index_info *pIdxInfo
2898 int i; /* Loop over constraints */
2899 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2900 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2901 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2902 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
2903 const struct sqlite3_index_constraint *pConstraint;
2906 pConstraint = pIdxInfo->aConstraint;
2907 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2908 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2909 switch( pConstraint->iColumn ){
2910 case FSDIR_COLUMN_PATH: {
2911 if( pConstraint->usable ){
2914 }else if( idxPath<0 ){
2919 case FSDIR_COLUMN_DIR: {
2920 if( pConstraint->usable ){
2923 }else if( idxDir<0 ){
2930 if( seenPath || seenDir ){
2931 /* If input parameters are unusable, disallow this plan */
2932 return SQLITE_CONSTRAINT;
2936 pIdxInfo->idxNum = 0;
2937 /* The pIdxInfo->estimatedCost should have been initialized to a huge
2938 ** number. Leave it unchanged. */
2939 pIdxInfo->estimatedRows = 0x7fffffff;
2941 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
2942 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
2944 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
2945 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
2946 pIdxInfo->idxNum = 2;
2947 pIdxInfo->estimatedCost = 10.0;
2949 pIdxInfo->idxNum = 1;
2950 pIdxInfo->estimatedCost = 100.0;
2958 ** Register the "fsdir" virtual table.
2960 static int fsdirRegister(sqlite3 *db){
2961 static sqlite3_module fsdirModule = {
2964 fsdirConnect, /* xConnect */
2965 fsdirBestIndex, /* xBestIndex */
2966 fsdirDisconnect, /* xDisconnect */
2968 fsdirOpen, /* xOpen - open a cursor */
2969 fsdirClose, /* xClose - close a cursor */
2970 fsdirFilter, /* xFilter - configure scan constraints */
2971 fsdirNext, /* xNext - advance a cursor */
2972 fsdirEof, /* xEof - check for end of scan */
2973 fsdirColumn, /* xColumn - read data */
2974 fsdirRowid, /* xRowid - read data */
2980 0, /* xFindMethod */
2984 0, /* xRollbackTo */
2985 0, /* xShadowName */
2988 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2991 #else /* SQLITE_OMIT_VIRTUALTABLE */
2992 # define fsdirRegister(x) SQLITE_OK
2998 int sqlite3_fileio_init(
3001 const sqlite3_api_routines *pApi
3004 SQLITE_EXTENSION_INIT2(pApi);
3005 (void)pzErrMsg; /* Unused parameter */
3006 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
3007 readfileFunc, 0, 0);
3008 if( rc==SQLITE_OK ){
3009 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
3010 writefileFunc, 0, 0);
3012 if( rc==SQLITE_OK ){
3013 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3016 if( rc==SQLITE_OK ){
3017 rc = fsdirRegister(db);
3022 /************************* End ../ext/misc/fileio.c ********************/
3023 /************************* Begin ../ext/misc/completion.c ******************/
3027 ** The author disclaims copyright to this source code. In place of
3028 ** a legal notice, here is a blessing:
3030 ** May you do good and not evil.
3031 ** May you find forgiveness for yourself and forgive others.
3032 ** May you share freely, never taking more than you give.
3034 *************************************************************************
3036 ** This file implements an eponymous virtual table that returns suggested
3037 ** completions for a partial SQL input.
3041 ** SELECT DISTINCT candidate COLLATE nocase
3042 ** FROM completion($prefix,$wholeline)
3045 ** The two query parameters are optional. $prefix is the text of the
3046 ** current word being typed and that is to be completed. $wholeline is
3047 ** the complete input line, used for context.
3049 ** The raw completion() table might return the same candidate multiple
3050 ** times, for example if the same column name is used to two or more
3051 ** tables. And the candidates are returned in an arbitrary order. Hence,
3052 ** the DISTINCT and ORDER BY are recommended.
3054 ** This virtual table operates at the speed of human typing, and so there
3055 ** is no attempt to make it fast. Even a slow implementation will be much
3056 ** faster than any human can type.
3059 SQLITE_EXTENSION_INIT1
3064 #ifndef SQLITE_OMIT_VIRTUALTABLE
3066 /* completion_vtab is a subclass of sqlite3_vtab which will
3067 ** serve as the underlying representation of a completion virtual table
3069 typedef struct completion_vtab completion_vtab;
3070 struct completion_vtab {
3071 sqlite3_vtab base; /* Base class - must be first */
3072 sqlite3 *db; /* Database connection for this completion vtab */
3075 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3076 ** serve as the underlying representation of a cursor that scans
3077 ** over rows of the result
3079 typedef struct completion_cursor completion_cursor;
3080 struct completion_cursor {
3081 sqlite3_vtab_cursor base; /* Base class - must be first */
3082 sqlite3 *db; /* Database connection for this cursor */
3083 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3084 char *zPrefix; /* The prefix for the word we want to complete */
3085 char *zLine; /* The whole that we want to complete */
3086 const char *zCurrentRow; /* Current output row */
3087 int szRow; /* Length of the zCurrentRow string */
3088 sqlite3_stmt *pStmt; /* Current statement */
3089 sqlite3_int64 iRowid; /* The rowid */
3090 int ePhase; /* Current phase */
3091 int j; /* inter-phase counter */
3094 /* Values for ePhase:
3096 #define COMPLETION_FIRST_PHASE 1
3097 #define COMPLETION_KEYWORDS 1
3098 #define COMPLETION_PRAGMAS 2
3099 #define COMPLETION_FUNCTIONS 3
3100 #define COMPLETION_COLLATIONS 4
3101 #define COMPLETION_INDEXES 5
3102 #define COMPLETION_TRIGGERS 6
3103 #define COMPLETION_DATABASES 7
3104 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3105 #define COMPLETION_COLUMNS 9
3106 #define COMPLETION_MODULES 10
3107 #define COMPLETION_EOF 11
3110 ** The completionConnect() method is invoked to create a new
3111 ** completion_vtab that describes the completion virtual table.
3113 ** Think of this routine as the constructor for completion_vtab objects.
3115 ** All this routine needs to do is:
3117 ** (1) Allocate the completion_vtab object and initialize all fields.
3119 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3120 ** result set of queries against completion will look like.
3122 static int completionConnect(
3125 int argc, const char *const*argv,
3126 sqlite3_vtab **ppVtab,
3129 completion_vtab *pNew;
3132 (void)(pAux); /* Unused parameter */
3133 (void)(argc); /* Unused parameter */
3134 (void)(argv); /* Unused parameter */
3135 (void)(pzErr); /* Unused parameter */
3137 /* Column numbers */
3138 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3139 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3140 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3141 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3143 rc = sqlite3_declare_vtab(db,
3146 " prefix TEXT HIDDEN,"
3147 " wholeline TEXT HIDDEN,"
3148 " phase INT HIDDEN" /* Used for debugging only */
3150 if( rc==SQLITE_OK ){
3151 pNew = sqlite3_malloc( sizeof(*pNew) );
3152 *ppVtab = (sqlite3_vtab*)pNew;
3153 if( pNew==0 ) return SQLITE_NOMEM;
3154 memset(pNew, 0, sizeof(*pNew));
3161 ** This method is the destructor for completion_cursor objects.
3163 static int completionDisconnect(sqlite3_vtab *pVtab){
3164 sqlite3_free(pVtab);
3169 ** Constructor for a new completion_cursor object.
3171 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3172 completion_cursor *pCur;
3173 pCur = sqlite3_malloc( sizeof(*pCur) );
3174 if( pCur==0 ) return SQLITE_NOMEM;
3175 memset(pCur, 0, sizeof(*pCur));
3176 pCur->db = ((completion_vtab*)p)->db;
3177 *ppCursor = &pCur->base;
3182 ** Reset the completion_cursor.
3184 static void completionCursorReset(completion_cursor *pCur){
3185 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3186 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3187 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3192 ** Destructor for a completion_cursor.
3194 static int completionClose(sqlite3_vtab_cursor *cur){
3195 completionCursorReset((completion_cursor*)cur);
3201 ** Advance a completion_cursor to its next row of output.
3203 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3204 ** record the current state of the scan. This routine sets ->zCurrentRow
3205 ** to the current row of output and then returns. If no more rows remain,
3206 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3207 ** table that has reached the end of its scan.
3209 ** The current implementation just lists potential identifiers and
3210 ** keywords and filters them by zPrefix. Future enhancements should
3211 ** take zLine into account to try to restrict the set of identifiers and
3212 ** keywords based on what would be legal at the current point of input.
3214 static int completionNext(sqlite3_vtab_cursor *cur){
3215 completion_cursor *pCur = (completion_cursor*)cur;
3216 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3217 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3219 while( pCur->ePhase!=COMPLETION_EOF ){
3220 switch( pCur->ePhase ){
3221 case COMPLETION_KEYWORDS: {
3222 if( pCur->j >= sqlite3_keyword_count() ){
3223 pCur->zCurrentRow = 0;
3224 pCur->ePhase = COMPLETION_DATABASES;
3226 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3231 case COMPLETION_DATABASES: {
3232 if( pCur->pStmt==0 ){
3233 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3237 eNextPhase = COMPLETION_TABLES;
3240 case COMPLETION_TABLES: {
3241 if( pCur->pStmt==0 ){
3244 const char *zSep = "";
3245 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3246 while( sqlite3_step(pS2)==SQLITE_ROW ){
3247 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3248 zSql = sqlite3_mprintf(
3250 "SELECT name FROM \"%w\".sqlite_master",
3253 if( zSql==0 ) return SQLITE_NOMEM;
3256 sqlite3_finalize(pS2);
3257 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3261 eNextPhase = COMPLETION_COLUMNS;
3264 case COMPLETION_COLUMNS: {
3265 if( pCur->pStmt==0 ){
3268 const char *zSep = "";
3269 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3270 while( sqlite3_step(pS2)==SQLITE_ROW ){
3271 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3272 zSql = sqlite3_mprintf(
3274 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3275 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3276 " WHERE sm.type='table'",
3277 zSql, zSep, zDb, zDb
3279 if( zSql==0 ) return SQLITE_NOMEM;
3282 sqlite3_finalize(pS2);
3283 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3287 eNextPhase = COMPLETION_EOF;
3292 /* This case is when the phase presets zCurrentRow */
3293 if( pCur->zCurrentRow==0 ) continue;
3295 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3296 /* Extract the next row of content */
3297 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3298 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3300 /* When all rows are finished, advance to the next phase */
3301 sqlite3_finalize(pCur->pStmt);
3303 pCur->ePhase = eNextPhase;
3307 if( pCur->nPrefix==0 ) break;
3308 if( pCur->nPrefix<=pCur->szRow
3309 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3319 ** Return values of columns for the row at which the completion_cursor
3320 ** is currently pointing.
3322 static int completionColumn(
3323 sqlite3_vtab_cursor *cur, /* The cursor */
3324 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3325 int i /* Which column to return */
3327 completion_cursor *pCur = (completion_cursor*)cur;
3329 case COMPLETION_COLUMN_CANDIDATE: {
3330 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3333 case COMPLETION_COLUMN_PREFIX: {
3334 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3337 case COMPLETION_COLUMN_WHOLELINE: {
3338 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3341 case COMPLETION_COLUMN_PHASE: {
3342 sqlite3_result_int(ctx, pCur->ePhase);
3350 ** Return the rowid for the current row. In this implementation, the
3351 ** rowid is the same as the output value.
3353 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3354 completion_cursor *pCur = (completion_cursor*)cur;
3355 *pRowid = pCur->iRowid;
3360 ** Return TRUE if the cursor has been moved off of the last
3363 static int completionEof(sqlite3_vtab_cursor *cur){
3364 completion_cursor *pCur = (completion_cursor*)cur;
3365 return pCur->ePhase >= COMPLETION_EOF;
3369 ** This method is called to "rewind" the completion_cursor object back
3370 ** to the first row of output. This method is always called at least
3371 ** once prior to any call to completionColumn() or completionRowid() or
3374 static int completionFilter(
3375 sqlite3_vtab_cursor *pVtabCursor,
3376 int idxNum, const char *idxStr,
3377 int argc, sqlite3_value **argv
3379 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3381 (void)(idxStr); /* Unused parameter */
3382 (void)(argc); /* Unused parameter */
3383 completionCursorReset(pCur);
3385 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3386 if( pCur->nPrefix>0 ){
3387 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3388 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3393 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3394 if( pCur->nLine>0 ){
3395 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3396 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3399 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3400 int i = pCur->nLine;
3401 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3404 pCur->nPrefix = pCur->nLine - i;
3405 if( pCur->nPrefix>0 ){
3406 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3407 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3411 pCur->ePhase = COMPLETION_FIRST_PHASE;
3412 return completionNext(pVtabCursor);
3416 ** SQLite will invoke this method one or more times while planning a query
3417 ** that uses the completion virtual table. This routine needs to create
3418 ** a query plan for each invocation and compute an estimated cost for that
3421 ** There are two hidden parameters that act as arguments to the table-valued
3422 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3423 ** is available and bit 1 is set if "wholeline" is available.
3425 static int completionBestIndex(
3427 sqlite3_index_info *pIdxInfo
3429 int i; /* Loop over constraints */
3430 int idxNum = 0; /* The query plan bitmask */
3431 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3432 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3433 int nArg = 0; /* Number of arguments that completeFilter() expects */
3434 const struct sqlite3_index_constraint *pConstraint;
3436 (void)(tab); /* Unused parameter */
3437 pConstraint = pIdxInfo->aConstraint;
3438 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3439 if( pConstraint->usable==0 ) continue;
3440 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3441 switch( pConstraint->iColumn ){
3442 case COMPLETION_COLUMN_PREFIX:
3446 case COMPLETION_COLUMN_WHOLELINE:
3453 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3454 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3456 if( wholelineIdx>=0 ){
3457 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3458 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3460 pIdxInfo->idxNum = idxNum;
3461 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3462 pIdxInfo->estimatedRows = 500 - 100*nArg;
3467 ** This following structure defines all the methods for the
3468 ** completion virtual table.
3470 static sqlite3_module completionModule = {
3473 completionConnect, /* xConnect */
3474 completionBestIndex, /* xBestIndex */
3475 completionDisconnect, /* xDisconnect */
3477 completionOpen, /* xOpen - open a cursor */
3478 completionClose, /* xClose - close a cursor */
3479 completionFilter, /* xFilter - configure scan constraints */
3480 completionNext, /* xNext - advance a cursor */
3481 completionEof, /* xEof - check for end of scan */
3482 completionColumn, /* xColumn - read data */
3483 completionRowid, /* xRowid - read data */
3489 0, /* xFindMethod */
3493 0, /* xRollbackTo */
3497 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3499 int sqlite3CompletionVtabInit(sqlite3 *db){
3501 #ifndef SQLITE_OMIT_VIRTUALTABLE
3502 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3510 int sqlite3_completion_init(
3513 const sqlite3_api_routines *pApi
3516 SQLITE_EXTENSION_INIT2(pApi);
3517 (void)(pzErrMsg); /* Unused parameter */
3518 #ifndef SQLITE_OMIT_VIRTUALTABLE
3519 rc = sqlite3CompletionVtabInit(db);
3524 /************************* End ../ext/misc/completion.c ********************/
3525 /************************* Begin ../ext/misc/appendvfs.c ******************/
3529 ** The author disclaims copyright to this source code. In place of
3530 ** a legal notice, here is a blessing:
3532 ** May you do good and not evil.
3533 ** May you find forgiveness for yourself and forgive others.
3534 ** May you share freely, never taking more than you give.
3536 ******************************************************************************
3538 ** This file implements a VFS shim that allows an SQLite database to be
3539 ** appended onto the end of some other file, such as an executable.
3541 ** A special record must appear at the end of the file that identifies the
3542 ** file as an appended database and provides an offset to page 1. For
3543 ** best performance page 1 should be located at a disk page boundary, though
3544 ** that is not required.
3546 ** When opening a database using this VFS, the connection might treat
3547 ** the file as an ordinary SQLite database, or it might treat is as a
3548 ** database appended onto some other file. Here are the rules:
3550 ** (1) When opening a new empty file, that file is treated as an ordinary
3553 ** (2) When opening a file that begins with the standard SQLite prefix
3554 ** string "SQLite format 3", that file is treated as an ordinary
3557 ** (3) When opening a file that ends with the appendvfs trailer string
3558 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3561 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3562 ** set, then a new database is appended to the already existing file.
3564 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3566 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3567 ** the file containing the database is limited to 1GB. This VFS will refuse
3568 ** to read or write past the 1GB mark. This restriction might be lifted in
3569 ** future versions. For now, if you need a large database, then keep the
3570 ** database in a separate file.
3572 ** If the file being opened is not an appended database, then this shim is
3573 ** a pass-through into the default underlying VFS.
3575 SQLITE_EXTENSION_INIT1
3579 /* The append mark at the end of the database is:
3581 ** Start-Of-SQLite3-NNNNNNNN
3582 ** 123456789 123456789 12345
3584 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3585 ** the offset to page 1.
3587 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3588 #define APND_MARK_PREFIX_SZ 17
3589 #define APND_MARK_SIZE 25
3592 ** Maximum size of the combined prefix + database + append-mark. This
3593 ** must be less than 0x40000000 to avoid locking issues on Windows.
3595 #define APND_MAX_SIZE (65536*15259)
3598 ** Forward declaration of objects used by this utility
3600 typedef struct sqlite3_vfs ApndVfs;
3601 typedef struct ApndFile ApndFile;
3603 /* Access to a lower-level VFS that (might) implement dynamic loading,
3604 ** access to randomness, etc.
3606 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3607 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3611 sqlite3_file base; /* IO methods */
3612 sqlite3_int64 iPgOne; /* File offset to page 1 */
3613 sqlite3_int64 iMark; /* Start of the append-mark */
3617 ** Methods for ApndFile
3619 static int apndClose(sqlite3_file*);
3620 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3621 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3622 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3623 static int apndSync(sqlite3_file*, int flags);
3624 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3625 static int apndLock(sqlite3_file*, int);
3626 static int apndUnlock(sqlite3_file*, int);
3627 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3628 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3629 static int apndSectorSize(sqlite3_file*);
3630 static int apndDeviceCharacteristics(sqlite3_file*);
3631 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3632 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3633 static void apndShmBarrier(sqlite3_file*);
3634 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3635 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3636 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3639 ** Methods for ApndVfs
3641 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3642 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3643 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3644 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3645 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3646 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3647 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3648 static void apndDlClose(sqlite3_vfs*, void*);
3649 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3650 static int apndSleep(sqlite3_vfs*, int microseconds);
3651 static int apndCurrentTime(sqlite3_vfs*, double*);
3652 static int apndGetLastError(sqlite3_vfs*, int, char *);
3653 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3654 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3655 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3656 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3658 static sqlite3_vfs apnd_vfs = {
3659 3, /* iVersion (set when registered) */
3660 0, /* szOsFile (set when registered) */
3661 1024, /* mxPathname */
3663 "apndvfs", /* zName */
3664 0, /* pAppData (set when registered) */
3665 apndOpen, /* xOpen */
3666 apndDelete, /* xDelete */
3667 apndAccess, /* xAccess */
3668 apndFullPathname, /* xFullPathname */
3669 apndDlOpen, /* xDlOpen */
3670 apndDlError, /* xDlError */
3671 apndDlSym, /* xDlSym */
3672 apndDlClose, /* xDlClose */
3673 apndRandomness, /* xRandomness */
3674 apndSleep, /* xSleep */
3675 apndCurrentTime, /* xCurrentTime */
3676 apndGetLastError, /* xGetLastError */
3677 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3678 apndSetSystemCall, /* xSetSystemCall */
3679 apndGetSystemCall, /* xGetSystemCall */
3680 apndNextSystemCall /* xNextSystemCall */
3683 static const sqlite3_io_methods apnd_io_methods = {
3685 apndClose, /* xClose */
3686 apndRead, /* xRead */
3687 apndWrite, /* xWrite */
3688 apndTruncate, /* xTruncate */
3689 apndSync, /* xSync */
3690 apndFileSize, /* xFileSize */
3691 apndLock, /* xLock */
3692 apndUnlock, /* xUnlock */
3693 apndCheckReservedLock, /* xCheckReservedLock */
3694 apndFileControl, /* xFileControl */
3695 apndSectorSize, /* xSectorSize */
3696 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3697 apndShmMap, /* xShmMap */
3698 apndShmLock, /* xShmLock */
3699 apndShmBarrier, /* xShmBarrier */
3700 apndShmUnmap, /* xShmUnmap */
3701 apndFetch, /* xFetch */
3702 apndUnfetch /* xUnfetch */
3708 ** Close an apnd-file.
3710 static int apndClose(sqlite3_file *pFile){
3711 pFile = ORIGFILE(pFile);
3712 return pFile->pMethods->xClose(pFile);
3716 ** Read data from an apnd-file.
3718 static int apndRead(
3719 sqlite3_file *pFile,
3724 ApndFile *p = (ApndFile *)pFile;
3725 pFile = ORIGFILE(pFile);
3726 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3730 ** Add the append-mark onto the end of the file.
3732 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3734 unsigned char a[APND_MARK_SIZE];
3735 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3737 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3739 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3743 ** Write data to an apnd-file.
3745 static int apndWrite(
3746 sqlite3_file *pFile,
3752 ApndFile *p = (ApndFile *)pFile;
3753 pFile = ORIGFILE(pFile);
3754 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3755 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3756 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3757 sqlite3_int64 sz = 0;
3758 rc = pFile->pMethods->xFileSize(pFile, &sz);
3759 if( rc==SQLITE_OK ){
3760 p->iMark = sz - APND_MARK_SIZE;
3761 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3762 p->iMark = p->iPgOne + iOfst + iAmt;
3763 rc = apndWriteMark(p, pFile);
3771 ** Truncate an apnd-file.
3773 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3775 ApndFile *p = (ApndFile *)pFile;
3776 pFile = ORIGFILE(pFile);
3777 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3778 if( rc==SQLITE_OK ){
3779 p->iMark = p->iPgOne+size;
3780 rc = apndWriteMark(p, pFile);
3786 ** Sync an apnd-file.
3788 static int apndSync(sqlite3_file *pFile, int flags){
3789 pFile = ORIGFILE(pFile);
3790 return pFile->pMethods->xSync(pFile, flags);
3794 ** Return the current file-size of an apnd-file.
3796 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3797 ApndFile *p = (ApndFile *)pFile;
3799 pFile = ORIGFILE(p);
3800 rc = pFile->pMethods->xFileSize(pFile, pSize);
3801 if( rc==SQLITE_OK && p->iPgOne ){
3802 *pSize -= p->iPgOne + APND_MARK_SIZE;
3808 ** Lock an apnd-file.
3810 static int apndLock(sqlite3_file *pFile, int eLock){
3811 pFile = ORIGFILE(pFile);
3812 return pFile->pMethods->xLock(pFile, eLock);
3816 ** Unlock an apnd-file.
3818 static int apndUnlock(sqlite3_file *pFile, int eLock){
3819 pFile = ORIGFILE(pFile);
3820 return pFile->pMethods->xUnlock(pFile, eLock);
3824 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3826 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3827 pFile = ORIGFILE(pFile);
3828 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3832 ** File control method. For custom operations on an apnd-file.
3834 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3835 ApndFile *p = (ApndFile *)pFile;
3837 pFile = ORIGFILE(pFile);
3838 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3839 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3840 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3846 ** Return the sector-size in bytes for an apnd-file.
3848 static int apndSectorSize(sqlite3_file *pFile){
3849 pFile = ORIGFILE(pFile);
3850 return pFile->pMethods->xSectorSize(pFile);
3854 ** Return the device characteristic flags supported by an apnd-file.
3856 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3857 pFile = ORIGFILE(pFile);
3858 return pFile->pMethods->xDeviceCharacteristics(pFile);
3861 /* Create a shared memory file mapping */
3862 static int apndShmMap(
3863 sqlite3_file *pFile,
3869 pFile = ORIGFILE(pFile);
3870 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3873 /* Perform locking on a shared-memory segment */
3874 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3875 pFile = ORIGFILE(pFile);
3876 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3879 /* Memory barrier operation on shared memory */
3880 static void apndShmBarrier(sqlite3_file *pFile){
3881 pFile = ORIGFILE(pFile);
3882 pFile->pMethods->xShmBarrier(pFile);
3885 /* Unmap a shared memory segment */
3886 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3887 pFile = ORIGFILE(pFile);
3888 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3891 /* Fetch a page of a memory-mapped file */
3892 static int apndFetch(
3893 sqlite3_file *pFile,
3894 sqlite3_int64 iOfst,
3898 ApndFile *p = (ApndFile *)pFile;
3899 pFile = ORIGFILE(pFile);
3900 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3903 /* Release a memory-mapped page */
3904 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3905 ApndFile *p = (ApndFile *)pFile;
3906 pFile = ORIGFILE(pFile);
3907 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3911 ** Check to see if the file is an ordinary SQLite database file.
3913 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3916 static const char aSqliteHdr[] = "SQLite format 3";
3917 if( sz<512 ) return 0;
3918 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3920 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3924 ** Try to read the append-mark off the end of a file. Return the
3925 ** start of the appended database if the append-mark is present. If
3926 ** there is no append-mark, return -1;
3928 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3930 sqlite3_int64 iMark;
3931 unsigned char a[APND_MARK_SIZE];
3933 if( sz<=APND_MARK_SIZE ) return -1;
3934 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3936 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3937 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3939 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3945 ** Open an apnd file handle.
3947 static int apndOpen(
3950 sqlite3_file *pFile,
3955 sqlite3_file *pSubFile;
3956 sqlite3_vfs *pSubVfs;
3959 pSubVfs = ORIGVFS(pVfs);
3960 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3961 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3963 p = (ApndFile*)pFile;
3964 memset(p, 0, sizeof(*p));
3965 pSubFile = ORIGFILE(pFile);
3966 p->base.pMethods = &apnd_io_methods;
3967 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3968 if( rc ) goto apnd_open_done;
3969 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3971 pSubFile->pMethods->xClose(pSubFile);
3972 goto apnd_open_done;
3974 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3975 memmove(pFile, pSubFile, pSubVfs->szOsFile);
3979 p->iPgOne = apndReadMark(sz, pFile);
3983 if( (flags & SQLITE_OPEN_CREATE)==0 ){
3984 pSubFile->pMethods->xClose(pSubFile);
3985 rc = SQLITE_CANTOPEN;
3987 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3989 if( rc ) pFile->pMethods = 0;
3994 ** All other VFS methods are pass-thrus.
3996 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3997 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3999 static int apndAccess(
4005 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4007 static int apndFullPathname(
4013 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4015 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4016 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4018 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4019 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4021 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4022 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4024 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4025 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4027 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4028 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4030 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4031 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4033 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4034 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4036 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4037 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4039 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4040 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4042 static int apndSetSystemCall(
4045 sqlite3_syscall_ptr pCall
4047 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4049 static sqlite3_syscall_ptr apndGetSystemCall(
4053 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4055 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4056 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4064 ** This routine is called when the extension is loaded.
4065 ** Register the new VFS.
4067 int sqlite3_appendvfs_init(
4070 const sqlite3_api_routines *pApi
4074 SQLITE_EXTENSION_INIT2(pApi);
4077 pOrig = sqlite3_vfs_find(0);
4078 apnd_vfs.iVersion = pOrig->iVersion;
4079 apnd_vfs.pAppData = pOrig;
4080 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4081 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4082 #ifdef APPENDVFS_TEST
4083 if( rc==SQLITE_OK ){
4084 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4087 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4091 /************************* End ../ext/misc/appendvfs.c ********************/
4092 /************************* Begin ../ext/misc/memtrace.c ******************/
4096 ** The author disclaims copyright to this source code. In place of
4097 ** a legal notice, here is a blessing:
4099 ** May you do good and not evil.
4100 ** May you find forgiveness for yourself and forgive others.
4101 ** May you share freely, never taking more than you give.
4103 *************************************************************************
4105 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4106 ** mechanism to add a tracing layer on top of SQLite. If this extension
4107 ** is registered prior to sqlite3_initialize(), it will cause all memory
4108 ** allocation activities to be logged on standard output, or to some other
4109 ** FILE specified by the initializer.
4111 ** This file needs to be compiled into the application that uses it.
4113 ** This extension is used to implement the --memtrace option of the
4114 ** command-line shell.
4120 /* The original memory allocation routines */
4121 static sqlite3_mem_methods memtraceBase;
4122 static FILE *memtraceOut;
4124 /* Methods that trace memory allocations */
4125 static void *memtraceMalloc(int n){
4127 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4128 memtraceBase.xRoundup(n));
4130 return memtraceBase.xMalloc(n);
4132 static void memtraceFree(void *p){
4135 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4137 memtraceBase.xFree(p);
4139 static void *memtraceRealloc(void *p, int n){
4140 if( p==0 ) return memtraceMalloc(n);
4146 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4147 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4149 return memtraceBase.xRealloc(p, n);
4151 static int memtraceSize(void *p){
4152 return memtraceBase.xSize(p);
4154 static int memtraceRoundup(int n){
4155 return memtraceBase.xRoundup(n);
4157 static int memtraceInit(void *p){
4158 return memtraceBase.xInit(p);
4160 static void memtraceShutdown(void *p){
4161 memtraceBase.xShutdown(p);
4164 /* The substitute memory allocator */
4165 static sqlite3_mem_methods ersaztMethods = {
4176 /* Begin tracing memory allocations to out. */
4177 int sqlite3MemTraceActivate(FILE *out){
4179 if( memtraceBase.xMalloc==0 ){
4180 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4181 if( rc==SQLITE_OK ){
4182 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4189 /* Deactivate memory tracing */
4190 int sqlite3MemTraceDeactivate(void){
4192 if( memtraceBase.xMalloc!=0 ){
4193 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4194 if( rc==SQLITE_OK ){
4195 memset(&memtraceBase, 0, sizeof(memtraceBase));
4202 /************************* End ../ext/misc/memtrace.c ********************/
4203 #ifdef SQLITE_HAVE_ZLIB
4204 /************************* Begin ../ext/misc/zipfile.c ******************/
4208 ** The author disclaims copyright to this source code. In place of
4209 ** a legal notice, here is a blessing:
4211 ** May you do good and not evil.
4212 ** May you find forgiveness for yourself and forgive others.
4213 ** May you share freely, never taking more than you give.
4215 ******************************************************************************
4217 ** This file implements a virtual table for reading and writing ZIP archive
4222 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4224 ** Current limitations:
4226 ** * No support for encryption
4227 ** * No support for ZIP archives spanning multiple files
4228 ** * No support for zip64 extensions
4229 ** * Only the "inflate/deflate" (zlib) compression method is supported
4231 SQLITE_EXTENSION_INIT1
4238 #ifndef SQLITE_OMIT_VIRTUALTABLE
4240 #ifndef SQLITE_AMALGAMATION
4242 /* typedef sqlite3_int64 i64; */
4243 /* typedef unsigned char u8; */
4244 typedef unsigned short u16;
4245 typedef unsigned long u32;
4246 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4248 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4249 # define ALWAYS(X) (1)
4250 # define NEVER(X) (0)
4251 #elif !defined(NDEBUG)
4252 # define ALWAYS(X) ((X)?1:(assert(0),0))
4253 # define NEVER(X) ((X)?(assert(0),1):0)
4255 # define ALWAYS(X) (X)
4256 # define NEVER(X) (X)
4259 #endif /* SQLITE_AMALGAMATION */
4262 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4264 ** In some ways it would be better to obtain these values from system
4265 ** header files. But, the dependency is undesirable and (a) these
4266 ** have been stable for decades, (b) the values are part of POSIX and
4267 ** are also made explicit in [man stat], and (c) are part of the
4268 ** file format for zip archives.
4271 # define S_IFDIR 0040000
4274 # define S_IFREG 0100000
4277 # define S_IFLNK 0120000
4280 static const char ZIPFILE_SCHEMA[] =
4282 "name PRIMARY KEY," /* 0: Name of file in zip archive */
4283 "mode," /* 1: POSIX mode for file */
4284 "mtime," /* 2: Last modification time (secs since 1970)*/
4285 "sz," /* 3: Size of object */
4286 "rawdata," /* 4: Raw data */
4287 "data," /* 5: Uncompressed data */
4288 "method," /* 6: Compression method (integer) */
4289 "z HIDDEN" /* 7: Name of zip file */
4292 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
4293 #define ZIPFILE_BUFFER_SIZE (64*1024)
4297 ** Magic numbers used to read and write zip files.
4299 ** ZIPFILE_NEWENTRY_MADEBY:
4300 ** Use this value for the "version-made-by" field in new zip file
4301 ** entries. The upper byte indicates "unix", and the lower byte
4302 ** indicates that the zip file matches pkzip specification 3.0.
4303 ** This is what info-zip seems to do.
4305 ** ZIPFILE_NEWENTRY_REQUIRED:
4306 ** Value for "version-required-to-extract" field of new entries.
4307 ** Version 2.0 is required to support folders and deflate compression.
4309 ** ZIPFILE_NEWENTRY_FLAGS:
4310 ** Value for "general-purpose-bit-flags" field of new entries. Bit
4311 ** 11 means "utf-8 filename and comment".
4313 ** ZIPFILE_SIGNATURE_CDS:
4314 ** First 4 bytes of a valid CDS record.
4316 ** ZIPFILE_SIGNATURE_LFH:
4317 ** First 4 bytes of a valid LFH record.
4319 ** ZIPFILE_SIGNATURE_EOCD
4320 ** First 4 bytes of a valid EOCD record.
4322 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4323 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4324 #define ZIPFILE_NEWENTRY_REQUIRED 20
4325 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4326 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4327 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4328 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4331 ** The sizes of the fixed-size part of each of the three main data
4332 ** structures in a zip archive.
4334 #define ZIPFILE_LFH_FIXED_SZ 30
4335 #define ZIPFILE_EOCD_FIXED_SZ 22
4336 #define ZIPFILE_CDS_FIXED_SZ 46
4339 *** 4.3.16 End of central directory record:
4341 *** end of central dir signature 4 bytes (0x06054b50)
4342 *** number of this disk 2 bytes
4343 *** number of the disk with the
4344 *** start of the central directory 2 bytes
4345 *** total number of entries in the
4346 *** central directory on this disk 2 bytes
4347 *** total number of entries in
4348 *** the central directory 2 bytes
4349 *** size of the central directory 4 bytes
4350 *** offset of start of central
4351 *** directory with respect to
4352 *** the starting disk number 4 bytes
4353 *** .ZIP file comment length 2 bytes
4354 *** .ZIP file comment (variable size)
4356 typedef struct ZipfileEOCD ZipfileEOCD;
4357 struct ZipfileEOCD {
4367 *** 4.3.12 Central directory structure:
4371 *** central file header signature 4 bytes (0x02014b50)
4372 *** version made by 2 bytes
4373 *** version needed to extract 2 bytes
4374 *** general purpose bit flag 2 bytes
4375 *** compression method 2 bytes
4376 *** last mod file time 2 bytes
4377 *** last mod file date 2 bytes
4379 *** compressed size 4 bytes
4380 *** uncompressed size 4 bytes
4381 *** file name length 2 bytes
4382 *** extra field length 2 bytes
4383 *** file comment length 2 bytes
4384 *** disk number start 2 bytes
4385 *** internal file attributes 2 bytes
4386 *** external file attributes 4 bytes
4387 *** relative offset of local header 4 bytes
4389 typedef struct ZipfileCDS ZipfileCDS;
4392 u16 iVersionExtract;
4407 char *zFile; /* Filename (sqlite3_malloc()) */
4411 *** 4.3.7 Local file header:
4413 *** local file header signature 4 bytes (0x04034b50)
4414 *** version needed to extract 2 bytes
4415 *** general purpose bit flag 2 bytes
4416 *** compression method 2 bytes
4417 *** last mod file time 2 bytes
4418 *** last mod file date 2 bytes
4420 *** compressed size 4 bytes
4421 *** uncompressed size 4 bytes
4422 *** file name length 2 bytes
4423 *** extra field length 2 bytes
4426 typedef struct ZipfileLFH ZipfileLFH;
4428 u16 iVersionExtract;
4440 typedef struct ZipfileEntry ZipfileEntry;
4441 struct ZipfileEntry {
4442 ZipfileCDS cds; /* Parsed CDS record */
4443 u32 mUnixTime; /* Modification time, in UNIX format */
4444 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4445 i64 iDataOff; /* Offset to data in file (if aData==0) */
4446 u8 *aData; /* cds.szCompressed bytes of compressed data */
4447 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4451 ** Cursor type for zipfile tables.
4453 typedef struct ZipfileCsr ZipfileCsr;
4455 sqlite3_vtab_cursor base; /* Base class - must be first */
4456 i64 iId; /* Cursor ID */
4457 u8 bEof; /* True when at EOF */
4458 u8 bNoop; /* If next xNext() call is no-op */
4460 /* Used outside of write transactions */
4461 FILE *pFile; /* Zip file */
4462 i64 iNextOff; /* Offset of next record in central directory */
4463 ZipfileEOCD eocd; /* Parse of central directory record */
4465 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4466 ZipfileEntry *pCurrent; /* Current entry */
4467 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4470 typedef struct ZipfileTab ZipfileTab;
4472 sqlite3_vtab base; /* Base class - must be first */
4473 char *zFile; /* Zip file this table accesses (may be NULL) */
4474 sqlite3 *db; /* Host database connection */
4475 u8 *aBuffer; /* Temporary buffer used for various tasks */
4477 ZipfileCsr *pCsrList; /* List of cursors */
4480 /* The following are used by write transactions only */
4481 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4482 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
4483 FILE *pWriteFd; /* File handle open on zip archive */
4484 i64 szCurrent; /* Current size of zip archive */
4485 i64 szOrig; /* Size of archive at start of transaction */
4489 ** Set the error message contained in context ctx to the results of
4490 ** vprintf(zFmt, ...).
4492 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4496 zMsg = sqlite3_vmprintf(zFmt, ap);
4497 sqlite3_result_error(ctx, zMsg, -1);
4503 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4504 ** is not quoted, do nothing.
4506 static void zipfileDequote(char *zIn){
4508 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4511 if( q=='[' ) q = ']';
4512 while( ALWAYS(zIn[iIn]) ){
4513 char c = zIn[iIn++];
4514 if( c==q && zIn[iIn++]!=q ) break;
4522 ** Construct a new ZipfileTab virtual table object.
4524 ** argv[0] -> module name ("zipfile")
4525 ** argv[1] -> database name
4526 ** argv[2] -> table name
4527 ** argv[...] -> "column name" and other module argument fields.
4529 static int zipfileConnect(
4532 int argc, const char *const*argv,
4533 sqlite3_vtab **ppVtab,
4536 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4538 const char *zFile = 0;
4539 ZipfileTab *pNew = 0;
4542 /* If the table name is not "zipfile", require that the argument be
4543 ** specified. This stops zipfile tables from being created as:
4545 ** CREATE VIRTUAL TABLE zzz USING zipfile();
4547 ** It does not prevent:
4549 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4551 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4552 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4553 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4554 return SQLITE_ERROR;
4559 nFile = (int)strlen(zFile)+1;
4562 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4563 if( rc==SQLITE_OK ){
4564 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4565 if( pNew==0 ) return SQLITE_NOMEM;
4566 memset(pNew, 0, nByte+nFile);
4568 pNew->aBuffer = (u8*)&pNew[1];
4570 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4571 memcpy(pNew->zFile, zFile, nFile);
4572 zipfileDequote(pNew->zFile);
4575 *ppVtab = (sqlite3_vtab*)pNew;
4580 ** Free the ZipfileEntry structure indicated by the only argument.
4582 static void zipfileEntryFree(ZipfileEntry *p){
4584 sqlite3_free(p->cds.zFile);
4590 ** Release resources that should be freed at the end of a write
4593 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4594 ZipfileEntry *pEntry;
4595 ZipfileEntry *pNext;
4597 if( pTab->pWriteFd ){
4598 fclose(pTab->pWriteFd);
4601 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4602 pNext = pEntry->pNext;
4603 zipfileEntryFree(pEntry);
4605 pTab->pFirstEntry = 0;
4606 pTab->pLastEntry = 0;
4607 pTab->szCurrent = 0;
4612 ** This method is the destructor for zipfile vtab objects.
4614 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4615 zipfileCleanupTransaction((ZipfileTab*)pVtab);
4616 sqlite3_free(pVtab);
4621 ** Constructor for a new ZipfileCsr object.
4623 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4624 ZipfileTab *pTab = (ZipfileTab*)p;
4626 pCsr = sqlite3_malloc(sizeof(*pCsr));
4627 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4629 return SQLITE_NOMEM;
4631 memset(pCsr, 0, sizeof(*pCsr));
4632 pCsr->iId = ++pTab->iNextCsrid;
4633 pCsr->pCsrNext = pTab->pCsrList;
4634 pTab->pCsrList = pCsr;
4639 ** Reset a cursor back to the state it was in when first returned
4640 ** by zipfileOpen().
4642 static void zipfileResetCursor(ZipfileCsr *pCsr){
4644 ZipfileEntry *pNext;
4648 fclose(pCsr->pFile);
4650 zipfileEntryFree(pCsr->pCurrent);
4654 for(p=pCsr->pFreeEntry; p; p=pNext){
4656 zipfileEntryFree(p);
4661 ** Destructor for an ZipfileCsr.
4663 static int zipfileClose(sqlite3_vtab_cursor *cur){
4664 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4665 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4667 zipfileResetCursor(pCsr);
4669 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4670 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4671 *pp = pCsr->pCsrNext;
4678 ** Set the error message for the virtual table associated with cursor
4679 ** pCsr to the results of vprintf(zFmt, ...).
4681 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4684 sqlite3_free(pTab->base.zErrMsg);
4685 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4688 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4691 sqlite3_free(pCsr->base.pVtab->zErrMsg);
4692 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4697 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4698 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4701 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4702 ** to an English language error message. It is the responsibility of the
4703 ** caller to eventually free this buffer using
4706 static int zipfileReadData(
4707 FILE *pFile, /* Read from this file */
4708 u8 *aRead, /* Read into this buffer */
4709 int nRead, /* Number of bytes to read */
4710 i64 iOff, /* Offset to read from */
4711 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
4714 fseek(pFile, (long)iOff, SEEK_SET);
4715 n = fread(aRead, 1, nRead, pFile);
4716 if( (int)n!=nRead ){
4717 *pzErrmsg = sqlite3_mprintf("error in fread()");
4718 return SQLITE_ERROR;
4723 static int zipfileAppendData(
4729 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4730 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4731 if( (int)n!=nWrite ){
4732 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4733 return SQLITE_ERROR;
4735 pTab->szCurrent += nWrite;
4740 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4742 static u16 zipfileGetU16(const u8 *aBuf){
4743 return (aBuf[1] << 8) + aBuf[0];
4747 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4749 static u32 zipfileGetU32(const u8 *aBuf){
4750 return ((u32)(aBuf[3]) << 24)
4751 + ((u32)(aBuf[2]) << 16)
4752 + ((u32)(aBuf[1]) << 8)
4753 + ((u32)(aBuf[0]) << 0);
4757 ** Write a 16-bit little endiate integer into buffer aBuf.
4759 static void zipfilePutU16(u8 *aBuf, u16 val){
4760 aBuf[0] = val & 0xFF;
4761 aBuf[1] = (val>>8) & 0xFF;
4765 ** Write a 32-bit little endiate integer into buffer aBuf.
4767 static void zipfilePutU32(u8 *aBuf, u32 val){
4768 aBuf[0] = val & 0xFF;
4769 aBuf[1] = (val>>8) & 0xFF;
4770 aBuf[2] = (val>>16) & 0xFF;
4771 aBuf[3] = (val>>24) & 0xFF;
4774 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4775 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4777 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4778 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4781 ** Magic numbers used to read CDS records.
4783 #define ZIPFILE_CDS_NFILE_OFF 28
4784 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4787 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4788 ** if the record is not well-formed, or SQLITE_OK otherwise.
4790 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4792 u32 sig = zipfileRead32(aRead);
4794 if( sig!=ZIPFILE_SIGNATURE_CDS ){
4797 pCDS->iVersionMadeBy = zipfileRead16(aRead);
4798 pCDS->iVersionExtract = zipfileRead16(aRead);
4799 pCDS->flags = zipfileRead16(aRead);
4800 pCDS->iCompression = zipfileRead16(aRead);
4801 pCDS->mTime = zipfileRead16(aRead);
4802 pCDS->mDate = zipfileRead16(aRead);
4803 pCDS->crc32 = zipfileRead32(aRead);
4804 pCDS->szCompressed = zipfileRead32(aRead);
4805 pCDS->szUncompressed = zipfileRead32(aRead);
4806 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4807 pCDS->nFile = zipfileRead16(aRead);
4808 pCDS->nExtra = zipfileRead16(aRead);
4809 pCDS->nComment = zipfileRead16(aRead);
4810 pCDS->iDiskStart = zipfileRead16(aRead);
4811 pCDS->iInternalAttr = zipfileRead16(aRead);
4812 pCDS->iExternalAttr = zipfileRead32(aRead);
4813 pCDS->iOffset = zipfileRead32(aRead);
4814 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4821 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
4822 ** if the record is not well-formed, or SQLITE_OK otherwise.
4824 static int zipfileReadLFH(
4828 u8 *aRead = aBuffer;
4831 u32 sig = zipfileRead32(aRead);
4832 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4835 pLFH->iVersionExtract = zipfileRead16(aRead);
4836 pLFH->flags = zipfileRead16(aRead);
4837 pLFH->iCompression = zipfileRead16(aRead);
4838 pLFH->mTime = zipfileRead16(aRead);
4839 pLFH->mDate = zipfileRead16(aRead);
4840 pLFH->crc32 = zipfileRead32(aRead);
4841 pLFH->szCompressed = zipfileRead32(aRead);
4842 pLFH->szUncompressed = zipfileRead32(aRead);
4843 pLFH->nFile = zipfileRead16(aRead);
4844 pLFH->nExtra = zipfileRead16(aRead);
4851 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
4852 ** Scan through this buffer to find an "extra-timestamp" field. If one
4853 ** exists, extract the 32-bit modification-timestamp from it and store
4854 ** the value in output parameter *pmTime.
4856 ** Zero is returned if no extra-timestamp record could be found (and so
4857 ** *pmTime is left unchanged), or non-zero otherwise.
4859 ** The general format of an extra field is:
4861 ** Header ID 2 bytes
4862 ** Data Size 2 bytes
4865 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
4868 u8 *pEnd = &aExtra[nExtra];
4871 u16 id = zipfileRead16(p);
4872 u16 nByte = zipfileRead16(p);
4875 case ZIPFILE_EXTRA_TIMESTAMP: {
4877 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4878 *pmTime = zipfileGetU32(&p[1]);
4891 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
4892 ** fields of the CDS structure passed as the only argument to a 32-bit
4893 ** UNIX seconds-since-the-epoch timestamp. Return the result.
4895 ** "Standard" MS-DOS time format:
4897 ** File modification time:
4898 ** Bits 00-04: seconds divided by 2
4899 ** Bits 05-10: minute
4901 ** File modification date:
4903 ** Bits 05-08: month (1-12)
4904 ** Bits 09-15: years from 1980
4906 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
4908 static u32 zipfileMtime(ZipfileCDS *pCDS){
4909 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
4910 int M = ((pCDS->mDate >> 5) & 0x0F);
4911 int D = (pCDS->mDate & 0x1F);
4914 int sec = (pCDS->mTime & 0x1F)*2;
4915 int min = (pCDS->mTime >> 5) & 0x3F;
4916 int hr = (pCDS->mTime >> 11) & 0x1F;
4919 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
4921 /* Calculate the JD in seconds for noon on the day in question */
4926 JD = (i64)(24*60*60) * (
4927 (int)(365.25 * (Y + 4716))
4928 + (int)(30.6001 * (M + 1))
4932 /* Correct the JD for the time within the day */
4933 JD += (hr-12) * 3600 + min * 60 + sec;
4935 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
4936 return (u32)(JD - (i64)(24405875) * 24*60*6);
4940 ** The opposite of zipfileMtime(). This function populates the mTime and
4941 ** mDate fields of the CDS structure passed as the first argument according
4942 ** to the UNIX timestamp value passed as the second.
4944 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
4945 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
4946 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
4952 A = (int)((JD - 1867216.25)/36524.25);
4953 A = (int)(JD + 1 + A - (A/4));
4955 C = (int)((B - 122.1)/365.25);
4956 D = (36525*(C&32767))/100;
4957 E = (int)((B-D)/30.6001);
4959 day = B - D - (int)(30.6001*E);
4960 mon = (E<14 ? E-1 : E-13);
4961 yr = mon>2 ? C-4716 : C-4715;
4963 hr = (mUnixTime % (24*60*60)) / (60*60);
4964 min = (mUnixTime % (60*60)) / 60;
4965 sec = (mUnixTime % 60);
4968 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
4969 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
4971 pCds->mDate = pCds->mTime = 0;
4974 assert( mUnixTime<315507600
4975 || mUnixTime==zipfileMtime(pCds)
4976 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
4977 /* || (mUnixTime % 2) */
4982 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
4983 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
4984 ** then pFile is a file-handle open on a zip file. In either case, this
4985 ** function creates a ZipfileEntry object based on the zip archive entry
4986 ** for which the CDS record is at offset iOff.
4988 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
4989 ** the new object. Otherwise, an SQLite error code is returned and the
4990 ** final value of (*ppEntry) undefined.
4992 static int zipfileGetEntry(
4993 ZipfileTab *pTab, /* Store any error message here */
4994 const u8 *aBlob, /* Pointer to in-memory file image */
4995 int nBlob, /* Size of aBlob[] in bytes */
4996 FILE *pFile, /* If aBlob==0, read from this file */
4997 i64 iOff, /* Offset of CDS record */
4998 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
5001 char **pzErr = &pTab->base.zErrMsg;
5005 aRead = pTab->aBuffer;
5006 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5008 aRead = (u8*)&aBlob[iOff];
5011 if( rc==SQLITE_OK ){
5012 sqlite3_int64 nAlloc;
5015 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5016 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5017 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5019 nAlloc = sizeof(ZipfileEntry) + nExtra;
5021 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5024 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5028 memset(pNew, 0, sizeof(ZipfileEntry));
5029 rc = zipfileReadCDS(aRead, &pNew->cds);
5030 if( rc!=SQLITE_OK ){
5031 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5032 }else if( aBlob==0 ){
5033 rc = zipfileReadData(
5034 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5037 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5041 if( rc==SQLITE_OK ){
5042 u32 *pt = &pNew->mUnixTime;
5043 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
5044 pNew->aExtra = (u8*)&pNew[1];
5045 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5046 if( pNew->cds.zFile==0 ){
5048 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5049 pNew->mUnixTime = zipfileMtime(&pNew->cds);
5053 if( rc==SQLITE_OK ){
5054 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5057 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5059 aRead = (u8*)&aBlob[pNew->cds.iOffset];
5062 rc = zipfileReadLFH(aRead, &lfh);
5063 if( rc==SQLITE_OK ){
5064 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5065 pNew->iDataOff += lfh.nFile + lfh.nExtra;
5066 if( aBlob && pNew->cds.szCompressed ){
5067 pNew->aData = &pNew->aExtra[nExtra];
5068 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5071 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
5072 (int)pNew->cds.iOffset
5077 if( rc!=SQLITE_OK ){
5078 zipfileEntryFree(pNew);
5088 ** Advance an ZipfileCsr to its next row of output.
5090 static int zipfileNext(sqlite3_vtab_cursor *cur){
5091 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5095 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5096 zipfileEntryFree(pCsr->pCurrent);
5098 if( pCsr->iNextOff>=iEof ){
5101 ZipfileEntry *p = 0;
5102 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5103 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5104 if( rc==SQLITE_OK ){
5105 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5106 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5112 pCsr->pCurrent = pCsr->pCurrent->pNext;
5114 if( pCsr->pCurrent==0 ){
5123 static void zipfileFree(void *p) {
5128 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5129 ** size is nOut bytes. This function uncompresses the data and sets the
5130 ** return value in context pCtx to the result (a blob).
5132 ** If an error occurs, an error code is left in pCtx instead.
5134 static void zipfileInflate(
5135 sqlite3_context *pCtx, /* Store result here */
5136 const u8 *aIn, /* Compressed data */
5137 int nIn, /* Size of buffer aIn[] in bytes */
5138 int nOut /* Expected output size */
5140 u8 *aRes = sqlite3_malloc(nOut);
5142 sqlite3_result_error_nomem(pCtx);
5146 memset(&str, 0, sizeof(str));
5148 str.next_in = (Byte*)aIn;
5150 str.next_out = (Byte*)aRes;
5151 str.avail_out = nOut;
5153 err = inflateInit2(&str, -15);
5155 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5157 err = inflate(&str, Z_NO_FLUSH);
5158 if( err!=Z_STREAM_END ){
5159 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5161 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5171 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5172 ** compresses it and sets (*ppOut) to point to a buffer containing the
5173 ** compressed data. The caller is responsible for eventually calling
5174 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
5175 ** is set to the size of buffer (*ppOut) in bytes.
5177 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5178 ** code is returned and an error message left in virtual-table handle
5179 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5182 static int zipfileDeflate(
5183 const u8 *aIn, int nIn, /* Input */
5184 u8 **ppOut, int *pnOut, /* Output */
5185 char **pzErr /* OUT: Error message */
5187 sqlite3_int64 nAlloc = compressBound(nIn);
5191 aOut = (u8*)sqlite3_malloc64(nAlloc);
5197 memset(&str, 0, sizeof(str));
5198 str.next_in = (Bytef*)aIn;
5200 str.next_out = aOut;
5201 str.avail_out = nAlloc;
5203 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5204 res = deflate(&str, Z_FINISH);
5206 if( res==Z_STREAM_END ){
5208 *pnOut = (int)str.total_out;
5211 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5222 ** Return values of columns for the row at which the series_cursor
5223 ** is currently pointing.
5225 static int zipfileColumn(
5226 sqlite3_vtab_cursor *cur, /* The cursor */
5227 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5228 int i /* Which column to return */
5230 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5231 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5235 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5238 /* TODO: Whether or not the following is correct surely depends on
5239 ** the platform on which the archive was created. */
5240 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5242 case 2: { /* mtime */
5243 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5247 if( sqlite3_vtab_nochange(ctx)==0 ){
5248 sqlite3_result_int64(ctx, pCDS->szUncompressed);
5252 case 4: /* rawdata */
5253 if( sqlite3_vtab_nochange(ctx) ) break;
5254 case 5: { /* data */
5255 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5256 int sz = pCDS->szCompressed;
5257 int szFinal = pCDS->szUncompressed;
5261 if( pCsr->pCurrent->aData ){
5262 aBuf = pCsr->pCurrent->aData;
5264 aBuf = aFree = sqlite3_malloc64(sz);
5268 FILE *pFile = pCsr->pFile;
5270 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5272 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5273 &pCsr->base.pVtab->zErrMsg
5277 if( rc==SQLITE_OK ){
5278 if( i==5 && pCDS->iCompression ){
5279 zipfileInflate(ctx, aBuf, sz, szFinal);
5281 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5284 sqlite3_free(aFree);
5286 /* Figure out if this is a directory or a zero-sized file. Consider
5287 ** it to be a directory either if the mode suggests so, or if
5288 ** the final character in the name is '/'. */
5289 u32 mode = pCDS->iExternalAttr >> 16;
5290 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5291 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5297 case 6: /* method */
5298 sqlite3_result_int(ctx, pCDS->iCompression);
5302 sqlite3_result_int64(ctx, pCsr->iId);
5310 ** Return TRUE if the cursor is at EOF.
5312 static int zipfileEof(sqlite3_vtab_cursor *cur){
5313 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5318 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5319 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5320 ** is guaranteed to be a file-handle open on a zip file.
5322 ** This function attempts to locate the EOCD record within the zip archive
5323 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5324 ** returned if successful. Otherwise, an SQLite error code is returned and
5325 ** an English language error message may be left in virtual-table pTab.
5327 static int zipfileReadEOCD(
5328 ZipfileTab *pTab, /* Return errors here */
5329 const u8 *aBlob, /* Pointer to in-memory file image */
5330 int nBlob, /* Size of aBlob[] in bytes */
5331 FILE *pFile, /* Read from this file if aBlob==0 */
5332 ZipfileEOCD *pEOCD /* Object to populate */
5334 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
5335 int nRead; /* Bytes to read from file */
5339 i64 iOff; /* Offset to read from */
5340 i64 szFile; /* Total size of file in bytes */
5341 fseek(pFile, 0, SEEK_END);
5342 szFile = (i64)ftell(pFile);
5344 memset(pEOCD, 0, sizeof(ZipfileEOCD));
5347 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5348 iOff = szFile - nRead;
5349 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5351 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5352 aRead = (u8*)&aBlob[nBlob-nRead];
5355 if( rc==SQLITE_OK ){
5358 /* Scan backwards looking for the signature bytes */
5359 for(i=nRead-20; i>=0; i--){
5360 if( aRead[i]==0x50 && aRead[i+1]==0x4b
5361 && aRead[i+2]==0x05 && aRead[i+3]==0x06
5367 pTab->base.zErrMsg = sqlite3_mprintf(
5368 "cannot find end of central directory record"
5370 return SQLITE_ERROR;
5374 pEOCD->iDisk = zipfileRead16(aRead);
5375 pEOCD->iFirstDisk = zipfileRead16(aRead);
5376 pEOCD->nEntry = zipfileRead16(aRead);
5377 pEOCD->nEntryTotal = zipfileRead16(aRead);
5378 pEOCD->nSize = zipfileRead32(aRead);
5379 pEOCD->iOffset = zipfileRead32(aRead);
5386 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5387 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5388 ** to the end of the list. Otherwise, it is added to the list immediately
5389 ** before pBefore (which is guaranteed to be a part of said list).
5391 static void zipfileAddEntry(
5393 ZipfileEntry *pBefore,
5396 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5397 assert( pNew->pNext==0 );
5399 if( pTab->pFirstEntry==0 ){
5400 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5402 assert( pTab->pLastEntry->pNext==0 );
5403 pTab->pLastEntry->pNext = pNew;
5404 pTab->pLastEntry = pNew;
5408 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5409 pNew->pNext = pBefore;
5414 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5420 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5421 iOff = eocd.iOffset;
5422 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5423 ZipfileEntry *pNew = 0;
5424 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5426 if( rc==SQLITE_OK ){
5427 zipfileAddEntry(pTab, 0, pNew);
5428 iOff += ZIPFILE_CDS_FIXED_SZ;
5429 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5436 ** xFilter callback.
5438 static int zipfileFilter(
5439 sqlite3_vtab_cursor *cur,
5440 int idxNum, const char *idxStr,
5441 int argc, sqlite3_value **argv
5443 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5444 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5445 const char *zFile = 0; /* Zip file to scan */
5446 int rc = SQLITE_OK; /* Return Code */
5447 int bInMemory = 0; /* True for an in-memory zipfile */
5449 zipfileResetCursor(pCsr);
5452 zFile = pTab->zFile;
5453 }else if( idxNum==0 ){
5454 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5455 return SQLITE_ERROR;
5456 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5457 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5458 int nBlob = sqlite3_value_bytes(argv[0]);
5459 assert( pTab->pFirstEntry==0 );
5460 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5461 pCsr->pFreeEntry = pTab->pFirstEntry;
5462 pTab->pFirstEntry = pTab->pLastEntry = 0;
5463 if( rc!=SQLITE_OK ) return rc;
5466 zFile = (const char*)sqlite3_value_text(argv[0]);
5469 if( 0==pTab->pWriteFd && 0==bInMemory ){
5470 pCsr->pFile = fopen(zFile, "rb");
5471 if( pCsr->pFile==0 ){
5472 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5475 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5476 if( rc==SQLITE_OK ){
5477 if( pCsr->eocd.nEntry==0 ){
5480 pCsr->iNextOff = pCsr->eocd.iOffset;
5481 rc = zipfileNext(cur);
5487 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5488 rc = zipfileNext(cur);
5495 ** xBestIndex callback.
5497 static int zipfileBestIndex(
5499 sqlite3_index_info *pIdxInfo
5505 for(i=0; i<pIdxInfo->nConstraint; i++){
5506 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5507 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5508 if( pCons->usable==0 ){
5510 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5515 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5516 pIdxInfo->aConstraintUsage[idx].omit = 1;
5517 pIdxInfo->estimatedCost = 1000.0;
5518 pIdxInfo->idxNum = 1;
5519 }else if( unusable ){
5520 return SQLITE_CONSTRAINT;
5525 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5527 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5529 memset(pNew, 0, sizeof(ZipfileEntry));
5530 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5531 if( pNew->cds.zFile==0 ){
5539 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5540 ZipfileCDS *pCds = &pEntry->cds;
5545 /* Write the LFH itself */
5546 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5547 zipfileWrite16(a, pCds->iVersionExtract);
5548 zipfileWrite16(a, pCds->flags);
5549 zipfileWrite16(a, pCds->iCompression);
5550 zipfileWrite16(a, pCds->mTime);
5551 zipfileWrite16(a, pCds->mDate);
5552 zipfileWrite32(a, pCds->crc32);
5553 zipfileWrite32(a, pCds->szCompressed);
5554 zipfileWrite32(a, pCds->szUncompressed);
5555 zipfileWrite16(a, (u16)pCds->nFile);
5556 zipfileWrite16(a, pCds->nExtra);
5557 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5559 /* Add the file name */
5560 memcpy(a, pCds->zFile, (int)pCds->nFile);
5561 a += (int)pCds->nFile;
5563 /* The "extra" data */
5564 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5565 zipfileWrite16(a, 5);
5567 zipfileWrite32(a, pEntry->mUnixTime);
5572 static int zipfileAppendEntry(
5574 ZipfileEntry *pEntry,
5578 u8 *aBuf = pTab->aBuffer;
5582 nBuf = zipfileSerializeLFH(pEntry, aBuf);
5583 rc = zipfileAppendData(pTab, aBuf, nBuf);
5584 if( rc==SQLITE_OK ){
5585 pEntry->iDataOff = pTab->szCurrent;
5586 rc = zipfileAppendData(pTab, pData, nData);
5592 static int zipfileGetMode(
5593 sqlite3_value *pVal,
5594 int bIsDir, /* If true, default to directory */
5595 u32 *pMode, /* OUT: Mode value */
5596 char **pzErr /* OUT: Error message */
5598 const char *z = (const char*)sqlite3_value_text(pVal);
5601 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5602 }else if( z[0]>='0' && z[0]<='9' ){
5603 mode = (unsigned int)sqlite3_value_int(pVal);
5605 const char zTemplate[11] = "-rwxrwxrwx";
5607 if( strlen(z)!=10 ) goto parse_error;
5609 case '-': mode |= S_IFREG; break;
5610 case 'd': mode |= S_IFDIR; break;
5611 case 'l': mode |= S_IFLNK; break;
5612 default: goto parse_error;
5614 for(i=1; i<10; i++){
5615 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5616 else if( z[i]!='-' ) goto parse_error;
5619 if( ((mode & S_IFDIR)==0)==bIsDir ){
5620 /* The "mode" attribute is a directory, but data has been specified.
5621 ** Or vice-versa - no data but "mode" is a file or symlink. */
5622 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5623 return SQLITE_CONSTRAINT;
5629 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5630 return SQLITE_ERROR;
5634 ** Both (const char*) arguments point to nul-terminated strings. Argument
5635 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5636 ** identical, ignoring any trailing '/' character in either path. */
5637 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5638 int nA = (int)strlen(zA);
5639 if( zA[nA-1]=='/' ) nA--;
5640 if( zB[nB-1]=='/' ) nB--;
5641 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5645 static int zipfileBegin(sqlite3_vtab *pVtab){
5646 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5649 assert( pTab->pWriteFd==0 );
5651 /* Open a write fd on the file. Also load the entire central directory
5652 ** structure into memory. During the transaction any new file data is
5653 ** appended to the archive file, but the central directory is accumulated
5654 ** in main-memory until the transaction is committed. */
5655 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5656 if( pTab->pWriteFd==0 ){
5657 pTab->base.zErrMsg = sqlite3_mprintf(
5658 "zipfile: failed to open file %s for writing", pTab->zFile
5662 fseek(pTab->pWriteFd, 0, SEEK_END);
5663 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5664 rc = zipfileLoadDirectory(pTab, 0, 0);
5667 if( rc!=SQLITE_OK ){
5668 zipfileCleanupTransaction(pTab);
5675 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5678 static u32 zipfileTime(void){
5679 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5681 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5683 pVfs->xCurrentTimeInt64(pVfs, &ms);
5684 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5687 pVfs->xCurrentTime(pVfs, &day);
5688 ret = (u32)((day - 2440587.5) * 86400);
5694 ** Return a 32-bit timestamp in UNIX epoch format.
5696 ** If the value passed as the only argument is either NULL or an SQL NULL,
5697 ** return the current time. Otherwise, return the value stored in (*pVal)
5698 ** cast to a 32-bit unsigned integer.
5700 static u32 zipfileGetTime(sqlite3_value *pVal){
5701 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5702 return zipfileTime();
5704 return (u32)sqlite3_value_int64(pVal);
5708 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5709 ** linked list. Remove it from the list and free the object.
5711 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5714 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5716 zipfileEntryFree(pOld);
5723 static int zipfileUpdate(
5724 sqlite3_vtab *pVtab,
5726 sqlite3_value **apVal,
5727 sqlite_int64 *pRowid
5729 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5730 int rc = SQLITE_OK; /* Return Code */
5731 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5733 u32 mode = 0; /* Mode for new entry */
5734 u32 mTime = 0; /* Modification time for new entry */
5735 i64 sz = 0; /* Uncompressed size */
5736 const char *zPath = 0; /* Path for new entry */
5737 int nPath = 0; /* strlen(zPath) */
5738 const u8 *pData = 0; /* Pointer to buffer containing content */
5739 int nData = 0; /* Size of pData buffer in bytes */
5740 int iMethod = 0; /* Compression method for new entry */
5741 u8 *pFree = 0; /* Free this */
5742 char *zFree = 0; /* Also free this */
5743 ZipfileEntry *pOld = 0;
5744 ZipfileEntry *pOld2 = 0;
5745 int bUpdate = 0; /* True for an update that modifies "name" */
5749 if( pTab->pWriteFd==0 ){
5750 rc = zipfileBegin(pVtab);
5751 if( rc!=SQLITE_OK ) return rc;
5754 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5755 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5756 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5757 int nDelete = (int)strlen(zDelete);
5759 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5760 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5764 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5765 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5768 assert( pOld->pNext );
5773 /* Check that "sz" and "rawdata" are both NULL: */
5774 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5775 zipfileTableErr(pTab, "sz must be NULL");
5776 rc = SQLITE_CONSTRAINT;
5778 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5779 zipfileTableErr(pTab, "rawdata must be NULL");
5780 rc = SQLITE_CONSTRAINT;
5783 if( rc==SQLITE_OK ){
5784 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5785 /* data=NULL. A directory */
5788 /* Value specified for "data", and possibly "method". This must be
5789 ** a regular file or a symlink. */
5790 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5791 int nIn = sqlite3_value_bytes(apVal[7]);
5792 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5794 iMethod = sqlite3_value_int(apVal[8]);
5798 if( iMethod!=0 && iMethod!=8 ){
5799 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5800 rc = SQLITE_CONSTRAINT;
5802 if( bAuto || iMethod ){
5804 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5805 if( rc==SQLITE_OK ){
5806 if( iMethod || nCmp<nIn ){
5813 iCrc32 = crc32(0, aIn, nIn);
5818 if( rc==SQLITE_OK ){
5819 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
5822 if( rc==SQLITE_OK ){
5823 zPath = (const char*)sqlite3_value_text(apVal[2]);
5824 nPath = (int)strlen(zPath);
5825 mTime = zipfileGetTime(apVal[4]);
5828 if( rc==SQLITE_OK && bIsDir ){
5829 /* For a directory, check that the last character in the path is a
5830 ** '/'. This appears to be required for compatibility with info-zip
5831 ** (the unzip command on unix). It does not create directories
5833 if( zPath[nPath-1]!='/' ){
5834 zFree = sqlite3_mprintf("%s/", zPath);
5835 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5836 zPath = (const char*)zFree;
5841 /* Check that we're not inserting a duplicate entry -OR- updating an
5842 ** entry with a path, thereby making it into a duplicate. */
5843 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
5845 for(p=pTab->pFirstEntry; p; p=p->pNext){
5846 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
5847 switch( sqlite3_vtab_on_conflict(pTab->db) ){
5848 case SQLITE_IGNORE: {
5849 goto zipfile_update_done;
5851 case SQLITE_REPLACE: {
5856 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
5857 rc = SQLITE_CONSTRAINT;
5866 if( rc==SQLITE_OK ){
5867 /* Create the new CDS record. */
5868 pNew = zipfileNewEntry(zPath);
5872 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5873 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5874 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5875 pNew->cds.iCompression = (u16)iMethod;
5876 zipfileMtimeToDos(&pNew->cds, mTime);
5877 pNew->cds.crc32 = iCrc32;
5878 pNew->cds.szCompressed = nData;
5879 pNew->cds.szUncompressed = (u32)sz;
5880 pNew->cds.iExternalAttr = (mode<<16);
5881 pNew->cds.iOffset = (u32)pTab->szCurrent;
5882 pNew->cds.nFile = (u16)nPath;
5883 pNew->mUnixTime = (u32)mTime;
5884 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
5885 zipfileAddEntry(pTab, pOld, pNew);
5890 if( rc==SQLITE_OK && (pOld || pOld2) ){
5892 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5893 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
5894 pCsr->pCurrent = pCsr->pCurrent->pNext;
5899 zipfileRemoveEntryFromList(pTab, pOld);
5900 zipfileRemoveEntryFromList(pTab, pOld2);
5903 zipfile_update_done:
5904 sqlite3_free(pFree);
5905 sqlite3_free(zFree);
5909 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
5911 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
5912 zipfileWrite16(a, p->iDisk);
5913 zipfileWrite16(a, p->iFirstDisk);
5914 zipfileWrite16(a, p->nEntry);
5915 zipfileWrite16(a, p->nEntryTotal);
5916 zipfileWrite32(a, p->nSize);
5917 zipfileWrite32(a, p->iOffset);
5918 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
5923 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5924 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
5925 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
5926 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
5930 ** Serialize the CDS structure into buffer aBuf[]. Return the number
5931 ** of bytes written.
5933 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
5935 ZipfileCDS *pCDS = &pEntry->cds;
5937 if( pEntry->aExtra==0 ){
5941 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
5942 zipfileWrite16(a, pCDS->iVersionMadeBy);
5943 zipfileWrite16(a, pCDS->iVersionExtract);
5944 zipfileWrite16(a, pCDS->flags);
5945 zipfileWrite16(a, pCDS->iCompression);
5946 zipfileWrite16(a, pCDS->mTime);
5947 zipfileWrite16(a, pCDS->mDate);
5948 zipfileWrite32(a, pCDS->crc32);
5949 zipfileWrite32(a, pCDS->szCompressed);
5950 zipfileWrite32(a, pCDS->szUncompressed);
5951 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
5952 zipfileWrite16(a, pCDS->nFile);
5953 zipfileWrite16(a, pCDS->nExtra);
5954 zipfileWrite16(a, pCDS->nComment);
5955 zipfileWrite16(a, pCDS->iDiskStart);
5956 zipfileWrite16(a, pCDS->iInternalAttr);
5957 zipfileWrite32(a, pCDS->iExternalAttr);
5958 zipfileWrite32(a, pCDS->iOffset);
5960 memcpy(a, pCDS->zFile, pCDS->nFile);
5963 if( pEntry->aExtra ){
5964 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
5965 memcpy(a, pEntry->aExtra, n);
5968 assert( pCDS->nExtra==9 );
5969 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5970 zipfileWrite16(a, 5);
5972 zipfileWrite32(a, pEntry->mUnixTime);
5978 static int zipfileCommit(sqlite3_vtab *pVtab){
5979 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5981 if( pTab->pWriteFd ){
5982 i64 iOffset = pTab->szCurrent;
5987 /* Write out all entries */
5988 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5989 int n = zipfileSerializeCDS(p, pTab->aBuffer);
5990 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
5994 /* Write out the EOCD record */
5996 eocd.iFirstDisk = 0;
5997 eocd.nEntry = (u16)nEntry;
5998 eocd.nEntryTotal = (u16)nEntry;
5999 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6000 eocd.iOffset = (u32)iOffset;
6001 rc = zipfileAppendEOCD(pTab, &eocd);
6003 zipfileCleanupTransaction(pTab);
6008 static int zipfileRollback(sqlite3_vtab *pVtab){
6009 return zipfileCommit(pVtab);
6012 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6014 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6015 if( iId==pCsr->iId ) break;
6020 static void zipfileFunctionCds(
6021 sqlite3_context *context,
6023 sqlite3_value **argv
6026 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6029 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6031 ZipfileCDS *p = &pCsr->pCurrent->cds;
6032 char *zRes = sqlite3_mprintf("{"
6033 "\"version-made-by\" : %u, "
6034 "\"version-to-extract\" : %u, "
6036 "\"compression\" : %u, "
6040 "\"compressed-size\" : %u, "
6041 "\"uncompressed-size\" : %u, "
6042 "\"file-name-length\" : %u, "
6043 "\"extra-field-length\" : %u, "
6044 "\"file-comment-length\" : %u, "
6045 "\"disk-number-start\" : %u, "
6046 "\"internal-attr\" : %u, "
6047 "\"external-attr\" : %u, "
6048 "\"offset\" : %u }",
6049 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6050 (u32)p->flags, (u32)p->iCompression,
6051 (u32)p->mTime, (u32)p->mDate,
6052 (u32)p->crc32, (u32)p->szCompressed,
6053 (u32)p->szUncompressed, (u32)p->nFile,
6054 (u32)p->nExtra, (u32)p->nComment,
6055 (u32)p->iDiskStart, (u32)p->iInternalAttr,
6056 (u32)p->iExternalAttr, (u32)p->iOffset
6060 sqlite3_result_error_nomem(context);
6062 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6069 ** xFindFunction method.
6071 static int zipfileFindFunction(
6072 sqlite3_vtab *pVtab, /* Virtual table handle */
6073 int nArg, /* Number of SQL function arguments */
6074 const char *zName, /* Name of SQL function */
6075 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6076 void **ppArg /* OUT: User data for *pxFunc */
6078 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6079 *pxFunc = zipfileFunctionCds;
6080 *ppArg = (void*)pVtab;
6086 typedef struct ZipfileBuffer ZipfileBuffer;
6087 struct ZipfileBuffer {
6088 u8 *a; /* Pointer to buffer */
6089 int n; /* Size of buffer in bytes */
6090 int nAlloc; /* Byte allocated at a[] */
6093 typedef struct ZipfileCtx ZipfileCtx;
6100 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6101 if( pBuf->n+nByte>pBuf->nAlloc ){
6103 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6104 int nReq = pBuf->n + nByte;
6106 while( nNew<nReq ) nNew = nNew*2;
6107 aNew = sqlite3_realloc64(pBuf->a, nNew);
6108 if( aNew==0 ) return SQLITE_NOMEM;
6110 pBuf->nAlloc = (int)nNew;
6116 ** xStep() callback for the zipfile() aggregate. This can be called in
6117 ** any of the following ways:
6119 ** SELECT zipfile(name,data) ...
6120 ** SELECT zipfile(name,mode,mtime,data) ...
6121 ** SELECT zipfile(name,mode,mtime,data,method) ...
6123 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6124 ZipfileCtx *p; /* Aggregate function context */
6125 ZipfileEntry e; /* New entry to add to zip archive */
6127 sqlite3_value *pName = 0;
6128 sqlite3_value *pMode = 0;
6129 sqlite3_value *pMtime = 0;
6130 sqlite3_value *pData = 0;
6131 sqlite3_value *pMethod = 0;
6138 int iMethod = -1; /* Compression method to use (0 or 8) */
6140 const u8 *aData = 0; /* Possibly compressed data for new entry */
6141 int nData = 0; /* Size of aData[] in bytes */
6142 int szUncompressed = 0; /* Size of data before compression */
6143 u8 *aFree = 0; /* Free this before returning */
6144 u32 iCrc32 = 0; /* crc32 of uncompressed data */
6146 char *zName = 0; /* Path (name) of new entry */
6147 int nName = 0; /* Size of zName in bytes */
6148 char *zFree = 0; /* Free this before returning */
6151 memset(&e, 0, sizeof(e));
6152 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6155 /* Martial the arguments into stack variables */
6156 if( nVal!=2 && nVal!=4 && nVal!=5 ){
6157 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6159 goto zipfile_step_out;
6173 /* Check that the 'name' parameter looks ok. */
6174 zName = (char*)sqlite3_value_text(pName);
6175 nName = sqlite3_value_bytes(pName);
6177 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6179 goto zipfile_step_out;
6182 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6183 ** deflate compression) or NULL (choose automatically). */
6184 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6185 iMethod = (int)sqlite3_value_int64(pMethod);
6186 if( iMethod!=0 && iMethod!=8 ){
6187 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6189 goto zipfile_step_out;
6193 /* Now inspect the data. If this is NULL, then the new entry must be a
6194 ** directory. Otherwise, figure out whether or not the data should
6195 ** be deflated or simply stored in the zip archive. */
6196 if( sqlite3_value_type(pData)==SQLITE_NULL ){
6200 aData = sqlite3_value_blob(pData);
6201 szUncompressed = nData = sqlite3_value_bytes(pData);
6202 iCrc32 = crc32(0, aData, nData);
6203 if( iMethod<0 || iMethod==8 ){
6205 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6206 if( rc!=SQLITE_OK ){
6207 goto zipfile_step_out;
6209 if( iMethod==8 || nOut<nData ){
6219 /* Decode the "mode" argument. */
6220 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6221 if( rc ) goto zipfile_step_out;
6223 /* Decode the "mtime" argument. */
6224 e.mUnixTime = zipfileGetTime(pMtime);
6226 /* If this is a directory entry, ensure that there is exactly one '/'
6227 ** at the end of the path. Or, if this is not a directory and the path
6228 ** ends in '/' it is an error. */
6230 if( zName[nName-1]=='/' ){
6231 zErr = sqlite3_mprintf("non-directory name must not end with /");
6233 goto zipfile_step_out;
6236 if( zName[nName-1]!='/' ){
6237 zName = zFree = sqlite3_mprintf("%s/", zName);
6241 goto zipfile_step_out;
6244 while( nName>1 && zName[nName-2]=='/' ) nName--;
6248 /* Assemble the ZipfileEntry object for the new zip archive entry */
6249 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6250 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6251 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6252 e.cds.iCompression = (u16)iMethod;
6253 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6254 e.cds.crc32 = iCrc32;
6255 e.cds.szCompressed = nData;
6256 e.cds.szUncompressed = szUncompressed;
6257 e.cds.iExternalAttr = (mode<<16);
6258 e.cds.iOffset = p->body.n;
6259 e.cds.nFile = (u16)nName;
6260 e.cds.zFile = zName;
6262 /* Append the LFH to the body of the new archive */
6263 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6264 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6265 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6267 /* Append the data to the body of the new archive */
6269 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6270 memcpy(&p->body.a[p->body.n], aData, nData);
6274 /* Append the CDS record to the directory of the new archive */
6275 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6276 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6277 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6279 /* Increment the count of entries in the archive */
6283 sqlite3_free(aFree);
6284 sqlite3_free(zFree);
6287 sqlite3_result_error(pCtx, zErr, -1);
6289 sqlite3_result_error_code(pCtx, rc);
6296 ** xFinalize() callback for zipfile aggregate function.
6298 void zipfileFinal(sqlite3_context *pCtx){
6304 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6307 memset(&eocd, 0, sizeof(eocd));
6308 eocd.nEntry = (u16)p->nEntry;
6309 eocd.nEntryTotal = (u16)p->nEntry;
6310 eocd.nSize = p->cds.n;
6311 eocd.iOffset = p->body.n;
6313 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6314 aZip = (u8*)sqlite3_malloc64(nZip);
6316 sqlite3_result_error_nomem(pCtx);
6318 memcpy(aZip, p->body.a, p->body.n);
6319 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6320 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6321 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6325 sqlite3_free(p->body.a);
6326 sqlite3_free(p->cds.a);
6331 ** Register the "zipfile" virtual table.
6333 static int zipfileRegister(sqlite3 *db){
6334 static sqlite3_module zipfileModule = {
6336 zipfileConnect, /* xCreate */
6337 zipfileConnect, /* xConnect */
6338 zipfileBestIndex, /* xBestIndex */
6339 zipfileDisconnect, /* xDisconnect */
6340 zipfileDisconnect, /* xDestroy */
6341 zipfileOpen, /* xOpen - open a cursor */
6342 zipfileClose, /* xClose - close a cursor */
6343 zipfileFilter, /* xFilter - configure scan constraints */
6344 zipfileNext, /* xNext - advance a cursor */
6345 zipfileEof, /* xEof - check for end of scan */
6346 zipfileColumn, /* xColumn - read data */
6347 0, /* xRowid - read data */
6348 zipfileUpdate, /* xUpdate */
6349 zipfileBegin, /* xBegin */
6351 zipfileCommit, /* xCommit */
6352 zipfileRollback, /* xRollback */
6353 zipfileFindFunction, /* xFindMethod */
6357 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6358 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6359 if( rc==SQLITE_OK ){
6360 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6361 zipfileStep, zipfileFinal
6366 #else /* SQLITE_OMIT_VIRTUALTABLE */
6367 # define zipfileRegister(x) SQLITE_OK
6373 int sqlite3_zipfile_init(
6376 const sqlite3_api_routines *pApi
6378 SQLITE_EXTENSION_INIT2(pApi);
6379 (void)pzErrMsg; /* Unused parameter */
6380 return zipfileRegister(db);
6383 /************************* End ../ext/misc/zipfile.c ********************/
6384 /************************* Begin ../ext/misc/sqlar.c ******************/
6388 ** The author disclaims copyright to this source code. In place of
6389 ** a legal notice, here is a blessing:
6391 ** May you do good and not evil.
6392 ** May you find forgiveness for yourself and forgive others.
6393 ** May you share freely, never taking more than you give.
6395 ******************************************************************************
6397 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6398 ** for working with sqlar archives and used by the shell tool's built-in
6401 SQLITE_EXTENSION_INIT1
6405 ** Implementation of the "sqlar_compress(X)" SQL function.
6407 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6408 ** zlib utility function compress() yields a smaller blob, return the
6409 ** compressed blob. Otherwise, return a copy of X.
6411 ** SQLar uses the "zlib format" for compressed content. The zlib format
6412 ** contains a two-byte identification header and a four-byte checksum at
6413 ** the end. This is different from ZIP which uses the raw deflate format.
6415 ** Future enhancements to SQLar might add support for new compression formats.
6416 ** If so, those new formats will be identified by alternative headers in the
6419 static void sqlarCompressFunc(
6420 sqlite3_context *context,
6422 sqlite3_value **argv
6425 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6426 const Bytef *pData = sqlite3_value_blob(argv[0]);
6427 uLong nData = sqlite3_value_bytes(argv[0]);
6428 uLongf nOut = compressBound(nData);
6431 pOut = (Bytef*)sqlite3_malloc(nOut);
6433 sqlite3_result_error_nomem(context);
6436 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6437 sqlite3_result_error(context, "error in compress()", -1);
6438 }else if( nOut<nData ){
6439 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6441 sqlite3_result_value(context, argv[0]);
6446 sqlite3_result_value(context, argv[0]);
6451 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6453 ** Parameter SZ is interpreted as an integer. If it is less than or
6454 ** equal to zero, then this function returns a copy of X. Or, if
6455 ** SZ is equal to the size of X when interpreted as a blob, also
6456 ** return a copy of X. Otherwise, decompress blob X using zlib
6457 ** utility function uncompress() and return the results (another
6460 static void sqlarUncompressFunc(
6461 sqlite3_context *context,
6463 sqlite3_value **argv
6469 sz = sqlite3_value_int(argv[1]);
6471 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6472 sqlite3_result_value(context, argv[0]);
6474 const Bytef *pData= sqlite3_value_blob(argv[0]);
6475 Bytef *pOut = sqlite3_malloc(sz);
6476 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6477 sqlite3_result_error(context, "error in uncompress()", -1);
6479 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6489 int sqlite3_sqlar_init(
6492 const sqlite3_api_routines *pApi
6495 SQLITE_EXTENSION_INIT2(pApi);
6496 (void)pzErrMsg; /* Unused parameter */
6497 rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
6498 sqlarCompressFunc, 0, 0);
6499 if( rc==SQLITE_OK ){
6500 rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
6501 sqlarUncompressFunc, 0, 0);
6506 /************************* End ../ext/misc/sqlar.c ********************/
6508 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6512 ** The author disclaims copyright to this source code. In place of
6513 ** a legal notice, here is a blessing:
6515 ** May you do good and not evil.
6516 ** May you find forgiveness for yourself and forgive others.
6517 ** May you share freely, never taking more than you give.
6519 *************************************************************************
6524 typedef struct sqlite3expert sqlite3expert;
6527 ** Create a new sqlite3expert object.
6529 ** If successful, a pointer to the new object is returned and (*pzErr) set
6530 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6531 ** an English-language error message. In this case it is the responsibility
6532 ** of the caller to eventually free the error message buffer using
6535 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6538 ** Configure an sqlite3expert object.
6540 ** EXPERT_CONFIG_SAMPLE:
6541 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6542 ** each candidate index. This involves scanning and sorting the entire
6543 ** contents of each user database table once for each candidate index
6544 ** associated with the table. For large databases, this can be
6545 ** prohibitively slow. This option allows the sqlite3expert object to
6546 ** be configured so that sqlite_stat1 data is instead generated based on a
6547 ** subset of each table, or so that no sqlite_stat1 data is used at all.
6549 ** A single integer argument is passed to this option. If the value is less
6550 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
6551 ** the analysis - indexes are recommended based on the database schema only.
6552 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
6553 ** generated for each candidate index (this is the default). Finally, if the
6554 ** value falls between 0 and 100, then it represents the percentage of user
6555 ** table rows that should be considered when generating sqlite_stat1 data.
6559 ** // Do not generate any sqlite_stat1 data
6560 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6562 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
6563 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6565 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6567 #define EXPERT_CONFIG_SAMPLE 1 /* int */
6570 ** Specify zero or more SQL statements to be included in the analysis.
6572 ** Buffer zSql must contain zero or more complete SQL statements. This
6573 ** function parses all statements contained in the buffer and adds them
6574 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6575 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6576 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6577 ** may be set to point to an English language error message. In this case
6578 ** the caller is responsible for eventually freeing the error message buffer
6579 ** using sqlite3_free().
6581 ** If an error does occur while processing one of the statements in the
6582 ** buffer passed as the second argument, none of the statements in the
6583 ** buffer are added to the analysis.
6585 ** This function must be called before sqlite3_expert_analyze(). If a call
6586 ** to this function is made on an sqlite3expert object that has already
6587 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6588 ** immediately and no statements are added to the analysis.
6590 int sqlite3_expert_sql(
6591 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
6592 const char *zSql, /* SQL statement(s) to add */
6593 char **pzErr /* OUT: Error message (if any) */
6598 ** This function is called after the sqlite3expert object has been configured
6599 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6600 ** the analysis. Once this function has been called, it is not possible to
6601 ** add further SQL statements to the analysis.
6603 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6604 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6605 ** point to a buffer containing an English language error message. In this
6606 ** case it is the responsibility of the caller to eventually free the buffer
6607 ** using sqlite3_free().
6609 ** If an error does occur within this function, the sqlite3expert object
6610 ** is no longer useful for any purpose. At that point it is no longer
6611 ** possible to add further SQL statements to the object or to re-attempt
6612 ** the analysis. The sqlite3expert object must still be freed using a call
6613 ** sqlite3_expert_destroy().
6615 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6618 ** Return the total number of statements loaded using sqlite3_expert_sql().
6619 ** The total number of SQL statements may be different from the total number
6620 ** to calls to sqlite3_expert_sql().
6622 int sqlite3_expert_count(sqlite3expert*);
6625 ** Return a component of the report.
6627 ** This function is called after sqlite3_expert_analyze() to extract the
6628 ** results of the analysis. Each call to this function returns either a
6629 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6630 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6631 ** #define constants defined below.
6633 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6634 ** information relating to a specific SQL statement. In these cases that
6635 ** SQL statement is identified by the value passed as the second argument.
6636 ** SQL statements are numbered from 0 in the order in which they are parsed.
6637 ** If an out-of-range value (less than zero or equal to or greater than the
6638 ** value returned by sqlite3_expert_count()) is passed as the second argument
6639 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6641 ** EXPERT_REPORT_SQL:
6642 ** Return the text of SQL statement iStmt.
6644 ** EXPERT_REPORT_INDEXES:
6645 ** Return a buffer containing the CREATE INDEX statements for all recommended
6646 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
6649 ** EXPERT_REPORT_PLAN:
6650 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6651 ** iStmt after the proposed indexes have been added to the database schema.
6653 ** EXPERT_REPORT_CANDIDATES:
6654 ** Return a pointer to a buffer containing the CREATE INDEX statements
6655 ** for all indexes that were tested (for all SQL statements). The iStmt
6656 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6658 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6661 ** Values for the third argument passed to sqlite3_expert_report().
6663 #define EXPERT_REPORT_SQL 1
6664 #define EXPERT_REPORT_INDEXES 2
6665 #define EXPERT_REPORT_PLAN 3
6666 #define EXPERT_REPORT_CANDIDATES 4
6669 ** Free an (sqlite3expert*) handle and all associated resources. There
6670 ** should be one call to this function for each successful call to
6671 ** sqlite3-expert_new().
6673 void sqlite3_expert_destroy(sqlite3expert*);
6677 /************************* End ../ext/expert/sqlite3expert.h ********************/
6678 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6682 ** The author disclaims copyright to this source code. In place of
6683 ** a legal notice, here is a blessing:
6685 ** May you do good and not evil.
6686 ** May you find forgiveness for yourself and forgive others.
6687 ** May you share freely, never taking more than you give.
6689 *************************************************************************
6695 #ifndef SQLITE_OMIT_VIRTUALTABLE
6697 /* typedef sqlite3_int64 i64; */
6698 /* typedef sqlite3_uint64 u64; */
6700 typedef struct IdxColumn IdxColumn;
6701 typedef struct IdxConstraint IdxConstraint;
6702 typedef struct IdxScan IdxScan;
6703 typedef struct IdxStatement IdxStatement;
6704 typedef struct IdxTable IdxTable;
6705 typedef struct IdxWrite IdxWrite;
6707 #define STRLEN (int)strlen
6710 ** A temp table name that we assume no user database will actually use.
6711 ** If this assumption proves incorrect triggers on the table with the
6712 ** conflicting name will be ignored.
6714 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6717 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6718 ** any other type of single-ended range constraint on a column).
6721 ** Used to temporarily link IdxConstraint objects into lists while
6722 ** creating candidate indexes.
6724 struct IdxConstraint {
6725 char *zColl; /* Collation sequence */
6726 int bRange; /* True for range, false for eq */
6727 int iCol; /* Constrained table column */
6728 int bFlag; /* Used by idxFindCompatible() */
6729 int bDesc; /* True if ORDER BY <expr> DESC */
6730 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
6731 IdxConstraint *pLink; /* See above */
6735 ** A single scan of a single table.
6738 IdxTable *pTab; /* Associated table object */
6739 int iDb; /* Database containing table zTable */
6740 i64 covering; /* Mask of columns required for cov. index */
6741 IdxConstraint *pOrder; /* ORDER BY columns */
6742 IdxConstraint *pEq; /* List of == constraints */
6743 IdxConstraint *pRange; /* List of < constraints */
6744 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
6748 ** Information regarding a single database table. Extracted from
6749 ** "PRAGMA table_info" by function idxGetTableInfo().
6758 char *zName; /* Table name */
6760 IdxTable *pNext; /* Next table in linked list of all tables */
6764 ** An object of the following type is created for each unique table/write-op
6765 ** seen. The objects are stored in a singly-linked list beginning at
6766 ** sqlite3expert.pWrite.
6770 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
6775 ** Each statement being analyzed is represented by an instance of this
6778 struct IdxStatement {
6779 int iId; /* Statement number */
6780 char *zSql; /* SQL statement */
6781 char *zIdx; /* Indexes */
6782 char *zEQP; /* Plan */
6783 IdxStatement *pNext;
6788 ** A hash table for storing strings. With space for a payload string
6789 ** with each entry. Methods are:
6796 #define IDX_HASH_SIZE 1023
6797 typedef struct IdxHashEntry IdxHashEntry;
6798 typedef struct IdxHash IdxHash;
6799 struct IdxHashEntry {
6800 char *zKey; /* nul-terminated key */
6801 char *zVal; /* nul-terminated value string */
6802 char *zVal2; /* nul-terminated value string 2 */
6803 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
6804 IdxHashEntry *pNext; /* Next entry in hash */
6807 IdxHashEntry *pFirst;
6808 IdxHashEntry *aHash[IDX_HASH_SIZE];
6812 ** sqlite3expert object.
6814 struct sqlite3expert {
6815 int iSample; /* Percentage of tables to sample for stat1 */
6816 sqlite3 *db; /* User database */
6817 sqlite3 *dbm; /* In-memory db for this analysis */
6818 sqlite3 *dbv; /* Vtab schema for this analysis */
6819 IdxTable *pTable; /* List of all IdxTable objects */
6820 IdxScan *pScan; /* List of scan objects */
6821 IdxWrite *pWrite; /* List of write objects */
6822 IdxStatement *pStatement; /* List of IdxStatement objects */
6823 int bRun; /* True once analysis has run */
6825 int rc; /* Error code from whereinfo hook */
6826 IdxHash hIdx; /* Hash containing all candidate indexes */
6827 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
6832 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6833 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6835 static void *idxMalloc(int *pRc, int nByte){
6837 assert( *pRc==SQLITE_OK );
6839 pRet = sqlite3_malloc(nByte);
6841 memset(pRet, 0, nByte);
6843 *pRc = SQLITE_NOMEM;
6849 ** Initialize an IdxHash hash table.
6851 static void idxHashInit(IdxHash *pHash){
6852 memset(pHash, 0, sizeof(IdxHash));
6856 ** Reset an IdxHash hash table.
6858 static void idxHashClear(IdxHash *pHash){
6860 for(i=0; i<IDX_HASH_SIZE; i++){
6861 IdxHashEntry *pEntry;
6862 IdxHashEntry *pNext;
6863 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6864 pNext = pEntry->pHashNext;
6865 sqlite3_free(pEntry->zVal2);
6866 sqlite3_free(pEntry);
6869 memset(pHash, 0, sizeof(IdxHash));
6873 ** Return the index of the hash bucket that the string specified by the
6874 ** arguments to this function belongs.
6876 static int idxHashString(const char *z, int n){
6877 unsigned int ret = 0;
6880 ret += (ret<<3) + (unsigned char)(z[i]);
6882 return (int)(ret % IDX_HASH_SIZE);
6886 ** If zKey is already present in the hash table, return non-zero and do
6887 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6888 ** the hash table passed as the second argument.
6890 static int idxHashAdd(
6896 int nKey = STRLEN(zKey);
6897 int iHash = idxHashString(zKey, nKey);
6898 int nVal = (zVal ? STRLEN(zVal) : 0);
6899 IdxHashEntry *pEntry;
6901 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6902 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6906 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6908 pEntry->zKey = (char*)&pEntry[1];
6909 memcpy(pEntry->zKey, zKey, nKey);
6911 pEntry->zVal = &pEntry->zKey[nKey+1];
6912 memcpy(pEntry->zVal, zVal, nVal);
6914 pEntry->pHashNext = pHash->aHash[iHash];
6915 pHash->aHash[iHash] = pEntry;
6917 pEntry->pNext = pHash->pFirst;
6918 pHash->pFirst = pEntry;
6924 ** If zKey/nKey is present in the hash table, return a pointer to the
6925 ** hash-entry object.
6927 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6929 IdxHashEntry *pEntry;
6930 if( nKey<0 ) nKey = STRLEN(zKey);
6931 iHash = idxHashString(zKey, nKey);
6933 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6934 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6942 ** If the hash table contains an entry with a key equal to the string
6943 ** passed as the final two arguments to this function, return a pointer
6944 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6945 ** hash table, return NULL.
6947 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6948 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6949 if( pEntry ) return pEntry->zVal;
6954 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6955 ** variable to point to a copy of nul-terminated string zColl.
6957 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6958 IdxConstraint *pNew;
6959 int nColl = STRLEN(zColl);
6961 assert( *pRc==SQLITE_OK );
6962 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6964 pNew->zColl = (char*)&pNew[1];
6965 memcpy(pNew->zColl, zColl, nColl+1);
6971 ** An error associated with database handle db has just occurred. Pass
6972 ** the error message to callback function xOut.
6974 static void idxDatabaseError(
6975 sqlite3 *db, /* Database handle */
6976 char **pzErrmsg /* Write error here */
6978 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6982 ** Prepare an SQL statement.
6984 static int idxPrepareStmt(
6985 sqlite3 *db, /* Database handle to compile against */
6986 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6987 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6988 const char *zSql /* SQL statement to compile */
6990 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6991 if( rc!=SQLITE_OK ){
6993 idxDatabaseError(db, pzErrmsg);
6999 ** Prepare an SQL statement using the results of a printf() formatting.
7001 static int idxPrintfPrepareStmt(
7002 sqlite3 *db, /* Database handle to compile against */
7003 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
7004 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
7005 const char *zFmt, /* printf() format of SQL statement */
7006 ... /* Trailing printf() arguments */
7012 zSql = sqlite3_vmprintf(zFmt, ap);
7016 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7024 /*************************************************************************
7025 ** Beginning of virtual table implementation.
7027 typedef struct ExpertVtab ExpertVtab;
7031 sqlite3expert *pExpert;
7034 typedef struct ExpertCsr ExpertCsr;
7036 sqlite3_vtab_cursor base;
7037 sqlite3_stmt *pData;
7040 static char *expertDequote(const char *zIn){
7041 int n = STRLEN(zIn);
7042 char *zRet = sqlite3_malloc(n);
7044 assert( zIn[0]=='\'' );
7045 assert( zIn[n-1]=='\'' );
7050 for(iIn=1; iIn<(n-1); iIn++){
7051 if( zIn[iIn]=='\'' ){
7052 assert( zIn[iIn+1]=='\'' );
7055 zRet[iOut++] = zIn[iIn];
7064 ** This function is the implementation of both the xConnect and xCreate
7065 ** methods of the r-tree virtual table.
7067 ** argv[0] -> module name
7068 ** argv[1] -> database name
7069 ** argv[2] -> table name
7070 ** argv[...] -> column names...
7072 static int expertConnect(
7075 int argc, const char *const*argv,
7076 sqlite3_vtab **ppVtab,
7079 sqlite3expert *pExpert = (sqlite3expert*)pAux;
7084 *pzErr = sqlite3_mprintf("internal error!");
7087 char *zCreateTable = expertDequote(argv[3]);
7089 rc = sqlite3_declare_vtab(db, zCreateTable);
7090 if( rc==SQLITE_OK ){
7091 p = idxMalloc(&rc, sizeof(ExpertVtab));
7093 if( rc==SQLITE_OK ){
7094 p->pExpert = pExpert;
7095 p->pTab = pExpert->pTable;
7096 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7098 sqlite3_free(zCreateTable);
7104 *ppVtab = (sqlite3_vtab*)p;
7108 static int expertDisconnect(sqlite3_vtab *pVtab){
7109 ExpertVtab *p = (ExpertVtab*)pVtab;
7114 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7115 ExpertVtab *p = (ExpertVtab*)pVtab;
7120 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7121 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7122 SQLITE_INDEX_CONSTRAINT_LE;
7124 pScan = idxMalloc(&rc, sizeof(IdxScan));
7128 /* Link the new scan object into the list */
7129 pScan->pTab = p->pTab;
7130 pScan->pNextScan = p->pExpert->pScan;
7131 p->pExpert->pScan = pScan;
7133 /* Add the constraints to the IdxScan object */
7134 for(i=0; i<pIdxInfo->nConstraint; i++){
7135 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7137 && pCons->iColumn>=0
7138 && p->pTab->aCol[pCons->iColumn].iPk==0
7139 && (pCons->op & opmask)
7141 IdxConstraint *pNew;
7142 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7143 pNew = idxNewConstraint(&rc, zColl);
7145 pNew->iCol = pCons->iColumn;
7146 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7147 pNew->pNext = pScan->pEq;
7151 pNew->pNext = pScan->pRange;
7152 pScan->pRange = pNew;
7156 pIdxInfo->aConstraintUsage[i].argvIndex = n;
7160 /* Add the ORDER BY to the IdxScan object */
7161 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7162 int iCol = pIdxInfo->aOrderBy[i].iColumn;
7164 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7167 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7168 pNew->pNext = pScan->pOrder;
7169 pNew->pLink = pScan->pOrder;
7170 pScan->pOrder = pNew;
7177 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7181 static int expertUpdate(
7182 sqlite3_vtab *pVtab,
7184 sqlite3_value **azData,
7185 sqlite_int64 *pRowid
7195 ** Virtual table module xOpen method.
7197 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7201 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7202 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7207 ** Virtual table module xClose method.
7209 static int expertClose(sqlite3_vtab_cursor *cur){
7210 ExpertCsr *pCsr = (ExpertCsr*)cur;
7211 sqlite3_finalize(pCsr->pData);
7217 ** Virtual table module xEof method.
7219 ** Return non-zero if the cursor does not currently point to a valid
7220 ** record (i.e if the scan has finished), or zero otherwise.
7222 static int expertEof(sqlite3_vtab_cursor *cur){
7223 ExpertCsr *pCsr = (ExpertCsr*)cur;
7224 return pCsr->pData==0;
7228 ** Virtual table module xNext method.
7230 static int expertNext(sqlite3_vtab_cursor *cur){
7231 ExpertCsr *pCsr = (ExpertCsr*)cur;
7234 assert( pCsr->pData );
7235 rc = sqlite3_step(pCsr->pData);
7236 if( rc!=SQLITE_ROW ){
7237 rc = sqlite3_finalize(pCsr->pData);
7247 ** Virtual table module xRowid method.
7249 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7256 ** Virtual table module xColumn method.
7258 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7259 ExpertCsr *pCsr = (ExpertCsr*)cur;
7260 sqlite3_value *pVal;
7261 pVal = sqlite3_column_value(pCsr->pData, i);
7263 sqlite3_result_value(ctx, pVal);
7269 ** Virtual table module xFilter method.
7271 static int expertFilter(
7272 sqlite3_vtab_cursor *cur,
7273 int idxNum, const char *idxStr,
7274 int argc, sqlite3_value **argv
7276 ExpertCsr *pCsr = (ExpertCsr*)cur;
7277 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7278 sqlite3expert *pExpert = pVtab->pExpert;
7285 rc = sqlite3_finalize(pCsr->pData);
7287 if( rc==SQLITE_OK ){
7288 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7289 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7293 if( rc==SQLITE_OK ){
7294 rc = expertNext(cur);
7299 static int idxRegisterVtab(sqlite3expert *p){
7300 static sqlite3_module expertModule = {
7302 expertConnect, /* xCreate - create a table */
7303 expertConnect, /* xConnect - connect to an existing table */
7304 expertBestIndex, /* xBestIndex - Determine search strategy */
7305 expertDisconnect, /* xDisconnect - Disconnect from a table */
7306 expertDisconnect, /* xDestroy - Drop a table */
7307 expertOpen, /* xOpen - open a cursor */
7308 expertClose, /* xClose - close a cursor */
7309 expertFilter, /* xFilter - configure scan constraints */
7310 expertNext, /* xNext - advance a cursor */
7311 expertEof, /* xEof */
7312 expertColumn, /* xColumn - read data */
7313 expertRowid, /* xRowid - read data */
7314 expertUpdate, /* xUpdate - write data */
7315 0, /* xBegin - begin transaction */
7316 0, /* xSync - sync transaction */
7317 0, /* xCommit - commit transaction */
7318 0, /* xRollback - rollback transaction */
7319 0, /* xFindFunction - function overloading */
7320 0, /* xRename - rename the table */
7323 0, /* xRollbackTo */
7324 0, /* xShadowName */
7327 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7330 ** End of virtual table implementation.
7331 *************************************************************************/
7333 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7334 ** is called, set it to the return value of sqlite3_finalize() before
7335 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7337 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7338 int rc = sqlite3_finalize(pStmt);
7339 if( *pRc==SQLITE_OK ) *pRc = rc;
7343 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7344 ** in the main database of connection db. If successful, set (*ppOut) to
7345 ** point to the new object and return SQLITE_OK. Otherwise, return an
7346 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7347 ** set to point to an error string.
7349 ** It is the responsibility of the caller to eventually free either the
7350 ** IdxTable object or error message using sqlite3_free().
7352 static int idxGetTableInfo(
7353 sqlite3 *db, /* Database connection to read details from */
7354 const char *zTab, /* Table name */
7355 IdxTable **ppOut, /* OUT: New object (if successful) */
7356 char **pzErrmsg /* OUT: Error message (if not) */
7358 sqlite3_stmt *p1 = 0;
7360 int nTab = STRLEN(zTab);
7361 int nByte = sizeof(IdxTable) + nTab + 1;
7366 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7367 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7368 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7369 nByte += 1 + STRLEN(zCol);
7370 rc = sqlite3_table_column_metadata(
7371 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7373 nByte += 1 + STRLEN(zCol);
7376 rc2 = sqlite3_reset(p1);
7377 if( rc==SQLITE_OK ) rc = rc2;
7379 nByte += sizeof(IdxColumn) * nCol;
7380 if( rc==SQLITE_OK ){
7381 pNew = idxMalloc(&rc, nByte);
7383 if( rc==SQLITE_OK ){
7384 pNew->aCol = (IdxColumn*)&pNew[1];
7386 pCsr = (char*)&pNew->aCol[nCol];
7390 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7391 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7392 int nCopy = STRLEN(zCol) + 1;
7393 pNew->aCol[nCol].zName = pCsr;
7394 pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7395 memcpy(pCsr, zCol, nCopy);
7398 rc = sqlite3_table_column_metadata(
7399 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7401 if( rc==SQLITE_OK ){
7402 nCopy = STRLEN(zCol) + 1;
7403 pNew->aCol[nCol].zColl = pCsr;
7404 memcpy(pCsr, zCol, nCopy);
7410 idxFinalize(&rc, p1);
7412 if( rc!=SQLITE_OK ){
7417 memcpy(pNew->zName, zTab, nTab+1);
7425 ** This function is a no-op if *pRc is set to anything other than
7426 ** SQLITE_OK when it is called.
7428 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7429 ** the printf() style arguments is appended to zIn and the result returned
7430 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7431 ** zIn before returning.
7433 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7437 int nIn = zIn ? STRLEN(zIn) : 0;
7440 if( *pRc==SQLITE_OK ){
7441 zAppend = sqlite3_vmprintf(zFmt, ap);
7443 nAppend = STRLEN(zAppend);
7444 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7446 if( zAppend && zRet ){
7447 if( nIn ) memcpy(zRet, zIn, nIn);
7448 memcpy(&zRet[nIn], zAppend, nAppend+1);
7452 *pRc = SQLITE_NOMEM;
7454 sqlite3_free(zAppend);
7462 ** Return true if zId must be quoted in order to use it as an SQL
7463 ** identifier, or false otherwise.
7465 static int idxIdentifierRequiresQuotes(const char *zId){
7467 for(i=0; zId[i]; i++){
7469 && !(zId[i]>='0' && zId[i]<='9')
7470 && !(zId[i]>='a' && zId[i]<='z')
7471 && !(zId[i]>='A' && zId[i]<='Z')
7480 ** This function appends an index column definition suitable for constraint
7481 ** pCons to the string passed as zIn and returns the result.
7483 static char *idxAppendColDefn(
7484 int *pRc, /* IN/OUT: Error code */
7485 char *zIn, /* Column defn accumulated so far */
7486 IdxTable *pTab, /* Table index will be created on */
7487 IdxConstraint *pCons
7490 IdxColumn *p = &pTab->aCol[pCons->iCol];
7491 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7493 if( idxIdentifierRequiresQuotes(p->zName) ){
7494 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7496 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7499 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7500 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7501 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7503 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7508 zRet = idxAppendText(pRc, zRet, " DESC");
7514 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7515 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7516 ** such an index is found, return non-zero. Or, if no such index is found,
7519 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7521 static int idxFindCompatible(
7522 int *pRc, /* OUT: Error code */
7523 sqlite3* dbm, /* Database to search */
7524 IdxScan *pScan, /* Scan for table to search for index on */
7525 IdxConstraint *pEq, /* List of == constraints */
7526 IdxConstraint *pTail /* List of range constraints */
7528 const char *zTbl = pScan->pTab->zName;
7529 sqlite3_stmt *pIdxList = 0;
7530 IdxConstraint *pIter;
7531 int nEq = 0; /* Number of elements in pEq */
7534 /* Count the elements in list pEq */
7535 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7537 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7538 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7540 IdxConstraint *pT = pTail;
7541 sqlite3_stmt *pInfo = 0;
7542 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7544 /* Zero the IdxConstraint.bFlag values in the pEq list */
7545 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7547 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7548 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7549 int iIdx = sqlite3_column_int(pInfo, 0);
7550 int iCol = sqlite3_column_int(pInfo, 1);
7551 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7554 for(pIter=pEq; pIter; pIter=pIter->pLink){
7555 if( pIter->bFlag ) continue;
7556 if( pIter->iCol!=iCol ) continue;
7557 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7567 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7575 idxFinalize(&rc, pInfo);
7577 if( rc==SQLITE_OK && bMatch ){
7578 sqlite3_finalize(pIdxList);
7582 idxFinalize(&rc, pIdxList);
7588 static int idxCreateFromCons(
7592 IdxConstraint *pTail
7594 sqlite3 *dbm = p->dbm;
7596 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7597 IdxTable *pTab = pScan->pTab;
7600 IdxConstraint *pCons;
7604 for(pCons=pEq; pCons; pCons=pCons->pLink){
7605 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7607 for(pCons=pTail; pCons; pCons=pCons->pLink){
7608 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7611 if( rc==SQLITE_OK ){
7612 /* Hash the list of columns to come up with a name for the index */
7613 const char *zTable = pScan->pTab->zName;
7614 char *zName; /* Index name */
7616 for(i=0; zCols[i]; i++){
7617 h += ((h<<3) + zCols[i]);
7619 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7623 if( idxIdentifierRequiresQuotes(zTable) ){
7624 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7626 zFmt = "CREATE INDEX %s ON %s(%s)";
7628 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7632 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7633 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7635 sqlite3_free(zName);
7640 sqlite3_free(zCols);
7646 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7647 ** a constraint compatible with *p. Otherwise return false.
7649 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7650 IdxConstraint *pCmp;
7651 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7652 if( p->iCol==pCmp->iCol ) return 1;
7657 static int idxCreateFromWhere(
7659 IdxScan *pScan, /* Create indexes for this scan */
7660 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
7662 IdxConstraint *p1 = 0;
7663 IdxConstraint *pCon;
7666 /* Gather up all the == constraints. */
7667 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7668 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7674 /* Create an index using the == constraints collected above. And the
7675 ** range constraint/ORDER BY terms passed in by the caller, if any. */
7676 rc = idxCreateFromCons(p, pScan, p1, pTail);
7678 /* If no range/ORDER BY passed by the caller, create a version of the
7679 ** index for each range constraint. */
7681 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7682 assert( pCon->pLink==0 );
7683 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7684 rc = idxCreateFromCons(p, pScan, p1, pCon);
7693 ** Create candidate indexes in database [dbm] based on the data in
7694 ** linked-list pScan.
7696 static int idxCreateCandidates(sqlite3expert *p){
7700 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7701 rc = idxCreateFromWhere(p, pIter, 0);
7702 if( rc==SQLITE_OK && pIter->pOrder ){
7703 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7711 ** Free all elements of the linked list starting at pConstraint.
7713 static void idxConstraintFree(IdxConstraint *pConstraint){
7714 IdxConstraint *pNext;
7717 for(p=pConstraint; p; p=pNext){
7724 ** Free all elements of the linked list starting from pScan up until pLast
7725 ** (pLast is not freed).
7727 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7730 for(p=pScan; p!=pLast; p=pNext){
7731 pNext = p->pNextScan;
7732 idxConstraintFree(p->pOrder);
7733 idxConstraintFree(p->pEq);
7734 idxConstraintFree(p->pRange);
7740 ** Free all elements of the linked list starting from pStatement up
7741 ** until pLast (pLast is not freed).
7743 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7745 IdxStatement *pNext;
7746 for(p=pStatement; p!=pLast; p=pNext){
7748 sqlite3_free(p->zEQP);
7749 sqlite3_free(p->zIdx);
7755 ** Free the linked list of IdxTable objects starting at pTab.
7757 static void idxTableFree(IdxTable *pTab){
7760 for(pIter=pTab; pIter; pIter=pNext){
7761 pNext = pIter->pNext;
7762 sqlite3_free(pIter);
7767 ** Free the linked list of IdxWrite objects starting at pTab.
7769 static void idxWriteFree(IdxWrite *pTab){
7772 for(pIter=pTab; pIter; pIter=pNext){
7773 pNext = pIter->pNext;
7774 sqlite3_free(pIter);
7781 ** This function is called after candidate indexes have been created. It
7782 ** runs all the queries to see which indexes they prefer, and populates
7783 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7787 char **pzErr /* OUT: Error message (sqlite3_malloc) */
7789 IdxStatement *pStmt;
7790 sqlite3 *dbm = p->dbm;
7796 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7797 IdxHashEntry *pEntry;
7798 sqlite3_stmt *pExplain = 0;
7799 idxHashClear(&hIdx);
7800 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7801 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7803 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7804 /* int iId = sqlite3_column_int(pExplain, 0); */
7805 /* int iParent = sqlite3_column_int(pExplain, 1); */
7806 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
7807 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7808 int nDetail = STRLEN(zDetail);
7811 for(i=0; i<nDetail; i++){
7812 const char *zIdx = 0;
7813 if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7814 zIdx = &zDetail[i+13];
7815 }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7816 zIdx = &zDetail[i+22];
7821 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7824 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7826 idxHashAdd(&rc, &hIdx, zSql, 0);
7827 if( rc ) goto find_indexes_out;
7833 if( zDetail[0]!='-' ){
7834 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
7838 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7839 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7842 idxFinalize(&rc, pExplain);
7846 idxHashClear(&hIdx);
7850 static int idxAuthCallback(
7856 const char *zTrigger
7861 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7862 if( sqlite3_stricmp(zDb, "main")==0 ){
7863 sqlite3expert *p = (sqlite3expert*)pCtx;
7865 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7866 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7870 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7871 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7874 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7875 if( rc==SQLITE_OK ){
7876 pWrite->pTab = pTab;
7878 pWrite->pNext = p->pWrite;
7888 static int idxProcessOneTrigger(
7893 static const char *zInt = UNIQUE_TABLE_NAME;
7894 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7895 IdxTable *pTab = pWrite->pTab;
7896 const char *zTab = pTab->zName;
7898 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7899 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7901 sqlite3_stmt *pSelect = 0;
7905 /* Create the table and its triggers in the temp schema */
7906 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7907 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7908 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7909 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7911 idxFinalize(&rc, pSelect);
7913 /* Rename the table in the temp schema to zInt */
7914 if( rc==SQLITE_OK ){
7915 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7919 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7924 switch( pWrite->eOp ){
7925 case SQLITE_INSERT: {
7927 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7928 for(i=0; i<pTab->nCol; i++){
7929 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7931 zWrite = idxAppendText(&rc, zWrite, ")");
7934 case SQLITE_UPDATE: {
7936 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7937 for(i=0; i<pTab->nCol; i++){
7938 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7945 assert( pWrite->eOp==SQLITE_DELETE );
7946 if( rc==SQLITE_OK ){
7947 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7948 if( zWrite==0 ) rc = SQLITE_NOMEM;
7953 if( rc==SQLITE_OK ){
7954 sqlite3_stmt *pX = 0;
7955 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7956 idxFinalize(&rc, pX);
7957 if( rc!=SQLITE_OK ){
7958 idxDatabaseError(p->dbv, pzErr);
7961 sqlite3_free(zWrite);
7963 if( rc==SQLITE_OK ){
7964 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7970 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7973 IdxWrite *pFirst = p->pWrite;
7975 while( rc==SQLITE_OK && pFirst!=pEnd ){
7977 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7978 rc = idxProcessOneTrigger(p, pIter, pzErr);
7988 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7989 int rc = idxRegisterVtab(p);
7990 sqlite3_stmt *pSchema = 0;
7992 /* For each table in the main db schema:
7994 ** 1) Add an entry to the p->pTable list, and
7995 ** 2) Create the equivalent virtual table in dbv.
7997 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7998 "SELECT type, name, sql, 1 FROM sqlite_master "
7999 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8001 "SELECT type, name, sql, 2 FROM sqlite_master "
8002 "WHERE type = 'trigger'"
8003 " AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8006 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8007 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8008 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8009 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8011 if( zType[0]=='v' || zType[1]=='r' ){
8012 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8015 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8016 if( rc==SQLITE_OK ){
8020 pTab->pNext = p->pTable;
8023 /* The statement the vtab will pass to sqlite3_declare_vtab() */
8024 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8025 for(i=0; i<pTab->nCol; i++){
8026 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
8027 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8030 zInner = idxAppendText(&rc, zInner, ")");
8032 /* The CVT statement to create the vtab */
8033 zOuter = idxAppendText(&rc, 0,
8034 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8036 if( rc==SQLITE_OK ){
8037 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8039 sqlite3_free(zInner);
8040 sqlite3_free(zOuter);
8044 idxFinalize(&rc, pSchema);
8048 struct IdxSampleCtx {
8050 double target; /* Target nRet/nRow value */
8051 double nRow; /* Number of rows seen */
8052 double nRet; /* Number of rows returned */
8055 static void idxSampleFunc(
8056 sqlite3_context *pCtx,
8058 sqlite3_value **argv
8060 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8068 bRet = (p->nRet / p->nRow) <= p->target;
8071 sqlite3_randomness(2, (void*)&rnd);
8072 bRet = ((int)rnd % 100) <= p->iTarget;
8076 sqlite3_result_int(pCtx, bRet);
8078 p->nRet += (double)bRet;
8084 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8085 i64 iVal; /* SQLITE_INTEGER value */
8086 double rVal; /* SQLITE_FLOAT value */
8087 int nByte; /* Bytes of space allocated at z */
8088 int n; /* Size of buffer z */
8089 char *z; /* SQLITE_TEXT/BLOB value */
8094 ** Implementation of scalar function rem().
8096 static void idxRemFunc(
8097 sqlite3_context *pCtx,
8099 sqlite3_value **argv
8101 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8102 struct IdxRemSlot *pSlot;
8106 iSlot = sqlite3_value_int(argv[0]);
8107 assert( iSlot<=p->nSlot );
8108 pSlot = &p->aSlot[iSlot];
8110 switch( pSlot->eType ){
8115 case SQLITE_INTEGER:
8116 sqlite3_result_int64(pCtx, pSlot->iVal);
8120 sqlite3_result_double(pCtx, pSlot->rVal);
8124 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8128 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8132 pSlot->eType = sqlite3_value_type(argv[1]);
8133 switch( pSlot->eType ){
8138 case SQLITE_INTEGER:
8139 pSlot->iVal = sqlite3_value_int64(argv[1]);
8143 pSlot->rVal = sqlite3_value_double(argv[1]);
8148 int nByte = sqlite3_value_bytes(argv[1]);
8149 if( nByte>pSlot->nByte ){
8150 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8152 sqlite3_result_error_nomem(pCtx);
8155 pSlot->nByte = nByte*2;
8159 if( pSlot->eType==SQLITE_BLOB ){
8160 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8162 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8169 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8172 "SELECT max(i.seqno) FROM "
8173 " sqlite_master AS s, "
8174 " pragma_index_list(s.name) AS l, "
8175 " pragma_index_info(l.name) AS i "
8176 "WHERE s.type = 'table'";
8177 sqlite3_stmt *pMax = 0;
8180 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8181 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8182 *pnMax = sqlite3_column_int(pMax, 0) + 1;
8184 idxFinalize(&rc, pMax);
8189 static int idxPopulateOneStat1(
8191 sqlite3_stmt *pIndexXInfo,
8192 sqlite3_stmt *pWriteStat,
8202 sqlite3_stmt *pQuery = 0;
8206 assert( p->iSample>0 );
8208 /* Formulate the query text */
8209 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8210 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8211 const char *zComma = zCols==0 ? "" : ", ";
8212 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8213 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8214 zCols = idxAppendText(&rc, zCols,
8215 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8217 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8219 sqlite3_reset(pIndexXInfo);
8220 if( rc==SQLITE_OK ){
8221 if( p->iSample==100 ){
8222 zQuery = sqlite3_mprintf(
8223 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8226 zQuery = sqlite3_mprintf(
8227 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8231 sqlite3_free(zCols);
8232 sqlite3_free(zOrder);
8234 /* Formulate the query text */
8235 if( rc==SQLITE_OK ){
8236 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8237 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8239 sqlite3_free(zQuery);
8241 if( rc==SQLITE_OK ){
8242 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8244 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8245 IdxHashEntry *pEntry;
8247 for(i=0; i<=nCol; i++) aStat[i] = 1;
8248 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8250 for(i=0; i<nCol; i++){
8251 if( sqlite3_column_int(pQuery, i)==0 ) break;
8253 for(/*no-op*/; i<nCol; i++){
8258 if( rc==SQLITE_OK ){
8260 zStat = sqlite3_mprintf("%d", s0);
8261 if( zStat==0 ) rc = SQLITE_NOMEM;
8262 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8263 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8267 if( rc==SQLITE_OK ){
8268 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8269 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8270 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8271 sqlite3_step(pWriteStat);
8272 rc = sqlite3_reset(pWriteStat);
8275 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8277 assert( pEntry->zVal2==0 );
8278 pEntry->zVal2 = zStat;
8280 sqlite3_free(zStat);
8283 sqlite3_free(aStat);
8284 idxFinalize(&rc, pQuery);
8289 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8293 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8294 if( rc!=SQLITE_OK ) return rc;
8296 zSql = sqlite3_mprintf(
8297 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8299 if( zSql==0 ) return SQLITE_NOMEM;
8300 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8307 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8308 ** indexes have already been created in database sqlite3expert.dbm, this
8309 ** function populates sqlite_stat1 table in the same database.
8311 ** The stat1 data is generated by querying the
8313 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8316 struct IdxRemCtx *pCtx = 0;
8317 struct IdxSampleCtx samplectx;
8319 i64 iPrev = -100000;
8320 sqlite3_stmt *pAllIndex = 0;
8321 sqlite3_stmt *pIndexXInfo = 0;
8322 sqlite3_stmt *pWrite = 0;
8324 const char *zAllIndex =
8325 "SELECT s.rowid, s.name, l.name FROM "
8326 " sqlite_master AS s, "
8327 " pragma_index_list(s.name) AS l "
8328 "WHERE s.type = 'table'";
8329 const char *zIndexXInfo =
8330 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8331 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8333 /* If iSample==0, no sqlite_stat1 data is required. */
8334 if( p->iSample==0 ) return SQLITE_OK;
8336 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8337 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8339 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8341 if( rc==SQLITE_OK ){
8342 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8343 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8346 if( rc==SQLITE_OK ){
8347 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8348 rc = sqlite3_create_function(
8349 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8352 if( rc==SQLITE_OK ){
8353 rc = sqlite3_create_function(
8354 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8358 if( rc==SQLITE_OK ){
8359 pCtx->nSlot = nMax+1;
8360 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8362 if( rc==SQLITE_OK ){
8363 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8365 if( rc==SQLITE_OK ){
8366 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8369 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8370 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8371 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8372 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8373 if( p->iSample<100 && iPrev!=iRowid ){
8374 samplectx.target = (double)p->iSample / 100.0;
8375 samplectx.iTarget = p->iSample;
8376 samplectx.nRow = 0.0;
8377 samplectx.nRet = 0.0;
8378 rc = idxBuildSampleTable(p, zTab);
8379 if( rc!=SQLITE_OK ) break;
8381 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8384 if( rc==SQLITE_OK && p->iSample<100 ){
8385 rc = sqlite3_exec(p->dbv,
8386 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8390 idxFinalize(&rc, pAllIndex);
8391 idxFinalize(&rc, pIndexXInfo);
8392 idxFinalize(&rc, pWrite);
8394 for(i=0; i<pCtx->nSlot; i++){
8395 sqlite3_free(pCtx->aSlot[i].z);
8399 if( rc==SQLITE_OK ){
8400 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8403 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8408 ** Allocate a new sqlite3expert object.
8410 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8412 sqlite3expert *pNew;
8414 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8416 /* Open two in-memory databases to work with. The "vtab database" (dbv)
8417 ** will contain a virtual table corresponding to each real table in
8418 ** the user database schema, and a copy of each view. It is used to
8419 ** collect information regarding the WHERE, ORDER BY and other clauses
8420 ** of the user's query.
8422 if( rc==SQLITE_OK ){
8424 pNew->iSample = 100;
8425 rc = sqlite3_open(":memory:", &pNew->dbv);
8427 if( rc==SQLITE_OK ){
8428 rc = sqlite3_open(":memory:", &pNew->dbm);
8429 if( rc==SQLITE_OK ){
8430 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8435 /* Copy the entire schema of database [db] into [dbm]. */
8436 if( rc==SQLITE_OK ){
8438 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8439 "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8440 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8442 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8443 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8444 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8446 idxFinalize(&rc, pSql);
8449 /* Create the vtab schema */
8450 if( rc==SQLITE_OK ){
8451 rc = idxCreateVtabSchema(pNew, pzErrmsg);
8454 /* Register the auth callback with dbv */
8455 if( rc==SQLITE_OK ){
8456 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8459 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8460 ** return the new sqlite3expert handle. */
8461 if( rc!=SQLITE_OK ){
8462 sqlite3_expert_destroy(pNew);
8469 ** Configure an sqlite3expert object.
8471 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8476 case EXPERT_CONFIG_SAMPLE: {
8477 int iVal = va_arg(ap, int);
8478 if( iVal<0 ) iVal = 0;
8479 if( iVal>100 ) iVal = 100;
8484 rc = SQLITE_NOTFOUND;
8493 ** Add an SQL statement to the analysis.
8495 int sqlite3_expert_sql(
8496 sqlite3expert *p, /* From sqlite3_expert_new() */
8497 const char *zSql, /* SQL statement to add */
8498 char **pzErr /* OUT: Error message (if any) */
8500 IdxScan *pScanOrig = p->pScan;
8501 IdxStatement *pStmtOrig = p->pStatement;
8503 const char *zStmt = zSql;
8505 if( p->bRun ) return SQLITE_MISUSE;
8507 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8508 sqlite3_stmt *pStmt = 0;
8509 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8510 if( rc==SQLITE_OK ){
8513 const char *z = sqlite3_sql(pStmt);
8515 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8516 if( rc==SQLITE_OK ){
8517 pNew->zSql = (char*)&pNew[1];
8518 memcpy(pNew->zSql, z, n+1);
8519 pNew->pNext = p->pStatement;
8520 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8521 p->pStatement = pNew;
8523 sqlite3_finalize(pStmt);
8526 idxDatabaseError(p->dbv, pzErr);
8530 if( rc!=SQLITE_OK ){
8531 idxScanFree(p->pScan, pScanOrig);
8532 idxStatementFree(p->pStatement, pStmtOrig);
8533 p->pScan = pScanOrig;
8534 p->pStatement = pStmtOrig;
8540 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8542 IdxHashEntry *pEntry;
8544 /* Do trigger processing to collect any extra IdxScan structures */
8545 rc = idxProcessTriggers(p, pzErr);
8547 /* Create candidate indexes within the in-memory database file */
8548 if( rc==SQLITE_OK ){
8549 rc = idxCreateCandidates(p);
8552 /* Generate the stat1 data */
8553 if( rc==SQLITE_OK ){
8554 rc = idxPopulateStat1(p, pzErr);
8557 /* Formulate the EXPERT_REPORT_CANDIDATES text */
8558 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8559 p->zCandidates = idxAppendText(&rc, p->zCandidates,
8560 "%s;%s%s\n", pEntry->zVal,
8561 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8565 /* Figure out which of the candidate indexes are preferred by the query
8566 ** planner and report the results to the user. */
8567 if( rc==SQLITE_OK ){
8568 rc = idxFindIndexes(p, pzErr);
8571 if( rc==SQLITE_OK ){
8578 ** Return the total number of statements that have been added to this
8579 ** sqlite3expert using sqlite3_expert_sql().
8581 int sqlite3_expert_count(sqlite3expert *p){
8583 if( p->pStatement ) nRet = p->pStatement->iId+1;
8588 ** Return a component of the report.
8590 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8591 const char *zRet = 0;
8592 IdxStatement *pStmt;
8594 if( p->bRun==0 ) return 0;
8595 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8597 case EXPERT_REPORT_SQL:
8598 if( pStmt ) zRet = pStmt->zSql;
8600 case EXPERT_REPORT_INDEXES:
8601 if( pStmt ) zRet = pStmt->zIdx;
8603 case EXPERT_REPORT_PLAN:
8604 if( pStmt ) zRet = pStmt->zEQP;
8606 case EXPERT_REPORT_CANDIDATES:
8607 zRet = p->zCandidates;
8614 ** Free an sqlite3expert object.
8616 void sqlite3_expert_destroy(sqlite3expert *p){
8618 sqlite3_close(p->dbm);
8619 sqlite3_close(p->dbv);
8620 idxScanFree(p->pScan, 0);
8621 idxStatementFree(p->pStatement, 0);
8622 idxTableFree(p->pTable);
8623 idxWriteFree(p->pWrite);
8624 idxHashClear(&p->hIdx);
8625 sqlite3_free(p->zCandidates);
8630 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
8632 /************************* End ../ext/expert/sqlite3expert.c ********************/
8634 #if defined(SQLITE_ENABLE_SESSION)
8636 ** State information for a single open session
8638 typedef struct OpenSession OpenSession;
8639 struct OpenSession {
8640 char *zName; /* Symbolic name for this session */
8641 int nFilter; /* Number of xFilter rejection GLOB patterns */
8642 char **azFilter; /* Array of xFilter rejection GLOB patterns */
8643 sqlite3_session *p; /* The open session */
8648 ** Shell output mode information from before ".explain on",
8649 ** saved so that it can be restored by ".explain off"
8651 typedef struct SavedModeInfo SavedModeInfo;
8652 struct SavedModeInfo {
8653 int valid; /* Is there legit data in here? */
8654 int mode; /* Mode prior to ".explain on" */
8655 int showHeader; /* The ".header" setting prior to ".explain on" */
8656 int colWidth[100]; /* Column widths prior to ".explain on" */
8659 typedef struct ExpertInfo ExpertInfo;
8661 sqlite3expert *pExpert;
8665 /* A single line in the EQP output */
8666 typedef struct EQPGraphRow EQPGraphRow;
8667 struct EQPGraphRow {
8668 int iEqpId; /* ID for this row */
8669 int iParentId; /* ID of the parent row */
8670 EQPGraphRow *pNext; /* Next row in sequence */
8671 char zText[1]; /* Text to display for this row */
8674 /* All EQP output is collected into an instance of the following */
8675 typedef struct EQPGraph EQPGraph;
8677 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
8678 EQPGraphRow *pLast; /* Last element of the pRow list */
8679 char zPrefix[100]; /* Graph prefix */
8683 ** State information about the database connection is contained in an
8684 ** instance of the following structure.
8686 typedef struct ShellState ShellState;
8688 sqlite3 *db; /* The database */
8689 u8 autoExplain; /* Automatically turn on .explain mode */
8690 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
8691 u8 autoEQPtest; /* autoEQP is in test mode */
8692 u8 autoEQPtrace; /* autoEQP is in trace mode */
8693 u8 statsOn; /* True to display memory stats before each finalize */
8694 u8 scanstatsOn; /* True to display scan stats before each finalize */
8695 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
8696 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
8697 u8 nEqpLevel; /* Depth of the EQP output graph */
8698 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
8699 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
8700 int outCount; /* Revert to stdout when reaching zero */
8701 int cnt; /* Number of records displayed so far */
8702 int lineno; /* Line number of last line read from in */
8703 FILE *in; /* Read commands from this stream */
8704 FILE *out; /* Write results here */
8705 FILE *traceOut; /* Output for sqlite3_trace() */
8706 int nErr; /* Number of errors seen */
8707 int mode; /* An output mode setting */
8708 int modePrior; /* Saved mode */
8709 int cMode; /* temporary output mode for the current query */
8710 int normalMode; /* Output mode before ".explain on" */
8711 int writableSchema; /* True if PRAGMA writable_schema=ON */
8712 int showHeader; /* True to show column names in List or Column mode */
8713 int nCheck; /* Number of ".check" commands run */
8714 unsigned nProgress; /* Number of progress callbacks encountered */
8715 unsigned mxProgress; /* Maximum progress callbacks before failing */
8716 unsigned flgProgress; /* Flags for the progress callback */
8717 unsigned shellFlgs; /* Various flags */
8718 sqlite3_int64 szMax; /* --maxsize argument to .open */
8719 char *zDestTable; /* Name of destination table when MODE_Insert */
8720 char *zTempFile; /* Temporary file that might need deleting */
8721 char zTestcase[30]; /* Name of current test case */
8722 char colSeparator[20]; /* Column separator character for several modes */
8723 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
8724 char colSepPrior[20]; /* Saved column separator */
8725 char rowSepPrior[20]; /* Saved row separator */
8726 int colWidth[100]; /* Requested width of each column when in column mode*/
8727 int actualWidth[100]; /* Actual width of each column */
8728 char nullValue[20]; /* The text to print when a NULL comes back from
8730 char outfile[FILENAME_MAX]; /* Filename for *out */
8731 const char *zDbFilename; /* name of the database file */
8732 char *zFreeOnClose; /* Filename to free when closing */
8733 const char *zVfs; /* Name of VFS to use */
8734 sqlite3_stmt *pStmt; /* Current statement if any. */
8735 FILE *pLog; /* Write log output here */
8736 int *aiIndent; /* Array of indents used in MODE_Explain */
8737 int nIndent; /* Size of array aiIndent[] */
8738 int iIndent; /* Index of current op in aiIndent[] */
8739 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
8740 #if defined(SQLITE_ENABLE_SESSION)
8741 int nSession; /* Number of active sessions */
8742 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
8744 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
8748 /* Allowed values for ShellState.autoEQP
8750 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
8751 #define AUTOEQP_on 1 /* Automatic EQP is on */
8752 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
8753 #define AUTOEQP_full 3 /* Show full EXPLAIN */
8755 /* Allowed values for ShellState.openMode
8757 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
8758 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
8759 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
8760 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
8761 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
8762 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
8763 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
8765 /* Allowed values for ShellState.eTraceType
8767 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
8768 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
8769 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
8771 /* Bits in the ShellState.flgProgress variable */
8772 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
8773 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
8774 ** callback limit is reached, and for each
8775 ** top-level SQL statement */
8776 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
8779 ** These are the allowed shellFlgs values
8781 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
8782 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
8783 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
8784 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
8785 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
8786 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
8787 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
8790 ** Macros for testing and setting shellFlgs
8792 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
8793 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
8794 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
8797 ** These are the allowed modes.
8799 #define MODE_Line 0 /* One column per line. Blank line between records */
8800 #define MODE_Column 1 /* One record per line in neat columns */
8801 #define MODE_List 2 /* One record per line with a separator */
8802 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
8803 #define MODE_Html 4 /* Generate an XHTML table */
8804 #define MODE_Insert 5 /* Generate SQL "insert" statements */
8805 #define MODE_Quote 6 /* Quote values as for SQL */
8806 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
8807 #define MODE_Csv 8 /* Quote strings, numbers are plain */
8808 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
8809 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
8810 #define MODE_Pretty 11 /* Pretty-print schemas */
8811 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
8813 static const char *modeDescr[] = {
8830 ** These are the column/row/line separators used by the various
8831 ** import/export modes.
8833 #define SEP_Column "|"
8834 #define SEP_Row "\n"
8835 #define SEP_Tab "\t"
8836 #define SEP_Space " "
8837 #define SEP_Comma ","
8838 #define SEP_CrLf "\r\n"
8839 #define SEP_Unit "\x1F"
8840 #define SEP_Record "\x1E"
8843 ** A callback for the sqlite3_log() interface.
8845 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8846 ShellState *p = (ShellState*)pArg;
8847 if( p->pLog==0 ) return;
8848 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8853 ** SQL function: shell_putsnl(X)
8855 ** Write the text X to the screen (or whatever output is being directed)
8856 ** adding a newline at the end, and then return X.
8858 static void shellPutsFunc(
8859 sqlite3_context *pCtx,
8861 sqlite3_value **apVal
8863 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8865 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8866 sqlite3_result_value(pCtx, apVal[0]);
8870 ** SQL function: edit(VALUE)
8871 ** edit(VALUE,EDITOR)
8875 ** (1) Write VALUE into a temporary file.
8876 ** (2) Run program EDITOR on that temporary file.
8877 ** (3) Read the temporary file back and return its content as the result.
8878 ** (4) Delete the temporary file
8880 ** If the EDITOR argument is omitted, use the value in the VISUAL
8881 ** environment variable. If still there is no EDITOR, through an error.
8883 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8885 #ifndef SQLITE_NOHAVE_SYSTEM
8886 static void editFunc(
8887 sqlite3_context *context,
8889 sqlite3_value **argv
8891 const char *zEditor;
8892 char *zTempFile = 0;
8901 unsigned char *p = 0;
8904 zEditor = (const char*)sqlite3_value_text(argv[1]);
8906 zEditor = getenv("VISUAL");
8909 sqlite3_result_error(context, "no editor for edit()", -1);
8912 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8913 sqlite3_result_error(context, "NULL input to edit()", -1);
8916 db = sqlite3_context_db_handle(context);
8918 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8920 sqlite3_uint64 r = 0;
8921 sqlite3_randomness(sizeof(r), &r);
8922 zTempFile = sqlite3_mprintf("temp%llx", r);
8924 sqlite3_result_error_nomem(context);
8928 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8929 /* When writing the file to be edited, do \n to \r\n conversions on systems
8930 ** that want \r\n line endings */
8931 f = fopen(zTempFile, bBin ? "wb" : "w");
8933 sqlite3_result_error(context, "edit() cannot open temp file", -1);
8936 sz = sqlite3_value_bytes(argv[0]);
8938 x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8940 const char *z = (const char*)sqlite3_value_text(argv[0]);
8941 /* Remember whether or not the value originally contained \r\n */
8942 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
8943 x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8948 sqlite3_result_error(context, "edit() could not write the whole file", -1);
8951 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8953 sqlite3_result_error_nomem(context);
8959 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8962 f = fopen(zTempFile, "rb");
8964 sqlite3_result_error(context,
8965 "edit() cannot reopen temp file after edit", -1);
8968 fseek(f, 0, SEEK_END);
8971 p = sqlite3_malloc64( sz+(bBin==0) );
8973 sqlite3_result_error_nomem(context);
8976 x = fread(p, 1, sz, f);
8980 sqlite3_result_error(context, "could not read back the whole file", -1);
8984 sqlite3_result_blob64(context, p, sz, sqlite3_free);
8988 /* If the original contains \r\n then do no conversions back to \n */
8991 /* If the file did not originally contain \r\n then convert any new
8992 ** \r\n back into \n */
8993 for(i=j=0; i<sz; i++){
8994 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
9000 sqlite3_result_text64(context, (const char*)p, sz,
9001 sqlite3_free, SQLITE_UTF8);
9008 sqlite3_free(zTempFile);
9011 #endif /* SQLITE_NOHAVE_SYSTEM */
9014 ** Save or restore the current output mode
9016 static void outputModePush(ShellState *p){
9017 p->modePrior = p->mode;
9018 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
9019 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
9021 static void outputModePop(ShellState *p){
9022 p->mode = p->modePrior;
9023 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
9024 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
9028 ** Output the given string as a hex-encoded blob (eg. X'1234' )
9030 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
9032 char *zBlob = (char *)pBlob;
9033 raw_printf(out,"X'");
9034 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
9035 raw_printf(out,"'");
9039 ** Find a string that is not found anywhere in z[]. Return a pointer
9042 ** Try to use zA and zB first. If both of those are already found in z[]
9043 ** then make up some string and store it in the buffer zBuf.
9045 static const char *unused_string(
9046 const char *z, /* Result must not appear anywhere in z */
9047 const char *zA, const char *zB, /* Try these first */
9048 char *zBuf /* Space to store a generated string */
9051 if( strstr(z, zA)==0 ) return zA;
9052 if( strstr(z, zB)==0 ) return zB;
9054 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
9055 }while( strstr(z,zBuf)!=0 );
9060 ** Output the given string as a quoted string using SQL quoting conventions.
9062 ** See also: output_quoted_escaped_string()
9064 static void output_quoted_string(FILE *out, const char *z){
9067 setBinaryMode(out, 1);
9068 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9070 utf8_printf(out,"'%s'",z);
9072 raw_printf(out, "'");
9074 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
9077 utf8_printf(out, "%.*s", i, z);
9081 raw_printf(out, "'");
9089 raw_printf(out, "'");
9091 setTextMode(out, 1);
9095 ** Output the given string as a quoted string using SQL quoting conventions.
9096 ** Additionallly , escape the "\n" and "\r" characters so that they do not
9097 ** get corrupted by end-of-line translation facilities in some operating
9100 ** This is like output_quoted_string() but with the addition of the \r\n
9101 ** escape mechanism.
9103 static void output_quoted_escaped_string(FILE *out, const char *z){
9106 setBinaryMode(out, 1);
9107 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
9109 utf8_printf(out,"'%s'",z);
9111 const char *zNL = 0;
9112 const char *zCR = 0;
9115 char zBuf1[20], zBuf2[20];
9116 for(i=0; z[i]; i++){
9117 if( z[i]=='\n' ) nNL++;
9118 if( z[i]=='\r' ) nCR++;
9121 raw_printf(out, "replace(");
9122 zNL = unused_string(z, "\\n", "\\012", zBuf1);
9125 raw_printf(out, "replace(");
9126 zCR = unused_string(z, "\\r", "\\015", zBuf2);
9128 raw_printf(out, "'");
9130 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
9133 utf8_printf(out, "%.*s", i, z);
9137 raw_printf(out, "'");
9145 raw_printf(out, "%s", zNL);
9148 raw_printf(out, "%s", zCR);
9150 raw_printf(out, "'");
9152 raw_printf(out, ",'%s',char(13))", zCR);
9155 raw_printf(out, ",'%s',char(10))", zNL);
9158 setTextMode(out, 1);
9162 ** Output the given string as a quoted according to C or TCL quoting rules.
9164 static void output_c_string(FILE *out, const char *z){
9167 while( (c = *(z++))!=0 ){
9174 }else if( c=='\t' ){
9177 }else if( c=='\n' ){
9180 }else if( c=='\r' ){
9183 }else if( !isprint(c&0xff) ){
9184 raw_printf(out, "\\%03o", c&0xff);
9193 ** Output the given string with characters that are special to
9196 static void output_html_string(FILE *out, const char *z){
9208 utf8_printf(out,"%.*s",i,z);
9211 raw_printf(out,"<");
9212 }else if( z[i]=='&' ){
9213 raw_printf(out,"&");
9214 }else if( z[i]=='>' ){
9215 raw_printf(out,">");
9216 }else if( z[i]=='\"' ){
9217 raw_printf(out,""");
9218 }else if( z[i]=='\'' ){
9219 raw_printf(out,"'");
9228 ** If a field contains any character identified by a 1 in the following
9229 ** array, then the string must be quoted for CSV.
9231 static const char needCsvQuote[] = {
9232 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9233 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9234 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
9235 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9236 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9237 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9238 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
9239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
9240 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9241 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9242 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9243 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9244 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9245 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9246 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9247 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
9251 ** Output a single term of CSV. Actually, p->colSeparator is used for
9252 ** the separator, which may or may not be a comma. p->nullValue is
9253 ** the null value. Strings are quoted if necessary. The separator
9254 ** is only issued if bSep is true.
9256 static void output_csv(ShellState *p, const char *z, int bSep){
9259 utf8_printf(out,"%s",p->nullValue);
9262 int nSep = strlen30(p->colSeparator);
9263 for(i=0; z[i]; i++){
9264 if( needCsvQuote[((unsigned char*)z)[i]]
9265 || (z[i]==p->colSeparator[0] &&
9266 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
9272 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
9273 utf8_printf(out, "%s", zQuoted);
9274 sqlite3_free(zQuoted);
9276 utf8_printf(out, "%s", z);
9280 utf8_printf(p->out, "%s", p->colSeparator);
9285 ** This routine runs when the user presses Ctrl-C
9287 static void interrupt_handler(int NotUsed){
9288 UNUSED_PARAMETER(NotUsed);
9290 if( seenInterrupt>2 ) exit(1);
9291 if( globalDb ) sqlite3_interrupt(globalDb);
9294 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
9296 ** This routine runs for console events (e.g. Ctrl-C) on Win32
9298 static BOOL WINAPI ConsoleCtrlHandler(
9299 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
9301 if( dwCtrlType==CTRL_C_EVENT ){
9302 interrupt_handler(0);
9309 #ifndef SQLITE_OMIT_AUTHORIZATION
9311 ** When the ".auth ON" is set, the following authorizer callback is
9312 ** invoked. It always returns SQLITE_OK.
9314 static int shellAuth(
9322 ShellState *p = (ShellState*)pClientData;
9323 static const char *azAction[] = { 0,
9324 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
9325 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
9326 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
9327 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
9328 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
9329 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
9330 "PRAGMA", "READ", "SELECT",
9331 "TRANSACTION", "UPDATE", "ATTACH",
9332 "DETACH", "ALTER_TABLE", "REINDEX",
9333 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
9334 "FUNCTION", "SAVEPOINT", "RECURSIVE"
9342 utf8_printf(p->out, "authorizer: %s", azAction[op]);
9344 raw_printf(p->out, " ");
9346 output_c_string(p->out, az[i]);
9348 raw_printf(p->out, "NULL");
9351 raw_printf(p->out, "\n");
9357 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
9359 ** This routine converts some CREATE TABLE statements for shadow tables
9360 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
9362 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
9363 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
9364 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
9366 utf8_printf(out, "%s%s", z, zTail);
9369 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
9372 printSchemaLine(out, z, zTail);
9377 ** Return true if string z[] has nothing but whitespace and comments to the
9378 ** end of the first line.
9380 static int wsToEol(const char *z){
9382 for(i=0; z[i]; i++){
9383 if( z[i]=='\n' ) return 1;
9384 if( IsSpace(z[i]) ) continue;
9385 if( z[i]=='-' && z[i+1]=='-' ) return 1;
9392 ** Add a new entry to the EXPLAIN QUERY PLAN data
9394 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
9396 int nText = strlen30(zText);
9397 if( p->autoEQPtest ){
9398 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
9400 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
9401 if( pNew==0 ) shell_out_of_memory();
9402 pNew->iEqpId = iEqpId;
9403 pNew->iParentId = p2;
9404 memcpy(pNew->zText, zText, nText+1);
9406 if( p->sGraph.pLast ){
9407 p->sGraph.pLast->pNext = pNew;
9409 p->sGraph.pRow = pNew;
9411 p->sGraph.pLast = pNew;
9415 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
9418 static void eqp_reset(ShellState *p){
9419 EQPGraphRow *pRow, *pNext;
9420 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
9421 pNext = pRow->pNext;
9424 memset(&p->sGraph, 0, sizeof(p->sGraph));
9427 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
9428 ** pOld, or return the first such line if pOld is NULL
9430 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
9431 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
9432 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
9436 /* Render a single level of the graph that has iEqpId as its parent. Called
9437 ** recursively to render sublevels.
9439 static void eqp_render_level(ShellState *p, int iEqpId){
9440 EQPGraphRow *pRow, *pNext;
9441 int n = strlen30(p->sGraph.zPrefix);
9443 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
9444 pNext = eqp_next_row(p, iEqpId, pRow);
9446 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix, pNext ? "|--" : "`--", z);
9447 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
9448 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
9449 eqp_render_level(p, pRow->iEqpId);
9450 p->sGraph.zPrefix[n] = 0;
9456 ** Display and reset the EXPLAIN QUERY PLAN data
9458 static void eqp_render(ShellState *p){
9459 EQPGraphRow *pRow = p->sGraph.pRow;
9461 if( pRow->zText[0]=='-' ){
9462 if( pRow->pNext==0 ){
9466 utf8_printf(p->out, "%s\n", pRow->zText+3);
9467 p->sGraph.pRow = pRow->pNext;
9470 utf8_printf(p->out, "QUERY PLAN\n");
9472 p->sGraph.zPrefix[0] = 0;
9473 eqp_render_level(p, 0);
9478 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
9480 ** Progress handler callback.
9482 static int progress_handler(void *pClientData) {
9483 ShellState *p = (ShellState*)pClientData;
9485 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
9486 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
9487 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
9488 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
9491 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
9492 raw_printf(p->out, "Progress %u\n", p->nProgress);
9496 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
9499 ** This is the callback routine that the shell
9500 ** invokes for each row of a query result.
9502 static int shell_callback(
9504 int nArg, /* Number of result columns */
9505 char **azArg, /* Text of each result column */
9506 char **azCol, /* Column names */
9507 int *aiType /* Column types */
9510 ShellState *p = (ShellState*)pArg;
9512 if( azArg==0 ) return 0;
9516 if( azArg==0 ) break;
9517 for(i=0; i<nArg; i++){
9518 int len = strlen30(azCol[i] ? azCol[i] : "");
9519 if( len>w ) w = len;
9521 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
9522 for(i=0; i<nArg; i++){
9523 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
9524 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
9530 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
9531 const int *colWidth;
9534 if( p->cMode==MODE_Column ){
9535 colWidth = p->colWidth;
9536 showHdr = p->showHeader;
9537 rowSep = p->rowSeparator;
9539 colWidth = aExplainWidths;
9544 for(i=0; i<nArg; i++){
9546 if( i<ArraySize(p->colWidth) ){
9552 w = strlenChar(azCol[i] ? azCol[i] : "");
9554 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
9557 if( i<ArraySize(p->actualWidth) ){
9558 p->actualWidth[i] = w;
9561 utf8_width_print(p->out, w, azCol[i]);
9562 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9566 for(i=0; i<nArg; i++){
9568 if( i<ArraySize(p->actualWidth) ){
9569 w = p->actualWidth[i];
9574 utf8_printf(p->out,"%-*.*s%s",w,w,
9575 "----------------------------------------------------------"
9576 "----------------------------------------------------------",
9577 i==nArg-1 ? rowSep : " ");
9581 if( azArg==0 ) break;
9582 for(i=0; i<nArg; i++){
9584 if( i<ArraySize(p->actualWidth) ){
9585 w = p->actualWidth[i];
9589 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
9590 w = strlenChar(azArg[i]);
9592 if( i==1 && p->aiIndent && p->pStmt ){
9593 if( p->iIndent<p->nIndent ){
9594 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
9598 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
9599 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
9603 case MODE_Semi: { /* .schema and .fullschema output */
9604 printSchemaLine(p->out, azArg[0], ";\n");
9607 case MODE_Pretty: { /* .schema and .fullschema with --indent */
9615 if( azArg[0]==0 ) break;
9616 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
9617 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
9619 utf8_printf(p->out, "%s;\n", azArg[0]);
9622 z = sqlite3_mprintf("%s", azArg[0]);
9624 for(i=0; IsSpace(z[i]); i++){}
9625 for(; (c = z[i])!=0; i++){
9627 if( z[j-1]=='\r' ) z[j-1] = '\n';
9628 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
9629 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
9634 while( j>0 && IsSpace(z[j-1]) ){ j--; }
9636 if( strlen30(z)>=79 ){
9637 for(i=j=0; (c = z[i])!=0; i++){ /* Copy changes from z[i] back to z[j] */
9640 }else if( c=='"' || c=='\'' || c=='`' ){
9644 }else if( c=='-' && z[i+1]=='-' ){
9650 if( nLine>0 && nParen==0 && j>0 ){
9651 printSchemaLineN(p->out, z, j, "\n");
9656 if( nParen==1 && cEnd==0
9657 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
9660 printSchemaLineN(p->out, z, j, "\n ");
9663 while( IsSpace(z[i+1]) ){ i++; }
9668 printSchemaLine(p->out, z, ";\n");
9673 if( p->cnt++==0 && p->showHeader ){
9674 for(i=0; i<nArg; i++){
9675 utf8_printf(p->out,"%s%s",azCol[i],
9676 i==nArg-1 ? p->rowSeparator : p->colSeparator);
9679 if( azArg==0 ) break;
9680 for(i=0; i<nArg; i++){
9682 if( z==0 ) z = p->nullValue;
9683 utf8_printf(p->out, "%s", z);
9685 utf8_printf(p->out, "%s", p->colSeparator);
9687 utf8_printf(p->out, "%s", p->rowSeparator);
9693 if( p->cnt++==0 && p->showHeader ){
9694 raw_printf(p->out,"<TR>");
9695 for(i=0; i<nArg; i++){
9696 raw_printf(p->out,"<TH>");
9697 output_html_string(p->out, azCol[i]);
9698 raw_printf(p->out,"</TH>\n");
9700 raw_printf(p->out,"</TR>\n");
9702 if( azArg==0 ) break;
9703 raw_printf(p->out,"<TR>");
9704 for(i=0; i<nArg; i++){
9705 raw_printf(p->out,"<TD>");
9706 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9707 raw_printf(p->out,"</TD>\n");
9709 raw_printf(p->out,"</TR>\n");
9713 if( p->cnt++==0 && p->showHeader ){
9714 for(i=0; i<nArg; i++){
9715 output_c_string(p->out,azCol[i] ? azCol[i] : "");
9716 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9718 utf8_printf(p->out, "%s", p->rowSeparator);
9720 if( azArg==0 ) break;
9721 for(i=0; i<nArg; i++){
9722 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
9723 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
9725 utf8_printf(p->out, "%s", p->rowSeparator);
9729 setBinaryMode(p->out, 1);
9730 if( p->cnt++==0 && p->showHeader ){
9731 for(i=0; i<nArg; i++){
9732 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
9734 utf8_printf(p->out, "%s", p->rowSeparator);
9737 for(i=0; i<nArg; i++){
9738 output_csv(p, azArg[i], i<nArg-1);
9740 utf8_printf(p->out, "%s", p->rowSeparator);
9742 setTextMode(p->out, 1);
9746 if( azArg==0 ) break;
9747 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
9748 if( p->showHeader ){
9749 raw_printf(p->out,"(");
9750 for(i=0; i<nArg; i++){
9751 if( i>0 ) raw_printf(p->out, ",");
9752 if( quoteChar(azCol[i]) ){
9753 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
9754 utf8_printf(p->out, "%s", z);
9757 raw_printf(p->out, "%s", azCol[i]);
9760 raw_printf(p->out,")");
9763 for(i=0; i<nArg; i++){
9764 raw_printf(p->out, i>0 ? "," : " VALUES(");
9765 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9766 utf8_printf(p->out,"NULL");
9767 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9768 if( ShellHasFlag(p, SHFLG_Newlines) ){
9769 output_quoted_string(p->out, azArg[i]);
9771 output_quoted_escaped_string(p->out, azArg[i]);
9773 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9774 utf8_printf(p->out,"%s", azArg[i]);
9775 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9777 double r = sqlite3_column_double(p->pStmt, i);
9779 memcpy(&ur,&r,sizeof(r));
9780 if( ur==0x7ff0000000000000LL ){
9781 raw_printf(p->out, "1e999");
9782 }else if( ur==0xfff0000000000000LL ){
9783 raw_printf(p->out, "-1e999");
9785 sqlite3_snprintf(50,z,"%!.20g", r);
9786 raw_printf(p->out, "%s", z);
9788 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9789 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9790 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9791 output_hex_blob(p->out, pBlob, nBlob);
9792 }else if( isNumber(azArg[i], 0) ){
9793 utf8_printf(p->out,"%s", azArg[i]);
9794 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
9795 output_quoted_string(p->out, azArg[i]);
9797 output_quoted_escaped_string(p->out, azArg[i]);
9800 raw_printf(p->out,");\n");
9804 if( azArg==0 ) break;
9805 if( p->cnt==0 && p->showHeader ){
9806 for(i=0; i<nArg; i++){
9807 if( i>0 ) raw_printf(p->out, ",");
9808 output_quoted_string(p->out, azCol[i]);
9810 raw_printf(p->out,"\n");
9813 for(i=0; i<nArg; i++){
9814 if( i>0 ) raw_printf(p->out, ",");
9815 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
9816 utf8_printf(p->out,"NULL");
9817 }else if( aiType && aiType[i]==SQLITE_TEXT ){
9818 output_quoted_string(p->out, azArg[i]);
9819 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
9820 utf8_printf(p->out,"%s", azArg[i]);
9821 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
9823 double r = sqlite3_column_double(p->pStmt, i);
9824 sqlite3_snprintf(50,z,"%!.20g", r);
9825 raw_printf(p->out, "%s", z);
9826 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
9827 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
9828 int nBlob = sqlite3_column_bytes(p->pStmt, i);
9829 output_hex_blob(p->out, pBlob, nBlob);
9830 }else if( isNumber(azArg[i], 0) ){
9831 utf8_printf(p->out,"%s", azArg[i]);
9833 output_quoted_string(p->out, azArg[i]);
9836 raw_printf(p->out,"\n");
9840 if( p->cnt++==0 && p->showHeader ){
9841 for(i=0; i<nArg; i++){
9842 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9843 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
9845 utf8_printf(p->out, "%s", p->rowSeparator);
9847 if( azArg==0 ) break;
9848 for(i=0; i<nArg; i++){
9849 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
9850 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
9852 utf8_printf(p->out, "%s", p->rowSeparator);
9856 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
9864 ** This is the callback routine that the SQLite library
9865 ** invokes for each row of a query result.
9867 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
9868 /* since we don't have type info, call the shell_callback with a NULL value */
9869 return shell_callback(pArg, nArg, azArg, azCol, NULL);
9873 ** This is the callback routine from sqlite3_exec() that appends all
9874 ** output onto the end of a ShellText object.
9876 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
9877 ShellText *p = (ShellText*)pArg;
9879 UNUSED_PARAMETER(az);
9880 if( azArg==0 ) return 0;
9881 if( p->n ) appendText(p, "|", 0);
9882 for(i=0; i<nArg; i++){
9883 if( i ) appendText(p, ",", 0);
9884 if( azArg[i] ) appendText(p, azArg[i], 0);
9890 ** Generate an appropriate SELFTEST table in the main database.
9892 static void createSelftestTable(ShellState *p){
9895 "SAVEPOINT selftest_init;\n"
9896 "CREATE TABLE IF NOT EXISTS selftest(\n"
9897 " tno INTEGER PRIMARY KEY,\n" /* Test number */
9898 " op TEXT,\n" /* Operator: memo run */
9899 " cmd TEXT,\n" /* Command text */
9900 " ans TEXT\n" /* Desired answer */
9902 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
9903 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
9904 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
9905 " 'memo','Tests generated by --init');\n"
9906 "INSERT INTO [_shell$self]\n"
9908 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
9909 "FROM sqlite_master ORDER BY 2'',224))',\n"
9910 " hex(sha3_query('SELECT type,name,tbl_name,sql "
9911 "FROM sqlite_master ORDER BY 2',224));\n"
9912 "INSERT INTO [_shell$self]\n"
9914 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
9915 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
9916 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
9918 " SELECT name FROM sqlite_master\n"
9919 " WHERE type='table'\n"
9920 " AND name<>'selftest'\n"
9921 " AND coalesce(rootpage,0)>0\n"
9924 "INSERT INTO [_shell$self]\n"
9925 " VALUES('run','PRAGMA integrity_check','ok');\n"
9926 "INSERT INTO selftest(tno,op,cmd,ans)"
9927 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
9928 "DROP TABLE [_shell$self];"
9931 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
9932 sqlite3_free(zErrMsg);
9934 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9939 ** Set the destination table field of the ShellState structure to
9940 ** the name of the table given. Escape any quote characters in the
9943 static void set_table_name(ShellState *p, const char *zName){
9948 if( p->zDestTable ){
9949 free(p->zDestTable);
9952 if( zName==0 ) return;
9953 cQuote = quoteChar(zName);
9954 n = strlen30(zName);
9955 if( cQuote ) n += n+2;
9956 z = p->zDestTable = malloc( n+1 );
9957 if( z==0 ) shell_out_of_memory();
9959 if( cQuote ) z[n++] = cQuote;
9960 for(i=0; zName[i]; i++){
9962 if( zName[i]==cQuote ) z[n++] = cQuote;
9964 if( cQuote ) z[n++] = cQuote;
9970 ** Execute a query statement that will generate SQL output. Print
9971 ** the result columns, comma-separated, on a line and then add a
9972 ** semicolon terminator to the end of that line.
9974 ** If the number of columns is 1 and that column contains text "--"
9975 ** then write the semicolon on a separate line. That way, if a
9976 ** "--" comment occurs at the end of the statement, the comment
9977 ** won't consume the semicolon terminator.
9979 static int run_table_dump_query(
9980 ShellState *p, /* Query context */
9981 const char *zSelect, /* SELECT statement to extract content */
9982 const char *zFirstRow /* Print before first row, if not NULL */
9984 sqlite3_stmt *pSelect;
9989 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9990 if( rc!=SQLITE_OK || !pSelect ){
9991 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9992 sqlite3_errmsg(p->db));
9993 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9996 rc = sqlite3_step(pSelect);
9997 nResult = sqlite3_column_count(pSelect);
9998 while( rc==SQLITE_ROW ){
10000 utf8_printf(p->out, "%s", zFirstRow);
10003 z = (const char*)sqlite3_column_text(pSelect, 0);
10004 utf8_printf(p->out, "%s", z);
10005 for(i=1; i<nResult; i++){
10006 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
10009 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
10011 raw_printf(p->out, "\n;\n");
10013 raw_printf(p->out, ";\n");
10015 rc = sqlite3_step(pSelect);
10017 rc = sqlite3_finalize(pSelect);
10018 if( rc!=SQLITE_OK ){
10019 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
10020 sqlite3_errmsg(p->db));
10021 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
10027 ** Allocate space and save off current error string.
10029 static char *save_err_msg(
10030 sqlite3 *db /* Database to query */
10032 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
10033 char *zErrMsg = sqlite3_malloc64(nErrMsg);
10035 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
10042 ** Attempt to display I/O stats on Linux using /proc/PID/io
10044 static void displayLinuxIoStats(FILE *out){
10047 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
10048 in = fopen(z, "rb");
10049 if( in==0 ) return;
10050 while( fgets(z, sizeof(z), in)!=0 ){
10051 static const struct {
10052 const char *zPattern;
10055 { "rchar: ", "Bytes received by read():" },
10056 { "wchar: ", "Bytes sent to write():" },
10057 { "syscr: ", "Read() system calls:" },
10058 { "syscw: ", "Write() system calls:" },
10059 { "read_bytes: ", "Bytes read from storage:" },
10060 { "write_bytes: ", "Bytes written to storage:" },
10061 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
10064 for(i=0; i<ArraySize(aTrans); i++){
10065 int n = strlen30(aTrans[i].zPattern);
10066 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
10067 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
10077 ** Display a single line of status using 64-bit values.
10079 static void displayStatLine(
10080 ShellState *p, /* The shell context */
10081 char *zLabel, /* Label for this one line */
10082 char *zFormat, /* Format for the result */
10083 int iStatusCtrl, /* Which status to display */
10084 int bReset /* True to reset the stats */
10086 sqlite3_int64 iCur = -1;
10087 sqlite3_int64 iHiwtr = -1;
10090 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
10091 for(i=0, nPercent=0; zFormat[i]; i++){
10092 if( zFormat[i]=='%' ) nPercent++;
10095 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
10097 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
10099 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
10103 ** Display memory stats.
10105 static int display_stats(
10106 sqlite3 *db, /* Database to query */
10107 ShellState *pArg, /* Pointer to ShellState */
10108 int bReset /* True to reset the stats */
10113 if( pArg==0 || pArg->out==0 ) return 0;
10116 if( pArg->pStmt && (pArg->statsOn & 2) ){
10118 sqlite3_stmt *pStmt = pArg->pStmt;
10120 nCol = sqlite3_column_count(pStmt);
10121 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
10122 for(i=0; i<nCol; i++){
10123 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
10124 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
10125 #ifndef SQLITE_OMIT_DECLTYPE
10126 sqlite3_snprintf(30, z+x, "declared type:");
10127 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
10129 #ifdef SQLITE_ENABLE_COLUMN_METADATA
10130 sqlite3_snprintf(30, z+x, "database name:");
10131 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
10132 sqlite3_snprintf(30, z+x, "table name:");
10133 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
10134 sqlite3_snprintf(30, z+x, "origin name:");
10135 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
10140 displayStatLine(pArg, "Memory Used:",
10141 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
10142 displayStatLine(pArg, "Number of Outstanding Allocations:",
10143 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
10144 if( pArg->shellFlgs & SHFLG_Pagecache ){
10145 displayStatLine(pArg, "Number of Pcache Pages Used:",
10146 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
10148 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
10149 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
10150 displayStatLine(pArg, "Largest Allocation:",
10151 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
10152 displayStatLine(pArg, "Largest Pcache Allocation:",
10153 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
10154 #ifdef YYTRACKMAXSTACKDEPTH
10155 displayStatLine(pArg, "Deepest Parser Stack:",
10156 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
10160 if( pArg->shellFlgs & SHFLG_Lookaside ){
10161 iHiwtr = iCur = -1;
10162 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
10163 &iCur, &iHiwtr, bReset);
10164 raw_printf(pArg->out,
10165 "Lookaside Slots Used: %d (max %d)\n",
10167 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
10168 &iCur, &iHiwtr, bReset);
10169 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
10171 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
10172 &iCur, &iHiwtr, bReset);
10173 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
10175 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
10176 &iCur, &iHiwtr, bReset);
10177 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
10180 iHiwtr = iCur = -1;
10181 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
10182 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
10184 iHiwtr = iCur = -1;
10185 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
10186 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
10187 iHiwtr = iCur = -1;
10188 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
10189 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
10190 iHiwtr = iCur = -1;
10191 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
10192 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
10193 iHiwtr = iCur = -1;
10194 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
10195 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
10196 iHiwtr = iCur = -1;
10197 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
10198 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
10200 iHiwtr = iCur = -1;
10201 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
10202 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
10207 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
10209 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
10210 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
10211 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
10212 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
10213 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
10214 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
10215 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
10216 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE, bReset);
10217 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
10218 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
10219 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
10220 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
10221 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
10225 displayLinuxIoStats(pArg->out);
10228 /* Do not remove this machine readable comment: extra-stats-output-here */
10234 ** Display scan stats.
10236 static void display_scanstats(
10237 sqlite3 *db, /* Database to query */
10238 ShellState *pArg /* Pointer to ShellState */
10240 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
10241 UNUSED_PARAMETER(db);
10242 UNUSED_PARAMETER(pArg);
10245 raw_printf(pArg->out, "-------- scanstats --------\n");
10247 for(k=0; k<=mx; k++){
10248 double rEstLoop = 1.0;
10249 for(i=n=0; 1; i++){
10250 sqlite3_stmt *p = pArg->pStmt;
10251 sqlite3_int64 nLoop, nVisit;
10254 const char *zExplain;
10255 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
10258 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
10259 if( iSid>mx ) mx = iSid;
10260 if( iSid!=k ) continue;
10262 rEstLoop = (double)nLoop;
10263 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
10266 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
10267 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
10268 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
10269 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
10271 raw_printf(pArg->out,
10272 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
10273 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
10277 raw_printf(pArg->out, "---------------------------\n");
10282 ** Parameter azArray points to a zero-terminated array of strings. zStr
10283 ** points to a single nul-terminated string. Return non-zero if zStr
10284 ** is equal, according to strcmp(), to any of the strings in the array.
10285 ** Otherwise, return zero.
10287 static int str_in_array(const char *zStr, const char **azArray){
10289 for(i=0; azArray[i]; i++){
10290 if( 0==strcmp(zStr, azArray[i]) ) return 1;
10296 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
10297 ** and populate the ShellState.aiIndent[] array with the number of
10298 ** spaces each opcode should be indented before it is output.
10300 ** The indenting rules are:
10302 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
10303 ** all opcodes that occur between the p2 jump destination and the opcode
10304 ** itself by 2 spaces.
10306 ** * For each "Goto", if the jump destination is earlier in the program
10307 ** and ends on one of:
10308 ** Yield SeekGt SeekLt RowSetRead Rewind
10309 ** or if the P1 parameter is one instead of zero,
10310 ** then indent all opcodes between the earlier instruction
10311 ** and "Goto" by 2 spaces.
10313 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
10314 const char *zSql; /* The text of the SQL statement */
10315 const char *z; /* Used to check if this is an EXPLAIN */
10316 int *abYield = 0; /* True if op is an OP_Yield */
10317 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
10318 int iOp; /* Index of operation in p->aiIndent[] */
10320 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
10321 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
10323 const char *azGoto[] = { "Goto", 0 };
10325 /* Try to figure out if this is really an EXPLAIN statement. If this
10326 ** cannot be verified, return early. */
10327 if( sqlite3_column_count(pSql)!=8 ){
10328 p->cMode = p->mode;
10331 zSql = sqlite3_sql(pSql);
10332 if( zSql==0 ) return;
10333 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
10334 if( sqlite3_strnicmp(z, "explain", 7) ){
10335 p->cMode = p->mode;
10339 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
10341 int iAddr = sqlite3_column_int(pSql, 0);
10342 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
10344 /* Set p2 to the P2 field of the current opcode. Then, assuming that
10345 ** p2 is an instruction address, set variable p2op to the index of that
10346 ** instruction in the aiIndent[] array. p2 and p2op may be different if
10347 ** the current instruction is part of a sub-program generated by an
10348 ** SQL trigger or foreign key. */
10349 int p2 = sqlite3_column_int(pSql, 3);
10350 int p2op = (p2 + (iOp-iAddr));
10352 /* Grow the p->aiIndent array as required */
10355 /* Do further verfication that this is explain output. Abort if
10357 static const char *explainCols[] = {
10358 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
10360 for(jj=0; jj<ArraySize(explainCols); jj++){
10361 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
10362 p->cMode = p->mode;
10363 sqlite3_reset(pSql);
10369 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
10370 if( p->aiIndent==0 ) shell_out_of_memory();
10371 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
10372 if( abYield==0 ) shell_out_of_memory();
10374 abYield[iOp] = str_in_array(zOp, azYield);
10375 p->aiIndent[iOp] = 0;
10376 p->nIndent = iOp+1;
10378 if( str_in_array(zOp, azNext) ){
10379 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10381 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
10382 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
10384 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
10389 sqlite3_free(abYield);
10390 sqlite3_reset(pSql);
10394 ** Free the array allocated by explain_data_prepare().
10396 static void explain_data_delete(ShellState *p){
10397 sqlite3_free(p->aiIndent);
10404 ** Disable and restore .wheretrace and .selecttrace settings.
10406 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10407 extern int sqlite3SelectTrace;
10408 static int savedSelectTrace;
10410 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10411 extern int sqlite3WhereTrace;
10412 static int savedWhereTrace;
10414 static void disable_debug_trace_modes(void){
10415 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10416 savedSelectTrace = sqlite3SelectTrace;
10417 sqlite3SelectTrace = 0;
10419 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10420 savedWhereTrace = sqlite3WhereTrace;
10421 sqlite3WhereTrace = 0;
10424 static void restore_debug_trace_modes(void){
10425 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
10426 sqlite3SelectTrace = savedSelectTrace;
10428 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
10429 sqlite3WhereTrace = savedWhereTrace;
10433 /* Create the TEMP table used to store parameter bindings */
10434 static void bind_table_init(ShellState *p){
10436 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
10437 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
10438 sqlite3_exec(p->db,
10439 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
10440 " key TEXT PRIMARY KEY,\n"
10442 ") WITHOUT ROWID;",
10444 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
10448 ** Bind parameters on a prepared statement.
10450 ** Parameter bindings are taken from a TEMP table of the form:
10452 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
10455 ** No bindings occur if this table does not exist. The special character '$'
10456 ** is included in the table name to help prevent collisions with actual tables.
10457 ** The table must be in the TEMP schema.
10459 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
10463 sqlite3_stmt *pQ = 0;
10465 nVar = sqlite3_bind_parameter_count(pStmt);
10466 if( nVar==0 ) return; /* Nothing to do */
10467 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
10468 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
10469 return; /* Parameter table does not exist */
10471 rc = sqlite3_prepare_v2(pArg->db,
10472 "SELECT value FROM temp.sqlite_parameters"
10473 " WHERE key=?1", -1, &pQ, 0);
10474 if( rc || pQ==0 ) return;
10475 for(i=1; i<=nVar; i++){
10477 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
10479 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
10482 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
10483 if( sqlite3_step(pQ)==SQLITE_ROW ){
10484 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
10486 sqlite3_bind_null(pStmt, i);
10490 sqlite3_finalize(pQ);
10494 ** Run a prepared statement
10496 static void exec_prepared_stmt(
10497 ShellState *pArg, /* Pointer to ShellState */
10498 sqlite3_stmt *pStmt /* Statment to run */
10502 /* perform the first step. this will tell us if we
10503 ** have a result set or not and how wide it is.
10505 rc = sqlite3_step(pStmt);
10506 /* if we have a result set... */
10507 if( SQLITE_ROW == rc ){
10508 /* allocate space for col name ptr, value ptr, and type */
10509 int nCol = sqlite3_column_count(pStmt);
10510 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10514 char **azCols = (char **)pData; /* Names of result columns */
10515 char **azVals = &azCols[nCol]; /* Results */
10516 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10518 assert(sizeof(int) <= sizeof(char *));
10519 /* save off ptrs to column names */
10520 for(i=0; i<nCol; i++){
10521 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10524 /* extract the data and data types */
10525 for(i=0; i<nCol; i++){
10526 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10527 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10530 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10532 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10534 break; /* from for */
10538 /* if data and types extracted successfully... */
10539 if( SQLITE_ROW == rc ){
10540 /* call the supplied callback with the result row data */
10541 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10544 rc = sqlite3_step(pStmt);
10547 } while( SQLITE_ROW == rc );
10548 sqlite3_free(pData);
10553 #ifndef SQLITE_OMIT_VIRTUALTABLE
10555 ** This function is called to process SQL if the previous shell command
10556 ** was ".expert". It passes the SQL in the second argument directly to
10557 ** the sqlite3expert object.
10559 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10560 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10561 ** an English language error message. It is the responsibility of the
10562 ** caller to eventually free this buffer using sqlite3_free().
10564 static int expertHandleSQL(
10565 ShellState *pState,
10569 assert( pState->expert.pExpert );
10570 assert( pzErr==0 || *pzErr==0 );
10571 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10575 ** This function is called either to silently clean up the object
10576 ** created by the ".expert" command (if bCancel==1), or to generate a
10577 ** report from it and then clean it up (if bCancel==0).
10579 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10580 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10581 ** an English language error message. It is the responsibility of the
10582 ** caller to eventually free this buffer using sqlite3_free().
10584 static int expertFinish(
10585 ShellState *pState,
10589 int rc = SQLITE_OK;
10590 sqlite3expert *p = pState->expert.pExpert;
10592 assert( bCancel || pzErr==0 || *pzErr==0 );
10594 FILE *out = pState->out;
10595 int bVerbose = pState->expert.bVerbose;
10597 rc = sqlite3_expert_analyze(p, pzErr);
10598 if( rc==SQLITE_OK ){
10599 int nQuery = sqlite3_expert_count(p);
10603 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10604 raw_printf(out, "-- Candidates -----------------------------\n");
10605 raw_printf(out, "%s\n", zCand);
10607 for(i=0; i<nQuery; i++){
10608 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10609 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10610 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10611 if( zIdx==0 ) zIdx = "(no new indexes)\n";
10613 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10614 raw_printf(out, "%s\n\n", zSql);
10616 raw_printf(out, "%s\n", zIdx);
10617 raw_printf(out, "%s\n", zEQP);
10621 sqlite3_expert_destroy(p);
10622 pState->expert.pExpert = 0;
10627 ** Implementation of ".expert" dot command.
10629 static int expertDotCommand(
10630 ShellState *pState, /* Current shell tool state */
10631 char **azArg, /* Array of arguments passed to dot command */
10632 int nArg /* Number of entries in azArg[] */
10634 int rc = SQLITE_OK;
10639 assert( pState->expert.pExpert==0 );
10640 memset(&pState->expert, 0, sizeof(ExpertInfo));
10642 for(i=1; rc==SQLITE_OK && i<nArg; i++){
10643 char *z = azArg[i];
10645 if( z[0]=='-' && z[1]=='-' ) z++;
10647 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10648 pState->expert.bVerbose = 1;
10650 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10652 raw_printf(stderr, "option requires an argument: %s\n", z);
10655 iSample = (int)integerValue(azArg[++i]);
10656 if( iSample<0 || iSample>100 ){
10657 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10663 raw_printf(stderr, "unknown option: %s\n", z);
10668 if( rc==SQLITE_OK ){
10669 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10670 if( pState->expert.pExpert==0 ){
10671 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10674 sqlite3_expert_config(
10675 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10682 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10685 ** Execute a statement or set of statements. Print
10686 ** any result rows/columns depending on the current mode
10687 ** set via the supplied callback.
10689 ** This is very similar to SQLite's built-in sqlite3_exec()
10690 ** function except it takes a slightly different callback
10691 ** and callback data argument.
10693 static int shell_exec(
10694 ShellState *pArg, /* Pointer to ShellState */
10695 const char *zSql, /* SQL to be evaluated */
10696 char **pzErrMsg /* Error msg written here */
10698 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10699 int rc = SQLITE_OK; /* Return Code */
10701 const char *zLeftover; /* Tail of unprocessed SQL */
10702 sqlite3 *db = pArg->db;
10708 #ifndef SQLITE_OMIT_VIRTUALTABLE
10709 if( pArg->expert.pExpert ){
10710 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10711 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10715 while( zSql[0] && (SQLITE_OK == rc) ){
10716 static const char *zStmtSql;
10717 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10718 if( SQLITE_OK != rc ){
10720 *pzErrMsg = save_err_msg(db);
10724 /* this happens for a comment or white-space */
10726 while( IsSpace(zSql[0]) ) zSql++;
10729 zStmtSql = sqlite3_sql(pStmt);
10730 if( zStmtSql==0 ) zStmtSql = "";
10731 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10733 /* save off the prepared statment handle and reset row count */
10735 pArg->pStmt = pStmt;
10739 /* echo the sql statement if echo on */
10740 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10741 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10744 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10745 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
10746 sqlite3_stmt *pExplain;
10748 int triggerEQP = 0;
10749 disable_debug_trace_modes();
10750 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10751 if( pArg->autoEQP>=AUTOEQP_trigger ){
10752 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10754 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10755 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10756 if( rc==SQLITE_OK ){
10757 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10758 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10759 int iEqpId = sqlite3_column_int(pExplain, 0);
10760 int iParentId = sqlite3_column_int(pExplain, 1);
10761 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10762 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10766 sqlite3_finalize(pExplain);
10767 sqlite3_free(zEQP);
10768 if( pArg->autoEQP>=AUTOEQP_full ){
10769 /* Also do an EXPLAIN for ".eqp full" mode */
10770 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10771 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10772 if( rc==SQLITE_OK ){
10773 pArg->cMode = MODE_Explain;
10774 explain_data_prepare(pArg, pExplain);
10775 exec_prepared_stmt(pArg, pExplain);
10776 explain_data_delete(pArg);
10778 sqlite3_finalize(pExplain);
10779 sqlite3_free(zEQP);
10781 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10782 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10783 /* Reprepare pStmt before reactiving trace modes */
10784 sqlite3_finalize(pStmt);
10785 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10786 if( pArg ) pArg->pStmt = pStmt;
10788 restore_debug_trace_modes();
10792 pArg->cMode = pArg->mode;
10793 if( pArg->autoExplain ){
10794 if( sqlite3_stmt_isexplain(pStmt)==1 ){
10795 pArg->cMode = MODE_Explain;
10797 if( sqlite3_stmt_isexplain(pStmt)==2 ){
10798 pArg->cMode = MODE_EQP;
10802 /* If the shell is currently in ".explain" mode, gather the extra
10803 ** data required to add indents to the output.*/
10804 if( pArg->cMode==MODE_Explain ){
10805 explain_data_prepare(pArg, pStmt);
10809 bind_prepared_stmt(pArg, pStmt);
10810 exec_prepared_stmt(pArg, pStmt);
10811 explain_data_delete(pArg);
10814 /* print usage stats if stats on */
10815 if( pArg && pArg->statsOn ){
10816 display_stats(db, pArg, 0);
10819 /* print loop-counters if required */
10820 if( pArg && pArg->scanstatsOn ){
10821 display_scanstats(db, pArg);
10824 /* Finalize the statement just executed. If this fails, save a
10825 ** copy of the error message. Otherwise, set zSql to point to the
10826 ** next statement to execute. */
10827 rc2 = sqlite3_finalize(pStmt);
10828 if( rc!=SQLITE_NOMEM ) rc = rc2;
10829 if( rc==SQLITE_OK ){
10831 while( IsSpace(zSql[0]) ) zSql++;
10832 }else if( pzErrMsg ){
10833 *pzErrMsg = save_err_msg(db);
10836 /* clear saved stmt handle */
10838 pArg->pStmt = NULL;
10847 ** Release memory previously allocated by tableColumnList().
10849 static void freeColumnList(char **azCol){
10851 for(i=1; azCol[i]; i++){
10852 sqlite3_free(azCol[i]);
10854 /* azCol[0] is a static string */
10855 sqlite3_free(azCol);
10859 ** Return a list of pointers to strings which are the names of all
10860 ** columns in table zTab. The memory to hold the names is dynamically
10861 ** allocated and must be released by the caller using a subsequent call
10862 ** to freeColumnList().
10864 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
10865 ** value that needs to be preserved, then azCol[0] is filled in with the
10866 ** name of the rowid column.
10868 ** The first regular column in the table is azCol[1]. The list is terminated
10869 ** by an entry with azCol[i]==0.
10871 static char **tableColumnList(ShellState *p, const char *zTab){
10873 sqlite3_stmt *pStmt;
10877 int nPK = 0; /* Number of PRIMARY KEY columns seen */
10878 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
10879 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10882 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10883 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10884 sqlite3_free(zSql);
10886 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10887 if( nCol>=nAlloc-2 ){
10888 nAlloc = nAlloc*2 + nCol + 10;
10889 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10890 if( azCol==0 ) shell_out_of_memory();
10892 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10893 if( sqlite3_column_int(pStmt, 5) ){
10896 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10905 sqlite3_finalize(pStmt);
10906 if( azCol==0 ) return 0;
10910 /* The decision of whether or not a rowid really needs to be preserved
10911 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
10912 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
10913 ** rowids on tables where the rowid is inaccessible because there are other
10914 ** columns in the table named "rowid", "_rowid_", and "oid".
10916 if( preserveRowid && isIPK ){
10917 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10918 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
10919 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10920 ** ROWID aliases. To distinguish these cases, check to see if
10921 ** there is a "pk" entry in "PRAGMA index_list". There will be
10922 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10924 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10925 " WHERE origin='pk'", zTab);
10926 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10927 sqlite3_free(zSql);
10929 freeColumnList(azCol);
10932 rc = sqlite3_step(pStmt);
10933 sqlite3_finalize(pStmt);
10934 preserveRowid = rc==SQLITE_ROW;
10936 if( preserveRowid ){
10937 /* Only preserve the rowid if we can find a name to use for the
10939 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10941 for(j=0; j<3; j++){
10942 for(i=1; i<=nCol; i++){
10943 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10946 /* At this point, we know that azRowid[j] is not the name of any
10947 ** ordinary column in the table. Verify that azRowid[j] is a valid
10948 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
10949 ** tables will fail this last check */
10950 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10951 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10960 ** Toggle the reverse_unordered_selects setting.
10962 static void toggleSelectOrder(sqlite3 *db){
10963 sqlite3_stmt *pStmt = 0;
10966 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10967 if( sqlite3_step(pStmt)==SQLITE_ROW ){
10968 iSetting = sqlite3_column_int(pStmt, 0);
10970 sqlite3_finalize(pStmt);
10971 sqlite3_snprintf(sizeof(zStmt), zStmt,
10972 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10973 sqlite3_exec(db, zStmt, 0, 0, 0);
10977 ** This is a different callback routine used for dumping the database.
10978 ** Each row received by this callback consists of a table name,
10979 ** the table type ("index" or "table") and SQL to create the table.
10980 ** This routine should print text sufficient to recreate the table.
10982 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10984 const char *zTable;
10987 ShellState *p = (ShellState *)pArg;
10989 UNUSED_PARAMETER(azNotUsed);
10990 if( nArg!=3 || azArg==0 ) return 0;
10995 if( strcmp(zTable, "sqlite_sequence")==0 ){
10996 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10997 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10998 raw_printf(p->out, "ANALYZE sqlite_master;\n");
10999 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
11001 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
11003 if( !p->writableSchema ){
11004 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
11005 p->writableSchema = 1;
11007 zIns = sqlite3_mprintf(
11008 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
11009 "VALUES('table','%q','%q',0,'%q');",
11010 zTable, zTable, zSql);
11011 utf8_printf(p->out, "%s\n", zIns);
11012 sqlite3_free(zIns);
11015 printSchemaLine(p->out, zSql, ";\n");
11018 if( strcmp(zType, "table")==0 ){
11023 char *savedDestTable;
11026 azCol = tableColumnList(p, zTable);
11032 /* Always quote the table name, even if it appears to be pure ascii,
11033 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
11035 appendText(&sTable, zTable, quoteChar(zTable));
11036 /* If preserving the rowid, add a column list after the table name.
11037 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
11038 ** instead of the usual "INSERT INTO tab VALUES(...)".
11041 appendText(&sTable, "(", 0);
11042 appendText(&sTable, azCol[0], 0);
11043 for(i=1; azCol[i]; i++){
11044 appendText(&sTable, ",", 0);
11045 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
11047 appendText(&sTable, ")", 0);
11050 /* Build an appropriate SELECT statement */
11051 initText(&sSelect);
11052 appendText(&sSelect, "SELECT ", 0);
11054 appendText(&sSelect, azCol[0], 0);
11055 appendText(&sSelect, ",", 0);
11057 for(i=1; azCol[i]; i++){
11058 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11060 appendText(&sSelect, ",", 0);
11063 freeColumnList(azCol);
11064 appendText(&sSelect, " FROM ", 0);
11065 appendText(&sSelect, zTable, quoteChar(zTable));
11067 savedDestTable = p->zDestTable;
11068 savedMode = p->mode;
11069 p->zDestTable = sTable.z;
11070 p->mode = p->cMode = MODE_Insert;
11071 rc = shell_exec(p, sSelect.z, 0);
11072 if( (rc&0xff)==SQLITE_CORRUPT ){
11073 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11074 toggleSelectOrder(p->db);
11075 shell_exec(p, sSelect.z, 0);
11076 toggleSelectOrder(p->db);
11078 p->zDestTable = savedDestTable;
11079 p->mode = savedMode;
11081 freeText(&sSelect);
11082 if( rc ) p->nErr++;
11088 ** Run zQuery. Use dump_callback() as the callback routine so that
11089 ** the contents of the query are output as SQL statements.
11091 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11092 ** "ORDER BY rowid DESC" to the end.
11094 static int run_schema_dump_query(
11100 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11101 if( rc==SQLITE_CORRUPT ){
11103 int len = strlen30(zQuery);
11104 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11106 utf8_printf(p->out, "/****** %s ******/\n", zErr);
11107 sqlite3_free(zErr);
11110 zQ2 = malloc( len+100 );
11111 if( zQ2==0 ) return rc;
11112 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
11113 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
11115 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
11117 rc = SQLITE_CORRUPT;
11119 sqlite3_free(zErr);
11126 ** Text of help messages.
11128 ** The help text for each individual command begins with a line that starts
11129 ** with ".". Subsequent lines are supplimental information.
11131 ** There must be two or more spaces between the end of the command and the
11132 ** start of the description of what that command does.
11134 static const char *(azHelp[]) = {
11135 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11136 ".archive ... Manage SQL archives",
11137 " Each command must have exactly one of the following options:",
11138 " -c, --create Create a new archive",
11139 " -u, --update Add files or update files with changed mtime",
11140 " -i, --insert Like -u but always add even if mtime unchanged",
11141 " -t, --list List contents of archive",
11142 " -x, --extract Extract files from archive",
11143 " Optional arguments:",
11144 " -v, --verbose Print each filename as it is processed",
11145 " -f FILE, --file FILE Operate on archive FILE (default is current db)",
11146 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS",
11147 " -C DIR, --directory DIR Change to directory DIR to read/extract files",
11148 " -n, --dryrun Show the SQL that would have occurred",
11150 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar",
11151 " .ar -tf archive.sar # List members of archive.sar",
11152 " .ar -xvf archive.sar # Verbosely extract files from archive.sar",
11154 " http://sqlite.org/cli.html#sqlar_archive_support",
11156 #ifndef SQLITE_OMIT_AUTHORIZATION
11157 ".auth ON|OFF Show authorizer callbacks",
11159 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
11160 " --append Use the appendvfs",
11161 " --async Write to FILE without a journal and without fsync()",
11162 ".bail on|off Stop after hitting an error. Default OFF",
11163 ".binary on|off Turn binary output on or off. Default OFF",
11164 ".cd DIRECTORY Change the working directory to DIRECTORY",
11165 ".changes on|off Show number of rows changed by SQL",
11166 ".check GLOB Fail if output since .testcase does not match",
11167 ".clone NEWDB Clone data into NEWDB from the existing database",
11168 ".databases List names and files of attached databases",
11169 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
11170 ".dbinfo ?DB? Show status information about the database",
11171 ".dump ?TABLE? ... Render all database content as SQL",
11173 " --preserve-rowids Include ROWID values in the output",
11174 " --newlines Allow unescaped newline characters in output",
11175 " TABLE is LIKE pattern for the tables to dump",
11176 ".echo on|off Turn command echo on or off",
11177 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
11179 #ifdef SQLITE_DEBUG
11180 " test Show raw EXPLAIN QUERY PLAN output",
11181 " trace Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
11183 " trigger Like \"full\" but also show trigger bytecode",
11184 ".excel Display the output of next command in a spreadsheet",
11185 ".exit ?CODE? Exit this program with return-code CODE",
11186 ".expert EXPERIMENTAL. Suggest indexes for specified queries",
11187 /* Because explain mode comes on automatically now, the ".explain" mode
11188 ** is removed from the help screen. It is still supported for legacy, however */
11189 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic",*/
11190 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
11191 ".headers on|off Turn display of headers on or off",
11192 ".help ?-all? ?PATTERN? Show help text for PATTERN",
11193 ".import FILE TABLE Import data from FILE into TABLE",
11194 #ifndef SQLITE_OMIT_TEST_CONTROL
11195 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
11197 ".indexes ?TABLE? Show names of indexes",
11198 " If TABLE is specified, only show indexes for",
11199 " tables matching TABLE using the LIKE operator.",
11200 #ifdef SQLITE_ENABLE_IOTRACE
11201 ".iotrace FILE Enable I/O diagnostic logging to FILE",
11203 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
11204 ".lint OPTIONS Report potential schema issues.",
11206 " fkey-indexes Find missing foreign key indexes",
11207 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11208 ".load FILE ?ENTRY? Load an extension library",
11210 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
11211 ".mode MODE ?TABLE? Set output mode",
11212 " MODE is one of:",
11213 " ascii Columns/rows delimited by 0x1F and 0x1E",
11214 " csv Comma-separated values",
11215 " column Left-aligned columns. (See .width)",
11216 " html HTML <table> code",
11217 " insert SQL insert statements for TABLE",
11218 " line One value per line",
11219 " list Values delimited by \"|\"",
11220 " quote Escape answers as for SQL",
11221 " tabs Tab-separated values",
11222 " tcl TCL list elements",
11223 ".nullvalue STRING Use STRING in place of NULL values",
11224 ".once (-e|-x|FILE) Output for the next SQL command only to FILE",
11225 " If FILE begins with '|' then open as a pipe",
11227 " -e Invoke system text editor",
11228 " -x Open in a spreadsheet",
11229 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
11231 " --append Use appendvfs to append database to the end of FILE",
11232 #ifdef SQLITE_ENABLE_DESERIALIZE
11233 " --deserialize Load into memory useing sqlite3_deserialize()",
11234 " --hexdb Load the output of \"dbtotxt\" as an in-memory database",
11235 " --maxsize N Maximum size for --hexdb or --deserialized database",
11237 " --new Initialize FILE to an empty database",
11238 " --readonly Open FILE readonly",
11239 " --zip FILE is a ZIP archive",
11240 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
11241 " If FILE begins with '|' then open it as a pipe.",
11242 ".parameter CMD ... Manage SQL parameter bindings",
11243 " clear Erase all bindings",
11244 " init Initialize the TEMP table that holds bindings",
11245 " list List the current parameter bindings",
11246 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
11247 " PARAMETER should start with '$', ':', '@', or '?'",
11248 " unset PARAMETER Remove PARAMETER from the binding table",
11249 ".print STRING... Print literal STRING",
11250 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11251 ".progress N Invoke progress handler after every N opcodes",
11252 " --limit N Interrupt after N progress callbacks",
11253 " --once Do no more than one progress interrupt",
11254 " --quiet|-q No output except at interrupts",
11255 " --reset Reset the count for each input and interrupt",
11257 ".prompt MAIN CONTINUE Replace the standard prompts",
11258 ".quit Exit this program",
11259 ".read FILE Read input from FILE",
11260 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
11261 ".save FILE Write in-memory database into FILE",
11262 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
11263 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
11265 " --indent Try to pretty-print the schema",
11266 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
11268 " --init Create a new SELFTEST table",
11269 " -v Verbose output",
11270 ".separator COL ?ROW? Change the column and row separators",
11271 #if defined(SQLITE_ENABLE_SESSION)
11272 ".session ?NAME? CMD ... Create or control sessions",
11274 " attach TABLE Attach TABLE",
11275 " changeset FILE Write a changeset into FILE",
11276 " close Close one session",
11277 " enable ?BOOLEAN? Set or query the enable bit",
11278 " filter GLOB... Reject tables matching GLOBs",
11279 " indirect ?BOOLEAN? Mark or query the indirect status",
11280 " isempty Query whether the session is empty",
11281 " list List currently open session names",
11282 " open DB NAME Open a new session on DB",
11283 " patchset FILE Write a patchset into FILE",
11284 " If ?NAME? is omitted, the first defined session is used.",
11286 ".sha3sum ... Compute a SHA3 hash of database content",
11288 " --schema Also hash the sqlite_master table",
11289 " --sha3-224 Use the sha3-224 algorithm",
11290 " --sha3-256 Use the sha3-256 algorithm. This is the default.",
11291 " --sha3-384 Use the sha3-384 algorithm",
11292 " --sha3-512 Use the sha3-512 algorithm",
11293 " Any other argument is a LIKE pattern for tables to hash",
11294 #ifndef SQLITE_NOHAVE_SYSTEM
11295 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
11297 ".show Show the current values for various settings",
11298 ".stats ?on|off? Show stats or turn stats on or off",
11299 #ifndef SQLITE_NOHAVE_SYSTEM
11300 ".system CMD ARGS... Run CMD ARGS... in a system shell",
11302 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
11303 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
11304 ".timeout MS Try opening locked tables for MS milliseconds",
11305 ".timer on|off Turn SQL timer on or off",
11306 #ifndef SQLITE_OMIT_TRACE
11307 ".trace ?OPTIONS? Output each SQL statement as it is run",
11308 " FILE Send output to FILE",
11309 " stdout Send output to stdout",
11310 " stderr Send output to stderr",
11311 " off Disable tracing",
11312 " --expanded Expand query parameters",
11313 #ifdef SQLITE_ENABLE_NORMALIZE
11314 " --normalized Normal the SQL statements",
11316 " --plain Show SQL as it is input",
11317 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
11318 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
11319 " --row Trace each row (SQLITE_TRACE_ROW)",
11320 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
11321 #endif /* SQLITE_OMIT_TRACE */
11322 ".vfsinfo ?AUX? Information about the top-level VFS",
11323 ".vfslist List all available VFSes",
11324 ".vfsname ?AUX? Print the name of the VFS stack",
11325 ".width NUM1 NUM2 ... Set column widths for \"column\" mode",
11326 " Negative values right-justify",
11330 ** Output help text.
11332 ** zPattern describes the set of commands for which help text is provided.
11333 ** If zPattern is NULL, then show all commands, but only give a one-line
11334 ** description of each.
11336 ** Return the number of matches.
11338 static int showHelp(FILE *out, const char *zPattern){
11344 || zPattern[0]=='0'
11345 || strcmp(zPattern,"-a")==0
11346 || strcmp(zPattern,"-all")==0
11348 /* Show all commands, but only one line per command */
11349 if( zPattern==0 ) zPattern = "";
11350 for(i=0; i<ArraySize(azHelp); i++){
11351 if( azHelp[i][0]=='.' || zPattern[0] ){
11352 utf8_printf(out, "%s\n", azHelp[i]);
11357 /* Look for commands that for which zPattern is an exact prefix */
11358 zPat = sqlite3_mprintf(".%s*", zPattern);
11359 for(i=0; i<ArraySize(azHelp); i++){
11360 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
11361 utf8_printf(out, "%s\n", azHelp[i]);
11366 sqlite3_free(zPat);
11369 /* when zPattern is a prefix of exactly one command, then include the
11370 ** details of that command, which should begin at offset j */
11371 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
11372 utf8_printf(out, "%s\n", azHelp[j]);
11378 /* Look for commands that contain zPattern anywhere. Show the complete
11379 ** text of all commands that match. */
11380 zPat = sqlite3_mprintf("%%%s%%", zPattern);
11381 for(i=0; i<ArraySize(azHelp); i++){
11382 if( azHelp[i][0]=='.' ) j = i;
11383 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
11384 utf8_printf(out, "%s\n", azHelp[j]);
11385 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
11387 utf8_printf(out, "%s\n", azHelp[j]);
11393 sqlite3_free(zPat);
11398 /* Forward reference */
11399 static int process_input(ShellState *p);
11402 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
11403 ** and return a pointer to the buffer. The caller is responsible for freeing
11406 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
11409 ** For convenience, a nul-terminator byte is always appended to the data read
11410 ** from the file before the buffer is returned. This byte is not included in
11411 ** the final value of (*pnByte), if applicable.
11413 ** NULL is returned if any error is encountered. The final value of *pnByte
11414 ** is undefined in this case.
11416 static char *readFile(const char *zName, int *pnByte){
11417 FILE *in = fopen(zName, "rb");
11421 if( in==0 ) return 0;
11422 fseek(in, 0, SEEK_END);
11425 pBuf = sqlite3_malloc64( nIn+1 );
11426 if( pBuf==0 ){ fclose(in); return 0; }
11427 nRead = fread(pBuf, nIn, 1, in);
11430 sqlite3_free(pBuf);
11434 if( pnByte ) *pnByte = nIn;
11438 #if defined(SQLITE_ENABLE_SESSION)
11440 ** Close a single OpenSession object and release all of its associated
11443 static void session_close(OpenSession *pSession){
11445 sqlite3session_delete(pSession->p);
11446 sqlite3_free(pSession->zName);
11447 for(i=0; i<pSession->nFilter; i++){
11448 sqlite3_free(pSession->azFilter[i]);
11450 sqlite3_free(pSession->azFilter);
11451 memset(pSession, 0, sizeof(OpenSession));
11456 ** Close all OpenSession objects and release all associated resources.
11458 #if defined(SQLITE_ENABLE_SESSION)
11459 static void session_close_all(ShellState *p){
11461 for(i=0; i<p->nSession; i++){
11462 session_close(&p->aSession[i]);
11467 # define session_close_all(X)
11471 ** Implementation of the xFilter function for an open session. Omit
11472 ** any tables named by ".session filter" but let all other table through.
11474 #if defined(SQLITE_ENABLE_SESSION)
11475 static int session_filter(void *pCtx, const char *zTab){
11476 OpenSession *pSession = (OpenSession*)pCtx;
11478 for(i=0; i<pSession->nFilter; i++){
11479 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11486 ** Try to deduce the type of file for zName based on its content. Return
11487 ** one of the SHELL_OPEN_* constants.
11489 ** If the file does not exist or is empty but its name looks like a ZIP
11490 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11491 ** Otherwise, assume an ordinary database regardless of the filename if
11492 ** the type cannot be determined from content.
11494 int deduceDatabaseType(const char *zName, int dfltZip){
11495 FILE *f = fopen(zName, "rb");
11497 int rc = SHELL_OPEN_UNSPEC;
11500 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11501 return SHELL_OPEN_ZIPFILE;
11503 return SHELL_OPEN_NORMAL;
11506 n = fread(zBuf, 16, 1, f);
11507 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
11509 return SHELL_OPEN_NORMAL;
11511 fseek(f, -25, SEEK_END);
11512 n = fread(zBuf, 25, 1, f);
11513 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11514 rc = SHELL_OPEN_APPENDVFS;
11516 fseek(f, -22, SEEK_END);
11517 n = fread(zBuf, 22, 1, f);
11518 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11519 && zBuf[3]==0x06 ){
11520 rc = SHELL_OPEN_ZIPFILE;
11521 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11522 rc = SHELL_OPEN_ZIPFILE;
11529 #ifdef SQLITE_ENABLE_DESERIALIZE
11531 ** Reconstruct an in-memory database using the output from the "dbtotxt"
11532 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
11533 ** is 0, then read from standard input.
11535 static unsigned char *readHexDb(ShellState *p, int *pnData){
11536 unsigned char *a = 0;
11544 unsigned char x[16];
11546 if( p->zDbFilename ){
11547 in = fopen(p->zDbFilename, "r");
11549 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
11559 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
11560 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
11561 if( rc!=2 ) goto readHexDb_error;
11562 if( n<=0 ) goto readHexDb_error;
11563 a = sqlite3_malloc( n );
11565 utf8_printf(stderr, "Out of memory!\n");
11566 goto readHexDb_error;
11569 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
11570 utf8_printf(stderr, "invalid pagesize\n");
11571 goto readHexDb_error;
11573 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
11574 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
11579 if( strncmp(zLine, "| end ", 6)==0 ){
11582 rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
11583 " %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
11584 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
11585 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
11589 memcpy(a+k, x, 16);
11605 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
11607 if(strncmp(zLine, "| end ", 6)==0 ) break;
11612 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
11615 #endif /* SQLITE_ENABLE_DESERIALIZE */
11617 /* Flags for open_db().
11619 ** The default behavior of open_db() is to exit(1) if the database fails to
11620 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11621 ** but still returns without calling exit.
11623 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11624 ** ZIP archive if the file does not exist or is empty and its name matches
11625 ** the *.zip pattern.
11627 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11628 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11631 ** Make sure the database is open. If it is not, then open it. If
11632 ** the database fails to open, print an error message and exit.
11634 static void open_db(ShellState *p, int openFlags){
11636 if( p->openMode==SHELL_OPEN_UNSPEC ){
11637 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11638 p->openMode = SHELL_OPEN_NORMAL;
11640 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11641 (openFlags & OPEN_DB_ZIPFILE)!=0);
11644 switch( p->openMode ){
11645 case SHELL_OPEN_APPENDVFS: {
11646 sqlite3_open_v2(p->zDbFilename, &p->db,
11647 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11650 case SHELL_OPEN_HEXDB:
11651 case SHELL_OPEN_DESERIALIZE: {
11652 sqlite3_open(0, &p->db);
11655 case SHELL_OPEN_ZIPFILE: {
11656 sqlite3_open(":memory:", &p->db);
11659 case SHELL_OPEN_READONLY: {
11660 sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11663 case SHELL_OPEN_UNSPEC:
11664 case SHELL_OPEN_NORMAL: {
11665 sqlite3_open(p->zDbFilename, &p->db);
11670 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11671 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11672 p->zDbFilename, sqlite3_errmsg(p->db));
11673 if( openFlags & OPEN_DB_KEEPALIVE ){
11674 sqlite3_open(":memory:", &p->db);
11679 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11680 sqlite3_enable_load_extension(p->db, 1);
11682 sqlite3_fileio_init(p->db, 0, 0);
11683 sqlite3_shathree_init(p->db, 0, 0);
11684 sqlite3_completion_init(p->db, 0, 0);
11685 #ifdef SQLITE_HAVE_ZLIB
11686 sqlite3_zipfile_init(p->db, 0, 0);
11687 sqlite3_sqlar_init(p->db, 0, 0);
11689 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11690 shellAddSchemaName, 0, 0);
11691 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11692 shellModuleSchema, 0, 0);
11693 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11694 shellPutsFunc, 0, 0);
11695 #ifndef SQLITE_NOHAVE_SYSTEM
11696 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11698 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11701 if( p->openMode==SHELL_OPEN_ZIPFILE ){
11702 char *zSql = sqlite3_mprintf(
11703 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11704 sqlite3_exec(p->db, zSql, 0, 0, 0);
11705 sqlite3_free(zSql);
11707 #ifdef SQLITE_ENABLE_DESERIALIZE
11709 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
11712 unsigned char *aData;
11713 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
11714 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
11716 aData = readHexDb(p, &nData);
11718 utf8_printf(stderr, "Error in hexdb input\n");
11722 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
11723 SQLITE_DESERIALIZE_RESIZEABLE |
11724 SQLITE_DESERIALIZE_FREEONCLOSE);
11726 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
11729 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
11737 ** Attempt to close the databaes connection. Report errors.
11739 void close_db(sqlite3 *db){
11740 int rc = sqlite3_close(db);
11742 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11743 rc, sqlite3_errmsg(db));
11747 #if HAVE_READLINE || HAVE_EDITLINE
11749 ** Readline completion callbacks
11751 static char *readline_completion_generator(const char *text, int state){
11752 static sqlite3_stmt *pStmt = 0;
11756 sqlite3_finalize(pStmt);
11757 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11758 " FROM completion(%Q) ORDER BY 1", text);
11759 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11760 sqlite3_free(zSql);
11762 if( sqlite3_step(pStmt)==SQLITE_ROW ){
11763 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11765 sqlite3_finalize(pStmt);
11771 static char **readline_completion(const char *zText, int iStart, int iEnd){
11772 rl_attempted_completion_over = 1;
11773 return rl_completion_matches(zText, readline_completion_generator);
11776 #elif HAVE_LINENOISE
11778 ** Linenoise completion callback
11780 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11781 int nLine = strlen30(zLine);
11783 sqlite3_stmt *pStmt = 0;
11787 if( nLine>sizeof(zBuf)-30 ) return;
11788 if( zLine[0]=='.' || zLine[0]=='#') return;
11789 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11790 if( i==nLine-1 ) return;
11792 memcpy(zBuf, zLine, iStart);
11793 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11794 " FROM completion(%Q,%Q) ORDER BY 1",
11795 &zLine[iStart], zLine);
11796 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11797 sqlite3_free(zSql);
11798 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11799 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11800 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11801 int nCompletion = sqlite3_column_bytes(pStmt, 0);
11802 if( iStart+nCompletion < sizeof(zBuf)-1 ){
11803 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11804 linenoiseAddCompletion(lc, zBuf);
11807 sqlite3_finalize(pStmt);
11812 ** Do C-language style dequoting.
11818 ** \v -> vertical tab
11820 ** \r -> carriage return
11825 ** \NNN -> ascii character NNN in octal
11827 static void resolve_backslashes(char *z){
11830 while( *z && *z!='\\' ) z++;
11831 for(i=j=0; (c = z[i])!=0; i++, j++){
11832 if( c=='\\' && z[i+1]!=0 ){
11836 }else if( c=='b' ){
11838 }else if( c=='t' ){
11840 }else if( c=='n' ){
11842 }else if( c=='v' ){
11844 }else if( c=='f' ){
11846 }else if( c=='r' ){
11848 }else if( c=='"' ){
11850 }else if( c=='\'' ){
11852 }else if( c=='\\' ){
11854 }else if( c>='0' && c<='7' ){
11856 if( z[i+1]>='0' && z[i+1]<='7' ){
11858 c = (c<<3) + z[i] - '0';
11859 if( z[i+1]>='0' && z[i+1]<='7' ){
11861 c = (c<<3) + z[i] - '0';
11868 if( j<i ) z[j] = 0;
11872 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
11873 ** for TRUE and FALSE. Return the integer value if appropriate.
11875 static int booleanValue(const char *zArg){
11877 if( zArg[0]=='0' && zArg[1]=='x' ){
11878 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11880 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11882 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11883 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11886 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11889 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11895 ** Set or clear a shell flag according to a boolean value.
11897 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11898 if( booleanValue(zArg) ){
11899 ShellSetFlag(p, mFlag);
11901 ShellClearFlag(p, mFlag);
11906 ** Close an output file, assuming it is not stderr or stdout
11908 static void output_file_close(FILE *f){
11909 if( f && f!=stdout && f!=stderr ) fclose(f);
11913 ** Try to open an output file. The names "stdout" and "stderr" are
11914 ** recognized and do the right thing. NULL is returned if the output
11915 ** filename is "off".
11917 static FILE *output_file_open(const char *zFile, int bTextMode){
11919 if( strcmp(zFile,"stdout")==0 ){
11921 }else if( strcmp(zFile, "stderr")==0 ){
11923 }else if( strcmp(zFile, "off")==0 ){
11926 f = fopen(zFile, bTextMode ? "w" : "wb");
11928 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11934 #ifndef SQLITE_OMIT_TRACE
11936 ** A routine for handling output from sqlite3_trace().
11938 static int sql_trace_callback(
11939 unsigned mType, /* The trace type */
11940 void *pArg, /* The ShellState pointer */
11941 void *pP, /* Usually a pointer to sqlite_stmt */
11942 void *pX /* Auxiliary output */
11944 ShellState *p = (ShellState*)pArg;
11945 sqlite3_stmt *pStmt;
11948 if( p->traceOut==0 ) return 0;
11949 if( mType==SQLITE_TRACE_CLOSE ){
11950 utf8_printf(p->traceOut, "-- closing database connection\n");
11953 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
11954 zSql = (const char*)pX;
11956 pStmt = (sqlite3_stmt*)pP;
11957 switch( p->eTraceType ){
11958 case SHELL_TRACE_EXPANDED: {
11959 zSql = sqlite3_expanded_sql(pStmt);
11962 #ifdef SQLITE_ENABLE_NORMALIZE
11963 case SHELL_TRACE_NORMALIZED: {
11964 zSql = sqlite3_normalized_sql(pStmt);
11969 zSql = sqlite3_sql(pStmt);
11974 if( zSql==0 ) return 0;
11975 nSql = strlen30(zSql);
11976 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
11978 case SQLITE_TRACE_ROW:
11979 case SQLITE_TRACE_STMT: {
11980 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
11983 case SQLITE_TRACE_PROFILE: {
11984 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
11985 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
11994 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
11995 ** a useful spot to set a debugger breakpoint.
11997 static void test_breakpoint(void){
11998 static int nCall = 0;
12003 ** An object used to read a CSV and other files for import.
12005 typedef struct ImportCtx ImportCtx;
12007 const char *zFile; /* Name of the input file */
12008 FILE *in; /* Read the CSV text from this input stream */
12009 char *z; /* Accumulated text for a field */
12010 int n; /* Number of bytes in z */
12011 int nAlloc; /* Space allocated for z[] */
12012 int nLine; /* Current line number */
12013 int bNotFirst; /* True if one or more bytes already read */
12014 int cTerm; /* Character that terminated the most recent field */
12015 int cColSep; /* The column separator character. (Usually ",") */
12016 int cRowSep; /* The row separator character. (Usually "\n") */
12019 /* Append a single byte to z[] */
12020 static void import_append_char(ImportCtx *p, int c){
12021 if( p->n+1>=p->nAlloc ){
12022 p->nAlloc += p->nAlloc + 100;
12023 p->z = sqlite3_realloc64(p->z, p->nAlloc);
12024 if( p->z==0 ) shell_out_of_memory();
12026 p->z[p->n++] = (char)c;
12029 /* Read a single field of CSV text. Compatible with rfc4180 and extended
12030 ** with the option of having a separator other than ",".
12032 ** + Input comes from p->in.
12033 ** + Store results in p->z of length p->n. Space to hold p->z comes
12034 ** from sqlite3_malloc64().
12035 ** + Use p->cSep as the column separator. The default is ",".
12036 ** + Use p->rSep as the row separator. The default is "\n".
12037 ** + Keep track of the line number in p->nLine.
12038 ** + Store the character that terminates the field in p->cTerm. Store
12039 ** EOF on end-of-file.
12040 ** + Report syntax errors on stderr
12042 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
12044 int cSep = p->cColSep;
12045 int rSep = p->cRowSep;
12048 if( c==EOF || seenInterrupt ){
12054 int startLine = p->nLine;
12059 if( c==rSep ) p->nLine++;
12066 if( (c==cSep && pc==cQuote)
12067 || (c==rSep && pc==cQuote)
12068 || (c==rSep && pc=='\r' && ppc==cQuote)
12069 || (c==EOF && pc==cQuote)
12071 do{ p->n--; }while( p->z[p->n]!=cQuote );
12075 if( pc==cQuote && c!='\r' ){
12076 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
12077 p->zFile, p->nLine, cQuote);
12080 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
12081 p->zFile, startLine, cQuote);
12085 import_append_char(p, c);
12090 /* If this is the first field being parsed and it begins with the
12091 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
12092 if( (c&0xff)==0xef && p->bNotFirst==0 ){
12093 import_append_char(p, c);
12095 if( (c&0xff)==0xbb ){
12096 import_append_char(p, c);
12098 if( (c&0xff)==0xbf ){
12101 return csv_read_one_field(p);
12105 while( c!=EOF && c!=cSep && c!=rSep ){
12106 import_append_char(p, c);
12111 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
12115 if( p->z ) p->z[p->n] = 0;
12120 /* Read a single field of ASCII delimited text.
12122 ** + Input comes from p->in.
12123 ** + Store results in p->z of length p->n. Space to hold p->z comes
12124 ** from sqlite3_malloc64().
12125 ** + Use p->cSep as the column separator. The default is "\x1F".
12126 ** + Use p->rSep as the row separator. The default is "\x1E".
12127 ** + Keep track of the row number in p->nLine.
12128 ** + Store the character that terminates the field in p->cTerm. Store
12129 ** EOF on end-of-file.
12130 ** + Report syntax errors on stderr
12132 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
12134 int cSep = p->cColSep;
12135 int rSep = p->cRowSep;
12138 if( c==EOF || seenInterrupt ){
12142 while( c!=EOF && c!=cSep && c!=rSep ){
12143 import_append_char(p, c);
12150 if( p->z ) p->z[p->n] = 0;
12155 ** Try to transfer data for table zTable. If an error is seen while
12156 ** moving forward, try to go backwards. The backwards movement won't
12157 ** work for WITHOUT ROWID tables.
12159 static void tryToCloneData(
12164 sqlite3_stmt *pQuery = 0;
12165 sqlite3_stmt *pInsert = 0;
12170 int nTable = strlen30(zTable);
12173 const int spinRate = 10000;
12175 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
12176 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12178 utf8_printf(stderr, "Error %d: %s on [%s]\n",
12179 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12181 goto end_data_xfer;
12183 n = sqlite3_column_count(pQuery);
12184 zInsert = sqlite3_malloc64(200 + nTable + n*3);
12185 if( zInsert==0 ) shell_out_of_memory();
12186 sqlite3_snprintf(200+nTable,zInsert,
12187 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
12188 i = strlen30(zInsert);
12189 for(j=1; j<n; j++){
12190 memcpy(zInsert+i, ",?", 2);
12193 memcpy(zInsert+i, ");", 3);
12194 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
12196 utf8_printf(stderr, "Error %d: %s on [%s]\n",
12197 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
12199 goto end_data_xfer;
12201 for(k=0; k<2; k++){
12202 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12203 for(i=0; i<n; i++){
12204 switch( sqlite3_column_type(pQuery, i) ){
12205 case SQLITE_NULL: {
12206 sqlite3_bind_null(pInsert, i+1);
12209 case SQLITE_INTEGER: {
12210 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
12213 case SQLITE_FLOAT: {
12214 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
12217 case SQLITE_TEXT: {
12218 sqlite3_bind_text(pInsert, i+1,
12219 (const char*)sqlite3_column_text(pQuery,i),
12220 -1, SQLITE_STATIC);
12223 case SQLITE_BLOB: {
12224 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
12225 sqlite3_column_bytes(pQuery,i),
12231 rc = sqlite3_step(pInsert);
12232 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
12233 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
12234 sqlite3_errmsg(newDb));
12236 sqlite3_reset(pInsert);
12238 if( (cnt%spinRate)==0 ){
12239 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
12243 if( rc==SQLITE_DONE ) break;
12244 sqlite3_finalize(pQuery);
12245 sqlite3_free(zQuery);
12246 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
12248 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12250 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
12253 } /* End for(k=0...) */
12256 sqlite3_finalize(pQuery);
12257 sqlite3_finalize(pInsert);
12258 sqlite3_free(zQuery);
12259 sqlite3_free(zInsert);
12264 ** Try to transfer all rows of the schema that match zWhere. For
12265 ** each row, invoke xForEach() on the object defined by that row.
12266 ** If an error is encountered while moving forward through the
12267 ** sqlite_master table, try again moving backwards.
12269 static void tryToCloneSchema(
12272 const char *zWhere,
12273 void (*xForEach)(ShellState*,sqlite3*,const char*)
12275 sqlite3_stmt *pQuery = 0;
12278 const unsigned char *zName;
12279 const unsigned char *zSql;
12282 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12283 " WHERE %s", zWhere);
12284 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12286 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12287 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12289 goto end_schema_xfer;
12291 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12292 zName = sqlite3_column_text(pQuery, 0);
12293 zSql = sqlite3_column_text(pQuery, 1);
12294 printf("%s... ", zName); fflush(stdout);
12295 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12297 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12298 sqlite3_free(zErrMsg);
12302 xForEach(p, newDb, (const char*)zName);
12306 if( rc!=SQLITE_DONE ){
12307 sqlite3_finalize(pQuery);
12308 sqlite3_free(zQuery);
12309 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12310 " WHERE %s ORDER BY rowid DESC", zWhere);
12311 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12313 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12314 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12316 goto end_schema_xfer;
12318 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12319 zName = sqlite3_column_text(pQuery, 0);
12320 zSql = sqlite3_column_text(pQuery, 1);
12321 printf("%s... ", zName); fflush(stdout);
12322 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12324 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12325 sqlite3_free(zErrMsg);
12329 xForEach(p, newDb, (const char*)zName);
12335 sqlite3_finalize(pQuery);
12336 sqlite3_free(zQuery);
12340 ** Open a new database file named "zNewDb". Try to recover as much information
12341 ** as possible out of the main database (which might be corrupt) and write it
12344 static void tryToClone(ShellState *p, const char *zNewDb){
12346 sqlite3 *newDb = 0;
12347 if( access(zNewDb,0)==0 ){
12348 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
12351 rc = sqlite3_open(zNewDb, &newDb);
12353 utf8_printf(stderr, "Cannot create output database: %s\n",
12354 sqlite3_errmsg(newDb));
12356 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
12357 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
12358 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
12359 tryToCloneSchema(p, newDb, "type!='table'", 0);
12360 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
12361 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12367 ** Change the output file back to stdout.
12369 ** If the p->doXdgOpen flag is set, that means the output was being
12370 ** redirected to a temporary file named by p->zTempFile. In that case,
12371 ** launch start/open/xdg-open on that temporary file.
12373 static void output_reset(ShellState *p){
12374 if( p->outfile[0]=='|' ){
12375 #ifndef SQLITE_OMIT_POPEN
12379 output_file_close(p->out);
12380 #ifndef SQLITE_NOHAVE_SYSTEM
12381 if( p->doXdgOpen ){
12382 const char *zXdgOpenCmd =
12383 #if defined(_WIN32)
12385 #elif defined(__APPLE__)
12391 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
12392 if( system(zCmd) ){
12393 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
12395 sqlite3_free(zCmd);
12399 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
12406 ** Run an SQL command and return the single integer result.
12408 static int db_int(ShellState *p, const char *zSql){
12409 sqlite3_stmt *pStmt;
12411 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12412 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
12413 res = sqlite3_column_int(pStmt,0);
12415 sqlite3_finalize(pStmt);
12420 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
12422 static unsigned int get2byteInt(unsigned char *a){
12423 return (a[0]<<8) + a[1];
12425 static unsigned int get4byteInt(unsigned char *a){
12426 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
12430 ** Implementation of the ".info" command.
12432 ** Return 1 on error, 2 to exit, and 0 otherwise.
12434 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
12435 static const struct { const char *zName; int ofst; } aField[] = {
12436 { "file change counter:", 24 },
12437 { "database page count:", 28 },
12438 { "freelist page count:", 36 },
12439 { "schema cookie:", 40 },
12440 { "schema format:", 44 },
12441 { "default cache size:", 48 },
12442 { "autovacuum top root:", 52 },
12443 { "incremental vacuum:", 64 },
12444 { "text encoding:", 56 },
12445 { "user version:", 60 },
12446 { "application id:", 68 },
12447 { "software version:", 96 },
12449 static const struct { const char *zName; const char *zSql; } aQuery[] = {
12450 { "number of tables:",
12451 "SELECT count(*) FROM %s WHERE type='table'" },
12452 { "number of indexes:",
12453 "SELECT count(*) FROM %s WHERE type='index'" },
12454 { "number of triggers:",
12455 "SELECT count(*) FROM %s WHERE type='trigger'" },
12456 { "number of views:",
12457 "SELECT count(*) FROM %s WHERE type='view'" },
12459 "SELECT total(length(sql)) FROM %s" },
12462 unsigned iDataVersion;
12464 char *zDb = nArg>=2 ? azArg[1] : "main";
12465 sqlite3_stmt *pStmt = 0;
12466 unsigned char aHdr[100];
12468 if( p->db==0 ) return 1;
12469 rc = sqlite3_prepare_v2(p->db,
12470 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12473 if( !sqlite3_compileoption_used("ENABLE_DBPAGE_VTAB") ){
12474 utf8_printf(stderr, "the \".dbinfo\" command requires the "
12475 "-DSQLITE_ENABLE_DBPAGE_VTAB compile-time options\n");
12477 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
12479 sqlite3_finalize(pStmt);
12482 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12483 if( sqlite3_step(pStmt)==SQLITE_ROW
12484 && sqlite3_column_bytes(pStmt,0)>100
12486 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
12487 sqlite3_finalize(pStmt);
12489 raw_printf(stderr, "unable to read database header\n");
12490 sqlite3_finalize(pStmt);
12493 i = get2byteInt(aHdr+16);
12494 if( i==1 ) i = 65536;
12495 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
12496 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
12497 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
12498 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
12499 for(i=0; i<ArraySize(aField); i++){
12500 int ofst = aField[i].ofst;
12501 unsigned int val = get4byteInt(aHdr + ofst);
12502 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
12505 if( val==1 ) raw_printf(p->out, " (utf8)");
12506 if( val==2 ) raw_printf(p->out, " (utf16le)");
12507 if( val==3 ) raw_printf(p->out, " (utf16be)");
12510 raw_printf(p->out, "\n");
12513 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
12514 }else if( strcmp(zDb,"temp")==0 ){
12515 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
12517 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
12519 for(i=0; i<ArraySize(aQuery); i++){
12520 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
12521 int val = db_int(p, zSql);
12522 sqlite3_free(zSql);
12523 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
12525 sqlite3_free(zSchemaTab);
12526 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
12527 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
12532 ** Print the current sqlite3_errmsg() value to stderr and return 1.
12534 static int shellDatabaseError(sqlite3 *db){
12535 const char *zErr = sqlite3_errmsg(db);
12536 utf8_printf(stderr, "Error: %s\n", zErr);
12541 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
12542 ** if they match and FALSE (0) if they do not match.
12546 ** '*' Matches any sequence of zero or more characters.
12548 ** '?' Matches exactly one character.
12550 ** [...] Matches one character from the enclosed list of
12553 ** [^...] Matches one character not in the enclosed list.
12555 ** '#' Matches any sequence of one or more digits with an
12556 ** optional + or - sign in front
12558 ** ' ' Any span of whitespace matches any other span of
12561 ** Extra whitespace at the end of z[] is ignored.
12563 static int testcase_glob(const char *zGlob, const char *z){
12568 while( (c = (*(zGlob++)))!=0 ){
12570 if( !IsSpace(*z) ) return 0;
12571 while( IsSpace(*zGlob) ) zGlob++;
12572 while( IsSpace(*z) ) z++;
12573 }else if( c=='*' ){
12574 while( (c=(*(zGlob++))) == '*' || c=='?' ){
12575 if( c=='?' && (*(z++))==0 ) return 0;
12579 }else if( c=='[' ){
12580 while( *z && testcase_glob(zGlob-1,z)==0 ){
12585 while( (c2 = (*(z++)))!=0 ){
12588 if( c2==0 ) return 0;
12590 if( testcase_glob(zGlob,z) ) return 1;
12593 }else if( c=='?' ){
12594 if( (*(z++))==0 ) return 0;
12595 }else if( c=='[' ){
12600 if( c==0 ) return 0;
12607 if( c==']' ) seen = 1;
12610 while( c2 && c2!=']' ){
12611 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
12613 if( c>=prior_c && c<=c2 ) seen = 1;
12623 if( c2==0 || (seen ^ invert)==0 ) return 0;
12624 }else if( c=='#' ){
12625 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12626 if( !IsDigit(z[0]) ) return 0;
12628 while( IsDigit(z[0]) ){ z++; }
12630 if( c!=(*(z++)) ) return 0;
12633 while( IsSpace(*z) ){ z++; }
12639 ** Compare the string as a command-line option with either one or two
12640 ** initial "-" characters.
12642 static int optionMatch(const char *zStr, const char *zOpt){
12643 if( zStr[0]!='-' ) return 0;
12645 if( zStr[0]=='-' ) zStr++;
12646 return strcmp(zStr, zOpt)==0;
12652 int shellDeleteFile(const char *zFilename){
12655 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12659 rc = unlink(zFilename);
12665 ** Try to delete the temporary file (if there is one) and free the
12666 ** memory used to hold the name of the temp file.
12668 static void clearTempFile(ShellState *p){
12669 if( p->zTempFile==0 ) return;
12670 if( p->doXdgOpen ) return;
12671 if( shellDeleteFile(p->zTempFile) ) return;
12672 sqlite3_free(p->zTempFile);
12677 ** Create a new temp file name with the given suffix.
12679 static void newTempFile(ShellState *p, const char *zSuffix){
12681 sqlite3_free(p->zTempFile);
12684 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12686 if( p->zTempFile==0 ){
12688 sqlite3_randomness(sizeof(r), &r);
12689 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12691 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12693 if( p->zTempFile==0 ){
12694 raw_printf(stderr, "out of memory\n");
12701 ** The implementation of SQL scalar function fkey_collate_clause(), used
12702 ** by the ".lint fkey-indexes" command. This scalar function is always
12703 ** called with four arguments - the parent table name, the parent column name,
12704 ** the child table name and the child column name.
12706 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12708 ** If either of the named tables or columns do not exist, this function
12709 ** returns an empty string. An empty string is also returned if both tables
12710 ** and columns exist but have the same default collation sequence. Or,
12711 ** if both exist but the default collation sequences are different, this
12712 ** function returns the string " COLLATE <parent-collation>", where
12713 ** <parent-collation> is the default collation sequence of the parent column.
12715 static void shellFkeyCollateClause(
12716 sqlite3_context *pCtx,
12718 sqlite3_value **apVal
12720 sqlite3 *db = sqlite3_context_db_handle(pCtx);
12721 const char *zParent;
12722 const char *zParentCol;
12723 const char *zParentSeq;
12724 const char *zChild;
12725 const char *zChildCol;
12726 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
12730 zParent = (const char*)sqlite3_value_text(apVal[0]);
12731 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12732 zChild = (const char*)sqlite3_value_text(apVal[2]);
12733 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12735 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12736 rc = sqlite3_table_column_metadata(
12737 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12739 if( rc==SQLITE_OK ){
12740 rc = sqlite3_table_column_metadata(
12741 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12745 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12746 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12747 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12754 ** The implementation of dot-command ".lint fkey-indexes".
12756 static int lintFkeyIndexes(
12757 ShellState *pState, /* Current shell tool state */
12758 char **azArg, /* Array of arguments passed to dot command */
12759 int nArg /* Number of entries in azArg[] */
12761 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
12762 FILE *out = pState->out; /* Stream to write non-error output to */
12763 int bVerbose = 0; /* If -verbose is present */
12764 int bGroupByParent = 0; /* If -groupbyparent is present */
12765 int i; /* To iterate through azArg[] */
12766 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
12767 int rc; /* Return code */
12768 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
12771 ** This SELECT statement returns one row for each foreign key constraint
12772 ** in the schema of the main database. The column values are:
12774 ** 0. The text of an SQL statement similar to:
12776 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12778 ** This SELECT is similar to the one that the foreign keys implementation
12779 ** needs to run internally on child tables. If there is an index that can
12780 ** be used to optimize this query, then it can also be used by the FK
12781 ** implementation to optimize DELETE or UPDATE statements on the parent
12784 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12785 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12786 ** contains an index that can be used to optimize the query.
12788 ** 2. Human readable text that describes the child table and columns. e.g.
12790 ** "child_table(child_key1, child_key2)"
12792 ** 3. Human readable text that describes the parent table and columns. e.g.
12794 ** "parent_table(parent_key1, parent_key2)"
12796 ** 4. A full CREATE INDEX statement for an index that could be used to
12797 ** optimize DELETE or UPDATE statements on the parent table. e.g.
12799 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
12801 ** 5. The name of the parent table.
12803 ** These six values are used by the C logic below to generate the report.
12807 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12808 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12809 " || fkey_collate_clause("
12810 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12812 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12813 " || group_concat('*=?', ' AND ') || ')'"
12815 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
12817 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12819 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12820 " || ' ON ' || quote(s.name) || '('"
12821 " || group_concat(quote(f.[from]) ||"
12822 " fkey_collate_clause("
12823 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12827 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12828 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12829 "GROUP BY s.name, f.id "
12830 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12832 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12834 for(i=2; i<nArg; i++){
12835 int n = strlen30(azArg[i]);
12836 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12839 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12840 bGroupByParent = 1;
12844 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12847 return SQLITE_ERROR;
12851 /* Register the fkey_collate_clause() SQL function */
12852 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12853 0, shellFkeyCollateClause, 0, 0
12857 if( rc==SQLITE_OK ){
12858 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12860 if( rc==SQLITE_OK ){
12861 sqlite3_bind_int(pSql, 1, bGroupByParent);
12864 if( rc==SQLITE_OK ){
12867 while( SQLITE_ROW==sqlite3_step(pSql) ){
12869 sqlite3_stmt *pExplain = 0;
12870 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12871 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12872 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12873 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12874 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12875 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12877 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12878 if( rc!=SQLITE_OK ) break;
12879 if( SQLITE_ROW==sqlite3_step(pExplain) ){
12880 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12882 0==sqlite3_strglob(zGlob, zPlan)
12883 || 0==sqlite3_strglob(zGlobIPK, zPlan)
12886 rc = sqlite3_finalize(pExplain);
12887 if( rc!=SQLITE_OK ) break;
12890 raw_printf(stderr, "Error: internal error");
12894 && (bVerbose || res==0)
12895 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12897 raw_printf(out, "-- Parent table %s\n", zParent);
12898 sqlite3_free(zPrev);
12899 zPrev = sqlite3_mprintf("%s", zParent);
12903 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12904 }else if( bVerbose ){
12905 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12906 zIndent, zFrom, zTarget
12911 sqlite3_free(zPrev);
12913 if( rc!=SQLITE_OK ){
12914 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12917 rc2 = sqlite3_finalize(pSql);
12918 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12920 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12923 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12930 ** Implementation of ".lint" dot command.
12932 static int lintDotCommand(
12933 ShellState *pState, /* Current shell tool state */
12934 char **azArg, /* Array of arguments passed to dot command */
12935 int nArg /* Number of entries in azArg[] */
12938 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12939 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12940 return lintFkeyIndexes(pState, azArg, nArg);
12943 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12944 raw_printf(stderr, "Where sub-commands are:\n");
12945 raw_printf(stderr, " fkey-indexes\n");
12946 return SQLITE_ERROR;
12949 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12950 /*********************************************************************************
12951 ** The ".archive" or ".ar" command.
12953 static void shellPrepare(
12957 sqlite3_stmt **ppStmt
12960 if( *pRc==SQLITE_OK ){
12961 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12962 if( rc!=SQLITE_OK ){
12963 raw_printf(stderr, "sql error: %s (%d)\n",
12964 sqlite3_errmsg(db), sqlite3_errcode(db)
12971 static void shellPreparePrintf(
12974 sqlite3_stmt **ppStmt,
12979 if( *pRc==SQLITE_OK ){
12982 va_start(ap, zFmt);
12983 z = sqlite3_vmprintf(zFmt, ap);
12986 *pRc = SQLITE_NOMEM;
12988 shellPrepare(db, pRc, z, ppStmt);
12994 static void shellFinalize(
12996 sqlite3_stmt *pStmt
12999 sqlite3 *db = sqlite3_db_handle(pStmt);
13000 int rc = sqlite3_finalize(pStmt);
13001 if( *pRc==SQLITE_OK ){
13002 if( rc!=SQLITE_OK ){
13003 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
13010 static void shellReset(
13012 sqlite3_stmt *pStmt
13014 int rc = sqlite3_reset(pStmt);
13015 if( *pRc==SQLITE_OK ){
13016 if( rc!=SQLITE_OK ){
13017 sqlite3 *db = sqlite3_db_handle(pStmt);
13018 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
13024 ** Structure representing a single ".ar" command.
13026 typedef struct ArCommand ArCommand;
13028 u8 eCmd; /* An AR_CMD_* value */
13029 u8 bVerbose; /* True if --verbose */
13030 u8 bZip; /* True if the archive is a ZIP */
13031 u8 bDryRun; /* True if --dry-run */
13032 u8 bAppend; /* True if --append */
13033 u8 fromCmdLine; /* Run from -A instead of .archive */
13034 int nArg; /* Number of command arguments */
13035 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
13036 const char *zFile; /* --file argument, or NULL */
13037 const char *zDir; /* --directory argument, or NULL */
13038 char **azArg; /* Array of command arguments */
13039 ShellState *p; /* Shell state */
13040 sqlite3 *db; /* Database containing the archive */
13044 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
13046 static int arUsage(FILE *f){
13047 showHelp(f,"archive");
13048 return SQLITE_ERROR;
13052 ** Print an error message for the .ar command to stderr and return
13055 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
13058 va_start(ap, zFmt);
13059 z = sqlite3_vmprintf(zFmt, ap);
13061 utf8_printf(stderr, "Error: %s\n", z);
13062 if( pAr->fromCmdLine ){
13063 utf8_printf(stderr, "Use \"-A\" for more help\n");
13065 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
13068 return SQLITE_ERROR;
13072 ** Values for ArCommand.eCmd.
13074 #define AR_CMD_CREATE 1
13075 #define AR_CMD_UPDATE 2
13076 #define AR_CMD_INSERT 3
13077 #define AR_CMD_EXTRACT 4
13078 #define AR_CMD_LIST 5
13079 #define AR_CMD_HELP 6
13082 ** Other (non-command) switches.
13084 #define AR_SWITCH_VERBOSE 7
13085 #define AR_SWITCH_FILE 8
13086 #define AR_SWITCH_DIRECTORY 9
13087 #define AR_SWITCH_APPEND 10
13088 #define AR_SWITCH_DRYRUN 11
13090 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13092 case AR_CMD_CREATE:
13093 case AR_CMD_EXTRACT:
13095 case AR_CMD_UPDATE:
13096 case AR_CMD_INSERT:
13099 return arErrorMsg(pAr, "multiple command options");
13101 pAr->eCmd = eSwitch;
13104 case AR_SWITCH_DRYRUN:
13107 case AR_SWITCH_VERBOSE:
13110 case AR_SWITCH_APPEND:
13112 /* Fall thru into --file */
13113 case AR_SWITCH_FILE:
13116 case AR_SWITCH_DIRECTORY:
13125 ** Parse the command line for an ".ar" command. The results are written into
13126 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
13127 ** successfully, otherwise an error message is written to stderr and
13128 ** SQLITE_ERROR returned.
13130 static int arParseCommand(
13131 char **azArg, /* Array of arguments passed to dot command */
13132 int nArg, /* Number of entries in azArg[] */
13133 ArCommand *pAr /* Populate this object */
13141 { "create", 'c', AR_CMD_CREATE, 0 },
13142 { "extract", 'x', AR_CMD_EXTRACT, 0 },
13143 { "insert", 'i', AR_CMD_INSERT, 0 },
13144 { "list", 't', AR_CMD_LIST, 0 },
13145 { "update", 'u', AR_CMD_UPDATE, 0 },
13146 { "help", 'h', AR_CMD_HELP, 0 },
13147 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
13148 { "file", 'f', AR_SWITCH_FILE, 1 },
13149 { "append", 'a', AR_SWITCH_APPEND, 1 },
13150 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
13151 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
13153 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
13154 struct ArSwitch *pEnd = &aSwitch[nSwitch];
13157 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
13158 return arUsage(stderr);
13160 char *z = azArg[1];
13162 /* Traditional style [tar] invocation */
13165 for(i=0; z[i]; i++){
13166 const char *zArg = 0;
13167 struct ArSwitch *pOpt;
13168 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13169 if( z[i]==pOpt->cShort ) break;
13172 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13176 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13178 zArg = azArg[iArg++];
13180 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13182 pAr->nArg = nArg-iArg;
13184 pAr->azArg = &azArg[iArg];
13187 /* Non-traditional invocation */
13189 for(iArg=1; iArg<nArg; iArg++){
13193 /* All remaining command line words are command arguments. */
13194 pAr->azArg = &azArg[iArg];
13195 pAr->nArg = nArg-iArg;
13202 /* One or more short options */
13203 for(i=1; i<n; i++){
13204 const char *zArg = 0;
13205 struct ArSwitch *pOpt;
13206 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13207 if( z[i]==pOpt->cShort ) break;
13210 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13217 if( iArg>=(nArg-1) ){
13218 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13220 zArg = azArg[++iArg];
13223 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13225 }else if( z[2]=='\0' ){
13226 /* A -- option, indicating that all remaining command line words
13227 ** are command arguments. */
13228 pAr->azArg = &azArg[iArg+1];
13229 pAr->nArg = nArg-iArg-1;
13232 /* A long option */
13233 const char *zArg = 0; /* Argument for option, if any */
13234 struct ArSwitch *pMatch = 0; /* Matching option */
13235 struct ArSwitch *pOpt; /* Iterator */
13236 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13237 const char *zLong = pOpt->zLong;
13238 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
13240 return arErrorMsg(pAr, "ambiguous option: %s",z);
13248 return arErrorMsg(pAr, "unrecognized option: %s", z);
13250 if( pMatch->bArg ){
13251 if( iArg>=(nArg-1) ){
13252 return arErrorMsg(pAr, "option requires an argument: %s", z);
13254 zArg = azArg[++iArg];
13256 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
13266 ** This function assumes that all arguments within the ArCommand.azArg[]
13267 ** array refer to archive members, as for the --extract or --list commands.
13268 ** It checks that each of them are present. If any specified file is not
13269 ** present in the archive, an error is printed to stderr and an error
13270 ** code returned. Otherwise, if all specified arguments are present in
13271 ** the archive, SQLITE_OK is returned.
13273 ** This function strips any trailing '/' characters from each argument.
13274 ** This is consistent with the way the [tar] command seems to work on
13277 static int arCheckEntries(ArCommand *pAr){
13278 int rc = SQLITE_OK;
13281 sqlite3_stmt *pTest = 0;
13283 shellPreparePrintf(pAr->db, &rc, &pTest,
13284 "SELECT name FROM %s WHERE name=$name",
13287 j = sqlite3_bind_parameter_index(pTest, "$name");
13288 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13289 char *z = pAr->azArg[i];
13290 int n = strlen30(z);
13292 while( n>0 && z[n-1]=='/' ) n--;
13294 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
13295 if( SQLITE_ROW==sqlite3_step(pTest) ){
13298 shellReset(&rc, pTest);
13299 if( rc==SQLITE_OK && bOk==0 ){
13300 utf8_printf(stderr, "not found in archive: %s\n", z);
13304 shellFinalize(&rc, pTest);
13310 ** Format a WHERE clause that can be used against the "sqlar" table to
13311 ** identify all archive members that match the command arguments held
13312 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
13313 ** The caller is responsible for eventually calling sqlite3_free() on
13314 ** any non-NULL (*pzWhere) value.
13316 static void arWhereClause(
13319 char **pzWhere /* OUT: New WHERE clause */
13322 if( *pRc==SQLITE_OK ){
13323 if( pAr->nArg==0 ){
13324 zWhere = sqlite3_mprintf("1");
13327 const char *zSep = "";
13328 for(i=0; i<pAr->nArg; i++){
13329 const char *z = pAr->azArg[i];
13330 zWhere = sqlite3_mprintf(
13331 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
13332 zWhere, zSep, z, strlen30(z)+1, z
13335 *pRc = SQLITE_NOMEM;
13346 ** Implementation of .ar "lisT" command.
13348 static int arListCommand(ArCommand *pAr){
13349 const char *zSql = "SELECT %s FROM %s WHERE %s";
13350 const char *azCols[] = {
13352 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
13356 sqlite3_stmt *pSql = 0;
13359 rc = arCheckEntries(pAr);
13360 arWhereClause(&rc, pAr, &zWhere);
13362 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
13363 pAr->zSrcTable, zWhere);
13364 if( pAr->bDryRun ){
13365 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13367 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13368 if( pAr->bVerbose ){
13369 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
13370 sqlite3_column_text(pSql, 0),
13371 sqlite3_column_int(pSql, 1),
13372 sqlite3_column_text(pSql, 2),
13373 sqlite3_column_text(pSql, 3)
13376 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13380 shellFinalize(&rc, pSql);
13381 sqlite3_free(zWhere);
13387 ** Implementation of .ar "eXtract" command.
13389 static int arExtractCommand(ArCommand *pAr){
13390 const char *zSql1 =
13393 " writefile(($dir || name), %s, mode, mtime) "
13394 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
13395 " AND name NOT GLOB '*..[/\\]*'";
13397 const char *azExtraArg[] = {
13398 "sqlar_uncompress(data, sz)",
13402 sqlite3_stmt *pSql = 0;
13403 int rc = SQLITE_OK;
13408 /* If arguments are specified, check that they actually exist within
13409 ** the archive before proceeding. And formulate a WHERE clause to
13411 rc = arCheckEntries(pAr);
13412 arWhereClause(&rc, pAr, &zWhere);
13414 if( rc==SQLITE_OK ){
13416 zDir = sqlite3_mprintf("%s/", pAr->zDir);
13418 zDir = sqlite3_mprintf("");
13420 if( zDir==0 ) rc = SQLITE_NOMEM;
13423 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
13424 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
13427 if( rc==SQLITE_OK ){
13428 j = sqlite3_bind_parameter_index(pSql, "$dir");
13429 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
13431 /* Run the SELECT statement twice. The first time, writefile() is called
13432 ** for all archive members that should be extracted. The second time,
13433 ** only for the directories. This is because the timestamps for
13434 ** extracted directories must be reset after they are populated (as
13435 ** populating them changes the timestamp). */
13436 for(i=0; i<2; i++){
13437 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
13438 sqlite3_bind_int(pSql, j, i);
13439 if( pAr->bDryRun ){
13440 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13442 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13443 if( i==0 && pAr->bVerbose ){
13444 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13448 shellReset(&rc, pSql);
13450 shellFinalize(&rc, pSql);
13453 sqlite3_free(zDir);
13454 sqlite3_free(zWhere);
13459 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
13461 static int arExecSql(ArCommand *pAr, const char *zSql){
13463 if( pAr->bDryRun ){
13464 utf8_printf(pAr->p->out, "%s\n", zSql);
13468 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
13470 utf8_printf(stdout, "ERROR: %s\n", zErr);
13471 sqlite3_free(zErr);
13479 ** Implementation of .ar "create", "insert", and "update" commands.
13481 ** create -> Create a new SQL archive
13482 ** insert -> Insert or reinsert all files listed
13483 ** update -> Insert files that have changed or that were not
13484 ** previously in the archive
13486 ** Create the "sqlar" table in the database if it does not already exist.
13487 ** Then add each file in the azFile[] array to the archive. Directories
13488 ** are added recursively. If argument bVerbose is non-zero, a message is
13489 ** printed on stdout for each file archived.
13491 ** The create command is the same as update, except that it drops
13492 ** any existing "sqlar" table before beginning. The "insert" command
13493 ** always overwrites every file named on the command-line, where as
13494 ** "update" only overwrites if the size or mtime or mode has changed.
13496 static int arCreateOrUpdateCommand(
13497 ArCommand *pAr, /* Command arguments and options */
13498 int bUpdate, /* true for a --create. */
13499 int bOnlyIfChanged /* Only update if file has changed */
13501 const char *zCreate =
13502 "CREATE TABLE IF NOT EXISTS sqlar(\n"
13503 " name TEXT PRIMARY KEY, -- name of the file\n"
13504 " mode INT, -- access permissions\n"
13505 " mtime INT, -- last modification time\n"
13506 " sz INT, -- original file size\n"
13507 " data BLOB -- compressed content\n"
13509 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
13510 const char *zInsertFmt[2] = {
13511 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
13516 " CASE substr(lsmode(mode),1,1)\n"
13517 " WHEN '-' THEN length(data)\n"
13518 " WHEN 'd' THEN 0\n"
13520 " sqlar_compress(data)\n"
13521 " FROM fsdir(%Q,%Q) AS disk\n"
13522 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13524 "REPLACE INTO %s(name,mode,mtime,data)\n"
13530 " FROM fsdir(%Q,%Q) AS disk\n"
13531 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
13533 int i; /* For iterating through azFile[] */
13534 int rc; /* Return code */
13535 const char *zTab = 0; /* SQL table into which to insert */
13540 arExecSql(pAr, "PRAGMA page_size=512");
13541 rc = arExecSql(pAr, "SAVEPOINT ar;");
13542 if( rc!=SQLITE_OK ) return rc;
13545 /* Initialize the zipfile virtual table, if necessary */
13548 sqlite3_randomness(sizeof(r),&r);
13549 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
13551 zSql = sqlite3_mprintf(
13552 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
13555 rc = arExecSql(pAr, zSql);
13556 sqlite3_free(zSql);
13561 /* Initialize the table for an SQLAR */
13564 rc = arExecSql(pAr, zDrop);
13565 if( rc!=SQLITE_OK ) goto end_ar_transaction;
13567 rc = arExecSql(pAr, zCreate);
13569 if( bOnlyIfChanged ){
13570 zExists = sqlite3_mprintf(
13572 "SELECT 1 FROM %s AS mem"
13573 " WHERE mem.name=disk.name"
13574 " AND mem.mtime=disk.mtime"
13575 " AND mem.mode=disk.mode)", zTab);
13577 zExists = sqlite3_mprintf("");
13579 if( zExists==0 ) rc = SQLITE_NOMEM;
13580 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13581 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13582 pAr->bVerbose ? "shell_putsnl(name)" : "name",
13583 pAr->azArg[i], pAr->zDir, zExists);
13584 rc = arExecSql(pAr, zSql2);
13585 sqlite3_free(zSql2);
13587 end_ar_transaction:
13588 if( rc!=SQLITE_OK ){
13589 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
13591 rc = arExecSql(pAr, "RELEASE ar;");
13592 if( pAr->bZip && pAr->zFile ){
13593 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13594 arExecSql(pAr, zSql);
13595 sqlite3_free(zSql);
13598 sqlite3_free(zExists);
13603 ** Implementation of ".ar" dot command.
13605 static int arDotCommand(
13606 ShellState *pState, /* Current shell tool state */
13607 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
13608 char **azArg, /* Array of arguments passed to dot command */
13609 int nArg /* Number of entries in azArg[] */
13613 memset(&cmd, 0, sizeof(cmd));
13614 cmd.fromCmdLine = fromCmdLine;
13615 rc = arParseCommand(azArg, nArg, &cmd);
13616 if( rc==SQLITE_OK ){
13617 int eDbType = SHELL_OPEN_UNSPEC;
13619 cmd.db = pState->db;
13621 eDbType = deduceDatabaseType(cmd.zFile, 1);
13623 eDbType = pState->openMode;
13625 if( eDbType==SHELL_OPEN_ZIPFILE ){
13626 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
13627 if( cmd.zFile==0 ){
13628 cmd.zSrcTable = sqlite3_mprintf("zip");
13630 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13634 }else if( cmd.zFile ){
13636 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13637 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
13638 || cmd.eCmd==AR_CMD_UPDATE ){
13639 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13641 flags = SQLITE_OPEN_READONLY;
13645 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13646 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13648 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
13649 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13650 if( rc!=SQLITE_OK ){
13651 utf8_printf(stderr, "cannot open file: %s (%s)\n",
13652 cmd.zFile, sqlite3_errmsg(cmd.db)
13654 goto end_ar_command;
13656 sqlite3_fileio_init(cmd.db, 0, 0);
13657 sqlite3_sqlar_init(cmd.db, 0, 0);
13658 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13659 shellPutsFunc, 0, 0);
13662 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13663 if( cmd.eCmd!=AR_CMD_CREATE
13664 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13666 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13668 goto end_ar_command;
13670 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13673 switch( cmd.eCmd ){
13674 case AR_CMD_CREATE:
13675 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
13678 case AR_CMD_EXTRACT:
13679 rc = arExtractCommand(&cmd);
13683 rc = arListCommand(&cmd);
13687 arUsage(pState->out);
13690 case AR_CMD_INSERT:
13691 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
13695 assert( cmd.eCmd==AR_CMD_UPDATE );
13696 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
13701 if( cmd.db!=pState->db ){
13704 sqlite3_free(cmd.zSrcTable);
13708 /* End of the ".archive" or ".ar" command logic
13709 **********************************************************************************/
13710 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13714 ** If an input line begins with "." then invoke this routine to
13715 ** process that line.
13717 ** Return 1 on error, 2 to exit, and 0 otherwise.
13719 static int do_meta_command(char *zLine, ShellState *p){
13726 #ifndef SQLITE_OMIT_VIRTUALTABLE
13727 if( p->expert.pExpert ){
13728 expertFinish(p, 1, 0);
13732 /* Parse the input line into tokens.
13734 while( zLine[h] && nArg<ArraySize(azArg) ){
13735 while( IsSpace(zLine[h]) ){ h++; }
13736 if( zLine[h]==0 ) break;
13737 if( zLine[h]=='\'' || zLine[h]=='"' ){
13738 int delim = zLine[h++];
13739 azArg[nArg++] = &zLine[h];
13740 while( zLine[h] && zLine[h]!=delim ){
13741 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13744 if( zLine[h]==delim ){
13747 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13749 azArg[nArg++] = &zLine[h];
13750 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13751 if( zLine[h] ) zLine[h++] = 0;
13752 resolve_backslashes(azArg[nArg-1]);
13756 /* Process the input line.
13758 if( nArg==0 ) return 0; /* no tokens, no error */
13759 n = strlen30(azArg[0]);
13763 #ifndef SQLITE_OMIT_AUTHORIZATION
13764 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13766 raw_printf(stderr, "Usage: .auth ON|OFF\n");
13768 goto meta_command_exit;
13771 if( booleanValue(azArg[1]) ){
13772 sqlite3_set_authorizer(p->db, shellAuth, p);
13774 sqlite3_set_authorizer(p->db, 0, 0);
13779 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13780 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13782 rc = arDotCommand(p, 0, azArg, nArg);
13786 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13787 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13789 const char *zDestFile = 0;
13790 const char *zDb = 0;
13792 sqlite3_backup *pBackup;
13795 const char *zVfs = 0;
13796 for(j=1; j<nArg; j++){
13797 const char *z = azArg[j];
13799 if( z[1]=='-' ) z++;
13800 if( strcmp(z, "-append")==0 ){
13803 if( strcmp(z, "-async")==0 ){
13807 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13810 }else if( zDestFile==0 ){
13811 zDestFile = azArg[j];
13812 }else if( zDb==0 ){
13814 zDestFile = azArg[j];
13816 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
13820 if( zDestFile==0 ){
13821 raw_printf(stderr, "missing FILENAME argument on .backup\n");
13824 if( zDb==0 ) zDb = "main";
13825 rc = sqlite3_open_v2(zDestFile, &pDest,
13826 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13827 if( rc!=SQLITE_OK ){
13828 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13833 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
13837 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13839 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13843 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13844 sqlite3_backup_finish(pBackup);
13845 if( rc==SQLITE_DONE ){
13848 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13854 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13856 bail_on_error = booleanValue(azArg[1]);
13858 raw_printf(stderr, "Usage: .bail on|off\n");
13863 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13865 if( booleanValue(azArg[1]) ){
13866 setBinaryMode(p->out, 1);
13868 setTextMode(p->out, 1);
13871 raw_printf(stderr, "Usage: .binary on|off\n");
13876 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13878 #if defined(_WIN32) || defined(WIN32)
13879 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13880 rc = !SetCurrentDirectoryW(z);
13883 rc = chdir(azArg[1]);
13886 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13890 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13895 /* The undocumented ".breakpoint" command causes a call to the no-op
13896 ** routine named test_breakpoint().
13898 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13902 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13904 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13906 raw_printf(stderr, "Usage: .changes on|off\n");
13911 /* Cancel output redirection, if it is currently set (by .testcase)
13912 ** Then read the content of the testcase-out.txt file and compare against
13913 ** azArg[1]. If there are differences, report an error and exit.
13915 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13919 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13921 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13922 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13924 }else if( testcase_glob(azArg[1],zRes)==0 ){
13925 utf8_printf(stderr,
13926 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
13927 p->zTestcase, azArg[1], zRes);
13930 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13933 sqlite3_free(zRes);
13936 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13938 tryToClone(p, azArg[1]);
13940 raw_printf(stderr, "Usage: .clone FILENAME\n");
13945 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13949 memcpy(&data, p, sizeof(data));
13950 data.showHeader = 0;
13951 data.cMode = data.mode = MODE_List;
13952 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13954 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13955 callback, &data, &zErrMsg);
13957 utf8_printf(stderr,"Error: %s\n", zErrMsg);
13958 sqlite3_free(zErrMsg);
13963 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13964 static const struct DbConfigChoices {
13968 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13969 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13970 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13971 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13972 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13973 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13974 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13975 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13976 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
13980 for(ii=0; ii<ArraySize(aDbConfig); ii++){
13981 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13983 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13985 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13986 utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13987 if( nArg>1 ) break;
13989 if( nArg>1 && ii==ArraySize(aDbConfig) ){
13990 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13991 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13995 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13996 rc = shell_dbinfo_command(p, nArg, azArg);
13999 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
14000 const char *zLike = 0;
14002 int savedShowHeader = p->showHeader;
14003 int savedShellFlags = p->shellFlgs;
14004 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
14005 for(i=1; i<nArg; i++){
14006 if( azArg[i][0]=='-' ){
14007 const char *z = azArg[i]+1;
14008 if( z[0]=='-' ) z++;
14009 if( strcmp(z,"preserve-rowids")==0 ){
14010 #ifdef SQLITE_OMIT_VIRTUALTABLE
14011 raw_printf(stderr, "The --preserve-rowids option is not compatible"
14012 " with SQLITE_OMIT_VIRTUALTABLE\n");
14014 goto meta_command_exit;
14016 ShellSetFlag(p, SHFLG_PreserveRowid);
14019 if( strcmp(z,"newlines")==0 ){
14020 ShellSetFlag(p, SHFLG_Newlines);
14023 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
14025 goto meta_command_exit;
14028 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
14029 "?--newlines? ?LIKE-PATTERN?\n");
14031 goto meta_command_exit;
14037 /* When playing back a "dump", the content might appear in an order
14038 ** which causes immediate foreign key constraints to be violated.
14039 ** So disable foreign-key constraint enforcement to prevent problems. */
14040 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
14041 raw_printf(p->out, "BEGIN TRANSACTION;\n");
14042 p->writableSchema = 0;
14044 /* Set writable_schema=ON since doing so forces SQLite to initialize
14045 ** as much of the schema as it can even if the sqlite_master table is
14047 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
14050 run_schema_dump_query(p,
14051 "SELECT name, type, sql FROM sqlite_master "
14052 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
14054 run_schema_dump_query(p,
14055 "SELECT name, type, sql FROM sqlite_master "
14056 "WHERE name=='sqlite_sequence'"
14058 run_table_dump_query(p,
14059 "SELECT sql FROM sqlite_master "
14060 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
14064 zSql = sqlite3_mprintf(
14065 "SELECT name, type, sql FROM sqlite_master "
14066 "WHERE tbl_name LIKE %Q AND type=='table'"
14067 " AND sql NOT NULL", zLike);
14068 run_schema_dump_query(p,zSql);
14069 sqlite3_free(zSql);
14070 zSql = sqlite3_mprintf(
14071 "SELECT sql FROM sqlite_master "
14072 "WHERE sql NOT NULL"
14073 " AND type IN ('index','trigger','view')"
14074 " AND tbl_name LIKE %Q", zLike);
14075 run_table_dump_query(p, zSql, 0);
14076 sqlite3_free(zSql);
14078 if( p->writableSchema ){
14079 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
14080 p->writableSchema = 0;
14082 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
14083 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
14084 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
14085 p->showHeader = savedShowHeader;
14086 p->shellFlgs = savedShellFlags;
14089 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
14091 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
14093 raw_printf(stderr, "Usage: .echo on|off\n");
14098 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
14100 p->autoEQPtest = 0;
14101 if( p->autoEQPtrace ){
14102 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
14103 p->autoEQPtrace = 0;
14105 if( strcmp(azArg[1],"full")==0 ){
14106 p->autoEQP = AUTOEQP_full;
14107 }else if( strcmp(azArg[1],"trigger")==0 ){
14108 p->autoEQP = AUTOEQP_trigger;
14109 #ifdef SQLITE_DEBUG
14110 }else if( strcmp(azArg[1],"test")==0 ){
14111 p->autoEQP = AUTOEQP_on;
14112 p->autoEQPtest = 1;
14113 }else if( strcmp(azArg[1],"trace")==0 ){
14114 p->autoEQP = AUTOEQP_full;
14115 p->autoEQPtrace = 1;
14117 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
14118 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
14121 p->autoEQP = (u8)booleanValue(azArg[1]);
14124 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
14129 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
14130 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
14134 /* The ".explain" command is automatic now. It is largely pointless. It
14135 ** retained purely for backwards compatibility */
14136 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
14139 if( strcmp(azArg[1],"auto")==0 ){
14142 val = booleanValue(azArg[1]);
14145 if( val==1 && p->mode!=MODE_Explain ){
14146 p->normalMode = p->mode;
14147 p->mode = MODE_Explain;
14148 p->autoExplain = 0;
14149 }else if( val==0 ){
14150 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14151 p->autoExplain = 0;
14152 }else if( val==99 ){
14153 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14154 p->autoExplain = 1;
14158 #ifndef SQLITE_OMIT_VIRTUALTABLE
14159 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
14161 expertDotCommand(p, azArg, nArg);
14165 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
14169 memcpy(&data, p, sizeof(data));
14170 data.showHeader = 0;
14171 data.cMode = data.mode = MODE_Semi;
14172 if( nArg==2 && optionMatch(azArg[1], "indent") ){
14173 data.cMode = data.mode = MODE_Pretty;
14177 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
14179 goto meta_command_exit;
14182 rc = sqlite3_exec(p->db,
14184 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
14185 " FROM sqlite_master UNION ALL"
14186 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
14187 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
14189 callback, &data, &zErrMsg
14191 if( rc==SQLITE_OK ){
14192 sqlite3_stmt *pStmt;
14193 rc = sqlite3_prepare_v2(p->db,
14194 "SELECT rowid FROM sqlite_master"
14195 " WHERE name GLOB 'sqlite_stat[134]'",
14197 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
14198 sqlite3_finalize(pStmt);
14201 raw_printf(p->out, "/* No STAT tables available */\n");
14203 raw_printf(p->out, "ANALYZE sqlite_master;\n");
14204 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
14205 callback, &data, &zErrMsg);
14206 data.cMode = data.mode = MODE_Insert;
14207 data.zDestTable = "sqlite_stat1";
14208 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
14209 data.zDestTable = "sqlite_stat3";
14210 shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
14211 data.zDestTable = "sqlite_stat4";
14212 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
14213 raw_printf(p->out, "ANALYZE sqlite_master;\n");
14217 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
14219 p->showHeader = booleanValue(azArg[1]);
14221 raw_printf(stderr, "Usage: .headers on|off\n");
14226 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
14228 n = showHelp(p->out, azArg[1]);
14230 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
14233 showHelp(p->out, 0);
14237 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
14238 char *zTable; /* Insert data into this table */
14239 char *zFile; /* Name of file to extra content from */
14240 sqlite3_stmt *pStmt = NULL; /* A statement */
14241 int nCol; /* Number of columns in the table */
14242 int nByte; /* Number of bytes in an SQL string */
14243 int i, j; /* Loop counters */
14244 int needCommit; /* True to COMMIT or ROLLBACK at end */
14245 int nSep; /* Number of bytes in p->colSeparator[] */
14246 char *zSql; /* An SQL statement */
14247 ImportCtx sCtx; /* Reader context */
14248 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
14249 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
14252 raw_printf(stderr, "Usage: .import FILE TABLE\n");
14253 goto meta_command_exit;
14258 memset(&sCtx, 0, sizeof(sCtx));
14260 nSep = strlen30(p->colSeparator);
14263 "Error: non-null column separator required for import\n");
14267 raw_printf(stderr, "Error: multi-character column separators not allowed"
14271 nSep = strlen30(p->rowSeparator);
14273 raw_printf(stderr, "Error: non-null row separator required for import\n");
14276 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
14277 /* When importing CSV (only), if the row separator is set to the
14278 ** default output row separator, change it to the default input
14279 ** row separator. This avoids having to maintain different input
14280 ** and output row separators. */
14281 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14282 nSep = strlen30(p->rowSeparator);
14285 raw_printf(stderr, "Error: multi-character row separators not allowed"
14289 sCtx.zFile = zFile;
14291 if( sCtx.zFile[0]=='|' ){
14292 #ifdef SQLITE_OMIT_POPEN
14293 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14296 sCtx.in = popen(sCtx.zFile+1, "r");
14297 sCtx.zFile = "<pipe>";
14301 sCtx.in = fopen(sCtx.zFile, "rb");
14304 if( p->mode==MODE_Ascii ){
14305 xRead = ascii_read_one_field;
14307 xRead = csv_read_one_field;
14310 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14313 sCtx.cColSep = p->colSeparator[0];
14314 sCtx.cRowSep = p->rowSeparator[0];
14315 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
14318 shell_out_of_memory();
14320 nByte = strlen30(zSql);
14321 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14322 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
14323 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
14324 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
14326 while( xRead(&sCtx) ){
14327 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
14329 if( sCtx.cTerm!=sCtx.cColSep ) break;
14332 sqlite3_free(zCreate);
14333 sqlite3_free(sCtx.z);
14335 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
14338 zCreate = sqlite3_mprintf("%z\n)", zCreate);
14339 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
14340 sqlite3_free(zCreate);
14342 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
14343 sqlite3_errmsg(p->db));
14344 sqlite3_free(sCtx.z);
14348 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14350 sqlite3_free(zSql);
14352 if (pStmt) sqlite3_finalize(pStmt);
14353 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
14357 nCol = sqlite3_column_count(pStmt);
14358 sqlite3_finalize(pStmt);
14360 if( nCol==0 ) return 0; /* no columns, no error */
14361 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
14364 shell_out_of_memory();
14366 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
14367 j = strlen30(zSql);
14368 for(i=1; i<nCol; i++){
14374 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14375 sqlite3_free(zSql);
14377 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14378 if (pStmt) sqlite3_finalize(pStmt);
14382 needCommit = sqlite3_get_autocommit(p->db);
14383 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
14385 int startLine = sCtx.nLine;
14386 for(i=0; i<nCol; i++){
14387 char *z = xRead(&sCtx);
14389 ** Did we reach end-of-file before finding any columns?
14390 ** If so, stop instead of NULL filling the remaining columns.
14392 if( z==0 && i==0 ) break;
14394 ** Did we reach end-of-file OR end-of-line before finding any
14395 ** columns in ASCII mode? If so, stop instead of NULL filling
14396 ** the remaining columns.
14398 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
14399 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
14400 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
14401 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14402 "filling the rest with NULL\n",
14403 sCtx.zFile, startLine, nCol, i+1);
14405 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
14408 if( sCtx.cTerm==sCtx.cColSep ){
14412 }while( sCtx.cTerm==sCtx.cColSep );
14413 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14414 "extras ignored\n",
14415 sCtx.zFile, startLine, nCol, i);
14418 sqlite3_step(pStmt);
14419 rc = sqlite3_reset(pStmt);
14420 if( rc!=SQLITE_OK ){
14421 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
14422 startLine, sqlite3_errmsg(p->db));
14425 }while( sCtx.cTerm!=EOF );
14428 sqlite3_free(sCtx.z);
14429 sqlite3_finalize(pStmt);
14430 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
14433 #ifndef SQLITE_UNTESTABLE
14434 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
14436 char *zCollist = 0;
14437 sqlite3_stmt *pStmt;
14440 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
14441 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
14442 " .imposter off\n");
14444 goto meta_command_exit;
14448 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
14449 goto meta_command_exit;
14451 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
14452 " WHERE name='%q' AND type='index'", azArg[1]);
14453 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14454 sqlite3_free(zSql);
14455 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14456 tnum = sqlite3_column_int(pStmt, 0);
14458 sqlite3_finalize(pStmt);
14460 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
14462 goto meta_command_exit;
14464 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
14465 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14466 sqlite3_free(zSql);
14468 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14470 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
14473 if( sqlite3_column_int(pStmt,1)==-1 ){
14476 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
14481 zCollist = sqlite3_mprintf("\"%w\"", zCol);
14483 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
14486 sqlite3_finalize(pStmt);
14487 zSql = sqlite3_mprintf(
14488 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
14489 azArg[2], zCollist, zCollist);
14490 sqlite3_free(zCollist);
14491 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
14492 if( rc==SQLITE_OK ){
14493 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
14494 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
14496 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
14498 utf8_printf(stdout, "%s;\n", zSql);
14500 "WARNING: writing to an imposter table will corrupt the index!\n"
14504 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
14507 sqlite3_free(zSql);
14509 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
14511 #ifdef SQLITE_ENABLE_IOTRACE
14512 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
14513 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
14514 if( iotrace && iotrace!=stdout ) fclose(iotrace);
14517 sqlite3IoTrace = 0;
14518 }else if( strcmp(azArg[1], "-")==0 ){
14519 sqlite3IoTrace = iotracePrintf;
14522 iotrace = fopen(azArg[1], "w");
14524 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
14525 sqlite3IoTrace = 0;
14528 sqlite3IoTrace = iotracePrintf;
14534 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
14535 static const struct {
14536 const char *zLimitName; /* Name of a limit */
14537 int limitCode; /* Integer code for that limit */
14539 { "length", SQLITE_LIMIT_LENGTH },
14540 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
14541 { "column", SQLITE_LIMIT_COLUMN },
14542 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
14543 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
14544 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
14545 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
14546 { "attached", SQLITE_LIMIT_ATTACHED },
14547 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
14548 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
14549 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
14550 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
14555 for(i=0; i<ArraySize(aLimit); i++){
14556 printf("%20s %d\n", aLimit[i].zLimitName,
14557 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
14559 }else if( nArg>3 ){
14560 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
14562 goto meta_command_exit;
14565 n2 = strlen30(azArg[1]);
14566 for(i=0; i<ArraySize(aLimit); i++){
14567 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
14571 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
14573 goto meta_command_exit;
14578 utf8_printf(stderr, "unknown limit: \"%s\"\n"
14579 "enter \".limits\" with no arguments for a list.\n",
14582 goto meta_command_exit;
14585 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
14586 (int)integerValue(azArg[2]));
14588 printf("%20s %d\n", aLimit[iLimit].zLimitName,
14589 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
14593 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
14595 lintDotCommand(p, azArg, nArg);
14598 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14599 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
14600 const char *zFile, *zProc;
14603 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
14605 goto meta_command_exit;
14608 zProc = nArg>=3 ? azArg[2] : 0;
14610 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
14611 if( rc!=SQLITE_OK ){
14612 utf8_printf(stderr, "Error: %s\n", zErrMsg);
14613 sqlite3_free(zErrMsg);
14619 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
14621 raw_printf(stderr, "Usage: .log FILENAME\n");
14624 const char *zFile = azArg[1];
14625 output_file_close(p->pLog);
14626 p->pLog = output_file_open(zFile, 0);
14630 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
14631 const char *zMode = nArg>=2 ? azArg[1] : "";
14632 int n2 = strlen30(zMode);
14634 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
14635 p->mode = MODE_Line;
14636 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14637 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
14638 p->mode = MODE_Column;
14639 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14640 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
14641 p->mode = MODE_List;
14642 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
14643 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14644 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
14645 p->mode = MODE_Html;
14646 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
14647 p->mode = MODE_Tcl;
14648 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
14649 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14650 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
14651 p->mode = MODE_Csv;
14652 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14653 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14654 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
14655 p->mode = MODE_List;
14656 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
14657 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
14658 p->mode = MODE_Insert;
14659 set_table_name(p, nArg>=3 ? azArg[2] : "table");
14660 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
14661 p->mode = MODE_Quote;
14662 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
14663 p->mode = MODE_Ascii;
14664 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
14665 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14666 }else if( nArg==1 ){
14667 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14669 raw_printf(stderr, "Error: mode should be one of: "
14670 "ascii column csv html insert line list quote tabs tcl\n");
14673 p->cMode = p->mode;
14676 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14678 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14679 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14681 raw_printf(stderr, "Usage: .nullvalue STRING\n");
14686 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14687 char *zNewFilename; /* Name of the database file to open */
14688 int iName = 1; /* Index in azArg[] of the filename */
14689 int newFlag = 0; /* True to delete file before opening */
14690 /* Close the existing database */
14691 session_close_all(p);
14694 p->zDbFilename = 0;
14695 sqlite3_free(p->zFreeOnClose);
14696 p->zFreeOnClose = 0;
14697 p->openMode = SHELL_OPEN_UNSPEC;
14699 /* Check for command-line arguments */
14700 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14701 const char *z = azArg[iName];
14702 if( optionMatch(z,"new") ){
14704 #ifdef SQLITE_HAVE_ZLIB
14705 }else if( optionMatch(z, "zip") ){
14706 p->openMode = SHELL_OPEN_ZIPFILE;
14708 }else if( optionMatch(z, "append") ){
14709 p->openMode = SHELL_OPEN_APPENDVFS;
14710 }else if( optionMatch(z, "readonly") ){
14711 p->openMode = SHELL_OPEN_READONLY;
14712 #ifdef SQLITE_ENABLE_DESERIALIZE
14713 }else if( optionMatch(z, "deserialize") ){
14714 p->openMode = SHELL_OPEN_DESERIALIZE;
14715 }else if( optionMatch(z, "hexdb") ){
14716 p->openMode = SHELL_OPEN_HEXDB;
14717 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
14718 p->szMax = integerValue(azArg[++iName]);
14719 #endif /* SQLITE_ENABLE_DESERIALIZE */
14720 }else if( z[0]=='-' ){
14721 utf8_printf(stderr, "unknown option: %s\n", z);
14723 goto meta_command_exit;
14726 /* If a filename is specified, try to open it first */
14727 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14728 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
14729 if( newFlag ) shellDeleteFile(zNewFilename);
14730 p->zDbFilename = zNewFilename;
14731 open_db(p, OPEN_DB_KEEPALIVE);
14733 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14734 sqlite3_free(zNewFilename);
14736 p->zFreeOnClose = zNewFilename;
14740 /* As a fall-back open a TEMP database */
14741 p->zDbFilename = 0;
14747 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14748 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14750 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14752 if( azArg[0][0]=='e' ){
14753 /* Transform the ".excel" command into ".once -x" */
14756 zFile = azArg[1] = "-x";
14760 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14762 goto meta_command_exit;
14764 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14766 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14768 goto meta_command_exit;
14775 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14776 #ifndef SQLITE_NOHAVE_SYSTEM
14777 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14780 if( zFile[1]=='x' ){
14781 newTempFile(p, "csv");
14782 p->mode = MODE_Csv;
14783 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14784 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14786 newTempFile(p, "txt");
14789 zFile = p->zTempFile;
14791 #endif /* SQLITE_NOHAVE_SYSTEM */
14792 if( zFile[0]=='|' ){
14793 #ifdef SQLITE_OMIT_POPEN
14794 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14798 p->out = popen(zFile + 1, "w");
14800 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14804 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14808 p->out = output_file_open(zFile, bTxtMode);
14810 if( strcmp(zFile,"off")!=0 ){
14811 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14816 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14821 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
14823 if( nArg<=1 ) goto parameter_syntax_error;
14825 /* .parameter clear
14826 ** Clear all bind parameters by dropping the TEMP table that holds them.
14828 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
14830 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14831 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14832 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
14834 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14838 ** List all bind parameters.
14840 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
14841 sqlite3_stmt *pStmt = 0;
14844 rx = sqlite3_prepare_v2(p->db,
14845 "SELECT max(length(key)) "
14846 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14847 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
14848 len = sqlite3_column_int(pStmt, 0);
14849 if( len>40 ) len = 40;
14851 sqlite3_finalize(pStmt);
14854 rx = sqlite3_prepare_v2(p->db,
14855 "SELECT key, quote(value) "
14856 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
14857 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14858 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
14859 sqlite3_column_text(pStmt,1));
14861 sqlite3_finalize(pStmt);
14866 ** Make sure the TEMP table used to hold bind parameters exists.
14867 ** Create it if necessary.
14869 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
14870 bind_table_init(p);
14873 /* .parameter set NAME VALUE
14874 ** Set or reset a bind parameter. NAME should be the full parameter
14875 ** name exactly as it appears in the query. (ex: $abc, @def). The
14876 ** VALUE can be in either SQL literal notation, or if not it will be
14877 ** understood to be a text string.
14879 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
14882 sqlite3_stmt *pStmt;
14883 const char *zKey = azArg[2];
14884 const char *zValue = azArg[3];
14885 bind_table_init(p);
14886 zSql = sqlite3_mprintf(
14887 "REPLACE INTO temp.sqlite_parameters(key,value)"
14888 "VALUES(%Q,%s);", zKey, zValue);
14889 if( zSql==0 ) shell_out_of_memory();
14891 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14892 sqlite3_free(zSql);
14893 if( rx!=SQLITE_OK ){
14894 sqlite3_finalize(pStmt);
14896 zSql = sqlite3_mprintf(
14897 "REPLACE INTO temp.sqlite_parameters(key,value)"
14898 "VALUES(%Q,%Q);", zKey, zValue);
14899 if( zSql==0 ) shell_out_of_memory();
14900 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14901 sqlite3_free(zSql);
14902 if( rx!=SQLITE_OK ){
14903 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
14904 sqlite3_finalize(pStmt);
14909 sqlite3_step(pStmt);
14910 sqlite3_finalize(pStmt);
14913 /* .parameter unset NAME
14914 ** Remove the NAME binding from the parameter binding table, if it
14917 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
14918 char *zSql = sqlite3_mprintf(
14919 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
14920 if( zSql==0 ) shell_out_of_memory();
14921 sqlite3_exec(p->db, zSql, 0, 0, 0);
14922 sqlite3_free(zSql);
14924 /* If no command name matches, show a syntax error */
14925 parameter_syntax_error:
14926 showHelp(p->out, "parameter");
14929 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14931 for(i=1; i<nArg; i++){
14932 if( i>1 ) raw_printf(p->out, " ");
14933 utf8_printf(p->out, "%s", azArg[i]);
14935 raw_printf(p->out, "\n");
14938 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14939 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
14942 p->flgProgress = 0;
14945 for(i=1; i<nArg; i++){
14946 const char *z = azArg[i];
14949 if( z[0]=='-' ) z++;
14950 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
14951 p->flgProgress |= SHELL_PROGRESS_QUIET;
14954 if( strcmp(z,"reset")==0 ){
14955 p->flgProgress |= SHELL_PROGRESS_RESET;
14958 if( strcmp(z,"once")==0 ){
14959 p->flgProgress |= SHELL_PROGRESS_ONCE;
14962 if( strcmp(z,"limit")==0 ){
14964 utf8_printf(stderr, "Error: missing argument on --limit\n");
14966 goto meta_command_exit;
14968 p->mxProgress = (int)integerValue(azArg[++i]);
14972 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
14974 goto meta_command_exit;
14976 nn = (int)integerValue(z);
14980 sqlite3_progress_handler(p->db, nn, progress_handler, p);
14982 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
14984 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14986 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14989 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14993 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14997 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14998 FILE *inSaved = p->in;
14999 int savedLineno = p->lineno;
15001 raw_printf(stderr, "Usage: .read FILE\n");
15003 goto meta_command_exit;
15005 p->in = fopen(azArg[1], "rb");
15007 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
15010 rc = process_input(p);
15014 p->lineno = savedLineno;
15017 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
15018 const char *zSrcFile;
15021 sqlite3_backup *pBackup;
15025 zSrcFile = azArg[1];
15027 }else if( nArg==3 ){
15028 zSrcFile = azArg[2];
15031 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
15033 goto meta_command_exit;
15035 rc = sqlite3_open(zSrcFile, &pSrc);
15036 if( rc!=SQLITE_OK ){
15037 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
15042 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
15044 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15048 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
15049 || rc==SQLITE_BUSY ){
15050 if( rc==SQLITE_BUSY ){
15051 if( nTimeout++ >= 3 ) break;
15052 sqlite3_sleep(100);
15055 sqlite3_backup_finish(pBackup);
15056 if( rc==SQLITE_DONE ){
15058 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
15059 raw_printf(stderr, "Error: source database is busy\n");
15062 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15068 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
15070 p->scanstatsOn = (u8)booleanValue(azArg[1]);
15071 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
15072 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
15075 raw_printf(stderr, "Usage: .scanstats on|off\n");
15080 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
15084 const char *zDiv = "(";
15085 const char *zName = 0;
15091 memcpy(&data, p, sizeof(data));
15092 data.showHeader = 0;
15093 data.cMode = data.mode = MODE_Semi;
15094 initText(&sSelect);
15095 for(ii=1; ii<nArg; ii++){
15096 if( optionMatch(azArg[ii],"indent") ){
15097 data.cMode = data.mode = MODE_Pretty;
15098 }else if( optionMatch(azArg[ii],"debug") ){
15100 }else if( zName==0 ){
15103 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
15105 goto meta_command_exit;
15109 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
15110 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
15111 char *new_argv[2], *new_colv[2];
15112 new_argv[0] = sqlite3_mprintf(
15113 "CREATE TABLE %s (\n"
15116 " tbl_name text,\n"
15117 " rootpage integer,\n"
15119 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
15121 new_colv[0] = "sql";
15123 callback(&data, 1, new_argv, new_colv);
15124 sqlite3_free(new_argv[0]);
15128 sqlite3_stmt *pStmt = 0;
15129 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
15132 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
15133 sqlite3_finalize(pStmt);
15135 goto meta_command_exit;
15137 appendText(&sSelect, "SELECT sql FROM", 0);
15139 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15140 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
15142 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
15143 appendText(&sSelect, zDiv, 0);
15144 zDiv = " UNION ALL ";
15145 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
15146 if( sqlite3_stricmp(zDb, "main")!=0 ){
15147 appendText(&sSelect, zDb, '"');
15149 appendText(&sSelect, "NULL", 0);
15151 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
15152 appendText(&sSelect, zScNum, 0);
15153 appendText(&sSelect, " AS snum, ", 0);
15154 appendText(&sSelect, zDb, '\'');
15155 appendText(&sSelect, " AS sname FROM ", 0);
15156 appendText(&sSelect, zDb, '"');
15157 appendText(&sSelect, ".sqlite_master", 0);
15159 sqlite3_finalize(pStmt);
15160 #ifdef SQLITE_INTROSPECTION_PRAGMAS
15162 appendText(&sSelect,
15163 " UNION ALL SELECT shell_module_schema(name),"
15164 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
15167 appendText(&sSelect, ") WHERE ", 0);
15169 char *zQarg = sqlite3_mprintf("%Q", zName);
15170 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
15171 strchr(zName, '[') != 0;
15172 if( strchr(zName, '.') ){
15173 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
15175 appendText(&sSelect, "lower(tbl_name)", 0);
15177 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
15178 appendText(&sSelect, zQarg, 0);
15180 appendText(&sSelect, " ESCAPE '\\' ", 0);
15182 appendText(&sSelect, " AND ", 0);
15183 sqlite3_free(zQarg);
15185 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
15186 " ORDER BY snum, rowid", 0);
15188 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
15190 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
15192 freeText(&sSelect);
15195 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15196 sqlite3_free(zErrMsg);
15198 }else if( rc != SQLITE_OK ){
15199 raw_printf(stderr,"Error: querying schema information\n");
15206 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
15207 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
15208 sqlite3SelectTrace = (int)integerValue(azArg[1]);
15212 #if defined(SQLITE_ENABLE_SESSION)
15213 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
15214 OpenSession *pSession = &p->aSession[0];
15215 char **azCmd = &azArg[1];
15217 int nCmd = nArg - 1;
15219 if( nArg<=1 ) goto session_syntax_error;
15222 for(iSes=0; iSes<p->nSession; iSes++){
15223 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
15225 if( iSes<p->nSession ){
15226 pSession = &p->aSession[iSes];
15230 pSession = &p->aSession[0];
15235 /* .session attach TABLE
15236 ** Invoke the sqlite3session_attach() interface to attach a particular
15237 ** table so that it is never filtered.
15239 if( strcmp(azCmd[0],"attach")==0 ){
15240 if( nCmd!=2 ) goto session_syntax_error;
15241 if( pSession->p==0 ){
15243 raw_printf(stderr, "ERROR: No sessions are open\n");
15245 rc = sqlite3session_attach(pSession->p, azCmd[1]);
15247 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
15253 /* .session changeset FILE
15254 ** .session patchset FILE
15255 ** Write a changeset or patchset into a file. The file is overwritten.
15257 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
15259 if( nCmd!=2 ) goto session_syntax_error;
15260 if( pSession->p==0 ) goto session_not_open;
15261 out = fopen(azCmd[1], "wb");
15263 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
15267 if( azCmd[0][0]=='c' ){
15268 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
15270 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
15273 printf("Error: error code %d\n", rc);
15277 && fwrite(pChng, szChng, 1, out)!=1 ){
15278 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
15281 sqlite3_free(pChng);
15287 ** Close the identified session
15289 if( strcmp(azCmd[0], "close")==0 ){
15290 if( nCmd!=1 ) goto session_syntax_error;
15292 session_close(pSession);
15293 p->aSession[iSes] = p->aSession[--p->nSession];
15297 /* .session enable ?BOOLEAN?
15298 ** Query or set the enable flag
15300 if( strcmp(azCmd[0], "enable")==0 ){
15302 if( nCmd>2 ) goto session_syntax_error;
15303 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15305 ii = sqlite3session_enable(pSession->p, ii);
15306 utf8_printf(p->out, "session %s enable flag = %d\n",
15307 pSession->zName, ii);
15311 /* .session filter GLOB ....
15312 ** Set a list of GLOB patterns of table names to be excluded.
15314 if( strcmp(azCmd[0], "filter")==0 ){
15316 if( nCmd<2 ) goto session_syntax_error;
15318 for(ii=0; ii<pSession->nFilter; ii++){
15319 sqlite3_free(pSession->azFilter[ii]);
15321 sqlite3_free(pSession->azFilter);
15322 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
15323 pSession->azFilter = sqlite3_malloc( nByte );
15324 if( pSession->azFilter==0 ){
15325 raw_printf(stderr, "Error: out or memory\n");
15328 for(ii=1; ii<nCmd; ii++){
15329 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
15331 pSession->nFilter = ii-1;
15335 /* .session indirect ?BOOLEAN?
15336 ** Query or set the indirect flag
15338 if( strcmp(azCmd[0], "indirect")==0 ){
15340 if( nCmd>2 ) goto session_syntax_error;
15341 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15343 ii = sqlite3session_indirect(pSession->p, ii);
15344 utf8_printf(p->out, "session %s indirect flag = %d\n",
15345 pSession->zName, ii);
15349 /* .session isempty
15350 ** Determine if the session is empty
15352 if( strcmp(azCmd[0], "isempty")==0 ){
15354 if( nCmd!=1 ) goto session_syntax_error;
15356 ii = sqlite3session_isempty(pSession->p);
15357 utf8_printf(p->out, "session %s isempty flag = %d\n",
15358 pSession->zName, ii);
15363 ** List all currently open sessions
15365 if( strcmp(azCmd[0],"list")==0 ){
15366 for(i=0; i<p->nSession; i++){
15367 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
15371 /* .session open DB NAME
15372 ** Open a new session called NAME on the attached database DB.
15373 ** DB is normally "main".
15375 if( strcmp(azCmd[0],"open")==0 ){
15377 if( nCmd!=3 ) goto session_syntax_error;
15379 if( zName[0]==0 ) goto session_syntax_error;
15380 for(i=0; i<p->nSession; i++){
15381 if( strcmp(p->aSession[i].zName,zName)==0 ){
15382 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
15383 goto meta_command_exit;
15386 if( p->nSession>=ArraySize(p->aSession) ){
15387 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
15388 goto meta_command_exit;
15390 pSession = &p->aSession[p->nSession];
15391 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
15393 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
15395 goto meta_command_exit;
15397 pSession->nFilter = 0;
15398 sqlite3session_table_filter(pSession->p, session_filter, pSession);
15400 pSession->zName = sqlite3_mprintf("%s", zName);
15402 /* If no command name matches, show a syntax error */
15403 session_syntax_error:
15404 showHelp(p->out, "session");
15408 #ifdef SQLITE_DEBUG
15409 /* Undocumented commands for internal testing. Subject to change
15410 ** without notice. */
15411 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
15412 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
15414 for(i=1; i<nArg; i++){
15415 v = booleanValue(azArg[i]);
15416 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
15419 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
15420 int i; sqlite3_int64 v;
15421 for(i=1; i<nArg; i++){
15423 v = integerValue(azArg[i]);
15424 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
15425 utf8_printf(p->out, "%s", zBuf);
15431 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
15432 int bIsInit = 0; /* True to initialize the SELFTEST table */
15433 int bVerbose = 0; /* Verbose output */
15434 int bSelftestExists; /* True if SELFTEST already exists */
15435 int i, k; /* Loop counters */
15436 int nTest = 0; /* Number of tests runs */
15437 int nErr = 0; /* Number of errors seen */
15438 ShellText str; /* Answer for a query */
15439 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
15442 for(i=1; i<nArg; i++){
15443 const char *z = azArg[i];
15444 if( z[0]=='-' && z[1]=='-' ) z++;
15445 if( strcmp(z,"-init")==0 ){
15448 if( strcmp(z,"-v")==0 ){
15452 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15453 azArg[i], azArg[0]);
15454 raw_printf(stderr, "Should be one of: --init -v\n");
15456 goto meta_command_exit;
15459 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
15461 bSelftestExists = 0;
15463 bSelftestExists = 1;
15466 createSelftestTable(p);
15467 bSelftestExists = 1;
15470 appendText(&str, "x", 0);
15471 for(k=bSelftestExists; k>=0; k--){
15473 rc = sqlite3_prepare_v2(p->db,
15474 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
15477 rc = sqlite3_prepare_v2(p->db,
15478 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
15479 " (1,'run','PRAGMA integrity_check','ok')",
15483 raw_printf(stderr, "Error querying the selftest table\n");
15485 sqlite3_finalize(pStmt);
15486 goto meta_command_exit;
15488 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
15489 int tno = sqlite3_column_int(pStmt, 0);
15490 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
15491 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
15492 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
15496 char *zQuote = sqlite3_mprintf("%q", zSql);
15497 printf("%d: %s %s\n", tno, zOp, zSql);
15498 sqlite3_free(zQuote);
15500 if( strcmp(zOp,"memo")==0 ){
15501 utf8_printf(p->out, "%s\n", zSql);
15503 if( strcmp(zOp,"run")==0 ){
15507 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
15510 utf8_printf(p->out, "Result: %s\n", str.z);
15512 if( rc || zErrMsg ){
15515 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
15516 sqlite3_free(zErrMsg);
15517 }else if( strcmp(zAns,str.z)!=0 ){
15520 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
15521 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
15525 utf8_printf(stderr,
15526 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
15530 } /* End loop over rows of content from SELFTEST */
15531 sqlite3_finalize(pStmt);
15532 } /* End loop over k */
15534 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
15537 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
15538 if( nArg<2 || nArg>3 ){
15539 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
15543 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
15544 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
15547 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
15548 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
15552 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
15553 const char *zLike = 0; /* Which table to checksum. 0 means everything */
15554 int i; /* Loop counter */
15555 int bSchema = 0; /* Also hash the schema */
15556 int bSeparate = 0; /* Hash each table separately */
15557 int iSize = 224; /* Hash algorithm to use */
15558 int bDebug = 0; /* Only show the query that would have run */
15559 sqlite3_stmt *pStmt; /* For querying tables names */
15560 char *zSql; /* SQL to be run */
15561 char *zSep; /* Separator */
15562 ShellText sSql; /* Complete SQL for the query to run the hash */
15563 ShellText sQuery; /* Set of queries used to read all content */
15565 for(i=1; i<nArg; i++){
15566 const char *z = azArg[i];
15569 if( z[0]=='-' ) z++;
15570 if( strcmp(z,"schema")==0 ){
15573 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
15574 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
15576 iSize = atoi(&z[5]);
15578 if( strcmp(z,"debug")==0 ){
15582 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15583 azArg[i], azArg[0]);
15584 raw_printf(stderr, "Should be one of: --schema"
15585 " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
15587 goto meta_command_exit;
15590 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
15592 goto meta_command_exit;
15596 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
15600 zSql = "SELECT lower(name) FROM sqlite_master"
15601 " WHERE type='table' AND coalesce(rootpage,0)>1"
15602 " UNION ALL SELECT 'sqlite_master'"
15603 " ORDER BY 1 collate nocase";
15605 zSql = "SELECT lower(name) FROM sqlite_master"
15606 " WHERE type='table' AND coalesce(rootpage,0)>1"
15607 " AND name NOT LIKE 'sqlite_%'"
15608 " ORDER BY 1 collate nocase";
15610 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15613 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
15615 while( SQLITE_ROW==sqlite3_step(pStmt) ){
15616 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
15617 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
15618 if( strncmp(zTab, "sqlite_",7)!=0 ){
15619 appendText(&sQuery,"SELECT * FROM ", 0);
15620 appendText(&sQuery,zTab,'"');
15621 appendText(&sQuery," NOT INDEXED;", 0);
15622 }else if( strcmp(zTab, "sqlite_master")==0 ){
15623 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
15624 " ORDER BY name;", 0);
15625 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
15626 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
15627 " ORDER BY name;", 0);
15628 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
15629 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
15630 " ORDER BY tbl,idx;", 0);
15631 }else if( strcmp(zTab, "sqlite_stat3")==0
15632 || strcmp(zTab, "sqlite_stat4")==0 ){
15633 appendText(&sQuery, "SELECT * FROM ", 0);
15634 appendText(&sQuery, zTab, 0);
15635 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
15637 appendText(&sSql, zSep, 0);
15638 appendText(&sSql, sQuery.z, '\'');
15640 appendText(&sSql, ",", 0);
15641 appendText(&sSql, zTab, '\'');
15644 sqlite3_finalize(pStmt);
15646 zSql = sqlite3_mprintf(
15648 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
15649 " FROM [sha3sum$query]",
15652 zSql = sqlite3_mprintf(
15654 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
15655 " FROM [sha3sum$query]",
15661 utf8_printf(p->out, "%s\n", zSql);
15663 shell_exec(p, zSql, 0);
15665 sqlite3_free(zSql);
15668 #ifndef SQLITE_NOHAVE_SYSTEM
15670 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
15675 raw_printf(stderr, "Usage: .system COMMAND\n");
15677 goto meta_command_exit;
15679 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
15680 for(i=2; i<nArg; i++){
15681 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
15685 sqlite3_free(zCmd);
15686 if( x ) raw_printf(stderr, "System command returns %d\n", x);
15688 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15690 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
15691 static const char *azBool[] = { "off", "on", "trigger", "full"};
15694 raw_printf(stderr, "Usage: .show\n");
15696 goto meta_command_exit;
15698 utf8_printf(p->out, "%12.12s: %s\n","echo",
15699 azBool[ShellHasFlag(p, SHFLG_Echo)]);
15700 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
15701 utf8_printf(p->out, "%12.12s: %s\n","explain",
15702 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
15703 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
15704 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
15705 utf8_printf(p->out, "%12.12s: ", "nullvalue");
15706 output_c_string(p->out, p->nullValue);
15707 raw_printf(p->out, "\n");
15708 utf8_printf(p->out,"%12.12s: %s\n","output",
15709 strlen30(p->outfile) ? p->outfile : "stdout");
15710 utf8_printf(p->out,"%12.12s: ", "colseparator");
15711 output_c_string(p->out, p->colSeparator);
15712 raw_printf(p->out, "\n");
15713 utf8_printf(p->out,"%12.12s: ", "rowseparator");
15714 output_c_string(p->out, p->rowSeparator);
15715 raw_printf(p->out, "\n");
15716 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
15717 utf8_printf(p->out, "%12.12s: ", "width");
15718 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
15719 raw_printf(p->out, "%d ", p->colWidth[i]);
15721 raw_printf(p->out, "\n");
15722 utf8_printf(p->out, "%12.12s: %s\n", "filename",
15723 p->zDbFilename ? p->zDbFilename : "");
15726 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
15728 p->statsOn = (u8)booleanValue(azArg[1]);
15729 }else if( nArg==1 ){
15730 display_stats(p->db, p, 0);
15732 raw_printf(stderr, "Usage: .stats ?on|off?\n");
15737 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
15738 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
15739 || strncmp(azArg[0], "indexes", n)==0) )
15741 sqlite3_stmt *pStmt;
15748 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
15750 sqlite3_finalize(pStmt);
15751 return shellDatabaseError(p->db);
15754 if( nArg>2 && c=='i' ){
15755 /* It is an historical accident that the .indexes command shows an error
15756 ** when called with the wrong number of arguments whereas the .tables
15757 ** command does not. */
15758 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
15760 sqlite3_finalize(pStmt);
15761 goto meta_command_exit;
15763 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
15764 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
15765 if( zDbName==0 ) continue;
15766 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
15767 if( sqlite3_stricmp(zDbName, "main")==0 ){
15768 appendText(&s, "SELECT name FROM ", 0);
15770 appendText(&s, "SELECT ", 0);
15771 appendText(&s, zDbName, '\'');
15772 appendText(&s, "||'.'||name FROM ", 0);
15774 appendText(&s, zDbName, '"');
15775 appendText(&s, ".sqlite_master ", 0);
15777 appendText(&s," WHERE type IN ('table','view')"
15778 " AND name NOT LIKE 'sqlite_%'"
15779 " AND name LIKE ?1", 0);
15781 appendText(&s," WHERE type='index'"
15782 " AND tbl_name LIKE ?1", 0);
15785 rc = sqlite3_finalize(pStmt);
15786 appendText(&s, " ORDER BY 1", 0);
15787 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
15789 if( rc ) return shellDatabaseError(p->db);
15791 /* Run the SQL statement prepared by the above block. Store the results
15792 ** as an array of nul-terminated strings in azResult[]. */
15796 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
15798 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
15800 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15801 if( nRow>=nAlloc ){
15803 int n2 = nAlloc*2 + 10;
15804 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
15805 if( azNew==0 ) shell_out_of_memory();
15809 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
15810 if( 0==azResult[nRow] ) shell_out_of_memory();
15813 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
15814 rc = shellDatabaseError(p->db);
15817 /* Pretty-print the contents of array azResult[] to the output */
15818 if( rc==0 && nRow>0 ){
15819 int len, maxlen = 0;
15821 int nPrintCol, nPrintRow;
15822 for(i=0; i<nRow; i++){
15823 len = strlen30(azResult[i]);
15824 if( len>maxlen ) maxlen = len;
15826 nPrintCol = 80/(maxlen+2);
15827 if( nPrintCol<1 ) nPrintCol = 1;
15828 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
15829 for(i=0; i<nPrintRow; i++){
15830 for(j=i; j<nRow; j+=nPrintRow){
15831 char *zSp = j<nPrintRow ? "" : " ";
15832 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15833 azResult[j] ? azResult[j]:"");
15835 raw_printf(p->out, "\n");
15839 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15840 sqlite3_free(azResult);
15843 /* Begin redirecting output to the file "testcase-out.txt" */
15844 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15846 p->out = output_file_open("testcase-out.txt", 0);
15848 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15851 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15853 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15857 #ifndef SQLITE_UNTESTABLE
15858 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15859 static const struct {
15860 const char *zCtrlName; /* Name of a test-control option */
15861 int ctrlCode; /* Integer code for that option */
15862 const char *zUsage; /* Usage notes */
15864 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
15865 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
15866 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
15867 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
15868 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
15869 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
15870 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
15871 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN" },
15872 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
15873 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
15874 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
15876 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
15878 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
15879 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" },
15880 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
15881 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
15882 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" },
15886 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
15889 const char *zCmd = 0;
15892 zCmd = nArg>=2 ? azArg[1] : "help";
15894 /* The argument can optionally begin with "-" or "--" */
15895 if( zCmd[0]=='-' && zCmd[1] ){
15897 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15900 /* --help lists all test-controls */
15901 if( strcmp(zCmd,"help")==0 ){
15902 utf8_printf(p->out, "Available test-controls:\n");
15903 for(i=0; i<ArraySize(aCtrl); i++){
15904 utf8_printf(p->out, " .testctrl %s %s\n",
15905 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15908 goto meta_command_exit;
15911 /* convert testctrl text option to value. allow any unique prefix
15912 ** of the option name, or a numerical value. */
15913 n2 = strlen30(zCmd);
15914 for(i=0; i<ArraySize(aCtrl); i++){
15915 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15917 testctrl = aCtrl[i].ctrlCode;
15920 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15921 "Use \".testctrl --help\" for help\n", zCmd);
15923 goto meta_command_exit;
15928 utf8_printf(stderr,"Error: unknown test-control: %s\n"
15929 "Use \".testctrl --help\" for help\n", zCmd);
15933 /* sqlite3_test_control(int, db, int) */
15934 case SQLITE_TESTCTRL_OPTIMIZATIONS:
15935 case SQLITE_TESTCTRL_RESERVE:
15937 int opt = (int)strtol(azArg[2], 0, 0);
15938 rc2 = sqlite3_test_control(testctrl, p->db, opt);
15943 /* sqlite3_test_control(int) */
15944 case SQLITE_TESTCTRL_PRNG_SAVE:
15945 case SQLITE_TESTCTRL_PRNG_RESTORE:
15946 case SQLITE_TESTCTRL_PRNG_RESET:
15947 case SQLITE_TESTCTRL_BYTEORDER:
15949 rc2 = sqlite3_test_control(testctrl);
15950 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15954 /* sqlite3_test_control(int, uint) */
15955 case SQLITE_TESTCTRL_PENDING_BYTE:
15957 unsigned int opt = (unsigned int)integerValue(azArg[2]);
15958 rc2 = sqlite3_test_control(testctrl, opt);
15963 /* sqlite3_test_control(int, int) */
15964 case SQLITE_TESTCTRL_ASSERT:
15965 case SQLITE_TESTCTRL_ALWAYS:
15966 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
15968 int opt = booleanValue(azArg[2]);
15969 rc2 = sqlite3_test_control(testctrl, opt);
15974 /* sqlite3_test_control(int, int) */
15975 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15976 case SQLITE_TESTCTRL_NEVER_CORRUPT:
15978 int opt = booleanValue(azArg[2]);
15979 rc2 = sqlite3_test_control(testctrl, opt);
15984 case SQLITE_TESTCTRL_IMPOSTER:
15986 rc2 = sqlite3_test_control(testctrl, p->db,
15988 integerValue(azArg[3]),
15989 integerValue(azArg[4]));
15995 case SQLITE_TESTCTRL_PARSER_COVERAGE:
15997 sqlite3_test_control(testctrl, p->out);
16003 if( isOk==0 && iCtrl>=0 ){
16004 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
16006 }else if( isOk==1 ){
16007 raw_printf(p->out, "%d\n", rc2);
16008 }else if( isOk==2 ){
16009 raw_printf(p->out, "0x%08x\n", rc2);
16012 #endif /* !defined(SQLITE_UNTESTABLE) */
16014 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
16016 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
16019 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
16021 enableTimer = booleanValue(azArg[1]);
16022 if( enableTimer && !HAS_TIMER ){
16023 raw_printf(stderr, "Error: timer not available on this system.\n");
16027 raw_printf(stderr, "Usage: .timer on|off\n");
16032 #ifndef SQLITE_OMIT_TRACE
16033 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
16037 for(jj=1; jj<nArg; jj++){
16038 const char *z = azArg[jj];
16040 if( optionMatch(z, "expanded") ){
16041 p->eTraceType = SHELL_TRACE_EXPANDED;
16043 #ifdef SQLITE_ENABLE_NORMALIZE
16044 else if( optionMatch(z, "normalized") ){
16045 p->eTraceType = SHELL_TRACE_NORMALIZED;
16048 else if( optionMatch(z, "plain") ){
16049 p->eTraceType = SHELL_TRACE_PLAIN;
16051 else if( optionMatch(z, "profile") ){
16052 mType |= SQLITE_TRACE_PROFILE;
16054 else if( optionMatch(z, "row") ){
16055 mType |= SQLITE_TRACE_ROW;
16057 else if( optionMatch(z, "stmt") ){
16058 mType |= SQLITE_TRACE_STMT;
16060 else if( optionMatch(z, "close") ){
16061 mType |= SQLITE_TRACE_CLOSE;
16064 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
16066 goto meta_command_exit;
16069 output_file_close(p->traceOut);
16070 p->traceOut = output_file_open(azArg[1], 0);
16073 if( p->traceOut==0 ){
16074 sqlite3_trace_v2(p->db, 0, 0, 0);
16076 if( mType==0 ) mType = SQLITE_TRACE_STMT;
16077 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
16080 #endif /* !defined(SQLITE_OMIT_TRACE) */
16082 #if SQLITE_USER_AUTHENTICATION
16083 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
16085 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
16087 goto meta_command_exit;
16090 if( strcmp(azArg[1],"login")==0 ){
16092 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
16094 goto meta_command_exit;
16096 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
16098 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
16101 }else if( strcmp(azArg[1],"add")==0 ){
16103 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
16105 goto meta_command_exit;
16107 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
16108 booleanValue(azArg[4]));
16110 raw_printf(stderr, "User-Add failed: %d\n", rc);
16113 }else if( strcmp(azArg[1],"edit")==0 ){
16115 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
16117 goto meta_command_exit;
16119 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
16120 booleanValue(azArg[4]));
16122 raw_printf(stderr, "User-Edit failed: %d\n", rc);
16125 }else if( strcmp(azArg[1],"delete")==0 ){
16127 raw_printf(stderr, "Usage: .user delete USER\n");
16129 goto meta_command_exit;
16131 rc = sqlite3_user_delete(p->db, azArg[2]);
16133 raw_printf(stderr, "User-Delete failed: %d\n", rc);
16137 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
16139 goto meta_command_exit;
16142 #endif /* SQLITE_USER_AUTHENTICATION */
16144 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
16145 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
16146 sqlite3_libversion(), sqlite3_sourceid());
16147 #if SQLITE_HAVE_ZLIB
16148 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
16150 #define CTIMEOPT_VAL_(opt) #opt
16151 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
16152 #if defined(__clang__) && defined(__clang_major__)
16153 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
16154 CTIMEOPT_VAL(__clang_minor__) "."
16155 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
16156 #elif defined(_MSC_VER)
16157 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
16158 #elif defined(__GNUC__) && defined(__VERSION__)
16159 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
16163 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
16164 const char *zDbName = nArg==2 ? azArg[1] : "main";
16165 sqlite3_vfs *pVfs = 0;
16167 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
16169 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
16170 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
16171 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
16172 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
16177 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
16179 sqlite3_vfs *pCurrent = 0;
16181 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
16183 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
16184 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
16185 pVfs==pCurrent ? " <--- CURRENT" : "");
16186 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
16187 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
16188 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
16190 raw_printf(p->out, "-----------------------------------\n");
16195 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
16196 const char *zDbName = nArg==2 ? azArg[1] : "main";
16197 char *zVfsName = 0;
16199 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
16201 utf8_printf(p->out, "%s\n", zVfsName);
16202 sqlite3_free(zVfsName);
16207 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
16208 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
16209 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
16213 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
16215 assert( nArg<=ArraySize(azArg) );
16216 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
16217 p->colWidth[j-1] = (int)integerValue(azArg[j]);
16222 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
16223 " \"%s\". Enter \".help\" for help\n", azArg[0]);
16230 if( p->outCount==0 ) output_reset(p);
16236 ** Return TRUE if a semicolon occurs anywhere in the first N characters
16239 static int line_contains_semicolon(const char *z, int N){
16241 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
16246 ** Test to see if a line consists entirely of whitespace.
16248 static int _all_whitespace(const char *z){
16250 if( IsSpace(z[0]) ) continue;
16251 if( *z=='/' && z[1]=='*' ){
16253 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
16254 if( *z==0 ) return 0;
16258 if( *z=='-' && z[1]=='-' ){
16260 while( *z && *z!='\n' ){ z++; }
16261 if( *z==0 ) return 1;
16270 ** Return TRUE if the line typed in is an SQL command terminator other
16271 ** than a semi-colon. The SQL Server style "go" command is understood
16272 ** as is the Oracle "/".
16274 static int line_is_command_terminator(const char *zLine){
16275 while( IsSpace(zLine[0]) ){ zLine++; };
16276 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
16277 return 1; /* Oracle */
16279 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
16280 && _all_whitespace(&zLine[2]) ){
16281 return 1; /* SQL Server */
16287 ** We need a default sqlite3_complete() implementation to use in case
16288 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
16289 ** any arbitrary text is a complete SQL statement. This is not very
16290 ** user-friendly, but it does seem to work.
16292 #ifdef SQLITE_OMIT_COMPLETE
16293 #define sqlite3_complete(x) 1
16297 ** Return true if zSql is a complete SQL statement. Return false if it
16298 ** ends in the middle of a string literal or C-style comment.
16300 static int line_is_complete(char *zSql, int nSql){
16302 if( zSql==0 ) return 1;
16305 rc = sqlite3_complete(zSql);
16311 ** Run a single line of SQL. Return the number of errors.
16313 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
16318 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
16319 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
16321 rc = shell_exec(p, zSql, &zErrMsg);
16323 if( rc || zErrMsg ){
16325 if( in!=0 || !stdin_is_interactive ){
16326 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
16327 "Error: near line %d:", startline);
16329 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
16332 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
16333 sqlite3_free(zErrMsg);
16336 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
16339 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
16340 raw_printf(p->out, "changes: %3d total_changes: %d\n",
16341 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
16348 ** Read input from *in and process it. If *in==0 then input
16349 ** is interactive - the user is typing it it. Otherwise, input
16350 ** is coming from a file or device. A prompt is issued and history
16351 ** is saved only if input is interactive. An interrupt signal will
16352 ** cause this routine to exit immediately, unless input is interactive.
16354 ** Return the number of errors.
16356 static int process_input(ShellState *p){
16357 char *zLine = 0; /* A single input line */
16358 char *zSql = 0; /* Accumulated SQL text */
16359 int nLine; /* Length of current line */
16360 int nSql = 0; /* Bytes of zSql[] used */
16361 int nAlloc = 0; /* Allocated zSql[] space */
16362 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
16363 int rc; /* Error code */
16364 int errCnt = 0; /* Number of errors seen */
16365 int startline = 0; /* Line number for start of current input */
16368 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
16370 zLine = one_input_line(p->in, zLine, nSql>0);
16373 if( p->in==0 && stdin_is_interactive ) printf("\n");
16376 if( seenInterrupt ){
16377 if( p->in!=0 ) break;
16381 if( nSql==0 && _all_whitespace(zLine) ){
16382 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16385 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
16386 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16387 if( zLine[0]=='.' ){
16388 rc = do_meta_command(zLine, p);
16389 if( rc==2 ){ /* exit requested */
16397 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
16398 memcpy(zLine,";",2);
16400 nLine = strlen30(zLine);
16401 if( nSql+nLine+2>=nAlloc ){
16402 nAlloc = nSql+nLine+100;
16403 zSql = realloc(zSql, nAlloc);
16404 if( zSql==0 ) shell_out_of_memory();
16409 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
16410 assert( nAlloc>0 && zSql!=0 );
16411 memcpy(zSql, zLine+i, nLine+1-i);
16412 startline = p->lineno;
16415 zSql[nSql++] = '\n';
16416 memcpy(zSql+nSql, zLine, nLine+1);
16419 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
16420 && sqlite3_complete(zSql) ){
16421 errCnt += runOneSqlLine(p, zSql, p->in, startline);
16429 }else if( nSql && _all_whitespace(zSql) ){
16430 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
16434 if( nSql && !_all_whitespace(zSql) ){
16435 errCnt += runOneSqlLine(p, zSql, p->in, startline);
16443 ** Return a pathname which is the user's home directory. A
16444 ** 0 return indicates an error of some kind.
16446 static char *find_home_dir(int clearFlag){
16447 static char *home_dir = NULL;
16453 if( home_dir ) return home_dir;
16455 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
16456 && !defined(__RTP__) && !defined(_WRS_KERNEL)
16458 struct passwd *pwent;
16459 uid_t uid = getuid();
16460 if( (pwent=getpwuid(uid)) != NULL) {
16461 home_dir = pwent->pw_dir;
16466 #if defined(_WIN32_WCE)
16467 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
16472 #if defined(_WIN32) || defined(WIN32)
16474 home_dir = getenv("USERPROFILE");
16479 home_dir = getenv("HOME");
16482 #if defined(_WIN32) || defined(WIN32)
16484 char *zDrive, *zPath;
16486 zDrive = getenv("HOMEDRIVE");
16487 zPath = getenv("HOMEPATH");
16488 if( zDrive && zPath ){
16489 n = strlen30(zDrive) + strlen30(zPath) + 1;
16490 home_dir = malloc( n );
16491 if( home_dir==0 ) return 0;
16492 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
16499 #endif /* !_WIN32_WCE */
16502 int n = strlen30(home_dir) + 1;
16503 char *z = malloc( n );
16504 if( z ) memcpy(z, home_dir, n);
16512 ** Read input from the file given by sqliterc_override. Or if that
16513 ** parameter is NULL, take input from ~/.sqliterc
16515 ** Returns the number of errors.
16517 static void process_sqliterc(
16518 ShellState *p, /* Configuration data */
16519 const char *sqliterc_override /* Name of config file. NULL to use default */
16521 char *home_dir = NULL;
16522 const char *sqliterc = sqliterc_override;
16524 FILE *inSaved = p->in;
16525 int savedLineno = p->lineno;
16527 if (sqliterc == NULL) {
16528 home_dir = find_home_dir(0);
16530 raw_printf(stderr, "-- warning: cannot find home directory;"
16531 " cannot read ~/.sqliterc\n");
16534 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
16537 p->in = fopen(sqliterc,"rb");
16539 if( stdin_is_interactive ){
16540 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
16546 p->lineno = savedLineno;
16547 sqlite3_free(zBuf);
16551 ** Show available command line options
16553 static const char zOptions[] =
16554 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
16555 " -A ARGS... run \".archive ARGS\" and exit\n"
16557 " -append append the database to the end of the file\n"
16558 " -ascii set output mode to 'ascii'\n"
16559 " -bail stop after hitting an error\n"
16560 " -batch force batch I/O\n"
16561 " -column set output mode to 'column'\n"
16562 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
16563 " -csv set output mode to 'csv'\n"
16564 #if defined(SQLITE_ENABLE_DESERIALIZE)
16565 " -deserialize open the database using sqlite3_deserialize()\n"
16567 " -echo print commands before execution\n"
16568 " -init FILENAME read/process named file\n"
16569 " -[no]header turn headers on or off\n"
16570 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16571 " -heap SIZE Size of heap for memsys3 or memsys5\n"
16573 " -help show this message\n"
16574 " -html set output mode to HTML\n"
16575 " -interactive force interactive I/O\n"
16576 " -line set output mode to 'line'\n"
16577 " -list set output mode to 'list'\n"
16578 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
16579 #if defined(SQLITE_ENABLE_DESERIALIZE)
16580 " -maxsize N maximum size for a --deserialize database\n"
16582 " -memtrace trace all memory allocations and deallocations\n"
16583 " -mmap N default mmap size set to N\n"
16584 #ifdef SQLITE_ENABLE_MULTIPLEX
16585 " -multiplex enable the multiplexor VFS\n"
16587 " -newline SEP set output row separator. Default: '\\n'\n"
16588 " -nullvalue TEXT set text string for NULL values. Default ''\n"
16589 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
16590 " -quote set output mode to 'quote'\n"
16591 " -readonly open the database read-only\n"
16592 " -separator SEP set output column separator. Default: '|'\n"
16593 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16594 " -sorterref SIZE sorter references threshold size\n"
16596 " -stats print memory stats before each finalize\n"
16597 " -version show SQLite version\n"
16598 " -vfs NAME use NAME as the default VFS\n"
16599 #ifdef SQLITE_ENABLE_VFSTRACE
16600 " -vfstrace enable tracing of all VFS calls\n"
16602 #ifdef SQLITE_HAVE_ZLIB
16603 " -zip open the file as a ZIP Archive\n"
16606 static void usage(int showDetail){
16607 utf8_printf(stderr,
16608 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
16609 "FILENAME is the name of an SQLite database. A new database is created\n"
16610 "if the file does not previously exist.\n", Argv0);
16612 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
16614 raw_printf(stderr, "Use the -help option for additional information\n");
16620 ** Internal check: Verify that the SQLite is uninitialized. Print a
16621 ** error message if it is initialized.
16623 static void verify_uninitialized(void){
16624 if( sqlite3_config(-1)==SQLITE_MISUSE ){
16625 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
16626 " initialization.\n");
16631 ** Initialize the state information in data
16633 static void main_init(ShellState *data) {
16634 memset(data, 0, sizeof(*data));
16635 data->normalMode = data->cMode = data->mode = MODE_List;
16636 data->autoExplain = 1;
16637 memcpy(data->colSeparator,SEP_Column, 2);
16638 memcpy(data->rowSeparator,SEP_Row, 2);
16639 data->showHeader = 0;
16640 data->shellFlgs = SHFLG_Lookaside;
16641 verify_uninitialized();
16642 sqlite3_config(SQLITE_CONFIG_URI, 1);
16643 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
16644 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
16645 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
16646 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
16650 ** Output text to the console in a font that attracts extra attention.
16653 static void printBold(const char *zText){
16654 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
16655 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
16656 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
16657 SetConsoleTextAttribute(out,
16658 FOREGROUND_RED|FOREGROUND_INTENSITY
16660 printf("%s", zText);
16661 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
16664 static void printBold(const char *zText){
16665 printf("\033[1m%s\033[0m", zText);
16670 ** Get the argument to an --option. Throw an error and die if no argument
16673 static char *cmdline_option_value(int argc, char **argv, int i){
16675 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
16676 argv[0], argv[argc-1]);
16682 #ifndef SQLITE_SHELL_IS_UTF8
16683 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
16684 # define SQLITE_SHELL_IS_UTF8 (0)
16686 # define SQLITE_SHELL_IS_UTF8 (1)
16690 #if SQLITE_SHELL_IS_UTF8
16691 int SQLITE_CDECL main(int argc, char **argv){
16693 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
16698 const char *zInitFile = 0;
16701 int warnInmemoryDb = 0;
16705 const char *zVfs = 0; /* Value of -vfs command-line option */
16706 #if !SQLITE_SHELL_IS_UTF8
16707 char **argvToFree = 0;
16708 int argcToFree = 0;
16711 setBinaryMode(stdin, 0);
16712 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
16713 stdin_is_interactive = isatty(0);
16714 stdout_is_console = isatty(1);
16716 #if !defined(_WIN32_WCE)
16717 if( getenv("SQLITE_DEBUG_BREAK") ){
16718 if( isatty(0) && isatty(2) ){
16720 "attach debugger to process %d and press any key to continue.\n",
16724 #if defined(_WIN32) || defined(WIN32)
16726 #elif defined(SIGTRAP)
16733 #if USE_SYSTEM_SQLITE+0!=1
16734 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
16735 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
16736 sqlite3_sourceid(), SQLITE_SOURCE_ID);
16742 /* On Windows, we must translate command-line arguments into UTF-8.
16743 ** The SQLite memory allocator subsystem has to be enabled in order to
16744 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
16745 ** subsequent sqlite3_config() calls will work. So copy all results into
16746 ** memory that does not come from the SQLite memory allocator.
16748 #if !SQLITE_SHELL_IS_UTF8
16749 sqlite3_initialize();
16750 argvToFree = malloc(sizeof(argv[0])*argc*2);
16752 argv = argvToFree + argc;
16753 if( argv==0 ) shell_out_of_memory();
16754 for(i=0; i<argc; i++){
16755 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
16757 if( z==0 ) shell_out_of_memory();
16758 n = (int)strlen(z);
16759 argv[i] = malloc( n+1 );
16760 if( argv[i]==0 ) shell_out_of_memory();
16761 memcpy(argv[i], z, n+1);
16762 argvToFree[i] = argv[i];
16765 sqlite3_shutdown();
16768 assert( argc>=1 && argv && argv[0] );
16771 /* Make sure we have a valid signal handler early, before anything
16775 signal(SIGINT, interrupt_handler);
16776 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
16777 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
16780 #ifdef SQLITE_SHELL_DBNAME_PROC
16782 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
16783 ** of a C-function that will provide the name of the database file. Use
16784 ** this compile-time option to embed this shell program in larger
16785 ** applications. */
16786 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
16787 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
16788 warnInmemoryDb = 0;
16792 /* Do an initial pass through the command-line argument to locate
16793 ** the name of the database file, the name of the initialization file,
16794 ** the size of the alternative malloc heap,
16795 ** and the first command to execute.
16797 verify_uninitialized();
16798 for(i=1; i<argc; i++){
16802 if( data.zDbFilename==0 ){
16803 data.zDbFilename = z;
16805 /* Excesss arguments are interpreted as SQL (or dot-commands) and
16806 ** mean that nothing is read from stdin */
16809 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
16810 if( azCmd==0 ) shell_out_of_memory();
16814 if( z[1]=='-' ) z++;
16815 if( strcmp(z,"-separator")==0
16816 || strcmp(z,"-nullvalue")==0
16817 || strcmp(z,"-newline")==0
16818 || strcmp(z,"-cmd")==0
16820 (void)cmdline_option_value(argc, argv, ++i);
16821 }else if( strcmp(z,"-init")==0 ){
16822 zInitFile = cmdline_option_value(argc, argv, ++i);
16823 }else if( strcmp(z,"-batch")==0 ){
16824 /* Need to check for batch mode here to so we can avoid printing
16825 ** informational messages (like from process_sqliterc) before
16826 ** we do the actual processing of arguments later in a second pass.
16828 stdin_is_interactive = 0;
16829 }else if( strcmp(z,"-heap")==0 ){
16830 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16832 sqlite3_int64 szHeap;
16834 zSize = cmdline_option_value(argc, argv, ++i);
16835 szHeap = integerValue(zSize);
16836 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
16837 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
16839 (void)cmdline_option_value(argc, argv, ++i);
16841 }else if( strcmp(z,"-pagecache")==0 ){
16843 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16844 if( sz>70000 ) sz = 70000;
16846 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16847 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
16848 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
16849 data.shellFlgs |= SHFLG_Pagecache;
16850 }else if( strcmp(z,"-lookaside")==0 ){
16852 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16854 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16856 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
16857 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
16858 #ifdef SQLITE_ENABLE_VFSTRACE
16859 }else if( strcmp(z,"-vfstrace")==0 ){
16860 extern int vfstrace_register(
16861 const char *zTraceName,
16862 const char *zOldVfsName,
16863 int (*xOut)(const char*,void*),
16867 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
16869 #ifdef SQLITE_ENABLE_MULTIPLEX
16870 }else if( strcmp(z,"-multiplex")==0 ){
16871 extern int sqlite3_multiple_initialize(const char*,int);
16872 sqlite3_multiplex_initialize(0, 1);
16874 }else if( strcmp(z,"-mmap")==0 ){
16875 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16876 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
16877 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16878 }else if( strcmp(z,"-sorterref")==0 ){
16879 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16880 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
16882 }else if( strcmp(z,"-vfs")==0 ){
16883 zVfs = cmdline_option_value(argc, argv, ++i);
16884 #ifdef SQLITE_HAVE_ZLIB
16885 }else if( strcmp(z,"-zip")==0 ){
16886 data.openMode = SHELL_OPEN_ZIPFILE;
16888 }else if( strcmp(z,"-append")==0 ){
16889 data.openMode = SHELL_OPEN_APPENDVFS;
16890 #ifdef SQLITE_ENABLE_DESERIALIZE
16891 }else if( strcmp(z,"-deserialize")==0 ){
16892 data.openMode = SHELL_OPEN_DESERIALIZE;
16893 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16894 data.szMax = integerValue(argv[++i]);
16896 }else if( strcmp(z,"-readonly")==0 ){
16897 data.openMode = SHELL_OPEN_READONLY;
16898 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16899 }else if( strncmp(z, "-A",2)==0 ){
16900 /* All remaining command-line arguments are passed to the ".archive"
16901 ** command, so ignore them */
16904 }else if( strcmp(z, "-memtrace")==0 ){
16905 sqlite3MemTraceActivate(stderr);
16908 verify_uninitialized();
16911 #ifdef SQLITE_SHELL_INIT_PROC
16913 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16914 ** of a C-function that will perform initialization actions on SQLite that
16915 ** occur just before or after sqlite3_initialize(). Use this compile-time
16916 ** option to embed this shell program in larger applications. */
16917 extern void SQLITE_SHELL_INIT_PROC(void);
16918 SQLITE_SHELL_INIT_PROC();
16921 /* All the sqlite3_config() calls have now been made. So it is safe
16922 ** to call sqlite3_initialize() and process any command line -vfs option. */
16923 sqlite3_initialize();
16927 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16929 sqlite3_vfs_register(pVfs, 1);
16931 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16936 if( data.zDbFilename==0 ){
16937 #ifndef SQLITE_OMIT_MEMORYDB
16938 data.zDbFilename = ":memory:";
16939 warnInmemoryDb = argc==1;
16941 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16946 sqlite3_appendvfs_init(0,0,0);
16948 /* Go ahead and open the database file if it already exists. If the
16949 ** file does not exist, delay opening it. This prevents empty database
16950 ** files from being created if a user mistypes the database name argument
16951 ** to the sqlite command-line tool.
16953 if( access(data.zDbFilename, 0)==0 ){
16957 /* Process the initialization file if there is one. If no -init option
16958 ** is given on the command line, look for a file named ~/.sqliterc and
16959 ** try to process it.
16961 process_sqliterc(&data,zInitFile);
16963 /* Make a second pass through the command-line argument and set
16964 ** options. This second pass is delayed until after the initialization
16965 ** file is processed so that the command-line arguments will override
16966 ** settings in the initialization file.
16968 for(i=1; i<argc; i++){
16970 if( z[0]!='-' ) continue;
16971 if( z[1]=='-' ){ z++; }
16972 if( strcmp(z,"-init")==0 ){
16974 }else if( strcmp(z,"-html")==0 ){
16975 data.mode = MODE_Html;
16976 }else if( strcmp(z,"-list")==0 ){
16977 data.mode = MODE_List;
16978 }else if( strcmp(z,"-quote")==0 ){
16979 data.mode = MODE_Quote;
16980 }else if( strcmp(z,"-line")==0 ){
16981 data.mode = MODE_Line;
16982 }else if( strcmp(z,"-column")==0 ){
16983 data.mode = MODE_Column;
16984 }else if( strcmp(z,"-csv")==0 ){
16985 data.mode = MODE_Csv;
16986 memcpy(data.colSeparator,",",2);
16987 #ifdef SQLITE_HAVE_ZLIB
16988 }else if( strcmp(z,"-zip")==0 ){
16989 data.openMode = SHELL_OPEN_ZIPFILE;
16991 }else if( strcmp(z,"-append")==0 ){
16992 data.openMode = SHELL_OPEN_APPENDVFS;
16993 #ifdef SQLITE_ENABLE_DESERIALIZE
16994 }else if( strcmp(z,"-deserialize")==0 ){
16995 data.openMode = SHELL_OPEN_DESERIALIZE;
16996 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16997 data.szMax = integerValue(argv[++i]);
16999 }else if( strcmp(z,"-readonly")==0 ){
17000 data.openMode = SHELL_OPEN_READONLY;
17001 }else if( strcmp(z,"-ascii")==0 ){
17002 data.mode = MODE_Ascii;
17003 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
17005 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
17007 }else if( strcmp(z,"-separator")==0 ){
17008 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
17009 "%s",cmdline_option_value(argc,argv,++i));
17010 }else if( strcmp(z,"-newline")==0 ){
17011 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
17012 "%s",cmdline_option_value(argc,argv,++i));
17013 }else if( strcmp(z,"-nullvalue")==0 ){
17014 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
17015 "%s",cmdline_option_value(argc,argv,++i));
17016 }else if( strcmp(z,"-header")==0 ){
17017 data.showHeader = 1;
17018 }else if( strcmp(z,"-noheader")==0 ){
17019 data.showHeader = 0;
17020 }else if( strcmp(z,"-echo")==0 ){
17021 ShellSetFlag(&data, SHFLG_Echo);
17022 }else if( strcmp(z,"-eqp")==0 ){
17023 data.autoEQP = AUTOEQP_on;
17024 }else if( strcmp(z,"-eqpfull")==0 ){
17025 data.autoEQP = AUTOEQP_full;
17026 }else if( strcmp(z,"-stats")==0 ){
17028 }else if( strcmp(z,"-scanstats")==0 ){
17029 data.scanstatsOn = 1;
17030 }else if( strcmp(z,"-backslash")==0 ){
17031 /* Undocumented command-line option: -backslash
17032 ** Causes C-style backslash escapes to be evaluated in SQL statements
17033 ** prior to sending the SQL into SQLite. Useful for injecting
17034 ** crazy bytes in the middle of SQL statements for testing and debugging.
17036 ShellSetFlag(&data, SHFLG_Backslash);
17037 }else if( strcmp(z,"-bail")==0 ){
17039 }else if( strcmp(z,"-version")==0 ){
17040 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
17042 }else if( strcmp(z,"-interactive")==0 ){
17043 stdin_is_interactive = 1;
17044 }else if( strcmp(z,"-batch")==0 ){
17045 stdin_is_interactive = 0;
17046 }else if( strcmp(z,"-heap")==0 ){
17048 }else if( strcmp(z,"-pagecache")==0 ){
17050 }else if( strcmp(z,"-lookaside")==0 ){
17052 }else if( strcmp(z,"-mmap")==0 ){
17054 }else if( strcmp(z,"-memtrace")==0 ){
17056 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
17057 }else if( strcmp(z,"-sorterref")==0 ){
17060 }else if( strcmp(z,"-vfs")==0 ){
17062 #ifdef SQLITE_ENABLE_VFSTRACE
17063 }else if( strcmp(z,"-vfstrace")==0 ){
17066 #ifdef SQLITE_ENABLE_MULTIPLEX
17067 }else if( strcmp(z,"-multiplex")==0 ){
17070 }else if( strcmp(z,"-help")==0 ){
17072 }else if( strcmp(z,"-cmd")==0 ){
17073 /* Run commands that follow -cmd first and separately from commands
17074 ** that simply appear on the command-line. This seems goofy. It would
17075 ** be better if all commands ran in the order that they appear. But
17076 ** we retain the goofy behavior for historical compatibility. */
17077 if( i==argc-1 ) break;
17078 z = cmdline_option_value(argc,argv,++i);
17080 rc = do_meta_command(z, &data);
17081 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
17084 rc = shell_exec(&data, z, &zErrMsg);
17086 utf8_printf(stderr,"Error: %s\n", zErrMsg);
17087 if( bail_on_error ) return rc!=0 ? rc : 1;
17089 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
17090 if( bail_on_error ) return rc;
17093 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17094 }else if( strncmp(z, "-A", 2)==0 ){
17096 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
17097 " with \"%s\"\n", z);
17100 open_db(&data, OPEN_DB_ZIPFILE);
17103 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
17105 arDotCommand(&data, 1, argv+i, argc-i);
17111 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
17112 raw_printf(stderr,"Use -help for a list of options.\n");
17115 data.cMode = data.mode;
17119 /* Run all arguments that do not begin with '-' as if they were separate
17120 ** command-line inputs, except for the argToSkip argument which contains
17121 ** the database filename.
17123 for(i=0; i<nCmd; i++){
17124 if( azCmd[i][0]=='.' ){
17125 rc = do_meta_command(azCmd[i], &data);
17126 if( rc ) return rc==2 ? 0 : rc;
17129 rc = shell_exec(&data, azCmd[i], &zErrMsg);
17131 utf8_printf(stderr,"Error: %s\n", zErrMsg);
17132 return rc!=0 ? rc : 1;
17134 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
17141 /* Run commands received from standard input
17143 if( stdin_is_interactive ){
17148 "SQLite version %s %.19s\n" /*extra-version-info*/
17149 "Enter \".help\" for usage hints.\n",
17150 sqlite3_libversion(), sqlite3_sourceid()
17152 if( warnInmemoryDb ){
17153 printf("Connected to a ");
17154 printBold("transient in-memory database");
17155 printf(".\nUse \".open FILENAME\" to reopen on a "
17156 "persistent database.\n");
17158 zHistory = getenv("SQLITE_HISTORY");
17160 zHistory = strdup(zHistory);
17161 }else if( (zHome = find_home_dir(0))!=0 ){
17162 nHistory = strlen30(zHome) + 20;
17163 if( (zHistory = malloc(nHistory))!=0 ){
17164 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
17167 if( zHistory ){ shell_read_history(zHistory); }
17168 #if HAVE_READLINE || HAVE_EDITLINE
17169 rl_attempted_completion_function = readline_completion;
17170 #elif HAVE_LINENOISE
17171 linenoiseSetCompletionCallback(linenoise_completion);
17174 rc = process_input(&data);
17176 shell_stifle_history(2000);
17177 shell_write_history(zHistory);
17182 rc = process_input(&data);
17185 set_table_name(&data, 0);
17187 session_close_all(&data);
17190 sqlite3_free(data.zFreeOnClose);
17192 output_reset(&data);
17193 data.doXdgOpen = 0;
17194 clearTempFile(&data);
17195 #if !SQLITE_SHELL_IS_UTF8
17196 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
17199 /* Clear the global data structure so that valgrind will detect memory
17201 memset(&data, 0, sizeof(data));