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);
2018 hash_step_vformat(&cx,"S%d:",n);
2019 SHA3Update(&cx,(unsigned char*)z,n);
2021 /* Compute a hash over the result of the query */
2022 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2023 SHA3Update(&cx,(const unsigned char*)"R",1);
2024 for(i=0; i<nCol; i++){
2025 switch( sqlite3_column_type(pStmt,i) ){
2027 SHA3Update(&cx, (const unsigned char*)"N",1);
2030 case SQLITE_INTEGER: {
2034 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2036 for(j=8; j>=1; j--){
2041 SHA3Update(&cx, x, 9);
2044 case SQLITE_FLOAT: {
2048 double r = sqlite3_column_double(pStmt,i);
2050 for(j=8; j>=1; j--){
2055 SHA3Update(&cx,x,9);
2059 int n2 = sqlite3_column_bytes(pStmt, i);
2060 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2061 hash_step_vformat(&cx,"T%d:",n2);
2062 SHA3Update(&cx, z2, n2);
2066 int n2 = sqlite3_column_bytes(pStmt, i);
2067 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2068 hash_step_vformat(&cx,"B%d:",n2);
2069 SHA3Update(&cx, z2, n2);
2075 sqlite3_finalize(pStmt);
2077 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2084 int sqlite3_shathree_init(
2087 const sqlite3_api_routines *pApi
2090 SQLITE_EXTENSION_INIT2(pApi);
2091 (void)pzErrMsg; /* Unused parameter */
2092 rc = sqlite3_create_function(db, "sha3", 1,
2093 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2095 if( rc==SQLITE_OK ){
2096 rc = sqlite3_create_function(db, "sha3", 2,
2097 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2100 if( rc==SQLITE_OK ){
2101 rc = sqlite3_create_function(db, "sha3_query", 1,
2102 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2103 0, sha3QueryFunc, 0, 0);
2105 if( rc==SQLITE_OK ){
2106 rc = sqlite3_create_function(db, "sha3_query", 2,
2107 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2108 0, sha3QueryFunc, 0, 0);
2113 /************************* End ../ext/misc/shathree.c ********************/
2114 /************************* Begin ../ext/misc/fileio.c ******************/
2118 ** The author disclaims copyright to this source code. In place of
2119 ** a legal notice, here is a blessing:
2121 ** May you do good and not evil.
2122 ** May you find forgiveness for yourself and forgive others.
2123 ** May you share freely, never taking more than you give.
2125 ******************************************************************************
2127 ** This SQLite extension implements SQL functions readfile() and
2128 ** writefile(), and eponymous virtual type "fsdir".
2130 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2132 ** If neither of the optional arguments is present, then this UDF
2133 ** function writes blob DATA to file FILE. If successful, the number
2134 ** of bytes written is returned. If an error occurs, NULL is returned.
2136 ** If the first option argument - MODE - is present, then it must
2137 ** be passed an integer value that corresponds to a POSIX mode
2138 ** value (file type + permissions, as returned in the stat.st_mode
2139 ** field by the stat() system call). Three types of files may
2140 ** be written/created:
2142 ** regular files: (mode & 0170000)==0100000
2143 ** symbolic links: (mode & 0170000)==0120000
2144 ** directories: (mode & 0170000)==0040000
2146 ** For a directory, the DATA is ignored. For a symbolic link, it is
2147 ** interpreted as text and used as the target of the link. For a
2148 ** regular file, it is interpreted as a blob and written into the
2149 ** named file. Regardless of the type of file, its permissions are
2150 ** set to (mode & 0777) before returning.
2152 ** If the optional MTIME argument is present, then it is interpreted
2153 ** as an integer - the number of seconds since the unix epoch. The
2154 ** modification-time of the target file is set to this value before
2157 ** If three or more arguments are passed to this function and an
2158 ** error is encountered, an exception is raised.
2162 ** Read and return the contents of file FILE (type blob) from disk.
2168 ** SELECT * FROM fsdir($path [, $dir]);
2170 ** Parameter $path is an absolute or relative pathname. If the file that it
2171 ** refers to does not exist, it is an error. If the path refers to a regular
2172 ** file or symbolic link, it returns a single row. Or, if the path refers
2173 ** to a directory, it returns one row for the directory, and one row for each
2174 ** file within the hierarchy rooted at $path.
2176 ** Each row has the following columns:
2178 ** name: Path to file or directory (text value).
2179 ** mode: Value of stat.st_mode for directory entry (an integer).
2180 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2181 ** data: For a regular file, a blob containing the file data. For a
2182 ** symlink, a text value containing the text of the link. For a
2185 ** If a non-NULL value is specified for the optional $dir parameter and
2186 ** $path is a relative path, then $path is interpreted relative to $dir.
2187 ** And the paths returned in the "name" column of the table are also
2188 ** relative to directory $dir.
2190 /* #include "sqlite3ext.h" */
2191 SQLITE_EXTENSION_INIT1
2196 #include <sys/types.h>
2197 #include <sys/stat.h>
2199 #if !defined(_WIN32) && !defined(WIN32)
2200 # include <unistd.h>
2201 # include <dirent.h>
2203 # include <sys/time.h>
2205 # include "windows.h"
2207 # include <direct.h>
2208 /* # include "test_windirent.h" */
2209 # define dirent DIRENT
2211 # define chmod _chmod
2216 # define mkdir(path,mode) _mkdir(path)
2217 # define lstat(path,buf) stat(path,buf)
2224 ** Structure of the fsdir() table-valued function
2227 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2228 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2229 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2230 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2231 #define FSDIR_COLUMN_DATA 3 /* File content */
2232 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2233 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2237 ** Set the result stored by context ctx to a blob containing the
2238 ** contents of file zName. Or, leave the result unchanged (NULL)
2239 ** if the file does not exist or is unreadable.
2241 ** If the file exceeds the SQLite blob size limit, through an
2242 ** SQLITE_TOOBIG error.
2244 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2247 static void readFileContents(sqlite3_context *ctx, const char *zName){
2254 in = fopen(zName, "rb");
2256 /* File does not exist or is unreadable. Leave the result set to NULL. */
2259 fseek(in, 0, SEEK_END);
2262 db = sqlite3_context_db_handle(ctx);
2263 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2265 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2269 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2271 sqlite3_result_error_nomem(ctx);
2275 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2276 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2278 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2285 ** Implementation of the "readfile(X)" SQL function. The entire content
2286 ** of the file named X is read and returned as a BLOB. NULL is returned
2287 ** if the file does not exist or is unreadable.
2289 static void readfileFunc(
2290 sqlite3_context *context,
2292 sqlite3_value **argv
2295 (void)(argc); /* Unused parameter */
2296 zName = (const char*)sqlite3_value_text(argv[0]);
2297 if( zName==0 ) return;
2298 readFileContents(context, zName);
2302 ** Set the error message contained in context ctx to the results of
2303 ** vprintf(zFmt, ...).
2305 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2309 zMsg = sqlite3_vmprintf(zFmt, ap);
2310 sqlite3_result_error(ctx, zMsg, -1);
2317 ** This function is designed to convert a Win32 FILETIME structure into the
2318 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2320 static sqlite3_uint64 fileTimeToUnixTime(
2321 LPFILETIME pFileTime
2323 SYSTEMTIME epochSystemTime;
2324 ULARGE_INTEGER epochIntervals;
2325 FILETIME epochFileTime;
2326 ULARGE_INTEGER fileIntervals;
2328 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2329 epochSystemTime.wYear = 1970;
2330 epochSystemTime.wMonth = 1;
2331 epochSystemTime.wDay = 1;
2332 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2333 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2334 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2336 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2337 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2339 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2343 ** This function attempts to normalize the time values found in the stat()
2344 ** buffer to UTC. This is necessary on Win32, where the runtime library
2345 ** appears to return these values as local times.
2347 static void statTimesToUtc(
2349 struct stat *pStatBuf
2352 WIN32_FIND_DATAW fd;
2353 LPWSTR zUnicodeName;
2354 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2355 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2357 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2358 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2359 if( hFindFile!=NULL ){
2360 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2361 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2362 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2363 FindClose(hFindFile);
2365 sqlite3_free(zUnicodeName);
2371 ** This function is used in place of stat(). On Windows, special handling
2372 ** is required in order for the included time to be returned as UTC. On all
2373 ** other systems, this function simply calls stat().
2375 static int fileStat(
2377 struct stat *pStatBuf
2380 int rc = stat(zPath, pStatBuf);
2381 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2384 return stat(zPath, pStatBuf);
2389 ** This function is used in place of lstat(). On Windows, special handling
2390 ** is required in order for the included time to be returned as UTC. On all
2391 ** other systems, this function simply calls lstat().
2393 static int fileLinkStat(
2395 struct stat *pStatBuf
2398 int rc = lstat(zPath, pStatBuf);
2399 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2402 return lstat(zPath, pStatBuf);
2407 ** Argument zFile is the name of a file that will be created and/or written
2408 ** by SQL function writefile(). This function ensures that the directory
2409 ** zFile will be written to exists, creating it if required. The permissions
2410 ** for any path components created by this function are set in accordance
2411 ** with the current umask.
2413 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2414 ** SQLITE_OK is returned if the directory is successfully created, or
2415 ** SQLITE_ERROR otherwise.
2417 static int makeDirectory(
2420 char *zCopy = sqlite3_mprintf("%s", zFile);
2426 int nCopy = (int)strlen(zCopy);
2429 while( rc==SQLITE_OK ){
2433 for(; zCopy[i]!='/' && i<nCopy; i++);
2434 if( i==nCopy ) break;
2437 rc2 = fileStat(zCopy, &sStat);
2439 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2441 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2447 sqlite3_free(zCopy);
2454 ** This function does the work for the writefile() UDF. Refer to
2455 ** header comments at the top of this file for details.
2457 static int writeFile(
2458 sqlite3_context *pCtx, /* Context to return bytes written in */
2459 const char *zFile, /* File to write */
2460 sqlite3_value *pData, /* Data to write */
2461 mode_t mode, /* MODE parameter passed to writefile() */
2462 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2464 #if !defined(_WIN32) && !defined(WIN32)
2465 if( S_ISLNK(mode) ){
2466 const char *zTo = (const char*)sqlite3_value_text(pData);
2467 if( symlink(zTo, zFile)<0 ) return 1;
2471 if( S_ISDIR(mode) ){
2472 if( mkdir(zFile, mode) ){
2473 /* The mkdir() call to create the directory failed. This might not
2474 ** be an error though - if there is already a directory at the same
2475 ** path and either the permissions already match or can be changed
2476 ** to do so using chmod(), it is not an error. */
2479 || 0!=fileStat(zFile, &sStat)
2480 || !S_ISDIR(sStat.st_mode)
2481 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2487 sqlite3_int64 nWrite = 0;
2490 FILE *out = fopen(zFile, "wb");
2491 if( out==0 ) return 1;
2492 z = (const char*)sqlite3_value_blob(pData);
2494 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2495 nWrite = sqlite3_value_bytes(pData);
2501 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2505 sqlite3_result_int64(pCtx, nWrite);
2511 #if !SQLITE_OS_WINRT
2513 FILETIME lastAccess;
2515 SYSTEMTIME currentTime;
2518 LPWSTR zUnicodeName;
2519 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2521 GetSystemTime(¤tTime);
2522 SystemTimeToFileTime(¤tTime, &lastAccess);
2523 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2524 lastWrite.dwLowDateTime = (DWORD)intervals;
2525 lastWrite.dwHighDateTime = intervals >> 32;
2526 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2527 if( zUnicodeName==0 ){
2530 hFile = CreateFileW(
2531 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2532 FILE_FLAG_BACKUP_SEMANTICS, NULL
2534 sqlite3_free(zUnicodeName);
2535 if( hFile!=INVALID_HANDLE_VALUE ){
2536 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2543 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2545 struct timespec times[2];
2546 times[0].tv_nsec = times[1].tv_nsec = 0;
2547 times[0].tv_sec = time(0);
2548 times[1].tv_sec = mtime;
2549 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2554 struct timeval times[2];
2555 times[0].tv_usec = times[1].tv_usec = 0;
2556 times[0].tv_sec = time(0);
2557 times[1].tv_sec = mtime;
2558 if( utimes(zFile, times) ){
2568 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2569 ** Refer to header comments at the top of this file for details.
2571 static void writefileFunc(
2572 sqlite3_context *context,
2574 sqlite3_value **argv
2579 sqlite3_int64 mtime = -1;
2581 if( argc<2 || argc>4 ){
2582 sqlite3_result_error(context,
2583 "wrong number of arguments to function writefile()", -1
2588 zFile = (const char*)sqlite3_value_text(argv[0]);
2589 if( zFile==0 ) return;
2591 mode = (mode_t)sqlite3_value_int(argv[2]);
2594 mtime = sqlite3_value_int64(argv[3]);
2597 res = writeFile(context, zFile, argv[1], mode, mtime);
2598 if( res==1 && errno==ENOENT ){
2599 if( makeDirectory(zFile)==SQLITE_OK ){
2600 res = writeFile(context, zFile, argv[1], mode, mtime);
2604 if( argc>2 && res!=0 ){
2605 if( S_ISLNK(mode) ){
2606 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2607 }else if( S_ISDIR(mode) ){
2608 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2610 ctxErrorMsg(context, "failed to write file: %s", zFile);
2616 ** SQL function: lsmode(MODE)
2618 ** Given a numberic st_mode from stat(), convert it into a human-readable
2619 ** text string in the style of "ls -l".
2621 static void lsModeFunc(
2622 sqlite3_context *context,
2624 sqlite3_value **argv
2627 int iMode = sqlite3_value_int(argv[0]);
2630 if( S_ISLNK(iMode) ){
2632 }else if( S_ISREG(iMode) ){
2634 }else if( S_ISDIR(iMode) ){
2640 int m = (iMode >> ((2-i)*3));
2641 char *a = &z[1 + i*3];
2642 a[0] = (m & 0x4) ? 'r' : '-';
2643 a[1] = (m & 0x2) ? 'w' : '-';
2644 a[2] = (m & 0x1) ? 'x' : '-';
2647 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2650 #ifndef SQLITE_OMIT_VIRTUALTABLE
2653 ** Cursor type for recursively iterating through a directory structure.
2655 typedef struct fsdir_cursor fsdir_cursor;
2656 typedef struct FsdirLevel FsdirLevel;
2659 DIR *pDir; /* From opendir() */
2660 char *zDir; /* Name of directory (nul-terminated) */
2663 struct fsdir_cursor {
2664 sqlite3_vtab_cursor base; /* Base class - must be first */
2666 int nLvl; /* Number of entries in aLvl[] array */
2667 int iLvl; /* Index of current entry */
2668 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2673 struct stat sStat; /* Current lstat() results */
2674 char *zPath; /* Path to current entry */
2675 sqlite3_int64 iRowid; /* Current rowid */
2678 typedef struct fsdir_tab fsdir_tab;
2680 sqlite3_vtab base; /* Base class - must be first */
2684 ** Construct a new fsdir virtual table object.
2686 static int fsdirConnect(
2689 int argc, const char *const*argv,
2690 sqlite3_vtab **ppVtab,
2693 fsdir_tab *pNew = 0;
2699 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2700 if( rc==SQLITE_OK ){
2701 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2702 if( pNew==0 ) return SQLITE_NOMEM;
2703 memset(pNew, 0, sizeof(*pNew));
2704 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2706 *ppVtab = (sqlite3_vtab*)pNew;
2711 ** This method is the destructor for fsdir vtab objects.
2713 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2714 sqlite3_free(pVtab);
2719 ** Constructor for a new fsdir_cursor object.
2721 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2724 pCur = sqlite3_malloc( sizeof(*pCur) );
2725 if( pCur==0 ) return SQLITE_NOMEM;
2726 memset(pCur, 0, sizeof(*pCur));
2728 *ppCursor = &pCur->base;
2733 ** Reset a cursor back to the state it was in when first returned
2736 static void fsdirResetCursor(fsdir_cursor *pCur){
2738 for(i=0; i<=pCur->iLvl; i++){
2739 FsdirLevel *pLvl = &pCur->aLvl[i];
2740 if( pLvl->pDir ) closedir(pLvl->pDir);
2741 sqlite3_free(pLvl->zDir);
2743 sqlite3_free(pCur->zPath);
2744 sqlite3_free(pCur->aLvl);
2755 ** Destructor for an fsdir_cursor.
2757 static int fsdirClose(sqlite3_vtab_cursor *cur){
2758 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2760 fsdirResetCursor(pCur);
2766 ** Set the error message for the virtual table associated with cursor
2767 ** pCur to the results of vprintf(zFmt, ...).
2769 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2772 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2778 ** Advance an fsdir_cursor to its next row of output.
2780 static int fsdirNext(sqlite3_vtab_cursor *cur){
2781 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2782 mode_t m = pCur->sStat.st_mode;
2786 /* Descend into this directory */
2787 int iNew = pCur->iLvl + 1;
2789 if( iNew>=pCur->nLvl ){
2791 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2792 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2793 if( aNew==0 ) return SQLITE_NOMEM;
2794 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2799 pLvl = &pCur->aLvl[iNew];
2801 pLvl->zDir = pCur->zPath;
2803 pLvl->pDir = opendir(pLvl->zDir);
2804 if( pLvl->pDir==0 ){
2805 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2806 return SQLITE_ERROR;
2810 while( pCur->iLvl>=0 ){
2811 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2812 struct dirent *pEntry = readdir(pLvl->pDir);
2814 if( pEntry->d_name[0]=='.' ){
2815 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2816 if( pEntry->d_name[1]=='\0' ) continue;
2818 sqlite3_free(pCur->zPath);
2819 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2820 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2821 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2822 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2823 return SQLITE_ERROR;
2827 closedir(pLvl->pDir);
2828 sqlite3_free(pLvl->zDir);
2835 sqlite3_free(pCur->zPath);
2841 ** Return values of columns for the row at which the series_cursor
2842 ** is currently pointing.
2844 static int fsdirColumn(
2845 sqlite3_vtab_cursor *cur, /* The cursor */
2846 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2847 int i /* Which column to return */
2849 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2851 case FSDIR_COLUMN_NAME: {
2852 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2856 case FSDIR_COLUMN_MODE:
2857 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2860 case FSDIR_COLUMN_MTIME:
2861 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2864 case FSDIR_COLUMN_DATA: {
2865 mode_t m = pCur->sStat.st_mode;
2867 sqlite3_result_null(ctx);
2868 #if !defined(_WIN32) && !defined(WIN32)
2869 }else if( S_ISLNK(m) ){
2871 char *aBuf = aStatic;
2872 sqlite3_int64 nBuf = 64;
2876 n = readlink(pCur->zPath, aBuf, nBuf);
2878 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2880 aBuf = sqlite3_malloc64(nBuf);
2882 sqlite3_result_error_nomem(ctx);
2883 return SQLITE_NOMEM;
2887 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2888 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2891 readFileContents(ctx, pCur->zPath);
2894 case FSDIR_COLUMN_PATH:
2896 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2897 ** always return their values as NULL */
2905 ** Return the rowid for the current row. In this implementation, the
2906 ** first row returned is assigned rowid value 1, and each subsequent
2907 ** row a value 1 more than that of the previous.
2909 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2910 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2911 *pRowid = pCur->iRowid;
2916 ** Return TRUE if the cursor has been moved off of the last
2919 static int fsdirEof(sqlite3_vtab_cursor *cur){
2920 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2921 return (pCur->zPath==0);
2925 ** xFilter callback.
2927 ** idxNum==1 PATH parameter only
2928 ** idxNum==2 Both PATH and DIR supplied
2930 static int fsdirFilter(
2931 sqlite3_vtab_cursor *cur,
2932 int idxNum, const char *idxStr,
2933 int argc, sqlite3_value **argv
2935 const char *zDir = 0;
2936 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2938 fsdirResetCursor(pCur);
2941 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2942 return SQLITE_ERROR;
2945 assert( argc==idxNum && (argc==1 || argc==2) );
2946 zDir = (const char*)sqlite3_value_text(argv[0]);
2948 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2949 return SQLITE_ERROR;
2952 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2955 pCur->nBase = (int)strlen(pCur->zBase)+1;
2956 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2958 pCur->zPath = sqlite3_mprintf("%s", zDir);
2961 if( pCur->zPath==0 ){
2962 return SQLITE_NOMEM;
2964 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2965 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2966 return SQLITE_ERROR;
2973 ** SQLite will invoke this method one or more times while planning a query
2974 ** that uses the generate_series virtual table. This routine needs to create
2975 ** a query plan for each invocation and compute an estimated cost for that
2978 ** In this implementation idxNum is used to represent the
2979 ** query plan. idxStr is unused.
2981 ** The query plan is represented by values of idxNum:
2983 ** (1) The path value is supplied by argv[0]
2984 ** (2) Path is in argv[0] and dir is in argv[1]
2986 static int fsdirBestIndex(
2988 sqlite3_index_info *pIdxInfo
2990 int i; /* Loop over constraints */
2991 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2992 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2993 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2994 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
2995 const struct sqlite3_index_constraint *pConstraint;
2998 pConstraint = pIdxInfo->aConstraint;
2999 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3000 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3001 switch( pConstraint->iColumn ){
3002 case FSDIR_COLUMN_PATH: {
3003 if( pConstraint->usable ){
3006 }else if( idxPath<0 ){
3011 case FSDIR_COLUMN_DIR: {
3012 if( pConstraint->usable ){
3015 }else if( idxDir<0 ){
3022 if( seenPath || seenDir ){
3023 /* If input parameters are unusable, disallow this plan */
3024 return SQLITE_CONSTRAINT;
3028 pIdxInfo->idxNum = 0;
3029 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3030 ** number. Leave it unchanged. */
3031 pIdxInfo->estimatedRows = 0x7fffffff;
3033 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3034 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3036 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3037 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3038 pIdxInfo->idxNum = 2;
3039 pIdxInfo->estimatedCost = 10.0;
3041 pIdxInfo->idxNum = 1;
3042 pIdxInfo->estimatedCost = 100.0;
3050 ** Register the "fsdir" virtual table.
3052 static int fsdirRegister(sqlite3 *db){
3053 static sqlite3_module fsdirModule = {
3056 fsdirConnect, /* xConnect */
3057 fsdirBestIndex, /* xBestIndex */
3058 fsdirDisconnect, /* xDisconnect */
3060 fsdirOpen, /* xOpen - open a cursor */
3061 fsdirClose, /* xClose - close a cursor */
3062 fsdirFilter, /* xFilter - configure scan constraints */
3063 fsdirNext, /* xNext - advance a cursor */
3064 fsdirEof, /* xEof - check for end of scan */
3065 fsdirColumn, /* xColumn - read data */
3066 fsdirRowid, /* xRowid - read data */
3072 0, /* xFindMethod */
3076 0, /* xRollbackTo */
3077 0, /* xShadowName */
3080 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3083 #else /* SQLITE_OMIT_VIRTUALTABLE */
3084 # define fsdirRegister(x) SQLITE_OK
3090 int sqlite3_fileio_init(
3093 const sqlite3_api_routines *pApi
3096 SQLITE_EXTENSION_INIT2(pApi);
3097 (void)pzErrMsg; /* Unused parameter */
3098 rc = sqlite3_create_function(db, "readfile", 1,
3099 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3100 readfileFunc, 0, 0);
3101 if( rc==SQLITE_OK ){
3102 rc = sqlite3_create_function(db, "writefile", -1,
3103 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3104 writefileFunc, 0, 0);
3106 if( rc==SQLITE_OK ){
3107 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3110 if( rc==SQLITE_OK ){
3111 rc = fsdirRegister(db);
3116 /************************* End ../ext/misc/fileio.c ********************/
3117 /************************* Begin ../ext/misc/completion.c ******************/
3121 ** The author disclaims copyright to this source code. In place of
3122 ** a legal notice, here is a blessing:
3124 ** May you do good and not evil.
3125 ** May you find forgiveness for yourself and forgive others.
3126 ** May you share freely, never taking more than you give.
3128 *************************************************************************
3130 ** This file implements an eponymous virtual table that returns suggested
3131 ** completions for a partial SQL input.
3135 ** SELECT DISTINCT candidate COLLATE nocase
3136 ** FROM completion($prefix,$wholeline)
3139 ** The two query parameters are optional. $prefix is the text of the
3140 ** current word being typed and that is to be completed. $wholeline is
3141 ** the complete input line, used for context.
3143 ** The raw completion() table might return the same candidate multiple
3144 ** times, for example if the same column name is used to two or more
3145 ** tables. And the candidates are returned in an arbitrary order. Hence,
3146 ** the DISTINCT and ORDER BY are recommended.
3148 ** This virtual table operates at the speed of human typing, and so there
3149 ** is no attempt to make it fast. Even a slow implementation will be much
3150 ** faster than any human can type.
3153 /* #include "sqlite3ext.h" */
3154 SQLITE_EXTENSION_INIT1
3159 #ifndef SQLITE_OMIT_VIRTUALTABLE
3161 /* completion_vtab is a subclass of sqlite3_vtab which will
3162 ** serve as the underlying representation of a completion virtual table
3164 typedef struct completion_vtab completion_vtab;
3165 struct completion_vtab {
3166 sqlite3_vtab base; /* Base class - must be first */
3167 sqlite3 *db; /* Database connection for this completion vtab */
3170 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3171 ** serve as the underlying representation of a cursor that scans
3172 ** over rows of the result
3174 typedef struct completion_cursor completion_cursor;
3175 struct completion_cursor {
3176 sqlite3_vtab_cursor base; /* Base class - must be first */
3177 sqlite3 *db; /* Database connection for this cursor */
3178 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3179 char *zPrefix; /* The prefix for the word we want to complete */
3180 char *zLine; /* The whole that we want to complete */
3181 const char *zCurrentRow; /* Current output row */
3182 int szRow; /* Length of the zCurrentRow string */
3183 sqlite3_stmt *pStmt; /* Current statement */
3184 sqlite3_int64 iRowid; /* The rowid */
3185 int ePhase; /* Current phase */
3186 int j; /* inter-phase counter */
3189 /* Values for ePhase:
3191 #define COMPLETION_FIRST_PHASE 1
3192 #define COMPLETION_KEYWORDS 1
3193 #define COMPLETION_PRAGMAS 2
3194 #define COMPLETION_FUNCTIONS 3
3195 #define COMPLETION_COLLATIONS 4
3196 #define COMPLETION_INDEXES 5
3197 #define COMPLETION_TRIGGERS 6
3198 #define COMPLETION_DATABASES 7
3199 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3200 #define COMPLETION_COLUMNS 9
3201 #define COMPLETION_MODULES 10
3202 #define COMPLETION_EOF 11
3205 ** The completionConnect() method is invoked to create a new
3206 ** completion_vtab that describes the completion virtual table.
3208 ** Think of this routine as the constructor for completion_vtab objects.
3210 ** All this routine needs to do is:
3212 ** (1) Allocate the completion_vtab object and initialize all fields.
3214 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3215 ** result set of queries against completion will look like.
3217 static int completionConnect(
3220 int argc, const char *const*argv,
3221 sqlite3_vtab **ppVtab,
3224 completion_vtab *pNew;
3227 (void)(pAux); /* Unused parameter */
3228 (void)(argc); /* Unused parameter */
3229 (void)(argv); /* Unused parameter */
3230 (void)(pzErr); /* Unused parameter */
3232 /* Column numbers */
3233 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3234 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3235 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3236 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3238 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3239 rc = sqlite3_declare_vtab(db,
3242 " prefix TEXT HIDDEN,"
3243 " wholeline TEXT HIDDEN,"
3244 " phase INT HIDDEN" /* Used for debugging only */
3246 if( rc==SQLITE_OK ){
3247 pNew = sqlite3_malloc( sizeof(*pNew) );
3248 *ppVtab = (sqlite3_vtab*)pNew;
3249 if( pNew==0 ) return SQLITE_NOMEM;
3250 memset(pNew, 0, sizeof(*pNew));
3257 ** This method is the destructor for completion_cursor objects.
3259 static int completionDisconnect(sqlite3_vtab *pVtab){
3260 sqlite3_free(pVtab);
3265 ** Constructor for a new completion_cursor object.
3267 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3268 completion_cursor *pCur;
3269 pCur = sqlite3_malloc( sizeof(*pCur) );
3270 if( pCur==0 ) return SQLITE_NOMEM;
3271 memset(pCur, 0, sizeof(*pCur));
3272 pCur->db = ((completion_vtab*)p)->db;
3273 *ppCursor = &pCur->base;
3278 ** Reset the completion_cursor.
3280 static void completionCursorReset(completion_cursor *pCur){
3281 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3282 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3283 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3288 ** Destructor for a completion_cursor.
3290 static int completionClose(sqlite3_vtab_cursor *cur){
3291 completionCursorReset((completion_cursor*)cur);
3297 ** Advance a completion_cursor to its next row of output.
3299 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3300 ** record the current state of the scan. This routine sets ->zCurrentRow
3301 ** to the current row of output and then returns. If no more rows remain,
3302 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3303 ** table that has reached the end of its scan.
3305 ** The current implementation just lists potential identifiers and
3306 ** keywords and filters them by zPrefix. Future enhancements should
3307 ** take zLine into account to try to restrict the set of identifiers and
3308 ** keywords based on what would be legal at the current point of input.
3310 static int completionNext(sqlite3_vtab_cursor *cur){
3311 completion_cursor *pCur = (completion_cursor*)cur;
3312 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3313 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3315 while( pCur->ePhase!=COMPLETION_EOF ){
3316 switch( pCur->ePhase ){
3317 case COMPLETION_KEYWORDS: {
3318 if( pCur->j >= sqlite3_keyword_count() ){
3319 pCur->zCurrentRow = 0;
3320 pCur->ePhase = COMPLETION_DATABASES;
3322 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3327 case COMPLETION_DATABASES: {
3328 if( pCur->pStmt==0 ){
3329 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3333 eNextPhase = COMPLETION_TABLES;
3336 case COMPLETION_TABLES: {
3337 if( pCur->pStmt==0 ){
3340 const char *zSep = "";
3341 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3342 while( sqlite3_step(pS2)==SQLITE_ROW ){
3343 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3344 zSql = sqlite3_mprintf(
3346 "SELECT name FROM \"%w\".sqlite_schema",
3349 if( zSql==0 ) return SQLITE_NOMEM;
3352 sqlite3_finalize(pS2);
3353 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3357 eNextPhase = COMPLETION_COLUMNS;
3360 case COMPLETION_COLUMNS: {
3361 if( pCur->pStmt==0 ){
3364 const char *zSep = "";
3365 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3366 while( sqlite3_step(pS2)==SQLITE_ROW ){
3367 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3368 zSql = sqlite3_mprintf(
3370 "SELECT pti.name FROM \"%w\".sqlite_schema AS sm"
3371 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3372 " WHERE sm.type='table'",
3373 zSql, zSep, zDb, zDb
3375 if( zSql==0 ) return SQLITE_NOMEM;
3378 sqlite3_finalize(pS2);
3379 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3383 eNextPhase = COMPLETION_EOF;
3388 /* This case is when the phase presets zCurrentRow */
3389 if( pCur->zCurrentRow==0 ) continue;
3391 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3392 /* Extract the next row of content */
3393 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3394 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3396 /* When all rows are finished, advance to the next phase */
3397 sqlite3_finalize(pCur->pStmt);
3399 pCur->ePhase = eNextPhase;
3403 if( pCur->nPrefix==0 ) break;
3404 if( pCur->nPrefix<=pCur->szRow
3405 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3415 ** Return values of columns for the row at which the completion_cursor
3416 ** is currently pointing.
3418 static int completionColumn(
3419 sqlite3_vtab_cursor *cur, /* The cursor */
3420 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3421 int i /* Which column to return */
3423 completion_cursor *pCur = (completion_cursor*)cur;
3425 case COMPLETION_COLUMN_CANDIDATE: {
3426 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3429 case COMPLETION_COLUMN_PREFIX: {
3430 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3433 case COMPLETION_COLUMN_WHOLELINE: {
3434 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3437 case COMPLETION_COLUMN_PHASE: {
3438 sqlite3_result_int(ctx, pCur->ePhase);
3446 ** Return the rowid for the current row. In this implementation, the
3447 ** rowid is the same as the output value.
3449 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3450 completion_cursor *pCur = (completion_cursor*)cur;
3451 *pRowid = pCur->iRowid;
3456 ** Return TRUE if the cursor has been moved off of the last
3459 static int completionEof(sqlite3_vtab_cursor *cur){
3460 completion_cursor *pCur = (completion_cursor*)cur;
3461 return pCur->ePhase >= COMPLETION_EOF;
3465 ** This method is called to "rewind" the completion_cursor object back
3466 ** to the first row of output. This method is always called at least
3467 ** once prior to any call to completionColumn() or completionRowid() or
3470 static int completionFilter(
3471 sqlite3_vtab_cursor *pVtabCursor,
3472 int idxNum, const char *idxStr,
3473 int argc, sqlite3_value **argv
3475 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3477 (void)(idxStr); /* Unused parameter */
3478 (void)(argc); /* Unused parameter */
3479 completionCursorReset(pCur);
3481 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3482 if( pCur->nPrefix>0 ){
3483 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3484 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3489 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3490 if( pCur->nLine>0 ){
3491 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3492 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3495 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3496 int i = pCur->nLine;
3497 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3500 pCur->nPrefix = pCur->nLine - i;
3501 if( pCur->nPrefix>0 ){
3502 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3503 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3507 pCur->ePhase = COMPLETION_FIRST_PHASE;
3508 return completionNext(pVtabCursor);
3512 ** SQLite will invoke this method one or more times while planning a query
3513 ** that uses the completion virtual table. This routine needs to create
3514 ** a query plan for each invocation and compute an estimated cost for that
3517 ** There are two hidden parameters that act as arguments to the table-valued
3518 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3519 ** is available and bit 1 is set if "wholeline" is available.
3521 static int completionBestIndex(
3523 sqlite3_index_info *pIdxInfo
3525 int i; /* Loop over constraints */
3526 int idxNum = 0; /* The query plan bitmask */
3527 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3528 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3529 int nArg = 0; /* Number of arguments that completeFilter() expects */
3530 const struct sqlite3_index_constraint *pConstraint;
3532 (void)(tab); /* Unused parameter */
3533 pConstraint = pIdxInfo->aConstraint;
3534 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3535 if( pConstraint->usable==0 ) continue;
3536 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3537 switch( pConstraint->iColumn ){
3538 case COMPLETION_COLUMN_PREFIX:
3542 case COMPLETION_COLUMN_WHOLELINE:
3549 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3550 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3552 if( wholelineIdx>=0 ){
3553 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3554 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3556 pIdxInfo->idxNum = idxNum;
3557 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3558 pIdxInfo->estimatedRows = 500 - 100*nArg;
3563 ** This following structure defines all the methods for the
3564 ** completion virtual table.
3566 static sqlite3_module completionModule = {
3569 completionConnect, /* xConnect */
3570 completionBestIndex, /* xBestIndex */
3571 completionDisconnect, /* xDisconnect */
3573 completionOpen, /* xOpen - open a cursor */
3574 completionClose, /* xClose - close a cursor */
3575 completionFilter, /* xFilter - configure scan constraints */
3576 completionNext, /* xNext - advance a cursor */
3577 completionEof, /* xEof - check for end of scan */
3578 completionColumn, /* xColumn - read data */
3579 completionRowid, /* xRowid - read data */
3585 0, /* xFindMethod */
3589 0, /* xRollbackTo */
3593 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3595 int sqlite3CompletionVtabInit(sqlite3 *db){
3597 #ifndef SQLITE_OMIT_VIRTUALTABLE
3598 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3606 int sqlite3_completion_init(
3609 const sqlite3_api_routines *pApi
3612 SQLITE_EXTENSION_INIT2(pApi);
3613 (void)(pzErrMsg); /* Unused parameter */
3614 #ifndef SQLITE_OMIT_VIRTUALTABLE
3615 rc = sqlite3CompletionVtabInit(db);
3620 /************************* End ../ext/misc/completion.c ********************/
3621 /************************* Begin ../ext/misc/appendvfs.c ******************/
3625 ** The author disclaims copyright to this source code. In place of
3626 ** a legal notice, here is a blessing:
3628 ** May you do good and not evil.
3629 ** May you find forgiveness for yourself and forgive others.
3630 ** May you share freely, never taking more than you give.
3632 ******************************************************************************
3634 ** This file implements a VFS shim that allows an SQLite database to be
3635 ** appended onto the end of some other file, such as an executable.
3637 ** A special record must appear at the end of the file that identifies the
3638 ** file as an appended database and provides an offset to page 1. For
3639 ** best performance page 1 should be located at a disk page boundary, though
3640 ** that is not required.
3642 ** When opening a database using this VFS, the connection might treat
3643 ** the file as an ordinary SQLite database, or it might treat is as a
3644 ** database appended onto some other file. Here are the rules:
3646 ** (1) When opening a new empty file, that file is treated as an ordinary
3649 ** (2) When opening a file that begins with the standard SQLite prefix
3650 ** string "SQLite format 3", that file is treated as an ordinary
3653 ** (3) When opening a file that ends with the appendvfs trailer string
3654 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3657 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3658 ** set, then a new database is appended to the already existing file.
3660 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3662 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3663 ** the file containing the database is limited to 1GB. This VFS will refuse
3664 ** to read or write past the 1GB mark. This restriction might be lifted in
3665 ** future versions. For now, if you need a large database, then keep the
3666 ** database in a separate file.
3668 ** If the file being opened is not an appended database, then this shim is
3669 ** a pass-through into the default underlying VFS.
3671 /* #include "sqlite3ext.h" */
3672 SQLITE_EXTENSION_INIT1
3676 /* The append mark at the end of the database is:
3678 ** Start-Of-SQLite3-NNNNNNNN
3679 ** 123456789 123456789 12345
3681 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3682 ** the offset to page 1.
3684 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3685 #define APND_MARK_PREFIX_SZ 17
3686 #define APND_MARK_SIZE 25
3689 ** Maximum size of the combined prefix + database + append-mark. This
3690 ** must be less than 0x40000000 to avoid locking issues on Windows.
3692 #define APND_MAX_SIZE (65536*15259)
3695 ** Forward declaration of objects used by this utility
3697 typedef struct sqlite3_vfs ApndVfs;
3698 typedef struct ApndFile ApndFile;
3700 /* Access to a lower-level VFS that (might) implement dynamic loading,
3701 ** access to randomness, etc.
3703 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3704 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3708 sqlite3_file base; /* IO methods */
3709 sqlite3_int64 iPgOne; /* File offset to page 1 */
3710 sqlite3_int64 iMark; /* Start of the append-mark */
3714 ** Methods for ApndFile
3716 static int apndClose(sqlite3_file*);
3717 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3718 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3719 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3720 static int apndSync(sqlite3_file*, int flags);
3721 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3722 static int apndLock(sqlite3_file*, int);
3723 static int apndUnlock(sqlite3_file*, int);
3724 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3725 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3726 static int apndSectorSize(sqlite3_file*);
3727 static int apndDeviceCharacteristics(sqlite3_file*);
3728 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3729 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3730 static void apndShmBarrier(sqlite3_file*);
3731 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3732 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3733 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3736 ** Methods for ApndVfs
3738 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3739 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3740 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3741 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3742 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3743 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3744 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3745 static void apndDlClose(sqlite3_vfs*, void*);
3746 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3747 static int apndSleep(sqlite3_vfs*, int microseconds);
3748 static int apndCurrentTime(sqlite3_vfs*, double*);
3749 static int apndGetLastError(sqlite3_vfs*, int, char *);
3750 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3751 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3752 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3753 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3755 static sqlite3_vfs apnd_vfs = {
3756 3, /* iVersion (set when registered) */
3757 0, /* szOsFile (set when registered) */
3758 1024, /* mxPathname */
3760 "apndvfs", /* zName */
3761 0, /* pAppData (set when registered) */
3762 apndOpen, /* xOpen */
3763 apndDelete, /* xDelete */
3764 apndAccess, /* xAccess */
3765 apndFullPathname, /* xFullPathname */
3766 apndDlOpen, /* xDlOpen */
3767 apndDlError, /* xDlError */
3768 apndDlSym, /* xDlSym */
3769 apndDlClose, /* xDlClose */
3770 apndRandomness, /* xRandomness */
3771 apndSleep, /* xSleep */
3772 apndCurrentTime, /* xCurrentTime */
3773 apndGetLastError, /* xGetLastError */
3774 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3775 apndSetSystemCall, /* xSetSystemCall */
3776 apndGetSystemCall, /* xGetSystemCall */
3777 apndNextSystemCall /* xNextSystemCall */
3780 static const sqlite3_io_methods apnd_io_methods = {
3782 apndClose, /* xClose */
3783 apndRead, /* xRead */
3784 apndWrite, /* xWrite */
3785 apndTruncate, /* xTruncate */
3786 apndSync, /* xSync */
3787 apndFileSize, /* xFileSize */
3788 apndLock, /* xLock */
3789 apndUnlock, /* xUnlock */
3790 apndCheckReservedLock, /* xCheckReservedLock */
3791 apndFileControl, /* xFileControl */
3792 apndSectorSize, /* xSectorSize */
3793 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3794 apndShmMap, /* xShmMap */
3795 apndShmLock, /* xShmLock */
3796 apndShmBarrier, /* xShmBarrier */
3797 apndShmUnmap, /* xShmUnmap */
3798 apndFetch, /* xFetch */
3799 apndUnfetch /* xUnfetch */
3805 ** Close an apnd-file.
3807 static int apndClose(sqlite3_file *pFile){
3808 pFile = ORIGFILE(pFile);
3809 return pFile->pMethods->xClose(pFile);
3813 ** Read data from an apnd-file.
3815 static int apndRead(
3816 sqlite3_file *pFile,
3821 ApndFile *p = (ApndFile *)pFile;
3822 pFile = ORIGFILE(pFile);
3823 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3827 ** Add the append-mark onto the end of the file.
3829 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3831 unsigned char a[APND_MARK_SIZE];
3832 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3834 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3836 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3840 ** Write data to an apnd-file.
3842 static int apndWrite(
3843 sqlite3_file *pFile,
3849 ApndFile *p = (ApndFile *)pFile;
3850 pFile = ORIGFILE(pFile);
3851 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3852 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3853 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3854 sqlite3_int64 sz = 0;
3855 rc = pFile->pMethods->xFileSize(pFile, &sz);
3856 if( rc==SQLITE_OK ){
3857 p->iMark = sz - APND_MARK_SIZE;
3858 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3859 p->iMark = p->iPgOne + iOfst + iAmt;
3860 rc = apndWriteMark(p, pFile);
3868 ** Truncate an apnd-file.
3870 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3872 ApndFile *p = (ApndFile *)pFile;
3873 pFile = ORIGFILE(pFile);
3874 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3875 if( rc==SQLITE_OK ){
3876 p->iMark = p->iPgOne+size;
3877 rc = apndWriteMark(p, pFile);
3883 ** Sync an apnd-file.
3885 static int apndSync(sqlite3_file *pFile, int flags){
3886 pFile = ORIGFILE(pFile);
3887 return pFile->pMethods->xSync(pFile, flags);
3891 ** Return the current file-size of an apnd-file.
3893 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3894 ApndFile *p = (ApndFile *)pFile;
3896 pFile = ORIGFILE(p);
3897 rc = pFile->pMethods->xFileSize(pFile, pSize);
3898 if( rc==SQLITE_OK && p->iPgOne ){
3899 *pSize -= p->iPgOne + APND_MARK_SIZE;
3905 ** Lock an apnd-file.
3907 static int apndLock(sqlite3_file *pFile, int eLock){
3908 pFile = ORIGFILE(pFile);
3909 return pFile->pMethods->xLock(pFile, eLock);
3913 ** Unlock an apnd-file.
3915 static int apndUnlock(sqlite3_file *pFile, int eLock){
3916 pFile = ORIGFILE(pFile);
3917 return pFile->pMethods->xUnlock(pFile, eLock);
3921 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3923 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3924 pFile = ORIGFILE(pFile);
3925 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3929 ** File control method. For custom operations on an apnd-file.
3931 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3932 ApndFile *p = (ApndFile *)pFile;
3934 pFile = ORIGFILE(pFile);
3935 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3936 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3937 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3943 ** Return the sector-size in bytes for an apnd-file.
3945 static int apndSectorSize(sqlite3_file *pFile){
3946 pFile = ORIGFILE(pFile);
3947 return pFile->pMethods->xSectorSize(pFile);
3951 ** Return the device characteristic flags supported by an apnd-file.
3953 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3954 pFile = ORIGFILE(pFile);
3955 return pFile->pMethods->xDeviceCharacteristics(pFile);
3958 /* Create a shared memory file mapping */
3959 static int apndShmMap(
3960 sqlite3_file *pFile,
3966 pFile = ORIGFILE(pFile);
3967 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3970 /* Perform locking on a shared-memory segment */
3971 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3972 pFile = ORIGFILE(pFile);
3973 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3976 /* Memory barrier operation on shared memory */
3977 static void apndShmBarrier(sqlite3_file *pFile){
3978 pFile = ORIGFILE(pFile);
3979 pFile->pMethods->xShmBarrier(pFile);
3982 /* Unmap a shared memory segment */
3983 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3984 pFile = ORIGFILE(pFile);
3985 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3988 /* Fetch a page of a memory-mapped file */
3989 static int apndFetch(
3990 sqlite3_file *pFile,
3991 sqlite3_int64 iOfst,
3995 ApndFile *p = (ApndFile *)pFile;
3996 pFile = ORIGFILE(pFile);
3997 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
4000 /* Release a memory-mapped page */
4001 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
4002 ApndFile *p = (ApndFile *)pFile;
4003 pFile = ORIGFILE(pFile);
4004 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
4008 ** Check to see if the file is an ordinary SQLite database file.
4010 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4013 static const char aSqliteHdr[] = "SQLite format 3";
4014 if( sz<512 ) return 0;
4015 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
4017 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
4021 ** Try to read the append-mark off the end of a file. Return the
4022 ** start of the appended database if the append-mark is present. If
4023 ** there is no append-mark, return -1;
4025 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4027 sqlite3_int64 iMark;
4028 unsigned char a[APND_MARK_SIZE];
4030 if( sz<=APND_MARK_SIZE ) return -1;
4031 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4033 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4034 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
4036 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
4042 ** Open an apnd file handle.
4044 static int apndOpen(
4047 sqlite3_file *pFile,
4052 sqlite3_file *pSubFile;
4053 sqlite3_vfs *pSubVfs;
4056 pSubVfs = ORIGVFS(pVfs);
4057 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4058 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4060 p = (ApndFile*)pFile;
4061 memset(p, 0, sizeof(*p));
4062 pSubFile = ORIGFILE(pFile);
4063 pFile->pMethods = &apnd_io_methods;
4064 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4065 if( rc ) goto apnd_open_done;
4066 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4068 pSubFile->pMethods->xClose(pSubFile);
4069 goto apnd_open_done;
4071 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4072 memmove(pFile, pSubFile, pSubVfs->szOsFile);
4076 p->iPgOne = apndReadMark(sz, pFile);
4080 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4081 pSubFile->pMethods->xClose(pSubFile);
4082 rc = SQLITE_CANTOPEN;
4084 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4086 if( rc ) pFile->pMethods = 0;
4091 ** All other VFS methods are pass-thrus.
4093 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4094 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4096 static int apndAccess(
4102 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4104 static int apndFullPathname(
4110 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4112 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4113 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4115 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4116 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4118 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4119 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4121 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4122 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4124 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4125 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4127 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4128 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4130 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4131 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4133 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4134 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4136 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4137 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4139 static int apndSetSystemCall(
4142 sqlite3_syscall_ptr pCall
4144 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4146 static sqlite3_syscall_ptr apndGetSystemCall(
4150 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4152 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4153 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4161 ** This routine is called when the extension is loaded.
4162 ** Register the new VFS.
4164 int sqlite3_appendvfs_init(
4167 const sqlite3_api_routines *pApi
4171 SQLITE_EXTENSION_INIT2(pApi);
4174 pOrig = sqlite3_vfs_find(0);
4175 apnd_vfs.iVersion = pOrig->iVersion;
4176 apnd_vfs.pAppData = pOrig;
4177 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4178 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4179 #ifdef APPENDVFS_TEST
4180 if( rc==SQLITE_OK ){
4181 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4184 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4188 /************************* End ../ext/misc/appendvfs.c ********************/
4189 /************************* Begin ../ext/misc/memtrace.c ******************/
4193 ** The author disclaims copyright to this source code. In place of
4194 ** a legal notice, here is a blessing:
4196 ** May you do good and not evil.
4197 ** May you find forgiveness for yourself and forgive others.
4198 ** May you share freely, never taking more than you give.
4200 *************************************************************************
4202 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4203 ** mechanism to add a tracing layer on top of SQLite. If this extension
4204 ** is registered prior to sqlite3_initialize(), it will cause all memory
4205 ** allocation activities to be logged on standard output, or to some other
4206 ** FILE specified by the initializer.
4208 ** This file needs to be compiled into the application that uses it.
4210 ** This extension is used to implement the --memtrace option of the
4211 ** command-line shell.
4217 /* The original memory allocation routines */
4218 static sqlite3_mem_methods memtraceBase;
4219 static FILE *memtraceOut;
4221 /* Methods that trace memory allocations */
4222 static void *memtraceMalloc(int n){
4224 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4225 memtraceBase.xRoundup(n));
4227 return memtraceBase.xMalloc(n);
4229 static void memtraceFree(void *p){
4232 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4234 memtraceBase.xFree(p);
4236 static void *memtraceRealloc(void *p, int n){
4237 if( p==0 ) return memtraceMalloc(n);
4243 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4244 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4246 return memtraceBase.xRealloc(p, n);
4248 static int memtraceSize(void *p){
4249 return memtraceBase.xSize(p);
4251 static int memtraceRoundup(int n){
4252 return memtraceBase.xRoundup(n);
4254 static int memtraceInit(void *p){
4255 return memtraceBase.xInit(p);
4257 static void memtraceShutdown(void *p){
4258 memtraceBase.xShutdown(p);
4261 /* The substitute memory allocator */
4262 static sqlite3_mem_methods ersaztMethods = {
4273 /* Begin tracing memory allocations to out. */
4274 int sqlite3MemTraceActivate(FILE *out){
4276 if( memtraceBase.xMalloc==0 ){
4277 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4278 if( rc==SQLITE_OK ){
4279 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4286 /* Deactivate memory tracing */
4287 int sqlite3MemTraceDeactivate(void){
4289 if( memtraceBase.xMalloc!=0 ){
4290 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4291 if( rc==SQLITE_OK ){
4292 memset(&memtraceBase, 0, sizeof(memtraceBase));
4299 /************************* End ../ext/misc/memtrace.c ********************/
4300 /************************* Begin ../ext/misc/uint.c ******************/
4304 ** The author disclaims copyright to this source code. In place of
4305 ** a legal notice, here is a blessing:
4307 ** May you do good and not evil.
4308 ** May you find forgiveness for yourself and forgive others.
4309 ** May you share freely, never taking more than you give.
4311 ******************************************************************************
4313 ** This SQLite extension implements the UINT collating sequence.
4315 ** UINT works like BINARY for text, except that embedded strings
4316 ** of digits compare in numeric order.
4318 ** * Leading zeros are handled properly, in the sense that
4319 ** they do not mess of the maginitude comparison of embedded
4320 ** strings of digits. "x00123y" is equal to "x123y".
4322 ** * Only unsigned integers are recognized. Plus and minus
4323 ** signs are ignored. Decimal points and exponential notation
4326 ** * Embedded integers can be of arbitrary length. Comparison
4327 ** is *not* limited integers that can be expressed as a
4328 ** 64-bit machine integer.
4330 /* #include "sqlite3ext.h" */
4331 SQLITE_EXTENSION_INIT1
4337 ** Compare text in lexicographic order, except strings of digits
4338 ** compare in numeric order.
4340 static int uintCollFunc(
4342 int nKey1, const void *pKey1,
4343 int nKey2, const void *pKey2
4345 const unsigned char *zA = (const unsigned char*)pKey1;
4346 const unsigned char *zB = (const unsigned char*)pKey2;
4349 while( i<nKey1 && j<nKey2 ){
4351 if( isdigit(zA[i]) ){
4353 if( !isdigit(zB[j]) ) return x;
4354 while( i<nKey1 && zA[i]=='0' ){ i++; }
4355 while( j<nKey2 && zB[j]=='0' ){ j++; }
4357 while( i+k<nKey1 && isdigit(zA[i+k])
4358 && j+k<nKey2 && isdigit(zB[j+k]) ){
4361 if( i+k<nKey1 && isdigit(zA[i+k]) ){
4363 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4366 x = memcmp(zA+i, zB+j, k);
4378 return (nKey1 - i) - (nKey2 - j);
4384 int sqlite3_uint_init(
4387 const sqlite3_api_routines *pApi
4389 SQLITE_EXTENSION_INIT2(pApi);
4390 (void)pzErrMsg; /* Unused parameter */
4391 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4394 /************************* End ../ext/misc/uint.c ********************/
4395 /************************* Begin ../ext/misc/decimal.c ******************/
4399 ** The author disclaims copyright to this source code. In place of
4400 ** a legal notice, here is a blessing:
4402 ** May you do good and not evil.
4403 ** May you find forgiveness for yourself and forgive others.
4404 ** May you share freely, never taking more than you give.
4406 ******************************************************************************
4408 ** Routines to implement arbitrary-precision decimal math.
4410 ** The focus here is on simplicity and correctness, not performance.
4412 /* #include "sqlite3ext.h" */
4413 SQLITE_EXTENSION_INIT1
4419 /* Mark a function parameter as unused, to suppress nuisance compiler
4421 #ifndef UNUSED_PARAMETER
4422 # define UNUSED_PARAMETER(X) (void)(X)
4426 /* A decimal object */
4427 typedef struct Decimal Decimal;
4429 char sign; /* 0 for positive, 1 for negative */
4430 char oom; /* True if an OOM is encountered */
4431 char isNull; /* True if holds a NULL rather than a number */
4432 char isInit; /* True upon initialization */
4433 int nDigit; /* Total number of digits */
4434 int nFrac; /* Number of digits to the right of the decimal point */
4435 signed char *a; /* Array of digits. Most significant first. */
4439 ** Release memory held by a Decimal, but do not free the object itself.
4441 static void decimal_clear(Decimal *p){
4446 ** Destroy a Decimal object
4448 static void decimal_free(Decimal *p){
4456 ** Allocate a new Decimal object. Initialize it to the number given
4457 ** by the input string.
4459 static Decimal *decimal_new(
4460 sqlite3_context *pCtx,
4463 const unsigned char *zAlt
4467 const unsigned char *zIn;
4469 p = sqlite3_malloc( sizeof(*p) );
4470 if( p==0 ) goto new_no_mem;
4481 if( sqlite3_value_type(pIn)==SQLITE_NULL ){
4486 n = sqlite3_value_bytes(pIn);
4487 zIn = sqlite3_value_text(pIn);
4489 p->a = sqlite3_malloc64( n+1 );
4490 if( p->a==0 ) goto new_no_mem;
4491 for(i=0; isspace(zIn[i]); i++){}
4495 }else if( zIn[i]=='+' ){
4498 while( i<n && zIn[i]=='0' ) i++;
4501 if( c>='0' && c<='9' ){
4502 p->a[p->nDigit++] = c - '0';
4504 p->nFrac = p->nDigit + 1;
4505 }else if( c=='e' || c=='E' ){
4512 }else if( zIn[j]=='+' ){
4515 while( j<n && iExp<1000000 ){
4516 if( zIn[j]>='0' && zIn[j]<='9' ){
4517 iExp = iExp*10 + zIn[j] - '0';
4521 if( neg ) iExp = -iExp;
4527 p->nFrac = p->nDigit - (p->nFrac - 1);
4531 if( iExp<=p->nFrac ){
4540 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4541 if( p->a==0 ) goto new_no_mem;
4542 memset(p->a+p->nDigit, 0, iExp);
4548 nExtra = p->nDigit - p->nFrac - 1;
4555 p->nFrac = p->nDigit - 1;
4559 p->a = sqlite3_realloc64(p->a, p->nDigit + iExp + 1 );
4560 if( p->a==0 ) goto new_no_mem;
4561 memmove(p->a+iExp, p->a, p->nDigit);
4562 memset(p->a, 0, iExp);
4570 if( pCtx ) sqlite3_result_error_nomem(pCtx);
4576 ** Make the given Decimal the result.
4578 static void decimal_result(sqlite3_context *pCtx, Decimal *p){
4582 if( p==0 || p->oom ){
4583 sqlite3_result_error_nomem(pCtx);
4587 sqlite3_result_null(pCtx);
4590 z = sqlite3_malloc( p->nDigit+4 );
4592 sqlite3_result_error_nomem(pCtx);
4596 if( p->nDigit==0 || (p->nDigit==1 && p->a[0]==0) ){
4603 n = p->nDigit - p->nFrac;
4608 while( n>1 && p->a[j]==0 ){
4613 z[i++] = p->a[j] + '0';
4620 z[i++] = p->a[j] + '0';
4622 }while( j<p->nDigit );
4625 sqlite3_result_text(pCtx, z, i, sqlite3_free);
4629 ** SQL Function: decimal(X)
4631 ** Convert input X into decimal and then back into text
4633 static void decimalFunc(
4634 sqlite3_context *context,
4636 sqlite3_value **argv
4638 Decimal *p = decimal_new(context, argv[0], 0, 0);
4639 UNUSED_PARAMETER(argc);
4640 decimal_result(context, p);
4645 ** Compare to Decimal objects. Return negative, 0, or positive if the
4646 ** first object is less than, equal to, or greater than the second.
4648 ** Preconditions for this routine:
4655 static int decimal_cmp(const Decimal *pA, const Decimal *pB){
4656 int nASig, nBSig, rc, n;
4657 if( pA->sign!=pB->sign ){
4658 return pA->sign ? -1 : +1;
4661 const Decimal *pTemp = pA;
4665 nASig = pA->nDigit - pA->nFrac;
4666 nBSig = pB->nDigit - pB->nFrac;
4668 return nASig - nBSig;
4671 if( n>pB->nDigit ) n = pB->nDigit;
4672 rc = memcmp(pA->a, pB->a, n);
4674 rc = pA->nDigit - pB->nDigit;
4680 ** SQL Function: decimal_cmp(X, Y)
4682 ** Return negative, zero, or positive if X is less then, equal to, or
4685 static void decimalCmpFunc(
4686 sqlite3_context *context,
4688 sqlite3_value **argv
4690 Decimal *pA = 0, *pB = 0;
4693 UNUSED_PARAMETER(argc);
4694 pA = decimal_new(context, argv[0], 0, 0);
4695 if( pA==0 || pA->isNull ) goto cmp_done;
4696 pB = decimal_new(context, argv[1], 0, 0);
4697 if( pB==0 || pB->isNull ) goto cmp_done;
4698 rc = decimal_cmp(pA, pB);
4700 else if( rc>0 ) rc = +1;
4701 sqlite3_result_int(context, rc);
4708 ** Expand the Decimal so that it has a least nDigit digits and nFrac
4709 ** digits to the right of the decimal point.
4711 static void decimal_expand(Decimal *p, int nDigit, int nFrac){
4715 nAddFrac = nFrac - p->nFrac;
4716 nAddSig = (nDigit - p->nDigit) - nAddFrac;
4717 if( nAddFrac==0 && nAddSig==0 ) return;
4718 p->a = sqlite3_realloc64(p->a, nDigit+1);
4724 memmove(p->a+nAddSig, p->a, p->nDigit);
4725 memset(p->a, 0, nAddSig);
4726 p->nDigit += nAddSig;
4729 memset(p->a+p->nDigit, 0, nAddFrac);
4730 p->nDigit += nAddFrac;
4731 p->nFrac += nAddFrac;
4736 ** Add the value pB into pA.
4738 ** Both pA and pB might become denormalized by this routine.
4740 static void decimal_add(Decimal *pA, Decimal *pB){
4741 int nSig, nFrac, nDigit;
4746 if( pA->oom || pB==0 || pB->oom ){
4750 if( pA->isNull || pB->isNull ){
4754 nSig = pA->nDigit - pA->nFrac;
4755 if( nSig && pA->a[0]==0 ) nSig--;
4756 if( nSig<pB->nDigit-pB->nFrac ){
4757 nSig = pB->nDigit - pB->nFrac;
4760 if( nFrac<pB->nFrac ) nFrac = pB->nFrac;
4761 nDigit = nSig + nFrac + 1;
4762 decimal_expand(pA, nDigit, nFrac);
4763 decimal_expand(pB, nDigit, nFrac);
4764 if( pA->oom || pB->oom ){
4767 if( pA->sign==pB->sign ){
4769 for(i=nDigit-1; i>=0; i--){
4770 int x = pA->a[i] + pB->a[i] + carry;
4780 signed char *aA, *aB;
4782 rc = memcmp(pA->a, pB->a, nDigit);
4786 pA->sign = !pA->sign;
4791 for(i=nDigit-1; i>=0; i--){
4792 int x = aA[i] - aB[i] - borrow;
4806 ** Compare text in decimal order.
4808 static int decimalCollFunc(
4810 int nKey1, const void *pKey1,
4811 int nKey2, const void *pKey2
4813 const unsigned char *zA = (const unsigned char*)pKey1;
4814 const unsigned char *zB = (const unsigned char*)pKey2;
4815 Decimal *pA = decimal_new(0, 0, nKey1, zA);
4816 Decimal *pB = decimal_new(0, 0, nKey2, zB);
4818 UNUSED_PARAMETER(notUsed);
4819 if( pA==0 || pB==0 ){
4822 rc = decimal_cmp(pA, pB);
4831 ** SQL Function: decimal_add(X, Y)
4832 ** decimal_sub(X, Y)
4834 ** Return the sum or difference of X and Y.
4836 static void decimalAddFunc(
4837 sqlite3_context *context,
4839 sqlite3_value **argv
4841 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4842 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4843 UNUSED_PARAMETER(argc);
4844 decimal_add(pA, pB);
4845 decimal_result(context, pA);
4849 static void decimalSubFunc(
4850 sqlite3_context *context,
4852 sqlite3_value **argv
4854 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4855 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4856 UNUSED_PARAMETER(argc);
4858 pB->sign = !pB->sign;
4859 decimal_add(pA, pB);
4860 decimal_result(context, pA);
4865 /* Aggregate funcion: decimal_sum(X)
4867 ** Works like sum() except that it uses decimal arithmetic for unlimited
4870 static void decimalSumStep(
4871 sqlite3_context *context,
4873 sqlite3_value **argv
4877 UNUSED_PARAMETER(argc);
4878 p = sqlite3_aggregate_context(context, sizeof(*p));
4882 p->a = sqlite3_malloc(2);
4891 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4892 pArg = decimal_new(context, argv[0], 0, 0);
4893 decimal_add(p, pArg);
4896 static void decimalSumInverse(
4897 sqlite3_context *context,
4899 sqlite3_value **argv
4903 UNUSED_PARAMETER(argc);
4904 p = sqlite3_aggregate_context(context, sizeof(*p));
4906 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
4907 pArg = decimal_new(context, argv[0], 0, 0);
4908 if( pArg ) pArg->sign = !pArg->sign;
4909 decimal_add(p, pArg);
4912 static void decimalSumValue(sqlite3_context *context){
4913 Decimal *p = sqlite3_aggregate_context(context, 0);
4915 decimal_result(context, p);
4917 static void decimalSumFinalize(sqlite3_context *context){
4918 Decimal *p = sqlite3_aggregate_context(context, 0);
4920 decimal_result(context, p);
4925 ** SQL Function: decimal_mul(X, Y)
4927 ** Return the product of X and Y.
4929 ** All significant digits after the decimal point are retained.
4930 ** Trailing zeros after the decimal point are omitted as long as
4931 ** the number of digits after the decimal point is no less than
4932 ** either the number of digits in either input.
4934 static void decimalMulFunc(
4935 sqlite3_context *context,
4937 sqlite3_value **argv
4939 Decimal *pA = decimal_new(context, argv[0], 0, 0);
4940 Decimal *pB = decimal_new(context, argv[1], 0, 0);
4941 signed char *acc = 0;
4944 UNUSED_PARAMETER(argc);
4945 if( pA==0 || pA->oom || pA->isNull
4946 || pB==0 || pB->oom || pB->isNull
4950 acc = sqlite3_malloc64( pA->nDigit + pB->nDigit + 2 );
4952 sqlite3_result_error_nomem(context);
4955 memset(acc, 0, pA->nDigit + pB->nDigit + 2);
4956 minFrac = pA->nFrac;
4957 if( pB->nFrac<minFrac ) minFrac = pB->nFrac;
4958 for(i=pA->nDigit-1; i>=0; i--){
4959 signed char f = pA->a[i];
4961 for(j=pB->nDigit-1, k=i+j+3; j>=0; j--, k--){
4962 x = acc[k] + f*pB->a[j] + carry;
4970 sqlite3_free(pA->a);
4973 pA->nDigit += pB->nDigit + 2;
4974 pA->nFrac += pB->nFrac;
4975 pA->sign ^= pB->sign;
4976 while( pA->nFrac>minFrac && pA->a[pA->nDigit-1]==0 ){
4980 decimal_result(context, pA);
4991 int sqlite3_decimal_init(
4994 const sqlite3_api_routines *pApi
4997 static const struct {
4998 const char *zFuncName;
5000 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5002 { "decimal", 1, decimalFunc },
5003 { "decimal_cmp", 2, decimalCmpFunc },
5004 { "decimal_add", 2, decimalAddFunc },
5005 { "decimal_sub", 2, decimalSubFunc },
5006 { "decimal_mul", 2, decimalMulFunc },
5009 (void)pzErrMsg; /* Unused parameter */
5011 SQLITE_EXTENSION_INIT2(pApi);
5013 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5014 rc = sqlite3_create_function(db, aFunc[i].zFuncName, aFunc[i].nArg,
5015 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC,
5016 0, aFunc[i].xFunc, 0, 0);
5018 if( rc==SQLITE_OK ){
5019 rc = sqlite3_create_window_function(db, "decimal_sum", 1,
5020 SQLITE_UTF8|SQLITE_INNOCUOUS|SQLITE_DETERMINISTIC, 0,
5021 decimalSumStep, decimalSumFinalize,
5022 decimalSumValue, decimalSumInverse, 0);
5024 if( rc==SQLITE_OK ){
5025 rc = sqlite3_create_collation(db, "decimal", SQLITE_UTF8,
5026 0, decimalCollFunc);
5031 /************************* End ../ext/misc/decimal.c ********************/
5032 /************************* Begin ../ext/misc/ieee754.c ******************/
5036 ** The author disclaims copyright to this source code. In place of
5037 ** a legal notice, here is a blessing:
5039 ** May you do good and not evil.
5040 ** May you find forgiveness for yourself and forgive others.
5041 ** May you share freely, never taking more than you give.
5043 ******************************************************************************
5045 ** This SQLite extension implements functions for the exact display
5046 ** and input of IEEE754 Binary64 floating-point numbers.
5051 ** In the first form, the value X should be a floating-point number.
5052 ** The function will return a string of the form 'ieee754(Y,Z)' where
5053 ** Y and Z are integers such that X==Y*pow(2,Z).
5055 ** In the second form, Y and Z are integers which are the mantissa and
5056 ** base-2 exponent of a new floating point number. The function returns
5057 ** a floating-point value equal to Y*pow(2,Z).
5061 ** ieee754(2.0) -> 'ieee754(2,0)'
5062 ** ieee754(45.25) -> 'ieee754(181,-2)'
5063 ** ieee754(2, 0) -> 2.0
5064 ** ieee754(181, -2) -> 45.25
5066 ** Two additional functions break apart the one-argument ieee754()
5067 ** result into separate integer values:
5069 ** ieee754_mantissa(45.25) -> 181
5070 ** ieee754_exponent(45.25) -> -2
5072 ** These functions convert binary64 numbers into blobs and back again.
5074 ** ieee754_from_blob(x'3ff0000000000000') -> 1.0
5075 ** ieee754_to_blob(1.0) -> x'3ff0000000000000'
5077 ** In all single-argument functions, if the argument is an 8-byte blob
5078 ** then that blob is interpreted as a big-endian binary64 value.
5081 ** EXACT DECIMAL REPRESENTATION OF BINARY64 VALUES
5082 ** -----------------------------------------------
5084 ** This extension in combination with the separate 'decimal' extension
5085 ** can be used to compute the exact decimal representation of binary64
5086 ** values. To begin, first compute a table of exponent values:
5088 ** CREATE TABLE pow2(x INTEGER PRIMARY KEY, v TEXT);
5089 ** WITH RECURSIVE c(x,v) AS (
5092 ** SELECT x+1, decimal_mul(v,'2') FROM c WHERE x+1<=971
5093 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5094 ** WITH RECURSIVE c(x,v) AS (
5097 ** SELECT x-1, decimal_mul(v,'0.5') FROM c WHERE x-1>=-1075
5098 ** ) INSERT INTO pow2(x,v) SELECT x, v FROM c;
5100 ** Then, to compute the exact decimal representation of a floating
5101 ** point value (the value 47.49 is used in the example) do:
5103 ** WITH c(n) AS (VALUES(47.49))
5104 ** ---------------^^^^^---- Replace with whatever you want
5105 ** SELECT decimal_mul(ieee754_mantissa(c.n),pow2.v)
5106 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.n);
5108 ** Here is a query to show various boundry values for the binary64
5111 ** WITH c(name,bin) AS (VALUES
5112 ** ('minimum positive value', x'0000000000000001'),
5113 ** ('maximum subnormal value', x'000fffffffffffff'),
5114 ** ('mininum positive nornal value', x'0010000000000000'),
5115 ** ('maximum value', x'7fefffffffffffff'))
5116 ** SELECT c.name, decimal_mul(ieee754_mantissa(c.bin),pow2.v)
5117 ** FROM pow2, c WHERE pow2.x=ieee754_exponent(c.bin);
5120 /* #include "sqlite3ext.h" */
5121 SQLITE_EXTENSION_INIT1
5125 /* Mark a function parameter as unused, to suppress nuisance compiler
5127 #ifndef UNUSED_PARAMETER
5128 # define UNUSED_PARAMETER(X) (void)(X)
5132 ** Implementation of the ieee754() function
5134 static void ieee754func(
5135 sqlite3_context *context,
5137 sqlite3_value **argv
5145 assert( sizeof(m)==sizeof(r) );
5146 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5147 && sqlite3_value_bytes(argv[0])==sizeof(r)
5149 const unsigned char *x = sqlite3_value_blob(argv[0]);
5151 sqlite3_uint64 v = 0;
5152 for(i=0; i<sizeof(r); i++){
5155 memcpy(&r, &v, sizeof(r));
5157 r = sqlite3_value_double(argv[0]);
5165 memcpy(&a,&r,sizeof(a));
5171 m = a & ((((sqlite3_int64)1)<<52)-1);
5175 m |= ((sqlite3_int64)1)<<52;
5177 while( e<1075 && m>0 && (m&1)==0 ){
5183 switch( *(int*)sqlite3_user_data(context) ){
5185 sqlite3_snprintf(sizeof(zResult), zResult, "ieee754(%lld,%d)",
5187 sqlite3_result_text(context, zResult, -1, SQLITE_TRANSIENT);
5190 sqlite3_result_int64(context, m);
5193 sqlite3_result_int(context, e-1075);
5197 sqlite3_int64 m, e, a;
5200 m = sqlite3_value_int64(argv[0]);
5201 e = sqlite3_value_int64(argv[1]);
5206 }else if( m==0 && e>-1000 && e<1000 ){
5207 sqlite3_result_double(context, 0.0);
5210 while( (m>>32)&0xffe00000 ){
5214 while( m!=0 && ((m>>32)&0xfff00000)==0 ){
5223 }else if( e>0x7ff ){
5226 a = m & ((((sqlite3_int64)1)<<52)-1);
5228 if( isNeg ) a |= ((sqlite3_uint64)1)<<63;
5229 memcpy(&r, &a, sizeof(r));
5230 sqlite3_result_double(context, r);
5235 ** Functions to convert between blobs and floats.
5237 static void ieee754func_from_blob(
5238 sqlite3_context *context,
5240 sqlite3_value **argv
5242 UNUSED_PARAMETER(argc);
5243 if( sqlite3_value_type(argv[0])==SQLITE_BLOB
5244 && sqlite3_value_bytes(argv[0])==sizeof(double)
5247 const unsigned char *x = sqlite3_value_blob(argv[0]);
5249 sqlite3_uint64 v = 0;
5250 for(i=0; i<sizeof(r); i++){
5253 memcpy(&r, &v, sizeof(r));
5254 sqlite3_result_double(context, r);
5257 static void ieee754func_to_blob(
5258 sqlite3_context *context,
5260 sqlite3_value **argv
5262 UNUSED_PARAMETER(argc);
5263 if( sqlite3_value_type(argv[0])==SQLITE_FLOAT
5264 || sqlite3_value_type(argv[0])==SQLITE_INTEGER
5266 double r = sqlite3_value_double(argv[0]);
5268 unsigned char a[sizeof(r)];
5270 memcpy(&v, &r, sizeof(r));
5271 for(i=1; i<=sizeof(r); i++){
5272 a[sizeof(r)-i] = v&0xff;
5275 sqlite3_result_blob(context, a, sizeof(r), SQLITE_TRANSIENT);
5283 int sqlite3_ieee_init(
5286 const sqlite3_api_routines *pApi
5288 static const struct {
5292 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
5294 { "ieee754", 1, 0, ieee754func },
5295 { "ieee754", 2, 0, ieee754func },
5296 { "ieee754_mantissa", 1, 1, ieee754func },
5297 { "ieee754_exponent", 1, 2, ieee754func },
5298 { "ieee754_to_blob", 1, 0, ieee754func_to_blob },
5299 { "ieee754_from_blob", 1, 0, ieee754func_from_blob },
5304 SQLITE_EXTENSION_INIT2(pApi);
5305 (void)pzErrMsg; /* Unused parameter */
5306 for(i=0; i<sizeof(aFunc)/sizeof(aFunc[0]) && rc==SQLITE_OK; i++){
5307 rc = sqlite3_create_function(db, aFunc[i].zFName, aFunc[i].nArg,
5308 SQLITE_UTF8|SQLITE_INNOCUOUS,
5309 (void*)&aFunc[i].iAux,
5310 aFunc[i].xFunc, 0, 0);
5315 /************************* End ../ext/misc/ieee754.c ********************/
5316 /************************* Begin ../ext/misc/series.c ******************/
5320 ** The author disclaims copyright to this source code. In place of
5321 ** a legal notice, here is a blessing:
5323 ** May you do good and not evil.
5324 ** May you find forgiveness for yourself and forgive others.
5325 ** May you share freely, never taking more than you give.
5327 *************************************************************************
5329 ** This file demonstrates how to create a table-valued-function using
5330 ** a virtual table. This demo implements the generate_series() function
5331 ** which gives similar results to the eponymous function in PostgreSQL.
5334 ** SELECT * FROM generate_series(0,100,5);
5336 ** The query above returns integers from 0 through 100 counting by steps
5339 ** SELECT * FROM generate_series(0,100);
5341 ** Integers from 0 through 100 with a step size of 1.
5343 ** SELECT * FROM generate_series(20) LIMIT 10;
5345 ** Integers 20 through 29.
5349 ** The generate_series "function" is really a virtual table with the
5350 ** following schema:
5352 ** CREATE TABLE generate_series(
5359 ** Function arguments in queries against this virtual table are translated
5360 ** into equality constraints against successive hidden columns. In other
5361 ** words, the following pairs of queries are equivalent to each other:
5363 ** SELECT * FROM generate_series(0,100,5);
5364 ** SELECT * FROM generate_series WHERE start=0 AND stop=100 AND step=5;
5366 ** SELECT * FROM generate_series(0,100);
5367 ** SELECT * FROM generate_series WHERE start=0 AND stop=100;
5369 ** SELECT * FROM generate_series(20) LIMIT 10;
5370 ** SELECT * FROM generate_series WHERE start=20 LIMIT 10;
5372 ** The generate_series virtual table implementation leaves the xCreate method
5373 ** set to NULL. This means that it is not possible to do a CREATE VIRTUAL
5374 ** TABLE command with "generate_series" as the USING argument. Instead, there
5375 ** is a single generate_series virtual table that is always available without
5376 ** having to be created first.
5378 ** The xBestIndex method looks for equality constraints against the hidden
5379 ** start, stop, and step columns, and if present, it uses those constraints
5380 ** to bound the sequence of generated values. If the equality constraints
5381 ** are missing, it uses 0 for start, 4294967295 for stop, and 1 for step.
5382 ** xBestIndex returns a small cost when both start and stop are available,
5383 ** and a very large cost if either start or stop are unavailable. This
5384 ** encourages the query planner to order joins such that the bounds of the
5385 ** series are well-defined.
5387 /* #include "sqlite3ext.h" */
5388 SQLITE_EXTENSION_INIT1
5392 #ifndef SQLITE_OMIT_VIRTUALTABLE
5395 /* series_cursor is a subclass of sqlite3_vtab_cursor which will
5396 ** serve as the underlying representation of a cursor that scans
5397 ** over rows of the result
5399 typedef struct series_cursor series_cursor;
5400 struct series_cursor {
5401 sqlite3_vtab_cursor base; /* Base class - must be first */
5402 int isDesc; /* True to count down rather than up */
5403 sqlite3_int64 iRowid; /* The rowid */
5404 sqlite3_int64 iValue; /* Current value ("value") */
5405 sqlite3_int64 mnValue; /* Mimimum value ("start") */
5406 sqlite3_int64 mxValue; /* Maximum value ("stop") */
5407 sqlite3_int64 iStep; /* Increment ("step") */
5411 ** The seriesConnect() method is invoked to create a new
5412 ** series_vtab that describes the generate_series virtual table.
5414 ** Think of this routine as the constructor for series_vtab objects.
5416 ** All this routine needs to do is:
5418 ** (1) Allocate the series_vtab object and initialize all fields.
5420 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
5421 ** result set of queries against generate_series will look like.
5423 static int seriesConnect(
5426 int argcUnused, const char *const*argvUnused,
5427 sqlite3_vtab **ppVtab,
5433 /* Column numbers */
5434 #define SERIES_COLUMN_VALUE 0
5435 #define SERIES_COLUMN_START 1
5436 #define SERIES_COLUMN_STOP 2
5437 #define SERIES_COLUMN_STEP 3
5443 rc = sqlite3_declare_vtab(db,
5444 "CREATE TABLE x(value,start hidden,stop hidden,step hidden)");
5445 if( rc==SQLITE_OK ){
5446 pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
5447 if( pNew==0 ) return SQLITE_NOMEM;
5448 memset(pNew, 0, sizeof(*pNew));
5449 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
5455 ** This method is the destructor for series_cursor objects.
5457 static int seriesDisconnect(sqlite3_vtab *pVtab){
5458 sqlite3_free(pVtab);
5463 ** Constructor for a new series_cursor object.
5465 static int seriesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor){
5466 series_cursor *pCur;
5468 pCur = sqlite3_malloc( sizeof(*pCur) );
5469 if( pCur==0 ) return SQLITE_NOMEM;
5470 memset(pCur, 0, sizeof(*pCur));
5471 *ppCursor = &pCur->base;
5476 ** Destructor for a series_cursor.
5478 static int seriesClose(sqlite3_vtab_cursor *cur){
5485 ** Advance a series_cursor to its next row of output.
5487 static int seriesNext(sqlite3_vtab_cursor *cur){
5488 series_cursor *pCur = (series_cursor*)cur;
5490 pCur->iValue -= pCur->iStep;
5492 pCur->iValue += pCur->iStep;
5499 ** Return values of columns for the row at which the series_cursor
5500 ** is currently pointing.
5502 static int seriesColumn(
5503 sqlite3_vtab_cursor *cur, /* The cursor */
5504 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5505 int i /* Which column to return */
5507 series_cursor *pCur = (series_cursor*)cur;
5508 sqlite3_int64 x = 0;
5510 case SERIES_COLUMN_START: x = pCur->mnValue; break;
5511 case SERIES_COLUMN_STOP: x = pCur->mxValue; break;
5512 case SERIES_COLUMN_STEP: x = pCur->iStep; break;
5513 default: x = pCur->iValue; break;
5515 sqlite3_result_int64(ctx, x);
5520 ** Return the rowid for the current row. In this implementation, the
5521 ** first row returned is assigned rowid value 1, and each subsequent
5522 ** row a value 1 more than that of the previous.
5524 static int seriesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
5525 series_cursor *pCur = (series_cursor*)cur;
5526 *pRowid = pCur->iRowid;
5531 ** Return TRUE if the cursor has been moved off of the last
5534 static int seriesEof(sqlite3_vtab_cursor *cur){
5535 series_cursor *pCur = (series_cursor*)cur;
5537 return pCur->iValue < pCur->mnValue;
5539 return pCur->iValue > pCur->mxValue;
5543 /* True to cause run-time checking of the start=, stop=, and/or step=
5544 ** parameters. The only reason to do this is for testing the
5545 ** constraint checking logic for virtual tables in the SQLite core.
5547 #ifndef SQLITE_SERIES_CONSTRAINT_VERIFY
5548 # define SQLITE_SERIES_CONSTRAINT_VERIFY 0
5552 ** This method is called to "rewind" the series_cursor object back
5553 ** to the first row of output. This method is always called at least
5554 ** once prior to any call to seriesColumn() or seriesRowid() or
5557 ** The query plan selected by seriesBestIndex is passed in the idxNum
5558 ** parameter. (idxStr is not used in this implementation.) idxNum
5559 ** is a bitmask showing which constraints are available:
5565 ** Also, if bit 8 is set, that means that the series should be output
5566 ** in descending order rather than in ascending order.
5568 ** This routine should initialize the cursor and position it so that it
5569 ** is pointing at the first row, or pointing off the end of the table
5570 ** (so that seriesEof() will return true) if the table is empty.
5572 static int seriesFilter(
5573 sqlite3_vtab_cursor *pVtabCursor,
5574 int idxNum, const char *idxStrUnused,
5575 int argc, sqlite3_value **argv
5577 series_cursor *pCur = (series_cursor *)pVtabCursor;
5581 pCur->mnValue = sqlite3_value_int64(argv[i++]);
5586 pCur->mxValue = sqlite3_value_int64(argv[i++]);
5588 pCur->mxValue = 0xffffffff;
5591 pCur->iStep = sqlite3_value_int64(argv[i++]);
5592 if( pCur->iStep<1 ) pCur->iStep = 1;
5596 for(i=0; i<argc; i++){
5597 if( sqlite3_value_type(argv[i])==SQLITE_NULL ){
5598 /* If any of the constraints have a NULL value, then return no rows.
5599 ** See ticket https://www.sqlite.org/src/info/fac496b61722daf2 */
5607 pCur->iValue = pCur->mxValue;
5608 if( pCur->iStep>0 ){
5609 pCur->iValue -= (pCur->mxValue - pCur->mnValue)%pCur->iStep;
5613 pCur->iValue = pCur->mnValue;
5620 ** SQLite will invoke this method one or more times while planning a query
5621 ** that uses the generate_series virtual table. This routine needs to create
5622 ** a query plan for each invocation and compute an estimated cost for that
5625 ** In this implementation idxNum is used to represent the
5626 ** query plan. idxStr is unused.
5628 ** The query plan is represented by bits in idxNum:
5630 ** (1) start = $value -- constraint exists
5631 ** (2) stop = $value -- constraint exists
5632 ** (4) step = $value -- constraint exists
5633 ** (8) output in descending order
5635 static int seriesBestIndex(
5636 sqlite3_vtab *tabUnused,
5637 sqlite3_index_info *pIdxInfo
5639 int i, j; /* Loop over constraints */
5640 int idxNum = 0; /* The query plan bitmask */
5641 int unusableMask = 0; /* Mask of unusable constraints */
5642 int nArg = 0; /* Number of arguments that seriesFilter() expects */
5643 int aIdx[3]; /* Constraints on start, stop, and step */
5644 const struct sqlite3_index_constraint *pConstraint;
5646 /* This implementation assumes that the start, stop, and step columns
5647 ** are the last three columns in the virtual table. */
5648 assert( SERIES_COLUMN_STOP == SERIES_COLUMN_START+1 );
5649 assert( SERIES_COLUMN_STEP == SERIES_COLUMN_START+2 );
5651 aIdx[0] = aIdx[1] = aIdx[2] = -1;
5652 pConstraint = pIdxInfo->aConstraint;
5653 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
5654 int iCol; /* 0 for start, 1 for stop, 2 for step */
5655 int iMask; /* bitmask for those column */
5656 if( pConstraint->iColumn<SERIES_COLUMN_START ) continue;
5657 iCol = pConstraint->iColumn - SERIES_COLUMN_START;
5658 assert( iCol>=0 && iCol<=2 );
5660 if( pConstraint->usable==0 ){
5661 unusableMask |= iMask;
5663 }else if( pConstraint->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5669 if( (j = aIdx[i])>=0 ){
5670 pIdxInfo->aConstraintUsage[j].argvIndex = ++nArg;
5671 pIdxInfo->aConstraintUsage[j].omit = !SQLITE_SERIES_CONSTRAINT_VERIFY;
5674 if( (unusableMask & ~idxNum)!=0 ){
5675 /* The start, stop, and step columns are inputs. Therefore if there
5676 ** are unusable constraints on any of start, stop, or step then
5677 ** this plan is unusable */
5678 return SQLITE_CONSTRAINT;
5680 if( (idxNum & 3)==3 ){
5681 /* Both start= and stop= boundaries are available. This is the
5682 ** the preferred case */
5683 pIdxInfo->estimatedCost = (double)(2 - ((idxNum&4)!=0));
5684 pIdxInfo->estimatedRows = 1000;
5685 if( pIdxInfo->nOrderBy==1 ){
5686 if( pIdxInfo->aOrderBy[0].desc ) idxNum |= 8;
5687 pIdxInfo->orderByConsumed = 1;
5690 /* If either boundary is missing, we have to generate a huge span
5691 ** of numbers. Make this case very expensive so that the query
5692 ** planner will work hard to avoid it. */
5693 pIdxInfo->estimatedRows = 2147483647;
5695 pIdxInfo->idxNum = idxNum;
5700 ** This following structure defines all the methods for the
5701 ** generate_series virtual table.
5703 static sqlite3_module seriesModule = {
5706 seriesConnect, /* xConnect */
5707 seriesBestIndex, /* xBestIndex */
5708 seriesDisconnect, /* xDisconnect */
5710 seriesOpen, /* xOpen - open a cursor */
5711 seriesClose, /* xClose - close a cursor */
5712 seriesFilter, /* xFilter - configure scan constraints */
5713 seriesNext, /* xNext - advance a cursor */
5714 seriesEof, /* xEof - check for end of scan */
5715 seriesColumn, /* xColumn - read data */
5716 seriesRowid, /* xRowid - read data */
5722 0, /* xFindMethod */
5726 0, /* xRollbackTo */
5730 #endif /* SQLITE_OMIT_VIRTUALTABLE */
5735 int sqlite3_series_init(
5738 const sqlite3_api_routines *pApi
5741 SQLITE_EXTENSION_INIT2(pApi);
5742 #ifndef SQLITE_OMIT_VIRTUALTABLE
5743 if( sqlite3_libversion_number()<3008012 ){
5744 *pzErrMsg = sqlite3_mprintf(
5745 "generate_series() requires SQLite 3.8.12 or later");
5746 return SQLITE_ERROR;
5748 rc = sqlite3_create_module(db, "generate_series", &seriesModule, 0);
5753 /************************* End ../ext/misc/series.c ********************/
5754 #ifdef SQLITE_HAVE_ZLIB
5755 /************************* Begin ../ext/misc/zipfile.c ******************/
5759 ** The author disclaims copyright to this source code. In place of
5760 ** a legal notice, here is a blessing:
5762 ** May you do good and not evil.
5763 ** May you find forgiveness for yourself and forgive others.
5764 ** May you share freely, never taking more than you give.
5766 ******************************************************************************
5768 ** This file implements a virtual table for reading and writing ZIP archive
5773 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
5775 ** Current limitations:
5777 ** * No support for encryption
5778 ** * No support for ZIP archives spanning multiple files
5779 ** * No support for zip64 extensions
5780 ** * Only the "inflate/deflate" (zlib) compression method is supported
5782 /* #include "sqlite3ext.h" */
5783 SQLITE_EXTENSION_INIT1
5790 #ifndef SQLITE_OMIT_VIRTUALTABLE
5792 #ifndef SQLITE_AMALGAMATION
5794 /* typedef sqlite3_int64 i64; */
5795 /* typedef unsigned char u8; */
5796 typedef unsigned short u16;
5797 typedef unsigned long u32;
5798 #define MIN(a,b) ((a)<(b) ? (a) : (b))
5800 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
5801 # define ALWAYS(X) (1)
5802 # define NEVER(X) (0)
5803 #elif !defined(NDEBUG)
5804 # define ALWAYS(X) ((X)?1:(assert(0),0))
5805 # define NEVER(X) ((X)?(assert(0),1):0)
5807 # define ALWAYS(X) (X)
5808 # define NEVER(X) (X)
5811 #endif /* SQLITE_AMALGAMATION */
5814 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
5816 ** In some ways it would be better to obtain these values from system
5817 ** header files. But, the dependency is undesirable and (a) these
5818 ** have been stable for decades, (b) the values are part of POSIX and
5819 ** are also made explicit in [man stat], and (c) are part of the
5820 ** file format for zip archives.
5823 # define S_IFDIR 0040000
5826 # define S_IFREG 0100000
5829 # define S_IFLNK 0120000
5832 static const char ZIPFILE_SCHEMA[] =
5834 "name PRIMARY KEY," /* 0: Name of file in zip archive */
5835 "mode," /* 1: POSIX mode for file */
5836 "mtime," /* 2: Last modification time (secs since 1970)*/
5837 "sz," /* 3: Size of object */
5838 "rawdata," /* 4: Raw data */
5839 "data," /* 5: Uncompressed data */
5840 "method," /* 6: Compression method (integer) */
5841 "z HIDDEN" /* 7: Name of zip file */
5844 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
5845 #define ZIPFILE_BUFFER_SIZE (64*1024)
5849 ** Magic numbers used to read and write zip files.
5851 ** ZIPFILE_NEWENTRY_MADEBY:
5852 ** Use this value for the "version-made-by" field in new zip file
5853 ** entries. The upper byte indicates "unix", and the lower byte
5854 ** indicates that the zip file matches pkzip specification 3.0.
5855 ** This is what info-zip seems to do.
5857 ** ZIPFILE_NEWENTRY_REQUIRED:
5858 ** Value for "version-required-to-extract" field of new entries.
5859 ** Version 2.0 is required to support folders and deflate compression.
5861 ** ZIPFILE_NEWENTRY_FLAGS:
5862 ** Value for "general-purpose-bit-flags" field of new entries. Bit
5863 ** 11 means "utf-8 filename and comment".
5865 ** ZIPFILE_SIGNATURE_CDS:
5866 ** First 4 bytes of a valid CDS record.
5868 ** ZIPFILE_SIGNATURE_LFH:
5869 ** First 4 bytes of a valid LFH record.
5871 ** ZIPFILE_SIGNATURE_EOCD
5872 ** First 4 bytes of a valid EOCD record.
5874 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
5875 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
5876 #define ZIPFILE_NEWENTRY_REQUIRED 20
5877 #define ZIPFILE_NEWENTRY_FLAGS 0x800
5878 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
5879 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
5880 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
5883 ** The sizes of the fixed-size part of each of the three main data
5884 ** structures in a zip archive.
5886 #define ZIPFILE_LFH_FIXED_SZ 30
5887 #define ZIPFILE_EOCD_FIXED_SZ 22
5888 #define ZIPFILE_CDS_FIXED_SZ 46
5891 *** 4.3.16 End of central directory record:
5893 *** end of central dir signature 4 bytes (0x06054b50)
5894 *** number of this disk 2 bytes
5895 *** number of the disk with the
5896 *** start of the central directory 2 bytes
5897 *** total number of entries in the
5898 *** central directory on this disk 2 bytes
5899 *** total number of entries in
5900 *** the central directory 2 bytes
5901 *** size of the central directory 4 bytes
5902 *** offset of start of central
5903 *** directory with respect to
5904 *** the starting disk number 4 bytes
5905 *** .ZIP file comment length 2 bytes
5906 *** .ZIP file comment (variable size)
5908 typedef struct ZipfileEOCD ZipfileEOCD;
5909 struct ZipfileEOCD {
5919 *** 4.3.12 Central directory structure:
5923 *** central file header signature 4 bytes (0x02014b50)
5924 *** version made by 2 bytes
5925 *** version needed to extract 2 bytes
5926 *** general purpose bit flag 2 bytes
5927 *** compression method 2 bytes
5928 *** last mod file time 2 bytes
5929 *** last mod file date 2 bytes
5931 *** compressed size 4 bytes
5932 *** uncompressed size 4 bytes
5933 *** file name length 2 bytes
5934 *** extra field length 2 bytes
5935 *** file comment length 2 bytes
5936 *** disk number start 2 bytes
5937 *** internal file attributes 2 bytes
5938 *** external file attributes 4 bytes
5939 *** relative offset of local header 4 bytes
5941 typedef struct ZipfileCDS ZipfileCDS;
5944 u16 iVersionExtract;
5959 char *zFile; /* Filename (sqlite3_malloc()) */
5963 *** 4.3.7 Local file header:
5965 *** local file header signature 4 bytes (0x04034b50)
5966 *** version needed to extract 2 bytes
5967 *** general purpose bit flag 2 bytes
5968 *** compression method 2 bytes
5969 *** last mod file time 2 bytes
5970 *** last mod file date 2 bytes
5972 *** compressed size 4 bytes
5973 *** uncompressed size 4 bytes
5974 *** file name length 2 bytes
5975 *** extra field length 2 bytes
5978 typedef struct ZipfileLFH ZipfileLFH;
5980 u16 iVersionExtract;
5992 typedef struct ZipfileEntry ZipfileEntry;
5993 struct ZipfileEntry {
5994 ZipfileCDS cds; /* Parsed CDS record */
5995 u32 mUnixTime; /* Modification time, in UNIX format */
5996 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
5997 i64 iDataOff; /* Offset to data in file (if aData==0) */
5998 u8 *aData; /* cds.szCompressed bytes of compressed data */
5999 ZipfileEntry *pNext; /* Next element in in-memory CDS */
6003 ** Cursor type for zipfile tables.
6005 typedef struct ZipfileCsr ZipfileCsr;
6007 sqlite3_vtab_cursor base; /* Base class - must be first */
6008 i64 iId; /* Cursor ID */
6009 u8 bEof; /* True when at EOF */
6010 u8 bNoop; /* If next xNext() call is no-op */
6012 /* Used outside of write transactions */
6013 FILE *pFile; /* Zip file */
6014 i64 iNextOff; /* Offset of next record in central directory */
6015 ZipfileEOCD eocd; /* Parse of central directory record */
6017 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
6018 ZipfileEntry *pCurrent; /* Current entry */
6019 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
6022 typedef struct ZipfileTab ZipfileTab;
6024 sqlite3_vtab base; /* Base class - must be first */
6025 char *zFile; /* Zip file this table accesses (may be NULL) */
6026 sqlite3 *db; /* Host database connection */
6027 u8 *aBuffer; /* Temporary buffer used for various tasks */
6029 ZipfileCsr *pCsrList; /* List of cursors */
6032 /* The following are used by write transactions only */
6033 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
6034 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
6035 FILE *pWriteFd; /* File handle open on zip archive */
6036 i64 szCurrent; /* Current size of zip archive */
6037 i64 szOrig; /* Size of archive at start of transaction */
6041 ** Set the error message contained in context ctx to the results of
6042 ** vprintf(zFmt, ...).
6044 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
6048 zMsg = sqlite3_vmprintf(zFmt, ap);
6049 sqlite3_result_error(ctx, zMsg, -1);
6055 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
6056 ** is not quoted, do nothing.
6058 static void zipfileDequote(char *zIn){
6060 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
6063 if( q=='[' ) q = ']';
6064 while( ALWAYS(zIn[iIn]) ){
6065 char c = zIn[iIn++];
6066 if( c==q && zIn[iIn++]!=q ) break;
6074 ** Construct a new ZipfileTab virtual table object.
6076 ** argv[0] -> module name ("zipfile")
6077 ** argv[1] -> database name
6078 ** argv[2] -> table name
6079 ** argv[...] -> "column name" and other module argument fields.
6081 static int zipfileConnect(
6084 int argc, const char *const*argv,
6085 sqlite3_vtab **ppVtab,
6088 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
6090 const char *zFile = 0;
6091 ZipfileTab *pNew = 0;
6094 /* If the table name is not "zipfile", require that the argument be
6095 ** specified. This stops zipfile tables from being created as:
6097 ** CREATE VIRTUAL TABLE zzz USING zipfile();
6099 ** It does not prevent:
6101 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
6103 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
6104 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
6105 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
6106 return SQLITE_ERROR;
6111 nFile = (int)strlen(zFile)+1;
6114 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
6115 if( rc==SQLITE_OK ){
6116 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
6117 if( pNew==0 ) return SQLITE_NOMEM;
6118 memset(pNew, 0, nByte+nFile);
6120 pNew->aBuffer = (u8*)&pNew[1];
6122 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
6123 memcpy(pNew->zFile, zFile, nFile);
6124 zipfileDequote(pNew->zFile);
6127 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
6128 *ppVtab = (sqlite3_vtab*)pNew;
6133 ** Free the ZipfileEntry structure indicated by the only argument.
6135 static void zipfileEntryFree(ZipfileEntry *p){
6137 sqlite3_free(p->cds.zFile);
6143 ** Release resources that should be freed at the end of a write
6146 static void zipfileCleanupTransaction(ZipfileTab *pTab){
6147 ZipfileEntry *pEntry;
6148 ZipfileEntry *pNext;
6150 if( pTab->pWriteFd ){
6151 fclose(pTab->pWriteFd);
6154 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
6155 pNext = pEntry->pNext;
6156 zipfileEntryFree(pEntry);
6158 pTab->pFirstEntry = 0;
6159 pTab->pLastEntry = 0;
6160 pTab->szCurrent = 0;
6165 ** This method is the destructor for zipfile vtab objects.
6167 static int zipfileDisconnect(sqlite3_vtab *pVtab){
6168 zipfileCleanupTransaction((ZipfileTab*)pVtab);
6169 sqlite3_free(pVtab);
6174 ** Constructor for a new ZipfileCsr object.
6176 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
6177 ZipfileTab *pTab = (ZipfileTab*)p;
6179 pCsr = sqlite3_malloc(sizeof(*pCsr));
6180 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
6182 return SQLITE_NOMEM;
6184 memset(pCsr, 0, sizeof(*pCsr));
6185 pCsr->iId = ++pTab->iNextCsrid;
6186 pCsr->pCsrNext = pTab->pCsrList;
6187 pTab->pCsrList = pCsr;
6192 ** Reset a cursor back to the state it was in when first returned
6193 ** by zipfileOpen().
6195 static void zipfileResetCursor(ZipfileCsr *pCsr){
6197 ZipfileEntry *pNext;
6201 fclose(pCsr->pFile);
6203 zipfileEntryFree(pCsr->pCurrent);
6207 for(p=pCsr->pFreeEntry; p; p=pNext){
6209 zipfileEntryFree(p);
6214 ** Destructor for an ZipfileCsr.
6216 static int zipfileClose(sqlite3_vtab_cursor *cur){
6217 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6218 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
6220 zipfileResetCursor(pCsr);
6222 /* Remove this cursor from the ZipfileTab.pCsrList list. */
6223 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
6224 *pp = pCsr->pCsrNext;
6231 ** Set the error message for the virtual table associated with cursor
6232 ** pCsr to the results of vprintf(zFmt, ...).
6234 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
6237 sqlite3_free(pTab->base.zErrMsg);
6238 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
6241 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
6244 sqlite3_free(pCsr->base.pVtab->zErrMsg);
6245 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
6250 ** Read nRead bytes of data from offset iOff of file pFile into buffer
6251 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
6254 ** If an error does occur, output variable (*pzErrmsg) may be set to point
6255 ** to an English language error message. It is the responsibility of the
6256 ** caller to eventually free this buffer using
6259 static int zipfileReadData(
6260 FILE *pFile, /* Read from this file */
6261 u8 *aRead, /* Read into this buffer */
6262 int nRead, /* Number of bytes to read */
6263 i64 iOff, /* Offset to read from */
6264 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
6267 fseek(pFile, (long)iOff, SEEK_SET);
6268 n = fread(aRead, 1, nRead, pFile);
6269 if( (int)n!=nRead ){
6270 *pzErrmsg = sqlite3_mprintf("error in fread()");
6271 return SQLITE_ERROR;
6276 static int zipfileAppendData(
6283 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
6284 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
6285 if( (int)n!=nWrite ){
6286 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
6287 return SQLITE_ERROR;
6289 pTab->szCurrent += nWrite;
6295 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
6297 static u16 zipfileGetU16(const u8 *aBuf){
6298 return (aBuf[1] << 8) + aBuf[0];
6302 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
6304 static u32 zipfileGetU32(const u8 *aBuf){
6305 return ((u32)(aBuf[3]) << 24)
6306 + ((u32)(aBuf[2]) << 16)
6307 + ((u32)(aBuf[1]) << 8)
6308 + ((u32)(aBuf[0]) << 0);
6312 ** Write a 16-bit little endiate integer into buffer aBuf.
6314 static void zipfilePutU16(u8 *aBuf, u16 val){
6315 aBuf[0] = val & 0xFF;
6316 aBuf[1] = (val>>8) & 0xFF;
6320 ** Write a 32-bit little endiate integer into buffer aBuf.
6322 static void zipfilePutU32(u8 *aBuf, u32 val){
6323 aBuf[0] = val & 0xFF;
6324 aBuf[1] = (val>>8) & 0xFF;
6325 aBuf[2] = (val>>16) & 0xFF;
6326 aBuf[3] = (val>>24) & 0xFF;
6329 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
6330 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
6332 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
6333 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
6336 ** Magic numbers used to read CDS records.
6338 #define ZIPFILE_CDS_NFILE_OFF 28
6339 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
6342 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
6343 ** if the record is not well-formed, or SQLITE_OK otherwise.
6345 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
6347 u32 sig = zipfileRead32(aRead);
6349 if( sig!=ZIPFILE_SIGNATURE_CDS ){
6352 pCDS->iVersionMadeBy = zipfileRead16(aRead);
6353 pCDS->iVersionExtract = zipfileRead16(aRead);
6354 pCDS->flags = zipfileRead16(aRead);
6355 pCDS->iCompression = zipfileRead16(aRead);
6356 pCDS->mTime = zipfileRead16(aRead);
6357 pCDS->mDate = zipfileRead16(aRead);
6358 pCDS->crc32 = zipfileRead32(aRead);
6359 pCDS->szCompressed = zipfileRead32(aRead);
6360 pCDS->szUncompressed = zipfileRead32(aRead);
6361 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6362 pCDS->nFile = zipfileRead16(aRead);
6363 pCDS->nExtra = zipfileRead16(aRead);
6364 pCDS->nComment = zipfileRead16(aRead);
6365 pCDS->iDiskStart = zipfileRead16(aRead);
6366 pCDS->iInternalAttr = zipfileRead16(aRead);
6367 pCDS->iExternalAttr = zipfileRead32(aRead);
6368 pCDS->iOffset = zipfileRead32(aRead);
6369 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
6376 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
6377 ** if the record is not well-formed, or SQLITE_OK otherwise.
6379 static int zipfileReadLFH(
6383 u8 *aRead = aBuffer;
6386 u32 sig = zipfileRead32(aRead);
6387 if( sig!=ZIPFILE_SIGNATURE_LFH ){
6390 pLFH->iVersionExtract = zipfileRead16(aRead);
6391 pLFH->flags = zipfileRead16(aRead);
6392 pLFH->iCompression = zipfileRead16(aRead);
6393 pLFH->mTime = zipfileRead16(aRead);
6394 pLFH->mDate = zipfileRead16(aRead);
6395 pLFH->crc32 = zipfileRead32(aRead);
6396 pLFH->szCompressed = zipfileRead32(aRead);
6397 pLFH->szUncompressed = zipfileRead32(aRead);
6398 pLFH->nFile = zipfileRead16(aRead);
6399 pLFH->nExtra = zipfileRead16(aRead);
6406 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
6407 ** Scan through this buffer to find an "extra-timestamp" field. If one
6408 ** exists, extract the 32-bit modification-timestamp from it and store
6409 ** the value in output parameter *pmTime.
6411 ** Zero is returned if no extra-timestamp record could be found (and so
6412 ** *pmTime is left unchanged), or non-zero otherwise.
6414 ** The general format of an extra field is:
6416 ** Header ID 2 bytes
6417 ** Data Size 2 bytes
6420 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
6423 u8 *pEnd = &aExtra[nExtra];
6426 u16 id = zipfileRead16(p);
6427 u16 nByte = zipfileRead16(p);
6430 case ZIPFILE_EXTRA_TIMESTAMP: {
6432 if( b & 0x01 ){ /* 0x01 -> modtime is present */
6433 *pmTime = zipfileGetU32(&p[1]);
6446 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
6447 ** fields of the CDS structure passed as the only argument to a 32-bit
6448 ** UNIX seconds-since-the-epoch timestamp. Return the result.
6450 ** "Standard" MS-DOS time format:
6452 ** File modification time:
6453 ** Bits 00-04: seconds divided by 2
6454 ** Bits 05-10: minute
6456 ** File modification date:
6458 ** Bits 05-08: month (1-12)
6459 ** Bits 09-15: years from 1980
6461 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
6463 static u32 zipfileMtime(ZipfileCDS *pCDS){
6464 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
6465 int M = ((pCDS->mDate >> 5) & 0x0F);
6466 int D = (pCDS->mDate & 0x1F);
6469 int sec = (pCDS->mTime & 0x1F)*2;
6470 int min = (pCDS->mTime >> 5) & 0x3F;
6471 int hr = (pCDS->mTime >> 11) & 0x1F;
6474 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
6476 /* Calculate the JD in seconds for noon on the day in question */
6481 JD = (i64)(24*60*60) * (
6482 (int)(365.25 * (Y + 4716))
6483 + (int)(30.6001 * (M + 1))
6487 /* Correct the JD for the time within the day */
6488 JD += (hr-12) * 3600 + min * 60 + sec;
6490 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
6491 return (u32)(JD - (i64)(24405875) * 24*60*6);
6495 ** The opposite of zipfileMtime(). This function populates the mTime and
6496 ** mDate fields of the CDS structure passed as the first argument according
6497 ** to the UNIX timestamp value passed as the second.
6499 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
6500 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
6501 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
6507 A = (int)((JD - 1867216.25)/36524.25);
6508 A = (int)(JD + 1 + A - (A/4));
6510 C = (int)((B - 122.1)/365.25);
6511 D = (36525*(C&32767))/100;
6512 E = (int)((B-D)/30.6001);
6514 day = B - D - (int)(30.6001*E);
6515 mon = (E<14 ? E-1 : E-13);
6516 yr = mon>2 ? C-4716 : C-4715;
6518 hr = (mUnixTime % (24*60*60)) / (60*60);
6519 min = (mUnixTime % (60*60)) / 60;
6520 sec = (mUnixTime % 60);
6523 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
6524 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
6526 pCds->mDate = pCds->mTime = 0;
6529 assert( mUnixTime<315507600
6530 || mUnixTime==zipfileMtime(pCds)
6531 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
6532 /* || (mUnixTime % 2) */
6537 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
6538 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
6539 ** then pFile is a file-handle open on a zip file. In either case, this
6540 ** function creates a ZipfileEntry object based on the zip archive entry
6541 ** for which the CDS record is at offset iOff.
6543 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
6544 ** the new object. Otherwise, an SQLite error code is returned and the
6545 ** final value of (*ppEntry) undefined.
6547 static int zipfileGetEntry(
6548 ZipfileTab *pTab, /* Store any error message here */
6549 const u8 *aBlob, /* Pointer to in-memory file image */
6550 int nBlob, /* Size of aBlob[] in bytes */
6551 FILE *pFile, /* If aBlob==0, read from this file */
6552 i64 iOff, /* Offset of CDS record */
6553 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
6556 char **pzErr = &pTab->base.zErrMsg;
6560 aRead = pTab->aBuffer;
6561 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
6563 aRead = (u8*)&aBlob[iOff];
6566 if( rc==SQLITE_OK ){
6567 sqlite3_int64 nAlloc;
6570 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
6571 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
6572 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
6574 nAlloc = sizeof(ZipfileEntry) + nExtra;
6576 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
6579 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
6583 memset(pNew, 0, sizeof(ZipfileEntry));
6584 rc = zipfileReadCDS(aRead, &pNew->cds);
6585 if( rc!=SQLITE_OK ){
6586 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
6587 }else if( aBlob==0 ){
6588 rc = zipfileReadData(
6589 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
6592 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
6596 if( rc==SQLITE_OK ){
6597 u32 *pt = &pNew->mUnixTime;
6598 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
6599 pNew->aExtra = (u8*)&pNew[1];
6600 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
6601 if( pNew->cds.zFile==0 ){
6603 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
6604 pNew->mUnixTime = zipfileMtime(&pNew->cds);
6608 if( rc==SQLITE_OK ){
6609 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
6612 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
6614 aRead = (u8*)&aBlob[pNew->cds.iOffset];
6617 rc = zipfileReadLFH(aRead, &lfh);
6618 if( rc==SQLITE_OK ){
6619 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
6620 pNew->iDataOff += lfh.nFile + lfh.nExtra;
6621 if( aBlob && pNew->cds.szCompressed ){
6622 pNew->aData = &pNew->aExtra[nExtra];
6623 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
6626 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
6627 (int)pNew->cds.iOffset
6632 if( rc!=SQLITE_OK ){
6633 zipfileEntryFree(pNew);
6643 ** Advance an ZipfileCsr to its next row of output.
6645 static int zipfileNext(sqlite3_vtab_cursor *cur){
6646 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6650 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
6651 zipfileEntryFree(pCsr->pCurrent);
6653 if( pCsr->iNextOff>=iEof ){
6656 ZipfileEntry *p = 0;
6657 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
6658 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
6659 if( rc==SQLITE_OK ){
6660 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
6661 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
6667 pCsr->pCurrent = pCsr->pCurrent->pNext;
6669 if( pCsr->pCurrent==0 ){
6678 static void zipfileFree(void *p) {
6683 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
6684 ** size is nOut bytes. This function uncompresses the data and sets the
6685 ** return value in context pCtx to the result (a blob).
6687 ** If an error occurs, an error code is left in pCtx instead.
6689 static void zipfileInflate(
6690 sqlite3_context *pCtx, /* Store result here */
6691 const u8 *aIn, /* Compressed data */
6692 int nIn, /* Size of buffer aIn[] in bytes */
6693 int nOut /* Expected output size */
6695 u8 *aRes = sqlite3_malloc(nOut);
6697 sqlite3_result_error_nomem(pCtx);
6701 memset(&str, 0, sizeof(str));
6703 str.next_in = (Byte*)aIn;
6705 str.next_out = (Byte*)aRes;
6706 str.avail_out = nOut;
6708 err = inflateInit2(&str, -15);
6710 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
6712 err = inflate(&str, Z_NO_FLUSH);
6713 if( err!=Z_STREAM_END ){
6714 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
6716 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
6726 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
6727 ** compresses it and sets (*ppOut) to point to a buffer containing the
6728 ** compressed data. The caller is responsible for eventually calling
6729 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
6730 ** is set to the size of buffer (*ppOut) in bytes.
6732 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
6733 ** code is returned and an error message left in virtual-table handle
6734 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
6737 static int zipfileDeflate(
6738 const u8 *aIn, int nIn, /* Input */
6739 u8 **ppOut, int *pnOut, /* Output */
6740 char **pzErr /* OUT: Error message */
6743 sqlite3_int64 nAlloc;
6747 memset(&str, 0, sizeof(str));
6748 str.next_in = (Bytef*)aIn;
6750 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
6752 nAlloc = deflateBound(&str, nIn);
6753 aOut = (u8*)sqlite3_malloc64(nAlloc);
6758 str.next_out = aOut;
6759 str.avail_out = nAlloc;
6760 res = deflate(&str, Z_FINISH);
6761 if( res==Z_STREAM_END ){
6763 *pnOut = (int)str.total_out;
6766 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
6777 ** Return values of columns for the row at which the series_cursor
6778 ** is currently pointing.
6780 static int zipfileColumn(
6781 sqlite3_vtab_cursor *cur, /* The cursor */
6782 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
6783 int i /* Which column to return */
6785 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6786 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
6790 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
6793 /* TODO: Whether or not the following is correct surely depends on
6794 ** the platform on which the archive was created. */
6795 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
6797 case 2: { /* mtime */
6798 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
6802 if( sqlite3_vtab_nochange(ctx)==0 ){
6803 sqlite3_result_int64(ctx, pCDS->szUncompressed);
6807 case 4: /* rawdata */
6808 if( sqlite3_vtab_nochange(ctx) ) break;
6809 case 5: { /* data */
6810 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
6811 int sz = pCDS->szCompressed;
6812 int szFinal = pCDS->szUncompressed;
6816 if( pCsr->pCurrent->aData ){
6817 aBuf = pCsr->pCurrent->aData;
6819 aBuf = aFree = sqlite3_malloc64(sz);
6823 FILE *pFile = pCsr->pFile;
6825 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
6827 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
6828 &pCsr->base.pVtab->zErrMsg
6832 if( rc==SQLITE_OK ){
6833 if( i==5 && pCDS->iCompression ){
6834 zipfileInflate(ctx, aBuf, sz, szFinal);
6836 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
6839 sqlite3_free(aFree);
6841 /* Figure out if this is a directory or a zero-sized file. Consider
6842 ** it to be a directory either if the mode suggests so, or if
6843 ** the final character in the name is '/'. */
6844 u32 mode = pCDS->iExternalAttr >> 16;
6845 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
6846 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
6852 case 6: /* method */
6853 sqlite3_result_int(ctx, pCDS->iCompression);
6857 sqlite3_result_int64(ctx, pCsr->iId);
6865 ** Return TRUE if the cursor is at EOF.
6867 static int zipfileEof(sqlite3_vtab_cursor *cur){
6868 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
6873 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
6874 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
6875 ** is guaranteed to be a file-handle open on a zip file.
6877 ** This function attempts to locate the EOCD record within the zip archive
6878 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
6879 ** returned if successful. Otherwise, an SQLite error code is returned and
6880 ** an English language error message may be left in virtual-table pTab.
6882 static int zipfileReadEOCD(
6883 ZipfileTab *pTab, /* Return errors here */
6884 const u8 *aBlob, /* Pointer to in-memory file image */
6885 int nBlob, /* Size of aBlob[] in bytes */
6886 FILE *pFile, /* Read from this file if aBlob==0 */
6887 ZipfileEOCD *pEOCD /* Object to populate */
6889 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
6890 int nRead; /* Bytes to read from file */
6894 i64 iOff; /* Offset to read from */
6895 i64 szFile; /* Total size of file in bytes */
6896 fseek(pFile, 0, SEEK_END);
6897 szFile = (i64)ftell(pFile);
6899 memset(pEOCD, 0, sizeof(ZipfileEOCD));
6902 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
6903 iOff = szFile - nRead;
6904 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
6906 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
6907 aRead = (u8*)&aBlob[nBlob-nRead];
6910 if( rc==SQLITE_OK ){
6913 /* Scan backwards looking for the signature bytes */
6914 for(i=nRead-20; i>=0; i--){
6915 if( aRead[i]==0x50 && aRead[i+1]==0x4b
6916 && aRead[i+2]==0x05 && aRead[i+3]==0x06
6922 pTab->base.zErrMsg = sqlite3_mprintf(
6923 "cannot find end of central directory record"
6925 return SQLITE_ERROR;
6929 pEOCD->iDisk = zipfileRead16(aRead);
6930 pEOCD->iFirstDisk = zipfileRead16(aRead);
6931 pEOCD->nEntry = zipfileRead16(aRead);
6932 pEOCD->nEntryTotal = zipfileRead16(aRead);
6933 pEOCD->nSize = zipfileRead32(aRead);
6934 pEOCD->iOffset = zipfileRead32(aRead);
6941 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
6942 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
6943 ** to the end of the list. Otherwise, it is added to the list immediately
6944 ** before pBefore (which is guaranteed to be a part of said list).
6946 static void zipfileAddEntry(
6948 ZipfileEntry *pBefore,
6951 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
6952 assert( pNew->pNext==0 );
6954 if( pTab->pFirstEntry==0 ){
6955 pTab->pFirstEntry = pTab->pLastEntry = pNew;
6957 assert( pTab->pLastEntry->pNext==0 );
6958 pTab->pLastEntry->pNext = pNew;
6959 pTab->pLastEntry = pNew;
6963 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
6964 pNew->pNext = pBefore;
6969 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
6975 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
6976 iOff = eocd.iOffset;
6977 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
6978 ZipfileEntry *pNew = 0;
6979 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
6981 if( rc==SQLITE_OK ){
6982 zipfileAddEntry(pTab, 0, pNew);
6983 iOff += ZIPFILE_CDS_FIXED_SZ;
6984 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
6991 ** xFilter callback.
6993 static int zipfileFilter(
6994 sqlite3_vtab_cursor *cur,
6995 int idxNum, const char *idxStr,
6996 int argc, sqlite3_value **argv
6998 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
6999 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
7000 const char *zFile = 0; /* Zip file to scan */
7001 int rc = SQLITE_OK; /* Return Code */
7002 int bInMemory = 0; /* True for an in-memory zipfile */
7004 zipfileResetCursor(pCsr);
7007 zFile = pTab->zFile;
7008 }else if( idxNum==0 ){
7009 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
7010 return SQLITE_ERROR;
7011 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7012 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
7013 int nBlob = sqlite3_value_bytes(argv[0]);
7014 assert( pTab->pFirstEntry==0 );
7015 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
7016 pCsr->pFreeEntry = pTab->pFirstEntry;
7017 pTab->pFirstEntry = pTab->pLastEntry = 0;
7018 if( rc!=SQLITE_OK ) return rc;
7021 zFile = (const char*)sqlite3_value_text(argv[0]);
7024 if( 0==pTab->pWriteFd && 0==bInMemory ){
7025 pCsr->pFile = fopen(zFile, "rb");
7026 if( pCsr->pFile==0 ){
7027 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
7030 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
7031 if( rc==SQLITE_OK ){
7032 if( pCsr->eocd.nEntry==0 ){
7035 pCsr->iNextOff = pCsr->eocd.iOffset;
7036 rc = zipfileNext(cur);
7042 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
7043 rc = zipfileNext(cur);
7050 ** xBestIndex callback.
7052 static int zipfileBestIndex(
7054 sqlite3_index_info *pIdxInfo
7060 for(i=0; i<pIdxInfo->nConstraint; i++){
7061 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7062 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
7063 if( pCons->usable==0 ){
7065 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7069 pIdxInfo->estimatedCost = 1000.0;
7071 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
7072 pIdxInfo->aConstraintUsage[idx].omit = 1;
7073 pIdxInfo->idxNum = 1;
7074 }else if( unusable ){
7075 return SQLITE_CONSTRAINT;
7080 static ZipfileEntry *zipfileNewEntry(const char *zPath){
7082 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
7084 memset(pNew, 0, sizeof(ZipfileEntry));
7085 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
7086 if( pNew->cds.zFile==0 ){
7094 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
7095 ZipfileCDS *pCds = &pEntry->cds;
7100 /* Write the LFH itself */
7101 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
7102 zipfileWrite16(a, pCds->iVersionExtract);
7103 zipfileWrite16(a, pCds->flags);
7104 zipfileWrite16(a, pCds->iCompression);
7105 zipfileWrite16(a, pCds->mTime);
7106 zipfileWrite16(a, pCds->mDate);
7107 zipfileWrite32(a, pCds->crc32);
7108 zipfileWrite32(a, pCds->szCompressed);
7109 zipfileWrite32(a, pCds->szUncompressed);
7110 zipfileWrite16(a, (u16)pCds->nFile);
7111 zipfileWrite16(a, pCds->nExtra);
7112 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
7114 /* Add the file name */
7115 memcpy(a, pCds->zFile, (int)pCds->nFile);
7116 a += (int)pCds->nFile;
7118 /* The "extra" data */
7119 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7120 zipfileWrite16(a, 5);
7122 zipfileWrite32(a, pEntry->mUnixTime);
7127 static int zipfileAppendEntry(
7129 ZipfileEntry *pEntry,
7133 u8 *aBuf = pTab->aBuffer;
7137 nBuf = zipfileSerializeLFH(pEntry, aBuf);
7138 rc = zipfileAppendData(pTab, aBuf, nBuf);
7139 if( rc==SQLITE_OK ){
7140 pEntry->iDataOff = pTab->szCurrent;
7141 rc = zipfileAppendData(pTab, pData, nData);
7147 static int zipfileGetMode(
7148 sqlite3_value *pVal,
7149 int bIsDir, /* If true, default to directory */
7150 u32 *pMode, /* OUT: Mode value */
7151 char **pzErr /* OUT: Error message */
7153 const char *z = (const char*)sqlite3_value_text(pVal);
7156 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
7157 }else if( z[0]>='0' && z[0]<='9' ){
7158 mode = (unsigned int)sqlite3_value_int(pVal);
7160 const char zTemplate[11] = "-rwxrwxrwx";
7162 if( strlen(z)!=10 ) goto parse_error;
7164 case '-': mode |= S_IFREG; break;
7165 case 'd': mode |= S_IFDIR; break;
7166 case 'l': mode |= S_IFLNK; break;
7167 default: goto parse_error;
7169 for(i=1; i<10; i++){
7170 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
7171 else if( z[i]!='-' ) goto parse_error;
7174 if( ((mode & S_IFDIR)==0)==bIsDir ){
7175 /* The "mode" attribute is a directory, but data has been specified.
7176 ** Or vice-versa - no data but "mode" is a file or symlink. */
7177 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
7178 return SQLITE_CONSTRAINT;
7184 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
7185 return SQLITE_ERROR;
7189 ** Both (const char*) arguments point to nul-terminated strings. Argument
7190 ** nB is the value of strlen(zB). This function returns 0 if the strings are
7191 ** identical, ignoring any trailing '/' character in either path. */
7192 static int zipfileComparePath(const char *zA, const char *zB, int nB){
7193 int nA = (int)strlen(zA);
7194 if( nA>0 && zA[nA-1]=='/' ) nA--;
7195 if( nB>0 && zB[nB-1]=='/' ) nB--;
7196 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
7200 static int zipfileBegin(sqlite3_vtab *pVtab){
7201 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7204 assert( pTab->pWriteFd==0 );
7205 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
7206 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
7207 return SQLITE_ERROR;
7210 /* Open a write fd on the file. Also load the entire central directory
7211 ** structure into memory. During the transaction any new file data is
7212 ** appended to the archive file, but the central directory is accumulated
7213 ** in main-memory until the transaction is committed. */
7214 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
7215 if( pTab->pWriteFd==0 ){
7216 pTab->base.zErrMsg = sqlite3_mprintf(
7217 "zipfile: failed to open file %s for writing", pTab->zFile
7221 fseek(pTab->pWriteFd, 0, SEEK_END);
7222 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
7223 rc = zipfileLoadDirectory(pTab, 0, 0);
7226 if( rc!=SQLITE_OK ){
7227 zipfileCleanupTransaction(pTab);
7234 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
7237 static u32 zipfileTime(void){
7238 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
7240 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
7242 pVfs->xCurrentTimeInt64(pVfs, &ms);
7243 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
7246 pVfs->xCurrentTime(pVfs, &day);
7247 ret = (u32)((day - 2440587.5) * 86400);
7253 ** Return a 32-bit timestamp in UNIX epoch format.
7255 ** If the value passed as the only argument is either NULL or an SQL NULL,
7256 ** return the current time. Otherwise, return the value stored in (*pVal)
7257 ** cast to a 32-bit unsigned integer.
7259 static u32 zipfileGetTime(sqlite3_value *pVal){
7260 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
7261 return zipfileTime();
7263 return (u32)sqlite3_value_int64(pVal);
7267 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
7268 ** linked list. Remove it from the list and free the object.
7270 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
7273 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
7275 zipfileEntryFree(pOld);
7282 static int zipfileUpdate(
7283 sqlite3_vtab *pVtab,
7285 sqlite3_value **apVal,
7286 sqlite_int64 *pRowid
7288 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7289 int rc = SQLITE_OK; /* Return Code */
7290 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
7292 u32 mode = 0; /* Mode for new entry */
7293 u32 mTime = 0; /* Modification time for new entry */
7294 i64 sz = 0; /* Uncompressed size */
7295 const char *zPath = 0; /* Path for new entry */
7296 int nPath = 0; /* strlen(zPath) */
7297 const u8 *pData = 0; /* Pointer to buffer containing content */
7298 int nData = 0; /* Size of pData buffer in bytes */
7299 int iMethod = 0; /* Compression method for new entry */
7300 u8 *pFree = 0; /* Free this */
7301 char *zFree = 0; /* Also free this */
7302 ZipfileEntry *pOld = 0;
7303 ZipfileEntry *pOld2 = 0;
7304 int bUpdate = 0; /* True for an update that modifies "name" */
7308 if( pTab->pWriteFd==0 ){
7309 rc = zipfileBegin(pVtab);
7310 if( rc!=SQLITE_OK ) return rc;
7313 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
7314 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
7315 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
7316 int nDelete = (int)strlen(zDelete);
7318 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
7319 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
7323 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
7324 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
7327 assert( pOld->pNext );
7332 /* Check that "sz" and "rawdata" are both NULL: */
7333 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
7334 zipfileTableErr(pTab, "sz must be NULL");
7335 rc = SQLITE_CONSTRAINT;
7337 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
7338 zipfileTableErr(pTab, "rawdata must be NULL");
7339 rc = SQLITE_CONSTRAINT;
7342 if( rc==SQLITE_OK ){
7343 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
7344 /* data=NULL. A directory */
7347 /* Value specified for "data", and possibly "method". This must be
7348 ** a regular file or a symlink. */
7349 const u8 *aIn = sqlite3_value_blob(apVal[7]);
7350 int nIn = sqlite3_value_bytes(apVal[7]);
7351 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
7353 iMethod = sqlite3_value_int(apVal[8]);
7357 if( iMethod!=0 && iMethod!=8 ){
7358 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
7359 rc = SQLITE_CONSTRAINT;
7361 if( bAuto || iMethod ){
7363 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
7364 if( rc==SQLITE_OK ){
7365 if( iMethod || nCmp<nIn ){
7372 iCrc32 = crc32(0, aIn, nIn);
7377 if( rc==SQLITE_OK ){
7378 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
7381 if( rc==SQLITE_OK ){
7382 zPath = (const char*)sqlite3_value_text(apVal[2]);
7383 if( zPath==0 ) zPath = "";
7384 nPath = (int)strlen(zPath);
7385 mTime = zipfileGetTime(apVal[4]);
7388 if( rc==SQLITE_OK && bIsDir ){
7389 /* For a directory, check that the last character in the path is a
7390 ** '/'. This appears to be required for compatibility with info-zip
7391 ** (the unzip command on unix). It does not create directories
7393 if( nPath<=0 || zPath[nPath-1]!='/' ){
7394 zFree = sqlite3_mprintf("%s/", zPath);
7395 zPath = (const char*)zFree;
7400 nPath = (int)strlen(zPath);
7405 /* Check that we're not inserting a duplicate entry -OR- updating an
7406 ** entry with a path, thereby making it into a duplicate. */
7407 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
7409 for(p=pTab->pFirstEntry; p; p=p->pNext){
7410 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
7411 switch( sqlite3_vtab_on_conflict(pTab->db) ){
7412 case SQLITE_IGNORE: {
7413 goto zipfile_update_done;
7415 case SQLITE_REPLACE: {
7420 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
7421 rc = SQLITE_CONSTRAINT;
7430 if( rc==SQLITE_OK ){
7431 /* Create the new CDS record. */
7432 pNew = zipfileNewEntry(zPath);
7436 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7437 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7438 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7439 pNew->cds.iCompression = (u16)iMethod;
7440 zipfileMtimeToDos(&pNew->cds, mTime);
7441 pNew->cds.crc32 = iCrc32;
7442 pNew->cds.szCompressed = nData;
7443 pNew->cds.szUncompressed = (u32)sz;
7444 pNew->cds.iExternalAttr = (mode<<16);
7445 pNew->cds.iOffset = (u32)pTab->szCurrent;
7446 pNew->cds.nFile = (u16)nPath;
7447 pNew->mUnixTime = (u32)mTime;
7448 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
7449 zipfileAddEntry(pTab, pOld, pNew);
7454 if( rc==SQLITE_OK && (pOld || pOld2) ){
7456 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7457 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
7458 pCsr->pCurrent = pCsr->pCurrent->pNext;
7463 zipfileRemoveEntryFromList(pTab, pOld);
7464 zipfileRemoveEntryFromList(pTab, pOld2);
7467 zipfile_update_done:
7468 sqlite3_free(pFree);
7469 sqlite3_free(zFree);
7473 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
7475 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
7476 zipfileWrite16(a, p->iDisk);
7477 zipfileWrite16(a, p->iFirstDisk);
7478 zipfileWrite16(a, p->nEntry);
7479 zipfileWrite16(a, p->nEntryTotal);
7480 zipfileWrite32(a, p->nSize);
7481 zipfileWrite32(a, p->iOffset);
7482 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
7487 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
7488 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
7489 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
7490 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
7494 ** Serialize the CDS structure into buffer aBuf[]. Return the number
7495 ** of bytes written.
7497 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
7499 ZipfileCDS *pCDS = &pEntry->cds;
7501 if( pEntry->aExtra==0 ){
7505 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
7506 zipfileWrite16(a, pCDS->iVersionMadeBy);
7507 zipfileWrite16(a, pCDS->iVersionExtract);
7508 zipfileWrite16(a, pCDS->flags);
7509 zipfileWrite16(a, pCDS->iCompression);
7510 zipfileWrite16(a, pCDS->mTime);
7511 zipfileWrite16(a, pCDS->mDate);
7512 zipfileWrite32(a, pCDS->crc32);
7513 zipfileWrite32(a, pCDS->szCompressed);
7514 zipfileWrite32(a, pCDS->szUncompressed);
7515 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
7516 zipfileWrite16(a, pCDS->nFile);
7517 zipfileWrite16(a, pCDS->nExtra);
7518 zipfileWrite16(a, pCDS->nComment);
7519 zipfileWrite16(a, pCDS->iDiskStart);
7520 zipfileWrite16(a, pCDS->iInternalAttr);
7521 zipfileWrite32(a, pCDS->iExternalAttr);
7522 zipfileWrite32(a, pCDS->iOffset);
7524 memcpy(a, pCDS->zFile, pCDS->nFile);
7527 if( pEntry->aExtra ){
7528 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
7529 memcpy(a, pEntry->aExtra, n);
7532 assert( pCDS->nExtra==9 );
7533 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
7534 zipfileWrite16(a, 5);
7536 zipfileWrite32(a, pEntry->mUnixTime);
7542 static int zipfileCommit(sqlite3_vtab *pVtab){
7543 ZipfileTab *pTab = (ZipfileTab*)pVtab;
7545 if( pTab->pWriteFd ){
7546 i64 iOffset = pTab->szCurrent;
7551 /* Write out all entries */
7552 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
7553 int n = zipfileSerializeCDS(p, pTab->aBuffer);
7554 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
7558 /* Write out the EOCD record */
7560 eocd.iFirstDisk = 0;
7561 eocd.nEntry = (u16)nEntry;
7562 eocd.nEntryTotal = (u16)nEntry;
7563 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
7564 eocd.iOffset = (u32)iOffset;
7565 rc = zipfileAppendEOCD(pTab, &eocd);
7567 zipfileCleanupTransaction(pTab);
7572 static int zipfileRollback(sqlite3_vtab *pVtab){
7573 return zipfileCommit(pVtab);
7576 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
7578 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
7579 if( iId==pCsr->iId ) break;
7584 static void zipfileFunctionCds(
7585 sqlite3_context *context,
7587 sqlite3_value **argv
7590 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
7593 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
7595 ZipfileCDS *p = &pCsr->pCurrent->cds;
7596 char *zRes = sqlite3_mprintf("{"
7597 "\"version-made-by\" : %u, "
7598 "\"version-to-extract\" : %u, "
7600 "\"compression\" : %u, "
7604 "\"compressed-size\" : %u, "
7605 "\"uncompressed-size\" : %u, "
7606 "\"file-name-length\" : %u, "
7607 "\"extra-field-length\" : %u, "
7608 "\"file-comment-length\" : %u, "
7609 "\"disk-number-start\" : %u, "
7610 "\"internal-attr\" : %u, "
7611 "\"external-attr\" : %u, "
7612 "\"offset\" : %u }",
7613 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
7614 (u32)p->flags, (u32)p->iCompression,
7615 (u32)p->mTime, (u32)p->mDate,
7616 (u32)p->crc32, (u32)p->szCompressed,
7617 (u32)p->szUncompressed, (u32)p->nFile,
7618 (u32)p->nExtra, (u32)p->nComment,
7619 (u32)p->iDiskStart, (u32)p->iInternalAttr,
7620 (u32)p->iExternalAttr, (u32)p->iOffset
7624 sqlite3_result_error_nomem(context);
7626 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
7633 ** xFindFunction method.
7635 static int zipfileFindFunction(
7636 sqlite3_vtab *pVtab, /* Virtual table handle */
7637 int nArg, /* Number of SQL function arguments */
7638 const char *zName, /* Name of SQL function */
7639 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
7640 void **ppArg /* OUT: User data for *pxFunc */
7642 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
7643 *pxFunc = zipfileFunctionCds;
7644 *ppArg = (void*)pVtab;
7650 typedef struct ZipfileBuffer ZipfileBuffer;
7651 struct ZipfileBuffer {
7652 u8 *a; /* Pointer to buffer */
7653 int n; /* Size of buffer in bytes */
7654 int nAlloc; /* Byte allocated at a[] */
7657 typedef struct ZipfileCtx ZipfileCtx;
7664 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
7665 if( pBuf->n+nByte>pBuf->nAlloc ){
7667 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
7668 int nReq = pBuf->n + nByte;
7670 while( nNew<nReq ) nNew = nNew*2;
7671 aNew = sqlite3_realloc64(pBuf->a, nNew);
7672 if( aNew==0 ) return SQLITE_NOMEM;
7674 pBuf->nAlloc = (int)nNew;
7680 ** xStep() callback for the zipfile() aggregate. This can be called in
7681 ** any of the following ways:
7683 ** SELECT zipfile(name,data) ...
7684 ** SELECT zipfile(name,mode,mtime,data) ...
7685 ** SELECT zipfile(name,mode,mtime,data,method) ...
7687 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
7688 ZipfileCtx *p; /* Aggregate function context */
7689 ZipfileEntry e; /* New entry to add to zip archive */
7691 sqlite3_value *pName = 0;
7692 sqlite3_value *pMode = 0;
7693 sqlite3_value *pMtime = 0;
7694 sqlite3_value *pData = 0;
7695 sqlite3_value *pMethod = 0;
7702 int iMethod = -1; /* Compression method to use (0 or 8) */
7704 const u8 *aData = 0; /* Possibly compressed data for new entry */
7705 int nData = 0; /* Size of aData[] in bytes */
7706 int szUncompressed = 0; /* Size of data before compression */
7707 u8 *aFree = 0; /* Free this before returning */
7708 u32 iCrc32 = 0; /* crc32 of uncompressed data */
7710 char *zName = 0; /* Path (name) of new entry */
7711 int nName = 0; /* Size of zName in bytes */
7712 char *zFree = 0; /* Free this before returning */
7715 memset(&e, 0, sizeof(e));
7716 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7719 /* Martial the arguments into stack variables */
7720 if( nVal!=2 && nVal!=4 && nVal!=5 ){
7721 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
7723 goto zipfile_step_out;
7737 /* Check that the 'name' parameter looks ok. */
7738 zName = (char*)sqlite3_value_text(pName);
7739 nName = sqlite3_value_bytes(pName);
7741 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
7743 goto zipfile_step_out;
7746 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
7747 ** deflate compression) or NULL (choose automatically). */
7748 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
7749 iMethod = (int)sqlite3_value_int64(pMethod);
7750 if( iMethod!=0 && iMethod!=8 ){
7751 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
7753 goto zipfile_step_out;
7757 /* Now inspect the data. If this is NULL, then the new entry must be a
7758 ** directory. Otherwise, figure out whether or not the data should
7759 ** be deflated or simply stored in the zip archive. */
7760 if( sqlite3_value_type(pData)==SQLITE_NULL ){
7764 aData = sqlite3_value_blob(pData);
7765 szUncompressed = nData = sqlite3_value_bytes(pData);
7766 iCrc32 = crc32(0, aData, nData);
7767 if( iMethod<0 || iMethod==8 ){
7769 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
7770 if( rc!=SQLITE_OK ){
7771 goto zipfile_step_out;
7773 if( iMethod==8 || nOut<nData ){
7783 /* Decode the "mode" argument. */
7784 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
7785 if( rc ) goto zipfile_step_out;
7787 /* Decode the "mtime" argument. */
7788 e.mUnixTime = zipfileGetTime(pMtime);
7790 /* If this is a directory entry, ensure that there is exactly one '/'
7791 ** at the end of the path. Or, if this is not a directory and the path
7792 ** ends in '/' it is an error. */
7794 if( nName>0 && zName[nName-1]=='/' ){
7795 zErr = sqlite3_mprintf("non-directory name must not end with /");
7797 goto zipfile_step_out;
7800 if( nName==0 || zName[nName-1]!='/' ){
7801 zName = zFree = sqlite3_mprintf("%s/", zName);
7804 goto zipfile_step_out;
7806 nName = (int)strlen(zName);
7808 while( nName>1 && zName[nName-2]=='/' ) nName--;
7812 /* Assemble the ZipfileEntry object for the new zip archive entry */
7813 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
7814 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
7815 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
7816 e.cds.iCompression = (u16)iMethod;
7817 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
7818 e.cds.crc32 = iCrc32;
7819 e.cds.szCompressed = nData;
7820 e.cds.szUncompressed = szUncompressed;
7821 e.cds.iExternalAttr = (mode<<16);
7822 e.cds.iOffset = p->body.n;
7823 e.cds.nFile = (u16)nName;
7824 e.cds.zFile = zName;
7826 /* Append the LFH to the body of the new archive */
7827 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
7828 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
7829 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
7831 /* Append the data to the body of the new archive */
7833 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
7834 memcpy(&p->body.a[p->body.n], aData, nData);
7838 /* Append the CDS record to the directory of the new archive */
7839 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
7840 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
7841 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
7843 /* Increment the count of entries in the archive */
7847 sqlite3_free(aFree);
7848 sqlite3_free(zFree);
7851 sqlite3_result_error(pCtx, zErr, -1);
7853 sqlite3_result_error_code(pCtx, rc);
7860 ** xFinalize() callback for zipfile aggregate function.
7862 void zipfileFinal(sqlite3_context *pCtx){
7868 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
7871 memset(&eocd, 0, sizeof(eocd));
7872 eocd.nEntry = (u16)p->nEntry;
7873 eocd.nEntryTotal = (u16)p->nEntry;
7874 eocd.nSize = p->cds.n;
7875 eocd.iOffset = p->body.n;
7877 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
7878 aZip = (u8*)sqlite3_malloc64(nZip);
7880 sqlite3_result_error_nomem(pCtx);
7882 memcpy(aZip, p->body.a, p->body.n);
7883 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
7884 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
7885 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
7889 sqlite3_free(p->body.a);
7890 sqlite3_free(p->cds.a);
7895 ** Register the "zipfile" virtual table.
7897 static int zipfileRegister(sqlite3 *db){
7898 static sqlite3_module zipfileModule = {
7900 zipfileConnect, /* xCreate */
7901 zipfileConnect, /* xConnect */
7902 zipfileBestIndex, /* xBestIndex */
7903 zipfileDisconnect, /* xDisconnect */
7904 zipfileDisconnect, /* xDestroy */
7905 zipfileOpen, /* xOpen - open a cursor */
7906 zipfileClose, /* xClose - close a cursor */
7907 zipfileFilter, /* xFilter - configure scan constraints */
7908 zipfileNext, /* xNext - advance a cursor */
7909 zipfileEof, /* xEof - check for end of scan */
7910 zipfileColumn, /* xColumn - read data */
7911 0, /* xRowid - read data */
7912 zipfileUpdate, /* xUpdate */
7913 zipfileBegin, /* xBegin */
7915 zipfileCommit, /* xCommit */
7916 zipfileRollback, /* xRollback */
7917 zipfileFindFunction, /* xFindMethod */
7921 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
7922 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
7923 if( rc==SQLITE_OK ){
7924 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
7925 zipfileStep, zipfileFinal
7930 #else /* SQLITE_OMIT_VIRTUALTABLE */
7931 # define zipfileRegister(x) SQLITE_OK
7937 int sqlite3_zipfile_init(
7940 const sqlite3_api_routines *pApi
7942 SQLITE_EXTENSION_INIT2(pApi);
7943 (void)pzErrMsg; /* Unused parameter */
7944 return zipfileRegister(db);
7947 /************************* End ../ext/misc/zipfile.c ********************/
7948 /************************* Begin ../ext/misc/sqlar.c ******************/
7952 ** The author disclaims copyright to this source code. In place of
7953 ** a legal notice, here is a blessing:
7955 ** May you do good and not evil.
7956 ** May you find forgiveness for yourself and forgive others.
7957 ** May you share freely, never taking more than you give.
7959 ******************************************************************************
7961 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
7962 ** for working with sqlar archives and used by the shell tool's built-in
7965 /* #include "sqlite3ext.h" */
7966 SQLITE_EXTENSION_INIT1
7971 ** Implementation of the "sqlar_compress(X)" SQL function.
7973 ** If the type of X is SQLITE_BLOB, and compressing that blob using
7974 ** zlib utility function compress() yields a smaller blob, return the
7975 ** compressed blob. Otherwise, return a copy of X.
7977 ** SQLar uses the "zlib format" for compressed content. The zlib format
7978 ** contains a two-byte identification header and a four-byte checksum at
7979 ** the end. This is different from ZIP which uses the raw deflate format.
7981 ** Future enhancements to SQLar might add support for new compression formats.
7982 ** If so, those new formats will be identified by alternative headers in the
7985 static void sqlarCompressFunc(
7986 sqlite3_context *context,
7988 sqlite3_value **argv
7991 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
7992 const Bytef *pData = sqlite3_value_blob(argv[0]);
7993 uLong nData = sqlite3_value_bytes(argv[0]);
7994 uLongf nOut = compressBound(nData);
7997 pOut = (Bytef*)sqlite3_malloc(nOut);
7999 sqlite3_result_error_nomem(context);
8002 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
8003 sqlite3_result_error(context, "error in compress()", -1);
8004 }else if( nOut<nData ){
8005 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
8007 sqlite3_result_value(context, argv[0]);
8012 sqlite3_result_value(context, argv[0]);
8017 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
8019 ** Parameter SZ is interpreted as an integer. If it is less than or
8020 ** equal to zero, then this function returns a copy of X. Or, if
8021 ** SZ is equal to the size of X when interpreted as a blob, also
8022 ** return a copy of X. Otherwise, decompress blob X using zlib
8023 ** utility function uncompress() and return the results (another
8026 static void sqlarUncompressFunc(
8027 sqlite3_context *context,
8029 sqlite3_value **argv
8035 sz = sqlite3_value_int(argv[1]);
8037 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
8038 sqlite3_result_value(context, argv[0]);
8040 const Bytef *pData= sqlite3_value_blob(argv[0]);
8041 Bytef *pOut = sqlite3_malloc(sz);
8042 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
8043 sqlite3_result_error(context, "error in uncompress()", -1);
8045 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
8055 int sqlite3_sqlar_init(
8058 const sqlite3_api_routines *pApi
8061 SQLITE_EXTENSION_INIT2(pApi);
8062 (void)pzErrMsg; /* Unused parameter */
8063 rc = sqlite3_create_function(db, "sqlar_compress", 1,
8064 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8065 sqlarCompressFunc, 0, 0);
8066 if( rc==SQLITE_OK ){
8067 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
8068 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
8069 sqlarUncompressFunc, 0, 0);
8074 /************************* End ../ext/misc/sqlar.c ********************/
8076 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
8080 ** The author disclaims copyright to this source code. In place of
8081 ** a legal notice, here is a blessing:
8083 ** May you do good and not evil.
8084 ** May you find forgiveness for yourself and forgive others.
8085 ** May you share freely, never taking more than you give.
8087 *************************************************************************
8089 #if !defined(SQLITEEXPERT_H)
8090 #define SQLITEEXPERT_H 1
8091 /* #include "sqlite3.h" */
8093 typedef struct sqlite3expert sqlite3expert;
8096 ** Create a new sqlite3expert object.
8098 ** If successful, a pointer to the new object is returned and (*pzErr) set
8099 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
8100 ** an English-language error message. In this case it is the responsibility
8101 ** of the caller to eventually free the error message buffer using
8104 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
8107 ** Configure an sqlite3expert object.
8109 ** EXPERT_CONFIG_SAMPLE:
8110 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
8111 ** each candidate index. This involves scanning and sorting the entire
8112 ** contents of each user database table once for each candidate index
8113 ** associated with the table. For large databases, this can be
8114 ** prohibitively slow. This option allows the sqlite3expert object to
8115 ** be configured so that sqlite_stat1 data is instead generated based on a
8116 ** subset of each table, or so that no sqlite_stat1 data is used at all.
8118 ** A single integer argument is passed to this option. If the value is less
8119 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
8120 ** the analysis - indexes are recommended based on the database schema only.
8121 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
8122 ** generated for each candidate index (this is the default). Finally, if the
8123 ** value falls between 0 and 100, then it represents the percentage of user
8124 ** table rows that should be considered when generating sqlite_stat1 data.
8128 ** // Do not generate any sqlite_stat1 data
8129 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
8131 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
8132 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
8134 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
8136 #define EXPERT_CONFIG_SAMPLE 1 /* int */
8139 ** Specify zero or more SQL statements to be included in the analysis.
8141 ** Buffer zSql must contain zero or more complete SQL statements. This
8142 ** function parses all statements contained in the buffer and adds them
8143 ** to the internal list of statements to analyze. If successful, SQLITE_OK
8144 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
8145 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
8146 ** may be set to point to an English language error message. In this case
8147 ** the caller is responsible for eventually freeing the error message buffer
8148 ** using sqlite3_free().
8150 ** If an error does occur while processing one of the statements in the
8151 ** buffer passed as the second argument, none of the statements in the
8152 ** buffer are added to the analysis.
8154 ** This function must be called before sqlite3_expert_analyze(). If a call
8155 ** to this function is made on an sqlite3expert object that has already
8156 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
8157 ** immediately and no statements are added to the analysis.
8159 int sqlite3_expert_sql(
8160 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
8161 const char *zSql, /* SQL statement(s) to add */
8162 char **pzErr /* OUT: Error message (if any) */
8167 ** This function is called after the sqlite3expert object has been configured
8168 ** with all SQL statements using sqlite3_expert_sql() to actually perform
8169 ** the analysis. Once this function has been called, it is not possible to
8170 ** add further SQL statements to the analysis.
8172 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
8173 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
8174 ** point to a buffer containing an English language error message. In this
8175 ** case it is the responsibility of the caller to eventually free the buffer
8176 ** using sqlite3_free().
8178 ** If an error does occur within this function, the sqlite3expert object
8179 ** is no longer useful for any purpose. At that point it is no longer
8180 ** possible to add further SQL statements to the object or to re-attempt
8181 ** the analysis. The sqlite3expert object must still be freed using a call
8182 ** sqlite3_expert_destroy().
8184 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
8187 ** Return the total number of statements loaded using sqlite3_expert_sql().
8188 ** The total number of SQL statements may be different from the total number
8189 ** to calls to sqlite3_expert_sql().
8191 int sqlite3_expert_count(sqlite3expert*);
8194 ** Return a component of the report.
8196 ** This function is called after sqlite3_expert_analyze() to extract the
8197 ** results of the analysis. Each call to this function returns either a
8198 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
8199 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
8200 ** #define constants defined below.
8202 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
8203 ** information relating to a specific SQL statement. In these cases that
8204 ** SQL statement is identified by the value passed as the second argument.
8205 ** SQL statements are numbered from 0 in the order in which they are parsed.
8206 ** If an out-of-range value (less than zero or equal to or greater than the
8207 ** value returned by sqlite3_expert_count()) is passed as the second argument
8208 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
8210 ** EXPERT_REPORT_SQL:
8211 ** Return the text of SQL statement iStmt.
8213 ** EXPERT_REPORT_INDEXES:
8214 ** Return a buffer containing the CREATE INDEX statements for all recommended
8215 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
8218 ** EXPERT_REPORT_PLAN:
8219 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
8220 ** iStmt after the proposed indexes have been added to the database schema.
8222 ** EXPERT_REPORT_CANDIDATES:
8223 ** Return a pointer to a buffer containing the CREATE INDEX statements
8224 ** for all indexes that were tested (for all SQL statements). The iStmt
8225 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
8227 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
8230 ** Values for the third argument passed to sqlite3_expert_report().
8232 #define EXPERT_REPORT_SQL 1
8233 #define EXPERT_REPORT_INDEXES 2
8234 #define EXPERT_REPORT_PLAN 3
8235 #define EXPERT_REPORT_CANDIDATES 4
8238 ** Free an (sqlite3expert*) handle and all associated resources. There
8239 ** should be one call to this function for each successful call to
8240 ** sqlite3-expert_new().
8242 void sqlite3_expert_destroy(sqlite3expert*);
8244 #endif /* !defined(SQLITEEXPERT_H) */
8246 /************************* End ../ext/expert/sqlite3expert.h ********************/
8247 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
8251 ** The author disclaims copyright to this source code. In place of
8252 ** a legal notice, here is a blessing:
8254 ** May you do good and not evil.
8255 ** May you find forgiveness for yourself and forgive others.
8256 ** May you share freely, never taking more than you give.
8258 *************************************************************************
8260 /* #include "sqlite3expert.h" */
8265 #ifndef SQLITE_OMIT_VIRTUALTABLE
8267 /* typedef sqlite3_int64 i64; */
8268 /* typedef sqlite3_uint64 u64; */
8270 typedef struct IdxColumn IdxColumn;
8271 typedef struct IdxConstraint IdxConstraint;
8272 typedef struct IdxScan IdxScan;
8273 typedef struct IdxStatement IdxStatement;
8274 typedef struct IdxTable IdxTable;
8275 typedef struct IdxWrite IdxWrite;
8277 #define STRLEN (int)strlen
8280 ** A temp table name that we assume no user database will actually use.
8281 ** If this assumption proves incorrect triggers on the table with the
8282 ** conflicting name will be ignored.
8284 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
8287 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
8288 ** any other type of single-ended range constraint on a column).
8291 ** Used to temporarily link IdxConstraint objects into lists while
8292 ** creating candidate indexes.
8294 struct IdxConstraint {
8295 char *zColl; /* Collation sequence */
8296 int bRange; /* True for range, false for eq */
8297 int iCol; /* Constrained table column */
8298 int bFlag; /* Used by idxFindCompatible() */
8299 int bDesc; /* True if ORDER BY <expr> DESC */
8300 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
8301 IdxConstraint *pLink; /* See above */
8305 ** A single scan of a single table.
8308 IdxTable *pTab; /* Associated table object */
8309 int iDb; /* Database containing table zTable */
8310 i64 covering; /* Mask of columns required for cov. index */
8311 IdxConstraint *pOrder; /* ORDER BY columns */
8312 IdxConstraint *pEq; /* List of == constraints */
8313 IdxConstraint *pRange; /* List of < constraints */
8314 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
8318 ** Information regarding a single database table. Extracted from
8319 ** "PRAGMA table_info" by function idxGetTableInfo().
8328 char *zName; /* Table name */
8330 IdxTable *pNext; /* Next table in linked list of all tables */
8334 ** An object of the following type is created for each unique table/write-op
8335 ** seen. The objects are stored in a singly-linked list beginning at
8336 ** sqlite3expert.pWrite.
8340 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
8345 ** Each statement being analyzed is represented by an instance of this
8348 struct IdxStatement {
8349 int iId; /* Statement number */
8350 char *zSql; /* SQL statement */
8351 char *zIdx; /* Indexes */
8352 char *zEQP; /* Plan */
8353 IdxStatement *pNext;
8358 ** A hash table for storing strings. With space for a payload string
8359 ** with each entry. Methods are:
8366 #define IDX_HASH_SIZE 1023
8367 typedef struct IdxHashEntry IdxHashEntry;
8368 typedef struct IdxHash IdxHash;
8369 struct IdxHashEntry {
8370 char *zKey; /* nul-terminated key */
8371 char *zVal; /* nul-terminated value string */
8372 char *zVal2; /* nul-terminated value string 2 */
8373 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
8374 IdxHashEntry *pNext; /* Next entry in hash */
8377 IdxHashEntry *pFirst;
8378 IdxHashEntry *aHash[IDX_HASH_SIZE];
8382 ** sqlite3expert object.
8384 struct sqlite3expert {
8385 int iSample; /* Percentage of tables to sample for stat1 */
8386 sqlite3 *db; /* User database */
8387 sqlite3 *dbm; /* In-memory db for this analysis */
8388 sqlite3 *dbv; /* Vtab schema for this analysis */
8389 IdxTable *pTable; /* List of all IdxTable objects */
8390 IdxScan *pScan; /* List of scan objects */
8391 IdxWrite *pWrite; /* List of write objects */
8392 IdxStatement *pStatement; /* List of IdxStatement objects */
8393 int bRun; /* True once analysis has run */
8395 int rc; /* Error code from whereinfo hook */
8396 IdxHash hIdx; /* Hash containing all candidate indexes */
8397 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
8402 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
8403 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
8405 static void *idxMalloc(int *pRc, int nByte){
8407 assert( *pRc==SQLITE_OK );
8409 pRet = sqlite3_malloc(nByte);
8411 memset(pRet, 0, nByte);
8413 *pRc = SQLITE_NOMEM;
8419 ** Initialize an IdxHash hash table.
8421 static void idxHashInit(IdxHash *pHash){
8422 memset(pHash, 0, sizeof(IdxHash));
8426 ** Reset an IdxHash hash table.
8428 static void idxHashClear(IdxHash *pHash){
8430 for(i=0; i<IDX_HASH_SIZE; i++){
8431 IdxHashEntry *pEntry;
8432 IdxHashEntry *pNext;
8433 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
8434 pNext = pEntry->pHashNext;
8435 sqlite3_free(pEntry->zVal2);
8436 sqlite3_free(pEntry);
8439 memset(pHash, 0, sizeof(IdxHash));
8443 ** Return the index of the hash bucket that the string specified by the
8444 ** arguments to this function belongs.
8446 static int idxHashString(const char *z, int n){
8447 unsigned int ret = 0;
8450 ret += (ret<<3) + (unsigned char)(z[i]);
8452 return (int)(ret % IDX_HASH_SIZE);
8456 ** If zKey is already present in the hash table, return non-zero and do
8457 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
8458 ** the hash table passed as the second argument.
8460 static int idxHashAdd(
8466 int nKey = STRLEN(zKey);
8467 int iHash = idxHashString(zKey, nKey);
8468 int nVal = (zVal ? STRLEN(zVal) : 0);
8469 IdxHashEntry *pEntry;
8471 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8472 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8476 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
8478 pEntry->zKey = (char*)&pEntry[1];
8479 memcpy(pEntry->zKey, zKey, nKey);
8481 pEntry->zVal = &pEntry->zKey[nKey+1];
8482 memcpy(pEntry->zVal, zVal, nVal);
8484 pEntry->pHashNext = pHash->aHash[iHash];
8485 pHash->aHash[iHash] = pEntry;
8487 pEntry->pNext = pHash->pFirst;
8488 pHash->pFirst = pEntry;
8494 ** If zKey/nKey is present in the hash table, return a pointer to the
8495 ** hash-entry object.
8497 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
8499 IdxHashEntry *pEntry;
8500 if( nKey<0 ) nKey = STRLEN(zKey);
8501 iHash = idxHashString(zKey, nKey);
8503 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
8504 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
8512 ** If the hash table contains an entry with a key equal to the string
8513 ** passed as the final two arguments to this function, return a pointer
8514 ** to the payload string. Otherwise, if zKey/nKey is not present in the
8515 ** hash table, return NULL.
8517 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
8518 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
8519 if( pEntry ) return pEntry->zVal;
8524 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
8525 ** variable to point to a copy of nul-terminated string zColl.
8527 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
8528 IdxConstraint *pNew;
8529 int nColl = STRLEN(zColl);
8531 assert( *pRc==SQLITE_OK );
8532 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
8534 pNew->zColl = (char*)&pNew[1];
8535 memcpy(pNew->zColl, zColl, nColl+1);
8541 ** An error associated with database handle db has just occurred. Pass
8542 ** the error message to callback function xOut.
8544 static void idxDatabaseError(
8545 sqlite3 *db, /* Database handle */
8546 char **pzErrmsg /* Write error here */
8548 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
8552 ** Prepare an SQL statement.
8554 static int idxPrepareStmt(
8555 sqlite3 *db, /* Database handle to compile against */
8556 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8557 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8558 const char *zSql /* SQL statement to compile */
8560 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
8561 if( rc!=SQLITE_OK ){
8563 idxDatabaseError(db, pzErrmsg);
8569 ** Prepare an SQL statement using the results of a printf() formatting.
8571 static int idxPrintfPrepareStmt(
8572 sqlite3 *db, /* Database handle to compile against */
8573 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
8574 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
8575 const char *zFmt, /* printf() format of SQL statement */
8576 ... /* Trailing printf() arguments */
8582 zSql = sqlite3_vmprintf(zFmt, ap);
8586 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
8594 /*************************************************************************
8595 ** Beginning of virtual table implementation.
8597 typedef struct ExpertVtab ExpertVtab;
8601 sqlite3expert *pExpert;
8604 typedef struct ExpertCsr ExpertCsr;
8606 sqlite3_vtab_cursor base;
8607 sqlite3_stmt *pData;
8610 static char *expertDequote(const char *zIn){
8611 int n = STRLEN(zIn);
8612 char *zRet = sqlite3_malloc(n);
8614 assert( zIn[0]=='\'' );
8615 assert( zIn[n-1]=='\'' );
8620 for(iIn=1; iIn<(n-1); iIn++){
8621 if( zIn[iIn]=='\'' ){
8622 assert( zIn[iIn+1]=='\'' );
8625 zRet[iOut++] = zIn[iIn];
8634 ** This function is the implementation of both the xConnect and xCreate
8635 ** methods of the r-tree virtual table.
8637 ** argv[0] -> module name
8638 ** argv[1] -> database name
8639 ** argv[2] -> table name
8640 ** argv[...] -> column names...
8642 static int expertConnect(
8645 int argc, const char *const*argv,
8646 sqlite3_vtab **ppVtab,
8649 sqlite3expert *pExpert = (sqlite3expert*)pAux;
8654 *pzErr = sqlite3_mprintf("internal error!");
8657 char *zCreateTable = expertDequote(argv[3]);
8659 rc = sqlite3_declare_vtab(db, zCreateTable);
8660 if( rc==SQLITE_OK ){
8661 p = idxMalloc(&rc, sizeof(ExpertVtab));
8663 if( rc==SQLITE_OK ){
8664 p->pExpert = pExpert;
8665 p->pTab = pExpert->pTable;
8666 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
8668 sqlite3_free(zCreateTable);
8674 *ppVtab = (sqlite3_vtab*)p;
8678 static int expertDisconnect(sqlite3_vtab *pVtab){
8679 ExpertVtab *p = (ExpertVtab*)pVtab;
8684 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
8685 ExpertVtab *p = (ExpertVtab*)pVtab;
8690 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
8691 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
8692 SQLITE_INDEX_CONSTRAINT_LE;
8694 pScan = idxMalloc(&rc, sizeof(IdxScan));
8698 /* Link the new scan object into the list */
8699 pScan->pTab = p->pTab;
8700 pScan->pNextScan = p->pExpert->pScan;
8701 p->pExpert->pScan = pScan;
8703 /* Add the constraints to the IdxScan object */
8704 for(i=0; i<pIdxInfo->nConstraint; i++){
8705 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
8707 && pCons->iColumn>=0
8708 && p->pTab->aCol[pCons->iColumn].iPk==0
8709 && (pCons->op & opmask)
8711 IdxConstraint *pNew;
8712 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
8713 pNew = idxNewConstraint(&rc, zColl);
8715 pNew->iCol = pCons->iColumn;
8716 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
8717 pNew->pNext = pScan->pEq;
8721 pNew->pNext = pScan->pRange;
8722 pScan->pRange = pNew;
8726 pIdxInfo->aConstraintUsage[i].argvIndex = n;
8730 /* Add the ORDER BY to the IdxScan object */
8731 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
8732 int iCol = pIdxInfo->aOrderBy[i].iColumn;
8734 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
8737 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
8738 pNew->pNext = pScan->pOrder;
8739 pNew->pLink = pScan->pOrder;
8740 pScan->pOrder = pNew;
8747 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
8751 static int expertUpdate(
8752 sqlite3_vtab *pVtab,
8754 sqlite3_value **azData,
8755 sqlite_int64 *pRowid
8765 ** Virtual table module xOpen method.
8767 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
8771 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
8772 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
8777 ** Virtual table module xClose method.
8779 static int expertClose(sqlite3_vtab_cursor *cur){
8780 ExpertCsr *pCsr = (ExpertCsr*)cur;
8781 sqlite3_finalize(pCsr->pData);
8787 ** Virtual table module xEof method.
8789 ** Return non-zero if the cursor does not currently point to a valid
8790 ** record (i.e if the scan has finished), or zero otherwise.
8792 static int expertEof(sqlite3_vtab_cursor *cur){
8793 ExpertCsr *pCsr = (ExpertCsr*)cur;
8794 return pCsr->pData==0;
8798 ** Virtual table module xNext method.
8800 static int expertNext(sqlite3_vtab_cursor *cur){
8801 ExpertCsr *pCsr = (ExpertCsr*)cur;
8804 assert( pCsr->pData );
8805 rc = sqlite3_step(pCsr->pData);
8806 if( rc!=SQLITE_ROW ){
8807 rc = sqlite3_finalize(pCsr->pData);
8817 ** Virtual table module xRowid method.
8819 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
8826 ** Virtual table module xColumn method.
8828 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
8829 ExpertCsr *pCsr = (ExpertCsr*)cur;
8830 sqlite3_value *pVal;
8831 pVal = sqlite3_column_value(pCsr->pData, i);
8833 sqlite3_result_value(ctx, pVal);
8839 ** Virtual table module xFilter method.
8841 static int expertFilter(
8842 sqlite3_vtab_cursor *cur,
8843 int idxNum, const char *idxStr,
8844 int argc, sqlite3_value **argv
8846 ExpertCsr *pCsr = (ExpertCsr*)cur;
8847 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
8848 sqlite3expert *pExpert = pVtab->pExpert;
8855 rc = sqlite3_finalize(pCsr->pData);
8857 if( rc==SQLITE_OK ){
8858 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
8859 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
8863 if( rc==SQLITE_OK ){
8864 rc = expertNext(cur);
8869 static int idxRegisterVtab(sqlite3expert *p){
8870 static sqlite3_module expertModule = {
8872 expertConnect, /* xCreate - create a table */
8873 expertConnect, /* xConnect - connect to an existing table */
8874 expertBestIndex, /* xBestIndex - Determine search strategy */
8875 expertDisconnect, /* xDisconnect - Disconnect from a table */
8876 expertDisconnect, /* xDestroy - Drop a table */
8877 expertOpen, /* xOpen - open a cursor */
8878 expertClose, /* xClose - close a cursor */
8879 expertFilter, /* xFilter - configure scan constraints */
8880 expertNext, /* xNext - advance a cursor */
8881 expertEof, /* xEof */
8882 expertColumn, /* xColumn - read data */
8883 expertRowid, /* xRowid - read data */
8884 expertUpdate, /* xUpdate - write data */
8885 0, /* xBegin - begin transaction */
8886 0, /* xSync - sync transaction */
8887 0, /* xCommit - commit transaction */
8888 0, /* xRollback - rollback transaction */
8889 0, /* xFindFunction - function overloading */
8890 0, /* xRename - rename the table */
8893 0, /* xRollbackTo */
8894 0, /* xShadowName */
8897 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
8900 ** End of virtual table implementation.
8901 *************************************************************************/
8903 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
8904 ** is called, set it to the return value of sqlite3_finalize() before
8905 ** returning. Otherwise, discard the sqlite3_finalize() return value.
8907 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
8908 int rc = sqlite3_finalize(pStmt);
8909 if( *pRc==SQLITE_OK ) *pRc = rc;
8913 ** Attempt to allocate an IdxTable structure corresponding to table zTab
8914 ** in the main database of connection db. If successful, set (*ppOut) to
8915 ** point to the new object and return SQLITE_OK. Otherwise, return an
8916 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
8917 ** set to point to an error string.
8919 ** It is the responsibility of the caller to eventually free either the
8920 ** IdxTable object or error message using sqlite3_free().
8922 static int idxGetTableInfo(
8923 sqlite3 *db, /* Database connection to read details from */
8924 const char *zTab, /* Table name */
8925 IdxTable **ppOut, /* OUT: New object (if successful) */
8926 char **pzErrmsg /* OUT: Error message (if not) */
8928 sqlite3_stmt *p1 = 0;
8930 int nTab = STRLEN(zTab);
8931 int nByte = sizeof(IdxTable) + nTab + 1;
8937 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
8938 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8939 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8940 nByte += 1 + STRLEN(zCol);
8941 rc = sqlite3_table_column_metadata(
8942 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8944 nByte += 1 + STRLEN(zCol);
8946 nPk += (sqlite3_column_int(p1, 5)>0);
8948 rc2 = sqlite3_reset(p1);
8949 if( rc==SQLITE_OK ) rc = rc2;
8951 nByte += sizeof(IdxColumn) * nCol;
8952 if( rc==SQLITE_OK ){
8953 pNew = idxMalloc(&rc, nByte);
8955 if( rc==SQLITE_OK ){
8956 pNew->aCol = (IdxColumn*)&pNew[1];
8958 pCsr = (char*)&pNew->aCol[nCol];
8962 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
8963 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
8964 int nCopy = STRLEN(zCol) + 1;
8965 pNew->aCol[nCol].zName = pCsr;
8966 pNew->aCol[nCol].iPk = (sqlite3_column_int(p1, 5)==1 && nPk==1);
8967 memcpy(pCsr, zCol, nCopy);
8970 rc = sqlite3_table_column_metadata(
8971 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
8973 if( rc==SQLITE_OK ){
8974 nCopy = STRLEN(zCol) + 1;
8975 pNew->aCol[nCol].zColl = pCsr;
8976 memcpy(pCsr, zCol, nCopy);
8982 idxFinalize(&rc, p1);
8984 if( rc!=SQLITE_OK ){
8989 memcpy(pNew->zName, zTab, nTab+1);
8997 ** This function is a no-op if *pRc is set to anything other than
8998 ** SQLITE_OK when it is called.
9000 ** If *pRc is initially set to SQLITE_OK, then the text specified by
9001 ** the printf() style arguments is appended to zIn and the result returned
9002 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
9003 ** zIn before returning.
9005 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
9009 int nIn = zIn ? STRLEN(zIn) : 0;
9012 if( *pRc==SQLITE_OK ){
9013 zAppend = sqlite3_vmprintf(zFmt, ap);
9015 nAppend = STRLEN(zAppend);
9016 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
9018 if( zAppend && zRet ){
9019 if( nIn ) memcpy(zRet, zIn, nIn);
9020 memcpy(&zRet[nIn], zAppend, nAppend+1);
9024 *pRc = SQLITE_NOMEM;
9026 sqlite3_free(zAppend);
9034 ** Return true if zId must be quoted in order to use it as an SQL
9035 ** identifier, or false otherwise.
9037 static int idxIdentifierRequiresQuotes(const char *zId){
9039 for(i=0; zId[i]; i++){
9041 && !(zId[i]>='0' && zId[i]<='9')
9042 && !(zId[i]>='a' && zId[i]<='z')
9043 && !(zId[i]>='A' && zId[i]<='Z')
9052 ** This function appends an index column definition suitable for constraint
9053 ** pCons to the string passed as zIn and returns the result.
9055 static char *idxAppendColDefn(
9056 int *pRc, /* IN/OUT: Error code */
9057 char *zIn, /* Column defn accumulated so far */
9058 IdxTable *pTab, /* Table index will be created on */
9059 IdxConstraint *pCons
9062 IdxColumn *p = &pTab->aCol[pCons->iCol];
9063 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
9065 if( idxIdentifierRequiresQuotes(p->zName) ){
9066 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
9068 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
9071 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
9072 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
9073 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
9075 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
9080 zRet = idxAppendText(pRc, zRet, " DESC");
9086 ** Search database dbm for an index compatible with the one idxCreateFromCons()
9087 ** would create from arguments pScan, pEq and pTail. If no error occurs and
9088 ** such an index is found, return non-zero. Or, if no such index is found,
9091 ** If an error occurs, set *pRc to an SQLite error code and return zero.
9093 static int idxFindCompatible(
9094 int *pRc, /* OUT: Error code */
9095 sqlite3* dbm, /* Database to search */
9096 IdxScan *pScan, /* Scan for table to search for index on */
9097 IdxConstraint *pEq, /* List of == constraints */
9098 IdxConstraint *pTail /* List of range constraints */
9100 const char *zTbl = pScan->pTab->zName;
9101 sqlite3_stmt *pIdxList = 0;
9102 IdxConstraint *pIter;
9103 int nEq = 0; /* Number of elements in pEq */
9106 /* Count the elements in list pEq */
9107 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
9109 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
9110 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
9112 IdxConstraint *pT = pTail;
9113 sqlite3_stmt *pInfo = 0;
9114 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
9116 /* Zero the IdxConstraint.bFlag values in the pEq list */
9117 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
9119 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
9120 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
9121 int iIdx = sqlite3_column_int(pInfo, 0);
9122 int iCol = sqlite3_column_int(pInfo, 1);
9123 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
9126 for(pIter=pEq; pIter; pIter=pIter->pLink){
9127 if( pIter->bFlag ) continue;
9128 if( pIter->iCol!=iCol ) continue;
9129 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
9139 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
9147 idxFinalize(&rc, pInfo);
9149 if( rc==SQLITE_OK && bMatch ){
9150 sqlite3_finalize(pIdxList);
9154 idxFinalize(&rc, pIdxList);
9160 static int idxCreateFromCons(
9164 IdxConstraint *pTail
9166 sqlite3 *dbm = p->dbm;
9168 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
9169 IdxTable *pTab = pScan->pTab;
9172 IdxConstraint *pCons;
9176 for(pCons=pEq; pCons; pCons=pCons->pLink){
9177 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9179 for(pCons=pTail; pCons; pCons=pCons->pLink){
9180 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
9183 if( rc==SQLITE_OK ){
9184 /* Hash the list of columns to come up with a name for the index */
9185 const char *zTable = pScan->pTab->zName;
9186 char *zName; /* Index name */
9188 for(i=0; zCols[i]; i++){
9189 h += ((h<<3) + zCols[i]);
9191 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
9195 if( idxIdentifierRequiresQuotes(zTable) ){
9196 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
9198 zFmt = "CREATE INDEX %s ON %s(%s)";
9200 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
9204 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
9205 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
9207 sqlite3_free(zName);
9212 sqlite3_free(zCols);
9218 ** Return true if list pList (linked by IdxConstraint.pLink) contains
9219 ** a constraint compatible with *p. Otherwise return false.
9221 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
9222 IdxConstraint *pCmp;
9223 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
9224 if( p->iCol==pCmp->iCol ) return 1;
9229 static int idxCreateFromWhere(
9231 IdxScan *pScan, /* Create indexes for this scan */
9232 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
9234 IdxConstraint *p1 = 0;
9235 IdxConstraint *pCon;
9238 /* Gather up all the == constraints. */
9239 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
9240 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9246 /* Create an index using the == constraints collected above. And the
9247 ** range constraint/ORDER BY terms passed in by the caller, if any. */
9248 rc = idxCreateFromCons(p, pScan, p1, pTail);
9250 /* If no range/ORDER BY passed by the caller, create a version of the
9251 ** index for each range constraint. */
9253 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
9254 assert( pCon->pLink==0 );
9255 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
9256 rc = idxCreateFromCons(p, pScan, p1, pCon);
9265 ** Create candidate indexes in database [dbm] based on the data in
9266 ** linked-list pScan.
9268 static int idxCreateCandidates(sqlite3expert *p){
9272 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
9273 rc = idxCreateFromWhere(p, pIter, 0);
9274 if( rc==SQLITE_OK && pIter->pOrder ){
9275 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
9283 ** Free all elements of the linked list starting at pConstraint.
9285 static void idxConstraintFree(IdxConstraint *pConstraint){
9286 IdxConstraint *pNext;
9289 for(p=pConstraint; p; p=pNext){
9296 ** Free all elements of the linked list starting from pScan up until pLast
9297 ** (pLast is not freed).
9299 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
9302 for(p=pScan; p!=pLast; p=pNext){
9303 pNext = p->pNextScan;
9304 idxConstraintFree(p->pOrder);
9305 idxConstraintFree(p->pEq);
9306 idxConstraintFree(p->pRange);
9312 ** Free all elements of the linked list starting from pStatement up
9313 ** until pLast (pLast is not freed).
9315 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
9317 IdxStatement *pNext;
9318 for(p=pStatement; p!=pLast; p=pNext){
9320 sqlite3_free(p->zEQP);
9321 sqlite3_free(p->zIdx);
9327 ** Free the linked list of IdxTable objects starting at pTab.
9329 static void idxTableFree(IdxTable *pTab){
9332 for(pIter=pTab; pIter; pIter=pNext){
9333 pNext = pIter->pNext;
9334 sqlite3_free(pIter);
9339 ** Free the linked list of IdxWrite objects starting at pTab.
9341 static void idxWriteFree(IdxWrite *pTab){
9344 for(pIter=pTab; pIter; pIter=pNext){
9345 pNext = pIter->pNext;
9346 sqlite3_free(pIter);
9353 ** This function is called after candidate indexes have been created. It
9354 ** runs all the queries to see which indexes they prefer, and populates
9355 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
9359 char **pzErr /* OUT: Error message (sqlite3_malloc) */
9361 IdxStatement *pStmt;
9362 sqlite3 *dbm = p->dbm;
9368 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
9369 IdxHashEntry *pEntry;
9370 sqlite3_stmt *pExplain = 0;
9371 idxHashClear(&hIdx);
9372 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
9373 "EXPLAIN QUERY PLAN %s", pStmt->zSql
9375 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
9376 /* int iId = sqlite3_column_int(pExplain, 0); */
9377 /* int iParent = sqlite3_column_int(pExplain, 1); */
9378 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
9379 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
9383 if( !zDetail ) continue;
9384 nDetail = STRLEN(zDetail);
9386 for(i=0; i<nDetail; i++){
9387 const char *zIdx = 0;
9388 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
9389 zIdx = &zDetail[i+13];
9390 }else if( i+22<nDetail
9391 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
9393 zIdx = &zDetail[i+22];
9398 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
9401 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
9403 idxHashAdd(&rc, &hIdx, zSql, 0);
9404 if( rc ) goto find_indexes_out;
9410 if( zDetail[0]!='-' ){
9411 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
9415 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
9416 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
9419 idxFinalize(&rc, pExplain);
9423 idxHashClear(&hIdx);
9427 static int idxAuthCallback(
9433 const char *zTrigger
9438 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
9439 if( sqlite3_stricmp(zDb, "main")==0 ){
9440 sqlite3expert *p = (sqlite3expert*)pCtx;
9442 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
9443 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
9447 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
9448 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
9451 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
9452 if( rc==SQLITE_OK ){
9453 pWrite->pTab = pTab;
9455 pWrite->pNext = p->pWrite;
9465 static int idxProcessOneTrigger(
9470 static const char *zInt = UNIQUE_TABLE_NAME;
9471 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
9472 IdxTable *pTab = pWrite->pTab;
9473 const char *zTab = pTab->zName;
9475 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_schema "
9476 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
9478 sqlite3_stmt *pSelect = 0;
9482 /* Create the table and its triggers in the temp schema */
9483 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
9484 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
9485 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
9486 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
9488 idxFinalize(&rc, pSelect);
9490 /* Rename the table in the temp schema to zInt */
9491 if( rc==SQLITE_OK ){
9492 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
9496 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
9501 switch( pWrite->eOp ){
9502 case SQLITE_INSERT: {
9504 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
9505 for(i=0; i<pTab->nCol; i++){
9506 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
9508 zWrite = idxAppendText(&rc, zWrite, ")");
9511 case SQLITE_UPDATE: {
9513 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
9514 for(i=0; i<pTab->nCol; i++){
9515 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
9522 assert( pWrite->eOp==SQLITE_DELETE );
9523 if( rc==SQLITE_OK ){
9524 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
9525 if( zWrite==0 ) rc = SQLITE_NOMEM;
9530 if( rc==SQLITE_OK ){
9531 sqlite3_stmt *pX = 0;
9532 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
9533 idxFinalize(&rc, pX);
9534 if( rc!=SQLITE_OK ){
9535 idxDatabaseError(p->dbv, pzErr);
9538 sqlite3_free(zWrite);
9540 if( rc==SQLITE_OK ){
9541 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
9547 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
9550 IdxWrite *pFirst = p->pWrite;
9552 while( rc==SQLITE_OK && pFirst!=pEnd ){
9554 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
9555 rc = idxProcessOneTrigger(p, pIter, pzErr);
9565 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
9566 int rc = idxRegisterVtab(p);
9567 sqlite3_stmt *pSchema = 0;
9569 /* For each table in the main db schema:
9571 ** 1) Add an entry to the p->pTable list, and
9572 ** 2) Create the equivalent virtual table in dbv.
9574 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
9575 "SELECT type, name, sql, 1 FROM sqlite_schema "
9576 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
9578 "SELECT type, name, sql, 2 FROM sqlite_schema "
9579 "WHERE type = 'trigger'"
9580 " AND tbl_name IN(SELECT name FROM sqlite_schema WHERE type = 'view') "
9583 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
9584 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
9585 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
9586 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
9588 if( zType[0]=='v' || zType[1]=='r' ){
9589 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
9592 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
9593 if( rc==SQLITE_OK ){
9597 pTab->pNext = p->pTable;
9600 /* The statement the vtab will pass to sqlite3_declare_vtab() */
9601 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
9602 for(i=0; i<pTab->nCol; i++){
9603 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
9604 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
9607 zInner = idxAppendText(&rc, zInner, ")");
9609 /* The CVT statement to create the vtab */
9610 zOuter = idxAppendText(&rc, 0,
9611 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
9613 if( rc==SQLITE_OK ){
9614 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
9616 sqlite3_free(zInner);
9617 sqlite3_free(zOuter);
9621 idxFinalize(&rc, pSchema);
9625 struct IdxSampleCtx {
9627 double target; /* Target nRet/nRow value */
9628 double nRow; /* Number of rows seen */
9629 double nRet; /* Number of rows returned */
9632 static void idxSampleFunc(
9633 sqlite3_context *pCtx,
9635 sqlite3_value **argv
9637 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
9645 bRet = (p->nRet / p->nRow) <= p->target;
9648 sqlite3_randomness(2, (void*)&rnd);
9649 bRet = ((int)rnd % 100) <= p->iTarget;
9653 sqlite3_result_int(pCtx, bRet);
9655 p->nRet += (double)bRet;
9661 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
9662 i64 iVal; /* SQLITE_INTEGER value */
9663 double rVal; /* SQLITE_FLOAT value */
9664 int nByte; /* Bytes of space allocated at z */
9665 int n; /* Size of buffer z */
9666 char *z; /* SQLITE_TEXT/BLOB value */
9671 ** Implementation of scalar function rem().
9673 static void idxRemFunc(
9674 sqlite3_context *pCtx,
9676 sqlite3_value **argv
9678 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
9679 struct IdxRemSlot *pSlot;
9683 iSlot = sqlite3_value_int(argv[0]);
9684 assert( iSlot<=p->nSlot );
9685 pSlot = &p->aSlot[iSlot];
9687 switch( pSlot->eType ){
9692 case SQLITE_INTEGER:
9693 sqlite3_result_int64(pCtx, pSlot->iVal);
9697 sqlite3_result_double(pCtx, pSlot->rVal);
9701 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9705 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
9709 pSlot->eType = sqlite3_value_type(argv[1]);
9710 switch( pSlot->eType ){
9715 case SQLITE_INTEGER:
9716 pSlot->iVal = sqlite3_value_int64(argv[1]);
9720 pSlot->rVal = sqlite3_value_double(argv[1]);
9725 int nByte = sqlite3_value_bytes(argv[1]);
9726 if( nByte>pSlot->nByte ){
9727 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
9729 sqlite3_result_error_nomem(pCtx);
9732 pSlot->nByte = nByte*2;
9736 if( pSlot->eType==SQLITE_BLOB ){
9737 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
9739 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
9746 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
9749 "SELECT max(i.seqno) FROM "
9750 " sqlite_schema AS s, "
9751 " pragma_index_list(s.name) AS l, "
9752 " pragma_index_info(l.name) AS i "
9753 "WHERE s.type = 'table'";
9754 sqlite3_stmt *pMax = 0;
9757 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
9758 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
9759 *pnMax = sqlite3_column_int(pMax, 0) + 1;
9761 idxFinalize(&rc, pMax);
9766 static int idxPopulateOneStat1(
9768 sqlite3_stmt *pIndexXInfo,
9769 sqlite3_stmt *pWriteStat,
9779 sqlite3_stmt *pQuery = 0;
9783 assert( p->iSample>0 );
9785 /* Formulate the query text */
9786 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
9787 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
9788 const char *zComma = zCols==0 ? "" : ", ";
9789 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
9790 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
9791 zCols = idxAppendText(&rc, zCols,
9792 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
9794 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
9796 sqlite3_reset(pIndexXInfo);
9797 if( rc==SQLITE_OK ){
9798 if( p->iSample==100 ){
9799 zQuery = sqlite3_mprintf(
9800 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
9803 zQuery = sqlite3_mprintf(
9804 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
9808 sqlite3_free(zCols);
9809 sqlite3_free(zOrder);
9811 /* Formulate the query text */
9812 if( rc==SQLITE_OK ){
9813 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9814 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
9816 sqlite3_free(zQuery);
9818 if( rc==SQLITE_OK ){
9819 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
9821 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9822 IdxHashEntry *pEntry;
9824 for(i=0; i<=nCol; i++) aStat[i] = 1;
9825 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
9827 for(i=0; i<nCol; i++){
9828 if( sqlite3_column_int(pQuery, i)==0 ) break;
9830 for(/*no-op*/; i<nCol; i++){
9835 if( rc==SQLITE_OK ){
9837 zStat = sqlite3_mprintf("%d", s0);
9838 if( zStat==0 ) rc = SQLITE_NOMEM;
9839 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
9840 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
9844 if( rc==SQLITE_OK ){
9845 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
9846 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
9847 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
9848 sqlite3_step(pWriteStat);
9849 rc = sqlite3_reset(pWriteStat);
9852 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
9854 assert( pEntry->zVal2==0 );
9855 pEntry->zVal2 = zStat;
9857 sqlite3_free(zStat);
9860 sqlite3_free(aStat);
9861 idxFinalize(&rc, pQuery);
9866 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
9870 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9871 if( rc!=SQLITE_OK ) return rc;
9873 zSql = sqlite3_mprintf(
9874 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
9876 if( zSql==0 ) return SQLITE_NOMEM;
9877 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
9884 ** This function is called as part of sqlite3_expert_analyze(). Candidate
9885 ** indexes have already been created in database sqlite3expert.dbm, this
9886 ** function populates sqlite_stat1 table in the same database.
9888 ** The stat1 data is generated by querying the
9890 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
9893 struct IdxRemCtx *pCtx = 0;
9894 struct IdxSampleCtx samplectx;
9896 i64 iPrev = -100000;
9897 sqlite3_stmt *pAllIndex = 0;
9898 sqlite3_stmt *pIndexXInfo = 0;
9899 sqlite3_stmt *pWrite = 0;
9901 const char *zAllIndex =
9902 "SELECT s.rowid, s.name, l.name FROM "
9903 " sqlite_schema AS s, "
9904 " pragma_index_list(s.name) AS l "
9905 "WHERE s.type = 'table'";
9906 const char *zIndexXInfo =
9907 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
9908 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
9910 /* If iSample==0, no sqlite_stat1 data is required. */
9911 if( p->iSample==0 ) return SQLITE_OK;
9913 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
9914 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
9916 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
9918 if( rc==SQLITE_OK ){
9919 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
9920 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
9923 if( rc==SQLITE_OK ){
9924 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
9925 rc = sqlite3_create_function(
9926 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
9929 if( rc==SQLITE_OK ){
9930 rc = sqlite3_create_function(
9931 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
9935 if( rc==SQLITE_OK ){
9936 pCtx->nSlot = nMax+1;
9937 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
9939 if( rc==SQLITE_OK ){
9940 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
9942 if( rc==SQLITE_OK ){
9943 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
9946 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
9947 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
9948 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
9949 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
9950 if( p->iSample<100 && iPrev!=iRowid ){
9951 samplectx.target = (double)p->iSample / 100.0;
9952 samplectx.iTarget = p->iSample;
9953 samplectx.nRow = 0.0;
9954 samplectx.nRet = 0.0;
9955 rc = idxBuildSampleTable(p, zTab);
9956 if( rc!=SQLITE_OK ) break;
9958 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
9961 if( rc==SQLITE_OK && p->iSample<100 ){
9962 rc = sqlite3_exec(p->dbv,
9963 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
9967 idxFinalize(&rc, pAllIndex);
9968 idxFinalize(&rc, pIndexXInfo);
9969 idxFinalize(&rc, pWrite);
9971 for(i=0; i<pCtx->nSlot; i++){
9972 sqlite3_free(pCtx->aSlot[i].z);
9976 if( rc==SQLITE_OK ){
9977 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_schema", 0, 0, 0);
9980 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
9985 ** Allocate a new sqlite3expert object.
9987 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
9989 sqlite3expert *pNew;
9991 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
9993 /* Open two in-memory databases to work with. The "vtab database" (dbv)
9994 ** will contain a virtual table corresponding to each real table in
9995 ** the user database schema, and a copy of each view. It is used to
9996 ** collect information regarding the WHERE, ORDER BY and other clauses
9997 ** of the user's query.
9999 if( rc==SQLITE_OK ){
10001 pNew->iSample = 100;
10002 rc = sqlite3_open(":memory:", &pNew->dbv);
10004 if( rc==SQLITE_OK ){
10005 rc = sqlite3_open(":memory:", &pNew->dbm);
10006 if( rc==SQLITE_OK ){
10007 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
10012 /* Copy the entire schema of database [db] into [dbm]. */
10013 if( rc==SQLITE_OK ){
10014 sqlite3_stmt *pSql;
10015 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
10016 "SELECT sql FROM sqlite_schema WHERE name NOT LIKE 'sqlite_%%'"
10017 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
10019 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
10020 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
10021 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
10023 idxFinalize(&rc, pSql);
10026 /* Create the vtab schema */
10027 if( rc==SQLITE_OK ){
10028 rc = idxCreateVtabSchema(pNew, pzErrmsg);
10031 /* Register the auth callback with dbv */
10032 if( rc==SQLITE_OK ){
10033 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
10036 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
10037 ** return the new sqlite3expert handle. */
10038 if( rc!=SQLITE_OK ){
10039 sqlite3_expert_destroy(pNew);
10046 ** Configure an sqlite3expert object.
10048 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
10049 int rc = SQLITE_OK;
10053 case EXPERT_CONFIG_SAMPLE: {
10054 int iVal = va_arg(ap, int);
10055 if( iVal<0 ) iVal = 0;
10056 if( iVal>100 ) iVal = 100;
10061 rc = SQLITE_NOTFOUND;
10070 ** Add an SQL statement to the analysis.
10072 int sqlite3_expert_sql(
10073 sqlite3expert *p, /* From sqlite3_expert_new() */
10074 const char *zSql, /* SQL statement to add */
10075 char **pzErr /* OUT: Error message (if any) */
10077 IdxScan *pScanOrig = p->pScan;
10078 IdxStatement *pStmtOrig = p->pStatement;
10079 int rc = SQLITE_OK;
10080 const char *zStmt = zSql;
10082 if( p->bRun ) return SQLITE_MISUSE;
10084 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
10085 sqlite3_stmt *pStmt = 0;
10086 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
10087 if( rc==SQLITE_OK ){
10089 IdxStatement *pNew;
10090 const char *z = sqlite3_sql(pStmt);
10092 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
10093 if( rc==SQLITE_OK ){
10094 pNew->zSql = (char*)&pNew[1];
10095 memcpy(pNew->zSql, z, n+1);
10096 pNew->pNext = p->pStatement;
10097 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
10098 p->pStatement = pNew;
10100 sqlite3_finalize(pStmt);
10103 idxDatabaseError(p->dbv, pzErr);
10107 if( rc!=SQLITE_OK ){
10108 idxScanFree(p->pScan, pScanOrig);
10109 idxStatementFree(p->pStatement, pStmtOrig);
10110 p->pScan = pScanOrig;
10111 p->pStatement = pStmtOrig;
10117 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
10119 IdxHashEntry *pEntry;
10121 /* Do trigger processing to collect any extra IdxScan structures */
10122 rc = idxProcessTriggers(p, pzErr);
10124 /* Create candidate indexes within the in-memory database file */
10125 if( rc==SQLITE_OK ){
10126 rc = idxCreateCandidates(p);
10129 /* Generate the stat1 data */
10130 if( rc==SQLITE_OK ){
10131 rc = idxPopulateStat1(p, pzErr);
10134 /* Formulate the EXPERT_REPORT_CANDIDATES text */
10135 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
10136 p->zCandidates = idxAppendText(&rc, p->zCandidates,
10137 "%s;%s%s\n", pEntry->zVal,
10138 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
10142 /* Figure out which of the candidate indexes are preferred by the query
10143 ** planner and report the results to the user. */
10144 if( rc==SQLITE_OK ){
10145 rc = idxFindIndexes(p, pzErr);
10148 if( rc==SQLITE_OK ){
10155 ** Return the total number of statements that have been added to this
10156 ** sqlite3expert using sqlite3_expert_sql().
10158 int sqlite3_expert_count(sqlite3expert *p){
10160 if( p->pStatement ) nRet = p->pStatement->iId+1;
10165 ** Return a component of the report.
10167 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
10168 const char *zRet = 0;
10169 IdxStatement *pStmt;
10171 if( p->bRun==0 ) return 0;
10172 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
10174 case EXPERT_REPORT_SQL:
10175 if( pStmt ) zRet = pStmt->zSql;
10177 case EXPERT_REPORT_INDEXES:
10178 if( pStmt ) zRet = pStmt->zIdx;
10180 case EXPERT_REPORT_PLAN:
10181 if( pStmt ) zRet = pStmt->zEQP;
10183 case EXPERT_REPORT_CANDIDATES:
10184 zRet = p->zCandidates;
10191 ** Free an sqlite3expert object.
10193 void sqlite3_expert_destroy(sqlite3expert *p){
10195 sqlite3_close(p->dbm);
10196 sqlite3_close(p->dbv);
10197 idxScanFree(p->pScan, 0);
10198 idxStatementFree(p->pStatement, 0);
10199 idxTableFree(p->pTable);
10200 idxWriteFree(p->pWrite);
10201 idxHashClear(&p->hIdx);
10202 sqlite3_free(p->zCandidates);
10207 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
10209 /************************* End ../ext/expert/sqlite3expert.c ********************/
10211 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
10212 /************************* Begin ../ext/misc/dbdata.c ******************/
10216 ** The author disclaims copyright to this source code. In place of
10217 ** a legal notice, here is a blessing:
10219 ** May you do good and not evil.
10220 ** May you find forgiveness for yourself and forgive others.
10221 ** May you share freely, never taking more than you give.
10223 ******************************************************************************
10225 ** This file contains an implementation of two eponymous virtual tables,
10226 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
10227 ** "sqlite_dbpage" eponymous virtual table be available.
10230 ** sqlite_dbdata is used to extract data directly from a database b-tree
10231 ** page and its associated overflow pages, bypassing the b-tree layer.
10232 ** The table schema is equivalent to:
10234 ** CREATE TABLE sqlite_dbdata(
10239 ** schema TEXT HIDDEN
10242 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
10243 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
10246 ** Each page of the database is inspected. If it cannot be interpreted as
10247 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
10248 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
10249 ** table contains one row for each field in the record associated with
10250 ** each cell on the page. For intkey b-trees, the key value is stored in
10253 ** For example, for the database:
10255 ** CREATE TABLE t1(a, b); -- root page is page 2
10256 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
10257 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
10259 ** the sqlite_dbdata table contains, as well as from entries related to
10260 ** page 1, content equivalent to:
10262 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
10264 ** (2, 0, 0, 'v' ),
10265 ** (2, 0, 1, 'five'),
10266 ** (2, 1, -1, 10 ),
10267 ** (2, 1, 0, 'x' ),
10268 ** (2, 1, 1, 'ten' );
10270 ** If database corruption is encountered, this module does not report an
10271 ** error. Instead, it attempts to extract as much data as possible and
10272 ** ignores the corruption.
10275 ** The sqlite_dbptr table has the following schema:
10277 ** CREATE TABLE sqlite_dbptr(
10280 ** schema TEXT HIDDEN
10283 ** It contains one entry for each b-tree pointer between a parent and
10284 ** child page in the database.
10286 #if !defined(SQLITEINT_H)
10287 /* #include "sqlite3ext.h" */
10289 /* typedef unsigned char u8; */
10292 SQLITE_EXTENSION_INIT1
10293 #include <string.h>
10294 #include <assert.h>
10296 #define DBDATA_PADDING_BYTES 100
10298 typedef struct DbdataTable DbdataTable;
10299 typedef struct DbdataCursor DbdataCursor;
10301 /* Cursor object */
10302 struct DbdataCursor {
10303 sqlite3_vtab_cursor base; /* Base class. Must be first */
10304 sqlite3_stmt *pStmt; /* For fetching database pages */
10306 int iPgno; /* Current page number */
10307 u8 *aPage; /* Buffer containing page */
10308 int nPage; /* Size of aPage[] in bytes */
10309 int nCell; /* Number of cells on aPage[] */
10310 int iCell; /* Current cell number */
10311 int bOnePage; /* True to stop after one page */
10313 sqlite3_int64 iRowid;
10315 /* Only for the sqlite_dbdata table */
10316 u8 *pRec; /* Buffer containing current record */
10317 int nRec; /* Size of pRec[] in bytes */
10318 int nHdr; /* Size of header in bytes */
10319 int iField; /* Current field number */
10323 sqlite3_int64 iIntkey; /* Integer key value */
10327 struct DbdataTable {
10328 sqlite3_vtab base; /* Base class. Must be first */
10329 sqlite3 *db; /* The database connection */
10330 sqlite3_stmt *pStmt; /* For fetching database pages */
10331 int bPtr; /* True for sqlite3_dbptr table */
10334 /* Column and schema definitions for sqlite_dbdata */
10335 #define DBDATA_COLUMN_PGNO 0
10336 #define DBDATA_COLUMN_CELL 1
10337 #define DBDATA_COLUMN_FIELD 2
10338 #define DBDATA_COLUMN_VALUE 3
10339 #define DBDATA_COLUMN_SCHEMA 4
10340 #define DBDATA_SCHEMA \
10341 "CREATE TABLE x(" \
10344 " field INTEGER," \
10346 " schema TEXT HIDDEN" \
10349 /* Column and schema definitions for sqlite_dbptr */
10350 #define DBPTR_COLUMN_PGNO 0
10351 #define DBPTR_COLUMN_CHILD 1
10352 #define DBPTR_COLUMN_SCHEMA 2
10353 #define DBPTR_SCHEMA \
10354 "CREATE TABLE x(" \
10356 " child INTEGER," \
10357 " schema TEXT HIDDEN" \
10361 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
10364 static int dbdataConnect(
10367 int argc, const char *const*argv,
10368 sqlite3_vtab **ppVtab,
10371 DbdataTable *pTab = 0;
10372 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
10374 if( rc==SQLITE_OK ){
10375 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
10379 memset(pTab, 0, sizeof(DbdataTable));
10381 pTab->bPtr = (pAux!=0);
10385 *ppVtab = (sqlite3_vtab*)pTab;
10390 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
10392 static int dbdataDisconnect(sqlite3_vtab *pVtab){
10393 DbdataTable *pTab = (DbdataTable*)pVtab;
10395 sqlite3_finalize(pTab->pStmt);
10396 sqlite3_free(pVtab);
10402 ** This function interprets two types of constraints:
10407 ** If neither are present, idxNum is set to 0. If schema=? is present,
10408 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
10409 ** in idxNum is set.
10411 ** If both parameters are present, schema is in position 0 and pgno in
10414 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
10415 DbdataTable *pTab = (DbdataTable*)tab;
10419 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
10421 for(i=0; i<pIdx->nConstraint; i++){
10422 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
10423 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
10424 if( p->iColumn==colSchema ){
10425 if( p->usable==0 ) return SQLITE_CONSTRAINT;
10428 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
10435 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
10436 pIdx->aConstraintUsage[iSchema].omit = 1;
10439 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
10440 pIdx->aConstraintUsage[iPgno].omit = 1;
10441 pIdx->estimatedCost = 100;
10442 pIdx->estimatedRows = 50;
10444 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
10445 int iCol = pIdx->aOrderBy[0].iColumn;
10446 if( pIdx->nOrderBy==1 ){
10447 pIdx->orderByConsumed = (iCol==0 || iCol==1);
10448 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
10449 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
10454 pIdx->estimatedCost = 100000000;
10455 pIdx->estimatedRows = 1000000000;
10457 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
10462 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
10464 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
10465 DbdataCursor *pCsr;
10467 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
10469 return SQLITE_NOMEM;
10471 memset(pCsr, 0, sizeof(DbdataCursor));
10472 pCsr->base.pVtab = pVTab;
10475 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
10480 ** Restore a cursor object to the state it was in when first allocated
10481 ** by dbdataOpen().
10483 static void dbdataResetCursor(DbdataCursor *pCsr){
10484 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
10485 if( pTab->pStmt==0 ){
10486 pTab->pStmt = pCsr->pStmt;
10488 sqlite3_finalize(pCsr->pStmt);
10494 pCsr->bOnePage = 0;
10495 sqlite3_free(pCsr->aPage);
10496 sqlite3_free(pCsr->pRec);
10502 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
10504 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
10505 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10506 dbdataResetCursor(pCsr);
10507 sqlite3_free(pCsr);
10512 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
10514 static unsigned int get_uint16(unsigned char *a){
10515 return (a[0]<<8)|a[1];
10517 static unsigned int get_uint32(unsigned char *a){
10518 return ((unsigned int)a[0]<<24)
10519 | ((unsigned int)a[1]<<16)
10520 | ((unsigned int)a[2]<<8)
10521 | ((unsigned int)a[3]);
10525 ** Load page pgno from the database via the sqlite_dbpage virtual table.
10526 ** If successful, set (*ppPage) to point to a buffer containing the page
10527 ** data, (*pnPage) to the size of that buffer in bytes and return
10528 ** SQLITE_OK. In this case it is the responsibility of the caller to
10529 ** eventually free the buffer using sqlite3_free().
10531 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
10532 ** return an SQLite error code.
10534 static int dbdataLoadPage(
10535 DbdataCursor *pCsr, /* Cursor object */
10536 unsigned int pgno, /* Page number of page to load */
10537 u8 **ppPage, /* OUT: pointer to page buffer */
10538 int *pnPage /* OUT: Size of (*ppPage) in bytes */
10541 int rc = SQLITE_OK;
10542 sqlite3_stmt *pStmt = pCsr->pStmt;
10546 sqlite3_bind_int64(pStmt, 2, pgno);
10547 if( SQLITE_ROW==sqlite3_step(pStmt) ){
10548 int nCopy = sqlite3_column_bytes(pStmt, 0);
10551 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
10555 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
10556 memcpy(pPage, pCopy, nCopy);
10557 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
10563 rc2 = sqlite3_reset(pStmt);
10564 if( rc==SQLITE_OK ) rc = rc2;
10570 ** Read a varint. Put the value in *pVal and return the number of bytes.
10572 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
10573 sqlite3_int64 v = 0;
10575 for(i=0; i<8; i++){
10576 v = (v<<7) + (z[i]&0x7f);
10577 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
10579 v = (v<<8) + (z[i]&0xff);
10585 ** Return the number of bytes of space used by an SQLite value of type
10588 static int dbdataValueBytes(int eType){
10590 case 0: case 8: case 9:
10608 return ((eType-12) / 2);
10615 ** Load a value of type eType from buffer pData and use it to set the
10616 ** result of context object pCtx.
10618 static void dbdataValue(
10619 sqlite3_context *pCtx,
10624 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
10629 sqlite3_result_null(pCtx);
10633 sqlite3_result_int(pCtx, 0);
10636 sqlite3_result_int(pCtx, 1);
10639 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
10640 sqlite3_uint64 v = (signed char)pData[0];
10644 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10645 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
10646 case 4: v = (v<<8) + pData[0]; pData++;
10647 case 3: v = (v<<8) + pData[0]; pData++;
10648 case 2: v = (v<<8) + pData[0]; pData++;
10653 memcpy(&r, &v, sizeof(r));
10654 sqlite3_result_double(pCtx, r);
10656 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
10662 int n = ((eType-12) / 2);
10664 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
10666 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
10674 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
10676 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
10677 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10678 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10683 int iOff = (pCsr->iPgno==1 ? 100 : 0);
10686 if( pCsr->aPage==0 ){
10688 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
10689 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
10690 if( rc!=SQLITE_OK ) return rc;
10691 if( pCsr->aPage ) break;
10694 pCsr->iCell = pTab->bPtr ? -2 : 0;
10695 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
10699 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
10700 pCsr->iCell = pCsr->nCell;
10703 if( pCsr->iCell>=pCsr->nCell ){
10704 sqlite3_free(pCsr->aPage);
10706 if( pCsr->bOnePage ) return SQLITE_OK;
10712 /* If there is no record loaded, load it now. */
10713 if( pCsr->pRec==0 ){
10716 sqlite3_int64 nPayload = 0;
10717 sqlite3_int64 nHdr = 0;
10722 switch( pCsr->aPage[iOff] ){
10732 /* This is not a b-tree page with records on it. Continue. */
10733 pCsr->iCell = pCsr->nCell;
10737 if( pCsr->iCell>=pCsr->nCell ){
10741 iOff += 8 + nPointer + pCsr->iCell*2;
10742 if( iOff>pCsr->nPage ){
10745 iOff = get_uint16(&pCsr->aPage[iOff]);
10748 /* For an interior node cell, skip past the child-page number */
10751 /* Load the "byte of payload including overflow" field */
10752 if( bNextPage || iOff>pCsr->nPage ){
10755 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
10758 /* If this is a leaf intkey cell, load the rowid */
10759 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
10760 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
10763 /* Figure out how much data to read from the local page */
10768 X = ((U-12)*64/255)-23;
10774 M = ((U-12)*32/255)-23;
10775 K = M+((nPayload-M)%(U-4));
10783 if( bNextPage || nLocal+iOff>pCsr->nPage ){
10787 /* Allocate space for payload. And a bit more to catch small buffer
10788 ** overruns caused by attempting to read a varint or similar from
10789 ** near the end of a corrupt record. */
10790 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
10791 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
10792 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
10793 pCsr->nRec = nPayload;
10795 /* Load the nLocal bytes of payload */
10796 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
10799 /* Load content from overflow pages */
10800 if( nPayload>nLocal ){
10801 sqlite3_int64 nRem = nPayload - nLocal;
10802 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
10807 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
10808 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
10809 if( rc!=SQLITE_OK ) return rc;
10810 if( aOvfl==0 ) break;
10813 if( nCopy>nRem ) nCopy = nRem;
10814 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
10817 pgnoOvfl = get_uint32(aOvfl);
10818 sqlite3_free(aOvfl);
10822 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
10824 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
10825 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
10826 pCsr->iField = (bHasRowid ? -1 : 0);
10831 if( pCsr->iField>0 ){
10832 sqlite3_int64 iType;
10833 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
10836 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
10837 pCsr->pPtr += dbdataValueBytes(iType);
10843 sqlite3_free(pCsr->aPage);
10844 sqlite3_free(pCsr->pRec);
10847 if( pCsr->bOnePage ) return SQLITE_OK;
10850 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
10854 /* Advance to the next cell. The next iteration of the loop will load
10855 ** the record and so on. */
10856 sqlite3_free(pCsr->pRec);
10863 assert( !"can't get here" );
10868 ** Return true if the cursor is at EOF.
10870 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
10871 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10872 return pCsr->aPage==0;
10876 ** Determine the size in pages of database zSchema (where zSchema is
10877 ** "main", "temp" or the name of an attached database) and set
10878 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
10879 ** an SQLite error code.
10881 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
10882 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
10885 sqlite3_stmt *pStmt = 0;
10887 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
10888 if( zSql==0 ) return SQLITE_NOMEM;
10889 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
10890 sqlite3_free(zSql);
10891 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
10892 pCsr->szDb = sqlite3_column_int(pStmt, 0);
10894 rc2 = sqlite3_finalize(pStmt);
10895 if( rc==SQLITE_OK ) rc = rc2;
10900 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
10902 static int dbdataFilter(
10903 sqlite3_vtab_cursor *pCursor,
10904 int idxNum, const char *idxStr,
10905 int argc, sqlite3_value **argv
10907 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10908 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10909 int rc = SQLITE_OK;
10910 const char *zSchema = "main";
10912 dbdataResetCursor(pCsr);
10913 assert( pCsr->iPgno==1 );
10914 if( idxNum & 0x01 ){
10915 zSchema = (const char*)sqlite3_value_text(argv[0]);
10917 if( idxNum & 0x02 ){
10918 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
10919 pCsr->bOnePage = 1;
10921 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
10922 rc = dbdataDbsize(pCsr, zSchema);
10925 if( rc==SQLITE_OK ){
10927 pCsr->pStmt = pTab->pStmt;
10930 rc = sqlite3_prepare_v2(pTab->db,
10931 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
10936 if( rc==SQLITE_OK ){
10937 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
10939 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
10941 if( rc==SQLITE_OK ){
10942 rc = dbdataNext(pCursor);
10948 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
10950 static int dbdataColumn(
10951 sqlite3_vtab_cursor *pCursor,
10952 sqlite3_context *ctx,
10955 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
10956 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
10959 case DBPTR_COLUMN_PGNO:
10960 sqlite3_result_int64(ctx, pCsr->iPgno);
10962 case DBPTR_COLUMN_CHILD: {
10963 int iOff = pCsr->iPgno==1 ? 100 : 0;
10964 if( pCsr->iCell<0 ){
10967 iOff += 12 + pCsr->iCell*2;
10968 if( iOff>pCsr->nPage ) return SQLITE_OK;
10969 iOff = get_uint16(&pCsr->aPage[iOff]);
10971 if( iOff<=pCsr->nPage ){
10972 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
10979 case DBDATA_COLUMN_PGNO:
10980 sqlite3_result_int64(ctx, pCsr->iPgno);
10982 case DBDATA_COLUMN_CELL:
10983 sqlite3_result_int(ctx, pCsr->iCell);
10985 case DBDATA_COLUMN_FIELD:
10986 sqlite3_result_int(ctx, pCsr->iField);
10988 case DBDATA_COLUMN_VALUE: {
10989 if( pCsr->iField<0 ){
10990 sqlite3_result_int64(ctx, pCsr->iIntkey);
10992 sqlite3_int64 iType;
10993 dbdataGetVarint(pCsr->pHdrPtr, &iType);
10995 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
11006 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
11008 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
11009 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
11010 *pRowid = pCsr->iRowid;
11016 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
11018 static int sqlite3DbdataRegister(sqlite3 *db){
11019 static sqlite3_module dbdata_module = {
11022 dbdataConnect, /* xConnect */
11023 dbdataBestIndex, /* xBestIndex */
11024 dbdataDisconnect, /* xDisconnect */
11026 dbdataOpen, /* xOpen - open a cursor */
11027 dbdataClose, /* xClose - close a cursor */
11028 dbdataFilter, /* xFilter - configure scan constraints */
11029 dbdataNext, /* xNext - advance a cursor */
11030 dbdataEof, /* xEof - check for end of scan */
11031 dbdataColumn, /* xColumn - read data */
11032 dbdataRowid, /* xRowid - read data */
11038 0, /* xFindMethod */
11040 0, /* xSavepoint */
11042 0, /* xRollbackTo */
11043 0 /* xShadowName */
11046 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
11047 if( rc==SQLITE_OK ){
11048 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
11056 int sqlite3_dbdata_init(
11059 const sqlite3_api_routines *pApi
11061 SQLITE_EXTENSION_INIT2(pApi);
11062 return sqlite3DbdataRegister(db);
11065 /************************* End ../ext/misc/dbdata.c ********************/
11068 #if defined(SQLITE_ENABLE_SESSION)
11070 ** State information for a single open session
11072 typedef struct OpenSession OpenSession;
11073 struct OpenSession {
11074 char *zName; /* Symbolic name for this session */
11075 int nFilter; /* Number of xFilter rejection GLOB patterns */
11076 char **azFilter; /* Array of xFilter rejection GLOB patterns */
11077 sqlite3_session *p; /* The open session */
11081 typedef struct ExpertInfo ExpertInfo;
11082 struct ExpertInfo {
11083 sqlite3expert *pExpert;
11087 /* A single line in the EQP output */
11088 typedef struct EQPGraphRow EQPGraphRow;
11089 struct EQPGraphRow {
11090 int iEqpId; /* ID for this row */
11091 int iParentId; /* ID of the parent row */
11092 EQPGraphRow *pNext; /* Next row in sequence */
11093 char zText[1]; /* Text to display for this row */
11096 /* All EQP output is collected into an instance of the following */
11097 typedef struct EQPGraph EQPGraph;
11099 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
11100 EQPGraphRow *pLast; /* Last element of the pRow list */
11101 char zPrefix[100]; /* Graph prefix */
11105 ** State information about the database connection is contained in an
11106 ** instance of the following structure.
11108 typedef struct ShellState ShellState;
11109 struct ShellState {
11110 sqlite3 *db; /* The database */
11111 u8 autoExplain; /* Automatically turn on .explain mode */
11112 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
11113 u8 autoEQPtest; /* autoEQP is in test mode */
11114 u8 autoEQPtrace; /* autoEQP is in trace mode */
11115 u8 statsOn; /* True to display memory stats before each finalize */
11116 u8 scanstatsOn; /* True to display scan stats before each finalize */
11117 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
11118 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
11119 u8 nEqpLevel; /* Depth of the EQP output graph */
11120 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
11121 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
11122 int outCount; /* Revert to stdout when reaching zero */
11123 int cnt; /* Number of records displayed so far */
11124 int lineno; /* Line number of last line read from in */
11125 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
11126 FILE *in; /* Read commands from this stream */
11127 FILE *out; /* Write results here */
11128 FILE *traceOut; /* Output for sqlite3_trace() */
11129 int nErr; /* Number of errors seen */
11130 int mode; /* An output mode setting */
11131 int modePrior; /* Saved mode */
11132 int cMode; /* temporary output mode for the current query */
11133 int normalMode; /* Output mode before ".explain on" */
11134 int writableSchema; /* True if PRAGMA writable_schema=ON */
11135 int showHeader; /* True to show column names in List or Column mode */
11136 int nCheck; /* Number of ".check" commands run */
11137 unsigned nProgress; /* Number of progress callbacks encountered */
11138 unsigned mxProgress; /* Maximum progress callbacks before failing */
11139 unsigned flgProgress; /* Flags for the progress callback */
11140 unsigned shellFlgs; /* Various flags */
11141 unsigned priorShFlgs; /* Saved copy of flags */
11142 sqlite3_int64 szMax; /* --maxsize argument to .open */
11143 char *zDestTable; /* Name of destination table when MODE_Insert */
11144 char *zTempFile; /* Temporary file that might need deleting */
11145 char zTestcase[30]; /* Name of current test case */
11146 char colSeparator[20]; /* Column separator character for several modes */
11147 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
11148 char colSepPrior[20]; /* Saved column separator */
11149 char rowSepPrior[20]; /* Saved row separator */
11150 int *colWidth; /* Requested width of each column in columnar modes */
11151 int *actualWidth; /* Actual width of each column */
11152 int nWidth; /* Number of slots in colWidth[] and actualWidth[] */
11153 char nullValue[20]; /* The text to print when a NULL comes back from
11155 char outfile[FILENAME_MAX]; /* Filename for *out */
11156 const char *zDbFilename; /* name of the database file */
11157 char *zFreeOnClose; /* Filename to free when closing */
11158 const char *zVfs; /* Name of VFS to use */
11159 sqlite3_stmt *pStmt; /* Current statement if any. */
11160 FILE *pLog; /* Write log output here */
11161 int *aiIndent; /* Array of indents used in MODE_Explain */
11162 int nIndent; /* Size of array aiIndent[] */
11163 int iIndent; /* Index of current op in aiIndent[] */
11164 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
11165 #if defined(SQLITE_ENABLE_SESSION)
11166 int nSession; /* Number of active sessions */
11167 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
11169 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
11173 /* Allowed values for ShellState.autoEQP
11175 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
11176 #define AUTOEQP_on 1 /* Automatic EQP is on */
11177 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
11178 #define AUTOEQP_full 3 /* Show full EXPLAIN */
11180 /* Allowed values for ShellState.openMode
11182 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
11183 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
11184 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
11185 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
11186 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
11187 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
11188 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
11190 /* Allowed values for ShellState.eTraceType
11192 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
11193 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
11194 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
11196 /* Bits in the ShellState.flgProgress variable */
11197 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
11198 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
11199 ** callback limit is reached, and for each
11200 ** top-level SQL statement */
11201 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
11204 ** These are the allowed shellFlgs values
11206 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
11207 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
11208 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
11209 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
11210 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
11211 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
11212 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
11213 #define SHFLG_HeaderSet 0x00000080 /* .header has been used */
11214 #define SHFLG_DumpDataOnly 0x00000100 /* .dump show data only */
11215 #define SHFLG_DumpNoSys 0x00000200 /* .dump omits system tables */
11218 ** Macros for testing and setting shellFlgs
11220 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
11221 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
11222 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
11225 ** These are the allowed modes.
11227 #define MODE_Line 0 /* One column per line. Blank line between records */
11228 #define MODE_Column 1 /* One record per line in neat columns */
11229 #define MODE_List 2 /* One record per line with a separator */
11230 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
11231 #define MODE_Html 4 /* Generate an XHTML table */
11232 #define MODE_Insert 5 /* Generate SQL "insert" statements */
11233 #define MODE_Quote 6 /* Quote values as for SQL */
11234 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
11235 #define MODE_Csv 8 /* Quote strings, numbers are plain */
11236 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
11237 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
11238 #define MODE_Pretty 11 /* Pretty-print schemas */
11239 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
11240 #define MODE_Json 13 /* Output JSON */
11241 #define MODE_Markdown 14 /* Markdown formatting */
11242 #define MODE_Table 15 /* MySQL-style table formatting */
11243 #define MODE_Box 16 /* Unicode box-drawing characters */
11245 static const char *modeDescr[] = {
11266 ** These are the column/row/line separators used by the various
11267 ** import/export modes.
11269 #define SEP_Column "|"
11270 #define SEP_Row "\n"
11271 #define SEP_Tab "\t"
11272 #define SEP_Space " "
11273 #define SEP_Comma ","
11274 #define SEP_CrLf "\r\n"
11275 #define SEP_Unit "\x1F"
11276 #define SEP_Record "\x1E"
11279 ** A callback for the sqlite3_log() interface.
11281 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
11282 ShellState *p = (ShellState*)pArg;
11283 if( p->pLog==0 ) return;
11284 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
11289 ** SQL function: shell_putsnl(X)
11291 ** Write the text X to the screen (or whatever output is being directed)
11292 ** adding a newline at the end, and then return X.
11294 static void shellPutsFunc(
11295 sqlite3_context *pCtx,
11297 sqlite3_value **apVal
11299 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
11301 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
11302 sqlite3_result_value(pCtx, apVal[0]);
11306 ** SQL function: edit(VALUE)
11307 ** edit(VALUE,EDITOR)
11311 ** (1) Write VALUE into a temporary file.
11312 ** (2) Run program EDITOR on that temporary file.
11313 ** (3) Read the temporary file back and return its content as the result.
11314 ** (4) Delete the temporary file
11316 ** If the EDITOR argument is omitted, use the value in the VISUAL
11317 ** environment variable. If still there is no EDITOR, through an error.
11319 ** Also throw an error if the EDITOR program returns a non-zero exit code.
11321 #ifndef SQLITE_NOHAVE_SYSTEM
11322 static void editFunc(
11323 sqlite3_context *context,
11325 sqlite3_value **argv
11327 const char *zEditor;
11328 char *zTempFile = 0;
11337 unsigned char *p = 0;
11340 zEditor = (const char*)sqlite3_value_text(argv[1]);
11342 zEditor = getenv("VISUAL");
11345 sqlite3_result_error(context, "no editor for edit()", -1);
11348 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
11349 sqlite3_result_error(context, "NULL input to edit()", -1);
11352 db = sqlite3_context_db_handle(context);
11354 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
11355 if( zTempFile==0 ){
11356 sqlite3_uint64 r = 0;
11357 sqlite3_randomness(sizeof(r), &r);
11358 zTempFile = sqlite3_mprintf("temp%llx", r);
11359 if( zTempFile==0 ){
11360 sqlite3_result_error_nomem(context);
11364 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
11365 /* When writing the file to be edited, do \n to \r\n conversions on systems
11366 ** that want \r\n line endings */
11367 f = fopen(zTempFile, bBin ? "wb" : "w");
11369 sqlite3_result_error(context, "edit() cannot open temp file", -1);
11370 goto edit_func_end;
11372 sz = sqlite3_value_bytes(argv[0]);
11374 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
11376 const char *z = (const char*)sqlite3_value_text(argv[0]);
11377 /* Remember whether or not the value originally contained \r\n */
11378 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
11379 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
11384 sqlite3_result_error(context, "edit() could not write the whole file", -1);
11385 goto edit_func_end;
11387 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
11389 sqlite3_result_error_nomem(context);
11390 goto edit_func_end;
11393 sqlite3_free(zCmd);
11395 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
11396 goto edit_func_end;
11398 f = fopen(zTempFile, "rb");
11400 sqlite3_result_error(context,
11401 "edit() cannot reopen temp file after edit", -1);
11402 goto edit_func_end;
11404 fseek(f, 0, SEEK_END);
11407 p = sqlite3_malloc64( sz+1 );
11409 sqlite3_result_error_nomem(context);
11410 goto edit_func_end;
11412 x = fread(p, 1, (size_t)sz, f);
11416 sqlite3_result_error(context, "could not read back the whole file", -1);
11417 goto edit_func_end;
11420 sqlite3_result_blob64(context, p, sz, sqlite3_free);
11422 sqlite3_int64 i, j;
11424 /* If the original contains \r\n then do no conversions back to \n */
11427 /* If the file did not originally contain \r\n then convert any new
11428 ** \r\n back into \n */
11429 for(i=j=0; i<sz; i++){
11430 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
11436 sqlite3_result_text64(context, (const char*)p, sz,
11437 sqlite3_free, SQLITE_UTF8);
11444 sqlite3_free(zTempFile);
11447 #endif /* SQLITE_NOHAVE_SYSTEM */
11450 ** Save or restore the current output mode
11452 static void outputModePush(ShellState *p){
11453 p->modePrior = p->mode;
11454 p->priorShFlgs = p->shellFlgs;
11455 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
11456 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
11458 static void outputModePop(ShellState *p){
11459 p->mode = p->modePrior;
11460 p->shellFlgs = p->priorShFlgs;
11461 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
11462 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
11466 ** Output the given string as a hex-encoded blob (eg. X'1234' )
11468 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
11470 char *zBlob = (char *)pBlob;
11471 raw_printf(out,"X'");
11472 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
11473 raw_printf(out,"'");
11477 ** Find a string that is not found anywhere in z[]. Return a pointer
11480 ** Try to use zA and zB first. If both of those are already found in z[]
11481 ** then make up some string and store it in the buffer zBuf.
11483 static const char *unused_string(
11484 const char *z, /* Result must not appear anywhere in z */
11485 const char *zA, const char *zB, /* Try these first */
11486 char *zBuf /* Space to store a generated string */
11489 if( strstr(z, zA)==0 ) return zA;
11490 if( strstr(z, zB)==0 ) return zB;
11492 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
11493 }while( strstr(z,zBuf)!=0 );
11498 ** Output the given string as a quoted string using SQL quoting conventions.
11500 ** See also: output_quoted_escaped_string()
11502 static void output_quoted_string(FILE *out, const char *z){
11505 setBinaryMode(out, 1);
11506 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11508 utf8_printf(out,"'%s'",z);
11510 raw_printf(out, "'");
11512 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
11515 utf8_printf(out, "%.*s", i, z);
11519 raw_printf(out, "'");
11527 raw_printf(out, "'");
11529 setTextMode(out, 1);
11533 ** Output the given string as a quoted string using SQL quoting conventions.
11534 ** Additionallly , escape the "\n" and "\r" characters so that they do not
11535 ** get corrupted by end-of-line translation facilities in some operating
11538 ** This is like output_quoted_string() but with the addition of the \r\n
11539 ** escape mechanism.
11541 static void output_quoted_escaped_string(FILE *out, const char *z){
11544 setBinaryMode(out, 1);
11545 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
11547 utf8_printf(out,"'%s'",z);
11549 const char *zNL = 0;
11550 const char *zCR = 0;
11553 char zBuf1[20], zBuf2[20];
11554 for(i=0; z[i]; i++){
11555 if( z[i]=='\n' ) nNL++;
11556 if( z[i]=='\r' ) nCR++;
11559 raw_printf(out, "replace(");
11560 zNL = unused_string(z, "\\n", "\\012", zBuf1);
11563 raw_printf(out, "replace(");
11564 zCR = unused_string(z, "\\r", "\\015", zBuf2);
11566 raw_printf(out, "'");
11568 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
11571 utf8_printf(out, "%.*s", i, z);
11575 raw_printf(out, "'");
11583 raw_printf(out, "%s", zNL);
11586 raw_printf(out, "%s", zCR);
11588 raw_printf(out, "'");
11590 raw_printf(out, ",'%s',char(13))", zCR);
11593 raw_printf(out, ",'%s',char(10))", zNL);
11596 setTextMode(out, 1);
11600 ** Output the given string as a quoted according to C or TCL quoting rules.
11602 static void output_c_string(FILE *out, const char *z){
11605 while( (c = *(z++))!=0 ){
11609 }else if( c=='"' ){
11612 }else if( c=='\t' ){
11615 }else if( c=='\n' ){
11618 }else if( c=='\r' ){
11621 }else if( !isprint(c&0xff) ){
11622 raw_printf(out, "\\%03o", c&0xff);
11631 ** Output the given string as a quoted according to JSON quoting rules.
11633 static void output_json_string(FILE *out, const char *z, int n){
11635 if( n<0 ) n = (int)strlen(z);
11639 if( c=='\\' || c=='"' ){
11642 }else if( c<=0x1f ){
11646 }else if( c=='\f' ){
11648 }else if( c=='\n' ){
11650 }else if( c=='\r' ){
11652 }else if( c=='\t' ){
11655 raw_printf(out, "u%04x",c);
11665 ** Output the given string with characters that are special to
11668 static void output_html_string(FILE *out, const char *z){
11680 utf8_printf(out,"%.*s",i,z);
11683 raw_printf(out,"<");
11684 }else if( z[i]=='&' ){
11685 raw_printf(out,"&");
11686 }else if( z[i]=='>' ){
11687 raw_printf(out,">");
11688 }else if( z[i]=='\"' ){
11689 raw_printf(out,""");
11690 }else if( z[i]=='\'' ){
11691 raw_printf(out,"'");
11700 ** If a field contains any character identified by a 1 in the following
11701 ** array, then the string must be quoted for CSV.
11703 static const char needCsvQuote[] = {
11704 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11705 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11706 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
11707 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11708 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11710 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
11711 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
11712 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11713 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11714 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
11715 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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,
11723 ** Output a single term of CSV. Actually, p->colSeparator is used for
11724 ** the separator, which may or may not be a comma. p->nullValue is
11725 ** the null value. Strings are quoted if necessary. The separator
11726 ** is only issued if bSep is true.
11728 static void output_csv(ShellState *p, const char *z, int bSep){
11729 FILE *out = p->out;
11731 utf8_printf(out,"%s",p->nullValue);
11734 int nSep = strlen30(p->colSeparator);
11735 for(i=0; z[i]; i++){
11736 if( needCsvQuote[((unsigned char*)z)[i]]
11737 || (z[i]==p->colSeparator[0] &&
11738 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
11744 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
11745 utf8_printf(out, "%s", zQuoted);
11746 sqlite3_free(zQuoted);
11748 utf8_printf(out, "%s", z);
11752 utf8_printf(p->out, "%s", p->colSeparator);
11757 ** This routine runs when the user presses Ctrl-C
11759 static void interrupt_handler(int NotUsed){
11760 UNUSED_PARAMETER(NotUsed);
11762 if( seenInterrupt>2 ) exit(1);
11763 if( globalDb ) sqlite3_interrupt(globalDb);
11766 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
11768 ** This routine runs for console events (e.g. Ctrl-C) on Win32
11770 static BOOL WINAPI ConsoleCtrlHandler(
11771 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
11773 if( dwCtrlType==CTRL_C_EVENT ){
11774 interrupt_handler(0);
11781 #ifndef SQLITE_OMIT_AUTHORIZATION
11783 ** When the ".auth ON" is set, the following authorizer callback is
11784 ** invoked. It always returns SQLITE_OK.
11786 static int shellAuth(
11794 ShellState *p = (ShellState*)pClientData;
11795 static const char *azAction[] = { 0,
11796 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
11797 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
11798 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
11799 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
11800 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
11801 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
11802 "PRAGMA", "READ", "SELECT",
11803 "TRANSACTION", "UPDATE", "ATTACH",
11804 "DETACH", "ALTER_TABLE", "REINDEX",
11805 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
11806 "FUNCTION", "SAVEPOINT", "RECURSIVE"
11814 utf8_printf(p->out, "authorizer: %s", azAction[op]);
11815 for(i=0; i<4; i++){
11816 raw_printf(p->out, " ");
11818 output_c_string(p->out, az[i]);
11820 raw_printf(p->out, "NULL");
11823 raw_printf(p->out, "\n");
11829 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
11831 ** This routine converts some CREATE TABLE statements for shadow tables
11832 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
11834 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
11836 if( zTail==0 ) return;
11837 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
11838 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
11840 utf8_printf(out, "%s%s", z, zTail);
11843 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
11846 printSchemaLine(out, z, zTail);
11851 ** Return true if string z[] has nothing but whitespace and comments to the
11852 ** end of the first line.
11854 static int wsToEol(const char *z){
11856 for(i=0; z[i]; i++){
11857 if( z[i]=='\n' ) return 1;
11858 if( IsSpace(z[i]) ) continue;
11859 if( z[i]=='-' && z[i+1]=='-' ) return 1;
11866 ** Add a new entry to the EXPLAIN QUERY PLAN data
11868 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
11870 int nText = strlen30(zText);
11871 if( p->autoEQPtest ){
11872 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
11874 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
11875 if( pNew==0 ) shell_out_of_memory();
11876 pNew->iEqpId = iEqpId;
11877 pNew->iParentId = p2;
11878 memcpy(pNew->zText, zText, nText+1);
11880 if( p->sGraph.pLast ){
11881 p->sGraph.pLast->pNext = pNew;
11883 p->sGraph.pRow = pNew;
11885 p->sGraph.pLast = pNew;
11889 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
11892 static void eqp_reset(ShellState *p){
11893 EQPGraphRow *pRow, *pNext;
11894 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
11895 pNext = pRow->pNext;
11896 sqlite3_free(pRow);
11898 memset(&p->sGraph, 0, sizeof(p->sGraph));
11901 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
11902 ** pOld, or return the first such line if pOld is NULL
11904 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
11905 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
11906 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
11910 /* Render a single level of the graph that has iEqpId as its parent. Called
11911 ** recursively to render sublevels.
11913 static void eqp_render_level(ShellState *p, int iEqpId){
11914 EQPGraphRow *pRow, *pNext;
11915 int n = strlen30(p->sGraph.zPrefix);
11917 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
11918 pNext = eqp_next_row(p, iEqpId, pRow);
11920 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
11921 pNext ? "|--" : "`--", z);
11922 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
11923 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
11924 eqp_render_level(p, pRow->iEqpId);
11925 p->sGraph.zPrefix[n] = 0;
11931 ** Display and reset the EXPLAIN QUERY PLAN data
11933 static void eqp_render(ShellState *p){
11934 EQPGraphRow *pRow = p->sGraph.pRow;
11936 if( pRow->zText[0]=='-' ){
11937 if( pRow->pNext==0 ){
11941 utf8_printf(p->out, "%s\n", pRow->zText+3);
11942 p->sGraph.pRow = pRow->pNext;
11943 sqlite3_free(pRow);
11945 utf8_printf(p->out, "QUERY PLAN\n");
11947 p->sGraph.zPrefix[0] = 0;
11948 eqp_render_level(p, 0);
11953 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
11955 ** Progress handler callback.
11957 static int progress_handler(void *pClientData) {
11958 ShellState *p = (ShellState*)pClientData;
11960 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
11961 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
11962 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
11963 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
11966 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
11967 raw_printf(p->out, "Progress %u\n", p->nProgress);
11971 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
11976 static void print_dashes(FILE *out, int N){
11977 const char zDash[] = "--------------------------------------------------";
11978 const int nDash = sizeof(zDash) - 1;
11983 raw_printf(out, "%.*s", N, zDash);
11987 ** Print a markdown or table-style row separator using ascii-art
11989 static void print_row_separator(
11996 fputs(zSep, p->out);
11997 print_dashes(p->out, p->actualWidth[0]+2);
11998 for(i=1; i<nArg; i++){
11999 fputs(zSep, p->out);
12000 print_dashes(p->out, p->actualWidth[i]+2);
12002 fputs(zSep, p->out);
12004 fputs("\n", p->out);
12008 ** This is the callback routine that the shell
12009 ** invokes for each row of a query result.
12011 static int shell_callback(
12013 int nArg, /* Number of result columns */
12014 char **azArg, /* Text of each result column */
12015 char **azCol, /* Column names */
12016 int *aiType /* Column types. Might be NULL */
12019 ShellState *p = (ShellState*)pArg;
12021 if( azArg==0 ) return 0;
12022 switch( p->cMode ){
12025 if( azArg==0 ) break;
12026 for(i=0; i<nArg; i++){
12027 int len = strlen30(azCol[i] ? azCol[i] : "");
12028 if( len>w ) w = len;
12030 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
12031 for(i=0; i<nArg; i++){
12032 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
12033 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
12037 case MODE_Explain: {
12038 static const int aExplainWidth[] = {4, 13, 4, 4, 4, 13, 2, 13};
12039 if( nArg>ArraySize(aExplainWidth) ){
12040 nArg = ArraySize(aExplainWidth);
12043 for(i=0; i<nArg; i++){
12044 int w = aExplainWidth[i];
12045 utf8_width_print(p->out, w, azCol[i]);
12046 fputs(i==nArg-1 ? "\n" : " ", p->out);
12048 for(i=0; i<nArg; i++){
12049 int w = aExplainWidth[i];
12050 print_dashes(p->out, w);
12051 fputs(i==nArg-1 ? "\n" : " ", p->out);
12054 if( azArg==0 ) break;
12055 for(i=0; i<nArg; i++){
12056 int w = aExplainWidth[i];
12057 if( azArg[i] && strlenChar(azArg[i])>w ){
12058 w = strlenChar(azArg[i]);
12060 if( i==1 && p->aiIndent && p->pStmt ){
12061 if( p->iIndent<p->nIndent ){
12062 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
12066 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
12067 fputs(i==nArg-1 ? "\n" : " ", p->out);
12071 case MODE_Semi: { /* .schema and .fullschema output */
12072 printSchemaLine(p->out, azArg[0], ";\n");
12075 case MODE_Pretty: { /* .schema and .fullschema with --indent */
12083 if( azArg[0]==0 ) break;
12084 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
12085 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
12087 utf8_printf(p->out, "%s;\n", azArg[0]);
12090 z = sqlite3_mprintf("%s", azArg[0]);
12092 for(i=0; IsSpace(z[i]); i++){}
12093 for(; (c = z[i])!=0; i++){
12095 if( z[j-1]=='\r' ) z[j-1] = '\n';
12096 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
12097 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
12102 while( j>0 && IsSpace(z[j-1]) ){ j--; }
12104 if( strlen30(z)>=79 ){
12105 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
12108 }else if( c=='"' || c=='\'' || c=='`' ){
12110 }else if( c=='[' ){
12112 }else if( c=='-' && z[i+1]=='-' ){
12114 }else if( c=='(' ){
12116 }else if( c==')' ){
12118 if( nLine>0 && nParen==0 && j>0 ){
12119 printSchemaLineN(p->out, z, j, "\n");
12124 if( nParen==1 && cEnd==0
12125 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
12128 printSchemaLineN(p->out, z, j, "\n ");
12131 while( IsSpace(z[i+1]) ){ i++; }
12136 printSchemaLine(p->out, z, ";\n");
12141 if( p->cnt++==0 && p->showHeader ){
12142 for(i=0; i<nArg; i++){
12143 utf8_printf(p->out,"%s%s",azCol[i],
12144 i==nArg-1 ? p->rowSeparator : p->colSeparator);
12147 if( azArg==0 ) break;
12148 for(i=0; i<nArg; i++){
12149 char *z = azArg[i];
12150 if( z==0 ) z = p->nullValue;
12151 utf8_printf(p->out, "%s", z);
12153 utf8_printf(p->out, "%s", p->colSeparator);
12155 utf8_printf(p->out, "%s", p->rowSeparator);
12161 if( p->cnt++==0 && p->showHeader ){
12162 raw_printf(p->out,"<TR>");
12163 for(i=0; i<nArg; i++){
12164 raw_printf(p->out,"<TH>");
12165 output_html_string(p->out, azCol[i]);
12166 raw_printf(p->out,"</TH>\n");
12168 raw_printf(p->out,"</TR>\n");
12170 if( azArg==0 ) break;
12171 raw_printf(p->out,"<TR>");
12172 for(i=0; i<nArg; i++){
12173 raw_printf(p->out,"<TD>");
12174 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12175 raw_printf(p->out,"</TD>\n");
12177 raw_printf(p->out,"</TR>\n");
12181 if( p->cnt++==0 && p->showHeader ){
12182 for(i=0; i<nArg; i++){
12183 output_c_string(p->out,azCol[i] ? azCol[i] : "");
12184 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12186 utf8_printf(p->out, "%s", p->rowSeparator);
12188 if( azArg==0 ) break;
12189 for(i=0; i<nArg; i++){
12190 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
12191 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
12193 utf8_printf(p->out, "%s", p->rowSeparator);
12197 setBinaryMode(p->out, 1);
12198 if( p->cnt++==0 && p->showHeader ){
12199 for(i=0; i<nArg; i++){
12200 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
12202 utf8_printf(p->out, "%s", p->rowSeparator);
12205 for(i=0; i<nArg; i++){
12206 output_csv(p, azArg[i], i<nArg-1);
12208 utf8_printf(p->out, "%s", p->rowSeparator);
12210 setTextMode(p->out, 1);
12213 case MODE_Insert: {
12214 if( azArg==0 ) break;
12215 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
12216 if( p->showHeader ){
12217 raw_printf(p->out,"(");
12218 for(i=0; i<nArg; i++){
12219 if( i>0 ) raw_printf(p->out, ",");
12220 if( quoteChar(azCol[i]) ){
12221 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
12222 utf8_printf(p->out, "%s", z);
12225 raw_printf(p->out, "%s", azCol[i]);
12228 raw_printf(p->out,")");
12231 for(i=0; i<nArg; i++){
12232 raw_printf(p->out, i>0 ? "," : " VALUES(");
12233 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12234 utf8_printf(p->out,"NULL");
12235 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12236 if( ShellHasFlag(p, SHFLG_Newlines) ){
12237 output_quoted_string(p->out, azArg[i]);
12239 output_quoted_escaped_string(p->out, azArg[i]);
12241 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12242 utf8_printf(p->out,"%s", azArg[i]);
12243 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12245 double r = sqlite3_column_double(p->pStmt, i);
12247 memcpy(&ur,&r,sizeof(r));
12248 if( ur==0x7ff0000000000000LL ){
12249 raw_printf(p->out, "1e999");
12250 }else if( ur==0xfff0000000000000LL ){
12251 raw_printf(p->out, "-1e999");
12253 sqlite3_snprintf(50,z,"%!.20g", r);
12254 raw_printf(p->out, "%s", z);
12256 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12257 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12258 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12259 output_hex_blob(p->out, pBlob, nBlob);
12260 }else if( isNumber(azArg[i], 0) ){
12261 utf8_printf(p->out,"%s", azArg[i]);
12262 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
12263 output_quoted_string(p->out, azArg[i]);
12265 output_quoted_escaped_string(p->out, azArg[i]);
12268 raw_printf(p->out,");\n");
12272 if( azArg==0 ) break;
12274 fputs("[{", p->out);
12276 fputs(",\n{", p->out);
12279 for(i=0; i<nArg; i++){
12280 output_json_string(p->out, azCol[i], -1);
12282 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12283 fputs("null",p->out);
12284 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12286 double r = sqlite3_column_double(p->pStmt, i);
12288 memcpy(&ur,&r,sizeof(r));
12289 if( ur==0x7ff0000000000000LL ){
12290 raw_printf(p->out, "1e999");
12291 }else if( ur==0xfff0000000000000LL ){
12292 raw_printf(p->out, "-1e999");
12294 sqlite3_snprintf(50,z,"%!.20g", r);
12295 raw_printf(p->out, "%s", z);
12297 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12298 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12299 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12300 output_json_string(p->out, pBlob, nBlob);
12301 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12302 output_json_string(p->out, azArg[i], -1);
12304 utf8_printf(p->out,"%s", azArg[i]);
12314 if( azArg==0 ) break;
12315 if( p->cnt==0 && p->showHeader ){
12316 for(i=0; i<nArg; i++){
12317 if( i>0 ) fputs(p->colSeparator, p->out);
12318 output_quoted_string(p->out, azCol[i]);
12320 fputs(p->rowSeparator, p->out);
12323 for(i=0; i<nArg; i++){
12324 if( i>0 ) fputs(p->colSeparator, p->out);
12325 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
12326 utf8_printf(p->out,"NULL");
12327 }else if( aiType && aiType[i]==SQLITE_TEXT ){
12328 output_quoted_string(p->out, azArg[i]);
12329 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
12330 utf8_printf(p->out,"%s", azArg[i]);
12331 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
12333 double r = sqlite3_column_double(p->pStmt, i);
12334 sqlite3_snprintf(50,z,"%!.20g", r);
12335 raw_printf(p->out, "%s", z);
12336 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
12337 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
12338 int nBlob = sqlite3_column_bytes(p->pStmt, i);
12339 output_hex_blob(p->out, pBlob, nBlob);
12340 }else if( isNumber(azArg[i], 0) ){
12341 utf8_printf(p->out,"%s", azArg[i]);
12343 output_quoted_string(p->out, azArg[i]);
12346 fputs(p->rowSeparator, p->out);
12350 if( p->cnt++==0 && p->showHeader ){
12351 for(i=0; i<nArg; i++){
12352 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12353 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
12355 utf8_printf(p->out, "%s", p->rowSeparator);
12357 if( azArg==0 ) break;
12358 for(i=0; i<nArg; i++){
12359 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
12360 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
12362 utf8_printf(p->out, "%s", p->rowSeparator);
12366 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
12374 ** This is the callback routine that the SQLite library
12375 ** invokes for each row of a query result.
12377 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
12378 /* since we don't have type info, call the shell_callback with a NULL value */
12379 return shell_callback(pArg, nArg, azArg, azCol, NULL);
12383 ** This is the callback routine from sqlite3_exec() that appends all
12384 ** output onto the end of a ShellText object.
12386 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
12387 ShellText *p = (ShellText*)pArg;
12389 UNUSED_PARAMETER(az);
12390 if( azArg==0 ) return 0;
12391 if( p->n ) appendText(p, "|", 0);
12392 for(i=0; i<nArg; i++){
12393 if( i ) appendText(p, ",", 0);
12394 if( azArg[i] ) appendText(p, azArg[i], 0);
12400 ** Generate an appropriate SELFTEST table in the main database.
12402 static void createSelftestTable(ShellState *p){
12404 sqlite3_exec(p->db,
12405 "SAVEPOINT selftest_init;\n"
12406 "CREATE TABLE IF NOT EXISTS selftest(\n"
12407 " tno INTEGER PRIMARY KEY,\n" /* Test number */
12408 " op TEXT,\n" /* Operator: memo run */
12409 " cmd TEXT,\n" /* Command text */
12410 " ans TEXT\n" /* Desired answer */
12412 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
12413 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
12414 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
12415 " 'memo','Tests generated by --init');\n"
12416 "INSERT INTO [_shell$self]\n"
12418 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
12419 "FROM sqlite_schema ORDER BY 2'',224))',\n"
12420 " hex(sha3_query('SELECT type,name,tbl_name,sql "
12421 "FROM sqlite_schema ORDER BY 2',224));\n"
12422 "INSERT INTO [_shell$self]\n"
12424 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
12425 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
12426 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
12428 " SELECT name FROM sqlite_schema\n"
12429 " WHERE type='table'\n"
12430 " AND name<>'selftest'\n"
12431 " AND coalesce(rootpage,0)>0\n"
12433 " ORDER BY name;\n"
12434 "INSERT INTO [_shell$self]\n"
12435 " VALUES('run','PRAGMA integrity_check','ok');\n"
12436 "INSERT INTO selftest(tno,op,cmd,ans)"
12437 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
12438 "DROP TABLE [_shell$self];"
12441 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
12442 sqlite3_free(zErrMsg);
12444 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
12449 ** Set the destination table field of the ShellState structure to
12450 ** the name of the table given. Escape any quote characters in the
12453 static void set_table_name(ShellState *p, const char *zName){
12458 if( p->zDestTable ){
12459 free(p->zDestTable);
12462 if( zName==0 ) return;
12463 cQuote = quoteChar(zName);
12464 n = strlen30(zName);
12465 if( cQuote ) n += n+2;
12466 z = p->zDestTable = malloc( n+1 );
12467 if( z==0 ) shell_out_of_memory();
12469 if( cQuote ) z[n++] = cQuote;
12470 for(i=0; zName[i]; i++){
12472 if( zName[i]==cQuote ) z[n++] = cQuote;
12474 if( cQuote ) z[n++] = cQuote;
12480 ** Execute a query statement that will generate SQL output. Print
12481 ** the result columns, comma-separated, on a line and then add a
12482 ** semicolon terminator to the end of that line.
12484 ** If the number of columns is 1 and that column contains text "--"
12485 ** then write the semicolon on a separate line. That way, if a
12486 ** "--" comment occurs at the end of the statement, the comment
12487 ** won't consume the semicolon terminator.
12489 static int run_table_dump_query(
12490 ShellState *p, /* Query context */
12491 const char *zSelect /* SELECT statement to extract content */
12493 sqlite3_stmt *pSelect;
12498 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
12499 if( rc!=SQLITE_OK || !pSelect ){
12500 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12501 sqlite3_errmsg(p->db));
12502 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12505 rc = sqlite3_step(pSelect);
12506 nResult = sqlite3_column_count(pSelect);
12507 while( rc==SQLITE_ROW ){
12508 z = (const char*)sqlite3_column_text(pSelect, 0);
12509 utf8_printf(p->out, "%s", z);
12510 for(i=1; i<nResult; i++){
12511 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
12514 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
12516 raw_printf(p->out, "\n;\n");
12518 raw_printf(p->out, ";\n");
12520 rc = sqlite3_step(pSelect);
12522 rc = sqlite3_finalize(pSelect);
12523 if( rc!=SQLITE_OK ){
12524 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
12525 sqlite3_errmsg(p->db));
12526 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
12532 ** Allocate space and save off current error string.
12534 static char *save_err_msg(
12535 sqlite3 *db /* Database to query */
12537 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
12538 char *zErrMsg = sqlite3_malloc64(nErrMsg);
12540 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
12547 ** Attempt to display I/O stats on Linux using /proc/PID/io
12549 static void displayLinuxIoStats(FILE *out){
12552 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
12553 in = fopen(z, "rb");
12554 if( in==0 ) return;
12555 while( fgets(z, sizeof(z), in)!=0 ){
12556 static const struct {
12557 const char *zPattern;
12560 { "rchar: ", "Bytes received by read():" },
12561 { "wchar: ", "Bytes sent to write():" },
12562 { "syscr: ", "Read() system calls:" },
12563 { "syscw: ", "Write() system calls:" },
12564 { "read_bytes: ", "Bytes read from storage:" },
12565 { "write_bytes: ", "Bytes written to storage:" },
12566 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
12569 for(i=0; i<ArraySize(aTrans); i++){
12570 int n = strlen30(aTrans[i].zPattern);
12571 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
12572 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
12582 ** Display a single line of status using 64-bit values.
12584 static void displayStatLine(
12585 ShellState *p, /* The shell context */
12586 char *zLabel, /* Label for this one line */
12587 char *zFormat, /* Format for the result */
12588 int iStatusCtrl, /* Which status to display */
12589 int bReset /* True to reset the stats */
12591 sqlite3_int64 iCur = -1;
12592 sqlite3_int64 iHiwtr = -1;
12595 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
12596 for(i=0, nPercent=0; zFormat[i]; i++){
12597 if( zFormat[i]=='%' ) nPercent++;
12600 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
12602 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
12604 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
12608 ** Display memory stats.
12610 static int display_stats(
12611 sqlite3 *db, /* Database to query */
12612 ShellState *pArg, /* Pointer to ShellState */
12613 int bReset /* True to reset the stats */
12618 if( pArg==0 || pArg->out==0 ) return 0;
12621 if( pArg->pStmt && (pArg->statsOn & 2) ){
12623 sqlite3_stmt *pStmt = pArg->pStmt;
12625 nCol = sqlite3_column_count(pStmt);
12626 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
12627 for(i=0; i<nCol; i++){
12628 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
12629 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
12630 #ifndef SQLITE_OMIT_DECLTYPE
12631 sqlite3_snprintf(30, z+x, "declared type:");
12632 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
12634 #ifdef SQLITE_ENABLE_COLUMN_METADATA
12635 sqlite3_snprintf(30, z+x, "database name:");
12636 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
12637 sqlite3_snprintf(30, z+x, "table name:");
12638 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
12639 sqlite3_snprintf(30, z+x, "origin name:");
12640 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
12645 displayStatLine(pArg, "Memory Used:",
12646 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
12647 displayStatLine(pArg, "Number of Outstanding Allocations:",
12648 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
12649 if( pArg->shellFlgs & SHFLG_Pagecache ){
12650 displayStatLine(pArg, "Number of Pcache Pages Used:",
12651 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
12653 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
12654 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
12655 displayStatLine(pArg, "Largest Allocation:",
12656 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
12657 displayStatLine(pArg, "Largest Pcache Allocation:",
12658 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
12659 #ifdef YYTRACKMAXSTACKDEPTH
12660 displayStatLine(pArg, "Deepest Parser Stack:",
12661 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
12665 if( pArg->shellFlgs & SHFLG_Lookaside ){
12666 iHiwtr = iCur = -1;
12667 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
12668 &iCur, &iHiwtr, bReset);
12669 raw_printf(pArg->out,
12670 "Lookaside Slots Used: %d (max %d)\n",
12672 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
12673 &iCur, &iHiwtr, bReset);
12674 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
12676 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
12677 &iCur, &iHiwtr, bReset);
12678 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
12680 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
12681 &iCur, &iHiwtr, bReset);
12682 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
12685 iHiwtr = iCur = -1;
12686 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
12687 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
12689 iHiwtr = iCur = -1;
12690 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
12691 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
12692 iHiwtr = iCur = -1;
12693 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
12694 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
12695 iHiwtr = iCur = -1;
12696 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
12697 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
12698 iHiwtr = iCur = -1;
12699 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
12700 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
12701 iHiwtr = iCur = -1;
12702 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
12703 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
12705 iHiwtr = iCur = -1;
12706 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
12707 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
12712 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
12714 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
12715 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
12716 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
12717 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
12718 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
12719 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
12720 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
12721 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
12722 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
12723 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
12724 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
12725 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
12726 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
12730 displayLinuxIoStats(pArg->out);
12733 /* Do not remove this machine readable comment: extra-stats-output-here */
12739 ** Display scan stats.
12741 static void display_scanstats(
12742 sqlite3 *db, /* Database to query */
12743 ShellState *pArg /* Pointer to ShellState */
12745 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
12746 UNUSED_PARAMETER(db);
12747 UNUSED_PARAMETER(pArg);
12750 raw_printf(pArg->out, "-------- scanstats --------\n");
12752 for(k=0; k<=mx; k++){
12753 double rEstLoop = 1.0;
12754 for(i=n=0; 1; i++){
12755 sqlite3_stmt *p = pArg->pStmt;
12756 sqlite3_int64 nLoop, nVisit;
12759 const char *zExplain;
12760 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
12763 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
12764 if( iSid>mx ) mx = iSid;
12765 if( iSid!=k ) continue;
12767 rEstLoop = (double)nLoop;
12768 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
12771 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
12772 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
12773 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
12774 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
12776 raw_printf(pArg->out,
12777 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
12778 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
12782 raw_printf(pArg->out, "---------------------------\n");
12787 ** Parameter azArray points to a zero-terminated array of strings. zStr
12788 ** points to a single nul-terminated string. Return non-zero if zStr
12789 ** is equal, according to strcmp(), to any of the strings in the array.
12790 ** Otherwise, return zero.
12792 static int str_in_array(const char *zStr, const char **azArray){
12794 for(i=0; azArray[i]; i++){
12795 if( 0==strcmp(zStr, azArray[i]) ) return 1;
12801 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
12802 ** and populate the ShellState.aiIndent[] array with the number of
12803 ** spaces each opcode should be indented before it is output.
12805 ** The indenting rules are:
12807 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
12808 ** all opcodes that occur between the p2 jump destination and the opcode
12809 ** itself by 2 spaces.
12811 ** * For each "Goto", if the jump destination is earlier in the program
12812 ** and ends on one of:
12813 ** Yield SeekGt SeekLt RowSetRead Rewind
12814 ** or if the P1 parameter is one instead of zero,
12815 ** then indent all opcodes between the earlier instruction
12816 ** and "Goto" by 2 spaces.
12818 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
12819 const char *zSql; /* The text of the SQL statement */
12820 const char *z; /* Used to check if this is an EXPLAIN */
12821 int *abYield = 0; /* True if op is an OP_Yield */
12822 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
12823 int iOp; /* Index of operation in p->aiIndent[] */
12825 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
12826 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
12828 const char *azGoto[] = { "Goto", 0 };
12830 /* Try to figure out if this is really an EXPLAIN statement. If this
12831 ** cannot be verified, return early. */
12832 if( sqlite3_column_count(pSql)!=8 ){
12833 p->cMode = p->mode;
12836 zSql = sqlite3_sql(pSql);
12837 if( zSql==0 ) return;
12838 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
12839 if( sqlite3_strnicmp(z, "explain", 7) ){
12840 p->cMode = p->mode;
12844 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
12846 int iAddr = sqlite3_column_int(pSql, 0);
12847 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
12849 /* Set p2 to the P2 field of the current opcode. Then, assuming that
12850 ** p2 is an instruction address, set variable p2op to the index of that
12851 ** instruction in the aiIndent[] array. p2 and p2op may be different if
12852 ** the current instruction is part of a sub-program generated by an
12853 ** SQL trigger or foreign key. */
12854 int p2 = sqlite3_column_int(pSql, 3);
12855 int p2op = (p2 + (iOp-iAddr));
12857 /* Grow the p->aiIndent array as required */
12860 /* Do further verfication that this is explain output. Abort if
12862 static const char *explainCols[] = {
12863 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
12865 for(jj=0; jj<ArraySize(explainCols); jj++){
12866 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
12867 p->cMode = p->mode;
12868 sqlite3_reset(pSql);
12874 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
12875 if( p->aiIndent==0 ) shell_out_of_memory();
12876 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
12877 if( abYield==0 ) shell_out_of_memory();
12879 abYield[iOp] = str_in_array(zOp, azYield);
12880 p->aiIndent[iOp] = 0;
12881 p->nIndent = iOp+1;
12883 if( str_in_array(zOp, azNext) ){
12884 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12886 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
12887 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
12889 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
12894 sqlite3_free(abYield);
12895 sqlite3_reset(pSql);
12899 ** Free the array allocated by explain_data_prepare().
12901 static void explain_data_delete(ShellState *p){
12902 sqlite3_free(p->aiIndent);
12909 ** Disable and restore .wheretrace and .selecttrace settings.
12911 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12912 extern unsigned int sqlite3_unsupported_selecttrace;
12913 static int savedSelectTrace;
12915 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12916 extern int sqlite3WhereTrace;
12917 static int savedWhereTrace;
12919 static void disable_debug_trace_modes(void){
12920 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12921 savedSelectTrace = sqlite3_unsupported_selecttrace;
12922 sqlite3_unsupported_selecttrace = 0;
12924 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12925 savedWhereTrace = sqlite3WhereTrace;
12926 sqlite3WhereTrace = 0;
12929 static void restore_debug_trace_modes(void){
12930 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
12931 sqlite3_unsupported_selecttrace = savedSelectTrace;
12933 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
12934 sqlite3WhereTrace = savedWhereTrace;
12938 /* Create the TEMP table used to store parameter bindings */
12939 static void bind_table_init(ShellState *p){
12941 int defensiveMode = 0;
12942 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
12943 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
12944 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
12945 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
12946 sqlite3_exec(p->db,
12947 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
12948 " key TEXT PRIMARY KEY,\n"
12950 ") WITHOUT ROWID;",
12952 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
12953 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
12957 ** Bind parameters on a prepared statement.
12959 ** Parameter bindings are taken from a TEMP table of the form:
12961 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
12964 ** No bindings occur if this table does not exist. The name of the table
12965 ** begins with "sqlite_" so that it will not collide with ordinary application
12966 ** tables. The table must be in the TEMP schema.
12968 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
12972 sqlite3_stmt *pQ = 0;
12974 nVar = sqlite3_bind_parameter_count(pStmt);
12975 if( nVar==0 ) return; /* Nothing to do */
12976 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
12977 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
12978 return; /* Parameter table does not exist */
12980 rc = sqlite3_prepare_v2(pArg->db,
12981 "SELECT value FROM temp.sqlite_parameters"
12982 " WHERE key=?1", -1, &pQ, 0);
12983 if( rc || pQ==0 ) return;
12984 for(i=1; i<=nVar; i++){
12986 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
12988 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
12991 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
12992 if( sqlite3_step(pQ)==SQLITE_ROW ){
12993 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
12995 sqlite3_bind_null(pStmt, i);
12999 sqlite3_finalize(pQ);
13003 ** UTF8 box-drawing characters. Imagine box lines like this:
13011 ** Each box characters has between 2 and 4 of the lines leading from
13012 ** the center. The characters are here identified by the numbers of
13013 ** their corresponding lines.
13015 #define BOX_24 "\342\224\200" /* U+2500 --- */
13016 #define BOX_13 "\342\224\202" /* U+2502 | */
13017 #define BOX_23 "\342\224\214" /* U+250c ,- */
13018 #define BOX_34 "\342\224\220" /* U+2510 -, */
13019 #define BOX_12 "\342\224\224" /* U+2514 '- */
13020 #define BOX_14 "\342\224\230" /* U+2518 -' */
13021 #define BOX_123 "\342\224\234" /* U+251c |- */
13022 #define BOX_134 "\342\224\244" /* U+2524 -| */
13023 #define BOX_234 "\342\224\254" /* U+252c -,- */
13024 #define BOX_124 "\342\224\264" /* U+2534 -'- */
13025 #define BOX_1234 "\342\224\274" /* U+253c -|- */
13027 /* Draw horizontal line N characters long using unicode box
13030 static void print_box_line(FILE *out, int N){
13031 const char zDash[] =
13032 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24
13033 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24 BOX_24;
13034 const int nDash = sizeof(zDash) - 1;
13037 utf8_printf(out, zDash);
13040 utf8_printf(out, "%.*s", N, zDash);
13044 ** Draw a horizontal separator for a MODE_Box table.
13046 static void print_box_row_separator(
13055 utf8_printf(p->out, "%s", zSep1);
13056 print_box_line(p->out, p->actualWidth[0]+2);
13057 for(i=1; i<nArg; i++){
13058 utf8_printf(p->out, "%s", zSep2);
13059 print_box_line(p->out, p->actualWidth[i]+2);
13061 utf8_printf(p->out, "%s", zSep3);
13063 fputs("\n", p->out);
13069 ** Run a prepared statement and output the result in one of the
13070 ** table-oriented formats: MODE_Column, MODE_Markdown, MODE_Table,
13073 ** This is different from ordinary exec_prepared_stmt() in that
13074 ** it has to run the entire query and gather the results into memory
13075 ** first, in order to determine column widths, before providing
13078 static void exec_prepared_stmt_columnar(
13079 ShellState *p, /* Pointer to ShellState */
13080 sqlite3_stmt *pStmt /* Statment to run */
13082 sqlite3_int64 nRow = 0;
13085 sqlite3_int64 nAlloc = 0;
13088 sqlite3_int64 i, nData;
13089 int j, nTotal, w, n;
13090 const char *colSep = 0;
13091 const char *rowSep = 0;
13093 rc = sqlite3_step(pStmt);
13094 if( rc!=SQLITE_ROW ) return;
13095 nColumn = sqlite3_column_count(pStmt);
13096 nAlloc = nColumn*4;
13097 azData = sqlite3_malloc64( nAlloc*sizeof(char*) );
13098 if( azData==0 ) shell_out_of_memory();
13099 for(i=0; i<nColumn; i++){
13100 azData[i] = strdup(sqlite3_column_name(pStmt,i));
13103 if( (nRow+2)*nColumn >= nAlloc ){
13105 azData = sqlite3_realloc64(azData, nAlloc*sizeof(char*));
13106 if( azData==0 ) shell_out_of_memory();
13109 for(i=0; i<nColumn; i++){
13110 z = (const char*)sqlite3_column_text(pStmt,i);
13111 azData[nRow*nColumn + i] = z ? strdup(z) : 0;
13113 }while( (rc = sqlite3_step(pStmt))==SQLITE_ROW );
13114 if( nColumn>p->nWidth ){
13115 p->colWidth = realloc(p->colWidth, nColumn*2*sizeof(int));
13116 if( p->colWidth==0 ) shell_out_of_memory();
13117 for(i=p->nWidth; i<nColumn; i++) p->colWidth[i] = 0;
13118 p->nWidth = nColumn;
13119 p->actualWidth = &p->colWidth[nColumn];
13121 memset(p->actualWidth, 0, nColumn*sizeof(int));
13122 for(i=0; i<nColumn; i++){
13123 w = p->colWidth[i];
13125 p->actualWidth[i] = w;
13127 nTotal = nColumn*(nRow+1);
13128 for(i=0; i<nTotal; i++){
13130 if( z==0 ) z = p->nullValue;
13133 if( n>p->actualWidth[j] ) p->actualWidth[j] = n;
13135 if( seenInterrupt ) goto columnar_end;
13136 switch( p->cMode ){
13137 case MODE_Column: {
13140 if( p->showHeader ){
13141 for(i=0; i<nColumn; i++){
13142 w = p->actualWidth[i];
13143 if( p->colWidth[i]<0 ) w = -w;
13144 utf8_width_print(p->out, w, azData[i]);
13145 fputs(i==nColumn-1?"\n":" ", p->out);
13147 for(i=0; i<nColumn; i++){
13148 print_dashes(p->out, p->actualWidth[i]);
13149 fputs(i==nColumn-1?"\n":" ", p->out);
13157 print_row_separator(p, nColumn, "+");
13158 fputs("| ", p->out);
13159 for(i=0; i<nColumn; i++){
13160 w = p->actualWidth[i];
13161 n = strlenChar(azData[i]);
13162 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13163 fputs(i==nColumn-1?" |\n":" | ", p->out);
13165 print_row_separator(p, nColumn, "+");
13168 case MODE_Markdown: {
13171 fputs("| ", p->out);
13172 for(i=0; i<nColumn; i++){
13173 w = p->actualWidth[i];
13174 n = strlenChar(azData[i]);
13175 utf8_printf(p->out, "%*s%s%*s", (w-n)/2, "", azData[i], (w-n+1)/2, "");
13176 fputs(i==nColumn-1?" |\n":" | ", p->out);
13178 print_row_separator(p, nColumn, "|");
13182 colSep = " " BOX_13 " ";
13183 rowSep = " " BOX_13 "\n";
13184 print_box_row_separator(p, nColumn, BOX_23, BOX_234, BOX_34);
13185 utf8_printf(p->out, BOX_13 " ");
13186 for(i=0; i<nColumn; i++){
13187 w = p->actualWidth[i];
13188 n = strlenChar(azData[i]);
13189 utf8_printf(p->out, "%*s%s%*s%s",
13190 (w-n)/2, "", azData[i], (w-n+1)/2, "",
13191 i==nColumn-1?" "BOX_13"\n":" "BOX_13" ");
13193 print_box_row_separator(p, nColumn, BOX_123, BOX_1234, BOX_134);
13197 for(i=nColumn, j=0; i<nTotal; i++, j++){
13198 if( j==0 && p->cMode!=MODE_Column ){
13199 utf8_printf(p->out, "%s", p->cMode==MODE_Box?BOX_13" ":"| ");
13202 if( z==0 ) z = p->nullValue;
13203 w = p->actualWidth[j];
13204 if( p->colWidth[j]<0 ) w = -w;
13205 utf8_width_print(p->out, w, z);
13206 if( j==nColumn-1 ){
13207 utf8_printf(p->out, "%s", rowSep);
13209 if( seenInterrupt ) goto columnar_end;
13211 utf8_printf(p->out, "%s", colSep);
13214 if( p->cMode==MODE_Table ){
13215 print_row_separator(p, nColumn, "+");
13216 }else if( p->cMode==MODE_Box ){
13217 print_box_row_separator(p, nColumn, BOX_12, BOX_124, BOX_14);
13220 if( seenInterrupt ){
13221 utf8_printf(p->out, "Interrupt\n");
13223 nData = (nRow+1)*nColumn;
13224 for(i=0; i<nData; i++) free(azData[i]);
13225 sqlite3_free(azData);
13229 ** Run a prepared statement
13231 static void exec_prepared_stmt(
13232 ShellState *pArg, /* Pointer to ShellState */
13233 sqlite3_stmt *pStmt /* Statment to run */
13237 if( pArg->cMode==MODE_Column
13238 || pArg->cMode==MODE_Table
13239 || pArg->cMode==MODE_Box
13240 || pArg->cMode==MODE_Markdown
13242 exec_prepared_stmt_columnar(pArg, pStmt);
13246 /* perform the first step. this will tell us if we
13247 ** have a result set or not and how wide it is.
13249 rc = sqlite3_step(pStmt);
13250 /* if we have a result set... */
13251 if( SQLITE_ROW == rc ){
13252 /* allocate space for col name ptr, value ptr, and type */
13253 int nCol = sqlite3_column_count(pStmt);
13254 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
13258 char **azCols = (char **)pData; /* Names of result columns */
13259 char **azVals = &azCols[nCol]; /* Results */
13260 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
13262 assert(sizeof(int) <= sizeof(char *));
13263 /* save off ptrs to column names */
13264 for(i=0; i<nCol; i++){
13265 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
13268 /* extract the data and data types */
13269 for(i=0; i<nCol; i++){
13270 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
13271 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
13274 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
13276 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
13278 break; /* from for */
13282 /* if data and types extracted successfully... */
13283 if( SQLITE_ROW == rc ){
13284 /* call the supplied callback with the result row data */
13285 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
13288 rc = sqlite3_step(pStmt);
13291 } while( SQLITE_ROW == rc );
13292 sqlite3_free(pData);
13293 if( pArg->cMode==MODE_Json ){
13294 fputs("]\n", pArg->out);
13300 #ifndef SQLITE_OMIT_VIRTUALTABLE
13302 ** This function is called to process SQL if the previous shell command
13303 ** was ".expert". It passes the SQL in the second argument directly to
13304 ** the sqlite3expert object.
13306 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13307 ** code. In this case, (*pzErr) may be set to point to a buffer containing
13308 ** an English language error message. It is the responsibility of the
13309 ** caller to eventually free this buffer using sqlite3_free().
13311 static int expertHandleSQL(
13312 ShellState *pState,
13316 assert( pState->expert.pExpert );
13317 assert( pzErr==0 || *pzErr==0 );
13318 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
13322 ** This function is called either to silently clean up the object
13323 ** created by the ".expert" command (if bCancel==1), or to generate a
13324 ** report from it and then clean it up (if bCancel==0).
13326 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
13327 ** code. In this case, (*pzErr) may be set to point to a buffer containing
13328 ** an English language error message. It is the responsibility of the
13329 ** caller to eventually free this buffer using sqlite3_free().
13331 static int expertFinish(
13332 ShellState *pState,
13336 int rc = SQLITE_OK;
13337 sqlite3expert *p = pState->expert.pExpert;
13339 assert( bCancel || pzErr==0 || *pzErr==0 );
13341 FILE *out = pState->out;
13342 int bVerbose = pState->expert.bVerbose;
13344 rc = sqlite3_expert_analyze(p, pzErr);
13345 if( rc==SQLITE_OK ){
13346 int nQuery = sqlite3_expert_count(p);
13350 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
13351 raw_printf(out, "-- Candidates -----------------------------\n");
13352 raw_printf(out, "%s\n", zCand);
13354 for(i=0; i<nQuery; i++){
13355 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
13356 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
13357 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
13358 if( zIdx==0 ) zIdx = "(no new indexes)\n";
13360 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
13361 raw_printf(out, "%s\n\n", zSql);
13363 raw_printf(out, "%s\n", zIdx);
13364 raw_printf(out, "%s\n", zEQP);
13368 sqlite3_expert_destroy(p);
13369 pState->expert.pExpert = 0;
13374 ** Implementation of ".expert" dot command.
13376 static int expertDotCommand(
13377 ShellState *pState, /* Current shell tool state */
13378 char **azArg, /* Array of arguments passed to dot command */
13379 int nArg /* Number of entries in azArg[] */
13381 int rc = SQLITE_OK;
13386 assert( pState->expert.pExpert==0 );
13387 memset(&pState->expert, 0, sizeof(ExpertInfo));
13389 for(i=1; rc==SQLITE_OK && i<nArg; i++){
13390 char *z = azArg[i];
13392 if( z[0]=='-' && z[1]=='-' ) z++;
13394 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
13395 pState->expert.bVerbose = 1;
13397 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
13399 raw_printf(stderr, "option requires an argument: %s\n", z);
13402 iSample = (int)integerValue(azArg[++i]);
13403 if( iSample<0 || iSample>100 ){
13404 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
13410 raw_printf(stderr, "unknown option: %s\n", z);
13415 if( rc==SQLITE_OK ){
13416 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
13417 if( pState->expert.pExpert==0 ){
13418 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
13421 sqlite3_expert_config(
13422 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
13429 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
13432 ** Execute a statement or set of statements. Print
13433 ** any result rows/columns depending on the current mode
13434 ** set via the supplied callback.
13436 ** This is very similar to SQLite's built-in sqlite3_exec()
13437 ** function except it takes a slightly different callback
13438 ** and callback data argument.
13440 static int shell_exec(
13441 ShellState *pArg, /* Pointer to ShellState */
13442 const char *zSql, /* SQL to be evaluated */
13443 char **pzErrMsg /* Error msg written here */
13445 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
13446 int rc = SQLITE_OK; /* Return Code */
13448 const char *zLeftover; /* Tail of unprocessed SQL */
13449 sqlite3 *db = pArg->db;
13455 #ifndef SQLITE_OMIT_VIRTUALTABLE
13456 if( pArg->expert.pExpert ){
13457 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
13458 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
13462 while( zSql[0] && (SQLITE_OK == rc) ){
13463 static const char *zStmtSql;
13464 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
13465 if( SQLITE_OK != rc ){
13467 *pzErrMsg = save_err_msg(db);
13471 /* this happens for a comment or white-space */
13473 while( IsSpace(zSql[0]) ) zSql++;
13476 zStmtSql = sqlite3_sql(pStmt);
13477 if( zStmtSql==0 ) zStmtSql = "";
13478 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
13480 /* save off the prepared statment handle and reset row count */
13482 pArg->pStmt = pStmt;
13486 /* echo the sql statement if echo on */
13487 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
13488 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
13491 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
13492 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
13493 sqlite3_stmt *pExplain;
13495 int triggerEQP = 0;
13496 disable_debug_trace_modes();
13497 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
13498 if( pArg->autoEQP>=AUTOEQP_trigger ){
13499 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
13501 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
13502 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13503 if( rc==SQLITE_OK ){
13504 while( sqlite3_step(pExplain)==SQLITE_ROW ){
13505 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
13506 int iEqpId = sqlite3_column_int(pExplain, 0);
13507 int iParentId = sqlite3_column_int(pExplain, 1);
13508 if( zEQPLine==0 ) zEQPLine = "";
13509 if( zEQPLine[0]=='-' ) eqp_render(pArg);
13510 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
13514 sqlite3_finalize(pExplain);
13515 sqlite3_free(zEQP);
13516 if( pArg->autoEQP>=AUTOEQP_full ){
13517 /* Also do an EXPLAIN for ".eqp full" mode */
13518 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
13519 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
13520 if( rc==SQLITE_OK ){
13521 pArg->cMode = MODE_Explain;
13522 explain_data_prepare(pArg, pExplain);
13523 exec_prepared_stmt(pArg, pExplain);
13524 explain_data_delete(pArg);
13526 sqlite3_finalize(pExplain);
13527 sqlite3_free(zEQP);
13529 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
13530 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
13531 /* Reprepare pStmt before reactiving trace modes */
13532 sqlite3_finalize(pStmt);
13533 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
13534 if( pArg ) pArg->pStmt = pStmt;
13536 restore_debug_trace_modes();
13540 pArg->cMode = pArg->mode;
13541 if( pArg->autoExplain ){
13542 if( sqlite3_stmt_isexplain(pStmt)==1 ){
13543 pArg->cMode = MODE_Explain;
13545 if( sqlite3_stmt_isexplain(pStmt)==2 ){
13546 pArg->cMode = MODE_EQP;
13550 /* If the shell is currently in ".explain" mode, gather the extra
13551 ** data required to add indents to the output.*/
13552 if( pArg->cMode==MODE_Explain ){
13553 explain_data_prepare(pArg, pStmt);
13557 bind_prepared_stmt(pArg, pStmt);
13558 exec_prepared_stmt(pArg, pStmt);
13559 explain_data_delete(pArg);
13562 /* print usage stats if stats on */
13563 if( pArg && pArg->statsOn ){
13564 display_stats(db, pArg, 0);
13567 /* print loop-counters if required */
13568 if( pArg && pArg->scanstatsOn ){
13569 display_scanstats(db, pArg);
13572 /* Finalize the statement just executed. If this fails, save a
13573 ** copy of the error message. Otherwise, set zSql to point to the
13574 ** next statement to execute. */
13575 rc2 = sqlite3_finalize(pStmt);
13576 if( rc!=SQLITE_NOMEM ) rc = rc2;
13577 if( rc==SQLITE_OK ){
13579 while( IsSpace(zSql[0]) ) zSql++;
13580 }else if( pzErrMsg ){
13581 *pzErrMsg = save_err_msg(db);
13584 /* clear saved stmt handle */
13586 pArg->pStmt = NULL;
13595 ** Release memory previously allocated by tableColumnList().
13597 static void freeColumnList(char **azCol){
13599 for(i=1; azCol[i]; i++){
13600 sqlite3_free(azCol[i]);
13602 /* azCol[0] is a static string */
13603 sqlite3_free(azCol);
13607 ** Return a list of pointers to strings which are the names of all
13608 ** columns in table zTab. The memory to hold the names is dynamically
13609 ** allocated and must be released by the caller using a subsequent call
13610 ** to freeColumnList().
13612 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
13613 ** value that needs to be preserved, then azCol[0] is filled in with the
13614 ** name of the rowid column.
13616 ** The first regular column in the table is azCol[1]. The list is terminated
13617 ** by an entry with azCol[i]==0.
13619 static char **tableColumnList(ShellState *p, const char *zTab){
13621 sqlite3_stmt *pStmt;
13625 int nPK = 0; /* Number of PRIMARY KEY columns seen */
13626 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
13627 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
13630 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
13631 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13632 sqlite3_free(zSql);
13634 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13635 if( nCol>=nAlloc-2 ){
13636 nAlloc = nAlloc*2 + nCol + 10;
13637 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
13638 if( azCol==0 ) shell_out_of_memory();
13640 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
13641 if( sqlite3_column_int(pStmt, 5) ){
13644 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
13653 sqlite3_finalize(pStmt);
13654 if( azCol==0 ) return 0;
13658 /* The decision of whether or not a rowid really needs to be preserved
13659 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
13660 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
13661 ** rowids on tables where the rowid is inaccessible because there are other
13662 ** columns in the table named "rowid", "_rowid_", and "oid".
13664 if( preserveRowid && isIPK ){
13665 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
13666 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
13667 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
13668 ** ROWID aliases. To distinguish these cases, check to see if
13669 ** there is a "pk" entry in "PRAGMA index_list". There will be
13670 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
13672 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
13673 " WHERE origin='pk'", zTab);
13674 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13675 sqlite3_free(zSql);
13677 freeColumnList(azCol);
13680 rc = sqlite3_step(pStmt);
13681 sqlite3_finalize(pStmt);
13682 preserveRowid = rc==SQLITE_ROW;
13684 if( preserveRowid ){
13685 /* Only preserve the rowid if we can find a name to use for the
13687 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
13689 for(j=0; j<3; j++){
13690 for(i=1; i<=nCol; i++){
13691 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
13694 /* At this point, we know that azRowid[j] is not the name of any
13695 ** ordinary column in the table. Verify that azRowid[j] is a valid
13696 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
13697 ** tables will fail this last check */
13698 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
13699 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
13708 ** Toggle the reverse_unordered_selects setting.
13710 static void toggleSelectOrder(sqlite3 *db){
13711 sqlite3_stmt *pStmt = 0;
13714 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
13715 if( sqlite3_step(pStmt)==SQLITE_ROW ){
13716 iSetting = sqlite3_column_int(pStmt, 0);
13718 sqlite3_finalize(pStmt);
13719 sqlite3_snprintf(sizeof(zStmt), zStmt,
13720 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
13721 sqlite3_exec(db, zStmt, 0, 0, 0);
13725 ** This is a different callback routine used for dumping the database.
13726 ** Each row received by this callback consists of a table name,
13727 ** the table type ("index" or "table") and SQL to create the table.
13728 ** This routine should print text sufficient to recreate the table.
13730 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
13732 const char *zTable;
13735 ShellState *p = (ShellState *)pArg;
13739 UNUSED_PARAMETER(azNotUsed);
13740 if( nArg!=3 || azArg==0 ) return 0;
13744 dataOnly = (p->shellFlgs & SHFLG_DumpDataOnly)!=0;
13745 noSys = (p->shellFlgs & SHFLG_DumpNoSys)!=0;
13747 if( strcmp(zTable, "sqlite_sequence")==0 && !noSys ){
13748 if( !dataOnly ) raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
13749 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 && !noSys ){
13750 if( !dataOnly ) raw_printf(p->out, "ANALYZE sqlite_schema;\n");
13751 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
13753 }else if( dataOnly ){
13755 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
13757 if( !p->writableSchema ){
13758 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
13759 p->writableSchema = 1;
13761 zIns = sqlite3_mprintf(
13762 "INSERT INTO sqlite_schema(type,name,tbl_name,rootpage,sql)"
13763 "VALUES('table','%q','%q',0,'%q');",
13764 zTable, zTable, zSql);
13765 utf8_printf(p->out, "%s\n", zIns);
13766 sqlite3_free(zIns);
13769 printSchemaLine(p->out, zSql, ";\n");
13772 if( strcmp(zType, "table")==0 ){
13777 char *savedDestTable;
13780 azCol = tableColumnList(p, zTable);
13786 /* Always quote the table name, even if it appears to be pure ascii,
13787 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
13789 appendText(&sTable, zTable, quoteChar(zTable));
13790 /* If preserving the rowid, add a column list after the table name.
13791 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
13792 ** instead of the usual "INSERT INTO tab VALUES(...)".
13795 appendText(&sTable, "(", 0);
13796 appendText(&sTable, azCol[0], 0);
13797 for(i=1; azCol[i]; i++){
13798 appendText(&sTable, ",", 0);
13799 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
13801 appendText(&sTable, ")", 0);
13804 /* Build an appropriate SELECT statement */
13805 initText(&sSelect);
13806 appendText(&sSelect, "SELECT ", 0);
13808 appendText(&sSelect, azCol[0], 0);
13809 appendText(&sSelect, ",", 0);
13811 for(i=1; azCol[i]; i++){
13812 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
13814 appendText(&sSelect, ",", 0);
13817 freeColumnList(azCol);
13818 appendText(&sSelect, " FROM ", 0);
13819 appendText(&sSelect, zTable, quoteChar(zTable));
13821 savedDestTable = p->zDestTable;
13822 savedMode = p->mode;
13823 p->zDestTable = sTable.z;
13824 p->mode = p->cMode = MODE_Insert;
13825 rc = shell_exec(p, sSelect.z, 0);
13826 if( (rc&0xff)==SQLITE_CORRUPT ){
13827 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13828 toggleSelectOrder(p->db);
13829 shell_exec(p, sSelect.z, 0);
13830 toggleSelectOrder(p->db);
13832 p->zDestTable = savedDestTable;
13833 p->mode = savedMode;
13835 freeText(&sSelect);
13836 if( rc ) p->nErr++;
13842 ** Run zQuery. Use dump_callback() as the callback routine so that
13843 ** the contents of the query are output as SQL statements.
13845 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
13846 ** "ORDER BY rowid DESC" to the end.
13848 static int run_schema_dump_query(
13854 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
13855 if( rc==SQLITE_CORRUPT ){
13857 int len = strlen30(zQuery);
13858 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
13860 utf8_printf(p->out, "/****** %s ******/\n", zErr);
13861 sqlite3_free(zErr);
13864 zQ2 = malloc( len+100 );
13865 if( zQ2==0 ) return rc;
13866 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
13867 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
13869 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
13871 rc = SQLITE_CORRUPT;
13873 sqlite3_free(zErr);
13880 ** Text of help messages.
13882 ** The help text for each individual command begins with a line that starts
13883 ** with ".". Subsequent lines are supplimental information.
13885 ** There must be two or more spaces between the end of the command and the
13886 ** start of the description of what that command does.
13888 static const char *(azHelp[]) = {
13889 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
13890 ".archive ... Manage SQL archives",
13891 " Each command must have exactly one of the following options:",
13892 " -c, --create Create a new archive",
13893 " -u, --update Add or update files with changed mtime",
13894 " -i, --insert Like -u but always add even if unchanged",
13895 " -t, --list List contents of archive",
13896 " -x, --extract Extract files from archive",
13897 " Optional arguments:",
13898 " -v, --verbose Print each filename as it is processed",
13899 " -f FILE, --file FILE Use archive FILE (default is current db)",
13900 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
13901 " -C DIR, --directory DIR Read/extract files from directory DIR",
13902 " -n, --dryrun Show the SQL that would have occurred",
13904 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
13905 " .ar -tf ARCHIVE # List members of ARCHIVE",
13906 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
13908 " http://sqlite.org/cli.html#sqlar_archive_support",
13910 #ifndef SQLITE_OMIT_AUTHORIZATION
13911 ".auth ON|OFF Show authorizer callbacks",
13913 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
13914 " --append Use the appendvfs",
13915 " --async Write to FILE without journal and fsync()",
13916 ".bail on|off Stop after hitting an error. Default OFF",
13917 ".binary on|off Turn binary output on or off. Default OFF",
13918 ".cd DIRECTORY Change the working directory to DIRECTORY",
13919 ".changes on|off Show number of rows changed by SQL",
13920 ".check GLOB Fail if output since .testcase does not match",
13921 ".clone NEWDB Clone data into NEWDB from the existing database",
13922 ".databases List names and files of attached databases",
13923 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
13924 ".dbinfo ?DB? Show status information about the database",
13925 ".dump ?TABLE? Render database content as SQL",
13927 " --data-only Output only INSERT statements",
13928 " --newlines Allow unescaped newline characters in output",
13929 " --nosys Omit system tables (ex: \"sqlite_stat1\")",
13930 " --preserve-rowids Include ROWID values in the output",
13931 " TABLE is a LIKE pattern for the tables to dump",
13932 " Additional LIKE patterns can be given in subsequent arguments",
13933 ".echo on|off Turn command echo on or off",
13934 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
13936 #ifdef SQLITE_DEBUG
13937 " test Show raw EXPLAIN QUERY PLAN output",
13938 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
13940 " trigger Like \"full\" but also show trigger bytecode",
13941 ".excel Display the output of next command in spreadsheet",
13942 " --bom Put a UTF8 byte-order mark on intermediate file",
13943 ".exit ?CODE? Exit this program with return-code CODE",
13944 ".expert EXPERIMENTAL. Suggest indexes for queries",
13945 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
13946 ".filectrl CMD ... Run various sqlite3_file_control() operations",
13947 " --schema SCHEMA Use SCHEMA instead of \"main\"",
13948 " --help Show CMD details",
13949 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
13950 ".headers on|off Turn display of headers on or off",
13951 ".help ?-all? ?PATTERN? Show help text for PATTERN",
13952 ".import FILE TABLE Import data from FILE into TABLE",
13954 " --ascii Use \\037 and \\036 as column and row separators",
13955 " --csv Use , and \\n as column and row separators",
13956 " --skip N Skip the first N rows of input",
13957 " -v \"Verbose\" - increase auxiliary output",
13959 " * If TABLE does not exist, it is created. The first row of input",
13960 " determines the column names.",
13961 " * If neither --csv or --ascii are used, the input mode is derived",
13962 " from the \".mode\" output mode",
13963 " * If FILE begins with \"|\" then it is a command that generates the",
13965 #ifndef SQLITE_OMIT_TEST_CONTROL
13966 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
13968 ".indexes ?TABLE? Show names of indexes",
13969 " If TABLE is specified, only show indexes for",
13970 " tables matching TABLE using the LIKE operator.",
13971 #ifdef SQLITE_ENABLE_IOTRACE
13972 ".iotrace FILE Enable I/O diagnostic logging to FILE",
13974 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
13975 ".lint OPTIONS Report potential schema issues.",
13977 " fkey-indexes Find missing foreign key indexes",
13978 #ifndef SQLITE_OMIT_LOAD_EXTENSION
13979 ".load FILE ?ENTRY? Load an extension library",
13981 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
13982 ".mode MODE ?TABLE? Set output mode",
13983 " MODE is one of:",
13984 " ascii Columns/rows delimited by 0x1F and 0x1E",
13985 " box Tables using unicode box-drawing characters",
13986 " csv Comma-separated values",
13987 " column Output in columns. (See .width)",
13988 " html HTML <table> code",
13989 " insert SQL insert statements for TABLE",
13990 " json Results in a JSON array",
13991 " line One value per line",
13992 " list Values delimited by \"|\"",
13993 " markdown Markdown table format",
13994 " quote Escape answers as for SQL",
13995 " table ASCII-art table",
13996 " tabs Tab-separated values",
13997 " tcl TCL list elements",
13998 ".nullvalue STRING Use STRING in place of NULL values",
13999 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
14000 " If FILE begins with '|' then open as a pipe",
14001 " --bom Put a UTF8 byte-order mark at the beginning",
14002 " -e Send output to the system text editor",
14003 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
14004 #ifdef SQLITE_DEBUG
14005 ".oom ?--repeat M? ?N? Simulate an OOM error on the N-th allocation",
14007 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
14009 " --append Use appendvfs to append database to the end of FILE",
14010 #ifdef SQLITE_ENABLE_DESERIALIZE
14011 " --deserialize Load into memory useing sqlite3_deserialize()",
14012 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
14013 " --maxsize N Maximum size for --hexdb or --deserialized database",
14015 " --new Initialize FILE to an empty database",
14016 " --nofollow Do not follow symbolic links",
14017 " --readonly Open FILE readonly",
14018 " --zip FILE is a ZIP archive",
14019 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
14020 " If FILE begins with '|' then open it as a pipe.",
14022 " --bom Prefix output with a UTF8 byte-order mark",
14023 " -e Send output to the system text editor",
14024 " -x Send output as CSV to a spreadsheet",
14025 ".parameter CMD ... Manage SQL parameter bindings",
14026 " clear Erase all bindings",
14027 " init Initialize the TEMP table that holds bindings",
14028 " list List the current parameter bindings",
14029 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
14030 " PARAMETER should start with one of: $ : @ ?",
14031 " unset PARAMETER Remove PARAMETER from the binding table",
14032 ".print STRING... Print literal STRING",
14033 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
14034 ".progress N Invoke progress handler after every N opcodes",
14035 " --limit N Interrupt after N progress callbacks",
14036 " --once Do no more than one progress interrupt",
14037 " --quiet|-q No output except at interrupts",
14038 " --reset Reset the count for each input and interrupt",
14040 ".prompt MAIN CONTINUE Replace the standard prompts",
14041 ".quit Exit this program",
14042 ".read FILE Read input from FILE",
14043 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14044 ".recover Recover as much data as possible from corrupt db.",
14045 " --freelist-corrupt Assume the freelist is corrupt",
14046 " --recovery-db NAME Store recovery metadata in database file NAME",
14047 " --lost-and-found TABLE Alternative name for the lost-and-found table",
14048 " --no-rowids Do not attempt to recover rowid values",
14049 " that are not also INTEGER PRIMARY KEYs",
14051 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
14052 ".save FILE Write in-memory database into FILE",
14053 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
14054 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
14056 " --indent Try to pretty-print the schema",
14057 " --nosys Omit objects whose names start with \"sqlite_\"",
14058 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
14060 " --init Create a new SELFTEST table",
14061 " -v Verbose output",
14062 ".separator COL ?ROW? Change the column and row separators",
14063 #if defined(SQLITE_ENABLE_SESSION)
14064 ".session ?NAME? CMD ... Create or control sessions",
14066 " attach TABLE Attach TABLE",
14067 " changeset FILE Write a changeset into FILE",
14068 " close Close one session",
14069 " enable ?BOOLEAN? Set or query the enable bit",
14070 " filter GLOB... Reject tables matching GLOBs",
14071 " indirect ?BOOLEAN? Mark or query the indirect status",
14072 " isempty Query whether the session is empty",
14073 " list List currently open session names",
14074 " open DB NAME Open a new session on DB",
14075 " patchset FILE Write a patchset into FILE",
14076 " If ?NAME? is omitted, the first defined session is used.",
14078 ".sha3sum ... Compute a SHA3 hash of database content",
14080 " --schema Also hash the sqlite_schema table",
14081 " --sha3-224 Use the sha3-224 algorithm",
14082 " --sha3-256 Use the sha3-256 algorithm (default)",
14083 " --sha3-384 Use the sha3-384 algorithm",
14084 " --sha3-512 Use the sha3-512 algorithm",
14085 " Any other argument is a LIKE pattern for tables to hash",
14086 #ifndef SQLITE_NOHAVE_SYSTEM
14087 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
14089 ".show Show the current values for various settings",
14090 ".stats ?on|off? Show stats or turn stats on or off",
14091 #ifndef SQLITE_NOHAVE_SYSTEM
14092 ".system CMD ARGS... Run CMD ARGS... in a system shell",
14094 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
14095 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
14096 ".testctrl CMD ... Run various sqlite3_test_control() operations",
14097 " Run \".testctrl\" with no arguments for details",
14098 ".timeout MS Try opening locked tables for MS milliseconds",
14099 ".timer on|off Turn SQL timer on or off",
14100 #ifndef SQLITE_OMIT_TRACE
14101 ".trace ?OPTIONS? Output each SQL statement as it is run",
14102 " FILE Send output to FILE",
14103 " stdout Send output to stdout",
14104 " stderr Send output to stderr",
14105 " off Disable tracing",
14106 " --expanded Expand query parameters",
14107 #ifdef SQLITE_ENABLE_NORMALIZE
14108 " --normalized Normal the SQL statements",
14110 " --plain Show SQL as it is input",
14111 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
14112 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
14113 " --row Trace each row (SQLITE_TRACE_ROW)",
14114 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
14115 #endif /* SQLITE_OMIT_TRACE */
14116 #ifdef SQLITE_DEBUG
14117 ".unmodule NAME ... Unregister virtual table modules",
14118 " --allexcept Unregister everything except those named",
14120 ".vfsinfo ?AUX? Information about the top-level VFS",
14121 ".vfslist List all available VFSes",
14122 ".vfsname ?AUX? Print the name of the VFS stack",
14123 ".width NUM1 NUM2 ... Set minimum column widths for columnar output",
14124 " Negative values right-justify",
14128 ** Output help text.
14130 ** zPattern describes the set of commands for which help text is provided.
14131 ** If zPattern is NULL, then show all commands, but only give a one-line
14132 ** description of each.
14134 ** Return the number of matches.
14136 static int showHelp(FILE *out, const char *zPattern){
14142 || zPattern[0]=='0'
14143 || strcmp(zPattern,"-a")==0
14144 || strcmp(zPattern,"-all")==0
14145 || strcmp(zPattern,"--all")==0
14147 /* Show all commands, but only one line per command */
14148 if( zPattern==0 ) zPattern = "";
14149 for(i=0; i<ArraySize(azHelp); i++){
14150 if( azHelp[i][0]=='.' || zPattern[0] ){
14151 utf8_printf(out, "%s\n", azHelp[i]);
14156 /* Look for commands that for which zPattern is an exact prefix */
14157 zPat = sqlite3_mprintf(".%s*", zPattern);
14158 for(i=0; i<ArraySize(azHelp); i++){
14159 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
14160 utf8_printf(out, "%s\n", azHelp[i]);
14165 sqlite3_free(zPat);
14168 /* when zPattern is a prefix of exactly one command, then include the
14169 ** details of that command, which should begin at offset j */
14170 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
14171 utf8_printf(out, "%s\n", azHelp[j]);
14177 /* Look for commands that contain zPattern anywhere. Show the complete
14178 ** text of all commands that match. */
14179 zPat = sqlite3_mprintf("%%%s%%", zPattern);
14180 for(i=0; i<ArraySize(azHelp); i++){
14181 if( azHelp[i][0]=='.' ) j = i;
14182 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
14183 utf8_printf(out, "%s\n", azHelp[j]);
14184 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
14186 utf8_printf(out, "%s\n", azHelp[j]);
14192 sqlite3_free(zPat);
14197 /* Forward reference */
14198 static int process_input(ShellState *p);
14201 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
14202 ** and return a pointer to the buffer. The caller is responsible for freeing
14205 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
14208 ** For convenience, a nul-terminator byte is always appended to the data read
14209 ** from the file before the buffer is returned. This byte is not included in
14210 ** the final value of (*pnByte), if applicable.
14212 ** NULL is returned if any error is encountered. The final value of *pnByte
14213 ** is undefined in this case.
14215 static char *readFile(const char *zName, int *pnByte){
14216 FILE *in = fopen(zName, "rb");
14220 if( in==0 ) return 0;
14221 fseek(in, 0, SEEK_END);
14224 pBuf = sqlite3_malloc64( nIn+1 );
14225 if( pBuf==0 ){ fclose(in); return 0; }
14226 nRead = fread(pBuf, nIn, 1, in);
14229 sqlite3_free(pBuf);
14233 if( pnByte ) *pnByte = nIn;
14237 #if defined(SQLITE_ENABLE_SESSION)
14239 ** Close a single OpenSession object and release all of its associated
14242 static void session_close(OpenSession *pSession){
14244 sqlite3session_delete(pSession->p);
14245 sqlite3_free(pSession->zName);
14246 for(i=0; i<pSession->nFilter; i++){
14247 sqlite3_free(pSession->azFilter[i]);
14249 sqlite3_free(pSession->azFilter);
14250 memset(pSession, 0, sizeof(OpenSession));
14255 ** Close all OpenSession objects and release all associated resources.
14257 #if defined(SQLITE_ENABLE_SESSION)
14258 static void session_close_all(ShellState *p){
14260 for(i=0; i<p->nSession; i++){
14261 session_close(&p->aSession[i]);
14266 # define session_close_all(X)
14270 ** Implementation of the xFilter function for an open session. Omit
14271 ** any tables named by ".session filter" but let all other table through.
14273 #if defined(SQLITE_ENABLE_SESSION)
14274 static int session_filter(void *pCtx, const char *zTab){
14275 OpenSession *pSession = (OpenSession*)pCtx;
14277 for(i=0; i<pSession->nFilter; i++){
14278 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
14285 ** Try to deduce the type of file for zName based on its content. Return
14286 ** one of the SHELL_OPEN_* constants.
14288 ** If the file does not exist or is empty but its name looks like a ZIP
14289 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
14290 ** Otherwise, assume an ordinary database regardless of the filename if
14291 ** the type cannot be determined from content.
14293 int deduceDatabaseType(const char *zName, int dfltZip){
14294 FILE *f = fopen(zName, "rb");
14296 int rc = SHELL_OPEN_UNSPEC;
14299 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14300 return SHELL_OPEN_ZIPFILE;
14302 return SHELL_OPEN_NORMAL;
14305 n = fread(zBuf, 16, 1, f);
14306 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
14308 return SHELL_OPEN_NORMAL;
14310 fseek(f, -25, SEEK_END);
14311 n = fread(zBuf, 25, 1, f);
14312 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
14313 rc = SHELL_OPEN_APPENDVFS;
14315 fseek(f, -22, SEEK_END);
14316 n = fread(zBuf, 22, 1, f);
14317 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
14318 && zBuf[3]==0x06 ){
14319 rc = SHELL_OPEN_ZIPFILE;
14320 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
14321 rc = SHELL_OPEN_ZIPFILE;
14328 #ifdef SQLITE_ENABLE_DESERIALIZE
14330 ** Reconstruct an in-memory database using the output from the "dbtotxt"
14331 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
14332 ** is 0, then read from standard input.
14334 static unsigned char *readHexDb(ShellState *p, int *pnData){
14335 unsigned char *a = 0;
14343 unsigned int x[16];
14345 if( p->zDbFilename ){
14346 in = fopen(p->zDbFilename, "r");
14348 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
14355 if( in==0 ) in = stdin;
14359 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
14360 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
14361 if( rc!=2 ) goto readHexDb_error;
14362 if( n<0 ) goto readHexDb_error;
14363 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
14364 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
14365 a = sqlite3_malloc( n ? n : 1 );
14367 utf8_printf(stderr, "Out of memory!\n");
14368 goto readHexDb_error;
14371 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
14372 utf8_printf(stderr, "invalid pagesize\n");
14373 goto readHexDb_error;
14375 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
14376 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
14381 if( strncmp(zLine, "| end ", 6)==0 ){
14384 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
14385 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
14386 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
14391 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
14407 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
14409 if(strncmp(zLine, "| end ", 6)==0 ) break;
14414 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
14417 #endif /* SQLITE_ENABLE_DESERIALIZE */
14420 ** Scalar function "shell_int32". The first argument to this function
14421 ** must be a blob. The second a non-negative integer. This function
14422 ** reads and returns a 32-bit big-endian integer from byte
14423 ** offset (4*<arg2>) of the blob.
14425 static void shellInt32(
14426 sqlite3_context *context,
14428 sqlite3_value **argv
14430 const unsigned char *pBlob;
14434 UNUSED_PARAMETER(argc);
14435 nBlob = sqlite3_value_bytes(argv[0]);
14436 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
14437 iInt = sqlite3_value_int(argv[1]);
14439 if( iInt>=0 && (iInt+1)*4<=nBlob ){
14440 const unsigned char *a = &pBlob[iInt*4];
14441 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
14442 + ((sqlite3_int64)a[1]<<16)
14443 + ((sqlite3_int64)a[2]<< 8)
14444 + ((sqlite3_int64)a[3]<< 0);
14445 sqlite3_result_int64(context, iVal);
14450 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
14451 ** using "..." with internal double-quote characters doubled.
14453 static void shellIdQuote(
14454 sqlite3_context *context,
14456 sqlite3_value **argv
14458 const char *zName = (const char*)sqlite3_value_text(argv[0]);
14459 UNUSED_PARAMETER(argc);
14461 char *z = sqlite3_mprintf("\"%w\"", zName);
14462 sqlite3_result_text(context, z, -1, sqlite3_free);
14467 ** Scalar function "usleep(X)" invokes sqlite3_sleep(X) and returns X.
14469 static void shellUSleepFunc(
14470 sqlite3_context *context,
14472 sqlite3_value **argv
14474 int sleep = sqlite3_value_int(argv[0]);
14476 sqlite3_sleep(sleep/1000);
14477 sqlite3_result_int(context, sleep);
14481 ** Scalar function "shell_escape_crnl" used by the .recover command.
14482 ** The argument passed to this function is the output of built-in
14483 ** function quote(). If the first character of the input is "'",
14484 ** indicating that the value passed to quote() was a text value,
14485 ** then this function searches the input for "\n" and "\r" characters
14486 ** and adds a wrapper similar to the following:
14488 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
14490 ** Or, if the first character of the input is not "'", then a copy
14491 ** of the input is returned.
14493 static void shellEscapeCrnl(
14494 sqlite3_context *context,
14496 sqlite3_value **argv
14498 const char *zText = (const char*)sqlite3_value_text(argv[0]);
14499 UNUSED_PARAMETER(argc);
14500 if( zText[0]=='\'' ){
14501 int nText = sqlite3_value_bytes(argv[0]);
14505 const char *zNL = 0;
14506 const char *zCR = 0;
14510 for(i=0; zText[i]; i++){
14511 if( zNL==0 && zText[i]=='\n' ){
14512 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
14513 nNL = (int)strlen(zNL);
14515 if( zCR==0 && zText[i]=='\r' ){
14516 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
14517 nCR = (int)strlen(zCR);
14523 i64 nMax = (nNL > nCR) ? nNL : nCR;
14524 i64 nAlloc = nMax * nText + (nMax+64)*2;
14525 char *zOut = (char*)sqlite3_malloc64(nAlloc);
14527 sqlite3_result_error_nomem(context);
14532 memcpy(&zOut[iOut], "replace(replace(", 16);
14535 memcpy(&zOut[iOut], "replace(", 8);
14538 for(i=0; zText[i]; i++){
14539 if( zText[i]=='\n' ){
14540 memcpy(&zOut[iOut], zNL, nNL);
14542 }else if( zText[i]=='\r' ){
14543 memcpy(&zOut[iOut], zCR, nCR);
14546 zOut[iOut] = zText[i];
14552 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14553 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
14554 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
14557 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
14558 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
14559 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
14562 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
14563 sqlite3_free(zOut);
14568 sqlite3_result_value(context, argv[0]);
14571 /* Flags for open_db().
14573 ** The default behavior of open_db() is to exit(1) if the database fails to
14574 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
14575 ** but still returns without calling exit.
14577 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
14578 ** ZIP archive if the file does not exist or is empty and its name matches
14579 ** the *.zip pattern.
14581 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
14582 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
14585 ** Make sure the database is open. If it is not, then open it. If
14586 ** the database fails to open, print an error message and exit.
14588 static void open_db(ShellState *p, int openFlags){
14590 if( p->openMode==SHELL_OPEN_UNSPEC ){
14591 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
14592 p->openMode = SHELL_OPEN_NORMAL;
14594 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
14595 (openFlags & OPEN_DB_ZIPFILE)!=0);
14598 switch( p->openMode ){
14599 case SHELL_OPEN_APPENDVFS: {
14600 sqlite3_open_v2(p->zDbFilename, &p->db,
14601 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
14604 case SHELL_OPEN_HEXDB:
14605 case SHELL_OPEN_DESERIALIZE: {
14606 sqlite3_open(0, &p->db);
14609 case SHELL_OPEN_ZIPFILE: {
14610 sqlite3_open(":memory:", &p->db);
14613 case SHELL_OPEN_READONLY: {
14614 sqlite3_open_v2(p->zDbFilename, &p->db,
14615 SQLITE_OPEN_READONLY|p->openFlags, 0);
14618 case SHELL_OPEN_UNSPEC:
14619 case SHELL_OPEN_NORMAL: {
14620 sqlite3_open_v2(p->zDbFilename, &p->db,
14621 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
14626 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
14627 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
14628 p->zDbFilename, sqlite3_errmsg(p->db));
14629 if( openFlags & OPEN_DB_KEEPALIVE ){
14630 sqlite3_open(":memory:", &p->db);
14635 #ifndef SQLITE_OMIT_LOAD_EXTENSION
14636 sqlite3_enable_load_extension(p->db, 1);
14638 sqlite3_fileio_init(p->db, 0, 0);
14639 sqlite3_shathree_init(p->db, 0, 0);
14640 sqlite3_completion_init(p->db, 0, 0);
14641 sqlite3_uint_init(p->db, 0, 0);
14642 sqlite3_decimal_init(p->db, 0, 0);
14643 sqlite3_ieee_init(p->db, 0, 0);
14644 sqlite3_series_init(p->db, 0, 0);
14645 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
14646 sqlite3_dbdata_init(p->db, 0, 0);
14648 #ifdef SQLITE_HAVE_ZLIB
14649 sqlite3_zipfile_init(p->db, 0, 0);
14650 sqlite3_sqlar_init(p->db, 0, 0);
14652 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
14653 shellAddSchemaName, 0, 0);
14654 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
14655 shellModuleSchema, 0, 0);
14656 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
14657 shellPutsFunc, 0, 0);
14658 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
14659 shellEscapeCrnl, 0, 0);
14660 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
14662 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
14663 shellIdQuote, 0, 0);
14664 sqlite3_create_function(p->db, "usleep",1,SQLITE_UTF8,0,
14665 shellUSleepFunc, 0, 0);
14666 #ifndef SQLITE_NOHAVE_SYSTEM
14667 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
14669 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
14672 if( p->openMode==SHELL_OPEN_ZIPFILE ){
14673 char *zSql = sqlite3_mprintf(
14674 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
14675 sqlite3_exec(p->db, zSql, 0, 0, 0);
14676 sqlite3_free(zSql);
14678 #ifdef SQLITE_ENABLE_DESERIALIZE
14680 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
14683 unsigned char *aData;
14684 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
14685 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
14687 aData = readHexDb(p, &nData);
14692 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
14693 SQLITE_DESERIALIZE_RESIZEABLE |
14694 SQLITE_DESERIALIZE_FREEONCLOSE);
14696 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
14699 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
14707 ** Attempt to close the databaes connection. Report errors.
14709 void close_db(sqlite3 *db){
14710 int rc = sqlite3_close(db);
14712 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
14713 rc, sqlite3_errmsg(db));
14717 #if HAVE_READLINE || HAVE_EDITLINE
14719 ** Readline completion callbacks
14721 static char *readline_completion_generator(const char *text, int state){
14722 static sqlite3_stmt *pStmt = 0;
14726 sqlite3_finalize(pStmt);
14727 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14728 " FROM completion(%Q) ORDER BY 1", text);
14729 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14730 sqlite3_free(zSql);
14732 if( sqlite3_step(pStmt)==SQLITE_ROW ){
14733 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
14735 sqlite3_finalize(pStmt);
14741 static char **readline_completion(const char *zText, int iStart, int iEnd){
14742 rl_attempted_completion_over = 1;
14743 return rl_completion_matches(zText, readline_completion_generator);
14746 #elif HAVE_LINENOISE
14748 ** Linenoise completion callback
14750 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
14751 int nLine = strlen30(zLine);
14753 sqlite3_stmt *pStmt = 0;
14757 if( nLine>sizeof(zBuf)-30 ) return;
14758 if( zLine[0]=='.' || zLine[0]=='#') return;
14759 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
14760 if( i==nLine-1 ) return;
14762 memcpy(zBuf, zLine, iStart);
14763 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
14764 " FROM completion(%Q,%Q) ORDER BY 1",
14765 &zLine[iStart], zLine);
14766 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
14767 sqlite3_free(zSql);
14768 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
14769 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14770 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
14771 int nCompletion = sqlite3_column_bytes(pStmt, 0);
14772 if( iStart+nCompletion < sizeof(zBuf)-1 ){
14773 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
14774 linenoiseAddCompletion(lc, zBuf);
14777 sqlite3_finalize(pStmt);
14782 ** Do C-language style dequoting.
14788 ** \v -> vertical tab
14790 ** \r -> carriage return
14795 ** \NNN -> ascii character NNN in octal
14797 static void resolve_backslashes(char *z){
14800 while( *z && *z!='\\' ) z++;
14801 for(i=j=0; (c = z[i])!=0; i++, j++){
14802 if( c=='\\' && z[i+1]!=0 ){
14806 }else if( c=='b' ){
14808 }else if( c=='t' ){
14810 }else if( c=='n' ){
14812 }else if( c=='v' ){
14814 }else if( c=='f' ){
14816 }else if( c=='r' ){
14818 }else if( c=='"' ){
14820 }else if( c=='\'' ){
14822 }else if( c=='\\' ){
14824 }else if( c>='0' && c<='7' ){
14826 if( z[i+1]>='0' && z[i+1]<='7' ){
14828 c = (c<<3) + z[i] - '0';
14829 if( z[i+1]>='0' && z[i+1]<='7' ){
14831 c = (c<<3) + z[i] - '0';
14838 if( j<i ) z[j] = 0;
14842 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
14843 ** for TRUE and FALSE. Return the integer value if appropriate.
14845 static int booleanValue(const char *zArg){
14847 if( zArg[0]=='0' && zArg[1]=='x' ){
14848 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
14850 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
14852 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
14853 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
14856 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
14859 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
14865 ** Set or clear a shell flag according to a boolean value.
14867 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
14868 if( booleanValue(zArg) ){
14869 ShellSetFlag(p, mFlag);
14871 ShellClearFlag(p, mFlag);
14876 ** Close an output file, assuming it is not stderr or stdout
14878 static void output_file_close(FILE *f){
14879 if( f && f!=stdout && f!=stderr ) fclose(f);
14883 ** Try to open an output file. The names "stdout" and "stderr" are
14884 ** recognized and do the right thing. NULL is returned if the output
14885 ** filename is "off".
14887 static FILE *output_file_open(const char *zFile, int bTextMode){
14889 if( strcmp(zFile,"stdout")==0 ){
14891 }else if( strcmp(zFile, "stderr")==0 ){
14893 }else if( strcmp(zFile, "off")==0 ){
14896 f = fopen(zFile, bTextMode ? "w" : "wb");
14898 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
14904 #ifndef SQLITE_OMIT_TRACE
14906 ** A routine for handling output from sqlite3_trace().
14908 static int sql_trace_callback(
14909 unsigned mType, /* The trace type */
14910 void *pArg, /* The ShellState pointer */
14911 void *pP, /* Usually a pointer to sqlite_stmt */
14912 void *pX /* Auxiliary output */
14914 ShellState *p = (ShellState*)pArg;
14915 sqlite3_stmt *pStmt;
14918 if( p->traceOut==0 ) return 0;
14919 if( mType==SQLITE_TRACE_CLOSE ){
14920 utf8_printf(p->traceOut, "-- closing database connection\n");
14923 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
14924 zSql = (const char*)pX;
14926 pStmt = (sqlite3_stmt*)pP;
14927 switch( p->eTraceType ){
14928 case SHELL_TRACE_EXPANDED: {
14929 zSql = sqlite3_expanded_sql(pStmt);
14932 #ifdef SQLITE_ENABLE_NORMALIZE
14933 case SHELL_TRACE_NORMALIZED: {
14934 zSql = sqlite3_normalized_sql(pStmt);
14939 zSql = sqlite3_sql(pStmt);
14944 if( zSql==0 ) return 0;
14945 nSql = strlen30(zSql);
14946 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
14948 case SQLITE_TRACE_ROW:
14949 case SQLITE_TRACE_STMT: {
14950 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
14953 case SQLITE_TRACE_PROFILE: {
14954 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
14955 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
14964 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
14965 ** a useful spot to set a debugger breakpoint.
14967 static void test_breakpoint(void){
14968 static int nCall = 0;
14973 ** An object used to read a CSV and other files for import.
14975 typedef struct ImportCtx ImportCtx;
14977 const char *zFile; /* Name of the input file */
14978 FILE *in; /* Read the CSV text from this input stream */
14979 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close in */
14980 char *z; /* Accumulated text for a field */
14981 int n; /* Number of bytes in z */
14982 int nAlloc; /* Space allocated for z[] */
14983 int nLine; /* Current line number */
14984 int nRow; /* Number of rows imported */
14985 int nErr; /* Number of errors encountered */
14986 int bNotFirst; /* True if one or more bytes already read */
14987 int cTerm; /* Character that terminated the most recent field */
14988 int cColSep; /* The column separator character. (Usually ",") */
14989 int cRowSep; /* The row separator character. (Usually "\n") */
14992 /* Clean up resourced used by an ImportCtx */
14993 static void import_cleanup(ImportCtx *p){
14994 if( p->in!=0 && p->xCloser!=0 ){
14998 sqlite3_free(p->z);
15002 /* Append a single byte to z[] */
15003 static void import_append_char(ImportCtx *p, int c){
15004 if( p->n+1>=p->nAlloc ){
15005 p->nAlloc += p->nAlloc + 100;
15006 p->z = sqlite3_realloc64(p->z, p->nAlloc);
15007 if( p->z==0 ) shell_out_of_memory();
15009 p->z[p->n++] = (char)c;
15012 /* Read a single field of CSV text. Compatible with rfc4180 and extended
15013 ** with the option of having a separator other than ",".
15015 ** + Input comes from p->in.
15016 ** + Store results in p->z of length p->n. Space to hold p->z comes
15017 ** from sqlite3_malloc64().
15018 ** + Use p->cSep as the column separator. The default is ",".
15019 ** + Use p->rSep as the row separator. The default is "\n".
15020 ** + Keep track of the line number in p->nLine.
15021 ** + Store the character that terminates the field in p->cTerm. Store
15022 ** EOF on end-of-file.
15023 ** + Report syntax errors on stderr
15025 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
15027 int cSep = p->cColSep;
15028 int rSep = p->cRowSep;
15031 if( c==EOF || seenInterrupt ){
15037 int startLine = p->nLine;
15042 if( c==rSep ) p->nLine++;
15049 if( (c==cSep && pc==cQuote)
15050 || (c==rSep && pc==cQuote)
15051 || (c==rSep && pc=='\r' && ppc==cQuote)
15052 || (c==EOF && pc==cQuote)
15054 do{ p->n--; }while( p->z[p->n]!=cQuote );
15058 if( pc==cQuote && c!='\r' ){
15059 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
15060 p->zFile, p->nLine, cQuote);
15063 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
15064 p->zFile, startLine, cQuote);
15068 import_append_char(p, c);
15073 /* If this is the first field being parsed and it begins with the
15074 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
15075 if( (c&0xff)==0xef && p->bNotFirst==0 ){
15076 import_append_char(p, c);
15078 if( (c&0xff)==0xbb ){
15079 import_append_char(p, c);
15081 if( (c&0xff)==0xbf ){
15084 return csv_read_one_field(p);
15088 while( c!=EOF && c!=cSep && c!=rSep ){
15089 import_append_char(p, c);
15094 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
15098 if( p->z ) p->z[p->n] = 0;
15103 /* Read a single field of ASCII delimited text.
15105 ** + Input comes from p->in.
15106 ** + Store results in p->z of length p->n. Space to hold p->z comes
15107 ** from sqlite3_malloc64().
15108 ** + Use p->cSep as the column separator. The default is "\x1F".
15109 ** + Use p->rSep as the row separator. The default is "\x1E".
15110 ** + Keep track of the row number in p->nLine.
15111 ** + Store the character that terminates the field in p->cTerm. Store
15112 ** EOF on end-of-file.
15113 ** + Report syntax errors on stderr
15115 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
15117 int cSep = p->cColSep;
15118 int rSep = p->cRowSep;
15121 if( c==EOF || seenInterrupt ){
15125 while( c!=EOF && c!=cSep && c!=rSep ){
15126 import_append_char(p, c);
15133 if( p->z ) p->z[p->n] = 0;
15138 ** Try to transfer data for table zTable. If an error is seen while
15139 ** moving forward, try to go backwards. The backwards movement won't
15140 ** work for WITHOUT ROWID tables.
15142 static void tryToCloneData(
15147 sqlite3_stmt *pQuery = 0;
15148 sqlite3_stmt *pInsert = 0;
15153 int nTable = strlen30(zTable);
15156 const int spinRate = 10000;
15158 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
15159 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15161 utf8_printf(stderr, "Error %d: %s on [%s]\n",
15162 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15164 goto end_data_xfer;
15166 n = sqlite3_column_count(pQuery);
15167 zInsert = sqlite3_malloc64(200 + nTable + n*3);
15168 if( zInsert==0 ) shell_out_of_memory();
15169 sqlite3_snprintf(200+nTable,zInsert,
15170 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
15171 i = strlen30(zInsert);
15172 for(j=1; j<n; j++){
15173 memcpy(zInsert+i, ",?", 2);
15176 memcpy(zInsert+i, ");", 3);
15177 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
15179 utf8_printf(stderr, "Error %d: %s on [%s]\n",
15180 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
15182 goto end_data_xfer;
15184 for(k=0; k<2; k++){
15185 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15186 for(i=0; i<n; i++){
15187 switch( sqlite3_column_type(pQuery, i) ){
15188 case SQLITE_NULL: {
15189 sqlite3_bind_null(pInsert, i+1);
15192 case SQLITE_INTEGER: {
15193 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
15196 case SQLITE_FLOAT: {
15197 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
15200 case SQLITE_TEXT: {
15201 sqlite3_bind_text(pInsert, i+1,
15202 (const char*)sqlite3_column_text(pQuery,i),
15203 -1, SQLITE_STATIC);
15206 case SQLITE_BLOB: {
15207 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
15208 sqlite3_column_bytes(pQuery,i),
15214 rc = sqlite3_step(pInsert);
15215 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
15216 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
15217 sqlite3_errmsg(newDb));
15219 sqlite3_reset(pInsert);
15221 if( (cnt%spinRate)==0 ){
15222 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
15226 if( rc==SQLITE_DONE ) break;
15227 sqlite3_finalize(pQuery);
15228 sqlite3_free(zQuery);
15229 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
15231 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15233 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
15236 } /* End for(k=0...) */
15239 sqlite3_finalize(pQuery);
15240 sqlite3_finalize(pInsert);
15241 sqlite3_free(zQuery);
15242 sqlite3_free(zInsert);
15247 ** Try to transfer all rows of the schema that match zWhere. For
15248 ** each row, invoke xForEach() on the object defined by that row.
15249 ** If an error is encountered while moving forward through the
15250 ** sqlite_schema table, try again moving backwards.
15252 static void tryToCloneSchema(
15255 const char *zWhere,
15256 void (*xForEach)(ShellState*,sqlite3*,const char*)
15258 sqlite3_stmt *pQuery = 0;
15261 const unsigned char *zName;
15262 const unsigned char *zSql;
15265 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15266 " WHERE %s", zWhere);
15267 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15269 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15270 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15272 goto end_schema_xfer;
15274 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15275 zName = sqlite3_column_text(pQuery, 0);
15276 zSql = sqlite3_column_text(pQuery, 1);
15277 printf("%s... ", zName); fflush(stdout);
15278 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15280 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15281 sqlite3_free(zErrMsg);
15285 xForEach(p, newDb, (const char*)zName);
15289 if( rc!=SQLITE_DONE ){
15290 sqlite3_finalize(pQuery);
15291 sqlite3_free(zQuery);
15292 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_schema"
15293 " WHERE %s ORDER BY rowid DESC", zWhere);
15294 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
15296 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
15297 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
15299 goto end_schema_xfer;
15301 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
15302 zName = sqlite3_column_text(pQuery, 0);
15303 zSql = sqlite3_column_text(pQuery, 1);
15304 printf("%s... ", zName); fflush(stdout);
15305 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
15307 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
15308 sqlite3_free(zErrMsg);
15312 xForEach(p, newDb, (const char*)zName);
15318 sqlite3_finalize(pQuery);
15319 sqlite3_free(zQuery);
15323 ** Open a new database file named "zNewDb". Try to recover as much information
15324 ** as possible out of the main database (which might be corrupt) and write it
15327 static void tryToClone(ShellState *p, const char *zNewDb){
15329 sqlite3 *newDb = 0;
15330 if( access(zNewDb,0)==0 ){
15331 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
15334 rc = sqlite3_open(zNewDb, &newDb);
15336 utf8_printf(stderr, "Cannot create output database: %s\n",
15337 sqlite3_errmsg(newDb));
15339 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
15340 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
15341 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
15342 tryToCloneSchema(p, newDb, "type!='table'", 0);
15343 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
15344 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
15350 ** Change the output file back to stdout.
15352 ** If the p->doXdgOpen flag is set, that means the output was being
15353 ** redirected to a temporary file named by p->zTempFile. In that case,
15354 ** launch start/open/xdg-open on that temporary file.
15356 static void output_reset(ShellState *p){
15357 if( p->outfile[0]=='|' ){
15358 #ifndef SQLITE_OMIT_POPEN
15362 output_file_close(p->out);
15363 #ifndef SQLITE_NOHAVE_SYSTEM
15364 if( p->doXdgOpen ){
15365 const char *zXdgOpenCmd =
15366 #if defined(_WIN32)
15368 #elif defined(__APPLE__)
15374 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
15375 if( system(zCmd) ){
15376 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
15378 /* Give the start/open/xdg-open command some time to get
15379 ** going before we continue, and potential delete the
15380 ** p->zTempFile data file out from under it */
15381 sqlite3_sleep(2000);
15383 sqlite3_free(zCmd);
15387 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
15394 ** Run an SQL command and return the single integer result.
15396 static int db_int(ShellState *p, const char *zSql){
15397 sqlite3_stmt *pStmt;
15399 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
15400 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
15401 res = sqlite3_column_int(pStmt,0);
15403 sqlite3_finalize(pStmt);
15408 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
15410 static unsigned int get2byteInt(unsigned char *a){
15411 return (a[0]<<8) + a[1];
15413 static unsigned int get4byteInt(unsigned char *a){
15414 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
15418 ** Implementation of the ".dbinfo" command.
15420 ** Return 1 on error, 2 to exit, and 0 otherwise.
15422 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
15423 static const struct { const char *zName; int ofst; } aField[] = {
15424 { "file change counter:", 24 },
15425 { "database page count:", 28 },
15426 { "freelist page count:", 36 },
15427 { "schema cookie:", 40 },
15428 { "schema format:", 44 },
15429 { "default cache size:", 48 },
15430 { "autovacuum top root:", 52 },
15431 { "incremental vacuum:", 64 },
15432 { "text encoding:", 56 },
15433 { "user version:", 60 },
15434 { "application id:", 68 },
15435 { "software version:", 96 },
15437 static const struct { const char *zName; const char *zSql; } aQuery[] = {
15438 { "number of tables:",
15439 "SELECT count(*) FROM %s WHERE type='table'" },
15440 { "number of indexes:",
15441 "SELECT count(*) FROM %s WHERE type='index'" },
15442 { "number of triggers:",
15443 "SELECT count(*) FROM %s WHERE type='trigger'" },
15444 { "number of views:",
15445 "SELECT count(*) FROM %s WHERE type='view'" },
15447 "SELECT total(length(sql)) FROM %s" },
15450 unsigned iDataVersion;
15452 char *zDb = nArg>=2 ? azArg[1] : "main";
15453 sqlite3_stmt *pStmt = 0;
15454 unsigned char aHdr[100];
15456 if( p->db==0 ) return 1;
15457 rc = sqlite3_prepare_v2(p->db,
15458 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
15461 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
15462 sqlite3_finalize(pStmt);
15465 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
15466 if( sqlite3_step(pStmt)==SQLITE_ROW
15467 && sqlite3_column_bytes(pStmt,0)>100
15469 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
15470 sqlite3_finalize(pStmt);
15472 raw_printf(stderr, "unable to read database header\n");
15473 sqlite3_finalize(pStmt);
15476 i = get2byteInt(aHdr+16);
15477 if( i==1 ) i = 65536;
15478 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
15479 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
15480 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
15481 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
15482 for(i=0; i<ArraySize(aField); i++){
15483 int ofst = aField[i].ofst;
15484 unsigned int val = get4byteInt(aHdr + ofst);
15485 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
15488 if( val==1 ) raw_printf(p->out, " (utf8)");
15489 if( val==2 ) raw_printf(p->out, " (utf16le)");
15490 if( val==3 ) raw_printf(p->out, " (utf16be)");
15493 raw_printf(p->out, "\n");
15496 zSchemaTab = sqlite3_mprintf("main.sqlite_schema");
15497 }else if( strcmp(zDb,"temp")==0 ){
15498 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_schema");
15500 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_schema", zDb);
15502 for(i=0; i<ArraySize(aQuery); i++){
15503 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
15504 int val = db_int(p, zSql);
15505 sqlite3_free(zSql);
15506 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
15508 sqlite3_free(zSchemaTab);
15509 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
15510 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
15515 ** Print the current sqlite3_errmsg() value to stderr and return 1.
15517 static int shellDatabaseError(sqlite3 *db){
15518 const char *zErr = sqlite3_errmsg(db);
15519 utf8_printf(stderr, "Error: %s\n", zErr);
15524 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
15525 ** if they match and FALSE (0) if they do not match.
15529 ** '*' Matches any sequence of zero or more characters.
15531 ** '?' Matches exactly one character.
15533 ** [...] Matches one character from the enclosed list of
15536 ** [^...] Matches one character not in the enclosed list.
15538 ** '#' Matches any sequence of one or more digits with an
15539 ** optional + or - sign in front
15541 ** ' ' Any span of whitespace matches any other span of
15544 ** Extra whitespace at the end of z[] is ignored.
15546 static int testcase_glob(const char *zGlob, const char *z){
15551 while( (c = (*(zGlob++)))!=0 ){
15553 if( !IsSpace(*z) ) return 0;
15554 while( IsSpace(*zGlob) ) zGlob++;
15555 while( IsSpace(*z) ) z++;
15556 }else if( c=='*' ){
15557 while( (c=(*(zGlob++))) == '*' || c=='?' ){
15558 if( c=='?' && (*(z++))==0 ) return 0;
15562 }else if( c=='[' ){
15563 while( *z && testcase_glob(zGlob-1,z)==0 ){
15568 while( (c2 = (*(z++)))!=0 ){
15571 if( c2==0 ) return 0;
15573 if( testcase_glob(zGlob,z) ) return 1;
15576 }else if( c=='?' ){
15577 if( (*(z++))==0 ) return 0;
15578 }else if( c=='[' ){
15583 if( c==0 ) return 0;
15590 if( c==']' ) seen = 1;
15593 while( c2 && c2!=']' ){
15594 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
15596 if( c>=prior_c && c<=c2 ) seen = 1;
15606 if( c2==0 || (seen ^ invert)==0 ) return 0;
15607 }else if( c=='#' ){
15608 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
15609 if( !IsDigit(z[0]) ) return 0;
15611 while( IsDigit(z[0]) ){ z++; }
15613 if( c!=(*(z++)) ) return 0;
15616 while( IsSpace(*z) ){ z++; }
15622 ** Compare the string as a command-line option with either one or two
15623 ** initial "-" characters.
15625 static int optionMatch(const char *zStr, const char *zOpt){
15626 if( zStr[0]!='-' ) return 0;
15628 if( zStr[0]=='-' ) zStr++;
15629 return strcmp(zStr, zOpt)==0;
15635 int shellDeleteFile(const char *zFilename){
15638 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
15642 rc = unlink(zFilename);
15648 ** Try to delete the temporary file (if there is one) and free the
15649 ** memory used to hold the name of the temp file.
15651 static void clearTempFile(ShellState *p){
15652 if( p->zTempFile==0 ) return;
15653 if( p->doXdgOpen ) return;
15654 if( shellDeleteFile(p->zTempFile) ) return;
15655 sqlite3_free(p->zTempFile);
15660 ** Create a new temp file name with the given suffix.
15662 static void newTempFile(ShellState *p, const char *zSuffix){
15664 sqlite3_free(p->zTempFile);
15667 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
15669 if( p->zTempFile==0 ){
15670 /* If p->db is an in-memory database then the TEMPFILENAME file-control
15671 ** will not work and we will need to fallback to guessing */
15674 sqlite3_randomness(sizeof(r), &r);
15675 zTemp = getenv("TEMP");
15676 if( zTemp==0 ) zTemp = getenv("TMP");
15684 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
15686 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
15688 if( p->zTempFile==0 ){
15689 raw_printf(stderr, "out of memory\n");
15696 ** The implementation of SQL scalar function fkey_collate_clause(), used
15697 ** by the ".lint fkey-indexes" command. This scalar function is always
15698 ** called with four arguments - the parent table name, the parent column name,
15699 ** the child table name and the child column name.
15701 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
15703 ** If either of the named tables or columns do not exist, this function
15704 ** returns an empty string. An empty string is also returned if both tables
15705 ** and columns exist but have the same default collation sequence. Or,
15706 ** if both exist but the default collation sequences are different, this
15707 ** function returns the string " COLLATE <parent-collation>", where
15708 ** <parent-collation> is the default collation sequence of the parent column.
15710 static void shellFkeyCollateClause(
15711 sqlite3_context *pCtx,
15713 sqlite3_value **apVal
15715 sqlite3 *db = sqlite3_context_db_handle(pCtx);
15716 const char *zParent;
15717 const char *zParentCol;
15718 const char *zParentSeq;
15719 const char *zChild;
15720 const char *zChildCol;
15721 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
15725 zParent = (const char*)sqlite3_value_text(apVal[0]);
15726 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
15727 zChild = (const char*)sqlite3_value_text(apVal[2]);
15728 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
15730 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
15731 rc = sqlite3_table_column_metadata(
15732 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
15734 if( rc==SQLITE_OK ){
15735 rc = sqlite3_table_column_metadata(
15736 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
15740 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
15741 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
15742 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
15749 ** The implementation of dot-command ".lint fkey-indexes".
15751 static int lintFkeyIndexes(
15752 ShellState *pState, /* Current shell tool state */
15753 char **azArg, /* Array of arguments passed to dot command */
15754 int nArg /* Number of entries in azArg[] */
15756 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
15757 FILE *out = pState->out; /* Stream to write non-error output to */
15758 int bVerbose = 0; /* If -verbose is present */
15759 int bGroupByParent = 0; /* If -groupbyparent is present */
15760 int i; /* To iterate through azArg[] */
15761 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
15762 int rc; /* Return code */
15763 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
15766 ** This SELECT statement returns one row for each foreign key constraint
15767 ** in the schema of the main database. The column values are:
15769 ** 0. The text of an SQL statement similar to:
15771 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
15773 ** This SELECT is similar to the one that the foreign keys implementation
15774 ** needs to run internally on child tables. If there is an index that can
15775 ** be used to optimize this query, then it can also be used by the FK
15776 ** implementation to optimize DELETE or UPDATE statements on the parent
15779 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
15780 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
15781 ** contains an index that can be used to optimize the query.
15783 ** 2. Human readable text that describes the child table and columns. e.g.
15785 ** "child_table(child_key1, child_key2)"
15787 ** 3. Human readable text that describes the parent table and columns. e.g.
15789 ** "parent_table(parent_key1, parent_key2)"
15791 ** 4. A full CREATE INDEX statement for an index that could be used to
15792 ** optimize DELETE or UPDATE statements on the parent table. e.g.
15794 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
15796 ** 5. The name of the parent table.
15798 ** These six values are used by the C logic below to generate the report.
15802 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
15803 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
15804 " || fkey_collate_clause("
15805 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
15807 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
15808 " || group_concat('*=?', ' AND ') || ')'"
15810 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
15812 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
15814 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
15815 " || ' ON ' || quote(s.name) || '('"
15816 " || group_concat(quote(f.[from]) ||"
15817 " fkey_collate_clause("
15818 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
15822 "FROM sqlite_schema AS s, pragma_foreign_key_list(s.name) AS f "
15823 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
15824 "GROUP BY s.name, f.id "
15825 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
15827 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
15829 for(i=2; i<nArg; i++){
15830 int n = strlen30(azArg[i]);
15831 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
15834 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
15835 bGroupByParent = 1;
15839 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
15842 return SQLITE_ERROR;
15846 /* Register the fkey_collate_clause() SQL function */
15847 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
15848 0, shellFkeyCollateClause, 0, 0
15852 if( rc==SQLITE_OK ){
15853 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
15855 if( rc==SQLITE_OK ){
15856 sqlite3_bind_int(pSql, 1, bGroupByParent);
15859 if( rc==SQLITE_OK ){
15862 while( SQLITE_ROW==sqlite3_step(pSql) ){
15864 sqlite3_stmt *pExplain = 0;
15865 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
15866 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
15867 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
15868 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
15869 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
15870 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
15872 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
15873 if( rc!=SQLITE_OK ) break;
15874 if( SQLITE_ROW==sqlite3_step(pExplain) ){
15875 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
15877 0==sqlite3_strglob(zGlob, zPlan)
15878 || 0==sqlite3_strglob(zGlobIPK, zPlan)
15881 rc = sqlite3_finalize(pExplain);
15882 if( rc!=SQLITE_OK ) break;
15885 raw_printf(stderr, "Error: internal error");
15889 && (bVerbose || res==0)
15890 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
15892 raw_printf(out, "-- Parent table %s\n", zParent);
15893 sqlite3_free(zPrev);
15894 zPrev = sqlite3_mprintf("%s", zParent);
15898 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
15899 }else if( bVerbose ){
15900 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
15901 zIndent, zFrom, zTarget
15906 sqlite3_free(zPrev);
15908 if( rc!=SQLITE_OK ){
15909 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15912 rc2 = sqlite3_finalize(pSql);
15913 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
15915 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15918 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
15925 ** Implementation of ".lint" dot command.
15927 static int lintDotCommand(
15928 ShellState *pState, /* Current shell tool state */
15929 char **azArg, /* Array of arguments passed to dot command */
15930 int nArg /* Number of entries in azArg[] */
15933 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
15934 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
15935 return lintFkeyIndexes(pState, azArg, nArg);
15938 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
15939 raw_printf(stderr, "Where sub-commands are:\n");
15940 raw_printf(stderr, " fkey-indexes\n");
15941 return SQLITE_ERROR;
15944 #if !defined SQLITE_OMIT_VIRTUALTABLE
15945 static void shellPrepare(
15949 sqlite3_stmt **ppStmt
15952 if( *pRc==SQLITE_OK ){
15953 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
15954 if( rc!=SQLITE_OK ){
15955 raw_printf(stderr, "sql error: %s (%d)\n",
15956 sqlite3_errmsg(db), sqlite3_errcode(db)
15964 ** Create a prepared statement using printf-style arguments for the SQL.
15966 ** This routine is could be marked "static". But it is not always used,
15967 ** depending on compile-time options. By omitting the "static", we avoid
15968 ** nuisance compiler warnings about "defined but not used".
15970 void shellPreparePrintf(
15973 sqlite3_stmt **ppStmt,
15978 if( *pRc==SQLITE_OK ){
15981 va_start(ap, zFmt);
15982 z = sqlite3_vmprintf(zFmt, ap);
15985 *pRc = SQLITE_NOMEM;
15987 shellPrepare(db, pRc, z, ppStmt);
15993 /* Finalize the prepared statement created using shellPreparePrintf().
15995 ** This routine is could be marked "static". But it is not always used,
15996 ** depending on compile-time options. By omitting the "static", we avoid
15997 ** nuisance compiler warnings about "defined but not used".
15999 void shellFinalize(
16001 sqlite3_stmt *pStmt
16004 sqlite3 *db = sqlite3_db_handle(pStmt);
16005 int rc = sqlite3_finalize(pStmt);
16006 if( *pRc==SQLITE_OK ){
16007 if( rc!=SQLITE_OK ){
16008 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16015 /* Reset the prepared statement created using shellPreparePrintf().
16017 ** This routine is could be marked "static". But it is not always used,
16018 ** depending on compile-time options. By omitting the "static", we avoid
16019 ** nuisance compiler warnings about "defined but not used".
16023 sqlite3_stmt *pStmt
16025 int rc = sqlite3_reset(pStmt);
16026 if( *pRc==SQLITE_OK ){
16027 if( rc!=SQLITE_OK ){
16028 sqlite3 *db = sqlite3_db_handle(pStmt);
16029 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
16034 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
16036 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
16037 /******************************************************************************
16038 ** The ".archive" or ".ar" command.
16041 ** Structure representing a single ".ar" command.
16043 typedef struct ArCommand ArCommand;
16045 u8 eCmd; /* An AR_CMD_* value */
16046 u8 bVerbose; /* True if --verbose */
16047 u8 bZip; /* True if the archive is a ZIP */
16048 u8 bDryRun; /* True if --dry-run */
16049 u8 bAppend; /* True if --append */
16050 u8 fromCmdLine; /* Run from -A instead of .archive */
16051 int nArg; /* Number of command arguments */
16052 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
16053 const char *zFile; /* --file argument, or NULL */
16054 const char *zDir; /* --directory argument, or NULL */
16055 char **azArg; /* Array of command arguments */
16056 ShellState *p; /* Shell state */
16057 sqlite3 *db; /* Database containing the archive */
16061 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
16063 static int arUsage(FILE *f){
16064 showHelp(f,"archive");
16065 return SQLITE_ERROR;
16069 ** Print an error message for the .ar command to stderr and return
16072 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
16075 va_start(ap, zFmt);
16076 z = sqlite3_vmprintf(zFmt, ap);
16078 utf8_printf(stderr, "Error: %s\n", z);
16079 if( pAr->fromCmdLine ){
16080 utf8_printf(stderr, "Use \"-A\" for more help\n");
16082 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
16085 return SQLITE_ERROR;
16089 ** Values for ArCommand.eCmd.
16091 #define AR_CMD_CREATE 1
16092 #define AR_CMD_UPDATE 2
16093 #define AR_CMD_INSERT 3
16094 #define AR_CMD_EXTRACT 4
16095 #define AR_CMD_LIST 5
16096 #define AR_CMD_HELP 6
16099 ** Other (non-command) switches.
16101 #define AR_SWITCH_VERBOSE 7
16102 #define AR_SWITCH_FILE 8
16103 #define AR_SWITCH_DIRECTORY 9
16104 #define AR_SWITCH_APPEND 10
16105 #define AR_SWITCH_DRYRUN 11
16107 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
16109 case AR_CMD_CREATE:
16110 case AR_CMD_EXTRACT:
16112 case AR_CMD_UPDATE:
16113 case AR_CMD_INSERT:
16116 return arErrorMsg(pAr, "multiple command options");
16118 pAr->eCmd = eSwitch;
16121 case AR_SWITCH_DRYRUN:
16124 case AR_SWITCH_VERBOSE:
16127 case AR_SWITCH_APPEND:
16129 /* Fall thru into --file */
16130 case AR_SWITCH_FILE:
16133 case AR_SWITCH_DIRECTORY:
16142 ** Parse the command line for an ".ar" command. The results are written into
16143 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
16144 ** successfully, otherwise an error message is written to stderr and
16145 ** SQLITE_ERROR returned.
16147 static int arParseCommand(
16148 char **azArg, /* Array of arguments passed to dot command */
16149 int nArg, /* Number of entries in azArg[] */
16150 ArCommand *pAr /* Populate this object */
16158 { "create", 'c', AR_CMD_CREATE, 0 },
16159 { "extract", 'x', AR_CMD_EXTRACT, 0 },
16160 { "insert", 'i', AR_CMD_INSERT, 0 },
16161 { "list", 't', AR_CMD_LIST, 0 },
16162 { "update", 'u', AR_CMD_UPDATE, 0 },
16163 { "help", 'h', AR_CMD_HELP, 0 },
16164 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
16165 { "file", 'f', AR_SWITCH_FILE, 1 },
16166 { "append", 'a', AR_SWITCH_APPEND, 1 },
16167 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
16168 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
16170 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
16171 struct ArSwitch *pEnd = &aSwitch[nSwitch];
16174 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
16175 return arUsage(stderr);
16177 char *z = azArg[1];
16179 /* Traditional style [tar] invocation */
16182 for(i=0; z[i]; i++){
16183 const char *zArg = 0;
16184 struct ArSwitch *pOpt;
16185 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16186 if( z[i]==pOpt->cShort ) break;
16189 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16193 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
16195 zArg = azArg[iArg++];
16197 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16199 pAr->nArg = nArg-iArg;
16201 pAr->azArg = &azArg[iArg];
16204 /* Non-traditional invocation */
16206 for(iArg=1; iArg<nArg; iArg++){
16210 /* All remaining command line words are command arguments. */
16211 pAr->azArg = &azArg[iArg];
16212 pAr->nArg = nArg-iArg;
16219 /* One or more short options */
16220 for(i=1; i<n; i++){
16221 const char *zArg = 0;
16222 struct ArSwitch *pOpt;
16223 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16224 if( z[i]==pOpt->cShort ) break;
16227 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
16234 if( iArg>=(nArg-1) ){
16235 return arErrorMsg(pAr, "option requires an argument: %c",
16238 zArg = azArg[++iArg];
16241 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
16243 }else if( z[2]=='\0' ){
16244 /* A -- option, indicating that all remaining command line words
16245 ** are command arguments. */
16246 pAr->azArg = &azArg[iArg+1];
16247 pAr->nArg = nArg-iArg-1;
16250 /* A long option */
16251 const char *zArg = 0; /* Argument for option, if any */
16252 struct ArSwitch *pMatch = 0; /* Matching option */
16253 struct ArSwitch *pOpt; /* Iterator */
16254 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
16255 const char *zLong = pOpt->zLong;
16256 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
16258 return arErrorMsg(pAr, "ambiguous option: %s",z);
16266 return arErrorMsg(pAr, "unrecognized option: %s", z);
16268 if( pMatch->bArg ){
16269 if( iArg>=(nArg-1) ){
16270 return arErrorMsg(pAr, "option requires an argument: %s", z);
16272 zArg = azArg[++iArg];
16274 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
16284 ** This function assumes that all arguments within the ArCommand.azArg[]
16285 ** array refer to archive members, as for the --extract or --list commands.
16286 ** It checks that each of them are present. If any specified file is not
16287 ** present in the archive, an error is printed to stderr and an error
16288 ** code returned. Otherwise, if all specified arguments are present in
16289 ** the archive, SQLITE_OK is returned.
16291 ** This function strips any trailing '/' characters from each argument.
16292 ** This is consistent with the way the [tar] command seems to work on
16295 static int arCheckEntries(ArCommand *pAr){
16296 int rc = SQLITE_OK;
16299 sqlite3_stmt *pTest = 0;
16301 shellPreparePrintf(pAr->db, &rc, &pTest,
16302 "SELECT name FROM %s WHERE name=$name",
16305 j = sqlite3_bind_parameter_index(pTest, "$name");
16306 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16307 char *z = pAr->azArg[i];
16308 int n = strlen30(z);
16310 while( n>0 && z[n-1]=='/' ) n--;
16312 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
16313 if( SQLITE_ROW==sqlite3_step(pTest) ){
16316 shellReset(&rc, pTest);
16317 if( rc==SQLITE_OK && bOk==0 ){
16318 utf8_printf(stderr, "not found in archive: %s\n", z);
16322 shellFinalize(&rc, pTest);
16328 ** Format a WHERE clause that can be used against the "sqlar" table to
16329 ** identify all archive members that match the command arguments held
16330 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
16331 ** The caller is responsible for eventually calling sqlite3_free() on
16332 ** any non-NULL (*pzWhere) value.
16334 static void arWhereClause(
16337 char **pzWhere /* OUT: New WHERE clause */
16340 if( *pRc==SQLITE_OK ){
16341 if( pAr->nArg==0 ){
16342 zWhere = sqlite3_mprintf("1");
16345 const char *zSep = "";
16346 for(i=0; i<pAr->nArg; i++){
16347 const char *z = pAr->azArg[i];
16348 zWhere = sqlite3_mprintf(
16349 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
16350 zWhere, zSep, z, strlen30(z)+1, z
16353 *pRc = SQLITE_NOMEM;
16364 ** Implementation of .ar "lisT" command.
16366 static int arListCommand(ArCommand *pAr){
16367 const char *zSql = "SELECT %s FROM %s WHERE %s";
16368 const char *azCols[] = {
16370 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
16374 sqlite3_stmt *pSql = 0;
16377 rc = arCheckEntries(pAr);
16378 arWhereClause(&rc, pAr, &zWhere);
16380 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
16381 pAr->zSrcTable, zWhere);
16382 if( pAr->bDryRun ){
16383 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16385 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16386 if( pAr->bVerbose ){
16387 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
16388 sqlite3_column_text(pSql, 0),
16389 sqlite3_column_int(pSql, 1),
16390 sqlite3_column_text(pSql, 2),
16391 sqlite3_column_text(pSql, 3)
16394 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16398 shellFinalize(&rc, pSql);
16399 sqlite3_free(zWhere);
16405 ** Implementation of .ar "eXtract" command.
16407 static int arExtractCommand(ArCommand *pAr){
16408 const char *zSql1 =
16411 " writefile(($dir || name), %s, mode, mtime) "
16412 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
16413 " AND name NOT GLOB '*..[/\\]*'";
16415 const char *azExtraArg[] = {
16416 "sqlar_uncompress(data, sz)",
16420 sqlite3_stmt *pSql = 0;
16421 int rc = SQLITE_OK;
16426 /* If arguments are specified, check that they actually exist within
16427 ** the archive before proceeding. And formulate a WHERE clause to
16429 rc = arCheckEntries(pAr);
16430 arWhereClause(&rc, pAr, &zWhere);
16432 if( rc==SQLITE_OK ){
16434 zDir = sqlite3_mprintf("%s/", pAr->zDir);
16436 zDir = sqlite3_mprintf("");
16438 if( zDir==0 ) rc = SQLITE_NOMEM;
16441 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
16442 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
16445 if( rc==SQLITE_OK ){
16446 j = sqlite3_bind_parameter_index(pSql, "$dir");
16447 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
16449 /* Run the SELECT statement twice. The first time, writefile() is called
16450 ** for all archive members that should be extracted. The second time,
16451 ** only for the directories. This is because the timestamps for
16452 ** extracted directories must be reset after they are populated (as
16453 ** populating them changes the timestamp). */
16454 for(i=0; i<2; i++){
16455 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
16456 sqlite3_bind_int(pSql, j, i);
16457 if( pAr->bDryRun ){
16458 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
16460 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
16461 if( i==0 && pAr->bVerbose ){
16462 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
16466 shellReset(&rc, pSql);
16468 shellFinalize(&rc, pSql);
16471 sqlite3_free(zDir);
16472 sqlite3_free(zWhere);
16477 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
16479 static int arExecSql(ArCommand *pAr, const char *zSql){
16481 if( pAr->bDryRun ){
16482 utf8_printf(pAr->p->out, "%s\n", zSql);
16486 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
16488 utf8_printf(stdout, "ERROR: %s\n", zErr);
16489 sqlite3_free(zErr);
16497 ** Implementation of .ar "create", "insert", and "update" commands.
16499 ** create -> Create a new SQL archive
16500 ** insert -> Insert or reinsert all files listed
16501 ** update -> Insert files that have changed or that were not
16502 ** previously in the archive
16504 ** Create the "sqlar" table in the database if it does not already exist.
16505 ** Then add each file in the azFile[] array to the archive. Directories
16506 ** are added recursively. If argument bVerbose is non-zero, a message is
16507 ** printed on stdout for each file archived.
16509 ** The create command is the same as update, except that it drops
16510 ** any existing "sqlar" table before beginning. The "insert" command
16511 ** always overwrites every file named on the command-line, where as
16512 ** "update" only overwrites if the size or mtime or mode has changed.
16514 static int arCreateOrUpdateCommand(
16515 ArCommand *pAr, /* Command arguments and options */
16516 int bUpdate, /* true for a --create. */
16517 int bOnlyIfChanged /* Only update if file has changed */
16519 const char *zCreate =
16520 "CREATE TABLE IF NOT EXISTS sqlar(\n"
16521 " name TEXT PRIMARY KEY, -- name of the file\n"
16522 " mode INT, -- access permissions\n"
16523 " mtime INT, -- last modification time\n"
16524 " sz INT, -- original file size\n"
16525 " data BLOB -- compressed content\n"
16527 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
16528 const char *zInsertFmt[2] = {
16529 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
16534 " CASE substr(lsmode(mode),1,1)\n"
16535 " WHEN '-' THEN length(data)\n"
16536 " WHEN 'd' THEN 0\n"
16538 " sqlar_compress(data)\n"
16539 " FROM fsdir(%Q,%Q) AS disk\n"
16540 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16542 "REPLACE INTO %s(name,mode,mtime,data)\n"
16548 " FROM fsdir(%Q,%Q) AS disk\n"
16549 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
16551 int i; /* For iterating through azFile[] */
16552 int rc; /* Return code */
16553 const char *zTab = 0; /* SQL table into which to insert */
16558 arExecSql(pAr, "PRAGMA page_size=512");
16559 rc = arExecSql(pAr, "SAVEPOINT ar;");
16560 if( rc!=SQLITE_OK ) return rc;
16563 /* Initialize the zipfile virtual table, if necessary */
16566 sqlite3_randomness(sizeof(r),&r);
16567 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
16569 zSql = sqlite3_mprintf(
16570 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
16573 rc = arExecSql(pAr, zSql);
16574 sqlite3_free(zSql);
16579 /* Initialize the table for an SQLAR */
16582 rc = arExecSql(pAr, zDrop);
16583 if( rc!=SQLITE_OK ) goto end_ar_transaction;
16585 rc = arExecSql(pAr, zCreate);
16587 if( bOnlyIfChanged ){
16588 zExists = sqlite3_mprintf(
16590 "SELECT 1 FROM %s AS mem"
16591 " WHERE mem.name=disk.name"
16592 " AND mem.mtime=disk.mtime"
16593 " AND mem.mode=disk.mode)", zTab);
16595 zExists = sqlite3_mprintf("");
16597 if( zExists==0 ) rc = SQLITE_NOMEM;
16598 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
16599 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
16600 pAr->bVerbose ? "shell_putsnl(name)" : "name",
16601 pAr->azArg[i], pAr->zDir, zExists);
16602 rc = arExecSql(pAr, zSql2);
16603 sqlite3_free(zSql2);
16605 end_ar_transaction:
16606 if( rc!=SQLITE_OK ){
16607 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
16609 rc = arExecSql(pAr, "RELEASE ar;");
16610 if( pAr->bZip && pAr->zFile ){
16611 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
16612 arExecSql(pAr, zSql);
16613 sqlite3_free(zSql);
16616 sqlite3_free(zExists);
16621 ** Implementation of ".ar" dot command.
16623 static int arDotCommand(
16624 ShellState *pState, /* Current shell tool state */
16625 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
16626 char **azArg, /* Array of arguments passed to dot command */
16627 int nArg /* Number of entries in azArg[] */
16631 memset(&cmd, 0, sizeof(cmd));
16632 cmd.fromCmdLine = fromCmdLine;
16633 rc = arParseCommand(azArg, nArg, &cmd);
16634 if( rc==SQLITE_OK ){
16635 int eDbType = SHELL_OPEN_UNSPEC;
16637 cmd.db = pState->db;
16639 eDbType = deduceDatabaseType(cmd.zFile, 1);
16641 eDbType = pState->openMode;
16643 if( eDbType==SHELL_OPEN_ZIPFILE ){
16644 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
16645 if( cmd.zFile==0 ){
16646 cmd.zSrcTable = sqlite3_mprintf("zip");
16648 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
16652 }else if( cmd.zFile ){
16654 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
16655 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
16656 || cmd.eCmd==AR_CMD_UPDATE ){
16657 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
16659 flags = SQLITE_OPEN_READONLY;
16663 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
16664 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
16666 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
16667 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
16668 if( rc!=SQLITE_OK ){
16669 utf8_printf(stderr, "cannot open file: %s (%s)\n",
16670 cmd.zFile, sqlite3_errmsg(cmd.db)
16672 goto end_ar_command;
16674 sqlite3_fileio_init(cmd.db, 0, 0);
16675 sqlite3_sqlar_init(cmd.db, 0, 0);
16676 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
16677 shellPutsFunc, 0, 0);
16680 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
16681 if( cmd.eCmd!=AR_CMD_CREATE
16682 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
16684 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
16686 goto end_ar_command;
16688 cmd.zSrcTable = sqlite3_mprintf("sqlar");
16691 switch( cmd.eCmd ){
16692 case AR_CMD_CREATE:
16693 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
16696 case AR_CMD_EXTRACT:
16697 rc = arExtractCommand(&cmd);
16701 rc = arListCommand(&cmd);
16705 arUsage(pState->out);
16708 case AR_CMD_INSERT:
16709 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
16713 assert( cmd.eCmd==AR_CMD_UPDATE );
16714 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
16719 if( cmd.db!=pState->db ){
16722 sqlite3_free(cmd.zSrcTable);
16726 /* End of the ".archive" or ".ar" command logic
16727 *******************************************************************************/
16728 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
16730 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
16732 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
16733 ** Otherwise, the SQL statement or statements in zSql are executed using
16734 ** database connection db and the error code written to *pRc before
16735 ** this function returns.
16737 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
16739 if( rc==SQLITE_OK ){
16741 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
16742 if( rc!=SQLITE_OK ){
16743 raw_printf(stderr, "SQL error: %s\n", zErr);
16750 ** Like shellExec(), except that zFmt is a printf() style format string.
16752 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
16754 if( *pRc==SQLITE_OK ){
16756 va_start(ap, zFmt);
16757 z = sqlite3_vmprintf(zFmt, ap);
16760 *pRc = SQLITE_NOMEM;
16762 shellExec(db, pRc, z);
16769 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16770 ** Otherwise, an attempt is made to allocate, zero and return a pointer
16771 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
16772 ** to SQLITE_NOMEM and NULL returned.
16774 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
16776 if( *pRc==SQLITE_OK ){
16777 pRet = sqlite3_malloc64(nByte);
16779 *pRc = SQLITE_NOMEM;
16781 memset(pRet, 0, nByte);
16788 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
16789 ** Otherwise, zFmt is treated as a printf() style string. The result of
16790 ** formatting it along with any trailing arguments is written into a
16791 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
16792 ** It is the responsibility of the caller to eventually free this buffer
16793 ** using a call to sqlite3_free().
16795 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
16796 ** pointer returned.
16798 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
16800 if( *pRc==SQLITE_OK ){
16802 va_start(ap, zFmt);
16803 z = sqlite3_vmprintf(zFmt, ap);
16806 *pRc = SQLITE_NOMEM;
16813 ** When running the ".recover" command, each output table, and the special
16814 ** orphaned row table if it is required, is represented by an instance
16815 ** of the following struct.
16817 typedef struct RecoverTable RecoverTable;
16818 struct RecoverTable {
16819 char *zQuoted; /* Quoted version of table name */
16820 int nCol; /* Number of columns in table */
16821 char **azlCol; /* Array of column lists */
16822 int iPk; /* Index of IPK column */
16826 ** Free a RecoverTable object allocated by recoverFindTable() or
16827 ** recoverOrphanTable().
16829 static void recoverFreeTable(RecoverTable *pTab){
16831 sqlite3_free(pTab->zQuoted);
16832 if( pTab->azlCol ){
16834 for(i=0; i<=pTab->nCol; i++){
16835 sqlite3_free(pTab->azlCol[i]);
16837 sqlite3_free(pTab->azlCol);
16839 sqlite3_free(pTab);
16844 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
16845 ** Otherwise, it allocates and returns a RecoverTable object based on the
16846 ** final four arguments passed to this function. It is the responsibility
16847 ** of the caller to eventually free the returned object using
16848 ** recoverFreeTable().
16850 static RecoverTable *recoverNewTable(
16851 int *pRc, /* IN/OUT: Error code */
16852 const char *zName, /* Name of table */
16853 const char *zSql, /* CREATE TABLE statement */
16857 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
16859 RecoverTable *pTab = 0;
16861 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
16862 if( rc==SQLITE_OK ){
16864 int bSqlIntkey = 0;
16865 sqlite3_stmt *pStmt = 0;
16867 rc = sqlite3_open("", &dbtmp);
16868 if( rc==SQLITE_OK ){
16869 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
16870 shellIdQuote, 0, 0);
16872 if( rc==SQLITE_OK ){
16873 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
16875 if( rc==SQLITE_OK ){
16876 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
16877 if( rc==SQLITE_ERROR ){
16882 shellPreparePrintf(dbtmp, &rc, &pStmt,
16883 "SELECT count(*) FROM pragma_table_info(%Q)", zName
16885 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16886 nSqlCol = sqlite3_column_int(pStmt, 0);
16888 shellFinalize(&rc, pStmt);
16890 if( rc!=SQLITE_OK || nSqlCol<nCol ){
16894 shellPreparePrintf(dbtmp, &rc, &pStmt,
16896 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
16897 ") FROM sqlite_schema WHERE name = %Q", zName
16899 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16900 bSqlIntkey = sqlite3_column_int(pStmt, 0);
16902 shellFinalize(&rc, pStmt);
16904 if( bIntkey==bSqlIntkey ){
16906 const char *zPk = "_rowid_";
16907 sqlite3_stmt *pPkFinder = 0;
16909 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
16910 ** set zPk to the name of the PK column, and pTab->iPk to the index
16911 ** of the column, where columns are 0-numbered from left to right.
16912 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
16913 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
16916 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
16917 "SELECT cid, name FROM pragma_table_info(%Q) "
16918 " WHERE pk=1 AND type='integer' COLLATE nocase"
16919 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
16922 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
16923 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
16924 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
16928 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
16929 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
16930 pTab->nCol = nSqlCol;
16933 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
16935 pTab->azlCol[0] = shellMPrintf(&rc, "");
16938 shellPreparePrintf(dbtmp, &rc, &pStmt,
16939 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
16940 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
16941 "FROM pragma_table_info(%Q)",
16942 bIntkey ? ", " : "", pTab->iPk,
16943 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
16946 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
16947 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
16948 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
16951 shellFinalize(&rc, pStmt);
16953 shellFinalize(&rc, pPkFinder);
16958 sqlite3_close(dbtmp);
16960 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
16961 recoverFreeTable(pTab);
16968 ** This function is called to search the schema recovered from the
16969 ** sqlite_schema table of the (possibly) corrupt database as part
16970 ** of a ".recover" command. Specifically, for a table with root page
16971 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
16972 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
16975 ** If a table is found, a (RecoverTable*) object is returned. Or, if
16976 ** no such table is found, but bIntkey is false and iRoot is the
16977 ** root page of an index in the recovered schema, then (*pbNoop) is
16978 ** set to true and NULL returned. Or, if there is no such table or
16979 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
16980 ** the caller should write data to the orphans table.
16982 static RecoverTable *recoverFindTable(
16983 ShellState *pState, /* Shell state object */
16984 int *pRc, /* IN/OUT: Error code */
16985 int iRoot, /* Root page of table */
16986 int bIntkey, /* True for an intkey table */
16987 int nCol, /* Number of columns in table */
16988 int *pbNoop /* OUT: True if iRoot is root of index */
16990 sqlite3_stmt *pStmt = 0;
16991 RecoverTable *pRet = 0;
16993 const char *zSql = 0;
16994 const char *zName = 0;
16996 /* Search the recovered schema for an object with root page iRoot. */
16997 shellPreparePrintf(pState->db, pRc, &pStmt,
16998 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
17000 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17001 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
17002 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
17006 if( sqlite3_stricmp(zType, "table")==0 ){
17007 zName = (const char*)sqlite3_column_text(pStmt, 1);
17008 zSql = (const char*)sqlite3_column_text(pStmt, 2);
17009 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
17014 shellFinalize(pRc, pStmt);
17020 ** Return a RecoverTable object representing the orphans table.
17022 static RecoverTable *recoverOrphanTable(
17023 ShellState *pState, /* Shell state object */
17024 int *pRc, /* IN/OUT: Error code */
17025 const char *zLostAndFound, /* Base name for orphans table */
17026 int nCol /* Number of user data columns */
17028 RecoverTable *pTab = 0;
17029 if( nCol>=0 && *pRc==SQLITE_OK ){
17032 /* This block determines the name of the orphan table. The prefered
17033 ** name is zLostAndFound. But if that clashes with another name
17034 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
17035 ** and so on until a non-clashing name is found. */
17037 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
17038 sqlite3_stmt *pTest = 0;
17039 shellPrepare(pState->db, pRc,
17040 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
17042 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17043 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
17044 shellReset(pRc, pTest);
17045 sqlite3_free(zTab);
17046 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
17047 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
17049 shellFinalize(pRc, pTest);
17051 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
17053 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
17057 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
17058 if( pTab->azlCol ){
17059 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
17060 for(i=nCol-1; i>=0; i--){
17061 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
17066 if( *pRc!=SQLITE_OK ){
17067 recoverFreeTable(pTab);
17070 raw_printf(pState->out,
17071 "CREATE TABLE %s(rootpgno INTEGER, "
17072 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
17074 for(i=0; i<nCol; i++){
17075 raw_printf(pState->out, ", c%d", i);
17077 raw_printf(pState->out, ");\n");
17080 sqlite3_free(zTab);
17086 ** This function is called to recover data from the database. A script
17087 ** to construct a new database containing all recovered data is output
17088 ** on stream pState->out.
17090 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
17091 int rc = SQLITE_OK;
17092 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
17093 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
17094 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
17095 const char *zRecoveryDb = ""; /* Name of "recovery" database */
17096 const char *zLostAndFound = "lost_and_found";
17099 RecoverTable *pOrphan = 0;
17101 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
17102 int bRowids = 1; /* 0 if --no-rowids */
17103 for(i=1; i<nArg; i++){
17104 char *z = azArg[i];
17106 if( z[0]=='-' && z[1]=='-' ) z++;
17108 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
17111 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
17113 zRecoveryDb = azArg[i];
17115 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
17117 zLostAndFound = azArg[i];
17119 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
17123 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
17124 showHelp(pState->out, azArg[0]);
17129 shellExecPrintf(pState->db, &rc,
17130 /* Attach an in-memory database named 'recovery'. Create an indexed
17131 ** cache of the sqlite_dbptr virtual table. */
17132 "PRAGMA writable_schema = on;"
17133 "ATTACH %Q AS recovery;"
17134 "DROP TABLE IF EXISTS recovery.dbptr;"
17135 "DROP TABLE IF EXISTS recovery.freelist;"
17136 "DROP TABLE IF EXISTS recovery.map;"
17137 "DROP TABLE IF EXISTS recovery.schema;"
17138 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
17142 shellExec(pState->db, &rc,
17143 "WITH trunk(pgno) AS ("
17144 " SELECT shell_int32("
17145 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
17148 " SELECT shell_int32("
17149 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
17150 " FROM trunk WHERE x>0"
17152 "freelist(data, n, freepgno) AS ("
17153 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
17154 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
17156 " SELECT data, n-1, shell_int32(data, 2+n) "
17157 " FROM freelist WHERE n>=0"
17159 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
17163 /* If this is an auto-vacuum database, add all pointer-map pages to
17164 ** the freelist table. Do this regardless of whether or not
17165 ** --freelist-corrupt was specified. */
17166 shellExec(pState->db, &rc,
17167 "WITH ptrmap(pgno) AS ("
17168 " SELECT 2 WHERE shell_int32("
17169 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
17172 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
17173 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
17175 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
17178 shellExec(pState->db, &rc,
17179 "CREATE TABLE recovery.dbptr("
17180 " pgno, child, PRIMARY KEY(child, pgno)"
17182 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
17183 " SELECT * FROM sqlite_dbptr"
17184 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
17186 /* Delete any pointer to page 1. This ensures that page 1 is considered
17187 ** a root page, regardless of how corrupt the db is. */
17188 "DELETE FROM recovery.dbptr WHERE child = 1;"
17190 /* Delete all pointers to any pages that have more than one pointer
17191 ** to them. Such pages will be treated as root pages when recovering
17193 "DELETE FROM recovery.dbptr WHERE child IN ("
17194 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
17197 /* Create the "map" table that will (eventually) contain instructions
17198 ** for dealing with each page in the db that contains one or more
17200 "CREATE TABLE recovery.map("
17201 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
17204 /* Populate table [map]. If there are circular loops of pages in the
17205 ** database, the following adds all pages in such a loop to the map
17206 ** as individual root pages. This could be handled better. */
17207 "WITH pages(i, maxlen) AS ("
17208 " SELECT page_count, ("
17209 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
17210 " ) FROM pragma_page_count WHERE page_count>0"
17213 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
17214 " ) FROM pages WHERE i>=2"
17216 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
17217 " SELECT i, maxlen, NULL, ("
17218 " WITH p(orig, pgno, parent) AS ("
17219 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
17221 " SELECT i, p.parent, "
17222 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
17224 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
17226 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
17227 "UPDATE recovery.map AS o SET intkey = ("
17228 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
17231 /* Extract data from page 1 and any linked pages into table
17232 ** recovery.schema. With the same schema as an sqlite_schema table. */
17233 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
17234 "INSERT INTO recovery.schema SELECT "
17235 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
17236 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
17237 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
17238 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
17239 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
17240 "FROM sqlite_dbdata WHERE pgno IN ("
17241 " SELECT pgno FROM recovery.map WHERE root=1"
17243 "GROUP BY pgno, cell;"
17244 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
17247 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
17248 ** CREATE TABLE statements that extracted from the existing schema. */
17249 if( rc==SQLITE_OK ){
17250 sqlite3_stmt *pStmt = 0;
17251 /* ".recover" might output content in an order which causes immediate
17252 ** foreign key constraints to be violated. So disable foreign-key
17253 ** constraint enforcement to prevent problems when running the output
17255 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
17256 raw_printf(pState->out, "BEGIN;\n");
17257 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
17258 shellPrepare(pState->db, &rc,
17259 "SELECT sql FROM recovery.schema "
17260 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
17262 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17263 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
17264 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
17268 shellFinalize(&rc, pStmt);
17271 /* Figure out if an orphan table will be required. And if so, how many
17272 ** user columns it should contain */
17273 shellPrepare(pState->db, &rc,
17274 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
17277 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17278 nOrphan = sqlite3_column_int(pLoop, 0);
17280 shellFinalize(&rc, pLoop);
17283 shellPrepare(pState->db, &rc,
17284 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
17287 shellPrepare(pState->db, &rc,
17288 "SELECT max(field), group_concat(shell_escape_crnl(quote"
17289 "(case when (? AND field<0) then NULL else value end)"
17292 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
17293 "GROUP BY cell", &pCells
17296 /* Loop through each root page. */
17297 shellPrepare(pState->db, &rc,
17298 "SELECT root, intkey, max(maxlen) FROM recovery.map"
17299 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
17300 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
17303 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
17304 int iRoot = sqlite3_column_int(pLoop, 0);
17305 int bIntkey = sqlite3_column_int(pLoop, 1);
17306 int nCol = sqlite3_column_int(pLoop, 2);
17308 RecoverTable *pTab;
17310 assert( bIntkey==0 || bIntkey==1 );
17311 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
17312 if( bNoop || rc ) continue;
17315 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17318 if( pTab==0 ) break;
17321 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
17322 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
17324 sqlite3_bind_int(pPages, 1, iRoot);
17325 if( bRowids==0 && pTab->iPk<0 ){
17326 sqlite3_bind_int(pCells, 1, 1);
17328 sqlite3_bind_int(pCells, 1, 0);
17330 sqlite3_bind_int(pCells, 3, pTab->iPk);
17332 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
17333 int iPgno = sqlite3_column_int(pPages, 0);
17334 sqlite3_bind_int(pCells, 2, iPgno);
17335 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
17336 int nField = sqlite3_column_int(pCells, 0);
17337 int iMin = sqlite3_column_int(pCells, 2);
17338 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
17340 RecoverTable *pTab2 = pTab;
17341 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
17343 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
17346 if( pTab2==0 ) break;
17350 if( pTab2==pOrphan ){
17351 raw_printf(pState->out,
17352 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
17353 pTab2->zQuoted, iRoot, iPgno, nField,
17354 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
17357 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
17358 pTab2->zQuoted, pTab2->azlCol[nField], zVal
17362 shellReset(&rc, pCells);
17364 shellReset(&rc, pPages);
17365 if( pTab!=pOrphan ) recoverFreeTable(pTab);
17367 shellFinalize(&rc, pLoop);
17368 shellFinalize(&rc, pPages);
17369 shellFinalize(&rc, pCells);
17370 recoverFreeTable(pOrphan);
17372 /* The rest of the schema */
17373 if( rc==SQLITE_OK ){
17374 sqlite3_stmt *pStmt = 0;
17375 shellPrepare(pState->db, &rc,
17376 "SELECT sql, name FROM recovery.schema "
17377 "WHERE sql NOT LIKE 'create table%'", &pStmt
17379 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
17380 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
17381 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
17382 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
17383 char *zPrint = shellMPrintf(&rc,
17384 "INSERT INTO sqlite_schema VALUES('table', %Q, %Q, 0, %Q)",
17387 raw_printf(pState->out, "%s;\n", zPrint);
17388 sqlite3_free(zPrint);
17390 raw_printf(pState->out, "%s;\n", zSql);
17393 shellFinalize(&rc, pStmt);
17396 if( rc==SQLITE_OK ){
17397 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
17398 raw_printf(pState->out, "COMMIT;\n");
17400 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
17403 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17407 ** If an input line begins with "." then invoke this routine to
17408 ** process that line.
17410 ** Return 1 on error, 2 to exit, and 0 otherwise.
17412 static int do_meta_command(char *zLine, ShellState *p){
17419 #ifndef SQLITE_OMIT_VIRTUALTABLE
17420 if( p->expert.pExpert ){
17421 expertFinish(p, 1, 0);
17425 /* Parse the input line into tokens.
17427 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
17428 while( IsSpace(zLine[h]) ){ h++; }
17429 if( zLine[h]==0 ) break;
17430 if( zLine[h]=='\'' || zLine[h]=='"' ){
17431 int delim = zLine[h++];
17432 azArg[nArg++] = &zLine[h];
17433 while( zLine[h] && zLine[h]!=delim ){
17434 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
17437 if( zLine[h]==delim ){
17440 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
17442 azArg[nArg++] = &zLine[h];
17443 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
17444 if( zLine[h] ) zLine[h++] = 0;
17445 resolve_backslashes(azArg[nArg-1]);
17450 /* Process the input line.
17452 if( nArg==0 ) return 0; /* no tokens, no error */
17453 n = strlen30(azArg[0]);
17457 #ifndef SQLITE_OMIT_AUTHORIZATION
17458 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
17460 raw_printf(stderr, "Usage: .auth ON|OFF\n");
17462 goto meta_command_exit;
17465 if( booleanValue(azArg[1]) ){
17466 sqlite3_set_authorizer(p->db, shellAuth, p);
17468 sqlite3_set_authorizer(p->db, 0, 0);
17473 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
17474 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
17476 rc = arDotCommand(p, 0, azArg, nArg);
17480 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
17481 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
17483 const char *zDestFile = 0;
17484 const char *zDb = 0;
17486 sqlite3_backup *pBackup;
17489 const char *zVfs = 0;
17490 for(j=1; j<nArg; j++){
17491 const char *z = azArg[j];
17493 if( z[1]=='-' ) z++;
17494 if( strcmp(z, "-append")==0 ){
17497 if( strcmp(z, "-async")==0 ){
17501 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
17504 }else if( zDestFile==0 ){
17505 zDestFile = azArg[j];
17506 }else if( zDb==0 ){
17508 zDestFile = azArg[j];
17510 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
17514 if( zDestFile==0 ){
17515 raw_printf(stderr, "missing FILENAME argument on .backup\n");
17518 if( zDb==0 ) zDb = "main";
17519 rc = sqlite3_open_v2(zDestFile, &pDest,
17520 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
17521 if( rc!=SQLITE_OK ){
17522 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
17527 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
17531 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
17533 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17537 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
17538 sqlite3_backup_finish(pBackup);
17539 if( rc==SQLITE_DONE ){
17542 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
17548 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
17550 bail_on_error = booleanValue(azArg[1]);
17552 raw_printf(stderr, "Usage: .bail on|off\n");
17557 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
17559 if( booleanValue(azArg[1]) ){
17560 setBinaryMode(p->out, 1);
17562 setTextMode(p->out, 1);
17565 raw_printf(stderr, "Usage: .binary on|off\n");
17570 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
17572 #if defined(_WIN32) || defined(WIN32)
17573 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
17574 rc = !SetCurrentDirectoryW(z);
17577 rc = chdir(azArg[1]);
17580 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
17584 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
17589 /* The undocumented ".breakpoint" command causes a call to the no-op
17590 ** routine named test_breakpoint().
17592 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
17596 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
17598 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
17600 raw_printf(stderr, "Usage: .changes on|off\n");
17605 /* Cancel output redirection, if it is currently set (by .testcase)
17606 ** Then read the content of the testcase-out.txt file and compare against
17607 ** azArg[1]. If there are differences, report an error and exit.
17609 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
17613 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
17615 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
17616 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
17618 }else if( testcase_glob(azArg[1],zRes)==0 ){
17619 utf8_printf(stderr,
17620 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
17621 p->zTestcase, azArg[1], zRes);
17624 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
17627 sqlite3_free(zRes);
17630 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
17632 tryToClone(p, azArg[1]);
17634 raw_printf(stderr, "Usage: .clone FILENAME\n");
17639 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
17642 sqlite3_stmt *pStmt;
17645 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
17647 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17650 while( sqlite3_step(pStmt)==SQLITE_ROW ){
17651 const char *zSchema = (const char *)sqlite3_column_text(pStmt,1);
17652 const char *zFile = (const char*)sqlite3_column_text(pStmt,2);
17653 azName = sqlite3_realloc(azName, (nName+1)*2*sizeof(char*));
17654 if( azName==0 ){ shell_out_of_memory(); /* Does not return */ }
17655 azName[nName*2] = strdup(zSchema);
17656 azName[nName*2+1] = strdup(zFile);
17660 sqlite3_finalize(pStmt);
17661 for(i=0; i<nName; i++){
17662 int eTxn = sqlite3_txn_state(p->db, azName[i*2]);
17663 int bRdonly = sqlite3_db_readonly(p->db, azName[i*2]);
17664 const char *z = azName[i*2+1];
17665 utf8_printf(p->out, "%s: %s %s%s\n",
17667 z && z[0] ? z : "\"\"",
17668 bRdonly ? "r/o" : "r/w",
17669 eTxn==SQLITE_TXN_NONE ? "" :
17670 eTxn==SQLITE_TXN_READ ? " read-txn" : " write-txn");
17672 free(azName[i*2+1]);
17674 sqlite3_free(azName);
17677 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
17678 static const struct DbConfigChoices {
17682 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
17683 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
17684 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
17685 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
17686 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
17687 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
17688 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
17689 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
17690 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
17691 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
17692 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
17693 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
17694 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
17695 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
17696 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
17697 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
17701 for(ii=0; ii<ArraySize(aDbConfig); ii++){
17702 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
17704 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
17706 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
17707 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
17708 if( nArg>1 ) break;
17710 if( nArg>1 && ii==ArraySize(aDbConfig) ){
17711 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
17712 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
17716 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
17717 rc = shell_dbinfo_command(p, nArg, azArg);
17720 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
17721 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
17723 rc = recoverDatabaseCmd(p, nArg, azArg);
17725 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
17727 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
17731 int savedShowHeader = p->showHeader;
17732 int savedShellFlags = p->shellFlgs;
17734 SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo
17735 |SHFLG_DumpDataOnly|SHFLG_DumpNoSys);
17736 for(i=1; i<nArg; i++){
17737 if( azArg[i][0]=='-' ){
17738 const char *z = azArg[i]+1;
17739 if( z[0]=='-' ) z++;
17740 if( strcmp(z,"preserve-rowids")==0 ){
17741 #ifdef SQLITE_OMIT_VIRTUALTABLE
17742 raw_printf(stderr, "The --preserve-rowids option is not compatible"
17743 " with SQLITE_OMIT_VIRTUALTABLE\n");
17745 sqlite3_free(zLike);
17746 goto meta_command_exit;
17748 ShellSetFlag(p, SHFLG_PreserveRowid);
17751 if( strcmp(z,"newlines")==0 ){
17752 ShellSetFlag(p, SHFLG_Newlines);
17754 if( strcmp(z,"data-only")==0 ){
17755 ShellSetFlag(p, SHFLG_DumpDataOnly);
17757 if( strcmp(z,"nosys")==0 ){
17758 ShellSetFlag(p, SHFLG_DumpNoSys);
17761 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
17763 sqlite3_free(zLike);
17764 goto meta_command_exit;
17767 zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
17770 zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
17776 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17777 /* When playing back a "dump", the content might appear in an order
17778 ** which causes immediate foreign key constraints to be violated.
17779 ** So disable foreign-key constraint enforcement to prevent problems. */
17780 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
17781 raw_printf(p->out, "BEGIN TRANSACTION;\n");
17783 p->writableSchema = 0;
17785 /* Set writable_schema=ON since doing so forces SQLite to initialize
17786 ** as much of the schema as it can even if the sqlite_schema table is
17788 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
17790 if( zLike==0 ) zLike = sqlite3_mprintf("true");
17791 zSql = sqlite3_mprintf(
17792 "SELECT name, type, sql FROM sqlite_schema "
17793 "WHERE (%s) AND type=='table'"
17794 " AND sql NOT NULL"
17795 " ORDER BY tbl_name='sqlite_sequence', rowid",
17798 run_schema_dump_query(p,zSql);
17799 sqlite3_free(zSql);
17800 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17801 zSql = sqlite3_mprintf(
17802 "SELECT sql FROM sqlite_schema "
17803 "WHERE (%s) AND sql NOT NULL"
17804 " AND type IN ('index','trigger','view')",
17807 run_table_dump_query(p, zSql);
17808 sqlite3_free(zSql);
17810 sqlite3_free(zLike);
17811 if( p->writableSchema ){
17812 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
17813 p->writableSchema = 0;
17815 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
17816 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
17817 if( (p->shellFlgs & SHFLG_DumpDataOnly)==0 ){
17818 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
17820 p->showHeader = savedShowHeader;
17821 p->shellFlgs = savedShellFlags;
17824 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
17826 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
17828 raw_printf(stderr, "Usage: .echo on|off\n");
17833 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
17835 p->autoEQPtest = 0;
17836 if( p->autoEQPtrace ){
17837 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
17838 p->autoEQPtrace = 0;
17840 if( strcmp(azArg[1],"full")==0 ){
17841 p->autoEQP = AUTOEQP_full;
17842 }else if( strcmp(azArg[1],"trigger")==0 ){
17843 p->autoEQP = AUTOEQP_trigger;
17844 #ifdef SQLITE_DEBUG
17845 }else if( strcmp(azArg[1],"test")==0 ){
17846 p->autoEQP = AUTOEQP_on;
17847 p->autoEQPtest = 1;
17848 }else if( strcmp(azArg[1],"trace")==0 ){
17849 p->autoEQP = AUTOEQP_full;
17850 p->autoEQPtrace = 1;
17852 sqlite3_exec(p->db, "SELECT name FROM sqlite_schema LIMIT 1", 0, 0, 0);
17853 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
17856 p->autoEQP = (u8)booleanValue(azArg[1]);
17859 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
17864 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
17865 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
17869 /* The ".explain" command is automatic now. It is largely pointless. It
17870 ** retained purely for backwards compatibility */
17871 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
17874 if( strcmp(azArg[1],"auto")==0 ){
17877 val = booleanValue(azArg[1]);
17880 if( val==1 && p->mode!=MODE_Explain ){
17881 p->normalMode = p->mode;
17882 p->mode = MODE_Explain;
17883 p->autoExplain = 0;
17884 }else if( val==0 ){
17885 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17886 p->autoExplain = 0;
17887 }else if( val==99 ){
17888 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
17889 p->autoExplain = 1;
17893 #ifndef SQLITE_OMIT_VIRTUALTABLE
17894 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
17896 expertDotCommand(p, azArg, nArg);
17900 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
17901 static const struct {
17902 const char *zCtrlName; /* Name of a test-control option */
17903 int ctrlCode; /* Integer code for that option */
17904 const char *zUsage; /* Usage notes */
17906 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
17907 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
17908 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
17909 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
17910 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
17911 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
17912 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
17913 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
17914 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
17915 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
17919 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
17920 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
17922 const char *zCmd = 0;
17923 const char *zSchema = 0;
17926 zCmd = nArg>=2 ? azArg[1] : "help";
17929 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
17932 zSchema = azArg[2];
17933 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
17938 /* The argument can optionally begin with "-" or "--" */
17939 if( zCmd[0]=='-' && zCmd[1] ){
17941 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
17944 /* --help lists all file-controls */
17945 if( strcmp(zCmd,"help")==0 ){
17946 utf8_printf(p->out, "Available file-controls:\n");
17947 for(i=0; i<ArraySize(aCtrl); i++){
17948 utf8_printf(p->out, " .filectrl %s %s\n",
17949 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
17952 goto meta_command_exit;
17955 /* convert filectrl text option to value. allow any unique prefix
17956 ** of the option name, or a numerical value. */
17957 n2 = strlen30(zCmd);
17958 for(i=0; i<ArraySize(aCtrl); i++){
17959 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
17961 filectrl = aCtrl[i].ctrlCode;
17964 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
17965 "Use \".filectrl --help\" for help\n", zCmd);
17967 goto meta_command_exit;
17972 utf8_printf(stderr,"Error: unknown file-control: %s\n"
17973 "Use \".filectrl --help\" for help\n", zCmd);
17976 case SQLITE_FCNTL_SIZE_LIMIT: {
17977 if( nArg!=2 && nArg!=3 ) break;
17978 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
17979 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
17983 case SQLITE_FCNTL_LOCK_TIMEOUT:
17984 case SQLITE_FCNTL_CHUNK_SIZE: {
17986 if( nArg!=3 ) break;
17987 x = (int)integerValue(azArg[2]);
17988 sqlite3_file_control(p->db, zSchema, filectrl, &x);
17992 case SQLITE_FCNTL_PERSIST_WAL:
17993 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
17995 if( nArg!=2 && nArg!=3 ) break;
17996 x = nArg==3 ? booleanValue(azArg[2]) : -1;
17997 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18002 case SQLITE_FCNTL_HAS_MOVED: {
18004 if( nArg!=2 ) break;
18005 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18010 case SQLITE_FCNTL_TEMPFILENAME: {
18012 if( nArg!=2 ) break;
18013 sqlite3_file_control(p->db, zSchema, filectrl, &z);
18015 utf8_printf(p->out, "%s\n", z);
18021 case SQLITE_FCNTL_RESERVE_BYTES: {
18024 x = atoi(azArg[2]);
18025 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18028 sqlite3_file_control(p->db, zSchema, filectrl, &x);
18029 utf8_printf(p->out,"%d\n", x);
18035 if( isOk==0 && iCtrl>=0 ){
18036 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18038 }else if( isOk==1 ){
18040 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
18041 raw_printf(p->out, "%s\n", zBuf);
18045 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
18049 memcpy(&data, p, sizeof(data));
18050 data.showHeader = 0;
18051 data.cMode = data.mode = MODE_Semi;
18052 if( nArg==2 && optionMatch(azArg[1], "indent") ){
18053 data.cMode = data.mode = MODE_Pretty;
18057 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
18059 goto meta_command_exit;
18062 rc = sqlite3_exec(p->db,
18064 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
18065 " FROM sqlite_schema UNION ALL"
18066 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_schema) "
18067 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
18069 callback, &data, &zErrMsg
18071 if( rc==SQLITE_OK ){
18072 sqlite3_stmt *pStmt;
18073 rc = sqlite3_prepare_v2(p->db,
18074 "SELECT rowid FROM sqlite_schema"
18075 " WHERE name GLOB 'sqlite_stat[134]'",
18077 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
18078 sqlite3_finalize(pStmt);
18081 raw_printf(p->out, "/* No STAT tables available */\n");
18083 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18084 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_schema'",
18085 callback, &data, &zErrMsg);
18086 data.cMode = data.mode = MODE_Insert;
18087 data.zDestTable = "sqlite_stat1";
18088 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
18089 data.zDestTable = "sqlite_stat4";
18090 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
18091 raw_printf(p->out, "ANALYZE sqlite_schema;\n");
18095 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
18097 p->showHeader = booleanValue(azArg[1]);
18098 p->shellFlgs |= SHFLG_HeaderSet;
18100 raw_printf(stderr, "Usage: .headers on|off\n");
18105 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
18107 n = showHelp(p->out, azArg[1]);
18109 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
18112 showHelp(p->out, 0);
18116 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
18117 char *zTable = 0; /* Insert data into this table */
18118 char *zFile = 0; /* Name of file to extra content from */
18119 sqlite3_stmt *pStmt = NULL; /* A statement */
18120 int nCol; /* Number of columns in the table */
18121 int nByte; /* Number of bytes in an SQL string */
18122 int i, j; /* Loop counters */
18123 int needCommit; /* True to COMMIT or ROLLBACK at end */
18124 int nSep; /* Number of bytes in p->colSeparator[] */
18125 char *zSql; /* An SQL statement */
18126 ImportCtx sCtx; /* Reader context */
18127 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
18128 int eVerbose = 0; /* Larger for more console output */
18129 int nSkip = 0; /* Initial lines to skip */
18130 int useOutputMode = 1; /* Use output mode to determine separators */
18132 memset(&sCtx, 0, sizeof(sCtx));
18133 if( p->mode==MODE_Ascii ){
18134 xRead = ascii_read_one_field;
18136 xRead = csv_read_one_field;
18138 for(i=1; i<nArg; i++){
18139 char *z = azArg[i];
18140 if( z[0]=='-' && z[1]=='-' ) z++;
18144 }else if( zTable==0 ){
18147 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
18148 showHelp(p->out, "import");
18150 goto meta_command_exit;
18152 }else if( strcmp(z,"-v")==0 ){
18154 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
18155 nSkip = integerValue(azArg[++i]);
18156 }else if( strcmp(z,"-ascii")==0 ){
18157 sCtx.cColSep = SEP_Unit[0];
18158 sCtx.cRowSep = SEP_Record[0];
18159 xRead = ascii_read_one_field;
18161 }else if( strcmp(z,"-csv")==0 ){
18162 sCtx.cColSep = ',';
18163 sCtx.cRowSep = '\n';
18164 xRead = csv_read_one_field;
18167 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
18168 showHelp(p->out, "import");
18170 goto meta_command_exit;
18174 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
18175 zFile==0 ? "FILE" : "TABLE");
18176 showHelp(p->out, "import");
18178 goto meta_command_exit;
18182 if( useOutputMode ){
18183 /* If neither the --csv or --ascii options are specified, then set
18184 ** the column and row separator characters from the output mode. */
18185 nSep = strlen30(p->colSeparator);
18188 "Error: non-null column separator required for import\n");
18190 goto meta_command_exit;
18194 "Error: multi-character column separators not allowed"
18197 goto meta_command_exit;
18199 nSep = strlen30(p->rowSeparator);
18202 "Error: non-null row separator required for import\n");
18204 goto meta_command_exit;
18206 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
18207 /* When importing CSV (only), if the row separator is set to the
18208 ** default output row separator, change it to the default input
18209 ** row separator. This avoids having to maintain different input
18210 ** and output row separators. */
18211 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18212 nSep = strlen30(p->rowSeparator);
18215 raw_printf(stderr, "Error: multi-character row separators not allowed"
18218 goto meta_command_exit;
18220 sCtx.cColSep = p->colSeparator[0];
18221 sCtx.cRowSep = p->rowSeparator[0];
18223 sCtx.zFile = zFile;
18225 if( sCtx.zFile[0]=='|' ){
18226 #ifdef SQLITE_OMIT_POPEN
18227 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18229 goto meta_command_exit;
18231 sCtx.in = popen(sCtx.zFile+1, "r");
18232 sCtx.zFile = "<pipe>";
18233 sCtx.xCloser = pclose;
18236 sCtx.in = fopen(sCtx.zFile, "rb");
18237 sCtx.xCloser = fclose;
18240 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
18242 goto meta_command_exit;
18244 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
18247 zSep[0] = sCtx.cColSep;
18248 utf8_printf(p->out, "Column separator ");
18249 output_c_string(p->out, zSep);
18250 utf8_printf(p->out, ", row separator ");
18251 zSep[0] = sCtx.cRowSep;
18252 output_c_string(p->out, zSep);
18253 utf8_printf(p->out, "\n");
18255 while( (nSkip--)>0 ){
18256 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
18258 zSql = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
18260 import_cleanup(&sCtx);
18261 shell_out_of_memory();
18263 nByte = strlen30(zSql);
18264 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18265 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
18266 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
18267 char *zCreate = sqlite3_mprintf("CREATE TABLE \"%w\"", zTable);
18269 while( xRead(&sCtx) ){
18270 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
18272 if( sCtx.cTerm!=sCtx.cColSep ) break;
18275 sqlite3_free(zCreate);
18276 import_cleanup(&sCtx);
18277 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
18279 goto meta_command_exit;
18281 zCreate = sqlite3_mprintf("%z\n)", zCreate);
18283 utf8_printf(p->out, "%s\n", zCreate);
18285 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
18286 sqlite3_free(zCreate);
18288 utf8_printf(stderr, "CREATE TABLE \"%s\"(...) failed: %s\n", zTable,
18289 sqlite3_errmsg(p->db));
18290 import_cleanup(&sCtx);
18292 goto meta_command_exit;
18294 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18296 sqlite3_free(zSql);
18298 if (pStmt) sqlite3_finalize(pStmt);
18299 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
18300 import_cleanup(&sCtx);
18302 goto meta_command_exit;
18304 nCol = sqlite3_column_count(pStmt);
18305 sqlite3_finalize(pStmt);
18307 if( nCol==0 ) return 0; /* no columns, no error */
18308 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
18310 import_cleanup(&sCtx);
18311 shell_out_of_memory();
18313 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
18314 j = strlen30(zSql);
18315 for(i=1; i<nCol; i++){
18322 utf8_printf(p->out, "Insert using: %s\n", zSql);
18324 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18325 sqlite3_free(zSql);
18327 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
18328 if (pStmt) sqlite3_finalize(pStmt);
18329 import_cleanup(&sCtx);
18331 goto meta_command_exit;
18333 needCommit = sqlite3_get_autocommit(p->db);
18334 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
18336 int startLine = sCtx.nLine;
18337 for(i=0; i<nCol; i++){
18338 char *z = xRead(&sCtx);
18340 ** Did we reach end-of-file before finding any columns?
18341 ** If so, stop instead of NULL filling the remaining columns.
18343 if( z==0 && i==0 ) break;
18345 ** Did we reach end-of-file OR end-of-line before finding any
18346 ** columns in ASCII mode? If so, stop instead of NULL filling
18347 ** the remaining columns.
18349 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
18350 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
18351 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
18352 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18353 "filling the rest with NULL\n",
18354 sCtx.zFile, startLine, nCol, i+1);
18356 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
18359 if( sCtx.cTerm==sCtx.cColSep ){
18363 }while( sCtx.cTerm==sCtx.cColSep );
18364 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
18365 "extras ignored\n",
18366 sCtx.zFile, startLine, nCol, i);
18369 sqlite3_step(pStmt);
18370 rc = sqlite3_reset(pStmt);
18371 if( rc!=SQLITE_OK ){
18372 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
18373 startLine, sqlite3_errmsg(p->db));
18379 }while( sCtx.cTerm!=EOF );
18381 import_cleanup(&sCtx);
18382 sqlite3_finalize(pStmt);
18383 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
18385 utf8_printf(p->out,
18386 "Added %d rows with %d errors using %d lines of input\n",
18387 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
18391 #ifndef SQLITE_UNTESTABLE
18392 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
18394 char *zCollist = 0;
18395 sqlite3_stmt *pStmt;
18397 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
18398 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
18400 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
18401 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
18402 " .imposter off\n");
18403 /* Also allowed, but not documented:
18405 ** .imposter TABLE IMPOSTER
18407 ** where TABLE is a WITHOUT ROWID table. In that case, the
18408 ** imposter is another WITHOUT ROWID table with the columns in
18409 ** storage order. */
18411 goto meta_command_exit;
18415 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
18416 goto meta_command_exit;
18418 zSql = sqlite3_mprintf(
18419 "SELECT rootpage, 0 FROM sqlite_schema"
18420 " WHERE name='%q' AND type='index'"
18422 "SELECT rootpage, 1 FROM sqlite_schema"
18423 " WHERE name='%q' AND type='table'"
18424 " AND sql LIKE '%%without%%rowid%%'",
18427 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18428 sqlite3_free(zSql);
18429 if( sqlite3_step(pStmt)==SQLITE_ROW ){
18430 tnum = sqlite3_column_int(pStmt, 0);
18431 isWO = sqlite3_column_int(pStmt, 1);
18433 sqlite3_finalize(pStmt);
18434 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
18435 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18436 sqlite3_free(zSql);
18438 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18440 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
18443 if( sqlite3_column_int(pStmt,1)==-1 ){
18446 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
18450 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
18451 lenPK = (int)strlen(zCollist);
18454 zCollist = sqlite3_mprintf("\"%w\"", zCol);
18456 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
18459 sqlite3_finalize(pStmt);
18460 if( i==0 || tnum==0 ){
18461 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
18463 sqlite3_free(zCollist);
18464 goto meta_command_exit;
18466 if( lenPK==0 ) lenPK = 100000;
18467 zSql = sqlite3_mprintf(
18468 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
18469 azArg[2], zCollist, lenPK, zCollist);
18470 sqlite3_free(zCollist);
18471 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
18472 if( rc==SQLITE_OK ){
18473 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
18474 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
18476 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
18478 utf8_printf(stdout, "%s;\n", zSql);
18480 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
18481 azArg[1], isWO ? "table" : "index"
18485 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
18488 sqlite3_free(zSql);
18490 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
18492 #ifdef SQLITE_ENABLE_IOTRACE
18493 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
18494 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
18495 if( iotrace && iotrace!=stdout ) fclose(iotrace);
18498 sqlite3IoTrace = 0;
18499 }else if( strcmp(azArg[1], "-")==0 ){
18500 sqlite3IoTrace = iotracePrintf;
18503 iotrace = fopen(azArg[1], "w");
18505 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
18506 sqlite3IoTrace = 0;
18509 sqlite3IoTrace = iotracePrintf;
18515 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
18516 static const struct {
18517 const char *zLimitName; /* Name of a limit */
18518 int limitCode; /* Integer code for that limit */
18520 { "length", SQLITE_LIMIT_LENGTH },
18521 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
18522 { "column", SQLITE_LIMIT_COLUMN },
18523 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
18524 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
18525 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
18526 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
18527 { "attached", SQLITE_LIMIT_ATTACHED },
18528 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
18529 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
18530 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
18531 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
18536 for(i=0; i<ArraySize(aLimit); i++){
18537 printf("%20s %d\n", aLimit[i].zLimitName,
18538 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
18540 }else if( nArg>3 ){
18541 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
18543 goto meta_command_exit;
18546 n2 = strlen30(azArg[1]);
18547 for(i=0; i<ArraySize(aLimit); i++){
18548 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
18552 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
18554 goto meta_command_exit;
18559 utf8_printf(stderr, "unknown limit: \"%s\"\n"
18560 "enter \".limits\" with no arguments for a list.\n",
18563 goto meta_command_exit;
18566 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
18567 (int)integerValue(azArg[2]));
18569 printf("%20s %d\n", aLimit[iLimit].zLimitName,
18570 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
18574 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
18576 lintDotCommand(p, azArg, nArg);
18579 #ifndef SQLITE_OMIT_LOAD_EXTENSION
18580 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
18581 const char *zFile, *zProc;
18584 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
18586 goto meta_command_exit;
18589 zProc = nArg>=3 ? azArg[2] : 0;
18591 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
18592 if( rc!=SQLITE_OK ){
18593 utf8_printf(stderr, "Error: %s\n", zErrMsg);
18594 sqlite3_free(zErrMsg);
18600 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
18602 raw_printf(stderr, "Usage: .log FILENAME\n");
18605 const char *zFile = azArg[1];
18606 output_file_close(p->pLog);
18607 p->pLog = output_file_open(zFile, 0);
18611 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
18612 const char *zMode = nArg>=2 ? azArg[1] : "";
18613 int n2 = strlen30(zMode);
18615 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
18616 p->mode = MODE_Line;
18617 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18618 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
18619 p->mode = MODE_Column;
18620 if( (p->shellFlgs & SHFLG_HeaderSet)==0 ){
18623 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18624 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
18625 p->mode = MODE_List;
18626 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
18627 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18628 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
18629 p->mode = MODE_Html;
18630 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
18631 p->mode = MODE_Tcl;
18632 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
18633 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18634 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
18635 p->mode = MODE_Csv;
18636 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18637 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18638 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
18639 p->mode = MODE_List;
18640 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
18641 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
18642 p->mode = MODE_Insert;
18643 set_table_name(p, nArg>=3 ? azArg[2] : "table");
18644 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
18645 p->mode = MODE_Quote;
18646 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18647 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
18648 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
18649 p->mode = MODE_Ascii;
18650 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
18651 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
18652 }else if( c2=='m' && strncmp(azArg[1],"markdown",n2)==0 ){
18653 p->mode = MODE_Markdown;
18654 }else if( c2=='t' && strncmp(azArg[1],"table",n2)==0 ){
18655 p->mode = MODE_Table;
18656 }else if( c2=='b' && strncmp(azArg[1],"box",n2)==0 ){
18657 p->mode = MODE_Box;
18658 }else if( c2=='j' && strncmp(azArg[1],"json",n2)==0 ){
18659 p->mode = MODE_Json;
18660 }else if( nArg==1 ){
18661 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
18663 raw_printf(stderr, "Error: mode should be one of: "
18664 "ascii box column csv html insert json line list markdown "
18665 "quote table tabs tcl\n");
18668 p->cMode = p->mode;
18671 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
18673 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
18674 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
18676 raw_printf(stderr, "Usage: .nullvalue STRING\n");
18681 #ifdef SQLITE_DEBUG
18682 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
18684 for(i=1; i<nArg; i++){
18685 const char *z = azArg[i];
18686 if( z[0]=='-' && z[1]=='-' ) z++;
18687 if( strcmp(z,"-repeat")==0 ){
18689 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
18692 oomRepeat = (int)integerValue(azArg[++i]);
18694 }else if( IsDigit(z[0]) ){
18695 oomCounter = (int)integerValue(azArg[i]);
18697 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
18698 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
18703 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
18704 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
18707 #endif /* SQLITE_DEBUG */
18709 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
18710 char *zNewFilename; /* Name of the database file to open */
18711 int iName = 1; /* Index in azArg[] of the filename */
18712 int newFlag = 0; /* True to delete file before opening */
18713 /* Close the existing database */
18714 session_close_all(p);
18717 p->zDbFilename = 0;
18718 sqlite3_free(p->zFreeOnClose);
18719 p->zFreeOnClose = 0;
18720 p->openMode = SHELL_OPEN_UNSPEC;
18723 /* Check for command-line arguments */
18724 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
18725 const char *z = azArg[iName];
18726 if( optionMatch(z,"new") ){
18728 #ifdef SQLITE_HAVE_ZLIB
18729 }else if( optionMatch(z, "zip") ){
18730 p->openMode = SHELL_OPEN_ZIPFILE;
18732 }else if( optionMatch(z, "append") ){
18733 p->openMode = SHELL_OPEN_APPENDVFS;
18734 }else if( optionMatch(z, "readonly") ){
18735 p->openMode = SHELL_OPEN_READONLY;
18736 }else if( optionMatch(z, "nofollow") ){
18737 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
18738 #ifdef SQLITE_ENABLE_DESERIALIZE
18739 }else if( optionMatch(z, "deserialize") ){
18740 p->openMode = SHELL_OPEN_DESERIALIZE;
18741 }else if( optionMatch(z, "hexdb") ){
18742 p->openMode = SHELL_OPEN_HEXDB;
18743 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
18744 p->szMax = integerValue(azArg[++iName]);
18745 #endif /* SQLITE_ENABLE_DESERIALIZE */
18746 }else if( z[0]=='-' ){
18747 utf8_printf(stderr, "unknown option: %s\n", z);
18749 goto meta_command_exit;
18752 /* If a filename is specified, try to open it first */
18753 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
18754 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
18755 if( newFlag ) shellDeleteFile(zNewFilename);
18756 p->zDbFilename = zNewFilename;
18757 open_db(p, OPEN_DB_KEEPALIVE);
18759 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
18760 sqlite3_free(zNewFilename);
18762 p->zFreeOnClose = zNewFilename;
18766 /* As a fall-back open a TEMP database */
18767 p->zDbFilename = 0;
18773 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
18774 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
18776 const char *zFile = 0;
18781 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
18786 }else if( strncmp(azArg[0],"once",n)==0 ){
18789 for(i=1; i<nArg; i++){
18790 char *z = azArg[i];
18792 if( z[1]=='-' ) z++;
18793 if( strcmp(z,"-bom")==0 ){
18795 }else if( c!='e' && strcmp(z,"-x")==0 ){
18796 eMode = 'x'; /* spreadsheet */
18797 }else if( c!='e' && strcmp(z,"-e")==0 ){
18798 eMode = 'e'; /* text editor */
18800 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
18802 showHelp(p->out, azArg[0]);
18804 goto meta_command_exit;
18806 }else if( zFile==0 ){
18809 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
18811 showHelp(p->out, azArg[0]);
18813 goto meta_command_exit;
18816 if( zFile==0 ) zFile = "stdout";
18823 #ifndef SQLITE_NOHAVE_SYSTEM
18824 if( eMode=='e' || eMode=='x' ){
18828 /* spreadsheet mode. Output as CSV. */
18829 newTempFile(p, "csv");
18830 ShellClearFlag(p, SHFLG_Echo);
18831 p->mode = MODE_Csv;
18832 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
18833 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
18835 /* text editor mode */
18836 newTempFile(p, "txt");
18839 zFile = p->zTempFile;
18841 #endif /* SQLITE_NOHAVE_SYSTEM */
18842 if( zFile[0]=='|' ){
18843 #ifdef SQLITE_OMIT_POPEN
18844 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
18848 p->out = popen(zFile + 1, "w");
18850 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
18854 if( bBOM ) fprintf(p->out,"\357\273\277");
18855 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18859 p->out = output_file_open(zFile, bTxtMode);
18861 if( strcmp(zFile,"off")!=0 ){
18862 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
18867 if( bBOM ) fprintf(p->out,"\357\273\277");
18868 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
18873 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
18875 if( nArg<=1 ) goto parameter_syntax_error;
18877 /* .parameter clear
18878 ** Clear all bind parameters by dropping the TEMP table that holds them.
18880 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
18881 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
18886 ** List all bind parameters.
18888 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
18889 sqlite3_stmt *pStmt = 0;
18892 rx = sqlite3_prepare_v2(p->db,
18893 "SELECT max(length(key)) "
18894 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18895 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
18896 len = sqlite3_column_int(pStmt, 0);
18897 if( len>40 ) len = 40;
18899 sqlite3_finalize(pStmt);
18902 rx = sqlite3_prepare_v2(p->db,
18903 "SELECT key, quote(value) "
18904 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
18905 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18906 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
18907 sqlite3_column_text(pStmt,1));
18909 sqlite3_finalize(pStmt);
18914 ** Make sure the TEMP table used to hold bind parameters exists.
18915 ** Create it if necessary.
18917 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
18918 bind_table_init(p);
18921 /* .parameter set NAME VALUE
18922 ** Set or reset a bind parameter. NAME should be the full parameter
18923 ** name exactly as it appears in the query. (ex: $abc, @def). The
18924 ** VALUE can be in either SQL literal notation, or if not it will be
18925 ** understood to be a text string.
18927 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
18930 sqlite3_stmt *pStmt;
18931 const char *zKey = azArg[2];
18932 const char *zValue = azArg[3];
18933 bind_table_init(p);
18934 zSql = sqlite3_mprintf(
18935 "REPLACE INTO temp.sqlite_parameters(key,value)"
18936 "VALUES(%Q,%s);", zKey, zValue);
18937 if( zSql==0 ) shell_out_of_memory();
18939 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18940 sqlite3_free(zSql);
18941 if( rx!=SQLITE_OK ){
18942 sqlite3_finalize(pStmt);
18944 zSql = sqlite3_mprintf(
18945 "REPLACE INTO temp.sqlite_parameters(key,value)"
18946 "VALUES(%Q,%Q);", zKey, zValue);
18947 if( zSql==0 ) shell_out_of_memory();
18948 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
18949 sqlite3_free(zSql);
18950 if( rx!=SQLITE_OK ){
18951 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
18952 sqlite3_finalize(pStmt);
18957 sqlite3_step(pStmt);
18958 sqlite3_finalize(pStmt);
18961 /* .parameter unset NAME
18962 ** Remove the NAME binding from the parameter binding table, if it
18965 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
18966 char *zSql = sqlite3_mprintf(
18967 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
18968 if( zSql==0 ) shell_out_of_memory();
18969 sqlite3_exec(p->db, zSql, 0, 0, 0);
18970 sqlite3_free(zSql);
18972 /* If no command name matches, show a syntax error */
18973 parameter_syntax_error:
18974 showHelp(p->out, "parameter");
18977 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
18979 for(i=1; i<nArg; i++){
18980 if( i>1 ) raw_printf(p->out, " ");
18981 utf8_printf(p->out, "%s", azArg[i]);
18983 raw_printf(p->out, "\n");
18986 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
18987 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
18990 p->flgProgress = 0;
18993 for(i=1; i<nArg; i++){
18994 const char *z = azArg[i];
18997 if( z[0]=='-' ) z++;
18998 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
18999 p->flgProgress |= SHELL_PROGRESS_QUIET;
19002 if( strcmp(z,"reset")==0 ){
19003 p->flgProgress |= SHELL_PROGRESS_RESET;
19006 if( strcmp(z,"once")==0 ){
19007 p->flgProgress |= SHELL_PROGRESS_ONCE;
19010 if( strcmp(z,"limit")==0 ){
19012 utf8_printf(stderr, "Error: missing argument on --limit\n");
19014 goto meta_command_exit;
19016 p->mxProgress = (int)integerValue(azArg[++i]);
19020 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
19022 goto meta_command_exit;
19024 nn = (int)integerValue(z);
19028 sqlite3_progress_handler(p->db, nn, progress_handler, p);
19030 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
19032 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
19034 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
19037 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
19041 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
19045 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
19046 FILE *inSaved = p->in;
19047 int savedLineno = p->lineno;
19049 raw_printf(stderr, "Usage: .read FILE\n");
19051 goto meta_command_exit;
19053 if( azArg[1][0]=='|' ){
19054 p->in = popen(azArg[1]+1, "r");
19056 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
19059 rc = process_input(p);
19062 }else if( notNormalFile(azArg[1]) || (p->in = fopen(azArg[1], "rb"))==0 ){
19063 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
19066 rc = process_input(p);
19070 p->lineno = savedLineno;
19073 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
19074 const char *zSrcFile;
19077 sqlite3_backup *pBackup;
19081 zSrcFile = azArg[1];
19083 }else if( nArg==3 ){
19084 zSrcFile = azArg[2];
19087 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
19089 goto meta_command_exit;
19091 rc = sqlite3_open(zSrcFile, &pSrc);
19092 if( rc!=SQLITE_OK ){
19093 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
19098 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
19100 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19104 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
19105 || rc==SQLITE_BUSY ){
19106 if( rc==SQLITE_BUSY ){
19107 if( nTimeout++ >= 3 ) break;
19108 sqlite3_sleep(100);
19111 sqlite3_backup_finish(pBackup);
19112 if( rc==SQLITE_DONE ){
19114 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
19115 raw_printf(stderr, "Error: source database is busy\n");
19118 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19124 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
19126 p->scanstatsOn = (u8)booleanValue(azArg[1]);
19127 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
19128 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
19131 raw_printf(stderr, "Usage: .scanstats on|off\n");
19136 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
19140 const char *zDiv = "(";
19141 const char *zName = 0;
19144 int bNoSystemTabs = 0;
19148 memcpy(&data, p, sizeof(data));
19149 data.showHeader = 0;
19150 data.cMode = data.mode = MODE_Semi;
19151 initText(&sSelect);
19152 for(ii=1; ii<nArg; ii++){
19153 if( optionMatch(azArg[ii],"indent") ){
19154 data.cMode = data.mode = MODE_Pretty;
19155 }else if( optionMatch(azArg[ii],"debug") ){
19157 }else if( optionMatch(azArg[ii],"nosys") ){
19159 }else if( azArg[ii][0]=='-' ){
19160 utf8_printf(stderr, "Unknown option: \"%s\"\n", azArg[ii]);
19162 goto meta_command_exit;
19163 }else if( zName==0 ){
19166 raw_printf(stderr, "Usage: .schema ?--indent? ?--nosys? ?LIKE-PATTERN?\n");
19168 goto meta_command_exit;
19172 int isSchema = sqlite3_strlike(zName, "sqlite_master", '\\')==0
19173 || sqlite3_strlike(zName, "sqlite_schema", '\\')==0
19174 || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0
19175 || sqlite3_strlike(zName,"sqlite_temp_schema", '\\')==0;
19177 char *new_argv[2], *new_colv[2];
19178 new_argv[0] = sqlite3_mprintf(
19179 "CREATE TABLE %s (\n"
19182 " tbl_name text,\n"
19183 " rootpage integer,\n"
19187 new_colv[0] = "sql";
19189 callback(&data, 1, new_argv, new_colv);
19190 sqlite3_free(new_argv[0]);
19194 sqlite3_stmt *pStmt = 0;
19195 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
19198 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
19199 sqlite3_finalize(pStmt);
19201 goto meta_command_exit;
19203 appendText(&sSelect, "SELECT sql FROM", 0);
19205 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19206 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
19208 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
19209 appendText(&sSelect, zDiv, 0);
19210 zDiv = " UNION ALL ";
19211 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
19212 if( sqlite3_stricmp(zDb, "main")!=0 ){
19213 appendText(&sSelect, zDb, '\'');
19215 appendText(&sSelect, "NULL", 0);
19217 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
19218 appendText(&sSelect, zScNum, 0);
19219 appendText(&sSelect, " AS snum, ", 0);
19220 appendText(&sSelect, zDb, '\'');
19221 appendText(&sSelect, " AS sname FROM ", 0);
19222 appendText(&sSelect, zDb, quoteChar(zDb));
19223 appendText(&sSelect, ".sqlite_schema", 0);
19225 sqlite3_finalize(pStmt);
19226 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
19228 appendText(&sSelect,
19229 " UNION ALL SELECT shell_module_schema(name),"
19230 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
19234 appendText(&sSelect, ") WHERE ", 0);
19236 char *zQarg = sqlite3_mprintf("%Q", zName);
19237 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
19238 strchr(zName, '[') != 0;
19239 if( strchr(zName, '.') ){
19240 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
19242 appendText(&sSelect, "lower(tbl_name)", 0);
19244 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
19245 appendText(&sSelect, zQarg, 0);
19247 appendText(&sSelect, " ESCAPE '\\' ", 0);
19249 appendText(&sSelect, " AND ", 0);
19250 sqlite3_free(zQarg);
19252 if( bNoSystemTabs ){
19253 appendText(&sSelect, "name NOT LIKE 'sqlite_%%' AND ", 0);
19255 appendText(&sSelect, "sql IS NOT NULL"
19256 " ORDER BY snum, rowid", 0);
19258 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
19260 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
19262 freeText(&sSelect);
19265 utf8_printf(stderr,"Error: %s\n", zErrMsg);
19266 sqlite3_free(zErrMsg);
19268 }else if( rc != SQLITE_OK ){
19269 raw_printf(stderr,"Error: querying schema information\n");
19276 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
19277 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
19278 sqlite3_unsupported_selecttrace = nArg>=2 ? (int)integerValue(azArg[1]) : 0xffff;
19282 #if defined(SQLITE_ENABLE_SESSION)
19283 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
19284 OpenSession *pSession = &p->aSession[0];
19285 char **azCmd = &azArg[1];
19287 int nCmd = nArg - 1;
19289 if( nArg<=1 ) goto session_syntax_error;
19292 for(iSes=0; iSes<p->nSession; iSes++){
19293 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
19295 if( iSes<p->nSession ){
19296 pSession = &p->aSession[iSes];
19300 pSession = &p->aSession[0];
19305 /* .session attach TABLE
19306 ** Invoke the sqlite3session_attach() interface to attach a particular
19307 ** table so that it is never filtered.
19309 if( strcmp(azCmd[0],"attach")==0 ){
19310 if( nCmd!=2 ) goto session_syntax_error;
19311 if( pSession->p==0 ){
19313 raw_printf(stderr, "ERROR: No sessions are open\n");
19315 rc = sqlite3session_attach(pSession->p, azCmd[1]);
19317 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
19323 /* .session changeset FILE
19324 ** .session patchset FILE
19325 ** Write a changeset or patchset into a file. The file is overwritten.
19327 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
19329 if( nCmd!=2 ) goto session_syntax_error;
19330 if( pSession->p==0 ) goto session_not_open;
19331 out = fopen(azCmd[1], "wb");
19333 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
19338 if( azCmd[0][0]=='c' ){
19339 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
19341 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
19344 printf("Error: error code %d\n", rc);
19348 && fwrite(pChng, szChng, 1, out)!=1 ){
19349 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
19352 sqlite3_free(pChng);
19358 ** Close the identified session
19360 if( strcmp(azCmd[0], "close")==0 ){
19361 if( nCmd!=1 ) goto session_syntax_error;
19363 session_close(pSession);
19364 p->aSession[iSes] = p->aSession[--p->nSession];
19368 /* .session enable ?BOOLEAN?
19369 ** Query or set the enable flag
19371 if( strcmp(azCmd[0], "enable")==0 ){
19373 if( nCmd>2 ) goto session_syntax_error;
19374 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19376 ii = sqlite3session_enable(pSession->p, ii);
19377 utf8_printf(p->out, "session %s enable flag = %d\n",
19378 pSession->zName, ii);
19382 /* .session filter GLOB ....
19383 ** Set a list of GLOB patterns of table names to be excluded.
19385 if( strcmp(azCmd[0], "filter")==0 ){
19387 if( nCmd<2 ) goto session_syntax_error;
19389 for(ii=0; ii<pSession->nFilter; ii++){
19390 sqlite3_free(pSession->azFilter[ii]);
19392 sqlite3_free(pSession->azFilter);
19393 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
19394 pSession->azFilter = sqlite3_malloc( nByte );
19395 if( pSession->azFilter==0 ){
19396 raw_printf(stderr, "Error: out or memory\n");
19399 for(ii=1; ii<nCmd; ii++){
19400 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
19402 pSession->nFilter = ii-1;
19406 /* .session indirect ?BOOLEAN?
19407 ** Query or set the indirect flag
19409 if( strcmp(azCmd[0], "indirect")==0 ){
19411 if( nCmd>2 ) goto session_syntax_error;
19412 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
19414 ii = sqlite3session_indirect(pSession->p, ii);
19415 utf8_printf(p->out, "session %s indirect flag = %d\n",
19416 pSession->zName, ii);
19420 /* .session isempty
19421 ** Determine if the session is empty
19423 if( strcmp(azCmd[0], "isempty")==0 ){
19425 if( nCmd!=1 ) goto session_syntax_error;
19427 ii = sqlite3session_isempty(pSession->p);
19428 utf8_printf(p->out, "session %s isempty flag = %d\n",
19429 pSession->zName, ii);
19434 ** List all currently open sessions
19436 if( strcmp(azCmd[0],"list")==0 ){
19437 for(i=0; i<p->nSession; i++){
19438 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
19442 /* .session open DB NAME
19443 ** Open a new session called NAME on the attached database DB.
19444 ** DB is normally "main".
19446 if( strcmp(azCmd[0],"open")==0 ){
19448 if( nCmd!=3 ) goto session_syntax_error;
19450 if( zName[0]==0 ) goto session_syntax_error;
19451 for(i=0; i<p->nSession; i++){
19452 if( strcmp(p->aSession[i].zName,zName)==0 ){
19453 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
19454 goto meta_command_exit;
19457 if( p->nSession>=ArraySize(p->aSession) ){
19458 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
19459 goto meta_command_exit;
19461 pSession = &p->aSession[p->nSession];
19462 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
19464 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
19466 goto meta_command_exit;
19468 pSession->nFilter = 0;
19469 sqlite3session_table_filter(pSession->p, session_filter, pSession);
19471 pSession->zName = sqlite3_mprintf("%s", zName);
19473 /* If no command name matches, show a syntax error */
19474 session_syntax_error:
19475 showHelp(p->out, "session");
19479 #ifdef SQLITE_DEBUG
19480 /* Undocumented commands for internal testing. Subject to change
19481 ** without notice. */
19482 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
19483 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
19485 for(i=1; i<nArg; i++){
19486 v = booleanValue(azArg[i]);
19487 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
19490 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
19491 int i; sqlite3_int64 v;
19492 for(i=1; i<nArg; i++){
19494 v = integerValue(azArg[i]);
19495 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
19496 utf8_printf(p->out, "%s", zBuf);
19502 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
19503 int bIsInit = 0; /* True to initialize the SELFTEST table */
19504 int bVerbose = 0; /* Verbose output */
19505 int bSelftestExists; /* True if SELFTEST already exists */
19506 int i, k; /* Loop counters */
19507 int nTest = 0; /* Number of tests runs */
19508 int nErr = 0; /* Number of errors seen */
19509 ShellText str; /* Answer for a query */
19510 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
19513 for(i=1; i<nArg; i++){
19514 const char *z = azArg[i];
19515 if( z[0]=='-' && z[1]=='-' ) z++;
19516 if( strcmp(z,"-init")==0 ){
19519 if( strcmp(z,"-v")==0 ){
19523 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19524 azArg[i], azArg[0]);
19525 raw_printf(stderr, "Should be one of: --init -v\n");
19527 goto meta_command_exit;
19530 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
19532 bSelftestExists = 0;
19534 bSelftestExists = 1;
19537 createSelftestTable(p);
19538 bSelftestExists = 1;
19541 appendText(&str, "x", 0);
19542 for(k=bSelftestExists; k>=0; k--){
19544 rc = sqlite3_prepare_v2(p->db,
19545 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
19548 rc = sqlite3_prepare_v2(p->db,
19549 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
19550 " (1,'run','PRAGMA integrity_check','ok')",
19554 raw_printf(stderr, "Error querying the selftest table\n");
19556 sqlite3_finalize(pStmt);
19557 goto meta_command_exit;
19559 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
19560 int tno = sqlite3_column_int(pStmt, 0);
19561 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
19562 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
19563 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
19567 char *zQuote = sqlite3_mprintf("%q", zSql);
19568 printf("%d: %s %s\n", tno, zOp, zSql);
19569 sqlite3_free(zQuote);
19571 if( strcmp(zOp,"memo")==0 ){
19572 utf8_printf(p->out, "%s\n", zSql);
19574 if( strcmp(zOp,"run")==0 ){
19578 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
19581 utf8_printf(p->out, "Result: %s\n", str.z);
19583 if( rc || zErrMsg ){
19586 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
19587 sqlite3_free(zErrMsg);
19588 }else if( strcmp(zAns,str.z)!=0 ){
19591 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
19592 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
19596 utf8_printf(stderr,
19597 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
19601 } /* End loop over rows of content from SELFTEST */
19602 sqlite3_finalize(pStmt);
19603 } /* End loop over k */
19605 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
19608 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
19609 if( nArg<2 || nArg>3 ){
19610 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
19614 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
19615 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
19618 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
19619 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
19623 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
19624 const char *zLike = 0; /* Which table to checksum. 0 means everything */
19625 int i; /* Loop counter */
19626 int bSchema = 0; /* Also hash the schema */
19627 int bSeparate = 0; /* Hash each table separately */
19628 int iSize = 224; /* Hash algorithm to use */
19629 int bDebug = 0; /* Only show the query that would have run */
19630 sqlite3_stmt *pStmt; /* For querying tables names */
19631 char *zSql; /* SQL to be run */
19632 char *zSep; /* Separator */
19633 ShellText sSql; /* Complete SQL for the query to run the hash */
19634 ShellText sQuery; /* Set of queries used to read all content */
19636 for(i=1; i<nArg; i++){
19637 const char *z = azArg[i];
19640 if( z[0]=='-' ) z++;
19641 if( strcmp(z,"schema")==0 ){
19644 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
19645 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
19647 iSize = atoi(&z[5]);
19649 if( strcmp(z,"debug")==0 ){
19653 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
19654 azArg[i], azArg[0]);
19655 showHelp(p->out, azArg[0]);
19657 goto meta_command_exit;
19660 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
19662 goto meta_command_exit;
19666 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
19670 zSql = "SELECT lower(name) FROM sqlite_schema"
19671 " WHERE type='table' AND coalesce(rootpage,0)>1"
19672 " UNION ALL SELECT 'sqlite_schema'"
19673 " ORDER BY 1 collate nocase";
19675 zSql = "SELECT lower(name) FROM sqlite_schema"
19676 " WHERE type='table' AND coalesce(rootpage,0)>1"
19677 " AND name NOT LIKE 'sqlite_%'"
19678 " ORDER BY 1 collate nocase";
19680 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
19683 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
19685 while( SQLITE_ROW==sqlite3_step(pStmt) ){
19686 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
19687 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
19688 if( strncmp(zTab, "sqlite_",7)!=0 ){
19689 appendText(&sQuery,"SELECT * FROM ", 0);
19690 appendText(&sQuery,zTab,'"');
19691 appendText(&sQuery," NOT INDEXED;", 0);
19692 }else if( strcmp(zTab, "sqlite_schema")==0 ){
19693 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_schema"
19694 " ORDER BY name;", 0);
19695 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
19696 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
19697 " ORDER BY name;", 0);
19698 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
19699 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
19700 " ORDER BY tbl,idx;", 0);
19701 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
19702 appendText(&sQuery, "SELECT * FROM ", 0);
19703 appendText(&sQuery, zTab, 0);
19704 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
19706 appendText(&sSql, zSep, 0);
19707 appendText(&sSql, sQuery.z, '\'');
19709 appendText(&sSql, ",", 0);
19710 appendText(&sSql, zTab, '\'');
19713 sqlite3_finalize(pStmt);
19715 zSql = sqlite3_mprintf(
19717 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
19718 " FROM [sha3sum$query]",
19721 zSql = sqlite3_mprintf(
19723 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
19724 " FROM [sha3sum$query]",
19730 utf8_printf(p->out, "%s\n", zSql);
19732 shell_exec(p, zSql, 0);
19734 sqlite3_free(zSql);
19737 #ifndef SQLITE_NOHAVE_SYSTEM
19739 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
19744 raw_printf(stderr, "Usage: .system COMMAND\n");
19746 goto meta_command_exit;
19748 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
19749 for(i=2; i<nArg; i++){
19750 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
19754 sqlite3_free(zCmd);
19755 if( x ) raw_printf(stderr, "System command returns %d\n", x);
19757 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
19759 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
19760 static const char *azBool[] = { "off", "on", "trigger", "full"};
19763 raw_printf(stderr, "Usage: .show\n");
19765 goto meta_command_exit;
19767 utf8_printf(p->out, "%12.12s: %s\n","echo",
19768 azBool[ShellHasFlag(p, SHFLG_Echo)]);
19769 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
19770 utf8_printf(p->out, "%12.12s: %s\n","explain",
19771 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
19772 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
19773 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
19774 utf8_printf(p->out, "%12.12s: ", "nullvalue");
19775 output_c_string(p->out, p->nullValue);
19776 raw_printf(p->out, "\n");
19777 utf8_printf(p->out,"%12.12s: %s\n","output",
19778 strlen30(p->outfile) ? p->outfile : "stdout");
19779 utf8_printf(p->out,"%12.12s: ", "colseparator");
19780 output_c_string(p->out, p->colSeparator);
19781 raw_printf(p->out, "\n");
19782 utf8_printf(p->out,"%12.12s: ", "rowseparator");
19783 output_c_string(p->out, p->rowSeparator);
19784 raw_printf(p->out, "\n");
19785 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
19786 utf8_printf(p->out, "%12.12s: ", "width");
19787 for (i=0;i<p->nWidth;i++) {
19788 raw_printf(p->out, "%d ", p->colWidth[i]);
19790 raw_printf(p->out, "\n");
19791 utf8_printf(p->out, "%12.12s: %s\n", "filename",
19792 p->zDbFilename ? p->zDbFilename : "");
19795 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
19797 p->statsOn = (u8)booleanValue(azArg[1]);
19798 }else if( nArg==1 ){
19799 display_stats(p->db, p, 0);
19801 raw_printf(stderr, "Usage: .stats ?on|off?\n");
19806 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
19807 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
19808 || strncmp(azArg[0], "indexes", n)==0) )
19810 sqlite3_stmt *pStmt;
19817 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
19819 sqlite3_finalize(pStmt);
19820 return shellDatabaseError(p->db);
19823 if( nArg>2 && c=='i' ){
19824 /* It is an historical accident that the .indexes command shows an error
19825 ** when called with the wrong number of arguments whereas the .tables
19826 ** command does not. */
19827 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
19829 sqlite3_finalize(pStmt);
19830 goto meta_command_exit;
19832 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
19833 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
19834 if( zDbName==0 ) continue;
19835 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
19836 if( sqlite3_stricmp(zDbName, "main")==0 ){
19837 appendText(&s, "SELECT name FROM ", 0);
19839 appendText(&s, "SELECT ", 0);
19840 appendText(&s, zDbName, '\'');
19841 appendText(&s, "||'.'||name FROM ", 0);
19843 appendText(&s, zDbName, '"');
19844 appendText(&s, ".sqlite_schema ", 0);
19846 appendText(&s," WHERE type IN ('table','view')"
19847 " AND name NOT LIKE 'sqlite_%'"
19848 " AND name LIKE ?1", 0);
19850 appendText(&s," WHERE type='index'"
19851 " AND tbl_name LIKE ?1", 0);
19854 rc = sqlite3_finalize(pStmt);
19855 appendText(&s, " ORDER BY 1", 0);
19856 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
19858 if( rc ) return shellDatabaseError(p->db);
19860 /* Run the SQL statement prepared by the above block. Store the results
19861 ** as an array of nul-terminated strings in azResult[]. */
19865 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
19867 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
19869 while( sqlite3_step(pStmt)==SQLITE_ROW ){
19870 if( nRow>=nAlloc ){
19872 int n2 = nAlloc*2 + 10;
19873 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
19874 if( azNew==0 ) shell_out_of_memory();
19878 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
19879 if( 0==azResult[nRow] ) shell_out_of_memory();
19882 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
19883 rc = shellDatabaseError(p->db);
19886 /* Pretty-print the contents of array azResult[] to the output */
19887 if( rc==0 && nRow>0 ){
19888 int len, maxlen = 0;
19890 int nPrintCol, nPrintRow;
19891 for(i=0; i<nRow; i++){
19892 len = strlen30(azResult[i]);
19893 if( len>maxlen ) maxlen = len;
19895 nPrintCol = 80/(maxlen+2);
19896 if( nPrintCol<1 ) nPrintCol = 1;
19897 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
19898 for(i=0; i<nPrintRow; i++){
19899 for(j=i; j<nRow; j+=nPrintRow){
19900 char *zSp = j<nPrintRow ? "" : " ";
19901 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
19902 azResult[j] ? azResult[j]:"");
19904 raw_printf(p->out, "\n");
19908 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
19909 sqlite3_free(azResult);
19912 /* Begin redirecting output to the file "testcase-out.txt" */
19913 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
19915 p->out = output_file_open("testcase-out.txt", 0);
19917 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
19920 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
19922 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
19926 #ifndef SQLITE_UNTESTABLE
19927 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
19928 static const struct {
19929 const char *zCtrlName; /* Name of a test-control option */
19930 int ctrlCode; /* Integer code for that option */
19931 const char *zUsage; /* Usage notes */
19933 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
19934 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
19935 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
19936 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
19937 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
19938 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
19939 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
19940 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
19941 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
19942 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
19943 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
19944 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
19946 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
19948 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
19949 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
19950 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
19951 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
19952 { "seek_count", SQLITE_TESTCTRL_SEEK_COUNT, "" },
19956 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
19959 const char *zCmd = 0;
19962 zCmd = nArg>=2 ? azArg[1] : "help";
19964 /* The argument can optionally begin with "-" or "--" */
19965 if( zCmd[0]=='-' && zCmd[1] ){
19967 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
19970 /* --help lists all test-controls */
19971 if( strcmp(zCmd,"help")==0 ){
19972 utf8_printf(p->out, "Available test-controls:\n");
19973 for(i=0; i<ArraySize(aCtrl); i++){
19974 utf8_printf(p->out, " .testctrl %s %s\n",
19975 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
19978 goto meta_command_exit;
19981 /* convert testctrl text option to value. allow any unique prefix
19982 ** of the option name, or a numerical value. */
19983 n2 = strlen30(zCmd);
19984 for(i=0; i<ArraySize(aCtrl); i++){
19985 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
19987 testctrl = aCtrl[i].ctrlCode;
19990 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
19991 "Use \".testctrl --help\" for help\n", zCmd);
19993 goto meta_command_exit;
19998 utf8_printf(stderr,"Error: unknown test-control: %s\n"
19999 "Use \".testctrl --help\" for help\n", zCmd);
20003 /* sqlite3_test_control(int, db, int) */
20004 case SQLITE_TESTCTRL_OPTIMIZATIONS:
20006 int opt = (int)strtol(azArg[2], 0, 0);
20007 rc2 = sqlite3_test_control(testctrl, p->db, opt);
20012 /* sqlite3_test_control(int) */
20013 case SQLITE_TESTCTRL_PRNG_SAVE:
20014 case SQLITE_TESTCTRL_PRNG_RESTORE:
20015 case SQLITE_TESTCTRL_BYTEORDER:
20017 rc2 = sqlite3_test_control(testctrl);
20018 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
20022 /* sqlite3_test_control(int, uint) */
20023 case SQLITE_TESTCTRL_PENDING_BYTE:
20025 unsigned int opt = (unsigned int)integerValue(azArg[2]);
20026 rc2 = sqlite3_test_control(testctrl, opt);
20031 /* sqlite3_test_control(int, int, sqlite3*) */
20032 case SQLITE_TESTCTRL_PRNG_SEED:
20033 if( nArg==3 || nArg==4 ){
20034 int ii = (int)integerValue(azArg[2]);
20036 if( ii==0 && strcmp(azArg[2],"random")==0 ){
20037 sqlite3_randomness(sizeof(ii),&ii);
20038 printf("-- random seed: %d\n", ii);
20044 /* Make sure the schema has been loaded */
20045 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
20047 rc2 = sqlite3_test_control(testctrl, ii, db);
20052 /* sqlite3_test_control(int, int) */
20053 case SQLITE_TESTCTRL_ASSERT:
20054 case SQLITE_TESTCTRL_ALWAYS:
20056 int opt = booleanValue(azArg[2]);
20057 rc2 = sqlite3_test_control(testctrl, opt);
20062 /* sqlite3_test_control(int, int) */
20063 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
20064 case SQLITE_TESTCTRL_NEVER_CORRUPT:
20066 int opt = booleanValue(azArg[2]);
20067 rc2 = sqlite3_test_control(testctrl, opt);
20072 /* sqlite3_test_control(sqlite3*) */
20073 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
20074 rc2 = sqlite3_test_control(testctrl, p->db);
20078 case SQLITE_TESTCTRL_IMPOSTER:
20080 rc2 = sqlite3_test_control(testctrl, p->db,
20082 integerValue(azArg[3]),
20083 integerValue(azArg[4]));
20088 case SQLITE_TESTCTRL_SEEK_COUNT: {
20090 rc2 = sqlite3_test_control(testctrl, p->db, &x);
20091 utf8_printf(p->out, "%llu\n", x);
20097 case SQLITE_TESTCTRL_PARSER_COVERAGE:
20099 sqlite3_test_control(testctrl, p->out);
20105 if( isOk==0 && iCtrl>=0 ){
20106 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
20108 }else if( isOk==1 ){
20109 raw_printf(p->out, "%d\n", rc2);
20110 }else if( isOk==2 ){
20111 raw_printf(p->out, "0x%08x\n", rc2);
20114 #endif /* !defined(SQLITE_UNTESTABLE) */
20116 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
20118 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
20121 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
20123 enableTimer = booleanValue(azArg[1]);
20124 if( enableTimer && !HAS_TIMER ){
20125 raw_printf(stderr, "Error: timer not available on this system.\n");
20129 raw_printf(stderr, "Usage: .timer on|off\n");
20134 #ifndef SQLITE_OMIT_TRACE
20135 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
20139 for(jj=1; jj<nArg; jj++){
20140 const char *z = azArg[jj];
20142 if( optionMatch(z, "expanded") ){
20143 p->eTraceType = SHELL_TRACE_EXPANDED;
20145 #ifdef SQLITE_ENABLE_NORMALIZE
20146 else if( optionMatch(z, "normalized") ){
20147 p->eTraceType = SHELL_TRACE_NORMALIZED;
20150 else if( optionMatch(z, "plain") ){
20151 p->eTraceType = SHELL_TRACE_PLAIN;
20153 else if( optionMatch(z, "profile") ){
20154 mType |= SQLITE_TRACE_PROFILE;
20156 else if( optionMatch(z, "row") ){
20157 mType |= SQLITE_TRACE_ROW;
20159 else if( optionMatch(z, "stmt") ){
20160 mType |= SQLITE_TRACE_STMT;
20162 else if( optionMatch(z, "close") ){
20163 mType |= SQLITE_TRACE_CLOSE;
20166 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
20168 goto meta_command_exit;
20171 output_file_close(p->traceOut);
20172 p->traceOut = output_file_open(azArg[1], 0);
20175 if( p->traceOut==0 ){
20176 sqlite3_trace_v2(p->db, 0, 0, 0);
20178 if( mType==0 ) mType = SQLITE_TRACE_STMT;
20179 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
20182 #endif /* !defined(SQLITE_OMIT_TRACE) */
20184 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20185 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
20190 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
20192 goto meta_command_exit;
20196 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
20197 lenOpt = (int)strlen(zOpt);
20198 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
20199 assert( azArg[nArg]==0 );
20200 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
20202 for(ii=1; ii<nArg; ii++){
20203 sqlite3_create_module(p->db, azArg[ii], 0, 0);
20209 #if SQLITE_USER_AUTHENTICATION
20210 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
20212 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
20214 goto meta_command_exit;
20217 if( strcmp(azArg[1],"login")==0 ){
20219 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
20221 goto meta_command_exit;
20223 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
20224 strlen30(azArg[3]));
20226 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
20229 }else if( strcmp(azArg[1],"add")==0 ){
20231 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
20233 goto meta_command_exit;
20235 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20236 booleanValue(azArg[4]));
20238 raw_printf(stderr, "User-Add failed: %d\n", rc);
20241 }else if( strcmp(azArg[1],"edit")==0 ){
20243 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
20245 goto meta_command_exit;
20247 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
20248 booleanValue(azArg[4]));
20250 raw_printf(stderr, "User-Edit failed: %d\n", rc);
20253 }else if( strcmp(azArg[1],"delete")==0 ){
20255 raw_printf(stderr, "Usage: .user delete USER\n");
20257 goto meta_command_exit;
20259 rc = sqlite3_user_delete(p->db, azArg[2]);
20261 raw_printf(stderr, "User-Delete failed: %d\n", rc);
20265 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
20267 goto meta_command_exit;
20270 #endif /* SQLITE_USER_AUTHENTICATION */
20272 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
20273 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
20274 sqlite3_libversion(), sqlite3_sourceid());
20275 #if SQLITE_HAVE_ZLIB
20276 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
20278 #define CTIMEOPT_VAL_(opt) #opt
20279 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
20280 #if defined(__clang__) && defined(__clang_major__)
20281 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
20282 CTIMEOPT_VAL(__clang_minor__) "."
20283 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
20284 #elif defined(_MSC_VER)
20285 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
20286 #elif defined(__GNUC__) && defined(__VERSION__)
20287 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
20291 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
20292 const char *zDbName = nArg==2 ? azArg[1] : "main";
20293 sqlite3_vfs *pVfs = 0;
20295 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
20297 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
20298 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20299 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20300 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20305 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
20307 sqlite3_vfs *pCurrent = 0;
20309 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
20311 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
20312 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
20313 pVfs==pCurrent ? " <--- CURRENT" : "");
20314 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
20315 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
20316 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
20318 raw_printf(p->out, "-----------------------------------\n");
20323 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
20324 const char *zDbName = nArg==2 ? azArg[1] : "main";
20325 char *zVfsName = 0;
20327 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
20329 utf8_printf(p->out, "%s\n", zVfsName);
20330 sqlite3_free(zVfsName);
20335 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
20336 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
20337 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
20341 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
20343 assert( nArg<=ArraySize(azArg) );
20344 p->nWidth = nArg-1;
20345 p->colWidth = realloc(p->colWidth, p->nWidth*sizeof(int)*2);
20346 if( p->colWidth==0 && p->nWidth>0 ) shell_out_of_memory();
20347 if( p->nWidth ) p->actualWidth = &p->colWidth[p->nWidth];
20348 for(j=1; j<nArg; j++){
20349 p->colWidth[j-1] = (int)integerValue(azArg[j]);
20354 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
20355 " \"%s\". Enter \".help\" for help\n", azArg[0]);
20362 if( p->outCount==0 ) output_reset(p);
20368 ** Return TRUE if a semicolon occurs anywhere in the first N characters
20371 static int line_contains_semicolon(const char *z, int N){
20373 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
20378 ** Test to see if a line consists entirely of whitespace.
20380 static int _all_whitespace(const char *z){
20382 if( IsSpace(z[0]) ) continue;
20383 if( *z=='/' && z[1]=='*' ){
20385 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
20386 if( *z==0 ) return 0;
20390 if( *z=='-' && z[1]=='-' ){
20392 while( *z && *z!='\n' ){ z++; }
20393 if( *z==0 ) return 1;
20402 ** Return TRUE if the line typed in is an SQL command terminator other
20403 ** than a semi-colon. The SQL Server style "go" command is understood
20404 ** as is the Oracle "/".
20406 static int line_is_command_terminator(const char *zLine){
20407 while( IsSpace(zLine[0]) ){ zLine++; };
20408 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
20409 return 1; /* Oracle */
20411 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
20412 && _all_whitespace(&zLine[2]) ){
20413 return 1; /* SQL Server */
20419 ** We need a default sqlite3_complete() implementation to use in case
20420 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
20421 ** any arbitrary text is a complete SQL statement. This is not very
20422 ** user-friendly, but it does seem to work.
20424 #ifdef SQLITE_OMIT_COMPLETE
20425 #define sqlite3_complete(x) 1
20429 ** Return true if zSql is a complete SQL statement. Return false if it
20430 ** ends in the middle of a string literal or C-style comment.
20432 static int line_is_complete(char *zSql, int nSql){
20434 if( zSql==0 ) return 1;
20437 rc = sqlite3_complete(zSql);
20443 ** Run a single line of SQL. Return the number of errors.
20445 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
20450 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
20451 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
20453 rc = shell_exec(p, zSql, &zErrMsg);
20455 if( rc || zErrMsg ){
20457 if( in!=0 || !stdin_is_interactive ){
20458 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
20459 "Error: near line %d:", startline);
20461 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
20464 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
20465 sqlite3_free(zErrMsg);
20468 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
20471 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
20472 raw_printf(p->out, "changes: %3d total_changes: %d\n",
20473 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
20480 ** Read input from *in and process it. If *in==0 then input
20481 ** is interactive - the user is typing it it. Otherwise, input
20482 ** is coming from a file or device. A prompt is issued and history
20483 ** is saved only if input is interactive. An interrupt signal will
20484 ** cause this routine to exit immediately, unless input is interactive.
20486 ** Return the number of errors.
20488 static int process_input(ShellState *p){
20489 char *zLine = 0; /* A single input line */
20490 char *zSql = 0; /* Accumulated SQL text */
20491 int nLine; /* Length of current line */
20492 int nSql = 0; /* Bytes of zSql[] used */
20493 int nAlloc = 0; /* Allocated zSql[] space */
20494 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
20495 int rc; /* Error code */
20496 int errCnt = 0; /* Number of errors seen */
20497 int startline = 0; /* Line number for start of current input */
20500 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
20502 zLine = one_input_line(p->in, zLine, nSql>0);
20505 if( p->in==0 && stdin_is_interactive ) printf("\n");
20508 if( seenInterrupt ){
20509 if( p->in!=0 ) break;
20513 if( nSql==0 && _all_whitespace(zLine) ){
20514 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20517 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
20518 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
20519 if( zLine[0]=='.' ){
20520 rc = do_meta_command(zLine, p);
20521 if( rc==2 ){ /* exit requested */
20529 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
20530 memcpy(zLine,";",2);
20532 nLine = strlen30(zLine);
20533 if( nSql+nLine+2>=nAlloc ){
20534 nAlloc = nSql+nLine+100;
20535 zSql = realloc(zSql, nAlloc);
20536 if( zSql==0 ) shell_out_of_memory();
20541 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
20542 assert( nAlloc>0 && zSql!=0 );
20543 memcpy(zSql, zLine+i, nLine+1-i);
20544 startline = p->lineno;
20547 zSql[nSql++] = '\n';
20548 memcpy(zSql+nSql, zLine, nLine+1);
20551 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
20552 && sqlite3_complete(zSql) ){
20553 errCnt += runOneSqlLine(p, zSql, p->in, startline);
20561 }else if( nSql && _all_whitespace(zSql) ){
20562 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
20566 if( nSql && !_all_whitespace(zSql) ){
20567 errCnt += runOneSqlLine(p, zSql, p->in, startline);
20575 ** Return a pathname which is the user's home directory. A
20576 ** 0 return indicates an error of some kind.
20578 static char *find_home_dir(int clearFlag){
20579 static char *home_dir = NULL;
20585 if( home_dir ) return home_dir;
20587 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
20588 && !defined(__RTP__) && !defined(_WRS_KERNEL)
20590 struct passwd *pwent;
20591 uid_t uid = getuid();
20592 if( (pwent=getpwuid(uid)) != NULL) {
20593 home_dir = pwent->pw_dir;
20598 #if defined(_WIN32_WCE)
20599 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
20604 #if defined(_WIN32) || defined(WIN32)
20606 home_dir = getenv("USERPROFILE");
20611 home_dir = getenv("HOME");
20614 #if defined(_WIN32) || defined(WIN32)
20616 char *zDrive, *zPath;
20618 zDrive = getenv("HOMEDRIVE");
20619 zPath = getenv("HOMEPATH");
20620 if( zDrive && zPath ){
20621 n = strlen30(zDrive) + strlen30(zPath) + 1;
20622 home_dir = malloc( n );
20623 if( home_dir==0 ) return 0;
20624 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
20631 #endif /* !_WIN32_WCE */
20634 int n = strlen30(home_dir) + 1;
20635 char *z = malloc( n );
20636 if( z ) memcpy(z, home_dir, n);
20644 ** Read input from the file given by sqliterc_override. Or if that
20645 ** parameter is NULL, take input from ~/.sqliterc
20647 ** Returns the number of errors.
20649 static void process_sqliterc(
20650 ShellState *p, /* Configuration data */
20651 const char *sqliterc_override /* Name of config file. NULL to use default */
20653 char *home_dir = NULL;
20654 const char *sqliterc = sqliterc_override;
20656 FILE *inSaved = p->in;
20657 int savedLineno = p->lineno;
20659 if (sqliterc == NULL) {
20660 home_dir = find_home_dir(0);
20662 raw_printf(stderr, "-- warning: cannot find home directory;"
20663 " cannot read ~/.sqliterc\n");
20666 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
20669 p->in = fopen(sqliterc,"rb");
20671 if( stdin_is_interactive ){
20672 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
20674 if( process_input(p) && bail_on_error ) exit(1);
20676 }else if( sqliterc_override!=0 ){
20677 utf8_printf(stderr,"cannot open: \"%s\"\n", sqliterc);
20678 if( bail_on_error ) exit(1);
20681 p->lineno = savedLineno;
20682 sqlite3_free(zBuf);
20686 ** Show available command line options
20688 static const char zOptions[] =
20689 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
20690 " -A ARGS... run \".archive ARGS\" and exit\n"
20692 " -append append the database to the end of the file\n"
20693 " -ascii set output mode to 'ascii'\n"
20694 " -bail stop after hitting an error\n"
20695 " -batch force batch I/O\n"
20696 " -box set output mode to 'box'\n"
20697 " -column set output mode to 'column'\n"
20698 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
20699 " -csv set output mode to 'csv'\n"
20700 #if defined(SQLITE_ENABLE_DESERIALIZE)
20701 " -deserialize open the database using sqlite3_deserialize()\n"
20703 " -echo print commands before execution\n"
20704 " -init FILENAME read/process named file\n"
20705 " -[no]header turn headers on or off\n"
20706 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20707 " -heap SIZE Size of heap for memsys3 or memsys5\n"
20709 " -help show this message\n"
20710 " -html set output mode to HTML\n"
20711 " -interactive force interactive I/O\n"
20712 " -json set output mode to 'json'\n"
20713 " -line set output mode to 'line'\n"
20714 " -list set output mode to 'list'\n"
20715 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
20716 " -markdown set output mode to 'markdown'\n"
20717 #if defined(SQLITE_ENABLE_DESERIALIZE)
20718 " -maxsize N maximum size for a --deserialize database\n"
20720 " -memtrace trace all memory allocations and deallocations\n"
20721 " -mmap N default mmap size set to N\n"
20722 #ifdef SQLITE_ENABLE_MULTIPLEX
20723 " -multiplex enable the multiplexor VFS\n"
20725 " -newline SEP set output row separator. Default: '\\n'\n"
20726 " -nofollow refuse to open symbolic links to database files\n"
20727 " -nullvalue TEXT set text string for NULL values. Default ''\n"
20728 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
20729 " -quote set output mode to 'quote'\n"
20730 " -readonly open the database read-only\n"
20731 " -separator SEP set output column separator. Default: '|'\n"
20732 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
20733 " -sorterref SIZE sorter references threshold size\n"
20735 " -stats print memory stats before each finalize\n"
20736 " -table set output mode to 'table'\n"
20737 " -tabs set output mode to 'tabs'\n"
20738 " -version show SQLite version\n"
20739 " -vfs NAME use NAME as the default VFS\n"
20740 #ifdef SQLITE_ENABLE_VFSTRACE
20741 " -vfstrace enable tracing of all VFS calls\n"
20743 #ifdef SQLITE_HAVE_ZLIB
20744 " -zip open the file as a ZIP Archive\n"
20747 static void usage(int showDetail){
20748 utf8_printf(stderr,
20749 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
20750 "FILENAME is the name of an SQLite database. A new database is created\n"
20751 "if the file does not previously exist.\n", Argv0);
20753 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
20755 raw_printf(stderr, "Use the -help option for additional information\n");
20761 ** Internal check: Verify that the SQLite is uninitialized. Print a
20762 ** error message if it is initialized.
20764 static void verify_uninitialized(void){
20765 if( sqlite3_config(-1)==SQLITE_MISUSE ){
20766 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
20767 " initialization.\n");
20772 ** Initialize the state information in data
20774 static void main_init(ShellState *data) {
20775 memset(data, 0, sizeof(*data));
20776 data->normalMode = data->cMode = data->mode = MODE_List;
20777 data->autoExplain = 1;
20778 memcpy(data->colSeparator,SEP_Column, 2);
20779 memcpy(data->rowSeparator,SEP_Row, 2);
20780 data->showHeader = 0;
20781 data->shellFlgs = SHFLG_Lookaside;
20782 verify_uninitialized();
20783 sqlite3_config(SQLITE_CONFIG_URI, 1);
20784 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
20785 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
20786 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
20787 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
20791 ** Output text to the console in a font that attracts extra attention.
20794 static void printBold(const char *zText){
20795 #if !SQLITE_OS_WINRT
20796 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
20797 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
20798 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
20799 SetConsoleTextAttribute(out,
20800 FOREGROUND_RED|FOREGROUND_INTENSITY
20803 printf("%s", zText);
20804 #if !SQLITE_OS_WINRT
20805 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
20809 static void printBold(const char *zText){
20810 printf("\033[1m%s\033[0m", zText);
20815 ** Get the argument to an --option. Throw an error and die if no argument
20818 static char *cmdline_option_value(int argc, char **argv, int i){
20820 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
20821 argv[0], argv[argc-1]);
20827 #ifndef SQLITE_SHELL_IS_UTF8
20828 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
20829 # define SQLITE_SHELL_IS_UTF8 (0)
20831 # define SQLITE_SHELL_IS_UTF8 (1)
20835 #if SQLITE_SHELL_IS_UTF8
20836 int SQLITE_CDECL main(int argc, char **argv){
20838 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
20843 const char *zInitFile = 0;
20846 int warnInmemoryDb = 0;
20850 const char *zVfs = 0; /* Value of -vfs command-line option */
20851 #if !SQLITE_SHELL_IS_UTF8
20852 char **argvToFree = 0;
20853 int argcToFree = 0;
20856 setBinaryMode(stdin, 0);
20857 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
20858 stdin_is_interactive = isatty(0);
20859 stdout_is_console = isatty(1);
20861 #ifdef SQLITE_DEBUG
20862 registerOomSimulator();
20865 #if !defined(_WIN32_WCE)
20866 if( getenv("SQLITE_DEBUG_BREAK") ){
20867 if( isatty(0) && isatty(2) ){
20869 "attach debugger to process %d and press any key to continue.\n",
20873 #if defined(_WIN32) || defined(WIN32)
20874 #if SQLITE_OS_WINRT
20879 #elif defined(SIGTRAP)
20886 #if USE_SYSTEM_SQLITE+0!=1
20887 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
20888 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
20889 sqlite3_sourceid(), SQLITE_SOURCE_ID);
20895 /* On Windows, we must translate command-line arguments into UTF-8.
20896 ** The SQLite memory allocator subsystem has to be enabled in order to
20897 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
20898 ** subsequent sqlite3_config() calls will work. So copy all results into
20899 ** memory that does not come from the SQLite memory allocator.
20901 #if !SQLITE_SHELL_IS_UTF8
20902 sqlite3_initialize();
20903 argvToFree = malloc(sizeof(argv[0])*argc*2);
20905 argv = argvToFree + argc;
20906 if( argv==0 ) shell_out_of_memory();
20907 for(i=0; i<argc; i++){
20908 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
20910 if( z==0 ) shell_out_of_memory();
20911 n = (int)strlen(z);
20912 argv[i] = malloc( n+1 );
20913 if( argv[i]==0 ) shell_out_of_memory();
20914 memcpy(argv[i], z, n+1);
20915 argvToFree[i] = argv[i];
20918 sqlite3_shutdown();
20921 assert( argc>=1 && argv && argv[0] );
20924 /* Make sure we have a valid signal handler early, before anything
20928 signal(SIGINT, interrupt_handler);
20929 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
20930 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
20933 #ifdef SQLITE_SHELL_DBNAME_PROC
20935 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
20936 ** of a C-function that will provide the name of the database file. Use
20937 ** this compile-time option to embed this shell program in larger
20938 ** applications. */
20939 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
20940 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
20941 warnInmemoryDb = 0;
20945 /* Do an initial pass through the command-line argument to locate
20946 ** the name of the database file, the name of the initialization file,
20947 ** the size of the alternative malloc heap,
20948 ** and the first command to execute.
20950 verify_uninitialized();
20951 for(i=1; i<argc; i++){
20955 if( data.zDbFilename==0 ){
20956 data.zDbFilename = z;
20958 /* Excesss arguments are interpreted as SQL (or dot-commands) and
20959 ** mean that nothing is read from stdin */
20962 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
20963 if( azCmd==0 ) shell_out_of_memory();
20967 if( z[1]=='-' ) z++;
20968 if( strcmp(z,"-separator")==0
20969 || strcmp(z,"-nullvalue")==0
20970 || strcmp(z,"-newline")==0
20971 || strcmp(z,"-cmd")==0
20973 (void)cmdline_option_value(argc, argv, ++i);
20974 }else if( strcmp(z,"-init")==0 ){
20975 zInitFile = cmdline_option_value(argc, argv, ++i);
20976 }else if( strcmp(z,"-batch")==0 ){
20977 /* Need to check for batch mode here to so we can avoid printing
20978 ** informational messages (like from process_sqliterc) before
20979 ** we do the actual processing of arguments later in a second pass.
20981 stdin_is_interactive = 0;
20982 }else if( strcmp(z,"-heap")==0 ){
20983 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
20985 sqlite3_int64 szHeap;
20987 zSize = cmdline_option_value(argc, argv, ++i);
20988 szHeap = integerValue(zSize);
20989 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
20990 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
20992 (void)cmdline_option_value(argc, argv, ++i);
20994 }else if( strcmp(z,"-pagecache")==0 ){
20995 sqlite3_int64 n, sz;
20996 sz = integerValue(cmdline_option_value(argc,argv,++i));
20997 if( sz>70000 ) sz = 70000;
20999 n = integerValue(cmdline_option_value(argc,argv,++i));
21000 if( sz>0 && n>0 && 0xffffffffffffLL/sz<n ){
21001 n = 0xffffffffffffLL/sz;
21003 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
21004 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
21005 data.shellFlgs |= SHFLG_Pagecache;
21006 }else if( strcmp(z,"-lookaside")==0 ){
21008 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
21010 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
21012 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
21013 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
21014 #ifdef SQLITE_ENABLE_VFSTRACE
21015 }else if( strcmp(z,"-vfstrace")==0 ){
21016 extern int vfstrace_register(
21017 const char *zTraceName,
21018 const char *zOldVfsName,
21019 int (*xOut)(const char*,void*),
21023 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
21025 #ifdef SQLITE_ENABLE_MULTIPLEX
21026 }else if( strcmp(z,"-multiplex")==0 ){
21027 extern int sqlite3_multiple_initialize(const char*,int);
21028 sqlite3_multiplex_initialize(0, 1);
21030 }else if( strcmp(z,"-mmap")==0 ){
21031 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21032 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
21033 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21034 }else if( strcmp(z,"-sorterref")==0 ){
21035 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
21036 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
21038 }else if( strcmp(z,"-vfs")==0 ){
21039 zVfs = cmdline_option_value(argc, argv, ++i);
21040 #ifdef SQLITE_HAVE_ZLIB
21041 }else if( strcmp(z,"-zip")==0 ){
21042 data.openMode = SHELL_OPEN_ZIPFILE;
21044 }else if( strcmp(z,"-append")==0 ){
21045 data.openMode = SHELL_OPEN_APPENDVFS;
21046 #ifdef SQLITE_ENABLE_DESERIALIZE
21047 }else if( strcmp(z,"-deserialize")==0 ){
21048 data.openMode = SHELL_OPEN_DESERIALIZE;
21049 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21050 data.szMax = integerValue(argv[++i]);
21052 }else if( strcmp(z,"-readonly")==0 ){
21053 data.openMode = SHELL_OPEN_READONLY;
21054 }else if( strcmp(z,"-nofollow")==0 ){
21055 data.openFlags = SQLITE_OPEN_NOFOLLOW;
21056 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21057 }else if( strncmp(z, "-A",2)==0 ){
21058 /* All remaining command-line arguments are passed to the ".archive"
21059 ** command, so ignore them */
21062 }else if( strcmp(z, "-memtrace")==0 ){
21063 sqlite3MemTraceActivate(stderr);
21064 }else if( strcmp(z,"-bail")==0 ){
21068 verify_uninitialized();
21071 #ifdef SQLITE_SHELL_INIT_PROC
21073 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
21074 ** of a C-function that will perform initialization actions on SQLite that
21075 ** occur just before or after sqlite3_initialize(). Use this compile-time
21076 ** option to embed this shell program in larger applications. */
21077 extern void SQLITE_SHELL_INIT_PROC(void);
21078 SQLITE_SHELL_INIT_PROC();
21081 /* All the sqlite3_config() calls have now been made. So it is safe
21082 ** to call sqlite3_initialize() and process any command line -vfs option. */
21083 sqlite3_initialize();
21087 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
21089 sqlite3_vfs_register(pVfs, 1);
21091 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
21096 if( data.zDbFilename==0 ){
21097 #ifndef SQLITE_OMIT_MEMORYDB
21098 data.zDbFilename = ":memory:";
21099 warnInmemoryDb = argc==1;
21101 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
21106 sqlite3_appendvfs_init(0,0,0);
21108 /* Go ahead and open the database file if it already exists. If the
21109 ** file does not exist, delay opening it. This prevents empty database
21110 ** files from being created if a user mistypes the database name argument
21111 ** to the sqlite command-line tool.
21113 if( access(data.zDbFilename, 0)==0 ){
21117 /* Process the initialization file if there is one. If no -init option
21118 ** is given on the command line, look for a file named ~/.sqliterc and
21119 ** try to process it.
21121 process_sqliterc(&data,zInitFile);
21123 /* Make a second pass through the command-line argument and set
21124 ** options. This second pass is delayed until after the initialization
21125 ** file is processed so that the command-line arguments will override
21126 ** settings in the initialization file.
21128 for(i=1; i<argc; i++){
21130 if( z[0]!='-' ) continue;
21131 if( z[1]=='-' ){ z++; }
21132 if( strcmp(z,"-init")==0 ){
21134 }else if( strcmp(z,"-html")==0 ){
21135 data.mode = MODE_Html;
21136 }else if( strcmp(z,"-list")==0 ){
21137 data.mode = MODE_List;
21138 }else if( strcmp(z,"-quote")==0 ){
21139 data.mode = MODE_Quote;
21140 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Comma);
21141 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21142 }else if( strcmp(z,"-line")==0 ){
21143 data.mode = MODE_Line;
21144 }else if( strcmp(z,"-column")==0 ){
21145 data.mode = MODE_Column;
21146 }else if( strcmp(z,"-json")==0 ){
21147 data.mode = MODE_Json;
21148 }else if( strcmp(z,"-markdown")==0 ){
21149 data.mode = MODE_Markdown;
21150 }else if( strcmp(z,"-table")==0 ){
21151 data.mode = MODE_Table;
21152 }else if( strcmp(z,"-box")==0 ){
21153 data.mode = MODE_Box;
21154 }else if( strcmp(z,"-csv")==0 ){
21155 data.mode = MODE_Csv;
21156 memcpy(data.colSeparator,",",2);
21157 #ifdef SQLITE_HAVE_ZLIB
21158 }else if( strcmp(z,"-zip")==0 ){
21159 data.openMode = SHELL_OPEN_ZIPFILE;
21161 }else if( strcmp(z,"-append")==0 ){
21162 data.openMode = SHELL_OPEN_APPENDVFS;
21163 #ifdef SQLITE_ENABLE_DESERIALIZE
21164 }else if( strcmp(z,"-deserialize")==0 ){
21165 data.openMode = SHELL_OPEN_DESERIALIZE;
21166 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
21167 data.szMax = integerValue(argv[++i]);
21169 }else if( strcmp(z,"-readonly")==0 ){
21170 data.openMode = SHELL_OPEN_READONLY;
21171 }else if( strcmp(z,"-nofollow")==0 ){
21172 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
21173 }else if( strcmp(z,"-ascii")==0 ){
21174 data.mode = MODE_Ascii;
21175 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Unit);
21176 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Record);
21177 }else if( strcmp(z,"-tabs")==0 ){
21178 data.mode = MODE_List;
21179 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator, SEP_Tab);
21180 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator, SEP_Row);
21181 }else if( strcmp(z,"-separator")==0 ){
21182 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
21183 "%s",cmdline_option_value(argc,argv,++i));
21184 }else if( strcmp(z,"-newline")==0 ){
21185 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
21186 "%s",cmdline_option_value(argc,argv,++i));
21187 }else if( strcmp(z,"-nullvalue")==0 ){
21188 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
21189 "%s",cmdline_option_value(argc,argv,++i));
21190 }else if( strcmp(z,"-header")==0 ){
21191 data.showHeader = 1;
21192 }else if( strcmp(z,"-noheader")==0 ){
21193 data.showHeader = 0;
21194 }else if( strcmp(z,"-echo")==0 ){
21195 ShellSetFlag(&data, SHFLG_Echo);
21196 }else if( strcmp(z,"-eqp")==0 ){
21197 data.autoEQP = AUTOEQP_on;
21198 }else if( strcmp(z,"-eqpfull")==0 ){
21199 data.autoEQP = AUTOEQP_full;
21200 }else if( strcmp(z,"-stats")==0 ){
21202 }else if( strcmp(z,"-scanstats")==0 ){
21203 data.scanstatsOn = 1;
21204 }else if( strcmp(z,"-backslash")==0 ){
21205 /* Undocumented command-line option: -backslash
21206 ** Causes C-style backslash escapes to be evaluated in SQL statements
21207 ** prior to sending the SQL into SQLite. Useful for injecting
21208 ** crazy bytes in the middle of SQL statements for testing and debugging.
21210 ShellSetFlag(&data, SHFLG_Backslash);
21211 }else if( strcmp(z,"-bail")==0 ){
21212 /* No-op. The bail_on_error flag should already be set. */
21213 }else if( strcmp(z,"-version")==0 ){
21214 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
21216 }else if( strcmp(z,"-interactive")==0 ){
21217 stdin_is_interactive = 1;
21218 }else if( strcmp(z,"-batch")==0 ){
21219 stdin_is_interactive = 0;
21220 }else if( strcmp(z,"-heap")==0 ){
21222 }else if( strcmp(z,"-pagecache")==0 ){
21224 }else if( strcmp(z,"-lookaside")==0 ){
21226 }else if( strcmp(z,"-mmap")==0 ){
21228 }else if( strcmp(z,"-memtrace")==0 ){
21230 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
21231 }else if( strcmp(z,"-sorterref")==0 ){
21234 }else if( strcmp(z,"-vfs")==0 ){
21236 #ifdef SQLITE_ENABLE_VFSTRACE
21237 }else if( strcmp(z,"-vfstrace")==0 ){
21240 #ifdef SQLITE_ENABLE_MULTIPLEX
21241 }else if( strcmp(z,"-multiplex")==0 ){
21244 }else if( strcmp(z,"-help")==0 ){
21246 }else if( strcmp(z,"-cmd")==0 ){
21247 /* Run commands that follow -cmd first and separately from commands
21248 ** that simply appear on the command-line. This seems goofy. It would
21249 ** be better if all commands ran in the order that they appear. But
21250 ** we retain the goofy behavior for historical compatibility. */
21251 if( i==argc-1 ) break;
21252 z = cmdline_option_value(argc,argv,++i);
21254 rc = do_meta_command(z, &data);
21255 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
21258 rc = shell_exec(&data, z, &zErrMsg);
21260 utf8_printf(stderr,"Error: %s\n", zErrMsg);
21261 if( bail_on_error ) return rc!=0 ? rc : 1;
21263 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
21264 if( bail_on_error ) return rc;
21267 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
21268 }else if( strncmp(z, "-A", 2)==0 ){
21270 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
21271 " with \"%s\"\n", z);
21274 open_db(&data, OPEN_DB_ZIPFILE);
21277 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
21279 arDotCommand(&data, 1, argv+i, argc-i);
21285 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
21286 raw_printf(stderr,"Use -help for a list of options.\n");
21289 data.cMode = data.mode;
21293 /* Run all arguments that do not begin with '-' as if they were separate
21294 ** command-line inputs, except for the argToSkip argument which contains
21295 ** the database filename.
21297 for(i=0; i<nCmd; i++){
21298 if( azCmd[i][0]=='.' ){
21299 rc = do_meta_command(azCmd[i], &data);
21302 return rc==2 ? 0 : rc;
21306 rc = shell_exec(&data, azCmd[i], &zErrMsg);
21307 if( zErrMsg || rc ){
21309 utf8_printf(stderr,"Error: %s\n", zErrMsg);
21311 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
21313 sqlite3_free(zErrMsg);
21315 return rc!=0 ? rc : 1;
21320 /* Run commands received from standard input
21322 if( stdin_is_interactive ){
21327 "SQLite version %s %.19s\n" /*extra-version-info*/
21328 "Enter \".help\" for usage hints.\n",
21329 sqlite3_libversion(), sqlite3_sourceid()
21331 if( warnInmemoryDb ){
21332 printf("Connected to a ");
21333 printBold("transient in-memory database");
21334 printf(".\nUse \".open FILENAME\" to reopen on a "
21335 "persistent database.\n");
21337 zHistory = getenv("SQLITE_HISTORY");
21339 zHistory = strdup(zHistory);
21340 }else if( (zHome = find_home_dir(0))!=0 ){
21341 nHistory = strlen30(zHome) + 20;
21342 if( (zHistory = malloc(nHistory))!=0 ){
21343 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
21346 if( zHistory ){ shell_read_history(zHistory); }
21347 #if HAVE_READLINE || HAVE_EDITLINE
21348 rl_attempted_completion_function = readline_completion;
21349 #elif HAVE_LINENOISE
21350 linenoiseSetCompletionCallback(linenoise_completion);
21353 rc = process_input(&data);
21355 shell_stifle_history(2000);
21356 shell_write_history(zHistory);
21361 rc = process_input(&data);
21365 set_table_name(&data, 0);
21367 session_close_all(&data);
21370 sqlite3_free(data.zFreeOnClose);
21372 output_reset(&data);
21373 data.doXdgOpen = 0;
21374 clearTempFile(&data);
21375 #if !SQLITE_SHELL_IS_UTF8
21376 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
21379 free(data.colWidth);
21380 /* Clear the global data structure so that valgrind will detect memory
21382 memset(&data, 0, sizeof(data));