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 ** Optionally #include a user-defined header, whereby compilation options
40 ** may be set prior to where they take effect, but after platform setup.
41 ** If SQLITE_CUSTOM_INCLUDE=? is defined, its value names the #include
42 ** file. Note that this macro has a like effect on sqlite3.c compilation.
44 #ifdef SQLITE_CUSTOM_INCLUDE
45 # define INC_STRINGIFY_(f) #f
46 # define INC_STRINGIFY(f) INC_STRINGIFY_(f)
47 # include INC_STRINGIFY(SQLITE_CUSTOM_INCLUDE)
51 ** Determine if we are dealing with WinRT, which provides only a subset of
52 ** the full Win32 API.
54 #if !defined(SQLITE_OS_WINRT)
55 # define SQLITE_OS_WINRT 0
59 ** Warning pragmas copied from msvc.h in the core.
62 #pragma warning(disable : 4054)
63 #pragma warning(disable : 4055)
64 #pragma warning(disable : 4100)
65 #pragma warning(disable : 4127)
66 #pragma warning(disable : 4130)
67 #pragma warning(disable : 4152)
68 #pragma warning(disable : 4189)
69 #pragma warning(disable : 4206)
70 #pragma warning(disable : 4210)
71 #pragma warning(disable : 4232)
72 #pragma warning(disable : 4244)
73 #pragma warning(disable : 4305)
74 #pragma warning(disable : 4306)
75 #pragma warning(disable : 4702)
76 #pragma warning(disable : 4706)
77 #endif /* defined(_MSC_VER) */
80 ** No support for loadable extensions in VxWorks.
82 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
83 # define SQLITE_OMIT_LOAD_EXTENSION 1
87 ** Enable large-file support for fopen() and friends on unix.
89 #ifndef SQLITE_DISABLE_LFS
90 # define _LARGE_FILE 1
91 # ifndef _FILE_OFFSET_BITS
92 # define _FILE_OFFSET_BITS 64
94 # define _LARGEFILE_SOURCE 1
102 typedef sqlite3_int64 i64;
103 typedef sqlite3_uint64 u64;
104 typedef unsigned char u8;
105 #if SQLITE_USER_AUTHENTICATION
106 # include "sqlite3userauth.h"
111 #if !defined(_WIN32) && !defined(WIN32)
113 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
117 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
120 # define GETPID getpid
121 # if defined(__MINGW32__)
122 # define DIRENT dirent
124 # define S_ISLNK(mode) (0)
128 # define GETPID (int)GetCurrentProcessId
130 #include <sys/types.h>
131 #include <sys/stat.h>
134 # include <readline/readline.h>
135 # include <readline/history.h>
139 # include <editline/readline.h>
142 #if HAVE_EDITLINE || HAVE_READLINE
144 # define shell_add_history(X) add_history(X)
145 # define shell_read_history(X) read_history(X)
146 # define shell_write_history(X) write_history(X)
147 # define shell_stifle_history(X) stifle_history(X)
148 # define shell_readline(X) readline(X)
152 # include "linenoise.h"
153 # define shell_add_history(X) linenoiseHistoryAdd(X)
154 # define shell_read_history(X) linenoiseHistoryLoad(X)
155 # define shell_write_history(X) linenoiseHistorySave(X)
156 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
157 # define shell_readline(X) linenoise(X)
161 # define shell_read_history(X)
162 # define shell_write_history(X)
163 # define shell_stifle_history(X)
165 # define SHELL_USE_LOCAL_GETLINE 1
169 #if defined(_WIN32) || defined(WIN32)
171 # define SQLITE_OMIT_POPEN 1
175 # define isatty(h) _isatty(h)
177 # define access(f,m) _access((f),(m))
180 # define unlink _unlink
183 # define strdup _strdup
186 # define popen _popen
188 # define pclose _pclose
191 /* Make sure isatty() has a prototype. */
192 extern int isatty(int);
194 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
195 /* popen and pclose are not C89 functions and so are
196 ** sometimes omitted from the <stdio.h> header */
197 extern FILE *popen(const char*,const char*);
198 extern int pclose(FILE*);
200 # define SQLITE_OMIT_POPEN 1
204 #if defined(_WIN32_WCE)
205 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
206 * thus we always assume that we have a console. That can be
207 * overridden with the -batch command line option.
212 /* ctype macros that work with signed characters */
213 #define IsSpace(X) isspace((unsigned char)X)
214 #define IsDigit(X) isdigit((unsigned char)X)
215 #define ToLower(X) (char)tolower((unsigned char)X)
217 #if defined(_WIN32) || defined(WIN32)
223 /* string conversion routines only needed on Win32 */
224 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
225 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
226 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
227 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
230 /* On Windows, we normally run with output mode of TEXT so that \n characters
231 ** are automatically translated into \r\n. However, this behavior needs
232 ** to be disabled in some cases (ex: when generating CSV output and when
233 ** rendering quoted strings that contain \n characters). The following
234 ** routines take care of that.
236 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
237 static void setBinaryMode(FILE *file, int isOutput){
238 if( isOutput ) fflush(file);
239 _setmode(_fileno(file), _O_BINARY);
241 static void setTextMode(FILE *file, int isOutput){
242 if( isOutput ) fflush(file);
243 _setmode(_fileno(file), _O_TEXT);
246 # define setBinaryMode(X,Y)
247 # define setTextMode(X,Y)
251 /* True if the timer is enabled */
252 static int enableTimer = 0;
254 /* Return the current wall-clock time */
255 static sqlite3_int64 timeOfDay(void){
256 static sqlite3_vfs *clockVfs = 0;
258 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
259 if( clockVfs==0 ) return 0; /* Never actually happens */
260 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
261 clockVfs->xCurrentTimeInt64(clockVfs, &t);
264 clockVfs->xCurrentTime(clockVfs, &r);
265 t = (sqlite3_int64)(r*86400000.0);
270 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
271 #include <sys/time.h>
272 #include <sys/resource.h>
274 /* VxWorks does not support getrusage() as far as we can determine */
275 #if defined(_WRS_KERNEL) || defined(__RTP__)
277 struct timeval ru_utime; /* user CPU time used */
278 struct timeval ru_stime; /* system CPU time used */
280 #define getrusage(A,B) memset(B,0,sizeof(*B))
283 /* Saved resource information for the beginning of an operation */
284 static struct rusage sBegin; /* CPU time at start */
285 static sqlite3_int64 iBegin; /* Wall-clock time at start */
288 ** Begin timing an operation
290 static void beginTimer(void){
292 getrusage(RUSAGE_SELF, &sBegin);
293 iBegin = timeOfDay();
297 /* Return the difference of two time_structs in seconds */
298 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
299 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
300 (double)(pEnd->tv_sec - pStart->tv_sec);
304 ** Print the timing results.
306 static void endTimer(void){
308 sqlite3_int64 iEnd = timeOfDay();
310 getrusage(RUSAGE_SELF, &sEnd);
311 printf("Run Time: real %.3f user %f sys %f\n",
312 (iEnd - iBegin)*0.001,
313 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
314 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
318 #define BEGIN_TIMER beginTimer()
319 #define END_TIMER endTimer()
322 #elif (defined(_WIN32) || defined(WIN32))
324 /* Saved resource information for the beginning of an operation */
325 static HANDLE hProcess;
326 static FILETIME ftKernelBegin;
327 static FILETIME ftUserBegin;
328 static sqlite3_int64 ftWallBegin;
329 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
330 LPFILETIME, LPFILETIME);
331 static GETPROCTIMES getProcessTimesAddr = NULL;
334 ** Check to see if we have timer support. Return 1 if necessary
335 ** support found (or found previously).
337 static int hasTimer(void){
338 if( getProcessTimesAddr ){
342 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
343 ** versions. See if the version we are running on has it, and if it
344 ** does, save off a pointer to it and the current process handle.
346 hProcess = GetCurrentProcess();
348 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
349 if( NULL != hinstLib ){
350 getProcessTimesAddr =
351 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
352 if( NULL != getProcessTimesAddr ){
355 FreeLibrary(hinstLib);
364 ** Begin timing an operation
366 static void beginTimer(void){
367 if( enableTimer && getProcessTimesAddr ){
368 FILETIME ftCreation, ftExit;
369 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
370 &ftKernelBegin,&ftUserBegin);
371 ftWallBegin = timeOfDay();
375 /* Return the difference of two FILETIME structs in seconds */
376 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
377 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
378 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
379 return (double) ((i64End - i64Start) / 10000000.0);
383 ** Print the timing results.
385 static void endTimer(void){
386 if( enableTimer && getProcessTimesAddr){
387 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
388 sqlite3_int64 ftWallEnd = timeOfDay();
389 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
390 printf("Run Time: real %.3f user %f sys %f\n",
391 (ftWallEnd - ftWallBegin)*0.001,
392 timeDiff(&ftUserBegin, &ftUserEnd),
393 timeDiff(&ftKernelBegin, &ftKernelEnd));
397 #define BEGIN_TIMER beginTimer()
398 #define END_TIMER endTimer()
399 #define HAS_TIMER hasTimer()
408 ** Used to prevent warnings about unused parameters
410 #define UNUSED_PARAMETER(x) (void)(x)
413 ** Number of elements in an array
415 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
418 ** If the following flag is set, then command execution stops
419 ** at an error if we are not interactive.
421 static int bail_on_error = 0;
424 ** Threat stdin as an interactive input if the following variable
425 ** is true. Otherwise, assume stdin is connected to a file or pipe.
427 static int stdin_is_interactive = 1;
430 ** On Windows systems we have to know if standard output is a console
431 ** in order to translate UTF-8 into MBCS. The following variable is
432 ** true if translation is required.
434 static int stdout_is_console = 1;
437 ** The following is the open SQLite database. We make a pointer
438 ** to this database a static variable so that it can be accessed
439 ** by the SIGINT handler to interrupt database processing.
441 static sqlite3 *globalDb = 0;
444 ** True if an interrupt (Control-C) has been received.
446 static volatile int seenInterrupt = 0;
450 ** Out-of-memory simulator variables
452 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
453 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
454 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
455 #endif /* SQLITE_DEBUG */
458 ** This is the name of our program. It is set in main(), used
459 ** in a number of other places, mostly for error messages.
464 ** Prompt strings. Initialized in main. Settable with
465 ** .prompt main continue
467 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
468 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
471 ** Render output like fprintf(). Except, if the output is going to the
472 ** console and if this is running on a Windows machine, translate the
473 ** output from UTF-8 into MBCS.
475 #if defined(_WIN32) || defined(WIN32)
476 void utf8_printf(FILE *out, const char *zFormat, ...){
478 va_start(ap, zFormat);
479 if( stdout_is_console && (out==stdout || out==stderr) ){
480 char *z1 = sqlite3_vmprintf(zFormat, ap);
481 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
486 vfprintf(out, zFormat, ap);
490 #elif !defined(utf8_printf)
491 # define utf8_printf fprintf
495 ** Render output like fprintf(). This should not be used on anything that
496 ** includes string formatting (e.g. "%s").
498 #if !defined(raw_printf)
499 # define raw_printf fprintf
502 /* Indicate out-of-memory and exit. */
503 static void shell_out_of_memory(void){
504 raw_printf(stderr,"Error: out of memory\n");
509 /* This routine is called when a simulated OOM occurs. It is broken
510 ** out as a separate routine to make it easy to set a breakpoint on
513 void shellOomFault(void){
520 #endif /* SQLITE_DEBUG */
523 /* This routine is a replacement malloc() that is used to simulate
524 ** Out-Of-Memory (OOM) errors for testing purposes.
526 static void *oomMalloc(int nByte){
535 return defaultMalloc(nByte);
537 #endif /* SQLITE_DEBUG */
540 /* Register the OOM simulator. This must occur before any memory
542 static void registerOomSimulator(void){
543 sqlite3_mem_methods mem;
544 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
545 defaultMalloc = mem.xMalloc;
546 mem.xMalloc = oomMalloc;
547 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
552 ** Write I/O traces to the following stream.
554 #ifdef SQLITE_ENABLE_IOTRACE
555 static FILE *iotrace = 0;
559 ** This routine works like printf in that its first argument is a
560 ** format string and subsequent arguments are values to be substituted
561 ** in place of % fields. The result of formatting this string
562 ** is written to iotrace.
564 #ifdef SQLITE_ENABLE_IOTRACE
565 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
568 if( iotrace==0 ) return;
569 va_start(ap, zFormat);
570 z = sqlite3_vmprintf(zFormat, ap);
572 utf8_printf(iotrace, "%s", z);
578 ** Output string zUtf to stream pOut as w characters. If w is negative,
579 ** then right-justify the text. W is the width in UTF-8 characters, not
580 ** in bytes. This is different from the %*.*s specification in printf
581 ** since with %*.*s the width is measured in bytes, not characters.
583 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
586 int aw = w<0 ? -w : w;
587 for(i=n=0; zUtf[i]; i++){
588 if( (zUtf[i]&0xc0)!=0x80 ){
591 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
597 utf8_printf(pOut, "%.*s", i, zUtf);
599 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
601 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
607 ** Determines if a string is a number of not.
609 static int isNumber(const char *z, int *realnum){
610 if( *z=='-' || *z=='+' ) z++;
615 if( realnum ) *realnum = 0;
616 while( IsDigit(*z) ){ z++; }
619 if( !IsDigit(*z) ) return 0;
620 while( IsDigit(*z) ){ z++; }
621 if( realnum ) *realnum = 1;
623 if( *z=='e' || *z=='E' ){
625 if( *z=='+' || *z=='-' ) z++;
626 if( !IsDigit(*z) ) return 0;
627 while( IsDigit(*z) ){ z++; }
628 if( realnum ) *realnum = 1;
634 ** Compute a string length that is limited to what can be stored in
635 ** lower 30 bits of a 32-bit signed integer.
637 static int strlen30(const char *z){
639 while( *z2 ){ z2++; }
640 return 0x3fffffff & (int)(z2 - z);
644 ** Return the length of a string in characters. Multibyte UTF8 characters
645 ** count as a single character.
647 static int strlenChar(const char *z){
650 if( (0xc0&*(z++))!=0x80 ) n++;
656 ** Return open FILE * if zFile exists, can be opened for read
657 ** and is an ordinary file or a character stream source.
658 ** Otherwise return 0.
660 static FILE * openChrSource(const char *zFile){
662 struct _stat x = {0};
663 # define STAT_CHR_SRC(mode) ((mode & (_S_IFCHR|_S_IFIFO|_S_IFREG))!=0)
664 /* On Windows, open first, then check the stream nature. This order
665 ** is necessary because _stat() and sibs, when checking a named pipe,
666 ** effectively break the pipe as its supplier sees it. */
667 FILE *rv = fopen(zFile, "rb");
668 if( rv==0 ) return 0;
669 if( _fstat(_fileno(rv), &x) != 0
670 || !STAT_CHR_SRC(x.st_mode)){
677 int rc = stat(zFile, &x);
678 # define STAT_CHR_SRC(mode) (S_ISREG(mode)||S_ISFIFO(mode)||S_ISCHR(mode))
679 if( rc!=0 ) return 0;
680 if( STAT_CHR_SRC(x.st_mode) ){
681 return fopen(zFile, "rb");
690 ** This routine reads a line of text from FILE in, stores
691 ** the text in memory obtained from malloc() and returns a pointer
692 ** to the text. NULL is returned at end of file, or if malloc()
695 ** If zLine is not NULL then it is a malloced buffer returned from
696 ** a previous call to this routine that may be reused.
698 static char *local_getline(char *zLine, FILE *in){
699 int nLine = zLine==0 ? 0 : 100;
704 nLine = nLine*2 + 100;
705 zLine = realloc(zLine, nLine);
706 if( zLine==0 ) shell_out_of_memory();
708 if( fgets(&zLine[n], nLine - n, in)==0 ){
716 while( zLine[n] ) n++;
717 if( n>0 && zLine[n-1]=='\n' ){
719 if( n>0 && zLine[n-1]=='\r' ) n--;
724 #if defined(_WIN32) || defined(WIN32)
725 /* For interactive input on Windows systems, translate the
726 ** multi-byte characterset characters into UTF-8. */
727 if( stdin_is_interactive && in==stdin ){
728 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
730 int nTrans = strlen30(zTrans)+1;
732 zLine = realloc(zLine, nTrans);
733 if( zLine==0 ) shell_out_of_memory();
735 memcpy(zLine, zTrans, nTrans);
736 sqlite3_free(zTrans);
739 #endif /* defined(_WIN32) || defined(WIN32) */
744 ** Retrieve a single line of input text.
746 ** If in==0 then read from standard input and prompt before each line.
747 ** If isContinuation is true, then a continuation prompt is appropriate.
748 ** If isContinuation is zero, then the main prompt should be used.
750 ** If zPrior is not NULL then it is a buffer from a prior call to this
751 ** routine that can be reused.
753 ** The result is stored in space obtained from malloc() and must either
754 ** be freed by the caller or else passed back into this routine via the
755 ** zPrior argument for reuse.
757 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
761 zResult = local_getline(zPrior, in);
763 zPrompt = isContinuation ? continuePrompt : mainPrompt;
764 #if SHELL_USE_LOCAL_GETLINE
765 printf("%s", zPrompt);
767 zResult = local_getline(zPrior, stdin);
770 zResult = shell_readline(zPrompt);
771 if( zResult && *zResult ) shell_add_history(zResult);
779 ** Return the value of a hexadecimal digit. Return -1 if the input
780 ** is not a hex digit.
782 static int hexDigitValue(char c){
783 if( c>='0' && c<='9' ) return c - '0';
784 if( c>='a' && c<='f' ) return c - 'a' + 10;
785 if( c>='A' && c<='F' ) return c - 'A' + 10;
790 ** Interpret zArg as an integer value, possibly with suffixes.
792 static sqlite3_int64 integerValue(const char *zArg){
794 static const struct { char *zSuffix; int iMult; } aMult[] = {
796 { "MiB", 1024*1024 },
797 { "GiB", 1024*1024*1024 },
800 { "GB", 1000000000 },
810 }else if( zArg[0]=='+' ){
813 if( zArg[0]=='0' && zArg[1]=='x' ){
816 while( (x = hexDigitValue(zArg[0]))>=0 ){
821 while( IsDigit(zArg[0]) ){
822 v = v*10 + zArg[0] - '0';
826 for(i=0; i<ArraySize(aMult); i++){
827 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
832 return isNeg? -v : v;
836 ** A variable length string to which one can append text.
838 typedef struct ShellText ShellText;
846 ** Initialize and destroy a ShellText object
848 static void initText(ShellText *p){
849 memset(p, 0, sizeof(*p));
851 static void freeText(ShellText *p){
856 /* zIn is either a pointer to a NULL-terminated string in memory obtained
857 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
858 ** added to zIn, and the result returned in memory obtained from malloc().
859 ** zIn, if it was not NULL, is freed.
861 ** If the third argument, quote, is not '\0', then it is used as a
862 ** quote character for zAppend.
864 static void appendText(ShellText *p, char const *zAppend, char quote){
867 int nAppend = strlen30(zAppend);
869 len = nAppend+p->n+1;
872 for(i=0; i<nAppend; i++){
873 if( zAppend[i]==quote ) len++;
877 if( p->z==0 || p->n+len>=p->nAlloc ){
878 p->nAlloc = p->nAlloc*2 + len + 20;
879 p->z = realloc(p->z, p->nAlloc);
880 if( p->z==0 ) shell_out_of_memory();
884 char *zCsr = p->z+p->n;
886 for(i=0; i<nAppend; i++){
887 *zCsr++ = zAppend[i];
888 if( zAppend[i]==quote ) *zCsr++ = quote;
891 p->n = (int)(zCsr - p->z);
894 memcpy(p->z+p->n, zAppend, nAppend);
901 ** Attempt to determine if identifier zName needs to be quoted, either
902 ** because it contains non-alphanumeric characters, or because it is an
903 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
904 ** that quoting is required.
906 ** Return '"' if quoting is required. Return 0 if no quoting is required.
908 static char quoteChar(const char *zName){
910 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
911 for(i=0; zName[i]; i++){
912 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
914 return sqlite3_keyword_check(zName, i) ? '"' : 0;
918 ** Construct a fake object name and column list to describe the structure
919 ** of the view, virtual table, or table valued function zSchema.zName.
921 static char *shellFakeSchema(
922 sqlite3 *db, /* The database connection containing the vtab */
923 const char *zSchema, /* Schema of the database holding the vtab */
924 const char *zName /* The name of the virtual table */
926 sqlite3_stmt *pStmt = 0;
933 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
934 zSchema ? zSchema : "main", zName);
935 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
939 cQuote = quoteChar(zSchema);
940 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
941 appendText(&s, zSchema, cQuote);
942 appendText(&s, ".", 0);
944 cQuote = quoteChar(zName);
945 appendText(&s, zName, cQuote);
946 while( sqlite3_step(pStmt)==SQLITE_ROW ){
947 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
949 appendText(&s, zDiv, 0);
951 cQuote = quoteChar(zCol);
952 appendText(&s, zCol, cQuote);
954 appendText(&s, ")", 0);
955 sqlite3_finalize(pStmt);
964 ** SQL function: shell_module_schema(X)
966 ** Return a fake schema for the table-valued function or eponymous virtual
969 static void shellModuleSchema(
970 sqlite3_context *pCtx,
972 sqlite3_value **apVal
974 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
975 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
976 UNUSED_PARAMETER(nVal);
978 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
985 ** SQL function: shell_add_schema(S,X)
987 ** Add the schema name X to the CREATE statement in S and return the result.
990 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
995 ** CREATE UNIQUE INDEX
998 ** CREATE VIRTUAL TABLE
1000 ** This UDF is used by the .schema command to insert the schema name of
1001 ** attached databases into the middle of the sqlite_schema.sql field.
1003 static void shellAddSchemaName(
1004 sqlite3_context *pCtx,
1006 sqlite3_value **apVal
1008 static const char *aPrefix[] = {
1017 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
1018 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
1019 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
1020 sqlite3 *db = sqlite3_context_db_handle(pCtx);
1021 UNUSED_PARAMETER(nVal);
1022 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
1023 for(i=0; i<ArraySize(aPrefix); i++){
1024 int n = strlen30(aPrefix[i]);
1025 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
1029 char cQuote = quoteChar(zSchema);
1030 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1031 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1033 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1037 && aPrefix[i][0]=='V'
1038 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1041 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1043 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1048 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1054 sqlite3_result_value(pCtx, apVal[0]);
1058 ** The source code for several run-time loadable extensions is inserted
1059 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1060 ** code, we need to override some macros to make the included program code
1061 ** work here in the middle of this regular program.
1063 #define SQLITE_EXTENSION_INIT1
1064 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1066 #if defined(_WIN32) && defined(_MSC_VER)
1067 /************************* Begin test_windirent.h ******************/
1071 ** The author disclaims copyright to this source code. In place of
1072 ** a legal notice, here is a blessing:
1074 ** May you do good and not evil.
1075 ** May you find forgiveness for yourself and forgive others.
1076 ** May you share freely, never taking more than you give.
1078 *************************************************************************
1079 ** This file contains declarations for most of the opendir() family of
1080 ** POSIX functions on Win32 using the MSVCRT.
1083 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1084 #define SQLITE_WINDIRENT_H
1087 ** We need several data types from the Windows SDK header.
1090 #ifndef WIN32_LEAN_AND_MEAN
1091 #define WIN32_LEAN_AND_MEAN
1094 #include "windows.h"
1097 ** We need several support functions from the SQLite core.
1100 /* #include "sqlite3.h" */
1103 ** We need several things from the ANSI and MSVCRT headers.
1111 #include <sys/types.h>
1112 #include <sys/stat.h>
1115 ** We may need several defines that should have been in "sys/stat.h".
1119 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1123 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1127 #define S_ISLNK(mode) (0)
1131 ** We may need to provide the "mode_t" type.
1134 #ifndef MODE_T_DEFINED
1135 #define MODE_T_DEFINED
1136 typedef unsigned short mode_t;
1140 ** We may need to provide the "ino_t" type.
1143 #ifndef INO_T_DEFINED
1144 #define INO_T_DEFINED
1145 typedef unsigned short ino_t;
1149 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1153 # ifdef FILENAME_MAX
1154 # define NAME_MAX (FILENAME_MAX)
1156 # define NAME_MAX (260)
1161 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1164 #ifndef NULL_INTPTR_T
1165 # define NULL_INTPTR_T ((intptr_t)(0))
1168 #ifndef BAD_INTPTR_T
1169 # define BAD_INTPTR_T ((intptr_t)(-1))
1173 ** We need to provide the necessary structures and related types.
1176 #ifndef DIRENT_DEFINED
1177 #define DIRENT_DEFINED
1178 typedef struct DIRENT DIRENT;
1179 typedef DIRENT *LPDIRENT;
1181 ino_t d_ino; /* Sequence number, do not use. */
1182 unsigned d_attributes; /* Win32 file attributes. */
1183 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1189 typedef struct DIR DIR;
1192 intptr_t d_handle; /* Value returned by "_findfirst". */
1193 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1194 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1199 ** Provide a macro, for use by the implementation, to determine if a
1200 ** particular directory entry should be skipped over when searching for
1201 ** the next directory entry that should be returned by the readdir() or
1202 ** readdir_r() functions.
1206 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1210 ** Provide the function prototype for the POSIX compatiable getenv()
1211 ** function. This function is not thread-safe.
1214 extern const char *windirent_getenv(const char *name);
1217 ** Finally, we can provide the function prototypes for the opendir(),
1218 ** readdir(), readdir_r(), and closedir() POSIX functions.
1221 extern LPDIR opendir(const char *dirname);
1222 extern LPDIRENT readdir(LPDIR dirp);
1223 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1224 extern INT closedir(LPDIR dirp);
1226 #endif /* defined(WIN32) && defined(_MSC_VER) */
1228 /************************* End test_windirent.h ********************/
1229 /************************* Begin test_windirent.c ******************/
1233 ** The author disclaims copyright to this source code. In place of
1234 ** a legal notice, here is a blessing:
1236 ** May you do good and not evil.
1237 ** May you find forgiveness for yourself and forgive others.
1238 ** May you share freely, never taking more than you give.
1240 *************************************************************************
1241 ** This file contains code to implement most of the opendir() family of
1242 ** POSIX functions on Win32 using the MSVCRT.
1245 #if defined(_WIN32) && defined(_MSC_VER)
1246 /* #include "test_windirent.h" */
1249 ** Implementation of the POSIX getenv() function using the Win32 API.
1250 ** This function is not thread-safe.
1252 const char *windirent_getenv(
1255 static char value[32768]; /* Maximum length, per MSDN */
1256 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1257 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1259 memset(value, 0, sizeof(value));
1260 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1261 if( dwRet==0 || dwRet>dwSize ){
1263 ** The function call to GetEnvironmentVariableA() failed -OR-
1264 ** the buffer is not large enough. Either way, return NULL.
1269 ** The function call to GetEnvironmentVariableA() succeeded
1270 ** -AND- the buffer contains the entire value.
1277 ** Implementation of the POSIX opendir() function using the MSVCRT.
1282 struct _finddata_t data;
1283 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1284 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1286 if( dirp==NULL ) return NULL;
1287 memset(dirp, 0, sizeof(DIR));
1289 /* TODO: Remove this if Unix-style root paths are not used. */
1290 if( sqlite3_stricmp(dirname, "/")==0 ){
1291 dirname = windirent_getenv("SystemDrive");
1294 memset(&data, 0, sizeof(struct _finddata_t));
1295 _snprintf(data.name, namesize, "%s\\*", dirname);
1296 dirp->d_handle = _findfirst(data.name, &data);
1298 if( dirp->d_handle==BAD_INTPTR_T ){
1303 /* TODO: Remove this block to allow hidden and/or system files. */
1304 if( is_filtered(data) ){
1307 memset(&data, 0, sizeof(struct _finddata_t));
1308 if( _findnext(dirp->d_handle, &data)==-1 ){
1313 /* TODO: Remove this block to allow hidden and/or system files. */
1314 if( is_filtered(data) ) goto next;
1317 dirp->d_first.d_attributes = data.attrib;
1318 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1319 dirp->d_first.d_name[NAME_MAX] = '\0';
1325 ** Implementation of the POSIX readdir() function using the MSVCRT.
1330 struct _finddata_t data;
1332 if( dirp==NULL ) return NULL;
1334 if( dirp->d_first.d_ino==0 ){
1335 dirp->d_first.d_ino++;
1336 dirp->d_next.d_ino++;
1338 return &dirp->d_first;
1343 memset(&data, 0, sizeof(struct _finddata_t));
1344 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1346 /* TODO: Remove this block to allow hidden and/or system files. */
1347 if( is_filtered(data) ) goto next;
1349 dirp->d_next.d_ino++;
1350 dirp->d_next.d_attributes = data.attrib;
1351 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1352 dirp->d_next.d_name[NAME_MAX] = '\0';
1354 return &dirp->d_next;
1358 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1365 struct _finddata_t data;
1367 if( dirp==NULL ) return EBADF;
1369 if( dirp->d_first.d_ino==0 ){
1370 dirp->d_first.d_ino++;
1371 dirp->d_next.d_ino++;
1373 entry->d_ino = dirp->d_first.d_ino;
1374 entry->d_attributes = dirp->d_first.d_attributes;
1375 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1376 entry->d_name[NAME_MAX] = '\0';
1384 memset(&data, 0, sizeof(struct _finddata_t));
1385 if( _findnext(dirp->d_handle, &data)==-1 ){
1390 /* TODO: Remove this block to allow hidden and/or system files. */
1391 if( is_filtered(data) ) goto next;
1393 entry->d_ino = (ino_t)-1; /* not available */
1394 entry->d_attributes = data.attrib;
1395 strncpy(entry->d_name, data.name, NAME_MAX);
1396 entry->d_name[NAME_MAX] = '\0';
1403 ** Implementation of the POSIX closedir() function using the MSVCRT.
1410 if( dirp==NULL ) return EINVAL;
1412 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1413 result = _findclose(dirp->d_handle);
1420 #endif /* defined(WIN32) && defined(_MSC_VER) */
1422 /************************* End test_windirent.c ********************/
1423 #define dirent DIRENT
1425 /************************* Begin ../ext/misc/shathree.c ******************/
1429 ** The author disclaims copyright to this source code. In place of
1430 ** a legal notice, here is a blessing:
1432 ** May you do good and not evil.
1433 ** May you find forgiveness for yourself and forgive others.
1434 ** May you share freely, never taking more than you give.
1436 ******************************************************************************
1438 ** This SQLite extension implements functions that compute SHA3 hashes.
1439 ** Two SQL functions are implemented:
1442 ** sha3_query(Y,SIZE)
1444 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1447 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1448 ** and returns a hash of their results.
1450 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1451 ** is used. If SIZE is included it must be one of the integers 224, 256,
1452 ** 384, or 512, to determine SHA3 hash variant that is computed.
1454 /* #include "sqlite3ext.h" */
1455 SQLITE_EXTENSION_INIT1
1460 #ifndef SQLITE_AMALGAMATION
1461 /* typedef sqlite3_uint64 u64; */
1462 #endif /* SQLITE_AMALGAMATION */
1464 /******************************************************************************
1468 ** Macros to determine whether the machine is big or little endian,
1469 ** and whether or not that determination is run-time or compile-time.
1471 ** For best performance, an attempt is made to guess at the byte-order
1472 ** using C-preprocessor macros. If that is unsuccessful, or if
1473 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1476 #ifndef SHA3_BYTEORDER
1477 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1478 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1479 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1481 # define SHA3_BYTEORDER 1234
1482 # elif defined(sparc) || defined(__ppc__)
1483 # define SHA3_BYTEORDER 4321
1485 # define SHA3_BYTEORDER 0
1491 ** State structure for a SHA3 hash in progress
1493 typedef struct SHA3Context SHA3Context;
1494 struct SHA3Context {
1496 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1497 unsigned char x[1600]; /* ... or 1600 bytes */
1499 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1500 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1501 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1505 ** A single step of the Keccak mixing function for a 1600-bit state
1507 static void KeccakF1600Step(SHA3Context *p){
1509 u64 b0, b1, b2, b3, b4;
1510 u64 c0, c1, c2, c3, c4;
1511 u64 d0, d1, d2, d3, d4;
1512 static const u64 RC[] = {
1513 0x0000000000000001ULL, 0x0000000000008082ULL,
1514 0x800000000000808aULL, 0x8000000080008000ULL,
1515 0x000000000000808bULL, 0x0000000080000001ULL,
1516 0x8000000080008081ULL, 0x8000000000008009ULL,
1517 0x000000000000008aULL, 0x0000000000000088ULL,
1518 0x0000000080008009ULL, 0x000000008000000aULL,
1519 0x000000008000808bULL, 0x800000000000008bULL,
1520 0x8000000000008089ULL, 0x8000000000008003ULL,
1521 0x8000000000008002ULL, 0x8000000000000080ULL,
1522 0x000000000000800aULL, 0x800000008000000aULL,
1523 0x8000000080008081ULL, 0x8000000000008080ULL,
1524 0x0000000080000001ULL, 0x8000000080008008ULL
1526 # define a00 (p->u.s[0])
1527 # define a01 (p->u.s[1])
1528 # define a02 (p->u.s[2])
1529 # define a03 (p->u.s[3])
1530 # define a04 (p->u.s[4])
1531 # define a10 (p->u.s[5])
1532 # define a11 (p->u.s[6])
1533 # define a12 (p->u.s[7])
1534 # define a13 (p->u.s[8])
1535 # define a14 (p->u.s[9])
1536 # define a20 (p->u.s[10])
1537 # define a21 (p->u.s[11])
1538 # define a22 (p->u.s[12])
1539 # define a23 (p->u.s[13])
1540 # define a24 (p->u.s[14])
1541 # define a30 (p->u.s[15])
1542 # define a31 (p->u.s[16])
1543 # define a32 (p->u.s[17])
1544 # define a33 (p->u.s[18])
1545 # define a34 (p->u.s[19])
1546 # define a40 (p->u.s[20])
1547 # define a41 (p->u.s[21])
1548 # define a42 (p->u.s[22])
1549 # define a43 (p->u.s[23])
1550 # define a44 (p->u.s[24])
1551 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1553 for(i=0; i<24; i+=4){
1554 c0 = a00^a10^a20^a30^a40;
1555 c1 = a01^a11^a21^a31^a41;
1556 c2 = a02^a12^a22^a32^a42;
1557 c3 = a03^a13^a23^a33^a43;
1558 c4 = a04^a14^a24^a34^a44;
1559 d0 = c4^ROL64(c1, 1);
1560 d1 = c0^ROL64(c2, 1);
1561 d2 = c1^ROL64(c3, 1);
1562 d3 = c2^ROL64(c4, 1);
1563 d4 = c3^ROL64(c0, 1);
1566 b1 = ROL64((a11^d1), 44);
1567 b2 = ROL64((a22^d2), 43);
1568 b3 = ROL64((a33^d3), 21);
1569 b4 = ROL64((a44^d4), 14);
1570 a00 = b0 ^((~b1)& b2 );
1572 a11 = b1 ^((~b2)& b3 );
1573 a22 = b2 ^((~b3)& b4 );
1574 a33 = b3 ^((~b4)& b0 );
1575 a44 = b4 ^((~b0)& b1 );
1577 b2 = ROL64((a20^d0), 3);
1578 b3 = ROL64((a31^d1), 45);
1579 b4 = ROL64((a42^d2), 61);
1580 b0 = ROL64((a03^d3), 28);
1581 b1 = ROL64((a14^d4), 20);
1582 a20 = b0 ^((~b1)& b2 );
1583 a31 = b1 ^((~b2)& b3 );
1584 a42 = b2 ^((~b3)& b4 );
1585 a03 = b3 ^((~b4)& b0 );
1586 a14 = b4 ^((~b0)& b1 );
1588 b4 = ROL64((a40^d0), 18);
1589 b0 = ROL64((a01^d1), 1);
1590 b1 = ROL64((a12^d2), 6);
1591 b2 = ROL64((a23^d3), 25);
1592 b3 = ROL64((a34^d4), 8);
1593 a40 = b0 ^((~b1)& b2 );
1594 a01 = b1 ^((~b2)& b3 );
1595 a12 = b2 ^((~b3)& b4 );
1596 a23 = b3 ^((~b4)& b0 );
1597 a34 = b4 ^((~b0)& b1 );
1599 b1 = ROL64((a10^d0), 36);
1600 b2 = ROL64((a21^d1), 10);
1601 b3 = ROL64((a32^d2), 15);
1602 b4 = ROL64((a43^d3), 56);
1603 b0 = ROL64((a04^d4), 27);
1604 a10 = b0 ^((~b1)& b2 );
1605 a21 = b1 ^((~b2)& b3 );
1606 a32 = b2 ^((~b3)& b4 );
1607 a43 = b3 ^((~b4)& b0 );
1608 a04 = b4 ^((~b0)& b1 );
1610 b3 = ROL64((a30^d0), 41);
1611 b4 = ROL64((a41^d1), 2);
1612 b0 = ROL64((a02^d2), 62);
1613 b1 = ROL64((a13^d3), 55);
1614 b2 = ROL64((a24^d4), 39);
1615 a30 = b0 ^((~b1)& b2 );
1616 a41 = b1 ^((~b2)& b3 );
1617 a02 = b2 ^((~b3)& b4 );
1618 a13 = b3 ^((~b4)& b0 );
1619 a24 = b4 ^((~b0)& b1 );
1621 c0 = a00^a20^a40^a10^a30;
1622 c1 = a11^a31^a01^a21^a41;
1623 c2 = a22^a42^a12^a32^a02;
1624 c3 = a33^a03^a23^a43^a13;
1625 c4 = a44^a14^a34^a04^a24;
1626 d0 = c4^ROL64(c1, 1);
1627 d1 = c0^ROL64(c2, 1);
1628 d2 = c1^ROL64(c3, 1);
1629 d3 = c2^ROL64(c4, 1);
1630 d4 = c3^ROL64(c0, 1);
1633 b1 = ROL64((a31^d1), 44);
1634 b2 = ROL64((a12^d2), 43);
1635 b3 = ROL64((a43^d3), 21);
1636 b4 = ROL64((a24^d4), 14);
1637 a00 = b0 ^((~b1)& b2 );
1639 a31 = b1 ^((~b2)& b3 );
1640 a12 = b2 ^((~b3)& b4 );
1641 a43 = b3 ^((~b4)& b0 );
1642 a24 = b4 ^((~b0)& b1 );
1644 b2 = ROL64((a40^d0), 3);
1645 b3 = ROL64((a21^d1), 45);
1646 b4 = ROL64((a02^d2), 61);
1647 b0 = ROL64((a33^d3), 28);
1648 b1 = ROL64((a14^d4), 20);
1649 a40 = b0 ^((~b1)& b2 );
1650 a21 = b1 ^((~b2)& b3 );
1651 a02 = b2 ^((~b3)& b4 );
1652 a33 = b3 ^((~b4)& b0 );
1653 a14 = b4 ^((~b0)& b1 );
1655 b4 = ROL64((a30^d0), 18);
1656 b0 = ROL64((a11^d1), 1);
1657 b1 = ROL64((a42^d2), 6);
1658 b2 = ROL64((a23^d3), 25);
1659 b3 = ROL64((a04^d4), 8);
1660 a30 = b0 ^((~b1)& b2 );
1661 a11 = b1 ^((~b2)& b3 );
1662 a42 = b2 ^((~b3)& b4 );
1663 a23 = b3 ^((~b4)& b0 );
1664 a04 = b4 ^((~b0)& b1 );
1666 b1 = ROL64((a20^d0), 36);
1667 b2 = ROL64((a01^d1), 10);
1668 b3 = ROL64((a32^d2), 15);
1669 b4 = ROL64((a13^d3), 56);
1670 b0 = ROL64((a44^d4), 27);
1671 a20 = b0 ^((~b1)& b2 );
1672 a01 = b1 ^((~b2)& b3 );
1673 a32 = b2 ^((~b3)& b4 );
1674 a13 = b3 ^((~b4)& b0 );
1675 a44 = b4 ^((~b0)& b1 );
1677 b3 = ROL64((a10^d0), 41);
1678 b4 = ROL64((a41^d1), 2);
1679 b0 = ROL64((a22^d2), 62);
1680 b1 = ROL64((a03^d3), 55);
1681 b2 = ROL64((a34^d4), 39);
1682 a10 = b0 ^((~b1)& b2 );
1683 a41 = b1 ^((~b2)& b3 );
1684 a22 = b2 ^((~b3)& b4 );
1685 a03 = b3 ^((~b4)& b0 );
1686 a34 = b4 ^((~b0)& b1 );
1688 c0 = a00^a40^a30^a20^a10;
1689 c1 = a31^a21^a11^a01^a41;
1690 c2 = a12^a02^a42^a32^a22;
1691 c3 = a43^a33^a23^a13^a03;
1692 c4 = a24^a14^a04^a44^a34;
1693 d0 = c4^ROL64(c1, 1);
1694 d1 = c0^ROL64(c2, 1);
1695 d2 = c1^ROL64(c3, 1);
1696 d3 = c2^ROL64(c4, 1);
1697 d4 = c3^ROL64(c0, 1);
1700 b1 = ROL64((a21^d1), 44);
1701 b2 = ROL64((a42^d2), 43);
1702 b3 = ROL64((a13^d3), 21);
1703 b4 = ROL64((a34^d4), 14);
1704 a00 = b0 ^((~b1)& b2 );
1706 a21 = b1 ^((~b2)& b3 );
1707 a42 = b2 ^((~b3)& b4 );
1708 a13 = b3 ^((~b4)& b0 );
1709 a34 = b4 ^((~b0)& b1 );
1711 b2 = ROL64((a30^d0), 3);
1712 b3 = ROL64((a01^d1), 45);
1713 b4 = ROL64((a22^d2), 61);
1714 b0 = ROL64((a43^d3), 28);
1715 b1 = ROL64((a14^d4), 20);
1716 a30 = b0 ^((~b1)& b2 );
1717 a01 = b1 ^((~b2)& b3 );
1718 a22 = b2 ^((~b3)& b4 );
1719 a43 = b3 ^((~b4)& b0 );
1720 a14 = b4 ^((~b0)& b1 );
1722 b4 = ROL64((a10^d0), 18);
1723 b0 = ROL64((a31^d1), 1);
1724 b1 = ROL64((a02^d2), 6);
1725 b2 = ROL64((a23^d3), 25);
1726 b3 = ROL64((a44^d4), 8);
1727 a10 = b0 ^((~b1)& b2 );
1728 a31 = b1 ^((~b2)& b3 );
1729 a02 = b2 ^((~b3)& b4 );
1730 a23 = b3 ^((~b4)& b0 );
1731 a44 = b4 ^((~b0)& b1 );
1733 b1 = ROL64((a40^d0), 36);
1734 b2 = ROL64((a11^d1), 10);
1735 b3 = ROL64((a32^d2), 15);
1736 b4 = ROL64((a03^d3), 56);
1737 b0 = ROL64((a24^d4), 27);
1738 a40 = b0 ^((~b1)& b2 );
1739 a11 = b1 ^((~b2)& b3 );
1740 a32 = b2 ^((~b3)& b4 );
1741 a03 = b3 ^((~b4)& b0 );
1742 a24 = b4 ^((~b0)& b1 );
1744 b3 = ROL64((a20^d0), 41);
1745 b4 = ROL64((a41^d1), 2);
1746 b0 = ROL64((a12^d2), 62);
1747 b1 = ROL64((a33^d3), 55);
1748 b2 = ROL64((a04^d4), 39);
1749 a20 = b0 ^((~b1)& b2 );
1750 a41 = b1 ^((~b2)& b3 );
1751 a12 = b2 ^((~b3)& b4 );
1752 a33 = b3 ^((~b4)& b0 );
1753 a04 = b4 ^((~b0)& b1 );
1755 c0 = a00^a30^a10^a40^a20;
1756 c1 = a21^a01^a31^a11^a41;
1757 c2 = a42^a22^a02^a32^a12;
1758 c3 = a13^a43^a23^a03^a33;
1759 c4 = a34^a14^a44^a24^a04;
1760 d0 = c4^ROL64(c1, 1);
1761 d1 = c0^ROL64(c2, 1);
1762 d2 = c1^ROL64(c3, 1);
1763 d3 = c2^ROL64(c4, 1);
1764 d4 = c3^ROL64(c0, 1);
1767 b1 = ROL64((a01^d1), 44);
1768 b2 = ROL64((a02^d2), 43);
1769 b3 = ROL64((a03^d3), 21);
1770 b4 = ROL64((a04^d4), 14);
1771 a00 = b0 ^((~b1)& b2 );
1773 a01 = b1 ^((~b2)& b3 );
1774 a02 = b2 ^((~b3)& b4 );
1775 a03 = b3 ^((~b4)& b0 );
1776 a04 = b4 ^((~b0)& b1 );
1778 b2 = ROL64((a10^d0), 3);
1779 b3 = ROL64((a11^d1), 45);
1780 b4 = ROL64((a12^d2), 61);
1781 b0 = ROL64((a13^d3), 28);
1782 b1 = ROL64((a14^d4), 20);
1783 a10 = b0 ^((~b1)& b2 );
1784 a11 = b1 ^((~b2)& b3 );
1785 a12 = b2 ^((~b3)& b4 );
1786 a13 = b3 ^((~b4)& b0 );
1787 a14 = b4 ^((~b0)& b1 );
1789 b4 = ROL64((a20^d0), 18);
1790 b0 = ROL64((a21^d1), 1);
1791 b1 = ROL64((a22^d2), 6);
1792 b2 = ROL64((a23^d3), 25);
1793 b3 = ROL64((a24^d4), 8);
1794 a20 = b0 ^((~b1)& b2 );
1795 a21 = b1 ^((~b2)& b3 );
1796 a22 = b2 ^((~b3)& b4 );
1797 a23 = b3 ^((~b4)& b0 );
1798 a24 = b4 ^((~b0)& b1 );
1800 b1 = ROL64((a30^d0), 36);
1801 b2 = ROL64((a31^d1), 10);
1802 b3 = ROL64((a32^d2), 15);
1803 b4 = ROL64((a33^d3), 56);
1804 b0 = ROL64((a34^d4), 27);
1805 a30 = b0 ^((~b1)& b2 );
1806 a31 = b1 ^((~b2)& b3 );
1807 a32 = b2 ^((~b3)& b4 );
1808 a33 = b3 ^((~b4)& b0 );
1809 a34 = b4 ^((~b0)& b1 );
1811 b3 = ROL64((a40^d0), 41);
1812 b4 = ROL64((a41^d1), 2);
1813 b0 = ROL64((a42^d2), 62);
1814 b1 = ROL64((a43^d3), 55);
1815 b2 = ROL64((a44^d4), 39);
1816 a40 = b0 ^((~b1)& b2 );
1817 a41 = b1 ^((~b2)& b3 );
1818 a42 = b2 ^((~b3)& b4 );
1819 a43 = b3 ^((~b4)& b0 );
1820 a44 = b4 ^((~b0)& b1 );
1825 ** Initialize a new hash. iSize determines the size of the hash
1826 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1827 ** can be zero to use the default hash size of 256 bits.
1829 static void SHA3Init(SHA3Context *p, int iSize){
1830 memset(p, 0, sizeof(*p));
1831 if( iSize>=128 && iSize<=512 ){
1832 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1834 p->nRate = (1600 - 2*256)/8;
1836 #if SHA3_BYTEORDER==1234
1837 /* Known to be little-endian at compile-time. No-op */
1838 #elif SHA3_BYTEORDER==4321
1839 p->ixMask = 7; /* Big-endian */
1842 static unsigned int one = 1;
1843 if( 1==*(unsigned char*)&one ){
1844 /* Little endian. No byte swapping. */
1847 /* Big endian. Byte swap. */
1855 ** Make consecutive calls to the SHA3Update function to add new content
1858 static void SHA3Update(
1860 const unsigned char *aData,
1864 #if SHA3_BYTEORDER==1234
1865 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1866 for(; i+7<nData; i+=8){
1867 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1869 if( p->nLoaded>=p->nRate ){
1876 for(; i<nData; i++){
1877 #if SHA3_BYTEORDER==1234
1878 p->u.x[p->nLoaded] ^= aData[i];
1879 #elif SHA3_BYTEORDER==4321
1880 p->u.x[p->nLoaded^0x07] ^= aData[i];
1882 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1885 if( p->nLoaded==p->nRate ){
1893 ** After all content has been added, invoke SHA3Final() to compute
1894 ** the final hash. The function returns a pointer to the binary
1897 static unsigned char *SHA3Final(SHA3Context *p){
1899 if( p->nLoaded==p->nRate-1 ){
1900 const unsigned char c1 = 0x86;
1901 SHA3Update(p, &c1, 1);
1903 const unsigned char c2 = 0x06;
1904 const unsigned char c3 = 0x80;
1905 SHA3Update(p, &c2, 1);
1906 p->nLoaded = p->nRate - 1;
1907 SHA3Update(p, &c3, 1);
1909 for(i=0; i<p->nRate; i++){
1910 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1912 return &p->u.x[p->nRate];
1914 /* End of the hashing logic
1915 *****************************************************************************/
1918 ** Implementation of the sha3(X,SIZE) function.
1920 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1921 ** size is 256. If X is a BLOB, it is hashed as is.
1922 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1923 ** and the string is hashed without the trailing 0x00 terminator. The hash
1924 ** of a NULL value is NULL.
1926 static void sha3Func(
1927 sqlite3_context *context,
1929 sqlite3_value **argv
1932 int eType = sqlite3_value_type(argv[0]);
1933 int nByte = sqlite3_value_bytes(argv[0]);
1938 iSize = sqlite3_value_int(argv[1]);
1939 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1940 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1945 if( eType==SQLITE_NULL ) return;
1946 SHA3Init(&cx, iSize);
1947 if( eType==SQLITE_BLOB ){
1948 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1950 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1952 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1955 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1956 ** of 50 bytes and add it to the hash.
1958 static void hash_step_vformat(
1959 SHA3Context *p, /* Add content to this context */
1960 const char *zFormat,
1966 va_start(ap, zFormat);
1967 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1969 n = (int)strlen(zBuf);
1970 SHA3Update(p, (unsigned char*)zBuf, n);
1974 ** Implementation of the sha3_query(SQL,SIZE) function.
1976 ** This function compiles and runs the SQL statement(s) given in the
1977 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1980 ** The format of the byte stream that is hashed is summarized as follows:
1990 ** <sql> is the original SQL text for each statement run and <n> is
1991 ** the size of that text. The SQL text is UTF-8. A single R character
1992 ** occurs before the start of each row. N means a NULL value.
1993 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1994 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1995 ** B means blobs of <size> bytes. T means text rendered as <size>
1996 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1999 ** For each SQL statement in the X input, there is one S segment. Each
2000 ** S segment is followed by zero or more R segments, one for each row in the
2001 ** result set. After each R, there are one or more N, I, F, B, or T segments,
2002 ** one for each column in the result set. Segments are concatentated directly
2003 ** with no delimiters of any kind.
2005 static void sha3QueryFunc(
2006 sqlite3_context *context,
2008 sqlite3_value **argv
2010 sqlite3 *db = sqlite3_context_db_handle(context);
2011 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
2012 sqlite3_stmt *pStmt = 0;
2013 int nCol; /* Number of columns in the result set */
2014 int i; /* Loop counter */
2024 iSize = sqlite3_value_int(argv[1]);
2025 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
2026 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
2031 if( zSql==0 ) return;
2032 SHA3Init(&cx, iSize);
2034 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2036 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2037 zSql, sqlite3_errmsg(db));
2038 sqlite3_finalize(pStmt);
2039 sqlite3_result_error(context, zMsg, -1);
2043 if( !sqlite3_stmt_readonly(pStmt) ){
2044 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2045 sqlite3_finalize(pStmt);
2046 sqlite3_result_error(context, zMsg, -1);
2050 nCol = sqlite3_column_count(pStmt);
2051 z = sqlite3_sql(pStmt);
2054 hash_step_vformat(&cx,"S%d:",n);
2055 SHA3Update(&cx,(unsigned char*)z,n);
2058 /* Compute a hash over the result of the query */
2059 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2060 SHA3Update(&cx,(const unsigned char*)"R",1);
2061 for(i=0; i<nCol; i++){
2062 switch( sqlite3_column_type(pStmt,i) ){
2064 SHA3Update(&cx, (const unsigned char*)"N",1);
2067 case SQLITE_INTEGER: {
2071 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2073 for(j=8; j>=1; j--){
2078 SHA3Update(&cx, x, 9);
2081 case SQLITE_FLOAT: {
2085 double r = sqlite3_column_double(pStmt,i);
2087 for(j=8; j>=1; j--){
2092 SHA3Update(&cx,x,9);
2096 int n2 = sqlite3_column_bytes(pStmt, i);
2097 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2098 hash_step_vformat(&cx,"T%d:",n2);
2099 SHA3Update(&cx, z2, n2);
2103 int n2 = sqlite3_column_bytes(pStmt, i);
2104 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2105 hash_step_vformat(&cx,"B%d:",n2);
2106 SHA3Update(&cx, z2, n2);
2112 sqlite3_finalize(pStmt);
2114 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2121 int sqlite3_shathree_init(
2124 const sqlite3_api_routines *pApi
2127 SQLITE_EXTENSION_INIT2(pApi);
2128 (void)pzErrMsg; /* Unused parameter */
2129 rc = sqlite3_create_function(db, "sha3", 1,
2130 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2132 if( rc==SQLITE_OK ){
2133 rc = sqlite3_create_function(db, "sha3", 2,
2134 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2137 if( rc==SQLITE_OK ){
2138 rc = sqlite3_create_function(db, "sha3_query", 1,
2139 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2140 0, sha3QueryFunc, 0, 0);
2142 if( rc==SQLITE_OK ){
2143 rc = sqlite3_create_function(db, "sha3_query", 2,
2144 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2145 0, sha3QueryFunc, 0, 0);
2150 /************************* End ../ext/misc/shathree.c ********************/
2151 /************************* Begin ../ext/misc/fileio.c ******************/
2155 ** The author disclaims copyright to this source code. In place of
2156 ** a legal notice, here is a blessing:
2158 ** May you do good and not evil.
2159 ** May you find forgiveness for yourself and forgive others.
2160 ** May you share freely, never taking more than you give.
2162 ******************************************************************************
2164 ** This SQLite extension implements SQL functions readfile() and
2165 ** writefile(), and eponymous virtual type "fsdir".
2167 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2169 ** If neither of the optional arguments is present, then this UDF
2170 ** function writes blob DATA to file FILE. If successful, the number
2171 ** of bytes written is returned. If an error occurs, NULL is returned.
2173 ** If the first option argument - MODE - is present, then it must
2174 ** be passed an integer value that corresponds to a POSIX mode
2175 ** value (file type + permissions, as returned in the stat.st_mode
2176 ** field by the stat() system call). Three types of files may
2177 ** be written/created:
2179 ** regular files: (mode & 0170000)==0100000
2180 ** symbolic links: (mode & 0170000)==0120000
2181 ** directories: (mode & 0170000)==0040000
2183 ** For a directory, the DATA is ignored. For a symbolic link, it is
2184 ** interpreted as text and used as the target of the link. For a
2185 ** regular file, it is interpreted as a blob and written into the
2186 ** named file. Regardless of the type of file, its permissions are
2187 ** set to (mode & 0777) before returning.
2189 ** If the optional MTIME argument is present, then it is interpreted
2190 ** as an integer - the number of seconds since the unix epoch. The
2191 ** modification-time of the target file is set to this value before
2194 ** If three or more arguments are passed to this function and an
2195 ** error is encountered, an exception is raised.
2199 ** Read and return the contents of file FILE (type blob) from disk.
2205 ** SELECT * FROM fsdir($path [, $dir]);
2207 ** Parameter $path is an absolute or relative pathname. If the file that it
2208 ** refers to does not exist, it is an error. If the path refers to a regular
2209 ** file or symbolic link, it returns a single row. Or, if the path refers
2210 ** to a directory, it returns one row for the directory, and one row for each
2211 ** file within the hierarchy rooted at $path.
2213 ** Each row has the following columns:
2215 ** name: Path to file or directory (text value).
2216 ** mode: Value of stat.st_mode for directory entry (an integer).
2217 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2218 ** data: For a regular file, a blob containing the file data. For a
2219 ** symlink, a text value containing the text of the link. For a
2222 ** If a non-NULL value is specified for the optional $dir parameter and
2223 ** $path is a relative path, then $path is interpreted relative to $dir.
2224 ** And the paths returned in the "name" column of the table are also
2225 ** relative to directory $dir.
2227 ** Notes on building this extension for Windows:
2228 ** Unless linked statically with the SQLite library, a preprocessor
2229 ** symbol, FILEIO_WIN32_DLL, must be #define'd to create a stand-alone
2230 ** DLL form of this extension for WIN32. See its use below for details.
2232 /* #include "sqlite3ext.h" */
2233 SQLITE_EXTENSION_INIT1
2238 #include <sys/types.h>
2239 #include <sys/stat.h>
2241 #if !defined(_WIN32) && !defined(WIN32)
2242 # include <unistd.h>
2243 # include <dirent.h>
2245 # include <sys/time.h>
2247 # include "windows.h"
2249 # include <direct.h>
2250 /* # include "test_windirent.h" */
2251 # define dirent DIRENT
2253 # define chmod _chmod
2258 # define mkdir(path,mode) _mkdir(path)
2259 # define lstat(path,buf) stat(path,buf)
2266 ** Structure of the fsdir() table-valued function
2269 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2270 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2271 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2272 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2273 #define FSDIR_COLUMN_DATA 3 /* File content */
2274 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2275 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2279 ** Set the result stored by context ctx to a blob containing the
2280 ** contents of file zName. Or, leave the result unchanged (NULL)
2281 ** if the file does not exist or is unreadable.
2283 ** If the file exceeds the SQLite blob size limit, through an
2284 ** SQLITE_TOOBIG error.
2286 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2289 static void readFileContents(sqlite3_context *ctx, const char *zName){
2296 in = fopen(zName, "rb");
2298 /* File does not exist or is unreadable. Leave the result set to NULL. */
2301 fseek(in, 0, SEEK_END);
2304 db = sqlite3_context_db_handle(ctx);
2305 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2307 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2311 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2313 sqlite3_result_error_nomem(ctx);
2317 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2318 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2320 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2327 ** Implementation of the "readfile(X)" SQL function. The entire content
2328 ** of the file named X is read and returned as a BLOB. NULL is returned
2329 ** if the file does not exist or is unreadable.
2331 static void readfileFunc(
2332 sqlite3_context *context,
2334 sqlite3_value **argv
2337 (void)(argc); /* Unused parameter */
2338 zName = (const char*)sqlite3_value_text(argv[0]);
2339 if( zName==0 ) return;
2340 readFileContents(context, zName);
2344 ** Set the error message contained in context ctx to the results of
2345 ** vprintf(zFmt, ...).
2347 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2351 zMsg = sqlite3_vmprintf(zFmt, ap);
2352 sqlite3_result_error(ctx, zMsg, -1);
2359 ** This function is designed to convert a Win32 FILETIME structure into the
2360 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2362 static sqlite3_uint64 fileTimeToUnixTime(
2363 LPFILETIME pFileTime
2365 SYSTEMTIME epochSystemTime;
2366 ULARGE_INTEGER epochIntervals;
2367 FILETIME epochFileTime;
2368 ULARGE_INTEGER fileIntervals;
2370 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2371 epochSystemTime.wYear = 1970;
2372 epochSystemTime.wMonth = 1;
2373 epochSystemTime.wDay = 1;
2374 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2375 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2376 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2378 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2379 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2381 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2385 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
2386 # /* To allow a standalone DLL, use this next replacement function: */
2387 # undef sqlite3_win32_utf8_to_unicode
2388 # define sqlite3_win32_utf8_to_unicode utf8_to_utf16
2390 LPWSTR utf8_to_utf16(const char *z){
2391 int nAllot = MultiByteToWideChar(CP_UTF8, 0, z, -1, NULL, 0);
2392 LPWSTR rv = sqlite3_malloc(nAllot * sizeof(WCHAR));
2393 if( rv!=0 && 0 < MultiByteToWideChar(CP_UTF8, 0, z, -1, rv, nAllot) )
2401 ** This function attempts to normalize the time values found in the stat()
2402 ** buffer to UTC. This is necessary on Win32, where the runtime library
2403 ** appears to return these values as local times.
2405 static void statTimesToUtc(
2407 struct stat *pStatBuf
2410 WIN32_FIND_DATAW fd;
2411 LPWSTR zUnicodeName;
2412 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2413 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2415 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2416 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2417 if( hFindFile!=NULL ){
2418 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2419 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2420 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2421 FindClose(hFindFile);
2423 sqlite3_free(zUnicodeName);
2429 ** This function is used in place of stat(). On Windows, special handling
2430 ** is required in order for the included time to be returned as UTC. On all
2431 ** other systems, this function simply calls stat().
2433 static int fileStat(
2435 struct stat *pStatBuf
2438 int rc = stat(zPath, pStatBuf);
2439 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2442 return stat(zPath, pStatBuf);
2447 ** This function is used in place of lstat(). On Windows, special handling
2448 ** is required in order for the included time to be returned as UTC. On all
2449 ** other systems, this function simply calls lstat().
2451 static int fileLinkStat(
2453 struct stat *pStatBuf
2456 int rc = lstat(zPath, pStatBuf);
2457 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2460 return lstat(zPath, pStatBuf);
2465 ** Argument zFile is the name of a file that will be created and/or written
2466 ** by SQL function writefile(). This function ensures that the directory
2467 ** zFile will be written to exists, creating it if required. The permissions
2468 ** for any path components created by this function are set in accordance
2469 ** with the current umask.
2471 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2472 ** SQLITE_OK is returned if the directory is successfully created, or
2473 ** SQLITE_ERROR otherwise.
2475 static int makeDirectory(
2478 char *zCopy = sqlite3_mprintf("%s", zFile);
2484 int nCopy = (int)strlen(zCopy);
2487 while( rc==SQLITE_OK ){
2491 for(; zCopy[i]!='/' && i<nCopy; i++);
2492 if( i==nCopy ) break;
2495 rc2 = fileStat(zCopy, &sStat);
2497 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2499 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2505 sqlite3_free(zCopy);
2512 ** This function does the work for the writefile() UDF. Refer to
2513 ** header comments at the top of this file for details.
2515 static int writeFile(
2516 sqlite3_context *pCtx, /* Context to return bytes written in */
2517 const char *zFile, /* File to write */
2518 sqlite3_value *pData, /* Data to write */
2519 mode_t mode, /* MODE parameter passed to writefile() */
2520 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2522 #if !defined(_WIN32) && !defined(WIN32)
2523 if( S_ISLNK(mode) ){
2524 const char *zTo = (const char*)sqlite3_value_text(pData);
2525 if( symlink(zTo, zFile)<0 ) return 1;
2529 if( S_ISDIR(mode) ){
2530 if( mkdir(zFile, mode) ){
2531 /* The mkdir() call to create the directory failed. This might not
2532 ** be an error though - if there is already a directory at the same
2533 ** path and either the permissions already match or can be changed
2534 ** to do so using chmod(), it is not an error. */
2537 || 0!=fileStat(zFile, &sStat)
2538 || !S_ISDIR(sStat.st_mode)
2539 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2545 sqlite3_int64 nWrite = 0;
2548 FILE *out = fopen(zFile, "wb");
2549 if( out==0 ) return 1;
2550 z = (const char*)sqlite3_value_blob(pData);
2552 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2553 nWrite = sqlite3_value_bytes(pData);
2559 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2563 sqlite3_result_int64(pCtx, nWrite);
2569 #if !SQLITE_OS_WINRT
2571 FILETIME lastAccess;
2573 SYSTEMTIME currentTime;
2576 LPWSTR zUnicodeName;
2577 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2579 GetSystemTime(¤tTime);
2580 SystemTimeToFileTime(¤tTime, &lastAccess);
2581 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2582 lastWrite.dwLowDateTime = (DWORD)intervals;
2583 lastWrite.dwHighDateTime = intervals >> 32;
2584 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2585 if( zUnicodeName==0 ){
2588 hFile = CreateFileW(
2589 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2590 FILE_FLAG_BACKUP_SEMANTICS, NULL
2592 sqlite3_free(zUnicodeName);
2593 if( hFile!=INVALID_HANDLE_VALUE ){
2594 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2601 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2603 struct timespec times[2];
2604 times[0].tv_nsec = times[1].tv_nsec = 0;
2605 times[0].tv_sec = time(0);
2606 times[1].tv_sec = mtime;
2607 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2612 struct timeval times[2];
2613 times[0].tv_usec = times[1].tv_usec = 0;
2614 times[0].tv_sec = time(0);
2615 times[1].tv_sec = mtime;
2616 if( utimes(zFile, times) ){
2626 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2627 ** Refer to header comments at the top of this file for details.
2629 static void writefileFunc(
2630 sqlite3_context *context,
2632 sqlite3_value **argv
2637 sqlite3_int64 mtime = -1;
2639 if( argc<2 || argc>4 ){
2640 sqlite3_result_error(context,
2641 "wrong number of arguments to function writefile()", -1
2646 zFile = (const char*)sqlite3_value_text(argv[0]);
2647 if( zFile==0 ) return;
2649 mode = (mode_t)sqlite3_value_int(argv[2]);
2652 mtime = sqlite3_value_int64(argv[3]);
2655 res = writeFile(context, zFile, argv[1], mode, mtime);
2656 if( res==1 && errno==ENOENT ){
2657 if( makeDirectory(zFile)==SQLITE_OK ){
2658 res = writeFile(context, zFile, argv[1], mode, mtime);
2662 if( argc>2 && res!=0 ){
2663 if( S_ISLNK(mode) ){
2664 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2665 }else if( S_ISDIR(mode) ){
2666 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2668 ctxErrorMsg(context, "failed to write file: %s", zFile);
2674 ** SQL function: lsmode(MODE)
2676 ** Given a numberic st_mode from stat(), convert it into a human-readable
2677 ** text string in the style of "ls -l".
2679 static void lsModeFunc(
2680 sqlite3_context *context,
2682 sqlite3_value **argv
2685 int iMode = sqlite3_value_int(argv[0]);
2688 if( S_ISLNK(iMode) ){
2690 }else if( S_ISREG(iMode) ){
2692 }else if( S_ISDIR(iMode) ){
2698 int m = (iMode >> ((2-i)*3));
2699 char *a = &z[1 + i*3];
2700 a[0] = (m & 0x4) ? 'r' : '-';
2701 a[1] = (m & 0x2) ? 'w' : '-';
2702 a[2] = (m & 0x1) ? 'x' : '-';
2705 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2708 #ifndef SQLITE_OMIT_VIRTUALTABLE
2711 ** Cursor type for recursively iterating through a directory structure.
2713 typedef struct fsdir_cursor fsdir_cursor;
2714 typedef struct FsdirLevel FsdirLevel;
2717 DIR *pDir; /* From opendir() */
2718 char *zDir; /* Name of directory (nul-terminated) */
2721 struct fsdir_cursor {
2722 sqlite3_vtab_cursor base; /* Base class - must be first */
2724 int nLvl; /* Number of entries in aLvl[] array */
2725 int iLvl; /* Index of current entry */
2726 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2731 struct stat sStat; /* Current lstat() results */
2732 char *zPath; /* Path to current entry */
2733 sqlite3_int64 iRowid; /* Current rowid */
2736 typedef struct fsdir_tab fsdir_tab;
2738 sqlite3_vtab base; /* Base class - must be first */
2742 ** Construct a new fsdir virtual table object.
2744 static int fsdirConnect(
2747 int argc, const char *const*argv,
2748 sqlite3_vtab **ppVtab,
2751 fsdir_tab *pNew = 0;
2757 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2758 if( rc==SQLITE_OK ){
2759 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2760 if( pNew==0 ) return SQLITE_NOMEM;
2761 memset(pNew, 0, sizeof(*pNew));
2762 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2764 *ppVtab = (sqlite3_vtab*)pNew;
2769 ** This method is the destructor for fsdir vtab objects.
2771 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2772 sqlite3_free(pVtab);
2777 ** Constructor for a new fsdir_cursor object.
2779 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2782 pCur = sqlite3_malloc( sizeof(*pCur) );
2783 if( pCur==0 ) return SQLITE_NOMEM;
2784 memset(pCur, 0, sizeof(*pCur));
2786 *ppCursor = &pCur->base;
2791 ** Reset a cursor back to the state it was in when first returned
2794 static void fsdirResetCursor(fsdir_cursor *pCur){
2796 for(i=0; i<=pCur->iLvl; i++){
2797 FsdirLevel *pLvl = &pCur->aLvl[i];
2798 if( pLvl->pDir ) closedir(pLvl->pDir);
2799 sqlite3_free(pLvl->zDir);
2801 sqlite3_free(pCur->zPath);
2802 sqlite3_free(pCur->aLvl);
2813 ** Destructor for an fsdir_cursor.
2815 static int fsdirClose(sqlite3_vtab_cursor *cur){
2816 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2818 fsdirResetCursor(pCur);
2824 ** Set the error message for the virtual table associated with cursor
2825 ** pCur to the results of vprintf(zFmt, ...).
2827 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2830 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2836 ** Advance an fsdir_cursor to its next row of output.
2838 static int fsdirNext(sqlite3_vtab_cursor *cur){
2839 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2840 mode_t m = pCur->sStat.st_mode;
2844 /* Descend into this directory */
2845 int iNew = pCur->iLvl + 1;
2847 if( iNew>=pCur->nLvl ){
2849 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2850 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2851 if( aNew==0 ) return SQLITE_NOMEM;
2852 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2857 pLvl = &pCur->aLvl[iNew];
2859 pLvl->zDir = pCur->zPath;
2861 pLvl->pDir = opendir(pLvl->zDir);
2862 if( pLvl->pDir==0 ){
2863 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2864 return SQLITE_ERROR;
2868 while( pCur->iLvl>=0 ){
2869 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2870 struct dirent *pEntry = readdir(pLvl->pDir);
2872 if( pEntry->d_name[0]=='.' ){
2873 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2874 if( pEntry->d_name[1]=='\0' ) continue;
2876 sqlite3_free(pCur->zPath);
2877 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2878 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2879 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2880 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2881 return SQLITE_ERROR;
2885 closedir(pLvl->pDir);
2886 sqlite3_free(pLvl->zDir);
2893 sqlite3_free(pCur->zPath);
2899 ** Return values of columns for the row at which the series_cursor
2900 ** is currently pointing.
2902 static int fsdirColumn(
2903 sqlite3_vtab_cursor *cur, /* The cursor */
2904 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2905 int i /* Which column to return */
2907 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2909 case FSDIR_COLUMN_NAME: {
2910 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2914 case FSDIR_COLUMN_MODE:
2915 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2918 case FSDIR_COLUMN_MTIME:
2919 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2922 case FSDIR_COLUMN_DATA: {
2923 mode_t m = pCur->sStat.st_mode;
2925 sqlite3_result_null(ctx);
2926 #if !defined(_WIN32) && !defined(WIN32)
2927 }else if( S_ISLNK(m) ){
2929 char *aBuf = aStatic;
2930 sqlite3_int64 nBuf = 64;
2934 n = readlink(pCur->zPath, aBuf, nBuf);
2936 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2938 aBuf = sqlite3_malloc64(nBuf);
2940 sqlite3_result_error_nomem(ctx);
2941 return SQLITE_NOMEM;
2945 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2946 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2949 readFileContents(ctx, pCur->zPath);
2952 case FSDIR_COLUMN_PATH:
2954 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2955 ** always return their values as NULL */
2963 ** Return the rowid for the current row. In this implementation, the
2964 ** first row returned is assigned rowid value 1, and each subsequent
2965 ** row a value 1 more than that of the previous.
2967 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2968 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2969 *pRowid = pCur->iRowid;
2974 ** Return TRUE if the cursor has been moved off of the last
2977 static int fsdirEof(sqlite3_vtab_cursor *cur){
2978 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2979 return (pCur->zPath==0);
2983 ** xFilter callback.
2985 ** idxNum==1 PATH parameter only
2986 ** idxNum==2 Both PATH and DIR supplied
2988 static int fsdirFilter(
2989 sqlite3_vtab_cursor *cur,
2990 int idxNum, const char *idxStr,
2991 int argc, sqlite3_value **argv
2993 const char *zDir = 0;
2994 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2996 fsdirResetCursor(pCur);
2999 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
3000 return SQLITE_ERROR;
3003 assert( argc==idxNum && (argc==1 || argc==2) );
3004 zDir = (const char*)sqlite3_value_text(argv[0]);
3006 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
3007 return SQLITE_ERROR;
3010 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
3013 pCur->nBase = (int)strlen(pCur->zBase)+1;
3014 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
3016 pCur->zPath = sqlite3_mprintf("%s", zDir);
3019 if( pCur->zPath==0 ){
3020 return SQLITE_NOMEM;
3022 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
3023 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
3024 return SQLITE_ERROR;
3031 ** SQLite will invoke this method one or more times while planning a query
3032 ** that uses the generate_series virtual table. This routine needs to create
3033 ** a query plan for each invocation and compute an estimated cost for that
3036 ** In this implementation idxNum is used to represent the
3037 ** query plan. idxStr is unused.
3039 ** The query plan is represented by values of idxNum:
3041 ** (1) The path value is supplied by argv[0]
3042 ** (2) Path is in argv[0] and dir is in argv[1]
3044 static int fsdirBestIndex(
3046 sqlite3_index_info *pIdxInfo
3048 int i; /* Loop over constraints */
3049 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
3050 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
3051 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
3052 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
3053 const struct sqlite3_index_constraint *pConstraint;
3056 pConstraint = pIdxInfo->aConstraint;
3057 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3058 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3059 switch( pConstraint->iColumn ){
3060 case FSDIR_COLUMN_PATH: {
3061 if( pConstraint->usable ){
3064 }else if( idxPath<0 ){
3069 case FSDIR_COLUMN_DIR: {
3070 if( pConstraint->usable ){
3073 }else if( idxDir<0 ){
3080 if( seenPath || seenDir ){
3081 /* If input parameters are unusable, disallow this plan */
3082 return SQLITE_CONSTRAINT;
3086 pIdxInfo->idxNum = 0;
3087 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3088 ** number. Leave it unchanged. */
3089 pIdxInfo->estimatedRows = 0x7fffffff;
3091 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3092 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3094 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3095 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3096 pIdxInfo->idxNum = 2;
3097 pIdxInfo->estimatedCost = 10.0;
3099 pIdxInfo->idxNum = 1;
3100 pIdxInfo->estimatedCost = 100.0;
3108 ** Register the "fsdir" virtual table.
3110 static int fsdirRegister(sqlite3 *db){
3111 static sqlite3_module fsdirModule = {
3114 fsdirConnect, /* xConnect */
3115 fsdirBestIndex, /* xBestIndex */
3116 fsdirDisconnect, /* xDisconnect */
3118 fsdirOpen, /* xOpen - open a cursor */
3119 fsdirClose, /* xClose - close a cursor */
3120 fsdirFilter, /* xFilter - configure scan constraints */
3121 fsdirNext, /* xNext - advance a cursor */
3122 fsdirEof, /* xEof - check for end of scan */
3123 fsdirColumn, /* xColumn - read data */
3124 fsdirRowid, /* xRowid - read data */
3130 0, /* xFindMethod */
3134 0, /* xRollbackTo */
3135 0, /* xShadowName */
3138 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3141 #else /* SQLITE_OMIT_VIRTUALTABLE */
3142 # define fsdirRegister(x) SQLITE_OK
3148 int sqlite3_fileio_init(
3151 const sqlite3_api_routines *pApi
3154 SQLITE_EXTENSION_INIT2(pApi);
3155 (void)pzErrMsg; /* Unused parameter */
3156 rc = sqlite3_create_function(db, "readfile", 1,
3157 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3158 readfileFunc, 0, 0);
3159 if( rc==SQLITE_OK ){
3160 rc = sqlite3_create_function(db, "writefile", -1,
3161 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3162 writefileFunc, 0, 0);
3164 if( rc==SQLITE_OK ){
3165 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3168 if( rc==SQLITE_OK ){
3169 rc = fsdirRegister(db);
3174 #if defined(FILEIO_WIN32_DLL) && (defined(_WIN32) || defined(WIN32))
3175 /* To allow a standalone DLL, make test_windirent.c use the same
3176 * redefined SQLite API calls as the above extension code does.
3177 * Just pull in this .c to accomplish this. As a beneficial side
3178 * effect, this extension becomes a single translation unit. */
3179 # include "test_windirent.c"
3182 /************************* End ../ext/misc/fileio.c ********************/
3183 /************************* Begin ../ext/misc/completion.c ******************/
3187 ** The author disclaims copyright to this source code. In place of
3188 ** a legal notice, here is a blessing:
3190 ** May you do good and not evil.
3191 ** May you find forgiveness for yourself and forgive others.
3192 ** May you share freely, never taking more than you give.
3194 *************************************************************************
3196 ** This file implements an eponymous virtual table that returns suggested
3197 ** completions for a partial SQL input.
3201 ** SELECT DISTINCT candidate COLLATE nocase
3202 ** FROM completion($prefix,$wholeline)
3205 ** The two query parameters are optional. $prefix is the text of the
3206 ** current word being typed and that is to be completed. $wholeline is
3207 ** the complete input line, used for context.
3209 ** The raw completion() table might return the same candidate multiple
3210 ** times, for example if the same column name is used to two or more
3211 ** tables. And the candidates are returned in an arbitrary order. Hence,
3212 ** the DISTINCT and ORDER BY are recommended.
3214 ** This virtual table operates at the speed of human typing, and so there
3215 ** is no attempt to make it fast. Even a slow implementation will be much
3216 ** faster than any human can type.
3219 /* #include "sqlite3ext.h" */
3220 SQLITE_EXTENSION_INIT1
3225 #ifndef SQLITE_OMIT_VIRTUALTABLE
3227 /* completion_vtab is a subclass of sqlite3_vtab which will
3228 ** serve as the underlying representation of a completion virtual table
3230 typedef struct completion_vtab completion_vtab;
3231 struct completion_vtab {
3232 sqlite3_vtab base; /* Base class - must be first */
3233 sqlite3 *db; /* Database connection for this completion vtab */
3236 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3237 ** serve as the underlying representation of a cursor that scans
3238 ** over rows of the result
3240 typedef struct completion_cursor completion_cursor;
3241 struct completion_cursor {
3242 sqlite3_vtab_cursor base; /* Base class - must be first */
3243 sqlite3 *db; /* Database connection for this cursor */
3244 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3245 char *zPrefix; /* The prefix for the word we want to complete */
3246 char *zLine; /* The whole that we want to complete */
3247 const char *zCurrentRow; /* Current output row */
3248 int szRow; /* Length of the zCurrentRow string */
3249 sqlite3_stmt *pStmt; /* Current statement */
3250 sqlite3_int64 iRowid; /* The rowid */
3251 int ePhase; /* Current phase */
3252 int j; /* inter-phase counter */
3255 /* Values for ePhase:
3257 #define COMPLETION_FIRST_PHASE 1
3258 #define COMPLETION_KEYWORDS 1
3259 #define COMPLETION_PRAGMAS 2
3260 #define COMPLETION_FUNCTIONS 3
3261 #define COMPLETION_COLLATIONS 4
3262 #define COMPLETION_INDEXES 5
3263 #define COMPLETION_TRIGGERS 6
3264 #define COMPLETION_DATABASES 7
3265 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3266 #define COMPLETION_COLUMNS 9
3267 #define COMPLETION_MODULES 10
3268 #define COMPLETION_EOF 11
3271 ** The completionConnect() method is invoked to create a new
3272 ** completion_vtab that describes the completion virtual table.
3274 ** Think of this routine as the constructor for completion_vtab objects.
3276 ** All this routine needs to do is:
3278 ** (1) Allocate the completion_vtab object and initialize all fields.
3280 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3281 ** result set of queries against completion will look like.
3283 static int completionConnect(
3286 int argc, const char *const*argv,
3287 sqlite3_vtab **ppVtab,
3290 completion_vtab *pNew;
3293 (void)(pAux); /* Unused parameter */
3294 (void)(argc); /* Unused parameter */
3295 (void)(argv); /* Unused parameter */
3296 (void)(pzErr); /* Unused parameter */
3298 /* Column numbers */
3299 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3300 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3301 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3302 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3304 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3305 rc = sqlite3_declare_vtab(db,
3308 " prefix TEXT HIDDEN,"
3309 " wholeline TEXT HIDDEN,"
3310 " phase INT HIDDEN" /* Used for debugging only */
3312 if( rc==SQLITE_OK ){
3313 pNew = sqlite3_malloc( sizeof(*pNew) );
3314 *ppVtab = (sqlite3_vtab*)pNew;
3315 if( pNew==0 ) return SQLITE_NOMEM;
3316 memset(pNew, 0, sizeof(*pNew));
3323 ** This method is the destructor for completion_cursor objects.
3325 static int completionDisconnect(sqlite3_vtab *pVtab){
3326 sqlite3_free(pVtab);
3331 ** Constructor for a new completion_cursor object.
3333 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3334 completion_cursor *pCur;
3335 pCur = sqlite3_malloc( sizeof(*pCur) );
3336 if( pCur==0 ) return SQLITE_NOMEM;
3337 memset(pCur, 0, sizeof(*pCur));
3338 pCur->db = ((completion_vtab*)p)->db;
3339 *ppCursor = &pCur->base;
3344 ** Reset the completion_cursor.
3346 static void completionCursorReset(completion_cursor *pCur){
3347 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3348 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3349 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3354 ** Destructor for a completion_cursor.
3356 static int completionClose(sqlite3_vtab_cursor *cur){
3357 completionCursorReset((completion_cursor*)cur);
3363 ** Advance a completion_cursor to its next row of output.
3365 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3366 ** record the current state of the scan. This routine sets ->zCurrentRow
3367 ** to the current row of output and then returns. If no more rows remain,
3368 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3369 ** table that has reached the end of its scan.
3371 ** The current implementation just lists potential identifiers and
3372 ** keywords and filters them by zPrefix. Future enhancements should
3373 ** take zLine into account to try to restrict the set of identifiers and
3374 ** keywords based on what would be legal at the current point of input.
3376 static int completionNext(sqlite3_vtab_cursor *cur){
3377 completion_cursor *pCur = (completion_cursor*)cur;
3378 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3379 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3381 while( pCur->ePhase!=COMPLETION_EOF ){
3382 switch( pCur->ePhase ){
3383 case COMPLETION_KEYWORDS: {
3384 if( pCur->j >= sqlite3_keyword_count() ){
3385 pCur->zCurrentRow = 0;
3386 pCur->ePhase = COMPLETION_DATABASES;
3388 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3393 case COMPLETION_DATABASES: {
3394 if( pCur->pStmt==0 ){
3395 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3399 eNextPhase = COMPLETION_TABLES;
3402 case COMPLETION_TABLES: {
3403 if( pCur->pStmt==0 ){
3406 const char *zSep = "";
3407 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3408 while( sqlite3_step(pS2)==SQLITE_ROW ){
3409 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3410 zSql = sqlite3_mprintf(
3412 "SELECT name FROM \"%w\".sqlite_schema",
3415 if( zSql==0 ) return SQLITE_NOMEM;
3418 sqlite3_finalize(pS2);
3419 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3423 eNextPhase = COMPLETION_COLUMNS;
3426 case COMPLETION_COLUMNS: {
3427 if( pCur->pStmt==0 ){
3430 const char *zSep = "";
3431 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3432 while( sqlite3_step(pS2)==SQLITE_ROW ){
3433 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3434 zSql = sqlite3_mprintf(
3436 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3437 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3438 " WHERE sm.type='table'",
3439 zSql, zSep, zDb, zDb
3441 if( zSql==0 ) return SQLITE_NOMEM;
3444 sqlite3_finalize(pS2);
3445 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3449 eNextPhase = COMPLETION_EOF;
3454 /* This case is when the phase presets zCurrentRow */
3455 if( pCur->zCurrentRow==0 ) continue;
3457 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3458 /* Extract the next row of content */
3459 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3460 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3462 /* When all rows are finished, advance to the next phase */
3463 sqlite3_finalize(pCur->pStmt);
3465 pCur->ePhase = eNextPhase;
3469 if( pCur->nPrefix==0 ) break;
3470 if( pCur->nPrefix<=pCur->szRow
3471 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3481 ** Return values of columns for the row at which the completion_cursor
3482 ** is currently pointing.
3484 static int completionColumn(
3485 sqlite3_vtab_cursor *cur, /* The cursor */
3486 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3487 int i /* Which column to return */
3489 completion_cursor *pCur = (completion_cursor*)cur;
3491 case COMPLETION_COLUMN_CANDIDATE: {
3492 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3495 case COMPLETION_COLUMN_PREFIX: {
3496 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3499 case COMPLETION_COLUMN_WHOLELINE: {
3500 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3503 case COMPLETION_COLUMN_PHASE: {
3504 sqlite3_result_int(ctx, pCur->ePhase);
3512 ** Return the rowid for the current row. In this implementation, the
3513 ** rowid is the same as the output value.
3515 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3516 completion_cursor *pCur = (completion_cursor*)cur;
3517 *pRowid = pCur->iRowid;
3522 ** Return TRUE if the cursor has been moved off of the last
3525 static int completionEof(sqlite3_vtab_cursor *cur){
3526 completion_cursor *pCur = (completion_cursor*)cur;
3527 return pCur->ePhase >= COMPLETION_EOF;
3531 ** This method is called to "rewind" the completion_cursor object back
3532 ** to the first row of output. This method is always called at least
3533 ** once prior to any call to completionColumn() or completionRowid() or
3536 static int completionFilter(
3537 sqlite3_vtab_cursor *pVtabCursor,
3538 int idxNum, const char *idxStr,
3539 int argc, sqlite3_value **argv
3541 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3543 (void)(idxStr); /* Unused parameter */
3544 (void)(argc); /* Unused parameter */
3545 completionCursorReset(pCur);
3547 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3548 if( pCur->nPrefix>0 ){
3549 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3550 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3555 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3556 if( pCur->nLine>0 ){
3557 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3558 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3561 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3562 int i = pCur->nLine;
3563 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3566 pCur->nPrefix = pCur->nLine - i;
3567 if( pCur->nPrefix>0 ){
3568 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3569 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3573 pCur->ePhase = COMPLETION_FIRST_PHASE;
3574 return completionNext(pVtabCursor);
3578 ** SQLite will invoke this method one or more times while planning a query
3579 ** that uses the completion virtual table. This routine needs to create
3580 ** a query plan for each invocation and compute an estimated cost for that
3583 ** There are two hidden parameters that act as arguments to the table-valued
3584 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3585 ** is available and bit 1 is set if "wholeline" is available.
3587 static int completionBestIndex(
3589 sqlite3_index_info *pIdxInfo
3591 int i; /* Loop over constraints */
3592 int idxNum = 0; /* The query plan bitmask */
3593 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3594 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3595 int nArg = 0; /* Number of arguments that completeFilter() expects */
3596 const struct sqlite3_index_constraint *pConstraint;
3598 (void)(tab); /* Unused parameter */
3599 pConstraint = pIdxInfo->aConstraint;
3600 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3601 if( pConstraint->usable==0 ) continue;
3602 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3603 switch( pConstraint->iColumn ){
3604 case COMPLETION_COLUMN_PREFIX:
3608 case COMPLETION_COLUMN_WHOLELINE:
3615 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3616 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3618 if( wholelineIdx>=0 ){
3619 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3620 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3622 pIdxInfo->idxNum = idxNum;
3623 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3624 pIdxInfo->estimatedRows = 500 - 100*nArg;
3629 ** This following structure defines all the methods for the
3630 ** completion virtual table.
3632 static sqlite3_module completionModule = {
3635 completionConnect, /* xConnect */
3636 completionBestIndex, /* xBestIndex */
3637 completionDisconnect, /* xDisconnect */
3639 completionOpen, /* xOpen - open a cursor */
3640 completionClose, /* xClose - close a cursor */
3641 completionFilter, /* xFilter - configure scan constraints */
3642 completionNext, /* xNext - advance a cursor */
3643 completionEof, /* xEof - check for end of scan */
3644 completionColumn, /* xColumn - read data */
3645 completionRowid, /* xRowid - read data */
3651 0, /* xFindMethod */
3655 0, /* xRollbackTo */
3659 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3661 int sqlite3CompletionVtabInit(sqlite3 *db){
3663 #ifndef SQLITE_OMIT_VIRTUALTABLE
3664 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3672 int sqlite3_completion_init(
3675 const sqlite3_api_routines *pApi
3678 SQLITE_EXTENSION_INIT2(pApi);
3679 (void)(pzErrMsg); /* Unused parameter */
3680 #ifndef SQLITE_OMIT_VIRTUALTABLE
3681 rc = sqlite3CompletionVtabInit(db);
3686 /************************* End ../ext/misc/completion.c ********************/
3687 /************************* Begin ../ext/misc/appendvfs.c ******************/
3691 ** The author disclaims copyright to this source code. In place of
3692 ** a legal notice, here is a blessing:
3694 ** May you do good and not evil.
3695 ** May you find forgiveness for yourself and forgive others.
3696 ** May you share freely, never taking more than you give.
3698 ******************************************************************************
3700 ** This file implements a VFS shim that allows an SQLite database to be
3701 ** appended onto the end of some other file, such as an executable.
3703 ** A special record must appear at the end of the file that identifies the
3704 ** file as an appended database and provides the offset to the first page
3705 ** of the exposed content. (Or, it is the length of the content prefix.)
3706 ** For best performance page 1 should be located at a disk page boundary,
3707 ** though that is not required.
3709 ** When opening a database using this VFS, the connection might treat
3710 ** the file as an ordinary SQLite database, or it might treat it as a
3711 ** database appended onto some other file. The decision is made by
3712 ** applying the following rules in order:
3714 ** (1) An empty file is an ordinary database.
3716 ** (2) If the file ends with the appendvfs trailer string
3717 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3719 ** (3) If the file begins with the standard SQLite prefix string
3720 ** "SQLite format 3", that file is an ordinary database.
3722 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3723 ** set, then a new database is appended to the already existing file.
3725 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3727 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3728 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3729 ** This VFS will not read or write past the 1GiB mark. This restriction
3730 ** might be lifted in future versions. For now, if you need a larger
3731 ** database, then keep it in a separate file.
3733 ** If the file being opened is a plain database (not an appended one), then
3734 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3736 /* #include "sqlite3ext.h" */
3737 SQLITE_EXTENSION_INIT1
3741 /* The append mark at the end of the database is:
3743 ** Start-Of-SQLite3-NNNNNNNN
3744 ** 123456789 123456789 12345
3746 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3747 ** the offset to page 1, and also the length of the prefix content.
3749 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3750 #define APND_MARK_PREFIX_SZ 17
3751 #define APND_MARK_FOS_SZ 8
3752 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3755 ** Maximum size of the combined prefix + database + append-mark. This
3756 ** must be less than 0x40000000 to avoid locking issues on Windows.
3758 #define APND_MAX_SIZE (0x40000000)
3761 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3763 #ifndef APND_ROUNDUP
3764 #define APND_ROUNDUP 4096
3766 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
3767 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3770 ** Forward declaration of objects used by this utility
3772 typedef struct sqlite3_vfs ApndVfs;
3773 typedef struct ApndFile ApndFile;
3775 /* Access to a lower-level VFS that (might) implement dynamic loading,
3776 ** access to randomness, etc.
3778 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3779 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3781 /* An open appendvfs file
3783 ** An instance of this structure describes the appended database file.
3784 ** A separate sqlite3_file object is always appended. The appended
3785 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3786 ** the entire file, including the prefix, the database, and the
3789 ** The structure of an AppendVFS database is like this:
3791 ** +-------------+---------+----------+-------------+
3792 ** | prefix-file | padding | database | append-mark |
3793 ** +-------------+---------+----------+-------------+
3799 ** "prefix file" - file onto which the database has been appended.
3800 ** "padding" - zero or more bytes inserted so that "database"
3801 ** starts on an APND_ROUNDUP boundary
3802 ** "database" - The SQLite database file
3803 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3804 ** the offset from the start of prefix-file to the start
3807 ** The size of the database is iMark - iPgOne.
3809 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3810 ** of iPgOne stored as a big-ending 64-bit integer.
3812 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3813 ** Or, iMark is -1 to indicate that it has not yet been written.
3816 sqlite3_file base; /* Subclass. MUST BE FIRST! */
3817 sqlite3_int64 iPgOne; /* Offset to the start of the database */
3818 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
3819 /* Always followed by another sqlite3_file that describes the whole file */
3823 ** Methods for ApndFile
3825 static int apndClose(sqlite3_file*);
3826 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3827 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3828 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3829 static int apndSync(sqlite3_file*, int flags);
3830 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3831 static int apndLock(sqlite3_file*, int);
3832 static int apndUnlock(sqlite3_file*, int);
3833 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3834 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3835 static int apndSectorSize(sqlite3_file*);
3836 static int apndDeviceCharacteristics(sqlite3_file*);
3837 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3838 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3839 static void apndShmBarrier(sqlite3_file*);
3840 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3841 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3842 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3845 ** Methods for ApndVfs
3847 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3848 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3849 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3850 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3851 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3852 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3853 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3854 static void apndDlClose(sqlite3_vfs*, void*);
3855 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3856 static int apndSleep(sqlite3_vfs*, int microseconds);
3857 static int apndCurrentTime(sqlite3_vfs*, double*);
3858 static int apndGetLastError(sqlite3_vfs*, int, char *);
3859 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3860 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3861 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3862 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3864 static sqlite3_vfs apnd_vfs = {
3865 3, /* iVersion (set when registered) */
3866 0, /* szOsFile (set when registered) */
3867 1024, /* mxPathname */
3869 "apndvfs", /* zName */
3870 0, /* pAppData (set when registered) */
3871 apndOpen, /* xOpen */
3872 apndDelete, /* xDelete */
3873 apndAccess, /* xAccess */
3874 apndFullPathname, /* xFullPathname */
3875 apndDlOpen, /* xDlOpen */
3876 apndDlError, /* xDlError */
3877 apndDlSym, /* xDlSym */
3878 apndDlClose, /* xDlClose */
3879 apndRandomness, /* xRandomness */
3880 apndSleep, /* xSleep */
3881 apndCurrentTime, /* xCurrentTime */
3882 apndGetLastError, /* xGetLastError */
3883 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3884 apndSetSystemCall, /* xSetSystemCall */
3885 apndGetSystemCall, /* xGetSystemCall */
3886 apndNextSystemCall /* xNextSystemCall */
3889 static const sqlite3_io_methods apnd_io_methods = {
3891 apndClose, /* xClose */
3892 apndRead, /* xRead */
3893 apndWrite, /* xWrite */
3894 apndTruncate, /* xTruncate */
3895 apndSync, /* xSync */
3896 apndFileSize, /* xFileSize */
3897 apndLock, /* xLock */
3898 apndUnlock, /* xUnlock */
3899 apndCheckReservedLock, /* xCheckReservedLock */
3900 apndFileControl, /* xFileControl */
3901 apndSectorSize, /* xSectorSize */
3902 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3903 apndShmMap, /* xShmMap */
3904 apndShmLock, /* xShmLock */
3905 apndShmBarrier, /* xShmBarrier */
3906 apndShmUnmap, /* xShmUnmap */
3907 apndFetch, /* xFetch */
3908 apndUnfetch /* xUnfetch */
3912 ** Close an apnd-file.
3914 static int apndClose(sqlite3_file *pFile){
3915 pFile = ORIGFILE(pFile);
3916 return pFile->pMethods->xClose(pFile);
3920 ** Read data from an apnd-file.
3922 static int apndRead(
3923 sqlite3_file *pFile,
3928 ApndFile *paf = (ApndFile *)pFile;
3929 pFile = ORIGFILE(pFile);
3930 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3934 ** Add the append-mark onto what should become the end of the file.
3935 * If and only if this succeeds, internal ApndFile.iMark is updated.
3936 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3938 static int apndWriteMark(
3940 sqlite3_file *pFile,
3941 sqlite_int64 iWriteEnd
3943 sqlite_int64 iPgOne = paf->iPgOne;
3944 unsigned char a[APND_MARK_SIZE];
3945 int i = APND_MARK_FOS_SZ;
3947 assert(pFile == ORIGFILE(paf));
3948 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3950 a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3953 iWriteEnd += paf->iPgOne;
3954 if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3955 (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3956 paf->iMark = iWriteEnd;
3962 ** Write data to an apnd-file.
3964 static int apndWrite(
3965 sqlite3_file *pFile,
3970 ApndFile *paf = (ApndFile *)pFile;
3971 sqlite_int64 iWriteEnd = iOfst + iAmt;
3972 if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3973 pFile = ORIGFILE(pFile);
3974 /* If append-mark is absent or will be overwritten, write it. */
3975 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3976 int rc = apndWriteMark(paf, pFile, iWriteEnd);
3977 if( SQLITE_OK!=rc ) return rc;
3979 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3983 ** Truncate an apnd-file.
3985 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3986 ApndFile *paf = (ApndFile *)pFile;
3987 pFile = ORIGFILE(pFile);
3988 /* The append mark goes out first so truncate failure does not lose it. */
3989 if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3990 /* Truncate underlying file just past append mark */
3991 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3995 ** Sync an apnd-file.
3997 static int apndSync(sqlite3_file *pFile, int flags){
3998 pFile = ORIGFILE(pFile);
3999 return pFile->pMethods->xSync(pFile, flags);
4003 ** Return the current file-size of an apnd-file.
4004 ** If the append mark is not yet there, the file-size is 0.
4006 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
4007 ApndFile *paf = (ApndFile *)pFile;
4008 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
4013 ** Lock an apnd-file.
4015 static int apndLock(sqlite3_file *pFile, int eLock){
4016 pFile = ORIGFILE(pFile);
4017 return pFile->pMethods->xLock(pFile, eLock);
4021 ** Unlock an apnd-file.
4023 static int apndUnlock(sqlite3_file *pFile, int eLock){
4024 pFile = ORIGFILE(pFile);
4025 return pFile->pMethods->xUnlock(pFile, eLock);
4029 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
4031 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
4032 pFile = ORIGFILE(pFile);
4033 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
4037 ** File control method. For custom operations on an apnd-file.
4039 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
4040 ApndFile *paf = (ApndFile *)pFile;
4042 pFile = ORIGFILE(pFile);
4043 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
4044 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
4045 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
4046 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
4052 ** Return the sector-size in bytes for an apnd-file.
4054 static int apndSectorSize(sqlite3_file *pFile){
4055 pFile = ORIGFILE(pFile);
4056 return pFile->pMethods->xSectorSize(pFile);
4060 ** Return the device characteristic flags supported by an apnd-file.
4062 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4063 pFile = ORIGFILE(pFile);
4064 return pFile->pMethods->xDeviceCharacteristics(pFile);
4067 /* Create a shared memory file mapping */
4068 static int apndShmMap(
4069 sqlite3_file *pFile,
4075 pFile = ORIGFILE(pFile);
4076 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4079 /* Perform locking on a shared-memory segment */
4080 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4081 pFile = ORIGFILE(pFile);
4082 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4085 /* Memory barrier operation on shared memory */
4086 static void apndShmBarrier(sqlite3_file *pFile){
4087 pFile = ORIGFILE(pFile);
4088 pFile->pMethods->xShmBarrier(pFile);
4091 /* Unmap a shared memory segment */
4092 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4093 pFile = ORIGFILE(pFile);
4094 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4097 /* Fetch a page of a memory-mapped file */
4098 static int apndFetch(
4099 sqlite3_file *pFile,
4100 sqlite3_int64 iOfst,
4104 ApndFile *p = (ApndFile *)pFile;
4105 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4106 return SQLITE_IOERR; /* Cannot read what is not yet there. */
4108 pFile = ORIGFILE(pFile);
4109 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4112 /* Release a memory-mapped page */
4113 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4114 ApndFile *p = (ApndFile *)pFile;
4115 pFile = ORIGFILE(pFile);
4116 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4120 ** Try to read the append-mark off the end of a file. Return the
4121 ** start of the appended database if the append-mark is present.
4122 ** If there is no valid append-mark, return -1;
4124 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4125 ** indicates that the appended database contains at least one page. The
4126 ** start-of-database value must be a multiple of 512.
4128 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4130 sqlite3_int64 iMark;
4131 int msbs = 8 * (APND_MARK_FOS_SZ-1);
4132 unsigned char a[APND_MARK_SIZE];
4134 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4135 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4137 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4138 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4141 iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4143 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4144 if( iMark & 0x1ff ) return -1;
4148 static const char apvfsSqliteHdr[] = "SQLite format 3";
4150 ** Check to see if the file is an appendvfs SQLite database file.
4151 ** Return true iff it is such. Parameter sz is the file's size.
4153 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4156 sqlite3_int64 iMark = apndReadMark(sz, pFile);
4158 /* If file has the correct end-marker, the expected odd size, and the
4159 ** SQLite DB type marker where the end-marker puts it, then it
4160 ** is an appendvfs database.
4162 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4164 && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4165 && (sz & 0x1ff) == APND_MARK_SIZE
4166 && sz>=512+APND_MARK_SIZE
4168 return 1; /* It's an appendvfs database */
4175 ** Check to see if the file is an ordinary SQLite database file.
4176 ** Return true iff so. Parameter sz is the file's size.
4178 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4180 if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4181 || (sz & 0x1ff) != 0
4182 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4183 || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4192 ** Open an apnd file handle.
4194 static int apndOpen(
4195 sqlite3_vfs *pApndVfs,
4197 sqlite3_file *pFile,
4201 ApndFile *pApndFile = (ApndFile*)pFile;
4202 sqlite3_file *pBaseFile = ORIGFILE(pFile);
4203 sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4205 sqlite3_int64 sz = 0;
4206 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4207 /* The appendvfs is not to be used for transient or temporary databases.
4208 ** Just use the base VFS open to initialize the given file object and
4209 ** open the underlying file. (Appendvfs is then unused for this file.)
4211 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4213 memset(pApndFile, 0, sizeof(ApndFile));
4214 pFile->pMethods = &apnd_io_methods;
4215 pApndFile->iMark = -1; /* Append mark not yet written */
4217 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4218 if( rc==SQLITE_OK ){
4219 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4221 pBaseFile->pMethods->xClose(pBaseFile);
4225 pFile->pMethods = 0;
4228 if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4229 /* The file being opened appears to be just an ordinary DB. Copy
4230 ** the base dispatch-table so this instance mimics the base VFS.
4232 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4235 pApndFile->iPgOne = apndReadMark(sz, pFile);
4236 if( pApndFile->iPgOne>=0 ){
4237 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4240 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4241 pBaseFile->pMethods->xClose(pBaseFile);
4242 rc = SQLITE_CANTOPEN;
4243 pFile->pMethods = 0;
4245 /* Round newly added appendvfs location to #define'd page boundary.
4246 ** Note that nothing has yet been written to the underlying file.
4247 ** The append mark will be written along with first content write.
4248 ** Until then, paf->iMark value indicates it is not yet written.
4250 pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4256 ** Delete an apnd file.
4257 ** For an appendvfs, this could mean delete the appendvfs portion,
4258 ** leaving the appendee as it was before it gained an appendvfs.
4259 ** For now, this code deletes the underlying file too.
4261 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4262 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4266 ** All other VFS methods are pass-thrus.
4268 static int apndAccess(
4274 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4276 static int apndFullPathname(
4282 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4284 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4285 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4287 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4288 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4290 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4291 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4293 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4294 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4296 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4297 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4299 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4300 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4302 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4303 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4305 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4306 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4308 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4309 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4311 static int apndSetSystemCall(
4314 sqlite3_syscall_ptr pCall
4316 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4318 static sqlite3_syscall_ptr apndGetSystemCall(
4322 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4324 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4325 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4333 ** This routine is called when the extension is loaded.
4334 ** Register the new VFS.
4336 int sqlite3_appendvfs_init(
4339 const sqlite3_api_routines *pApi
4343 SQLITE_EXTENSION_INIT2(pApi);
4346 pOrig = sqlite3_vfs_find(0);
4347 if( pOrig==0 ) return SQLITE_ERROR;
4348 apnd_vfs.iVersion = pOrig->iVersion;
4349 apnd_vfs.pAppData = pOrig;
4350 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4351 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4352 #ifdef APPENDVFS_TEST
4353 if( rc==SQLITE_OK ){
4354 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4357 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4361 /************************* End ../ext/misc/appendvfs.c ********************/
4362 /************************* Begin ../ext/misc/memtrace.c ******************/
4366 ** The author disclaims copyright to this source code. In place of
4367 ** a legal notice, here is a blessing:
4369 ** May you do good and not evil.
4370 ** May you find forgiveness for yourself and forgive others.
4371 ** May you share freely, never taking more than you give.
4373 *************************************************************************
4375 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4376 ** mechanism to add a tracing layer on top of SQLite. If this extension
4377 ** is registered prior to sqlite3_initialize(), it will cause all memory
4378 ** allocation activities to be logged on standard output, or to some other
4379 ** FILE specified by the initializer.
4381 ** This file needs to be compiled into the application that uses it.
4383 ** This extension is used to implement the --memtrace option of the
4384 ** command-line shell.
4390 /* The original memory allocation routines */
4391 static sqlite3_mem_methods memtraceBase;
4392 static FILE *memtraceOut;
4394 /* Methods that trace memory allocations */
4395 static void *memtraceMalloc(int n){
4397 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4398 memtraceBase.xRoundup(n));
4400 return memtraceBase.xMalloc(n);
4402 static void memtraceFree(void *p){
4405 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4407 memtraceBase.xFree(p);
4409 static void *memtraceRealloc(void *p, int n){
4410 if( p==0 ) return memtraceMalloc(n);
4416 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4417 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4419 return memtraceBase.xRealloc(p, n);
4421 static int memtraceSize(void *p){
4422 return memtraceBase.xSize(p);
4424 static int memtraceRoundup(int n){
4425 return memtraceBase.xRoundup(n);
4427 static int memtraceInit(void *p){
4428 return memtraceBase.xInit(p);
4430 static void memtraceShutdown(void *p){
4431 memtraceBase.xShutdown(p);
4434 /* The substitute memory allocator */
4435 static sqlite3_mem_methods ersaztMethods = {
4446 /* Begin tracing memory allocations to out. */
4447 int sqlite3MemTraceActivate(FILE *out){
4449 if( memtraceBase.xMalloc==0 ){
4450 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4451 if( rc==SQLITE_OK ){
4452 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4459 /* Deactivate memory tracing */
4460 int sqlite3MemTraceDeactivate(void){
4462 if( memtraceBase.xMalloc!=0 ){
4463 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4464 if( rc==SQLITE_OK ){
4465 memset(&memtraceBase, 0, sizeof(memtraceBase));
4472 /************************* End ../ext/misc/memtrace.c ********************/
4473 /************************* Begin ../ext/misc/uint.c ******************/
4477 ** The author disclaims copyright to this source code. In place of
4478 ** a legal notice, here is a blessing:
4480 ** May you do good and not evil.
4481 ** May you find forgiveness for yourself and forgive others.
4482 ** May you share freely, never taking more than you give.
4484 ******************************************************************************
4486 ** This SQLite extension implements the UINT collating sequence.
4488 ** UINT works like BINARY for text, except that embedded strings
4489 ** of digits compare in numeric order.
4491 ** * Leading zeros are handled properly, in the sense that
4492 ** they do not mess of the maginitude comparison of embedded
4493 ** strings of digits. "x00123y" is equal to "x123y".
4495 ** * Only unsigned integers are recognized. Plus and minus
4496 ** signs are ignored. Decimal points and exponential notation
4499 ** * Embedded integers can be of arbitrary length. Comparison
4500 ** is *not* limited integers that can be expressed as a
4501 ** 64-bit machine integer.
4503 /* #include "sqlite3ext.h" */
4504 SQLITE_EXTENSION_INIT1
4510 ** Compare text in lexicographic order, except strings of digits
4511 ** compare in numeric order.
4513 static int uintCollFunc(
4515 int nKey1, const void *pKey1,
4516 int nKey2, const void *pKey2
4518 const unsigned char *zA = (const unsigned char*)pKey1;
4519 const unsigned char *zB = (const unsigned char*)pKey2;
4522 while( i<nKey1 && j<nKey2 ){
4524 if( isdigit(zA[i]) ){
4526 if( !isdigit(zB[j]) ) return x;
4527 while( i<nKey1 && zA[i]=='0' ){ i++; }
4528 while( j<nKey2 && zB[j]=='0' ){ j++; }
4530 while( i+k<nKey1 && isdigit(zA[i+k])
4531 && j+k<nKey2 && isdigit(zB[j+k]) ){
4534 if( i+k<nKey1 && isdigit(zA[i+k]) ){
4536 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4539 x = memcmp(zA+i, zB+j, k);
4551 return (nKey1 - i) - (nKey2 - j);
4557 int sqlite3_uint_init(
4560 const sqlite3_api_routines *pApi
4562 SQLITE_EXTENSION_INIT2(pApi);
4563 (void)pzErrMsg; /* Unused parameter */
4564 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4567 /************************* End ../ext/misc/uint.c ********************/
4568 /************************* Begin ../ext/misc/decimal.c ******************/
4572 ** The author disclaims copyright to this source code. In place of
4573 ** a legal notice, here is a blessing:
4575 ** May you do good and not evil.
4576 ** May you find forgiveness for yourself and forgive others.
4577 ** May you share freely, never taking more than you give.
4579 ******************************************************************************
4581 ** Routines to implement arbitrary-precision decimal math.
4583 ** The focus here is on simplicity and correctness, not performance.
4585 /* #include "sqlite3ext.h" */
4586 SQLITE_EXTENSION_INIT1
4592 /* Mark a function parameter as unused, to suppress nuisance compiler
4594 #ifndef UNUSED_PARAMETER
4595 # define UNUSED_PARAMETER(X) (void)(X)
4599 /* A decimal object */
4600 typedef struct Decimal Decimal;
4602 char sign; /* 0 for positive, 1 for negative */
4603 char oom; /* True if an OOM is encountered */
4604 char isNull; /* True if holds a NULL rather than a number */
4605 char isInit; /* True upon initialization */
4606 int nDigit; /* Total number of digits */
4607 int nFrac; /* Number of digits to the right of the decimal point */
4608 signed char *a; /* Array of digits. Most significant first. */
4612 ** Release memory held by a Decimal, but do not free the object itself.
4614 static void decimal_clear(Decimal *p){
4619 ** Destroy a Decimal object
4621 static void decimal_free(Decimal *p){
4629 ** Allocate a new Decimal object. Initialize it to the number given
4630 ** by the input string.
4632 static Decimal *decimal_new(
4633 sqlite3_context *pCtx,
4636 const unsigned char *zAlt
4640 const unsigned char *zIn;
4642 p = sqlite3_malloc( sizeof(*p) );
4643 if( p==0 ) goto new_no_mem;
4654 if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4659 n = sqlite3_value_bytes(pIn);
4660 zIn = sqlite3_value_text(pIn);
4662 p->a = sqlite3_malloc64( n+1 );
4663 if( p->a==0 ) goto new_no_mem;
4664 for(i=0; isspace(zIn[i]); i++){}
4668 }else if( zIn[i]=='+' ){
4671 while( i<n && zIn[i]=='0' ) i++;
4674 if( c>='0' && c<='9' ){
4675 p->a[p->nDigit++] = c - '0';
4677 p->nFrac = p->nDigit + 1;
4678 }else if( c=='e' || c=='E' ){
4685 }else if( zIn[j]=='+' ){
4688 while( j<n && iExp<1000000 ){
4689 if( zIn[j]>='0' && zIn[j]<='9' ){
4690 iExp = iExp*10 + zIn[j] - '0';
4694 if( neg ) iExp = -iExp;
4700 p->nFrac = p->nDigit - (p->nFrac - 1);
4704 if( iExp<=p->nFrac ){
4713 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4714 if( p->a==0 ) goto new_no_mem;
4715 memset(p->a+p->nDigit, 0, iExp);
4721 nExtra = p->nDigit - p->nFrac - 1;
4728 p->nFrac = p->nDigit - 1;
4732 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4733 if( p->a==0 ) goto new_no_mem;
4734 memmove(p->a+iExp, p->a, p->nDigit);
4735 memset(p->a, 0, iExp);
4743 if( pCtx ) sqlite3_result_error_nomem(pCtx);
4749 ** Make the given Decimal the result.
4751 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4755 if( p==0 || p->oom ){
4756 sqlite3_result_error_nomem(pCtx);
4760 sqlite3_result_null(pCtx);
4763 z = sqlite3_malloc( p->nDigit+4 );
4765 sqlite3_result_error_nomem(pCtx);
4769 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4776 n = p->nDigit - p->nFrac;
4781 while( n>1 && p->a[j]==0 ){
4786 z[i++] = p->a[j] + '0';
4793 z[i++] = p->a[j] + '0';
4795 }while( j<p->nDigit );
4798 sqlite3_result_text(pCtx, z, i, sqlite3_free);
4802 ** SQL Function: decimal(X)
4804 ** Convert input X into decimal and then back into text
4806 static void decimalFunc(
4807 sqlite3_context *context,
4809 sqlite3_value **argv
4811 Decimal *p = decimal_new(context, argv[0], 0, 0);
4812 UNUSED_PARAMETER(argc);
4813 decimal_result(context, p);
4818 ** Compare to Decimal objects. Return negative, 0, or positive if the
4819 ** first object is less than, equal to, or greater than the second.
4821 ** Preconditions for this routine:
4828 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4829 int nASig, nBSig, rc, n;
4830 if( pA->sign!=pB->sign ){
4831 return pA->sign ? -1 : +1;
4834 const Decimal *pTemp = pA;
4838 nASig = pA->nDigit - pA->nFrac;
4839 nBSig = pB->nDigit - pB->nFrac;
4841 return nASig - nBSig;
4844 if( n>pB->nDigit ) n = pB->nDigit;
4845 rc = memcmp(pA->a, pB->a, n);
4847 rc = pA->nDigit - pB->nDigit;
4853 ** SQL Function: decimal_cmp(X, Y)
4855 ** Return negative, zero, or positive if X is less then, equal to, or
4858 static void decimalCmpFunc(
4859 sqlite3_context *context,
4861 sqlite3_value **argv
4863 Decimal *pA = 0, *pB = 0;
4866 UNUSED_PARAMETER(argc);
4867 pA = decimal_new(context, argv[0], 0, 0);
4868 if( pA==0 || pA->isNull ) goto cmp_done;
4869 pB = decimal_new(context, argv[1], 0, 0);
4870 if( pB==0 || pB->isNull ) goto cmp_done;
4871 rc = decimal_cmp(pA, pB);
4873 else if( rc>0 ) rc = +1;
4874 sqlite3_result_int(context, rc);
4881 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4882 ** digits to the right of the decimal point.
4884 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4888 nAddFrac = nFrac - p->nFrac;
4889 nAddSig = (nDigit - p->nDigit) - nAddFrac;
4890 if( nAddFrac==0 && nAddSig==0 ) return;
4891 p->a = sqlite3_realloc64(p->a, nDigit+1);
4897 memmove(p->a+nAddSig, p->a, p->nDigit);
4898 memset(p->a, 0, nAddSig);
4899 p->nDigit += nAddSig;
4902 memset(p->a+p->nDigit, 0, nAddFrac);
4903 p->nDigit += nAddFrac;
4904 p->nFrac += nAddFrac;
4909 ** Add the value pB into pA.
4911 ** Both pA and pB might become denormalized by this routine.
4913 static void decimal_add(Decimal *pA, Decimal *pB){
4914 int nSig, nFrac, nDigit;
4919 if( pA->oom || pB==0 || pB->oom ){
4923 if( pA->isNull || pB->isNull ){
4927 nSig = pA->nDigit - pA->nFrac;
4928 if( nSig && pA->a[0]==0 ) nSig--;
4929 if( nSig<pB->nDigit-pB->nFrac ){
4930 nSig = pB->nDigit - pB->nFrac;
4933 if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4934 nDigit = nSig + nFrac + 1;
4935 decimal_expand(pA, nDigit, nFrac);
4936 decimal_expand(pB, nDigit, nFrac);
4937 if( pA->oom || pB->oom ){
4940 if( pA->sign==pB->sign ){
4942 for(i=nDigit-1; i>=0; i--){
4943 int x = pA->a[i] + pB->a[i] + carry;
4953 signed char *aA, *aB;
4955 rc = memcmp(pA->a, pB->a, nDigit);
4959 pA->sign = !pA->sign;
4964 for(i=nDigit-1; i>=0; i--){
4965 int x = aA[i] - aB[i] - borrow;
4979 ** Compare text in decimal order.
4981 static int decimalCollFunc(
4983 int nKey1, const void *pKey1,
4984 int nKey2, const void *pKey2
4986 const unsigned char *zA = (const unsigned char*)pKey1;
4987 const unsigned char *zB = (const unsigned char*)pKey2;
4988 Decimal *pA = decimal_new(0, 0, nKey1, zA);
4989 Decimal *pB = decimal_new(0, 0, nKey2, zB);
4991 UNUSED_PARAMETER(notUsed);
4992 if( pA==0 || pB==0 ){
4995 rc = decimal_cmp(pA, pB);
5004 ** SQL Function: decimal_add(X, Y)
5005 ** decimal_sub(X, Y)
5007 ** Return the sum or difference of X and Y.
5009 static void decimalAddFunc(
5010 sqlite3_context *context,
5012 sqlite3_value **argv
5014 Decimal *pA = decimal_new(context, argv[0], 0, 0);
5015 Decimal *pB = decimal_new(context, argv[1], 0, 0);
5016 UNUSED_PARAMETER(argc);
5017 decimal_add(pA, pB);
5018 decimal_result(context, pA);
5022 static void decimalSubFunc(
5023 sqlite3_context *context,
5025 sqlite3_value **argv
5027 Decimal *pA = decimal_new(context, argv[0], 0, 0);
5028 Decimal *pB = decimal_new(context, argv[1], 0, 0);
5029 UNUSED_PARAMETER(argc);
5031 pB->sign = !pB->sign;
5032 decimal_add(pA, pB);
5033 decimal_result(context, pA);
5039 /* Aggregate funcion: decimal_sum(X)
5041 ** Works like sum() except that it uses decimal arithmetic for unlimited
5044 static void decimalSumStep(
5045 sqlite3_context *context,
5047 sqlite3_value **argv
5051 UNUSED_PARAMETER(argc);
5052 p = sqlite3_aggregate_context(context, sizeof(*p));
5056 p->a = sqlite3_malloc(2);
5065 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5066 pArg = decimal_new(context, argv[0], 0, 0);
5067 decimal_add(p, pArg);
5070 static void decimalSumInverse(
5071 sqlite3_context *context,
5073 sqlite3_value **argv
5077 UNUSED_PARAMETER(argc);
5078 p = sqlite3_aggregate_context(context, sizeof(*p));
5080 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5081 pArg = decimal_new(context, argv[0], 0, 0);
5082 if( pArg ) pArg->sign = !pArg->sign;
5083 decimal_add(p, pArg);
5086 static void decimalSumValue(sqlite3_context *context){
5087 Decimal *p = sqlite3_aggregate_context(context, 0);
5089 decimal_result(context, p);
5091 static void decimalSumFinalize(sqlite3_context *context){
5092 Decimal *p = sqlite3_aggregate_context(context, 0);
5094 decimal_result(context, p);
5099 ** SQL Function: decimal_mul(X, Y)
5101 ** Return the product of X and Y.
5103 ** All significant digits after the decimal point are retained.
5104 ** Trailing zeros after the decimal point are omitted as long as
5105 ** the number of digits after the decimal point is no less than
5106 ** either the number of digits in either input.
5108 static void decimalMulFunc(
5109 sqlite3_context *context,
5111 sqlite3_value **argv
5113 Decimal *pA = decimal_new(context, argv[0], 0, 0);
5114 Decimal *pB = decimal_new(context, argv[1], 0, 0);
5115 signed char *acc = 0;
5118 UNUSED_PARAMETER(argc);
5119 if( pA==0 || pA->oom || pA->isNull
5120 || pB==0 || pB->oom || pB->isNull
5124 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5126 sqlite3_result_error_nomem(context);
5129 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5130 minFrac = pA->nFrac;
5131 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5132 for(i=pA->nDigit-1; i>=0; i--){
5133 signed char f = pA->a[i];
5135 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5136 x = acc[k] + f*pB->a[j] + carry;
5144 sqlite3_free(pA->a);
5147 pA->nDigit += pB->nDigit + 2;
5148 pA->nFrac += pB->nFrac;
5149 pA->sign ^= pB->sign;
5150 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5154 decimal_result(context, pA);
5165 int sqlite3_decimal_init(
5168 const sqlite3_api_routines *pApi
5171 static const struct {
5172 const char *zFuncName;
5174 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5176 { "decimal", 1, decimalFunc },
5177 { "decimal_cmp", 2, decimalCmpFunc },
5178 { "decimal_add", 2, decimalAddFunc },
5179 { "decimal_sub", 2, decimalSubFunc },
5180 { "decimal_mul", 2, decimalMulFunc },
5183 (void)pzErrMsg; /* Unused parameter */
5185 SQLITE_EXTENSION_INIT2(pApi);
5187 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5188 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5189 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5190 0, aFunc[i].xFunc, 0, 0);
5192 if( rc==SQLITE_OK ){
5193 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5194 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5195 decimalSumStep, decimalSumFinalize,
5196 decimalSumValue, decimalSumInverse, 0);
5198 if( rc==SQLITE_OK ){
5199 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5200 0, decimalCollFunc);
5205 /************************* End ../ext/misc/decimal.c ********************/
5206 /************************* Begin ../ext/misc/ieee754.c ******************/
5210 ** The author disclaims copyright to this source code. In place of
5211 ** a legal notice, here is a blessing:
5213 ** May you do good and not evil.
5214 ** May you find forgiveness for yourself and forgive others.
5215 ** May you share freely, never taking more than you give.
5217 ******************************************************************************
5219 ** This SQLite extension implements functions for the exact display
5220 ** and input of IEEE754 Binary64 floating-point numbers.
5225 ** In the first form, the value X should be a floating-point number.
5226 ** The function will return a string of the form 'ieee754(Y,Z)' where
5227 ** Y and Z are integers such that X==Y*pow(2,Z).
5229 ** In the second form, Y and Z are integers which are the mantissa and
5230 ** base-2 exponent of a new floating point number. The function returns
5231 ** a floating-point value equal to Y*pow(2,Z).
5235 ** ieee754(2.0) -> 'ieee754(2,0)'
5236 ** ieee754(45.25) -> 'ieee754(181,-2)'
5237 ** ieee754(2, 0) -> 2.0
5238 ** ieee754(181, -2) -> 45.25
5240 ** Two additional functions break apart the one-argument ieee754()
5241 ** result into separate integer values:
5243 ** ieee754_mantissa(45.25) -> 181
5244 ** ieee754_exponent(45.25) -> -2
5246 ** These functions convert binary64 numbers into blobs and back again.
5248 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5249 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5251 ** In all single-argument functions, if the argument is an 8-byte blob
5252 ** then that blob is interpreted as a big-endian binary64 value.
5255 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5256 ** -----------------------------------------------
5258 ** This extension in combination with the separate 'decimal' extension
5259 ** can be used to compute the exact decimal representation of binary64
5260 ** values. To begin, first compute a table of exponent values:
5262 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5263 ** WITH RECURSIVE c(x,v) AS (
5266 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5267 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5268 ** WITH RECURSIVE c(x,v) AS (
5271 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5272 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5274 ** Then, to compute the exact decimal representation of a floating
5275 ** point value (the value 47.49 is used in the example) do:
5277 ** WITH c(n) AS (VALUES(47.49))
5278 ** ---------------^^^^^---- Replace with whatever you want
5279 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5280 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5282 ** Here is a query to show various boundry values for the binary64
5285 ** WITH c(name,bin) AS (VALUES
5286 ** ('minimum positive value', x'0000000000000001'),
5287 ** ('maximum subnormal value', x'000fffffffffffff'),
5288 ** ('mininum positive nornal value', x'0010000000000000'),
5289 ** ('maximum value', x'7fefffffffffffff'))
5290 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5291 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5294 /* #include "sqlite3ext.h" */
5295 SQLITE_EXTENSION_INIT1
5299 /* Mark a function parameter as unused, to suppress nuisance compiler
5301 #ifndef UNUSED_PARAMETER
5302 # define UNUSED_PARAMETER(X) (void)(X)
5306 ** Implementation of the ieee754() function
5308 static void ieee754func(
5309 sqlite3_context *context,
5311 sqlite3_value **argv
5319 assert( sizeof(m)==sizeof(r) );
5320 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5321 && sqlite3_value_bytes(argv[0])==sizeof(r)
5323 const unsigned char *x = sqlite3_value_blob(argv[0]);
5325 sqlite3_uint64 v = 0;
5326 for(i=0; i<sizeof(r); i++){
5329 memcpy(&r, &v, sizeof(r));
5331 r = sqlite3_value_double(argv[0]);
5339 memcpy(&a,&r,sizeof(a));
5345 m = a & ((((sqlite3_int64)1)<<52)-1);
5349 m |= ((sqlite3_int64)1)<<52;
5351 while( e<1075 && m>0 && (m&1)==0 ){
5357 switch( *(int*)sqlite3_user_data(context) ){
5359 sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5361 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5364 sqlite3_result_int64(context, m);
5367 sqlite3_result_int(context, e-1075);
5371 sqlite3_int64 m, e, a;
5374 m = sqlite3_value_int64(argv[0]);
5375 e = sqlite3_value_int64(argv[1]);
5377 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */
5380 }else if( e<-10000 ){
5388 }else if( m==0 && e>-1000 && e<1000 ){
5389 sqlite3_result_double(context, 0.0);
5392 while( (m>>32)&0xffe00000 ){
5396 while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5409 }else if( e>0x7ff ){
5412 a = m & ((((sqlite3_int64)1)<<52)-1);
5414 if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5415 memcpy(&r, &a, sizeof(r));
5416 sqlite3_result_double(context, r);
5421 ** Functions to convert between blobs and floats.
5423 static void ieee754func_from_blob(
5424 sqlite3_context *context,
5426 sqlite3_value **argv
5428 UNUSED_PARAMETER(argc);
5429 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5430 && sqlite3_value_bytes(argv[0])==sizeof(double)
5433 const unsigned char *x = sqlite3_value_blob(argv[0]);
5435 sqlite3_uint64 v = 0;
5436 for(i=0; i<sizeof(r); i++){
5439 memcpy(&r, &v, sizeof(r));
5440 sqlite3_result_double(context, r);
5443 static void ieee754func_to_blob(
5444 sqlite3_context *context,
5446 sqlite3_value **argv
5448 UNUSED_PARAMETER(argc);
5449 if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5450 || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5452 double r = sqlite3_value_double(argv[0]);
5454 unsigned char a[sizeof(r)];
5456 memcpy(&v, &r, sizeof(r));
5457 for(i=1; i<=sizeof(r); i++){
5458 a[sizeof(r)-i] = v&0xff;
5461 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5469 int sqlite3_ieee_init(
5472 const sqlite3_api_routines *pApi
5474 static const struct {
5478 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5480 { "ieee754", 1, 0, ieee754func },
5481 { "ieee754", 2, 0, ieee754func },
5482 { "ieee754_mantissa", 1, 1, ieee754func },
5483 { "ieee754_exponent", 1, 2, ieee754func },
5484 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5485 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5490 SQLITE_EXTENSION_INIT2(pApi);
5491 (void)pzErrMsg; /* Unused parameter */
5492 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5493 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5494 SQLITE_UTF8|SQLITE_INNOCUOUS,
5495 (void*)&aFunc[i].iAux,
5496 aFunc[i].xFunc, 0, 0);
5501 /************************* End ../ext/misc/ieee754.c ********************/
5502 /************************* Begin ../ext/misc/series.c ******************/
5506 ** The author disclaims copyright to this source code. In place of
5507 ** a legal notice, here is a blessing:
5509 ** May you do good and not evil.
5510 ** May you find forgiveness for yourself and forgive others.
5511 ** May you share freely, never taking more than you give.
5513 *************************************************************************
5515 ** This file demonstrates how to create a table-valued-function using
5516 ** a virtual table. This demo implements the generate_series() function
5517 ** which gives similar results to the eponymous function in PostgreSQL.
5520 ** SELECT * FROM generate_series(0,100,5);
5522 ** The query above returns integers from 0 through 100 counting by steps
5525 ** SELECT * FROM generate_series(0,100);
5527 ** Integers from 0 through 100 with a step size of 1.
5529 ** SELECT * FROM generate_series(20) LIMIT 10;
5531 ** Integers 20 through 29.
5535 ** The generate_series "function" is really a virtual table with the
5536 ** following schema:
5538 ** CREATE TABLE generate_series(
5545 ** Function arguments in queries against this virtual table are translated
5546 ** into equality constraints against successive hidden columns. In other
5547 ** words, the following pairs of queries are equivalent to each other:
5549 ** SELECT * FROM generate_series(0,100,5);
5550 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5552 ** SELECT * FROM generate_series(0,100);
5553 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5555 ** SELECT * FROM generate_series(20) LIMIT 10;
5556 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5558 ** The generate_series virtual table implementation leaves the xCreate method
5559 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5560 ** TABLE command with "generate_series" as the USING argument. Instead, there
5561 ** is a single generate_series virtual table that is always available without
5562 ** having to be created first.
5564 ** The xBestIndex method looks for equality constraints against the hidden
5565 ** start, stop, and step columns, and if present, it uses those constraints
5566 ** to bound the sequence of generated values. If the equality constraints
5567 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5568 ** xBestIndex returns a small cost when both start and stop are available,
5569 ** and a very large cost if either start or stop are unavailable. This
5570 ** encourages the query planner to order joins such that the bounds of the
5571 ** series are well-defined.
5573 /* #include "sqlite3ext.h" */
5574 SQLITE_EXTENSION_INIT1
5578 #ifndef SQLITE_OMIT_VIRTUALTABLE
5581 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5582 ** serve as the underlying representation of a cursor that scans
5583 ** over rows of the result
5585 typedef struct series_cursor series_cursor;
5586 struct series_cursor {
5587 sqlite3_vtab_cursor base; /* Base class - must be first */
5588 int isDesc; /* True to count down rather than up */
5589 sqlite3_int64 iRowid; /* The rowid */
5590 sqlite3_int64 iValue; /* Current value ("value") */
5591 sqlite3_int64 mnValue; /* Mimimum value ("start") */
5592 sqlite3_int64 mxValue; /* Maximum value ("stop") */
5593 sqlite3_int64 iStep; /* Increment ("step") */
5597 ** The seriesConnect() method is invoked to create a new
5598 ** series_vtab that describes the generate_series virtual table.
5600 ** Think of this routine as the constructor for series_vtab objects.
5602 ** All this routine needs to do is:
5604 ** (1) Allocate the series_vtab object and initialize all fields.
5606 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5607 ** result set of queries against generate_series will look like.
5609 static int seriesConnect(
5612 int argcUnused, const char *const*argvUnused,
5613 sqlite3_vtab **ppVtab,
5619 /* Column numbers */
5620 #define SERIES_COLUMN_VALUE 0
5621 #define SERIES_COLUMN_START 1
5622 #define SERIES_COLUMN_STOP 2
5623 #define SERIES_COLUMN_STEP 3
5629 rc = sqlite3_declare_vtab(db,
5630 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5631 if( rc==SQLITE_OK ){
5632 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5633 if( pNew==0 ) return SQLITE_NOMEM;
5634 memset(pNew, 0, sizeof(*pNew));
5635 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5641 ** This method is the destructor for series_cursor objects.
5643 static int seriesDisconnect(sqlite3_vtab *pVtab){
5644 sqlite3_free(pVtab);
5649 ** Constructor for a new series_cursor object.
5651 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5652 series_cursor *pCur;
5654 pCur = sqlite3_malloc( sizeof(*pCur) );
5655 if( pCur==0 ) return SQLITE_NOMEM;
5656 memset(pCur, 0, sizeof(*pCur));
5657 *ppCursor = &pCur->base;
5662 ** Destructor for a series_cursor.
5664 static int seriesClose(sqlite3_vtab_cursor *cur){
5671 ** Advance a series_cursor to its next row of output.
5673 static int seriesNext(sqlite3_vtab_cursor *cur){
5674 series_cursor *pCur = (series_cursor*)cur;
5676 pCur->iValue -= pCur->iStep;
5678 pCur->iValue += pCur->iStep;
5685 ** Return values of columns for the row at which the series_cursor
5686 ** is currently pointing.
5688 static int seriesColumn(
5689 sqlite3_vtab_cursor *cur, /* The cursor */
5690 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5691 int i /* Which column to return */
5693 series_cursor *pCur = (series_cursor*)cur;
5694 sqlite3_int64 x = 0;
5696 case SERIES_COLUMN_START: x = pCur->mnValue; break;
5697 case SERIES_COLUMN_STOP: x = pCur->mxValue; break;
5698 case SERIES_COLUMN_STEP: x = pCur->iStep; break;
5699 default: x = pCur->iValue; break;
5701 sqlite3_result_int64(ctx, x);
5706 ** Return the rowid for the current row. In this implementation, the
5707 ** first row returned is assigned rowid value 1, and each subsequent
5708 ** row a value 1 more than that of the previous.
5710 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5711 series_cursor *pCur = (series_cursor*)cur;
5712 *pRowid = pCur->iRowid;
5717 ** Return TRUE if the cursor has been moved off of the last
5720 static int seriesEof(sqlite3_vtab_cursor *cur){
5721 series_cursor *pCur = (series_cursor*)cur;
5723 return pCur->iValue < pCur->mnValue;
5725 return pCur->iValue > pCur->mxValue;
5729 /* True to cause run-time checking of the start=, stop=, and/or step=
5730 ** parameters. The only reason to do this is for testing the
5731 ** constraint checking logic for virtual tables in the SQLite core.
5733 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5734 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5738 ** This method is called to "rewind" the series_cursor object back
5739 ** to the first row of output. This method is always called at least
5740 ** once prior to any call to seriesColumn() or seriesRowid() or
5743 ** The query plan selected by seriesBestIndex is passed in the idxNum
5744 ** parameter. (idxStr is not used in this implementation.) idxNum
5745 ** is a bitmask showing which constraints are available:
5751 ** Also, if bit 8 is set, that means that the series should be output
5752 ** in descending order rather than in ascending order. If bit 16 is
5753 ** set, then output must appear in ascending order.
5755 ** This routine should initialize the cursor and position it so that it
5756 ** is pointing at the first row, or pointing off the end of the table
5757 ** (so that seriesEof() will return true) if the table is empty.
5759 static int seriesFilter(
5760 sqlite3_vtab_cursor *pVtabCursor,
5761 int idxNum, const char *idxStrUnused,
5762 int argc, sqlite3_value **argv
5764 series_cursor *pCur = (series_cursor *)pVtabCursor;
5768 pCur->mnValue = sqlite3_value_int64(argv[i++]);
5773 pCur->mxValue = sqlite3_value_int64(argv[i++]);
5775 pCur->mxValue = 0xffffffff;
5778 pCur->iStep = sqlite3_value_int64(argv[i++]);
5779 if( pCur->iStep==0 ){
5781 }else if( pCur->iStep<0 ){
5782 pCur->iStep = -pCur->iStep;
5783 if( (idxNum & 16)==0 ) idxNum |= 8;
5788 for(i=0; i<argc; i++){
5789 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5790 /* If any of the constraints have a NULL value, then return no rows.
5791 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5799 pCur->iValue = pCur->mxValue;
5800 if( pCur->iStep>0 ){
5801 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5805 pCur->iValue = pCur->mnValue;
5812 ** SQLite will invoke this method one or more times while planning a query
5813 ** that uses the generate_series virtual table. This routine needs to create
5814 ** a query plan for each invocation and compute an estimated cost for that
5817 ** In this implementation idxNum is used to represent the
5818 ** query plan. idxStr is unused.
5820 ** The query plan is represented by bits in idxNum:
5822 ** (1) start = $value -- constraint exists
5823 ** (2) stop = $value -- constraint exists
5824 ** (4) step = $value -- constraint exists
5825 ** (8) output in descending order
5827 static int seriesBestIndex(
5828 sqlite3_vtab *pVTab,
5829 sqlite3_index_info *pIdxInfo
5831 int i, j; /* Loop over constraints */
5832 int idxNum = 0; /* The query plan bitmask */
5833 int bStartSeen = 0; /* EQ constraint seen on the START column */
5834 int unusableMask = 0; /* Mask of unusable constraints */
5835 int nArg = 0; /* Number of arguments that seriesFilter() expects */
5836 int aIdx[3]; /* Constraints on start, stop, and step */
5837 const struct sqlite3_index_constraint *pConstraint;
5839 /* This implementation assumes that the start, stop, and step columns
5840 ** are the last three columns in the virtual table. */
5841 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5842 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5844 aIdx[0] = aIdx[1] = aIdx[2] = -1;
5845 pConstraint = pIdxInfo->aConstraint;
5846 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5847 int iCol; /* 0 for start, 1 for stop, 2 for step */
5848 int iMask; /* bitmask for those column */
5849 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5850 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5851 assert( iCol>=0 && iCol<=2 );
5853 if( iCol==0 ) bStartSeen = 1;
5854 if( pConstraint->usable==0 ){
5855 unusableMask |= iMask;
5857 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5863 if( (j = aIdx[i])>=0 ){
5864 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5865 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5868 /* The current generate_column() implementation requires at least one
5869 ** argument (the START value). Legacy versions assumed START=0 if the
5870 ** first argument was omitted. Compile with -DZERO_ARGUMENT_GENERATE_SERIES
5871 ** to obtain the legacy behavior */
5872 #ifndef ZERO_ARGUMENT_GENERATE_SERIES
5874 sqlite3_free(pVTab->zErrMsg);
5875 pVTab->zErrMsg = sqlite3_mprintf(
5876 "first argument to \"generate_series()\" missing or unusable");
5877 return SQLITE_ERROR;
5880 if( (unusableMask & ~idxNum)!=0 ){
5881 /* The start, stop, and step columns are inputs. Therefore if there
5882 ** are unusable constraints on any of start, stop, or step then
5883 ** this plan is unusable */
5884 return SQLITE_CONSTRAINT;
5886 if( (idxNum & 3)==3 ){
5887 /* Both start= and stop= boundaries are available. This is the
5888 ** the preferred case */
5889 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5890 pIdxInfo->estimatedRows = 1000;
5891 if( pIdxInfo->nOrderBy==1 ){
5892 if( pIdxInfo->aOrderBy[0].desc ){
5897 pIdxInfo->orderByConsumed = 1;
5900 /* If either boundary is missing, we have to generate a huge span
5901 ** of numbers. Make this case very expensive so that the query
5902 ** planner will work hard to avoid it. */
5903 pIdxInfo->estimatedRows = 2147483647;
5905 pIdxInfo->idxNum = idxNum;
5910 ** This following structure defines all the methods for the
5911 ** generate_series virtual table.
5913 static sqlite3_module seriesModule = {
5916 seriesConnect, /* xConnect */
5917 seriesBestIndex, /* xBestIndex */
5918 seriesDisconnect, /* xDisconnect */
5920 seriesOpen, /* xOpen - open a cursor */
5921 seriesClose, /* xClose - close a cursor */
5922 seriesFilter, /* xFilter - configure scan constraints */
5923 seriesNext, /* xNext - advance a cursor */
5924 seriesEof, /* xEof - check for end of scan */
5925 seriesColumn, /* xColumn - read data */
5926 seriesRowid, /* xRowid - read data */
5932 0, /* xFindMethod */
5936 0, /* xRollbackTo */
5940 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5945 int sqlite3_series_init(
5948 const sqlite3_api_routines *pApi
5951 SQLITE_EXTENSION_INIT2(pApi);
5952 #ifndef SQLITE_OMIT_VIRTUALTABLE
5953 if( sqlite3_libversion_number()<3008012 && pzErrMsg!=0 ){
5954 *pzErrMsg = sqlite3_mprintf(
5955 "generate_series() requires SQLite 3.8.12 or later");
5956 return SQLITE_ERROR;
5958 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5963 /************************* End ../ext/misc/series.c ********************/
5964 /************************* Begin ../ext/misc/regexp.c ******************/
5968 ** The author disclaims copyright to this source code. In place of
5969 ** a legal notice, here is a blessing:
5971 ** May you do good and not evil.
5972 ** May you find forgiveness for yourself and forgive others.
5973 ** May you share freely, never taking more than you give.
5975 ******************************************************************************
5977 ** The code in this file implements a compact but reasonably
5978 ** efficient regular-expression matcher for posix extended regular
5979 ** expressions against UTF8 text.
5981 ** This file is an SQLite extension. It registers a single function
5982 ** named "regexp(A,B)" where A is the regular expression and B is the
5983 ** string to be matched. By registering this function, SQLite will also
5984 ** then implement the "B regexp A" operator. Note that with the function
5985 ** the regular expression comes first, but with the operator it comes
5988 ** The following regular expression syntax is supported:
5990 ** X* zero or more occurrences of X
5991 ** X+ one or more occurrences of X
5992 ** X? zero or one occurrences of X
5993 ** X{p,q} between p and q occurrences of X
5996 ** ^X X occurring at the beginning of the string
5997 ** X$ X occurring at the end of the string
5998 ** . Match any single character
5999 ** \c Character c where c is one of \{}()[]|*+?.
6000 ** \c C-language escapes for c in afnrtv. ex: \t or \n
6001 ** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX
6002 ** \xXX Where XX is exactly 2 hex digits, unicode value XX
6003 ** [abc] Any single character from the set abc
6004 ** [^abc] Any single character not in the set abc
6005 ** [a-z] Any single character in the range a-z
6006 ** [^a-z] Any single character not in the range a-z
6008 ** \w Word character. [A-Za-z0-9_]
6009 ** \W Non-word character
6012 ** \s Whitespace character
6013 ** \S Non-whitespace character
6015 ** A nondeterministic finite automaton (NFA) is used for matching, so the
6016 ** performance is bounded by O(N*M) where N is the size of the regular
6017 ** expression and M is the size of the input string. The matcher never
6018 ** exhibits exponential behavior. Note that the X{p,q} operator expands
6019 ** to p copies of X following by q-p copies of X? and that the size of the
6020 ** regular expression in the O(N*M) performance bound is computed after
6025 /* #include "sqlite3ext.h" */
6026 SQLITE_EXTENSION_INIT1
6029 ** The following #defines change the names of some functions implemented in
6030 ** this file to prevent name collisions with C-library functions of the
6033 #define re_match sqlite3re_match
6034 #define re_compile sqlite3re_compile
6035 #define re_free sqlite3re_free
6037 /* The end-of-input character */
6038 #define RE_EOF 0 /* End of input */
6040 /* The NFA is implemented as sequence of opcodes taken from the following
6041 ** set. Each opcode has a single integer argument.
6043 #define RE_OP_MATCH 1 /* Match the one character in the argument */
6044 #define RE_OP_ANY 2 /* Match any one character. (Implements ".") */
6045 #define RE_OP_ANYSTAR 3 /* Special optimized version of .* */
6046 #define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */
6047 #define RE_OP_GOTO 5 /* Jump to opcode at iArg */
6048 #define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */
6049 #define RE_OP_CC_INC 7 /* Beginning of a [...] character class */
6050 #define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */
6051 #define RE_OP_CC_VALUE 9 /* Single value in a character class */
6052 #define RE_OP_CC_RANGE 10 /* Range of values in a character class */
6053 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
6054 #define RE_OP_NOTWORD 12 /* Not a perl word character */
6055 #define RE_OP_DIGIT 13 /* digit: [0-9] */
6056 #define RE_OP_NOTDIGIT 14 /* Not a digit */
6057 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
6058 #define RE_OP_NOTSPACE 16 /* Not a digit */
6059 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
6061 /* Each opcode is a "state" in the NFA */
6062 typedef unsigned short ReStateNumber;
6064 /* Because this is an NFA and not a DFA, multiple states can be active at
6065 ** once. An instance of the following object records all active states in
6066 ** the NFA. The implementation is optimized for the common case where the
6067 ** number of actives states is small.
6069 typedef struct ReStateSet {
6070 unsigned nState; /* Number of current states */
6071 ReStateNumber *aState; /* Current states */
6074 /* An input string read one character at a time.
6076 typedef struct ReInput ReInput;
6078 const unsigned char *z; /* All text */
6079 int i; /* Next byte to read */
6080 int mx; /* EOF when i>=mx */
6083 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6084 ** an instance of the following object.
6086 typedef struct ReCompiled ReCompiled;
6088 ReInput sIn; /* Regular expression text */
6089 const char *zErr; /* Error message to return */
6090 char *aOp; /* Operators for the virtual machine */
6091 int *aArg; /* Arguments to each operator */
6092 unsigned (*xNextChar)(ReInput*); /* Next character function */
6093 unsigned char zInit[12]; /* Initial text to match */
6094 int nInit; /* Number of characters in zInit */
6095 unsigned nState; /* Number of entries in aOp[] and aArg[] */
6096 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
6099 /* Add a state to the given state set if it is not already there */
6100 static void re_add_state(ReStateSet *pSet, int newState){
6102 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6103 pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6106 /* Extract the next unicode character from *pzIn and return it. Advance
6107 ** *pzIn to the first byte past the end of the character returned. To
6108 ** be clear: this routine converts utf8 to unicode. This routine is
6109 ** optimized for the common case where the next character is a single byte.
6111 static unsigned re_next_char(ReInput *p){
6113 if( p->i>=p->mx ) return 0;
6116 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6117 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6118 if( c<0x80 ) c = 0xfffd;
6119 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6120 && (p->z[p->i+1]&0xc0)==0x80 ){
6121 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6123 if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6124 }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
6125 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6126 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6127 | (p->z[p->i+2]&0x3f);
6129 if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6136 static unsigned re_next_char_nocase(ReInput *p){
6137 unsigned c = re_next_char(p);
6138 if( c>='A' && c<='Z' ) c += 'a' - 'A';
6142 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
6143 static int re_word_char(int c){
6144 return (c>='0' && c<='9') || (c>='a' && c<='z')
6145 || (c>='A' && c<='Z') || c=='_';
6148 /* Return true if c is a "digit" character: [0-9] */
6149 static int re_digit_char(int c){
6150 return (c>='0' && c<='9');
6153 /* Return true if c is a perl "space" character: [ \t\r\n\v\f] */
6154 static int re_space_char(int c){
6155 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6158 /* Run a compiled regular expression on the zero-terminated input
6159 ** string zIn[]. Return true on a match and false if there is no match.
6161 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6162 ReStateSet aStateSet[2], *pThis, *pNext;
6163 ReStateNumber aSpace[100];
6164 ReStateNumber *pToFree;
6166 unsigned int iSwap = 0;
6174 in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6176 /* Look for the initial prefix match, if there is one. */
6178 unsigned char x = pRe->zInit[0];
6179 while( in.i+pRe->nInit<=in.mx
6181 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6185 if( in.i+pRe->nInit>in.mx ) return 0;
6188 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6190 aStateSet[0].aState = aSpace;
6192 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6193 if( pToFree==0 ) return -1;
6194 aStateSet[0].aState = pToFree;
6196 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6197 pNext = &aStateSet[1];
6199 re_add_state(pNext, 0);
6200 while( c!=RE_EOF && pNext->nState>0 ){
6202 c = pRe->xNextChar(&in);
6204 pNext = &aStateSet[iSwap];
6207 for(i=0; i<pThis->nState; i++){
6208 int x = pThis->aState[i];
6209 switch( pRe->aOp[x] ){
6211 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6215 if( c!=0 ) re_add_state(pNext, x+1);
6219 if( re_word_char(c) ) re_add_state(pNext, x+1);
6222 case RE_OP_NOTWORD: {
6223 if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6227 if( re_digit_char(c) ) re_add_state(pNext, x+1);
6230 case RE_OP_NOTDIGIT: {
6231 if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6235 if( re_space_char(c) ) re_add_state(pNext, x+1);
6238 case RE_OP_NOTSPACE: {
6239 if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6242 case RE_OP_BOUNDARY: {
6243 if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6246 case RE_OP_ANYSTAR: {
6247 re_add_state(pNext, x);
6248 re_add_state(pThis, x+1);
6252 re_add_state(pThis, x+pRe->aArg[x]);
6253 re_add_state(pThis, x+1);
6257 re_add_state(pThis, x+pRe->aArg[x]);
6260 case RE_OP_ACCEPT: {
6264 case RE_OP_CC_EXC: {
6266 /* fall-through */ goto re_op_cc_inc;
6268 case RE_OP_CC_INC: re_op_cc_inc: {
6270 int n = pRe->aArg[x];
6272 for(j=1; j>0 && j<n; j++){
6273 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6274 if( pRe->aArg[x+j]==c ){
6279 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6287 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6288 if( hit ) re_add_state(pNext, x+n);
6294 for(i=0; i<pNext->nState; i++){
6295 if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
6298 sqlite3_free(pToFree);
6302 /* Resize the opcode and argument arrays for an RE under construction.
6304 static int re_resize(ReCompiled *p, int N){
6307 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6308 if( aOp==0 ) return 1;
6310 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6311 if( aArg==0 ) return 1;
6317 /* Insert a new opcode and argument into an RE under construction. The
6318 ** insertion point is just prior to existing opcode iBefore.
6320 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6322 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6323 for(i=p->nState; i>iBefore; i--){
6324 p->aOp[i] = p->aOp[i-1];
6325 p->aArg[i] = p->aArg[i-1];
6328 p->aOp[iBefore] = (char)op;
6329 p->aArg[iBefore] = arg;
6333 /* Append a new opcode and argument to the end of the RE under construction.
6335 static int re_append(ReCompiled *p, int op, int arg){
6336 return re_insert(p, p->nState, op, arg);
6339 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6340 ** under construction.
6342 static void re_copy(ReCompiled *p, int iStart, int N){
6343 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6344 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6345 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6349 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
6350 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If
6351 ** c is not a hex digit *pV is unchanged.
6353 static int re_hex(int c, int *pV){
6354 if( c>='0' && c<='9' ){
6356 }else if( c>='a' && c<='f' ){
6358 }else if( c>='A' && c<='F' ){
6363 *pV = (*pV)*16 + (c & 0xff);
6367 /* A backslash character has been seen, read the next character and
6368 ** return its interpretation.
6370 static unsigned re_esc_char(ReCompiled *p){
6371 static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6372 static const char zTrans[] = "\a\f\n\r\t\v";
6375 if( p->sIn.i>=p->sIn.mx ) return 0;
6376 c = p->sIn.z[p->sIn.i];
6377 if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6378 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6379 if( re_hex(zIn[1],&v)
6380 && re_hex(zIn[2],&v)
6381 && re_hex(zIn[3],&v)
6382 && re_hex(zIn[4],&v)
6388 if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6389 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6390 if( re_hex(zIn[1],&v)
6391 && re_hex(zIn[2],&v)
6397 for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6399 if( i<6 ) c = zTrans[i];
6402 p->zErr = "unknown \\ escape";
6407 /* Forward declaration */
6408 static const char *re_subcompile_string(ReCompiled*);
6410 /* Peek at the next byte of input */
6411 static unsigned char rePeek(ReCompiled *p){
6412 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6415 /* Compile RE text into a sequence of opcodes. Continue up to the
6416 ** first unmatched ")" character, then return. If an error is found,
6417 ** return a pointer to the error message string.
6419 static const char *re_subcompile_re(ReCompiled *p){
6421 int iStart, iEnd, iGoto;
6423 zErr = re_subcompile_string(p);
6424 if( zErr ) return zErr;
6425 while( rePeek(p)=='|' ){
6427 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6428 iGoto = re_append(p, RE_OP_GOTO, 0);
6430 zErr = re_subcompile_string(p);
6431 if( zErr ) return zErr;
6432 p->aArg[iGoto] = p->nState - iGoto;
6437 /* Compile an element of regular expression text (anything that can be
6438 ** an operand to the "|" operator). Return NULL on success or a pointer
6439 ** to the error message if there is a problem.
6441 static const char *re_subcompile_string(ReCompiled *p){
6446 while( (c = p->xNextChar(&p->sIn))!=0 ){
6456 zErr = re_subcompile_re(p);
6457 if( zErr ) return zErr;
6458 if( rePeek(p)!=')' ) return "unmatched '('";
6463 if( rePeek(p)=='*' ){
6464 re_append(p, RE_OP_ANYSTAR, 0);
6467 re_append(p, RE_OP_ANY, 0);
6472 if( iPrev<0 ) return "'*' without operand";
6473 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6474 re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6478 if( iPrev<0 ) return "'+' without operand";
6479 re_append(p, RE_OP_FORK, iPrev - p->nState);
6483 if( iPrev<0 ) return "'?' without operand";
6484 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6490 if( iPrev<0 ) return "'{m,n}' without operand";
6491 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6496 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6498 if( c!='}' ) return "unmatched '{'";
6499 if( n>0 && n<m ) return "n less than m in '{m,n}'";
6501 sz = p->nState - iPrev;
6503 if( n==0 ) return "both m and n are zero in '{m,n}'";
6504 re_insert(p, iPrev, RE_OP_FORK, sz+1);
6507 for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6510 re_append(p, RE_OP_FORK, sz+1);
6511 re_copy(p, iPrev, sz);
6514 re_append(p, RE_OP_FORK, -sz);
6519 int iFirst = p->nState;
6520 if( rePeek(p)=='^' ){
6521 re_append(p, RE_OP_CC_EXC, 0);
6524 re_append(p, RE_OP_CC_INC, 0);
6526 while( (c = p->xNextChar(&p->sIn))!=0 ){
6527 if( c=='[' && rePeek(p)==':' ){
6528 return "POSIX character classes not supported";
6530 if( c=='\\' ) c = re_esc_char(p);
6531 if( rePeek(p)=='-' ){
6532 re_append(p, RE_OP_CC_RANGE, c);
6534 c = p->xNextChar(&p->sIn);
6535 if( c=='\\' ) c = re_esc_char(p);
6536 re_append(p, RE_OP_CC_RANGE, c);
6538 re_append(p, RE_OP_CC_VALUE, c);
6540 if( rePeek(p)==']' ){ p->sIn.i++; break; }
6542 if( c==0 ) return "unclosed '['";
6543 p->aArg[iFirst] = p->nState - iFirst;
6548 switch( rePeek(p) ){
6549 case 'b': specialOp = RE_OP_BOUNDARY; break;
6550 case 'd': specialOp = RE_OP_DIGIT; break;
6551 case 'D': specialOp = RE_OP_NOTDIGIT; break;
6552 case 's': specialOp = RE_OP_SPACE; break;
6553 case 'S': specialOp = RE_OP_NOTSPACE; break;
6554 case 'w': specialOp = RE_OP_WORD; break;
6555 case 'W': specialOp = RE_OP_NOTWORD; break;
6559 re_append(p, specialOp, 0);
6562 re_append(p, RE_OP_MATCH, c);
6567 re_append(p, RE_OP_MATCH, c);
6576 /* Free and reclaim all the memory used by a previously compiled
6577 ** regular expression. Applications should invoke this routine once
6578 ** for every call to re_compile() to avoid memory leaks.
6580 static void re_free(ReCompiled *pRe){
6582 sqlite3_free(pRe->aOp);
6583 sqlite3_free(pRe->aArg);
6589 ** Compile a textual regular expression in zIn[] into a compiled regular
6590 ** expression suitable for us by re_match() and return a pointer to the
6591 ** compiled regular expression in *ppRe. Return NULL on success or an
6592 ** error message if something goes wrong.
6594 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6600 pRe = sqlite3_malloc( sizeof(*pRe) );
6602 return "out of memory";
6604 memset(pRe, 0, sizeof(*pRe));
6605 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6606 if( re_resize(pRe, 30) ){
6608 return "out of memory";
6613 re_append(pRe, RE_OP_ANYSTAR, 0);
6615 pRe->sIn.z = (unsigned char*)zIn;
6617 pRe->sIn.mx = (int)strlen(zIn);
6618 zErr = re_subcompile_re(pRe);
6623 if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
6624 re_append(pRe, RE_OP_MATCH, RE_EOF);
6625 re_append(pRe, RE_OP_ACCEPT, 0);
6627 }else if( pRe->sIn.i>=pRe->sIn.mx ){
6628 re_append(pRe, RE_OP_ACCEPT, 0);
6632 return "unrecognized character";
6635 /* The following is a performance optimization. If the regex begins with
6636 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6637 ** one or more matching characters, enter those matching characters into
6638 ** zInit[]. The re_match() routine can then search ahead in the input
6639 ** string looking for the initial match without having to run the whole
6640 ** regex engine over the string. Do not worry able trying to match
6641 ** unicode characters beyond plane 0 - those are very rare and this is
6642 ** just an optimization. */
6643 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6644 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6645 unsigned x = pRe->aArg[i];
6647 pRe->zInit[j++] = (unsigned char)x;
6648 }else if( x<=0xfff ){
6649 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6650 pRe->zInit[j++] = 0x80 | (x&0x3f);
6651 }else if( x<=0xffff ){
6652 pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));
6653 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6654 pRe->zInit[j++] = 0x80 | (x&0x3f);
6659 if( j>0 && pRe->zInit[j-1]==0 ) j--;
6666 ** Implementation of the regexp() SQL function. This function implements
6667 ** the build-in REGEXP operator. The first argument to the function is the
6668 ** pattern and the second argument is the string. So, the SQL statements:
6672 ** is implemented as regexp(B,A).
6674 static void re_sql_func(
6675 sqlite3_context *context,
6677 sqlite3_value **argv
6679 ReCompiled *pRe; /* Compiled regular expression */
6680 const char *zPattern; /* The regular expression */
6681 const unsigned char *zStr;/* String being searched */
6682 const char *zErr; /* Compile error message */
6683 int setAux = 0; /* True to invoke sqlite3_set_auxdata() */
6685 (void)argc; /* Unused */
6686 pRe = sqlite3_get_auxdata(context, 0);
6688 zPattern = (const char*)sqlite3_value_text(argv[0]);
6689 if( zPattern==0 ) return;
6690 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6693 sqlite3_result_error(context, zErr, -1);
6697 sqlite3_result_error_nomem(context);
6702 zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6704 sqlite3_result_int(context, re_match(pRe, zStr, -1));
6707 sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6712 ** Invoke this routine to register the regexp() function with the
6713 ** SQLite database connection.
6718 int sqlite3_regexp_init(
6721 const sqlite3_api_routines *pApi
6724 SQLITE_EXTENSION_INIT2(pApi);
6725 (void)pzErrMsg; /* Unused */
6726 rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6727 0, re_sql_func, 0, 0);
6728 if( rc==SQLITE_OK ){
6729 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6730 ** of regexp(PATTERN,STRING). */
6731 rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6732 (void*)db, re_sql_func, 0, 0);
6737 /************************* End ../ext/misc/regexp.c ********************/
6738 #ifdef SQLITE_HAVE_ZLIB
6739 /************************* Begin ../ext/misc/zipfile.c ******************/
6743 ** The author disclaims copyright to this source code. In place of
6744 ** a legal notice, here is a blessing:
6746 ** May you do good and not evil.
6747 ** May you find forgiveness for yourself and forgive others.
6748 ** May you share freely, never taking more than you give.
6750 ******************************************************************************
6752 ** This file implements a virtual table for reading and writing ZIP archive
6757 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
6759 ** Current limitations:
6761 ** * No support for encryption
6762 ** * No support for ZIP archives spanning multiple files
6763 ** * No support for zip64 extensions
6764 ** * Only the "inflate/deflate" (zlib) compression method is supported
6766 /* #include "sqlite3ext.h" */
6767 SQLITE_EXTENSION_INIT1
6774 #ifndef SQLITE_OMIT_VIRTUALTABLE
6776 #ifndef SQLITE_AMALGAMATION
6779 # ifdef HAVE_UINT32_T
6780 # define UINT32_TYPE uint32_t
6782 # define UINT32_TYPE unsigned int
6786 # ifdef HAVE_UINT16_T
6787 # define UINT16_TYPE uint16_t
6789 # define UINT16_TYPE unsigned short int
6792 /* typedef sqlite3_int64 i64; */
6793 /* typedef unsigned char u8; */
6794 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
6795 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
6796 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6798 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6799 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
6801 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
6802 # define ALWAYS(X) (1)
6803 # define NEVER(X) (0)
6804 #elif !defined(NDEBUG)
6805 # define ALWAYS(X) ((X)?1:(assert(0),0))
6806 # define NEVER(X) ((X)?(assert(0),1):0)
6808 # define ALWAYS(X) (X)
6809 # define NEVER(X) (X)
6812 #endif /* SQLITE_AMALGAMATION */
6815 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
6817 ** In some ways it would be better to obtain these values from system
6818 ** header files. But, the dependency is undesirable and (a) these
6819 ** have been stable for decades, (b) the values are part of POSIX and
6820 ** are also made explicit in [man stat], and (c) are part of the
6821 ** file format for zip archives.
6824 # define S_IFDIR 0040000
6827 # define S_IFREG 0100000
6830 # define S_IFLNK 0120000
6833 static const char ZIPFILE_SCHEMA[] =
6835 "name PRIMARY KEY," /* 0: Name of file in zip archive */
6836 "mode," /* 1: POSIX mode for file */
6837 "mtime," /* 2: Last modification time (secs since 1970)*/
6838 "sz," /* 3: Size of object */
6839 "rawdata," /* 4: Raw data */
6840 "data," /* 5: Uncompressed data */
6841 "method," /* 6: Compression method (integer) */
6842 "z HIDDEN" /* 7: Name of zip file */
6845 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
6846 #define ZIPFILE_BUFFER_SIZE (64*1024)
6850 ** Magic numbers used to read and write zip files.
6852 ** ZIPFILE_NEWENTRY_MADEBY:
6853 ** Use this value for the "version-made-by" field in new zip file
6854 ** entries. The upper byte indicates "unix", and the lower byte
6855 ** indicates that the zip file matches pkzip specification 3.0.
6856 ** This is what info-zip seems to do.
6858 ** ZIPFILE_NEWENTRY_REQUIRED:
6859 ** Value for "version-required-to-extract" field of new entries.
6860 ** Version 2.0 is required to support folders and deflate compression.
6862 ** ZIPFILE_NEWENTRY_FLAGS:
6863 ** Value for "general-purpose-bit-flags" field of new entries. Bit
6864 ** 11 means "utf-8 filename and comment".
6866 ** ZIPFILE_SIGNATURE_CDS:
6867 ** First 4 bytes of a valid CDS record.
6869 ** ZIPFILE_SIGNATURE_LFH:
6870 ** First 4 bytes of a valid LFH record.
6872 ** ZIPFILE_SIGNATURE_EOCD
6873 ** First 4 bytes of a valid EOCD record.
6875 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
6876 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
6877 #define ZIPFILE_NEWENTRY_REQUIRED 20
6878 #define ZIPFILE_NEWENTRY_FLAGS 0x800
6879 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
6880 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
6881 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
6884 ** The sizes of the fixed-size part of each of the three main data
6885 ** structures in a zip archive.
6887 #define ZIPFILE_LFH_FIXED_SZ 30
6888 #define ZIPFILE_EOCD_FIXED_SZ 22
6889 #define ZIPFILE_CDS_FIXED_SZ 46
6892 *** 4.3.16 End of central directory record:
6894 *** end of central dir signature 4 bytes (0x06054b50)
6895 *** number of this disk 2 bytes
6896 *** number of the disk with the
6897 *** start of the central directory 2 bytes
6898 *** total number of entries in the
6899 *** central directory on this disk 2 bytes
6900 *** total number of entries in
6901 *** the central directory 2 bytes
6902 *** size of the central directory 4 bytes
6903 *** offset of start of central
6904 *** directory with respect to
6905 *** the starting disk number 4 bytes
6906 *** .ZIP file comment length 2 bytes
6907 *** .ZIP file comment (variable size)
6909 typedef struct ZipfileEOCD ZipfileEOCD;
6910 struct ZipfileEOCD {
6920 *** 4.3.12 Central directory structure:
6924 *** central file header signature 4 bytes (0x02014b50)
6925 *** version made by 2 bytes
6926 *** version needed to extract 2 bytes
6927 *** general purpose bit flag 2 bytes
6928 *** compression method 2 bytes
6929 *** last mod file time 2 bytes
6930 *** last mod file date 2 bytes
6932 *** compressed size 4 bytes
6933 *** uncompressed size 4 bytes
6934 *** file name length 2 bytes
6935 *** extra field length 2 bytes
6936 *** file comment length 2 bytes
6937 *** disk number start 2 bytes
6938 *** internal file attributes 2 bytes
6939 *** external file attributes 4 bytes
6940 *** relative offset of local header 4 bytes
6942 typedef struct ZipfileCDS ZipfileCDS;
6945 u16 iVersionExtract;
6960 char *zFile; /* Filename (sqlite3_malloc()) */
6964 *** 4.3.7 Local file header:
6966 *** local file header signature 4 bytes (0x04034b50)
6967 *** version needed to extract 2 bytes
6968 *** general purpose bit flag 2 bytes
6969 *** compression method 2 bytes
6970 *** last mod file time 2 bytes
6971 *** last mod file date 2 bytes
6973 *** compressed size 4 bytes
6974 *** uncompressed size 4 bytes
6975 *** file name length 2 bytes
6976 *** extra field length 2 bytes
6979 typedef struct ZipfileLFH ZipfileLFH;
6981 u16 iVersionExtract;
6993 typedef struct ZipfileEntry ZipfileEntry;
6994 struct ZipfileEntry {
6995 ZipfileCDS cds; /* Parsed CDS record */
6996 u32 mUnixTime; /* Modification time, in UNIX format */
6997 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
6998 i64 iDataOff; /* Offset to data in file (if aData==0) */
6999 u8 *aData; /* cds.szCompressed bytes of compressed data */
7000 ZipfileEntry *pNext; /* Next element in in-memory CDS */
7004 ** Cursor type for zipfile tables.
7006 typedef struct ZipfileCsr ZipfileCsr;
7008 sqlite3_vtab_cursor base; /* Base class - must be first */
7009 i64 iId; /* Cursor ID */
7010 u8 bEof; /* True when at EOF */
7011 u8 bNoop; /* If next xNext() call is no-op */
7013 /* Used outside of write transactions */
7014 FILE *pFile; /* Zip file */
7015 i64 iNextOff; /* Offset of next record in central directory */
7016 ZipfileEOCD eocd; /* Parse of central directory record */
7018 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
7019 ZipfileEntry *pCurrent; /* Current entry */
7020 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
7023 typedef struct ZipfileTab ZipfileTab;
7025 sqlite3_vtab base; /* Base class - must be first */
7026 char *zFile; /* Zip file this table accesses (may be NULL) */
7027 sqlite3 *db; /* Host database connection */
7028 u8 *aBuffer; /* Temporary buffer used for various tasks */
7030 ZipfileCsr *pCsrList; /* List of cursors */
7033 /* The following are used by write transactions only */
7034 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
7035 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
7036 FILE *pWriteFd; /* File handle open on zip archive */
7037 i64 szCurrent; /* Current size of zip archive */
7038 i64 szOrig; /* Size of archive at start of transaction */
7042 ** Set the error message contained in context ctx to the results of
7043 ** vprintf(zFmt, ...).
7045 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
7049 zMsg = sqlite3_vmprintf(zFmt, ap);
7050 sqlite3_result_error(ctx, zMsg, -1);
7056 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
7057 ** is not quoted, do nothing.
7059 static void zipfileDequote(char *zIn){
7061 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
7064 if( q=='[' ) q = ']';
7065 while( ALWAYS(zIn[iIn]) ){
7066 char c = zIn[iIn++];
7067 if( c==q && zIn[iIn++]!=q ) break;
7075 ** Construct a new ZipfileTab virtual table object.
7077 ** argv[0] -> module name ("zipfile")
7078 ** argv[1] -> database name
7079 ** argv[2] -> table name
7080 ** argv[...] -> "column name" and other module argument fields.
7082 static int zipfileConnect(
7085 int argc, const char *const*argv,
7086 sqlite3_vtab **ppVtab,
7089 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
7091 const char *zFile = 0;
7092 ZipfileTab *pNew = 0;
7095 /* If the table name is not "zipfile", require that the argument be
7096 ** specified. This stops zipfile tables from being created as:
7098 ** CREATE VIRTUAL TABLE zzz USING zipfile();
7100 ** It does not prevent:
7102 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
7104 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
7105 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
7106 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
7107 return SQLITE_ERROR;
7112 nFile = (int)strlen(zFile)+1;
7115 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
7116 if( rc==SQLITE_OK ){
7117 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
7118 if( pNew==0 ) return SQLITE_NOMEM;
7119 memset(pNew, 0, nByte+nFile);
7121 pNew->aBuffer = (u8*)&pNew[1];
7123 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
7124 memcpy(pNew->zFile, zFile, nFile);
7125 zipfileDequote(pNew->zFile);
7128 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7129 *ppVtab = (sqlite3_vtab*)pNew;
7134 ** Free the ZipfileEntry structure indicated by the only argument.
7136 static void zipfileEntryFree(ZipfileEntry *p){
7138 sqlite3_free(p->cds.zFile);
7144 ** Release resources that should be freed at the end of a write
7147 static void zipfileCleanupTransaction(ZipfileTab *pTab){
7148 ZipfileEntry *pEntry;
7149 ZipfileEntry *pNext;
7151 if( pTab->pWriteFd ){
7152 fclose(pTab->pWriteFd);
7155 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
7156 pNext = pEntry->pNext;
7157 zipfileEntryFree(pEntry);
7159 pTab->pFirstEntry = 0;
7160 pTab->pLastEntry = 0;
7161 pTab->szCurrent = 0;
7166 ** This method is the destructor for zipfile vtab objects.
7168 static int zipfileDisconnect(sqlite3_vtab *pVtab){
7169 zipfileCleanupTransaction((ZipfileTab*)pVtab);
7170 sqlite3_free(pVtab);
7175 ** Constructor for a new ZipfileCsr object.
7177 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
7178 ZipfileTab *pTab = (ZipfileTab*)p;
7180 pCsr = sqlite3_malloc(sizeof(*pCsr));
7181 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
7183 return SQLITE_NOMEM;
7185 memset(pCsr, 0, sizeof(*pCsr));
7186 pCsr->iId = ++pTab->iNextCsrid;
7187 pCsr->pCsrNext = pTab->pCsrList;
7188 pTab->pCsrList = pCsr;
7193 ** Reset a cursor back to the state it was in when first returned
7194 ** by zipfileOpen().
7196 static void zipfileResetCursor(ZipfileCsr *pCsr){
7198 ZipfileEntry *pNext;
7202 fclose(pCsr->pFile);
7204 zipfileEntryFree(pCsr->pCurrent);
7208 for(p=pCsr->pFreeEntry; p; p=pNext){
7210 zipfileEntryFree(p);
7215 ** Destructor for an ZipfileCsr.
7217 static int zipfileClose(sqlite3_vtab_cursor *cur){
7218 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7219 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
7221 zipfileResetCursor(pCsr);
7223 /* Remove this cursor from the ZipfileTab.pCsrList list. */
7224 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
7225 *pp = pCsr->pCsrNext;
7232 ** Set the error message for the virtual table associated with cursor
7233 ** pCsr to the results of vprintf(zFmt, ...).
7235 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
7238 sqlite3_free(pTab->base.zErrMsg);
7239 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
7242 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
7245 sqlite3_free(pCsr->base.pVtab->zErrMsg);
7246 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7251 ** Read nRead bytes of data from offset iOff of file pFile into buffer
7252 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
7255 ** If an error does occur, output variable (*pzErrmsg) may be set to point
7256 ** to an English language error message. It is the responsibility of the
7257 ** caller to eventually free this buffer using
7260 static int zipfileReadData(
7261 FILE *pFile, /* Read from this file */
7262 u8 *aRead, /* Read into this buffer */
7263 int nRead, /* Number of bytes to read */
7264 i64 iOff, /* Offset to read from */
7265 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
7268 fseek(pFile, (long)iOff, SEEK_SET);
7269 n = fread(aRead, 1, nRead, pFile);
7270 if( (int)n!=nRead ){
7271 *pzErrmsg = sqlite3_mprintf("error in fread()");
7272 return SQLITE_ERROR;
7277 static int zipfileAppendData(
7284 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
7285 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
7286 if( (int)n!=nWrite ){
7287 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
7288 return SQLITE_ERROR;
7290 pTab->szCurrent += nWrite;
7296 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7298 static u16 zipfileGetU16(const u8 *aBuf){
7299 return (aBuf[1] << 8) + aBuf[0];
7303 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7305 static u32 zipfileGetU32(const u8 *aBuf){
7306 if( aBuf==0 ) return 0;
7307 return ((u32)(aBuf[3]) << 24)
7308 + ((u32)(aBuf[2]) << 16)
7309 + ((u32)(aBuf[1]) << 8)
7310 + ((u32)(aBuf[0]) << 0);
7314 ** Write a 16-bit little endiate integer into buffer aBuf.
7316 static void zipfilePutU16(u8 *aBuf, u16 val){
7317 aBuf[0] = val & 0xFF;
7318 aBuf[1] = (val>>8) & 0xFF;
7322 ** Write a 32-bit little endiate integer into buffer aBuf.
7324 static void zipfilePutU32(u8 *aBuf, u32 val){
7325 aBuf[0] = val & 0xFF;
7326 aBuf[1] = (val>>8) & 0xFF;
7327 aBuf[2] = (val>>16) & 0xFF;
7328 aBuf[3] = (val>>24) & 0xFF;
7331 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7332 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7334 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
7335 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
7338 ** Magic numbers used to read CDS records.
7340 #define ZIPFILE_CDS_NFILE_OFF 28
7341 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
7344 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
7345 ** if the record is not well-formed, or SQLITE_OK otherwise.
7347 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
7349 u32 sig = zipfileRead32(aRead);
7351 if( sig!=ZIPFILE_SIGNATURE_CDS ){
7354 pCDS->iVersionMadeBy = zipfileRead16(aRead);
7355 pCDS->iVersionExtract = zipfileRead16(aRead);
7356 pCDS->flags = zipfileRead16(aRead);
7357 pCDS->iCompression = zipfileRead16(aRead);
7358 pCDS->mTime = zipfileRead16(aRead);
7359 pCDS->mDate = zipfileRead16(aRead);
7360 pCDS->crc32 = zipfileRead32(aRead);
7361 pCDS->szCompressed = zipfileRead32(aRead);
7362 pCDS->szUncompressed = zipfileRead32(aRead);
7363 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7364 pCDS->nFile = zipfileRead16(aRead);
7365 pCDS->nExtra = zipfileRead16(aRead);
7366 pCDS->nComment = zipfileRead16(aRead);
7367 pCDS->iDiskStart = zipfileRead16(aRead);
7368 pCDS->iInternalAttr = zipfileRead16(aRead);
7369 pCDS->iExternalAttr = zipfileRead32(aRead);
7370 pCDS->iOffset = zipfileRead32(aRead);
7371 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
7378 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
7379 ** if the record is not well-formed, or SQLITE_OK otherwise.
7381 static int zipfileReadLFH(
7385 u8 *aRead = aBuffer;
7388 u32 sig = zipfileRead32(aRead);
7389 if( sig!=ZIPFILE_SIGNATURE_LFH ){
7392 pLFH->iVersionExtract = zipfileRead16(aRead);
7393 pLFH->flags = zipfileRead16(aRead);
7394 pLFH->iCompression = zipfileRead16(aRead);
7395 pLFH->mTime = zipfileRead16(aRead);
7396 pLFH->mDate = zipfileRead16(aRead);
7397 pLFH->crc32 = zipfileRead32(aRead);
7398 pLFH->szCompressed = zipfileRead32(aRead);
7399 pLFH->szUncompressed = zipfileRead32(aRead);
7400 pLFH->nFile = zipfileRead16(aRead);
7401 pLFH->nExtra = zipfileRead16(aRead);
7408 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
7409 ** Scan through this buffer to find an "extra-timestamp" field. If one
7410 ** exists, extract the 32-bit modification-timestamp from it and store
7411 ** the value in output parameter *pmTime.
7413 ** Zero is returned if no extra-timestamp record could be found (and so
7414 ** *pmTime is left unchanged), or non-zero otherwise.
7416 ** The general format of an extra field is:
7418 ** Header ID 2 bytes
7419 ** Data Size 2 bytes
7422 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
7425 u8 *pEnd = &aExtra[nExtra];
7428 u16 id = zipfileRead16(p);
7429 u16 nByte = zipfileRead16(p);
7432 case ZIPFILE_EXTRA_TIMESTAMP: {
7434 if( b & 0x01 ){ /* 0x01 -> modtime is present */
7435 *pmTime = zipfileGetU32(&p[1]);
7448 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7449 ** fields of the CDS structure passed as the only argument to a 32-bit
7450 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7452 ** "Standard" MS-DOS time format:
7454 ** File modification time:
7455 ** Bits 00-04: seconds divided by 2
7456 ** Bits 05-10: minute
7458 ** File modification date:
7460 ** Bits 05-08: month (1-12)
7461 ** Bits 09-15: years from 1980
7463 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7465 static u32 zipfileMtime(ZipfileCDS *pCDS){
7466 int Y,M,D,X1,X2,A,B,sec,min,hr;
7468 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
7469 M = ((pCDS->mDate >> 5) & 0x0F);
7470 D = (pCDS->mDate & 0x1F);
7471 sec = (pCDS->mTime & 0x1F)*2;
7472 min = (pCDS->mTime >> 5) & 0x3F;
7473 hr = (pCDS->mTime >> 11) & 0x1F;
7478 X1 = 36525*(Y+4716)/100;
7479 X2 = 306001*(M+1)/10000;
7482 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
7483 return (u32)(JDsec - (i64)24405875*(i64)8640);
7487 ** The opposite of zipfileMtime(). This function populates the mTime and
7488 ** mDate fields of the CDS structure passed as the first argument according
7489 ** to the UNIX timestamp value passed as the second.
7491 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
7492 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
7493 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
7499 A = (int)((JD - 1867216.25)/36524.25);
7500 A = (int)(JD + 1 + A - (A/4));
7502 C = (int)((B - 122.1)/365.25);
7503 D = (36525*(C&32767))/100;
7504 E = (int)((B-D)/30.6001);
7506 day = B - D - (int)(30.6001*E);
7507 mon = (E<14 ? E-1 : E-13);
7508 yr = mon>2 ? C-4716 : C-4715;
7510 hr = (mUnixTime % (24*60*60)) / (60*60);
7511 min = (mUnixTime % (60*60)) / 60;
7512 sec = (mUnixTime % 60);
7515 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
7516 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
7518 pCds->mDate = pCds->mTime = 0;
7521 assert( mUnixTime<315507600
7522 || mUnixTime==zipfileMtime(pCds)
7523 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
7524 /* || (mUnixTime % 2) */
7529 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7530 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
7531 ** then pFile is a file-handle open on a zip file. In either case, this
7532 ** function creates a ZipfileEntry object based on the zip archive entry
7533 ** for which the CDS record is at offset iOff.
7535 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
7536 ** the new object. Otherwise, an SQLite error code is returned and the
7537 ** final value of (*ppEntry) undefined.
7539 static int zipfileGetEntry(
7540 ZipfileTab *pTab, /* Store any error message here */
7541 const u8 *aBlob, /* Pointer to in-memory file image */
7542 int nBlob, /* Size of aBlob[] in bytes */
7543 FILE *pFile, /* If aBlob==0, read from this file */
7544 i64 iOff, /* Offset of CDS record */
7545 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
7548 char **pzErr = &pTab->base.zErrMsg;
7552 aRead = pTab->aBuffer;
7553 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
7555 aRead = (u8*)&aBlob[iOff];
7558 if( rc==SQLITE_OK ){
7559 sqlite3_int64 nAlloc;
7562 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
7563 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
7564 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
7566 nAlloc = sizeof(ZipfileEntry) + nExtra;
7568 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
7571 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
7575 memset(pNew, 0, sizeof(ZipfileEntry));
7576 rc = zipfileReadCDS(aRead, &pNew->cds);
7577 if( rc!=SQLITE_OK ){
7578 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
7579 }else if( aBlob==0 ){
7580 rc = zipfileReadData(
7581 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
7584 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
7588 if( rc==SQLITE_OK ){
7589 u32 *pt = &pNew->mUnixTime;
7590 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
7591 pNew->aExtra = (u8*)&pNew[1];
7592 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
7593 if( pNew->cds.zFile==0 ){
7595 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
7596 pNew->mUnixTime = zipfileMtime(&pNew->cds);
7600 if( rc==SQLITE_OK ){
7601 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
7604 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
7606 aRead = (u8*)&aBlob[pNew->cds.iOffset];
7609 if( rc==SQLITE_OK ) rc = zipfileReadLFH(aRead, &lfh);
7610 if( rc==SQLITE_OK ){
7611 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
7612 pNew->iDataOff += lfh.nFile + lfh.nExtra;
7613 if( aBlob && pNew->cds.szCompressed ){
7614 pNew->aData = &pNew->aExtra[nExtra];
7615 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
7618 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
7619 (int)pNew->cds.iOffset
7624 if( rc!=SQLITE_OK ){
7625 zipfileEntryFree(pNew);
7635 ** Advance an ZipfileCsr to its next row of output.
7637 static int zipfileNext(sqlite3_vtab_cursor *cur){
7638 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7642 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
7643 zipfileEntryFree(pCsr->pCurrent);
7645 if( pCsr->iNextOff>=iEof ){
7648 ZipfileEntry *p = 0;
7649 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
7650 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
7651 if( rc==SQLITE_OK ){
7652 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
7653 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
7659 pCsr->pCurrent = pCsr->pCurrent->pNext;
7661 if( pCsr->pCurrent==0 ){
7670 static void zipfileFree(void *p) {
7675 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
7676 ** size is nOut bytes. This function uncompresses the data and sets the
7677 ** return value in context pCtx to the result (a blob).
7679 ** If an error occurs, an error code is left in pCtx instead.
7681 static void zipfileInflate(
7682 sqlite3_context *pCtx, /* Store result here */
7683 const u8 *aIn, /* Compressed data */
7684 int nIn, /* Size of buffer aIn[] in bytes */
7685 int nOut /* Expected output size */
7687 u8 *aRes = sqlite3_malloc(nOut);
7689 sqlite3_result_error_nomem(pCtx);
7693 memset(&str, 0, sizeof(str));
7695 str.next_in = (Byte*)aIn;
7697 str.next_out = (Byte*)aRes;
7698 str.avail_out = nOut;
7700 err = inflateInit2(&str, -15);
7702 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
7704 err = inflate(&str, Z_NO_FLUSH);
7705 if( err!=Z_STREAM_END ){
7706 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
7708 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
7718 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
7719 ** compresses it and sets (*ppOut) to point to a buffer containing the
7720 ** compressed data. The caller is responsible for eventually calling
7721 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
7722 ** is set to the size of buffer (*ppOut) in bytes.
7724 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
7725 ** code is returned and an error message left in virtual-table handle
7726 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
7729 static int zipfileDeflate(
7730 const u8 *aIn, int nIn, /* Input */
7731 u8 **ppOut, int *pnOut, /* Output */
7732 char **pzErr /* OUT: Error message */
7735 sqlite3_int64 nAlloc;
7739 memset(&str, 0, sizeof(str));
7740 str.next_in = (Bytef*)aIn;
7742 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
7744 nAlloc = deflateBound(&str, nIn);
7745 aOut = (u8*)sqlite3_malloc64(nAlloc);
7750 str.next_out = aOut;
7751 str.avail_out = nAlloc;
7752 res = deflate(&str, Z_FINISH);
7753 if( res==Z_STREAM_END ){
7755 *pnOut = (int)str.total_out;
7758 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
7769 ** Return values of columns for the row at which the series_cursor
7770 ** is currently pointing.
7772 static int zipfileColumn(
7773 sqlite3_vtab_cursor *cur, /* The cursor */
7774 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
7775 int i /* Which column to return */
7777 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7778 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
7782 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
7785 /* TODO: Whether or not the following is correct surely depends on
7786 ** the platform on which the archive was created. */
7787 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
7789 case 2: { /* mtime */
7790 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
7794 if( sqlite3_vtab_nochange(ctx)==0 ){
7795 sqlite3_result_int64(ctx, pCDS->szUncompressed);
7799 case 4: /* rawdata */
7800 if( sqlite3_vtab_nochange(ctx) ) break;
7801 case 5: { /* data */
7802 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
7803 int sz = pCDS->szCompressed;
7804 int szFinal = pCDS->szUncompressed;
7808 if( pCsr->pCurrent->aData ){
7809 aBuf = pCsr->pCurrent->aData;
7811 aBuf = aFree = sqlite3_malloc64(sz);
7815 FILE *pFile = pCsr->pFile;
7817 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
7819 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
7820 &pCsr->base.pVtab->zErrMsg
7824 if( rc==SQLITE_OK ){
7825 if( i==5 && pCDS->iCompression ){
7826 zipfileInflate(ctx, aBuf, sz, szFinal);
7828 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
7831 sqlite3_free(aFree);
7833 /* Figure out if this is a directory or a zero-sized file. Consider
7834 ** it to be a directory either if the mode suggests so, or if
7835 ** the final character in the name is '/'. */
7836 u32 mode = pCDS->iExternalAttr >> 16;
7837 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
7838 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
7844 case 6: /* method */
7845 sqlite3_result_int(ctx, pCDS->iCompression);
7849 sqlite3_result_int64(ctx, pCsr->iId);
7857 ** Return TRUE if the cursor is at EOF.
7859 static int zipfileEof(sqlite3_vtab_cursor *cur){
7860 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7865 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7866 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7867 ** is guaranteed to be a file-handle open on a zip file.
7869 ** This function attempts to locate the EOCD record within the zip archive
7870 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7871 ** returned if successful. Otherwise, an SQLite error code is returned and
7872 ** an English language error message may be left in virtual-table pTab.
7874 static int zipfileReadEOCD(
7875 ZipfileTab *pTab, /* Return errors here */
7876 const u8 *aBlob, /* Pointer to in-memory file image */
7877 int nBlob, /* Size of aBlob[] in bytes */
7878 FILE *pFile, /* Read from this file if aBlob==0 */
7879 ZipfileEOCD *pEOCD /* Object to populate */
7881 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
7882 int nRead; /* Bytes to read from file */
7885 memset(pEOCD, 0, sizeof(ZipfileEOCD));
7887 i64 iOff; /* Offset to read from */
7888 i64 szFile; /* Total size of file in bytes */
7889 fseek(pFile, 0, SEEK_END);
7890 szFile = (i64)ftell(pFile);
7894 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7895 iOff = szFile - nRead;
7896 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7898 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7899 aRead = (u8*)&aBlob[nBlob-nRead];
7902 if( rc==SQLITE_OK ){
7905 /* Scan backwards looking for the signature bytes */
7906 for(i=nRead-20; i>=0; i--){
7907 if( aRead[i]==0x50 && aRead[i+1]==0x4b
7908 && aRead[i+2]==0x05 && aRead[i+3]==0x06
7914 pTab->base.zErrMsg = sqlite3_mprintf(
7915 "cannot find end of central directory record"
7917 return SQLITE_ERROR;
7921 pEOCD->iDisk = zipfileRead16(aRead);
7922 pEOCD->iFirstDisk = zipfileRead16(aRead);
7923 pEOCD->nEntry = zipfileRead16(aRead);
7924 pEOCD->nEntryTotal = zipfileRead16(aRead);
7925 pEOCD->nSize = zipfileRead32(aRead);
7926 pEOCD->iOffset = zipfileRead32(aRead);
7933 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
7934 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7935 ** to the end of the list. Otherwise, it is added to the list immediately
7936 ** before pBefore (which is guaranteed to be a part of said list).
7938 static void zipfileAddEntry(
7940 ZipfileEntry *pBefore,
7943 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7944 assert( pNew->pNext==0 );
7946 if( pTab->pFirstEntry==0 ){
7947 pTab->pFirstEntry = pTab->pLastEntry = pNew;
7949 assert( pTab->pLastEntry->pNext==0 );
7950 pTab->pLastEntry->pNext = pNew;
7951 pTab->pLastEntry = pNew;
7955 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7956 pNew->pNext = pBefore;
7961 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7967 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7968 iOff = eocd.iOffset;
7969 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7970 ZipfileEntry *pNew = 0;
7971 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7973 if( rc==SQLITE_OK ){
7974 zipfileAddEntry(pTab, 0, pNew);
7975 iOff += ZIPFILE_CDS_FIXED_SZ;
7976 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7983 ** xFilter callback.
7985 static int zipfileFilter(
7986 sqlite3_vtab_cursor *cur,
7987 int idxNum, const char *idxStr,
7988 int argc, sqlite3_value **argv
7990 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7991 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7992 const char *zFile = 0; /* Zip file to scan */
7993 int rc = SQLITE_OK; /* Return Code */
7994 int bInMemory = 0; /* True for an in-memory zipfile */
7996 zipfileResetCursor(pCsr);
7999 zFile = pTab->zFile;
8000 }else if( idxNum==0 ){
8001 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
8002 return SQLITE_ERROR;
8003 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8004 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
8005 int nBlob = sqlite3_value_bytes(argv[0]);
8006 assert( pTab->pFirstEntry==0 );
8007 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
8008 pCsr->pFreeEntry = pTab->pFirstEntry;
8009 pTab->pFirstEntry = pTab->pLastEntry = 0;
8010 if( rc!=SQLITE_OK ) return rc;
8013 zFile = (const char*)sqlite3_value_text(argv[0]);
8016 if( 0==pTab->pWriteFd && 0==bInMemory ){
8017 pCsr->pFile = fopen(zFile, "rb");
8018 if( pCsr->pFile==0 ){
8019 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
8022 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
8023 if( rc==SQLITE_OK ){
8024 if( pCsr->eocd.nEntry==0 ){
8027 pCsr->iNextOff = pCsr->eocd.iOffset;
8028 rc = zipfileNext(cur);
8034 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
8035 rc = zipfileNext(cur);
8042 ** xBestIndex callback.
8044 static int zipfileBestIndex(
8046 sqlite3_index_info *pIdxInfo
8052 for(i=0; i<pIdxInfo->nConstraint; i++){
8053 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8054 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
8055 if( pCons->usable==0 ){
8057 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8061 pIdxInfo->estimatedCost = 1000.0;
8063 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
8064 pIdxInfo->aConstraintUsage[idx].omit = 1;
8065 pIdxInfo->idxNum = 1;
8066 }else if( unusable ){
8067 return SQLITE_CONSTRAINT;
8072 static ZipfileEntry *zipfileNewEntry(const char *zPath){
8074 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
8076 memset(pNew, 0, sizeof(ZipfileEntry));
8077 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
8078 if( pNew->cds.zFile==0 ){
8086 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
8087 ZipfileCDS *pCds = &pEntry->cds;
8092 /* Write the LFH itself */
8093 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
8094 zipfileWrite16(a, pCds->iVersionExtract);
8095 zipfileWrite16(a, pCds->flags);
8096 zipfileWrite16(a, pCds->iCompression);
8097 zipfileWrite16(a, pCds->mTime);
8098 zipfileWrite16(a, pCds->mDate);
8099 zipfileWrite32(a, pCds->crc32);
8100 zipfileWrite32(a, pCds->szCompressed);
8101 zipfileWrite32(a, pCds->szUncompressed);
8102 zipfileWrite16(a, (u16)pCds->nFile);
8103 zipfileWrite16(a, pCds->nExtra);
8104 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
8106 /* Add the file name */
8107 memcpy(a, pCds->zFile, (int)pCds->nFile);
8108 a += (int)pCds->nFile;
8110 /* The "extra" data */
8111 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8112 zipfileWrite16(a, 5);
8114 zipfileWrite32(a, pEntry->mUnixTime);
8119 static int zipfileAppendEntry(
8121 ZipfileEntry *pEntry,
8125 u8 *aBuf = pTab->aBuffer;
8129 nBuf = zipfileSerializeLFH(pEntry, aBuf);
8130 rc = zipfileAppendData(pTab, aBuf, nBuf);
8131 if( rc==SQLITE_OK ){
8132 pEntry->iDataOff = pTab->szCurrent;
8133 rc = zipfileAppendData(pTab, pData, nData);
8139 static int zipfileGetMode(
8140 sqlite3_value *pVal,
8141 int bIsDir, /* If true, default to directory */
8142 u32 *pMode, /* OUT: Mode value */
8143 char **pzErr /* OUT: Error message */
8145 const char *z = (const char*)sqlite3_value_text(pVal);
8148 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
8149 }else if( z[0]>='0' && z[0]<='9' ){
8150 mode = (unsigned int)sqlite3_value_int(pVal);
8152 const char zTemplate[11] = "-rwxrwxrwx";
8154 if( strlen(z)!=10 ) goto parse_error;
8156 case '-': mode |= S_IFREG; break;
8157 case 'd': mode |= S_IFDIR; break;
8158 case 'l': mode |= S_IFLNK; break;
8159 default: goto parse_error;
8161 for(i=1; i<10; i++){
8162 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
8163 else if( z[i]!='-' ) goto parse_error;
8166 if( ((mode & S_IFDIR)==0)==bIsDir ){
8167 /* The "mode" attribute is a directory, but data has been specified.
8168 ** Or vice-versa - no data but "mode" is a file or symlink. */
8169 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
8170 return SQLITE_CONSTRAINT;
8176 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
8177 return SQLITE_ERROR;
8181 ** Both (const char*) arguments point to nul-terminated strings. Argument
8182 ** nB is the value of strlen(zB). This function returns 0 if the strings are
8183 ** identical, ignoring any trailing '/' character in either path. */
8184 static int zipfileComparePath(const char *zA, const char *zB, int nB){
8185 int nA = (int)strlen(zA);
8186 if( nA>0 && zA[nA-1]=='/' ) nA--;
8187 if( nB>0 && zB[nB-1]=='/' ) nB--;
8188 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
8192 static int zipfileBegin(sqlite3_vtab *pVtab){
8193 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8196 assert( pTab->pWriteFd==0 );
8197 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
8198 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
8199 return SQLITE_ERROR;
8202 /* Open a write fd on the file. Also load the entire central directory
8203 ** structure into memory. During the transaction any new file data is
8204 ** appended to the archive file, but the central directory is accumulated
8205 ** in main-memory until the transaction is committed. */
8206 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
8207 if( pTab->pWriteFd==0 ){
8208 pTab->base.zErrMsg = sqlite3_mprintf(
8209 "zipfile: failed to open file %s for writing", pTab->zFile
8213 fseek(pTab->pWriteFd, 0, SEEK_END);
8214 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
8215 rc = zipfileLoadDirectory(pTab, 0, 0);
8218 if( rc!=SQLITE_OK ){
8219 zipfileCleanupTransaction(pTab);
8226 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8229 static u32 zipfileTime(void){
8230 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
8232 if( pVfs==0 ) return 0;
8233 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
8235 pVfs->xCurrentTimeInt64(pVfs, &ms);
8236 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
8239 pVfs->xCurrentTime(pVfs, &day);
8240 ret = (u32)((day - 2440587.5) * 86400);
8246 ** Return a 32-bit timestamp in UNIX epoch format.
8248 ** If the value passed as the only argument is either NULL or an SQL NULL,
8249 ** return the current time. Otherwise, return the value stored in (*pVal)
8250 ** cast to a 32-bit unsigned integer.
8252 static u32 zipfileGetTime(sqlite3_value *pVal){
8253 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
8254 return zipfileTime();
8256 return (u32)sqlite3_value_int64(pVal);
8260 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8261 ** linked list. Remove it from the list and free the object.
8263 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
8266 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
8268 zipfileEntryFree(pOld);
8275 static int zipfileUpdate(
8276 sqlite3_vtab *pVtab,
8278 sqlite3_value **apVal,
8279 sqlite_int64 *pRowid
8281 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8282 int rc = SQLITE_OK; /* Return Code */
8283 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
8285 u32 mode = 0; /* Mode for new entry */
8286 u32 mTime = 0; /* Modification time for new entry */
8287 i64 sz = 0; /* Uncompressed size */
8288 const char *zPath = 0; /* Path for new entry */
8289 int nPath = 0; /* strlen(zPath) */
8290 const u8 *pData = 0; /* Pointer to buffer containing content */
8291 int nData = 0; /* Size of pData buffer in bytes */
8292 int iMethod = 0; /* Compression method for new entry */
8293 u8 *pFree = 0; /* Free this */
8294 char *zFree = 0; /* Also free this */
8295 ZipfileEntry *pOld = 0;
8296 ZipfileEntry *pOld2 = 0;
8297 int bUpdate = 0; /* True for an update that modifies "name" */
8301 if( pTab->pWriteFd==0 ){
8302 rc = zipfileBegin(pVtab);
8303 if( rc!=SQLITE_OK ) return rc;
8306 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
8307 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
8308 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
8309 int nDelete = (int)strlen(zDelete);
8311 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
8312 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
8316 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
8317 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
8320 assert( pOld->pNext );
8325 /* Check that "sz" and "rawdata" are both NULL: */
8326 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
8327 zipfileTableErr(pTab, "sz must be NULL");
8328 rc = SQLITE_CONSTRAINT;
8330 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
8331 zipfileTableErr(pTab, "rawdata must be NULL");
8332 rc = SQLITE_CONSTRAINT;
8335 if( rc==SQLITE_OK ){
8336 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
8337 /* data=NULL. A directory */
8340 /* Value specified for "data", and possibly "method". This must be
8341 ** a regular file or a symlink. */
8342 const u8 *aIn = sqlite3_value_blob(apVal[7]);
8343 int nIn = sqlite3_value_bytes(apVal[7]);
8344 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
8346 iMethod = sqlite3_value_int(apVal[8]);
8350 if( iMethod!=0 && iMethod!=8 ){
8351 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
8352 rc = SQLITE_CONSTRAINT;
8354 if( bAuto || iMethod ){
8356 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
8357 if( rc==SQLITE_OK ){
8358 if( iMethod || nCmp<nIn ){
8365 iCrc32 = crc32(0, aIn, nIn);
8370 if( rc==SQLITE_OK ){
8371 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
8374 if( rc==SQLITE_OK ){
8375 zPath = (const char*)sqlite3_value_text(apVal[2]);
8376 if( zPath==0 ) zPath = "";
8377 nPath = (int)strlen(zPath);
8378 mTime = zipfileGetTime(apVal[4]);
8381 if( rc==SQLITE_OK && bIsDir ){
8382 /* For a directory, check that the last character in the path is a
8383 ** '/'. This appears to be required for compatibility with info-zip
8384 ** (the unzip command on unix). It does not create directories
8386 if( nPath<=0 || zPath[nPath-1]!='/' ){
8387 zFree = sqlite3_mprintf("%s/", zPath);
8388 zPath = (const char*)zFree;
8393 nPath = (int)strlen(zPath);
8398 /* Check that we're not inserting a duplicate entry -OR- updating an
8399 ** entry with a path, thereby making it into a duplicate. */
8400 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
8402 for(p=pTab->pFirstEntry; p; p=p->pNext){
8403 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
8404 switch( sqlite3_vtab_on_conflict(pTab->db) ){
8405 case SQLITE_IGNORE: {
8406 goto zipfile_update_done;
8408 case SQLITE_REPLACE: {
8413 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
8414 rc = SQLITE_CONSTRAINT;
8423 if( rc==SQLITE_OK ){
8424 /* Create the new CDS record. */
8425 pNew = zipfileNewEntry(zPath);
8429 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8430 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8431 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8432 pNew->cds.iCompression = (u16)iMethod;
8433 zipfileMtimeToDos(&pNew->cds, mTime);
8434 pNew->cds.crc32 = iCrc32;
8435 pNew->cds.szCompressed = nData;
8436 pNew->cds.szUncompressed = (u32)sz;
8437 pNew->cds.iExternalAttr = (mode<<16);
8438 pNew->cds.iOffset = (u32)pTab->szCurrent;
8439 pNew->cds.nFile = (u16)nPath;
8440 pNew->mUnixTime = (u32)mTime;
8441 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
8442 zipfileAddEntry(pTab, pOld, pNew);
8447 if( rc==SQLITE_OK && (pOld || pOld2) ){
8449 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8450 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
8451 pCsr->pCurrent = pCsr->pCurrent->pNext;
8456 zipfileRemoveEntryFromList(pTab, pOld);
8457 zipfileRemoveEntryFromList(pTab, pOld2);
8460 zipfile_update_done:
8461 sqlite3_free(pFree);
8462 sqlite3_free(zFree);
8466 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
8468 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
8469 zipfileWrite16(a, p->iDisk);
8470 zipfileWrite16(a, p->iFirstDisk);
8471 zipfileWrite16(a, p->nEntry);
8472 zipfileWrite16(a, p->nEntryTotal);
8473 zipfileWrite32(a, p->nSize);
8474 zipfileWrite32(a, p->iOffset);
8475 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
8480 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
8481 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
8482 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
8483 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
8487 ** Serialize the CDS structure into buffer aBuf[]. Return the number
8488 ** of bytes written.
8490 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
8492 ZipfileCDS *pCDS = &pEntry->cds;
8494 if( pEntry->aExtra==0 ){
8498 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
8499 zipfileWrite16(a, pCDS->iVersionMadeBy);
8500 zipfileWrite16(a, pCDS->iVersionExtract);
8501 zipfileWrite16(a, pCDS->flags);
8502 zipfileWrite16(a, pCDS->iCompression);
8503 zipfileWrite16(a, pCDS->mTime);
8504 zipfileWrite16(a, pCDS->mDate);
8505 zipfileWrite32(a, pCDS->crc32);
8506 zipfileWrite32(a, pCDS->szCompressed);
8507 zipfileWrite32(a, pCDS->szUncompressed);
8508 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
8509 zipfileWrite16(a, pCDS->nFile);
8510 zipfileWrite16(a, pCDS->nExtra);
8511 zipfileWrite16(a, pCDS->nComment);
8512 zipfileWrite16(a, pCDS->iDiskStart);
8513 zipfileWrite16(a, pCDS->iInternalAttr);
8514 zipfileWrite32(a, pCDS->iExternalAttr);
8515 zipfileWrite32(a, pCDS->iOffset);
8517 memcpy(a, pCDS->zFile, pCDS->nFile);
8520 if( pEntry->aExtra ){
8521 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
8522 memcpy(a, pEntry->aExtra, n);
8525 assert( pCDS->nExtra==9 );
8526 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8527 zipfileWrite16(a, 5);
8529 zipfileWrite32(a, pEntry->mUnixTime);
8535 static int zipfileCommit(sqlite3_vtab *pVtab){
8536 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8538 if( pTab->pWriteFd ){
8539 i64 iOffset = pTab->szCurrent;
8544 /* Write out all entries */
8545 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
8546 int n = zipfileSerializeCDS(p, pTab->aBuffer);
8547 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
8551 /* Write out the EOCD record */
8553 eocd.iFirstDisk = 0;
8554 eocd.nEntry = (u16)nEntry;
8555 eocd.nEntryTotal = (u16)nEntry;
8556 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
8557 eocd.iOffset = (u32)iOffset;
8558 rc = zipfileAppendEOCD(pTab, &eocd);
8560 zipfileCleanupTransaction(pTab);
8565 static int zipfileRollback(sqlite3_vtab *pVtab){
8566 return zipfileCommit(pVtab);
8569 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
8571 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8572 if( iId==pCsr->iId ) break;
8577 static void zipfileFunctionCds(
8578 sqlite3_context *context,
8580 sqlite3_value **argv
8583 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
8586 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
8588 ZipfileCDS *p = &pCsr->pCurrent->cds;
8589 char *zRes = sqlite3_mprintf("{"
8590 "\"version-made-by\" : %u, "
8591 "\"version-to-extract\" : %u, "
8593 "\"compression\" : %u, "
8597 "\"compressed-size\" : %u, "
8598 "\"uncompressed-size\" : %u, "
8599 "\"file-name-length\" : %u, "
8600 "\"extra-field-length\" : %u, "
8601 "\"file-comment-length\" : %u, "
8602 "\"disk-number-start\" : %u, "
8603 "\"internal-attr\" : %u, "
8604 "\"external-attr\" : %u, "
8605 "\"offset\" : %u }",
8606 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
8607 (u32)p->flags, (u32)p->iCompression,
8608 (u32)p->mTime, (u32)p->mDate,
8609 (u32)p->crc32, (u32)p->szCompressed,
8610 (u32)p->szUncompressed, (u32)p->nFile,
8611 (u32)p->nExtra, (u32)p->nComment,
8612 (u32)p->iDiskStart, (u32)p->iInternalAttr,
8613 (u32)p->iExternalAttr, (u32)p->iOffset
8617 sqlite3_result_error_nomem(context);
8619 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
8626 ** xFindFunction method.
8628 static int zipfileFindFunction(
8629 sqlite3_vtab *pVtab, /* Virtual table handle */
8630 int nArg, /* Number of SQL function arguments */
8631 const char *zName, /* Name of SQL function */
8632 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
8633 void **ppArg /* OUT: User data for *pxFunc */
8635 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
8636 *pxFunc = zipfileFunctionCds;
8637 *ppArg = (void*)pVtab;
8643 typedef struct ZipfileBuffer ZipfileBuffer;
8644 struct ZipfileBuffer {
8645 u8 *a; /* Pointer to buffer */
8646 int n; /* Size of buffer in bytes */
8647 int nAlloc; /* Byte allocated at a[] */
8650 typedef struct ZipfileCtx ZipfileCtx;
8657 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
8658 if( pBuf->n+nByte>pBuf->nAlloc ){
8660 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
8661 int nReq = pBuf->n + nByte;
8663 while( nNew<nReq ) nNew = nNew*2;
8664 aNew = sqlite3_realloc64(pBuf->a, nNew);
8665 if( aNew==0 ) return SQLITE_NOMEM;
8667 pBuf->nAlloc = (int)nNew;
8673 ** xStep() callback for the zipfile() aggregate. This can be called in
8674 ** any of the following ways:
8676 ** SELECT zipfile(name,data) ...
8677 ** SELECT zipfile(name,mode,mtime,data) ...
8678 ** SELECT zipfile(name,mode,mtime,data,method) ...
8680 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
8681 ZipfileCtx *p; /* Aggregate function context */
8682 ZipfileEntry e; /* New entry to add to zip archive */
8684 sqlite3_value *pName = 0;
8685 sqlite3_value *pMode = 0;
8686 sqlite3_value *pMtime = 0;
8687 sqlite3_value *pData = 0;
8688 sqlite3_value *pMethod = 0;
8695 int iMethod = -1; /* Compression method to use (0 or 8) */
8697 const u8 *aData = 0; /* Possibly compressed data for new entry */
8698 int nData = 0; /* Size of aData[] in bytes */
8699 int szUncompressed = 0; /* Size of data before compression */
8700 u8 *aFree = 0; /* Free this before returning */
8701 u32 iCrc32 = 0; /* crc32 of uncompressed data */
8703 char *zName = 0; /* Path (name) of new entry */
8704 int nName = 0; /* Size of zName in bytes */
8705 char *zFree = 0; /* Free this before returning */
8708 memset(&e, 0, sizeof(e));
8709 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8712 /* Martial the arguments into stack variables */
8713 if( nVal!=2 && nVal!=4 && nVal!=5 ){
8714 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
8716 goto zipfile_step_out;
8730 /* Check that the 'name' parameter looks ok. */
8731 zName = (char*)sqlite3_value_text(pName);
8732 nName = sqlite3_value_bytes(pName);
8734 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
8736 goto zipfile_step_out;
8739 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
8740 ** deflate compression) or NULL (choose automatically). */
8741 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
8742 iMethod = (int)sqlite3_value_int64(pMethod);
8743 if( iMethod!=0 && iMethod!=8 ){
8744 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
8746 goto zipfile_step_out;
8750 /* Now inspect the data. If this is NULL, then the new entry must be a
8751 ** directory. Otherwise, figure out whether or not the data should
8752 ** be deflated or simply stored in the zip archive. */
8753 if( sqlite3_value_type(pData)==SQLITE_NULL ){
8757 aData = sqlite3_value_blob(pData);
8758 szUncompressed = nData = sqlite3_value_bytes(pData);
8759 iCrc32 = crc32(0, aData, nData);
8760 if( iMethod<0 || iMethod==8 ){
8762 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
8763 if( rc!=SQLITE_OK ){
8764 goto zipfile_step_out;
8766 if( iMethod==8 || nOut<nData ){
8776 /* Decode the "mode" argument. */
8777 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
8778 if( rc ) goto zipfile_step_out;
8780 /* Decode the "mtime" argument. */
8781 e.mUnixTime = zipfileGetTime(pMtime);
8783 /* If this is a directory entry, ensure that there is exactly one '/'
8784 ** at the end of the path. Or, if this is not a directory and the path
8785 ** ends in '/' it is an error. */
8787 if( nName>0 && zName[nName-1]=='/' ){
8788 zErr = sqlite3_mprintf("non-directory name must not end with /");
8790 goto zipfile_step_out;
8793 if( nName==0 || zName[nName-1]!='/' ){
8794 zName = zFree = sqlite3_mprintf("%s/", zName);
8797 goto zipfile_step_out;
8799 nName = (int)strlen(zName);
8801 while( nName>1 && zName[nName-2]=='/' ) nName--;
8805 /* Assemble the ZipfileEntry object for the new zip archive entry */
8806 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8807 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8808 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8809 e.cds.iCompression = (u16)iMethod;
8810 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
8811 e.cds.crc32 = iCrc32;
8812 e.cds.szCompressed = nData;
8813 e.cds.szUncompressed = szUncompressed;
8814 e.cds.iExternalAttr = (mode<<16);
8815 e.cds.iOffset = p->body.n;
8816 e.cds.nFile = (u16)nName;
8817 e.cds.zFile = zName;
8819 /* Append the LFH to the body of the new archive */
8820 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
8821 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
8822 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
8824 /* Append the data to the body of the new archive */
8826 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
8827 memcpy(&p->body.a[p->body.n], aData, nData);
8831 /* Append the CDS record to the directory of the new archive */
8832 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
8833 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
8834 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
8836 /* Increment the count of entries in the archive */
8840 sqlite3_free(aFree);
8841 sqlite3_free(zFree);
8844 sqlite3_result_error(pCtx, zErr, -1);
8846 sqlite3_result_error_code(pCtx, rc);
8853 ** xFinalize() callback for zipfile aggregate function.
8855 void zipfileFinal(sqlite3_context *pCtx){
8861 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8864 memset(&eocd, 0, sizeof(eocd));
8865 eocd.nEntry = (u16)p->nEntry;
8866 eocd.nEntryTotal = (u16)p->nEntry;
8867 eocd.nSize = p->cds.n;
8868 eocd.iOffset = p->body.n;
8870 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8871 aZip = (u8*)sqlite3_malloc64(nZip);
8873 sqlite3_result_error_nomem(pCtx);
8875 memcpy(aZip, p->body.a, p->body.n);
8876 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8877 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8878 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8882 sqlite3_free(p->body.a);
8883 sqlite3_free(p->cds.a);
8888 ** Register the "zipfile" virtual table.
8890 static int zipfileRegister(sqlite3 *db){
8891 static sqlite3_module zipfileModule = {
8893 zipfileConnect, /* xCreate */
8894 zipfileConnect, /* xConnect */
8895 zipfileBestIndex, /* xBestIndex */
8896 zipfileDisconnect, /* xDisconnect */
8897 zipfileDisconnect, /* xDestroy */
8898 zipfileOpen, /* xOpen - open a cursor */
8899 zipfileClose, /* xClose - close a cursor */
8900 zipfileFilter, /* xFilter - configure scan constraints */
8901 zipfileNext, /* xNext - advance a cursor */
8902 zipfileEof, /* xEof - check for end of scan */
8903 zipfileColumn, /* xColumn - read data */
8904 0, /* xRowid - read data */
8905 zipfileUpdate, /* xUpdate */
8906 zipfileBegin, /* xBegin */
8908 zipfileCommit, /* xCommit */
8909 zipfileRollback, /* xRollback */
8910 zipfileFindFunction, /* xFindMethod */
8914 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
8915 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8916 if( rc==SQLITE_OK ){
8917 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
8918 zipfileStep, zipfileFinal
8921 assert( sizeof(i64)==8 );
8922 assert( sizeof(u32)==4 );
8923 assert( sizeof(u16)==2 );
8924 assert( sizeof(u8)==1 );
8927 #else /* SQLITE_OMIT_VIRTUALTABLE */
8928 # define zipfileRegister(x) SQLITE_OK
8934 int sqlite3_zipfile_init(
8937 const sqlite3_api_routines *pApi
8939 SQLITE_EXTENSION_INIT2(pApi);
8940 (void)pzErrMsg; /* Unused parameter */
8941 return zipfileRegister(db);
8944 /************************* End ../ext/misc/zipfile.c ********************/
8945 /************************* Begin ../ext/misc/sqlar.c ******************/
8949 ** The author disclaims copyright to this source code. In place of
8950 ** a legal notice, here is a blessing:
8952 ** May you do good and not evil.
8953 ** May you find forgiveness for yourself and forgive others.
8954 ** May you share freely, never taking more than you give.
8956 ******************************************************************************
8958 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8959 ** for working with sqlar archives and used by the shell tool's built-in
8962 /* #include "sqlite3ext.h" */
8963 SQLITE_EXTENSION_INIT1
8968 ** Implementation of the "sqlar_compress(X)" SQL function.
8970 ** If the type of X is SQLITE_BLOB, and compressing that blob using
8971 ** zlib utility function compress() yields a smaller blob, return the
8972 ** compressed blob. Otherwise, return a copy of X.
8974 ** SQLar uses the "zlib format" for compressed content. The zlib format
8975 ** contains a two-byte identification header and a four-byte checksum at
8976 ** the end. This is different from ZIP which uses the raw deflate format.
8978 ** Future enhancements to SQLar might add support for new compression formats.
8979 ** If so, those new formats will be identified by alternative headers in the
8982 static void sqlarCompressFunc(
8983 sqlite3_context *context,
8985 sqlite3_value **argv
8988 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8989 const Bytef *pData = sqlite3_value_blob(argv[0]);
8990 uLong nData = sqlite3_value_bytes(argv[0]);
8991 uLongf nOut = compressBound(nData);
8994 pOut = (Bytef*)sqlite3_malloc(nOut);
8996 sqlite3_result_error_nomem(context);
8999 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
9000 sqlite3_result_error(context, "error in compress()", -1);
9001 }else if( nOut<nData ){
9002 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
9004 sqlite3_result_value(context, argv[0]);
9009 sqlite3_result_value(context, argv[0]);
9014 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
9016 ** Parameter SZ is interpreted as an integer. If it is less than or
9017 ** equal to zero, then this function returns a copy of X. Or, if
9018 ** SZ is equal to the size of X when interpreted as a blob, also
9019 ** return a copy of X. Otherwise, decompress blob X using zlib
9020 ** utility function uncompress() and return the results (another
9023 static void sqlarUncompressFunc(
9024 sqlite3_context *context,
9026 sqlite3_value **argv
9032 sz = sqlite3_value_int(argv[1]);
9034 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
9035 sqlite3_result_value(context, argv[0]);
9037 const Bytef *pData= sqlite3_value_blob(argv[0]);
9038 Bytef *pOut = sqlite3_malloc(sz);
9039 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
9040 sqlite3_result_error(context, "error in uncompress()", -1);
9042 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
9052 int sqlite3_sqlar_init(
9055 const sqlite3_api_routines *pApi
9058 SQLITE_EXTENSION_INIT2(pApi);
9059 (void)pzErrMsg; /* Unused parameter */
9060 rc = sqlite3_create_function(db, "sqlar_compress", 1,
9061 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9062 sqlarCompressFunc, 0, 0);
9063 if( rc==SQLITE_OK ){
9064 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
9065 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
9066 sqlarUncompressFunc, 0, 0);
9071 /************************* End ../ext/misc/sqlar.c ********************/
9073 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
9077 ** The author disclaims copyright to this source code. In place of
9078 ** a legal notice, here is a blessing:
9080 ** May you do good and not evil.
9081 ** May you find forgiveness for yourself and forgive others.
9082 ** May you share freely, never taking more than you give.
9084 *************************************************************************
9086 #if !defined(SQLITEEXPERT_H)
9087 #define SQLITEEXPERT_H 1
9088 /* #include "sqlite3.h" */
9090 typedef struct sqlite3expert sqlite3expert;
9093 ** Create a new sqlite3expert object.
9095 ** If successful, a pointer to the new object is returned and (*pzErr) set
9096 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
9097 ** an English-language error message. In this case it is the responsibility
9098 ** of the caller to eventually free the error message buffer using
9101 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9104 ** Configure an sqlite3expert object.
9106 ** EXPERT_CONFIG_SAMPLE:
9107 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
9108 ** each candidate index. This involves scanning and sorting the entire
9109 ** contents of each user database table once for each candidate index
9110 ** associated with the table. For large databases, this can be
9111 ** prohibitively slow. This option allows the sqlite3expert object to
9112 ** be configured so that sqlite_stat1 data is instead generated based on a
9113 ** subset of each table, or so that no sqlite_stat1 data is used at all.
9115 ** A single integer argument is passed to this option. If the value is less
9116 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
9117 ** the analysis - indexes are recommended based on the database schema only.
9118 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
9119 ** generated for each candidate index (this is the default). Finally, if the
9120 ** value falls between 0 and 100, then it represents the percentage of user
9121 ** table rows that should be considered when generating sqlite_stat1 data.
9125 ** // Do not generate any sqlite_stat1 data
9126 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
9128 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
9129 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
9131 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
9133 #define EXPERT_CONFIG_SAMPLE 1 /* int */
9136 ** Specify zero or more SQL statements to be included in the analysis.
9138 ** Buffer zSql must contain zero or more complete SQL statements. This
9139 ** function parses all statements contained in the buffer and adds them
9140 ** to the internal list of statements to analyze. If successful, SQLITE_OK
9141 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9142 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9143 ** may be set to point to an English language error message. In this case
9144 ** the caller is responsible for eventually freeing the error message buffer
9145 ** using sqlite3_free().
9147 ** If an error does occur while processing one of the statements in the
9148 ** buffer passed as the second argument, none of the statements in the
9149 ** buffer are added to the analysis.
9151 ** This function must be called before sqlite3_expert_analyze(). If a call
9152 ** to this function is made on an sqlite3expert object that has already
9153 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
9154 ** immediately and no statements are added to the analysis.
9156 int sqlite3_expert_sql(
9157 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
9158 const char *zSql, /* SQL statement(s) to add */
9159 char **pzErr /* OUT: Error message (if any) */
9164 ** This function is called after the sqlite3expert object has been configured
9165 ** with all SQL statements using sqlite3_expert_sql() to actually perform
9166 ** the analysis. Once this function has been called, it is not possible to
9167 ** add further SQL statements to the analysis.
9169 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
9170 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
9171 ** point to a buffer containing an English language error message. In this
9172 ** case it is the responsibility of the caller to eventually free the buffer
9173 ** using sqlite3_free().
9175 ** If an error does occur within this function, the sqlite3expert object
9176 ** is no longer useful for any purpose. At that point it is no longer
9177 ** possible to add further SQL statements to the object or to re-attempt
9178 ** the analysis. The sqlite3expert object must still be freed using a call
9179 ** sqlite3_expert_destroy().
9181 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
9184 ** Return the total number of statements loaded using sqlite3_expert_sql().
9185 ** The total number of SQL statements may be different from the total number
9186 ** to calls to sqlite3_expert_sql().
9188 int sqlite3_expert_count(sqlite3expert*);
9191 ** Return a component of the report.
9193 ** This function is called after sqlite3_expert_analyze() to extract the
9194 ** results of the analysis. Each call to this function returns either a
9195 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9196 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
9197 ** #define constants defined below.
9199 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
9200 ** information relating to a specific SQL statement. In these cases that
9201 ** SQL statement is identified by the value passed as the second argument.
9202 ** SQL statements are numbered from 0 in the order in which they are parsed.
9203 ** If an out-of-range value (less than zero or equal to or greater than the
9204 ** value returned by sqlite3_expert_count()) is passed as the second argument
9205 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
9207 ** EXPERT_REPORT_SQL:
9208 ** Return the text of SQL statement iStmt.
9210 ** EXPERT_REPORT_INDEXES:
9211 ** Return a buffer containing the CREATE INDEX statements for all recommended
9212 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
9215 ** EXPERT_REPORT_PLAN:
9216 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
9217 ** iStmt after the proposed indexes have been added to the database schema.
9219 ** EXPERT_REPORT_CANDIDATES:
9220 ** Return a pointer to a buffer containing the CREATE INDEX statements
9221 ** for all indexes that were tested (for all SQL statements). The iStmt
9222 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
9224 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
9227 ** Values for the third argument passed to sqlite3_expert_report().
9229 #define EXPERT_REPORT_SQL 1
9230 #define EXPERT_REPORT_INDEXES 2
9231 #define EXPERT_REPORT_PLAN 3
9232 #define EXPERT_REPORT_CANDIDATES 4
9235 ** Free an (sqlite3expert*) handle and all associated resources. There
9236 ** should be one call to this function for each successful call to
9237 ** sqlite3-expert_new().
9239 void sqlite3_expert_destroy(sqlite3expert*);
9241 #endif /* !defined(SQLITEEXPERT_H) */
9243 /************************* End ../ext/expert/sqlite3expert.h ********************/
9244 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
9248 ** The author disclaims copyright to this source code. In place of
9249 ** a legal notice, here is a blessing:
9251 ** May you do good and not evil.
9252 ** May you find forgiveness for yourself and forgive others.
9253 ** May you share freely, never taking more than you give.
9255 *************************************************************************
9257 /* #include "sqlite3expert.h" */
9262 #if !defined(SQLITE_AMALGAMATION)
9263 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
9264 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
9266 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
9267 # define ALWAYS(X) (1)
9268 # define NEVER(X) (0)
9269 #elif !defined(NDEBUG)
9270 # define ALWAYS(X) ((X)?1:(assert(0),0))
9271 # define NEVER(X) ((X)?(assert(0),1):0)
9273 # define ALWAYS(X) (X)
9274 # define NEVER(X) (X)
9276 #endif /* !defined(SQLITE_AMALGAMATION) */
9279 #ifndef SQLITE_OMIT_VIRTUALTABLE
9281 /* typedef sqlite3_int64 i64; */
9282 /* typedef sqlite3_uint64 u64; */
9284 typedef struct IdxColumn IdxColumn;
9285 typedef struct IdxConstraint IdxConstraint;
9286 typedef struct IdxScan IdxScan;
9287 typedef struct IdxStatement IdxStatement;
9288 typedef struct IdxTable IdxTable;
9289 typedef struct IdxWrite IdxWrite;
9291 #define STRLEN (int)strlen
9294 ** A temp table name that we assume no user database will actually use.
9295 ** If this assumption proves incorrect triggers on the table with the
9296 ** conflicting name will be ignored.
9298 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
9301 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
9302 ** any other type of single-ended range constraint on a column).
9305 ** Used to temporarily link IdxConstraint objects into lists while
9306 ** creating candidate indexes.
9308 struct IdxConstraint {
9309 char *zColl; /* Collation sequence */
9310 int bRange; /* True for range, false for eq */
9311 int iCol; /* Constrained table column */
9312 int bFlag; /* Used by idxFindCompatible() */
9313 int bDesc; /* True if ORDER BY <expr> DESC */
9314 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
9315 IdxConstraint *pLink; /* See above */
9319 ** A single scan of a single table.
9322 IdxTable *pTab; /* Associated table object */
9323 int iDb; /* Database containing table zTable */
9324 i64 covering; /* Mask of columns required for cov. index */
9325 IdxConstraint *pOrder; /* ORDER BY columns */
9326 IdxConstraint *pEq; /* List of == constraints */
9327 IdxConstraint *pRange; /* List of < constraints */
9328 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
9332 ** Information regarding a single database table. Extracted from
9333 ** "PRAGMA table_info" by function idxGetTableInfo().
9342 char *zName; /* Table name */
9344 IdxTable *pNext; /* Next table in linked list of all tables */
9348 ** An object of the following type is created for each unique table/write-op
9349 ** seen. The objects are stored in a singly-linked list beginning at
9350 ** sqlite3expert.pWrite.
9354 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
9359 ** Each statement being analyzed is represented by an instance of this
9362 struct IdxStatement {
9363 int iId; /* Statement number */
9364 char *zSql; /* SQL statement */
9365 char *zIdx; /* Indexes */
9366 char *zEQP; /* Plan */
9367 IdxStatement *pNext;
9372 ** A hash table for storing strings. With space for a payload string
9373 ** with each entry. Methods are:
9380 #define IDX_HASH_SIZE 1023
9381 typedef struct IdxHashEntry IdxHashEntry;
9382 typedef struct IdxHash IdxHash;
9383 struct IdxHashEntry {
9384 char *zKey; /* nul-terminated key */
9385 char *zVal; /* nul-terminated value string */
9386 char *zVal2; /* nul-terminated value string 2 */
9387 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
9388 IdxHashEntry *pNext; /* Next entry in hash */
9391 IdxHashEntry *pFirst;
9392 IdxHashEntry *aHash[IDX_HASH_SIZE];
9396 ** sqlite3expert object.
9398 struct sqlite3expert {
9399 int iSample; /* Percentage of tables to sample for stat1 */
9400 sqlite3 *db; /* User database */
9401 sqlite3 *dbm; /* In-memory db for this analysis */
9402 sqlite3 *dbv; /* Vtab schema for this analysis */
9403 IdxTable *pTable; /* List of all IdxTable objects */
9404 IdxScan *pScan; /* List of scan objects */
9405 IdxWrite *pWrite; /* List of write objects */
9406 IdxStatement *pStatement; /* List of IdxStatement objects */
9407 int bRun; /* True once analysis has run */
9409 int rc; /* Error code from whereinfo hook */
9410 IdxHash hIdx; /* Hash containing all candidate indexes */
9411 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
9416 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
9417 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
9419 static void *idxMalloc(int *pRc, int nByte){
9421 assert( *pRc==SQLITE_OK );
9423 pRet = sqlite3_malloc(nByte);
9425 memset(pRet, 0, nByte);
9427 *pRc = SQLITE_NOMEM;
9433 ** Initialize an IdxHash hash table.
9435 static void idxHashInit(IdxHash *pHash){
9436 memset(pHash, 0, sizeof(IdxHash));
9440 ** Reset an IdxHash hash table.
9442 static void idxHashClear(IdxHash *pHash){
9444 for(i=0; i<IDX_HASH_SIZE; i++){
9445 IdxHashEntry *pEntry;
9446 IdxHashEntry *pNext;
9447 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
9448 pNext = pEntry->pHashNext;
9449 sqlite3_free(pEntry->zVal2);
9450 sqlite3_free(pEntry);
9453 memset(pHash, 0, sizeof(IdxHash));
9457 ** Return the index of the hash bucket that the string specified by the
9458 ** arguments to this function belongs.
9460 static int idxHashString(const char *z, int n){
9461 unsigned int ret = 0;
9464 ret += (ret<<3) + (unsigned char)(z[i]);
9466 return (int)(ret % IDX_HASH_SIZE);
9470 ** If zKey is already present in the hash table, return non-zero and do
9471 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
9472 ** the hash table passed as the second argument.
9474 static int idxHashAdd(
9480 int nKey = STRLEN(zKey);
9481 int iHash = idxHashString(zKey, nKey);
9482 int nVal = (zVal ? STRLEN(zVal) : 0);
9483 IdxHashEntry *pEntry;
9485 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9486 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9490 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
9492 pEntry->zKey = (char*)&pEntry[1];
9493 memcpy(pEntry->zKey, zKey, nKey);
9495 pEntry->zVal = &pEntry->zKey[nKey+1];
9496 memcpy(pEntry->zVal, zVal, nVal);
9498 pEntry->pHashNext = pHash->aHash[iHash];
9499 pHash->aHash[iHash] = pEntry;
9501 pEntry->pNext = pHash->pFirst;
9502 pHash->pFirst = pEntry;
9508 ** If zKey/nKey is present in the hash table, return a pointer to the
9509 ** hash-entry object.
9511 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
9513 IdxHashEntry *pEntry;
9514 if( nKey<0 ) nKey = STRLEN(zKey);
9515 iHash = idxHashString(zKey, nKey);
9517 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9518 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9526 ** If the hash table contains an entry with a key equal to the string
9527 ** passed as the final two arguments to this function, return a pointer
9528 ** to the payload string. Otherwise, if zKey/nKey is not present in the
9529 ** hash table, return NULL.
9531 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
9532 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
9533 if( pEntry ) return pEntry->zVal;
9538 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
9539 ** variable to point to a copy of nul-terminated string zColl.
9541 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
9542 IdxConstraint *pNew;
9543 int nColl = STRLEN(zColl);
9545 assert( *pRc==SQLITE_OK );
9546 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
9548 pNew->zColl = (char*)&pNew[1];
9549 memcpy(pNew->zColl, zColl, nColl+1);
9555 ** An error associated with database handle db has just occurred. Pass
9556 ** the error message to callback function xOut.
9558 static void idxDatabaseError(
9559 sqlite3 *db, /* Database handle */
9560 char **pzErrmsg /* Write error here */
9562 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
9566 ** Prepare an SQL statement.
9568 static int idxPrepareStmt(
9569 sqlite3 *db, /* Database handle to compile against */
9570 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
9571 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
9572 const char *zSql /* SQL statement to compile */
9574 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
9575 if( rc!=SQLITE_OK ){
9577 idxDatabaseError(db, pzErrmsg);
9583 ** Prepare an SQL statement using the results of a printf() formatting.
9585 static int idxPrintfPrepareStmt(
9586 sqlite3 *db, /* Database handle to compile against */
9587 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
9588 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
9589 const char *zFmt, /* printf() format of SQL statement */
9590 ... /* Trailing printf() arguments */
9596 zSql = sqlite3_vmprintf(zFmt, ap);
9600 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
9608 /*************************************************************************
9609 ** Beginning of virtual table implementation.
9611 typedef struct ExpertVtab ExpertVtab;
9615 sqlite3expert *pExpert;
9618 typedef struct ExpertCsr ExpertCsr;
9620 sqlite3_vtab_cursor base;
9621 sqlite3_stmt *pData;
9624 static char *expertDequote(const char *zIn){
9625 int n = STRLEN(zIn);
9626 char *zRet = sqlite3_malloc(n);
9628 assert( zIn[0]=='\'' );
9629 assert( zIn[n-1]=='\'' );
9634 for(iIn=1; iIn<(n-1); iIn++){
9635 if( zIn[iIn]=='\'' ){
9636 assert( zIn[iIn+1]=='\'' );
9639 zRet[iOut++] = zIn[iIn];
9648 ** This function is the implementation of both the xConnect and xCreate
9649 ** methods of the r-tree virtual table.
9651 ** argv[0] -> module name
9652 ** argv[1] -> database name
9653 ** argv[2] -> table name
9654 ** argv[...] -> column names...
9656 static int expertConnect(
9659 int argc, const char *const*argv,
9660 sqlite3_vtab **ppVtab,
9663 sqlite3expert *pExpert = (sqlite3expert*)pAux;
9668 *pzErr = sqlite3_mprintf("internal error!");
9671 char *zCreateTable = expertDequote(argv[3]);
9673 rc = sqlite3_declare_vtab(db, zCreateTable);
9674 if( rc==SQLITE_OK ){
9675 p = idxMalloc(&rc, sizeof(ExpertVtab));
9677 if( rc==SQLITE_OK ){
9678 p->pExpert = pExpert;
9679 p->pTab = pExpert->pTable;
9680 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
9682 sqlite3_free(zCreateTable);
9688 *ppVtab = (sqlite3_vtab*)p;
9692 static int expertDisconnect(sqlite3_vtab *pVtab){
9693 ExpertVtab *p = (ExpertVtab*)pVtab;
9698 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
9699 ExpertVtab *p = (ExpertVtab*)pVtab;
9704 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
9705 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
9706 SQLITE_INDEX_CONSTRAINT_LE;
9708 pScan = idxMalloc(&rc, sizeof(IdxScan));
9712 /* Link the new scan object into the list */
9713 pScan->pTab = p->pTab;
9714 pScan->pNextScan = p->pExpert->pScan;
9715 p->pExpert->pScan = pScan;
9717 /* Add the constraints to the IdxScan object */
9718 for(i=0; i<pIdxInfo->nConstraint; i++){
9719 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
9721 && pCons->iColumn>=0
9722 && p->pTab->aCol[pCons->iColumn].iPk==0
9723 && (pCons->op & opmask)
9725 IdxConstraint *pNew;
9726 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
9727 pNew = idxNewConstraint(&rc, zColl);
9729 pNew->iCol = pCons->iColumn;
9730 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9731 pNew->pNext = pScan->pEq;
9735 pNew->pNext = pScan->pRange;
9736 pScan->pRange = pNew;
9740 pIdxInfo->aConstraintUsage[i].argvIndex = n;
9744 /* Add the ORDER BY to the IdxScan object */
9745 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
9746 int iCol = pIdxInfo->aOrderBy[i].iColumn;
9748 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
9751 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
9752 pNew->pNext = pScan->pOrder;
9753 pNew->pLink = pScan->pOrder;
9754 pScan->pOrder = pNew;
9761 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
9765 static int expertUpdate(
9766 sqlite3_vtab *pVtab,
9768 sqlite3_value **azData,
9769 sqlite_int64 *pRowid
9779 ** Virtual table module xOpen method.
9781 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9785 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
9786 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
9791 ** Virtual table module xClose method.
9793 static int expertClose(sqlite3_vtab_cursor *cur){
9794 ExpertCsr *pCsr = (ExpertCsr*)cur;
9795 sqlite3_finalize(pCsr->pData);
9801 ** Virtual table module xEof method.
9803 ** Return non-zero if the cursor does not currently point to a valid
9804 ** record (i.e if the scan has finished), or zero otherwise.
9806 static int expertEof(sqlite3_vtab_cursor *cur){
9807 ExpertCsr *pCsr = (ExpertCsr*)cur;
9808 return pCsr->pData==0;
9812 ** Virtual table module xNext method.
9814 static int expertNext(sqlite3_vtab_cursor *cur){
9815 ExpertCsr *pCsr = (ExpertCsr*)cur;
9818 assert( pCsr->pData );
9819 rc = sqlite3_step(pCsr->pData);
9820 if( rc!=SQLITE_ROW ){
9821 rc = sqlite3_finalize(pCsr->pData);
9831 ** Virtual table module xRowid method.
9833 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
9840 ** Virtual table module xColumn method.
9842 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
9843 ExpertCsr *pCsr = (ExpertCsr*)cur;
9844 sqlite3_value *pVal;
9845 pVal = sqlite3_column_value(pCsr->pData, i);
9847 sqlite3_result_value(ctx, pVal);
9853 ** Virtual table module xFilter method.
9855 static int expertFilter(
9856 sqlite3_vtab_cursor *cur,
9857 int idxNum, const char *idxStr,
9858 int argc, sqlite3_value **argv
9860 ExpertCsr *pCsr = (ExpertCsr*)cur;
9861 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
9862 sqlite3expert *pExpert = pVtab->pExpert;
9869 rc = sqlite3_finalize(pCsr->pData);
9871 if( rc==SQLITE_OK ){
9872 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
9873 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
9877 if( rc==SQLITE_OK ){
9878 rc = expertNext(cur);
9883 static int idxRegisterVtab(sqlite3expert *p){
9884 static sqlite3_module expertModule = {
9886 expertConnect, /* xCreate - create a table */
9887 expertConnect, /* xConnect - connect to an existing table */
9888 expertBestIndex, /* xBestIndex - Determine search strategy */
9889 expertDisconnect, /* xDisconnect - Disconnect from a table */
9890 expertDisconnect, /* xDestroy - Drop a table */
9891 expertOpen, /* xOpen - open a cursor */
9892 expertClose, /* xClose - close a cursor */
9893 expertFilter, /* xFilter - configure scan constraints */
9894 expertNext, /* xNext - advance a cursor */
9895 expertEof, /* xEof */
9896 expertColumn, /* xColumn - read data */
9897 expertRowid, /* xRowid - read data */
9898 expertUpdate, /* xUpdate - write data */
9899 0, /* xBegin - begin transaction */
9900 0, /* xSync - sync transaction */
9901 0, /* xCommit - commit transaction */
9902 0, /* xRollback - rollback transaction */
9903 0, /* xFindFunction - function overloading */
9904 0, /* xRename - rename the table */
9907 0, /* xRollbackTo */
9908 0, /* xShadowName */
9911 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9914 ** End of virtual table implementation.
9915 *************************************************************************/
9917 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9918 ** is called, set it to the return value of sqlite3_finalize() before
9919 ** returning. Otherwise, discard the sqlite3_finalize() return value.
9921 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9922 int rc = sqlite3_finalize(pStmt);
9923 if( *pRc==SQLITE_OK ) *pRc = rc;
9927 ** Attempt to allocate an IdxTable structure corresponding to table zTab
9928 ** in the main database of connection db. If successful, set (*ppOut) to
9929 ** point to the new object and return SQLITE_OK. Otherwise, return an
9930 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9931 ** set to point to an error string.
9933 ** It is the responsibility of the caller to eventually free either the
9934 ** IdxTable object or error message using sqlite3_free().
9936 static int idxGetTableInfo(
9937 sqlite3 *db, /* Database connection to read details from */
9938 const char *zTab, /* Table name */
9939 IdxTable **ppOut, /* OUT: New object (if successful) */
9940 char **pzErrmsg /* OUT: Error message (if not) */
9942 sqlite3_stmt *p1 = 0;
9944 int nTab = STRLEN(zTab);
9945 int nByte = sizeof(IdxTable) + nTab + 1;
9951 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9952 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9953 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9954 const char *zColSeq = 0;
9955 nByte += 1 + STRLEN(zCol);
9956 rc = sqlite3_table_column_metadata(
9957 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9959 if( zColSeq==0 ) zColSeq = "binary";
9960 nByte += 1 + STRLEN(zColSeq);
9962 nPk += (sqlite3_column_int(p1, 5)>0);
9964 rc2 = sqlite3_reset(p1);
9965 if( rc==SQLITE_OK ) rc = rc2;
9967 nByte += sizeof(IdxColumn) * nCol;
9968 if( rc==SQLITE_OK ){
9969 pNew = idxMalloc(&rc, nByte);
9971 if( rc==SQLITE_OK ){
9972 pNew->aCol = (IdxColumn*)&pNew[1];
9974 pCsr = (char*)&pNew->aCol[nCol];
9978 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9979 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9980 const char *zColSeq = 0;
9981 int nCopy = STRLEN(zCol) + 1;
9982 pNew->aCol[nCol].zName = pCsr;
9983 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9984 memcpy(pCsr, zCol, nCopy);
9987 rc = sqlite3_table_column_metadata(
9988 db, "main", zTab, zCol, 0, &zColSeq, 0, 0, 0
9990 if( rc==SQLITE_OK ){
9991 if( zColSeq==0 ) zColSeq = "binary";
9992 nCopy = STRLEN(zColSeq) + 1;
9993 pNew->aCol[nCol].zColl = pCsr;
9994 memcpy(pCsr, zColSeq, nCopy);
10000 idxFinalize(&rc, p1);
10002 if( rc!=SQLITE_OK ){
10003 sqlite3_free(pNew);
10005 }else if( ALWAYS(pNew!=0) ){
10006 pNew->zName = pCsr;
10007 if( ALWAYS(pNew->zName!=0) ) memcpy(pNew->zName, zTab, nTab+1);
10015 ** This function is a no-op if *pRc is set to anything other than
10016 ** SQLITE_OK when it is called.
10018 ** If *pRc is initially set to SQLITE_OK, then the text specified by
10019 ** the printf() style arguments is appended to zIn and the result returned
10020 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
10021 ** zIn before returning.
10023 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
10027 int nIn = zIn ? STRLEN(zIn) : 0;
10029 va_start(ap, zFmt);
10030 if( *pRc==SQLITE_OK ){
10031 zAppend = sqlite3_vmprintf(zFmt, ap);
10033 nAppend = STRLEN(zAppend);
10034 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
10036 if( zAppend && zRet ){
10037 if( nIn ) memcpy(zRet, zIn, nIn);
10038 memcpy(&zRet[nIn], zAppend, nAppend+1);
10040 sqlite3_free(zRet);
10042 *pRc = SQLITE_NOMEM;
10044 sqlite3_free(zAppend);
10052 ** Return true if zId must be quoted in order to use it as an SQL
10053 ** identifier, or false otherwise.
10055 static int idxIdentifierRequiresQuotes(const char *zId){
10057 for(i=0; zId[i]; i++){
10059 && !(zId[i]>='0' && zId[i]<='9')
10060 && !(zId[i]>='a' && zId[i]<='z')
10061 && !(zId[i]>='A' && zId[i]<='Z')
10070 ** This function appends an index column definition suitable for constraint
10071 ** pCons to the string passed as zIn and returns the result.
10073 static char *idxAppendColDefn(
10074 int *pRc, /* IN/OUT: Error code */
10075 char *zIn, /* Column defn accumulated so far */
10076 IdxTable *pTab, /* Table index will be created on */
10077 IdxConstraint *pCons
10080 IdxColumn *p = &pTab->aCol[pCons->iCol];
10081 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
10083 if( idxIdentifierRequiresQuotes(p->zName) ){
10084 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
10086 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
10089 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
10090 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
10091 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
10093 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
10097 if( pCons->bDesc ){
10098 zRet = idxAppendText(pRc, zRet, " DESC");
10104 ** Search database dbm for an index compatible with the one idxCreateFromCons()
10105 ** would create from arguments pScan, pEq and pTail. If no error occurs and
10106 ** such an index is found, return non-zero. Or, if no such index is found,
10109 ** If an error occurs, set *pRc to an SQLite error code and return zero.
10111 static int idxFindCompatible(
10112 int *pRc, /* OUT: Error code */
10113 sqlite3* dbm, /* Database to search */
10114 IdxScan *pScan, /* Scan for table to search for index on */
10115 IdxConstraint *pEq, /* List of == constraints */
10116 IdxConstraint *pTail /* List of range constraints */
10118 const char *zTbl = pScan->pTab->zName;
10119 sqlite3_stmt *pIdxList = 0;
10120 IdxConstraint *pIter;
10121 int nEq = 0; /* Number of elements in pEq */
10124 /* Count the elements in list pEq */
10125 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
10127 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
10128 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10130 IdxConstraint *pT = pTail;
10131 sqlite3_stmt *pInfo = 0;
10132 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10134 /* Zero the IdxConstraint.bFlag values in the pEq list */
10135 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10137 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
10138 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
10139 int iIdx = sqlite3_column_int(pInfo, 0);
10140 int iCol = sqlite3_column_int(pInfo, 1);
10141 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
10144 for(pIter=pEq; pIter; pIter=pIter->pLink){
10145 if( pIter->bFlag ) continue;
10146 if( pIter->iCol!=iCol ) continue;
10147 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
10157 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
10165 idxFinalize(&rc, pInfo);
10167 if( rc==SQLITE_OK && bMatch ){
10168 sqlite3_finalize(pIdxList);
10172 idxFinalize(&rc, pIdxList);
10178 /* Callback for sqlite3_exec() with query with leading count(*) column.
10179 * The first argument is expected to be an int*, referent to be incremented
10180 * if that leading column is not exactly '0'.
10182 static int countNonzeros(void* pCount, int nc,
10183 char* azResults[], char* azColumns[]){
10184 (void)azColumns; /* Suppress unused parameter warning */
10185 if( nc>0 && (azResults[0][0]!='0' || azResults[0][1]!=0) ){
10186 *((int *)pCount) += 1;
10191 static int idxCreateFromCons(
10194 IdxConstraint *pEq,
10195 IdxConstraint *pTail
10197 sqlite3 *dbm = p->dbm;
10198 int rc = SQLITE_OK;
10199 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
10200 IdxTable *pTab = pScan->pTab;
10203 IdxConstraint *pCons;
10204 unsigned int h = 0;
10207 for(pCons=pEq; pCons; pCons=pCons->pLink){
10208 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10210 for(pCons=pTail; pCons; pCons=pCons->pLink){
10211 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10214 if( rc==SQLITE_OK ){
10215 /* Hash the list of columns to come up with a name for the index */
10216 const char *zTable = pScan->pTab->zName;
10217 int quoteTable = idxIdentifierRequiresQuotes(zTable);
10218 char *zName = 0; /* Index name */
10219 int collisions = 0;
10223 for(i=0; zCols[i]; i++){
10224 h += ((h<<3) + zCols[i]);
10226 sqlite3_free(zName);
10227 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
10228 if( zName==0 ) break;
10229 /* Is is unique among table, view and index names? */
10230 zFmt = "SELECT count(*) FROM sqlite_schema WHERE name=%Q"
10231 " AND type in ('index','table','view')";
10232 zFind = sqlite3_mprintf(zFmt, zName);
10234 rc = sqlite3_exec(dbm, zFind, countNonzeros, &i, 0);
10235 assert(rc==SQLITE_OK);
10236 sqlite3_free(zFind);
10242 }while( collisions<50 && zName!=0 );
10244 /* This return means "Gave up trying to find a unique index name." */
10245 rc = SQLITE_BUSY_TIMEOUT;
10246 }else if( zName==0 ){
10250 zFmt = "CREATE INDEX \"%w\" ON \"%w\"(%s)";
10252 zFmt = "CREATE INDEX %s ON %s(%s)";
10254 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
10258 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
10259 if( rc!=SQLITE_OK ){
10260 rc = SQLITE_BUSY_TIMEOUT;
10262 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
10265 sqlite3_free(zName);
10266 sqlite3_free(zIdx);
10270 sqlite3_free(zCols);
10276 ** Return true if list pList (linked by IdxConstraint.pLink) contains
10277 ** a constraint compatible with *p. Otherwise return false.
10279 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
10280 IdxConstraint *pCmp;
10281 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
10282 if( p->iCol==pCmp->iCol ) return 1;
10287 static int idxCreateFromWhere(
10289 IdxScan *pScan, /* Create indexes for this scan */
10290 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
10292 IdxConstraint *p1 = 0;
10293 IdxConstraint *pCon;
10296 /* Gather up all the == constraints. */
10297 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
10298 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10304 /* Create an index using the == constraints collected above. And the
10305 ** range constraint/ORDER BY terms passed in by the caller, if any. */
10306 rc = idxCreateFromCons(p, pScan, p1, pTail);
10308 /* If no range/ORDER BY passed by the caller, create a version of the
10309 ** index for each range constraint. */
10311 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
10312 assert( pCon->pLink==0 );
10313 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10314 rc = idxCreateFromCons(p, pScan, p1, pCon);
10323 ** Create candidate indexes in database [dbm] based on the data in
10324 ** linked-list pScan.
10326 static int idxCreateCandidates(sqlite3expert *p){
10327 int rc = SQLITE_OK;
10330 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
10331 rc = idxCreateFromWhere(p, pIter, 0);
10332 if( rc==SQLITE_OK && pIter->pOrder ){
10333 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
10341 ** Free all elements of the linked list starting at pConstraint.
10343 static void idxConstraintFree(IdxConstraint *pConstraint){
10344 IdxConstraint *pNext;
10347 for(p=pConstraint; p; p=pNext){
10354 ** Free all elements of the linked list starting from pScan up until pLast
10355 ** (pLast is not freed).
10357 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
10360 for(p=pScan; p!=pLast; p=pNext){
10361 pNext = p->pNextScan;
10362 idxConstraintFree(p->pOrder);
10363 idxConstraintFree(p->pEq);
10364 idxConstraintFree(p->pRange);
10370 ** Free all elements of the linked list starting from pStatement up
10371 ** until pLast (pLast is not freed).
10373 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
10375 IdxStatement *pNext;
10376 for(p=pStatement; p!=pLast; p=pNext){
10378 sqlite3_free(p->zEQP);
10379 sqlite3_free(p->zIdx);
10385 ** Free the linked list of IdxTable objects starting at pTab.
10387 static void idxTableFree(IdxTable *pTab){
10390 for(pIter=pTab; pIter; pIter=pNext){
10391 pNext = pIter->pNext;
10392 sqlite3_free(pIter);
10397 ** Free the linked list of IdxWrite objects starting at pTab.
10399 static void idxWriteFree(IdxWrite *pTab){
10402 for(pIter=pTab; pIter; pIter=pNext){
10403 pNext = pIter->pNext;
10404 sqlite3_free(pIter);
10411 ** This function is called after candidate indexes have been created. It
10412 ** runs all the queries to see which indexes they prefer, and populates
10413 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
10415 int idxFindIndexes(
10417 char **pzErr /* OUT: Error message (sqlite3_malloc) */
10419 IdxStatement *pStmt;
10420 sqlite3 *dbm = p->dbm;
10421 int rc = SQLITE_OK;
10424 idxHashInit(&hIdx);
10426 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
10427 IdxHashEntry *pEntry;
10428 sqlite3_stmt *pExplain = 0;
10429 idxHashClear(&hIdx);
10430 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
10431 "EXPLAIN QUERY PLAN %s", pStmt->zSql
10433 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
10434 /* int iId = sqlite3_column_int(pExplain, 0); */
10435 /* int iParent = sqlite3_column_int(pExplain, 1); */
10436 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
10437 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
10441 if( !zDetail ) continue;
10442 nDetail = STRLEN(zDetail);
10444 for(i=0; i<nDetail; i++){
10445 const char *zIdx = 0;
10446 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
10447 zIdx = &zDetail[i+13];
10448 }else if( i+22<nDetail
10449 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
10451 zIdx = &zDetail[i+22];
10456 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
10459 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
10461 idxHashAdd(&rc, &hIdx, zSql, 0);
10462 if( rc ) goto find_indexes_out;
10468 if( zDetail[0]!='-' ){
10469 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
10473 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10474 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
10477 idxFinalize(&rc, pExplain);
10481 idxHashClear(&hIdx);
10485 static int idxAuthCallback(
10491 const char *zTrigger
10493 int rc = SQLITE_OK;
10496 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
10497 if( sqlite3_stricmp(zDb, "main")==0 ){
10498 sqlite3expert *p = (sqlite3expert*)pCtx;
10500 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
10501 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
10505 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
10506 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
10509 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
10510 if( rc==SQLITE_OK ){
10511 pWrite->pTab = pTab;
10513 pWrite->pNext = p->pWrite;
10514 p->pWrite = pWrite;
10523 static int idxProcessOneTrigger(
10528 static const char *zInt = UNIQUE_TABLE_NAME;
10529 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
10530 IdxTable *pTab = pWrite->pTab;
10531 const char *zTab = pTab->zName;
10533 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
10534 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
10536 sqlite3_stmt *pSelect = 0;
10537 int rc = SQLITE_OK;
10540 /* Create the table and its triggers in the temp schema */
10541 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10542 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10543 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10544 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10546 idxFinalize(&rc, pSelect);
10548 /* Rename the table in the temp schema to zInt */
10549 if( rc==SQLITE_OK ){
10550 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
10554 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
10559 switch( pWrite->eOp ){
10560 case SQLITE_INSERT: {
10562 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
10563 for(i=0; i<pTab->nCol; i++){
10564 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
10566 zWrite = idxAppendText(&rc, zWrite, ")");
10569 case SQLITE_UPDATE: {
10571 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
10572 for(i=0; i<pTab->nCol; i++){
10573 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
10574 pTab->aCol[i].zName
10580 assert( pWrite->eOp==SQLITE_DELETE );
10581 if( rc==SQLITE_OK ){
10582 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
10583 if( zWrite==0 ) rc = SQLITE_NOMEM;
10588 if( rc==SQLITE_OK ){
10589 sqlite3_stmt *pX = 0;
10590 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
10591 idxFinalize(&rc, pX);
10592 if( rc!=SQLITE_OK ){
10593 idxDatabaseError(p->dbv, pzErr);
10596 sqlite3_free(zWrite);
10598 if( rc==SQLITE_OK ){
10599 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
10605 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
10606 int rc = SQLITE_OK;
10607 IdxWrite *pEnd = 0;
10608 IdxWrite *pFirst = p->pWrite;
10610 while( rc==SQLITE_OK && pFirst!=pEnd ){
10612 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
10613 rc = idxProcessOneTrigger(p, pIter, pzErr);
10616 pFirst = p->pWrite;
10623 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
10624 int rc = idxRegisterVtab(p);
10625 sqlite3_stmt *pSchema = 0;
10627 /* For each table in the main db schema:
10629 ** 1) Add an entry to the p->pTable list, and
10630 ** 2) Create the equivalent virtual table in dbv.
10632 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
10633 "SELECT type, name, sql, 1 FROM sqlite_schema "
10634 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
10636 "SELECT type, name, sql, 2 FROM sqlite_schema "
10637 "WHERE type = 'trigger'"
10638 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
10641 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10642 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10643 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10644 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10646 if( zType[0]=='v' || zType[1]=='r' ){
10647 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10650 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10651 if( rc==SQLITE_OK ){
10655 pTab->pNext = p->pTable;
10658 /* The statement the vtab will pass to sqlite3_declare_vtab() */
10659 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
10660 for(i=0; i<pTab->nCol; i++){
10661 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
10662 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
10665 zInner = idxAppendText(&rc, zInner, ")");
10667 /* The CVT statement to create the vtab */
10668 zOuter = idxAppendText(&rc, 0,
10669 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
10671 if( rc==SQLITE_OK ){
10672 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
10674 sqlite3_free(zInner);
10675 sqlite3_free(zOuter);
10679 idxFinalize(&rc, pSchema);
10683 struct IdxSampleCtx {
10685 double target; /* Target nRet/nRow value */
10686 double nRow; /* Number of rows seen */
10687 double nRet; /* Number of rows returned */
10690 static void idxSampleFunc(
10691 sqlite3_context *pCtx,
10693 sqlite3_value **argv
10695 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
10700 if( p->nRow==0.0 ){
10703 bRet = (p->nRet / p->nRow) <= p->target;
10705 unsigned short rnd;
10706 sqlite3_randomness(2, (void*)&rnd);
10707 bRet = ((int)rnd % 100) <= p->iTarget;
10711 sqlite3_result_int(pCtx, bRet);
10713 p->nRet += (double)bRet;
10718 struct IdxRemSlot {
10719 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
10720 i64 iVal; /* SQLITE_INTEGER value */
10721 double rVal; /* SQLITE_FLOAT value */
10722 int nByte; /* Bytes of space allocated at z */
10723 int n; /* Size of buffer z */
10724 char *z; /* SQLITE_TEXT/BLOB value */
10729 ** Implementation of scalar function rem().
10731 static void idxRemFunc(
10732 sqlite3_context *pCtx,
10734 sqlite3_value **argv
10736 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
10737 struct IdxRemSlot *pSlot;
10741 iSlot = sqlite3_value_int(argv[0]);
10742 assert( iSlot<=p->nSlot );
10743 pSlot = &p->aSlot[iSlot];
10745 switch( pSlot->eType ){
10750 case SQLITE_INTEGER:
10751 sqlite3_result_int64(pCtx, pSlot->iVal);
10755 sqlite3_result_double(pCtx, pSlot->rVal);
10759 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10763 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10767 pSlot->eType = sqlite3_value_type(argv[1]);
10768 switch( pSlot->eType ){
10773 case SQLITE_INTEGER:
10774 pSlot->iVal = sqlite3_value_int64(argv[1]);
10778 pSlot->rVal = sqlite3_value_double(argv[1]);
10782 case SQLITE_TEXT: {
10783 int nByte = sqlite3_value_bytes(argv[1]);
10784 if( nByte>pSlot->nByte ){
10785 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10787 sqlite3_result_error_nomem(pCtx);
10790 pSlot->nByte = nByte*2;
10794 if( pSlot->eType==SQLITE_BLOB ){
10795 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
10797 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
10804 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
10805 int rc = SQLITE_OK;
10807 "SELECT max(i.seqno) FROM "
10808 " sqlite_schema AS s, "
10809 " pragma_index_list(s.name) AS l, "
10810 " pragma_index_info(l.name) AS i "
10811 "WHERE s.type = 'table'";
10812 sqlite3_stmt *pMax = 0;
10815 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
10816 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
10817 *pnMax = sqlite3_column_int(pMax, 0) + 1;
10819 idxFinalize(&rc, pMax);
10824 static int idxPopulateOneStat1(
10826 sqlite3_stmt *pIndexXInfo,
10827 sqlite3_stmt *pWriteStat,
10837 sqlite3_stmt *pQuery = 0;
10839 int rc = SQLITE_OK;
10841 assert( p->iSample>0 );
10843 /* Formulate the query text */
10844 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
10845 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
10846 const char *zComma = zCols==0 ? "" : ", ";
10847 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
10848 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
10849 zCols = idxAppendText(&rc, zCols,
10850 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
10852 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
10854 sqlite3_reset(pIndexXInfo);
10855 if( rc==SQLITE_OK ){
10856 if( p->iSample==100 ){
10857 zQuery = sqlite3_mprintf(
10858 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
10861 zQuery = sqlite3_mprintf(
10862 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
10866 sqlite3_free(zCols);
10867 sqlite3_free(zOrder);
10869 /* Formulate the query text */
10870 if( rc==SQLITE_OK ){
10871 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10872 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
10874 sqlite3_free(zQuery);
10876 if( rc==SQLITE_OK ){
10877 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
10879 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10880 IdxHashEntry *pEntry;
10882 for(i=0; i<=nCol; i++) aStat[i] = 1;
10883 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10885 for(i=0; i<nCol; i++){
10886 if( sqlite3_column_int(pQuery, i)==0 ) break;
10888 for(/*no-op*/; i<nCol; i++){
10893 if( rc==SQLITE_OK ){
10895 zStat = sqlite3_mprintf("%d", s0);
10896 if( zStat==0 ) rc = SQLITE_NOMEM;
10897 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
10898 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
10902 if( rc==SQLITE_OK ){
10903 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
10904 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
10905 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
10906 sqlite3_step(pWriteStat);
10907 rc = sqlite3_reset(pWriteStat);
10910 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
10912 assert( pEntry->zVal2==0 );
10913 pEntry->zVal2 = zStat;
10915 sqlite3_free(zStat);
10918 sqlite3_free(aStat);
10919 idxFinalize(&rc, pQuery);
10924 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10928 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10929 if( rc!=SQLITE_OK ) return rc;
10931 zSql = sqlite3_mprintf(
10932 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10934 if( zSql==0 ) return SQLITE_NOMEM;
10935 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10936 sqlite3_free(zSql);
10942 ** This function is called as part of sqlite3_expert_analyze(). Candidate
10943 ** indexes have already been created in database sqlite3expert.dbm, this
10944 ** function populates sqlite_stat1 table in the same database.
10946 ** The stat1 data is generated by querying the
10948 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10949 int rc = SQLITE_OK;
10951 struct IdxRemCtx *pCtx = 0;
10952 struct IdxSampleCtx samplectx;
10954 i64 iPrev = -100000;
10955 sqlite3_stmt *pAllIndex = 0;
10956 sqlite3_stmt *pIndexXInfo = 0;
10957 sqlite3_stmt *pWrite = 0;
10959 const char *zAllIndex =
10960 "SELECT s.rowid, s.name, l.name FROM "
10961 " sqlite_schema AS s, "
10962 " pragma_index_list(s.name) AS l "
10963 "WHERE s.type = 'table'";
10964 const char *zIndexXInfo =
10965 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10966 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10968 /* If iSample==0, no sqlite_stat1 data is required. */
10969 if( p->iSample==0 ) return SQLITE_OK;
10971 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10972 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10974 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10976 if( rc==SQLITE_OK ){
10977 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10978 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10981 if( rc==SQLITE_OK ){
10982 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10983 rc = sqlite3_create_function(
10984 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10987 if( rc==SQLITE_OK ){
10988 rc = sqlite3_create_function(
10989 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10993 if( rc==SQLITE_OK ){
10994 pCtx->nSlot = nMax+1;
10995 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10997 if( rc==SQLITE_OK ){
10998 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
11000 if( rc==SQLITE_OK ){
11001 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
11004 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
11005 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
11006 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
11007 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
11008 if( p->iSample<100 && iPrev!=iRowid ){
11009 samplectx.target = (double)p->iSample / 100.0;
11010 samplectx.iTarget = p->iSample;
11011 samplectx.nRow = 0.0;
11012 samplectx.nRet = 0.0;
11013 rc = idxBuildSampleTable(p, zTab);
11014 if( rc!=SQLITE_OK ) break;
11016 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
11019 if( rc==SQLITE_OK && p->iSample<100 ){
11020 rc = sqlite3_exec(p->dbv,
11021 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
11025 idxFinalize(&rc, pAllIndex);
11026 idxFinalize(&rc, pIndexXInfo);
11027 idxFinalize(&rc, pWrite);
11030 for(i=0; i<pCtx->nSlot; i++){
11031 sqlite3_free(pCtx->aSlot[i].z);
11033 sqlite3_free(pCtx);
11036 if( rc==SQLITE_OK ){
11037 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
11040 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
11045 ** Allocate a new sqlite3expert object.
11047 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
11048 int rc = SQLITE_OK;
11049 sqlite3expert *pNew;
11051 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
11053 /* Open two in-memory databases to work with. The "vtab database" (dbv)
11054 ** will contain a virtual table corresponding to each real table in
11055 ** the user database schema, and a copy of each view. It is used to
11056 ** collect information regarding the WHERE, ORDER BY and other clauses
11057 ** of the user's query.
11059 if( rc==SQLITE_OK ){
11061 pNew->iSample = 100;
11062 rc = sqlite3_open(":memory:", &pNew->dbv);
11064 if( rc==SQLITE_OK ){
11065 rc = sqlite3_open(":memory:", &pNew->dbm);
11066 if( rc==SQLITE_OK ){
11067 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
11072 /* Copy the entire schema of database [db] into [dbm]. */
11073 if( rc==SQLITE_OK ){
11074 sqlite3_stmt *pSql;
11075 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
11076 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
11077 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
11079 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
11080 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
11081 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
11083 idxFinalize(&rc, pSql);
11086 /* Create the vtab schema */
11087 if( rc==SQLITE_OK ){
11088 rc = idxCreateVtabSchema(pNew, pzErrmsg);
11091 /* Register the auth callback with dbv */
11092 if( rc==SQLITE_OK ){
11093 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
11096 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
11097 ** return the new sqlite3expert handle. */
11098 if( rc!=SQLITE_OK ){
11099 sqlite3_expert_destroy(pNew);
11106 ** Configure an sqlite3expert object.
11108 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
11109 int rc = SQLITE_OK;
11113 case EXPERT_CONFIG_SAMPLE: {
11114 int iVal = va_arg(ap, int);
11115 if( iVal<0 ) iVal = 0;
11116 if( iVal>100 ) iVal = 100;
11121 rc = SQLITE_NOTFOUND;
11130 ** Add an SQL statement to the analysis.
11132 int sqlite3_expert_sql(
11133 sqlite3expert *p, /* From sqlite3_expert_new() */
11134 const char *zSql, /* SQL statement to add */
11135 char **pzErr /* OUT: Error message (if any) */
11137 IdxScan *pScanOrig = p->pScan;
11138 IdxStatement *pStmtOrig = p->pStatement;
11139 int rc = SQLITE_OK;
11140 const char *zStmt = zSql;
11142 if( p->bRun ) return SQLITE_MISUSE;
11144 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
11145 sqlite3_stmt *pStmt = 0;
11146 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
11147 if( rc==SQLITE_OK ){
11149 IdxStatement *pNew;
11150 const char *z = sqlite3_sql(pStmt);
11152 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
11153 if( rc==SQLITE_OK ){
11154 pNew->zSql = (char*)&pNew[1];
11155 memcpy(pNew->zSql, z, n+1);
11156 pNew->pNext = p->pStatement;
11157 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
11158 p->pStatement = pNew;
11160 sqlite3_finalize(pStmt);
11163 idxDatabaseError(p->dbv, pzErr);
11167 if( rc!=SQLITE_OK ){
11168 idxScanFree(p->pScan, pScanOrig);
11169 idxStatementFree(p->pStatement, pStmtOrig);
11170 p->pScan = pScanOrig;
11171 p->pStatement = pStmtOrig;
11177 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
11179 IdxHashEntry *pEntry;
11181 /* Do trigger processing to collect any extra IdxScan structures */
11182 rc = idxProcessTriggers(p, pzErr);
11184 /* Create candidate indexes within the in-memory database file */
11185 if( rc==SQLITE_OK ){
11186 rc = idxCreateCandidates(p);
11187 }else if ( rc==SQLITE_BUSY_TIMEOUT ){
11189 *pzErr = sqlite3_mprintf("Cannot find a unique index name to propose.");
11193 /* Generate the stat1 data */
11194 if( rc==SQLITE_OK ){
11195 rc = idxPopulateStat1(p, pzErr);
11198 /* Formulate the EXPERT_REPORT_CANDIDATES text */
11199 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
11200 p->zCandidates = idxAppendText(&rc, p->zCandidates,
11201 "%s;%s%s\n", pEntry->zVal,
11202 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
11206 /* Figure out which of the candidate indexes are preferred by the query
11207 ** planner and report the results to the user. */
11208 if( rc==SQLITE_OK ){
11209 rc = idxFindIndexes(p, pzErr);
11212 if( rc==SQLITE_OK ){
11219 ** Return the total number of statements that have been added to this
11220 ** sqlite3expert using sqlite3_expert_sql().
11222 int sqlite3_expert_count(sqlite3expert *p){
11224 if( p->pStatement ) nRet = p->pStatement->iId+1;
11229 ** Return a component of the report.
11231 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
11232 const char *zRet = 0;
11233 IdxStatement *pStmt;
11235 if( p->bRun==0 ) return 0;
11236 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
11238 case EXPERT_REPORT_SQL:
11239 if( pStmt ) zRet = pStmt->zSql;
11241 case EXPERT_REPORT_INDEXES:
11242 if( pStmt ) zRet = pStmt->zIdx;
11244 case EXPERT_REPORT_PLAN:
11245 if( pStmt ) zRet = pStmt->zEQP;
11247 case EXPERT_REPORT_CANDIDATES:
11248 zRet = p->zCandidates;
11255 ** Free an sqlite3expert object.
11257 void sqlite3_expert_destroy(sqlite3expert *p){
11259 sqlite3_close(p->dbm);
11260 sqlite3_close(p->dbv);
11261 idxScanFree(p->pScan, 0);
11262 idxStatementFree(p->pStatement, 0);
11263 idxTableFree(p->pTable);
11264 idxWriteFree(p->pWrite);
11265 idxHashClear(&p->hIdx);
11266 sqlite3_free(p->zCandidates);
11271 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11273 /************************* End ../ext/expert/sqlite3expert.c ********************/
11275 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11276 /************************* Begin ../ext/misc/dbdata.c ******************/
11280 ** The author disclaims copyright to this source code. In place of
11281 ** a legal notice, here is a blessing:
11283 ** May you do good and not evil.
11284 ** May you find forgiveness for yourself and forgive others.
11285 ** May you share freely, never taking more than you give.
11287 ******************************************************************************
11289 ** This file contains an implementation of two eponymous virtual tables,
11290 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
11291 ** "sqlite_dbpage" eponymous virtual table be available.
11294 ** sqlite_dbdata is used to extract data directly from a database b-tree
11295 ** page and its associated overflow pages, bypassing the b-tree layer.
11296 ** The table schema is equivalent to:
11298 ** CREATE TABLE sqlite_dbdata(
11303 ** schema TEXT HIDDEN
11306 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
11307 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11310 ** Each page of the database is inspected. If it cannot be interpreted as
11311 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
11312 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
11313 ** table contains one row for each field in the record associated with
11314 ** each cell on the page. For intkey b-trees, the key value is stored in
11317 ** For example, for the database:
11319 ** CREATE TABLE t1(a, b); -- root page is page 2
11320 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
11321 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
11323 ** the sqlite_dbdata table contains, as well as from entries related to
11324 ** page 1, content equivalent to:
11326 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
11328 ** (2, 0, 0, 'v' ),
11329 ** (2, 0, 1, 'five'),
11330 ** (2, 1, -1, 10 ),
11331 ** (2, 1, 0, 'x' ),
11332 ** (2, 1, 1, 'ten' );
11334 ** If database corruption is encountered, this module does not report an
11335 ** error. Instead, it attempts to extract as much data as possible and
11336 ** ignores the corruption.
11339 ** The sqlite_dbptr table has the following schema:
11341 ** CREATE TABLE sqlite_dbptr(
11344 ** schema TEXT HIDDEN
11347 ** It contains one entry for each b-tree pointer between a parent and
11348 ** child page in the database.
11350 #if !defined(SQLITEINT_H)
11351 /* #include "sqlite3ext.h" */
11353 /* typedef unsigned char u8; */
11356 SQLITE_EXTENSION_INIT1
11357 #include <string.h>
11358 #include <assert.h>
11360 #define DBDATA_PADDING_BYTES 100
11362 typedef struct DbdataTable DbdataTable;
11363 typedef struct DbdataCursor DbdataCursor;
11365 /* Cursor object */
11366 struct DbdataCursor {
11367 sqlite3_vtab_cursor base; /* Base class. Must be first */
11368 sqlite3_stmt *pStmt; /* For fetching database pages */
11370 int iPgno; /* Current page number */
11371 u8 *aPage; /* Buffer containing page */
11372 int nPage; /* Size of aPage[] in bytes */
11373 int nCell; /* Number of cells on aPage[] */
11374 int iCell; /* Current cell number */
11375 int bOnePage; /* True to stop after one page */
11377 sqlite3_int64 iRowid;
11379 /* Only for the sqlite_dbdata table */
11380 u8 *pRec; /* Buffer containing current record */
11381 int nRec; /* Size of pRec[] in bytes */
11382 int nHdr; /* Size of header in bytes */
11383 int iField; /* Current field number */
11387 sqlite3_int64 iIntkey; /* Integer key value */
11391 struct DbdataTable {
11392 sqlite3_vtab base; /* Base class. Must be first */
11393 sqlite3 *db; /* The database connection */
11394 sqlite3_stmt *pStmt; /* For fetching database pages */
11395 int bPtr; /* True for sqlite3_dbptr table */
11398 /* Column and schema definitions for sqlite_dbdata */
11399 #define DBDATA_COLUMN_PGNO 0
11400 #define DBDATA_COLUMN_CELL 1
11401 #define DBDATA_COLUMN_FIELD 2
11402 #define DBDATA_COLUMN_VALUE 3
11403 #define DBDATA_COLUMN_SCHEMA 4
11404 #define DBDATA_SCHEMA \
11405 "CREATE TABLE x(" \
11408 " field INTEGER," \
11410 " schema TEXT HIDDEN" \
11413 /* Column and schema definitions for sqlite_dbptr */
11414 #define DBPTR_COLUMN_PGNO 0
11415 #define DBPTR_COLUMN_CHILD 1
11416 #define DBPTR_COLUMN_SCHEMA 2
11417 #define DBPTR_SCHEMA \
11418 "CREATE TABLE x(" \
11420 " child INTEGER," \
11421 " schema TEXT HIDDEN" \
11425 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
11428 static int dbdataConnect(
11431 int argc, const char *const*argv,
11432 sqlite3_vtab **ppVtab,
11435 DbdataTable *pTab = 0;
11436 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
11438 if( rc==SQLITE_OK ){
11439 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
11443 memset(pTab, 0, sizeof(DbdataTable));
11445 pTab->bPtr = (pAux!=0);
11449 *ppVtab = (sqlite3_vtab*)pTab;
11454 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
11456 static int dbdataDisconnect(sqlite3_vtab *pVtab){
11457 DbdataTable *pTab = (DbdataTable*)pVtab;
11459 sqlite3_finalize(pTab->pStmt);
11460 sqlite3_free(pVtab);
11466 ** This function interprets two types of constraints:
11471 ** If neither are present, idxNum is set to 0. If schema=? is present,
11472 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
11473 ** in idxNum is set.
11475 ** If both parameters are present, schema is in position 0 and pgno in
11478 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
11479 DbdataTable *pTab = (DbdataTable*)tab;
11483 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
11485 for(i=0; i<pIdx->nConstraint; i++){
11486 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
11487 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
11488 if( p->iColumn==colSchema ){
11489 if( p->usable==0 ) return SQLITE_CONSTRAINT;
11492 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
11499 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
11500 pIdx->aConstraintUsage[iSchema].omit = 1;
11503 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
11504 pIdx->aConstraintUsage[iPgno].omit = 1;
11505 pIdx->estimatedCost = 100;
11506 pIdx->estimatedRows = 50;
11508 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
11509 int iCol = pIdx->aOrderBy[0].iColumn;
11510 if( pIdx->nOrderBy==1 ){
11511 pIdx->orderByConsumed = (iCol==0 || iCol==1);
11512 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
11513 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
11518 pIdx->estimatedCost = 100000000;
11519 pIdx->estimatedRows = 1000000000;
11521 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
11526 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
11528 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
11529 DbdataCursor *pCsr;
11531 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
11533 return SQLITE_NOMEM;
11535 memset(pCsr, 0, sizeof(DbdataCursor));
11536 pCsr->base.pVtab = pVTab;
11539 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
11544 ** Restore a cursor object to the state it was in when first allocated
11545 ** by dbdataOpen().
11547 static void dbdataResetCursor(DbdataCursor *pCsr){
11548 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
11549 if( pTab->pStmt==0 ){
11550 pTab->pStmt = pCsr->pStmt;
11552 sqlite3_finalize(pCsr->pStmt);
11558 pCsr->bOnePage = 0;
11559 sqlite3_free(pCsr->aPage);
11560 sqlite3_free(pCsr->pRec);
11566 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
11568 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
11569 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11570 dbdataResetCursor(pCsr);
11571 sqlite3_free(pCsr);
11576 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11578 static unsigned int get_uint16(unsigned char *a){
11579 return (a[0]<<8)|a[1];
11581 static unsigned int get_uint32(unsigned char *a){
11582 return ((unsigned int)a[0]<<24)
11583 | ((unsigned int)a[1]<<16)
11584 | ((unsigned int)a[2]<<8)
11585 | ((unsigned int)a[3]);
11589 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11590 ** If successful, set (*ppPage) to point to a buffer containing the page
11591 ** data, (*pnPage) to the size of that buffer in bytes and return
11592 ** SQLITE_OK. In this case it is the responsibility of the caller to
11593 ** eventually free the buffer using sqlite3_free().
11595 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11596 ** return an SQLite error code.
11598 static int dbdataLoadPage(
11599 DbdataCursor *pCsr, /* Cursor object */
11600 unsigned int pgno, /* Page number of page to load */
11601 u8 **ppPage, /* OUT: pointer to page buffer */
11602 int *pnPage /* OUT: Size of (*ppPage) in bytes */
11605 int rc = SQLITE_OK;
11606 sqlite3_stmt *pStmt = pCsr->pStmt;
11610 sqlite3_bind_int64(pStmt, 2, pgno);
11611 if( SQLITE_ROW==sqlite3_step(pStmt) ){
11612 int nCopy = sqlite3_column_bytes(pStmt, 0);
11615 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11619 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11620 memcpy(pPage, pCopy, nCopy);
11621 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11627 rc2 = sqlite3_reset(pStmt);
11628 if( rc==SQLITE_OK ) rc = rc2;
11634 ** Read a varint. Put the value in *pVal and return the number of bytes.
11636 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11637 sqlite3_int64 v = 0;
11639 for(i=0; i<8; i++){
11640 v = (v<<7) + (z[i]&0x7f);
11641 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11643 v = (v<<8) + (z[i]&0xff);
11649 ** Return the number of bytes of space used by an SQLite value of type
11652 static int dbdataValueBytes(int eType){
11654 case 0: case 8: case 9:
11672 return ((eType-12) / 2);
11679 ** Load a value of type eType from buffer pData and use it to set the
11680 ** result of context object pCtx.
11682 static void dbdataValue(
11683 sqlite3_context *pCtx,
11688 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
11693 sqlite3_result_null(pCtx);
11697 sqlite3_result_int(pCtx, 0);
11700 sqlite3_result_int(pCtx, 1);
11703 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
11704 sqlite3_uint64 v = (signed char)pData[0];
11708 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
11709 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
11710 case 4: v = (v<<8) + pData[0]; pData++;
11711 case 3: v = (v<<8) + pData[0]; pData++;
11712 case 2: v = (v<<8) + pData[0]; pData++;
11717 memcpy(&r, &v, sizeof(r));
11718 sqlite3_result_double(pCtx, r);
11720 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
11726 int n = ((eType-12) / 2);
11728 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11730 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11738 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
11740 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
11741 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11742 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11747 int iOff = (pCsr->iPgno==1 ? 100 : 0);
11750 if( pCsr->aPage==0 ){
11752 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11753 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11754 if( rc!=SQLITE_OK ) return rc;
11755 if( pCsr->aPage ) break;
11758 pCsr->iCell = pTab->bPtr ? -2 : 0;
11759 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11763 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
11764 pCsr->iCell = pCsr->nCell;
11767 if( pCsr->iCell>=pCsr->nCell ){
11768 sqlite3_free(pCsr->aPage);
11770 if( pCsr->bOnePage ) return SQLITE_OK;
11776 /* If there is no record loaded, load it now. */
11777 if( pCsr->pRec==0 ){
11780 sqlite3_int64 nPayload = 0;
11781 sqlite3_int64 nHdr = 0;
11786 switch( pCsr->aPage[iOff] ){
11796 /* This is not a b-tree page with records on it. Continue. */
11797 pCsr->iCell = pCsr->nCell;
11801 if( pCsr->iCell>=pCsr->nCell ){
11805 iOff += 8 + nPointer + pCsr->iCell*2;
11806 if( iOff>pCsr->nPage ){
11809 iOff = get_uint16(&pCsr->aPage[iOff]);
11812 /* For an interior node cell, skip past the child-page number */
11815 /* Load the "byte of payload including overflow" field */
11816 if( bNextPage || iOff>pCsr->nPage ){
11819 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11822 /* If this is a leaf intkey cell, load the rowid */
11823 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11824 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
11827 /* Figure out how much data to read from the local page */
11832 X = ((U-12)*64/255)-23;
11838 M = ((U-12)*32/255)-23;
11839 K = M+((nPayload-M)%(U-4));
11847 if( bNextPage || nLocal+iOff>pCsr->nPage ){
11851 /* Allocate space for payload. And a bit more to catch small buffer
11852 ** overruns caused by attempting to read a varint or similar from
11853 ** near the end of a corrupt record. */
11854 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
11855 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
11856 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
11857 pCsr->nRec = nPayload;
11859 /* Load the nLocal bytes of payload */
11860 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
11863 /* Load content from overflow pages */
11864 if( nPayload>nLocal ){
11865 sqlite3_int64 nRem = nPayload - nLocal;
11866 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11871 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
11872 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
11873 if( rc!=SQLITE_OK ) return rc;
11874 if( aOvfl==0 ) break;
11877 if( nCopy>nRem ) nCopy = nRem;
11878 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
11881 pgnoOvfl = get_uint32(aOvfl);
11882 sqlite3_free(aOvfl);
11886 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
11888 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
11889 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
11890 pCsr->iField = (bHasRowid ? -1 : 0);
11895 if( pCsr->iField>0 ){
11896 sqlite3_int64 iType;
11897 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
11900 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
11901 pCsr->pPtr += dbdataValueBytes(iType);
11907 sqlite3_free(pCsr->aPage);
11908 sqlite3_free(pCsr->pRec);
11911 if( pCsr->bOnePage ) return SQLITE_OK;
11914 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
11918 /* Advance to the next cell. The next iteration of the loop will load
11919 ** the record and so on. */
11920 sqlite3_free(pCsr->pRec);
11927 assert( !"can't get here" );
11932 ** Return true if the cursor is at EOF.
11934 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11935 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11936 return pCsr->aPage==0;
11940 ** Determine the size in pages of database zSchema (where zSchema is
11941 ** "main", "temp" or the name of an attached database) and set
11942 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11943 ** an SQLite error code.
11945 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11946 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11949 sqlite3_stmt *pStmt = 0;
11951 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11952 if( zSql==0 ) return SQLITE_NOMEM;
11953 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11954 sqlite3_free(zSql);
11955 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11956 pCsr->szDb = sqlite3_column_int(pStmt, 0);
11958 rc2 = sqlite3_finalize(pStmt);
11959 if( rc==SQLITE_OK ) rc = rc2;
11964 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11966 static int dbdataFilter(
11967 sqlite3_vtab_cursor *pCursor,
11968 int idxNum, const char *idxStr,
11969 int argc, sqlite3_value **argv
11971 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11972 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11973 int rc = SQLITE_OK;
11974 const char *zSchema = "main";
11976 dbdataResetCursor(pCsr);
11977 assert( pCsr->iPgno==1 );
11978 if( idxNum & 0x01 ){
11979 zSchema = (const char*)sqlite3_value_text(argv[0]);
11981 if( idxNum & 0x02 ){
11982 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11983 pCsr->bOnePage = 1;
11985 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11986 rc = dbdataDbsize(pCsr, zSchema);
11989 if( rc==SQLITE_OK ){
11991 pCsr->pStmt = pTab->pStmt;
11994 rc = sqlite3_prepare_v2(pTab->db,
11995 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
12000 if( rc==SQLITE_OK ){
12001 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
12003 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
12005 if( rc==SQLITE_OK ){
12006 rc = dbdataNext(pCursor);
12012 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
12014 static int dbdataColumn(
12015 sqlite3_vtab_cursor *pCursor,
12016 sqlite3_context *ctx,
12019 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12020 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
12023 case DBPTR_COLUMN_PGNO:
12024 sqlite3_result_int64(ctx, pCsr->iPgno);
12026 case DBPTR_COLUMN_CHILD: {
12027 int iOff = pCsr->iPgno==1 ? 100 : 0;
12028 if( pCsr->iCell<0 ){
12031 iOff += 12 + pCsr->iCell*2;
12032 if( iOff>pCsr->nPage ) return SQLITE_OK;
12033 iOff = get_uint16(&pCsr->aPage[iOff]);
12035 if( iOff<=pCsr->nPage ){
12036 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
12043 case DBDATA_COLUMN_PGNO:
12044 sqlite3_result_int64(ctx, pCsr->iPgno);
12046 case DBDATA_COLUMN_CELL:
12047 sqlite3_result_int(ctx, pCsr->iCell);
12049 case DBDATA_COLUMN_FIELD:
12050 sqlite3_result_int(ctx, pCsr->iField);
12052 case DBDATA_COLUMN_VALUE: {
12053 if( pCsr->iField<0 ){
12054 sqlite3_result_int64(ctx, pCsr->iIntkey);
12056 sqlite3_int64 iType;
12057 dbdataGetVarint(pCsr->pHdrPtr, &iType);
12059 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
12070 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
12072 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
12073 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
12074 *pRowid = pCsr->iRowid;
12080 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
12082 static int sqlite3DbdataRegister(sqlite3 *db){
12083 static sqlite3_module dbdata_module = {
12086 dbdataConnect, /* xConnect */
12087 dbdataBestIndex, /* xBestIndex */
12088 dbdataDisconnect, /* xDisconnect */
12090 dbdataOpen, /* xOpen - open a cursor */
12091 dbdataClose, /* xClose - close a cursor */
12092 dbdataFilter, /* xFilter - configure scan constraints */
12093 dbdataNext, /* xNext - advance a cursor */
12094 dbdataEof, /* xEof - check for end of scan */
12095 dbdataColumn, /* xColumn - read data */
12096 dbdataRowid, /* xRowid - read data */
12102 0, /* xFindMethod */
12104 0, /* xSavepoint */
12106 0, /* xRollbackTo */
12107 0 /* xShadowName */
12110 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
12111 if( rc==SQLITE_OK ){
12112 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
12120 int sqlite3_dbdata_init(
12123 const sqlite3_api_routines *pApi
12125 SQLITE_EXTENSION_INIT2(pApi);
12126 return sqlite3DbdataRegister(db);
12129 /************************* End ../ext/misc/dbdata.c ********************/
12132 #if defined(SQLITE_ENABLE_SESSION)
12134 ** State information for a single open session
12136 typedef struct OpenSession OpenSession;
12137 struct OpenSession {
12138 char *zName; /* Symbolic name for this session */
12139 int nFilter; /* Number of xFilter rejection GLOB patterns */
12140 char **azFilter; /* Array of xFilter rejection GLOB patterns */
12141 sqlite3_session *p; /* The open session */
12145 typedef struct ExpertInfo ExpertInfo;
12146 struct ExpertInfo {
12147 sqlite3expert *pExpert;
12151 /* A single line in the EQP output */
12152 typedef struct EQPGraphRow EQPGraphRow;
12153 struct EQPGraphRow {
12154 int iEqpId; /* ID for this row */
12155 int iParentId; /* ID of the parent row */
12156 EQPGraphRow *pNext; /* Next row in sequence */
12157 char zText[1]; /* Text to display for this row */
12160 /* All EQP output is collected into an instance of the following */
12161 typedef struct EQPGraph EQPGraph;
12163 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
12164 EQPGraphRow *pLast; /* Last element of the pRow list */
12165 char zPrefix[100]; /* Graph prefix */
12169 ** State information about the database connection is contained in an
12170 ** instance of the following structure.
12172 typedef struct ShellState ShellState;
12173 struct ShellState {
12174 sqlite3 *db; /* The database */
12175 u8 autoExplain; /* Automatically turn on .explain mode */
12176 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
12177 u8 autoEQPtest; /* autoEQP is in test mode */
12178 u8 autoEQPtrace; /* autoEQP is in trace mode */
12179 u8 scanstatsOn; /* True to display scan stats before each finalize */
12180 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
12181 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
12182 u8 nEqpLevel; /* Depth of the EQP output graph */
12183 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
12184 u8 bSafeMode; /* True to prohibit unsafe operations */
12185 u8 bSafeModePersist; /* The long-term value of bSafeMode */
12186 unsigned statsOn; /* True to display memory stats before each finalize */
12187 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
12188 int outCount; /* Revert to stdout when reaching zero */
12189 int cnt; /* Number of records displayed so far */
12190 int lineno; /* Line number of last line read from in */
12191 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
12192 FILE *in; /* Read commands from this stream */
12193 FILE *out; /* Write results here */
12194 FILE *traceOut; /* Output for sqlite3_trace() */
12195 int nErr; /* Number of errors seen */
12196 int mode; /* An output mode setting */
12197 int modePrior; /* Saved mode */
12198 int cMode; /* temporary output mode for the current query */
12199 int normalMode; /* Output mode before ".explain on" */
12200 int writableSchema; /* True if PRAGMA writable_schema=ON */
12201 int showHeader; /* True to show column names in List or Column mode */
12202 int nCheck; /* Number of ".check" commands run */
12203 unsigned nProgress; /* Number of progress callbacks encountered */
12204 unsigned mxProgress; /* Maximum progress callbacks before failing */
12205 unsigned flgProgress; /* Flags for the progress callback */
12206 unsigned shellFlgs; /* Various flags */
12207 unsigned priorShFlgs; /* Saved copy of flags */
12208 sqlite3_int64 szMax; /* --maxsize argument to .open */
12209 char *zDestTable; /* Name of destination table when MODE_Insert */
12210 char *zTempFile; /* Temporary file that might need deleting */
12211 char zTestcase[30]; /* Name of current test case */
12212 char colSeparator[20]; /* Column separator character for several modes */
12213 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
12214 char colSepPrior[20]; /* Saved column separator */
12215 char rowSepPrior[20]; /* Saved row separator */
12216 int *colWidth; /* Requested width of each column in columnar modes */
12217 int *actualWidth; /* Actual width of each column */
12218 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
12219 char nullValue[20]; /* The text to print when a NULL comes back from
12221 char outfile[FILENAME_MAX]; /* Filename for *out */
12222 sqlite3_stmt *pStmt; /* Current statement if any. */
12223 FILE *pLog; /* Write log output here */
12224 struct AuxDb { /* Storage space for auxiliary database connections */
12225 sqlite3 *db; /* Connection pointer */
12226 const char *zDbFilename; /* Filename used to open the connection */
12227 char *zFreeOnClose; /* Free this memory allocation on close */
12228 #if defined(SQLITE_ENABLE_SESSION)
12229 int nSession; /* Number of active sessions */
12230 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
12232 } aAuxDb[5], /* Array of all database connections */
12233 *pAuxDb; /* Currently active database connection */
12234 int *aiIndent; /* Array of indents used in MODE_Explain */
12235 int nIndent; /* Size of array aiIndent[] */
12236 int iIndent; /* Index of current op in aiIndent[] */
12237 char *zNonce; /* Nonce for temporary safe-mode excapes */
12238 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12239 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12243 /* Allowed values for ShellState.autoEQP
12245 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
12246 #define AUTOEQP_on 1 /* Automatic EQP is on */
12247 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
12248 #define AUTOEQP_full 3 /* Show full EXPLAIN */
12250 /* Allowed values for ShellState.openMode
12252 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
12253 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
12254 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
12255 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
12256 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
12257 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
12258 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
12260 /* Allowed values for ShellState.eTraceType
12262 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
12263 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
12264 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
12266 /* Bits in the ShellState.flgProgress variable */
12267 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
12268 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
12269 ** callback limit is reached, and for each
12270 ** top-level SQL statement */
12271 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
12274 ** These are the allowed shellFlgs values
12276 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
12277 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
12278 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
12279 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
12280 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
12281 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
12282 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
12283 #define SHFLG_HeaderSet 0x00000080 /* showHeader has been specified */
12284 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
12285 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
12288 ** Macros for testing and setting shellFlgs
12290 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
12291 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
12292 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
12295 ** These are the allowed modes.
12297 #define MODE_Line 0 /* One column per line. Blank line between records */
12298 #define MODE_Column 1 /* One record per line in neat columns */
12299 #define MODE_List 2 /* One record per line with a separator */
12300 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
12301 #define MODE_Html 4 /* Generate an XHTML table */
12302 #define MODE_Insert 5 /* Generate SQL "insert" statements */
12303 #define MODE_Quote 6 /* Quote values as for SQL */
12304 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
12305 #define MODE_Csv 8 /* Quote strings, numbers are plain */
12306 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
12307 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
12308 #define MODE_Pretty 11 /* Pretty-print schemas */
12309 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
12310 #define MODE_Json 13 /* Output JSON */
12311 #define MODE_Markdown 14 /* Markdown formatting */
12312 #define MODE_Table 15 /* MySQL-style table formatting */
12313 #define MODE_Box 16 /* Unicode box-drawing characters */
12315 static const char *modeDescr[] = {
12336 ** These are the column/row/line separators used by the various
12337 ** import/export modes.
12339 #define SEP_Column "|"
12340 #define SEP_Row "\n"
12341 #define SEP_Tab "\t"
12342 #define SEP_Space " "
12343 #define SEP_Comma ","
12344 #define SEP_CrLf "\r\n"
12345 #define SEP_Unit "\x1F"
12346 #define SEP_Record "\x1E"
12349 ** A callback for the sqlite3_log() interface.
12351 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
12352 ShellState *p = (ShellState*)pArg;
12353 if( p->pLog==0 ) return;
12354 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
12359 ** SQL function: shell_putsnl(X)
12361 ** Write the text X to the screen (or whatever output is being directed)
12362 ** adding a newline at the end, and then return X.
12364 static void shellPutsFunc(
12365 sqlite3_context *pCtx,
12367 sqlite3_value **apVal
12369 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
12371 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
12372 sqlite3_result_value(pCtx, apVal[0]);
12376 ** If in safe mode, print an error message described by the arguments
12377 ** and exit immediately.
12379 static void failIfSafeMode(
12381 const char *zErrMsg,
12384 if( p->bSafeMode ){
12387 va_start(ap, zErrMsg);
12388 zMsg = sqlite3_vmprintf(zErrMsg, ap);
12390 raw_printf(stderr, "line %d: ", p->lineno);
12391 utf8_printf(stderr, "%s\n", zMsg);
12397 ** SQL function: edit(VALUE)
12398 ** edit(VALUE,EDITOR)
12402 ** (1) Write VALUE into a temporary file.
12403 ** (2) Run program EDITOR on that temporary file.
12404 ** (3) Read the temporary file back and return its content as the result.
12405 ** (4) Delete the temporary file
12407 ** If the EDITOR argument is omitted, use the value in the VISUAL
12408 ** environment variable. If still there is no EDITOR, through an error.
12410 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12412 #ifndef SQLITE_NOHAVE_SYSTEM
12413 static void editFunc(
12414 sqlite3_context *context,
12416 sqlite3_value **argv
12418 const char *zEditor;
12419 char *zTempFile = 0;
12428 unsigned char *p = 0;
12431 zEditor = (const char*)sqlite3_value_text(argv[1]);
12433 zEditor = getenv("VISUAL");
12436 sqlite3_result_error(context, "no editor for edit()", -1);
12439 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
12440 sqlite3_result_error(context, "NULL input to edit()", -1);
12443 db = sqlite3_context_db_handle(context);
12445 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
12446 if( zTempFile==0 ){
12447 sqlite3_uint64 r = 0;
12448 sqlite3_randomness(sizeof(r), &r);
12449 zTempFile = sqlite3_mprintf("temp%llx", r);
12450 if( zTempFile==0 ){
12451 sqlite3_result_error_nomem(context);
12455 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
12456 /* When writing the file to be edited, do \n to \r\n conversions on systems
12457 ** that want \r\n line endings */
12458 f = fopen(zTempFile, bBin ? "wb" : "w");
12460 sqlite3_result_error(context, "edit() cannot open temp file", -1);
12461 goto edit_func_end;
12463 sz = sqlite3_value_bytes(argv[0]);
12465 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
12467 const char *z = (const char*)sqlite3_value_text(argv[0]);
12468 /* Remember whether or not the value originally contained \r\n */
12469 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
12470 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
12475 sqlite3_result_error(context, "edit() could not write the whole file", -1);
12476 goto edit_func_end;
12478 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
12480 sqlite3_result_error_nomem(context);
12481 goto edit_func_end;
12484 sqlite3_free(zCmd);
12486 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
12487 goto edit_func_end;
12489 f = fopen(zTempFile, "rb");
12491 sqlite3_result_error(context,
12492 "edit() cannot reopen temp file after edit", -1);
12493 goto edit_func_end;
12495 fseek(f, 0, SEEK_END);
12498 p = sqlite3_malloc64( sz+1 );
12500 sqlite3_result_error_nomem(context);
12501 goto edit_func_end;
12503 x = fread(p, 1, (size_t)sz, f);
12507 sqlite3_result_error(context, "could not read back the whole file", -1);
12508 goto edit_func_end;
12511 sqlite3_result_blob64(context, p, sz, sqlite3_free);
12513 sqlite3_int64 i, j;
12515 /* If the original contains \r\n then do no conversions back to \n */
12517 /* If the file did not originally contain \r\n then convert any new
12518 ** \r\n back into \n */
12519 for(i=j=0; i<sz; i++){
12520 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
12526 sqlite3_result_text64(context, (const char*)p, sz,
12527 sqlite3_free, SQLITE_UTF8);
12534 sqlite3_free(zTempFile);
12537 #endif /* SQLITE_NOHAVE_SYSTEM */
12540 ** Save or restore the current output mode
12542 static void outputModePush(ShellState *p){
12543 p->modePrior = p->mode;
12544 p->priorShFlgs = p->shellFlgs;
12545 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
12546 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
12548 static void outputModePop(ShellState *p){
12549 p->mode = p->modePrior;
12550 p->shellFlgs = p->priorShFlgs;
12551 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
12552 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
12556 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12558 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12560 char *zBlob = (char *)pBlob;
12561 raw_printf(out,"X'");
12562 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12563 raw_printf(out,"'");
12567 ** Find a string that is not found anywhere in z[]. Return a pointer
12570 ** Try to use zA and zB first. If both of those are already found in z[]
12571 ** then make up some string and store it in the buffer zBuf.
12573 static const char *unused_string(
12574 const char *z, /* Result must not appear anywhere in z */
12575 const char *zA, const char *zB, /* Try these first */
12576 char *zBuf /* Space to store a generated string */
12579 if( strstr(z, zA)==0 ) return zA;
12580 if( strstr(z, zB)==0 ) return zB;
12582 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
12583 }while( strstr(z,zBuf)!=0 );
12588 ** Output the given string as a quoted string using SQL quoting conventions.
12590 ** See also: output_quoted_escaped_string()
12592 static void output_quoted_string(FILE *out, const char *z){
12595 setBinaryMode(out, 1);
12596 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12598 utf8_printf(out,"'%s'",z);
12600 raw_printf(out, "'");
12602 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12605 utf8_printf(out, "%.*s", i, z);
12609 raw_printf(out, "'");
12617 raw_printf(out, "'");
12619 setTextMode(out, 1);
12623 ** Output the given string as a quoted string using SQL quoting conventions.
12624 ** Additionallly , escape the "\n" and "\r" characters so that they do not
12625 ** get corrupted by end-of-line translation facilities in some operating
12628 ** This is like output_quoted_string() but with the addition of the \r\n
12629 ** escape mechanism.
12631 static void output_quoted_escaped_string(FILE *out, const char *z){
12634 setBinaryMode(out, 1);
12635 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
12637 utf8_printf(out,"'%s'",z);
12639 const char *zNL = 0;
12640 const char *zCR = 0;
12643 char zBuf1[20], zBuf2[20];
12644 for(i=0; z[i]; i++){
12645 if( z[i]=='\n' ) nNL++;
12646 if( z[i]=='\r' ) nCR++;
12649 raw_printf(out, "replace(");
12650 zNL = unused_string(z, "\\n", "\\012", zBuf1);
12653 raw_printf(out, "replace(");
12654 zCR = unused_string(z, "\\r", "\\015", zBuf2);
12656 raw_printf(out, "'");
12658 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
12661 utf8_printf(out, "%.*s", i, z);
12665 raw_printf(out, "'");
12673 raw_printf(out, "%s", zNL);
12676 raw_printf(out, "%s", zCR);
12678 raw_printf(out, "'");
12680 raw_printf(out, ",'%s',char(13))", zCR);
12683 raw_printf(out, ",'%s',char(10))", zNL);
12686 setTextMode(out, 1);
12690 ** Output the given string as a quoted according to C or TCL quoting rules.
12692 static void output_c_string(FILE *out, const char *z){
12695 while( (c = *(z++))!=0 ){
12699 }else if( c=='"' ){
12702 }else if( c=='\t' ){
12705 }else if( c=='\n' ){
12708 }else if( c=='\r' ){
12711 }else if( !isprint(c&0xff) ){
12712 raw_printf(out, "\\%03o", c&0xff);
12721 ** Output the given string as a quoted according to JSON quoting rules.
12723 static void output_json_string(FILE *out, const char *z, int n){
12725 if( n<0 ) n = (int)strlen(z);
12729 if( c=='\\' || c=='"' ){
12732 }else if( c<=0x1f ){
12736 }else if( c=='\f' ){
12738 }else if( c=='\n' ){
12740 }else if( c=='\r' ){
12742 }else if( c=='\t' ){
12745 raw_printf(out, "u%04x",c);
12755 ** Output the given string with characters that are special to
12758 static void output_html_string(FILE *out, const char *z){
12770 utf8_printf(out,"%.*s",i,z);
12773 raw_printf(out,"<");
12774 }else if( z[i]=='&' ){
12775 raw_printf(out,"&");
12776 }else if( z[i]=='>' ){
12777 raw_printf(out,">");
12778 }else if( z[i]=='\"' ){
12779 raw_printf(out,""");
12780 }else if( z[i]=='\'' ){
12781 raw_printf(out,"'");
12790 ** If a field contains any character identified by a 1 in the following
12791 ** array, then the string must be quoted for CSV.
12793 static const char needCsvQuote[] = {
12794 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12795 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12796 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
12797 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12798 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12799 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12800 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12801 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
12802 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12803 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12804 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12805 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12806 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12807 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12808 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12809 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12813 ** Output a single term of CSV. Actually, p->colSeparator is used for
12814 ** the separator, which may or may not be a comma. p->nullValue is
12815 ** the null value. Strings are quoted if necessary. The separator
12816 ** is only issued if bSep is true.
12818 static void output_csv(ShellState *p, const char *z, int bSep){
12819 FILE *out = p->out;
12821 utf8_printf(out,"%s",p->nullValue);
12824 for(i=0; z[i]; i++){
12825 if( needCsvQuote[((unsigned char*)z)[i]] ){
12830 if( i==0 || strstr(z, p->colSeparator)!=0 ){
12831 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12832 utf8_printf(out, "%s", zQuoted);
12833 sqlite3_free(zQuoted);
12835 utf8_printf(out, "%s", z);
12839 utf8_printf(p->out, "%s", p->colSeparator);
12844 ** This routine runs when the user presses Ctrl-C
12846 static void interrupt_handler(int NotUsed){
12847 UNUSED_PARAMETER(NotUsed);
12849 if( seenInterrupt>2 ) exit(1);
12850 if( globalDb ) sqlite3_interrupt(globalDb);
12853 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12855 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12857 static BOOL WINAPI ConsoleCtrlHandler(
12858 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
12860 if( dwCtrlType==CTRL_C_EVENT ){
12861 interrupt_handler(0);
12868 #ifndef SQLITE_OMIT_AUTHORIZATION
12870 ** This authorizer runs in safe mode.
12872 static int safeModeAuth(
12880 ShellState *p = (ShellState*)pClientData;
12881 static const char *azProhibitedFunctions[] = {
12890 UNUSED_PARAMETER(zA2);
12891 UNUSED_PARAMETER(zA3);
12892 UNUSED_PARAMETER(zA4);
12894 case SQLITE_ATTACH: {
12895 failIfSafeMode(p, "cannot run ATTACH in safe mode");
12898 case SQLITE_FUNCTION: {
12900 for(i=0; i<ArraySize(azProhibitedFunctions); i++){
12901 if( sqlite3_stricmp(zA1, azProhibitedFunctions[i])==0 ){
12902 failIfSafeMode(p, "cannot use the %s() function in safe mode",
12903 azProhibitedFunctions[i]);
12913 ** When the ".auth ON" is set, the following authorizer callback is
12914 ** invoked. It always returns SQLITE_OK.
12916 static int shellAuth(
12924 ShellState *p = (ShellState*)pClientData;
12925 static const char *azAction[] = { 0,
12926 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
12927 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
12928 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
12929 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
12930 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
12931 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
12932 "PRAGMA", "READ", "SELECT",
12933 "TRANSACTION", "UPDATE", "ATTACH",
12934 "DETACH", "ALTER_TABLE", "REINDEX",
12935 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
12936 "FUNCTION", "SAVEPOINT", "RECURSIVE"
12944 utf8_printf(p->out, "authorizer: %s", azAction[op]);
12945 for(i=0; i<4; i++){
12946 raw_printf(p->out, " ");
12948 output_c_string(p->out, az[i]);
12950 raw_printf(p->out, "NULL");
12953 raw_printf(p->out, "\n");
12954 if( p->bSafeMode ) (void)safeModeAuth(pClientData, op, zA1, zA2, zA3, zA4);
12960 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
12962 ** This routine converts some CREATE TABLE statements for shadow tables
12963 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
12965 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
12967 if( zTail==0 ) return;
12968 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
12969 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
12971 utf8_printf(out, "%s%s", z, zTail);
12974 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
12977 printSchemaLine(out, z, zTail);
12982 ** Return true if string z[] has nothing but whitespace and comments to the
12983 ** end of the first line.
12985 static int wsToEol(const char *z){
12987 for(i=0; z[i]; i++){
12988 if( z[i]=='\n' ) return 1;
12989 if( IsSpace(z[i]) ) continue;
12990 if( z[i]=='-' && z[i+1]=='-' ) return 1;
12997 ** Add a new entry to the EXPLAIN QUERY PLAN data
12999 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
13001 int nText = strlen30(zText);
13002 if( p->autoEQPtest ){
13003 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
13005 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
13006 if( pNew==0 ) shell_out_of_memory();
13007 pNew->iEqpId = iEqpId;
13008 pNew->iParentId = p2;
13009 memcpy(pNew->zText, zText, nText+1);
13011 if( p->sGraph.pLast ){
13012 p->sGraph.pLast->pNext = pNew;
13014 p->sGraph.pRow = pNew;
13016 p->sGraph.pLast = pNew;
13020 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
13023 static void eqp_reset(ShellState *p){
13024 EQPGraphRow *pRow, *pNext;
13025 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
13026 pNext = pRow->pNext;
13027 sqlite3_free(pRow);
13029 memset(&p->sGraph, 0, sizeof(p->sGraph));
13032 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
13033 ** pOld, or return the first such line if pOld is NULL
13035 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
13036 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
13037 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
13041 /* Render a single level of the graph that has iEqpId as its parent. Called
13042 ** recursively to render sublevels.
13044 static void eqp_render_level(ShellState *p, int iEqpId){
13045 EQPGraphRow *pRow, *pNext;
13046 int n = strlen30(p->sGraph.zPrefix);
13048 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
13049 pNext = eqp_next_row(p, iEqpId, pRow);
13051 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
13052 pNext ? "|--" : "`--", z);
13053 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
13054 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
13055 eqp_render_level(p, pRow->iEqpId);
13056 p->sGraph.zPrefix[n] = 0;
13062 ** Display and reset the EXPLAIN QUERY PLAN data
13064 static void eqp_render(ShellState *p){
13065 EQPGraphRow *pRow = p->sGraph.pRow;
13067 if( pRow->zText[0]=='-' ){
13068 if( pRow->pNext==0 ){
13072 utf8_printf(p->out, "%s\n", pRow->zText+3);
13073 p->sGraph.pRow = pRow->pNext;
13074 sqlite3_free(pRow);
13076 utf8_printf(p->out, "QUERY PLAN\n");
13078 p->sGraph.zPrefix[0] = 0;
13079 eqp_render_level(p, 0);
13084 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
13086 ** Progress handler callback.
13088 static int progress_handler(void *pClientData) {
13089 ShellState *p = (ShellState*)pClientData;
13091 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
13092 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
13093 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
13094 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
13097 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
13098 raw_printf(p->out, "Progress %u\n", p->nProgress);
13102 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
13107 static void print_dashes(FILE *out, int N){
13108 const char zDash[] = "--------------------------------------------------";
13109 const int nDash = sizeof(zDash) - 1;
13114 raw_printf(out, "%.*s", N, zDash);
13118 ** Print a markdown or table-style row separator using ascii-art
13120 static void print_row_separator(
13127 fputs(zSep, p->out);
13128 print_dashes(p->out, p->actualWidth[0]+2);
13129 for(i=1; i<nArg; i++){
13130 fputs(zSep, p->out);
13131 print_dashes(p->out, p->actualWidth[i]+2);
13133 fputs(zSep, p->out);
13135 fputs("\n", p->out);
13139 ** This is the callback routine that the shell
13140 ** invokes for each row of a query result.
13142 static int shell_callback(
13144 int nArg, /* Number of result columns */
13145 char **azArg, /* Text of each result column */
13146 char **azCol, /* Column names */
13147 int *aiType /* Column types. Might be NULL */
13150 ShellState *p = (ShellState*)pArg;
13152 if( azArg==0 ) return 0;
13153 switch( p->cMode ){
13156 if( azArg==0 ) break;
13157 for(i=0; i<nArg; i++){
13158 int len = strlen30(azCol[i] ? azCol[i] : "");
13159 if( len>w ) w = len;
13161 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
13162 for(i=0; i<nArg; i++){
13163 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
13164 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
13168 case MODE_Explain: {
13169 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
13170 if( nArg>ArraySize(aExplainWidth) ){
13171 nArg = ArraySize(aExplainWidth);
13174 for(i=0; i<nArg; i++){
13175 int w = aExplainWidth[i];
13176 utf8_width_print(p->out, w, azCol[i]);
13177 fputs(i==nArg-1 ? "\n" : " ", p->out);
13179 for(i=0; i<nArg; i++){
13180 int w = aExplainWidth[i];
13181 print_dashes(p->out, w);
13182 fputs(i==nArg-1 ? "\n" : " ", p->out);
13185 if( azArg==0 ) break;
13186 for(i=0; i<nArg; i++){
13187 int w = aExplainWidth[i];
13188 if( i==nArg-1 ) w = 0;
13189 if( azArg[i] && strlenChar(azArg[i])>w ){
13190 w = strlenChar(azArg[i]);
13192 if( i==1 && p->aiIndent && p->pStmt ){
13193 if( p->iIndent<p->nIndent ){
13194 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
13198 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
13199 fputs(i==nArg-1 ? "\n" : " ", p->out);
13203 case MODE_Semi: { /* .schema and .fullschema output */
13204 printSchemaLine(p->out, azArg[0], ";\n");
13207 case MODE_Pretty: { /* .schema and .fullschema with --indent */
13215 if( azArg[0]==0 ) break;
13216 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
13217 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
13219 utf8_printf(p->out, "%s;\n", azArg[0]);
13222 z = sqlite3_mprintf("%s", azArg[0]);
13224 for(i=0; IsSpace(z[i]); i++){}
13225 for(; (c = z[i])!=0; i++){
13227 if( z[j-1]=='\r' ) z[j-1] = '\n';
13228 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
13229 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
13234 while( j>0 && IsSpace(z[j-1]) ){ j--; }
13236 if( strlen30(z)>=79 ){
13237 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
13240 }else if( c=='"' || c=='\'' || c=='`' ){
13242 }else if( c=='[' ){
13244 }else if( c=='-' && z[i+1]=='-' ){
13246 }else if( c=='(' ){
13248 }else if( c==')' ){
13250 if( nLine>0 && nParen==0 && j>0 ){
13251 printSchemaLineN(p->out, z, j, "\n");
13256 if( nParen==1 && cEnd==0
13257 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
13260 printSchemaLineN(p->out, z, j, "\n ");
13263 while( IsSpace(z[i+1]) ){ i++; }
13268 printSchemaLine(p->out, z, ";\n");
13273 if( p->cnt++==0 && p->showHeader ){
13274 for(i=0; i<nArg; i++){
13275 utf8_printf(p->out,"%s%s",azCol[i],
13276 i==nArg-1 ? p->rowSeparator : p->colSeparator);
13279 if( azArg==0 ) break;
13280 for(i=0; i<nArg; i++){
13281 char *z = azArg[i];
13282 if( z==0 ) z = p->nullValue;
13283 utf8_printf(p->out, "%s", z);
13285 utf8_printf(p->out, "%s", p->colSeparator);
13287 utf8_printf(p->out, "%s", p->rowSeparator);
13293 if( p->cnt++==0 && p->showHeader ){
13294 raw_printf(p->out,"<TR>");
13295 for(i=0; i<nArg; i++){
13296 raw_printf(p->out,"<TH>");
13297 output_html_string(p->out, azCol[i]);
13298 raw_printf(p->out,"</TH>\n");
13300 raw_printf(p->out,"</TR>\n");
13302 if( azArg==0 ) break;
13303 raw_printf(p->out,"<TR>");
13304 for(i=0; i<nArg; i++){
13305 raw_printf(p->out,"<TD>");
13306 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13307 raw_printf(p->out,"</TD>\n");
13309 raw_printf(p->out,"</TR>\n");
13313 if( p->cnt++==0 && p->showHeader ){
13314 for(i=0; i<nArg; i++){
13315 output_c_string(p->out,azCol[i] ? azCol[i] : "");
13316 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13318 utf8_printf(p->out, "%s", p->rowSeparator);
13320 if( azArg==0 ) break;
13321 for(i=0; i<nArg; i++){
13322 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13323 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13325 utf8_printf(p->out, "%s", p->rowSeparator);
13329 setBinaryMode(p->out, 1);
13330 if( p->cnt++==0 && p->showHeader ){
13331 for(i=0; i<nArg; i++){
13332 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
13334 utf8_printf(p->out, "%s", p->rowSeparator);
13337 for(i=0; i<nArg; i++){
13338 output_csv(p, azArg[i], i<nArg-1);
13340 utf8_printf(p->out, "%s", p->rowSeparator);
13342 setTextMode(p->out, 1);
13345 case MODE_Insert: {
13346 if( azArg==0 ) break;
13347 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
13348 if( p->showHeader ){
13349 raw_printf(p->out,"(");
13350 for(i=0; i<nArg; i++){
13351 if( i>0 ) raw_printf(p->out, ",");
13352 if( quoteChar(azCol[i]) ){
13353 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13354 utf8_printf(p->out, "%s", z);
13357 raw_printf(p->out, "%s", azCol[i]);
13360 raw_printf(p->out,")");
13363 for(i=0; i<nArg; i++){
13364 raw_printf(p->out, i>0 ? "," : " VALUES(");
13365 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13366 utf8_printf(p->out,"NULL");
13367 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13368 if( ShellHasFlag(p, SHFLG_Newlines) ){
13369 output_quoted_string(p->out, azArg[i]);
13371 output_quoted_escaped_string(p->out, azArg[i]);
13373 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13374 utf8_printf(p->out,"%s", azArg[i]);
13375 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13377 double r = sqlite3_column_double(p->pStmt, i);
13379 memcpy(&ur,&r,sizeof(r));
13380 if( ur==0x7ff0000000000000LL ){
13381 raw_printf(p->out, "1e999");
13382 }else if( ur==0xfff0000000000000LL ){
13383 raw_printf(p->out, "-1e999");
13385 sqlite3_snprintf(50,z,"%!.20g", r);
13386 raw_printf(p->out, "%s", z);
13388 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13389 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13390 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13391 output_hex_blob(p->out, pBlob, nBlob);
13392 }else if( isNumber(azArg[i], 0) ){
13393 utf8_printf(p->out,"%s", azArg[i]);
13394 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
13395 output_quoted_string(p->out, azArg[i]);
13397 output_quoted_escaped_string(p->out, azArg[i]);
13400 raw_printf(p->out,");\n");
13404 if( azArg==0 ) break;
13406 fputs("[{", p->out);
13408 fputs(",\n{", p->out);
13411 for(i=0; i<nArg; i++){
13412 output_json_string(p->out, azCol[i], -1);
13414 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13415 fputs("null",p->out);
13416 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13418 double r = sqlite3_column_double(p->pStmt, i);
13420 memcpy(&ur,&r,sizeof(r));
13421 if( ur==0x7ff0000000000000LL ){
13422 raw_printf(p->out, "1e999");
13423 }else if( ur==0xfff0000000000000LL ){
13424 raw_printf(p->out, "-1e999");
13426 sqlite3_snprintf(50,z,"%!.20g", r);
13427 raw_printf(p->out, "%s", z);
13429 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13430 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13431 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13432 output_json_string(p->out, pBlob, nBlob);
13433 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13434 output_json_string(p->out, azArg[i], -1);
13436 utf8_printf(p->out,"%s", azArg[i]);
13446 if( azArg==0 ) break;
13447 if( p->cnt==0 && p->showHeader ){
13448 for(i=0; i<nArg; i++){
13449 if( i>0 ) fputs(p->colSeparator, p->out);
13450 output_quoted_string(p->out, azCol[i]);
13452 fputs(p->rowSeparator, p->out);
13455 for(i=0; i<nArg; i++){
13456 if( i>0 ) fputs(p->colSeparator, p->out);
13457 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13458 utf8_printf(p->out,"NULL");
13459 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13460 output_quoted_string(p->out, azArg[i]);
13461 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13462 utf8_printf(p->out,"%s", azArg[i]);
13463 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13465 double r = sqlite3_column_double(p->pStmt, i);
13466 sqlite3_snprintf(50,z,"%!.20g", r);
13467 raw_printf(p->out, "%s", z);
13468 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13469 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13470 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13471 output_hex_blob(p->out, pBlob, nBlob);
13472 }else if( isNumber(azArg[i], 0) ){
13473 utf8_printf(p->out,"%s", azArg[i]);
13475 output_quoted_string(p->out, azArg[i]);
13478 fputs(p->rowSeparator, p->out);
13482 if( p->cnt++==0 && p->showHeader ){
13483 for(i=0; i<nArg; i++){
13484 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13485 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
13487 utf8_printf(p->out, "%s", p->rowSeparator);
13489 if( azArg==0 ) break;
13490 for(i=0; i<nArg; i++){
13491 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13492 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
13494 utf8_printf(p->out, "%s", p->rowSeparator);
13498 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
13506 ** This is the callback routine that the SQLite library
13507 ** invokes for each row of a query result.
13509 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
13510 /* since we don't have type info, call the shell_callback with a NULL value */
13511 return shell_callback(pArg, nArg, azArg, azCol, NULL);
13515 ** This is the callback routine from sqlite3_exec() that appends all
13516 ** output onto the end of a ShellText object.
13518 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
13519 ShellText *p = (ShellText*)pArg;
13521 UNUSED_PARAMETER(az);
13522 if( azArg==0 ) return 0;
13523 if( p->n ) appendText(p, "|", 0);
13524 for(i=0; i<nArg; i++){
13525 if( i ) appendText(p, ",", 0);
13526 if( azArg[i] ) appendText(p, azArg[i], 0);
13532 ** Generate an appropriate SELFTEST table in the main database.
13534 static void createSelftestTable(ShellState *p){
13536 sqlite3_exec(p->db,
13537 "SAVEPOINT selftest_init;\n"
13538 "CREATE TABLE IF NOT EXISTS selftest(\n"
13539 " tno INTEGER PRIMARY KEY,\n" /* Test number */
13540 " op TEXT,\n" /* Operator: memo run */
13541 " cmd TEXT,\n" /* Command text */
13542 " ans TEXT\n" /* Desired answer */
13544 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
13545 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
13546 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
13547 " 'memo','Tests generated by --init');\n"
13548 "INSERT INTO [_shell$self]\n"
13550 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
13551 "FROM sqlite_schema ORDER BY 2'',224))',\n"
13552 " hex(sha3_query('SELECT type,name,tbl_name,sql "
13553 "FROM sqlite_schema ORDER BY 2',224));\n"
13554 "INSERT INTO [_shell$self]\n"
13556 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
13557 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
13558 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
13560 " SELECT name FROM sqlite_schema\n"
13561 " WHERE type='table'\n"
13562 " AND name<>'selftest'\n"
13563 " AND coalesce(rootpage,0)>0\n"
13565 " ORDER BY name;\n"
13566 "INSERT INTO [_shell$self]\n"
13567 " VALUES('run','PRAGMA integrity_check','ok');\n"
13568 "INSERT INTO selftest(tno,op,cmd,ans)"
13569 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
13570 "DROP TABLE [_shell$self];"
13573 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
13574 sqlite3_free(zErrMsg);
13576 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
13581 ** Set the destination table field of the ShellState structure to
13582 ** the name of the table given. Escape any quote characters in the
13585 static void set_table_name(ShellState *p, const char *zName){
13590 if( p->zDestTable ){
13591 free(p->zDestTable);
13594 if( zName==0 ) return;
13595 cQuote = quoteChar(zName);
13596 n = strlen30(zName);
13597 if( cQuote ) n += n+2;
13598 z = p->zDestTable = malloc( n+1 );
13599 if( z==0 ) shell_out_of_memory();
13601 if( cQuote ) z[n++] = cQuote;
13602 for(i=0; zName[i]; i++){
13604 if( zName[i]==cQuote ) z[n++] = cQuote;
13606 if( cQuote ) z[n++] = cQuote;
13612 ** Execute a query statement that will generate SQL output. Print
13613 ** the result columns, comma-separated, on a line and then add a
13614 ** semicolon terminator to the end of that line.
13616 ** If the number of columns is 1 and that column contains text "--"
13617 ** then write the semicolon on a separate line. That way, if a
13618 ** "--" comment occurs at the end of the statement, the comment
13619 ** won't consume the semicolon terminator.
13621 static int run_table_dump_query(
13622 ShellState *p, /* Query context */
13623 const char *zSelect /* SELECT statement to extract content */
13625 sqlite3_stmt *pSelect;
13630 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13631 if( rc!=SQLITE_OK || !pSelect ){
13632 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13633 sqlite3_errmsg(p->db));
13634 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13637 rc = sqlite3_step(pSelect);
13638 nResult = sqlite3_column_count(pSelect);
13639 while( rc==SQLITE_ROW ){
13640 z = (const char*)sqlite3_column_text(pSelect, 0);
13641 utf8_printf(p->out, "%s", z);
13642 for(i=1; i<nResult; i++){
13643 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
13646 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
13648 raw_printf(p->out, "\n;\n");
13650 raw_printf(p->out, ";\n");
13652 rc = sqlite3_step(pSelect);
13654 rc = sqlite3_finalize(pSelect);
13655 if( rc!=SQLITE_OK ){
13656 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13657 sqlite3_errmsg(p->db));
13658 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13664 ** Allocate space and save off string indicating current error.
13666 static char *save_err_msg(
13667 sqlite3 *db, /* Database to query */
13668 const char *zWhen, /* Qualifier (format) wrapper */
13669 int rc /* Error code returned from API */
13673 return sqlite3_mprintf(zWhen, sqlite3_errmsg(db), rc);
13678 ** Attempt to display I/O stats on Linux using /proc/PID/io
13680 static void displayLinuxIoStats(FILE *out){
13683 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
13684 in = fopen(z, "rb");
13685 if( in==0 ) return;
13686 while( fgets(z, sizeof(z), in)!=0 ){
13687 static const struct {
13688 const char *zPattern;
13691 { "rchar: ", "Bytes received by read():" },
13692 { "wchar: ", "Bytes sent to write():" },
13693 { "syscr: ", "Read() system calls:" },
13694 { "syscw: ", "Write() system calls:" },
13695 { "read_bytes: ", "Bytes read from storage:" },
13696 { "write_bytes: ", "Bytes written to storage:" },
13697 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
13700 for(i=0; i<ArraySize(aTrans); i++){
13701 int n = strlen30(aTrans[i].zPattern);
13702 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13703 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13713 ** Display a single line of status using 64-bit values.
13715 static void displayStatLine(
13716 ShellState *p, /* The shell context */
13717 char *zLabel, /* Label for this one line */
13718 char *zFormat, /* Format for the result */
13719 int iStatusCtrl, /* Which status to display */
13720 int bReset /* True to reset the stats */
13722 sqlite3_int64 iCur = -1;
13723 sqlite3_int64 iHiwtr = -1;
13726 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
13727 for(i=0, nPercent=0; zFormat[i]; i++){
13728 if( zFormat[i]=='%' ) nPercent++;
13731 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
13733 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
13735 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
13739 ** Display memory stats.
13741 static int display_stats(
13742 sqlite3 *db, /* Database to query */
13743 ShellState *pArg, /* Pointer to ShellState */
13744 int bReset /* True to reset the stats */
13749 if( pArg==0 || pArg->out==0 ) return 0;
13752 if( pArg->pStmt && pArg->statsOn==2 ){
13754 sqlite3_stmt *pStmt = pArg->pStmt;
13756 nCol = sqlite3_column_count(pStmt);
13757 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
13758 for(i=0; i<nCol; i++){
13759 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
13760 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
13761 #ifndef SQLITE_OMIT_DECLTYPE
13762 sqlite3_snprintf(30, z+x, "declared type:");
13763 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
13765 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13766 sqlite3_snprintf(30, z+x, "database name:");
13767 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
13768 sqlite3_snprintf(30, z+x, "table name:");
13769 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
13770 sqlite3_snprintf(30, z+x, "origin name:");
13771 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
13776 if( pArg->statsOn==3 ){
13778 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13779 raw_printf(pArg->out, "VM-steps: %d\n", iCur);
13784 displayStatLine(pArg, "Memory Used:",
13785 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
13786 displayStatLine(pArg, "Number of Outstanding Allocations:",
13787 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
13788 if( pArg->shellFlgs & SHFLG_Pagecache ){
13789 displayStatLine(pArg, "Number of Pcache Pages Used:",
13790 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
13792 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
13793 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
13794 displayStatLine(pArg, "Largest Allocation:",
13795 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
13796 displayStatLine(pArg, "Largest Pcache Allocation:",
13797 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
13798 #ifdef YYTRACKMAXSTACKDEPTH
13799 displayStatLine(pArg, "Deepest Parser Stack:",
13800 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
13804 if( pArg->shellFlgs & SHFLG_Lookaside ){
13805 iHiwtr = iCur = -1;
13806 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
13807 &iCur, &iHiwtr, bReset);
13808 raw_printf(pArg->out,
13809 "Lookaside Slots Used: %d (max %d)\n",
13811 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
13812 &iCur, &iHiwtr, bReset);
13813 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
13815 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
13816 &iCur, &iHiwtr, bReset);
13817 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
13819 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
13820 &iCur, &iHiwtr, bReset);
13821 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
13824 iHiwtr = iCur = -1;
13825 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
13826 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
13828 iHiwtr = iCur = -1;
13829 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
13830 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
13831 iHiwtr = iCur = -1;
13832 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
13833 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
13834 iHiwtr = iCur = -1;
13835 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
13836 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
13837 iHiwtr = iCur = -1;
13838 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
13839 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
13840 iHiwtr = iCur = -1;
13841 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
13842 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
13844 iHiwtr = iCur = -1;
13845 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
13846 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
13851 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
13853 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
13854 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
13855 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
13856 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
13857 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
13858 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13859 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
13860 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
13861 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
13862 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
13863 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
13864 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
13865 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
13869 displayLinuxIoStats(pArg->out);
13872 /* Do not remove this machine readable comment: extra-stats-output-here */
13878 ** Display scan stats.
13880 static void display_scanstats(
13881 sqlite3 *db, /* Database to query */
13882 ShellState *pArg /* Pointer to ShellState */
13884 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13885 UNUSED_PARAMETER(db);
13886 UNUSED_PARAMETER(pArg);
13889 raw_printf(pArg->out, "-------- scanstats --------\n");
13891 for(k=0; k<=mx; k++){
13892 double rEstLoop = 1.0;
13893 for(i=n=0; 1; i++){
13894 sqlite3_stmt *p = pArg->pStmt;
13895 sqlite3_int64 nLoop, nVisit;
13898 const char *zExplain;
13899 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
13902 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
13903 if( iSid>mx ) mx = iSid;
13904 if( iSid!=k ) continue;
13906 rEstLoop = (double)nLoop;
13907 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
13910 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
13911 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
13912 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
13913 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
13915 raw_printf(pArg->out,
13916 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
13917 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
13921 raw_printf(pArg->out, "---------------------------\n");
13926 ** Parameter azArray points to a zero-terminated array of strings. zStr
13927 ** points to a single nul-terminated string. Return non-zero if zStr
13928 ** is equal, according to strcmp(), to any of the strings in the array.
13929 ** Otherwise, return zero.
13931 static int str_in_array(const char *zStr, const char **azArray){
13933 for(i=0; azArray[i]; i++){
13934 if( 0==strcmp(zStr, azArray[i]) ) return 1;
13940 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
13941 ** and populate the ShellState.aiIndent[] array with the number of
13942 ** spaces each opcode should be indented before it is output.
13944 ** The indenting rules are:
13946 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
13947 ** all opcodes that occur between the p2 jump destination and the opcode
13948 ** itself by 2 spaces.
13950 ** * For each "Goto", if the jump destination is earlier in the program
13951 ** and ends on one of:
13952 ** Yield SeekGt SeekLt RowSetRead Rewind
13953 ** or if the P1 parameter is one instead of zero,
13954 ** then indent all opcodes between the earlier instruction
13955 ** and "Goto" by 2 spaces.
13957 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
13958 const char *zSql; /* The text of the SQL statement */
13959 const char *z; /* Used to check if this is an EXPLAIN */
13960 int *abYield = 0; /* True if op is an OP_Yield */
13961 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
13962 int iOp; /* Index of operation in p->aiIndent[] */
13964 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
13965 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
13967 const char *azGoto[] = { "Goto", 0 };
13969 /* Try to figure out if this is really an EXPLAIN statement. If this
13970 ** cannot be verified, return early. */
13971 if( sqlite3_column_count(pSql)!=8 ){
13972 p->cMode = p->mode;
13975 zSql = sqlite3_sql(pSql);
13976 if( zSql==0 ) return;
13977 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
13978 if( sqlite3_strnicmp(z, "explain", 7) ){
13979 p->cMode = p->mode;
13983 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
13985 int iAddr = sqlite3_column_int(pSql, 0);
13986 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
13988 /* Set p2 to the P2 field of the current opcode. Then, assuming that
13989 ** p2 is an instruction address, set variable p2op to the index of that
13990 ** instruction in the aiIndent[] array. p2 and p2op may be different if
13991 ** the current instruction is part of a sub-program generated by an
13992 ** SQL trigger or foreign key. */
13993 int p2 = sqlite3_column_int(pSql, 3);
13994 int p2op = (p2 + (iOp-iAddr));
13996 /* Grow the p->aiIndent array as required */
13999 /* Do further verfication that this is explain output. Abort if
14001 static const char *explainCols[] = {
14002 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
14004 for(jj=0; jj<ArraySize(explainCols); jj++){
14005 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
14006 p->cMode = p->mode;
14007 sqlite3_reset(pSql);
14013 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
14014 if( p->aiIndent==0 ) shell_out_of_memory();
14015 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
14016 if( abYield==0 ) shell_out_of_memory();
14018 abYield[iOp] = str_in_array(zOp, azYield);
14019 p->aiIndent[iOp] = 0;
14020 p->nIndent = iOp+1;
14022 if( str_in_array(zOp, azNext) ){
14023 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14025 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
14026 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
14028 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
14033 sqlite3_free(abYield);
14034 sqlite3_reset(pSql);
14038 ** Free the array allocated by explain_data_prepare().
14040 static void explain_data_delete(ShellState *p){
14041 sqlite3_free(p->aiIndent);
14048 ** Disable and restore .wheretrace and .selecttrace settings.
14050 static unsigned int savedSelectTrace;
14051 static unsigned int savedWhereTrace;
14052 static void disable_debug_trace_modes(void){
14053 unsigned int zero = 0;
14054 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
14055 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
14056 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
14057 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
14059 static void restore_debug_trace_modes(void){
14060 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
14061 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
14064 /* Create the TEMP table used to store parameter bindings */
14065 static void bind_table_init(ShellState *p){
14067 int defensiveMode = 0;
14068 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
14069 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
14070 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
14071 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
14072 sqlite3_exec(p->db,
14073 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
14074 " key TEXT PRIMARY KEY,\n"
14076 ") WITHOUT ROWID;",
14078 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
14079 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
14083 ** Bind parameters on a prepared statement.
14085 ** Parameter bindings are taken from a TEMP table of the form:
14087 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
14090 ** No bindings occur if this table does not exist. The name of the table
14091 ** begins with "sqlite_" so that it will not collide with ordinary application
14092 ** tables. The table must be in the TEMP schema.
14094 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
14098 sqlite3_stmt *pQ = 0;
14100 nVar = sqlite3_bind_parameter_count(pStmt);
14101 if( nVar==0 ) return; /* Nothing to do */
14102 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
14103 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
14104 return; /* Parameter table does not exist */
14106 rc = sqlite3_prepare_v2(pArg->db,
14107 "SELECT value FROM temp.sqlite_parameters"
14108 " WHERE key=?1", -1, &pQ, 0);
14109 if( rc || pQ==0 ) return;
14110 for(i=1; i<=nVar; i++){
14112 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
14114 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
14117 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
14118 if( sqlite3_step(pQ)==SQLITE_ROW ){
14119 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
14121 sqlite3_bind_null(pStmt, i);
14125 sqlite3_finalize(pQ);
14129 ** UTF8 box-drawing characters. Imagine box lines like this:
14137 ** Each box characters has between 2 and 4 of the lines leading from
14138 ** the center. The characters are here identified by the numbers of
14139 ** their corresponding lines.
14141 #define BOX_24 "\342\224\200" /* U+2500 --- */
14142 #define BOX_13 "\342\224\202" /* U+2502 | */
14143 #define BOX_23 "\342\224\214" /* U+250c ,- */
14144 #define BOX_34 "\342\224\220" /* U+2510 -, */
14145 #define BOX_12 "\342\224\224" /* U+2514 '- */
14146 #define BOX_14 "\342\224\230" /* U+2518 -' */
14147 #define BOX_123 "\342\224\234" /* U+251c |- */
14148 #define BOX_134 "\342\224\244" /* U+2524 -| */
14149 #define BOX_234 "\342\224\254" /* U+252c -,- */
14150 #define BOX_124 "\342\224\264" /* U+2534 -'- */
14151 #define BOX_1234 "\342\224\274" /* U+253c -|- */
14153 /* Draw horizontal line N characters long using unicode box
14156 static void print_box_line(FILE *out, int N){
14157 const char zDash[] =
14158 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
14159 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
14160 const int nDash = sizeof(zDash) - 1;
14163 utf8_printf(out, zDash);
14166 utf8_printf(out, "%.*s", N, zDash);
14170 ** Draw a horizontal separator for a MODE_Box table.
14172 static void print_box_row_separator(
14181 utf8_printf(p->out, "%s", zSep1);
14182 print_box_line(p->out, p->actualWidth[0]+2);
14183 for(i=1; i<nArg; i++){
14184 utf8_printf(p->out, "%s", zSep2);
14185 print_box_line(p->out, p->actualWidth[i]+2);
14187 utf8_printf(p->out, "%s", zSep3);
14189 fputs("\n", p->out);
14195 ** Run a prepared statement and output the result in one of the
14196 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
14199 ** This is different from ordinary exec_prepared_stmt() in that
14200 ** it has to run the entire query and gather the results into memory
14201 ** first, in order to determine column widths, before providing
14204 static void exec_prepared_stmt_columnar(
14205 ShellState *p, /* Pointer to ShellState */
14206 sqlite3_stmt *pStmt /* Statment to run */
14208 sqlite3_int64 nRow = 0;
14211 sqlite3_int64 nAlloc = 0;
14214 sqlite3_int64 i, nData;
14215 int j, nTotal, w, n;
14216 const char *colSep = 0;
14217 const char *rowSep = 0;
14219 rc = sqlite3_step(pStmt);
14220 if( rc!=SQLITE_ROW ) return;
14221 nColumn = sqlite3_column_count(pStmt);
14222 nAlloc = nColumn*4;
14223 if( nAlloc<=0 ) nAlloc = 1;
14224 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14225 if( azData==0 ) shell_out_of_memory();
14226 for(i=0; i<nColumn; i++){
14227 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14230 if( (nRow+2)*nColumn >= nAlloc ){
14232 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14233 if( azData==0 ) shell_out_of_memory();
14236 for(i=0; i<nColumn; i++){
14237 z = (const char*)sqlite3_column_text(pStmt,i);
14238 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14240 }while( sqlite3_step(pStmt)==SQLITE_ROW );
14241 if( nColumn>p->nWidth ){
14242 p->colWidth = realloc(p->colWidth, (nColumn+1)*2*sizeof(int));
14243 if( p->colWidth==0 ) shell_out_of_memory();
14244 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14245 p->nWidth = nColumn;
14246 p->actualWidth = &p->colWidth[nColumn];
14248 memset(p->actualWidth, 0, nColumn*sizeof(int));
14249 for(i=0; i<nColumn; i++){
14250 w = p->colWidth[i];
14252 p->actualWidth[i] = w;
14254 nTotal = nColumn*(nRow+1);
14255 for(i=0; i<nTotal; i++){
14257 if( z==0 ) z = p->nullValue;
14260 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
14262 if( seenInterrupt ) goto columnar_end;
14263 if( nColumn==0 ) goto columnar_end;
14264 switch( p->cMode ){
14265 case MODE_Column: {
14268 if( p->showHeader ){
14269 for(i=0; i<nColumn; i++){
14270 w = p->actualWidth[i];
14271 if( p->colWidth[i]<0 ) w = -w;
14272 utf8_width_print(p->out, w, azData[i]);
14273 fputs(i==nColumn-1?"\n":" ", p->out);
14275 for(i=0; i<nColumn; i++){
14276 print_dashes(p->out, p->actualWidth[i]);
14277 fputs(i==nColumn-1?"\n":" ", p->out);
14285 print_row_separator(p, nColumn, "+");
14286 fputs("| ", p->out);
14287 for(i=0; i<nColumn; i++){
14288 w = p->actualWidth[i];
14289 n = strlenChar(azData[i]);
14290 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14291 fputs(i==nColumn-1?" |\n":" | ", p->out);
14293 print_row_separator(p, nColumn, "+");
14296 case MODE_Markdown: {
14299 fputs("| ", p->out);
14300 for(i=0; i<nColumn; i++){
14301 w = p->actualWidth[i];
14302 n = strlenChar(azData[i]);
14303 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14304 fputs(i==nColumn-1?" |\n":" | ", p->out);
14306 print_row_separator(p, nColumn, "|");
14310 colSep = " " BOX_13 " ";
14311 rowSep = " " BOX_13 "\n";
14312 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
14313 utf8_printf(p->out, BOX_13 " ");
14314 for(i=0; i<nColumn; i++){
14315 w = p->actualWidth[i];
14316 n = strlenChar(azData[i]);
14317 utf8_printf(p->out, "%*s%s%*s%s",
14318 (w-n)/2, "", azData[i], (w-n+1)/2, "",
14319 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
14321 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14325 for(i=nColumn, j=0; i<nTotal; i++, j++){
14326 if( j==0 && p->cMode!=MODE_Column ){
14327 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
14330 if( z==0 ) z = p->nullValue;
14331 w = p->actualWidth[j];
14332 if( p->colWidth[j]<0 ) w = -w;
14333 utf8_width_print(p->out, w, z);
14334 if( j==nColumn-1 ){
14335 utf8_printf(p->out, "%s", rowSep);
14337 if( seenInterrupt ) goto columnar_end;
14339 utf8_printf(p->out, "%s", colSep);
14342 if( p->cMode==MODE_Table ){
14343 print_row_separator(p, nColumn, "+");
14344 }else if( p->cMode==MODE_Box ){
14345 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
14348 if( seenInterrupt ){
14349 utf8_printf(p->out, "Interrupt\n");
14351 nData = (nRow+1)*nColumn;
14352 for(i=0; i<nData; i++) free(azData[i]);
14353 sqlite3_free(azData);
14357 ** Run a prepared statement
14359 static void exec_prepared_stmt(
14360 ShellState *pArg, /* Pointer to ShellState */
14361 sqlite3_stmt *pStmt /* Statment to run */
14365 if( pArg->cMode==MODE_Column
14366 || pArg->cMode==MODE_Table
14367 || pArg->cMode==MODE_Box
14368 || pArg->cMode==MODE_Markdown
14370 exec_prepared_stmt_columnar(pArg, pStmt);
14374 /* perform the first step. this will tell us if we
14375 ** have a result set or not and how wide it is.
14377 rc = sqlite3_step(pStmt);
14378 /* if we have a result set... */
14379 if( SQLITE_ROW == rc ){
14380 /* allocate space for col name ptr, value ptr, and type */
14381 int nCol = sqlite3_column_count(pStmt);
14382 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
14384 shell_out_of_memory();
14386 char **azCols = (char **)pData; /* Names of result columns */
14387 char **azVals = &azCols[nCol]; /* Results */
14388 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
14390 assert(sizeof(int) <= sizeof(char *));
14391 /* save off ptrs to column names */
14392 for(i=0; i<nCol; i++){
14393 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
14396 /* extract the data and data types */
14397 for(i=0; i<nCol; i++){
14398 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
14399 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
14402 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
14404 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
14406 break; /* from for */
14410 /* if data and types extracted successfully... */
14411 if( SQLITE_ROW == rc ){
14412 /* call the supplied callback with the result row data */
14413 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
14416 rc = sqlite3_step(pStmt);
14419 } while( SQLITE_ROW == rc );
14420 sqlite3_free(pData);
14421 if( pArg->cMode==MODE_Json ){
14422 fputs("]\n", pArg->out);
14428 #ifndef SQLITE_OMIT_VIRTUALTABLE
14430 ** This function is called to process SQL if the previous shell command
14431 ** was ".expert". It passes the SQL in the second argument directly to
14432 ** the sqlite3expert object.
14434 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14435 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14436 ** an English language error message. It is the responsibility of the
14437 ** caller to eventually free this buffer using sqlite3_free().
14439 static int expertHandleSQL(
14440 ShellState *pState,
14444 assert( pState->expert.pExpert );
14445 assert( pzErr==0 || *pzErr==0 );
14446 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
14450 ** This function is called either to silently clean up the object
14451 ** created by the ".expert" command (if bCancel==1), or to generate a
14452 ** report from it and then clean it up (if bCancel==0).
14454 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14455 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14456 ** an English language error message. It is the responsibility of the
14457 ** caller to eventually free this buffer using sqlite3_free().
14459 static int expertFinish(
14460 ShellState *pState,
14464 int rc = SQLITE_OK;
14465 sqlite3expert *p = pState->expert.pExpert;
14467 assert( bCancel || pzErr==0 || *pzErr==0 );
14469 FILE *out = pState->out;
14470 int bVerbose = pState->expert.bVerbose;
14472 rc = sqlite3_expert_analyze(p, pzErr);
14473 if( rc==SQLITE_OK ){
14474 int nQuery = sqlite3_expert_count(p);
14478 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
14479 raw_printf(out, "-- Candidates -----------------------------\n");
14480 raw_printf(out, "%s\n", zCand);
14482 for(i=0; i<nQuery; i++){
14483 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
14484 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
14485 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
14486 if( zIdx==0 ) zIdx = "(no new indexes)\n";
14488 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
14489 raw_printf(out, "%s\n\n", zSql);
14491 raw_printf(out, "%s\n", zIdx);
14492 raw_printf(out, "%s\n", zEQP);
14496 sqlite3_expert_destroy(p);
14497 pState->expert.pExpert = 0;
14502 ** Implementation of ".expert" dot command.
14504 static int expertDotCommand(
14505 ShellState *pState, /* Current shell tool state */
14506 char **azArg, /* Array of arguments passed to dot command */
14507 int nArg /* Number of entries in azArg[] */
14509 int rc = SQLITE_OK;
14514 assert( pState->expert.pExpert==0 );
14515 memset(&pState->expert, 0, sizeof(ExpertInfo));
14517 for(i=1; rc==SQLITE_OK && i<nArg; i++){
14518 char *z = azArg[i];
14520 if( z[0]=='-' && z[1]=='-' ) z++;
14522 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14523 pState->expert.bVerbose = 1;
14525 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14527 raw_printf(stderr, "option requires an argument: %s\n", z);
14530 iSample = (int)integerValue(azArg[++i]);
14531 if( iSample<0 || iSample>100 ){
14532 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
14538 raw_printf(stderr, "unknown option: %s\n", z);
14543 if( rc==SQLITE_OK ){
14544 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14545 if( pState->expert.pExpert==0 ){
14546 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14549 sqlite3_expert_config(
14550 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14557 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14560 ** Execute a statement or set of statements. Print
14561 ** any result rows/columns depending on the current mode
14562 ** set via the supplied callback.
14564 ** This is very similar to SQLite's built-in sqlite3_exec()
14565 ** function except it takes a slightly different callback
14566 ** and callback data argument.
14568 static int shell_exec(
14569 ShellState *pArg, /* Pointer to ShellState */
14570 const char *zSql, /* SQL to be evaluated */
14571 char **pzErrMsg /* Error msg written here */
14573 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
14574 int rc = SQLITE_OK; /* Return Code */
14576 const char *zLeftover; /* Tail of unprocessed SQL */
14577 sqlite3 *db = pArg->db;
14583 #ifndef SQLITE_OMIT_VIRTUALTABLE
14584 if( pArg->expert.pExpert ){
14585 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
14586 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
14590 while( zSql[0] && (SQLITE_OK == rc) ){
14591 static const char *zStmtSql;
14592 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14593 if( SQLITE_OK != rc ){
14595 *pzErrMsg = save_err_msg(db, "in prepare, %s (%d)", rc);
14599 /* this happens for a comment or white-space */
14601 while( IsSpace(zSql[0]) ) zSql++;
14604 zStmtSql = sqlite3_sql(pStmt);
14605 if( zStmtSql==0 ) zStmtSql = "";
14606 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
14608 /* save off the prepared statment handle and reset row count */
14610 pArg->pStmt = pStmt;
14614 /* echo the sql statement if echo on */
14615 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
14616 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
14619 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
14620 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
14621 sqlite3_stmt *pExplain;
14623 int triggerEQP = 0;
14624 disable_debug_trace_modes();
14625 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14626 if( pArg->autoEQP>=AUTOEQP_trigger ){
14627 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14629 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14630 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14631 if( rc==SQLITE_OK ){
14632 while( sqlite3_step(pExplain)==SQLITE_ROW ){
14633 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14634 int iEqpId = sqlite3_column_int(pExplain, 0);
14635 int iParentId = sqlite3_column_int(pExplain, 1);
14636 if( zEQPLine==0 ) zEQPLine = "";
14637 if( zEQPLine[0]=='-' ) eqp_render(pArg);
14638 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
14642 sqlite3_finalize(pExplain);
14643 sqlite3_free(zEQP);
14644 if( pArg->autoEQP>=AUTOEQP_full ){
14645 /* Also do an EXPLAIN for ".eqp full" mode */
14646 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14647 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14648 if( rc==SQLITE_OK ){
14649 pArg->cMode = MODE_Explain;
14650 explain_data_prepare(pArg, pExplain);
14651 exec_prepared_stmt(pArg, pExplain);
14652 explain_data_delete(pArg);
14654 sqlite3_finalize(pExplain);
14655 sqlite3_free(zEQP);
14657 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
14658 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
14659 /* Reprepare pStmt before reactiving trace modes */
14660 sqlite3_finalize(pStmt);
14661 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
14662 if( pArg ) pArg->pStmt = pStmt;
14664 restore_debug_trace_modes();
14668 pArg->cMode = pArg->mode;
14669 if( pArg->autoExplain ){
14670 if( sqlite3_stmt_isexplain(pStmt)==1 ){
14671 pArg->cMode = MODE_Explain;
14673 if( sqlite3_stmt_isexplain(pStmt)==2 ){
14674 pArg->cMode = MODE_EQP;
14678 /* If the shell is currently in ".explain" mode, gather the extra
14679 ** data required to add indents to the output.*/
14680 if( pArg->cMode==MODE_Explain ){
14681 explain_data_prepare(pArg, pStmt);
14685 bind_prepared_stmt(pArg, pStmt);
14686 exec_prepared_stmt(pArg, pStmt);
14687 explain_data_delete(pArg);
14690 /* print usage stats if stats on */
14691 if( pArg && pArg->statsOn ){
14692 display_stats(db, pArg, 0);
14695 /* print loop-counters if required */
14696 if( pArg && pArg->scanstatsOn ){
14697 display_scanstats(db, pArg);
14700 /* Finalize the statement just executed. If this fails, save a
14701 ** copy of the error message. Otherwise, set zSql to point to the
14702 ** next statement to execute. */
14703 rc2 = sqlite3_finalize(pStmt);
14704 if( rc!=SQLITE_NOMEM ) rc = rc2;
14705 if( rc==SQLITE_OK ){
14707 while( IsSpace(zSql[0]) ) zSql++;
14708 }else if( pzErrMsg ){
14709 *pzErrMsg = save_err_msg(db, "stepping, %s (%d)", rc);
14712 /* clear saved stmt handle */
14714 pArg->pStmt = NULL;
14723 ** Release memory previously allocated by tableColumnList().
14725 static void freeColumnList(char **azCol){
14727 for(i=1; azCol[i]; i++){
14728 sqlite3_free(azCol[i]);
14730 /* azCol[0] is a static string */
14731 sqlite3_free(azCol);
14735 ** Return a list of pointers to strings which are the names of all
14736 ** columns in table zTab. The memory to hold the names is dynamically
14737 ** allocated and must be released by the caller using a subsequent call
14738 ** to freeColumnList().
14740 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
14741 ** value that needs to be preserved, then azCol[0] is filled in with the
14742 ** name of the rowid column.
14744 ** The first regular column in the table is azCol[1]. The list is terminated
14745 ** by an entry with azCol[i]==0.
14747 static char **tableColumnList(ShellState *p, const char *zTab){
14749 sqlite3_stmt *pStmt;
14753 int nPK = 0; /* Number of PRIMARY KEY columns seen */
14754 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
14755 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14758 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14759 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14760 sqlite3_free(zSql);
14762 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14763 if( nCol>=nAlloc-2 ){
14764 nAlloc = nAlloc*2 + nCol + 10;
14765 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14766 if( azCol==0 ) shell_out_of_memory();
14768 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14769 if( sqlite3_column_int(pStmt, 5) ){
14772 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14781 sqlite3_finalize(pStmt);
14782 if( azCol==0 ) return 0;
14786 /* The decision of whether or not a rowid really needs to be preserved
14787 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
14788 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
14789 ** rowids on tables where the rowid is inaccessible because there are other
14790 ** columns in the table named "rowid", "_rowid_", and "oid".
14792 if( preserveRowid && isIPK ){
14793 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
14794 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
14795 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
14796 ** ROWID aliases. To distinguish these cases, check to see if
14797 ** there is a "pk" entry in "PRAGMA index_list". There will be
14798 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14800 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14801 " WHERE origin='pk'", zTab);
14802 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14803 sqlite3_free(zSql);
14805 freeColumnList(azCol);
14808 rc = sqlite3_step(pStmt);
14809 sqlite3_finalize(pStmt);
14810 preserveRowid = rc==SQLITE_ROW;
14812 if( preserveRowid ){
14813 /* Only preserve the rowid if we can find a name to use for the
14815 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
14817 for(j=0; j<3; j++){
14818 for(i=1; i<=nCol; i++){
14819 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
14822 /* At this point, we know that azRowid[j] is not the name of any
14823 ** ordinary column in the table. Verify that azRowid[j] is a valid
14824 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
14825 ** tables will fail this last check */
14826 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
14827 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
14836 ** Toggle the reverse_unordered_selects setting.
14838 static void toggleSelectOrder(sqlite3 *db){
14839 sqlite3_stmt *pStmt = 0;
14842 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
14843 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14844 iSetting = sqlite3_column_int(pStmt, 0);
14846 sqlite3_finalize(pStmt);
14847 sqlite3_snprintf(sizeof(zStmt), zStmt,
14848 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
14849 sqlite3_exec(db, zStmt, 0, 0, 0);
14853 ** This is a different callback routine used for dumping the database.
14854 ** Each row received by this callback consists of a table name,
14855 ** the table type ("index" or "table") and SQL to create the table.
14856 ** This routine should print text sufficient to recreate the table.
14858 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
14860 const char *zTable;
14863 ShellState *p = (ShellState *)pArg;
14867 UNUSED_PARAMETER(azNotUsed);
14868 if( nArg!=3 || azArg==0 ) return 0;
14872 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
14873 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
14875 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
14876 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
14877 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
14878 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
14879 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
14881 }else if( dataOnly ){
14883 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
14885 if( !p->writableSchema ){
14886 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
14887 p->writableSchema = 1;
14889 zIns = sqlite3_mprintf(
14890 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14891 "VALUES('table','%q','%q',0,'%q');",
14892 zTable, zTable, zSql);
14893 utf8_printf(p->out, "%s\n", zIns);
14894 sqlite3_free(zIns);
14897 printSchemaLine(p->out, zSql, ";\n");
14900 if( strcmp(zType, "table")==0 ){
14905 char *savedDestTable;
14908 azCol = tableColumnList(p, zTable);
14914 /* Always quote the table name, even if it appears to be pure ascii,
14915 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
14917 appendText(&sTable, zTable, quoteChar(zTable));
14918 /* If preserving the rowid, add a column list after the table name.
14919 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
14920 ** instead of the usual "INSERT INTO tab VALUES(...)".
14923 appendText(&sTable, "(", 0);
14924 appendText(&sTable, azCol[0], 0);
14925 for(i=1; azCol[i]; i++){
14926 appendText(&sTable, ",", 0);
14927 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
14929 appendText(&sTable, ")", 0);
14932 /* Build an appropriate SELECT statement */
14933 initText(&sSelect);
14934 appendText(&sSelect, "SELECT ", 0);
14936 appendText(&sSelect, azCol[0], 0);
14937 appendText(&sSelect, ",", 0);
14939 for(i=1; azCol[i]; i++){
14940 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
14942 appendText(&sSelect, ",", 0);
14945 freeColumnList(azCol);
14946 appendText(&sSelect, " FROM ", 0);
14947 appendText(&sSelect, zTable, quoteChar(zTable));
14949 savedDestTable = p->zDestTable;
14950 savedMode = p->mode;
14951 p->zDestTable = sTable.z;
14952 p->mode = p->cMode = MODE_Insert;
14953 rc = shell_exec(p, sSelect.z, 0);
14954 if( (rc&0xff)==SQLITE_CORRUPT ){
14955 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14956 toggleSelectOrder(p->db);
14957 shell_exec(p, sSelect.z, 0);
14958 toggleSelectOrder(p->db);
14960 p->zDestTable = savedDestTable;
14961 p->mode = savedMode;
14963 freeText(&sSelect);
14964 if( rc ) p->nErr++;
14970 ** Run zQuery. Use dump_callback() as the callback routine so that
14971 ** the contents of the query are output as SQL statements.
14973 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
14974 ** "ORDER BY rowid DESC" to the end.
14976 static int run_schema_dump_query(
14982 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
14983 if( rc==SQLITE_CORRUPT ){
14985 int len = strlen30(zQuery);
14986 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14988 utf8_printf(p->out, "/****** %s ******/\n", zErr);
14989 sqlite3_free(zErr);
14992 zQ2 = malloc( len+100 );
14993 if( zQ2==0 ) return rc;
14994 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
14995 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
14997 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
14999 rc = SQLITE_CORRUPT;
15001 sqlite3_free(zErr);
15008 ** Text of help messages.
15010 ** The help text for each individual command begins with a line that starts
15011 ** with ".". Subsequent lines are supplimental information.
15013 ** There must be two or more spaces between the end of the command and the
15014 ** start of the description of what that command does.
15016 static const char *(azHelp[]) = {
15017 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
15018 ".archive ... Manage SQL archives",
15019 " Each command must have exactly one of the following options:",
15020 " -c, --create Create a new archive",
15021 " -u, --update Add or update files with changed mtime",
15022 " -i, --insert Like -u but always add even if unchanged",
15023 " -r, --remove Remove files from archive",
15024 " -t, --list List contents of archive",
15025 " -x, --extract Extract files from archive",
15026 " Optional arguments:",
15027 " -v, --verbose Print each filename as it is processed",
15028 " -f FILE, --file FILE Use archive FILE (default is current db)",
15029 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
15030 " -C DIR, --directory DIR Read/extract files from directory DIR",
15031 " -g, --glob Use glob matching for names in archive",
15032 " -n, --dryrun Show the SQL that would have occurred",
15034 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
15035 " .ar -tf ARCHIVE # List members of ARCHIVE",
15036 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
15038 " http://sqlite.org/cli.html#sqlite_archive_support",
15040 #ifndef SQLITE_OMIT_AUTHORIZATION
15041 ".auth ON|OFF Show authorizer callbacks",
15043 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
15044 " --append Use the appendvfs",
15045 " --async Write to FILE without journal and fsync()",
15046 ".bail on|off Stop after hitting an error. Default OFF",
15047 ".binary on|off Turn binary output on or off. Default OFF",
15048 ".cd DIRECTORY Change the working directory to DIRECTORY",
15049 ".changes on|off Show number of rows changed by SQL",
15050 ".check GLOB Fail if output since .testcase does not match",
15051 ".clone NEWDB Clone data into NEWDB from the existing database",
15052 ".connection [close] [#] Open or close an auxiliary database connection",
15053 ".databases List names and files of attached databases",
15054 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
15055 ".dbinfo ?DB? Show status information about the database",
15056 ".dump ?OBJECTS? Render database content as SQL",
15058 " --data-only Output only INSERT statements",
15059 " --newlines Allow unescaped newline characters in output",
15060 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
15061 " --preserve-rowids Include ROWID values in the output",
15062 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
15063 " Additional LIKE patterns can be given in subsequent arguments",
15064 ".echo on|off Turn command echo on or off",
15065 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
15067 #ifdef SQLITE_DEBUG
15068 " test Show raw EXPLAIN QUERY PLAN output",
15069 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
15071 " trigger Like \"full\" but also show trigger bytecode",
15072 ".excel Display the output of next command in spreadsheet",
15073 " --bom Put a UTF8 byte-order mark on intermediate file",
15074 ".exit ?CODE? Exit this program with return-code CODE",
15075 ".expert EXPERIMENTAL. Suggest indexes for queries",
15076 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
15077 ".filectrl CMD ... Run various sqlite3_file_control() operations",
15078 " --schema SCHEMA Use SCHEMA instead of \"main\"",
15079 " --help Show CMD details",
15080 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
15081 ".headers on|off Turn display of headers on or off",
15082 ".help ?-all? ?PATTERN? Show help text for PATTERN",
15083 ".import FILE TABLE Import data from FILE into TABLE",
15085 " --ascii Use \\037 and \\036 as column and row separators",
15086 " --csv Use , and \\n as column and row separators",
15087 " --skip N Skip the first N rows of input",
15088 " -v \"Verbose\" - increase auxiliary output",
15090 " * If TABLE does not exist, it is created. The first row of input",
15091 " determines the column names.",
15092 " * If neither --csv or --ascii are used, the input mode is derived",
15093 " from the \".mode\" output mode",
15094 " * If FILE begins with \"|\" then it is a command that generates the",
15096 #ifndef SQLITE_OMIT_TEST_CONTROL
15097 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
15099 ".indexes ?TABLE? Show names of indexes",
15100 " If TABLE is specified, only show indexes for",
15101 " tables matching TABLE using the LIKE operator.",
15102 #ifdef SQLITE_ENABLE_IOTRACE
15103 ".iotrace FILE Enable I/O diagnostic logging to FILE",
15105 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
15106 ".lint OPTIONS Report potential schema issues.",
15108 " fkey-indexes Find missing foreign key indexes",
15109 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15110 ".load FILE ?ENTRY? Load an extension library",
15112 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
15113 ".mode MODE ?TABLE? Set output mode",
15114 " MODE is one of:",
15115 " ascii Columns/rows delimited by 0x1F and 0x1E",
15116 " box Tables using unicode box-drawing characters",
15117 " csv Comma-separated values",
15118 " column Output in columns. (See .width)",
15119 " html HTML <table> code",
15120 " insert SQL insert statements for TABLE",
15121 " json Results in a JSON array",
15122 " line One value per line",
15123 " list Values delimited by \"|\"",
15124 " markdown Markdown table format",
15125 " quote Escape answers as for SQL",
15126 " table ASCII-art table",
15127 " tabs Tab-separated values",
15128 " tcl TCL list elements",
15129 ".nonce STRING Disable safe mode for one command if the nonce matches",
15130 ".nullvalue STRING Use STRING in place of NULL values",
15131 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
15132 " If FILE begins with '|' then open as a pipe",
15133 " --bom Put a UTF8 byte-order mark at the beginning",
15134 " -e Send output to the system text editor",
15135 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
15136 #ifdef SQLITE_DEBUG
15137 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
15139 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
15141 " --append Use appendvfs to append database to the end of FILE",
15142 #ifndef SQLITE_OMIT_DESERIALIZE
15143 " --deserialize Load into memory using sqlite3_deserialize()",
15144 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
15145 " --maxsize N Maximum size for --hexdb or --deserialized database",
15147 " --new Initialize FILE to an empty database",
15148 " --nofollow Do not follow symbolic links",
15149 " --readonly Open FILE readonly",
15150 " --zip FILE is a ZIP archive",
15151 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
15152 " If FILE begins with '|' then open it as a pipe.",
15154 " --bom Prefix output with a UTF8 byte-order mark",
15155 " -e Send output to the system text editor",
15156 " -x Send output as CSV to a spreadsheet",
15157 ".parameter CMD ... Manage SQL parameter bindings",
15158 " clear Erase all bindings",
15159 " init Initialize the TEMP table that holds bindings",
15160 " list List the current parameter bindings",
15161 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
15162 " PARAMETER should start with one of: $ : @ ?",
15163 " unset PARAMETER Remove PARAMETER from the binding table",
15164 ".print STRING... Print literal STRING",
15165 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
15166 ".progress N Invoke progress handler after every N opcodes",
15167 " --limit N Interrupt after N progress callbacks",
15168 " --once Do no more than one progress interrupt",
15169 " --quiet|-q No output except at interrupts",
15170 " --reset Reset the count for each input and interrupt",
15172 ".prompt MAIN CONTINUE Replace the standard prompts",
15173 ".quit Exit this program",
15174 ".read FILE Read input from FILE",
15175 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15176 ".recover Recover as much data as possible from corrupt db.",
15177 " --freelist-corrupt Assume the freelist is corrupt",
15178 " --recovery-db NAME Store recovery metadata in database file NAME",
15179 " --lost-and-found TABLE Alternative name for the lost-and-found table",
15180 " --no-rowids Do not attempt to recover rowid values",
15181 " that are not also INTEGER PRIMARY KEYs",
15183 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
15184 ".save FILE Write in-memory database into FILE",
15185 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
15186 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
15188 " --indent Try to pretty-print the schema",
15189 " --nosys Omit objects whose names start with \"sqlite_\"",
15190 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
15192 " --init Create a new SELFTEST table",
15193 " -v Verbose output",
15194 ".separator COL ?ROW? Change the column and row separators",
15195 #if defined(SQLITE_ENABLE_SESSION)
15196 ".session ?NAME? CMD ... Create or control sessions",
15198 " attach TABLE Attach TABLE",
15199 " changeset FILE Write a changeset into FILE",
15200 " close Close one session",
15201 " enable ?BOOLEAN? Set or query the enable bit",
15202 " filter GLOB... Reject tables matching GLOBs",
15203 " indirect ?BOOLEAN? Mark or query the indirect status",
15204 " isempty Query whether the session is empty",
15205 " list List currently open session names",
15206 " open DB NAME Open a new session on DB",
15207 " patchset FILE Write a patchset into FILE",
15208 " If ?NAME? is omitted, the first defined session is used.",
15210 ".sha3sum ... Compute a SHA3 hash of database content",
15212 " --schema Also hash the sqlite_schema table",
15213 " --sha3-224 Use the sha3-224 algorithm",
15214 " --sha3-256 Use the sha3-256 algorithm (default)",
15215 " --sha3-384 Use the sha3-384 algorithm",
15216 " --sha3-512 Use the sha3-512 algorithm",
15217 " Any other argument is a LIKE pattern for tables to hash",
15218 #ifndef SQLITE_NOHAVE_SYSTEM
15219 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15221 ".show Show the current values for various settings",
15222 ".stats ?ARG? Show stats or turn stats on or off",
15223 " off Turn off automatic stat display",
15224 " on Turn on automatic stat display",
15225 " stmt Show statement stats",
15226 " vmstep Show the virtual machine step count only",
15227 #ifndef SQLITE_NOHAVE_SYSTEM
15228 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15230 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15231 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15232 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15233 " Run \".testctrl\" with no arguments for details",
15234 ".timeout MS Try opening locked tables for MS milliseconds",
15235 ".timer on|off Turn SQL timer on or off",
15236 #ifndef SQLITE_OMIT_TRACE
15237 ".trace ?OPTIONS? Output each SQL statement as it is run",
15238 " FILE Send output to FILE",
15239 " stdout Send output to stdout",
15240 " stderr Send output to stderr",
15241 " off Disable tracing",
15242 " --expanded Expand query parameters",
15243 #ifdef SQLITE_ENABLE_NORMALIZE
15244 " --normalized Normal the SQL statements",
15246 " --plain Show SQL as it is input",
15247 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
15248 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
15249 " --row Trace each row (SQLITE_TRACE_ROW)",
15250 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
15251 #endif /* SQLITE_OMIT_TRACE */
15252 #ifdef SQLITE_DEBUG
15253 ".unmodule NAME ... Unregister virtual table modules",
15254 " --allexcept Unregister everything except those named",
15256 ".vfsinfo ?AUX? Information about the top-level VFS",
15257 ".vfslist List all available VFSes",
15258 ".vfsname ?AUX? Print the name of the VFS stack",
15259 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
15260 " Negative values right-justify",
15264 ** Output help text.
15266 ** zPattern describes the set of commands for which help text is provided.
15267 ** If zPattern is NULL, then show all commands, but only give a one-line
15268 ** description of each.
15270 ** Return the number of matches.
15272 static int showHelp(FILE *out, const char *zPattern){
15278 || zPattern[0]=='0'
15279 || strcmp(zPattern,"-a")==0
15280 || strcmp(zPattern,"-all")==0
15281 || strcmp(zPattern,"--all")==0
15283 /* Show all commands, but only one line per command */
15284 if( zPattern==0 ) zPattern = "";
15285 for(i=0; i<ArraySize(azHelp); i++){
15286 if( azHelp[i][0]=='.' || zPattern[0] ){
15287 utf8_printf(out, "%s\n", azHelp[i]);
15292 /* Look for commands that for which zPattern is an exact prefix */
15293 zPat = sqlite3_mprintf(".%s*", zPattern);
15294 for(i=0; i<ArraySize(azHelp); i++){
15295 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15296 utf8_printf(out, "%s\n", azHelp[i]);
15301 sqlite3_free(zPat);
15304 /* when zPattern is a prefix of exactly one command, then include the
15305 ** details of that command, which should begin at offset j */
15306 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
15307 utf8_printf(out, "%s\n", azHelp[j]);
15313 /* Look for commands that contain zPattern anywhere. Show the complete
15314 ** text of all commands that match. */
15315 zPat = sqlite3_mprintf("%%%s%%", zPattern);
15316 for(i=0; i<ArraySize(azHelp); i++){
15317 if( azHelp[i][0]=='.' ) j = i;
15318 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15319 utf8_printf(out, "%s\n", azHelp[j]);
15320 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
15322 utf8_printf(out, "%s\n", azHelp[j]);
15328 sqlite3_free(zPat);
15333 /* Forward reference */
15334 static int process_input(ShellState *p);
15337 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
15338 ** and return a pointer to the buffer. The caller is responsible for freeing
15341 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
15344 ** For convenience, a nul-terminator byte is always appended to the data read
15345 ** from the file before the buffer is returned. This byte is not included in
15346 ** the final value of (*pnByte), if applicable.
15348 ** NULL is returned if any error is encountered. The final value of *pnByte
15349 ** is undefined in this case.
15351 static char *readFile(const char *zName, int *pnByte){
15352 FILE *in = fopen(zName, "rb");
15356 if( in==0 ) return 0;
15357 fseek(in, 0, SEEK_END);
15360 pBuf = sqlite3_malloc64( nIn+1 );
15361 if( pBuf==0 ){ fclose(in); return 0; }
15362 nRead = fread(pBuf, nIn, 1, in);
15365 sqlite3_free(pBuf);
15369 if( pnByte ) *pnByte = nIn;
15373 #if defined(SQLITE_ENABLE_SESSION)
15375 ** Close a single OpenSession object and release all of its associated
15378 static void session_close(OpenSession *pSession){
15380 sqlite3session_delete(pSession->p);
15381 sqlite3_free(pSession->zName);
15382 for(i=0; i<pSession->nFilter; i++){
15383 sqlite3_free(pSession->azFilter[i]);
15385 sqlite3_free(pSession->azFilter);
15386 memset(pSession, 0, sizeof(OpenSession));
15391 ** Close all OpenSession objects and release all associated resources.
15393 #if defined(SQLITE_ENABLE_SESSION)
15394 static void session_close_all(ShellState *p, int i){
15396 struct AuxDb *pAuxDb = i<0 ? p->pAuxDb : &p->aAuxDb[i];
15397 for(j=0; j<pAuxDb->nSession; j++){
15398 session_close(&pAuxDb->aSession[j]);
15400 pAuxDb->nSession = 0;
15403 # define session_close_all(X,Y)
15407 ** Implementation of the xFilter function for an open session. Omit
15408 ** any tables named by ".session filter" but let all other table through.
15410 #if defined(SQLITE_ENABLE_SESSION)
15411 static int session_filter(void *pCtx, const char *zTab){
15412 OpenSession *pSession = (OpenSession*)pCtx;
15414 for(i=0; i<pSession->nFilter; i++){
15415 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
15422 ** Try to deduce the type of file for zName based on its content. Return
15423 ** one of the SHELL_OPEN_* constants.
15425 ** If the file does not exist or is empty but its name looks like a ZIP
15426 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
15427 ** Otherwise, assume an ordinary database regardless of the filename if
15428 ** the type cannot be determined from content.
15430 int deduceDatabaseType(const char *zName, int dfltZip){
15431 FILE *f = fopen(zName, "rb");
15433 int rc = SHELL_OPEN_UNSPEC;
15436 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15437 return SHELL_OPEN_ZIPFILE;
15439 return SHELL_OPEN_NORMAL;
15442 n = fread(zBuf, 16, 1, f);
15443 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
15445 return SHELL_OPEN_NORMAL;
15447 fseek(f, -25, SEEK_END);
15448 n = fread(zBuf, 25, 1, f);
15449 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
15450 rc = SHELL_OPEN_APPENDVFS;
15452 fseek(f, -22, SEEK_END);
15453 n = fread(zBuf, 22, 1, f);
15454 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
15455 && zBuf[3]==0x06 ){
15456 rc = SHELL_OPEN_ZIPFILE;
15457 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15458 rc = SHELL_OPEN_ZIPFILE;
15465 #ifndef SQLITE_OMIT_DESERIALIZE
15467 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15468 ** program. Read content from the file in p->aAuxDb[].zDbFilename.
15469 ** If p->aAuxDb[].zDbFilename is 0, then read from standard input.
15471 static unsigned char *readHexDb(ShellState *p, int *pnData){
15472 unsigned char *a = 0;
15480 const char *zDbFilename = p->pAuxDb->zDbFilename;
15481 unsigned int x[16];
15484 in = fopen(zDbFilename, "r");
15486 utf8_printf(stderr, "cannot open \"%s\" for reading\n", zDbFilename);
15493 if( in==0 ) in = stdin;
15497 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
15498 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
15499 if( rc!=2 ) goto readHexDb_error;
15500 if( n<0 ) goto readHexDb_error;
15501 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15502 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
15503 a = sqlite3_malloc( n ? n : 1 );
15505 utf8_printf(stderr, "Out of memory!\n");
15506 goto readHexDb_error;
15509 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15510 utf8_printf(stderr, "invalid pagesize\n");
15511 goto readHexDb_error;
15513 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
15514 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
15519 if( strncmp(zLine, "| end ", 6)==0 ){
15522 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
15523 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
15524 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
15527 if( k+16<=n && k>=0 ){
15529 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
15545 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
15547 if(strncmp(zLine, "| end ", 6)==0 ) break;
15552 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
15555 #endif /* SQLITE_OMIT_DESERIALIZE */
15558 ** Scalar function "shell_int32". The first argument to this function
15559 ** must be a blob. The second a non-negative integer. This function
15560 ** reads and returns a 32-bit big-endian integer from byte
15561 ** offset (4*<arg2>) of the blob.
15563 static void shellInt32(
15564 sqlite3_context *context,
15566 sqlite3_value **argv
15568 const unsigned char *pBlob;
15572 UNUSED_PARAMETER(argc);
15573 nBlob = sqlite3_value_bytes(argv[0]);
15574 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15575 iInt = sqlite3_value_int(argv[1]);
15577 if( iInt>=0 && (iInt+1)*4<=nBlob ){
15578 const unsigned char *a = &pBlob[iInt*4];
15579 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
15580 + ((sqlite3_int64)a[1]<<16)
15581 + ((sqlite3_int64)a[2]<< 8)
15582 + ((sqlite3_int64)a[3]<< 0);
15583 sqlite3_result_int64(context, iVal);
15588 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
15589 ** using "..." with internal double-quote characters doubled.
15591 static void shellIdQuote(
15592 sqlite3_context *context,
15594 sqlite3_value **argv
15596 const char *zName = (const char*)sqlite3_value_text(argv[0]);
15597 UNUSED_PARAMETER(argc);
15599 char *z = sqlite3_mprintf("\"%w\"", zName);
15600 sqlite3_result_text(context, z, -1, sqlite3_free);
15605 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
15607 static void shellUSleepFunc(
15608 sqlite3_context *context,
15610 sqlite3_value **argv
15612 int sleep = sqlite3_value_int(argv[0]);
15614 sqlite3_sleep(sleep/1000);
15615 sqlite3_result_int(context, sleep);
15619 ** Scalar function "shell_escape_crnl" used by the .recover command.
15620 ** The argument passed to this function is the output of built-in
15621 ** function quote(). If the first character of the input is "'",
15622 ** indicating that the value passed to quote() was a text value,
15623 ** then this function searches the input for "\n" and "\r" characters
15624 ** and adds a wrapper similar to the following:
15626 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
15628 ** Or, if the first character of the input is not "'", then a copy
15629 ** of the input is returned.
15631 static void shellEscapeCrnl(
15632 sqlite3_context *context,
15634 sqlite3_value **argv
15636 const char *zText = (const char*)sqlite3_value_text(argv[0]);
15637 UNUSED_PARAMETER(argc);
15638 if( zText[0]=='\'' ){
15639 int nText = sqlite3_value_bytes(argv[0]);
15643 const char *zNL = 0;
15644 const char *zCR = 0;
15648 for(i=0; zText[i]; i++){
15649 if( zNL==0 && zText[i]=='\n' ){
15650 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
15651 nNL = (int)strlen(zNL);
15653 if( zCR==0 && zText[i]=='\r' ){
15654 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
15655 nCR = (int)strlen(zCR);
15661 i64 nMax = (nNL > nCR) ? nNL : nCR;
15662 i64 nAlloc = nMax * nText + (nMax+64)*2;
15663 char *zOut = (char*)sqlite3_malloc64(nAlloc);
15665 sqlite3_result_error_nomem(context);
15670 memcpy(&zOut[iOut], "replace(replace(", 16);
15673 memcpy(&zOut[iOut], "replace(", 8);
15676 for(i=0; zText[i]; i++){
15677 if( zText[i]=='\n' ){
15678 memcpy(&zOut[iOut], zNL, nNL);
15680 }else if( zText[i]=='\r' ){
15681 memcpy(&zOut[iOut], zCR, nCR);
15684 zOut[iOut] = zText[i];
15690 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15691 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
15692 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
15695 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15696 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
15697 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
15700 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
15701 sqlite3_free(zOut);
15706 sqlite3_result_value(context, argv[0]);
15709 /* Flags for open_db().
15711 ** The default behavior of open_db() is to exit(1) if the database fails to
15712 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
15713 ** but still returns without calling exit.
15715 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
15716 ** ZIP archive if the file does not exist or is empty and its name matches
15717 ** the *.zip pattern.
15719 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
15720 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
15723 ** Make sure the database is open. If it is not, then open it. If
15724 ** the database fails to open, print an error message and exit.
15726 static void open_db(ShellState *p, int openFlags){
15728 const char *zDbFilename = p->pAuxDb->zDbFilename;
15729 if( p->openMode==SHELL_OPEN_UNSPEC ){
15730 if( zDbFilename==0 || zDbFilename[0]==0 ){
15731 p->openMode = SHELL_OPEN_NORMAL;
15733 p->openMode = (u8)deduceDatabaseType(zDbFilename,
15734 (openFlags & OPEN_DB_ZIPFILE)!=0);
15737 switch( p->openMode ){
15738 case SHELL_OPEN_APPENDVFS: {
15739 sqlite3_open_v2(zDbFilename, &p->db,
15740 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
15743 case SHELL_OPEN_HEXDB:
15744 case SHELL_OPEN_DESERIALIZE: {
15745 sqlite3_open(0, &p->db);
15748 case SHELL_OPEN_ZIPFILE: {
15749 sqlite3_open(":memory:", &p->db);
15752 case SHELL_OPEN_READONLY: {
15753 sqlite3_open_v2(zDbFilename, &p->db,
15754 SQLITE_OPEN_READONLY|p->openFlags, 0);
15757 case SHELL_OPEN_UNSPEC:
15758 case SHELL_OPEN_NORMAL: {
15759 sqlite3_open_v2(zDbFilename, &p->db,
15760 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
15765 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
15766 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
15767 zDbFilename, sqlite3_errmsg(p->db));
15768 if( openFlags & OPEN_DB_KEEPALIVE ){
15769 sqlite3_open(":memory:", &p->db);
15774 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15775 sqlite3_enable_load_extension(p->db, 1);
15777 sqlite3_fileio_init(p->db, 0, 0);
15778 sqlite3_shathree_init(p->db, 0, 0);
15779 sqlite3_completion_init(p->db, 0, 0);
15780 sqlite3_uint_init(p->db, 0, 0);
15781 sqlite3_decimal_init(p->db, 0, 0);
15782 sqlite3_regexp_init(p->db, 0, 0);
15783 sqlite3_ieee_init(p->db, 0, 0);
15784 sqlite3_series_init(p->db, 0, 0);
15785 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15786 sqlite3_dbdata_init(p->db, 0, 0);
15788 #ifdef SQLITE_HAVE_ZLIB
15789 sqlite3_zipfile_init(p->db, 0, 0);
15790 sqlite3_sqlar_init(p->db, 0, 0);
15792 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
15793 shellAddSchemaName, 0, 0);
15794 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
15795 shellModuleSchema, 0, 0);
15796 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
15797 shellPutsFunc, 0, 0);
15798 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
15799 shellEscapeCrnl, 0, 0);
15800 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
15802 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
15803 shellIdQuote, 0, 0);
15804 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
15805 shellUSleepFunc, 0, 0);
15806 #ifndef SQLITE_NOHAVE_SYSTEM
15807 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
15809 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
15812 if( p->openMode==SHELL_OPEN_ZIPFILE ){
15813 char *zSql = sqlite3_mprintf(
15814 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", zDbFilename);
15815 sqlite3_exec(p->db, zSql, 0, 0, 0);
15816 sqlite3_free(zSql);
15818 #ifndef SQLITE_OMIT_DESERIALIZE
15820 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
15823 unsigned char *aData;
15824 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
15825 aData = (unsigned char*)readFile(zDbFilename, &nData);
15827 aData = readHexDb(p, &nData);
15832 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
15833 SQLITE_DESERIALIZE_RESIZEABLE |
15834 SQLITE_DESERIALIZE_FREEONCLOSE);
15836 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
15839 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
15844 if( p->bSafeModePersist && p->db!=0 ){
15845 sqlite3_set_authorizer(p->db, safeModeAuth, p);
15850 ** Attempt to close the databaes connection. Report errors.
15852 void close_db(sqlite3 *db){
15853 int rc = sqlite3_close(db);
15855 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
15856 rc, sqlite3_errmsg(db));
15860 #if HAVE_READLINE || HAVE_EDITLINE
15862 ** Readline completion callbacks
15864 static char *readline_completion_generator(const char *text, int state){
15865 static sqlite3_stmt *pStmt = 0;
15869 sqlite3_finalize(pStmt);
15870 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15871 " FROM completion(%Q) ORDER BY 1", text);
15872 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15873 sqlite3_free(zSql);
15875 if( sqlite3_step(pStmt)==SQLITE_ROW ){
15876 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
15878 sqlite3_finalize(pStmt);
15884 static char **readline_completion(const char *zText, int iStart, int iEnd){
15885 rl_attempted_completion_over = 1;
15886 return rl_completion_matches(zText, readline_completion_generator);
15889 #elif HAVE_LINENOISE
15891 ** Linenoise completion callback
15893 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
15894 int nLine = strlen30(zLine);
15896 sqlite3_stmt *pStmt = 0;
15900 if( nLine>sizeof(zBuf)-30 ) return;
15901 if( zLine[0]=='.' || zLine[0]=='#') return;
15902 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
15903 if( i==nLine-1 ) return;
15905 memcpy(zBuf, zLine, iStart);
15906 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15907 " FROM completion(%Q,%Q) ORDER BY 1",
15908 &zLine[iStart], zLine);
15909 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15910 sqlite3_free(zSql);
15911 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15912 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15913 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15914 int nCompletion = sqlite3_column_bytes(pStmt, 0);
15915 if( iStart+nCompletion < sizeof(zBuf)-1 ){
15916 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15917 linenoiseAddCompletion(lc, zBuf);
15920 sqlite3_finalize(pStmt);
15925 ** Do C-language style dequoting.
15931 ** \v -> vertical tab
15933 ** \r -> carriage return
15938 ** \NNN -> ascii character NNN in octal
15940 static void resolve_backslashes(char *z){
15943 while( *z && *z!='\\' ) z++;
15944 for(i=j=0; (c = z[i])!=0; i++, j++){
15945 if( c=='\\' && z[i+1]!=0 ){
15949 }else if( c=='b' ){
15951 }else if( c=='t' ){
15953 }else if( c=='n' ){
15955 }else if( c=='v' ){
15957 }else if( c=='f' ){
15959 }else if( c=='r' ){
15961 }else if( c=='"' ){
15963 }else if( c=='\'' ){
15965 }else if( c=='\\' ){
15967 }else if( c>='0' && c<='7' ){
15969 if( z[i+1]>='0' && z[i+1]<='7' ){
15971 c = (c<<3) + z[i] - '0';
15972 if( z[i+1]>='0' && z[i+1]<='7' ){
15974 c = (c<<3) + z[i] - '0';
15981 if( j<i ) z[j] = 0;
15985 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
15986 ** for TRUE and FALSE. Return the integer value if appropriate.
15988 static int booleanValue(const char *zArg){
15990 if( zArg[0]=='0' && zArg[1]=='x' ){
15991 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
15993 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
15995 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
15996 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
15999 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
16002 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
16008 ** Set or clear a shell flag according to a boolean value.
16010 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
16011 if( booleanValue(zArg) ){
16012 ShellSetFlag(p, mFlag);
16014 ShellClearFlag(p, mFlag);
16019 ** Close an output file, assuming it is not stderr or stdout
16021 static void output_file_close(FILE *f){
16022 if( f && f!=stdout && f!=stderr ) fclose(f);
16026 ** Try to open an output file. The names "stdout" and "stderr" are
16027 ** recognized and do the right thing. NULL is returned if the output
16028 ** filename is "off".
16030 static FILE *output_file_open(const char *zFile, int bTextMode){
16032 if( strcmp(zFile,"stdout")==0 ){
16034 }else if( strcmp(zFile, "stderr")==0 ){
16036 }else if( strcmp(zFile, "off")==0 ){
16039 f = fopen(zFile, bTextMode ? "w" : "wb");
16041 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16047 #ifndef SQLITE_OMIT_TRACE
16049 ** A routine for handling output from sqlite3_trace().
16051 static int sql_trace_callback(
16052 unsigned mType, /* The trace type */
16053 void *pArg, /* The ShellState pointer */
16054 void *pP, /* Usually a pointer to sqlite_stmt */
16055 void *pX /* Auxiliary output */
16057 ShellState *p = (ShellState*)pArg;
16058 sqlite3_stmt *pStmt;
16061 if( p->traceOut==0 ) return 0;
16062 if( mType==SQLITE_TRACE_CLOSE ){
16063 utf8_printf(p->traceOut, "-- closing database connection\n");
16066 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
16067 zSql = (const char*)pX;
16069 pStmt = (sqlite3_stmt*)pP;
16070 switch( p->eTraceType ){
16071 case SHELL_TRACE_EXPANDED: {
16072 zSql = sqlite3_expanded_sql(pStmt);
16075 #ifdef SQLITE_ENABLE_NORMALIZE
16076 case SHELL_TRACE_NORMALIZED: {
16077 zSql = sqlite3_normalized_sql(pStmt);
16082 zSql = sqlite3_sql(pStmt);
16087 if( zSql==0 ) return 0;
16088 nSql = strlen30(zSql);
16089 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
16091 case SQLITE_TRACE_ROW:
16092 case SQLITE_TRACE_STMT: {
16093 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
16096 case SQLITE_TRACE_PROFILE: {
16097 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
16098 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
16107 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
16108 ** a useful spot to set a debugger breakpoint.
16110 static void test_breakpoint(void){
16111 static int nCall = 0;
16116 ** An object used to read a CSV and other files for import.
16118 typedef struct ImportCtx ImportCtx;
16120 const char *zFile; /* Name of the input file */
16121 FILE *in; /* Read the CSV text from this input stream */
16122 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
16123 char *z; /* Accumulated text for a field */
16124 int n; /* Number of bytes in z */
16125 int nAlloc; /* Space allocated for z[] */
16126 int nLine; /* Current line number */
16127 int nRow; /* Number of rows imported */
16128 int nErr; /* Number of errors encountered */
16129 int bNotFirst; /* True if one or more bytes already read */
16130 int cTerm; /* Character that terminated the most recent field */
16131 int cColSep; /* The column separator character. (Usually ",") */
16132 int cRowSep; /* The row separator character. (Usually "\n") */
16135 /* Clean up resourced used by an ImportCtx */
16136 static void import_cleanup(ImportCtx *p){
16137 if( p->in!=0 && p->xCloser!=0 ){
16141 sqlite3_free(p->z);
16145 /* Append a single byte to z[] */
16146 static void import_append_char(ImportCtx *p, int c){
16147 if( p->n+1>=p->nAlloc ){
16148 p->nAlloc += p->nAlloc + 100;
16149 p->z = sqlite3_realloc64(p->z, p->nAlloc);
16150 if( p->z==0 ) shell_out_of_memory();
16152 p->z[p->n++] = (char)c;
16155 /* Read a single field of CSV text. Compatible with rfc4180 and extended
16156 ** with the option of having a separator other than ",".
16158 ** + Input comes from p->in.
16159 ** + Store results in p->z of length p->n. Space to hold p->z comes
16160 ** from sqlite3_malloc64().
16161 ** + Use p->cSep as the column separator. The default is ",".
16162 ** + Use p->rSep as the row separator. The default is "\n".
16163 ** + Keep track of the line number in p->nLine.
16164 ** + Store the character that terminates the field in p->cTerm. Store
16165 ** EOF on end-of-file.
16166 ** + Report syntax errors on stderr
16168 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
16170 int cSep = p->cColSep;
16171 int rSep = p->cRowSep;
16174 if( c==EOF || seenInterrupt ){
16180 int startLine = p->nLine;
16185 if( c==rSep ) p->nLine++;
16192 if( (c==cSep && pc==cQuote)
16193 || (c==rSep && pc==cQuote)
16194 || (c==rSep && pc=='\r' && ppc==cQuote)
16195 || (c==EOF && pc==cQuote)
16197 do{ p->n--; }while( p->z[p->n]!=cQuote );
16201 if( pc==cQuote && c!='\r' ){
16202 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
16203 p->zFile, p->nLine, cQuote);
16206 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
16207 p->zFile, startLine, cQuote);
16211 import_append_char(p, c);
16216 /* If this is the first field being parsed and it begins with the
16217 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
16218 if( (c&0xff)==0xef && p->bNotFirst==0 ){
16219 import_append_char(p, c);
16221 if( (c&0xff)==0xbb ){
16222 import_append_char(p, c);
16224 if( (c&0xff)==0xbf ){
16227 return csv_read_one_field(p);
16231 while( c!=EOF && c!=cSep && c!=rSep ){
16232 import_append_char(p, c);
16237 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
16241 if( p->z ) p->z[p->n] = 0;
16246 /* Read a single field of ASCII delimited text.
16248 ** + Input comes from p->in.
16249 ** + Store results in p->z of length p->n. Space to hold p->z comes
16250 ** from sqlite3_malloc64().
16251 ** + Use p->cSep as the column separator. The default is "\x1F".
16252 ** + Use p->rSep as the row separator. The default is "\x1E".
16253 ** + Keep track of the row number in p->nLine.
16254 ** + Store the character that terminates the field in p->cTerm. Store
16255 ** EOF on end-of-file.
16256 ** + Report syntax errors on stderr
16258 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
16260 int cSep = p->cColSep;
16261 int rSep = p->cRowSep;
16264 if( c==EOF || seenInterrupt ){
16268 while( c!=EOF && c!=cSep && c!=rSep ){
16269 import_append_char(p, c);
16276 if( p->z ) p->z[p->n] = 0;
16281 ** Try to transfer data for table zTable. If an error is seen while
16282 ** moving forward, try to go backwards. The backwards movement won't
16283 ** work for WITHOUT ROWID tables.
16285 static void tryToCloneData(
16290 sqlite3_stmt *pQuery = 0;
16291 sqlite3_stmt *pInsert = 0;
16296 int nTable = strlen30(zTable);
16299 const int spinRate = 10000;
16301 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16302 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16304 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16305 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16307 goto end_data_xfer;
16309 n = sqlite3_column_count(pQuery);
16310 zInsert = sqlite3_malloc64(200 + nTable + n*3);
16311 if( zInsert==0 ) shell_out_of_memory();
16312 sqlite3_snprintf(200+nTable,zInsert,
16313 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16314 i = strlen30(zInsert);
16315 for(j=1; j<n; j++){
16316 memcpy(zInsert+i, ",?", 2);
16319 memcpy(zInsert+i, ");", 3);
16320 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
16322 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16323 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
16325 goto end_data_xfer;
16327 for(k=0; k<2; k++){
16328 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16329 for(i=0; i<n; i++){
16330 switch( sqlite3_column_type(pQuery, i) ){
16331 case SQLITE_NULL: {
16332 sqlite3_bind_null(pInsert, i+1);
16335 case SQLITE_INTEGER: {
16336 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
16339 case SQLITE_FLOAT: {
16340 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
16343 case SQLITE_TEXT: {
16344 sqlite3_bind_text(pInsert, i+1,
16345 (const char*)sqlite3_column_text(pQuery,i),
16346 -1, SQLITE_STATIC);
16349 case SQLITE_BLOB: {
16350 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
16351 sqlite3_column_bytes(pQuery,i),
16357 rc = sqlite3_step(pInsert);
16358 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
16359 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
16360 sqlite3_errmsg(newDb));
16362 sqlite3_reset(pInsert);
16364 if( (cnt%spinRate)==0 ){
16365 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
16369 if( rc==SQLITE_DONE ) break;
16370 sqlite3_finalize(pQuery);
16371 sqlite3_free(zQuery);
16372 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16374 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16376 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16379 } /* End for(k=0...) */
16382 sqlite3_finalize(pQuery);
16383 sqlite3_finalize(pInsert);
16384 sqlite3_free(zQuery);
16385 sqlite3_free(zInsert);
16390 ** Try to transfer all rows of the schema that match zWhere. For
16391 ** each row, invoke xForEach() on the object defined by that row.
16392 ** If an error is encountered while moving forward through the
16393 ** sqlite_schema table, try again moving backwards.
16395 static void tryToCloneSchema(
16398 const char *zWhere,
16399 void (*xForEach)(ShellState*,sqlite3*,const char*)
16401 sqlite3_stmt *pQuery = 0;
16404 const unsigned char *zName;
16405 const unsigned char *zSql;
16408 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16409 " WHERE %s", zWhere);
16410 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16412 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16413 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16415 goto end_schema_xfer;
16417 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16418 zName = sqlite3_column_text(pQuery, 0);
16419 zSql = sqlite3_column_text(pQuery, 1);
16420 printf("%s... ", zName); fflush(stdout);
16421 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16423 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16424 sqlite3_free(zErrMsg);
16428 xForEach(p, newDb, (const char*)zName);
16432 if( rc!=SQLITE_DONE ){
16433 sqlite3_finalize(pQuery);
16434 sqlite3_free(zQuery);
16435 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16436 " WHERE %s ORDER BY rowid DESC", zWhere);
16437 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16439 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16440 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16442 goto end_schema_xfer;
16444 while( sqlite3_step(pQuery)==SQLITE_ROW ){
16445 zName = sqlite3_column_text(pQuery, 0);
16446 zSql = sqlite3_column_text(pQuery, 1);
16447 printf("%s... ", zName); fflush(stdout);
16448 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16450 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16451 sqlite3_free(zErrMsg);
16455 xForEach(p, newDb, (const char*)zName);
16461 sqlite3_finalize(pQuery);
16462 sqlite3_free(zQuery);
16466 ** Open a new database file named "zNewDb". Try to recover as much information
16467 ** as possible out of the main database (which might be corrupt) and write it
16470 static void tryToClone(ShellState *p, const char *zNewDb){
16472 sqlite3 *newDb = 0;
16473 if( access(zNewDb,0)==0 ){
16474 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
16477 rc = sqlite3_open(zNewDb, &newDb);
16479 utf8_printf(stderr, "Cannot create output database: %s\n",
16480 sqlite3_errmsg(newDb));
16482 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
16483 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
16484 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
16485 tryToCloneSchema(p, newDb, "type!='table'", 0);
16486 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
16487 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16493 ** Change the output file back to stdout.
16495 ** If the p->doXdgOpen flag is set, that means the output was being
16496 ** redirected to a temporary file named by p->zTempFile. In that case,
16497 ** launch start/open/xdg-open on that temporary file.
16499 static void output_reset(ShellState *p){
16500 if( p->outfile[0]=='|' ){
16501 #ifndef SQLITE_OMIT_POPEN
16505 output_file_close(p->out);
16506 #ifndef SQLITE_NOHAVE_SYSTEM
16507 if( p->doXdgOpen ){
16508 const char *zXdgOpenCmd =
16509 #if defined(_WIN32)
16511 #elif defined(__APPLE__)
16517 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
16518 if( system(zCmd) ){
16519 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
16521 /* Give the start/open/xdg-open command some time to get
16522 ** going before we continue, and potential delete the
16523 ** p->zTempFile data file out from under it */
16524 sqlite3_sleep(2000);
16526 sqlite3_free(zCmd);
16530 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
16537 ** Run an SQL command and return the single integer result.
16539 static int db_int(ShellState *p, const char *zSql){
16540 sqlite3_stmt *pStmt;
16542 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16543 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16544 res = sqlite3_column_int(pStmt,0);
16546 sqlite3_finalize(pStmt);
16551 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16553 static unsigned int get2byteInt(unsigned char *a){
16554 return (a[0]<<8) + a[1];
16556 static unsigned int get4byteInt(unsigned char *a){
16557 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
16561 ** Implementation of the ".dbinfo" command.
16563 ** Return 1 on error, 2 to exit, and 0 otherwise.
16565 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
16566 static const struct { const char *zName; int ofst; } aField[] = {
16567 { "file change counter:", 24 },
16568 { "database page count:", 28 },
16569 { "freelist page count:", 36 },
16570 { "schema cookie:", 40 },
16571 { "schema format:", 44 },
16572 { "default cache size:", 48 },
16573 { "autovacuum top root:", 52 },
16574 { "incremental vacuum:", 64 },
16575 { "text encoding:", 56 },
16576 { "user version:", 60 },
16577 { "application id:", 68 },
16578 { "software version:", 96 },
16580 static const struct { const char *zName; const char *zSql; } aQuery[] = {
16581 { "number of tables:",
16582 "SELECT count(*) FROM %s WHERE type='table'" },
16583 { "number of indexes:",
16584 "SELECT count(*) FROM %s WHERE type='index'" },
16585 { "number of triggers:",
16586 "SELECT count(*) FROM %s WHERE type='trigger'" },
16587 { "number of views:",
16588 "SELECT count(*) FROM %s WHERE type='view'" },
16590 "SELECT total(length(sql)) FROM %s" },
16593 unsigned iDataVersion;
16595 char *zDb = nArg>=2 ? azArg[1] : "main";
16596 sqlite3_stmt *pStmt = 0;
16597 unsigned char aHdr[100];
16599 if( p->db==0 ) return 1;
16600 rc = sqlite3_prepare_v2(p->db,
16601 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
16604 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
16605 sqlite3_finalize(pStmt);
16608 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
16609 if( sqlite3_step(pStmt)==SQLITE_ROW
16610 && sqlite3_column_bytes(pStmt,0)>100
16612 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
16613 sqlite3_finalize(pStmt);
16615 raw_printf(stderr, "unable to read database header\n");
16616 sqlite3_finalize(pStmt);
16619 i = get2byteInt(aHdr+16);
16620 if( i==1 ) i = 65536;
16621 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
16622 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
16623 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
16624 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
16625 for(i=0; i<ArraySize(aField); i++){
16626 int ofst = aField[i].ofst;
16627 unsigned int val = get4byteInt(aHdr + ofst);
16628 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
16631 if( val==1 ) raw_printf(p->out, " (utf8)");
16632 if( val==2 ) raw_printf(p->out, " (utf16le)");
16633 if( val==3 ) raw_printf(p->out, " (utf16be)");
16636 raw_printf(p->out, "\n");
16639 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
16640 }else if( strcmp(zDb,"temp")==0 ){
16641 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
16643 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
16645 for(i=0; i<ArraySize(aQuery); i++){
16646 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
16647 int val = db_int(p, zSql);
16648 sqlite3_free(zSql);
16649 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
16651 sqlite3_free(zSchemaTab);
16652 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
16653 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
16658 ** Print the current sqlite3_errmsg() value to stderr and return 1.
16660 static int shellDatabaseError(sqlite3 *db){
16661 const char *zErr = sqlite3_errmsg(db);
16662 utf8_printf(stderr, "Error: %s\n", zErr);
16667 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
16668 ** if they match and FALSE (0) if they do not match.
16672 ** '*' Matches any sequence of zero or more characters.
16674 ** '?' Matches exactly one character.
16676 ** [...] Matches one character from the enclosed list of
16679 ** [^...] Matches one character not in the enclosed list.
16681 ** '#' Matches any sequence of one or more digits with an
16682 ** optional + or - sign in front
16684 ** ' ' Any span of whitespace matches any other span of
16687 ** Extra whitespace at the end of z[] is ignored.
16689 static int testcase_glob(const char *zGlob, const char *z){
16694 while( (c = (*(zGlob++)))!=0 ){
16696 if( !IsSpace(*z) ) return 0;
16697 while( IsSpace(*zGlob) ) zGlob++;
16698 while( IsSpace(*z) ) z++;
16699 }else if( c=='*' ){
16700 while( (c=(*(zGlob++))) == '*' || c=='?' ){
16701 if( c=='?' && (*(z++))==0 ) return 0;
16705 }else if( c=='[' ){
16706 while( *z && testcase_glob(zGlob-1,z)==0 ){
16711 while( (c2 = (*(z++)))!=0 ){
16714 if( c2==0 ) return 0;
16716 if( testcase_glob(zGlob,z) ) return 1;
16719 }else if( c=='?' ){
16720 if( (*(z++))==0 ) return 0;
16721 }else if( c=='[' ){
16726 if( c==0 ) return 0;
16733 if( c==']' ) seen = 1;
16736 while( c2 && c2!=']' ){
16737 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
16739 if( c>=prior_c && c<=c2 ) seen = 1;
16749 if( c2==0 || (seen ^ invert)==0 ) return 0;
16750 }else if( c=='#' ){
16751 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
16752 if( !IsDigit(z[0]) ) return 0;
16754 while( IsDigit(z[0]) ){ z++; }
16756 if( c!=(*(z++)) ) return 0;
16759 while( IsSpace(*z) ){ z++; }
16765 ** Compare the string as a command-line option with either one or two
16766 ** initial "-" characters.
16768 static int optionMatch(const char *zStr, const char *zOpt){
16769 if( zStr[0]!='-' ) return 0;
16771 if( zStr[0]=='-' ) zStr++;
16772 return strcmp(zStr, zOpt)==0;
16778 int shellDeleteFile(const char *zFilename){
16781 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
16785 rc = unlink(zFilename);
16791 ** Try to delete the temporary file (if there is one) and free the
16792 ** memory used to hold the name of the temp file.
16794 static void clearTempFile(ShellState *p){
16795 if( p->zTempFile==0 ) return;
16796 if( p->doXdgOpen ) return;
16797 if( shellDeleteFile(p->zTempFile) ) return;
16798 sqlite3_free(p->zTempFile);
16803 ** Create a new temp file name with the given suffix.
16805 static void newTempFile(ShellState *p, const char *zSuffix){
16807 sqlite3_free(p->zTempFile);
16810 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
16812 if( p->zTempFile==0 ){
16813 /* If p->db is an in-memory database then the TEMPFILENAME file-control
16814 ** will not work and we will need to fallback to guessing */
16817 sqlite3_randomness(sizeof(r), &r);
16818 zTemp = getenv("TEMP");
16819 if( zTemp==0 ) zTemp = getenv("TMP");
16827 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16829 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16831 if( p->zTempFile==0 ){
16832 shell_out_of_memory();
16838 ** The implementation of SQL scalar function fkey_collate_clause(), used
16839 ** by the ".lint fkey-indexes" command. This scalar function is always
16840 ** called with four arguments - the parent table name, the parent column name,
16841 ** the child table name and the child column name.
16843 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
16845 ** If either of the named tables or columns do not exist, this function
16846 ** returns an empty string. An empty string is also returned if both tables
16847 ** and columns exist but have the same default collation sequence. Or,
16848 ** if both exist but the default collation sequences are different, this
16849 ** function returns the string " COLLATE <parent-collation>", where
16850 ** <parent-collation> is the default collation sequence of the parent column.
16852 static void shellFkeyCollateClause(
16853 sqlite3_context *pCtx,
16855 sqlite3_value **apVal
16857 sqlite3 *db = sqlite3_context_db_handle(pCtx);
16858 const char *zParent;
16859 const char *zParentCol;
16860 const char *zParentSeq;
16861 const char *zChild;
16862 const char *zChildCol;
16863 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
16867 zParent = (const char*)sqlite3_value_text(apVal[0]);
16868 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
16869 zChild = (const char*)sqlite3_value_text(apVal[2]);
16870 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
16872 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
16873 rc = sqlite3_table_column_metadata(
16874 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
16876 if( rc==SQLITE_OK ){
16877 rc = sqlite3_table_column_metadata(
16878 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
16882 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
16883 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
16884 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
16891 ** The implementation of dot-command ".lint fkey-indexes".
16893 static int lintFkeyIndexes(
16894 ShellState *pState, /* Current shell tool state */
16895 char **azArg, /* Array of arguments passed to dot command */
16896 int nArg /* Number of entries in azArg[] */
16898 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
16899 FILE *out = pState->out; /* Stream to write non-error output to */
16900 int bVerbose = 0; /* If -verbose is present */
16901 int bGroupByParent = 0; /* If -groupbyparent is present */
16902 int i; /* To iterate through azArg[] */
16903 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
16904 int rc; /* Return code */
16905 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
16908 ** This SELECT statement returns one row for each foreign key constraint
16909 ** in the schema of the main database. The column values are:
16911 ** 0. The text of an SQL statement similar to:
16913 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
16915 ** This SELECT is similar to the one that the foreign keys implementation
16916 ** needs to run internally on child tables. If there is an index that can
16917 ** be used to optimize this query, then it can also be used by the FK
16918 ** implementation to optimize DELETE or UPDATE statements on the parent
16921 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
16922 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
16923 ** contains an index that can be used to optimize the query.
16925 ** 2. Human readable text that describes the child table and columns. e.g.
16927 ** "child_table(child_key1, child_key2)"
16929 ** 3. Human readable text that describes the parent table and columns. e.g.
16931 ** "parent_table(parent_key1, parent_key2)"
16933 ** 4. A full CREATE INDEX statement for an index that could be used to
16934 ** optimize DELETE or UPDATE statements on the parent table. e.g.
16936 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
16938 ** 5. The name of the parent table.
16940 ** These six values are used by the C logic below to generate the report.
16944 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
16945 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
16946 " || fkey_collate_clause("
16947 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
16949 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
16950 " || group_concat('*=?', ' AND ') || ')'"
16952 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
16954 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
16956 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
16957 " || ' ON ' || quote(s.name) || '('"
16958 " || group_concat(quote(f.[from]) ||"
16959 " fkey_collate_clause("
16960 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
16964 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
16965 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
16966 "GROUP BY s.name, f.id "
16967 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
16969 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
16971 for(i=2; i<nArg; i++){
16972 int n = strlen30(azArg[i]);
16973 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
16976 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
16977 bGroupByParent = 1;
16981 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
16984 return SQLITE_ERROR;
16988 /* Register the fkey_collate_clause() SQL function */
16989 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
16990 0, shellFkeyCollateClause, 0, 0
16994 if( rc==SQLITE_OK ){
16995 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
16997 if( rc==SQLITE_OK ){
16998 sqlite3_bind_int(pSql, 1, bGroupByParent);
17001 if( rc==SQLITE_OK ){
17004 while( SQLITE_ROW==sqlite3_step(pSql) ){
17006 sqlite3_stmt *pExplain = 0;
17007 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
17008 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
17009 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
17010 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
17011 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
17012 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
17014 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
17015 if( rc!=SQLITE_OK ) break;
17016 if( SQLITE_ROW==sqlite3_step(pExplain) ){
17017 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
17019 0==sqlite3_strglob(zGlob, zPlan)
17020 || 0==sqlite3_strglob(zGlobIPK, zPlan)
17023 rc = sqlite3_finalize(pExplain);
17024 if( rc!=SQLITE_OK ) break;
17027 raw_printf(stderr, "Error: internal error");
17031 && (bVerbose || res==0)
17032 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
17034 raw_printf(out, "-- Parent table %s\n", zParent);
17035 sqlite3_free(zPrev);
17036 zPrev = sqlite3_mprintf("%s", zParent);
17040 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
17041 }else if( bVerbose ){
17042 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
17043 zIndent, zFrom, zTarget
17048 sqlite3_free(zPrev);
17050 if( rc!=SQLITE_OK ){
17051 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17054 rc2 = sqlite3_finalize(pSql);
17055 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
17057 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17060 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
17067 ** Implementation of ".lint" dot command.
17069 static int lintDotCommand(
17070 ShellState *pState, /* Current shell tool state */
17071 char **azArg, /* Array of arguments passed to dot command */
17072 int nArg /* Number of entries in azArg[] */
17075 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
17076 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
17077 return lintFkeyIndexes(pState, azArg, nArg);
17080 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
17081 raw_printf(stderr, "Where sub-commands are:\n");
17082 raw_printf(stderr, " fkey-indexes\n");
17083 return SQLITE_ERROR;
17086 #if !defined SQLITE_OMIT_VIRTUALTABLE
17087 static void shellPrepare(
17091 sqlite3_stmt **ppStmt
17094 if( *pRc==SQLITE_OK ){
17095 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
17096 if( rc!=SQLITE_OK ){
17097 raw_printf(stderr, "sql error: %s (%d)\n",
17098 sqlite3_errmsg(db), sqlite3_errcode(db)
17106 ** Create a prepared statement using printf-style arguments for the SQL.
17108 ** This routine is could be marked "static". But it is not always used,
17109 ** depending on compile-time options. By omitting the "static", we avoid
17110 ** nuisance compiler warnings about "defined but not used".
17112 void shellPreparePrintf(
17115 sqlite3_stmt **ppStmt,
17120 if( *pRc==SQLITE_OK ){
17123 va_start(ap, zFmt);
17124 z = sqlite3_vmprintf(zFmt, ap);
17127 *pRc = SQLITE_NOMEM;
17129 shellPrepare(db, pRc, z, ppStmt);
17135 /* Finalize the prepared statement created using shellPreparePrintf().
17137 ** This routine is could be marked "static". But it is not always used,
17138 ** depending on compile-time options. By omitting the "static", we avoid
17139 ** nuisance compiler warnings about "defined but not used".
17141 void shellFinalize(
17143 sqlite3_stmt *pStmt
17146 sqlite3 *db = sqlite3_db_handle(pStmt);
17147 int rc = sqlite3_finalize(pStmt);
17148 if( *pRc==SQLITE_OK ){
17149 if( rc!=SQLITE_OK ){
17150 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17157 /* Reset the prepared statement created using shellPreparePrintf().
17159 ** This routine is could be marked "static". But it is not always used,
17160 ** depending on compile-time options. By omitting the "static", we avoid
17161 ** nuisance compiler warnings about "defined but not used".
17165 sqlite3_stmt *pStmt
17167 int rc = sqlite3_reset(pStmt);
17168 if( *pRc==SQLITE_OK ){
17169 if( rc!=SQLITE_OK ){
17170 sqlite3 *db = sqlite3_db_handle(pStmt);
17171 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
17176 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
17178 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17179 /******************************************************************************
17180 ** The ".archive" or ".ar" command.
17183 ** Structure representing a single ".ar" command.
17185 typedef struct ArCommand ArCommand;
17187 u8 eCmd; /* An AR_CMD_* value */
17188 u8 bVerbose; /* True if --verbose */
17189 u8 bZip; /* True if the archive is a ZIP */
17190 u8 bDryRun; /* True if --dry-run */
17191 u8 bAppend; /* True if --append */
17192 u8 bGlob; /* True if --glob */
17193 u8 fromCmdLine; /* Run from -A instead of .archive */
17194 int nArg; /* Number of command arguments */
17195 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
17196 const char *zFile; /* --file argument, or NULL */
17197 const char *zDir; /* --directory argument, or NULL */
17198 char **azArg; /* Array of command arguments */
17199 ShellState *p; /* Shell state */
17200 sqlite3 *db; /* Database containing the archive */
17204 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
17206 static int arUsage(FILE *f){
17207 showHelp(f,"archive");
17208 return SQLITE_ERROR;
17212 ** Print an error message for the .ar command to stderr and return
17215 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
17218 va_start(ap, zFmt);
17219 z = sqlite3_vmprintf(zFmt, ap);
17221 utf8_printf(stderr, "Error: %s\n", z);
17222 if( pAr->fromCmdLine ){
17223 utf8_printf(stderr, "Use \"-A\" for more help\n");
17225 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
17228 return SQLITE_ERROR;
17232 ** Values for ArCommand.eCmd.
17234 #define AR_CMD_CREATE 1
17235 #define AR_CMD_UPDATE 2
17236 #define AR_CMD_INSERT 3
17237 #define AR_CMD_EXTRACT 4
17238 #define AR_CMD_LIST 5
17239 #define AR_CMD_HELP 6
17240 #define AR_CMD_REMOVE 7
17243 ** Other (non-command) switches.
17245 #define AR_SWITCH_VERBOSE 8
17246 #define AR_SWITCH_FILE 9
17247 #define AR_SWITCH_DIRECTORY 10
17248 #define AR_SWITCH_APPEND 11
17249 #define AR_SWITCH_DRYRUN 12
17250 #define AR_SWITCH_GLOB 13
17252 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
17254 case AR_CMD_CREATE:
17255 case AR_CMD_EXTRACT:
17257 case AR_CMD_REMOVE:
17258 case AR_CMD_UPDATE:
17259 case AR_CMD_INSERT:
17262 return arErrorMsg(pAr, "multiple command options");
17264 pAr->eCmd = eSwitch;
17267 case AR_SWITCH_DRYRUN:
17270 case AR_SWITCH_GLOB:
17273 case AR_SWITCH_VERBOSE:
17276 case AR_SWITCH_APPEND:
17278 /* Fall thru into --file */
17279 case AR_SWITCH_FILE:
17282 case AR_SWITCH_DIRECTORY:
17291 ** Parse the command line for an ".ar" command. The results are written into
17292 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
17293 ** successfully, otherwise an error message is written to stderr and
17294 ** SQLITE_ERROR returned.
17296 static int arParseCommand(
17297 char **azArg, /* Array of arguments passed to dot command */
17298 int nArg, /* Number of entries in azArg[] */
17299 ArCommand *pAr /* Populate this object */
17307 { "create", 'c', AR_CMD_CREATE, 0 },
17308 { "extract", 'x', AR_CMD_EXTRACT, 0 },
17309 { "insert", 'i', AR_CMD_INSERT, 0 },
17310 { "list", 't', AR_CMD_LIST, 0 },
17311 { "remove", 'r', AR_CMD_REMOVE, 0 },
17312 { "update", 'u', AR_CMD_UPDATE, 0 },
17313 { "help", 'h', AR_CMD_HELP, 0 },
17314 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
17315 { "file", 'f', AR_SWITCH_FILE, 1 },
17316 { "append", 'a', AR_SWITCH_APPEND, 1 },
17317 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
17318 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
17319 { "glob", 'g', AR_SWITCH_GLOB, 0 },
17321 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
17322 struct ArSwitch *pEnd = &aSwitch[nSwitch];
17325 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
17326 return arUsage(stderr);
17328 char *z = azArg[1];
17330 /* Traditional style [tar] invocation */
17333 for(i=0; z[i]; i++){
17334 const char *zArg = 0;
17335 struct ArSwitch *pOpt;
17336 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17337 if( z[i]==pOpt->cShort ) break;
17340 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17344 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
17346 zArg = azArg[iArg++];
17348 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17350 pAr->nArg = nArg-iArg;
17352 pAr->azArg = &azArg[iArg];
17355 /* Non-traditional invocation */
17357 for(iArg=1; iArg<nArg; iArg++){
17361 /* All remaining command line words are command arguments. */
17362 pAr->azArg = &azArg[iArg];
17363 pAr->nArg = nArg-iArg;
17370 /* One or more short options */
17371 for(i=1; i<n; i++){
17372 const char *zArg = 0;
17373 struct ArSwitch *pOpt;
17374 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17375 if( z[i]==pOpt->cShort ) break;
17378 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17385 if( iArg>=(nArg-1) ){
17386 return arErrorMsg(pAr, "option requires an argument: %c",
17389 zArg = azArg[++iArg];
17392 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17394 }else if( z[2]=='\0' ){
17395 /* A -- option, indicating that all remaining command line words
17396 ** are command arguments. */
17397 pAr->azArg = &azArg[iArg+1];
17398 pAr->nArg = nArg-iArg-1;
17401 /* A long option */
17402 const char *zArg = 0; /* Argument for option, if any */
17403 struct ArSwitch *pMatch = 0; /* Matching option */
17404 struct ArSwitch *pOpt; /* Iterator */
17405 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17406 const char *zLong = pOpt->zLong;
17407 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
17409 return arErrorMsg(pAr, "ambiguous option: %s",z);
17417 return arErrorMsg(pAr, "unrecognized option: %s", z);
17419 if( pMatch->bArg ){
17420 if( iArg>=(nArg-1) ){
17421 return arErrorMsg(pAr, "option requires an argument: %s", z);
17423 zArg = azArg[++iArg];
17425 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
17435 ** This function assumes that all arguments within the ArCommand.azArg[]
17436 ** array refer to archive members, as for the --extract, --list or --remove
17437 ** commands. It checks that each of them are "present". If any specified
17438 ** file is not present in the archive, an error is printed to stderr and an
17439 ** error code returned. Otherwise, if all specified arguments are present
17440 ** in the archive, SQLITE_OK is returned. Here, "present" means either an
17441 ** exact equality when pAr->bGlob is false or a "name GLOB pattern" match
17442 ** when pAr->bGlob is true.
17444 ** This function strips any trailing '/' characters from each argument.
17445 ** This is consistent with the way the [tar] command seems to work on
17448 static int arCheckEntries(ArCommand *pAr){
17449 int rc = SQLITE_OK;
17452 sqlite3_stmt *pTest = 0;
17453 const char *zSel = (pAr->bGlob)
17454 ? "SELECT name FROM %s WHERE glob($name,name)"
17455 : "SELECT name FROM %s WHERE name=$name";
17457 shellPreparePrintf(pAr->db, &rc, &pTest, zSel, pAr->zSrcTable);
17458 j = sqlite3_bind_parameter_index(pTest, "$name");
17459 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17460 char *z = pAr->azArg[i];
17461 int n = strlen30(z);
17463 while( n>0 && z[n-1]=='/' ) n--;
17465 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
17466 if( SQLITE_ROW==sqlite3_step(pTest) ){
17469 shellReset(&rc, pTest);
17470 if( rc==SQLITE_OK && bOk==0 ){
17471 utf8_printf(stderr, "not found in archive: %s\n", z);
17475 shellFinalize(&rc, pTest);
17481 ** Format a WHERE clause that can be used against the "sqlar" table to
17482 ** identify all archive members that match the command arguments held
17483 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
17484 ** The caller is responsible for eventually calling sqlite3_free() on
17485 ** any non-NULL (*pzWhere) value. Here, "match" means strict equality
17486 ** when pAr->bGlob is false and GLOB match when pAr->bGlob is true.
17488 static void arWhereClause(
17491 char **pzWhere /* OUT: New WHERE clause */
17494 const char *zSameOp = (pAr->bGlob)? "GLOB" : "=";
17495 if( *pRc==SQLITE_OK ){
17496 if( pAr->nArg==0 ){
17497 zWhere = sqlite3_mprintf("1");
17500 const char *zSep = "";
17501 for(i=0; i<pAr->nArg; i++){
17502 const char *z = pAr->azArg[i];
17503 zWhere = sqlite3_mprintf(
17504 "%z%s name %s '%q' OR substr(name,1,%d) %s '%q/'",
17505 zWhere, zSep, zSameOp, z, strlen30(z)+1, zSameOp, z
17508 *pRc = SQLITE_NOMEM;
17519 ** Implementation of .ar "lisT" command.
17521 static int arListCommand(ArCommand *pAr){
17522 const char *zSql = "SELECT %s FROM %s WHERE %s";
17523 const char *azCols[] = {
17525 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
17529 sqlite3_stmt *pSql = 0;
17532 rc = arCheckEntries(pAr);
17533 arWhereClause(&rc, pAr, &zWhere);
17535 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
17536 pAr->zSrcTable, zWhere);
17537 if( pAr->bDryRun ){
17538 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17540 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17541 if( pAr->bVerbose ){
17542 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
17543 sqlite3_column_text(pSql, 0),
17544 sqlite3_column_int(pSql, 1),
17545 sqlite3_column_text(pSql, 2),
17546 sqlite3_column_text(pSql, 3)
17549 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17553 shellFinalize(&rc, pSql);
17554 sqlite3_free(zWhere);
17560 ** Implementation of .ar "Remove" command.
17562 static int arRemoveCommand(ArCommand *pAr){
17568 /* Verify that args actually exist within the archive before proceeding.
17569 ** And formulate a WHERE clause to match them. */
17570 rc = arCheckEntries(pAr);
17571 arWhereClause(&rc, pAr, &zWhere);
17573 if( rc==SQLITE_OK ){
17574 zSql = sqlite3_mprintf("DELETE FROM %s WHERE %s;",
17575 pAr->zSrcTable, zWhere);
17576 if( pAr->bDryRun ){
17577 utf8_printf(pAr->p->out, "%s\n", zSql);
17580 rc = sqlite3_exec(pAr->db, "SAVEPOINT ar;", 0, 0, 0);
17581 if( rc==SQLITE_OK ){
17582 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17583 if( rc!=SQLITE_OK ){
17584 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17586 rc = sqlite3_exec(pAr->db, "RELEASE ar;", 0, 0, 0);
17590 utf8_printf(stdout, "ERROR: %s\n", zErr);
17591 sqlite3_free(zErr);
17595 sqlite3_free(zWhere);
17596 sqlite3_free(zSql);
17601 ** Implementation of .ar "eXtract" command.
17603 static int arExtractCommand(ArCommand *pAr){
17604 const char *zSql1 =
17607 " writefile(($dir || name), %s, mode, mtime) "
17608 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
17609 " AND name NOT GLOB '*..[/\\]*'";
17611 const char *azExtraArg[] = {
17612 "sqlar_uncompress(data, sz)",
17616 sqlite3_stmt *pSql = 0;
17617 int rc = SQLITE_OK;
17622 /* If arguments are specified, check that they actually exist within
17623 ** the archive before proceeding. And formulate a WHERE clause to
17625 rc = arCheckEntries(pAr);
17626 arWhereClause(&rc, pAr, &zWhere);
17628 if( rc==SQLITE_OK ){
17630 zDir = sqlite3_mprintf("%s/", pAr->zDir);
17632 zDir = sqlite3_mprintf("");
17634 if( zDir==0 ) rc = SQLITE_NOMEM;
17637 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
17638 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
17641 if( rc==SQLITE_OK ){
17642 j = sqlite3_bind_parameter_index(pSql, "$dir");
17643 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
17645 /* Run the SELECT statement twice. The first time, writefile() is called
17646 ** for all archive members that should be extracted. The second time,
17647 ** only for the directories. This is because the timestamps for
17648 ** extracted directories must be reset after they are populated (as
17649 ** populating them changes the timestamp). */
17650 for(i=0; i<2; i++){
17651 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
17652 sqlite3_bind_int(pSql, j, i);
17653 if( pAr->bDryRun ){
17654 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17656 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17657 if( i==0 && pAr->bVerbose ){
17658 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17662 shellReset(&rc, pSql);
17664 shellFinalize(&rc, pSql);
17667 sqlite3_free(zDir);
17668 sqlite3_free(zWhere);
17673 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
17675 static int arExecSql(ArCommand *pAr, const char *zSql){
17677 if( pAr->bDryRun ){
17678 utf8_printf(pAr->p->out, "%s\n", zSql);
17682 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17684 utf8_printf(stdout, "ERROR: %s\n", zErr);
17685 sqlite3_free(zErr);
17693 ** Implementation of .ar "create", "insert", and "update" commands.
17695 ** create -> Create a new SQL archive
17696 ** insert -> Insert or reinsert all files listed
17697 ** update -> Insert files that have changed or that were not
17698 ** previously in the archive
17700 ** Create the "sqlar" table in the database if it does not already exist.
17701 ** Then add each file in the azFile[] array to the archive. Directories
17702 ** are added recursively. If argument bVerbose is non-zero, a message is
17703 ** printed on stdout for each file archived.
17705 ** The create command is the same as update, except that it drops
17706 ** any existing "sqlar" table before beginning. The "insert" command
17707 ** always overwrites every file named on the command-line, where as
17708 ** "update" only overwrites if the size or mtime or mode has changed.
17710 static int arCreateOrUpdateCommand(
17711 ArCommand *pAr, /* Command arguments and options */
17712 int bUpdate, /* true for a --create. */
17713 int bOnlyIfChanged /* Only update if file has changed */
17715 const char *zCreate =
17716 "CREATE TABLE IF NOT EXISTS sqlar(\n"
17717 " name TEXT PRIMARY KEY, -- name of the file\n"
17718 " mode INT, -- access permissions\n"
17719 " mtime INT, -- last modification time\n"
17720 " sz INT, -- original file size\n"
17721 " data BLOB -- compressed content\n"
17723 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
17724 const char *zInsertFmt[2] = {
17725 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
17730 " CASE substr(lsmode(mode),1,1)\n"
17731 " WHEN '-' THEN length(data)\n"
17732 " WHEN 'd' THEN 0\n"
17734 " sqlar_compress(data)\n"
17735 " FROM fsdir(%Q,%Q) AS disk\n"
17736 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17738 "REPLACE INTO %s(name,mode,mtime,data)\n"
17744 " FROM fsdir(%Q,%Q) AS disk\n"
17745 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17747 int i; /* For iterating through azFile[] */
17748 int rc; /* Return code */
17749 const char *zTab = 0; /* SQL table into which to insert */
17754 arExecSql(pAr, "PRAGMA page_size=512");
17755 rc = arExecSql(pAr, "SAVEPOINT ar;");
17756 if( rc!=SQLITE_OK ) return rc;
17759 /* Initialize the zipfile virtual table, if necessary */
17762 sqlite3_randomness(sizeof(r),&r);
17763 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
17765 zSql = sqlite3_mprintf(
17766 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
17769 rc = arExecSql(pAr, zSql);
17770 sqlite3_free(zSql);
17775 /* Initialize the table for an SQLAR */
17778 rc = arExecSql(pAr, zDrop);
17779 if( rc!=SQLITE_OK ) goto end_ar_transaction;
17781 rc = arExecSql(pAr, zCreate);
17783 if( bOnlyIfChanged ){
17784 zExists = sqlite3_mprintf(
17786 "SELECT 1 FROM %s AS mem"
17787 " WHERE mem.name=disk.name"
17788 " AND mem.mtime=disk.mtime"
17789 " AND mem.mode=disk.mode)", zTab);
17791 zExists = sqlite3_mprintf("");
17793 if( zExists==0 ) rc = SQLITE_NOMEM;
17794 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17795 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
17796 pAr->bVerbose ? "shell_putsnl(name)" : "name",
17797 pAr->azArg[i], pAr->zDir, zExists);
17798 rc = arExecSql(pAr, zSql2);
17799 sqlite3_free(zSql2);
17801 end_ar_transaction:
17802 if( rc!=SQLITE_OK ){
17803 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17805 rc = arExecSql(pAr, "RELEASE ar;");
17806 if( pAr->bZip && pAr->zFile ){
17807 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
17808 arExecSql(pAr, zSql);
17809 sqlite3_free(zSql);
17812 sqlite3_free(zExists);
17817 ** Implementation of ".ar" dot command.
17819 static int arDotCommand(
17820 ShellState *pState, /* Current shell tool state */
17821 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
17822 char **azArg, /* Array of arguments passed to dot command */
17823 int nArg /* Number of entries in azArg[] */
17827 memset(&cmd, 0, sizeof(cmd));
17828 cmd.fromCmdLine = fromCmdLine;
17829 rc = arParseCommand(azArg, nArg, &cmd);
17830 if( rc==SQLITE_OK ){
17831 int eDbType = SHELL_OPEN_UNSPEC;
17833 cmd.db = pState->db;
17835 eDbType = deduceDatabaseType(cmd.zFile, 1);
17837 eDbType = pState->openMode;
17839 if( eDbType==SHELL_OPEN_ZIPFILE ){
17840 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
17841 if( cmd.zFile==0 ){
17842 cmd.zSrcTable = sqlite3_mprintf("zip");
17844 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
17848 }else if( cmd.zFile ){
17850 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
17851 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
17852 || cmd.eCmd==AR_CMD_REMOVE || cmd.eCmd==AR_CMD_UPDATE ){
17853 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
17855 flags = SQLITE_OPEN_READONLY;
17859 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
17860 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
17862 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
17863 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
17864 if( rc!=SQLITE_OK ){
17865 utf8_printf(stderr, "cannot open file: %s (%s)\n",
17866 cmd.zFile, sqlite3_errmsg(cmd.db)
17868 goto end_ar_command;
17870 sqlite3_fileio_init(cmd.db, 0, 0);
17871 sqlite3_sqlar_init(cmd.db, 0, 0);
17872 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
17873 shellPutsFunc, 0, 0);
17876 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
17877 if( cmd.eCmd!=AR_CMD_CREATE
17878 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
17880 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
17882 goto end_ar_command;
17884 cmd.zSrcTable = sqlite3_mprintf("sqlar");
17887 switch( cmd.eCmd ){
17888 case AR_CMD_CREATE:
17889 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
17892 case AR_CMD_EXTRACT:
17893 rc = arExtractCommand(&cmd);
17897 rc = arListCommand(&cmd);
17901 arUsage(pState->out);
17904 case AR_CMD_INSERT:
17905 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
17908 case AR_CMD_REMOVE:
17909 rc = arRemoveCommand(&cmd);
17913 assert( cmd.eCmd==AR_CMD_UPDATE );
17914 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
17919 if( cmd.db!=pState->db ){
17922 sqlite3_free(cmd.zSrcTable);
17926 /* End of the ".archive" or ".ar" command logic
17927 *******************************************************************************/
17928 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
17930 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17932 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
17933 ** Otherwise, the SQL statement or statements in zSql are executed using
17934 ** database connection db and the error code written to *pRc before
17935 ** this function returns.
17937 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
17939 if( rc==SQLITE_OK ){
17941 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
17942 if( rc!=SQLITE_OK ){
17943 raw_printf(stderr, "SQL error: %s\n", zErr);
17945 sqlite3_free(zErr);
17951 ** Like shellExec(), except that zFmt is a printf() style format string.
17953 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
17955 if( *pRc==SQLITE_OK ){
17957 va_start(ap, zFmt);
17958 z = sqlite3_vmprintf(zFmt, ap);
17961 *pRc = SQLITE_NOMEM;
17963 shellExec(db, pRc, z);
17970 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17971 ** Otherwise, an attempt is made to allocate, zero and return a pointer
17972 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
17973 ** to SQLITE_NOMEM and NULL returned.
17975 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
17977 if( *pRc==SQLITE_OK ){
17978 pRet = sqlite3_malloc64(nByte);
17980 *pRc = SQLITE_NOMEM;
17982 memset(pRet, 0, nByte);
17989 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17990 ** Otherwise, zFmt is treated as a printf() style string. The result of
17991 ** formatting it along with any trailing arguments is written into a
17992 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
17993 ** It is the responsibility of the caller to eventually free this buffer
17994 ** using a call to sqlite3_free().
17996 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
17997 ** pointer returned.
17999 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
18001 if( *pRc==SQLITE_OK ){
18003 va_start(ap, zFmt);
18004 z = sqlite3_vmprintf(zFmt, ap);
18007 *pRc = SQLITE_NOMEM;
18014 ** When running the ".recover" command, each output table, and the special
18015 ** orphaned row table if it is required, is represented by an instance
18016 ** of the following struct.
18018 typedef struct RecoverTable RecoverTable;
18019 struct RecoverTable {
18020 char *zQuoted; /* Quoted version of table name */
18021 int nCol; /* Number of columns in table */
18022 char **azlCol; /* Array of column lists */
18023 int iPk; /* Index of IPK column */
18027 ** Free a RecoverTable object allocated by recoverFindTable() or
18028 ** recoverOrphanTable().
18030 static void recoverFreeTable(RecoverTable *pTab){
18032 sqlite3_free(pTab->zQuoted);
18033 if( pTab->azlCol ){
18035 for(i=0; i<=pTab->nCol; i++){
18036 sqlite3_free(pTab->azlCol[i]);
18038 sqlite3_free(pTab->azlCol);
18040 sqlite3_free(pTab);
18045 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
18046 ** Otherwise, it allocates and returns a RecoverTable object based on the
18047 ** final four arguments passed to this function. It is the responsibility
18048 ** of the caller to eventually free the returned object using
18049 ** recoverFreeTable().
18051 static RecoverTable *recoverNewTable(
18052 int *pRc, /* IN/OUT: Error code */
18053 const char *zName, /* Name of table */
18054 const char *zSql, /* CREATE TABLE statement */
18058 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
18060 RecoverTable *pTab = 0;
18062 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
18063 if( rc==SQLITE_OK ){
18065 int bSqlIntkey = 0;
18066 sqlite3_stmt *pStmt = 0;
18068 rc = sqlite3_open("", &dbtmp);
18069 if( rc==SQLITE_OK ){
18070 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
18071 shellIdQuote, 0, 0);
18073 if( rc==SQLITE_OK ){
18074 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
18076 if( rc==SQLITE_OK ){
18077 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
18078 if( rc==SQLITE_ERROR ){
18083 shellPreparePrintf(dbtmp, &rc, &pStmt,
18084 "SELECT count(*) FROM pragma_table_info(%Q)", zName
18086 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18087 nSqlCol = sqlite3_column_int(pStmt, 0);
18089 shellFinalize(&rc, pStmt);
18091 if( rc!=SQLITE_OK || nSqlCol<nCol ){
18095 shellPreparePrintf(dbtmp, &rc, &pStmt,
18097 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
18098 ") FROM sqlite_schema WHERE name = %Q", zName
18100 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18101 bSqlIntkey = sqlite3_column_int(pStmt, 0);
18103 shellFinalize(&rc, pStmt);
18105 if( bIntkey==bSqlIntkey ){
18107 const char *zPk = "_rowid_";
18108 sqlite3_stmt *pPkFinder = 0;
18110 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
18111 ** set zPk to the name of the PK column, and pTab->iPk to the index
18112 ** of the column, where columns are 0-numbered from left to right.
18113 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
18114 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
18117 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
18118 "SELECT cid, name FROM pragma_table_info(%Q) "
18119 " WHERE pk=1 AND type='integer' COLLATE nocase"
18120 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
18123 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
18124 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
18125 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
18129 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
18130 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
18131 pTab->nCol = nSqlCol;
18134 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
18136 pTab->azlCol[0] = shellMPrintf(&rc, "");
18139 shellPreparePrintf(dbtmp, &rc, &pStmt,
18140 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
18141 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
18142 "FROM pragma_table_info(%Q)",
18143 bIntkey ? ", " : "", pTab->iPk,
18144 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
18147 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18148 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
18149 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
18152 shellFinalize(&rc, pStmt);
18154 shellFinalize(&rc, pPkFinder);
18159 sqlite3_close(dbtmp);
18161 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
18162 recoverFreeTable(pTab);
18169 ** This function is called to search the schema recovered from the
18170 ** sqlite_schema table of the (possibly) corrupt database as part
18171 ** of a ".recover" command. Specifically, for a table with root page
18172 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
18173 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
18176 ** If a table is found, a (RecoverTable*) object is returned. Or, if
18177 ** no such table is found, but bIntkey is false and iRoot is the
18178 ** root page of an index in the recovered schema, then (*pbNoop) is
18179 ** set to true and NULL returned. Or, if there is no such table or
18180 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
18181 ** the caller should write data to the orphans table.
18183 static RecoverTable *recoverFindTable(
18184 ShellState *pState, /* Shell state object */
18185 int *pRc, /* IN/OUT: Error code */
18186 int iRoot, /* Root page of table */
18187 int bIntkey, /* True for an intkey table */
18188 int nCol, /* Number of columns in table */
18189 int *pbNoop /* OUT: True if iRoot is root of index */
18191 sqlite3_stmt *pStmt = 0;
18192 RecoverTable *pRet = 0;
18194 const char *zSql = 0;
18195 const char *zName = 0;
18197 /* Search the recovered schema for an object with root page iRoot. */
18198 shellPreparePrintf(pState->db, pRc, &pStmt,
18199 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
18201 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18202 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
18203 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
18207 if( sqlite3_stricmp(zType, "table")==0 ){
18208 zName = (const char*)sqlite3_column_text(pStmt, 1);
18209 zSql = (const char*)sqlite3_column_text(pStmt, 2);
18210 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
18215 shellFinalize(pRc, pStmt);
18221 ** Return a RecoverTable object representing the orphans table.
18223 static RecoverTable *recoverOrphanTable(
18224 ShellState *pState, /* Shell state object */
18225 int *pRc, /* IN/OUT: Error code */
18226 const char *zLostAndFound, /* Base name for orphans table */
18227 int nCol /* Number of user data columns */
18229 RecoverTable *pTab = 0;
18230 if( nCol>=0 && *pRc==SQLITE_OK ){
18233 /* This block determines the name of the orphan table. The prefered
18234 ** name is zLostAndFound. But if that clashes with another name
18235 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
18236 ** and so on until a non-clashing name is found. */
18238 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
18239 sqlite3_stmt *pTest = 0;
18240 shellPrepare(pState->db, pRc,
18241 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
18243 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18244 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
18245 shellReset(pRc, pTest);
18246 sqlite3_free(zTab);
18247 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
18248 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
18250 shellFinalize(pRc, pTest);
18252 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
18254 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
18258 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
18259 if( pTab->azlCol ){
18260 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
18261 for(i=nCol-1; i>=0; i--){
18262 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
18267 if( *pRc!=SQLITE_OK ){
18268 recoverFreeTable(pTab);
18271 raw_printf(pState->out,
18272 "CREATE TABLE %s(rootpgno INTEGER, "
18273 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
18275 for(i=0; i<nCol; i++){
18276 raw_printf(pState->out, ", c%d", i);
18278 raw_printf(pState->out, ");\n");
18281 sqlite3_free(zTab);
18287 ** This function is called to recover data from the database. A script
18288 ** to construct a new database containing all recovered data is output
18289 ** on stream pState->out.
18291 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18292 int rc = SQLITE_OK;
18293 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
18294 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
18295 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
18296 const char *zRecoveryDb = ""; /* Name of "recovery" database */
18297 const char *zLostAndFound = "lost_and_found";
18300 RecoverTable *pOrphan = 0;
18302 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
18303 int bRowids = 1; /* 0 if --no-rowids */
18304 for(i=1; i<nArg; i++){
18305 char *z = azArg[i];
18307 if( z[0]=='-' && z[1]=='-' ) z++;
18309 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18312 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
18314 zRecoveryDb = azArg[i];
18316 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18318 zLostAndFound = azArg[i];
18320 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18324 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
18325 showHelp(pState->out, azArg[0]);
18330 shellExecPrintf(pState->db, &rc,
18331 /* Attach an in-memory database named 'recovery'. Create an indexed
18332 ** cache of the sqlite_dbptr virtual table. */
18333 "PRAGMA writable_schema = on;"
18334 "ATTACH %Q AS recovery;"
18335 "DROP TABLE IF EXISTS recovery.dbptr;"
18336 "DROP TABLE IF EXISTS recovery.freelist;"
18337 "DROP TABLE IF EXISTS recovery.map;"
18338 "DROP TABLE IF EXISTS recovery.schema;"
18339 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18343 shellExec(pState->db, &rc,
18344 "WITH trunk(pgno) AS ("
18345 " SELECT shell_int32("
18346 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18349 " SELECT shell_int32("
18350 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18351 " FROM trunk WHERE x>0"
18353 "freelist(data, n, freepgno) AS ("
18354 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18355 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18357 " SELECT data, n-1, shell_int32(data, 2+n) "
18358 " FROM freelist WHERE n>=0"
18360 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18364 /* If this is an auto-vacuum database, add all pointer-map pages to
18365 ** the freelist table. Do this regardless of whether or not
18366 ** --freelist-corrupt was specified. */
18367 shellExec(pState->db, &rc,
18368 "WITH ptrmap(pgno) AS ("
18369 " SELECT 2 WHERE shell_int32("
18370 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18373 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18374 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18376 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18379 shellExec(pState->db, &rc,
18380 "CREATE TABLE recovery.dbptr("
18381 " pgno, child, PRIMARY KEY(child, pgno)"
18383 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18384 " SELECT * FROM sqlite_dbptr"
18385 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18387 /* Delete any pointer to page 1. This ensures that page 1 is considered
18388 ** a root page, regardless of how corrupt the db is. */
18389 "DELETE FROM recovery.dbptr WHERE child = 1;"
18391 /* Delete all pointers to any pages that have more than one pointer
18392 ** to them. Such pages will be treated as root pages when recovering
18394 "DELETE FROM recovery.dbptr WHERE child IN ("
18395 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18398 /* Create the "map" table that will (eventually) contain instructions
18399 ** for dealing with each page in the db that contains one or more
18401 "CREATE TABLE recovery.map("
18402 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18405 /* Populate table [map]. If there are circular loops of pages in the
18406 ** database, the following adds all pages in such a loop to the map
18407 ** as individual root pages. This could be handled better. */
18408 "WITH pages(i, maxlen) AS ("
18409 " SELECT page_count, ("
18410 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18411 " ) FROM pragma_page_count WHERE page_count>0"
18414 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18415 " ) FROM pages WHERE i>=2"
18417 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18418 " SELECT i, maxlen, NULL, ("
18419 " WITH p(orig, pgno, parent) AS ("
18420 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18422 " SELECT i, p.parent, "
18423 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18425 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18427 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18428 "UPDATE recovery.map AS o SET intkey = ("
18429 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18432 /* Extract data from page 1 and any linked pages into table
18433 ** recovery.schema. With the same schema as an sqlite_schema table. */
18434 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18435 "INSERT INTO recovery.schema SELECT "
18436 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
18437 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
18438 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
18439 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
18440 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
18441 "FROM sqlite_dbdata WHERE pgno IN ("
18442 " SELECT pgno FROM recovery.map WHERE root=1"
18444 "GROUP BY pgno, cell;"
18445 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18448 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
18449 ** CREATE TABLE statements that extracted from the existing schema. */
18450 if( rc==SQLITE_OK ){
18451 sqlite3_stmt *pStmt = 0;
18452 /* ".recover" might output content in an order which causes immediate
18453 ** foreign key constraints to be violated. So disable foreign-key
18454 ** constraint enforcement to prevent problems when running the output
18456 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
18457 raw_printf(pState->out, "BEGIN;\n");
18458 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
18459 shellPrepare(pState->db, &rc,
18460 "SELECT sql FROM recovery.schema "
18461 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
18463 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18464 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
18465 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
18469 shellFinalize(&rc, pStmt);
18472 /* Figure out if an orphan table will be required. And if so, how many
18473 ** user columns it should contain */
18474 shellPrepare(pState->db, &rc,
18475 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
18478 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18479 nOrphan = sqlite3_column_int(pLoop, 0);
18481 shellFinalize(&rc, pLoop);
18484 shellPrepare(pState->db, &rc,
18485 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
18488 shellPrepare(pState->db, &rc,
18489 "SELECT max(field), group_concat(shell_escape_crnl(quote"
18490 "(case when (? AND field<0) then NULL else value end)"
18493 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
18494 "GROUP BY cell", &pCells
18497 /* Loop through each root page. */
18498 shellPrepare(pState->db, &rc,
18499 "SELECT root, intkey, max(maxlen) FROM recovery.map"
18500 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
18501 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
18504 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18505 int iRoot = sqlite3_column_int(pLoop, 0);
18506 int bIntkey = sqlite3_column_int(pLoop, 1);
18507 int nCol = sqlite3_column_int(pLoop, 2);
18509 RecoverTable *pTab;
18511 assert( bIntkey==0 || bIntkey==1 );
18512 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
18513 if( bNoop || rc ) continue;
18516 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18519 if( pTab==0 ) break;
18522 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
18523 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
18525 sqlite3_bind_int(pPages, 1, iRoot);
18526 if( bRowids==0 && pTab->iPk<0 ){
18527 sqlite3_bind_int(pCells, 1, 1);
18529 sqlite3_bind_int(pCells, 1, 0);
18531 sqlite3_bind_int(pCells, 3, pTab->iPk);
18533 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
18534 int iPgno = sqlite3_column_int(pPages, 0);
18535 sqlite3_bind_int(pCells, 2, iPgno);
18536 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
18537 int nField = sqlite3_column_int(pCells, 0);
18538 int iMin = sqlite3_column_int(pCells, 2);
18539 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
18541 RecoverTable *pTab2 = pTab;
18542 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
18544 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18547 if( pTab2==0 ) break;
18551 if( pTab2==pOrphan ){
18552 raw_printf(pState->out,
18553 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
18554 pTab2->zQuoted, iRoot, iPgno, nField,
18555 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
18558 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
18559 pTab2->zQuoted, pTab2->azlCol[nField], zVal
18563 shellReset(&rc, pCells);
18565 shellReset(&rc, pPages);
18566 if( pTab!=pOrphan ) recoverFreeTable(pTab);
18568 shellFinalize(&rc, pLoop);
18569 shellFinalize(&rc, pPages);
18570 shellFinalize(&rc, pCells);
18571 recoverFreeTable(pOrphan);
18573 /* The rest of the schema */
18574 if( rc==SQLITE_OK ){
18575 sqlite3_stmt *pStmt = 0;
18576 shellPrepare(pState->db, &rc,
18577 "SELECT sql, name FROM recovery.schema "
18578 "WHERE sql NOT LIKE 'create table%'", &pStmt
18580 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18581 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
18582 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
18583 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
18584 char *zPrint = shellMPrintf(&rc,
18585 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
18588 raw_printf(pState->out, "%s;\n", zPrint);
18589 sqlite3_free(zPrint);
18591 raw_printf(pState->out, "%s;\n", zSql);
18594 shellFinalize(&rc, pStmt);
18597 if( rc==SQLITE_OK ){
18598 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
18599 raw_printf(pState->out, "COMMIT;\n");
18601 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
18604 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18607 ** If an input line begins with "." then invoke this routine to
18608 ** process that line.
18610 ** Return 1 on error, 2 to exit, and 0 otherwise.
18612 static int do_meta_command(char *zLine, ShellState *p){
18619 #ifndef SQLITE_OMIT_VIRTUALTABLE
18620 if( p->expert.pExpert ){
18621 expertFinish(p, 1, 0);
18625 /* Parse the input line into tokens.
18627 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
18628 while( IsSpace(zLine[h]) ){ h++; }
18629 if( zLine[h]==0 ) break;
18630 if( zLine[h]=='\'' || zLine[h]=='"' ){
18631 int delim = zLine[h++];
18632 azArg[nArg++] = &zLine[h];
18633 while( zLine[h] && zLine[h]!=delim ){
18634 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
18637 if( zLine[h]==delim ){
18640 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
18642 azArg[nArg++] = &zLine[h];
18643 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
18644 if( zLine[h] ) zLine[h++] = 0;
18645 resolve_backslashes(azArg[nArg-1]);
18650 /* Process the input line.
18652 if( nArg==0 ) return 0; /* no tokens, no error */
18653 n = strlen30(azArg[0]);
18657 #ifndef SQLITE_OMIT_AUTHORIZATION
18658 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
18660 raw_printf(stderr, "Usage: .auth ON|OFF\n");
18662 goto meta_command_exit;
18665 if( booleanValue(azArg[1]) ){
18666 sqlite3_set_authorizer(p->db, shellAuth, p);
18667 }else if( p->bSafeModePersist ){
18668 sqlite3_set_authorizer(p->db, safeModeAuth, p);
18670 sqlite3_set_authorizer(p->db, 0, 0);
18675 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18676 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
18678 failIfSafeMode(p, "cannot run .archive in safe mode");
18679 rc = arDotCommand(p, 0, azArg, nArg);
18683 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
18684 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
18686 const char *zDestFile = 0;
18687 const char *zDb = 0;
18689 sqlite3_backup *pBackup;
18692 const char *zVfs = 0;
18693 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
18694 for(j=1; j<nArg; j++){
18695 const char *z = azArg[j];
18697 if( z[1]=='-' ) z++;
18698 if( strcmp(z, "-append")==0 ){
18701 if( strcmp(z, "-async")==0 ){
18705 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
18708 }else if( zDestFile==0 ){
18709 zDestFile = azArg[j];
18710 }else if( zDb==0 ){
18712 zDestFile = azArg[j];
18714 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
18718 if( zDestFile==0 ){
18719 raw_printf(stderr, "missing FILENAME argument on .backup\n");
18722 if( zDb==0 ) zDb = "main";
18723 rc = sqlite3_open_v2(zDestFile, &pDest,
18724 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
18725 if( rc!=SQLITE_OK ){
18726 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
18731 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
18735 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
18737 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18741 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
18742 sqlite3_backup_finish(pBackup);
18743 if( rc==SQLITE_DONE ){
18746 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18752 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
18754 bail_on_error = booleanValue(azArg[1]);
18756 raw_printf(stderr, "Usage: .bail on|off\n");
18761 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
18763 if( booleanValue(azArg[1]) ){
18764 setBinaryMode(p->out, 1);
18766 setTextMode(p->out, 1);
18769 raw_printf(stderr, "Usage: .binary on|off\n");
18774 /* The undocumented ".breakpoint" command causes a call to the no-op
18775 ** routine named test_breakpoint().
18777 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
18781 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
18782 failIfSafeMode(p, "cannot run .cd in safe mode");
18784 #if defined(_WIN32) || defined(WIN32)
18785 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
18786 rc = !SetCurrentDirectoryW(z);
18789 rc = chdir(azArg[1]);
18792 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
18796 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
18801 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
18803 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
18805 raw_printf(stderr, "Usage: .changes on|off\n");
18810 /* Cancel output redirection, if it is currently set (by .testcase)
18811 ** Then read the content of the testcase-out.txt file and compare against
18812 ** azArg[1]. If there are differences, report an error and exit.
18814 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
18818 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
18820 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
18821 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
18823 }else if( testcase_glob(azArg[1],zRes)==0 ){
18824 utf8_printf(stderr,
18825 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
18826 p->zTestcase, azArg[1], zRes);
18829 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
18832 sqlite3_free(zRes);
18835 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
18836 failIfSafeMode(p, "cannot run .clone in safe mode");
18838 tryToClone(p, azArg[1]);
18840 raw_printf(stderr, "Usage: .clone FILENAME\n");
18845 if( c=='c' && strncmp(azArg[0], "connection", n)==0 ){
18847 /* List available connections */
18849 for(i=0; i<ArraySize(p->aAuxDb); i++){
18850 const char *zFile = p->aAuxDb[i].zDbFilename;
18851 if( p->aAuxDb[i].db==0 && p->pAuxDb!=&p->aAuxDb[i] ){
18852 zFile = "(not open)";
18853 }else if( zFile==0 ){
18854 zFile = "(memory)";
18855 }else if( zFile[0]==0 ){
18856 zFile = "(temporary-file)";
18858 if( p->pAuxDb == &p->aAuxDb[i] ){
18859 utf8_printf(stdout, "ACTIVE %d: %s\n", i, zFile);
18860 }else if( p->aAuxDb[i].db!=0 ){
18861 utf8_printf(stdout, " %d: %s\n", i, zFile);
18864 }else if( nArg==2 && IsDigit(azArg[1][0]) && azArg[1][1]==0 ){
18865 int i = azArg[1][0] - '0';
18866 if( p->pAuxDb != &p->aAuxDb[i] && i>=0 && i<ArraySize(p->aAuxDb) ){
18867 p->pAuxDb->db = p->db;
18868 p->pAuxDb = &p->aAuxDb[i];
18869 globalDb = p->db = p->pAuxDb->db;
18872 }else if( nArg==3 && strcmp(azArg[1], "close")==0
18873 && IsDigit(azArg[2][0]) && azArg[2][1]==0 ){
18874 int i = azArg[2][0] - '0';
18875 if( i<0 || i>=ArraySize(p->aAuxDb) ){
18877 }else if( p->pAuxDb == &p->aAuxDb[i] ){
18878 raw_printf(stderr, "cannot close the active database connection\n");
18880 }else if( p->aAuxDb[i].db ){
18881 session_close_all(p, i);
18882 close_db(p->aAuxDb[i].db);
18883 p->aAuxDb[i].db = 0;
18886 raw_printf(stderr, "Usage: .connection [close] [CONNECTION-NUMBER]\n");
18891 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
18894 sqlite3_stmt *pStmt;
18897 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18899 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18902 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18903 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18904 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18905 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18906 if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
18907 azName[nName*2] = strdup(zSchema);
18908 azName[nName*2+1] = strdup(zFile);
18912 sqlite3_finalize(pStmt);
18913 for(i=0; i<nName; i++){
18914 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
18915 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
18916 const char *z = azName[i*2+1];
18917 utf8_printf(p->out, "%s: %s %s%s\n",
18919 z && z[0] ? z : "\"\"",
18920 bRdonly ? "r/o" : "r/w",
18921 eTxn==SQLITE_TXN_NONE ? "" :
18922 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
18924 free(azName[i*2+1]);
18926 sqlite3_free(azName);
18929 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
18930 static const struct DbConfigChoices {
18934 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
18935 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
18936 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
18937 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
18938 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
18939 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
18940 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
18941 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
18942 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
18943 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
18944 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
18945 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
18946 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
18947 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
18948 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
18949 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
18953 for(ii=0; ii<ArraySize(aDbConfig); ii++){
18954 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
18956 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
18958 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
18959 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
18960 if( nArg>1 ) break;
18962 if( nArg>1 && ii==ArraySize(aDbConfig) ){
18963 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
18964 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
18968 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
18969 rc = shell_dbinfo_command(p, nArg, azArg);
18972 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18973 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
18975 rc = recoverDatabaseCmd(p, nArg, azArg);
18977 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18979 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
18983 int savedShowHeader = p->showHeader;
18984 int savedShellFlags = p->shellFlgs;
18986 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
18987 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
18988 for(i=1; i<nArg; i++){
18989 if( azArg[i][0]=='-' ){
18990 const char *z = azArg[i]+1;
18991 if( z[0]=='-' ) z++;
18992 if( strcmp(z,"preserve-rowids")==0 ){
18993 #ifdef SQLITE_OMIT_VIRTUALTABLE
18994 raw_printf(stderr, "The --preserve-rowids option is not compatible"
18995 " with SQLITE_OMIT_VIRTUALTABLE\n");
18997 sqlite3_free(zLike);
18998 goto meta_command_exit;
19000 ShellSetFlag(p, SHFLG_PreserveRowid);
19003 if( strcmp(z,"newlines")==0 ){
19004 ShellSetFlag(p, SHFLG_Newlines);
19006 if( strcmp(z,"data-only")==0 ){
19007 ShellSetFlag(p, SHFLG_DumpDataOnly);
19009 if( strcmp(z,"nosys")==0 ){
19010 ShellSetFlag(p, SHFLG_DumpNoSys);
19013 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
19015 sqlite3_free(zLike);
19016 goto meta_command_exit;
19019 /* azArg[i] contains a LIKE pattern. This ".dump" request should
19020 ** only dump data for tables for which either the table name matches
19021 ** the LIKE pattern, or the table appears to be a shadow table of
19022 ** a virtual table for which the name matches the LIKE pattern.
19024 char *zExpr = sqlite3_mprintf(
19025 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
19026 " SELECT 1 FROM sqlite_schema WHERE "
19027 " name LIKE %Q ESCAPE '\\' AND"
19028 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
19029 " substr(o.name, 1, length(name)+1) == (name||'_')"
19030 ")", azArg[i], azArg[i]
19034 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
19043 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19044 /* When playing back a "dump", the content might appear in an order
19045 ** which causes immediate foreign key constraints to be violated.
19046 ** So disable foreign-key constraint enforcement to prevent problems. */
19047 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
19048 raw_printf(p->out, "BEGIN TRANSACTION;\n");
19050 p->writableSchema = 0;
19052 /* Set writable_schema=ON since doing so forces SQLite to initialize
19053 ** as much of the schema as it can even if the sqlite_schema table is
19055 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
19057 if( zLike==0 ) zLike = sqlite3_mprintf("true");
19058 zSql = sqlite3_mprintf(
19059 "SELECT name, type, sql FROM sqlite_schema AS o "
19060 "WHERE (%s) AND type=='table'"
19061 " AND sql NOT NULL"
19062 " ORDER BY tbl_name='sqlite_sequence', rowid",
19065 run_schema_dump_query(p,zSql);
19066 sqlite3_free(zSql);
19067 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19068 zSql = sqlite3_mprintf(
19069 "SELECT sql FROM sqlite_schema AS o "
19070 "WHERE (%s) AND sql NOT NULL"
19071 " AND type IN ('index','trigger','view')",
19074 run_table_dump_query(p, zSql);
19075 sqlite3_free(zSql);
19077 sqlite3_free(zLike);
19078 if( p->writableSchema ){
19079 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
19080 p->writableSchema = 0;
19082 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
19083 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
19084 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
19085 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
19087 p->showHeader = savedShowHeader;
19088 p->shellFlgs = savedShellFlags;
19091 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
19093 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
19095 raw_printf(stderr, "Usage: .echo on|off\n");
19100 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
19102 p->autoEQPtest = 0;
19103 if( p->autoEQPtrace ){
19104 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
19105 p->autoEQPtrace = 0;
19107 if( strcmp(azArg[1],"full")==0 ){
19108 p->autoEQP = AUTOEQP_full;
19109 }else if( strcmp(azArg[1],"trigger")==0 ){
19110 p->autoEQP = AUTOEQP_trigger;
19111 #ifdef SQLITE_DEBUG
19112 }else if( strcmp(azArg[1],"test")==0 ){
19113 p->autoEQP = AUTOEQP_on;
19114 p->autoEQPtest = 1;
19115 }else if( strcmp(azArg[1],"trace")==0 ){
19116 p->autoEQP = AUTOEQP_full;
19117 p->autoEQPtrace = 1;
19119 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
19120 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
19123 p->autoEQP = (u8)booleanValue(azArg[1]);
19126 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
19131 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
19132 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
19136 /* The ".explain" command is automatic now. It is largely pointless. It
19137 ** retained purely for backwards compatibility */
19138 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
19141 if( strcmp(azArg[1],"auto")==0 ){
19144 val = booleanValue(azArg[1]);
19147 if( val==1 && p->mode!=MODE_Explain ){
19148 p->normalMode = p->mode;
19149 p->mode = MODE_Explain;
19150 p->autoExplain = 0;
19151 }else if( val==0 ){
19152 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19153 p->autoExplain = 0;
19154 }else if( val==99 ){
19155 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
19156 p->autoExplain = 1;
19160 #ifndef SQLITE_OMIT_VIRTUALTABLE
19161 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
19163 expertDotCommand(p, azArg, nArg);
19167 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
19168 static const struct {
19169 const char *zCtrlName; /* Name of a test-control option */
19170 int ctrlCode; /* Integer code for that option */
19171 const char *zUsage; /* Usage notes */
19173 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
19174 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
19175 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
19176 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
19177 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
19178 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
19179 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
19180 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
19181 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
19182 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
19183 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
19187 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
19188 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
19190 const char *zCmd = 0;
19191 const char *zSchema = 0;
19194 zCmd = nArg>=2 ? azArg[1] : "help";
19197 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
19200 zSchema = azArg[2];
19201 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
19206 /* The argument can optionally begin with "-" or "--" */
19207 if( zCmd[0]=='-' && zCmd[1] ){
19209 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19212 /* --help lists all file-controls */
19213 if( strcmp(zCmd,"help")==0 ){
19214 utf8_printf(p->out, "Available file-controls:\n");
19215 for(i=0; i<ArraySize(aCtrl); i++){
19216 utf8_printf(p->out, " .filectrl %s %s\n",
19217 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19220 goto meta_command_exit;
19223 /* convert filectrl text option to value. allow any unique prefix
19224 ** of the option name, or a numerical value. */
19225 n2 = strlen30(zCmd);
19226 for(i=0; i<ArraySize(aCtrl); i++){
19227 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19229 filectrl = aCtrl[i].ctrlCode;
19232 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
19233 "Use \".filectrl --help\" for help\n", zCmd);
19235 goto meta_command_exit;
19240 utf8_printf(stderr,"Error: unknown file-control: %s\n"
19241 "Use \".filectrl --help\" for help\n", zCmd);
19244 case SQLITE_FCNTL_SIZE_LIMIT: {
19245 if( nArg!=2 && nArg!=3 ) break;
19246 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
19247 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
19251 case SQLITE_FCNTL_LOCK_TIMEOUT:
19252 case SQLITE_FCNTL_CHUNK_SIZE: {
19254 if( nArg!=3 ) break;
19255 x = (int)integerValue(azArg[2]);
19256 sqlite3_file_control(p->db, zSchema, filectrl, &x);
19260 case SQLITE_FCNTL_PERSIST_WAL:
19261 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
19263 if( nArg!=2 && nArg!=3 ) break;
19264 x = nArg==3 ? booleanValue(azArg[2]) : -1;
19265 sqlite3_file_control(p->db, zSchema, filectrl, &x);
19270 case SQLITE_FCNTL_DATA_VERSION:
19271 case SQLITE_FCNTL_HAS_MOVED: {
19273 if( nArg!=2 ) break;
19274 sqlite3_file_control(p->db, zSchema, filectrl, &x);
19279 case SQLITE_FCNTL_TEMPFILENAME: {
19281 if( nArg!=2 ) break;
19282 sqlite3_file_control(p->db, zSchema, filectrl, &z);
19284 utf8_printf(p->out, "%s\n", z);
19290 case SQLITE_FCNTL_RESERVE_BYTES: {
19293 x = atoi(azArg[2]);
19294 sqlite3_file_control(p->db, zSchema, filectrl, &x);
19297 sqlite3_file_control(p->db, zSchema, filectrl, &x);
19298 utf8_printf(p->out,"%d\n", x);
19304 if( isOk==0 && iCtrl>=0 ){
19305 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
19307 }else if( isOk==1 ){
19309 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
19310 raw_printf(p->out, "%s\n", zBuf);
19314 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
19317 memcpy(&data, p, sizeof(data));
19318 data.showHeader = 0;
19319 data.cMode = data.mode = MODE_Semi;
19320 if( nArg==2 && optionMatch(azArg[1], "indent") ){
19321 data.cMode = data.mode = MODE_Pretty;
19325 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
19327 goto meta_command_exit;
19330 rc = sqlite3_exec(p->db,
19332 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
19333 " FROM sqlite_schema UNION ALL"
19334 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
19335 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
19339 if( rc==SQLITE_OK ){
19340 sqlite3_stmt *pStmt;
19341 rc = sqlite3_prepare_v2(p->db,
19342 "SELECT rowid FROM sqlite_schema"
19343 " WHERE name GLOB 'sqlite_stat[134]'",
19345 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
19346 sqlite3_finalize(pStmt);
19349 raw_printf(p->out, "/* No STAT tables available */\n");
19351 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19352 data.cMode = data.mode = MODE_Insert;
19353 data.zDestTable = "sqlite_stat1";
19354 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
19355 data.zDestTable = "sqlite_stat4";
19356 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
19357 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19361 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
19363 p->showHeader = booleanValue(azArg[1]);
19364 p->shellFlgs |= SHFLG_HeaderSet;
19366 raw_printf(stderr, "Usage: .headers on|off\n");
19371 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
19373 n = showHelp(p->out, azArg[1]);
19375 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
19378 showHelp(p->out, 0);
19382 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19383 char *zTable = 0; /* Insert data into this table */
19384 char *zFile = 0; /* Name of file to extra content from */
19385 sqlite3_stmt *pStmt = NULL; /* A statement */
19386 int nCol; /* Number of columns in the table */
19387 int nByte; /* Number of bytes in an SQL string */
19388 int i, j; /* Loop counters */
19389 int needCommit; /* True to COMMIT or ROLLBACK at end */
19390 int nSep; /* Number of bytes in p->colSeparator[] */
19391 char *zSql; /* An SQL statement */
19392 ImportCtx sCtx; /* Reader context */
19393 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
19394 int eVerbose = 0; /* Larger for more console output */
19395 int nSkip = 0; /* Initial lines to skip */
19396 int useOutputMode = 1; /* Use output mode to determine separators */
19398 failIfSafeMode(p, "cannot run .import in safe mode");
19399 memset(&sCtx, 0, sizeof(sCtx));
19400 sCtx.z = sqlite3_malloc64(120);
19402 import_cleanup(&sCtx);
19403 shell_out_of_memory();
19405 if( p->mode==MODE_Ascii ){
19406 xRead = ascii_read_one_field;
19408 xRead = csv_read_one_field;
19410 for(i=1; i<nArg; i++){
19411 char *z = azArg[i];
19412 if( z[0]=='-' && z[1]=='-' ) z++;
19416 }else if( zTable==0 ){
19419 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
19420 showHelp(p->out, "import");
19422 goto meta_command_exit;
19424 }else if( strcmp(z,"-v")==0 ){
19426 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
19427 nSkip = integerValue(azArg[++i]);
19428 }else if( strcmp(z,"-ascii")==0 ){
19429 sCtx.cColSep = SEP_Unit[0];
19430 sCtx.cRowSep = SEP_Record[0];
19431 xRead = ascii_read_one_field;
19433 }else if( strcmp(z,"-csv")==0 ){
19434 sCtx.cColSep = ',';
19435 sCtx.cRowSep = '\n';
19436 xRead = csv_read_one_field;
19439 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
19440 showHelp(p->out, "import");
19442 goto meta_command_exit;
19446 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
19447 zFile==0 ? "FILE" : "TABLE");
19448 showHelp(p->out, "import");
19450 goto meta_command_exit;
19454 if( useOutputMode ){
19455 /* If neither the --csv or --ascii options are specified, then set
19456 ** the column and row separator characters from the output mode. */
19457 nSep = strlen30(p->colSeparator);
19460 "Error: non-null column separator required for import\n");
19462 goto meta_command_exit;
19466 "Error: multi-character column separators not allowed"
19469 goto meta_command_exit;
19471 nSep = strlen30(p->rowSeparator);
19474 "Error: non-null row separator required for import\n");
19476 goto meta_command_exit;
19478 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
19479 /* When importing CSV (only), if the row separator is set to the
19480 ** default output row separator, change it to the default input
19481 ** row separator. This avoids having to maintain different input
19482 ** and output row separators. */
19483 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19484 nSep = strlen30(p->rowSeparator);
19487 raw_printf(stderr, "Error: multi-character row separators not allowed"
19490 goto meta_command_exit;
19492 sCtx.cColSep = p->colSeparator[0];
19493 sCtx.cRowSep = p->rowSeparator[0];
19495 sCtx.zFile = zFile;
19497 if( sCtx.zFile[0]=='|' ){
19498 #ifdef SQLITE_OMIT_POPEN
19499 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19501 goto meta_command_exit;
19503 sCtx.in = popen(sCtx.zFile+1, "r");
19504 sCtx.zFile = "<pipe>";
19505 sCtx.xCloser = pclose;
19508 sCtx.in = fopen(sCtx.zFile, "rb");
19509 sCtx.xCloser = fclose;
19512 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
19514 import_cleanup(&sCtx);
19515 goto meta_command_exit;
19517 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
19520 zSep[0] = sCtx.cColSep;
19521 utf8_printf(p->out, "Column separator ");
19522 output_c_string(p->out, zSep);
19523 utf8_printf(p->out, ", row separator ");
19524 zSep[0] = sCtx.cRowSep;
19525 output_c_string(p->out, zSep);
19526 utf8_printf(p->out, "\n");
19528 while( (nSkip--)>0 ){
19529 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
19531 zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
19533 import_cleanup(&sCtx);
19534 shell_out_of_memory();
19536 nByte = strlen30(zSql);
19537 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19538 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
19539 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
19540 char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
19542 while( xRead(&sCtx) ){
19543 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
19545 if( sCtx.cTerm!=sCtx.cColSep ) break;
19548 sqlite3_free(zCreate);
19549 import_cleanup(&sCtx);
19550 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
19552 goto meta_command_exit;
19554 zCreate = sqlite3_mprintf("%z\n)", zCreate);
19556 utf8_printf(p->out, "%s\n", zCreate);
19558 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
19559 sqlite3_free(zCreate);
19561 utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
19562 sqlite3_errmsg(p->db));
19563 import_cleanup(&sCtx);
19565 goto meta_command_exit;
19567 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19569 sqlite3_free(zSql);
19571 if (pStmt) sqlite3_finalize(pStmt);
19572 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
19573 import_cleanup(&sCtx);
19575 goto meta_command_exit;
19577 nCol = sqlite3_column_count(pStmt);
19578 sqlite3_finalize(pStmt);
19580 if( nCol==0 ) return 0; /* no columns, no error */
19581 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
19583 import_cleanup(&sCtx);
19584 shell_out_of_memory();
19586 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
19587 j = strlen30(zSql);
19588 for(i=1; i<nCol; i++){
19595 utf8_printf(p->out, "Insert using: %s\n", zSql);
19597 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19598 sqlite3_free(zSql);
19600 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19601 if (pStmt) sqlite3_finalize(pStmt);
19602 import_cleanup(&sCtx);
19604 goto meta_command_exit;
19606 needCommit = sqlite3_get_autocommit(p->db);
19607 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
19609 int startLine = sCtx.nLine;
19610 for(i=0; i<nCol; i++){
19611 char *z = xRead(&sCtx);
19613 ** Did we reach end-of-file before finding any columns?
19614 ** If so, stop instead of NULL filling the remaining columns.
19616 if( z==0 && i==0 ) break;
19618 ** Did we reach end-of-file OR end-of-line before finding any
19619 ** columns in ASCII mode? If so, stop instead of NULL filling
19620 ** the remaining columns.
19622 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
19623 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
19624 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
19625 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19626 "filling the rest with NULL\n",
19627 sCtx.zFile, startLine, nCol, i+1);
19629 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
19632 if( sCtx.cTerm==sCtx.cColSep ){
19636 }while( sCtx.cTerm==sCtx.cColSep );
19637 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19638 "extras ignored\n",
19639 sCtx.zFile, startLine, nCol, i);
19642 sqlite3_step(pStmt);
19643 rc = sqlite3_reset(pStmt);
19644 if( rc!=SQLITE_OK ){
19645 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
19646 startLine, sqlite3_errmsg(p->db));
19652 }while( sCtx.cTerm!=EOF );
19654 import_cleanup(&sCtx);
19655 sqlite3_finalize(pStmt);
19656 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
19658 utf8_printf(p->out,
19659 "Added %d rows with %d errors using %d lines of input\n",
19660 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
19664 #ifndef SQLITE_UNTESTABLE
19665 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
19667 char *zCollist = 0;
19668 sqlite3_stmt *pStmt;
19670 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
19671 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
19673 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
19674 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
19675 " .imposter off\n");
19676 /* Also allowed, but not documented:
19678 ** .imposter TABLE IMPOSTER
19680 ** where TABLE is a WITHOUT ROWID table. In that case, the
19681 ** imposter is another WITHOUT ROWID table with the columns in
19682 ** storage order. */
19684 goto meta_command_exit;
19688 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
19689 goto meta_command_exit;
19691 zSql = sqlite3_mprintf(
19692 "SELECT rootpage, 0 FROM sqlite_schema"
19693 " WHERE name='%q' AND type='index'"
19695 "SELECT rootpage, 1 FROM sqlite_schema"
19696 " WHERE name='%q' AND type='table'"
19697 " AND sql LIKE '%%without%%rowid%%'",
19700 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19701 sqlite3_free(zSql);
19702 if( sqlite3_step(pStmt)==SQLITE_ROW ){
19703 tnum = sqlite3_column_int(pStmt, 0);
19704 isWO = sqlite3_column_int(pStmt, 1);
19706 sqlite3_finalize(pStmt);
19707 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
19708 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19709 sqlite3_free(zSql);
19711 while( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19713 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
19716 if( sqlite3_column_int(pStmt,1)==-1 ){
19719 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
19723 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
19724 lenPK = (int)strlen(zCollist);
19727 zCollist = sqlite3_mprintf("\"%w\"", zCol);
19729 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
19732 sqlite3_finalize(pStmt);
19733 if( i==0 || tnum==0 ){
19734 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
19736 sqlite3_free(zCollist);
19737 goto meta_command_exit;
19739 if( lenPK==0 ) lenPK = 100000;
19740 zSql = sqlite3_mprintf(
19741 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
19742 azArg[2], zCollist, lenPK, zCollist);
19743 sqlite3_free(zCollist);
19744 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
19745 if( rc==SQLITE_OK ){
19746 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
19747 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
19749 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
19751 utf8_printf(stdout, "%s;\n", zSql);
19753 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
19754 azArg[1], isWO ? "table" : "index"
19758 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
19761 sqlite3_free(zSql);
19763 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
19765 #ifdef SQLITE_ENABLE_IOTRACE
19766 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
19767 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
19768 if( iotrace && iotrace!=stdout ) fclose(iotrace);
19771 sqlite3IoTrace = 0;
19772 }else if( strcmp(azArg[1], "-")==0 ){
19773 sqlite3IoTrace = iotracePrintf;
19776 iotrace = fopen(azArg[1], "w");
19778 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19779 sqlite3IoTrace = 0;
19782 sqlite3IoTrace = iotracePrintf;
19788 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
19789 static const struct {
19790 const char *zLimitName; /* Name of a limit */
19791 int limitCode; /* Integer code for that limit */
19793 { "length", SQLITE_LIMIT_LENGTH },
19794 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
19795 { "column", SQLITE_LIMIT_COLUMN },
19796 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
19797 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
19798 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
19799 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
19800 { "attached", SQLITE_LIMIT_ATTACHED },
19801 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
19802 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
19803 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
19804 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
19809 for(i=0; i<ArraySize(aLimit); i++){
19810 printf("%20s %d\n", aLimit[i].zLimitName,
19811 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
19813 }else if( nArg>3 ){
19814 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
19816 goto meta_command_exit;
19819 n2 = strlen30(azArg[1]);
19820 for(i=0; i<ArraySize(aLimit); i++){
19821 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
19825 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
19827 goto meta_command_exit;
19832 utf8_printf(stderr, "unknown limit: \"%s\"\n"
19833 "enter \".limits\" with no arguments for a list.\n",
19836 goto meta_command_exit;
19839 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
19840 (int)integerValue(azArg[2]));
19842 printf("%20s %d\n", aLimit[iLimit].zLimitName,
19843 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
19847 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
19849 lintDotCommand(p, azArg, nArg);
19852 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19853 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
19854 const char *zFile, *zProc;
19856 failIfSafeMode(p, "cannot run .load in safe mode");
19858 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
19860 goto meta_command_exit;
19863 zProc = nArg>=3 ? azArg[2] : 0;
19865 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
19866 if( rc!=SQLITE_OK ){
19867 utf8_printf(stderr, "Error: %s\n", zErrMsg);
19868 sqlite3_free(zErrMsg);
19874 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
19875 failIfSafeMode(p, "cannot run .log in safe mode");
19877 raw_printf(stderr, "Usage: .log FILENAME\n");
19880 const char *zFile = azArg[1];
19881 output_file_close(p->pLog);
19882 p->pLog = output_file_open(zFile, 0);
19886 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19887 const char *zMode = nArg>=2 ? azArg[1] : "";
19888 int n2 = strlen30(zMode);
19890 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
19891 p->mode = MODE_Line;
19892 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19893 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
19894 p->mode = MODE_Column;
19895 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
19898 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19899 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
19900 p->mode = MODE_List;
19901 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
19902 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19903 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
19904 p->mode = MODE_Html;
19905 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
19906 p->mode = MODE_Tcl;
19907 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
19908 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19909 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
19910 p->mode = MODE_Csv;
19911 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19912 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19913 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
19914 p->mode = MODE_List;
19915 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
19916 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
19917 p->mode = MODE_Insert;
19918 set_table_name(p, nArg>=3 ? azArg[2] : "table");
19919 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
19920 p->mode = MODE_Quote;
19921 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19922 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19923 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
19924 p->mode = MODE_Ascii;
19925 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
19926 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
19927 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
19928 p->mode = MODE_Markdown;
19929 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
19930 p->mode = MODE_Table;
19931 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
19932 p->mode = MODE_Box;
19933 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
19934 p->mode = MODE_Json;
19935 }else if( nArg==1 ){
19936 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
19938 raw_printf(stderr, "Error: mode should be one of: "
19939 "ascii box column csv html insert json line list markdown "
19940 "quote table tabs tcl\n");
19943 p->cMode = p->mode;
19946 if( c=='n' && strcmp(azArg[0], "nonce")==0 ){
19948 raw_printf(stderr, "Usage: .nonce NONCE\n");
19950 }else if( p->zNonce==0 || strcmp(azArg[1],p->zNonce)!=0 ){
19951 raw_printf(stderr, "line %d: incorrect nonce: \"%s\"\n", p->lineno, azArg[1]);
19955 return 0; /* Return immediately to bypass the safe mode reset
19956 ** at the end of this procedure */
19960 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
19962 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
19963 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
19965 raw_printf(stderr, "Usage: .nullvalue STRING\n");
19970 #ifdef SQLITE_DEBUG
19971 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19973 for(i=1; i<nArg; i++){
19974 const char *z = azArg[i];
19975 if( z[0]=='-' && z[1]=='-' ) z++;
19976 if( strcmp(z,"-repeat")==0 ){
19978 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
19981 oomRepeat = (int)integerValue(azArg[++i]);
19983 }else if( IsDigit(z[0]) ){
19984 oomCounter = (int)integerValue(azArg[i]);
19986 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
19987 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
19992 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
19993 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
19996 #endif /* SQLITE_DEBUG */
19998 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
19999 char *zNewFilename = 0; /* Name of the database file to open */
20000 int iName = 1; /* Index in azArg[] of the filename */
20001 int newFlag = 0; /* True to delete file before opening */
20002 /* Close the existing database */
20003 session_close_all(p, -1);
20006 p->pAuxDb->zDbFilename = 0;
20007 sqlite3_free(p->pAuxDb->zFreeOnClose);
20008 p->pAuxDb->zFreeOnClose = 0;
20009 p->openMode = SHELL_OPEN_UNSPEC;
20012 /* Check for command-line arguments */
20013 for(iName=1; iName<nArg; iName++){
20014 const char *z = azArg[iName];
20015 if( optionMatch(z,"new") ){
20017 #ifdef SQLITE_HAVE_ZLIB
20018 }else if( optionMatch(z, "zip") ){
20019 p->openMode = SHELL_OPEN_ZIPFILE;
20021 }else if( optionMatch(z, "append") ){
20022 p->openMode = SHELL_OPEN_APPENDVFS;
20023 }else if( optionMatch(z, "readonly") ){
20024 p->openMode = SHELL_OPEN_READONLY;
20025 }else if( optionMatch(z, "nofollow") ){
20026 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
20027 #ifndef SQLITE_OMIT_DESERIALIZE
20028 }else if( optionMatch(z, "deserialize") ){
20029 p->openMode = SHELL_OPEN_DESERIALIZE;
20030 }else if( optionMatch(z, "hexdb") ){
20031 p->openMode = SHELL_OPEN_HEXDB;
20032 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
20033 p->szMax = integerValue(azArg[++iName]);
20034 #endif /* SQLITE_OMIT_DESERIALIZE */
20035 }else if( z[0]=='-' ){
20036 utf8_printf(stderr, "unknown option: %s\n", z);
20038 goto meta_command_exit;
20039 }else if( zNewFilename ){
20040 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
20042 goto meta_command_exit;
20044 zNewFilename = sqlite3_mprintf("%s", z);
20047 /* If a filename is specified, try to open it first */
20048 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
20049 if( newFlag && !p->bSafeMode ) shellDeleteFile(zNewFilename);
20051 && p->openMode!=SHELL_OPEN_HEXDB
20053 && strcmp(zNewFilename,":memory:")!=0
20055 failIfSafeMode(p, "cannot open disk-based database files in safe mode");
20057 p->pAuxDb->zDbFilename = zNewFilename;
20058 open_db(p, OPEN_DB_KEEPALIVE);
20060 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
20061 sqlite3_free(zNewFilename);
20063 p->pAuxDb->zFreeOnClose = zNewFilename;
20067 /* As a fall-back open a TEMP database */
20068 p->pAuxDb->zDbFilename = 0;
20074 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
20075 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
20082 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
20084 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
20088 }else if( strncmp(azArg[0],"once",n)==0 ){
20091 for(i=1; i<nArg; i++){
20092 char *z = azArg[i];
20094 if( z[1]=='-' ) z++;
20095 if( strcmp(z,"-bom")==0 ){
20097 }else if( c!='e' && strcmp(z,"-x")==0 ){
20098 eMode = 'x'; /* spreadsheet */
20099 }else if( c!='e' && strcmp(z,"-e")==0 ){
20100 eMode = 'e'; /* text editor */
20102 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
20104 showHelp(p->out, azArg[0]);
20106 goto meta_command_exit;
20108 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
20109 zFile = sqlite3_mprintf("%s", z);
20110 if( zFile[0]=='|' ){
20111 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
20115 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
20117 showHelp(p->out, azArg[0]);
20119 sqlite3_free(zFile);
20120 goto meta_command_exit;
20123 if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
20130 #ifndef SQLITE_NOHAVE_SYSTEM
20131 if( eMode=='e' || eMode=='x' ){
20135 /* spreadsheet mode. Output as CSV. */
20136 newTempFile(p, "csv");
20137 ShellClearFlag(p, SHFLG_Echo);
20138 p->mode = MODE_Csv;
20139 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
20140 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
20142 /* text editor mode */
20143 newTempFile(p, "txt");
20146 sqlite3_free(zFile);
20147 zFile = sqlite3_mprintf("%s", p->zTempFile);
20149 #endif /* SQLITE_NOHAVE_SYSTEM */
20150 if( zFile[0]=='|' ){
20151 #ifdef SQLITE_OMIT_POPEN
20152 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20156 p->out = popen(zFile + 1, "w");
20158 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
20162 if( bBOM ) fprintf(p->out,"\357\273\277");
20163 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20167 p->out = output_file_open(zFile, bTxtMode);
20169 if( strcmp(zFile,"off")!=0 ){
20170 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
20175 if( bBOM ) fprintf(p->out,"\357\273\277");
20176 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
20179 sqlite3_free(zFile);
20182 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
20184 if( nArg<=1 ) goto parameter_syntax_error;
20186 /* .parameter clear
20187 ** Clear all bind parameters by dropping the TEMP table that holds them.
20189 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
20190 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
20195 ** List all bind parameters.
20197 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
20198 sqlite3_stmt *pStmt = 0;
20201 rx = sqlite3_prepare_v2(p->db,
20202 "SELECT max(length(key)) "
20203 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20204 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20205 len = sqlite3_column_int(pStmt, 0);
20206 if( len>40 ) len = 40;
20208 sqlite3_finalize(pStmt);
20211 rx = sqlite3_prepare_v2(p->db,
20212 "SELECT key, quote(value) "
20213 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
20214 while( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
20215 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
20216 sqlite3_column_text(pStmt,1));
20218 sqlite3_finalize(pStmt);
20223 ** Make sure the TEMP table used to hold bind parameters exists.
20224 ** Create it if necessary.
20226 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
20227 bind_table_init(p);
20230 /* .parameter set NAME VALUE
20231 ** Set or reset a bind parameter. NAME should be the full parameter
20232 ** name exactly as it appears in the query. (ex: $abc, @def). The
20233 ** VALUE can be in either SQL literal notation, or if not it will be
20234 ** understood to be a text string.
20236 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
20239 sqlite3_stmt *pStmt;
20240 const char *zKey = azArg[2];
20241 const char *zValue = azArg[3];
20242 bind_table_init(p);
20243 zSql = sqlite3_mprintf(
20244 "REPLACE INTO temp.sqlite_parameters(key,value)"
20245 "VALUES(%Q,%s);", zKey, zValue);
20246 if( zSql==0 ) shell_out_of_memory();
20248 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20249 sqlite3_free(zSql);
20250 if( rx!=SQLITE_OK ){
20251 sqlite3_finalize(pStmt);
20253 zSql = sqlite3_mprintf(
20254 "REPLACE INTO temp.sqlite_parameters(key,value)"
20255 "VALUES(%Q,%Q);", zKey, zValue);
20256 if( zSql==0 ) shell_out_of_memory();
20257 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20258 sqlite3_free(zSql);
20259 if( rx!=SQLITE_OK ){
20260 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
20261 sqlite3_finalize(pStmt);
20266 sqlite3_step(pStmt);
20267 sqlite3_finalize(pStmt);
20270 /* .parameter unset NAME
20271 ** Remove the NAME binding from the parameter binding table, if it
20274 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
20275 char *zSql = sqlite3_mprintf(
20276 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
20277 if( zSql==0 ) shell_out_of_memory();
20278 sqlite3_exec(p->db, zSql, 0, 0, 0);
20279 sqlite3_free(zSql);
20281 /* If no command name matches, show a syntax error */
20282 parameter_syntax_error:
20283 showHelp(p->out, "parameter");
20286 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
20288 for(i=1; i<nArg; i++){
20289 if( i>1 ) raw_printf(p->out, " ");
20290 utf8_printf(p->out, "%s", azArg[i]);
20292 raw_printf(p->out, "\n");
20295 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
20296 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
20299 p->flgProgress = 0;
20302 for(i=1; i<nArg; i++){
20303 const char *z = azArg[i];
20306 if( z[0]=='-' ) z++;
20307 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
20308 p->flgProgress |= SHELL_PROGRESS_QUIET;
20311 if( strcmp(z,"reset")==0 ){
20312 p->flgProgress |= SHELL_PROGRESS_RESET;
20315 if( strcmp(z,"once")==0 ){
20316 p->flgProgress |= SHELL_PROGRESS_ONCE;
20319 if( strcmp(z,"limit")==0 ){
20321 utf8_printf(stderr, "Error: missing argument on --limit\n");
20323 goto meta_command_exit;
20325 p->mxProgress = (int)integerValue(azArg[++i]);
20329 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
20331 goto meta_command_exit;
20333 nn = (int)integerValue(z);
20337 sqlite3_progress_handler(p->db, nn, progress_handler, p);
20339 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
20341 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
20343 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
20346 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
20350 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
20354 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
20355 FILE *inSaved = p->in;
20356 int savedLineno = p->lineno;
20357 failIfSafeMode(p, "cannot run .read in safe mode");
20359 raw_printf(stderr, "Usage: .read FILE\n");
20361 goto meta_command_exit;
20363 if( azArg[1][0]=='|' ){
20364 #ifdef SQLITE_OMIT_POPEN
20365 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20369 p->in = popen(azArg[1]+1, "r");
20371 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20374 rc = process_input(p);
20378 }else if( (p->in = openChrSource(azArg[1]))==0 ){
20379 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
20382 rc = process_input(p);
20386 p->lineno = savedLineno;
20389 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20390 const char *zSrcFile;
20393 sqlite3_backup *pBackup;
20396 failIfSafeMode(p, "cannot run .restore in safe mode");
20398 zSrcFile = azArg[1];
20400 }else if( nArg==3 ){
20401 zSrcFile = azArg[2];
20404 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
20406 goto meta_command_exit;
20408 rc = sqlite3_open(zSrcFile, &pSrc);
20409 if( rc!=SQLITE_OK ){
20410 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
20415 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
20417 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20421 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
20422 || rc==SQLITE_BUSY ){
20423 if( rc==SQLITE_BUSY ){
20424 if( nTimeout++ >= 3 ) break;
20425 sqlite3_sleep(100);
20428 sqlite3_backup_finish(pBackup);
20429 if( rc==SQLITE_DONE ){
20431 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
20432 raw_printf(stderr, "Error: source database is busy\n");
20435 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20441 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
20443 p->scanstatsOn = (u8)booleanValue(azArg[1]);
20444 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20445 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
20448 raw_printf(stderr, "Usage: .scanstats on|off\n");
20453 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
20457 const char *zDiv = "(";
20458 const char *zName = 0;
20461 int bNoSystemTabs = 0;
20465 memcpy(&data, p, sizeof(data));
20466 data.showHeader = 0;
20467 data.cMode = data.mode = MODE_Semi;
20468 initText(&sSelect);
20469 for(ii=1; ii<nArg; ii++){
20470 if( optionMatch(azArg[ii],"indent") ){
20471 data.cMode = data.mode = MODE_Pretty;
20472 }else if( optionMatch(azArg[ii],"debug") ){
20474 }else if( optionMatch(azArg[ii],"nosys") ){
20476 }else if( azArg[ii][0]=='-' ){
20477 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
20479 goto meta_command_exit;
20480 }else if( zName==0 ){
20483 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
20485 goto meta_command_exit;
20489 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
20490 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
20491 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
20492 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
20494 char *new_argv[2], *new_colv[2];
20495 new_argv[0] = sqlite3_mprintf(
20496 "CREATE TABLE %s (\n"
20499 " tbl_name text,\n"
20500 " rootpage integer,\n"
20504 new_colv[0] = "sql";
20506 callback(&data, 1, new_argv, new_colv);
20507 sqlite3_free(new_argv[0]);
20511 sqlite3_stmt *pStmt = 0;
20512 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
20515 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20516 sqlite3_finalize(pStmt);
20518 goto meta_command_exit;
20520 appendText(&sSelect, "SELECT sql FROM", 0);
20522 while( sqlite3_step(pStmt)==SQLITE_ROW ){
20523 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
20525 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
20526 appendText(&sSelect, zDiv, 0);
20527 zDiv = " UNION ALL ";
20528 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
20529 if( sqlite3_stricmp(zDb, "main")!=0 ){
20530 appendText(&sSelect, zDb, '\'');
20532 appendText(&sSelect, "NULL", 0);
20534 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
20535 appendText(&sSelect, zScNum, 0);
20536 appendText(&sSelect, " AS snum, ", 0);
20537 appendText(&sSelect, zDb, '\'');
20538 appendText(&sSelect, " AS sname FROM ", 0);
20539 appendText(&sSelect, zDb, quoteChar(zDb));
20540 appendText(&sSelect, ".sqlite_schema", 0);
20542 sqlite3_finalize(pStmt);
20543 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
20545 appendText(&sSelect,
20546 " UNION ALL SELECT shell_module_schema(name),"
20547 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
20551 appendText(&sSelect, ") WHERE ", 0);
20553 char *zQarg = sqlite3_mprintf("%Q", zName);
20554 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20555 strchr(zName, '[') != 0;
20556 if( strchr(zName, '.') ){
20557 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20559 appendText(&sSelect, "lower(tbl_name)", 0);
20561 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
20562 appendText(&sSelect, zQarg, 0);
20564 appendText(&sSelect, " ESCAPE '\\' ", 0);
20566 appendText(&sSelect, " AND ", 0);
20567 sqlite3_free(zQarg);
20569 if( bNoSystemTabs ){
20570 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
20572 appendText(&sSelect, "sql IS NOT NULL"
20573 " ORDER BY snum, rowid", 0);
20575 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
20577 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
20579 freeText(&sSelect);
20582 utf8_printf(stderr,"Error: %s\n", zErrMsg);
20583 sqlite3_free(zErrMsg);
20585 }else if( rc != SQLITE_OK ){
20586 raw_printf(stderr,"Error: querying schema information\n");
20593 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
20594 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20595 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
20598 #if defined(SQLITE_ENABLE_SESSION)
20599 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
20600 struct AuxDb *pAuxDb = p->pAuxDb;
20601 OpenSession *pSession = &pAuxDb->aSession[0];
20602 char **azCmd = &azArg[1];
20604 int nCmd = nArg - 1;
20606 if( nArg<=1 ) goto session_syntax_error;
20609 for(iSes=0; iSes<pAuxDb->nSession; iSes++){
20610 if( strcmp(pAuxDb->aSession[iSes].zName, azArg[1])==0 ) break;
20612 if( iSes<pAuxDb->nSession ){
20613 pSession = &pAuxDb->aSession[iSes];
20617 pSession = &pAuxDb->aSession[0];
20622 /* .session attach TABLE
20623 ** Invoke the sqlite3session_attach() interface to attach a particular
20624 ** table so that it is never filtered.
20626 if( strcmp(azCmd[0],"attach")==0 ){
20627 if( nCmd!=2 ) goto session_syntax_error;
20628 if( pSession->p==0 ){
20630 raw_printf(stderr, "ERROR: No sessions are open\n");
20632 rc = sqlite3session_attach(pSession->p, azCmd[1]);
20634 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
20640 /* .session changeset FILE
20641 ** .session patchset FILE
20642 ** Write a changeset or patchset into a file. The file is overwritten.
20644 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
20646 failIfSafeMode(p, "cannot run \".session %s\" in safe mode", azCmd[0]);
20647 if( nCmd!=2 ) goto session_syntax_error;
20648 if( pSession->p==0 ) goto session_not_open;
20649 out = fopen(azCmd[1], "wb");
20651 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
20656 if( azCmd[0][0]=='c' ){
20657 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
20659 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
20662 printf("Error: error code %d\n", rc);
20666 && fwrite(pChng, szChng, 1, out)!=1 ){
20667 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
20670 sqlite3_free(pChng);
20676 ** Close the identified session
20678 if( strcmp(azCmd[0], "close")==0 ){
20679 if( nCmd!=1 ) goto session_syntax_error;
20680 if( pAuxDb->nSession ){
20681 session_close(pSession);
20682 pAuxDb->aSession[iSes] = pAuxDb->aSession[--pAuxDb->nSession];
20686 /* .session enable ?BOOLEAN?
20687 ** Query or set the enable flag
20689 if( strcmp(azCmd[0], "enable")==0 ){
20691 if( nCmd>2 ) goto session_syntax_error;
20692 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20693 if( pAuxDb->nSession ){
20694 ii = sqlite3session_enable(pSession->p, ii);
20695 utf8_printf(p->out, "session %s enable flag = %d\n",
20696 pSession->zName, ii);
20700 /* .session filter GLOB ....
20701 ** Set a list of GLOB patterns of table names to be excluded.
20703 if( strcmp(azCmd[0], "filter")==0 ){
20705 if( nCmd<2 ) goto session_syntax_error;
20706 if( pAuxDb->nSession ){
20707 for(ii=0; ii<pSession->nFilter; ii++){
20708 sqlite3_free(pSession->azFilter[ii]);
20710 sqlite3_free(pSession->azFilter);
20711 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
20712 pSession->azFilter = sqlite3_malloc( nByte );
20713 if( pSession->azFilter==0 ){
20714 raw_printf(stderr, "Error: out or memory\n");
20717 for(ii=1; ii<nCmd; ii++){
20718 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20720 pSession->nFilter = ii-1;
20724 /* .session indirect ?BOOLEAN?
20725 ** Query or set the indirect flag
20727 if( strcmp(azCmd[0], "indirect")==0 ){
20729 if( nCmd>2 ) goto session_syntax_error;
20730 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20731 if( pAuxDb->nSession ){
20732 ii = sqlite3session_indirect(pSession->p, ii);
20733 utf8_printf(p->out, "session %s indirect flag = %d\n",
20734 pSession->zName, ii);
20738 /* .session isempty
20739 ** Determine if the session is empty
20741 if( strcmp(azCmd[0], "isempty")==0 ){
20743 if( nCmd!=1 ) goto session_syntax_error;
20744 if( pAuxDb->nSession ){
20745 ii = sqlite3session_isempty(pSession->p);
20746 utf8_printf(p->out, "session %s isempty flag = %d\n",
20747 pSession->zName, ii);
20752 ** List all currently open sessions
20754 if( strcmp(azCmd[0],"list")==0 ){
20755 for(i=0; i<pAuxDb->nSession; i++){
20756 utf8_printf(p->out, "%d %s\n", i, pAuxDb->aSession[i].zName);
20760 /* .session open DB NAME
20761 ** Open a new session called NAME on the attached database DB.
20762 ** DB is normally "main".
20764 if( strcmp(azCmd[0],"open")==0 ){
20766 if( nCmd!=3 ) goto session_syntax_error;
20768 if( zName[0]==0 ) goto session_syntax_error;
20769 for(i=0; i<pAuxDb->nSession; i++){
20770 if( strcmp(pAuxDb->aSession[i].zName,zName)==0 ){
20771 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
20772 goto meta_command_exit;
20775 if( pAuxDb->nSession>=ArraySize(pAuxDb->aSession) ){
20776 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(pAuxDb->aSession));
20777 goto meta_command_exit;
20779 pSession = &pAuxDb->aSession[pAuxDb->nSession];
20780 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
20782 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
20784 goto meta_command_exit;
20786 pSession->nFilter = 0;
20787 sqlite3session_table_filter(pSession->p, session_filter, pSession);
20788 pAuxDb->nSession++;
20789 pSession->zName = sqlite3_mprintf("%s", zName);
20791 /* If no command name matches, show a syntax error */
20792 session_syntax_error:
20793 showHelp(p->out, "session");
20797 #ifdef SQLITE_DEBUG
20798 /* Undocumented commands for internal testing. Subject to change
20799 ** without notice. */
20800 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
20801 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
20803 for(i=1; i<nArg; i++){
20804 v = booleanValue(azArg[i]);
20805 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
20808 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
20809 int i; sqlite3_int64 v;
20810 for(i=1; i<nArg; i++){
20812 v = integerValue(azArg[i]);
20813 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
20814 utf8_printf(p->out, "%s", zBuf);
20820 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
20821 int bIsInit = 0; /* True to initialize the SELFTEST table */
20822 int bVerbose = 0; /* Verbose output */
20823 int bSelftestExists; /* True if SELFTEST already exists */
20824 int i, k; /* Loop counters */
20825 int nTest = 0; /* Number of tests runs */
20826 int nErr = 0; /* Number of errors seen */
20827 ShellText str; /* Answer for a query */
20828 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
20831 for(i=1; i<nArg; i++){
20832 const char *z = azArg[i];
20833 if( z[0]=='-' && z[1]=='-' ) z++;
20834 if( strcmp(z,"-init")==0 ){
20837 if( strcmp(z,"-v")==0 ){
20841 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20842 azArg[i], azArg[0]);
20843 raw_printf(stderr, "Should be one of: --init -v\n");
20845 goto meta_command_exit;
20848 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
20850 bSelftestExists = 0;
20852 bSelftestExists = 1;
20855 createSelftestTable(p);
20856 bSelftestExists = 1;
20859 appendText(&str, "x", 0);
20860 for(k=bSelftestExists; k>=0; k--){
20862 rc = sqlite3_prepare_v2(p->db,
20863 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
20866 rc = sqlite3_prepare_v2(p->db,
20867 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
20868 " (1,'run','PRAGMA integrity_check','ok')",
20872 raw_printf(stderr, "Error querying the selftest table\n");
20874 sqlite3_finalize(pStmt);
20875 goto meta_command_exit;
20877 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
20878 int tno = sqlite3_column_int(pStmt, 0);
20879 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20880 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20881 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20885 char *zQuote = sqlite3_mprintf("%q", zSql);
20886 printf("%d: %s %s\n", tno, zOp, zSql);
20887 sqlite3_free(zQuote);
20889 if( strcmp(zOp,"memo")==0 ){
20890 utf8_printf(p->out, "%s\n", zSql);
20892 if( strcmp(zOp,"run")==0 ){
20896 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
20899 utf8_printf(p->out, "Result: %s\n", str.z);
20901 if( rc || zErrMsg ){
20904 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
20905 sqlite3_free(zErrMsg);
20906 }else if( strcmp(zAns,str.z)!=0 ){
20909 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
20910 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
20914 utf8_printf(stderr,
20915 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
20919 } /* End loop over rows of content from SELFTEST */
20920 sqlite3_finalize(pStmt);
20921 } /* End loop over k */
20923 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
20926 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
20927 if( nArg<2 || nArg>3 ){
20928 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
20932 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
20933 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
20936 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
20937 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
20941 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
20942 const char *zLike = 0; /* Which table to checksum. 0 means everything */
20943 int i; /* Loop counter */
20944 int bSchema = 0; /* Also hash the schema */
20945 int bSeparate = 0; /* Hash each table separately */
20946 int iSize = 224; /* Hash algorithm to use */
20947 int bDebug = 0; /* Only show the query that would have run */
20948 sqlite3_stmt *pStmt; /* For querying tables names */
20949 char *zSql; /* SQL to be run */
20950 char *zSep; /* Separator */
20951 ShellText sSql; /* Complete SQL for the query to run the hash */
20952 ShellText sQuery; /* Set of queries used to read all content */
20954 for(i=1; i<nArg; i++){
20955 const char *z = azArg[i];
20958 if( z[0]=='-' ) z++;
20959 if( strcmp(z,"schema")==0 ){
20962 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
20963 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
20965 iSize = atoi(&z[5]);
20967 if( strcmp(z,"debug")==0 ){
20971 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20972 azArg[i], azArg[0]);
20973 showHelp(p->out, azArg[0]);
20975 goto meta_command_exit;
20978 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
20980 goto meta_command_exit;
20984 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
20988 zSql = "SELECT lower(name) FROM sqlite_schema"
20989 " WHERE type='table' AND coalesce(rootpage,0)>1"
20990 " UNION ALL SELECT 'sqlite_schema'"
20991 " ORDER BY 1 collate nocase";
20993 zSql = "SELECT lower(name) FROM sqlite_schema"
20994 " WHERE type='table' AND coalesce(rootpage,0)>1"
20995 " AND name NOT LIKE 'sqlite_%'"
20996 " ORDER BY 1 collate nocase";
20998 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
21001 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
21003 while( SQLITE_ROW==sqlite3_step(pStmt) ){
21004 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
21005 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
21006 if( strncmp(zTab, "sqlite_",7)!=0 ){
21007 appendText(&sQuery,"SELECT * FROM ", 0);
21008 appendText(&sQuery,zTab,'"');
21009 appendText(&sQuery," NOT INDEXED;", 0);
21010 }else if( strcmp(zTab, "sqlite_schema")==0 ){
21011 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
21012 " ORDER BY name;", 0);
21013 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
21014 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
21015 " ORDER BY name;", 0);
21016 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
21017 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
21018 " ORDER BY tbl,idx;", 0);
21019 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
21020 appendText(&sQuery, "SELECT * FROM ", 0);
21021 appendText(&sQuery, zTab, 0);
21022 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
21024 appendText(&sSql, zSep, 0);
21025 appendText(&sSql, sQuery.z, '\'');
21027 appendText(&sSql, ",", 0);
21028 appendText(&sSql, zTab, '\'');
21031 sqlite3_finalize(pStmt);
21033 zSql = sqlite3_mprintf(
21035 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
21036 " FROM [sha3sum$query]",
21039 zSql = sqlite3_mprintf(
21041 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
21042 " FROM [sha3sum$query]",
21048 utf8_printf(p->out, "%s\n", zSql);
21050 shell_exec(p, zSql, 0);
21052 sqlite3_free(zSql);
21055 #ifndef SQLITE_NOHAVE_SYSTEM
21057 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
21061 failIfSafeMode(p, "cannot run .%s in safe mode", azArg[0]);
21063 raw_printf(stderr, "Usage: .system COMMAND\n");
21065 goto meta_command_exit;
21067 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
21068 for(i=2; i<nArg; i++){
21069 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
21073 sqlite3_free(zCmd);
21074 if( x ) raw_printf(stderr, "System command returns %d\n", x);
21076 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
21078 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
21079 static const char *azBool[] = { "off", "on", "trigger", "full"};
21083 raw_printf(stderr, "Usage: .show\n");
21085 goto meta_command_exit;
21087 utf8_printf(p->out, "%12.12s: %s\n","echo",
21088 azBool[ShellHasFlag(p, SHFLG_Echo)]);
21089 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
21090 utf8_printf(p->out, "%12.12s: %s\n","explain",
21091 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
21092 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
21093 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
21094 utf8_printf(p->out, "%12.12s: ", "nullvalue");
21095 output_c_string(p->out, p->nullValue);
21096 raw_printf(p->out, "\n");
21097 utf8_printf(p->out,"%12.12s: %s\n","output",
21098 strlen30(p->outfile) ? p->outfile : "stdout");
21099 utf8_printf(p->out,"%12.12s: ", "colseparator");
21100 output_c_string(p->out, p->colSeparator);
21101 raw_printf(p->out, "\n");
21102 utf8_printf(p->out,"%12.12s: ", "rowseparator");
21103 output_c_string(p->out, p->rowSeparator);
21104 raw_printf(p->out, "\n");
21105 switch( p->statsOn ){
21106 case 0: zOut = "off"; break;
21107 default: zOut = "on"; break;
21108 case 2: zOut = "stmt"; break;
21109 case 3: zOut = "vmstep"; break;
21111 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
21112 utf8_printf(p->out, "%12.12s: ", "width");
21113 for (i=0;i<p->nWidth;i++) {
21114 raw_printf(p->out, "%d ", p->colWidth[i]);
21116 raw_printf(p->out, "\n");
21117 utf8_printf(p->out, "%12.12s: %s\n", "filename",
21118 p->pAuxDb->zDbFilename ? p->pAuxDb->zDbFilename : "");
21121 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
21123 if( strcmp(azArg[1],"stmt")==0 ){
21125 }else if( strcmp(azArg[1],"vmstep")==0 ){
21128 p->statsOn = (u8)booleanValue(azArg[1]);
21130 }else if( nArg==1 ){
21131 display_stats(p->db, p, 0);
21133 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
21138 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
21139 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
21140 || strncmp(azArg[0], "indexes", n)==0) )
21142 sqlite3_stmt *pStmt;
21149 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
21151 sqlite3_finalize(pStmt);
21152 return shellDatabaseError(p->db);
21155 if( nArg>2 && c=='i' ){
21156 /* It is an historical accident that the .indexes command shows an error
21157 ** when called with the wrong number of arguments whereas the .tables
21158 ** command does not. */
21159 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
21161 sqlite3_finalize(pStmt);
21162 goto meta_command_exit;
21164 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
21165 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
21166 if( zDbName==0 ) continue;
21167 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
21168 if( sqlite3_stricmp(zDbName, "main")==0 ){
21169 appendText(&s, "SELECT name FROM ", 0);
21171 appendText(&s, "SELECT ", 0);
21172 appendText(&s, zDbName, '\'');
21173 appendText(&s, "||'.'||name FROM ", 0);
21175 appendText(&s, zDbName, '"');
21176 appendText(&s, ".sqlite_schema ", 0);
21178 appendText(&s," WHERE type IN ('table','view')"
21179 " AND name NOT LIKE 'sqlite_%'"
21180 " AND name LIKE ?1", 0);
21182 appendText(&s," WHERE type='index'"
21183 " AND tbl_name LIKE ?1", 0);
21186 rc = sqlite3_finalize(pStmt);
21187 if( rc==SQLITE_OK ){
21188 appendText(&s, " ORDER BY 1", 0);
21189 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
21192 if( rc ) return shellDatabaseError(p->db);
21194 /* Run the SQL statement prepared by the above block. Store the results
21195 ** as an array of nul-terminated strings in azResult[]. */
21199 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
21201 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
21203 while( sqlite3_step(pStmt)==SQLITE_ROW ){
21204 if( nRow>=nAlloc ){
21206 int n2 = nAlloc*2 + 10;
21207 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
21208 if( azNew==0 ) shell_out_of_memory();
21212 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
21213 if( 0==azResult[nRow] ) shell_out_of_memory();
21216 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
21217 rc = shellDatabaseError(p->db);
21220 /* Pretty-print the contents of array azResult[] to the output */
21221 if( rc==0 && nRow>0 ){
21222 int len, maxlen = 0;
21224 int nPrintCol, nPrintRow;
21225 for(i=0; i<nRow; i++){
21226 len = strlen30(azResult[i]);
21227 if( len>maxlen ) maxlen = len;
21229 nPrintCol = 80/(maxlen+2);
21230 if( nPrintCol<1 ) nPrintCol = 1;
21231 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
21232 for(i=0; i<nPrintRow; i++){
21233 for(j=i; j<nRow; j+=nPrintRow){
21234 char *zSp = j<nPrintRow ? "" : " ";
21235 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
21236 azResult[j] ? azResult[j]:"");
21238 raw_printf(p->out, "\n");
21242 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
21243 sqlite3_free(azResult);
21246 /* Begin redirecting output to the file "testcase-out.txt" */
21247 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
21249 p->out = output_file_open("testcase-out.txt", 0);
21251 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
21254 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
21256 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
21260 #ifndef SQLITE_UNTESTABLE
21261 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
21262 static const struct {
21263 const char *zCtrlName; /* Name of a test-control option */
21264 int ctrlCode; /* Integer code for that option */
21265 const char *zUsage; /* Usage notes */
21267 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
21268 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
21269 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
21270 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
21271 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
21272 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
21273 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
21274 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
21275 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
21276 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
21277 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
21278 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
21280 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
21282 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
21283 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
21284 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
21285 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
21286 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" },
21287 { "sorter_mmap", SQLITE_TESTCTRL_SORTER_MMAP, "NMAX" },
21288 { "tune", SQLITE_TESTCTRL_TUNE, "ID VALUE" },
21292 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
21295 const char *zCmd = 0;
21298 zCmd = nArg>=2 ? azArg[1] : "help";
21300 /* The argument can optionally begin with "-" or "--" */
21301 if( zCmd[0]=='-' && zCmd[1] ){
21303 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
21306 /* --help lists all test-controls */
21307 if( strcmp(zCmd,"help")==0 ){
21308 utf8_printf(p->out, "Available test-controls:\n");
21309 for(i=0; i<ArraySize(aCtrl); i++){
21310 utf8_printf(p->out, " .testctrl %s %s\n",
21311 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
21314 goto meta_command_exit;
21317 /* convert testctrl text option to value. allow any unique prefix
21318 ** of the option name, or a numerical value. */
21319 n2 = strlen30(zCmd);
21320 for(i=0; i<ArraySize(aCtrl); i++){
21321 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
21323 testctrl = aCtrl[i].ctrlCode;
21326 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
21327 "Use \".testctrl --help\" for help\n", zCmd);
21329 goto meta_command_exit;
21334 utf8_printf(stderr,"Error: unknown test-control: %s\n"
21335 "Use \".testctrl --help\" for help\n", zCmd);
21339 /* sqlite3_test_control(int, db, int) */
21340 case SQLITE_TESTCTRL_OPTIMIZATIONS:
21342 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
21343 rc2 = sqlite3_test_control(testctrl, p->db, opt);
21348 /* sqlite3_test_control(int) */
21349 case SQLITE_TESTCTRL_PRNG_SAVE:
21350 case SQLITE_TESTCTRL_PRNG_RESTORE:
21351 case SQLITE_TESTCTRL_BYTEORDER:
21353 rc2 = sqlite3_test_control(testctrl);
21354 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
21358 /* sqlite3_test_control(int, uint) */
21359 case SQLITE_TESTCTRL_PENDING_BYTE:
21361 unsigned int opt = (unsigned int)integerValue(azArg[2]);
21362 rc2 = sqlite3_test_control(testctrl, opt);
21367 /* sqlite3_test_control(int, int, sqlite3*) */
21368 case SQLITE_TESTCTRL_PRNG_SEED:
21369 if( nArg==3 || nArg==4 ){
21370 int ii = (int)integerValue(azArg[2]);
21372 if( ii==0 && strcmp(azArg[2],"random")==0 ){
21373 sqlite3_randomness(sizeof(ii),&ii);
21374 printf("-- random seed: %d\n", ii);
21380 /* Make sure the schema has been loaded */
21381 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
21383 rc2 = sqlite3_test_control(testctrl, ii, db);
21388 /* sqlite3_test_control(int, int) */
21389 case SQLITE_TESTCTRL_ASSERT:
21390 case SQLITE_TESTCTRL_ALWAYS:
21392 int opt = booleanValue(azArg[2]);
21393 rc2 = sqlite3_test_control(testctrl, opt);
21398 /* sqlite3_test_control(int, int) */
21399 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
21400 case SQLITE_TESTCTRL_NEVER_CORRUPT:
21402 int opt = booleanValue(azArg[2]);
21403 rc2 = sqlite3_test_control(testctrl, opt);
21408 /* sqlite3_test_control(sqlite3*) */
21409 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
21410 rc2 = sqlite3_test_control(testctrl, p->db);
21414 case SQLITE_TESTCTRL_IMPOSTER:
21416 rc2 = sqlite3_test_control(testctrl, p->db,
21418 integerValue(azArg[3]),
21419 integerValue(azArg[4]));
21424 case SQLITE_TESTCTRL_SEEK_COUNT: {
21426 rc2 = sqlite3_test_control(testctrl, p->db, &x);
21427 utf8_printf(p->out, "%llu\n", x);
21433 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
21435 sqlite3_test_control(testctrl, p->out);
21441 #ifdef SQLITE_DEBUG
21442 case SQLITE_TESTCTRL_TUNE: {
21444 int id = (int)integerValue(azArg[2]);
21445 int val = (int)integerValue(azArg[3]);
21446 sqlite3_test_control(testctrl, id, &val);
21448 }else if( nArg==3 ){
21449 int id = (int)integerValue(azArg[2]);
21450 sqlite3_test_control(testctrl, -id, &rc2);
21452 }else if( nArg==2 ){
21456 rc2 = sqlite3_test_control(testctrl, -id, &val);
21457 if( rc2!=SQLITE_OK ) break;
21458 if( id>1 ) utf8_printf(p->out, " ");
21459 utf8_printf(p->out, "%d: %d", id, val);
21462 if( id>1 ) utf8_printf(p->out, "\n");
21468 case SQLITE_TESTCTRL_SORTER_MMAP:
21470 int opt = (unsigned int)integerValue(azArg[2]);
21471 rc2 = sqlite3_test_control(testctrl, p->db, opt);
21477 if( isOk==0 && iCtrl>=0 ){
21478 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
21480 }else if( isOk==1 ){
21481 raw_printf(p->out, "%d\n", rc2);
21482 }else if( isOk==2 ){
21483 raw_printf(p->out, "0x%08x\n", rc2);
21486 #endif /* !defined(SQLITE_UNTESTABLE) */
21488 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
21490 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
21493 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
21495 enableTimer = booleanValue(azArg[1]);
21496 if( enableTimer && !HAS_TIMER ){
21497 raw_printf(stderr, "Error: timer not available on this system.\n");
21501 raw_printf(stderr, "Usage: .timer on|off\n");
21506 #ifndef SQLITE_OMIT_TRACE
21507 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
21511 for(jj=1; jj<nArg; jj++){
21512 const char *z = azArg[jj];
21514 if( optionMatch(z, "expanded") ){
21515 p->eTraceType = SHELL_TRACE_EXPANDED;
21517 #ifdef SQLITE_ENABLE_NORMALIZE
21518 else if( optionMatch(z, "normalized") ){
21519 p->eTraceType = SHELL_TRACE_NORMALIZED;
21522 else if( optionMatch(z, "plain") ){
21523 p->eTraceType = SHELL_TRACE_PLAIN;
21525 else if( optionMatch(z, "profile") ){
21526 mType |= SQLITE_TRACE_PROFILE;
21528 else if( optionMatch(z, "row") ){
21529 mType |= SQLITE_TRACE_ROW;
21531 else if( optionMatch(z, "stmt") ){
21532 mType |= SQLITE_TRACE_STMT;
21534 else if( optionMatch(z, "close") ){
21535 mType |= SQLITE_TRACE_CLOSE;
21538 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
21540 goto meta_command_exit;
21543 output_file_close(p->traceOut);
21544 p->traceOut = output_file_open(azArg[1], 0);
21547 if( p->traceOut==0 ){
21548 sqlite3_trace_v2(p->db, 0, 0, 0);
21550 if( mType==0 ) mType = SQLITE_TRACE_STMT;
21551 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
21554 #endif /* !defined(SQLITE_OMIT_TRACE) */
21556 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21557 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
21562 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
21564 goto meta_command_exit;
21568 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
21569 lenOpt = (int)strlen(zOpt);
21570 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
21571 assert( azArg[nArg]==0 );
21572 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
21574 for(ii=1; ii<nArg; ii++){
21575 sqlite3_create_module(p->db, azArg[ii], 0, 0);
21581 #if SQLITE_USER_AUTHENTICATION
21582 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
21584 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
21586 goto meta_command_exit;
21589 if( strcmp(azArg[1],"login")==0 ){
21591 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
21593 goto meta_command_exit;
21595 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
21596 strlen30(azArg[3]));
21598 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
21601 }else if( strcmp(azArg[1],"add")==0 ){
21603 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
21605 goto meta_command_exit;
21607 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21608 booleanValue(azArg[4]));
21610 raw_printf(stderr, "User-Add failed: %d\n", rc);
21613 }else if( strcmp(azArg[1],"edit")==0 ){
21615 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
21617 goto meta_command_exit;
21619 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21620 booleanValue(azArg[4]));
21622 raw_printf(stderr, "User-Edit failed: %d\n", rc);
21625 }else if( strcmp(azArg[1],"delete")==0 ){
21627 raw_printf(stderr, "Usage: .user delete USER\n");
21629 goto meta_command_exit;
21631 rc = sqlite3_user_delete(p->db, azArg[2]);
21633 raw_printf(stderr, "User-Delete failed: %d\n", rc);
21637 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
21639 goto meta_command_exit;
21642 #endif /* SQLITE_USER_AUTHENTICATION */
21644 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
21645 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
21646 sqlite3_libversion(), sqlite3_sourceid());
21647 #if SQLITE_HAVE_ZLIB
21648 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
21650 #define CTIMEOPT_VAL_(opt) #opt
21651 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
21652 #if defined(__clang__) && defined(__clang_major__)
21653 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
21654 CTIMEOPT_VAL(__clang_minor__) "."
21655 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
21656 #elif defined(_MSC_VER)
21657 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
21658 #elif defined(__GNUC__) && defined(__VERSION__)
21659 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
21663 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
21664 const char *zDbName = nArg==2 ? azArg[1] : "main";
21665 sqlite3_vfs *pVfs = 0;
21667 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
21669 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
21670 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
21671 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
21672 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21677 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
21679 sqlite3_vfs *pCurrent = 0;
21681 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
21683 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
21684 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
21685 pVfs==pCurrent ? " <--- CURRENT" : "");
21686 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
21687 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
21688 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21690 raw_printf(p->out, "-----------------------------------\n");
21695 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
21696 const char *zDbName = nArg==2 ? azArg[1] : "main";
21697 char *zVfsName = 0;
21699 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
21701 utf8_printf(p->out, "%s\n", zVfsName);
21702 sqlite3_free(zVfsName);
21707 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
21708 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21709 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
21712 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
21714 assert( nArg<=ArraySize(azArg) );
21715 p->nWidth = nArg-1;
21716 p->colWidth = realloc(p->colWidth, (p->nWidth+1)*sizeof(int)*2);
21717 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
21718 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
21719 for(j=1; j<nArg; j++){
21720 p->colWidth[j-1] = (int)integerValue(azArg[j]);
21725 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
21726 " \"%s\". Enter \".help\" for help\n", azArg[0]);
21733 if( p->outCount==0 ) output_reset(p);
21735 p->bSafeMode = p->bSafeModePersist;
21739 /* Line scan result and intermediate states (supporting scan resumption)
21742 # define CHAR_BIT 8
21745 QSS_HasDark = 1<<CHAR_BIT, QSS_EndingSemi = 2<<CHAR_BIT,
21746 QSS_CharMask = (1<<CHAR_BIT)-1, QSS_ScanMask = 3<<CHAR_BIT,
21749 #define QSS_SETV(qss, newst) ((newst) | ((qss) & QSS_ScanMask))
21750 #define QSS_INPLAIN(qss) (((qss)&QSS_CharMask)==QSS_Start)
21751 #define QSS_PLAINWHITE(qss) (((qss)&~QSS_EndingSemi)==QSS_Start)
21752 #define QSS_PLAINDARK(qss) (((qss)&~QSS_EndingSemi)==QSS_HasDark)
21753 #define QSS_SEMITERM(qss) (((qss)&~QSS_HasDark)==QSS_EndingSemi)
21756 ** Scan line for classification to guide shell's handling.
21757 ** The scan is resumable for subsequent lines when prior
21758 ** return values are passed as the 2nd argument.
21760 static QuickScanState quickscan(char *zLine, QuickScanState qss){
21762 char cWait = (char)qss; /* intentional narrowing loss */
21765 assert( cWait==0 );
21766 while( (cin = *zLine++)!=0 ){
21773 while((cin = *++zLine)!=0 )
21778 qss |= QSS_EndingSemi;
21784 qss = QSS_SETV(qss, cWait);
21791 case '`': case '\'': case '"':
21793 qss = QSS_HasDark | cWait;
21798 qss = (qss & ~QSS_EndingSemi) | QSS_HasDark;
21802 while( (cin = *zLine++)!=0 ){
21806 if( *zLine != '/' )
21810 qss = QSS_SETV(qss, 0);
21812 case '`': case '\'': case '"':
21820 qss = QSS_SETV(qss, 0);
21822 default: assert(0);
21831 ** Return TRUE if the line typed in is an SQL command terminator other
21832 ** than a semi-colon. The SQL Server style "go" command is understood
21833 ** as is the Oracle "/".
21835 static int line_is_command_terminator(char *zLine){
21836 while( IsSpace(zLine[0]) ){ zLine++; };
21837 if( zLine[0]=='/' )
21838 zLine += 1; /* Oracle */
21839 else if ( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o' )
21840 zLine += 2; /* SQL Server */
21843 return quickscan(zLine,QSS_Start)==QSS_Start;
21847 ** We need a default sqlite3_complete() implementation to use in case
21848 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
21849 ** any arbitrary text is a complete SQL statement. This is not very
21850 ** user-friendly, but it does seem to work.
21852 #ifdef SQLITE_OMIT_COMPLETE
21853 #define sqlite3_complete(x) 1
21857 ** Return true if zSql is a complete SQL statement. Return false if it
21858 ** ends in the middle of a string literal or C-style comment.
21860 static int line_is_complete(char *zSql, int nSql){
21862 if( zSql==0 ) return 1;
21865 rc = sqlite3_complete(zSql);
21871 ** Run a single line of SQL. Return the number of errors.
21873 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
21878 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
21879 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
21881 rc = shell_exec(p, zSql, &zErrMsg);
21883 if( rc || zErrMsg ){
21885 if( in!=0 || !stdin_is_interactive ){
21886 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
21887 "Error: near line %d:", startline);
21889 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
21892 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
21893 sqlite3_free(zErrMsg);
21896 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
21899 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
21900 char zLineBuf[2000];
21901 sqlite3_snprintf(sizeof(zLineBuf), zLineBuf,
21902 "changes: %lld total_changes: %lld",
21903 sqlite3_changes64(p->db), sqlite3_total_changes64(p->db));
21904 raw_printf(p->out, "%s\n", zLineBuf);
21911 ** Read input from *in and process it. If *in==0 then input
21912 ** is interactive - the user is typing it it. Otherwise, input
21913 ** is coming from a file or device. A prompt is issued and history
21914 ** is saved only if input is interactive. An interrupt signal will
21915 ** cause this routine to exit immediately, unless input is interactive.
21917 ** Return the number of errors.
21919 static int process_input(ShellState *p){
21920 char *zLine = 0; /* A single input line */
21921 char *zSql = 0; /* Accumulated SQL text */
21922 int nLine; /* Length of current line */
21923 int nSql = 0; /* Bytes of zSql[] used */
21924 int nAlloc = 0; /* Allocated zSql[] space */
21925 int rc; /* Error code */
21926 int errCnt = 0; /* Number of errors seen */
21927 int startline = 0; /* Line number for start of current input */
21928 QuickScanState qss = QSS_Start; /* Accumulated line status (so far) */
21931 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
21933 zLine = one_input_line(p->in, zLine, nSql>0);
21936 if( p->in==0 && stdin_is_interactive ) printf("\n");
21939 if( seenInterrupt ){
21940 if( p->in!=0 ) break;
21944 if( QSS_INPLAIN(qss)
21945 && line_is_command_terminator(zLine)
21946 && line_is_complete(zSql, nSql) ){
21947 memcpy(zLine,";",2);
21949 qss = quickscan(zLine, qss);
21950 if( QSS_PLAINWHITE(qss) && nSql==0 ){
21951 if( ShellHasFlag(p, SHFLG_Echo) )
21952 printf("%s\n", zLine);
21953 /* Just swallow single-line whitespace */
21957 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
21958 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21959 if( zLine[0]=='.' ){
21960 rc = do_meta_command(zLine, p);
21961 if( rc==2 ){ /* exit requested */
21970 /* No single-line dispositions remain; accumulate line(s). */
21971 nLine = strlen30(zLine);
21972 if( nSql+nLine+2>=nAlloc ){
21973 /* Grow buffer by half-again increments when big. */
21974 nAlloc = nSql+(nSql>>1)+nLine+100;
21975 zSql = realloc(zSql, nAlloc);
21976 if( zSql==0 ) shell_out_of_memory();
21980 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
21981 assert( nAlloc>0 && zSql!=0 );
21982 memcpy(zSql, zLine+i, nLine+1-i);
21983 startline = p->lineno;
21986 zSql[nSql++] = '\n';
21987 memcpy(zSql+nSql, zLine, nLine+1);
21990 if( nSql && QSS_SEMITERM(qss) && sqlite3_complete(zSql) ){
21991 errCnt += runOneSqlLine(p, zSql, p->in, startline);
21999 p->bSafeMode = p->bSafeModePersist;
22001 }else if( nSql && QSS_PLAINWHITE(qss) ){
22002 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
22007 if( nSql && QSS_PLAINDARK(qss) ){
22008 errCnt += runOneSqlLine(p, zSql, p->in, startline);
22016 ** Return a pathname which is the user's home directory. A
22017 ** 0 return indicates an error of some kind.
22019 static char *find_home_dir(int clearFlag){
22020 static char *home_dir = NULL;
22026 if( home_dir ) return home_dir;
22028 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
22029 && !defined(__RTP__) && !defined(_WRS_KERNEL)
22031 struct passwd *pwent;
22032 uid_t uid = getuid();
22033 if( (pwent=getpwuid(uid)) != NULL) {
22034 home_dir = pwent->pw_dir;
22039 #if defined(_WIN32_WCE)
22040 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
22045 #if defined(_WIN32) || defined(WIN32)
22047 home_dir = getenv("USERPROFILE");
22052 home_dir = getenv("HOME");
22055 #if defined(_WIN32) || defined(WIN32)
22057 char *zDrive, *zPath;
22059 zDrive = getenv("HOMEDRIVE");
22060 zPath = getenv("HOMEPATH");
22061 if( zDrive && zPath ){
22062 n = strlen30(zDrive) + strlen30(zPath) + 1;
22063 home_dir = malloc( n );
22064 if( home_dir==0 ) return 0;
22065 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
22072 #endif /* !_WIN32_WCE */
22075 int n = strlen30(home_dir) + 1;
22076 char *z = malloc( n );
22077 if( z ) memcpy(z, home_dir, n);
22085 ** Read input from the file given by sqliterc_override. Or if that
22086 ** parameter is NULL, take input from ~/.sqliterc
22088 ** Returns the number of errors.
22090 static void process_sqliterc(
22091 ShellState *p, /* Configuration data */
22092 const char *sqliterc_override /* Name of config file. NULL to use default */
22094 char *home_dir = NULL;
22095 const char *sqliterc = sqliterc_override;
22097 FILE *inSaved = p->in;
22098 int savedLineno = p->lineno;
22100 if (sqliterc == NULL) {
22101 home_dir = find_home_dir(0);
22103 raw_printf(stderr, "-- warning: cannot find home directory;"
22104 " cannot read ~/.sqliterc\n");
22107 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
22110 p->in = fopen(sqliterc,"rb");
22112 if( stdin_is_interactive ){
22113 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
22115 if( process_input(p) && bail_on_error ) exit(1);
22117 }else if( sqliterc_override!=0 ){
22118 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
22119 if( bail_on_error ) exit(1);
22122 p->lineno = savedLineno;
22123 sqlite3_free(zBuf);
22127 ** Show available command line options
22129 static const char zOptions[] =
22130 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
22131 " -A ARGS... run \".archive ARGS\" and exit\n"
22133 " -append append the database to the end of the file\n"
22134 " -ascii set output mode to 'ascii'\n"
22135 " -bail stop after hitting an error\n"
22136 " -batch force batch I/O\n"
22137 " -box set output mode to 'box'\n"
22138 " -column set output mode to 'column'\n"
22139 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
22140 " -csv set output mode to 'csv'\n"
22141 #if !defined(SQLITE_OMIT_DESERIALIZE)
22142 " -deserialize open the database using sqlite3_deserialize()\n"
22144 " -echo print commands before execution\n"
22145 " -init FILENAME read/process named file\n"
22146 " -[no]header turn headers on or off\n"
22147 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
22148 " -heap SIZE Size of heap for memsys3 or memsys5\n"
22150 " -help show this message\n"
22151 " -html set output mode to HTML\n"
22152 " -interactive force interactive I/O\n"
22153 " -json set output mode to 'json'\n"
22154 " -line set output mode to 'line'\n"
22155 " -list set output mode to 'list'\n"
22156 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
22157 " -markdown set output mode to 'markdown'\n"
22158 #if !defined(SQLITE_OMIT_DESERIALIZE)
22159 " -maxsize N maximum size for a --deserialize database\n"
22161 " -memtrace trace all memory allocations and deallocations\n"
22162 " -mmap N default mmap size set to N\n"
22163 #ifdef SQLITE_ENABLE_MULTIPLEX
22164 " -multiplex enable the multiplexor VFS\n"
22166 " -newline SEP set output row separator. Default: '\\n'\n"
22167 " -nofollow refuse to open symbolic links to database files\n"
22168 " -nonce STRING set the safe-mode escape nonce\n"
22169 " -nullvalue TEXT set text string for NULL values. Default ''\n"
22170 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
22171 " -quote set output mode to 'quote'\n"
22172 " -readonly open the database read-only\n"
22173 " -safe enable safe-mode\n"
22174 " -separator SEP set output column separator. Default: '|'\n"
22175 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22176 " -sorterref SIZE sorter references threshold size\n"
22178 " -stats print memory stats before each finalize\n"
22179 " -table set output mode to 'table'\n"
22180 " -tabs set output mode to 'tabs'\n"
22181 " -version show SQLite version\n"
22182 " -vfs NAME use NAME as the default VFS\n"
22183 #ifdef SQLITE_ENABLE_VFSTRACE
22184 " -vfstrace enable tracing of all VFS calls\n"
22186 #ifdef SQLITE_HAVE_ZLIB
22187 " -zip open the file as a ZIP Archive\n"
22190 static void usage(int showDetail){
22191 utf8_printf(stderr,
22192 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
22193 "FILENAME is the name of an SQLite database. A new database is created\n"
22194 "if the file does not previously exist.\n", Argv0);
22196 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
22198 raw_printf(stderr, "Use the -help option for additional information\n");
22204 ** Internal check: Verify that the SQLite is uninitialized. Print a
22205 ** error message if it is initialized.
22207 static void verify_uninitialized(void){
22208 if( sqlite3_config(-1)==SQLITE_MISUSE ){
22209 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
22210 " initialization.\n");
22215 ** Initialize the state information in data
22217 static void main_init(ShellState *data) {
22218 memset(data, 0, sizeof(*data));
22219 data->normalMode = data->cMode = data->mode = MODE_List;
22220 data->autoExplain = 1;
22221 data->pAuxDb = &data->aAuxDb[0];
22222 memcpy(data->colSeparator,SEP_Column, 2);
22223 memcpy(data->rowSeparator,SEP_Row, 2);
22224 data->showHeader = 0;
22225 data->shellFlgs = SHFLG_Lookaside;
22226 verify_uninitialized();
22227 sqlite3_config(SQLITE_CONFIG_URI, 1);
22228 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
22229 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
22230 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
22231 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
22235 ** Output text to the console in a font that attracts extra attention.
22238 static void printBold(const char *zText){
22239 #if !SQLITE_OS_WINRT
22240 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
22241 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
22242 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
22243 SetConsoleTextAttribute(out,
22244 FOREGROUND_RED|FOREGROUND_INTENSITY
22247 printf("%s", zText);
22248 #if !SQLITE_OS_WINRT
22249 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
22253 static void printBold(const char *zText){
22254 printf("\033[1m%s\033[0m", zText);
22259 ** Get the argument to an --option. Throw an error and die if no argument
22262 static char *cmdline_option_value(int argc, char **argv, int i){
22264 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
22265 argv[0], argv[argc-1]);
22271 #ifndef SQLITE_SHELL_IS_UTF8
22272 # if (defined(_WIN32) || defined(WIN32)) \
22273 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
22274 # define SQLITE_SHELL_IS_UTF8 (0)
22276 # define SQLITE_SHELL_IS_UTF8 (1)
22280 #if SQLITE_SHELL_IS_UTF8
22281 int SQLITE_CDECL main(int argc, char **argv){
22283 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
22288 const char *zInitFile = 0;
22291 int warnInmemoryDb = 0;
22295 const char *zVfs = 0; /* Value of -vfs command-line option */
22296 #if !SQLITE_SHELL_IS_UTF8
22297 char **argvToFree = 0;
22298 int argcToFree = 0;
22301 setBinaryMode(stdin, 0);
22302 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
22303 stdin_is_interactive = isatty(0);
22304 stdout_is_console = isatty(1);
22306 #ifdef SQLITE_DEBUG
22307 registerOomSimulator();
22310 #if !defined(_WIN32_WCE)
22311 if( getenv("SQLITE_DEBUG_BREAK") ){
22312 if( isatty(0) && isatty(2) ){
22314 "attach debugger to process %d and press any key to continue.\n",
22318 #if defined(_WIN32) || defined(WIN32)
22319 #if SQLITE_OS_WINRT
22324 #elif defined(SIGTRAP)
22331 #if USE_SYSTEM_SQLITE+0!=1
22332 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
22333 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
22334 sqlite3_sourceid(), SQLITE_SOURCE_ID);
22340 /* On Windows, we must translate command-line arguments into UTF-8.
22341 ** The SQLite memory allocator subsystem has to be enabled in order to
22342 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
22343 ** subsequent sqlite3_config() calls will work. So copy all results into
22344 ** memory that does not come from the SQLite memory allocator.
22346 #if !SQLITE_SHELL_IS_UTF8
22347 sqlite3_initialize();
22348 argvToFree = malloc(sizeof(argv[0])*argc*2);
22350 argv = argvToFree + argc;
22351 if( argv==0 ) shell_out_of_memory();
22352 for(i=0; i<argc; i++){
22353 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
22355 if( z==0 ) shell_out_of_memory();
22356 n = (int)strlen(z);
22357 argv[i] = malloc( n+1 );
22358 if( argv[i]==0 ) shell_out_of_memory();
22359 memcpy(argv[i], z, n+1);
22360 argvToFree[i] = argv[i];
22363 sqlite3_shutdown();
22366 assert( argc>=1 && argv && argv[0] );
22369 /* Make sure we have a valid signal handler early, before anything
22373 signal(SIGINT, interrupt_handler);
22374 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
22375 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
22378 #ifdef SQLITE_SHELL_DBNAME_PROC
22380 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
22381 ** of a C-function that will provide the name of the database file. Use
22382 ** this compile-time option to embed this shell program in larger
22383 ** applications. */
22384 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
22385 SQLITE_SHELL_DBNAME_PROC(&data.pAuxDb->zDbFilename);
22386 warnInmemoryDb = 0;
22390 /* Do an initial pass through the command-line argument to locate
22391 ** the name of the database file, the name of the initialization file,
22392 ** the size of the alternative malloc heap,
22393 ** and the first command to execute.
22395 verify_uninitialized();
22396 for(i=1; i<argc; i++){
22400 if( data.aAuxDb->zDbFilename==0 ){
22401 data.aAuxDb->zDbFilename = z;
22403 /* Excesss arguments are interpreted as SQL (or dot-commands) and
22404 ** mean that nothing is read from stdin */
22407 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
22408 if( azCmd==0 ) shell_out_of_memory();
22412 if( z[1]=='-' ) z++;
22413 if( strcmp(z,"-separator")==0
22414 || strcmp(z,"-nullvalue")==0
22415 || strcmp(z,"-newline")==0
22416 || strcmp(z,"-cmd")==0
22418 (void)cmdline_option_value(argc, argv, ++i);
22419 }else if( strcmp(z,"-init")==0 ){
22420 zInitFile = cmdline_option_value(argc, argv, ++i);
22421 }else if( strcmp(z,"-batch")==0 ){
22422 /* Need to check for batch mode here to so we can avoid printing
22423 ** informational messages (like from process_sqliterc) before
22424 ** we do the actual processing of arguments later in a second pass.
22426 stdin_is_interactive = 0;
22427 }else if( strcmp(z,"-heap")==0 ){
22428 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
22430 sqlite3_int64 szHeap;
22432 zSize = cmdline_option_value(argc, argv, ++i);
22433 szHeap = integerValue(zSize);
22434 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
22435 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
22437 (void)cmdline_option_value(argc, argv, ++i);
22439 }else if( strcmp(z,"-pagecache")==0 ){
22440 sqlite3_int64 n, sz;
22441 sz = integerValue(cmdline_option_value(argc,argv,++i));
22442 if( sz>70000 ) sz = 70000;
22444 n = integerValue(cmdline_option_value(argc,argv,++i));
22445 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
22446 n = 0xffffffffffffLL/sz;
22448 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
22449 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
22450 data.shellFlgs |= SHFLG_Pagecache;
22451 }else if( strcmp(z,"-lookaside")==0 ){
22453 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
22455 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22457 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
22458 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
22459 }else if( strcmp(z,"-threadsafe")==0 ){
22461 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22463 case 0: sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); break;
22464 case 2: sqlite3_config(SQLITE_CONFIG_MULTITHREAD); break;
22465 default: sqlite3_config(SQLITE_CONFIG_SERIALIZED); break;
22467 #ifdef SQLITE_ENABLE_VFSTRACE
22468 }else if( strcmp(z,"-vfstrace")==0 ){
22469 extern int vfstrace_register(
22470 const char *zTraceName,
22471 const char *zOldVfsName,
22472 int (*xOut)(const char*,void*),
22476 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
22478 #ifdef SQLITE_ENABLE_MULTIPLEX
22479 }else if( strcmp(z,"-multiplex")==0 ){
22480 extern int sqlite3_multiple_initialize(const char*,int);
22481 sqlite3_multiplex_initialize(0, 1);
22483 }else if( strcmp(z,"-mmap")==0 ){
22484 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22485 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
22486 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22487 }else if( strcmp(z,"-sorterref")==0 ){
22488 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22489 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
22491 }else if( strcmp(z,"-vfs")==0 ){
22492 zVfs = cmdline_option_value(argc, argv, ++i);
22493 #ifdef SQLITE_HAVE_ZLIB
22494 }else if( strcmp(z,"-zip")==0 ){
22495 data.openMode = SHELL_OPEN_ZIPFILE;
22497 }else if( strcmp(z,"-append")==0 ){
22498 data.openMode = SHELL_OPEN_APPENDVFS;
22499 #ifndef SQLITE_OMIT_DESERIALIZE
22500 }else if( strcmp(z,"-deserialize")==0 ){
22501 data.openMode = SHELL_OPEN_DESERIALIZE;
22502 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22503 data.szMax = integerValue(argv[++i]);
22505 }else if( strcmp(z,"-readonly")==0 ){
22506 data.openMode = SHELL_OPEN_READONLY;
22507 }else if( strcmp(z,"-nofollow")==0 ){
22508 data.openFlags = SQLITE_OPEN_NOFOLLOW;
22509 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22510 }else if( strncmp(z, "-A",2)==0 ){
22511 /* All remaining command-line arguments are passed to the ".archive"
22512 ** command, so ignore them */
22515 }else if( strcmp(z, "-memtrace")==0 ){
22516 sqlite3MemTraceActivate(stderr);
22517 }else if( strcmp(z,"-bail")==0 ){
22519 }else if( strcmp(z,"-nonce")==0 ){
22521 data.zNonce = strdup(argv[++i]);
22522 }else if( strcmp(z,"-safe")==0 ){
22523 /* no-op - catch this on the second pass */
22526 verify_uninitialized();
22529 #ifdef SQLITE_SHELL_INIT_PROC
22531 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
22532 ** of a C-function that will perform initialization actions on SQLite that
22533 ** occur just before or after sqlite3_initialize(). Use this compile-time
22534 ** option to embed this shell program in larger applications. */
22535 extern void SQLITE_SHELL_INIT_PROC(void);
22536 SQLITE_SHELL_INIT_PROC();
22539 /* All the sqlite3_config() calls have now been made. So it is safe
22540 ** to call sqlite3_initialize() and process any command line -vfs option. */
22541 sqlite3_initialize();
22545 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
22547 sqlite3_vfs_register(pVfs, 1);
22549 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
22554 if( data.pAuxDb->zDbFilename==0 ){
22555 #ifndef SQLITE_OMIT_MEMORYDB
22556 data.pAuxDb->zDbFilename = ":memory:";
22557 warnInmemoryDb = argc==1;
22559 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
22564 sqlite3_appendvfs_init(0,0,0);
22566 /* Go ahead and open the database file if it already exists. If the
22567 ** file does not exist, delay opening it. This prevents empty database
22568 ** files from being created if a user mistypes the database name argument
22569 ** to the sqlite command-line tool.
22571 if( access(data.pAuxDb->zDbFilename, 0)==0 ){
22575 /* Process the initialization file if there is one. If no -init option
22576 ** is given on the command line, look for a file named ~/.sqliterc and
22577 ** try to process it.
22579 process_sqliterc(&data,zInitFile);
22581 /* Make a second pass through the command-line argument and set
22582 ** options. This second pass is delayed until after the initialization
22583 ** file is processed so that the command-line arguments will override
22584 ** settings in the initialization file.
22586 for(i=1; i<argc; i++){
22588 if( z[0]!='-' ) continue;
22589 if( z[1]=='-' ){ z++; }
22590 if( strcmp(z,"-init")==0 ){
22592 }else if( strcmp(z,"-html")==0 ){
22593 data.mode = MODE_Html;
22594 }else if( strcmp(z,"-list")==0 ){
22595 data.mode = MODE_List;
22596 }else if( strcmp(z,"-quote")==0 ){
22597 data.mode = MODE_Quote;
22598 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
22599 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22600 }else if( strcmp(z,"-line")==0 ){
22601 data.mode = MODE_Line;
22602 }else if( strcmp(z,"-column")==0 ){
22603 data.mode = MODE_Column;
22604 }else if( strcmp(z,"-json")==0 ){
22605 data.mode = MODE_Json;
22606 }else if( strcmp(z,"-markdown")==0 ){
22607 data.mode = MODE_Markdown;
22608 }else if( strcmp(z,"-table")==0 ){
22609 data.mode = MODE_Table;
22610 }else if( strcmp(z,"-box")==0 ){
22611 data.mode = MODE_Box;
22612 }else if( strcmp(z,"-csv")==0 ){
22613 data.mode = MODE_Csv;
22614 memcpy(data.colSeparator,",",2);
22615 #ifdef SQLITE_HAVE_ZLIB
22616 }else if( strcmp(z,"-zip")==0 ){
22617 data.openMode = SHELL_OPEN_ZIPFILE;
22619 }else if( strcmp(z,"-append")==0 ){
22620 data.openMode = SHELL_OPEN_APPENDVFS;
22621 #ifndef SQLITE_OMIT_DESERIALIZE
22622 }else if( strcmp(z,"-deserialize")==0 ){
22623 data.openMode = SHELL_OPEN_DESERIALIZE;
22624 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22625 data.szMax = integerValue(argv[++i]);
22627 }else if( strcmp(z,"-readonly")==0 ){
22628 data.openMode = SHELL_OPEN_READONLY;
22629 }else if( strcmp(z,"-nofollow")==0 ){
22630 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
22631 }else if( strcmp(z,"-ascii")==0 ){
22632 data.mode = MODE_Ascii;
22633 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
22634 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
22635 }else if( strcmp(z,"-tabs")==0 ){
22636 data.mode = MODE_List;
22637 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
22638 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22639 }else if( strcmp(z,"-separator")==0 ){
22640 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
22641 "%s",cmdline_option_value(argc,argv,++i));
22642 }else if( strcmp(z,"-newline")==0 ){
22643 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
22644 "%s",cmdline_option_value(argc,argv,++i));
22645 }else if( strcmp(z,"-nullvalue")==0 ){
22646 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
22647 "%s",cmdline_option_value(argc,argv,++i));
22648 }else if( strcmp(z,"-header")==0 ){
22649 data.showHeader = 1;
22650 ShellSetFlag(&data, SHFLG_HeaderSet);
22651 }else if( strcmp(z,"-noheader")==0 ){
22652 data.showHeader = 0;
22653 ShellSetFlag(&data, SHFLG_HeaderSet);
22654 }else if( strcmp(z,"-echo")==0 ){
22655 ShellSetFlag(&data, SHFLG_Echo);
22656 }else if( strcmp(z,"-eqp")==0 ){
22657 data.autoEQP = AUTOEQP_on;
22658 }else if( strcmp(z,"-eqpfull")==0 ){
22659 data.autoEQP = AUTOEQP_full;
22660 }else if( strcmp(z,"-stats")==0 ){
22662 }else if( strcmp(z,"-scanstats")==0 ){
22663 data.scanstatsOn = 1;
22664 }else if( strcmp(z,"-backslash")==0 ){
22665 /* Undocumented command-line option: -backslash
22666 ** Causes C-style backslash escapes to be evaluated in SQL statements
22667 ** prior to sending the SQL into SQLite. Useful for injecting
22668 ** crazy bytes in the middle of SQL statements for testing and debugging.
22670 ShellSetFlag(&data, SHFLG_Backslash);
22671 }else if( strcmp(z,"-bail")==0 ){
22672 /* No-op. The bail_on_error flag should already be set. */
22673 }else if( strcmp(z,"-version")==0 ){
22674 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
22676 }else if( strcmp(z,"-interactive")==0 ){
22677 stdin_is_interactive = 1;
22678 }else if( strcmp(z,"-batch")==0 ){
22679 stdin_is_interactive = 0;
22680 }else if( strcmp(z,"-heap")==0 ){
22682 }else if( strcmp(z,"-pagecache")==0 ){
22684 }else if( strcmp(z,"-lookaside")==0 ){
22686 }else if( strcmp(z,"-threadsafe")==0 ){
22688 }else if( strcmp(z,"-nonce")==0 ){
22690 }else if( strcmp(z,"-mmap")==0 ){
22692 }else if( strcmp(z,"-memtrace")==0 ){
22694 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22695 }else if( strcmp(z,"-sorterref")==0 ){
22698 }else if( strcmp(z,"-vfs")==0 ){
22700 #ifdef SQLITE_ENABLE_VFSTRACE
22701 }else if( strcmp(z,"-vfstrace")==0 ){
22704 #ifdef SQLITE_ENABLE_MULTIPLEX
22705 }else if( strcmp(z,"-multiplex")==0 ){
22708 }else if( strcmp(z,"-help")==0 ){
22710 }else if( strcmp(z,"-cmd")==0 ){
22711 /* Run commands that follow -cmd first and separately from commands
22712 ** that simply appear on the command-line. This seems goofy. It would
22713 ** be better if all commands ran in the order that they appear. But
22714 ** we retain the goofy behavior for historical compatibility. */
22715 if( i==argc-1 ) break;
22716 z = cmdline_option_value(argc,argv,++i);
22718 rc = do_meta_command(z, &data);
22719 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
22722 rc = shell_exec(&data, z, &zErrMsg);
22724 utf8_printf(stderr,"Error: %s\n", zErrMsg);
22725 if( bail_on_error ) return rc!=0 ? rc : 1;
22727 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
22728 if( bail_on_error ) return rc;
22731 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22732 }else if( strncmp(z, "-A", 2)==0 ){
22734 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
22735 " with \"%s\"\n", z);
22738 open_db(&data, OPEN_DB_ZIPFILE);
22741 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
22743 arDotCommand(&data, 1, argv+i, argc-i);
22748 }else if( strcmp(z,"-safe")==0 ){
22749 data.bSafeMode = data.bSafeModePersist = 1;
22751 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
22752 raw_printf(stderr,"Use -help for a list of options.\n");
22755 data.cMode = data.mode;
22759 /* Run all arguments that do not begin with '-' as if they were separate
22760 ** command-line inputs, except for the argToSkip argument which contains
22761 ** the database filename.
22763 for(i=0; i<nCmd; i++){
22764 if( azCmd[i][0]=='.' ){
22765 rc = do_meta_command(azCmd[i], &data);
22768 return rc==2 ? 0 : rc;
22772 rc = shell_exec(&data, azCmd[i], &zErrMsg);
22773 if( zErrMsg || rc ){
22775 utf8_printf(stderr,"Error: %s\n", zErrMsg);
22777 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
22779 sqlite3_free(zErrMsg);
22781 return rc!=0 ? rc : 1;
22786 /* Run commands received from standard input
22788 if( stdin_is_interactive ){
22793 "SQLite version %s %.19s\n" /*extra-version-info*/
22794 "Enter \".help\" for usage hints.\n",
22795 sqlite3_libversion(), sqlite3_sourceid()
22797 if( warnInmemoryDb ){
22798 printf("Connected to a ");
22799 printBold("transient in-memory database");
22800 printf(".\nUse \".open FILENAME\" to reopen on a "
22801 "persistent database.\n");
22803 zHistory = getenv("SQLITE_HISTORY");
22805 zHistory = strdup(zHistory);
22806 }else if( (zHome = find_home_dir(0))!=0 ){
22807 nHistory = strlen30(zHome) + 20;
22808 if( (zHistory = malloc(nHistory))!=0 ){
22809 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
22812 if( zHistory ){ shell_read_history(zHistory); }
22813 #if HAVE_READLINE || HAVE_EDITLINE
22814 rl_attempted_completion_function = readline_completion;
22815 #elif HAVE_LINENOISE
22816 linenoiseSetCompletionCallback(linenoise_completion);
22819 rc = process_input(&data);
22821 shell_stifle_history(2000);
22822 shell_write_history(zHistory);
22827 rc = process_input(&data);
22831 set_table_name(&data, 0);
22833 session_close_all(&data, -1);
22836 for(i=0; i<ArraySize(data.aAuxDb); i++){
22837 sqlite3_free(data.aAuxDb[i].zFreeOnClose);
22838 if( data.aAuxDb[i].db ){
22839 session_close_all(&data, i);
22840 close_db(data.aAuxDb[i].db);
22844 output_reset(&data);
22845 data.doXdgOpen = 0;
22846 clearTempFile(&data);
22847 #if !SQLITE_SHELL_IS_UTF8
22848 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
22851 free(data.colWidth);
22853 /* Clear the global data structure so that valgrind will detect memory
22855 memset(&data, 0, sizeof(data));