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 ** Determine if we are dealing with WinRT, which provides only a subset of
40 ** the full Win32 API.
42 #if !defined(SQLITE_OS_WINRT)
43 # define SQLITE_OS_WINRT 0
47 ** Warning pragmas copied from msvc.h in the core.
50 #pragma warning(disable : 4054)
51 #pragma warning(disable : 4055)
52 #pragma warning(disable : 4100)
53 #pragma warning(disable : 4127)
54 #pragma warning(disable : 4130)
55 #pragma warning(disable : 4152)
56 #pragma warning(disable : 4189)
57 #pragma warning(disable : 4206)
58 #pragma warning(disable : 4210)
59 #pragma warning(disable : 4232)
60 #pragma warning(disable : 4244)
61 #pragma warning(disable : 4305)
62 #pragma warning(disable : 4306)
63 #pragma warning(disable : 4702)
64 #pragma warning(disable : 4706)
65 #endif /* defined(_MSC_VER) */
68 ** No support for loadable extensions in VxWorks.
70 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
71 # define SQLITE_OMIT_LOAD_EXTENSION 1
75 ** Enable large-file support for fopen() and friends on unix.
77 #ifndef SQLITE_DISABLE_LFS
78 # define _LARGE_FILE 1
79 # ifndef _FILE_OFFSET_BITS
80 # define _FILE_OFFSET_BITS 64
82 # define _LARGEFILE_SOURCE 1
90 typedef sqlite3_int64 i64;
91 typedef sqlite3_uint64 u64;
92 typedef unsigned char u8;
93 #if SQLITE_USER_AUTHENTICATION
94 # include "sqlite3userauth.h"
99 #if !defined(_WIN32) && !defined(WIN32)
101 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
105 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
108 # define GETPID getpid
109 # if defined(__MINGW32__)
110 # define DIRENT dirent
112 # define S_ISLNK(mode) (0)
116 # define GETPID (int)GetCurrentProcessId
118 #include <sys/types.h>
119 #include <sys/stat.h>
122 # include <readline/readline.h>
123 # include <readline/history.h>
127 # include <editline/readline.h>
130 #if HAVE_EDITLINE || HAVE_READLINE
132 # define shell_add_history(X) add_history(X)
133 # define shell_read_history(X) read_history(X)
134 # define shell_write_history(X) write_history(X)
135 # define shell_stifle_history(X) stifle_history(X)
136 # define shell_readline(X) readline(X)
140 # include "linenoise.h"
141 # define shell_add_history(X) linenoiseHistoryAdd(X)
142 # define shell_read_history(X) linenoiseHistoryLoad(X)
143 # define shell_write_history(X) linenoiseHistorySave(X)
144 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
145 # define shell_readline(X) linenoise(X)
149 # define shell_read_history(X)
150 # define shell_write_history(X)
151 # define shell_stifle_history(X)
153 # define SHELL_USE_LOCAL_GETLINE 1
157 #if defined(_WIN32) || defined(WIN32)
159 # define SQLITE_OMIT_POPEN 1
163 # define isatty(h) _isatty(h)
165 # define access(f,m) _access((f),(m))
168 # define unlink _unlink
171 # define strdup _strdup
174 # define popen _popen
176 # define pclose _pclose
179 /* Make sure isatty() has a prototype. */
180 extern int isatty(int);
182 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
183 /* popen and pclose are not C89 functions and so are
184 ** sometimes omitted from the <stdio.h> header */
185 extern FILE *popen(const char*,const char*);
186 extern int pclose(FILE*);
188 # define SQLITE_OMIT_POPEN 1
192 #if defined(_WIN32_WCE)
193 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
194 * thus we always assume that we have a console. That can be
195 * overridden with the -batch command line option.
200 /* ctype macros that work with signed characters */
201 #define IsSpace(X) isspace((unsigned char)X)
202 #define IsDigit(X) isdigit((unsigned char)X)
203 #define ToLower(X) (char)tolower((unsigned char)X)
205 #if defined(_WIN32) || defined(WIN32)
211 /* string conversion routines only needed on Win32 */
212 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
213 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
214 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
215 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
218 /* On Windows, we normally run with output mode of TEXT so that \n characters
219 ** are automatically translated into \r\n. However, this behavior needs
220 ** to be disabled in some cases (ex: when generating CSV output and when
221 ** rendering quoted strings that contain \n characters). The following
222 ** routines take care of that.
224 #if (defined(_WIN32) || defined(WIN32)) && !SQLITE_OS_WINRT
225 static void setBinaryMode(FILE *file, int isOutput){
226 if( isOutput ) fflush(file);
227 _setmode(_fileno(file), _O_BINARY);
229 static void setTextMode(FILE *file, int isOutput){
230 if( isOutput ) fflush(file);
231 _setmode(_fileno(file), _O_TEXT);
234 # define setBinaryMode(X,Y)
235 # define setTextMode(X,Y)
239 /* True if the timer is enabled */
240 static int enableTimer = 0;
242 /* Return the current wall-clock time */
243 static sqlite3_int64 timeOfDay(void){
244 static sqlite3_vfs *clockVfs = 0;
246 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
247 if( clockVfs==0 ) return 0; /* Never actually happens */
248 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
249 clockVfs->xCurrentTimeInt64(clockVfs, &t);
252 clockVfs->xCurrentTime(clockVfs, &r);
253 t = (sqlite3_int64)(r*86400000.0);
258 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
259 #include <sys/time.h>
260 #include <sys/resource.h>
262 /* VxWorks does not support getrusage() as far as we can determine */
263 #if defined(_WRS_KERNEL) || defined(__RTP__)
265 struct timeval ru_utime; /* user CPU time used */
266 struct timeval ru_stime; /* system CPU time used */
268 #define getrusage(A,B) memset(B,0,sizeof(*B))
271 /* Saved resource information for the beginning of an operation */
272 static struct rusage sBegin; /* CPU time at start */
273 static sqlite3_int64 iBegin; /* Wall-clock time at start */
276 ** Begin timing an operation
278 static void beginTimer(void){
280 getrusage(RUSAGE_SELF, &sBegin);
281 iBegin = timeOfDay();
285 /* Return the difference of two time_structs in seconds */
286 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
287 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
288 (double)(pEnd->tv_sec - pStart->tv_sec);
292 ** Print the timing results.
294 static void endTimer(void){
296 sqlite3_int64 iEnd = timeOfDay();
298 getrusage(RUSAGE_SELF, &sEnd);
299 printf("Run Time: real %.3f user %f sys %f\n",
300 (iEnd - iBegin)*0.001,
301 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
302 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
306 #define BEGIN_TIMER beginTimer()
307 #define END_TIMER endTimer()
310 #elif (defined(_WIN32) || defined(WIN32))
312 /* Saved resource information for the beginning of an operation */
313 static HANDLE hProcess;
314 static FILETIME ftKernelBegin;
315 static FILETIME ftUserBegin;
316 static sqlite3_int64 ftWallBegin;
317 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
318 LPFILETIME, LPFILETIME);
319 static GETPROCTIMES getProcessTimesAddr = NULL;
322 ** Check to see if we have timer support. Return 1 if necessary
323 ** support found (or found previously).
325 static int hasTimer(void){
326 if( getProcessTimesAddr ){
330 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
331 ** versions. See if the version we are running on has it, and if it
332 ** does, save off a pointer to it and the current process handle.
334 hProcess = GetCurrentProcess();
336 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
337 if( NULL != hinstLib ){
338 getProcessTimesAddr =
339 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
340 if( NULL != getProcessTimesAddr ){
343 FreeLibrary(hinstLib);
352 ** Begin timing an operation
354 static void beginTimer(void){
355 if( enableTimer && getProcessTimesAddr ){
356 FILETIME ftCreation, ftExit;
357 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
358 &ftKernelBegin,&ftUserBegin);
359 ftWallBegin = timeOfDay();
363 /* Return the difference of two FILETIME structs in seconds */
364 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
365 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
366 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
367 return (double) ((i64End - i64Start) / 10000000.0);
371 ** Print the timing results.
373 static void endTimer(void){
374 if( enableTimer && getProcessTimesAddr){
375 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
376 sqlite3_int64 ftWallEnd = timeOfDay();
377 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
378 printf("Run Time: real %.3f user %f sys %f\n",
379 (ftWallEnd - ftWallBegin)*0.001,
380 timeDiff(&ftUserBegin, &ftUserEnd),
381 timeDiff(&ftKernelBegin, &ftKernelEnd));
385 #define BEGIN_TIMER beginTimer()
386 #define END_TIMER endTimer()
387 #define HAS_TIMER hasTimer()
396 ** Used to prevent warnings about unused parameters
398 #define UNUSED_PARAMETER(x) (void)(x)
401 ** Number of elements in an array
403 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
406 ** If the following flag is set, then command execution stops
407 ** at an error if we are not interactive.
409 static int bail_on_error = 0;
412 ** Threat stdin as an interactive input if the following variable
413 ** is true. Otherwise, assume stdin is connected to a file or pipe.
415 static int stdin_is_interactive = 1;
418 ** On Windows systems we have to know if standard output is a console
419 ** in order to translate UTF-8 into MBCS. The following variable is
420 ** true if translation is required.
422 static int stdout_is_console = 1;
425 ** The following is the open SQLite database. We make a pointer
426 ** to this database a static variable so that it can be accessed
427 ** by the SIGINT handler to interrupt database processing.
429 static sqlite3 *globalDb = 0;
432 ** True if an interrupt (Control-C) has been received.
434 static volatile int seenInterrupt = 0;
438 ** Out-of-memory simulator variables
440 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
441 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
442 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
443 #endif /* SQLITE_DEBUG */
446 ** This is the name of our program. It is set in main(), used
447 ** in a number of other places, mostly for error messages.
452 ** Prompt strings. Initialized in main. Settable with
453 ** .prompt main continue
455 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
456 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
459 ** Render output like fprintf(). Except, if the output is going to the
460 ** console and if this is running on a Windows machine, translate the
461 ** output from UTF-8 into MBCS.
463 #if defined(_WIN32) || defined(WIN32)
464 void utf8_printf(FILE *out, const char *zFormat, ...){
466 va_start(ap, zFormat);
467 if( stdout_is_console && (out==stdout || out==stderr) ){
468 char *z1 = sqlite3_vmprintf(zFormat, ap);
469 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
474 vfprintf(out, zFormat, ap);
478 #elif !defined(utf8_printf)
479 # define utf8_printf fprintf
483 ** Render output like fprintf(). This should not be used on anything that
484 ** includes string formatting (e.g. "%s").
486 #if !defined(raw_printf)
487 # define raw_printf fprintf
490 /* Indicate out-of-memory and exit. */
491 static void shell_out_of_memory(void){
492 raw_printf(stderr,"Error: out of memory\n");
497 /* This routine is called when a simulated OOM occurs. It is broken
498 ** out as a separate routine to make it easy to set a breakpoint on
501 void shellOomFault(void){
508 #endif /* SQLITE_DEBUG */
511 /* This routine is a replacement malloc() that is used to simulate
512 ** Out-Of-Memory (OOM) errors for testing purposes.
514 static void *oomMalloc(int nByte){
523 return defaultMalloc(nByte);
525 #endif /* SQLITE_DEBUG */
528 /* Register the OOM simulator. This must occur before any memory
530 static void registerOomSimulator(void){
531 sqlite3_mem_methods mem;
532 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
533 defaultMalloc = mem.xMalloc;
534 mem.xMalloc = oomMalloc;
535 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
540 ** Write I/O traces to the following stream.
542 #ifdef SQLITE_ENABLE_IOTRACE
543 static FILE *iotrace = 0;
547 ** This routine works like printf in that its first argument is a
548 ** format string and subsequent arguments are values to be substituted
549 ** in place of % fields. The result of formatting this string
550 ** is written to iotrace.
552 #ifdef SQLITE_ENABLE_IOTRACE
553 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
556 if( iotrace==0 ) return;
557 va_start(ap, zFormat);
558 z = sqlite3_vmprintf(zFormat, ap);
560 utf8_printf(iotrace, "%s", z);
566 ** Output string zUtf to stream pOut as w characters. If w is negative,
567 ** then right-justify the text. W is the width in UTF-8 characters, not
568 ** in bytes. This is different from the %*.*s specification in printf
569 ** since with %*.*s the width is measured in bytes, not characters.
571 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
574 int aw = w<0 ? -w : w;
575 for(i=n=0; zUtf[i]; i++){
576 if( (zUtf[i]&0xc0)!=0x80 ){
579 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
585 utf8_printf(pOut, "%.*s", i, zUtf);
587 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
589 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
595 ** Determines if a string is a number of not.
597 static int isNumber(const char *z, int *realnum){
598 if( *z=='-' || *z=='+' ) z++;
603 if( realnum ) *realnum = 0;
604 while( IsDigit(*z) ){ z++; }
607 if( !IsDigit(*z) ) return 0;
608 while( IsDigit(*z) ){ z++; }
609 if( realnum ) *realnum = 1;
611 if( *z=='e' || *z=='E' ){
613 if( *z=='+' || *z=='-' ) z++;
614 if( !IsDigit(*z) ) return 0;
615 while( IsDigit(*z) ){ z++; }
616 if( realnum ) *realnum = 1;
622 ** Compute a string length that is limited to what can be stored in
623 ** lower 30 bits of a 32-bit signed integer.
625 static int strlen30(const char *z){
627 while( *z2 ){ z2++; }
628 return 0x3fffffff & (int)(z2 - z);
632 ** Return the length of a string in characters. Multibyte UTF8 characters
633 ** count as a single character.
635 static int strlenChar(const char *z){
638 if( (0xc0&*(z++))!=0x80 ) n++;
644 ** Return true if zFile does not exist or if it is not an ordinary file.
647 # define notNormalFile(X) 0
649 static int notNormalFile(const char *zFile){
652 memset(&x, 0, sizeof(x));
653 rc = stat(zFile, &x);
654 return rc || !S_ISREG(x.st_mode);
659 ** This routine reads a line of text from FILE in, stores
660 ** the text in memory obtained from malloc() and returns a pointer
661 ** to the text. NULL is returned at end of file, or if malloc()
664 ** If zLine is not NULL then it is a malloced buffer returned from
665 ** a previous call to this routine that may be reused.
667 static char *local_getline(char *zLine, FILE *in){
668 int nLine = zLine==0 ? 0 : 100;
673 nLine = nLine*2 + 100;
674 zLine = realloc(zLine, nLine);
675 if( zLine==0 ) shell_out_of_memory();
677 if( fgets(&zLine[n], nLine - n, in)==0 ){
685 while( zLine[n] ) n++;
686 if( n>0 && zLine[n-1]=='\n' ){
688 if( n>0 && zLine[n-1]=='\r' ) n--;
693 #if defined(_WIN32) || defined(WIN32)
694 /* For interactive input on Windows systems, translate the
695 ** multi-byte characterset characters into UTF-8. */
696 if( stdin_is_interactive && in==stdin ){
697 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
699 int nTrans = strlen30(zTrans)+1;
701 zLine = realloc(zLine, nTrans);
702 if( zLine==0 ) shell_out_of_memory();
704 memcpy(zLine, zTrans, nTrans);
705 sqlite3_free(zTrans);
708 #endif /* defined(_WIN32) || defined(WIN32) */
713 ** Retrieve a single line of input text.
715 ** If in==0 then read from standard input and prompt before each line.
716 ** If isContinuation is true, then a continuation prompt is appropriate.
717 ** If isContinuation is zero, then the main prompt should be used.
719 ** If zPrior is not NULL then it is a buffer from a prior call to this
720 ** routine that can be reused.
722 ** The result is stored in space obtained from malloc() and must either
723 ** be freed by the caller or else passed back into this routine via the
724 ** zPrior argument for reuse.
726 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
730 zResult = local_getline(zPrior, in);
732 zPrompt = isContinuation ? continuePrompt : mainPrompt;
733 #if SHELL_USE_LOCAL_GETLINE
734 printf("%s", zPrompt);
736 zResult = local_getline(zPrior, stdin);
739 zResult = shell_readline(zPrompt);
740 if( zResult && *zResult ) shell_add_history(zResult);
748 ** Return the value of a hexadecimal digit. Return -1 if the input
749 ** is not a hex digit.
751 static int hexDigitValue(char c){
752 if( c>='0' && c<='9' ) return c - '0';
753 if( c>='a' && c<='f' ) return c - 'a' + 10;
754 if( c>='A' && c<='F' ) return c - 'A' + 10;
759 ** Interpret zArg as an integer value, possibly with suffixes.
761 static sqlite3_int64 integerValue(const char *zArg){
763 static const struct { char *zSuffix; int iMult; } aMult[] = {
765 { "MiB", 1024*1024 },
766 { "GiB", 1024*1024*1024 },
769 { "GB", 1000000000 },
779 }else if( zArg[0]=='+' ){
782 if( zArg[0]=='0' && zArg[1]=='x' ){
785 while( (x = hexDigitValue(zArg[0]))>=0 ){
790 while( IsDigit(zArg[0]) ){
791 v = v*10 + zArg[0] - '0';
795 for(i=0; i<ArraySize(aMult); i++){
796 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
801 return isNeg? -v : v;
805 ** A variable length string to which one can append text.
807 typedef struct ShellText ShellText;
815 ** Initialize and destroy a ShellText object
817 static void initText(ShellText *p){
818 memset(p, 0, sizeof(*p));
820 static void freeText(ShellText *p){
825 /* zIn is either a pointer to a NULL-terminated string in memory obtained
826 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
827 ** added to zIn, and the result returned in memory obtained from malloc().
828 ** zIn, if it was not NULL, is freed.
830 ** If the third argument, quote, is not '\0', then it is used as a
831 ** quote character for zAppend.
833 static void appendText(ShellText *p, char const *zAppend, char quote){
836 int nAppend = strlen30(zAppend);
838 len = nAppend+p->n+1;
841 for(i=0; i<nAppend; i++){
842 if( zAppend[i]==quote ) len++;
846 if( p->n+len>=p->nAlloc ){
847 p->nAlloc = p->nAlloc*2 + len + 20;
848 p->z = realloc(p->z, p->nAlloc);
849 if( p->z==0 ) shell_out_of_memory();
853 char *zCsr = p->z+p->n;
855 for(i=0; i<nAppend; i++){
856 *zCsr++ = zAppend[i];
857 if( zAppend[i]==quote ) *zCsr++ = quote;
860 p->n = (int)(zCsr - p->z);
863 memcpy(p->z+p->n, zAppend, nAppend);
870 ** Attempt to determine if identifier zName needs to be quoted, either
871 ** because it contains non-alphanumeric characters, or because it is an
872 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
873 ** that quoting is required.
875 ** Return '"' if quoting is required. Return 0 if no quoting is required.
877 static char quoteChar(const char *zName){
879 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
880 for(i=0; zName[i]; i++){
881 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
883 return sqlite3_keyword_check(zName, i) ? '"' : 0;
887 ** Construct a fake object name and column list to describe the structure
888 ** of the view, virtual table, or table valued function zSchema.zName.
890 static char *shellFakeSchema(
891 sqlite3 *db, /* The database connection containing the vtab */
892 const char *zSchema, /* Schema of the database holding the vtab */
893 const char *zName /* The name of the virtual table */
895 sqlite3_stmt *pStmt = 0;
902 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
903 zSchema ? zSchema : "main", zName);
904 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
908 cQuote = quoteChar(zSchema);
909 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
910 appendText(&s, zSchema, cQuote);
911 appendText(&s, ".", 0);
913 cQuote = quoteChar(zName);
914 appendText(&s, zName, cQuote);
915 while( sqlite3_step(pStmt)==SQLITE_ROW ){
916 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
918 appendText(&s, zDiv, 0);
920 cQuote = quoteChar(zCol);
921 appendText(&s, zCol, cQuote);
923 appendText(&s, ")", 0);
924 sqlite3_finalize(pStmt);
933 ** SQL function: shell_module_schema(X)
935 ** Return a fake schema for the table-valued function or eponymous virtual
938 static void shellModuleSchema(
939 sqlite3_context *pCtx,
941 sqlite3_value **apVal
943 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
944 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
945 UNUSED_PARAMETER(nVal);
947 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
954 ** SQL function: shell_add_schema(S,X)
956 ** Add the schema name X to the CREATE statement in S and return the result.
959 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
964 ** CREATE UNIQUE INDEX
967 ** CREATE VIRTUAL TABLE
969 ** This UDF is used by the .schema command to insert the schema name of
970 ** attached databases into the middle of the sqlite_schema.sql field.
972 static void shellAddSchemaName(
973 sqlite3_context *pCtx,
975 sqlite3_value **apVal
977 static const char *aPrefix[] = {
986 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
987 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
988 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
989 sqlite3 *db = sqlite3_context_db_handle(pCtx);
990 UNUSED_PARAMETER(nVal);
991 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
992 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
993 int n = strlen30(aPrefix[i]);
994 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
998 char cQuote = quoteChar(zSchema);
999 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
1000 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1002 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1006 && aPrefix[i][0]=='V'
1007 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1010 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1012 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1017 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1023 sqlite3_result_value(pCtx, apVal[0]);
1027 ** The source code for several run-time loadable extensions is inserted
1028 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1029 ** code, we need to override some macros to make the included program code
1030 ** work here in the middle of this regular program.
1032 #define SQLITE_EXTENSION_INIT1
1033 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1035 #if defined(_WIN32) && defined(_MSC_VER)
1036 /************************* Begin test_windirent.h ******************/
1040 ** The author disclaims copyright to this source code. In place of
1041 ** a legal notice, here is a blessing:
1043 ** May you do good and not evil.
1044 ** May you find forgiveness for yourself and forgive others.
1045 ** May you share freely, never taking more than you give.
1047 *************************************************************************
1048 ** This file contains declarations for most of the opendir() family of
1049 ** POSIX functions on Win32 using the MSVCRT.
1052 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1053 #define SQLITE_WINDIRENT_H
1056 ** We need several data types from the Windows SDK header.
1059 #ifndef WIN32_LEAN_AND_MEAN
1060 #define WIN32_LEAN_AND_MEAN
1063 #include "windows.h"
1066 ** We need several support functions from the SQLite core.
1069 /* #include "sqlite3.h" */
1072 ** We need several things from the ANSI and MSVCRT headers.
1080 #include <sys/types.h>
1081 #include <sys/stat.h>
1084 ** We may need several defines that should have been in "sys/stat.h".
1088 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1092 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1096 #define S_ISLNK(mode) (0)
1100 ** We may need to provide the "mode_t" type.
1103 #ifndef MODE_T_DEFINED
1104 #define MODE_T_DEFINED
1105 typedef unsigned short mode_t;
1109 ** We may need to provide the "ino_t" type.
1112 #ifndef INO_T_DEFINED
1113 #define INO_T_DEFINED
1114 typedef unsigned short ino_t;
1118 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1122 # ifdef FILENAME_MAX
1123 # define NAME_MAX (FILENAME_MAX)
1125 # define NAME_MAX (260)
1130 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1133 #ifndef NULL_INTPTR_T
1134 # define NULL_INTPTR_T ((intptr_t)(0))
1137 #ifndef BAD_INTPTR_T
1138 # define BAD_INTPTR_T ((intptr_t)(-1))
1142 ** We need to provide the necessary structures and related types.
1145 #ifndef DIRENT_DEFINED
1146 #define DIRENT_DEFINED
1147 typedef struct DIRENT DIRENT;
1148 typedef DIRENT *LPDIRENT;
1150 ino_t d_ino; /* Sequence number, do not use. */
1151 unsigned d_attributes; /* Win32 file attributes. */
1152 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1158 typedef struct DIR DIR;
1161 intptr_t d_handle; /* Value returned by "_findfirst". */
1162 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1163 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1168 ** Provide a macro, for use by the implementation, to determine if a
1169 ** particular directory entry should be skipped over when searching for
1170 ** the next directory entry that should be returned by the readdir() or
1171 ** readdir_r() functions.
1175 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1179 ** Provide the function prototype for the POSIX compatiable getenv()
1180 ** function. This function is not thread-safe.
1183 extern const char *windirent_getenv(const char *name);
1186 ** Finally, we can provide the function prototypes for the opendir(),
1187 ** readdir(), readdir_r(), and closedir() POSIX functions.
1190 extern LPDIR opendir(const char *dirname);
1191 extern LPDIRENT readdir(LPDIR dirp);
1192 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1193 extern INT closedir(LPDIR dirp);
1195 #endif /* defined(WIN32) && defined(_MSC_VER) */
1197 /************************* End test_windirent.h ********************/
1198 /************************* Begin test_windirent.c ******************/
1202 ** The author disclaims copyright to this source code. In place of
1203 ** a legal notice, here is a blessing:
1205 ** May you do good and not evil.
1206 ** May you find forgiveness for yourself and forgive others.
1207 ** May you share freely, never taking more than you give.
1209 *************************************************************************
1210 ** This file contains code to implement most of the opendir() family of
1211 ** POSIX functions on Win32 using the MSVCRT.
1214 #if defined(_WIN32) && defined(_MSC_VER)
1215 /* #include "test_windirent.h" */
1218 ** Implementation of the POSIX getenv() function using the Win32 API.
1219 ** This function is not thread-safe.
1221 const char *windirent_getenv(
1224 static char value[32768]; /* Maximum length, per MSDN */
1225 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1226 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1228 memset(value, 0, sizeof(value));
1229 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1230 if( dwRet==0 || dwRet>dwSize ){
1232 ** The function call to GetEnvironmentVariableA() failed -OR-
1233 ** the buffer is not large enough. Either way, return NULL.
1238 ** The function call to GetEnvironmentVariableA() succeeded
1239 ** -AND- the buffer contains the entire value.
1246 ** Implementation of the POSIX opendir() function using the MSVCRT.
1251 struct _finddata_t data;
1252 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1253 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1255 if( dirp==NULL ) return NULL;
1256 memset(dirp, 0, sizeof(DIR));
1258 /* TODO: Remove this if Unix-style root paths are not used. */
1259 if( sqlite3_stricmp(dirname, "/")==0 ){
1260 dirname = windirent_getenv("SystemDrive");
1263 memset(&data, 0, sizeof(struct _finddata_t));
1264 _snprintf(data.name, namesize, "%s\\*", dirname);
1265 dirp->d_handle = _findfirst(data.name, &data);
1267 if( dirp->d_handle==BAD_INTPTR_T ){
1272 /* TODO: Remove this block to allow hidden and/or system files. */
1273 if( is_filtered(data) ){
1276 memset(&data, 0, sizeof(struct _finddata_t));
1277 if( _findnext(dirp->d_handle, &data)==-1 ){
1282 /* TODO: Remove this block to allow hidden and/or system files. */
1283 if( is_filtered(data) ) goto next;
1286 dirp->d_first.d_attributes = data.attrib;
1287 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1288 dirp->d_first.d_name[NAME_MAX] = '\0';
1294 ** Implementation of the POSIX readdir() function using the MSVCRT.
1299 struct _finddata_t data;
1301 if( dirp==NULL ) return NULL;
1303 if( dirp->d_first.d_ino==0 ){
1304 dirp->d_first.d_ino++;
1305 dirp->d_next.d_ino++;
1307 return &dirp->d_first;
1312 memset(&data, 0, sizeof(struct _finddata_t));
1313 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1315 /* TODO: Remove this block to allow hidden and/or system files. */
1316 if( is_filtered(data) ) goto next;
1318 dirp->d_next.d_ino++;
1319 dirp->d_next.d_attributes = data.attrib;
1320 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1321 dirp->d_next.d_name[NAME_MAX] = '\0';
1323 return &dirp->d_next;
1327 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1334 struct _finddata_t data;
1336 if( dirp==NULL ) return EBADF;
1338 if( dirp->d_first.d_ino==0 ){
1339 dirp->d_first.d_ino++;
1340 dirp->d_next.d_ino++;
1342 entry->d_ino = dirp->d_first.d_ino;
1343 entry->d_attributes = dirp->d_first.d_attributes;
1344 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1345 entry->d_name[NAME_MAX] = '\0';
1353 memset(&data, 0, sizeof(struct _finddata_t));
1354 if( _findnext(dirp->d_handle, &data)==-1 ){
1359 /* TODO: Remove this block to allow hidden and/or system files. */
1360 if( is_filtered(data) ) goto next;
1362 entry->d_ino = (ino_t)-1; /* not available */
1363 entry->d_attributes = data.attrib;
1364 strncpy(entry->d_name, data.name, NAME_MAX);
1365 entry->d_name[NAME_MAX] = '\0';
1372 ** Implementation of the POSIX closedir() function using the MSVCRT.
1379 if( dirp==NULL ) return EINVAL;
1381 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1382 result = _findclose(dirp->d_handle);
1389 #endif /* defined(WIN32) && defined(_MSC_VER) */
1391 /************************* End test_windirent.c ********************/
1392 #define dirent DIRENT
1394 /************************* Begin ../ext/misc/shathree.c ******************/
1398 ** The author disclaims copyright to this source code. In place of
1399 ** a legal notice, here is a blessing:
1401 ** May you do good and not evil.
1402 ** May you find forgiveness for yourself and forgive others.
1403 ** May you share freely, never taking more than you give.
1405 ******************************************************************************
1407 ** This SQLite extension implements functions that compute SHA3 hashes.
1408 ** Two SQL functions are implemented:
1411 ** sha3_query(Y,SIZE)
1413 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1416 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1417 ** and returns a hash of their results.
1419 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1420 ** is used. If SIZE is included it must be one of the integers 224, 256,
1421 ** 384, or 512, to determine SHA3 hash variant that is computed.
1423 /* #include "sqlite3ext.h" */
1424 SQLITE_EXTENSION_INIT1
1429 #ifndef SQLITE_AMALGAMATION
1430 /* typedef sqlite3_uint64 u64; */
1431 #endif /* SQLITE_AMALGAMATION */
1433 /******************************************************************************
1437 ** Macros to determine whether the machine is big or little endian,
1438 ** and whether or not that determination is run-time or compile-time.
1440 ** For best performance, an attempt is made to guess at the byte-order
1441 ** using C-preprocessor macros. If that is unsuccessful, or if
1442 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1445 #ifndef SHA3_BYTEORDER
1446 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1447 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1448 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1450 # define SHA3_BYTEORDER 1234
1451 # elif defined(sparc) || defined(__ppc__)
1452 # define SHA3_BYTEORDER 4321
1454 # define SHA3_BYTEORDER 0
1460 ** State structure for a SHA3 hash in progress
1462 typedef struct SHA3Context SHA3Context;
1463 struct SHA3Context {
1465 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1466 unsigned char x[1600]; /* ... or 1600 bytes */
1468 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1469 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1470 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1474 ** A single step of the Keccak mixing function for a 1600-bit state
1476 static void KeccakF1600Step(SHA3Context *p){
1478 u64 b0, b1, b2, b3, b4;
1479 u64 c0, c1, c2, c3, c4;
1480 u64 d0, d1, d2, d3, d4;
1481 static const u64 RC[] = {
1482 0x0000000000000001ULL, 0x0000000000008082ULL,
1483 0x800000000000808aULL, 0x8000000080008000ULL,
1484 0x000000000000808bULL, 0x0000000080000001ULL,
1485 0x8000000080008081ULL, 0x8000000000008009ULL,
1486 0x000000000000008aULL, 0x0000000000000088ULL,
1487 0x0000000080008009ULL, 0x000000008000000aULL,
1488 0x000000008000808bULL, 0x800000000000008bULL,
1489 0x8000000000008089ULL, 0x8000000000008003ULL,
1490 0x8000000000008002ULL, 0x8000000000000080ULL,
1491 0x000000000000800aULL, 0x800000008000000aULL,
1492 0x8000000080008081ULL, 0x8000000000008080ULL,
1493 0x0000000080000001ULL, 0x8000000080008008ULL
1495 # define a00 (p->u.s[0])
1496 # define a01 (p->u.s[1])
1497 # define a02 (p->u.s[2])
1498 # define a03 (p->u.s[3])
1499 # define a04 (p->u.s[4])
1500 # define a10 (p->u.s[5])
1501 # define a11 (p->u.s[6])
1502 # define a12 (p->u.s[7])
1503 # define a13 (p->u.s[8])
1504 # define a14 (p->u.s[9])
1505 # define a20 (p->u.s[10])
1506 # define a21 (p->u.s[11])
1507 # define a22 (p->u.s[12])
1508 # define a23 (p->u.s[13])
1509 # define a24 (p->u.s[14])
1510 # define a30 (p->u.s[15])
1511 # define a31 (p->u.s[16])
1512 # define a32 (p->u.s[17])
1513 # define a33 (p->u.s[18])
1514 # define a34 (p->u.s[19])
1515 # define a40 (p->u.s[20])
1516 # define a41 (p->u.s[21])
1517 # define a42 (p->u.s[22])
1518 # define a43 (p->u.s[23])
1519 # define a44 (p->u.s[24])
1520 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1522 for(i=0; i<24; i+=4){
1523 c0 = a00^a10^a20^a30^a40;
1524 c1 = a01^a11^a21^a31^a41;
1525 c2 = a02^a12^a22^a32^a42;
1526 c3 = a03^a13^a23^a33^a43;
1527 c4 = a04^a14^a24^a34^a44;
1528 d0 = c4^ROL64(c1, 1);
1529 d1 = c0^ROL64(c2, 1);
1530 d2 = c1^ROL64(c3, 1);
1531 d3 = c2^ROL64(c4, 1);
1532 d4 = c3^ROL64(c0, 1);
1535 b1 = ROL64((a11^d1), 44);
1536 b2 = ROL64((a22^d2), 43);
1537 b3 = ROL64((a33^d3), 21);
1538 b4 = ROL64((a44^d4), 14);
1539 a00 = b0 ^((~b1)& b2 );
1541 a11 = b1 ^((~b2)& b3 );
1542 a22 = b2 ^((~b3)& b4 );
1543 a33 = b3 ^((~b4)& b0 );
1544 a44 = b4 ^((~b0)& b1 );
1546 b2 = ROL64((a20^d0), 3);
1547 b3 = ROL64((a31^d1), 45);
1548 b4 = ROL64((a42^d2), 61);
1549 b0 = ROL64((a03^d3), 28);
1550 b1 = ROL64((a14^d4), 20);
1551 a20 = b0 ^((~b1)& b2 );
1552 a31 = b1 ^((~b2)& b3 );
1553 a42 = b2 ^((~b3)& b4 );
1554 a03 = b3 ^((~b4)& b0 );
1555 a14 = b4 ^((~b0)& b1 );
1557 b4 = ROL64((a40^d0), 18);
1558 b0 = ROL64((a01^d1), 1);
1559 b1 = ROL64((a12^d2), 6);
1560 b2 = ROL64((a23^d3), 25);
1561 b3 = ROL64((a34^d4), 8);
1562 a40 = b0 ^((~b1)& b2 );
1563 a01 = b1 ^((~b2)& b3 );
1564 a12 = b2 ^((~b3)& b4 );
1565 a23 = b3 ^((~b4)& b0 );
1566 a34 = b4 ^((~b0)& b1 );
1568 b1 = ROL64((a10^d0), 36);
1569 b2 = ROL64((a21^d1), 10);
1570 b3 = ROL64((a32^d2), 15);
1571 b4 = ROL64((a43^d3), 56);
1572 b0 = ROL64((a04^d4), 27);
1573 a10 = b0 ^((~b1)& b2 );
1574 a21 = b1 ^((~b2)& b3 );
1575 a32 = b2 ^((~b3)& b4 );
1576 a43 = b3 ^((~b4)& b0 );
1577 a04 = b4 ^((~b0)& b1 );
1579 b3 = ROL64((a30^d0), 41);
1580 b4 = ROL64((a41^d1), 2);
1581 b0 = ROL64((a02^d2), 62);
1582 b1 = ROL64((a13^d3), 55);
1583 b2 = ROL64((a24^d4), 39);
1584 a30 = b0 ^((~b1)& b2 );
1585 a41 = b1 ^((~b2)& b3 );
1586 a02 = b2 ^((~b3)& b4 );
1587 a13 = b3 ^((~b4)& b0 );
1588 a24 = b4 ^((~b0)& b1 );
1590 c0 = a00^a20^a40^a10^a30;
1591 c1 = a11^a31^a01^a21^a41;
1592 c2 = a22^a42^a12^a32^a02;
1593 c3 = a33^a03^a23^a43^a13;
1594 c4 = a44^a14^a34^a04^a24;
1595 d0 = c4^ROL64(c1, 1);
1596 d1 = c0^ROL64(c2, 1);
1597 d2 = c1^ROL64(c3, 1);
1598 d3 = c2^ROL64(c4, 1);
1599 d4 = c3^ROL64(c0, 1);
1602 b1 = ROL64((a31^d1), 44);
1603 b2 = ROL64((a12^d2), 43);
1604 b3 = ROL64((a43^d3), 21);
1605 b4 = ROL64((a24^d4), 14);
1606 a00 = b0 ^((~b1)& b2 );
1608 a31 = b1 ^((~b2)& b3 );
1609 a12 = b2 ^((~b3)& b4 );
1610 a43 = b3 ^((~b4)& b0 );
1611 a24 = b4 ^((~b0)& b1 );
1613 b2 = ROL64((a40^d0), 3);
1614 b3 = ROL64((a21^d1), 45);
1615 b4 = ROL64((a02^d2), 61);
1616 b0 = ROL64((a33^d3), 28);
1617 b1 = ROL64((a14^d4), 20);
1618 a40 = b0 ^((~b1)& b2 );
1619 a21 = b1 ^((~b2)& b3 );
1620 a02 = b2 ^((~b3)& b4 );
1621 a33 = b3 ^((~b4)& b0 );
1622 a14 = b4 ^((~b0)& b1 );
1624 b4 = ROL64((a30^d0), 18);
1625 b0 = ROL64((a11^d1), 1);
1626 b1 = ROL64((a42^d2), 6);
1627 b2 = ROL64((a23^d3), 25);
1628 b3 = ROL64((a04^d4), 8);
1629 a30 = b0 ^((~b1)& b2 );
1630 a11 = b1 ^((~b2)& b3 );
1631 a42 = b2 ^((~b3)& b4 );
1632 a23 = b3 ^((~b4)& b0 );
1633 a04 = b4 ^((~b0)& b1 );
1635 b1 = ROL64((a20^d0), 36);
1636 b2 = ROL64((a01^d1), 10);
1637 b3 = ROL64((a32^d2), 15);
1638 b4 = ROL64((a13^d3), 56);
1639 b0 = ROL64((a44^d4), 27);
1640 a20 = b0 ^((~b1)& b2 );
1641 a01 = b1 ^((~b2)& b3 );
1642 a32 = b2 ^((~b3)& b4 );
1643 a13 = b3 ^((~b4)& b0 );
1644 a44 = b4 ^((~b0)& b1 );
1646 b3 = ROL64((a10^d0), 41);
1647 b4 = ROL64((a41^d1), 2);
1648 b0 = ROL64((a22^d2), 62);
1649 b1 = ROL64((a03^d3), 55);
1650 b2 = ROL64((a34^d4), 39);
1651 a10 = b0 ^((~b1)& b2 );
1652 a41 = b1 ^((~b2)& b3 );
1653 a22 = b2 ^((~b3)& b4 );
1654 a03 = b3 ^((~b4)& b0 );
1655 a34 = b4 ^((~b0)& b1 );
1657 c0 = a00^a40^a30^a20^a10;
1658 c1 = a31^a21^a11^a01^a41;
1659 c2 = a12^a02^a42^a32^a22;
1660 c3 = a43^a33^a23^a13^a03;
1661 c4 = a24^a14^a04^a44^a34;
1662 d0 = c4^ROL64(c1, 1);
1663 d1 = c0^ROL64(c2, 1);
1664 d2 = c1^ROL64(c3, 1);
1665 d3 = c2^ROL64(c4, 1);
1666 d4 = c3^ROL64(c0, 1);
1669 b1 = ROL64((a21^d1), 44);
1670 b2 = ROL64((a42^d2), 43);
1671 b3 = ROL64((a13^d3), 21);
1672 b4 = ROL64((a34^d4), 14);
1673 a00 = b0 ^((~b1)& b2 );
1675 a21 = b1 ^((~b2)& b3 );
1676 a42 = b2 ^((~b3)& b4 );
1677 a13 = b3 ^((~b4)& b0 );
1678 a34 = b4 ^((~b0)& b1 );
1680 b2 = ROL64((a30^d0), 3);
1681 b3 = ROL64((a01^d1), 45);
1682 b4 = ROL64((a22^d2), 61);
1683 b0 = ROL64((a43^d3), 28);
1684 b1 = ROL64((a14^d4), 20);
1685 a30 = b0 ^((~b1)& b2 );
1686 a01 = b1 ^((~b2)& b3 );
1687 a22 = b2 ^((~b3)& b4 );
1688 a43 = b3 ^((~b4)& b0 );
1689 a14 = b4 ^((~b0)& b1 );
1691 b4 = ROL64((a10^d0), 18);
1692 b0 = ROL64((a31^d1), 1);
1693 b1 = ROL64((a02^d2), 6);
1694 b2 = ROL64((a23^d3), 25);
1695 b3 = ROL64((a44^d4), 8);
1696 a10 = b0 ^((~b1)& b2 );
1697 a31 = b1 ^((~b2)& b3 );
1698 a02 = b2 ^((~b3)& b4 );
1699 a23 = b3 ^((~b4)& b0 );
1700 a44 = b4 ^((~b0)& b1 );
1702 b1 = ROL64((a40^d0), 36);
1703 b2 = ROL64((a11^d1), 10);
1704 b3 = ROL64((a32^d2), 15);
1705 b4 = ROL64((a03^d3), 56);
1706 b0 = ROL64((a24^d4), 27);
1707 a40 = b0 ^((~b1)& b2 );
1708 a11 = b1 ^((~b2)& b3 );
1709 a32 = b2 ^((~b3)& b4 );
1710 a03 = b3 ^((~b4)& b0 );
1711 a24 = b4 ^((~b0)& b1 );
1713 b3 = ROL64((a20^d0), 41);
1714 b4 = ROL64((a41^d1), 2);
1715 b0 = ROL64((a12^d2), 62);
1716 b1 = ROL64((a33^d3), 55);
1717 b2 = ROL64((a04^d4), 39);
1718 a20 = b0 ^((~b1)& b2 );
1719 a41 = b1 ^((~b2)& b3 );
1720 a12 = b2 ^((~b3)& b4 );
1721 a33 = b3 ^((~b4)& b0 );
1722 a04 = b4 ^((~b0)& b1 );
1724 c0 = a00^a30^a10^a40^a20;
1725 c1 = a21^a01^a31^a11^a41;
1726 c2 = a42^a22^a02^a32^a12;
1727 c3 = a13^a43^a23^a03^a33;
1728 c4 = a34^a14^a44^a24^a04;
1729 d0 = c4^ROL64(c1, 1);
1730 d1 = c0^ROL64(c2, 1);
1731 d2 = c1^ROL64(c3, 1);
1732 d3 = c2^ROL64(c4, 1);
1733 d4 = c3^ROL64(c0, 1);
1736 b1 = ROL64((a01^d1), 44);
1737 b2 = ROL64((a02^d2), 43);
1738 b3 = ROL64((a03^d3), 21);
1739 b4 = ROL64((a04^d4), 14);
1740 a00 = b0 ^((~b1)& b2 );
1742 a01 = b1 ^((~b2)& b3 );
1743 a02 = b2 ^((~b3)& b4 );
1744 a03 = b3 ^((~b4)& b0 );
1745 a04 = b4 ^((~b0)& b1 );
1747 b2 = ROL64((a10^d0), 3);
1748 b3 = ROL64((a11^d1), 45);
1749 b4 = ROL64((a12^d2), 61);
1750 b0 = ROL64((a13^d3), 28);
1751 b1 = ROL64((a14^d4), 20);
1752 a10 = b0 ^((~b1)& b2 );
1753 a11 = b1 ^((~b2)& b3 );
1754 a12 = b2 ^((~b3)& b4 );
1755 a13 = b3 ^((~b4)& b0 );
1756 a14 = b4 ^((~b0)& b1 );
1758 b4 = ROL64((a20^d0), 18);
1759 b0 = ROL64((a21^d1), 1);
1760 b1 = ROL64((a22^d2), 6);
1761 b2 = ROL64((a23^d3), 25);
1762 b3 = ROL64((a24^d4), 8);
1763 a20 = b0 ^((~b1)& b2 );
1764 a21 = b1 ^((~b2)& b3 );
1765 a22 = b2 ^((~b3)& b4 );
1766 a23 = b3 ^((~b4)& b0 );
1767 a24 = b4 ^((~b0)& b1 );
1769 b1 = ROL64((a30^d0), 36);
1770 b2 = ROL64((a31^d1), 10);
1771 b3 = ROL64((a32^d2), 15);
1772 b4 = ROL64((a33^d3), 56);
1773 b0 = ROL64((a34^d4), 27);
1774 a30 = b0 ^((~b1)& b2 );
1775 a31 = b1 ^((~b2)& b3 );
1776 a32 = b2 ^((~b3)& b4 );
1777 a33 = b3 ^((~b4)& b0 );
1778 a34 = b4 ^((~b0)& b1 );
1780 b3 = ROL64((a40^d0), 41);
1781 b4 = ROL64((a41^d1), 2);
1782 b0 = ROL64((a42^d2), 62);
1783 b1 = ROL64((a43^d3), 55);
1784 b2 = ROL64((a44^d4), 39);
1785 a40 = b0 ^((~b1)& b2 );
1786 a41 = b1 ^((~b2)& b3 );
1787 a42 = b2 ^((~b3)& b4 );
1788 a43 = b3 ^((~b4)& b0 );
1789 a44 = b4 ^((~b0)& b1 );
1794 ** Initialize a new hash. iSize determines the size of the hash
1795 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1796 ** can be zero to use the default hash size of 256 bits.
1798 static void SHA3Init(SHA3Context *p, int iSize){
1799 memset(p, 0, sizeof(*p));
1800 if( iSize>=128 && iSize<=512 ){
1801 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1803 p->nRate = (1600 - 2*256)/8;
1805 #if SHA3_BYTEORDER==1234
1806 /* Known to be little-endian at compile-time. No-op */
1807 #elif SHA3_BYTEORDER==4321
1808 p->ixMask = 7; /* Big-endian */
1811 static unsigned int one = 1;
1812 if( 1==*(unsigned char*)&one ){
1813 /* Little endian. No byte swapping. */
1816 /* Big endian. Byte swap. */
1824 ** Make consecutive calls to the SHA3Update function to add new content
1827 static void SHA3Update(
1829 const unsigned char *aData,
1833 #if SHA3_BYTEORDER==1234
1834 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1835 for(; i+7<nData; i+=8){
1836 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1838 if( p->nLoaded>=p->nRate ){
1845 for(; i<nData; i++){
1846 #if SHA3_BYTEORDER==1234
1847 p->u.x[p->nLoaded] ^= aData[i];
1848 #elif SHA3_BYTEORDER==4321
1849 p->u.x[p->nLoaded^0x07] ^= aData[i];
1851 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1854 if( p->nLoaded==p->nRate ){
1862 ** After all content has been added, invoke SHA3Final() to compute
1863 ** the final hash. The function returns a pointer to the binary
1866 static unsigned char *SHA3Final(SHA3Context *p){
1868 if( p->nLoaded==p->nRate-1 ){
1869 const unsigned char c1 = 0x86;
1870 SHA3Update(p, &c1, 1);
1872 const unsigned char c2 = 0x06;
1873 const unsigned char c3 = 0x80;
1874 SHA3Update(p, &c2, 1);
1875 p->nLoaded = p->nRate - 1;
1876 SHA3Update(p, &c3, 1);
1878 for(i=0; i<p->nRate; i++){
1879 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1881 return &p->u.x[p->nRate];
1883 /* End of the hashing logic
1884 *****************************************************************************/
1887 ** Implementation of the sha3(X,SIZE) function.
1889 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1890 ** size is 256. If X is a BLOB, it is hashed as is.
1891 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1892 ** and the string is hashed without the trailing 0x00 terminator. The hash
1893 ** of a NULL value is NULL.
1895 static void sha3Func(
1896 sqlite3_context *context,
1898 sqlite3_value **argv
1901 int eType = sqlite3_value_type(argv[0]);
1902 int nByte = sqlite3_value_bytes(argv[0]);
1907 iSize = sqlite3_value_int(argv[1]);
1908 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1909 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1914 if( eType==SQLITE_NULL ) return;
1915 SHA3Init(&cx, iSize);
1916 if( eType==SQLITE_BLOB ){
1917 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1919 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1921 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1924 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1925 ** of 50 bytes and add it to the hash.
1927 static void hash_step_vformat(
1928 SHA3Context *p, /* Add content to this context */
1929 const char *zFormat,
1935 va_start(ap, zFormat);
1936 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1938 n = (int)strlen(zBuf);
1939 SHA3Update(p, (unsigned char*)zBuf, n);
1943 ** Implementation of the sha3_query(SQL,SIZE) function.
1945 ** This function compiles and runs the SQL statement(s) given in the
1946 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1949 ** The format of the byte stream that is hashed is summarized as follows:
1959 ** <sql> is the original SQL text for each statement run and <n> is
1960 ** the size of that text. The SQL text is UTF-8. A single R character
1961 ** occurs before the start of each row. N means a NULL value.
1962 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1963 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1964 ** B means blobs of <size> bytes. T means text rendered as <size>
1965 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1968 ** For each SQL statement in the X input, there is one S segment. Each
1969 ** S segment is followed by zero or more R segments, one for each row in the
1970 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1971 ** one for each column in the result set. Segments are concatentated directly
1972 ** with no delimiters of any kind.
1974 static void sha3QueryFunc(
1975 sqlite3_context *context,
1977 sqlite3_value **argv
1979 sqlite3 *db = sqlite3_context_db_handle(context);
1980 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1981 sqlite3_stmt *pStmt = 0;
1982 int nCol; /* Number of columns in the result set */
1983 int i; /* Loop counter */
1993 iSize = sqlite3_value_int(argv[1]);
1994 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1995 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
2000 if( zSql==0 ) return;
2001 SHA3Init(&cx, iSize);
2003 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2005 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2006 zSql, sqlite3_errmsg(db));
2007 sqlite3_finalize(pStmt);
2008 sqlite3_result_error(context, zMsg, -1);
2012 if( !sqlite3_stmt_readonly(pStmt) ){
2013 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2014 sqlite3_finalize(pStmt);
2015 sqlite3_result_error(context, zMsg, -1);
2019 nCol = sqlite3_column_count(pStmt);
2020 z = sqlite3_sql(pStmt);
2023 hash_step_vformat(&cx,"S%d:",n);
2024 SHA3Update(&cx,(unsigned char*)z,n);
2027 /* Compute a hash over the result of the query */
2028 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2029 SHA3Update(&cx,(const unsigned char*)"R",1);
2030 for(i=0; i<nCol; i++){
2031 switch( sqlite3_column_type(pStmt,i) ){
2033 SHA3Update(&cx, (const unsigned char*)"N",1);
2036 case SQLITE_INTEGER: {
2040 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2042 for(j=8; j>=1; j--){
2047 SHA3Update(&cx, x, 9);
2050 case SQLITE_FLOAT: {
2054 double r = sqlite3_column_double(pStmt,i);
2056 for(j=8; j>=1; j--){
2061 SHA3Update(&cx,x,9);
2065 int n2 = sqlite3_column_bytes(pStmt, i);
2066 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2067 hash_step_vformat(&cx,"T%d:",n2);
2068 SHA3Update(&cx, z2, n2);
2072 int n2 = sqlite3_column_bytes(pStmt, i);
2073 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2074 hash_step_vformat(&cx,"B%d:",n2);
2075 SHA3Update(&cx, z2, n2);
2081 sqlite3_finalize(pStmt);
2083 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2090 int sqlite3_shathree_init(
2093 const sqlite3_api_routines *pApi
2096 SQLITE_EXTENSION_INIT2(pApi);
2097 (void)pzErrMsg; /* Unused parameter */
2098 rc = sqlite3_create_function(db, "sha3", 1,
2099 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2101 if( rc==SQLITE_OK ){
2102 rc = sqlite3_create_function(db, "sha3", 2,
2103 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2106 if( rc==SQLITE_OK ){
2107 rc = sqlite3_create_function(db, "sha3_query", 1,
2108 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2109 0, sha3QueryFunc, 0, 0);
2111 if( rc==SQLITE_OK ){
2112 rc = sqlite3_create_function(db, "sha3_query", 2,
2113 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2114 0, sha3QueryFunc, 0, 0);
2119 /************************* End ../ext/misc/shathree.c ********************/
2120 /************************* Begin ../ext/misc/fileio.c ******************/
2124 ** The author disclaims copyright to this source code. In place of
2125 ** a legal notice, here is a blessing:
2127 ** May you do good and not evil.
2128 ** May you find forgiveness for yourself and forgive others.
2129 ** May you share freely, never taking more than you give.
2131 ******************************************************************************
2133 ** This SQLite extension implements SQL functions readfile() and
2134 ** writefile(), and eponymous virtual type "fsdir".
2136 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2138 ** If neither of the optional arguments is present, then this UDF
2139 ** function writes blob DATA to file FILE. If successful, the number
2140 ** of bytes written is returned. If an error occurs, NULL is returned.
2142 ** If the first option argument - MODE - is present, then it must
2143 ** be passed an integer value that corresponds to a POSIX mode
2144 ** value (file type + permissions, as returned in the stat.st_mode
2145 ** field by the stat() system call). Three types of files may
2146 ** be written/created:
2148 ** regular files: (mode & 0170000)==0100000
2149 ** symbolic links: (mode & 0170000)==0120000
2150 ** directories: (mode & 0170000)==0040000
2152 ** For a directory, the DATA is ignored. For a symbolic link, it is
2153 ** interpreted as text and used as the target of the link. For a
2154 ** regular file, it is interpreted as a blob and written into the
2155 ** named file. Regardless of the type of file, its permissions are
2156 ** set to (mode & 0777) before returning.
2158 ** If the optional MTIME argument is present, then it is interpreted
2159 ** as an integer - the number of seconds since the unix epoch. The
2160 ** modification-time of the target file is set to this value before
2163 ** If three or more arguments are passed to this function and an
2164 ** error is encountered, an exception is raised.
2168 ** Read and return the contents of file FILE (type blob) from disk.
2174 ** SELECT * FROM fsdir($path [, $dir]);
2176 ** Parameter $path is an absolute or relative pathname. If the file that it
2177 ** refers to does not exist, it is an error. If the path refers to a regular
2178 ** file or symbolic link, it returns a single row. Or, if the path refers
2179 ** to a directory, it returns one row for the directory, and one row for each
2180 ** file within the hierarchy rooted at $path.
2182 ** Each row has the following columns:
2184 ** name: Path to file or directory (text value).
2185 ** mode: Value of stat.st_mode for directory entry (an integer).
2186 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2187 ** data: For a regular file, a blob containing the file data. For a
2188 ** symlink, a text value containing the text of the link. For a
2191 ** If a non-NULL value is specified for the optional $dir parameter and
2192 ** $path is a relative path, then $path is interpreted relative to $dir.
2193 ** And the paths returned in the "name" column of the table are also
2194 ** relative to directory $dir.
2196 /* #include "sqlite3ext.h" */
2197 SQLITE_EXTENSION_INIT1
2202 #include <sys/types.h>
2203 #include <sys/stat.h>
2205 #if !defined(_WIN32) && !defined(WIN32)
2206 # include <unistd.h>
2207 # include <dirent.h>
2209 # include <sys/time.h>
2211 # include "windows.h"
2213 # include <direct.h>
2214 /* # include "test_windirent.h" */
2215 # define dirent DIRENT
2217 # define chmod _chmod
2222 # define mkdir(path,mode) _mkdir(path)
2223 # define lstat(path,buf) stat(path,buf)
2230 ** Structure of the fsdir() table-valued function
2233 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2234 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2235 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2236 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2237 #define FSDIR_COLUMN_DATA 3 /* File content */
2238 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2239 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2243 ** Set the result stored by context ctx to a blob containing the
2244 ** contents of file zName. Or, leave the result unchanged (NULL)
2245 ** if the file does not exist or is unreadable.
2247 ** If the file exceeds the SQLite blob size limit, through an
2248 ** SQLITE_TOOBIG error.
2250 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2253 static void readFileContents(sqlite3_context *ctx, const char *zName){
2260 in = fopen(zName, "rb");
2262 /* File does not exist or is unreadable. Leave the result set to NULL. */
2265 fseek(in, 0, SEEK_END);
2268 db = sqlite3_context_db_handle(ctx);
2269 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2271 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2275 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2277 sqlite3_result_error_nomem(ctx);
2281 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2282 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2284 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2291 ** Implementation of the "readfile(X)" SQL function. The entire content
2292 ** of the file named X is read and returned as a BLOB. NULL is returned
2293 ** if the file does not exist or is unreadable.
2295 static void readfileFunc(
2296 sqlite3_context *context,
2298 sqlite3_value **argv
2301 (void)(argc); /* Unused parameter */
2302 zName = (const char*)sqlite3_value_text(argv[0]);
2303 if( zName==0 ) return;
2304 readFileContents(context, zName);
2308 ** Set the error message contained in context ctx to the results of
2309 ** vprintf(zFmt, ...).
2311 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2315 zMsg = sqlite3_vmprintf(zFmt, ap);
2316 sqlite3_result_error(ctx, zMsg, -1);
2323 ** This function is designed to convert a Win32 FILETIME structure into the
2324 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2326 static sqlite3_uint64 fileTimeToUnixTime(
2327 LPFILETIME pFileTime
2329 SYSTEMTIME epochSystemTime;
2330 ULARGE_INTEGER epochIntervals;
2331 FILETIME epochFileTime;
2332 ULARGE_INTEGER fileIntervals;
2334 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2335 epochSystemTime.wYear = 1970;
2336 epochSystemTime.wMonth = 1;
2337 epochSystemTime.wDay = 1;
2338 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2339 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2340 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2342 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2343 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2345 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2349 ** This function attempts to normalize the time values found in the stat()
2350 ** buffer to UTC. This is necessary on Win32, where the runtime library
2351 ** appears to return these values as local times.
2353 static void statTimesToUtc(
2355 struct stat *pStatBuf
2358 WIN32_FIND_DATAW fd;
2359 LPWSTR zUnicodeName;
2360 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2361 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2363 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2364 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2365 if( hFindFile!=NULL ){
2366 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2367 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2368 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2369 FindClose(hFindFile);
2371 sqlite3_free(zUnicodeName);
2377 ** This function is used in place of stat(). On Windows, special handling
2378 ** is required in order for the included time to be returned as UTC. On all
2379 ** other systems, this function simply calls stat().
2381 static int fileStat(
2383 struct stat *pStatBuf
2386 int rc = stat(zPath, pStatBuf);
2387 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2390 return stat(zPath, pStatBuf);
2395 ** This function is used in place of lstat(). On Windows, special handling
2396 ** is required in order for the included time to be returned as UTC. On all
2397 ** other systems, this function simply calls lstat().
2399 static int fileLinkStat(
2401 struct stat *pStatBuf
2404 int rc = lstat(zPath, pStatBuf);
2405 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2408 return lstat(zPath, pStatBuf);
2413 ** Argument zFile is the name of a file that will be created and/or written
2414 ** by SQL function writefile(). This function ensures that the directory
2415 ** zFile will be written to exists, creating it if required. The permissions
2416 ** for any path components created by this function are set in accordance
2417 ** with the current umask.
2419 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2420 ** SQLITE_OK is returned if the directory is successfully created, or
2421 ** SQLITE_ERROR otherwise.
2423 static int makeDirectory(
2426 char *zCopy = sqlite3_mprintf("%s", zFile);
2432 int nCopy = (int)strlen(zCopy);
2435 while( rc==SQLITE_OK ){
2439 for(; zCopy[i]!='/' && i<nCopy; i++);
2440 if( i==nCopy ) break;
2443 rc2 = fileStat(zCopy, &sStat);
2445 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2447 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2453 sqlite3_free(zCopy);
2460 ** This function does the work for the writefile() UDF. Refer to
2461 ** header comments at the top of this file for details.
2463 static int writeFile(
2464 sqlite3_context *pCtx, /* Context to return bytes written in */
2465 const char *zFile, /* File to write */
2466 sqlite3_value *pData, /* Data to write */
2467 mode_t mode, /* MODE parameter passed to writefile() */
2468 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2470 #if !defined(_WIN32) && !defined(WIN32)
2471 if( S_ISLNK(mode) ){
2472 const char *zTo = (const char*)sqlite3_value_text(pData);
2473 if( symlink(zTo, zFile)<0 ) return 1;
2477 if( S_ISDIR(mode) ){
2478 if( mkdir(zFile, mode) ){
2479 /* The mkdir() call to create the directory failed. This might not
2480 ** be an error though - if there is already a directory at the same
2481 ** path and either the permissions already match or can be changed
2482 ** to do so using chmod(), it is not an error. */
2485 || 0!=fileStat(zFile, &sStat)
2486 || !S_ISDIR(sStat.st_mode)
2487 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2493 sqlite3_int64 nWrite = 0;
2496 FILE *out = fopen(zFile, "wb");
2497 if( out==0 ) return 1;
2498 z = (const char*)sqlite3_value_blob(pData);
2500 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2501 nWrite = sqlite3_value_bytes(pData);
2507 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2511 sqlite3_result_int64(pCtx, nWrite);
2517 #if !SQLITE_OS_WINRT
2519 FILETIME lastAccess;
2521 SYSTEMTIME currentTime;
2524 LPWSTR zUnicodeName;
2525 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2527 GetSystemTime(¤tTime);
2528 SystemTimeToFileTime(¤tTime, &lastAccess);
2529 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2530 lastWrite.dwLowDateTime = (DWORD)intervals;
2531 lastWrite.dwHighDateTime = intervals >> 32;
2532 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2533 if( zUnicodeName==0 ){
2536 hFile = CreateFileW(
2537 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2538 FILE_FLAG_BACKUP_SEMANTICS, NULL
2540 sqlite3_free(zUnicodeName);
2541 if( hFile!=INVALID_HANDLE_VALUE ){
2542 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2549 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2551 struct timespec times[2];
2552 times[0].tv_nsec = times[1].tv_nsec = 0;
2553 times[0].tv_sec = time(0);
2554 times[1].tv_sec = mtime;
2555 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2560 struct timeval times[2];
2561 times[0].tv_usec = times[1].tv_usec = 0;
2562 times[0].tv_sec = time(0);
2563 times[1].tv_sec = mtime;
2564 if( utimes(zFile, times) ){
2574 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2575 ** Refer to header comments at the top of this file for details.
2577 static void writefileFunc(
2578 sqlite3_context *context,
2580 sqlite3_value **argv
2585 sqlite3_int64 mtime = -1;
2587 if( argc<2 || argc>4 ){
2588 sqlite3_result_error(context,
2589 "wrong number of arguments to function writefile()", -1
2594 zFile = (const char*)sqlite3_value_text(argv[0]);
2595 if( zFile==0 ) return;
2597 mode = (mode_t)sqlite3_value_int(argv[2]);
2600 mtime = sqlite3_value_int64(argv[3]);
2603 res = writeFile(context, zFile, argv[1], mode, mtime);
2604 if( res==1 && errno==ENOENT ){
2605 if( makeDirectory(zFile)==SQLITE_OK ){
2606 res = writeFile(context, zFile, argv[1], mode, mtime);
2610 if( argc>2 && res!=0 ){
2611 if( S_ISLNK(mode) ){
2612 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2613 }else if( S_ISDIR(mode) ){
2614 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2616 ctxErrorMsg(context, "failed to write file: %s", zFile);
2622 ** SQL function: lsmode(MODE)
2624 ** Given a numberic st_mode from stat(), convert it into a human-readable
2625 ** text string in the style of "ls -l".
2627 static void lsModeFunc(
2628 sqlite3_context *context,
2630 sqlite3_value **argv
2633 int iMode = sqlite3_value_int(argv[0]);
2636 if( S_ISLNK(iMode) ){
2638 }else if( S_ISREG(iMode) ){
2640 }else if( S_ISDIR(iMode) ){
2646 int m = (iMode >> ((2-i)*3));
2647 char *a = &z[1 + i*3];
2648 a[0] = (m & 0x4) ? 'r' : '-';
2649 a[1] = (m & 0x2) ? 'w' : '-';
2650 a[2] = (m & 0x1) ? 'x' : '-';
2653 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2656 #ifndef SQLITE_OMIT_VIRTUALTABLE
2659 ** Cursor type for recursively iterating through a directory structure.
2661 typedef struct fsdir_cursor fsdir_cursor;
2662 typedef struct FsdirLevel FsdirLevel;
2665 DIR *pDir; /* From opendir() */
2666 char *zDir; /* Name of directory (nul-terminated) */
2669 struct fsdir_cursor {
2670 sqlite3_vtab_cursor base; /* Base class - must be first */
2672 int nLvl; /* Number of entries in aLvl[] array */
2673 int iLvl; /* Index of current entry */
2674 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2679 struct stat sStat; /* Current lstat() results */
2680 char *zPath; /* Path to current entry */
2681 sqlite3_int64 iRowid; /* Current rowid */
2684 typedef struct fsdir_tab fsdir_tab;
2686 sqlite3_vtab base; /* Base class - must be first */
2690 ** Construct a new fsdir virtual table object.
2692 static int fsdirConnect(
2695 int argc, const char *const*argv,
2696 sqlite3_vtab **ppVtab,
2699 fsdir_tab *pNew = 0;
2705 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2706 if( rc==SQLITE_OK ){
2707 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2708 if( pNew==0 ) return SQLITE_NOMEM;
2709 memset(pNew, 0, sizeof(*pNew));
2710 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2712 *ppVtab = (sqlite3_vtab*)pNew;
2717 ** This method is the destructor for fsdir vtab objects.
2719 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2720 sqlite3_free(pVtab);
2725 ** Constructor for a new fsdir_cursor object.
2727 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2730 pCur = sqlite3_malloc( sizeof(*pCur) );
2731 if( pCur==0 ) return SQLITE_NOMEM;
2732 memset(pCur, 0, sizeof(*pCur));
2734 *ppCursor = &pCur->base;
2739 ** Reset a cursor back to the state it was in when first returned
2742 static void fsdirResetCursor(fsdir_cursor *pCur){
2744 for(i=0; i<=pCur->iLvl; i++){
2745 FsdirLevel *pLvl = &pCur->aLvl[i];
2746 if( pLvl->pDir ) closedir(pLvl->pDir);
2747 sqlite3_free(pLvl->zDir);
2749 sqlite3_free(pCur->zPath);
2750 sqlite3_free(pCur->aLvl);
2761 ** Destructor for an fsdir_cursor.
2763 static int fsdirClose(sqlite3_vtab_cursor *cur){
2764 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2766 fsdirResetCursor(pCur);
2772 ** Set the error message for the virtual table associated with cursor
2773 ** pCur to the results of vprintf(zFmt, ...).
2775 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2778 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2784 ** Advance an fsdir_cursor to its next row of output.
2786 static int fsdirNext(sqlite3_vtab_cursor *cur){
2787 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2788 mode_t m = pCur->sStat.st_mode;
2792 /* Descend into this directory */
2793 int iNew = pCur->iLvl + 1;
2795 if( iNew>=pCur->nLvl ){
2797 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2798 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2799 if( aNew==0 ) return SQLITE_NOMEM;
2800 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2805 pLvl = &pCur->aLvl[iNew];
2807 pLvl->zDir = pCur->zPath;
2809 pLvl->pDir = opendir(pLvl->zDir);
2810 if( pLvl->pDir==0 ){
2811 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2812 return SQLITE_ERROR;
2816 while( pCur->iLvl>=0 ){
2817 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2818 struct dirent *pEntry = readdir(pLvl->pDir);
2820 if( pEntry->d_name[0]=='.' ){
2821 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2822 if( pEntry->d_name[1]=='\0' ) continue;
2824 sqlite3_free(pCur->zPath);
2825 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2826 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2827 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2828 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2829 return SQLITE_ERROR;
2833 closedir(pLvl->pDir);
2834 sqlite3_free(pLvl->zDir);
2841 sqlite3_free(pCur->zPath);
2847 ** Return values of columns for the row at which the series_cursor
2848 ** is currently pointing.
2850 static int fsdirColumn(
2851 sqlite3_vtab_cursor *cur, /* The cursor */
2852 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2853 int i /* Which column to return */
2855 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2857 case FSDIR_COLUMN_NAME: {
2858 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2862 case FSDIR_COLUMN_MODE:
2863 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2866 case FSDIR_COLUMN_MTIME:
2867 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2870 case FSDIR_COLUMN_DATA: {
2871 mode_t m = pCur->sStat.st_mode;
2873 sqlite3_result_null(ctx);
2874 #if !defined(_WIN32) && !defined(WIN32)
2875 }else if( S_ISLNK(m) ){
2877 char *aBuf = aStatic;
2878 sqlite3_int64 nBuf = 64;
2882 n = readlink(pCur->zPath, aBuf, nBuf);
2884 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2886 aBuf = sqlite3_malloc64(nBuf);
2888 sqlite3_result_error_nomem(ctx);
2889 return SQLITE_NOMEM;
2893 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2894 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2897 readFileContents(ctx, pCur->zPath);
2900 case FSDIR_COLUMN_PATH:
2902 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2903 ** always return their values as NULL */
2911 ** Return the rowid for the current row. In this implementation, the
2912 ** first row returned is assigned rowid value 1, and each subsequent
2913 ** row a value 1 more than that of the previous.
2915 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2916 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2917 *pRowid = pCur->iRowid;
2922 ** Return TRUE if the cursor has been moved off of the last
2925 static int fsdirEof(sqlite3_vtab_cursor *cur){
2926 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2927 return (pCur->zPath==0);
2931 ** xFilter callback.
2933 ** idxNum==1 PATH parameter only
2934 ** idxNum==2 Both PATH and DIR supplied
2936 static int fsdirFilter(
2937 sqlite3_vtab_cursor *cur,
2938 int idxNum, const char *idxStr,
2939 int argc, sqlite3_value **argv
2941 const char *zDir = 0;
2942 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2944 fsdirResetCursor(pCur);
2947 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2948 return SQLITE_ERROR;
2951 assert( argc==idxNum && (argc==1 || argc==2) );
2952 zDir = (const char*)sqlite3_value_text(argv[0]);
2954 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2955 return SQLITE_ERROR;
2958 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2961 pCur->nBase = (int)strlen(pCur->zBase)+1;
2962 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2964 pCur->zPath = sqlite3_mprintf("%s", zDir);
2967 if( pCur->zPath==0 ){
2968 return SQLITE_NOMEM;
2970 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2971 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2972 return SQLITE_ERROR;
2979 ** SQLite will invoke this method one or more times while planning a query
2980 ** that uses the generate_series virtual table. This routine needs to create
2981 ** a query plan for each invocation and compute an estimated cost for that
2984 ** In this implementation idxNum is used to represent the
2985 ** query plan. idxStr is unused.
2987 ** The query plan is represented by values of idxNum:
2989 ** (1) The path value is supplied by argv[0]
2990 ** (2) Path is in argv[0] and dir is in argv[1]
2992 static int fsdirBestIndex(
2994 sqlite3_index_info *pIdxInfo
2996 int i; /* Loop over constraints */
2997 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2998 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2999 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
3000 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
3001 const struct sqlite3_index_constraint *pConstraint;
3004 pConstraint = pIdxInfo->aConstraint;
3005 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3006 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3007 switch( pConstraint->iColumn ){
3008 case FSDIR_COLUMN_PATH: {
3009 if( pConstraint->usable ){
3012 }else if( idxPath<0 ){
3017 case FSDIR_COLUMN_DIR: {
3018 if( pConstraint->usable ){
3021 }else if( idxDir<0 ){
3028 if( seenPath || seenDir ){
3029 /* If input parameters are unusable, disallow this plan */
3030 return SQLITE_CONSTRAINT;
3034 pIdxInfo->idxNum = 0;
3035 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3036 ** number. Leave it unchanged. */
3037 pIdxInfo->estimatedRows = 0x7fffffff;
3039 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3040 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3042 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3043 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3044 pIdxInfo->idxNum = 2;
3045 pIdxInfo->estimatedCost = 10.0;
3047 pIdxInfo->idxNum = 1;
3048 pIdxInfo->estimatedCost = 100.0;
3056 ** Register the "fsdir" virtual table.
3058 static int fsdirRegister(sqlite3 *db){
3059 static sqlite3_module fsdirModule = {
3062 fsdirConnect, /* xConnect */
3063 fsdirBestIndex, /* xBestIndex */
3064 fsdirDisconnect, /* xDisconnect */
3066 fsdirOpen, /* xOpen - open a cursor */
3067 fsdirClose, /* xClose - close a cursor */
3068 fsdirFilter, /* xFilter - configure scan constraints */
3069 fsdirNext, /* xNext - advance a cursor */
3070 fsdirEof, /* xEof - check for end of scan */
3071 fsdirColumn, /* xColumn - read data */
3072 fsdirRowid, /* xRowid - read data */
3078 0, /* xFindMethod */
3082 0, /* xRollbackTo */
3083 0, /* xShadowName */
3086 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3089 #else /* SQLITE_OMIT_VIRTUALTABLE */
3090 # define fsdirRegister(x) SQLITE_OK
3096 int sqlite3_fileio_init(
3099 const sqlite3_api_routines *pApi
3102 SQLITE_EXTENSION_INIT2(pApi);
3103 (void)pzErrMsg; /* Unused parameter */
3104 rc = sqlite3_create_function(db, "readfile", 1,
3105 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3106 readfileFunc, 0, 0);
3107 if( rc==SQLITE_OK ){
3108 rc = sqlite3_create_function(db, "writefile", -1,
3109 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3110 writefileFunc, 0, 0);
3112 if( rc==SQLITE_OK ){
3113 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3116 if( rc==SQLITE_OK ){
3117 rc = fsdirRegister(db);
3122 /************************* End ../ext/misc/fileio.c ********************/
3123 /************************* Begin ../ext/misc/completion.c ******************/
3127 ** The author disclaims copyright to this source code. In place of
3128 ** a legal notice, here is a blessing:
3130 ** May you do good and not evil.
3131 ** May you find forgiveness for yourself and forgive others.
3132 ** May you share freely, never taking more than you give.
3134 *************************************************************************
3136 ** This file implements an eponymous virtual table that returns suggested
3137 ** completions for a partial SQL input.
3141 ** SELECT DISTINCT candidate COLLATE nocase
3142 ** FROM completion($prefix,$wholeline)
3145 ** The two query parameters are optional. $prefix is the text of the
3146 ** current word being typed and that is to be completed. $wholeline is
3147 ** the complete input line, used for context.
3149 ** The raw completion() table might return the same candidate multiple
3150 ** times, for example if the same column name is used to two or more
3151 ** tables. And the candidates are returned in an arbitrary order. Hence,
3152 ** the DISTINCT and ORDER BY are recommended.
3154 ** This virtual table operates at the speed of human typing, and so there
3155 ** is no attempt to make it fast. Even a slow implementation will be much
3156 ** faster than any human can type.
3159 /* #include "sqlite3ext.h" */
3160 SQLITE_EXTENSION_INIT1
3165 #ifndef SQLITE_OMIT_VIRTUALTABLE
3167 /* completion_vtab is a subclass of sqlite3_vtab which will
3168 ** serve as the underlying representation of a completion virtual table
3170 typedef struct completion_vtab completion_vtab;
3171 struct completion_vtab {
3172 sqlite3_vtab base; /* Base class - must be first */
3173 sqlite3 *db; /* Database connection for this completion vtab */
3176 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3177 ** serve as the underlying representation of a cursor that scans
3178 ** over rows of the result
3180 typedef struct completion_cursor completion_cursor;
3181 struct completion_cursor {
3182 sqlite3_vtab_cursor base; /* Base class - must be first */
3183 sqlite3 *db; /* Database connection for this cursor */
3184 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3185 char *zPrefix; /* The prefix for the word we want to complete */
3186 char *zLine; /* The whole that we want to complete */
3187 const char *zCurrentRow; /* Current output row */
3188 int szRow; /* Length of the zCurrentRow string */
3189 sqlite3_stmt *pStmt; /* Current statement */
3190 sqlite3_int64 iRowid; /* The rowid */
3191 int ePhase; /* Current phase */
3192 int j; /* inter-phase counter */
3195 /* Values for ePhase:
3197 #define COMPLETION_FIRST_PHASE 1
3198 #define COMPLETION_KEYWORDS 1
3199 #define COMPLETION_PRAGMAS 2
3200 #define COMPLETION_FUNCTIONS 3
3201 #define COMPLETION_COLLATIONS 4
3202 #define COMPLETION_INDEXES 5
3203 #define COMPLETION_TRIGGERS 6
3204 #define COMPLETION_DATABASES 7
3205 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3206 #define COMPLETION_COLUMNS 9
3207 #define COMPLETION_MODULES 10
3208 #define COMPLETION_EOF 11
3211 ** The completionConnect() method is invoked to create a new
3212 ** completion_vtab that describes the completion virtual table.
3214 ** Think of this routine as the constructor for completion_vtab objects.
3216 ** All this routine needs to do is:
3218 ** (1) Allocate the completion_vtab object and initialize all fields.
3220 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3221 ** result set of queries against completion will look like.
3223 static int completionConnect(
3226 int argc, const char *const*argv,
3227 sqlite3_vtab **ppVtab,
3230 completion_vtab *pNew;
3233 (void)(pAux); /* Unused parameter */
3234 (void)(argc); /* Unused parameter */
3235 (void)(argv); /* Unused parameter */
3236 (void)(pzErr); /* Unused parameter */
3238 /* Column numbers */
3239 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3240 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3241 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3242 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3244 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3245 rc = sqlite3_declare_vtab(db,
3248 " prefix TEXT HIDDEN,"
3249 " wholeline TEXT HIDDEN,"
3250 " phase INT HIDDEN" /* Used for debugging only */
3252 if( rc==SQLITE_OK ){
3253 pNew = sqlite3_malloc( sizeof(*pNew) );
3254 *ppVtab = (sqlite3_vtab*)pNew;
3255 if( pNew==0 ) return SQLITE_NOMEM;
3256 memset(pNew, 0, sizeof(*pNew));
3263 ** This method is the destructor for completion_cursor objects.
3265 static int completionDisconnect(sqlite3_vtab *pVtab){
3266 sqlite3_free(pVtab);
3271 ** Constructor for a new completion_cursor object.
3273 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3274 completion_cursor *pCur;
3275 pCur = sqlite3_malloc( sizeof(*pCur) );
3276 if( pCur==0 ) return SQLITE_NOMEM;
3277 memset(pCur, 0, sizeof(*pCur));
3278 pCur->db = ((completion_vtab*)p)->db;
3279 *ppCursor = &pCur->base;
3284 ** Reset the completion_cursor.
3286 static void completionCursorReset(completion_cursor *pCur){
3287 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3288 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3289 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3294 ** Destructor for a completion_cursor.
3296 static int completionClose(sqlite3_vtab_cursor *cur){
3297 completionCursorReset((completion_cursor*)cur);
3303 ** Advance a completion_cursor to its next row of output.
3305 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3306 ** record the current state of the scan. This routine sets ->zCurrentRow
3307 ** to the current row of output and then returns. If no more rows remain,
3308 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3309 ** table that has reached the end of its scan.
3311 ** The current implementation just lists potential identifiers and
3312 ** keywords and filters them by zPrefix. Future enhancements should
3313 ** take zLine into account to try to restrict the set of identifiers and
3314 ** keywords based on what would be legal at the current point of input.
3316 static int completionNext(sqlite3_vtab_cursor *cur){
3317 completion_cursor *pCur = (completion_cursor*)cur;
3318 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3319 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3321 while( pCur->ePhase!=COMPLETION_EOF ){
3322 switch( pCur->ePhase ){
3323 case COMPLETION_KEYWORDS: {
3324 if( pCur->j >= sqlite3_keyword_count() ){
3325 pCur->zCurrentRow = 0;
3326 pCur->ePhase = COMPLETION_DATABASES;
3328 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3333 case COMPLETION_DATABASES: {
3334 if( pCur->pStmt==0 ){
3335 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3339 eNextPhase = COMPLETION_TABLES;
3342 case COMPLETION_TABLES: {
3343 if( pCur->pStmt==0 ){
3346 const char *zSep = "";
3347 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3348 while( sqlite3_step(pS2)==SQLITE_ROW ){
3349 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3350 zSql = sqlite3_mprintf(
3352 "SELECT name FROM \"%w\".sqlite_schema",
3355 if( zSql==0 ) return SQLITE_NOMEM;
3358 sqlite3_finalize(pS2);
3359 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3363 eNextPhase = COMPLETION_COLUMNS;
3366 case COMPLETION_COLUMNS: {
3367 if( pCur->pStmt==0 ){
3370 const char *zSep = "";
3371 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3372 while( sqlite3_step(pS2)==SQLITE_ROW ){
3373 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3374 zSql = sqlite3_mprintf(
3376 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3377 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3378 " WHERE sm.type='table'",
3379 zSql, zSep, zDb, zDb
3381 if( zSql==0 ) return SQLITE_NOMEM;
3384 sqlite3_finalize(pS2);
3385 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3389 eNextPhase = COMPLETION_EOF;
3394 /* This case is when the phase presets zCurrentRow */
3395 if( pCur->zCurrentRow==0 ) continue;
3397 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3398 /* Extract the next row of content */
3399 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3400 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3402 /* When all rows are finished, advance to the next phase */
3403 sqlite3_finalize(pCur->pStmt);
3405 pCur->ePhase = eNextPhase;
3409 if( pCur->nPrefix==0 ) break;
3410 if( pCur->nPrefix<=pCur->szRow
3411 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3421 ** Return values of columns for the row at which the completion_cursor
3422 ** is currently pointing.
3424 static int completionColumn(
3425 sqlite3_vtab_cursor *cur, /* The cursor */
3426 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3427 int i /* Which column to return */
3429 completion_cursor *pCur = (completion_cursor*)cur;
3431 case COMPLETION_COLUMN_CANDIDATE: {
3432 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3435 case COMPLETION_COLUMN_PREFIX: {
3436 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3439 case COMPLETION_COLUMN_WHOLELINE: {
3440 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3443 case COMPLETION_COLUMN_PHASE: {
3444 sqlite3_result_int(ctx, pCur->ePhase);
3452 ** Return the rowid for the current row. In this implementation, the
3453 ** rowid is the same as the output value.
3455 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3456 completion_cursor *pCur = (completion_cursor*)cur;
3457 *pRowid = pCur->iRowid;
3462 ** Return TRUE if the cursor has been moved off of the last
3465 static int completionEof(sqlite3_vtab_cursor *cur){
3466 completion_cursor *pCur = (completion_cursor*)cur;
3467 return pCur->ePhase >= COMPLETION_EOF;
3471 ** This method is called to "rewind" the completion_cursor object back
3472 ** to the first row of output. This method is always called at least
3473 ** once prior to any call to completionColumn() or completionRowid() or
3476 static int completionFilter(
3477 sqlite3_vtab_cursor *pVtabCursor,
3478 int idxNum, const char *idxStr,
3479 int argc, sqlite3_value **argv
3481 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3483 (void)(idxStr); /* Unused parameter */
3484 (void)(argc); /* Unused parameter */
3485 completionCursorReset(pCur);
3487 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3488 if( pCur->nPrefix>0 ){
3489 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3490 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3495 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3496 if( pCur->nLine>0 ){
3497 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3498 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3501 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3502 int i = pCur->nLine;
3503 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3506 pCur->nPrefix = pCur->nLine - i;
3507 if( pCur->nPrefix>0 ){
3508 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3509 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3513 pCur->ePhase = COMPLETION_FIRST_PHASE;
3514 return completionNext(pVtabCursor);
3518 ** SQLite will invoke this method one or more times while planning a query
3519 ** that uses the completion virtual table. This routine needs to create
3520 ** a query plan for each invocation and compute an estimated cost for that
3523 ** There are two hidden parameters that act as arguments to the table-valued
3524 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3525 ** is available and bit 1 is set if "wholeline" is available.
3527 static int completionBestIndex(
3529 sqlite3_index_info *pIdxInfo
3531 int i; /* Loop over constraints */
3532 int idxNum = 0; /* The query plan bitmask */
3533 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3534 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3535 int nArg = 0; /* Number of arguments that completeFilter() expects */
3536 const struct sqlite3_index_constraint *pConstraint;
3538 (void)(tab); /* Unused parameter */
3539 pConstraint = pIdxInfo->aConstraint;
3540 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3541 if( pConstraint->usable==0 ) continue;
3542 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3543 switch( pConstraint->iColumn ){
3544 case COMPLETION_COLUMN_PREFIX:
3548 case COMPLETION_COLUMN_WHOLELINE:
3555 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3556 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3558 if( wholelineIdx>=0 ){
3559 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3560 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3562 pIdxInfo->idxNum = idxNum;
3563 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3564 pIdxInfo->estimatedRows = 500 - 100*nArg;
3569 ** This following structure defines all the methods for the
3570 ** completion virtual table.
3572 static sqlite3_module completionModule = {
3575 completionConnect, /* xConnect */
3576 completionBestIndex, /* xBestIndex */
3577 completionDisconnect, /* xDisconnect */
3579 completionOpen, /* xOpen - open a cursor */
3580 completionClose, /* xClose - close a cursor */
3581 completionFilter, /* xFilter - configure scan constraints */
3582 completionNext, /* xNext - advance a cursor */
3583 completionEof, /* xEof - check for end of scan */
3584 completionColumn, /* xColumn - read data */
3585 completionRowid, /* xRowid - read data */
3591 0, /* xFindMethod */
3595 0, /* xRollbackTo */
3599 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3601 int sqlite3CompletionVtabInit(sqlite3 *db){
3603 #ifndef SQLITE_OMIT_VIRTUALTABLE
3604 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3612 int sqlite3_completion_init(
3615 const sqlite3_api_routines *pApi
3618 SQLITE_EXTENSION_INIT2(pApi);
3619 (void)(pzErrMsg); /* Unused parameter */
3620 #ifndef SQLITE_OMIT_VIRTUALTABLE
3621 rc = sqlite3CompletionVtabInit(db);
3626 /************************* End ../ext/misc/completion.c ********************/
3627 /************************* Begin ../ext/misc/appendvfs.c ******************/
3631 ** The author disclaims copyright to this source code. In place of
3632 ** a legal notice, here is a blessing:
3634 ** May you do good and not evil.
3635 ** May you find forgiveness for yourself and forgive others.
3636 ** May you share freely, never taking more than you give.
3638 ******************************************************************************
3640 ** This file implements a VFS shim that allows an SQLite database to be
3641 ** appended onto the end of some other file, such as an executable.
3643 ** A special record must appear at the end of the file that identifies the
3644 ** file as an appended database and provides the offset to the first page
3645 ** of the exposed content. (Or, it is the length of the content prefix.)
3646 ** For best performance page 1 should be located at a disk page boundary,
3647 ** though that is not required.
3649 ** When opening a database using this VFS, the connection might treat
3650 ** the file as an ordinary SQLite database, or it might treat it as a
3651 ** database appended onto some other file. The decision is made by
3652 ** applying the following rules in order:
3654 ** (1) An empty file is an ordinary database.
3656 ** (2) If the file ends with the appendvfs trailer string
3657 ** "Start-Of-SQLite3-NNNNNNNN" that file is an appended database.
3659 ** (3) If the file begins with the standard SQLite prefix string
3660 ** "SQLite format 3", that file is an ordinary database.
3662 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3663 ** set, then a new database is appended to the already existing file.
3665 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3667 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3668 ** the file containing the database is limited to 1GiB. (1073741824 bytes)
3669 ** This VFS will not read or write past the 1GiB mark. This restriction
3670 ** might be lifted in future versions. For now, if you need a larger
3671 ** database, then keep it in a separate file.
3673 ** If the file being opened is a plain database (not an appended one), then
3674 ** this shim is a pass-through into the default underlying VFS. (rule 3)
3676 /* #include "sqlite3ext.h" */
3677 SQLITE_EXTENSION_INIT1
3681 /* The append mark at the end of the database is:
3683 ** Start-Of-SQLite3-NNNNNNNN
3684 ** 123456789 123456789 12345
3686 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3687 ** the offset to page 1, and also the length of the prefix content.
3689 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3690 #define APND_MARK_PREFIX_SZ 17
3691 #define APND_MARK_FOS_SZ 8
3692 #define APND_MARK_SIZE (APND_MARK_PREFIX_SZ+APND_MARK_FOS_SZ)
3695 ** Maximum size of the combined prefix + database + append-mark. This
3696 ** must be less than 0x40000000 to avoid locking issues on Windows.
3698 #define APND_MAX_SIZE (0x40000000)
3701 ** Try to align the database to an even multiple of APND_ROUNDUP bytes.
3703 #ifndef APND_ROUNDUP
3704 #define APND_ROUNDUP 4096
3706 #define APND_ALIGN_MASK ((sqlite3_int64)(APND_ROUNDUP-1))
3707 #define APND_START_ROUNDUP(fsz) (((fsz)+APND_ALIGN_MASK) & ~APND_ALIGN_MASK)
3710 ** Forward declaration of objects used by this utility
3712 typedef struct sqlite3_vfs ApndVfs;
3713 typedef struct ApndFile ApndFile;
3715 /* Access to a lower-level VFS that (might) implement dynamic loading,
3716 ** access to randomness, etc.
3718 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3719 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3721 /* An open appendvfs file
3723 ** An instance of this structure describes the appended database file.
3724 ** A separate sqlite3_file object is always appended. The appended
3725 ** sqlite3_file object (which can be accessed using ORIGFILE()) describes
3726 ** the entire file, including the prefix, the database, and the
3729 ** The structure of an AppendVFS database is like this:
3731 ** +-------------+---------+----------+-------------+
3732 ** | prefix-file | padding | database | append-mark |
3733 ** +-------------+---------+----------+-------------+
3739 ** "prefix file" - file onto which the database has been appended.
3740 ** "padding" - zero or more bytes inserted so that "database"
3741 ** starts on an APND_ROUNDUP boundary
3742 ** "database" - The SQLite database file
3743 ** "append-mark" - The 25-byte "Start-Of-SQLite3-NNNNNNNN" that indicates
3744 ** the offset from the start of prefix-file to the start
3747 ** The size of the database is iMark - iPgOne.
3749 ** The NNNNNNNN in the "Start-Of-SQLite3-NNNNNNNN" suffix is the value
3750 ** of iPgOne stored as a big-ending 64-bit integer.
3752 ** iMark will be the size of the underlying file minus 25 (APND_MARKSIZE).
3753 ** Or, iMark is -1 to indicate that it has not yet been written.
3756 sqlite3_file base; /* Subclass. MUST BE FIRST! */
3757 sqlite3_int64 iPgOne; /* Offset to the start of the database */
3758 sqlite3_int64 iMark; /* Offset of the append mark. -1 if unwritten */
3759 /* Always followed by another sqlite3_file that describes the whole file */
3763 ** Methods for ApndFile
3765 static int apndClose(sqlite3_file*);
3766 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3767 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3768 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3769 static int apndSync(sqlite3_file*, int flags);
3770 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3771 static int apndLock(sqlite3_file*, int);
3772 static int apndUnlock(sqlite3_file*, int);
3773 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3774 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3775 static int apndSectorSize(sqlite3_file*);
3776 static int apndDeviceCharacteristics(sqlite3_file*);
3777 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3778 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3779 static void apndShmBarrier(sqlite3_file*);
3780 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3781 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3782 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3785 ** Methods for ApndVfs
3787 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3788 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3789 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3790 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3791 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3792 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3793 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3794 static void apndDlClose(sqlite3_vfs*, void*);
3795 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3796 static int apndSleep(sqlite3_vfs*, int microseconds);
3797 static int apndCurrentTime(sqlite3_vfs*, double*);
3798 static int apndGetLastError(sqlite3_vfs*, int, char *);
3799 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3800 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3801 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3802 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3804 static sqlite3_vfs apnd_vfs = {
3805 3, /* iVersion (set when registered) */
3806 0, /* szOsFile (set when registered) */
3807 1024, /* mxPathname */
3809 "apndvfs", /* zName */
3810 0, /* pAppData (set when registered) */
3811 apndOpen, /* xOpen */
3812 apndDelete, /* xDelete */
3813 apndAccess, /* xAccess */
3814 apndFullPathname, /* xFullPathname */
3815 apndDlOpen, /* xDlOpen */
3816 apndDlError, /* xDlError */
3817 apndDlSym, /* xDlSym */
3818 apndDlClose, /* xDlClose */
3819 apndRandomness, /* xRandomness */
3820 apndSleep, /* xSleep */
3821 apndCurrentTime, /* xCurrentTime */
3822 apndGetLastError, /* xGetLastError */
3823 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3824 apndSetSystemCall, /* xSetSystemCall */
3825 apndGetSystemCall, /* xGetSystemCall */
3826 apndNextSystemCall /* xNextSystemCall */
3829 static const sqlite3_io_methods apnd_io_methods = {
3831 apndClose, /* xClose */
3832 apndRead, /* xRead */
3833 apndWrite, /* xWrite */
3834 apndTruncate, /* xTruncate */
3835 apndSync, /* xSync */
3836 apndFileSize, /* xFileSize */
3837 apndLock, /* xLock */
3838 apndUnlock, /* xUnlock */
3839 apndCheckReservedLock, /* xCheckReservedLock */
3840 apndFileControl, /* xFileControl */
3841 apndSectorSize, /* xSectorSize */
3842 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3843 apndShmMap, /* xShmMap */
3844 apndShmLock, /* xShmLock */
3845 apndShmBarrier, /* xShmBarrier */
3846 apndShmUnmap, /* xShmUnmap */
3847 apndFetch, /* xFetch */
3848 apndUnfetch /* xUnfetch */
3852 ** Close an apnd-file.
3854 static int apndClose(sqlite3_file *pFile){
3855 pFile = ORIGFILE(pFile);
3856 return pFile->pMethods->xClose(pFile);
3860 ** Read data from an apnd-file.
3862 static int apndRead(
3863 sqlite3_file *pFile,
3868 ApndFile *paf = (ApndFile *)pFile;
3869 pFile = ORIGFILE(pFile);
3870 return pFile->pMethods->xRead(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3874 ** Add the append-mark onto what should become the end of the file.
3875 * If and only if this succeeds, internal ApndFile.iMark is updated.
3876 * Parameter iWriteEnd is the appendvfs-relative offset of the new mark.
3878 static int apndWriteMark(
3880 sqlite3_file *pFile,
3881 sqlite_int64 iWriteEnd
3883 sqlite_int64 iPgOne = paf->iPgOne;
3884 unsigned char a[APND_MARK_SIZE];
3885 int i = APND_MARK_FOS_SZ;
3887 assert(pFile == ORIGFILE(paf));
3888 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3890 a[APND_MARK_PREFIX_SZ+i] = (unsigned char)(iPgOne & 0xff);
3893 iWriteEnd += paf->iPgOne;
3894 if( SQLITE_OK==(rc = pFile->pMethods->xWrite
3895 (pFile, a, APND_MARK_SIZE, iWriteEnd)) ){
3896 paf->iMark = iWriteEnd;
3902 ** Write data to an apnd-file.
3904 static int apndWrite(
3905 sqlite3_file *pFile,
3910 ApndFile *paf = (ApndFile *)pFile;
3911 sqlite_int64 iWriteEnd = iOfst + iAmt;
3912 if( iWriteEnd>=APND_MAX_SIZE ) return SQLITE_FULL;
3913 pFile = ORIGFILE(pFile);
3914 /* If append-mark is absent or will be overwritten, write it. */
3915 if( paf->iMark < 0 || paf->iPgOne + iWriteEnd > paf->iMark ){
3916 int rc = apndWriteMark(paf, pFile, iWriteEnd);
3917 if( SQLITE_OK!=rc ) return rc;
3919 return pFile->pMethods->xWrite(pFile, zBuf, iAmt, paf->iPgOne+iOfst);
3923 ** Truncate an apnd-file.
3925 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3926 ApndFile *paf = (ApndFile *)pFile;
3927 pFile = ORIGFILE(pFile);
3928 /* The append mark goes out first so truncate failure does not lose it. */
3929 if( SQLITE_OK!=apndWriteMark(paf, pFile, size) ) return SQLITE_IOERR;
3930 /* Truncate underlying file just past append mark */
3931 return pFile->pMethods->xTruncate(pFile, paf->iMark+APND_MARK_SIZE);
3935 ** Sync an apnd-file.
3937 static int apndSync(sqlite3_file *pFile, int flags){
3938 pFile = ORIGFILE(pFile);
3939 return pFile->pMethods->xSync(pFile, flags);
3943 ** Return the current file-size of an apnd-file.
3944 ** If the append mark is not yet there, the file-size is 0.
3946 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3947 ApndFile *paf = (ApndFile *)pFile;
3948 *pSize = ( paf->iMark >= 0 )? (paf->iMark - paf->iPgOne) : 0;
3953 ** Lock an apnd-file.
3955 static int apndLock(sqlite3_file *pFile, int eLock){
3956 pFile = ORIGFILE(pFile);
3957 return pFile->pMethods->xLock(pFile, eLock);
3961 ** Unlock an apnd-file.
3963 static int apndUnlock(sqlite3_file *pFile, int eLock){
3964 pFile = ORIGFILE(pFile);
3965 return pFile->pMethods->xUnlock(pFile, eLock);
3969 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3971 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3972 pFile = ORIGFILE(pFile);
3973 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3977 ** File control method. For custom operations on an apnd-file.
3979 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3980 ApndFile *paf = (ApndFile *)pFile;
3982 pFile = ORIGFILE(pFile);
3983 if( op==SQLITE_FCNTL_SIZE_HINT ) *(sqlite3_int64*)pArg += paf->iPgOne;
3984 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3985 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3986 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", paf->iPgOne,*(char**)pArg);
3992 ** Return the sector-size in bytes for an apnd-file.
3994 static int apndSectorSize(sqlite3_file *pFile){
3995 pFile = ORIGFILE(pFile);
3996 return pFile->pMethods->xSectorSize(pFile);
4000 ** Return the device characteristic flags supported by an apnd-file.
4002 static int apndDeviceCharacteristics(sqlite3_file *pFile){
4003 pFile = ORIGFILE(pFile);
4004 return pFile->pMethods->xDeviceCharacteristics(pFile);
4007 /* Create a shared memory file mapping */
4008 static int apndShmMap(
4009 sqlite3_file *pFile,
4015 pFile = ORIGFILE(pFile);
4016 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
4019 /* Perform locking on a shared-memory segment */
4020 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
4021 pFile = ORIGFILE(pFile);
4022 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
4025 /* Memory barrier operation on shared memory */
4026 static void apndShmBarrier(sqlite3_file *pFile){
4027 pFile = ORIGFILE(pFile);
4028 pFile->pMethods->xShmBarrier(pFile);
4031 /* Unmap a shared memory segment */
4032 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
4033 pFile = ORIGFILE(pFile);
4034 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
4037 /* Fetch a page of a memory-mapped file */
4038 static int apndFetch(
4039 sqlite3_file *pFile,
4040 sqlite3_int64 iOfst,
4044 ApndFile *p = (ApndFile *)pFile;
4045 if( p->iMark < 0 || iOfst+iAmt > p->iMark ){
4046 return SQLITE_IOERR; /* Cannot read what is not yet there. */
4048 pFile = ORIGFILE(pFile);
4049 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4052 /* Release a memory-mapped page */
4053 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4054 ApndFile *p = (ApndFile *)pFile;
4055 pFile = ORIGFILE(pFile);
4056 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4060 ** Try to read the append-mark off the end of a file. Return the
4061 ** start of the appended database if the append-mark is present.
4062 ** If there is no valid append-mark, return -1;
4064 ** An append-mark is only valid if the NNNNNNNN start-of-database offset
4065 ** indicates that the appended database contains at least one page. The
4066 ** start-of-database value must be a multiple of 512.
4068 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4070 sqlite3_int64 iMark;
4071 int msbs = 8 * (APND_MARK_FOS_SZ-1);
4072 unsigned char a[APND_MARK_SIZE];
4074 if( APND_MARK_SIZE!=(sz & 0x1ff) ) return -1;
4075 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4077 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4078 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ] & 0x7f)) << msbs;
4081 iMark |= (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<msbs;
4083 if( iMark > (sz - APND_MARK_SIZE - 512) ) return -1;
4084 if( iMark & 0x1ff ) return -1;
4088 static const char apvfsSqliteHdr[] = "SQLite format 3";
4090 ** Check to see if the file is an appendvfs SQLite database file.
4091 ** Return true iff it is such. Parameter sz is the file's size.
4093 static int apndIsAppendvfsDatabase(sqlite3_int64 sz, sqlite3_file *pFile){
4096 sqlite3_int64 iMark = apndReadMark(sz, pFile);
4098 /* If file has the correct end-marker, the expected odd size, and the
4099 ** SQLite DB type marker where the end-marker puts it, then it
4100 ** is an appendvfs database.
4102 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), iMark);
4104 && memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))==0
4105 && (sz & 0x1ff) == APND_MARK_SIZE
4106 && sz>=512+APND_MARK_SIZE
4108 return 1; /* It's an appendvfs database */
4115 ** Check to see if the file is an ordinary SQLite database file.
4116 ** Return true iff so. Parameter sz is the file's size.
4118 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4120 if( apndIsAppendvfsDatabase(sz, pFile) /* rule 2 */
4121 || (sz & 0x1ff) != 0
4122 || SQLITE_OK!=pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0)
4123 || memcmp(zHdr, apvfsSqliteHdr, sizeof(zHdr))!=0
4132 ** Open an apnd file handle.
4134 static int apndOpen(
4135 sqlite3_vfs *pApndVfs,
4137 sqlite3_file *pFile,
4141 ApndFile *pApndFile = (ApndFile*)pFile;
4142 sqlite3_file *pBaseFile = ORIGFILE(pFile);
4143 sqlite3_vfs *pBaseVfs = ORIGVFS(pApndVfs);
4145 sqlite3_int64 sz = 0;
4146 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4147 /* The appendvfs is not to be used for transient or temporary databases.
4148 ** Just use the base VFS open to initialize the given file object and
4149 ** open the underlying file. (Appendvfs is then unused for this file.)
4151 return pBaseVfs->xOpen(pBaseVfs, zName, pFile, flags, pOutFlags);
4153 memset(pApndFile, 0, sizeof(ApndFile));
4154 pFile->pMethods = &apnd_io_methods;
4155 pApndFile->iMark = -1; /* Append mark not yet written */
4157 rc = pBaseVfs->xOpen(pBaseVfs, zName, pBaseFile, flags, pOutFlags);
4158 if( rc==SQLITE_OK ){
4159 rc = pBaseFile->pMethods->xFileSize(pBaseFile, &sz);
4161 pBaseFile->pMethods->xClose(pBaseFile);
4165 pFile->pMethods = 0;
4168 if( apndIsOrdinaryDatabaseFile(sz, pBaseFile) ){
4169 /* The file being opened appears to be just an ordinary DB. Copy
4170 ** the base dispatch-table so this instance mimics the base VFS.
4172 memmove(pApndFile, pBaseFile, pBaseVfs->szOsFile);
4175 pApndFile->iPgOne = apndReadMark(sz, pFile);
4176 if( pApndFile->iPgOne>=0 ){
4177 pApndFile->iMark = sz - APND_MARK_SIZE; /* Append mark found */
4180 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4181 pBaseFile->pMethods->xClose(pBaseFile);
4182 rc = SQLITE_CANTOPEN;
4183 pFile->pMethods = 0;
4185 /* Round newly added appendvfs location to #define'd page boundary.
4186 ** Note that nothing has yet been written to the underlying file.
4187 ** The append mark will be written along with first content write.
4188 ** Until then, paf->iMark value indicates it is not yet written.
4190 pApndFile->iPgOne = APND_START_ROUNDUP(sz);
4196 ** Delete an apnd file.
4197 ** For an appendvfs, this could mean delete the appendvfs portion,
4198 ** leaving the appendee as it was before it gained an appendvfs.
4199 ** For now, this code deletes the underlying file too.
4201 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4202 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4206 ** All other VFS methods are pass-thrus.
4208 static int apndAccess(
4214 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4216 static int apndFullPathname(
4222 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4224 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4225 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4227 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4228 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4230 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4231 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4233 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4234 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4236 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4237 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4239 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4240 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4242 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4243 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4245 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4246 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4248 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4249 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4251 static int apndSetSystemCall(
4254 sqlite3_syscall_ptr pCall
4256 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4258 static sqlite3_syscall_ptr apndGetSystemCall(
4262 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4264 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4265 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4273 ** This routine is called when the extension is loaded.
4274 ** Register the new VFS.
4276 int sqlite3_appendvfs_init(
4279 const sqlite3_api_routines *pApi
4283 SQLITE_EXTENSION_INIT2(pApi);
4286 pOrig = sqlite3_vfs_find(0);
4287 if( pOrig==0 ) return SQLITE_ERROR;
4288 apnd_vfs.iVersion = pOrig->iVersion;
4289 apnd_vfs.pAppData = pOrig;
4290 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4291 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4292 #ifdef APPENDVFS_TEST
4293 if( rc==SQLITE_OK ){
4294 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4297 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4301 /************************* End ../ext/misc/appendvfs.c ********************/
4302 /************************* Begin ../ext/misc/memtrace.c ******************/
4306 ** The author disclaims copyright to this source code. In place of
4307 ** a legal notice, here is a blessing:
4309 ** May you do good and not evil.
4310 ** May you find forgiveness for yourself and forgive others.
4311 ** May you share freely, never taking more than you give.
4313 *************************************************************************
4315 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4316 ** mechanism to add a tracing layer on top of SQLite. If this extension
4317 ** is registered prior to sqlite3_initialize(), it will cause all memory
4318 ** allocation activities to be logged on standard output, or to some other
4319 ** FILE specified by the initializer.
4321 ** This file needs to be compiled into the application that uses it.
4323 ** This extension is used to implement the --memtrace option of the
4324 ** command-line shell.
4330 /* The original memory allocation routines */
4331 static sqlite3_mem_methods memtraceBase;
4332 static FILE *memtraceOut;
4334 /* Methods that trace memory allocations */
4335 static void *memtraceMalloc(int n){
4337 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4338 memtraceBase.xRoundup(n));
4340 return memtraceBase.xMalloc(n);
4342 static void memtraceFree(void *p){
4345 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4347 memtraceBase.xFree(p);
4349 static void *memtraceRealloc(void *p, int n){
4350 if( p==0 ) return memtraceMalloc(n);
4356 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4357 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4359 return memtraceBase.xRealloc(p, n);
4361 static int memtraceSize(void *p){
4362 return memtraceBase.xSize(p);
4364 static int memtraceRoundup(int n){
4365 return memtraceBase.xRoundup(n);
4367 static int memtraceInit(void *p){
4368 return memtraceBase.xInit(p);
4370 static void memtraceShutdown(void *p){
4371 memtraceBase.xShutdown(p);
4374 /* The substitute memory allocator */
4375 static sqlite3_mem_methods ersaztMethods = {
4386 /* Begin tracing memory allocations to out. */
4387 int sqlite3MemTraceActivate(FILE *out){
4389 if( memtraceBase.xMalloc==0 ){
4390 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4391 if( rc==SQLITE_OK ){
4392 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4399 /* Deactivate memory tracing */
4400 int sqlite3MemTraceDeactivate(void){
4402 if( memtraceBase.xMalloc!=0 ){
4403 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4404 if( rc==SQLITE_OK ){
4405 memset(&memtraceBase, 0, sizeof(memtraceBase));
4412 /************************* End ../ext/misc/memtrace.c ********************/
4413 /************************* Begin ../ext/misc/uint.c ******************/
4417 ** The author disclaims copyright to this source code. In place of
4418 ** a legal notice, here is a blessing:
4420 ** May you do good and not evil.
4421 ** May you find forgiveness for yourself and forgive others.
4422 ** May you share freely, never taking more than you give.
4424 ******************************************************************************
4426 ** This SQLite extension implements the UINT collating sequence.
4428 ** UINT works like BINARY for text, except that embedded strings
4429 ** of digits compare in numeric order.
4431 ** * Leading zeros are handled properly, in the sense that
4432 ** they do not mess of the maginitude comparison of embedded
4433 ** strings of digits. "x00123y" is equal to "x123y".
4435 ** * Only unsigned integers are recognized. Plus and minus
4436 ** signs are ignored. Decimal points and exponential notation
4439 ** * Embedded integers can be of arbitrary length. Comparison
4440 ** is *not* limited integers that can be expressed as a
4441 ** 64-bit machine integer.
4443 /* #include "sqlite3ext.h" */
4444 SQLITE_EXTENSION_INIT1
4450 ** Compare text in lexicographic order, except strings of digits
4451 ** compare in numeric order.
4453 static int uintCollFunc(
4455 int nKey1, const void *pKey1,
4456 int nKey2, const void *pKey2
4458 const unsigned char *zA = (const unsigned char*)pKey1;
4459 const unsigned char *zB = (const unsigned char*)pKey2;
4462 while( i<nKey1 && j<nKey2 ){
4464 if( isdigit(zA[i]) ){
4466 if( !isdigit(zB[j]) ) return x;
4467 while( i<nKey1 && zA[i]=='0' ){ i++; }
4468 while( j<nKey2 && zB[j]=='0' ){ j++; }
4470 while( i+k<nKey1 && isdigit(zA[i+k])
4471 && j+k<nKey2 && isdigit(zB[j+k]) ){
4474 if( i+k<nKey1 && isdigit(zA[i+k]) ){
4476 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4479 x = memcmp(zA+i, zB+j, k);
4491 return (nKey1 - i) - (nKey2 - j);
4497 int sqlite3_uint_init(
4500 const sqlite3_api_routines *pApi
4502 SQLITE_EXTENSION_INIT2(pApi);
4503 (void)pzErrMsg; /* Unused parameter */
4504 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4507 /************************* End ../ext/misc/uint.c ********************/
4508 /************************* Begin ../ext/misc/decimal.c ******************/
4512 ** The author disclaims copyright to this source code. In place of
4513 ** a legal notice, here is a blessing:
4515 ** May you do good and not evil.
4516 ** May you find forgiveness for yourself and forgive others.
4517 ** May you share freely, never taking more than you give.
4519 ******************************************************************************
4521 ** Routines to implement arbitrary-precision decimal math.
4523 ** The focus here is on simplicity and correctness, not performance.
4525 /* #include "sqlite3ext.h" */
4526 SQLITE_EXTENSION_INIT1
4532 /* Mark a function parameter as unused, to suppress nuisance compiler
4534 #ifndef UNUSED_PARAMETER
4535 # define UNUSED_PARAMETER(X) (void)(X)
4539 /* A decimal object */
4540 typedef struct Decimal Decimal;
4542 char sign; /* 0 for positive, 1 for negative */
4543 char oom; /* True if an OOM is encountered */
4544 char isNull; /* True if holds a NULL rather than a number */
4545 char isInit; /* True upon initialization */
4546 int nDigit; /* Total number of digits */
4547 int nFrac; /* Number of digits to the right of the decimal point */
4548 signed char *a; /* Array of digits. Most significant first. */
4552 ** Release memory held by a Decimal, but do not free the object itself.
4554 static void decimal_clear(Decimal *p){
4559 ** Destroy a Decimal object
4561 static void decimal_free(Decimal *p){
4569 ** Allocate a new Decimal object. Initialize it to the number given
4570 ** by the input string.
4572 static Decimal *decimal_new(
4573 sqlite3_context *pCtx,
4576 const unsigned char *zAlt
4580 const unsigned char *zIn;
4582 p = sqlite3_malloc( sizeof(*p) );
4583 if( p==0 ) goto new_no_mem;
4594 if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4599 n = sqlite3_value_bytes(pIn);
4600 zIn = sqlite3_value_text(pIn);
4602 p->a = sqlite3_malloc64( n+1 );
4603 if( p->a==0 ) goto new_no_mem;
4604 for(i=0; isspace(zIn[i]); i++){}
4608 }else if( zIn[i]=='+' ){
4611 while( i<n && zIn[i]=='0' ) i++;
4614 if( c>='0' && c<='9' ){
4615 p->a[p->nDigit++] = c - '0';
4617 p->nFrac = p->nDigit + 1;
4618 }else if( c=='e' || c=='E' ){
4625 }else if( zIn[j]=='+' ){
4628 while( j<n && iExp<1000000 ){
4629 if( zIn[j]>='0' && zIn[j]<='9' ){
4630 iExp = iExp*10 + zIn[j] - '0';
4634 if( neg ) iExp = -iExp;
4640 p->nFrac = p->nDigit - (p->nFrac - 1);
4644 if( iExp<=p->nFrac ){
4653 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4654 if( p->a==0 ) goto new_no_mem;
4655 memset(p->a+p->nDigit, 0, iExp);
4661 nExtra = p->nDigit - p->nFrac - 1;
4668 p->nFrac = p->nDigit - 1;
4672 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4673 if( p->a==0 ) goto new_no_mem;
4674 memmove(p->a+iExp, p->a, p->nDigit);
4675 memset(p->a, 0, iExp);
4683 if( pCtx ) sqlite3_result_error_nomem(pCtx);
4689 ** Make the given Decimal the result.
4691 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4695 if( p==0 || p->oom ){
4696 sqlite3_result_error_nomem(pCtx);
4700 sqlite3_result_null(pCtx);
4703 z = sqlite3_malloc( p->nDigit+4 );
4705 sqlite3_result_error_nomem(pCtx);
4709 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4716 n = p->nDigit - p->nFrac;
4721 while( n>1 && p->a[j]==0 ){
4726 z[i++] = p->a[j] + '0';
4733 z[i++] = p->a[j] + '0';
4735 }while( j<p->nDigit );
4738 sqlite3_result_text(pCtx, z, i, sqlite3_free);
4742 ** SQL Function: decimal(X)
4744 ** Convert input X into decimal and then back into text
4746 static void decimalFunc(
4747 sqlite3_context *context,
4749 sqlite3_value **argv
4751 Decimal *p = decimal_new(context, argv[0], 0, 0);
4752 UNUSED_PARAMETER(argc);
4753 decimal_result(context, p);
4758 ** Compare to Decimal objects. Return negative, 0, or positive if the
4759 ** first object is less than, equal to, or greater than the second.
4761 ** Preconditions for this routine:
4768 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4769 int nASig, nBSig, rc, n;
4770 if( pA->sign!=pB->sign ){
4771 return pA->sign ? -1 : +1;
4774 const Decimal *pTemp = pA;
4778 nASig = pA->nDigit - pA->nFrac;
4779 nBSig = pB->nDigit - pB->nFrac;
4781 return nASig - nBSig;
4784 if( n>pB->nDigit ) n = pB->nDigit;
4785 rc = memcmp(pA->a, pB->a, n);
4787 rc = pA->nDigit - pB->nDigit;
4793 ** SQL Function: decimal_cmp(X, Y)
4795 ** Return negative, zero, or positive if X is less then, equal to, or
4798 static void decimalCmpFunc(
4799 sqlite3_context *context,
4801 sqlite3_value **argv
4803 Decimal *pA = 0, *pB = 0;
4806 UNUSED_PARAMETER(argc);
4807 pA = decimal_new(context, argv[0], 0, 0);
4808 if( pA==0 || pA->isNull ) goto cmp_done;
4809 pB = decimal_new(context, argv[1], 0, 0);
4810 if( pB==0 || pB->isNull ) goto cmp_done;
4811 rc = decimal_cmp(pA, pB);
4813 else if( rc>0 ) rc = +1;
4814 sqlite3_result_int(context, rc);
4821 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4822 ** digits to the right of the decimal point.
4824 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4828 nAddFrac = nFrac - p->nFrac;
4829 nAddSig = (nDigit - p->nDigit) - nAddFrac;
4830 if( nAddFrac==0 && nAddSig==0 ) return;
4831 p->a = sqlite3_realloc64(p->a, nDigit+1);
4837 memmove(p->a+nAddSig, p->a, p->nDigit);
4838 memset(p->a, 0, nAddSig);
4839 p->nDigit += nAddSig;
4842 memset(p->a+p->nDigit, 0, nAddFrac);
4843 p->nDigit += nAddFrac;
4844 p->nFrac += nAddFrac;
4849 ** Add the value pB into pA.
4851 ** Both pA and pB might become denormalized by this routine.
4853 static void decimal_add(Decimal *pA, Decimal *pB){
4854 int nSig, nFrac, nDigit;
4859 if( pA->oom || pB==0 || pB->oom ){
4863 if( pA->isNull || pB->isNull ){
4867 nSig = pA->nDigit - pA->nFrac;
4868 if( nSig && pA->a[0]==0 ) nSig--;
4869 if( nSig<pB->nDigit-pB->nFrac ){
4870 nSig = pB->nDigit - pB->nFrac;
4873 if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4874 nDigit = nSig + nFrac + 1;
4875 decimal_expand(pA, nDigit, nFrac);
4876 decimal_expand(pB, nDigit, nFrac);
4877 if( pA->oom || pB->oom ){
4880 if( pA->sign==pB->sign ){
4882 for(i=nDigit-1; i>=0; i--){
4883 int x = pA->a[i] + pB->a[i] + carry;
4893 signed char *aA, *aB;
4895 rc = memcmp(pA->a, pB->a, nDigit);
4899 pA->sign = !pA->sign;
4904 for(i=nDigit-1; i>=0; i--){
4905 int x = aA[i] - aB[i] - borrow;
4919 ** Compare text in decimal order.
4921 static int decimalCollFunc(
4923 int nKey1, const void *pKey1,
4924 int nKey2, const void *pKey2
4926 const unsigned char *zA = (const unsigned char*)pKey1;
4927 const unsigned char *zB = (const unsigned char*)pKey2;
4928 Decimal *pA = decimal_new(0, 0, nKey1, zA);
4929 Decimal *pB = decimal_new(0, 0, nKey2, zB);
4931 UNUSED_PARAMETER(notUsed);
4932 if( pA==0 || pB==0 ){
4935 rc = decimal_cmp(pA, pB);
4944 ** SQL Function: decimal_add(X, Y)
4945 ** decimal_sub(X, Y)
4947 ** Return the sum or difference of X and Y.
4949 static void decimalAddFunc(
4950 sqlite3_context *context,
4952 sqlite3_value **argv
4954 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4955 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4956 UNUSED_PARAMETER(argc);
4957 decimal_add(pA, pB);
4958 decimal_result(context, pA);
4962 static void decimalSubFunc(
4963 sqlite3_context *context,
4965 sqlite3_value **argv
4967 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4968 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4969 UNUSED_PARAMETER(argc);
4971 pB->sign = !pB->sign;
4972 decimal_add(pA, pB);
4973 decimal_result(context, pA);
4979 /* Aggregate funcion: decimal_sum(X)
4981 ** Works like sum() except that it uses decimal arithmetic for unlimited
4984 static void decimalSumStep(
4985 sqlite3_context *context,
4987 sqlite3_value **argv
4991 UNUSED_PARAMETER(argc);
4992 p = sqlite3_aggregate_context(context, sizeof(*p));
4996 p->a = sqlite3_malloc(2);
5005 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5006 pArg = decimal_new(context, argv[0], 0, 0);
5007 decimal_add(p, pArg);
5010 static void decimalSumInverse(
5011 sqlite3_context *context,
5013 sqlite3_value **argv
5017 UNUSED_PARAMETER(argc);
5018 p = sqlite3_aggregate_context(context, sizeof(*p));
5020 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
5021 pArg = decimal_new(context, argv[0], 0, 0);
5022 if( pArg ) pArg->sign = !pArg->sign;
5023 decimal_add(p, pArg);
5026 static void decimalSumValue(sqlite3_context *context){
5027 Decimal *p = sqlite3_aggregate_context(context, 0);
5029 decimal_result(context, p);
5031 static void decimalSumFinalize(sqlite3_context *context){
5032 Decimal *p = sqlite3_aggregate_context(context, 0);
5034 decimal_result(context, p);
5039 ** SQL Function: decimal_mul(X, Y)
5041 ** Return the product of X and Y.
5043 ** All significant digits after the decimal point are retained.
5044 ** Trailing zeros after the decimal point are omitted as long as
5045 ** the number of digits after the decimal point is no less than
5046 ** either the number of digits in either input.
5048 static void decimalMulFunc(
5049 sqlite3_context *context,
5051 sqlite3_value **argv
5053 Decimal *pA = decimal_new(context, argv[0], 0, 0);
5054 Decimal *pB = decimal_new(context, argv[1], 0, 0);
5055 signed char *acc = 0;
5058 UNUSED_PARAMETER(argc);
5059 if( pA==0 || pA->oom || pA->isNull
5060 || pB==0 || pB->oom || pB->isNull
5064 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
5066 sqlite3_result_error_nomem(context);
5069 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
5070 minFrac = pA->nFrac;
5071 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
5072 for(i=pA->nDigit-1; i>=0; i--){
5073 signed char f = pA->a[i];
5075 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
5076 x = acc[k] + f*pB->a[j] + carry;
5084 sqlite3_free(pA->a);
5087 pA->nDigit += pB->nDigit + 2;
5088 pA->nFrac += pB->nFrac;
5089 pA->sign ^= pB->sign;
5090 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
5094 decimal_result(context, pA);
5105 int sqlite3_decimal_init(
5108 const sqlite3_api_routines *pApi
5111 static const struct {
5112 const char *zFuncName;
5114 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5116 { "decimal", 1, decimalFunc },
5117 { "decimal_cmp", 2, decimalCmpFunc },
5118 { "decimal_add", 2, decimalAddFunc },
5119 { "decimal_sub", 2, decimalSubFunc },
5120 { "decimal_mul", 2, decimalMulFunc },
5123 (void)pzErrMsg; /* Unused parameter */
5125 SQLITE_EXTENSION_INIT2(pApi);
5127 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5128 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5129 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5130 0, aFunc[i].xFunc, 0, 0);
5132 if( rc==SQLITE_OK ){
5133 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5134 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5135 decimalSumStep, decimalSumFinalize,
5136 decimalSumValue, decimalSumInverse, 0);
5138 if( rc==SQLITE_OK ){
5139 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5140 0, decimalCollFunc);
5145 /************************* End ../ext/misc/decimal.c ********************/
5146 /************************* Begin ../ext/misc/ieee754.c ******************/
5150 ** The author disclaims copyright to this source code. In place of
5151 ** a legal notice, here is a blessing:
5153 ** May you do good and not evil.
5154 ** May you find forgiveness for yourself and forgive others.
5155 ** May you share freely, never taking more than you give.
5157 ******************************************************************************
5159 ** This SQLite extension implements functions for the exact display
5160 ** and input of IEEE754 Binary64 floating-point numbers.
5165 ** In the first form, the value X should be a floating-point number.
5166 ** The function will return a string of the form 'ieee754(Y,Z)' where
5167 ** Y and Z are integers such that X==Y*pow(2,Z).
5169 ** In the second form, Y and Z are integers which are the mantissa and
5170 ** base-2 exponent of a new floating point number. The function returns
5171 ** a floating-point value equal to Y*pow(2,Z).
5175 ** ieee754(2.0) -> 'ieee754(2,0)'
5176 ** ieee754(45.25) -> 'ieee754(181,-2)'
5177 ** ieee754(2, 0) -> 2.0
5178 ** ieee754(181, -2) -> 45.25
5180 ** Two additional functions break apart the one-argument ieee754()
5181 ** result into separate integer values:
5183 ** ieee754_mantissa(45.25) -> 181
5184 ** ieee754_exponent(45.25) -> -2
5186 ** These functions convert binary64 numbers into blobs and back again.
5188 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5189 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5191 ** In all single-argument functions, if the argument is an 8-byte blob
5192 ** then that blob is interpreted as a big-endian binary64 value.
5195 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5196 ** -----------------------------------------------
5198 ** This extension in combination with the separate 'decimal' extension
5199 ** can be used to compute the exact decimal representation of binary64
5200 ** values. To begin, first compute a table of exponent values:
5202 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5203 ** WITH RECURSIVE c(x,v) AS (
5206 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5207 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5208 ** WITH RECURSIVE c(x,v) AS (
5211 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5212 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5214 ** Then, to compute the exact decimal representation of a floating
5215 ** point value (the value 47.49 is used in the example) do:
5217 ** WITH c(n) AS (VALUES(47.49))
5218 ** ---------------^^^^^---- Replace with whatever you want
5219 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5220 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5222 ** Here is a query to show various boundry values for the binary64
5225 ** WITH c(name,bin) AS (VALUES
5226 ** ('minimum positive value', x'0000000000000001'),
5227 ** ('maximum subnormal value', x'000fffffffffffff'),
5228 ** ('mininum positive nornal value', x'0010000000000000'),
5229 ** ('maximum value', x'7fefffffffffffff'))
5230 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5231 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5234 /* #include "sqlite3ext.h" */
5235 SQLITE_EXTENSION_INIT1
5239 /* Mark a function parameter as unused, to suppress nuisance compiler
5241 #ifndef UNUSED_PARAMETER
5242 # define UNUSED_PARAMETER(X) (void)(X)
5246 ** Implementation of the ieee754() function
5248 static void ieee754func(
5249 sqlite3_context *context,
5251 sqlite3_value **argv
5259 assert( sizeof(m)==sizeof(r) );
5260 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5261 && sqlite3_value_bytes(argv[0])==sizeof(r)
5263 const unsigned char *x = sqlite3_value_blob(argv[0]);
5265 sqlite3_uint64 v = 0;
5266 for(i=0; i<sizeof(r); i++){
5269 memcpy(&r, &v, sizeof(r));
5271 r = sqlite3_value_double(argv[0]);
5279 memcpy(&a,&r,sizeof(a));
5285 m = a & ((((sqlite3_int64)1)<<52)-1);
5289 m |= ((sqlite3_int64)1)<<52;
5291 while( e<1075 && m>0 && (m&1)==0 ){
5297 switch( *(int*)sqlite3_user_data(context) ){
5299 sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5301 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5304 sqlite3_result_int64(context, m);
5307 sqlite3_result_int(context, e-1075);
5311 sqlite3_int64 m, e, a;
5314 m = sqlite3_value_int64(argv[0]);
5315 e = sqlite3_value_int64(argv[1]);
5317 /* Limit the range of e. Ticket 22dea1cfdb9151e4 2021-03-02 */
5320 }else if( e<-10000 ){
5328 }else if( m==0 && e>-1000 && e<1000 ){
5329 sqlite3_result_double(context, 0.0);
5332 while( (m>>32)&0xffe00000 ){
5336 while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5345 }else if( e>0x7ff ){
5348 a = m & ((((sqlite3_int64)1)<<52)-1);
5350 if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5351 memcpy(&r, &a, sizeof(r));
5352 sqlite3_result_double(context, r);
5357 ** Functions to convert between blobs and floats.
5359 static void ieee754func_from_blob(
5360 sqlite3_context *context,
5362 sqlite3_value **argv
5364 UNUSED_PARAMETER(argc);
5365 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5366 && sqlite3_value_bytes(argv[0])==sizeof(double)
5369 const unsigned char *x = sqlite3_value_blob(argv[0]);
5371 sqlite3_uint64 v = 0;
5372 for(i=0; i<sizeof(r); i++){
5375 memcpy(&r, &v, sizeof(r));
5376 sqlite3_result_double(context, r);
5379 static void ieee754func_to_blob(
5380 sqlite3_context *context,
5382 sqlite3_value **argv
5384 UNUSED_PARAMETER(argc);
5385 if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5386 || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5388 double r = sqlite3_value_double(argv[0]);
5390 unsigned char a[sizeof(r)];
5392 memcpy(&v, &r, sizeof(r));
5393 for(i=1; i<=sizeof(r); i++){
5394 a[sizeof(r)-i] = v&0xff;
5397 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5405 int sqlite3_ieee_init(
5408 const sqlite3_api_routines *pApi
5410 static const struct {
5414 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5416 { "ieee754", 1, 0, ieee754func },
5417 { "ieee754", 2, 0, ieee754func },
5418 { "ieee754_mantissa", 1, 1, ieee754func },
5419 { "ieee754_exponent", 1, 2, ieee754func },
5420 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5421 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5426 SQLITE_EXTENSION_INIT2(pApi);
5427 (void)pzErrMsg; /* Unused parameter */
5428 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5429 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5430 SQLITE_UTF8|SQLITE_INNOCUOUS,
5431 (void*)&aFunc[i].iAux,
5432 aFunc[i].xFunc, 0, 0);
5437 /************************* End ../ext/misc/ieee754.c ********************/
5438 /************************* Begin ../ext/misc/series.c ******************/
5442 ** The author disclaims copyright to this source code. In place of
5443 ** a legal notice, here is a blessing:
5445 ** May you do good and not evil.
5446 ** May you find forgiveness for yourself and forgive others.
5447 ** May you share freely, never taking more than you give.
5449 *************************************************************************
5451 ** This file demonstrates how to create a table-valued-function using
5452 ** a virtual table. This demo implements the generate_series() function
5453 ** which gives similar results to the eponymous function in PostgreSQL.
5456 ** SELECT * FROM generate_series(0,100,5);
5458 ** The query above returns integers from 0 through 100 counting by steps
5461 ** SELECT * FROM generate_series(0,100);
5463 ** Integers from 0 through 100 with a step size of 1.
5465 ** SELECT * FROM generate_series(20) LIMIT 10;
5467 ** Integers 20 through 29.
5471 ** The generate_series "function" is really a virtual table with the
5472 ** following schema:
5474 ** CREATE TABLE generate_series(
5481 ** Function arguments in queries against this virtual table are translated
5482 ** into equality constraints against successive hidden columns. In other
5483 ** words, the following pairs of queries are equivalent to each other:
5485 ** SELECT * FROM generate_series(0,100,5);
5486 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5488 ** SELECT * FROM generate_series(0,100);
5489 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5491 ** SELECT * FROM generate_series(20) LIMIT 10;
5492 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5494 ** The generate_series virtual table implementation leaves the xCreate method
5495 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5496 ** TABLE command with "generate_series" as the USING argument. Instead, there
5497 ** is a single generate_series virtual table that is always available without
5498 ** having to be created first.
5500 ** The xBestIndex method looks for equality constraints against the hidden
5501 ** start, stop, and step columns, and if present, it uses those constraints
5502 ** to bound the sequence of generated values. If the equality constraints
5503 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5504 ** xBestIndex returns a small cost when both start and stop are available,
5505 ** and a very large cost if either start or stop are unavailable. This
5506 ** encourages the query planner to order joins such that the bounds of the
5507 ** series are well-defined.
5509 /* #include "sqlite3ext.h" */
5510 SQLITE_EXTENSION_INIT1
5514 #ifndef SQLITE_OMIT_VIRTUALTABLE
5517 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5518 ** serve as the underlying representation of a cursor that scans
5519 ** over rows of the result
5521 typedef struct series_cursor series_cursor;
5522 struct series_cursor {
5523 sqlite3_vtab_cursor base; /* Base class - must be first */
5524 int isDesc; /* True to count down rather than up */
5525 sqlite3_int64 iRowid; /* The rowid */
5526 sqlite3_int64 iValue; /* Current value ("value") */
5527 sqlite3_int64 mnValue; /* Mimimum value ("start") */
5528 sqlite3_int64 mxValue; /* Maximum value ("stop") */
5529 sqlite3_int64 iStep; /* Increment ("step") */
5533 ** The seriesConnect() method is invoked to create a new
5534 ** series_vtab that describes the generate_series virtual table.
5536 ** Think of this routine as the constructor for series_vtab objects.
5538 ** All this routine needs to do is:
5540 ** (1) Allocate the series_vtab object and initialize all fields.
5542 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5543 ** result set of queries against generate_series will look like.
5545 static int seriesConnect(
5548 int argcUnused, const char *const*argvUnused,
5549 sqlite3_vtab **ppVtab,
5555 /* Column numbers */
5556 #define SERIES_COLUMN_VALUE 0
5557 #define SERIES_COLUMN_START 1
5558 #define SERIES_COLUMN_STOP 2
5559 #define SERIES_COLUMN_STEP 3
5565 rc = sqlite3_declare_vtab(db,
5566 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5567 if( rc==SQLITE_OK ){
5568 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5569 if( pNew==0 ) return SQLITE_NOMEM;
5570 memset(pNew, 0, sizeof(*pNew));
5571 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5577 ** This method is the destructor for series_cursor objects.
5579 static int seriesDisconnect(sqlite3_vtab *pVtab){
5580 sqlite3_free(pVtab);
5585 ** Constructor for a new series_cursor object.
5587 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5588 series_cursor *pCur;
5590 pCur = sqlite3_malloc( sizeof(*pCur) );
5591 if( pCur==0 ) return SQLITE_NOMEM;
5592 memset(pCur, 0, sizeof(*pCur));
5593 *ppCursor = &pCur->base;
5598 ** Destructor for a series_cursor.
5600 static int seriesClose(sqlite3_vtab_cursor *cur){
5607 ** Advance a series_cursor to its next row of output.
5609 static int seriesNext(sqlite3_vtab_cursor *cur){
5610 series_cursor *pCur = (series_cursor*)cur;
5612 pCur->iValue -= pCur->iStep;
5614 pCur->iValue += pCur->iStep;
5621 ** Return values of columns for the row at which the series_cursor
5622 ** is currently pointing.
5624 static int seriesColumn(
5625 sqlite3_vtab_cursor *cur, /* The cursor */
5626 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5627 int i /* Which column to return */
5629 series_cursor *pCur = (series_cursor*)cur;
5630 sqlite3_int64 x = 0;
5632 case SERIES_COLUMN_START: x = pCur->mnValue; break;
5633 case SERIES_COLUMN_STOP: x = pCur->mxValue; break;
5634 case SERIES_COLUMN_STEP: x = pCur->iStep; break;
5635 default: x = pCur->iValue; break;
5637 sqlite3_result_int64(ctx, x);
5642 ** Return the rowid for the current row. In this implementation, the
5643 ** first row returned is assigned rowid value 1, and each subsequent
5644 ** row a value 1 more than that of the previous.
5646 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5647 series_cursor *pCur = (series_cursor*)cur;
5648 *pRowid = pCur->iRowid;
5653 ** Return TRUE if the cursor has been moved off of the last
5656 static int seriesEof(sqlite3_vtab_cursor *cur){
5657 series_cursor *pCur = (series_cursor*)cur;
5659 return pCur->iValue < pCur->mnValue;
5661 return pCur->iValue > pCur->mxValue;
5665 /* True to cause run-time checking of the start=, stop=, and/or step=
5666 ** parameters. The only reason to do this is for testing the
5667 ** constraint checking logic for virtual tables in the SQLite core.
5669 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5670 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5674 ** This method is called to "rewind" the series_cursor object back
5675 ** to the first row of output. This method is always called at least
5676 ** once prior to any call to seriesColumn() or seriesRowid() or
5679 ** The query plan selected by seriesBestIndex is passed in the idxNum
5680 ** parameter. (idxStr is not used in this implementation.) idxNum
5681 ** is a bitmask showing which constraints are available:
5687 ** Also, if bit 8 is set, that means that the series should be output
5688 ** in descending order rather than in ascending order. If bit 16 is
5689 ** set, then output must appear in ascending order.
5691 ** This routine should initialize the cursor and position it so that it
5692 ** is pointing at the first row, or pointing off the end of the table
5693 ** (so that seriesEof() will return true) if the table is empty.
5695 static int seriesFilter(
5696 sqlite3_vtab_cursor *pVtabCursor,
5697 int idxNum, const char *idxStrUnused,
5698 int argc, sqlite3_value **argv
5700 series_cursor *pCur = (series_cursor *)pVtabCursor;
5704 pCur->mnValue = sqlite3_value_int64(argv[i++]);
5709 pCur->mxValue = sqlite3_value_int64(argv[i++]);
5711 pCur->mxValue = 0xffffffff;
5714 pCur->iStep = sqlite3_value_int64(argv[i++]);
5715 if( pCur->iStep==0 ){
5717 }else if( pCur->iStep<0 ){
5718 pCur->iStep = -pCur->iStep;
5719 if( (idxNum & 16)==0 ) idxNum |= 8;
5724 for(i=0; i<argc; i++){
5725 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5726 /* If any of the constraints have a NULL value, then return no rows.
5727 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5735 pCur->iValue = pCur->mxValue;
5736 if( pCur->iStep>0 ){
5737 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5741 pCur->iValue = pCur->mnValue;
5748 ** SQLite will invoke this method one or more times while planning a query
5749 ** that uses the generate_series virtual table. This routine needs to create
5750 ** a query plan for each invocation and compute an estimated cost for that
5753 ** In this implementation idxNum is used to represent the
5754 ** query plan. idxStr is unused.
5756 ** The query plan is represented by bits in idxNum:
5758 ** (1) start = $value -- constraint exists
5759 ** (2) stop = $value -- constraint exists
5760 ** (4) step = $value -- constraint exists
5761 ** (8) output in descending order
5763 static int seriesBestIndex(
5764 sqlite3_vtab *tabUnused,
5765 sqlite3_index_info *pIdxInfo
5767 int i, j; /* Loop over constraints */
5768 int idxNum = 0; /* The query plan bitmask */
5769 int unusableMask = 0; /* Mask of unusable constraints */
5770 int nArg = 0; /* Number of arguments that seriesFilter() expects */
5771 int aIdx[3]; /* Constraints on start, stop, and step */
5772 const struct sqlite3_index_constraint *pConstraint;
5774 /* This implementation assumes that the start, stop, and step columns
5775 ** are the last three columns in the virtual table. */
5776 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5777 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5779 aIdx[0] = aIdx[1] = aIdx[2] = -1;
5780 pConstraint = pIdxInfo->aConstraint;
5781 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5782 int iCol; /* 0 for start, 1 for stop, 2 for step */
5783 int iMask; /* bitmask for those column */
5784 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5785 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5786 assert( iCol>=0 && iCol<=2 );
5788 if( pConstraint->usable==0 ){
5789 unusableMask |= iMask;
5791 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5797 if( (j = aIdx[i])>=0 ){
5798 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5799 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5802 if( (unusableMask & ~idxNum)!=0 ){
5803 /* The start, stop, and step columns are inputs. Therefore if there
5804 ** are unusable constraints on any of start, stop, or step then
5805 ** this plan is unusable */
5806 return SQLITE_CONSTRAINT;
5808 if( (idxNum & 3)==3 ){
5809 /* Both start= and stop= boundaries are available. This is the
5810 ** the preferred case */
5811 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5812 pIdxInfo->estimatedRows = 1000;
5813 if( pIdxInfo->nOrderBy==1 ){
5814 if( pIdxInfo->aOrderBy[0].desc ){
5819 pIdxInfo->orderByConsumed = 1;
5822 /* If either boundary is missing, we have to generate a huge span
5823 ** of numbers. Make this case very expensive so that the query
5824 ** planner will work hard to avoid it. */
5825 pIdxInfo->estimatedRows = 2147483647;
5827 pIdxInfo->idxNum = idxNum;
5832 ** This following structure defines all the methods for the
5833 ** generate_series virtual table.
5835 static sqlite3_module seriesModule = {
5838 seriesConnect, /* xConnect */
5839 seriesBestIndex, /* xBestIndex */
5840 seriesDisconnect, /* xDisconnect */
5842 seriesOpen, /* xOpen - open a cursor */
5843 seriesClose, /* xClose - close a cursor */
5844 seriesFilter, /* xFilter - configure scan constraints */
5845 seriesNext, /* xNext - advance a cursor */
5846 seriesEof, /* xEof - check for end of scan */
5847 seriesColumn, /* xColumn - read data */
5848 seriesRowid, /* xRowid - read data */
5854 0, /* xFindMethod */
5858 0, /* xRollbackTo */
5862 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5867 int sqlite3_series_init(
5870 const sqlite3_api_routines *pApi
5873 SQLITE_EXTENSION_INIT2(pApi);
5874 #ifndef SQLITE_OMIT_VIRTUALTABLE
5875 if( sqlite3_libversion_number()<3008012 ){
5876 *pzErrMsg = sqlite3_mprintf(
5877 "generate_series() requires SQLite 3.8.12 or later");
5878 return SQLITE_ERROR;
5880 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5885 /************************* End ../ext/misc/series.c ********************/
5886 /************************* Begin ../ext/misc/regexp.c ******************/
5890 ** The author disclaims copyright to this source code. In place of
5891 ** a legal notice, here is a blessing:
5893 ** May you do good and not evil.
5894 ** May you find forgiveness for yourself and forgive others.
5895 ** May you share freely, never taking more than you give.
5897 ******************************************************************************
5899 ** The code in this file implements a compact but reasonably
5900 ** efficient regular-expression matcher for posix extended regular
5901 ** expressions against UTF8 text.
5903 ** This file is an SQLite extension. It registers a single function
5904 ** named "regexp(A,B)" where A is the regular expression and B is the
5905 ** string to be matched. By registering this function, SQLite will also
5906 ** then implement the "B regexp A" operator. Note that with the function
5907 ** the regular expression comes first, but with the operator it comes
5910 ** The following regular expression syntax is supported:
5912 ** X* zero or more occurrences of X
5913 ** X+ one or more occurrences of X
5914 ** X? zero or one occurrences of X
5915 ** X{p,q} between p and q occurrences of X
5918 ** ^X X occurring at the beginning of the string
5919 ** X$ X occurring at the end of the string
5920 ** . Match any single character
5921 ** \c Character c where c is one of \{}()[]|*+?.
5922 ** \c C-language escapes for c in afnrtv. ex: \t or \n
5923 ** \uXXXX Where XXXX is exactly 4 hex digits, unicode value XXXX
5924 ** \xXX Where XX is exactly 2 hex digits, unicode value XX
5925 ** [abc] Any single character from the set abc
5926 ** [^abc] Any single character not in the set abc
5927 ** [a-z] Any single character in the range a-z
5928 ** [^a-z] Any single character not in the range a-z
5930 ** \w Word character. [A-Za-z0-9_]
5931 ** \W Non-word character
5934 ** \s Whitespace character
5935 ** \S Non-whitespace character
5937 ** A nondeterministic finite automaton (NFA) is used for matching, so the
5938 ** performance is bounded by O(N*M) where N is the size of the regular
5939 ** expression and M is the size of the input string. The matcher never
5940 ** exhibits exponential behavior. Note that the X{p,q} operator expands
5941 ** to p copies of X following by q-p copies of X? and that the size of the
5942 ** regular expression in the O(N*M) performance bound is computed after
5947 /* #include "sqlite3ext.h" */
5948 SQLITE_EXTENSION_INIT1
5951 ** The following #defines change the names of some functions implemented in
5952 ** this file to prevent name collisions with C-library functions of the
5955 #define re_match sqlite3re_match
5956 #define re_compile sqlite3re_compile
5957 #define re_free sqlite3re_free
5959 /* The end-of-input character */
5960 #define RE_EOF 0 /* End of input */
5962 /* The NFA is implemented as sequence of opcodes taken from the following
5963 ** set. Each opcode has a single integer argument.
5965 #define RE_OP_MATCH 1 /* Match the one character in the argument */
5966 #define RE_OP_ANY 2 /* Match any one character. (Implements ".") */
5967 #define RE_OP_ANYSTAR 3 /* Special optimized version of .* */
5968 #define RE_OP_FORK 4 /* Continue to both next and opcode at iArg */
5969 #define RE_OP_GOTO 5 /* Jump to opcode at iArg */
5970 #define RE_OP_ACCEPT 6 /* Halt and indicate a successful match */
5971 #define RE_OP_CC_INC 7 /* Beginning of a [...] character class */
5972 #define RE_OP_CC_EXC 8 /* Beginning of a [^...] character class */
5973 #define RE_OP_CC_VALUE 9 /* Single value in a character class */
5974 #define RE_OP_CC_RANGE 10 /* Range of values in a character class */
5975 #define RE_OP_WORD 11 /* Perl word character [A-Za-z0-9_] */
5976 #define RE_OP_NOTWORD 12 /* Not a perl word character */
5977 #define RE_OP_DIGIT 13 /* digit: [0-9] */
5978 #define RE_OP_NOTDIGIT 14 /* Not a digit */
5979 #define RE_OP_SPACE 15 /* space: [ \t\n\r\v\f] */
5980 #define RE_OP_NOTSPACE 16 /* Not a digit */
5981 #define RE_OP_BOUNDARY 17 /* Boundary between word and non-word */
5983 /* Each opcode is a "state" in the NFA */
5984 typedef unsigned short ReStateNumber;
5986 /* Because this is an NFA and not a DFA, multiple states can be active at
5987 ** once. An instance of the following object records all active states in
5988 ** the NFA. The implementation is optimized for the common case where the
5989 ** number of actives states is small.
5991 typedef struct ReStateSet {
5992 unsigned nState; /* Number of current states */
5993 ReStateNumber *aState; /* Current states */
5996 /* An input string read one character at a time.
5998 typedef struct ReInput ReInput;
6000 const unsigned char *z; /* All text */
6001 int i; /* Next byte to read */
6002 int mx; /* EOF when i>=mx */
6005 /* A compiled NFA (or an NFA that is in the process of being compiled) is
6006 ** an instance of the following object.
6008 typedef struct ReCompiled ReCompiled;
6010 ReInput sIn; /* Regular expression text */
6011 const char *zErr; /* Error message to return */
6012 char *aOp; /* Operators for the virtual machine */
6013 int *aArg; /* Arguments to each operator */
6014 unsigned (*xNextChar)(ReInput*); /* Next character function */
6015 unsigned char zInit[12]; /* Initial text to match */
6016 int nInit; /* Number of characters in zInit */
6017 unsigned nState; /* Number of entries in aOp[] and aArg[] */
6018 unsigned nAlloc; /* Slots allocated for aOp[] and aArg[] */
6021 /* Add a state to the given state set if it is not already there */
6022 static void re_add_state(ReStateSet *pSet, int newState){
6024 for(i=0; i<pSet->nState; i++) if( pSet->aState[i]==newState ) return;
6025 pSet->aState[pSet->nState++] = (ReStateNumber)newState;
6028 /* Extract the next unicode character from *pzIn and return it. Advance
6029 ** *pzIn to the first byte past the end of the character returned. To
6030 ** be clear: this routine converts utf8 to unicode. This routine is
6031 ** optimized for the common case where the next character is a single byte.
6033 static unsigned re_next_char(ReInput *p){
6035 if( p->i>=p->mx ) return 0;
6038 if( (c&0xe0)==0xc0 && p->i<p->mx && (p->z[p->i]&0xc0)==0x80 ){
6039 c = (c&0x1f)<<6 | (p->z[p->i++]&0x3f);
6040 if( c<0x80 ) c = 0xfffd;
6041 }else if( (c&0xf0)==0xe0 && p->i+1<p->mx && (p->z[p->i]&0xc0)==0x80
6042 && (p->z[p->i+1]&0xc0)==0x80 ){
6043 c = (c&0x0f)<<12 | ((p->z[p->i]&0x3f)<<6) | (p->z[p->i+1]&0x3f);
6045 if( c<=0x7ff || (c>=0xd800 && c<=0xdfff) ) c = 0xfffd;
6046 }else if( (c&0xf8)==0xf0 && p->i+3<p->mx && (p->z[p->i]&0xc0)==0x80
6047 && (p->z[p->i+1]&0xc0)==0x80 && (p->z[p->i+2]&0xc0)==0x80 ){
6048 c = (c&0x07)<<18 | ((p->z[p->i]&0x3f)<<12) | ((p->z[p->i+1]&0x3f)<<6)
6049 | (p->z[p->i+2]&0x3f);
6051 if( c<=0xffff || c>0x10ffff ) c = 0xfffd;
6058 static unsigned re_next_char_nocase(ReInput *p){
6059 unsigned c = re_next_char(p);
6060 if( c>='A' && c<='Z' ) c += 'a' - 'A';
6064 /* Return true if c is a perl "word" character: [A-Za-z0-9_] */
6065 static int re_word_char(int c){
6066 return (c>='0' && c<='9') || (c>='a' && c<='z')
6067 || (c>='A' && c<='Z') || c=='_';
6070 /* Return true if c is a "digit" character: [0-9] */
6071 static int re_digit_char(int c){
6072 return (c>='0' && c<='9');
6075 /* Return true if c is a perl "space" character: [ \t\r\n\v\f] */
6076 static int re_space_char(int c){
6077 return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
6080 /* Run a compiled regular expression on the zero-terminated input
6081 ** string zIn[]. Return true on a match and false if there is no match.
6083 static int re_match(ReCompiled *pRe, const unsigned char *zIn, int nIn){
6084 ReStateSet aStateSet[2], *pThis, *pNext;
6085 ReStateNumber aSpace[100];
6086 ReStateNumber *pToFree;
6088 unsigned int iSwap = 0;
6096 in.mx = nIn>=0 ? nIn : (int)strlen((char const*)zIn);
6098 /* Look for the initial prefix match, if there is one. */
6100 unsigned char x = pRe->zInit[0];
6101 while( in.i+pRe->nInit<=in.mx
6103 strncmp((const char*)zIn+in.i, (const char*)pRe->zInit, pRe->nInit)!=0)
6107 if( in.i+pRe->nInit>in.mx ) return 0;
6110 if( pRe->nState<=(sizeof(aSpace)/(sizeof(aSpace[0])*2)) ){
6112 aStateSet[0].aState = aSpace;
6114 pToFree = sqlite3_malloc64( sizeof(ReStateNumber)*2*pRe->nState );
6115 if( pToFree==0 ) return -1;
6116 aStateSet[0].aState = pToFree;
6118 aStateSet[1].aState = &aStateSet[0].aState[pRe->nState];
6119 pNext = &aStateSet[1];
6121 re_add_state(pNext, 0);
6122 while( c!=RE_EOF && pNext->nState>0 ){
6124 c = pRe->xNextChar(&in);
6126 pNext = &aStateSet[iSwap];
6129 for(i=0; i<pThis->nState; i++){
6130 int x = pThis->aState[i];
6131 switch( pRe->aOp[x] ){
6133 if( pRe->aArg[x]==c ) re_add_state(pNext, x+1);
6137 if( c!=0 ) re_add_state(pNext, x+1);
6141 if( re_word_char(c) ) re_add_state(pNext, x+1);
6144 case RE_OP_NOTWORD: {
6145 if( !re_word_char(c) && c!=0 ) re_add_state(pNext, x+1);
6149 if( re_digit_char(c) ) re_add_state(pNext, x+1);
6152 case RE_OP_NOTDIGIT: {
6153 if( !re_digit_char(c) && c!=0 ) re_add_state(pNext, x+1);
6157 if( re_space_char(c) ) re_add_state(pNext, x+1);
6160 case RE_OP_NOTSPACE: {
6161 if( !re_space_char(c) && c!=0 ) re_add_state(pNext, x+1);
6164 case RE_OP_BOUNDARY: {
6165 if( re_word_char(c)!=re_word_char(cPrev) ) re_add_state(pThis, x+1);
6168 case RE_OP_ANYSTAR: {
6169 re_add_state(pNext, x);
6170 re_add_state(pThis, x+1);
6174 re_add_state(pThis, x+pRe->aArg[x]);
6175 re_add_state(pThis, x+1);
6179 re_add_state(pThis, x+pRe->aArg[x]);
6182 case RE_OP_ACCEPT: {
6186 case RE_OP_CC_EXC: {
6190 case RE_OP_CC_INC: {
6192 int n = pRe->aArg[x];
6194 for(j=1; j>0 && j<n; j++){
6195 if( pRe->aOp[x+j]==RE_OP_CC_VALUE ){
6196 if( pRe->aArg[x+j]==c ){
6201 if( pRe->aArg[x+j]<=c && pRe->aArg[x+j+1]>=c ){
6209 if( pRe->aOp[x]==RE_OP_CC_EXC ) hit = !hit;
6210 if( hit ) re_add_state(pNext, x+n);
6216 for(i=0; i<pNext->nState; i++){
6217 if( pRe->aOp[pNext->aState[i]]==RE_OP_ACCEPT ){ rc = 1; break; }
6220 sqlite3_free(pToFree);
6224 /* Resize the opcode and argument arrays for an RE under construction.
6226 static int re_resize(ReCompiled *p, int N){
6229 aOp = sqlite3_realloc64(p->aOp, N*sizeof(p->aOp[0]));
6230 if( aOp==0 ) return 1;
6232 aArg = sqlite3_realloc64(p->aArg, N*sizeof(p->aArg[0]));
6233 if( aArg==0 ) return 1;
6239 /* Insert a new opcode and argument into an RE under construction. The
6240 ** insertion point is just prior to existing opcode iBefore.
6242 static int re_insert(ReCompiled *p, int iBefore, int op, int arg){
6244 if( p->nAlloc<=p->nState && re_resize(p, p->nAlloc*2) ) return 0;
6245 for(i=p->nState; i>iBefore; i--){
6246 p->aOp[i] = p->aOp[i-1];
6247 p->aArg[i] = p->aArg[i-1];
6250 p->aOp[iBefore] = (char)op;
6251 p->aArg[iBefore] = arg;
6255 /* Append a new opcode and argument to the end of the RE under construction.
6257 static int re_append(ReCompiled *p, int op, int arg){
6258 return re_insert(p, p->nState, op, arg);
6261 /* Make a copy of N opcodes starting at iStart onto the end of the RE
6262 ** under construction.
6264 static void re_copy(ReCompiled *p, int iStart, int N){
6265 if( p->nState+N>=p->nAlloc && re_resize(p, p->nAlloc*2+N) ) return;
6266 memcpy(&p->aOp[p->nState], &p->aOp[iStart], N*sizeof(p->aOp[0]));
6267 memcpy(&p->aArg[p->nState], &p->aArg[iStart], N*sizeof(p->aArg[0]));
6271 /* Return true if c is a hexadecimal digit character: [0-9a-fA-F]
6272 ** If c is a hex digit, also set *pV = (*pV)*16 + valueof(c). If
6273 ** c is not a hex digit *pV is unchanged.
6275 static int re_hex(int c, int *pV){
6276 if( c>='0' && c<='9' ){
6278 }else if( c>='a' && c<='f' ){
6280 }else if( c>='A' && c<='F' ){
6285 *pV = (*pV)*16 + (c & 0xff);
6289 /* A backslash character has been seen, read the next character and
6290 ** return its interpretation.
6292 static unsigned re_esc_char(ReCompiled *p){
6293 static const char zEsc[] = "afnrtv\\()*.+?[$^{|}]";
6294 static const char zTrans[] = "\a\f\n\r\t\v";
6297 if( p->sIn.i>=p->sIn.mx ) return 0;
6298 c = p->sIn.z[p->sIn.i];
6299 if( c=='u' && p->sIn.i+4<p->sIn.mx ){
6300 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6301 if( re_hex(zIn[1],&v)
6302 && re_hex(zIn[2],&v)
6303 && re_hex(zIn[3],&v)
6304 && re_hex(zIn[4],&v)
6310 if( c=='x' && p->sIn.i+2<p->sIn.mx ){
6311 const unsigned char *zIn = p->sIn.z + p->sIn.i;
6312 if( re_hex(zIn[1],&v)
6313 && re_hex(zIn[2],&v)
6319 for(i=0; zEsc[i] && zEsc[i]!=c; i++){}
6321 if( i<6 ) c = zTrans[i];
6324 p->zErr = "unknown \\ escape";
6329 /* Forward declaration */
6330 static const char *re_subcompile_string(ReCompiled*);
6332 /* Peek at the next byte of input */
6333 static unsigned char rePeek(ReCompiled *p){
6334 return p->sIn.i<p->sIn.mx ? p->sIn.z[p->sIn.i] : 0;
6337 /* Compile RE text into a sequence of opcodes. Continue up to the
6338 ** first unmatched ")" character, then return. If an error is found,
6339 ** return a pointer to the error message string.
6341 static const char *re_subcompile_re(ReCompiled *p){
6343 int iStart, iEnd, iGoto;
6345 zErr = re_subcompile_string(p);
6346 if( zErr ) return zErr;
6347 while( rePeek(p)=='|' ){
6349 re_insert(p, iStart, RE_OP_FORK, iEnd + 2 - iStart);
6350 iGoto = re_append(p, RE_OP_GOTO, 0);
6352 zErr = re_subcompile_string(p);
6353 if( zErr ) return zErr;
6354 p->aArg[iGoto] = p->nState - iGoto;
6359 /* Compile an element of regular expression text (anything that can be
6360 ** an operand to the "|" operator). Return NULL on success or a pointer
6361 ** to the error message if there is a problem.
6363 static const char *re_subcompile_string(ReCompiled *p){
6368 while( (c = p->xNextChar(&p->sIn))!=0 ){
6378 zErr = re_subcompile_re(p);
6379 if( zErr ) return zErr;
6380 if( rePeek(p)!=')' ) return "unmatched '('";
6385 if( rePeek(p)=='*' ){
6386 re_append(p, RE_OP_ANYSTAR, 0);
6389 re_append(p, RE_OP_ANY, 0);
6394 if( iPrev<0 ) return "'*' without operand";
6395 re_insert(p, iPrev, RE_OP_GOTO, p->nState - iPrev + 1);
6396 re_append(p, RE_OP_FORK, iPrev - p->nState + 1);
6400 if( iPrev<0 ) return "'+' without operand";
6401 re_append(p, RE_OP_FORK, iPrev - p->nState);
6405 if( iPrev<0 ) return "'?' without operand";
6406 re_insert(p, iPrev, RE_OP_FORK, p->nState - iPrev+1);
6412 if( iPrev<0 ) return "'{m,n}' without operand";
6413 while( (c=rePeek(p))>='0' && c<='9' ){ m = m*10 + c - '0'; p->sIn.i++; }
6418 while( (c=rePeek(p))>='0' && c<='9' ){ n = n*10 + c-'0'; p->sIn.i++; }
6420 if( c!='}' ) return "unmatched '{'";
6421 if( n>0 && n<m ) return "n less than m in '{m,n}'";
6423 sz = p->nState - iPrev;
6425 if( n==0 ) return "both m and n are zero in '{m,n}'";
6426 re_insert(p, iPrev, RE_OP_FORK, sz+1);
6429 for(j=1; j<m; j++) re_copy(p, iPrev, sz);
6432 re_append(p, RE_OP_FORK, sz+1);
6433 re_copy(p, iPrev, sz);
6436 re_append(p, RE_OP_FORK, -sz);
6441 int iFirst = p->nState;
6442 if( rePeek(p)=='^' ){
6443 re_append(p, RE_OP_CC_EXC, 0);
6446 re_append(p, RE_OP_CC_INC, 0);
6448 while( (c = p->xNextChar(&p->sIn))!=0 ){
6449 if( c=='[' && rePeek(p)==':' ){
6450 return "POSIX character classes not supported";
6452 if( c=='\\' ) c = re_esc_char(p);
6453 if( rePeek(p)=='-' ){
6454 re_append(p, RE_OP_CC_RANGE, c);
6456 c = p->xNextChar(&p->sIn);
6457 if( c=='\\' ) c = re_esc_char(p);
6458 re_append(p, RE_OP_CC_RANGE, c);
6460 re_append(p, RE_OP_CC_VALUE, c);
6462 if( rePeek(p)==']' ){ p->sIn.i++; break; }
6464 if( c==0 ) return "unclosed '['";
6465 p->aArg[iFirst] = p->nState - iFirst;
6470 switch( rePeek(p) ){
6471 case 'b': specialOp = RE_OP_BOUNDARY; break;
6472 case 'd': specialOp = RE_OP_DIGIT; break;
6473 case 'D': specialOp = RE_OP_NOTDIGIT; break;
6474 case 's': specialOp = RE_OP_SPACE; break;
6475 case 'S': specialOp = RE_OP_NOTSPACE; break;
6476 case 'w': specialOp = RE_OP_WORD; break;
6477 case 'W': specialOp = RE_OP_NOTWORD; break;
6481 re_append(p, specialOp, 0);
6484 re_append(p, RE_OP_MATCH, c);
6489 re_append(p, RE_OP_MATCH, c);
6498 /* Free and reclaim all the memory used by a previously compiled
6499 ** regular expression. Applications should invoke this routine once
6500 ** for every call to re_compile() to avoid memory leaks.
6502 static void re_free(ReCompiled *pRe){
6504 sqlite3_free(pRe->aOp);
6505 sqlite3_free(pRe->aArg);
6511 ** Compile a textual regular expression in zIn[] into a compiled regular
6512 ** expression suitable for us by re_match() and return a pointer to the
6513 ** compiled regular expression in *ppRe. Return NULL on success or an
6514 ** error message if something goes wrong.
6516 static const char *re_compile(ReCompiled **ppRe, const char *zIn, int noCase){
6522 pRe = sqlite3_malloc( sizeof(*pRe) );
6524 return "out of memory";
6526 memset(pRe, 0, sizeof(*pRe));
6527 pRe->xNextChar = noCase ? re_next_char_nocase : re_next_char;
6528 if( re_resize(pRe, 30) ){
6530 return "out of memory";
6535 re_append(pRe, RE_OP_ANYSTAR, 0);
6537 pRe->sIn.z = (unsigned char*)zIn;
6539 pRe->sIn.mx = (int)strlen(zIn);
6540 zErr = re_subcompile_re(pRe);
6545 if( rePeek(pRe)=='$' && pRe->sIn.i+1>=pRe->sIn.mx ){
6546 re_append(pRe, RE_OP_MATCH, RE_EOF);
6547 re_append(pRe, RE_OP_ACCEPT, 0);
6549 }else if( pRe->sIn.i>=pRe->sIn.mx ){
6550 re_append(pRe, RE_OP_ACCEPT, 0);
6554 return "unrecognized character";
6557 /* The following is a performance optimization. If the regex begins with
6558 ** ".*" (if the input regex lacks an initial "^") and afterwards there are
6559 ** one or more matching characters, enter those matching characters into
6560 ** zInit[]. The re_match() routine can then search ahead in the input
6561 ** string looking for the initial match without having to run the whole
6562 ** regex engine over the string. Do not worry able trying to match
6563 ** unicode characters beyond plane 0 - those are very rare and this is
6564 ** just an optimization. */
6565 if( pRe->aOp[0]==RE_OP_ANYSTAR && !noCase ){
6566 for(j=0, i=1; j<(int)sizeof(pRe->zInit)-2 && pRe->aOp[i]==RE_OP_MATCH; i++){
6567 unsigned x = pRe->aArg[i];
6569 pRe->zInit[j++] = (unsigned char)x;
6570 }else if( x<=0xfff ){
6571 pRe->zInit[j++] = (unsigned char)(0xc0 | (x>>6));
6572 pRe->zInit[j++] = 0x80 | (x&0x3f);
6573 }else if( x<=0xffff ){
6574 pRe->zInit[j++] = (unsigned char)(0xd0 | (x>>12));
6575 pRe->zInit[j++] = 0x80 | ((x>>6)&0x3f);
6576 pRe->zInit[j++] = 0x80 | (x&0x3f);
6581 if( j>0 && pRe->zInit[j-1]==0 ) j--;
6588 ** Implementation of the regexp() SQL function. This function implements
6589 ** the build-in REGEXP operator. The first argument to the function is the
6590 ** pattern and the second argument is the string. So, the SQL statements:
6594 ** is implemented as regexp(B,A).
6596 static void re_sql_func(
6597 sqlite3_context *context,
6599 sqlite3_value **argv
6601 ReCompiled *pRe; /* Compiled regular expression */
6602 const char *zPattern; /* The regular expression */
6603 const unsigned char *zStr;/* String being searched */
6604 const char *zErr; /* Compile error message */
6605 int setAux = 0; /* True to invoke sqlite3_set_auxdata() */
6607 (void)argc; /* Unused */
6608 pRe = sqlite3_get_auxdata(context, 0);
6610 zPattern = (const char*)sqlite3_value_text(argv[0]);
6611 if( zPattern==0 ) return;
6612 zErr = re_compile(&pRe, zPattern, sqlite3_user_data(context)!=0);
6615 sqlite3_result_error(context, zErr, -1);
6619 sqlite3_result_error_nomem(context);
6624 zStr = (const unsigned char*)sqlite3_value_text(argv[1]);
6626 sqlite3_result_int(context, re_match(pRe, zStr, -1));
6629 sqlite3_set_auxdata(context, 0, pRe, (void(*)(void*))re_free);
6634 ** Invoke this routine to register the regexp() function with the
6635 ** SQLite database connection.
6640 int sqlite3_regexp_init(
6643 const sqlite3_api_routines *pApi
6646 SQLITE_EXTENSION_INIT2(pApi);
6647 (void)pzErrMsg; /* Unused */
6648 rc = sqlite3_create_function(db, "regexp", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6649 0, re_sql_func, 0, 0);
6650 if( rc==SQLITE_OK ){
6651 /* The regexpi(PATTERN,STRING) function is a case-insensitive version
6652 ** of regexp(PATTERN,STRING). */
6653 rc = sqlite3_create_function(db, "regexpi", 2, SQLITE_UTF8|SQLITE_INNOCUOUS,
6654 (void*)db, re_sql_func, 0, 0);
6659 /************************* End ../ext/misc/regexp.c ********************/
6660 #ifdef SQLITE_HAVE_ZLIB
6661 /************************* Begin ../ext/misc/zipfile.c ******************/
6665 ** The author disclaims copyright to this source code. In place of
6666 ** a legal notice, here is a blessing:
6668 ** May you do good and not evil.
6669 ** May you find forgiveness for yourself and forgive others.
6670 ** May you share freely, never taking more than you give.
6672 ******************************************************************************
6674 ** This file implements a virtual table for reading and writing ZIP archive
6679 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
6681 ** Current limitations:
6683 ** * No support for encryption
6684 ** * No support for ZIP archives spanning multiple files
6685 ** * No support for zip64 extensions
6686 ** * Only the "inflate/deflate" (zlib) compression method is supported
6688 /* #include "sqlite3ext.h" */
6689 SQLITE_EXTENSION_INIT1
6696 #ifndef SQLITE_OMIT_VIRTUALTABLE
6698 #ifndef SQLITE_AMALGAMATION
6701 # ifdef HAVE_UINT32_T
6702 # define UINT32_TYPE uint32_t
6704 # define UINT32_TYPE unsigned int
6708 # ifdef HAVE_UINT16_T
6709 # define UINT16_TYPE uint16_t
6711 # define UINT16_TYPE unsigned short int
6714 /* typedef sqlite3_int64 i64; */
6715 /* typedef unsigned char u8; */
6716 typedef UINT32_TYPE u32; /* 4-byte unsigned integer */
6717 typedef UINT16_TYPE u16; /* 2-byte unsigned integer */
6718 #define MIN(a,b) ((a)<(b) ? (a) : (b))
6720 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
6721 # define ALWAYS(X) (1)
6722 # define NEVER(X) (0)
6723 #elif !defined(NDEBUG)
6724 # define ALWAYS(X) ((X)?1:(assert(0),0))
6725 # define NEVER(X) ((X)?(assert(0),1):0)
6727 # define ALWAYS(X) (X)
6728 # define NEVER(X) (X)
6731 #endif /* SQLITE_AMALGAMATION */
6734 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
6736 ** In some ways it would be better to obtain these values from system
6737 ** header files. But, the dependency is undesirable and (a) these
6738 ** have been stable for decades, (b) the values are part of POSIX and
6739 ** are also made explicit in [man stat], and (c) are part of the
6740 ** file format for zip archives.
6743 # define S_IFDIR 0040000
6746 # define S_IFREG 0100000
6749 # define S_IFLNK 0120000
6752 static const char ZIPFILE_SCHEMA[] =
6754 "name PRIMARY KEY," /* 0: Name of file in zip archive */
6755 "mode," /* 1: POSIX mode for file */
6756 "mtime," /* 2: Last modification time (secs since 1970)*/
6757 "sz," /* 3: Size of object */
6758 "rawdata," /* 4: Raw data */
6759 "data," /* 5: Uncompressed data */
6760 "method," /* 6: Compression method (integer) */
6761 "z HIDDEN" /* 7: Name of zip file */
6764 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
6765 #define ZIPFILE_BUFFER_SIZE (64*1024)
6769 ** Magic numbers used to read and write zip files.
6771 ** ZIPFILE_NEWENTRY_MADEBY:
6772 ** Use this value for the "version-made-by" field in new zip file
6773 ** entries. The upper byte indicates "unix", and the lower byte
6774 ** indicates that the zip file matches pkzip specification 3.0.
6775 ** This is what info-zip seems to do.
6777 ** ZIPFILE_NEWENTRY_REQUIRED:
6778 ** Value for "version-required-to-extract" field of new entries.
6779 ** Version 2.0 is required to support folders and deflate compression.
6781 ** ZIPFILE_NEWENTRY_FLAGS:
6782 ** Value for "general-purpose-bit-flags" field of new entries. Bit
6783 ** 11 means "utf-8 filename and comment".
6785 ** ZIPFILE_SIGNATURE_CDS:
6786 ** First 4 bytes of a valid CDS record.
6788 ** ZIPFILE_SIGNATURE_LFH:
6789 ** First 4 bytes of a valid LFH record.
6791 ** ZIPFILE_SIGNATURE_EOCD
6792 ** First 4 bytes of a valid EOCD record.
6794 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
6795 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
6796 #define ZIPFILE_NEWENTRY_REQUIRED 20
6797 #define ZIPFILE_NEWENTRY_FLAGS 0x800
6798 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
6799 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
6800 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
6803 ** The sizes of the fixed-size part of each of the three main data
6804 ** structures in a zip archive.
6806 #define ZIPFILE_LFH_FIXED_SZ 30
6807 #define ZIPFILE_EOCD_FIXED_SZ 22
6808 #define ZIPFILE_CDS_FIXED_SZ 46
6811 *** 4.3.16 End of central directory record:
6813 *** end of central dir signature 4 bytes (0x06054b50)
6814 *** number of this disk 2 bytes
6815 *** number of the disk with the
6816 *** start of the central directory 2 bytes
6817 *** total number of entries in the
6818 *** central directory on this disk 2 bytes
6819 *** total number of entries in
6820 *** the central directory 2 bytes
6821 *** size of the central directory 4 bytes
6822 *** offset of start of central
6823 *** directory with respect to
6824 *** the starting disk number 4 bytes
6825 *** .ZIP file comment length 2 bytes
6826 *** .ZIP file comment (variable size)
6828 typedef struct ZipfileEOCD ZipfileEOCD;
6829 struct ZipfileEOCD {
6839 *** 4.3.12 Central directory structure:
6843 *** central file header signature 4 bytes (0x02014b50)
6844 *** version made by 2 bytes
6845 *** version needed to extract 2 bytes
6846 *** general purpose bit flag 2 bytes
6847 *** compression method 2 bytes
6848 *** last mod file time 2 bytes
6849 *** last mod file date 2 bytes
6851 *** compressed size 4 bytes
6852 *** uncompressed size 4 bytes
6853 *** file name length 2 bytes
6854 *** extra field length 2 bytes
6855 *** file comment length 2 bytes
6856 *** disk number start 2 bytes
6857 *** internal file attributes 2 bytes
6858 *** external file attributes 4 bytes
6859 *** relative offset of local header 4 bytes
6861 typedef struct ZipfileCDS ZipfileCDS;
6864 u16 iVersionExtract;
6879 char *zFile; /* Filename (sqlite3_malloc()) */
6883 *** 4.3.7 Local file header:
6885 *** local file header signature 4 bytes (0x04034b50)
6886 *** version needed to extract 2 bytes
6887 *** general purpose bit flag 2 bytes
6888 *** compression method 2 bytes
6889 *** last mod file time 2 bytes
6890 *** last mod file date 2 bytes
6892 *** compressed size 4 bytes
6893 *** uncompressed size 4 bytes
6894 *** file name length 2 bytes
6895 *** extra field length 2 bytes
6898 typedef struct ZipfileLFH ZipfileLFH;
6900 u16 iVersionExtract;
6912 typedef struct ZipfileEntry ZipfileEntry;
6913 struct ZipfileEntry {
6914 ZipfileCDS cds; /* Parsed CDS record */
6915 u32 mUnixTime; /* Modification time, in UNIX format */
6916 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
6917 i64 iDataOff; /* Offset to data in file (if aData==0) */
6918 u8 *aData; /* cds.szCompressed bytes of compressed data */
6919 ZipfileEntry *pNext; /* Next element in in-memory CDS */
6923 ** Cursor type for zipfile tables.
6925 typedef struct ZipfileCsr ZipfileCsr;
6927 sqlite3_vtab_cursor base; /* Base class - must be first */
6928 i64 iId; /* Cursor ID */
6929 u8 bEof; /* True when at EOF */
6930 u8 bNoop; /* If next xNext() call is no-op */
6932 /* Used outside of write transactions */
6933 FILE *pFile; /* Zip file */
6934 i64 iNextOff; /* Offset of next record in central directory */
6935 ZipfileEOCD eocd; /* Parse of central directory record */
6937 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
6938 ZipfileEntry *pCurrent; /* Current entry */
6939 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
6942 typedef struct ZipfileTab ZipfileTab;
6944 sqlite3_vtab base; /* Base class - must be first */
6945 char *zFile; /* Zip file this table accesses (may be NULL) */
6946 sqlite3 *db; /* Host database connection */
6947 u8 *aBuffer; /* Temporary buffer used for various tasks */
6949 ZipfileCsr *pCsrList; /* List of cursors */
6952 /* The following are used by write transactions only */
6953 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6954 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
6955 FILE *pWriteFd; /* File handle open on zip archive */
6956 i64 szCurrent; /* Current size of zip archive */
6957 i64 szOrig; /* Size of archive at start of transaction */
6961 ** Set the error message contained in context ctx to the results of
6962 ** vprintf(zFmt, ...).
6964 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6968 zMsg = sqlite3_vmprintf(zFmt, ap);
6969 sqlite3_result_error(ctx, zMsg, -1);
6975 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
6976 ** is not quoted, do nothing.
6978 static void zipfileDequote(char *zIn){
6980 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6983 if( q=='[' ) q = ']';
6984 while( ALWAYS(zIn[iIn]) ){
6985 char c = zIn[iIn++];
6986 if( c==q && zIn[iIn++]!=q ) break;
6994 ** Construct a new ZipfileTab virtual table object.
6996 ** argv[0] -> module name ("zipfile")
6997 ** argv[1] -> database name
6998 ** argv[2] -> table name
6999 ** argv[...] -> "column name" and other module argument fields.
7001 static int zipfileConnect(
7004 int argc, const char *const*argv,
7005 sqlite3_vtab **ppVtab,
7008 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
7010 const char *zFile = 0;
7011 ZipfileTab *pNew = 0;
7014 /* If the table name is not "zipfile", require that the argument be
7015 ** specified. This stops zipfile tables from being created as:
7017 ** CREATE VIRTUAL TABLE zzz USING zipfile();
7019 ** It does not prevent:
7021 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
7023 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
7024 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
7025 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
7026 return SQLITE_ERROR;
7031 nFile = (int)strlen(zFile)+1;
7034 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
7035 if( rc==SQLITE_OK ){
7036 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
7037 if( pNew==0 ) return SQLITE_NOMEM;
7038 memset(pNew, 0, nByte+nFile);
7040 pNew->aBuffer = (u8*)&pNew[1];
7042 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
7043 memcpy(pNew->zFile, zFile, nFile);
7044 zipfileDequote(pNew->zFile);
7047 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
7048 *ppVtab = (sqlite3_vtab*)pNew;
7053 ** Free the ZipfileEntry structure indicated by the only argument.
7055 static void zipfileEntryFree(ZipfileEntry *p){
7057 sqlite3_free(p->cds.zFile);
7063 ** Release resources that should be freed at the end of a write
7066 static void zipfileCleanupTransaction(ZipfileTab *pTab){
7067 ZipfileEntry *pEntry;
7068 ZipfileEntry *pNext;
7070 if( pTab->pWriteFd ){
7071 fclose(pTab->pWriteFd);
7074 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
7075 pNext = pEntry->pNext;
7076 zipfileEntryFree(pEntry);
7078 pTab->pFirstEntry = 0;
7079 pTab->pLastEntry = 0;
7080 pTab->szCurrent = 0;
7085 ** This method is the destructor for zipfile vtab objects.
7087 static int zipfileDisconnect(sqlite3_vtab *pVtab){
7088 zipfileCleanupTransaction((ZipfileTab*)pVtab);
7089 sqlite3_free(pVtab);
7094 ** Constructor for a new ZipfileCsr object.
7096 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
7097 ZipfileTab *pTab = (ZipfileTab*)p;
7099 pCsr = sqlite3_malloc(sizeof(*pCsr));
7100 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
7102 return SQLITE_NOMEM;
7104 memset(pCsr, 0, sizeof(*pCsr));
7105 pCsr->iId = ++pTab->iNextCsrid;
7106 pCsr->pCsrNext = pTab->pCsrList;
7107 pTab->pCsrList = pCsr;
7112 ** Reset a cursor back to the state it was in when first returned
7113 ** by zipfileOpen().
7115 static void zipfileResetCursor(ZipfileCsr *pCsr){
7117 ZipfileEntry *pNext;
7121 fclose(pCsr->pFile);
7123 zipfileEntryFree(pCsr->pCurrent);
7127 for(p=pCsr->pFreeEntry; p; p=pNext){
7129 zipfileEntryFree(p);
7134 ** Destructor for an ZipfileCsr.
7136 static int zipfileClose(sqlite3_vtab_cursor *cur){
7137 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7138 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
7140 zipfileResetCursor(pCsr);
7142 /* Remove this cursor from the ZipfileTab.pCsrList list. */
7143 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
7144 *pp = pCsr->pCsrNext;
7151 ** Set the error message for the virtual table associated with cursor
7152 ** pCsr to the results of vprintf(zFmt, ...).
7154 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
7157 sqlite3_free(pTab->base.zErrMsg);
7158 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
7161 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
7164 sqlite3_free(pCsr->base.pVtab->zErrMsg);
7165 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
7170 ** Read nRead bytes of data from offset iOff of file pFile into buffer
7171 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
7174 ** If an error does occur, output variable (*pzErrmsg) may be set to point
7175 ** to an English language error message. It is the responsibility of the
7176 ** caller to eventually free this buffer using
7179 static int zipfileReadData(
7180 FILE *pFile, /* Read from this file */
7181 u8 *aRead, /* Read into this buffer */
7182 int nRead, /* Number of bytes to read */
7183 i64 iOff, /* Offset to read from */
7184 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
7187 fseek(pFile, (long)iOff, SEEK_SET);
7188 n = fread(aRead, 1, nRead, pFile);
7189 if( (int)n!=nRead ){
7190 *pzErrmsg = sqlite3_mprintf("error in fread()");
7191 return SQLITE_ERROR;
7196 static int zipfileAppendData(
7203 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
7204 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
7205 if( (int)n!=nWrite ){
7206 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
7207 return SQLITE_ERROR;
7209 pTab->szCurrent += nWrite;
7215 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
7217 static u16 zipfileGetU16(const u8 *aBuf){
7218 return (aBuf[1] << 8) + aBuf[0];
7222 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
7224 static u32 zipfileGetU32(const u8 *aBuf){
7225 return ((u32)(aBuf[3]) << 24)
7226 + ((u32)(aBuf[2]) << 16)
7227 + ((u32)(aBuf[1]) << 8)
7228 + ((u32)(aBuf[0]) << 0);
7232 ** Write a 16-bit little endiate integer into buffer aBuf.
7234 static void zipfilePutU16(u8 *aBuf, u16 val){
7235 aBuf[0] = val & 0xFF;
7236 aBuf[1] = (val>>8) & 0xFF;
7240 ** Write a 32-bit little endiate integer into buffer aBuf.
7242 static void zipfilePutU32(u8 *aBuf, u32 val){
7243 aBuf[0] = val & 0xFF;
7244 aBuf[1] = (val>>8) & 0xFF;
7245 aBuf[2] = (val>>16) & 0xFF;
7246 aBuf[3] = (val>>24) & 0xFF;
7249 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
7250 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
7252 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
7253 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
7256 ** Magic numbers used to read CDS records.
7258 #define ZIPFILE_CDS_NFILE_OFF 28
7259 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
7262 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
7263 ** if the record is not well-formed, or SQLITE_OK otherwise.
7265 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
7267 u32 sig = zipfileRead32(aRead);
7269 if( sig!=ZIPFILE_SIGNATURE_CDS ){
7272 pCDS->iVersionMadeBy = zipfileRead16(aRead);
7273 pCDS->iVersionExtract = zipfileRead16(aRead);
7274 pCDS->flags = zipfileRead16(aRead);
7275 pCDS->iCompression = zipfileRead16(aRead);
7276 pCDS->mTime = zipfileRead16(aRead);
7277 pCDS->mDate = zipfileRead16(aRead);
7278 pCDS->crc32 = zipfileRead32(aRead);
7279 pCDS->szCompressed = zipfileRead32(aRead);
7280 pCDS->szUncompressed = zipfileRead32(aRead);
7281 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7282 pCDS->nFile = zipfileRead16(aRead);
7283 pCDS->nExtra = zipfileRead16(aRead);
7284 pCDS->nComment = zipfileRead16(aRead);
7285 pCDS->iDiskStart = zipfileRead16(aRead);
7286 pCDS->iInternalAttr = zipfileRead16(aRead);
7287 pCDS->iExternalAttr = zipfileRead32(aRead);
7288 pCDS->iOffset = zipfileRead32(aRead);
7289 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
7296 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
7297 ** if the record is not well-formed, or SQLITE_OK otherwise.
7299 static int zipfileReadLFH(
7303 u8 *aRead = aBuffer;
7306 u32 sig = zipfileRead32(aRead);
7307 if( sig!=ZIPFILE_SIGNATURE_LFH ){
7310 pLFH->iVersionExtract = zipfileRead16(aRead);
7311 pLFH->flags = zipfileRead16(aRead);
7312 pLFH->iCompression = zipfileRead16(aRead);
7313 pLFH->mTime = zipfileRead16(aRead);
7314 pLFH->mDate = zipfileRead16(aRead);
7315 pLFH->crc32 = zipfileRead32(aRead);
7316 pLFH->szCompressed = zipfileRead32(aRead);
7317 pLFH->szUncompressed = zipfileRead32(aRead);
7318 pLFH->nFile = zipfileRead16(aRead);
7319 pLFH->nExtra = zipfileRead16(aRead);
7326 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
7327 ** Scan through this buffer to find an "extra-timestamp" field. If one
7328 ** exists, extract the 32-bit modification-timestamp from it and store
7329 ** the value in output parameter *pmTime.
7331 ** Zero is returned if no extra-timestamp record could be found (and so
7332 ** *pmTime is left unchanged), or non-zero otherwise.
7334 ** The general format of an extra field is:
7336 ** Header ID 2 bytes
7337 ** Data Size 2 bytes
7340 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
7343 u8 *pEnd = &aExtra[nExtra];
7346 u16 id = zipfileRead16(p);
7347 u16 nByte = zipfileRead16(p);
7350 case ZIPFILE_EXTRA_TIMESTAMP: {
7352 if( b & 0x01 ){ /* 0x01 -> modtime is present */
7353 *pmTime = zipfileGetU32(&p[1]);
7366 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
7367 ** fields of the CDS structure passed as the only argument to a 32-bit
7368 ** UNIX seconds-since-the-epoch timestamp. Return the result.
7370 ** "Standard" MS-DOS time format:
7372 ** File modification time:
7373 ** Bits 00-04: seconds divided by 2
7374 ** Bits 05-10: minute
7376 ** File modification date:
7378 ** Bits 05-08: month (1-12)
7379 ** Bits 09-15: years from 1980
7381 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
7383 static u32 zipfileMtime(ZipfileCDS *pCDS){
7384 int Y,M,D,X1,X2,A,B,sec,min,hr;
7386 Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
7387 M = ((pCDS->mDate >> 5) & 0x0F);
7388 D = (pCDS->mDate & 0x1F);
7389 sec = (pCDS->mTime & 0x1F)*2;
7390 min = (pCDS->mTime >> 5) & 0x3F;
7391 hr = (pCDS->mTime >> 11) & 0x1F;
7396 X1 = 36525*(Y+4716)/100;
7397 X2 = 306001*(M+1)/10000;
7400 JDsec = (i64)((X1 + X2 + D + B - 1524.5)*86400) + hr*3600 + min*60 + sec;
7401 return (u32)(JDsec - (i64)24405875*(i64)8640);
7405 ** The opposite of zipfileMtime(). This function populates the mTime and
7406 ** mDate fields of the CDS structure passed as the first argument according
7407 ** to the UNIX timestamp value passed as the second.
7409 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
7410 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
7411 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
7417 A = (int)((JD - 1867216.25)/36524.25);
7418 A = (int)(JD + 1 + A - (A/4));
7420 C = (int)((B - 122.1)/365.25);
7421 D = (36525*(C&32767))/100;
7422 E = (int)((B-D)/30.6001);
7424 day = B - D - (int)(30.6001*E);
7425 mon = (E<14 ? E-1 : E-13);
7426 yr = mon>2 ? C-4716 : C-4715;
7428 hr = (mUnixTime % (24*60*60)) / (60*60);
7429 min = (mUnixTime % (60*60)) / 60;
7430 sec = (mUnixTime % 60);
7433 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
7434 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
7436 pCds->mDate = pCds->mTime = 0;
7439 assert( mUnixTime<315507600
7440 || mUnixTime==zipfileMtime(pCds)
7441 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
7442 /* || (mUnixTime % 2) */
7447 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
7448 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
7449 ** then pFile is a file-handle open on a zip file. In either case, this
7450 ** function creates a ZipfileEntry object based on the zip archive entry
7451 ** for which the CDS record is at offset iOff.
7453 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
7454 ** the new object. Otherwise, an SQLite error code is returned and the
7455 ** final value of (*ppEntry) undefined.
7457 static int zipfileGetEntry(
7458 ZipfileTab *pTab, /* Store any error message here */
7459 const u8 *aBlob, /* Pointer to in-memory file image */
7460 int nBlob, /* Size of aBlob[] in bytes */
7461 FILE *pFile, /* If aBlob==0, read from this file */
7462 i64 iOff, /* Offset of CDS record */
7463 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
7466 char **pzErr = &pTab->base.zErrMsg;
7470 aRead = pTab->aBuffer;
7471 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
7473 aRead = (u8*)&aBlob[iOff];
7476 if( rc==SQLITE_OK ){
7477 sqlite3_int64 nAlloc;
7480 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
7481 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
7482 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
7484 nAlloc = sizeof(ZipfileEntry) + nExtra;
7486 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
7489 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
7493 memset(pNew, 0, sizeof(ZipfileEntry));
7494 rc = zipfileReadCDS(aRead, &pNew->cds);
7495 if( rc!=SQLITE_OK ){
7496 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
7497 }else if( aBlob==0 ){
7498 rc = zipfileReadData(
7499 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
7502 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
7506 if( rc==SQLITE_OK ){
7507 u32 *pt = &pNew->mUnixTime;
7508 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
7509 pNew->aExtra = (u8*)&pNew[1];
7510 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
7511 if( pNew->cds.zFile==0 ){
7513 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
7514 pNew->mUnixTime = zipfileMtime(&pNew->cds);
7518 if( rc==SQLITE_OK ){
7519 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
7522 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
7524 aRead = (u8*)&aBlob[pNew->cds.iOffset];
7527 rc = zipfileReadLFH(aRead, &lfh);
7528 if( rc==SQLITE_OK ){
7529 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
7530 pNew->iDataOff += lfh.nFile + lfh.nExtra;
7531 if( aBlob && pNew->cds.szCompressed ){
7532 pNew->aData = &pNew->aExtra[nExtra];
7533 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
7536 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
7537 (int)pNew->cds.iOffset
7542 if( rc!=SQLITE_OK ){
7543 zipfileEntryFree(pNew);
7553 ** Advance an ZipfileCsr to its next row of output.
7555 static int zipfileNext(sqlite3_vtab_cursor *cur){
7556 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7560 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
7561 zipfileEntryFree(pCsr->pCurrent);
7563 if( pCsr->iNextOff>=iEof ){
7566 ZipfileEntry *p = 0;
7567 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
7568 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
7569 if( rc==SQLITE_OK ){
7570 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
7571 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
7577 pCsr->pCurrent = pCsr->pCurrent->pNext;
7579 if( pCsr->pCurrent==0 ){
7588 static void zipfileFree(void *p) {
7593 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
7594 ** size is nOut bytes. This function uncompresses the data and sets the
7595 ** return value in context pCtx to the result (a blob).
7597 ** If an error occurs, an error code is left in pCtx instead.
7599 static void zipfileInflate(
7600 sqlite3_context *pCtx, /* Store result here */
7601 const u8 *aIn, /* Compressed data */
7602 int nIn, /* Size of buffer aIn[] in bytes */
7603 int nOut /* Expected output size */
7605 u8 *aRes = sqlite3_malloc(nOut);
7607 sqlite3_result_error_nomem(pCtx);
7611 memset(&str, 0, sizeof(str));
7613 str.next_in = (Byte*)aIn;
7615 str.next_out = (Byte*)aRes;
7616 str.avail_out = nOut;
7618 err = inflateInit2(&str, -15);
7620 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
7622 err = inflate(&str, Z_NO_FLUSH);
7623 if( err!=Z_STREAM_END ){
7624 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
7626 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
7636 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
7637 ** compresses it and sets (*ppOut) to point to a buffer containing the
7638 ** compressed data. The caller is responsible for eventually calling
7639 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
7640 ** is set to the size of buffer (*ppOut) in bytes.
7642 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
7643 ** code is returned and an error message left in virtual-table handle
7644 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
7647 static int zipfileDeflate(
7648 const u8 *aIn, int nIn, /* Input */
7649 u8 **ppOut, int *pnOut, /* Output */
7650 char **pzErr /* OUT: Error message */
7653 sqlite3_int64 nAlloc;
7657 memset(&str, 0, sizeof(str));
7658 str.next_in = (Bytef*)aIn;
7660 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
7662 nAlloc = deflateBound(&str, nIn);
7663 aOut = (u8*)sqlite3_malloc64(nAlloc);
7668 str.next_out = aOut;
7669 str.avail_out = nAlloc;
7670 res = deflate(&str, Z_FINISH);
7671 if( res==Z_STREAM_END ){
7673 *pnOut = (int)str.total_out;
7676 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
7687 ** Return values of columns for the row at which the series_cursor
7688 ** is currently pointing.
7690 static int zipfileColumn(
7691 sqlite3_vtab_cursor *cur, /* The cursor */
7692 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
7693 int i /* Which column to return */
7695 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7696 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
7700 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
7703 /* TODO: Whether or not the following is correct surely depends on
7704 ** the platform on which the archive was created. */
7705 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
7707 case 2: { /* mtime */
7708 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
7712 if( sqlite3_vtab_nochange(ctx)==0 ){
7713 sqlite3_result_int64(ctx, pCDS->szUncompressed);
7717 case 4: /* rawdata */
7718 if( sqlite3_vtab_nochange(ctx) ) break;
7719 case 5: { /* data */
7720 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
7721 int sz = pCDS->szCompressed;
7722 int szFinal = pCDS->szUncompressed;
7726 if( pCsr->pCurrent->aData ){
7727 aBuf = pCsr->pCurrent->aData;
7729 aBuf = aFree = sqlite3_malloc64(sz);
7733 FILE *pFile = pCsr->pFile;
7735 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
7737 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
7738 &pCsr->base.pVtab->zErrMsg
7742 if( rc==SQLITE_OK ){
7743 if( i==5 && pCDS->iCompression ){
7744 zipfileInflate(ctx, aBuf, sz, szFinal);
7746 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
7749 sqlite3_free(aFree);
7751 /* Figure out if this is a directory or a zero-sized file. Consider
7752 ** it to be a directory either if the mode suggests so, or if
7753 ** the final character in the name is '/'. */
7754 u32 mode = pCDS->iExternalAttr >> 16;
7755 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
7756 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
7762 case 6: /* method */
7763 sqlite3_result_int(ctx, pCDS->iCompression);
7767 sqlite3_result_int64(ctx, pCsr->iId);
7775 ** Return TRUE if the cursor is at EOF.
7777 static int zipfileEof(sqlite3_vtab_cursor *cur){
7778 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7783 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
7784 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
7785 ** is guaranteed to be a file-handle open on a zip file.
7787 ** This function attempts to locate the EOCD record within the zip archive
7788 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
7789 ** returned if successful. Otherwise, an SQLite error code is returned and
7790 ** an English language error message may be left in virtual-table pTab.
7792 static int zipfileReadEOCD(
7793 ZipfileTab *pTab, /* Return errors here */
7794 const u8 *aBlob, /* Pointer to in-memory file image */
7795 int nBlob, /* Size of aBlob[] in bytes */
7796 FILE *pFile, /* Read from this file if aBlob==0 */
7797 ZipfileEOCD *pEOCD /* Object to populate */
7799 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
7800 int nRead; /* Bytes to read from file */
7804 i64 iOff; /* Offset to read from */
7805 i64 szFile; /* Total size of file in bytes */
7806 fseek(pFile, 0, SEEK_END);
7807 szFile = (i64)ftell(pFile);
7809 memset(pEOCD, 0, sizeof(ZipfileEOCD));
7812 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
7813 iOff = szFile - nRead;
7814 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
7816 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
7817 aRead = (u8*)&aBlob[nBlob-nRead];
7820 if( rc==SQLITE_OK ){
7823 /* Scan backwards looking for the signature bytes */
7824 for(i=nRead-20; i>=0; i--){
7825 if( aRead[i]==0x50 && aRead[i+1]==0x4b
7826 && aRead[i+2]==0x05 && aRead[i+3]==0x06
7832 pTab->base.zErrMsg = sqlite3_mprintf(
7833 "cannot find end of central directory record"
7835 return SQLITE_ERROR;
7839 pEOCD->iDisk = zipfileRead16(aRead);
7840 pEOCD->iFirstDisk = zipfileRead16(aRead);
7841 pEOCD->nEntry = zipfileRead16(aRead);
7842 pEOCD->nEntryTotal = zipfileRead16(aRead);
7843 pEOCD->nSize = zipfileRead32(aRead);
7844 pEOCD->iOffset = zipfileRead32(aRead);
7851 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
7852 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
7853 ** to the end of the list. Otherwise, it is added to the list immediately
7854 ** before pBefore (which is guaranteed to be a part of said list).
7856 static void zipfileAddEntry(
7858 ZipfileEntry *pBefore,
7861 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
7862 assert( pNew->pNext==0 );
7864 if( pTab->pFirstEntry==0 ){
7865 pTab->pFirstEntry = pTab->pLastEntry = pNew;
7867 assert( pTab->pLastEntry->pNext==0 );
7868 pTab->pLastEntry->pNext = pNew;
7869 pTab->pLastEntry = pNew;
7873 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
7874 pNew->pNext = pBefore;
7879 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
7885 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
7886 iOff = eocd.iOffset;
7887 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
7888 ZipfileEntry *pNew = 0;
7889 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
7891 if( rc==SQLITE_OK ){
7892 zipfileAddEntry(pTab, 0, pNew);
7893 iOff += ZIPFILE_CDS_FIXED_SZ;
7894 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
7901 ** xFilter callback.
7903 static int zipfileFilter(
7904 sqlite3_vtab_cursor *cur,
7905 int idxNum, const char *idxStr,
7906 int argc, sqlite3_value **argv
7908 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7909 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7910 const char *zFile = 0; /* Zip file to scan */
7911 int rc = SQLITE_OK; /* Return Code */
7912 int bInMemory = 0; /* True for an in-memory zipfile */
7914 zipfileResetCursor(pCsr);
7917 zFile = pTab->zFile;
7918 }else if( idxNum==0 ){
7919 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7920 return SQLITE_ERROR;
7921 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7922 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7923 int nBlob = sqlite3_value_bytes(argv[0]);
7924 assert( pTab->pFirstEntry==0 );
7925 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7926 pCsr->pFreeEntry = pTab->pFirstEntry;
7927 pTab->pFirstEntry = pTab->pLastEntry = 0;
7928 if( rc!=SQLITE_OK ) return rc;
7931 zFile = (const char*)sqlite3_value_text(argv[0]);
7934 if( 0==pTab->pWriteFd && 0==bInMemory ){
7935 pCsr->pFile = fopen(zFile, "rb");
7936 if( pCsr->pFile==0 ){
7937 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7940 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7941 if( rc==SQLITE_OK ){
7942 if( pCsr->eocd.nEntry==0 ){
7945 pCsr->iNextOff = pCsr->eocd.iOffset;
7946 rc = zipfileNext(cur);
7952 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7953 rc = zipfileNext(cur);
7960 ** xBestIndex callback.
7962 static int zipfileBestIndex(
7964 sqlite3_index_info *pIdxInfo
7970 for(i=0; i<pIdxInfo->nConstraint; i++){
7971 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7972 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7973 if( pCons->usable==0 ){
7975 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7979 pIdxInfo->estimatedCost = 1000.0;
7981 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7982 pIdxInfo->aConstraintUsage[idx].omit = 1;
7983 pIdxInfo->idxNum = 1;
7984 }else if( unusable ){
7985 return SQLITE_CONSTRAINT;
7990 static ZipfileEntry *zipfileNewEntry(const char *zPath){
7992 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7994 memset(pNew, 0, sizeof(ZipfileEntry));
7995 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7996 if( pNew->cds.zFile==0 ){
8004 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
8005 ZipfileCDS *pCds = &pEntry->cds;
8010 /* Write the LFH itself */
8011 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
8012 zipfileWrite16(a, pCds->iVersionExtract);
8013 zipfileWrite16(a, pCds->flags);
8014 zipfileWrite16(a, pCds->iCompression);
8015 zipfileWrite16(a, pCds->mTime);
8016 zipfileWrite16(a, pCds->mDate);
8017 zipfileWrite32(a, pCds->crc32);
8018 zipfileWrite32(a, pCds->szCompressed);
8019 zipfileWrite32(a, pCds->szUncompressed);
8020 zipfileWrite16(a, (u16)pCds->nFile);
8021 zipfileWrite16(a, pCds->nExtra);
8022 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
8024 /* Add the file name */
8025 memcpy(a, pCds->zFile, (int)pCds->nFile);
8026 a += (int)pCds->nFile;
8028 /* The "extra" data */
8029 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8030 zipfileWrite16(a, 5);
8032 zipfileWrite32(a, pEntry->mUnixTime);
8037 static int zipfileAppendEntry(
8039 ZipfileEntry *pEntry,
8043 u8 *aBuf = pTab->aBuffer;
8047 nBuf = zipfileSerializeLFH(pEntry, aBuf);
8048 rc = zipfileAppendData(pTab, aBuf, nBuf);
8049 if( rc==SQLITE_OK ){
8050 pEntry->iDataOff = pTab->szCurrent;
8051 rc = zipfileAppendData(pTab, pData, nData);
8057 static int zipfileGetMode(
8058 sqlite3_value *pVal,
8059 int bIsDir, /* If true, default to directory */
8060 u32 *pMode, /* OUT: Mode value */
8061 char **pzErr /* OUT: Error message */
8063 const char *z = (const char*)sqlite3_value_text(pVal);
8066 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
8067 }else if( z[0]>='0' && z[0]<='9' ){
8068 mode = (unsigned int)sqlite3_value_int(pVal);
8070 const char zTemplate[11] = "-rwxrwxrwx";
8072 if( strlen(z)!=10 ) goto parse_error;
8074 case '-': mode |= S_IFREG; break;
8075 case 'd': mode |= S_IFDIR; break;
8076 case 'l': mode |= S_IFLNK; break;
8077 default: goto parse_error;
8079 for(i=1; i<10; i++){
8080 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
8081 else if( z[i]!='-' ) goto parse_error;
8084 if( ((mode & S_IFDIR)==0)==bIsDir ){
8085 /* The "mode" attribute is a directory, but data has been specified.
8086 ** Or vice-versa - no data but "mode" is a file or symlink. */
8087 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
8088 return SQLITE_CONSTRAINT;
8094 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
8095 return SQLITE_ERROR;
8099 ** Both (const char*) arguments point to nul-terminated strings. Argument
8100 ** nB is the value of strlen(zB). This function returns 0 if the strings are
8101 ** identical, ignoring any trailing '/' character in either path. */
8102 static int zipfileComparePath(const char *zA, const char *zB, int nB){
8103 int nA = (int)strlen(zA);
8104 if( nA>0 && zA[nA-1]=='/' ) nA--;
8105 if( nB>0 && zB[nB-1]=='/' ) nB--;
8106 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
8110 static int zipfileBegin(sqlite3_vtab *pVtab){
8111 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8114 assert( pTab->pWriteFd==0 );
8115 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
8116 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
8117 return SQLITE_ERROR;
8120 /* Open a write fd on the file. Also load the entire central directory
8121 ** structure into memory. During the transaction any new file data is
8122 ** appended to the archive file, but the central directory is accumulated
8123 ** in main-memory until the transaction is committed. */
8124 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
8125 if( pTab->pWriteFd==0 ){
8126 pTab->base.zErrMsg = sqlite3_mprintf(
8127 "zipfile: failed to open file %s for writing", pTab->zFile
8131 fseek(pTab->pWriteFd, 0, SEEK_END);
8132 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
8133 rc = zipfileLoadDirectory(pTab, 0, 0);
8136 if( rc!=SQLITE_OK ){
8137 zipfileCleanupTransaction(pTab);
8144 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
8147 static u32 zipfileTime(void){
8148 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
8150 if( pVfs==0 ) return 0;
8151 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
8153 pVfs->xCurrentTimeInt64(pVfs, &ms);
8154 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
8157 pVfs->xCurrentTime(pVfs, &day);
8158 ret = (u32)((day - 2440587.5) * 86400);
8164 ** Return a 32-bit timestamp in UNIX epoch format.
8166 ** If the value passed as the only argument is either NULL or an SQL NULL,
8167 ** return the current time. Otherwise, return the value stored in (*pVal)
8168 ** cast to a 32-bit unsigned integer.
8170 static u32 zipfileGetTime(sqlite3_value *pVal){
8171 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
8172 return zipfileTime();
8174 return (u32)sqlite3_value_int64(pVal);
8178 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
8179 ** linked list. Remove it from the list and free the object.
8181 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
8184 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
8186 zipfileEntryFree(pOld);
8193 static int zipfileUpdate(
8194 sqlite3_vtab *pVtab,
8196 sqlite3_value **apVal,
8197 sqlite_int64 *pRowid
8199 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8200 int rc = SQLITE_OK; /* Return Code */
8201 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
8203 u32 mode = 0; /* Mode for new entry */
8204 u32 mTime = 0; /* Modification time for new entry */
8205 i64 sz = 0; /* Uncompressed size */
8206 const char *zPath = 0; /* Path for new entry */
8207 int nPath = 0; /* strlen(zPath) */
8208 const u8 *pData = 0; /* Pointer to buffer containing content */
8209 int nData = 0; /* Size of pData buffer in bytes */
8210 int iMethod = 0; /* Compression method for new entry */
8211 u8 *pFree = 0; /* Free this */
8212 char *zFree = 0; /* Also free this */
8213 ZipfileEntry *pOld = 0;
8214 ZipfileEntry *pOld2 = 0;
8215 int bUpdate = 0; /* True for an update that modifies "name" */
8219 if( pTab->pWriteFd==0 ){
8220 rc = zipfileBegin(pVtab);
8221 if( rc!=SQLITE_OK ) return rc;
8224 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
8225 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
8226 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
8227 int nDelete = (int)strlen(zDelete);
8229 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
8230 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
8234 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
8235 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
8238 assert( pOld->pNext );
8243 /* Check that "sz" and "rawdata" are both NULL: */
8244 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
8245 zipfileTableErr(pTab, "sz must be NULL");
8246 rc = SQLITE_CONSTRAINT;
8248 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
8249 zipfileTableErr(pTab, "rawdata must be NULL");
8250 rc = SQLITE_CONSTRAINT;
8253 if( rc==SQLITE_OK ){
8254 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
8255 /* data=NULL. A directory */
8258 /* Value specified for "data", and possibly "method". This must be
8259 ** a regular file or a symlink. */
8260 const u8 *aIn = sqlite3_value_blob(apVal[7]);
8261 int nIn = sqlite3_value_bytes(apVal[7]);
8262 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
8264 iMethod = sqlite3_value_int(apVal[8]);
8268 if( iMethod!=0 && iMethod!=8 ){
8269 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
8270 rc = SQLITE_CONSTRAINT;
8272 if( bAuto || iMethod ){
8274 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
8275 if( rc==SQLITE_OK ){
8276 if( iMethod || nCmp<nIn ){
8283 iCrc32 = crc32(0, aIn, nIn);
8288 if( rc==SQLITE_OK ){
8289 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
8292 if( rc==SQLITE_OK ){
8293 zPath = (const char*)sqlite3_value_text(apVal[2]);
8294 if( zPath==0 ) zPath = "";
8295 nPath = (int)strlen(zPath);
8296 mTime = zipfileGetTime(apVal[4]);
8299 if( rc==SQLITE_OK && bIsDir ){
8300 /* For a directory, check that the last character in the path is a
8301 ** '/'. This appears to be required for compatibility with info-zip
8302 ** (the unzip command on unix). It does not create directories
8304 if( nPath<=0 || zPath[nPath-1]!='/' ){
8305 zFree = sqlite3_mprintf("%s/", zPath);
8306 zPath = (const char*)zFree;
8311 nPath = (int)strlen(zPath);
8316 /* Check that we're not inserting a duplicate entry -OR- updating an
8317 ** entry with a path, thereby making it into a duplicate. */
8318 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
8320 for(p=pTab->pFirstEntry; p; p=p->pNext){
8321 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
8322 switch( sqlite3_vtab_on_conflict(pTab->db) ){
8323 case SQLITE_IGNORE: {
8324 goto zipfile_update_done;
8326 case SQLITE_REPLACE: {
8331 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
8332 rc = SQLITE_CONSTRAINT;
8341 if( rc==SQLITE_OK ){
8342 /* Create the new CDS record. */
8343 pNew = zipfileNewEntry(zPath);
8347 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8348 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8349 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8350 pNew->cds.iCompression = (u16)iMethod;
8351 zipfileMtimeToDos(&pNew->cds, mTime);
8352 pNew->cds.crc32 = iCrc32;
8353 pNew->cds.szCompressed = nData;
8354 pNew->cds.szUncompressed = (u32)sz;
8355 pNew->cds.iExternalAttr = (mode<<16);
8356 pNew->cds.iOffset = (u32)pTab->szCurrent;
8357 pNew->cds.nFile = (u16)nPath;
8358 pNew->mUnixTime = (u32)mTime;
8359 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
8360 zipfileAddEntry(pTab, pOld, pNew);
8365 if( rc==SQLITE_OK && (pOld || pOld2) ){
8367 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8368 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
8369 pCsr->pCurrent = pCsr->pCurrent->pNext;
8374 zipfileRemoveEntryFromList(pTab, pOld);
8375 zipfileRemoveEntryFromList(pTab, pOld2);
8378 zipfile_update_done:
8379 sqlite3_free(pFree);
8380 sqlite3_free(zFree);
8384 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
8386 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
8387 zipfileWrite16(a, p->iDisk);
8388 zipfileWrite16(a, p->iFirstDisk);
8389 zipfileWrite16(a, p->nEntry);
8390 zipfileWrite16(a, p->nEntryTotal);
8391 zipfileWrite32(a, p->nSize);
8392 zipfileWrite32(a, p->iOffset);
8393 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
8398 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
8399 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
8400 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
8401 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
8405 ** Serialize the CDS structure into buffer aBuf[]. Return the number
8406 ** of bytes written.
8408 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
8410 ZipfileCDS *pCDS = &pEntry->cds;
8412 if( pEntry->aExtra==0 ){
8416 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
8417 zipfileWrite16(a, pCDS->iVersionMadeBy);
8418 zipfileWrite16(a, pCDS->iVersionExtract);
8419 zipfileWrite16(a, pCDS->flags);
8420 zipfileWrite16(a, pCDS->iCompression);
8421 zipfileWrite16(a, pCDS->mTime);
8422 zipfileWrite16(a, pCDS->mDate);
8423 zipfileWrite32(a, pCDS->crc32);
8424 zipfileWrite32(a, pCDS->szCompressed);
8425 zipfileWrite32(a, pCDS->szUncompressed);
8426 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
8427 zipfileWrite16(a, pCDS->nFile);
8428 zipfileWrite16(a, pCDS->nExtra);
8429 zipfileWrite16(a, pCDS->nComment);
8430 zipfileWrite16(a, pCDS->iDiskStart);
8431 zipfileWrite16(a, pCDS->iInternalAttr);
8432 zipfileWrite32(a, pCDS->iExternalAttr);
8433 zipfileWrite32(a, pCDS->iOffset);
8435 memcpy(a, pCDS->zFile, pCDS->nFile);
8438 if( pEntry->aExtra ){
8439 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
8440 memcpy(a, pEntry->aExtra, n);
8443 assert( pCDS->nExtra==9 );
8444 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
8445 zipfileWrite16(a, 5);
8447 zipfileWrite32(a, pEntry->mUnixTime);
8453 static int zipfileCommit(sqlite3_vtab *pVtab){
8454 ZipfileTab *pTab = (ZipfileTab*)pVtab;
8456 if( pTab->pWriteFd ){
8457 i64 iOffset = pTab->szCurrent;
8462 /* Write out all entries */
8463 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
8464 int n = zipfileSerializeCDS(p, pTab->aBuffer);
8465 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
8469 /* Write out the EOCD record */
8471 eocd.iFirstDisk = 0;
8472 eocd.nEntry = (u16)nEntry;
8473 eocd.nEntryTotal = (u16)nEntry;
8474 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
8475 eocd.iOffset = (u32)iOffset;
8476 rc = zipfileAppendEOCD(pTab, &eocd);
8478 zipfileCleanupTransaction(pTab);
8483 static int zipfileRollback(sqlite3_vtab *pVtab){
8484 return zipfileCommit(pVtab);
8487 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
8489 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
8490 if( iId==pCsr->iId ) break;
8495 static void zipfileFunctionCds(
8496 sqlite3_context *context,
8498 sqlite3_value **argv
8501 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
8504 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
8506 ZipfileCDS *p = &pCsr->pCurrent->cds;
8507 char *zRes = sqlite3_mprintf("{"
8508 "\"version-made-by\" : %u, "
8509 "\"version-to-extract\" : %u, "
8511 "\"compression\" : %u, "
8515 "\"compressed-size\" : %u, "
8516 "\"uncompressed-size\" : %u, "
8517 "\"file-name-length\" : %u, "
8518 "\"extra-field-length\" : %u, "
8519 "\"file-comment-length\" : %u, "
8520 "\"disk-number-start\" : %u, "
8521 "\"internal-attr\" : %u, "
8522 "\"external-attr\" : %u, "
8523 "\"offset\" : %u }",
8524 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
8525 (u32)p->flags, (u32)p->iCompression,
8526 (u32)p->mTime, (u32)p->mDate,
8527 (u32)p->crc32, (u32)p->szCompressed,
8528 (u32)p->szUncompressed, (u32)p->nFile,
8529 (u32)p->nExtra, (u32)p->nComment,
8530 (u32)p->iDiskStart, (u32)p->iInternalAttr,
8531 (u32)p->iExternalAttr, (u32)p->iOffset
8535 sqlite3_result_error_nomem(context);
8537 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
8544 ** xFindFunction method.
8546 static int zipfileFindFunction(
8547 sqlite3_vtab *pVtab, /* Virtual table handle */
8548 int nArg, /* Number of SQL function arguments */
8549 const char *zName, /* Name of SQL function */
8550 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
8551 void **ppArg /* OUT: User data for *pxFunc */
8553 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
8554 *pxFunc = zipfileFunctionCds;
8555 *ppArg = (void*)pVtab;
8561 typedef struct ZipfileBuffer ZipfileBuffer;
8562 struct ZipfileBuffer {
8563 u8 *a; /* Pointer to buffer */
8564 int n; /* Size of buffer in bytes */
8565 int nAlloc; /* Byte allocated at a[] */
8568 typedef struct ZipfileCtx ZipfileCtx;
8575 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
8576 if( pBuf->n+nByte>pBuf->nAlloc ){
8578 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
8579 int nReq = pBuf->n + nByte;
8581 while( nNew<nReq ) nNew = nNew*2;
8582 aNew = sqlite3_realloc64(pBuf->a, nNew);
8583 if( aNew==0 ) return SQLITE_NOMEM;
8585 pBuf->nAlloc = (int)nNew;
8591 ** xStep() callback for the zipfile() aggregate. This can be called in
8592 ** any of the following ways:
8594 ** SELECT zipfile(name,data) ...
8595 ** SELECT zipfile(name,mode,mtime,data) ...
8596 ** SELECT zipfile(name,mode,mtime,data,method) ...
8598 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
8599 ZipfileCtx *p; /* Aggregate function context */
8600 ZipfileEntry e; /* New entry to add to zip archive */
8602 sqlite3_value *pName = 0;
8603 sqlite3_value *pMode = 0;
8604 sqlite3_value *pMtime = 0;
8605 sqlite3_value *pData = 0;
8606 sqlite3_value *pMethod = 0;
8613 int iMethod = -1; /* Compression method to use (0 or 8) */
8615 const u8 *aData = 0; /* Possibly compressed data for new entry */
8616 int nData = 0; /* Size of aData[] in bytes */
8617 int szUncompressed = 0; /* Size of data before compression */
8618 u8 *aFree = 0; /* Free this before returning */
8619 u32 iCrc32 = 0; /* crc32 of uncompressed data */
8621 char *zName = 0; /* Path (name) of new entry */
8622 int nName = 0; /* Size of zName in bytes */
8623 char *zFree = 0; /* Free this before returning */
8626 memset(&e, 0, sizeof(e));
8627 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8630 /* Martial the arguments into stack variables */
8631 if( nVal!=2 && nVal!=4 && nVal!=5 ){
8632 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
8634 goto zipfile_step_out;
8648 /* Check that the 'name' parameter looks ok. */
8649 zName = (char*)sqlite3_value_text(pName);
8650 nName = sqlite3_value_bytes(pName);
8652 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
8654 goto zipfile_step_out;
8657 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
8658 ** deflate compression) or NULL (choose automatically). */
8659 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
8660 iMethod = (int)sqlite3_value_int64(pMethod);
8661 if( iMethod!=0 && iMethod!=8 ){
8662 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
8664 goto zipfile_step_out;
8668 /* Now inspect the data. If this is NULL, then the new entry must be a
8669 ** directory. Otherwise, figure out whether or not the data should
8670 ** be deflated or simply stored in the zip archive. */
8671 if( sqlite3_value_type(pData)==SQLITE_NULL ){
8675 aData = sqlite3_value_blob(pData);
8676 szUncompressed = nData = sqlite3_value_bytes(pData);
8677 iCrc32 = crc32(0, aData, nData);
8678 if( iMethod<0 || iMethod==8 ){
8680 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
8681 if( rc!=SQLITE_OK ){
8682 goto zipfile_step_out;
8684 if( iMethod==8 || nOut<nData ){
8694 /* Decode the "mode" argument. */
8695 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
8696 if( rc ) goto zipfile_step_out;
8698 /* Decode the "mtime" argument. */
8699 e.mUnixTime = zipfileGetTime(pMtime);
8701 /* If this is a directory entry, ensure that there is exactly one '/'
8702 ** at the end of the path. Or, if this is not a directory and the path
8703 ** ends in '/' it is an error. */
8705 if( nName>0 && zName[nName-1]=='/' ){
8706 zErr = sqlite3_mprintf("non-directory name must not end with /");
8708 goto zipfile_step_out;
8711 if( nName==0 || zName[nName-1]!='/' ){
8712 zName = zFree = sqlite3_mprintf("%s/", zName);
8715 goto zipfile_step_out;
8717 nName = (int)strlen(zName);
8719 while( nName>1 && zName[nName-2]=='/' ) nName--;
8723 /* Assemble the ZipfileEntry object for the new zip archive entry */
8724 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
8725 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
8726 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
8727 e.cds.iCompression = (u16)iMethod;
8728 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
8729 e.cds.crc32 = iCrc32;
8730 e.cds.szCompressed = nData;
8731 e.cds.szUncompressed = szUncompressed;
8732 e.cds.iExternalAttr = (mode<<16);
8733 e.cds.iOffset = p->body.n;
8734 e.cds.nFile = (u16)nName;
8735 e.cds.zFile = zName;
8737 /* Append the LFH to the body of the new archive */
8738 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
8739 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
8740 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
8742 /* Append the data to the body of the new archive */
8744 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
8745 memcpy(&p->body.a[p->body.n], aData, nData);
8749 /* Append the CDS record to the directory of the new archive */
8750 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
8751 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
8752 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
8754 /* Increment the count of entries in the archive */
8758 sqlite3_free(aFree);
8759 sqlite3_free(zFree);
8762 sqlite3_result_error(pCtx, zErr, -1);
8764 sqlite3_result_error_code(pCtx, rc);
8771 ** xFinalize() callback for zipfile aggregate function.
8773 void zipfileFinal(sqlite3_context *pCtx){
8779 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
8782 memset(&eocd, 0, sizeof(eocd));
8783 eocd.nEntry = (u16)p->nEntry;
8784 eocd.nEntryTotal = (u16)p->nEntry;
8785 eocd.nSize = p->cds.n;
8786 eocd.iOffset = p->body.n;
8788 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
8789 aZip = (u8*)sqlite3_malloc64(nZip);
8791 sqlite3_result_error_nomem(pCtx);
8793 memcpy(aZip, p->body.a, p->body.n);
8794 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
8795 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
8796 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
8800 sqlite3_free(p->body.a);
8801 sqlite3_free(p->cds.a);
8806 ** Register the "zipfile" virtual table.
8808 static int zipfileRegister(sqlite3 *db){
8809 static sqlite3_module zipfileModule = {
8811 zipfileConnect, /* xCreate */
8812 zipfileConnect, /* xConnect */
8813 zipfileBestIndex, /* xBestIndex */
8814 zipfileDisconnect, /* xDisconnect */
8815 zipfileDisconnect, /* xDestroy */
8816 zipfileOpen, /* xOpen - open a cursor */
8817 zipfileClose, /* xClose - close a cursor */
8818 zipfileFilter, /* xFilter - configure scan constraints */
8819 zipfileNext, /* xNext - advance a cursor */
8820 zipfileEof, /* xEof - check for end of scan */
8821 zipfileColumn, /* xColumn - read data */
8822 0, /* xRowid - read data */
8823 zipfileUpdate, /* xUpdate */
8824 zipfileBegin, /* xBegin */
8826 zipfileCommit, /* xCommit */
8827 zipfileRollback, /* xRollback */
8828 zipfileFindFunction, /* xFindMethod */
8832 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
8833 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
8834 if( rc==SQLITE_OK ){
8835 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
8836 zipfileStep, zipfileFinal
8839 assert( sizeof(i64)==8 );
8840 assert( sizeof(u32)==4 );
8841 assert( sizeof(u16)==2 );
8842 assert( sizeof(u8)==1 );
8845 #else /* SQLITE_OMIT_VIRTUALTABLE */
8846 # define zipfileRegister(x) SQLITE_OK
8852 int sqlite3_zipfile_init(
8855 const sqlite3_api_routines *pApi
8857 SQLITE_EXTENSION_INIT2(pApi);
8858 (void)pzErrMsg; /* Unused parameter */
8859 return zipfileRegister(db);
8862 /************************* End ../ext/misc/zipfile.c ********************/
8863 /************************* Begin ../ext/misc/sqlar.c ******************/
8867 ** The author disclaims copyright to this source code. In place of
8868 ** a legal notice, here is a blessing:
8870 ** May you do good and not evil.
8871 ** May you find forgiveness for yourself and forgive others.
8872 ** May you share freely, never taking more than you give.
8874 ******************************************************************************
8876 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
8877 ** for working with sqlar archives and used by the shell tool's built-in
8880 /* #include "sqlite3ext.h" */
8881 SQLITE_EXTENSION_INIT1
8886 ** Implementation of the "sqlar_compress(X)" SQL function.
8888 ** If the type of X is SQLITE_BLOB, and compressing that blob using
8889 ** zlib utility function compress() yields a smaller blob, return the
8890 ** compressed blob. Otherwise, return a copy of X.
8892 ** SQLar uses the "zlib format" for compressed content. The zlib format
8893 ** contains a two-byte identification header and a four-byte checksum at
8894 ** the end. This is different from ZIP which uses the raw deflate format.
8896 ** Future enhancements to SQLar might add support for new compression formats.
8897 ** If so, those new formats will be identified by alternative headers in the
8900 static void sqlarCompressFunc(
8901 sqlite3_context *context,
8903 sqlite3_value **argv
8906 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
8907 const Bytef *pData = sqlite3_value_blob(argv[0]);
8908 uLong nData = sqlite3_value_bytes(argv[0]);
8909 uLongf nOut = compressBound(nData);
8912 pOut = (Bytef*)sqlite3_malloc(nOut);
8914 sqlite3_result_error_nomem(context);
8917 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8918 sqlite3_result_error(context, "error in compress()", -1);
8919 }else if( nOut<nData ){
8920 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8922 sqlite3_result_value(context, argv[0]);
8927 sqlite3_result_value(context, argv[0]);
8932 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8934 ** Parameter SZ is interpreted as an integer. If it is less than or
8935 ** equal to zero, then this function returns a copy of X. Or, if
8936 ** SZ is equal to the size of X when interpreted as a blob, also
8937 ** return a copy of X. Otherwise, decompress blob X using zlib
8938 ** utility function uncompress() and return the results (another
8941 static void sqlarUncompressFunc(
8942 sqlite3_context *context,
8944 sqlite3_value **argv
8950 sz = sqlite3_value_int(argv[1]);
8952 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8953 sqlite3_result_value(context, argv[0]);
8955 const Bytef *pData= sqlite3_value_blob(argv[0]);
8956 Bytef *pOut = sqlite3_malloc(sz);
8957 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8958 sqlite3_result_error(context, "error in uncompress()", -1);
8960 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8970 int sqlite3_sqlar_init(
8973 const sqlite3_api_routines *pApi
8976 SQLITE_EXTENSION_INIT2(pApi);
8977 (void)pzErrMsg; /* Unused parameter */
8978 rc = sqlite3_create_function(db, "sqlar_compress", 1,
8979 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8980 sqlarCompressFunc, 0, 0);
8981 if( rc==SQLITE_OK ){
8982 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8983 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8984 sqlarUncompressFunc, 0, 0);
8989 /************************* End ../ext/misc/sqlar.c ********************/
8991 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
8995 ** The author disclaims copyright to this source code. In place of
8996 ** a legal notice, here is a blessing:
8998 ** May you do good and not evil.
8999 ** May you find forgiveness for yourself and forgive others.
9000 ** May you share freely, never taking more than you give.
9002 *************************************************************************
9004 #if !defined(SQLITEEXPERT_H)
9005 #define SQLITEEXPERT_H 1
9006 /* #include "sqlite3.h" */
9008 typedef struct sqlite3expert sqlite3expert;
9011 ** Create a new sqlite3expert object.
9013 ** If successful, a pointer to the new object is returned and (*pzErr) set
9014 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
9015 ** an English-language error message. In this case it is the responsibility
9016 ** of the caller to eventually free the error message buffer using
9019 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
9022 ** Configure an sqlite3expert object.
9024 ** EXPERT_CONFIG_SAMPLE:
9025 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
9026 ** each candidate index. This involves scanning and sorting the entire
9027 ** contents of each user database table once for each candidate index
9028 ** associated with the table. For large databases, this can be
9029 ** prohibitively slow. This option allows the sqlite3expert object to
9030 ** be configured so that sqlite_stat1 data is instead generated based on a
9031 ** subset of each table, or so that no sqlite_stat1 data is used at all.
9033 ** A single integer argument is passed to this option. If the value is less
9034 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
9035 ** the analysis - indexes are recommended based on the database schema only.
9036 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
9037 ** generated for each candidate index (this is the default). Finally, if the
9038 ** value falls between 0 and 100, then it represents the percentage of user
9039 ** table rows that should be considered when generating sqlite_stat1 data.
9043 ** // Do not generate any sqlite_stat1 data
9044 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
9046 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
9047 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
9049 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
9051 #define EXPERT_CONFIG_SAMPLE 1 /* int */
9054 ** Specify zero or more SQL statements to be included in the analysis.
9056 ** Buffer zSql must contain zero or more complete SQL statements. This
9057 ** function parses all statements contained in the buffer and adds them
9058 ** to the internal list of statements to analyze. If successful, SQLITE_OK
9059 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
9060 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
9061 ** may be set to point to an English language error message. In this case
9062 ** the caller is responsible for eventually freeing the error message buffer
9063 ** using sqlite3_free().
9065 ** If an error does occur while processing one of the statements in the
9066 ** buffer passed as the second argument, none of the statements in the
9067 ** buffer are added to the analysis.
9069 ** This function must be called before sqlite3_expert_analyze(). If a call
9070 ** to this function is made on an sqlite3expert object that has already
9071 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
9072 ** immediately and no statements are added to the analysis.
9074 int sqlite3_expert_sql(
9075 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
9076 const char *zSql, /* SQL statement(s) to add */
9077 char **pzErr /* OUT: Error message (if any) */
9082 ** This function is called after the sqlite3expert object has been configured
9083 ** with all SQL statements using sqlite3_expert_sql() to actually perform
9084 ** the analysis. Once this function has been called, it is not possible to
9085 ** add further SQL statements to the analysis.
9087 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
9088 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
9089 ** point to a buffer containing an English language error message. In this
9090 ** case it is the responsibility of the caller to eventually free the buffer
9091 ** using sqlite3_free().
9093 ** If an error does occur within this function, the sqlite3expert object
9094 ** is no longer useful for any purpose. At that point it is no longer
9095 ** possible to add further SQL statements to the object or to re-attempt
9096 ** the analysis. The sqlite3expert object must still be freed using a call
9097 ** sqlite3_expert_destroy().
9099 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
9102 ** Return the total number of statements loaded using sqlite3_expert_sql().
9103 ** The total number of SQL statements may be different from the total number
9104 ** to calls to sqlite3_expert_sql().
9106 int sqlite3_expert_count(sqlite3expert*);
9109 ** Return a component of the report.
9111 ** This function is called after sqlite3_expert_analyze() to extract the
9112 ** results of the analysis. Each call to this function returns either a
9113 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
9114 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
9115 ** #define constants defined below.
9117 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
9118 ** information relating to a specific SQL statement. In these cases that
9119 ** SQL statement is identified by the value passed as the second argument.
9120 ** SQL statements are numbered from 0 in the order in which they are parsed.
9121 ** If an out-of-range value (less than zero or equal to or greater than the
9122 ** value returned by sqlite3_expert_count()) is passed as the second argument
9123 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
9125 ** EXPERT_REPORT_SQL:
9126 ** Return the text of SQL statement iStmt.
9128 ** EXPERT_REPORT_INDEXES:
9129 ** Return a buffer containing the CREATE INDEX statements for all recommended
9130 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
9133 ** EXPERT_REPORT_PLAN:
9134 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
9135 ** iStmt after the proposed indexes have been added to the database schema.
9137 ** EXPERT_REPORT_CANDIDATES:
9138 ** Return a pointer to a buffer containing the CREATE INDEX statements
9139 ** for all indexes that were tested (for all SQL statements). The iStmt
9140 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
9142 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
9145 ** Values for the third argument passed to sqlite3_expert_report().
9147 #define EXPERT_REPORT_SQL 1
9148 #define EXPERT_REPORT_INDEXES 2
9149 #define EXPERT_REPORT_PLAN 3
9150 #define EXPERT_REPORT_CANDIDATES 4
9153 ** Free an (sqlite3expert*) handle and all associated resources. There
9154 ** should be one call to this function for each successful call to
9155 ** sqlite3-expert_new().
9157 void sqlite3_expert_destroy(sqlite3expert*);
9159 #endif /* !defined(SQLITEEXPERT_H) */
9161 /************************* End ../ext/expert/sqlite3expert.h ********************/
9162 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
9166 ** The author disclaims copyright to this source code. In place of
9167 ** a legal notice, here is a blessing:
9169 ** May you do good and not evil.
9170 ** May you find forgiveness for yourself and forgive others.
9171 ** May you share freely, never taking more than you give.
9173 *************************************************************************
9175 /* #include "sqlite3expert.h" */
9180 #ifndef SQLITE_OMIT_VIRTUALTABLE
9182 /* typedef sqlite3_int64 i64; */
9183 /* typedef sqlite3_uint64 u64; */
9185 typedef struct IdxColumn IdxColumn;
9186 typedef struct IdxConstraint IdxConstraint;
9187 typedef struct IdxScan IdxScan;
9188 typedef struct IdxStatement IdxStatement;
9189 typedef struct IdxTable IdxTable;
9190 typedef struct IdxWrite IdxWrite;
9192 #define STRLEN (int)strlen
9195 ** A temp table name that we assume no user database will actually use.
9196 ** If this assumption proves incorrect triggers on the table with the
9197 ** conflicting name will be ignored.
9199 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
9202 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
9203 ** any other type of single-ended range constraint on a column).
9206 ** Used to temporarily link IdxConstraint objects into lists while
9207 ** creating candidate indexes.
9209 struct IdxConstraint {
9210 char *zColl; /* Collation sequence */
9211 int bRange; /* True for range, false for eq */
9212 int iCol; /* Constrained table column */
9213 int bFlag; /* Used by idxFindCompatible() */
9214 int bDesc; /* True if ORDER BY <expr> DESC */
9215 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
9216 IdxConstraint *pLink; /* See above */
9220 ** A single scan of a single table.
9223 IdxTable *pTab; /* Associated table object */
9224 int iDb; /* Database containing table zTable */
9225 i64 covering; /* Mask of columns required for cov. index */
9226 IdxConstraint *pOrder; /* ORDER BY columns */
9227 IdxConstraint *pEq; /* List of == constraints */
9228 IdxConstraint *pRange; /* List of < constraints */
9229 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
9233 ** Information regarding a single database table. Extracted from
9234 ** "PRAGMA table_info" by function idxGetTableInfo().
9243 char *zName; /* Table name */
9245 IdxTable *pNext; /* Next table in linked list of all tables */
9249 ** An object of the following type is created for each unique table/write-op
9250 ** seen. The objects are stored in a singly-linked list beginning at
9251 ** sqlite3expert.pWrite.
9255 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
9260 ** Each statement being analyzed is represented by an instance of this
9263 struct IdxStatement {
9264 int iId; /* Statement number */
9265 char *zSql; /* SQL statement */
9266 char *zIdx; /* Indexes */
9267 char *zEQP; /* Plan */
9268 IdxStatement *pNext;
9273 ** A hash table for storing strings. With space for a payload string
9274 ** with each entry. Methods are:
9281 #define IDX_HASH_SIZE 1023
9282 typedef struct IdxHashEntry IdxHashEntry;
9283 typedef struct IdxHash IdxHash;
9284 struct IdxHashEntry {
9285 char *zKey; /* nul-terminated key */
9286 char *zVal; /* nul-terminated value string */
9287 char *zVal2; /* nul-terminated value string 2 */
9288 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
9289 IdxHashEntry *pNext; /* Next entry in hash */
9292 IdxHashEntry *pFirst;
9293 IdxHashEntry *aHash[IDX_HASH_SIZE];
9297 ** sqlite3expert object.
9299 struct sqlite3expert {
9300 int iSample; /* Percentage of tables to sample for stat1 */
9301 sqlite3 *db; /* User database */
9302 sqlite3 *dbm; /* In-memory db for this analysis */
9303 sqlite3 *dbv; /* Vtab schema for this analysis */
9304 IdxTable *pTable; /* List of all IdxTable objects */
9305 IdxScan *pScan; /* List of scan objects */
9306 IdxWrite *pWrite; /* List of write objects */
9307 IdxStatement *pStatement; /* List of IdxStatement objects */
9308 int bRun; /* True once analysis has run */
9310 int rc; /* Error code from whereinfo hook */
9311 IdxHash hIdx; /* Hash containing all candidate indexes */
9312 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
9317 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
9318 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
9320 static void *idxMalloc(int *pRc, int nByte){
9322 assert( *pRc==SQLITE_OK );
9324 pRet = sqlite3_malloc(nByte);
9326 memset(pRet, 0, nByte);
9328 *pRc = SQLITE_NOMEM;
9334 ** Initialize an IdxHash hash table.
9336 static void idxHashInit(IdxHash *pHash){
9337 memset(pHash, 0, sizeof(IdxHash));
9341 ** Reset an IdxHash hash table.
9343 static void idxHashClear(IdxHash *pHash){
9345 for(i=0; i<IDX_HASH_SIZE; i++){
9346 IdxHashEntry *pEntry;
9347 IdxHashEntry *pNext;
9348 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
9349 pNext = pEntry->pHashNext;
9350 sqlite3_free(pEntry->zVal2);
9351 sqlite3_free(pEntry);
9354 memset(pHash, 0, sizeof(IdxHash));
9358 ** Return the index of the hash bucket that the string specified by the
9359 ** arguments to this function belongs.
9361 static int idxHashString(const char *z, int n){
9362 unsigned int ret = 0;
9365 ret += (ret<<3) + (unsigned char)(z[i]);
9367 return (int)(ret % IDX_HASH_SIZE);
9371 ** If zKey is already present in the hash table, return non-zero and do
9372 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
9373 ** the hash table passed as the second argument.
9375 static int idxHashAdd(
9381 int nKey = STRLEN(zKey);
9382 int iHash = idxHashString(zKey, nKey);
9383 int nVal = (zVal ? STRLEN(zVal) : 0);
9384 IdxHashEntry *pEntry;
9386 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9387 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9391 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
9393 pEntry->zKey = (char*)&pEntry[1];
9394 memcpy(pEntry->zKey, zKey, nKey);
9396 pEntry->zVal = &pEntry->zKey[nKey+1];
9397 memcpy(pEntry->zVal, zVal, nVal);
9399 pEntry->pHashNext = pHash->aHash[iHash];
9400 pHash->aHash[iHash] = pEntry;
9402 pEntry->pNext = pHash->pFirst;
9403 pHash->pFirst = pEntry;
9409 ** If zKey/nKey is present in the hash table, return a pointer to the
9410 ** hash-entry object.
9412 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
9414 IdxHashEntry *pEntry;
9415 if( nKey<0 ) nKey = STRLEN(zKey);
9416 iHash = idxHashString(zKey, nKey);
9418 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
9419 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
9427 ** If the hash table contains an entry with a key equal to the string
9428 ** passed as the final two arguments to this function, return a pointer
9429 ** to the payload string. Otherwise, if zKey/nKey is not present in the
9430 ** hash table, return NULL.
9432 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
9433 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
9434 if( pEntry ) return pEntry->zVal;
9439 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
9440 ** variable to point to a copy of nul-terminated string zColl.
9442 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
9443 IdxConstraint *pNew;
9444 int nColl = STRLEN(zColl);
9446 assert( *pRc==SQLITE_OK );
9447 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
9449 pNew->zColl = (char*)&pNew[1];
9450 memcpy(pNew->zColl, zColl, nColl+1);
9456 ** An error associated with database handle db has just occurred. Pass
9457 ** the error message to callback function xOut.
9459 static void idxDatabaseError(
9460 sqlite3 *db, /* Database handle */
9461 char **pzErrmsg /* Write error here */
9463 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
9467 ** Prepare an SQL statement.
9469 static int idxPrepareStmt(
9470 sqlite3 *db, /* Database handle to compile against */
9471 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
9472 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
9473 const char *zSql /* SQL statement to compile */
9475 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
9476 if( rc!=SQLITE_OK ){
9478 idxDatabaseError(db, pzErrmsg);
9484 ** Prepare an SQL statement using the results of a printf() formatting.
9486 static int idxPrintfPrepareStmt(
9487 sqlite3 *db, /* Database handle to compile against */
9488 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
9489 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
9490 const char *zFmt, /* printf() format of SQL statement */
9491 ... /* Trailing printf() arguments */
9497 zSql = sqlite3_vmprintf(zFmt, ap);
9501 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
9509 /*************************************************************************
9510 ** Beginning of virtual table implementation.
9512 typedef struct ExpertVtab ExpertVtab;
9516 sqlite3expert *pExpert;
9519 typedef struct ExpertCsr ExpertCsr;
9521 sqlite3_vtab_cursor base;
9522 sqlite3_stmt *pData;
9525 static char *expertDequote(const char *zIn){
9526 int n = STRLEN(zIn);
9527 char *zRet = sqlite3_malloc(n);
9529 assert( zIn[0]=='\'' );
9530 assert( zIn[n-1]=='\'' );
9535 for(iIn=1; iIn<(n-1); iIn++){
9536 if( zIn[iIn]=='\'' ){
9537 assert( zIn[iIn+1]=='\'' );
9540 zRet[iOut++] = zIn[iIn];
9549 ** This function is the implementation of both the xConnect and xCreate
9550 ** methods of the r-tree virtual table.
9552 ** argv[0] -> module name
9553 ** argv[1] -> database name
9554 ** argv[2] -> table name
9555 ** argv[...] -> column names...
9557 static int expertConnect(
9560 int argc, const char *const*argv,
9561 sqlite3_vtab **ppVtab,
9564 sqlite3expert *pExpert = (sqlite3expert*)pAux;
9569 *pzErr = sqlite3_mprintf("internal error!");
9572 char *zCreateTable = expertDequote(argv[3]);
9574 rc = sqlite3_declare_vtab(db, zCreateTable);
9575 if( rc==SQLITE_OK ){
9576 p = idxMalloc(&rc, sizeof(ExpertVtab));
9578 if( rc==SQLITE_OK ){
9579 p->pExpert = pExpert;
9580 p->pTab = pExpert->pTable;
9581 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
9583 sqlite3_free(zCreateTable);
9589 *ppVtab = (sqlite3_vtab*)p;
9593 static int expertDisconnect(sqlite3_vtab *pVtab){
9594 ExpertVtab *p = (ExpertVtab*)pVtab;
9599 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
9600 ExpertVtab *p = (ExpertVtab*)pVtab;
9605 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
9606 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
9607 SQLITE_INDEX_CONSTRAINT_LE;
9609 pScan = idxMalloc(&rc, sizeof(IdxScan));
9613 /* Link the new scan object into the list */
9614 pScan->pTab = p->pTab;
9615 pScan->pNextScan = p->pExpert->pScan;
9616 p->pExpert->pScan = pScan;
9618 /* Add the constraints to the IdxScan object */
9619 for(i=0; i<pIdxInfo->nConstraint; i++){
9620 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
9622 && pCons->iColumn>=0
9623 && p->pTab->aCol[pCons->iColumn].iPk==0
9624 && (pCons->op & opmask)
9626 IdxConstraint *pNew;
9627 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
9628 pNew = idxNewConstraint(&rc, zColl);
9630 pNew->iCol = pCons->iColumn;
9631 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9632 pNew->pNext = pScan->pEq;
9636 pNew->pNext = pScan->pRange;
9637 pScan->pRange = pNew;
9641 pIdxInfo->aConstraintUsage[i].argvIndex = n;
9645 /* Add the ORDER BY to the IdxScan object */
9646 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
9647 int iCol = pIdxInfo->aOrderBy[i].iColumn;
9649 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
9652 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
9653 pNew->pNext = pScan->pOrder;
9654 pNew->pLink = pScan->pOrder;
9655 pScan->pOrder = pNew;
9662 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
9666 static int expertUpdate(
9667 sqlite3_vtab *pVtab,
9669 sqlite3_value **azData,
9670 sqlite_int64 *pRowid
9680 ** Virtual table module xOpen method.
9682 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9686 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
9687 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
9692 ** Virtual table module xClose method.
9694 static int expertClose(sqlite3_vtab_cursor *cur){
9695 ExpertCsr *pCsr = (ExpertCsr*)cur;
9696 sqlite3_finalize(pCsr->pData);
9702 ** Virtual table module xEof method.
9704 ** Return non-zero if the cursor does not currently point to a valid
9705 ** record (i.e if the scan has finished), or zero otherwise.
9707 static int expertEof(sqlite3_vtab_cursor *cur){
9708 ExpertCsr *pCsr = (ExpertCsr*)cur;
9709 return pCsr->pData==0;
9713 ** Virtual table module xNext method.
9715 static int expertNext(sqlite3_vtab_cursor *cur){
9716 ExpertCsr *pCsr = (ExpertCsr*)cur;
9719 assert( pCsr->pData );
9720 rc = sqlite3_step(pCsr->pData);
9721 if( rc!=SQLITE_ROW ){
9722 rc = sqlite3_finalize(pCsr->pData);
9732 ** Virtual table module xRowid method.
9734 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
9741 ** Virtual table module xColumn method.
9743 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
9744 ExpertCsr *pCsr = (ExpertCsr*)cur;
9745 sqlite3_value *pVal;
9746 pVal = sqlite3_column_value(pCsr->pData, i);
9748 sqlite3_result_value(ctx, pVal);
9754 ** Virtual table module xFilter method.
9756 static int expertFilter(
9757 sqlite3_vtab_cursor *cur,
9758 int idxNum, const char *idxStr,
9759 int argc, sqlite3_value **argv
9761 ExpertCsr *pCsr = (ExpertCsr*)cur;
9762 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
9763 sqlite3expert *pExpert = pVtab->pExpert;
9770 rc = sqlite3_finalize(pCsr->pData);
9772 if( rc==SQLITE_OK ){
9773 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
9774 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
9778 if( rc==SQLITE_OK ){
9779 rc = expertNext(cur);
9784 static int idxRegisterVtab(sqlite3expert *p){
9785 static sqlite3_module expertModule = {
9787 expertConnect, /* xCreate - create a table */
9788 expertConnect, /* xConnect - connect to an existing table */
9789 expertBestIndex, /* xBestIndex - Determine search strategy */
9790 expertDisconnect, /* xDisconnect - Disconnect from a table */
9791 expertDisconnect, /* xDestroy - Drop a table */
9792 expertOpen, /* xOpen - open a cursor */
9793 expertClose, /* xClose - close a cursor */
9794 expertFilter, /* xFilter - configure scan constraints */
9795 expertNext, /* xNext - advance a cursor */
9796 expertEof, /* xEof */
9797 expertColumn, /* xColumn - read data */
9798 expertRowid, /* xRowid - read data */
9799 expertUpdate, /* xUpdate - write data */
9800 0, /* xBegin - begin transaction */
9801 0, /* xSync - sync transaction */
9802 0, /* xCommit - commit transaction */
9803 0, /* xRollback - rollback transaction */
9804 0, /* xFindFunction - function overloading */
9805 0, /* xRename - rename the table */
9808 0, /* xRollbackTo */
9809 0, /* xShadowName */
9812 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
9815 ** End of virtual table implementation.
9816 *************************************************************************/
9818 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
9819 ** is called, set it to the return value of sqlite3_finalize() before
9820 ** returning. Otherwise, discard the sqlite3_finalize() return value.
9822 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
9823 int rc = sqlite3_finalize(pStmt);
9824 if( *pRc==SQLITE_OK ) *pRc = rc;
9828 ** Attempt to allocate an IdxTable structure corresponding to table zTab
9829 ** in the main database of connection db. If successful, set (*ppOut) to
9830 ** point to the new object and return SQLITE_OK. Otherwise, return an
9831 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
9832 ** set to point to an error string.
9834 ** It is the responsibility of the caller to eventually free either the
9835 ** IdxTable object or error message using sqlite3_free().
9837 static int idxGetTableInfo(
9838 sqlite3 *db, /* Database connection to read details from */
9839 const char *zTab, /* Table name */
9840 IdxTable **ppOut, /* OUT: New object (if successful) */
9841 char **pzErrmsg /* OUT: Error message (if not) */
9843 sqlite3_stmt *p1 = 0;
9845 int nTab = STRLEN(zTab);
9846 int nByte = sizeof(IdxTable) + nTab + 1;
9852 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_xinfo=%Q", zTab);
9853 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9854 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9855 nByte += 1 + STRLEN(zCol);
9856 rc = sqlite3_table_column_metadata(
9857 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9859 nByte += 1 + STRLEN(zCol);
9861 nPk += (sqlite3_column_int(p1, 5)>0);
9863 rc2 = sqlite3_reset(p1);
9864 if( rc==SQLITE_OK ) rc = rc2;
9866 nByte += sizeof(IdxColumn) * nCol;
9867 if( rc==SQLITE_OK ){
9868 pNew = idxMalloc(&rc, nByte);
9870 if( rc==SQLITE_OK ){
9871 pNew->aCol = (IdxColumn*)&pNew[1];
9873 pCsr = (char*)&pNew->aCol[nCol];
9877 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
9878 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
9879 int nCopy = STRLEN(zCol) + 1;
9880 pNew->aCol[nCol].zName = pCsr;
9881 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
9882 memcpy(pCsr, zCol, nCopy);
9885 rc = sqlite3_table_column_metadata(
9886 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
9888 if( rc==SQLITE_OK ){
9889 nCopy = STRLEN(zCol) + 1;
9890 pNew->aCol[nCol].zColl = pCsr;
9891 memcpy(pCsr, zCol, nCopy);
9897 idxFinalize(&rc, p1);
9899 if( rc!=SQLITE_OK ){
9904 memcpy(pNew->zName, zTab, nTab+1);
9912 ** This function is a no-op if *pRc is set to anything other than
9913 ** SQLITE_OK when it is called.
9915 ** If *pRc is initially set to SQLITE_OK, then the text specified by
9916 ** the printf() style arguments is appended to zIn and the result returned
9917 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9918 ** zIn before returning.
9920 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9924 int nIn = zIn ? STRLEN(zIn) : 0;
9927 if( *pRc==SQLITE_OK ){
9928 zAppend = sqlite3_vmprintf(zFmt, ap);
9930 nAppend = STRLEN(zAppend);
9931 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9933 if( zAppend && zRet ){
9934 if( nIn ) memcpy(zRet, zIn, nIn);
9935 memcpy(&zRet[nIn], zAppend, nAppend+1);
9939 *pRc = SQLITE_NOMEM;
9941 sqlite3_free(zAppend);
9949 ** Return true if zId must be quoted in order to use it as an SQL
9950 ** identifier, or false otherwise.
9952 static int idxIdentifierRequiresQuotes(const char *zId){
9954 for(i=0; zId[i]; i++){
9956 && !(zId[i]>='0' && zId[i]<='9')
9957 && !(zId[i]>='a' && zId[i]<='z')
9958 && !(zId[i]>='A' && zId[i]<='Z')
9967 ** This function appends an index column definition suitable for constraint
9968 ** pCons to the string passed as zIn and returns the result.
9970 static char *idxAppendColDefn(
9971 int *pRc, /* IN/OUT: Error code */
9972 char *zIn, /* Column defn accumulated so far */
9973 IdxTable *pTab, /* Table index will be created on */
9974 IdxConstraint *pCons
9977 IdxColumn *p = &pTab->aCol[pCons->iCol];
9978 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9980 if( idxIdentifierRequiresQuotes(p->zName) ){
9981 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9983 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9986 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9987 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9988 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9990 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9995 zRet = idxAppendText(pRc, zRet, " DESC");
10001 ** Search database dbm for an index compatible with the one idxCreateFromCons()
10002 ** would create from arguments pScan, pEq and pTail. If no error occurs and
10003 ** such an index is found, return non-zero. Or, if no such index is found,
10006 ** If an error occurs, set *pRc to an SQLite error code and return zero.
10008 static int idxFindCompatible(
10009 int *pRc, /* OUT: Error code */
10010 sqlite3* dbm, /* Database to search */
10011 IdxScan *pScan, /* Scan for table to search for index on */
10012 IdxConstraint *pEq, /* List of == constraints */
10013 IdxConstraint *pTail /* List of range constraints */
10015 const char *zTbl = pScan->pTab->zName;
10016 sqlite3_stmt *pIdxList = 0;
10017 IdxConstraint *pIter;
10018 int nEq = 0; /* Number of elements in pEq */
10021 /* Count the elements in list pEq */
10022 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
10024 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
10025 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
10027 IdxConstraint *pT = pTail;
10028 sqlite3_stmt *pInfo = 0;
10029 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
10031 /* Zero the IdxConstraint.bFlag values in the pEq list */
10032 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
10034 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
10035 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
10036 int iIdx = sqlite3_column_int(pInfo, 0);
10037 int iCol = sqlite3_column_int(pInfo, 1);
10038 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
10041 for(pIter=pEq; pIter; pIter=pIter->pLink){
10042 if( pIter->bFlag ) continue;
10043 if( pIter->iCol!=iCol ) continue;
10044 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
10054 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
10062 idxFinalize(&rc, pInfo);
10064 if( rc==SQLITE_OK && bMatch ){
10065 sqlite3_finalize(pIdxList);
10069 idxFinalize(&rc, pIdxList);
10075 static int idxCreateFromCons(
10078 IdxConstraint *pEq,
10079 IdxConstraint *pTail
10081 sqlite3 *dbm = p->dbm;
10082 int rc = SQLITE_OK;
10083 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
10084 IdxTable *pTab = pScan->pTab;
10087 IdxConstraint *pCons;
10088 unsigned int h = 0;
10091 for(pCons=pEq; pCons; pCons=pCons->pLink){
10092 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10094 for(pCons=pTail; pCons; pCons=pCons->pLink){
10095 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
10098 if( rc==SQLITE_OK ){
10099 /* Hash the list of columns to come up with a name for the index */
10100 const char *zTable = pScan->pTab->zName;
10101 char *zName; /* Index name */
10103 for(i=0; zCols[i]; i++){
10104 h += ((h<<3) + zCols[i]);
10106 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
10110 if( idxIdentifierRequiresQuotes(zTable) ){
10111 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
10113 zFmt = "CREATE INDEX %s ON %s(%s)";
10115 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
10119 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
10120 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
10122 sqlite3_free(zName);
10123 sqlite3_free(zIdx);
10127 sqlite3_free(zCols);
10133 ** Return true if list pList (linked by IdxConstraint.pLink) contains
10134 ** a constraint compatible with *p. Otherwise return false.
10136 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
10137 IdxConstraint *pCmp;
10138 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
10139 if( p->iCol==pCmp->iCol ) return 1;
10144 static int idxCreateFromWhere(
10146 IdxScan *pScan, /* Create indexes for this scan */
10147 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
10149 IdxConstraint *p1 = 0;
10150 IdxConstraint *pCon;
10153 /* Gather up all the == constraints. */
10154 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
10155 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10161 /* Create an index using the == constraints collected above. And the
10162 ** range constraint/ORDER BY terms passed in by the caller, if any. */
10163 rc = idxCreateFromCons(p, pScan, p1, pTail);
10165 /* If no range/ORDER BY passed by the caller, create a version of the
10166 ** index for each range constraint. */
10168 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
10169 assert( pCon->pLink==0 );
10170 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
10171 rc = idxCreateFromCons(p, pScan, p1, pCon);
10180 ** Create candidate indexes in database [dbm] based on the data in
10181 ** linked-list pScan.
10183 static int idxCreateCandidates(sqlite3expert *p){
10184 int rc = SQLITE_OK;
10187 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
10188 rc = idxCreateFromWhere(p, pIter, 0);
10189 if( rc==SQLITE_OK && pIter->pOrder ){
10190 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
10198 ** Free all elements of the linked list starting at pConstraint.
10200 static void idxConstraintFree(IdxConstraint *pConstraint){
10201 IdxConstraint *pNext;
10204 for(p=pConstraint; p; p=pNext){
10211 ** Free all elements of the linked list starting from pScan up until pLast
10212 ** (pLast is not freed).
10214 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
10217 for(p=pScan; p!=pLast; p=pNext){
10218 pNext = p->pNextScan;
10219 idxConstraintFree(p->pOrder);
10220 idxConstraintFree(p->pEq);
10221 idxConstraintFree(p->pRange);
10227 ** Free all elements of the linked list starting from pStatement up
10228 ** until pLast (pLast is not freed).
10230 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
10232 IdxStatement *pNext;
10233 for(p=pStatement; p!=pLast; p=pNext){
10235 sqlite3_free(p->zEQP);
10236 sqlite3_free(p->zIdx);
10242 ** Free the linked list of IdxTable objects starting at pTab.
10244 static void idxTableFree(IdxTable *pTab){
10247 for(pIter=pTab; pIter; pIter=pNext){
10248 pNext = pIter->pNext;
10249 sqlite3_free(pIter);
10254 ** Free the linked list of IdxWrite objects starting at pTab.
10256 static void idxWriteFree(IdxWrite *pTab){
10259 for(pIter=pTab; pIter; pIter=pNext){
10260 pNext = pIter->pNext;
10261 sqlite3_free(pIter);
10268 ** This function is called after candidate indexes have been created. It
10269 ** runs all the queries to see which indexes they prefer, and populates
10270 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
10272 int idxFindIndexes(
10274 char **pzErr /* OUT: Error message (sqlite3_malloc) */
10276 IdxStatement *pStmt;
10277 sqlite3 *dbm = p->dbm;
10278 int rc = SQLITE_OK;
10281 idxHashInit(&hIdx);
10283 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
10284 IdxHashEntry *pEntry;
10285 sqlite3_stmt *pExplain = 0;
10286 idxHashClear(&hIdx);
10287 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
10288 "EXPLAIN QUERY PLAN %s", pStmt->zSql
10290 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
10291 /* int iId = sqlite3_column_int(pExplain, 0); */
10292 /* int iParent = sqlite3_column_int(pExplain, 1); */
10293 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
10294 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
10298 if( !zDetail ) continue;
10299 nDetail = STRLEN(zDetail);
10301 for(i=0; i<nDetail; i++){
10302 const char *zIdx = 0;
10303 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
10304 zIdx = &zDetail[i+13];
10305 }else if( i+22<nDetail
10306 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
10308 zIdx = &zDetail[i+22];
10313 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
10316 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
10318 idxHashAdd(&rc, &hIdx, zSql, 0);
10319 if( rc ) goto find_indexes_out;
10325 if( zDetail[0]!='-' ){
10326 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
10330 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10331 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
10334 idxFinalize(&rc, pExplain);
10338 idxHashClear(&hIdx);
10342 static int idxAuthCallback(
10348 const char *zTrigger
10350 int rc = SQLITE_OK;
10353 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
10354 if( sqlite3_stricmp(zDb, "main")==0 ){
10355 sqlite3expert *p = (sqlite3expert*)pCtx;
10357 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
10358 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
10362 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
10363 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
10366 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
10367 if( rc==SQLITE_OK ){
10368 pWrite->pTab = pTab;
10370 pWrite->pNext = p->pWrite;
10371 p->pWrite = pWrite;
10380 static int idxProcessOneTrigger(
10385 static const char *zInt = UNIQUE_TABLE_NAME;
10386 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
10387 IdxTable *pTab = pWrite->pTab;
10388 const char *zTab = pTab->zName;
10390 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
10391 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
10393 sqlite3_stmt *pSelect = 0;
10394 int rc = SQLITE_OK;
10397 /* Create the table and its triggers in the temp schema */
10398 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
10399 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
10400 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
10401 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
10403 idxFinalize(&rc, pSelect);
10405 /* Rename the table in the temp schema to zInt */
10406 if( rc==SQLITE_OK ){
10407 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
10411 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
10416 switch( pWrite->eOp ){
10417 case SQLITE_INSERT: {
10419 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
10420 for(i=0; i<pTab->nCol; i++){
10421 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
10423 zWrite = idxAppendText(&rc, zWrite, ")");
10426 case SQLITE_UPDATE: {
10428 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
10429 for(i=0; i<pTab->nCol; i++){
10430 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
10431 pTab->aCol[i].zName
10437 assert( pWrite->eOp==SQLITE_DELETE );
10438 if( rc==SQLITE_OK ){
10439 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
10440 if( zWrite==0 ) rc = SQLITE_NOMEM;
10445 if( rc==SQLITE_OK ){
10446 sqlite3_stmt *pX = 0;
10447 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
10448 idxFinalize(&rc, pX);
10449 if( rc!=SQLITE_OK ){
10450 idxDatabaseError(p->dbv, pzErr);
10453 sqlite3_free(zWrite);
10455 if( rc==SQLITE_OK ){
10456 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
10462 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
10463 int rc = SQLITE_OK;
10464 IdxWrite *pEnd = 0;
10465 IdxWrite *pFirst = p->pWrite;
10467 while( rc==SQLITE_OK && pFirst!=pEnd ){
10469 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
10470 rc = idxProcessOneTrigger(p, pIter, pzErr);
10473 pFirst = p->pWrite;
10480 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
10481 int rc = idxRegisterVtab(p);
10482 sqlite3_stmt *pSchema = 0;
10484 /* For each table in the main db schema:
10486 ** 1) Add an entry to the p->pTable list, and
10487 ** 2) Create the equivalent virtual table in dbv.
10489 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
10490 "SELECT type, name, sql, 1 FROM sqlite_schema "
10491 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
10493 "SELECT type, name, sql, 2 FROM sqlite_schema "
10494 "WHERE type = 'trigger'"
10495 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
10498 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
10499 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
10500 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
10501 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
10503 if( zType[0]=='v' || zType[1]=='r' ){
10504 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
10507 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
10508 if( rc==SQLITE_OK ){
10512 pTab->pNext = p->pTable;
10515 /* The statement the vtab will pass to sqlite3_declare_vtab() */
10516 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
10517 for(i=0; i<pTab->nCol; i++){
10518 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
10519 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
10522 zInner = idxAppendText(&rc, zInner, ")");
10524 /* The CVT statement to create the vtab */
10525 zOuter = idxAppendText(&rc, 0,
10526 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
10528 if( rc==SQLITE_OK ){
10529 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
10531 sqlite3_free(zInner);
10532 sqlite3_free(zOuter);
10536 idxFinalize(&rc, pSchema);
10540 struct IdxSampleCtx {
10542 double target; /* Target nRet/nRow value */
10543 double nRow; /* Number of rows seen */
10544 double nRet; /* Number of rows returned */
10547 static void idxSampleFunc(
10548 sqlite3_context *pCtx,
10550 sqlite3_value **argv
10552 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
10557 if( p->nRow==0.0 ){
10560 bRet = (p->nRet / p->nRow) <= p->target;
10562 unsigned short rnd;
10563 sqlite3_randomness(2, (void*)&rnd);
10564 bRet = ((int)rnd % 100) <= p->iTarget;
10568 sqlite3_result_int(pCtx, bRet);
10570 p->nRet += (double)bRet;
10575 struct IdxRemSlot {
10576 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
10577 i64 iVal; /* SQLITE_INTEGER value */
10578 double rVal; /* SQLITE_FLOAT value */
10579 int nByte; /* Bytes of space allocated at z */
10580 int n; /* Size of buffer z */
10581 char *z; /* SQLITE_TEXT/BLOB value */
10586 ** Implementation of scalar function rem().
10588 static void idxRemFunc(
10589 sqlite3_context *pCtx,
10591 sqlite3_value **argv
10593 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
10594 struct IdxRemSlot *pSlot;
10598 iSlot = sqlite3_value_int(argv[0]);
10599 assert( iSlot<=p->nSlot );
10600 pSlot = &p->aSlot[iSlot];
10602 switch( pSlot->eType ){
10607 case SQLITE_INTEGER:
10608 sqlite3_result_int64(pCtx, pSlot->iVal);
10612 sqlite3_result_double(pCtx, pSlot->rVal);
10616 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10620 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
10624 pSlot->eType = sqlite3_value_type(argv[1]);
10625 switch( pSlot->eType ){
10630 case SQLITE_INTEGER:
10631 pSlot->iVal = sqlite3_value_int64(argv[1]);
10635 pSlot->rVal = sqlite3_value_double(argv[1]);
10639 case SQLITE_TEXT: {
10640 int nByte = sqlite3_value_bytes(argv[1]);
10641 if( nByte>pSlot->nByte ){
10642 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
10644 sqlite3_result_error_nomem(pCtx);
10647 pSlot->nByte = nByte*2;
10651 if( pSlot->eType==SQLITE_BLOB ){
10652 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
10654 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
10661 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
10662 int rc = SQLITE_OK;
10664 "SELECT max(i.seqno) FROM "
10665 " sqlite_schema AS s, "
10666 " pragma_index_list(s.name) AS l, "
10667 " pragma_index_info(l.name) AS i "
10668 "WHERE s.type = 'table'";
10669 sqlite3_stmt *pMax = 0;
10672 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
10673 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
10674 *pnMax = sqlite3_column_int(pMax, 0) + 1;
10676 idxFinalize(&rc, pMax);
10681 static int idxPopulateOneStat1(
10683 sqlite3_stmt *pIndexXInfo,
10684 sqlite3_stmt *pWriteStat,
10694 sqlite3_stmt *pQuery = 0;
10696 int rc = SQLITE_OK;
10698 assert( p->iSample>0 );
10700 /* Formulate the query text */
10701 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
10702 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
10703 const char *zComma = zCols==0 ? "" : ", ";
10704 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
10705 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
10706 zCols = idxAppendText(&rc, zCols,
10707 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
10709 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
10711 sqlite3_reset(pIndexXInfo);
10712 if( rc==SQLITE_OK ){
10713 if( p->iSample==100 ){
10714 zQuery = sqlite3_mprintf(
10715 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
10718 zQuery = sqlite3_mprintf(
10719 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
10723 sqlite3_free(zCols);
10724 sqlite3_free(zOrder);
10726 /* Formulate the query text */
10727 if( rc==SQLITE_OK ){
10728 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10729 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
10731 sqlite3_free(zQuery);
10733 if( rc==SQLITE_OK ){
10734 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
10736 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10737 IdxHashEntry *pEntry;
10739 for(i=0; i<=nCol; i++) aStat[i] = 1;
10740 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
10742 for(i=0; i<nCol; i++){
10743 if( sqlite3_column_int(pQuery, i)==0 ) break;
10745 for(/*no-op*/; i<nCol; i++){
10750 if( rc==SQLITE_OK ){
10752 zStat = sqlite3_mprintf("%d", s0);
10753 if( zStat==0 ) rc = SQLITE_NOMEM;
10754 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
10755 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
10759 if( rc==SQLITE_OK ){
10760 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
10761 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
10762 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
10763 sqlite3_step(pWriteStat);
10764 rc = sqlite3_reset(pWriteStat);
10767 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
10769 assert( pEntry->zVal2==0 );
10770 pEntry->zVal2 = zStat;
10772 sqlite3_free(zStat);
10775 sqlite3_free(aStat);
10776 idxFinalize(&rc, pQuery);
10781 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
10785 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10786 if( rc!=SQLITE_OK ) return rc;
10788 zSql = sqlite3_mprintf(
10789 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
10791 if( zSql==0 ) return SQLITE_NOMEM;
10792 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
10793 sqlite3_free(zSql);
10799 ** This function is called as part of sqlite3_expert_analyze(). Candidate
10800 ** indexes have already been created in database sqlite3expert.dbm, this
10801 ** function populates sqlite_stat1 table in the same database.
10803 ** The stat1 data is generated by querying the
10805 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
10806 int rc = SQLITE_OK;
10808 struct IdxRemCtx *pCtx = 0;
10809 struct IdxSampleCtx samplectx;
10811 i64 iPrev = -100000;
10812 sqlite3_stmt *pAllIndex = 0;
10813 sqlite3_stmt *pIndexXInfo = 0;
10814 sqlite3_stmt *pWrite = 0;
10816 const char *zAllIndex =
10817 "SELECT s.rowid, s.name, l.name FROM "
10818 " sqlite_schema AS s, "
10819 " pragma_index_list(s.name) AS l "
10820 "WHERE s.type = 'table'";
10821 const char *zIndexXInfo =
10822 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
10823 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
10825 /* If iSample==0, no sqlite_stat1 data is required. */
10826 if( p->iSample==0 ) return SQLITE_OK;
10828 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
10829 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
10831 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
10833 if( rc==SQLITE_OK ){
10834 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
10835 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
10838 if( rc==SQLITE_OK ){
10839 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
10840 rc = sqlite3_create_function(
10841 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
10844 if( rc==SQLITE_OK ){
10845 rc = sqlite3_create_function(
10846 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
10850 if( rc==SQLITE_OK ){
10851 pCtx->nSlot = nMax+1;
10852 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
10854 if( rc==SQLITE_OK ){
10855 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
10857 if( rc==SQLITE_OK ){
10858 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
10861 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
10862 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
10863 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
10864 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
10865 if( p->iSample<100 && iPrev!=iRowid ){
10866 samplectx.target = (double)p->iSample / 100.0;
10867 samplectx.iTarget = p->iSample;
10868 samplectx.nRow = 0.0;
10869 samplectx.nRet = 0.0;
10870 rc = idxBuildSampleTable(p, zTab);
10871 if( rc!=SQLITE_OK ) break;
10873 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
10876 if( rc==SQLITE_OK && p->iSample<100 ){
10877 rc = sqlite3_exec(p->dbv,
10878 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
10882 idxFinalize(&rc, pAllIndex);
10883 idxFinalize(&rc, pIndexXInfo);
10884 idxFinalize(&rc, pWrite);
10887 for(i=0; i<pCtx->nSlot; i++){
10888 sqlite3_free(pCtx->aSlot[i].z);
10890 sqlite3_free(pCtx);
10893 if( rc==SQLITE_OK ){
10894 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
10897 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
10902 ** Allocate a new sqlite3expert object.
10904 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
10905 int rc = SQLITE_OK;
10906 sqlite3expert *pNew;
10908 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
10910 /* Open two in-memory databases to work with. The "vtab database" (dbv)
10911 ** will contain a virtual table corresponding to each real table in
10912 ** the user database schema, and a copy of each view. It is used to
10913 ** collect information regarding the WHERE, ORDER BY and other clauses
10914 ** of the user's query.
10916 if( rc==SQLITE_OK ){
10918 pNew->iSample = 100;
10919 rc = sqlite3_open(":memory:", &pNew->dbv);
10921 if( rc==SQLITE_OK ){
10922 rc = sqlite3_open(":memory:", &pNew->dbm);
10923 if( rc==SQLITE_OK ){
10924 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10929 /* Copy the entire schema of database [db] into [dbm]. */
10930 if( rc==SQLITE_OK ){
10931 sqlite3_stmt *pSql;
10932 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10933 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10934 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10936 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10937 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10938 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10940 idxFinalize(&rc, pSql);
10943 /* Create the vtab schema */
10944 if( rc==SQLITE_OK ){
10945 rc = idxCreateVtabSchema(pNew, pzErrmsg);
10948 /* Register the auth callback with dbv */
10949 if( rc==SQLITE_OK ){
10950 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10953 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10954 ** return the new sqlite3expert handle. */
10955 if( rc!=SQLITE_OK ){
10956 sqlite3_expert_destroy(pNew);
10963 ** Configure an sqlite3expert object.
10965 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10966 int rc = SQLITE_OK;
10970 case EXPERT_CONFIG_SAMPLE: {
10971 int iVal = va_arg(ap, int);
10972 if( iVal<0 ) iVal = 0;
10973 if( iVal>100 ) iVal = 100;
10978 rc = SQLITE_NOTFOUND;
10987 ** Add an SQL statement to the analysis.
10989 int sqlite3_expert_sql(
10990 sqlite3expert *p, /* From sqlite3_expert_new() */
10991 const char *zSql, /* SQL statement to add */
10992 char **pzErr /* OUT: Error message (if any) */
10994 IdxScan *pScanOrig = p->pScan;
10995 IdxStatement *pStmtOrig = p->pStatement;
10996 int rc = SQLITE_OK;
10997 const char *zStmt = zSql;
10999 if( p->bRun ) return SQLITE_MISUSE;
11001 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
11002 sqlite3_stmt *pStmt = 0;
11003 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
11004 if( rc==SQLITE_OK ){
11006 IdxStatement *pNew;
11007 const char *z = sqlite3_sql(pStmt);
11009 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
11010 if( rc==SQLITE_OK ){
11011 pNew->zSql = (char*)&pNew[1];
11012 memcpy(pNew->zSql, z, n+1);
11013 pNew->pNext = p->pStatement;
11014 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
11015 p->pStatement = pNew;
11017 sqlite3_finalize(pStmt);
11020 idxDatabaseError(p->dbv, pzErr);
11024 if( rc!=SQLITE_OK ){
11025 idxScanFree(p->pScan, pScanOrig);
11026 idxStatementFree(p->pStatement, pStmtOrig);
11027 p->pScan = pScanOrig;
11028 p->pStatement = pStmtOrig;
11034 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
11036 IdxHashEntry *pEntry;
11038 /* Do trigger processing to collect any extra IdxScan structures */
11039 rc = idxProcessTriggers(p, pzErr);
11041 /* Create candidate indexes within the in-memory database file */
11042 if( rc==SQLITE_OK ){
11043 rc = idxCreateCandidates(p);
11046 /* Generate the stat1 data */
11047 if( rc==SQLITE_OK ){
11048 rc = idxPopulateStat1(p, pzErr);
11051 /* Formulate the EXPERT_REPORT_CANDIDATES text */
11052 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
11053 p->zCandidates = idxAppendText(&rc, p->zCandidates,
11054 "%s;%s%s\n", pEntry->zVal,
11055 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
11059 /* Figure out which of the candidate indexes are preferred by the query
11060 ** planner and report the results to the user. */
11061 if( rc==SQLITE_OK ){
11062 rc = idxFindIndexes(p, pzErr);
11065 if( rc==SQLITE_OK ){
11072 ** Return the total number of statements that have been added to this
11073 ** sqlite3expert using sqlite3_expert_sql().
11075 int sqlite3_expert_count(sqlite3expert *p){
11077 if( p->pStatement ) nRet = p->pStatement->iId+1;
11082 ** Return a component of the report.
11084 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
11085 const char *zRet = 0;
11086 IdxStatement *pStmt;
11088 if( p->bRun==0 ) return 0;
11089 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
11091 case EXPERT_REPORT_SQL:
11092 if( pStmt ) zRet = pStmt->zSql;
11094 case EXPERT_REPORT_INDEXES:
11095 if( pStmt ) zRet = pStmt->zIdx;
11097 case EXPERT_REPORT_PLAN:
11098 if( pStmt ) zRet = pStmt->zEQP;
11100 case EXPERT_REPORT_CANDIDATES:
11101 zRet = p->zCandidates;
11108 ** Free an sqlite3expert object.
11110 void sqlite3_expert_destroy(sqlite3expert *p){
11112 sqlite3_close(p->dbm);
11113 sqlite3_close(p->dbv);
11114 idxScanFree(p->pScan, 0);
11115 idxStatementFree(p->pStatement, 0);
11116 idxTableFree(p->pTable);
11117 idxWriteFree(p->pWrite);
11118 idxHashClear(&p->hIdx);
11119 sqlite3_free(p->zCandidates);
11124 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11126 /************************* End ../ext/expert/sqlite3expert.c ********************/
11128 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
11129 /************************* Begin ../ext/misc/dbdata.c ******************/
11133 ** The author disclaims copyright to this source code. In place of
11134 ** a legal notice, here is a blessing:
11136 ** May you do good and not evil.
11137 ** May you find forgiveness for yourself and forgive others.
11138 ** May you share freely, never taking more than you give.
11140 ******************************************************************************
11142 ** This file contains an implementation of two eponymous virtual tables,
11143 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
11144 ** "sqlite_dbpage" eponymous virtual table be available.
11147 ** sqlite_dbdata is used to extract data directly from a database b-tree
11148 ** page and its associated overflow pages, bypassing the b-tree layer.
11149 ** The table schema is equivalent to:
11151 ** CREATE TABLE sqlite_dbdata(
11156 ** schema TEXT HIDDEN
11159 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
11160 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
11163 ** Each page of the database is inspected. If it cannot be interpreted as
11164 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
11165 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
11166 ** table contains one row for each field in the record associated with
11167 ** each cell on the page. For intkey b-trees, the key value is stored in
11170 ** For example, for the database:
11172 ** CREATE TABLE t1(a, b); -- root page is page 2
11173 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
11174 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
11176 ** the sqlite_dbdata table contains, as well as from entries related to
11177 ** page 1, content equivalent to:
11179 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
11181 ** (2, 0, 0, 'v' ),
11182 ** (2, 0, 1, 'five'),
11183 ** (2, 1, -1, 10 ),
11184 ** (2, 1, 0, 'x' ),
11185 ** (2, 1, 1, 'ten' );
11187 ** If database corruption is encountered, this module does not report an
11188 ** error. Instead, it attempts to extract as much data as possible and
11189 ** ignores the corruption.
11192 ** The sqlite_dbptr table has the following schema:
11194 ** CREATE TABLE sqlite_dbptr(
11197 ** schema TEXT HIDDEN
11200 ** It contains one entry for each b-tree pointer between a parent and
11201 ** child page in the database.
11203 #if !defined(SQLITEINT_H)
11204 /* #include "sqlite3ext.h" */
11206 /* typedef unsigned char u8; */
11209 SQLITE_EXTENSION_INIT1
11210 #include <string.h>
11211 #include <assert.h>
11213 #define DBDATA_PADDING_BYTES 100
11215 typedef struct DbdataTable DbdataTable;
11216 typedef struct DbdataCursor DbdataCursor;
11218 /* Cursor object */
11219 struct DbdataCursor {
11220 sqlite3_vtab_cursor base; /* Base class. Must be first */
11221 sqlite3_stmt *pStmt; /* For fetching database pages */
11223 int iPgno; /* Current page number */
11224 u8 *aPage; /* Buffer containing page */
11225 int nPage; /* Size of aPage[] in bytes */
11226 int nCell; /* Number of cells on aPage[] */
11227 int iCell; /* Current cell number */
11228 int bOnePage; /* True to stop after one page */
11230 sqlite3_int64 iRowid;
11232 /* Only for the sqlite_dbdata table */
11233 u8 *pRec; /* Buffer containing current record */
11234 int nRec; /* Size of pRec[] in bytes */
11235 int nHdr; /* Size of header in bytes */
11236 int iField; /* Current field number */
11240 sqlite3_int64 iIntkey; /* Integer key value */
11244 struct DbdataTable {
11245 sqlite3_vtab base; /* Base class. Must be first */
11246 sqlite3 *db; /* The database connection */
11247 sqlite3_stmt *pStmt; /* For fetching database pages */
11248 int bPtr; /* True for sqlite3_dbptr table */
11251 /* Column and schema definitions for sqlite_dbdata */
11252 #define DBDATA_COLUMN_PGNO 0
11253 #define DBDATA_COLUMN_CELL 1
11254 #define DBDATA_COLUMN_FIELD 2
11255 #define DBDATA_COLUMN_VALUE 3
11256 #define DBDATA_COLUMN_SCHEMA 4
11257 #define DBDATA_SCHEMA \
11258 "CREATE TABLE x(" \
11261 " field INTEGER," \
11263 " schema TEXT HIDDEN" \
11266 /* Column and schema definitions for sqlite_dbptr */
11267 #define DBPTR_COLUMN_PGNO 0
11268 #define DBPTR_COLUMN_CHILD 1
11269 #define DBPTR_COLUMN_SCHEMA 2
11270 #define DBPTR_SCHEMA \
11271 "CREATE TABLE x(" \
11273 " child INTEGER," \
11274 " schema TEXT HIDDEN" \
11278 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
11281 static int dbdataConnect(
11284 int argc, const char *const*argv,
11285 sqlite3_vtab **ppVtab,
11288 DbdataTable *pTab = 0;
11289 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
11291 if( rc==SQLITE_OK ){
11292 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
11296 memset(pTab, 0, sizeof(DbdataTable));
11298 pTab->bPtr = (pAux!=0);
11302 *ppVtab = (sqlite3_vtab*)pTab;
11307 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
11309 static int dbdataDisconnect(sqlite3_vtab *pVtab){
11310 DbdataTable *pTab = (DbdataTable*)pVtab;
11312 sqlite3_finalize(pTab->pStmt);
11313 sqlite3_free(pVtab);
11319 ** This function interprets two types of constraints:
11324 ** If neither are present, idxNum is set to 0. If schema=? is present,
11325 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
11326 ** in idxNum is set.
11328 ** If both parameters are present, schema is in position 0 and pgno in
11331 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
11332 DbdataTable *pTab = (DbdataTable*)tab;
11336 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
11338 for(i=0; i<pIdx->nConstraint; i++){
11339 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
11340 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
11341 if( p->iColumn==colSchema ){
11342 if( p->usable==0 ) return SQLITE_CONSTRAINT;
11345 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
11352 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
11353 pIdx->aConstraintUsage[iSchema].omit = 1;
11356 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
11357 pIdx->aConstraintUsage[iPgno].omit = 1;
11358 pIdx->estimatedCost = 100;
11359 pIdx->estimatedRows = 50;
11361 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
11362 int iCol = pIdx->aOrderBy[0].iColumn;
11363 if( pIdx->nOrderBy==1 ){
11364 pIdx->orderByConsumed = (iCol==0 || iCol==1);
11365 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
11366 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
11371 pIdx->estimatedCost = 100000000;
11372 pIdx->estimatedRows = 1000000000;
11374 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
11379 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
11381 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
11382 DbdataCursor *pCsr;
11384 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
11386 return SQLITE_NOMEM;
11388 memset(pCsr, 0, sizeof(DbdataCursor));
11389 pCsr->base.pVtab = pVTab;
11392 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
11397 ** Restore a cursor object to the state it was in when first allocated
11398 ** by dbdataOpen().
11400 static void dbdataResetCursor(DbdataCursor *pCsr){
11401 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
11402 if( pTab->pStmt==0 ){
11403 pTab->pStmt = pCsr->pStmt;
11405 sqlite3_finalize(pCsr->pStmt);
11411 pCsr->bOnePage = 0;
11412 sqlite3_free(pCsr->aPage);
11413 sqlite3_free(pCsr->pRec);
11419 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
11421 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
11422 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11423 dbdataResetCursor(pCsr);
11424 sqlite3_free(pCsr);
11429 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
11431 static unsigned int get_uint16(unsigned char *a){
11432 return (a[0]<<8)|a[1];
11434 static unsigned int get_uint32(unsigned char *a){
11435 return ((unsigned int)a[0]<<24)
11436 | ((unsigned int)a[1]<<16)
11437 | ((unsigned int)a[2]<<8)
11438 | ((unsigned int)a[3]);
11442 ** Load page pgno from the database via the sqlite_dbpage virtual table.
11443 ** If successful, set (*ppPage) to point to a buffer containing the page
11444 ** data, (*pnPage) to the size of that buffer in bytes and return
11445 ** SQLITE_OK. In this case it is the responsibility of the caller to
11446 ** eventually free the buffer using sqlite3_free().
11448 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
11449 ** return an SQLite error code.
11451 static int dbdataLoadPage(
11452 DbdataCursor *pCsr, /* Cursor object */
11453 unsigned int pgno, /* Page number of page to load */
11454 u8 **ppPage, /* OUT: pointer to page buffer */
11455 int *pnPage /* OUT: Size of (*ppPage) in bytes */
11458 int rc = SQLITE_OK;
11459 sqlite3_stmt *pStmt = pCsr->pStmt;
11463 sqlite3_bind_int64(pStmt, 2, pgno);
11464 if( SQLITE_ROW==sqlite3_step(pStmt) ){
11465 int nCopy = sqlite3_column_bytes(pStmt, 0);
11468 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
11472 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
11473 memcpy(pPage, pCopy, nCopy);
11474 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
11480 rc2 = sqlite3_reset(pStmt);
11481 if( rc==SQLITE_OK ) rc = rc2;
11487 ** Read a varint. Put the value in *pVal and return the number of bytes.
11489 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
11490 sqlite3_int64 v = 0;
11492 for(i=0; i<8; i++){
11493 v = (v<<7) + (z[i]&0x7f);
11494 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
11496 v = (v<<8) + (z[i]&0xff);
11502 ** Return the number of bytes of space used by an SQLite value of type
11505 static int dbdataValueBytes(int eType){
11507 case 0: case 8: case 9:
11525 return ((eType-12) / 2);
11532 ** Load a value of type eType from buffer pData and use it to set the
11533 ** result of context object pCtx.
11535 static void dbdataValue(
11536 sqlite3_context *pCtx,
11541 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
11546 sqlite3_result_null(pCtx);
11550 sqlite3_result_int(pCtx, 0);
11553 sqlite3_result_int(pCtx, 1);
11556 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
11557 sqlite3_uint64 v = (signed char)pData[0];
11561 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
11562 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
11563 case 4: v = (v<<8) + pData[0]; pData++;
11564 case 3: v = (v<<8) + pData[0]; pData++;
11565 case 2: v = (v<<8) + pData[0]; pData++;
11570 memcpy(&r, &v, sizeof(r));
11571 sqlite3_result_double(pCtx, r);
11573 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
11579 int n = ((eType-12) / 2);
11581 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
11583 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
11591 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
11593 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
11594 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11595 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11600 int iOff = (pCsr->iPgno==1 ? 100 : 0);
11603 if( pCsr->aPage==0 ){
11605 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
11606 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
11607 if( rc!=SQLITE_OK ) return rc;
11608 if( pCsr->aPage ) break;
11611 pCsr->iCell = pTab->bPtr ? -2 : 0;
11612 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
11616 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
11617 pCsr->iCell = pCsr->nCell;
11620 if( pCsr->iCell>=pCsr->nCell ){
11621 sqlite3_free(pCsr->aPage);
11623 if( pCsr->bOnePage ) return SQLITE_OK;
11629 /* If there is no record loaded, load it now. */
11630 if( pCsr->pRec==0 ){
11633 sqlite3_int64 nPayload = 0;
11634 sqlite3_int64 nHdr = 0;
11639 switch( pCsr->aPage[iOff] ){
11649 /* This is not a b-tree page with records on it. Continue. */
11650 pCsr->iCell = pCsr->nCell;
11654 if( pCsr->iCell>=pCsr->nCell ){
11658 iOff += 8 + nPointer + pCsr->iCell*2;
11659 if( iOff>pCsr->nPage ){
11662 iOff = get_uint16(&pCsr->aPage[iOff]);
11665 /* For an interior node cell, skip past the child-page number */
11668 /* Load the "byte of payload including overflow" field */
11669 if( bNextPage || iOff>pCsr->nPage ){
11672 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
11675 /* If this is a leaf intkey cell, load the rowid */
11676 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
11677 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
11680 /* Figure out how much data to read from the local page */
11685 X = ((U-12)*64/255)-23;
11691 M = ((U-12)*32/255)-23;
11692 K = M+((nPayload-M)%(U-4));
11700 if( bNextPage || nLocal+iOff>pCsr->nPage ){
11704 /* Allocate space for payload. And a bit more to catch small buffer
11705 ** overruns caused by attempting to read a varint or similar from
11706 ** near the end of a corrupt record. */
11707 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
11708 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
11709 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
11710 pCsr->nRec = nPayload;
11712 /* Load the nLocal bytes of payload */
11713 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
11716 /* Load content from overflow pages */
11717 if( nPayload>nLocal ){
11718 sqlite3_int64 nRem = nPayload - nLocal;
11719 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
11724 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
11725 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
11726 if( rc!=SQLITE_OK ) return rc;
11727 if( aOvfl==0 ) break;
11730 if( nCopy>nRem ) nCopy = nRem;
11731 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
11734 pgnoOvfl = get_uint32(aOvfl);
11735 sqlite3_free(aOvfl);
11739 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
11741 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
11742 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
11743 pCsr->iField = (bHasRowid ? -1 : 0);
11748 if( pCsr->iField>0 ){
11749 sqlite3_int64 iType;
11750 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
11753 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
11754 pCsr->pPtr += dbdataValueBytes(iType);
11760 sqlite3_free(pCsr->aPage);
11761 sqlite3_free(pCsr->pRec);
11764 if( pCsr->bOnePage ) return SQLITE_OK;
11767 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
11771 /* Advance to the next cell. The next iteration of the loop will load
11772 ** the record and so on. */
11773 sqlite3_free(pCsr->pRec);
11780 assert( !"can't get here" );
11785 ** Return true if the cursor is at EOF.
11787 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
11788 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11789 return pCsr->aPage==0;
11793 ** Determine the size in pages of database zSchema (where zSchema is
11794 ** "main", "temp" or the name of an attached database) and set
11795 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
11796 ** an SQLite error code.
11798 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
11799 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
11802 sqlite3_stmt *pStmt = 0;
11804 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
11805 if( zSql==0 ) return SQLITE_NOMEM;
11806 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
11807 sqlite3_free(zSql);
11808 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
11809 pCsr->szDb = sqlite3_column_int(pStmt, 0);
11811 rc2 = sqlite3_finalize(pStmt);
11812 if( rc==SQLITE_OK ) rc = rc2;
11817 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
11819 static int dbdataFilter(
11820 sqlite3_vtab_cursor *pCursor,
11821 int idxNum, const char *idxStr,
11822 int argc, sqlite3_value **argv
11824 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11825 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11826 int rc = SQLITE_OK;
11827 const char *zSchema = "main";
11829 dbdataResetCursor(pCsr);
11830 assert( pCsr->iPgno==1 );
11831 if( idxNum & 0x01 ){
11832 zSchema = (const char*)sqlite3_value_text(argv[0]);
11834 if( idxNum & 0x02 ){
11835 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
11836 pCsr->bOnePage = 1;
11838 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
11839 rc = dbdataDbsize(pCsr, zSchema);
11842 if( rc==SQLITE_OK ){
11844 pCsr->pStmt = pTab->pStmt;
11847 rc = sqlite3_prepare_v2(pTab->db,
11848 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
11853 if( rc==SQLITE_OK ){
11854 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
11856 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
11858 if( rc==SQLITE_OK ){
11859 rc = dbdataNext(pCursor);
11865 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
11867 static int dbdataColumn(
11868 sqlite3_vtab_cursor *pCursor,
11869 sqlite3_context *ctx,
11872 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11873 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
11876 case DBPTR_COLUMN_PGNO:
11877 sqlite3_result_int64(ctx, pCsr->iPgno);
11879 case DBPTR_COLUMN_CHILD: {
11880 int iOff = pCsr->iPgno==1 ? 100 : 0;
11881 if( pCsr->iCell<0 ){
11884 iOff += 12 + pCsr->iCell*2;
11885 if( iOff>pCsr->nPage ) return SQLITE_OK;
11886 iOff = get_uint16(&pCsr->aPage[iOff]);
11888 if( iOff<=pCsr->nPage ){
11889 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
11896 case DBDATA_COLUMN_PGNO:
11897 sqlite3_result_int64(ctx, pCsr->iPgno);
11899 case DBDATA_COLUMN_CELL:
11900 sqlite3_result_int(ctx, pCsr->iCell);
11902 case DBDATA_COLUMN_FIELD:
11903 sqlite3_result_int(ctx, pCsr->iField);
11905 case DBDATA_COLUMN_VALUE: {
11906 if( pCsr->iField<0 ){
11907 sqlite3_result_int64(ctx, pCsr->iIntkey);
11909 sqlite3_int64 iType;
11910 dbdataGetVarint(pCsr->pHdrPtr, &iType);
11912 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11923 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11925 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11926 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11927 *pRowid = pCsr->iRowid;
11933 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
11935 static int sqlite3DbdataRegister(sqlite3 *db){
11936 static sqlite3_module dbdata_module = {
11939 dbdataConnect, /* xConnect */
11940 dbdataBestIndex, /* xBestIndex */
11941 dbdataDisconnect, /* xDisconnect */
11943 dbdataOpen, /* xOpen - open a cursor */
11944 dbdataClose, /* xClose - close a cursor */
11945 dbdataFilter, /* xFilter - configure scan constraints */
11946 dbdataNext, /* xNext - advance a cursor */
11947 dbdataEof, /* xEof - check for end of scan */
11948 dbdataColumn, /* xColumn - read data */
11949 dbdataRowid, /* xRowid - read data */
11955 0, /* xFindMethod */
11957 0, /* xSavepoint */
11959 0, /* xRollbackTo */
11960 0 /* xShadowName */
11963 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11964 if( rc==SQLITE_OK ){
11965 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11973 int sqlite3_dbdata_init(
11976 const sqlite3_api_routines *pApi
11978 SQLITE_EXTENSION_INIT2(pApi);
11979 return sqlite3DbdataRegister(db);
11982 /************************* End ../ext/misc/dbdata.c ********************/
11985 #if defined(SQLITE_ENABLE_SESSION)
11987 ** State information for a single open session
11989 typedef struct OpenSession OpenSession;
11990 struct OpenSession {
11991 char *zName; /* Symbolic name for this session */
11992 int nFilter; /* Number of xFilter rejection GLOB patterns */
11993 char **azFilter; /* Array of xFilter rejection GLOB patterns */
11994 sqlite3_session *p; /* The open session */
11998 typedef struct ExpertInfo ExpertInfo;
11999 struct ExpertInfo {
12000 sqlite3expert *pExpert;
12004 /* A single line in the EQP output */
12005 typedef struct EQPGraphRow EQPGraphRow;
12006 struct EQPGraphRow {
12007 int iEqpId; /* ID for this row */
12008 int iParentId; /* ID of the parent row */
12009 EQPGraphRow *pNext; /* Next row in sequence */
12010 char zText[1]; /* Text to display for this row */
12013 /* All EQP output is collected into an instance of the following */
12014 typedef struct EQPGraph EQPGraph;
12016 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
12017 EQPGraphRow *pLast; /* Last element of the pRow list */
12018 char zPrefix[100]; /* Graph prefix */
12022 ** State information about the database connection is contained in an
12023 ** instance of the following structure.
12025 typedef struct ShellState ShellState;
12026 struct ShellState {
12027 sqlite3 *db; /* The database */
12028 u8 autoExplain; /* Automatically turn on .explain mode */
12029 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
12030 u8 autoEQPtest; /* autoEQP is in test mode */
12031 u8 autoEQPtrace; /* autoEQP is in trace mode */
12032 u8 scanstatsOn; /* True to display scan stats before each finalize */
12033 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
12034 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
12035 u8 nEqpLevel; /* Depth of the EQP output graph */
12036 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
12037 unsigned statsOn; /* True to display memory stats before each finalize */
12038 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
12039 int outCount; /* Revert to stdout when reaching zero */
12040 int cnt; /* Number of records displayed so far */
12041 int lineno; /* Line number of last line read from in */
12042 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
12043 FILE *in; /* Read commands from this stream */
12044 FILE *out; /* Write results here */
12045 FILE *traceOut; /* Output for sqlite3_trace() */
12046 int nErr; /* Number of errors seen */
12047 int mode; /* An output mode setting */
12048 int modePrior; /* Saved mode */
12049 int cMode; /* temporary output mode for the current query */
12050 int normalMode; /* Output mode before ".explain on" */
12051 int writableSchema; /* True if PRAGMA writable_schema=ON */
12052 int showHeader; /* True to show column names in List or Column mode */
12053 int nCheck; /* Number of ".check" commands run */
12054 unsigned nProgress; /* Number of progress callbacks encountered */
12055 unsigned mxProgress; /* Maximum progress callbacks before failing */
12056 unsigned flgProgress; /* Flags for the progress callback */
12057 unsigned shellFlgs; /* Various flags */
12058 unsigned priorShFlgs; /* Saved copy of flags */
12059 sqlite3_int64 szMax; /* --maxsize argument to .open */
12060 char *zDestTable; /* Name of destination table when MODE_Insert */
12061 char *zTempFile; /* Temporary file that might need deleting */
12062 char zTestcase[30]; /* Name of current test case */
12063 char colSeparator[20]; /* Column separator character for several modes */
12064 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
12065 char colSepPrior[20]; /* Saved column separator */
12066 char rowSepPrior[20]; /* Saved row separator */
12067 int *colWidth; /* Requested width of each column in columnar modes */
12068 int *actualWidth; /* Actual width of each column */
12069 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
12070 char nullValue[20]; /* The text to print when a NULL comes back from
12072 char outfile[FILENAME_MAX]; /* Filename for *out */
12073 const char *zDbFilename; /* name of the database file */
12074 char *zFreeOnClose; /* Filename to free when closing */
12075 const char *zVfs; /* Name of VFS to use */
12076 sqlite3_stmt *pStmt; /* Current statement if any. */
12077 FILE *pLog; /* Write log output here */
12078 int *aiIndent; /* Array of indents used in MODE_Explain */
12079 int nIndent; /* Size of array aiIndent[] */
12080 int iIndent; /* Index of current op in aiIndent[] */
12081 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
12082 #if defined(SQLITE_ENABLE_SESSION)
12083 int nSession; /* Number of active sessions */
12084 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
12086 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
12090 /* Allowed values for ShellState.autoEQP
12092 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
12093 #define AUTOEQP_on 1 /* Automatic EQP is on */
12094 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
12095 #define AUTOEQP_full 3 /* Show full EXPLAIN */
12097 /* Allowed values for ShellState.openMode
12099 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
12100 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
12101 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
12102 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
12103 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
12104 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
12105 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
12107 /* Allowed values for ShellState.eTraceType
12109 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
12110 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
12111 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
12113 /* Bits in the ShellState.flgProgress variable */
12114 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
12115 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
12116 ** callback limit is reached, and for each
12117 ** top-level SQL statement */
12118 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
12121 ** These are the allowed shellFlgs values
12123 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
12124 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
12125 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
12126 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
12127 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
12128 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
12129 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
12130 #define SHFLG_HeaderSet 0x00000080 /* .header has been used */
12131 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
12132 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
12135 ** Macros for testing and setting shellFlgs
12137 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
12138 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
12139 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
12142 ** These are the allowed modes.
12144 #define MODE_Line 0 /* One column per line. Blank line between records */
12145 #define MODE_Column 1 /* One record per line in neat columns */
12146 #define MODE_List 2 /* One record per line with a separator */
12147 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
12148 #define MODE_Html 4 /* Generate an XHTML table */
12149 #define MODE_Insert 5 /* Generate SQL "insert" statements */
12150 #define MODE_Quote 6 /* Quote values as for SQL */
12151 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
12152 #define MODE_Csv 8 /* Quote strings, numbers are plain */
12153 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
12154 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
12155 #define MODE_Pretty 11 /* Pretty-print schemas */
12156 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
12157 #define MODE_Json 13 /* Output JSON */
12158 #define MODE_Markdown 14 /* Markdown formatting */
12159 #define MODE_Table 15 /* MySQL-style table formatting */
12160 #define MODE_Box 16 /* Unicode box-drawing characters */
12162 static const char *modeDescr[] = {
12183 ** These are the column/row/line separators used by the various
12184 ** import/export modes.
12186 #define SEP_Column "|"
12187 #define SEP_Row "\n"
12188 #define SEP_Tab "\t"
12189 #define SEP_Space " "
12190 #define SEP_Comma ","
12191 #define SEP_CrLf "\r\n"
12192 #define SEP_Unit "\x1F"
12193 #define SEP_Record "\x1E"
12196 ** A callback for the sqlite3_log() interface.
12198 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
12199 ShellState *p = (ShellState*)pArg;
12200 if( p->pLog==0 ) return;
12201 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
12206 ** SQL function: shell_putsnl(X)
12208 ** Write the text X to the screen (or whatever output is being directed)
12209 ** adding a newline at the end, and then return X.
12211 static void shellPutsFunc(
12212 sqlite3_context *pCtx,
12214 sqlite3_value **apVal
12216 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
12218 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
12219 sqlite3_result_value(pCtx, apVal[0]);
12223 ** SQL function: edit(VALUE)
12224 ** edit(VALUE,EDITOR)
12228 ** (1) Write VALUE into a temporary file.
12229 ** (2) Run program EDITOR on that temporary file.
12230 ** (3) Read the temporary file back and return its content as the result.
12231 ** (4) Delete the temporary file
12233 ** If the EDITOR argument is omitted, use the value in the VISUAL
12234 ** environment variable. If still there is no EDITOR, through an error.
12236 ** Also throw an error if the EDITOR program returns a non-zero exit code.
12238 #ifndef SQLITE_NOHAVE_SYSTEM
12239 static void editFunc(
12240 sqlite3_context *context,
12242 sqlite3_value **argv
12244 const char *zEditor;
12245 char *zTempFile = 0;
12254 unsigned char *p = 0;
12257 zEditor = (const char*)sqlite3_value_text(argv[1]);
12259 zEditor = getenv("VISUAL");
12262 sqlite3_result_error(context, "no editor for edit()", -1);
12265 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
12266 sqlite3_result_error(context, "NULL input to edit()", -1);
12269 db = sqlite3_context_db_handle(context);
12271 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
12272 if( zTempFile==0 ){
12273 sqlite3_uint64 r = 0;
12274 sqlite3_randomness(sizeof(r), &r);
12275 zTempFile = sqlite3_mprintf("temp%llx", r);
12276 if( zTempFile==0 ){
12277 sqlite3_result_error_nomem(context);
12281 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
12282 /* When writing the file to be edited, do \n to \r\n conversions on systems
12283 ** that want \r\n line endings */
12284 f = fopen(zTempFile, bBin ? "wb" : "w");
12286 sqlite3_result_error(context, "edit() cannot open temp file", -1);
12287 goto edit_func_end;
12289 sz = sqlite3_value_bytes(argv[0]);
12291 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
12293 const char *z = (const char*)sqlite3_value_text(argv[0]);
12294 /* Remember whether or not the value originally contained \r\n */
12295 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
12296 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
12301 sqlite3_result_error(context, "edit() could not write the whole file", -1);
12302 goto edit_func_end;
12304 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
12306 sqlite3_result_error_nomem(context);
12307 goto edit_func_end;
12310 sqlite3_free(zCmd);
12312 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
12313 goto edit_func_end;
12315 f = fopen(zTempFile, "rb");
12317 sqlite3_result_error(context,
12318 "edit() cannot reopen temp file after edit", -1);
12319 goto edit_func_end;
12321 fseek(f, 0, SEEK_END);
12324 p = sqlite3_malloc64( sz+1 );
12326 sqlite3_result_error_nomem(context);
12327 goto edit_func_end;
12329 x = fread(p, 1, (size_t)sz, f);
12333 sqlite3_result_error(context, "could not read back the whole file", -1);
12334 goto edit_func_end;
12337 sqlite3_result_blob64(context, p, sz, sqlite3_free);
12339 sqlite3_int64 i, j;
12341 /* If the original contains \r\n then do no conversions back to \n */
12344 /* If the file did not originally contain \r\n then convert any new
12345 ** \r\n back into \n */
12346 for(i=j=0; i<sz; i++){
12347 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
12353 sqlite3_result_text64(context, (const char*)p, sz,
12354 sqlite3_free, SQLITE_UTF8);
12361 sqlite3_free(zTempFile);
12364 #endif /* SQLITE_NOHAVE_SYSTEM */
12367 ** Save or restore the current output mode
12369 static void outputModePush(ShellState *p){
12370 p->modePrior = p->mode;
12371 p->priorShFlgs = p->shellFlgs;
12372 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
12373 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
12375 static void outputModePop(ShellState *p){
12376 p->mode = p->modePrior;
12377 p->shellFlgs = p->priorShFlgs;
12378 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
12379 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
12383 ** Output the given string as a hex-encoded blob (eg. X'1234' )
12385 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
12387 char *zBlob = (char *)pBlob;
12388 raw_printf(out,"X'");
12389 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
12390 raw_printf(out,"'");
12394 ** Find a string that is not found anywhere in z[]. Return a pointer
12397 ** Try to use zA and zB first. If both of those are already found in z[]
12398 ** then make up some string and store it in the buffer zBuf.
12400 static const char *unused_string(
12401 const char *z, /* Result must not appear anywhere in z */
12402 const char *zA, const char *zB, /* Try these first */
12403 char *zBuf /* Space to store a generated string */
12406 if( strstr(z, zA)==0 ) return zA;
12407 if( strstr(z, zB)==0 ) return zB;
12409 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
12410 }while( strstr(z,zBuf)!=0 );
12415 ** Output the given string as a quoted string using SQL quoting conventions.
12417 ** See also: output_quoted_escaped_string()
12419 static void output_quoted_string(FILE *out, const char *z){
12422 setBinaryMode(out, 1);
12423 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12425 utf8_printf(out,"'%s'",z);
12427 raw_printf(out, "'");
12429 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
12432 utf8_printf(out, "%.*s", i, z);
12436 raw_printf(out, "'");
12444 raw_printf(out, "'");
12446 setTextMode(out, 1);
12450 ** Output the given string as a quoted string using SQL quoting conventions.
12451 ** Additionallly , escape the "\n" and "\r" characters so that they do not
12452 ** get corrupted by end-of-line translation facilities in some operating
12455 ** This is like output_quoted_string() but with the addition of the \r\n
12456 ** escape mechanism.
12458 static void output_quoted_escaped_string(FILE *out, const char *z){
12461 setBinaryMode(out, 1);
12462 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
12464 utf8_printf(out,"'%s'",z);
12466 const char *zNL = 0;
12467 const char *zCR = 0;
12470 char zBuf1[20], zBuf2[20];
12471 for(i=0; z[i]; i++){
12472 if( z[i]=='\n' ) nNL++;
12473 if( z[i]=='\r' ) nCR++;
12476 raw_printf(out, "replace(");
12477 zNL = unused_string(z, "\\n", "\\012", zBuf1);
12480 raw_printf(out, "replace(");
12481 zCR = unused_string(z, "\\r", "\\015", zBuf2);
12483 raw_printf(out, "'");
12485 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
12488 utf8_printf(out, "%.*s", i, z);
12492 raw_printf(out, "'");
12500 raw_printf(out, "%s", zNL);
12503 raw_printf(out, "%s", zCR);
12505 raw_printf(out, "'");
12507 raw_printf(out, ",'%s',char(13))", zCR);
12510 raw_printf(out, ",'%s',char(10))", zNL);
12513 setTextMode(out, 1);
12517 ** Output the given string as a quoted according to C or TCL quoting rules.
12519 static void output_c_string(FILE *out, const char *z){
12522 while( (c = *(z++))!=0 ){
12526 }else if( c=='"' ){
12529 }else if( c=='\t' ){
12532 }else if( c=='\n' ){
12535 }else if( c=='\r' ){
12538 }else if( !isprint(c&0xff) ){
12539 raw_printf(out, "\\%03o", c&0xff);
12548 ** Output the given string as a quoted according to JSON quoting rules.
12550 static void output_json_string(FILE *out, const char *z, int n){
12552 if( n<0 ) n = (int)strlen(z);
12556 if( c=='\\' || c=='"' ){
12559 }else if( c<=0x1f ){
12563 }else if( c=='\f' ){
12565 }else if( c=='\n' ){
12567 }else if( c=='\r' ){
12569 }else if( c=='\t' ){
12572 raw_printf(out, "u%04x",c);
12582 ** Output the given string with characters that are special to
12585 static void output_html_string(FILE *out, const char *z){
12597 utf8_printf(out,"%.*s",i,z);
12600 raw_printf(out,"<");
12601 }else if( z[i]=='&' ){
12602 raw_printf(out,"&");
12603 }else if( z[i]=='>' ){
12604 raw_printf(out,">");
12605 }else if( z[i]=='\"' ){
12606 raw_printf(out,""");
12607 }else if( z[i]=='\'' ){
12608 raw_printf(out,"'");
12617 ** If a field contains any character identified by a 1 in the following
12618 ** array, then the string must be quoted for CSV.
12620 static const char needCsvQuote[] = {
12621 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12622 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12623 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
12624 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12625 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12626 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12627 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
12628 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
12629 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12630 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12631 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12632 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12633 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12634 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12635 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12636 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
12640 ** Output a single term of CSV. Actually, p->colSeparator is used for
12641 ** the separator, which may or may not be a comma. p->nullValue is
12642 ** the null value. Strings are quoted if necessary. The separator
12643 ** is only issued if bSep is true.
12645 static void output_csv(ShellState *p, const char *z, int bSep){
12646 FILE *out = p->out;
12648 utf8_printf(out,"%s",p->nullValue);
12651 int nSep = strlen30(p->colSeparator);
12652 for(i=0; z[i]; i++){
12653 if( needCsvQuote[((unsigned char*)z)[i]]
12654 || (z[i]==p->colSeparator[0] &&
12655 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
12661 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
12662 utf8_printf(out, "%s", zQuoted);
12663 sqlite3_free(zQuoted);
12665 utf8_printf(out, "%s", z);
12669 utf8_printf(p->out, "%s", p->colSeparator);
12674 ** This routine runs when the user presses Ctrl-C
12676 static void interrupt_handler(int NotUsed){
12677 UNUSED_PARAMETER(NotUsed);
12679 if( seenInterrupt>2 ) exit(1);
12680 if( globalDb ) sqlite3_interrupt(globalDb);
12683 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
12685 ** This routine runs for console events (e.g. Ctrl-C) on Win32
12687 static BOOL WINAPI ConsoleCtrlHandler(
12688 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
12690 if( dwCtrlType==CTRL_C_EVENT ){
12691 interrupt_handler(0);
12698 #ifndef SQLITE_OMIT_AUTHORIZATION
12700 ** When the ".auth ON" is set, the following authorizer callback is
12701 ** invoked. It always returns SQLITE_OK.
12703 static int shellAuth(
12711 ShellState *p = (ShellState*)pClientData;
12712 static const char *azAction[] = { 0,
12713 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
12714 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
12715 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
12716 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
12717 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
12718 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
12719 "PRAGMA", "READ", "SELECT",
12720 "TRANSACTION", "UPDATE", "ATTACH",
12721 "DETACH", "ALTER_TABLE", "REINDEX",
12722 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
12723 "FUNCTION", "SAVEPOINT", "RECURSIVE"
12731 utf8_printf(p->out, "authorizer: %s", azAction[op]);
12732 for(i=0; i<4; i++){
12733 raw_printf(p->out, " ");
12735 output_c_string(p->out, az[i]);
12737 raw_printf(p->out, "NULL");
12740 raw_printf(p->out, "\n");
12746 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
12748 ** This routine converts some CREATE TABLE statements for shadow tables
12749 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
12751 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
12753 if( zTail==0 ) return;
12754 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
12755 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
12757 utf8_printf(out, "%s%s", z, zTail);
12760 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
12763 printSchemaLine(out, z, zTail);
12768 ** Return true if string z[] has nothing but whitespace and comments to the
12769 ** end of the first line.
12771 static int wsToEol(const char *z){
12773 for(i=0; z[i]; i++){
12774 if( z[i]=='\n' ) return 1;
12775 if( IsSpace(z[i]) ) continue;
12776 if( z[i]=='-' && z[i+1]=='-' ) return 1;
12783 ** Add a new entry to the EXPLAIN QUERY PLAN data
12785 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
12787 int nText = strlen30(zText);
12788 if( p->autoEQPtest ){
12789 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
12791 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
12792 if( pNew==0 ) shell_out_of_memory();
12793 pNew->iEqpId = iEqpId;
12794 pNew->iParentId = p2;
12795 memcpy(pNew->zText, zText, nText+1);
12797 if( p->sGraph.pLast ){
12798 p->sGraph.pLast->pNext = pNew;
12800 p->sGraph.pRow = pNew;
12802 p->sGraph.pLast = pNew;
12806 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
12809 static void eqp_reset(ShellState *p){
12810 EQPGraphRow *pRow, *pNext;
12811 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
12812 pNext = pRow->pNext;
12813 sqlite3_free(pRow);
12815 memset(&p->sGraph, 0, sizeof(p->sGraph));
12818 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
12819 ** pOld, or return the first such line if pOld is NULL
12821 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
12822 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
12823 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
12827 /* Render a single level of the graph that has iEqpId as its parent. Called
12828 ** recursively to render sublevels.
12830 static void eqp_render_level(ShellState *p, int iEqpId){
12831 EQPGraphRow *pRow, *pNext;
12832 int n = strlen30(p->sGraph.zPrefix);
12834 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
12835 pNext = eqp_next_row(p, iEqpId, pRow);
12837 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
12838 pNext ? "|--" : "`--", z);
12839 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
12840 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
12841 eqp_render_level(p, pRow->iEqpId);
12842 p->sGraph.zPrefix[n] = 0;
12848 ** Display and reset the EXPLAIN QUERY PLAN data
12850 static void eqp_render(ShellState *p){
12851 EQPGraphRow *pRow = p->sGraph.pRow;
12853 if( pRow->zText[0]=='-' ){
12854 if( pRow->pNext==0 ){
12858 utf8_printf(p->out, "%s\n", pRow->zText+3);
12859 p->sGraph.pRow = pRow->pNext;
12860 sqlite3_free(pRow);
12862 utf8_printf(p->out, "QUERY PLAN\n");
12864 p->sGraph.zPrefix[0] = 0;
12865 eqp_render_level(p, 0);
12870 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12872 ** Progress handler callback.
12874 static int progress_handler(void *pClientData) {
12875 ShellState *p = (ShellState*)pClientData;
12877 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
12878 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
12879 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
12880 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
12883 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
12884 raw_printf(p->out, "Progress %u\n", p->nProgress);
12888 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
12893 static void print_dashes(FILE *out, int N){
12894 const char zDash[] = "--------------------------------------------------";
12895 const int nDash = sizeof(zDash) - 1;
12900 raw_printf(out, "%.*s", N, zDash);
12904 ** Print a markdown or table-style row separator using ascii-art
12906 static void print_row_separator(
12913 fputs(zSep, p->out);
12914 print_dashes(p->out, p->actualWidth[0]+2);
12915 for(i=1; i<nArg; i++){
12916 fputs(zSep, p->out);
12917 print_dashes(p->out, p->actualWidth[i]+2);
12919 fputs(zSep, p->out);
12921 fputs("\n", p->out);
12925 ** This is the callback routine that the shell
12926 ** invokes for each row of a query result.
12928 static int shell_callback(
12930 int nArg, /* Number of result columns */
12931 char **azArg, /* Text of each result column */
12932 char **azCol, /* Column names */
12933 int *aiType /* Column types. Might be NULL */
12936 ShellState *p = (ShellState*)pArg;
12938 if( azArg==0 ) return 0;
12939 switch( p->cMode ){
12942 if( azArg==0 ) break;
12943 for(i=0; i<nArg; i++){
12944 int len = strlen30(azCol[i] ? azCol[i] : "");
12945 if( len>w ) w = len;
12947 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12948 for(i=0; i<nArg; i++){
12949 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12950 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12954 case MODE_Explain: {
12955 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12956 if( nArg>ArraySize(aExplainWidth) ){
12957 nArg = ArraySize(aExplainWidth);
12960 for(i=0; i<nArg; i++){
12961 int w = aExplainWidth[i];
12962 utf8_width_print(p->out, w, azCol[i]);
12963 fputs(i==nArg-1 ? "\n" : " ", p->out);
12965 for(i=0; i<nArg; i++){
12966 int w = aExplainWidth[i];
12967 print_dashes(p->out, w);
12968 fputs(i==nArg-1 ? "\n" : " ", p->out);
12971 if( azArg==0 ) break;
12972 for(i=0; i<nArg; i++){
12973 int w = aExplainWidth[i];
12974 if( i==nArg-1 ) w = 0;
12975 if( azArg[i] && strlenChar(azArg[i])>w ){
12976 w = strlenChar(azArg[i]);
12978 if( i==1 && p->aiIndent && p->pStmt ){
12979 if( p->iIndent<p->nIndent ){
12980 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12984 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12985 fputs(i==nArg-1 ? "\n" : " ", p->out);
12989 case MODE_Semi: { /* .schema and .fullschema output */
12990 printSchemaLine(p->out, azArg[0], ";\n");
12993 case MODE_Pretty: { /* .schema and .fullschema with --indent */
13001 if( azArg[0]==0 ) break;
13002 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
13003 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
13005 utf8_printf(p->out, "%s;\n", azArg[0]);
13008 z = sqlite3_mprintf("%s", azArg[0]);
13010 for(i=0; IsSpace(z[i]); i++){}
13011 for(; (c = z[i])!=0; i++){
13013 if( z[j-1]=='\r' ) z[j-1] = '\n';
13014 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
13015 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
13020 while( j>0 && IsSpace(z[j-1]) ){ j--; }
13022 if( strlen30(z)>=79 ){
13023 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
13026 }else if( c=='"' || c=='\'' || c=='`' ){
13028 }else if( c=='[' ){
13030 }else if( c=='-' && z[i+1]=='-' ){
13032 }else if( c=='(' ){
13034 }else if( c==')' ){
13036 if( nLine>0 && nParen==0 && j>0 ){
13037 printSchemaLineN(p->out, z, j, "\n");
13042 if( nParen==1 && cEnd==0
13043 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
13046 printSchemaLineN(p->out, z, j, "\n ");
13049 while( IsSpace(z[i+1]) ){ i++; }
13054 printSchemaLine(p->out, z, ";\n");
13059 if( p->cnt++==0 && p->showHeader ){
13060 for(i=0; i<nArg; i++){
13061 utf8_printf(p->out,"%s%s",azCol[i],
13062 i==nArg-1 ? p->rowSeparator : p->colSeparator);
13065 if( azArg==0 ) break;
13066 for(i=0; i<nArg; i++){
13067 char *z = azArg[i];
13068 if( z==0 ) z = p->nullValue;
13069 utf8_printf(p->out, "%s", z);
13071 utf8_printf(p->out, "%s", p->colSeparator);
13073 utf8_printf(p->out, "%s", p->rowSeparator);
13079 if( p->cnt++==0 && p->showHeader ){
13080 raw_printf(p->out,"<TR>");
13081 for(i=0; i<nArg; i++){
13082 raw_printf(p->out,"<TH>");
13083 output_html_string(p->out, azCol[i]);
13084 raw_printf(p->out,"</TH>\n");
13086 raw_printf(p->out,"</TR>\n");
13088 if( azArg==0 ) break;
13089 raw_printf(p->out,"<TR>");
13090 for(i=0; i<nArg; i++){
13091 raw_printf(p->out,"<TD>");
13092 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13093 raw_printf(p->out,"</TD>\n");
13095 raw_printf(p->out,"</TR>\n");
13099 if( p->cnt++==0 && p->showHeader ){
13100 for(i=0; i<nArg; i++){
13101 output_c_string(p->out,azCol[i] ? azCol[i] : "");
13102 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13104 utf8_printf(p->out, "%s", p->rowSeparator);
13106 if( azArg==0 ) break;
13107 for(i=0; i<nArg; i++){
13108 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
13109 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
13111 utf8_printf(p->out, "%s", p->rowSeparator);
13115 setBinaryMode(p->out, 1);
13116 if( p->cnt++==0 && p->showHeader ){
13117 for(i=0; i<nArg; i++){
13118 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
13120 utf8_printf(p->out, "%s", p->rowSeparator);
13123 for(i=0; i<nArg; i++){
13124 output_csv(p, azArg[i], i<nArg-1);
13126 utf8_printf(p->out, "%s", p->rowSeparator);
13128 setTextMode(p->out, 1);
13131 case MODE_Insert: {
13132 if( azArg==0 ) break;
13133 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
13134 if( p->showHeader ){
13135 raw_printf(p->out,"(");
13136 for(i=0; i<nArg; i++){
13137 if( i>0 ) raw_printf(p->out, ",");
13138 if( quoteChar(azCol[i]) ){
13139 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
13140 utf8_printf(p->out, "%s", z);
13143 raw_printf(p->out, "%s", azCol[i]);
13146 raw_printf(p->out,")");
13149 for(i=0; i<nArg; i++){
13150 raw_printf(p->out, i>0 ? "," : " VALUES(");
13151 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13152 utf8_printf(p->out,"NULL");
13153 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13154 if( ShellHasFlag(p, SHFLG_Newlines) ){
13155 output_quoted_string(p->out, azArg[i]);
13157 output_quoted_escaped_string(p->out, azArg[i]);
13159 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13160 utf8_printf(p->out,"%s", azArg[i]);
13161 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13163 double r = sqlite3_column_double(p->pStmt, i);
13165 memcpy(&ur,&r,sizeof(r));
13166 if( ur==0x7ff0000000000000LL ){
13167 raw_printf(p->out, "1e999");
13168 }else if( ur==0xfff0000000000000LL ){
13169 raw_printf(p->out, "-1e999");
13171 sqlite3_snprintf(50,z,"%!.20g", r);
13172 raw_printf(p->out, "%s", z);
13174 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13175 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13176 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13177 output_hex_blob(p->out, pBlob, nBlob);
13178 }else if( isNumber(azArg[i], 0) ){
13179 utf8_printf(p->out,"%s", azArg[i]);
13180 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
13181 output_quoted_string(p->out, azArg[i]);
13183 output_quoted_escaped_string(p->out, azArg[i]);
13186 raw_printf(p->out,");\n");
13190 if( azArg==0 ) break;
13192 fputs("[{", p->out);
13194 fputs(",\n{", p->out);
13197 for(i=0; i<nArg; i++){
13198 output_json_string(p->out, azCol[i], -1);
13200 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13201 fputs("null",p->out);
13202 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13204 double r = sqlite3_column_double(p->pStmt, i);
13206 memcpy(&ur,&r,sizeof(r));
13207 if( ur==0x7ff0000000000000LL ){
13208 raw_printf(p->out, "1e999");
13209 }else if( ur==0xfff0000000000000LL ){
13210 raw_printf(p->out, "-1e999");
13212 sqlite3_snprintf(50,z,"%!.20g", r);
13213 raw_printf(p->out, "%s", z);
13215 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13216 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13217 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13218 output_json_string(p->out, pBlob, nBlob);
13219 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13220 output_json_string(p->out, azArg[i], -1);
13222 utf8_printf(p->out,"%s", azArg[i]);
13232 if( azArg==0 ) break;
13233 if( p->cnt==0 && p->showHeader ){
13234 for(i=0; i<nArg; i++){
13235 if( i>0 ) fputs(p->colSeparator, p->out);
13236 output_quoted_string(p->out, azCol[i]);
13238 fputs(p->rowSeparator, p->out);
13241 for(i=0; i<nArg; i++){
13242 if( i>0 ) fputs(p->colSeparator, p->out);
13243 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
13244 utf8_printf(p->out,"NULL");
13245 }else if( aiType && aiType[i]==SQLITE_TEXT ){
13246 output_quoted_string(p->out, azArg[i]);
13247 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
13248 utf8_printf(p->out,"%s", azArg[i]);
13249 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
13251 double r = sqlite3_column_double(p->pStmt, i);
13252 sqlite3_snprintf(50,z,"%!.20g", r);
13253 raw_printf(p->out, "%s", z);
13254 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
13255 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
13256 int nBlob = sqlite3_column_bytes(p->pStmt, i);
13257 output_hex_blob(p->out, pBlob, nBlob);
13258 }else if( isNumber(azArg[i], 0) ){
13259 utf8_printf(p->out,"%s", azArg[i]);
13261 output_quoted_string(p->out, azArg[i]);
13264 fputs(p->rowSeparator, p->out);
13268 if( p->cnt++==0 && p->showHeader ){
13269 for(i=0; i<nArg; i++){
13270 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13271 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
13273 utf8_printf(p->out, "%s", p->rowSeparator);
13275 if( azArg==0 ) break;
13276 for(i=0; i<nArg; i++){
13277 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
13278 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
13280 utf8_printf(p->out, "%s", p->rowSeparator);
13284 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
13292 ** This is the callback routine that the SQLite library
13293 ** invokes for each row of a query result.
13295 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
13296 /* since we don't have type info, call the shell_callback with a NULL value */
13297 return shell_callback(pArg, nArg, azArg, azCol, NULL);
13301 ** This is the callback routine from sqlite3_exec() that appends all
13302 ** output onto the end of a ShellText object.
13304 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
13305 ShellText *p = (ShellText*)pArg;
13307 UNUSED_PARAMETER(az);
13308 if( azArg==0 ) return 0;
13309 if( p->n ) appendText(p, "|", 0);
13310 for(i=0; i<nArg; i++){
13311 if( i ) appendText(p, ",", 0);
13312 if( azArg[i] ) appendText(p, azArg[i], 0);
13318 ** Generate an appropriate SELFTEST table in the main database.
13320 static void createSelftestTable(ShellState *p){
13322 sqlite3_exec(p->db,
13323 "SAVEPOINT selftest_init;\n"
13324 "CREATE TABLE IF NOT EXISTS selftest(\n"
13325 " tno INTEGER PRIMARY KEY,\n" /* Test number */
13326 " op TEXT,\n" /* Operator: memo run */
13327 " cmd TEXT,\n" /* Command text */
13328 " ans TEXT\n" /* Desired answer */
13330 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
13331 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
13332 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
13333 " 'memo','Tests generated by --init');\n"
13334 "INSERT INTO [_shell$self]\n"
13336 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
13337 "FROM sqlite_schema ORDER BY 2'',224))',\n"
13338 " hex(sha3_query('SELECT type,name,tbl_name,sql "
13339 "FROM sqlite_schema ORDER BY 2',224));\n"
13340 "INSERT INTO [_shell$self]\n"
13342 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
13343 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
13344 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
13346 " SELECT name FROM sqlite_schema\n"
13347 " WHERE type='table'\n"
13348 " AND name<>'selftest'\n"
13349 " AND coalesce(rootpage,0)>0\n"
13351 " ORDER BY name;\n"
13352 "INSERT INTO [_shell$self]\n"
13353 " VALUES('run','PRAGMA integrity_check','ok');\n"
13354 "INSERT INTO selftest(tno,op,cmd,ans)"
13355 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
13356 "DROP TABLE [_shell$self];"
13359 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
13360 sqlite3_free(zErrMsg);
13362 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
13367 ** Set the destination table field of the ShellState structure to
13368 ** the name of the table given. Escape any quote characters in the
13371 static void set_table_name(ShellState *p, const char *zName){
13376 if( p->zDestTable ){
13377 free(p->zDestTable);
13380 if( zName==0 ) return;
13381 cQuote = quoteChar(zName);
13382 n = strlen30(zName);
13383 if( cQuote ) n += n+2;
13384 z = p->zDestTable = malloc( n+1 );
13385 if( z==0 ) shell_out_of_memory();
13387 if( cQuote ) z[n++] = cQuote;
13388 for(i=0; zName[i]; i++){
13390 if( zName[i]==cQuote ) z[n++] = cQuote;
13392 if( cQuote ) z[n++] = cQuote;
13398 ** Execute a query statement that will generate SQL output. Print
13399 ** the result columns, comma-separated, on a line and then add a
13400 ** semicolon terminator to the end of that line.
13402 ** If the number of columns is 1 and that column contains text "--"
13403 ** then write the semicolon on a separate line. That way, if a
13404 ** "--" comment occurs at the end of the statement, the comment
13405 ** won't consume the semicolon terminator.
13407 static int run_table_dump_query(
13408 ShellState *p, /* Query context */
13409 const char *zSelect /* SELECT statement to extract content */
13411 sqlite3_stmt *pSelect;
13416 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
13417 if( rc!=SQLITE_OK || !pSelect ){
13418 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13419 sqlite3_errmsg(p->db));
13420 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13423 rc = sqlite3_step(pSelect);
13424 nResult = sqlite3_column_count(pSelect);
13425 while( rc==SQLITE_ROW ){
13426 z = (const char*)sqlite3_column_text(pSelect, 0);
13427 utf8_printf(p->out, "%s", z);
13428 for(i=1; i<nResult; i++){
13429 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
13432 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
13434 raw_printf(p->out, "\n;\n");
13436 raw_printf(p->out, ";\n");
13438 rc = sqlite3_step(pSelect);
13440 rc = sqlite3_finalize(pSelect);
13441 if( rc!=SQLITE_OK ){
13442 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
13443 sqlite3_errmsg(p->db));
13444 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
13450 ** Allocate space and save off current error string.
13452 static char *save_err_msg(
13453 sqlite3 *db /* Database to query */
13455 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
13456 char *zErrMsg = sqlite3_malloc64(nErrMsg);
13458 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
13465 ** Attempt to display I/O stats on Linux using /proc/PID/io
13467 static void displayLinuxIoStats(FILE *out){
13470 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
13471 in = fopen(z, "rb");
13472 if( in==0 ) return;
13473 while( fgets(z, sizeof(z), in)!=0 ){
13474 static const struct {
13475 const char *zPattern;
13478 { "rchar: ", "Bytes received by read():" },
13479 { "wchar: ", "Bytes sent to write():" },
13480 { "syscr: ", "Read() system calls:" },
13481 { "syscw: ", "Write() system calls:" },
13482 { "read_bytes: ", "Bytes read from storage:" },
13483 { "write_bytes: ", "Bytes written to storage:" },
13484 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
13487 for(i=0; i<ArraySize(aTrans); i++){
13488 int n = strlen30(aTrans[i].zPattern);
13489 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
13490 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
13500 ** Display a single line of status using 64-bit values.
13502 static void displayStatLine(
13503 ShellState *p, /* The shell context */
13504 char *zLabel, /* Label for this one line */
13505 char *zFormat, /* Format for the result */
13506 int iStatusCtrl, /* Which status to display */
13507 int bReset /* True to reset the stats */
13509 sqlite3_int64 iCur = -1;
13510 sqlite3_int64 iHiwtr = -1;
13513 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
13514 for(i=0, nPercent=0; zFormat[i]; i++){
13515 if( zFormat[i]=='%' ) nPercent++;
13518 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
13520 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
13522 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
13526 ** Display memory stats.
13528 static int display_stats(
13529 sqlite3 *db, /* Database to query */
13530 ShellState *pArg, /* Pointer to ShellState */
13531 int bReset /* True to reset the stats */
13536 if( pArg==0 || pArg->out==0 ) return 0;
13539 if( pArg->pStmt && pArg->statsOn==2 ){
13541 sqlite3_stmt *pStmt = pArg->pStmt;
13543 nCol = sqlite3_column_count(pStmt);
13544 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
13545 for(i=0; i<nCol; i++){
13546 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
13547 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
13548 #ifndef SQLITE_OMIT_DECLTYPE
13549 sqlite3_snprintf(30, z+x, "declared type:");
13550 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
13552 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13553 sqlite3_snprintf(30, z+x, "database name:");
13554 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
13555 sqlite3_snprintf(30, z+x, "table name:");
13556 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
13557 sqlite3_snprintf(30, z+x, "origin name:");
13558 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
13563 if( pArg->statsOn==3 ){
13565 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13566 raw_printf(pArg->out, "VM-steps: %d\n", iCur);
13571 displayStatLine(pArg, "Memory Used:",
13572 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
13573 displayStatLine(pArg, "Number of Outstanding Allocations:",
13574 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
13575 if( pArg->shellFlgs & SHFLG_Pagecache ){
13576 displayStatLine(pArg, "Number of Pcache Pages Used:",
13577 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
13579 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
13580 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
13581 displayStatLine(pArg, "Largest Allocation:",
13582 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
13583 displayStatLine(pArg, "Largest Pcache Allocation:",
13584 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
13585 #ifdef YYTRACKMAXSTACKDEPTH
13586 displayStatLine(pArg, "Deepest Parser Stack:",
13587 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
13591 if( pArg->shellFlgs & SHFLG_Lookaside ){
13592 iHiwtr = iCur = -1;
13593 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
13594 &iCur, &iHiwtr, bReset);
13595 raw_printf(pArg->out,
13596 "Lookaside Slots Used: %d (max %d)\n",
13598 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
13599 &iCur, &iHiwtr, bReset);
13600 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
13602 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
13603 &iCur, &iHiwtr, bReset);
13604 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
13606 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
13607 &iCur, &iHiwtr, bReset);
13608 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
13611 iHiwtr = iCur = -1;
13612 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
13613 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
13615 iHiwtr = iCur = -1;
13616 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
13617 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
13618 iHiwtr = iCur = -1;
13619 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
13620 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
13621 iHiwtr = iCur = -1;
13622 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
13623 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
13624 iHiwtr = iCur = -1;
13625 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
13626 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
13627 iHiwtr = iCur = -1;
13628 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
13629 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
13631 iHiwtr = iCur = -1;
13632 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
13633 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
13638 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
13640 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
13641 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
13642 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
13643 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
13644 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
13645 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
13646 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
13647 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
13648 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
13649 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
13650 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
13651 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
13652 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
13656 displayLinuxIoStats(pArg->out);
13659 /* Do not remove this machine readable comment: extra-stats-output-here */
13665 ** Display scan stats.
13667 static void display_scanstats(
13668 sqlite3 *db, /* Database to query */
13669 ShellState *pArg /* Pointer to ShellState */
13671 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13672 UNUSED_PARAMETER(db);
13673 UNUSED_PARAMETER(pArg);
13676 raw_printf(pArg->out, "-------- scanstats --------\n");
13678 for(k=0; k<=mx; k++){
13679 double rEstLoop = 1.0;
13680 for(i=n=0; 1; i++){
13681 sqlite3_stmt *p = pArg->pStmt;
13682 sqlite3_int64 nLoop, nVisit;
13685 const char *zExplain;
13686 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
13689 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
13690 if( iSid>mx ) mx = iSid;
13691 if( iSid!=k ) continue;
13693 rEstLoop = (double)nLoop;
13694 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
13697 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
13698 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
13699 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
13700 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
13702 raw_printf(pArg->out,
13703 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
13704 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
13708 raw_printf(pArg->out, "---------------------------\n");
13713 ** Parameter azArray points to a zero-terminated array of strings. zStr
13714 ** points to a single nul-terminated string. Return non-zero if zStr
13715 ** is equal, according to strcmp(), to any of the strings in the array.
13716 ** Otherwise, return zero.
13718 static int str_in_array(const char *zStr, const char **azArray){
13720 for(i=0; azArray[i]; i++){
13721 if( 0==strcmp(zStr, azArray[i]) ) return 1;
13727 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
13728 ** and populate the ShellState.aiIndent[] array with the number of
13729 ** spaces each opcode should be indented before it is output.
13731 ** The indenting rules are:
13733 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
13734 ** all opcodes that occur between the p2 jump destination and the opcode
13735 ** itself by 2 spaces.
13737 ** * For each "Goto", if the jump destination is earlier in the program
13738 ** and ends on one of:
13739 ** Yield SeekGt SeekLt RowSetRead Rewind
13740 ** or if the P1 parameter is one instead of zero,
13741 ** then indent all opcodes between the earlier instruction
13742 ** and "Goto" by 2 spaces.
13744 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
13745 const char *zSql; /* The text of the SQL statement */
13746 const char *z; /* Used to check if this is an EXPLAIN */
13747 int *abYield = 0; /* True if op is an OP_Yield */
13748 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
13749 int iOp; /* Index of operation in p->aiIndent[] */
13751 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
13752 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
13754 const char *azGoto[] = { "Goto", 0 };
13756 /* Try to figure out if this is really an EXPLAIN statement. If this
13757 ** cannot be verified, return early. */
13758 if( sqlite3_column_count(pSql)!=8 ){
13759 p->cMode = p->mode;
13762 zSql = sqlite3_sql(pSql);
13763 if( zSql==0 ) return;
13764 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
13765 if( sqlite3_strnicmp(z, "explain", 7) ){
13766 p->cMode = p->mode;
13770 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
13772 int iAddr = sqlite3_column_int(pSql, 0);
13773 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
13775 /* Set p2 to the P2 field of the current opcode. Then, assuming that
13776 ** p2 is an instruction address, set variable p2op to the index of that
13777 ** instruction in the aiIndent[] array. p2 and p2op may be different if
13778 ** the current instruction is part of a sub-program generated by an
13779 ** SQL trigger or foreign key. */
13780 int p2 = sqlite3_column_int(pSql, 3);
13781 int p2op = (p2 + (iOp-iAddr));
13783 /* Grow the p->aiIndent array as required */
13786 /* Do further verfication that this is explain output. Abort if
13788 static const char *explainCols[] = {
13789 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
13791 for(jj=0; jj<ArraySize(explainCols); jj++){
13792 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
13793 p->cMode = p->mode;
13794 sqlite3_reset(pSql);
13800 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
13801 if( p->aiIndent==0 ) shell_out_of_memory();
13802 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
13803 if( abYield==0 ) shell_out_of_memory();
13805 abYield[iOp] = str_in_array(zOp, azYield);
13806 p->aiIndent[iOp] = 0;
13807 p->nIndent = iOp+1;
13809 if( str_in_array(zOp, azNext) ){
13810 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13812 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
13813 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
13815 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
13820 sqlite3_free(abYield);
13821 sqlite3_reset(pSql);
13825 ** Free the array allocated by explain_data_prepare().
13827 static void explain_data_delete(ShellState *p){
13828 sqlite3_free(p->aiIndent);
13835 ** Disable and restore .wheretrace and .selecttrace settings.
13837 static unsigned int savedSelectTrace;
13838 static unsigned int savedWhereTrace;
13839 static void disable_debug_trace_modes(void){
13840 unsigned int zero = 0;
13841 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 0, &savedSelectTrace);
13842 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &zero);
13843 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 2, &savedWhereTrace);
13844 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &zero);
13846 static void restore_debug_trace_modes(void){
13847 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &savedSelectTrace);
13848 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &savedWhereTrace);
13851 /* Create the TEMP table used to store parameter bindings */
13852 static void bind_table_init(ShellState *p){
13854 int defensiveMode = 0;
13855 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
13856 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
13857 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
13858 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
13859 sqlite3_exec(p->db,
13860 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
13861 " key TEXT PRIMARY KEY,\n"
13863 ") WITHOUT ROWID;",
13865 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
13866 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
13870 ** Bind parameters on a prepared statement.
13872 ** Parameter bindings are taken from a TEMP table of the form:
13874 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
13877 ** No bindings occur if this table does not exist. The name of the table
13878 ** begins with "sqlite_" so that it will not collide with ordinary application
13879 ** tables. The table must be in the TEMP schema.
13881 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
13885 sqlite3_stmt *pQ = 0;
13887 nVar = sqlite3_bind_parameter_count(pStmt);
13888 if( nVar==0 ) return; /* Nothing to do */
13889 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
13890 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
13891 return; /* Parameter table does not exist */
13893 rc = sqlite3_prepare_v2(pArg->db,
13894 "SELECT value FROM temp.sqlite_parameters"
13895 " WHERE key=?1", -1, &pQ, 0);
13896 if( rc || pQ==0 ) return;
13897 for(i=1; i<=nVar; i++){
13899 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
13901 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
13904 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
13905 if( sqlite3_step(pQ)==SQLITE_ROW ){
13906 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
13908 sqlite3_bind_null(pStmt, i);
13912 sqlite3_finalize(pQ);
13916 ** UTF8 box-drawing characters. Imagine box lines like this:
13924 ** Each box characters has between 2 and 4 of the lines leading from
13925 ** the center. The characters are here identified by the numbers of
13926 ** their corresponding lines.
13928 #define BOX_24 "\342\224\200" /* U+2500 --- */
13929 #define BOX_13 "\342\224\202" /* U+2502 | */
13930 #define BOX_23 "\342\224\214" /* U+250c ,- */
13931 #define BOX_34 "\342\224\220" /* U+2510 -, */
13932 #define BOX_12 "\342\224\224" /* U+2514 '- */
13933 #define BOX_14 "\342\224\230" /* U+2518 -' */
13934 #define BOX_123 "\342\224\234" /* U+251c |- */
13935 #define BOX_134 "\342\224\244" /* U+2524 -| */
13936 #define BOX_234 "\342\224\254" /* U+252c -,- */
13937 #define BOX_124 "\342\224\264" /* U+2534 -'- */
13938 #define BOX_1234 "\342\224\274" /* U+253c -|- */
13940 /* Draw horizontal line N characters long using unicode box
13943 static void print_box_line(FILE *out, int N){
13944 const char zDash[] =
13945 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13946 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13947 const int nDash = sizeof(zDash) - 1;
13950 utf8_printf(out, zDash);
13953 utf8_printf(out, "%.*s", N, zDash);
13957 ** Draw a horizontal separator for a MODE_Box table.
13959 static void print_box_row_separator(
13968 utf8_printf(p->out, "%s", zSep1);
13969 print_box_line(p->out, p->actualWidth[0]+2);
13970 for(i=1; i<nArg; i++){
13971 utf8_printf(p->out, "%s", zSep2);
13972 print_box_line(p->out, p->actualWidth[i]+2);
13974 utf8_printf(p->out, "%s", zSep3);
13976 fputs("\n", p->out);
13982 ** Run a prepared statement and output the result in one of the
13983 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13986 ** This is different from ordinary exec_prepared_stmt() in that
13987 ** it has to run the entire query and gather the results into memory
13988 ** first, in order to determine column widths, before providing
13991 static void exec_prepared_stmt_columnar(
13992 ShellState *p, /* Pointer to ShellState */
13993 sqlite3_stmt *pStmt /* Statment to run */
13995 sqlite3_int64 nRow = 0;
13998 sqlite3_int64 nAlloc = 0;
14001 sqlite3_int64 i, nData;
14002 int j, nTotal, w, n;
14003 const char *colSep = 0;
14004 const char *rowSep = 0;
14006 rc = sqlite3_step(pStmt);
14007 if( rc!=SQLITE_ROW ) return;
14008 nColumn = sqlite3_column_count(pStmt);
14009 nAlloc = nColumn*4;
14010 if( nAlloc<=0 ) nAlloc = 1;
14011 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
14012 if( azData==0 ) shell_out_of_memory();
14013 for(i=0; i<nColumn; i++){
14014 azData[i] = strdup(sqlite3_column_name(pStmt,i));
14017 if( (nRow+2)*nColumn >= nAlloc ){
14019 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
14020 if( azData==0 ) shell_out_of_memory();
14023 for(i=0; i<nColumn; i++){
14024 z = (const char*)sqlite3_column_text(pStmt,i);
14025 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
14027 }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
14028 if( nColumn>p->nWidth ){
14029 p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
14030 if( p->colWidth==0 ) shell_out_of_memory();
14031 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
14032 p->nWidth = nColumn;
14033 p->actualWidth = &p->colWidth[nColumn];
14035 memset(p->actualWidth, 0, nColumn*sizeof(int));
14036 for(i=0; i<nColumn; i++){
14037 w = p->colWidth[i];
14039 p->actualWidth[i] = w;
14041 nTotal = nColumn*(nRow+1);
14042 for(i=0; i<nTotal; i++){
14044 if( z==0 ) z = p->nullValue;
14047 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
14049 if( seenInterrupt ) goto columnar_end;
14050 if( nColumn==0 ) goto columnar_end;
14051 switch( p->cMode ){
14052 case MODE_Column: {
14055 if( p->showHeader ){
14056 for(i=0; i<nColumn; i++){
14057 w = p->actualWidth[i];
14058 if( p->colWidth[i]<0 ) w = -w;
14059 utf8_width_print(p->out, w, azData[i]);
14060 fputs(i==nColumn-1?"\n":" ", p->out);
14062 for(i=0; i<nColumn; i++){
14063 print_dashes(p->out, p->actualWidth[i]);
14064 fputs(i==nColumn-1?"\n":" ", p->out);
14072 print_row_separator(p, nColumn, "+");
14073 fputs("| ", p->out);
14074 for(i=0; i<nColumn; i++){
14075 w = p->actualWidth[i];
14076 n = strlenChar(azData[i]);
14077 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14078 fputs(i==nColumn-1?" |\n":" | ", p->out);
14080 print_row_separator(p, nColumn, "+");
14083 case MODE_Markdown: {
14086 fputs("| ", p->out);
14087 for(i=0; i<nColumn; i++){
14088 w = p->actualWidth[i];
14089 n = strlenChar(azData[i]);
14090 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
14091 fputs(i==nColumn-1?" |\n":" | ", p->out);
14093 print_row_separator(p, nColumn, "|");
14097 colSep = " " BOX_13 " ";
14098 rowSep = " " BOX_13 "\n";
14099 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
14100 utf8_printf(p->out, BOX_13 " ");
14101 for(i=0; i<nColumn; i++){
14102 w = p->actualWidth[i];
14103 n = strlenChar(azData[i]);
14104 utf8_printf(p->out, "%*s%s%*s%s",
14105 (w-n)/2, "", azData[i], (w-n+1)/2, "",
14106 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
14108 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
14112 for(i=nColumn, j=0; i<nTotal; i++, j++){
14113 if( j==0 && p->cMode!=MODE_Column ){
14114 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
14117 if( z==0 ) z = p->nullValue;
14118 w = p->actualWidth[j];
14119 if( p->colWidth[j]<0 ) w = -w;
14120 utf8_width_print(p->out, w, z);
14121 if( j==nColumn-1 ){
14122 utf8_printf(p->out, "%s", rowSep);
14124 if( seenInterrupt ) goto columnar_end;
14126 utf8_printf(p->out, "%s", colSep);
14129 if( p->cMode==MODE_Table ){
14130 print_row_separator(p, nColumn, "+");
14131 }else if( p->cMode==MODE_Box ){
14132 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
14135 if( seenInterrupt ){
14136 utf8_printf(p->out, "Interrupt\n");
14138 nData = (nRow+1)*nColumn;
14139 for(i=0; i<nData; i++) free(azData[i]);
14140 sqlite3_free(azData);
14144 ** Run a prepared statement
14146 static void exec_prepared_stmt(
14147 ShellState *pArg, /* Pointer to ShellState */
14148 sqlite3_stmt *pStmt /* Statment to run */
14152 if( pArg->cMode==MODE_Column
14153 || pArg->cMode==MODE_Table
14154 || pArg->cMode==MODE_Box
14155 || pArg->cMode==MODE_Markdown
14157 exec_prepared_stmt_columnar(pArg, pStmt);
14161 /* perform the first step. this will tell us if we
14162 ** have a result set or not and how wide it is.
14164 rc = sqlite3_step(pStmt);
14165 /* if we have a result set... */
14166 if( SQLITE_ROW == rc ){
14167 /* allocate space for col name ptr, value ptr, and type */
14168 int nCol = sqlite3_column_count(pStmt);
14169 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
14173 char **azCols = (char **)pData; /* Names of result columns */
14174 char **azVals = &azCols[nCol]; /* Results */
14175 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
14177 assert(sizeof(int) <= sizeof(char *));
14178 /* save off ptrs to column names */
14179 for(i=0; i<nCol; i++){
14180 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
14183 /* extract the data and data types */
14184 for(i=0; i<nCol; i++){
14185 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
14186 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
14189 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
14191 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
14193 break; /* from for */
14197 /* if data and types extracted successfully... */
14198 if( SQLITE_ROW == rc ){
14199 /* call the supplied callback with the result row data */
14200 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
14203 rc = sqlite3_step(pStmt);
14206 } while( SQLITE_ROW == rc );
14207 sqlite3_free(pData);
14208 if( pArg->cMode==MODE_Json ){
14209 fputs("]\n", pArg->out);
14215 #ifndef SQLITE_OMIT_VIRTUALTABLE
14217 ** This function is called to process SQL if the previous shell command
14218 ** was ".expert". It passes the SQL in the second argument directly to
14219 ** the sqlite3expert object.
14221 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14222 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14223 ** an English language error message. It is the responsibility of the
14224 ** caller to eventually free this buffer using sqlite3_free().
14226 static int expertHandleSQL(
14227 ShellState *pState,
14231 assert( pState->expert.pExpert );
14232 assert( pzErr==0 || *pzErr==0 );
14233 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
14237 ** This function is called either to silently clean up the object
14238 ** created by the ".expert" command (if bCancel==1), or to generate a
14239 ** report from it and then clean it up (if bCancel==0).
14241 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
14242 ** code. In this case, (*pzErr) may be set to point to a buffer containing
14243 ** an English language error message. It is the responsibility of the
14244 ** caller to eventually free this buffer using sqlite3_free().
14246 static int expertFinish(
14247 ShellState *pState,
14251 int rc = SQLITE_OK;
14252 sqlite3expert *p = pState->expert.pExpert;
14254 assert( bCancel || pzErr==0 || *pzErr==0 );
14256 FILE *out = pState->out;
14257 int bVerbose = pState->expert.bVerbose;
14259 rc = sqlite3_expert_analyze(p, pzErr);
14260 if( rc==SQLITE_OK ){
14261 int nQuery = sqlite3_expert_count(p);
14265 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
14266 raw_printf(out, "-- Candidates -----------------------------\n");
14267 raw_printf(out, "%s\n", zCand);
14269 for(i=0; i<nQuery; i++){
14270 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
14271 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
14272 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
14273 if( zIdx==0 ) zIdx = "(no new indexes)\n";
14275 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
14276 raw_printf(out, "%s\n\n", zSql);
14278 raw_printf(out, "%s\n", zIdx);
14279 raw_printf(out, "%s\n", zEQP);
14283 sqlite3_expert_destroy(p);
14284 pState->expert.pExpert = 0;
14289 ** Implementation of ".expert" dot command.
14291 static int expertDotCommand(
14292 ShellState *pState, /* Current shell tool state */
14293 char **azArg, /* Array of arguments passed to dot command */
14294 int nArg /* Number of entries in azArg[] */
14296 int rc = SQLITE_OK;
14301 assert( pState->expert.pExpert==0 );
14302 memset(&pState->expert, 0, sizeof(ExpertInfo));
14304 for(i=1; rc==SQLITE_OK && i<nArg; i++){
14305 char *z = azArg[i];
14307 if( z[0]=='-' && z[1]=='-' ) z++;
14309 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
14310 pState->expert.bVerbose = 1;
14312 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
14314 raw_printf(stderr, "option requires an argument: %s\n", z);
14317 iSample = (int)integerValue(azArg[++i]);
14318 if( iSample<0 || iSample>100 ){
14319 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
14325 raw_printf(stderr, "unknown option: %s\n", z);
14330 if( rc==SQLITE_OK ){
14331 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
14332 if( pState->expert.pExpert==0 ){
14333 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
14336 sqlite3_expert_config(
14337 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
14344 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
14347 ** Execute a statement or set of statements. Print
14348 ** any result rows/columns depending on the current mode
14349 ** set via the supplied callback.
14351 ** This is very similar to SQLite's built-in sqlite3_exec()
14352 ** function except it takes a slightly different callback
14353 ** and callback data argument.
14355 static int shell_exec(
14356 ShellState *pArg, /* Pointer to ShellState */
14357 const char *zSql, /* SQL to be evaluated */
14358 char **pzErrMsg /* Error msg written here */
14360 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
14361 int rc = SQLITE_OK; /* Return Code */
14363 const char *zLeftover; /* Tail of unprocessed SQL */
14364 sqlite3 *db = pArg->db;
14370 #ifndef SQLITE_OMIT_VIRTUALTABLE
14371 if( pArg->expert.pExpert ){
14372 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
14373 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
14377 while( zSql[0] && (SQLITE_OK == rc) ){
14378 static const char *zStmtSql;
14379 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
14380 if( SQLITE_OK != rc ){
14382 *pzErrMsg = save_err_msg(db);
14386 /* this happens for a comment or white-space */
14388 while( IsSpace(zSql[0]) ) zSql++;
14391 zStmtSql = sqlite3_sql(pStmt);
14392 if( zStmtSql==0 ) zStmtSql = "";
14393 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
14395 /* save off the prepared statment handle and reset row count */
14397 pArg->pStmt = pStmt;
14401 /* echo the sql statement if echo on */
14402 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
14403 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
14406 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
14407 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
14408 sqlite3_stmt *pExplain;
14410 int triggerEQP = 0;
14411 disable_debug_trace_modes();
14412 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
14413 if( pArg->autoEQP>=AUTOEQP_trigger ){
14414 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
14416 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
14417 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14418 if( rc==SQLITE_OK ){
14419 while( sqlite3_step(pExplain)==SQLITE_ROW ){
14420 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
14421 int iEqpId = sqlite3_column_int(pExplain, 0);
14422 int iParentId = sqlite3_column_int(pExplain, 1);
14423 if( zEQPLine==0 ) zEQPLine = "";
14424 if( zEQPLine[0]=='-' ) eqp_render(pArg);
14425 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
14429 sqlite3_finalize(pExplain);
14430 sqlite3_free(zEQP);
14431 if( pArg->autoEQP>=AUTOEQP_full ){
14432 /* Also do an EXPLAIN for ".eqp full" mode */
14433 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
14434 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14435 if( rc==SQLITE_OK ){
14436 pArg->cMode = MODE_Explain;
14437 explain_data_prepare(pArg, pExplain);
14438 exec_prepared_stmt(pArg, pExplain);
14439 explain_data_delete(pArg);
14441 sqlite3_finalize(pExplain);
14442 sqlite3_free(zEQP);
14444 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
14445 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
14446 /* Reprepare pStmt before reactiving trace modes */
14447 sqlite3_finalize(pStmt);
14448 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
14449 if( pArg ) pArg->pStmt = pStmt;
14451 restore_debug_trace_modes();
14455 pArg->cMode = pArg->mode;
14456 if( pArg->autoExplain ){
14457 if( sqlite3_stmt_isexplain(pStmt)==1 ){
14458 pArg->cMode = MODE_Explain;
14460 if( sqlite3_stmt_isexplain(pStmt)==2 ){
14461 pArg->cMode = MODE_EQP;
14465 /* If the shell is currently in ".explain" mode, gather the extra
14466 ** data required to add indents to the output.*/
14467 if( pArg->cMode==MODE_Explain ){
14468 explain_data_prepare(pArg, pStmt);
14472 bind_prepared_stmt(pArg, pStmt);
14473 exec_prepared_stmt(pArg, pStmt);
14474 explain_data_delete(pArg);
14477 /* print usage stats if stats on */
14478 if( pArg && pArg->statsOn ){
14479 display_stats(db, pArg, 0);
14482 /* print loop-counters if required */
14483 if( pArg && pArg->scanstatsOn ){
14484 display_scanstats(db, pArg);
14487 /* Finalize the statement just executed. If this fails, save a
14488 ** copy of the error message. Otherwise, set zSql to point to the
14489 ** next statement to execute. */
14490 rc2 = sqlite3_finalize(pStmt);
14491 if( rc!=SQLITE_NOMEM ) rc = rc2;
14492 if( rc==SQLITE_OK ){
14494 while( IsSpace(zSql[0]) ) zSql++;
14495 }else if( pzErrMsg ){
14496 *pzErrMsg = save_err_msg(db);
14499 /* clear saved stmt handle */
14501 pArg->pStmt = NULL;
14510 ** Release memory previously allocated by tableColumnList().
14512 static void freeColumnList(char **azCol){
14514 for(i=1; azCol[i]; i++){
14515 sqlite3_free(azCol[i]);
14517 /* azCol[0] is a static string */
14518 sqlite3_free(azCol);
14522 ** Return a list of pointers to strings which are the names of all
14523 ** columns in table zTab. The memory to hold the names is dynamically
14524 ** allocated and must be released by the caller using a subsequent call
14525 ** to freeColumnList().
14527 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
14528 ** value that needs to be preserved, then azCol[0] is filled in with the
14529 ** name of the rowid column.
14531 ** The first regular column in the table is azCol[1]. The list is terminated
14532 ** by an entry with azCol[i]==0.
14534 static char **tableColumnList(ShellState *p, const char *zTab){
14536 sqlite3_stmt *pStmt;
14540 int nPK = 0; /* Number of PRIMARY KEY columns seen */
14541 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
14542 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
14545 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
14546 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14547 sqlite3_free(zSql);
14549 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14550 if( nCol>=nAlloc-2 ){
14551 nAlloc = nAlloc*2 + nCol + 10;
14552 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
14553 if( azCol==0 ) shell_out_of_memory();
14555 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
14556 if( sqlite3_column_int(pStmt, 5) ){
14559 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
14568 sqlite3_finalize(pStmt);
14569 if( azCol==0 ) return 0;
14573 /* The decision of whether or not a rowid really needs to be preserved
14574 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
14575 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
14576 ** rowids on tables where the rowid is inaccessible because there are other
14577 ** columns in the table named "rowid", "_rowid_", and "oid".
14579 if( preserveRowid && isIPK ){
14580 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
14581 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
14582 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
14583 ** ROWID aliases. To distinguish these cases, check to see if
14584 ** there is a "pk" entry in "PRAGMA index_list". There will be
14585 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
14587 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
14588 " WHERE origin='pk'", zTab);
14589 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
14590 sqlite3_free(zSql);
14592 freeColumnList(azCol);
14595 rc = sqlite3_step(pStmt);
14596 sqlite3_finalize(pStmt);
14597 preserveRowid = rc==SQLITE_ROW;
14599 if( preserveRowid ){
14600 /* Only preserve the rowid if we can find a name to use for the
14602 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
14604 for(j=0; j<3; j++){
14605 for(i=1; i<=nCol; i++){
14606 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
14609 /* At this point, we know that azRowid[j] is not the name of any
14610 ** ordinary column in the table. Verify that azRowid[j] is a valid
14611 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
14612 ** tables will fail this last check */
14613 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
14614 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
14623 ** Toggle the reverse_unordered_selects setting.
14625 static void toggleSelectOrder(sqlite3 *db){
14626 sqlite3_stmt *pStmt = 0;
14629 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
14630 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14631 iSetting = sqlite3_column_int(pStmt, 0);
14633 sqlite3_finalize(pStmt);
14634 sqlite3_snprintf(sizeof(zStmt), zStmt,
14635 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
14636 sqlite3_exec(db, zStmt, 0, 0, 0);
14640 ** This is a different callback routine used for dumping the database.
14641 ** Each row received by this callback consists of a table name,
14642 ** the table type ("index" or "table") and SQL to create the table.
14643 ** This routine should print text sufficient to recreate the table.
14645 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
14647 const char *zTable;
14650 ShellState *p = (ShellState *)pArg;
14654 UNUSED_PARAMETER(azNotUsed);
14655 if( nArg!=3 || azArg==0 ) return 0;
14659 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
14660 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
14662 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
14663 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
14664 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
14665 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
14666 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
14668 }else if( dataOnly ){
14670 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
14672 if( !p->writableSchema ){
14673 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
14674 p->writableSchema = 1;
14676 zIns = sqlite3_mprintf(
14677 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
14678 "VALUES('table','%q','%q',0,'%q');",
14679 zTable, zTable, zSql);
14680 utf8_printf(p->out, "%s\n", zIns);
14681 sqlite3_free(zIns);
14684 printSchemaLine(p->out, zSql, ";\n");
14687 if( strcmp(zType, "table")==0 ){
14692 char *savedDestTable;
14695 azCol = tableColumnList(p, zTable);
14701 /* Always quote the table name, even if it appears to be pure ascii,
14702 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
14704 appendText(&sTable, zTable, quoteChar(zTable));
14705 /* If preserving the rowid, add a column list after the table name.
14706 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
14707 ** instead of the usual "INSERT INTO tab VALUES(...)".
14710 appendText(&sTable, "(", 0);
14711 appendText(&sTable, azCol[0], 0);
14712 for(i=1; azCol[i]; i++){
14713 appendText(&sTable, ",", 0);
14714 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
14716 appendText(&sTable, ")", 0);
14719 /* Build an appropriate SELECT statement */
14720 initText(&sSelect);
14721 appendText(&sSelect, "SELECT ", 0);
14723 appendText(&sSelect, azCol[0], 0);
14724 appendText(&sSelect, ",", 0);
14726 for(i=1; azCol[i]; i++){
14727 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
14729 appendText(&sSelect, ",", 0);
14732 freeColumnList(azCol);
14733 appendText(&sSelect, " FROM ", 0);
14734 appendText(&sSelect, zTable, quoteChar(zTable));
14736 savedDestTable = p->zDestTable;
14737 savedMode = p->mode;
14738 p->zDestTable = sTable.z;
14739 p->mode = p->cMode = MODE_Insert;
14740 rc = shell_exec(p, sSelect.z, 0);
14741 if( (rc&0xff)==SQLITE_CORRUPT ){
14742 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14743 toggleSelectOrder(p->db);
14744 shell_exec(p, sSelect.z, 0);
14745 toggleSelectOrder(p->db);
14747 p->zDestTable = savedDestTable;
14748 p->mode = savedMode;
14750 freeText(&sSelect);
14751 if( rc ) p->nErr++;
14757 ** Run zQuery. Use dump_callback() as the callback routine so that
14758 ** the contents of the query are output as SQL statements.
14760 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
14761 ** "ORDER BY rowid DESC" to the end.
14763 static int run_schema_dump_query(
14769 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
14770 if( rc==SQLITE_CORRUPT ){
14772 int len = strlen30(zQuery);
14773 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
14775 utf8_printf(p->out, "/****** %s ******/\n", zErr);
14776 sqlite3_free(zErr);
14779 zQ2 = malloc( len+100 );
14780 if( zQ2==0 ) return rc;
14781 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
14782 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
14784 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
14786 rc = SQLITE_CORRUPT;
14788 sqlite3_free(zErr);
14795 ** Text of help messages.
14797 ** The help text for each individual command begins with a line that starts
14798 ** with ".". Subsequent lines are supplimental information.
14800 ** There must be two or more spaces between the end of the command and the
14801 ** start of the description of what that command does.
14803 static const char *(azHelp[]) = {
14804 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
14805 ".archive ... Manage SQL archives",
14806 " Each command must have exactly one of the following options:",
14807 " -c, --create Create a new archive",
14808 " -u, --update Add or update files with changed mtime",
14809 " -i, --insert Like -u but always add even if unchanged",
14810 " -t, --list List contents of archive",
14811 " -x, --extract Extract files from archive",
14812 " Optional arguments:",
14813 " -v, --verbose Print each filename as it is processed",
14814 " -f FILE, --file FILE Use archive FILE (default is current db)",
14815 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
14816 " -C DIR, --directory DIR Read/extract files from directory DIR",
14817 " -n, --dryrun Show the SQL that would have occurred",
14819 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
14820 " .ar -tf ARCHIVE # List members of ARCHIVE",
14821 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
14823 " http://sqlite.org/cli.html#sqlite_archive_support",
14825 #ifndef SQLITE_OMIT_AUTHORIZATION
14826 ".auth ON|OFF Show authorizer callbacks",
14828 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
14829 " --append Use the appendvfs",
14830 " --async Write to FILE without journal and fsync()",
14831 ".bail on|off Stop after hitting an error. Default OFF",
14832 ".binary on|off Turn binary output on or off. Default OFF",
14833 ".cd DIRECTORY Change the working directory to DIRECTORY",
14834 ".changes on|off Show number of rows changed by SQL",
14835 ".check GLOB Fail if output since .testcase does not match",
14836 ".clone NEWDB Clone data into NEWDB from the existing database",
14837 ".databases List names and files of attached databases",
14838 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
14839 ".dbinfo ?DB? Show status information about the database",
14840 ".dump ?OBJECTS? Render database content as SQL",
14842 " --data-only Output only INSERT statements",
14843 " --newlines Allow unescaped newline characters in output",
14844 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
14845 " --preserve-rowids Include ROWID values in the output",
14846 " OBJECTS is a LIKE pattern for tables, indexes, triggers or views to dump",
14847 " Additional LIKE patterns can be given in subsequent arguments",
14848 ".echo on|off Turn command echo on or off",
14849 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
14851 #ifdef SQLITE_DEBUG
14852 " test Show raw EXPLAIN QUERY PLAN output",
14853 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
14855 " trigger Like \"full\" but also show trigger bytecode",
14856 ".excel Display the output of next command in spreadsheet",
14857 " --bom Put a UTF8 byte-order mark on intermediate file",
14858 ".exit ?CODE? Exit this program with return-code CODE",
14859 ".expert EXPERIMENTAL. Suggest indexes for queries",
14860 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
14861 ".filectrl CMD ... Run various sqlite3_file_control() operations",
14862 " --schema SCHEMA Use SCHEMA instead of \"main\"",
14863 " --help Show CMD details",
14864 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
14865 ".headers on|off Turn display of headers on or off",
14866 ".help ?-all? ?PATTERN? Show help text for PATTERN",
14867 ".import FILE TABLE Import data from FILE into TABLE",
14869 " --ascii Use \\037 and \\036 as column and row separators",
14870 " --csv Use , and \\n as column and row separators",
14871 " --skip N Skip the first N rows of input",
14872 " -v \"Verbose\" - increase auxiliary output",
14874 " * If TABLE does not exist, it is created. The first row of input",
14875 " determines the column names.",
14876 " * If neither --csv or --ascii are used, the input mode is derived",
14877 " from the \".mode\" output mode",
14878 " * If FILE begins with \"|\" then it is a command that generates the",
14880 #ifndef SQLITE_OMIT_TEST_CONTROL
14881 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
14883 ".indexes ?TABLE? Show names of indexes",
14884 " If TABLE is specified, only show indexes for",
14885 " tables matching TABLE using the LIKE operator.",
14886 #ifdef SQLITE_ENABLE_IOTRACE
14887 ".iotrace FILE Enable I/O diagnostic logging to FILE",
14889 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
14890 ".lint OPTIONS Report potential schema issues.",
14892 " fkey-indexes Find missing foreign key indexes",
14893 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14894 ".load FILE ?ENTRY? Load an extension library",
14896 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
14897 ".mode MODE ?TABLE? Set output mode",
14898 " MODE is one of:",
14899 " ascii Columns/rows delimited by 0x1F and 0x1E",
14900 " box Tables using unicode box-drawing characters",
14901 " csv Comma-separated values",
14902 " column Output in columns. (See .width)",
14903 " html HTML <table> code",
14904 " insert SQL insert statements for TABLE",
14905 " json Results in a JSON array",
14906 " line One value per line",
14907 " list Values delimited by \"|\"",
14908 " markdown Markdown table format",
14909 " quote Escape answers as for SQL",
14910 " table ASCII-art table",
14911 " tabs Tab-separated values",
14912 " tcl TCL list elements",
14913 ".nullvalue STRING Use STRING in place of NULL values",
14914 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
14915 " If FILE begins with '|' then open as a pipe",
14916 " --bom Put a UTF8 byte-order mark at the beginning",
14917 " -e Send output to the system text editor",
14918 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
14919 #ifdef SQLITE_DEBUG
14920 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
14922 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
14924 " --append Use appendvfs to append database to the end of FILE",
14925 #ifndef SQLITE_OMIT_DESERIALIZE
14926 " --deserialize Load into memory using sqlite3_deserialize()",
14927 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
14928 " --maxsize N Maximum size for --hexdb or --deserialized database",
14930 " --new Initialize FILE to an empty database",
14931 " --nofollow Do not follow symbolic links",
14932 " --readonly Open FILE readonly",
14933 " --zip FILE is a ZIP archive",
14934 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
14935 " If FILE begins with '|' then open it as a pipe.",
14937 " --bom Prefix output with a UTF8 byte-order mark",
14938 " -e Send output to the system text editor",
14939 " -x Send output as CSV to a spreadsheet",
14940 ".parameter CMD ... Manage SQL parameter bindings",
14941 " clear Erase all bindings",
14942 " init Initialize the TEMP table that holds bindings",
14943 " list List the current parameter bindings",
14944 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
14945 " PARAMETER should start with one of: $ : @ ?",
14946 " unset PARAMETER Remove PARAMETER from the binding table",
14947 ".print STRING... Print literal STRING",
14948 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14949 ".progress N Invoke progress handler after every N opcodes",
14950 " --limit N Interrupt after N progress callbacks",
14951 " --once Do no more than one progress interrupt",
14952 " --quiet|-q No output except at interrupts",
14953 " --reset Reset the count for each input and interrupt",
14955 ".prompt MAIN CONTINUE Replace the standard prompts",
14956 ".quit Exit this program",
14957 ".read FILE Read input from FILE",
14958 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14959 ".recover Recover as much data as possible from corrupt db.",
14960 " --freelist-corrupt Assume the freelist is corrupt",
14961 " --recovery-db NAME Store recovery metadata in database file NAME",
14962 " --lost-and-found TABLE Alternative name for the lost-and-found table",
14963 " --no-rowids Do not attempt to recover rowid values",
14964 " that are not also INTEGER PRIMARY KEYs",
14966 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
14967 ".save FILE Write in-memory database into FILE",
14968 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
14969 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
14971 " --indent Try to pretty-print the schema",
14972 " --nosys Omit objects whose names start with \"sqlite_\"",
14973 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
14975 " --init Create a new SELFTEST table",
14976 " -v Verbose output",
14977 ".separator COL ?ROW? Change the column and row separators",
14978 #if defined(SQLITE_ENABLE_SESSION)
14979 ".session ?NAME? CMD ... Create or control sessions",
14981 " attach TABLE Attach TABLE",
14982 " changeset FILE Write a changeset into FILE",
14983 " close Close one session",
14984 " enable ?BOOLEAN? Set or query the enable bit",
14985 " filter GLOB... Reject tables matching GLOBs",
14986 " indirect ?BOOLEAN? Mark or query the indirect status",
14987 " isempty Query whether the session is empty",
14988 " list List currently open session names",
14989 " open DB NAME Open a new session on DB",
14990 " patchset FILE Write a patchset into FILE",
14991 " If ?NAME? is omitted, the first defined session is used.",
14993 ".sha3sum ... Compute a SHA3 hash of database content",
14995 " --schema Also hash the sqlite_schema table",
14996 " --sha3-224 Use the sha3-224 algorithm",
14997 " --sha3-256 Use the sha3-256 algorithm (default)",
14998 " --sha3-384 Use the sha3-384 algorithm",
14999 " --sha3-512 Use the sha3-512 algorithm",
15000 " Any other argument is a LIKE pattern for tables to hash",
15001 #ifndef SQLITE_NOHAVE_SYSTEM
15002 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
15004 ".show Show the current values for various settings",
15005 ".stats ?ARG? Show stats or turn stats on or off",
15006 " off Turn off automatic stat display",
15007 " on Turn on automatic stat display",
15008 " stmt Show statement stats",
15009 " vmstep Show the virtual machine step count only",
15010 #ifndef SQLITE_NOHAVE_SYSTEM
15011 ".system CMD ARGS... Run CMD ARGS... in a system shell",
15013 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
15014 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
15015 ".testctrl CMD ... Run various sqlite3_test_control() operations",
15016 " Run \".testctrl\" with no arguments for details",
15017 ".timeout MS Try opening locked tables for MS milliseconds",
15018 ".timer on|off Turn SQL timer on or off",
15019 #ifndef SQLITE_OMIT_TRACE
15020 ".trace ?OPTIONS? Output each SQL statement as it is run",
15021 " FILE Send output to FILE",
15022 " stdout Send output to stdout",
15023 " stderr Send output to stderr",
15024 " off Disable tracing",
15025 " --expanded Expand query parameters",
15026 #ifdef SQLITE_ENABLE_NORMALIZE
15027 " --normalized Normal the SQL statements",
15029 " --plain Show SQL as it is input",
15030 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
15031 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
15032 " --row Trace each row (SQLITE_TRACE_ROW)",
15033 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
15034 #endif /* SQLITE_OMIT_TRACE */
15035 #ifdef SQLITE_DEBUG
15036 ".unmodule NAME ... Unregister virtual table modules",
15037 " --allexcept Unregister everything except those named",
15039 ".vfsinfo ?AUX? Information about the top-level VFS",
15040 ".vfslist List all available VFSes",
15041 ".vfsname ?AUX? Print the name of the VFS stack",
15042 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
15043 " Negative values right-justify",
15047 ** Output help text.
15049 ** zPattern describes the set of commands for which help text is provided.
15050 ** If zPattern is NULL, then show all commands, but only give a one-line
15051 ** description of each.
15053 ** Return the number of matches.
15055 static int showHelp(FILE *out, const char *zPattern){
15061 || zPattern[0]=='0'
15062 || strcmp(zPattern,"-a")==0
15063 || strcmp(zPattern,"-all")==0
15064 || strcmp(zPattern,"--all")==0
15066 /* Show all commands, but only one line per command */
15067 if( zPattern==0 ) zPattern = "";
15068 for(i=0; i<ArraySize(azHelp); i++){
15069 if( azHelp[i][0]=='.' || zPattern[0] ){
15070 utf8_printf(out, "%s\n", azHelp[i]);
15075 /* Look for commands that for which zPattern is an exact prefix */
15076 zPat = sqlite3_mprintf(".%s*", zPattern);
15077 for(i=0; i<ArraySize(azHelp); i++){
15078 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
15079 utf8_printf(out, "%s\n", azHelp[i]);
15084 sqlite3_free(zPat);
15087 /* when zPattern is a prefix of exactly one command, then include the
15088 ** details of that command, which should begin at offset j */
15089 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
15090 utf8_printf(out, "%s\n", azHelp[j]);
15096 /* Look for commands that contain zPattern anywhere. Show the complete
15097 ** text of all commands that match. */
15098 zPat = sqlite3_mprintf("%%%s%%", zPattern);
15099 for(i=0; i<ArraySize(azHelp); i++){
15100 if( azHelp[i][0]=='.' ) j = i;
15101 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
15102 utf8_printf(out, "%s\n", azHelp[j]);
15103 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
15105 utf8_printf(out, "%s\n", azHelp[j]);
15111 sqlite3_free(zPat);
15116 /* Forward reference */
15117 static int process_input(ShellState *p);
15120 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
15121 ** and return a pointer to the buffer. The caller is responsible for freeing
15124 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
15127 ** For convenience, a nul-terminator byte is always appended to the data read
15128 ** from the file before the buffer is returned. This byte is not included in
15129 ** the final value of (*pnByte), if applicable.
15131 ** NULL is returned if any error is encountered. The final value of *pnByte
15132 ** is undefined in this case.
15134 static char *readFile(const char *zName, int *pnByte){
15135 FILE *in = fopen(zName, "rb");
15139 if( in==0 ) return 0;
15140 fseek(in, 0, SEEK_END);
15143 pBuf = sqlite3_malloc64( nIn+1 );
15144 if( pBuf==0 ){ fclose(in); return 0; }
15145 nRead = fread(pBuf, nIn, 1, in);
15148 sqlite3_free(pBuf);
15152 if( pnByte ) *pnByte = nIn;
15156 #if defined(SQLITE_ENABLE_SESSION)
15158 ** Close a single OpenSession object and release all of its associated
15161 static void session_close(OpenSession *pSession){
15163 sqlite3session_delete(pSession->p);
15164 sqlite3_free(pSession->zName);
15165 for(i=0; i<pSession->nFilter; i++){
15166 sqlite3_free(pSession->azFilter[i]);
15168 sqlite3_free(pSession->azFilter);
15169 memset(pSession, 0, sizeof(OpenSession));
15174 ** Close all OpenSession objects and release all associated resources.
15176 #if defined(SQLITE_ENABLE_SESSION)
15177 static void session_close_all(ShellState *p){
15179 for(i=0; i<p->nSession; i++){
15180 session_close(&p->aSession[i]);
15185 # define session_close_all(X)
15189 ** Implementation of the xFilter function for an open session. Omit
15190 ** any tables named by ".session filter" but let all other table through.
15192 #if defined(SQLITE_ENABLE_SESSION)
15193 static int session_filter(void *pCtx, const char *zTab){
15194 OpenSession *pSession = (OpenSession*)pCtx;
15196 for(i=0; i<pSession->nFilter; i++){
15197 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
15204 ** Try to deduce the type of file for zName based on its content. Return
15205 ** one of the SHELL_OPEN_* constants.
15207 ** If the file does not exist or is empty but its name looks like a ZIP
15208 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
15209 ** Otherwise, assume an ordinary database regardless of the filename if
15210 ** the type cannot be determined from content.
15212 int deduceDatabaseType(const char *zName, int dfltZip){
15213 FILE *f = fopen(zName, "rb");
15215 int rc = SHELL_OPEN_UNSPEC;
15218 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15219 return SHELL_OPEN_ZIPFILE;
15221 return SHELL_OPEN_NORMAL;
15224 n = fread(zBuf, 16, 1, f);
15225 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
15227 return SHELL_OPEN_NORMAL;
15229 fseek(f, -25, SEEK_END);
15230 n = fread(zBuf, 25, 1, f);
15231 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
15232 rc = SHELL_OPEN_APPENDVFS;
15234 fseek(f, -22, SEEK_END);
15235 n = fread(zBuf, 22, 1, f);
15236 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
15237 && zBuf[3]==0x06 ){
15238 rc = SHELL_OPEN_ZIPFILE;
15239 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
15240 rc = SHELL_OPEN_ZIPFILE;
15247 #ifndef SQLITE_OMIT_DESERIALIZE
15249 ** Reconstruct an in-memory database using the output from the "dbtotxt"
15250 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
15251 ** is 0, then read from standard input.
15253 static unsigned char *readHexDb(ShellState *p, int *pnData){
15254 unsigned char *a = 0;
15262 unsigned int x[16];
15264 if( p->zDbFilename ){
15265 in = fopen(p->zDbFilename, "r");
15267 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
15274 if( in==0 ) in = stdin;
15278 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
15279 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
15280 if( rc!=2 ) goto readHexDb_error;
15281 if( n<0 ) goto readHexDb_error;
15282 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
15283 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
15284 a = sqlite3_malloc( n ? n : 1 );
15286 utf8_printf(stderr, "Out of memory!\n");
15287 goto readHexDb_error;
15290 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
15291 utf8_printf(stderr, "invalid pagesize\n");
15292 goto readHexDb_error;
15294 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
15295 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
15300 if( strncmp(zLine, "| end ", 6)==0 ){
15303 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
15304 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
15305 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
15310 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
15326 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
15328 if(strncmp(zLine, "| end ", 6)==0 ) break;
15333 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
15336 #endif /* SQLITE_OMIT_DESERIALIZE */
15339 ** Scalar function "shell_int32". The first argument to this function
15340 ** must be a blob. The second a non-negative integer. This function
15341 ** reads and returns a 32-bit big-endian integer from byte
15342 ** offset (4*<arg2>) of the blob.
15344 static void shellInt32(
15345 sqlite3_context *context,
15347 sqlite3_value **argv
15349 const unsigned char *pBlob;
15353 UNUSED_PARAMETER(argc);
15354 nBlob = sqlite3_value_bytes(argv[0]);
15355 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
15356 iInt = sqlite3_value_int(argv[1]);
15358 if( iInt>=0 && (iInt+1)*4<=nBlob ){
15359 const unsigned char *a = &pBlob[iInt*4];
15360 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
15361 + ((sqlite3_int64)a[1]<<16)
15362 + ((sqlite3_int64)a[2]<< 8)
15363 + ((sqlite3_int64)a[3]<< 0);
15364 sqlite3_result_int64(context, iVal);
15369 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
15370 ** using "..." with internal double-quote characters doubled.
15372 static void shellIdQuote(
15373 sqlite3_context *context,
15375 sqlite3_value **argv
15377 const char *zName = (const char*)sqlite3_value_text(argv[0]);
15378 UNUSED_PARAMETER(argc);
15380 char *z = sqlite3_mprintf("\"%w\"", zName);
15381 sqlite3_result_text(context, z, -1, sqlite3_free);
15386 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
15388 static void shellUSleepFunc(
15389 sqlite3_context *context,
15391 sqlite3_value **argv
15393 int sleep = sqlite3_value_int(argv[0]);
15395 sqlite3_sleep(sleep/1000);
15396 sqlite3_result_int(context, sleep);
15400 ** Scalar function "shell_escape_crnl" used by the .recover command.
15401 ** The argument passed to this function is the output of built-in
15402 ** function quote(). If the first character of the input is "'",
15403 ** indicating that the value passed to quote() was a text value,
15404 ** then this function searches the input for "\n" and "\r" characters
15405 ** and adds a wrapper similar to the following:
15407 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
15409 ** Or, if the first character of the input is not "'", then a copy
15410 ** of the input is returned.
15412 static void shellEscapeCrnl(
15413 sqlite3_context *context,
15415 sqlite3_value **argv
15417 const char *zText = (const char*)sqlite3_value_text(argv[0]);
15418 UNUSED_PARAMETER(argc);
15419 if( zText[0]=='\'' ){
15420 int nText = sqlite3_value_bytes(argv[0]);
15424 const char *zNL = 0;
15425 const char *zCR = 0;
15429 for(i=0; zText[i]; i++){
15430 if( zNL==0 && zText[i]=='\n' ){
15431 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
15432 nNL = (int)strlen(zNL);
15434 if( zCR==0 && zText[i]=='\r' ){
15435 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
15436 nCR = (int)strlen(zCR);
15442 i64 nMax = (nNL > nCR) ? nNL : nCR;
15443 i64 nAlloc = nMax * nText + (nMax+64)*2;
15444 char *zOut = (char*)sqlite3_malloc64(nAlloc);
15446 sqlite3_result_error_nomem(context);
15451 memcpy(&zOut[iOut], "replace(replace(", 16);
15454 memcpy(&zOut[iOut], "replace(", 8);
15457 for(i=0; zText[i]; i++){
15458 if( zText[i]=='\n' ){
15459 memcpy(&zOut[iOut], zNL, nNL);
15461 }else if( zText[i]=='\r' ){
15462 memcpy(&zOut[iOut], zCR, nCR);
15465 zOut[iOut] = zText[i];
15471 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15472 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
15473 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
15476 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
15477 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
15478 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
15481 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
15482 sqlite3_free(zOut);
15487 sqlite3_result_value(context, argv[0]);
15490 /* Flags for open_db().
15492 ** The default behavior of open_db() is to exit(1) if the database fails to
15493 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
15494 ** but still returns without calling exit.
15496 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
15497 ** ZIP archive if the file does not exist or is empty and its name matches
15498 ** the *.zip pattern.
15500 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
15501 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
15504 ** Make sure the database is open. If it is not, then open it. If
15505 ** the database fails to open, print an error message and exit.
15507 static void open_db(ShellState *p, int openFlags){
15509 if( p->openMode==SHELL_OPEN_UNSPEC ){
15510 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
15511 p->openMode = SHELL_OPEN_NORMAL;
15513 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
15514 (openFlags & OPEN_DB_ZIPFILE)!=0);
15517 switch( p->openMode ){
15518 case SHELL_OPEN_APPENDVFS: {
15519 sqlite3_open_v2(p->zDbFilename, &p->db,
15520 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
15523 case SHELL_OPEN_HEXDB:
15524 case SHELL_OPEN_DESERIALIZE: {
15525 sqlite3_open(0, &p->db);
15528 case SHELL_OPEN_ZIPFILE: {
15529 sqlite3_open(":memory:", &p->db);
15532 case SHELL_OPEN_READONLY: {
15533 sqlite3_open_v2(p->zDbFilename, &p->db,
15534 SQLITE_OPEN_READONLY|p->openFlags, 0);
15537 case SHELL_OPEN_UNSPEC:
15538 case SHELL_OPEN_NORMAL: {
15539 sqlite3_open_v2(p->zDbFilename, &p->db,
15540 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
15545 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
15546 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
15547 p->zDbFilename, sqlite3_errmsg(p->db));
15548 if( openFlags & OPEN_DB_KEEPALIVE ){
15549 sqlite3_open(":memory:", &p->db);
15554 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15555 sqlite3_enable_load_extension(p->db, 1);
15557 sqlite3_fileio_init(p->db, 0, 0);
15558 sqlite3_shathree_init(p->db, 0, 0);
15559 sqlite3_completion_init(p->db, 0, 0);
15560 sqlite3_uint_init(p->db, 0, 0);
15561 sqlite3_decimal_init(p->db, 0, 0);
15562 sqlite3_regexp_init(p->db, 0, 0);
15563 sqlite3_ieee_init(p->db, 0, 0);
15564 sqlite3_series_init(p->db, 0, 0);
15565 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15566 sqlite3_dbdata_init(p->db, 0, 0);
15568 #ifdef SQLITE_HAVE_ZLIB
15569 sqlite3_zipfile_init(p->db, 0, 0);
15570 sqlite3_sqlar_init(p->db, 0, 0);
15572 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
15573 shellAddSchemaName, 0, 0);
15574 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
15575 shellModuleSchema, 0, 0);
15576 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
15577 shellPutsFunc, 0, 0);
15578 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
15579 shellEscapeCrnl, 0, 0);
15580 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
15582 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
15583 shellIdQuote, 0, 0);
15584 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
15585 shellUSleepFunc, 0, 0);
15586 #ifndef SQLITE_NOHAVE_SYSTEM
15587 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
15589 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
15592 if( p->openMode==SHELL_OPEN_ZIPFILE ){
15593 char *zSql = sqlite3_mprintf(
15594 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
15595 sqlite3_exec(p->db, zSql, 0, 0, 0);
15596 sqlite3_free(zSql);
15598 #ifndef SQLITE_OMIT_DESERIALIZE
15600 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
15603 unsigned char *aData;
15604 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
15605 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
15607 aData = readHexDb(p, &nData);
15612 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
15613 SQLITE_DESERIALIZE_RESIZEABLE |
15614 SQLITE_DESERIALIZE_FREEONCLOSE);
15616 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
15619 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
15627 ** Attempt to close the databaes connection. Report errors.
15629 void close_db(sqlite3 *db){
15630 int rc = sqlite3_close(db);
15632 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
15633 rc, sqlite3_errmsg(db));
15637 #if HAVE_READLINE || HAVE_EDITLINE
15639 ** Readline completion callbacks
15641 static char *readline_completion_generator(const char *text, int state){
15642 static sqlite3_stmt *pStmt = 0;
15646 sqlite3_finalize(pStmt);
15647 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15648 " FROM completion(%Q) ORDER BY 1", text);
15649 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15650 sqlite3_free(zSql);
15652 if( sqlite3_step(pStmt)==SQLITE_ROW ){
15653 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
15655 sqlite3_finalize(pStmt);
15661 static char **readline_completion(const char *zText, int iStart, int iEnd){
15662 rl_attempted_completion_over = 1;
15663 return rl_completion_matches(zText, readline_completion_generator);
15666 #elif HAVE_LINENOISE
15668 ** Linenoise completion callback
15670 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
15671 int nLine = strlen30(zLine);
15673 sqlite3_stmt *pStmt = 0;
15677 if( nLine>sizeof(zBuf)-30 ) return;
15678 if( zLine[0]=='.' || zLine[0]=='#') return;
15679 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
15680 if( i==nLine-1 ) return;
15682 memcpy(zBuf, zLine, iStart);
15683 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
15684 " FROM completion(%Q,%Q) ORDER BY 1",
15685 &zLine[iStart], zLine);
15686 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
15687 sqlite3_free(zSql);
15688 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
15689 while( sqlite3_step(pStmt)==SQLITE_ROW ){
15690 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
15691 int nCompletion = sqlite3_column_bytes(pStmt, 0);
15692 if( iStart+nCompletion < sizeof(zBuf)-1 ){
15693 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
15694 linenoiseAddCompletion(lc, zBuf);
15697 sqlite3_finalize(pStmt);
15702 ** Do C-language style dequoting.
15708 ** \v -> vertical tab
15710 ** \r -> carriage return
15715 ** \NNN -> ascii character NNN in octal
15717 static void resolve_backslashes(char *z){
15720 while( *z && *z!='\\' ) z++;
15721 for(i=j=0; (c = z[i])!=0; i++, j++){
15722 if( c=='\\' && z[i+1]!=0 ){
15726 }else if( c=='b' ){
15728 }else if( c=='t' ){
15730 }else if( c=='n' ){
15732 }else if( c=='v' ){
15734 }else if( c=='f' ){
15736 }else if( c=='r' ){
15738 }else if( c=='"' ){
15740 }else if( c=='\'' ){
15742 }else if( c=='\\' ){
15744 }else if( c>='0' && c<='7' ){
15746 if( z[i+1]>='0' && z[i+1]<='7' ){
15748 c = (c<<3) + z[i] - '0';
15749 if( z[i+1]>='0' && z[i+1]<='7' ){
15751 c = (c<<3) + z[i] - '0';
15758 if( j<i ) z[j] = 0;
15762 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
15763 ** for TRUE and FALSE. Return the integer value if appropriate.
15765 static int booleanValue(const char *zArg){
15767 if( zArg[0]=='0' && zArg[1]=='x' ){
15768 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
15770 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
15772 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
15773 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
15776 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
15779 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
15785 ** Set or clear a shell flag according to a boolean value.
15787 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
15788 if( booleanValue(zArg) ){
15789 ShellSetFlag(p, mFlag);
15791 ShellClearFlag(p, mFlag);
15796 ** Close an output file, assuming it is not stderr or stdout
15798 static void output_file_close(FILE *f){
15799 if( f && f!=stdout && f!=stderr ) fclose(f);
15803 ** Try to open an output file. The names "stdout" and "stderr" are
15804 ** recognized and do the right thing. NULL is returned if the output
15805 ** filename is "off".
15807 static FILE *output_file_open(const char *zFile, int bTextMode){
15809 if( strcmp(zFile,"stdout")==0 ){
15811 }else if( strcmp(zFile, "stderr")==0 ){
15813 }else if( strcmp(zFile, "off")==0 ){
15816 f = fopen(zFile, bTextMode ? "w" : "wb");
15818 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
15824 #ifndef SQLITE_OMIT_TRACE
15826 ** A routine for handling output from sqlite3_trace().
15828 static int sql_trace_callback(
15829 unsigned mType, /* The trace type */
15830 void *pArg, /* The ShellState pointer */
15831 void *pP, /* Usually a pointer to sqlite_stmt */
15832 void *pX /* Auxiliary output */
15834 ShellState *p = (ShellState*)pArg;
15835 sqlite3_stmt *pStmt;
15838 if( p->traceOut==0 ) return 0;
15839 if( mType==SQLITE_TRACE_CLOSE ){
15840 utf8_printf(p->traceOut, "-- closing database connection\n");
15843 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
15844 zSql = (const char*)pX;
15846 pStmt = (sqlite3_stmt*)pP;
15847 switch( p->eTraceType ){
15848 case SHELL_TRACE_EXPANDED: {
15849 zSql = sqlite3_expanded_sql(pStmt);
15852 #ifdef SQLITE_ENABLE_NORMALIZE
15853 case SHELL_TRACE_NORMALIZED: {
15854 zSql = sqlite3_normalized_sql(pStmt);
15859 zSql = sqlite3_sql(pStmt);
15864 if( zSql==0 ) return 0;
15865 nSql = strlen30(zSql);
15866 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
15868 case SQLITE_TRACE_ROW:
15869 case SQLITE_TRACE_STMT: {
15870 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
15873 case SQLITE_TRACE_PROFILE: {
15874 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
15875 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
15884 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
15885 ** a useful spot to set a debugger breakpoint.
15887 static void test_breakpoint(void){
15888 static int nCall = 0;
15893 ** An object used to read a CSV and other files for import.
15895 typedef struct ImportCtx ImportCtx;
15897 const char *zFile; /* Name of the input file */
15898 FILE *in; /* Read the CSV text from this input stream */
15899 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
15900 char *z; /* Accumulated text for a field */
15901 int n; /* Number of bytes in z */
15902 int nAlloc; /* Space allocated for z[] */
15903 int nLine; /* Current line number */
15904 int nRow; /* Number of rows imported */
15905 int nErr; /* Number of errors encountered */
15906 int bNotFirst; /* True if one or more bytes already read */
15907 int cTerm; /* Character that terminated the most recent field */
15908 int cColSep; /* The column separator character. (Usually ",") */
15909 int cRowSep; /* The row separator character. (Usually "\n") */
15912 /* Clean up resourced used by an ImportCtx */
15913 static void import_cleanup(ImportCtx *p){
15914 if( p->in!=0 && p->xCloser!=0 ){
15918 sqlite3_free(p->z);
15922 /* Append a single byte to z[] */
15923 static void import_append_char(ImportCtx *p, int c){
15924 if( p->n+1>=p->nAlloc ){
15925 p->nAlloc += p->nAlloc + 100;
15926 p->z = sqlite3_realloc64(p->z, p->nAlloc);
15927 if( p->z==0 ) shell_out_of_memory();
15929 p->z[p->n++] = (char)c;
15932 /* Read a single field of CSV text. Compatible with rfc4180 and extended
15933 ** with the option of having a separator other than ",".
15935 ** + Input comes from p->in.
15936 ** + Store results in p->z of length p->n. Space to hold p->z comes
15937 ** from sqlite3_malloc64().
15938 ** + Use p->cSep as the column separator. The default is ",".
15939 ** + Use p->rSep as the row separator. The default is "\n".
15940 ** + Keep track of the line number in p->nLine.
15941 ** + Store the character that terminates the field in p->cTerm. Store
15942 ** EOF on end-of-file.
15943 ** + Report syntax errors on stderr
15945 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15947 int cSep = p->cColSep;
15948 int rSep = p->cRowSep;
15951 if( c==EOF || seenInterrupt ){
15957 int startLine = p->nLine;
15962 if( c==rSep ) p->nLine++;
15969 if( (c==cSep && pc==cQuote)
15970 || (c==rSep && pc==cQuote)
15971 || (c==rSep && pc=='\r' && ppc==cQuote)
15972 || (c==EOF && pc==cQuote)
15974 do{ p->n--; }while( p->z[p->n]!=cQuote );
15978 if( pc==cQuote && c!='\r' ){
15979 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15980 p->zFile, p->nLine, cQuote);
15983 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15984 p->zFile, startLine, cQuote);
15988 import_append_char(p, c);
15993 /* If this is the first field being parsed and it begins with the
15994 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
15995 if( (c&0xff)==0xef && p->bNotFirst==0 ){
15996 import_append_char(p, c);
15998 if( (c&0xff)==0xbb ){
15999 import_append_char(p, c);
16001 if( (c&0xff)==0xbf ){
16004 return csv_read_one_field(p);
16008 while( c!=EOF && c!=cSep && c!=rSep ){
16009 import_append_char(p, c);
16014 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
16018 if( p->z ) p->z[p->n] = 0;
16023 /* Read a single field of ASCII delimited text.
16025 ** + Input comes from p->in.
16026 ** + Store results in p->z of length p->n. Space to hold p->z comes
16027 ** from sqlite3_malloc64().
16028 ** + Use p->cSep as the column separator. The default is "\x1F".
16029 ** + Use p->rSep as the row separator. The default is "\x1E".
16030 ** + Keep track of the row number in p->nLine.
16031 ** + Store the character that terminates the field in p->cTerm. Store
16032 ** EOF on end-of-file.
16033 ** + Report syntax errors on stderr
16035 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
16037 int cSep = p->cColSep;
16038 int rSep = p->cRowSep;
16041 if( c==EOF || seenInterrupt ){
16045 while( c!=EOF && c!=cSep && c!=rSep ){
16046 import_append_char(p, c);
16053 if( p->z ) p->z[p->n] = 0;
16058 ** Try to transfer data for table zTable. If an error is seen while
16059 ** moving forward, try to go backwards. The backwards movement won't
16060 ** work for WITHOUT ROWID tables.
16062 static void tryToCloneData(
16067 sqlite3_stmt *pQuery = 0;
16068 sqlite3_stmt *pInsert = 0;
16073 int nTable = strlen30(zTable);
16076 const int spinRate = 10000;
16078 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
16079 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16081 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16082 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16084 goto end_data_xfer;
16086 n = sqlite3_column_count(pQuery);
16087 zInsert = sqlite3_malloc64(200 + nTable + n*3);
16088 if( zInsert==0 ) shell_out_of_memory();
16089 sqlite3_snprintf(200+nTable,zInsert,
16090 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
16091 i = strlen30(zInsert);
16092 for(j=1; j<n; j++){
16093 memcpy(zInsert+i, ",?", 2);
16096 memcpy(zInsert+i, ");", 3);
16097 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
16099 utf8_printf(stderr, "Error %d: %s on [%s]\n",
16100 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
16102 goto end_data_xfer;
16104 for(k=0; k<2; k++){
16105 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16106 for(i=0; i<n; i++){
16107 switch( sqlite3_column_type(pQuery, i) ){
16108 case SQLITE_NULL: {
16109 sqlite3_bind_null(pInsert, i+1);
16112 case SQLITE_INTEGER: {
16113 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
16116 case SQLITE_FLOAT: {
16117 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
16120 case SQLITE_TEXT: {
16121 sqlite3_bind_text(pInsert, i+1,
16122 (const char*)sqlite3_column_text(pQuery,i),
16123 -1, SQLITE_STATIC);
16126 case SQLITE_BLOB: {
16127 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
16128 sqlite3_column_bytes(pQuery,i),
16134 rc = sqlite3_step(pInsert);
16135 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
16136 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
16137 sqlite3_errmsg(newDb));
16139 sqlite3_reset(pInsert);
16141 if( (cnt%spinRate)==0 ){
16142 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
16146 if( rc==SQLITE_DONE ) break;
16147 sqlite3_finalize(pQuery);
16148 sqlite3_free(zQuery);
16149 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
16151 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16153 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
16156 } /* End for(k=0...) */
16159 sqlite3_finalize(pQuery);
16160 sqlite3_finalize(pInsert);
16161 sqlite3_free(zQuery);
16162 sqlite3_free(zInsert);
16167 ** Try to transfer all rows of the schema that match zWhere. For
16168 ** each row, invoke xForEach() on the object defined by that row.
16169 ** If an error is encountered while moving forward through the
16170 ** sqlite_schema table, try again moving backwards.
16172 static void tryToCloneSchema(
16175 const char *zWhere,
16176 void (*xForEach)(ShellState*,sqlite3*,const char*)
16178 sqlite3_stmt *pQuery = 0;
16181 const unsigned char *zName;
16182 const unsigned char *zSql;
16185 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16186 " WHERE %s", zWhere);
16187 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16189 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16190 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16192 goto end_schema_xfer;
16194 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16195 zName = sqlite3_column_text(pQuery, 0);
16196 zSql = sqlite3_column_text(pQuery, 1);
16197 printf("%s... ", zName); fflush(stdout);
16198 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16200 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16201 sqlite3_free(zErrMsg);
16205 xForEach(p, newDb, (const char*)zName);
16209 if( rc!=SQLITE_DONE ){
16210 sqlite3_finalize(pQuery);
16211 sqlite3_free(zQuery);
16212 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
16213 " WHERE %s ORDER BY rowid DESC", zWhere);
16214 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
16216 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
16217 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
16219 goto end_schema_xfer;
16221 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
16222 zName = sqlite3_column_text(pQuery, 0);
16223 zSql = sqlite3_column_text(pQuery, 1);
16224 printf("%s... ", zName); fflush(stdout);
16225 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
16227 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
16228 sqlite3_free(zErrMsg);
16232 xForEach(p, newDb, (const char*)zName);
16238 sqlite3_finalize(pQuery);
16239 sqlite3_free(zQuery);
16243 ** Open a new database file named "zNewDb". Try to recover as much information
16244 ** as possible out of the main database (which might be corrupt) and write it
16247 static void tryToClone(ShellState *p, const char *zNewDb){
16249 sqlite3 *newDb = 0;
16250 if( access(zNewDb,0)==0 ){
16251 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
16254 rc = sqlite3_open(zNewDb, &newDb);
16256 utf8_printf(stderr, "Cannot create output database: %s\n",
16257 sqlite3_errmsg(newDb));
16259 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
16260 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
16261 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
16262 tryToCloneSchema(p, newDb, "type!='table'", 0);
16263 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
16264 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16270 ** Change the output file back to stdout.
16272 ** If the p->doXdgOpen flag is set, that means the output was being
16273 ** redirected to a temporary file named by p->zTempFile. In that case,
16274 ** launch start/open/xdg-open on that temporary file.
16276 static void output_reset(ShellState *p){
16277 if( p->outfile[0]=='|' ){
16278 #ifndef SQLITE_OMIT_POPEN
16282 output_file_close(p->out);
16283 #ifndef SQLITE_NOHAVE_SYSTEM
16284 if( p->doXdgOpen ){
16285 const char *zXdgOpenCmd =
16286 #if defined(_WIN32)
16288 #elif defined(__APPLE__)
16294 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
16295 if( system(zCmd) ){
16296 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
16298 /* Give the start/open/xdg-open command some time to get
16299 ** going before we continue, and potential delete the
16300 ** p->zTempFile data file out from under it */
16301 sqlite3_sleep(2000);
16303 sqlite3_free(zCmd);
16307 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
16314 ** Run an SQL command and return the single integer result.
16316 static int db_int(ShellState *p, const char *zSql){
16317 sqlite3_stmt *pStmt;
16319 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16320 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
16321 res = sqlite3_column_int(pStmt,0);
16323 sqlite3_finalize(pStmt);
16328 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
16330 static unsigned int get2byteInt(unsigned char *a){
16331 return (a[0]<<8) + a[1];
16333 static unsigned int get4byteInt(unsigned char *a){
16334 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
16338 ** Implementation of the ".dbinfo" command.
16340 ** Return 1 on error, 2 to exit, and 0 otherwise.
16342 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
16343 static const struct { const char *zName; int ofst; } aField[] = {
16344 { "file change counter:", 24 },
16345 { "database page count:", 28 },
16346 { "freelist page count:", 36 },
16347 { "schema cookie:", 40 },
16348 { "schema format:", 44 },
16349 { "default cache size:", 48 },
16350 { "autovacuum top root:", 52 },
16351 { "incremental vacuum:", 64 },
16352 { "text encoding:", 56 },
16353 { "user version:", 60 },
16354 { "application id:", 68 },
16355 { "software version:", 96 },
16357 static const struct { const char *zName; const char *zSql; } aQuery[] = {
16358 { "number of tables:",
16359 "SELECT count(*) FROM %s WHERE type='table'" },
16360 { "number of indexes:",
16361 "SELECT count(*) FROM %s WHERE type='index'" },
16362 { "number of triggers:",
16363 "SELECT count(*) FROM %s WHERE type='trigger'" },
16364 { "number of views:",
16365 "SELECT count(*) FROM %s WHERE type='view'" },
16367 "SELECT total(length(sql)) FROM %s" },
16370 unsigned iDataVersion;
16372 char *zDb = nArg>=2 ? azArg[1] : "main";
16373 sqlite3_stmt *pStmt = 0;
16374 unsigned char aHdr[100];
16376 if( p->db==0 ) return 1;
16377 rc = sqlite3_prepare_v2(p->db,
16378 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
16381 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
16382 sqlite3_finalize(pStmt);
16385 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
16386 if( sqlite3_step(pStmt)==SQLITE_ROW
16387 && sqlite3_column_bytes(pStmt,0)>100
16389 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
16390 sqlite3_finalize(pStmt);
16392 raw_printf(stderr, "unable to read database header\n");
16393 sqlite3_finalize(pStmt);
16396 i = get2byteInt(aHdr+16);
16397 if( i==1 ) i = 65536;
16398 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
16399 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
16400 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
16401 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
16402 for(i=0; i<ArraySize(aField); i++){
16403 int ofst = aField[i].ofst;
16404 unsigned int val = get4byteInt(aHdr + ofst);
16405 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
16408 if( val==1 ) raw_printf(p->out, " (utf8)");
16409 if( val==2 ) raw_printf(p->out, " (utf16le)");
16410 if( val==3 ) raw_printf(p->out, " (utf16be)");
16413 raw_printf(p->out, "\n");
16416 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
16417 }else if( strcmp(zDb,"temp")==0 ){
16418 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
16420 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
16422 for(i=0; i<ArraySize(aQuery); i++){
16423 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
16424 int val = db_int(p, zSql);
16425 sqlite3_free(zSql);
16426 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
16428 sqlite3_free(zSchemaTab);
16429 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
16430 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
16435 ** Print the current sqlite3_errmsg() value to stderr and return 1.
16437 static int shellDatabaseError(sqlite3 *db){
16438 const char *zErr = sqlite3_errmsg(db);
16439 utf8_printf(stderr, "Error: %s\n", zErr);
16444 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
16445 ** if they match and FALSE (0) if they do not match.
16449 ** '*' Matches any sequence of zero or more characters.
16451 ** '?' Matches exactly one character.
16453 ** [...] Matches one character from the enclosed list of
16456 ** [^...] Matches one character not in the enclosed list.
16458 ** '#' Matches any sequence of one or more digits with an
16459 ** optional + or - sign in front
16461 ** ' ' Any span of whitespace matches any other span of
16464 ** Extra whitespace at the end of z[] is ignored.
16466 static int testcase_glob(const char *zGlob, const char *z){
16471 while( (c = (*(zGlob++)))!=0 ){
16473 if( !IsSpace(*z) ) return 0;
16474 while( IsSpace(*zGlob) ) zGlob++;
16475 while( IsSpace(*z) ) z++;
16476 }else if( c=='*' ){
16477 while( (c=(*(zGlob++))) == '*' || c=='?' ){
16478 if( c=='?' && (*(z++))==0 ) return 0;
16482 }else if( c=='[' ){
16483 while( *z && testcase_glob(zGlob-1,z)==0 ){
16488 while( (c2 = (*(z++)))!=0 ){
16491 if( c2==0 ) return 0;
16493 if( testcase_glob(zGlob,z) ) return 1;
16496 }else if( c=='?' ){
16497 if( (*(z++))==0 ) return 0;
16498 }else if( c=='[' ){
16503 if( c==0 ) return 0;
16510 if( c==']' ) seen = 1;
16513 while( c2 && c2!=']' ){
16514 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
16516 if( c>=prior_c && c<=c2 ) seen = 1;
16526 if( c2==0 || (seen ^ invert)==0 ) return 0;
16527 }else if( c=='#' ){
16528 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
16529 if( !IsDigit(z[0]) ) return 0;
16531 while( IsDigit(z[0]) ){ z++; }
16533 if( c!=(*(z++)) ) return 0;
16536 while( IsSpace(*z) ){ z++; }
16542 ** Compare the string as a command-line option with either one or two
16543 ** initial "-" characters.
16545 static int optionMatch(const char *zStr, const char *zOpt){
16546 if( zStr[0]!='-' ) return 0;
16548 if( zStr[0]=='-' ) zStr++;
16549 return strcmp(zStr, zOpt)==0;
16555 int shellDeleteFile(const char *zFilename){
16558 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
16562 rc = unlink(zFilename);
16568 ** Try to delete the temporary file (if there is one) and free the
16569 ** memory used to hold the name of the temp file.
16571 static void clearTempFile(ShellState *p){
16572 if( p->zTempFile==0 ) return;
16573 if( p->doXdgOpen ) return;
16574 if( shellDeleteFile(p->zTempFile) ) return;
16575 sqlite3_free(p->zTempFile);
16580 ** Create a new temp file name with the given suffix.
16582 static void newTempFile(ShellState *p, const char *zSuffix){
16584 sqlite3_free(p->zTempFile);
16587 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
16589 if( p->zTempFile==0 ){
16590 /* If p->db is an in-memory database then the TEMPFILENAME file-control
16591 ** will not work and we will need to fallback to guessing */
16594 sqlite3_randomness(sizeof(r), &r);
16595 zTemp = getenv("TEMP");
16596 if( zTemp==0 ) zTemp = getenv("TMP");
16604 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
16606 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
16608 if( p->zTempFile==0 ){
16609 raw_printf(stderr, "out of memory\n");
16616 ** The implementation of SQL scalar function fkey_collate_clause(), used
16617 ** by the ".lint fkey-indexes" command. This scalar function is always
16618 ** called with four arguments - the parent table name, the parent column name,
16619 ** the child table name and the child column name.
16621 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
16623 ** If either of the named tables or columns do not exist, this function
16624 ** returns an empty string. An empty string is also returned if both tables
16625 ** and columns exist but have the same default collation sequence. Or,
16626 ** if both exist but the default collation sequences are different, this
16627 ** function returns the string " COLLATE <parent-collation>", where
16628 ** <parent-collation> is the default collation sequence of the parent column.
16630 static void shellFkeyCollateClause(
16631 sqlite3_context *pCtx,
16633 sqlite3_value **apVal
16635 sqlite3 *db = sqlite3_context_db_handle(pCtx);
16636 const char *zParent;
16637 const char *zParentCol;
16638 const char *zParentSeq;
16639 const char *zChild;
16640 const char *zChildCol;
16641 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
16645 zParent = (const char*)sqlite3_value_text(apVal[0]);
16646 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
16647 zChild = (const char*)sqlite3_value_text(apVal[2]);
16648 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
16650 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
16651 rc = sqlite3_table_column_metadata(
16652 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
16654 if( rc==SQLITE_OK ){
16655 rc = sqlite3_table_column_metadata(
16656 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
16660 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
16661 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
16662 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
16669 ** The implementation of dot-command ".lint fkey-indexes".
16671 static int lintFkeyIndexes(
16672 ShellState *pState, /* Current shell tool state */
16673 char **azArg, /* Array of arguments passed to dot command */
16674 int nArg /* Number of entries in azArg[] */
16676 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
16677 FILE *out = pState->out; /* Stream to write non-error output to */
16678 int bVerbose = 0; /* If -verbose is present */
16679 int bGroupByParent = 0; /* If -groupbyparent is present */
16680 int i; /* To iterate through azArg[] */
16681 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
16682 int rc; /* Return code */
16683 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
16686 ** This SELECT statement returns one row for each foreign key constraint
16687 ** in the schema of the main database. The column values are:
16689 ** 0. The text of an SQL statement similar to:
16691 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
16693 ** This SELECT is similar to the one that the foreign keys implementation
16694 ** needs to run internally on child tables. If there is an index that can
16695 ** be used to optimize this query, then it can also be used by the FK
16696 ** implementation to optimize DELETE or UPDATE statements on the parent
16699 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
16700 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
16701 ** contains an index that can be used to optimize the query.
16703 ** 2. Human readable text that describes the child table and columns. e.g.
16705 ** "child_table(child_key1, child_key2)"
16707 ** 3. Human readable text that describes the parent table and columns. e.g.
16709 ** "parent_table(parent_key1, parent_key2)"
16711 ** 4. A full CREATE INDEX statement for an index that could be used to
16712 ** optimize DELETE or UPDATE statements on the parent table. e.g.
16714 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
16716 ** 5. The name of the parent table.
16718 ** These six values are used by the C logic below to generate the report.
16722 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
16723 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
16724 " || fkey_collate_clause("
16725 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
16727 " 'SEARCH ' || s.name || ' USING COVERING INDEX*('"
16728 " || group_concat('*=?', ' AND ') || ')'"
16730 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
16732 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
16734 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
16735 " || ' ON ' || quote(s.name) || '('"
16736 " || group_concat(quote(f.[from]) ||"
16737 " fkey_collate_clause("
16738 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
16742 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
16743 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
16744 "GROUP BY s.name, f.id "
16745 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
16747 const char *zGlobIPK = "SEARCH * USING INTEGER PRIMARY KEY (rowid=?)";
16749 for(i=2; i<nArg; i++){
16750 int n = strlen30(azArg[i]);
16751 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
16754 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
16755 bGroupByParent = 1;
16759 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
16762 return SQLITE_ERROR;
16766 /* Register the fkey_collate_clause() SQL function */
16767 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
16768 0, shellFkeyCollateClause, 0, 0
16772 if( rc==SQLITE_OK ){
16773 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
16775 if( rc==SQLITE_OK ){
16776 sqlite3_bind_int(pSql, 1, bGroupByParent);
16779 if( rc==SQLITE_OK ){
16782 while( SQLITE_ROW==sqlite3_step(pSql) ){
16784 sqlite3_stmt *pExplain = 0;
16785 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
16786 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
16787 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
16788 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
16789 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
16790 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
16792 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
16793 if( rc!=SQLITE_OK ) break;
16794 if( SQLITE_ROW==sqlite3_step(pExplain) ){
16795 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
16797 0==sqlite3_strglob(zGlob, zPlan)
16798 || 0==sqlite3_strglob(zGlobIPK, zPlan)
16801 rc = sqlite3_finalize(pExplain);
16802 if( rc!=SQLITE_OK ) break;
16805 raw_printf(stderr, "Error: internal error");
16809 && (bVerbose || res==0)
16810 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
16812 raw_printf(out, "-- Parent table %s\n", zParent);
16813 sqlite3_free(zPrev);
16814 zPrev = sqlite3_mprintf("%s", zParent);
16818 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
16819 }else if( bVerbose ){
16820 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
16821 zIndent, zFrom, zTarget
16826 sqlite3_free(zPrev);
16828 if( rc!=SQLITE_OK ){
16829 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16832 rc2 = sqlite3_finalize(pSql);
16833 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
16835 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16838 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
16845 ** Implementation of ".lint" dot command.
16847 static int lintDotCommand(
16848 ShellState *pState, /* Current shell tool state */
16849 char **azArg, /* Array of arguments passed to dot command */
16850 int nArg /* Number of entries in azArg[] */
16853 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
16854 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
16855 return lintFkeyIndexes(pState, azArg, nArg);
16858 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
16859 raw_printf(stderr, "Where sub-commands are:\n");
16860 raw_printf(stderr, " fkey-indexes\n");
16861 return SQLITE_ERROR;
16864 #if !defined SQLITE_OMIT_VIRTUALTABLE
16865 static void shellPrepare(
16869 sqlite3_stmt **ppStmt
16872 if( *pRc==SQLITE_OK ){
16873 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
16874 if( rc!=SQLITE_OK ){
16875 raw_printf(stderr, "sql error: %s (%d)\n",
16876 sqlite3_errmsg(db), sqlite3_errcode(db)
16884 ** Create a prepared statement using printf-style arguments for the SQL.
16886 ** This routine is could be marked "static". But it is not always used,
16887 ** depending on compile-time options. By omitting the "static", we avoid
16888 ** nuisance compiler warnings about "defined but not used".
16890 void shellPreparePrintf(
16893 sqlite3_stmt **ppStmt,
16898 if( *pRc==SQLITE_OK ){
16901 va_start(ap, zFmt);
16902 z = sqlite3_vmprintf(zFmt, ap);
16905 *pRc = SQLITE_NOMEM;
16907 shellPrepare(db, pRc, z, ppStmt);
16913 /* Finalize the prepared statement created using shellPreparePrintf().
16915 ** This routine is could be marked "static". But it is not always used,
16916 ** depending on compile-time options. By omitting the "static", we avoid
16917 ** nuisance compiler warnings about "defined but not used".
16919 void shellFinalize(
16921 sqlite3_stmt *pStmt
16924 sqlite3 *db = sqlite3_db_handle(pStmt);
16925 int rc = sqlite3_finalize(pStmt);
16926 if( *pRc==SQLITE_OK ){
16927 if( rc!=SQLITE_OK ){
16928 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16935 /* Reset the prepared statement created using shellPreparePrintf().
16937 ** This routine is could be marked "static". But it is not always used,
16938 ** depending on compile-time options. By omitting the "static", we avoid
16939 ** nuisance compiler warnings about "defined but not used".
16943 sqlite3_stmt *pStmt
16945 int rc = sqlite3_reset(pStmt);
16946 if( *pRc==SQLITE_OK ){
16947 if( rc!=SQLITE_OK ){
16948 sqlite3 *db = sqlite3_db_handle(pStmt);
16949 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16954 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16956 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16957 /******************************************************************************
16958 ** The ".archive" or ".ar" command.
16961 ** Structure representing a single ".ar" command.
16963 typedef struct ArCommand ArCommand;
16965 u8 eCmd; /* An AR_CMD_* value */
16966 u8 bVerbose; /* True if --verbose */
16967 u8 bZip; /* True if the archive is a ZIP */
16968 u8 bDryRun; /* True if --dry-run */
16969 u8 bAppend; /* True if --append */
16970 u8 fromCmdLine; /* Run from -A instead of .archive */
16971 int nArg; /* Number of command arguments */
16972 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
16973 const char *zFile; /* --file argument, or NULL */
16974 const char *zDir; /* --directory argument, or NULL */
16975 char **azArg; /* Array of command arguments */
16976 ShellState *p; /* Shell state */
16977 sqlite3 *db; /* Database containing the archive */
16981 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16983 static int arUsage(FILE *f){
16984 showHelp(f,"archive");
16985 return SQLITE_ERROR;
16989 ** Print an error message for the .ar command to stderr and return
16992 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16995 va_start(ap, zFmt);
16996 z = sqlite3_vmprintf(zFmt, ap);
16998 utf8_printf(stderr, "Error: %s\n", z);
16999 if( pAr->fromCmdLine ){
17000 utf8_printf(stderr, "Use \"-A\" for more help\n");
17002 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
17005 return SQLITE_ERROR;
17009 ** Values for ArCommand.eCmd.
17011 #define AR_CMD_CREATE 1
17012 #define AR_CMD_UPDATE 2
17013 #define AR_CMD_INSERT 3
17014 #define AR_CMD_EXTRACT 4
17015 #define AR_CMD_LIST 5
17016 #define AR_CMD_HELP 6
17019 ** Other (non-command) switches.
17021 #define AR_SWITCH_VERBOSE 7
17022 #define AR_SWITCH_FILE 8
17023 #define AR_SWITCH_DIRECTORY 9
17024 #define AR_SWITCH_APPEND 10
17025 #define AR_SWITCH_DRYRUN 11
17027 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
17029 case AR_CMD_CREATE:
17030 case AR_CMD_EXTRACT:
17032 case AR_CMD_UPDATE:
17033 case AR_CMD_INSERT:
17036 return arErrorMsg(pAr, "multiple command options");
17038 pAr->eCmd = eSwitch;
17041 case AR_SWITCH_DRYRUN:
17044 case AR_SWITCH_VERBOSE:
17047 case AR_SWITCH_APPEND:
17049 /* Fall thru into --file */
17050 case AR_SWITCH_FILE:
17053 case AR_SWITCH_DIRECTORY:
17062 ** Parse the command line for an ".ar" command. The results are written into
17063 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
17064 ** successfully, otherwise an error message is written to stderr and
17065 ** SQLITE_ERROR returned.
17067 static int arParseCommand(
17068 char **azArg, /* Array of arguments passed to dot command */
17069 int nArg, /* Number of entries in azArg[] */
17070 ArCommand *pAr /* Populate this object */
17078 { "create", 'c', AR_CMD_CREATE, 0 },
17079 { "extract", 'x', AR_CMD_EXTRACT, 0 },
17080 { "insert", 'i', AR_CMD_INSERT, 0 },
17081 { "list", 't', AR_CMD_LIST, 0 },
17082 { "update", 'u', AR_CMD_UPDATE, 0 },
17083 { "help", 'h', AR_CMD_HELP, 0 },
17084 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
17085 { "file", 'f', AR_SWITCH_FILE, 1 },
17086 { "append", 'a', AR_SWITCH_APPEND, 1 },
17087 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
17088 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
17090 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
17091 struct ArSwitch *pEnd = &aSwitch[nSwitch];
17094 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
17095 return arUsage(stderr);
17097 char *z = azArg[1];
17099 /* Traditional style [tar] invocation */
17102 for(i=0; z[i]; i++){
17103 const char *zArg = 0;
17104 struct ArSwitch *pOpt;
17105 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17106 if( z[i]==pOpt->cShort ) break;
17109 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17113 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
17115 zArg = azArg[iArg++];
17117 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17119 pAr->nArg = nArg-iArg;
17121 pAr->azArg = &azArg[iArg];
17124 /* Non-traditional invocation */
17126 for(iArg=1; iArg<nArg; iArg++){
17130 /* All remaining command line words are command arguments. */
17131 pAr->azArg = &azArg[iArg];
17132 pAr->nArg = nArg-iArg;
17139 /* One or more short options */
17140 for(i=1; i<n; i++){
17141 const char *zArg = 0;
17142 struct ArSwitch *pOpt;
17143 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17144 if( z[i]==pOpt->cShort ) break;
17147 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
17154 if( iArg>=(nArg-1) ){
17155 return arErrorMsg(pAr, "option requires an argument: %c",
17158 zArg = azArg[++iArg];
17161 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
17163 }else if( z[2]=='\0' ){
17164 /* A -- option, indicating that all remaining command line words
17165 ** are command arguments. */
17166 pAr->azArg = &azArg[iArg+1];
17167 pAr->nArg = nArg-iArg-1;
17170 /* A long option */
17171 const char *zArg = 0; /* Argument for option, if any */
17172 struct ArSwitch *pMatch = 0; /* Matching option */
17173 struct ArSwitch *pOpt; /* Iterator */
17174 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
17175 const char *zLong = pOpt->zLong;
17176 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
17178 return arErrorMsg(pAr, "ambiguous option: %s",z);
17186 return arErrorMsg(pAr, "unrecognized option: %s", z);
17188 if( pMatch->bArg ){
17189 if( iArg>=(nArg-1) ){
17190 return arErrorMsg(pAr, "option requires an argument: %s", z);
17192 zArg = azArg[++iArg];
17194 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
17204 ** This function assumes that all arguments within the ArCommand.azArg[]
17205 ** array refer to archive members, as for the --extract or --list commands.
17206 ** It checks that each of them are present. If any specified file is not
17207 ** present in the archive, an error is printed to stderr and an error
17208 ** code returned. Otherwise, if all specified arguments are present in
17209 ** the archive, SQLITE_OK is returned.
17211 ** This function strips any trailing '/' characters from each argument.
17212 ** This is consistent with the way the [tar] command seems to work on
17215 static int arCheckEntries(ArCommand *pAr){
17216 int rc = SQLITE_OK;
17219 sqlite3_stmt *pTest = 0;
17221 shellPreparePrintf(pAr->db, &rc, &pTest,
17222 "SELECT name FROM %s WHERE name=$name",
17225 j = sqlite3_bind_parameter_index(pTest, "$name");
17226 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17227 char *z = pAr->azArg[i];
17228 int n = strlen30(z);
17230 while( n>0 && z[n-1]=='/' ) n--;
17232 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
17233 if( SQLITE_ROW==sqlite3_step(pTest) ){
17236 shellReset(&rc, pTest);
17237 if( rc==SQLITE_OK && bOk==0 ){
17238 utf8_printf(stderr, "not found in archive: %s\n", z);
17242 shellFinalize(&rc, pTest);
17248 ** Format a WHERE clause that can be used against the "sqlar" table to
17249 ** identify all archive members that match the command arguments held
17250 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
17251 ** The caller is responsible for eventually calling sqlite3_free() on
17252 ** any non-NULL (*pzWhere) value.
17254 static void arWhereClause(
17257 char **pzWhere /* OUT: New WHERE clause */
17260 if( *pRc==SQLITE_OK ){
17261 if( pAr->nArg==0 ){
17262 zWhere = sqlite3_mprintf("1");
17265 const char *zSep = "";
17266 for(i=0; i<pAr->nArg; i++){
17267 const char *z = pAr->azArg[i];
17268 zWhere = sqlite3_mprintf(
17269 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
17270 zWhere, zSep, z, strlen30(z)+1, z
17273 *pRc = SQLITE_NOMEM;
17284 ** Implementation of .ar "lisT" command.
17286 static int arListCommand(ArCommand *pAr){
17287 const char *zSql = "SELECT %s FROM %s WHERE %s";
17288 const char *azCols[] = {
17290 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
17294 sqlite3_stmt *pSql = 0;
17297 rc = arCheckEntries(pAr);
17298 arWhereClause(&rc, pAr, &zWhere);
17300 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
17301 pAr->zSrcTable, zWhere);
17302 if( pAr->bDryRun ){
17303 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17305 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17306 if( pAr->bVerbose ){
17307 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
17308 sqlite3_column_text(pSql, 0),
17309 sqlite3_column_int(pSql, 1),
17310 sqlite3_column_text(pSql, 2),
17311 sqlite3_column_text(pSql, 3)
17314 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17318 shellFinalize(&rc, pSql);
17319 sqlite3_free(zWhere);
17325 ** Implementation of .ar "eXtract" command.
17327 static int arExtractCommand(ArCommand *pAr){
17328 const char *zSql1 =
17331 " writefile(($dir || name), %s, mode, mtime) "
17332 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
17333 " AND name NOT GLOB '*..[/\\]*'";
17335 const char *azExtraArg[] = {
17336 "sqlar_uncompress(data, sz)",
17340 sqlite3_stmt *pSql = 0;
17341 int rc = SQLITE_OK;
17346 /* If arguments are specified, check that they actually exist within
17347 ** the archive before proceeding. And formulate a WHERE clause to
17349 rc = arCheckEntries(pAr);
17350 arWhereClause(&rc, pAr, &zWhere);
17352 if( rc==SQLITE_OK ){
17354 zDir = sqlite3_mprintf("%s/", pAr->zDir);
17356 zDir = sqlite3_mprintf("");
17358 if( zDir==0 ) rc = SQLITE_NOMEM;
17361 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
17362 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
17365 if( rc==SQLITE_OK ){
17366 j = sqlite3_bind_parameter_index(pSql, "$dir");
17367 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
17369 /* Run the SELECT statement twice. The first time, writefile() is called
17370 ** for all archive members that should be extracted. The second time,
17371 ** only for the directories. This is because the timestamps for
17372 ** extracted directories must be reset after they are populated (as
17373 ** populating them changes the timestamp). */
17374 for(i=0; i<2; i++){
17375 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
17376 sqlite3_bind_int(pSql, j, i);
17377 if( pAr->bDryRun ){
17378 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
17380 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
17381 if( i==0 && pAr->bVerbose ){
17382 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
17386 shellReset(&rc, pSql);
17388 shellFinalize(&rc, pSql);
17391 sqlite3_free(zDir);
17392 sqlite3_free(zWhere);
17397 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
17399 static int arExecSql(ArCommand *pAr, const char *zSql){
17401 if( pAr->bDryRun ){
17402 utf8_printf(pAr->p->out, "%s\n", zSql);
17406 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
17408 utf8_printf(stdout, "ERROR: %s\n", zErr);
17409 sqlite3_free(zErr);
17417 ** Implementation of .ar "create", "insert", and "update" commands.
17419 ** create -> Create a new SQL archive
17420 ** insert -> Insert or reinsert all files listed
17421 ** update -> Insert files that have changed or that were not
17422 ** previously in the archive
17424 ** Create the "sqlar" table in the database if it does not already exist.
17425 ** Then add each file in the azFile[] array to the archive. Directories
17426 ** are added recursively. If argument bVerbose is non-zero, a message is
17427 ** printed on stdout for each file archived.
17429 ** The create command is the same as update, except that it drops
17430 ** any existing "sqlar" table before beginning. The "insert" command
17431 ** always overwrites every file named on the command-line, where as
17432 ** "update" only overwrites if the size or mtime or mode has changed.
17434 static int arCreateOrUpdateCommand(
17435 ArCommand *pAr, /* Command arguments and options */
17436 int bUpdate, /* true for a --create. */
17437 int bOnlyIfChanged /* Only update if file has changed */
17439 const char *zCreate =
17440 "CREATE TABLE IF NOT EXISTS sqlar(\n"
17441 " name TEXT PRIMARY KEY, -- name of the file\n"
17442 " mode INT, -- access permissions\n"
17443 " mtime INT, -- last modification time\n"
17444 " sz INT, -- original file size\n"
17445 " data BLOB -- compressed content\n"
17447 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
17448 const char *zInsertFmt[2] = {
17449 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
17454 " CASE substr(lsmode(mode),1,1)\n"
17455 " WHEN '-' THEN length(data)\n"
17456 " WHEN 'd' THEN 0\n"
17458 " sqlar_compress(data)\n"
17459 " FROM fsdir(%Q,%Q) AS disk\n"
17460 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17462 "REPLACE INTO %s(name,mode,mtime,data)\n"
17468 " FROM fsdir(%Q,%Q) AS disk\n"
17469 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
17471 int i; /* For iterating through azFile[] */
17472 int rc; /* Return code */
17473 const char *zTab = 0; /* SQL table into which to insert */
17478 arExecSql(pAr, "PRAGMA page_size=512");
17479 rc = arExecSql(pAr, "SAVEPOINT ar;");
17480 if( rc!=SQLITE_OK ) return rc;
17483 /* Initialize the zipfile virtual table, if necessary */
17486 sqlite3_randomness(sizeof(r),&r);
17487 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
17489 zSql = sqlite3_mprintf(
17490 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
17493 rc = arExecSql(pAr, zSql);
17494 sqlite3_free(zSql);
17499 /* Initialize the table for an SQLAR */
17502 rc = arExecSql(pAr, zDrop);
17503 if( rc!=SQLITE_OK ) goto end_ar_transaction;
17505 rc = arExecSql(pAr, zCreate);
17507 if( bOnlyIfChanged ){
17508 zExists = sqlite3_mprintf(
17510 "SELECT 1 FROM %s AS mem"
17511 " WHERE mem.name=disk.name"
17512 " AND mem.mtime=disk.mtime"
17513 " AND mem.mode=disk.mode)", zTab);
17515 zExists = sqlite3_mprintf("");
17517 if( zExists==0 ) rc = SQLITE_NOMEM;
17518 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
17519 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
17520 pAr->bVerbose ? "shell_putsnl(name)" : "name",
17521 pAr->azArg[i], pAr->zDir, zExists);
17522 rc = arExecSql(pAr, zSql2);
17523 sqlite3_free(zSql2);
17525 end_ar_transaction:
17526 if( rc!=SQLITE_OK ){
17527 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
17529 rc = arExecSql(pAr, "RELEASE ar;");
17530 if( pAr->bZip && pAr->zFile ){
17531 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
17532 arExecSql(pAr, zSql);
17533 sqlite3_free(zSql);
17536 sqlite3_free(zExists);
17541 ** Implementation of ".ar" dot command.
17543 static int arDotCommand(
17544 ShellState *pState, /* Current shell tool state */
17545 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
17546 char **azArg, /* Array of arguments passed to dot command */
17547 int nArg /* Number of entries in azArg[] */
17551 memset(&cmd, 0, sizeof(cmd));
17552 cmd.fromCmdLine = fromCmdLine;
17553 rc = arParseCommand(azArg, nArg, &cmd);
17554 if( rc==SQLITE_OK ){
17555 int eDbType = SHELL_OPEN_UNSPEC;
17557 cmd.db = pState->db;
17559 eDbType = deduceDatabaseType(cmd.zFile, 1);
17561 eDbType = pState->openMode;
17563 if( eDbType==SHELL_OPEN_ZIPFILE ){
17564 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
17565 if( cmd.zFile==0 ){
17566 cmd.zSrcTable = sqlite3_mprintf("zip");
17568 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
17572 }else if( cmd.zFile ){
17574 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
17575 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
17576 || cmd.eCmd==AR_CMD_UPDATE ){
17577 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
17579 flags = SQLITE_OPEN_READONLY;
17583 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
17584 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
17586 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
17587 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
17588 if( rc!=SQLITE_OK ){
17589 utf8_printf(stderr, "cannot open file: %s (%s)\n",
17590 cmd.zFile, sqlite3_errmsg(cmd.db)
17592 goto end_ar_command;
17594 sqlite3_fileio_init(cmd.db, 0, 0);
17595 sqlite3_sqlar_init(cmd.db, 0, 0);
17596 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
17597 shellPutsFunc, 0, 0);
17600 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
17601 if( cmd.eCmd!=AR_CMD_CREATE
17602 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
17604 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
17606 goto end_ar_command;
17608 cmd.zSrcTable = sqlite3_mprintf("sqlar");
17611 switch( cmd.eCmd ){
17612 case AR_CMD_CREATE:
17613 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
17616 case AR_CMD_EXTRACT:
17617 rc = arExtractCommand(&cmd);
17621 rc = arListCommand(&cmd);
17625 arUsage(pState->out);
17628 case AR_CMD_INSERT:
17629 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
17633 assert( cmd.eCmd==AR_CMD_UPDATE );
17634 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
17639 if( cmd.db!=pState->db ){
17642 sqlite3_free(cmd.zSrcTable);
17646 /* End of the ".archive" or ".ar" command logic
17647 *******************************************************************************/
17648 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
17650 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17652 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
17653 ** Otherwise, the SQL statement or statements in zSql are executed using
17654 ** database connection db and the error code written to *pRc before
17655 ** this function returns.
17657 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
17659 if( rc==SQLITE_OK ){
17661 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
17662 if( rc!=SQLITE_OK ){
17663 raw_printf(stderr, "SQL error: %s\n", zErr);
17665 sqlite3_free(zErr);
17671 ** Like shellExec(), except that zFmt is a printf() style format string.
17673 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
17675 if( *pRc==SQLITE_OK ){
17677 va_start(ap, zFmt);
17678 z = sqlite3_vmprintf(zFmt, ap);
17681 *pRc = SQLITE_NOMEM;
17683 shellExec(db, pRc, z);
17690 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17691 ** Otherwise, an attempt is made to allocate, zero and return a pointer
17692 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
17693 ** to SQLITE_NOMEM and NULL returned.
17695 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
17697 if( *pRc==SQLITE_OK ){
17698 pRet = sqlite3_malloc64(nByte);
17700 *pRc = SQLITE_NOMEM;
17702 memset(pRet, 0, nByte);
17709 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
17710 ** Otherwise, zFmt is treated as a printf() style string. The result of
17711 ** formatting it along with any trailing arguments is written into a
17712 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
17713 ** It is the responsibility of the caller to eventually free this buffer
17714 ** using a call to sqlite3_free().
17716 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
17717 ** pointer returned.
17719 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
17721 if( *pRc==SQLITE_OK ){
17723 va_start(ap, zFmt);
17724 z = sqlite3_vmprintf(zFmt, ap);
17727 *pRc = SQLITE_NOMEM;
17734 ** When running the ".recover" command, each output table, and the special
17735 ** orphaned row table if it is required, is represented by an instance
17736 ** of the following struct.
17738 typedef struct RecoverTable RecoverTable;
17739 struct RecoverTable {
17740 char *zQuoted; /* Quoted version of table name */
17741 int nCol; /* Number of columns in table */
17742 char **azlCol; /* Array of column lists */
17743 int iPk; /* Index of IPK column */
17747 ** Free a RecoverTable object allocated by recoverFindTable() or
17748 ** recoverOrphanTable().
17750 static void recoverFreeTable(RecoverTable *pTab){
17752 sqlite3_free(pTab->zQuoted);
17753 if( pTab->azlCol ){
17755 for(i=0; i<=pTab->nCol; i++){
17756 sqlite3_free(pTab->azlCol[i]);
17758 sqlite3_free(pTab->azlCol);
17760 sqlite3_free(pTab);
17765 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
17766 ** Otherwise, it allocates and returns a RecoverTable object based on the
17767 ** final four arguments passed to this function. It is the responsibility
17768 ** of the caller to eventually free the returned object using
17769 ** recoverFreeTable().
17771 static RecoverTable *recoverNewTable(
17772 int *pRc, /* IN/OUT: Error code */
17773 const char *zName, /* Name of table */
17774 const char *zSql, /* CREATE TABLE statement */
17778 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
17780 RecoverTable *pTab = 0;
17782 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
17783 if( rc==SQLITE_OK ){
17785 int bSqlIntkey = 0;
17786 sqlite3_stmt *pStmt = 0;
17788 rc = sqlite3_open("", &dbtmp);
17789 if( rc==SQLITE_OK ){
17790 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
17791 shellIdQuote, 0, 0);
17793 if( rc==SQLITE_OK ){
17794 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
17796 if( rc==SQLITE_OK ){
17797 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
17798 if( rc==SQLITE_ERROR ){
17803 shellPreparePrintf(dbtmp, &rc, &pStmt,
17804 "SELECT count(*) FROM pragma_table_info(%Q)", zName
17806 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17807 nSqlCol = sqlite3_column_int(pStmt, 0);
17809 shellFinalize(&rc, pStmt);
17811 if( rc!=SQLITE_OK || nSqlCol<nCol ){
17815 shellPreparePrintf(dbtmp, &rc, &pStmt,
17817 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
17818 ") FROM sqlite_schema WHERE name = %Q", zName
17820 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17821 bSqlIntkey = sqlite3_column_int(pStmt, 0);
17823 shellFinalize(&rc, pStmt);
17825 if( bIntkey==bSqlIntkey ){
17827 const char *zPk = "_rowid_";
17828 sqlite3_stmt *pPkFinder = 0;
17830 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
17831 ** set zPk to the name of the PK column, and pTab->iPk to the index
17832 ** of the column, where columns are 0-numbered from left to right.
17833 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
17834 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
17837 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
17838 "SELECT cid, name FROM pragma_table_info(%Q) "
17839 " WHERE pk=1 AND type='integer' COLLATE nocase"
17840 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
17843 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
17844 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
17845 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
17849 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
17850 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
17851 pTab->nCol = nSqlCol;
17854 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
17856 pTab->azlCol[0] = shellMPrintf(&rc, "");
17859 shellPreparePrintf(dbtmp, &rc, &pStmt,
17860 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
17861 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
17862 "FROM pragma_table_info(%Q)",
17863 bIntkey ? ", " : "", pTab->iPk,
17864 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
17867 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17868 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
17869 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
17872 shellFinalize(&rc, pStmt);
17874 shellFinalize(&rc, pPkFinder);
17879 sqlite3_close(dbtmp);
17881 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
17882 recoverFreeTable(pTab);
17889 ** This function is called to search the schema recovered from the
17890 ** sqlite_schema table of the (possibly) corrupt database as part
17891 ** of a ".recover" command. Specifically, for a table with root page
17892 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
17893 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
17896 ** If a table is found, a (RecoverTable*) object is returned. Or, if
17897 ** no such table is found, but bIntkey is false and iRoot is the
17898 ** root page of an index in the recovered schema, then (*pbNoop) is
17899 ** set to true and NULL returned. Or, if there is no such table or
17900 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
17901 ** the caller should write data to the orphans table.
17903 static RecoverTable *recoverFindTable(
17904 ShellState *pState, /* Shell state object */
17905 int *pRc, /* IN/OUT: Error code */
17906 int iRoot, /* Root page of table */
17907 int bIntkey, /* True for an intkey table */
17908 int nCol, /* Number of columns in table */
17909 int *pbNoop /* OUT: True if iRoot is root of index */
17911 sqlite3_stmt *pStmt = 0;
17912 RecoverTable *pRet = 0;
17914 const char *zSql = 0;
17915 const char *zName = 0;
17917 /* Search the recovered schema for an object with root page iRoot. */
17918 shellPreparePrintf(pState->db, pRc, &pStmt,
17919 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17921 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17922 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17923 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17927 if( sqlite3_stricmp(zType, "table")==0 ){
17928 zName = (const char*)sqlite3_column_text(pStmt, 1);
17929 zSql = (const char*)sqlite3_column_text(pStmt, 2);
17930 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17935 shellFinalize(pRc, pStmt);
17941 ** Return a RecoverTable object representing the orphans table.
17943 static RecoverTable *recoverOrphanTable(
17944 ShellState *pState, /* Shell state object */
17945 int *pRc, /* IN/OUT: Error code */
17946 const char *zLostAndFound, /* Base name for orphans table */
17947 int nCol /* Number of user data columns */
17949 RecoverTable *pTab = 0;
17950 if( nCol>=0 && *pRc==SQLITE_OK ){
17953 /* This block determines the name of the orphan table. The prefered
17954 ** name is zLostAndFound. But if that clashes with another name
17955 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17956 ** and so on until a non-clashing name is found. */
17958 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17959 sqlite3_stmt *pTest = 0;
17960 shellPrepare(pState->db, pRc,
17961 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17963 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17964 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17965 shellReset(pRc, pTest);
17966 sqlite3_free(zTab);
17967 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17968 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17970 shellFinalize(pRc, pTest);
17972 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17974 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17978 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17979 if( pTab->azlCol ){
17980 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17981 for(i=nCol-1; i>=0; i--){
17982 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17987 if( *pRc!=SQLITE_OK ){
17988 recoverFreeTable(pTab);
17991 raw_printf(pState->out,
17992 "CREATE TABLE %s(rootpgno INTEGER, "
17993 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17995 for(i=0; i<nCol; i++){
17996 raw_printf(pState->out, ", c%d", i);
17998 raw_printf(pState->out, ");\n");
18001 sqlite3_free(zTab);
18007 ** This function is called to recover data from the database. A script
18008 ** to construct a new database containing all recovered data is output
18009 ** on stream pState->out.
18011 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
18012 int rc = SQLITE_OK;
18013 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
18014 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
18015 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
18016 const char *zRecoveryDb = ""; /* Name of "recovery" database */
18017 const char *zLostAndFound = "lost_and_found";
18020 RecoverTable *pOrphan = 0;
18022 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
18023 int bRowids = 1; /* 0 if --no-rowids */
18024 for(i=1; i<nArg; i++){
18025 char *z = azArg[i];
18027 if( z[0]=='-' && z[1]=='-' ) z++;
18029 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
18032 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
18034 zRecoveryDb = azArg[i];
18036 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
18038 zLostAndFound = azArg[i];
18040 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
18044 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
18045 showHelp(pState->out, azArg[0]);
18050 shellExecPrintf(pState->db, &rc,
18051 /* Attach an in-memory database named 'recovery'. Create an indexed
18052 ** cache of the sqlite_dbptr virtual table. */
18053 "PRAGMA writable_schema = on;"
18054 "ATTACH %Q AS recovery;"
18055 "DROP TABLE IF EXISTS recovery.dbptr;"
18056 "DROP TABLE IF EXISTS recovery.freelist;"
18057 "DROP TABLE IF EXISTS recovery.map;"
18058 "DROP TABLE IF EXISTS recovery.schema;"
18059 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
18063 shellExec(pState->db, &rc,
18064 "WITH trunk(pgno) AS ("
18065 " SELECT shell_int32("
18066 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
18069 " SELECT shell_int32("
18070 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
18071 " FROM trunk WHERE x>0"
18073 "freelist(data, n, freepgno) AS ("
18074 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
18075 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
18077 " SELECT data, n-1, shell_int32(data, 2+n) "
18078 " FROM freelist WHERE n>=0"
18080 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
18084 /* If this is an auto-vacuum database, add all pointer-map pages to
18085 ** the freelist table. Do this regardless of whether or not
18086 ** --freelist-corrupt was specified. */
18087 shellExec(pState->db, &rc,
18088 "WITH ptrmap(pgno) AS ("
18089 " SELECT 2 WHERE shell_int32("
18090 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
18093 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
18094 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
18096 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
18099 shellExec(pState->db, &rc,
18100 "CREATE TABLE recovery.dbptr("
18101 " pgno, child, PRIMARY KEY(child, pgno)"
18103 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
18104 " SELECT * FROM sqlite_dbptr"
18105 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
18107 /* Delete any pointer to page 1. This ensures that page 1 is considered
18108 ** a root page, regardless of how corrupt the db is. */
18109 "DELETE FROM recovery.dbptr WHERE child = 1;"
18111 /* Delete all pointers to any pages that have more than one pointer
18112 ** to them. Such pages will be treated as root pages when recovering
18114 "DELETE FROM recovery.dbptr WHERE child IN ("
18115 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
18118 /* Create the "map" table that will (eventually) contain instructions
18119 ** for dealing with each page in the db that contains one or more
18121 "CREATE TABLE recovery.map("
18122 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
18125 /* Populate table [map]. If there are circular loops of pages in the
18126 ** database, the following adds all pages in such a loop to the map
18127 ** as individual root pages. This could be handled better. */
18128 "WITH pages(i, maxlen) AS ("
18129 " SELECT page_count, ("
18130 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
18131 " ) FROM pragma_page_count WHERE page_count>0"
18134 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
18135 " ) FROM pages WHERE i>=2"
18137 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
18138 " SELECT i, maxlen, NULL, ("
18139 " WITH p(orig, pgno, parent) AS ("
18140 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
18142 " SELECT i, p.parent, "
18143 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
18145 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
18147 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
18148 "UPDATE recovery.map AS o SET intkey = ("
18149 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
18152 /* Extract data from page 1 and any linked pages into table
18153 ** recovery.schema. With the same schema as an sqlite_schema table. */
18154 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
18155 "INSERT INTO recovery.schema SELECT "
18156 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
18157 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
18158 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
18159 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
18160 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
18161 "FROM sqlite_dbdata WHERE pgno IN ("
18162 " SELECT pgno FROM recovery.map WHERE root=1"
18164 "GROUP BY pgno, cell;"
18165 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
18168 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
18169 ** CREATE TABLE statements that extracted from the existing schema. */
18170 if( rc==SQLITE_OK ){
18171 sqlite3_stmt *pStmt = 0;
18172 /* ".recover" might output content in an order which causes immediate
18173 ** foreign key constraints to be violated. So disable foreign-key
18174 ** constraint enforcement to prevent problems when running the output
18176 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
18177 raw_printf(pState->out, "BEGIN;\n");
18178 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
18179 shellPrepare(pState->db, &rc,
18180 "SELECT sql FROM recovery.schema "
18181 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
18183 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18184 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
18185 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
18189 shellFinalize(&rc, pStmt);
18192 /* Figure out if an orphan table will be required. And if so, how many
18193 ** user columns it should contain */
18194 shellPrepare(pState->db, &rc,
18195 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
18198 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18199 nOrphan = sqlite3_column_int(pLoop, 0);
18201 shellFinalize(&rc, pLoop);
18204 shellPrepare(pState->db, &rc,
18205 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
18208 shellPrepare(pState->db, &rc,
18209 "SELECT max(field), group_concat(shell_escape_crnl(quote"
18210 "(case when (? AND field<0) then NULL else value end)"
18213 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
18214 "GROUP BY cell", &pCells
18217 /* Loop through each root page. */
18218 shellPrepare(pState->db, &rc,
18219 "SELECT root, intkey, max(maxlen) FROM recovery.map"
18220 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
18221 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
18224 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
18225 int iRoot = sqlite3_column_int(pLoop, 0);
18226 int bIntkey = sqlite3_column_int(pLoop, 1);
18227 int nCol = sqlite3_column_int(pLoop, 2);
18229 RecoverTable *pTab;
18231 assert( bIntkey==0 || bIntkey==1 );
18232 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
18233 if( bNoop || rc ) continue;
18236 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18239 if( pTab==0 ) break;
18242 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
18243 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
18245 sqlite3_bind_int(pPages, 1, iRoot);
18246 if( bRowids==0 && pTab->iPk<0 ){
18247 sqlite3_bind_int(pCells, 1, 1);
18249 sqlite3_bind_int(pCells, 1, 0);
18251 sqlite3_bind_int(pCells, 3, pTab->iPk);
18253 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
18254 int iPgno = sqlite3_column_int(pPages, 0);
18255 sqlite3_bind_int(pCells, 2, iPgno);
18256 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
18257 int nField = sqlite3_column_int(pCells, 0);
18258 int iMin = sqlite3_column_int(pCells, 2);
18259 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
18261 RecoverTable *pTab2 = pTab;
18262 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
18264 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
18267 if( pTab2==0 ) break;
18271 if( pTab2==pOrphan ){
18272 raw_printf(pState->out,
18273 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
18274 pTab2->zQuoted, iRoot, iPgno, nField,
18275 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
18278 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
18279 pTab2->zQuoted, pTab2->azlCol[nField], zVal
18283 shellReset(&rc, pCells);
18285 shellReset(&rc, pPages);
18286 if( pTab!=pOrphan ) recoverFreeTable(pTab);
18288 shellFinalize(&rc, pLoop);
18289 shellFinalize(&rc, pPages);
18290 shellFinalize(&rc, pCells);
18291 recoverFreeTable(pOrphan);
18293 /* The rest of the schema */
18294 if( rc==SQLITE_OK ){
18295 sqlite3_stmt *pStmt = 0;
18296 shellPrepare(pState->db, &rc,
18297 "SELECT sql, name FROM recovery.schema "
18298 "WHERE sql NOT LIKE 'create table%'", &pStmt
18300 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
18301 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
18302 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
18303 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
18304 char *zPrint = shellMPrintf(&rc,
18305 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
18308 raw_printf(pState->out, "%s;\n", zPrint);
18309 sqlite3_free(zPrint);
18311 raw_printf(pState->out, "%s;\n", zSql);
18314 shellFinalize(&rc, pStmt);
18317 if( rc==SQLITE_OK ){
18318 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
18319 raw_printf(pState->out, "COMMIT;\n");
18321 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
18324 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18328 ** If an input line begins with "." then invoke this routine to
18329 ** process that line.
18331 ** Return 1 on error, 2 to exit, and 0 otherwise.
18333 static int do_meta_command(char *zLine, ShellState *p){
18340 #ifndef SQLITE_OMIT_VIRTUALTABLE
18341 if( p->expert.pExpert ){
18342 expertFinish(p, 1, 0);
18346 /* Parse the input line into tokens.
18348 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
18349 while( IsSpace(zLine[h]) ){ h++; }
18350 if( zLine[h]==0 ) break;
18351 if( zLine[h]=='\'' || zLine[h]=='"' ){
18352 int delim = zLine[h++];
18353 azArg[nArg++] = &zLine[h];
18354 while( zLine[h] && zLine[h]!=delim ){
18355 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
18358 if( zLine[h]==delim ){
18361 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
18363 azArg[nArg++] = &zLine[h];
18364 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
18365 if( zLine[h] ) zLine[h++] = 0;
18366 resolve_backslashes(azArg[nArg-1]);
18371 /* Process the input line.
18373 if( nArg==0 ) return 0; /* no tokens, no error */
18374 n = strlen30(azArg[0]);
18378 #ifndef SQLITE_OMIT_AUTHORIZATION
18379 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
18381 raw_printf(stderr, "Usage: .auth ON|OFF\n");
18383 goto meta_command_exit;
18386 if( booleanValue(azArg[1]) ){
18387 sqlite3_set_authorizer(p->db, shellAuth, p);
18389 sqlite3_set_authorizer(p->db, 0, 0);
18394 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
18395 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
18397 rc = arDotCommand(p, 0, azArg, nArg);
18401 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
18402 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
18404 const char *zDestFile = 0;
18405 const char *zDb = 0;
18407 sqlite3_backup *pBackup;
18410 const char *zVfs = 0;
18411 for(j=1; j<nArg; j++){
18412 const char *z = azArg[j];
18414 if( z[1]=='-' ) z++;
18415 if( strcmp(z, "-append")==0 ){
18418 if( strcmp(z, "-async")==0 ){
18422 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
18425 }else if( zDestFile==0 ){
18426 zDestFile = azArg[j];
18427 }else if( zDb==0 ){
18429 zDestFile = azArg[j];
18431 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
18435 if( zDestFile==0 ){
18436 raw_printf(stderr, "missing FILENAME argument on .backup\n");
18439 if( zDb==0 ) zDb = "main";
18440 rc = sqlite3_open_v2(zDestFile, &pDest,
18441 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
18442 if( rc!=SQLITE_OK ){
18443 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
18448 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
18452 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
18454 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18458 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
18459 sqlite3_backup_finish(pBackup);
18460 if( rc==SQLITE_DONE ){
18463 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
18469 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
18471 bail_on_error = booleanValue(azArg[1]);
18473 raw_printf(stderr, "Usage: .bail on|off\n");
18478 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
18480 if( booleanValue(azArg[1]) ){
18481 setBinaryMode(p->out, 1);
18483 setTextMode(p->out, 1);
18486 raw_printf(stderr, "Usage: .binary on|off\n");
18491 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
18493 #if defined(_WIN32) || defined(WIN32)
18494 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
18495 rc = !SetCurrentDirectoryW(z);
18498 rc = chdir(azArg[1]);
18501 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
18505 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
18510 /* The undocumented ".breakpoint" command causes a call to the no-op
18511 ** routine named test_breakpoint().
18513 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
18517 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
18519 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
18521 raw_printf(stderr, "Usage: .changes on|off\n");
18526 /* Cancel output redirection, if it is currently set (by .testcase)
18527 ** Then read the content of the testcase-out.txt file and compare against
18528 ** azArg[1]. If there are differences, report an error and exit.
18530 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
18534 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
18536 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
18537 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
18539 }else if( testcase_glob(azArg[1],zRes)==0 ){
18540 utf8_printf(stderr,
18541 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
18542 p->zTestcase, azArg[1], zRes);
18545 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
18548 sqlite3_free(zRes);
18551 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
18553 tryToClone(p, azArg[1]);
18555 raw_printf(stderr, "Usage: .clone FILENAME\n");
18560 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
18563 sqlite3_stmt *pStmt;
18566 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18568 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18571 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18572 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
18573 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
18574 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
18575 if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
18576 azName[nName*2] = strdup(zSchema);
18577 azName[nName*2+1] = strdup(zFile);
18581 sqlite3_finalize(pStmt);
18582 for(i=0; i<nName; i++){
18583 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
18584 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
18585 const char *z = azName[i*2+1];
18586 utf8_printf(p->out, "%s: %s %s%s\n",
18588 z && z[0] ? z : "\"\"",
18589 bRdonly ? "r/o" : "r/w",
18590 eTxn==SQLITE_TXN_NONE ? "" :
18591 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
18593 free(azName[i*2+1]);
18595 sqlite3_free(azName);
18598 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
18599 static const struct DbConfigChoices {
18603 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
18604 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
18605 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
18606 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
18607 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
18608 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
18609 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
18610 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
18611 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
18612 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
18613 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
18614 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
18615 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
18616 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
18617 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
18618 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
18622 for(ii=0; ii<ArraySize(aDbConfig); ii++){
18623 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
18625 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
18627 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
18628 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
18629 if( nArg>1 ) break;
18631 if( nArg>1 && ii==ArraySize(aDbConfig) ){
18632 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
18633 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
18637 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
18638 rc = shell_dbinfo_command(p, nArg, azArg);
18641 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
18642 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
18644 rc = recoverDatabaseCmd(p, nArg, azArg);
18646 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
18648 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
18652 int savedShowHeader = p->showHeader;
18653 int savedShellFlags = p->shellFlgs;
18655 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
18656 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
18657 for(i=1; i<nArg; i++){
18658 if( azArg[i][0]=='-' ){
18659 const char *z = azArg[i]+1;
18660 if( z[0]=='-' ) z++;
18661 if( strcmp(z,"preserve-rowids")==0 ){
18662 #ifdef SQLITE_OMIT_VIRTUALTABLE
18663 raw_printf(stderr, "The --preserve-rowids option is not compatible"
18664 " with SQLITE_OMIT_VIRTUALTABLE\n");
18666 sqlite3_free(zLike);
18667 goto meta_command_exit;
18669 ShellSetFlag(p, SHFLG_PreserveRowid);
18672 if( strcmp(z,"newlines")==0 ){
18673 ShellSetFlag(p, SHFLG_Newlines);
18675 if( strcmp(z,"data-only")==0 ){
18676 ShellSetFlag(p, SHFLG_DumpDataOnly);
18678 if( strcmp(z,"nosys")==0 ){
18679 ShellSetFlag(p, SHFLG_DumpNoSys);
18682 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
18684 sqlite3_free(zLike);
18685 goto meta_command_exit;
18688 /* azArg[i] contains a LIKE pattern. This ".dump" request should
18689 ** only dump data for tables for which either the table name matches
18690 ** the LIKE pattern, or the table appears to be a shadow table of
18691 ** a virtual table for which the name matches the LIKE pattern.
18693 char *zExpr = sqlite3_mprintf(
18694 "name LIKE %Q ESCAPE '\\' OR EXISTS ("
18695 " SELECT 1 FROM sqlite_schema WHERE "
18696 " name LIKE %Q ESCAPE '\\' AND"
18697 " sql LIKE 'CREATE VIRTUAL TABLE%%' AND"
18698 " substr(o.name, 1, length(name)+1) == (name||'_')"
18699 ")", azArg[i], azArg[i]
18703 zLike = sqlite3_mprintf("%z OR %z", zLike, zExpr);
18712 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18713 /* When playing back a "dump", the content might appear in an order
18714 ** which causes immediate foreign key constraints to be violated.
18715 ** So disable foreign-key constraint enforcement to prevent problems. */
18716 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
18717 raw_printf(p->out, "BEGIN TRANSACTION;\n");
18719 p->writableSchema = 0;
18721 /* Set writable_schema=ON since doing so forces SQLite to initialize
18722 ** as much of the schema as it can even if the sqlite_schema table is
18724 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
18726 if( zLike==0 ) zLike = sqlite3_mprintf("true");
18727 zSql = sqlite3_mprintf(
18728 "SELECT name, type, sql FROM sqlite_schema AS o "
18729 "WHERE (%s) AND type=='table'"
18730 " AND sql NOT NULL"
18731 " ORDER BY tbl_name='sqlite_sequence', rowid",
18734 run_schema_dump_query(p,zSql);
18735 sqlite3_free(zSql);
18736 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18737 zSql = sqlite3_mprintf(
18738 "SELECT sql FROM sqlite_schema AS o "
18739 "WHERE (%s) AND sql NOT NULL"
18740 " AND type IN ('index','trigger','view')",
18743 run_table_dump_query(p, zSql);
18744 sqlite3_free(zSql);
18746 sqlite3_free(zLike);
18747 if( p->writableSchema ){
18748 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
18749 p->writableSchema = 0;
18751 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
18752 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
18753 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
18754 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
18756 p->showHeader = savedShowHeader;
18757 p->shellFlgs = savedShellFlags;
18760 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
18762 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
18764 raw_printf(stderr, "Usage: .echo on|off\n");
18769 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
18771 p->autoEQPtest = 0;
18772 if( p->autoEQPtrace ){
18773 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
18774 p->autoEQPtrace = 0;
18776 if( strcmp(azArg[1],"full")==0 ){
18777 p->autoEQP = AUTOEQP_full;
18778 }else if( strcmp(azArg[1],"trigger")==0 ){
18779 p->autoEQP = AUTOEQP_trigger;
18780 #ifdef SQLITE_DEBUG
18781 }else if( strcmp(azArg[1],"test")==0 ){
18782 p->autoEQP = AUTOEQP_on;
18783 p->autoEQPtest = 1;
18784 }else if( strcmp(azArg[1],"trace")==0 ){
18785 p->autoEQP = AUTOEQP_full;
18786 p->autoEQPtrace = 1;
18788 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
18789 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
18792 p->autoEQP = (u8)booleanValue(azArg[1]);
18795 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
18800 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
18801 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
18805 /* The ".explain" command is automatic now. It is largely pointless. It
18806 ** retained purely for backwards compatibility */
18807 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
18810 if( strcmp(azArg[1],"auto")==0 ){
18813 val = booleanValue(azArg[1]);
18816 if( val==1 && p->mode!=MODE_Explain ){
18817 p->normalMode = p->mode;
18818 p->mode = MODE_Explain;
18819 p->autoExplain = 0;
18820 }else if( val==0 ){
18821 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18822 p->autoExplain = 0;
18823 }else if( val==99 ){
18824 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
18825 p->autoExplain = 1;
18829 #ifndef SQLITE_OMIT_VIRTUALTABLE
18830 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
18832 expertDotCommand(p, azArg, nArg);
18836 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
18837 static const struct {
18838 const char *zCtrlName; /* Name of a test-control option */
18839 int ctrlCode; /* Integer code for that option */
18840 const char *zUsage; /* Usage notes */
18842 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
18843 { "data_version", SQLITE_FCNTL_DATA_VERSION, "" },
18844 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
18845 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
18846 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
18847 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
18848 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
18849 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
18850 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
18851 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
18852 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
18856 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
18857 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
18859 const char *zCmd = 0;
18860 const char *zSchema = 0;
18863 zCmd = nArg>=2 ? azArg[1] : "help";
18866 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
18869 zSchema = azArg[2];
18870 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
18875 /* The argument can optionally begin with "-" or "--" */
18876 if( zCmd[0]=='-' && zCmd[1] ){
18878 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18881 /* --help lists all file-controls */
18882 if( strcmp(zCmd,"help")==0 ){
18883 utf8_printf(p->out, "Available file-controls:\n");
18884 for(i=0; i<ArraySize(aCtrl); i++){
18885 utf8_printf(p->out, " .filectrl %s %s\n",
18886 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18889 goto meta_command_exit;
18892 /* convert filectrl text option to value. allow any unique prefix
18893 ** of the option name, or a numerical value. */
18894 n2 = strlen30(zCmd);
18895 for(i=0; i<ArraySize(aCtrl); i++){
18896 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18898 filectrl = aCtrl[i].ctrlCode;
18901 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
18902 "Use \".filectrl --help\" for help\n", zCmd);
18904 goto meta_command_exit;
18909 utf8_printf(stderr,"Error: unknown file-control: %s\n"
18910 "Use \".filectrl --help\" for help\n", zCmd);
18913 case SQLITE_FCNTL_SIZE_LIMIT: {
18914 if( nArg!=2 && nArg!=3 ) break;
18915 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
18916 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
18920 case SQLITE_FCNTL_LOCK_TIMEOUT:
18921 case SQLITE_FCNTL_CHUNK_SIZE: {
18923 if( nArg!=3 ) break;
18924 x = (int)integerValue(azArg[2]);
18925 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18929 case SQLITE_FCNTL_PERSIST_WAL:
18930 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
18932 if( nArg!=2 && nArg!=3 ) break;
18933 x = nArg==3 ? booleanValue(azArg[2]) : -1;
18934 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18939 case SQLITE_FCNTL_DATA_VERSION:
18940 case SQLITE_FCNTL_HAS_MOVED: {
18942 if( nArg!=2 ) break;
18943 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18948 case SQLITE_FCNTL_TEMPFILENAME: {
18950 if( nArg!=2 ) break;
18951 sqlite3_file_control(p->db, zSchema, filectrl, &z);
18953 utf8_printf(p->out, "%s\n", z);
18959 case SQLITE_FCNTL_RESERVE_BYTES: {
18962 x = atoi(azArg[2]);
18963 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18966 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18967 utf8_printf(p->out,"%d\n", x);
18973 if( isOk==0 && iCtrl>=0 ){
18974 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18976 }else if( isOk==1 ){
18978 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18979 raw_printf(p->out, "%s\n", zBuf);
18983 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18986 memcpy(&data, p, sizeof(data));
18987 data.showHeader = 0;
18988 data.cMode = data.mode = MODE_Semi;
18989 if( nArg==2 && optionMatch(azArg[1], "indent") ){
18990 data.cMode = data.mode = MODE_Pretty;
18994 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18996 goto meta_command_exit;
18999 rc = sqlite3_exec(p->db,
19001 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
19002 " FROM sqlite_schema UNION ALL"
19003 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
19004 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
19008 if( rc==SQLITE_OK ){
19009 sqlite3_stmt *pStmt;
19010 rc = sqlite3_prepare_v2(p->db,
19011 "SELECT rowid FROM sqlite_schema"
19012 " WHERE name GLOB 'sqlite_stat[134]'",
19014 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
19015 sqlite3_finalize(pStmt);
19018 raw_printf(p->out, "/* No STAT tables available */\n");
19020 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19021 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
19022 callback, &data, 0);
19023 data.cMode = data.mode = MODE_Insert;
19024 data.zDestTable = "sqlite_stat1";
19025 shell_exec(&data, "SELECT * FROM sqlite_stat1", 0);
19026 data.zDestTable = "sqlite_stat4";
19027 shell_exec(&data, "SELECT * FROM sqlite_stat4", 0);
19028 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
19032 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
19034 p->showHeader = booleanValue(azArg[1]);
19035 p->shellFlgs |= SHFLG_HeaderSet;
19037 raw_printf(stderr, "Usage: .headers on|off\n");
19042 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
19044 n = showHelp(p->out, azArg[1]);
19046 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
19049 showHelp(p->out, 0);
19053 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
19054 char *zTable = 0; /* Insert data into this table */
19055 char *zFile = 0; /* Name of file to extra content from */
19056 sqlite3_stmt *pStmt = NULL; /* A statement */
19057 int nCol; /* Number of columns in the table */
19058 int nByte; /* Number of bytes in an SQL string */
19059 int i, j; /* Loop counters */
19060 int needCommit; /* True to COMMIT or ROLLBACK at end */
19061 int nSep; /* Number of bytes in p->colSeparator[] */
19062 char *zSql; /* An SQL statement */
19063 ImportCtx sCtx; /* Reader context */
19064 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
19065 int eVerbose = 0; /* Larger for more console output */
19066 int nSkip = 0; /* Initial lines to skip */
19067 int useOutputMode = 1; /* Use output mode to determine separators */
19069 memset(&sCtx, 0, sizeof(sCtx));
19070 if( p->mode==MODE_Ascii ){
19071 xRead = ascii_read_one_field;
19073 xRead = csv_read_one_field;
19075 for(i=1; i<nArg; i++){
19076 char *z = azArg[i];
19077 if( z[0]=='-' && z[1]=='-' ) z++;
19081 }else if( zTable==0 ){
19084 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
19085 showHelp(p->out, "import");
19087 goto meta_command_exit;
19089 }else if( strcmp(z,"-v")==0 ){
19091 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
19092 nSkip = integerValue(azArg[++i]);
19093 }else if( strcmp(z,"-ascii")==0 ){
19094 sCtx.cColSep = SEP_Unit[0];
19095 sCtx.cRowSep = SEP_Record[0];
19096 xRead = ascii_read_one_field;
19098 }else if( strcmp(z,"-csv")==0 ){
19099 sCtx.cColSep = ',';
19100 sCtx.cRowSep = '\n';
19101 xRead = csv_read_one_field;
19104 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
19105 showHelp(p->out, "import");
19107 goto meta_command_exit;
19111 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
19112 zFile==0 ? "FILE" : "TABLE");
19113 showHelp(p->out, "import");
19115 goto meta_command_exit;
19119 if( useOutputMode ){
19120 /* If neither the --csv or --ascii options are specified, then set
19121 ** the column and row separator characters from the output mode. */
19122 nSep = strlen30(p->colSeparator);
19125 "Error: non-null column separator required for import\n");
19127 goto meta_command_exit;
19131 "Error: multi-character column separators not allowed"
19134 goto meta_command_exit;
19136 nSep = strlen30(p->rowSeparator);
19139 "Error: non-null row separator required for import\n");
19141 goto meta_command_exit;
19143 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
19144 /* When importing CSV (only), if the row separator is set to the
19145 ** default output row separator, change it to the default input
19146 ** row separator. This avoids having to maintain different input
19147 ** and output row separators. */
19148 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19149 nSep = strlen30(p->rowSeparator);
19152 raw_printf(stderr, "Error: multi-character row separators not allowed"
19155 goto meta_command_exit;
19157 sCtx.cColSep = p->colSeparator[0];
19158 sCtx.cRowSep = p->rowSeparator[0];
19160 sCtx.zFile = zFile;
19162 if( sCtx.zFile[0]=='|' ){
19163 #ifdef SQLITE_OMIT_POPEN
19164 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19166 goto meta_command_exit;
19168 sCtx.in = popen(sCtx.zFile+1, "r");
19169 sCtx.zFile = "<pipe>";
19170 sCtx.xCloser = pclose;
19173 sCtx.in = fopen(sCtx.zFile, "rb");
19174 sCtx.xCloser = fclose;
19177 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
19179 goto meta_command_exit;
19181 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
19184 zSep[0] = sCtx.cColSep;
19185 utf8_printf(p->out, "Column separator ");
19186 output_c_string(p->out, zSep);
19187 utf8_printf(p->out, ", row separator ");
19188 zSep[0] = sCtx.cRowSep;
19189 output_c_string(p->out, zSep);
19190 utf8_printf(p->out, "\n");
19192 while( (nSkip--)>0 ){
19193 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
19195 zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
19197 import_cleanup(&sCtx);
19198 shell_out_of_memory();
19200 nByte = strlen30(zSql);
19201 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19202 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
19203 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
19204 char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
19206 while( xRead(&sCtx) ){
19207 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
19209 if( sCtx.cTerm!=sCtx.cColSep ) break;
19212 sqlite3_free(zCreate);
19213 import_cleanup(&sCtx);
19214 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
19216 goto meta_command_exit;
19218 zCreate = sqlite3_mprintf("%z\n)", zCreate);
19220 utf8_printf(p->out, "%s\n", zCreate);
19222 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
19223 sqlite3_free(zCreate);
19225 utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
19226 sqlite3_errmsg(p->db));
19227 import_cleanup(&sCtx);
19229 goto meta_command_exit;
19231 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19233 sqlite3_free(zSql);
19235 if (pStmt) sqlite3_finalize(pStmt);
19236 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
19237 import_cleanup(&sCtx);
19239 goto meta_command_exit;
19241 nCol = sqlite3_column_count(pStmt);
19242 sqlite3_finalize(pStmt);
19244 if( nCol==0 ) return 0; /* no columns, no error */
19245 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
19247 import_cleanup(&sCtx);
19248 shell_out_of_memory();
19250 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
19251 j = strlen30(zSql);
19252 for(i=1; i<nCol; i++){
19259 utf8_printf(p->out, "Insert using: %s\n", zSql);
19261 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19262 sqlite3_free(zSql);
19264 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19265 if (pStmt) sqlite3_finalize(pStmt);
19266 import_cleanup(&sCtx);
19268 goto meta_command_exit;
19270 needCommit = sqlite3_get_autocommit(p->db);
19271 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
19273 int startLine = sCtx.nLine;
19274 for(i=0; i<nCol; i++){
19275 char *z = xRead(&sCtx);
19277 ** Did we reach end-of-file before finding any columns?
19278 ** If so, stop instead of NULL filling the remaining columns.
19280 if( z==0 && i==0 ) break;
19282 ** Did we reach end-of-file OR end-of-line before finding any
19283 ** columns in ASCII mode? If so, stop instead of NULL filling
19284 ** the remaining columns.
19286 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
19287 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
19288 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
19289 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19290 "filling the rest with NULL\n",
19291 sCtx.zFile, startLine, nCol, i+1);
19293 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
19296 if( sCtx.cTerm==sCtx.cColSep ){
19300 }while( sCtx.cTerm==sCtx.cColSep );
19301 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
19302 "extras ignored\n",
19303 sCtx.zFile, startLine, nCol, i);
19306 sqlite3_step(pStmt);
19307 rc = sqlite3_reset(pStmt);
19308 if( rc!=SQLITE_OK ){
19309 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
19310 startLine, sqlite3_errmsg(p->db));
19316 }while( sCtx.cTerm!=EOF );
19318 import_cleanup(&sCtx);
19319 sqlite3_finalize(pStmt);
19320 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
19322 utf8_printf(p->out,
19323 "Added %d rows with %d errors using %d lines of input\n",
19324 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
19328 #ifndef SQLITE_UNTESTABLE
19329 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
19331 char *zCollist = 0;
19332 sqlite3_stmt *pStmt;
19334 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
19335 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
19337 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
19338 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
19339 " .imposter off\n");
19340 /* Also allowed, but not documented:
19342 ** .imposter TABLE IMPOSTER
19344 ** where TABLE is a WITHOUT ROWID table. In that case, the
19345 ** imposter is another WITHOUT ROWID table with the columns in
19346 ** storage order. */
19348 goto meta_command_exit;
19352 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
19353 goto meta_command_exit;
19355 zSql = sqlite3_mprintf(
19356 "SELECT rootpage, 0 FROM sqlite_schema"
19357 " WHERE name='%q' AND type='index'"
19359 "SELECT rootpage, 1 FROM sqlite_schema"
19360 " WHERE name='%q' AND type='table'"
19361 " AND sql LIKE '%%without%%rowid%%'",
19364 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19365 sqlite3_free(zSql);
19366 if( sqlite3_step(pStmt)==SQLITE_ROW ){
19367 tnum = sqlite3_column_int(pStmt, 0);
19368 isWO = sqlite3_column_int(pStmt, 1);
19370 sqlite3_finalize(pStmt);
19371 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
19372 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19373 sqlite3_free(zSql);
19375 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19377 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
19380 if( sqlite3_column_int(pStmt,1)==-1 ){
19383 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
19387 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
19388 lenPK = (int)strlen(zCollist);
19391 zCollist = sqlite3_mprintf("\"%w\"", zCol);
19393 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
19396 sqlite3_finalize(pStmt);
19397 if( i==0 || tnum==0 ){
19398 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
19400 sqlite3_free(zCollist);
19401 goto meta_command_exit;
19403 if( lenPK==0 ) lenPK = 100000;
19404 zSql = sqlite3_mprintf(
19405 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
19406 azArg[2], zCollist, lenPK, zCollist);
19407 sqlite3_free(zCollist);
19408 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
19409 if( rc==SQLITE_OK ){
19410 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
19411 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
19413 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
19415 utf8_printf(stdout, "%s;\n", zSql);
19417 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
19418 azArg[1], isWO ? "table" : "index"
19422 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
19425 sqlite3_free(zSql);
19427 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
19429 #ifdef SQLITE_ENABLE_IOTRACE
19430 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
19431 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
19432 if( iotrace && iotrace!=stdout ) fclose(iotrace);
19435 sqlite3IoTrace = 0;
19436 }else if( strcmp(azArg[1], "-")==0 ){
19437 sqlite3IoTrace = iotracePrintf;
19440 iotrace = fopen(azArg[1], "w");
19442 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19443 sqlite3IoTrace = 0;
19446 sqlite3IoTrace = iotracePrintf;
19452 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
19453 static const struct {
19454 const char *zLimitName; /* Name of a limit */
19455 int limitCode; /* Integer code for that limit */
19457 { "length", SQLITE_LIMIT_LENGTH },
19458 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
19459 { "column", SQLITE_LIMIT_COLUMN },
19460 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
19461 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
19462 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
19463 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
19464 { "attached", SQLITE_LIMIT_ATTACHED },
19465 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
19466 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
19467 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
19468 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
19473 for(i=0; i<ArraySize(aLimit); i++){
19474 printf("%20s %d\n", aLimit[i].zLimitName,
19475 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
19477 }else if( nArg>3 ){
19478 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
19480 goto meta_command_exit;
19483 n2 = strlen30(azArg[1]);
19484 for(i=0; i<ArraySize(aLimit); i++){
19485 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
19489 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
19491 goto meta_command_exit;
19496 utf8_printf(stderr, "unknown limit: \"%s\"\n"
19497 "enter \".limits\" with no arguments for a list.\n",
19500 goto meta_command_exit;
19503 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
19504 (int)integerValue(azArg[2]));
19506 printf("%20s %d\n", aLimit[iLimit].zLimitName,
19507 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
19511 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
19513 lintDotCommand(p, azArg, nArg);
19516 #ifndef SQLITE_OMIT_LOAD_EXTENSION
19517 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
19518 const char *zFile, *zProc;
19521 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
19523 goto meta_command_exit;
19526 zProc = nArg>=3 ? azArg[2] : 0;
19528 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
19529 if( rc!=SQLITE_OK ){
19530 utf8_printf(stderr, "Error: %s\n", zErrMsg);
19531 sqlite3_free(zErrMsg);
19537 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
19539 raw_printf(stderr, "Usage: .log FILENAME\n");
19542 const char *zFile = azArg[1];
19543 output_file_close(p->pLog);
19544 p->pLog = output_file_open(zFile, 0);
19548 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
19549 const char *zMode = nArg>=2 ? azArg[1] : "";
19550 int n2 = strlen30(zMode);
19552 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
19553 p->mode = MODE_Line;
19554 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19555 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
19556 p->mode = MODE_Column;
19557 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
19560 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19561 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
19562 p->mode = MODE_List;
19563 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
19564 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19565 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
19566 p->mode = MODE_Html;
19567 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
19568 p->mode = MODE_Tcl;
19569 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
19570 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19571 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
19572 p->mode = MODE_Csv;
19573 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19574 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19575 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
19576 p->mode = MODE_List;
19577 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
19578 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
19579 p->mode = MODE_Insert;
19580 set_table_name(p, nArg>=3 ? azArg[2] : "table");
19581 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
19582 p->mode = MODE_Quote;
19583 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19584 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
19585 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
19586 p->mode = MODE_Ascii;
19587 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
19588 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
19589 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
19590 p->mode = MODE_Markdown;
19591 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
19592 p->mode = MODE_Table;
19593 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
19594 p->mode = MODE_Box;
19595 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
19596 p->mode = MODE_Json;
19597 }else if( nArg==1 ){
19598 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
19600 raw_printf(stderr, "Error: mode should be one of: "
19601 "ascii box column csv html insert json line list markdown "
19602 "quote table tabs tcl\n");
19605 p->cMode = p->mode;
19608 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
19610 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
19611 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
19613 raw_printf(stderr, "Usage: .nullvalue STRING\n");
19618 #ifdef SQLITE_DEBUG
19619 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
19621 for(i=1; i<nArg; i++){
19622 const char *z = azArg[i];
19623 if( z[0]=='-' && z[1]=='-' ) z++;
19624 if( strcmp(z,"-repeat")==0 ){
19626 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
19629 oomRepeat = (int)integerValue(azArg[++i]);
19631 }else if( IsDigit(z[0]) ){
19632 oomCounter = (int)integerValue(azArg[i]);
19634 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
19635 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
19640 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
19641 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
19644 #endif /* SQLITE_DEBUG */
19646 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
19647 char *zNewFilename = 0; /* Name of the database file to open */
19648 int iName = 1; /* Index in azArg[] of the filename */
19649 int newFlag = 0; /* True to delete file before opening */
19650 /* Close the existing database */
19651 session_close_all(p);
19654 p->zDbFilename = 0;
19655 sqlite3_free(p->zFreeOnClose);
19656 p->zFreeOnClose = 0;
19657 p->openMode = SHELL_OPEN_UNSPEC;
19660 /* Check for command-line arguments */
19661 for(iName=1; iName<nArg; iName++){
19662 const char *z = azArg[iName];
19663 if( optionMatch(z,"new") ){
19665 #ifdef SQLITE_HAVE_ZLIB
19666 }else if( optionMatch(z, "zip") ){
19667 p->openMode = SHELL_OPEN_ZIPFILE;
19669 }else if( optionMatch(z, "append") ){
19670 p->openMode = SHELL_OPEN_APPENDVFS;
19671 }else if( optionMatch(z, "readonly") ){
19672 p->openMode = SHELL_OPEN_READONLY;
19673 }else if( optionMatch(z, "nofollow") ){
19674 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
19675 #ifndef SQLITE_OMIT_DESERIALIZE
19676 }else if( optionMatch(z, "deserialize") ){
19677 p->openMode = SHELL_OPEN_DESERIALIZE;
19678 }else if( optionMatch(z, "hexdb") ){
19679 p->openMode = SHELL_OPEN_HEXDB;
19680 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
19681 p->szMax = integerValue(azArg[++iName]);
19682 #endif /* SQLITE_OMIT_DESERIALIZE */
19683 }else if( z[0]=='-' ){
19684 utf8_printf(stderr, "unknown option: %s\n", z);
19686 goto meta_command_exit;
19687 }else if( zNewFilename ){
19688 utf8_printf(stderr, "extra argument: \"%s\"\n", z);
19690 goto meta_command_exit;
19692 zNewFilename = sqlite3_mprintf("%s", z);
19695 /* If a filename is specified, try to open it first */
19696 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
19697 if( newFlag ) shellDeleteFile(zNewFilename);
19698 p->zDbFilename = zNewFilename;
19699 open_db(p, OPEN_DB_KEEPALIVE);
19701 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
19702 sqlite3_free(zNewFilename);
19704 p->zFreeOnClose = zNewFilename;
19708 /* As a fall-back open a TEMP database */
19709 p->zDbFilename = 0;
19715 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
19716 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
19723 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
19728 }else if( strncmp(azArg[0],"once",n)==0 ){
19731 for(i=1; i<nArg; i++){
19732 char *z = azArg[i];
19734 if( z[1]=='-' ) z++;
19735 if( strcmp(z,"-bom")==0 ){
19737 }else if( c!='e' && strcmp(z,"-x")==0 ){
19738 eMode = 'x'; /* spreadsheet */
19739 }else if( c!='e' && strcmp(z,"-e")==0 ){
19740 eMode = 'e'; /* text editor */
19742 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
19744 showHelp(p->out, azArg[0]);
19746 goto meta_command_exit;
19748 }else if( zFile==0 && eMode!='e' && eMode!='x' ){
19749 zFile = sqlite3_mprintf("%s", z);
19750 if( zFile[0]=='|' ){
19751 while( i+1<nArg ) zFile = sqlite3_mprintf("%z %s", zFile, azArg[++i]);
19755 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
19757 showHelp(p->out, azArg[0]);
19759 sqlite3_free(zFile);
19760 goto meta_command_exit;
19763 if( zFile==0 ) zFile = sqlite3_mprintf("stdout");
19770 #ifndef SQLITE_NOHAVE_SYSTEM
19771 if( eMode=='e' || eMode=='x' ){
19775 /* spreadsheet mode. Output as CSV. */
19776 newTempFile(p, "csv");
19777 ShellClearFlag(p, SHFLG_Echo);
19778 p->mode = MODE_Csv;
19779 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
19780 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
19782 /* text editor mode */
19783 newTempFile(p, "txt");
19786 sqlite3_free(zFile);
19787 zFile = sqlite3_mprintf("%s", p->zTempFile);
19789 #endif /* SQLITE_NOHAVE_SYSTEM */
19790 if( zFile[0]=='|' ){
19791 #ifdef SQLITE_OMIT_POPEN
19792 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
19796 p->out = popen(zFile + 1, "w");
19798 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
19802 if( bBOM ) fprintf(p->out,"\357\273\277");
19803 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19807 p->out = output_file_open(zFile, bTxtMode);
19809 if( strcmp(zFile,"off")!=0 ){
19810 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
19815 if( bBOM ) fprintf(p->out,"\357\273\277");
19816 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
19819 sqlite3_free(zFile);
19822 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
19824 if( nArg<=1 ) goto parameter_syntax_error;
19826 /* .parameter clear
19827 ** Clear all bind parameters by dropping the TEMP table that holds them.
19829 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
19830 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
19835 ** List all bind parameters.
19837 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
19838 sqlite3_stmt *pStmt = 0;
19841 rx = sqlite3_prepare_v2(p->db,
19842 "SELECT max(length(key)) "
19843 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19844 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
19845 len = sqlite3_column_int(pStmt, 0);
19846 if( len>40 ) len = 40;
19848 sqlite3_finalize(pStmt);
19851 rx = sqlite3_prepare_v2(p->db,
19852 "SELECT key, quote(value) "
19853 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
19854 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19855 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
19856 sqlite3_column_text(pStmt,1));
19858 sqlite3_finalize(pStmt);
19863 ** Make sure the TEMP table used to hold bind parameters exists.
19864 ** Create it if necessary.
19866 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
19867 bind_table_init(p);
19870 /* .parameter set NAME VALUE
19871 ** Set or reset a bind parameter. NAME should be the full parameter
19872 ** name exactly as it appears in the query. (ex: $abc, @def). The
19873 ** VALUE can be in either SQL literal notation, or if not it will be
19874 ** understood to be a text string.
19876 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
19879 sqlite3_stmt *pStmt;
19880 const char *zKey = azArg[2];
19881 const char *zValue = azArg[3];
19882 bind_table_init(p);
19883 zSql = sqlite3_mprintf(
19884 "REPLACE INTO temp.sqlite_parameters(key,value)"
19885 "VALUES(%Q,%s);", zKey, zValue);
19886 if( zSql==0 ) shell_out_of_memory();
19888 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19889 sqlite3_free(zSql);
19890 if( rx!=SQLITE_OK ){
19891 sqlite3_finalize(pStmt);
19893 zSql = sqlite3_mprintf(
19894 "REPLACE INTO temp.sqlite_parameters(key,value)"
19895 "VALUES(%Q,%Q);", zKey, zValue);
19896 if( zSql==0 ) shell_out_of_memory();
19897 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19898 sqlite3_free(zSql);
19899 if( rx!=SQLITE_OK ){
19900 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
19901 sqlite3_finalize(pStmt);
19906 sqlite3_step(pStmt);
19907 sqlite3_finalize(pStmt);
19910 /* .parameter unset NAME
19911 ** Remove the NAME binding from the parameter binding table, if it
19914 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
19915 char *zSql = sqlite3_mprintf(
19916 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
19917 if( zSql==0 ) shell_out_of_memory();
19918 sqlite3_exec(p->db, zSql, 0, 0, 0);
19919 sqlite3_free(zSql);
19921 /* If no command name matches, show a syntax error */
19922 parameter_syntax_error:
19923 showHelp(p->out, "parameter");
19926 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
19928 for(i=1; i<nArg; i++){
19929 if( i>1 ) raw_printf(p->out, " ");
19930 utf8_printf(p->out, "%s", azArg[i]);
19932 raw_printf(p->out, "\n");
19935 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
19936 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
19939 p->flgProgress = 0;
19942 for(i=1; i<nArg; i++){
19943 const char *z = azArg[i];
19946 if( z[0]=='-' ) z++;
19947 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19948 p->flgProgress |= SHELL_PROGRESS_QUIET;
19951 if( strcmp(z,"reset")==0 ){
19952 p->flgProgress |= SHELL_PROGRESS_RESET;
19955 if( strcmp(z,"once")==0 ){
19956 p->flgProgress |= SHELL_PROGRESS_ONCE;
19959 if( strcmp(z,"limit")==0 ){
19961 utf8_printf(stderr, "Error: missing argument on --limit\n");
19963 goto meta_command_exit;
19965 p->mxProgress = (int)integerValue(azArg[++i]);
19969 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19971 goto meta_command_exit;
19973 nn = (int)integerValue(z);
19977 sqlite3_progress_handler(p->db, nn, progress_handler, p);
19979 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19981 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19983 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19986 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19990 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19994 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19995 FILE *inSaved = p->in;
19996 int savedLineno = p->lineno;
19998 raw_printf(stderr, "Usage: .read FILE\n");
20000 goto meta_command_exit;
20002 if( azArg[1][0]=='|' ){
20003 #ifdef SQLITE_OMIT_POPEN
20004 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
20008 p->in = popen(azArg[1]+1, "r");
20010 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
20013 rc = process_input(p);
20017 }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
20018 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
20021 rc = process_input(p);
20025 p->lineno = savedLineno;
20028 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
20029 const char *zSrcFile;
20032 sqlite3_backup *pBackup;
20036 zSrcFile = azArg[1];
20038 }else if( nArg==3 ){
20039 zSrcFile = azArg[2];
20042 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
20044 goto meta_command_exit;
20046 rc = sqlite3_open(zSrcFile, &pSrc);
20047 if( rc!=SQLITE_OK ){
20048 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
20053 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
20055 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20059 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
20060 || rc==SQLITE_BUSY ){
20061 if( rc==SQLITE_BUSY ){
20062 if( nTimeout++ >= 3 ) break;
20063 sqlite3_sleep(100);
20066 sqlite3_backup_finish(pBackup);
20067 if( rc==SQLITE_DONE ){
20069 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
20070 raw_printf(stderr, "Error: source database is busy\n");
20073 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20079 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
20081 p->scanstatsOn = (u8)booleanValue(azArg[1]);
20082 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
20083 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
20086 raw_printf(stderr, "Usage: .scanstats on|off\n");
20091 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
20095 const char *zDiv = "(";
20096 const char *zName = 0;
20099 int bNoSystemTabs = 0;
20103 memcpy(&data, p, sizeof(data));
20104 data.showHeader = 0;
20105 data.cMode = data.mode = MODE_Semi;
20106 initText(&sSelect);
20107 for(ii=1; ii<nArg; ii++){
20108 if( optionMatch(azArg[ii],"indent") ){
20109 data.cMode = data.mode = MODE_Pretty;
20110 }else if( optionMatch(azArg[ii],"debug") ){
20112 }else if( optionMatch(azArg[ii],"nosys") ){
20114 }else if( azArg[ii][0]=='-' ){
20115 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
20117 goto meta_command_exit;
20118 }else if( zName==0 ){
20121 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
20123 goto meta_command_exit;
20127 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
20128 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
20129 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
20130 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
20132 char *new_argv[2], *new_colv[2];
20133 new_argv[0] = sqlite3_mprintf(
20134 "CREATE TABLE %s (\n"
20137 " tbl_name text,\n"
20138 " rootpage integer,\n"
20142 new_colv[0] = "sql";
20144 callback(&data, 1, new_argv, new_colv);
20145 sqlite3_free(new_argv[0]);
20149 sqlite3_stmt *pStmt = 0;
20150 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
20153 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
20154 sqlite3_finalize(pStmt);
20156 goto meta_command_exit;
20158 appendText(&sSelect, "SELECT sql FROM", 0);
20160 while( sqlite3_step(pStmt)==SQLITE_ROW ){
20161 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
20163 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
20164 appendText(&sSelect, zDiv, 0);
20165 zDiv = " UNION ALL ";
20166 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
20167 if( sqlite3_stricmp(zDb, "main")!=0 ){
20168 appendText(&sSelect, zDb, '\'');
20170 appendText(&sSelect, "NULL", 0);
20172 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
20173 appendText(&sSelect, zScNum, 0);
20174 appendText(&sSelect, " AS snum, ", 0);
20175 appendText(&sSelect, zDb, '\'');
20176 appendText(&sSelect, " AS sname FROM ", 0);
20177 appendText(&sSelect, zDb, quoteChar(zDb));
20178 appendText(&sSelect, ".sqlite_schema", 0);
20180 sqlite3_finalize(pStmt);
20181 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
20183 appendText(&sSelect,
20184 " UNION ALL SELECT shell_module_schema(name),"
20185 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
20189 appendText(&sSelect, ") WHERE ", 0);
20191 char *zQarg = sqlite3_mprintf("%Q", zName);
20192 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
20193 strchr(zName, '[') != 0;
20194 if( strchr(zName, '.') ){
20195 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
20197 appendText(&sSelect, "lower(tbl_name)", 0);
20199 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
20200 appendText(&sSelect, zQarg, 0);
20202 appendText(&sSelect, " ESCAPE '\\' ", 0);
20204 appendText(&sSelect, " AND ", 0);
20205 sqlite3_free(zQarg);
20207 if( bNoSystemTabs ){
20208 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
20210 appendText(&sSelect, "sql IS NOT NULL"
20211 " ORDER BY snum, rowid", 0);
20213 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
20215 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
20217 freeText(&sSelect);
20220 utf8_printf(stderr,"Error: %s\n", zErrMsg);
20221 sqlite3_free(zErrMsg);
20223 }else if( rc != SQLITE_OK ){
20224 raw_printf(stderr,"Error: querying schema information\n");
20231 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
20232 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
20233 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 1, &x);
20236 #if defined(SQLITE_ENABLE_SESSION)
20237 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
20238 OpenSession *pSession = &p->aSession[0];
20239 char **azCmd = &azArg[1];
20241 int nCmd = nArg - 1;
20243 if( nArg<=1 ) goto session_syntax_error;
20246 for(iSes=0; iSes<p->nSession; iSes++){
20247 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
20249 if( iSes<p->nSession ){
20250 pSession = &p->aSession[iSes];
20254 pSession = &p->aSession[0];
20259 /* .session attach TABLE
20260 ** Invoke the sqlite3session_attach() interface to attach a particular
20261 ** table so that it is never filtered.
20263 if( strcmp(azCmd[0],"attach")==0 ){
20264 if( nCmd!=2 ) goto session_syntax_error;
20265 if( pSession->p==0 ){
20267 raw_printf(stderr, "ERROR: No sessions are open\n");
20269 rc = sqlite3session_attach(pSession->p, azCmd[1]);
20271 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
20277 /* .session changeset FILE
20278 ** .session patchset FILE
20279 ** Write a changeset or patchset into a file. The file is overwritten.
20281 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
20283 if( nCmd!=2 ) goto session_syntax_error;
20284 if( pSession->p==0 ) goto session_not_open;
20285 out = fopen(azCmd[1], "wb");
20287 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
20292 if( azCmd[0][0]=='c' ){
20293 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
20295 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
20298 printf("Error: error code %d\n", rc);
20302 && fwrite(pChng, szChng, 1, out)!=1 ){
20303 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
20306 sqlite3_free(pChng);
20312 ** Close the identified session
20314 if( strcmp(azCmd[0], "close")==0 ){
20315 if( nCmd!=1 ) goto session_syntax_error;
20317 session_close(pSession);
20318 p->aSession[iSes] = p->aSession[--p->nSession];
20322 /* .session enable ?BOOLEAN?
20323 ** Query or set the enable flag
20325 if( strcmp(azCmd[0], "enable")==0 ){
20327 if( nCmd>2 ) goto session_syntax_error;
20328 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20330 ii = sqlite3session_enable(pSession->p, ii);
20331 utf8_printf(p->out, "session %s enable flag = %d\n",
20332 pSession->zName, ii);
20336 /* .session filter GLOB ....
20337 ** Set a list of GLOB patterns of table names to be excluded.
20339 if( strcmp(azCmd[0], "filter")==0 ){
20341 if( nCmd<2 ) goto session_syntax_error;
20343 for(ii=0; ii<pSession->nFilter; ii++){
20344 sqlite3_free(pSession->azFilter[ii]);
20346 sqlite3_free(pSession->azFilter);
20347 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
20348 pSession->azFilter = sqlite3_malloc( nByte );
20349 if( pSession->azFilter==0 ){
20350 raw_printf(stderr, "Error: out or memory\n");
20353 for(ii=1; ii<nCmd; ii++){
20354 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
20356 pSession->nFilter = ii-1;
20360 /* .session indirect ?BOOLEAN?
20361 ** Query or set the indirect flag
20363 if( strcmp(azCmd[0], "indirect")==0 ){
20365 if( nCmd>2 ) goto session_syntax_error;
20366 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
20368 ii = sqlite3session_indirect(pSession->p, ii);
20369 utf8_printf(p->out, "session %s indirect flag = %d\n",
20370 pSession->zName, ii);
20374 /* .session isempty
20375 ** Determine if the session is empty
20377 if( strcmp(azCmd[0], "isempty")==0 ){
20379 if( nCmd!=1 ) goto session_syntax_error;
20381 ii = sqlite3session_isempty(pSession->p);
20382 utf8_printf(p->out, "session %s isempty flag = %d\n",
20383 pSession->zName, ii);
20388 ** List all currently open sessions
20390 if( strcmp(azCmd[0],"list")==0 ){
20391 for(i=0; i<p->nSession; i++){
20392 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
20396 /* .session open DB NAME
20397 ** Open a new session called NAME on the attached database DB.
20398 ** DB is normally "main".
20400 if( strcmp(azCmd[0],"open")==0 ){
20402 if( nCmd!=3 ) goto session_syntax_error;
20404 if( zName[0]==0 ) goto session_syntax_error;
20405 for(i=0; i<p->nSession; i++){
20406 if( strcmp(p->aSession[i].zName,zName)==0 ){
20407 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
20408 goto meta_command_exit;
20411 if( p->nSession>=ArraySize(p->aSession) ){
20412 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
20413 goto meta_command_exit;
20415 pSession = &p->aSession[p->nSession];
20416 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
20418 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
20420 goto meta_command_exit;
20422 pSession->nFilter = 0;
20423 sqlite3session_table_filter(pSession->p, session_filter, pSession);
20425 pSession->zName = sqlite3_mprintf("%s", zName);
20427 /* If no command name matches, show a syntax error */
20428 session_syntax_error:
20429 showHelp(p->out, "session");
20433 #ifdef SQLITE_DEBUG
20434 /* Undocumented commands for internal testing. Subject to change
20435 ** without notice. */
20436 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
20437 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
20439 for(i=1; i<nArg; i++){
20440 v = booleanValue(azArg[i]);
20441 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
20444 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
20445 int i; sqlite3_int64 v;
20446 for(i=1; i<nArg; i++){
20448 v = integerValue(azArg[i]);
20449 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
20450 utf8_printf(p->out, "%s", zBuf);
20456 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
20457 int bIsInit = 0; /* True to initialize the SELFTEST table */
20458 int bVerbose = 0; /* Verbose output */
20459 int bSelftestExists; /* True if SELFTEST already exists */
20460 int i, k; /* Loop counters */
20461 int nTest = 0; /* Number of tests runs */
20462 int nErr = 0; /* Number of errors seen */
20463 ShellText str; /* Answer for a query */
20464 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
20467 for(i=1; i<nArg; i++){
20468 const char *z = azArg[i];
20469 if( z[0]=='-' && z[1]=='-' ) z++;
20470 if( strcmp(z,"-init")==0 ){
20473 if( strcmp(z,"-v")==0 ){
20477 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20478 azArg[i], azArg[0]);
20479 raw_printf(stderr, "Should be one of: --init -v\n");
20481 goto meta_command_exit;
20484 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
20486 bSelftestExists = 0;
20488 bSelftestExists = 1;
20491 createSelftestTable(p);
20492 bSelftestExists = 1;
20495 appendText(&str, "x", 0);
20496 for(k=bSelftestExists; k>=0; k--){
20498 rc = sqlite3_prepare_v2(p->db,
20499 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
20502 rc = sqlite3_prepare_v2(p->db,
20503 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
20504 " (1,'run','PRAGMA integrity_check','ok')",
20508 raw_printf(stderr, "Error querying the selftest table\n");
20510 sqlite3_finalize(pStmt);
20511 goto meta_command_exit;
20513 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
20514 int tno = sqlite3_column_int(pStmt, 0);
20515 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
20516 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
20517 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
20521 char *zQuote = sqlite3_mprintf("%q", zSql);
20522 printf("%d: %s %s\n", tno, zOp, zSql);
20523 sqlite3_free(zQuote);
20525 if( strcmp(zOp,"memo")==0 ){
20526 utf8_printf(p->out, "%s\n", zSql);
20528 if( strcmp(zOp,"run")==0 ){
20532 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
20535 utf8_printf(p->out, "Result: %s\n", str.z);
20537 if( rc || zErrMsg ){
20540 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
20541 sqlite3_free(zErrMsg);
20542 }else if( strcmp(zAns,str.z)!=0 ){
20545 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
20546 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
20550 utf8_printf(stderr,
20551 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
20555 } /* End loop over rows of content from SELFTEST */
20556 sqlite3_finalize(pStmt);
20557 } /* End loop over k */
20559 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
20562 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
20563 if( nArg<2 || nArg>3 ){
20564 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
20568 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
20569 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
20572 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
20573 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
20577 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
20578 const char *zLike = 0; /* Which table to checksum. 0 means everything */
20579 int i; /* Loop counter */
20580 int bSchema = 0; /* Also hash the schema */
20581 int bSeparate = 0; /* Hash each table separately */
20582 int iSize = 224; /* Hash algorithm to use */
20583 int bDebug = 0; /* Only show the query that would have run */
20584 sqlite3_stmt *pStmt; /* For querying tables names */
20585 char *zSql; /* SQL to be run */
20586 char *zSep; /* Separator */
20587 ShellText sSql; /* Complete SQL for the query to run the hash */
20588 ShellText sQuery; /* Set of queries used to read all content */
20590 for(i=1; i<nArg; i++){
20591 const char *z = azArg[i];
20594 if( z[0]=='-' ) z++;
20595 if( strcmp(z,"schema")==0 ){
20598 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
20599 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
20601 iSize = atoi(&z[5]);
20603 if( strcmp(z,"debug")==0 ){
20607 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
20608 azArg[i], azArg[0]);
20609 showHelp(p->out, azArg[0]);
20611 goto meta_command_exit;
20614 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
20616 goto meta_command_exit;
20620 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
20624 zSql = "SELECT lower(name) FROM sqlite_schema"
20625 " WHERE type='table' AND coalesce(rootpage,0)>1"
20626 " UNION ALL SELECT 'sqlite_schema'"
20627 " ORDER BY 1 collate nocase";
20629 zSql = "SELECT lower(name) FROM sqlite_schema"
20630 " WHERE type='table' AND coalesce(rootpage,0)>1"
20631 " AND name NOT LIKE 'sqlite_%'"
20632 " ORDER BY 1 collate nocase";
20634 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
20637 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
20639 while( SQLITE_ROW==sqlite3_step(pStmt) ){
20640 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
20641 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
20642 if( strncmp(zTab, "sqlite_",7)!=0 ){
20643 appendText(&sQuery,"SELECT * FROM ", 0);
20644 appendText(&sQuery,zTab,'"');
20645 appendText(&sQuery," NOT INDEXED;", 0);
20646 }else if( strcmp(zTab, "sqlite_schema")==0 ){
20647 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
20648 " ORDER BY name;", 0);
20649 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
20650 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
20651 " ORDER BY name;", 0);
20652 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
20653 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
20654 " ORDER BY tbl,idx;", 0);
20655 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
20656 appendText(&sQuery, "SELECT * FROM ", 0);
20657 appendText(&sQuery, zTab, 0);
20658 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
20660 appendText(&sSql, zSep, 0);
20661 appendText(&sSql, sQuery.z, '\'');
20663 appendText(&sSql, ",", 0);
20664 appendText(&sSql, zTab, '\'');
20667 sqlite3_finalize(pStmt);
20669 zSql = sqlite3_mprintf(
20671 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
20672 " FROM [sha3sum$query]",
20675 zSql = sqlite3_mprintf(
20677 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
20678 " FROM [sha3sum$query]",
20684 utf8_printf(p->out, "%s\n", zSql);
20686 shell_exec(p, zSql, 0);
20688 sqlite3_free(zSql);
20691 #ifndef SQLITE_NOHAVE_SYSTEM
20693 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
20698 raw_printf(stderr, "Usage: .system COMMAND\n");
20700 goto meta_command_exit;
20702 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
20703 for(i=2; i<nArg; i++){
20704 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
20708 sqlite3_free(zCmd);
20709 if( x ) raw_printf(stderr, "System command returns %d\n", x);
20711 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
20713 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
20714 static const char *azBool[] = { "off", "on", "trigger", "full"};
20718 raw_printf(stderr, "Usage: .show\n");
20720 goto meta_command_exit;
20722 utf8_printf(p->out, "%12.12s: %s\n","echo",
20723 azBool[ShellHasFlag(p, SHFLG_Echo)]);
20724 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
20725 utf8_printf(p->out, "%12.12s: %s\n","explain",
20726 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
20727 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
20728 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
20729 utf8_printf(p->out, "%12.12s: ", "nullvalue");
20730 output_c_string(p->out, p->nullValue);
20731 raw_printf(p->out, "\n");
20732 utf8_printf(p->out,"%12.12s: %s\n","output",
20733 strlen30(p->outfile) ? p->outfile : "stdout");
20734 utf8_printf(p->out,"%12.12s: ", "colseparator");
20735 output_c_string(p->out, p->colSeparator);
20736 raw_printf(p->out, "\n");
20737 utf8_printf(p->out,"%12.12s: ", "rowseparator");
20738 output_c_string(p->out, p->rowSeparator);
20739 raw_printf(p->out, "\n");
20740 switch( p->statsOn ){
20741 case 0: zOut = "off"; break;
20742 default: zOut = "on"; break;
20743 case 2: zOut = "stmt"; break;
20744 case 3: zOut = "vmstep"; break;
20746 utf8_printf(p->out, "%12.12s: %s\n","stats", zOut);
20747 utf8_printf(p->out, "%12.12s: ", "width");
20748 for (i=0;i<p->nWidth;i++) {
20749 raw_printf(p->out, "%d ", p->colWidth[i]);
20751 raw_printf(p->out, "\n");
20752 utf8_printf(p->out, "%12.12s: %s\n", "filename",
20753 p->zDbFilename ? p->zDbFilename : "");
20756 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
20758 if( strcmp(azArg[1],"stmt")==0 ){
20760 }else if( strcmp(azArg[1],"vmstep")==0 ){
20763 p->statsOn = (u8)booleanValue(azArg[1]);
20765 }else if( nArg==1 ){
20766 display_stats(p->db, p, 0);
20768 raw_printf(stderr, "Usage: .stats ?on|off|stmt|vmstep?\n");
20773 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
20774 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
20775 || strncmp(azArg[0], "indexes", n)==0) )
20777 sqlite3_stmt *pStmt;
20784 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
20786 sqlite3_finalize(pStmt);
20787 return shellDatabaseError(p->db);
20790 if( nArg>2 && c=='i' ){
20791 /* It is an historical accident that the .indexes command shows an error
20792 ** when called with the wrong number of arguments whereas the .tables
20793 ** command does not. */
20794 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
20796 sqlite3_finalize(pStmt);
20797 goto meta_command_exit;
20799 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
20800 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
20801 if( zDbName==0 ) continue;
20802 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
20803 if( sqlite3_stricmp(zDbName, "main")==0 ){
20804 appendText(&s, "SELECT name FROM ", 0);
20806 appendText(&s, "SELECT ", 0);
20807 appendText(&s, zDbName, '\'');
20808 appendText(&s, "||'.'||name FROM ", 0);
20810 appendText(&s, zDbName, '"');
20811 appendText(&s, ".sqlite_schema ", 0);
20813 appendText(&s," WHERE type IN ('table','view')"
20814 " AND name NOT LIKE 'sqlite_%'"
20815 " AND name LIKE ?1", 0);
20817 appendText(&s," WHERE type='index'"
20818 " AND tbl_name LIKE ?1", 0);
20821 rc = sqlite3_finalize(pStmt);
20822 appendText(&s, " ORDER BY 1", 0);
20823 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
20825 if( rc ) return shellDatabaseError(p->db);
20827 /* Run the SQL statement prepared by the above block. Store the results
20828 ** as an array of nul-terminated strings in azResult[]. */
20832 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
20834 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
20836 while( sqlite3_step(pStmt)==SQLITE_ROW ){
20837 if( nRow>=nAlloc ){
20839 int n2 = nAlloc*2 + 10;
20840 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
20841 if( azNew==0 ) shell_out_of_memory();
20845 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
20846 if( 0==azResult[nRow] ) shell_out_of_memory();
20849 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
20850 rc = shellDatabaseError(p->db);
20853 /* Pretty-print the contents of array azResult[] to the output */
20854 if( rc==0 && nRow>0 ){
20855 int len, maxlen = 0;
20857 int nPrintCol, nPrintRow;
20858 for(i=0; i<nRow; i++){
20859 len = strlen30(azResult[i]);
20860 if( len>maxlen ) maxlen = len;
20862 nPrintCol = 80/(maxlen+2);
20863 if( nPrintCol<1 ) nPrintCol = 1;
20864 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
20865 for(i=0; i<nPrintRow; i++){
20866 for(j=i; j<nRow; j+=nPrintRow){
20867 char *zSp = j<nPrintRow ? "" : " ";
20868 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
20869 azResult[j] ? azResult[j]:"");
20871 raw_printf(p->out, "\n");
20875 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
20876 sqlite3_free(azResult);
20879 /* Begin redirecting output to the file "testcase-out.txt" */
20880 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
20882 p->out = output_file_open("testcase-out.txt", 0);
20884 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
20887 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
20889 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
20893 #ifndef SQLITE_UNTESTABLE
20894 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
20895 static const struct {
20896 const char *zCtrlName; /* Name of a test-control option */
20897 int ctrlCode; /* Integer code for that option */
20898 const char *zUsage; /* Usage notes */
20900 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
20901 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
20902 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
20903 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
20904 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
20905 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
20906 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
20907 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
20908 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
20909 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
20910 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
20911 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
20913 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
20915 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
20916 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
20917 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
20918 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
20919 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" },
20920 { "tune", SQLITE_TESTCTRL_TUNE, "ID VALUE" },
20924 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
20927 const char *zCmd = 0;
20930 zCmd = nArg>=2 ? azArg[1] : "help";
20932 /* The argument can optionally begin with "-" or "--" */
20933 if( zCmd[0]=='-' && zCmd[1] ){
20935 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
20938 /* --help lists all test-controls */
20939 if( strcmp(zCmd,"help")==0 ){
20940 utf8_printf(p->out, "Available test-controls:\n");
20941 for(i=0; i<ArraySize(aCtrl); i++){
20942 utf8_printf(p->out, " .testctrl %s %s\n",
20943 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
20946 goto meta_command_exit;
20949 /* convert testctrl text option to value. allow any unique prefix
20950 ** of the option name, or a numerical value. */
20951 n2 = strlen30(zCmd);
20952 for(i=0; i<ArraySize(aCtrl); i++){
20953 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
20955 testctrl = aCtrl[i].ctrlCode;
20958 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
20959 "Use \".testctrl --help\" for help\n", zCmd);
20961 goto meta_command_exit;
20966 utf8_printf(stderr,"Error: unknown test-control: %s\n"
20967 "Use \".testctrl --help\" for help\n", zCmd);
20971 /* sqlite3_test_control(int, db, int) */
20972 case SQLITE_TESTCTRL_OPTIMIZATIONS:
20974 unsigned int opt = (unsigned int)strtol(azArg[2], 0, 0);
20975 rc2 = sqlite3_test_control(testctrl, p->db, opt);
20980 /* sqlite3_test_control(int) */
20981 case SQLITE_TESTCTRL_PRNG_SAVE:
20982 case SQLITE_TESTCTRL_PRNG_RESTORE:
20983 case SQLITE_TESTCTRL_BYTEORDER:
20985 rc2 = sqlite3_test_control(testctrl);
20986 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20990 /* sqlite3_test_control(int, uint) */
20991 case SQLITE_TESTCTRL_PENDING_BYTE:
20993 unsigned int opt = (unsigned int)integerValue(azArg[2]);
20994 rc2 = sqlite3_test_control(testctrl, opt);
20999 /* sqlite3_test_control(int, int, sqlite3*) */
21000 case SQLITE_TESTCTRL_PRNG_SEED:
21001 if( nArg==3 || nArg==4 ){
21002 int ii = (int)integerValue(azArg[2]);
21004 if( ii==0 && strcmp(azArg[2],"random")==0 ){
21005 sqlite3_randomness(sizeof(ii),&ii);
21006 printf("-- random seed: %d\n", ii);
21012 /* Make sure the schema has been loaded */
21013 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
21015 rc2 = sqlite3_test_control(testctrl, ii, db);
21020 /* sqlite3_test_control(int, int) */
21021 case SQLITE_TESTCTRL_ASSERT:
21022 case SQLITE_TESTCTRL_ALWAYS:
21024 int opt = booleanValue(azArg[2]);
21025 rc2 = sqlite3_test_control(testctrl, opt);
21030 /* sqlite3_test_control(int, int) */
21031 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
21032 case SQLITE_TESTCTRL_NEVER_CORRUPT:
21034 int opt = booleanValue(azArg[2]);
21035 rc2 = sqlite3_test_control(testctrl, opt);
21040 /* sqlite3_test_control(sqlite3*) */
21041 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
21042 rc2 = sqlite3_test_control(testctrl, p->db);
21046 case SQLITE_TESTCTRL_IMPOSTER:
21048 rc2 = sqlite3_test_control(testctrl, p->db,
21050 integerValue(azArg[3]),
21051 integerValue(azArg[4]));
21056 case SQLITE_TESTCTRL_SEEK_COUNT: {
21058 rc2 = sqlite3_test_control(testctrl, p->db, &x);
21059 utf8_printf(p->out, "%llu\n", x);
21065 case SQLITE_TESTCTRL_PARSER_COVERAGE: {
21067 sqlite3_test_control(testctrl, p->out);
21073 #ifdef SQLITE_DEBUG
21074 case SQLITE_TESTCTRL_TUNE: {
21076 int id = (int)integerValue(azArg[2]);
21077 int val = (int)integerValue(azArg[3]);
21078 sqlite3_test_control(testctrl, id, &val);
21080 }else if( nArg==3 ){
21081 int id = (int)integerValue(azArg[2]);
21082 sqlite3_test_control(testctrl, -id, &rc2);
21084 }else if( nArg==2 ){
21088 rc2 = sqlite3_test_control(testctrl, -id, &val);
21089 if( rc2!=SQLITE_OK ) break;
21090 if( id>1 ) utf8_printf(p->out, " ");
21091 utf8_printf(p->out, "%d: %d", id, val);
21094 if( id>1 ) utf8_printf(p->out, "\n");
21102 if( isOk==0 && iCtrl>=0 ){
21103 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
21105 }else if( isOk==1 ){
21106 raw_printf(p->out, "%d\n", rc2);
21107 }else if( isOk==2 ){
21108 raw_printf(p->out, "0x%08x\n", rc2);
21111 #endif /* !defined(SQLITE_UNTESTABLE) */
21113 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
21115 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
21118 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
21120 enableTimer = booleanValue(azArg[1]);
21121 if( enableTimer && !HAS_TIMER ){
21122 raw_printf(stderr, "Error: timer not available on this system.\n");
21126 raw_printf(stderr, "Usage: .timer on|off\n");
21131 #ifndef SQLITE_OMIT_TRACE
21132 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
21136 for(jj=1; jj<nArg; jj++){
21137 const char *z = azArg[jj];
21139 if( optionMatch(z, "expanded") ){
21140 p->eTraceType = SHELL_TRACE_EXPANDED;
21142 #ifdef SQLITE_ENABLE_NORMALIZE
21143 else if( optionMatch(z, "normalized") ){
21144 p->eTraceType = SHELL_TRACE_NORMALIZED;
21147 else if( optionMatch(z, "plain") ){
21148 p->eTraceType = SHELL_TRACE_PLAIN;
21150 else if( optionMatch(z, "profile") ){
21151 mType |= SQLITE_TRACE_PROFILE;
21153 else if( optionMatch(z, "row") ){
21154 mType |= SQLITE_TRACE_ROW;
21156 else if( optionMatch(z, "stmt") ){
21157 mType |= SQLITE_TRACE_STMT;
21159 else if( optionMatch(z, "close") ){
21160 mType |= SQLITE_TRACE_CLOSE;
21163 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
21165 goto meta_command_exit;
21168 output_file_close(p->traceOut);
21169 p->traceOut = output_file_open(azArg[1], 0);
21172 if( p->traceOut==0 ){
21173 sqlite3_trace_v2(p->db, 0, 0, 0);
21175 if( mType==0 ) mType = SQLITE_TRACE_STMT;
21176 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
21179 #endif /* !defined(SQLITE_OMIT_TRACE) */
21181 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21182 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
21187 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
21189 goto meta_command_exit;
21193 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
21194 lenOpt = (int)strlen(zOpt);
21195 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
21196 assert( azArg[nArg]==0 );
21197 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
21199 for(ii=1; ii<nArg; ii++){
21200 sqlite3_create_module(p->db, azArg[ii], 0, 0);
21206 #if SQLITE_USER_AUTHENTICATION
21207 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
21209 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
21211 goto meta_command_exit;
21214 if( strcmp(azArg[1],"login")==0 ){
21216 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
21218 goto meta_command_exit;
21220 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
21221 strlen30(azArg[3]));
21223 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
21226 }else if( strcmp(azArg[1],"add")==0 ){
21228 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
21230 goto meta_command_exit;
21232 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21233 booleanValue(azArg[4]));
21235 raw_printf(stderr, "User-Add failed: %d\n", rc);
21238 }else if( strcmp(azArg[1],"edit")==0 ){
21240 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
21242 goto meta_command_exit;
21244 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
21245 booleanValue(azArg[4]));
21247 raw_printf(stderr, "User-Edit failed: %d\n", rc);
21250 }else if( strcmp(azArg[1],"delete")==0 ){
21252 raw_printf(stderr, "Usage: .user delete USER\n");
21254 goto meta_command_exit;
21256 rc = sqlite3_user_delete(p->db, azArg[2]);
21258 raw_printf(stderr, "User-Delete failed: %d\n", rc);
21262 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
21264 goto meta_command_exit;
21267 #endif /* SQLITE_USER_AUTHENTICATION */
21269 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
21270 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
21271 sqlite3_libversion(), sqlite3_sourceid());
21272 #if SQLITE_HAVE_ZLIB
21273 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
21275 #define CTIMEOPT_VAL_(opt) #opt
21276 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
21277 #if defined(__clang__) && defined(__clang_major__)
21278 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
21279 CTIMEOPT_VAL(__clang_minor__) "."
21280 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
21281 #elif defined(_MSC_VER)
21282 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
21283 #elif defined(__GNUC__) && defined(__VERSION__)
21284 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
21288 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
21289 const char *zDbName = nArg==2 ? azArg[1] : "main";
21290 sqlite3_vfs *pVfs = 0;
21292 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
21294 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
21295 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
21296 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
21297 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21302 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
21304 sqlite3_vfs *pCurrent = 0;
21306 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
21308 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
21309 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
21310 pVfs==pCurrent ? " <--- CURRENT" : "");
21311 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
21312 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
21313 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
21315 raw_printf(p->out, "-----------------------------------\n");
21320 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
21321 const char *zDbName = nArg==2 ? azArg[1] : "main";
21322 char *zVfsName = 0;
21324 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
21326 utf8_printf(p->out, "%s\n", zVfsName);
21327 sqlite3_free(zVfsName);
21332 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
21333 unsigned int x = nArg>=2 ? (unsigned int)integerValue(azArg[1]) : 0xffffffff;
21334 sqlite3_test_control(SQLITE_TESTCTRL_TRACEFLAGS, 3, &x);
21337 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
21339 assert( nArg<=ArraySize(azArg) );
21340 p->nWidth = nArg-1;
21341 p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
21342 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
21343 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
21344 for(j=1; j<nArg; j++){
21345 p->colWidth[j-1] = (int)integerValue(azArg[j]);
21350 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
21351 " \"%s\". Enter \".help\" for help\n", azArg[0]);
21358 if( p->outCount==0 ) output_reset(p);
21364 ** Return TRUE if a semicolon occurs anywhere in the first N characters
21367 static int line_contains_semicolon(const char *z, int N){
21369 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
21374 ** Test to see if a line consists entirely of whitespace.
21376 static int _all_whitespace(const char *z){
21378 if( IsSpace(z[0]) ) continue;
21379 if( *z=='/' && z[1]=='*' ){
21381 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
21382 if( *z==0 ) return 0;
21386 if( *z=='-' && z[1]=='-' ){
21388 while( *z && *z!='\n' ){ z++; }
21389 if( *z==0 ) return 1;
21398 ** Return TRUE if the line typed in is an SQL command terminator other
21399 ** than a semi-colon. The SQL Server style "go" command is understood
21400 ** as is the Oracle "/".
21402 static int line_is_command_terminator(const char *zLine){
21403 while( IsSpace(zLine[0]) ){ zLine++; };
21404 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
21405 return 1; /* Oracle */
21407 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
21408 && _all_whitespace(&zLine[2]) ){
21409 return 1; /* SQL Server */
21415 ** We need a default sqlite3_complete() implementation to use in case
21416 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
21417 ** any arbitrary text is a complete SQL statement. This is not very
21418 ** user-friendly, but it does seem to work.
21420 #ifdef SQLITE_OMIT_COMPLETE
21421 #define sqlite3_complete(x) 1
21425 ** Return true if zSql is a complete SQL statement. Return false if it
21426 ** ends in the middle of a string literal or C-style comment.
21428 static int line_is_complete(char *zSql, int nSql){
21430 if( zSql==0 ) return 1;
21433 rc = sqlite3_complete(zSql);
21439 ** Run a single line of SQL. Return the number of errors.
21441 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
21446 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
21447 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
21449 rc = shell_exec(p, zSql, &zErrMsg);
21451 if( rc || zErrMsg ){
21453 if( in!=0 || !stdin_is_interactive ){
21454 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
21455 "Error: near line %d:", startline);
21457 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
21460 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
21461 sqlite3_free(zErrMsg);
21464 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
21467 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
21468 raw_printf(p->out, "changes: %3d total_changes: %d\n",
21469 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
21476 ** Read input from *in and process it. If *in==0 then input
21477 ** is interactive - the user is typing it it. Otherwise, input
21478 ** is coming from a file or device. A prompt is issued and history
21479 ** is saved only if input is interactive. An interrupt signal will
21480 ** cause this routine to exit immediately, unless input is interactive.
21482 ** Return the number of errors.
21484 static int process_input(ShellState *p){
21485 char *zLine = 0; /* A single input line */
21486 char *zSql = 0; /* Accumulated SQL text */
21487 int nLine; /* Length of current line */
21488 int nSql = 0; /* Bytes of zSql[] used */
21489 int nAlloc = 0; /* Allocated zSql[] space */
21490 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
21491 int rc; /* Error code */
21492 int errCnt = 0; /* Number of errors seen */
21493 int startline = 0; /* Line number for start of current input */
21496 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
21498 zLine = one_input_line(p->in, zLine, nSql>0);
21501 if( p->in==0 && stdin_is_interactive ) printf("\n");
21504 if( seenInterrupt ){
21505 if( p->in!=0 ) break;
21509 if( nSql==0 && _all_whitespace(zLine) ){
21510 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21513 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
21514 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
21515 if( zLine[0]=='.' ){
21516 rc = do_meta_command(zLine, p);
21517 if( rc==2 ){ /* exit requested */
21525 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
21526 memcpy(zLine,";",2);
21528 nLine = strlen30(zLine);
21529 if( nSql+nLine+2>=nAlloc ){
21530 nAlloc = nSql+nLine+100;
21531 zSql = realloc(zSql, nAlloc);
21532 if( zSql==0 ) shell_out_of_memory();
21537 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
21538 assert( nAlloc>0 && zSql!=0 );
21539 memcpy(zSql, zLine+i, nLine+1-i);
21540 startline = p->lineno;
21543 zSql[nSql++] = '\n';
21544 memcpy(zSql+nSql, zLine, nLine+1);
21547 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
21548 && sqlite3_complete(zSql) ){
21549 errCnt += runOneSqlLine(p, zSql, p->in, startline);
21557 }else if( nSql && _all_whitespace(zSql) ){
21558 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
21562 if( nSql && !_all_whitespace(zSql) ){
21563 errCnt += runOneSqlLine(p, zSql, p->in, startline);
21571 ** Return a pathname which is the user's home directory. A
21572 ** 0 return indicates an error of some kind.
21574 static char *find_home_dir(int clearFlag){
21575 static char *home_dir = NULL;
21581 if( home_dir ) return home_dir;
21583 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
21584 && !defined(__RTP__) && !defined(_WRS_KERNEL)
21586 struct passwd *pwent;
21587 uid_t uid = getuid();
21588 if( (pwent=getpwuid(uid)) != NULL) {
21589 home_dir = pwent->pw_dir;
21594 #if defined(_WIN32_WCE)
21595 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
21600 #if defined(_WIN32) || defined(WIN32)
21602 home_dir = getenv("USERPROFILE");
21607 home_dir = getenv("HOME");
21610 #if defined(_WIN32) || defined(WIN32)
21612 char *zDrive, *zPath;
21614 zDrive = getenv("HOMEDRIVE");
21615 zPath = getenv("HOMEPATH");
21616 if( zDrive && zPath ){
21617 n = strlen30(zDrive) + strlen30(zPath) + 1;
21618 home_dir = malloc( n );
21619 if( home_dir==0 ) return 0;
21620 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
21627 #endif /* !_WIN32_WCE */
21630 int n = strlen30(home_dir) + 1;
21631 char *z = malloc( n );
21632 if( z ) memcpy(z, home_dir, n);
21640 ** Read input from the file given by sqliterc_override. Or if that
21641 ** parameter is NULL, take input from ~/.sqliterc
21643 ** Returns the number of errors.
21645 static void process_sqliterc(
21646 ShellState *p, /* Configuration data */
21647 const char *sqliterc_override /* Name of config file. NULL to use default */
21649 char *home_dir = NULL;
21650 const char *sqliterc = sqliterc_override;
21652 FILE *inSaved = p->in;
21653 int savedLineno = p->lineno;
21655 if (sqliterc == NULL) {
21656 home_dir = find_home_dir(0);
21658 raw_printf(stderr, "-- warning: cannot find home directory;"
21659 " cannot read ~/.sqliterc\n");
21662 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
21665 p->in = fopen(sqliterc,"rb");
21667 if( stdin_is_interactive ){
21668 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
21670 if( process_input(p) && bail_on_error ) exit(1);
21672 }else if( sqliterc_override!=0 ){
21673 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
21674 if( bail_on_error ) exit(1);
21677 p->lineno = savedLineno;
21678 sqlite3_free(zBuf);
21682 ** Show available command line options
21684 static const char zOptions[] =
21685 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
21686 " -A ARGS... run \".archive ARGS\" and exit\n"
21688 " -append append the database to the end of the file\n"
21689 " -ascii set output mode to 'ascii'\n"
21690 " -bail stop after hitting an error\n"
21691 " -batch force batch I/O\n"
21692 " -box set output mode to 'box'\n"
21693 " -column set output mode to 'column'\n"
21694 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
21695 " -csv set output mode to 'csv'\n"
21696 #if !defined(SQLITE_OMIT_DESERIALIZE)
21697 " -deserialize open the database using sqlite3_deserialize()\n"
21699 " -echo print commands before execution\n"
21700 " -init FILENAME read/process named file\n"
21701 " -[no]header turn headers on or off\n"
21702 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21703 " -heap SIZE Size of heap for memsys3 or memsys5\n"
21705 " -help show this message\n"
21706 " -html set output mode to HTML\n"
21707 " -interactive force interactive I/O\n"
21708 " -json set output mode to 'json'\n"
21709 " -line set output mode to 'line'\n"
21710 " -list set output mode to 'list'\n"
21711 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
21712 " -markdown set output mode to 'markdown'\n"
21713 #if !defined(SQLITE_OMIT_DESERIALIZE)
21714 " -maxsize N maximum size for a --deserialize database\n"
21716 " -memtrace trace all memory allocations and deallocations\n"
21717 " -mmap N default mmap size set to N\n"
21718 #ifdef SQLITE_ENABLE_MULTIPLEX
21719 " -multiplex enable the multiplexor VFS\n"
21721 " -newline SEP set output row separator. Default: '\\n'\n"
21722 " -nofollow refuse to open symbolic links to database files\n"
21723 " -nullvalue TEXT set text string for NULL values. Default ''\n"
21724 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
21725 " -quote set output mode to 'quote'\n"
21726 " -readonly open the database read-only\n"
21727 " -separator SEP set output column separator. Default: '|'\n"
21728 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21729 " -sorterref SIZE sorter references threshold size\n"
21731 " -stats print memory stats before each finalize\n"
21732 " -table set output mode to 'table'\n"
21733 " -tabs set output mode to 'tabs'\n"
21734 " -version show SQLite version\n"
21735 " -vfs NAME use NAME as the default VFS\n"
21736 #ifdef SQLITE_ENABLE_VFSTRACE
21737 " -vfstrace enable tracing of all VFS calls\n"
21739 #ifdef SQLITE_HAVE_ZLIB
21740 " -zip open the file as a ZIP Archive\n"
21743 static void usage(int showDetail){
21744 utf8_printf(stderr,
21745 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
21746 "FILENAME is the name of an SQLite database. A new database is created\n"
21747 "if the file does not previously exist.\n", Argv0);
21749 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
21751 raw_printf(stderr, "Use the -help option for additional information\n");
21757 ** Internal check: Verify that the SQLite is uninitialized. Print a
21758 ** error message if it is initialized.
21760 static void verify_uninitialized(void){
21761 if( sqlite3_config(-1)==SQLITE_MISUSE ){
21762 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
21763 " initialization.\n");
21768 ** Initialize the state information in data
21770 static void main_init(ShellState *data) {
21771 memset(data, 0, sizeof(*data));
21772 data->normalMode = data->cMode = data->mode = MODE_List;
21773 data->autoExplain = 1;
21774 memcpy(data->colSeparator,SEP_Column, 2);
21775 memcpy(data->rowSeparator,SEP_Row, 2);
21776 data->showHeader = 0;
21777 data->shellFlgs = SHFLG_Lookaside;
21778 verify_uninitialized();
21779 sqlite3_config(SQLITE_CONFIG_URI, 1);
21780 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
21781 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
21782 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
21783 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
21787 ** Output text to the console in a font that attracts extra attention.
21790 static void printBold(const char *zText){
21791 #if !SQLITE_OS_WINRT
21792 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
21793 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
21794 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
21795 SetConsoleTextAttribute(out,
21796 FOREGROUND_RED|FOREGROUND_INTENSITY
21799 printf("%s", zText);
21800 #if !SQLITE_OS_WINRT
21801 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
21805 static void printBold(const char *zText){
21806 printf("\033[1m%s\033[0m", zText);
21811 ** Get the argument to an --option. Throw an error and die if no argument
21814 static char *cmdline_option_value(int argc, char **argv, int i){
21816 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
21817 argv[0], argv[argc-1]);
21823 #ifndef SQLITE_SHELL_IS_UTF8
21824 # if (defined(_WIN32) || defined(WIN32)) \
21825 && (defined(_MSC_VER) || (defined(UNICODE) && defined(__GNUC__)))
21826 # define SQLITE_SHELL_IS_UTF8 (0)
21828 # define SQLITE_SHELL_IS_UTF8 (1)
21832 #if SQLITE_SHELL_IS_UTF8
21833 int SQLITE_CDECL main(int argc, char **argv){
21835 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
21840 const char *zInitFile = 0;
21843 int warnInmemoryDb = 0;
21847 const char *zVfs = 0; /* Value of -vfs command-line option */
21848 #if !SQLITE_SHELL_IS_UTF8
21849 char **argvToFree = 0;
21850 int argcToFree = 0;
21853 setBinaryMode(stdin, 0);
21854 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
21855 stdin_is_interactive = isatty(0);
21856 stdout_is_console = isatty(1);
21858 #ifdef SQLITE_DEBUG
21859 registerOomSimulator();
21862 #if !defined(_WIN32_WCE)
21863 if( getenv("SQLITE_DEBUG_BREAK") ){
21864 if( isatty(0) && isatty(2) ){
21866 "attach debugger to process %d and press any key to continue.\n",
21870 #if defined(_WIN32) || defined(WIN32)
21871 #if SQLITE_OS_WINRT
21876 #elif defined(SIGTRAP)
21883 #if USE_SYSTEM_SQLITE+0!=1
21884 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
21885 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
21886 sqlite3_sourceid(), SQLITE_SOURCE_ID);
21892 /* On Windows, we must translate command-line arguments into UTF-8.
21893 ** The SQLite memory allocator subsystem has to be enabled in order to
21894 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
21895 ** subsequent sqlite3_config() calls will work. So copy all results into
21896 ** memory that does not come from the SQLite memory allocator.
21898 #if !SQLITE_SHELL_IS_UTF8
21899 sqlite3_initialize();
21900 argvToFree = malloc(sizeof(argv[0])*argc*2);
21902 argv = argvToFree + argc;
21903 if( argv==0 ) shell_out_of_memory();
21904 for(i=0; i<argc; i++){
21905 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
21907 if( z==0 ) shell_out_of_memory();
21908 n = (int)strlen(z);
21909 argv[i] = malloc( n+1 );
21910 if( argv[i]==0 ) shell_out_of_memory();
21911 memcpy(argv[i], z, n+1);
21912 argvToFree[i] = argv[i];
21915 sqlite3_shutdown();
21918 assert( argc>=1 && argv && argv[0] );
21921 /* Make sure we have a valid signal handler early, before anything
21925 signal(SIGINT, interrupt_handler);
21926 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
21927 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
21930 #ifdef SQLITE_SHELL_DBNAME_PROC
21932 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
21933 ** of a C-function that will provide the name of the database file. Use
21934 ** this compile-time option to embed this shell program in larger
21935 ** applications. */
21936 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
21937 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
21938 warnInmemoryDb = 0;
21942 /* Do an initial pass through the command-line argument to locate
21943 ** the name of the database file, the name of the initialization file,
21944 ** the size of the alternative malloc heap,
21945 ** and the first command to execute.
21947 verify_uninitialized();
21948 for(i=1; i<argc; i++){
21952 if( data.zDbFilename==0 ){
21953 data.zDbFilename = z;
21955 /* Excesss arguments are interpreted as SQL (or dot-commands) and
21956 ** mean that nothing is read from stdin */
21959 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
21960 if( azCmd==0 ) shell_out_of_memory();
21964 if( z[1]=='-' ) z++;
21965 if( strcmp(z,"-separator")==0
21966 || strcmp(z,"-nullvalue")==0
21967 || strcmp(z,"-newline")==0
21968 || strcmp(z,"-cmd")==0
21970 (void)cmdline_option_value(argc, argv, ++i);
21971 }else if( strcmp(z,"-init")==0 ){
21972 zInitFile = cmdline_option_value(argc, argv, ++i);
21973 }else if( strcmp(z,"-batch")==0 ){
21974 /* Need to check for batch mode here to so we can avoid printing
21975 ** informational messages (like from process_sqliterc) before
21976 ** we do the actual processing of arguments later in a second pass.
21978 stdin_is_interactive = 0;
21979 }else if( strcmp(z,"-heap")==0 ){
21980 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
21982 sqlite3_int64 szHeap;
21984 zSize = cmdline_option_value(argc, argv, ++i);
21985 szHeap = integerValue(zSize);
21986 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
21987 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
21989 (void)cmdline_option_value(argc, argv, ++i);
21991 }else if( strcmp(z,"-pagecache")==0 ){
21992 sqlite3_int64 n, sz;
21993 sz = integerValue(cmdline_option_value(argc,argv,++i));
21994 if( sz>70000 ) sz = 70000;
21996 n = integerValue(cmdline_option_value(argc,argv,++i));
21997 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21998 n = 0xffffffffffffLL/sz;
22000 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
22001 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
22002 data.shellFlgs |= SHFLG_Pagecache;
22003 }else if( strcmp(z,"-lookaside")==0 ){
22005 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
22007 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
22009 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
22010 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
22011 #ifdef SQLITE_ENABLE_VFSTRACE
22012 }else if( strcmp(z,"-vfstrace")==0 ){
22013 extern int vfstrace_register(
22014 const char *zTraceName,
22015 const char *zOldVfsName,
22016 int (*xOut)(const char*,void*),
22020 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
22022 #ifdef SQLITE_ENABLE_MULTIPLEX
22023 }else if( strcmp(z,"-multiplex")==0 ){
22024 extern int sqlite3_multiple_initialize(const char*,int);
22025 sqlite3_multiplex_initialize(0, 1);
22027 }else if( strcmp(z,"-mmap")==0 ){
22028 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22029 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
22030 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22031 }else if( strcmp(z,"-sorterref")==0 ){
22032 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
22033 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
22035 }else if( strcmp(z,"-vfs")==0 ){
22036 zVfs = cmdline_option_value(argc, argv, ++i);
22037 #ifdef SQLITE_HAVE_ZLIB
22038 }else if( strcmp(z,"-zip")==0 ){
22039 data.openMode = SHELL_OPEN_ZIPFILE;
22041 }else if( strcmp(z,"-append")==0 ){
22042 data.openMode = SHELL_OPEN_APPENDVFS;
22043 #ifndef SQLITE_OMIT_DESERIALIZE
22044 }else if( strcmp(z,"-deserialize")==0 ){
22045 data.openMode = SHELL_OPEN_DESERIALIZE;
22046 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22047 data.szMax = integerValue(argv[++i]);
22049 }else if( strcmp(z,"-readonly")==0 ){
22050 data.openMode = SHELL_OPEN_READONLY;
22051 }else if( strcmp(z,"-nofollow")==0 ){
22052 data.openFlags = SQLITE_OPEN_NOFOLLOW;
22053 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22054 }else if( strncmp(z, "-A",2)==0 ){
22055 /* All remaining command-line arguments are passed to the ".archive"
22056 ** command, so ignore them */
22059 }else if( strcmp(z, "-memtrace")==0 ){
22060 sqlite3MemTraceActivate(stderr);
22061 }else if( strcmp(z,"-bail")==0 ){
22065 verify_uninitialized();
22068 #ifdef SQLITE_SHELL_INIT_PROC
22070 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
22071 ** of a C-function that will perform initialization actions on SQLite that
22072 ** occur just before or after sqlite3_initialize(). Use this compile-time
22073 ** option to embed this shell program in larger applications. */
22074 extern void SQLITE_SHELL_INIT_PROC(void);
22075 SQLITE_SHELL_INIT_PROC();
22078 /* All the sqlite3_config() calls have now been made. So it is safe
22079 ** to call sqlite3_initialize() and process any command line -vfs option. */
22080 sqlite3_initialize();
22084 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
22086 sqlite3_vfs_register(pVfs, 1);
22088 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
22093 if( data.zDbFilename==0 ){
22094 #ifndef SQLITE_OMIT_MEMORYDB
22095 data.zDbFilename = ":memory:";
22096 warnInmemoryDb = argc==1;
22098 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
22103 sqlite3_appendvfs_init(0,0,0);
22105 /* Go ahead and open the database file if it already exists. If the
22106 ** file does not exist, delay opening it. This prevents empty database
22107 ** files from being created if a user mistypes the database name argument
22108 ** to the sqlite command-line tool.
22110 if( access(data.zDbFilename, 0)==0 ){
22114 /* Process the initialization file if there is one. If no -init option
22115 ** is given on the command line, look for a file named ~/.sqliterc and
22116 ** try to process it.
22118 process_sqliterc(&data,zInitFile);
22120 /* Make a second pass through the command-line argument and set
22121 ** options. This second pass is delayed until after the initialization
22122 ** file is processed so that the command-line arguments will override
22123 ** settings in the initialization file.
22125 for(i=1; i<argc; i++){
22127 if( z[0]!='-' ) continue;
22128 if( z[1]=='-' ){ z++; }
22129 if( strcmp(z,"-init")==0 ){
22131 }else if( strcmp(z,"-html")==0 ){
22132 data.mode = MODE_Html;
22133 }else if( strcmp(z,"-list")==0 ){
22134 data.mode = MODE_List;
22135 }else if( strcmp(z,"-quote")==0 ){
22136 data.mode = MODE_Quote;
22137 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
22138 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22139 }else if( strcmp(z,"-line")==0 ){
22140 data.mode = MODE_Line;
22141 }else if( strcmp(z,"-column")==0 ){
22142 data.mode = MODE_Column;
22143 }else if( strcmp(z,"-json")==0 ){
22144 data.mode = MODE_Json;
22145 }else if( strcmp(z,"-markdown")==0 ){
22146 data.mode = MODE_Markdown;
22147 }else if( strcmp(z,"-table")==0 ){
22148 data.mode = MODE_Table;
22149 }else if( strcmp(z,"-box")==0 ){
22150 data.mode = MODE_Box;
22151 }else if( strcmp(z,"-csv")==0 ){
22152 data.mode = MODE_Csv;
22153 memcpy(data.colSeparator,",",2);
22154 #ifdef SQLITE_HAVE_ZLIB
22155 }else if( strcmp(z,"-zip")==0 ){
22156 data.openMode = SHELL_OPEN_ZIPFILE;
22158 }else if( strcmp(z,"-append")==0 ){
22159 data.openMode = SHELL_OPEN_APPENDVFS;
22160 #ifndef SQLITE_OMIT_DESERIALIZE
22161 }else if( strcmp(z,"-deserialize")==0 ){
22162 data.openMode = SHELL_OPEN_DESERIALIZE;
22163 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
22164 data.szMax = integerValue(argv[++i]);
22166 }else if( strcmp(z,"-readonly")==0 ){
22167 data.openMode = SHELL_OPEN_READONLY;
22168 }else if( strcmp(z,"-nofollow")==0 ){
22169 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
22170 }else if( strcmp(z,"-ascii")==0 ){
22171 data.mode = MODE_Ascii;
22172 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
22173 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
22174 }else if( strcmp(z,"-tabs")==0 ){
22175 data.mode = MODE_List;
22176 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
22177 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
22178 }else if( strcmp(z,"-separator")==0 ){
22179 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
22180 "%s",cmdline_option_value(argc,argv,++i));
22181 }else if( strcmp(z,"-newline")==0 ){
22182 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
22183 "%s",cmdline_option_value(argc,argv,++i));
22184 }else if( strcmp(z,"-nullvalue")==0 ){
22185 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
22186 "%s",cmdline_option_value(argc,argv,++i));
22187 }else if( strcmp(z,"-header")==0 ){
22188 data.showHeader = 1;
22189 }else if( strcmp(z,"-noheader")==0 ){
22190 data.showHeader = 0;
22191 }else if( strcmp(z,"-echo")==0 ){
22192 ShellSetFlag(&data, SHFLG_Echo);
22193 }else if( strcmp(z,"-eqp")==0 ){
22194 data.autoEQP = AUTOEQP_on;
22195 }else if( strcmp(z,"-eqpfull")==0 ){
22196 data.autoEQP = AUTOEQP_full;
22197 }else if( strcmp(z,"-stats")==0 ){
22199 }else if( strcmp(z,"-scanstats")==0 ){
22200 data.scanstatsOn = 1;
22201 }else if( strcmp(z,"-backslash")==0 ){
22202 /* Undocumented command-line option: -backslash
22203 ** Causes C-style backslash escapes to be evaluated in SQL statements
22204 ** prior to sending the SQL into SQLite. Useful for injecting
22205 ** crazy bytes in the middle of SQL statements for testing and debugging.
22207 ShellSetFlag(&data, SHFLG_Backslash);
22208 }else if( strcmp(z,"-bail")==0 ){
22209 /* No-op. The bail_on_error flag should already be set. */
22210 }else if( strcmp(z,"-version")==0 ){
22211 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
22213 }else if( strcmp(z,"-interactive")==0 ){
22214 stdin_is_interactive = 1;
22215 }else if( strcmp(z,"-batch")==0 ){
22216 stdin_is_interactive = 0;
22217 }else if( strcmp(z,"-heap")==0 ){
22219 }else if( strcmp(z,"-pagecache")==0 ){
22221 }else if( strcmp(z,"-lookaside")==0 ){
22223 }else if( strcmp(z,"-mmap")==0 ){
22225 }else if( strcmp(z,"-memtrace")==0 ){
22227 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
22228 }else if( strcmp(z,"-sorterref")==0 ){
22231 }else if( strcmp(z,"-vfs")==0 ){
22233 #ifdef SQLITE_ENABLE_VFSTRACE
22234 }else if( strcmp(z,"-vfstrace")==0 ){
22237 #ifdef SQLITE_ENABLE_MULTIPLEX
22238 }else if( strcmp(z,"-multiplex")==0 ){
22241 }else if( strcmp(z,"-help")==0 ){
22243 }else if( strcmp(z,"-cmd")==0 ){
22244 /* Run commands that follow -cmd first and separately from commands
22245 ** that simply appear on the command-line. This seems goofy. It would
22246 ** be better if all commands ran in the order that they appear. But
22247 ** we retain the goofy behavior for historical compatibility. */
22248 if( i==argc-1 ) break;
22249 z = cmdline_option_value(argc,argv,++i);
22251 rc = do_meta_command(z, &data);
22252 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
22255 rc = shell_exec(&data, z, &zErrMsg);
22257 utf8_printf(stderr,"Error: %s\n", zErrMsg);
22258 if( bail_on_error ) return rc!=0 ? rc : 1;
22260 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
22261 if( bail_on_error ) return rc;
22264 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
22265 }else if( strncmp(z, "-A", 2)==0 ){
22267 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
22268 " with \"%s\"\n", z);
22271 open_db(&data, OPEN_DB_ZIPFILE);
22274 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
22276 arDotCommand(&data, 1, argv+i, argc-i);
22282 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
22283 raw_printf(stderr,"Use -help for a list of options.\n");
22286 data.cMode = data.mode;
22290 /* Run all arguments that do not begin with '-' as if they were separate
22291 ** command-line inputs, except for the argToSkip argument which contains
22292 ** the database filename.
22294 for(i=0; i<nCmd; i++){
22295 if( azCmd[i][0]=='.' ){
22296 rc = do_meta_command(azCmd[i], &data);
22299 return rc==2 ? 0 : rc;
22303 rc = shell_exec(&data, azCmd[i], &zErrMsg);
22304 if( zErrMsg || rc ){
22306 utf8_printf(stderr,"Error: %s\n", zErrMsg);
22308 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
22310 sqlite3_free(zErrMsg);
22312 return rc!=0 ? rc : 1;
22317 /* Run commands received from standard input
22319 if( stdin_is_interactive ){
22324 "SQLite version %s %.19s\n" /*extra-version-info*/
22325 "Enter \".help\" for usage hints.\n",
22326 sqlite3_libversion(), sqlite3_sourceid()
22328 if( warnInmemoryDb ){
22329 printf("Connected to a ");
22330 printBold("transient in-memory database");
22331 printf(".\nUse \".open FILENAME\" to reopen on a "
22332 "persistent database.\n");
22334 zHistory = getenv("SQLITE_HISTORY");
22336 zHistory = strdup(zHistory);
22337 }else if( (zHome = find_home_dir(0))!=0 ){
22338 nHistory = strlen30(zHome) + 20;
22339 if( (zHistory = malloc(nHistory))!=0 ){
22340 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
22343 if( zHistory ){ shell_read_history(zHistory); }
22344 #if HAVE_READLINE || HAVE_EDITLINE
22345 rl_attempted_completion_function = readline_completion;
22346 #elif HAVE_LINENOISE
22347 linenoiseSetCompletionCallback(linenoise_completion);
22350 rc = process_input(&data);
22352 shell_stifle_history(2000);
22353 shell_write_history(zHistory);
22358 rc = process_input(&data);
22362 set_table_name(&data, 0);
22364 session_close_all(&data);
22367 sqlite3_free(data.zFreeOnClose);
22369 output_reset(&data);
22370 data.doXdgOpen = 0;
22371 clearTempFile(&data);
22372 #if !SQLITE_SHELL_IS_UTF8
22373 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
22376 free(data.colWidth);
22377 /* Clear the global data structure so that valgrind will detect memory
22379 memset(&data, 0, sizeof(data));