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 );
2182 sqlite3_result_error_nomem(ctx);
2186 if( 1==fread(pBuf, nIn, 1, 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 to (mode&0777).
2323 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2324 ** SQLITE_OK is returned if the directory is successfully created, or
2325 ** SQLITE_ERROR otherwise.
2327 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, mode & 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, mode)==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;
10434 ** Run a prepared statement
10436 static void exec_prepared_stmt(
10437 ShellState *pArg, /* Pointer to ShellState */
10438 sqlite3_stmt *pStmt /* Statment to run */
10442 /* perform the first step. this will tell us if we
10443 ** have a result set or not and how wide it is.
10445 rc = sqlite3_step(pStmt);
10446 /* if we have a result set... */
10447 if( SQLITE_ROW == rc ){
10448 /* allocate space for col name ptr, value ptr, and type */
10449 int nCol = sqlite3_column_count(pStmt);
10450 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
10454 char **azCols = (char **)pData; /* Names of result columns */
10455 char **azVals = &azCols[nCol]; /* Results */
10456 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
10458 assert(sizeof(int) <= sizeof(char *));
10459 /* save off ptrs to column names */
10460 for(i=0; i<nCol; i++){
10461 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
10464 /* extract the data and data types */
10465 for(i=0; i<nCol; i++){
10466 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
10467 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
10470 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
10472 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
10474 break; /* from for */
10478 /* if data and types extracted successfully... */
10479 if( SQLITE_ROW == rc ){
10480 /* call the supplied callback with the result row data */
10481 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
10484 rc = sqlite3_step(pStmt);
10487 } while( SQLITE_ROW == rc );
10488 sqlite3_free(pData);
10493 #ifndef SQLITE_OMIT_VIRTUALTABLE
10495 ** This function is called to process SQL if the previous shell command
10496 ** was ".expert". It passes the SQL in the second argument directly to
10497 ** the sqlite3expert object.
10499 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10500 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10501 ** an English language error message. It is the responsibility of the
10502 ** caller to eventually free this buffer using sqlite3_free().
10504 static int expertHandleSQL(
10505 ShellState *pState,
10509 assert( pState->expert.pExpert );
10510 assert( pzErr==0 || *pzErr==0 );
10511 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
10515 ** This function is called either to silently clean up the object
10516 ** created by the ".expert" command (if bCancel==1), or to generate a
10517 ** report from it and then clean it up (if bCancel==0).
10519 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
10520 ** code. In this case, (*pzErr) may be set to point to a buffer containing
10521 ** an English language error message. It is the responsibility of the
10522 ** caller to eventually free this buffer using sqlite3_free().
10524 static int expertFinish(
10525 ShellState *pState,
10529 int rc = SQLITE_OK;
10530 sqlite3expert *p = pState->expert.pExpert;
10532 assert( bCancel || pzErr==0 || *pzErr==0 );
10534 FILE *out = pState->out;
10535 int bVerbose = pState->expert.bVerbose;
10537 rc = sqlite3_expert_analyze(p, pzErr);
10538 if( rc==SQLITE_OK ){
10539 int nQuery = sqlite3_expert_count(p);
10543 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
10544 raw_printf(out, "-- Candidates -----------------------------\n");
10545 raw_printf(out, "%s\n", zCand);
10547 for(i=0; i<nQuery; i++){
10548 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
10549 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
10550 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
10551 if( zIdx==0 ) zIdx = "(no new indexes)\n";
10553 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
10554 raw_printf(out, "%s\n\n", zSql);
10556 raw_printf(out, "%s\n", zIdx);
10557 raw_printf(out, "%s\n", zEQP);
10561 sqlite3_expert_destroy(p);
10562 pState->expert.pExpert = 0;
10567 ** Implementation of ".expert" dot command.
10569 static int expertDotCommand(
10570 ShellState *pState, /* Current shell tool state */
10571 char **azArg, /* Array of arguments passed to dot command */
10572 int nArg /* Number of entries in azArg[] */
10574 int rc = SQLITE_OK;
10579 assert( pState->expert.pExpert==0 );
10580 memset(&pState->expert, 0, sizeof(ExpertInfo));
10582 for(i=1; rc==SQLITE_OK && i<nArg; i++){
10583 char *z = azArg[i];
10585 if( z[0]=='-' && z[1]=='-' ) z++;
10587 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
10588 pState->expert.bVerbose = 1;
10590 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
10592 raw_printf(stderr, "option requires an argument: %s\n", z);
10595 iSample = (int)integerValue(azArg[++i]);
10596 if( iSample<0 || iSample>100 ){
10597 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
10603 raw_printf(stderr, "unknown option: %s\n", z);
10608 if( rc==SQLITE_OK ){
10609 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
10610 if( pState->expert.pExpert==0 ){
10611 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
10614 sqlite3_expert_config(
10615 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
10622 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10625 ** Execute a statement or set of statements. Print
10626 ** any result rows/columns depending on the current mode
10627 ** set via the supplied callback.
10629 ** This is very similar to SQLite's built-in sqlite3_exec()
10630 ** function except it takes a slightly different callback
10631 ** and callback data argument.
10633 static int shell_exec(
10634 ShellState *pArg, /* Pointer to ShellState */
10635 const char *zSql, /* SQL to be evaluated */
10636 char **pzErrMsg /* Error msg written here */
10638 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
10639 int rc = SQLITE_OK; /* Return Code */
10641 const char *zLeftover; /* Tail of unprocessed SQL */
10642 sqlite3 *db = pArg->db;
10648 #ifndef SQLITE_OMIT_VIRTUALTABLE
10649 if( pArg->expert.pExpert ){
10650 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
10651 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
10655 while( zSql[0] && (SQLITE_OK == rc) ){
10656 static const char *zStmtSql;
10657 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
10658 if( SQLITE_OK != rc ){
10660 *pzErrMsg = save_err_msg(db);
10664 /* this happens for a comment or white-space */
10666 while( IsSpace(zSql[0]) ) zSql++;
10669 zStmtSql = sqlite3_sql(pStmt);
10670 if( zStmtSql==0 ) zStmtSql = "";
10671 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
10673 /* save off the prepared statment handle and reset row count */
10675 pArg->pStmt = pStmt;
10679 /* echo the sql statement if echo on */
10680 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
10681 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
10684 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
10685 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
10686 sqlite3_stmt *pExplain;
10688 int triggerEQP = 0;
10689 disable_debug_trace_modes();
10690 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
10691 if( pArg->autoEQP>=AUTOEQP_trigger ){
10692 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
10694 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
10695 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10696 if( rc==SQLITE_OK ){
10697 while( sqlite3_step(pExplain)==SQLITE_ROW ){
10698 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
10699 int iEqpId = sqlite3_column_int(pExplain, 0);
10700 int iParentId = sqlite3_column_int(pExplain, 1);
10701 if( zEQPLine[0]=='-' ) eqp_render(pArg);
10702 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
10706 sqlite3_finalize(pExplain);
10707 sqlite3_free(zEQP);
10708 if( pArg->autoEQP>=AUTOEQP_full ){
10709 /* Also do an EXPLAIN for ".eqp full" mode */
10710 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
10711 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
10712 if( rc==SQLITE_OK ){
10713 pArg->cMode = MODE_Explain;
10714 explain_data_prepare(pArg, pExplain);
10715 exec_prepared_stmt(pArg, pExplain);
10716 explain_data_delete(pArg);
10718 sqlite3_finalize(pExplain);
10719 sqlite3_free(zEQP);
10721 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
10722 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
10723 /* Reprepare pStmt before reactiving trace modes */
10724 sqlite3_finalize(pStmt);
10725 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
10726 if( pArg ) pArg->pStmt = pStmt;
10728 restore_debug_trace_modes();
10732 pArg->cMode = pArg->mode;
10733 if( pArg->autoExplain ){
10734 if( sqlite3_column_count(pStmt)==8
10735 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
10737 pArg->cMode = MODE_Explain;
10739 if( sqlite3_column_count(pStmt)==4
10740 && sqlite3_strlike("EXPLAIN QUERY PLAN%", zStmtSql,0)==0 ){
10741 pArg->cMode = MODE_EQP;
10745 /* If the shell is currently in ".explain" mode, gather the extra
10746 ** data required to add indents to the output.*/
10747 if( pArg->cMode==MODE_Explain ){
10748 explain_data_prepare(pArg, pStmt);
10752 exec_prepared_stmt(pArg, pStmt);
10753 explain_data_delete(pArg);
10756 /* print usage stats if stats on */
10757 if( pArg && pArg->statsOn ){
10758 display_stats(db, pArg, 0);
10761 /* print loop-counters if required */
10762 if( pArg && pArg->scanstatsOn ){
10763 display_scanstats(db, pArg);
10766 /* Finalize the statement just executed. If this fails, save a
10767 ** copy of the error message. Otherwise, set zSql to point to the
10768 ** next statement to execute. */
10769 rc2 = sqlite3_finalize(pStmt);
10770 if( rc!=SQLITE_NOMEM ) rc = rc2;
10771 if( rc==SQLITE_OK ){
10773 while( IsSpace(zSql[0]) ) zSql++;
10774 }else if( pzErrMsg ){
10775 *pzErrMsg = save_err_msg(db);
10778 /* clear saved stmt handle */
10780 pArg->pStmt = NULL;
10789 ** Release memory previously allocated by tableColumnList().
10791 static void freeColumnList(char **azCol){
10793 for(i=1; azCol[i]; i++){
10794 sqlite3_free(azCol[i]);
10796 /* azCol[0] is a static string */
10797 sqlite3_free(azCol);
10801 ** Return a list of pointers to strings which are the names of all
10802 ** columns in table zTab. The memory to hold the names is dynamically
10803 ** allocated and must be released by the caller using a subsequent call
10804 ** to freeColumnList().
10806 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
10807 ** value that needs to be preserved, then azCol[0] is filled in with the
10808 ** name of the rowid column.
10810 ** The first regular column in the table is azCol[1]. The list is terminated
10811 ** by an entry with azCol[i]==0.
10813 static char **tableColumnList(ShellState *p, const char *zTab){
10815 sqlite3_stmt *pStmt;
10819 int nPK = 0; /* Number of PRIMARY KEY columns seen */
10820 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
10821 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
10824 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
10825 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10826 sqlite3_free(zSql);
10828 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10829 if( nCol>=nAlloc-2 ){
10830 nAlloc = nAlloc*2 + nCol + 10;
10831 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
10832 if( azCol==0 ) shell_out_of_memory();
10834 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
10835 if( sqlite3_column_int(pStmt, 5) ){
10838 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
10847 sqlite3_finalize(pStmt);
10848 if( azCol==0 ) return 0;
10852 /* The decision of whether or not a rowid really needs to be preserved
10853 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
10854 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
10855 ** rowids on tables where the rowid is inaccessible because there are other
10856 ** columns in the table named "rowid", "_rowid_", and "oid".
10858 if( preserveRowid && isIPK ){
10859 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
10860 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
10861 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
10862 ** ROWID aliases. To distinguish these cases, check to see if
10863 ** there is a "pk" entry in "PRAGMA index_list". There will be
10864 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
10866 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
10867 " WHERE origin='pk'", zTab);
10868 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
10869 sqlite3_free(zSql);
10871 freeColumnList(azCol);
10874 rc = sqlite3_step(pStmt);
10875 sqlite3_finalize(pStmt);
10876 preserveRowid = rc==SQLITE_ROW;
10878 if( preserveRowid ){
10879 /* Only preserve the rowid if we can find a name to use for the
10881 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
10883 for(j=0; j<3; j++){
10884 for(i=1; i<=nCol; i++){
10885 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
10888 /* At this point, we know that azRowid[j] is not the name of any
10889 ** ordinary column in the table. Verify that azRowid[j] is a valid
10890 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
10891 ** tables will fail this last check */
10892 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
10893 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
10902 ** Toggle the reverse_unordered_selects setting.
10904 static void toggleSelectOrder(sqlite3 *db){
10905 sqlite3_stmt *pStmt = 0;
10908 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
10909 if( sqlite3_step(pStmt)==SQLITE_ROW ){
10910 iSetting = sqlite3_column_int(pStmt, 0);
10912 sqlite3_finalize(pStmt);
10913 sqlite3_snprintf(sizeof(zStmt), zStmt,
10914 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
10915 sqlite3_exec(db, zStmt, 0, 0, 0);
10919 ** This is a different callback routine used for dumping the database.
10920 ** Each row received by this callback consists of a table name,
10921 ** the table type ("index" or "table") and SQL to create the table.
10922 ** This routine should print text sufficient to recreate the table.
10924 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
10926 const char *zTable;
10929 ShellState *p = (ShellState *)pArg;
10931 UNUSED_PARAMETER(azNotUsed);
10932 if( nArg!=3 || azArg==0 ) return 0;
10937 if( strcmp(zTable, "sqlite_sequence")==0 ){
10938 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
10939 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
10940 raw_printf(p->out, "ANALYZE sqlite_master;\n");
10941 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
10943 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
10945 if( !p->writableSchema ){
10946 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
10947 p->writableSchema = 1;
10949 zIns = sqlite3_mprintf(
10950 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
10951 "VALUES('table','%q','%q',0,'%q');",
10952 zTable, zTable, zSql);
10953 utf8_printf(p->out, "%s\n", zIns);
10954 sqlite3_free(zIns);
10957 printSchemaLine(p->out, zSql, ";\n");
10960 if( strcmp(zType, "table")==0 ){
10965 char *savedDestTable;
10968 azCol = tableColumnList(p, zTable);
10974 /* Always quote the table name, even if it appears to be pure ascii,
10975 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
10977 appendText(&sTable, zTable, quoteChar(zTable));
10978 /* If preserving the rowid, add a column list after the table name.
10979 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10980 ** instead of the usual "INSERT INTO tab VALUES(...)".
10983 appendText(&sTable, "(", 0);
10984 appendText(&sTable, azCol[0], 0);
10985 for(i=1; azCol[i]; i++){
10986 appendText(&sTable, ",", 0);
10987 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10989 appendText(&sTable, ")", 0);
10992 /* Build an appropriate SELECT statement */
10993 initText(&sSelect);
10994 appendText(&sSelect, "SELECT ", 0);
10996 appendText(&sSelect, azCol[0], 0);
10997 appendText(&sSelect, ",", 0);
10999 for(i=1; azCol[i]; i++){
11000 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
11002 appendText(&sSelect, ",", 0);
11005 freeColumnList(azCol);
11006 appendText(&sSelect, " FROM ", 0);
11007 appendText(&sSelect, zTable, quoteChar(zTable));
11009 savedDestTable = p->zDestTable;
11010 savedMode = p->mode;
11011 p->zDestTable = sTable.z;
11012 p->mode = p->cMode = MODE_Insert;
11013 rc = shell_exec(p, sSelect.z, 0);
11014 if( (rc&0xff)==SQLITE_CORRUPT ){
11015 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11016 toggleSelectOrder(p->db);
11017 shell_exec(p, sSelect.z, 0);
11018 toggleSelectOrder(p->db);
11020 p->zDestTable = savedDestTable;
11021 p->mode = savedMode;
11023 freeText(&sSelect);
11024 if( rc ) p->nErr++;
11030 ** Run zQuery. Use dump_callback() as the callback routine so that
11031 ** the contents of the query are output as SQL statements.
11033 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
11034 ** "ORDER BY rowid DESC" to the end.
11036 static int run_schema_dump_query(
11042 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
11043 if( rc==SQLITE_CORRUPT ){
11045 int len = strlen30(zQuery);
11046 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
11048 utf8_printf(p->out, "/****** %s ******/\n", zErr);
11049 sqlite3_free(zErr);
11052 zQ2 = malloc( len+100 );
11053 if( zQ2==0 ) return rc;
11054 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
11055 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
11057 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
11059 rc = SQLITE_CORRUPT;
11061 sqlite3_free(zErr);
11068 ** Text of help messages.
11070 ** The help text for each individual command begins with a line that starts
11071 ** with ".". Subsequent lines are supplimental information.
11073 ** There must be two or more spaces between the end of the command and the
11074 ** start of the description of what that command does.
11076 static const char *(azHelp[]) = {
11077 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
11078 ".archive ... Manage SQL archives",
11079 " Each command must have exactly one of the following options:",
11080 " -c, --create Create a new archive",
11081 " -u, --update Update or add files to an existing archive",
11082 " -t, --list List contents of archive",
11083 " -x, --extract Extract files from archive",
11084 " Optional arguments:",
11085 " -v, --verbose Print each filename as it is processed",
11086 " -f FILE, --file FILE Operate on archive FILE (default is current db)",
11087 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS",
11088 " -C DIR, --directory DIR Change to directory DIR to read/extract files",
11089 " -n, --dryrun Show the SQL that would have occurred",
11091 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar",
11092 " .ar -tf archive.sar # List members of archive.sar",
11093 " .ar -xvf archive.sar # Verbosely extract files from archive.sar",
11095 " http://sqlite.org/cli.html#sqlar_archive_support",
11097 #ifndef SQLITE_OMIT_AUTHORIZATION
11098 ".auth ON|OFF Show authorizer callbacks",
11100 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
11101 " --append Use the appendvfs",
11102 " --async Write to FILE without a journal and without fsync()",
11103 ".bail on|off Stop after hitting an error. Default OFF",
11104 ".binary on|off Turn binary output on or off. Default OFF",
11105 ".cd DIRECTORY Change the working directory to DIRECTORY",
11106 ".changes on|off Show number of rows changed by SQL",
11107 ".check GLOB Fail if output since .testcase does not match",
11108 ".clone NEWDB Clone data into NEWDB from the existing database",
11109 ".databases List names and files of attached databases",
11110 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
11111 ".dbinfo ?DB? Show status information about the database",
11112 ".dump ?TABLE? ... Render all database content as SQL",
11114 " --preserve-rowids Include ROWID values in the output",
11115 " --newlines Allow unescaped newline characters in output",
11116 " TABLE is LIKE pattern for the tables to dump",
11117 ".echo on|off Turn command echo on or off",
11118 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
11120 #ifdef SQLITE_DEBUG
11121 " test Show raw EXPLAIN QUERY PLAN output",
11122 " trace Like \"full\" but also enable \"PRAGMA vdbe_trace\"",
11124 " trigger Like \"full\" but also show trigger bytecode",
11125 ".excel Display the output of next command in a spreadsheet",
11126 ".exit ?CODE? Exit this program with return-code CODE",
11127 ".expert EXPERIMENTAL. Suggest indexes for specified queries",
11128 /* Because explain mode comes on automatically now, the ".explain" mode
11129 ** is removed from the help screen. It is still supported for legacy, however */
11130 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic",*/
11131 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
11132 ".headers on|off Turn display of headers on or off",
11133 ".help ?-all? ?PATTERN? Show help text for PATTERN",
11134 ".import FILE TABLE Import data from FILE into TABLE",
11135 #ifndef SQLITE_OMIT_TEST_CONTROL
11136 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
11138 ".indexes ?TABLE? Show names of indexes",
11139 " If TABLE is specified, only show indexes for",
11140 " tables matching TABLE using the LIKE operator.",
11141 #ifdef SQLITE_ENABLE_IOTRACE
11142 ".iotrace FILE Enable I/O diagnostic logging to FILE",
11144 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
11145 ".lint OPTIONS Report potential schema issues.",
11147 " fkey-indexes Find missing foreign key indexes",
11148 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11149 ".load FILE ?ENTRY? Load an extension library",
11151 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
11152 ".mode MODE ?TABLE? Set output mode",
11153 " MODE is one of:",
11154 " ascii Columns/rows delimited by 0x1F and 0x1E",
11155 " csv Comma-separated values",
11156 " column Left-aligned columns. (See .width)",
11157 " html HTML <table> code",
11158 " insert SQL insert statements for TABLE",
11159 " line One value per line",
11160 " list Values delimited by \"|\"",
11161 " quote Escape answers as for SQL",
11162 " tabs Tab-separated values",
11163 " tcl TCL list elements",
11164 ".nullvalue STRING Use STRING in place of NULL values",
11165 ".once (-e|-x|FILE) Output for the next SQL command only to FILE",
11166 " If FILE begins with '|' then open as a pipe",
11168 " -e Invoke system text editor",
11169 " -x Open in a spreadsheet",
11170 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
11172 " --append Use appendvfs to append database to the end of FILE",
11173 #ifdef SQLITE_ENABLE_DESERIALIZE
11174 " --deserialize Load into memory useing sqlite3_deserialize()",
11175 " --hexdb Load the output of \"dbtotxt\" as an in-memory database",
11176 " --maxsize N Maximum size for --hexdb or --deserialized database",
11178 " --new Initialize FILE to an empty database",
11179 " --readonly Open FILE readonly",
11180 " --zip FILE is a ZIP archive",
11181 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
11182 " If FILE begins with '|' then open it as a pipe.",
11183 ".print STRING... Print literal STRING",
11184 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11185 ".progress N Invoke progress handler after every N opcodes",
11186 " --limit N Interrupt after N progress callbacks",
11187 " --once Do no more than one progress interrupt",
11188 " --quiet|-q No output except at interrupts",
11189 " --reset Reset the count for each input and interrupt",
11191 ".prompt MAIN CONTINUE Replace the standard prompts",
11192 ".quit Exit this program",
11193 ".read FILE Read input from FILE",
11194 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
11195 ".save FILE Write in-memory database into FILE",
11196 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
11197 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
11199 " --indent Try to pretty-print the schema",
11200 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
11202 " --init Create a new SELFTEST table",
11203 " -v Verbose output",
11204 ".separator COL ?ROW? Change the column and row separators",
11205 #if defined(SQLITE_ENABLE_SESSION)
11206 ".session ?NAME? CMD ... Create or control sessions",
11208 " attach TABLE Attach TABLE",
11209 " changeset FILE Write a changeset into FILE",
11210 " close Close one session",
11211 " enable ?BOOLEAN? Set or query the enable bit",
11212 " filter GLOB... Reject tables matching GLOBs",
11213 " indirect ?BOOLEAN? Mark or query the indirect status",
11214 " isempty Query whether the session is empty",
11215 " list List currently open session names",
11216 " open DB NAME Open a new session on DB",
11217 " patchset FILE Write a patchset into FILE",
11218 " If ?NAME? is omitted, the first defined session is used.",
11220 ".sha3sum ... Compute a SHA3 hash of database content",
11222 " --schema Also hash the sqlite_master table",
11223 " --sha3-224 Use the sha3-224 algorithm",
11224 " --sha3-256 Use the sha3-256 algorithm. This is the default.",
11225 " --sha3-384 Use the sha3-384 algorithm",
11226 " --sha3-512 Use the sha3-512 algorithm",
11227 " Any other argument is a LIKE pattern for tables to hash",
11228 #ifndef SQLITE_NOHAVE_SYSTEM
11229 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
11231 ".show Show the current values for various settings",
11232 ".stats ?on|off? Show stats or turn stats on or off",
11233 #ifndef SQLITE_NOHAVE_SYSTEM
11234 ".system CMD ARGS... Run CMD ARGS... in a system shell",
11236 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
11237 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
11238 ".timeout MS Try opening locked tables for MS milliseconds",
11239 ".timer on|off Turn SQL timer on or off",
11240 #ifndef SQLITE_OMIT_TRACE
11241 ".trace ?OPTIONS? Output each SQL statement as it is run",
11242 " FILE Send output to FILE",
11243 " stdout Send output to stdout",
11244 " stderr Send output to stderr",
11245 " off Disable tracing",
11246 " --expanded Expand query parameters",
11247 #ifdef SQLITE_ENABLE_NORMALIZE
11248 " --normalized Normal the SQL statements",
11250 " --plain Show SQL as it is input",
11251 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
11252 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
11253 " --row Trace each row (SQLITE_TRACE_ROW)",
11254 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
11255 #endif /* SQLITE_OMIT_TRACE */
11256 ".vfsinfo ?AUX? Information about the top-level VFS",
11257 ".vfslist List all available VFSes",
11258 ".vfsname ?AUX? Print the name of the VFS stack",
11259 ".width NUM1 NUM2 ... Set column widths for \"column\" mode",
11260 " Negative values right-justify",
11264 ** Output help text.
11266 ** zPattern describes the set of commands for which help text is provided.
11267 ** If zPattern is NULL, then show all commands, but only give a one-line
11268 ** description of each.
11270 ** Return the number of matches.
11272 static int showHelp(FILE *out, const char *zPattern){
11278 || zPattern[0]=='0'
11279 || strcmp(zPattern,"-a")==0
11280 || strcmp(zPattern,"-all")==0
11282 /* Show all commands, but only one line per command */
11283 if( zPattern==0 ) zPattern = "";
11284 for(i=0; i<ArraySize(azHelp); i++){
11285 if( azHelp[i][0]=='.' || zPattern[0] ){
11286 utf8_printf(out, "%s\n", azHelp[i]);
11291 /* Look for commands that for which zPattern is an exact prefix */
11292 zPat = sqlite3_mprintf(".%s*", zPattern);
11293 for(i=0; i<ArraySize(azHelp); i++){
11294 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
11295 utf8_printf(out, "%s\n", azHelp[i]);
11300 sqlite3_free(zPat);
11303 /* when zPattern is a prefix of exactly one command, then include the
11304 ** details of that command, which should begin at offset j */
11305 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
11306 utf8_printf(out, "%s\n", azHelp[j]);
11312 /* Look for commands that contain zPattern anywhere. Show the complete
11313 ** text of all commands that match. */
11314 zPat = sqlite3_mprintf("%%%s%%", zPattern);
11315 for(i=0; i<ArraySize(azHelp); i++){
11316 if( azHelp[i][0]=='.' ) j = i;
11317 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
11318 utf8_printf(out, "%s\n", azHelp[j]);
11319 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
11321 utf8_printf(out, "%s\n", azHelp[j]);
11327 sqlite3_free(zPat);
11332 /* Forward reference */
11333 static int process_input(ShellState *p);
11336 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
11337 ** and return a pointer to the buffer. The caller is responsible for freeing
11340 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
11343 ** For convenience, a nul-terminator byte is always appended to the data read
11344 ** from the file before the buffer is returned. This byte is not included in
11345 ** the final value of (*pnByte), if applicable.
11347 ** NULL is returned if any error is encountered. The final value of *pnByte
11348 ** is undefined in this case.
11350 static char *readFile(const char *zName, int *pnByte){
11351 FILE *in = fopen(zName, "rb");
11355 if( in==0 ) return 0;
11356 fseek(in, 0, SEEK_END);
11359 pBuf = sqlite3_malloc64( nIn+1 );
11360 if( pBuf==0 ){ fclose(in); return 0; }
11361 nRead = fread(pBuf, nIn, 1, in);
11364 sqlite3_free(pBuf);
11368 if( pnByte ) *pnByte = nIn;
11372 #if defined(SQLITE_ENABLE_SESSION)
11374 ** Close a single OpenSession object and release all of its associated
11377 static void session_close(OpenSession *pSession){
11379 sqlite3session_delete(pSession->p);
11380 sqlite3_free(pSession->zName);
11381 for(i=0; i<pSession->nFilter; i++){
11382 sqlite3_free(pSession->azFilter[i]);
11384 sqlite3_free(pSession->azFilter);
11385 memset(pSession, 0, sizeof(OpenSession));
11390 ** Close all OpenSession objects and release all associated resources.
11392 #if defined(SQLITE_ENABLE_SESSION)
11393 static void session_close_all(ShellState *p){
11395 for(i=0; i<p->nSession; i++){
11396 session_close(&p->aSession[i]);
11401 # define session_close_all(X)
11405 ** Implementation of the xFilter function for an open session. Omit
11406 ** any tables named by ".session filter" but let all other table through.
11408 #if defined(SQLITE_ENABLE_SESSION)
11409 static int session_filter(void *pCtx, const char *zTab){
11410 OpenSession *pSession = (OpenSession*)pCtx;
11412 for(i=0; i<pSession->nFilter; i++){
11413 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
11420 ** Try to deduce the type of file for zName based on its content. Return
11421 ** one of the SHELL_OPEN_* constants.
11423 ** If the file does not exist or is empty but its name looks like a ZIP
11424 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
11425 ** Otherwise, assume an ordinary database regardless of the filename if
11426 ** the type cannot be determined from content.
11428 int deduceDatabaseType(const char *zName, int dfltZip){
11429 FILE *f = fopen(zName, "rb");
11431 int rc = SHELL_OPEN_UNSPEC;
11434 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11435 return SHELL_OPEN_ZIPFILE;
11437 return SHELL_OPEN_NORMAL;
11440 n = fread(zBuf, 16, 1, f);
11441 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
11443 return SHELL_OPEN_NORMAL;
11445 fseek(f, -25, SEEK_END);
11446 n = fread(zBuf, 25, 1, f);
11447 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
11448 rc = SHELL_OPEN_APPENDVFS;
11450 fseek(f, -22, SEEK_END);
11451 n = fread(zBuf, 22, 1, f);
11452 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
11453 && zBuf[3]==0x06 ){
11454 rc = SHELL_OPEN_ZIPFILE;
11455 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
11456 rc = SHELL_OPEN_ZIPFILE;
11463 #ifdef SQLITE_ENABLE_DESERIALIZE
11465 ** Reconstruct an in-memory database using the output from the "dbtotxt"
11466 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
11467 ** is 0, then read from standard input.
11469 static unsigned char *readHexDb(ShellState *p, int *pnData){
11470 unsigned char *a = 0;
11478 unsigned char x[16];
11480 if( p->zDbFilename ){
11481 in = fopen(p->zDbFilename, "r");
11483 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
11493 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
11494 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
11495 if( rc!=2 ) goto readHexDb_error;
11496 if( n<=0 ) goto readHexDb_error;
11497 a = sqlite3_malloc( n );
11499 utf8_printf(stderr, "Out of memory!\n");
11500 goto readHexDb_error;
11503 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
11504 utf8_printf(stderr, "invalid pagesize\n");
11505 goto readHexDb_error;
11507 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
11508 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
11513 if( strncmp(zLine, "| end ", 6)==0 ){
11516 rc = sscanf(zLine,"| %d: %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx"
11517 " %hhx %hhx %hhx %hhx %hhx %hhx %hhx %hhx",
11518 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
11519 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
11523 memcpy(a+k, x, 16);
11539 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
11541 if(strncmp(zLine, "| end ", 6)==0 ) break;
11546 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
11549 #endif /* SQLITE_ENABLE_DESERIALIZE */
11551 /* Flags for open_db().
11553 ** The default behavior of open_db() is to exit(1) if the database fails to
11554 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
11555 ** but still returns without calling exit.
11557 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
11558 ** ZIP archive if the file does not exist or is empty and its name matches
11559 ** the *.zip pattern.
11561 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
11562 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
11565 ** Make sure the database is open. If it is not, then open it. If
11566 ** the database fails to open, print an error message and exit.
11568 static void open_db(ShellState *p, int openFlags){
11570 if( p->openMode==SHELL_OPEN_UNSPEC ){
11571 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
11572 p->openMode = SHELL_OPEN_NORMAL;
11574 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
11575 (openFlags & OPEN_DB_ZIPFILE)!=0);
11578 switch( p->openMode ){
11579 case SHELL_OPEN_APPENDVFS: {
11580 sqlite3_open_v2(p->zDbFilename, &p->db,
11581 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
11584 case SHELL_OPEN_HEXDB:
11585 case SHELL_OPEN_DESERIALIZE: {
11586 sqlite3_open(0, &p->db);
11589 case SHELL_OPEN_ZIPFILE: {
11590 sqlite3_open(":memory:", &p->db);
11593 case SHELL_OPEN_READONLY: {
11594 sqlite3_open_v2(p->zDbFilename, &p->db, SQLITE_OPEN_READONLY, 0);
11597 case SHELL_OPEN_UNSPEC:
11598 case SHELL_OPEN_NORMAL: {
11599 sqlite3_open(p->zDbFilename, &p->db);
11604 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
11605 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
11606 p->zDbFilename, sqlite3_errmsg(p->db));
11607 if( openFlags & OPEN_DB_KEEPALIVE ){
11608 sqlite3_open(":memory:", &p->db);
11613 #ifndef SQLITE_OMIT_LOAD_EXTENSION
11614 sqlite3_enable_load_extension(p->db, 1);
11616 sqlite3_fileio_init(p->db, 0, 0);
11617 sqlite3_shathree_init(p->db, 0, 0);
11618 sqlite3_completion_init(p->db, 0, 0);
11619 #ifdef SQLITE_HAVE_ZLIB
11620 sqlite3_zipfile_init(p->db, 0, 0);
11621 sqlite3_sqlar_init(p->db, 0, 0);
11623 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
11624 shellAddSchemaName, 0, 0);
11625 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
11626 shellModuleSchema, 0, 0);
11627 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
11628 shellPutsFunc, 0, 0);
11629 #ifndef SQLITE_NOHAVE_SYSTEM
11630 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
11632 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
11635 if( p->openMode==SHELL_OPEN_ZIPFILE ){
11636 char *zSql = sqlite3_mprintf(
11637 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
11638 sqlite3_exec(p->db, zSql, 0, 0, 0);
11639 sqlite3_free(zSql);
11641 #ifdef SQLITE_ENABLE_DESERIALIZE
11643 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
11646 unsigned char *aData;
11647 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
11648 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
11650 aData = readHexDb(p, &nData);
11652 utf8_printf(stderr, "Error in hexdb input\n");
11656 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
11657 SQLITE_DESERIALIZE_RESIZEABLE |
11658 SQLITE_DESERIALIZE_FREEONCLOSE);
11660 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
11663 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
11671 ** Attempt to close the databaes connection. Report errors.
11673 void close_db(sqlite3 *db){
11674 int rc = sqlite3_close(db);
11676 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
11677 rc, sqlite3_errmsg(db));
11681 #if HAVE_READLINE || HAVE_EDITLINE
11683 ** Readline completion callbacks
11685 static char *readline_completion_generator(const char *text, int state){
11686 static sqlite3_stmt *pStmt = 0;
11690 sqlite3_finalize(pStmt);
11691 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11692 " FROM completion(%Q) ORDER BY 1", text);
11693 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11694 sqlite3_free(zSql);
11696 if( sqlite3_step(pStmt)==SQLITE_ROW ){
11697 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
11699 sqlite3_finalize(pStmt);
11705 static char **readline_completion(const char *zText, int iStart, int iEnd){
11706 rl_attempted_completion_over = 1;
11707 return rl_completion_matches(zText, readline_completion_generator);
11710 #elif HAVE_LINENOISE
11712 ** Linenoise completion callback
11714 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
11715 int nLine = strlen30(zLine);
11717 sqlite3_stmt *pStmt = 0;
11721 if( nLine>sizeof(zBuf)-30 ) return;
11722 if( zLine[0]=='.' || zLine[0]=='#') return;
11723 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
11724 if( i==nLine-1 ) return;
11726 memcpy(zBuf, zLine, iStart);
11727 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
11728 " FROM completion(%Q,%Q) ORDER BY 1",
11729 &zLine[iStart], zLine);
11730 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
11731 sqlite3_free(zSql);
11732 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
11733 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11734 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
11735 int nCompletion = sqlite3_column_bytes(pStmt, 0);
11736 if( iStart+nCompletion < sizeof(zBuf)-1 ){
11737 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
11738 linenoiseAddCompletion(lc, zBuf);
11741 sqlite3_finalize(pStmt);
11746 ** Do C-language style dequoting.
11752 ** \v -> vertical tab
11754 ** \r -> carriage return
11759 ** \NNN -> ascii character NNN in octal
11761 static void resolve_backslashes(char *z){
11764 while( *z && *z!='\\' ) z++;
11765 for(i=j=0; (c = z[i])!=0; i++, j++){
11766 if( c=='\\' && z[i+1]!=0 ){
11770 }else if( c=='b' ){
11772 }else if( c=='t' ){
11774 }else if( c=='n' ){
11776 }else if( c=='v' ){
11778 }else if( c=='f' ){
11780 }else if( c=='r' ){
11782 }else if( c=='"' ){
11784 }else if( c=='\'' ){
11786 }else if( c=='\\' ){
11788 }else if( c>='0' && c<='7' ){
11790 if( z[i+1]>='0' && z[i+1]<='7' ){
11792 c = (c<<3) + z[i] - '0';
11793 if( z[i+1]>='0' && z[i+1]<='7' ){
11795 c = (c<<3) + z[i] - '0';
11802 if( j<i ) z[j] = 0;
11806 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
11807 ** for TRUE and FALSE. Return the integer value if appropriate.
11809 static int booleanValue(const char *zArg){
11811 if( zArg[0]=='0' && zArg[1]=='x' ){
11812 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
11814 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
11816 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
11817 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
11820 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
11823 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
11829 ** Set or clear a shell flag according to a boolean value.
11831 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
11832 if( booleanValue(zArg) ){
11833 ShellSetFlag(p, mFlag);
11835 ShellClearFlag(p, mFlag);
11840 ** Close an output file, assuming it is not stderr or stdout
11842 static void output_file_close(FILE *f){
11843 if( f && f!=stdout && f!=stderr ) fclose(f);
11847 ** Try to open an output file. The names "stdout" and "stderr" are
11848 ** recognized and do the right thing. NULL is returned if the output
11849 ** filename is "off".
11851 static FILE *output_file_open(const char *zFile, int bTextMode){
11853 if( strcmp(zFile,"stdout")==0 ){
11855 }else if( strcmp(zFile, "stderr")==0 ){
11857 }else if( strcmp(zFile, "off")==0 ){
11860 f = fopen(zFile, bTextMode ? "w" : "wb");
11862 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
11868 #ifndef SQLITE_OMIT_TRACE
11870 ** A routine for handling output from sqlite3_trace().
11872 static int sql_trace_callback(
11873 unsigned mType, /* The trace type */
11874 void *pArg, /* The ShellState pointer */
11875 void *pP, /* Usually a pointer to sqlite_stmt */
11876 void *pX /* Auxiliary output */
11878 ShellState *p = (ShellState*)pArg;
11879 sqlite3_stmt *pStmt;
11882 if( p->traceOut==0 ) return 0;
11883 if( mType==SQLITE_TRACE_CLOSE ){
11884 utf8_printf(p->traceOut, "-- closing database connection\n");
11887 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
11888 zSql = (const char*)pX;
11890 pStmt = (sqlite3_stmt*)pP;
11891 switch( p->eTraceType ){
11892 case SHELL_TRACE_EXPANDED: {
11893 zSql = sqlite3_expanded_sql(pStmt);
11896 #ifdef SQLITE_ENABLE_NORMALIZE
11897 case SHELL_TRACE_NORMALIZED: {
11898 zSql = sqlite3_normalized_sql(pStmt);
11903 zSql = sqlite3_sql(pStmt);
11908 if( zSql==0 ) return 0;
11909 nSql = strlen30(zSql);
11910 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
11912 case SQLITE_TRACE_ROW:
11913 case SQLITE_TRACE_STMT: {
11914 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
11917 case SQLITE_TRACE_PROFILE: {
11918 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
11919 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
11928 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
11929 ** a useful spot to set a debugger breakpoint.
11931 static void test_breakpoint(void){
11932 static int nCall = 0;
11937 ** An object used to read a CSV and other files for import.
11939 typedef struct ImportCtx ImportCtx;
11941 const char *zFile; /* Name of the input file */
11942 FILE *in; /* Read the CSV text from this input stream */
11943 char *z; /* Accumulated text for a field */
11944 int n; /* Number of bytes in z */
11945 int nAlloc; /* Space allocated for z[] */
11946 int nLine; /* Current line number */
11947 int bNotFirst; /* True if one or more bytes already read */
11948 int cTerm; /* Character that terminated the most recent field */
11949 int cColSep; /* The column separator character. (Usually ",") */
11950 int cRowSep; /* The row separator character. (Usually "\n") */
11953 /* Append a single byte to z[] */
11954 static void import_append_char(ImportCtx *p, int c){
11955 if( p->n+1>=p->nAlloc ){
11956 p->nAlloc += p->nAlloc + 100;
11957 p->z = sqlite3_realloc64(p->z, p->nAlloc);
11958 if( p->z==0 ) shell_out_of_memory();
11960 p->z[p->n++] = (char)c;
11963 /* Read a single field of CSV text. Compatible with rfc4180 and extended
11964 ** with the option of having a separator other than ",".
11966 ** + Input comes from p->in.
11967 ** + Store results in p->z of length p->n. Space to hold p->z comes
11968 ** from sqlite3_malloc64().
11969 ** + Use p->cSep as the column separator. The default is ",".
11970 ** + Use p->rSep as the row separator. The default is "\n".
11971 ** + Keep track of the line number in p->nLine.
11972 ** + Store the character that terminates the field in p->cTerm. Store
11973 ** EOF on end-of-file.
11974 ** + Report syntax errors on stderr
11976 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
11978 int cSep = p->cColSep;
11979 int rSep = p->cRowSep;
11982 if( c==EOF || seenInterrupt ){
11988 int startLine = p->nLine;
11993 if( c==rSep ) p->nLine++;
12000 if( (c==cSep && pc==cQuote)
12001 || (c==rSep && pc==cQuote)
12002 || (c==rSep && pc=='\r' && ppc==cQuote)
12003 || (c==EOF && pc==cQuote)
12005 do{ p->n--; }while( p->z[p->n]!=cQuote );
12009 if( pc==cQuote && c!='\r' ){
12010 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
12011 p->zFile, p->nLine, cQuote);
12014 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
12015 p->zFile, startLine, cQuote);
12019 import_append_char(p, c);
12024 /* If this is the first field being parsed and it begins with the
12025 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
12026 if( (c&0xff)==0xef && p->bNotFirst==0 ){
12027 import_append_char(p, c);
12029 if( (c&0xff)==0xbb ){
12030 import_append_char(p, c);
12032 if( (c&0xff)==0xbf ){
12035 return csv_read_one_field(p);
12039 while( c!=EOF && c!=cSep && c!=rSep ){
12040 import_append_char(p, c);
12045 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
12049 if( p->z ) p->z[p->n] = 0;
12054 /* Read a single field of ASCII delimited text.
12056 ** + Input comes from p->in.
12057 ** + Store results in p->z of length p->n. Space to hold p->z comes
12058 ** from sqlite3_malloc64().
12059 ** + Use p->cSep as the column separator. The default is "\x1F".
12060 ** + Use p->rSep as the row separator. The default is "\x1E".
12061 ** + Keep track of the row number in p->nLine.
12062 ** + Store the character that terminates the field in p->cTerm. Store
12063 ** EOF on end-of-file.
12064 ** + Report syntax errors on stderr
12066 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
12068 int cSep = p->cColSep;
12069 int rSep = p->cRowSep;
12072 if( c==EOF || seenInterrupt ){
12076 while( c!=EOF && c!=cSep && c!=rSep ){
12077 import_append_char(p, c);
12084 if( p->z ) p->z[p->n] = 0;
12089 ** Try to transfer data for table zTable. If an error is seen while
12090 ** moving forward, try to go backwards. The backwards movement won't
12091 ** work for WITHOUT ROWID tables.
12093 static void tryToCloneData(
12098 sqlite3_stmt *pQuery = 0;
12099 sqlite3_stmt *pInsert = 0;
12104 int nTable = strlen30(zTable);
12107 const int spinRate = 10000;
12109 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
12110 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12112 utf8_printf(stderr, "Error %d: %s on [%s]\n",
12113 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12115 goto end_data_xfer;
12117 n = sqlite3_column_count(pQuery);
12118 zInsert = sqlite3_malloc64(200 + nTable + n*3);
12119 if( zInsert==0 ) shell_out_of_memory();
12120 sqlite3_snprintf(200+nTable,zInsert,
12121 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
12122 i = strlen30(zInsert);
12123 for(j=1; j<n; j++){
12124 memcpy(zInsert+i, ",?", 2);
12127 memcpy(zInsert+i, ");", 3);
12128 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
12130 utf8_printf(stderr, "Error %d: %s on [%s]\n",
12131 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
12133 goto end_data_xfer;
12135 for(k=0; k<2; k++){
12136 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12137 for(i=0; i<n; i++){
12138 switch( sqlite3_column_type(pQuery, i) ){
12139 case SQLITE_NULL: {
12140 sqlite3_bind_null(pInsert, i+1);
12143 case SQLITE_INTEGER: {
12144 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
12147 case SQLITE_FLOAT: {
12148 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
12151 case SQLITE_TEXT: {
12152 sqlite3_bind_text(pInsert, i+1,
12153 (const char*)sqlite3_column_text(pQuery,i),
12154 -1, SQLITE_STATIC);
12157 case SQLITE_BLOB: {
12158 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
12159 sqlite3_column_bytes(pQuery,i),
12165 rc = sqlite3_step(pInsert);
12166 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
12167 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
12168 sqlite3_errmsg(newDb));
12170 sqlite3_reset(pInsert);
12172 if( (cnt%spinRate)==0 ){
12173 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
12177 if( rc==SQLITE_DONE ) break;
12178 sqlite3_finalize(pQuery);
12179 sqlite3_free(zQuery);
12180 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
12182 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12184 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
12187 } /* End for(k=0...) */
12190 sqlite3_finalize(pQuery);
12191 sqlite3_finalize(pInsert);
12192 sqlite3_free(zQuery);
12193 sqlite3_free(zInsert);
12198 ** Try to transfer all rows of the schema that match zWhere. For
12199 ** each row, invoke xForEach() on the object defined by that row.
12200 ** If an error is encountered while moving forward through the
12201 ** sqlite_master table, try again moving backwards.
12203 static void tryToCloneSchema(
12206 const char *zWhere,
12207 void (*xForEach)(ShellState*,sqlite3*,const char*)
12209 sqlite3_stmt *pQuery = 0;
12212 const unsigned char *zName;
12213 const unsigned char *zSql;
12216 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12217 " WHERE %s", zWhere);
12218 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12220 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12221 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12223 goto end_schema_xfer;
12225 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12226 zName = sqlite3_column_text(pQuery, 0);
12227 zSql = sqlite3_column_text(pQuery, 1);
12228 printf("%s... ", zName); fflush(stdout);
12229 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12231 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12232 sqlite3_free(zErrMsg);
12236 xForEach(p, newDb, (const char*)zName);
12240 if( rc!=SQLITE_DONE ){
12241 sqlite3_finalize(pQuery);
12242 sqlite3_free(zQuery);
12243 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
12244 " WHERE %s ORDER BY rowid DESC", zWhere);
12245 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
12247 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
12248 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
12250 goto end_schema_xfer;
12252 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
12253 zName = sqlite3_column_text(pQuery, 0);
12254 zSql = sqlite3_column_text(pQuery, 1);
12255 printf("%s... ", zName); fflush(stdout);
12256 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
12258 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
12259 sqlite3_free(zErrMsg);
12263 xForEach(p, newDb, (const char*)zName);
12269 sqlite3_finalize(pQuery);
12270 sqlite3_free(zQuery);
12274 ** Open a new database file named "zNewDb". Try to recover as much information
12275 ** as possible out of the main database (which might be corrupt) and write it
12278 static void tryToClone(ShellState *p, const char *zNewDb){
12280 sqlite3 *newDb = 0;
12281 if( access(zNewDb,0)==0 ){
12282 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
12285 rc = sqlite3_open(zNewDb, &newDb);
12287 utf8_printf(stderr, "Cannot create output database: %s\n",
12288 sqlite3_errmsg(newDb));
12290 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
12291 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
12292 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
12293 tryToCloneSchema(p, newDb, "type!='table'", 0);
12294 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
12295 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12301 ** Change the output file back to stdout.
12303 ** If the p->doXdgOpen flag is set, that means the output was being
12304 ** redirected to a temporary file named by p->zTempFile. In that case,
12305 ** launch start/open/xdg-open on that temporary file.
12307 static void output_reset(ShellState *p){
12308 if( p->outfile[0]=='|' ){
12309 #ifndef SQLITE_OMIT_POPEN
12313 output_file_close(p->out);
12314 #ifndef SQLITE_NOHAVE_SYSTEM
12315 if( p->doXdgOpen ){
12316 const char *zXdgOpenCmd =
12317 #if defined(_WIN32)
12319 #elif defined(__APPLE__)
12325 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
12326 if( system(zCmd) ){
12327 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
12329 sqlite3_free(zCmd);
12333 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
12340 ** Run an SQL command and return the single integer result.
12342 static int db_int(ShellState *p, const char *zSql){
12343 sqlite3_stmt *pStmt;
12345 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12346 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
12347 res = sqlite3_column_int(pStmt,0);
12349 sqlite3_finalize(pStmt);
12354 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
12356 static unsigned int get2byteInt(unsigned char *a){
12357 return (a[0]<<8) + a[1];
12359 static unsigned int get4byteInt(unsigned char *a){
12360 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
12364 ** Implementation of the ".info" command.
12366 ** Return 1 on error, 2 to exit, and 0 otherwise.
12368 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
12369 static const struct { const char *zName; int ofst; } aField[] = {
12370 { "file change counter:", 24 },
12371 { "database page count:", 28 },
12372 { "freelist page count:", 36 },
12373 { "schema cookie:", 40 },
12374 { "schema format:", 44 },
12375 { "default cache size:", 48 },
12376 { "autovacuum top root:", 52 },
12377 { "incremental vacuum:", 64 },
12378 { "text encoding:", 56 },
12379 { "user version:", 60 },
12380 { "application id:", 68 },
12381 { "software version:", 96 },
12383 static const struct { const char *zName; const char *zSql; } aQuery[] = {
12384 { "number of tables:",
12385 "SELECT count(*) FROM %s WHERE type='table'" },
12386 { "number of indexes:",
12387 "SELECT count(*) FROM %s WHERE type='index'" },
12388 { "number of triggers:",
12389 "SELECT count(*) FROM %s WHERE type='trigger'" },
12390 { "number of views:",
12391 "SELECT count(*) FROM %s WHERE type='view'" },
12393 "SELECT total(length(sql)) FROM %s" },
12396 unsigned iDataVersion;
12398 char *zDb = nArg>=2 ? azArg[1] : "main";
12399 sqlite3_stmt *pStmt = 0;
12400 unsigned char aHdr[100];
12402 if( p->db==0 ) return 1;
12403 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
12405 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
12406 if( sqlite3_step(pStmt)==SQLITE_ROW
12407 && sqlite3_column_bytes(pStmt,0)>100
12409 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
12410 sqlite3_finalize(pStmt);
12412 raw_printf(stderr, "unable to read database header\n");
12413 sqlite3_finalize(pStmt);
12416 i = get2byteInt(aHdr+16);
12417 if( i==1 ) i = 65536;
12418 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
12419 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
12420 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
12421 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
12422 for(i=0; i<ArraySize(aField); i++){
12423 int ofst = aField[i].ofst;
12424 unsigned int val = get4byteInt(aHdr + ofst);
12425 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
12428 if( val==1 ) raw_printf(p->out, " (utf8)");
12429 if( val==2 ) raw_printf(p->out, " (utf16le)");
12430 if( val==3 ) raw_printf(p->out, " (utf16be)");
12433 raw_printf(p->out, "\n");
12436 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
12437 }else if( strcmp(zDb,"temp")==0 ){
12438 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
12440 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
12442 for(i=0; i<ArraySize(aQuery); i++){
12443 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
12444 int val = db_int(p, zSql);
12445 sqlite3_free(zSql);
12446 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
12448 sqlite3_free(zSchemaTab);
12449 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
12450 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
12455 ** Print the current sqlite3_errmsg() value to stderr and return 1.
12457 static int shellDatabaseError(sqlite3 *db){
12458 const char *zErr = sqlite3_errmsg(db);
12459 utf8_printf(stderr, "Error: %s\n", zErr);
12464 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
12465 ** if they match and FALSE (0) if they do not match.
12469 ** '*' Matches any sequence of zero or more characters.
12471 ** '?' Matches exactly one character.
12473 ** [...] Matches one character from the enclosed list of
12476 ** [^...] Matches one character not in the enclosed list.
12478 ** '#' Matches any sequence of one or more digits with an
12479 ** optional + or - sign in front
12481 ** ' ' Any span of whitespace matches any other span of
12484 ** Extra whitespace at the end of z[] is ignored.
12486 static int testcase_glob(const char *zGlob, const char *z){
12491 while( (c = (*(zGlob++)))!=0 ){
12493 if( !IsSpace(*z) ) return 0;
12494 while( IsSpace(*zGlob) ) zGlob++;
12495 while( IsSpace(*z) ) z++;
12496 }else if( c=='*' ){
12497 while( (c=(*(zGlob++))) == '*' || c=='?' ){
12498 if( c=='?' && (*(z++))==0 ) return 0;
12502 }else if( c=='[' ){
12503 while( *z && testcase_glob(zGlob-1,z)==0 ){
12508 while( (c2 = (*(z++)))!=0 ){
12511 if( c2==0 ) return 0;
12513 if( testcase_glob(zGlob,z) ) return 1;
12516 }else if( c=='?' ){
12517 if( (*(z++))==0 ) return 0;
12518 }else if( c=='[' ){
12523 if( c==0 ) return 0;
12530 if( c==']' ) seen = 1;
12533 while( c2 && c2!=']' ){
12534 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
12536 if( c>=prior_c && c<=c2 ) seen = 1;
12546 if( c2==0 || (seen ^ invert)==0 ) return 0;
12547 }else if( c=='#' ){
12548 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
12549 if( !IsDigit(z[0]) ) return 0;
12551 while( IsDigit(z[0]) ){ z++; }
12553 if( c!=(*(z++)) ) return 0;
12556 while( IsSpace(*z) ){ z++; }
12562 ** Compare the string as a command-line option with either one or two
12563 ** initial "-" characters.
12565 static int optionMatch(const char *zStr, const char *zOpt){
12566 if( zStr[0]!='-' ) return 0;
12568 if( zStr[0]=='-' ) zStr++;
12569 return strcmp(zStr, zOpt)==0;
12575 int shellDeleteFile(const char *zFilename){
12578 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
12582 rc = unlink(zFilename);
12588 ** Try to delete the temporary file (if there is one) and free the
12589 ** memory used to hold the name of the temp file.
12591 static void clearTempFile(ShellState *p){
12592 if( p->zTempFile==0 ) return;
12593 if( p->doXdgOpen ) return;
12594 if( shellDeleteFile(p->zTempFile) ) return;
12595 sqlite3_free(p->zTempFile);
12600 ** Create a new temp file name with the given suffix.
12602 static void newTempFile(ShellState *p, const char *zSuffix){
12604 sqlite3_free(p->zTempFile);
12607 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
12609 if( p->zTempFile==0 ){
12611 sqlite3_randomness(sizeof(r), &r);
12612 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
12614 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
12616 if( p->zTempFile==0 ){
12617 raw_printf(stderr, "out of memory\n");
12624 ** The implementation of SQL scalar function fkey_collate_clause(), used
12625 ** by the ".lint fkey-indexes" command. This scalar function is always
12626 ** called with four arguments - the parent table name, the parent column name,
12627 ** the child table name and the child column name.
12629 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
12631 ** If either of the named tables or columns do not exist, this function
12632 ** returns an empty string. An empty string is also returned if both tables
12633 ** and columns exist but have the same default collation sequence. Or,
12634 ** if both exist but the default collation sequences are different, this
12635 ** function returns the string " COLLATE <parent-collation>", where
12636 ** <parent-collation> is the default collation sequence of the parent column.
12638 static void shellFkeyCollateClause(
12639 sqlite3_context *pCtx,
12641 sqlite3_value **apVal
12643 sqlite3 *db = sqlite3_context_db_handle(pCtx);
12644 const char *zParent;
12645 const char *zParentCol;
12646 const char *zParentSeq;
12647 const char *zChild;
12648 const char *zChildCol;
12649 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
12653 zParent = (const char*)sqlite3_value_text(apVal[0]);
12654 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
12655 zChild = (const char*)sqlite3_value_text(apVal[2]);
12656 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
12658 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
12659 rc = sqlite3_table_column_metadata(
12660 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
12662 if( rc==SQLITE_OK ){
12663 rc = sqlite3_table_column_metadata(
12664 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
12668 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
12669 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
12670 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
12677 ** The implementation of dot-command ".lint fkey-indexes".
12679 static int lintFkeyIndexes(
12680 ShellState *pState, /* Current shell tool state */
12681 char **azArg, /* Array of arguments passed to dot command */
12682 int nArg /* Number of entries in azArg[] */
12684 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
12685 FILE *out = pState->out; /* Stream to write non-error output to */
12686 int bVerbose = 0; /* If -verbose is present */
12687 int bGroupByParent = 0; /* If -groupbyparent is present */
12688 int i; /* To iterate through azArg[] */
12689 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
12690 int rc; /* Return code */
12691 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
12694 ** This SELECT statement returns one row for each foreign key constraint
12695 ** in the schema of the main database. The column values are:
12697 ** 0. The text of an SQL statement similar to:
12699 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
12701 ** This SELECT is similar to the one that the foreign keys implementation
12702 ** needs to run internally on child tables. If there is an index that can
12703 ** be used to optimize this query, then it can also be used by the FK
12704 ** implementation to optimize DELETE or UPDATE statements on the parent
12707 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
12708 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
12709 ** contains an index that can be used to optimize the query.
12711 ** 2. Human readable text that describes the child table and columns. e.g.
12713 ** "child_table(child_key1, child_key2)"
12715 ** 3. Human readable text that describes the parent table and columns. e.g.
12717 ** "parent_table(parent_key1, parent_key2)"
12719 ** 4. A full CREATE INDEX statement for an index that could be used to
12720 ** optimize DELETE or UPDATE statements on the parent table. e.g.
12722 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
12724 ** 5. The name of the parent table.
12726 ** These six values are used by the C logic below to generate the report.
12730 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
12731 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
12732 " || fkey_collate_clause("
12733 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
12735 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
12736 " || group_concat('*=?', ' AND ') || ')'"
12738 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
12740 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
12742 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
12743 " || ' ON ' || quote(s.name) || '('"
12744 " || group_concat(quote(f.[from]) ||"
12745 " fkey_collate_clause("
12746 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
12750 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
12751 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
12752 "GROUP BY s.name, f.id "
12753 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
12755 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
12757 for(i=2; i<nArg; i++){
12758 int n = strlen30(azArg[i]);
12759 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
12762 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
12763 bGroupByParent = 1;
12767 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
12770 return SQLITE_ERROR;
12774 /* Register the fkey_collate_clause() SQL function */
12775 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
12776 0, shellFkeyCollateClause, 0, 0
12780 if( rc==SQLITE_OK ){
12781 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
12783 if( rc==SQLITE_OK ){
12784 sqlite3_bind_int(pSql, 1, bGroupByParent);
12787 if( rc==SQLITE_OK ){
12790 while( SQLITE_ROW==sqlite3_step(pSql) ){
12792 sqlite3_stmt *pExplain = 0;
12793 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
12794 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
12795 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
12796 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
12797 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
12798 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
12800 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
12801 if( rc!=SQLITE_OK ) break;
12802 if( SQLITE_ROW==sqlite3_step(pExplain) ){
12803 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
12805 0==sqlite3_strglob(zGlob, zPlan)
12806 || 0==sqlite3_strglob(zGlobIPK, zPlan)
12809 rc = sqlite3_finalize(pExplain);
12810 if( rc!=SQLITE_OK ) break;
12813 raw_printf(stderr, "Error: internal error");
12817 && (bVerbose || res==0)
12818 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
12820 raw_printf(out, "-- Parent table %s\n", zParent);
12821 sqlite3_free(zPrev);
12822 zPrev = sqlite3_mprintf("%s", zParent);
12826 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
12827 }else if( bVerbose ){
12828 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
12829 zIndent, zFrom, zTarget
12834 sqlite3_free(zPrev);
12836 if( rc!=SQLITE_OK ){
12837 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12840 rc2 = sqlite3_finalize(pSql);
12841 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
12843 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12846 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
12853 ** Implementation of ".lint" dot command.
12855 static int lintDotCommand(
12856 ShellState *pState, /* Current shell tool state */
12857 char **azArg, /* Array of arguments passed to dot command */
12858 int nArg /* Number of entries in azArg[] */
12861 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
12862 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
12863 return lintFkeyIndexes(pState, azArg, nArg);
12866 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
12867 raw_printf(stderr, "Where sub-commands are:\n");
12868 raw_printf(stderr, " fkey-indexes\n");
12869 return SQLITE_ERROR;
12872 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12873 /*********************************************************************************
12874 ** The ".archive" or ".ar" command.
12876 static void shellPrepare(
12880 sqlite3_stmt **ppStmt
12883 if( *pRc==SQLITE_OK ){
12884 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
12885 if( rc!=SQLITE_OK ){
12886 raw_printf(stderr, "sql error: %s (%d)\n",
12887 sqlite3_errmsg(db), sqlite3_errcode(db)
12894 static void shellPreparePrintf(
12897 sqlite3_stmt **ppStmt,
12902 if( *pRc==SQLITE_OK ){
12905 va_start(ap, zFmt);
12906 z = sqlite3_vmprintf(zFmt, ap);
12909 *pRc = SQLITE_NOMEM;
12911 shellPrepare(db, pRc, z, ppStmt);
12917 static void shellFinalize(
12919 sqlite3_stmt *pStmt
12922 sqlite3 *db = sqlite3_db_handle(pStmt);
12923 int rc = sqlite3_finalize(pStmt);
12924 if( *pRc==SQLITE_OK ){
12925 if( rc!=SQLITE_OK ){
12926 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12933 static void shellReset(
12935 sqlite3_stmt *pStmt
12937 int rc = sqlite3_reset(pStmt);
12938 if( *pRc==SQLITE_OK ){
12939 if( rc!=SQLITE_OK ){
12940 sqlite3 *db = sqlite3_db_handle(pStmt);
12941 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
12947 ** Structure representing a single ".ar" command.
12949 typedef struct ArCommand ArCommand;
12951 u8 eCmd; /* An AR_CMD_* value */
12952 u8 bVerbose; /* True if --verbose */
12953 u8 bZip; /* True if the archive is a ZIP */
12954 u8 bDryRun; /* True if --dry-run */
12955 u8 bAppend; /* True if --append */
12956 u8 fromCmdLine; /* Run from -A instead of .archive */
12957 int nArg; /* Number of command arguments */
12958 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
12959 const char *zFile; /* --file argument, or NULL */
12960 const char *zDir; /* --directory argument, or NULL */
12961 char **azArg; /* Array of command arguments */
12962 ShellState *p; /* Shell state */
12963 sqlite3 *db; /* Database containing the archive */
12967 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
12969 static int arUsage(FILE *f){
12970 showHelp(f,"archive");
12971 return SQLITE_ERROR;
12975 ** Print an error message for the .ar command to stderr and return
12978 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
12981 va_start(ap, zFmt);
12982 z = sqlite3_vmprintf(zFmt, ap);
12984 utf8_printf(stderr, "Error: %s\n", z);
12985 if( pAr->fromCmdLine ){
12986 utf8_printf(stderr, "Use \"-A\" for more help\n");
12988 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
12991 return SQLITE_ERROR;
12995 ** Values for ArCommand.eCmd.
12997 #define AR_CMD_CREATE 1
12998 #define AR_CMD_EXTRACT 2
12999 #define AR_CMD_LIST 3
13000 #define AR_CMD_UPDATE 4
13001 #define AR_CMD_HELP 5
13004 ** Other (non-command) switches.
13006 #define AR_SWITCH_VERBOSE 6
13007 #define AR_SWITCH_FILE 7
13008 #define AR_SWITCH_DIRECTORY 8
13009 #define AR_SWITCH_APPEND 9
13010 #define AR_SWITCH_DRYRUN 10
13012 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
13014 case AR_CMD_CREATE:
13015 case AR_CMD_EXTRACT:
13017 case AR_CMD_UPDATE:
13020 return arErrorMsg(pAr, "multiple command options");
13022 pAr->eCmd = eSwitch;
13025 case AR_SWITCH_DRYRUN:
13028 case AR_SWITCH_VERBOSE:
13031 case AR_SWITCH_APPEND:
13033 /* Fall thru into --file */
13034 case AR_SWITCH_FILE:
13037 case AR_SWITCH_DIRECTORY:
13046 ** Parse the command line for an ".ar" command. The results are written into
13047 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
13048 ** successfully, otherwise an error message is written to stderr and
13049 ** SQLITE_ERROR returned.
13051 static int arParseCommand(
13052 char **azArg, /* Array of arguments passed to dot command */
13053 int nArg, /* Number of entries in azArg[] */
13054 ArCommand *pAr /* Populate this object */
13062 { "create", 'c', AR_CMD_CREATE, 0 },
13063 { "extract", 'x', AR_CMD_EXTRACT, 0 },
13064 { "list", 't', AR_CMD_LIST, 0 },
13065 { "update", 'u', AR_CMD_UPDATE, 0 },
13066 { "help", 'h', AR_CMD_HELP, 0 },
13067 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
13068 { "file", 'f', AR_SWITCH_FILE, 1 },
13069 { "append", 'a', AR_SWITCH_APPEND, 1 },
13070 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
13071 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
13073 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
13074 struct ArSwitch *pEnd = &aSwitch[nSwitch];
13077 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
13078 return arUsage(stderr);
13080 char *z = azArg[1];
13082 /* Traditional style [tar] invocation */
13085 for(i=0; z[i]; i++){
13086 const char *zArg = 0;
13087 struct ArSwitch *pOpt;
13088 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13089 if( z[i]==pOpt->cShort ) break;
13092 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13096 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13098 zArg = azArg[iArg++];
13100 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13102 pAr->nArg = nArg-iArg;
13104 pAr->azArg = &azArg[iArg];
13107 /* Non-traditional invocation */
13109 for(iArg=1; iArg<nArg; iArg++){
13113 /* All remaining command line words are command arguments. */
13114 pAr->azArg = &azArg[iArg];
13115 pAr->nArg = nArg-iArg;
13122 /* One or more short options */
13123 for(i=1; i<n; i++){
13124 const char *zArg = 0;
13125 struct ArSwitch *pOpt;
13126 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13127 if( z[i]==pOpt->cShort ) break;
13130 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
13137 if( iArg>=(nArg-1) ){
13138 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
13140 zArg = azArg[++iArg];
13143 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
13145 }else if( z[2]=='\0' ){
13146 /* A -- option, indicating that all remaining command line words
13147 ** are command arguments. */
13148 pAr->azArg = &azArg[iArg+1];
13149 pAr->nArg = nArg-iArg-1;
13152 /* A long option */
13153 const char *zArg = 0; /* Argument for option, if any */
13154 struct ArSwitch *pMatch = 0; /* Matching option */
13155 struct ArSwitch *pOpt; /* Iterator */
13156 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
13157 const char *zLong = pOpt->zLong;
13158 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
13160 return arErrorMsg(pAr, "ambiguous option: %s",z);
13168 return arErrorMsg(pAr, "unrecognized option: %s", z);
13170 if( pMatch->bArg ){
13171 if( iArg>=(nArg-1) ){
13172 return arErrorMsg(pAr, "option requires an argument: %s", z);
13174 zArg = azArg[++iArg];
13176 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
13186 ** This function assumes that all arguments within the ArCommand.azArg[]
13187 ** array refer to archive members, as for the --extract or --list commands.
13188 ** It checks that each of them are present. If any specified file is not
13189 ** present in the archive, an error is printed to stderr and an error
13190 ** code returned. Otherwise, if all specified arguments are present in
13191 ** the archive, SQLITE_OK is returned.
13193 ** This function strips any trailing '/' characters from each argument.
13194 ** This is consistent with the way the [tar] command seems to work on
13197 static int arCheckEntries(ArCommand *pAr){
13198 int rc = SQLITE_OK;
13201 sqlite3_stmt *pTest = 0;
13203 shellPreparePrintf(pAr->db, &rc, &pTest,
13204 "SELECT name FROM %s WHERE name=$name",
13207 j = sqlite3_bind_parameter_index(pTest, "$name");
13208 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13209 char *z = pAr->azArg[i];
13210 int n = strlen30(z);
13212 while( n>0 && z[n-1]=='/' ) n--;
13214 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
13215 if( SQLITE_ROW==sqlite3_step(pTest) ){
13218 shellReset(&rc, pTest);
13219 if( rc==SQLITE_OK && bOk==0 ){
13220 utf8_printf(stderr, "not found in archive: %s\n", z);
13224 shellFinalize(&rc, pTest);
13230 ** Format a WHERE clause that can be used against the "sqlar" table to
13231 ** identify all archive members that match the command arguments held
13232 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
13233 ** The caller is responsible for eventually calling sqlite3_free() on
13234 ** any non-NULL (*pzWhere) value.
13236 static void arWhereClause(
13239 char **pzWhere /* OUT: New WHERE clause */
13242 if( *pRc==SQLITE_OK ){
13243 if( pAr->nArg==0 ){
13244 zWhere = sqlite3_mprintf("1");
13247 const char *zSep = "";
13248 for(i=0; i<pAr->nArg; i++){
13249 const char *z = pAr->azArg[i];
13250 zWhere = sqlite3_mprintf(
13251 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
13252 zWhere, zSep, z, strlen30(z)+1, z
13255 *pRc = SQLITE_NOMEM;
13266 ** Implementation of .ar "lisT" command.
13268 static int arListCommand(ArCommand *pAr){
13269 const char *zSql = "SELECT %s FROM %s WHERE %s";
13270 const char *azCols[] = {
13272 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
13276 sqlite3_stmt *pSql = 0;
13279 rc = arCheckEntries(pAr);
13280 arWhereClause(&rc, pAr, &zWhere);
13282 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
13283 pAr->zSrcTable, zWhere);
13284 if( pAr->bDryRun ){
13285 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13287 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13288 if( pAr->bVerbose ){
13289 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
13290 sqlite3_column_text(pSql, 0),
13291 sqlite3_column_int(pSql, 1),
13292 sqlite3_column_text(pSql, 2),
13293 sqlite3_column_text(pSql, 3)
13296 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13300 shellFinalize(&rc, pSql);
13301 sqlite3_free(zWhere);
13307 ** Implementation of .ar "eXtract" command.
13309 static int arExtractCommand(ArCommand *pAr){
13310 const char *zSql1 =
13313 " writefile(($dir || name), %s, mode, mtime) "
13314 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
13315 " AND name NOT GLOB '*..[/\\]*'";
13317 const char *azExtraArg[] = {
13318 "sqlar_uncompress(data, sz)",
13322 sqlite3_stmt *pSql = 0;
13323 int rc = SQLITE_OK;
13328 /* If arguments are specified, check that they actually exist within
13329 ** the archive before proceeding. And formulate a WHERE clause to
13331 rc = arCheckEntries(pAr);
13332 arWhereClause(&rc, pAr, &zWhere);
13334 if( rc==SQLITE_OK ){
13336 zDir = sqlite3_mprintf("%s/", pAr->zDir);
13338 zDir = sqlite3_mprintf("");
13340 if( zDir==0 ) rc = SQLITE_NOMEM;
13343 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
13344 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
13347 if( rc==SQLITE_OK ){
13348 j = sqlite3_bind_parameter_index(pSql, "$dir");
13349 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
13351 /* Run the SELECT statement twice. The first time, writefile() is called
13352 ** for all archive members that should be extracted. The second time,
13353 ** only for the directories. This is because the timestamps for
13354 ** extracted directories must be reset after they are populated (as
13355 ** populating them changes the timestamp). */
13356 for(i=0; i<2; i++){
13357 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
13358 sqlite3_bind_int(pSql, j, i);
13359 if( pAr->bDryRun ){
13360 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
13362 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
13363 if( i==0 && pAr->bVerbose ){
13364 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
13368 shellReset(&rc, pSql);
13370 shellFinalize(&rc, pSql);
13373 sqlite3_free(zDir);
13374 sqlite3_free(zWhere);
13379 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
13381 static int arExecSql(ArCommand *pAr, const char *zSql){
13383 if( pAr->bDryRun ){
13384 utf8_printf(pAr->p->out, "%s\n", zSql);
13388 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
13390 utf8_printf(stdout, "ERROR: %s\n", zErr);
13391 sqlite3_free(zErr);
13399 ** Implementation of .ar "create" and "update" commands.
13401 ** Create the "sqlar" table in the database if it does not already exist.
13402 ** Then add each file in the azFile[] array to the archive. Directories
13403 ** are added recursively. If argument bVerbose is non-zero, a message is
13404 ** printed on stdout for each file archived.
13406 ** The create command is the same as update, except that it drops
13407 ** any existing "sqlar" table before beginning.
13409 static int arCreateOrUpdateCommand(
13410 ArCommand *pAr, /* Command arguments and options */
13411 int bUpdate /* true for a --create. false for --update */
13413 const char *zCreate =
13414 "CREATE TABLE IF NOT EXISTS sqlar(\n"
13415 " name TEXT PRIMARY KEY, -- name of the file\n"
13416 " mode INT, -- access permissions\n"
13417 " mtime INT, -- last modification time\n"
13418 " sz INT, -- original file size\n"
13419 " data BLOB -- compressed content\n"
13421 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
13422 const char *zInsertFmt[2] = {
13423 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
13428 " CASE substr(lsmode(mode),1,1)\n"
13429 " WHEN '-' THEN length(data)\n"
13430 " WHEN 'd' THEN 0\n"
13432 " sqlar_compress(data)\n"
13433 " FROM fsdir(%Q,%Q)\n"
13434 " WHERE lsmode(mode) NOT LIKE '?%%';",
13435 "REPLACE INTO %s(name,mode,mtime,data)\n"
13441 " FROM fsdir(%Q,%Q)\n"
13442 " WHERE lsmode(mode) NOT LIKE '?%%';"
13444 int i; /* For iterating through azFile[] */
13445 int rc; /* Return code */
13446 const char *zTab = 0; /* SQL table into which to insert */
13450 arExecSql(pAr, "PRAGMA page_size=512");
13451 rc = arExecSql(pAr, "SAVEPOINT ar;");
13452 if( rc!=SQLITE_OK ) return rc;
13455 /* Initialize the zipfile virtual table, if necessary */
13458 sqlite3_randomness(sizeof(r),&r);
13459 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
13461 zSql = sqlite3_mprintf(
13462 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
13465 rc = arExecSql(pAr, zSql);
13466 sqlite3_free(zSql);
13471 /* Initialize the table for an SQLAR */
13474 rc = arExecSql(pAr, zDrop);
13475 if( rc!=SQLITE_OK ) goto end_ar_transaction;
13477 rc = arExecSql(pAr, zCreate);
13479 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
13480 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
13481 pAr->bVerbose ? "shell_putsnl(name)" : "name",
13482 pAr->azArg[i], pAr->zDir);
13483 rc = arExecSql(pAr, zSql2);
13484 sqlite3_free(zSql2);
13486 end_ar_transaction:
13487 if( rc!=SQLITE_OK ){
13488 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
13490 rc = arExecSql(pAr, "RELEASE ar;");
13491 if( pAr->bZip && pAr->zFile ){
13492 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
13493 arExecSql(pAr, zSql);
13494 sqlite3_free(zSql);
13501 ** Implementation of ".ar" dot command.
13503 static int arDotCommand(
13504 ShellState *pState, /* Current shell tool state */
13505 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
13506 char **azArg, /* Array of arguments passed to dot command */
13507 int nArg /* Number of entries in azArg[] */
13511 memset(&cmd, 0, sizeof(cmd));
13512 cmd.fromCmdLine = fromCmdLine;
13513 rc = arParseCommand(azArg, nArg, &cmd);
13514 if( rc==SQLITE_OK ){
13515 int eDbType = SHELL_OPEN_UNSPEC;
13517 cmd.db = pState->db;
13519 eDbType = deduceDatabaseType(cmd.zFile, 1);
13521 eDbType = pState->openMode;
13523 if( eDbType==SHELL_OPEN_ZIPFILE ){
13524 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
13525 if( cmd.zFile==0 ){
13526 cmd.zSrcTable = sqlite3_mprintf("zip");
13528 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
13532 }else if( cmd.zFile ){
13534 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
13535 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
13536 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
13538 flags = SQLITE_OPEN_READONLY;
13542 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
13543 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
13545 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
13546 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
13547 if( rc!=SQLITE_OK ){
13548 utf8_printf(stderr, "cannot open file: %s (%s)\n",
13549 cmd.zFile, sqlite3_errmsg(cmd.db)
13551 goto end_ar_command;
13553 sqlite3_fileio_init(cmd.db, 0, 0);
13554 sqlite3_sqlar_init(cmd.db, 0, 0);
13555 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
13556 shellPutsFunc, 0, 0);
13559 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
13560 if( cmd.eCmd!=AR_CMD_CREATE
13561 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
13563 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
13565 goto end_ar_command;
13567 cmd.zSrcTable = sqlite3_mprintf("sqlar");
13570 switch( cmd.eCmd ){
13571 case AR_CMD_CREATE:
13572 rc = arCreateOrUpdateCommand(&cmd, 0);
13575 case AR_CMD_EXTRACT:
13576 rc = arExtractCommand(&cmd);
13580 rc = arListCommand(&cmd);
13584 arUsage(pState->out);
13588 assert( cmd.eCmd==AR_CMD_UPDATE );
13589 rc = arCreateOrUpdateCommand(&cmd, 1);
13594 if( cmd.db!=pState->db ){
13597 sqlite3_free(cmd.zSrcTable);
13601 /* End of the ".archive" or ".ar" command logic
13602 **********************************************************************************/
13603 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
13607 ** If an input line begins with "." then invoke this routine to
13608 ** process that line.
13610 ** Return 1 on error, 2 to exit, and 0 otherwise.
13612 static int do_meta_command(char *zLine, ShellState *p){
13619 #ifndef SQLITE_OMIT_VIRTUALTABLE
13620 if( p->expert.pExpert ){
13621 expertFinish(p, 1, 0);
13625 /* Parse the input line into tokens.
13627 while( zLine[h] && nArg<ArraySize(azArg) ){
13628 while( IsSpace(zLine[h]) ){ h++; }
13629 if( zLine[h]==0 ) break;
13630 if( zLine[h]=='\'' || zLine[h]=='"' ){
13631 int delim = zLine[h++];
13632 azArg[nArg++] = &zLine[h];
13633 while( zLine[h] && zLine[h]!=delim ){
13634 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
13637 if( zLine[h]==delim ){
13640 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
13642 azArg[nArg++] = &zLine[h];
13643 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
13644 if( zLine[h] ) zLine[h++] = 0;
13645 resolve_backslashes(azArg[nArg-1]);
13649 /* Process the input line.
13651 if( nArg==0 ) return 0; /* no tokens, no error */
13652 n = strlen30(azArg[0]);
13656 #ifndef SQLITE_OMIT_AUTHORIZATION
13657 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
13659 raw_printf(stderr, "Usage: .auth ON|OFF\n");
13661 goto meta_command_exit;
13664 if( booleanValue(azArg[1]) ){
13665 sqlite3_set_authorizer(p->db, shellAuth, p);
13667 sqlite3_set_authorizer(p->db, 0, 0);
13672 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
13673 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
13675 rc = arDotCommand(p, 0, azArg, nArg);
13679 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
13680 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
13682 const char *zDestFile = 0;
13683 const char *zDb = 0;
13685 sqlite3_backup *pBackup;
13688 const char *zVfs = 0;
13689 for(j=1; j<nArg; j++){
13690 const char *z = azArg[j];
13692 if( z[1]=='-' ) z++;
13693 if( strcmp(z, "-append")==0 ){
13696 if( strcmp(z, "-async")==0 ){
13700 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
13703 }else if( zDestFile==0 ){
13704 zDestFile = azArg[j];
13705 }else if( zDb==0 ){
13707 zDestFile = azArg[j];
13709 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
13713 if( zDestFile==0 ){
13714 raw_printf(stderr, "missing FILENAME argument on .backup\n");
13717 if( zDb==0 ) zDb = "main";
13718 rc = sqlite3_open_v2(zDestFile, &pDest,
13719 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
13720 if( rc!=SQLITE_OK ){
13721 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
13726 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
13730 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
13732 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13736 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
13737 sqlite3_backup_finish(pBackup);
13738 if( rc==SQLITE_DONE ){
13741 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
13747 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
13749 bail_on_error = booleanValue(azArg[1]);
13751 raw_printf(stderr, "Usage: .bail on|off\n");
13756 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
13758 if( booleanValue(azArg[1]) ){
13759 setBinaryMode(p->out, 1);
13761 setTextMode(p->out, 1);
13764 raw_printf(stderr, "Usage: .binary on|off\n");
13769 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
13771 #if defined(_WIN32) || defined(WIN32)
13772 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
13773 rc = !SetCurrentDirectoryW(z);
13776 rc = chdir(azArg[1]);
13779 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
13783 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
13788 /* The undocumented ".breakpoint" command causes a call to the no-op
13789 ** routine named test_breakpoint().
13791 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
13795 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
13797 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
13799 raw_printf(stderr, "Usage: .changes on|off\n");
13804 /* Cancel output redirection, if it is currently set (by .testcase)
13805 ** Then read the content of the testcase-out.txt file and compare against
13806 ** azArg[1]. If there are differences, report an error and exit.
13808 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
13812 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
13814 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
13815 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
13817 }else if( testcase_glob(azArg[1],zRes)==0 ){
13818 utf8_printf(stderr,
13819 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
13820 p->zTestcase, azArg[1], zRes);
13823 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
13826 sqlite3_free(zRes);
13829 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
13831 tryToClone(p, azArg[1]);
13833 raw_printf(stderr, "Usage: .clone FILENAME\n");
13838 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
13842 memcpy(&data, p, sizeof(data));
13843 data.showHeader = 0;
13844 data.cMode = data.mode = MODE_List;
13845 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
13847 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
13848 callback, &data, &zErrMsg);
13850 utf8_printf(stderr,"Error: %s\n", zErrMsg);
13851 sqlite3_free(zErrMsg);
13856 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
13857 static const struct DbConfigChoices {
13861 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
13862 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
13863 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
13864 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
13865 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
13866 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
13867 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
13868 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
13869 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
13873 for(ii=0; ii<ArraySize(aDbConfig); ii++){
13874 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
13876 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
13878 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
13879 utf8_printf(p->out, "%18s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
13880 if( nArg>1 ) break;
13882 if( nArg>1 && ii==ArraySize(aDbConfig) ){
13883 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
13884 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
13888 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
13889 rc = shell_dbinfo_command(p, nArg, azArg);
13892 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
13893 const char *zLike = 0;
13895 int savedShowHeader = p->showHeader;
13896 int savedShellFlags = p->shellFlgs;
13897 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
13898 for(i=1; i<nArg; i++){
13899 if( azArg[i][0]=='-' ){
13900 const char *z = azArg[i]+1;
13901 if( z[0]=='-' ) z++;
13902 if( strcmp(z,"preserve-rowids")==0 ){
13903 #ifdef SQLITE_OMIT_VIRTUALTABLE
13904 raw_printf(stderr, "The --preserve-rowids option is not compatible"
13905 " with SQLITE_OMIT_VIRTUALTABLE\n");
13907 goto meta_command_exit;
13909 ShellSetFlag(p, SHFLG_PreserveRowid);
13912 if( strcmp(z,"newlines")==0 ){
13913 ShellSetFlag(p, SHFLG_Newlines);
13916 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
13918 goto meta_command_exit;
13921 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
13922 "?--newlines? ?LIKE-PATTERN?\n");
13924 goto meta_command_exit;
13930 /* When playing back a "dump", the content might appear in an order
13931 ** which causes immediate foreign key constraints to be violated.
13932 ** So disable foreign-key constraint enforcement to prevent problems. */
13933 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
13934 raw_printf(p->out, "BEGIN TRANSACTION;\n");
13935 p->writableSchema = 0;
13937 /* Set writable_schema=ON since doing so forces SQLite to initialize
13938 ** as much of the schema as it can even if the sqlite_master table is
13940 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
13943 run_schema_dump_query(p,
13944 "SELECT name, type, sql FROM sqlite_master "
13945 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
13947 run_schema_dump_query(p,
13948 "SELECT name, type, sql FROM sqlite_master "
13949 "WHERE name=='sqlite_sequence'"
13951 run_table_dump_query(p,
13952 "SELECT sql FROM sqlite_master "
13953 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
13957 zSql = sqlite3_mprintf(
13958 "SELECT name, type, sql FROM sqlite_master "
13959 "WHERE tbl_name LIKE %Q AND type=='table'"
13960 " AND sql NOT NULL", zLike);
13961 run_schema_dump_query(p,zSql);
13962 sqlite3_free(zSql);
13963 zSql = sqlite3_mprintf(
13964 "SELECT sql FROM sqlite_master "
13965 "WHERE sql NOT NULL"
13966 " AND type IN ('index','trigger','view')"
13967 " AND tbl_name LIKE %Q", zLike);
13968 run_table_dump_query(p, zSql, 0);
13969 sqlite3_free(zSql);
13971 if( p->writableSchema ){
13972 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
13973 p->writableSchema = 0;
13975 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13976 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
13977 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
13978 p->showHeader = savedShowHeader;
13979 p->shellFlgs = savedShellFlags;
13982 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
13984 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
13986 raw_printf(stderr, "Usage: .echo on|off\n");
13991 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
13993 p->autoEQPtest = 0;
13994 if( p->autoEQPtrace ){
13995 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
13996 p->autoEQPtrace = 0;
13998 if( strcmp(azArg[1],"full")==0 ){
13999 p->autoEQP = AUTOEQP_full;
14000 }else if( strcmp(azArg[1],"trigger")==0 ){
14001 p->autoEQP = AUTOEQP_trigger;
14002 #ifdef SQLITE_DEBUG
14003 }else if( strcmp(azArg[1],"test")==0 ){
14004 p->autoEQP = AUTOEQP_on;
14005 p->autoEQPtest = 1;
14006 }else if( strcmp(azArg[1],"trace")==0 ){
14007 p->autoEQP = AUTOEQP_full;
14008 p->autoEQPtrace = 1;
14010 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
14011 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
14014 p->autoEQP = (u8)booleanValue(azArg[1]);
14017 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
14022 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
14023 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
14027 /* The ".explain" command is automatic now. It is largely pointless. It
14028 ** retained purely for backwards compatibility */
14029 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
14032 if( strcmp(azArg[1],"auto")==0 ){
14035 val = booleanValue(azArg[1]);
14038 if( val==1 && p->mode!=MODE_Explain ){
14039 p->normalMode = p->mode;
14040 p->mode = MODE_Explain;
14041 p->autoExplain = 0;
14042 }else if( val==0 ){
14043 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14044 p->autoExplain = 0;
14045 }else if( val==99 ){
14046 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
14047 p->autoExplain = 1;
14051 #ifndef SQLITE_OMIT_VIRTUALTABLE
14052 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
14054 expertDotCommand(p, azArg, nArg);
14058 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
14062 memcpy(&data, p, sizeof(data));
14063 data.showHeader = 0;
14064 data.cMode = data.mode = MODE_Semi;
14065 if( nArg==2 && optionMatch(azArg[1], "indent") ){
14066 data.cMode = data.mode = MODE_Pretty;
14070 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
14072 goto meta_command_exit;
14075 rc = sqlite3_exec(p->db,
14077 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
14078 " FROM sqlite_master UNION ALL"
14079 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
14080 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
14082 callback, &data, &zErrMsg
14084 if( rc==SQLITE_OK ){
14085 sqlite3_stmt *pStmt;
14086 rc = sqlite3_prepare_v2(p->db,
14087 "SELECT rowid FROM sqlite_master"
14088 " WHERE name GLOB 'sqlite_stat[134]'",
14090 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
14091 sqlite3_finalize(pStmt);
14094 raw_printf(p->out, "/* No STAT tables available */\n");
14096 raw_printf(p->out, "ANALYZE sqlite_master;\n");
14097 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
14098 callback, &data, &zErrMsg);
14099 data.cMode = data.mode = MODE_Insert;
14100 data.zDestTable = "sqlite_stat1";
14101 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
14102 data.zDestTable = "sqlite_stat3";
14103 shell_exec(&data, "SELECT * FROM sqlite_stat3", &zErrMsg);
14104 data.zDestTable = "sqlite_stat4";
14105 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
14106 raw_printf(p->out, "ANALYZE sqlite_master;\n");
14110 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
14112 p->showHeader = booleanValue(azArg[1]);
14114 raw_printf(stderr, "Usage: .headers on|off\n");
14119 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
14121 n = showHelp(p->out, azArg[1]);
14123 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
14126 showHelp(p->out, 0);
14130 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
14131 char *zTable; /* Insert data into this table */
14132 char *zFile; /* Name of file to extra content from */
14133 sqlite3_stmt *pStmt = NULL; /* A statement */
14134 int nCol; /* Number of columns in the table */
14135 int nByte; /* Number of bytes in an SQL string */
14136 int i, j; /* Loop counters */
14137 int needCommit; /* True to COMMIT or ROLLBACK at end */
14138 int nSep; /* Number of bytes in p->colSeparator[] */
14139 char *zSql; /* An SQL statement */
14140 ImportCtx sCtx; /* Reader context */
14141 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
14142 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
14145 raw_printf(stderr, "Usage: .import FILE TABLE\n");
14146 goto meta_command_exit;
14151 memset(&sCtx, 0, sizeof(sCtx));
14153 nSep = strlen30(p->colSeparator);
14156 "Error: non-null column separator required for import\n");
14160 raw_printf(stderr, "Error: multi-character column separators not allowed"
14164 nSep = strlen30(p->rowSeparator);
14166 raw_printf(stderr, "Error: non-null row separator required for import\n");
14169 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
14170 /* When importing CSV (only), if the row separator is set to the
14171 ** default output row separator, change it to the default input
14172 ** row separator. This avoids having to maintain different input
14173 ** and output row separators. */
14174 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14175 nSep = strlen30(p->rowSeparator);
14178 raw_printf(stderr, "Error: multi-character row separators not allowed"
14182 sCtx.zFile = zFile;
14184 if( sCtx.zFile[0]=='|' ){
14185 #ifdef SQLITE_OMIT_POPEN
14186 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14189 sCtx.in = popen(sCtx.zFile+1, "r");
14190 sCtx.zFile = "<pipe>";
14194 sCtx.in = fopen(sCtx.zFile, "rb");
14197 if( p->mode==MODE_Ascii ){
14198 xRead = ascii_read_one_field;
14200 xRead = csv_read_one_field;
14203 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14206 sCtx.cColSep = p->colSeparator[0];
14207 sCtx.cRowSep = p->rowSeparator[0];
14208 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
14211 shell_out_of_memory();
14213 nByte = strlen30(zSql);
14214 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14215 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
14216 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
14217 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
14219 while( xRead(&sCtx) ){
14220 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
14222 if( sCtx.cTerm!=sCtx.cColSep ) break;
14225 sqlite3_free(zCreate);
14226 sqlite3_free(sCtx.z);
14228 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
14231 zCreate = sqlite3_mprintf("%z\n)", zCreate);
14232 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
14233 sqlite3_free(zCreate);
14235 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
14236 sqlite3_errmsg(p->db));
14237 sqlite3_free(sCtx.z);
14241 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14243 sqlite3_free(zSql);
14245 if (pStmt) sqlite3_finalize(pStmt);
14246 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
14250 nCol = sqlite3_column_count(pStmt);
14251 sqlite3_finalize(pStmt);
14253 if( nCol==0 ) return 0; /* no columns, no error */
14254 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
14257 shell_out_of_memory();
14259 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
14260 j = strlen30(zSql);
14261 for(i=1; i<nCol; i++){
14267 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14268 sqlite3_free(zSql);
14270 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14271 if (pStmt) sqlite3_finalize(pStmt);
14275 needCommit = sqlite3_get_autocommit(p->db);
14276 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
14278 int startLine = sCtx.nLine;
14279 for(i=0; i<nCol; i++){
14280 char *z = xRead(&sCtx);
14282 ** Did we reach end-of-file before finding any columns?
14283 ** If so, stop instead of NULL filling the remaining columns.
14285 if( z==0 && i==0 ) break;
14287 ** Did we reach end-of-file OR end-of-line before finding any
14288 ** columns in ASCII mode? If so, stop instead of NULL filling
14289 ** the remaining columns.
14291 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
14292 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
14293 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
14294 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14295 "filling the rest with NULL\n",
14296 sCtx.zFile, startLine, nCol, i+1);
14298 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
14301 if( sCtx.cTerm==sCtx.cColSep ){
14305 }while( sCtx.cTerm==sCtx.cColSep );
14306 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
14307 "extras ignored\n",
14308 sCtx.zFile, startLine, nCol, i);
14311 sqlite3_step(pStmt);
14312 rc = sqlite3_reset(pStmt);
14313 if( rc!=SQLITE_OK ){
14314 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
14315 startLine, sqlite3_errmsg(p->db));
14318 }while( sCtx.cTerm!=EOF );
14321 sqlite3_free(sCtx.z);
14322 sqlite3_finalize(pStmt);
14323 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
14326 #ifndef SQLITE_UNTESTABLE
14327 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
14329 char *zCollist = 0;
14330 sqlite3_stmt *pStmt;
14333 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
14334 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
14335 " .imposter off\n");
14337 goto meta_command_exit;
14341 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
14342 goto meta_command_exit;
14344 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
14345 " WHERE name='%q' AND type='index'", azArg[1]);
14346 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14347 sqlite3_free(zSql);
14348 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14349 tnum = sqlite3_column_int(pStmt, 0);
14351 sqlite3_finalize(pStmt);
14353 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
14355 goto meta_command_exit;
14357 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
14358 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14359 sqlite3_free(zSql);
14361 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14363 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
14366 if( sqlite3_column_int(pStmt,1)==-1 ){
14369 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
14374 zCollist = sqlite3_mprintf("\"%w\"", zCol);
14376 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
14379 sqlite3_finalize(pStmt);
14380 zSql = sqlite3_mprintf(
14381 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
14382 azArg[2], zCollist, zCollist);
14383 sqlite3_free(zCollist);
14384 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
14385 if( rc==SQLITE_OK ){
14386 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
14387 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
14389 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
14391 utf8_printf(stdout, "%s;\n", zSql);
14393 "WARNING: writing to an imposter table will corrupt the index!\n"
14397 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
14400 sqlite3_free(zSql);
14402 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
14404 #ifdef SQLITE_ENABLE_IOTRACE
14405 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
14406 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
14407 if( iotrace && iotrace!=stdout ) fclose(iotrace);
14410 sqlite3IoTrace = 0;
14411 }else if( strcmp(azArg[1], "-")==0 ){
14412 sqlite3IoTrace = iotracePrintf;
14415 iotrace = fopen(azArg[1], "w");
14417 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
14418 sqlite3IoTrace = 0;
14421 sqlite3IoTrace = iotracePrintf;
14427 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
14428 static const struct {
14429 const char *zLimitName; /* Name of a limit */
14430 int limitCode; /* Integer code for that limit */
14432 { "length", SQLITE_LIMIT_LENGTH },
14433 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
14434 { "column", SQLITE_LIMIT_COLUMN },
14435 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
14436 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
14437 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
14438 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
14439 { "attached", SQLITE_LIMIT_ATTACHED },
14440 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
14441 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
14442 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
14443 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
14448 for(i=0; i<ArraySize(aLimit); i++){
14449 printf("%20s %d\n", aLimit[i].zLimitName,
14450 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
14452 }else if( nArg>3 ){
14453 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
14455 goto meta_command_exit;
14458 n2 = strlen30(azArg[1]);
14459 for(i=0; i<ArraySize(aLimit); i++){
14460 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
14464 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
14466 goto meta_command_exit;
14471 utf8_printf(stderr, "unknown limit: \"%s\"\n"
14472 "enter \".limits\" with no arguments for a list.\n",
14475 goto meta_command_exit;
14478 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
14479 (int)integerValue(azArg[2]));
14481 printf("%20s %d\n", aLimit[iLimit].zLimitName,
14482 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
14486 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
14488 lintDotCommand(p, azArg, nArg);
14491 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14492 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
14493 const char *zFile, *zProc;
14496 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
14498 goto meta_command_exit;
14501 zProc = nArg>=3 ? azArg[2] : 0;
14503 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
14504 if( rc!=SQLITE_OK ){
14505 utf8_printf(stderr, "Error: %s\n", zErrMsg);
14506 sqlite3_free(zErrMsg);
14512 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
14514 raw_printf(stderr, "Usage: .log FILENAME\n");
14517 const char *zFile = azArg[1];
14518 output_file_close(p->pLog);
14519 p->pLog = output_file_open(zFile, 0);
14523 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
14524 const char *zMode = nArg>=2 ? azArg[1] : "";
14525 int n2 = strlen30(zMode);
14527 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
14528 p->mode = MODE_Line;
14529 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14530 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
14531 p->mode = MODE_Column;
14532 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14533 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
14534 p->mode = MODE_List;
14535 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
14536 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14537 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
14538 p->mode = MODE_Html;
14539 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
14540 p->mode = MODE_Tcl;
14541 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
14542 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
14543 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
14544 p->mode = MODE_Csv;
14545 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14546 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14547 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
14548 p->mode = MODE_List;
14549 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
14550 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
14551 p->mode = MODE_Insert;
14552 set_table_name(p, nArg>=3 ? azArg[2] : "table");
14553 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
14554 p->mode = MODE_Quote;
14555 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
14556 p->mode = MODE_Ascii;
14557 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
14558 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
14559 }else if( nArg==1 ){
14560 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
14562 raw_printf(stderr, "Error: mode should be one of: "
14563 "ascii column csv html insert line list quote tabs tcl\n");
14566 p->cMode = p->mode;
14569 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
14571 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
14572 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
14574 raw_printf(stderr, "Usage: .nullvalue STRING\n");
14579 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
14580 char *zNewFilename; /* Name of the database file to open */
14581 int iName = 1; /* Index in azArg[] of the filename */
14582 int newFlag = 0; /* True to delete file before opening */
14583 /* Close the existing database */
14584 session_close_all(p);
14587 p->zDbFilename = 0;
14588 sqlite3_free(p->zFreeOnClose);
14589 p->zFreeOnClose = 0;
14590 p->openMode = SHELL_OPEN_UNSPEC;
14592 /* Check for command-line arguments */
14593 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
14594 const char *z = azArg[iName];
14595 if( optionMatch(z,"new") ){
14597 #ifdef SQLITE_HAVE_ZLIB
14598 }else if( optionMatch(z, "zip") ){
14599 p->openMode = SHELL_OPEN_ZIPFILE;
14601 }else if( optionMatch(z, "append") ){
14602 p->openMode = SHELL_OPEN_APPENDVFS;
14603 }else if( optionMatch(z, "readonly") ){
14604 p->openMode = SHELL_OPEN_READONLY;
14605 #ifdef SQLITE_ENABLE_DESERIALIZE
14606 }else if( optionMatch(z, "deserialize") ){
14607 p->openMode = SHELL_OPEN_DESERIALIZE;
14608 }else if( optionMatch(z, "hexdb") ){
14609 p->openMode = SHELL_OPEN_HEXDB;
14610 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
14611 p->szMax = integerValue(azArg[++iName]);
14612 #endif /* SQLITE_ENABLE_DESERIALIZE */
14613 }else if( z[0]=='-' ){
14614 utf8_printf(stderr, "unknown option: %s\n", z);
14616 goto meta_command_exit;
14619 /* If a filename is specified, try to open it first */
14620 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
14621 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
14622 if( newFlag ) shellDeleteFile(zNewFilename);
14623 p->zDbFilename = zNewFilename;
14624 open_db(p, OPEN_DB_KEEPALIVE);
14626 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
14627 sqlite3_free(zNewFilename);
14629 p->zFreeOnClose = zNewFilename;
14633 /* As a fall-back open a TEMP database */
14634 p->zDbFilename = 0;
14640 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
14641 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
14643 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
14645 if( azArg[0][0]=='e' ){
14646 /* Transform the ".excel" command into ".once -x" */
14649 zFile = azArg[1] = "-x";
14653 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
14655 goto meta_command_exit;
14657 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
14659 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
14661 goto meta_command_exit;
14668 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
14669 #ifndef SQLITE_NOHAVE_SYSTEM
14670 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
14673 if( zFile[1]=='x' ){
14674 newTempFile(p, "csv");
14675 p->mode = MODE_Csv;
14676 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
14677 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
14679 newTempFile(p, "txt");
14682 zFile = p->zTempFile;
14684 #endif /* SQLITE_NOHAVE_SYSTEM */
14685 if( zFile[0]=='|' ){
14686 #ifdef SQLITE_OMIT_POPEN
14687 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
14691 p->out = popen(zFile + 1, "w");
14693 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
14697 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14701 p->out = output_file_open(zFile, bTxtMode);
14703 if( strcmp(zFile,"off")!=0 ){
14704 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
14709 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
14714 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
14716 for(i=1; i<nArg; i++){
14717 if( i>1 ) raw_printf(p->out, " ");
14718 utf8_printf(p->out, "%s", azArg[i]);
14720 raw_printf(p->out, "\n");
14723 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14724 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
14727 p->flgProgress = 0;
14730 for(i=1; i<nArg; i++){
14731 const char *z = azArg[i];
14734 if( z[0]=='-' ) z++;
14735 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
14736 p->flgProgress |= SHELL_PROGRESS_QUIET;
14739 if( strcmp(z,"reset")==0 ){
14740 p->flgProgress |= SHELL_PROGRESS_RESET;
14743 if( strcmp(z,"once")==0 ){
14744 p->flgProgress |= SHELL_PROGRESS_ONCE;
14747 if( strcmp(z,"limit")==0 ){
14749 utf8_printf(stderr, "Error: missing argument on --limit\n");
14751 goto meta_command_exit;
14753 p->mxProgress = (int)integerValue(azArg[++i]);
14757 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
14759 goto meta_command_exit;
14761 nn = (int)integerValue(z);
14765 sqlite3_progress_handler(p->db, nn, progress_handler, p);
14767 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
14769 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
14771 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
14774 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
14778 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
14782 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
14783 FILE *inSaved = p->in;
14784 int savedLineno = p->lineno;
14786 raw_printf(stderr, "Usage: .read FILE\n");
14788 goto meta_command_exit;
14790 p->in = fopen(azArg[1], "rb");
14792 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
14795 rc = process_input(p);
14799 p->lineno = savedLineno;
14802 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
14803 const char *zSrcFile;
14806 sqlite3_backup *pBackup;
14810 zSrcFile = azArg[1];
14812 }else if( nArg==3 ){
14813 zSrcFile = azArg[2];
14816 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
14818 goto meta_command_exit;
14820 rc = sqlite3_open(zSrcFile, &pSrc);
14821 if( rc!=SQLITE_OK ){
14822 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
14827 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
14829 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14833 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
14834 || rc==SQLITE_BUSY ){
14835 if( rc==SQLITE_BUSY ){
14836 if( nTimeout++ >= 3 ) break;
14837 sqlite3_sleep(100);
14840 sqlite3_backup_finish(pBackup);
14841 if( rc==SQLITE_DONE ){
14843 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
14844 raw_printf(stderr, "Error: source database is busy\n");
14847 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14853 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
14855 p->scanstatsOn = (u8)booleanValue(azArg[1]);
14856 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
14857 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
14860 raw_printf(stderr, "Usage: .scanstats on|off\n");
14865 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
14869 const char *zDiv = "(";
14870 const char *zName = 0;
14876 memcpy(&data, p, sizeof(data));
14877 data.showHeader = 0;
14878 data.cMode = data.mode = MODE_Semi;
14879 initText(&sSelect);
14880 for(ii=1; ii<nArg; ii++){
14881 if( optionMatch(azArg[ii],"indent") ){
14882 data.cMode = data.mode = MODE_Pretty;
14883 }else if( optionMatch(azArg[ii],"debug") ){
14885 }else if( zName==0 ){
14888 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
14890 goto meta_command_exit;
14894 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
14895 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
14896 char *new_argv[2], *new_colv[2];
14897 new_argv[0] = sqlite3_mprintf(
14898 "CREATE TABLE %s (\n"
14901 " tbl_name text,\n"
14902 " rootpage integer,\n"
14904 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
14906 new_colv[0] = "sql";
14908 callback(&data, 1, new_argv, new_colv);
14909 sqlite3_free(new_argv[0]);
14913 sqlite3_stmt *pStmt = 0;
14914 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
14917 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
14918 sqlite3_finalize(pStmt);
14920 goto meta_command_exit;
14922 appendText(&sSelect, "SELECT sql FROM", 0);
14924 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14925 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
14927 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
14928 appendText(&sSelect, zDiv, 0);
14929 zDiv = " UNION ALL ";
14930 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
14931 if( sqlite3_stricmp(zDb, "main")!=0 ){
14932 appendText(&sSelect, zDb, '"');
14934 appendText(&sSelect, "NULL", 0);
14936 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
14937 appendText(&sSelect, zScNum, 0);
14938 appendText(&sSelect, " AS snum, ", 0);
14939 appendText(&sSelect, zDb, '\'');
14940 appendText(&sSelect, " AS sname FROM ", 0);
14941 appendText(&sSelect, zDb, '"');
14942 appendText(&sSelect, ".sqlite_master", 0);
14944 sqlite3_finalize(pStmt);
14945 #ifdef SQLITE_INTROSPECTION_PRAGMAS
14947 appendText(&sSelect,
14948 " UNION ALL SELECT shell_module_schema(name),"
14949 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
14952 appendText(&sSelect, ") WHERE ", 0);
14954 char *zQarg = sqlite3_mprintf("%Q", zName);
14955 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
14956 strchr(zName, '[') != 0;
14957 if( strchr(zName, '.') ){
14958 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
14960 appendText(&sSelect, "lower(tbl_name)", 0);
14962 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
14963 appendText(&sSelect, zQarg, 0);
14965 appendText(&sSelect, " ESCAPE '\\' ", 0);
14967 appendText(&sSelect, " AND ", 0);
14968 sqlite3_free(zQarg);
14970 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
14971 " ORDER BY snum, rowid", 0);
14973 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
14975 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
14977 freeText(&sSelect);
14980 utf8_printf(stderr,"Error: %s\n", zErrMsg);
14981 sqlite3_free(zErrMsg);
14983 }else if( rc != SQLITE_OK ){
14984 raw_printf(stderr,"Error: querying schema information\n");
14991 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
14992 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
14993 sqlite3SelectTrace = (int)integerValue(azArg[1]);
14997 #if defined(SQLITE_ENABLE_SESSION)
14998 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
14999 OpenSession *pSession = &p->aSession[0];
15000 char **azCmd = &azArg[1];
15002 int nCmd = nArg - 1;
15004 if( nArg<=1 ) goto session_syntax_error;
15007 for(iSes=0; iSes<p->nSession; iSes++){
15008 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
15010 if( iSes<p->nSession ){
15011 pSession = &p->aSession[iSes];
15015 pSession = &p->aSession[0];
15020 /* .session attach TABLE
15021 ** Invoke the sqlite3session_attach() interface to attach a particular
15022 ** table so that it is never filtered.
15024 if( strcmp(azCmd[0],"attach")==0 ){
15025 if( nCmd!=2 ) goto session_syntax_error;
15026 if( pSession->p==0 ){
15028 raw_printf(stderr, "ERROR: No sessions are open\n");
15030 rc = sqlite3session_attach(pSession->p, azCmd[1]);
15032 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
15038 /* .session changeset FILE
15039 ** .session patchset FILE
15040 ** Write a changeset or patchset into a file. The file is overwritten.
15042 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
15044 if( nCmd!=2 ) goto session_syntax_error;
15045 if( pSession->p==0 ) goto session_not_open;
15046 out = fopen(azCmd[1], "wb");
15048 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
15052 if( azCmd[0][0]=='c' ){
15053 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
15055 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
15058 printf("Error: error code %d\n", rc);
15062 && fwrite(pChng, szChng, 1, out)!=1 ){
15063 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
15066 sqlite3_free(pChng);
15072 ** Close the identified session
15074 if( strcmp(azCmd[0], "close")==0 ){
15075 if( nCmd!=1 ) goto session_syntax_error;
15077 session_close(pSession);
15078 p->aSession[iSes] = p->aSession[--p->nSession];
15082 /* .session enable ?BOOLEAN?
15083 ** Query or set the enable flag
15085 if( strcmp(azCmd[0], "enable")==0 ){
15087 if( nCmd>2 ) goto session_syntax_error;
15088 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15090 ii = sqlite3session_enable(pSession->p, ii);
15091 utf8_printf(p->out, "session %s enable flag = %d\n",
15092 pSession->zName, ii);
15096 /* .session filter GLOB ....
15097 ** Set a list of GLOB patterns of table names to be excluded.
15099 if( strcmp(azCmd[0], "filter")==0 ){
15101 if( nCmd<2 ) goto session_syntax_error;
15103 for(ii=0; ii<pSession->nFilter; ii++){
15104 sqlite3_free(pSession->azFilter[ii]);
15106 sqlite3_free(pSession->azFilter);
15107 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
15108 pSession->azFilter = sqlite3_malloc( nByte );
15109 if( pSession->azFilter==0 ){
15110 raw_printf(stderr, "Error: out or memory\n");
15113 for(ii=1; ii<nCmd; ii++){
15114 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
15116 pSession->nFilter = ii-1;
15120 /* .session indirect ?BOOLEAN?
15121 ** Query or set the indirect flag
15123 if( strcmp(azCmd[0], "indirect")==0 ){
15125 if( nCmd>2 ) goto session_syntax_error;
15126 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
15128 ii = sqlite3session_indirect(pSession->p, ii);
15129 utf8_printf(p->out, "session %s indirect flag = %d\n",
15130 pSession->zName, ii);
15134 /* .session isempty
15135 ** Determine if the session is empty
15137 if( strcmp(azCmd[0], "isempty")==0 ){
15139 if( nCmd!=1 ) goto session_syntax_error;
15141 ii = sqlite3session_isempty(pSession->p);
15142 utf8_printf(p->out, "session %s isempty flag = %d\n",
15143 pSession->zName, ii);
15148 ** List all currently open sessions
15150 if( strcmp(azCmd[0],"list")==0 ){
15151 for(i=0; i<p->nSession; i++){
15152 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
15156 /* .session open DB NAME
15157 ** Open a new session called NAME on the attached database DB.
15158 ** DB is normally "main".
15160 if( strcmp(azCmd[0],"open")==0 ){
15162 if( nCmd!=3 ) goto session_syntax_error;
15164 if( zName[0]==0 ) goto session_syntax_error;
15165 for(i=0; i<p->nSession; i++){
15166 if( strcmp(p->aSession[i].zName,zName)==0 ){
15167 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
15168 goto meta_command_exit;
15171 if( p->nSession>=ArraySize(p->aSession) ){
15172 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
15173 goto meta_command_exit;
15175 pSession = &p->aSession[p->nSession];
15176 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
15178 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
15180 goto meta_command_exit;
15182 pSession->nFilter = 0;
15183 sqlite3session_table_filter(pSession->p, session_filter, pSession);
15185 pSession->zName = sqlite3_mprintf("%s", zName);
15187 /* If no command name matches, show a syntax error */
15188 session_syntax_error:
15189 showHelp(p->out, "session");
15193 #ifdef SQLITE_DEBUG
15194 /* Undocumented commands for internal testing. Subject to change
15195 ** without notice. */
15196 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
15197 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
15199 for(i=1; i<nArg; i++){
15200 v = booleanValue(azArg[i]);
15201 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
15204 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
15205 int i; sqlite3_int64 v;
15206 for(i=1; i<nArg; i++){
15208 v = integerValue(azArg[i]);
15209 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
15210 utf8_printf(p->out, "%s", zBuf);
15216 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
15217 int bIsInit = 0; /* True to initialize the SELFTEST table */
15218 int bVerbose = 0; /* Verbose output */
15219 int bSelftestExists; /* True if SELFTEST already exists */
15220 int i, k; /* Loop counters */
15221 int nTest = 0; /* Number of tests runs */
15222 int nErr = 0; /* Number of errors seen */
15223 ShellText str; /* Answer for a query */
15224 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
15227 for(i=1; i<nArg; i++){
15228 const char *z = azArg[i];
15229 if( z[0]=='-' && z[1]=='-' ) z++;
15230 if( strcmp(z,"-init")==0 ){
15233 if( strcmp(z,"-v")==0 ){
15237 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15238 azArg[i], azArg[0]);
15239 raw_printf(stderr, "Should be one of: --init -v\n");
15241 goto meta_command_exit;
15244 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
15246 bSelftestExists = 0;
15248 bSelftestExists = 1;
15251 createSelftestTable(p);
15252 bSelftestExists = 1;
15255 appendText(&str, "x", 0);
15256 for(k=bSelftestExists; k>=0; k--){
15258 rc = sqlite3_prepare_v2(p->db,
15259 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
15262 rc = sqlite3_prepare_v2(p->db,
15263 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
15264 " (1,'run','PRAGMA integrity_check','ok')",
15268 raw_printf(stderr, "Error querying the selftest table\n");
15270 sqlite3_finalize(pStmt);
15271 goto meta_command_exit;
15273 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
15274 int tno = sqlite3_column_int(pStmt, 0);
15275 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
15276 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
15277 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
15281 char *zQuote = sqlite3_mprintf("%q", zSql);
15282 printf("%d: %s %s\n", tno, zOp, zSql);
15283 sqlite3_free(zQuote);
15285 if( strcmp(zOp,"memo")==0 ){
15286 utf8_printf(p->out, "%s\n", zSql);
15288 if( strcmp(zOp,"run")==0 ){
15292 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
15295 utf8_printf(p->out, "Result: %s\n", str.z);
15297 if( rc || zErrMsg ){
15300 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
15301 sqlite3_free(zErrMsg);
15302 }else if( strcmp(zAns,str.z)!=0 ){
15305 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
15306 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
15310 utf8_printf(stderr,
15311 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
15315 } /* End loop over rows of content from SELFTEST */
15316 sqlite3_finalize(pStmt);
15317 } /* End loop over k */
15319 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
15322 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
15323 if( nArg<2 || nArg>3 ){
15324 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
15328 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
15329 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
15332 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
15333 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
15337 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
15338 const char *zLike = 0; /* Which table to checksum. 0 means everything */
15339 int i; /* Loop counter */
15340 int bSchema = 0; /* Also hash the schema */
15341 int bSeparate = 0; /* Hash each table separately */
15342 int iSize = 224; /* Hash algorithm to use */
15343 int bDebug = 0; /* Only show the query that would have run */
15344 sqlite3_stmt *pStmt; /* For querying tables names */
15345 char *zSql; /* SQL to be run */
15346 char *zSep; /* Separator */
15347 ShellText sSql; /* Complete SQL for the query to run the hash */
15348 ShellText sQuery; /* Set of queries used to read all content */
15350 for(i=1; i<nArg; i++){
15351 const char *z = azArg[i];
15354 if( z[0]=='-' ) z++;
15355 if( strcmp(z,"schema")==0 ){
15358 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
15359 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
15361 iSize = atoi(&z[5]);
15363 if( strcmp(z,"debug")==0 ){
15367 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
15368 azArg[i], azArg[0]);
15369 raw_printf(stderr, "Should be one of: --schema"
15370 " --sha3-224 --sha3-256 --sha3-384 --sha3-512\n");
15372 goto meta_command_exit;
15375 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
15377 goto meta_command_exit;
15381 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
15385 zSql = "SELECT lower(name) FROM sqlite_master"
15386 " WHERE type='table' AND coalesce(rootpage,0)>1"
15387 " UNION ALL SELECT 'sqlite_master'"
15388 " ORDER BY 1 collate nocase";
15390 zSql = "SELECT lower(name) FROM sqlite_master"
15391 " WHERE type='table' AND coalesce(rootpage,0)>1"
15392 " AND name NOT LIKE 'sqlite_%'"
15393 " ORDER BY 1 collate nocase";
15395 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15398 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
15400 while( SQLITE_ROW==sqlite3_step(pStmt) ){
15401 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
15402 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
15403 if( strncmp(zTab, "sqlite_",7)!=0 ){
15404 appendText(&sQuery,"SELECT * FROM ", 0);
15405 appendText(&sQuery,zTab,'"');
15406 appendText(&sQuery," NOT INDEXED;", 0);
15407 }else if( strcmp(zTab, "sqlite_master")==0 ){
15408 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
15409 " ORDER BY name;", 0);
15410 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
15411 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
15412 " ORDER BY name;", 0);
15413 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
15414 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
15415 " ORDER BY tbl,idx;", 0);
15416 }else if( strcmp(zTab, "sqlite_stat3")==0
15417 || strcmp(zTab, "sqlite_stat4")==0 ){
15418 appendText(&sQuery, "SELECT * FROM ", 0);
15419 appendText(&sQuery, zTab, 0);
15420 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
15422 appendText(&sSql, zSep, 0);
15423 appendText(&sSql, sQuery.z, '\'');
15425 appendText(&sSql, ",", 0);
15426 appendText(&sSql, zTab, '\'');
15429 sqlite3_finalize(pStmt);
15431 zSql = sqlite3_mprintf(
15433 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
15434 " FROM [sha3sum$query]",
15437 zSql = sqlite3_mprintf(
15439 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
15440 " FROM [sha3sum$query]",
15446 utf8_printf(p->out, "%s\n", zSql);
15448 shell_exec(p, zSql, 0);
15450 sqlite3_free(zSql);
15453 #ifndef SQLITE_NOHAVE_SYSTEM
15455 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
15460 raw_printf(stderr, "Usage: .system COMMAND\n");
15462 goto meta_command_exit;
15464 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
15465 for(i=2; i<nArg; i++){
15466 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
15470 sqlite3_free(zCmd);
15471 if( x ) raw_printf(stderr, "System command returns %d\n", x);
15473 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15475 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
15476 static const char *azBool[] = { "off", "on", "trigger", "full"};
15479 raw_printf(stderr, "Usage: .show\n");
15481 goto meta_command_exit;
15483 utf8_printf(p->out, "%12.12s: %s\n","echo",
15484 azBool[ShellHasFlag(p, SHFLG_Echo)]);
15485 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
15486 utf8_printf(p->out, "%12.12s: %s\n","explain",
15487 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
15488 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
15489 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
15490 utf8_printf(p->out, "%12.12s: ", "nullvalue");
15491 output_c_string(p->out, p->nullValue);
15492 raw_printf(p->out, "\n");
15493 utf8_printf(p->out,"%12.12s: %s\n","output",
15494 strlen30(p->outfile) ? p->outfile : "stdout");
15495 utf8_printf(p->out,"%12.12s: ", "colseparator");
15496 output_c_string(p->out, p->colSeparator);
15497 raw_printf(p->out, "\n");
15498 utf8_printf(p->out,"%12.12s: ", "rowseparator");
15499 output_c_string(p->out, p->rowSeparator);
15500 raw_printf(p->out, "\n");
15501 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
15502 utf8_printf(p->out, "%12.12s: ", "width");
15503 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
15504 raw_printf(p->out, "%d ", p->colWidth[i]);
15506 raw_printf(p->out, "\n");
15507 utf8_printf(p->out, "%12.12s: %s\n", "filename",
15508 p->zDbFilename ? p->zDbFilename : "");
15511 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
15513 p->statsOn = (u8)booleanValue(azArg[1]);
15514 }else if( nArg==1 ){
15515 display_stats(p->db, p, 0);
15517 raw_printf(stderr, "Usage: .stats ?on|off?\n");
15522 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
15523 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
15524 || strncmp(azArg[0], "indexes", n)==0) )
15526 sqlite3_stmt *pStmt;
15533 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
15535 sqlite3_finalize(pStmt);
15536 return shellDatabaseError(p->db);
15539 if( nArg>2 && c=='i' ){
15540 /* It is an historical accident that the .indexes command shows an error
15541 ** when called with the wrong number of arguments whereas the .tables
15542 ** command does not. */
15543 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
15545 sqlite3_finalize(pStmt);
15546 goto meta_command_exit;
15548 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
15549 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
15550 if( zDbName==0 ) continue;
15551 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
15552 if( sqlite3_stricmp(zDbName, "main")==0 ){
15553 appendText(&s, "SELECT name FROM ", 0);
15555 appendText(&s, "SELECT ", 0);
15556 appendText(&s, zDbName, '\'');
15557 appendText(&s, "||'.'||name FROM ", 0);
15559 appendText(&s, zDbName, '"');
15560 appendText(&s, ".sqlite_master ", 0);
15562 appendText(&s," WHERE type IN ('table','view')"
15563 " AND name NOT LIKE 'sqlite_%'"
15564 " AND name LIKE ?1", 0);
15566 appendText(&s," WHERE type='index'"
15567 " AND tbl_name LIKE ?1", 0);
15570 rc = sqlite3_finalize(pStmt);
15571 appendText(&s, " ORDER BY 1", 0);
15572 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
15574 if( rc ) return shellDatabaseError(p->db);
15576 /* Run the SQL statement prepared by the above block. Store the results
15577 ** as an array of nul-terminated strings in azResult[]. */
15581 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
15583 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
15585 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15586 if( nRow>=nAlloc ){
15588 int n2 = nAlloc*2 + 10;
15589 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
15590 if( azNew==0 ) shell_out_of_memory();
15594 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
15595 if( 0==azResult[nRow] ) shell_out_of_memory();
15598 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
15599 rc = shellDatabaseError(p->db);
15602 /* Pretty-print the contents of array azResult[] to the output */
15603 if( rc==0 && nRow>0 ){
15604 int len, maxlen = 0;
15606 int nPrintCol, nPrintRow;
15607 for(i=0; i<nRow; i++){
15608 len = strlen30(azResult[i]);
15609 if( len>maxlen ) maxlen = len;
15611 nPrintCol = 80/(maxlen+2);
15612 if( nPrintCol<1 ) nPrintCol = 1;
15613 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
15614 for(i=0; i<nPrintRow; i++){
15615 for(j=i; j<nRow; j+=nPrintRow){
15616 char *zSp = j<nPrintRow ? "" : " ";
15617 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
15618 azResult[j] ? azResult[j]:"");
15620 raw_printf(p->out, "\n");
15624 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
15625 sqlite3_free(azResult);
15628 /* Begin redirecting output to the file "testcase-out.txt" */
15629 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
15631 p->out = output_file_open("testcase-out.txt", 0);
15633 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
15636 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
15638 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
15642 #ifndef SQLITE_UNTESTABLE
15643 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
15644 static const struct {
15645 const char *zCtrlName; /* Name of a test-control option */
15646 int ctrlCode; /* Integer code for that option */
15647 const char *zUsage; /* Usage notes */
15649 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
15650 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
15651 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
15652 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
15653 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
15654 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
15655 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
15656 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "BOOLEAN" },
15657 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
15658 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
15659 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
15661 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
15663 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
15664 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" },
15665 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
15666 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
15667 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" },
15671 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
15674 const char *zCmd = 0;
15677 zCmd = nArg>=2 ? azArg[1] : "help";
15679 /* The argument can optionally begin with "-" or "--" */
15680 if( zCmd[0]=='-' && zCmd[1] ){
15682 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
15685 /* --help lists all test-controls */
15686 if( strcmp(zCmd,"help")==0 ){
15687 utf8_printf(p->out, "Available test-controls:\n");
15688 for(i=0; i<ArraySize(aCtrl); i++){
15689 utf8_printf(p->out, " .testctrl %s %s\n",
15690 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
15693 goto meta_command_exit;
15696 /* convert testctrl text option to value. allow any unique prefix
15697 ** of the option name, or a numerical value. */
15698 n2 = strlen30(zCmd);
15699 for(i=0; i<ArraySize(aCtrl); i++){
15700 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
15702 testctrl = aCtrl[i].ctrlCode;
15705 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
15706 "Use \".testctrl --help\" for help\n", zCmd);
15708 goto meta_command_exit;
15713 utf8_printf(stderr,"Error: unknown test-control: %s\n"
15714 "Use \".testctrl --help\" for help\n", zCmd);
15718 /* sqlite3_test_control(int, db, int) */
15719 case SQLITE_TESTCTRL_OPTIMIZATIONS:
15720 case SQLITE_TESTCTRL_RESERVE:
15722 int opt = (int)strtol(azArg[2], 0, 0);
15723 rc2 = sqlite3_test_control(testctrl, p->db, opt);
15728 /* sqlite3_test_control(int) */
15729 case SQLITE_TESTCTRL_PRNG_SAVE:
15730 case SQLITE_TESTCTRL_PRNG_RESTORE:
15731 case SQLITE_TESTCTRL_PRNG_RESET:
15732 case SQLITE_TESTCTRL_BYTEORDER:
15734 rc2 = sqlite3_test_control(testctrl);
15735 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
15739 /* sqlite3_test_control(int, uint) */
15740 case SQLITE_TESTCTRL_PENDING_BYTE:
15742 unsigned int opt = (unsigned int)integerValue(azArg[2]);
15743 rc2 = sqlite3_test_control(testctrl, opt);
15748 /* sqlite3_test_control(int, int) */
15749 case SQLITE_TESTCTRL_ASSERT:
15750 case SQLITE_TESTCTRL_ALWAYS:
15751 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
15753 int opt = booleanValue(azArg[2]);
15754 rc2 = sqlite3_test_control(testctrl, opt);
15759 /* sqlite3_test_control(int, int) */
15760 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
15761 case SQLITE_TESTCTRL_NEVER_CORRUPT:
15763 int opt = booleanValue(azArg[2]);
15764 rc2 = sqlite3_test_control(testctrl, opt);
15769 case SQLITE_TESTCTRL_IMPOSTER:
15771 rc2 = sqlite3_test_control(testctrl, p->db,
15773 integerValue(azArg[3]),
15774 integerValue(azArg[4]));
15780 case SQLITE_TESTCTRL_PARSER_COVERAGE:
15782 sqlite3_test_control(testctrl, p->out);
15788 if( isOk==0 && iCtrl>=0 ){
15789 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
15791 }else if( isOk==1 ){
15792 raw_printf(p->out, "%d\n", rc2);
15793 }else if( isOk==2 ){
15794 raw_printf(p->out, "0x%08x\n", rc2);
15797 #endif /* !defined(SQLITE_UNTESTABLE) */
15799 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
15801 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
15804 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
15806 enableTimer = booleanValue(azArg[1]);
15807 if( enableTimer && !HAS_TIMER ){
15808 raw_printf(stderr, "Error: timer not available on this system.\n");
15812 raw_printf(stderr, "Usage: .timer on|off\n");
15817 #ifndef SQLITE_OMIT_TRACE
15818 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
15822 for(jj=1; jj<nArg; jj++){
15823 const char *z = azArg[jj];
15825 if( optionMatch(z, "expanded") ){
15826 p->eTraceType = SHELL_TRACE_EXPANDED;
15828 #ifdef SQLITE_ENABLE_NORMALIZE
15829 else if( optionMatch(z, "normalized") ){
15830 p->eTraceType = SHELL_TRACE_NORMALIZED;
15833 else if( optionMatch(z, "plain") ){
15834 p->eTraceType = SHELL_TRACE_PLAIN;
15836 else if( optionMatch(z, "profile") ){
15837 mType |= SQLITE_TRACE_PROFILE;
15839 else if( optionMatch(z, "row") ){
15840 mType |= SQLITE_TRACE_ROW;
15842 else if( optionMatch(z, "stmt") ){
15843 mType |= SQLITE_TRACE_STMT;
15845 else if( optionMatch(z, "close") ){
15846 mType |= SQLITE_TRACE_CLOSE;
15849 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
15851 goto meta_command_exit;
15854 output_file_close(p->traceOut);
15855 p->traceOut = output_file_open(azArg[1], 0);
15858 if( p->traceOut==0 ){
15859 sqlite3_trace_v2(p->db, 0, 0, 0);
15861 if( mType==0 ) mType = SQLITE_TRACE_STMT;
15862 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
15865 #endif /* !defined(SQLITE_OMIT_TRACE) */
15867 #if SQLITE_USER_AUTHENTICATION
15868 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
15870 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
15872 goto meta_command_exit;
15875 if( strcmp(azArg[1],"login")==0 ){
15877 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
15879 goto meta_command_exit;
15881 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
15883 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
15886 }else if( strcmp(azArg[1],"add")==0 ){
15888 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
15890 goto meta_command_exit;
15892 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15893 booleanValue(azArg[4]));
15895 raw_printf(stderr, "User-Add failed: %d\n", rc);
15898 }else if( strcmp(azArg[1],"edit")==0 ){
15900 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
15902 goto meta_command_exit;
15904 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
15905 booleanValue(azArg[4]));
15907 raw_printf(stderr, "User-Edit failed: %d\n", rc);
15910 }else if( strcmp(azArg[1],"delete")==0 ){
15912 raw_printf(stderr, "Usage: .user delete USER\n");
15914 goto meta_command_exit;
15916 rc = sqlite3_user_delete(p->db, azArg[2]);
15918 raw_printf(stderr, "User-Delete failed: %d\n", rc);
15922 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
15924 goto meta_command_exit;
15927 #endif /* SQLITE_USER_AUTHENTICATION */
15929 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
15930 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
15931 sqlite3_libversion(), sqlite3_sourceid());
15932 #if SQLITE_HAVE_ZLIB
15933 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
15935 #define CTIMEOPT_VAL_(opt) #opt
15936 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
15937 #if defined(__clang__) && defined(__clang_major__)
15938 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
15939 CTIMEOPT_VAL(__clang_minor__) "."
15940 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
15941 #elif defined(_MSC_VER)
15942 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
15943 #elif defined(__GNUC__) && defined(__VERSION__)
15944 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
15948 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
15949 const char *zDbName = nArg==2 ? azArg[1] : "main";
15950 sqlite3_vfs *pVfs = 0;
15952 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
15954 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
15955 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15956 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15957 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15962 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
15964 sqlite3_vfs *pCurrent = 0;
15966 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
15968 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
15969 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
15970 pVfs==pCurrent ? " <--- CURRENT" : "");
15971 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
15972 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
15973 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
15975 raw_printf(p->out, "-----------------------------------\n");
15980 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
15981 const char *zDbName = nArg==2 ? azArg[1] : "main";
15982 char *zVfsName = 0;
15984 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
15986 utf8_printf(p->out, "%s\n", zVfsName);
15987 sqlite3_free(zVfsName);
15992 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
15993 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
15994 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
15998 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
16000 assert( nArg<=ArraySize(azArg) );
16001 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
16002 p->colWidth[j-1] = (int)integerValue(azArg[j]);
16007 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
16008 " \"%s\". Enter \".help\" for help\n", azArg[0]);
16015 if( p->outCount==0 ) output_reset(p);
16021 ** Return TRUE if a semicolon occurs anywhere in the first N characters
16024 static int line_contains_semicolon(const char *z, int N){
16026 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
16031 ** Test to see if a line consists entirely of whitespace.
16033 static int _all_whitespace(const char *z){
16035 if( IsSpace(z[0]) ) continue;
16036 if( *z=='/' && z[1]=='*' ){
16038 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
16039 if( *z==0 ) return 0;
16043 if( *z=='-' && z[1]=='-' ){
16045 while( *z && *z!='\n' ){ z++; }
16046 if( *z==0 ) return 1;
16055 ** Return TRUE if the line typed in is an SQL command terminator other
16056 ** than a semi-colon. The SQL Server style "go" command is understood
16057 ** as is the Oracle "/".
16059 static int line_is_command_terminator(const char *zLine){
16060 while( IsSpace(zLine[0]) ){ zLine++; };
16061 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
16062 return 1; /* Oracle */
16064 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
16065 && _all_whitespace(&zLine[2]) ){
16066 return 1; /* SQL Server */
16072 ** We need a default sqlite3_complete() implementation to use in case
16073 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
16074 ** any arbitrary text is a complete SQL statement. This is not very
16075 ** user-friendly, but it does seem to work.
16077 #ifdef SQLITE_OMIT_COMPLETE
16078 #define sqlite3_complete(x) 1
16082 ** Return true if zSql is a complete SQL statement. Return false if it
16083 ** ends in the middle of a string literal or C-style comment.
16085 static int line_is_complete(char *zSql, int nSql){
16087 if( zSql==0 ) return 1;
16090 rc = sqlite3_complete(zSql);
16096 ** Run a single line of SQL. Return the number of errors.
16098 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
16103 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
16104 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
16106 rc = shell_exec(p, zSql, &zErrMsg);
16108 if( rc || zErrMsg ){
16110 if( in!=0 || !stdin_is_interactive ){
16111 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
16112 "Error: near line %d:", startline);
16114 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
16117 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
16118 sqlite3_free(zErrMsg);
16121 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
16124 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
16125 raw_printf(p->out, "changes: %3d total_changes: %d\n",
16126 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
16133 ** Read input from *in and process it. If *in==0 then input
16134 ** is interactive - the user is typing it it. Otherwise, input
16135 ** is coming from a file or device. A prompt is issued and history
16136 ** is saved only if input is interactive. An interrupt signal will
16137 ** cause this routine to exit immediately, unless input is interactive.
16139 ** Return the number of errors.
16141 static int process_input(ShellState *p){
16142 char *zLine = 0; /* A single input line */
16143 char *zSql = 0; /* Accumulated SQL text */
16144 int nLine; /* Length of current line */
16145 int nSql = 0; /* Bytes of zSql[] used */
16146 int nAlloc = 0; /* Allocated zSql[] space */
16147 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
16148 int rc; /* Error code */
16149 int errCnt = 0; /* Number of errors seen */
16150 int startline = 0; /* Line number for start of current input */
16153 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
16155 zLine = one_input_line(p->in, zLine, nSql>0);
16158 if( p->in==0 && stdin_is_interactive ) printf("\n");
16161 if( seenInterrupt ){
16162 if( p->in!=0 ) break;
16166 if( nSql==0 && _all_whitespace(zLine) ){
16167 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16170 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
16171 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
16172 if( zLine[0]=='.' ){
16173 rc = do_meta_command(zLine, p);
16174 if( rc==2 ){ /* exit requested */
16182 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
16183 memcpy(zLine,";",2);
16185 nLine = strlen30(zLine);
16186 if( nSql+nLine+2>=nAlloc ){
16187 nAlloc = nSql+nLine+100;
16188 zSql = realloc(zSql, nAlloc);
16189 if( zSql==0 ) shell_out_of_memory();
16194 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
16195 assert( nAlloc>0 && zSql!=0 );
16196 memcpy(zSql, zLine+i, nLine+1-i);
16197 startline = p->lineno;
16200 zSql[nSql++] = '\n';
16201 memcpy(zSql+nSql, zLine, nLine+1);
16204 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
16205 && sqlite3_complete(zSql) ){
16206 errCnt += runOneSqlLine(p, zSql, p->in, startline);
16214 }else if( nSql && _all_whitespace(zSql) ){
16215 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
16219 if( nSql && !_all_whitespace(zSql) ){
16220 errCnt += runOneSqlLine(p, zSql, p->in, startline);
16228 ** Return a pathname which is the user's home directory. A
16229 ** 0 return indicates an error of some kind.
16231 static char *find_home_dir(int clearFlag){
16232 static char *home_dir = NULL;
16238 if( home_dir ) return home_dir;
16240 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
16241 && !defined(__RTP__) && !defined(_WRS_KERNEL)
16243 struct passwd *pwent;
16244 uid_t uid = getuid();
16245 if( (pwent=getpwuid(uid)) != NULL) {
16246 home_dir = pwent->pw_dir;
16251 #if defined(_WIN32_WCE)
16252 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
16257 #if defined(_WIN32) || defined(WIN32)
16259 home_dir = getenv("USERPROFILE");
16264 home_dir = getenv("HOME");
16267 #if defined(_WIN32) || defined(WIN32)
16269 char *zDrive, *zPath;
16271 zDrive = getenv("HOMEDRIVE");
16272 zPath = getenv("HOMEPATH");
16273 if( zDrive && zPath ){
16274 n = strlen30(zDrive) + strlen30(zPath) + 1;
16275 home_dir = malloc( n );
16276 if( home_dir==0 ) return 0;
16277 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
16284 #endif /* !_WIN32_WCE */
16287 int n = strlen30(home_dir) + 1;
16288 char *z = malloc( n );
16289 if( z ) memcpy(z, home_dir, n);
16297 ** Read input from the file given by sqliterc_override. Or if that
16298 ** parameter is NULL, take input from ~/.sqliterc
16300 ** Returns the number of errors.
16302 static void process_sqliterc(
16303 ShellState *p, /* Configuration data */
16304 const char *sqliterc_override /* Name of config file. NULL to use default */
16306 char *home_dir = NULL;
16307 const char *sqliterc = sqliterc_override;
16309 FILE *inSaved = p->in;
16310 int savedLineno = p->lineno;
16312 if (sqliterc == NULL) {
16313 home_dir = find_home_dir(0);
16315 raw_printf(stderr, "-- warning: cannot find home directory;"
16316 " cannot read ~/.sqliterc\n");
16319 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
16322 p->in = fopen(sqliterc,"rb");
16324 if( stdin_is_interactive ){
16325 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
16331 p->lineno = savedLineno;
16332 sqlite3_free(zBuf);
16336 ** Show available command line options
16338 static const char zOptions[] =
16339 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
16340 " -A ARGS... run \".archive ARGS\" and exit\n"
16342 " -append append the database to the end of the file\n"
16343 " -ascii set output mode to 'ascii'\n"
16344 " -bail stop after hitting an error\n"
16345 " -batch force batch I/O\n"
16346 " -column set output mode to 'column'\n"
16347 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
16348 " -csv set output mode to 'csv'\n"
16349 #if defined(SQLITE_ENABLE_DESERIALIZE)
16350 " -deserialize open the database using sqlite3_deserialize()\n"
16352 " -echo print commands before execution\n"
16353 " -init FILENAME read/process named file\n"
16354 " -[no]header turn headers on or off\n"
16355 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16356 " -heap SIZE Size of heap for memsys3 or memsys5\n"
16358 " -help show this message\n"
16359 " -html set output mode to HTML\n"
16360 " -interactive force interactive I/O\n"
16361 " -line set output mode to 'line'\n"
16362 " -list set output mode to 'list'\n"
16363 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
16364 #if defined(SQLITE_ENABLE_DESERIALIZE)
16365 " -maxsize N maximum size for a --deserialize database\n"
16367 " -memtrace trace all memory allocations and deallocations\n"
16368 " -mmap N default mmap size set to N\n"
16369 #ifdef SQLITE_ENABLE_MULTIPLEX
16370 " -multiplex enable the multiplexor VFS\n"
16372 " -newline SEP set output row separator. Default: '\\n'\n"
16373 " -nullvalue TEXT set text string for NULL values. Default ''\n"
16374 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
16375 " -quote set output mode to 'quote'\n"
16376 " -readonly open the database read-only\n"
16377 " -separator SEP set output column separator. Default: '|'\n"
16378 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16379 " -sorterref SIZE sorter references threshold size\n"
16381 " -stats print memory stats before each finalize\n"
16382 " -version show SQLite version\n"
16383 " -vfs NAME use NAME as the default VFS\n"
16384 #ifdef SQLITE_ENABLE_VFSTRACE
16385 " -vfstrace enable tracing of all VFS calls\n"
16387 #ifdef SQLITE_HAVE_ZLIB
16388 " -zip open the file as a ZIP Archive\n"
16391 static void usage(int showDetail){
16392 utf8_printf(stderr,
16393 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
16394 "FILENAME is the name of an SQLite database. A new database is created\n"
16395 "if the file does not previously exist.\n", Argv0);
16397 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
16399 raw_printf(stderr, "Use the -help option for additional information\n");
16405 ** Internal check: Verify that the SQLite is uninitialized. Print a
16406 ** error message if it is initialized.
16408 static void verify_uninitialized(void){
16409 if( sqlite3_config(-1)==SQLITE_MISUSE ){
16410 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
16411 " initialization.\n");
16416 ** Initialize the state information in data
16418 static void main_init(ShellState *data) {
16419 memset(data, 0, sizeof(*data));
16420 data->normalMode = data->cMode = data->mode = MODE_List;
16421 data->autoExplain = 1;
16422 memcpy(data->colSeparator,SEP_Column, 2);
16423 memcpy(data->rowSeparator,SEP_Row, 2);
16424 data->showHeader = 0;
16425 data->shellFlgs = SHFLG_Lookaside;
16426 verify_uninitialized();
16427 sqlite3_config(SQLITE_CONFIG_URI, 1);
16428 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
16429 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
16430 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
16431 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
16435 ** Output text to the console in a font that attracts extra attention.
16438 static void printBold(const char *zText){
16439 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
16440 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
16441 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
16442 SetConsoleTextAttribute(out,
16443 FOREGROUND_RED|FOREGROUND_INTENSITY
16445 printf("%s", zText);
16446 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
16449 static void printBold(const char *zText){
16450 printf("\033[1m%s\033[0m", zText);
16455 ** Get the argument to an --option. Throw an error and die if no argument
16458 static char *cmdline_option_value(int argc, char **argv, int i){
16460 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
16461 argv[0], argv[argc-1]);
16467 #ifndef SQLITE_SHELL_IS_UTF8
16468 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
16469 # define SQLITE_SHELL_IS_UTF8 (0)
16471 # define SQLITE_SHELL_IS_UTF8 (1)
16475 #if SQLITE_SHELL_IS_UTF8
16476 int SQLITE_CDECL main(int argc, char **argv){
16478 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
16483 const char *zInitFile = 0;
16486 int warnInmemoryDb = 0;
16490 const char *zVfs = 0; /* Value of -vfs command-line option */
16491 #if !SQLITE_SHELL_IS_UTF8
16492 char **argvToFree = 0;
16493 int argcToFree = 0;
16496 setBinaryMode(stdin, 0);
16497 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
16498 stdin_is_interactive = isatty(0);
16499 stdout_is_console = isatty(1);
16501 #if !defined(_WIN32_WCE)
16502 if( getenv("SQLITE_DEBUG_BREAK") ){
16503 if( isatty(0) && isatty(2) ){
16505 "attach debugger to process %d and press any key to continue.\n",
16509 #if defined(_WIN32) || defined(WIN32)
16511 #elif defined(SIGTRAP)
16518 #if USE_SYSTEM_SQLITE+0!=1
16519 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
16520 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
16521 sqlite3_sourceid(), SQLITE_SOURCE_ID);
16527 /* On Windows, we must translate command-line arguments into UTF-8.
16528 ** The SQLite memory allocator subsystem has to be enabled in order to
16529 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
16530 ** subsequent sqlite3_config() calls will work. So copy all results into
16531 ** memory that does not come from the SQLite memory allocator.
16533 #if !SQLITE_SHELL_IS_UTF8
16534 sqlite3_initialize();
16535 argvToFree = malloc(sizeof(argv[0])*argc*2);
16537 argv = argvToFree + argc;
16538 if( argv==0 ) shell_out_of_memory();
16539 for(i=0; i<argc; i++){
16540 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
16542 if( z==0 ) shell_out_of_memory();
16543 n = (int)strlen(z);
16544 argv[i] = malloc( n+1 );
16545 if( argv[i]==0 ) shell_out_of_memory();
16546 memcpy(argv[i], z, n+1);
16547 argvToFree[i] = argv[i];
16550 sqlite3_shutdown();
16553 assert( argc>=1 && argv && argv[0] );
16556 /* Make sure we have a valid signal handler early, before anything
16560 signal(SIGINT, interrupt_handler);
16561 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
16562 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
16565 #ifdef SQLITE_SHELL_DBNAME_PROC
16567 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
16568 ** of a C-function that will provide the name of the database file. Use
16569 ** this compile-time option to embed this shell program in larger
16570 ** applications. */
16571 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
16572 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
16573 warnInmemoryDb = 0;
16577 /* Do an initial pass through the command-line argument to locate
16578 ** the name of the database file, the name of the initialization file,
16579 ** the size of the alternative malloc heap,
16580 ** and the first command to execute.
16582 verify_uninitialized();
16583 for(i=1; i<argc; i++){
16587 if( data.zDbFilename==0 ){
16588 data.zDbFilename = z;
16590 /* Excesss arguments are interpreted as SQL (or dot-commands) and
16591 ** mean that nothing is read from stdin */
16594 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
16595 if( azCmd==0 ) shell_out_of_memory();
16599 if( z[1]=='-' ) z++;
16600 if( strcmp(z,"-separator")==0
16601 || strcmp(z,"-nullvalue")==0
16602 || strcmp(z,"-newline")==0
16603 || strcmp(z,"-cmd")==0
16605 (void)cmdline_option_value(argc, argv, ++i);
16606 }else if( strcmp(z,"-init")==0 ){
16607 zInitFile = cmdline_option_value(argc, argv, ++i);
16608 }else if( strcmp(z,"-batch")==0 ){
16609 /* Need to check for batch mode here to so we can avoid printing
16610 ** informational messages (like from process_sqliterc) before
16611 ** we do the actual processing of arguments later in a second pass.
16613 stdin_is_interactive = 0;
16614 }else if( strcmp(z,"-heap")==0 ){
16615 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
16617 sqlite3_int64 szHeap;
16619 zSize = cmdline_option_value(argc, argv, ++i);
16620 szHeap = integerValue(zSize);
16621 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
16622 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
16624 (void)cmdline_option_value(argc, argv, ++i);
16626 }else if( strcmp(z,"-pagecache")==0 ){
16628 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16629 if( sz>70000 ) sz = 70000;
16631 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16632 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
16633 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
16634 data.shellFlgs |= SHFLG_Pagecache;
16635 }else if( strcmp(z,"-lookaside")==0 ){
16637 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
16639 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
16641 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
16642 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
16643 #ifdef SQLITE_ENABLE_VFSTRACE
16644 }else if( strcmp(z,"-vfstrace")==0 ){
16645 extern int vfstrace_register(
16646 const char *zTraceName,
16647 const char *zOldVfsName,
16648 int (*xOut)(const char*,void*),
16652 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
16654 #ifdef SQLITE_ENABLE_MULTIPLEX
16655 }else if( strcmp(z,"-multiplex")==0 ){
16656 extern int sqlite3_multiple_initialize(const char*,int);
16657 sqlite3_multiplex_initialize(0, 1);
16659 }else if( strcmp(z,"-mmap")==0 ){
16660 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16661 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
16662 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16663 }else if( strcmp(z,"-sorterref")==0 ){
16664 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
16665 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
16667 }else if( strcmp(z,"-vfs")==0 ){
16668 zVfs = cmdline_option_value(argc, argv, ++i);
16669 #ifdef SQLITE_HAVE_ZLIB
16670 }else if( strcmp(z,"-zip")==0 ){
16671 data.openMode = SHELL_OPEN_ZIPFILE;
16673 }else if( strcmp(z,"-append")==0 ){
16674 data.openMode = SHELL_OPEN_APPENDVFS;
16675 #ifdef SQLITE_ENABLE_DESERIALIZE
16676 }else if( strcmp(z,"-deserialize")==0 ){
16677 data.openMode = SHELL_OPEN_DESERIALIZE;
16678 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16679 data.szMax = integerValue(argv[++i]);
16681 }else if( strcmp(z,"-readonly")==0 ){
16682 data.openMode = SHELL_OPEN_READONLY;
16683 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16684 }else if( strncmp(z, "-A",2)==0 ){
16685 /* All remaining command-line arguments are passed to the ".archive"
16686 ** command, so ignore them */
16689 }else if( strcmp(z, "-memtrace")==0 ){
16690 sqlite3MemTraceActivate(stderr);
16693 verify_uninitialized();
16696 #ifdef SQLITE_SHELL_INIT_PROC
16698 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
16699 ** of a C-function that will perform initialization actions on SQLite that
16700 ** occur just before or after sqlite3_initialize(). Use this compile-time
16701 ** option to embed this shell program in larger applications. */
16702 extern void SQLITE_SHELL_INIT_PROC(void);
16703 SQLITE_SHELL_INIT_PROC();
16706 /* All the sqlite3_config() calls have now been made. So it is safe
16707 ** to call sqlite3_initialize() and process any command line -vfs option. */
16708 sqlite3_initialize();
16712 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
16714 sqlite3_vfs_register(pVfs, 1);
16716 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
16721 if( data.zDbFilename==0 ){
16722 #ifndef SQLITE_OMIT_MEMORYDB
16723 data.zDbFilename = ":memory:";
16724 warnInmemoryDb = argc==1;
16726 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
16731 sqlite3_appendvfs_init(0,0,0);
16733 /* Go ahead and open the database file if it already exists. If the
16734 ** file does not exist, delay opening it. This prevents empty database
16735 ** files from being created if a user mistypes the database name argument
16736 ** to the sqlite command-line tool.
16738 if( access(data.zDbFilename, 0)==0 ){
16742 /* Process the initialization file if there is one. If no -init option
16743 ** is given on the command line, look for a file named ~/.sqliterc and
16744 ** try to process it.
16746 process_sqliterc(&data,zInitFile);
16748 /* Make a second pass through the command-line argument and set
16749 ** options. This second pass is delayed until after the initialization
16750 ** file is processed so that the command-line arguments will override
16751 ** settings in the initialization file.
16753 for(i=1; i<argc; i++){
16755 if( z[0]!='-' ) continue;
16756 if( z[1]=='-' ){ z++; }
16757 if( strcmp(z,"-init")==0 ){
16759 }else if( strcmp(z,"-html")==0 ){
16760 data.mode = MODE_Html;
16761 }else if( strcmp(z,"-list")==0 ){
16762 data.mode = MODE_List;
16763 }else if( strcmp(z,"-quote")==0 ){
16764 data.mode = MODE_Quote;
16765 }else if( strcmp(z,"-line")==0 ){
16766 data.mode = MODE_Line;
16767 }else if( strcmp(z,"-column")==0 ){
16768 data.mode = MODE_Column;
16769 }else if( strcmp(z,"-csv")==0 ){
16770 data.mode = MODE_Csv;
16771 memcpy(data.colSeparator,",",2);
16772 #ifdef SQLITE_HAVE_ZLIB
16773 }else if( strcmp(z,"-zip")==0 ){
16774 data.openMode = SHELL_OPEN_ZIPFILE;
16776 }else if( strcmp(z,"-append")==0 ){
16777 data.openMode = SHELL_OPEN_APPENDVFS;
16778 #ifdef SQLITE_ENABLE_DESERIALIZE
16779 }else if( strcmp(z,"-deserialize")==0 ){
16780 data.openMode = SHELL_OPEN_DESERIALIZE;
16781 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
16782 data.szMax = integerValue(argv[++i]);
16784 }else if( strcmp(z,"-readonly")==0 ){
16785 data.openMode = SHELL_OPEN_READONLY;
16786 }else if( strcmp(z,"-ascii")==0 ){
16787 data.mode = MODE_Ascii;
16788 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16790 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16792 }else if( strcmp(z,"-separator")==0 ){
16793 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
16794 "%s",cmdline_option_value(argc,argv,++i));
16795 }else if( strcmp(z,"-newline")==0 ){
16796 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
16797 "%s",cmdline_option_value(argc,argv,++i));
16798 }else if( strcmp(z,"-nullvalue")==0 ){
16799 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
16800 "%s",cmdline_option_value(argc,argv,++i));
16801 }else if( strcmp(z,"-header")==0 ){
16802 data.showHeader = 1;
16803 }else if( strcmp(z,"-noheader")==0 ){
16804 data.showHeader = 0;
16805 }else if( strcmp(z,"-echo")==0 ){
16806 ShellSetFlag(&data, SHFLG_Echo);
16807 }else if( strcmp(z,"-eqp")==0 ){
16808 data.autoEQP = AUTOEQP_on;
16809 }else if( strcmp(z,"-eqpfull")==0 ){
16810 data.autoEQP = AUTOEQP_full;
16811 }else if( strcmp(z,"-stats")==0 ){
16813 }else if( strcmp(z,"-scanstats")==0 ){
16814 data.scanstatsOn = 1;
16815 }else if( strcmp(z,"-backslash")==0 ){
16816 /* Undocumented command-line option: -backslash
16817 ** Causes C-style backslash escapes to be evaluated in SQL statements
16818 ** prior to sending the SQL into SQLite. Useful for injecting
16819 ** crazy bytes in the middle of SQL statements for testing and debugging.
16821 ShellSetFlag(&data, SHFLG_Backslash);
16822 }else if( strcmp(z,"-bail")==0 ){
16824 }else if( strcmp(z,"-version")==0 ){
16825 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
16827 }else if( strcmp(z,"-interactive")==0 ){
16828 stdin_is_interactive = 1;
16829 }else if( strcmp(z,"-batch")==0 ){
16830 stdin_is_interactive = 0;
16831 }else if( strcmp(z,"-heap")==0 ){
16833 }else if( strcmp(z,"-pagecache")==0 ){
16835 }else if( strcmp(z,"-lookaside")==0 ){
16837 }else if( strcmp(z,"-mmap")==0 ){
16839 }else if( strcmp(z,"-memtrace")==0 ){
16841 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
16842 }else if( strcmp(z,"-sorterref")==0 ){
16845 }else if( strcmp(z,"-vfs")==0 ){
16847 #ifdef SQLITE_ENABLE_VFSTRACE
16848 }else if( strcmp(z,"-vfstrace")==0 ){
16851 #ifdef SQLITE_ENABLE_MULTIPLEX
16852 }else if( strcmp(z,"-multiplex")==0 ){
16855 }else if( strcmp(z,"-help")==0 ){
16857 }else if( strcmp(z,"-cmd")==0 ){
16858 /* Run commands that follow -cmd first and separately from commands
16859 ** that simply appear on the command-line. This seems goofy. It would
16860 ** be better if all commands ran in the order that they appear. But
16861 ** we retain the goofy behavior for historical compatibility. */
16862 if( i==argc-1 ) break;
16863 z = cmdline_option_value(argc,argv,++i);
16865 rc = do_meta_command(z, &data);
16866 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
16869 rc = shell_exec(&data, z, &zErrMsg);
16871 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16872 if( bail_on_error ) return rc!=0 ? rc : 1;
16874 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
16875 if( bail_on_error ) return rc;
16878 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16879 }else if( strncmp(z, "-A", 2)==0 ){
16881 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
16882 " with \"%s\"\n", z);
16885 open_db(&data, OPEN_DB_ZIPFILE);
16888 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
16890 arDotCommand(&data, 1, argv+i, argc-i);
16896 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
16897 raw_printf(stderr,"Use -help for a list of options.\n");
16900 data.cMode = data.mode;
16904 /* Run all arguments that do not begin with '-' as if they were separate
16905 ** command-line inputs, except for the argToSkip argument which contains
16906 ** the database filename.
16908 for(i=0; i<nCmd; i++){
16909 if( azCmd[i][0]=='.' ){
16910 rc = do_meta_command(azCmd[i], &data);
16911 if( rc ) return rc==2 ? 0 : rc;
16914 rc = shell_exec(&data, azCmd[i], &zErrMsg);
16916 utf8_printf(stderr,"Error: %s\n", zErrMsg);
16917 return rc!=0 ? rc : 1;
16919 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
16926 /* Run commands received from standard input
16928 if( stdin_is_interactive ){
16933 "SQLite version %s %.19s\n" /*extra-version-info*/
16934 "Enter \".help\" for usage hints.\n",
16935 sqlite3_libversion(), sqlite3_sourceid()
16937 if( warnInmemoryDb ){
16938 printf("Connected to a ");
16939 printBold("transient in-memory database");
16940 printf(".\nUse \".open FILENAME\" to reopen on a "
16941 "persistent database.\n");
16943 zHistory = getenv("SQLITE_HISTORY");
16945 zHistory = strdup(zHistory);
16946 }else if( (zHome = find_home_dir(0))!=0 ){
16947 nHistory = strlen30(zHome) + 20;
16948 if( (zHistory = malloc(nHistory))!=0 ){
16949 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
16952 if( zHistory ){ shell_read_history(zHistory); }
16953 #if HAVE_READLINE || HAVE_EDITLINE
16954 rl_attempted_completion_function = readline_completion;
16955 #elif HAVE_LINENOISE
16956 linenoiseSetCompletionCallback(linenoise_completion);
16959 rc = process_input(&data);
16961 shell_stifle_history(2000);
16962 shell_write_history(zHistory);
16967 rc = process_input(&data);
16970 set_table_name(&data, 0);
16972 session_close_all(&data);
16975 sqlite3_free(data.zFreeOnClose);
16977 output_reset(&data);
16978 data.doXdgOpen = 0;
16979 clearTempFile(&data);
16980 #if !SQLITE_SHELL_IS_UTF8
16981 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
16984 /* Clear the global data structure so that valgrind will detect memory
16986 memset(&data, 0, sizeof(data));