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->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
248 clockVfs->xCurrentTimeInt64(clockVfs, &t);
251 clockVfs->xCurrentTime(clockVfs, &r);
252 t = (sqlite3_int64)(r*86400000.0);
257 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
258 #include <sys/time.h>
259 #include <sys/resource.h>
261 /* VxWorks does not support getrusage() as far as we can determine */
262 #if defined(_WRS_KERNEL) || defined(__RTP__)
264 struct timeval ru_utime; /* user CPU time used */
265 struct timeval ru_stime; /* system CPU time used */
267 #define getrusage(A,B) memset(B,0,sizeof(*B))
270 /* Saved resource information for the beginning of an operation */
271 static struct rusage sBegin; /* CPU time at start */
272 static sqlite3_int64 iBegin; /* Wall-clock time at start */
275 ** Begin timing an operation
277 static void beginTimer(void){
279 getrusage(RUSAGE_SELF, &sBegin);
280 iBegin = timeOfDay();
284 /* Return the difference of two time_structs in seconds */
285 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
286 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
287 (double)(pEnd->tv_sec - pStart->tv_sec);
291 ** Print the timing results.
293 static void endTimer(void){
295 sqlite3_int64 iEnd = timeOfDay();
297 getrusage(RUSAGE_SELF, &sEnd);
298 printf("Run Time: real %.3f user %f sys %f\n",
299 (iEnd - iBegin)*0.001,
300 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
301 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
305 #define BEGIN_TIMER beginTimer()
306 #define END_TIMER endTimer()
309 #elif (defined(_WIN32) || defined(WIN32))
311 /* Saved resource information for the beginning of an operation */
312 static HANDLE hProcess;
313 static FILETIME ftKernelBegin;
314 static FILETIME ftUserBegin;
315 static sqlite3_int64 ftWallBegin;
316 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
317 LPFILETIME, LPFILETIME);
318 static GETPROCTIMES getProcessTimesAddr = NULL;
321 ** Check to see if we have timer support. Return 1 if necessary
322 ** support found (or found previously).
324 static int hasTimer(void){
325 if( getProcessTimesAddr ){
329 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
330 ** versions. See if the version we are running on has it, and if it
331 ** does, save off a pointer to it and the current process handle.
333 hProcess = GetCurrentProcess();
335 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
336 if( NULL != hinstLib ){
337 getProcessTimesAddr =
338 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
339 if( NULL != getProcessTimesAddr ){
342 FreeLibrary(hinstLib);
351 ** Begin timing an operation
353 static void beginTimer(void){
354 if( enableTimer && getProcessTimesAddr ){
355 FILETIME ftCreation, ftExit;
356 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
357 &ftKernelBegin,&ftUserBegin);
358 ftWallBegin = timeOfDay();
362 /* Return the difference of two FILETIME structs in seconds */
363 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
364 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
365 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
366 return (double) ((i64End - i64Start) / 10000000.0);
370 ** Print the timing results.
372 static void endTimer(void){
373 if( enableTimer && getProcessTimesAddr){
374 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
375 sqlite3_int64 ftWallEnd = timeOfDay();
376 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
377 printf("Run Time: real %.3f user %f sys %f\n",
378 (ftWallEnd - ftWallBegin)*0.001,
379 timeDiff(&ftUserBegin, &ftUserEnd),
380 timeDiff(&ftKernelBegin, &ftKernelEnd));
384 #define BEGIN_TIMER beginTimer()
385 #define END_TIMER endTimer()
386 #define HAS_TIMER hasTimer()
395 ** Used to prevent warnings about unused parameters
397 #define UNUSED_PARAMETER(x) (void)(x)
400 ** Number of elements in an array
402 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
405 ** If the following flag is set, then command execution stops
406 ** at an error if we are not interactive.
408 static int bail_on_error = 0;
411 ** Threat stdin as an interactive input if the following variable
412 ** is true. Otherwise, assume stdin is connected to a file or pipe.
414 static int stdin_is_interactive = 1;
417 ** On Windows systems we have to know if standard output is a console
418 ** in order to translate UTF-8 into MBCS. The following variable is
419 ** true if translation is required.
421 static int stdout_is_console = 1;
424 ** The following is the open SQLite database. We make a pointer
425 ** to this database a static variable so that it can be accessed
426 ** by the SIGINT handler to interrupt database processing.
428 static sqlite3 *globalDb = 0;
431 ** True if an interrupt (Control-C) has been received.
433 static volatile int seenInterrupt = 0;
437 ** Out-of-memory simulator variables
439 static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
440 static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
441 static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
442 #endif /* SQLITE_DEBUG */
445 ** This is the name of our program. It is set in main(), used
446 ** in a number of other places, mostly for error messages.
451 ** Prompt strings. Initialized in main. Settable with
452 ** .prompt main continue
454 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
455 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
458 ** Render output like fprintf(). Except, if the output is going to the
459 ** console and if this is running on a Windows machine, translate the
460 ** output from UTF-8 into MBCS.
462 #if defined(_WIN32) || defined(WIN32)
463 void utf8_printf(FILE *out, const char *zFormat, ...){
465 va_start(ap, zFormat);
466 if( stdout_is_console && (out==stdout || out==stderr) ){
467 char *z1 = sqlite3_vmprintf(zFormat, ap);
468 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
473 vfprintf(out, zFormat, ap);
477 #elif !defined(utf8_printf)
478 # define utf8_printf fprintf
482 ** Render output like fprintf(). This should not be used on anything that
483 ** includes string formatting (e.g. "%s").
485 #if !defined(raw_printf)
486 # define raw_printf fprintf
489 /* Indicate out-of-memory and exit. */
490 static void shell_out_of_memory(void){
491 raw_printf(stderr,"Error: out of memory\n");
496 /* This routine is called when a simulated OOM occurs. It is broken
497 ** out as a separate routine to make it easy to set a breakpoint on
500 void shellOomFault(void){
507 #endif /* SQLITE_DEBUG */
510 /* This routine is a replacement malloc() that is used to simulate
511 ** Out-Of-Memory (OOM) errors for testing purposes.
513 static void *oomMalloc(int nByte){
522 return defaultMalloc(nByte);
524 #endif /* SQLITE_DEBUG */
527 /* Register the OOM simulator. This must occur before any memory
529 static void registerOomSimulator(void){
530 sqlite3_mem_methods mem;
531 sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
532 defaultMalloc = mem.xMalloc;
533 mem.xMalloc = oomMalloc;
534 sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
539 ** Write I/O traces to the following stream.
541 #ifdef SQLITE_ENABLE_IOTRACE
542 static FILE *iotrace = 0;
546 ** This routine works like printf in that its first argument is a
547 ** format string and subsequent arguments are values to be substituted
548 ** in place of % fields. The result of formatting this string
549 ** is written to iotrace.
551 #ifdef SQLITE_ENABLE_IOTRACE
552 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
555 if( iotrace==0 ) return;
556 va_start(ap, zFormat);
557 z = sqlite3_vmprintf(zFormat, ap);
559 utf8_printf(iotrace, "%s", z);
565 ** Output string zUtf to stream pOut as w characters. If w is negative,
566 ** then right-justify the text. W is the width in UTF-8 characters, not
567 ** in bytes. This is different from the %*.*s specification in printf
568 ** since with %*.*s the width is measured in bytes, not characters.
570 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
573 int aw = w<0 ? -w : w;
574 for(i=n=0; zUtf[i]; i++){
575 if( (zUtf[i]&0xc0)!=0x80 ){
578 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
584 utf8_printf(pOut, "%.*s", i, zUtf);
586 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
588 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
594 ** Determines if a string is a number of not.
596 static int isNumber(const char *z, int *realnum){
597 if( *z=='-' || *z=='+' ) z++;
602 if( realnum ) *realnum = 0;
603 while( IsDigit(*z) ){ z++; }
606 if( !IsDigit(*z) ) return 0;
607 while( IsDigit(*z) ){ z++; }
608 if( realnum ) *realnum = 1;
610 if( *z=='e' || *z=='E' ){
612 if( *z=='+' || *z=='-' ) z++;
613 if( !IsDigit(*z) ) return 0;
614 while( IsDigit(*z) ){ z++; }
615 if( realnum ) *realnum = 1;
621 ** Compute a string length that is limited to what can be stored in
622 ** lower 30 bits of a 32-bit signed integer.
624 static int strlen30(const char *z){
626 while( *z2 ){ z2++; }
627 return 0x3fffffff & (int)(z2 - z);
631 ** Return the length of a string in characters. Multibyte UTF8 characters
632 ** count as a single character.
634 static int strlenChar(const char *z){
637 if( (0xc0&*(z++))!=0x80 ) n++;
643 ** Return true if zFile does not exist or if it is not an ordinary file.
646 # define notNormalFile(X) 0
648 static int notNormalFile(const char *zFile){
651 memset(&x, 0, sizeof(x));
652 rc = stat(zFile, &x);
653 return rc || !S_ISREG(x.st_mode);
658 ** This routine reads a line of text from FILE in, stores
659 ** the text in memory obtained from malloc() and returns a pointer
660 ** to the text. NULL is returned at end of file, or if malloc()
663 ** If zLine is not NULL then it is a malloced buffer returned from
664 ** a previous call to this routine that may be reused.
666 static char *local_getline(char *zLine, FILE *in){
667 int nLine = zLine==0 ? 0 : 100;
672 nLine = nLine*2 + 100;
673 zLine = realloc(zLine, nLine);
674 if( zLine==0 ) shell_out_of_memory();
676 if( fgets(&zLine[n], nLine - n, in)==0 ){
684 while( zLine[n] ) n++;
685 if( n>0 && zLine[n-1]=='\n' ){
687 if( n>0 && zLine[n-1]=='\r' ) n--;
692 #if defined(_WIN32) || defined(WIN32)
693 /* For interactive input on Windows systems, translate the
694 ** multi-byte characterset characters into UTF-8. */
695 if( stdin_is_interactive && in==stdin ){
696 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
698 int nTrans = strlen30(zTrans)+1;
700 zLine = realloc(zLine, nTrans);
701 if( zLine==0 ) shell_out_of_memory();
703 memcpy(zLine, zTrans, nTrans);
704 sqlite3_free(zTrans);
707 #endif /* defined(_WIN32) || defined(WIN32) */
712 ** Retrieve a single line of input text.
714 ** If in==0 then read from standard input and prompt before each line.
715 ** If isContinuation is true, then a continuation prompt is appropriate.
716 ** If isContinuation is zero, then the main prompt should be used.
718 ** If zPrior is not NULL then it is a buffer from a prior call to this
719 ** routine that can be reused.
721 ** The result is stored in space obtained from malloc() and must either
722 ** be freed by the caller or else passed back into this routine via the
723 ** zPrior argument for reuse.
725 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
729 zResult = local_getline(zPrior, in);
731 zPrompt = isContinuation ? continuePrompt : mainPrompt;
732 #if SHELL_USE_LOCAL_GETLINE
733 printf("%s", zPrompt);
735 zResult = local_getline(zPrior, stdin);
738 zResult = shell_readline(zPrompt);
739 if( zResult && *zResult ) shell_add_history(zResult);
747 ** Return the value of a hexadecimal digit. Return -1 if the input
748 ** is not a hex digit.
750 static int hexDigitValue(char c){
751 if( c>='0' && c<='9' ) return c - '0';
752 if( c>='a' && c<='f' ) return c - 'a' + 10;
753 if( c>='A' && c<='F' ) return c - 'A' + 10;
758 ** Interpret zArg as an integer value, possibly with suffixes.
760 static sqlite3_int64 integerValue(const char *zArg){
762 static const struct { char *zSuffix; int iMult; } aMult[] = {
764 { "MiB", 1024*1024 },
765 { "GiB", 1024*1024*1024 },
768 { "GB", 1000000000 },
778 }else if( zArg[0]=='+' ){
781 if( zArg[0]=='0' && zArg[1]=='x' ){
784 while( (x = hexDigitValue(zArg[0]))>=0 ){
789 while( IsDigit(zArg[0]) ){
790 v = v*10 + zArg[0] - '0';
794 for(i=0; i<ArraySize(aMult); i++){
795 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
800 return isNeg? -v : v;
804 ** A variable length string to which one can append text.
806 typedef struct ShellText ShellText;
814 ** Initialize and destroy a ShellText object
816 static void initText(ShellText *p){
817 memset(p, 0, sizeof(*p));
819 static void freeText(ShellText *p){
824 /* zIn is either a pointer to a NULL-terminated string in memory obtained
825 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
826 ** added to zIn, and the result returned in memory obtained from malloc().
827 ** zIn, if it was not NULL, is freed.
829 ** If the third argument, quote, is not '\0', then it is used as a
830 ** quote character for zAppend.
832 static void appendText(ShellText *p, char const *zAppend, char quote){
835 int nAppend = strlen30(zAppend);
837 len = nAppend+p->n+1;
840 for(i=0; i<nAppend; i++){
841 if( zAppend[i]==quote ) len++;
845 if( p->n+len>=p->nAlloc ){
846 p->nAlloc = p->nAlloc*2 + len + 20;
847 p->z = realloc(p->z, p->nAlloc);
848 if( p->z==0 ) shell_out_of_memory();
852 char *zCsr = p->z+p->n;
854 for(i=0; i<nAppend; i++){
855 *zCsr++ = zAppend[i];
856 if( zAppend[i]==quote ) *zCsr++ = quote;
859 p->n = (int)(zCsr - p->z);
862 memcpy(p->z+p->n, zAppend, nAppend);
869 ** Attempt to determine if identifier zName needs to be quoted, either
870 ** because it contains non-alphanumeric characters, or because it is an
871 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
872 ** that quoting is required.
874 ** Return '"' if quoting is required. Return 0 if no quoting is required.
876 static char quoteChar(const char *zName){
878 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
879 for(i=0; zName[i]; i++){
880 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
882 return sqlite3_keyword_check(zName, i) ? '"' : 0;
886 ** Construct a fake object name and column list to describe the structure
887 ** of the view, virtual table, or table valued function zSchema.zName.
889 static char *shellFakeSchema(
890 sqlite3 *db, /* The database connection containing the vtab */
891 const char *zSchema, /* Schema of the database holding the vtab */
892 const char *zName /* The name of the virtual table */
894 sqlite3_stmt *pStmt = 0;
901 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
902 zSchema ? zSchema : "main", zName);
903 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
907 cQuote = quoteChar(zSchema);
908 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
909 appendText(&s, zSchema, cQuote);
910 appendText(&s, ".", 0);
912 cQuote = quoteChar(zName);
913 appendText(&s, zName, cQuote);
914 while( sqlite3_step(pStmt)==SQLITE_ROW ){
915 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
917 appendText(&s, zDiv, 0);
919 cQuote = quoteChar(zCol);
920 appendText(&s, zCol, cQuote);
922 appendText(&s, ")", 0);
923 sqlite3_finalize(pStmt);
932 ** SQL function: shell_module_schema(X)
934 ** Return a fake schema for the table-valued function or eponymous virtual
937 static void shellModuleSchema(
938 sqlite3_context *pCtx,
940 sqlite3_value **apVal
942 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
943 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
944 UNUSED_PARAMETER(nVal);
946 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
953 ** SQL function: shell_add_schema(S,X)
955 ** Add the schema name X to the CREATE statement in S and return the result.
958 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
963 ** CREATE UNIQUE INDEX
966 ** CREATE VIRTUAL TABLE
968 ** This UDF is used by the .schema command to insert the schema name of
969 ** attached databases into the middle of the sqlite_schema.sql field.
971 static void shellAddSchemaName(
972 sqlite3_context *pCtx,
974 sqlite3_value **apVal
976 static const char *aPrefix[] = {
985 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
986 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
987 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
988 sqlite3 *db = sqlite3_context_db_handle(pCtx);
989 UNUSED_PARAMETER(nVal);
990 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
991 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
992 int n = strlen30(aPrefix[i]);
993 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
997 char cQuote = quoteChar(zSchema);
998 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
999 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
1001 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
1005 && aPrefix[i][0]=='V'
1006 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
1009 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
1011 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1016 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1022 sqlite3_result_value(pCtx, apVal[0]);
1026 ** The source code for several run-time loadable extensions is inserted
1027 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1028 ** code, we need to override some macros to make the included program code
1029 ** work here in the middle of this regular program.
1031 #define SQLITE_EXTENSION_INIT1
1032 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1034 #if defined(_WIN32) && defined(_MSC_VER)
1035 /************************* Begin test_windirent.h ******************/
1039 ** The author disclaims copyright to this source code. In place of
1040 ** a legal notice, here is a blessing:
1042 ** May you do good and not evil.
1043 ** May you find forgiveness for yourself and forgive others.
1044 ** May you share freely, never taking more than you give.
1046 *************************************************************************
1047 ** This file contains declarations for most of the opendir() family of
1048 ** POSIX functions on Win32 using the MSVCRT.
1051 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1052 #define SQLITE_WINDIRENT_H
1055 ** We need several data types from the Windows SDK header.
1058 #ifndef WIN32_LEAN_AND_MEAN
1059 #define WIN32_LEAN_AND_MEAN
1062 #include "windows.h"
1065 ** We need several support functions from the SQLite core.
1068 /* #include "sqlite3.h" */
1071 ** We need several things from the ANSI and MSVCRT headers.
1079 #include <sys/types.h>
1080 #include <sys/stat.h>
1083 ** We may need several defines that should have been in "sys/stat.h".
1087 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1091 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1095 #define S_ISLNK(mode) (0)
1099 ** We may need to provide the "mode_t" type.
1102 #ifndef MODE_T_DEFINED
1103 #define MODE_T_DEFINED
1104 typedef unsigned short mode_t;
1108 ** We may need to provide the "ino_t" type.
1111 #ifndef INO_T_DEFINED
1112 #define INO_T_DEFINED
1113 typedef unsigned short ino_t;
1117 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1121 # ifdef FILENAME_MAX
1122 # define NAME_MAX (FILENAME_MAX)
1124 # define NAME_MAX (260)
1129 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1132 #ifndef NULL_INTPTR_T
1133 # define NULL_INTPTR_T ((intptr_t)(0))
1136 #ifndef BAD_INTPTR_T
1137 # define BAD_INTPTR_T ((intptr_t)(-1))
1141 ** We need to provide the necessary structures and related types.
1144 #ifndef DIRENT_DEFINED
1145 #define DIRENT_DEFINED
1146 typedef struct DIRENT DIRENT;
1147 typedef DIRENT *LPDIRENT;
1149 ino_t d_ino; /* Sequence number, do not use. */
1150 unsigned d_attributes; /* Win32 file attributes. */
1151 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1157 typedef struct DIR DIR;
1160 intptr_t d_handle; /* Value returned by "_findfirst". */
1161 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1162 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1167 ** Provide a macro, for use by the implementation, to determine if a
1168 ** particular directory entry should be skipped over when searching for
1169 ** the next directory entry that should be returned by the readdir() or
1170 ** readdir_r() functions.
1174 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1178 ** Provide the function prototype for the POSIX compatiable getenv()
1179 ** function. This function is not thread-safe.
1182 extern const char *windirent_getenv(const char *name);
1185 ** Finally, we can provide the function prototypes for the opendir(),
1186 ** readdir(), readdir_r(), and closedir() POSIX functions.
1189 extern LPDIR opendir(const char *dirname);
1190 extern LPDIRENT readdir(LPDIR dirp);
1191 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1192 extern INT closedir(LPDIR dirp);
1194 #endif /* defined(WIN32) && defined(_MSC_VER) */
1196 /************************* End test_windirent.h ********************/
1197 /************************* Begin test_windirent.c ******************/
1201 ** The author disclaims copyright to this source code. In place of
1202 ** a legal notice, here is a blessing:
1204 ** May you do good and not evil.
1205 ** May you find forgiveness for yourself and forgive others.
1206 ** May you share freely, never taking more than you give.
1208 *************************************************************************
1209 ** This file contains code to implement most of the opendir() family of
1210 ** POSIX functions on Win32 using the MSVCRT.
1213 #if defined(_WIN32) && defined(_MSC_VER)
1214 /* #include "test_windirent.h" */
1217 ** Implementation of the POSIX getenv() function using the Win32 API.
1218 ** This function is not thread-safe.
1220 const char *windirent_getenv(
1223 static char value[32768]; /* Maximum length, per MSDN */
1224 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1225 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1227 memset(value, 0, sizeof(value));
1228 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1229 if( dwRet==0 || dwRet>dwSize ){
1231 ** The function call to GetEnvironmentVariableA() failed -OR-
1232 ** the buffer is not large enough. Either way, return NULL.
1237 ** The function call to GetEnvironmentVariableA() succeeded
1238 ** -AND- the buffer contains the entire value.
1245 ** Implementation of the POSIX opendir() function using the MSVCRT.
1250 struct _finddata_t data;
1251 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1252 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1254 if( dirp==NULL ) return NULL;
1255 memset(dirp, 0, sizeof(DIR));
1257 /* TODO: Remove this if Unix-style root paths are not used. */
1258 if( sqlite3_stricmp(dirname, "/")==0 ){
1259 dirname = windirent_getenv("SystemDrive");
1262 memset(&data, 0, sizeof(struct _finddata_t));
1263 _snprintf(data.name, namesize, "%s\\*", dirname);
1264 dirp->d_handle = _findfirst(data.name, &data);
1266 if( dirp->d_handle==BAD_INTPTR_T ){
1271 /* TODO: Remove this block to allow hidden and/or system files. */
1272 if( is_filtered(data) ){
1275 memset(&data, 0, sizeof(struct _finddata_t));
1276 if( _findnext(dirp->d_handle, &data)==-1 ){
1281 /* TODO: Remove this block to allow hidden and/or system files. */
1282 if( is_filtered(data) ) goto next;
1285 dirp->d_first.d_attributes = data.attrib;
1286 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1287 dirp->d_first.d_name[NAME_MAX] = '\0';
1293 ** Implementation of the POSIX readdir() function using the MSVCRT.
1298 struct _finddata_t data;
1300 if( dirp==NULL ) return NULL;
1302 if( dirp->d_first.d_ino==0 ){
1303 dirp->d_first.d_ino++;
1304 dirp->d_next.d_ino++;
1306 return &dirp->d_first;
1311 memset(&data, 0, sizeof(struct _finddata_t));
1312 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1314 /* TODO: Remove this block to allow hidden and/or system files. */
1315 if( is_filtered(data) ) goto next;
1317 dirp->d_next.d_ino++;
1318 dirp->d_next.d_attributes = data.attrib;
1319 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1320 dirp->d_next.d_name[NAME_MAX] = '\0';
1322 return &dirp->d_next;
1326 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1333 struct _finddata_t data;
1335 if( dirp==NULL ) return EBADF;
1337 if( dirp->d_first.d_ino==0 ){
1338 dirp->d_first.d_ino++;
1339 dirp->d_next.d_ino++;
1341 entry->d_ino = dirp->d_first.d_ino;
1342 entry->d_attributes = dirp->d_first.d_attributes;
1343 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1344 entry->d_name[NAME_MAX] = '\0';
1352 memset(&data, 0, sizeof(struct _finddata_t));
1353 if( _findnext(dirp->d_handle, &data)==-1 ){
1358 /* TODO: Remove this block to allow hidden and/or system files. */
1359 if( is_filtered(data) ) goto next;
1361 entry->d_ino = (ino_t)-1; /* not available */
1362 entry->d_attributes = data.attrib;
1363 strncpy(entry->d_name, data.name, NAME_MAX);
1364 entry->d_name[NAME_MAX] = '\0';
1371 ** Implementation of the POSIX closedir() function using the MSVCRT.
1378 if( dirp==NULL ) return EINVAL;
1380 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1381 result = _findclose(dirp->d_handle);
1388 #endif /* defined(WIN32) && defined(_MSC_VER) */
1390 /************************* End test_windirent.c ********************/
1391 #define dirent DIRENT
1393 /************************* Begin ../ext/misc/shathree.c ******************/
1397 ** The author disclaims copyright to this source code. In place of
1398 ** a legal notice, here is a blessing:
1400 ** May you do good and not evil.
1401 ** May you find forgiveness for yourself and forgive others.
1402 ** May you share freely, never taking more than you give.
1404 ******************************************************************************
1406 ** This SQLite extension implements functions that compute SHA3 hashes.
1407 ** Two SQL functions are implemented:
1410 ** sha3_query(Y,SIZE)
1412 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1415 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1416 ** and returns a hash of their results.
1418 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1419 ** is used. If SIZE is included it must be one of the integers 224, 256,
1420 ** 384, or 512, to determine SHA3 hash variant that is computed.
1422 /* #include "sqlite3ext.h" */
1423 SQLITE_EXTENSION_INIT1
1427 /* typedef sqlite3_uint64 u64; */
1429 /******************************************************************************
1433 ** Macros to determine whether the machine is big or little endian,
1434 ** and whether or not that determination is run-time or compile-time.
1436 ** For best performance, an attempt is made to guess at the byte-order
1437 ** using C-preprocessor macros. If that is unsuccessful, or if
1438 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1441 #ifndef SHA3_BYTEORDER
1442 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1443 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1444 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1446 # define SHA3_BYTEORDER 1234
1447 # elif defined(sparc) || defined(__ppc__)
1448 # define SHA3_BYTEORDER 4321
1450 # define SHA3_BYTEORDER 0
1456 ** State structure for a SHA3 hash in progress
1458 typedef struct SHA3Context SHA3Context;
1459 struct SHA3Context {
1461 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1462 unsigned char x[1600]; /* ... or 1600 bytes */
1464 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1465 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1466 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1470 ** A single step of the Keccak mixing function for a 1600-bit state
1472 static void KeccakF1600Step(SHA3Context *p){
1474 u64 b0, b1, b2, b3, b4;
1475 u64 c0, c1, c2, c3, c4;
1476 u64 d0, d1, d2, d3, d4;
1477 static const u64 RC[] = {
1478 0x0000000000000001ULL, 0x0000000000008082ULL,
1479 0x800000000000808aULL, 0x8000000080008000ULL,
1480 0x000000000000808bULL, 0x0000000080000001ULL,
1481 0x8000000080008081ULL, 0x8000000000008009ULL,
1482 0x000000000000008aULL, 0x0000000000000088ULL,
1483 0x0000000080008009ULL, 0x000000008000000aULL,
1484 0x000000008000808bULL, 0x800000000000008bULL,
1485 0x8000000000008089ULL, 0x8000000000008003ULL,
1486 0x8000000000008002ULL, 0x8000000000000080ULL,
1487 0x000000000000800aULL, 0x800000008000000aULL,
1488 0x8000000080008081ULL, 0x8000000000008080ULL,
1489 0x0000000080000001ULL, 0x8000000080008008ULL
1491 # define a00 (p->u.s[0])
1492 # define a01 (p->u.s[1])
1493 # define a02 (p->u.s[2])
1494 # define a03 (p->u.s[3])
1495 # define a04 (p->u.s[4])
1496 # define a10 (p->u.s[5])
1497 # define a11 (p->u.s[6])
1498 # define a12 (p->u.s[7])
1499 # define a13 (p->u.s[8])
1500 # define a14 (p->u.s[9])
1501 # define a20 (p->u.s[10])
1502 # define a21 (p->u.s[11])
1503 # define a22 (p->u.s[12])
1504 # define a23 (p->u.s[13])
1505 # define a24 (p->u.s[14])
1506 # define a30 (p->u.s[15])
1507 # define a31 (p->u.s[16])
1508 # define a32 (p->u.s[17])
1509 # define a33 (p->u.s[18])
1510 # define a34 (p->u.s[19])
1511 # define a40 (p->u.s[20])
1512 # define a41 (p->u.s[21])
1513 # define a42 (p->u.s[22])
1514 # define a43 (p->u.s[23])
1515 # define a44 (p->u.s[24])
1516 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1518 for(i=0; i<24; i+=4){
1519 c0 = a00^a10^a20^a30^a40;
1520 c1 = a01^a11^a21^a31^a41;
1521 c2 = a02^a12^a22^a32^a42;
1522 c3 = a03^a13^a23^a33^a43;
1523 c4 = a04^a14^a24^a34^a44;
1524 d0 = c4^ROL64(c1, 1);
1525 d1 = c0^ROL64(c2, 1);
1526 d2 = c1^ROL64(c3, 1);
1527 d3 = c2^ROL64(c4, 1);
1528 d4 = c3^ROL64(c0, 1);
1531 b1 = ROL64((a11^d1), 44);
1532 b2 = ROL64((a22^d2), 43);
1533 b3 = ROL64((a33^d3), 21);
1534 b4 = ROL64((a44^d4), 14);
1535 a00 = b0 ^((~b1)& b2 );
1537 a11 = b1 ^((~b2)& b3 );
1538 a22 = b2 ^((~b3)& b4 );
1539 a33 = b3 ^((~b4)& b0 );
1540 a44 = b4 ^((~b0)& b1 );
1542 b2 = ROL64((a20^d0), 3);
1543 b3 = ROL64((a31^d1), 45);
1544 b4 = ROL64((a42^d2), 61);
1545 b0 = ROL64((a03^d3), 28);
1546 b1 = ROL64((a14^d4), 20);
1547 a20 = b0 ^((~b1)& b2 );
1548 a31 = b1 ^((~b2)& b3 );
1549 a42 = b2 ^((~b3)& b4 );
1550 a03 = b3 ^((~b4)& b0 );
1551 a14 = b4 ^((~b0)& b1 );
1553 b4 = ROL64((a40^d0), 18);
1554 b0 = ROL64((a01^d1), 1);
1555 b1 = ROL64((a12^d2), 6);
1556 b2 = ROL64((a23^d3), 25);
1557 b3 = ROL64((a34^d4), 8);
1558 a40 = b0 ^((~b1)& b2 );
1559 a01 = b1 ^((~b2)& b3 );
1560 a12 = b2 ^((~b3)& b4 );
1561 a23 = b3 ^((~b4)& b0 );
1562 a34 = b4 ^((~b0)& b1 );
1564 b1 = ROL64((a10^d0), 36);
1565 b2 = ROL64((a21^d1), 10);
1566 b3 = ROL64((a32^d2), 15);
1567 b4 = ROL64((a43^d3), 56);
1568 b0 = ROL64((a04^d4), 27);
1569 a10 = b0 ^((~b1)& b2 );
1570 a21 = b1 ^((~b2)& b3 );
1571 a32 = b2 ^((~b3)& b4 );
1572 a43 = b3 ^((~b4)& b0 );
1573 a04 = b4 ^((~b0)& b1 );
1575 b3 = ROL64((a30^d0), 41);
1576 b4 = ROL64((a41^d1), 2);
1577 b0 = ROL64((a02^d2), 62);
1578 b1 = ROL64((a13^d3), 55);
1579 b2 = ROL64((a24^d4), 39);
1580 a30 = b0 ^((~b1)& b2 );
1581 a41 = b1 ^((~b2)& b3 );
1582 a02 = b2 ^((~b3)& b4 );
1583 a13 = b3 ^((~b4)& b0 );
1584 a24 = b4 ^((~b0)& b1 );
1586 c0 = a00^a20^a40^a10^a30;
1587 c1 = a11^a31^a01^a21^a41;
1588 c2 = a22^a42^a12^a32^a02;
1589 c3 = a33^a03^a23^a43^a13;
1590 c4 = a44^a14^a34^a04^a24;
1591 d0 = c4^ROL64(c1, 1);
1592 d1 = c0^ROL64(c2, 1);
1593 d2 = c1^ROL64(c3, 1);
1594 d3 = c2^ROL64(c4, 1);
1595 d4 = c3^ROL64(c0, 1);
1598 b1 = ROL64((a31^d1), 44);
1599 b2 = ROL64((a12^d2), 43);
1600 b3 = ROL64((a43^d3), 21);
1601 b4 = ROL64((a24^d4), 14);
1602 a00 = b0 ^((~b1)& b2 );
1604 a31 = b1 ^((~b2)& b3 );
1605 a12 = b2 ^((~b3)& b4 );
1606 a43 = b3 ^((~b4)& b0 );
1607 a24 = b4 ^((~b0)& b1 );
1609 b2 = ROL64((a40^d0), 3);
1610 b3 = ROL64((a21^d1), 45);
1611 b4 = ROL64((a02^d2), 61);
1612 b0 = ROL64((a33^d3), 28);
1613 b1 = ROL64((a14^d4), 20);
1614 a40 = b0 ^((~b1)& b2 );
1615 a21 = b1 ^((~b2)& b3 );
1616 a02 = b2 ^((~b3)& b4 );
1617 a33 = b3 ^((~b4)& b0 );
1618 a14 = b4 ^((~b0)& b1 );
1620 b4 = ROL64((a30^d0), 18);
1621 b0 = ROL64((a11^d1), 1);
1622 b1 = ROL64((a42^d2), 6);
1623 b2 = ROL64((a23^d3), 25);
1624 b3 = ROL64((a04^d4), 8);
1625 a30 = b0 ^((~b1)& b2 );
1626 a11 = b1 ^((~b2)& b3 );
1627 a42 = b2 ^((~b3)& b4 );
1628 a23 = b3 ^((~b4)& b0 );
1629 a04 = b4 ^((~b0)& b1 );
1631 b1 = ROL64((a20^d0), 36);
1632 b2 = ROL64((a01^d1), 10);
1633 b3 = ROL64((a32^d2), 15);
1634 b4 = ROL64((a13^d3), 56);
1635 b0 = ROL64((a44^d4), 27);
1636 a20 = b0 ^((~b1)& b2 );
1637 a01 = b1 ^((~b2)& b3 );
1638 a32 = b2 ^((~b3)& b4 );
1639 a13 = b3 ^((~b4)& b0 );
1640 a44 = b4 ^((~b0)& b1 );
1642 b3 = ROL64((a10^d0), 41);
1643 b4 = ROL64((a41^d1), 2);
1644 b0 = ROL64((a22^d2), 62);
1645 b1 = ROL64((a03^d3), 55);
1646 b2 = ROL64((a34^d4), 39);
1647 a10 = b0 ^((~b1)& b2 );
1648 a41 = b1 ^((~b2)& b3 );
1649 a22 = b2 ^((~b3)& b4 );
1650 a03 = b3 ^((~b4)& b0 );
1651 a34 = b4 ^((~b0)& b1 );
1653 c0 = a00^a40^a30^a20^a10;
1654 c1 = a31^a21^a11^a01^a41;
1655 c2 = a12^a02^a42^a32^a22;
1656 c3 = a43^a33^a23^a13^a03;
1657 c4 = a24^a14^a04^a44^a34;
1658 d0 = c4^ROL64(c1, 1);
1659 d1 = c0^ROL64(c2, 1);
1660 d2 = c1^ROL64(c3, 1);
1661 d3 = c2^ROL64(c4, 1);
1662 d4 = c3^ROL64(c0, 1);
1665 b1 = ROL64((a21^d1), 44);
1666 b2 = ROL64((a42^d2), 43);
1667 b3 = ROL64((a13^d3), 21);
1668 b4 = ROL64((a34^d4), 14);
1669 a00 = b0 ^((~b1)& b2 );
1671 a21 = b1 ^((~b2)& b3 );
1672 a42 = b2 ^((~b3)& b4 );
1673 a13 = b3 ^((~b4)& b0 );
1674 a34 = b4 ^((~b0)& b1 );
1676 b2 = ROL64((a30^d0), 3);
1677 b3 = ROL64((a01^d1), 45);
1678 b4 = ROL64((a22^d2), 61);
1679 b0 = ROL64((a43^d3), 28);
1680 b1 = ROL64((a14^d4), 20);
1681 a30 = b0 ^((~b1)& b2 );
1682 a01 = b1 ^((~b2)& b3 );
1683 a22 = b2 ^((~b3)& b4 );
1684 a43 = b3 ^((~b4)& b0 );
1685 a14 = b4 ^((~b0)& b1 );
1687 b4 = ROL64((a10^d0), 18);
1688 b0 = ROL64((a31^d1), 1);
1689 b1 = ROL64((a02^d2), 6);
1690 b2 = ROL64((a23^d3), 25);
1691 b3 = ROL64((a44^d4), 8);
1692 a10 = b0 ^((~b1)& b2 );
1693 a31 = b1 ^((~b2)& b3 );
1694 a02 = b2 ^((~b3)& b4 );
1695 a23 = b3 ^((~b4)& b0 );
1696 a44 = b4 ^((~b0)& b1 );
1698 b1 = ROL64((a40^d0), 36);
1699 b2 = ROL64((a11^d1), 10);
1700 b3 = ROL64((a32^d2), 15);
1701 b4 = ROL64((a03^d3), 56);
1702 b0 = ROL64((a24^d4), 27);
1703 a40 = b0 ^((~b1)& b2 );
1704 a11 = b1 ^((~b2)& b3 );
1705 a32 = b2 ^((~b3)& b4 );
1706 a03 = b3 ^((~b4)& b0 );
1707 a24 = b4 ^((~b0)& b1 );
1709 b3 = ROL64((a20^d0), 41);
1710 b4 = ROL64((a41^d1), 2);
1711 b0 = ROL64((a12^d2), 62);
1712 b1 = ROL64((a33^d3), 55);
1713 b2 = ROL64((a04^d4), 39);
1714 a20 = b0 ^((~b1)& b2 );
1715 a41 = b1 ^((~b2)& b3 );
1716 a12 = b2 ^((~b3)& b4 );
1717 a33 = b3 ^((~b4)& b0 );
1718 a04 = b4 ^((~b0)& b1 );
1720 c0 = a00^a30^a10^a40^a20;
1721 c1 = a21^a01^a31^a11^a41;
1722 c2 = a42^a22^a02^a32^a12;
1723 c3 = a13^a43^a23^a03^a33;
1724 c4 = a34^a14^a44^a24^a04;
1725 d0 = c4^ROL64(c1, 1);
1726 d1 = c0^ROL64(c2, 1);
1727 d2 = c1^ROL64(c3, 1);
1728 d3 = c2^ROL64(c4, 1);
1729 d4 = c3^ROL64(c0, 1);
1732 b1 = ROL64((a01^d1), 44);
1733 b2 = ROL64((a02^d2), 43);
1734 b3 = ROL64((a03^d3), 21);
1735 b4 = ROL64((a04^d4), 14);
1736 a00 = b0 ^((~b1)& b2 );
1738 a01 = b1 ^((~b2)& b3 );
1739 a02 = b2 ^((~b3)& b4 );
1740 a03 = b3 ^((~b4)& b0 );
1741 a04 = b4 ^((~b0)& b1 );
1743 b2 = ROL64((a10^d0), 3);
1744 b3 = ROL64((a11^d1), 45);
1745 b4 = ROL64((a12^d2), 61);
1746 b0 = ROL64((a13^d3), 28);
1747 b1 = ROL64((a14^d4), 20);
1748 a10 = b0 ^((~b1)& b2 );
1749 a11 = b1 ^((~b2)& b3 );
1750 a12 = b2 ^((~b3)& b4 );
1751 a13 = b3 ^((~b4)& b0 );
1752 a14 = b4 ^((~b0)& b1 );
1754 b4 = ROL64((a20^d0), 18);
1755 b0 = ROL64((a21^d1), 1);
1756 b1 = ROL64((a22^d2), 6);
1757 b2 = ROL64((a23^d3), 25);
1758 b3 = ROL64((a24^d4), 8);
1759 a20 = b0 ^((~b1)& b2 );
1760 a21 = b1 ^((~b2)& b3 );
1761 a22 = b2 ^((~b3)& b4 );
1762 a23 = b3 ^((~b4)& b0 );
1763 a24 = b4 ^((~b0)& b1 );
1765 b1 = ROL64((a30^d0), 36);
1766 b2 = ROL64((a31^d1), 10);
1767 b3 = ROL64((a32^d2), 15);
1768 b4 = ROL64((a33^d3), 56);
1769 b0 = ROL64((a34^d4), 27);
1770 a30 = b0 ^((~b1)& b2 );
1771 a31 = b1 ^((~b2)& b3 );
1772 a32 = b2 ^((~b3)& b4 );
1773 a33 = b3 ^((~b4)& b0 );
1774 a34 = b4 ^((~b0)& b1 );
1776 b3 = ROL64((a40^d0), 41);
1777 b4 = ROL64((a41^d1), 2);
1778 b0 = ROL64((a42^d2), 62);
1779 b1 = ROL64((a43^d3), 55);
1780 b2 = ROL64((a44^d4), 39);
1781 a40 = b0 ^((~b1)& b2 );
1782 a41 = b1 ^((~b2)& b3 );
1783 a42 = b2 ^((~b3)& b4 );
1784 a43 = b3 ^((~b4)& b0 );
1785 a44 = b4 ^((~b0)& b1 );
1790 ** Initialize a new hash. iSize determines the size of the hash
1791 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1792 ** can be zero to use the default hash size of 256 bits.
1794 static void SHA3Init(SHA3Context *p, int iSize){
1795 memset(p, 0, sizeof(*p));
1796 if( iSize>=128 && iSize<=512 ){
1797 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1799 p->nRate = (1600 - 2*256)/8;
1801 #if SHA3_BYTEORDER==1234
1802 /* Known to be little-endian at compile-time. No-op */
1803 #elif SHA3_BYTEORDER==4321
1804 p->ixMask = 7; /* Big-endian */
1807 static unsigned int one = 1;
1808 if( 1==*(unsigned char*)&one ){
1809 /* Little endian. No byte swapping. */
1812 /* Big endian. Byte swap. */
1820 ** Make consecutive calls to the SHA3Update function to add new content
1823 static void SHA3Update(
1825 const unsigned char *aData,
1829 #if SHA3_BYTEORDER==1234
1830 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1831 for(; i+7<nData; i+=8){
1832 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1834 if( p->nLoaded>=p->nRate ){
1841 for(; i<nData; i++){
1842 #if SHA3_BYTEORDER==1234
1843 p->u.x[p->nLoaded] ^= aData[i];
1844 #elif SHA3_BYTEORDER==4321
1845 p->u.x[p->nLoaded^0x07] ^= aData[i];
1847 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1850 if( p->nLoaded==p->nRate ){
1858 ** After all content has been added, invoke SHA3Final() to compute
1859 ** the final hash. The function returns a pointer to the binary
1862 static unsigned char *SHA3Final(SHA3Context *p){
1864 if( p->nLoaded==p->nRate-1 ){
1865 const unsigned char c1 = 0x86;
1866 SHA3Update(p, &c1, 1);
1868 const unsigned char c2 = 0x06;
1869 const unsigned char c3 = 0x80;
1870 SHA3Update(p, &c2, 1);
1871 p->nLoaded = p->nRate - 1;
1872 SHA3Update(p, &c3, 1);
1874 for(i=0; i<p->nRate; i++){
1875 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1877 return &p->u.x[p->nRate];
1879 /* End of the hashing logic
1880 *****************************************************************************/
1883 ** Implementation of the sha3(X,SIZE) function.
1885 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1886 ** size is 256. If X is a BLOB, it is hashed as is.
1887 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1888 ** and the string is hashed without the trailing 0x00 terminator. The hash
1889 ** of a NULL value is NULL.
1891 static void sha3Func(
1892 sqlite3_context *context,
1894 sqlite3_value **argv
1897 int eType = sqlite3_value_type(argv[0]);
1898 int nByte = sqlite3_value_bytes(argv[0]);
1903 iSize = sqlite3_value_int(argv[1]);
1904 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1905 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1910 if( eType==SQLITE_NULL ) return;
1911 SHA3Init(&cx, iSize);
1912 if( eType==SQLITE_BLOB ){
1913 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1915 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1917 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1920 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1921 ** of 50 bytes and add it to the hash.
1923 static void hash_step_vformat(
1924 SHA3Context *p, /* Add content to this context */
1925 const char *zFormat,
1931 va_start(ap, zFormat);
1932 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1934 n = (int)strlen(zBuf);
1935 SHA3Update(p, (unsigned char*)zBuf, n);
1939 ** Implementation of the sha3_query(SQL,SIZE) function.
1941 ** This function compiles and runs the SQL statement(s) given in the
1942 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1945 ** The format of the byte stream that is hashed is summarized as follows:
1955 ** <sql> is the original SQL text for each statement run and <n> is
1956 ** the size of that text. The SQL text is UTF-8. A single R character
1957 ** occurs before the start of each row. N means a NULL value.
1958 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1959 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1960 ** B means blobs of <size> bytes. T means text rendered as <size>
1961 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1964 ** For each SQL statement in the X input, there is one S segment. Each
1965 ** S segment is followed by zero or more R segments, one for each row in the
1966 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1967 ** one for each column in the result set. Segments are concatentated directly
1968 ** with no delimiters of any kind.
1970 static void sha3QueryFunc(
1971 sqlite3_context *context,
1973 sqlite3_value **argv
1975 sqlite3 *db = sqlite3_context_db_handle(context);
1976 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1977 sqlite3_stmt *pStmt = 0;
1978 int nCol; /* Number of columns in the result set */
1979 int i; /* Loop counter */
1989 iSize = sqlite3_value_int(argv[1]);
1990 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1991 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1996 if( zSql==0 ) return;
1997 SHA3Init(&cx, iSize);
1999 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
2001 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
2002 zSql, sqlite3_errmsg(db));
2003 sqlite3_finalize(pStmt);
2004 sqlite3_result_error(context, zMsg, -1);
2008 if( !sqlite3_stmt_readonly(pStmt) ){
2009 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
2010 sqlite3_finalize(pStmt);
2011 sqlite3_result_error(context, zMsg, -1);
2015 nCol = sqlite3_column_count(pStmt);
2016 z = sqlite3_sql(pStmt);
2019 hash_step_vformat(&cx,"S%d:",n);
2020 SHA3Update(&cx,(unsigned char*)z,n);
2023 /* Compute a hash over the result of the query */
2024 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2025 SHA3Update(&cx,(const unsigned char*)"R",1);
2026 for(i=0; i<nCol; i++){
2027 switch( sqlite3_column_type(pStmt,i) ){
2029 SHA3Update(&cx, (const unsigned char*)"N",1);
2032 case SQLITE_INTEGER: {
2036 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2038 for(j=8; j>=1; j--){
2043 SHA3Update(&cx, x, 9);
2046 case SQLITE_FLOAT: {
2050 double r = sqlite3_column_double(pStmt,i);
2052 for(j=8; j>=1; j--){
2057 SHA3Update(&cx,x,9);
2061 int n2 = sqlite3_column_bytes(pStmt, i);
2062 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2063 hash_step_vformat(&cx,"T%d:",n2);
2064 SHA3Update(&cx, z2, n2);
2068 int n2 = sqlite3_column_bytes(pStmt, i);
2069 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2070 hash_step_vformat(&cx,"B%d:",n2);
2071 SHA3Update(&cx, z2, n2);
2077 sqlite3_finalize(pStmt);
2079 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2086 int sqlite3_shathree_init(
2089 const sqlite3_api_routines *pApi
2092 SQLITE_EXTENSION_INIT2(pApi);
2093 (void)pzErrMsg; /* Unused parameter */
2094 rc = sqlite3_create_function(db, "sha3", 1,
2095 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2097 if( rc==SQLITE_OK ){
2098 rc = sqlite3_create_function(db, "sha3", 2,
2099 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2102 if( rc==SQLITE_OK ){
2103 rc = sqlite3_create_function(db, "sha3_query", 1,
2104 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2105 0, sha3QueryFunc, 0, 0);
2107 if( rc==SQLITE_OK ){
2108 rc = sqlite3_create_function(db, "sha3_query", 2,
2109 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2110 0, sha3QueryFunc, 0, 0);
2115 /************************* End ../ext/misc/shathree.c ********************/
2116 /************************* Begin ../ext/misc/fileio.c ******************/
2120 ** The author disclaims copyright to this source code. In place of
2121 ** a legal notice, here is a blessing:
2123 ** May you do good and not evil.
2124 ** May you find forgiveness for yourself and forgive others.
2125 ** May you share freely, never taking more than you give.
2127 ******************************************************************************
2129 ** This SQLite extension implements SQL functions readfile() and
2130 ** writefile(), and eponymous virtual type "fsdir".
2132 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2134 ** If neither of the optional arguments is present, then this UDF
2135 ** function writes blob DATA to file FILE. If successful, the number
2136 ** of bytes written is returned. If an error occurs, NULL is returned.
2138 ** If the first option argument - MODE - is present, then it must
2139 ** be passed an integer value that corresponds to a POSIX mode
2140 ** value (file type + permissions, as returned in the stat.st_mode
2141 ** field by the stat() system call). Three types of files may
2142 ** be written/created:
2144 ** regular files: (mode & 0170000)==0100000
2145 ** symbolic links: (mode & 0170000)==0120000
2146 ** directories: (mode & 0170000)==0040000
2148 ** For a directory, the DATA is ignored. For a symbolic link, it is
2149 ** interpreted as text and used as the target of the link. For a
2150 ** regular file, it is interpreted as a blob and written into the
2151 ** named file. Regardless of the type of file, its permissions are
2152 ** set to (mode & 0777) before returning.
2154 ** If the optional MTIME argument is present, then it is interpreted
2155 ** as an integer - the number of seconds since the unix epoch. The
2156 ** modification-time of the target file is set to this value before
2159 ** If three or more arguments are passed to this function and an
2160 ** error is encountered, an exception is raised.
2164 ** Read and return the contents of file FILE (type blob) from disk.
2170 ** SELECT * FROM fsdir($path [, $dir]);
2172 ** Parameter $path is an absolute or relative pathname. If the file that it
2173 ** refers to does not exist, it is an error. If the path refers to a regular
2174 ** file or symbolic link, it returns a single row. Or, if the path refers
2175 ** to a directory, it returns one row for the directory, and one row for each
2176 ** file within the hierarchy rooted at $path.
2178 ** Each row has the following columns:
2180 ** name: Path to file or directory (text value).
2181 ** mode: Value of stat.st_mode for directory entry (an integer).
2182 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2183 ** data: For a regular file, a blob containing the file data. For a
2184 ** symlink, a text value containing the text of the link. For a
2187 ** If a non-NULL value is specified for the optional $dir parameter and
2188 ** $path is a relative path, then $path is interpreted relative to $dir.
2189 ** And the paths returned in the "name" column of the table are also
2190 ** relative to directory $dir.
2192 /* #include "sqlite3ext.h" */
2193 SQLITE_EXTENSION_INIT1
2198 #include <sys/types.h>
2199 #include <sys/stat.h>
2201 #if !defined(_WIN32) && !defined(WIN32)
2202 # include <unistd.h>
2203 # include <dirent.h>
2205 # include <sys/time.h>
2207 # include "windows.h"
2209 # include <direct.h>
2210 /* # include "test_windirent.h" */
2211 # define dirent DIRENT
2213 # define chmod _chmod
2218 # define mkdir(path,mode) _mkdir(path)
2219 # define lstat(path,buf) stat(path,buf)
2226 ** Structure of the fsdir() table-valued function
2229 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2230 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2231 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2232 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2233 #define FSDIR_COLUMN_DATA 3 /* File content */
2234 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2235 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2239 ** Set the result stored by context ctx to a blob containing the
2240 ** contents of file zName. Or, leave the result unchanged (NULL)
2241 ** if the file does not exist or is unreadable.
2243 ** If the file exceeds the SQLite blob size limit, through an
2244 ** SQLITE_TOOBIG error.
2246 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2249 static void readFileContents(sqlite3_context *ctx, const char *zName){
2256 in = fopen(zName, "rb");
2258 /* File does not exist or is unreadable. Leave the result set to NULL. */
2261 fseek(in, 0, SEEK_END);
2264 db = sqlite3_context_db_handle(ctx);
2265 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2267 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2271 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2273 sqlite3_result_error_nomem(ctx);
2277 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2278 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2280 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2287 ** Implementation of the "readfile(X)" SQL function. The entire content
2288 ** of the file named X is read and returned as a BLOB. NULL is returned
2289 ** if the file does not exist or is unreadable.
2291 static void readfileFunc(
2292 sqlite3_context *context,
2294 sqlite3_value **argv
2297 (void)(argc); /* Unused parameter */
2298 zName = (const char*)sqlite3_value_text(argv[0]);
2299 if( zName==0 ) return;
2300 readFileContents(context, zName);
2304 ** Set the error message contained in context ctx to the results of
2305 ** vprintf(zFmt, ...).
2307 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2311 zMsg = sqlite3_vmprintf(zFmt, ap);
2312 sqlite3_result_error(ctx, zMsg, -1);
2319 ** This function is designed to convert a Win32 FILETIME structure into the
2320 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2322 static sqlite3_uint64 fileTimeToUnixTime(
2323 LPFILETIME pFileTime
2325 SYSTEMTIME epochSystemTime;
2326 ULARGE_INTEGER epochIntervals;
2327 FILETIME epochFileTime;
2328 ULARGE_INTEGER fileIntervals;
2330 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2331 epochSystemTime.wYear = 1970;
2332 epochSystemTime.wMonth = 1;
2333 epochSystemTime.wDay = 1;
2334 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2335 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2336 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2338 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2339 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2341 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2345 ** This function attempts to normalize the time values found in the stat()
2346 ** buffer to UTC. This is necessary on Win32, where the runtime library
2347 ** appears to return these values as local times.
2349 static void statTimesToUtc(
2351 struct stat *pStatBuf
2354 WIN32_FIND_DATAW fd;
2355 LPWSTR zUnicodeName;
2356 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2357 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2359 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2360 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2361 if( hFindFile!=NULL ){
2362 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2363 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2364 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2365 FindClose(hFindFile);
2367 sqlite3_free(zUnicodeName);
2373 ** This function is used in place of stat(). On Windows, special handling
2374 ** is required in order for the included time to be returned as UTC. On all
2375 ** other systems, this function simply calls stat().
2377 static int fileStat(
2379 struct stat *pStatBuf
2382 int rc = stat(zPath, pStatBuf);
2383 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2386 return stat(zPath, pStatBuf);
2391 ** This function is used in place of lstat(). On Windows, special handling
2392 ** is required in order for the included time to be returned as UTC. On all
2393 ** other systems, this function simply calls lstat().
2395 static int fileLinkStat(
2397 struct stat *pStatBuf
2400 int rc = lstat(zPath, pStatBuf);
2401 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2404 return lstat(zPath, pStatBuf);
2409 ** Argument zFile is the name of a file that will be created and/or written
2410 ** by SQL function writefile(). This function ensures that the directory
2411 ** zFile will be written to exists, creating it if required. The permissions
2412 ** for any path components created by this function are set in accordance
2413 ** with the current umask.
2415 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2416 ** SQLITE_OK is returned if the directory is successfully created, or
2417 ** SQLITE_ERROR otherwise.
2419 static int makeDirectory(
2422 char *zCopy = sqlite3_mprintf("%s", zFile);
2428 int nCopy = (int)strlen(zCopy);
2431 while( rc==SQLITE_OK ){
2435 for(; zCopy[i]!='/' && i<nCopy; i++);
2436 if( i==nCopy ) break;
2439 rc2 = fileStat(zCopy, &sStat);
2441 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2443 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2449 sqlite3_free(zCopy);
2456 ** This function does the work for the writefile() UDF. Refer to
2457 ** header comments at the top of this file for details.
2459 static int writeFile(
2460 sqlite3_context *pCtx, /* Context to return bytes written in */
2461 const char *zFile, /* File to write */
2462 sqlite3_value *pData, /* Data to write */
2463 mode_t mode, /* MODE parameter passed to writefile() */
2464 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2466 #if !defined(_WIN32) && !defined(WIN32)
2467 if( S_ISLNK(mode) ){
2468 const char *zTo = (const char*)sqlite3_value_text(pData);
2469 if( symlink(zTo, zFile)<0 ) return 1;
2473 if( S_ISDIR(mode) ){
2474 if( mkdir(zFile, mode) ){
2475 /* The mkdir() call to create the directory failed. This might not
2476 ** be an error though - if there is already a directory at the same
2477 ** path and either the permissions already match or can be changed
2478 ** to do so using chmod(), it is not an error. */
2481 || 0!=fileStat(zFile, &sStat)
2482 || !S_ISDIR(sStat.st_mode)
2483 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2489 sqlite3_int64 nWrite = 0;
2492 FILE *out = fopen(zFile, "wb");
2493 if( out==0 ) return 1;
2494 z = (const char*)sqlite3_value_blob(pData);
2496 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2497 nWrite = sqlite3_value_bytes(pData);
2503 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2507 sqlite3_result_int64(pCtx, nWrite);
2513 #if !SQLITE_OS_WINRT
2515 FILETIME lastAccess;
2517 SYSTEMTIME currentTime;
2520 LPWSTR zUnicodeName;
2521 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2523 GetSystemTime(¤tTime);
2524 SystemTimeToFileTime(¤tTime, &lastAccess);
2525 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2526 lastWrite.dwLowDateTime = (DWORD)intervals;
2527 lastWrite.dwHighDateTime = intervals >> 32;
2528 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2529 if( zUnicodeName==0 ){
2532 hFile = CreateFileW(
2533 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2534 FILE_FLAG_BACKUP_SEMANTICS, NULL
2536 sqlite3_free(zUnicodeName);
2537 if( hFile!=INVALID_HANDLE_VALUE ){
2538 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2545 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2547 struct timespec times[2];
2548 times[0].tv_nsec = times[1].tv_nsec = 0;
2549 times[0].tv_sec = time(0);
2550 times[1].tv_sec = mtime;
2551 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2556 struct timeval times[2];
2557 times[0].tv_usec = times[1].tv_usec = 0;
2558 times[0].tv_sec = time(0);
2559 times[1].tv_sec = mtime;
2560 if( utimes(zFile, times) ){
2570 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2571 ** Refer to header comments at the top of this file for details.
2573 static void writefileFunc(
2574 sqlite3_context *context,
2576 sqlite3_value **argv
2581 sqlite3_int64 mtime = -1;
2583 if( argc<2 || argc>4 ){
2584 sqlite3_result_error(context,
2585 "wrong number of arguments to function writefile()", -1
2590 zFile = (const char*)sqlite3_value_text(argv[0]);
2591 if( zFile==0 ) return;
2593 mode = (mode_t)sqlite3_value_int(argv[2]);
2596 mtime = sqlite3_value_int64(argv[3]);
2599 res = writeFile(context, zFile, argv[1], mode, mtime);
2600 if( res==1 && errno==ENOENT ){
2601 if( makeDirectory(zFile)==SQLITE_OK ){
2602 res = writeFile(context, zFile, argv[1], mode, mtime);
2606 if( argc>2 && res!=0 ){
2607 if( S_ISLNK(mode) ){
2608 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2609 }else if( S_ISDIR(mode) ){
2610 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2612 ctxErrorMsg(context, "failed to write file: %s", zFile);
2618 ** SQL function: lsmode(MODE)
2620 ** Given a numberic st_mode from stat(), convert it into a human-readable
2621 ** text string in the style of "ls -l".
2623 static void lsModeFunc(
2624 sqlite3_context *context,
2626 sqlite3_value **argv
2629 int iMode = sqlite3_value_int(argv[0]);
2632 if( S_ISLNK(iMode) ){
2634 }else if( S_ISREG(iMode) ){
2636 }else if( S_ISDIR(iMode) ){
2642 int m = (iMode >> ((2-i)*3));
2643 char *a = &z[1 + i*3];
2644 a[0] = (m & 0x4) ? 'r' : '-';
2645 a[1] = (m & 0x2) ? 'w' : '-';
2646 a[2] = (m & 0x1) ? 'x' : '-';
2649 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2652 #ifndef SQLITE_OMIT_VIRTUALTABLE
2655 ** Cursor type for recursively iterating through a directory structure.
2657 typedef struct fsdir_cursor fsdir_cursor;
2658 typedef struct FsdirLevel FsdirLevel;
2661 DIR *pDir; /* From opendir() */
2662 char *zDir; /* Name of directory (nul-terminated) */
2665 struct fsdir_cursor {
2666 sqlite3_vtab_cursor base; /* Base class - must be first */
2668 int nLvl; /* Number of entries in aLvl[] array */
2669 int iLvl; /* Index of current entry */
2670 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2675 struct stat sStat; /* Current lstat() results */
2676 char *zPath; /* Path to current entry */
2677 sqlite3_int64 iRowid; /* Current rowid */
2680 typedef struct fsdir_tab fsdir_tab;
2682 sqlite3_vtab base; /* Base class - must be first */
2686 ** Construct a new fsdir virtual table object.
2688 static int fsdirConnect(
2691 int argc, const char *const*argv,
2692 sqlite3_vtab **ppVtab,
2695 fsdir_tab *pNew = 0;
2701 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2702 if( rc==SQLITE_OK ){
2703 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2704 if( pNew==0 ) return SQLITE_NOMEM;
2705 memset(pNew, 0, sizeof(*pNew));
2706 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2708 *ppVtab = (sqlite3_vtab*)pNew;
2713 ** This method is the destructor for fsdir vtab objects.
2715 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2716 sqlite3_free(pVtab);
2721 ** Constructor for a new fsdir_cursor object.
2723 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2726 pCur = sqlite3_malloc( sizeof(*pCur) );
2727 if( pCur==0 ) return SQLITE_NOMEM;
2728 memset(pCur, 0, sizeof(*pCur));
2730 *ppCursor = &pCur->base;
2735 ** Reset a cursor back to the state it was in when first returned
2738 static void fsdirResetCursor(fsdir_cursor *pCur){
2740 for(i=0; i<=pCur->iLvl; i++){
2741 FsdirLevel *pLvl = &pCur->aLvl[i];
2742 if( pLvl->pDir ) closedir(pLvl->pDir);
2743 sqlite3_free(pLvl->zDir);
2745 sqlite3_free(pCur->zPath);
2746 sqlite3_free(pCur->aLvl);
2757 ** Destructor for an fsdir_cursor.
2759 static int fsdirClose(sqlite3_vtab_cursor *cur){
2760 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2762 fsdirResetCursor(pCur);
2768 ** Set the error message for the virtual table associated with cursor
2769 ** pCur to the results of vprintf(zFmt, ...).
2771 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2774 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2780 ** Advance an fsdir_cursor to its next row of output.
2782 static int fsdirNext(sqlite3_vtab_cursor *cur){
2783 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2784 mode_t m = pCur->sStat.st_mode;
2788 /* Descend into this directory */
2789 int iNew = pCur->iLvl + 1;
2791 if( iNew>=pCur->nLvl ){
2793 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2794 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2795 if( aNew==0 ) return SQLITE_NOMEM;
2796 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2801 pLvl = &pCur->aLvl[iNew];
2803 pLvl->zDir = pCur->zPath;
2805 pLvl->pDir = opendir(pLvl->zDir);
2806 if( pLvl->pDir==0 ){
2807 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2808 return SQLITE_ERROR;
2812 while( pCur->iLvl>=0 ){
2813 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2814 struct dirent *pEntry = readdir(pLvl->pDir);
2816 if( pEntry->d_name[0]=='.' ){
2817 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2818 if( pEntry->d_name[1]=='\0' ) continue;
2820 sqlite3_free(pCur->zPath);
2821 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2822 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2823 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2824 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2825 return SQLITE_ERROR;
2829 closedir(pLvl->pDir);
2830 sqlite3_free(pLvl->zDir);
2837 sqlite3_free(pCur->zPath);
2843 ** Return values of columns for the row at which the series_cursor
2844 ** is currently pointing.
2846 static int fsdirColumn(
2847 sqlite3_vtab_cursor *cur, /* The cursor */
2848 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2849 int i /* Which column to return */
2851 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2853 case FSDIR_COLUMN_NAME: {
2854 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2858 case FSDIR_COLUMN_MODE:
2859 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2862 case FSDIR_COLUMN_MTIME:
2863 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2866 case FSDIR_COLUMN_DATA: {
2867 mode_t m = pCur->sStat.st_mode;
2869 sqlite3_result_null(ctx);
2870 #if !defined(_WIN32) && !defined(WIN32)
2871 }else if( S_ISLNK(m) ){
2873 char *aBuf = aStatic;
2874 sqlite3_int64 nBuf = 64;
2878 n = readlink(pCur->zPath, aBuf, nBuf);
2880 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2882 aBuf = sqlite3_malloc64(nBuf);
2884 sqlite3_result_error_nomem(ctx);
2885 return SQLITE_NOMEM;
2889 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2890 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2893 readFileContents(ctx, pCur->zPath);
2896 case FSDIR_COLUMN_PATH:
2898 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2899 ** always return their values as NULL */
2907 ** Return the rowid for the current row. In this implementation, the
2908 ** first row returned is assigned rowid value 1, and each subsequent
2909 ** row a value 1 more than that of the previous.
2911 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2912 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2913 *pRowid = pCur->iRowid;
2918 ** Return TRUE if the cursor has been moved off of the last
2921 static int fsdirEof(sqlite3_vtab_cursor *cur){
2922 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2923 return (pCur->zPath==0);
2927 ** xFilter callback.
2929 ** idxNum==1 PATH parameter only
2930 ** idxNum==2 Both PATH and DIR supplied
2932 static int fsdirFilter(
2933 sqlite3_vtab_cursor *cur,
2934 int idxNum, const char *idxStr,
2935 int argc, sqlite3_value **argv
2937 const char *zDir = 0;
2938 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2940 fsdirResetCursor(pCur);
2943 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2944 return SQLITE_ERROR;
2947 assert( argc==idxNum && (argc==1 || argc==2) );
2948 zDir = (const char*)sqlite3_value_text(argv[0]);
2950 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2951 return SQLITE_ERROR;
2954 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2957 pCur->nBase = (int)strlen(pCur->zBase)+1;
2958 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2960 pCur->zPath = sqlite3_mprintf("%s", zDir);
2963 if( pCur->zPath==0 ){
2964 return SQLITE_NOMEM;
2966 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2967 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2968 return SQLITE_ERROR;
2975 ** SQLite will invoke this method one or more times while planning a query
2976 ** that uses the generate_series virtual table. This routine needs to create
2977 ** a query plan for each invocation and compute an estimated cost for that
2980 ** In this implementation idxNum is used to represent the
2981 ** query plan. idxStr is unused.
2983 ** The query plan is represented by values of idxNum:
2985 ** (1) The path value is supplied by argv[0]
2986 ** (2) Path is in argv[0] and dir is in argv[1]
2988 static int fsdirBestIndex(
2990 sqlite3_index_info *pIdxInfo
2992 int i; /* Loop over constraints */
2993 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2994 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2995 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2996 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
2997 const struct sqlite3_index_constraint *pConstraint;
3000 pConstraint = pIdxInfo->aConstraint;
3001 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3002 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3003 switch( pConstraint->iColumn ){
3004 case FSDIR_COLUMN_PATH: {
3005 if( pConstraint->usable ){
3008 }else if( idxPath<0 ){
3013 case FSDIR_COLUMN_DIR: {
3014 if( pConstraint->usable ){
3017 }else if( idxDir<0 ){
3024 if( seenPath || seenDir ){
3025 /* If input parameters are unusable, disallow this plan */
3026 return SQLITE_CONSTRAINT;
3030 pIdxInfo->idxNum = 0;
3031 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3032 ** number. Leave it unchanged. */
3033 pIdxInfo->estimatedRows = 0x7fffffff;
3035 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3036 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3038 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3039 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3040 pIdxInfo->idxNum = 2;
3041 pIdxInfo->estimatedCost = 10.0;
3043 pIdxInfo->idxNum = 1;
3044 pIdxInfo->estimatedCost = 100.0;
3052 ** Register the "fsdir" virtual table.
3054 static int fsdirRegister(sqlite3 *db){
3055 static sqlite3_module fsdirModule = {
3058 fsdirConnect, /* xConnect */
3059 fsdirBestIndex, /* xBestIndex */
3060 fsdirDisconnect, /* xDisconnect */
3062 fsdirOpen, /* xOpen - open a cursor */
3063 fsdirClose, /* xClose - close a cursor */
3064 fsdirFilter, /* xFilter - configure scan constraints */
3065 fsdirNext, /* xNext - advance a cursor */
3066 fsdirEof, /* xEof - check for end of scan */
3067 fsdirColumn, /* xColumn - read data */
3068 fsdirRowid, /* xRowid - read data */
3074 0, /* xFindMethod */
3078 0, /* xRollbackTo */
3079 0, /* xShadowName */
3082 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3085 #else /* SQLITE_OMIT_VIRTUALTABLE */
3086 # define fsdirRegister(x) SQLITE_OK
3092 int sqlite3_fileio_init(
3095 const sqlite3_api_routines *pApi
3098 SQLITE_EXTENSION_INIT2(pApi);
3099 (void)pzErrMsg; /* Unused parameter */
3100 rc = sqlite3_create_function(db, "readfile", 1,
3101 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3102 readfileFunc, 0, 0);
3103 if( rc==SQLITE_OK ){
3104 rc = sqlite3_create_function(db, "writefile", -1,
3105 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3106 writefileFunc, 0, 0);
3108 if( rc==SQLITE_OK ){
3109 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3112 if( rc==SQLITE_OK ){
3113 rc = fsdirRegister(db);
3118 /************************* End ../ext/misc/fileio.c ********************/
3119 /************************* Begin ../ext/misc/completion.c ******************/
3123 ** The author disclaims copyright to this source code. In place of
3124 ** a legal notice, here is a blessing:
3126 ** May you do good and not evil.
3127 ** May you find forgiveness for yourself and forgive others.
3128 ** May you share freely, never taking more than you give.
3130 *************************************************************************
3132 ** This file implements an eponymous virtual table that returns suggested
3133 ** completions for a partial SQL input.
3137 ** SELECT DISTINCT candidate COLLATE nocase
3138 ** FROM completion($prefix,$wholeline)
3141 ** The two query parameters are optional. $prefix is the text of the
3142 ** current word being typed and that is to be completed. $wholeline is
3143 ** the complete input line, used for context.
3145 ** The raw completion() table might return the same candidate multiple
3146 ** times, for example if the same column name is used to two or more
3147 ** tables. And the candidates are returned in an arbitrary order. Hence,
3148 ** the DISTINCT and ORDER BY are recommended.
3150 ** This virtual table operates at the speed of human typing, and so there
3151 ** is no attempt to make it fast. Even a slow implementation will be much
3152 ** faster than any human can type.
3155 /* #include "sqlite3ext.h" */
3156 SQLITE_EXTENSION_INIT1
3161 #ifndef SQLITE_OMIT_VIRTUALTABLE
3163 /* completion_vtab is a subclass of sqlite3_vtab which will
3164 ** serve as the underlying representation of a completion virtual table
3166 typedef struct completion_vtab completion_vtab;
3167 struct completion_vtab {
3168 sqlite3_vtab base; /* Base class - must be first */
3169 sqlite3 *db; /* Database connection for this completion vtab */
3172 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3173 ** serve as the underlying representation of a cursor that scans
3174 ** over rows of the result
3176 typedef struct completion_cursor completion_cursor;
3177 struct completion_cursor {
3178 sqlite3_vtab_cursor base; /* Base class - must be first */
3179 sqlite3 *db; /* Database connection for this cursor */
3180 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3181 char *zPrefix; /* The prefix for the word we want to complete */
3182 char *zLine; /* The whole that we want to complete */
3183 const char *zCurrentRow; /* Current output row */
3184 int szRow; /* Length of the zCurrentRow string */
3185 sqlite3_stmt *pStmt; /* Current statement */
3186 sqlite3_int64 iRowid; /* The rowid */
3187 int ePhase; /* Current phase */
3188 int j; /* inter-phase counter */
3191 /* Values for ePhase:
3193 #define COMPLETION_FIRST_PHASE 1
3194 #define COMPLETION_KEYWORDS 1
3195 #define COMPLETION_PRAGMAS 2
3196 #define COMPLETION_FUNCTIONS 3
3197 #define COMPLETION_COLLATIONS 4
3198 #define COMPLETION_INDEXES 5
3199 #define COMPLETION_TRIGGERS 6
3200 #define COMPLETION_DATABASES 7
3201 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3202 #define COMPLETION_COLUMNS 9
3203 #define COMPLETION_MODULES 10
3204 #define COMPLETION_EOF 11
3207 ** The completionConnect() method is invoked to create a new
3208 ** completion_vtab that describes the completion virtual table.
3210 ** Think of this routine as the constructor for completion_vtab objects.
3212 ** All this routine needs to do is:
3214 ** (1) Allocate the completion_vtab object and initialize all fields.
3216 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3217 ** result set of queries against completion will look like.
3219 static int completionConnect(
3222 int argc, const char *const*argv,
3223 sqlite3_vtab **ppVtab,
3226 completion_vtab *pNew;
3229 (void)(pAux); /* Unused parameter */
3230 (void)(argc); /* Unused parameter */
3231 (void)(argv); /* Unused parameter */
3232 (void)(pzErr); /* Unused parameter */
3234 /* Column numbers */
3235 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3236 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3237 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3238 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3240 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3241 rc = sqlite3_declare_vtab(db,
3244 " prefix TEXT HIDDEN,"
3245 " wholeline TEXT HIDDEN,"
3246 " phase INT HIDDEN" /* Used for debugging only */
3248 if( rc==SQLITE_OK ){
3249 pNew = sqlite3_malloc( sizeof(*pNew) );
3250 *ppVtab = (sqlite3_vtab*)pNew;
3251 if( pNew==0 ) return SQLITE_NOMEM;
3252 memset(pNew, 0, sizeof(*pNew));
3259 ** This method is the destructor for completion_cursor objects.
3261 static int completionDisconnect(sqlite3_vtab *pVtab){
3262 sqlite3_free(pVtab);
3267 ** Constructor for a new completion_cursor object.
3269 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3270 completion_cursor *pCur;
3271 pCur = sqlite3_malloc( sizeof(*pCur) );
3272 if( pCur==0 ) return SQLITE_NOMEM;
3273 memset(pCur, 0, sizeof(*pCur));
3274 pCur->db = ((completion_vtab*)p)->db;
3275 *ppCursor = &pCur->base;
3280 ** Reset the completion_cursor.
3282 static void completionCursorReset(completion_cursor *pCur){
3283 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3284 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3285 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3290 ** Destructor for a completion_cursor.
3292 static int completionClose(sqlite3_vtab_cursor *cur){
3293 completionCursorReset((completion_cursor*)cur);
3299 ** Advance a completion_cursor to its next row of output.
3301 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3302 ** record the current state of the scan. This routine sets ->zCurrentRow
3303 ** to the current row of output and then returns. If no more rows remain,
3304 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3305 ** table that has reached the end of its scan.
3307 ** The current implementation just lists potential identifiers and
3308 ** keywords and filters them by zPrefix. Future enhancements should
3309 ** take zLine into account to try to restrict the set of identifiers and
3310 ** keywords based on what would be legal at the current point of input.
3312 static int completionNext(sqlite3_vtab_cursor *cur){
3313 completion_cursor *pCur = (completion_cursor*)cur;
3314 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3315 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3317 while( pCur->ePhase!=COMPLETION_EOF ){
3318 switch( pCur->ePhase ){
3319 case COMPLETION_KEYWORDS: {
3320 if( pCur->j >= sqlite3_keyword_count() ){
3321 pCur->zCurrentRow = 0;
3322 pCur->ePhase = COMPLETION_DATABASES;
3324 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3329 case COMPLETION_DATABASES: {
3330 if( pCur->pStmt==0 ){
3331 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3335 eNextPhase = COMPLETION_TABLES;
3338 case COMPLETION_TABLES: {
3339 if( pCur->pStmt==0 ){
3342 const char *zSep = "";
3343 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3344 while( sqlite3_step(pS2)==SQLITE_ROW ){
3345 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3346 zSql = sqlite3_mprintf(
3348 "SELECT name FROM \"%w\".sqlite_schema",
3351 if( zSql==0 ) return SQLITE_NOMEM;
3354 sqlite3_finalize(pS2);
3355 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3359 eNextPhase = COMPLETION_COLUMNS;
3362 case COMPLETION_COLUMNS: {
3363 if( pCur->pStmt==0 ){
3366 const char *zSep = "";
3367 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3368 while( sqlite3_step(pS2)==SQLITE_ROW ){
3369 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3370 zSql = sqlite3_mprintf(
3372 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3373 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3374 " WHERE sm.type='table'",
3375 zSql, zSep, zDb, zDb
3377 if( zSql==0 ) return SQLITE_NOMEM;
3380 sqlite3_finalize(pS2);
3381 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3385 eNextPhase = COMPLETION_EOF;
3390 /* This case is when the phase presets zCurrentRow */
3391 if( pCur->zCurrentRow==0 ) continue;
3393 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3394 /* Extract the next row of content */
3395 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3396 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3398 /* When all rows are finished, advance to the next phase */
3399 sqlite3_finalize(pCur->pStmt);
3401 pCur->ePhase = eNextPhase;
3405 if( pCur->nPrefix==0 ) break;
3406 if( pCur->nPrefix<=pCur->szRow
3407 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3417 ** Return values of columns for the row at which the completion_cursor
3418 ** is currently pointing.
3420 static int completionColumn(
3421 sqlite3_vtab_cursor *cur, /* The cursor */
3422 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3423 int i /* Which column to return */
3425 completion_cursor *pCur = (completion_cursor*)cur;
3427 case COMPLETION_COLUMN_CANDIDATE: {
3428 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3431 case COMPLETION_COLUMN_PREFIX: {
3432 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3435 case COMPLETION_COLUMN_WHOLELINE: {
3436 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3439 case COMPLETION_COLUMN_PHASE: {
3440 sqlite3_result_int(ctx, pCur->ePhase);
3448 ** Return the rowid for the current row. In this implementation, the
3449 ** rowid is the same as the output value.
3451 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3452 completion_cursor *pCur = (completion_cursor*)cur;
3453 *pRowid = pCur->iRowid;
3458 ** Return TRUE if the cursor has been moved off of the last
3461 static int completionEof(sqlite3_vtab_cursor *cur){
3462 completion_cursor *pCur = (completion_cursor*)cur;
3463 return pCur->ePhase >= COMPLETION_EOF;
3467 ** This method is called to "rewind" the completion_cursor object back
3468 ** to the first row of output. This method is always called at least
3469 ** once prior to any call to completionColumn() or completionRowid() or
3472 static int completionFilter(
3473 sqlite3_vtab_cursor *pVtabCursor,
3474 int idxNum, const char *idxStr,
3475 int argc, sqlite3_value **argv
3477 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3479 (void)(idxStr); /* Unused parameter */
3480 (void)(argc); /* Unused parameter */
3481 completionCursorReset(pCur);
3483 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3484 if( pCur->nPrefix>0 ){
3485 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3486 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3491 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3492 if( pCur->nLine>0 ){
3493 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3494 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3497 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3498 int i = pCur->nLine;
3499 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3502 pCur->nPrefix = pCur->nLine - i;
3503 if( pCur->nPrefix>0 ){
3504 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3505 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3509 pCur->ePhase = COMPLETION_FIRST_PHASE;
3510 return completionNext(pVtabCursor);
3514 ** SQLite will invoke this method one or more times while planning a query
3515 ** that uses the completion virtual table. This routine needs to create
3516 ** a query plan for each invocation and compute an estimated cost for that
3519 ** There are two hidden parameters that act as arguments to the table-valued
3520 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3521 ** is available and bit 1 is set if "wholeline" is available.
3523 static int completionBestIndex(
3525 sqlite3_index_info *pIdxInfo
3527 int i; /* Loop over constraints */
3528 int idxNum = 0; /* The query plan bitmask */
3529 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3530 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3531 int nArg = 0; /* Number of arguments that completeFilter() expects */
3532 const struct sqlite3_index_constraint *pConstraint;
3534 (void)(tab); /* Unused parameter */
3535 pConstraint = pIdxInfo->aConstraint;
3536 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3537 if( pConstraint->usable==0 ) continue;
3538 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3539 switch( pConstraint->iColumn ){
3540 case COMPLETION_COLUMN_PREFIX:
3544 case COMPLETION_COLUMN_WHOLELINE:
3551 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3552 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3554 if( wholelineIdx>=0 ){
3555 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3556 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3558 pIdxInfo->idxNum = idxNum;
3559 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3560 pIdxInfo->estimatedRows = 500 - 100*nArg;
3565 ** This following structure defines all the methods for the
3566 ** completion virtual table.
3568 static sqlite3_module completionModule = {
3571 completionConnect, /* xConnect */
3572 completionBestIndex, /* xBestIndex */
3573 completionDisconnect, /* xDisconnect */
3575 completionOpen, /* xOpen - open a cursor */
3576 completionClose, /* xClose - close a cursor */
3577 completionFilter, /* xFilter - configure scan constraints */
3578 completionNext, /* xNext - advance a cursor */
3579 completionEof, /* xEof - check for end of scan */
3580 completionColumn, /* xColumn - read data */
3581 completionRowid, /* xRowid - read data */
3587 0, /* xFindMethod */
3591 0, /* xRollbackTo */
3595 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3597 int sqlite3CompletionVtabInit(sqlite3 *db){
3599 #ifndef SQLITE_OMIT_VIRTUALTABLE
3600 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3608 int sqlite3_completion_init(
3611 const sqlite3_api_routines *pApi
3614 SQLITE_EXTENSION_INIT2(pApi);
3615 (void)(pzErrMsg); /* Unused parameter */
3616 #ifndef SQLITE_OMIT_VIRTUALTABLE
3617 rc = sqlite3CompletionVtabInit(db);
3622 /************************* End ../ext/misc/completion.c ********************/
3623 /************************* Begin ../ext/misc/appendvfs.c ******************/
3627 ** The author disclaims copyright to this source code. In place of
3628 ** a legal notice, here is a blessing:
3630 ** May you do good and not evil.
3631 ** May you find forgiveness for yourself and forgive others.
3632 ** May you share freely, never taking more than you give.
3634 ******************************************************************************
3636 ** This file implements a VFS shim that allows an SQLite database to be
3637 ** appended onto the end of some other file, such as an executable.
3639 ** A special record must appear at the end of the file that identifies the
3640 ** file as an appended database and provides an offset to page 1. For
3641 ** best performance page 1 should be located at a disk page boundary, though
3642 ** that is not required.
3644 ** When opening a database using this VFS, the connection might treat
3645 ** the file as an ordinary SQLite database, or it might treat is as a
3646 ** database appended onto some other file. Here are the rules:
3648 ** (1) When opening a new empty file, that file is treated as an ordinary
3651 ** (2) When opening a file that begins with the standard SQLite prefix
3652 ** string "SQLite format 3", that file is treated as an ordinary
3655 ** (3) When opening a file that ends with the appendvfs trailer string
3656 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3659 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3660 ** set, then a new database is appended to the already existing file.
3662 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3664 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3665 ** the file containing the database is limited to 1GB. This VFS will refuse
3666 ** to read or write past the 1GB mark. This restriction might be lifted in
3667 ** future versions. For now, if you need a large database, then keep the
3668 ** database in a separate file.
3670 ** If the file being opened is not an appended database, then this shim is
3671 ** a pass-through into the default underlying VFS.
3673 /* #include "sqlite3ext.h" */
3674 SQLITE_EXTENSION_INIT1
3678 /* The append mark at the end of the database is:
3680 ** Start-Of-SQLite3-NNNNNNNN
3681 ** 123456789 123456789 12345
3683 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3684 ** the offset to page 1.
3686 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3687 #define APND_MARK_PREFIX_SZ 17
3688 #define APND_MARK_SIZE 25
3691 ** Maximum size of the combined prefix + database + append-mark. This
3692 ** must be less than 0x40000000 to avoid locking issues on Windows.
3694 #define APND_MAX_SIZE (65536*15259)
3697 ** Forward declaration of objects used by this utility
3699 typedef struct sqlite3_vfs ApndVfs;
3700 typedef struct ApndFile ApndFile;
3702 /* Access to a lower-level VFS that (might) implement dynamic loading,
3703 ** access to randomness, etc.
3705 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3706 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3710 sqlite3_file base; /* IO methods */
3711 sqlite3_int64 iPgOne; /* File offset to page 1 */
3712 sqlite3_int64 iMark; /* Start of the append-mark */
3716 ** Methods for ApndFile
3718 static int apndClose(sqlite3_file*);
3719 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3720 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3721 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3722 static int apndSync(sqlite3_file*, int flags);
3723 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3724 static int apndLock(sqlite3_file*, int);
3725 static int apndUnlock(sqlite3_file*, int);
3726 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3727 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3728 static int apndSectorSize(sqlite3_file*);
3729 static int apndDeviceCharacteristics(sqlite3_file*);
3730 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3731 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3732 static void apndShmBarrier(sqlite3_file*);
3733 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3734 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3735 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3738 ** Methods for ApndVfs
3740 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3741 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3742 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3743 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3744 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3745 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3746 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3747 static void apndDlClose(sqlite3_vfs*, void*);
3748 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3749 static int apndSleep(sqlite3_vfs*, int microseconds);
3750 static int apndCurrentTime(sqlite3_vfs*, double*);
3751 static int apndGetLastError(sqlite3_vfs*, int, char *);
3752 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3753 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3754 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3755 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3757 static sqlite3_vfs apnd_vfs = {
3758 3, /* iVersion (set when registered) */
3759 0, /* szOsFile (set when registered) */
3760 1024, /* mxPathname */
3762 "apndvfs", /* zName */
3763 0, /* pAppData (set when registered) */
3764 apndOpen, /* xOpen */
3765 apndDelete, /* xDelete */
3766 apndAccess, /* xAccess */
3767 apndFullPathname, /* xFullPathname */
3768 apndDlOpen, /* xDlOpen */
3769 apndDlError, /* xDlError */
3770 apndDlSym, /* xDlSym */
3771 apndDlClose, /* xDlClose */
3772 apndRandomness, /* xRandomness */
3773 apndSleep, /* xSleep */
3774 apndCurrentTime, /* xCurrentTime */
3775 apndGetLastError, /* xGetLastError */
3776 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3777 apndSetSystemCall, /* xSetSystemCall */
3778 apndGetSystemCall, /* xGetSystemCall */
3779 apndNextSystemCall /* xNextSystemCall */
3782 static const sqlite3_io_methods apnd_io_methods = {
3784 apndClose, /* xClose */
3785 apndRead, /* xRead */
3786 apndWrite, /* xWrite */
3787 apndTruncate, /* xTruncate */
3788 apndSync, /* xSync */
3789 apndFileSize, /* xFileSize */
3790 apndLock, /* xLock */
3791 apndUnlock, /* xUnlock */
3792 apndCheckReservedLock, /* xCheckReservedLock */
3793 apndFileControl, /* xFileControl */
3794 apndSectorSize, /* xSectorSize */
3795 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3796 apndShmMap, /* xShmMap */
3797 apndShmLock, /* xShmLock */
3798 apndShmBarrier, /* xShmBarrier */
3799 apndShmUnmap, /* xShmUnmap */
3800 apndFetch, /* xFetch */
3801 apndUnfetch /* xUnfetch */
3807 ** Close an apnd-file.
3809 static int apndClose(sqlite3_file *pFile){
3810 pFile = ORIGFILE(pFile);
3811 return pFile->pMethods->xClose(pFile);
3815 ** Read data from an apnd-file.
3817 static int apndRead(
3818 sqlite3_file *pFile,
3823 ApndFile *p = (ApndFile *)pFile;
3824 pFile = ORIGFILE(pFile);
3825 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3829 ** Add the append-mark onto the end of the file.
3831 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3833 unsigned char a[APND_MARK_SIZE];
3834 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3836 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3838 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3842 ** Write data to an apnd-file.
3844 static int apndWrite(
3845 sqlite3_file *pFile,
3851 ApndFile *p = (ApndFile *)pFile;
3852 pFile = ORIGFILE(pFile);
3853 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3854 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3855 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3856 sqlite3_int64 sz = 0;
3857 rc = pFile->pMethods->xFileSize(pFile, &sz);
3858 if( rc==SQLITE_OK ){
3859 p->iMark = sz - APND_MARK_SIZE;
3860 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3861 p->iMark = p->iPgOne + iOfst + iAmt;
3862 rc = apndWriteMark(p, pFile);
3870 ** Truncate an apnd-file.
3872 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3874 ApndFile *p = (ApndFile *)pFile;
3875 pFile = ORIGFILE(pFile);
3876 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3877 if( rc==SQLITE_OK ){
3878 p->iMark = p->iPgOne+size;
3879 rc = apndWriteMark(p, pFile);
3885 ** Sync an apnd-file.
3887 static int apndSync(sqlite3_file *pFile, int flags){
3888 pFile = ORIGFILE(pFile);
3889 return pFile->pMethods->xSync(pFile, flags);
3893 ** Return the current file-size of an apnd-file.
3895 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3896 ApndFile *p = (ApndFile *)pFile;
3898 pFile = ORIGFILE(p);
3899 rc = pFile->pMethods->xFileSize(pFile, pSize);
3900 if( rc==SQLITE_OK && p->iPgOne ){
3901 *pSize -= p->iPgOne + APND_MARK_SIZE;
3907 ** Lock an apnd-file.
3909 static int apndLock(sqlite3_file *pFile, int eLock){
3910 pFile = ORIGFILE(pFile);
3911 return pFile->pMethods->xLock(pFile, eLock);
3915 ** Unlock an apnd-file.
3917 static int apndUnlock(sqlite3_file *pFile, int eLock){
3918 pFile = ORIGFILE(pFile);
3919 return pFile->pMethods->xUnlock(pFile, eLock);
3923 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3925 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3926 pFile = ORIGFILE(pFile);
3927 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3931 ** File control method. For custom operations on an apnd-file.
3933 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3934 ApndFile *p = (ApndFile *)pFile;
3936 pFile = ORIGFILE(pFile);
3937 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3938 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3939 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3945 ** Return the sector-size in bytes for an apnd-file.
3947 static int apndSectorSize(sqlite3_file *pFile){
3948 pFile = ORIGFILE(pFile);
3949 return pFile->pMethods->xSectorSize(pFile);
3953 ** Return the device characteristic flags supported by an apnd-file.
3955 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3956 pFile = ORIGFILE(pFile);
3957 return pFile->pMethods->xDeviceCharacteristics(pFile);
3960 /* Create a shared memory file mapping */
3961 static int apndShmMap(
3962 sqlite3_file *pFile,
3968 pFile = ORIGFILE(pFile);
3969 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3972 /* Perform locking on a shared-memory segment */
3973 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3974 pFile = ORIGFILE(pFile);
3975 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3978 /* Memory barrier operation on shared memory */
3979 static void apndShmBarrier(sqlite3_file *pFile){
3980 pFile = ORIGFILE(pFile);
3981 pFile->pMethods->xShmBarrier(pFile);
3984 /* Unmap a shared memory segment */
3985 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3986 pFile = ORIGFILE(pFile);
3987 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3990 /* Fetch a page of a memory-mapped file */
3991 static int apndFetch(
3992 sqlite3_file *pFile,
3993 sqlite3_int64 iOfst,
3997 ApndFile *p = (ApndFile *)pFile;
3998 pFile = ORIGFILE(pFile);
3999 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4002 /* Release a memory-mapped page */
4003 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4004 ApndFile *p = (ApndFile *)pFile;
4005 pFile = ORIGFILE(pFile);
4006 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4010 ** Check to see if the file is an ordinary SQLite database file.
4012 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4015 static const char aSqliteHdr[] = "SQLite format 3";
4016 if( sz<512 ) return 0;
4017 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
4019 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
4023 ** Try to read the append-mark off the end of a file. Return the
4024 ** start of the appended database if the append-mark is present. If
4025 ** there is no append-mark, return -1;
4027 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4029 sqlite3_int64 iMark;
4030 unsigned char a[APND_MARK_SIZE];
4032 if( sz<=APND_MARK_SIZE ) return -1;
4033 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4035 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4036 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
4038 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
4044 ** Open an apnd file handle.
4046 static int apndOpen(
4049 sqlite3_file *pFile,
4054 sqlite3_file *pSubFile;
4055 sqlite3_vfs *pSubVfs;
4058 pSubVfs = ORIGVFS(pVfs);
4059 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4060 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4062 p = (ApndFile*)pFile;
4063 memset(p, 0, sizeof(*p));
4064 pSubFile = ORIGFILE(pFile);
4065 pFile->pMethods = &apnd_io_methods;
4066 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4067 if( rc ) goto apnd_open_done;
4068 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4070 pSubFile->pMethods->xClose(pSubFile);
4071 goto apnd_open_done;
4073 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4074 memmove(pFile, pSubFile, pSubVfs->szOsFile);
4078 p->iPgOne = apndReadMark(sz, pFile);
4082 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4083 pSubFile->pMethods->xClose(pSubFile);
4084 rc = SQLITE_CANTOPEN;
4086 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4088 if( rc ) pFile->pMethods = 0;
4093 ** All other VFS methods are pass-thrus.
4095 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4096 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4098 static int apndAccess(
4104 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4106 static int apndFullPathname(
4112 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4114 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4115 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4117 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4118 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4120 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4121 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4123 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4124 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4126 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4127 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4129 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4130 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4132 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4133 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4135 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4136 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4138 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4139 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4141 static int apndSetSystemCall(
4144 sqlite3_syscall_ptr pCall
4146 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4148 static sqlite3_syscall_ptr apndGetSystemCall(
4152 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4154 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4155 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4163 ** This routine is called when the extension is loaded.
4164 ** Register the new VFS.
4166 int sqlite3_appendvfs_init(
4169 const sqlite3_api_routines *pApi
4173 SQLITE_EXTENSION_INIT2(pApi);
4176 pOrig = sqlite3_vfs_find(0);
4177 apnd_vfs.iVersion = pOrig->iVersion;
4178 apnd_vfs.pAppData = pOrig;
4179 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4180 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4181 #ifdef APPENDVFS_TEST
4182 if( rc==SQLITE_OK ){
4183 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4186 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4190 /************************* End ../ext/misc/appendvfs.c ********************/
4191 /************************* Begin ../ext/misc/memtrace.c ******************/
4195 ** The author disclaims copyright to this source code. In place of
4196 ** a legal notice, here is a blessing:
4198 ** May you do good and not evil.
4199 ** May you find forgiveness for yourself and forgive others.
4200 ** May you share freely, never taking more than you give.
4202 *************************************************************************
4204 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4205 ** mechanism to add a tracing layer on top of SQLite. If this extension
4206 ** is registered prior to sqlite3_initialize(), it will cause all memory
4207 ** allocation activities to be logged on standard output, or to some other
4208 ** FILE specified by the initializer.
4210 ** This file needs to be compiled into the application that uses it.
4212 ** This extension is used to implement the --memtrace option of the
4213 ** command-line shell.
4219 /* The original memory allocation routines */
4220 static sqlite3_mem_methods memtraceBase;
4221 static FILE *memtraceOut;
4223 /* Methods that trace memory allocations */
4224 static void *memtraceMalloc(int n){
4226 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4227 memtraceBase.xRoundup(n));
4229 return memtraceBase.xMalloc(n);
4231 static void memtraceFree(void *p){
4234 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4236 memtraceBase.xFree(p);
4238 static void *memtraceRealloc(void *p, int n){
4239 if( p==0 ) return memtraceMalloc(n);
4245 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4246 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4248 return memtraceBase.xRealloc(p, n);
4250 static int memtraceSize(void *p){
4251 return memtraceBase.xSize(p);
4253 static int memtraceRoundup(int n){
4254 return memtraceBase.xRoundup(n);
4256 static int memtraceInit(void *p){
4257 return memtraceBase.xInit(p);
4259 static void memtraceShutdown(void *p){
4260 memtraceBase.xShutdown(p);
4263 /* The substitute memory allocator */
4264 static sqlite3_mem_methods ersaztMethods = {
4275 /* Begin tracing memory allocations to out. */
4276 int sqlite3MemTraceActivate(FILE *out){
4278 if( memtraceBase.xMalloc==0 ){
4279 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4280 if( rc==SQLITE_OK ){
4281 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4288 /* Deactivate memory tracing */
4289 int sqlite3MemTraceDeactivate(void){
4291 if( memtraceBase.xMalloc!=0 ){
4292 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4293 if( rc==SQLITE_OK ){
4294 memset(&memtraceBase, 0, sizeof(memtraceBase));
4301 /************************* End ../ext/misc/memtrace.c ********************/
4302 /************************* Begin ../ext/misc/uint.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 SQLite extension implements the UINT collating sequence.
4317 ** UINT works like BINARY for text, except that embedded strings
4318 ** of digits compare in numeric order.
4320 ** * Leading zeros are handled properly, in the sense that
4321 ** they do not mess of the maginitude comparison of embedded
4322 ** strings of digits. "x00123y" is equal to "x123y".
4324 ** * Only unsigned integers are recognized. Plus and minus
4325 ** signs are ignored. Decimal points and exponential notation
4328 ** * Embedded integers can be of arbitrary length. Comparison
4329 ** is *not* limited integers that can be expressed as a
4330 ** 64-bit machine integer.
4332 /* #include "sqlite3ext.h" */
4333 SQLITE_EXTENSION_INIT1
4339 ** Compare text in lexicographic order, except strings of digits
4340 ** compare in numeric order.
4342 static int uintCollFunc(
4344 int nKey1, const void *pKey1,
4345 int nKey2, const void *pKey2
4347 const unsigned char *zA = (const unsigned char*)pKey1;
4348 const unsigned char *zB = (const unsigned char*)pKey2;
4351 while( i<nKey1 && j<nKey2 ){
4353 if( isdigit(zA[i]) ){
4355 if( !isdigit(zB[j]) ) return x;
4356 while( i<nKey1 && zA[i]=='0' ){ i++; }
4357 while( j<nKey2 && zB[j]=='0' ){ j++; }
4359 while( i+k<nKey1 && isdigit(zA[i+k])
4360 && j+k<nKey2 && isdigit(zB[j+k]) ){
4363 if( i+k<nKey1 && isdigit(zA[i+k]) ){
4365 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4368 x = memcmp(zA+i, zB+j, k);
4380 return (nKey1 - i) - (nKey2 - j);
4386 int sqlite3_uint_init(
4389 const sqlite3_api_routines *pApi
4391 SQLITE_EXTENSION_INIT2(pApi);
4392 (void)pzErrMsg; /* Unused parameter */
4393 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4396 /************************* End ../ext/misc/uint.c ********************/
4397 /************************* Begin ../ext/misc/decimal.c ******************/
4401 ** The author disclaims copyright to this source code. In place of
4402 ** a legal notice, here is a blessing:
4404 ** May you do good and not evil.
4405 ** May you find forgiveness for yourself and forgive others.
4406 ** May you share freely, never taking more than you give.
4408 ******************************************************************************
4410 ** Routines to implement arbitrary-precision decimal math.
4412 ** The focus here is on simplicity and correctness, not performance.
4414 /* #include "sqlite3ext.h" */
4415 SQLITE_EXTENSION_INIT1
4421 /* Mark a function parameter as unused, to suppress nuisance compiler
4423 #ifndef UNUSED_PARAMETER
4424 # define UNUSED_PARAMETER(X) (void)(X)
4428 /* A decimal object */
4429 typedef struct Decimal Decimal;
4431 char sign; /* 0 for positive, 1 for negative */
4432 char oom; /* True if an OOM is encountered */
4433 char isNull; /* True if holds a NULL rather than a number */
4434 char isInit; /* True upon initialization */
4435 int nDigit; /* Total number of digits */
4436 int nFrac; /* Number of digits to the right of the decimal point */
4437 signed char *a; /* Array of digits. Most significant first. */
4441 ** Release memory held by a Decimal, but do not free the object itself.
4443 static void decimal_clear(Decimal *p){
4448 ** Destroy a Decimal object
4450 static void decimal_free(Decimal *p){
4458 ** Allocate a new Decimal object. Initialize it to the number given
4459 ** by the input string.
4461 static Decimal *decimal_new(
4462 sqlite3_context *pCtx,
4465 const unsigned char *zAlt
4469 const unsigned char *zIn;
4471 p = sqlite3_malloc( sizeof(*p) );
4472 if( p==0 ) goto new_no_mem;
4483 if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4488 n = sqlite3_value_bytes(pIn);
4489 zIn = sqlite3_value_text(pIn);
4491 p->a = sqlite3_malloc64( n+1 );
4492 if( p->a==0 ) goto new_no_mem;
4493 for(i=0; isspace(zIn[i]); i++){}
4497 }else if( zIn[i]=='+' ){
4500 while( i<n && zIn[i]=='0' ) i++;
4503 if( c>='0' && c<='9' ){
4504 p->a[p->nDigit++] = c - '0';
4506 p->nFrac = p->nDigit + 1;
4507 }else if( c=='e' || c=='E' ){
4514 }else if( zIn[j]=='+' ){
4517 while( j<n && iExp<1000000 ){
4518 if( zIn[j]>='0' && zIn[j]<='9' ){
4519 iExp = iExp*10 + zIn[j] - '0';
4523 if( neg ) iExp = -iExp;
4529 p->nFrac = p->nDigit - (p->nFrac - 1);
4533 if( iExp<=p->nFrac ){
4542 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4543 if( p->a==0 ) goto new_no_mem;
4544 memset(p->a+p->nDigit, 0, iExp);
4550 nExtra = p->nDigit - p->nFrac - 1;
4557 p->nFrac = p->nDigit - 1;
4561 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4562 if( p->a==0 ) goto new_no_mem;
4563 memmove(p->a+iExp, p->a, p->nDigit);
4564 memset(p->a, 0, iExp);
4572 if( pCtx ) sqlite3_result_error_nomem(pCtx);
4578 ** Make the given Decimal the result.
4580 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4584 if( p==0 || p->oom ){
4585 sqlite3_result_error_nomem(pCtx);
4589 sqlite3_result_null(pCtx);
4592 z = sqlite3_malloc( p->nDigit+4 );
4594 sqlite3_result_error_nomem(pCtx);
4598 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4605 n = p->nDigit - p->nFrac;
4610 while( n>1 && p->a[j]==0 ){
4615 z[i++] = p->a[j] + '0';
4622 z[i++] = p->a[j] + '0';
4624 }while( j<p->nDigit );
4627 sqlite3_result_text(pCtx, z, i, sqlite3_free);
4631 ** SQL Function: decimal(X)
4633 ** Convert input X into decimal and then back into text
4635 static void decimalFunc(
4636 sqlite3_context *context,
4638 sqlite3_value **argv
4640 Decimal *p = decimal_new(context, argv[0], 0, 0);
4641 UNUSED_PARAMETER(argc);
4642 decimal_result(context, p);
4647 ** Compare to Decimal objects. Return negative, 0, or positive if the
4648 ** first object is less than, equal to, or greater than the second.
4650 ** Preconditions for this routine:
4657 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4658 int nASig, nBSig, rc, n;
4659 if( pA->sign!=pB->sign ){
4660 return pA->sign ? -1 : +1;
4663 const Decimal *pTemp = pA;
4667 nASig = pA->nDigit - pA->nFrac;
4668 nBSig = pB->nDigit - pB->nFrac;
4670 return nASig - nBSig;
4673 if( n>pB->nDigit ) n = pB->nDigit;
4674 rc = memcmp(pA->a, pB->a, n);
4676 rc = pA->nDigit - pB->nDigit;
4682 ** SQL Function: decimal_cmp(X, Y)
4684 ** Return negative, zero, or positive if X is less then, equal to, or
4687 static void decimalCmpFunc(
4688 sqlite3_context *context,
4690 sqlite3_value **argv
4692 Decimal *pA = 0, *pB = 0;
4695 UNUSED_PARAMETER(argc);
4696 pA = decimal_new(context, argv[0], 0, 0);
4697 if( pA==0 || pA->isNull ) goto cmp_done;
4698 pB = decimal_new(context, argv[1], 0, 0);
4699 if( pB==0 || pB->isNull ) goto cmp_done;
4700 rc = decimal_cmp(pA, pB);
4702 else if( rc>0 ) rc = +1;
4703 sqlite3_result_int(context, rc);
4710 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4711 ** digits to the right of the decimal point.
4713 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4717 nAddFrac = nFrac - p->nFrac;
4718 nAddSig = (nDigit - p->nDigit) - nAddFrac;
4719 if( nAddFrac==0 && nAddSig==0 ) return;
4720 p->a = sqlite3_realloc64(p->a, nDigit+1);
4726 memmove(p->a+nAddSig, p->a, p->nDigit);
4727 memset(p->a, 0, nAddSig);
4728 p->nDigit += nAddSig;
4731 memset(p->a+p->nDigit, 0, nAddFrac);
4732 p->nDigit += nAddFrac;
4733 p->nFrac += nAddFrac;
4738 ** Add the value pB into pA.
4740 ** Both pA and pB might become denormalized by this routine.
4742 static void decimal_add(Decimal *pA, Decimal *pB){
4743 int nSig, nFrac, nDigit;
4748 if( pA->oom || pB==0 || pB->oom ){
4752 if( pA->isNull || pB->isNull ){
4756 nSig = pA->nDigit - pA->nFrac;
4757 if( nSig && pA->a[0]==0 ) nSig--;
4758 if( nSig<pB->nDigit-pB->nFrac ){
4759 nSig = pB->nDigit - pB->nFrac;
4762 if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4763 nDigit = nSig + nFrac + 1;
4764 decimal_expand(pA, nDigit, nFrac);
4765 decimal_expand(pB, nDigit, nFrac);
4766 if( pA->oom || pB->oom ){
4769 if( pA->sign==pB->sign ){
4771 for(i=nDigit-1; i>=0; i--){
4772 int x = pA->a[i] + pB->a[i] + carry;
4782 signed char *aA, *aB;
4784 rc = memcmp(pA->a, pB->a, nDigit);
4788 pA->sign = !pA->sign;
4793 for(i=nDigit-1; i>=0; i--){
4794 int x = aA[i] - aB[i] - borrow;
4808 ** Compare text in decimal order.
4810 static int decimalCollFunc(
4812 int nKey1, const void *pKey1,
4813 int nKey2, const void *pKey2
4815 const unsigned char *zA = (const unsigned char*)pKey1;
4816 const unsigned char *zB = (const unsigned char*)pKey2;
4817 Decimal *pA = decimal_new(0, 0, nKey1, zA);
4818 Decimal *pB = decimal_new(0, 0, nKey2, zB);
4820 UNUSED_PARAMETER(notUsed);
4821 if( pA==0 || pB==0 ){
4824 rc = decimal_cmp(pA, pB);
4833 ** SQL Function: decimal_add(X, Y)
4834 ** decimal_sub(X, Y)
4836 ** Return the sum or difference of X and Y.
4838 static void decimalAddFunc(
4839 sqlite3_context *context,
4841 sqlite3_value **argv
4843 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4844 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4845 UNUSED_PARAMETER(argc);
4846 decimal_add(pA, pB);
4847 decimal_result(context, pA);
4851 static void decimalSubFunc(
4852 sqlite3_context *context,
4854 sqlite3_value **argv
4856 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4857 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4858 UNUSED_PARAMETER(argc);
4860 pB->sign = !pB->sign;
4861 decimal_add(pA, pB);
4862 decimal_result(context, pA);
4867 /* Aggregate funcion: decimal_sum(X)
4869 ** Works like sum() except that it uses decimal arithmetic for unlimited
4872 static void decimalSumStep(
4873 sqlite3_context *context,
4875 sqlite3_value **argv
4879 UNUSED_PARAMETER(argc);
4880 p = sqlite3_aggregate_context(context, sizeof(*p));
4884 p->a = sqlite3_malloc(2);
4893 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4894 pArg = decimal_new(context, argv[0], 0, 0);
4895 decimal_add(p, pArg);
4898 static void decimalSumInverse(
4899 sqlite3_context *context,
4901 sqlite3_value **argv
4905 UNUSED_PARAMETER(argc);
4906 p = sqlite3_aggregate_context(context, sizeof(*p));
4908 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4909 pArg = decimal_new(context, argv[0], 0, 0);
4910 if( pArg ) pArg->sign = !pArg->sign;
4911 decimal_add(p, pArg);
4914 static void decimalSumValue(sqlite3_context *context){
4915 Decimal *p = sqlite3_aggregate_context(context, 0);
4917 decimal_result(context, p);
4919 static void decimalSumFinalize(sqlite3_context *context){
4920 Decimal *p = sqlite3_aggregate_context(context, 0);
4922 decimal_result(context, p);
4927 ** SQL Function: decimal_mul(X, Y)
4929 ** Return the product of X and Y.
4931 ** All significant digits after the decimal point are retained.
4932 ** Trailing zeros after the decimal point are omitted as long as
4933 ** the number of digits after the decimal point is no less than
4934 ** either the number of digits in either input.
4936 static void decimalMulFunc(
4937 sqlite3_context *context,
4939 sqlite3_value **argv
4941 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4942 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4943 signed char *acc = 0;
4946 UNUSED_PARAMETER(argc);
4947 if( pA==0 || pA->oom || pA->isNull
4948 || pB==0 || pB->oom || pB->isNull
4952 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4954 sqlite3_result_error_nomem(context);
4957 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
4958 minFrac = pA->nFrac;
4959 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
4960 for(i=pA->nDigit-1; i>=0; i--){
4961 signed char f = pA->a[i];
4963 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
4964 x = acc[k] + f*pB->a[j] + carry;
4972 sqlite3_free(pA->a);
4975 pA->nDigit += pB->nDigit + 2;
4976 pA->nFrac += pB->nFrac;
4977 pA->sign ^= pB->sign;
4978 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
4982 decimal_result(context, pA);
4993 int sqlite3_decimal_init(
4996 const sqlite3_api_routines *pApi
4999 static const struct {
5000 const char *zFuncName;
5002 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5004 { "decimal", 1, decimalFunc },
5005 { "decimal_cmp", 2, decimalCmpFunc },
5006 { "decimal_add", 2, decimalAddFunc },
5007 { "decimal_sub", 2, decimalSubFunc },
5008 { "decimal_mul", 2, decimalMulFunc },
5011 (void)pzErrMsg; /* Unused parameter */
5013 SQLITE_EXTENSION_INIT2(pApi);
5015 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5016 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5017 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5018 0, aFunc[i].xFunc, 0, 0);
5020 if( rc==SQLITE_OK ){
5021 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5022 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5023 decimalSumStep, decimalSumFinalize,
5024 decimalSumValue, decimalSumInverse, 0);
5026 if( rc==SQLITE_OK ){
5027 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5028 0, decimalCollFunc);
5033 /************************* End ../ext/misc/decimal.c ********************/
5034 /************************* Begin ../ext/misc/ieee754.c ******************/
5038 ** The author disclaims copyright to this source code. In place of
5039 ** a legal notice, here is a blessing:
5041 ** May you do good and not evil.
5042 ** May you find forgiveness for yourself and forgive others.
5043 ** May you share freely, never taking more than you give.
5045 ******************************************************************************
5047 ** This SQLite extension implements functions for the exact display
5048 ** and input of IEEE754 Binary64 floating-point numbers.
5053 ** In the first form, the value X should be a floating-point number.
5054 ** The function will return a string of the form 'ieee754(Y,Z)' where
5055 ** Y and Z are integers such that X==Y*pow(2,Z).
5057 ** In the second form, Y and Z are integers which are the mantissa and
5058 ** base-2 exponent of a new floating point number. The function returns
5059 ** a floating-point value equal to Y*pow(2,Z).
5063 ** ieee754(2.0) -> 'ieee754(2,0)'
5064 ** ieee754(45.25) -> 'ieee754(181,-2)'
5065 ** ieee754(2, 0) -> 2.0
5066 ** ieee754(181, -2) -> 45.25
5068 ** Two additional functions break apart the one-argument ieee754()
5069 ** result into separate integer values:
5071 ** ieee754_mantissa(45.25) -> 181
5072 ** ieee754_exponent(45.25) -> -2
5074 ** These functions convert binary64 numbers into blobs and back again.
5076 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5077 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5079 ** In all single-argument functions, if the argument is an 8-byte blob
5080 ** then that blob is interpreted as a big-endian binary64 value.
5083 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5084 ** -----------------------------------------------
5086 ** This extension in combination with the separate 'decimal' extension
5087 ** can be used to compute the exact decimal representation of binary64
5088 ** values. To begin, first compute a table of exponent values:
5090 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5091 ** WITH RECURSIVE c(x,v) AS (
5094 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5095 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5096 ** WITH RECURSIVE c(x,v) AS (
5099 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5100 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5102 ** Then, to compute the exact decimal representation of a floating
5103 ** point value (the value 47.49 is used in the example) do:
5105 ** WITH c(n) AS (VALUES(47.49))
5106 ** ---------------^^^^^---- Replace with whatever you want
5107 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5108 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5110 ** Here is a query to show various boundry values for the binary64
5113 ** WITH c(name,bin) AS (VALUES
5114 ** ('minimum positive value', x'0000000000000001'),
5115 ** ('maximum subnormal value', x'000fffffffffffff'),
5116 ** ('mininum positive nornal value', x'0010000000000000'),
5117 ** ('maximum value', x'7fefffffffffffff'))
5118 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5119 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5122 /* #include "sqlite3ext.h" */
5123 SQLITE_EXTENSION_INIT1
5127 /* Mark a function parameter as unused, to suppress nuisance compiler
5129 #ifndef UNUSED_PARAMETER
5130 # define UNUSED_PARAMETER(X) (void)(X)
5134 ** Implementation of the ieee754() function
5136 static void ieee754func(
5137 sqlite3_context *context,
5139 sqlite3_value **argv
5147 assert( sizeof(m)==sizeof(r) );
5148 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5149 && sqlite3_value_bytes(argv[0])==sizeof(r)
5151 const unsigned char *x = sqlite3_value_blob(argv[0]);
5153 sqlite3_uint64 v = 0;
5154 for(i=0; i<sizeof(r); i++){
5157 memcpy(&r, &v, sizeof(r));
5159 r = sqlite3_value_double(argv[0]);
5167 memcpy(&a,&r,sizeof(a));
5173 m = a & ((((sqlite3_int64)1)<<52)-1);
5177 m |= ((sqlite3_int64)1)<<52;
5179 while( e<1075 && m>0 && (m&1)==0 ){
5185 switch( *(int*)sqlite3_user_data(context) ){
5187 sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5189 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5192 sqlite3_result_int64(context, m);
5195 sqlite3_result_int(context, e-1075);
5199 sqlite3_int64 m, e, a;
5202 m = sqlite3_value_int64(argv[0]);
5203 e = sqlite3_value_int64(argv[1]);
5208 }else if( m==0 && e>-1000 && e<1000 ){
5209 sqlite3_result_double(context, 0.0);
5212 while( (m>>32)&0xffe00000 ){
5216 while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5225 }else if( e>0x7ff ){
5228 a = m & ((((sqlite3_int64)1)<<52)-1);
5230 if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5231 memcpy(&r, &a, sizeof(r));
5232 sqlite3_result_double(context, r);
5237 ** Functions to convert between blobs and floats.
5239 static void ieee754func_from_blob(
5240 sqlite3_context *context,
5242 sqlite3_value **argv
5244 UNUSED_PARAMETER(argc);
5245 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5246 && sqlite3_value_bytes(argv[0])==sizeof(double)
5249 const unsigned char *x = sqlite3_value_blob(argv[0]);
5251 sqlite3_uint64 v = 0;
5252 for(i=0; i<sizeof(r); i++){
5255 memcpy(&r, &v, sizeof(r));
5256 sqlite3_result_double(context, r);
5259 static void ieee754func_to_blob(
5260 sqlite3_context *context,
5262 sqlite3_value **argv
5264 UNUSED_PARAMETER(argc);
5265 if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5266 || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5268 double r = sqlite3_value_double(argv[0]);
5270 unsigned char a[sizeof(r)];
5272 memcpy(&v, &r, sizeof(r));
5273 for(i=1; i<=sizeof(r); i++){
5274 a[sizeof(r)-i] = v&0xff;
5277 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5285 int sqlite3_ieee_init(
5288 const sqlite3_api_routines *pApi
5290 static const struct {
5294 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5296 { "ieee754", 1, 0, ieee754func },
5297 { "ieee754", 2, 0, ieee754func },
5298 { "ieee754_mantissa", 1, 1, ieee754func },
5299 { "ieee754_exponent", 1, 2, ieee754func },
5300 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5301 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5306 SQLITE_EXTENSION_INIT2(pApi);
5307 (void)pzErrMsg; /* Unused parameter */
5308 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5309 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5310 SQLITE_UTF8|SQLITE_INNOCUOUS,
5311 (void*)&aFunc[i].iAux,
5312 aFunc[i].xFunc, 0, 0);
5317 /************************* End ../ext/misc/ieee754.c ********************/
5318 /************************* Begin ../ext/misc/series.c ******************/
5322 ** The author disclaims copyright to this source code. In place of
5323 ** a legal notice, here is a blessing:
5325 ** May you do good and not evil.
5326 ** May you find forgiveness for yourself and forgive others.
5327 ** May you share freely, never taking more than you give.
5329 *************************************************************************
5331 ** This file demonstrates how to create a table-valued-function using
5332 ** a virtual table. This demo implements the generate_series() function
5333 ** which gives similar results to the eponymous function in PostgreSQL.
5336 ** SELECT * FROM generate_series(0,100,5);
5338 ** The query above returns integers from 0 through 100 counting by steps
5341 ** SELECT * FROM generate_series(0,100);
5343 ** Integers from 0 through 100 with a step size of 1.
5345 ** SELECT * FROM generate_series(20) LIMIT 10;
5347 ** Integers 20 through 29.
5351 ** The generate_series "function" is really a virtual table with the
5352 ** following schema:
5354 ** CREATE TABLE generate_series(
5361 ** Function arguments in queries against this virtual table are translated
5362 ** into equality constraints against successive hidden columns. In other
5363 ** words, the following pairs of queries are equivalent to each other:
5365 ** SELECT * FROM generate_series(0,100,5);
5366 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5368 ** SELECT * FROM generate_series(0,100);
5369 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5371 ** SELECT * FROM generate_series(20) LIMIT 10;
5372 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5374 ** The generate_series virtual table implementation leaves the xCreate method
5375 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5376 ** TABLE command with "generate_series" as the USING argument. Instead, there
5377 ** is a single generate_series virtual table that is always available without
5378 ** having to be created first.
5380 ** The xBestIndex method looks for equality constraints against the hidden
5381 ** start, stop, and step columns, and if present, it uses those constraints
5382 ** to bound the sequence of generated values. If the equality constraints
5383 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5384 ** xBestIndex returns a small cost when both start and stop are available,
5385 ** and a very large cost if either start or stop are unavailable. This
5386 ** encourages the query planner to order joins such that the bounds of the
5387 ** series are well-defined.
5389 /* #include "sqlite3ext.h" */
5390 SQLITE_EXTENSION_INIT1
5394 #ifndef SQLITE_OMIT_VIRTUALTABLE
5397 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5398 ** serve as the underlying representation of a cursor that scans
5399 ** over rows of the result
5401 typedef struct series_cursor series_cursor;
5402 struct series_cursor {
5403 sqlite3_vtab_cursor base; /* Base class - must be first */
5404 int isDesc; /* True to count down rather than up */
5405 sqlite3_int64 iRowid; /* The rowid */
5406 sqlite3_int64 iValue; /* Current value ("value") */
5407 sqlite3_int64 mnValue; /* Mimimum value ("start") */
5408 sqlite3_int64 mxValue; /* Maximum value ("stop") */
5409 sqlite3_int64 iStep; /* Increment ("step") */
5413 ** The seriesConnect() method is invoked to create a new
5414 ** series_vtab that describes the generate_series virtual table.
5416 ** Think of this routine as the constructor for series_vtab objects.
5418 ** All this routine needs to do is:
5420 ** (1) Allocate the series_vtab object and initialize all fields.
5422 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5423 ** result set of queries against generate_series will look like.
5425 static int seriesConnect(
5428 int argcUnused, const char *const*argvUnused,
5429 sqlite3_vtab **ppVtab,
5435 /* Column numbers */
5436 #define SERIES_COLUMN_VALUE 0
5437 #define SERIES_COLUMN_START 1
5438 #define SERIES_COLUMN_STOP 2
5439 #define SERIES_COLUMN_STEP 3
5445 rc = sqlite3_declare_vtab(db,
5446 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5447 if( rc==SQLITE_OK ){
5448 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5449 if( pNew==0 ) return SQLITE_NOMEM;
5450 memset(pNew, 0, sizeof(*pNew));
5451 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5457 ** This method is the destructor for series_cursor objects.
5459 static int seriesDisconnect(sqlite3_vtab *pVtab){
5460 sqlite3_free(pVtab);
5465 ** Constructor for a new series_cursor object.
5467 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5468 series_cursor *pCur;
5470 pCur = sqlite3_malloc( sizeof(*pCur) );
5471 if( pCur==0 ) return SQLITE_NOMEM;
5472 memset(pCur, 0, sizeof(*pCur));
5473 *ppCursor = &pCur->base;
5478 ** Destructor for a series_cursor.
5480 static int seriesClose(sqlite3_vtab_cursor *cur){
5487 ** Advance a series_cursor to its next row of output.
5489 static int seriesNext(sqlite3_vtab_cursor *cur){
5490 series_cursor *pCur = (series_cursor*)cur;
5492 pCur->iValue -= pCur->iStep;
5494 pCur->iValue += pCur->iStep;
5501 ** Return values of columns for the row at which the series_cursor
5502 ** is currently pointing.
5504 static int seriesColumn(
5505 sqlite3_vtab_cursor *cur, /* The cursor */
5506 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5507 int i /* Which column to return */
5509 series_cursor *pCur = (series_cursor*)cur;
5510 sqlite3_int64 x = 0;
5512 case SERIES_COLUMN_START: x = pCur->mnValue; break;
5513 case SERIES_COLUMN_STOP: x = pCur->mxValue; break;
5514 case SERIES_COLUMN_STEP: x = pCur->iStep; break;
5515 default: x = pCur->iValue; break;
5517 sqlite3_result_int64(ctx, x);
5522 ** Return the rowid for the current row. In this implementation, the
5523 ** first row returned is assigned rowid value 1, and each subsequent
5524 ** row a value 1 more than that of the previous.
5526 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5527 series_cursor *pCur = (series_cursor*)cur;
5528 *pRowid = pCur->iRowid;
5533 ** Return TRUE if the cursor has been moved off of the last
5536 static int seriesEof(sqlite3_vtab_cursor *cur){
5537 series_cursor *pCur = (series_cursor*)cur;
5539 return pCur->iValue < pCur->mnValue;
5541 return pCur->iValue > pCur->mxValue;
5545 /* True to cause run-time checking of the start=, stop=, and/or step=
5546 ** parameters. The only reason to do this is for testing the
5547 ** constraint checking logic for virtual tables in the SQLite core.
5549 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5550 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5554 ** This method is called to "rewind" the series_cursor object back
5555 ** to the first row of output. This method is always called at least
5556 ** once prior to any call to seriesColumn() or seriesRowid() or
5559 ** The query plan selected by seriesBestIndex is passed in the idxNum
5560 ** parameter. (idxStr is not used in this implementation.) idxNum
5561 ** is a bitmask showing which constraints are available:
5567 ** Also, if bit 8 is set, that means that the series should be output
5568 ** in descending order rather than in ascending order.
5570 ** This routine should initialize the cursor and position it so that it
5571 ** is pointing at the first row, or pointing off the end of the table
5572 ** (so that seriesEof() will return true) if the table is empty.
5574 static int seriesFilter(
5575 sqlite3_vtab_cursor *pVtabCursor,
5576 int idxNum, const char *idxStrUnused,
5577 int argc, sqlite3_value **argv
5579 series_cursor *pCur = (series_cursor *)pVtabCursor;
5583 pCur->mnValue = sqlite3_value_int64(argv[i++]);
5588 pCur->mxValue = sqlite3_value_int64(argv[i++]);
5590 pCur->mxValue = 0xffffffff;
5593 pCur->iStep = sqlite3_value_int64(argv[i++]);
5594 if( pCur->iStep<1 ) pCur->iStep = 1;
5598 for(i=0; i<argc; i++){
5599 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5600 /* If any of the constraints have a NULL value, then return no rows.
5601 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5609 pCur->iValue = pCur->mxValue;
5610 if( pCur->iStep>0 ){
5611 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5615 pCur->iValue = pCur->mnValue;
5622 ** SQLite will invoke this method one or more times while planning a query
5623 ** that uses the generate_series virtual table. This routine needs to create
5624 ** a query plan for each invocation and compute an estimated cost for that
5627 ** In this implementation idxNum is used to represent the
5628 ** query plan. idxStr is unused.
5630 ** The query plan is represented by bits in idxNum:
5632 ** (1) start = $value -- constraint exists
5633 ** (2) stop = $value -- constraint exists
5634 ** (4) step = $value -- constraint exists
5635 ** (8) output in descending order
5637 static int seriesBestIndex(
5638 sqlite3_vtab *tabUnused,
5639 sqlite3_index_info *pIdxInfo
5641 int i, j; /* Loop over constraints */
5642 int idxNum = 0; /* The query plan bitmask */
5643 int unusableMask = 0; /* Mask of unusable constraints */
5644 int nArg = 0; /* Number of arguments that seriesFilter() expects */
5645 int aIdx[3]; /* Constraints on start, stop, and step */
5646 const struct sqlite3_index_constraint *pConstraint;
5648 /* This implementation assumes that the start, stop, and step columns
5649 ** are the last three columns in the virtual table. */
5650 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5651 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5653 aIdx[0] = aIdx[1] = aIdx[2] = -1;
5654 pConstraint = pIdxInfo->aConstraint;
5655 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5656 int iCol; /* 0 for start, 1 for stop, 2 for step */
5657 int iMask; /* bitmask for those column */
5658 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5659 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5660 assert( iCol>=0 && iCol<=2 );
5662 if( pConstraint->usable==0 ){
5663 unusableMask |= iMask;
5665 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5671 if( (j = aIdx[i])>=0 ){
5672 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5673 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5676 if( (unusableMask & ~idxNum)!=0 ){
5677 /* The start, stop, and step columns are inputs. Therefore if there
5678 ** are unusable constraints on any of start, stop, or step then
5679 ** this plan is unusable */
5680 return SQLITE_CONSTRAINT;
5682 if( (idxNum & 3)==3 ){
5683 /* Both start= and stop= boundaries are available. This is the
5684 ** the preferred case */
5685 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5686 pIdxInfo->estimatedRows = 1000;
5687 if( pIdxInfo->nOrderBy==1 ){
5688 if( pIdxInfo->aOrderBy[0].desc ) idxNum |= 8;
5689 pIdxInfo->orderByConsumed = 1;
5692 /* If either boundary is missing, we have to generate a huge span
5693 ** of numbers. Make this case very expensive so that the query
5694 ** planner will work hard to avoid it. */
5695 pIdxInfo->estimatedRows = 2147483647;
5697 pIdxInfo->idxNum = idxNum;
5702 ** This following structure defines all the methods for the
5703 ** generate_series virtual table.
5705 static sqlite3_module seriesModule = {
5708 seriesConnect, /* xConnect */
5709 seriesBestIndex, /* xBestIndex */
5710 seriesDisconnect, /* xDisconnect */
5712 seriesOpen, /* xOpen - open a cursor */
5713 seriesClose, /* xClose - close a cursor */
5714 seriesFilter, /* xFilter - configure scan constraints */
5715 seriesNext, /* xNext - advance a cursor */
5716 seriesEof, /* xEof - check for end of scan */
5717 seriesColumn, /* xColumn - read data */
5718 seriesRowid, /* xRowid - read data */
5724 0, /* xFindMethod */
5728 0, /* xRollbackTo */
5732 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5737 int sqlite3_series_init(
5740 const sqlite3_api_routines *pApi
5743 SQLITE_EXTENSION_INIT2(pApi);
5744 #ifndef SQLITE_OMIT_VIRTUALTABLE
5745 if( sqlite3_libversion_number()<3008012 ){
5746 *pzErrMsg = sqlite3_mprintf(
5747 "generate_series() requires SQLite 3.8.12 or later");
5748 return SQLITE_ERROR;
5750 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5755 /************************* End ../ext/misc/series.c ********************/
5756 #ifdef SQLITE_HAVE_ZLIB
5757 /************************* Begin ../ext/misc/zipfile.c ******************/
5761 ** The author disclaims copyright to this source code. In place of
5762 ** a legal notice, here is a blessing:
5764 ** May you do good and not evil.
5765 ** May you find forgiveness for yourself and forgive others.
5766 ** May you share freely, never taking more than you give.
5768 ******************************************************************************
5770 ** This file implements a virtual table for reading and writing ZIP archive
5775 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
5777 ** Current limitations:
5779 ** * No support for encryption
5780 ** * No support for ZIP archives spanning multiple files
5781 ** * No support for zip64 extensions
5782 ** * Only the "inflate/deflate" (zlib) compression method is supported
5784 /* #include "sqlite3ext.h" */
5785 SQLITE_EXTENSION_INIT1
5792 #ifndef SQLITE_OMIT_VIRTUALTABLE
5794 #ifndef SQLITE_AMALGAMATION
5796 /* typedef sqlite3_int64 i64; */
5797 /* typedef unsigned char u8; */
5798 typedef unsigned short u16;
5799 typedef unsigned long u32;
5800 #define MIN(a,b) ((a)<(b) ? (a) : (b))
5802 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
5803 # define ALWAYS(X) (1)
5804 # define NEVER(X) (0)
5805 #elif !defined(NDEBUG)
5806 # define ALWAYS(X) ((X)?1:(assert(0),0))
5807 # define NEVER(X) ((X)?(assert(0),1):0)
5809 # define ALWAYS(X) (X)
5810 # define NEVER(X) (X)
5813 #endif /* SQLITE_AMALGAMATION */
5816 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
5818 ** In some ways it would be better to obtain these values from system
5819 ** header files. But, the dependency is undesirable and (a) these
5820 ** have been stable for decades, (b) the values are part of POSIX and
5821 ** are also made explicit in [man stat], and (c) are part of the
5822 ** file format for zip archives.
5825 # define S_IFDIR 0040000
5828 # define S_IFREG 0100000
5831 # define S_IFLNK 0120000
5834 static const char ZIPFILE_SCHEMA[] =
5836 "name PRIMARY KEY," /* 0: Name of file in zip archive */
5837 "mode," /* 1: POSIX mode for file */
5838 "mtime," /* 2: Last modification time (secs since 1970)*/
5839 "sz," /* 3: Size of object */
5840 "rawdata," /* 4: Raw data */
5841 "data," /* 5: Uncompressed data */
5842 "method," /* 6: Compression method (integer) */
5843 "z HIDDEN" /* 7: Name of zip file */
5846 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
5847 #define ZIPFILE_BUFFER_SIZE (64*1024)
5851 ** Magic numbers used to read and write zip files.
5853 ** ZIPFILE_NEWENTRY_MADEBY:
5854 ** Use this value for the "version-made-by" field in new zip file
5855 ** entries. The upper byte indicates "unix", and the lower byte
5856 ** indicates that the zip file matches pkzip specification 3.0.
5857 ** This is what info-zip seems to do.
5859 ** ZIPFILE_NEWENTRY_REQUIRED:
5860 ** Value for "version-required-to-extract" field of new entries.
5861 ** Version 2.0 is required to support folders and deflate compression.
5863 ** ZIPFILE_NEWENTRY_FLAGS:
5864 ** Value for "general-purpose-bit-flags" field of new entries. Bit
5865 ** 11 means "utf-8 filename and comment".
5867 ** ZIPFILE_SIGNATURE_CDS:
5868 ** First 4 bytes of a valid CDS record.
5870 ** ZIPFILE_SIGNATURE_LFH:
5871 ** First 4 bytes of a valid LFH record.
5873 ** ZIPFILE_SIGNATURE_EOCD
5874 ** First 4 bytes of a valid EOCD record.
5876 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
5877 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
5878 #define ZIPFILE_NEWENTRY_REQUIRED 20
5879 #define ZIPFILE_NEWENTRY_FLAGS 0x800
5880 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
5881 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
5882 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
5885 ** The sizes of the fixed-size part of each of the three main data
5886 ** structures in a zip archive.
5888 #define ZIPFILE_LFH_FIXED_SZ 30
5889 #define ZIPFILE_EOCD_FIXED_SZ 22
5890 #define ZIPFILE_CDS_FIXED_SZ 46
5893 *** 4.3.16 End of central directory record:
5895 *** end of central dir signature 4 bytes (0x06054b50)
5896 *** number of this disk 2 bytes
5897 *** number of the disk with the
5898 *** start of the central directory 2 bytes
5899 *** total number of entries in the
5900 *** central directory on this disk 2 bytes
5901 *** total number of entries in
5902 *** the central directory 2 bytes
5903 *** size of the central directory 4 bytes
5904 *** offset of start of central
5905 *** directory with respect to
5906 *** the starting disk number 4 bytes
5907 *** .ZIP file comment length 2 bytes
5908 *** .ZIP file comment (variable size)
5910 typedef struct ZipfileEOCD ZipfileEOCD;
5911 struct ZipfileEOCD {
5921 *** 4.3.12 Central directory structure:
5925 *** central file header signature 4 bytes (0x02014b50)
5926 *** version made by 2 bytes
5927 *** version needed to extract 2 bytes
5928 *** general purpose bit flag 2 bytes
5929 *** compression method 2 bytes
5930 *** last mod file time 2 bytes
5931 *** last mod file date 2 bytes
5933 *** compressed size 4 bytes
5934 *** uncompressed size 4 bytes
5935 *** file name length 2 bytes
5936 *** extra field length 2 bytes
5937 *** file comment length 2 bytes
5938 *** disk number start 2 bytes
5939 *** internal file attributes 2 bytes
5940 *** external file attributes 4 bytes
5941 *** relative offset of local header 4 bytes
5943 typedef struct ZipfileCDS ZipfileCDS;
5946 u16 iVersionExtract;
5961 char *zFile; /* Filename (sqlite3_malloc()) */
5965 *** 4.3.7 Local file header:
5967 *** local file header signature 4 bytes (0x04034b50)
5968 *** version needed to extract 2 bytes
5969 *** general purpose bit flag 2 bytes
5970 *** compression method 2 bytes
5971 *** last mod file time 2 bytes
5972 *** last mod file date 2 bytes
5974 *** compressed size 4 bytes
5975 *** uncompressed size 4 bytes
5976 *** file name length 2 bytes
5977 *** extra field length 2 bytes
5980 typedef struct ZipfileLFH ZipfileLFH;
5982 u16 iVersionExtract;
5994 typedef struct ZipfileEntry ZipfileEntry;
5995 struct ZipfileEntry {
5996 ZipfileCDS cds; /* Parsed CDS record */
5997 u32 mUnixTime; /* Modification time, in UNIX format */
5998 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
5999 i64 iDataOff; /* Offset to data in file (if aData==0) */
6000 u8 *aData; /* cds.szCompressed bytes of compressed data */
6001 ZipfileEntry *pNext; /* Next element in in-memory CDS */
6005 ** Cursor type for zipfile tables.
6007 typedef struct ZipfileCsr ZipfileCsr;
6009 sqlite3_vtab_cursor base; /* Base class - must be first */
6010 i64 iId; /* Cursor ID */
6011 u8 bEof; /* True when at EOF */
6012 u8 bNoop; /* If next xNext() call is no-op */
6014 /* Used outside of write transactions */
6015 FILE *pFile; /* Zip file */
6016 i64 iNextOff; /* Offset of next record in central directory */
6017 ZipfileEOCD eocd; /* Parse of central directory record */
6019 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
6020 ZipfileEntry *pCurrent; /* Current entry */
6021 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
6024 typedef struct ZipfileTab ZipfileTab;
6026 sqlite3_vtab base; /* Base class - must be first */
6027 char *zFile; /* Zip file this table accesses (may be NULL) */
6028 sqlite3 *db; /* Host database connection */
6029 u8 *aBuffer; /* Temporary buffer used for various tasks */
6031 ZipfileCsr *pCsrList; /* List of cursors */
6034 /* The following are used by write transactions only */
6035 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6036 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
6037 FILE *pWriteFd; /* File handle open on zip archive */
6038 i64 szCurrent; /* Current size of zip archive */
6039 i64 szOrig; /* Size of archive at start of transaction */
6043 ** Set the error message contained in context ctx to the results of
6044 ** vprintf(zFmt, ...).
6046 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6050 zMsg = sqlite3_vmprintf(zFmt, ap);
6051 sqlite3_result_error(ctx, zMsg, -1);
6057 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
6058 ** is not quoted, do nothing.
6060 static void zipfileDequote(char *zIn){
6062 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6065 if( q=='[' ) q = ']';
6066 while( ALWAYS(zIn[iIn]) ){
6067 char c = zIn[iIn++];
6068 if( c==q && zIn[iIn++]!=q ) break;
6076 ** Construct a new ZipfileTab virtual table object.
6078 ** argv[0] -> module name ("zipfile")
6079 ** argv[1] -> database name
6080 ** argv[2] -> table name
6081 ** argv[...] -> "column name" and other module argument fields.
6083 static int zipfileConnect(
6086 int argc, const char *const*argv,
6087 sqlite3_vtab **ppVtab,
6090 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
6092 const char *zFile = 0;
6093 ZipfileTab *pNew = 0;
6096 /* If the table name is not "zipfile", require that the argument be
6097 ** specified. This stops zipfile tables from being created as:
6099 ** CREATE VIRTUAL TABLE zzz USING zipfile();
6101 ** It does not prevent:
6103 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
6105 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
6106 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
6107 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
6108 return SQLITE_ERROR;
6113 nFile = (int)strlen(zFile)+1;
6116 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
6117 if( rc==SQLITE_OK ){
6118 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
6119 if( pNew==0 ) return SQLITE_NOMEM;
6120 memset(pNew, 0, nByte+nFile);
6122 pNew->aBuffer = (u8*)&pNew[1];
6124 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
6125 memcpy(pNew->zFile, zFile, nFile);
6126 zipfileDequote(pNew->zFile);
6129 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
6130 *ppVtab = (sqlite3_vtab*)pNew;
6135 ** Free the ZipfileEntry structure indicated by the only argument.
6137 static void zipfileEntryFree(ZipfileEntry *p){
6139 sqlite3_free(p->cds.zFile);
6145 ** Release resources that should be freed at the end of a write
6148 static void zipfileCleanupTransaction(ZipfileTab *pTab){
6149 ZipfileEntry *pEntry;
6150 ZipfileEntry *pNext;
6152 if( pTab->pWriteFd ){
6153 fclose(pTab->pWriteFd);
6156 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
6157 pNext = pEntry->pNext;
6158 zipfileEntryFree(pEntry);
6160 pTab->pFirstEntry = 0;
6161 pTab->pLastEntry = 0;
6162 pTab->szCurrent = 0;
6167 ** This method is the destructor for zipfile vtab objects.
6169 static int zipfileDisconnect(sqlite3_vtab *pVtab){
6170 zipfileCleanupTransaction((ZipfileTab*)pVtab);
6171 sqlite3_free(pVtab);
6176 ** Constructor for a new ZipfileCsr object.
6178 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
6179 ZipfileTab *pTab = (ZipfileTab*)p;
6181 pCsr = sqlite3_malloc(sizeof(*pCsr));
6182 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
6184 return SQLITE_NOMEM;
6186 memset(pCsr, 0, sizeof(*pCsr));
6187 pCsr->iId = ++pTab->iNextCsrid;
6188 pCsr->pCsrNext = pTab->pCsrList;
6189 pTab->pCsrList = pCsr;
6194 ** Reset a cursor back to the state it was in when first returned
6195 ** by zipfileOpen().
6197 static void zipfileResetCursor(ZipfileCsr *pCsr){
6199 ZipfileEntry *pNext;
6203 fclose(pCsr->pFile);
6205 zipfileEntryFree(pCsr->pCurrent);
6209 for(p=pCsr->pFreeEntry; p; p=pNext){
6211 zipfileEntryFree(p);
6216 ** Destructor for an ZipfileCsr.
6218 static int zipfileClose(sqlite3_vtab_cursor *cur){
6219 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6220 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
6222 zipfileResetCursor(pCsr);
6224 /* Remove this cursor from the ZipfileTab.pCsrList list. */
6225 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
6226 *pp = pCsr->pCsrNext;
6233 ** Set the error message for the virtual table associated with cursor
6234 ** pCsr to the results of vprintf(zFmt, ...).
6236 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
6239 sqlite3_free(pTab->base.zErrMsg);
6240 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
6243 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
6246 sqlite3_free(pCsr->base.pVtab->zErrMsg);
6247 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
6252 ** Read nRead bytes of data from offset iOff of file pFile into buffer
6253 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
6256 ** If an error does occur, output variable (*pzErrmsg) may be set to point
6257 ** to an English language error message. It is the responsibility of the
6258 ** caller to eventually free this buffer using
6261 static int zipfileReadData(
6262 FILE *pFile, /* Read from this file */
6263 u8 *aRead, /* Read into this buffer */
6264 int nRead, /* Number of bytes to read */
6265 i64 iOff, /* Offset to read from */
6266 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
6269 fseek(pFile, (long)iOff, SEEK_SET);
6270 n = fread(aRead, 1, nRead, pFile);
6271 if( (int)n!=nRead ){
6272 *pzErrmsg = sqlite3_mprintf("error in fread()");
6273 return SQLITE_ERROR;
6278 static int zipfileAppendData(
6285 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
6286 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
6287 if( (int)n!=nWrite ){
6288 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
6289 return SQLITE_ERROR;
6291 pTab->szCurrent += nWrite;
6297 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
6299 static u16 zipfileGetU16(const u8 *aBuf){
6300 return (aBuf[1] << 8) + aBuf[0];
6304 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
6306 static u32 zipfileGetU32(const u8 *aBuf){
6307 return ((u32)(aBuf[3]) << 24)
6308 + ((u32)(aBuf[2]) << 16)
6309 + ((u32)(aBuf[1]) << 8)
6310 + ((u32)(aBuf[0]) << 0);
6314 ** Write a 16-bit little endiate integer into buffer aBuf.
6316 static void zipfilePutU16(u8 *aBuf, u16 val){
6317 aBuf[0] = val & 0xFF;
6318 aBuf[1] = (val>>8) & 0xFF;
6322 ** Write a 32-bit little endiate integer into buffer aBuf.
6324 static void zipfilePutU32(u8 *aBuf, u32 val){
6325 aBuf[0] = val & 0xFF;
6326 aBuf[1] = (val>>8) & 0xFF;
6327 aBuf[2] = (val>>16) & 0xFF;
6328 aBuf[3] = (val>>24) & 0xFF;
6331 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
6332 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
6334 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
6335 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
6338 ** Magic numbers used to read CDS records.
6340 #define ZIPFILE_CDS_NFILE_OFF 28
6341 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
6344 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
6345 ** if the record is not well-formed, or SQLITE_OK otherwise.
6347 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
6349 u32 sig = zipfileRead32(aRead);
6351 if( sig!=ZIPFILE_SIGNATURE_CDS ){
6354 pCDS->iVersionMadeBy = zipfileRead16(aRead);
6355 pCDS->iVersionExtract = zipfileRead16(aRead);
6356 pCDS->flags = zipfileRead16(aRead);
6357 pCDS->iCompression = zipfileRead16(aRead);
6358 pCDS->mTime = zipfileRead16(aRead);
6359 pCDS->mDate = zipfileRead16(aRead);
6360 pCDS->crc32 = zipfileRead32(aRead);
6361 pCDS->szCompressed = zipfileRead32(aRead);
6362 pCDS->szUncompressed = zipfileRead32(aRead);
6363 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6364 pCDS->nFile = zipfileRead16(aRead);
6365 pCDS->nExtra = zipfileRead16(aRead);
6366 pCDS->nComment = zipfileRead16(aRead);
6367 pCDS->iDiskStart = zipfileRead16(aRead);
6368 pCDS->iInternalAttr = zipfileRead16(aRead);
6369 pCDS->iExternalAttr = zipfileRead32(aRead);
6370 pCDS->iOffset = zipfileRead32(aRead);
6371 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
6378 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
6379 ** if the record is not well-formed, or SQLITE_OK otherwise.
6381 static int zipfileReadLFH(
6385 u8 *aRead = aBuffer;
6388 u32 sig = zipfileRead32(aRead);
6389 if( sig!=ZIPFILE_SIGNATURE_LFH ){
6392 pLFH->iVersionExtract = zipfileRead16(aRead);
6393 pLFH->flags = zipfileRead16(aRead);
6394 pLFH->iCompression = zipfileRead16(aRead);
6395 pLFH->mTime = zipfileRead16(aRead);
6396 pLFH->mDate = zipfileRead16(aRead);
6397 pLFH->crc32 = zipfileRead32(aRead);
6398 pLFH->szCompressed = zipfileRead32(aRead);
6399 pLFH->szUncompressed = zipfileRead32(aRead);
6400 pLFH->nFile = zipfileRead16(aRead);
6401 pLFH->nExtra = zipfileRead16(aRead);
6408 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
6409 ** Scan through this buffer to find an "extra-timestamp" field. If one
6410 ** exists, extract the 32-bit modification-timestamp from it and store
6411 ** the value in output parameter *pmTime.
6413 ** Zero is returned if no extra-timestamp record could be found (and so
6414 ** *pmTime is left unchanged), or non-zero otherwise.
6416 ** The general format of an extra field is:
6418 ** Header ID 2 bytes
6419 ** Data Size 2 bytes
6422 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
6425 u8 *pEnd = &aExtra[nExtra];
6428 u16 id = zipfileRead16(p);
6429 u16 nByte = zipfileRead16(p);
6432 case ZIPFILE_EXTRA_TIMESTAMP: {
6434 if( b & 0x01 ){ /* 0x01 -> modtime is present */
6435 *pmTime = zipfileGetU32(&p[1]);
6448 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
6449 ** fields of the CDS structure passed as the only argument to a 32-bit
6450 ** UNIX seconds-since-the-epoch timestamp. Return the result.
6452 ** "Standard" MS-DOS time format:
6454 ** File modification time:
6455 ** Bits 00-04: seconds divided by 2
6456 ** Bits 05-10: minute
6458 ** File modification date:
6460 ** Bits 05-08: month (1-12)
6461 ** Bits 09-15: years from 1980
6463 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
6465 static u32 zipfileMtime(ZipfileCDS *pCDS){
6466 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
6467 int M = ((pCDS->mDate >> 5) & 0x0F);
6468 int D = (pCDS->mDate & 0x1F);
6471 int sec = (pCDS->mTime & 0x1F)*2;
6472 int min = (pCDS->mTime >> 5) & 0x3F;
6473 int hr = (pCDS->mTime >> 11) & 0x1F;
6476 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
6478 /* Calculate the JD in seconds for noon on the day in question */
6483 JD = (i64)(24*60*60) * (
6484 (int)(365.25 * (Y + 4716))
6485 + (int)(30.6001 * (M + 1))
6489 /* Correct the JD for the time within the day */
6490 JD += (hr-12) * 3600 + min * 60 + sec;
6492 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
6493 return (u32)(JD - (i64)(24405875) * 24*60*6);
6497 ** The opposite of zipfileMtime(). This function populates the mTime and
6498 ** mDate fields of the CDS structure passed as the first argument according
6499 ** to the UNIX timestamp value passed as the second.
6501 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
6502 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
6503 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
6509 A = (int)((JD - 1867216.25)/36524.25);
6510 A = (int)(JD + 1 + A - (A/4));
6512 C = (int)((B - 122.1)/365.25);
6513 D = (36525*(C&32767))/100;
6514 E = (int)((B-D)/30.6001);
6516 day = B - D - (int)(30.6001*E);
6517 mon = (E<14 ? E-1 : E-13);
6518 yr = mon>2 ? C-4716 : C-4715;
6520 hr = (mUnixTime % (24*60*60)) / (60*60);
6521 min = (mUnixTime % (60*60)) / 60;
6522 sec = (mUnixTime % 60);
6525 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
6526 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
6528 pCds->mDate = pCds->mTime = 0;
6531 assert( mUnixTime<315507600
6532 || mUnixTime==zipfileMtime(pCds)
6533 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
6534 /* || (mUnixTime % 2) */
6539 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
6540 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
6541 ** then pFile is a file-handle open on a zip file. In either case, this
6542 ** function creates a ZipfileEntry object based on the zip archive entry
6543 ** for which the CDS record is at offset iOff.
6545 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
6546 ** the new object. Otherwise, an SQLite error code is returned and the
6547 ** final value of (*ppEntry) undefined.
6549 static int zipfileGetEntry(
6550 ZipfileTab *pTab, /* Store any error message here */
6551 const u8 *aBlob, /* Pointer to in-memory file image */
6552 int nBlob, /* Size of aBlob[] in bytes */
6553 FILE *pFile, /* If aBlob==0, read from this file */
6554 i64 iOff, /* Offset of CDS record */
6555 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
6558 char **pzErr = &pTab->base.zErrMsg;
6562 aRead = pTab->aBuffer;
6563 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
6565 aRead = (u8*)&aBlob[iOff];
6568 if( rc==SQLITE_OK ){
6569 sqlite3_int64 nAlloc;
6572 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
6573 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
6574 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
6576 nAlloc = sizeof(ZipfileEntry) + nExtra;
6578 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
6581 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
6585 memset(pNew, 0, sizeof(ZipfileEntry));
6586 rc = zipfileReadCDS(aRead, &pNew->cds);
6587 if( rc!=SQLITE_OK ){
6588 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
6589 }else if( aBlob==0 ){
6590 rc = zipfileReadData(
6591 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
6594 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
6598 if( rc==SQLITE_OK ){
6599 u32 *pt = &pNew->mUnixTime;
6600 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
6601 pNew->aExtra = (u8*)&pNew[1];
6602 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
6603 if( pNew->cds.zFile==0 ){
6605 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
6606 pNew->mUnixTime = zipfileMtime(&pNew->cds);
6610 if( rc==SQLITE_OK ){
6611 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
6614 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
6616 aRead = (u8*)&aBlob[pNew->cds.iOffset];
6619 rc = zipfileReadLFH(aRead, &lfh);
6620 if( rc==SQLITE_OK ){
6621 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
6622 pNew->iDataOff += lfh.nFile + lfh.nExtra;
6623 if( aBlob && pNew->cds.szCompressed ){
6624 pNew->aData = &pNew->aExtra[nExtra];
6625 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
6628 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
6629 (int)pNew->cds.iOffset
6634 if( rc!=SQLITE_OK ){
6635 zipfileEntryFree(pNew);
6645 ** Advance an ZipfileCsr to its next row of output.
6647 static int zipfileNext(sqlite3_vtab_cursor *cur){
6648 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6652 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
6653 zipfileEntryFree(pCsr->pCurrent);
6655 if( pCsr->iNextOff>=iEof ){
6658 ZipfileEntry *p = 0;
6659 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
6660 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
6661 if( rc==SQLITE_OK ){
6662 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
6663 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
6669 pCsr->pCurrent = pCsr->pCurrent->pNext;
6671 if( pCsr->pCurrent==0 ){
6680 static void zipfileFree(void *p) {
6685 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
6686 ** size is nOut bytes. This function uncompresses the data and sets the
6687 ** return value in context pCtx to the result (a blob).
6689 ** If an error occurs, an error code is left in pCtx instead.
6691 static void zipfileInflate(
6692 sqlite3_context *pCtx, /* Store result here */
6693 const u8 *aIn, /* Compressed data */
6694 int nIn, /* Size of buffer aIn[] in bytes */
6695 int nOut /* Expected output size */
6697 u8 *aRes = sqlite3_malloc(nOut);
6699 sqlite3_result_error_nomem(pCtx);
6703 memset(&str, 0, sizeof(str));
6705 str.next_in = (Byte*)aIn;
6707 str.next_out = (Byte*)aRes;
6708 str.avail_out = nOut;
6710 err = inflateInit2(&str, -15);
6712 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
6714 err = inflate(&str, Z_NO_FLUSH);
6715 if( err!=Z_STREAM_END ){
6716 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
6718 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
6728 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
6729 ** compresses it and sets (*ppOut) to point to a buffer containing the
6730 ** compressed data. The caller is responsible for eventually calling
6731 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
6732 ** is set to the size of buffer (*ppOut) in bytes.
6734 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
6735 ** code is returned and an error message left in virtual-table handle
6736 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
6739 static int zipfileDeflate(
6740 const u8 *aIn, int nIn, /* Input */
6741 u8 **ppOut, int *pnOut, /* Output */
6742 char **pzErr /* OUT: Error message */
6745 sqlite3_int64 nAlloc;
6749 memset(&str, 0, sizeof(str));
6750 str.next_in = (Bytef*)aIn;
6752 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
6754 nAlloc = deflateBound(&str, nIn);
6755 aOut = (u8*)sqlite3_malloc64(nAlloc);
6760 str.next_out = aOut;
6761 str.avail_out = nAlloc;
6762 res = deflate(&str, Z_FINISH);
6763 if( res==Z_STREAM_END ){
6765 *pnOut = (int)str.total_out;
6768 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
6779 ** Return values of columns for the row at which the series_cursor
6780 ** is currently pointing.
6782 static int zipfileColumn(
6783 sqlite3_vtab_cursor *cur, /* The cursor */
6784 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
6785 int i /* Which column to return */
6787 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6788 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
6792 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
6795 /* TODO: Whether or not the following is correct surely depends on
6796 ** the platform on which the archive was created. */
6797 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
6799 case 2: { /* mtime */
6800 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
6804 if( sqlite3_vtab_nochange(ctx)==0 ){
6805 sqlite3_result_int64(ctx, pCDS->szUncompressed);
6809 case 4: /* rawdata */
6810 if( sqlite3_vtab_nochange(ctx) ) break;
6811 case 5: { /* data */
6812 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
6813 int sz = pCDS->szCompressed;
6814 int szFinal = pCDS->szUncompressed;
6818 if( pCsr->pCurrent->aData ){
6819 aBuf = pCsr->pCurrent->aData;
6821 aBuf = aFree = sqlite3_malloc64(sz);
6825 FILE *pFile = pCsr->pFile;
6827 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
6829 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
6830 &pCsr->base.pVtab->zErrMsg
6834 if( rc==SQLITE_OK ){
6835 if( i==5 && pCDS->iCompression ){
6836 zipfileInflate(ctx, aBuf, sz, szFinal);
6838 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
6841 sqlite3_free(aFree);
6843 /* Figure out if this is a directory or a zero-sized file. Consider
6844 ** it to be a directory either if the mode suggests so, or if
6845 ** the final character in the name is '/'. */
6846 u32 mode = pCDS->iExternalAttr >> 16;
6847 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
6848 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
6854 case 6: /* method */
6855 sqlite3_result_int(ctx, pCDS->iCompression);
6859 sqlite3_result_int64(ctx, pCsr->iId);
6867 ** Return TRUE if the cursor is at EOF.
6869 static int zipfileEof(sqlite3_vtab_cursor *cur){
6870 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6875 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
6876 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
6877 ** is guaranteed to be a file-handle open on a zip file.
6879 ** This function attempts to locate the EOCD record within the zip archive
6880 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
6881 ** returned if successful. Otherwise, an SQLite error code is returned and
6882 ** an English language error message may be left in virtual-table pTab.
6884 static int zipfileReadEOCD(
6885 ZipfileTab *pTab, /* Return errors here */
6886 const u8 *aBlob, /* Pointer to in-memory file image */
6887 int nBlob, /* Size of aBlob[] in bytes */
6888 FILE *pFile, /* Read from this file if aBlob==0 */
6889 ZipfileEOCD *pEOCD /* Object to populate */
6891 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
6892 int nRead; /* Bytes to read from file */
6896 i64 iOff; /* Offset to read from */
6897 i64 szFile; /* Total size of file in bytes */
6898 fseek(pFile, 0, SEEK_END);
6899 szFile = (i64)ftell(pFile);
6901 memset(pEOCD, 0, sizeof(ZipfileEOCD));
6904 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
6905 iOff = szFile - nRead;
6906 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
6908 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
6909 aRead = (u8*)&aBlob[nBlob-nRead];
6912 if( rc==SQLITE_OK ){
6915 /* Scan backwards looking for the signature bytes */
6916 for(i=nRead-20; i>=0; i--){
6917 if( aRead[i]==0x50 && aRead[i+1]==0x4b
6918 && aRead[i+2]==0x05 && aRead[i+3]==0x06
6924 pTab->base.zErrMsg = sqlite3_mprintf(
6925 "cannot find end of central directory record"
6927 return SQLITE_ERROR;
6931 pEOCD->iDisk = zipfileRead16(aRead);
6932 pEOCD->iFirstDisk = zipfileRead16(aRead);
6933 pEOCD->nEntry = zipfileRead16(aRead);
6934 pEOCD->nEntryTotal = zipfileRead16(aRead);
6935 pEOCD->nSize = zipfileRead32(aRead);
6936 pEOCD->iOffset = zipfileRead32(aRead);
6943 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
6944 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
6945 ** to the end of the list. Otherwise, it is added to the list immediately
6946 ** before pBefore (which is guaranteed to be a part of said list).
6948 static void zipfileAddEntry(
6950 ZipfileEntry *pBefore,
6953 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
6954 assert( pNew->pNext==0 );
6956 if( pTab->pFirstEntry==0 ){
6957 pTab->pFirstEntry = pTab->pLastEntry = pNew;
6959 assert( pTab->pLastEntry->pNext==0 );
6960 pTab->pLastEntry->pNext = pNew;
6961 pTab->pLastEntry = pNew;
6965 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
6966 pNew->pNext = pBefore;
6971 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
6977 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
6978 iOff = eocd.iOffset;
6979 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
6980 ZipfileEntry *pNew = 0;
6981 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
6983 if( rc==SQLITE_OK ){
6984 zipfileAddEntry(pTab, 0, pNew);
6985 iOff += ZIPFILE_CDS_FIXED_SZ;
6986 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
6993 ** xFilter callback.
6995 static int zipfileFilter(
6996 sqlite3_vtab_cursor *cur,
6997 int idxNum, const char *idxStr,
6998 int argc, sqlite3_value **argv
7000 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
7001 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7002 const char *zFile = 0; /* Zip file to scan */
7003 int rc = SQLITE_OK; /* Return Code */
7004 int bInMemory = 0; /* True for an in-memory zipfile */
7006 zipfileResetCursor(pCsr);
7009 zFile = pTab->zFile;
7010 }else if( idxNum==0 ){
7011 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7012 return SQLITE_ERROR;
7013 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7014 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7015 int nBlob = sqlite3_value_bytes(argv[0]);
7016 assert( pTab->pFirstEntry==0 );
7017 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7018 pCsr->pFreeEntry = pTab->pFirstEntry;
7019 pTab->pFirstEntry = pTab->pLastEntry = 0;
7020 if( rc!=SQLITE_OK ) return rc;
7023 zFile = (const char*)sqlite3_value_text(argv[0]);
7026 if( 0==pTab->pWriteFd && 0==bInMemory ){
7027 pCsr->pFile = fopen(zFile, "rb");
7028 if( pCsr->pFile==0 ){
7029 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7032 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7033 if( rc==SQLITE_OK ){
7034 if( pCsr->eocd.nEntry==0 ){
7037 pCsr->iNextOff = pCsr->eocd.iOffset;
7038 rc = zipfileNext(cur);
7044 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7045 rc = zipfileNext(cur);
7052 ** xBestIndex callback.
7054 static int zipfileBestIndex(
7056 sqlite3_index_info *pIdxInfo
7062 for(i=0; i<pIdxInfo->nConstraint; i++){
7063 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7064 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7065 if( pCons->usable==0 ){
7067 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7071 pIdxInfo->estimatedCost = 1000.0;
7073 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7074 pIdxInfo->aConstraintUsage[idx].omit = 1;
7075 pIdxInfo->idxNum = 1;
7076 }else if( unusable ){
7077 return SQLITE_CONSTRAINT;
7082 static ZipfileEntry *zipfileNewEntry(const char *zPath){
7084 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7086 memset(pNew, 0, sizeof(ZipfileEntry));
7087 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7088 if( pNew->cds.zFile==0 ){
7096 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
7097 ZipfileCDS *pCds = &pEntry->cds;
7102 /* Write the LFH itself */
7103 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
7104 zipfileWrite16(a, pCds->iVersionExtract);
7105 zipfileWrite16(a, pCds->flags);
7106 zipfileWrite16(a, pCds->iCompression);
7107 zipfileWrite16(a, pCds->mTime);
7108 zipfileWrite16(a, pCds->mDate);
7109 zipfileWrite32(a, pCds->crc32);
7110 zipfileWrite32(a, pCds->szCompressed);
7111 zipfileWrite32(a, pCds->szUncompressed);
7112 zipfileWrite16(a, (u16)pCds->nFile);
7113 zipfileWrite16(a, pCds->nExtra);
7114 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
7116 /* Add the file name */
7117 memcpy(a, pCds->zFile, (int)pCds->nFile);
7118 a += (int)pCds->nFile;
7120 /* The "extra" data */
7121 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7122 zipfileWrite16(a, 5);
7124 zipfileWrite32(a, pEntry->mUnixTime);
7129 static int zipfileAppendEntry(
7131 ZipfileEntry *pEntry,
7135 u8 *aBuf = pTab->aBuffer;
7139 nBuf = zipfileSerializeLFH(pEntry, aBuf);
7140 rc = zipfileAppendData(pTab, aBuf, nBuf);
7141 if( rc==SQLITE_OK ){
7142 pEntry->iDataOff = pTab->szCurrent;
7143 rc = zipfileAppendData(pTab, pData, nData);
7149 static int zipfileGetMode(
7150 sqlite3_value *pVal,
7151 int bIsDir, /* If true, default to directory */
7152 u32 *pMode, /* OUT: Mode value */
7153 char **pzErr /* OUT: Error message */
7155 const char *z = (const char*)sqlite3_value_text(pVal);
7158 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
7159 }else if( z[0]>='0' && z[0]<='9' ){
7160 mode = (unsigned int)sqlite3_value_int(pVal);
7162 const char zTemplate[11] = "-rwxrwxrwx";
7164 if( strlen(z)!=10 ) goto parse_error;
7166 case '-': mode |= S_IFREG; break;
7167 case 'd': mode |= S_IFDIR; break;
7168 case 'l': mode |= S_IFLNK; break;
7169 default: goto parse_error;
7171 for(i=1; i<10; i++){
7172 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
7173 else if( z[i]!='-' ) goto parse_error;
7176 if( ((mode & S_IFDIR)==0)==bIsDir ){
7177 /* The "mode" attribute is a directory, but data has been specified.
7178 ** Or vice-versa - no data but "mode" is a file or symlink. */
7179 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
7180 return SQLITE_CONSTRAINT;
7186 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
7187 return SQLITE_ERROR;
7191 ** Both (const char*) arguments point to nul-terminated strings. Argument
7192 ** nB is the value of strlen(zB). This function returns 0 if the strings are
7193 ** identical, ignoring any trailing '/' character in either path. */
7194 static int zipfileComparePath(const char *zA, const char *zB, int nB){
7195 int nA = (int)strlen(zA);
7196 if( nA>0 && zA[nA-1]=='/' ) nA--;
7197 if( nB>0 && zB[nB-1]=='/' ) nB--;
7198 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
7202 static int zipfileBegin(sqlite3_vtab *pVtab){
7203 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7206 assert( pTab->pWriteFd==0 );
7207 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
7208 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
7209 return SQLITE_ERROR;
7212 /* Open a write fd on the file. Also load the entire central directory
7213 ** structure into memory. During the transaction any new file data is
7214 ** appended to the archive file, but the central directory is accumulated
7215 ** in main-memory until the transaction is committed. */
7216 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
7217 if( pTab->pWriteFd==0 ){
7218 pTab->base.zErrMsg = sqlite3_mprintf(
7219 "zipfile: failed to open file %s for writing", pTab->zFile
7223 fseek(pTab->pWriteFd, 0, SEEK_END);
7224 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
7225 rc = zipfileLoadDirectory(pTab, 0, 0);
7228 if( rc!=SQLITE_OK ){
7229 zipfileCleanupTransaction(pTab);
7236 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
7239 static u32 zipfileTime(void){
7240 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
7242 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
7244 pVfs->xCurrentTimeInt64(pVfs, &ms);
7245 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
7248 pVfs->xCurrentTime(pVfs, &day);
7249 ret = (u32)((day - 2440587.5) * 86400);
7255 ** Return a 32-bit timestamp in UNIX epoch format.
7257 ** If the value passed as the only argument is either NULL or an SQL NULL,
7258 ** return the current time. Otherwise, return the value stored in (*pVal)
7259 ** cast to a 32-bit unsigned integer.
7261 static u32 zipfileGetTime(sqlite3_value *pVal){
7262 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
7263 return zipfileTime();
7265 return (u32)sqlite3_value_int64(pVal);
7269 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
7270 ** linked list. Remove it from the list and free the object.
7272 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
7275 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
7277 zipfileEntryFree(pOld);
7284 static int zipfileUpdate(
7285 sqlite3_vtab *pVtab,
7287 sqlite3_value **apVal,
7288 sqlite_int64 *pRowid
7290 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7291 int rc = SQLITE_OK; /* Return Code */
7292 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
7294 u32 mode = 0; /* Mode for new entry */
7295 u32 mTime = 0; /* Modification time for new entry */
7296 i64 sz = 0; /* Uncompressed size */
7297 const char *zPath = 0; /* Path for new entry */
7298 int nPath = 0; /* strlen(zPath) */
7299 const u8 *pData = 0; /* Pointer to buffer containing content */
7300 int nData = 0; /* Size of pData buffer in bytes */
7301 int iMethod = 0; /* Compression method for new entry */
7302 u8 *pFree = 0; /* Free this */
7303 char *zFree = 0; /* Also free this */
7304 ZipfileEntry *pOld = 0;
7305 ZipfileEntry *pOld2 = 0;
7306 int bUpdate = 0; /* True for an update that modifies "name" */
7310 if( pTab->pWriteFd==0 ){
7311 rc = zipfileBegin(pVtab);
7312 if( rc!=SQLITE_OK ) return rc;
7315 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
7316 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
7317 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
7318 int nDelete = (int)strlen(zDelete);
7320 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
7321 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
7325 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
7326 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
7329 assert( pOld->pNext );
7334 /* Check that "sz" and "rawdata" are both NULL: */
7335 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
7336 zipfileTableErr(pTab, "sz must be NULL");
7337 rc = SQLITE_CONSTRAINT;
7339 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
7340 zipfileTableErr(pTab, "rawdata must be NULL");
7341 rc = SQLITE_CONSTRAINT;
7344 if( rc==SQLITE_OK ){
7345 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
7346 /* data=NULL. A directory */
7349 /* Value specified for "data", and possibly "method". This must be
7350 ** a regular file or a symlink. */
7351 const u8 *aIn = sqlite3_value_blob(apVal[7]);
7352 int nIn = sqlite3_value_bytes(apVal[7]);
7353 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
7355 iMethod = sqlite3_value_int(apVal[8]);
7359 if( iMethod!=0 && iMethod!=8 ){
7360 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
7361 rc = SQLITE_CONSTRAINT;
7363 if( bAuto || iMethod ){
7365 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
7366 if( rc==SQLITE_OK ){
7367 if( iMethod || nCmp<nIn ){
7374 iCrc32 = crc32(0, aIn, nIn);
7379 if( rc==SQLITE_OK ){
7380 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
7383 if( rc==SQLITE_OK ){
7384 zPath = (const char*)sqlite3_value_text(apVal[2]);
7385 if( zPath==0 ) zPath = "";
7386 nPath = (int)strlen(zPath);
7387 mTime = zipfileGetTime(apVal[4]);
7390 if( rc==SQLITE_OK && bIsDir ){
7391 /* For a directory, check that the last character in the path is a
7392 ** '/'. This appears to be required for compatibility with info-zip
7393 ** (the unzip command on unix). It does not create directories
7395 if( nPath<=0 || zPath[nPath-1]!='/' ){
7396 zFree = sqlite3_mprintf("%s/", zPath);
7397 zPath = (const char*)zFree;
7402 nPath = (int)strlen(zPath);
7407 /* Check that we're not inserting a duplicate entry -OR- updating an
7408 ** entry with a path, thereby making it into a duplicate. */
7409 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
7411 for(p=pTab->pFirstEntry; p; p=p->pNext){
7412 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
7413 switch( sqlite3_vtab_on_conflict(pTab->db) ){
7414 case SQLITE_IGNORE: {
7415 goto zipfile_update_done;
7417 case SQLITE_REPLACE: {
7422 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
7423 rc = SQLITE_CONSTRAINT;
7432 if( rc==SQLITE_OK ){
7433 /* Create the new CDS record. */
7434 pNew = zipfileNewEntry(zPath);
7438 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7439 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7440 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7441 pNew->cds.iCompression = (u16)iMethod;
7442 zipfileMtimeToDos(&pNew->cds, mTime);
7443 pNew->cds.crc32 = iCrc32;
7444 pNew->cds.szCompressed = nData;
7445 pNew->cds.szUncompressed = (u32)sz;
7446 pNew->cds.iExternalAttr = (mode<<16);
7447 pNew->cds.iOffset = (u32)pTab->szCurrent;
7448 pNew->cds.nFile = (u16)nPath;
7449 pNew->mUnixTime = (u32)mTime;
7450 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
7451 zipfileAddEntry(pTab, pOld, pNew);
7456 if( rc==SQLITE_OK && (pOld || pOld2) ){
7458 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7459 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
7460 pCsr->pCurrent = pCsr->pCurrent->pNext;
7465 zipfileRemoveEntryFromList(pTab, pOld);
7466 zipfileRemoveEntryFromList(pTab, pOld2);
7469 zipfile_update_done:
7470 sqlite3_free(pFree);
7471 sqlite3_free(zFree);
7475 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
7477 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
7478 zipfileWrite16(a, p->iDisk);
7479 zipfileWrite16(a, p->iFirstDisk);
7480 zipfileWrite16(a, p->nEntry);
7481 zipfileWrite16(a, p->nEntryTotal);
7482 zipfileWrite32(a, p->nSize);
7483 zipfileWrite32(a, p->iOffset);
7484 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
7489 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
7490 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
7491 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
7492 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
7496 ** Serialize the CDS structure into buffer aBuf[]. Return the number
7497 ** of bytes written.
7499 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
7501 ZipfileCDS *pCDS = &pEntry->cds;
7503 if( pEntry->aExtra==0 ){
7507 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
7508 zipfileWrite16(a, pCDS->iVersionMadeBy);
7509 zipfileWrite16(a, pCDS->iVersionExtract);
7510 zipfileWrite16(a, pCDS->flags);
7511 zipfileWrite16(a, pCDS->iCompression);
7512 zipfileWrite16(a, pCDS->mTime);
7513 zipfileWrite16(a, pCDS->mDate);
7514 zipfileWrite32(a, pCDS->crc32);
7515 zipfileWrite32(a, pCDS->szCompressed);
7516 zipfileWrite32(a, pCDS->szUncompressed);
7517 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7518 zipfileWrite16(a, pCDS->nFile);
7519 zipfileWrite16(a, pCDS->nExtra);
7520 zipfileWrite16(a, pCDS->nComment);
7521 zipfileWrite16(a, pCDS->iDiskStart);
7522 zipfileWrite16(a, pCDS->iInternalAttr);
7523 zipfileWrite32(a, pCDS->iExternalAttr);
7524 zipfileWrite32(a, pCDS->iOffset);
7526 memcpy(a, pCDS->zFile, pCDS->nFile);
7529 if( pEntry->aExtra ){
7530 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
7531 memcpy(a, pEntry->aExtra, n);
7534 assert( pCDS->nExtra==9 );
7535 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7536 zipfileWrite16(a, 5);
7538 zipfileWrite32(a, pEntry->mUnixTime);
7544 static int zipfileCommit(sqlite3_vtab *pVtab){
7545 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7547 if( pTab->pWriteFd ){
7548 i64 iOffset = pTab->szCurrent;
7553 /* Write out all entries */
7554 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
7555 int n = zipfileSerializeCDS(p, pTab->aBuffer);
7556 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
7560 /* Write out the EOCD record */
7562 eocd.iFirstDisk = 0;
7563 eocd.nEntry = (u16)nEntry;
7564 eocd.nEntryTotal = (u16)nEntry;
7565 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
7566 eocd.iOffset = (u32)iOffset;
7567 rc = zipfileAppendEOCD(pTab, &eocd);
7569 zipfileCleanupTransaction(pTab);
7574 static int zipfileRollback(sqlite3_vtab *pVtab){
7575 return zipfileCommit(pVtab);
7578 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
7580 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7581 if( iId==pCsr->iId ) break;
7586 static void zipfileFunctionCds(
7587 sqlite3_context *context,
7589 sqlite3_value **argv
7592 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
7595 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
7597 ZipfileCDS *p = &pCsr->pCurrent->cds;
7598 char *zRes = sqlite3_mprintf("{"
7599 "\"version-made-by\" : %u, "
7600 "\"version-to-extract\" : %u, "
7602 "\"compression\" : %u, "
7606 "\"compressed-size\" : %u, "
7607 "\"uncompressed-size\" : %u, "
7608 "\"file-name-length\" : %u, "
7609 "\"extra-field-length\" : %u, "
7610 "\"file-comment-length\" : %u, "
7611 "\"disk-number-start\" : %u, "
7612 "\"internal-attr\" : %u, "
7613 "\"external-attr\" : %u, "
7614 "\"offset\" : %u }",
7615 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
7616 (u32)p->flags, (u32)p->iCompression,
7617 (u32)p->mTime, (u32)p->mDate,
7618 (u32)p->crc32, (u32)p->szCompressed,
7619 (u32)p->szUncompressed, (u32)p->nFile,
7620 (u32)p->nExtra, (u32)p->nComment,
7621 (u32)p->iDiskStart, (u32)p->iInternalAttr,
7622 (u32)p->iExternalAttr, (u32)p->iOffset
7626 sqlite3_result_error_nomem(context);
7628 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
7635 ** xFindFunction method.
7637 static int zipfileFindFunction(
7638 sqlite3_vtab *pVtab, /* Virtual table handle */
7639 int nArg, /* Number of SQL function arguments */
7640 const char *zName, /* Name of SQL function */
7641 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
7642 void **ppArg /* OUT: User data for *pxFunc */
7644 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
7645 *pxFunc = zipfileFunctionCds;
7646 *ppArg = (void*)pVtab;
7652 typedef struct ZipfileBuffer ZipfileBuffer;
7653 struct ZipfileBuffer {
7654 u8 *a; /* Pointer to buffer */
7655 int n; /* Size of buffer in bytes */
7656 int nAlloc; /* Byte allocated at a[] */
7659 typedef struct ZipfileCtx ZipfileCtx;
7666 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
7667 if( pBuf->n+nByte>pBuf->nAlloc ){
7669 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
7670 int nReq = pBuf->n + nByte;
7672 while( nNew<nReq ) nNew = nNew*2;
7673 aNew = sqlite3_realloc64(pBuf->a, nNew);
7674 if( aNew==0 ) return SQLITE_NOMEM;
7676 pBuf->nAlloc = (int)nNew;
7682 ** xStep() callback for the zipfile() aggregate. This can be called in
7683 ** any of the following ways:
7685 ** SELECT zipfile(name,data) ...
7686 ** SELECT zipfile(name,mode,mtime,data) ...
7687 ** SELECT zipfile(name,mode,mtime,data,method) ...
7689 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
7690 ZipfileCtx *p; /* Aggregate function context */
7691 ZipfileEntry e; /* New entry to add to zip archive */
7693 sqlite3_value *pName = 0;
7694 sqlite3_value *pMode = 0;
7695 sqlite3_value *pMtime = 0;
7696 sqlite3_value *pData = 0;
7697 sqlite3_value *pMethod = 0;
7704 int iMethod = -1; /* Compression method to use (0 or 8) */
7706 const u8 *aData = 0; /* Possibly compressed data for new entry */
7707 int nData = 0; /* Size of aData[] in bytes */
7708 int szUncompressed = 0; /* Size of data before compression */
7709 u8 *aFree = 0; /* Free this before returning */
7710 u32 iCrc32 = 0; /* crc32 of uncompressed data */
7712 char *zName = 0; /* Path (name) of new entry */
7713 int nName = 0; /* Size of zName in bytes */
7714 char *zFree = 0; /* Free this before returning */
7717 memset(&e, 0, sizeof(e));
7718 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7721 /* Martial the arguments into stack variables */
7722 if( nVal!=2 && nVal!=4 && nVal!=5 ){
7723 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
7725 goto zipfile_step_out;
7739 /* Check that the 'name' parameter looks ok. */
7740 zName = (char*)sqlite3_value_text(pName);
7741 nName = sqlite3_value_bytes(pName);
7743 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
7745 goto zipfile_step_out;
7748 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
7749 ** deflate compression) or NULL (choose automatically). */
7750 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
7751 iMethod = (int)sqlite3_value_int64(pMethod);
7752 if( iMethod!=0 && iMethod!=8 ){
7753 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
7755 goto zipfile_step_out;
7759 /* Now inspect the data. If this is NULL, then the new entry must be a
7760 ** directory. Otherwise, figure out whether or not the data should
7761 ** be deflated or simply stored in the zip archive. */
7762 if( sqlite3_value_type(pData)==SQLITE_NULL ){
7766 aData = sqlite3_value_blob(pData);
7767 szUncompressed = nData = sqlite3_value_bytes(pData);
7768 iCrc32 = crc32(0, aData, nData);
7769 if( iMethod<0 || iMethod==8 ){
7771 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
7772 if( rc!=SQLITE_OK ){
7773 goto zipfile_step_out;
7775 if( iMethod==8 || nOut<nData ){
7785 /* Decode the "mode" argument. */
7786 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
7787 if( rc ) goto zipfile_step_out;
7789 /* Decode the "mtime" argument. */
7790 e.mUnixTime = zipfileGetTime(pMtime);
7792 /* If this is a directory entry, ensure that there is exactly one '/'
7793 ** at the end of the path. Or, if this is not a directory and the path
7794 ** ends in '/' it is an error. */
7796 if( nName>0 && zName[nName-1]=='/' ){
7797 zErr = sqlite3_mprintf("non-directory name must not end with /");
7799 goto zipfile_step_out;
7802 if( nName==0 || zName[nName-1]!='/' ){
7803 zName = zFree = sqlite3_mprintf("%s/", zName);
7806 goto zipfile_step_out;
7808 nName = (int)strlen(zName);
7810 while( nName>1 && zName[nName-2]=='/' ) nName--;
7814 /* Assemble the ZipfileEntry object for the new zip archive entry */
7815 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7816 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7817 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7818 e.cds.iCompression = (u16)iMethod;
7819 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
7820 e.cds.crc32 = iCrc32;
7821 e.cds.szCompressed = nData;
7822 e.cds.szUncompressed = szUncompressed;
7823 e.cds.iExternalAttr = (mode<<16);
7824 e.cds.iOffset = p->body.n;
7825 e.cds.nFile = (u16)nName;
7826 e.cds.zFile = zName;
7828 /* Append the LFH to the body of the new archive */
7829 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
7830 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
7831 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
7833 /* Append the data to the body of the new archive */
7835 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
7836 memcpy(&p->body.a[p->body.n], aData, nData);
7840 /* Append the CDS record to the directory of the new archive */
7841 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
7842 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
7843 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
7845 /* Increment the count of entries in the archive */
7849 sqlite3_free(aFree);
7850 sqlite3_free(zFree);
7853 sqlite3_result_error(pCtx, zErr, -1);
7855 sqlite3_result_error_code(pCtx, rc);
7862 ** xFinalize() callback for zipfile aggregate function.
7864 void zipfileFinal(sqlite3_context *pCtx){
7870 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7873 memset(&eocd, 0, sizeof(eocd));
7874 eocd.nEntry = (u16)p->nEntry;
7875 eocd.nEntryTotal = (u16)p->nEntry;
7876 eocd.nSize = p->cds.n;
7877 eocd.iOffset = p->body.n;
7879 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
7880 aZip = (u8*)sqlite3_malloc64(nZip);
7882 sqlite3_result_error_nomem(pCtx);
7884 memcpy(aZip, p->body.a, p->body.n);
7885 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
7886 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
7887 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
7891 sqlite3_free(p->body.a);
7892 sqlite3_free(p->cds.a);
7897 ** Register the "zipfile" virtual table.
7899 static int zipfileRegister(sqlite3 *db){
7900 static sqlite3_module zipfileModule = {
7902 zipfileConnect, /* xCreate */
7903 zipfileConnect, /* xConnect */
7904 zipfileBestIndex, /* xBestIndex */
7905 zipfileDisconnect, /* xDisconnect */
7906 zipfileDisconnect, /* xDestroy */
7907 zipfileOpen, /* xOpen - open a cursor */
7908 zipfileClose, /* xClose - close a cursor */
7909 zipfileFilter, /* xFilter - configure scan constraints */
7910 zipfileNext, /* xNext - advance a cursor */
7911 zipfileEof, /* xEof - check for end of scan */
7912 zipfileColumn, /* xColumn - read data */
7913 0, /* xRowid - read data */
7914 zipfileUpdate, /* xUpdate */
7915 zipfileBegin, /* xBegin */
7917 zipfileCommit, /* xCommit */
7918 zipfileRollback, /* xRollback */
7919 zipfileFindFunction, /* xFindMethod */
7923 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
7924 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
7925 if( rc==SQLITE_OK ){
7926 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
7927 zipfileStep, zipfileFinal
7932 #else /* SQLITE_OMIT_VIRTUALTABLE */
7933 # define zipfileRegister(x) SQLITE_OK
7939 int sqlite3_zipfile_init(
7942 const sqlite3_api_routines *pApi
7944 SQLITE_EXTENSION_INIT2(pApi);
7945 (void)pzErrMsg; /* Unused parameter */
7946 return zipfileRegister(db);
7949 /************************* End ../ext/misc/zipfile.c ********************/
7950 /************************* Begin ../ext/misc/sqlar.c ******************/
7954 ** The author disclaims copyright to this source code. In place of
7955 ** a legal notice, here is a blessing:
7957 ** May you do good and not evil.
7958 ** May you find forgiveness for yourself and forgive others.
7959 ** May you share freely, never taking more than you give.
7961 ******************************************************************************
7963 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
7964 ** for working with sqlar archives and used by the shell tool's built-in
7967 /* #include "sqlite3ext.h" */
7968 SQLITE_EXTENSION_INIT1
7973 ** Implementation of the "sqlar_compress(X)" SQL function.
7975 ** If the type of X is SQLITE_BLOB, and compressing that blob using
7976 ** zlib utility function compress() yields a smaller blob, return the
7977 ** compressed blob. Otherwise, return a copy of X.
7979 ** SQLar uses the "zlib format" for compressed content. The zlib format
7980 ** contains a two-byte identification header and a four-byte checksum at
7981 ** the end. This is different from ZIP which uses the raw deflate format.
7983 ** Future enhancements to SQLar might add support for new compression formats.
7984 ** If so, those new formats will be identified by alternative headers in the
7987 static void sqlarCompressFunc(
7988 sqlite3_context *context,
7990 sqlite3_value **argv
7993 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7994 const Bytef *pData = sqlite3_value_blob(argv[0]);
7995 uLong nData = sqlite3_value_bytes(argv[0]);
7996 uLongf nOut = compressBound(nData);
7999 pOut = (Bytef*)sqlite3_malloc(nOut);
8001 sqlite3_result_error_nomem(context);
8004 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8005 sqlite3_result_error(context, "error in compress()", -1);
8006 }else if( nOut<nData ){
8007 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8009 sqlite3_result_value(context, argv[0]);
8014 sqlite3_result_value(context, argv[0]);
8019 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8021 ** Parameter SZ is interpreted as an integer. If it is less than or
8022 ** equal to zero, then this function returns a copy of X. Or, if
8023 ** SZ is equal to the size of X when interpreted as a blob, also
8024 ** return a copy of X. Otherwise, decompress blob X using zlib
8025 ** utility function uncompress() and return the results (another
8028 static void sqlarUncompressFunc(
8029 sqlite3_context *context,
8031 sqlite3_value **argv
8037 sz = sqlite3_value_int(argv[1]);
8039 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8040 sqlite3_result_value(context, argv[0]);
8042 const Bytef *pData= sqlite3_value_blob(argv[0]);
8043 Bytef *pOut = sqlite3_malloc(sz);
8044 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8045 sqlite3_result_error(context, "error in uncompress()", -1);
8047 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8057 int sqlite3_sqlar_init(
8060 const sqlite3_api_routines *pApi
8063 SQLITE_EXTENSION_INIT2(pApi);
8064 (void)pzErrMsg; /* Unused parameter */
8065 rc = sqlite3_create_function(db, "sqlar_compress", 1,
8066 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8067 sqlarCompressFunc, 0, 0);
8068 if( rc==SQLITE_OK ){
8069 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8070 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8071 sqlarUncompressFunc, 0, 0);
8076 /************************* End ../ext/misc/sqlar.c ********************/
8078 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
8082 ** The author disclaims copyright to this source code. In place of
8083 ** a legal notice, here is a blessing:
8085 ** May you do good and not evil.
8086 ** May you find forgiveness for yourself and forgive others.
8087 ** May you share freely, never taking more than you give.
8089 *************************************************************************
8091 #if !defined(SQLITEEXPERT_H)
8092 #define SQLITEEXPERT_H 1
8093 /* #include "sqlite3.h" */
8095 typedef struct sqlite3expert sqlite3expert;
8098 ** Create a new sqlite3expert object.
8100 ** If successful, a pointer to the new object is returned and (*pzErr) set
8101 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
8102 ** an English-language error message. In this case it is the responsibility
8103 ** of the caller to eventually free the error message buffer using
8106 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
8109 ** Configure an sqlite3expert object.
8111 ** EXPERT_CONFIG_SAMPLE:
8112 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
8113 ** each candidate index. This involves scanning and sorting the entire
8114 ** contents of each user database table once for each candidate index
8115 ** associated with the table. For large databases, this can be
8116 ** prohibitively slow. This option allows the sqlite3expert object to
8117 ** be configured so that sqlite_stat1 data is instead generated based on a
8118 ** subset of each table, or so that no sqlite_stat1 data is used at all.
8120 ** A single integer argument is passed to this option. If the value is less
8121 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
8122 ** the analysis - indexes are recommended based on the database schema only.
8123 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
8124 ** generated for each candidate index (this is the default). Finally, if the
8125 ** value falls between 0 and 100, then it represents the percentage of user
8126 ** table rows that should be considered when generating sqlite_stat1 data.
8130 ** // Do not generate any sqlite_stat1 data
8131 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
8133 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
8134 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
8136 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
8138 #define EXPERT_CONFIG_SAMPLE 1 /* int */
8141 ** Specify zero or more SQL statements to be included in the analysis.
8143 ** Buffer zSql must contain zero or more complete SQL statements. This
8144 ** function parses all statements contained in the buffer and adds them
8145 ** to the internal list of statements to analyze. If successful, SQLITE_OK
8146 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
8147 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
8148 ** may be set to point to an English language error message. In this case
8149 ** the caller is responsible for eventually freeing the error message buffer
8150 ** using sqlite3_free().
8152 ** If an error does occur while processing one of the statements in the
8153 ** buffer passed as the second argument, none of the statements in the
8154 ** buffer are added to the analysis.
8156 ** This function must be called before sqlite3_expert_analyze(). If a call
8157 ** to this function is made on an sqlite3expert object that has already
8158 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
8159 ** immediately and no statements are added to the analysis.
8161 int sqlite3_expert_sql(
8162 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
8163 const char *zSql, /* SQL statement(s) to add */
8164 char **pzErr /* OUT: Error message (if any) */
8169 ** This function is called after the sqlite3expert object has been configured
8170 ** with all SQL statements using sqlite3_expert_sql() to actually perform
8171 ** the analysis. Once this function has been called, it is not possible to
8172 ** add further SQL statements to the analysis.
8174 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
8175 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
8176 ** point to a buffer containing an English language error message. In this
8177 ** case it is the responsibility of the caller to eventually free the buffer
8178 ** using sqlite3_free().
8180 ** If an error does occur within this function, the sqlite3expert object
8181 ** is no longer useful for any purpose. At that point it is no longer
8182 ** possible to add further SQL statements to the object or to re-attempt
8183 ** the analysis. The sqlite3expert object must still be freed using a call
8184 ** sqlite3_expert_destroy().
8186 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
8189 ** Return the total number of statements loaded using sqlite3_expert_sql().
8190 ** The total number of SQL statements may be different from the total number
8191 ** to calls to sqlite3_expert_sql().
8193 int sqlite3_expert_count(sqlite3expert*);
8196 ** Return a component of the report.
8198 ** This function is called after sqlite3_expert_analyze() to extract the
8199 ** results of the analysis. Each call to this function returns either a
8200 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
8201 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
8202 ** #define constants defined below.
8204 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
8205 ** information relating to a specific SQL statement. In these cases that
8206 ** SQL statement is identified by the value passed as the second argument.
8207 ** SQL statements are numbered from 0 in the order in which they are parsed.
8208 ** If an out-of-range value (less than zero or equal to or greater than the
8209 ** value returned by sqlite3_expert_count()) is passed as the second argument
8210 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
8212 ** EXPERT_REPORT_SQL:
8213 ** Return the text of SQL statement iStmt.
8215 ** EXPERT_REPORT_INDEXES:
8216 ** Return a buffer containing the CREATE INDEX statements for all recommended
8217 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
8220 ** EXPERT_REPORT_PLAN:
8221 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
8222 ** iStmt after the proposed indexes have been added to the database schema.
8224 ** EXPERT_REPORT_CANDIDATES:
8225 ** Return a pointer to a buffer containing the CREATE INDEX statements
8226 ** for all indexes that were tested (for all SQL statements). The iStmt
8227 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
8229 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
8232 ** Values for the third argument passed to sqlite3_expert_report().
8234 #define EXPERT_REPORT_SQL 1
8235 #define EXPERT_REPORT_INDEXES 2
8236 #define EXPERT_REPORT_PLAN 3
8237 #define EXPERT_REPORT_CANDIDATES 4
8240 ** Free an (sqlite3expert*) handle and all associated resources. There
8241 ** should be one call to this function for each successful call to
8242 ** sqlite3-expert_new().
8244 void sqlite3_expert_destroy(sqlite3expert*);
8246 #endif /* !defined(SQLITEEXPERT_H) */
8248 /************************* End ../ext/expert/sqlite3expert.h ********************/
8249 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
8253 ** The author disclaims copyright to this source code. In place of
8254 ** a legal notice, here is a blessing:
8256 ** May you do good and not evil.
8257 ** May you find forgiveness for yourself and forgive others.
8258 ** May you share freely, never taking more than you give.
8260 *************************************************************************
8262 /* #include "sqlite3expert.h" */
8267 #ifndef SQLITE_OMIT_VIRTUALTABLE
8269 /* typedef sqlite3_int64 i64; */
8270 /* typedef sqlite3_uint64 u64; */
8272 typedef struct IdxColumn IdxColumn;
8273 typedef struct IdxConstraint IdxConstraint;
8274 typedef struct IdxScan IdxScan;
8275 typedef struct IdxStatement IdxStatement;
8276 typedef struct IdxTable IdxTable;
8277 typedef struct IdxWrite IdxWrite;
8279 #define STRLEN (int)strlen
8282 ** A temp table name that we assume no user database will actually use.
8283 ** If this assumption proves incorrect triggers on the table with the
8284 ** conflicting name will be ignored.
8286 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
8289 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
8290 ** any other type of single-ended range constraint on a column).
8293 ** Used to temporarily link IdxConstraint objects into lists while
8294 ** creating candidate indexes.
8296 struct IdxConstraint {
8297 char *zColl; /* Collation sequence */
8298 int bRange; /* True for range, false for eq */
8299 int iCol; /* Constrained table column */
8300 int bFlag; /* Used by idxFindCompatible() */
8301 int bDesc; /* True if ORDER BY <expr> DESC */
8302 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
8303 IdxConstraint *pLink; /* See above */
8307 ** A single scan of a single table.
8310 IdxTable *pTab; /* Associated table object */
8311 int iDb; /* Database containing table zTable */
8312 i64 covering; /* Mask of columns required for cov. index */
8313 IdxConstraint *pOrder; /* ORDER BY columns */
8314 IdxConstraint *pEq; /* List of == constraints */
8315 IdxConstraint *pRange; /* List of < constraints */
8316 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
8320 ** Information regarding a single database table. Extracted from
8321 ** "PRAGMA table_info" by function idxGetTableInfo().
8330 char *zName; /* Table name */
8332 IdxTable *pNext; /* Next table in linked list of all tables */
8336 ** An object of the following type is created for each unique table/write-op
8337 ** seen. The objects are stored in a singly-linked list beginning at
8338 ** sqlite3expert.pWrite.
8342 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
8347 ** Each statement being analyzed is represented by an instance of this
8350 struct IdxStatement {
8351 int iId; /* Statement number */
8352 char *zSql; /* SQL statement */
8353 char *zIdx; /* Indexes */
8354 char *zEQP; /* Plan */
8355 IdxStatement *pNext;
8360 ** A hash table for storing strings. With space for a payload string
8361 ** with each entry. Methods are:
8368 #define IDX_HASH_SIZE 1023
8369 typedef struct IdxHashEntry IdxHashEntry;
8370 typedef struct IdxHash IdxHash;
8371 struct IdxHashEntry {
8372 char *zKey; /* nul-terminated key */
8373 char *zVal; /* nul-terminated value string */
8374 char *zVal2; /* nul-terminated value string 2 */
8375 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
8376 IdxHashEntry *pNext; /* Next entry in hash */
8379 IdxHashEntry *pFirst;
8380 IdxHashEntry *aHash[IDX_HASH_SIZE];
8384 ** sqlite3expert object.
8386 struct sqlite3expert {
8387 int iSample; /* Percentage of tables to sample for stat1 */
8388 sqlite3 *db; /* User database */
8389 sqlite3 *dbm; /* In-memory db for this analysis */
8390 sqlite3 *dbv; /* Vtab schema for this analysis */
8391 IdxTable *pTable; /* List of all IdxTable objects */
8392 IdxScan *pScan; /* List of scan objects */
8393 IdxWrite *pWrite; /* List of write objects */
8394 IdxStatement *pStatement; /* List of IdxStatement objects */
8395 int bRun; /* True once analysis has run */
8397 int rc; /* Error code from whereinfo hook */
8398 IdxHash hIdx; /* Hash containing all candidate indexes */
8399 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
8404 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
8405 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
8407 static void *idxMalloc(int *pRc, int nByte){
8409 assert( *pRc==SQLITE_OK );
8411 pRet = sqlite3_malloc(nByte);
8413 memset(pRet, 0, nByte);
8415 *pRc = SQLITE_NOMEM;
8421 ** Initialize an IdxHash hash table.
8423 static void idxHashInit(IdxHash *pHash){
8424 memset(pHash, 0, sizeof(IdxHash));
8428 ** Reset an IdxHash hash table.
8430 static void idxHashClear(IdxHash *pHash){
8432 for(i=0; i<IDX_HASH_SIZE; i++){
8433 IdxHashEntry *pEntry;
8434 IdxHashEntry *pNext;
8435 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
8436 pNext = pEntry->pHashNext;
8437 sqlite3_free(pEntry->zVal2);
8438 sqlite3_free(pEntry);
8441 memset(pHash, 0, sizeof(IdxHash));
8445 ** Return the index of the hash bucket that the string specified by the
8446 ** arguments to this function belongs.
8448 static int idxHashString(const char *z, int n){
8449 unsigned int ret = 0;
8452 ret += (ret<<3) + (unsigned char)(z[i]);
8454 return (int)(ret % IDX_HASH_SIZE);
8458 ** If zKey is already present in the hash table, return non-zero and do
8459 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
8460 ** the hash table passed as the second argument.
8462 static int idxHashAdd(
8468 int nKey = STRLEN(zKey);
8469 int iHash = idxHashString(zKey, nKey);
8470 int nVal = (zVal ? STRLEN(zVal) : 0);
8471 IdxHashEntry *pEntry;
8473 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8474 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8478 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
8480 pEntry->zKey = (char*)&pEntry[1];
8481 memcpy(pEntry->zKey, zKey, nKey);
8483 pEntry->zVal = &pEntry->zKey[nKey+1];
8484 memcpy(pEntry->zVal, zVal, nVal);
8486 pEntry->pHashNext = pHash->aHash[iHash];
8487 pHash->aHash[iHash] = pEntry;
8489 pEntry->pNext = pHash->pFirst;
8490 pHash->pFirst = pEntry;
8496 ** If zKey/nKey is present in the hash table, return a pointer to the
8497 ** hash-entry object.
8499 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
8501 IdxHashEntry *pEntry;
8502 if( nKey<0 ) nKey = STRLEN(zKey);
8503 iHash = idxHashString(zKey, nKey);
8505 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8506 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8514 ** If the hash table contains an entry with a key equal to the string
8515 ** passed as the final two arguments to this function, return a pointer
8516 ** to the payload string. Otherwise, if zKey/nKey is not present in the
8517 ** hash table, return NULL.
8519 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
8520 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
8521 if( pEntry ) return pEntry->zVal;
8526 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
8527 ** variable to point to a copy of nul-terminated string zColl.
8529 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
8530 IdxConstraint *pNew;
8531 int nColl = STRLEN(zColl);
8533 assert( *pRc==SQLITE_OK );
8534 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
8536 pNew->zColl = (char*)&pNew[1];
8537 memcpy(pNew->zColl, zColl, nColl+1);
8543 ** An error associated with database handle db has just occurred. Pass
8544 ** the error message to callback function xOut.
8546 static void idxDatabaseError(
8547 sqlite3 *db, /* Database handle */
8548 char **pzErrmsg /* Write error here */
8550 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
8554 ** Prepare an SQL statement.
8556 static int idxPrepareStmt(
8557 sqlite3 *db, /* Database handle to compile against */
8558 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8559 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8560 const char *zSql /* SQL statement to compile */
8562 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
8563 if( rc!=SQLITE_OK ){
8565 idxDatabaseError(db, pzErrmsg);
8571 ** Prepare an SQL statement using the results of a printf() formatting.
8573 static int idxPrintfPrepareStmt(
8574 sqlite3 *db, /* Database handle to compile against */
8575 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8576 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8577 const char *zFmt, /* printf() format of SQL statement */
8578 ... /* Trailing printf() arguments */
8584 zSql = sqlite3_vmprintf(zFmt, ap);
8588 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
8596 /*************************************************************************
8597 ** Beginning of virtual table implementation.
8599 typedef struct ExpertVtab ExpertVtab;
8603 sqlite3expert *pExpert;
8606 typedef struct ExpertCsr ExpertCsr;
8608 sqlite3_vtab_cursor base;
8609 sqlite3_stmt *pData;
8612 static char *expertDequote(const char *zIn){
8613 int n = STRLEN(zIn);
8614 char *zRet = sqlite3_malloc(n);
8616 assert( zIn[0]=='\'' );
8617 assert( zIn[n-1]=='\'' );
8622 for(iIn=1; iIn<(n-1); iIn++){
8623 if( zIn[iIn]=='\'' ){
8624 assert( zIn[iIn+1]=='\'' );
8627 zRet[iOut++] = zIn[iIn];
8636 ** This function is the implementation of both the xConnect and xCreate
8637 ** methods of the r-tree virtual table.
8639 ** argv[0] -> module name
8640 ** argv[1] -> database name
8641 ** argv[2] -> table name
8642 ** argv[...] -> column names...
8644 static int expertConnect(
8647 int argc, const char *const*argv,
8648 sqlite3_vtab **ppVtab,
8651 sqlite3expert *pExpert = (sqlite3expert*)pAux;
8656 *pzErr = sqlite3_mprintf("internal error!");
8659 char *zCreateTable = expertDequote(argv[3]);
8661 rc = sqlite3_declare_vtab(db, zCreateTable);
8662 if( rc==SQLITE_OK ){
8663 p = idxMalloc(&rc, sizeof(ExpertVtab));
8665 if( rc==SQLITE_OK ){
8666 p->pExpert = pExpert;
8667 p->pTab = pExpert->pTable;
8668 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
8670 sqlite3_free(zCreateTable);
8676 *ppVtab = (sqlite3_vtab*)p;
8680 static int expertDisconnect(sqlite3_vtab *pVtab){
8681 ExpertVtab *p = (ExpertVtab*)pVtab;
8686 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
8687 ExpertVtab *p = (ExpertVtab*)pVtab;
8692 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
8693 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
8694 SQLITE_INDEX_CONSTRAINT_LE;
8696 pScan = idxMalloc(&rc, sizeof(IdxScan));
8700 /* Link the new scan object into the list */
8701 pScan->pTab = p->pTab;
8702 pScan->pNextScan = p->pExpert->pScan;
8703 p->pExpert->pScan = pScan;
8705 /* Add the constraints to the IdxScan object */
8706 for(i=0; i<pIdxInfo->nConstraint; i++){
8707 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8709 && pCons->iColumn>=0
8710 && p->pTab->aCol[pCons->iColumn].iPk==0
8711 && (pCons->op & opmask)
8713 IdxConstraint *pNew;
8714 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
8715 pNew = idxNewConstraint(&rc, zColl);
8717 pNew->iCol = pCons->iColumn;
8718 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8719 pNew->pNext = pScan->pEq;
8723 pNew->pNext = pScan->pRange;
8724 pScan->pRange = pNew;
8728 pIdxInfo->aConstraintUsage[i].argvIndex = n;
8732 /* Add the ORDER BY to the IdxScan object */
8733 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
8734 int iCol = pIdxInfo->aOrderBy[i].iColumn;
8736 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
8739 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
8740 pNew->pNext = pScan->pOrder;
8741 pNew->pLink = pScan->pOrder;
8742 pScan->pOrder = pNew;
8749 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
8753 static int expertUpdate(
8754 sqlite3_vtab *pVtab,
8756 sqlite3_value **azData,
8757 sqlite_int64 *pRowid
8767 ** Virtual table module xOpen method.
8769 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8773 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
8774 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
8779 ** Virtual table module xClose method.
8781 static int expertClose(sqlite3_vtab_cursor *cur){
8782 ExpertCsr *pCsr = (ExpertCsr*)cur;
8783 sqlite3_finalize(pCsr->pData);
8789 ** Virtual table module xEof method.
8791 ** Return non-zero if the cursor does not currently point to a valid
8792 ** record (i.e if the scan has finished), or zero otherwise.
8794 static int expertEof(sqlite3_vtab_cursor *cur){
8795 ExpertCsr *pCsr = (ExpertCsr*)cur;
8796 return pCsr->pData==0;
8800 ** Virtual table module xNext method.
8802 static int expertNext(sqlite3_vtab_cursor *cur){
8803 ExpertCsr *pCsr = (ExpertCsr*)cur;
8806 assert( pCsr->pData );
8807 rc = sqlite3_step(pCsr->pData);
8808 if( rc!=SQLITE_ROW ){
8809 rc = sqlite3_finalize(pCsr->pData);
8819 ** Virtual table module xRowid method.
8821 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8828 ** Virtual table module xColumn method.
8830 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
8831 ExpertCsr *pCsr = (ExpertCsr*)cur;
8832 sqlite3_value *pVal;
8833 pVal = sqlite3_column_value(pCsr->pData, i);
8835 sqlite3_result_value(ctx, pVal);
8841 ** Virtual table module xFilter method.
8843 static int expertFilter(
8844 sqlite3_vtab_cursor *cur,
8845 int idxNum, const char *idxStr,
8846 int argc, sqlite3_value **argv
8848 ExpertCsr *pCsr = (ExpertCsr*)cur;
8849 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
8850 sqlite3expert *pExpert = pVtab->pExpert;
8857 rc = sqlite3_finalize(pCsr->pData);
8859 if( rc==SQLITE_OK ){
8860 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
8861 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
8865 if( rc==SQLITE_OK ){
8866 rc = expertNext(cur);
8871 static int idxRegisterVtab(sqlite3expert *p){
8872 static sqlite3_module expertModule = {
8874 expertConnect, /* xCreate - create a table */
8875 expertConnect, /* xConnect - connect to an existing table */
8876 expertBestIndex, /* xBestIndex - Determine search strategy */
8877 expertDisconnect, /* xDisconnect - Disconnect from a table */
8878 expertDisconnect, /* xDestroy - Drop a table */
8879 expertOpen, /* xOpen - open a cursor */
8880 expertClose, /* xClose - close a cursor */
8881 expertFilter, /* xFilter - configure scan constraints */
8882 expertNext, /* xNext - advance a cursor */
8883 expertEof, /* xEof */
8884 expertColumn, /* xColumn - read data */
8885 expertRowid, /* xRowid - read data */
8886 expertUpdate, /* xUpdate - write data */
8887 0, /* xBegin - begin transaction */
8888 0, /* xSync - sync transaction */
8889 0, /* xCommit - commit transaction */
8890 0, /* xRollback - rollback transaction */
8891 0, /* xFindFunction - function overloading */
8892 0, /* xRename - rename the table */
8895 0, /* xRollbackTo */
8896 0, /* xShadowName */
8899 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
8902 ** End of virtual table implementation.
8903 *************************************************************************/
8905 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
8906 ** is called, set it to the return value of sqlite3_finalize() before
8907 ** returning. Otherwise, discard the sqlite3_finalize() return value.
8909 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
8910 int rc = sqlite3_finalize(pStmt);
8911 if( *pRc==SQLITE_OK ) *pRc = rc;
8915 ** Attempt to allocate an IdxTable structure corresponding to table zTab
8916 ** in the main database of connection db. If successful, set (*ppOut) to
8917 ** point to the new object and return SQLITE_OK. Otherwise, return an
8918 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
8919 ** set to point to an error string.
8921 ** It is the responsibility of the caller to eventually free either the
8922 ** IdxTable object or error message using sqlite3_free().
8924 static int idxGetTableInfo(
8925 sqlite3 *db, /* Database connection to read details from */
8926 const char *zTab, /* Table name */
8927 IdxTable **ppOut, /* OUT: New object (if successful) */
8928 char **pzErrmsg /* OUT: Error message (if not) */
8930 sqlite3_stmt *p1 = 0;
8932 int nTab = STRLEN(zTab);
8933 int nByte = sizeof(IdxTable) + nTab + 1;
8939 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
8940 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8941 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8942 nByte += 1 + STRLEN(zCol);
8943 rc = sqlite3_table_column_metadata(
8944 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8946 nByte += 1 + STRLEN(zCol);
8948 nPk += (sqlite3_column_int(p1, 5)>0);
8950 rc2 = sqlite3_reset(p1);
8951 if( rc==SQLITE_OK ) rc = rc2;
8953 nByte += sizeof(IdxColumn) * nCol;
8954 if( rc==SQLITE_OK ){
8955 pNew = idxMalloc(&rc, nByte);
8957 if( rc==SQLITE_OK ){
8958 pNew->aCol = (IdxColumn*)&pNew[1];
8960 pCsr = (char*)&pNew->aCol[nCol];
8964 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8965 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8966 int nCopy = STRLEN(zCol) + 1;
8967 pNew->aCol[nCol].zName = pCsr;
8968 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
8969 memcpy(pCsr, zCol, nCopy);
8972 rc = sqlite3_table_column_metadata(
8973 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8975 if( rc==SQLITE_OK ){
8976 nCopy = STRLEN(zCol) + 1;
8977 pNew->aCol[nCol].zColl = pCsr;
8978 memcpy(pCsr, zCol, nCopy);
8984 idxFinalize(&rc, p1);
8986 if( rc!=SQLITE_OK ){
8991 memcpy(pNew->zName, zTab, nTab+1);
8999 ** This function is a no-op if *pRc is set to anything other than
9000 ** SQLITE_OK when it is called.
9002 ** If *pRc is initially set to SQLITE_OK, then the text specified by
9003 ** the printf() style arguments is appended to zIn and the result returned
9004 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9005 ** zIn before returning.
9007 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9011 int nIn = zIn ? STRLEN(zIn) : 0;
9014 if( *pRc==SQLITE_OK ){
9015 zAppend = sqlite3_vmprintf(zFmt, ap);
9017 nAppend = STRLEN(zAppend);
9018 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9020 if( zAppend && zRet ){
9021 if( nIn ) memcpy(zRet, zIn, nIn);
9022 memcpy(&zRet[nIn], zAppend, nAppend+1);
9026 *pRc = SQLITE_NOMEM;
9028 sqlite3_free(zAppend);
9036 ** Return true if zId must be quoted in order to use it as an SQL
9037 ** identifier, or false otherwise.
9039 static int idxIdentifierRequiresQuotes(const char *zId){
9041 for(i=0; zId[i]; i++){
9043 && !(zId[i]>='0' && zId[i]<='9')
9044 && !(zId[i]>='a' && zId[i]<='z')
9045 && !(zId[i]>='A' && zId[i]<='Z')
9054 ** This function appends an index column definition suitable for constraint
9055 ** pCons to the string passed as zIn and returns the result.
9057 static char *idxAppendColDefn(
9058 int *pRc, /* IN/OUT: Error code */
9059 char *zIn, /* Column defn accumulated so far */
9060 IdxTable *pTab, /* Table index will be created on */
9061 IdxConstraint *pCons
9064 IdxColumn *p = &pTab->aCol[pCons->iCol];
9065 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9067 if( idxIdentifierRequiresQuotes(p->zName) ){
9068 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9070 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9073 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9074 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9075 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9077 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9082 zRet = idxAppendText(pRc, zRet, " DESC");
9088 ** Search database dbm for an index compatible with the one idxCreateFromCons()
9089 ** would create from arguments pScan, pEq and pTail. If no error occurs and
9090 ** such an index is found, return non-zero. Or, if no such index is found,
9093 ** If an error occurs, set *pRc to an SQLite error code and return zero.
9095 static int idxFindCompatible(
9096 int *pRc, /* OUT: Error code */
9097 sqlite3* dbm, /* Database to search */
9098 IdxScan *pScan, /* Scan for table to search for index on */
9099 IdxConstraint *pEq, /* List of == constraints */
9100 IdxConstraint *pTail /* List of range constraints */
9102 const char *zTbl = pScan->pTab->zName;
9103 sqlite3_stmt *pIdxList = 0;
9104 IdxConstraint *pIter;
9105 int nEq = 0; /* Number of elements in pEq */
9108 /* Count the elements in list pEq */
9109 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
9111 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
9112 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
9114 IdxConstraint *pT = pTail;
9115 sqlite3_stmt *pInfo = 0;
9116 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
9118 /* Zero the IdxConstraint.bFlag values in the pEq list */
9119 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
9121 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
9122 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
9123 int iIdx = sqlite3_column_int(pInfo, 0);
9124 int iCol = sqlite3_column_int(pInfo, 1);
9125 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
9128 for(pIter=pEq; pIter; pIter=pIter->pLink){
9129 if( pIter->bFlag ) continue;
9130 if( pIter->iCol!=iCol ) continue;
9131 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
9141 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
9149 idxFinalize(&rc, pInfo);
9151 if( rc==SQLITE_OK && bMatch ){
9152 sqlite3_finalize(pIdxList);
9156 idxFinalize(&rc, pIdxList);
9162 static int idxCreateFromCons(
9166 IdxConstraint *pTail
9168 sqlite3 *dbm = p->dbm;
9170 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
9171 IdxTable *pTab = pScan->pTab;
9174 IdxConstraint *pCons;
9178 for(pCons=pEq; pCons; pCons=pCons->pLink){
9179 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9181 for(pCons=pTail; pCons; pCons=pCons->pLink){
9182 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9185 if( rc==SQLITE_OK ){
9186 /* Hash the list of columns to come up with a name for the index */
9187 const char *zTable = pScan->pTab->zName;
9188 char *zName; /* Index name */
9190 for(i=0; zCols[i]; i++){
9191 h += ((h<<3) + zCols[i]);
9193 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
9197 if( idxIdentifierRequiresQuotes(zTable) ){
9198 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
9200 zFmt = "CREATE INDEX %s ON %s(%s)";
9202 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
9206 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
9207 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
9209 sqlite3_free(zName);
9214 sqlite3_free(zCols);
9220 ** Return true if list pList (linked by IdxConstraint.pLink) contains
9221 ** a constraint compatible with *p. Otherwise return false.
9223 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
9224 IdxConstraint *pCmp;
9225 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
9226 if( p->iCol==pCmp->iCol ) return 1;
9231 static int idxCreateFromWhere(
9233 IdxScan *pScan, /* Create indexes for this scan */
9234 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
9236 IdxConstraint *p1 = 0;
9237 IdxConstraint *pCon;
9240 /* Gather up all the == constraints. */
9241 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
9242 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9248 /* Create an index using the == constraints collected above. And the
9249 ** range constraint/ORDER BY terms passed in by the caller, if any. */
9250 rc = idxCreateFromCons(p, pScan, p1, pTail);
9252 /* If no range/ORDER BY passed by the caller, create a version of the
9253 ** index for each range constraint. */
9255 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
9256 assert( pCon->pLink==0 );
9257 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9258 rc = idxCreateFromCons(p, pScan, p1, pCon);
9267 ** Create candidate indexes in database [dbm] based on the data in
9268 ** linked-list pScan.
9270 static int idxCreateCandidates(sqlite3expert *p){
9274 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
9275 rc = idxCreateFromWhere(p, pIter, 0);
9276 if( rc==SQLITE_OK && pIter->pOrder ){
9277 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
9285 ** Free all elements of the linked list starting at pConstraint.
9287 static void idxConstraintFree(IdxConstraint *pConstraint){
9288 IdxConstraint *pNext;
9291 for(p=pConstraint; p; p=pNext){
9298 ** Free all elements of the linked list starting from pScan up until pLast
9299 ** (pLast is not freed).
9301 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
9304 for(p=pScan; p!=pLast; p=pNext){
9305 pNext = p->pNextScan;
9306 idxConstraintFree(p->pOrder);
9307 idxConstraintFree(p->pEq);
9308 idxConstraintFree(p->pRange);
9314 ** Free all elements of the linked list starting from pStatement up
9315 ** until pLast (pLast is not freed).
9317 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
9319 IdxStatement *pNext;
9320 for(p=pStatement; p!=pLast; p=pNext){
9322 sqlite3_free(p->zEQP);
9323 sqlite3_free(p->zIdx);
9329 ** Free the linked list of IdxTable objects starting at pTab.
9331 static void idxTableFree(IdxTable *pTab){
9334 for(pIter=pTab; pIter; pIter=pNext){
9335 pNext = pIter->pNext;
9336 sqlite3_free(pIter);
9341 ** Free the linked list of IdxWrite objects starting at pTab.
9343 static void idxWriteFree(IdxWrite *pTab){
9346 for(pIter=pTab; pIter; pIter=pNext){
9347 pNext = pIter->pNext;
9348 sqlite3_free(pIter);
9355 ** This function is called after candidate indexes have been created. It
9356 ** runs all the queries to see which indexes they prefer, and populates
9357 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
9361 char **pzErr /* OUT: Error message (sqlite3_malloc) */
9363 IdxStatement *pStmt;
9364 sqlite3 *dbm = p->dbm;
9370 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
9371 IdxHashEntry *pEntry;
9372 sqlite3_stmt *pExplain = 0;
9373 idxHashClear(&hIdx);
9374 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
9375 "EXPLAIN QUERY PLAN %s", pStmt->zSql
9377 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
9378 /* int iId = sqlite3_column_int(pExplain, 0); */
9379 /* int iParent = sqlite3_column_int(pExplain, 1); */
9380 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
9381 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
9385 if( !zDetail ) continue;
9386 nDetail = STRLEN(zDetail);
9388 for(i=0; i<nDetail; i++){
9389 const char *zIdx = 0;
9390 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
9391 zIdx = &zDetail[i+13];
9392 }else if( i+22<nDetail
9393 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
9395 zIdx = &zDetail[i+22];
9400 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
9403 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
9405 idxHashAdd(&rc, &hIdx, zSql, 0);
9406 if( rc ) goto find_indexes_out;
9412 if( zDetail[0]!='-' ){
9413 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
9417 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
9418 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
9421 idxFinalize(&rc, pExplain);
9425 idxHashClear(&hIdx);
9429 static int idxAuthCallback(
9435 const char *zTrigger
9440 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
9441 if( sqlite3_stricmp(zDb, "main")==0 ){
9442 sqlite3expert *p = (sqlite3expert*)pCtx;
9444 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
9445 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
9449 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
9450 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
9453 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
9454 if( rc==SQLITE_OK ){
9455 pWrite->pTab = pTab;
9457 pWrite->pNext = p->pWrite;
9467 static int idxProcessOneTrigger(
9472 static const char *zInt = UNIQUE_TABLE_NAME;
9473 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
9474 IdxTable *pTab = pWrite->pTab;
9475 const char *zTab = pTab->zName;
9477 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
9478 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
9480 sqlite3_stmt *pSelect = 0;
9484 /* Create the table and its triggers in the temp schema */
9485 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
9486 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
9487 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
9488 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
9490 idxFinalize(&rc, pSelect);
9492 /* Rename the table in the temp schema to zInt */
9493 if( rc==SQLITE_OK ){
9494 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
9498 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
9503 switch( pWrite->eOp ){
9504 case SQLITE_INSERT: {
9506 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
9507 for(i=0; i<pTab->nCol; i++){
9508 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
9510 zWrite = idxAppendText(&rc, zWrite, ")");
9513 case SQLITE_UPDATE: {
9515 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
9516 for(i=0; i<pTab->nCol; i++){
9517 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
9524 assert( pWrite->eOp==SQLITE_DELETE );
9525 if( rc==SQLITE_OK ){
9526 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
9527 if( zWrite==0 ) rc = SQLITE_NOMEM;
9532 if( rc==SQLITE_OK ){
9533 sqlite3_stmt *pX = 0;
9534 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
9535 idxFinalize(&rc, pX);
9536 if( rc!=SQLITE_OK ){
9537 idxDatabaseError(p->dbv, pzErr);
9540 sqlite3_free(zWrite);
9542 if( rc==SQLITE_OK ){
9543 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
9549 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
9552 IdxWrite *pFirst = p->pWrite;
9554 while( rc==SQLITE_OK && pFirst!=pEnd ){
9556 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
9557 rc = idxProcessOneTrigger(p, pIter, pzErr);
9567 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9568 int rc = idxRegisterVtab(p);
9569 sqlite3_stmt *pSchema = 0;
9571 /* For each table in the main db schema:
9573 ** 1) Add an entry to the p->pTable list, and
9574 ** 2) Create the equivalent virtual table in dbv.
9576 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9577 "SELECT type, name, sql, 1 FROM sqlite_schema "
9578 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9580 "SELECT type, name, sql, 2 FROM sqlite_schema "
9581 "WHERE type = 'trigger'"
9582 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9585 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9586 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9587 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9588 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9590 if( zType[0]=='v' || zType[1]=='r' ){
9591 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9594 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9595 if( rc==SQLITE_OK ){
9599 pTab->pNext = p->pTable;
9602 /* The statement the vtab will pass to sqlite3_declare_vtab() */
9603 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9604 for(i=0; i<pTab->nCol; i++){
9605 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9606 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9609 zInner = idxAppendText(&rc, zInner, ")");
9611 /* The CVT statement to create the vtab */
9612 zOuter = idxAppendText(&rc, 0,
9613 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9615 if( rc==SQLITE_OK ){
9616 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9618 sqlite3_free(zInner);
9619 sqlite3_free(zOuter);
9623 idxFinalize(&rc, pSchema);
9627 struct IdxSampleCtx {
9629 double target; /* Target nRet/nRow value */
9630 double nRow; /* Number of rows seen */
9631 double nRet; /* Number of rows returned */
9634 static void idxSampleFunc(
9635 sqlite3_context *pCtx,
9637 sqlite3_value **argv
9639 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
9647 bRet = (p->nRet / p->nRow) <= p->target;
9650 sqlite3_randomness(2, (void*)&rnd);
9651 bRet = ((int)rnd % 100) <= p->iTarget;
9655 sqlite3_result_int(pCtx, bRet);
9657 p->nRet += (double)bRet;
9663 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
9664 i64 iVal; /* SQLITE_INTEGER value */
9665 double rVal; /* SQLITE_FLOAT value */
9666 int nByte; /* Bytes of space allocated at z */
9667 int n; /* Size of buffer z */
9668 char *z; /* SQLITE_TEXT/BLOB value */
9673 ** Implementation of scalar function rem().
9675 static void idxRemFunc(
9676 sqlite3_context *pCtx,
9678 sqlite3_value **argv
9680 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
9681 struct IdxRemSlot *pSlot;
9685 iSlot = sqlite3_value_int(argv[0]);
9686 assert( iSlot<=p->nSlot );
9687 pSlot = &p->aSlot[iSlot];
9689 switch( pSlot->eType ){
9694 case SQLITE_INTEGER:
9695 sqlite3_result_int64(pCtx, pSlot->iVal);
9699 sqlite3_result_double(pCtx, pSlot->rVal);
9703 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9707 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9711 pSlot->eType = sqlite3_value_type(argv[1]);
9712 switch( pSlot->eType ){
9717 case SQLITE_INTEGER:
9718 pSlot->iVal = sqlite3_value_int64(argv[1]);
9722 pSlot->rVal = sqlite3_value_double(argv[1]);
9727 int nByte = sqlite3_value_bytes(argv[1]);
9728 if( nByte>pSlot->nByte ){
9729 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
9731 sqlite3_result_error_nomem(pCtx);
9734 pSlot->nByte = nByte*2;
9738 if( pSlot->eType==SQLITE_BLOB ){
9739 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
9741 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
9748 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
9751 "SELECT max(i.seqno) FROM "
9752 " sqlite_schema AS s, "
9753 " pragma_index_list(s.name) AS l, "
9754 " pragma_index_info(l.name) AS i "
9755 "WHERE s.type = 'table'";
9756 sqlite3_stmt *pMax = 0;
9759 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
9760 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
9761 *pnMax = sqlite3_column_int(pMax, 0) + 1;
9763 idxFinalize(&rc, pMax);
9768 static int idxPopulateOneStat1(
9770 sqlite3_stmt *pIndexXInfo,
9771 sqlite3_stmt *pWriteStat,
9781 sqlite3_stmt *pQuery = 0;
9785 assert( p->iSample>0 );
9787 /* Formulate the query text */
9788 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
9789 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
9790 const char *zComma = zCols==0 ? "" : ", ";
9791 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
9792 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
9793 zCols = idxAppendText(&rc, zCols,
9794 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
9796 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
9798 sqlite3_reset(pIndexXInfo);
9799 if( rc==SQLITE_OK ){
9800 if( p->iSample==100 ){
9801 zQuery = sqlite3_mprintf(
9802 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
9805 zQuery = sqlite3_mprintf(
9806 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
9810 sqlite3_free(zCols);
9811 sqlite3_free(zOrder);
9813 /* Formulate the query text */
9814 if( rc==SQLITE_OK ){
9815 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9816 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
9818 sqlite3_free(zQuery);
9820 if( rc==SQLITE_OK ){
9821 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
9823 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9824 IdxHashEntry *pEntry;
9826 for(i=0; i<=nCol; i++) aStat[i] = 1;
9827 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9829 for(i=0; i<nCol; i++){
9830 if( sqlite3_column_int(pQuery, i)==0 ) break;
9832 for(/*no-op*/; i<nCol; i++){
9837 if( rc==SQLITE_OK ){
9839 zStat = sqlite3_mprintf("%d", s0);
9840 if( zStat==0 ) rc = SQLITE_NOMEM;
9841 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
9842 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
9846 if( rc==SQLITE_OK ){
9847 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
9848 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
9849 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
9850 sqlite3_step(pWriteStat);
9851 rc = sqlite3_reset(pWriteStat);
9854 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
9856 assert( pEntry->zVal2==0 );
9857 pEntry->zVal2 = zStat;
9859 sqlite3_free(zStat);
9862 sqlite3_free(aStat);
9863 idxFinalize(&rc, pQuery);
9868 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
9872 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9873 if( rc!=SQLITE_OK ) return rc;
9875 zSql = sqlite3_mprintf(
9876 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
9878 if( zSql==0 ) return SQLITE_NOMEM;
9879 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
9886 ** This function is called as part of sqlite3_expert_analyze(). Candidate
9887 ** indexes have already been created in database sqlite3expert.dbm, this
9888 ** function populates sqlite_stat1 table in the same database.
9890 ** The stat1 data is generated by querying the
9892 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
9895 struct IdxRemCtx *pCtx = 0;
9896 struct IdxSampleCtx samplectx;
9898 i64 iPrev = -100000;
9899 sqlite3_stmt *pAllIndex = 0;
9900 sqlite3_stmt *pIndexXInfo = 0;
9901 sqlite3_stmt *pWrite = 0;
9903 const char *zAllIndex =
9904 "SELECT s.rowid, s.name, l.name FROM "
9905 " sqlite_schema AS s, "
9906 " pragma_index_list(s.name) AS l "
9907 "WHERE s.type = 'table'";
9908 const char *zIndexXInfo =
9909 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
9910 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
9912 /* If iSample==0, no sqlite_stat1 data is required. */
9913 if( p->iSample==0 ) return SQLITE_OK;
9915 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
9916 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
9918 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
9920 if( rc==SQLITE_OK ){
9921 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
9922 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
9925 if( rc==SQLITE_OK ){
9926 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9927 rc = sqlite3_create_function(
9928 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
9931 if( rc==SQLITE_OK ){
9932 rc = sqlite3_create_function(
9933 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
9937 if( rc==SQLITE_OK ){
9938 pCtx->nSlot = nMax+1;
9939 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
9941 if( rc==SQLITE_OK ){
9942 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
9944 if( rc==SQLITE_OK ){
9945 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
9948 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
9949 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
9950 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
9951 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
9952 if( p->iSample<100 && iPrev!=iRowid ){
9953 samplectx.target = (double)p->iSample / 100.0;
9954 samplectx.iTarget = p->iSample;
9955 samplectx.nRow = 0.0;
9956 samplectx.nRet = 0.0;
9957 rc = idxBuildSampleTable(p, zTab);
9958 if( rc!=SQLITE_OK ) break;
9960 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
9963 if( rc==SQLITE_OK && p->iSample<100 ){
9964 rc = sqlite3_exec(p->dbv,
9965 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
9969 idxFinalize(&rc, pAllIndex);
9970 idxFinalize(&rc, pIndexXInfo);
9971 idxFinalize(&rc, pWrite);
9974 for(i=0; i<pCtx->nSlot; i++){
9975 sqlite3_free(pCtx->aSlot[i].z);
9980 if( rc==SQLITE_OK ){
9981 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
9984 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9989 ** Allocate a new sqlite3expert object.
9991 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
9993 sqlite3expert *pNew;
9995 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
9997 /* Open two in-memory databases to work with. The "vtab database" (dbv)
9998 ** will contain a virtual table corresponding to each real table in
9999 ** the user database schema, and a copy of each view. It is used to
10000 ** collect information regarding the WHERE, ORDER BY and other clauses
10001 ** of the user's query.
10003 if( rc==SQLITE_OK ){
10005 pNew->iSample = 100;
10006 rc = sqlite3_open(":memory:", &pNew->dbv);
10008 if( rc==SQLITE_OK ){
10009 rc = sqlite3_open(":memory:", &pNew->dbm);
10010 if( rc==SQLITE_OK ){
10011 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10016 /* Copy the entire schema of database [db] into [dbm]. */
10017 if( rc==SQLITE_OK ){
10018 sqlite3_stmt *pSql;
10019 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10020 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10021 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10023 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10024 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10025 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10027 idxFinalize(&rc, pSql);
10030 /* Create the vtab schema */
10031 if( rc==SQLITE_OK ){
10032 rc = idxCreateVtabSchema(pNew, pzErrmsg);
10035 /* Register the auth callback with dbv */
10036 if( rc==SQLITE_OK ){
10037 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10040 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10041 ** return the new sqlite3expert handle. */
10042 if( rc!=SQLITE_OK ){
10043 sqlite3_expert_destroy(pNew);
10050 ** Configure an sqlite3expert object.
10052 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10053 int rc = SQLITE_OK;
10057 case EXPERT_CONFIG_SAMPLE: {
10058 int iVal = va_arg(ap, int);
10059 if( iVal<0 ) iVal = 0;
10060 if( iVal>100 ) iVal = 100;
10065 rc = SQLITE_NOTFOUND;
10074 ** Add an SQL statement to the analysis.
10076 int sqlite3_expert_sql(
10077 sqlite3expert *p, /* From sqlite3_expert_new() */
10078 const char *zSql, /* SQL statement to add */
10079 char **pzErr /* OUT: Error message (if any) */
10081 IdxScan *pScanOrig = p->pScan;
10082 IdxStatement *pStmtOrig = p->pStatement;
10083 int rc = SQLITE_OK;
10084 const char *zStmt = zSql;
10086 if( p->bRun ) return SQLITE_MISUSE;
10088 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
10089 sqlite3_stmt *pStmt = 0;
10090 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
10091 if( rc==SQLITE_OK ){
10093 IdxStatement *pNew;
10094 const char *z = sqlite3_sql(pStmt);
10096 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
10097 if( rc==SQLITE_OK ){
10098 pNew->zSql = (char*)&pNew[1];
10099 memcpy(pNew->zSql, z, n+1);
10100 pNew->pNext = p->pStatement;
10101 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
10102 p->pStatement = pNew;
10104 sqlite3_finalize(pStmt);
10107 idxDatabaseError(p->dbv, pzErr);
10111 if( rc!=SQLITE_OK ){
10112 idxScanFree(p->pScan, pScanOrig);
10113 idxStatementFree(p->pStatement, pStmtOrig);
10114 p->pScan = pScanOrig;
10115 p->pStatement = pStmtOrig;
10121 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
10123 IdxHashEntry *pEntry;
10125 /* Do trigger processing to collect any extra IdxScan structures */
10126 rc = idxProcessTriggers(p, pzErr);
10128 /* Create candidate indexes within the in-memory database file */
10129 if( rc==SQLITE_OK ){
10130 rc = idxCreateCandidates(p);
10133 /* Generate the stat1 data */
10134 if( rc==SQLITE_OK ){
10135 rc = idxPopulateStat1(p, pzErr);
10138 /* Formulate the EXPERT_REPORT_CANDIDATES text */
10139 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10140 p->zCandidates = idxAppendText(&rc, p->zCandidates,
10141 "%s;%s%s\n", pEntry->zVal,
10142 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
10146 /* Figure out which of the candidate indexes are preferred by the query
10147 ** planner and report the results to the user. */
10148 if( rc==SQLITE_OK ){
10149 rc = idxFindIndexes(p, pzErr);
10152 if( rc==SQLITE_OK ){
10159 ** Return the total number of statements that have been added to this
10160 ** sqlite3expert using sqlite3_expert_sql().
10162 int sqlite3_expert_count(sqlite3expert *p){
10164 if( p->pStatement ) nRet = p->pStatement->iId+1;
10169 ** Return a component of the report.
10171 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
10172 const char *zRet = 0;
10173 IdxStatement *pStmt;
10175 if( p->bRun==0 ) return 0;
10176 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
10178 case EXPERT_REPORT_SQL:
10179 if( pStmt ) zRet = pStmt->zSql;
10181 case EXPERT_REPORT_INDEXES:
10182 if( pStmt ) zRet = pStmt->zIdx;
10184 case EXPERT_REPORT_PLAN:
10185 if( pStmt ) zRet = pStmt->zEQP;
10187 case EXPERT_REPORT_CANDIDATES:
10188 zRet = p->zCandidates;
10195 ** Free an sqlite3expert object.
10197 void sqlite3_expert_destroy(sqlite3expert *p){
10199 sqlite3_close(p->dbm);
10200 sqlite3_close(p->dbv);
10201 idxScanFree(p->pScan, 0);
10202 idxStatementFree(p->pStatement, 0);
10203 idxTableFree(p->pTable);
10204 idxWriteFree(p->pWrite);
10205 idxHashClear(&p->hIdx);
10206 sqlite3_free(p->zCandidates);
10211 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10213 /************************* End ../ext/expert/sqlite3expert.c ********************/
10215 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
10216 /************************* Begin ../ext/misc/dbdata.c ******************/
10220 ** The author disclaims copyright to this source code. In place of
10221 ** a legal notice, here is a blessing:
10223 ** May you do good and not evil.
10224 ** May you find forgiveness for yourself and forgive others.
10225 ** May you share freely, never taking more than you give.
10227 ******************************************************************************
10229 ** This file contains an implementation of two eponymous virtual tables,
10230 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
10231 ** "sqlite_dbpage" eponymous virtual table be available.
10234 ** sqlite_dbdata is used to extract data directly from a database b-tree
10235 ** page and its associated overflow pages, bypassing the b-tree layer.
10236 ** The table schema is equivalent to:
10238 ** CREATE TABLE sqlite_dbdata(
10243 ** schema TEXT HIDDEN
10246 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
10247 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
10250 ** Each page of the database is inspected. If it cannot be interpreted as
10251 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
10252 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
10253 ** table contains one row for each field in the record associated with
10254 ** each cell on the page. For intkey b-trees, the key value is stored in
10257 ** For example, for the database:
10259 ** CREATE TABLE t1(a, b); -- root page is page 2
10260 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
10261 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
10263 ** the sqlite_dbdata table contains, as well as from entries related to
10264 ** page 1, content equivalent to:
10266 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
10268 ** (2, 0, 0, 'v' ),
10269 ** (2, 0, 1, 'five'),
10270 ** (2, 1, -1, 10 ),
10271 ** (2, 1, 0, 'x' ),
10272 ** (2, 1, 1, 'ten' );
10274 ** If database corruption is encountered, this module does not report an
10275 ** error. Instead, it attempts to extract as much data as possible and
10276 ** ignores the corruption.
10279 ** The sqlite_dbptr table has the following schema:
10281 ** CREATE TABLE sqlite_dbptr(
10284 ** schema TEXT HIDDEN
10287 ** It contains one entry for each b-tree pointer between a parent and
10288 ** child page in the database.
10290 #if !defined(SQLITEINT_H)
10291 /* #include "sqlite3ext.h" */
10293 /* typedef unsigned char u8; */
10296 SQLITE_EXTENSION_INIT1
10297 #include <string.h>
10298 #include <assert.h>
10300 #define DBDATA_PADDING_BYTES 100
10302 typedef struct DbdataTable DbdataTable;
10303 typedef struct DbdataCursor DbdataCursor;
10305 /* Cursor object */
10306 struct DbdataCursor {
10307 sqlite3_vtab_cursor base; /* Base class. Must be first */
10308 sqlite3_stmt *pStmt; /* For fetching database pages */
10310 int iPgno; /* Current page number */
10311 u8 *aPage; /* Buffer containing page */
10312 int nPage; /* Size of aPage[] in bytes */
10313 int nCell; /* Number of cells on aPage[] */
10314 int iCell; /* Current cell number */
10315 int bOnePage; /* True to stop after one page */
10317 sqlite3_int64 iRowid;
10319 /* Only for the sqlite_dbdata table */
10320 u8 *pRec; /* Buffer containing current record */
10321 int nRec; /* Size of pRec[] in bytes */
10322 int nHdr; /* Size of header in bytes */
10323 int iField; /* Current field number */
10327 sqlite3_int64 iIntkey; /* Integer key value */
10331 struct DbdataTable {
10332 sqlite3_vtab base; /* Base class. Must be first */
10333 sqlite3 *db; /* The database connection */
10334 sqlite3_stmt *pStmt; /* For fetching database pages */
10335 int bPtr; /* True for sqlite3_dbptr table */
10338 /* Column and schema definitions for sqlite_dbdata */
10339 #define DBDATA_COLUMN_PGNO 0
10340 #define DBDATA_COLUMN_CELL 1
10341 #define DBDATA_COLUMN_FIELD 2
10342 #define DBDATA_COLUMN_VALUE 3
10343 #define DBDATA_COLUMN_SCHEMA 4
10344 #define DBDATA_SCHEMA \
10345 "CREATE TABLE x(" \
10348 " field INTEGER," \
10350 " schema TEXT HIDDEN" \
10353 /* Column and schema definitions for sqlite_dbptr */
10354 #define DBPTR_COLUMN_PGNO 0
10355 #define DBPTR_COLUMN_CHILD 1
10356 #define DBPTR_COLUMN_SCHEMA 2
10357 #define DBPTR_SCHEMA \
10358 "CREATE TABLE x(" \
10360 " child INTEGER," \
10361 " schema TEXT HIDDEN" \
10365 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
10368 static int dbdataConnect(
10371 int argc, const char *const*argv,
10372 sqlite3_vtab **ppVtab,
10375 DbdataTable *pTab = 0;
10376 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
10378 if( rc==SQLITE_OK ){
10379 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
10383 memset(pTab, 0, sizeof(DbdataTable));
10385 pTab->bPtr = (pAux!=0);
10389 *ppVtab = (sqlite3_vtab*)pTab;
10394 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
10396 static int dbdataDisconnect(sqlite3_vtab *pVtab){
10397 DbdataTable *pTab = (DbdataTable*)pVtab;
10399 sqlite3_finalize(pTab->pStmt);
10400 sqlite3_free(pVtab);
10406 ** This function interprets two types of constraints:
10411 ** If neither are present, idxNum is set to 0. If schema=? is present,
10412 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
10413 ** in idxNum is set.
10415 ** If both parameters are present, schema is in position 0 and pgno in
10418 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
10419 DbdataTable *pTab = (DbdataTable*)tab;
10423 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
10425 for(i=0; i<pIdx->nConstraint; i++){
10426 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
10427 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10428 if( p->iColumn==colSchema ){
10429 if( p->usable==0 ) return SQLITE_CONSTRAINT;
10432 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
10439 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
10440 pIdx->aConstraintUsage[iSchema].omit = 1;
10443 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
10444 pIdx->aConstraintUsage[iPgno].omit = 1;
10445 pIdx->estimatedCost = 100;
10446 pIdx->estimatedRows = 50;
10448 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
10449 int iCol = pIdx->aOrderBy[0].iColumn;
10450 if( pIdx->nOrderBy==1 ){
10451 pIdx->orderByConsumed = (iCol==0 || iCol==1);
10452 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
10453 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
10458 pIdx->estimatedCost = 100000000;
10459 pIdx->estimatedRows = 1000000000;
10461 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
10466 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
10468 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
10469 DbdataCursor *pCsr;
10471 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
10473 return SQLITE_NOMEM;
10475 memset(pCsr, 0, sizeof(DbdataCursor));
10476 pCsr->base.pVtab = pVTab;
10479 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
10484 ** Restore a cursor object to the state it was in when first allocated
10485 ** by dbdataOpen().
10487 static void dbdataResetCursor(DbdataCursor *pCsr){
10488 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
10489 if( pTab->pStmt==0 ){
10490 pTab->pStmt = pCsr->pStmt;
10492 sqlite3_finalize(pCsr->pStmt);
10498 pCsr->bOnePage = 0;
10499 sqlite3_free(pCsr->aPage);
10500 sqlite3_free(pCsr->pRec);
10506 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
10508 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
10509 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10510 dbdataResetCursor(pCsr);
10511 sqlite3_free(pCsr);
10516 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
10518 static unsigned int get_uint16(unsigned char *a){
10519 return (a[0]<<8)|a[1];
10521 static unsigned int get_uint32(unsigned char *a){
10522 return ((unsigned int)a[0]<<24)
10523 | ((unsigned int)a[1]<<16)
10524 | ((unsigned int)a[2]<<8)
10525 | ((unsigned int)a[3]);
10529 ** Load page pgno from the database via the sqlite_dbpage virtual table.
10530 ** If successful, set (*ppPage) to point to a buffer containing the page
10531 ** data, (*pnPage) to the size of that buffer in bytes and return
10532 ** SQLITE_OK. In this case it is the responsibility of the caller to
10533 ** eventually free the buffer using sqlite3_free().
10535 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
10536 ** return an SQLite error code.
10538 static int dbdataLoadPage(
10539 DbdataCursor *pCsr, /* Cursor object */
10540 unsigned int pgno, /* Page number of page to load */
10541 u8 **ppPage, /* OUT: pointer to page buffer */
10542 int *pnPage /* OUT: Size of (*ppPage) in bytes */
10545 int rc = SQLITE_OK;
10546 sqlite3_stmt *pStmt = pCsr->pStmt;
10550 sqlite3_bind_int64(pStmt, 2, pgno);
10551 if( SQLITE_ROW==sqlite3_step(pStmt) ){
10552 int nCopy = sqlite3_column_bytes(pStmt, 0);
10555 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
10559 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
10560 memcpy(pPage, pCopy, nCopy);
10561 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
10567 rc2 = sqlite3_reset(pStmt);
10568 if( rc==SQLITE_OK ) rc = rc2;
10574 ** Read a varint. Put the value in *pVal and return the number of bytes.
10576 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
10577 sqlite3_int64 v = 0;
10579 for(i=0; i<8; i++){
10580 v = (v<<7) + (z[i]&0x7f);
10581 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
10583 v = (v<<8) + (z[i]&0xff);
10589 ** Return the number of bytes of space used by an SQLite value of type
10592 static int dbdataValueBytes(int eType){
10594 case 0: case 8: case 9:
10612 return ((eType-12) / 2);
10619 ** Load a value of type eType from buffer pData and use it to set the
10620 ** result of context object pCtx.
10622 static void dbdataValue(
10623 sqlite3_context *pCtx,
10628 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
10633 sqlite3_result_null(pCtx);
10637 sqlite3_result_int(pCtx, 0);
10640 sqlite3_result_int(pCtx, 1);
10643 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
10644 sqlite3_uint64 v = (signed char)pData[0];
10648 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10649 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10650 case 4: v = (v<<8) + pData[0]; pData++;
10651 case 3: v = (v<<8) + pData[0]; pData++;
10652 case 2: v = (v<<8) + pData[0]; pData++;
10657 memcpy(&r, &v, sizeof(r));
10658 sqlite3_result_double(pCtx, r);
10660 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
10666 int n = ((eType-12) / 2);
10668 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
10670 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
10678 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
10680 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
10681 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10682 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10687 int iOff = (pCsr->iPgno==1 ? 100 : 0);
10690 if( pCsr->aPage==0 ){
10692 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
10693 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
10694 if( rc!=SQLITE_OK ) return rc;
10695 if( pCsr->aPage ) break;
10698 pCsr->iCell = pTab->bPtr ? -2 : 0;
10699 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
10703 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
10704 pCsr->iCell = pCsr->nCell;
10707 if( pCsr->iCell>=pCsr->nCell ){
10708 sqlite3_free(pCsr->aPage);
10710 if( pCsr->bOnePage ) return SQLITE_OK;
10716 /* If there is no record loaded, load it now. */
10717 if( pCsr->pRec==0 ){
10720 sqlite3_int64 nPayload = 0;
10721 sqlite3_int64 nHdr = 0;
10726 switch( pCsr->aPage[iOff] ){
10736 /* This is not a b-tree page with records on it. Continue. */
10737 pCsr->iCell = pCsr->nCell;
10741 if( pCsr->iCell>=pCsr->nCell ){
10745 iOff += 8 + nPointer + pCsr->iCell*2;
10746 if( iOff>pCsr->nPage ){
10749 iOff = get_uint16(&pCsr->aPage[iOff]);
10752 /* For an interior node cell, skip past the child-page number */
10755 /* Load the "byte of payload including overflow" field */
10756 if( bNextPage || iOff>pCsr->nPage ){
10759 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
10762 /* If this is a leaf intkey cell, load the rowid */
10763 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
10764 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
10767 /* Figure out how much data to read from the local page */
10772 X = ((U-12)*64/255)-23;
10778 M = ((U-12)*32/255)-23;
10779 K = M+((nPayload-M)%(U-4));
10787 if( bNextPage || nLocal+iOff>pCsr->nPage ){
10791 /* Allocate space for payload. And a bit more to catch small buffer
10792 ** overruns caused by attempting to read a varint or similar from
10793 ** near the end of a corrupt record. */
10794 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
10795 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
10796 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
10797 pCsr->nRec = nPayload;
10799 /* Load the nLocal bytes of payload */
10800 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
10803 /* Load content from overflow pages */
10804 if( nPayload>nLocal ){
10805 sqlite3_int64 nRem = nPayload - nLocal;
10806 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
10811 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
10812 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
10813 if( rc!=SQLITE_OK ) return rc;
10814 if( aOvfl==0 ) break;
10817 if( nCopy>nRem ) nCopy = nRem;
10818 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
10821 pgnoOvfl = get_uint32(aOvfl);
10822 sqlite3_free(aOvfl);
10826 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
10828 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
10829 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
10830 pCsr->iField = (bHasRowid ? -1 : 0);
10835 if( pCsr->iField>0 ){
10836 sqlite3_int64 iType;
10837 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
10840 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
10841 pCsr->pPtr += dbdataValueBytes(iType);
10847 sqlite3_free(pCsr->aPage);
10848 sqlite3_free(pCsr->pRec);
10851 if( pCsr->bOnePage ) return SQLITE_OK;
10854 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
10858 /* Advance to the next cell. The next iteration of the loop will load
10859 ** the record and so on. */
10860 sqlite3_free(pCsr->pRec);
10867 assert( !"can't get here" );
10872 ** Return true if the cursor is at EOF.
10874 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
10875 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10876 return pCsr->aPage==0;
10880 ** Determine the size in pages of database zSchema (where zSchema is
10881 ** "main", "temp" or the name of an attached database) and set
10882 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
10883 ** an SQLite error code.
10885 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
10886 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
10889 sqlite3_stmt *pStmt = 0;
10891 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
10892 if( zSql==0 ) return SQLITE_NOMEM;
10893 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
10894 sqlite3_free(zSql);
10895 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
10896 pCsr->szDb = sqlite3_column_int(pStmt, 0);
10898 rc2 = sqlite3_finalize(pStmt);
10899 if( rc==SQLITE_OK ) rc = rc2;
10904 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
10906 static int dbdataFilter(
10907 sqlite3_vtab_cursor *pCursor,
10908 int idxNum, const char *idxStr,
10909 int argc, sqlite3_value **argv
10911 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10912 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10913 int rc = SQLITE_OK;
10914 const char *zSchema = "main";
10916 dbdataResetCursor(pCsr);
10917 assert( pCsr->iPgno==1 );
10918 if( idxNum & 0x01 ){
10919 zSchema = (const char*)sqlite3_value_text(argv[0]);
10921 if( idxNum & 0x02 ){
10922 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
10923 pCsr->bOnePage = 1;
10925 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
10926 rc = dbdataDbsize(pCsr, zSchema);
10929 if( rc==SQLITE_OK ){
10931 pCsr->pStmt = pTab->pStmt;
10934 rc = sqlite3_prepare_v2(pTab->db,
10935 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
10940 if( rc==SQLITE_OK ){
10941 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
10943 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
10945 if( rc==SQLITE_OK ){
10946 rc = dbdataNext(pCursor);
10952 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
10954 static int dbdataColumn(
10955 sqlite3_vtab_cursor *pCursor,
10956 sqlite3_context *ctx,
10959 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10960 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10963 case DBPTR_COLUMN_PGNO:
10964 sqlite3_result_int64(ctx, pCsr->iPgno);
10966 case DBPTR_COLUMN_CHILD: {
10967 int iOff = pCsr->iPgno==1 ? 100 : 0;
10968 if( pCsr->iCell<0 ){
10971 iOff += 12 + pCsr->iCell*2;
10972 if( iOff>pCsr->nPage ) return SQLITE_OK;
10973 iOff = get_uint16(&pCsr->aPage[iOff]);
10975 if( iOff<=pCsr->nPage ){
10976 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
10983 case DBDATA_COLUMN_PGNO:
10984 sqlite3_result_int64(ctx, pCsr->iPgno);
10986 case DBDATA_COLUMN_CELL:
10987 sqlite3_result_int(ctx, pCsr->iCell);
10989 case DBDATA_COLUMN_FIELD:
10990 sqlite3_result_int(ctx, pCsr->iField);
10992 case DBDATA_COLUMN_VALUE: {
10993 if( pCsr->iField<0 ){
10994 sqlite3_result_int64(ctx, pCsr->iIntkey);
10996 sqlite3_int64 iType;
10997 dbdataGetVarint(pCsr->pHdrPtr, &iType);
10999 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11010 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11012 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11013 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11014 *pRowid = pCsr->iRowid;
11020 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
11022 static int sqlite3DbdataRegister(sqlite3 *db){
11023 static sqlite3_module dbdata_module = {
11026 dbdataConnect, /* xConnect */
11027 dbdataBestIndex, /* xBestIndex */
11028 dbdataDisconnect, /* xDisconnect */
11030 dbdataOpen, /* xOpen - open a cursor */
11031 dbdataClose, /* xClose - close a cursor */
11032 dbdataFilter, /* xFilter - configure scan constraints */
11033 dbdataNext, /* xNext - advance a cursor */
11034 dbdataEof, /* xEof - check for end of scan */
11035 dbdataColumn, /* xColumn - read data */
11036 dbdataRowid, /* xRowid - read data */
11042 0, /* xFindMethod */
11044 0, /* xSavepoint */
11046 0, /* xRollbackTo */
11047 0 /* xShadowName */
11050 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11051 if( rc==SQLITE_OK ){
11052 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11060 int sqlite3_dbdata_init(
11063 const sqlite3_api_routines *pApi
11065 SQLITE_EXTENSION_INIT2(pApi);
11066 return sqlite3DbdataRegister(db);
11069 /************************* End ../ext/misc/dbdata.c ********************/
11072 #if defined(SQLITE_ENABLE_SESSION)
11074 ** State information for a single open session
11076 typedef struct OpenSession OpenSession;
11077 struct OpenSession {
11078 char *zName; /* Symbolic name for this session */
11079 int nFilter; /* Number of xFilter rejection GLOB patterns */
11080 char **azFilter; /* Array of xFilter rejection GLOB patterns */
11081 sqlite3_session *p; /* The open session */
11085 typedef struct ExpertInfo ExpertInfo;
11086 struct ExpertInfo {
11087 sqlite3expert *pExpert;
11091 /* A single line in the EQP output */
11092 typedef struct EQPGraphRow EQPGraphRow;
11093 struct EQPGraphRow {
11094 int iEqpId; /* ID for this row */
11095 int iParentId; /* ID of the parent row */
11096 EQPGraphRow *pNext; /* Next row in sequence */
11097 char zText[1]; /* Text to display for this row */
11100 /* All EQP output is collected into an instance of the following */
11101 typedef struct EQPGraph EQPGraph;
11103 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
11104 EQPGraphRow *pLast; /* Last element of the pRow list */
11105 char zPrefix[100]; /* Graph prefix */
11109 ** State information about the database connection is contained in an
11110 ** instance of the following structure.
11112 typedef struct ShellState ShellState;
11113 struct ShellState {
11114 sqlite3 *db; /* The database */
11115 u8 autoExplain; /* Automatically turn on .explain mode */
11116 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
11117 u8 autoEQPtest; /* autoEQP is in test mode */
11118 u8 autoEQPtrace; /* autoEQP is in trace mode */
11119 u8 statsOn; /* True to display memory stats before each finalize */
11120 u8 scanstatsOn; /* True to display scan stats before each finalize */
11121 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
11122 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
11123 u8 nEqpLevel; /* Depth of the EQP output graph */
11124 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
11125 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
11126 int outCount; /* Revert to stdout when reaching zero */
11127 int cnt; /* Number of records displayed so far */
11128 int lineno; /* Line number of last line read from in */
11129 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
11130 FILE *in; /* Read commands from this stream */
11131 FILE *out; /* Write results here */
11132 FILE *traceOut; /* Output for sqlite3_trace() */
11133 int nErr; /* Number of errors seen */
11134 int mode; /* An output mode setting */
11135 int modePrior; /* Saved mode */
11136 int cMode; /* temporary output mode for the current query */
11137 int normalMode; /* Output mode before ".explain on" */
11138 int writableSchema; /* True if PRAGMA writable_schema=ON */
11139 int showHeader; /* True to show column names in List or Column mode */
11140 int nCheck; /* Number of ".check" commands run */
11141 unsigned nProgress; /* Number of progress callbacks encountered */
11142 unsigned mxProgress; /* Maximum progress callbacks before failing */
11143 unsigned flgProgress; /* Flags for the progress callback */
11144 unsigned shellFlgs; /* Various flags */
11145 unsigned priorShFlgs; /* Saved copy of flags */
11146 sqlite3_int64 szMax; /* --maxsize argument to .open */
11147 char *zDestTable; /* Name of destination table when MODE_Insert */
11148 char *zTempFile; /* Temporary file that might need deleting */
11149 char zTestcase[30]; /* Name of current test case */
11150 char colSeparator[20]; /* Column separator character for several modes */
11151 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
11152 char colSepPrior[20]; /* Saved column separator */
11153 char rowSepPrior[20]; /* Saved row separator */
11154 int *colWidth; /* Requested width of each column in columnar modes */
11155 int *actualWidth; /* Actual width of each column */
11156 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
11157 char nullValue[20]; /* The text to print when a NULL comes back from
11159 char outfile[FILENAME_MAX]; /* Filename for *out */
11160 const char *zDbFilename; /* name of the database file */
11161 char *zFreeOnClose; /* Filename to free when closing */
11162 const char *zVfs; /* Name of VFS to use */
11163 sqlite3_stmt *pStmt; /* Current statement if any. */
11164 FILE *pLog; /* Write log output here */
11165 int *aiIndent; /* Array of indents used in MODE_Explain */
11166 int nIndent; /* Size of array aiIndent[] */
11167 int iIndent; /* Index of current op in aiIndent[] */
11168 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
11169 #if defined(SQLITE_ENABLE_SESSION)
11170 int nSession; /* Number of active sessions */
11171 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
11173 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
11177 /* Allowed values for ShellState.autoEQP
11179 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
11180 #define AUTOEQP_on 1 /* Automatic EQP is on */
11181 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
11182 #define AUTOEQP_full 3 /* Show full EXPLAIN */
11184 /* Allowed values for ShellState.openMode
11186 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
11187 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
11188 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
11189 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
11190 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
11191 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
11192 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
11194 /* Allowed values for ShellState.eTraceType
11196 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
11197 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
11198 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
11200 /* Bits in the ShellState.flgProgress variable */
11201 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
11202 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
11203 ** callback limit is reached, and for each
11204 ** top-level SQL statement */
11205 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
11208 ** These are the allowed shellFlgs values
11210 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
11211 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
11212 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
11213 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
11214 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
11215 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
11216 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
11217 #define SHFLG_HeaderSet 0x00000080 /* .header has been used */
11218 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
11219 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
11222 ** Macros for testing and setting shellFlgs
11224 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
11225 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
11226 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
11229 ** These are the allowed modes.
11231 #define MODE_Line 0 /* One column per line. Blank line between records */
11232 #define MODE_Column 1 /* One record per line in neat columns */
11233 #define MODE_List 2 /* One record per line with a separator */
11234 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
11235 #define MODE_Html 4 /* Generate an XHTML table */
11236 #define MODE_Insert 5 /* Generate SQL "insert" statements */
11237 #define MODE_Quote 6 /* Quote values as for SQL */
11238 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
11239 #define MODE_Csv 8 /* Quote strings, numbers are plain */
11240 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
11241 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
11242 #define MODE_Pretty 11 /* Pretty-print schemas */
11243 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
11244 #define MODE_Json 13 /* Output JSON */
11245 #define MODE_Markdown 14 /* Markdown formatting */
11246 #define MODE_Table 15 /* MySQL-style table formatting */
11247 #define MODE_Box 16 /* Unicode box-drawing characters */
11249 static const char *modeDescr[] = {
11270 ** These are the column/row/line separators used by the various
11271 ** import/export modes.
11273 #define SEP_Column "|"
11274 #define SEP_Row "\n"
11275 #define SEP_Tab "\t"
11276 #define SEP_Space " "
11277 #define SEP_Comma ","
11278 #define SEP_CrLf "\r\n"
11279 #define SEP_Unit "\x1F"
11280 #define SEP_Record "\x1E"
11283 ** A callback for the sqlite3_log() interface.
11285 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
11286 ShellState *p = (ShellState*)pArg;
11287 if( p->pLog==0 ) return;
11288 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
11293 ** SQL function: shell_putsnl(X)
11295 ** Write the text X to the screen (or whatever output is being directed)
11296 ** adding a newline at the end, and then return X.
11298 static void shellPutsFunc(
11299 sqlite3_context *pCtx,
11301 sqlite3_value **apVal
11303 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
11305 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
11306 sqlite3_result_value(pCtx, apVal[0]);
11310 ** SQL function: edit(VALUE)
11311 ** edit(VALUE,EDITOR)
11315 ** (1) Write VALUE into a temporary file.
11316 ** (2) Run program EDITOR on that temporary file.
11317 ** (3) Read the temporary file back and return its content as the result.
11318 ** (4) Delete the temporary file
11320 ** If the EDITOR argument is omitted, use the value in the VISUAL
11321 ** environment variable. If still there is no EDITOR, through an error.
11323 ** Also throw an error if the EDITOR program returns a non-zero exit code.
11325 #ifndef SQLITE_NOHAVE_SYSTEM
11326 static void editFunc(
11327 sqlite3_context *context,
11329 sqlite3_value **argv
11331 const char *zEditor;
11332 char *zTempFile = 0;
11341 unsigned char *p = 0;
11344 zEditor = (const char*)sqlite3_value_text(argv[1]);
11346 zEditor = getenv("VISUAL");
11349 sqlite3_result_error(context, "no editor for edit()", -1);
11352 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
11353 sqlite3_result_error(context, "NULL input to edit()", -1);
11356 db = sqlite3_context_db_handle(context);
11358 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
11359 if( zTempFile==0 ){
11360 sqlite3_uint64 r = 0;
11361 sqlite3_randomness(sizeof(r), &r);
11362 zTempFile = sqlite3_mprintf("temp%llx", r);
11363 if( zTempFile==0 ){
11364 sqlite3_result_error_nomem(context);
11368 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
11369 /* When writing the file to be edited, do \n to \r\n conversions on systems
11370 ** that want \r\n line endings */
11371 f = fopen(zTempFile, bBin ? "wb" : "w");
11373 sqlite3_result_error(context, "edit() cannot open temp file", -1);
11374 goto edit_func_end;
11376 sz = sqlite3_value_bytes(argv[0]);
11378 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
11380 const char *z = (const char*)sqlite3_value_text(argv[0]);
11381 /* Remember whether or not the value originally contained \r\n */
11382 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
11383 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
11388 sqlite3_result_error(context, "edit() could not write the whole file", -1);
11389 goto edit_func_end;
11391 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
11393 sqlite3_result_error_nomem(context);
11394 goto edit_func_end;
11397 sqlite3_free(zCmd);
11399 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
11400 goto edit_func_end;
11402 f = fopen(zTempFile, "rb");
11404 sqlite3_result_error(context,
11405 "edit() cannot reopen temp file after edit", -1);
11406 goto edit_func_end;
11408 fseek(f, 0, SEEK_END);
11411 p = sqlite3_malloc64( sz+1 );
11413 sqlite3_result_error_nomem(context);
11414 goto edit_func_end;
11416 x = fread(p, 1, (size_t)sz, f);
11420 sqlite3_result_error(context, "could not read back the whole file", -1);
11421 goto edit_func_end;
11424 sqlite3_result_blob64(context, p, sz, sqlite3_free);
11426 sqlite3_int64 i, j;
11428 /* If the original contains \r\n then do no conversions back to \n */
11431 /* If the file did not originally contain \r\n then convert any new
11432 ** \r\n back into \n */
11433 for(i=j=0; i<sz; i++){
11434 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
11440 sqlite3_result_text64(context, (const char*)p, sz,
11441 sqlite3_free, SQLITE_UTF8);
11448 sqlite3_free(zTempFile);
11451 #endif /* SQLITE_NOHAVE_SYSTEM */
11454 ** Save or restore the current output mode
11456 static void outputModePush(ShellState *p){
11457 p->modePrior = p->mode;
11458 p->priorShFlgs = p->shellFlgs;
11459 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
11460 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
11462 static void outputModePop(ShellState *p){
11463 p->mode = p->modePrior;
11464 p->shellFlgs = p->priorShFlgs;
11465 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
11466 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
11470 ** Output the given string as a hex-encoded blob (eg. X'1234' )
11472 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
11474 char *zBlob = (char *)pBlob;
11475 raw_printf(out,"X'");
11476 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
11477 raw_printf(out,"'");
11481 ** Find a string that is not found anywhere in z[]. Return a pointer
11484 ** Try to use zA and zB first. If both of those are already found in z[]
11485 ** then make up some string and store it in the buffer zBuf.
11487 static const char *unused_string(
11488 const char *z, /* Result must not appear anywhere in z */
11489 const char *zA, const char *zB, /* Try these first */
11490 char *zBuf /* Space to store a generated string */
11493 if( strstr(z, zA)==0 ) return zA;
11494 if( strstr(z, zB)==0 ) return zB;
11496 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
11497 }while( strstr(z,zBuf)!=0 );
11502 ** Output the given string as a quoted string using SQL quoting conventions.
11504 ** See also: output_quoted_escaped_string()
11506 static void output_quoted_string(FILE *out, const char *z){
11509 setBinaryMode(out, 1);
11510 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11512 utf8_printf(out,"'%s'",z);
11514 raw_printf(out, "'");
11516 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11519 utf8_printf(out, "%.*s", i, z);
11523 raw_printf(out, "'");
11531 raw_printf(out, "'");
11533 setTextMode(out, 1);
11537 ** Output the given string as a quoted string using SQL quoting conventions.
11538 ** Additionallly , escape the "\n" and "\r" characters so that they do not
11539 ** get corrupted by end-of-line translation facilities in some operating
11542 ** This is like output_quoted_string() but with the addition of the \r\n
11543 ** escape mechanism.
11545 static void output_quoted_escaped_string(FILE *out, const char *z){
11548 setBinaryMode(out, 1);
11549 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
11551 utf8_printf(out,"'%s'",z);
11553 const char *zNL = 0;
11554 const char *zCR = 0;
11557 char zBuf1[20], zBuf2[20];
11558 for(i=0; z[i]; i++){
11559 if( z[i]=='\n' ) nNL++;
11560 if( z[i]=='\r' ) nCR++;
11563 raw_printf(out, "replace(");
11564 zNL = unused_string(z, "\\n", "\\012", zBuf1);
11567 raw_printf(out, "replace(");
11568 zCR = unused_string(z, "\\r", "\\015", zBuf2);
11570 raw_printf(out, "'");
11572 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
11575 utf8_printf(out, "%.*s", i, z);
11579 raw_printf(out, "'");
11587 raw_printf(out, "%s", zNL);
11590 raw_printf(out, "%s", zCR);
11592 raw_printf(out, "'");
11594 raw_printf(out, ",'%s',char(13))", zCR);
11597 raw_printf(out, ",'%s',char(10))", zNL);
11600 setTextMode(out, 1);
11604 ** Output the given string as a quoted according to C or TCL quoting rules.
11606 static void output_c_string(FILE *out, const char *z){
11609 while( (c = *(z++))!=0 ){
11613 }else if( c=='"' ){
11616 }else if( c=='\t' ){
11619 }else if( c=='\n' ){
11622 }else if( c=='\r' ){
11625 }else if( !isprint(c&0xff) ){
11626 raw_printf(out, "\\%03o", c&0xff);
11635 ** Output the given string as a quoted according to JSON quoting rules.
11637 static void output_json_string(FILE *out, const char *z, int n){
11639 if( n<0 ) n = (int)strlen(z);
11643 if( c=='\\' || c=='"' ){
11646 }else if( c<=0x1f ){
11650 }else if( c=='\f' ){
11652 }else if( c=='\n' ){
11654 }else if( c=='\r' ){
11656 }else if( c=='\t' ){
11659 raw_printf(out, "u%04x",c);
11669 ** Output the given string with characters that are special to
11672 static void output_html_string(FILE *out, const char *z){
11684 utf8_printf(out,"%.*s",i,z);
11687 raw_printf(out,"<");
11688 }else if( z[i]=='&' ){
11689 raw_printf(out,"&");
11690 }else if( z[i]=='>' ){
11691 raw_printf(out,">");
11692 }else if( z[i]=='\"' ){
11693 raw_printf(out,""");
11694 }else if( z[i]=='\'' ){
11695 raw_printf(out,"'");
11704 ** If a field contains any character identified by a 1 in the following
11705 ** array, then the string must be quoted for CSV.
11707 static const char needCsvQuote[] = {
11708 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11709 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11710 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
11711 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11712 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11713 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11714 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11715 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
11716 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11717 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11718 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11719 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11720 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11721 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11722 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11723 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11727 ** Output a single term of CSV. Actually, p->colSeparator is used for
11728 ** the separator, which may or may not be a comma. p->nullValue is
11729 ** the null value. Strings are quoted if necessary. The separator
11730 ** is only issued if bSep is true.
11732 static void output_csv(ShellState *p, const char *z, int bSep){
11733 FILE *out = p->out;
11735 utf8_printf(out,"%s",p->nullValue);
11738 int nSep = strlen30(p->colSeparator);
11739 for(i=0; z[i]; i++){
11740 if( needCsvQuote[((unsigned char*)z)[i]]
11741 || (z[i]==p->colSeparator[0] &&
11742 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
11748 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
11749 utf8_printf(out, "%s", zQuoted);
11750 sqlite3_free(zQuoted);
11752 utf8_printf(out, "%s", z);
11756 utf8_printf(p->out, "%s", p->colSeparator);
11761 ** This routine runs when the user presses Ctrl-C
11763 static void interrupt_handler(int NotUsed){
11764 UNUSED_PARAMETER(NotUsed);
11766 if( seenInterrupt>2 ) exit(1);
11767 if( globalDb ) sqlite3_interrupt(globalDb);
11770 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11772 ** This routine runs for console events (e.g. Ctrl-C) on Win32
11774 static BOOL WINAPI ConsoleCtrlHandler(
11775 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
11777 if( dwCtrlType==CTRL_C_EVENT ){
11778 interrupt_handler(0);
11785 #ifndef SQLITE_OMIT_AUTHORIZATION
11787 ** When the ".auth ON" is set, the following authorizer callback is
11788 ** invoked. It always returns SQLITE_OK.
11790 static int shellAuth(
11798 ShellState *p = (ShellState*)pClientData;
11799 static const char *azAction[] = { 0,
11800 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
11801 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
11802 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
11803 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
11804 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
11805 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
11806 "PRAGMA", "READ", "SELECT",
11807 "TRANSACTION", "UPDATE", "ATTACH",
11808 "DETACH", "ALTER_TABLE", "REINDEX",
11809 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
11810 "FUNCTION", "SAVEPOINT", "RECURSIVE"
11818 utf8_printf(p->out, "authorizer: %s", azAction[op]);
11819 for(i=0; i<4; i++){
11820 raw_printf(p->out, " ");
11822 output_c_string(p->out, az[i]);
11824 raw_printf(p->out, "NULL");
11827 raw_printf(p->out, "\n");
11833 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
11835 ** This routine converts some CREATE TABLE statements for shadow tables
11836 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
11838 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
11840 if( zTail==0 ) return;
11841 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
11842 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
11844 utf8_printf(out, "%s%s", z, zTail);
11847 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
11850 printSchemaLine(out, z, zTail);
11855 ** Return true if string z[] has nothing but whitespace and comments to the
11856 ** end of the first line.
11858 static int wsToEol(const char *z){
11860 for(i=0; z[i]; i++){
11861 if( z[i]=='\n' ) return 1;
11862 if( IsSpace(z[i]) ) continue;
11863 if( z[i]=='-' && z[i+1]=='-' ) return 1;
11870 ** Add a new entry to the EXPLAIN QUERY PLAN data
11872 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
11874 int nText = strlen30(zText);
11875 if( p->autoEQPtest ){
11876 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
11878 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
11879 if( pNew==0 ) shell_out_of_memory();
11880 pNew->iEqpId = iEqpId;
11881 pNew->iParentId = p2;
11882 memcpy(pNew->zText, zText, nText+1);
11884 if( p->sGraph.pLast ){
11885 p->sGraph.pLast->pNext = pNew;
11887 p->sGraph.pRow = pNew;
11889 p->sGraph.pLast = pNew;
11893 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
11896 static void eqp_reset(ShellState *p){
11897 EQPGraphRow *pRow, *pNext;
11898 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
11899 pNext = pRow->pNext;
11900 sqlite3_free(pRow);
11902 memset(&p->sGraph, 0, sizeof(p->sGraph));
11905 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
11906 ** pOld, or return the first such line if pOld is NULL
11908 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
11909 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
11910 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
11914 /* Render a single level of the graph that has iEqpId as its parent. Called
11915 ** recursively to render sublevels.
11917 static void eqp_render_level(ShellState *p, int iEqpId){
11918 EQPGraphRow *pRow, *pNext;
11919 int n = strlen30(p->sGraph.zPrefix);
11921 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
11922 pNext = eqp_next_row(p, iEqpId, pRow);
11924 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
11925 pNext ? "|--" : "`--", z);
11926 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
11927 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
11928 eqp_render_level(p, pRow->iEqpId);
11929 p->sGraph.zPrefix[n] = 0;
11935 ** Display and reset the EXPLAIN QUERY PLAN data
11937 static void eqp_render(ShellState *p){
11938 EQPGraphRow *pRow = p->sGraph.pRow;
11940 if( pRow->zText[0]=='-' ){
11941 if( pRow->pNext==0 ){
11945 utf8_printf(p->out, "%s\n", pRow->zText+3);
11946 p->sGraph.pRow = pRow->pNext;
11947 sqlite3_free(pRow);
11949 utf8_printf(p->out, "QUERY PLAN\n");
11951 p->sGraph.zPrefix[0] = 0;
11952 eqp_render_level(p, 0);
11957 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11959 ** Progress handler callback.
11961 static int progress_handler(void *pClientData) {
11962 ShellState *p = (ShellState*)pClientData;
11964 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
11965 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
11966 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11967 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
11970 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
11971 raw_printf(p->out, "Progress %u\n", p->nProgress);
11975 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
11980 static void print_dashes(FILE *out, int N){
11981 const char zDash[] = "--------------------------------------------------";
11982 const int nDash = sizeof(zDash) - 1;
11987 raw_printf(out, "%.*s", N, zDash);
11991 ** Print a markdown or table-style row separator using ascii-art
11993 static void print_row_separator(
12000 fputs(zSep, p->out);
12001 print_dashes(p->out, p->actualWidth[0]+2);
12002 for(i=1; i<nArg; i++){
12003 fputs(zSep, p->out);
12004 print_dashes(p->out, p->actualWidth[i]+2);
12006 fputs(zSep, p->out);
12008 fputs("\n", p->out);
12012 ** This is the callback routine that the shell
12013 ** invokes for each row of a query result.
12015 static int shell_callback(
12017 int nArg, /* Number of result columns */
12018 char **azArg, /* Text of each result column */
12019 char **azCol, /* Column names */
12020 int *aiType /* Column types. Might be NULL */
12023 ShellState *p = (ShellState*)pArg;
12025 if( azArg==0 ) return 0;
12026 switch( p->cMode ){
12029 if( azArg==0 ) break;
12030 for(i=0; i<nArg; i++){
12031 int len = strlen30(azCol[i] ? azCol[i] : "");
12032 if( len>w ) w = len;
12034 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12035 for(i=0; i<nArg; i++){
12036 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12037 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12041 case MODE_Explain: {
12042 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12043 if( nArg>ArraySize(aExplainWidth) ){
12044 nArg = ArraySize(aExplainWidth);
12047 for(i=0; i<nArg; i++){
12048 int w = aExplainWidth[i];
12049 utf8_width_print(p->out, w, azCol[i]);
12050 fputs(i==nArg-1 ? "\n" : " ", p->out);
12052 for(i=0; i<nArg; i++){
12053 int w = aExplainWidth[i];
12054 print_dashes(p->out, w);
12055 fputs(i==nArg-1 ? "\n" : " ", p->out);
12058 if( azArg==0 ) break;
12059 for(i=0; i<nArg; i++){
12060 int w = aExplainWidth[i];
12061 if( azArg[i] && strlenChar(azArg[i])>w ){
12062 w = strlenChar(azArg[i]);
12064 if( i==1 && p->aiIndent && p->pStmt ){
12065 if( p->iIndent<p->nIndent ){
12066 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12070 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12071 fputs(i==nArg-1 ? "\n" : " ", p->out);
12075 case MODE_Semi: { /* .schema and .fullschema output */
12076 printSchemaLine(p->out, azArg[0], ";\n");
12079 case MODE_Pretty: { /* .schema and .fullschema with --indent */
12087 if( azArg[0]==0 ) break;
12088 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
12089 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
12091 utf8_printf(p->out, "%s;\n", azArg[0]);
12094 z = sqlite3_mprintf("%s", azArg[0]);
12096 for(i=0; IsSpace(z[i]); i++){}
12097 for(; (c = z[i])!=0; i++){
12099 if( z[j-1]=='\r' ) z[j-1] = '\n';
12100 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
12101 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
12106 while( j>0 && IsSpace(z[j-1]) ){ j--; }
12108 if( strlen30(z)>=79 ){
12109 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
12112 }else if( c=='"' || c=='\'' || c=='`' ){
12114 }else if( c=='[' ){
12116 }else if( c=='-' && z[i+1]=='-' ){
12118 }else if( c=='(' ){
12120 }else if( c==')' ){
12122 if( nLine>0 && nParen==0 && j>0 ){
12123 printSchemaLineN(p->out, z, j, "\n");
12128 if( nParen==1 && cEnd==0
12129 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
12132 printSchemaLineN(p->out, z, j, "\n ");
12135 while( IsSpace(z[i+1]) ){ i++; }
12140 printSchemaLine(p->out, z, ";\n");
12145 if( p->cnt++==0 && p->showHeader ){
12146 for(i=0; i<nArg; i++){
12147 utf8_printf(p->out,"%s%s",azCol[i],
12148 i==nArg-1 ? p->rowSeparator : p->colSeparator);
12151 if( azArg==0 ) break;
12152 for(i=0; i<nArg; i++){
12153 char *z = azArg[i];
12154 if( z==0 ) z = p->nullValue;
12155 utf8_printf(p->out, "%s", z);
12157 utf8_printf(p->out, "%s", p->colSeparator);
12159 utf8_printf(p->out, "%s", p->rowSeparator);
12165 if( p->cnt++==0 && p->showHeader ){
12166 raw_printf(p->out,"<TR>");
12167 for(i=0; i<nArg; i++){
12168 raw_printf(p->out,"<TH>");
12169 output_html_string(p->out, azCol[i]);
12170 raw_printf(p->out,"</TH>\n");
12172 raw_printf(p->out,"</TR>\n");
12174 if( azArg==0 ) break;
12175 raw_printf(p->out,"<TR>");
12176 for(i=0; i<nArg; i++){
12177 raw_printf(p->out,"<TD>");
12178 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12179 raw_printf(p->out,"</TD>\n");
12181 raw_printf(p->out,"</TR>\n");
12185 if( p->cnt++==0 && p->showHeader ){
12186 for(i=0; i<nArg; i++){
12187 output_c_string(p->out,azCol[i] ? azCol[i] : "");
12188 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12190 utf8_printf(p->out, "%s", p->rowSeparator);
12192 if( azArg==0 ) break;
12193 for(i=0; i<nArg; i++){
12194 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12195 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12197 utf8_printf(p->out, "%s", p->rowSeparator);
12201 setBinaryMode(p->out, 1);
12202 if( p->cnt++==0 && p->showHeader ){
12203 for(i=0; i<nArg; i++){
12204 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
12206 utf8_printf(p->out, "%s", p->rowSeparator);
12209 for(i=0; i<nArg; i++){
12210 output_csv(p, azArg[i], i<nArg-1);
12212 utf8_printf(p->out, "%s", p->rowSeparator);
12214 setTextMode(p->out, 1);
12217 case MODE_Insert: {
12218 if( azArg==0 ) break;
12219 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
12220 if( p->showHeader ){
12221 raw_printf(p->out,"(");
12222 for(i=0; i<nArg; i++){
12223 if( i>0 ) raw_printf(p->out, ",");
12224 if( quoteChar(azCol[i]) ){
12225 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
12226 utf8_printf(p->out, "%s", z);
12229 raw_printf(p->out, "%s", azCol[i]);
12232 raw_printf(p->out,")");
12235 for(i=0; i<nArg; i++){
12236 raw_printf(p->out, i>0 ? "," : " VALUES(");
12237 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12238 utf8_printf(p->out,"NULL");
12239 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12240 if( ShellHasFlag(p, SHFLG_Newlines) ){
12241 output_quoted_string(p->out, azArg[i]);
12243 output_quoted_escaped_string(p->out, azArg[i]);
12245 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12246 utf8_printf(p->out,"%s", azArg[i]);
12247 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12249 double r = sqlite3_column_double(p->pStmt, i);
12251 memcpy(&ur,&r,sizeof(r));
12252 if( ur==0x7ff0000000000000LL ){
12253 raw_printf(p->out, "1e999");
12254 }else if( ur==0xfff0000000000000LL ){
12255 raw_printf(p->out, "-1e999");
12257 sqlite3_snprintf(50,z,"%!.20g", r);
12258 raw_printf(p->out, "%s", z);
12260 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12261 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12262 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12263 output_hex_blob(p->out, pBlob, nBlob);
12264 }else if( isNumber(azArg[i], 0) ){
12265 utf8_printf(p->out,"%s", azArg[i]);
12266 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
12267 output_quoted_string(p->out, azArg[i]);
12269 output_quoted_escaped_string(p->out, azArg[i]);
12272 raw_printf(p->out,");\n");
12276 if( azArg==0 ) break;
12278 fputs("[{", p->out);
12280 fputs(",\n{", p->out);
12283 for(i=0; i<nArg; i++){
12284 output_json_string(p->out, azCol[i], -1);
12286 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12287 fputs("null",p->out);
12288 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12290 double r = sqlite3_column_double(p->pStmt, i);
12292 memcpy(&ur,&r,sizeof(r));
12293 if( ur==0x7ff0000000000000LL ){
12294 raw_printf(p->out, "1e999");
12295 }else if( ur==0xfff0000000000000LL ){
12296 raw_printf(p->out, "-1e999");
12298 sqlite3_snprintf(50,z,"%!.20g", r);
12299 raw_printf(p->out, "%s", z);
12301 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12302 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12303 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12304 output_json_string(p->out, pBlob, nBlob);
12305 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12306 output_json_string(p->out, azArg[i], -1);
12308 utf8_printf(p->out,"%s", azArg[i]);
12318 if( azArg==0 ) break;
12319 if( p->cnt==0 && p->showHeader ){
12320 for(i=0; i<nArg; i++){
12321 if( i>0 ) fputs(p->colSeparator, p->out);
12322 output_quoted_string(p->out, azCol[i]);
12324 fputs(p->rowSeparator, p->out);
12327 for(i=0; i<nArg; i++){
12328 if( i>0 ) fputs(p->colSeparator, p->out);
12329 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12330 utf8_printf(p->out,"NULL");
12331 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12332 output_quoted_string(p->out, azArg[i]);
12333 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12334 utf8_printf(p->out,"%s", azArg[i]);
12335 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12337 double r = sqlite3_column_double(p->pStmt, i);
12338 sqlite3_snprintf(50,z,"%!.20g", r);
12339 raw_printf(p->out, "%s", z);
12340 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12341 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12342 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12343 output_hex_blob(p->out, pBlob, nBlob);
12344 }else if( isNumber(azArg[i], 0) ){
12345 utf8_printf(p->out,"%s", azArg[i]);
12347 output_quoted_string(p->out, azArg[i]);
12350 fputs(p->rowSeparator, p->out);
12354 if( p->cnt++==0 && p->showHeader ){
12355 for(i=0; i<nArg; i++){
12356 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12357 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
12359 utf8_printf(p->out, "%s", p->rowSeparator);
12361 if( azArg==0 ) break;
12362 for(i=0; i<nArg; i++){
12363 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12364 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
12366 utf8_printf(p->out, "%s", p->rowSeparator);
12370 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
12378 ** This is the callback routine that the SQLite library
12379 ** invokes for each row of a query result.
12381 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12382 /* since we don't have type info, call the shell_callback with a NULL value */
12383 return shell_callback(pArg, nArg, azArg, azCol, NULL);
12387 ** This is the callback routine from sqlite3_exec() that appends all
12388 ** output onto the end of a ShellText object.
12390 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
12391 ShellText *p = (ShellText*)pArg;
12393 UNUSED_PARAMETER(az);
12394 if( azArg==0 ) return 0;
12395 if( p->n ) appendText(p, "|", 0);
12396 for(i=0; i<nArg; i++){
12397 if( i ) appendText(p, ",", 0);
12398 if( azArg[i] ) appendText(p, azArg[i], 0);
12404 ** Generate an appropriate SELFTEST table in the main database.
12406 static void createSelftestTable(ShellState *p){
12408 sqlite3_exec(p->db,
12409 "SAVEPOINT selftest_init;\n"
12410 "CREATE TABLE IF NOT EXISTS selftest(\n"
12411 " tno INTEGER PRIMARY KEY,\n" /* Test number */
12412 " op TEXT,\n" /* Operator: memo run */
12413 " cmd TEXT,\n" /* Command text */
12414 " ans TEXT\n" /* Desired answer */
12416 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
12417 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
12418 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
12419 " 'memo','Tests generated by --init');\n"
12420 "INSERT INTO [_shell$self]\n"
12422 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
12423 "FROM sqlite_schema ORDER BY 2'',224))',\n"
12424 " hex(sha3_query('SELECT type,name,tbl_name,sql "
12425 "FROM sqlite_schema ORDER BY 2',224));\n"
12426 "INSERT INTO [_shell$self]\n"
12428 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
12429 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
12430 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
12432 " SELECT name FROM sqlite_schema\n"
12433 " WHERE type='table'\n"
12434 " AND name<>'selftest'\n"
12435 " AND coalesce(rootpage,0)>0\n"
12437 " ORDER BY name;\n"
12438 "INSERT INTO [_shell$self]\n"
12439 " VALUES('run','PRAGMA integrity_check','ok');\n"
12440 "INSERT INTO selftest(tno,op,cmd,ans)"
12441 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
12442 "DROP TABLE [_shell$self];"
12445 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
12446 sqlite3_free(zErrMsg);
12448 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
12453 ** Set the destination table field of the ShellState structure to
12454 ** the name of the table given. Escape any quote characters in the
12457 static void set_table_name(ShellState *p, const char *zName){
12462 if( p->zDestTable ){
12463 free(p->zDestTable);
12466 if( zName==0 ) return;
12467 cQuote = quoteChar(zName);
12468 n = strlen30(zName);
12469 if( cQuote ) n += n+2;
12470 z = p->zDestTable = malloc( n+1 );
12471 if( z==0 ) shell_out_of_memory();
12473 if( cQuote ) z[n++] = cQuote;
12474 for(i=0; zName[i]; i++){
12476 if( zName[i]==cQuote ) z[n++] = cQuote;
12478 if( cQuote ) z[n++] = cQuote;
12484 ** Execute a query statement that will generate SQL output. Print
12485 ** the result columns, comma-separated, on a line and then add a
12486 ** semicolon terminator to the end of that line.
12488 ** If the number of columns is 1 and that column contains text "--"
12489 ** then write the semicolon on a separate line. That way, if a
12490 ** "--" comment occurs at the end of the statement, the comment
12491 ** won't consume the semicolon terminator.
12493 static int run_table_dump_query(
12494 ShellState *p, /* Query context */
12495 const char *zSelect /* SELECT statement to extract content */
12497 sqlite3_stmt *pSelect;
12502 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
12503 if( rc!=SQLITE_OK || !pSelect ){
12504 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12505 sqlite3_errmsg(p->db));
12506 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12509 rc = sqlite3_step(pSelect);
12510 nResult = sqlite3_column_count(pSelect);
12511 while( rc==SQLITE_ROW ){
12512 z = (const char*)sqlite3_column_text(pSelect, 0);
12513 utf8_printf(p->out, "%s", z);
12514 for(i=1; i<nResult; i++){
12515 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
12518 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
12520 raw_printf(p->out, "\n;\n");
12522 raw_printf(p->out, ";\n");
12524 rc = sqlite3_step(pSelect);
12526 rc = sqlite3_finalize(pSelect);
12527 if( rc!=SQLITE_OK ){
12528 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12529 sqlite3_errmsg(p->db));
12530 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12536 ** Allocate space and save off current error string.
12538 static char *save_err_msg(
12539 sqlite3 *db /* Database to query */
12541 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
12542 char *zErrMsg = sqlite3_malloc64(nErrMsg);
12544 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
12551 ** Attempt to display I/O stats on Linux using /proc/PID/io
12553 static void displayLinuxIoStats(FILE *out){
12556 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
12557 in = fopen(z, "rb");
12558 if( in==0 ) return;
12559 while( fgets(z, sizeof(z), in)!=0 ){
12560 static const struct {
12561 const char *zPattern;
12564 { "rchar: ", "Bytes received by read():" },
12565 { "wchar: ", "Bytes sent to write():" },
12566 { "syscr: ", "Read() system calls:" },
12567 { "syscw: ", "Write() system calls:" },
12568 { "read_bytes: ", "Bytes read from storage:" },
12569 { "write_bytes: ", "Bytes written to storage:" },
12570 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
12573 for(i=0; i<ArraySize(aTrans); i++){
12574 int n = strlen30(aTrans[i].zPattern);
12575 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
12576 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
12586 ** Display a single line of status using 64-bit values.
12588 static void displayStatLine(
12589 ShellState *p, /* The shell context */
12590 char *zLabel, /* Label for this one line */
12591 char *zFormat, /* Format for the result */
12592 int iStatusCtrl, /* Which status to display */
12593 int bReset /* True to reset the stats */
12595 sqlite3_int64 iCur = -1;
12596 sqlite3_int64 iHiwtr = -1;
12599 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
12600 for(i=0, nPercent=0; zFormat[i]; i++){
12601 if( zFormat[i]=='%' ) nPercent++;
12604 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
12606 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
12608 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
12612 ** Display memory stats.
12614 static int display_stats(
12615 sqlite3 *db, /* Database to query */
12616 ShellState *pArg, /* Pointer to ShellState */
12617 int bReset /* True to reset the stats */
12622 if( pArg==0 || pArg->out==0 ) return 0;
12625 if( pArg->pStmt && (pArg->statsOn & 2) ){
12627 sqlite3_stmt *pStmt = pArg->pStmt;
12629 nCol = sqlite3_column_count(pStmt);
12630 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
12631 for(i=0; i<nCol; i++){
12632 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
12633 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
12634 #ifndef SQLITE_OMIT_DECLTYPE
12635 sqlite3_snprintf(30, z+x, "declared type:");
12636 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
12638 #ifdef SQLITE_ENABLE_COLUMN_METADATA
12639 sqlite3_snprintf(30, z+x, "database name:");
12640 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
12641 sqlite3_snprintf(30, z+x, "table name:");
12642 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
12643 sqlite3_snprintf(30, z+x, "origin name:");
12644 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
12649 displayStatLine(pArg, "Memory Used:",
12650 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
12651 displayStatLine(pArg, "Number of Outstanding Allocations:",
12652 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
12653 if( pArg->shellFlgs & SHFLG_Pagecache ){
12654 displayStatLine(pArg, "Number of Pcache Pages Used:",
12655 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
12657 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
12658 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
12659 displayStatLine(pArg, "Largest Allocation:",
12660 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
12661 displayStatLine(pArg, "Largest Pcache Allocation:",
12662 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
12663 #ifdef YYTRACKMAXSTACKDEPTH
12664 displayStatLine(pArg, "Deepest Parser Stack:",
12665 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
12669 if( pArg->shellFlgs & SHFLG_Lookaside ){
12670 iHiwtr = iCur = -1;
12671 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
12672 &iCur, &iHiwtr, bReset);
12673 raw_printf(pArg->out,
12674 "Lookaside Slots Used: %d (max %d)\n",
12676 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
12677 &iCur, &iHiwtr, bReset);
12678 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
12680 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
12681 &iCur, &iHiwtr, bReset);
12682 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
12684 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
12685 &iCur, &iHiwtr, bReset);
12686 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
12689 iHiwtr = iCur = -1;
12690 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
12691 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
12693 iHiwtr = iCur = -1;
12694 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
12695 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
12696 iHiwtr = iCur = -1;
12697 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
12698 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
12699 iHiwtr = iCur = -1;
12700 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
12701 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
12702 iHiwtr = iCur = -1;
12703 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
12704 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
12705 iHiwtr = iCur = -1;
12706 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
12707 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
12709 iHiwtr = iCur = -1;
12710 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
12711 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
12716 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
12718 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
12719 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
12720 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
12721 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
12722 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
12723 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12724 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
12725 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
12726 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
12727 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
12728 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
12729 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
12730 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
12734 displayLinuxIoStats(pArg->out);
12737 /* Do not remove this machine readable comment: extra-stats-output-here */
12743 ** Display scan stats.
12745 static void display_scanstats(
12746 sqlite3 *db, /* Database to query */
12747 ShellState *pArg /* Pointer to ShellState */
12749 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
12750 UNUSED_PARAMETER(db);
12751 UNUSED_PARAMETER(pArg);
12754 raw_printf(pArg->out, "-------- scanstats --------\n");
12756 for(k=0; k<=mx; k++){
12757 double rEstLoop = 1.0;
12758 for(i=n=0; 1; i++){
12759 sqlite3_stmt *p = pArg->pStmt;
12760 sqlite3_int64 nLoop, nVisit;
12763 const char *zExplain;
12764 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
12767 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
12768 if( iSid>mx ) mx = iSid;
12769 if( iSid!=k ) continue;
12771 rEstLoop = (double)nLoop;
12772 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
12775 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
12776 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
12777 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
12778 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
12780 raw_printf(pArg->out,
12781 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
12782 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
12786 raw_printf(pArg->out, "---------------------------\n");
12791 ** Parameter azArray points to a zero-terminated array of strings. zStr
12792 ** points to a single nul-terminated string. Return non-zero if zStr
12793 ** is equal, according to strcmp(), to any of the strings in the array.
12794 ** Otherwise, return zero.
12796 static int str_in_array(const char *zStr, const char **azArray){
12798 for(i=0; azArray[i]; i++){
12799 if( 0==strcmp(zStr, azArray[i]) ) return 1;
12805 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
12806 ** and populate the ShellState.aiIndent[] array with the number of
12807 ** spaces each opcode should be indented before it is output.
12809 ** The indenting rules are:
12811 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
12812 ** all opcodes that occur between the p2 jump destination and the opcode
12813 ** itself by 2 spaces.
12815 ** * For each "Goto", if the jump destination is earlier in the program
12816 ** and ends on one of:
12817 ** Yield SeekGt SeekLt RowSetRead Rewind
12818 ** or if the P1 parameter is one instead of zero,
12819 ** then indent all opcodes between the earlier instruction
12820 ** and "Goto" by 2 spaces.
12822 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12823 const char *zSql; /* The text of the SQL statement */
12824 const char *z; /* Used to check if this is an EXPLAIN */
12825 int *abYield = 0; /* True if op is an OP_Yield */
12826 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12827 int iOp; /* Index of operation in p->aiIndent[] */
12829 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
12830 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
12832 const char *azGoto[] = { "Goto", 0 };
12834 /* Try to figure out if this is really an EXPLAIN statement. If this
12835 ** cannot be verified, return early. */
12836 if( sqlite3_column_count(pSql)!=8 ){
12837 p->cMode = p->mode;
12840 zSql = sqlite3_sql(pSql);
12841 if( zSql==0 ) return;
12842 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
12843 if( sqlite3_strnicmp(z, "explain", 7) ){
12844 p->cMode = p->mode;
12848 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12850 int iAddr = sqlite3_column_int(pSql, 0);
12851 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
12853 /* Set p2 to the P2 field of the current opcode. Then, assuming that
12854 ** p2 is an instruction address, set variable p2op to the index of that
12855 ** instruction in the aiIndent[] array. p2 and p2op may be different if
12856 ** the current instruction is part of a sub-program generated by an
12857 ** SQL trigger or foreign key. */
12858 int p2 = sqlite3_column_int(pSql, 3);
12859 int p2op = (p2 + (iOp-iAddr));
12861 /* Grow the p->aiIndent array as required */
12864 /* Do further verfication that this is explain output. Abort if
12866 static const char *explainCols[] = {
12867 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
12869 for(jj=0; jj<ArraySize(explainCols); jj++){
12870 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
12871 p->cMode = p->mode;
12872 sqlite3_reset(pSql);
12878 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
12879 if( p->aiIndent==0 ) shell_out_of_memory();
12880 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
12881 if( abYield==0 ) shell_out_of_memory();
12883 abYield[iOp] = str_in_array(zOp, azYield);
12884 p->aiIndent[iOp] = 0;
12885 p->nIndent = iOp+1;
12887 if( str_in_array(zOp, azNext) ){
12888 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12890 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
12891 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
12893 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12898 sqlite3_free(abYield);
12899 sqlite3_reset(pSql);
12903 ** Free the array allocated by explain_data_prepare().
12905 static void explain_data_delete(ShellState *p){
12906 sqlite3_free(p->aiIndent);
12913 ** Disable and restore .wheretrace and .selecttrace settings.
12915 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12916 extern unsigned int sqlite3_unsupported_selecttrace;
12917 static int savedSelectTrace;
12919 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12920 extern int sqlite3WhereTrace;
12921 static int savedWhereTrace;
12923 static void disable_debug_trace_modes(void){
12924 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12925 savedSelectTrace = sqlite3_unsupported_selecttrace;
12926 sqlite3_unsupported_selecttrace = 0;
12928 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12929 savedWhereTrace = sqlite3WhereTrace;
12930 sqlite3WhereTrace = 0;
12933 static void restore_debug_trace_modes(void){
12934 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12935 sqlite3_unsupported_selecttrace = savedSelectTrace;
12937 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12938 sqlite3WhereTrace = savedWhereTrace;
12942 /* Create the TEMP table used to store parameter bindings */
12943 static void bind_table_init(ShellState *p){
12945 int defensiveMode = 0;
12946 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
12947 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
12948 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
12949 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
12950 sqlite3_exec(p->db,
12951 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
12952 " key TEXT PRIMARY KEY,\n"
12954 ") WITHOUT ROWID;",
12956 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
12957 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
12961 ** Bind parameters on a prepared statement.
12963 ** Parameter bindings are taken from a TEMP table of the form:
12965 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
12968 ** No bindings occur if this table does not exist. The name of the table
12969 ** begins with "sqlite_" so that it will not collide with ordinary application
12970 ** tables. The table must be in the TEMP schema.
12972 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
12976 sqlite3_stmt *pQ = 0;
12978 nVar = sqlite3_bind_parameter_count(pStmt);
12979 if( nVar==0 ) return; /* Nothing to do */
12980 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
12981 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
12982 return; /* Parameter table does not exist */
12984 rc = sqlite3_prepare_v2(pArg->db,
12985 "SELECT value FROM temp.sqlite_parameters"
12986 " WHERE key=?1", -1, &pQ, 0);
12987 if( rc || pQ==0 ) return;
12988 for(i=1; i<=nVar; i++){
12990 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
12992 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
12995 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
12996 if( sqlite3_step(pQ)==SQLITE_ROW ){
12997 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
12999 sqlite3_bind_null(pStmt, i);
13003 sqlite3_finalize(pQ);
13007 ** UTF8 box-drawing characters. Imagine box lines like this:
13015 ** Each box characters has between 2 and 4 of the lines leading from
13016 ** the center. The characters are here identified by the numbers of
13017 ** their corresponding lines.
13019 #define BOX_24 "\342\224\200" /* U+2500 --- */
13020 #define BOX_13 "\342\224\202" /* U+2502 | */
13021 #define BOX_23 "\342\224\214" /* U+250c ,- */
13022 #define BOX_34 "\342\224\220" /* U+2510 -, */
13023 #define BOX_12 "\342\224\224" /* U+2514 '- */
13024 #define BOX_14 "\342\224\230" /* U+2518 -' */
13025 #define BOX_123 "\342\224\234" /* U+251c |- */
13026 #define BOX_134 "\342\224\244" /* U+2524 -| */
13027 #define BOX_234 "\342\224\254" /* U+252c -,- */
13028 #define BOX_124 "\342\224\264" /* U+2534 -'- */
13029 #define BOX_1234 "\342\224\274" /* U+253c -|- */
13031 /* Draw horizontal line N characters long using unicode box
13034 static void print_box_line(FILE *out, int N){
13035 const char zDash[] =
13036 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13037 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13038 const int nDash = sizeof(zDash) - 1;
13041 utf8_printf(out, zDash);
13044 utf8_printf(out, "%.*s", N, zDash);
13048 ** Draw a horizontal separator for a MODE_Box table.
13050 static void print_box_row_separator(
13059 utf8_printf(p->out, "%s", zSep1);
13060 print_box_line(p->out, p->actualWidth[0]+2);
13061 for(i=1; i<nArg; i++){
13062 utf8_printf(p->out, "%s", zSep2);
13063 print_box_line(p->out, p->actualWidth[i]+2);
13065 utf8_printf(p->out, "%s", zSep3);
13067 fputs("\n", p->out);
13073 ** Run a prepared statement and output the result in one of the
13074 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13077 ** This is different from ordinary exec_prepared_stmt() in that
13078 ** it has to run the entire query and gather the results into memory
13079 ** first, in order to determine column widths, before providing
13082 static void exec_prepared_stmt_columnar(
13083 ShellState *p, /* Pointer to ShellState */
13084 sqlite3_stmt *pStmt /* Statment to run */
13086 sqlite3_int64 nRow = 0;
13089 sqlite3_int64 nAlloc = 0;
13092 sqlite3_int64 i, nData;
13093 int j, nTotal, w, n;
13094 const char *colSep = 0;
13095 const char *rowSep = 0;
13097 rc = sqlite3_step(pStmt);
13098 if( rc!=SQLITE_ROW ) return;
13099 nColumn = sqlite3_column_count(pStmt);
13100 nAlloc = nColumn*4;
13101 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
13102 if( azData==0 ) shell_out_of_memory();
13103 for(i=0; i<nColumn; i++){
13104 azData[i] = strdup(sqlite3_column_name(pStmt,i));
13107 if( (nRow+2)*nColumn >= nAlloc ){
13109 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
13110 if( azData==0 ) shell_out_of_memory();
13113 for(i=0; i<nColumn; i++){
13114 z = (const char*)sqlite3_column_text(pStmt,i);
13115 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
13117 }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
13118 if( nColumn>p->nWidth ){
13119 p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
13120 if( p->colWidth==0 ) shell_out_of_memory();
13121 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
13122 p->nWidth = nColumn;
13123 p->actualWidth = &p->colWidth[nColumn];
13125 memset(p->actualWidth, 0, nColumn*sizeof(int));
13126 for(i=0; i<nColumn; i++){
13127 w = p->colWidth[i];
13129 p->actualWidth[i] = w;
13131 nTotal = nColumn*(nRow+1);
13132 for(i=0; i<nTotal; i++){
13134 if( z==0 ) z = p->nullValue;
13137 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
13139 if( seenInterrupt ) goto columnar_end;
13140 switch( p->cMode ){
13141 case MODE_Column: {
13144 if( p->showHeader ){
13145 for(i=0; i<nColumn; i++){
13146 w = p->actualWidth[i];
13147 if( p->colWidth[i]<0 ) w = -w;
13148 utf8_width_print(p->out, w, azData[i]);
13149 fputs(i==nColumn-1?"\n":" ", p->out);
13151 for(i=0; i<nColumn; i++){
13152 print_dashes(p->out, p->actualWidth[i]);
13153 fputs(i==nColumn-1?"\n":" ", p->out);
13161 print_row_separator(p, nColumn, "+");
13162 fputs("| ", p->out);
13163 for(i=0; i<nColumn; i++){
13164 w = p->actualWidth[i];
13165 n = strlenChar(azData[i]);
13166 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13167 fputs(i==nColumn-1?" |\n":" | ", p->out);
13169 print_row_separator(p, nColumn, "+");
13172 case MODE_Markdown: {
13175 fputs("| ", p->out);
13176 for(i=0; i<nColumn; i++){
13177 w = p->actualWidth[i];
13178 n = strlenChar(azData[i]);
13179 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13180 fputs(i==nColumn-1?" |\n":" | ", p->out);
13182 print_row_separator(p, nColumn, "|");
13186 colSep = " " BOX_13 " ";
13187 rowSep = " " BOX_13 "\n";
13188 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
13189 utf8_printf(p->out, BOX_13 " ");
13190 for(i=0; i<nColumn; i++){
13191 w = p->actualWidth[i];
13192 n = strlenChar(azData[i]);
13193 utf8_printf(p->out, "%*s%s%*s%s",
13194 (w-n)/2, "", azData[i], (w-n+1)/2, "",
13195 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
13197 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
13201 for(i=nColumn, j=0; i<nTotal; i++, j++){
13202 if( j==0 && p->cMode!=MODE_Column ){
13203 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
13206 if( z==0 ) z = p->nullValue;
13207 w = p->actualWidth[j];
13208 if( p->colWidth[j]<0 ) w = -w;
13209 utf8_width_print(p->out, w, z);
13210 if( j==nColumn-1 ){
13211 utf8_printf(p->out, "%s", rowSep);
13213 if( seenInterrupt ) goto columnar_end;
13215 utf8_printf(p->out, "%s", colSep);
13218 if( p->cMode==MODE_Table ){
13219 print_row_separator(p, nColumn, "+");
13220 }else if( p->cMode==MODE_Box ){
13221 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
13224 if( seenInterrupt ){
13225 utf8_printf(p->out, "Interrupt\n");
13227 nData = (nRow+1)*nColumn;
13228 for(i=0; i<nData; i++) free(azData[i]);
13229 sqlite3_free(azData);
13233 ** Run a prepared statement
13235 static void exec_prepared_stmt(
13236 ShellState *pArg, /* Pointer to ShellState */
13237 sqlite3_stmt *pStmt /* Statment to run */
13241 if( pArg->cMode==MODE_Column
13242 || pArg->cMode==MODE_Table
13243 || pArg->cMode==MODE_Box
13244 || pArg->cMode==MODE_Markdown
13246 exec_prepared_stmt_columnar(pArg, pStmt);
13250 /* perform the first step. this will tell us if we
13251 ** have a result set or not and how wide it is.
13253 rc = sqlite3_step(pStmt);
13254 /* if we have a result set... */
13255 if( SQLITE_ROW == rc ){
13256 /* allocate space for col name ptr, value ptr, and type */
13257 int nCol = sqlite3_column_count(pStmt);
13258 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
13262 char **azCols = (char **)pData; /* Names of result columns */
13263 char **azVals = &azCols[nCol]; /* Results */
13264 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
13266 assert(sizeof(int) <= sizeof(char *));
13267 /* save off ptrs to column names */
13268 for(i=0; i<nCol; i++){
13269 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13272 /* extract the data and data types */
13273 for(i=0; i<nCol; i++){
13274 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
13275 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
13278 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
13280 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
13282 break; /* from for */
13286 /* if data and types extracted successfully... */
13287 if( SQLITE_ROW == rc ){
13288 /* call the supplied callback with the result row data */
13289 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
13292 rc = sqlite3_step(pStmt);
13295 } while( SQLITE_ROW == rc );
13296 sqlite3_free(pData);
13297 if( pArg->cMode==MODE_Json ){
13298 fputs("]\n", pArg->out);
13304 #ifndef SQLITE_OMIT_VIRTUALTABLE
13306 ** This function is called to process SQL if the previous shell command
13307 ** was ".expert". It passes the SQL in the second argument directly to
13308 ** the sqlite3expert object.
13310 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13311 ** code. In this case, (*pzErr) may be set to point to a buffer containing
13312 ** an English language error message. It is the responsibility of the
13313 ** caller to eventually free this buffer using sqlite3_free().
13315 static int expertHandleSQL(
13316 ShellState *pState,
13320 assert( pState->expert.pExpert );
13321 assert( pzErr==0 || *pzErr==0 );
13322 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
13326 ** This function is called either to silently clean up the object
13327 ** created by the ".expert" command (if bCancel==1), or to generate a
13328 ** report from it and then clean it up (if bCancel==0).
13330 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13331 ** code. In this case, (*pzErr) may be set to point to a buffer containing
13332 ** an English language error message. It is the responsibility of the
13333 ** caller to eventually free this buffer using sqlite3_free().
13335 static int expertFinish(
13336 ShellState *pState,
13340 int rc = SQLITE_OK;
13341 sqlite3expert *p = pState->expert.pExpert;
13343 assert( bCancel || pzErr==0 || *pzErr==0 );
13345 FILE *out = pState->out;
13346 int bVerbose = pState->expert.bVerbose;
13348 rc = sqlite3_expert_analyze(p, pzErr);
13349 if( rc==SQLITE_OK ){
13350 int nQuery = sqlite3_expert_count(p);
13354 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
13355 raw_printf(out, "-- Candidates -----------------------------\n");
13356 raw_printf(out, "%s\n", zCand);
13358 for(i=0; i<nQuery; i++){
13359 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
13360 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
13361 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
13362 if( zIdx==0 ) zIdx = "(no new indexes)\n";
13364 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
13365 raw_printf(out, "%s\n\n", zSql);
13367 raw_printf(out, "%s\n", zIdx);
13368 raw_printf(out, "%s\n", zEQP);
13372 sqlite3_expert_destroy(p);
13373 pState->expert.pExpert = 0;
13378 ** Implementation of ".expert" dot command.
13380 static int expertDotCommand(
13381 ShellState *pState, /* Current shell tool state */
13382 char **azArg, /* Array of arguments passed to dot command */
13383 int nArg /* Number of entries in azArg[] */
13385 int rc = SQLITE_OK;
13390 assert( pState->expert.pExpert==0 );
13391 memset(&pState->expert, 0, sizeof(ExpertInfo));
13393 for(i=1; rc==SQLITE_OK && i<nArg; i++){
13394 char *z = azArg[i];
13396 if( z[0]=='-' && z[1]=='-' ) z++;
13398 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
13399 pState->expert.bVerbose = 1;
13401 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
13403 raw_printf(stderr, "option requires an argument: %s\n", z);
13406 iSample = (int)integerValue(azArg[++i]);
13407 if( iSample<0 || iSample>100 ){
13408 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
13414 raw_printf(stderr, "unknown option: %s\n", z);
13419 if( rc==SQLITE_OK ){
13420 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
13421 if( pState->expert.pExpert==0 ){
13422 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
13425 sqlite3_expert_config(
13426 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
13433 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13436 ** Execute a statement or set of statements. Print
13437 ** any result rows/columns depending on the current mode
13438 ** set via the supplied callback.
13440 ** This is very similar to SQLite's built-in sqlite3_exec()
13441 ** function except it takes a slightly different callback
13442 ** and callback data argument.
13444 static int shell_exec(
13445 ShellState *pArg, /* Pointer to ShellState */
13446 const char *zSql, /* SQL to be evaluated */
13447 char **pzErrMsg /* Error msg written here */
13449 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
13450 int rc = SQLITE_OK; /* Return Code */
13452 const char *zLeftover; /* Tail of unprocessed SQL */
13453 sqlite3 *db = pArg->db;
13459 #ifndef SQLITE_OMIT_VIRTUALTABLE
13460 if( pArg->expert.pExpert ){
13461 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
13462 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
13466 while( zSql[0] && (SQLITE_OK == rc) ){
13467 static const char *zStmtSql;
13468 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13469 if( SQLITE_OK != rc ){
13471 *pzErrMsg = save_err_msg(db);
13475 /* this happens for a comment or white-space */
13477 while( IsSpace(zSql[0]) ) zSql++;
13480 zStmtSql = sqlite3_sql(pStmt);
13481 if( zStmtSql==0 ) zStmtSql = "";
13482 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
13484 /* save off the prepared statment handle and reset row count */
13486 pArg->pStmt = pStmt;
13490 /* echo the sql statement if echo on */
13491 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
13492 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13495 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
13496 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
13497 sqlite3_stmt *pExplain;
13499 int triggerEQP = 0;
13500 disable_debug_trace_modes();
13501 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
13502 if( pArg->autoEQP>=AUTOEQP_trigger ){
13503 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
13505 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
13506 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13507 if( rc==SQLITE_OK ){
13508 while( sqlite3_step(pExplain)==SQLITE_ROW ){
13509 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
13510 int iEqpId = sqlite3_column_int(pExplain, 0);
13511 int iParentId = sqlite3_column_int(pExplain, 1);
13512 if( zEQPLine==0 ) zEQPLine = "";
13513 if( zEQPLine[0]=='-' ) eqp_render(pArg);
13514 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
13518 sqlite3_finalize(pExplain);
13519 sqlite3_free(zEQP);
13520 if( pArg->autoEQP>=AUTOEQP_full ){
13521 /* Also do an EXPLAIN for ".eqp full" mode */
13522 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
13523 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13524 if( rc==SQLITE_OK ){
13525 pArg->cMode = MODE_Explain;
13526 explain_data_prepare(pArg, pExplain);
13527 exec_prepared_stmt(pArg, pExplain);
13528 explain_data_delete(pArg);
13530 sqlite3_finalize(pExplain);
13531 sqlite3_free(zEQP);
13533 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
13534 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
13535 /* Reprepare pStmt before reactiving trace modes */
13536 sqlite3_finalize(pStmt);
13537 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
13538 if( pArg ) pArg->pStmt = pStmt;
13540 restore_debug_trace_modes();
13544 pArg->cMode = pArg->mode;
13545 if( pArg->autoExplain ){
13546 if( sqlite3_stmt_isexplain(pStmt)==1 ){
13547 pArg->cMode = MODE_Explain;
13549 if( sqlite3_stmt_isexplain(pStmt)==2 ){
13550 pArg->cMode = MODE_EQP;
13554 /* If the shell is currently in ".explain" mode, gather the extra
13555 ** data required to add indents to the output.*/
13556 if( pArg->cMode==MODE_Explain ){
13557 explain_data_prepare(pArg, pStmt);
13561 bind_prepared_stmt(pArg, pStmt);
13562 exec_prepared_stmt(pArg, pStmt);
13563 explain_data_delete(pArg);
13566 /* print usage stats if stats on */
13567 if( pArg && pArg->statsOn ){
13568 display_stats(db, pArg, 0);
13571 /* print loop-counters if required */
13572 if( pArg && pArg->scanstatsOn ){
13573 display_scanstats(db, pArg);
13576 /* Finalize the statement just executed. If this fails, save a
13577 ** copy of the error message. Otherwise, set zSql to point to the
13578 ** next statement to execute. */
13579 rc2 = sqlite3_finalize(pStmt);
13580 if( rc!=SQLITE_NOMEM ) rc = rc2;
13581 if( rc==SQLITE_OK ){
13583 while( IsSpace(zSql[0]) ) zSql++;
13584 }else if( pzErrMsg ){
13585 *pzErrMsg = save_err_msg(db);
13588 /* clear saved stmt handle */
13590 pArg->pStmt = NULL;
13599 ** Release memory previously allocated by tableColumnList().
13601 static void freeColumnList(char **azCol){
13603 for(i=1; azCol[i]; i++){
13604 sqlite3_free(azCol[i]);
13606 /* azCol[0] is a static string */
13607 sqlite3_free(azCol);
13611 ** Return a list of pointers to strings which are the names of all
13612 ** columns in table zTab. The memory to hold the names is dynamically
13613 ** allocated and must be released by the caller using a subsequent call
13614 ** to freeColumnList().
13616 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
13617 ** value that needs to be preserved, then azCol[0] is filled in with the
13618 ** name of the rowid column.
13620 ** The first regular column in the table is azCol[1]. The list is terminated
13621 ** by an entry with azCol[i]==0.
13623 static char **tableColumnList(ShellState *p, const char *zTab){
13625 sqlite3_stmt *pStmt;
13629 int nPK = 0; /* Number of PRIMARY KEY columns seen */
13630 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
13631 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
13634 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
13635 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13636 sqlite3_free(zSql);
13638 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13639 if( nCol>=nAlloc-2 ){
13640 nAlloc = nAlloc*2 + nCol + 10;
13641 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
13642 if( azCol==0 ) shell_out_of_memory();
13644 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
13645 if( sqlite3_column_int(pStmt, 5) ){
13648 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
13657 sqlite3_finalize(pStmt);
13658 if( azCol==0 ) return 0;
13662 /* The decision of whether or not a rowid really needs to be preserved
13663 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
13664 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
13665 ** rowids on tables where the rowid is inaccessible because there are other
13666 ** columns in the table named "rowid", "_rowid_", and "oid".
13668 if( preserveRowid && isIPK ){
13669 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
13670 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
13671 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
13672 ** ROWID aliases. To distinguish these cases, check to see if
13673 ** there is a "pk" entry in "PRAGMA index_list". There will be
13674 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
13676 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
13677 " WHERE origin='pk'", zTab);
13678 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13679 sqlite3_free(zSql);
13681 freeColumnList(azCol);
13684 rc = sqlite3_step(pStmt);
13685 sqlite3_finalize(pStmt);
13686 preserveRowid = rc==SQLITE_ROW;
13688 if( preserveRowid ){
13689 /* Only preserve the rowid if we can find a name to use for the
13691 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
13693 for(j=0; j<3; j++){
13694 for(i=1; i<=nCol; i++){
13695 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
13698 /* At this point, we know that azRowid[j] is not the name of any
13699 ** ordinary column in the table. Verify that azRowid[j] is a valid
13700 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
13701 ** tables will fail this last check */
13702 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
13703 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
13712 ** Toggle the reverse_unordered_selects setting.
13714 static void toggleSelectOrder(sqlite3 *db){
13715 sqlite3_stmt *pStmt = 0;
13718 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
13719 if( sqlite3_step(pStmt)==SQLITE_ROW ){
13720 iSetting = sqlite3_column_int(pStmt, 0);
13722 sqlite3_finalize(pStmt);
13723 sqlite3_snprintf(sizeof(zStmt), zStmt,
13724 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
13725 sqlite3_exec(db, zStmt, 0, 0, 0);
13729 ** This is a different callback routine used for dumping the database.
13730 ** Each row received by this callback consists of a table name,
13731 ** the table type ("index" or "table") and SQL to create the table.
13732 ** This routine should print text sufficient to recreate the table.
13734 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
13736 const char *zTable;
13739 ShellState *p = (ShellState *)pArg;
13743 UNUSED_PARAMETER(azNotUsed);
13744 if( nArg!=3 || azArg==0 ) return 0;
13748 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
13749 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
13751 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
13752 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
13753 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
13754 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
13755 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
13757 }else if( dataOnly ){
13759 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
13761 if( !p->writableSchema ){
13762 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
13763 p->writableSchema = 1;
13765 zIns = sqlite3_mprintf(
13766 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
13767 "VALUES('table','%q','%q',0,'%q');",
13768 zTable, zTable, zSql);
13769 utf8_printf(p->out, "%s\n", zIns);
13770 sqlite3_free(zIns);
13773 printSchemaLine(p->out, zSql, ";\n");
13776 if( strcmp(zType, "table")==0 ){
13781 char *savedDestTable;
13784 azCol = tableColumnList(p, zTable);
13790 /* Always quote the table name, even if it appears to be pure ascii,
13791 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
13793 appendText(&sTable, zTable, quoteChar(zTable));
13794 /* If preserving the rowid, add a column list after the table name.
13795 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
13796 ** instead of the usual "INSERT INTO tab VALUES(...)".
13799 appendText(&sTable, "(", 0);
13800 appendText(&sTable, azCol[0], 0);
13801 for(i=1; azCol[i]; i++){
13802 appendText(&sTable, ",", 0);
13803 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
13805 appendText(&sTable, ")", 0);
13808 /* Build an appropriate SELECT statement */
13809 initText(&sSelect);
13810 appendText(&sSelect, "SELECT ", 0);
13812 appendText(&sSelect, azCol[0], 0);
13813 appendText(&sSelect, ",", 0);
13815 for(i=1; azCol[i]; i++){
13816 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
13818 appendText(&sSelect, ",", 0);
13821 freeColumnList(azCol);
13822 appendText(&sSelect, " FROM ", 0);
13823 appendText(&sSelect, zTable, quoteChar(zTable));
13825 savedDestTable = p->zDestTable;
13826 savedMode = p->mode;
13827 p->zDestTable = sTable.z;
13828 p->mode = p->cMode = MODE_Insert;
13829 rc = shell_exec(p, sSelect.z, 0);
13830 if( (rc&0xff)==SQLITE_CORRUPT ){
13831 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13832 toggleSelectOrder(p->db);
13833 shell_exec(p, sSelect.z, 0);
13834 toggleSelectOrder(p->db);
13836 p->zDestTable = savedDestTable;
13837 p->mode = savedMode;
13839 freeText(&sSelect);
13840 if( rc ) p->nErr++;
13846 ** Run zQuery. Use dump_callback() as the callback routine so that
13847 ** the contents of the query are output as SQL statements.
13849 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
13850 ** "ORDER BY rowid DESC" to the end.
13852 static int run_schema_dump_query(
13858 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
13859 if( rc==SQLITE_CORRUPT ){
13861 int len = strlen30(zQuery);
13862 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13864 utf8_printf(p->out, "/****** %s ******/\n", zErr);
13865 sqlite3_free(zErr);
13868 zQ2 = malloc( len+100 );
13869 if( zQ2==0 ) return rc;
13870 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
13871 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
13873 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
13875 rc = SQLITE_CORRUPT;
13877 sqlite3_free(zErr);
13884 ** Text of help messages.
13886 ** The help text for each individual command begins with a line that starts
13887 ** with ".". Subsequent lines are supplimental information.
13889 ** There must be two or more spaces between the end of the command and the
13890 ** start of the description of what that command does.
13892 static const char *(azHelp[]) = {
13893 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
13894 ".archive ... Manage SQL archives",
13895 " Each command must have exactly one of the following options:",
13896 " -c, --create Create a new archive",
13897 " -u, --update Add or update files with changed mtime",
13898 " -i, --insert Like -u but always add even if unchanged",
13899 " -t, --list List contents of archive",
13900 " -x, --extract Extract files from archive",
13901 " Optional arguments:",
13902 " -v, --verbose Print each filename as it is processed",
13903 " -f FILE, --file FILE Use archive FILE (default is current db)",
13904 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
13905 " -C DIR, --directory DIR Read/extract files from directory DIR",
13906 " -n, --dryrun Show the SQL that would have occurred",
13908 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
13909 " .ar -tf ARCHIVE # List members of ARCHIVE",
13910 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
13912 " http://sqlite.org/cli.html#sqlar_archive_support",
13914 #ifndef SQLITE_OMIT_AUTHORIZATION
13915 ".auth ON|OFF Show authorizer callbacks",
13917 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
13918 " --append Use the appendvfs",
13919 " --async Write to FILE without journal and fsync()",
13920 ".bail on|off Stop after hitting an error. Default OFF",
13921 ".binary on|off Turn binary output on or off. Default OFF",
13922 ".cd DIRECTORY Change the working directory to DIRECTORY",
13923 ".changes on|off Show number of rows changed by SQL",
13924 ".check GLOB Fail if output since .testcase does not match",
13925 ".clone NEWDB Clone data into NEWDB from the existing database",
13926 ".databases List names and files of attached databases",
13927 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
13928 ".dbinfo ?DB? Show status information about the database",
13929 ".dump ?TABLE? Render database content as SQL",
13931 " --data-only Output only INSERT statements",
13932 " --newlines Allow unescaped newline characters in output",
13933 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
13934 " --preserve-rowids Include ROWID values in the output",
13935 " TABLE is a LIKE pattern for the tables to dump",
13936 " Additional LIKE patterns can be given in subsequent arguments",
13937 ".echo on|off Turn command echo on or off",
13938 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
13940 #ifdef SQLITE_DEBUG
13941 " test Show raw EXPLAIN QUERY PLAN output",
13942 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
13944 " trigger Like \"full\" but also show trigger bytecode",
13945 ".excel Display the output of next command in spreadsheet",
13946 " --bom Put a UTF8 byte-order mark on intermediate file",
13947 ".exit ?CODE? Exit this program with return-code CODE",
13948 ".expert EXPERIMENTAL. Suggest indexes for queries",
13949 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
13950 ".filectrl CMD ... Run various sqlite3_file_control() operations",
13951 " --schema SCHEMA Use SCHEMA instead of \"main\"",
13952 " --help Show CMD details",
13953 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
13954 ".headers on|off Turn display of headers on or off",
13955 ".help ?-all? ?PATTERN? Show help text for PATTERN",
13956 ".import FILE TABLE Import data from FILE into TABLE",
13958 " --ascii Use \\037 and \\036 as column and row separators",
13959 " --csv Use , and \\n as column and row separators",
13960 " --skip N Skip the first N rows of input",
13961 " -v \"Verbose\" - increase auxiliary output",
13963 " * If TABLE does not exist, it is created. The first row of input",
13964 " determines the column names.",
13965 " * If neither --csv or --ascii are used, the input mode is derived",
13966 " from the \".mode\" output mode",
13967 " * If FILE begins with \"|\" then it is a command that generates the",
13969 #ifndef SQLITE_OMIT_TEST_CONTROL
13970 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
13972 ".indexes ?TABLE? Show names of indexes",
13973 " If TABLE is specified, only show indexes for",
13974 " tables matching TABLE using the LIKE operator.",
13975 #ifdef SQLITE_ENABLE_IOTRACE
13976 ".iotrace FILE Enable I/O diagnostic logging to FILE",
13978 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
13979 ".lint OPTIONS Report potential schema issues.",
13981 " fkey-indexes Find missing foreign key indexes",
13982 #ifndef SQLITE_OMIT_LOAD_EXTENSION
13983 ".load FILE ?ENTRY? Load an extension library",
13985 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
13986 ".mode MODE ?TABLE? Set output mode",
13987 " MODE is one of:",
13988 " ascii Columns/rows delimited by 0x1F and 0x1E",
13989 " box Tables using unicode box-drawing characters",
13990 " csv Comma-separated values",
13991 " column Output in columns. (See .width)",
13992 " html HTML <table> code",
13993 " insert SQL insert statements for TABLE",
13994 " json Results in a JSON array",
13995 " line One value per line",
13996 " list Values delimited by \"|\"",
13997 " markdown Markdown table format",
13998 " quote Escape answers as for SQL",
13999 " table ASCII-art table",
14000 " tabs Tab-separated values",
14001 " tcl TCL list elements",
14002 ".nullvalue STRING Use STRING in place of NULL values",
14003 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
14004 " If FILE begins with '|' then open as a pipe",
14005 " --bom Put a UTF8 byte-order mark at the beginning",
14006 " -e Send output to the system text editor",
14007 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
14008 #ifdef SQLITE_DEBUG
14009 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
14011 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
14013 " --append Use appendvfs to append database to the end of FILE",
14014 #ifdef SQLITE_ENABLE_DESERIALIZE
14015 " --deserialize Load into memory useing sqlite3_deserialize()",
14016 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
14017 " --maxsize N Maximum size for --hexdb or --deserialized database",
14019 " --new Initialize FILE to an empty database",
14020 " --nofollow Do not follow symbolic links",
14021 " --readonly Open FILE readonly",
14022 " --zip FILE is a ZIP archive",
14023 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
14024 " If FILE begins with '|' then open it as a pipe.",
14026 " --bom Prefix output with a UTF8 byte-order mark",
14027 " -e Send output to the system text editor",
14028 " -x Send output as CSV to a spreadsheet",
14029 ".parameter CMD ... Manage SQL parameter bindings",
14030 " clear Erase all bindings",
14031 " init Initialize the TEMP table that holds bindings",
14032 " list List the current parameter bindings",
14033 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
14034 " PARAMETER should start with one of: $ : @ ?",
14035 " unset PARAMETER Remove PARAMETER from the binding table",
14036 ".print STRING... Print literal STRING",
14037 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14038 ".progress N Invoke progress handler after every N opcodes",
14039 " --limit N Interrupt after N progress callbacks",
14040 " --once Do no more than one progress interrupt",
14041 " --quiet|-q No output except at interrupts",
14042 " --reset Reset the count for each input and interrupt",
14044 ".prompt MAIN CONTINUE Replace the standard prompts",
14045 ".quit Exit this program",
14046 ".read FILE Read input from FILE",
14047 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14048 ".recover Recover as much data as possible from corrupt db.",
14049 " --freelist-corrupt Assume the freelist is corrupt",
14050 " --recovery-db NAME Store recovery metadata in database file NAME",
14051 " --lost-and-found TABLE Alternative name for the lost-and-found table",
14052 " --no-rowids Do not attempt to recover rowid values",
14053 " that are not also INTEGER PRIMARY KEYs",
14055 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
14056 ".save FILE Write in-memory database into FILE",
14057 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
14058 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
14060 " --indent Try to pretty-print the schema",
14061 " --nosys Omit objects whose names start with \"sqlite_\"",
14062 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
14064 " --init Create a new SELFTEST table",
14065 " -v Verbose output",
14066 ".separator COL ?ROW? Change the column and row separators",
14067 #if defined(SQLITE_ENABLE_SESSION)
14068 ".session ?NAME? CMD ... Create or control sessions",
14070 " attach TABLE Attach TABLE",
14071 " changeset FILE Write a changeset into FILE",
14072 " close Close one session",
14073 " enable ?BOOLEAN? Set or query the enable bit",
14074 " filter GLOB... Reject tables matching GLOBs",
14075 " indirect ?BOOLEAN? Mark or query the indirect status",
14076 " isempty Query whether the session is empty",
14077 " list List currently open session names",
14078 " open DB NAME Open a new session on DB",
14079 " patchset FILE Write a patchset into FILE",
14080 " If ?NAME? is omitted, the first defined session is used.",
14082 ".sha3sum ... Compute a SHA3 hash of database content",
14084 " --schema Also hash the sqlite_schema table",
14085 " --sha3-224 Use the sha3-224 algorithm",
14086 " --sha3-256 Use the sha3-256 algorithm (default)",
14087 " --sha3-384 Use the sha3-384 algorithm",
14088 " --sha3-512 Use the sha3-512 algorithm",
14089 " Any other argument is a LIKE pattern for tables to hash",
14090 #ifndef SQLITE_NOHAVE_SYSTEM
14091 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
14093 ".show Show the current values for various settings",
14094 ".stats ?on|off? Show stats or turn stats on or off",
14095 #ifndef SQLITE_NOHAVE_SYSTEM
14096 ".system CMD ARGS... Run CMD ARGS... in a system shell",
14098 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
14099 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
14100 ".testctrl CMD ... Run various sqlite3_test_control() operations",
14101 " Run \".testctrl\" with no arguments for details",
14102 ".timeout MS Try opening locked tables for MS milliseconds",
14103 ".timer on|off Turn SQL timer on or off",
14104 #ifndef SQLITE_OMIT_TRACE
14105 ".trace ?OPTIONS? Output each SQL statement as it is run",
14106 " FILE Send output to FILE",
14107 " stdout Send output to stdout",
14108 " stderr Send output to stderr",
14109 " off Disable tracing",
14110 " --expanded Expand query parameters",
14111 #ifdef SQLITE_ENABLE_NORMALIZE
14112 " --normalized Normal the SQL statements",
14114 " --plain Show SQL as it is input",
14115 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
14116 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
14117 " --row Trace each row (SQLITE_TRACE_ROW)",
14118 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
14119 #endif /* SQLITE_OMIT_TRACE */
14120 #ifdef SQLITE_DEBUG
14121 ".unmodule NAME ... Unregister virtual table modules",
14122 " --allexcept Unregister everything except those named",
14124 ".vfsinfo ?AUX? Information about the top-level VFS",
14125 ".vfslist List all available VFSes",
14126 ".vfsname ?AUX? Print the name of the VFS stack",
14127 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
14128 " Negative values right-justify",
14132 ** Output help text.
14134 ** zPattern describes the set of commands for which help text is provided.
14135 ** If zPattern is NULL, then show all commands, but only give a one-line
14136 ** description of each.
14138 ** Return the number of matches.
14140 static int showHelp(FILE *out, const char *zPattern){
14146 || zPattern[0]=='0'
14147 || strcmp(zPattern,"-a")==0
14148 || strcmp(zPattern,"-all")==0
14149 || strcmp(zPattern,"--all")==0
14151 /* Show all commands, but only one line per command */
14152 if( zPattern==0 ) zPattern = "";
14153 for(i=0; i<ArraySize(azHelp); i++){
14154 if( azHelp[i][0]=='.' || zPattern[0] ){
14155 utf8_printf(out, "%s\n", azHelp[i]);
14160 /* Look for commands that for which zPattern is an exact prefix */
14161 zPat = sqlite3_mprintf(".%s*", zPattern);
14162 for(i=0; i<ArraySize(azHelp); i++){
14163 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
14164 utf8_printf(out, "%s\n", azHelp[i]);
14169 sqlite3_free(zPat);
14172 /* when zPattern is a prefix of exactly one command, then include the
14173 ** details of that command, which should begin at offset j */
14174 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
14175 utf8_printf(out, "%s\n", azHelp[j]);
14181 /* Look for commands that contain zPattern anywhere. Show the complete
14182 ** text of all commands that match. */
14183 zPat = sqlite3_mprintf("%%%s%%", zPattern);
14184 for(i=0; i<ArraySize(azHelp); i++){
14185 if( azHelp[i][0]=='.' ) j = i;
14186 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
14187 utf8_printf(out, "%s\n", azHelp[j]);
14188 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
14190 utf8_printf(out, "%s\n", azHelp[j]);
14196 sqlite3_free(zPat);
14201 /* Forward reference */
14202 static int process_input(ShellState *p);
14205 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
14206 ** and return a pointer to the buffer. The caller is responsible for freeing
14209 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
14212 ** For convenience, a nul-terminator byte is always appended to the data read
14213 ** from the file before the buffer is returned. This byte is not included in
14214 ** the final value of (*pnByte), if applicable.
14216 ** NULL is returned if any error is encountered. The final value of *pnByte
14217 ** is undefined in this case.
14219 static char *readFile(const char *zName, int *pnByte){
14220 FILE *in = fopen(zName, "rb");
14224 if( in==0 ) return 0;
14225 fseek(in, 0, SEEK_END);
14228 pBuf = sqlite3_malloc64( nIn+1 );
14229 if( pBuf==0 ){ fclose(in); return 0; }
14230 nRead = fread(pBuf, nIn, 1, in);
14233 sqlite3_free(pBuf);
14237 if( pnByte ) *pnByte = nIn;
14241 #if defined(SQLITE_ENABLE_SESSION)
14243 ** Close a single OpenSession object and release all of its associated
14246 static void session_close(OpenSession *pSession){
14248 sqlite3session_delete(pSession->p);
14249 sqlite3_free(pSession->zName);
14250 for(i=0; i<pSession->nFilter; i++){
14251 sqlite3_free(pSession->azFilter[i]);
14253 sqlite3_free(pSession->azFilter);
14254 memset(pSession, 0, sizeof(OpenSession));
14259 ** Close all OpenSession objects and release all associated resources.
14261 #if defined(SQLITE_ENABLE_SESSION)
14262 static void session_close_all(ShellState *p){
14264 for(i=0; i<p->nSession; i++){
14265 session_close(&p->aSession[i]);
14270 # define session_close_all(X)
14274 ** Implementation of the xFilter function for an open session. Omit
14275 ** any tables named by ".session filter" but let all other table through.
14277 #if defined(SQLITE_ENABLE_SESSION)
14278 static int session_filter(void *pCtx, const char *zTab){
14279 OpenSession *pSession = (OpenSession*)pCtx;
14281 for(i=0; i<pSession->nFilter; i++){
14282 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
14289 ** Try to deduce the type of file for zName based on its content. Return
14290 ** one of the SHELL_OPEN_* constants.
14292 ** If the file does not exist or is empty but its name looks like a ZIP
14293 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
14294 ** Otherwise, assume an ordinary database regardless of the filename if
14295 ** the type cannot be determined from content.
14297 int deduceDatabaseType(const char *zName, int dfltZip){
14298 FILE *f = fopen(zName, "rb");
14300 int rc = SHELL_OPEN_UNSPEC;
14303 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14304 return SHELL_OPEN_ZIPFILE;
14306 return SHELL_OPEN_NORMAL;
14309 n = fread(zBuf, 16, 1, f);
14310 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
14312 return SHELL_OPEN_NORMAL;
14314 fseek(f, -25, SEEK_END);
14315 n = fread(zBuf, 25, 1, f);
14316 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
14317 rc = SHELL_OPEN_APPENDVFS;
14319 fseek(f, -22, SEEK_END);
14320 n = fread(zBuf, 22, 1, f);
14321 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
14322 && zBuf[3]==0x06 ){
14323 rc = SHELL_OPEN_ZIPFILE;
14324 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14325 rc = SHELL_OPEN_ZIPFILE;
14332 #ifdef SQLITE_ENABLE_DESERIALIZE
14334 ** Reconstruct an in-memory database using the output from the "dbtotxt"
14335 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
14336 ** is 0, then read from standard input.
14338 static unsigned char *readHexDb(ShellState *p, int *pnData){
14339 unsigned char *a = 0;
14347 unsigned int x[16];
14349 if( p->zDbFilename ){
14350 in = fopen(p->zDbFilename, "r");
14352 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
14359 if( in==0 ) in = stdin;
14363 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
14364 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
14365 if( rc!=2 ) goto readHexDb_error;
14366 if( n<0 ) goto readHexDb_error;
14367 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
14368 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
14369 a = sqlite3_malloc( n ? n : 1 );
14371 utf8_printf(stderr, "Out of memory!\n");
14372 goto readHexDb_error;
14375 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
14376 utf8_printf(stderr, "invalid pagesize\n");
14377 goto readHexDb_error;
14379 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
14380 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
14385 if( strncmp(zLine, "| end ", 6)==0 ){
14388 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
14389 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
14390 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
14395 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
14411 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
14413 if(strncmp(zLine, "| end ", 6)==0 ) break;
14418 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
14421 #endif /* SQLITE_ENABLE_DESERIALIZE */
14424 ** Scalar function "shell_int32". The first argument to this function
14425 ** must be a blob. The second a non-negative integer. This function
14426 ** reads and returns a 32-bit big-endian integer from byte
14427 ** offset (4*<arg2>) of the blob.
14429 static void shellInt32(
14430 sqlite3_context *context,
14432 sqlite3_value **argv
14434 const unsigned char *pBlob;
14438 UNUSED_PARAMETER(argc);
14439 nBlob = sqlite3_value_bytes(argv[0]);
14440 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
14441 iInt = sqlite3_value_int(argv[1]);
14443 if( iInt>=0 && (iInt+1)*4<=nBlob ){
14444 const unsigned char *a = &pBlob[iInt*4];
14445 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
14446 + ((sqlite3_int64)a[1]<<16)
14447 + ((sqlite3_int64)a[2]<< 8)
14448 + ((sqlite3_int64)a[3]<< 0);
14449 sqlite3_result_int64(context, iVal);
14454 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
14455 ** using "..." with internal double-quote characters doubled.
14457 static void shellIdQuote(
14458 sqlite3_context *context,
14460 sqlite3_value **argv
14462 const char *zName = (const char*)sqlite3_value_text(argv[0]);
14463 UNUSED_PARAMETER(argc);
14465 char *z = sqlite3_mprintf("\"%w\"", zName);
14466 sqlite3_result_text(context, z, -1, sqlite3_free);
14471 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
14473 static void shellUSleepFunc(
14474 sqlite3_context *context,
14476 sqlite3_value **argv
14478 int sleep = sqlite3_value_int(argv[0]);
14480 sqlite3_sleep(sleep/1000);
14481 sqlite3_result_int(context, sleep);
14485 ** Scalar function "shell_escape_crnl" used by the .recover command.
14486 ** The argument passed to this function is the output of built-in
14487 ** function quote(). If the first character of the input is "'",
14488 ** indicating that the value passed to quote() was a text value,
14489 ** then this function searches the input for "\n" and "\r" characters
14490 ** and adds a wrapper similar to the following:
14492 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
14494 ** Or, if the first character of the input is not "'", then a copy
14495 ** of the input is returned.
14497 static void shellEscapeCrnl(
14498 sqlite3_context *context,
14500 sqlite3_value **argv
14502 const char *zText = (const char*)sqlite3_value_text(argv[0]);
14503 UNUSED_PARAMETER(argc);
14504 if( zText[0]=='\'' ){
14505 int nText = sqlite3_value_bytes(argv[0]);
14509 const char *zNL = 0;
14510 const char *zCR = 0;
14514 for(i=0; zText[i]; i++){
14515 if( zNL==0 && zText[i]=='\n' ){
14516 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
14517 nNL = (int)strlen(zNL);
14519 if( zCR==0 && zText[i]=='\r' ){
14520 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
14521 nCR = (int)strlen(zCR);
14527 i64 nMax = (nNL > nCR) ? nNL : nCR;
14528 i64 nAlloc = nMax * nText + (nMax+64)*2;
14529 char *zOut = (char*)sqlite3_malloc64(nAlloc);
14531 sqlite3_result_error_nomem(context);
14536 memcpy(&zOut[iOut], "replace(replace(", 16);
14539 memcpy(&zOut[iOut], "replace(", 8);
14542 for(i=0; zText[i]; i++){
14543 if( zText[i]=='\n' ){
14544 memcpy(&zOut[iOut], zNL, nNL);
14546 }else if( zText[i]=='\r' ){
14547 memcpy(&zOut[iOut], zCR, nCR);
14550 zOut[iOut] = zText[i];
14556 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14557 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
14558 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
14561 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14562 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
14563 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
14566 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
14567 sqlite3_free(zOut);
14572 sqlite3_result_value(context, argv[0]);
14575 /* Flags for open_db().
14577 ** The default behavior of open_db() is to exit(1) if the database fails to
14578 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
14579 ** but still returns without calling exit.
14581 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
14582 ** ZIP archive if the file does not exist or is empty and its name matches
14583 ** the *.zip pattern.
14585 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
14586 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
14589 ** Make sure the database is open. If it is not, then open it. If
14590 ** the database fails to open, print an error message and exit.
14592 static void open_db(ShellState *p, int openFlags){
14594 if( p->openMode==SHELL_OPEN_UNSPEC ){
14595 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
14596 p->openMode = SHELL_OPEN_NORMAL;
14598 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
14599 (openFlags & OPEN_DB_ZIPFILE)!=0);
14602 switch( p->openMode ){
14603 case SHELL_OPEN_APPENDVFS: {
14604 sqlite3_open_v2(p->zDbFilename, &p->db,
14605 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
14608 case SHELL_OPEN_HEXDB:
14609 case SHELL_OPEN_DESERIALIZE: {
14610 sqlite3_open(0, &p->db);
14613 case SHELL_OPEN_ZIPFILE: {
14614 sqlite3_open(":memory:", &p->db);
14617 case SHELL_OPEN_READONLY: {
14618 sqlite3_open_v2(p->zDbFilename, &p->db,
14619 SQLITE_OPEN_READONLY|p->openFlags, 0);
14622 case SHELL_OPEN_UNSPEC:
14623 case SHELL_OPEN_NORMAL: {
14624 sqlite3_open_v2(p->zDbFilename, &p->db,
14625 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
14630 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
14631 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
14632 p->zDbFilename, sqlite3_errmsg(p->db));
14633 if( openFlags & OPEN_DB_KEEPALIVE ){
14634 sqlite3_open(":memory:", &p->db);
14639 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14640 sqlite3_enable_load_extension(p->db, 1);
14642 sqlite3_fileio_init(p->db, 0, 0);
14643 sqlite3_shathree_init(p->db, 0, 0);
14644 sqlite3_completion_init(p->db, 0, 0);
14645 sqlite3_uint_init(p->db, 0, 0);
14646 sqlite3_decimal_init(p->db, 0, 0);
14647 sqlite3_ieee_init(p->db, 0, 0);
14648 sqlite3_series_init(p->db, 0, 0);
14649 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14650 sqlite3_dbdata_init(p->db, 0, 0);
14652 #ifdef SQLITE_HAVE_ZLIB
14653 sqlite3_zipfile_init(p->db, 0, 0);
14654 sqlite3_sqlar_init(p->db, 0, 0);
14656 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
14657 shellAddSchemaName, 0, 0);
14658 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
14659 shellModuleSchema, 0, 0);
14660 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
14661 shellPutsFunc, 0, 0);
14662 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
14663 shellEscapeCrnl, 0, 0);
14664 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
14666 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
14667 shellIdQuote, 0, 0);
14668 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
14669 shellUSleepFunc, 0, 0);
14670 #ifndef SQLITE_NOHAVE_SYSTEM
14671 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
14673 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
14676 if( p->openMode==SHELL_OPEN_ZIPFILE ){
14677 char *zSql = sqlite3_mprintf(
14678 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
14679 sqlite3_exec(p->db, zSql, 0, 0, 0);
14680 sqlite3_free(zSql);
14682 #ifdef SQLITE_ENABLE_DESERIALIZE
14684 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
14687 unsigned char *aData;
14688 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
14689 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
14691 aData = readHexDb(p, &nData);
14696 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
14697 SQLITE_DESERIALIZE_RESIZEABLE |
14698 SQLITE_DESERIALIZE_FREEONCLOSE);
14700 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
14703 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
14711 ** Attempt to close the databaes connection. Report errors.
14713 void close_db(sqlite3 *db){
14714 int rc = sqlite3_close(db);
14716 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
14717 rc, sqlite3_errmsg(db));
14721 #if HAVE_READLINE || HAVE_EDITLINE
14723 ** Readline completion callbacks
14725 static char *readline_completion_generator(const char *text, int state){
14726 static sqlite3_stmt *pStmt = 0;
14730 sqlite3_finalize(pStmt);
14731 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14732 " FROM completion(%Q) ORDER BY 1", text);
14733 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14734 sqlite3_free(zSql);
14736 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14737 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
14739 sqlite3_finalize(pStmt);
14745 static char **readline_completion(const char *zText, int iStart, int iEnd){
14746 rl_attempted_completion_over = 1;
14747 return rl_completion_matches(zText, readline_completion_generator);
14750 #elif HAVE_LINENOISE
14752 ** Linenoise completion callback
14754 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
14755 int nLine = strlen30(zLine);
14757 sqlite3_stmt *pStmt = 0;
14761 if( nLine>sizeof(zBuf)-30 ) return;
14762 if( zLine[0]=='.' || zLine[0]=='#') return;
14763 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
14764 if( i==nLine-1 ) return;
14766 memcpy(zBuf, zLine, iStart);
14767 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14768 " FROM completion(%Q,%Q) ORDER BY 1",
14769 &zLine[iStart], zLine);
14770 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14771 sqlite3_free(zSql);
14772 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
14773 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14774 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
14775 int nCompletion = sqlite3_column_bytes(pStmt, 0);
14776 if( iStart+nCompletion < sizeof(zBuf)-1 ){
14777 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
14778 linenoiseAddCompletion(lc, zBuf);
14781 sqlite3_finalize(pStmt);
14786 ** Do C-language style dequoting.
14792 ** \v -> vertical tab
14794 ** \r -> carriage return
14799 ** \NNN -> ascii character NNN in octal
14801 static void resolve_backslashes(char *z){
14804 while( *z && *z!='\\' ) z++;
14805 for(i=j=0; (c = z[i])!=0; i++, j++){
14806 if( c=='\\' && z[i+1]!=0 ){
14810 }else if( c=='b' ){
14812 }else if( c=='t' ){
14814 }else if( c=='n' ){
14816 }else if( c=='v' ){
14818 }else if( c=='f' ){
14820 }else if( c=='r' ){
14822 }else if( c=='"' ){
14824 }else if( c=='\'' ){
14826 }else if( c=='\\' ){
14828 }else if( c>='0' && c<='7' ){
14830 if( z[i+1]>='0' && z[i+1]<='7' ){
14832 c = (c<<3) + z[i] - '0';
14833 if( z[i+1]>='0' && z[i+1]<='7' ){
14835 c = (c<<3) + z[i] - '0';
14842 if( j<i ) z[j] = 0;
14846 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
14847 ** for TRUE and FALSE. Return the integer value if appropriate.
14849 static int booleanValue(const char *zArg){
14851 if( zArg[0]=='0' && zArg[1]=='x' ){
14852 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
14854 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
14856 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
14857 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
14860 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
14863 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
14869 ** Set or clear a shell flag according to a boolean value.
14871 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
14872 if( booleanValue(zArg) ){
14873 ShellSetFlag(p, mFlag);
14875 ShellClearFlag(p, mFlag);
14880 ** Close an output file, assuming it is not stderr or stdout
14882 static void output_file_close(FILE *f){
14883 if( f && f!=stdout && f!=stderr ) fclose(f);
14887 ** Try to open an output file. The names "stdout" and "stderr" are
14888 ** recognized and do the right thing. NULL is returned if the output
14889 ** filename is "off".
14891 static FILE *output_file_open(const char *zFile, int bTextMode){
14893 if( strcmp(zFile,"stdout")==0 ){
14895 }else if( strcmp(zFile, "stderr")==0 ){
14897 }else if( strcmp(zFile, "off")==0 ){
14900 f = fopen(zFile, bTextMode ? "w" : "wb");
14902 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14908 #ifndef SQLITE_OMIT_TRACE
14910 ** A routine for handling output from sqlite3_trace().
14912 static int sql_trace_callback(
14913 unsigned mType, /* The trace type */
14914 void *pArg, /* The ShellState pointer */
14915 void *pP, /* Usually a pointer to sqlite_stmt */
14916 void *pX /* Auxiliary output */
14918 ShellState *p = (ShellState*)pArg;
14919 sqlite3_stmt *pStmt;
14922 if( p->traceOut==0 ) return 0;
14923 if( mType==SQLITE_TRACE_CLOSE ){
14924 utf8_printf(p->traceOut, "-- closing database connection\n");
14927 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
14928 zSql = (const char*)pX;
14930 pStmt = (sqlite3_stmt*)pP;
14931 switch( p->eTraceType ){
14932 case SHELL_TRACE_EXPANDED: {
14933 zSql = sqlite3_expanded_sql(pStmt);
14936 #ifdef SQLITE_ENABLE_NORMALIZE
14937 case SHELL_TRACE_NORMALIZED: {
14938 zSql = sqlite3_normalized_sql(pStmt);
14943 zSql = sqlite3_sql(pStmt);
14948 if( zSql==0 ) return 0;
14949 nSql = strlen30(zSql);
14950 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
14952 case SQLITE_TRACE_ROW:
14953 case SQLITE_TRACE_STMT: {
14954 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
14957 case SQLITE_TRACE_PROFILE: {
14958 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
14959 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
14968 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
14969 ** a useful spot to set a debugger breakpoint.
14971 static void test_breakpoint(void){
14972 static int nCall = 0;
14977 ** An object used to read a CSV and other files for import.
14979 typedef struct ImportCtx ImportCtx;
14981 const char *zFile; /* Name of the input file */
14982 FILE *in; /* Read the CSV text from this input stream */
14983 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
14984 char *z; /* Accumulated text for a field */
14985 int n; /* Number of bytes in z */
14986 int nAlloc; /* Space allocated for z[] */
14987 int nLine; /* Current line number */
14988 int nRow; /* Number of rows imported */
14989 int nErr; /* Number of errors encountered */
14990 int bNotFirst; /* True if one or more bytes already read */
14991 int cTerm; /* Character that terminated the most recent field */
14992 int cColSep; /* The column separator character. (Usually ",") */
14993 int cRowSep; /* The row separator character. (Usually "\n") */
14996 /* Clean up resourced used by an ImportCtx */
14997 static void import_cleanup(ImportCtx *p){
14998 if( p->in!=0 && p->xCloser!=0 ){
15002 sqlite3_free(p->z);
15006 /* Append a single byte to z[] */
15007 static void import_append_char(ImportCtx *p, int c){
15008 if( p->n+1>=p->nAlloc ){
15009 p->nAlloc += p->nAlloc + 100;
15010 p->z = sqlite3_realloc64(p->z, p->nAlloc);
15011 if( p->z==0 ) shell_out_of_memory();
15013 p->z[p->n++] = (char)c;
15016 /* Read a single field of CSV text. Compatible with rfc4180 and extended
15017 ** with the option of having a separator other than ",".
15019 ** + Input comes from p->in.
15020 ** + Store results in p->z of length p->n. Space to hold p->z comes
15021 ** from sqlite3_malloc64().
15022 ** + Use p->cSep as the column separator. The default is ",".
15023 ** + Use p->rSep as the row separator. The default is "\n".
15024 ** + Keep track of the line number in p->nLine.
15025 ** + Store the character that terminates the field in p->cTerm. Store
15026 ** EOF on end-of-file.
15027 ** + Report syntax errors on stderr
15029 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15031 int cSep = p->cColSep;
15032 int rSep = p->cRowSep;
15035 if( c==EOF || seenInterrupt ){
15041 int startLine = p->nLine;
15046 if( c==rSep ) p->nLine++;
15053 if( (c==cSep && pc==cQuote)
15054 || (c==rSep && pc==cQuote)
15055 || (c==rSep && pc=='\r' && ppc==cQuote)
15056 || (c==EOF && pc==cQuote)
15058 do{ p->n--; }while( p->z[p->n]!=cQuote );
15062 if( pc==cQuote && c!='\r' ){
15063 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15064 p->zFile, p->nLine, cQuote);
15067 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15068 p->zFile, startLine, cQuote);
15072 import_append_char(p, c);
15077 /* If this is the first field being parsed and it begins with the
15078 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
15079 if( (c&0xff)==0xef && p->bNotFirst==0 ){
15080 import_append_char(p, c);
15082 if( (c&0xff)==0xbb ){
15083 import_append_char(p, c);
15085 if( (c&0xff)==0xbf ){
15088 return csv_read_one_field(p);
15092 while( c!=EOF && c!=cSep && c!=rSep ){
15093 import_append_char(p, c);
15098 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
15102 if( p->z ) p->z[p->n] = 0;
15107 /* Read a single field of ASCII delimited text.
15109 ** + Input comes from p->in.
15110 ** + Store results in p->z of length p->n. Space to hold p->z comes
15111 ** from sqlite3_malloc64().
15112 ** + Use p->cSep as the column separator. The default is "\x1F".
15113 ** + Use p->rSep as the row separator. The default is "\x1E".
15114 ** + Keep track of the row number in p->nLine.
15115 ** + Store the character that terminates the field in p->cTerm. Store
15116 ** EOF on end-of-file.
15117 ** + Report syntax errors on stderr
15119 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
15121 int cSep = p->cColSep;
15122 int rSep = p->cRowSep;
15125 if( c==EOF || seenInterrupt ){
15129 while( c!=EOF && c!=cSep && c!=rSep ){
15130 import_append_char(p, c);
15137 if( p->z ) p->z[p->n] = 0;
15142 ** Try to transfer data for table zTable. If an error is seen while
15143 ** moving forward, try to go backwards. The backwards movement won't
15144 ** work for WITHOUT ROWID tables.
15146 static void tryToCloneData(
15151 sqlite3_stmt *pQuery = 0;
15152 sqlite3_stmt *pInsert = 0;
15157 int nTable = strlen30(zTable);
15160 const int spinRate = 10000;
15162 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
15163 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15165 utf8_printf(stderr, "Error %d: %s on [%s]\n",
15166 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15168 goto end_data_xfer;
15170 n = sqlite3_column_count(pQuery);
15171 zInsert = sqlite3_malloc64(200 + nTable + n*3);
15172 if( zInsert==0 ) shell_out_of_memory();
15173 sqlite3_snprintf(200+nTable,zInsert,
15174 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
15175 i = strlen30(zInsert);
15176 for(j=1; j<n; j++){
15177 memcpy(zInsert+i, ",?", 2);
15180 memcpy(zInsert+i, ");", 3);
15181 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
15183 utf8_printf(stderr, "Error %d: %s on [%s]\n",
15184 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
15186 goto end_data_xfer;
15188 for(k=0; k<2; k++){
15189 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15190 for(i=0; i<n; i++){
15191 switch( sqlite3_column_type(pQuery, i) ){
15192 case SQLITE_NULL: {
15193 sqlite3_bind_null(pInsert, i+1);
15196 case SQLITE_INTEGER: {
15197 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
15200 case SQLITE_FLOAT: {
15201 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
15204 case SQLITE_TEXT: {
15205 sqlite3_bind_text(pInsert, i+1,
15206 (const char*)sqlite3_column_text(pQuery,i),
15207 -1, SQLITE_STATIC);
15210 case SQLITE_BLOB: {
15211 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
15212 sqlite3_column_bytes(pQuery,i),
15218 rc = sqlite3_step(pInsert);
15219 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
15220 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
15221 sqlite3_errmsg(newDb));
15223 sqlite3_reset(pInsert);
15225 if( (cnt%spinRate)==0 ){
15226 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
15230 if( rc==SQLITE_DONE ) break;
15231 sqlite3_finalize(pQuery);
15232 sqlite3_free(zQuery);
15233 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
15235 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15237 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
15240 } /* End for(k=0...) */
15243 sqlite3_finalize(pQuery);
15244 sqlite3_finalize(pInsert);
15245 sqlite3_free(zQuery);
15246 sqlite3_free(zInsert);
15251 ** Try to transfer all rows of the schema that match zWhere. For
15252 ** each row, invoke xForEach() on the object defined by that row.
15253 ** If an error is encountered while moving forward through the
15254 ** sqlite_schema table, try again moving backwards.
15256 static void tryToCloneSchema(
15259 const char *zWhere,
15260 void (*xForEach)(ShellState*,sqlite3*,const char*)
15262 sqlite3_stmt *pQuery = 0;
15265 const unsigned char *zName;
15266 const unsigned char *zSql;
15269 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15270 " WHERE %s", zWhere);
15271 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15273 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15274 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15276 goto end_schema_xfer;
15278 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15279 zName = sqlite3_column_text(pQuery, 0);
15280 zSql = sqlite3_column_text(pQuery, 1);
15281 printf("%s... ", zName); fflush(stdout);
15282 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15284 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15285 sqlite3_free(zErrMsg);
15289 xForEach(p, newDb, (const char*)zName);
15293 if( rc!=SQLITE_DONE ){
15294 sqlite3_finalize(pQuery);
15295 sqlite3_free(zQuery);
15296 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15297 " WHERE %s ORDER BY rowid DESC", zWhere);
15298 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15300 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15301 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15303 goto end_schema_xfer;
15305 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15306 zName = sqlite3_column_text(pQuery, 0);
15307 zSql = sqlite3_column_text(pQuery, 1);
15308 printf("%s... ", zName); fflush(stdout);
15309 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15311 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15312 sqlite3_free(zErrMsg);
15316 xForEach(p, newDb, (const char*)zName);
15322 sqlite3_finalize(pQuery);
15323 sqlite3_free(zQuery);
15327 ** Open a new database file named "zNewDb". Try to recover as much information
15328 ** as possible out of the main database (which might be corrupt) and write it
15331 static void tryToClone(ShellState *p, const char *zNewDb){
15333 sqlite3 *newDb = 0;
15334 if( access(zNewDb,0)==0 ){
15335 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
15338 rc = sqlite3_open(zNewDb, &newDb);
15340 utf8_printf(stderr, "Cannot create output database: %s\n",
15341 sqlite3_errmsg(newDb));
15343 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
15344 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
15345 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
15346 tryToCloneSchema(p, newDb, "type!='table'", 0);
15347 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
15348 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15354 ** Change the output file back to stdout.
15356 ** If the p->doXdgOpen flag is set, that means the output was being
15357 ** redirected to a temporary file named by p->zTempFile. In that case,
15358 ** launch start/open/xdg-open on that temporary file.
15360 static void output_reset(ShellState *p){
15361 if( p->outfile[0]=='|' ){
15362 #ifndef SQLITE_OMIT_POPEN
15366 output_file_close(p->out);
15367 #ifndef SQLITE_NOHAVE_SYSTEM
15368 if( p->doXdgOpen ){
15369 const char *zXdgOpenCmd =
15370 #if defined(_WIN32)
15372 #elif defined(__APPLE__)
15378 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
15379 if( system(zCmd) ){
15380 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
15382 /* Give the start/open/xdg-open command some time to get
15383 ** going before we continue, and potential delete the
15384 ** p->zTempFile data file out from under it */
15385 sqlite3_sleep(2000);
15387 sqlite3_free(zCmd);
15391 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15398 ** Run an SQL command and return the single integer result.
15400 static int db_int(ShellState *p, const char *zSql){
15401 sqlite3_stmt *pStmt;
15403 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15404 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
15405 res = sqlite3_column_int(pStmt,0);
15407 sqlite3_finalize(pStmt);
15412 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
15414 static unsigned int get2byteInt(unsigned char *a){
15415 return (a[0]<<8) + a[1];
15417 static unsigned int get4byteInt(unsigned char *a){
15418 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
15422 ** Implementation of the ".dbinfo" command.
15424 ** Return 1 on error, 2 to exit, and 0 otherwise.
15426 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
15427 static const struct { const char *zName; int ofst; } aField[] = {
15428 { "file change counter:", 24 },
15429 { "database page count:", 28 },
15430 { "freelist page count:", 36 },
15431 { "schema cookie:", 40 },
15432 { "schema format:", 44 },
15433 { "default cache size:", 48 },
15434 { "autovacuum top root:", 52 },
15435 { "incremental vacuum:", 64 },
15436 { "text encoding:", 56 },
15437 { "user version:", 60 },
15438 { "application id:", 68 },
15439 { "software version:", 96 },
15441 static const struct { const char *zName; const char *zSql; } aQuery[] = {
15442 { "number of tables:",
15443 "SELECT count(*) FROM %s WHERE type='table'" },
15444 { "number of indexes:",
15445 "SELECT count(*) FROM %s WHERE type='index'" },
15446 { "number of triggers:",
15447 "SELECT count(*) FROM %s WHERE type='trigger'" },
15448 { "number of views:",
15449 "SELECT count(*) FROM %s WHERE type='view'" },
15451 "SELECT total(length(sql)) FROM %s" },
15454 unsigned iDataVersion;
15456 char *zDb = nArg>=2 ? azArg[1] : "main";
15457 sqlite3_stmt *pStmt = 0;
15458 unsigned char aHdr[100];
15460 if( p->db==0 ) return 1;
15461 rc = sqlite3_prepare_v2(p->db,
15462 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
15465 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
15466 sqlite3_finalize(pStmt);
15469 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
15470 if( sqlite3_step(pStmt)==SQLITE_ROW
15471 && sqlite3_column_bytes(pStmt,0)>100
15473 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
15474 sqlite3_finalize(pStmt);
15476 raw_printf(stderr, "unable to read database header\n");
15477 sqlite3_finalize(pStmt);
15480 i = get2byteInt(aHdr+16);
15481 if( i==1 ) i = 65536;
15482 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
15483 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
15484 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
15485 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
15486 for(i=0; i<ArraySize(aField); i++){
15487 int ofst = aField[i].ofst;
15488 unsigned int val = get4byteInt(aHdr + ofst);
15489 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
15492 if( val==1 ) raw_printf(p->out, " (utf8)");
15493 if( val==2 ) raw_printf(p->out, " (utf16le)");
15494 if( val==3 ) raw_printf(p->out, " (utf16be)");
15497 raw_printf(p->out, "\n");
15500 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
15501 }else if( strcmp(zDb,"temp")==0 ){
15502 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
15504 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
15506 for(i=0; i<ArraySize(aQuery); i++){
15507 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
15508 int val = db_int(p, zSql);
15509 sqlite3_free(zSql);
15510 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
15512 sqlite3_free(zSchemaTab);
15513 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
15514 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
15519 ** Print the current sqlite3_errmsg() value to stderr and return 1.
15521 static int shellDatabaseError(sqlite3 *db){
15522 const char *zErr = sqlite3_errmsg(db);
15523 utf8_printf(stderr, "Error: %s\n", zErr);
15528 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
15529 ** if they match and FALSE (0) if they do not match.
15533 ** '*' Matches any sequence of zero or more characters.
15535 ** '?' Matches exactly one character.
15537 ** [...] Matches one character from the enclosed list of
15540 ** [^...] Matches one character not in the enclosed list.
15542 ** '#' Matches any sequence of one or more digits with an
15543 ** optional + or - sign in front
15545 ** ' ' Any span of whitespace matches any other span of
15548 ** Extra whitespace at the end of z[] is ignored.
15550 static int testcase_glob(const char *zGlob, const char *z){
15555 while( (c = (*(zGlob++)))!=0 ){
15557 if( !IsSpace(*z) ) return 0;
15558 while( IsSpace(*zGlob) ) zGlob++;
15559 while( IsSpace(*z) ) z++;
15560 }else if( c=='*' ){
15561 while( (c=(*(zGlob++))) == '*' || c=='?' ){
15562 if( c=='?' && (*(z++))==0 ) return 0;
15566 }else if( c=='[' ){
15567 while( *z && testcase_glob(zGlob-1,z)==0 ){
15572 while( (c2 = (*(z++)))!=0 ){
15575 if( c2==0 ) return 0;
15577 if( testcase_glob(zGlob,z) ) return 1;
15580 }else if( c=='?' ){
15581 if( (*(z++))==0 ) return 0;
15582 }else if( c=='[' ){
15587 if( c==0 ) return 0;
15594 if( c==']' ) seen = 1;
15597 while( c2 && c2!=']' ){
15598 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
15600 if( c>=prior_c && c<=c2 ) seen = 1;
15610 if( c2==0 || (seen ^ invert)==0 ) return 0;
15611 }else if( c=='#' ){
15612 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
15613 if( !IsDigit(z[0]) ) return 0;
15615 while( IsDigit(z[0]) ){ z++; }
15617 if( c!=(*(z++)) ) return 0;
15620 while( IsSpace(*z) ){ z++; }
15626 ** Compare the string as a command-line option with either one or two
15627 ** initial "-" characters.
15629 static int optionMatch(const char *zStr, const char *zOpt){
15630 if( zStr[0]!='-' ) return 0;
15632 if( zStr[0]=='-' ) zStr++;
15633 return strcmp(zStr, zOpt)==0;
15639 int shellDeleteFile(const char *zFilename){
15642 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
15646 rc = unlink(zFilename);
15652 ** Try to delete the temporary file (if there is one) and free the
15653 ** memory used to hold the name of the temp file.
15655 static void clearTempFile(ShellState *p){
15656 if( p->zTempFile==0 ) return;
15657 if( p->doXdgOpen ) return;
15658 if( shellDeleteFile(p->zTempFile) ) return;
15659 sqlite3_free(p->zTempFile);
15664 ** Create a new temp file name with the given suffix.
15666 static void newTempFile(ShellState *p, const char *zSuffix){
15668 sqlite3_free(p->zTempFile);
15671 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
15673 if( p->zTempFile==0 ){
15674 /* If p->db is an in-memory database then the TEMPFILENAME file-control
15675 ** will not work and we will need to fallback to guessing */
15678 sqlite3_randomness(sizeof(r), &r);
15679 zTemp = getenv("TEMP");
15680 if( zTemp==0 ) zTemp = getenv("TMP");
15688 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
15690 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
15692 if( p->zTempFile==0 ){
15693 raw_printf(stderr, "out of memory\n");
15700 ** The implementation of SQL scalar function fkey_collate_clause(), used
15701 ** by the ".lint fkey-indexes" command. This scalar function is always
15702 ** called with four arguments - the parent table name, the parent column name,
15703 ** the child table name and the child column name.
15705 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
15707 ** If either of the named tables or columns do not exist, this function
15708 ** returns an empty string. An empty string is also returned if both tables
15709 ** and columns exist but have the same default collation sequence. Or,
15710 ** if both exist but the default collation sequences are different, this
15711 ** function returns the string " COLLATE <parent-collation>", where
15712 ** <parent-collation> is the default collation sequence of the parent column.
15714 static void shellFkeyCollateClause(
15715 sqlite3_context *pCtx,
15717 sqlite3_value **apVal
15719 sqlite3 *db = sqlite3_context_db_handle(pCtx);
15720 const char *zParent;
15721 const char *zParentCol;
15722 const char *zParentSeq;
15723 const char *zChild;
15724 const char *zChildCol;
15725 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
15729 zParent = (const char*)sqlite3_value_text(apVal[0]);
15730 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
15731 zChild = (const char*)sqlite3_value_text(apVal[2]);
15732 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
15734 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
15735 rc = sqlite3_table_column_metadata(
15736 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
15738 if( rc==SQLITE_OK ){
15739 rc = sqlite3_table_column_metadata(
15740 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
15744 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
15745 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
15746 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
15753 ** The implementation of dot-command ".lint fkey-indexes".
15755 static int lintFkeyIndexes(
15756 ShellState *pState, /* Current shell tool state */
15757 char **azArg, /* Array of arguments passed to dot command */
15758 int nArg /* Number of entries in azArg[] */
15760 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
15761 FILE *out = pState->out; /* Stream to write non-error output to */
15762 int bVerbose = 0; /* If -verbose is present */
15763 int bGroupByParent = 0; /* If -groupbyparent is present */
15764 int i; /* To iterate through azArg[] */
15765 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
15766 int rc; /* Return code */
15767 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
15770 ** This SELECT statement returns one row for each foreign key constraint
15771 ** in the schema of the main database. The column values are:
15773 ** 0. The text of an SQL statement similar to:
15775 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
15777 ** This SELECT is similar to the one that the foreign keys implementation
15778 ** needs to run internally on child tables. If there is an index that can
15779 ** be used to optimize this query, then it can also be used by the FK
15780 ** implementation to optimize DELETE or UPDATE statements on the parent
15783 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
15784 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
15785 ** contains an index that can be used to optimize the query.
15787 ** 2. Human readable text that describes the child table and columns. e.g.
15789 ** "child_table(child_key1, child_key2)"
15791 ** 3. Human readable text that describes the parent table and columns. e.g.
15793 ** "parent_table(parent_key1, parent_key2)"
15795 ** 4. A full CREATE INDEX statement for an index that could be used to
15796 ** optimize DELETE or UPDATE statements on the parent table. e.g.
15798 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
15800 ** 5. The name of the parent table.
15802 ** These six values are used by the C logic below to generate the report.
15806 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
15807 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
15808 " || fkey_collate_clause("
15809 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
15811 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
15812 " || group_concat('*=?', ' AND ') || ')'"
15814 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
15816 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
15818 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
15819 " || ' ON ' || quote(s.name) || '('"
15820 " || group_concat(quote(f.[from]) ||"
15821 " fkey_collate_clause("
15822 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
15826 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
15827 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
15828 "GROUP BY s.name, f.id "
15829 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
15831 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
15833 for(i=2; i<nArg; i++){
15834 int n = strlen30(azArg[i]);
15835 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
15838 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
15839 bGroupByParent = 1;
15843 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
15846 return SQLITE_ERROR;
15850 /* Register the fkey_collate_clause() SQL function */
15851 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
15852 0, shellFkeyCollateClause, 0, 0
15856 if( rc==SQLITE_OK ){
15857 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
15859 if( rc==SQLITE_OK ){
15860 sqlite3_bind_int(pSql, 1, bGroupByParent);
15863 if( rc==SQLITE_OK ){
15866 while( SQLITE_ROW==sqlite3_step(pSql) ){
15868 sqlite3_stmt *pExplain = 0;
15869 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
15870 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
15871 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
15872 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
15873 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
15874 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
15876 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
15877 if( rc!=SQLITE_OK ) break;
15878 if( SQLITE_ROW==sqlite3_step(pExplain) ){
15879 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
15881 0==sqlite3_strglob(zGlob, zPlan)
15882 || 0==sqlite3_strglob(zGlobIPK, zPlan)
15885 rc = sqlite3_finalize(pExplain);
15886 if( rc!=SQLITE_OK ) break;
15889 raw_printf(stderr, "Error: internal error");
15893 && (bVerbose || res==0)
15894 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
15896 raw_printf(out, "-- Parent table %s\n", zParent);
15897 sqlite3_free(zPrev);
15898 zPrev = sqlite3_mprintf("%s", zParent);
15902 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
15903 }else if( bVerbose ){
15904 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
15905 zIndent, zFrom, zTarget
15910 sqlite3_free(zPrev);
15912 if( rc!=SQLITE_OK ){
15913 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15916 rc2 = sqlite3_finalize(pSql);
15917 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
15919 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15922 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15929 ** Implementation of ".lint" dot command.
15931 static int lintDotCommand(
15932 ShellState *pState, /* Current shell tool state */
15933 char **azArg, /* Array of arguments passed to dot command */
15934 int nArg /* Number of entries in azArg[] */
15937 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
15938 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
15939 return lintFkeyIndexes(pState, azArg, nArg);
15942 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
15943 raw_printf(stderr, "Where sub-commands are:\n");
15944 raw_printf(stderr, " fkey-indexes\n");
15945 return SQLITE_ERROR;
15948 #if !defined SQLITE_OMIT_VIRTUALTABLE
15949 static void shellPrepare(
15953 sqlite3_stmt **ppStmt
15956 if( *pRc==SQLITE_OK ){
15957 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
15958 if( rc!=SQLITE_OK ){
15959 raw_printf(stderr, "sql error: %s (%d)\n",
15960 sqlite3_errmsg(db), sqlite3_errcode(db)
15968 ** Create a prepared statement using printf-style arguments for the SQL.
15970 ** This routine is could be marked "static". But it is not always used,
15971 ** depending on compile-time options. By omitting the "static", we avoid
15972 ** nuisance compiler warnings about "defined but not used".
15974 void shellPreparePrintf(
15977 sqlite3_stmt **ppStmt,
15982 if( *pRc==SQLITE_OK ){
15985 va_start(ap, zFmt);
15986 z = sqlite3_vmprintf(zFmt, ap);
15989 *pRc = SQLITE_NOMEM;
15991 shellPrepare(db, pRc, z, ppStmt);
15997 /* Finalize the prepared statement created using shellPreparePrintf().
15999 ** This routine is could be marked "static". But it is not always used,
16000 ** depending on compile-time options. By omitting the "static", we avoid
16001 ** nuisance compiler warnings about "defined but not used".
16003 void shellFinalize(
16005 sqlite3_stmt *pStmt
16008 sqlite3 *db = sqlite3_db_handle(pStmt);
16009 int rc = sqlite3_finalize(pStmt);
16010 if( *pRc==SQLITE_OK ){
16011 if( rc!=SQLITE_OK ){
16012 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16019 /* Reset the prepared statement created using shellPreparePrintf().
16021 ** This routine is could be marked "static". But it is not always used,
16022 ** depending on compile-time options. By omitting the "static", we avoid
16023 ** nuisance compiler warnings about "defined but not used".
16027 sqlite3_stmt *pStmt
16029 int rc = sqlite3_reset(pStmt);
16030 if( *pRc==SQLITE_OK ){
16031 if( rc!=SQLITE_OK ){
16032 sqlite3 *db = sqlite3_db_handle(pStmt);
16033 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16038 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16040 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16041 /******************************************************************************
16042 ** The ".archive" or ".ar" command.
16045 ** Structure representing a single ".ar" command.
16047 typedef struct ArCommand ArCommand;
16049 u8 eCmd; /* An AR_CMD_* value */
16050 u8 bVerbose; /* True if --verbose */
16051 u8 bZip; /* True if the archive is a ZIP */
16052 u8 bDryRun; /* True if --dry-run */
16053 u8 bAppend; /* True if --append */
16054 u8 fromCmdLine; /* Run from -A instead of .archive */
16055 int nArg; /* Number of command arguments */
16056 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
16057 const char *zFile; /* --file argument, or NULL */
16058 const char *zDir; /* --directory argument, or NULL */
16059 char **azArg; /* Array of command arguments */
16060 ShellState *p; /* Shell state */
16061 sqlite3 *db; /* Database containing the archive */
16065 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16067 static int arUsage(FILE *f){
16068 showHelp(f,"archive");
16069 return SQLITE_ERROR;
16073 ** Print an error message for the .ar command to stderr and return
16076 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16079 va_start(ap, zFmt);
16080 z = sqlite3_vmprintf(zFmt, ap);
16082 utf8_printf(stderr, "Error: %s\n", z);
16083 if( pAr->fromCmdLine ){
16084 utf8_printf(stderr, "Use \"-A\" for more help\n");
16086 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
16089 return SQLITE_ERROR;
16093 ** Values for ArCommand.eCmd.
16095 #define AR_CMD_CREATE 1
16096 #define AR_CMD_UPDATE 2
16097 #define AR_CMD_INSERT 3
16098 #define AR_CMD_EXTRACT 4
16099 #define AR_CMD_LIST 5
16100 #define AR_CMD_HELP 6
16103 ** Other (non-command) switches.
16105 #define AR_SWITCH_VERBOSE 7
16106 #define AR_SWITCH_FILE 8
16107 #define AR_SWITCH_DIRECTORY 9
16108 #define AR_SWITCH_APPEND 10
16109 #define AR_SWITCH_DRYRUN 11
16111 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
16113 case AR_CMD_CREATE:
16114 case AR_CMD_EXTRACT:
16116 case AR_CMD_UPDATE:
16117 case AR_CMD_INSERT:
16120 return arErrorMsg(pAr, "multiple command options");
16122 pAr->eCmd = eSwitch;
16125 case AR_SWITCH_DRYRUN:
16128 case AR_SWITCH_VERBOSE:
16131 case AR_SWITCH_APPEND:
16133 /* Fall thru into --file */
16134 case AR_SWITCH_FILE:
16137 case AR_SWITCH_DIRECTORY:
16146 ** Parse the command line for an ".ar" command. The results are written into
16147 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
16148 ** successfully, otherwise an error message is written to stderr and
16149 ** SQLITE_ERROR returned.
16151 static int arParseCommand(
16152 char **azArg, /* Array of arguments passed to dot command */
16153 int nArg, /* Number of entries in azArg[] */
16154 ArCommand *pAr /* Populate this object */
16162 { "create", 'c', AR_CMD_CREATE, 0 },
16163 { "extract", 'x', AR_CMD_EXTRACT, 0 },
16164 { "insert", 'i', AR_CMD_INSERT, 0 },
16165 { "list", 't', AR_CMD_LIST, 0 },
16166 { "update", 'u', AR_CMD_UPDATE, 0 },
16167 { "help", 'h', AR_CMD_HELP, 0 },
16168 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
16169 { "file", 'f', AR_SWITCH_FILE, 1 },
16170 { "append", 'a', AR_SWITCH_APPEND, 1 },
16171 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
16172 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
16174 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
16175 struct ArSwitch *pEnd = &aSwitch[nSwitch];
16178 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
16179 return arUsage(stderr);
16181 char *z = azArg[1];
16183 /* Traditional style [tar] invocation */
16186 for(i=0; z[i]; i++){
16187 const char *zArg = 0;
16188 struct ArSwitch *pOpt;
16189 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16190 if( z[i]==pOpt->cShort ) break;
16193 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16197 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
16199 zArg = azArg[iArg++];
16201 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16203 pAr->nArg = nArg-iArg;
16205 pAr->azArg = &azArg[iArg];
16208 /* Non-traditional invocation */
16210 for(iArg=1; iArg<nArg; iArg++){
16214 /* All remaining command line words are command arguments. */
16215 pAr->azArg = &azArg[iArg];
16216 pAr->nArg = nArg-iArg;
16223 /* One or more short options */
16224 for(i=1; i<n; i++){
16225 const char *zArg = 0;
16226 struct ArSwitch *pOpt;
16227 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16228 if( z[i]==pOpt->cShort ) break;
16231 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16238 if( iArg>=(nArg-1) ){
16239 return arErrorMsg(pAr, "option requires an argument: %c",
16242 zArg = azArg[++iArg];
16245 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16247 }else if( z[2]=='\0' ){
16248 /* A -- option, indicating that all remaining command line words
16249 ** are command arguments. */
16250 pAr->azArg = &azArg[iArg+1];
16251 pAr->nArg = nArg-iArg-1;
16254 /* A long option */
16255 const char *zArg = 0; /* Argument for option, if any */
16256 struct ArSwitch *pMatch = 0; /* Matching option */
16257 struct ArSwitch *pOpt; /* Iterator */
16258 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16259 const char *zLong = pOpt->zLong;
16260 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
16262 return arErrorMsg(pAr, "ambiguous option: %s",z);
16270 return arErrorMsg(pAr, "unrecognized option: %s", z);
16272 if( pMatch->bArg ){
16273 if( iArg>=(nArg-1) ){
16274 return arErrorMsg(pAr, "option requires an argument: %s", z);
16276 zArg = azArg[++iArg];
16278 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
16288 ** This function assumes that all arguments within the ArCommand.azArg[]
16289 ** array refer to archive members, as for the --extract or --list commands.
16290 ** It checks that each of them are present. If any specified file is not
16291 ** present in the archive, an error is printed to stderr and an error
16292 ** code returned. Otherwise, if all specified arguments are present in
16293 ** the archive, SQLITE_OK is returned.
16295 ** This function strips any trailing '/' characters from each argument.
16296 ** This is consistent with the way the [tar] command seems to work on
16299 static int arCheckEntries(ArCommand *pAr){
16300 int rc = SQLITE_OK;
16303 sqlite3_stmt *pTest = 0;
16305 shellPreparePrintf(pAr->db, &rc, &pTest,
16306 "SELECT name FROM %s WHERE name=$name",
16309 j = sqlite3_bind_parameter_index(pTest, "$name");
16310 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16311 char *z = pAr->azArg[i];
16312 int n = strlen30(z);
16314 while( n>0 && z[n-1]=='/' ) n--;
16316 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
16317 if( SQLITE_ROW==sqlite3_step(pTest) ){
16320 shellReset(&rc, pTest);
16321 if( rc==SQLITE_OK && bOk==0 ){
16322 utf8_printf(stderr, "not found in archive: %s\n", z);
16326 shellFinalize(&rc, pTest);
16332 ** Format a WHERE clause that can be used against the "sqlar" table to
16333 ** identify all archive members that match the command arguments held
16334 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
16335 ** The caller is responsible for eventually calling sqlite3_free() on
16336 ** any non-NULL (*pzWhere) value.
16338 static void arWhereClause(
16341 char **pzWhere /* OUT: New WHERE clause */
16344 if( *pRc==SQLITE_OK ){
16345 if( pAr->nArg==0 ){
16346 zWhere = sqlite3_mprintf("1");
16349 const char *zSep = "";
16350 for(i=0; i<pAr->nArg; i++){
16351 const char *z = pAr->azArg[i];
16352 zWhere = sqlite3_mprintf(
16353 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
16354 zWhere, zSep, z, strlen30(z)+1, z
16357 *pRc = SQLITE_NOMEM;
16368 ** Implementation of .ar "lisT" command.
16370 static int arListCommand(ArCommand *pAr){
16371 const char *zSql = "SELECT %s FROM %s WHERE %s";
16372 const char *azCols[] = {
16374 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
16378 sqlite3_stmt *pSql = 0;
16381 rc = arCheckEntries(pAr);
16382 arWhereClause(&rc, pAr, &zWhere);
16384 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
16385 pAr->zSrcTable, zWhere);
16386 if( pAr->bDryRun ){
16387 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16389 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16390 if( pAr->bVerbose ){
16391 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
16392 sqlite3_column_text(pSql, 0),
16393 sqlite3_column_int(pSql, 1),
16394 sqlite3_column_text(pSql, 2),
16395 sqlite3_column_text(pSql, 3)
16398 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16402 shellFinalize(&rc, pSql);
16403 sqlite3_free(zWhere);
16409 ** Implementation of .ar "eXtract" command.
16411 static int arExtractCommand(ArCommand *pAr){
16412 const char *zSql1 =
16415 " writefile(($dir || name), %s, mode, mtime) "
16416 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
16417 " AND name NOT GLOB '*..[/\\]*'";
16419 const char *azExtraArg[] = {
16420 "sqlar_uncompress(data, sz)",
16424 sqlite3_stmt *pSql = 0;
16425 int rc = SQLITE_OK;
16430 /* If arguments are specified, check that they actually exist within
16431 ** the archive before proceeding. And formulate a WHERE clause to
16433 rc = arCheckEntries(pAr);
16434 arWhereClause(&rc, pAr, &zWhere);
16436 if( rc==SQLITE_OK ){
16438 zDir = sqlite3_mprintf("%s/", pAr->zDir);
16440 zDir = sqlite3_mprintf("");
16442 if( zDir==0 ) rc = SQLITE_NOMEM;
16445 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
16446 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
16449 if( rc==SQLITE_OK ){
16450 j = sqlite3_bind_parameter_index(pSql, "$dir");
16451 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
16453 /* Run the SELECT statement twice. The first time, writefile() is called
16454 ** for all archive members that should be extracted. The second time,
16455 ** only for the directories. This is because the timestamps for
16456 ** extracted directories must be reset after they are populated (as
16457 ** populating them changes the timestamp). */
16458 for(i=0; i<2; i++){
16459 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
16460 sqlite3_bind_int(pSql, j, i);
16461 if( pAr->bDryRun ){
16462 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16464 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16465 if( i==0 && pAr->bVerbose ){
16466 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16470 shellReset(&rc, pSql);
16472 shellFinalize(&rc, pSql);
16475 sqlite3_free(zDir);
16476 sqlite3_free(zWhere);
16481 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
16483 static int arExecSql(ArCommand *pAr, const char *zSql){
16485 if( pAr->bDryRun ){
16486 utf8_printf(pAr->p->out, "%s\n", zSql);
16490 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
16492 utf8_printf(stdout, "ERROR: %s\n", zErr);
16493 sqlite3_free(zErr);
16501 ** Implementation of .ar "create", "insert", and "update" commands.
16503 ** create -> Create a new SQL archive
16504 ** insert -> Insert or reinsert all files listed
16505 ** update -> Insert files that have changed or that were not
16506 ** previously in the archive
16508 ** Create the "sqlar" table in the database if it does not already exist.
16509 ** Then add each file in the azFile[] array to the archive. Directories
16510 ** are added recursively. If argument bVerbose is non-zero, a message is
16511 ** printed on stdout for each file archived.
16513 ** The create command is the same as update, except that it drops
16514 ** any existing "sqlar" table before beginning. The "insert" command
16515 ** always overwrites every file named on the command-line, where as
16516 ** "update" only overwrites if the size or mtime or mode has changed.
16518 static int arCreateOrUpdateCommand(
16519 ArCommand *pAr, /* Command arguments and options */
16520 int bUpdate, /* true for a --create. */
16521 int bOnlyIfChanged /* Only update if file has changed */
16523 const char *zCreate =
16524 "CREATE TABLE IF NOT EXISTS sqlar(\n"
16525 " name TEXT PRIMARY KEY, -- name of the file\n"
16526 " mode INT, -- access permissions\n"
16527 " mtime INT, -- last modification time\n"
16528 " sz INT, -- original file size\n"
16529 " data BLOB -- compressed content\n"
16531 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
16532 const char *zInsertFmt[2] = {
16533 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
16538 " CASE substr(lsmode(mode),1,1)\n"
16539 " WHEN '-' THEN length(data)\n"
16540 " WHEN 'd' THEN 0\n"
16542 " sqlar_compress(data)\n"
16543 " FROM fsdir(%Q,%Q) AS disk\n"
16544 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16546 "REPLACE INTO %s(name,mode,mtime,data)\n"
16552 " FROM fsdir(%Q,%Q) AS disk\n"
16553 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16555 int i; /* For iterating through azFile[] */
16556 int rc; /* Return code */
16557 const char *zTab = 0; /* SQL table into which to insert */
16562 arExecSql(pAr, "PRAGMA page_size=512");
16563 rc = arExecSql(pAr, "SAVEPOINT ar;");
16564 if( rc!=SQLITE_OK ) return rc;
16567 /* Initialize the zipfile virtual table, if necessary */
16570 sqlite3_randomness(sizeof(r),&r);
16571 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
16573 zSql = sqlite3_mprintf(
16574 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
16577 rc = arExecSql(pAr, zSql);
16578 sqlite3_free(zSql);
16583 /* Initialize the table for an SQLAR */
16586 rc = arExecSql(pAr, zDrop);
16587 if( rc!=SQLITE_OK ) goto end_ar_transaction;
16589 rc = arExecSql(pAr, zCreate);
16591 if( bOnlyIfChanged ){
16592 zExists = sqlite3_mprintf(
16594 "SELECT 1 FROM %s AS mem"
16595 " WHERE mem.name=disk.name"
16596 " AND mem.mtime=disk.mtime"
16597 " AND mem.mode=disk.mode)", zTab);
16599 zExists = sqlite3_mprintf("");
16601 if( zExists==0 ) rc = SQLITE_NOMEM;
16602 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16603 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
16604 pAr->bVerbose ? "shell_putsnl(name)" : "name",
16605 pAr->azArg[i], pAr->zDir, zExists);
16606 rc = arExecSql(pAr, zSql2);
16607 sqlite3_free(zSql2);
16609 end_ar_transaction:
16610 if( rc!=SQLITE_OK ){
16611 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
16613 rc = arExecSql(pAr, "RELEASE ar;");
16614 if( pAr->bZip && pAr->zFile ){
16615 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
16616 arExecSql(pAr, zSql);
16617 sqlite3_free(zSql);
16620 sqlite3_free(zExists);
16625 ** Implementation of ".ar" dot command.
16627 static int arDotCommand(
16628 ShellState *pState, /* Current shell tool state */
16629 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
16630 char **azArg, /* Array of arguments passed to dot command */
16631 int nArg /* Number of entries in azArg[] */
16635 memset(&cmd, 0, sizeof(cmd));
16636 cmd.fromCmdLine = fromCmdLine;
16637 rc = arParseCommand(azArg, nArg, &cmd);
16638 if( rc==SQLITE_OK ){
16639 int eDbType = SHELL_OPEN_UNSPEC;
16641 cmd.db = pState->db;
16643 eDbType = deduceDatabaseType(cmd.zFile, 1);
16645 eDbType = pState->openMode;
16647 if( eDbType==SHELL_OPEN_ZIPFILE ){
16648 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
16649 if( cmd.zFile==0 ){
16650 cmd.zSrcTable = sqlite3_mprintf("zip");
16652 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
16656 }else if( cmd.zFile ){
16658 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
16659 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
16660 || cmd.eCmd==AR_CMD_UPDATE ){
16661 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
16663 flags = SQLITE_OPEN_READONLY;
16667 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
16668 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
16670 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
16671 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
16672 if( rc!=SQLITE_OK ){
16673 utf8_printf(stderr, "cannot open file: %s (%s)\n",
16674 cmd.zFile, sqlite3_errmsg(cmd.db)
16676 goto end_ar_command;
16678 sqlite3_fileio_init(cmd.db, 0, 0);
16679 sqlite3_sqlar_init(cmd.db, 0, 0);
16680 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
16681 shellPutsFunc, 0, 0);
16684 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
16685 if( cmd.eCmd!=AR_CMD_CREATE
16686 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
16688 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
16690 goto end_ar_command;
16692 cmd.zSrcTable = sqlite3_mprintf("sqlar");
16695 switch( cmd.eCmd ){
16696 case AR_CMD_CREATE:
16697 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
16700 case AR_CMD_EXTRACT:
16701 rc = arExtractCommand(&cmd);
16705 rc = arListCommand(&cmd);
16709 arUsage(pState->out);
16712 case AR_CMD_INSERT:
16713 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
16717 assert( cmd.eCmd==AR_CMD_UPDATE );
16718 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
16723 if( cmd.db!=pState->db ){
16726 sqlite3_free(cmd.zSrcTable);
16730 /* End of the ".archive" or ".ar" command logic
16731 *******************************************************************************/
16732 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
16734 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16736 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
16737 ** Otherwise, the SQL statement or statements in zSql are executed using
16738 ** database connection db and the error code written to *pRc before
16739 ** this function returns.
16741 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
16743 if( rc==SQLITE_OK ){
16745 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16746 if( rc!=SQLITE_OK ){
16747 raw_printf(stderr, "SQL error: %s\n", zErr);
16754 ** Like shellExec(), except that zFmt is a printf() style format string.
16756 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
16758 if( *pRc==SQLITE_OK ){
16760 va_start(ap, zFmt);
16761 z = sqlite3_vmprintf(zFmt, ap);
16764 *pRc = SQLITE_NOMEM;
16766 shellExec(db, pRc, z);
16773 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16774 ** Otherwise, an attempt is made to allocate, zero and return a pointer
16775 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
16776 ** to SQLITE_NOMEM and NULL returned.
16778 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
16780 if( *pRc==SQLITE_OK ){
16781 pRet = sqlite3_malloc64(nByte);
16783 *pRc = SQLITE_NOMEM;
16785 memset(pRet, 0, nByte);
16792 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16793 ** Otherwise, zFmt is treated as a printf() style string. The result of
16794 ** formatting it along with any trailing arguments is written into a
16795 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
16796 ** It is the responsibility of the caller to eventually free this buffer
16797 ** using a call to sqlite3_free().
16799 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
16800 ** pointer returned.
16802 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
16804 if( *pRc==SQLITE_OK ){
16806 va_start(ap, zFmt);
16807 z = sqlite3_vmprintf(zFmt, ap);
16810 *pRc = SQLITE_NOMEM;
16817 ** When running the ".recover" command, each output table, and the special
16818 ** orphaned row table if it is required, is represented by an instance
16819 ** of the following struct.
16821 typedef struct RecoverTable RecoverTable;
16822 struct RecoverTable {
16823 char *zQuoted; /* Quoted version of table name */
16824 int nCol; /* Number of columns in table */
16825 char **azlCol; /* Array of column lists */
16826 int iPk; /* Index of IPK column */
16830 ** Free a RecoverTable object allocated by recoverFindTable() or
16831 ** recoverOrphanTable().
16833 static void recoverFreeTable(RecoverTable *pTab){
16835 sqlite3_free(pTab->zQuoted);
16836 if( pTab->azlCol ){
16838 for(i=0; i<=pTab->nCol; i++){
16839 sqlite3_free(pTab->azlCol[i]);
16841 sqlite3_free(pTab->azlCol);
16843 sqlite3_free(pTab);
16848 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
16849 ** Otherwise, it allocates and returns a RecoverTable object based on the
16850 ** final four arguments passed to this function. It is the responsibility
16851 ** of the caller to eventually free the returned object using
16852 ** recoverFreeTable().
16854 static RecoverTable *recoverNewTable(
16855 int *pRc, /* IN/OUT: Error code */
16856 const char *zName, /* Name of table */
16857 const char *zSql, /* CREATE TABLE statement */
16861 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
16863 RecoverTable *pTab = 0;
16865 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
16866 if( rc==SQLITE_OK ){
16868 int bSqlIntkey = 0;
16869 sqlite3_stmt *pStmt = 0;
16871 rc = sqlite3_open("", &dbtmp);
16872 if( rc==SQLITE_OK ){
16873 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
16874 shellIdQuote, 0, 0);
16876 if( rc==SQLITE_OK ){
16877 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
16879 if( rc==SQLITE_OK ){
16880 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
16881 if( rc==SQLITE_ERROR ){
16886 shellPreparePrintf(dbtmp, &rc, &pStmt,
16887 "SELECT count(*) FROM pragma_table_info(%Q)", zName
16889 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16890 nSqlCol = sqlite3_column_int(pStmt, 0);
16892 shellFinalize(&rc, pStmt);
16894 if( rc!=SQLITE_OK || nSqlCol<nCol ){
16898 shellPreparePrintf(dbtmp, &rc, &pStmt,
16900 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
16901 ") FROM sqlite_schema WHERE name = %Q", zName
16903 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16904 bSqlIntkey = sqlite3_column_int(pStmt, 0);
16906 shellFinalize(&rc, pStmt);
16908 if( bIntkey==bSqlIntkey ){
16910 const char *zPk = "_rowid_";
16911 sqlite3_stmt *pPkFinder = 0;
16913 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
16914 ** set zPk to the name of the PK column, and pTab->iPk to the index
16915 ** of the column, where columns are 0-numbered from left to right.
16916 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
16917 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
16920 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
16921 "SELECT cid, name FROM pragma_table_info(%Q) "
16922 " WHERE pk=1 AND type='integer' COLLATE nocase"
16923 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
16926 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
16927 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
16928 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
16932 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
16933 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
16934 pTab->nCol = nSqlCol;
16937 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
16939 pTab->azlCol[0] = shellMPrintf(&rc, "");
16942 shellPreparePrintf(dbtmp, &rc, &pStmt,
16943 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
16944 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
16945 "FROM pragma_table_info(%Q)",
16946 bIntkey ? ", " : "", pTab->iPk,
16947 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
16950 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16951 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
16952 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
16955 shellFinalize(&rc, pStmt);
16957 shellFinalize(&rc, pPkFinder);
16962 sqlite3_close(dbtmp);
16964 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
16965 recoverFreeTable(pTab);
16972 ** This function is called to search the schema recovered from the
16973 ** sqlite_schema table of the (possibly) corrupt database as part
16974 ** of a ".recover" command. Specifically, for a table with root page
16975 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
16976 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
16979 ** If a table is found, a (RecoverTable*) object is returned. Or, if
16980 ** no such table is found, but bIntkey is false and iRoot is the
16981 ** root page of an index in the recovered schema, then (*pbNoop) is
16982 ** set to true and NULL returned. Or, if there is no such table or
16983 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
16984 ** the caller should write data to the orphans table.
16986 static RecoverTable *recoverFindTable(
16987 ShellState *pState, /* Shell state object */
16988 int *pRc, /* IN/OUT: Error code */
16989 int iRoot, /* Root page of table */
16990 int bIntkey, /* True for an intkey table */
16991 int nCol, /* Number of columns in table */
16992 int *pbNoop /* OUT: True if iRoot is root of index */
16994 sqlite3_stmt *pStmt = 0;
16995 RecoverTable *pRet = 0;
16997 const char *zSql = 0;
16998 const char *zName = 0;
17000 /* Search the recovered schema for an object with root page iRoot. */
17001 shellPreparePrintf(pState->db, pRc, &pStmt,
17002 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17004 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17005 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17006 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17010 if( sqlite3_stricmp(zType, "table")==0 ){
17011 zName = (const char*)sqlite3_column_text(pStmt, 1);
17012 zSql = (const char*)sqlite3_column_text(pStmt, 2);
17013 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17018 shellFinalize(pRc, pStmt);
17024 ** Return a RecoverTable object representing the orphans table.
17026 static RecoverTable *recoverOrphanTable(
17027 ShellState *pState, /* Shell state object */
17028 int *pRc, /* IN/OUT: Error code */
17029 const char *zLostAndFound, /* Base name for orphans table */
17030 int nCol /* Number of user data columns */
17032 RecoverTable *pTab = 0;
17033 if( nCol>=0 && *pRc==SQLITE_OK ){
17036 /* This block determines the name of the orphan table. The prefered
17037 ** name is zLostAndFound. But if that clashes with another name
17038 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17039 ** and so on until a non-clashing name is found. */
17041 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17042 sqlite3_stmt *pTest = 0;
17043 shellPrepare(pState->db, pRc,
17044 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17046 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17047 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17048 shellReset(pRc, pTest);
17049 sqlite3_free(zTab);
17050 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17051 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17053 shellFinalize(pRc, pTest);
17055 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17057 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17061 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17062 if( pTab->azlCol ){
17063 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17064 for(i=nCol-1; i>=0; i--){
17065 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17070 if( *pRc!=SQLITE_OK ){
17071 recoverFreeTable(pTab);
17074 raw_printf(pState->out,
17075 "CREATE TABLE %s(rootpgno INTEGER, "
17076 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17078 for(i=0; i<nCol; i++){
17079 raw_printf(pState->out, ", c%d", i);
17081 raw_printf(pState->out, ");\n");
17084 sqlite3_free(zTab);
17090 ** This function is called to recover data from the database. A script
17091 ** to construct a new database containing all recovered data is output
17092 ** on stream pState->out.
17094 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
17095 int rc = SQLITE_OK;
17096 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
17097 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
17098 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
17099 const char *zRecoveryDb = ""; /* Name of "recovery" database */
17100 const char *zLostAndFound = "lost_and_found";
17103 RecoverTable *pOrphan = 0;
17105 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
17106 int bRowids = 1; /* 0 if --no-rowids */
17107 for(i=1; i<nArg; i++){
17108 char *z = azArg[i];
17110 if( z[0]=='-' && z[1]=='-' ) z++;
17112 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
17115 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
17117 zRecoveryDb = azArg[i];
17119 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
17121 zLostAndFound = azArg[i];
17123 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
17127 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
17128 showHelp(pState->out, azArg[0]);
17133 shellExecPrintf(pState->db, &rc,
17134 /* Attach an in-memory database named 'recovery'. Create an indexed
17135 ** cache of the sqlite_dbptr virtual table. */
17136 "PRAGMA writable_schema = on;"
17137 "ATTACH %Q AS recovery;"
17138 "DROP TABLE IF EXISTS recovery.dbptr;"
17139 "DROP TABLE IF EXISTS recovery.freelist;"
17140 "DROP TABLE IF EXISTS recovery.map;"
17141 "DROP TABLE IF EXISTS recovery.schema;"
17142 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
17146 shellExec(pState->db, &rc,
17147 "WITH trunk(pgno) AS ("
17148 " SELECT shell_int32("
17149 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
17152 " SELECT shell_int32("
17153 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
17154 " FROM trunk WHERE x>0"
17156 "freelist(data, n, freepgno) AS ("
17157 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
17158 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
17160 " SELECT data, n-1, shell_int32(data, 2+n) "
17161 " FROM freelist WHERE n>=0"
17163 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
17167 /* If this is an auto-vacuum database, add all pointer-map pages to
17168 ** the freelist table. Do this regardless of whether or not
17169 ** --freelist-corrupt was specified. */
17170 shellExec(pState->db, &rc,
17171 "WITH ptrmap(pgno) AS ("
17172 " SELECT 2 WHERE shell_int32("
17173 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
17176 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
17177 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
17179 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
17182 shellExec(pState->db, &rc,
17183 "CREATE TABLE recovery.dbptr("
17184 " pgno, child, PRIMARY KEY(child, pgno)"
17186 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
17187 " SELECT * FROM sqlite_dbptr"
17188 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
17190 /* Delete any pointer to page 1. This ensures that page 1 is considered
17191 ** a root page, regardless of how corrupt the db is. */
17192 "DELETE FROM recovery.dbptr WHERE child = 1;"
17194 /* Delete all pointers to any pages that have more than one pointer
17195 ** to them. Such pages will be treated as root pages when recovering
17197 "DELETE FROM recovery.dbptr WHERE child IN ("
17198 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
17201 /* Create the "map" table that will (eventually) contain instructions
17202 ** for dealing with each page in the db that contains one or more
17204 "CREATE TABLE recovery.map("
17205 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
17208 /* Populate table [map]. If there are circular loops of pages in the
17209 ** database, the following adds all pages in such a loop to the map
17210 ** as individual root pages. This could be handled better. */
17211 "WITH pages(i, maxlen) AS ("
17212 " SELECT page_count, ("
17213 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
17214 " ) FROM pragma_page_count WHERE page_count>0"
17217 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
17218 " ) FROM pages WHERE i>=2"
17220 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
17221 " SELECT i, maxlen, NULL, ("
17222 " WITH p(orig, pgno, parent) AS ("
17223 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
17225 " SELECT i, p.parent, "
17226 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
17228 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
17230 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
17231 "UPDATE recovery.map AS o SET intkey = ("
17232 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
17235 /* Extract data from page 1 and any linked pages into table
17236 ** recovery.schema. With the same schema as an sqlite_schema table. */
17237 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
17238 "INSERT INTO recovery.schema SELECT "
17239 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
17240 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
17241 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
17242 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
17243 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
17244 "FROM sqlite_dbdata WHERE pgno IN ("
17245 " SELECT pgno FROM recovery.map WHERE root=1"
17247 "GROUP BY pgno, cell;"
17248 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
17251 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
17252 ** CREATE TABLE statements that extracted from the existing schema. */
17253 if( rc==SQLITE_OK ){
17254 sqlite3_stmt *pStmt = 0;
17255 /* ".recover" might output content in an order which causes immediate
17256 ** foreign key constraints to be violated. So disable foreign-key
17257 ** constraint enforcement to prevent problems when running the output
17259 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
17260 raw_printf(pState->out, "BEGIN;\n");
17261 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
17262 shellPrepare(pState->db, &rc,
17263 "SELECT sql FROM recovery.schema "
17264 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
17266 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17267 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
17268 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
17272 shellFinalize(&rc, pStmt);
17275 /* Figure out if an orphan table will be required. And if so, how many
17276 ** user columns it should contain */
17277 shellPrepare(pState->db, &rc,
17278 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
17281 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17282 nOrphan = sqlite3_column_int(pLoop, 0);
17284 shellFinalize(&rc, pLoop);
17287 shellPrepare(pState->db, &rc,
17288 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
17291 shellPrepare(pState->db, &rc,
17292 "SELECT max(field), group_concat(shell_escape_crnl(quote"
17293 "(case when (? AND field<0) then NULL else value end)"
17296 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
17297 "GROUP BY cell", &pCells
17300 /* Loop through each root page. */
17301 shellPrepare(pState->db, &rc,
17302 "SELECT root, intkey, max(maxlen) FROM recovery.map"
17303 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
17304 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
17307 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17308 int iRoot = sqlite3_column_int(pLoop, 0);
17309 int bIntkey = sqlite3_column_int(pLoop, 1);
17310 int nCol = sqlite3_column_int(pLoop, 2);
17312 RecoverTable *pTab;
17314 assert( bIntkey==0 || bIntkey==1 );
17315 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
17316 if( bNoop || rc ) continue;
17319 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17322 if( pTab==0 ) break;
17325 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
17326 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
17328 sqlite3_bind_int(pPages, 1, iRoot);
17329 if( bRowids==0 && pTab->iPk<0 ){
17330 sqlite3_bind_int(pCells, 1, 1);
17332 sqlite3_bind_int(pCells, 1, 0);
17334 sqlite3_bind_int(pCells, 3, pTab->iPk);
17336 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
17337 int iPgno = sqlite3_column_int(pPages, 0);
17338 sqlite3_bind_int(pCells, 2, iPgno);
17339 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
17340 int nField = sqlite3_column_int(pCells, 0);
17341 int iMin = sqlite3_column_int(pCells, 2);
17342 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
17344 RecoverTable *pTab2 = pTab;
17345 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
17347 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17350 if( pTab2==0 ) break;
17354 if( pTab2==pOrphan ){
17355 raw_printf(pState->out,
17356 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
17357 pTab2->zQuoted, iRoot, iPgno, nField,
17358 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
17361 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
17362 pTab2->zQuoted, pTab2->azlCol[nField], zVal
17366 shellReset(&rc, pCells);
17368 shellReset(&rc, pPages);
17369 if( pTab!=pOrphan ) recoverFreeTable(pTab);
17371 shellFinalize(&rc, pLoop);
17372 shellFinalize(&rc, pPages);
17373 shellFinalize(&rc, pCells);
17374 recoverFreeTable(pOrphan);
17376 /* The rest of the schema */
17377 if( rc==SQLITE_OK ){
17378 sqlite3_stmt *pStmt = 0;
17379 shellPrepare(pState->db, &rc,
17380 "SELECT sql, name FROM recovery.schema "
17381 "WHERE sql NOT LIKE 'create table%'", &pStmt
17383 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17384 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
17385 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
17386 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
17387 char *zPrint = shellMPrintf(&rc,
17388 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
17391 raw_printf(pState->out, "%s;\n", zPrint);
17392 sqlite3_free(zPrint);
17394 raw_printf(pState->out, "%s;\n", zSql);
17397 shellFinalize(&rc, pStmt);
17400 if( rc==SQLITE_OK ){
17401 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
17402 raw_printf(pState->out, "COMMIT;\n");
17404 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
17407 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17411 ** If an input line begins with "." then invoke this routine to
17412 ** process that line.
17414 ** Return 1 on error, 2 to exit, and 0 otherwise.
17416 static int do_meta_command(char *zLine, ShellState *p){
17423 #ifndef SQLITE_OMIT_VIRTUALTABLE
17424 if( p->expert.pExpert ){
17425 expertFinish(p, 1, 0);
17429 /* Parse the input line into tokens.
17431 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
17432 while( IsSpace(zLine[h]) ){ h++; }
17433 if( zLine[h]==0 ) break;
17434 if( zLine[h]=='\'' || zLine[h]=='"' ){
17435 int delim = zLine[h++];
17436 azArg[nArg++] = &zLine[h];
17437 while( zLine[h] && zLine[h]!=delim ){
17438 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
17441 if( zLine[h]==delim ){
17444 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
17446 azArg[nArg++] = &zLine[h];
17447 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
17448 if( zLine[h] ) zLine[h++] = 0;
17449 resolve_backslashes(azArg[nArg-1]);
17454 /* Process the input line.
17456 if( nArg==0 ) return 0; /* no tokens, no error */
17457 n = strlen30(azArg[0]);
17461 #ifndef SQLITE_OMIT_AUTHORIZATION
17462 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
17464 raw_printf(stderr, "Usage: .auth ON|OFF\n");
17466 goto meta_command_exit;
17469 if( booleanValue(azArg[1]) ){
17470 sqlite3_set_authorizer(p->db, shellAuth, p);
17472 sqlite3_set_authorizer(p->db, 0, 0);
17477 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17478 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
17480 rc = arDotCommand(p, 0, azArg, nArg);
17484 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
17485 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
17487 const char *zDestFile = 0;
17488 const char *zDb = 0;
17490 sqlite3_backup *pBackup;
17493 const char *zVfs = 0;
17494 for(j=1; j<nArg; j++){
17495 const char *z = azArg[j];
17497 if( z[1]=='-' ) z++;
17498 if( strcmp(z, "-append")==0 ){
17501 if( strcmp(z, "-async")==0 ){
17505 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
17508 }else if( zDestFile==0 ){
17509 zDestFile = azArg[j];
17510 }else if( zDb==0 ){
17512 zDestFile = azArg[j];
17514 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
17518 if( zDestFile==0 ){
17519 raw_printf(stderr, "missing FILENAME argument on .backup\n");
17522 if( zDb==0 ) zDb = "main";
17523 rc = sqlite3_open_v2(zDestFile, &pDest,
17524 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
17525 if( rc!=SQLITE_OK ){
17526 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
17531 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
17535 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
17537 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17541 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
17542 sqlite3_backup_finish(pBackup);
17543 if( rc==SQLITE_DONE ){
17546 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17552 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
17554 bail_on_error = booleanValue(azArg[1]);
17556 raw_printf(stderr, "Usage: .bail on|off\n");
17561 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
17563 if( booleanValue(azArg[1]) ){
17564 setBinaryMode(p->out, 1);
17566 setTextMode(p->out, 1);
17569 raw_printf(stderr, "Usage: .binary on|off\n");
17574 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
17576 #if defined(_WIN32) || defined(WIN32)
17577 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
17578 rc = !SetCurrentDirectoryW(z);
17581 rc = chdir(azArg[1]);
17584 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
17588 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
17593 /* The undocumented ".breakpoint" command causes a call to the no-op
17594 ** routine named test_breakpoint().
17596 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
17600 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
17602 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
17604 raw_printf(stderr, "Usage: .changes on|off\n");
17609 /* Cancel output redirection, if it is currently set (by .testcase)
17610 ** Then read the content of the testcase-out.txt file and compare against
17611 ** azArg[1]. If there are differences, report an error and exit.
17613 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
17617 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
17619 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
17620 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
17622 }else if( testcase_glob(azArg[1],zRes)==0 ){
17623 utf8_printf(stderr,
17624 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
17625 p->zTestcase, azArg[1], zRes);
17628 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
17631 sqlite3_free(zRes);
17634 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
17636 tryToClone(p, azArg[1]);
17638 raw_printf(stderr, "Usage: .clone FILENAME\n");
17643 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
17646 sqlite3_stmt *pStmt;
17649 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17651 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17654 while( sqlite3_step(pStmt)==SQLITE_ROW ){
17655 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
17656 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
17657 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
17658 if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
17659 azName[nName*2] = strdup(zSchema);
17660 azName[nName*2+1] = strdup(zFile);
17664 sqlite3_finalize(pStmt);
17665 for(i=0; i<nName; i++){
17666 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
17667 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
17668 const char *z = azName[i*2+1];
17669 utf8_printf(p->out, "%s: %s %s%s\n",
17671 z && z[0] ? z : "\"\"",
17672 bRdonly ? "r/o" : "r/w",
17673 eTxn==SQLITE_TXN_NONE ? "" :
17674 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
17676 free(azName[i*2+1]);
17678 sqlite3_free(azName);
17681 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
17682 static const struct DbConfigChoices {
17686 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
17687 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
17688 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
17689 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
17690 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
17691 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
17692 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
17693 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
17694 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
17695 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
17696 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
17697 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
17698 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
17699 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
17700 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
17701 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
17705 for(ii=0; ii<ArraySize(aDbConfig); ii++){
17706 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
17708 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
17710 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
17711 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
17712 if( nArg>1 ) break;
17714 if( nArg>1 && ii==ArraySize(aDbConfig) ){
17715 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
17716 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
17720 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
17721 rc = shell_dbinfo_command(p, nArg, azArg);
17724 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17725 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
17727 rc = recoverDatabaseCmd(p, nArg, azArg);
17729 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17731 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
17735 int savedShowHeader = p->showHeader;
17736 int savedShellFlags = p->shellFlgs;
17738 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
17739 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
17740 for(i=1; i<nArg; i++){
17741 if( azArg[i][0]=='-' ){
17742 const char *z = azArg[i]+1;
17743 if( z[0]=='-' ) z++;
17744 if( strcmp(z,"preserve-rowids")==0 ){
17745 #ifdef SQLITE_OMIT_VIRTUALTABLE
17746 raw_printf(stderr, "The --preserve-rowids option is not compatible"
17747 " with SQLITE_OMIT_VIRTUALTABLE\n");
17749 sqlite3_free(zLike);
17750 goto meta_command_exit;
17752 ShellSetFlag(p, SHFLG_PreserveRowid);
17755 if( strcmp(z,"newlines")==0 ){
17756 ShellSetFlag(p, SHFLG_Newlines);
17758 if( strcmp(z,"data-only")==0 ){
17759 ShellSetFlag(p, SHFLG_DumpDataOnly);
17761 if( strcmp(z,"nosys")==0 ){
17762 ShellSetFlag(p, SHFLG_DumpNoSys);
17765 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
17767 sqlite3_free(zLike);
17768 goto meta_command_exit;
17771 zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
17774 zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
17780 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17781 /* When playing back a "dump", the content might appear in an order
17782 ** which causes immediate foreign key constraints to be violated.
17783 ** So disable foreign-key constraint enforcement to prevent problems. */
17784 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
17785 raw_printf(p->out, "BEGIN TRANSACTION;\n");
17787 p->writableSchema = 0;
17789 /* Set writable_schema=ON since doing so forces SQLite to initialize
17790 ** as much of the schema as it can even if the sqlite_schema table is
17792 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
17794 if( zLike==0 ) zLike = sqlite3_mprintf("true");
17795 zSql = sqlite3_mprintf(
17796 "SELECT name, type, sql FROM sqlite_schema "
17797 "WHERE (%s) AND type=='table'"
17798 " AND sql NOT NULL"
17799 " ORDER BY tbl_name='sqlite_sequence', rowid",
17802 run_schema_dump_query(p,zSql);
17803 sqlite3_free(zSql);
17804 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17805 zSql = sqlite3_mprintf(
17806 "SELECT sql FROM sqlite_schema "
17807 "WHERE (%s) AND sql NOT NULL"
17808 " AND type IN ('index','trigger','view')",
17811 run_table_dump_query(p, zSql);
17812 sqlite3_free(zSql);
17814 sqlite3_free(zLike);
17815 if( p->writableSchema ){
17816 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
17817 p->writableSchema = 0;
17819 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
17820 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
17821 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17822 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
17824 p->showHeader = savedShowHeader;
17825 p->shellFlgs = savedShellFlags;
17828 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
17830 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
17832 raw_printf(stderr, "Usage: .echo on|off\n");
17837 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
17839 p->autoEQPtest = 0;
17840 if( p->autoEQPtrace ){
17841 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
17842 p->autoEQPtrace = 0;
17844 if( strcmp(azArg[1],"full")==0 ){
17845 p->autoEQP = AUTOEQP_full;
17846 }else if( strcmp(azArg[1],"trigger")==0 ){
17847 p->autoEQP = AUTOEQP_trigger;
17848 #ifdef SQLITE_DEBUG
17849 }else if( strcmp(azArg[1],"test")==0 ){
17850 p->autoEQP = AUTOEQP_on;
17851 p->autoEQPtest = 1;
17852 }else if( strcmp(azArg[1],"trace")==0 ){
17853 p->autoEQP = AUTOEQP_full;
17854 p->autoEQPtrace = 1;
17856 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
17857 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
17860 p->autoEQP = (u8)booleanValue(azArg[1]);
17863 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
17868 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
17869 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
17873 /* The ".explain" command is automatic now. It is largely pointless. It
17874 ** retained purely for backwards compatibility */
17875 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
17878 if( strcmp(azArg[1],"auto")==0 ){
17881 val = booleanValue(azArg[1]);
17884 if( val==1 && p->mode!=MODE_Explain ){
17885 p->normalMode = p->mode;
17886 p->mode = MODE_Explain;
17887 p->autoExplain = 0;
17888 }else if( val==0 ){
17889 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17890 p->autoExplain = 0;
17891 }else if( val==99 ){
17892 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17893 p->autoExplain = 1;
17897 #ifndef SQLITE_OMIT_VIRTUALTABLE
17898 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
17900 expertDotCommand(p, azArg, nArg);
17904 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
17905 static const struct {
17906 const char *zCtrlName; /* Name of a test-control option */
17907 int ctrlCode; /* Integer code for that option */
17908 const char *zUsage; /* Usage notes */
17910 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
17911 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
17912 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
17913 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17914 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
17915 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17916 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17917 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
17918 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
17919 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
17923 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
17924 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
17926 const char *zCmd = 0;
17927 const char *zSchema = 0;
17930 zCmd = nArg>=2 ? azArg[1] : "help";
17933 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
17936 zSchema = azArg[2];
17937 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
17942 /* The argument can optionally begin with "-" or "--" */
17943 if( zCmd[0]=='-' && zCmd[1] ){
17945 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
17948 /* --help lists all file-controls */
17949 if( strcmp(zCmd,"help")==0 ){
17950 utf8_printf(p->out, "Available file-controls:\n");
17951 for(i=0; i<ArraySize(aCtrl); i++){
17952 utf8_printf(p->out, " .filectrl %s %s\n",
17953 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
17956 goto meta_command_exit;
17959 /* convert filectrl text option to value. allow any unique prefix
17960 ** of the option name, or a numerical value. */
17961 n2 = strlen30(zCmd);
17962 for(i=0; i<ArraySize(aCtrl); i++){
17963 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
17965 filectrl = aCtrl[i].ctrlCode;
17968 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
17969 "Use \".filectrl --help\" for help\n", zCmd);
17971 goto meta_command_exit;
17976 utf8_printf(stderr,"Error: unknown file-control: %s\n"
17977 "Use \".filectrl --help\" for help\n", zCmd);
17980 case SQLITE_FCNTL_SIZE_LIMIT: {
17981 if( nArg!=2 && nArg!=3 ) break;
17982 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
17983 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
17987 case SQLITE_FCNTL_LOCK_TIMEOUT:
17988 case SQLITE_FCNTL_CHUNK_SIZE: {
17990 if( nArg!=3 ) break;
17991 x = (int)integerValue(azArg[2]);
17992 sqlite3_file_control(p->db, zSchema, filectrl, &x);
17996 case SQLITE_FCNTL_PERSIST_WAL:
17997 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
17999 if( nArg!=2 && nArg!=3 ) break;
18000 x = nArg==3 ? booleanValue(azArg[2]) : -1;
18001 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18006 case SQLITE_FCNTL_HAS_MOVED: {
18008 if( nArg!=2 ) break;
18009 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18014 case SQLITE_FCNTL_TEMPFILENAME: {
18016 if( nArg!=2 ) break;
18017 sqlite3_file_control(p->db, zSchema, filectrl, &z);
18019 utf8_printf(p->out, "%s\n", z);
18025 case SQLITE_FCNTL_RESERVE_BYTES: {
18028 x = atoi(azArg[2]);
18029 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18032 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18033 utf8_printf(p->out,"%d\n", x);
18039 if( isOk==0 && iCtrl>=0 ){
18040 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18042 }else if( isOk==1 ){
18044 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18045 raw_printf(p->out, "%s\n", zBuf);
18049 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18053 memcpy(&data, p, sizeof(data));
18054 data.showHeader = 0;
18055 data.cMode = data.mode = MODE_Semi;
18056 if( nArg==2 && optionMatch(azArg[1], "indent") ){
18057 data.cMode = data.mode = MODE_Pretty;
18061 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18063 goto meta_command_exit;
18066 rc = sqlite3_exec(p->db,
18068 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
18069 " FROM sqlite_schema UNION ALL"
18070 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
18071 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
18073 callback, &data, &zErrMsg
18075 if( rc==SQLITE_OK ){
18076 sqlite3_stmt *pStmt;
18077 rc = sqlite3_prepare_v2(p->db,
18078 "SELECT rowid FROM sqlite_schema"
18079 " WHERE name GLOB 'sqlite_stat[134]'",
18081 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
18082 sqlite3_finalize(pStmt);
18085 raw_printf(p->out, "/* No STAT tables available */\n");
18087 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18088 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
18089 callback, &data, &zErrMsg);
18090 data.cMode = data.mode = MODE_Insert;
18091 data.zDestTable = "sqlite_stat1";
18092 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
18093 data.zDestTable = "sqlite_stat4";
18094 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
18095 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18099 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
18101 p->showHeader = booleanValue(azArg[1]);
18102 p->shellFlgs |= SHFLG_HeaderSet;
18104 raw_printf(stderr, "Usage: .headers on|off\n");
18109 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
18111 n = showHelp(p->out, azArg[1]);
18113 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
18116 showHelp(p->out, 0);
18120 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
18121 char *zTable = 0; /* Insert data into this table */
18122 char *zFile = 0; /* Name of file to extra content from */
18123 sqlite3_stmt *pStmt = NULL; /* A statement */
18124 int nCol; /* Number of columns in the table */
18125 int nByte; /* Number of bytes in an SQL string */
18126 int i, j; /* Loop counters */
18127 int needCommit; /* True to COMMIT or ROLLBACK at end */
18128 int nSep; /* Number of bytes in p->colSeparator[] */
18129 char *zSql; /* An SQL statement */
18130 ImportCtx sCtx; /* Reader context */
18131 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
18132 int eVerbose = 0; /* Larger for more console output */
18133 int nSkip = 0; /* Initial lines to skip */
18134 int useOutputMode = 1; /* Use output mode to determine separators */
18136 memset(&sCtx, 0, sizeof(sCtx));
18137 if( p->mode==MODE_Ascii ){
18138 xRead = ascii_read_one_field;
18140 xRead = csv_read_one_field;
18142 for(i=1; i<nArg; i++){
18143 char *z = azArg[i];
18144 if( z[0]=='-' && z[1]=='-' ) z++;
18148 }else if( zTable==0 ){
18151 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
18152 showHelp(p->out, "import");
18154 goto meta_command_exit;
18156 }else if( strcmp(z,"-v")==0 ){
18158 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
18159 nSkip = integerValue(azArg[++i]);
18160 }else if( strcmp(z,"-ascii")==0 ){
18161 sCtx.cColSep = SEP_Unit[0];
18162 sCtx.cRowSep = SEP_Record[0];
18163 xRead = ascii_read_one_field;
18165 }else if( strcmp(z,"-csv")==0 ){
18166 sCtx.cColSep = ',';
18167 sCtx.cRowSep = '\n';
18168 xRead = csv_read_one_field;
18171 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
18172 showHelp(p->out, "import");
18174 goto meta_command_exit;
18178 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
18179 zFile==0 ? "FILE" : "TABLE");
18180 showHelp(p->out, "import");
18182 goto meta_command_exit;
18186 if( useOutputMode ){
18187 /* If neither the --csv or --ascii options are specified, then set
18188 ** the column and row separator characters from the output mode. */
18189 nSep = strlen30(p->colSeparator);
18192 "Error: non-null column separator required for import\n");
18194 goto meta_command_exit;
18198 "Error: multi-character column separators not allowed"
18201 goto meta_command_exit;
18203 nSep = strlen30(p->rowSeparator);
18206 "Error: non-null row separator required for import\n");
18208 goto meta_command_exit;
18210 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
18211 /* When importing CSV (only), if the row separator is set to the
18212 ** default output row separator, change it to the default input
18213 ** row separator. This avoids having to maintain different input
18214 ** and output row separators. */
18215 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18216 nSep = strlen30(p->rowSeparator);
18219 raw_printf(stderr, "Error: multi-character row separators not allowed"
18222 goto meta_command_exit;
18224 sCtx.cColSep = p->colSeparator[0];
18225 sCtx.cRowSep = p->rowSeparator[0];
18227 sCtx.zFile = zFile;
18229 if( sCtx.zFile[0]=='|' ){
18230 #ifdef SQLITE_OMIT_POPEN
18231 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18233 goto meta_command_exit;
18235 sCtx.in = popen(sCtx.zFile+1, "r");
18236 sCtx.zFile = "<pipe>";
18237 sCtx.xCloser = pclose;
18240 sCtx.in = fopen(sCtx.zFile, "rb");
18241 sCtx.xCloser = fclose;
18244 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
18246 goto meta_command_exit;
18248 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
18251 zSep[0] = sCtx.cColSep;
18252 utf8_printf(p->out, "Column separator ");
18253 output_c_string(p->out, zSep);
18254 utf8_printf(p->out, ", row separator ");
18255 zSep[0] = sCtx.cRowSep;
18256 output_c_string(p->out, zSep);
18257 utf8_printf(p->out, "\n");
18259 while( (nSkip--)>0 ){
18260 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
18262 zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
18264 import_cleanup(&sCtx);
18265 shell_out_of_memory();
18267 nByte = strlen30(zSql);
18268 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18269 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
18270 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
18271 char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
18273 while( xRead(&sCtx) ){
18274 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
18276 if( sCtx.cTerm!=sCtx.cColSep ) break;
18279 sqlite3_free(zCreate);
18280 import_cleanup(&sCtx);
18281 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
18283 goto meta_command_exit;
18285 zCreate = sqlite3_mprintf("%z\n)", zCreate);
18287 utf8_printf(p->out, "%s\n", zCreate);
18289 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
18290 sqlite3_free(zCreate);
18292 utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
18293 sqlite3_errmsg(p->db));
18294 import_cleanup(&sCtx);
18296 goto meta_command_exit;
18298 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18300 sqlite3_free(zSql);
18302 if (pStmt) sqlite3_finalize(pStmt);
18303 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
18304 import_cleanup(&sCtx);
18306 goto meta_command_exit;
18308 nCol = sqlite3_column_count(pStmt);
18309 sqlite3_finalize(pStmt);
18311 if( nCol==0 ) return 0; /* no columns, no error */
18312 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
18314 import_cleanup(&sCtx);
18315 shell_out_of_memory();
18317 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
18318 j = strlen30(zSql);
18319 for(i=1; i<nCol; i++){
18326 utf8_printf(p->out, "Insert using: %s\n", zSql);
18328 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18329 sqlite3_free(zSql);
18331 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18332 if (pStmt) sqlite3_finalize(pStmt);
18333 import_cleanup(&sCtx);
18335 goto meta_command_exit;
18337 needCommit = sqlite3_get_autocommit(p->db);
18338 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
18340 int startLine = sCtx.nLine;
18341 for(i=0; i<nCol; i++){
18342 char *z = xRead(&sCtx);
18344 ** Did we reach end-of-file before finding any columns?
18345 ** If so, stop instead of NULL filling the remaining columns.
18347 if( z==0 && i==0 ) break;
18349 ** Did we reach end-of-file OR end-of-line before finding any
18350 ** columns in ASCII mode? If so, stop instead of NULL filling
18351 ** the remaining columns.
18353 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
18354 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
18355 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
18356 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18357 "filling the rest with NULL\n",
18358 sCtx.zFile, startLine, nCol, i+1);
18360 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
18363 if( sCtx.cTerm==sCtx.cColSep ){
18367 }while( sCtx.cTerm==sCtx.cColSep );
18368 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18369 "extras ignored\n",
18370 sCtx.zFile, startLine, nCol, i);
18373 sqlite3_step(pStmt);
18374 rc = sqlite3_reset(pStmt);
18375 if( rc!=SQLITE_OK ){
18376 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
18377 startLine, sqlite3_errmsg(p->db));
18383 }while( sCtx.cTerm!=EOF );
18385 import_cleanup(&sCtx);
18386 sqlite3_finalize(pStmt);
18387 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
18389 utf8_printf(p->out,
18390 "Added %d rows with %d errors using %d lines of input\n",
18391 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
18395 #ifndef SQLITE_UNTESTABLE
18396 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
18398 char *zCollist = 0;
18399 sqlite3_stmt *pStmt;
18401 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
18402 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
18404 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
18405 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
18406 " .imposter off\n");
18407 /* Also allowed, but not documented:
18409 ** .imposter TABLE IMPOSTER
18411 ** where TABLE is a WITHOUT ROWID table. In that case, the
18412 ** imposter is another WITHOUT ROWID table with the columns in
18413 ** storage order. */
18415 goto meta_command_exit;
18419 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
18420 goto meta_command_exit;
18422 zSql = sqlite3_mprintf(
18423 "SELECT rootpage, 0 FROM sqlite_schema"
18424 " WHERE name='%q' AND type='index'"
18426 "SELECT rootpage, 1 FROM sqlite_schema"
18427 " WHERE name='%q' AND type='table'"
18428 " AND sql LIKE '%%without%%rowid%%'",
18431 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18432 sqlite3_free(zSql);
18433 if( sqlite3_step(pStmt)==SQLITE_ROW ){
18434 tnum = sqlite3_column_int(pStmt, 0);
18435 isWO = sqlite3_column_int(pStmt, 1);
18437 sqlite3_finalize(pStmt);
18438 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
18439 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18440 sqlite3_free(zSql);
18442 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18444 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
18447 if( sqlite3_column_int(pStmt,1)==-1 ){
18450 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
18454 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
18455 lenPK = (int)strlen(zCollist);
18458 zCollist = sqlite3_mprintf("\"%w\"", zCol);
18460 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
18463 sqlite3_finalize(pStmt);
18464 if( i==0 || tnum==0 ){
18465 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
18467 sqlite3_free(zCollist);
18468 goto meta_command_exit;
18470 if( lenPK==0 ) lenPK = 100000;
18471 zSql = sqlite3_mprintf(
18472 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
18473 azArg[2], zCollist, lenPK, zCollist);
18474 sqlite3_free(zCollist);
18475 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
18476 if( rc==SQLITE_OK ){
18477 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
18478 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
18480 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
18482 utf8_printf(stdout, "%s;\n", zSql);
18484 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
18485 azArg[1], isWO ? "table" : "index"
18489 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
18492 sqlite3_free(zSql);
18494 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
18496 #ifdef SQLITE_ENABLE_IOTRACE
18497 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
18498 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
18499 if( iotrace && iotrace!=stdout ) fclose(iotrace);
18502 sqlite3IoTrace = 0;
18503 }else if( strcmp(azArg[1], "-")==0 ){
18504 sqlite3IoTrace = iotracePrintf;
18507 iotrace = fopen(azArg[1], "w");
18509 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
18510 sqlite3IoTrace = 0;
18513 sqlite3IoTrace = iotracePrintf;
18519 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
18520 static const struct {
18521 const char *zLimitName; /* Name of a limit */
18522 int limitCode; /* Integer code for that limit */
18524 { "length", SQLITE_LIMIT_LENGTH },
18525 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
18526 { "column", SQLITE_LIMIT_COLUMN },
18527 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
18528 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
18529 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
18530 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
18531 { "attached", SQLITE_LIMIT_ATTACHED },
18532 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
18533 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
18534 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
18535 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
18540 for(i=0; i<ArraySize(aLimit); i++){
18541 printf("%20s %d\n", aLimit[i].zLimitName,
18542 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
18544 }else if( nArg>3 ){
18545 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
18547 goto meta_command_exit;
18550 n2 = strlen30(azArg[1]);
18551 for(i=0; i<ArraySize(aLimit); i++){
18552 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
18556 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
18558 goto meta_command_exit;
18563 utf8_printf(stderr, "unknown limit: \"%s\"\n"
18564 "enter \".limits\" with no arguments for a list.\n",
18567 goto meta_command_exit;
18570 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
18571 (int)integerValue(azArg[2]));
18573 printf("%20s %d\n", aLimit[iLimit].zLimitName,
18574 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
18578 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
18580 lintDotCommand(p, azArg, nArg);
18583 #ifndef SQLITE_OMIT_LOAD_EXTENSION
18584 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
18585 const char *zFile, *zProc;
18588 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
18590 goto meta_command_exit;
18593 zProc = nArg>=3 ? azArg[2] : 0;
18595 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
18596 if( rc!=SQLITE_OK ){
18597 utf8_printf(stderr, "Error: %s\n", zErrMsg);
18598 sqlite3_free(zErrMsg);
18604 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
18606 raw_printf(stderr, "Usage: .log FILENAME\n");
18609 const char *zFile = azArg[1];
18610 output_file_close(p->pLog);
18611 p->pLog = output_file_open(zFile, 0);
18615 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
18616 const char *zMode = nArg>=2 ? azArg[1] : "";
18617 int n2 = strlen30(zMode);
18619 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
18620 p->mode = MODE_Line;
18621 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18622 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
18623 p->mode = MODE_Column;
18624 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
18627 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18628 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
18629 p->mode = MODE_List;
18630 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
18631 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18632 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
18633 p->mode = MODE_Html;
18634 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
18635 p->mode = MODE_Tcl;
18636 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
18637 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18638 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
18639 p->mode = MODE_Csv;
18640 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18641 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18642 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
18643 p->mode = MODE_List;
18644 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
18645 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
18646 p->mode = MODE_Insert;
18647 set_table_name(p, nArg>=3 ? azArg[2] : "table");
18648 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
18649 p->mode = MODE_Quote;
18650 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18651 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18652 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
18653 p->mode = MODE_Ascii;
18654 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
18655 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
18656 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
18657 p->mode = MODE_Markdown;
18658 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
18659 p->mode = MODE_Table;
18660 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
18661 p->mode = MODE_Box;
18662 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
18663 p->mode = MODE_Json;
18664 }else if( nArg==1 ){
18665 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
18667 raw_printf(stderr, "Error: mode should be one of: "
18668 "ascii box column csv html insert json line list markdown "
18669 "quote table tabs tcl\n");
18672 p->cMode = p->mode;
18675 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
18677 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
18678 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
18680 raw_printf(stderr, "Usage: .nullvalue STRING\n");
18685 #ifdef SQLITE_DEBUG
18686 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
18688 for(i=1; i<nArg; i++){
18689 const char *z = azArg[i];
18690 if( z[0]=='-' && z[1]=='-' ) z++;
18691 if( strcmp(z,"-repeat")==0 ){
18693 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
18696 oomRepeat = (int)integerValue(azArg[++i]);
18698 }else if( IsDigit(z[0]) ){
18699 oomCounter = (int)integerValue(azArg[i]);
18701 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
18702 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
18707 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
18708 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
18711 #endif /* SQLITE_DEBUG */
18713 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
18714 char *zNewFilename; /* Name of the database file to open */
18715 int iName = 1; /* Index in azArg[] of the filename */
18716 int newFlag = 0; /* True to delete file before opening */
18717 /* Close the existing database */
18718 session_close_all(p);
18721 p->zDbFilename = 0;
18722 sqlite3_free(p->zFreeOnClose);
18723 p->zFreeOnClose = 0;
18724 p->openMode = SHELL_OPEN_UNSPEC;
18727 /* Check for command-line arguments */
18728 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
18729 const char *z = azArg[iName];
18730 if( optionMatch(z,"new") ){
18732 #ifdef SQLITE_HAVE_ZLIB
18733 }else if( optionMatch(z, "zip") ){
18734 p->openMode = SHELL_OPEN_ZIPFILE;
18736 }else if( optionMatch(z, "append") ){
18737 p->openMode = SHELL_OPEN_APPENDVFS;
18738 }else if( optionMatch(z, "readonly") ){
18739 p->openMode = SHELL_OPEN_READONLY;
18740 }else if( optionMatch(z, "nofollow") ){
18741 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
18742 #ifdef SQLITE_ENABLE_DESERIALIZE
18743 }else if( optionMatch(z, "deserialize") ){
18744 p->openMode = SHELL_OPEN_DESERIALIZE;
18745 }else if( optionMatch(z, "hexdb") ){
18746 p->openMode = SHELL_OPEN_HEXDB;
18747 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
18748 p->szMax = integerValue(azArg[++iName]);
18749 #endif /* SQLITE_ENABLE_DESERIALIZE */
18750 }else if( z[0]=='-' ){
18751 utf8_printf(stderr, "unknown option: %s\n", z);
18753 goto meta_command_exit;
18756 /* If a filename is specified, try to open it first */
18757 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
18758 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
18759 if( newFlag ) shellDeleteFile(zNewFilename);
18760 p->zDbFilename = zNewFilename;
18761 open_db(p, OPEN_DB_KEEPALIVE);
18763 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
18764 sqlite3_free(zNewFilename);
18766 p->zFreeOnClose = zNewFilename;
18770 /* As a fall-back open a TEMP database */
18771 p->zDbFilename = 0;
18777 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
18778 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
18780 const char *zFile = 0;
18785 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
18790 }else if( strncmp(azArg[0],"once",n)==0 ){
18793 for(i=1; i<nArg; i++){
18794 char *z = azArg[i];
18796 if( z[1]=='-' ) z++;
18797 if( strcmp(z,"-bom")==0 ){
18799 }else if( c!='e' && strcmp(z,"-x")==0 ){
18800 eMode = 'x'; /* spreadsheet */
18801 }else if( c!='e' && strcmp(z,"-e")==0 ){
18802 eMode = 'e'; /* text editor */
18804 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
18806 showHelp(p->out, azArg[0]);
18808 goto meta_command_exit;
18810 }else if( zFile==0 ){
18813 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
18815 showHelp(p->out, azArg[0]);
18817 goto meta_command_exit;
18820 if( zFile==0 ) zFile = "stdout";
18827 #ifndef SQLITE_NOHAVE_SYSTEM
18828 if( eMode=='e' || eMode=='x' ){
18832 /* spreadsheet mode. Output as CSV. */
18833 newTempFile(p, "csv");
18834 ShellClearFlag(p, SHFLG_Echo);
18835 p->mode = MODE_Csv;
18836 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18837 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18839 /* text editor mode */
18840 newTempFile(p, "txt");
18843 zFile = p->zTempFile;
18845 #endif /* SQLITE_NOHAVE_SYSTEM */
18846 if( zFile[0]=='|' ){
18847 #ifdef SQLITE_OMIT_POPEN
18848 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18852 p->out = popen(zFile + 1, "w");
18854 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
18858 if( bBOM ) fprintf(p->out,"\357\273\277");
18859 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18863 p->out = output_file_open(zFile, bTxtMode);
18865 if( strcmp(zFile,"off")!=0 ){
18866 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
18871 if( bBOM ) fprintf(p->out,"\357\273\277");
18872 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18877 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
18879 if( nArg<=1 ) goto parameter_syntax_error;
18881 /* .parameter clear
18882 ** Clear all bind parameters by dropping the TEMP table that holds them.
18884 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
18885 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
18890 ** List all bind parameters.
18892 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
18893 sqlite3_stmt *pStmt = 0;
18896 rx = sqlite3_prepare_v2(p->db,
18897 "SELECT max(length(key)) "
18898 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18899 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
18900 len = sqlite3_column_int(pStmt, 0);
18901 if( len>40 ) len = 40;
18903 sqlite3_finalize(pStmt);
18906 rx = sqlite3_prepare_v2(p->db,
18907 "SELECT key, quote(value) "
18908 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18909 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18910 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
18911 sqlite3_column_text(pStmt,1));
18913 sqlite3_finalize(pStmt);
18918 ** Make sure the TEMP table used to hold bind parameters exists.
18919 ** Create it if necessary.
18921 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
18922 bind_table_init(p);
18925 /* .parameter set NAME VALUE
18926 ** Set or reset a bind parameter. NAME should be the full parameter
18927 ** name exactly as it appears in the query. (ex: $abc, @def). The
18928 ** VALUE can be in either SQL literal notation, or if not it will be
18929 ** understood to be a text string.
18931 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
18934 sqlite3_stmt *pStmt;
18935 const char *zKey = azArg[2];
18936 const char *zValue = azArg[3];
18937 bind_table_init(p);
18938 zSql = sqlite3_mprintf(
18939 "REPLACE INTO temp.sqlite_parameters(key,value)"
18940 "VALUES(%Q,%s);", zKey, zValue);
18941 if( zSql==0 ) shell_out_of_memory();
18943 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18944 sqlite3_free(zSql);
18945 if( rx!=SQLITE_OK ){
18946 sqlite3_finalize(pStmt);
18948 zSql = sqlite3_mprintf(
18949 "REPLACE INTO temp.sqlite_parameters(key,value)"
18950 "VALUES(%Q,%Q);", zKey, zValue);
18951 if( zSql==0 ) shell_out_of_memory();
18952 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18953 sqlite3_free(zSql);
18954 if( rx!=SQLITE_OK ){
18955 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
18956 sqlite3_finalize(pStmt);
18961 sqlite3_step(pStmt);
18962 sqlite3_finalize(pStmt);
18965 /* .parameter unset NAME
18966 ** Remove the NAME binding from the parameter binding table, if it
18969 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
18970 char *zSql = sqlite3_mprintf(
18971 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
18972 if( zSql==0 ) shell_out_of_memory();
18973 sqlite3_exec(p->db, zSql, 0, 0, 0);
18974 sqlite3_free(zSql);
18976 /* If no command name matches, show a syntax error */
18977 parameter_syntax_error:
18978 showHelp(p->out, "parameter");
18981 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
18983 for(i=1; i<nArg; i++){
18984 if( i>1 ) raw_printf(p->out, " ");
18985 utf8_printf(p->out, "%s", azArg[i]);
18987 raw_printf(p->out, "\n");
18990 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
18991 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
18994 p->flgProgress = 0;
18997 for(i=1; i<nArg; i++){
18998 const char *z = azArg[i];
19001 if( z[0]=='-' ) z++;
19002 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
19003 p->flgProgress |= SHELL_PROGRESS_QUIET;
19006 if( strcmp(z,"reset")==0 ){
19007 p->flgProgress |= SHELL_PROGRESS_RESET;
19010 if( strcmp(z,"once")==0 ){
19011 p->flgProgress |= SHELL_PROGRESS_ONCE;
19014 if( strcmp(z,"limit")==0 ){
19016 utf8_printf(stderr, "Error: missing argument on --limit\n");
19018 goto meta_command_exit;
19020 p->mxProgress = (int)integerValue(azArg[++i]);
19024 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19026 goto meta_command_exit;
19028 nn = (int)integerValue(z);
19032 sqlite3_progress_handler(p->db, nn, progress_handler, p);
19034 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19036 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19038 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19041 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19045 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19049 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19050 FILE *inSaved = p->in;
19051 int savedLineno = p->lineno;
19053 raw_printf(stderr, "Usage: .read FILE\n");
19055 goto meta_command_exit;
19057 if( azArg[1][0]=='|' ){
19058 p->in = popen(azArg[1]+1, "r");
19060 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19063 rc = process_input(p);
19066 }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
19067 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
19070 rc = process_input(p);
19074 p->lineno = savedLineno;
19077 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
19078 const char *zSrcFile;
19081 sqlite3_backup *pBackup;
19085 zSrcFile = azArg[1];
19087 }else if( nArg==3 ){
19088 zSrcFile = azArg[2];
19091 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
19093 goto meta_command_exit;
19095 rc = sqlite3_open(zSrcFile, &pSrc);
19096 if( rc!=SQLITE_OK ){
19097 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
19102 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
19104 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19108 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
19109 || rc==SQLITE_BUSY ){
19110 if( rc==SQLITE_BUSY ){
19111 if( nTimeout++ >= 3 ) break;
19112 sqlite3_sleep(100);
19115 sqlite3_backup_finish(pBackup);
19116 if( rc==SQLITE_DONE ){
19118 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
19119 raw_printf(stderr, "Error: source database is busy\n");
19122 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19128 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
19130 p->scanstatsOn = (u8)booleanValue(azArg[1]);
19131 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19132 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
19135 raw_printf(stderr, "Usage: .scanstats on|off\n");
19140 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
19144 const char *zDiv = "(";
19145 const char *zName = 0;
19148 int bNoSystemTabs = 0;
19152 memcpy(&data, p, sizeof(data));
19153 data.showHeader = 0;
19154 data.cMode = data.mode = MODE_Semi;
19155 initText(&sSelect);
19156 for(ii=1; ii<nArg; ii++){
19157 if( optionMatch(azArg[ii],"indent") ){
19158 data.cMode = data.mode = MODE_Pretty;
19159 }else if( optionMatch(azArg[ii],"debug") ){
19161 }else if( optionMatch(azArg[ii],"nosys") ){
19163 }else if( azArg[ii][0]=='-' ){
19164 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
19166 goto meta_command_exit;
19167 }else if( zName==0 ){
19170 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
19172 goto meta_command_exit;
19176 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
19177 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
19178 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
19179 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
19181 char *new_argv[2], *new_colv[2];
19182 new_argv[0] = sqlite3_mprintf(
19183 "CREATE TABLE %s (\n"
19186 " tbl_name text,\n"
19187 " rootpage integer,\n"
19191 new_colv[0] = "sql";
19193 callback(&data, 1, new_argv, new_colv);
19194 sqlite3_free(new_argv[0]);
19198 sqlite3_stmt *pStmt = 0;
19199 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
19202 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19203 sqlite3_finalize(pStmt);
19205 goto meta_command_exit;
19207 appendText(&sSelect, "SELECT sql FROM", 0);
19209 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19210 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
19212 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
19213 appendText(&sSelect, zDiv, 0);
19214 zDiv = " UNION ALL ";
19215 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
19216 if( sqlite3_stricmp(zDb, "main")!=0 ){
19217 appendText(&sSelect, zDb, '\'');
19219 appendText(&sSelect, "NULL", 0);
19221 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
19222 appendText(&sSelect, zScNum, 0);
19223 appendText(&sSelect, " AS snum, ", 0);
19224 appendText(&sSelect, zDb, '\'');
19225 appendText(&sSelect, " AS sname FROM ", 0);
19226 appendText(&sSelect, zDb, quoteChar(zDb));
19227 appendText(&sSelect, ".sqlite_schema", 0);
19229 sqlite3_finalize(pStmt);
19230 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
19232 appendText(&sSelect,
19233 " UNION ALL SELECT shell_module_schema(name),"
19234 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
19238 appendText(&sSelect, ") WHERE ", 0);
19240 char *zQarg = sqlite3_mprintf("%Q", zName);
19241 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
19242 strchr(zName, '[') != 0;
19243 if( strchr(zName, '.') ){
19244 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
19246 appendText(&sSelect, "lower(tbl_name)", 0);
19248 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
19249 appendText(&sSelect, zQarg, 0);
19251 appendText(&sSelect, " ESCAPE '\\' ", 0);
19253 appendText(&sSelect, " AND ", 0);
19254 sqlite3_free(zQarg);
19256 if( bNoSystemTabs ){
19257 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
19259 appendText(&sSelect, "sql IS NOT NULL"
19260 " ORDER BY snum, rowid", 0);
19262 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
19264 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
19266 freeText(&sSelect);
19269 utf8_printf(stderr,"Error: %s\n", zErrMsg);
19270 sqlite3_free(zErrMsg);
19272 }else if( rc != SQLITE_OK ){
19273 raw_printf(stderr,"Error: querying schema information\n");
19280 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
19281 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
19282 sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
19286 #if defined(SQLITE_ENABLE_SESSION)
19287 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
19288 OpenSession *pSession = &p->aSession[0];
19289 char **azCmd = &azArg[1];
19291 int nCmd = nArg - 1;
19293 if( nArg<=1 ) goto session_syntax_error;
19296 for(iSes=0; iSes<p->nSession; iSes++){
19297 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
19299 if( iSes<p->nSession ){
19300 pSession = &p->aSession[iSes];
19304 pSession = &p->aSession[0];
19309 /* .session attach TABLE
19310 ** Invoke the sqlite3session_attach() interface to attach a particular
19311 ** table so that it is never filtered.
19313 if( strcmp(azCmd[0],"attach")==0 ){
19314 if( nCmd!=2 ) goto session_syntax_error;
19315 if( pSession->p==0 ){
19317 raw_printf(stderr, "ERROR: No sessions are open\n");
19319 rc = sqlite3session_attach(pSession->p, azCmd[1]);
19321 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
19327 /* .session changeset FILE
19328 ** .session patchset FILE
19329 ** Write a changeset or patchset into a file. The file is overwritten.
19331 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
19333 if( nCmd!=2 ) goto session_syntax_error;
19334 if( pSession->p==0 ) goto session_not_open;
19335 out = fopen(azCmd[1], "wb");
19337 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
19342 if( azCmd[0][0]=='c' ){
19343 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
19345 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
19348 printf("Error: error code %d\n", rc);
19352 && fwrite(pChng, szChng, 1, out)!=1 ){
19353 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
19356 sqlite3_free(pChng);
19362 ** Close the identified session
19364 if( strcmp(azCmd[0], "close")==0 ){
19365 if( nCmd!=1 ) goto session_syntax_error;
19367 session_close(pSession);
19368 p->aSession[iSes] = p->aSession[--p->nSession];
19372 /* .session enable ?BOOLEAN?
19373 ** Query or set the enable flag
19375 if( strcmp(azCmd[0], "enable")==0 ){
19377 if( nCmd>2 ) goto session_syntax_error;
19378 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19380 ii = sqlite3session_enable(pSession->p, ii);
19381 utf8_printf(p->out, "session %s enable flag = %d\n",
19382 pSession->zName, ii);
19386 /* .session filter GLOB ....
19387 ** Set a list of GLOB patterns of table names to be excluded.
19389 if( strcmp(azCmd[0], "filter")==0 ){
19391 if( nCmd<2 ) goto session_syntax_error;
19393 for(ii=0; ii<pSession->nFilter; ii++){
19394 sqlite3_free(pSession->azFilter[ii]);
19396 sqlite3_free(pSession->azFilter);
19397 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
19398 pSession->azFilter = sqlite3_malloc( nByte );
19399 if( pSession->azFilter==0 ){
19400 raw_printf(stderr, "Error: out or memory\n");
19403 for(ii=1; ii<nCmd; ii++){
19404 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
19406 pSession->nFilter = ii-1;
19410 /* .session indirect ?BOOLEAN?
19411 ** Query or set the indirect flag
19413 if( strcmp(azCmd[0], "indirect")==0 ){
19415 if( nCmd>2 ) goto session_syntax_error;
19416 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19418 ii = sqlite3session_indirect(pSession->p, ii);
19419 utf8_printf(p->out, "session %s indirect flag = %d\n",
19420 pSession->zName, ii);
19424 /* .session isempty
19425 ** Determine if the session is empty
19427 if( strcmp(azCmd[0], "isempty")==0 ){
19429 if( nCmd!=1 ) goto session_syntax_error;
19431 ii = sqlite3session_isempty(pSession->p);
19432 utf8_printf(p->out, "session %s isempty flag = %d\n",
19433 pSession->zName, ii);
19438 ** List all currently open sessions
19440 if( strcmp(azCmd[0],"list")==0 ){
19441 for(i=0; i<p->nSession; i++){
19442 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
19446 /* .session open DB NAME
19447 ** Open a new session called NAME on the attached database DB.
19448 ** DB is normally "main".
19450 if( strcmp(azCmd[0],"open")==0 ){
19452 if( nCmd!=3 ) goto session_syntax_error;
19454 if( zName[0]==0 ) goto session_syntax_error;
19455 for(i=0; i<p->nSession; i++){
19456 if( strcmp(p->aSession[i].zName,zName)==0 ){
19457 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
19458 goto meta_command_exit;
19461 if( p->nSession>=ArraySize(p->aSession) ){
19462 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
19463 goto meta_command_exit;
19465 pSession = &p->aSession[p->nSession];
19466 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
19468 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
19470 goto meta_command_exit;
19472 pSession->nFilter = 0;
19473 sqlite3session_table_filter(pSession->p, session_filter, pSession);
19475 pSession->zName = sqlite3_mprintf("%s", zName);
19477 /* If no command name matches, show a syntax error */
19478 session_syntax_error:
19479 showHelp(p->out, "session");
19483 #ifdef SQLITE_DEBUG
19484 /* Undocumented commands for internal testing. Subject to change
19485 ** without notice. */
19486 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
19487 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
19489 for(i=1; i<nArg; i++){
19490 v = booleanValue(azArg[i]);
19491 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
19494 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
19495 int i; sqlite3_int64 v;
19496 for(i=1; i<nArg; i++){
19498 v = integerValue(azArg[i]);
19499 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
19500 utf8_printf(p->out, "%s", zBuf);
19506 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
19507 int bIsInit = 0; /* True to initialize the SELFTEST table */
19508 int bVerbose = 0; /* Verbose output */
19509 int bSelftestExists; /* True if SELFTEST already exists */
19510 int i, k; /* Loop counters */
19511 int nTest = 0; /* Number of tests runs */
19512 int nErr = 0; /* Number of errors seen */
19513 ShellText str; /* Answer for a query */
19514 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
19517 for(i=1; i<nArg; i++){
19518 const char *z = azArg[i];
19519 if( z[0]=='-' && z[1]=='-' ) z++;
19520 if( strcmp(z,"-init")==0 ){
19523 if( strcmp(z,"-v")==0 ){
19527 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19528 azArg[i], azArg[0]);
19529 raw_printf(stderr, "Should be one of: --init -v\n");
19531 goto meta_command_exit;
19534 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
19536 bSelftestExists = 0;
19538 bSelftestExists = 1;
19541 createSelftestTable(p);
19542 bSelftestExists = 1;
19545 appendText(&str, "x", 0);
19546 for(k=bSelftestExists; k>=0; k--){
19548 rc = sqlite3_prepare_v2(p->db,
19549 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
19552 rc = sqlite3_prepare_v2(p->db,
19553 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
19554 " (1,'run','PRAGMA integrity_check','ok')",
19558 raw_printf(stderr, "Error querying the selftest table\n");
19560 sqlite3_finalize(pStmt);
19561 goto meta_command_exit;
19563 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
19564 int tno = sqlite3_column_int(pStmt, 0);
19565 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
19566 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
19567 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
19571 char *zQuote = sqlite3_mprintf("%q", zSql);
19572 printf("%d: %s %s\n", tno, zOp, zSql);
19573 sqlite3_free(zQuote);
19575 if( strcmp(zOp,"memo")==0 ){
19576 utf8_printf(p->out, "%s\n", zSql);
19578 if( strcmp(zOp,"run")==0 ){
19582 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
19585 utf8_printf(p->out, "Result: %s\n", str.z);
19587 if( rc || zErrMsg ){
19590 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
19591 sqlite3_free(zErrMsg);
19592 }else if( strcmp(zAns,str.z)!=0 ){
19595 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
19596 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
19600 utf8_printf(stderr,
19601 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
19605 } /* End loop over rows of content from SELFTEST */
19606 sqlite3_finalize(pStmt);
19607 } /* End loop over k */
19609 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
19612 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
19613 if( nArg<2 || nArg>3 ){
19614 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
19618 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
19619 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
19622 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
19623 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
19627 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
19628 const char *zLike = 0; /* Which table to checksum. 0 means everything */
19629 int i; /* Loop counter */
19630 int bSchema = 0; /* Also hash the schema */
19631 int bSeparate = 0; /* Hash each table separately */
19632 int iSize = 224; /* Hash algorithm to use */
19633 int bDebug = 0; /* Only show the query that would have run */
19634 sqlite3_stmt *pStmt; /* For querying tables names */
19635 char *zSql; /* SQL to be run */
19636 char *zSep; /* Separator */
19637 ShellText sSql; /* Complete SQL for the query to run the hash */
19638 ShellText sQuery; /* Set of queries used to read all content */
19640 for(i=1; i<nArg; i++){
19641 const char *z = azArg[i];
19644 if( z[0]=='-' ) z++;
19645 if( strcmp(z,"schema")==0 ){
19648 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
19649 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
19651 iSize = atoi(&z[5]);
19653 if( strcmp(z,"debug")==0 ){
19657 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19658 azArg[i], azArg[0]);
19659 showHelp(p->out, azArg[0]);
19661 goto meta_command_exit;
19664 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
19666 goto meta_command_exit;
19670 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
19674 zSql = "SELECT lower(name) FROM sqlite_schema"
19675 " WHERE type='table' AND coalesce(rootpage,0)>1"
19676 " UNION ALL SELECT 'sqlite_schema'"
19677 " ORDER BY 1 collate nocase";
19679 zSql = "SELECT lower(name) FROM sqlite_schema"
19680 " WHERE type='table' AND coalesce(rootpage,0)>1"
19681 " AND name NOT LIKE 'sqlite_%'"
19682 " ORDER BY 1 collate nocase";
19684 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19687 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
19689 while( SQLITE_ROW==sqlite3_step(pStmt) ){
19690 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
19691 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
19692 if( strncmp(zTab, "sqlite_",7)!=0 ){
19693 appendText(&sQuery,"SELECT * FROM ", 0);
19694 appendText(&sQuery,zTab,'"');
19695 appendText(&sQuery," NOT INDEXED;", 0);
19696 }else if( strcmp(zTab, "sqlite_schema")==0 ){
19697 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
19698 " ORDER BY name;", 0);
19699 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
19700 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
19701 " ORDER BY name;", 0);
19702 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
19703 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
19704 " ORDER BY tbl,idx;", 0);
19705 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
19706 appendText(&sQuery, "SELECT * FROM ", 0);
19707 appendText(&sQuery, zTab, 0);
19708 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
19710 appendText(&sSql, zSep, 0);
19711 appendText(&sSql, sQuery.z, '\'');
19713 appendText(&sSql, ",", 0);
19714 appendText(&sSql, zTab, '\'');
19717 sqlite3_finalize(pStmt);
19719 zSql = sqlite3_mprintf(
19721 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
19722 " FROM [sha3sum$query]",
19725 zSql = sqlite3_mprintf(
19727 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
19728 " FROM [sha3sum$query]",
19734 utf8_printf(p->out, "%s\n", zSql);
19736 shell_exec(p, zSql, 0);
19738 sqlite3_free(zSql);
19741 #ifndef SQLITE_NOHAVE_SYSTEM
19743 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
19748 raw_printf(stderr, "Usage: .system COMMAND\n");
19750 goto meta_command_exit;
19752 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
19753 for(i=2; i<nArg; i++){
19754 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
19758 sqlite3_free(zCmd);
19759 if( x ) raw_printf(stderr, "System command returns %d\n", x);
19761 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
19763 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
19764 static const char *azBool[] = { "off", "on", "trigger", "full"};
19767 raw_printf(stderr, "Usage: .show\n");
19769 goto meta_command_exit;
19771 utf8_printf(p->out, "%12.12s: %s\n","echo",
19772 azBool[ShellHasFlag(p, SHFLG_Echo)]);
19773 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
19774 utf8_printf(p->out, "%12.12s: %s\n","explain",
19775 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
19776 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
19777 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
19778 utf8_printf(p->out, "%12.12s: ", "nullvalue");
19779 output_c_string(p->out, p->nullValue);
19780 raw_printf(p->out, "\n");
19781 utf8_printf(p->out,"%12.12s: %s\n","output",
19782 strlen30(p->outfile) ? p->outfile : "stdout");
19783 utf8_printf(p->out,"%12.12s: ", "colseparator");
19784 output_c_string(p->out, p->colSeparator);
19785 raw_printf(p->out, "\n");
19786 utf8_printf(p->out,"%12.12s: ", "rowseparator");
19787 output_c_string(p->out, p->rowSeparator);
19788 raw_printf(p->out, "\n");
19789 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
19790 utf8_printf(p->out, "%12.12s: ", "width");
19791 for (i=0;i<p->nWidth;i++) {
19792 raw_printf(p->out, "%d ", p->colWidth[i]);
19794 raw_printf(p->out, "\n");
19795 utf8_printf(p->out, "%12.12s: %s\n", "filename",
19796 p->zDbFilename ? p->zDbFilename : "");
19799 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
19801 p->statsOn = (u8)booleanValue(azArg[1]);
19802 }else if( nArg==1 ){
19803 display_stats(p->db, p, 0);
19805 raw_printf(stderr, "Usage: .stats ?on|off?\n");
19810 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
19811 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
19812 || strncmp(azArg[0], "indexes", n)==0) )
19814 sqlite3_stmt *pStmt;
19821 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19823 sqlite3_finalize(pStmt);
19824 return shellDatabaseError(p->db);
19827 if( nArg>2 && c=='i' ){
19828 /* It is an historical accident that the .indexes command shows an error
19829 ** when called with the wrong number of arguments whereas the .tables
19830 ** command does not. */
19831 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
19833 sqlite3_finalize(pStmt);
19834 goto meta_command_exit;
19836 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
19837 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
19838 if( zDbName==0 ) continue;
19839 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
19840 if( sqlite3_stricmp(zDbName, "main")==0 ){
19841 appendText(&s, "SELECT name FROM ", 0);
19843 appendText(&s, "SELECT ", 0);
19844 appendText(&s, zDbName, '\'');
19845 appendText(&s, "||'.'||name FROM ", 0);
19847 appendText(&s, zDbName, '"');
19848 appendText(&s, ".sqlite_schema ", 0);
19850 appendText(&s," WHERE type IN ('table','view')"
19851 " AND name NOT LIKE 'sqlite_%'"
19852 " AND name LIKE ?1", 0);
19854 appendText(&s," WHERE type='index'"
19855 " AND tbl_name LIKE ?1", 0);
19858 rc = sqlite3_finalize(pStmt);
19859 appendText(&s, " ORDER BY 1", 0);
19860 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
19862 if( rc ) return shellDatabaseError(p->db);
19864 /* Run the SQL statement prepared by the above block. Store the results
19865 ** as an array of nul-terminated strings in azResult[]. */
19869 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
19871 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
19873 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19874 if( nRow>=nAlloc ){
19876 int n2 = nAlloc*2 + 10;
19877 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
19878 if( azNew==0 ) shell_out_of_memory();
19882 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
19883 if( 0==azResult[nRow] ) shell_out_of_memory();
19886 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
19887 rc = shellDatabaseError(p->db);
19890 /* Pretty-print the contents of array azResult[] to the output */
19891 if( rc==0 && nRow>0 ){
19892 int len, maxlen = 0;
19894 int nPrintCol, nPrintRow;
19895 for(i=0; i<nRow; i++){
19896 len = strlen30(azResult[i]);
19897 if( len>maxlen ) maxlen = len;
19899 nPrintCol = 80/(maxlen+2);
19900 if( nPrintCol<1 ) nPrintCol = 1;
19901 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
19902 for(i=0; i<nPrintRow; i++){
19903 for(j=i; j<nRow; j+=nPrintRow){
19904 char *zSp = j<nPrintRow ? "" : " ";
19905 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
19906 azResult[j] ? azResult[j]:"");
19908 raw_printf(p->out, "\n");
19912 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
19913 sqlite3_free(azResult);
19916 /* Begin redirecting output to the file "testcase-out.txt" */
19917 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
19919 p->out = output_file_open("testcase-out.txt", 0);
19921 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
19924 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
19926 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
19930 #ifndef SQLITE_UNTESTABLE
19931 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
19932 static const struct {
19933 const char *zCtrlName; /* Name of a test-control option */
19934 int ctrlCode; /* Integer code for that option */
19935 const char *zUsage; /* Usage notes */
19937 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
19938 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
19939 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
19940 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
19941 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
19942 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
19943 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
19944 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
19945 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
19946 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
19947 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
19948 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
19950 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
19952 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
19953 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
19954 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
19955 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
19956 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" },
19960 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
19963 const char *zCmd = 0;
19966 zCmd = nArg>=2 ? azArg[1] : "help";
19968 /* The argument can optionally begin with "-" or "--" */
19969 if( zCmd[0]=='-' && zCmd[1] ){
19971 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19974 /* --help lists all test-controls */
19975 if( strcmp(zCmd,"help")==0 ){
19976 utf8_printf(p->out, "Available test-controls:\n");
19977 for(i=0; i<ArraySize(aCtrl); i++){
19978 utf8_printf(p->out, " .testctrl %s %s\n",
19979 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19982 goto meta_command_exit;
19985 /* convert testctrl text option to value. allow any unique prefix
19986 ** of the option name, or a numerical value. */
19987 n2 = strlen30(zCmd);
19988 for(i=0; i<ArraySize(aCtrl); i++){
19989 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19991 testctrl = aCtrl[i].ctrlCode;
19994 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
19995 "Use \".testctrl --help\" for help\n", zCmd);
19997 goto meta_command_exit;
20002 utf8_printf(stderr,"Error: unknown test-control: %s\n"
20003 "Use \".testctrl --help\" for help\n", zCmd);
20007 /* sqlite3_test_control(int, db, int) */
20008 case SQLITE_TESTCTRL_OPTIMIZATIONS:
20010 int opt = (int)strtol(azArg[2], 0, 0);
20011 rc2 = sqlite3_test_control(testctrl, p->db, opt);
20016 /* sqlite3_test_control(int) */
20017 case SQLITE_TESTCTRL_PRNG_SAVE:
20018 case SQLITE_TESTCTRL_PRNG_RESTORE:
20019 case SQLITE_TESTCTRL_BYTEORDER:
20021 rc2 = sqlite3_test_control(testctrl);
20022 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20026 /* sqlite3_test_control(int, uint) */
20027 case SQLITE_TESTCTRL_PENDING_BYTE:
20029 unsigned int opt = (unsigned int)integerValue(azArg[2]);
20030 rc2 = sqlite3_test_control(testctrl, opt);
20035 /* sqlite3_test_control(int, int, sqlite3*) */
20036 case SQLITE_TESTCTRL_PRNG_SEED:
20037 if( nArg==3 || nArg==4 ){
20038 int ii = (int)integerValue(azArg[2]);
20040 if( ii==0 && strcmp(azArg[2],"random")==0 ){
20041 sqlite3_randomness(sizeof(ii),&ii);
20042 printf("-- random seed: %d\n", ii);
20048 /* Make sure the schema has been loaded */
20049 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
20051 rc2 = sqlite3_test_control(testctrl, ii, db);
20056 /* sqlite3_test_control(int, int) */
20057 case SQLITE_TESTCTRL_ASSERT:
20058 case SQLITE_TESTCTRL_ALWAYS:
20060 int opt = booleanValue(azArg[2]);
20061 rc2 = sqlite3_test_control(testctrl, opt);
20066 /* sqlite3_test_control(int, int) */
20067 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
20068 case SQLITE_TESTCTRL_NEVER_CORRUPT:
20070 int opt = booleanValue(azArg[2]);
20071 rc2 = sqlite3_test_control(testctrl, opt);
20076 /* sqlite3_test_control(sqlite3*) */
20077 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
20078 rc2 = sqlite3_test_control(testctrl, p->db);
20082 case SQLITE_TESTCTRL_IMPOSTER:
20084 rc2 = sqlite3_test_control(testctrl, p->db,
20086 integerValue(azArg[3]),
20087 integerValue(azArg[4]));
20092 case SQLITE_TESTCTRL_SEEK_COUNT: {
20094 rc2 = sqlite3_test_control(testctrl, p->db, &x);
20095 utf8_printf(p->out, "%llu\n", x);
20101 case SQLITE_TESTCTRL_PARSER_COVERAGE:
20103 sqlite3_test_control(testctrl, p->out);
20109 if( isOk==0 && iCtrl>=0 ){
20110 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
20112 }else if( isOk==1 ){
20113 raw_printf(p->out, "%d\n", rc2);
20114 }else if( isOk==2 ){
20115 raw_printf(p->out, "0x%08x\n", rc2);
20118 #endif /* !defined(SQLITE_UNTESTABLE) */
20120 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
20122 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
20125 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
20127 enableTimer = booleanValue(azArg[1]);
20128 if( enableTimer && !HAS_TIMER ){
20129 raw_printf(stderr, "Error: timer not available on this system.\n");
20133 raw_printf(stderr, "Usage: .timer on|off\n");
20138 #ifndef SQLITE_OMIT_TRACE
20139 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
20143 for(jj=1; jj<nArg; jj++){
20144 const char *z = azArg[jj];
20146 if( optionMatch(z, "expanded") ){
20147 p->eTraceType = SHELL_TRACE_EXPANDED;
20149 #ifdef SQLITE_ENABLE_NORMALIZE
20150 else if( optionMatch(z, "normalized") ){
20151 p->eTraceType = SHELL_TRACE_NORMALIZED;
20154 else if( optionMatch(z, "plain") ){
20155 p->eTraceType = SHELL_TRACE_PLAIN;
20157 else if( optionMatch(z, "profile") ){
20158 mType |= SQLITE_TRACE_PROFILE;
20160 else if( optionMatch(z, "row") ){
20161 mType |= SQLITE_TRACE_ROW;
20163 else if( optionMatch(z, "stmt") ){
20164 mType |= SQLITE_TRACE_STMT;
20166 else if( optionMatch(z, "close") ){
20167 mType |= SQLITE_TRACE_CLOSE;
20170 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
20172 goto meta_command_exit;
20175 output_file_close(p->traceOut);
20176 p->traceOut = output_file_open(azArg[1], 0);
20179 if( p->traceOut==0 ){
20180 sqlite3_trace_v2(p->db, 0, 0, 0);
20182 if( mType==0 ) mType = SQLITE_TRACE_STMT;
20183 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
20186 #endif /* !defined(SQLITE_OMIT_TRACE) */
20188 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20189 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
20194 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
20196 goto meta_command_exit;
20200 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
20201 lenOpt = (int)strlen(zOpt);
20202 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
20203 assert( azArg[nArg]==0 );
20204 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
20206 for(ii=1; ii<nArg; ii++){
20207 sqlite3_create_module(p->db, azArg[ii], 0, 0);
20213 #if SQLITE_USER_AUTHENTICATION
20214 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
20216 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
20218 goto meta_command_exit;
20221 if( strcmp(azArg[1],"login")==0 ){
20223 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
20225 goto meta_command_exit;
20227 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
20228 strlen30(azArg[3]));
20230 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
20233 }else if( strcmp(azArg[1],"add")==0 ){
20235 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
20237 goto meta_command_exit;
20239 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20240 booleanValue(azArg[4]));
20242 raw_printf(stderr, "User-Add failed: %d\n", rc);
20245 }else if( strcmp(azArg[1],"edit")==0 ){
20247 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
20249 goto meta_command_exit;
20251 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20252 booleanValue(azArg[4]));
20254 raw_printf(stderr, "User-Edit failed: %d\n", rc);
20257 }else if( strcmp(azArg[1],"delete")==0 ){
20259 raw_printf(stderr, "Usage: .user delete USER\n");
20261 goto meta_command_exit;
20263 rc = sqlite3_user_delete(p->db, azArg[2]);
20265 raw_printf(stderr, "User-Delete failed: %d\n", rc);
20269 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
20271 goto meta_command_exit;
20274 #endif /* SQLITE_USER_AUTHENTICATION */
20276 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
20277 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
20278 sqlite3_libversion(), sqlite3_sourceid());
20279 #if SQLITE_HAVE_ZLIB
20280 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
20282 #define CTIMEOPT_VAL_(opt) #opt
20283 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20284 #if defined(__clang__) && defined(__clang_major__)
20285 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
20286 CTIMEOPT_VAL(__clang_minor__) "."
20287 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
20288 #elif defined(_MSC_VER)
20289 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
20290 #elif defined(__GNUC__) && defined(__VERSION__)
20291 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
20295 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
20296 const char *zDbName = nArg==2 ? azArg[1] : "main";
20297 sqlite3_vfs *pVfs = 0;
20299 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
20301 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
20302 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20303 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20304 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20309 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
20311 sqlite3_vfs *pCurrent = 0;
20313 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
20315 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
20316 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
20317 pVfs==pCurrent ? " <--- CURRENT" : "");
20318 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20319 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20320 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20322 raw_printf(p->out, "-----------------------------------\n");
20327 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
20328 const char *zDbName = nArg==2 ? azArg[1] : "main";
20329 char *zVfsName = 0;
20331 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
20333 utf8_printf(p->out, "%s\n", zVfsName);
20334 sqlite3_free(zVfsName);
20339 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
20340 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
20341 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
20345 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
20347 assert( nArg<=ArraySize(azArg) );
20348 p->nWidth = nArg-1;
20349 p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
20350 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
20351 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
20352 for(j=1; j<nArg; j++){
20353 p->colWidth[j-1] = (int)integerValue(azArg[j]);
20358 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
20359 " \"%s\". Enter \".help\" for help\n", azArg[0]);
20366 if( p->outCount==0 ) output_reset(p);
20372 ** Return TRUE if a semicolon occurs anywhere in the first N characters
20375 static int line_contains_semicolon(const char *z, int N){
20377 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
20382 ** Test to see if a line consists entirely of whitespace.
20384 static int _all_whitespace(const char *z){
20386 if( IsSpace(z[0]) ) continue;
20387 if( *z=='/' && z[1]=='*' ){
20389 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
20390 if( *z==0 ) return 0;
20394 if( *z=='-' && z[1]=='-' ){
20396 while( *z && *z!='\n' ){ z++; }
20397 if( *z==0 ) return 1;
20406 ** Return TRUE if the line typed in is an SQL command terminator other
20407 ** than a semi-colon. The SQL Server style "go" command is understood
20408 ** as is the Oracle "/".
20410 static int line_is_command_terminator(const char *zLine){
20411 while( IsSpace(zLine[0]) ){ zLine++; };
20412 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
20413 return 1; /* Oracle */
20415 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
20416 && _all_whitespace(&zLine[2]) ){
20417 return 1; /* SQL Server */
20423 ** We need a default sqlite3_complete() implementation to use in case
20424 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
20425 ** any arbitrary text is a complete SQL statement. This is not very
20426 ** user-friendly, but it does seem to work.
20428 #ifdef SQLITE_OMIT_COMPLETE
20429 #define sqlite3_complete(x) 1
20433 ** Return true if zSql is a complete SQL statement. Return false if it
20434 ** ends in the middle of a string literal or C-style comment.
20436 static int line_is_complete(char *zSql, int nSql){
20438 if( zSql==0 ) return 1;
20441 rc = sqlite3_complete(zSql);
20447 ** Run a single line of SQL. Return the number of errors.
20449 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
20454 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
20455 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
20457 rc = shell_exec(p, zSql, &zErrMsg);
20459 if( rc || zErrMsg ){
20461 if( in!=0 || !stdin_is_interactive ){
20462 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
20463 "Error: near line %d:", startline);
20465 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
20468 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
20469 sqlite3_free(zErrMsg);
20472 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
20475 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
20476 raw_printf(p->out, "changes: %3d total_changes: %d\n",
20477 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
20484 ** Read input from *in and process it. If *in==0 then input
20485 ** is interactive - the user is typing it it. Otherwise, input
20486 ** is coming from a file or device. A prompt is issued and history
20487 ** is saved only if input is interactive. An interrupt signal will
20488 ** cause this routine to exit immediately, unless input is interactive.
20490 ** Return the number of errors.
20492 static int process_input(ShellState *p){
20493 char *zLine = 0; /* A single input line */
20494 char *zSql = 0; /* Accumulated SQL text */
20495 int nLine; /* Length of current line */
20496 int nSql = 0; /* Bytes of zSql[] used */
20497 int nAlloc = 0; /* Allocated zSql[] space */
20498 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
20499 int rc; /* Error code */
20500 int errCnt = 0; /* Number of errors seen */
20501 int startline = 0; /* Line number for start of current input */
20504 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
20506 zLine = one_input_line(p->in, zLine, nSql>0);
20509 if( p->in==0 && stdin_is_interactive ) printf("\n");
20512 if( seenInterrupt ){
20513 if( p->in!=0 ) break;
20517 if( nSql==0 && _all_whitespace(zLine) ){
20518 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20521 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
20522 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20523 if( zLine[0]=='.' ){
20524 rc = do_meta_command(zLine, p);
20525 if( rc==2 ){ /* exit requested */
20533 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
20534 memcpy(zLine,";",2);
20536 nLine = strlen30(zLine);
20537 if( nSql+nLine+2>=nAlloc ){
20538 nAlloc = nSql+nLine+100;
20539 zSql = realloc(zSql, nAlloc);
20540 if( zSql==0 ) shell_out_of_memory();
20545 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
20546 assert( nAlloc>0 && zSql!=0 );
20547 memcpy(zSql, zLine+i, nLine+1-i);
20548 startline = p->lineno;
20551 zSql[nSql++] = '\n';
20552 memcpy(zSql+nSql, zLine, nLine+1);
20555 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
20556 && sqlite3_complete(zSql) ){
20557 errCnt += runOneSqlLine(p, zSql, p->in, startline);
20565 }else if( nSql && _all_whitespace(zSql) ){
20566 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
20570 if( nSql && !_all_whitespace(zSql) ){
20571 errCnt += runOneSqlLine(p, zSql, p->in, startline);
20579 ** Return a pathname which is the user's home directory. A
20580 ** 0 return indicates an error of some kind.
20582 static char *find_home_dir(int clearFlag){
20583 static char *home_dir = NULL;
20589 if( home_dir ) return home_dir;
20591 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
20592 && !defined(__RTP__) && !defined(_WRS_KERNEL)
20594 struct passwd *pwent;
20595 uid_t uid = getuid();
20596 if( (pwent=getpwuid(uid)) != NULL) {
20597 home_dir = pwent->pw_dir;
20602 #if defined(_WIN32_WCE)
20603 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
20608 #if defined(_WIN32) || defined(WIN32)
20610 home_dir = getenv("USERPROFILE");
20615 home_dir = getenv("HOME");
20618 #if defined(_WIN32) || defined(WIN32)
20620 char *zDrive, *zPath;
20622 zDrive = getenv("HOMEDRIVE");
20623 zPath = getenv("HOMEPATH");
20624 if( zDrive && zPath ){
20625 n = strlen30(zDrive) + strlen30(zPath) + 1;
20626 home_dir = malloc( n );
20627 if( home_dir==0 ) return 0;
20628 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
20635 #endif /* !_WIN32_WCE */
20638 int n = strlen30(home_dir) + 1;
20639 char *z = malloc( n );
20640 if( z ) memcpy(z, home_dir, n);
20648 ** Read input from the file given by sqliterc_override. Or if that
20649 ** parameter is NULL, take input from ~/.sqliterc
20651 ** Returns the number of errors.
20653 static void process_sqliterc(
20654 ShellState *p, /* Configuration data */
20655 const char *sqliterc_override /* Name of config file. NULL to use default */
20657 char *home_dir = NULL;
20658 const char *sqliterc = sqliterc_override;
20660 FILE *inSaved = p->in;
20661 int savedLineno = p->lineno;
20663 if (sqliterc == NULL) {
20664 home_dir = find_home_dir(0);
20666 raw_printf(stderr, "-- warning: cannot find home directory;"
20667 " cannot read ~/.sqliterc\n");
20670 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
20673 p->in = fopen(sqliterc,"rb");
20675 if( stdin_is_interactive ){
20676 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
20678 if( process_input(p) && bail_on_error ) exit(1);
20680 }else if( sqliterc_override!=0 ){
20681 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
20682 if( bail_on_error ) exit(1);
20685 p->lineno = savedLineno;
20686 sqlite3_free(zBuf);
20690 ** Show available command line options
20692 static const char zOptions[] =
20693 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20694 " -A ARGS... run \".archive ARGS\" and exit\n"
20696 " -append append the database to the end of the file\n"
20697 " -ascii set output mode to 'ascii'\n"
20698 " -bail stop after hitting an error\n"
20699 " -batch force batch I/O\n"
20700 " -box set output mode to 'box'\n"
20701 " -column set output mode to 'column'\n"
20702 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
20703 " -csv set output mode to 'csv'\n"
20704 #if defined(SQLITE_ENABLE_DESERIALIZE)
20705 " -deserialize open the database using sqlite3_deserialize()\n"
20707 " -echo print commands before execution\n"
20708 " -init FILENAME read/process named file\n"
20709 " -[no]header turn headers on or off\n"
20710 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20711 " -heap SIZE Size of heap for memsys3 or memsys5\n"
20713 " -help show this message\n"
20714 " -html set output mode to HTML\n"
20715 " -interactive force interactive I/O\n"
20716 " -json set output mode to 'json'\n"
20717 " -line set output mode to 'line'\n"
20718 " -list set output mode to 'list'\n"
20719 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
20720 " -markdown set output mode to 'markdown'\n"
20721 #if defined(SQLITE_ENABLE_DESERIALIZE)
20722 " -maxsize N maximum size for a --deserialize database\n"
20724 " -memtrace trace all memory allocations and deallocations\n"
20725 " -mmap N default mmap size set to N\n"
20726 #ifdef SQLITE_ENABLE_MULTIPLEX
20727 " -multiplex enable the multiplexor VFS\n"
20729 " -newline SEP set output row separator. Default: '\\n'\n"
20730 " -nofollow refuse to open symbolic links to database files\n"
20731 " -nullvalue TEXT set text string for NULL values. Default ''\n"
20732 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
20733 " -quote set output mode to 'quote'\n"
20734 " -readonly open the database read-only\n"
20735 " -separator SEP set output column separator. Default: '|'\n"
20736 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
20737 " -sorterref SIZE sorter references threshold size\n"
20739 " -stats print memory stats before each finalize\n"
20740 " -table set output mode to 'table'\n"
20741 " -tabs set output mode to 'tabs'\n"
20742 " -version show SQLite version\n"
20743 " -vfs NAME use NAME as the default VFS\n"
20744 #ifdef SQLITE_ENABLE_VFSTRACE
20745 " -vfstrace enable tracing of all VFS calls\n"
20747 #ifdef SQLITE_HAVE_ZLIB
20748 " -zip open the file as a ZIP Archive\n"
20751 static void usage(int showDetail){
20752 utf8_printf(stderr,
20753 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
20754 "FILENAME is the name of an SQLite database. A new database is created\n"
20755 "if the file does not previously exist.\n", Argv0);
20757 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
20759 raw_printf(stderr, "Use the -help option for additional information\n");
20765 ** Internal check: Verify that the SQLite is uninitialized. Print a
20766 ** error message if it is initialized.
20768 static void verify_uninitialized(void){
20769 if( sqlite3_config(-1)==SQLITE_MISUSE ){
20770 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
20771 " initialization.\n");
20776 ** Initialize the state information in data
20778 static void main_init(ShellState *data) {
20779 memset(data, 0, sizeof(*data));
20780 data->normalMode = data->cMode = data->mode = MODE_List;
20781 data->autoExplain = 1;
20782 memcpy(data->colSeparator,SEP_Column, 2);
20783 memcpy(data->rowSeparator,SEP_Row, 2);
20784 data->showHeader = 0;
20785 data->shellFlgs = SHFLG_Lookaside;
20786 verify_uninitialized();
20787 sqlite3_config(SQLITE_CONFIG_URI, 1);
20788 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
20789 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
20790 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
20791 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
20795 ** Output text to the console in a font that attracts extra attention.
20798 static void printBold(const char *zText){
20799 #if !SQLITE_OS_WINRT
20800 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
20801 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
20802 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
20803 SetConsoleTextAttribute(out,
20804 FOREGROUND_RED|FOREGROUND_INTENSITY
20807 printf("%s", zText);
20808 #if !SQLITE_OS_WINRT
20809 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
20813 static void printBold(const char *zText){
20814 printf("\033[1m%s\033[0m", zText);
20819 ** Get the argument to an --option. Throw an error and die if no argument
20822 static char *cmdline_option_value(int argc, char **argv, int i){
20824 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
20825 argv[0], argv[argc-1]);
20831 #ifndef SQLITE_SHELL_IS_UTF8
20832 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
20833 # define SQLITE_SHELL_IS_UTF8 (0)
20835 # define SQLITE_SHELL_IS_UTF8 (1)
20839 #if SQLITE_SHELL_IS_UTF8
20840 int SQLITE_CDECL main(int argc, char **argv){
20842 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
20847 const char *zInitFile = 0;
20850 int warnInmemoryDb = 0;
20854 const char *zVfs = 0; /* Value of -vfs command-line option */
20855 #if !SQLITE_SHELL_IS_UTF8
20856 char **argvToFree = 0;
20857 int argcToFree = 0;
20860 setBinaryMode(stdin, 0);
20861 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
20862 stdin_is_interactive = isatty(0);
20863 stdout_is_console = isatty(1);
20865 #ifdef SQLITE_DEBUG
20866 registerOomSimulator();
20869 #if !defined(_WIN32_WCE)
20870 if( getenv("SQLITE_DEBUG_BREAK") ){
20871 if( isatty(0) && isatty(2) ){
20873 "attach debugger to process %d and press any key to continue.\n",
20877 #if defined(_WIN32) || defined(WIN32)
20878 #if SQLITE_OS_WINRT
20883 #elif defined(SIGTRAP)
20890 #if USE_SYSTEM_SQLITE+0!=1
20891 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
20892 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
20893 sqlite3_sourceid(), SQLITE_SOURCE_ID);
20899 /* On Windows, we must translate command-line arguments into UTF-8.
20900 ** The SQLite memory allocator subsystem has to be enabled in order to
20901 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
20902 ** subsequent sqlite3_config() calls will work. So copy all results into
20903 ** memory that does not come from the SQLite memory allocator.
20905 #if !SQLITE_SHELL_IS_UTF8
20906 sqlite3_initialize();
20907 argvToFree = malloc(sizeof(argv[0])*argc*2);
20909 argv = argvToFree + argc;
20910 if( argv==0 ) shell_out_of_memory();
20911 for(i=0; i<argc; i++){
20912 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
20914 if( z==0 ) shell_out_of_memory();
20915 n = (int)strlen(z);
20916 argv[i] = malloc( n+1 );
20917 if( argv[i]==0 ) shell_out_of_memory();
20918 memcpy(argv[i], z, n+1);
20919 argvToFree[i] = argv[i];
20922 sqlite3_shutdown();
20925 assert( argc>=1 && argv && argv[0] );
20928 /* Make sure we have a valid signal handler early, before anything
20932 signal(SIGINT, interrupt_handler);
20933 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
20934 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
20937 #ifdef SQLITE_SHELL_DBNAME_PROC
20939 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
20940 ** of a C-function that will provide the name of the database file. Use
20941 ** this compile-time option to embed this shell program in larger
20942 ** applications. */
20943 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
20944 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
20945 warnInmemoryDb = 0;
20949 /* Do an initial pass through the command-line argument to locate
20950 ** the name of the database file, the name of the initialization file,
20951 ** the size of the alternative malloc heap,
20952 ** and the first command to execute.
20954 verify_uninitialized();
20955 for(i=1; i<argc; i++){
20959 if( data.zDbFilename==0 ){
20960 data.zDbFilename = z;
20962 /* Excesss arguments are interpreted as SQL (or dot-commands) and
20963 ** mean that nothing is read from stdin */
20966 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
20967 if( azCmd==0 ) shell_out_of_memory();
20971 if( z[1]=='-' ) z++;
20972 if( strcmp(z,"-separator")==0
20973 || strcmp(z,"-nullvalue")==0
20974 || strcmp(z,"-newline")==0
20975 || strcmp(z,"-cmd")==0
20977 (void)cmdline_option_value(argc, argv, ++i);
20978 }else if( strcmp(z,"-init")==0 ){
20979 zInitFile = cmdline_option_value(argc, argv, ++i);
20980 }else if( strcmp(z,"-batch")==0 ){
20981 /* Need to check for batch mode here to so we can avoid printing
20982 ** informational messages (like from process_sqliterc) before
20983 ** we do the actual processing of arguments later in a second pass.
20985 stdin_is_interactive = 0;
20986 }else if( strcmp(z,"-heap")==0 ){
20987 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20989 sqlite3_int64 szHeap;
20991 zSize = cmdline_option_value(argc, argv, ++i);
20992 szHeap = integerValue(zSize);
20993 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
20994 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
20996 (void)cmdline_option_value(argc, argv, ++i);
20998 }else if( strcmp(z,"-pagecache")==0 ){
20999 sqlite3_int64 n, sz;
21000 sz = integerValue(cmdline_option_value(argc,argv,++i));
21001 if( sz>70000 ) sz = 70000;
21003 n = integerValue(cmdline_option_value(argc,argv,++i));
21004 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21005 n = 0xffffffffffffLL/sz;
21007 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
21008 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
21009 data.shellFlgs |= SHFLG_Pagecache;
21010 }else if( strcmp(z,"-lookaside")==0 ){
21012 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
21014 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
21016 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
21017 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
21018 #ifdef SQLITE_ENABLE_VFSTRACE
21019 }else if( strcmp(z,"-vfstrace")==0 ){
21020 extern int vfstrace_register(
21021 const char *zTraceName,
21022 const char *zOldVfsName,
21023 int (*xOut)(const char*,void*),
21027 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
21029 #ifdef SQLITE_ENABLE_MULTIPLEX
21030 }else if( strcmp(z,"-multiplex")==0 ){
21031 extern int sqlite3_multiple_initialize(const char*,int);
21032 sqlite3_multiplex_initialize(0, 1);
21034 }else if( strcmp(z,"-mmap")==0 ){
21035 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21036 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
21037 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21038 }else if( strcmp(z,"-sorterref")==0 ){
21039 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21040 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
21042 }else if( strcmp(z,"-vfs")==0 ){
21043 zVfs = cmdline_option_value(argc, argv, ++i);
21044 #ifdef SQLITE_HAVE_ZLIB
21045 }else if( strcmp(z,"-zip")==0 ){
21046 data.openMode = SHELL_OPEN_ZIPFILE;
21048 }else if( strcmp(z,"-append")==0 ){
21049 data.openMode = SHELL_OPEN_APPENDVFS;
21050 #ifdef SQLITE_ENABLE_DESERIALIZE
21051 }else if( strcmp(z,"-deserialize")==0 ){
21052 data.openMode = SHELL_OPEN_DESERIALIZE;
21053 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21054 data.szMax = integerValue(argv[++i]);
21056 }else if( strcmp(z,"-readonly")==0 ){
21057 data.openMode = SHELL_OPEN_READONLY;
21058 }else if( strcmp(z,"-nofollow")==0 ){
21059 data.openFlags = SQLITE_OPEN_NOFOLLOW;
21060 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21061 }else if( strncmp(z, "-A",2)==0 ){
21062 /* All remaining command-line arguments are passed to the ".archive"
21063 ** command, so ignore them */
21066 }else if( strcmp(z, "-memtrace")==0 ){
21067 sqlite3MemTraceActivate(stderr);
21068 }else if( strcmp(z,"-bail")==0 ){
21072 verify_uninitialized();
21075 #ifdef SQLITE_SHELL_INIT_PROC
21077 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
21078 ** of a C-function that will perform initialization actions on SQLite that
21079 ** occur just before or after sqlite3_initialize(). Use this compile-time
21080 ** option to embed this shell program in larger applications. */
21081 extern void SQLITE_SHELL_INIT_PROC(void);
21082 SQLITE_SHELL_INIT_PROC();
21085 /* All the sqlite3_config() calls have now been made. So it is safe
21086 ** to call sqlite3_initialize() and process any command line -vfs option. */
21087 sqlite3_initialize();
21091 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
21093 sqlite3_vfs_register(pVfs, 1);
21095 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
21100 if( data.zDbFilename==0 ){
21101 #ifndef SQLITE_OMIT_MEMORYDB
21102 data.zDbFilename = ":memory:";
21103 warnInmemoryDb = argc==1;
21105 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
21110 sqlite3_appendvfs_init(0,0,0);
21112 /* Go ahead and open the database file if it already exists. If the
21113 ** file does not exist, delay opening it. This prevents empty database
21114 ** files from being created if a user mistypes the database name argument
21115 ** to the sqlite command-line tool.
21117 if( access(data.zDbFilename, 0)==0 ){
21121 /* Process the initialization file if there is one. If no -init option
21122 ** is given on the command line, look for a file named ~/.sqliterc and
21123 ** try to process it.
21125 process_sqliterc(&data,zInitFile);
21127 /* Make a second pass through the command-line argument and set
21128 ** options. This second pass is delayed until after the initialization
21129 ** file is processed so that the command-line arguments will override
21130 ** settings in the initialization file.
21132 for(i=1; i<argc; i++){
21134 if( z[0]!='-' ) continue;
21135 if( z[1]=='-' ){ z++; }
21136 if( strcmp(z,"-init")==0 ){
21138 }else if( strcmp(z,"-html")==0 ){
21139 data.mode = MODE_Html;
21140 }else if( strcmp(z,"-list")==0 ){
21141 data.mode = MODE_List;
21142 }else if( strcmp(z,"-quote")==0 ){
21143 data.mode = MODE_Quote;
21144 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
21145 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21146 }else if( strcmp(z,"-line")==0 ){
21147 data.mode = MODE_Line;
21148 }else if( strcmp(z,"-column")==0 ){
21149 data.mode = MODE_Column;
21150 }else if( strcmp(z,"-json")==0 ){
21151 data.mode = MODE_Json;
21152 }else if( strcmp(z,"-markdown")==0 ){
21153 data.mode = MODE_Markdown;
21154 }else if( strcmp(z,"-table")==0 ){
21155 data.mode = MODE_Table;
21156 }else if( strcmp(z,"-box")==0 ){
21157 data.mode = MODE_Box;
21158 }else if( strcmp(z,"-csv")==0 ){
21159 data.mode = MODE_Csv;
21160 memcpy(data.colSeparator,",",2);
21161 #ifdef SQLITE_HAVE_ZLIB
21162 }else if( strcmp(z,"-zip")==0 ){
21163 data.openMode = SHELL_OPEN_ZIPFILE;
21165 }else if( strcmp(z,"-append")==0 ){
21166 data.openMode = SHELL_OPEN_APPENDVFS;
21167 #ifdef SQLITE_ENABLE_DESERIALIZE
21168 }else if( strcmp(z,"-deserialize")==0 ){
21169 data.openMode = SHELL_OPEN_DESERIALIZE;
21170 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21171 data.szMax = integerValue(argv[++i]);
21173 }else if( strcmp(z,"-readonly")==0 ){
21174 data.openMode = SHELL_OPEN_READONLY;
21175 }else if( strcmp(z,"-nofollow")==0 ){
21176 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
21177 }else if( strcmp(z,"-ascii")==0 ){
21178 data.mode = MODE_Ascii;
21179 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
21180 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
21181 }else if( strcmp(z,"-tabs")==0 ){
21182 data.mode = MODE_List;
21183 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
21184 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21185 }else if( strcmp(z,"-separator")==0 ){
21186 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
21187 "%s",cmdline_option_value(argc,argv,++i));
21188 }else if( strcmp(z,"-newline")==0 ){
21189 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
21190 "%s",cmdline_option_value(argc,argv,++i));
21191 }else if( strcmp(z,"-nullvalue")==0 ){
21192 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
21193 "%s",cmdline_option_value(argc,argv,++i));
21194 }else if( strcmp(z,"-header")==0 ){
21195 data.showHeader = 1;
21196 }else if( strcmp(z,"-noheader")==0 ){
21197 data.showHeader = 0;
21198 }else if( strcmp(z,"-echo")==0 ){
21199 ShellSetFlag(&data, SHFLG_Echo);
21200 }else if( strcmp(z,"-eqp")==0 ){
21201 data.autoEQP = AUTOEQP_on;
21202 }else if( strcmp(z,"-eqpfull")==0 ){
21203 data.autoEQP = AUTOEQP_full;
21204 }else if( strcmp(z,"-stats")==0 ){
21206 }else if( strcmp(z,"-scanstats")==0 ){
21207 data.scanstatsOn = 1;
21208 }else if( strcmp(z,"-backslash")==0 ){
21209 /* Undocumented command-line option: -backslash
21210 ** Causes C-style backslash escapes to be evaluated in SQL statements
21211 ** prior to sending the SQL into SQLite. Useful for injecting
21212 ** crazy bytes in the middle of SQL statements for testing and debugging.
21214 ShellSetFlag(&data, SHFLG_Backslash);
21215 }else if( strcmp(z,"-bail")==0 ){
21216 /* No-op. The bail_on_error flag should already be set. */
21217 }else if( strcmp(z,"-version")==0 ){
21218 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
21220 }else if( strcmp(z,"-interactive")==0 ){
21221 stdin_is_interactive = 1;
21222 }else if( strcmp(z,"-batch")==0 ){
21223 stdin_is_interactive = 0;
21224 }else if( strcmp(z,"-heap")==0 ){
21226 }else if( strcmp(z,"-pagecache")==0 ){
21228 }else if( strcmp(z,"-lookaside")==0 ){
21230 }else if( strcmp(z,"-mmap")==0 ){
21232 }else if( strcmp(z,"-memtrace")==0 ){
21234 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21235 }else if( strcmp(z,"-sorterref")==0 ){
21238 }else if( strcmp(z,"-vfs")==0 ){
21240 #ifdef SQLITE_ENABLE_VFSTRACE
21241 }else if( strcmp(z,"-vfstrace")==0 ){
21244 #ifdef SQLITE_ENABLE_MULTIPLEX
21245 }else if( strcmp(z,"-multiplex")==0 ){
21248 }else if( strcmp(z,"-help")==0 ){
21250 }else if( strcmp(z,"-cmd")==0 ){
21251 /* Run commands that follow -cmd first and separately from commands
21252 ** that simply appear on the command-line. This seems goofy. It would
21253 ** be better if all commands ran in the order that they appear. But
21254 ** we retain the goofy behavior for historical compatibility. */
21255 if( i==argc-1 ) break;
21256 z = cmdline_option_value(argc,argv,++i);
21258 rc = do_meta_command(z, &data);
21259 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
21262 rc = shell_exec(&data, z, &zErrMsg);
21264 utf8_printf(stderr,"Error: %s\n", zErrMsg);
21265 if( bail_on_error ) return rc!=0 ? rc : 1;
21267 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
21268 if( bail_on_error ) return rc;
21271 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21272 }else if( strncmp(z, "-A", 2)==0 ){
21274 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
21275 " with \"%s\"\n", z);
21278 open_db(&data, OPEN_DB_ZIPFILE);
21281 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
21283 arDotCommand(&data, 1, argv+i, argc-i);
21289 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
21290 raw_printf(stderr,"Use -help for a list of options.\n");
21293 data.cMode = data.mode;
21297 /* Run all arguments that do not begin with '-' as if they were separate
21298 ** command-line inputs, except for the argToSkip argument which contains
21299 ** the database filename.
21301 for(i=0; i<nCmd; i++){
21302 if( azCmd[i][0]=='.' ){
21303 rc = do_meta_command(azCmd[i], &data);
21306 return rc==2 ? 0 : rc;
21310 rc = shell_exec(&data, azCmd[i], &zErrMsg);
21311 if( zErrMsg || rc ){
21313 utf8_printf(stderr,"Error: %s\n", zErrMsg);
21315 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
21317 sqlite3_free(zErrMsg);
21319 return rc!=0 ? rc : 1;
21324 /* Run commands received from standard input
21326 if( stdin_is_interactive ){
21331 "SQLite version %s %.19s\n" /*extra-version-info*/
21332 "Enter \".help\" for usage hints.\n",
21333 sqlite3_libversion(), sqlite3_sourceid()
21335 if( warnInmemoryDb ){
21336 printf("Connected to a ");
21337 printBold("transient in-memory database");
21338 printf(".\nUse \".open FILENAME\" to reopen on a "
21339 "persistent database.\n");
21341 zHistory = getenv("SQLITE_HISTORY");
21343 zHistory = strdup(zHistory);
21344 }else if( (zHome = find_home_dir(0))!=0 ){
21345 nHistory = strlen30(zHome) + 20;
21346 if( (zHistory = malloc(nHistory))!=0 ){
21347 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
21350 if( zHistory ){ shell_read_history(zHistory); }
21351 #if HAVE_READLINE || HAVE_EDITLINE
21352 rl_attempted_completion_function = readline_completion;
21353 #elif HAVE_LINENOISE
21354 linenoiseSetCompletionCallback(linenoise_completion);
21357 rc = process_input(&data);
21359 shell_stifle_history(2000);
21360 shell_write_history(zHistory);
21365 rc = process_input(&data);
21369 set_table_name(&data, 0);
21371 session_close_all(&data);
21374 sqlite3_free(data.zFreeOnClose);
21376 output_reset(&data);
21377 data.doXdgOpen = 0;
21378 clearTempFile(&data);
21379 #if !SQLITE_SHELL_IS_UTF8
21380 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
21383 free(data.colWidth);
21384 /* Clear the global data structure so that valgrind will detect memory
21386 memset(&data, 0, sizeof(data));