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;
575 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
576 for(i=n=0; zUtf[i]; i++){
577 if( (zUtf[i]&0xc0)!=0x80 ){
580 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
586 utf8_printf(pOut, "%.*s", i, zUtf);
588 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
590 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
596 ** Determines if a string is a number of not.
598 static int isNumber(const char *z, int *realnum){
599 if( *z=='-' || *z=='+' ) z++;
604 if( realnum ) *realnum = 0;
605 while( IsDigit(*z) ){ z++; }
608 if( !IsDigit(*z) ) return 0;
609 while( IsDigit(*z) ){ z++; }
610 if( realnum ) *realnum = 1;
612 if( *z=='e' || *z=='E' ){
614 if( *z=='+' || *z=='-' ) z++;
615 if( !IsDigit(*z) ) return 0;
616 while( IsDigit(*z) ){ z++; }
617 if( realnum ) *realnum = 1;
623 ** Compute a string length that is limited to what can be stored in
624 ** lower 30 bits of a 32-bit signed integer.
626 static int strlen30(const char *z){
628 while( *z2 ){ z2++; }
629 return 0x3fffffff & (int)(z2 - z);
633 ** Return the length of a string in characters. Multibyte UTF8 characters
634 ** count as a single character.
636 static int strlenChar(const char *z){
639 if( (0xc0&*(z++))!=0x80 ) n++;
645 ** This routine reads a line of text from FILE in, stores
646 ** the text in memory obtained from malloc() and returns a pointer
647 ** to the text. NULL is returned at end of file, or if malloc()
650 ** If zLine is not NULL then it is a malloced buffer returned from
651 ** a previous call to this routine that may be reused.
653 static char *local_getline(char *zLine, FILE *in){
654 int nLine = zLine==0 ? 0 : 100;
659 nLine = nLine*2 + 100;
660 zLine = realloc(zLine, nLine);
661 if( zLine==0 ) shell_out_of_memory();
663 if( fgets(&zLine[n], nLine - n, in)==0 ){
671 while( zLine[n] ) n++;
672 if( n>0 && zLine[n-1]=='\n' ){
674 if( n>0 && zLine[n-1]=='\r' ) n--;
679 #if defined(_WIN32) || defined(WIN32)
680 /* For interactive input on Windows systems, translate the
681 ** multi-byte characterset characters into UTF-8. */
682 if( stdin_is_interactive && in==stdin ){
683 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
685 int nTrans = strlen30(zTrans)+1;
687 zLine = realloc(zLine, nTrans);
688 if( zLine==0 ) shell_out_of_memory();
690 memcpy(zLine, zTrans, nTrans);
691 sqlite3_free(zTrans);
694 #endif /* defined(_WIN32) || defined(WIN32) */
699 ** Retrieve a single line of input text.
701 ** If in==0 then read from standard input and prompt before each line.
702 ** If isContinuation is true, then a continuation prompt is appropriate.
703 ** If isContinuation is zero, then the main prompt should be used.
705 ** If zPrior is not NULL then it is a buffer from a prior call to this
706 ** routine that can be reused.
708 ** The result is stored in space obtained from malloc() and must either
709 ** be freed by the caller or else passed back into this routine via the
710 ** zPrior argument for reuse.
712 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
716 zResult = local_getline(zPrior, in);
718 zPrompt = isContinuation ? continuePrompt : mainPrompt;
719 #if SHELL_USE_LOCAL_GETLINE
720 printf("%s", zPrompt);
722 zResult = local_getline(zPrior, stdin);
725 zResult = shell_readline(zPrompt);
726 if( zResult && *zResult ) shell_add_history(zResult);
734 ** Return the value of a hexadecimal digit. Return -1 if the input
735 ** is not a hex digit.
737 static int hexDigitValue(char c){
738 if( c>='0' && c<='9' ) return c - '0';
739 if( c>='a' && c<='f' ) return c - 'a' + 10;
740 if( c>='A' && c<='F' ) return c - 'A' + 10;
745 ** Interpret zArg as an integer value, possibly with suffixes.
747 static sqlite3_int64 integerValue(const char *zArg){
749 static const struct { char *zSuffix; int iMult; } aMult[] = {
751 { "MiB", 1024*1024 },
752 { "GiB", 1024*1024*1024 },
755 { "GB", 1000000000 },
765 }else if( zArg[0]=='+' ){
768 if( zArg[0]=='0' && zArg[1]=='x' ){
771 while( (x = hexDigitValue(zArg[0]))>=0 ){
776 while( IsDigit(zArg[0]) ){
777 v = v*10 + zArg[0] - '0';
781 for(i=0; i<ArraySize(aMult); i++){
782 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
787 return isNeg? -v : v;
791 ** A variable length string to which one can append text.
793 typedef struct ShellText ShellText;
801 ** Initialize and destroy a ShellText object
803 static void initText(ShellText *p){
804 memset(p, 0, sizeof(*p));
806 static void freeText(ShellText *p){
811 /* zIn is either a pointer to a NULL-terminated string in memory obtained
812 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
813 ** added to zIn, and the result returned in memory obtained from malloc().
814 ** zIn, if it was not NULL, is freed.
816 ** If the third argument, quote, is not '\0', then it is used as a
817 ** quote character for zAppend.
819 static void appendText(ShellText *p, char const *zAppend, char quote){
822 int nAppend = strlen30(zAppend);
824 len = nAppend+p->n+1;
827 for(i=0; i<nAppend; i++){
828 if( zAppend[i]==quote ) len++;
832 if( p->n+len>=p->nAlloc ){
833 p->nAlloc = p->nAlloc*2 + len + 20;
834 p->z = realloc(p->z, p->nAlloc);
835 if( p->z==0 ) shell_out_of_memory();
839 char *zCsr = p->z+p->n;
841 for(i=0; i<nAppend; i++){
842 *zCsr++ = zAppend[i];
843 if( zAppend[i]==quote ) *zCsr++ = quote;
846 p->n = (int)(zCsr - p->z);
849 memcpy(p->z+p->n, zAppend, nAppend);
856 ** Attempt to determine if identifier zName needs to be quoted, either
857 ** because it contains non-alphanumeric characters, or because it is an
858 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
859 ** that quoting is required.
861 ** Return '"' if quoting is required. Return 0 if no quoting is required.
863 static char quoteChar(const char *zName){
865 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
866 for(i=0; zName[i]; i++){
867 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
869 return sqlite3_keyword_check(zName, i) ? '"' : 0;
873 ** Construct a fake object name and column list to describe the structure
874 ** of the view, virtual table, or table valued function zSchema.zName.
876 static char *shellFakeSchema(
877 sqlite3 *db, /* The database connection containing the vtab */
878 const char *zSchema, /* Schema of the database holding the vtab */
879 const char *zName /* The name of the virtual table */
881 sqlite3_stmt *pStmt = 0;
888 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
889 zSchema ? zSchema : "main", zName);
890 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
894 cQuote = quoteChar(zSchema);
895 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
896 appendText(&s, zSchema, cQuote);
897 appendText(&s, ".", 0);
899 cQuote = quoteChar(zName);
900 appendText(&s, zName, cQuote);
901 while( sqlite3_step(pStmt)==SQLITE_ROW ){
902 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
904 appendText(&s, zDiv, 0);
906 cQuote = quoteChar(zCol);
907 appendText(&s, zCol, cQuote);
909 appendText(&s, ")", 0);
910 sqlite3_finalize(pStmt);
919 ** SQL function: shell_module_schema(X)
921 ** Return a fake schema for the table-valued function or eponymous virtual
924 static void shellModuleSchema(
925 sqlite3_context *pCtx,
927 sqlite3_value **apVal
929 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
930 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
931 UNUSED_PARAMETER(nVal);
933 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
940 ** SQL function: shell_add_schema(S,X)
942 ** Add the schema name X to the CREATE statement in S and return the result.
945 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
950 ** CREATE UNIQUE INDEX
953 ** CREATE VIRTUAL TABLE
955 ** This UDF is used by the .schema command to insert the schema name of
956 ** attached databases into the middle of the sqlite_master.sql field.
958 static void shellAddSchemaName(
959 sqlite3_context *pCtx,
961 sqlite3_value **apVal
963 static const char *aPrefix[] = {
972 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
973 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
974 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
975 sqlite3 *db = sqlite3_context_db_handle(pCtx);
976 UNUSED_PARAMETER(nVal);
977 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
978 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
979 int n = strlen30(aPrefix[i]);
980 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
984 char cQuote = quoteChar(zSchema);
985 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
986 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
988 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
992 && aPrefix[i][0]=='V'
993 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
996 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
998 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
1003 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
1009 sqlite3_result_value(pCtx, apVal[0]);
1013 ** The source code for several run-time loadable extensions is inserted
1014 ** below by the ../tool/mkshellc.tcl script. Before processing that included
1015 ** code, we need to override some macros to make the included program code
1016 ** work here in the middle of this regular program.
1018 #define SQLITE_EXTENSION_INIT1
1019 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
1021 #if defined(_WIN32) && defined(_MSC_VER)
1022 /************************* Begin test_windirent.h ******************/
1026 ** The author disclaims copyright to this source code. In place of
1027 ** a legal notice, here is a blessing:
1029 ** May you do good and not evil.
1030 ** May you find forgiveness for yourself and forgive others.
1031 ** May you share freely, never taking more than you give.
1033 *************************************************************************
1034 ** This file contains declarations for most of the opendir() family of
1035 ** POSIX functions on Win32 using the MSVCRT.
1038 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
1039 #define SQLITE_WINDIRENT_H
1042 ** We need several data types from the Windows SDK header.
1045 #ifndef WIN32_LEAN_AND_MEAN
1046 #define WIN32_LEAN_AND_MEAN
1049 #include "windows.h"
1052 ** We need several support functions from the SQLite core.
1055 /* #include "sqlite3.h" */
1058 ** We need several things from the ANSI and MSVCRT headers.
1066 #include <sys/types.h>
1067 #include <sys/stat.h>
1070 ** We may need several defines that should have been in "sys/stat.h".
1074 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1078 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1082 #define S_ISLNK(mode) (0)
1086 ** We may need to provide the "mode_t" type.
1089 #ifndef MODE_T_DEFINED
1090 #define MODE_T_DEFINED
1091 typedef unsigned short mode_t;
1095 ** We may need to provide the "ino_t" type.
1098 #ifndef INO_T_DEFINED
1099 #define INO_T_DEFINED
1100 typedef unsigned short ino_t;
1104 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1108 # ifdef FILENAME_MAX
1109 # define NAME_MAX (FILENAME_MAX)
1111 # define NAME_MAX (260)
1116 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1119 #ifndef NULL_INTPTR_T
1120 # define NULL_INTPTR_T ((intptr_t)(0))
1123 #ifndef BAD_INTPTR_T
1124 # define BAD_INTPTR_T ((intptr_t)(-1))
1128 ** We need to provide the necessary structures and related types.
1131 #ifndef DIRENT_DEFINED
1132 #define DIRENT_DEFINED
1133 typedef struct DIRENT DIRENT;
1134 typedef DIRENT *LPDIRENT;
1136 ino_t d_ino; /* Sequence number, do not use. */
1137 unsigned d_attributes; /* Win32 file attributes. */
1138 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1144 typedef struct DIR DIR;
1147 intptr_t d_handle; /* Value returned by "_findfirst". */
1148 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1149 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1154 ** Provide a macro, for use by the implementation, to determine if a
1155 ** particular directory entry should be skipped over when searching for
1156 ** the next directory entry that should be returned by the readdir() or
1157 ** readdir_r() functions.
1161 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1165 ** Provide the function prototype for the POSIX compatiable getenv()
1166 ** function. This function is not thread-safe.
1169 extern const char *windirent_getenv(const char *name);
1172 ** Finally, we can provide the function prototypes for the opendir(),
1173 ** readdir(), readdir_r(), and closedir() POSIX functions.
1176 extern LPDIR opendir(const char *dirname);
1177 extern LPDIRENT readdir(LPDIR dirp);
1178 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1179 extern INT closedir(LPDIR dirp);
1181 #endif /* defined(WIN32) && defined(_MSC_VER) */
1183 /************************* End test_windirent.h ********************/
1184 /************************* Begin test_windirent.c ******************/
1188 ** The author disclaims copyright to this source code. In place of
1189 ** a legal notice, here is a blessing:
1191 ** May you do good and not evil.
1192 ** May you find forgiveness for yourself and forgive others.
1193 ** May you share freely, never taking more than you give.
1195 *************************************************************************
1196 ** This file contains code to implement most of the opendir() family of
1197 ** POSIX functions on Win32 using the MSVCRT.
1200 #if defined(_WIN32) && defined(_MSC_VER)
1201 /* #include "test_windirent.h" */
1204 ** Implementation of the POSIX getenv() function using the Win32 API.
1205 ** This function is not thread-safe.
1207 const char *windirent_getenv(
1210 static char value[32768]; /* Maximum length, per MSDN */
1211 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1212 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1214 memset(value, 0, sizeof(value));
1215 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1216 if( dwRet==0 || dwRet>dwSize ){
1218 ** The function call to GetEnvironmentVariableA() failed -OR-
1219 ** the buffer is not large enough. Either way, return NULL.
1224 ** The function call to GetEnvironmentVariableA() succeeded
1225 ** -AND- the buffer contains the entire value.
1232 ** Implementation of the POSIX opendir() function using the MSVCRT.
1237 struct _finddata_t data;
1238 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1239 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1241 if( dirp==NULL ) return NULL;
1242 memset(dirp, 0, sizeof(DIR));
1244 /* TODO: Remove this if Unix-style root paths are not used. */
1245 if( sqlite3_stricmp(dirname, "/")==0 ){
1246 dirname = windirent_getenv("SystemDrive");
1249 memset(&data, 0, sizeof(struct _finddata_t));
1250 _snprintf(data.name, namesize, "%s\\*", dirname);
1251 dirp->d_handle = _findfirst(data.name, &data);
1253 if( dirp->d_handle==BAD_INTPTR_T ){
1258 /* TODO: Remove this block to allow hidden and/or system files. */
1259 if( is_filtered(data) ){
1262 memset(&data, 0, sizeof(struct _finddata_t));
1263 if( _findnext(dirp->d_handle, &data)==-1 ){
1268 /* TODO: Remove this block to allow hidden and/or system files. */
1269 if( is_filtered(data) ) goto next;
1272 dirp->d_first.d_attributes = data.attrib;
1273 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1274 dirp->d_first.d_name[NAME_MAX] = '\0';
1280 ** Implementation of the POSIX readdir() function using the MSVCRT.
1285 struct _finddata_t data;
1287 if( dirp==NULL ) return NULL;
1289 if( dirp->d_first.d_ino==0 ){
1290 dirp->d_first.d_ino++;
1291 dirp->d_next.d_ino++;
1293 return &dirp->d_first;
1298 memset(&data, 0, sizeof(struct _finddata_t));
1299 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1301 /* TODO: Remove this block to allow hidden and/or system files. */
1302 if( is_filtered(data) ) goto next;
1304 dirp->d_next.d_ino++;
1305 dirp->d_next.d_attributes = data.attrib;
1306 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1307 dirp->d_next.d_name[NAME_MAX] = '\0';
1309 return &dirp->d_next;
1313 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1320 struct _finddata_t data;
1322 if( dirp==NULL ) return EBADF;
1324 if( dirp->d_first.d_ino==0 ){
1325 dirp->d_first.d_ino++;
1326 dirp->d_next.d_ino++;
1328 entry->d_ino = dirp->d_first.d_ino;
1329 entry->d_attributes = dirp->d_first.d_attributes;
1330 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1331 entry->d_name[NAME_MAX] = '\0';
1339 memset(&data, 0, sizeof(struct _finddata_t));
1340 if( _findnext(dirp->d_handle, &data)==-1 ){
1345 /* TODO: Remove this block to allow hidden and/or system files. */
1346 if( is_filtered(data) ) goto next;
1348 entry->d_ino = (ino_t)-1; /* not available */
1349 entry->d_attributes = data.attrib;
1350 strncpy(entry->d_name, data.name, NAME_MAX);
1351 entry->d_name[NAME_MAX] = '\0';
1358 ** Implementation of the POSIX closedir() function using the MSVCRT.
1365 if( dirp==NULL ) return EINVAL;
1367 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1368 result = _findclose(dirp->d_handle);
1375 #endif /* defined(WIN32) && defined(_MSC_VER) */
1377 /************************* End test_windirent.c ********************/
1378 #define dirent DIRENT
1380 /************************* Begin ../ext/misc/shathree.c ******************/
1384 ** The author disclaims copyright to this source code. In place of
1385 ** a legal notice, here is a blessing:
1387 ** May you do good and not evil.
1388 ** May you find forgiveness for yourself and forgive others.
1389 ** May you share freely, never taking more than you give.
1391 ******************************************************************************
1393 ** This SQLite extension implements functions that compute SHA3 hashes.
1394 ** Two SQL functions are implemented:
1397 ** sha3_query(Y,SIZE)
1399 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1402 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1403 ** and returns a hash of their results.
1405 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1406 ** is used. If SIZE is included it must be one of the integers 224, 256,
1407 ** 384, or 512, to determine SHA3 hash variant that is computed.
1409 /* #include "sqlite3ext.h" */
1410 SQLITE_EXTENSION_INIT1
1414 /* typedef sqlite3_uint64 u64; */
1416 /******************************************************************************
1420 ** Macros to determine whether the machine is big or little endian,
1421 ** and whether or not that determination is run-time or compile-time.
1423 ** For best performance, an attempt is made to guess at the byte-order
1424 ** using C-preprocessor macros. If that is unsuccessful, or if
1425 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1428 #ifndef SHA3_BYTEORDER
1429 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1430 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1431 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1433 # define SHA3_BYTEORDER 1234
1434 # elif defined(sparc) || defined(__ppc__)
1435 # define SHA3_BYTEORDER 4321
1437 # define SHA3_BYTEORDER 0
1443 ** State structure for a SHA3 hash in progress
1445 typedef struct SHA3Context SHA3Context;
1446 struct SHA3Context {
1448 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1449 unsigned char x[1600]; /* ... or 1600 bytes */
1451 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1452 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1453 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1457 ** A single step of the Keccak mixing function for a 1600-bit state
1459 static void KeccakF1600Step(SHA3Context *p){
1461 u64 b0, b1, b2, b3, b4;
1462 u64 c0, c1, c2, c3, c4;
1463 u64 d0, d1, d2, d3, d4;
1464 static const u64 RC[] = {
1465 0x0000000000000001ULL, 0x0000000000008082ULL,
1466 0x800000000000808aULL, 0x8000000080008000ULL,
1467 0x000000000000808bULL, 0x0000000080000001ULL,
1468 0x8000000080008081ULL, 0x8000000000008009ULL,
1469 0x000000000000008aULL, 0x0000000000000088ULL,
1470 0x0000000080008009ULL, 0x000000008000000aULL,
1471 0x000000008000808bULL, 0x800000000000008bULL,
1472 0x8000000000008089ULL, 0x8000000000008003ULL,
1473 0x8000000000008002ULL, 0x8000000000000080ULL,
1474 0x000000000000800aULL, 0x800000008000000aULL,
1475 0x8000000080008081ULL, 0x8000000000008080ULL,
1476 0x0000000080000001ULL, 0x8000000080008008ULL
1478 # define a00 (p->u.s[0])
1479 # define a01 (p->u.s[1])
1480 # define a02 (p->u.s[2])
1481 # define a03 (p->u.s[3])
1482 # define a04 (p->u.s[4])
1483 # define a10 (p->u.s[5])
1484 # define a11 (p->u.s[6])
1485 # define a12 (p->u.s[7])
1486 # define a13 (p->u.s[8])
1487 # define a14 (p->u.s[9])
1488 # define a20 (p->u.s[10])
1489 # define a21 (p->u.s[11])
1490 # define a22 (p->u.s[12])
1491 # define a23 (p->u.s[13])
1492 # define a24 (p->u.s[14])
1493 # define a30 (p->u.s[15])
1494 # define a31 (p->u.s[16])
1495 # define a32 (p->u.s[17])
1496 # define a33 (p->u.s[18])
1497 # define a34 (p->u.s[19])
1498 # define a40 (p->u.s[20])
1499 # define a41 (p->u.s[21])
1500 # define a42 (p->u.s[22])
1501 # define a43 (p->u.s[23])
1502 # define a44 (p->u.s[24])
1503 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1505 for(i=0; i<24; i+=4){
1506 c0 = a00^a10^a20^a30^a40;
1507 c1 = a01^a11^a21^a31^a41;
1508 c2 = a02^a12^a22^a32^a42;
1509 c3 = a03^a13^a23^a33^a43;
1510 c4 = a04^a14^a24^a34^a44;
1511 d0 = c4^ROL64(c1, 1);
1512 d1 = c0^ROL64(c2, 1);
1513 d2 = c1^ROL64(c3, 1);
1514 d3 = c2^ROL64(c4, 1);
1515 d4 = c3^ROL64(c0, 1);
1518 b1 = ROL64((a11^d1), 44);
1519 b2 = ROL64((a22^d2), 43);
1520 b3 = ROL64((a33^d3), 21);
1521 b4 = ROL64((a44^d4), 14);
1522 a00 = b0 ^((~b1)& b2 );
1524 a11 = b1 ^((~b2)& b3 );
1525 a22 = b2 ^((~b3)& b4 );
1526 a33 = b3 ^((~b4)& b0 );
1527 a44 = b4 ^((~b0)& b1 );
1529 b2 = ROL64((a20^d0), 3);
1530 b3 = ROL64((a31^d1), 45);
1531 b4 = ROL64((a42^d2), 61);
1532 b0 = ROL64((a03^d3), 28);
1533 b1 = ROL64((a14^d4), 20);
1534 a20 = b0 ^((~b1)& b2 );
1535 a31 = b1 ^((~b2)& b3 );
1536 a42 = b2 ^((~b3)& b4 );
1537 a03 = b3 ^((~b4)& b0 );
1538 a14 = b4 ^((~b0)& b1 );
1540 b4 = ROL64((a40^d0), 18);
1541 b0 = ROL64((a01^d1), 1);
1542 b1 = ROL64((a12^d2), 6);
1543 b2 = ROL64((a23^d3), 25);
1544 b3 = ROL64((a34^d4), 8);
1545 a40 = b0 ^((~b1)& b2 );
1546 a01 = b1 ^((~b2)& b3 );
1547 a12 = b2 ^((~b3)& b4 );
1548 a23 = b3 ^((~b4)& b0 );
1549 a34 = b4 ^((~b0)& b1 );
1551 b1 = ROL64((a10^d0), 36);
1552 b2 = ROL64((a21^d1), 10);
1553 b3 = ROL64((a32^d2), 15);
1554 b4 = ROL64((a43^d3), 56);
1555 b0 = ROL64((a04^d4), 27);
1556 a10 = b0 ^((~b1)& b2 );
1557 a21 = b1 ^((~b2)& b3 );
1558 a32 = b2 ^((~b3)& b4 );
1559 a43 = b3 ^((~b4)& b0 );
1560 a04 = b4 ^((~b0)& b1 );
1562 b3 = ROL64((a30^d0), 41);
1563 b4 = ROL64((a41^d1), 2);
1564 b0 = ROL64((a02^d2), 62);
1565 b1 = ROL64((a13^d3), 55);
1566 b2 = ROL64((a24^d4), 39);
1567 a30 = b0 ^((~b1)& b2 );
1568 a41 = b1 ^((~b2)& b3 );
1569 a02 = b2 ^((~b3)& b4 );
1570 a13 = b3 ^((~b4)& b0 );
1571 a24 = b4 ^((~b0)& b1 );
1573 c0 = a00^a20^a40^a10^a30;
1574 c1 = a11^a31^a01^a21^a41;
1575 c2 = a22^a42^a12^a32^a02;
1576 c3 = a33^a03^a23^a43^a13;
1577 c4 = a44^a14^a34^a04^a24;
1578 d0 = c4^ROL64(c1, 1);
1579 d1 = c0^ROL64(c2, 1);
1580 d2 = c1^ROL64(c3, 1);
1581 d3 = c2^ROL64(c4, 1);
1582 d4 = c3^ROL64(c0, 1);
1585 b1 = ROL64((a31^d1), 44);
1586 b2 = ROL64((a12^d2), 43);
1587 b3 = ROL64((a43^d3), 21);
1588 b4 = ROL64((a24^d4), 14);
1589 a00 = b0 ^((~b1)& b2 );
1591 a31 = b1 ^((~b2)& b3 );
1592 a12 = b2 ^((~b3)& b4 );
1593 a43 = b3 ^((~b4)& b0 );
1594 a24 = b4 ^((~b0)& b1 );
1596 b2 = ROL64((a40^d0), 3);
1597 b3 = ROL64((a21^d1), 45);
1598 b4 = ROL64((a02^d2), 61);
1599 b0 = ROL64((a33^d3), 28);
1600 b1 = ROL64((a14^d4), 20);
1601 a40 = b0 ^((~b1)& b2 );
1602 a21 = b1 ^((~b2)& b3 );
1603 a02 = b2 ^((~b3)& b4 );
1604 a33 = b3 ^((~b4)& b0 );
1605 a14 = b4 ^((~b0)& b1 );
1607 b4 = ROL64((a30^d0), 18);
1608 b0 = ROL64((a11^d1), 1);
1609 b1 = ROL64((a42^d2), 6);
1610 b2 = ROL64((a23^d3), 25);
1611 b3 = ROL64((a04^d4), 8);
1612 a30 = b0 ^((~b1)& b2 );
1613 a11 = b1 ^((~b2)& b3 );
1614 a42 = b2 ^((~b3)& b4 );
1615 a23 = b3 ^((~b4)& b0 );
1616 a04 = b4 ^((~b0)& b1 );
1618 b1 = ROL64((a20^d0), 36);
1619 b2 = ROL64((a01^d1), 10);
1620 b3 = ROL64((a32^d2), 15);
1621 b4 = ROL64((a13^d3), 56);
1622 b0 = ROL64((a44^d4), 27);
1623 a20 = b0 ^((~b1)& b2 );
1624 a01 = b1 ^((~b2)& b3 );
1625 a32 = b2 ^((~b3)& b4 );
1626 a13 = b3 ^((~b4)& b0 );
1627 a44 = b4 ^((~b0)& b1 );
1629 b3 = ROL64((a10^d0), 41);
1630 b4 = ROL64((a41^d1), 2);
1631 b0 = ROL64((a22^d2), 62);
1632 b1 = ROL64((a03^d3), 55);
1633 b2 = ROL64((a34^d4), 39);
1634 a10 = b0 ^((~b1)& b2 );
1635 a41 = b1 ^((~b2)& b3 );
1636 a22 = b2 ^((~b3)& b4 );
1637 a03 = b3 ^((~b4)& b0 );
1638 a34 = b4 ^((~b0)& b1 );
1640 c0 = a00^a40^a30^a20^a10;
1641 c1 = a31^a21^a11^a01^a41;
1642 c2 = a12^a02^a42^a32^a22;
1643 c3 = a43^a33^a23^a13^a03;
1644 c4 = a24^a14^a04^a44^a34;
1645 d0 = c4^ROL64(c1, 1);
1646 d1 = c0^ROL64(c2, 1);
1647 d2 = c1^ROL64(c3, 1);
1648 d3 = c2^ROL64(c4, 1);
1649 d4 = c3^ROL64(c0, 1);
1652 b1 = ROL64((a21^d1), 44);
1653 b2 = ROL64((a42^d2), 43);
1654 b3 = ROL64((a13^d3), 21);
1655 b4 = ROL64((a34^d4), 14);
1656 a00 = b0 ^((~b1)& b2 );
1658 a21 = b1 ^((~b2)& b3 );
1659 a42 = b2 ^((~b3)& b4 );
1660 a13 = b3 ^((~b4)& b0 );
1661 a34 = b4 ^((~b0)& b1 );
1663 b2 = ROL64((a30^d0), 3);
1664 b3 = ROL64((a01^d1), 45);
1665 b4 = ROL64((a22^d2), 61);
1666 b0 = ROL64((a43^d3), 28);
1667 b1 = ROL64((a14^d4), 20);
1668 a30 = b0 ^((~b1)& b2 );
1669 a01 = b1 ^((~b2)& b3 );
1670 a22 = b2 ^((~b3)& b4 );
1671 a43 = b3 ^((~b4)& b0 );
1672 a14 = b4 ^((~b0)& b1 );
1674 b4 = ROL64((a10^d0), 18);
1675 b0 = ROL64((a31^d1), 1);
1676 b1 = ROL64((a02^d2), 6);
1677 b2 = ROL64((a23^d3), 25);
1678 b3 = ROL64((a44^d4), 8);
1679 a10 = b0 ^((~b1)& b2 );
1680 a31 = b1 ^((~b2)& b3 );
1681 a02 = b2 ^((~b3)& b4 );
1682 a23 = b3 ^((~b4)& b0 );
1683 a44 = b4 ^((~b0)& b1 );
1685 b1 = ROL64((a40^d0), 36);
1686 b2 = ROL64((a11^d1), 10);
1687 b3 = ROL64((a32^d2), 15);
1688 b4 = ROL64((a03^d3), 56);
1689 b0 = ROL64((a24^d4), 27);
1690 a40 = b0 ^((~b1)& b2 );
1691 a11 = b1 ^((~b2)& b3 );
1692 a32 = b2 ^((~b3)& b4 );
1693 a03 = b3 ^((~b4)& b0 );
1694 a24 = b4 ^((~b0)& b1 );
1696 b3 = ROL64((a20^d0), 41);
1697 b4 = ROL64((a41^d1), 2);
1698 b0 = ROL64((a12^d2), 62);
1699 b1 = ROL64((a33^d3), 55);
1700 b2 = ROL64((a04^d4), 39);
1701 a20 = b0 ^((~b1)& b2 );
1702 a41 = b1 ^((~b2)& b3 );
1703 a12 = b2 ^((~b3)& b4 );
1704 a33 = b3 ^((~b4)& b0 );
1705 a04 = b4 ^((~b0)& b1 );
1707 c0 = a00^a30^a10^a40^a20;
1708 c1 = a21^a01^a31^a11^a41;
1709 c2 = a42^a22^a02^a32^a12;
1710 c3 = a13^a43^a23^a03^a33;
1711 c4 = a34^a14^a44^a24^a04;
1712 d0 = c4^ROL64(c1, 1);
1713 d1 = c0^ROL64(c2, 1);
1714 d2 = c1^ROL64(c3, 1);
1715 d3 = c2^ROL64(c4, 1);
1716 d4 = c3^ROL64(c0, 1);
1719 b1 = ROL64((a01^d1), 44);
1720 b2 = ROL64((a02^d2), 43);
1721 b3 = ROL64((a03^d3), 21);
1722 b4 = ROL64((a04^d4), 14);
1723 a00 = b0 ^((~b1)& b2 );
1725 a01 = b1 ^((~b2)& b3 );
1726 a02 = b2 ^((~b3)& b4 );
1727 a03 = b3 ^((~b4)& b0 );
1728 a04 = b4 ^((~b0)& b1 );
1730 b2 = ROL64((a10^d0), 3);
1731 b3 = ROL64((a11^d1), 45);
1732 b4 = ROL64((a12^d2), 61);
1733 b0 = ROL64((a13^d3), 28);
1734 b1 = ROL64((a14^d4), 20);
1735 a10 = b0 ^((~b1)& b2 );
1736 a11 = b1 ^((~b2)& b3 );
1737 a12 = b2 ^((~b3)& b4 );
1738 a13 = b3 ^((~b4)& b0 );
1739 a14 = b4 ^((~b0)& b1 );
1741 b4 = ROL64((a20^d0), 18);
1742 b0 = ROL64((a21^d1), 1);
1743 b1 = ROL64((a22^d2), 6);
1744 b2 = ROL64((a23^d3), 25);
1745 b3 = ROL64((a24^d4), 8);
1746 a20 = b0 ^((~b1)& b2 );
1747 a21 = b1 ^((~b2)& b3 );
1748 a22 = b2 ^((~b3)& b4 );
1749 a23 = b3 ^((~b4)& b0 );
1750 a24 = b4 ^((~b0)& b1 );
1752 b1 = ROL64((a30^d0), 36);
1753 b2 = ROL64((a31^d1), 10);
1754 b3 = ROL64((a32^d2), 15);
1755 b4 = ROL64((a33^d3), 56);
1756 b0 = ROL64((a34^d4), 27);
1757 a30 = b0 ^((~b1)& b2 );
1758 a31 = b1 ^((~b2)& b3 );
1759 a32 = b2 ^((~b3)& b4 );
1760 a33 = b3 ^((~b4)& b0 );
1761 a34 = b4 ^((~b0)& b1 );
1763 b3 = ROL64((a40^d0), 41);
1764 b4 = ROL64((a41^d1), 2);
1765 b0 = ROL64((a42^d2), 62);
1766 b1 = ROL64((a43^d3), 55);
1767 b2 = ROL64((a44^d4), 39);
1768 a40 = b0 ^((~b1)& b2 );
1769 a41 = b1 ^((~b2)& b3 );
1770 a42 = b2 ^((~b3)& b4 );
1771 a43 = b3 ^((~b4)& b0 );
1772 a44 = b4 ^((~b0)& b1 );
1777 ** Initialize a new hash. iSize determines the size of the hash
1778 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1779 ** can be zero to use the default hash size of 256 bits.
1781 static void SHA3Init(SHA3Context *p, int iSize){
1782 memset(p, 0, sizeof(*p));
1783 if( iSize>=128 && iSize<=512 ){
1784 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1786 p->nRate = (1600 - 2*256)/8;
1788 #if SHA3_BYTEORDER==1234
1789 /* Known to be little-endian at compile-time. No-op */
1790 #elif SHA3_BYTEORDER==4321
1791 p->ixMask = 7; /* Big-endian */
1794 static unsigned int one = 1;
1795 if( 1==*(unsigned char*)&one ){
1796 /* Little endian. No byte swapping. */
1799 /* Big endian. Byte swap. */
1807 ** Make consecutive calls to the SHA3Update function to add new content
1810 static void SHA3Update(
1812 const unsigned char *aData,
1816 #if SHA3_BYTEORDER==1234
1817 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1818 for(; i+7<nData; i+=8){
1819 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1821 if( p->nLoaded>=p->nRate ){
1828 for(; i<nData; i++){
1829 #if SHA3_BYTEORDER==1234
1830 p->u.x[p->nLoaded] ^= aData[i];
1831 #elif SHA3_BYTEORDER==4321
1832 p->u.x[p->nLoaded^0x07] ^= aData[i];
1834 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1837 if( p->nLoaded==p->nRate ){
1845 ** After all content has been added, invoke SHA3Final() to compute
1846 ** the final hash. The function returns a pointer to the binary
1849 static unsigned char *SHA3Final(SHA3Context *p){
1851 if( p->nLoaded==p->nRate-1 ){
1852 const unsigned char c1 = 0x86;
1853 SHA3Update(p, &c1, 1);
1855 const unsigned char c2 = 0x06;
1856 const unsigned char c3 = 0x80;
1857 SHA3Update(p, &c2, 1);
1858 p->nLoaded = p->nRate - 1;
1859 SHA3Update(p, &c3, 1);
1861 for(i=0; i<p->nRate; i++){
1862 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1864 return &p->u.x[p->nRate];
1866 /* End of the hashing logic
1867 *****************************************************************************/
1870 ** Implementation of the sha3(X,SIZE) function.
1872 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1873 ** size is 256. If X is a BLOB, it is hashed as is.
1874 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1875 ** and the string is hashed without the trailing 0x00 terminator. The hash
1876 ** of a NULL value is NULL.
1878 static void sha3Func(
1879 sqlite3_context *context,
1881 sqlite3_value **argv
1884 int eType = sqlite3_value_type(argv[0]);
1885 int nByte = sqlite3_value_bytes(argv[0]);
1890 iSize = sqlite3_value_int(argv[1]);
1891 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1892 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1897 if( eType==SQLITE_NULL ) return;
1898 SHA3Init(&cx, iSize);
1899 if( eType==SQLITE_BLOB ){
1900 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1902 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1904 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1907 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1908 ** of 50 bytes and add it to the hash.
1910 static void hash_step_vformat(
1911 SHA3Context *p, /* Add content to this context */
1912 const char *zFormat,
1918 va_start(ap, zFormat);
1919 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1921 n = (int)strlen(zBuf);
1922 SHA3Update(p, (unsigned char*)zBuf, n);
1926 ** Implementation of the sha3_query(SQL,SIZE) function.
1928 ** This function compiles and runs the SQL statement(s) given in the
1929 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1932 ** The format of the byte stream that is hashed is summarized as follows:
1942 ** <sql> is the original SQL text for each statement run and <n> is
1943 ** the size of that text. The SQL text is UTF-8. A single R character
1944 ** occurs before the start of each row. N means a NULL value.
1945 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1946 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1947 ** B means blobs of <size> bytes. T means text rendered as <size>
1948 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1951 ** For each SQL statement in the X input, there is one S segment. Each
1952 ** S segment is followed by zero or more R segments, one for each row in the
1953 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1954 ** one for each column in the result set. Segments are concatentated directly
1955 ** with no delimiters of any kind.
1957 static void sha3QueryFunc(
1958 sqlite3_context *context,
1960 sqlite3_value **argv
1962 sqlite3 *db = sqlite3_context_db_handle(context);
1963 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1964 sqlite3_stmt *pStmt = 0;
1965 int nCol; /* Number of columns in the result set */
1966 int i; /* Loop counter */
1976 iSize = sqlite3_value_int(argv[1]);
1977 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1978 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1983 if( zSql==0 ) return;
1984 SHA3Init(&cx, iSize);
1986 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1988 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1989 zSql, sqlite3_errmsg(db));
1990 sqlite3_finalize(pStmt);
1991 sqlite3_result_error(context, zMsg, -1);
1995 if( !sqlite3_stmt_readonly(pStmt) ){
1996 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1997 sqlite3_finalize(pStmt);
1998 sqlite3_result_error(context, zMsg, -1);
2002 nCol = sqlite3_column_count(pStmt);
2003 z = sqlite3_sql(pStmt);
2005 hash_step_vformat(&cx,"S%d:",n);
2006 SHA3Update(&cx,(unsigned char*)z,n);
2008 /* Compute a hash over the result of the query */
2009 while( SQLITE_ROW==sqlite3_step(pStmt) ){
2010 SHA3Update(&cx,(const unsigned char*)"R",1);
2011 for(i=0; i<nCol; i++){
2012 switch( sqlite3_column_type(pStmt,i) ){
2014 SHA3Update(&cx, (const unsigned char*)"N",1);
2017 case SQLITE_INTEGER: {
2021 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
2023 for(j=8; j>=1; j--){
2028 SHA3Update(&cx, x, 9);
2031 case SQLITE_FLOAT: {
2035 double r = sqlite3_column_double(pStmt,i);
2037 for(j=8; j>=1; j--){
2042 SHA3Update(&cx,x,9);
2046 int n2 = sqlite3_column_bytes(pStmt, i);
2047 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
2048 hash_step_vformat(&cx,"T%d:",n2);
2049 SHA3Update(&cx, z2, n2);
2053 int n2 = sqlite3_column_bytes(pStmt, i);
2054 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2055 hash_step_vformat(&cx,"B%d:",n2);
2056 SHA3Update(&cx, z2, n2);
2062 sqlite3_finalize(pStmt);
2064 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2071 int sqlite3_shathree_init(
2074 const sqlite3_api_routines *pApi
2077 SQLITE_EXTENSION_INIT2(pApi);
2078 (void)pzErrMsg; /* Unused parameter */
2079 rc = sqlite3_create_function(db, "sha3", 1,
2080 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2082 if( rc==SQLITE_OK ){
2083 rc = sqlite3_create_function(db, "sha3", 2,
2084 SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC,
2087 if( rc==SQLITE_OK ){
2088 rc = sqlite3_create_function(db, "sha3_query", 1,
2089 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2090 0, sha3QueryFunc, 0, 0);
2092 if( rc==SQLITE_OK ){
2093 rc = sqlite3_create_function(db, "sha3_query", 2,
2094 SQLITE_UTF8 | SQLITE_DIRECTONLY,
2095 0, sha3QueryFunc, 0, 0);
2100 /************************* End ../ext/misc/shathree.c ********************/
2101 /************************* Begin ../ext/misc/fileio.c ******************/
2105 ** The author disclaims copyright to this source code. In place of
2106 ** a legal notice, here is a blessing:
2108 ** May you do good and not evil.
2109 ** May you find forgiveness for yourself and forgive others.
2110 ** May you share freely, never taking more than you give.
2112 ******************************************************************************
2114 ** This SQLite extension implements SQL functions readfile() and
2115 ** writefile(), and eponymous virtual type "fsdir".
2117 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2119 ** If neither of the optional arguments is present, then this UDF
2120 ** function writes blob DATA to file FILE. If successful, the number
2121 ** of bytes written is returned. If an error occurs, NULL is returned.
2123 ** If the first option argument - MODE - is present, then it must
2124 ** be passed an integer value that corresponds to a POSIX mode
2125 ** value (file type + permissions, as returned in the stat.st_mode
2126 ** field by the stat() system call). Three types of files may
2127 ** be written/created:
2129 ** regular files: (mode & 0170000)==0100000
2130 ** symbolic links: (mode & 0170000)==0120000
2131 ** directories: (mode & 0170000)==0040000
2133 ** For a directory, the DATA is ignored. For a symbolic link, it is
2134 ** interpreted as text and used as the target of the link. For a
2135 ** regular file, it is interpreted as a blob and written into the
2136 ** named file. Regardless of the type of file, its permissions are
2137 ** set to (mode & 0777) before returning.
2139 ** If the optional MTIME argument is present, then it is interpreted
2140 ** as an integer - the number of seconds since the unix epoch. The
2141 ** modification-time of the target file is set to this value before
2144 ** If three or more arguments are passed to this function and an
2145 ** error is encountered, an exception is raised.
2149 ** Read and return the contents of file FILE (type blob) from disk.
2155 ** SELECT * FROM fsdir($path [, $dir]);
2157 ** Parameter $path is an absolute or relative pathname. If the file that it
2158 ** refers to does not exist, it is an error. If the path refers to a regular
2159 ** file or symbolic link, it returns a single row. Or, if the path refers
2160 ** to a directory, it returns one row for the directory, and one row for each
2161 ** file within the hierarchy rooted at $path.
2163 ** Each row has the following columns:
2165 ** name: Path to file or directory (text value).
2166 ** mode: Value of stat.st_mode for directory entry (an integer).
2167 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2168 ** data: For a regular file, a blob containing the file data. For a
2169 ** symlink, a text value containing the text of the link. For a
2172 ** If a non-NULL value is specified for the optional $dir parameter and
2173 ** $path is a relative path, then $path is interpreted relative to $dir.
2174 ** And the paths returned in the "name" column of the table are also
2175 ** relative to directory $dir.
2177 /* #include "sqlite3ext.h" */
2178 SQLITE_EXTENSION_INIT1
2183 #include <sys/types.h>
2184 #include <sys/stat.h>
2186 #if !defined(_WIN32) && !defined(WIN32)
2187 # include <unistd.h>
2188 # include <dirent.h>
2190 # include <sys/time.h>
2192 # include "windows.h"
2194 # include <direct.h>
2195 /* # include "test_windirent.h" */
2196 # define dirent DIRENT
2198 # define chmod _chmod
2203 # define mkdir(path,mode) _mkdir(path)
2204 # define lstat(path,buf) stat(path,buf)
2211 ** Structure of the fsdir() table-valued function
2214 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2215 #define FSDIR_COLUMN_NAME 0 /* Name of the file */
2216 #define FSDIR_COLUMN_MODE 1 /* Access mode */
2217 #define FSDIR_COLUMN_MTIME 2 /* Last modification time */
2218 #define FSDIR_COLUMN_DATA 3 /* File content */
2219 #define FSDIR_COLUMN_PATH 4 /* Path to top of search */
2220 #define FSDIR_COLUMN_DIR 5 /* Path is relative to this directory */
2224 ** Set the result stored by context ctx to a blob containing the
2225 ** contents of file zName. Or, leave the result unchanged (NULL)
2226 ** if the file does not exist or is unreadable.
2228 ** If the file exceeds the SQLite blob size limit, through an
2229 ** SQLITE_TOOBIG error.
2231 ** Throw an SQLITE_IOERR if there are difficulties pulling the file
2234 static void readFileContents(sqlite3_context *ctx, const char *zName){
2241 in = fopen(zName, "rb");
2243 /* File does not exist or is unreadable. Leave the result set to NULL. */
2246 fseek(in, 0, SEEK_END);
2249 db = sqlite3_context_db_handle(ctx);
2250 mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
2252 sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
2256 pBuf = sqlite3_malloc64( nIn ? nIn : 1 );
2258 sqlite3_result_error_nomem(ctx);
2262 if( nIn==(sqlite3_int64)fread(pBuf, 1, (size_t)nIn, in) ){
2263 sqlite3_result_blob64(ctx, pBuf, nIn, sqlite3_free);
2265 sqlite3_result_error_code(ctx, SQLITE_IOERR);
2272 ** Implementation of the "readfile(X)" SQL function. The entire content
2273 ** of the file named X is read and returned as a BLOB. NULL is returned
2274 ** if the file does not exist or is unreadable.
2276 static void readfileFunc(
2277 sqlite3_context *context,
2279 sqlite3_value **argv
2282 (void)(argc); /* Unused parameter */
2283 zName = (const char*)sqlite3_value_text(argv[0]);
2284 if( zName==0 ) return;
2285 readFileContents(context, zName);
2289 ** Set the error message contained in context ctx to the results of
2290 ** vprintf(zFmt, ...).
2292 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2296 zMsg = sqlite3_vmprintf(zFmt, ap);
2297 sqlite3_result_error(ctx, zMsg, -1);
2304 ** This function is designed to convert a Win32 FILETIME structure into the
2305 ** number of seconds since the Unix Epoch (1970-01-01 00:00:00 UTC).
2307 static sqlite3_uint64 fileTimeToUnixTime(
2308 LPFILETIME pFileTime
2310 SYSTEMTIME epochSystemTime;
2311 ULARGE_INTEGER epochIntervals;
2312 FILETIME epochFileTime;
2313 ULARGE_INTEGER fileIntervals;
2315 memset(&epochSystemTime, 0, sizeof(SYSTEMTIME));
2316 epochSystemTime.wYear = 1970;
2317 epochSystemTime.wMonth = 1;
2318 epochSystemTime.wDay = 1;
2319 SystemTimeToFileTime(&epochSystemTime, &epochFileTime);
2320 epochIntervals.LowPart = epochFileTime.dwLowDateTime;
2321 epochIntervals.HighPart = epochFileTime.dwHighDateTime;
2323 fileIntervals.LowPart = pFileTime->dwLowDateTime;
2324 fileIntervals.HighPart = pFileTime->dwHighDateTime;
2326 return (fileIntervals.QuadPart - epochIntervals.QuadPart) / 10000000;
2330 ** This function attempts to normalize the time values found in the stat()
2331 ** buffer to UTC. This is necessary on Win32, where the runtime library
2332 ** appears to return these values as local times.
2334 static void statTimesToUtc(
2336 struct stat *pStatBuf
2339 WIN32_FIND_DATAW fd;
2340 LPWSTR zUnicodeName;
2341 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2342 zUnicodeName = sqlite3_win32_utf8_to_unicode(zPath);
2344 memset(&fd, 0, sizeof(WIN32_FIND_DATAW));
2345 hFindFile = FindFirstFileW(zUnicodeName, &fd);
2346 if( hFindFile!=NULL ){
2347 pStatBuf->st_ctime = (time_t)fileTimeToUnixTime(&fd.ftCreationTime);
2348 pStatBuf->st_atime = (time_t)fileTimeToUnixTime(&fd.ftLastAccessTime);
2349 pStatBuf->st_mtime = (time_t)fileTimeToUnixTime(&fd.ftLastWriteTime);
2350 FindClose(hFindFile);
2352 sqlite3_free(zUnicodeName);
2358 ** This function is used in place of stat(). On Windows, special handling
2359 ** is required in order for the included time to be returned as UTC. On all
2360 ** other systems, this function simply calls stat().
2362 static int fileStat(
2364 struct stat *pStatBuf
2367 int rc = stat(zPath, pStatBuf);
2368 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2371 return stat(zPath, pStatBuf);
2376 ** This function is used in place of lstat(). On Windows, special handling
2377 ** is required in order for the included time to be returned as UTC. On all
2378 ** other systems, this function simply calls lstat().
2380 static int fileLinkStat(
2382 struct stat *pStatBuf
2385 int rc = lstat(zPath, pStatBuf);
2386 if( rc==0 ) statTimesToUtc(zPath, pStatBuf);
2389 return lstat(zPath, pStatBuf);
2394 ** Argument zFile is the name of a file that will be created and/or written
2395 ** by SQL function writefile(). This function ensures that the directory
2396 ** zFile will be written to exists, creating it if required. The permissions
2397 ** for any path components created by this function are set in accordance
2398 ** with the current umask.
2400 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2401 ** SQLITE_OK is returned if the directory is successfully created, or
2402 ** SQLITE_ERROR otherwise.
2404 static int makeDirectory(
2407 char *zCopy = sqlite3_mprintf("%s", zFile);
2413 int nCopy = (int)strlen(zCopy);
2416 while( rc==SQLITE_OK ){
2420 for(; zCopy[i]!='/' && i<nCopy; i++);
2421 if( i==nCopy ) break;
2424 rc2 = fileStat(zCopy, &sStat);
2426 if( mkdir(zCopy, 0777) ) rc = SQLITE_ERROR;
2428 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2434 sqlite3_free(zCopy);
2441 ** This function does the work for the writefile() UDF. Refer to
2442 ** header comments at the top of this file for details.
2444 static int writeFile(
2445 sqlite3_context *pCtx, /* Context to return bytes written in */
2446 const char *zFile, /* File to write */
2447 sqlite3_value *pData, /* Data to write */
2448 mode_t mode, /* MODE parameter passed to writefile() */
2449 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2451 #if !defined(_WIN32) && !defined(WIN32)
2452 if( S_ISLNK(mode) ){
2453 const char *zTo = (const char*)sqlite3_value_text(pData);
2454 if( symlink(zTo, zFile)<0 ) return 1;
2458 if( S_ISDIR(mode) ){
2459 if( mkdir(zFile, mode) ){
2460 /* The mkdir() call to create the directory failed. This might not
2461 ** be an error though - if there is already a directory at the same
2462 ** path and either the permissions already match or can be changed
2463 ** to do so using chmod(), it is not an error. */
2466 || 0!=fileStat(zFile, &sStat)
2467 || !S_ISDIR(sStat.st_mode)
2468 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2474 sqlite3_int64 nWrite = 0;
2477 FILE *out = fopen(zFile, "wb");
2478 if( out==0 ) return 1;
2479 z = (const char*)sqlite3_value_blob(pData);
2481 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2482 nWrite = sqlite3_value_bytes(pData);
2488 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2492 sqlite3_result_int64(pCtx, nWrite);
2498 #if !SQLITE_OS_WINRT
2500 FILETIME lastAccess;
2502 SYSTEMTIME currentTime;
2505 LPWSTR zUnicodeName;
2506 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
2508 GetSystemTime(¤tTime);
2509 SystemTimeToFileTime(¤tTime, &lastAccess);
2510 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2511 lastWrite.dwLowDateTime = (DWORD)intervals;
2512 lastWrite.dwHighDateTime = intervals >> 32;
2513 zUnicodeName = sqlite3_win32_utf8_to_unicode(zFile);
2514 if( zUnicodeName==0 ){
2517 hFile = CreateFileW(
2518 zUnicodeName, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2519 FILE_FLAG_BACKUP_SEMANTICS, NULL
2521 sqlite3_free(zUnicodeName);
2522 if( hFile!=INVALID_HANDLE_VALUE ){
2523 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2530 #elif defined(AT_FDCWD) && 0 /* utimensat() is not universally available */
2532 struct timespec times[2];
2533 times[0].tv_nsec = times[1].tv_nsec = 0;
2534 times[0].tv_sec = time(0);
2535 times[1].tv_sec = mtime;
2536 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2541 struct timeval times[2];
2542 times[0].tv_usec = times[1].tv_usec = 0;
2543 times[0].tv_sec = time(0);
2544 times[1].tv_sec = mtime;
2545 if( utimes(zFile, times) ){
2555 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2556 ** Refer to header comments at the top of this file for details.
2558 static void writefileFunc(
2559 sqlite3_context *context,
2561 sqlite3_value **argv
2566 sqlite3_int64 mtime = -1;
2568 if( argc<2 || argc>4 ){
2569 sqlite3_result_error(context,
2570 "wrong number of arguments to function writefile()", -1
2575 zFile = (const char*)sqlite3_value_text(argv[0]);
2576 if( zFile==0 ) return;
2578 mode = (mode_t)sqlite3_value_int(argv[2]);
2581 mtime = sqlite3_value_int64(argv[3]);
2584 res = writeFile(context, zFile, argv[1], mode, mtime);
2585 if( res==1 && errno==ENOENT ){
2586 if( makeDirectory(zFile)==SQLITE_OK ){
2587 res = writeFile(context, zFile, argv[1], mode, mtime);
2591 if( argc>2 && res!=0 ){
2592 if( S_ISLNK(mode) ){
2593 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2594 }else if( S_ISDIR(mode) ){
2595 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2597 ctxErrorMsg(context, "failed to write file: %s", zFile);
2603 ** SQL function: lsmode(MODE)
2605 ** Given a numberic st_mode from stat(), convert it into a human-readable
2606 ** text string in the style of "ls -l".
2608 static void lsModeFunc(
2609 sqlite3_context *context,
2611 sqlite3_value **argv
2614 int iMode = sqlite3_value_int(argv[0]);
2617 if( S_ISLNK(iMode) ){
2619 }else if( S_ISREG(iMode) ){
2621 }else if( S_ISDIR(iMode) ){
2627 int m = (iMode >> ((2-i)*3));
2628 char *a = &z[1 + i*3];
2629 a[0] = (m & 0x4) ? 'r' : '-';
2630 a[1] = (m & 0x2) ? 'w' : '-';
2631 a[2] = (m & 0x1) ? 'x' : '-';
2634 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2637 #ifndef SQLITE_OMIT_VIRTUALTABLE
2640 ** Cursor type for recursively iterating through a directory structure.
2642 typedef struct fsdir_cursor fsdir_cursor;
2643 typedef struct FsdirLevel FsdirLevel;
2646 DIR *pDir; /* From opendir() */
2647 char *zDir; /* Name of directory (nul-terminated) */
2650 struct fsdir_cursor {
2651 sqlite3_vtab_cursor base; /* Base class - must be first */
2653 int nLvl; /* Number of entries in aLvl[] array */
2654 int iLvl; /* Index of current entry */
2655 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2660 struct stat sStat; /* Current lstat() results */
2661 char *zPath; /* Path to current entry */
2662 sqlite3_int64 iRowid; /* Current rowid */
2665 typedef struct fsdir_tab fsdir_tab;
2667 sqlite3_vtab base; /* Base class - must be first */
2671 ** Construct a new fsdir virtual table object.
2673 static int fsdirConnect(
2676 int argc, const char *const*argv,
2677 sqlite3_vtab **ppVtab,
2680 fsdir_tab *pNew = 0;
2686 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2687 if( rc==SQLITE_OK ){
2688 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2689 if( pNew==0 ) return SQLITE_NOMEM;
2690 memset(pNew, 0, sizeof(*pNew));
2691 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
2693 *ppVtab = (sqlite3_vtab*)pNew;
2698 ** This method is the destructor for fsdir vtab objects.
2700 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2701 sqlite3_free(pVtab);
2706 ** Constructor for a new fsdir_cursor object.
2708 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2711 pCur = sqlite3_malloc( sizeof(*pCur) );
2712 if( pCur==0 ) return SQLITE_NOMEM;
2713 memset(pCur, 0, sizeof(*pCur));
2715 *ppCursor = &pCur->base;
2720 ** Reset a cursor back to the state it was in when first returned
2723 static void fsdirResetCursor(fsdir_cursor *pCur){
2725 for(i=0; i<=pCur->iLvl; i++){
2726 FsdirLevel *pLvl = &pCur->aLvl[i];
2727 if( pLvl->pDir ) closedir(pLvl->pDir);
2728 sqlite3_free(pLvl->zDir);
2730 sqlite3_free(pCur->zPath);
2731 sqlite3_free(pCur->aLvl);
2742 ** Destructor for an fsdir_cursor.
2744 static int fsdirClose(sqlite3_vtab_cursor *cur){
2745 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2747 fsdirResetCursor(pCur);
2753 ** Set the error message for the virtual table associated with cursor
2754 ** pCur to the results of vprintf(zFmt, ...).
2756 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2759 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2765 ** Advance an fsdir_cursor to its next row of output.
2767 static int fsdirNext(sqlite3_vtab_cursor *cur){
2768 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2769 mode_t m = pCur->sStat.st_mode;
2773 /* Descend into this directory */
2774 int iNew = pCur->iLvl + 1;
2776 if( iNew>=pCur->nLvl ){
2778 sqlite3_int64 nByte = nNew*sizeof(FsdirLevel);
2779 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc64(pCur->aLvl, nByte);
2780 if( aNew==0 ) return SQLITE_NOMEM;
2781 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2786 pLvl = &pCur->aLvl[iNew];
2788 pLvl->zDir = pCur->zPath;
2790 pLvl->pDir = opendir(pLvl->zDir);
2791 if( pLvl->pDir==0 ){
2792 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2793 return SQLITE_ERROR;
2797 while( pCur->iLvl>=0 ){
2798 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2799 struct dirent *pEntry = readdir(pLvl->pDir);
2801 if( pEntry->d_name[0]=='.' ){
2802 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2803 if( pEntry->d_name[1]=='\0' ) continue;
2805 sqlite3_free(pCur->zPath);
2806 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2807 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2808 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2809 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2810 return SQLITE_ERROR;
2814 closedir(pLvl->pDir);
2815 sqlite3_free(pLvl->zDir);
2822 sqlite3_free(pCur->zPath);
2828 ** Return values of columns for the row at which the series_cursor
2829 ** is currently pointing.
2831 static int fsdirColumn(
2832 sqlite3_vtab_cursor *cur, /* The cursor */
2833 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2834 int i /* Which column to return */
2836 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2838 case FSDIR_COLUMN_NAME: {
2839 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2843 case FSDIR_COLUMN_MODE:
2844 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2847 case FSDIR_COLUMN_MTIME:
2848 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2851 case FSDIR_COLUMN_DATA: {
2852 mode_t m = pCur->sStat.st_mode;
2854 sqlite3_result_null(ctx);
2855 #if !defined(_WIN32) && !defined(WIN32)
2856 }else if( S_ISLNK(m) ){
2858 char *aBuf = aStatic;
2859 sqlite3_int64 nBuf = 64;
2863 n = readlink(pCur->zPath, aBuf, nBuf);
2865 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2867 aBuf = sqlite3_malloc64(nBuf);
2869 sqlite3_result_error_nomem(ctx);
2870 return SQLITE_NOMEM;
2874 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2875 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2878 readFileContents(ctx, pCur->zPath);
2881 case FSDIR_COLUMN_PATH:
2883 /* The FSDIR_COLUMN_PATH and FSDIR_COLUMN_DIR are input parameters.
2884 ** always return their values as NULL */
2892 ** Return the rowid for the current row. In this implementation, the
2893 ** first row returned is assigned rowid value 1, and each subsequent
2894 ** row a value 1 more than that of the previous.
2896 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2897 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2898 *pRowid = pCur->iRowid;
2903 ** Return TRUE if the cursor has been moved off of the last
2906 static int fsdirEof(sqlite3_vtab_cursor *cur){
2907 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2908 return (pCur->zPath==0);
2912 ** xFilter callback.
2914 ** idxNum==1 PATH parameter only
2915 ** idxNum==2 Both PATH and DIR supplied
2917 static int fsdirFilter(
2918 sqlite3_vtab_cursor *cur,
2919 int idxNum, const char *idxStr,
2920 int argc, sqlite3_value **argv
2922 const char *zDir = 0;
2923 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2925 fsdirResetCursor(pCur);
2928 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2929 return SQLITE_ERROR;
2932 assert( argc==idxNum && (argc==1 || argc==2) );
2933 zDir = (const char*)sqlite3_value_text(argv[0]);
2935 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2936 return SQLITE_ERROR;
2939 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2942 pCur->nBase = (int)strlen(pCur->zBase)+1;
2943 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2945 pCur->zPath = sqlite3_mprintf("%s", zDir);
2948 if( pCur->zPath==0 ){
2949 return SQLITE_NOMEM;
2951 if( fileLinkStat(pCur->zPath, &pCur->sStat) ){
2952 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2953 return SQLITE_ERROR;
2960 ** SQLite will invoke this method one or more times while planning a query
2961 ** that uses the generate_series virtual table. This routine needs to create
2962 ** a query plan for each invocation and compute an estimated cost for that
2965 ** In this implementation idxNum is used to represent the
2966 ** query plan. idxStr is unused.
2968 ** The query plan is represented by values of idxNum:
2970 ** (1) The path value is supplied by argv[0]
2971 ** (2) Path is in argv[0] and dir is in argv[1]
2973 static int fsdirBestIndex(
2975 sqlite3_index_info *pIdxInfo
2977 int i; /* Loop over constraints */
2978 int idxPath = -1; /* Index in pIdxInfo->aConstraint of PATH= */
2979 int idxDir = -1; /* Index in pIdxInfo->aConstraint of DIR= */
2980 int seenPath = 0; /* True if an unusable PATH= constraint is seen */
2981 int seenDir = 0; /* True if an unusable DIR= constraint is seen */
2982 const struct sqlite3_index_constraint *pConstraint;
2985 pConstraint = pIdxInfo->aConstraint;
2986 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2987 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2988 switch( pConstraint->iColumn ){
2989 case FSDIR_COLUMN_PATH: {
2990 if( pConstraint->usable ){
2993 }else if( idxPath<0 ){
2998 case FSDIR_COLUMN_DIR: {
2999 if( pConstraint->usable ){
3002 }else if( idxDir<0 ){
3009 if( seenPath || seenDir ){
3010 /* If input parameters are unusable, disallow this plan */
3011 return SQLITE_CONSTRAINT;
3015 pIdxInfo->idxNum = 0;
3016 /* The pIdxInfo->estimatedCost should have been initialized to a huge
3017 ** number. Leave it unchanged. */
3018 pIdxInfo->estimatedRows = 0x7fffffff;
3020 pIdxInfo->aConstraintUsage[idxPath].omit = 1;
3021 pIdxInfo->aConstraintUsage[idxPath].argvIndex = 1;
3023 pIdxInfo->aConstraintUsage[idxDir].omit = 1;
3024 pIdxInfo->aConstraintUsage[idxDir].argvIndex = 2;
3025 pIdxInfo->idxNum = 2;
3026 pIdxInfo->estimatedCost = 10.0;
3028 pIdxInfo->idxNum = 1;
3029 pIdxInfo->estimatedCost = 100.0;
3037 ** Register the "fsdir" virtual table.
3039 static int fsdirRegister(sqlite3 *db){
3040 static sqlite3_module fsdirModule = {
3043 fsdirConnect, /* xConnect */
3044 fsdirBestIndex, /* xBestIndex */
3045 fsdirDisconnect, /* xDisconnect */
3047 fsdirOpen, /* xOpen - open a cursor */
3048 fsdirClose, /* xClose - close a cursor */
3049 fsdirFilter, /* xFilter - configure scan constraints */
3050 fsdirNext, /* xNext - advance a cursor */
3051 fsdirEof, /* xEof - check for end of scan */
3052 fsdirColumn, /* xColumn - read data */
3053 fsdirRowid, /* xRowid - read data */
3059 0, /* xFindMethod */
3063 0, /* xRollbackTo */
3064 0, /* xShadowName */
3067 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
3070 #else /* SQLITE_OMIT_VIRTUALTABLE */
3071 # define fsdirRegister(x) SQLITE_OK
3077 int sqlite3_fileio_init(
3080 const sqlite3_api_routines *pApi
3083 SQLITE_EXTENSION_INIT2(pApi);
3084 (void)pzErrMsg; /* Unused parameter */
3085 rc = sqlite3_create_function(db, "readfile", 1,
3086 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3087 readfileFunc, 0, 0);
3088 if( rc==SQLITE_OK ){
3089 rc = sqlite3_create_function(db, "writefile", -1,
3090 SQLITE_UTF8|SQLITE_DIRECTONLY, 0,
3091 writefileFunc, 0, 0);
3093 if( rc==SQLITE_OK ){
3094 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
3097 if( rc==SQLITE_OK ){
3098 rc = fsdirRegister(db);
3103 /************************* End ../ext/misc/fileio.c ********************/
3104 /************************* Begin ../ext/misc/completion.c ******************/
3108 ** The author disclaims copyright to this source code. In place of
3109 ** a legal notice, here is a blessing:
3111 ** May you do good and not evil.
3112 ** May you find forgiveness for yourself and forgive others.
3113 ** May you share freely, never taking more than you give.
3115 *************************************************************************
3117 ** This file implements an eponymous virtual table that returns suggested
3118 ** completions for a partial SQL input.
3122 ** SELECT DISTINCT candidate COLLATE nocase
3123 ** FROM completion($prefix,$wholeline)
3126 ** The two query parameters are optional. $prefix is the text of the
3127 ** current word being typed and that is to be completed. $wholeline is
3128 ** the complete input line, used for context.
3130 ** The raw completion() table might return the same candidate multiple
3131 ** times, for example if the same column name is used to two or more
3132 ** tables. And the candidates are returned in an arbitrary order. Hence,
3133 ** the DISTINCT and ORDER BY are recommended.
3135 ** This virtual table operates at the speed of human typing, and so there
3136 ** is no attempt to make it fast. Even a slow implementation will be much
3137 ** faster than any human can type.
3140 /* #include "sqlite3ext.h" */
3141 SQLITE_EXTENSION_INIT1
3146 #ifndef SQLITE_OMIT_VIRTUALTABLE
3148 /* completion_vtab is a subclass of sqlite3_vtab which will
3149 ** serve as the underlying representation of a completion virtual table
3151 typedef struct completion_vtab completion_vtab;
3152 struct completion_vtab {
3153 sqlite3_vtab base; /* Base class - must be first */
3154 sqlite3 *db; /* Database connection for this completion vtab */
3157 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
3158 ** serve as the underlying representation of a cursor that scans
3159 ** over rows of the result
3161 typedef struct completion_cursor completion_cursor;
3162 struct completion_cursor {
3163 sqlite3_vtab_cursor base; /* Base class - must be first */
3164 sqlite3 *db; /* Database connection for this cursor */
3165 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
3166 char *zPrefix; /* The prefix for the word we want to complete */
3167 char *zLine; /* The whole that we want to complete */
3168 const char *zCurrentRow; /* Current output row */
3169 int szRow; /* Length of the zCurrentRow string */
3170 sqlite3_stmt *pStmt; /* Current statement */
3171 sqlite3_int64 iRowid; /* The rowid */
3172 int ePhase; /* Current phase */
3173 int j; /* inter-phase counter */
3176 /* Values for ePhase:
3178 #define COMPLETION_FIRST_PHASE 1
3179 #define COMPLETION_KEYWORDS 1
3180 #define COMPLETION_PRAGMAS 2
3181 #define COMPLETION_FUNCTIONS 3
3182 #define COMPLETION_COLLATIONS 4
3183 #define COMPLETION_INDEXES 5
3184 #define COMPLETION_TRIGGERS 6
3185 #define COMPLETION_DATABASES 7
3186 #define COMPLETION_TABLES 8 /* Also VIEWs and TRIGGERs */
3187 #define COMPLETION_COLUMNS 9
3188 #define COMPLETION_MODULES 10
3189 #define COMPLETION_EOF 11
3192 ** The completionConnect() method is invoked to create a new
3193 ** completion_vtab that describes the completion virtual table.
3195 ** Think of this routine as the constructor for completion_vtab objects.
3197 ** All this routine needs to do is:
3199 ** (1) Allocate the completion_vtab object and initialize all fields.
3201 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
3202 ** result set of queries against completion will look like.
3204 static int completionConnect(
3207 int argc, const char *const*argv,
3208 sqlite3_vtab **ppVtab,
3211 completion_vtab *pNew;
3214 (void)(pAux); /* Unused parameter */
3215 (void)(argc); /* Unused parameter */
3216 (void)(argv); /* Unused parameter */
3217 (void)(pzErr); /* Unused parameter */
3219 /* Column numbers */
3220 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
3221 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
3222 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
3223 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
3225 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3226 rc = sqlite3_declare_vtab(db,
3229 " prefix TEXT HIDDEN,"
3230 " wholeline TEXT HIDDEN,"
3231 " phase INT HIDDEN" /* Used for debugging only */
3233 if( rc==SQLITE_OK ){
3234 pNew = sqlite3_malloc( sizeof(*pNew) );
3235 *ppVtab = (sqlite3_vtab*)pNew;
3236 if( pNew==0 ) return SQLITE_NOMEM;
3237 memset(pNew, 0, sizeof(*pNew));
3244 ** This method is the destructor for completion_cursor objects.
3246 static int completionDisconnect(sqlite3_vtab *pVtab){
3247 sqlite3_free(pVtab);
3252 ** Constructor for a new completion_cursor object.
3254 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3255 completion_cursor *pCur;
3256 pCur = sqlite3_malloc( sizeof(*pCur) );
3257 if( pCur==0 ) return SQLITE_NOMEM;
3258 memset(pCur, 0, sizeof(*pCur));
3259 pCur->db = ((completion_vtab*)p)->db;
3260 *ppCursor = &pCur->base;
3265 ** Reset the completion_cursor.
3267 static void completionCursorReset(completion_cursor *pCur){
3268 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3269 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3270 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3275 ** Destructor for a completion_cursor.
3277 static int completionClose(sqlite3_vtab_cursor *cur){
3278 completionCursorReset((completion_cursor*)cur);
3284 ** Advance a completion_cursor to its next row of output.
3286 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3287 ** record the current state of the scan. This routine sets ->zCurrentRow
3288 ** to the current row of output and then returns. If no more rows remain,
3289 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3290 ** table that has reached the end of its scan.
3292 ** The current implementation just lists potential identifiers and
3293 ** keywords and filters them by zPrefix. Future enhancements should
3294 ** take zLine into account to try to restrict the set of identifiers and
3295 ** keywords based on what would be legal at the current point of input.
3297 static int completionNext(sqlite3_vtab_cursor *cur){
3298 completion_cursor *pCur = (completion_cursor*)cur;
3299 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3300 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3302 while( pCur->ePhase!=COMPLETION_EOF ){
3303 switch( pCur->ePhase ){
3304 case COMPLETION_KEYWORDS: {
3305 if( pCur->j >= sqlite3_keyword_count() ){
3306 pCur->zCurrentRow = 0;
3307 pCur->ePhase = COMPLETION_DATABASES;
3309 sqlite3_keyword_name(pCur->j++, &pCur->zCurrentRow, &pCur->szRow);
3314 case COMPLETION_DATABASES: {
3315 if( pCur->pStmt==0 ){
3316 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3320 eNextPhase = COMPLETION_TABLES;
3323 case COMPLETION_TABLES: {
3324 if( pCur->pStmt==0 ){
3327 const char *zSep = "";
3328 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3329 while( sqlite3_step(pS2)==SQLITE_ROW ){
3330 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3331 zSql = sqlite3_mprintf(
3333 "SELECT name FROM \"%w\".sqlite_master",
3336 if( zSql==0 ) return SQLITE_NOMEM;
3339 sqlite3_finalize(pS2);
3340 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3344 eNextPhase = COMPLETION_COLUMNS;
3347 case COMPLETION_COLUMNS: {
3348 if( pCur->pStmt==0 ){
3351 const char *zSep = "";
3352 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3353 while( sqlite3_step(pS2)==SQLITE_ROW ){
3354 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3355 zSql = sqlite3_mprintf(
3357 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3358 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3359 " WHERE sm.type='table'",
3360 zSql, zSep, zDb, zDb
3362 if( zSql==0 ) return SQLITE_NOMEM;
3365 sqlite3_finalize(pS2);
3366 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3370 eNextPhase = COMPLETION_EOF;
3375 /* This case is when the phase presets zCurrentRow */
3376 if( pCur->zCurrentRow==0 ) continue;
3378 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3379 /* Extract the next row of content */
3380 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3381 pCur->szRow = sqlite3_column_bytes(pCur->pStmt, iCol);
3383 /* When all rows are finished, advance to the next phase */
3384 sqlite3_finalize(pCur->pStmt);
3386 pCur->ePhase = eNextPhase;
3390 if( pCur->nPrefix==0 ) break;
3391 if( pCur->nPrefix<=pCur->szRow
3392 && sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0
3402 ** Return values of columns for the row at which the completion_cursor
3403 ** is currently pointing.
3405 static int completionColumn(
3406 sqlite3_vtab_cursor *cur, /* The cursor */
3407 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3408 int i /* Which column to return */
3410 completion_cursor *pCur = (completion_cursor*)cur;
3412 case COMPLETION_COLUMN_CANDIDATE: {
3413 sqlite3_result_text(ctx, pCur->zCurrentRow, pCur->szRow,SQLITE_TRANSIENT);
3416 case COMPLETION_COLUMN_PREFIX: {
3417 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3420 case COMPLETION_COLUMN_WHOLELINE: {
3421 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3424 case COMPLETION_COLUMN_PHASE: {
3425 sqlite3_result_int(ctx, pCur->ePhase);
3433 ** Return the rowid for the current row. In this implementation, the
3434 ** rowid is the same as the output value.
3436 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3437 completion_cursor *pCur = (completion_cursor*)cur;
3438 *pRowid = pCur->iRowid;
3443 ** Return TRUE if the cursor has been moved off of the last
3446 static int completionEof(sqlite3_vtab_cursor *cur){
3447 completion_cursor *pCur = (completion_cursor*)cur;
3448 return pCur->ePhase >= COMPLETION_EOF;
3452 ** This method is called to "rewind" the completion_cursor object back
3453 ** to the first row of output. This method is always called at least
3454 ** once prior to any call to completionColumn() or completionRowid() or
3457 static int completionFilter(
3458 sqlite3_vtab_cursor *pVtabCursor,
3459 int idxNum, const char *idxStr,
3460 int argc, sqlite3_value **argv
3462 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3464 (void)(idxStr); /* Unused parameter */
3465 (void)(argc); /* Unused parameter */
3466 completionCursorReset(pCur);
3468 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3469 if( pCur->nPrefix>0 ){
3470 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3471 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3476 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3477 if( pCur->nLine>0 ){
3478 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3479 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3482 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3483 int i = pCur->nLine;
3484 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3487 pCur->nPrefix = pCur->nLine - i;
3488 if( pCur->nPrefix>0 ){
3489 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3490 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3494 pCur->ePhase = COMPLETION_FIRST_PHASE;
3495 return completionNext(pVtabCursor);
3499 ** SQLite will invoke this method one or more times while planning a query
3500 ** that uses the completion virtual table. This routine needs to create
3501 ** a query plan for each invocation and compute an estimated cost for that
3504 ** There are two hidden parameters that act as arguments to the table-valued
3505 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3506 ** is available and bit 1 is set if "wholeline" is available.
3508 static int completionBestIndex(
3510 sqlite3_index_info *pIdxInfo
3512 int i; /* Loop over constraints */
3513 int idxNum = 0; /* The query plan bitmask */
3514 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3515 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3516 int nArg = 0; /* Number of arguments that completeFilter() expects */
3517 const struct sqlite3_index_constraint *pConstraint;
3519 (void)(tab); /* Unused parameter */
3520 pConstraint = pIdxInfo->aConstraint;
3521 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3522 if( pConstraint->usable==0 ) continue;
3523 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3524 switch( pConstraint->iColumn ){
3525 case COMPLETION_COLUMN_PREFIX:
3529 case COMPLETION_COLUMN_WHOLELINE:
3536 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3537 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3539 if( wholelineIdx>=0 ){
3540 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3541 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3543 pIdxInfo->idxNum = idxNum;
3544 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3545 pIdxInfo->estimatedRows = 500 - 100*nArg;
3550 ** This following structure defines all the methods for the
3551 ** completion virtual table.
3553 static sqlite3_module completionModule = {
3556 completionConnect, /* xConnect */
3557 completionBestIndex, /* xBestIndex */
3558 completionDisconnect, /* xDisconnect */
3560 completionOpen, /* xOpen - open a cursor */
3561 completionClose, /* xClose - close a cursor */
3562 completionFilter, /* xFilter - configure scan constraints */
3563 completionNext, /* xNext - advance a cursor */
3564 completionEof, /* xEof - check for end of scan */
3565 completionColumn, /* xColumn - read data */
3566 completionRowid, /* xRowid - read data */
3572 0, /* xFindMethod */
3576 0, /* xRollbackTo */
3580 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3582 int sqlite3CompletionVtabInit(sqlite3 *db){
3584 #ifndef SQLITE_OMIT_VIRTUALTABLE
3585 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3593 int sqlite3_completion_init(
3596 const sqlite3_api_routines *pApi
3599 SQLITE_EXTENSION_INIT2(pApi);
3600 (void)(pzErrMsg); /* Unused parameter */
3601 #ifndef SQLITE_OMIT_VIRTUALTABLE
3602 rc = sqlite3CompletionVtabInit(db);
3607 /************************* End ../ext/misc/completion.c ********************/
3608 /************************* Begin ../ext/misc/appendvfs.c ******************/
3612 ** The author disclaims copyright to this source code. In place of
3613 ** a legal notice, here is a blessing:
3615 ** May you do good and not evil.
3616 ** May you find forgiveness for yourself and forgive others.
3617 ** May you share freely, never taking more than you give.
3619 ******************************************************************************
3621 ** This file implements a VFS shim that allows an SQLite database to be
3622 ** appended onto the end of some other file, such as an executable.
3624 ** A special record must appear at the end of the file that identifies the
3625 ** file as an appended database and provides an offset to page 1. For
3626 ** best performance page 1 should be located at a disk page boundary, though
3627 ** that is not required.
3629 ** When opening a database using this VFS, the connection might treat
3630 ** the file as an ordinary SQLite database, or it might treat is as a
3631 ** database appended onto some other file. Here are the rules:
3633 ** (1) When opening a new empty file, that file is treated as an ordinary
3636 ** (2) When opening a file that begins with the standard SQLite prefix
3637 ** string "SQLite format 3", that file is treated as an ordinary
3640 ** (3) When opening a file that ends with the appendvfs trailer string
3641 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3644 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3645 ** set, then a new database is appended to the already existing file.
3647 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3649 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3650 ** the file containing the database is limited to 1GB. This VFS will refuse
3651 ** to read or write past the 1GB mark. This restriction might be lifted in
3652 ** future versions. For now, if you need a large database, then keep the
3653 ** database in a separate file.
3655 ** If the file being opened is not an appended database, then this shim is
3656 ** a pass-through into the default underlying VFS.
3658 /* #include "sqlite3ext.h" */
3659 SQLITE_EXTENSION_INIT1
3663 /* The append mark at the end of the database is:
3665 ** Start-Of-SQLite3-NNNNNNNN
3666 ** 123456789 123456789 12345
3668 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3669 ** the offset to page 1.
3671 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3672 #define APND_MARK_PREFIX_SZ 17
3673 #define APND_MARK_SIZE 25
3676 ** Maximum size of the combined prefix + database + append-mark. This
3677 ** must be less than 0x40000000 to avoid locking issues on Windows.
3679 #define APND_MAX_SIZE (65536*15259)
3682 ** Forward declaration of objects used by this utility
3684 typedef struct sqlite3_vfs ApndVfs;
3685 typedef struct ApndFile ApndFile;
3687 /* Access to a lower-level VFS that (might) implement dynamic loading,
3688 ** access to randomness, etc.
3690 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3691 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3695 sqlite3_file base; /* IO methods */
3696 sqlite3_int64 iPgOne; /* File offset to page 1 */
3697 sqlite3_int64 iMark; /* Start of the append-mark */
3701 ** Methods for ApndFile
3703 static int apndClose(sqlite3_file*);
3704 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3705 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3706 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3707 static int apndSync(sqlite3_file*, int flags);
3708 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3709 static int apndLock(sqlite3_file*, int);
3710 static int apndUnlock(sqlite3_file*, int);
3711 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3712 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3713 static int apndSectorSize(sqlite3_file*);
3714 static int apndDeviceCharacteristics(sqlite3_file*);
3715 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3716 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3717 static void apndShmBarrier(sqlite3_file*);
3718 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3719 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3720 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3723 ** Methods for ApndVfs
3725 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3726 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3727 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3728 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3729 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3730 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3731 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3732 static void apndDlClose(sqlite3_vfs*, void*);
3733 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3734 static int apndSleep(sqlite3_vfs*, int microseconds);
3735 static int apndCurrentTime(sqlite3_vfs*, double*);
3736 static int apndGetLastError(sqlite3_vfs*, int, char *);
3737 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3738 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3739 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3740 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3742 static sqlite3_vfs apnd_vfs = {
3743 3, /* iVersion (set when registered) */
3744 0, /* szOsFile (set when registered) */
3745 1024, /* mxPathname */
3747 "apndvfs", /* zName */
3748 0, /* pAppData (set when registered) */
3749 apndOpen, /* xOpen */
3750 apndDelete, /* xDelete */
3751 apndAccess, /* xAccess */
3752 apndFullPathname, /* xFullPathname */
3753 apndDlOpen, /* xDlOpen */
3754 apndDlError, /* xDlError */
3755 apndDlSym, /* xDlSym */
3756 apndDlClose, /* xDlClose */
3757 apndRandomness, /* xRandomness */
3758 apndSleep, /* xSleep */
3759 apndCurrentTime, /* xCurrentTime */
3760 apndGetLastError, /* xGetLastError */
3761 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3762 apndSetSystemCall, /* xSetSystemCall */
3763 apndGetSystemCall, /* xGetSystemCall */
3764 apndNextSystemCall /* xNextSystemCall */
3767 static const sqlite3_io_methods apnd_io_methods = {
3769 apndClose, /* xClose */
3770 apndRead, /* xRead */
3771 apndWrite, /* xWrite */
3772 apndTruncate, /* xTruncate */
3773 apndSync, /* xSync */
3774 apndFileSize, /* xFileSize */
3775 apndLock, /* xLock */
3776 apndUnlock, /* xUnlock */
3777 apndCheckReservedLock, /* xCheckReservedLock */
3778 apndFileControl, /* xFileControl */
3779 apndSectorSize, /* xSectorSize */
3780 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3781 apndShmMap, /* xShmMap */
3782 apndShmLock, /* xShmLock */
3783 apndShmBarrier, /* xShmBarrier */
3784 apndShmUnmap, /* xShmUnmap */
3785 apndFetch, /* xFetch */
3786 apndUnfetch /* xUnfetch */
3792 ** Close an apnd-file.
3794 static int apndClose(sqlite3_file *pFile){
3795 pFile = ORIGFILE(pFile);
3796 return pFile->pMethods->xClose(pFile);
3800 ** Read data from an apnd-file.
3802 static int apndRead(
3803 sqlite3_file *pFile,
3808 ApndFile *p = (ApndFile *)pFile;
3809 pFile = ORIGFILE(pFile);
3810 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3814 ** Add the append-mark onto the end of the file.
3816 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3818 unsigned char a[APND_MARK_SIZE];
3819 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3821 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3823 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3827 ** Write data to an apnd-file.
3829 static int apndWrite(
3830 sqlite3_file *pFile,
3836 ApndFile *p = (ApndFile *)pFile;
3837 pFile = ORIGFILE(pFile);
3838 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3839 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3840 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3841 sqlite3_int64 sz = 0;
3842 rc = pFile->pMethods->xFileSize(pFile, &sz);
3843 if( rc==SQLITE_OK ){
3844 p->iMark = sz - APND_MARK_SIZE;
3845 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3846 p->iMark = p->iPgOne + iOfst + iAmt;
3847 rc = apndWriteMark(p, pFile);
3855 ** Truncate an apnd-file.
3857 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3859 ApndFile *p = (ApndFile *)pFile;
3860 pFile = ORIGFILE(pFile);
3861 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3862 if( rc==SQLITE_OK ){
3863 p->iMark = p->iPgOne+size;
3864 rc = apndWriteMark(p, pFile);
3870 ** Sync an apnd-file.
3872 static int apndSync(sqlite3_file *pFile, int flags){
3873 pFile = ORIGFILE(pFile);
3874 return pFile->pMethods->xSync(pFile, flags);
3878 ** Return the current file-size of an apnd-file.
3880 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3881 ApndFile *p = (ApndFile *)pFile;
3883 pFile = ORIGFILE(p);
3884 rc = pFile->pMethods->xFileSize(pFile, pSize);
3885 if( rc==SQLITE_OK && p->iPgOne ){
3886 *pSize -= p->iPgOne + APND_MARK_SIZE;
3892 ** Lock an apnd-file.
3894 static int apndLock(sqlite3_file *pFile, int eLock){
3895 pFile = ORIGFILE(pFile);
3896 return pFile->pMethods->xLock(pFile, eLock);
3900 ** Unlock an apnd-file.
3902 static int apndUnlock(sqlite3_file *pFile, int eLock){
3903 pFile = ORIGFILE(pFile);
3904 return pFile->pMethods->xUnlock(pFile, eLock);
3908 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3910 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3911 pFile = ORIGFILE(pFile);
3912 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3916 ** File control method. For custom operations on an apnd-file.
3918 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3919 ApndFile *p = (ApndFile *)pFile;
3921 pFile = ORIGFILE(pFile);
3922 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3923 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3924 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3930 ** Return the sector-size in bytes for an apnd-file.
3932 static int apndSectorSize(sqlite3_file *pFile){
3933 pFile = ORIGFILE(pFile);
3934 return pFile->pMethods->xSectorSize(pFile);
3938 ** Return the device characteristic flags supported by an apnd-file.
3940 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3941 pFile = ORIGFILE(pFile);
3942 return pFile->pMethods->xDeviceCharacteristics(pFile);
3945 /* Create a shared memory file mapping */
3946 static int apndShmMap(
3947 sqlite3_file *pFile,
3953 pFile = ORIGFILE(pFile);
3954 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3957 /* Perform locking on a shared-memory segment */
3958 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3959 pFile = ORIGFILE(pFile);
3960 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3963 /* Memory barrier operation on shared memory */
3964 static void apndShmBarrier(sqlite3_file *pFile){
3965 pFile = ORIGFILE(pFile);
3966 pFile->pMethods->xShmBarrier(pFile);
3969 /* Unmap a shared memory segment */
3970 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3971 pFile = ORIGFILE(pFile);
3972 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3975 /* Fetch a page of a memory-mapped file */
3976 static int apndFetch(
3977 sqlite3_file *pFile,
3978 sqlite3_int64 iOfst,
3982 ApndFile *p = (ApndFile *)pFile;
3983 pFile = ORIGFILE(pFile);
3984 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3987 /* Release a memory-mapped page */
3988 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3989 ApndFile *p = (ApndFile *)pFile;
3990 pFile = ORIGFILE(pFile);
3991 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3995 ** Check to see if the file is an ordinary SQLite database file.
3997 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
4000 static const char aSqliteHdr[] = "SQLite format 3";
4001 if( sz<512 ) return 0;
4002 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
4004 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
4008 ** Try to read the append-mark off the end of a file. Return the
4009 ** start of the appended database if the append-mark is present. If
4010 ** there is no append-mark, return -1;
4012 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
4014 sqlite3_int64 iMark;
4015 unsigned char a[APND_MARK_SIZE];
4017 if( sz<=APND_MARK_SIZE ) return -1;
4018 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
4020 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
4021 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
4023 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
4029 ** Open an apnd file handle.
4031 static int apndOpen(
4034 sqlite3_file *pFile,
4039 sqlite3_file *pSubFile;
4040 sqlite3_vfs *pSubVfs;
4043 pSubVfs = ORIGVFS(pVfs);
4044 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
4045 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
4047 p = (ApndFile*)pFile;
4048 memset(p, 0, sizeof(*p));
4049 pSubFile = ORIGFILE(pFile);
4050 p->base.pMethods = &apnd_io_methods;
4051 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
4052 if( rc ) goto apnd_open_done;
4053 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
4055 pSubFile->pMethods->xClose(pSubFile);
4056 goto apnd_open_done;
4058 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
4059 memmove(pFile, pSubFile, pSubVfs->szOsFile);
4063 p->iPgOne = apndReadMark(sz, pFile);
4067 if( (flags & SQLITE_OPEN_CREATE)==0 ){
4068 pSubFile->pMethods->xClose(pSubFile);
4069 rc = SQLITE_CANTOPEN;
4071 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
4073 if( rc ) pFile->pMethods = 0;
4078 ** All other VFS methods are pass-thrus.
4080 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
4081 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
4083 static int apndAccess(
4089 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
4091 static int apndFullPathname(
4097 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
4099 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
4100 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
4102 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
4103 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
4105 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
4106 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
4108 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
4109 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
4111 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
4112 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
4114 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
4115 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
4117 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
4118 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
4120 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
4121 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
4123 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
4124 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
4126 static int apndSetSystemCall(
4129 sqlite3_syscall_ptr pCall
4131 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
4133 static sqlite3_syscall_ptr apndGetSystemCall(
4137 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
4139 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
4140 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
4148 ** This routine is called when the extension is loaded.
4149 ** Register the new VFS.
4151 int sqlite3_appendvfs_init(
4154 const sqlite3_api_routines *pApi
4158 SQLITE_EXTENSION_INIT2(pApi);
4161 pOrig = sqlite3_vfs_find(0);
4162 apnd_vfs.iVersion = pOrig->iVersion;
4163 apnd_vfs.pAppData = pOrig;
4164 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
4165 rc = sqlite3_vfs_register(&apnd_vfs, 0);
4166 #ifdef APPENDVFS_TEST
4167 if( rc==SQLITE_OK ){
4168 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
4171 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
4175 /************************* End ../ext/misc/appendvfs.c ********************/
4176 /************************* Begin ../ext/misc/memtrace.c ******************/
4180 ** The author disclaims copyright to this source code. In place of
4181 ** a legal notice, here is a blessing:
4183 ** May you do good and not evil.
4184 ** May you find forgiveness for yourself and forgive others.
4185 ** May you share freely, never taking more than you give.
4187 *************************************************************************
4189 ** This file implements an extension that uses the SQLITE_CONFIG_MALLOC
4190 ** mechanism to add a tracing layer on top of SQLite. If this extension
4191 ** is registered prior to sqlite3_initialize(), it will cause all memory
4192 ** allocation activities to be logged on standard output, or to some other
4193 ** FILE specified by the initializer.
4195 ** This file needs to be compiled into the application that uses it.
4197 ** This extension is used to implement the --memtrace option of the
4198 ** command-line shell.
4204 /* The original memory allocation routines */
4205 static sqlite3_mem_methods memtraceBase;
4206 static FILE *memtraceOut;
4208 /* Methods that trace memory allocations */
4209 static void *memtraceMalloc(int n){
4211 fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n",
4212 memtraceBase.xRoundup(n));
4214 return memtraceBase.xMalloc(n);
4216 static void memtraceFree(void *p){
4219 fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
4221 memtraceBase.xFree(p);
4223 static void *memtraceRealloc(void *p, int n){
4224 if( p==0 ) return memtraceMalloc(n);
4230 fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
4231 memtraceBase.xSize(p), memtraceBase.xRoundup(n));
4233 return memtraceBase.xRealloc(p, n);
4235 static int memtraceSize(void *p){
4236 return memtraceBase.xSize(p);
4238 static int memtraceRoundup(int n){
4239 return memtraceBase.xRoundup(n);
4241 static int memtraceInit(void *p){
4242 return memtraceBase.xInit(p);
4244 static void memtraceShutdown(void *p){
4245 memtraceBase.xShutdown(p);
4248 /* The substitute memory allocator */
4249 static sqlite3_mem_methods ersaztMethods = {
4260 /* Begin tracing memory allocations to out. */
4261 int sqlite3MemTraceActivate(FILE *out){
4263 if( memtraceBase.xMalloc==0 ){
4264 rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
4265 if( rc==SQLITE_OK ){
4266 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
4273 /* Deactivate memory tracing */
4274 int sqlite3MemTraceDeactivate(void){
4276 if( memtraceBase.xMalloc!=0 ){
4277 rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
4278 if( rc==SQLITE_OK ){
4279 memset(&memtraceBase, 0, sizeof(memtraceBase));
4286 /************************* End ../ext/misc/memtrace.c ********************/
4287 /************************* Begin ../ext/misc/uint.c ******************/
4291 ** The author disclaims copyright to this source code. In place of
4292 ** a legal notice, here is a blessing:
4294 ** May you do good and not evil.
4295 ** May you find forgiveness for yourself and forgive others.
4296 ** May you share freely, never taking more than you give.
4298 ******************************************************************************
4300 ** This SQLite extension implements the UINT collating sequence.
4302 ** UINT works like BINARY for text, except that embedded strings
4303 ** of digits compare in numeric order.
4305 ** * Leading zeros are handled properly, in the sense that
4306 ** they do not mess of the maginitude comparison of embedded
4307 ** strings of digits. "x00123y" is equal to "x123y".
4309 ** * Only unsigned integers are recognized. Plus and minus
4310 ** signs are ignored. Decimal points and exponential notation
4313 ** * Embedded integers can be of arbitrary length. Comparison
4314 ** is *not* limited integers that can be expressed as a
4315 ** 64-bit machine integer.
4317 /* #include "sqlite3ext.h" */
4318 SQLITE_EXTENSION_INIT1
4324 ** Compare text in lexicographic order, except strings of digits
4325 ** compare in numeric order.
4327 static int uintCollFunc(
4329 int nKey1, const void *pKey1,
4330 int nKey2, const void *pKey2
4332 const unsigned char *zA = (const unsigned char*)pKey1;
4333 const unsigned char *zB = (const unsigned char*)pKey2;
4336 while( i<nKey1 && j<nKey2 ){
4338 if( isdigit(zA[i]) ){
4340 if( !isdigit(zB[j]) ) return x;
4341 while( i<nKey1 && zA[i]=='0' ){ i++; }
4342 while( j<nKey2 && zB[j]=='0' ){ j++; }
4344 while( i+k<nKey1 && isdigit(zA[i+k])
4345 && j+k<nKey2 && isdigit(zB[j+k]) ){
4348 if( i+k<nKey1 && isdigit(zA[i+k]) ){
4350 }else if( j+k<nKey2 && isdigit(zB[j+k]) ){
4353 x = memcmp(zA+i, zB+j, k);
4365 return (nKey1 - i) - (nKey2 - j);
4371 int sqlite3_uint_init(
4374 const sqlite3_api_routines *pApi
4376 SQLITE_EXTENSION_INIT2(pApi);
4377 (void)pzErrMsg; /* Unused parameter */
4378 return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc);
4381 /************************* End ../ext/misc/uint.c ********************/
4382 #ifdef SQLITE_HAVE_ZLIB
4383 /************************* Begin ../ext/misc/zipfile.c ******************/
4387 ** The author disclaims copyright to this source code. In place of
4388 ** a legal notice, here is a blessing:
4390 ** May you do good and not evil.
4391 ** May you find forgiveness for yourself and forgive others.
4392 ** May you share freely, never taking more than you give.
4394 ******************************************************************************
4396 ** This file implements a virtual table for reading and writing ZIP archive
4401 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
4403 ** Current limitations:
4405 ** * No support for encryption
4406 ** * No support for ZIP archives spanning multiple files
4407 ** * No support for zip64 extensions
4408 ** * Only the "inflate/deflate" (zlib) compression method is supported
4410 /* #include "sqlite3ext.h" */
4411 SQLITE_EXTENSION_INIT1
4418 #ifndef SQLITE_OMIT_VIRTUALTABLE
4420 #ifndef SQLITE_AMALGAMATION
4422 /* typedef sqlite3_int64 i64; */
4423 /* typedef unsigned char u8; */
4424 typedef unsigned short u16;
4425 typedef unsigned long u32;
4426 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4428 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
4429 # define ALWAYS(X) (1)
4430 # define NEVER(X) (0)
4431 #elif !defined(NDEBUG)
4432 # define ALWAYS(X) ((X)?1:(assert(0),0))
4433 # define NEVER(X) ((X)?(assert(0),1):0)
4435 # define ALWAYS(X) (X)
4436 # define NEVER(X) (X)
4439 #endif /* SQLITE_AMALGAMATION */
4442 ** Definitions for mode bitmasks S_IFDIR, S_IFREG and S_IFLNK.
4444 ** In some ways it would be better to obtain these values from system
4445 ** header files. But, the dependency is undesirable and (a) these
4446 ** have been stable for decades, (b) the values are part of POSIX and
4447 ** are also made explicit in [man stat], and (c) are part of the
4448 ** file format for zip archives.
4451 # define S_IFDIR 0040000
4454 # define S_IFREG 0100000
4457 # define S_IFLNK 0120000
4460 static const char ZIPFILE_SCHEMA[] =
4462 "name PRIMARY KEY," /* 0: Name of file in zip archive */
4463 "mode," /* 1: POSIX mode for file */
4464 "mtime," /* 2: Last modification time (secs since 1970)*/
4465 "sz," /* 3: Size of object */
4466 "rawdata," /* 4: Raw data */
4467 "data," /* 5: Uncompressed data */
4468 "method," /* 6: Compression method (integer) */
4469 "z HIDDEN" /* 7: Name of zip file */
4472 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
4473 #define ZIPFILE_BUFFER_SIZE (64*1024)
4477 ** Magic numbers used to read and write zip files.
4479 ** ZIPFILE_NEWENTRY_MADEBY:
4480 ** Use this value for the "version-made-by" field in new zip file
4481 ** entries. The upper byte indicates "unix", and the lower byte
4482 ** indicates that the zip file matches pkzip specification 3.0.
4483 ** This is what info-zip seems to do.
4485 ** ZIPFILE_NEWENTRY_REQUIRED:
4486 ** Value for "version-required-to-extract" field of new entries.
4487 ** Version 2.0 is required to support folders and deflate compression.
4489 ** ZIPFILE_NEWENTRY_FLAGS:
4490 ** Value for "general-purpose-bit-flags" field of new entries. Bit
4491 ** 11 means "utf-8 filename and comment".
4493 ** ZIPFILE_SIGNATURE_CDS:
4494 ** First 4 bytes of a valid CDS record.
4496 ** ZIPFILE_SIGNATURE_LFH:
4497 ** First 4 bytes of a valid LFH record.
4499 ** ZIPFILE_SIGNATURE_EOCD
4500 ** First 4 bytes of a valid EOCD record.
4502 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4503 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4504 #define ZIPFILE_NEWENTRY_REQUIRED 20
4505 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4506 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4507 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4508 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4511 ** The sizes of the fixed-size part of each of the three main data
4512 ** structures in a zip archive.
4514 #define ZIPFILE_LFH_FIXED_SZ 30
4515 #define ZIPFILE_EOCD_FIXED_SZ 22
4516 #define ZIPFILE_CDS_FIXED_SZ 46
4519 *** 4.3.16 End of central directory record:
4521 *** end of central dir signature 4 bytes (0x06054b50)
4522 *** number of this disk 2 bytes
4523 *** number of the disk with the
4524 *** start of the central directory 2 bytes
4525 *** total number of entries in the
4526 *** central directory on this disk 2 bytes
4527 *** total number of entries in
4528 *** the central directory 2 bytes
4529 *** size of the central directory 4 bytes
4530 *** offset of start of central
4531 *** directory with respect to
4532 *** the starting disk number 4 bytes
4533 *** .ZIP file comment length 2 bytes
4534 *** .ZIP file comment (variable size)
4536 typedef struct ZipfileEOCD ZipfileEOCD;
4537 struct ZipfileEOCD {
4547 *** 4.3.12 Central directory structure:
4551 *** central file header signature 4 bytes (0x02014b50)
4552 *** version made by 2 bytes
4553 *** version needed to extract 2 bytes
4554 *** general purpose bit flag 2 bytes
4555 *** compression method 2 bytes
4556 *** last mod file time 2 bytes
4557 *** last mod file date 2 bytes
4559 *** compressed size 4 bytes
4560 *** uncompressed size 4 bytes
4561 *** file name length 2 bytes
4562 *** extra field length 2 bytes
4563 *** file comment length 2 bytes
4564 *** disk number start 2 bytes
4565 *** internal file attributes 2 bytes
4566 *** external file attributes 4 bytes
4567 *** relative offset of local header 4 bytes
4569 typedef struct ZipfileCDS ZipfileCDS;
4572 u16 iVersionExtract;
4587 char *zFile; /* Filename (sqlite3_malloc()) */
4591 *** 4.3.7 Local file header:
4593 *** local file header signature 4 bytes (0x04034b50)
4594 *** version needed to extract 2 bytes
4595 *** general purpose bit flag 2 bytes
4596 *** compression method 2 bytes
4597 *** last mod file time 2 bytes
4598 *** last mod file date 2 bytes
4600 *** compressed size 4 bytes
4601 *** uncompressed size 4 bytes
4602 *** file name length 2 bytes
4603 *** extra field length 2 bytes
4606 typedef struct ZipfileLFH ZipfileLFH;
4608 u16 iVersionExtract;
4620 typedef struct ZipfileEntry ZipfileEntry;
4621 struct ZipfileEntry {
4622 ZipfileCDS cds; /* Parsed CDS record */
4623 u32 mUnixTime; /* Modification time, in UNIX format */
4624 u8 *aExtra; /* cds.nExtra+cds.nComment bytes of extra data */
4625 i64 iDataOff; /* Offset to data in file (if aData==0) */
4626 u8 *aData; /* cds.szCompressed bytes of compressed data */
4627 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4631 ** Cursor type for zipfile tables.
4633 typedef struct ZipfileCsr ZipfileCsr;
4635 sqlite3_vtab_cursor base; /* Base class - must be first */
4636 i64 iId; /* Cursor ID */
4637 u8 bEof; /* True when at EOF */
4638 u8 bNoop; /* If next xNext() call is no-op */
4640 /* Used outside of write transactions */
4641 FILE *pFile; /* Zip file */
4642 i64 iNextOff; /* Offset of next record in central directory */
4643 ZipfileEOCD eocd; /* Parse of central directory record */
4645 ZipfileEntry *pFreeEntry; /* Free this list when cursor is closed or reset */
4646 ZipfileEntry *pCurrent; /* Current entry */
4647 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4650 typedef struct ZipfileTab ZipfileTab;
4652 sqlite3_vtab base; /* Base class - must be first */
4653 char *zFile; /* Zip file this table accesses (may be NULL) */
4654 sqlite3 *db; /* Host database connection */
4655 u8 *aBuffer; /* Temporary buffer used for various tasks */
4657 ZipfileCsr *pCsrList; /* List of cursors */
4660 /* The following are used by write transactions only */
4661 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4662 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
4663 FILE *pWriteFd; /* File handle open on zip archive */
4664 i64 szCurrent; /* Current size of zip archive */
4665 i64 szOrig; /* Size of archive at start of transaction */
4669 ** Set the error message contained in context ctx to the results of
4670 ** vprintf(zFmt, ...).
4672 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4676 zMsg = sqlite3_vmprintf(zFmt, ap);
4677 sqlite3_result_error(ctx, zMsg, -1);
4683 ** If string zIn is quoted, dequote it in place. Otherwise, if the string
4684 ** is not quoted, do nothing.
4686 static void zipfileDequote(char *zIn){
4688 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4691 if( q=='[' ) q = ']';
4692 while( ALWAYS(zIn[iIn]) ){
4693 char c = zIn[iIn++];
4694 if( c==q && zIn[iIn++]!=q ) break;
4702 ** Construct a new ZipfileTab virtual table object.
4704 ** argv[0] -> module name ("zipfile")
4705 ** argv[1] -> database name
4706 ** argv[2] -> table name
4707 ** argv[...] -> "column name" and other module argument fields.
4709 static int zipfileConnect(
4712 int argc, const char *const*argv,
4713 sqlite3_vtab **ppVtab,
4716 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4718 const char *zFile = 0;
4719 ZipfileTab *pNew = 0;
4722 /* If the table name is not "zipfile", require that the argument be
4723 ** specified. This stops zipfile tables from being created as:
4725 ** CREATE VIRTUAL TABLE zzz USING zipfile();
4727 ** It does not prevent:
4729 ** CREATE VIRTUAL TABLE zipfile USING zipfile();
4731 assert( 0==sqlite3_stricmp(argv[0], "zipfile") );
4732 if( (0!=sqlite3_stricmp(argv[2], "zipfile") && argc<4) || argc>4 ){
4733 *pzErr = sqlite3_mprintf("zipfile constructor requires one argument");
4734 return SQLITE_ERROR;
4739 nFile = (int)strlen(zFile)+1;
4742 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4743 if( rc==SQLITE_OK ){
4744 pNew = (ZipfileTab*)sqlite3_malloc64((sqlite3_int64)nByte+nFile);
4745 if( pNew==0 ) return SQLITE_NOMEM;
4746 memset(pNew, 0, nByte+nFile);
4748 pNew->aBuffer = (u8*)&pNew[1];
4750 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4751 memcpy(pNew->zFile, zFile, nFile);
4752 zipfileDequote(pNew->zFile);
4755 sqlite3_vtab_config(db, SQLITE_VTAB_DIRECTONLY);
4756 *ppVtab = (sqlite3_vtab*)pNew;
4761 ** Free the ZipfileEntry structure indicated by the only argument.
4763 static void zipfileEntryFree(ZipfileEntry *p){
4765 sqlite3_free(p->cds.zFile);
4771 ** Release resources that should be freed at the end of a write
4774 static void zipfileCleanupTransaction(ZipfileTab *pTab){
4775 ZipfileEntry *pEntry;
4776 ZipfileEntry *pNext;
4778 if( pTab->pWriteFd ){
4779 fclose(pTab->pWriteFd);
4782 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
4783 pNext = pEntry->pNext;
4784 zipfileEntryFree(pEntry);
4786 pTab->pFirstEntry = 0;
4787 pTab->pLastEntry = 0;
4788 pTab->szCurrent = 0;
4793 ** This method is the destructor for zipfile vtab objects.
4795 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4796 zipfileCleanupTransaction((ZipfileTab*)pVtab);
4797 sqlite3_free(pVtab);
4802 ** Constructor for a new ZipfileCsr object.
4804 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4805 ZipfileTab *pTab = (ZipfileTab*)p;
4807 pCsr = sqlite3_malloc(sizeof(*pCsr));
4808 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4810 return SQLITE_NOMEM;
4812 memset(pCsr, 0, sizeof(*pCsr));
4813 pCsr->iId = ++pTab->iNextCsrid;
4814 pCsr->pCsrNext = pTab->pCsrList;
4815 pTab->pCsrList = pCsr;
4820 ** Reset a cursor back to the state it was in when first returned
4821 ** by zipfileOpen().
4823 static void zipfileResetCursor(ZipfileCsr *pCsr){
4825 ZipfileEntry *pNext;
4829 fclose(pCsr->pFile);
4831 zipfileEntryFree(pCsr->pCurrent);
4835 for(p=pCsr->pFreeEntry; p; p=pNext){
4837 zipfileEntryFree(p);
4842 ** Destructor for an ZipfileCsr.
4844 static int zipfileClose(sqlite3_vtab_cursor *cur){
4845 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4846 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4848 zipfileResetCursor(pCsr);
4850 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4851 for(pp=&pTab->pCsrList; *pp!=pCsr; pp=&((*pp)->pCsrNext));
4852 *pp = pCsr->pCsrNext;
4859 ** Set the error message for the virtual table associated with cursor
4860 ** pCsr to the results of vprintf(zFmt, ...).
4862 static void zipfileTableErr(ZipfileTab *pTab, const char *zFmt, ...){
4865 sqlite3_free(pTab->base.zErrMsg);
4866 pTab->base.zErrMsg = sqlite3_vmprintf(zFmt, ap);
4869 static void zipfileCursorErr(ZipfileCsr *pCsr, const char *zFmt, ...){
4872 sqlite3_free(pCsr->base.pVtab->zErrMsg);
4873 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4878 ** Read nRead bytes of data from offset iOff of file pFile into buffer
4879 ** aRead[]. Return SQLITE_OK if successful, or an SQLite error code
4882 ** If an error does occur, output variable (*pzErrmsg) may be set to point
4883 ** to an English language error message. It is the responsibility of the
4884 ** caller to eventually free this buffer using
4887 static int zipfileReadData(
4888 FILE *pFile, /* Read from this file */
4889 u8 *aRead, /* Read into this buffer */
4890 int nRead, /* Number of bytes to read */
4891 i64 iOff, /* Offset to read from */
4892 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
4895 fseek(pFile, (long)iOff, SEEK_SET);
4896 n = fread(aRead, 1, nRead, pFile);
4897 if( (int)n!=nRead ){
4898 *pzErrmsg = sqlite3_mprintf("error in fread()");
4899 return SQLITE_ERROR;
4904 static int zipfileAppendData(
4910 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4911 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4912 if( (int)n!=nWrite ){
4913 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4914 return SQLITE_ERROR;
4916 pTab->szCurrent += nWrite;
4921 ** Read and return a 16-bit little-endian unsigned integer from buffer aBuf.
4923 static u16 zipfileGetU16(const u8 *aBuf){
4924 return (aBuf[1] << 8) + aBuf[0];
4928 ** Read and return a 32-bit little-endian unsigned integer from buffer aBuf.
4930 static u32 zipfileGetU32(const u8 *aBuf){
4931 return ((u32)(aBuf[3]) << 24)
4932 + ((u32)(aBuf[2]) << 16)
4933 + ((u32)(aBuf[1]) << 8)
4934 + ((u32)(aBuf[0]) << 0);
4938 ** Write a 16-bit little endiate integer into buffer aBuf.
4940 static void zipfilePutU16(u8 *aBuf, u16 val){
4941 aBuf[0] = val & 0xFF;
4942 aBuf[1] = (val>>8) & 0xFF;
4946 ** Write a 32-bit little endiate integer into buffer aBuf.
4948 static void zipfilePutU32(u8 *aBuf, u32 val){
4949 aBuf[0] = val & 0xFF;
4950 aBuf[1] = (val>>8) & 0xFF;
4951 aBuf[2] = (val>>16) & 0xFF;
4952 aBuf[3] = (val>>24) & 0xFF;
4955 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4956 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4958 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4959 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4962 ** Magic numbers used to read CDS records.
4964 #define ZIPFILE_CDS_NFILE_OFF 28
4965 #define ZIPFILE_CDS_SZCOMPRESSED_OFF 20
4968 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4969 ** if the record is not well-formed, or SQLITE_OK otherwise.
4971 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4973 u32 sig = zipfileRead32(aRead);
4975 if( sig!=ZIPFILE_SIGNATURE_CDS ){
4978 pCDS->iVersionMadeBy = zipfileRead16(aRead);
4979 pCDS->iVersionExtract = zipfileRead16(aRead);
4980 pCDS->flags = zipfileRead16(aRead);
4981 pCDS->iCompression = zipfileRead16(aRead);
4982 pCDS->mTime = zipfileRead16(aRead);
4983 pCDS->mDate = zipfileRead16(aRead);
4984 pCDS->crc32 = zipfileRead32(aRead);
4985 pCDS->szCompressed = zipfileRead32(aRead);
4986 pCDS->szUncompressed = zipfileRead32(aRead);
4987 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4988 pCDS->nFile = zipfileRead16(aRead);
4989 pCDS->nExtra = zipfileRead16(aRead);
4990 pCDS->nComment = zipfileRead16(aRead);
4991 pCDS->iDiskStart = zipfileRead16(aRead);
4992 pCDS->iInternalAttr = zipfileRead16(aRead);
4993 pCDS->iExternalAttr = zipfileRead32(aRead);
4994 pCDS->iOffset = zipfileRead32(aRead);
4995 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
5002 ** Decode the LFH record in buffer aBuf into (*pLFH). Return SQLITE_ERROR
5003 ** if the record is not well-formed, or SQLITE_OK otherwise.
5005 static int zipfileReadLFH(
5009 u8 *aRead = aBuffer;
5012 u32 sig = zipfileRead32(aRead);
5013 if( sig!=ZIPFILE_SIGNATURE_LFH ){
5016 pLFH->iVersionExtract = zipfileRead16(aRead);
5017 pLFH->flags = zipfileRead16(aRead);
5018 pLFH->iCompression = zipfileRead16(aRead);
5019 pLFH->mTime = zipfileRead16(aRead);
5020 pLFH->mDate = zipfileRead16(aRead);
5021 pLFH->crc32 = zipfileRead32(aRead);
5022 pLFH->szCompressed = zipfileRead32(aRead);
5023 pLFH->szUncompressed = zipfileRead32(aRead);
5024 pLFH->nFile = zipfileRead16(aRead);
5025 pLFH->nExtra = zipfileRead16(aRead);
5032 ** Buffer aExtra (size nExtra bytes) contains zip archive "extra" fields.
5033 ** Scan through this buffer to find an "extra-timestamp" field. If one
5034 ** exists, extract the 32-bit modification-timestamp from it and store
5035 ** the value in output parameter *pmTime.
5037 ** Zero is returned if no extra-timestamp record could be found (and so
5038 ** *pmTime is left unchanged), or non-zero otherwise.
5040 ** The general format of an extra field is:
5042 ** Header ID 2 bytes
5043 ** Data Size 2 bytes
5046 static int zipfileScanExtra(u8 *aExtra, int nExtra, u32 *pmTime){
5049 u8 *pEnd = &aExtra[nExtra];
5052 u16 id = zipfileRead16(p);
5053 u16 nByte = zipfileRead16(p);
5056 case ZIPFILE_EXTRA_TIMESTAMP: {
5058 if( b & 0x01 ){ /* 0x01 -> modtime is present */
5059 *pmTime = zipfileGetU32(&p[1]);
5072 ** Convert the standard MS-DOS timestamp stored in the mTime and mDate
5073 ** fields of the CDS structure passed as the only argument to a 32-bit
5074 ** UNIX seconds-since-the-epoch timestamp. Return the result.
5076 ** "Standard" MS-DOS time format:
5078 ** File modification time:
5079 ** Bits 00-04: seconds divided by 2
5080 ** Bits 05-10: minute
5082 ** File modification date:
5084 ** Bits 05-08: month (1-12)
5085 ** Bits 09-15: years from 1980
5087 ** https://msdn.microsoft.com/en-us/library/9kkf9tah.aspx
5089 static u32 zipfileMtime(ZipfileCDS *pCDS){
5090 int Y = (1980 + ((pCDS->mDate >> 9) & 0x7F));
5091 int M = ((pCDS->mDate >> 5) & 0x0F);
5092 int D = (pCDS->mDate & 0x1F);
5095 int sec = (pCDS->mTime & 0x1F)*2;
5096 int min = (pCDS->mTime >> 5) & 0x3F;
5097 int hr = (pCDS->mTime >> 11) & 0x1F;
5100 /* JD = INT(365.25 * (Y+4716)) + INT(30.6001 * (M+1)) + D + B - 1524.5 */
5102 /* Calculate the JD in seconds for noon on the day in question */
5107 JD = (i64)(24*60*60) * (
5108 (int)(365.25 * (Y + 4716))
5109 + (int)(30.6001 * (M + 1))
5113 /* Correct the JD for the time within the day */
5114 JD += (hr-12) * 3600 + min * 60 + sec;
5116 /* Convert JD to unix timestamp (the JD epoch is 2440587.5) */
5117 return (u32)(JD - (i64)(24405875) * 24*60*6);
5121 ** The opposite of zipfileMtime(). This function populates the mTime and
5122 ** mDate fields of the CDS structure passed as the first argument according
5123 ** to the UNIX timestamp value passed as the second.
5125 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mUnixTime){
5126 /* Convert unix timestamp to JD (2440588 is noon on 1/1/1970) */
5127 i64 JD = (i64)2440588 + mUnixTime / (24*60*60);
5133 A = (int)((JD - 1867216.25)/36524.25);
5134 A = (int)(JD + 1 + A - (A/4));
5136 C = (int)((B - 122.1)/365.25);
5137 D = (36525*(C&32767))/100;
5138 E = (int)((B-D)/30.6001);
5140 day = B - D - (int)(30.6001*E);
5141 mon = (E<14 ? E-1 : E-13);
5142 yr = mon>2 ? C-4716 : C-4715;
5144 hr = (mUnixTime % (24*60*60)) / (60*60);
5145 min = (mUnixTime % (60*60)) / 60;
5146 sec = (mUnixTime % 60);
5149 pCds->mDate = (u16)(day + (mon << 5) + ((yr-1980) << 9));
5150 pCds->mTime = (u16)(sec/2 + (min<<5) + (hr<<11));
5152 pCds->mDate = pCds->mTime = 0;
5155 assert( mUnixTime<315507600
5156 || mUnixTime==zipfileMtime(pCds)
5157 || ((mUnixTime % 2) && mUnixTime-1==zipfileMtime(pCds))
5158 /* || (mUnixTime % 2) */
5163 ** If aBlob is not NULL, then it is a pointer to a buffer (nBlob bytes in
5164 ** size) containing an entire zip archive image. Or, if aBlob is NULL,
5165 ** then pFile is a file-handle open on a zip file. In either case, this
5166 ** function creates a ZipfileEntry object based on the zip archive entry
5167 ** for which the CDS record is at offset iOff.
5169 ** If successful, SQLITE_OK is returned and (*ppEntry) set to point to
5170 ** the new object. Otherwise, an SQLite error code is returned and the
5171 ** final value of (*ppEntry) undefined.
5173 static int zipfileGetEntry(
5174 ZipfileTab *pTab, /* Store any error message here */
5175 const u8 *aBlob, /* Pointer to in-memory file image */
5176 int nBlob, /* Size of aBlob[] in bytes */
5177 FILE *pFile, /* If aBlob==0, read from this file */
5178 i64 iOff, /* Offset of CDS record */
5179 ZipfileEntry **ppEntry /* OUT: Pointer to new object */
5182 char **pzErr = &pTab->base.zErrMsg;
5186 aRead = pTab->aBuffer;
5187 rc = zipfileReadData(pFile, aRead, ZIPFILE_CDS_FIXED_SZ, iOff, pzErr);
5189 aRead = (u8*)&aBlob[iOff];
5192 if( rc==SQLITE_OK ){
5193 sqlite3_int64 nAlloc;
5196 int nFile = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF]);
5197 int nExtra = zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+2]);
5198 nExtra += zipfileGetU16(&aRead[ZIPFILE_CDS_NFILE_OFF+4]);
5200 nAlloc = sizeof(ZipfileEntry) + nExtra;
5202 nAlloc += zipfileGetU32(&aRead[ZIPFILE_CDS_SZCOMPRESSED_OFF]);
5205 pNew = (ZipfileEntry*)sqlite3_malloc64(nAlloc);
5209 memset(pNew, 0, sizeof(ZipfileEntry));
5210 rc = zipfileReadCDS(aRead, &pNew->cds);
5211 if( rc!=SQLITE_OK ){
5212 *pzErr = sqlite3_mprintf("failed to read CDS at offset %lld", iOff);
5213 }else if( aBlob==0 ){
5214 rc = zipfileReadData(
5215 pFile, aRead, nExtra+nFile, iOff+ZIPFILE_CDS_FIXED_SZ, pzErr
5218 aRead = (u8*)&aBlob[iOff + ZIPFILE_CDS_FIXED_SZ];
5222 if( rc==SQLITE_OK ){
5223 u32 *pt = &pNew->mUnixTime;
5224 pNew->cds.zFile = sqlite3_mprintf("%.*s", nFile, aRead);
5225 pNew->aExtra = (u8*)&pNew[1];
5226 memcpy(pNew->aExtra, &aRead[nFile], nExtra);
5227 if( pNew->cds.zFile==0 ){
5229 }else if( 0==zipfileScanExtra(&aRead[nFile], pNew->cds.nExtra, pt) ){
5230 pNew->mUnixTime = zipfileMtime(&pNew->cds);
5234 if( rc==SQLITE_OK ){
5235 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
5238 rc = zipfileReadData(pFile, aRead, szFix, pNew->cds.iOffset, pzErr);
5240 aRead = (u8*)&aBlob[pNew->cds.iOffset];
5243 rc = zipfileReadLFH(aRead, &lfh);
5244 if( rc==SQLITE_OK ){
5245 pNew->iDataOff = pNew->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
5246 pNew->iDataOff += lfh.nFile + lfh.nExtra;
5247 if( aBlob && pNew->cds.szCompressed ){
5248 pNew->aData = &pNew->aExtra[nExtra];
5249 memcpy(pNew->aData, &aBlob[pNew->iDataOff], pNew->cds.szCompressed);
5252 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d",
5253 (int)pNew->cds.iOffset
5258 if( rc!=SQLITE_OK ){
5259 zipfileEntryFree(pNew);
5269 ** Advance an ZipfileCsr to its next row of output.
5271 static int zipfileNext(sqlite3_vtab_cursor *cur){
5272 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5276 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
5277 zipfileEntryFree(pCsr->pCurrent);
5279 if( pCsr->iNextOff>=iEof ){
5282 ZipfileEntry *p = 0;
5283 ZipfileTab *pTab = (ZipfileTab*)(cur->pVtab);
5284 rc = zipfileGetEntry(pTab, 0, 0, pCsr->pFile, pCsr->iNextOff, &p);
5285 if( rc==SQLITE_OK ){
5286 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
5287 pCsr->iNextOff += (int)p->cds.nExtra + p->cds.nFile + p->cds.nComment;
5293 pCsr->pCurrent = pCsr->pCurrent->pNext;
5295 if( pCsr->pCurrent==0 ){
5304 static void zipfileFree(void *p) {
5309 ** Buffer aIn (size nIn bytes) contains compressed data. Uncompressed, the
5310 ** size is nOut bytes. This function uncompresses the data and sets the
5311 ** return value in context pCtx to the result (a blob).
5313 ** If an error occurs, an error code is left in pCtx instead.
5315 static void zipfileInflate(
5316 sqlite3_context *pCtx, /* Store result here */
5317 const u8 *aIn, /* Compressed data */
5318 int nIn, /* Size of buffer aIn[] in bytes */
5319 int nOut /* Expected output size */
5321 u8 *aRes = sqlite3_malloc(nOut);
5323 sqlite3_result_error_nomem(pCtx);
5327 memset(&str, 0, sizeof(str));
5329 str.next_in = (Byte*)aIn;
5331 str.next_out = (Byte*)aRes;
5332 str.avail_out = nOut;
5334 err = inflateInit2(&str, -15);
5336 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
5338 err = inflate(&str, Z_NO_FLUSH);
5339 if( err!=Z_STREAM_END ){
5340 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
5342 sqlite3_result_blob(pCtx, aRes, nOut, zipfileFree);
5352 ** Buffer aIn (size nIn bytes) contains uncompressed data. This function
5353 ** compresses it and sets (*ppOut) to point to a buffer containing the
5354 ** compressed data. The caller is responsible for eventually calling
5355 ** sqlite3_free() to release buffer (*ppOut). Before returning, (*pnOut)
5356 ** is set to the size of buffer (*ppOut) in bytes.
5358 ** If no error occurs, SQLITE_OK is returned. Otherwise, an SQLite error
5359 ** code is returned and an error message left in virtual-table handle
5360 ** pTab. The values of (*ppOut) and (*pnOut) are left unchanged in this
5363 static int zipfileDeflate(
5364 const u8 *aIn, int nIn, /* Input */
5365 u8 **ppOut, int *pnOut, /* Output */
5366 char **pzErr /* OUT: Error message */
5369 sqlite3_int64 nAlloc;
5373 memset(&str, 0, sizeof(str));
5374 str.next_in = (Bytef*)aIn;
5376 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
5378 nAlloc = deflateBound(&str, nIn);
5379 aOut = (u8*)sqlite3_malloc64(nAlloc);
5384 str.next_out = aOut;
5385 str.avail_out = nAlloc;
5386 res = deflate(&str, Z_FINISH);
5387 if( res==Z_STREAM_END ){
5389 *pnOut = (int)str.total_out;
5392 *pzErr = sqlite3_mprintf("zipfile: deflate() error");
5403 ** Return values of columns for the row at which the series_cursor
5404 ** is currently pointing.
5406 static int zipfileColumn(
5407 sqlite3_vtab_cursor *cur, /* The cursor */
5408 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
5409 int i /* Which column to return */
5411 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5412 ZipfileCDS *pCDS = &pCsr->pCurrent->cds;
5416 sqlite3_result_text(ctx, pCDS->zFile, -1, SQLITE_TRANSIENT);
5419 /* TODO: Whether or not the following is correct surely depends on
5420 ** the platform on which the archive was created. */
5421 sqlite3_result_int(ctx, pCDS->iExternalAttr >> 16);
5423 case 2: { /* mtime */
5424 sqlite3_result_int64(ctx, pCsr->pCurrent->mUnixTime);
5428 if( sqlite3_vtab_nochange(ctx)==0 ){
5429 sqlite3_result_int64(ctx, pCDS->szUncompressed);
5433 case 4: /* rawdata */
5434 if( sqlite3_vtab_nochange(ctx) ) break;
5435 case 5: { /* data */
5436 if( i==4 || pCDS->iCompression==0 || pCDS->iCompression==8 ){
5437 int sz = pCDS->szCompressed;
5438 int szFinal = pCDS->szUncompressed;
5442 if( pCsr->pCurrent->aData ){
5443 aBuf = pCsr->pCurrent->aData;
5445 aBuf = aFree = sqlite3_malloc64(sz);
5449 FILE *pFile = pCsr->pFile;
5451 pFile = ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
5453 rc = zipfileReadData(pFile, aBuf, sz, pCsr->pCurrent->iDataOff,
5454 &pCsr->base.pVtab->zErrMsg
5458 if( rc==SQLITE_OK ){
5459 if( i==5 && pCDS->iCompression ){
5460 zipfileInflate(ctx, aBuf, sz, szFinal);
5462 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
5465 sqlite3_free(aFree);
5467 /* Figure out if this is a directory or a zero-sized file. Consider
5468 ** it to be a directory either if the mode suggests so, or if
5469 ** the final character in the name is '/'. */
5470 u32 mode = pCDS->iExternalAttr >> 16;
5471 if( !(mode & S_IFDIR) && pCDS->zFile[pCDS->nFile-1]!='/' ){
5472 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
5478 case 6: /* method */
5479 sqlite3_result_int(ctx, pCDS->iCompression);
5483 sqlite3_result_int64(ctx, pCsr->iId);
5491 ** Return TRUE if the cursor is at EOF.
5493 static int zipfileEof(sqlite3_vtab_cursor *cur){
5494 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5499 ** If aBlob is not NULL, then it points to a buffer nBlob bytes in size
5500 ** containing an entire zip archive image. Or, if aBlob is NULL, then pFile
5501 ** is guaranteed to be a file-handle open on a zip file.
5503 ** This function attempts to locate the EOCD record within the zip archive
5504 ** and populate *pEOCD with the results of decoding it. SQLITE_OK is
5505 ** returned if successful. Otherwise, an SQLite error code is returned and
5506 ** an English language error message may be left in virtual-table pTab.
5508 static int zipfileReadEOCD(
5509 ZipfileTab *pTab, /* Return errors here */
5510 const u8 *aBlob, /* Pointer to in-memory file image */
5511 int nBlob, /* Size of aBlob[] in bytes */
5512 FILE *pFile, /* Read from this file if aBlob==0 */
5513 ZipfileEOCD *pEOCD /* Object to populate */
5515 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
5516 int nRead; /* Bytes to read from file */
5520 i64 iOff; /* Offset to read from */
5521 i64 szFile; /* Total size of file in bytes */
5522 fseek(pFile, 0, SEEK_END);
5523 szFile = (i64)ftell(pFile);
5525 memset(pEOCD, 0, sizeof(ZipfileEOCD));
5528 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
5529 iOff = szFile - nRead;
5530 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
5532 nRead = (int)(MIN(nBlob, ZIPFILE_BUFFER_SIZE));
5533 aRead = (u8*)&aBlob[nBlob-nRead];
5536 if( rc==SQLITE_OK ){
5539 /* Scan backwards looking for the signature bytes */
5540 for(i=nRead-20; i>=0; i--){
5541 if( aRead[i]==0x50 && aRead[i+1]==0x4b
5542 && aRead[i+2]==0x05 && aRead[i+3]==0x06
5548 pTab->base.zErrMsg = sqlite3_mprintf(
5549 "cannot find end of central directory record"
5551 return SQLITE_ERROR;
5555 pEOCD->iDisk = zipfileRead16(aRead);
5556 pEOCD->iFirstDisk = zipfileRead16(aRead);
5557 pEOCD->nEntry = zipfileRead16(aRead);
5558 pEOCD->nEntryTotal = zipfileRead16(aRead);
5559 pEOCD->nSize = zipfileRead32(aRead);
5560 pEOCD->iOffset = zipfileRead32(aRead);
5567 ** Add object pNew to the linked list that begins at ZipfileTab.pFirstEntry
5568 ** and ends with pLastEntry. If argument pBefore is NULL, then pNew is added
5569 ** to the end of the list. Otherwise, it is added to the list immediately
5570 ** before pBefore (which is guaranteed to be a part of said list).
5572 static void zipfileAddEntry(
5574 ZipfileEntry *pBefore,
5577 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5578 assert( pNew->pNext==0 );
5580 if( pTab->pFirstEntry==0 ){
5581 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5583 assert( pTab->pLastEntry->pNext==0 );
5584 pTab->pLastEntry->pNext = pNew;
5585 pTab->pLastEntry = pNew;
5589 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5590 pNew->pNext = pBefore;
5595 static int zipfileLoadDirectory(ZipfileTab *pTab, const u8 *aBlob, int nBlob){
5601 rc = zipfileReadEOCD(pTab, aBlob, nBlob, pTab->pWriteFd, &eocd);
5602 iOff = eocd.iOffset;
5603 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5604 ZipfileEntry *pNew = 0;
5605 rc = zipfileGetEntry(pTab, aBlob, nBlob, pTab->pWriteFd, iOff, &pNew);
5607 if( rc==SQLITE_OK ){
5608 zipfileAddEntry(pTab, 0, pNew);
5609 iOff += ZIPFILE_CDS_FIXED_SZ;
5610 iOff += (int)pNew->cds.nExtra + pNew->cds.nFile + pNew->cds.nComment;
5617 ** xFilter callback.
5619 static int zipfileFilter(
5620 sqlite3_vtab_cursor *cur,
5621 int idxNum, const char *idxStr,
5622 int argc, sqlite3_value **argv
5624 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
5625 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
5626 const char *zFile = 0; /* Zip file to scan */
5627 int rc = SQLITE_OK; /* Return Code */
5628 int bInMemory = 0; /* True for an in-memory zipfile */
5630 zipfileResetCursor(pCsr);
5633 zFile = pTab->zFile;
5634 }else if( idxNum==0 ){
5635 zipfileCursorErr(pCsr, "zipfile() function requires an argument");
5636 return SQLITE_ERROR;
5637 }else if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5638 const u8 *aBlob = (const u8*)sqlite3_value_blob(argv[0]);
5639 int nBlob = sqlite3_value_bytes(argv[0]);
5640 assert( pTab->pFirstEntry==0 );
5641 rc = zipfileLoadDirectory(pTab, aBlob, nBlob);
5642 pCsr->pFreeEntry = pTab->pFirstEntry;
5643 pTab->pFirstEntry = pTab->pLastEntry = 0;
5644 if( rc!=SQLITE_OK ) return rc;
5647 zFile = (const char*)sqlite3_value_text(argv[0]);
5650 if( 0==pTab->pWriteFd && 0==bInMemory ){
5651 pCsr->pFile = fopen(zFile, "rb");
5652 if( pCsr->pFile==0 ){
5653 zipfileCursorErr(pCsr, "cannot open file: %s", zFile);
5656 rc = zipfileReadEOCD(pTab, 0, 0, pCsr->pFile, &pCsr->eocd);
5657 if( rc==SQLITE_OK ){
5658 if( pCsr->eocd.nEntry==0 ){
5661 pCsr->iNextOff = pCsr->eocd.iOffset;
5662 rc = zipfileNext(cur);
5668 pCsr->pCurrent = pCsr->pFreeEntry ? pCsr->pFreeEntry : pTab->pFirstEntry;
5669 rc = zipfileNext(cur);
5676 ** xBestIndex callback.
5678 static int zipfileBestIndex(
5680 sqlite3_index_info *pIdxInfo
5686 for(i=0; i<pIdxInfo->nConstraint; i++){
5687 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
5688 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
5689 if( pCons->usable==0 ){
5691 }else if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
5695 pIdxInfo->estimatedCost = 1000.0;
5697 pIdxInfo->aConstraintUsage[idx].argvIndex = 1;
5698 pIdxInfo->aConstraintUsage[idx].omit = 1;
5699 pIdxInfo->idxNum = 1;
5700 }else if( unusable ){
5701 return SQLITE_CONSTRAINT;
5706 static ZipfileEntry *zipfileNewEntry(const char *zPath){
5708 pNew = sqlite3_malloc(sizeof(ZipfileEntry));
5710 memset(pNew, 0, sizeof(ZipfileEntry));
5711 pNew->cds.zFile = sqlite3_mprintf("%s", zPath);
5712 if( pNew->cds.zFile==0 ){
5720 static int zipfileSerializeLFH(ZipfileEntry *pEntry, u8 *aBuf){
5721 ZipfileCDS *pCds = &pEntry->cds;
5726 /* Write the LFH itself */
5727 zipfileWrite32(a, ZIPFILE_SIGNATURE_LFH);
5728 zipfileWrite16(a, pCds->iVersionExtract);
5729 zipfileWrite16(a, pCds->flags);
5730 zipfileWrite16(a, pCds->iCompression);
5731 zipfileWrite16(a, pCds->mTime);
5732 zipfileWrite16(a, pCds->mDate);
5733 zipfileWrite32(a, pCds->crc32);
5734 zipfileWrite32(a, pCds->szCompressed);
5735 zipfileWrite32(a, pCds->szUncompressed);
5736 zipfileWrite16(a, (u16)pCds->nFile);
5737 zipfileWrite16(a, pCds->nExtra);
5738 assert( a==&aBuf[ZIPFILE_LFH_FIXED_SZ] );
5740 /* Add the file name */
5741 memcpy(a, pCds->zFile, (int)pCds->nFile);
5742 a += (int)pCds->nFile;
5744 /* The "extra" data */
5745 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
5746 zipfileWrite16(a, 5);
5748 zipfileWrite32(a, pEntry->mUnixTime);
5753 static int zipfileAppendEntry(
5755 ZipfileEntry *pEntry,
5759 u8 *aBuf = pTab->aBuffer;
5763 nBuf = zipfileSerializeLFH(pEntry, aBuf);
5764 rc = zipfileAppendData(pTab, aBuf, nBuf);
5765 if( rc==SQLITE_OK ){
5766 pEntry->iDataOff = pTab->szCurrent;
5767 rc = zipfileAppendData(pTab, pData, nData);
5773 static int zipfileGetMode(
5774 sqlite3_value *pVal,
5775 int bIsDir, /* If true, default to directory */
5776 u32 *pMode, /* OUT: Mode value */
5777 char **pzErr /* OUT: Error message */
5779 const char *z = (const char*)sqlite3_value_text(pVal);
5782 mode = (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644));
5783 }else if( z[0]>='0' && z[0]<='9' ){
5784 mode = (unsigned int)sqlite3_value_int(pVal);
5786 const char zTemplate[11] = "-rwxrwxrwx";
5788 if( strlen(z)!=10 ) goto parse_error;
5790 case '-': mode |= S_IFREG; break;
5791 case 'd': mode |= S_IFDIR; break;
5792 case 'l': mode |= S_IFLNK; break;
5793 default: goto parse_error;
5795 for(i=1; i<10; i++){
5796 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5797 else if( z[i]!='-' ) goto parse_error;
5800 if( ((mode & S_IFDIR)==0)==bIsDir ){
5801 /* The "mode" attribute is a directory, but data has been specified.
5802 ** Or vice-versa - no data but "mode" is a file or symlink. */
5803 *pzErr = sqlite3_mprintf("zipfile: mode does not match data");
5804 return SQLITE_CONSTRAINT;
5810 *pzErr = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5811 return SQLITE_ERROR;
5815 ** Both (const char*) arguments point to nul-terminated strings. Argument
5816 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5817 ** identical, ignoring any trailing '/' character in either path. */
5818 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5819 int nA = (int)strlen(zA);
5820 if( nA>0 && zA[nA-1]=='/' ) nA--;
5821 if( nB>0 && zB[nB-1]=='/' ) nB--;
5822 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5826 static int zipfileBegin(sqlite3_vtab *pVtab){
5827 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5830 assert( pTab->pWriteFd==0 );
5831 if( pTab->zFile==0 || pTab->zFile[0]==0 ){
5832 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: missing filename");
5833 return SQLITE_ERROR;
5836 /* Open a write fd on the file. Also load the entire central directory
5837 ** structure into memory. During the transaction any new file data is
5838 ** appended to the archive file, but the central directory is accumulated
5839 ** in main-memory until the transaction is committed. */
5840 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5841 if( pTab->pWriteFd==0 ){
5842 pTab->base.zErrMsg = sqlite3_mprintf(
5843 "zipfile: failed to open file %s for writing", pTab->zFile
5847 fseek(pTab->pWriteFd, 0, SEEK_END);
5848 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5849 rc = zipfileLoadDirectory(pTab, 0, 0);
5852 if( rc!=SQLITE_OK ){
5853 zipfileCleanupTransaction(pTab);
5860 ** Return the current time as a 32-bit timestamp in UNIX epoch format (like
5863 static u32 zipfileTime(void){
5864 sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
5866 if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
5868 pVfs->xCurrentTimeInt64(pVfs, &ms);
5869 ret = (u32)((ms/1000) - ((i64)24405875 * 8640));
5872 pVfs->xCurrentTime(pVfs, &day);
5873 ret = (u32)((day - 2440587.5) * 86400);
5879 ** Return a 32-bit timestamp in UNIX epoch format.
5881 ** If the value passed as the only argument is either NULL or an SQL NULL,
5882 ** return the current time. Otherwise, return the value stored in (*pVal)
5883 ** cast to a 32-bit unsigned integer.
5885 static u32 zipfileGetTime(sqlite3_value *pVal){
5886 if( pVal==0 || sqlite3_value_type(pVal)==SQLITE_NULL ){
5887 return zipfileTime();
5889 return (u32)sqlite3_value_int64(pVal);
5893 ** Unless it is NULL, entry pOld is currently part of the pTab->pFirstEntry
5894 ** linked list. Remove it from the list and free the object.
5896 static void zipfileRemoveEntryFromList(ZipfileTab *pTab, ZipfileEntry *pOld){
5899 for(pp=&pTab->pFirstEntry; (*pp)!=pOld; pp=&((*pp)->pNext));
5901 zipfileEntryFree(pOld);
5908 static int zipfileUpdate(
5909 sqlite3_vtab *pVtab,
5911 sqlite3_value **apVal,
5912 sqlite_int64 *pRowid
5914 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5915 int rc = SQLITE_OK; /* Return Code */
5916 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5918 u32 mode = 0; /* Mode for new entry */
5919 u32 mTime = 0; /* Modification time for new entry */
5920 i64 sz = 0; /* Uncompressed size */
5921 const char *zPath = 0; /* Path for new entry */
5922 int nPath = 0; /* strlen(zPath) */
5923 const u8 *pData = 0; /* Pointer to buffer containing content */
5924 int nData = 0; /* Size of pData buffer in bytes */
5925 int iMethod = 0; /* Compression method for new entry */
5926 u8 *pFree = 0; /* Free this */
5927 char *zFree = 0; /* Also free this */
5928 ZipfileEntry *pOld = 0;
5929 ZipfileEntry *pOld2 = 0;
5930 int bUpdate = 0; /* True for an update that modifies "name" */
5934 if( pTab->pWriteFd==0 ){
5935 rc = zipfileBegin(pVtab);
5936 if( rc!=SQLITE_OK ) return rc;
5939 /* If this is a DELETE or UPDATE, find the archive entry to delete. */
5940 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5941 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5942 int nDelete = (int)strlen(zDelete);
5944 const char *zUpdate = (const char*)sqlite3_value_text(apVal[1]);
5945 if( zUpdate && zipfileComparePath(zUpdate, zDelete, nDelete)!=0 ){
5949 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5950 if( zipfileComparePath(pOld->cds.zFile, zDelete, nDelete)==0 ){
5953 assert( pOld->pNext );
5958 /* Check that "sz" and "rawdata" are both NULL: */
5959 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL ){
5960 zipfileTableErr(pTab, "sz must be NULL");
5961 rc = SQLITE_CONSTRAINT;
5963 if( sqlite3_value_type(apVal[6])!=SQLITE_NULL ){
5964 zipfileTableErr(pTab, "rawdata must be NULL");
5965 rc = SQLITE_CONSTRAINT;
5968 if( rc==SQLITE_OK ){
5969 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5970 /* data=NULL. A directory */
5973 /* Value specified for "data", and possibly "method". This must be
5974 ** a regular file or a symlink. */
5975 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5976 int nIn = sqlite3_value_bytes(apVal[7]);
5977 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5979 iMethod = sqlite3_value_int(apVal[8]);
5983 if( iMethod!=0 && iMethod!=8 ){
5984 zipfileTableErr(pTab, "unknown compression method: %d", iMethod);
5985 rc = SQLITE_CONSTRAINT;
5987 if( bAuto || iMethod ){
5989 rc = zipfileDeflate(aIn, nIn, &pFree, &nCmp, &pTab->base.zErrMsg);
5990 if( rc==SQLITE_OK ){
5991 if( iMethod || nCmp<nIn ){
5998 iCrc32 = crc32(0, aIn, nIn);
6003 if( rc==SQLITE_OK ){
6004 rc = zipfileGetMode(apVal[3], bIsDir, &mode, &pTab->base.zErrMsg);
6007 if( rc==SQLITE_OK ){
6008 zPath = (const char*)sqlite3_value_text(apVal[2]);
6009 if( zPath==0 ) zPath = "";
6010 nPath = (int)strlen(zPath);
6011 mTime = zipfileGetTime(apVal[4]);
6014 if( rc==SQLITE_OK && bIsDir ){
6015 /* For a directory, check that the last character in the path is a
6016 ** '/'. This appears to be required for compatibility with info-zip
6017 ** (the unzip command on unix). It does not create directories
6019 if( nPath<=0 || zPath[nPath-1]!='/' ){
6020 zFree = sqlite3_mprintf("%s/", zPath);
6021 zPath = (const char*)zFree;
6026 nPath = (int)strlen(zPath);
6031 /* Check that we're not inserting a duplicate entry -OR- updating an
6032 ** entry with a path, thereby making it into a duplicate. */
6033 if( (pOld==0 || bUpdate) && rc==SQLITE_OK ){
6035 for(p=pTab->pFirstEntry; p; p=p->pNext){
6036 if( zipfileComparePath(p->cds.zFile, zPath, nPath)==0 ){
6037 switch( sqlite3_vtab_on_conflict(pTab->db) ){
6038 case SQLITE_IGNORE: {
6039 goto zipfile_update_done;
6041 case SQLITE_REPLACE: {
6046 zipfileTableErr(pTab, "duplicate name: \"%s\"", zPath);
6047 rc = SQLITE_CONSTRAINT;
6056 if( rc==SQLITE_OK ){
6057 /* Create the new CDS record. */
6058 pNew = zipfileNewEntry(zPath);
6062 pNew->cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6063 pNew->cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6064 pNew->cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6065 pNew->cds.iCompression = (u16)iMethod;
6066 zipfileMtimeToDos(&pNew->cds, mTime);
6067 pNew->cds.crc32 = iCrc32;
6068 pNew->cds.szCompressed = nData;
6069 pNew->cds.szUncompressed = (u32)sz;
6070 pNew->cds.iExternalAttr = (mode<<16);
6071 pNew->cds.iOffset = (u32)pTab->szCurrent;
6072 pNew->cds.nFile = (u16)nPath;
6073 pNew->mUnixTime = (u32)mTime;
6074 rc = zipfileAppendEntry(pTab, pNew, pData, nData);
6075 zipfileAddEntry(pTab, pOld, pNew);
6080 if( rc==SQLITE_OK && (pOld || pOld2) ){
6082 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6083 if( pCsr->pCurrent && (pCsr->pCurrent==pOld || pCsr->pCurrent==pOld2) ){
6084 pCsr->pCurrent = pCsr->pCurrent->pNext;
6089 zipfileRemoveEntryFromList(pTab, pOld);
6090 zipfileRemoveEntryFromList(pTab, pOld2);
6093 zipfile_update_done:
6094 sqlite3_free(pFree);
6095 sqlite3_free(zFree);
6099 static int zipfileSerializeEOCD(ZipfileEOCD *p, u8 *aBuf){
6101 zipfileWrite32(a, ZIPFILE_SIGNATURE_EOCD);
6102 zipfileWrite16(a, p->iDisk);
6103 zipfileWrite16(a, p->iFirstDisk);
6104 zipfileWrite16(a, p->nEntry);
6105 zipfileWrite16(a, p->nEntryTotal);
6106 zipfileWrite32(a, p->nSize);
6107 zipfileWrite32(a, p->iOffset);
6108 zipfileWrite16(a, 0); /* Size of trailing comment in bytes*/
6113 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
6114 int nBuf = zipfileSerializeEOCD(p, pTab->aBuffer);
6115 assert( nBuf==ZIPFILE_EOCD_FIXED_SZ );
6116 return zipfileAppendData(pTab, pTab->aBuffer, nBuf);
6120 ** Serialize the CDS structure into buffer aBuf[]. Return the number
6121 ** of bytes written.
6123 static int zipfileSerializeCDS(ZipfileEntry *pEntry, u8 *aBuf){
6125 ZipfileCDS *pCDS = &pEntry->cds;
6127 if( pEntry->aExtra==0 ){
6131 zipfileWrite32(a, ZIPFILE_SIGNATURE_CDS);
6132 zipfileWrite16(a, pCDS->iVersionMadeBy);
6133 zipfileWrite16(a, pCDS->iVersionExtract);
6134 zipfileWrite16(a, pCDS->flags);
6135 zipfileWrite16(a, pCDS->iCompression);
6136 zipfileWrite16(a, pCDS->mTime);
6137 zipfileWrite16(a, pCDS->mDate);
6138 zipfileWrite32(a, pCDS->crc32);
6139 zipfileWrite32(a, pCDS->szCompressed);
6140 zipfileWrite32(a, pCDS->szUncompressed);
6141 assert( a==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
6142 zipfileWrite16(a, pCDS->nFile);
6143 zipfileWrite16(a, pCDS->nExtra);
6144 zipfileWrite16(a, pCDS->nComment);
6145 zipfileWrite16(a, pCDS->iDiskStart);
6146 zipfileWrite16(a, pCDS->iInternalAttr);
6147 zipfileWrite32(a, pCDS->iExternalAttr);
6148 zipfileWrite32(a, pCDS->iOffset);
6150 memcpy(a, pCDS->zFile, pCDS->nFile);
6153 if( pEntry->aExtra ){
6154 int n = (int)pCDS->nExtra + (int)pCDS->nComment;
6155 memcpy(a, pEntry->aExtra, n);
6158 assert( pCDS->nExtra==9 );
6159 zipfileWrite16(a, ZIPFILE_EXTRA_TIMESTAMP);
6160 zipfileWrite16(a, 5);
6162 zipfileWrite32(a, pEntry->mUnixTime);
6168 static int zipfileCommit(sqlite3_vtab *pVtab){
6169 ZipfileTab *pTab = (ZipfileTab*)pVtab;
6171 if( pTab->pWriteFd ){
6172 i64 iOffset = pTab->szCurrent;
6177 /* Write out all entries */
6178 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
6179 int n = zipfileSerializeCDS(p, pTab->aBuffer);
6180 rc = zipfileAppendData(pTab, pTab->aBuffer, n);
6184 /* Write out the EOCD record */
6186 eocd.iFirstDisk = 0;
6187 eocd.nEntry = (u16)nEntry;
6188 eocd.nEntryTotal = (u16)nEntry;
6189 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
6190 eocd.iOffset = (u32)iOffset;
6191 rc = zipfileAppendEOCD(pTab, &eocd);
6193 zipfileCleanupTransaction(pTab);
6198 static int zipfileRollback(sqlite3_vtab *pVtab){
6199 return zipfileCommit(pVtab);
6202 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
6204 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
6205 if( iId==pCsr->iId ) break;
6210 static void zipfileFunctionCds(
6211 sqlite3_context *context,
6213 sqlite3_value **argv
6216 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
6219 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
6221 ZipfileCDS *p = &pCsr->pCurrent->cds;
6222 char *zRes = sqlite3_mprintf("{"
6223 "\"version-made-by\" : %u, "
6224 "\"version-to-extract\" : %u, "
6226 "\"compression\" : %u, "
6230 "\"compressed-size\" : %u, "
6231 "\"uncompressed-size\" : %u, "
6232 "\"file-name-length\" : %u, "
6233 "\"extra-field-length\" : %u, "
6234 "\"file-comment-length\" : %u, "
6235 "\"disk-number-start\" : %u, "
6236 "\"internal-attr\" : %u, "
6237 "\"external-attr\" : %u, "
6238 "\"offset\" : %u }",
6239 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
6240 (u32)p->flags, (u32)p->iCompression,
6241 (u32)p->mTime, (u32)p->mDate,
6242 (u32)p->crc32, (u32)p->szCompressed,
6243 (u32)p->szUncompressed, (u32)p->nFile,
6244 (u32)p->nExtra, (u32)p->nComment,
6245 (u32)p->iDiskStart, (u32)p->iInternalAttr,
6246 (u32)p->iExternalAttr, (u32)p->iOffset
6250 sqlite3_result_error_nomem(context);
6252 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
6259 ** xFindFunction method.
6261 static int zipfileFindFunction(
6262 sqlite3_vtab *pVtab, /* Virtual table handle */
6263 int nArg, /* Number of SQL function arguments */
6264 const char *zName, /* Name of SQL function */
6265 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
6266 void **ppArg /* OUT: User data for *pxFunc */
6268 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
6269 *pxFunc = zipfileFunctionCds;
6270 *ppArg = (void*)pVtab;
6276 typedef struct ZipfileBuffer ZipfileBuffer;
6277 struct ZipfileBuffer {
6278 u8 *a; /* Pointer to buffer */
6279 int n; /* Size of buffer in bytes */
6280 int nAlloc; /* Byte allocated at a[] */
6283 typedef struct ZipfileCtx ZipfileCtx;
6290 static int zipfileBufferGrow(ZipfileBuffer *pBuf, int nByte){
6291 if( pBuf->n+nByte>pBuf->nAlloc ){
6293 sqlite3_int64 nNew = pBuf->n ? pBuf->n*2 : 512;
6294 int nReq = pBuf->n + nByte;
6296 while( nNew<nReq ) nNew = nNew*2;
6297 aNew = sqlite3_realloc64(pBuf->a, nNew);
6298 if( aNew==0 ) return SQLITE_NOMEM;
6300 pBuf->nAlloc = (int)nNew;
6306 ** xStep() callback for the zipfile() aggregate. This can be called in
6307 ** any of the following ways:
6309 ** SELECT zipfile(name,data) ...
6310 ** SELECT zipfile(name,mode,mtime,data) ...
6311 ** SELECT zipfile(name,mode,mtime,data,method) ...
6313 void zipfileStep(sqlite3_context *pCtx, int nVal, sqlite3_value **apVal){
6314 ZipfileCtx *p; /* Aggregate function context */
6315 ZipfileEntry e; /* New entry to add to zip archive */
6317 sqlite3_value *pName = 0;
6318 sqlite3_value *pMode = 0;
6319 sqlite3_value *pMtime = 0;
6320 sqlite3_value *pData = 0;
6321 sqlite3_value *pMethod = 0;
6328 int iMethod = -1; /* Compression method to use (0 or 8) */
6330 const u8 *aData = 0; /* Possibly compressed data for new entry */
6331 int nData = 0; /* Size of aData[] in bytes */
6332 int szUncompressed = 0; /* Size of data before compression */
6333 u8 *aFree = 0; /* Free this before returning */
6334 u32 iCrc32 = 0; /* crc32 of uncompressed data */
6336 char *zName = 0; /* Path (name) of new entry */
6337 int nName = 0; /* Size of zName in bytes */
6338 char *zFree = 0; /* Free this before returning */
6341 memset(&e, 0, sizeof(e));
6342 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6345 /* Martial the arguments into stack variables */
6346 if( nVal!=2 && nVal!=4 && nVal!=5 ){
6347 zErr = sqlite3_mprintf("wrong number of arguments to function zipfile()");
6349 goto zipfile_step_out;
6363 /* Check that the 'name' parameter looks ok. */
6364 zName = (char*)sqlite3_value_text(pName);
6365 nName = sqlite3_value_bytes(pName);
6367 zErr = sqlite3_mprintf("first argument to zipfile() must be non-NULL");
6369 goto zipfile_step_out;
6372 /* Inspect the 'method' parameter. This must be either 0 (store), 8 (use
6373 ** deflate compression) or NULL (choose automatically). */
6374 if( pMethod && SQLITE_NULL!=sqlite3_value_type(pMethod) ){
6375 iMethod = (int)sqlite3_value_int64(pMethod);
6376 if( iMethod!=0 && iMethod!=8 ){
6377 zErr = sqlite3_mprintf("illegal method value: %d", iMethod);
6379 goto zipfile_step_out;
6383 /* Now inspect the data. If this is NULL, then the new entry must be a
6384 ** directory. Otherwise, figure out whether or not the data should
6385 ** be deflated or simply stored in the zip archive. */
6386 if( sqlite3_value_type(pData)==SQLITE_NULL ){
6390 aData = sqlite3_value_blob(pData);
6391 szUncompressed = nData = sqlite3_value_bytes(pData);
6392 iCrc32 = crc32(0, aData, nData);
6393 if( iMethod<0 || iMethod==8 ){
6395 rc = zipfileDeflate(aData, nData, &aFree, &nOut, &zErr);
6396 if( rc!=SQLITE_OK ){
6397 goto zipfile_step_out;
6399 if( iMethod==8 || nOut<nData ){
6409 /* Decode the "mode" argument. */
6410 rc = zipfileGetMode(pMode, bIsDir, &mode, &zErr);
6411 if( rc ) goto zipfile_step_out;
6413 /* Decode the "mtime" argument. */
6414 e.mUnixTime = zipfileGetTime(pMtime);
6416 /* If this is a directory entry, ensure that there is exactly one '/'
6417 ** at the end of the path. Or, if this is not a directory and the path
6418 ** ends in '/' it is an error. */
6420 if( nName>0 && zName[nName-1]=='/' ){
6421 zErr = sqlite3_mprintf("non-directory name must not end with /");
6423 goto zipfile_step_out;
6426 if( nName==0 || zName[nName-1]!='/' ){
6427 zName = zFree = sqlite3_mprintf("%s/", zName);
6430 goto zipfile_step_out;
6432 nName = (int)strlen(zName);
6434 while( nName>1 && zName[nName-2]=='/' ) nName--;
6438 /* Assemble the ZipfileEntry object for the new zip archive entry */
6439 e.cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
6440 e.cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
6441 e.cds.flags = ZIPFILE_NEWENTRY_FLAGS;
6442 e.cds.iCompression = (u16)iMethod;
6443 zipfileMtimeToDos(&e.cds, (u32)e.mUnixTime);
6444 e.cds.crc32 = iCrc32;
6445 e.cds.szCompressed = nData;
6446 e.cds.szUncompressed = szUncompressed;
6447 e.cds.iExternalAttr = (mode<<16);
6448 e.cds.iOffset = p->body.n;
6449 e.cds.nFile = (u16)nName;
6450 e.cds.zFile = zName;
6452 /* Append the LFH to the body of the new archive */
6453 nByte = ZIPFILE_LFH_FIXED_SZ + e.cds.nFile + 9;
6454 if( (rc = zipfileBufferGrow(&p->body, nByte)) ) goto zipfile_step_out;
6455 p->body.n += zipfileSerializeLFH(&e, &p->body.a[p->body.n]);
6457 /* Append the data to the body of the new archive */
6459 if( (rc = zipfileBufferGrow(&p->body, nData)) ) goto zipfile_step_out;
6460 memcpy(&p->body.a[p->body.n], aData, nData);
6464 /* Append the CDS record to the directory of the new archive */
6465 nByte = ZIPFILE_CDS_FIXED_SZ + e.cds.nFile + 9;
6466 if( (rc = zipfileBufferGrow(&p->cds, nByte)) ) goto zipfile_step_out;
6467 p->cds.n += zipfileSerializeCDS(&e, &p->cds.a[p->cds.n]);
6469 /* Increment the count of entries in the archive */
6473 sqlite3_free(aFree);
6474 sqlite3_free(zFree);
6477 sqlite3_result_error(pCtx, zErr, -1);
6479 sqlite3_result_error_code(pCtx, rc);
6486 ** xFinalize() callback for zipfile aggregate function.
6488 void zipfileFinal(sqlite3_context *pCtx){
6494 p = (ZipfileCtx*)sqlite3_aggregate_context(pCtx, sizeof(ZipfileCtx));
6497 memset(&eocd, 0, sizeof(eocd));
6498 eocd.nEntry = (u16)p->nEntry;
6499 eocd.nEntryTotal = (u16)p->nEntry;
6500 eocd.nSize = p->cds.n;
6501 eocd.iOffset = p->body.n;
6503 nZip = p->body.n + p->cds.n + ZIPFILE_EOCD_FIXED_SZ;
6504 aZip = (u8*)sqlite3_malloc64(nZip);
6506 sqlite3_result_error_nomem(pCtx);
6508 memcpy(aZip, p->body.a, p->body.n);
6509 memcpy(&aZip[p->body.n], p->cds.a, p->cds.n);
6510 zipfileSerializeEOCD(&eocd, &aZip[p->body.n + p->cds.n]);
6511 sqlite3_result_blob(pCtx, aZip, (int)nZip, zipfileFree);
6515 sqlite3_free(p->body.a);
6516 sqlite3_free(p->cds.a);
6521 ** Register the "zipfile" virtual table.
6523 static int zipfileRegister(sqlite3 *db){
6524 static sqlite3_module zipfileModule = {
6526 zipfileConnect, /* xCreate */
6527 zipfileConnect, /* xConnect */
6528 zipfileBestIndex, /* xBestIndex */
6529 zipfileDisconnect, /* xDisconnect */
6530 zipfileDisconnect, /* xDestroy */
6531 zipfileOpen, /* xOpen - open a cursor */
6532 zipfileClose, /* xClose - close a cursor */
6533 zipfileFilter, /* xFilter - configure scan constraints */
6534 zipfileNext, /* xNext - advance a cursor */
6535 zipfileEof, /* xEof - check for end of scan */
6536 zipfileColumn, /* xColumn - read data */
6537 0, /* xRowid - read data */
6538 zipfileUpdate, /* xUpdate */
6539 zipfileBegin, /* xBegin */
6541 zipfileCommit, /* xCommit */
6542 zipfileRollback, /* xRollback */
6543 zipfileFindFunction, /* xFindMethod */
6547 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
6548 if( rc==SQLITE_OK ) rc = sqlite3_overload_function(db, "zipfile_cds", -1);
6549 if( rc==SQLITE_OK ){
6550 rc = sqlite3_create_function(db, "zipfile", -1, SQLITE_UTF8, 0, 0,
6551 zipfileStep, zipfileFinal
6556 #else /* SQLITE_OMIT_VIRTUALTABLE */
6557 # define zipfileRegister(x) SQLITE_OK
6563 int sqlite3_zipfile_init(
6566 const sqlite3_api_routines *pApi
6568 SQLITE_EXTENSION_INIT2(pApi);
6569 (void)pzErrMsg; /* Unused parameter */
6570 return zipfileRegister(db);
6573 /************************* End ../ext/misc/zipfile.c ********************/
6574 /************************* Begin ../ext/misc/sqlar.c ******************/
6578 ** The author disclaims copyright to this source code. In place of
6579 ** a legal notice, here is a blessing:
6581 ** May you do good and not evil.
6582 ** May you find forgiveness for yourself and forgive others.
6583 ** May you share freely, never taking more than you give.
6585 ******************************************************************************
6587 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
6588 ** for working with sqlar archives and used by the shell tool's built-in
6591 /* #include "sqlite3ext.h" */
6592 SQLITE_EXTENSION_INIT1
6597 ** Implementation of the "sqlar_compress(X)" SQL function.
6599 ** If the type of X is SQLITE_BLOB, and compressing that blob using
6600 ** zlib utility function compress() yields a smaller blob, return the
6601 ** compressed blob. Otherwise, return a copy of X.
6603 ** SQLar uses the "zlib format" for compressed content. The zlib format
6604 ** contains a two-byte identification header and a four-byte checksum at
6605 ** the end. This is different from ZIP which uses the raw deflate format.
6607 ** Future enhancements to SQLar might add support for new compression formats.
6608 ** If so, those new formats will be identified by alternative headers in the
6611 static void sqlarCompressFunc(
6612 sqlite3_context *context,
6614 sqlite3_value **argv
6617 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
6618 const Bytef *pData = sqlite3_value_blob(argv[0]);
6619 uLong nData = sqlite3_value_bytes(argv[0]);
6620 uLongf nOut = compressBound(nData);
6623 pOut = (Bytef*)sqlite3_malloc(nOut);
6625 sqlite3_result_error_nomem(context);
6628 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
6629 sqlite3_result_error(context, "error in compress()", -1);
6630 }else if( nOut<nData ){
6631 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
6633 sqlite3_result_value(context, argv[0]);
6638 sqlite3_result_value(context, argv[0]);
6643 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
6645 ** Parameter SZ is interpreted as an integer. If it is less than or
6646 ** equal to zero, then this function returns a copy of X. Or, if
6647 ** SZ is equal to the size of X when interpreted as a blob, also
6648 ** return a copy of X. Otherwise, decompress blob X using zlib
6649 ** utility function uncompress() and return the results (another
6652 static void sqlarUncompressFunc(
6653 sqlite3_context *context,
6655 sqlite3_value **argv
6661 sz = sqlite3_value_int(argv[1]);
6663 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
6664 sqlite3_result_value(context, argv[0]);
6666 const Bytef *pData= sqlite3_value_blob(argv[0]);
6667 Bytef *pOut = sqlite3_malloc(sz);
6668 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
6669 sqlite3_result_error(context, "error in uncompress()", -1);
6671 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
6681 int sqlite3_sqlar_init(
6684 const sqlite3_api_routines *pApi
6687 SQLITE_EXTENSION_INIT2(pApi);
6688 (void)pzErrMsg; /* Unused parameter */
6689 rc = sqlite3_create_function(db, "sqlar_compress", 1,
6690 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6691 sqlarCompressFunc, 0, 0);
6692 if( rc==SQLITE_OK ){
6693 rc = sqlite3_create_function(db, "sqlar_uncompress", 2,
6694 SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
6695 sqlarUncompressFunc, 0, 0);
6700 /************************* End ../ext/misc/sqlar.c ********************/
6702 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
6706 ** The author disclaims copyright to this source code. In place of
6707 ** a legal notice, here is a blessing:
6709 ** May you do good and not evil.
6710 ** May you find forgiveness for yourself and forgive others.
6711 ** May you share freely, never taking more than you give.
6713 *************************************************************************
6715 #if !defined(SQLITEEXPERT_H)
6716 #define SQLITEEXPERT_H 1
6717 /* #include "sqlite3.h" */
6719 typedef struct sqlite3expert sqlite3expert;
6722 ** Create a new sqlite3expert object.
6724 ** If successful, a pointer to the new object is returned and (*pzErr) set
6725 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
6726 ** an English-language error message. In this case it is the responsibility
6727 ** of the caller to eventually free the error message buffer using
6730 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
6733 ** Configure an sqlite3expert object.
6735 ** EXPERT_CONFIG_SAMPLE:
6736 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
6737 ** each candidate index. This involves scanning and sorting the entire
6738 ** contents of each user database table once for each candidate index
6739 ** associated with the table. For large databases, this can be
6740 ** prohibitively slow. This option allows the sqlite3expert object to
6741 ** be configured so that sqlite_stat1 data is instead generated based on a
6742 ** subset of each table, or so that no sqlite_stat1 data is used at all.
6744 ** A single integer argument is passed to this option. If the value is less
6745 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
6746 ** the analysis - indexes are recommended based on the database schema only.
6747 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
6748 ** generated for each candidate index (this is the default). Finally, if the
6749 ** value falls between 0 and 100, then it represents the percentage of user
6750 ** table rows that should be considered when generating sqlite_stat1 data.
6754 ** // Do not generate any sqlite_stat1 data
6755 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
6757 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
6758 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
6760 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
6762 #define EXPERT_CONFIG_SAMPLE 1 /* int */
6765 ** Specify zero or more SQL statements to be included in the analysis.
6767 ** Buffer zSql must contain zero or more complete SQL statements. This
6768 ** function parses all statements contained in the buffer and adds them
6769 ** to the internal list of statements to analyze. If successful, SQLITE_OK
6770 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
6771 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
6772 ** may be set to point to an English language error message. In this case
6773 ** the caller is responsible for eventually freeing the error message buffer
6774 ** using sqlite3_free().
6776 ** If an error does occur while processing one of the statements in the
6777 ** buffer passed as the second argument, none of the statements in the
6778 ** buffer are added to the analysis.
6780 ** This function must be called before sqlite3_expert_analyze(). If a call
6781 ** to this function is made on an sqlite3expert object that has already
6782 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
6783 ** immediately and no statements are added to the analysis.
6785 int sqlite3_expert_sql(
6786 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
6787 const char *zSql, /* SQL statement(s) to add */
6788 char **pzErr /* OUT: Error message (if any) */
6793 ** This function is called after the sqlite3expert object has been configured
6794 ** with all SQL statements using sqlite3_expert_sql() to actually perform
6795 ** the analysis. Once this function has been called, it is not possible to
6796 ** add further SQL statements to the analysis.
6798 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
6799 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
6800 ** point to a buffer containing an English language error message. In this
6801 ** case it is the responsibility of the caller to eventually free the buffer
6802 ** using sqlite3_free().
6804 ** If an error does occur within this function, the sqlite3expert object
6805 ** is no longer useful for any purpose. At that point it is no longer
6806 ** possible to add further SQL statements to the object or to re-attempt
6807 ** the analysis. The sqlite3expert object must still be freed using a call
6808 ** sqlite3_expert_destroy().
6810 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
6813 ** Return the total number of statements loaded using sqlite3_expert_sql().
6814 ** The total number of SQL statements may be different from the total number
6815 ** to calls to sqlite3_expert_sql().
6817 int sqlite3_expert_count(sqlite3expert*);
6820 ** Return a component of the report.
6822 ** This function is called after sqlite3_expert_analyze() to extract the
6823 ** results of the analysis. Each call to this function returns either a
6824 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
6825 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
6826 ** #define constants defined below.
6828 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
6829 ** information relating to a specific SQL statement. In these cases that
6830 ** SQL statement is identified by the value passed as the second argument.
6831 ** SQL statements are numbered from 0 in the order in which they are parsed.
6832 ** If an out-of-range value (less than zero or equal to or greater than the
6833 ** value returned by sqlite3_expert_count()) is passed as the second argument
6834 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
6836 ** EXPERT_REPORT_SQL:
6837 ** Return the text of SQL statement iStmt.
6839 ** EXPERT_REPORT_INDEXES:
6840 ** Return a buffer containing the CREATE INDEX statements for all recommended
6841 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
6844 ** EXPERT_REPORT_PLAN:
6845 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
6846 ** iStmt after the proposed indexes have been added to the database schema.
6848 ** EXPERT_REPORT_CANDIDATES:
6849 ** Return a pointer to a buffer containing the CREATE INDEX statements
6850 ** for all indexes that were tested (for all SQL statements). The iStmt
6851 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
6853 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
6856 ** Values for the third argument passed to sqlite3_expert_report().
6858 #define EXPERT_REPORT_SQL 1
6859 #define EXPERT_REPORT_INDEXES 2
6860 #define EXPERT_REPORT_PLAN 3
6861 #define EXPERT_REPORT_CANDIDATES 4
6864 ** Free an (sqlite3expert*) handle and all associated resources. There
6865 ** should be one call to this function for each successful call to
6866 ** sqlite3-expert_new().
6868 void sqlite3_expert_destroy(sqlite3expert*);
6870 #endif /* !defined(SQLITEEXPERT_H) */
6872 /************************* End ../ext/expert/sqlite3expert.h ********************/
6873 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
6877 ** The author disclaims copyright to this source code. In place of
6878 ** a legal notice, here is a blessing:
6880 ** May you do good and not evil.
6881 ** May you find forgiveness for yourself and forgive others.
6882 ** May you share freely, never taking more than you give.
6884 *************************************************************************
6886 /* #include "sqlite3expert.h" */
6891 #ifndef SQLITE_OMIT_VIRTUALTABLE
6893 /* typedef sqlite3_int64 i64; */
6894 /* typedef sqlite3_uint64 u64; */
6896 typedef struct IdxColumn IdxColumn;
6897 typedef struct IdxConstraint IdxConstraint;
6898 typedef struct IdxScan IdxScan;
6899 typedef struct IdxStatement IdxStatement;
6900 typedef struct IdxTable IdxTable;
6901 typedef struct IdxWrite IdxWrite;
6903 #define STRLEN (int)strlen
6906 ** A temp table name that we assume no user database will actually use.
6907 ** If this assumption proves incorrect triggers on the table with the
6908 ** conflicting name will be ignored.
6910 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
6913 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
6914 ** any other type of single-ended range constraint on a column).
6917 ** Used to temporarily link IdxConstraint objects into lists while
6918 ** creating candidate indexes.
6920 struct IdxConstraint {
6921 char *zColl; /* Collation sequence */
6922 int bRange; /* True for range, false for eq */
6923 int iCol; /* Constrained table column */
6924 int bFlag; /* Used by idxFindCompatible() */
6925 int bDesc; /* True if ORDER BY <expr> DESC */
6926 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
6927 IdxConstraint *pLink; /* See above */
6931 ** A single scan of a single table.
6934 IdxTable *pTab; /* Associated table object */
6935 int iDb; /* Database containing table zTable */
6936 i64 covering; /* Mask of columns required for cov. index */
6937 IdxConstraint *pOrder; /* ORDER BY columns */
6938 IdxConstraint *pEq; /* List of == constraints */
6939 IdxConstraint *pRange; /* List of < constraints */
6940 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
6944 ** Information regarding a single database table. Extracted from
6945 ** "PRAGMA table_info" by function idxGetTableInfo().
6954 char *zName; /* Table name */
6956 IdxTable *pNext; /* Next table in linked list of all tables */
6960 ** An object of the following type is created for each unique table/write-op
6961 ** seen. The objects are stored in a singly-linked list beginning at
6962 ** sqlite3expert.pWrite.
6966 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
6971 ** Each statement being analyzed is represented by an instance of this
6974 struct IdxStatement {
6975 int iId; /* Statement number */
6976 char *zSql; /* SQL statement */
6977 char *zIdx; /* Indexes */
6978 char *zEQP; /* Plan */
6979 IdxStatement *pNext;
6984 ** A hash table for storing strings. With space for a payload string
6985 ** with each entry. Methods are:
6992 #define IDX_HASH_SIZE 1023
6993 typedef struct IdxHashEntry IdxHashEntry;
6994 typedef struct IdxHash IdxHash;
6995 struct IdxHashEntry {
6996 char *zKey; /* nul-terminated key */
6997 char *zVal; /* nul-terminated value string */
6998 char *zVal2; /* nul-terminated value string 2 */
6999 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
7000 IdxHashEntry *pNext; /* Next entry in hash */
7003 IdxHashEntry *pFirst;
7004 IdxHashEntry *aHash[IDX_HASH_SIZE];
7008 ** sqlite3expert object.
7010 struct sqlite3expert {
7011 int iSample; /* Percentage of tables to sample for stat1 */
7012 sqlite3 *db; /* User database */
7013 sqlite3 *dbm; /* In-memory db for this analysis */
7014 sqlite3 *dbv; /* Vtab schema for this analysis */
7015 IdxTable *pTable; /* List of all IdxTable objects */
7016 IdxScan *pScan; /* List of scan objects */
7017 IdxWrite *pWrite; /* List of write objects */
7018 IdxStatement *pStatement; /* List of IdxStatement objects */
7019 int bRun; /* True once analysis has run */
7021 int rc; /* Error code from whereinfo hook */
7022 IdxHash hIdx; /* Hash containing all candidate indexes */
7023 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
7028 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
7029 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
7031 static void *idxMalloc(int *pRc, int nByte){
7033 assert( *pRc==SQLITE_OK );
7035 pRet = sqlite3_malloc(nByte);
7037 memset(pRet, 0, nByte);
7039 *pRc = SQLITE_NOMEM;
7045 ** Initialize an IdxHash hash table.
7047 static void idxHashInit(IdxHash *pHash){
7048 memset(pHash, 0, sizeof(IdxHash));
7052 ** Reset an IdxHash hash table.
7054 static void idxHashClear(IdxHash *pHash){
7056 for(i=0; i<IDX_HASH_SIZE; i++){
7057 IdxHashEntry *pEntry;
7058 IdxHashEntry *pNext;
7059 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
7060 pNext = pEntry->pHashNext;
7061 sqlite3_free(pEntry->zVal2);
7062 sqlite3_free(pEntry);
7065 memset(pHash, 0, sizeof(IdxHash));
7069 ** Return the index of the hash bucket that the string specified by the
7070 ** arguments to this function belongs.
7072 static int idxHashString(const char *z, int n){
7073 unsigned int ret = 0;
7076 ret += (ret<<3) + (unsigned char)(z[i]);
7078 return (int)(ret % IDX_HASH_SIZE);
7082 ** If zKey is already present in the hash table, return non-zero and do
7083 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
7084 ** the hash table passed as the second argument.
7086 static int idxHashAdd(
7092 int nKey = STRLEN(zKey);
7093 int iHash = idxHashString(zKey, nKey);
7094 int nVal = (zVal ? STRLEN(zVal) : 0);
7095 IdxHashEntry *pEntry;
7097 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
7098 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
7102 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
7104 pEntry->zKey = (char*)&pEntry[1];
7105 memcpy(pEntry->zKey, zKey, nKey);
7107 pEntry->zVal = &pEntry->zKey[nKey+1];
7108 memcpy(pEntry->zVal, zVal, nVal);
7110 pEntry->pHashNext = pHash->aHash[iHash];
7111 pHash->aHash[iHash] = pEntry;
7113 pEntry->pNext = pHash->pFirst;
7114 pHash->pFirst = pEntry;
7120 ** If zKey/nKey is present in the hash table, return a pointer to the
7121 ** hash-entry object.
7123 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
7125 IdxHashEntry *pEntry;
7126 if( nKey<0 ) nKey = STRLEN(zKey);
7127 iHash = idxHashString(zKey, nKey);
7129 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
7130 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
7138 ** If the hash table contains an entry with a key equal to the string
7139 ** passed as the final two arguments to this function, return a pointer
7140 ** to the payload string. Otherwise, if zKey/nKey is not present in the
7141 ** hash table, return NULL.
7143 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
7144 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
7145 if( pEntry ) return pEntry->zVal;
7150 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
7151 ** variable to point to a copy of nul-terminated string zColl.
7153 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
7154 IdxConstraint *pNew;
7155 int nColl = STRLEN(zColl);
7157 assert( *pRc==SQLITE_OK );
7158 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
7160 pNew->zColl = (char*)&pNew[1];
7161 memcpy(pNew->zColl, zColl, nColl+1);
7167 ** An error associated with database handle db has just occurred. Pass
7168 ** the error message to callback function xOut.
7170 static void idxDatabaseError(
7171 sqlite3 *db, /* Database handle */
7172 char **pzErrmsg /* Write error here */
7174 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
7178 ** Prepare an SQL statement.
7180 static int idxPrepareStmt(
7181 sqlite3 *db, /* Database handle to compile against */
7182 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
7183 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
7184 const char *zSql /* SQL statement to compile */
7186 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
7187 if( rc!=SQLITE_OK ){
7189 idxDatabaseError(db, pzErrmsg);
7195 ** Prepare an SQL statement using the results of a printf() formatting.
7197 static int idxPrintfPrepareStmt(
7198 sqlite3 *db, /* Database handle to compile against */
7199 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
7200 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
7201 const char *zFmt, /* printf() format of SQL statement */
7202 ... /* Trailing printf() arguments */
7208 zSql = sqlite3_vmprintf(zFmt, ap);
7212 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
7220 /*************************************************************************
7221 ** Beginning of virtual table implementation.
7223 typedef struct ExpertVtab ExpertVtab;
7227 sqlite3expert *pExpert;
7230 typedef struct ExpertCsr ExpertCsr;
7232 sqlite3_vtab_cursor base;
7233 sqlite3_stmt *pData;
7236 static char *expertDequote(const char *zIn){
7237 int n = STRLEN(zIn);
7238 char *zRet = sqlite3_malloc(n);
7240 assert( zIn[0]=='\'' );
7241 assert( zIn[n-1]=='\'' );
7246 for(iIn=1; iIn<(n-1); iIn++){
7247 if( zIn[iIn]=='\'' ){
7248 assert( zIn[iIn+1]=='\'' );
7251 zRet[iOut++] = zIn[iIn];
7260 ** This function is the implementation of both the xConnect and xCreate
7261 ** methods of the r-tree virtual table.
7263 ** argv[0] -> module name
7264 ** argv[1] -> database name
7265 ** argv[2] -> table name
7266 ** argv[...] -> column names...
7268 static int expertConnect(
7271 int argc, const char *const*argv,
7272 sqlite3_vtab **ppVtab,
7275 sqlite3expert *pExpert = (sqlite3expert*)pAux;
7280 *pzErr = sqlite3_mprintf("internal error!");
7283 char *zCreateTable = expertDequote(argv[3]);
7285 rc = sqlite3_declare_vtab(db, zCreateTable);
7286 if( rc==SQLITE_OK ){
7287 p = idxMalloc(&rc, sizeof(ExpertVtab));
7289 if( rc==SQLITE_OK ){
7290 p->pExpert = pExpert;
7291 p->pTab = pExpert->pTable;
7292 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
7294 sqlite3_free(zCreateTable);
7300 *ppVtab = (sqlite3_vtab*)p;
7304 static int expertDisconnect(sqlite3_vtab *pVtab){
7305 ExpertVtab *p = (ExpertVtab*)pVtab;
7310 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
7311 ExpertVtab *p = (ExpertVtab*)pVtab;
7316 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
7317 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
7318 SQLITE_INDEX_CONSTRAINT_LE;
7320 pScan = idxMalloc(&rc, sizeof(IdxScan));
7324 /* Link the new scan object into the list */
7325 pScan->pTab = p->pTab;
7326 pScan->pNextScan = p->pExpert->pScan;
7327 p->pExpert->pScan = pScan;
7329 /* Add the constraints to the IdxScan object */
7330 for(i=0; i<pIdxInfo->nConstraint; i++){
7331 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
7333 && pCons->iColumn>=0
7334 && p->pTab->aCol[pCons->iColumn].iPk==0
7335 && (pCons->op & opmask)
7337 IdxConstraint *pNew;
7338 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
7339 pNew = idxNewConstraint(&rc, zColl);
7341 pNew->iCol = pCons->iColumn;
7342 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
7343 pNew->pNext = pScan->pEq;
7347 pNew->pNext = pScan->pRange;
7348 pScan->pRange = pNew;
7352 pIdxInfo->aConstraintUsage[i].argvIndex = n;
7356 /* Add the ORDER BY to the IdxScan object */
7357 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
7358 int iCol = pIdxInfo->aOrderBy[i].iColumn;
7360 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
7363 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
7364 pNew->pNext = pScan->pOrder;
7365 pNew->pLink = pScan->pOrder;
7366 pScan->pOrder = pNew;
7373 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
7377 static int expertUpdate(
7378 sqlite3_vtab *pVtab,
7380 sqlite3_value **azData,
7381 sqlite_int64 *pRowid
7391 ** Virtual table module xOpen method.
7393 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
7397 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
7398 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
7403 ** Virtual table module xClose method.
7405 static int expertClose(sqlite3_vtab_cursor *cur){
7406 ExpertCsr *pCsr = (ExpertCsr*)cur;
7407 sqlite3_finalize(pCsr->pData);
7413 ** Virtual table module xEof method.
7415 ** Return non-zero if the cursor does not currently point to a valid
7416 ** record (i.e if the scan has finished), or zero otherwise.
7418 static int expertEof(sqlite3_vtab_cursor *cur){
7419 ExpertCsr *pCsr = (ExpertCsr*)cur;
7420 return pCsr->pData==0;
7424 ** Virtual table module xNext method.
7426 static int expertNext(sqlite3_vtab_cursor *cur){
7427 ExpertCsr *pCsr = (ExpertCsr*)cur;
7430 assert( pCsr->pData );
7431 rc = sqlite3_step(pCsr->pData);
7432 if( rc!=SQLITE_ROW ){
7433 rc = sqlite3_finalize(pCsr->pData);
7443 ** Virtual table module xRowid method.
7445 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
7452 ** Virtual table module xColumn method.
7454 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
7455 ExpertCsr *pCsr = (ExpertCsr*)cur;
7456 sqlite3_value *pVal;
7457 pVal = sqlite3_column_value(pCsr->pData, i);
7459 sqlite3_result_value(ctx, pVal);
7465 ** Virtual table module xFilter method.
7467 static int expertFilter(
7468 sqlite3_vtab_cursor *cur,
7469 int idxNum, const char *idxStr,
7470 int argc, sqlite3_value **argv
7472 ExpertCsr *pCsr = (ExpertCsr*)cur;
7473 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
7474 sqlite3expert *pExpert = pVtab->pExpert;
7481 rc = sqlite3_finalize(pCsr->pData);
7483 if( rc==SQLITE_OK ){
7484 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
7485 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
7489 if( rc==SQLITE_OK ){
7490 rc = expertNext(cur);
7495 static int idxRegisterVtab(sqlite3expert *p){
7496 static sqlite3_module expertModule = {
7498 expertConnect, /* xCreate - create a table */
7499 expertConnect, /* xConnect - connect to an existing table */
7500 expertBestIndex, /* xBestIndex - Determine search strategy */
7501 expertDisconnect, /* xDisconnect - Disconnect from a table */
7502 expertDisconnect, /* xDestroy - Drop a table */
7503 expertOpen, /* xOpen - open a cursor */
7504 expertClose, /* xClose - close a cursor */
7505 expertFilter, /* xFilter - configure scan constraints */
7506 expertNext, /* xNext - advance a cursor */
7507 expertEof, /* xEof */
7508 expertColumn, /* xColumn - read data */
7509 expertRowid, /* xRowid - read data */
7510 expertUpdate, /* xUpdate - write data */
7511 0, /* xBegin - begin transaction */
7512 0, /* xSync - sync transaction */
7513 0, /* xCommit - commit transaction */
7514 0, /* xRollback - rollback transaction */
7515 0, /* xFindFunction - function overloading */
7516 0, /* xRename - rename the table */
7519 0, /* xRollbackTo */
7520 0, /* xShadowName */
7523 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
7526 ** End of virtual table implementation.
7527 *************************************************************************/
7529 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
7530 ** is called, set it to the return value of sqlite3_finalize() before
7531 ** returning. Otherwise, discard the sqlite3_finalize() return value.
7533 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
7534 int rc = sqlite3_finalize(pStmt);
7535 if( *pRc==SQLITE_OK ) *pRc = rc;
7539 ** Attempt to allocate an IdxTable structure corresponding to table zTab
7540 ** in the main database of connection db. If successful, set (*ppOut) to
7541 ** point to the new object and return SQLITE_OK. Otherwise, return an
7542 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
7543 ** set to point to an error string.
7545 ** It is the responsibility of the caller to eventually free either the
7546 ** IdxTable object or error message using sqlite3_free().
7548 static int idxGetTableInfo(
7549 sqlite3 *db, /* Database connection to read details from */
7550 const char *zTab, /* Table name */
7551 IdxTable **ppOut, /* OUT: New object (if successful) */
7552 char **pzErrmsg /* OUT: Error message (if not) */
7554 sqlite3_stmt *p1 = 0;
7556 int nTab = STRLEN(zTab);
7557 int nByte = sizeof(IdxTable) + nTab + 1;
7562 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
7563 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7564 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7565 nByte += 1 + STRLEN(zCol);
7566 rc = sqlite3_table_column_metadata(
7567 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7569 nByte += 1 + STRLEN(zCol);
7572 rc2 = sqlite3_reset(p1);
7573 if( rc==SQLITE_OK ) rc = rc2;
7575 nByte += sizeof(IdxColumn) * nCol;
7576 if( rc==SQLITE_OK ){
7577 pNew = idxMalloc(&rc, nByte);
7579 if( rc==SQLITE_OK ){
7580 pNew->aCol = (IdxColumn*)&pNew[1];
7582 pCsr = (char*)&pNew->aCol[nCol];
7586 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
7587 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
7588 int nCopy = STRLEN(zCol) + 1;
7589 pNew->aCol[nCol].zName = pCsr;
7590 pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
7591 memcpy(pCsr, zCol, nCopy);
7594 rc = sqlite3_table_column_metadata(
7595 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
7597 if( rc==SQLITE_OK ){
7598 nCopy = STRLEN(zCol) + 1;
7599 pNew->aCol[nCol].zColl = pCsr;
7600 memcpy(pCsr, zCol, nCopy);
7606 idxFinalize(&rc, p1);
7608 if( rc!=SQLITE_OK ){
7613 memcpy(pNew->zName, zTab, nTab+1);
7621 ** This function is a no-op if *pRc is set to anything other than
7622 ** SQLITE_OK when it is called.
7624 ** If *pRc is initially set to SQLITE_OK, then the text specified by
7625 ** the printf() style arguments is appended to zIn and the result returned
7626 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
7627 ** zIn before returning.
7629 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
7633 int nIn = zIn ? STRLEN(zIn) : 0;
7636 if( *pRc==SQLITE_OK ){
7637 zAppend = sqlite3_vmprintf(zFmt, ap);
7639 nAppend = STRLEN(zAppend);
7640 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
7642 if( zAppend && zRet ){
7643 if( nIn ) memcpy(zRet, zIn, nIn);
7644 memcpy(&zRet[nIn], zAppend, nAppend+1);
7648 *pRc = SQLITE_NOMEM;
7650 sqlite3_free(zAppend);
7658 ** Return true if zId must be quoted in order to use it as an SQL
7659 ** identifier, or false otherwise.
7661 static int idxIdentifierRequiresQuotes(const char *zId){
7663 for(i=0; zId[i]; i++){
7665 && !(zId[i]>='0' && zId[i]<='9')
7666 && !(zId[i]>='a' && zId[i]<='z')
7667 && !(zId[i]>='A' && zId[i]<='Z')
7676 ** This function appends an index column definition suitable for constraint
7677 ** pCons to the string passed as zIn and returns the result.
7679 static char *idxAppendColDefn(
7680 int *pRc, /* IN/OUT: Error code */
7681 char *zIn, /* Column defn accumulated so far */
7682 IdxTable *pTab, /* Table index will be created on */
7683 IdxConstraint *pCons
7686 IdxColumn *p = &pTab->aCol[pCons->iCol];
7687 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
7689 if( idxIdentifierRequiresQuotes(p->zName) ){
7690 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
7692 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
7695 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
7696 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
7697 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
7699 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
7704 zRet = idxAppendText(pRc, zRet, " DESC");
7710 ** Search database dbm for an index compatible with the one idxCreateFromCons()
7711 ** would create from arguments pScan, pEq and pTail. If no error occurs and
7712 ** such an index is found, return non-zero. Or, if no such index is found,
7715 ** If an error occurs, set *pRc to an SQLite error code and return zero.
7717 static int idxFindCompatible(
7718 int *pRc, /* OUT: Error code */
7719 sqlite3* dbm, /* Database to search */
7720 IdxScan *pScan, /* Scan for table to search for index on */
7721 IdxConstraint *pEq, /* List of == constraints */
7722 IdxConstraint *pTail /* List of range constraints */
7724 const char *zTbl = pScan->pTab->zName;
7725 sqlite3_stmt *pIdxList = 0;
7726 IdxConstraint *pIter;
7727 int nEq = 0; /* Number of elements in pEq */
7730 /* Count the elements in list pEq */
7731 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
7733 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
7734 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
7736 IdxConstraint *pT = pTail;
7737 sqlite3_stmt *pInfo = 0;
7738 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
7740 /* Zero the IdxConstraint.bFlag values in the pEq list */
7741 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
7743 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
7744 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
7745 int iIdx = sqlite3_column_int(pInfo, 0);
7746 int iCol = sqlite3_column_int(pInfo, 1);
7747 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
7750 for(pIter=pEq; pIter; pIter=pIter->pLink){
7751 if( pIter->bFlag ) continue;
7752 if( pIter->iCol!=iCol ) continue;
7753 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
7763 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
7771 idxFinalize(&rc, pInfo);
7773 if( rc==SQLITE_OK && bMatch ){
7774 sqlite3_finalize(pIdxList);
7778 idxFinalize(&rc, pIdxList);
7784 static int idxCreateFromCons(
7788 IdxConstraint *pTail
7790 sqlite3 *dbm = p->dbm;
7792 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
7793 IdxTable *pTab = pScan->pTab;
7796 IdxConstraint *pCons;
7800 for(pCons=pEq; pCons; pCons=pCons->pLink){
7801 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7803 for(pCons=pTail; pCons; pCons=pCons->pLink){
7804 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
7807 if( rc==SQLITE_OK ){
7808 /* Hash the list of columns to come up with a name for the index */
7809 const char *zTable = pScan->pTab->zName;
7810 char *zName; /* Index name */
7812 for(i=0; zCols[i]; i++){
7813 h += ((h<<3) + zCols[i]);
7815 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
7819 if( idxIdentifierRequiresQuotes(zTable) ){
7820 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
7822 zFmt = "CREATE INDEX %s ON %s(%s)";
7824 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
7828 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
7829 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
7831 sqlite3_free(zName);
7836 sqlite3_free(zCols);
7842 ** Return true if list pList (linked by IdxConstraint.pLink) contains
7843 ** a constraint compatible with *p. Otherwise return false.
7845 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
7846 IdxConstraint *pCmp;
7847 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
7848 if( p->iCol==pCmp->iCol ) return 1;
7853 static int idxCreateFromWhere(
7855 IdxScan *pScan, /* Create indexes for this scan */
7856 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
7858 IdxConstraint *p1 = 0;
7859 IdxConstraint *pCon;
7862 /* Gather up all the == constraints. */
7863 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
7864 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7870 /* Create an index using the == constraints collected above. And the
7871 ** range constraint/ORDER BY terms passed in by the caller, if any. */
7872 rc = idxCreateFromCons(p, pScan, p1, pTail);
7874 /* If no range/ORDER BY passed by the caller, create a version of the
7875 ** index for each range constraint. */
7877 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
7878 assert( pCon->pLink==0 );
7879 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
7880 rc = idxCreateFromCons(p, pScan, p1, pCon);
7889 ** Create candidate indexes in database [dbm] based on the data in
7890 ** linked-list pScan.
7892 static int idxCreateCandidates(sqlite3expert *p){
7896 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
7897 rc = idxCreateFromWhere(p, pIter, 0);
7898 if( rc==SQLITE_OK && pIter->pOrder ){
7899 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
7907 ** Free all elements of the linked list starting at pConstraint.
7909 static void idxConstraintFree(IdxConstraint *pConstraint){
7910 IdxConstraint *pNext;
7913 for(p=pConstraint; p; p=pNext){
7920 ** Free all elements of the linked list starting from pScan up until pLast
7921 ** (pLast is not freed).
7923 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
7926 for(p=pScan; p!=pLast; p=pNext){
7927 pNext = p->pNextScan;
7928 idxConstraintFree(p->pOrder);
7929 idxConstraintFree(p->pEq);
7930 idxConstraintFree(p->pRange);
7936 ** Free all elements of the linked list starting from pStatement up
7937 ** until pLast (pLast is not freed).
7939 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
7941 IdxStatement *pNext;
7942 for(p=pStatement; p!=pLast; p=pNext){
7944 sqlite3_free(p->zEQP);
7945 sqlite3_free(p->zIdx);
7951 ** Free the linked list of IdxTable objects starting at pTab.
7953 static void idxTableFree(IdxTable *pTab){
7956 for(pIter=pTab; pIter; pIter=pNext){
7957 pNext = pIter->pNext;
7958 sqlite3_free(pIter);
7963 ** Free the linked list of IdxWrite objects starting at pTab.
7965 static void idxWriteFree(IdxWrite *pTab){
7968 for(pIter=pTab; pIter; pIter=pNext){
7969 pNext = pIter->pNext;
7970 sqlite3_free(pIter);
7977 ** This function is called after candidate indexes have been created. It
7978 ** runs all the queries to see which indexes they prefer, and populates
7979 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7983 char **pzErr /* OUT: Error message (sqlite3_malloc) */
7985 IdxStatement *pStmt;
7986 sqlite3 *dbm = p->dbm;
7992 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7993 IdxHashEntry *pEntry;
7994 sqlite3_stmt *pExplain = 0;
7995 idxHashClear(&hIdx);
7996 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7997 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7999 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
8000 /* int iId = sqlite3_column_int(pExplain, 0); */
8001 /* int iParent = sqlite3_column_int(pExplain, 1); */
8002 /* int iNotUsed = sqlite3_column_int(pExplain, 2); */
8003 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
8007 if( !zDetail ) continue;
8008 nDetail = STRLEN(zDetail);
8010 for(i=0; i<nDetail; i++){
8011 const char *zIdx = 0;
8012 if( i+13<nDetail && memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
8013 zIdx = &zDetail[i+13];
8014 }else if( i+22<nDetail
8015 && memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0
8017 zIdx = &zDetail[i+22];
8022 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
8025 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
8027 idxHashAdd(&rc, &hIdx, zSql, 0);
8028 if( rc ) goto find_indexes_out;
8034 if( zDetail[0]!='-' ){
8035 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%s\n", zDetail);
8039 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8040 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
8043 idxFinalize(&rc, pExplain);
8047 idxHashClear(&hIdx);
8051 static int idxAuthCallback(
8057 const char *zTrigger
8062 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
8063 if( sqlite3_stricmp(zDb, "main")==0 ){
8064 sqlite3expert *p = (sqlite3expert*)pCtx;
8066 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
8067 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
8071 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
8072 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
8075 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
8076 if( rc==SQLITE_OK ){
8077 pWrite->pTab = pTab;
8079 pWrite->pNext = p->pWrite;
8089 static int idxProcessOneTrigger(
8094 static const char *zInt = UNIQUE_TABLE_NAME;
8095 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
8096 IdxTable *pTab = pWrite->pTab;
8097 const char *zTab = pTab->zName;
8099 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
8100 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
8102 sqlite3_stmt *pSelect = 0;
8106 /* Create the table and its triggers in the temp schema */
8107 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
8108 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
8109 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
8110 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
8112 idxFinalize(&rc, pSelect);
8114 /* Rename the table in the temp schema to zInt */
8115 if( rc==SQLITE_OK ){
8116 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
8120 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
8125 switch( pWrite->eOp ){
8126 case SQLITE_INSERT: {
8128 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
8129 for(i=0; i<pTab->nCol; i++){
8130 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
8132 zWrite = idxAppendText(&rc, zWrite, ")");
8135 case SQLITE_UPDATE: {
8137 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
8138 for(i=0; i<pTab->nCol; i++){
8139 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
8146 assert( pWrite->eOp==SQLITE_DELETE );
8147 if( rc==SQLITE_OK ){
8148 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
8149 if( zWrite==0 ) rc = SQLITE_NOMEM;
8154 if( rc==SQLITE_OK ){
8155 sqlite3_stmt *pX = 0;
8156 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
8157 idxFinalize(&rc, pX);
8158 if( rc!=SQLITE_OK ){
8159 idxDatabaseError(p->dbv, pzErr);
8162 sqlite3_free(zWrite);
8164 if( rc==SQLITE_OK ){
8165 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
8171 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
8174 IdxWrite *pFirst = p->pWrite;
8176 while( rc==SQLITE_OK && pFirst!=pEnd ){
8178 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
8179 rc = idxProcessOneTrigger(p, pIter, pzErr);
8189 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
8190 int rc = idxRegisterVtab(p);
8191 sqlite3_stmt *pSchema = 0;
8193 /* For each table in the main db schema:
8195 ** 1) Add an entry to the p->pTable list, and
8196 ** 2) Create the equivalent virtual table in dbv.
8198 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
8199 "SELECT type, name, sql, 1 FROM sqlite_master "
8200 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
8202 "SELECT type, name, sql, 2 FROM sqlite_master "
8203 "WHERE type = 'trigger'"
8204 " AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
8207 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
8208 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
8209 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
8210 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
8212 if( zType[0]=='v' || zType[1]=='r' ){
8213 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
8216 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
8217 if( rc==SQLITE_OK ){
8221 pTab->pNext = p->pTable;
8224 /* The statement the vtab will pass to sqlite3_declare_vtab() */
8225 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
8226 for(i=0; i<pTab->nCol; i++){
8227 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
8228 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
8231 zInner = idxAppendText(&rc, zInner, ")");
8233 /* The CVT statement to create the vtab */
8234 zOuter = idxAppendText(&rc, 0,
8235 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
8237 if( rc==SQLITE_OK ){
8238 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
8240 sqlite3_free(zInner);
8241 sqlite3_free(zOuter);
8245 idxFinalize(&rc, pSchema);
8249 struct IdxSampleCtx {
8251 double target; /* Target nRet/nRow value */
8252 double nRow; /* Number of rows seen */
8253 double nRet; /* Number of rows returned */
8256 static void idxSampleFunc(
8257 sqlite3_context *pCtx,
8259 sqlite3_value **argv
8261 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
8269 bRet = (p->nRet / p->nRow) <= p->target;
8272 sqlite3_randomness(2, (void*)&rnd);
8273 bRet = ((int)rnd % 100) <= p->iTarget;
8277 sqlite3_result_int(pCtx, bRet);
8279 p->nRet += (double)bRet;
8285 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
8286 i64 iVal; /* SQLITE_INTEGER value */
8287 double rVal; /* SQLITE_FLOAT value */
8288 int nByte; /* Bytes of space allocated at z */
8289 int n; /* Size of buffer z */
8290 char *z; /* SQLITE_TEXT/BLOB value */
8295 ** Implementation of scalar function rem().
8297 static void idxRemFunc(
8298 sqlite3_context *pCtx,
8300 sqlite3_value **argv
8302 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
8303 struct IdxRemSlot *pSlot;
8307 iSlot = sqlite3_value_int(argv[0]);
8308 assert( iSlot<=p->nSlot );
8309 pSlot = &p->aSlot[iSlot];
8311 switch( pSlot->eType ){
8316 case SQLITE_INTEGER:
8317 sqlite3_result_int64(pCtx, pSlot->iVal);
8321 sqlite3_result_double(pCtx, pSlot->rVal);
8325 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8329 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
8333 pSlot->eType = sqlite3_value_type(argv[1]);
8334 switch( pSlot->eType ){
8339 case SQLITE_INTEGER:
8340 pSlot->iVal = sqlite3_value_int64(argv[1]);
8344 pSlot->rVal = sqlite3_value_double(argv[1]);
8349 int nByte = sqlite3_value_bytes(argv[1]);
8350 if( nByte>pSlot->nByte ){
8351 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
8353 sqlite3_result_error_nomem(pCtx);
8356 pSlot->nByte = nByte*2;
8360 if( pSlot->eType==SQLITE_BLOB ){
8361 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
8363 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
8370 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
8373 "SELECT max(i.seqno) FROM "
8374 " sqlite_master AS s, "
8375 " pragma_index_list(s.name) AS l, "
8376 " pragma_index_info(l.name) AS i "
8377 "WHERE s.type = 'table'";
8378 sqlite3_stmt *pMax = 0;
8381 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
8382 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
8383 *pnMax = sqlite3_column_int(pMax, 0) + 1;
8385 idxFinalize(&rc, pMax);
8390 static int idxPopulateOneStat1(
8392 sqlite3_stmt *pIndexXInfo,
8393 sqlite3_stmt *pWriteStat,
8403 sqlite3_stmt *pQuery = 0;
8407 assert( p->iSample>0 );
8409 /* Formulate the query text */
8410 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
8411 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
8412 const char *zComma = zCols==0 ? "" : ", ";
8413 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
8414 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
8415 zCols = idxAppendText(&rc, zCols,
8416 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
8418 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
8420 sqlite3_reset(pIndexXInfo);
8421 if( rc==SQLITE_OK ){
8422 if( p->iSample==100 ){
8423 zQuery = sqlite3_mprintf(
8424 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
8427 zQuery = sqlite3_mprintf(
8428 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
8432 sqlite3_free(zCols);
8433 sqlite3_free(zOrder);
8435 /* Formulate the query text */
8436 if( rc==SQLITE_OK ){
8437 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8438 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
8440 sqlite3_free(zQuery);
8442 if( rc==SQLITE_OK ){
8443 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
8445 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8446 IdxHashEntry *pEntry;
8448 for(i=0; i<=nCol; i++) aStat[i] = 1;
8449 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
8451 for(i=0; i<nCol; i++){
8452 if( sqlite3_column_int(pQuery, i)==0 ) break;
8454 for(/*no-op*/; i<nCol; i++){
8459 if( rc==SQLITE_OK ){
8461 zStat = sqlite3_mprintf("%d", s0);
8462 if( zStat==0 ) rc = SQLITE_NOMEM;
8463 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
8464 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
8468 if( rc==SQLITE_OK ){
8469 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
8470 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
8471 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
8472 sqlite3_step(pWriteStat);
8473 rc = sqlite3_reset(pWriteStat);
8476 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
8478 assert( pEntry->zVal2==0 );
8479 pEntry->zVal2 = zStat;
8481 sqlite3_free(zStat);
8484 sqlite3_free(aStat);
8485 idxFinalize(&rc, pQuery);
8490 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
8494 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8495 if( rc!=SQLITE_OK ) return rc;
8497 zSql = sqlite3_mprintf(
8498 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
8500 if( zSql==0 ) return SQLITE_NOMEM;
8501 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
8508 ** This function is called as part of sqlite3_expert_analyze(). Candidate
8509 ** indexes have already been created in database sqlite3expert.dbm, this
8510 ** function populates sqlite_stat1 table in the same database.
8512 ** The stat1 data is generated by querying the
8514 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
8517 struct IdxRemCtx *pCtx = 0;
8518 struct IdxSampleCtx samplectx;
8520 i64 iPrev = -100000;
8521 sqlite3_stmt *pAllIndex = 0;
8522 sqlite3_stmt *pIndexXInfo = 0;
8523 sqlite3_stmt *pWrite = 0;
8525 const char *zAllIndex =
8526 "SELECT s.rowid, s.name, l.name FROM "
8527 " sqlite_master AS s, "
8528 " pragma_index_list(s.name) AS l "
8529 "WHERE s.type = 'table'";
8530 const char *zIndexXInfo =
8531 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
8532 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
8534 /* If iSample==0, no sqlite_stat1 data is required. */
8535 if( p->iSample==0 ) return SQLITE_OK;
8537 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
8538 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
8540 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
8542 if( rc==SQLITE_OK ){
8543 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
8544 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
8547 if( rc==SQLITE_OK ){
8548 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
8549 rc = sqlite3_create_function(
8550 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
8553 if( rc==SQLITE_OK ){
8554 rc = sqlite3_create_function(
8555 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
8559 if( rc==SQLITE_OK ){
8560 pCtx->nSlot = nMax+1;
8561 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
8563 if( rc==SQLITE_OK ){
8564 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
8566 if( rc==SQLITE_OK ){
8567 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
8570 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
8571 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
8572 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
8573 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
8574 if( p->iSample<100 && iPrev!=iRowid ){
8575 samplectx.target = (double)p->iSample / 100.0;
8576 samplectx.iTarget = p->iSample;
8577 samplectx.nRow = 0.0;
8578 samplectx.nRet = 0.0;
8579 rc = idxBuildSampleTable(p, zTab);
8580 if( rc!=SQLITE_OK ) break;
8582 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
8585 if( rc==SQLITE_OK && p->iSample<100 ){
8586 rc = sqlite3_exec(p->dbv,
8587 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
8591 idxFinalize(&rc, pAllIndex);
8592 idxFinalize(&rc, pIndexXInfo);
8593 idxFinalize(&rc, pWrite);
8595 for(i=0; i<pCtx->nSlot; i++){
8596 sqlite3_free(pCtx->aSlot[i].z);
8600 if( rc==SQLITE_OK ){
8601 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
8604 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
8609 ** Allocate a new sqlite3expert object.
8611 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
8613 sqlite3expert *pNew;
8615 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
8617 /* Open two in-memory databases to work with. The "vtab database" (dbv)
8618 ** will contain a virtual table corresponding to each real table in
8619 ** the user database schema, and a copy of each view. It is used to
8620 ** collect information regarding the WHERE, ORDER BY and other clauses
8621 ** of the user's query.
8623 if( rc==SQLITE_OK ){
8625 pNew->iSample = 100;
8626 rc = sqlite3_open(":memory:", &pNew->dbv);
8628 if( rc==SQLITE_OK ){
8629 rc = sqlite3_open(":memory:", &pNew->dbm);
8630 if( rc==SQLITE_OK ){
8631 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
8636 /* Copy the entire schema of database [db] into [dbm]. */
8637 if( rc==SQLITE_OK ){
8639 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
8640 "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
8641 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
8643 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
8644 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
8645 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
8647 idxFinalize(&rc, pSql);
8650 /* Create the vtab schema */
8651 if( rc==SQLITE_OK ){
8652 rc = idxCreateVtabSchema(pNew, pzErrmsg);
8655 /* Register the auth callback with dbv */
8656 if( rc==SQLITE_OK ){
8657 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
8660 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
8661 ** return the new sqlite3expert handle. */
8662 if( rc!=SQLITE_OK ){
8663 sqlite3_expert_destroy(pNew);
8670 ** Configure an sqlite3expert object.
8672 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
8677 case EXPERT_CONFIG_SAMPLE: {
8678 int iVal = va_arg(ap, int);
8679 if( iVal<0 ) iVal = 0;
8680 if( iVal>100 ) iVal = 100;
8685 rc = SQLITE_NOTFOUND;
8694 ** Add an SQL statement to the analysis.
8696 int sqlite3_expert_sql(
8697 sqlite3expert *p, /* From sqlite3_expert_new() */
8698 const char *zSql, /* SQL statement to add */
8699 char **pzErr /* OUT: Error message (if any) */
8701 IdxScan *pScanOrig = p->pScan;
8702 IdxStatement *pStmtOrig = p->pStatement;
8704 const char *zStmt = zSql;
8706 if( p->bRun ) return SQLITE_MISUSE;
8708 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
8709 sqlite3_stmt *pStmt = 0;
8710 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
8711 if( rc==SQLITE_OK ){
8714 const char *z = sqlite3_sql(pStmt);
8716 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
8717 if( rc==SQLITE_OK ){
8718 pNew->zSql = (char*)&pNew[1];
8719 memcpy(pNew->zSql, z, n+1);
8720 pNew->pNext = p->pStatement;
8721 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
8722 p->pStatement = pNew;
8724 sqlite3_finalize(pStmt);
8727 idxDatabaseError(p->dbv, pzErr);
8731 if( rc!=SQLITE_OK ){
8732 idxScanFree(p->pScan, pScanOrig);
8733 idxStatementFree(p->pStatement, pStmtOrig);
8734 p->pScan = pScanOrig;
8735 p->pStatement = pStmtOrig;
8741 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
8743 IdxHashEntry *pEntry;
8745 /* Do trigger processing to collect any extra IdxScan structures */
8746 rc = idxProcessTriggers(p, pzErr);
8748 /* Create candidate indexes within the in-memory database file */
8749 if( rc==SQLITE_OK ){
8750 rc = idxCreateCandidates(p);
8753 /* Generate the stat1 data */
8754 if( rc==SQLITE_OK ){
8755 rc = idxPopulateStat1(p, pzErr);
8758 /* Formulate the EXPERT_REPORT_CANDIDATES text */
8759 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
8760 p->zCandidates = idxAppendText(&rc, p->zCandidates,
8761 "%s;%s%s\n", pEntry->zVal,
8762 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
8766 /* Figure out which of the candidate indexes are preferred by the query
8767 ** planner and report the results to the user. */
8768 if( rc==SQLITE_OK ){
8769 rc = idxFindIndexes(p, pzErr);
8772 if( rc==SQLITE_OK ){
8779 ** Return the total number of statements that have been added to this
8780 ** sqlite3expert using sqlite3_expert_sql().
8782 int sqlite3_expert_count(sqlite3expert *p){
8784 if( p->pStatement ) nRet = p->pStatement->iId+1;
8789 ** Return a component of the report.
8791 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
8792 const char *zRet = 0;
8793 IdxStatement *pStmt;
8795 if( p->bRun==0 ) return 0;
8796 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
8798 case EXPERT_REPORT_SQL:
8799 if( pStmt ) zRet = pStmt->zSql;
8801 case EXPERT_REPORT_INDEXES:
8802 if( pStmt ) zRet = pStmt->zIdx;
8804 case EXPERT_REPORT_PLAN:
8805 if( pStmt ) zRet = pStmt->zEQP;
8807 case EXPERT_REPORT_CANDIDATES:
8808 zRet = p->zCandidates;
8815 ** Free an sqlite3expert object.
8817 void sqlite3_expert_destroy(sqlite3expert *p){
8819 sqlite3_close(p->dbm);
8820 sqlite3_close(p->dbv);
8821 idxScanFree(p->pScan, 0);
8822 idxStatementFree(p->pStatement, 0);
8823 idxTableFree(p->pTable);
8824 idxWriteFree(p->pWrite);
8825 idxHashClear(&p->hIdx);
8826 sqlite3_free(p->zCandidates);
8831 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
8833 /************************* End ../ext/expert/sqlite3expert.c ********************/
8835 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
8836 /************************* Begin ../ext/misc/dbdata.c ******************/
8840 ** The author disclaims copyright to this source code. In place of
8841 ** a legal notice, here is a blessing:
8843 ** May you do good and not evil.
8844 ** May you find forgiveness for yourself and forgive others.
8845 ** May you share freely, never taking more than you give.
8847 ******************************************************************************
8849 ** This file contains an implementation of two eponymous virtual tables,
8850 ** "sqlite_dbdata" and "sqlite_dbptr". Both modules require that the
8851 ** "sqlite_dbpage" eponymous virtual table be available.
8854 ** sqlite_dbdata is used to extract data directly from a database b-tree
8855 ** page and its associated overflow pages, bypassing the b-tree layer.
8856 ** The table schema is equivalent to:
8858 ** CREATE TABLE sqlite_dbdata(
8863 ** schema TEXT HIDDEN
8866 ** IMPORTANT: THE VIRTUAL TABLE SCHEMA ABOVE IS SUBJECT TO CHANGE. IN THE
8867 ** FUTURE NEW NON-HIDDEN COLUMNS MAY BE ADDED BETWEEN "value" AND
8870 ** Each page of the database is inspected. If it cannot be interpreted as
8871 ** a b-tree page, or if it is a b-tree page containing 0 entries, the
8872 ** sqlite_dbdata table contains no rows for that page. Otherwise, the
8873 ** table contains one row for each field in the record associated with
8874 ** each cell on the page. For intkey b-trees, the key value is stored in
8877 ** For example, for the database:
8879 ** CREATE TABLE t1(a, b); -- root page is page 2
8880 ** INSERT INTO t1(rowid, a, b) VALUES(5, 'v', 'five');
8881 ** INSERT INTO t1(rowid, a, b) VALUES(10, 'x', 'ten');
8883 ** the sqlite_dbdata table contains, as well as from entries related to
8884 ** page 1, content equivalent to:
8886 ** INSERT INTO sqlite_dbdata(pgno, cell, field, value) VALUES
8889 ** (2, 0, 1, 'five'),
8892 ** (2, 1, 1, 'ten' );
8894 ** If database corruption is encountered, this module does not report an
8895 ** error. Instead, it attempts to extract as much data as possible and
8896 ** ignores the corruption.
8899 ** The sqlite_dbptr table has the following schema:
8901 ** CREATE TABLE sqlite_dbptr(
8904 ** schema TEXT HIDDEN
8907 ** It contains one entry for each b-tree pointer between a parent and
8908 ** child page in the database.
8910 #if !defined(SQLITEINT_H)
8911 /* #include "sqlite3ext.h" */
8913 /* typedef unsigned char u8; */
8916 SQLITE_EXTENSION_INIT1
8920 #define DBDATA_PADDING_BYTES 100
8922 typedef struct DbdataTable DbdataTable;
8923 typedef struct DbdataCursor DbdataCursor;
8926 struct DbdataCursor {
8927 sqlite3_vtab_cursor base; /* Base class. Must be first */
8928 sqlite3_stmt *pStmt; /* For fetching database pages */
8930 int iPgno; /* Current page number */
8931 u8 *aPage; /* Buffer containing page */
8932 int nPage; /* Size of aPage[] in bytes */
8933 int nCell; /* Number of cells on aPage[] */
8934 int iCell; /* Current cell number */
8935 int bOnePage; /* True to stop after one page */
8937 sqlite3_int64 iRowid;
8939 /* Only for the sqlite_dbdata table */
8940 u8 *pRec; /* Buffer containing current record */
8941 int nRec; /* Size of pRec[] in bytes */
8942 int nHdr; /* Size of header in bytes */
8943 int iField; /* Current field number */
8947 sqlite3_int64 iIntkey; /* Integer key value */
8951 struct DbdataTable {
8952 sqlite3_vtab base; /* Base class. Must be first */
8953 sqlite3 *db; /* The database connection */
8954 sqlite3_stmt *pStmt; /* For fetching database pages */
8955 int bPtr; /* True for sqlite3_dbptr table */
8958 /* Column and schema definitions for sqlite_dbdata */
8959 #define DBDATA_COLUMN_PGNO 0
8960 #define DBDATA_COLUMN_CELL 1
8961 #define DBDATA_COLUMN_FIELD 2
8962 #define DBDATA_COLUMN_VALUE 3
8963 #define DBDATA_COLUMN_SCHEMA 4
8964 #define DBDATA_SCHEMA \
8970 " schema TEXT HIDDEN" \
8973 /* Column and schema definitions for sqlite_dbptr */
8974 #define DBPTR_COLUMN_PGNO 0
8975 #define DBPTR_COLUMN_CHILD 1
8976 #define DBPTR_COLUMN_SCHEMA 2
8977 #define DBPTR_SCHEMA \
8981 " schema TEXT HIDDEN" \
8985 ** Connect to an sqlite_dbdata (pAux==0) or sqlite_dbptr (pAux!=0) virtual
8988 static int dbdataConnect(
8991 int argc, const char *const*argv,
8992 sqlite3_vtab **ppVtab,
8995 DbdataTable *pTab = 0;
8996 int rc = sqlite3_declare_vtab(db, pAux ? DBPTR_SCHEMA : DBDATA_SCHEMA);
8998 if( rc==SQLITE_OK ){
8999 pTab = (DbdataTable*)sqlite3_malloc64(sizeof(DbdataTable));
9003 memset(pTab, 0, sizeof(DbdataTable));
9005 pTab->bPtr = (pAux!=0);
9009 *ppVtab = (sqlite3_vtab*)pTab;
9014 ** Disconnect from or destroy a sqlite_dbdata or sqlite_dbptr virtual table.
9016 static int dbdataDisconnect(sqlite3_vtab *pVtab){
9017 DbdataTable *pTab = (DbdataTable*)pVtab;
9019 sqlite3_finalize(pTab->pStmt);
9020 sqlite3_free(pVtab);
9026 ** This function interprets two types of constraints:
9031 ** If neither are present, idxNum is set to 0. If schema=? is present,
9032 ** the 0x01 bit in idxNum is set. If pgno=? is present, the 0x02 bit
9033 ** in idxNum is set.
9035 ** If both parameters are present, schema is in position 0 and pgno in
9038 static int dbdataBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdx){
9039 DbdataTable *pTab = (DbdataTable*)tab;
9043 int colSchema = (pTab->bPtr ? DBPTR_COLUMN_SCHEMA : DBDATA_COLUMN_SCHEMA);
9045 for(i=0; i<pIdx->nConstraint; i++){
9046 struct sqlite3_index_constraint *p = &pIdx->aConstraint[i];
9047 if( p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
9048 if( p->iColumn==colSchema ){
9049 if( p->usable==0 ) return SQLITE_CONSTRAINT;
9052 if( p->iColumn==DBDATA_COLUMN_PGNO && p->usable ){
9059 pIdx->aConstraintUsage[iSchema].argvIndex = 1;
9060 pIdx->aConstraintUsage[iSchema].omit = 1;
9063 pIdx->aConstraintUsage[iPgno].argvIndex = 1 + (iSchema>=0);
9064 pIdx->aConstraintUsage[iPgno].omit = 1;
9065 pIdx->estimatedCost = 100;
9066 pIdx->estimatedRows = 50;
9068 if( pTab->bPtr==0 && pIdx->nOrderBy && pIdx->aOrderBy[0].desc==0 ){
9069 int iCol = pIdx->aOrderBy[0].iColumn;
9070 if( pIdx->nOrderBy==1 ){
9071 pIdx->orderByConsumed = (iCol==0 || iCol==1);
9072 }else if( pIdx->nOrderBy==2 && pIdx->aOrderBy[1].desc==0 && iCol==0 ){
9073 pIdx->orderByConsumed = (pIdx->aOrderBy[1].iColumn==1);
9078 pIdx->estimatedCost = 100000000;
9079 pIdx->estimatedRows = 1000000000;
9081 pIdx->idxNum = (iSchema>=0 ? 0x01 : 0x00) | (iPgno>=0 ? 0x02 : 0x00);
9086 ** Open a new sqlite_dbdata or sqlite_dbptr cursor.
9088 static int dbdataOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
9091 pCsr = (DbdataCursor*)sqlite3_malloc64(sizeof(DbdataCursor));
9093 return SQLITE_NOMEM;
9095 memset(pCsr, 0, sizeof(DbdataCursor));
9096 pCsr->base.pVtab = pVTab;
9099 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
9104 ** Restore a cursor object to the state it was in when first allocated
9107 static void dbdataResetCursor(DbdataCursor *pCsr){
9108 DbdataTable *pTab = (DbdataTable*)(pCsr->base.pVtab);
9109 if( pTab->pStmt==0 ){
9110 pTab->pStmt = pCsr->pStmt;
9112 sqlite3_finalize(pCsr->pStmt);
9119 sqlite3_free(pCsr->aPage);
9120 sqlite3_free(pCsr->pRec);
9126 ** Close an sqlite_dbdata or sqlite_dbptr cursor.
9128 static int dbdataClose(sqlite3_vtab_cursor *pCursor){
9129 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9130 dbdataResetCursor(pCsr);
9136 ** Utility methods to decode 16 and 32-bit big-endian unsigned integers.
9138 static unsigned int get_uint16(unsigned char *a){
9139 return (a[0]<<8)|a[1];
9141 static unsigned int get_uint32(unsigned char *a){
9142 return ((unsigned int)a[0]<<24)
9143 | ((unsigned int)a[1]<<16)
9144 | ((unsigned int)a[2]<<8)
9145 | ((unsigned int)a[3]);
9149 ** Load page pgno from the database via the sqlite_dbpage virtual table.
9150 ** If successful, set (*ppPage) to point to a buffer containing the page
9151 ** data, (*pnPage) to the size of that buffer in bytes and return
9152 ** SQLITE_OK. In this case it is the responsibility of the caller to
9153 ** eventually free the buffer using sqlite3_free().
9155 ** Or, if an error occurs, set both (*ppPage) and (*pnPage) to 0 and
9156 ** return an SQLite error code.
9158 static int dbdataLoadPage(
9159 DbdataCursor *pCsr, /* Cursor object */
9160 unsigned int pgno, /* Page number of page to load */
9161 u8 **ppPage, /* OUT: pointer to page buffer */
9162 int *pnPage /* OUT: Size of (*ppPage) in bytes */
9166 sqlite3_stmt *pStmt = pCsr->pStmt;
9170 sqlite3_bind_int64(pStmt, 2, pgno);
9171 if( SQLITE_ROW==sqlite3_step(pStmt) ){
9172 int nCopy = sqlite3_column_bytes(pStmt, 0);
9175 pPage = (u8*)sqlite3_malloc64(nCopy + DBDATA_PADDING_BYTES);
9179 const u8 *pCopy = sqlite3_column_blob(pStmt, 0);
9180 memcpy(pPage, pCopy, nCopy);
9181 memset(&pPage[nCopy], 0, DBDATA_PADDING_BYTES);
9187 rc2 = sqlite3_reset(pStmt);
9188 if( rc==SQLITE_OK ) rc = rc2;
9194 ** Read a varint. Put the value in *pVal and return the number of bytes.
9196 static int dbdataGetVarint(const u8 *z, sqlite3_int64 *pVal){
9197 sqlite3_int64 v = 0;
9200 v = (v<<7) + (z[i]&0x7f);
9201 if( (z[i]&0x80)==0 ){ *pVal = v; return i+1; }
9203 v = (v<<8) + (z[i]&0xff);
9209 ** Return the number of bytes of space used by an SQLite value of type
9212 static int dbdataValueBytes(int eType){
9214 case 0: case 8: case 9:
9232 return ((eType-12) / 2);
9239 ** Load a value of type eType from buffer pData and use it to set the
9240 ** result of context object pCtx.
9242 static void dbdataValue(
9243 sqlite3_context *pCtx,
9248 if( eType>=0 && dbdataValueBytes(eType)<=nData ){
9253 sqlite3_result_null(pCtx);
9257 sqlite3_result_int(pCtx, 0);
9260 sqlite3_result_int(pCtx, 1);
9263 case 1: case 2: case 3: case 4: case 5: case 6: case 7: {
9264 sqlite3_uint64 v = (signed char)pData[0];
9268 case 6: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
9269 case 5: v = (v<<16) + (pData[0]<<8) + pData[1]; pData += 2;
9270 case 4: v = (v<<8) + pData[0]; pData++;
9271 case 3: v = (v<<8) + pData[0]; pData++;
9272 case 2: v = (v<<8) + pData[0]; pData++;
9277 memcpy(&r, &v, sizeof(r));
9278 sqlite3_result_double(pCtx, r);
9280 sqlite3_result_int64(pCtx, (sqlite3_int64)v);
9286 int n = ((eType-12) / 2);
9288 sqlite3_result_text(pCtx, (const char*)pData, n, SQLITE_TRANSIENT);
9290 sqlite3_result_blob(pCtx, pData, n, SQLITE_TRANSIENT);
9298 ** Move an sqlite_dbdata or sqlite_dbptr cursor to the next entry.
9300 static int dbdataNext(sqlite3_vtab_cursor *pCursor){
9301 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9302 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9307 int iOff = (pCsr->iPgno==1 ? 100 : 0);
9310 if( pCsr->aPage==0 ){
9312 if( pCsr->bOnePage==0 && pCsr->iPgno>pCsr->szDb ) return SQLITE_OK;
9313 rc = dbdataLoadPage(pCsr, pCsr->iPgno, &pCsr->aPage, &pCsr->nPage);
9314 if( rc!=SQLITE_OK ) return rc;
9315 if( pCsr->aPage ) break;
9318 pCsr->iCell = pTab->bPtr ? -2 : 0;
9319 pCsr->nCell = get_uint16(&pCsr->aPage[iOff+3]);
9323 if( pCsr->aPage[iOff]!=0x02 && pCsr->aPage[iOff]!=0x05 ){
9324 pCsr->iCell = pCsr->nCell;
9327 if( pCsr->iCell>=pCsr->nCell ){
9328 sqlite3_free(pCsr->aPage);
9330 if( pCsr->bOnePage ) return SQLITE_OK;
9336 /* If there is no record loaded, load it now. */
9337 if( pCsr->pRec==0 ){
9340 sqlite3_int64 nPayload = 0;
9341 sqlite3_int64 nHdr = 0;
9346 switch( pCsr->aPage[iOff] ){
9356 /* This is not a b-tree page with records on it. Continue. */
9357 pCsr->iCell = pCsr->nCell;
9361 if( pCsr->iCell>=pCsr->nCell ){
9365 iOff += 8 + nPointer + pCsr->iCell*2;
9366 if( iOff>pCsr->nPage ){
9369 iOff = get_uint16(&pCsr->aPage[iOff]);
9372 /* For an interior node cell, skip past the child-page number */
9375 /* Load the "byte of payload including overflow" field */
9376 if( bNextPage || iOff>pCsr->nPage ){
9379 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &nPayload);
9382 /* If this is a leaf intkey cell, load the rowid */
9383 if( bHasRowid && !bNextPage && iOff<pCsr->nPage ){
9384 iOff += dbdataGetVarint(&pCsr->aPage[iOff], &pCsr->iIntkey);
9387 /* Figure out how much data to read from the local page */
9392 X = ((U-12)*64/255)-23;
9398 M = ((U-12)*32/255)-23;
9399 K = M+((nPayload-M)%(U-4));
9407 if( bNextPage || nLocal+iOff>pCsr->nPage ){
9411 /* Allocate space for payload. And a bit more to catch small buffer
9412 ** overruns caused by attempting to read a varint or similar from
9413 ** near the end of a corrupt record. */
9414 pCsr->pRec = (u8*)sqlite3_malloc64(nPayload+DBDATA_PADDING_BYTES);
9415 if( pCsr->pRec==0 ) return SQLITE_NOMEM;
9416 memset(pCsr->pRec, 0, nPayload+DBDATA_PADDING_BYTES);
9417 pCsr->nRec = nPayload;
9419 /* Load the nLocal bytes of payload */
9420 memcpy(pCsr->pRec, &pCsr->aPage[iOff], nLocal);
9423 /* Load content from overflow pages */
9424 if( nPayload>nLocal ){
9425 sqlite3_int64 nRem = nPayload - nLocal;
9426 unsigned int pgnoOvfl = get_uint32(&pCsr->aPage[iOff]);
9431 rc = dbdataLoadPage(pCsr, pgnoOvfl, &aOvfl, &nOvfl);
9432 assert( rc!=SQLITE_OK || aOvfl==0 || nOvfl==pCsr->nPage );
9433 if( rc!=SQLITE_OK ) return rc;
9434 if( aOvfl==0 ) break;
9437 if( nCopy>nRem ) nCopy = nRem;
9438 memcpy(&pCsr->pRec[nPayload-nRem], &aOvfl[4], nCopy);
9441 pgnoOvfl = get_uint32(aOvfl);
9442 sqlite3_free(aOvfl);
9446 iHdr = dbdataGetVarint(pCsr->pRec, &nHdr);
9448 pCsr->pHdrPtr = &pCsr->pRec[iHdr];
9449 pCsr->pPtr = &pCsr->pRec[pCsr->nHdr];
9450 pCsr->iField = (bHasRowid ? -1 : 0);
9455 if( pCsr->iField>0 ){
9456 sqlite3_int64 iType;
9457 if( pCsr->pHdrPtr>&pCsr->pRec[pCsr->nRec] ){
9460 pCsr->pHdrPtr += dbdataGetVarint(pCsr->pHdrPtr, &iType);
9461 pCsr->pPtr += dbdataValueBytes(iType);
9467 sqlite3_free(pCsr->aPage);
9468 sqlite3_free(pCsr->pRec);
9471 if( pCsr->bOnePage ) return SQLITE_OK;
9474 if( pCsr->iField<0 || pCsr->pHdrPtr<&pCsr->pRec[pCsr->nHdr] ){
9478 /* Advance to the next cell. The next iteration of the loop will load
9479 ** the record and so on. */
9480 sqlite3_free(pCsr->pRec);
9487 assert( !"can't get here" );
9492 ** Return true if the cursor is at EOF.
9494 static int dbdataEof(sqlite3_vtab_cursor *pCursor){
9495 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9496 return pCsr->aPage==0;
9500 ** Determine the size in pages of database zSchema (where zSchema is
9501 ** "main", "temp" or the name of an attached database) and set
9502 ** pCsr->szDb accordingly. If successful, return SQLITE_OK. Otherwise,
9503 ** an SQLite error code.
9505 static int dbdataDbsize(DbdataCursor *pCsr, const char *zSchema){
9506 DbdataTable *pTab = (DbdataTable*)pCsr->base.pVtab;
9509 sqlite3_stmt *pStmt = 0;
9511 zSql = sqlite3_mprintf("PRAGMA %Q.page_count", zSchema);
9512 if( zSql==0 ) return SQLITE_NOMEM;
9513 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pStmt, 0);
9515 if( rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
9516 pCsr->szDb = sqlite3_column_int(pStmt, 0);
9518 rc2 = sqlite3_finalize(pStmt);
9519 if( rc==SQLITE_OK ) rc = rc2;
9524 ** xFilter method for sqlite_dbdata and sqlite_dbptr.
9526 static int dbdataFilter(
9527 sqlite3_vtab_cursor *pCursor,
9528 int idxNum, const char *idxStr,
9529 int argc, sqlite3_value **argv
9531 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9532 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9534 const char *zSchema = "main";
9536 dbdataResetCursor(pCsr);
9537 assert( pCsr->iPgno==1 );
9538 if( idxNum & 0x01 ){
9539 zSchema = (const char*)sqlite3_value_text(argv[0]);
9541 if( idxNum & 0x02 ){
9542 pCsr->iPgno = sqlite3_value_int(argv[(idxNum & 0x01)]);
9545 pCsr->nPage = dbdataDbsize(pCsr, zSchema);
9546 rc = dbdataDbsize(pCsr, zSchema);
9549 if( rc==SQLITE_OK ){
9551 pCsr->pStmt = pTab->pStmt;
9554 rc = sqlite3_prepare_v2(pTab->db,
9555 "SELECT data FROM sqlite_dbpage(?) WHERE pgno=?", -1,
9560 if( rc==SQLITE_OK ){
9561 rc = sqlite3_bind_text(pCsr->pStmt, 1, zSchema, -1, SQLITE_TRANSIENT);
9563 pTab->base.zErrMsg = sqlite3_mprintf("%s", sqlite3_errmsg(pTab->db));
9565 if( rc==SQLITE_OK ){
9566 rc = dbdataNext(pCursor);
9572 ** Return a column for the sqlite_dbdata or sqlite_dbptr table.
9574 static int dbdataColumn(
9575 sqlite3_vtab_cursor *pCursor,
9576 sqlite3_context *ctx,
9579 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9580 DbdataTable *pTab = (DbdataTable*)pCursor->pVtab;
9583 case DBPTR_COLUMN_PGNO:
9584 sqlite3_result_int64(ctx, pCsr->iPgno);
9586 case DBPTR_COLUMN_CHILD: {
9587 int iOff = pCsr->iPgno==1 ? 100 : 0;
9588 if( pCsr->iCell<0 ){
9591 iOff += 12 + pCsr->iCell*2;
9592 if( iOff>pCsr->nPage ) return SQLITE_OK;
9593 iOff = get_uint16(&pCsr->aPage[iOff]);
9595 if( iOff<=pCsr->nPage ){
9596 sqlite3_result_int64(ctx, get_uint32(&pCsr->aPage[iOff]));
9603 case DBDATA_COLUMN_PGNO:
9604 sqlite3_result_int64(ctx, pCsr->iPgno);
9606 case DBDATA_COLUMN_CELL:
9607 sqlite3_result_int(ctx, pCsr->iCell);
9609 case DBDATA_COLUMN_FIELD:
9610 sqlite3_result_int(ctx, pCsr->iField);
9612 case DBDATA_COLUMN_VALUE: {
9613 if( pCsr->iField<0 ){
9614 sqlite3_result_int64(ctx, pCsr->iIntkey);
9616 sqlite3_int64 iType;
9617 dbdataGetVarint(pCsr->pHdrPtr, &iType);
9619 ctx, iType, pCsr->pPtr, &pCsr->pRec[pCsr->nRec] - pCsr->pPtr
9630 ** Return the rowid for an sqlite_dbdata or sqlite_dptr table.
9632 static int dbdataRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
9633 DbdataCursor *pCsr = (DbdataCursor*)pCursor;
9634 *pRowid = pCsr->iRowid;
9640 ** Invoke this routine to register the "sqlite_dbdata" virtual table module
9642 static int sqlite3DbdataRegister(sqlite3 *db){
9643 static sqlite3_module dbdata_module = {
9646 dbdataConnect, /* xConnect */
9647 dbdataBestIndex, /* xBestIndex */
9648 dbdataDisconnect, /* xDisconnect */
9650 dbdataOpen, /* xOpen - open a cursor */
9651 dbdataClose, /* xClose - close a cursor */
9652 dbdataFilter, /* xFilter - configure scan constraints */
9653 dbdataNext, /* xNext - advance a cursor */
9654 dbdataEof, /* xEof - check for end of scan */
9655 dbdataColumn, /* xColumn - read data */
9656 dbdataRowid, /* xRowid - read data */
9662 0, /* xFindMethod */
9666 0, /* xRollbackTo */
9670 int rc = sqlite3_create_module(db, "sqlite_dbdata", &dbdata_module, 0);
9671 if( rc==SQLITE_OK ){
9672 rc = sqlite3_create_module(db, "sqlite_dbptr", &dbdata_module, (void*)1);
9680 int sqlite3_dbdata_init(
9683 const sqlite3_api_routines *pApi
9685 SQLITE_EXTENSION_INIT2(pApi);
9686 return sqlite3DbdataRegister(db);
9689 /************************* End ../ext/misc/dbdata.c ********************/
9692 #if defined(SQLITE_ENABLE_SESSION)
9694 ** State information for a single open session
9696 typedef struct OpenSession OpenSession;
9697 struct OpenSession {
9698 char *zName; /* Symbolic name for this session */
9699 int nFilter; /* Number of xFilter rejection GLOB patterns */
9700 char **azFilter; /* Array of xFilter rejection GLOB patterns */
9701 sqlite3_session *p; /* The open session */
9706 ** Shell output mode information from before ".explain on",
9707 ** saved so that it can be restored by ".explain off"
9709 typedef struct SavedModeInfo SavedModeInfo;
9710 struct SavedModeInfo {
9711 int valid; /* Is there legit data in here? */
9712 int mode; /* Mode prior to ".explain on" */
9713 int showHeader; /* The ".header" setting prior to ".explain on" */
9714 int colWidth[100]; /* Column widths prior to ".explain on" */
9717 typedef struct ExpertInfo ExpertInfo;
9719 sqlite3expert *pExpert;
9723 /* A single line in the EQP output */
9724 typedef struct EQPGraphRow EQPGraphRow;
9725 struct EQPGraphRow {
9726 int iEqpId; /* ID for this row */
9727 int iParentId; /* ID of the parent row */
9728 EQPGraphRow *pNext; /* Next row in sequence */
9729 char zText[1]; /* Text to display for this row */
9732 /* All EQP output is collected into an instance of the following */
9733 typedef struct EQPGraph EQPGraph;
9735 EQPGraphRow *pRow; /* Linked list of all rows of the EQP output */
9736 EQPGraphRow *pLast; /* Last element of the pRow list */
9737 char zPrefix[100]; /* Graph prefix */
9741 ** State information about the database connection is contained in an
9742 ** instance of the following structure.
9744 typedef struct ShellState ShellState;
9746 sqlite3 *db; /* The database */
9747 u8 autoExplain; /* Automatically turn on .explain mode */
9748 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
9749 u8 autoEQPtest; /* autoEQP is in test mode */
9750 u8 autoEQPtrace; /* autoEQP is in trace mode */
9751 u8 statsOn; /* True to display memory stats before each finalize */
9752 u8 scanstatsOn; /* True to display scan stats before each finalize */
9753 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
9754 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
9755 u8 nEqpLevel; /* Depth of the EQP output graph */
9756 u8 eTraceType; /* SHELL_TRACE_* value for type of trace */
9757 unsigned mEqpLines; /* Mask of veritical lines in the EQP output graph */
9758 int outCount; /* Revert to stdout when reaching zero */
9759 int cnt; /* Number of records displayed so far */
9760 int lineno; /* Line number of last line read from in */
9761 int openFlags; /* Additional flags to open. (SQLITE_OPEN_NOFOLLOW) */
9762 FILE *in; /* Read commands from this stream */
9763 FILE *out; /* Write results here */
9764 FILE *traceOut; /* Output for sqlite3_trace() */
9765 int nErr; /* Number of errors seen */
9766 int mode; /* An output mode setting */
9767 int modePrior; /* Saved mode */
9768 int cMode; /* temporary output mode for the current query */
9769 int normalMode; /* Output mode before ".explain on" */
9770 int writableSchema; /* True if PRAGMA writable_schema=ON */
9771 int showHeader; /* True to show column names in List or Column mode */
9772 int nCheck; /* Number of ".check" commands run */
9773 unsigned nProgress; /* Number of progress callbacks encountered */
9774 unsigned mxProgress; /* Maximum progress callbacks before failing */
9775 unsigned flgProgress; /* Flags for the progress callback */
9776 unsigned shellFlgs; /* Various flags */
9777 unsigned priorShFlgs; /* Saved copy of flags */
9778 sqlite3_int64 szMax; /* --maxsize argument to .open */
9779 char *zDestTable; /* Name of destination table when MODE_Insert */
9780 char *zTempFile; /* Temporary file that might need deleting */
9781 char zTestcase[30]; /* Name of current test case */
9782 char colSeparator[20]; /* Column separator character for several modes */
9783 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
9784 char colSepPrior[20]; /* Saved column separator */
9785 char rowSepPrior[20]; /* Saved row separator */
9786 int colWidth[100]; /* Requested width of each column when in column mode*/
9787 int actualWidth[100]; /* Actual width of each column */
9788 char nullValue[20]; /* The text to print when a NULL comes back from
9790 char outfile[FILENAME_MAX]; /* Filename for *out */
9791 const char *zDbFilename; /* name of the database file */
9792 char *zFreeOnClose; /* Filename to free when closing */
9793 const char *zVfs; /* Name of VFS to use */
9794 sqlite3_stmt *pStmt; /* Current statement if any. */
9795 FILE *pLog; /* Write log output here */
9796 int *aiIndent; /* Array of indents used in MODE_Explain */
9797 int nIndent; /* Size of array aiIndent[] */
9798 int iIndent; /* Index of current op in aiIndent[] */
9799 EQPGraph sGraph; /* Information for the graphical EXPLAIN QUERY PLAN */
9800 #if defined(SQLITE_ENABLE_SESSION)
9801 int nSession; /* Number of active sessions */
9802 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
9804 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
9808 /* Allowed values for ShellState.autoEQP
9810 #define AUTOEQP_off 0 /* Automatic EXPLAIN QUERY PLAN is off */
9811 #define AUTOEQP_on 1 /* Automatic EQP is on */
9812 #define AUTOEQP_trigger 2 /* On and also show plans for triggers */
9813 #define AUTOEQP_full 3 /* Show full EXPLAIN */
9815 /* Allowed values for ShellState.openMode
9817 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
9818 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
9819 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
9820 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
9821 #define SHELL_OPEN_READONLY 4 /* Open a normal database read-only */
9822 #define SHELL_OPEN_DESERIALIZE 5 /* Open using sqlite3_deserialize() */
9823 #define SHELL_OPEN_HEXDB 6 /* Use "dbtotxt" output as data source */
9825 /* Allowed values for ShellState.eTraceType
9827 #define SHELL_TRACE_PLAIN 0 /* Show input SQL text */
9828 #define SHELL_TRACE_EXPANDED 1 /* Show expanded SQL text */
9829 #define SHELL_TRACE_NORMALIZED 2 /* Show normalized SQL text */
9831 /* Bits in the ShellState.flgProgress variable */
9832 #define SHELL_PROGRESS_QUIET 0x01 /* Omit announcing every progress callback */
9833 #define SHELL_PROGRESS_RESET 0x02 /* Reset the count when the progres
9834 ** callback limit is reached, and for each
9835 ** top-level SQL statement */
9836 #define SHELL_PROGRESS_ONCE 0x04 /* Cancel the --limit after firing once */
9839 ** These are the allowed shellFlgs values
9841 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
9842 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
9843 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
9844 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
9845 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
9846 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
9847 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
9850 ** Macros for testing and setting shellFlgs
9852 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
9853 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
9854 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
9857 ** These are the allowed modes.
9859 #define MODE_Line 0 /* One column per line. Blank line between records */
9860 #define MODE_Column 1 /* One record per line in neat columns */
9861 #define MODE_List 2 /* One record per line with a separator */
9862 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
9863 #define MODE_Html 4 /* Generate an XHTML table */
9864 #define MODE_Insert 5 /* Generate SQL "insert" statements */
9865 #define MODE_Quote 6 /* Quote values as for SQL */
9866 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
9867 #define MODE_Csv 8 /* Quote strings, numbers are plain */
9868 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
9869 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
9870 #define MODE_Pretty 11 /* Pretty-print schemas */
9871 #define MODE_EQP 12 /* Converts EXPLAIN QUERY PLAN output into a graph */
9873 static const char *modeDescr[] = {
9890 ** These are the column/row/line separators used by the various
9891 ** import/export modes.
9893 #define SEP_Column "|"
9894 #define SEP_Row "\n"
9895 #define SEP_Tab "\t"
9896 #define SEP_Space " "
9897 #define SEP_Comma ","
9898 #define SEP_CrLf "\r\n"
9899 #define SEP_Unit "\x1F"
9900 #define SEP_Record "\x1E"
9903 ** A callback for the sqlite3_log() interface.
9905 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
9906 ShellState *p = (ShellState*)pArg;
9907 if( p->pLog==0 ) return;
9908 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
9913 ** SQL function: shell_putsnl(X)
9915 ** Write the text X to the screen (or whatever output is being directed)
9916 ** adding a newline at the end, and then return X.
9918 static void shellPutsFunc(
9919 sqlite3_context *pCtx,
9921 sqlite3_value **apVal
9923 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
9925 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
9926 sqlite3_result_value(pCtx, apVal[0]);
9930 ** SQL function: edit(VALUE)
9931 ** edit(VALUE,EDITOR)
9935 ** (1) Write VALUE into a temporary file.
9936 ** (2) Run program EDITOR on that temporary file.
9937 ** (3) Read the temporary file back and return its content as the result.
9938 ** (4) Delete the temporary file
9940 ** If the EDITOR argument is omitted, use the value in the VISUAL
9941 ** environment variable. If still there is no EDITOR, through an error.
9943 ** Also throw an error if the EDITOR program returns a non-zero exit code.
9945 #ifndef SQLITE_NOHAVE_SYSTEM
9946 static void editFunc(
9947 sqlite3_context *context,
9949 sqlite3_value **argv
9951 const char *zEditor;
9952 char *zTempFile = 0;
9961 unsigned char *p = 0;
9964 zEditor = (const char*)sqlite3_value_text(argv[1]);
9966 zEditor = getenv("VISUAL");
9969 sqlite3_result_error(context, "no editor for edit()", -1);
9972 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
9973 sqlite3_result_error(context, "NULL input to edit()", -1);
9976 db = sqlite3_context_db_handle(context);
9978 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
9980 sqlite3_uint64 r = 0;
9981 sqlite3_randomness(sizeof(r), &r);
9982 zTempFile = sqlite3_mprintf("temp%llx", r);
9984 sqlite3_result_error_nomem(context);
9988 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
9989 /* When writing the file to be edited, do \n to \r\n conversions on systems
9990 ** that want \r\n line endings */
9991 f = fopen(zTempFile, bBin ? "wb" : "w");
9993 sqlite3_result_error(context, "edit() cannot open temp file", -1);
9996 sz = sqlite3_value_bytes(argv[0]);
9998 x = fwrite(sqlite3_value_blob(argv[0]), 1, (size_t)sz, f);
10000 const char *z = (const char*)sqlite3_value_text(argv[0]);
10001 /* Remember whether or not the value originally contained \r\n */
10002 if( z && strstr(z,"\r\n")!=0 ) hasCRNL = 1;
10003 x = fwrite(sqlite3_value_text(argv[0]), 1, (size_t)sz, f);
10008 sqlite3_result_error(context, "edit() could not write the whole file", -1);
10009 goto edit_func_end;
10011 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
10013 sqlite3_result_error_nomem(context);
10014 goto edit_func_end;
10017 sqlite3_free(zCmd);
10019 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
10020 goto edit_func_end;
10022 f = fopen(zTempFile, "rb");
10024 sqlite3_result_error(context,
10025 "edit() cannot reopen temp file after edit", -1);
10026 goto edit_func_end;
10028 fseek(f, 0, SEEK_END);
10031 p = sqlite3_malloc64( sz+1 );
10033 sqlite3_result_error_nomem(context);
10034 goto edit_func_end;
10036 x = fread(p, 1, (size_t)sz, f);
10040 sqlite3_result_error(context, "could not read back the whole file", -1);
10041 goto edit_func_end;
10044 sqlite3_result_blob64(context, p, sz, sqlite3_free);
10046 sqlite3_int64 i, j;
10048 /* If the original contains \r\n then do no conversions back to \n */
10051 /* If the file did not originally contain \r\n then convert any new
10052 ** \r\n back into \n */
10053 for(i=j=0; i<sz; i++){
10054 if( p[i]=='\r' && p[i+1]=='\n' ) i++;
10060 sqlite3_result_text64(context, (const char*)p, sz,
10061 sqlite3_free, SQLITE_UTF8);
10068 sqlite3_free(zTempFile);
10071 #endif /* SQLITE_NOHAVE_SYSTEM */
10074 ** Save or restore the current output mode
10076 static void outputModePush(ShellState *p){
10077 p->modePrior = p->mode;
10078 p->priorShFlgs = p->shellFlgs;
10079 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
10080 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
10082 static void outputModePop(ShellState *p){
10083 p->mode = p->modePrior;
10084 p->shellFlgs = p->priorShFlgs;
10085 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
10086 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
10090 ** Output the given string as a hex-encoded blob (eg. X'1234' )
10092 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
10094 char *zBlob = (char *)pBlob;
10095 raw_printf(out,"X'");
10096 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
10097 raw_printf(out,"'");
10101 ** Find a string that is not found anywhere in z[]. Return a pointer
10104 ** Try to use zA and zB first. If both of those are already found in z[]
10105 ** then make up some string and store it in the buffer zBuf.
10107 static const char *unused_string(
10108 const char *z, /* Result must not appear anywhere in z */
10109 const char *zA, const char *zB, /* Try these first */
10110 char *zBuf /* Space to store a generated string */
10113 if( strstr(z, zA)==0 ) return zA;
10114 if( strstr(z, zB)==0 ) return zB;
10116 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
10117 }while( strstr(z,zBuf)!=0 );
10122 ** Output the given string as a quoted string using SQL quoting conventions.
10124 ** See also: output_quoted_escaped_string()
10126 static void output_quoted_string(FILE *out, const char *z){
10129 setBinaryMode(out, 1);
10130 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
10132 utf8_printf(out,"'%s'",z);
10134 raw_printf(out, "'");
10136 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
10139 utf8_printf(out, "%.*s", i, z);
10143 raw_printf(out, "'");
10151 raw_printf(out, "'");
10153 setTextMode(out, 1);
10157 ** Output the given string as a quoted string using SQL quoting conventions.
10158 ** Additionallly , escape the "\n" and "\r" characters so that they do not
10159 ** get corrupted by end-of-line translation facilities in some operating
10162 ** This is like output_quoted_string() but with the addition of the \r\n
10163 ** escape mechanism.
10165 static void output_quoted_escaped_string(FILE *out, const char *z){
10168 setBinaryMode(out, 1);
10169 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
10171 utf8_printf(out,"'%s'",z);
10173 const char *zNL = 0;
10174 const char *zCR = 0;
10177 char zBuf1[20], zBuf2[20];
10178 for(i=0; z[i]; i++){
10179 if( z[i]=='\n' ) nNL++;
10180 if( z[i]=='\r' ) nCR++;
10183 raw_printf(out, "replace(");
10184 zNL = unused_string(z, "\\n", "\\012", zBuf1);
10187 raw_printf(out, "replace(");
10188 zCR = unused_string(z, "\\r", "\\015", zBuf2);
10190 raw_printf(out, "'");
10192 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
10195 utf8_printf(out, "%.*s", i, z);
10199 raw_printf(out, "'");
10207 raw_printf(out, "%s", zNL);
10210 raw_printf(out, "%s", zCR);
10212 raw_printf(out, "'");
10214 raw_printf(out, ",'%s',char(13))", zCR);
10217 raw_printf(out, ",'%s',char(10))", zNL);
10220 setTextMode(out, 1);
10224 ** Output the given string as a quoted according to C or TCL quoting rules.
10226 static void output_c_string(FILE *out, const char *z){
10229 while( (c = *(z++))!=0 ){
10233 }else if( c=='"' ){
10236 }else if( c=='\t' ){
10239 }else if( c=='\n' ){
10242 }else if( c=='\r' ){
10245 }else if( !isprint(c&0xff) ){
10246 raw_printf(out, "\\%03o", c&0xff);
10255 ** Output the given string with characters that are special to
10258 static void output_html_string(FILE *out, const char *z){
10270 utf8_printf(out,"%.*s",i,z);
10273 raw_printf(out,"<");
10274 }else if( z[i]=='&' ){
10275 raw_printf(out,"&");
10276 }else if( z[i]=='>' ){
10277 raw_printf(out,">");
10278 }else if( z[i]=='\"' ){
10279 raw_printf(out,""");
10280 }else if( z[i]=='\'' ){
10281 raw_printf(out,"'");
10290 ** If a field contains any character identified by a 1 in the following
10291 ** array, then the string must be quoted for CSV.
10293 static const char needCsvQuote[] = {
10294 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10295 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10296 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
10297 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10298 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10299 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10300 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10301 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
10302 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10303 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10304 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10305 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10306 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10307 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10308 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10309 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
10313 ** Output a single term of CSV. Actually, p->colSeparator is used for
10314 ** the separator, which may or may not be a comma. p->nullValue is
10315 ** the null value. Strings are quoted if necessary. The separator
10316 ** is only issued if bSep is true.
10318 static void output_csv(ShellState *p, const char *z, int bSep){
10319 FILE *out = p->out;
10321 utf8_printf(out,"%s",p->nullValue);
10324 int nSep = strlen30(p->colSeparator);
10325 for(i=0; z[i]; i++){
10326 if( needCsvQuote[((unsigned char*)z)[i]]
10327 || (z[i]==p->colSeparator[0] &&
10328 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
10334 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
10335 utf8_printf(out, "%s", zQuoted);
10336 sqlite3_free(zQuoted);
10338 utf8_printf(out, "%s", z);
10342 utf8_printf(p->out, "%s", p->colSeparator);
10347 ** This routine runs when the user presses Ctrl-C
10349 static void interrupt_handler(int NotUsed){
10350 UNUSED_PARAMETER(NotUsed);
10352 if( seenInterrupt>2 ) exit(1);
10353 if( globalDb ) sqlite3_interrupt(globalDb);
10356 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
10358 ** This routine runs for console events (e.g. Ctrl-C) on Win32
10360 static BOOL WINAPI ConsoleCtrlHandler(
10361 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
10363 if( dwCtrlType==CTRL_C_EVENT ){
10364 interrupt_handler(0);
10371 #ifndef SQLITE_OMIT_AUTHORIZATION
10373 ** When the ".auth ON" is set, the following authorizer callback is
10374 ** invoked. It always returns SQLITE_OK.
10376 static int shellAuth(
10384 ShellState *p = (ShellState*)pClientData;
10385 static const char *azAction[] = { 0,
10386 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
10387 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
10388 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
10389 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
10390 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
10391 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
10392 "PRAGMA", "READ", "SELECT",
10393 "TRANSACTION", "UPDATE", "ATTACH",
10394 "DETACH", "ALTER_TABLE", "REINDEX",
10395 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
10396 "FUNCTION", "SAVEPOINT", "RECURSIVE"
10404 utf8_printf(p->out, "authorizer: %s", azAction[op]);
10405 for(i=0; i<4; i++){
10406 raw_printf(p->out, " ");
10408 output_c_string(p->out, az[i]);
10410 raw_printf(p->out, "NULL");
10413 raw_printf(p->out, "\n");
10419 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
10421 ** This routine converts some CREATE TABLE statements for shadow tables
10422 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
10424 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
10426 if( zTail==0 ) return;
10427 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
10428 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
10430 utf8_printf(out, "%s%s", z, zTail);
10433 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
10436 printSchemaLine(out, z, zTail);
10441 ** Return true if string z[] has nothing but whitespace and comments to the
10442 ** end of the first line.
10444 static int wsToEol(const char *z){
10446 for(i=0; z[i]; i++){
10447 if( z[i]=='\n' ) return 1;
10448 if( IsSpace(z[i]) ) continue;
10449 if( z[i]=='-' && z[i+1]=='-' ) return 1;
10456 ** Add a new entry to the EXPLAIN QUERY PLAN data
10458 static void eqp_append(ShellState *p, int iEqpId, int p2, const char *zText){
10460 int nText = strlen30(zText);
10461 if( p->autoEQPtest ){
10462 utf8_printf(p->out, "%d,%d,%s\n", iEqpId, p2, zText);
10464 pNew = sqlite3_malloc64( sizeof(*pNew) + nText );
10465 if( pNew==0 ) shell_out_of_memory();
10466 pNew->iEqpId = iEqpId;
10467 pNew->iParentId = p2;
10468 memcpy(pNew->zText, zText, nText+1);
10470 if( p->sGraph.pLast ){
10471 p->sGraph.pLast->pNext = pNew;
10473 p->sGraph.pRow = pNew;
10475 p->sGraph.pLast = pNew;
10479 ** Free and reset the EXPLAIN QUERY PLAN data that has been collected
10482 static void eqp_reset(ShellState *p){
10483 EQPGraphRow *pRow, *pNext;
10484 for(pRow = p->sGraph.pRow; pRow; pRow = pNext){
10485 pNext = pRow->pNext;
10486 sqlite3_free(pRow);
10488 memset(&p->sGraph, 0, sizeof(p->sGraph));
10491 /* Return the next EXPLAIN QUERY PLAN line with iEqpId that occurs after
10492 ** pOld, or return the first such line if pOld is NULL
10494 static EQPGraphRow *eqp_next_row(ShellState *p, int iEqpId, EQPGraphRow *pOld){
10495 EQPGraphRow *pRow = pOld ? pOld->pNext : p->sGraph.pRow;
10496 while( pRow && pRow->iParentId!=iEqpId ) pRow = pRow->pNext;
10500 /* Render a single level of the graph that has iEqpId as its parent. Called
10501 ** recursively to render sublevels.
10503 static void eqp_render_level(ShellState *p, int iEqpId){
10504 EQPGraphRow *pRow, *pNext;
10505 int n = strlen30(p->sGraph.zPrefix);
10507 for(pRow = eqp_next_row(p, iEqpId, 0); pRow; pRow = pNext){
10508 pNext = eqp_next_row(p, iEqpId, pRow);
10510 utf8_printf(p->out, "%s%s%s\n", p->sGraph.zPrefix,
10511 pNext ? "|--" : "`--", z);
10512 if( n<(int)sizeof(p->sGraph.zPrefix)-7 ){
10513 memcpy(&p->sGraph.zPrefix[n], pNext ? "| " : " ", 4);
10514 eqp_render_level(p, pRow->iEqpId);
10515 p->sGraph.zPrefix[n] = 0;
10521 ** Display and reset the EXPLAIN QUERY PLAN data
10523 static void eqp_render(ShellState *p){
10524 EQPGraphRow *pRow = p->sGraph.pRow;
10526 if( pRow->zText[0]=='-' ){
10527 if( pRow->pNext==0 ){
10531 utf8_printf(p->out, "%s\n", pRow->zText+3);
10532 p->sGraph.pRow = pRow->pNext;
10533 sqlite3_free(pRow);
10535 utf8_printf(p->out, "QUERY PLAN\n");
10537 p->sGraph.zPrefix[0] = 0;
10538 eqp_render_level(p, 0);
10543 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10545 ** Progress handler callback.
10547 static int progress_handler(void *pClientData) {
10548 ShellState *p = (ShellState*)pClientData;
10550 if( p->nProgress>=p->mxProgress && p->mxProgress>0 ){
10551 raw_printf(p->out, "Progress limit reached (%u)\n", p->nProgress);
10552 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
10553 if( p->flgProgress & SHELL_PROGRESS_ONCE ) p->mxProgress = 0;
10556 if( (p->flgProgress & SHELL_PROGRESS_QUIET)==0 ){
10557 raw_printf(p->out, "Progress %u\n", p->nProgress);
10561 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
10564 ** This is the callback routine that the shell
10565 ** invokes for each row of a query result.
10567 static int shell_callback(
10569 int nArg, /* Number of result columns */
10570 char **azArg, /* Text of each result column */
10571 char **azCol, /* Column names */
10572 int *aiType /* Column types */
10575 ShellState *p = (ShellState*)pArg;
10577 if( azArg==0 ) return 0;
10578 switch( p->cMode ){
10581 if( azArg==0 ) break;
10582 for(i=0; i<nArg; i++){
10583 int len = strlen30(azCol[i] ? azCol[i] : "");
10584 if( len>w ) w = len;
10586 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
10587 for(i=0; i<nArg; i++){
10588 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
10589 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
10594 case MODE_Column: {
10595 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
10596 const int *colWidth;
10600 if( p->cMode==MODE_Column ){
10601 colWidth = p->colWidth;
10602 nWidth = ArraySize(p->colWidth);
10603 showHdr = p->showHeader;
10604 rowSep = p->rowSeparator;
10606 colWidth = aExplainWidths;
10607 nWidth = ArraySize(aExplainWidths);
10612 for(i=0; i<nArg; i++){
10620 w = strlenChar(azCol[i] ? azCol[i] : "");
10622 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
10625 if( i<ArraySize(p->actualWidth) ){
10626 p->actualWidth[i] = w;
10629 utf8_width_print(p->out, w, azCol[i]);
10630 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
10634 for(i=0; i<nArg; i++){
10636 if( i<ArraySize(p->actualWidth) ){
10637 w = p->actualWidth[i];
10642 utf8_printf(p->out,"%-*.*s%s",w,w,
10643 "----------------------------------------------------------"
10644 "----------------------------------------------------------",
10645 i==nArg-1 ? rowSep : " ");
10649 if( azArg==0 ) break;
10650 for(i=0; i<nArg; i++){
10652 if( i<ArraySize(p->actualWidth) ){
10653 w = p->actualWidth[i];
10657 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
10658 w = strlenChar(azArg[i]);
10660 if( i==1 && p->aiIndent && p->pStmt ){
10661 if( p->iIndent<p->nIndent ){
10662 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
10666 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
10667 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
10671 case MODE_Semi: { /* .schema and .fullschema output */
10672 printSchemaLine(p->out, azArg[0], ";\n");
10675 case MODE_Pretty: { /* .schema and .fullschema with --indent */
10683 if( azArg[0]==0 ) break;
10684 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
10685 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
10687 utf8_printf(p->out, "%s;\n", azArg[0]);
10690 z = sqlite3_mprintf("%s", azArg[0]);
10692 for(i=0; IsSpace(z[i]); i++){}
10693 for(; (c = z[i])!=0; i++){
10695 if( z[j-1]=='\r' ) z[j-1] = '\n';
10696 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
10697 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
10702 while( j>0 && IsSpace(z[j-1]) ){ j--; }
10704 if( strlen30(z)>=79 ){
10705 for(i=j=0; (c = z[i])!=0; i++){ /* Copy from z[i] back to z[j] */
10708 }else if( c=='"' || c=='\'' || c=='`' ){
10710 }else if( c=='[' ){
10712 }else if( c=='-' && z[i+1]=='-' ){
10714 }else if( c=='(' ){
10716 }else if( c==')' ){
10718 if( nLine>0 && nParen==0 && j>0 ){
10719 printSchemaLineN(p->out, z, j, "\n");
10724 if( nParen==1 && cEnd==0
10725 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
10728 printSchemaLineN(p->out, z, j, "\n ");
10731 while( IsSpace(z[i+1]) ){ i++; }
10736 printSchemaLine(p->out, z, ";\n");
10741 if( p->cnt++==0 && p->showHeader ){
10742 for(i=0; i<nArg; i++){
10743 utf8_printf(p->out,"%s%s",azCol[i],
10744 i==nArg-1 ? p->rowSeparator : p->colSeparator);
10747 if( azArg==0 ) break;
10748 for(i=0; i<nArg; i++){
10749 char *z = azArg[i];
10750 if( z==0 ) z = p->nullValue;
10751 utf8_printf(p->out, "%s", z);
10753 utf8_printf(p->out, "%s", p->colSeparator);
10755 utf8_printf(p->out, "%s", p->rowSeparator);
10761 if( p->cnt++==0 && p->showHeader ){
10762 raw_printf(p->out,"<TR>");
10763 for(i=0; i<nArg; i++){
10764 raw_printf(p->out,"<TH>");
10765 output_html_string(p->out, azCol[i]);
10766 raw_printf(p->out,"</TH>\n");
10768 raw_printf(p->out,"</TR>\n");
10770 if( azArg==0 ) break;
10771 raw_printf(p->out,"<TR>");
10772 for(i=0; i<nArg; i++){
10773 raw_printf(p->out,"<TD>");
10774 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10775 raw_printf(p->out,"</TD>\n");
10777 raw_printf(p->out,"</TR>\n");
10781 if( p->cnt++==0 && p->showHeader ){
10782 for(i=0; i<nArg; i++){
10783 output_c_string(p->out,azCol[i] ? azCol[i] : "");
10784 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10786 utf8_printf(p->out, "%s", p->rowSeparator);
10788 if( azArg==0 ) break;
10789 for(i=0; i<nArg; i++){
10790 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
10791 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
10793 utf8_printf(p->out, "%s", p->rowSeparator);
10797 setBinaryMode(p->out, 1);
10798 if( p->cnt++==0 && p->showHeader ){
10799 for(i=0; i<nArg; i++){
10800 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
10802 utf8_printf(p->out, "%s", p->rowSeparator);
10805 for(i=0; i<nArg; i++){
10806 output_csv(p, azArg[i], i<nArg-1);
10808 utf8_printf(p->out, "%s", p->rowSeparator);
10810 setTextMode(p->out, 1);
10813 case MODE_Insert: {
10814 if( azArg==0 ) break;
10815 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
10816 if( p->showHeader ){
10817 raw_printf(p->out,"(");
10818 for(i=0; i<nArg; i++){
10819 if( i>0 ) raw_printf(p->out, ",");
10820 if( quoteChar(azCol[i]) ){
10821 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
10822 utf8_printf(p->out, "%s", z);
10825 raw_printf(p->out, "%s", azCol[i]);
10828 raw_printf(p->out,")");
10831 for(i=0; i<nArg; i++){
10832 raw_printf(p->out, i>0 ? "," : " VALUES(");
10833 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10834 utf8_printf(p->out,"NULL");
10835 }else if( aiType && aiType[i]==SQLITE_TEXT ){
10836 if( ShellHasFlag(p, SHFLG_Newlines) ){
10837 output_quoted_string(p->out, azArg[i]);
10839 output_quoted_escaped_string(p->out, azArg[i]);
10841 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10842 utf8_printf(p->out,"%s", azArg[i]);
10843 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10845 double r = sqlite3_column_double(p->pStmt, i);
10847 memcpy(&ur,&r,sizeof(r));
10848 if( ur==0x7ff0000000000000LL ){
10849 raw_printf(p->out, "1e999");
10850 }else if( ur==0xfff0000000000000LL ){
10851 raw_printf(p->out, "-1e999");
10853 sqlite3_snprintf(50,z,"%!.20g", r);
10854 raw_printf(p->out, "%s", z);
10856 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10857 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10858 int nBlob = sqlite3_column_bytes(p->pStmt, i);
10859 output_hex_blob(p->out, pBlob, nBlob);
10860 }else if( isNumber(azArg[i], 0) ){
10861 utf8_printf(p->out,"%s", azArg[i]);
10862 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
10863 output_quoted_string(p->out, azArg[i]);
10865 output_quoted_escaped_string(p->out, azArg[i]);
10868 raw_printf(p->out,");\n");
10872 if( azArg==0 ) break;
10873 if( p->cnt==0 && p->showHeader ){
10874 for(i=0; i<nArg; i++){
10875 if( i>0 ) raw_printf(p->out, ",");
10876 output_quoted_string(p->out, azCol[i]);
10878 raw_printf(p->out,"\n");
10881 for(i=0; i<nArg; i++){
10882 if( i>0 ) raw_printf(p->out, ",");
10883 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
10884 utf8_printf(p->out,"NULL");
10885 }else if( aiType && aiType[i]==SQLITE_TEXT ){
10886 output_quoted_string(p->out, azArg[i]);
10887 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
10888 utf8_printf(p->out,"%s", azArg[i]);
10889 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
10891 double r = sqlite3_column_double(p->pStmt, i);
10892 sqlite3_snprintf(50,z,"%!.20g", r);
10893 raw_printf(p->out, "%s", z);
10894 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
10895 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
10896 int nBlob = sqlite3_column_bytes(p->pStmt, i);
10897 output_hex_blob(p->out, pBlob, nBlob);
10898 }else if( isNumber(azArg[i], 0) ){
10899 utf8_printf(p->out,"%s", azArg[i]);
10901 output_quoted_string(p->out, azArg[i]);
10904 raw_printf(p->out,"\n");
10908 if( p->cnt++==0 && p->showHeader ){
10909 for(i=0; i<nArg; i++){
10910 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10911 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
10913 utf8_printf(p->out, "%s", p->rowSeparator);
10915 if( azArg==0 ) break;
10916 for(i=0; i<nArg; i++){
10917 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
10918 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
10920 utf8_printf(p->out, "%s", p->rowSeparator);
10924 eqp_append(p, atoi(azArg[0]), atoi(azArg[1]), azArg[3]);
10932 ** This is the callback routine that the SQLite library
10933 ** invokes for each row of a query result.
10935 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
10936 /* since we don't have type info, call the shell_callback with a NULL value */
10937 return shell_callback(pArg, nArg, azArg, azCol, NULL);
10941 ** This is the callback routine from sqlite3_exec() that appends all
10942 ** output onto the end of a ShellText object.
10944 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
10945 ShellText *p = (ShellText*)pArg;
10947 UNUSED_PARAMETER(az);
10948 if( azArg==0 ) return 0;
10949 if( p->n ) appendText(p, "|", 0);
10950 for(i=0; i<nArg; i++){
10951 if( i ) appendText(p, ",", 0);
10952 if( azArg[i] ) appendText(p, azArg[i], 0);
10958 ** Generate an appropriate SELFTEST table in the main database.
10960 static void createSelftestTable(ShellState *p){
10962 sqlite3_exec(p->db,
10963 "SAVEPOINT selftest_init;\n"
10964 "CREATE TABLE IF NOT EXISTS selftest(\n"
10965 " tno INTEGER PRIMARY KEY,\n" /* Test number */
10966 " op TEXT,\n" /* Operator: memo run */
10967 " cmd TEXT,\n" /* Command text */
10968 " ans TEXT\n" /* Desired answer */
10970 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
10971 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
10972 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
10973 " 'memo','Tests generated by --init');\n"
10974 "INSERT INTO [_shell$self]\n"
10976 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
10977 "FROM sqlite_master ORDER BY 2'',224))',\n"
10978 " hex(sha3_query('SELECT type,name,tbl_name,sql "
10979 "FROM sqlite_master ORDER BY 2',224));\n"
10980 "INSERT INTO [_shell$self]\n"
10982 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
10983 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
10984 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
10986 " SELECT name FROM sqlite_master\n"
10987 " WHERE type='table'\n"
10988 " AND name<>'selftest'\n"
10989 " AND coalesce(rootpage,0)>0\n"
10991 " ORDER BY name;\n"
10992 "INSERT INTO [_shell$self]\n"
10993 " VALUES('run','PRAGMA integrity_check','ok');\n"
10994 "INSERT INTO selftest(tno,op,cmd,ans)"
10995 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
10996 "DROP TABLE [_shell$self];"
10999 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
11000 sqlite3_free(zErrMsg);
11002 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
11007 ** Set the destination table field of the ShellState structure to
11008 ** the name of the table given. Escape any quote characters in the
11011 static void set_table_name(ShellState *p, const char *zName){
11016 if( p->zDestTable ){
11017 free(p->zDestTable);
11020 if( zName==0 ) return;
11021 cQuote = quoteChar(zName);
11022 n = strlen30(zName);
11023 if( cQuote ) n += n+2;
11024 z = p->zDestTable = malloc( n+1 );
11025 if( z==0 ) shell_out_of_memory();
11027 if( cQuote ) z[n++] = cQuote;
11028 for(i=0; zName[i]; i++){
11030 if( zName[i]==cQuote ) z[n++] = cQuote;
11032 if( cQuote ) z[n++] = cQuote;
11038 ** Execute a query statement that will generate SQL output. Print
11039 ** the result columns, comma-separated, on a line and then add a
11040 ** semicolon terminator to the end of that line.
11042 ** If the number of columns is 1 and that column contains text "--"
11043 ** then write the semicolon on a separate line. That way, if a
11044 ** "--" comment occurs at the end of the statement, the comment
11045 ** won't consume the semicolon terminator.
11047 static int run_table_dump_query(
11048 ShellState *p, /* Query context */
11049 const char *zSelect /* SELECT statement to extract content */
11051 sqlite3_stmt *pSelect;
11056 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
11057 if( rc!=SQLITE_OK || !pSelect ){
11058 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
11059 sqlite3_errmsg(p->db));
11060 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
11063 rc = sqlite3_step(pSelect);
11064 nResult = sqlite3_column_count(pSelect);
11065 while( rc==SQLITE_ROW ){
11066 z = (const char*)sqlite3_column_text(pSelect, 0);
11067 utf8_printf(p->out, "%s", z);
11068 for(i=1; i<nResult; i++){
11069 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
11072 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
11074 raw_printf(p->out, "\n;\n");
11076 raw_printf(p->out, ";\n");
11078 rc = sqlite3_step(pSelect);
11080 rc = sqlite3_finalize(pSelect);
11081 if( rc!=SQLITE_OK ){
11082 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
11083 sqlite3_errmsg(p->db));
11084 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
11090 ** Allocate space and save off current error string.
11092 static char *save_err_msg(
11093 sqlite3 *db /* Database to query */
11095 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
11096 char *zErrMsg = sqlite3_malloc64(nErrMsg);
11098 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
11105 ** Attempt to display I/O stats on Linux using /proc/PID/io
11107 static void displayLinuxIoStats(FILE *out){
11110 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
11111 in = fopen(z, "rb");
11112 if( in==0 ) return;
11113 while( fgets(z, sizeof(z), in)!=0 ){
11114 static const struct {
11115 const char *zPattern;
11118 { "rchar: ", "Bytes received by read():" },
11119 { "wchar: ", "Bytes sent to write():" },
11120 { "syscr: ", "Read() system calls:" },
11121 { "syscw: ", "Write() system calls:" },
11122 { "read_bytes: ", "Bytes read from storage:" },
11123 { "write_bytes: ", "Bytes written to storage:" },
11124 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
11127 for(i=0; i<ArraySize(aTrans); i++){
11128 int n = strlen30(aTrans[i].zPattern);
11129 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
11130 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
11140 ** Display a single line of status using 64-bit values.
11142 static void displayStatLine(
11143 ShellState *p, /* The shell context */
11144 char *zLabel, /* Label for this one line */
11145 char *zFormat, /* Format for the result */
11146 int iStatusCtrl, /* Which status to display */
11147 int bReset /* True to reset the stats */
11149 sqlite3_int64 iCur = -1;
11150 sqlite3_int64 iHiwtr = -1;
11153 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
11154 for(i=0, nPercent=0; zFormat[i]; i++){
11155 if( zFormat[i]=='%' ) nPercent++;
11158 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
11160 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
11162 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
11166 ** Display memory stats.
11168 static int display_stats(
11169 sqlite3 *db, /* Database to query */
11170 ShellState *pArg, /* Pointer to ShellState */
11171 int bReset /* True to reset the stats */
11176 if( pArg==0 || pArg->out==0 ) return 0;
11179 if( pArg->pStmt && (pArg->statsOn & 2) ){
11181 sqlite3_stmt *pStmt = pArg->pStmt;
11183 nCol = sqlite3_column_count(pStmt);
11184 raw_printf(out, "%-36s %d\n", "Number of output columns:", nCol);
11185 for(i=0; i<nCol; i++){
11186 sqlite3_snprintf(sizeof(z),z,"Column %d %nname:", i, &x);
11187 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_name(pStmt,i));
11188 #ifndef SQLITE_OMIT_DECLTYPE
11189 sqlite3_snprintf(30, z+x, "declared type:");
11190 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_decltype(pStmt, i));
11192 #ifdef SQLITE_ENABLE_COLUMN_METADATA
11193 sqlite3_snprintf(30, z+x, "database name:");
11194 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_database_name(pStmt,i));
11195 sqlite3_snprintf(30, z+x, "table name:");
11196 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_table_name(pStmt,i));
11197 sqlite3_snprintf(30, z+x, "origin name:");
11198 utf8_printf(out, "%-36s %s\n", z, sqlite3_column_origin_name(pStmt,i));
11203 displayStatLine(pArg, "Memory Used:",
11204 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
11205 displayStatLine(pArg, "Number of Outstanding Allocations:",
11206 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
11207 if( pArg->shellFlgs & SHFLG_Pagecache ){
11208 displayStatLine(pArg, "Number of Pcache Pages Used:",
11209 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
11211 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
11212 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
11213 displayStatLine(pArg, "Largest Allocation:",
11214 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
11215 displayStatLine(pArg, "Largest Pcache Allocation:",
11216 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
11217 #ifdef YYTRACKMAXSTACKDEPTH
11218 displayStatLine(pArg, "Deepest Parser Stack:",
11219 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
11223 if( pArg->shellFlgs & SHFLG_Lookaside ){
11224 iHiwtr = iCur = -1;
11225 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
11226 &iCur, &iHiwtr, bReset);
11227 raw_printf(pArg->out,
11228 "Lookaside Slots Used: %d (max %d)\n",
11230 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
11231 &iCur, &iHiwtr, bReset);
11232 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
11234 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
11235 &iCur, &iHiwtr, bReset);
11236 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
11238 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
11239 &iCur, &iHiwtr, bReset);
11240 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
11243 iHiwtr = iCur = -1;
11244 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
11245 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
11247 iHiwtr = iCur = -1;
11248 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
11249 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
11250 iHiwtr = iCur = -1;
11251 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
11252 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
11253 iHiwtr = iCur = -1;
11254 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
11255 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
11256 iHiwtr = iCur = -1;
11257 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_SPILL, &iCur, &iHiwtr, 1);
11258 raw_printf(pArg->out, "Page cache spills: %d\n", iCur);
11259 iHiwtr = iCur = -1;
11260 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
11261 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
11263 iHiwtr = iCur = -1;
11264 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
11265 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
11270 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
11272 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
11273 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
11274 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
11275 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
11276 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
11277 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
11278 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
11279 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_REPREPARE,bReset);
11280 raw_printf(pArg->out, "Reprepare operations: %d\n", iCur);
11281 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_RUN, bReset);
11282 raw_printf(pArg->out, "Number of times run: %d\n", iCur);
11283 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_MEMUSED, bReset);
11284 raw_printf(pArg->out, "Memory used by prepared stmt: %d\n", iCur);
11288 displayLinuxIoStats(pArg->out);
11291 /* Do not remove this machine readable comment: extra-stats-output-here */
11297 ** Display scan stats.
11299 static void display_scanstats(
11300 sqlite3 *db, /* Database to query */
11301 ShellState *pArg /* Pointer to ShellState */
11303 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
11304 UNUSED_PARAMETER(db);
11305 UNUSED_PARAMETER(pArg);
11308 raw_printf(pArg->out, "-------- scanstats --------\n");
11310 for(k=0; k<=mx; k++){
11311 double rEstLoop = 1.0;
11312 for(i=n=0; 1; i++){
11313 sqlite3_stmt *p = pArg->pStmt;
11314 sqlite3_int64 nLoop, nVisit;
11317 const char *zExplain;
11318 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
11321 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
11322 if( iSid>mx ) mx = iSid;
11323 if( iSid!=k ) continue;
11325 rEstLoop = (double)nLoop;
11326 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
11329 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
11330 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
11331 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
11332 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
11334 raw_printf(pArg->out,
11335 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
11336 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
11340 raw_printf(pArg->out, "---------------------------\n");
11345 ** Parameter azArray points to a zero-terminated array of strings. zStr
11346 ** points to a single nul-terminated string. Return non-zero if zStr
11347 ** is equal, according to strcmp(), to any of the strings in the array.
11348 ** Otherwise, return zero.
11350 static int str_in_array(const char *zStr, const char **azArray){
11352 for(i=0; azArray[i]; i++){
11353 if( 0==strcmp(zStr, azArray[i]) ) return 1;
11359 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
11360 ** and populate the ShellState.aiIndent[] array with the number of
11361 ** spaces each opcode should be indented before it is output.
11363 ** The indenting rules are:
11365 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
11366 ** all opcodes that occur between the p2 jump destination and the opcode
11367 ** itself by 2 spaces.
11369 ** * For each "Goto", if the jump destination is earlier in the program
11370 ** and ends on one of:
11371 ** Yield SeekGt SeekLt RowSetRead Rewind
11372 ** or if the P1 parameter is one instead of zero,
11373 ** then indent all opcodes between the earlier instruction
11374 ** and "Goto" by 2 spaces.
11376 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
11377 const char *zSql; /* The text of the SQL statement */
11378 const char *z; /* Used to check if this is an EXPLAIN */
11379 int *abYield = 0; /* True if op is an OP_Yield */
11380 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
11381 int iOp; /* Index of operation in p->aiIndent[] */
11383 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext", 0 };
11384 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
11386 const char *azGoto[] = { "Goto", 0 };
11388 /* Try to figure out if this is really an EXPLAIN statement. If this
11389 ** cannot be verified, return early. */
11390 if( sqlite3_column_count(pSql)!=8 ){
11391 p->cMode = p->mode;
11394 zSql = sqlite3_sql(pSql);
11395 if( zSql==0 ) return;
11396 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
11397 if( sqlite3_strnicmp(z, "explain", 7) ){
11398 p->cMode = p->mode;
11402 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
11404 int iAddr = sqlite3_column_int(pSql, 0);
11405 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
11407 /* Set p2 to the P2 field of the current opcode. Then, assuming that
11408 ** p2 is an instruction address, set variable p2op to the index of that
11409 ** instruction in the aiIndent[] array. p2 and p2op may be different if
11410 ** the current instruction is part of a sub-program generated by an
11411 ** SQL trigger or foreign key. */
11412 int p2 = sqlite3_column_int(pSql, 3);
11413 int p2op = (p2 + (iOp-iAddr));
11415 /* Grow the p->aiIndent array as required */
11418 /* Do further verfication that this is explain output. Abort if
11420 static const char *explainCols[] = {
11421 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
11423 for(jj=0; jj<ArraySize(explainCols); jj++){
11424 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
11425 p->cMode = p->mode;
11426 sqlite3_reset(pSql);
11432 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
11433 if( p->aiIndent==0 ) shell_out_of_memory();
11434 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
11435 if( abYield==0 ) shell_out_of_memory();
11437 abYield[iOp] = str_in_array(zOp, azYield);
11438 p->aiIndent[iOp] = 0;
11439 p->nIndent = iOp+1;
11441 if( str_in_array(zOp, azNext) ){
11442 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11444 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
11445 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
11447 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
11452 sqlite3_free(abYield);
11453 sqlite3_reset(pSql);
11457 ** Free the array allocated by explain_data_prepare().
11459 static void explain_data_delete(ShellState *p){
11460 sqlite3_free(p->aiIndent);
11467 ** Disable and restore .wheretrace and .selecttrace settings.
11469 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11470 extern int sqlite3SelectTrace;
11471 static int savedSelectTrace;
11473 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11474 extern int sqlite3WhereTrace;
11475 static int savedWhereTrace;
11477 static void disable_debug_trace_modes(void){
11478 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11479 savedSelectTrace = sqlite3SelectTrace;
11480 sqlite3SelectTrace = 0;
11482 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11483 savedWhereTrace = sqlite3WhereTrace;
11484 sqlite3WhereTrace = 0;
11487 static void restore_debug_trace_modes(void){
11488 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
11489 sqlite3SelectTrace = savedSelectTrace;
11491 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
11492 sqlite3WhereTrace = savedWhereTrace;
11496 /* Create the TEMP table used to store parameter bindings */
11497 static void bind_table_init(ShellState *p){
11499 int defensiveMode = 0;
11500 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, -1, &defensiveMode);
11501 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, 0, 0);
11502 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, -1, &wrSchema);
11503 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, 1, 0);
11504 sqlite3_exec(p->db,
11505 "CREATE TABLE IF NOT EXISTS temp.sqlite_parameters(\n"
11506 " key TEXT PRIMARY KEY,\n"
11508 ") WITHOUT ROWID;",
11510 sqlite3_db_config(p->db, SQLITE_DBCONFIG_WRITABLE_SCHEMA, wrSchema, 0);
11511 sqlite3_db_config(p->db, SQLITE_DBCONFIG_DEFENSIVE, defensiveMode, 0);
11515 ** Bind parameters on a prepared statement.
11517 ** Parameter bindings are taken from a TEMP table of the form:
11519 ** CREATE TEMP TABLE sqlite_parameters(key TEXT PRIMARY KEY, value)
11522 ** No bindings occur if this table does not exist. The name of the table
11523 ** begins with "sqlite_" so that it will not collide with ordinary application
11524 ** tables. The table must be in the TEMP schema.
11526 static void bind_prepared_stmt(ShellState *pArg, sqlite3_stmt *pStmt){
11530 sqlite3_stmt *pQ = 0;
11532 nVar = sqlite3_bind_parameter_count(pStmt);
11533 if( nVar==0 ) return; /* Nothing to do */
11534 if( sqlite3_table_column_metadata(pArg->db, "TEMP", "sqlite_parameters",
11535 "key", 0, 0, 0, 0, 0)!=SQLITE_OK ){
11536 return; /* Parameter table does not exist */
11538 rc = sqlite3_prepare_v2(pArg->db,
11539 "SELECT value FROM temp.sqlite_parameters"
11540 " WHERE key=?1", -1, &pQ, 0);
11541 if( rc || pQ==0 ) return;
11542 for(i=1; i<=nVar; i++){
11544 const char *zVar = sqlite3_bind_parameter_name(pStmt, i);
11546 sqlite3_snprintf(sizeof(zNum),zNum,"?%d",i);
11549 sqlite3_bind_text(pQ, 1, zVar, -1, SQLITE_STATIC);
11550 if( sqlite3_step(pQ)==SQLITE_ROW ){
11551 sqlite3_bind_value(pStmt, i, sqlite3_column_value(pQ, 0));
11553 sqlite3_bind_null(pStmt, i);
11557 sqlite3_finalize(pQ);
11561 ** Run a prepared statement
11563 static void exec_prepared_stmt(
11564 ShellState *pArg, /* Pointer to ShellState */
11565 sqlite3_stmt *pStmt /* Statment to run */
11569 /* perform the first step. this will tell us if we
11570 ** have a result set or not and how wide it is.
11572 rc = sqlite3_step(pStmt);
11573 /* if we have a result set... */
11574 if( SQLITE_ROW == rc ){
11575 /* allocate space for col name ptr, value ptr, and type */
11576 int nCol = sqlite3_column_count(pStmt);
11577 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
11581 char **azCols = (char **)pData; /* Names of result columns */
11582 char **azVals = &azCols[nCol]; /* Results */
11583 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
11585 assert(sizeof(int) <= sizeof(char *));
11586 /* save off ptrs to column names */
11587 for(i=0; i<nCol; i++){
11588 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
11591 /* extract the data and data types */
11592 for(i=0; i<nCol; i++){
11593 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
11594 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
11597 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
11599 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
11601 break; /* from for */
11605 /* if data and types extracted successfully... */
11606 if( SQLITE_ROW == rc ){
11607 /* call the supplied callback with the result row data */
11608 if( shell_callback(pArg, nCol, azVals, azCols, aiTypes) ){
11611 rc = sqlite3_step(pStmt);
11614 } while( SQLITE_ROW == rc );
11615 sqlite3_free(pData);
11620 #ifndef SQLITE_OMIT_VIRTUALTABLE
11622 ** This function is called to process SQL if the previous shell command
11623 ** was ".expert". It passes the SQL in the second argument directly to
11624 ** the sqlite3expert object.
11626 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11627 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11628 ** an English language error message. It is the responsibility of the
11629 ** caller to eventually free this buffer using sqlite3_free().
11631 static int expertHandleSQL(
11632 ShellState *pState,
11636 assert( pState->expert.pExpert );
11637 assert( pzErr==0 || *pzErr==0 );
11638 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
11642 ** This function is called either to silently clean up the object
11643 ** created by the ".expert" command (if bCancel==1), or to generate a
11644 ** report from it and then clean it up (if bCancel==0).
11646 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
11647 ** code. In this case, (*pzErr) may be set to point to a buffer containing
11648 ** an English language error message. It is the responsibility of the
11649 ** caller to eventually free this buffer using sqlite3_free().
11651 static int expertFinish(
11652 ShellState *pState,
11656 int rc = SQLITE_OK;
11657 sqlite3expert *p = pState->expert.pExpert;
11659 assert( bCancel || pzErr==0 || *pzErr==0 );
11661 FILE *out = pState->out;
11662 int bVerbose = pState->expert.bVerbose;
11664 rc = sqlite3_expert_analyze(p, pzErr);
11665 if( rc==SQLITE_OK ){
11666 int nQuery = sqlite3_expert_count(p);
11670 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
11671 raw_printf(out, "-- Candidates -----------------------------\n");
11672 raw_printf(out, "%s\n", zCand);
11674 for(i=0; i<nQuery; i++){
11675 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
11676 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
11677 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
11678 if( zIdx==0 ) zIdx = "(no new indexes)\n";
11680 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
11681 raw_printf(out, "%s\n\n", zSql);
11683 raw_printf(out, "%s\n", zIdx);
11684 raw_printf(out, "%s\n", zEQP);
11688 sqlite3_expert_destroy(p);
11689 pState->expert.pExpert = 0;
11694 ** Implementation of ".expert" dot command.
11696 static int expertDotCommand(
11697 ShellState *pState, /* Current shell tool state */
11698 char **azArg, /* Array of arguments passed to dot command */
11699 int nArg /* Number of entries in azArg[] */
11701 int rc = SQLITE_OK;
11706 assert( pState->expert.pExpert==0 );
11707 memset(&pState->expert, 0, sizeof(ExpertInfo));
11709 for(i=1; rc==SQLITE_OK && i<nArg; i++){
11710 char *z = azArg[i];
11712 if( z[0]=='-' && z[1]=='-' ) z++;
11714 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
11715 pState->expert.bVerbose = 1;
11717 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
11719 raw_printf(stderr, "option requires an argument: %s\n", z);
11722 iSample = (int)integerValue(azArg[++i]);
11723 if( iSample<0 || iSample>100 ){
11724 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
11730 raw_printf(stderr, "unknown option: %s\n", z);
11735 if( rc==SQLITE_OK ){
11736 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
11737 if( pState->expert.pExpert==0 ){
11738 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
11741 sqlite3_expert_config(
11742 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
11749 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
11752 ** Execute a statement or set of statements. Print
11753 ** any result rows/columns depending on the current mode
11754 ** set via the supplied callback.
11756 ** This is very similar to SQLite's built-in sqlite3_exec()
11757 ** function except it takes a slightly different callback
11758 ** and callback data argument.
11760 static int shell_exec(
11761 ShellState *pArg, /* Pointer to ShellState */
11762 const char *zSql, /* SQL to be evaluated */
11763 char **pzErrMsg /* Error msg written here */
11765 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
11766 int rc = SQLITE_OK; /* Return Code */
11768 const char *zLeftover; /* Tail of unprocessed SQL */
11769 sqlite3 *db = pArg->db;
11775 #ifndef SQLITE_OMIT_VIRTUALTABLE
11776 if( pArg->expert.pExpert ){
11777 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
11778 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
11782 while( zSql[0] && (SQLITE_OK == rc) ){
11783 static const char *zStmtSql;
11784 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
11785 if( SQLITE_OK != rc ){
11787 *pzErrMsg = save_err_msg(db);
11791 /* this happens for a comment or white-space */
11793 while( IsSpace(zSql[0]) ) zSql++;
11796 zStmtSql = sqlite3_sql(pStmt);
11797 if( zStmtSql==0 ) zStmtSql = "";
11798 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
11800 /* save off the prepared statment handle and reset row count */
11802 pArg->pStmt = pStmt;
11806 /* echo the sql statement if echo on */
11807 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
11808 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
11811 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
11812 if( pArg && pArg->autoEQP && sqlite3_stmt_isexplain(pStmt)==0 ){
11813 sqlite3_stmt *pExplain;
11815 int triggerEQP = 0;
11816 disable_debug_trace_modes();
11817 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
11818 if( pArg->autoEQP>=AUTOEQP_trigger ){
11819 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
11821 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
11822 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11823 if( rc==SQLITE_OK ){
11824 while( sqlite3_step(pExplain)==SQLITE_ROW ){
11825 const char *zEQPLine = (const char*)sqlite3_column_text(pExplain,3);
11826 int iEqpId = sqlite3_column_int(pExplain, 0);
11827 int iParentId = sqlite3_column_int(pExplain, 1);
11828 if( zEQPLine==0 ) zEQPLine = "";
11829 if( zEQPLine[0]=='-' ) eqp_render(pArg);
11830 eqp_append(pArg, iEqpId, iParentId, zEQPLine);
11834 sqlite3_finalize(pExplain);
11835 sqlite3_free(zEQP);
11836 if( pArg->autoEQP>=AUTOEQP_full ){
11837 /* Also do an EXPLAIN for ".eqp full" mode */
11838 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
11839 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11840 if( rc==SQLITE_OK ){
11841 pArg->cMode = MODE_Explain;
11842 explain_data_prepare(pArg, pExplain);
11843 exec_prepared_stmt(pArg, pExplain);
11844 explain_data_delete(pArg);
11846 sqlite3_finalize(pExplain);
11847 sqlite3_free(zEQP);
11849 if( pArg->autoEQP>=AUTOEQP_trigger && triggerEQP==0 ){
11850 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 0, 0);
11851 /* Reprepare pStmt before reactiving trace modes */
11852 sqlite3_finalize(pStmt);
11853 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
11854 if( pArg ) pArg->pStmt = pStmt;
11856 restore_debug_trace_modes();
11860 pArg->cMode = pArg->mode;
11861 if( pArg->autoExplain ){
11862 if( sqlite3_stmt_isexplain(pStmt)==1 ){
11863 pArg->cMode = MODE_Explain;
11865 if( sqlite3_stmt_isexplain(pStmt)==2 ){
11866 pArg->cMode = MODE_EQP;
11870 /* If the shell is currently in ".explain" mode, gather the extra
11871 ** data required to add indents to the output.*/
11872 if( pArg->cMode==MODE_Explain ){
11873 explain_data_prepare(pArg, pStmt);
11877 bind_prepared_stmt(pArg, pStmt);
11878 exec_prepared_stmt(pArg, pStmt);
11879 explain_data_delete(pArg);
11882 /* print usage stats if stats on */
11883 if( pArg && pArg->statsOn ){
11884 display_stats(db, pArg, 0);
11887 /* print loop-counters if required */
11888 if( pArg && pArg->scanstatsOn ){
11889 display_scanstats(db, pArg);
11892 /* Finalize the statement just executed. If this fails, save a
11893 ** copy of the error message. Otherwise, set zSql to point to the
11894 ** next statement to execute. */
11895 rc2 = sqlite3_finalize(pStmt);
11896 if( rc!=SQLITE_NOMEM ) rc = rc2;
11897 if( rc==SQLITE_OK ){
11899 while( IsSpace(zSql[0]) ) zSql++;
11900 }else if( pzErrMsg ){
11901 *pzErrMsg = save_err_msg(db);
11904 /* clear saved stmt handle */
11906 pArg->pStmt = NULL;
11915 ** Release memory previously allocated by tableColumnList().
11917 static void freeColumnList(char **azCol){
11919 for(i=1; azCol[i]; i++){
11920 sqlite3_free(azCol[i]);
11922 /* azCol[0] is a static string */
11923 sqlite3_free(azCol);
11927 ** Return a list of pointers to strings which are the names of all
11928 ** columns in table zTab. The memory to hold the names is dynamically
11929 ** allocated and must be released by the caller using a subsequent call
11930 ** to freeColumnList().
11932 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
11933 ** value that needs to be preserved, then azCol[0] is filled in with the
11934 ** name of the rowid column.
11936 ** The first regular column in the table is azCol[1]. The list is terminated
11937 ** by an entry with azCol[i]==0.
11939 static char **tableColumnList(ShellState *p, const char *zTab){
11941 sqlite3_stmt *pStmt;
11945 int nPK = 0; /* Number of PRIMARY KEY columns seen */
11946 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
11947 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
11950 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
11951 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11952 sqlite3_free(zSql);
11954 while( sqlite3_step(pStmt)==SQLITE_ROW ){
11955 if( nCol>=nAlloc-2 ){
11956 nAlloc = nAlloc*2 + nCol + 10;
11957 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
11958 if( azCol==0 ) shell_out_of_memory();
11960 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
11961 if( sqlite3_column_int(pStmt, 5) ){
11964 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
11973 sqlite3_finalize(pStmt);
11974 if( azCol==0 ) return 0;
11978 /* The decision of whether or not a rowid really needs to be preserved
11979 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
11980 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
11981 ** rowids on tables where the rowid is inaccessible because there are other
11982 ** columns in the table named "rowid", "_rowid_", and "oid".
11984 if( preserveRowid && isIPK ){
11985 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
11986 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
11987 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
11988 ** ROWID aliases. To distinguish these cases, check to see if
11989 ** there is a "pk" entry in "PRAGMA index_list". There will be
11990 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
11992 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
11993 " WHERE origin='pk'", zTab);
11994 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11995 sqlite3_free(zSql);
11997 freeColumnList(azCol);
12000 rc = sqlite3_step(pStmt);
12001 sqlite3_finalize(pStmt);
12002 preserveRowid = rc==SQLITE_ROW;
12004 if( preserveRowid ){
12005 /* Only preserve the rowid if we can find a name to use for the
12007 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
12009 for(j=0; j<3; j++){
12010 for(i=1; i<=nCol; i++){
12011 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
12014 /* At this point, we know that azRowid[j] is not the name of any
12015 ** ordinary column in the table. Verify that azRowid[j] is a valid
12016 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
12017 ** tables will fail this last check */
12018 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
12019 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
12028 ** Toggle the reverse_unordered_selects setting.
12030 static void toggleSelectOrder(sqlite3 *db){
12031 sqlite3_stmt *pStmt = 0;
12034 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
12035 if( sqlite3_step(pStmt)==SQLITE_ROW ){
12036 iSetting = sqlite3_column_int(pStmt, 0);
12038 sqlite3_finalize(pStmt);
12039 sqlite3_snprintf(sizeof(zStmt), zStmt,
12040 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
12041 sqlite3_exec(db, zStmt, 0, 0, 0);
12045 ** This is a different callback routine used for dumping the database.
12046 ** Each row received by this callback consists of a table name,
12047 ** the table type ("index" or "table") and SQL to create the table.
12048 ** This routine should print text sufficient to recreate the table.
12050 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
12052 const char *zTable;
12055 ShellState *p = (ShellState *)pArg;
12057 UNUSED_PARAMETER(azNotUsed);
12058 if( nArg!=3 || azArg==0 ) return 0;
12063 if( strcmp(zTable, "sqlite_sequence")==0 ){
12064 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
12065 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
12066 raw_printf(p->out, "ANALYZE sqlite_master;\n");
12067 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
12069 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
12071 if( !p->writableSchema ){
12072 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
12073 p->writableSchema = 1;
12075 zIns = sqlite3_mprintf(
12076 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
12077 "VALUES('table','%q','%q',0,'%q');",
12078 zTable, zTable, zSql);
12079 utf8_printf(p->out, "%s\n", zIns);
12080 sqlite3_free(zIns);
12083 printSchemaLine(p->out, zSql, ";\n");
12086 if( strcmp(zType, "table")==0 ){
12091 char *savedDestTable;
12094 azCol = tableColumnList(p, zTable);
12100 /* Always quote the table name, even if it appears to be pure ascii,
12101 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
12103 appendText(&sTable, zTable, quoteChar(zTable));
12104 /* If preserving the rowid, add a column list after the table name.
12105 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
12106 ** instead of the usual "INSERT INTO tab VALUES(...)".
12109 appendText(&sTable, "(", 0);
12110 appendText(&sTable, azCol[0], 0);
12111 for(i=1; azCol[i]; i++){
12112 appendText(&sTable, ",", 0);
12113 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
12115 appendText(&sTable, ")", 0);
12118 /* Build an appropriate SELECT statement */
12119 initText(&sSelect);
12120 appendText(&sSelect, "SELECT ", 0);
12122 appendText(&sSelect, azCol[0], 0);
12123 appendText(&sSelect, ",", 0);
12125 for(i=1; azCol[i]; i++){
12126 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
12128 appendText(&sSelect, ",", 0);
12131 freeColumnList(azCol);
12132 appendText(&sSelect, " FROM ", 0);
12133 appendText(&sSelect, zTable, quoteChar(zTable));
12135 savedDestTable = p->zDestTable;
12136 savedMode = p->mode;
12137 p->zDestTable = sTable.z;
12138 p->mode = p->cMode = MODE_Insert;
12139 rc = shell_exec(p, sSelect.z, 0);
12140 if( (rc&0xff)==SQLITE_CORRUPT ){
12141 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
12142 toggleSelectOrder(p->db);
12143 shell_exec(p, sSelect.z, 0);
12144 toggleSelectOrder(p->db);
12146 p->zDestTable = savedDestTable;
12147 p->mode = savedMode;
12149 freeText(&sSelect);
12150 if( rc ) p->nErr++;
12156 ** Run zQuery. Use dump_callback() as the callback routine so that
12157 ** the contents of the query are output as SQL statements.
12159 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
12160 ** "ORDER BY rowid DESC" to the end.
12162 static int run_schema_dump_query(
12168 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
12169 if( rc==SQLITE_CORRUPT ){
12171 int len = strlen30(zQuery);
12172 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
12174 utf8_printf(p->out, "/****** %s ******/\n", zErr);
12175 sqlite3_free(zErr);
12178 zQ2 = malloc( len+100 );
12179 if( zQ2==0 ) return rc;
12180 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
12181 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
12183 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
12185 rc = SQLITE_CORRUPT;
12187 sqlite3_free(zErr);
12194 ** Text of help messages.
12196 ** The help text for each individual command begins with a line that starts
12197 ** with ".". Subsequent lines are supplimental information.
12199 ** There must be two or more spaces between the end of the command and the
12200 ** start of the description of what that command does.
12202 static const char *(azHelp[]) = {
12203 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
12204 ".archive ... Manage SQL archives",
12205 " Each command must have exactly one of the following options:",
12206 " -c, --create Create a new archive",
12207 " -u, --update Add or update files with changed mtime",
12208 " -i, --insert Like -u but always add even if unchanged",
12209 " -t, --list List contents of archive",
12210 " -x, --extract Extract files from archive",
12211 " Optional arguments:",
12212 " -v, --verbose Print each filename as it is processed",
12213 " -f FILE, --file FILE Use archive FILE (default is current db)",
12214 " -a FILE, --append FILE Open FILE using the apndvfs VFS",
12215 " -C DIR, --directory DIR Read/extract files from directory DIR",
12216 " -n, --dryrun Show the SQL that would have occurred",
12218 " .ar -cf ARCHIVE foo bar # Create ARCHIVE from files foo and bar",
12219 " .ar -tf ARCHIVE # List members of ARCHIVE",
12220 " .ar -xvf ARCHIVE # Verbosely extract files from ARCHIVE",
12222 " http://sqlite.org/cli.html#sqlar_archive_support",
12224 #ifndef SQLITE_OMIT_AUTHORIZATION
12225 ".auth ON|OFF Show authorizer callbacks",
12227 ".backup ?DB? FILE Backup DB (default \"main\") to FILE",
12228 " --append Use the appendvfs",
12229 " --async Write to FILE without journal and fsync()",
12230 ".bail on|off Stop after hitting an error. Default OFF",
12231 ".binary on|off Turn binary output on or off. Default OFF",
12232 ".cd DIRECTORY Change the working directory to DIRECTORY",
12233 ".changes on|off Show number of rows changed by SQL",
12234 ".check GLOB Fail if output since .testcase does not match",
12235 ".clone NEWDB Clone data into NEWDB from the existing database",
12236 ".databases List names and files of attached databases",
12237 ".dbconfig ?op? ?val? List or change sqlite3_db_config() options",
12238 ".dbinfo ?DB? Show status information about the database",
12239 ".dump ?TABLE? Render database content as SQL",
12241 " --preserve-rowids Include ROWID values in the output",
12242 " --newlines Allow unescaped newline characters in output",
12243 " TABLE is a LIKE pattern for the tables to dump",
12244 " Additional LIKE patterns can be given in subsequent arguments",
12245 ".echo on|off Turn command echo on or off",
12246 ".eqp on|off|full|... Enable or disable automatic EXPLAIN QUERY PLAN",
12248 #ifdef SQLITE_DEBUG
12249 " test Show raw EXPLAIN QUERY PLAN output",
12250 " trace Like \"full\" but enable \"PRAGMA vdbe_trace\"",
12252 " trigger Like \"full\" but also show trigger bytecode",
12253 ".excel Display the output of next command in spreadsheet",
12254 " --bom Put a UTF8 byte-order mark on intermediate file",
12255 ".exit ?CODE? Exit this program with return-code CODE",
12256 ".expert EXPERIMENTAL. Suggest indexes for queries",
12257 ".explain ?on|off|auto? Change the EXPLAIN formatting mode. Default: auto",
12258 ".filectrl CMD ... Run various sqlite3_file_control() operations",
12259 " --schema SCHEMA Use SCHEMA instead of \"main\"",
12260 " --help Show CMD details",
12261 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables",
12262 ".headers on|off Turn display of headers on or off",
12263 ".help ?-all? ?PATTERN? Show help text for PATTERN",
12264 ".import FILE TABLE Import data from FILE into TABLE",
12266 " --ascii Use \\037 and \\036 as column and row separators",
12267 " --csv Use , and \\n as column and row separators",
12268 " --skip N Skip the first N rows of input",
12269 " -v \"Verbose\" - increase auxiliary output",
12271 " * If TABLE does not exist, it is created. The first row of input",
12272 " determines the column names.",
12273 " * If neither --csv or --ascii are used, the input mode is derived",
12274 " from the \".mode\" output mode",
12275 " * If FILE begins with \"|\" then it is a command that generates the",
12277 #ifndef SQLITE_OMIT_TEST_CONTROL
12278 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX",
12280 ".indexes ?TABLE? Show names of indexes",
12281 " If TABLE is specified, only show indexes for",
12282 " tables matching TABLE using the LIKE operator.",
12283 #ifdef SQLITE_ENABLE_IOTRACE
12284 ".iotrace FILE Enable I/O diagnostic logging to FILE",
12286 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT",
12287 ".lint OPTIONS Report potential schema issues.",
12289 " fkey-indexes Find missing foreign key indexes",
12290 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12291 ".load FILE ?ENTRY? Load an extension library",
12293 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout",
12294 ".mode MODE ?TABLE? Set output mode",
12295 " MODE is one of:",
12296 " ascii Columns/rows delimited by 0x1F and 0x1E",
12297 " csv Comma-separated values",
12298 " column Left-aligned columns. (See .width)",
12299 " html HTML <table> code",
12300 " insert SQL insert statements for TABLE",
12301 " line One value per line",
12302 " list Values delimited by \"|\"",
12303 " quote Escape answers as for SQL",
12304 " tabs Tab-separated values",
12305 " tcl TCL list elements",
12306 ".nullvalue STRING Use STRING in place of NULL values",
12307 ".once ?OPTIONS? ?FILE? Output for the next SQL command only to FILE",
12308 " If FILE begins with '|' then open as a pipe",
12309 " --bom Put a UTF8 byte-order mark at the beginning",
12310 " -e Send output to the system text editor",
12311 " -x Send output as CSV to a spreadsheet (same as \".excel\")",
12312 #ifdef SQLITE_DEBUG
12313 ".oom [--repeat M] [N] Simulate an OOM error on the N-th allocation",
12315 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE",
12317 " --append Use appendvfs to append database to the end of FILE",
12318 #ifdef SQLITE_ENABLE_DESERIALIZE
12319 " --deserialize Load into memory useing sqlite3_deserialize()",
12320 " --hexdb Load the output of \"dbtotxt\" as an in-memory db",
12321 " --maxsize N Maximum size for --hexdb or --deserialized database",
12323 " --new Initialize FILE to an empty database",
12324 " --nofollow Do not follow symbolic links",
12325 " --readonly Open FILE readonly",
12326 " --zip FILE is a ZIP archive",
12327 ".output ?FILE? Send output to FILE or stdout if FILE is omitted",
12328 " If FILE begins with '|' then open it as a pipe.",
12330 " --bom Prefix output with a UTF8 byte-order mark",
12331 " -e Send output to the system text editor",
12332 " -x Send output as CSV to a spreadsheet",
12333 ".parameter CMD ... Manage SQL parameter bindings",
12334 " clear Erase all bindings",
12335 " init Initialize the TEMP table that holds bindings",
12336 " list List the current parameter bindings",
12337 " set PARAMETER VALUE Given SQL parameter PARAMETER a value of VALUE",
12338 " PARAMETER should start with one of: $ : @ ?",
12339 " unset PARAMETER Remove PARAMETER from the binding table",
12340 ".print STRING... Print literal STRING",
12341 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
12342 ".progress N Invoke progress handler after every N opcodes",
12343 " --limit N Interrupt after N progress callbacks",
12344 " --once Do no more than one progress interrupt",
12345 " --quiet|-q No output except at interrupts",
12346 " --reset Reset the count for each input and interrupt",
12348 ".prompt MAIN CONTINUE Replace the standard prompts",
12349 ".quit Exit this program",
12350 ".read FILE Read input from FILE",
12351 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12352 ".recover Recover as much data as possible from corrupt db.",
12353 " --freelist-corrupt Assume the freelist is corrupt",
12354 " --recovery-db NAME Store recovery metadata in database file NAME",
12355 " --lost-and-found TABLE Alternative name for the lost-and-found table",
12356 " --no-rowids Do not attempt to recover rowid values",
12357 " that are not also INTEGER PRIMARY KEYs",
12359 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE",
12360 ".save FILE Write in-memory database into FILE",
12361 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off",
12362 ".schema ?PATTERN? Show the CREATE statements matching PATTERN",
12364 " --indent Try to pretty-print the schema",
12365 ".selftest ?OPTIONS? Run tests defined in the SELFTEST table",
12367 " --init Create a new SELFTEST table",
12368 " -v Verbose output",
12369 ".separator COL ?ROW? Change the column and row separators",
12370 #if defined(SQLITE_ENABLE_SESSION)
12371 ".session ?NAME? CMD ... Create or control sessions",
12373 " attach TABLE Attach TABLE",
12374 " changeset FILE Write a changeset into FILE",
12375 " close Close one session",
12376 " enable ?BOOLEAN? Set or query the enable bit",
12377 " filter GLOB... Reject tables matching GLOBs",
12378 " indirect ?BOOLEAN? Mark or query the indirect status",
12379 " isempty Query whether the session is empty",
12380 " list List currently open session names",
12381 " open DB NAME Open a new session on DB",
12382 " patchset FILE Write a patchset into FILE",
12383 " If ?NAME? is omitted, the first defined session is used.",
12385 ".sha3sum ... Compute a SHA3 hash of database content",
12387 " --schema Also hash the sqlite_master table",
12388 " --sha3-224 Use the sha3-224 algorithm",
12389 " --sha3-256 Use the sha3-256 algorithm (default)",
12390 " --sha3-384 Use the sha3-384 algorithm",
12391 " --sha3-512 Use the sha3-512 algorithm",
12392 " Any other argument is a LIKE pattern for tables to hash",
12393 #ifndef SQLITE_NOHAVE_SYSTEM
12394 ".shell CMD ARGS... Run CMD ARGS... in a system shell",
12396 ".show Show the current values for various settings",
12397 ".stats ?on|off? Show stats or turn stats on or off",
12398 #ifndef SQLITE_NOHAVE_SYSTEM
12399 ".system CMD ARGS... Run CMD ARGS... in a system shell",
12401 ".tables ?TABLE? List names of tables matching LIKE pattern TABLE",
12402 ".testcase NAME Begin redirecting output to 'testcase-out.txt'",
12403 ".testctrl CMD ... Run various sqlite3_test_control() operations",
12404 " Run \".testctrl\" with no arguments for details",
12405 ".timeout MS Try opening locked tables for MS milliseconds",
12406 ".timer on|off Turn SQL timer on or off",
12407 #ifndef SQLITE_OMIT_TRACE
12408 ".trace ?OPTIONS? Output each SQL statement as it is run",
12409 " FILE Send output to FILE",
12410 " stdout Send output to stdout",
12411 " stderr Send output to stderr",
12412 " off Disable tracing",
12413 " --expanded Expand query parameters",
12414 #ifdef SQLITE_ENABLE_NORMALIZE
12415 " --normalized Normal the SQL statements",
12417 " --plain Show SQL as it is input",
12418 " --stmt Trace statement execution (SQLITE_TRACE_STMT)",
12419 " --profile Profile statements (SQLITE_TRACE_PROFILE)",
12420 " --row Trace each row (SQLITE_TRACE_ROW)",
12421 " --close Trace connection close (SQLITE_TRACE_CLOSE)",
12422 #endif /* SQLITE_OMIT_TRACE */
12423 #ifdef SQLITE_DEBUG
12424 ".unmodule NAME ... Unregister virtual table modules",
12425 " --allexcept Unregister everything except those named",
12427 ".vfsinfo ?AUX? Information about the top-level VFS",
12428 ".vfslist List all available VFSes",
12429 ".vfsname ?AUX? Print the name of the VFS stack",
12430 ".width NUM1 NUM2 ... Set column widths for \"column\" mode",
12431 " Negative values right-justify",
12435 ** Output help text.
12437 ** zPattern describes the set of commands for which help text is provided.
12438 ** If zPattern is NULL, then show all commands, but only give a one-line
12439 ** description of each.
12441 ** Return the number of matches.
12443 static int showHelp(FILE *out, const char *zPattern){
12449 || zPattern[0]=='0'
12450 || strcmp(zPattern,"-a")==0
12451 || strcmp(zPattern,"-all")==0
12452 || strcmp(zPattern,"--all")==0
12454 /* Show all commands, but only one line per command */
12455 if( zPattern==0 ) zPattern = "";
12456 for(i=0; i<ArraySize(azHelp); i++){
12457 if( azHelp[i][0]=='.' || zPattern[0] ){
12458 utf8_printf(out, "%s\n", azHelp[i]);
12463 /* Look for commands that for which zPattern is an exact prefix */
12464 zPat = sqlite3_mprintf(".%s*", zPattern);
12465 for(i=0; i<ArraySize(azHelp); i++){
12466 if( sqlite3_strglob(zPat, azHelp[i])==0 ){
12467 utf8_printf(out, "%s\n", azHelp[i]);
12472 sqlite3_free(zPat);
12475 /* when zPattern is a prefix of exactly one command, then include the
12476 ** details of that command, which should begin at offset j */
12477 while( j<ArraySize(azHelp)-1 && azHelp[j][0]!='.' ){
12478 utf8_printf(out, "%s\n", azHelp[j]);
12484 /* Look for commands that contain zPattern anywhere. Show the complete
12485 ** text of all commands that match. */
12486 zPat = sqlite3_mprintf("%%%s%%", zPattern);
12487 for(i=0; i<ArraySize(azHelp); i++){
12488 if( azHelp[i][0]=='.' ) j = i;
12489 if( sqlite3_strlike(zPat, azHelp[i], 0)==0 ){
12490 utf8_printf(out, "%s\n", azHelp[j]);
12491 while( j<ArraySize(azHelp)-1 && azHelp[j+1][0]!='.' ){
12493 utf8_printf(out, "%s\n", azHelp[j]);
12499 sqlite3_free(zPat);
12504 /* Forward reference */
12505 static int process_input(ShellState *p);
12508 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
12509 ** and return a pointer to the buffer. The caller is responsible for freeing
12512 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
12515 ** For convenience, a nul-terminator byte is always appended to the data read
12516 ** from the file before the buffer is returned. This byte is not included in
12517 ** the final value of (*pnByte), if applicable.
12519 ** NULL is returned if any error is encountered. The final value of *pnByte
12520 ** is undefined in this case.
12522 static char *readFile(const char *zName, int *pnByte){
12523 FILE *in = fopen(zName, "rb");
12527 if( in==0 ) return 0;
12528 fseek(in, 0, SEEK_END);
12531 pBuf = sqlite3_malloc64( nIn+1 );
12532 if( pBuf==0 ){ fclose(in); return 0; }
12533 nRead = fread(pBuf, nIn, 1, in);
12536 sqlite3_free(pBuf);
12540 if( pnByte ) *pnByte = nIn;
12544 #if defined(SQLITE_ENABLE_SESSION)
12546 ** Close a single OpenSession object and release all of its associated
12549 static void session_close(OpenSession *pSession){
12551 sqlite3session_delete(pSession->p);
12552 sqlite3_free(pSession->zName);
12553 for(i=0; i<pSession->nFilter; i++){
12554 sqlite3_free(pSession->azFilter[i]);
12556 sqlite3_free(pSession->azFilter);
12557 memset(pSession, 0, sizeof(OpenSession));
12562 ** Close all OpenSession objects and release all associated resources.
12564 #if defined(SQLITE_ENABLE_SESSION)
12565 static void session_close_all(ShellState *p){
12567 for(i=0; i<p->nSession; i++){
12568 session_close(&p->aSession[i]);
12573 # define session_close_all(X)
12577 ** Implementation of the xFilter function for an open session. Omit
12578 ** any tables named by ".session filter" but let all other table through.
12580 #if defined(SQLITE_ENABLE_SESSION)
12581 static int session_filter(void *pCtx, const char *zTab){
12582 OpenSession *pSession = (OpenSession*)pCtx;
12584 for(i=0; i<pSession->nFilter; i++){
12585 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
12592 ** Try to deduce the type of file for zName based on its content. Return
12593 ** one of the SHELL_OPEN_* constants.
12595 ** If the file does not exist or is empty but its name looks like a ZIP
12596 ** archive and the dfltZip flag is true, then assume it is a ZIP archive.
12597 ** Otherwise, assume an ordinary database regardless of the filename if
12598 ** the type cannot be determined from content.
12600 int deduceDatabaseType(const char *zName, int dfltZip){
12601 FILE *f = fopen(zName, "rb");
12603 int rc = SHELL_OPEN_UNSPEC;
12606 if( dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12607 return SHELL_OPEN_ZIPFILE;
12609 return SHELL_OPEN_NORMAL;
12612 n = fread(zBuf, 16, 1, f);
12613 if( n==1 && memcmp(zBuf, "SQLite format 3", 16)==0 ){
12615 return SHELL_OPEN_NORMAL;
12617 fseek(f, -25, SEEK_END);
12618 n = fread(zBuf, 25, 1, f);
12619 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
12620 rc = SHELL_OPEN_APPENDVFS;
12622 fseek(f, -22, SEEK_END);
12623 n = fread(zBuf, 22, 1, f);
12624 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
12625 && zBuf[3]==0x06 ){
12626 rc = SHELL_OPEN_ZIPFILE;
12627 }else if( n==0 && dfltZip && sqlite3_strlike("%.zip",zName,0)==0 ){
12628 rc = SHELL_OPEN_ZIPFILE;
12635 #ifdef SQLITE_ENABLE_DESERIALIZE
12637 ** Reconstruct an in-memory database using the output from the "dbtotxt"
12638 ** program. Read content from the file in p->zDbFilename. If p->zDbFilename
12639 ** is 0, then read from standard input.
12641 static unsigned char *readHexDb(ShellState *p, int *pnData){
12642 unsigned char *a = 0;
12650 unsigned int x[16];
12652 if( p->zDbFilename ){
12653 in = fopen(p->zDbFilename, "r");
12655 utf8_printf(stderr, "cannot open \"%s\" for reading\n", p->zDbFilename);
12662 if( in==0 ) in = stdin;
12666 if( fgets(zLine, sizeof(zLine), in)==0 ) goto readHexDb_error;
12667 rc = sscanf(zLine, "| size %d pagesize %d", &n, &pgsz);
12668 if( rc!=2 ) goto readHexDb_error;
12669 if( n<0 ) goto readHexDb_error;
12670 if( pgsz<512 || pgsz>65536 || (pgsz&(pgsz-1))!=0 ) goto readHexDb_error;
12671 n = (n+pgsz-1)&~(pgsz-1); /* Round n up to the next multiple of pgsz */
12672 a = sqlite3_malloc( n ? n : 1 );
12674 utf8_printf(stderr, "Out of memory!\n");
12675 goto readHexDb_error;
12678 if( pgsz<512 || pgsz>65536 || (pgsz & (pgsz-1))!=0 ){
12679 utf8_printf(stderr, "invalid pagesize\n");
12680 goto readHexDb_error;
12682 for(nLine++; fgets(zLine, sizeof(zLine), in)!=0; nLine++){
12683 rc = sscanf(zLine, "| page %d offset %d", &j, &k);
12688 if( strncmp(zLine, "| end ", 6)==0 ){
12691 rc = sscanf(zLine,"| %d: %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x %x",
12692 &j, &x[0], &x[1], &x[2], &x[3], &x[4], &x[5], &x[6], &x[7],
12693 &x[8], &x[9], &x[10], &x[11], &x[12], &x[13], &x[14], &x[15]);
12698 for(ii=0; ii<16; ii++) a[k+ii] = x[ii]&0xff;
12714 while( fgets(zLine, sizeof(zLine), p->in)!=0 ){
12716 if(strncmp(zLine, "| end ", 6)==0 ) break;
12721 utf8_printf(stderr,"Error on line %d of --hexdb input\n", nLine);
12724 #endif /* SQLITE_ENABLE_DESERIALIZE */
12727 ** Scalar function "shell_int32". The first argument to this function
12728 ** must be a blob. The second a non-negative integer. This function
12729 ** reads and returns a 32-bit big-endian integer from byte
12730 ** offset (4*<arg2>) of the blob.
12732 static void shellInt32(
12733 sqlite3_context *context,
12735 sqlite3_value **argv
12737 const unsigned char *pBlob;
12741 UNUSED_PARAMETER(argc);
12742 nBlob = sqlite3_value_bytes(argv[0]);
12743 pBlob = (const unsigned char*)sqlite3_value_blob(argv[0]);
12744 iInt = sqlite3_value_int(argv[1]);
12746 if( iInt>=0 && (iInt+1)*4<=nBlob ){
12747 const unsigned char *a = &pBlob[iInt*4];
12748 sqlite3_int64 iVal = ((sqlite3_int64)a[0]<<24)
12749 + ((sqlite3_int64)a[1]<<16)
12750 + ((sqlite3_int64)a[2]<< 8)
12751 + ((sqlite3_int64)a[3]<< 0);
12752 sqlite3_result_int64(context, iVal);
12757 ** Scalar function "shell_idquote(X)" returns string X quoted as an identifier,
12758 ** using "..." with internal double-quote characters doubled.
12760 static void shellIdQuote(
12761 sqlite3_context *context,
12763 sqlite3_value **argv
12765 const char *zName = (const char*)sqlite3_value_text(argv[0]);
12766 UNUSED_PARAMETER(argc);
12768 char *z = sqlite3_mprintf("\"%w\"", zName);
12769 sqlite3_result_text(context, z, -1, sqlite3_free);
12774 ** Scalar function "shell_escape_crnl" used by the .recover command.
12775 ** The argument passed to this function is the output of built-in
12776 ** function quote(). If the first character of the input is "'",
12777 ** indicating that the value passed to quote() was a text value,
12778 ** then this function searches the input for "\n" and "\r" characters
12779 ** and adds a wrapper similar to the following:
12781 ** replace(replace(<input>, '\n', char(10), '\r', char(13));
12783 ** Or, if the first character of the input is not "'", then a copy
12784 ** of the input is returned.
12786 static void shellEscapeCrnl(
12787 sqlite3_context *context,
12789 sqlite3_value **argv
12791 const char *zText = (const char*)sqlite3_value_text(argv[0]);
12792 UNUSED_PARAMETER(argc);
12793 if( zText[0]=='\'' ){
12794 int nText = sqlite3_value_bytes(argv[0]);
12798 const char *zNL = 0;
12799 const char *zCR = 0;
12803 for(i=0; zText[i]; i++){
12804 if( zNL==0 && zText[i]=='\n' ){
12805 zNL = unused_string(zText, "\\n", "\\012", zBuf1);
12806 nNL = (int)strlen(zNL);
12808 if( zCR==0 && zText[i]=='\r' ){
12809 zCR = unused_string(zText, "\\r", "\\015", zBuf2);
12810 nCR = (int)strlen(zCR);
12816 i64 nMax = (nNL > nCR) ? nNL : nCR;
12817 i64 nAlloc = nMax * nText + (nMax+64)*2;
12818 char *zOut = (char*)sqlite3_malloc64(nAlloc);
12820 sqlite3_result_error_nomem(context);
12825 memcpy(&zOut[iOut], "replace(replace(", 16);
12828 memcpy(&zOut[iOut], "replace(", 8);
12831 for(i=0; zText[i]; i++){
12832 if( zText[i]=='\n' ){
12833 memcpy(&zOut[iOut], zNL, nNL);
12835 }else if( zText[i]=='\r' ){
12836 memcpy(&zOut[iOut], zCR, nCR);
12839 zOut[iOut] = zText[i];
12845 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12846 memcpy(&zOut[iOut], zNL, nNL); iOut += nNL;
12847 memcpy(&zOut[iOut], "', char(10))", 12); iOut += 12;
12850 memcpy(&zOut[iOut], ",'", 2); iOut += 2;
12851 memcpy(&zOut[iOut], zCR, nCR); iOut += nCR;
12852 memcpy(&zOut[iOut], "', char(13))", 12); iOut += 12;
12855 sqlite3_result_text(context, zOut, iOut, SQLITE_TRANSIENT);
12856 sqlite3_free(zOut);
12861 sqlite3_result_value(context, argv[0]);
12864 /* Flags for open_db().
12866 ** The default behavior of open_db() is to exit(1) if the database fails to
12867 ** open. The OPEN_DB_KEEPALIVE flag changes that so that it prints an error
12868 ** but still returns without calling exit.
12870 ** The OPEN_DB_ZIPFILE flag causes open_db() to prefer to open files as a
12871 ** ZIP archive if the file does not exist or is empty and its name matches
12872 ** the *.zip pattern.
12874 #define OPEN_DB_KEEPALIVE 0x001 /* Return after error if true */
12875 #define OPEN_DB_ZIPFILE 0x002 /* Open as ZIP if name matches *.zip */
12878 ** Make sure the database is open. If it is not, then open it. If
12879 ** the database fails to open, print an error message and exit.
12881 static void open_db(ShellState *p, int openFlags){
12883 if( p->openMode==SHELL_OPEN_UNSPEC ){
12884 if( p->zDbFilename==0 || p->zDbFilename[0]==0 ){
12885 p->openMode = SHELL_OPEN_NORMAL;
12887 p->openMode = (u8)deduceDatabaseType(p->zDbFilename,
12888 (openFlags & OPEN_DB_ZIPFILE)!=0);
12891 switch( p->openMode ){
12892 case SHELL_OPEN_APPENDVFS: {
12893 sqlite3_open_v2(p->zDbFilename, &p->db,
12894 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, "apndvfs");
12897 case SHELL_OPEN_HEXDB:
12898 case SHELL_OPEN_DESERIALIZE: {
12899 sqlite3_open(0, &p->db);
12902 case SHELL_OPEN_ZIPFILE: {
12903 sqlite3_open(":memory:", &p->db);
12906 case SHELL_OPEN_READONLY: {
12907 sqlite3_open_v2(p->zDbFilename, &p->db,
12908 SQLITE_OPEN_READONLY|p->openFlags, 0);
12911 case SHELL_OPEN_UNSPEC:
12912 case SHELL_OPEN_NORMAL: {
12913 sqlite3_open_v2(p->zDbFilename, &p->db,
12914 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|p->openFlags, 0);
12919 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
12920 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
12921 p->zDbFilename, sqlite3_errmsg(p->db));
12922 if( openFlags & OPEN_DB_KEEPALIVE ){
12923 sqlite3_open(":memory:", &p->db);
12928 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12929 sqlite3_enable_load_extension(p->db, 1);
12931 sqlite3_fileio_init(p->db, 0, 0);
12932 sqlite3_shathree_init(p->db, 0, 0);
12933 sqlite3_completion_init(p->db, 0, 0);
12934 sqlite3_uint_init(p->db, 0, 0);
12935 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
12936 sqlite3_dbdata_init(p->db, 0, 0);
12938 #ifdef SQLITE_HAVE_ZLIB
12939 sqlite3_zipfile_init(p->db, 0, 0);
12940 sqlite3_sqlar_init(p->db, 0, 0);
12942 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
12943 shellAddSchemaName, 0, 0);
12944 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
12945 shellModuleSchema, 0, 0);
12946 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
12947 shellPutsFunc, 0, 0);
12948 sqlite3_create_function(p->db, "shell_escape_crnl", 1, SQLITE_UTF8, 0,
12949 shellEscapeCrnl, 0, 0);
12950 sqlite3_create_function(p->db, "shell_int32", 2, SQLITE_UTF8, 0,
12952 sqlite3_create_function(p->db, "shell_idquote", 1, SQLITE_UTF8, 0,
12953 shellIdQuote, 0, 0);
12954 #ifndef SQLITE_NOHAVE_SYSTEM
12955 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
12957 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
12960 if( p->openMode==SHELL_OPEN_ZIPFILE ){
12961 char *zSql = sqlite3_mprintf(
12962 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
12963 sqlite3_exec(p->db, zSql, 0, 0, 0);
12964 sqlite3_free(zSql);
12966 #ifdef SQLITE_ENABLE_DESERIALIZE
12968 if( p->openMode==SHELL_OPEN_DESERIALIZE || p->openMode==SHELL_OPEN_HEXDB ){
12971 unsigned char *aData;
12972 if( p->openMode==SHELL_OPEN_DESERIALIZE ){
12973 aData = (unsigned char*)readFile(p->zDbFilename, &nData);
12975 aData = readHexDb(p, &nData);
12980 rc = sqlite3_deserialize(p->db, "main", aData, nData, nData,
12981 SQLITE_DESERIALIZE_RESIZEABLE |
12982 SQLITE_DESERIALIZE_FREEONCLOSE);
12984 utf8_printf(stderr, "Error: sqlite3_deserialize() returns %d\n", rc);
12987 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_SIZE_LIMIT, &p->szMax);
12995 ** Attempt to close the databaes connection. Report errors.
12997 void close_db(sqlite3 *db){
12998 int rc = sqlite3_close(db);
13000 utf8_printf(stderr, "Error: sqlite3_close() returns %d: %s\n",
13001 rc, sqlite3_errmsg(db));
13005 #if HAVE_READLINE || HAVE_EDITLINE
13007 ** Readline completion callbacks
13009 static char *readline_completion_generator(const char *text, int state){
13010 static sqlite3_stmt *pStmt = 0;
13014 sqlite3_finalize(pStmt);
13015 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
13016 " FROM completion(%Q) ORDER BY 1", text);
13017 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
13018 sqlite3_free(zSql);
13020 if( sqlite3_step(pStmt)==SQLITE_ROW ){
13021 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
13023 sqlite3_finalize(pStmt);
13029 static char **readline_completion(const char *zText, int iStart, int iEnd){
13030 rl_attempted_completion_over = 1;
13031 return rl_completion_matches(zText, readline_completion_generator);
13034 #elif HAVE_LINENOISE
13036 ** Linenoise completion callback
13038 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
13039 int nLine = strlen30(zLine);
13041 sqlite3_stmt *pStmt = 0;
13045 if( nLine>sizeof(zBuf)-30 ) return;
13046 if( zLine[0]=='.' || zLine[0]=='#') return;
13047 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
13048 if( i==nLine-1 ) return;
13050 memcpy(zBuf, zLine, iStart);
13051 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
13052 " FROM completion(%Q,%Q) ORDER BY 1",
13053 &zLine[iStart], zLine);
13054 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
13055 sqlite3_free(zSql);
13056 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
13057 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13058 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
13059 int nCompletion = sqlite3_column_bytes(pStmt, 0);
13060 if( iStart+nCompletion < sizeof(zBuf)-1 ){
13061 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
13062 linenoiseAddCompletion(lc, zBuf);
13065 sqlite3_finalize(pStmt);
13070 ** Do C-language style dequoting.
13076 ** \v -> vertical tab
13078 ** \r -> carriage return
13083 ** \NNN -> ascii character NNN in octal
13085 static void resolve_backslashes(char *z){
13088 while( *z && *z!='\\' ) z++;
13089 for(i=j=0; (c = z[i])!=0; i++, j++){
13090 if( c=='\\' && z[i+1]!=0 ){
13094 }else if( c=='b' ){
13096 }else if( c=='t' ){
13098 }else if( c=='n' ){
13100 }else if( c=='v' ){
13102 }else if( c=='f' ){
13104 }else if( c=='r' ){
13106 }else if( c=='"' ){
13108 }else if( c=='\'' ){
13110 }else if( c=='\\' ){
13112 }else if( c>='0' && c<='7' ){
13114 if( z[i+1]>='0' && z[i+1]<='7' ){
13116 c = (c<<3) + z[i] - '0';
13117 if( z[i+1]>='0' && z[i+1]<='7' ){
13119 c = (c<<3) + z[i] - '0';
13126 if( j<i ) z[j] = 0;
13130 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
13131 ** for TRUE and FALSE. Return the integer value if appropriate.
13133 static int booleanValue(const char *zArg){
13135 if( zArg[0]=='0' && zArg[1]=='x' ){
13136 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
13138 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
13140 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
13141 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
13144 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
13147 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
13153 ** Set or clear a shell flag according to a boolean value.
13155 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
13156 if( booleanValue(zArg) ){
13157 ShellSetFlag(p, mFlag);
13159 ShellClearFlag(p, mFlag);
13164 ** Close an output file, assuming it is not stderr or stdout
13166 static void output_file_close(FILE *f){
13167 if( f && f!=stdout && f!=stderr ) fclose(f);
13171 ** Try to open an output file. The names "stdout" and "stderr" are
13172 ** recognized and do the right thing. NULL is returned if the output
13173 ** filename is "off".
13175 static FILE *output_file_open(const char *zFile, int bTextMode){
13177 if( strcmp(zFile,"stdout")==0 ){
13179 }else if( strcmp(zFile, "stderr")==0 ){
13181 }else if( strcmp(zFile, "off")==0 ){
13184 f = fopen(zFile, bTextMode ? "w" : "wb");
13186 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
13192 #ifndef SQLITE_OMIT_TRACE
13194 ** A routine for handling output from sqlite3_trace().
13196 static int sql_trace_callback(
13197 unsigned mType, /* The trace type */
13198 void *pArg, /* The ShellState pointer */
13199 void *pP, /* Usually a pointer to sqlite_stmt */
13200 void *pX /* Auxiliary output */
13202 ShellState *p = (ShellState*)pArg;
13203 sqlite3_stmt *pStmt;
13206 if( p->traceOut==0 ) return 0;
13207 if( mType==SQLITE_TRACE_CLOSE ){
13208 utf8_printf(p->traceOut, "-- closing database connection\n");
13211 if( mType!=SQLITE_TRACE_ROW && ((const char*)pX)[0]=='-' ){
13212 zSql = (const char*)pX;
13214 pStmt = (sqlite3_stmt*)pP;
13215 switch( p->eTraceType ){
13216 case SHELL_TRACE_EXPANDED: {
13217 zSql = sqlite3_expanded_sql(pStmt);
13220 #ifdef SQLITE_ENABLE_NORMALIZE
13221 case SHELL_TRACE_NORMALIZED: {
13222 zSql = sqlite3_normalized_sql(pStmt);
13227 zSql = sqlite3_sql(pStmt);
13232 if( zSql==0 ) return 0;
13233 nSql = strlen30(zSql);
13234 while( nSql>0 && zSql[nSql-1]==';' ){ nSql--; }
13236 case SQLITE_TRACE_ROW:
13237 case SQLITE_TRACE_STMT: {
13238 utf8_printf(p->traceOut, "%.*s;\n", nSql, zSql);
13241 case SQLITE_TRACE_PROFILE: {
13242 sqlite3_int64 nNanosec = *(sqlite3_int64*)pX;
13243 utf8_printf(p->traceOut, "%.*s; -- %lld ns\n", nSql, zSql, nNanosec);
13252 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
13253 ** a useful spot to set a debugger breakpoint.
13255 static void test_breakpoint(void){
13256 static int nCall = 0;
13261 ** An object used to read a CSV and other files for import.
13263 typedef struct ImportCtx ImportCtx;
13265 const char *zFile; /* Name of the input file */
13266 FILE *in; /* Read the CSV text from this input stream */
13267 char *z; /* Accumulated text for a field */
13268 int n; /* Number of bytes in z */
13269 int nAlloc; /* Space allocated for z[] */
13270 int nLine; /* Current line number */
13271 int nRow; /* Number of rows imported */
13272 int nErr; /* Number of errors encountered */
13273 int bNotFirst; /* True if one or more bytes already read */
13274 int cTerm; /* Character that terminated the most recent field */
13275 int cColSep; /* The column separator character. (Usually ",") */
13276 int cRowSep; /* The row separator character. (Usually "\n") */
13279 /* Append a single byte to z[] */
13280 static void import_append_char(ImportCtx *p, int c){
13281 if( p->n+1>=p->nAlloc ){
13282 p->nAlloc += p->nAlloc + 100;
13283 p->z = sqlite3_realloc64(p->z, p->nAlloc);
13284 if( p->z==0 ) shell_out_of_memory();
13286 p->z[p->n++] = (char)c;
13289 /* Read a single field of CSV text. Compatible with rfc4180 and extended
13290 ** with the option of having a separator other than ",".
13292 ** + Input comes from p->in.
13293 ** + Store results in p->z of length p->n. Space to hold p->z comes
13294 ** from sqlite3_malloc64().
13295 ** + Use p->cSep as the column separator. The default is ",".
13296 ** + Use p->rSep as the row separator. The default is "\n".
13297 ** + Keep track of the line number in p->nLine.
13298 ** + Store the character that terminates the field in p->cTerm. Store
13299 ** EOF on end-of-file.
13300 ** + Report syntax errors on stderr
13302 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
13304 int cSep = p->cColSep;
13305 int rSep = p->cRowSep;
13308 if( c==EOF || seenInterrupt ){
13314 int startLine = p->nLine;
13319 if( c==rSep ) p->nLine++;
13326 if( (c==cSep && pc==cQuote)
13327 || (c==rSep && pc==cQuote)
13328 || (c==rSep && pc=='\r' && ppc==cQuote)
13329 || (c==EOF && pc==cQuote)
13331 do{ p->n--; }while( p->z[p->n]!=cQuote );
13335 if( pc==cQuote && c!='\r' ){
13336 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
13337 p->zFile, p->nLine, cQuote);
13340 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
13341 p->zFile, startLine, cQuote);
13345 import_append_char(p, c);
13350 /* If this is the first field being parsed and it begins with the
13351 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
13352 if( (c&0xff)==0xef && p->bNotFirst==0 ){
13353 import_append_char(p, c);
13355 if( (c&0xff)==0xbb ){
13356 import_append_char(p, c);
13358 if( (c&0xff)==0xbf ){
13361 return csv_read_one_field(p);
13365 while( c!=EOF && c!=cSep && c!=rSep ){
13366 import_append_char(p, c);
13371 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
13375 if( p->z ) p->z[p->n] = 0;
13380 /* Read a single field of ASCII delimited text.
13382 ** + Input comes from p->in.
13383 ** + Store results in p->z of length p->n. Space to hold p->z comes
13384 ** from sqlite3_malloc64().
13385 ** + Use p->cSep as the column separator. The default is "\x1F".
13386 ** + Use p->rSep as the row separator. The default is "\x1E".
13387 ** + Keep track of the row number in p->nLine.
13388 ** + Store the character that terminates the field in p->cTerm. Store
13389 ** EOF on end-of-file.
13390 ** + Report syntax errors on stderr
13392 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
13394 int cSep = p->cColSep;
13395 int rSep = p->cRowSep;
13398 if( c==EOF || seenInterrupt ){
13402 while( c!=EOF && c!=cSep && c!=rSep ){
13403 import_append_char(p, c);
13410 if( p->z ) p->z[p->n] = 0;
13415 ** Try to transfer data for table zTable. If an error is seen while
13416 ** moving forward, try to go backwards. The backwards movement won't
13417 ** work for WITHOUT ROWID tables.
13419 static void tryToCloneData(
13424 sqlite3_stmt *pQuery = 0;
13425 sqlite3_stmt *pInsert = 0;
13430 int nTable = strlen30(zTable);
13433 const int spinRate = 10000;
13435 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
13436 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13438 utf8_printf(stderr, "Error %d: %s on [%s]\n",
13439 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13441 goto end_data_xfer;
13443 n = sqlite3_column_count(pQuery);
13444 zInsert = sqlite3_malloc64(200 + nTable + n*3);
13445 if( zInsert==0 ) shell_out_of_memory();
13446 sqlite3_snprintf(200+nTable,zInsert,
13447 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
13448 i = strlen30(zInsert);
13449 for(j=1; j<n; j++){
13450 memcpy(zInsert+i, ",?", 2);
13453 memcpy(zInsert+i, ");", 3);
13454 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
13456 utf8_printf(stderr, "Error %d: %s on [%s]\n",
13457 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
13459 goto end_data_xfer;
13461 for(k=0; k<2; k++){
13462 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13463 for(i=0; i<n; i++){
13464 switch( sqlite3_column_type(pQuery, i) ){
13465 case SQLITE_NULL: {
13466 sqlite3_bind_null(pInsert, i+1);
13469 case SQLITE_INTEGER: {
13470 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
13473 case SQLITE_FLOAT: {
13474 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
13477 case SQLITE_TEXT: {
13478 sqlite3_bind_text(pInsert, i+1,
13479 (const char*)sqlite3_column_text(pQuery,i),
13480 -1, SQLITE_STATIC);
13483 case SQLITE_BLOB: {
13484 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
13485 sqlite3_column_bytes(pQuery,i),
13491 rc = sqlite3_step(pInsert);
13492 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
13493 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
13494 sqlite3_errmsg(newDb));
13496 sqlite3_reset(pInsert);
13498 if( (cnt%spinRate)==0 ){
13499 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
13503 if( rc==SQLITE_DONE ) break;
13504 sqlite3_finalize(pQuery);
13505 sqlite3_free(zQuery);
13506 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
13508 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13510 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
13513 } /* End for(k=0...) */
13516 sqlite3_finalize(pQuery);
13517 sqlite3_finalize(pInsert);
13518 sqlite3_free(zQuery);
13519 sqlite3_free(zInsert);
13524 ** Try to transfer all rows of the schema that match zWhere. For
13525 ** each row, invoke xForEach() on the object defined by that row.
13526 ** If an error is encountered while moving forward through the
13527 ** sqlite_master table, try again moving backwards.
13529 static void tryToCloneSchema(
13532 const char *zWhere,
13533 void (*xForEach)(ShellState*,sqlite3*,const char*)
13535 sqlite3_stmt *pQuery = 0;
13538 const unsigned char *zName;
13539 const unsigned char *zSql;
13542 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13543 " WHERE %s", zWhere);
13544 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13546 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13547 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13549 goto end_schema_xfer;
13551 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13552 zName = sqlite3_column_text(pQuery, 0);
13553 zSql = sqlite3_column_text(pQuery, 1);
13554 printf("%s... ", zName); fflush(stdout);
13555 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13557 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13558 sqlite3_free(zErrMsg);
13562 xForEach(p, newDb, (const char*)zName);
13566 if( rc!=SQLITE_DONE ){
13567 sqlite3_finalize(pQuery);
13568 sqlite3_free(zQuery);
13569 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
13570 " WHERE %s ORDER BY rowid DESC", zWhere);
13571 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
13573 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
13574 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
13576 goto end_schema_xfer;
13578 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
13579 zName = sqlite3_column_text(pQuery, 0);
13580 zSql = sqlite3_column_text(pQuery, 1);
13581 printf("%s... ", zName); fflush(stdout);
13582 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
13584 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
13585 sqlite3_free(zErrMsg);
13589 xForEach(p, newDb, (const char*)zName);
13595 sqlite3_finalize(pQuery);
13596 sqlite3_free(zQuery);
13600 ** Open a new database file named "zNewDb". Try to recover as much information
13601 ** as possible out of the main database (which might be corrupt) and write it
13604 static void tryToClone(ShellState *p, const char *zNewDb){
13606 sqlite3 *newDb = 0;
13607 if( access(zNewDb,0)==0 ){
13608 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
13611 rc = sqlite3_open(zNewDb, &newDb);
13613 utf8_printf(stderr, "Cannot create output database: %s\n",
13614 sqlite3_errmsg(newDb));
13616 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
13617 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
13618 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
13619 tryToCloneSchema(p, newDb, "type!='table'", 0);
13620 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
13621 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
13627 ** Change the output file back to stdout.
13629 ** If the p->doXdgOpen flag is set, that means the output was being
13630 ** redirected to a temporary file named by p->zTempFile. In that case,
13631 ** launch start/open/xdg-open on that temporary file.
13633 static void output_reset(ShellState *p){
13634 if( p->outfile[0]=='|' ){
13635 #ifndef SQLITE_OMIT_POPEN
13639 output_file_close(p->out);
13640 #ifndef SQLITE_NOHAVE_SYSTEM
13641 if( p->doXdgOpen ){
13642 const char *zXdgOpenCmd =
13643 #if defined(_WIN32)
13645 #elif defined(__APPLE__)
13651 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
13652 if( system(zCmd) ){
13653 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
13655 /* Give the start/open/xdg-open command some time to get
13656 ** going before we continue, and potential delete the
13657 ** p->zTempFile data file out from under it */
13658 sqlite3_sleep(2000);
13660 sqlite3_free(zCmd);
13664 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
13671 ** Run an SQL command and return the single integer result.
13673 static int db_int(ShellState *p, const char *zSql){
13674 sqlite3_stmt *pStmt;
13676 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13677 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
13678 res = sqlite3_column_int(pStmt,0);
13680 sqlite3_finalize(pStmt);
13685 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
13687 static unsigned int get2byteInt(unsigned char *a){
13688 return (a[0]<<8) + a[1];
13690 static unsigned int get4byteInt(unsigned char *a){
13691 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
13695 ** Implementation of the ".dbinfo" command.
13697 ** Return 1 on error, 2 to exit, and 0 otherwise.
13699 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
13700 static const struct { const char *zName; int ofst; } aField[] = {
13701 { "file change counter:", 24 },
13702 { "database page count:", 28 },
13703 { "freelist page count:", 36 },
13704 { "schema cookie:", 40 },
13705 { "schema format:", 44 },
13706 { "default cache size:", 48 },
13707 { "autovacuum top root:", 52 },
13708 { "incremental vacuum:", 64 },
13709 { "text encoding:", 56 },
13710 { "user version:", 60 },
13711 { "application id:", 68 },
13712 { "software version:", 96 },
13714 static const struct { const char *zName; const char *zSql; } aQuery[] = {
13715 { "number of tables:",
13716 "SELECT count(*) FROM %s WHERE type='table'" },
13717 { "number of indexes:",
13718 "SELECT count(*) FROM %s WHERE type='index'" },
13719 { "number of triggers:",
13720 "SELECT count(*) FROM %s WHERE type='trigger'" },
13721 { "number of views:",
13722 "SELECT count(*) FROM %s WHERE type='view'" },
13724 "SELECT total(length(sql)) FROM %s" },
13727 unsigned iDataVersion;
13729 char *zDb = nArg>=2 ? azArg[1] : "main";
13730 sqlite3_stmt *pStmt = 0;
13731 unsigned char aHdr[100];
13733 if( p->db==0 ) return 1;
13734 rc = sqlite3_prepare_v2(p->db,
13735 "SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
13738 utf8_printf(stderr, "error: %s\n", sqlite3_errmsg(p->db));
13739 sqlite3_finalize(pStmt);
13742 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
13743 if( sqlite3_step(pStmt)==SQLITE_ROW
13744 && sqlite3_column_bytes(pStmt,0)>100
13746 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
13747 sqlite3_finalize(pStmt);
13749 raw_printf(stderr, "unable to read database header\n");
13750 sqlite3_finalize(pStmt);
13753 i = get2byteInt(aHdr+16);
13754 if( i==1 ) i = 65536;
13755 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
13756 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
13757 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
13758 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
13759 for(i=0; i<ArraySize(aField); i++){
13760 int ofst = aField[i].ofst;
13761 unsigned int val = get4byteInt(aHdr + ofst);
13762 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
13765 if( val==1 ) raw_printf(p->out, " (utf8)");
13766 if( val==2 ) raw_printf(p->out, " (utf16le)");
13767 if( val==3 ) raw_printf(p->out, " (utf16be)");
13770 raw_printf(p->out, "\n");
13773 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
13774 }else if( strcmp(zDb,"temp")==0 ){
13775 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
13777 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
13779 for(i=0; i<ArraySize(aQuery); i++){
13780 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
13781 int val = db_int(p, zSql);
13782 sqlite3_free(zSql);
13783 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
13785 sqlite3_free(zSchemaTab);
13786 sqlite3_file_control(p->db, zDb, SQLITE_FCNTL_DATA_VERSION, &iDataVersion);
13787 utf8_printf(p->out, "%-20s %u\n", "data version", iDataVersion);
13792 ** Print the current sqlite3_errmsg() value to stderr and return 1.
13794 static int shellDatabaseError(sqlite3 *db){
13795 const char *zErr = sqlite3_errmsg(db);
13796 utf8_printf(stderr, "Error: %s\n", zErr);
13801 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
13802 ** if they match and FALSE (0) if they do not match.
13806 ** '*' Matches any sequence of zero or more characters.
13808 ** '?' Matches exactly one character.
13810 ** [...] Matches one character from the enclosed list of
13813 ** [^...] Matches one character not in the enclosed list.
13815 ** '#' Matches any sequence of one or more digits with an
13816 ** optional + or - sign in front
13818 ** ' ' Any span of whitespace matches any other span of
13821 ** Extra whitespace at the end of z[] is ignored.
13823 static int testcase_glob(const char *zGlob, const char *z){
13828 while( (c = (*(zGlob++)))!=0 ){
13830 if( !IsSpace(*z) ) return 0;
13831 while( IsSpace(*zGlob) ) zGlob++;
13832 while( IsSpace(*z) ) z++;
13833 }else if( c=='*' ){
13834 while( (c=(*(zGlob++))) == '*' || c=='?' ){
13835 if( c=='?' && (*(z++))==0 ) return 0;
13839 }else if( c=='[' ){
13840 while( *z && testcase_glob(zGlob-1,z)==0 ){
13845 while( (c2 = (*(z++)))!=0 ){
13848 if( c2==0 ) return 0;
13850 if( testcase_glob(zGlob,z) ) return 1;
13853 }else if( c=='?' ){
13854 if( (*(z++))==0 ) return 0;
13855 }else if( c=='[' ){
13860 if( c==0 ) return 0;
13867 if( c==']' ) seen = 1;
13870 while( c2 && c2!=']' ){
13871 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
13873 if( c>=prior_c && c<=c2 ) seen = 1;
13883 if( c2==0 || (seen ^ invert)==0 ) return 0;
13884 }else if( c=='#' ){
13885 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
13886 if( !IsDigit(z[0]) ) return 0;
13888 while( IsDigit(z[0]) ){ z++; }
13890 if( c!=(*(z++)) ) return 0;
13893 while( IsSpace(*z) ){ z++; }
13899 ** Compare the string as a command-line option with either one or two
13900 ** initial "-" characters.
13902 static int optionMatch(const char *zStr, const char *zOpt){
13903 if( zStr[0]!='-' ) return 0;
13905 if( zStr[0]=='-' ) zStr++;
13906 return strcmp(zStr, zOpt)==0;
13912 int shellDeleteFile(const char *zFilename){
13915 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
13919 rc = unlink(zFilename);
13925 ** Try to delete the temporary file (if there is one) and free the
13926 ** memory used to hold the name of the temp file.
13928 static void clearTempFile(ShellState *p){
13929 if( p->zTempFile==0 ) return;
13930 if( p->doXdgOpen ) return;
13931 if( shellDeleteFile(p->zTempFile) ) return;
13932 sqlite3_free(p->zTempFile);
13937 ** Create a new temp file name with the given suffix.
13939 static void newTempFile(ShellState *p, const char *zSuffix){
13941 sqlite3_free(p->zTempFile);
13944 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
13946 if( p->zTempFile==0 ){
13947 /* If p->db is an in-memory database then the TEMPFILENAME file-control
13948 ** will not work and we will need to fallback to guessing */
13951 sqlite3_randomness(sizeof(r), &r);
13952 zTemp = getenv("TEMP");
13953 if( zTemp==0 ) zTemp = getenv("TMP");
13961 p->zTempFile = sqlite3_mprintf("%s/temp%llx.%s", zTemp, r, zSuffix);
13963 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
13965 if( p->zTempFile==0 ){
13966 raw_printf(stderr, "out of memory\n");
13973 ** The implementation of SQL scalar function fkey_collate_clause(), used
13974 ** by the ".lint fkey-indexes" command. This scalar function is always
13975 ** called with four arguments - the parent table name, the parent column name,
13976 ** the child table name and the child column name.
13978 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
13980 ** If either of the named tables or columns do not exist, this function
13981 ** returns an empty string. An empty string is also returned if both tables
13982 ** and columns exist but have the same default collation sequence. Or,
13983 ** if both exist but the default collation sequences are different, this
13984 ** function returns the string " COLLATE <parent-collation>", where
13985 ** <parent-collation> is the default collation sequence of the parent column.
13987 static void shellFkeyCollateClause(
13988 sqlite3_context *pCtx,
13990 sqlite3_value **apVal
13992 sqlite3 *db = sqlite3_context_db_handle(pCtx);
13993 const char *zParent;
13994 const char *zParentCol;
13995 const char *zParentSeq;
13996 const char *zChild;
13997 const char *zChildCol;
13998 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
14002 zParent = (const char*)sqlite3_value_text(apVal[0]);
14003 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
14004 zChild = (const char*)sqlite3_value_text(apVal[2]);
14005 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
14007 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
14008 rc = sqlite3_table_column_metadata(
14009 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
14011 if( rc==SQLITE_OK ){
14012 rc = sqlite3_table_column_metadata(
14013 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
14017 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
14018 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
14019 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
14026 ** The implementation of dot-command ".lint fkey-indexes".
14028 static int lintFkeyIndexes(
14029 ShellState *pState, /* Current shell tool state */
14030 char **azArg, /* Array of arguments passed to dot command */
14031 int nArg /* Number of entries in azArg[] */
14033 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
14034 FILE *out = pState->out; /* Stream to write non-error output to */
14035 int bVerbose = 0; /* If -verbose is present */
14036 int bGroupByParent = 0; /* If -groupbyparent is present */
14037 int i; /* To iterate through azArg[] */
14038 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
14039 int rc; /* Return code */
14040 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
14043 ** This SELECT statement returns one row for each foreign key constraint
14044 ** in the schema of the main database. The column values are:
14046 ** 0. The text of an SQL statement similar to:
14048 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
14050 ** This SELECT is similar to the one that the foreign keys implementation
14051 ** needs to run internally on child tables. If there is an index that can
14052 ** be used to optimize this query, then it can also be used by the FK
14053 ** implementation to optimize DELETE or UPDATE statements on the parent
14056 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
14057 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
14058 ** contains an index that can be used to optimize the query.
14060 ** 2. Human readable text that describes the child table and columns. e.g.
14062 ** "child_table(child_key1, child_key2)"
14064 ** 3. Human readable text that describes the parent table and columns. e.g.
14066 ** "parent_table(parent_key1, parent_key2)"
14068 ** 4. A full CREATE INDEX statement for an index that could be used to
14069 ** optimize DELETE or UPDATE statements on the parent table. e.g.
14071 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
14073 ** 5. The name of the parent table.
14075 ** These six values are used by the C logic below to generate the report.
14079 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
14080 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
14081 " || fkey_collate_clause("
14082 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
14084 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
14085 " || group_concat('*=?', ' AND ') || ')'"
14087 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
14089 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
14091 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
14092 " || ' ON ' || quote(s.name) || '('"
14093 " || group_concat(quote(f.[from]) ||"
14094 " fkey_collate_clause("
14095 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
14099 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
14100 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
14101 "GROUP BY s.name, f.id "
14102 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
14104 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
14106 for(i=2; i<nArg; i++){
14107 int n = strlen30(azArg[i]);
14108 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
14111 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
14112 bGroupByParent = 1;
14116 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
14119 return SQLITE_ERROR;
14123 /* Register the fkey_collate_clause() SQL function */
14124 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
14125 0, shellFkeyCollateClause, 0, 0
14129 if( rc==SQLITE_OK ){
14130 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
14132 if( rc==SQLITE_OK ){
14133 sqlite3_bind_int(pSql, 1, bGroupByParent);
14136 if( rc==SQLITE_OK ){
14139 while( SQLITE_ROW==sqlite3_step(pSql) ){
14141 sqlite3_stmt *pExplain = 0;
14142 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
14143 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
14144 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
14145 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
14146 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
14147 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
14149 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
14150 if( rc!=SQLITE_OK ) break;
14151 if( SQLITE_ROW==sqlite3_step(pExplain) ){
14152 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
14154 0==sqlite3_strglob(zGlob, zPlan)
14155 || 0==sqlite3_strglob(zGlobIPK, zPlan)
14158 rc = sqlite3_finalize(pExplain);
14159 if( rc!=SQLITE_OK ) break;
14162 raw_printf(stderr, "Error: internal error");
14166 && (bVerbose || res==0)
14167 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
14169 raw_printf(out, "-- Parent table %s\n", zParent);
14170 sqlite3_free(zPrev);
14171 zPrev = sqlite3_mprintf("%s", zParent);
14175 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
14176 }else if( bVerbose ){
14177 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
14178 zIndent, zFrom, zTarget
14183 sqlite3_free(zPrev);
14185 if( rc!=SQLITE_OK ){
14186 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14189 rc2 = sqlite3_finalize(pSql);
14190 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
14192 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14195 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
14202 ** Implementation of ".lint" dot command.
14204 static int lintDotCommand(
14205 ShellState *pState, /* Current shell tool state */
14206 char **azArg, /* Array of arguments passed to dot command */
14207 int nArg /* Number of entries in azArg[] */
14210 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
14211 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
14212 return lintFkeyIndexes(pState, azArg, nArg);
14215 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
14216 raw_printf(stderr, "Where sub-commands are:\n");
14217 raw_printf(stderr, " fkey-indexes\n");
14218 return SQLITE_ERROR;
14221 #if !defined SQLITE_OMIT_VIRTUALTABLE
14222 static void shellPrepare(
14226 sqlite3_stmt **ppStmt
14229 if( *pRc==SQLITE_OK ){
14230 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
14231 if( rc!=SQLITE_OK ){
14232 raw_printf(stderr, "sql error: %s (%d)\n",
14233 sqlite3_errmsg(db), sqlite3_errcode(db)
14241 ** Create a prepared statement using printf-style arguments for the SQL.
14243 ** This routine is could be marked "static". But it is not always used,
14244 ** depending on compile-time options. By omitting the "static", we avoid
14245 ** nuisance compiler warnings about "defined but not used".
14247 void shellPreparePrintf(
14250 sqlite3_stmt **ppStmt,
14255 if( *pRc==SQLITE_OK ){
14258 va_start(ap, zFmt);
14259 z = sqlite3_vmprintf(zFmt, ap);
14262 *pRc = SQLITE_NOMEM;
14264 shellPrepare(db, pRc, z, ppStmt);
14270 /* Finalize the prepared statement created using shellPreparePrintf().
14272 ** This routine is could be marked "static". But it is not always used,
14273 ** depending on compile-time options. By omitting the "static", we avoid
14274 ** nuisance compiler warnings about "defined but not used".
14276 void shellFinalize(
14278 sqlite3_stmt *pStmt
14281 sqlite3 *db = sqlite3_db_handle(pStmt);
14282 int rc = sqlite3_finalize(pStmt);
14283 if( *pRc==SQLITE_OK ){
14284 if( rc!=SQLITE_OK ){
14285 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14292 /* Reset the prepared statement created using shellPreparePrintf().
14294 ** This routine is could be marked "static". But it is not always used,
14295 ** depending on compile-time options. By omitting the "static", we avoid
14296 ** nuisance compiler warnings about "defined but not used".
14300 sqlite3_stmt *pStmt
14302 int rc = sqlite3_reset(pStmt);
14303 if( *pRc==SQLITE_OK ){
14304 if( rc!=SQLITE_OK ){
14305 sqlite3 *db = sqlite3_db_handle(pStmt);
14306 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
14311 #endif /* !defined SQLITE_OMIT_VIRTUALTABLE */
14313 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
14314 /******************************************************************************
14315 ** The ".archive" or ".ar" command.
14318 ** Structure representing a single ".ar" command.
14320 typedef struct ArCommand ArCommand;
14322 u8 eCmd; /* An AR_CMD_* value */
14323 u8 bVerbose; /* True if --verbose */
14324 u8 bZip; /* True if the archive is a ZIP */
14325 u8 bDryRun; /* True if --dry-run */
14326 u8 bAppend; /* True if --append */
14327 u8 fromCmdLine; /* Run from -A instead of .archive */
14328 int nArg; /* Number of command arguments */
14329 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
14330 const char *zFile; /* --file argument, or NULL */
14331 const char *zDir; /* --directory argument, or NULL */
14332 char **azArg; /* Array of command arguments */
14333 ShellState *p; /* Shell state */
14334 sqlite3 *db; /* Database containing the archive */
14338 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
14340 static int arUsage(FILE *f){
14341 showHelp(f,"archive");
14342 return SQLITE_ERROR;
14346 ** Print an error message for the .ar command to stderr and return
14349 static int arErrorMsg(ArCommand *pAr, const char *zFmt, ...){
14352 va_start(ap, zFmt);
14353 z = sqlite3_vmprintf(zFmt, ap);
14355 utf8_printf(stderr, "Error: %s\n", z);
14356 if( pAr->fromCmdLine ){
14357 utf8_printf(stderr, "Use \"-A\" for more help\n");
14359 utf8_printf(stderr, "Use \".archive --help\" for more help\n");
14362 return SQLITE_ERROR;
14366 ** Values for ArCommand.eCmd.
14368 #define AR_CMD_CREATE 1
14369 #define AR_CMD_UPDATE 2
14370 #define AR_CMD_INSERT 3
14371 #define AR_CMD_EXTRACT 4
14372 #define AR_CMD_LIST 5
14373 #define AR_CMD_HELP 6
14376 ** Other (non-command) switches.
14378 #define AR_SWITCH_VERBOSE 7
14379 #define AR_SWITCH_FILE 8
14380 #define AR_SWITCH_DIRECTORY 9
14381 #define AR_SWITCH_APPEND 10
14382 #define AR_SWITCH_DRYRUN 11
14384 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
14386 case AR_CMD_CREATE:
14387 case AR_CMD_EXTRACT:
14389 case AR_CMD_UPDATE:
14390 case AR_CMD_INSERT:
14393 return arErrorMsg(pAr, "multiple command options");
14395 pAr->eCmd = eSwitch;
14398 case AR_SWITCH_DRYRUN:
14401 case AR_SWITCH_VERBOSE:
14404 case AR_SWITCH_APPEND:
14406 /* Fall thru into --file */
14407 case AR_SWITCH_FILE:
14410 case AR_SWITCH_DIRECTORY:
14419 ** Parse the command line for an ".ar" command. The results are written into
14420 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
14421 ** successfully, otherwise an error message is written to stderr and
14422 ** SQLITE_ERROR returned.
14424 static int arParseCommand(
14425 char **azArg, /* Array of arguments passed to dot command */
14426 int nArg, /* Number of entries in azArg[] */
14427 ArCommand *pAr /* Populate this object */
14435 { "create", 'c', AR_CMD_CREATE, 0 },
14436 { "extract", 'x', AR_CMD_EXTRACT, 0 },
14437 { "insert", 'i', AR_CMD_INSERT, 0 },
14438 { "list", 't', AR_CMD_LIST, 0 },
14439 { "update", 'u', AR_CMD_UPDATE, 0 },
14440 { "help", 'h', AR_CMD_HELP, 0 },
14441 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
14442 { "file", 'f', AR_SWITCH_FILE, 1 },
14443 { "append", 'a', AR_SWITCH_APPEND, 1 },
14444 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
14445 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
14447 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
14448 struct ArSwitch *pEnd = &aSwitch[nSwitch];
14451 utf8_printf(stderr, "Wrong number of arguments. Usage:\n");
14452 return arUsage(stderr);
14454 char *z = azArg[1];
14456 /* Traditional style [tar] invocation */
14459 for(i=0; z[i]; i++){
14460 const char *zArg = 0;
14461 struct ArSwitch *pOpt;
14462 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14463 if( z[i]==pOpt->cShort ) break;
14466 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14470 return arErrorMsg(pAr, "option requires an argument: %c",z[i]);
14472 zArg = azArg[iArg++];
14474 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14476 pAr->nArg = nArg-iArg;
14478 pAr->azArg = &azArg[iArg];
14481 /* Non-traditional invocation */
14483 for(iArg=1; iArg<nArg; iArg++){
14487 /* All remaining command line words are command arguments. */
14488 pAr->azArg = &azArg[iArg];
14489 pAr->nArg = nArg-iArg;
14496 /* One or more short options */
14497 for(i=1; i<n; i++){
14498 const char *zArg = 0;
14499 struct ArSwitch *pOpt;
14500 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14501 if( z[i]==pOpt->cShort ) break;
14504 return arErrorMsg(pAr, "unrecognized option: %c", z[i]);
14511 if( iArg>=(nArg-1) ){
14512 return arErrorMsg(pAr, "option requires an argument: %c",
14515 zArg = azArg[++iArg];
14518 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
14520 }else if( z[2]=='\0' ){
14521 /* A -- option, indicating that all remaining command line words
14522 ** are command arguments. */
14523 pAr->azArg = &azArg[iArg+1];
14524 pAr->nArg = nArg-iArg-1;
14527 /* A long option */
14528 const char *zArg = 0; /* Argument for option, if any */
14529 struct ArSwitch *pMatch = 0; /* Matching option */
14530 struct ArSwitch *pOpt; /* Iterator */
14531 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
14532 const char *zLong = pOpt->zLong;
14533 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
14535 return arErrorMsg(pAr, "ambiguous option: %s",z);
14543 return arErrorMsg(pAr, "unrecognized option: %s", z);
14545 if( pMatch->bArg ){
14546 if( iArg>=(nArg-1) ){
14547 return arErrorMsg(pAr, "option requires an argument: %s", z);
14549 zArg = azArg[++iArg];
14551 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
14561 ** This function assumes that all arguments within the ArCommand.azArg[]
14562 ** array refer to archive members, as for the --extract or --list commands.
14563 ** It checks that each of them are present. If any specified file is not
14564 ** present in the archive, an error is printed to stderr and an error
14565 ** code returned. Otherwise, if all specified arguments are present in
14566 ** the archive, SQLITE_OK is returned.
14568 ** This function strips any trailing '/' characters from each argument.
14569 ** This is consistent with the way the [tar] command seems to work on
14572 static int arCheckEntries(ArCommand *pAr){
14573 int rc = SQLITE_OK;
14576 sqlite3_stmt *pTest = 0;
14578 shellPreparePrintf(pAr->db, &rc, &pTest,
14579 "SELECT name FROM %s WHERE name=$name",
14582 j = sqlite3_bind_parameter_index(pTest, "$name");
14583 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14584 char *z = pAr->azArg[i];
14585 int n = strlen30(z);
14587 while( n>0 && z[n-1]=='/' ) n--;
14589 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
14590 if( SQLITE_ROW==sqlite3_step(pTest) ){
14593 shellReset(&rc, pTest);
14594 if( rc==SQLITE_OK && bOk==0 ){
14595 utf8_printf(stderr, "not found in archive: %s\n", z);
14599 shellFinalize(&rc, pTest);
14605 ** Format a WHERE clause that can be used against the "sqlar" table to
14606 ** identify all archive members that match the command arguments held
14607 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
14608 ** The caller is responsible for eventually calling sqlite3_free() on
14609 ** any non-NULL (*pzWhere) value.
14611 static void arWhereClause(
14614 char **pzWhere /* OUT: New WHERE clause */
14617 if( *pRc==SQLITE_OK ){
14618 if( pAr->nArg==0 ){
14619 zWhere = sqlite3_mprintf("1");
14622 const char *zSep = "";
14623 for(i=0; i<pAr->nArg; i++){
14624 const char *z = pAr->azArg[i];
14625 zWhere = sqlite3_mprintf(
14626 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
14627 zWhere, zSep, z, strlen30(z)+1, z
14630 *pRc = SQLITE_NOMEM;
14641 ** Implementation of .ar "lisT" command.
14643 static int arListCommand(ArCommand *pAr){
14644 const char *zSql = "SELECT %s FROM %s WHERE %s";
14645 const char *azCols[] = {
14647 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
14651 sqlite3_stmt *pSql = 0;
14654 rc = arCheckEntries(pAr);
14655 arWhereClause(&rc, pAr, &zWhere);
14657 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
14658 pAr->zSrcTable, zWhere);
14659 if( pAr->bDryRun ){
14660 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14662 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14663 if( pAr->bVerbose ){
14664 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
14665 sqlite3_column_text(pSql, 0),
14666 sqlite3_column_int(pSql, 1),
14667 sqlite3_column_text(pSql, 2),
14668 sqlite3_column_text(pSql, 3)
14671 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14675 shellFinalize(&rc, pSql);
14676 sqlite3_free(zWhere);
14682 ** Implementation of .ar "eXtract" command.
14684 static int arExtractCommand(ArCommand *pAr){
14685 const char *zSql1 =
14688 " writefile(($dir || name), %s, mode, mtime) "
14689 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)"
14690 " AND name NOT GLOB '*..[/\\]*'";
14692 const char *azExtraArg[] = {
14693 "sqlar_uncompress(data, sz)",
14697 sqlite3_stmt *pSql = 0;
14698 int rc = SQLITE_OK;
14703 /* If arguments are specified, check that they actually exist within
14704 ** the archive before proceeding. And formulate a WHERE clause to
14706 rc = arCheckEntries(pAr);
14707 arWhereClause(&rc, pAr, &zWhere);
14709 if( rc==SQLITE_OK ){
14711 zDir = sqlite3_mprintf("%s/", pAr->zDir);
14713 zDir = sqlite3_mprintf("");
14715 if( zDir==0 ) rc = SQLITE_NOMEM;
14718 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
14719 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
14722 if( rc==SQLITE_OK ){
14723 j = sqlite3_bind_parameter_index(pSql, "$dir");
14724 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
14726 /* Run the SELECT statement twice. The first time, writefile() is called
14727 ** for all archive members that should be extracted. The second time,
14728 ** only for the directories. This is because the timestamps for
14729 ** extracted directories must be reset after they are populated (as
14730 ** populating them changes the timestamp). */
14731 for(i=0; i<2; i++){
14732 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
14733 sqlite3_bind_int(pSql, j, i);
14734 if( pAr->bDryRun ){
14735 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
14737 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
14738 if( i==0 && pAr->bVerbose ){
14739 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
14743 shellReset(&rc, pSql);
14745 shellFinalize(&rc, pSql);
14748 sqlite3_free(zDir);
14749 sqlite3_free(zWhere);
14754 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
14756 static int arExecSql(ArCommand *pAr, const char *zSql){
14758 if( pAr->bDryRun ){
14759 utf8_printf(pAr->p->out, "%s\n", zSql);
14763 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
14765 utf8_printf(stdout, "ERROR: %s\n", zErr);
14766 sqlite3_free(zErr);
14774 ** Implementation of .ar "create", "insert", and "update" commands.
14776 ** create -> Create a new SQL archive
14777 ** insert -> Insert or reinsert all files listed
14778 ** update -> Insert files that have changed or that were not
14779 ** previously in the archive
14781 ** Create the "sqlar" table in the database if it does not already exist.
14782 ** Then add each file in the azFile[] array to the archive. Directories
14783 ** are added recursively. If argument bVerbose is non-zero, a message is
14784 ** printed on stdout for each file archived.
14786 ** The create command is the same as update, except that it drops
14787 ** any existing "sqlar" table before beginning. The "insert" command
14788 ** always overwrites every file named on the command-line, where as
14789 ** "update" only overwrites if the size or mtime or mode has changed.
14791 static int arCreateOrUpdateCommand(
14792 ArCommand *pAr, /* Command arguments and options */
14793 int bUpdate, /* true for a --create. */
14794 int bOnlyIfChanged /* Only update if file has changed */
14796 const char *zCreate =
14797 "CREATE TABLE IF NOT EXISTS sqlar(\n"
14798 " name TEXT PRIMARY KEY, -- name of the file\n"
14799 " mode INT, -- access permissions\n"
14800 " mtime INT, -- last modification time\n"
14801 " sz INT, -- original file size\n"
14802 " data BLOB -- compressed content\n"
14804 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
14805 const char *zInsertFmt[2] = {
14806 "REPLACE INTO %s(name,mode,mtime,sz,data)\n"
14811 " CASE substr(lsmode(mode),1,1)\n"
14812 " WHEN '-' THEN length(data)\n"
14813 " WHEN 'd' THEN 0\n"
14815 " sqlar_compress(data)\n"
14816 " FROM fsdir(%Q,%Q) AS disk\n"
14817 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14819 "REPLACE INTO %s(name,mode,mtime,data)\n"
14825 " FROM fsdir(%Q,%Q) AS disk\n"
14826 " WHERE lsmode(mode) NOT LIKE '?%%'%s;"
14828 int i; /* For iterating through azFile[] */
14829 int rc; /* Return code */
14830 const char *zTab = 0; /* SQL table into which to insert */
14835 arExecSql(pAr, "PRAGMA page_size=512");
14836 rc = arExecSql(pAr, "SAVEPOINT ar;");
14837 if( rc!=SQLITE_OK ) return rc;
14840 /* Initialize the zipfile virtual table, if necessary */
14843 sqlite3_randomness(sizeof(r),&r);
14844 sqlite3_snprintf(sizeof(zTemp),zTemp,"zip%016llx",r);
14846 zSql = sqlite3_mprintf(
14847 "CREATE VIRTUAL TABLE temp.%s USING zipfile(%Q)",
14850 rc = arExecSql(pAr, zSql);
14851 sqlite3_free(zSql);
14856 /* Initialize the table for an SQLAR */
14859 rc = arExecSql(pAr, zDrop);
14860 if( rc!=SQLITE_OK ) goto end_ar_transaction;
14862 rc = arExecSql(pAr, zCreate);
14864 if( bOnlyIfChanged ){
14865 zExists = sqlite3_mprintf(
14867 "SELECT 1 FROM %s AS mem"
14868 " WHERE mem.name=disk.name"
14869 " AND mem.mtime=disk.mtime"
14870 " AND mem.mode=disk.mode)", zTab);
14872 zExists = sqlite3_mprintf("");
14874 if( zExists==0 ) rc = SQLITE_NOMEM;
14875 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
14876 char *zSql2 = sqlite3_mprintf(zInsertFmt[pAr->bZip], zTab,
14877 pAr->bVerbose ? "shell_putsnl(name)" : "name",
14878 pAr->azArg[i], pAr->zDir, zExists);
14879 rc = arExecSql(pAr, zSql2);
14880 sqlite3_free(zSql2);
14882 end_ar_transaction:
14883 if( rc!=SQLITE_OK ){
14884 sqlite3_exec(pAr->db, "ROLLBACK TO ar; RELEASE ar;", 0, 0, 0);
14886 rc = arExecSql(pAr, "RELEASE ar;");
14887 if( pAr->bZip && pAr->zFile ){
14888 zSql = sqlite3_mprintf("DROP TABLE %s", zTemp);
14889 arExecSql(pAr, zSql);
14890 sqlite3_free(zSql);
14893 sqlite3_free(zExists);
14898 ** Implementation of ".ar" dot command.
14900 static int arDotCommand(
14901 ShellState *pState, /* Current shell tool state */
14902 int fromCmdLine, /* True if -A command-line option, not .ar cmd */
14903 char **azArg, /* Array of arguments passed to dot command */
14904 int nArg /* Number of entries in azArg[] */
14908 memset(&cmd, 0, sizeof(cmd));
14909 cmd.fromCmdLine = fromCmdLine;
14910 rc = arParseCommand(azArg, nArg, &cmd);
14911 if( rc==SQLITE_OK ){
14912 int eDbType = SHELL_OPEN_UNSPEC;
14914 cmd.db = pState->db;
14916 eDbType = deduceDatabaseType(cmd.zFile, 1);
14918 eDbType = pState->openMode;
14920 if( eDbType==SHELL_OPEN_ZIPFILE ){
14921 if( cmd.eCmd==AR_CMD_EXTRACT || cmd.eCmd==AR_CMD_LIST ){
14922 if( cmd.zFile==0 ){
14923 cmd.zSrcTable = sqlite3_mprintf("zip");
14925 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
14929 }else if( cmd.zFile ){
14931 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
14932 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_INSERT
14933 || cmd.eCmd==AR_CMD_UPDATE ){
14934 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
14936 flags = SQLITE_OPEN_READONLY;
14940 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
14941 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
14943 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
14944 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
14945 if( rc!=SQLITE_OK ){
14946 utf8_printf(stderr, "cannot open file: %s (%s)\n",
14947 cmd.zFile, sqlite3_errmsg(cmd.db)
14949 goto end_ar_command;
14951 sqlite3_fileio_init(cmd.db, 0, 0);
14952 sqlite3_sqlar_init(cmd.db, 0, 0);
14953 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
14954 shellPutsFunc, 0, 0);
14957 if( cmd.zSrcTable==0 && cmd.bZip==0 && cmd.eCmd!=AR_CMD_HELP ){
14958 if( cmd.eCmd!=AR_CMD_CREATE
14959 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
14961 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
14963 goto end_ar_command;
14965 cmd.zSrcTable = sqlite3_mprintf("sqlar");
14968 switch( cmd.eCmd ){
14969 case AR_CMD_CREATE:
14970 rc = arCreateOrUpdateCommand(&cmd, 0, 0);
14973 case AR_CMD_EXTRACT:
14974 rc = arExtractCommand(&cmd);
14978 rc = arListCommand(&cmd);
14982 arUsage(pState->out);
14985 case AR_CMD_INSERT:
14986 rc = arCreateOrUpdateCommand(&cmd, 1, 0);
14990 assert( cmd.eCmd==AR_CMD_UPDATE );
14991 rc = arCreateOrUpdateCommand(&cmd, 1, 1);
14996 if( cmd.db!=pState->db ){
14999 sqlite3_free(cmd.zSrcTable);
15003 /* End of the ".archive" or ".ar" command logic
15004 *******************************************************************************/
15005 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
15007 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15009 ** If (*pRc) is not SQLITE_OK when this function is called, it is a no-op.
15010 ** Otherwise, the SQL statement or statements in zSql are executed using
15011 ** database connection db and the error code written to *pRc before
15012 ** this function returns.
15014 static void shellExec(sqlite3 *db, int *pRc, const char *zSql){
15016 if( rc==SQLITE_OK ){
15018 rc = sqlite3_exec(db, zSql, 0, 0, &zErr);
15019 if( rc!=SQLITE_OK ){
15020 raw_printf(stderr, "SQL error: %s\n", zErr);
15027 ** Like shellExec(), except that zFmt is a printf() style format string.
15029 static void shellExecPrintf(sqlite3 *db, int *pRc, const char *zFmt, ...){
15031 if( *pRc==SQLITE_OK ){
15033 va_start(ap, zFmt);
15034 z = sqlite3_vmprintf(zFmt, ap);
15037 *pRc = SQLITE_NOMEM;
15039 shellExec(db, pRc, z);
15046 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
15047 ** Otherwise, an attempt is made to allocate, zero and return a pointer
15048 ** to a buffer nByte bytes in size. If an OOM error occurs, *pRc is set
15049 ** to SQLITE_NOMEM and NULL returned.
15051 static void *shellMalloc(int *pRc, sqlite3_int64 nByte){
15053 if( *pRc==SQLITE_OK ){
15054 pRet = sqlite3_malloc64(nByte);
15056 *pRc = SQLITE_NOMEM;
15058 memset(pRet, 0, nByte);
15065 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
15066 ** Otherwise, zFmt is treated as a printf() style string. The result of
15067 ** formatting it along with any trailing arguments is written into a
15068 ** buffer obtained from sqlite3_malloc(), and pointer to which is returned.
15069 ** It is the responsibility of the caller to eventually free this buffer
15070 ** using a call to sqlite3_free().
15072 ** If an OOM error occurs, (*pRc) is set to SQLITE_NOMEM and a NULL
15073 ** pointer returned.
15075 static char *shellMPrintf(int *pRc, const char *zFmt, ...){
15077 if( *pRc==SQLITE_OK ){
15079 va_start(ap, zFmt);
15080 z = sqlite3_vmprintf(zFmt, ap);
15083 *pRc = SQLITE_NOMEM;
15090 ** When running the ".recover" command, each output table, and the special
15091 ** orphaned row table if it is required, is represented by an instance
15092 ** of the following struct.
15094 typedef struct RecoverTable RecoverTable;
15095 struct RecoverTable {
15096 char *zQuoted; /* Quoted version of table name */
15097 int nCol; /* Number of columns in table */
15098 char **azlCol; /* Array of column lists */
15099 int iPk; /* Index of IPK column */
15103 ** Free a RecoverTable object allocated by recoverFindTable() or
15104 ** recoverOrphanTable().
15106 static void recoverFreeTable(RecoverTable *pTab){
15108 sqlite3_free(pTab->zQuoted);
15109 if( pTab->azlCol ){
15111 for(i=0; i<=pTab->nCol; i++){
15112 sqlite3_free(pTab->azlCol[i]);
15114 sqlite3_free(pTab->azlCol);
15116 sqlite3_free(pTab);
15121 ** This function is a no-op if (*pRc) is not SQLITE_OK when it is called.
15122 ** Otherwise, it allocates and returns a RecoverTable object based on the
15123 ** final four arguments passed to this function. It is the responsibility
15124 ** of the caller to eventually free the returned object using
15125 ** recoverFreeTable().
15127 static RecoverTable *recoverNewTable(
15128 int *pRc, /* IN/OUT: Error code */
15129 const char *zName, /* Name of table */
15130 const char *zSql, /* CREATE TABLE statement */
15134 sqlite3 *dbtmp = 0; /* sqlite3 handle for testing CREATE TABLE */
15136 RecoverTable *pTab = 0;
15138 pTab = (RecoverTable*)shellMalloc(&rc, sizeof(RecoverTable));
15139 if( rc==SQLITE_OK ){
15141 int bSqlIntkey = 0;
15142 sqlite3_stmt *pStmt = 0;
15144 rc = sqlite3_open("", &dbtmp);
15145 if( rc==SQLITE_OK ){
15146 sqlite3_create_function(dbtmp, "shell_idquote", 1, SQLITE_UTF8, 0,
15147 shellIdQuote, 0, 0);
15149 if( rc==SQLITE_OK ){
15150 rc = sqlite3_exec(dbtmp, "PRAGMA writable_schema = on", 0, 0, 0);
15152 if( rc==SQLITE_OK ){
15153 rc = sqlite3_exec(dbtmp, zSql, 0, 0, 0);
15154 if( rc==SQLITE_ERROR ){
15159 shellPreparePrintf(dbtmp, &rc, &pStmt,
15160 "SELECT count(*) FROM pragma_table_info(%Q)", zName
15162 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15163 nSqlCol = sqlite3_column_int(pStmt, 0);
15165 shellFinalize(&rc, pStmt);
15167 if( rc!=SQLITE_OK || nSqlCol<nCol ){
15171 shellPreparePrintf(dbtmp, &rc, &pStmt,
15173 " SELECT substr(data,1,1)==X'0D' FROM sqlite_dbpage WHERE pgno=rootpage"
15174 ") FROM sqlite_master WHERE name = %Q", zName
15176 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15177 bSqlIntkey = sqlite3_column_int(pStmt, 0);
15179 shellFinalize(&rc, pStmt);
15181 if( bIntkey==bSqlIntkey ){
15183 const char *zPk = "_rowid_";
15184 sqlite3_stmt *pPkFinder = 0;
15186 /* If this is an intkey table and there is an INTEGER PRIMARY KEY,
15187 ** set zPk to the name of the PK column, and pTab->iPk to the index
15188 ** of the column, where columns are 0-numbered from left to right.
15189 ** Or, if this is a WITHOUT ROWID table or if there is no IPK column,
15190 ** leave zPk as "_rowid_" and pTab->iPk at -2. */
15193 shellPreparePrintf(dbtmp, &rc, &pPkFinder,
15194 "SELECT cid, name FROM pragma_table_info(%Q) "
15195 " WHERE pk=1 AND type='integer' COLLATE nocase"
15196 " AND NOT EXISTS (SELECT cid FROM pragma_table_info(%Q) WHERE pk=2)"
15199 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPkFinder) ){
15200 pTab->iPk = sqlite3_column_int(pPkFinder, 0);
15201 zPk = (const char*)sqlite3_column_text(pPkFinder, 1);
15205 pTab->zQuoted = shellMPrintf(&rc, "\"%w\"", zName);
15206 pTab->azlCol = (char**)shellMalloc(&rc, sizeof(char*) * (nSqlCol+1));
15207 pTab->nCol = nSqlCol;
15210 pTab->azlCol[0] = shellMPrintf(&rc, "\"%w\"", zPk);
15212 pTab->azlCol[0] = shellMPrintf(&rc, "");
15215 shellPreparePrintf(dbtmp, &rc, &pStmt,
15216 "SELECT %Q || group_concat(shell_idquote(name), ', ') "
15217 " FILTER (WHERE cid!=%d) OVER (ORDER BY %s cid) "
15218 "FROM pragma_table_info(%Q)",
15219 bIntkey ? ", " : "", pTab->iPk,
15220 bIntkey ? "" : "(CASE WHEN pk=0 THEN 1000000 ELSE pk END), ",
15223 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15224 const char *zText = (const char*)sqlite3_column_text(pStmt, 0);
15225 pTab->azlCol[i] = shellMPrintf(&rc, "%s%s", pTab->azlCol[0], zText);
15228 shellFinalize(&rc, pStmt);
15230 shellFinalize(&rc, pPkFinder);
15235 sqlite3_close(dbtmp);
15237 if( rc!=SQLITE_OK || (pTab && pTab->zQuoted==0) ){
15238 recoverFreeTable(pTab);
15245 ** This function is called to search the schema recovered from the
15246 ** sqlite_master table of the (possibly) corrupt database as part
15247 ** of a ".recover" command. Specifically, for a table with root page
15248 ** iRoot and at least nCol columns. Additionally, if bIntkey is 0, the
15249 ** table must be a WITHOUT ROWID table, or if non-zero, not one of
15252 ** If a table is found, a (RecoverTable*) object is returned. Or, if
15253 ** no such table is found, but bIntkey is false and iRoot is the
15254 ** root page of an index in the recovered schema, then (*pbNoop) is
15255 ** set to true and NULL returned. Or, if there is no such table or
15256 ** index, NULL is returned and (*pbNoop) set to 0, indicating that
15257 ** the caller should write data to the orphans table.
15259 static RecoverTable *recoverFindTable(
15260 ShellState *pState, /* Shell state object */
15261 int *pRc, /* IN/OUT: Error code */
15262 int iRoot, /* Root page of table */
15263 int bIntkey, /* True for an intkey table */
15264 int nCol, /* Number of columns in table */
15265 int *pbNoop /* OUT: True if iRoot is root of index */
15267 sqlite3_stmt *pStmt = 0;
15268 RecoverTable *pRet = 0;
15270 const char *zSql = 0;
15271 const char *zName = 0;
15273 /* Search the recovered schema for an object with root page iRoot. */
15274 shellPreparePrintf(pState->db, pRc, &pStmt,
15275 "SELECT type, name, sql FROM recovery.schema WHERE rootpage=%d", iRoot
15277 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15278 const char *zType = (const char*)sqlite3_column_text(pStmt, 0);
15279 if( bIntkey==0 && sqlite3_stricmp(zType, "index")==0 ){
15283 if( sqlite3_stricmp(zType, "table")==0 ){
15284 zName = (const char*)sqlite3_column_text(pStmt, 1);
15285 zSql = (const char*)sqlite3_column_text(pStmt, 2);
15286 pRet = recoverNewTable(pRc, zName, zSql, bIntkey, nCol);
15291 shellFinalize(pRc, pStmt);
15297 ** Return a RecoverTable object representing the orphans table.
15299 static RecoverTable *recoverOrphanTable(
15300 ShellState *pState, /* Shell state object */
15301 int *pRc, /* IN/OUT: Error code */
15302 const char *zLostAndFound, /* Base name for orphans table */
15303 int nCol /* Number of user data columns */
15305 RecoverTable *pTab = 0;
15306 if( nCol>=0 && *pRc==SQLITE_OK ){
15309 /* This block determines the name of the orphan table. The prefered
15310 ** name is zLostAndFound. But if that clashes with another name
15311 ** in the recovered schema, try zLostAndFound_0, zLostAndFound_1
15312 ** and so on until a non-clashing name is found. */
15314 char *zTab = shellMPrintf(pRc, "%s", zLostAndFound);
15315 sqlite3_stmt *pTest = 0;
15316 shellPrepare(pState->db, pRc,
15317 "SELECT 1 FROM recovery.schema WHERE name=?", &pTest
15319 if( pTest ) sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15320 while( *pRc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pTest) ){
15321 shellReset(pRc, pTest);
15322 sqlite3_free(zTab);
15323 zTab = shellMPrintf(pRc, "%s_%d", zLostAndFound, iTab++);
15324 sqlite3_bind_text(pTest, 1, zTab, -1, SQLITE_TRANSIENT);
15326 shellFinalize(pRc, pTest);
15328 pTab = (RecoverTable*)shellMalloc(pRc, sizeof(RecoverTable));
15330 pTab->zQuoted = shellMPrintf(pRc, "\"%w\"", zTab);
15334 pTab->azlCol = (char**)shellMalloc(pRc, sizeof(char*) * (nCol+1));
15335 if( pTab->azlCol ){
15336 pTab->azlCol[nCol] = shellMPrintf(pRc, "");
15337 for(i=nCol-1; i>=0; i--){
15338 pTab->azlCol[i] = shellMPrintf(pRc, "%s, NULL", pTab->azlCol[i+1]);
15343 if( *pRc!=SQLITE_OK ){
15344 recoverFreeTable(pTab);
15347 raw_printf(pState->out,
15348 "CREATE TABLE %s(rootpgno INTEGER, "
15349 "pgno INTEGER, nfield INTEGER, id INTEGER", pTab->zQuoted
15351 for(i=0; i<nCol; i++){
15352 raw_printf(pState->out, ", c%d", i);
15354 raw_printf(pState->out, ");\n");
15357 sqlite3_free(zTab);
15363 ** This function is called to recover data from the database. A script
15364 ** to construct a new database containing all recovered data is output
15365 ** on stream pState->out.
15367 static int recoverDatabaseCmd(ShellState *pState, int nArg, char **azArg){
15368 int rc = SQLITE_OK;
15369 sqlite3_stmt *pLoop = 0; /* Loop through all root pages */
15370 sqlite3_stmt *pPages = 0; /* Loop through all pages in a group */
15371 sqlite3_stmt *pCells = 0; /* Loop through all cells in a page */
15372 const char *zRecoveryDb = ""; /* Name of "recovery" database */
15373 const char *zLostAndFound = "lost_and_found";
15376 RecoverTable *pOrphan = 0;
15378 int bFreelist = 1; /* 0 if --freelist-corrupt is specified */
15379 int bRowids = 1; /* 0 if --no-rowids */
15380 for(i=1; i<nArg; i++){
15381 char *z = azArg[i];
15383 if( z[0]=='-' && z[1]=='-' ) z++;
15385 if( n<=17 && memcmp("-freelist-corrupt", z, n)==0 ){
15388 if( n<=12 && memcmp("-recovery-db", z, n)==0 && i<(nArg-1) ){
15390 zRecoveryDb = azArg[i];
15392 if( n<=15 && memcmp("-lost-and-found", z, n)==0 && i<(nArg-1) ){
15394 zLostAndFound = azArg[i];
15396 if( n<=10 && memcmp("-no-rowids", z, n)==0 ){
15400 utf8_printf(stderr, "unexpected option: %s\n", azArg[i]);
15401 showHelp(pState->out, azArg[0]);
15406 shellExecPrintf(pState->db, &rc,
15407 /* Attach an in-memory database named 'recovery'. Create an indexed
15408 ** cache of the sqlite_dbptr virtual table. */
15409 "PRAGMA writable_schema = on;"
15410 "ATTACH %Q AS recovery;"
15411 "DROP TABLE IF EXISTS recovery.dbptr;"
15412 "DROP TABLE IF EXISTS recovery.freelist;"
15413 "DROP TABLE IF EXISTS recovery.map;"
15414 "DROP TABLE IF EXISTS recovery.schema;"
15415 "CREATE TABLE recovery.freelist(pgno INTEGER PRIMARY KEY);", zRecoveryDb
15419 shellExec(pState->db, &rc,
15420 "WITH trunk(pgno) AS ("
15421 " SELECT shell_int32("
15422 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 8) AS x "
15425 " SELECT shell_int32("
15426 " (SELECT data FROM sqlite_dbpage WHERE pgno=trunk.pgno), 0) AS x "
15427 " FROM trunk WHERE x>0"
15429 "freelist(data, n, freepgno) AS ("
15430 " SELECT data, min(16384, shell_int32(data, 1)-1), t.pgno "
15431 " FROM trunk t, sqlite_dbpage s WHERE s.pgno=t.pgno"
15433 " SELECT data, n-1, shell_int32(data, 2+n) "
15434 " FROM freelist WHERE n>=0"
15436 "REPLACE INTO recovery.freelist SELECT freepgno FROM freelist;"
15440 /* If this is an auto-vacuum database, add all pointer-map pages to
15441 ** the freelist table. Do this regardless of whether or not
15442 ** --freelist-corrupt was specified. */
15443 shellExec(pState->db, &rc,
15444 "WITH ptrmap(pgno) AS ("
15445 " SELECT 2 WHERE shell_int32("
15446 " (SELECT data FROM sqlite_dbpage WHERE pgno=1), 13"
15449 " SELECT pgno+1+(SELECT page_size FROM pragma_page_size)/5 AS pp "
15450 " FROM ptrmap WHERE pp<=(SELECT page_count FROM pragma_page_count)"
15452 "REPLACE INTO recovery.freelist SELECT pgno FROM ptrmap"
15455 shellExec(pState->db, &rc,
15456 "CREATE TABLE recovery.dbptr("
15457 " pgno, child, PRIMARY KEY(child, pgno)"
15459 "INSERT OR IGNORE INTO recovery.dbptr(pgno, child) "
15460 " SELECT * FROM sqlite_dbptr"
15461 " WHERE pgno NOT IN freelist AND child NOT IN freelist;"
15463 /* Delete any pointer to page 1. This ensures that page 1 is considered
15464 ** a root page, regardless of how corrupt the db is. */
15465 "DELETE FROM recovery.dbptr WHERE child = 1;"
15467 /* Delete all pointers to any pages that have more than one pointer
15468 ** to them. Such pages will be treated as root pages when recovering
15470 "DELETE FROM recovery.dbptr WHERE child IN ("
15471 " SELECT child FROM recovery.dbptr GROUP BY child HAVING count(*)>1"
15474 /* Create the "map" table that will (eventually) contain instructions
15475 ** for dealing with each page in the db that contains one or more
15477 "CREATE TABLE recovery.map("
15478 "pgno INTEGER PRIMARY KEY, maxlen INT, intkey, root INT"
15481 /* Populate table [map]. If there are circular loops of pages in the
15482 ** database, the following adds all pages in such a loop to the map
15483 ** as individual root pages. This could be handled better. */
15484 "WITH pages(i, maxlen) AS ("
15485 " SELECT page_count, ("
15486 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=page_count"
15487 " ) FROM pragma_page_count WHERE page_count>0"
15490 " SELECT max(field+1) FROM sqlite_dbdata WHERE pgno=i-1"
15491 " ) FROM pages WHERE i>=2"
15493 "INSERT INTO recovery.map(pgno, maxlen, intkey, root) "
15494 " SELECT i, maxlen, NULL, ("
15495 " WITH p(orig, pgno, parent) AS ("
15496 " SELECT 0, i, (SELECT pgno FROM recovery.dbptr WHERE child=i)"
15498 " SELECT i, p.parent, "
15499 " (SELECT pgno FROM recovery.dbptr WHERE child=p.parent) FROM p"
15501 " SELECT pgno FROM p WHERE (parent IS NULL OR pgno = orig)"
15503 "FROM pages WHERE maxlen IS NOT NULL AND i NOT IN freelist;"
15504 "UPDATE recovery.map AS o SET intkey = ("
15505 " SELECT substr(data, 1, 1)==X'0D' FROM sqlite_dbpage WHERE pgno=o.pgno"
15508 /* Extract data from page 1 and any linked pages into table
15509 ** recovery.schema. With the same schema as an sqlite_master table. */
15510 "CREATE TABLE recovery.schema(type, name, tbl_name, rootpage, sql);"
15511 "INSERT INTO recovery.schema SELECT "
15512 " max(CASE WHEN field=0 THEN value ELSE NULL END),"
15513 " max(CASE WHEN field=1 THEN value ELSE NULL END),"
15514 " max(CASE WHEN field=2 THEN value ELSE NULL END),"
15515 " max(CASE WHEN field=3 THEN value ELSE NULL END),"
15516 " max(CASE WHEN field=4 THEN value ELSE NULL END)"
15517 "FROM sqlite_dbdata WHERE pgno IN ("
15518 " SELECT pgno FROM recovery.map WHERE root=1"
15520 "GROUP BY pgno, cell;"
15521 "CREATE INDEX recovery.schema_rootpage ON schema(rootpage);"
15524 /* Open a transaction, then print out all non-virtual, non-"sqlite_%"
15525 ** CREATE TABLE statements that extracted from the existing schema. */
15526 if( rc==SQLITE_OK ){
15527 sqlite3_stmt *pStmt = 0;
15528 /* ".recover" might output content in an order which causes immediate
15529 ** foreign key constraints to be violated. So disable foreign-key
15530 ** constraint enforcement to prevent problems when running the output
15532 raw_printf(pState->out, "PRAGMA foreign_keys=OFF;\n");
15533 raw_printf(pState->out, "BEGIN;\n");
15534 raw_printf(pState->out, "PRAGMA writable_schema = on;\n");
15535 shellPrepare(pState->db, &rc,
15536 "SELECT sql FROM recovery.schema "
15537 "WHERE type='table' AND sql LIKE 'create table%'", &pStmt
15539 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15540 const char *zCreateTable = (const char*)sqlite3_column_text(pStmt, 0);
15541 raw_printf(pState->out, "CREATE TABLE IF NOT EXISTS %s;\n",
15545 shellFinalize(&rc, pStmt);
15548 /* Figure out if an orphan table will be required. And if so, how many
15549 ** user columns it should contain */
15550 shellPrepare(pState->db, &rc,
15551 "SELECT coalesce(max(maxlen), -2) FROM recovery.map WHERE root>1"
15554 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15555 nOrphan = sqlite3_column_int(pLoop, 0);
15557 shellFinalize(&rc, pLoop);
15560 shellPrepare(pState->db, &rc,
15561 "SELECT pgno FROM recovery.map WHERE root=?", &pPages
15564 shellPrepare(pState->db, &rc,
15565 "SELECT max(field), group_concat(shell_escape_crnl(quote"
15566 "(case when (? AND field<0) then NULL else value end)"
15569 "FROM sqlite_dbdata WHERE pgno = ? AND field != ?"
15570 "GROUP BY cell", &pCells
15573 /* Loop through each root page. */
15574 shellPrepare(pState->db, &rc,
15575 "SELECT root, intkey, max(maxlen) FROM recovery.map"
15576 " WHERE root>1 GROUP BY root, intkey ORDER BY root=("
15577 " SELECT rootpage FROM recovery.schema WHERE name='sqlite_sequence'"
15580 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pLoop) ){
15581 int iRoot = sqlite3_column_int(pLoop, 0);
15582 int bIntkey = sqlite3_column_int(pLoop, 1);
15583 int nCol = sqlite3_column_int(pLoop, 2);
15585 RecoverTable *pTab;
15587 assert( bIntkey==0 || bIntkey==1 );
15588 pTab = recoverFindTable(pState, &rc, iRoot, bIntkey, nCol, &bNoop);
15589 if( bNoop || rc ) continue;
15592 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15595 if( pTab==0 ) break;
15598 if( 0==sqlite3_stricmp(pTab->zQuoted, "\"sqlite_sequence\"") ){
15599 raw_printf(pState->out, "DELETE FROM sqlite_sequence;\n");
15601 sqlite3_bind_int(pPages, 1, iRoot);
15602 if( bRowids==0 && pTab->iPk<0 ){
15603 sqlite3_bind_int(pCells, 1, 1);
15605 sqlite3_bind_int(pCells, 1, 0);
15607 sqlite3_bind_int(pCells, 3, pTab->iPk);
15609 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pPages) ){
15610 int iPgno = sqlite3_column_int(pPages, 0);
15611 sqlite3_bind_int(pCells, 2, iPgno);
15612 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pCells) ){
15613 int nField = sqlite3_column_int(pCells, 0);
15614 int iMin = sqlite3_column_int(pCells, 2);
15615 const char *zVal = (const char*)sqlite3_column_text(pCells, 1);
15617 RecoverTable *pTab2 = pTab;
15618 if( pTab!=pOrphan && (iMin<0)!=bIntkey ){
15620 pOrphan = recoverOrphanTable(pState, &rc, zLostAndFound, nOrphan);
15623 if( pTab2==0 ) break;
15627 if( pTab2==pOrphan ){
15628 raw_printf(pState->out,
15629 "INSERT INTO %s VALUES(%d, %d, %d, %s%s%s);\n",
15630 pTab2->zQuoted, iRoot, iPgno, nField,
15631 iMin<0 ? "" : "NULL, ", zVal, pTab2->azlCol[nField]
15634 raw_printf(pState->out, "INSERT INTO %s(%s) VALUES( %s );\n",
15635 pTab2->zQuoted, pTab2->azlCol[nField], zVal
15639 shellReset(&rc, pCells);
15641 shellReset(&rc, pPages);
15642 if( pTab!=pOrphan ) recoverFreeTable(pTab);
15644 shellFinalize(&rc, pLoop);
15645 shellFinalize(&rc, pPages);
15646 shellFinalize(&rc, pCells);
15647 recoverFreeTable(pOrphan);
15649 /* The rest of the schema */
15650 if( rc==SQLITE_OK ){
15651 sqlite3_stmt *pStmt = 0;
15652 shellPrepare(pState->db, &rc,
15653 "SELECT sql, name FROM recovery.schema "
15654 "WHERE sql NOT LIKE 'create table%'", &pStmt
15656 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
15657 const char *zSql = (const char*)sqlite3_column_text(pStmt, 0);
15658 if( sqlite3_strnicmp(zSql, "create virt", 11)==0 ){
15659 const char *zName = (const char*)sqlite3_column_text(pStmt, 1);
15660 char *zPrint = shellMPrintf(&rc,
15661 "INSERT INTO sqlite_master VALUES('table', %Q, %Q, 0, %Q)",
15664 raw_printf(pState->out, "%s;\n", zPrint);
15665 sqlite3_free(zPrint);
15667 raw_printf(pState->out, "%s;\n", zSql);
15670 shellFinalize(&rc, pStmt);
15673 if( rc==SQLITE_OK ){
15674 raw_printf(pState->out, "PRAGMA writable_schema = off;\n");
15675 raw_printf(pState->out, "COMMIT;\n");
15677 sqlite3_exec(pState->db, "DETACH recovery", 0, 0, 0);
15680 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15684 ** If an input line begins with "." then invoke this routine to
15685 ** process that line.
15687 ** Return 1 on error, 2 to exit, and 0 otherwise.
15689 static int do_meta_command(char *zLine, ShellState *p){
15696 #ifndef SQLITE_OMIT_VIRTUALTABLE
15697 if( p->expert.pExpert ){
15698 expertFinish(p, 1, 0);
15702 /* Parse the input line into tokens.
15704 while( zLine[h] && nArg<ArraySize(azArg)-1 ){
15705 while( IsSpace(zLine[h]) ){ h++; }
15706 if( zLine[h]==0 ) break;
15707 if( zLine[h]=='\'' || zLine[h]=='"' ){
15708 int delim = zLine[h++];
15709 azArg[nArg++] = &zLine[h];
15710 while( zLine[h] && zLine[h]!=delim ){
15711 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
15714 if( zLine[h]==delim ){
15717 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
15719 azArg[nArg++] = &zLine[h];
15720 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
15721 if( zLine[h] ) zLine[h++] = 0;
15722 resolve_backslashes(azArg[nArg-1]);
15727 /* Process the input line.
15729 if( nArg==0 ) return 0; /* no tokens, no error */
15730 n = strlen30(azArg[0]);
15734 #ifndef SQLITE_OMIT_AUTHORIZATION
15735 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
15737 raw_printf(stderr, "Usage: .auth ON|OFF\n");
15739 goto meta_command_exit;
15742 if( booleanValue(azArg[1]) ){
15743 sqlite3_set_authorizer(p->db, shellAuth, p);
15745 sqlite3_set_authorizer(p->db, 0, 0);
15750 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
15751 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
15753 rc = arDotCommand(p, 0, azArg, nArg);
15757 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
15758 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
15760 const char *zDestFile = 0;
15761 const char *zDb = 0;
15763 sqlite3_backup *pBackup;
15766 const char *zVfs = 0;
15767 for(j=1; j<nArg; j++){
15768 const char *z = azArg[j];
15770 if( z[1]=='-' ) z++;
15771 if( strcmp(z, "-append")==0 ){
15774 if( strcmp(z, "-async")==0 ){
15778 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
15781 }else if( zDestFile==0 ){
15782 zDestFile = azArg[j];
15783 }else if( zDb==0 ){
15785 zDestFile = azArg[j];
15787 raw_printf(stderr, "Usage: .backup ?DB? ?OPTIONS? FILENAME\n");
15791 if( zDestFile==0 ){
15792 raw_printf(stderr, "missing FILENAME argument on .backup\n");
15795 if( zDb==0 ) zDb = "main";
15796 rc = sqlite3_open_v2(zDestFile, &pDest,
15797 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, zVfs);
15798 if( rc!=SQLITE_OK ){
15799 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
15804 sqlite3_exec(pDest, "PRAGMA synchronous=OFF; PRAGMA journal_mode=OFF;",
15808 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
15810 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15814 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
15815 sqlite3_backup_finish(pBackup);
15816 if( rc==SQLITE_DONE ){
15819 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
15825 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
15827 bail_on_error = booleanValue(azArg[1]);
15829 raw_printf(stderr, "Usage: .bail on|off\n");
15834 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
15836 if( booleanValue(azArg[1]) ){
15837 setBinaryMode(p->out, 1);
15839 setTextMode(p->out, 1);
15842 raw_printf(stderr, "Usage: .binary on|off\n");
15847 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
15849 #if defined(_WIN32) || defined(WIN32)
15850 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
15851 rc = !SetCurrentDirectoryW(z);
15854 rc = chdir(azArg[1]);
15857 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
15861 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
15866 /* The undocumented ".breakpoint" command causes a call to the no-op
15867 ** routine named test_breakpoint().
15869 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
15873 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
15875 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
15877 raw_printf(stderr, "Usage: .changes on|off\n");
15882 /* Cancel output redirection, if it is currently set (by .testcase)
15883 ** Then read the content of the testcase-out.txt file and compare against
15884 ** azArg[1]. If there are differences, report an error and exit.
15886 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
15890 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
15892 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
15893 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
15895 }else if( testcase_glob(azArg[1],zRes)==0 ){
15896 utf8_printf(stderr,
15897 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
15898 p->zTestcase, azArg[1], zRes);
15901 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
15904 sqlite3_free(zRes);
15907 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
15909 tryToClone(p, azArg[1]);
15911 raw_printf(stderr, "Usage: .clone FILENAME\n");
15916 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
15920 memcpy(&data, p, sizeof(data));
15921 data.showHeader = 0;
15922 data.cMode = data.mode = MODE_List;
15923 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
15925 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
15926 callback, &data, &zErrMsg);
15928 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15929 sqlite3_free(zErrMsg);
15934 if( c=='d' && n>=3 && strncmp(azArg[0], "dbconfig", n)==0 ){
15935 static const struct DbConfigChoices {
15939 { "defensive", SQLITE_DBCONFIG_DEFENSIVE },
15940 { "dqs_ddl", SQLITE_DBCONFIG_DQS_DDL },
15941 { "dqs_dml", SQLITE_DBCONFIG_DQS_DML },
15942 { "enable_fkey", SQLITE_DBCONFIG_ENABLE_FKEY },
15943 { "enable_qpsg", SQLITE_DBCONFIG_ENABLE_QPSG },
15944 { "enable_trigger", SQLITE_DBCONFIG_ENABLE_TRIGGER },
15945 { "enable_view", SQLITE_DBCONFIG_ENABLE_VIEW },
15946 { "fts3_tokenizer", SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER },
15947 { "legacy_alter_table", SQLITE_DBCONFIG_LEGACY_ALTER_TABLE },
15948 { "legacy_file_format", SQLITE_DBCONFIG_LEGACY_FILE_FORMAT },
15949 { "load_extension", SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION },
15950 { "no_ckpt_on_close", SQLITE_DBCONFIG_NO_CKPT_ON_CLOSE },
15951 { "reset_database", SQLITE_DBCONFIG_RESET_DATABASE },
15952 { "trigger_eqp", SQLITE_DBCONFIG_TRIGGER_EQP },
15953 { "trusted_schema", SQLITE_DBCONFIG_TRUSTED_SCHEMA },
15954 { "writable_schema", SQLITE_DBCONFIG_WRITABLE_SCHEMA },
15958 for(ii=0; ii<ArraySize(aDbConfig); ii++){
15959 if( nArg>1 && strcmp(azArg[1], aDbConfig[ii].zName)!=0 ) continue;
15961 sqlite3_db_config(p->db, aDbConfig[ii].op, booleanValue(azArg[2]), 0);
15963 sqlite3_db_config(p->db, aDbConfig[ii].op, -1, &v);
15964 utf8_printf(p->out, "%19s %s\n", aDbConfig[ii].zName, v ? "on" : "off");
15965 if( nArg>1 ) break;
15967 if( nArg>1 && ii==ArraySize(aDbConfig) ){
15968 utf8_printf(stderr, "Error: unknown dbconfig \"%s\"\n", azArg[1]);
15969 utf8_printf(stderr, "Enter \".dbconfig\" with no arguments for a list\n");
15973 if( c=='d' && n>=3 && strncmp(azArg[0], "dbinfo", n)==0 ){
15974 rc = shell_dbinfo_command(p, nArg, azArg);
15977 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB)
15978 if( c=='r' && strncmp(azArg[0], "recover", n)==0 ){
15980 rc = recoverDatabaseCmd(p, nArg, azArg);
15982 #endif /* !(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_ENABLE_DBPAGE_VTAB) */
15984 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
15988 int savedShowHeader = p->showHeader;
15989 int savedShellFlags = p->shellFlgs;
15990 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines|SHFLG_Echo);
15991 for(i=1; i<nArg; i++){
15992 if( azArg[i][0]=='-' ){
15993 const char *z = azArg[i]+1;
15994 if( z[0]=='-' ) z++;
15995 if( strcmp(z,"preserve-rowids")==0 ){
15996 #ifdef SQLITE_OMIT_VIRTUALTABLE
15997 raw_printf(stderr, "The --preserve-rowids option is not compatible"
15998 " with SQLITE_OMIT_VIRTUALTABLE\n");
16000 goto meta_command_exit;
16002 ShellSetFlag(p, SHFLG_PreserveRowid);
16005 if( strcmp(z,"newlines")==0 ){
16006 ShellSetFlag(p, SHFLG_Newlines);
16009 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
16011 goto meta_command_exit;
16014 zLike = sqlite3_mprintf("%z OR name LIKE %Q ESCAPE '\\'",
16017 zLike = sqlite3_mprintf("name LIKE %Q ESCAPE '\\'", azArg[i]);
16023 /* When playing back a "dump", the content might appear in an order
16024 ** which causes immediate foreign key constraints to be violated.
16025 ** So disable foreign-key constraint enforcement to prevent problems. */
16026 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
16027 raw_printf(p->out, "BEGIN TRANSACTION;\n");
16028 p->writableSchema = 0;
16030 /* Set writable_schema=ON since doing so forces SQLite to initialize
16031 ** as much of the schema as it can even if the sqlite_master table is
16033 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
16035 if( zLike==0 ) zLike = sqlite3_mprintf("true");
16036 zSql = sqlite3_mprintf(
16037 "SELECT name, type, sql FROM sqlite_master "
16038 "WHERE (%s) AND type=='table'"
16039 " AND sql NOT NULL"
16040 " ORDER BY tbl_name='sqlite_sequence', rowid",
16043 run_schema_dump_query(p,zSql);
16044 sqlite3_free(zSql);
16045 zSql = sqlite3_mprintf(
16046 "SELECT sql FROM sqlite_master "
16047 "WHERE (%s) AND sql NOT NULL"
16048 " AND type IN ('index','trigger','view')",
16051 run_table_dump_query(p, zSql);
16052 sqlite3_free(zSql);
16053 sqlite3_free(zLike);
16054 if( p->writableSchema ){
16055 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
16056 p->writableSchema = 0;
16058 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
16059 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
16060 raw_printf(p->out, p->nErr?"ROLLBACK; -- due to errors\n":"COMMIT;\n");
16061 p->showHeader = savedShowHeader;
16062 p->shellFlgs = savedShellFlags;
16065 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
16067 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
16069 raw_printf(stderr, "Usage: .echo on|off\n");
16074 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
16076 p->autoEQPtest = 0;
16077 if( p->autoEQPtrace ){
16078 if( p->db ) sqlite3_exec(p->db, "PRAGMA vdbe_trace=OFF;", 0, 0, 0);
16079 p->autoEQPtrace = 0;
16081 if( strcmp(azArg[1],"full")==0 ){
16082 p->autoEQP = AUTOEQP_full;
16083 }else if( strcmp(azArg[1],"trigger")==0 ){
16084 p->autoEQP = AUTOEQP_trigger;
16085 #ifdef SQLITE_DEBUG
16086 }else if( strcmp(azArg[1],"test")==0 ){
16087 p->autoEQP = AUTOEQP_on;
16088 p->autoEQPtest = 1;
16089 }else if( strcmp(azArg[1],"trace")==0 ){
16090 p->autoEQP = AUTOEQP_full;
16091 p->autoEQPtrace = 1;
16093 sqlite3_exec(p->db, "SELECT name FROM sqlite_master LIMIT 1", 0, 0, 0);
16094 sqlite3_exec(p->db, "PRAGMA vdbe_trace=ON;", 0, 0, 0);
16097 p->autoEQP = (u8)booleanValue(azArg[1]);
16100 raw_printf(stderr, "Usage: .eqp off|on|trace|trigger|full\n");
16105 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
16106 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
16110 /* The ".explain" command is automatic now. It is largely pointless. It
16111 ** retained purely for backwards compatibility */
16112 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
16115 if( strcmp(azArg[1],"auto")==0 ){
16118 val = booleanValue(azArg[1]);
16121 if( val==1 && p->mode!=MODE_Explain ){
16122 p->normalMode = p->mode;
16123 p->mode = MODE_Explain;
16124 p->autoExplain = 0;
16125 }else if( val==0 ){
16126 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
16127 p->autoExplain = 0;
16128 }else if( val==99 ){
16129 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
16130 p->autoExplain = 1;
16134 #ifndef SQLITE_OMIT_VIRTUALTABLE
16135 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
16137 expertDotCommand(p, azArg, nArg);
16141 if( c=='f' && strncmp(azArg[0], "filectrl", n)==0 ){
16142 static const struct {
16143 const char *zCtrlName; /* Name of a test-control option */
16144 int ctrlCode; /* Integer code for that option */
16145 const char *zUsage; /* Usage notes */
16147 { "size_limit", SQLITE_FCNTL_SIZE_LIMIT, "[LIMIT]" },
16148 { "chunk_size", SQLITE_FCNTL_CHUNK_SIZE, "SIZE" },
16149 /* { "win32_av_retry", SQLITE_FCNTL_WIN32_AV_RETRY, "COUNT DELAY" },*/
16150 { "persist_wal", SQLITE_FCNTL_PERSIST_WAL, "[BOOLEAN]" },
16151 { "psow", SQLITE_FCNTL_POWERSAFE_OVERWRITE, "[BOOLEAN]" },
16152 /* { "pragma", SQLITE_FCNTL_PRAGMA, "NAME ARG" },*/
16153 { "tempfilename", SQLITE_FCNTL_TEMPFILENAME, "" },
16154 { "has_moved", SQLITE_FCNTL_HAS_MOVED, "" },
16155 { "lock_timeout", SQLITE_FCNTL_LOCK_TIMEOUT, "MILLISEC" },
16156 { "reserve_bytes", SQLITE_FCNTL_RESERVE_BYTES, "[N]" },
16160 sqlite3_int64 iRes = 0; /* Integer result to display if rc2==1 */
16161 int isOk = 0; /* 0: usage 1: %lld 2: no-result */
16163 const char *zCmd = 0;
16164 const char *zSchema = 0;
16167 zCmd = nArg>=2 ? azArg[1] : "help";
16170 && (strcmp(zCmd,"--schema")==0 || strcmp(zCmd,"-schema")==0)
16173 zSchema = azArg[2];
16174 for(i=3; i<nArg; i++) azArg[i-2] = azArg[i];
16179 /* The argument can optionally begin with "-" or "--" */
16180 if( zCmd[0]=='-' && zCmd[1] ){
16182 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
16185 /* --help lists all file-controls */
16186 if( strcmp(zCmd,"help")==0 ){
16187 utf8_printf(p->out, "Available file-controls:\n");
16188 for(i=0; i<ArraySize(aCtrl); i++){
16189 utf8_printf(p->out, " .filectrl %s %s\n",
16190 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
16193 goto meta_command_exit;
16196 /* convert filectrl text option to value. allow any unique prefix
16197 ** of the option name, or a numerical value. */
16198 n2 = strlen30(zCmd);
16199 for(i=0; i<ArraySize(aCtrl); i++){
16200 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
16202 filectrl = aCtrl[i].ctrlCode;
16205 utf8_printf(stderr, "Error: ambiguous file-control: \"%s\"\n"
16206 "Use \".filectrl --help\" for help\n", zCmd);
16208 goto meta_command_exit;
16213 utf8_printf(stderr,"Error: unknown file-control: %s\n"
16214 "Use \".filectrl --help\" for help\n", zCmd);
16217 case SQLITE_FCNTL_SIZE_LIMIT: {
16218 if( nArg!=2 && nArg!=3 ) break;
16219 iRes = nArg==3 ? integerValue(azArg[2]) : -1;
16220 sqlite3_file_control(p->db, zSchema, SQLITE_FCNTL_SIZE_LIMIT, &iRes);
16224 case SQLITE_FCNTL_LOCK_TIMEOUT:
16225 case SQLITE_FCNTL_CHUNK_SIZE: {
16227 if( nArg!=3 ) break;
16228 x = (int)integerValue(azArg[2]);
16229 sqlite3_file_control(p->db, zSchema, filectrl, &x);
16233 case SQLITE_FCNTL_PERSIST_WAL:
16234 case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
16236 if( nArg!=2 && nArg!=3 ) break;
16237 x = nArg==3 ? booleanValue(azArg[2]) : -1;
16238 sqlite3_file_control(p->db, zSchema, filectrl, &x);
16243 case SQLITE_FCNTL_HAS_MOVED: {
16245 if( nArg!=2 ) break;
16246 sqlite3_file_control(p->db, zSchema, filectrl, &x);
16251 case SQLITE_FCNTL_TEMPFILENAME: {
16253 if( nArg!=2 ) break;
16254 sqlite3_file_control(p->db, zSchema, filectrl, &z);
16256 utf8_printf(p->out, "%s\n", z);
16262 case SQLITE_FCNTL_RESERVE_BYTES: {
16265 x = atoi(azArg[2]);
16266 sqlite3_file_control(p->db, zSchema, filectrl, &x);
16269 sqlite3_file_control(p->db, zSchema, filectrl, &x);
16270 utf8_printf(p->out,"%d\n", x);
16276 if( isOk==0 && iCtrl>=0 ){
16277 utf8_printf(p->out, "Usage: .filectrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
16279 }else if( isOk==1 ){
16281 sqlite3_snprintf(sizeof(zBuf), zBuf, "%lld", iRes);
16282 raw_printf(p->out, "%s\n", zBuf);
16286 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
16290 memcpy(&data, p, sizeof(data));
16291 data.showHeader = 0;
16292 data.cMode = data.mode = MODE_Semi;
16293 if( nArg==2 && optionMatch(azArg[1], "indent") ){
16294 data.cMode = data.mode = MODE_Pretty;
16298 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
16300 goto meta_command_exit;
16303 rc = sqlite3_exec(p->db,
16305 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
16306 " FROM sqlite_master UNION ALL"
16307 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
16308 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
16310 callback, &data, &zErrMsg
16312 if( rc==SQLITE_OK ){
16313 sqlite3_stmt *pStmt;
16314 rc = sqlite3_prepare_v2(p->db,
16315 "SELECT rowid FROM sqlite_master"
16316 " WHERE name GLOB 'sqlite_stat[134]'",
16318 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
16319 sqlite3_finalize(pStmt);
16322 raw_printf(p->out, "/* No STAT tables available */\n");
16324 raw_printf(p->out, "ANALYZE sqlite_master;\n");
16325 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
16326 callback, &data, &zErrMsg);
16327 data.cMode = data.mode = MODE_Insert;
16328 data.zDestTable = "sqlite_stat1";
16329 shell_exec(&data, "SELECT * FROM sqlite_stat1", &zErrMsg);
16330 data.zDestTable = "sqlite_stat4";
16331 shell_exec(&data, "SELECT * FROM sqlite_stat4", &zErrMsg);
16332 raw_printf(p->out, "ANALYZE sqlite_master;\n");
16336 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
16338 p->showHeader = booleanValue(azArg[1]);
16340 raw_printf(stderr, "Usage: .headers on|off\n");
16345 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
16347 n = showHelp(p->out, azArg[1]);
16349 utf8_printf(p->out, "Nothing matches '%s'\n", azArg[1]);
16352 showHelp(p->out, 0);
16356 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
16357 char *zTable = 0; /* Insert data into this table */
16358 char *zFile = 0; /* Name of file to extra content from */
16359 sqlite3_stmt *pStmt = NULL; /* A statement */
16360 int nCol; /* Number of columns in the table */
16361 int nByte; /* Number of bytes in an SQL string */
16362 int i, j; /* Loop counters */
16363 int needCommit; /* True to COMMIT or ROLLBACK at end */
16364 int nSep; /* Number of bytes in p->colSeparator[] */
16365 char *zSql; /* An SQL statement */
16366 ImportCtx sCtx; /* Reader context */
16367 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
16368 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
16369 int eVerbose = 0; /* Larger for more console output */
16370 int nSkip = 0; /* Initial lines to skip */
16371 int useOutputMode = 1; /* Use output mode to determine separators */
16373 memset(&sCtx, 0, sizeof(sCtx));
16374 if( p->mode==MODE_Ascii ){
16375 xRead = ascii_read_one_field;
16377 xRead = csv_read_one_field;
16379 for(i=1; i<nArg; i++){
16380 char *z = azArg[i];
16381 if( z[0]=='-' && z[1]=='-' ) z++;
16385 }else if( zTable==0 ){
16388 utf8_printf(p->out, "ERROR: extra argument: \"%s\". Usage:\n", z);
16389 showHelp(p->out, "import");
16391 goto meta_command_exit;
16393 }else if( strcmp(z,"-v")==0 ){
16395 }else if( strcmp(z,"-skip")==0 && i<nArg-1 ){
16396 nSkip = integerValue(azArg[++i]);
16397 }else if( strcmp(z,"-ascii")==0 ){
16398 sCtx.cColSep = SEP_Unit[0];
16399 sCtx.cRowSep = SEP_Record[0];
16400 xRead = ascii_read_one_field;
16402 }else if( strcmp(z,"-csv")==0 ){
16403 sCtx.cColSep = ',';
16404 sCtx.cRowSep = '\n';
16405 xRead = csv_read_one_field;
16408 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n", z);
16409 showHelp(p->out, "import");
16411 goto meta_command_exit;
16415 utf8_printf(p->out, "ERROR: missing %s argument. Usage:\n",
16416 zFile==0 ? "FILE" : "TABLE");
16417 showHelp(p->out, "import");
16419 goto meta_command_exit;
16423 if( useOutputMode ){
16424 /* If neither the --csv or --ascii options are specified, then set
16425 ** the column and row separator characters from the output mode. */
16426 nSep = strlen30(p->colSeparator);
16429 "Error: non-null column separator required for import\n");
16431 goto meta_command_exit;
16435 "Error: multi-character column separators not allowed"
16438 goto meta_command_exit;
16440 nSep = strlen30(p->rowSeparator);
16443 "Error: non-null row separator required for import\n");
16445 goto meta_command_exit;
16447 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator,SEP_CrLf)==0 ){
16448 /* When importing CSV (only), if the row separator is set to the
16449 ** default output row separator, change it to the default input
16450 ** row separator. This avoids having to maintain different input
16451 ** and output row separators. */
16452 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16453 nSep = strlen30(p->rowSeparator);
16456 raw_printf(stderr, "Error: multi-character row separators not allowed"
16459 goto meta_command_exit;
16461 sCtx.cColSep = p->colSeparator[0];
16462 sCtx.cRowSep = p->rowSeparator[0];
16464 sCtx.zFile = zFile;
16466 if( sCtx.zFile[0]=='|' ){
16467 #ifdef SQLITE_OMIT_POPEN
16468 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
16470 goto meta_command_exit;
16472 sCtx.in = popen(sCtx.zFile+1, "r");
16473 sCtx.zFile = "<pipe>";
16477 sCtx.in = fopen(sCtx.zFile, "rb");
16481 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
16483 goto meta_command_exit;
16485 if( eVerbose>=2 || (eVerbose>=1 && useOutputMode) ){
16488 zSep[0] = sCtx.cColSep;
16489 utf8_printf(p->out, "Column separator ");
16490 output_c_string(p->out, zSep);
16491 utf8_printf(p->out, ", row separator ");
16492 zSep[0] = sCtx.cRowSep;
16493 output_c_string(p->out, zSep);
16494 utf8_printf(p->out, "\n");
16496 while( (nSkip--)>0 ){
16497 while( xRead(&sCtx) && sCtx.cTerm==sCtx.cColSep ){}
16500 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
16503 shell_out_of_memory();
16505 nByte = strlen30(zSql);
16506 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16507 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
16508 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
16509 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
16511 while( xRead(&sCtx) ){
16512 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
16514 if( sCtx.cTerm!=sCtx.cColSep ) break;
16517 sqlite3_free(zCreate);
16518 sqlite3_free(sCtx.z);
16520 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
16522 goto meta_command_exit;
16524 zCreate = sqlite3_mprintf("%z\n)", zCreate);
16526 utf8_printf(p->out, "%s\n", zCreate);
16528 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
16529 sqlite3_free(zCreate);
16531 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
16532 sqlite3_errmsg(p->db));
16533 sqlite3_free(sCtx.z);
16536 goto meta_command_exit;
16538 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16540 sqlite3_free(zSql);
16542 if (pStmt) sqlite3_finalize(pStmt);
16543 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
16546 goto meta_command_exit;
16548 nCol = sqlite3_column_count(pStmt);
16549 sqlite3_finalize(pStmt);
16551 if( nCol==0 ) return 0; /* no columns, no error */
16552 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
16555 shell_out_of_memory();
16557 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
16558 j = strlen30(zSql);
16559 for(i=1; i<nCol; i++){
16566 utf8_printf(p->out, "Insert using: %s\n", zSql);
16568 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16569 sqlite3_free(zSql);
16571 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
16572 if (pStmt) sqlite3_finalize(pStmt);
16575 goto meta_command_exit;
16577 needCommit = sqlite3_get_autocommit(p->db);
16578 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
16580 int startLine = sCtx.nLine;
16581 for(i=0; i<nCol; i++){
16582 char *z = xRead(&sCtx);
16584 ** Did we reach end-of-file before finding any columns?
16585 ** If so, stop instead of NULL filling the remaining columns.
16587 if( z==0 && i==0 ) break;
16589 ** Did we reach end-of-file OR end-of-line before finding any
16590 ** columns in ASCII mode? If so, stop instead of NULL filling
16591 ** the remaining columns.
16593 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
16594 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
16595 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
16596 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16597 "filling the rest with NULL\n",
16598 sCtx.zFile, startLine, nCol, i+1);
16600 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
16603 if( sCtx.cTerm==sCtx.cColSep ){
16607 }while( sCtx.cTerm==sCtx.cColSep );
16608 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
16609 "extras ignored\n",
16610 sCtx.zFile, startLine, nCol, i);
16613 sqlite3_step(pStmt);
16614 rc = sqlite3_reset(pStmt);
16615 if( rc!=SQLITE_OK ){
16616 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
16617 startLine, sqlite3_errmsg(p->db));
16623 }while( sCtx.cTerm!=EOF );
16626 sqlite3_free(sCtx.z);
16627 sqlite3_finalize(pStmt);
16628 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
16630 utf8_printf(p->out,
16631 "Added %d rows with %d errors using %d lines of input\n",
16632 sCtx.nRow, sCtx.nErr, sCtx.nLine-1);
16636 #ifndef SQLITE_UNTESTABLE
16637 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
16639 char *zCollist = 0;
16640 sqlite3_stmt *pStmt;
16642 int isWO = 0; /* True if making an imposter of a WITHOUT ROWID table */
16643 int lenPK = 0; /* Length of the PRIMARY KEY string for isWO tables */
16645 if( !(nArg==3 || (nArg==2 && sqlite3_stricmp(azArg[1],"off")==0)) ){
16646 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n"
16647 " .imposter off\n");
16648 /* Also allowed, but not documented:
16650 ** .imposter TABLE IMPOSTER
16652 ** where TABLE is a WITHOUT ROWID table. In that case, the
16653 ** imposter is another WITHOUT ROWID table with the columns in
16654 ** storage order. */
16656 goto meta_command_exit;
16660 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 1);
16661 goto meta_command_exit;
16663 zSql = sqlite3_mprintf(
16664 "SELECT rootpage, 0 FROM sqlite_master"
16665 " WHERE name='%q' AND type='index'"
16667 "SELECT rootpage, 1 FROM sqlite_master"
16668 " WHERE name='%q' AND type='table'"
16669 " AND sql LIKE '%%without%%rowid%%'",
16672 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16673 sqlite3_free(zSql);
16674 if( sqlite3_step(pStmt)==SQLITE_ROW ){
16675 tnum = sqlite3_column_int(pStmt, 0);
16676 isWO = sqlite3_column_int(pStmt, 1);
16678 sqlite3_finalize(pStmt);
16679 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
16680 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
16681 sqlite3_free(zSql);
16683 while( sqlite3_step(pStmt)==SQLITE_ROW ){
16685 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
16688 if( sqlite3_column_int(pStmt,1)==-1 ){
16691 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
16695 if( isWO && lenPK==0 && sqlite3_column_int(pStmt,5)==0 && zCollist ){
16696 lenPK = (int)strlen(zCollist);
16699 zCollist = sqlite3_mprintf("\"%w\"", zCol);
16701 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
16704 sqlite3_finalize(pStmt);
16705 if( i==0 || tnum==0 ){
16706 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
16708 sqlite3_free(zCollist);
16709 goto meta_command_exit;
16711 if( lenPK==0 ) lenPK = 100000;
16712 zSql = sqlite3_mprintf(
16713 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%.*s))WITHOUT ROWID",
16714 azArg[2], zCollist, lenPK, zCollist);
16715 sqlite3_free(zCollist);
16716 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
16717 if( rc==SQLITE_OK ){
16718 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
16719 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
16721 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
16723 utf8_printf(stdout, "%s;\n", zSql);
16725 "WARNING: writing to an imposter table will corrupt the \"%s\" %s!\n",
16726 azArg[1], isWO ? "table" : "index"
16730 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
16733 sqlite3_free(zSql);
16735 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
16737 #ifdef SQLITE_ENABLE_IOTRACE
16738 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
16739 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
16740 if( iotrace && iotrace!=stdout ) fclose(iotrace);
16743 sqlite3IoTrace = 0;
16744 }else if( strcmp(azArg[1], "-")==0 ){
16745 sqlite3IoTrace = iotracePrintf;
16748 iotrace = fopen(azArg[1], "w");
16750 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
16751 sqlite3IoTrace = 0;
16754 sqlite3IoTrace = iotracePrintf;
16760 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
16761 static const struct {
16762 const char *zLimitName; /* Name of a limit */
16763 int limitCode; /* Integer code for that limit */
16765 { "length", SQLITE_LIMIT_LENGTH },
16766 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
16767 { "column", SQLITE_LIMIT_COLUMN },
16768 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
16769 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
16770 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
16771 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
16772 { "attached", SQLITE_LIMIT_ATTACHED },
16773 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
16774 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
16775 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
16776 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
16781 for(i=0; i<ArraySize(aLimit); i++){
16782 printf("%20s %d\n", aLimit[i].zLimitName,
16783 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
16785 }else if( nArg>3 ){
16786 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
16788 goto meta_command_exit;
16791 n2 = strlen30(azArg[1]);
16792 for(i=0; i<ArraySize(aLimit); i++){
16793 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
16797 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
16799 goto meta_command_exit;
16804 utf8_printf(stderr, "unknown limit: \"%s\"\n"
16805 "enter \".limits\" with no arguments for a list.\n",
16808 goto meta_command_exit;
16811 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
16812 (int)integerValue(azArg[2]));
16814 printf("%20s %d\n", aLimit[iLimit].zLimitName,
16815 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
16819 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
16821 lintDotCommand(p, azArg, nArg);
16824 #ifndef SQLITE_OMIT_LOAD_EXTENSION
16825 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
16826 const char *zFile, *zProc;
16829 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
16831 goto meta_command_exit;
16834 zProc = nArg>=3 ? azArg[2] : 0;
16836 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
16837 if( rc!=SQLITE_OK ){
16838 utf8_printf(stderr, "Error: %s\n", zErrMsg);
16839 sqlite3_free(zErrMsg);
16845 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
16847 raw_printf(stderr, "Usage: .log FILENAME\n");
16850 const char *zFile = azArg[1];
16851 output_file_close(p->pLog);
16852 p->pLog = output_file_open(zFile, 0);
16856 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
16857 const char *zMode = nArg>=2 ? azArg[1] : "";
16858 int n2 = strlen30(zMode);
16860 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
16861 p->mode = MODE_Line;
16862 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16863 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
16864 p->mode = MODE_Column;
16865 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16866 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
16867 p->mode = MODE_List;
16868 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
16869 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16870 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
16871 p->mode = MODE_Html;
16872 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
16873 p->mode = MODE_Tcl;
16874 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
16875 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
16876 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
16877 p->mode = MODE_Csv;
16878 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
16879 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
16880 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
16881 p->mode = MODE_List;
16882 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
16883 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
16884 p->mode = MODE_Insert;
16885 set_table_name(p, nArg>=3 ? azArg[2] : "table");
16886 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
16887 p->mode = MODE_Quote;
16888 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
16889 p->mode = MODE_Ascii;
16890 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
16891 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
16892 }else if( nArg==1 ){
16893 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
16895 raw_printf(stderr, "Error: mode should be one of: "
16896 "ascii column csv html insert line list quote tabs tcl\n");
16899 p->cMode = p->mode;
16902 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
16904 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
16905 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
16907 raw_printf(stderr, "Usage: .nullvalue STRING\n");
16912 #ifdef SQLITE_DEBUG
16913 if( c=='o' && strcmp(azArg[0],"oom")==0 ){
16915 for(i=1; i<nArg; i++){
16916 const char *z = azArg[i];
16917 if( z[0]=='-' && z[1]=='-' ) z++;
16918 if( strcmp(z,"-repeat")==0 ){
16920 raw_printf(p->out, "missing argument on \"%s\"\n", azArg[i]);
16923 oomRepeat = (int)integerValue(azArg[++i]);
16925 }else if( IsDigit(z[0]) ){
16926 oomCounter = (int)integerValue(azArg[i]);
16928 raw_printf(p->out, "unknown argument: \"%s\"\n", azArg[i]);
16929 raw_printf(p->out, "Usage: .oom [--repeat N] [M]\n");
16934 raw_printf(p->out, "oomCounter = %d\n", oomCounter);
16935 raw_printf(p->out, "oomRepeat = %d\n", oomRepeat);
16938 #endif /* SQLITE_DEBUG */
16940 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
16941 char *zNewFilename; /* Name of the database file to open */
16942 int iName = 1; /* Index in azArg[] of the filename */
16943 int newFlag = 0; /* True to delete file before opening */
16944 /* Close the existing database */
16945 session_close_all(p);
16948 p->zDbFilename = 0;
16949 sqlite3_free(p->zFreeOnClose);
16950 p->zFreeOnClose = 0;
16951 p->openMode = SHELL_OPEN_UNSPEC;
16954 /* Check for command-line arguments */
16955 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
16956 const char *z = azArg[iName];
16957 if( optionMatch(z,"new") ){
16959 #ifdef SQLITE_HAVE_ZLIB
16960 }else if( optionMatch(z, "zip") ){
16961 p->openMode = SHELL_OPEN_ZIPFILE;
16963 }else if( optionMatch(z, "append") ){
16964 p->openMode = SHELL_OPEN_APPENDVFS;
16965 }else if( optionMatch(z, "readonly") ){
16966 p->openMode = SHELL_OPEN_READONLY;
16967 }else if( optionMatch(z, "nofollow") ){
16968 p->openFlags |= SQLITE_OPEN_NOFOLLOW;
16969 #ifdef SQLITE_ENABLE_DESERIALIZE
16970 }else if( optionMatch(z, "deserialize") ){
16971 p->openMode = SHELL_OPEN_DESERIALIZE;
16972 }else if( optionMatch(z, "hexdb") ){
16973 p->openMode = SHELL_OPEN_HEXDB;
16974 }else if( optionMatch(z, "maxsize") && iName+1<nArg ){
16975 p->szMax = integerValue(azArg[++iName]);
16976 #endif /* SQLITE_ENABLE_DESERIALIZE */
16977 }else if( z[0]=='-' ){
16978 utf8_printf(stderr, "unknown option: %s\n", z);
16980 goto meta_command_exit;
16983 /* If a filename is specified, try to open it first */
16984 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
16985 if( zNewFilename || p->openMode==SHELL_OPEN_HEXDB ){
16986 if( newFlag ) shellDeleteFile(zNewFilename);
16987 p->zDbFilename = zNewFilename;
16988 open_db(p, OPEN_DB_KEEPALIVE);
16990 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
16991 sqlite3_free(zNewFilename);
16993 p->zFreeOnClose = zNewFilename;
16997 /* As a fall-back open a TEMP database */
16998 p->zDbFilename = 0;
17004 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
17005 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
17007 const char *zFile = 0;
17012 int bOnce = 0; /* 0: .output, 1: .once, 2: .excel */
17017 }else if( strncmp(azArg[0],"once",n)==0 ){
17020 for(i=1; i<nArg; i++){
17021 char *z = azArg[i];
17023 if( z[1]=='-' ) z++;
17024 if( strcmp(z,"-bom")==0 ){
17026 }else if( c!='e' && strcmp(z,"-x")==0 ){
17027 eMode = 'x'; /* spreadsheet */
17028 }else if( c!='e' && strcmp(z,"-e")==0 ){
17029 eMode = 'e'; /* text editor */
17031 utf8_printf(p->out, "ERROR: unknown option: \"%s\". Usage:\n",
17033 showHelp(p->out, azArg[0]);
17035 goto meta_command_exit;
17037 }else if( zFile==0 ){
17040 utf8_printf(p->out,"ERROR: extra parameter: \"%s\". Usage:\n",
17042 showHelp(p->out, azArg[0]);
17044 goto meta_command_exit;
17047 if( zFile==0 ) zFile = "stdout";
17054 #ifndef SQLITE_NOHAVE_SYSTEM
17055 if( eMode=='e' || eMode=='x' ){
17059 /* spreadsheet mode. Output as CSV. */
17060 newTempFile(p, "csv");
17061 ShellClearFlag(p, SHFLG_Echo);
17062 p->mode = MODE_Csv;
17063 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
17064 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
17066 /* text editor mode */
17067 newTempFile(p, "txt");
17070 zFile = p->zTempFile;
17072 #endif /* SQLITE_NOHAVE_SYSTEM */
17073 if( zFile[0]=='|' ){
17074 #ifdef SQLITE_OMIT_POPEN
17075 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
17079 p->out = popen(zFile + 1, "w");
17081 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
17085 if( bBOM ) fprintf(p->out,"\357\273\277");
17086 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
17090 p->out = output_file_open(zFile, bTxtMode);
17092 if( strcmp(zFile,"off")!=0 ){
17093 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
17098 if( bBOM ) fprintf(p->out,"\357\273\277");
17099 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
17104 if( c=='p' && n>=3 && strncmp(azArg[0], "parameter", n)==0 ){
17106 if( nArg<=1 ) goto parameter_syntax_error;
17108 /* .parameter clear
17109 ** Clear all bind parameters by dropping the TEMP table that holds them.
17111 if( nArg==2 && strcmp(azArg[1],"clear")==0 ){
17112 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp.sqlite_parameters;",
17117 ** List all bind parameters.
17119 if( nArg==2 && strcmp(azArg[1],"list")==0 ){
17120 sqlite3_stmt *pStmt = 0;
17123 rx = sqlite3_prepare_v2(p->db,
17124 "SELECT max(length(key)) "
17125 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
17126 if( rx==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW ){
17127 len = sqlite3_column_int(pStmt, 0);
17128 if( len>40 ) len = 40;
17130 sqlite3_finalize(pStmt);
17133 rx = sqlite3_prepare_v2(p->db,
17134 "SELECT key, quote(value) "
17135 "FROM temp.sqlite_parameters;", -1, &pStmt, 0);
17136 while( sqlite3_step(pStmt)==SQLITE_ROW ){
17137 utf8_printf(p->out, "%-*s %s\n", len, sqlite3_column_text(pStmt,0),
17138 sqlite3_column_text(pStmt,1));
17140 sqlite3_finalize(pStmt);
17145 ** Make sure the TEMP table used to hold bind parameters exists.
17146 ** Create it if necessary.
17148 if( nArg==2 && strcmp(azArg[1],"init")==0 ){
17149 bind_table_init(p);
17152 /* .parameter set NAME VALUE
17153 ** Set or reset a bind parameter. NAME should be the full parameter
17154 ** name exactly as it appears in the query. (ex: $abc, @def). The
17155 ** VALUE can be in either SQL literal notation, or if not it will be
17156 ** understood to be a text string.
17158 if( nArg==4 && strcmp(azArg[1],"set")==0 ){
17161 sqlite3_stmt *pStmt;
17162 const char *zKey = azArg[2];
17163 const char *zValue = azArg[3];
17164 bind_table_init(p);
17165 zSql = sqlite3_mprintf(
17166 "REPLACE INTO temp.sqlite_parameters(key,value)"
17167 "VALUES(%Q,%s);", zKey, zValue);
17168 if( zSql==0 ) shell_out_of_memory();
17170 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17171 sqlite3_free(zSql);
17172 if( rx!=SQLITE_OK ){
17173 sqlite3_finalize(pStmt);
17175 zSql = sqlite3_mprintf(
17176 "REPLACE INTO temp.sqlite_parameters(key,value)"
17177 "VALUES(%Q,%Q);", zKey, zValue);
17178 if( zSql==0 ) shell_out_of_memory();
17179 rx = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17180 sqlite3_free(zSql);
17181 if( rx!=SQLITE_OK ){
17182 utf8_printf(p->out, "Error: %s\n", sqlite3_errmsg(p->db));
17183 sqlite3_finalize(pStmt);
17188 sqlite3_step(pStmt);
17189 sqlite3_finalize(pStmt);
17192 /* .parameter unset NAME
17193 ** Remove the NAME binding from the parameter binding table, if it
17196 if( nArg==3 && strcmp(azArg[1],"unset")==0 ){
17197 char *zSql = sqlite3_mprintf(
17198 "DELETE FROM temp.sqlite_parameters WHERE key=%Q", azArg[2]);
17199 if( zSql==0 ) shell_out_of_memory();
17200 sqlite3_exec(p->db, zSql, 0, 0, 0);
17201 sqlite3_free(zSql);
17203 /* If no command name matches, show a syntax error */
17204 parameter_syntax_error:
17205 showHelp(p->out, "parameter");
17208 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
17210 for(i=1; i<nArg; i++){
17211 if( i>1 ) raw_printf(p->out, " ");
17212 utf8_printf(p->out, "%s", azArg[i]);
17214 raw_printf(p->out, "\n");
17217 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
17218 if( c=='p' && n>=3 && strncmp(azArg[0], "progress", n)==0 ){
17221 p->flgProgress = 0;
17224 for(i=1; i<nArg; i++){
17225 const char *z = azArg[i];
17228 if( z[0]=='-' ) z++;
17229 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
17230 p->flgProgress |= SHELL_PROGRESS_QUIET;
17233 if( strcmp(z,"reset")==0 ){
17234 p->flgProgress |= SHELL_PROGRESS_RESET;
17237 if( strcmp(z,"once")==0 ){
17238 p->flgProgress |= SHELL_PROGRESS_ONCE;
17241 if( strcmp(z,"limit")==0 ){
17243 utf8_printf(stderr, "Error: missing argument on --limit\n");
17245 goto meta_command_exit;
17247 p->mxProgress = (int)integerValue(azArg[++i]);
17251 utf8_printf(stderr, "Error: unknown option: \"%s\"\n", azArg[i]);
17253 goto meta_command_exit;
17255 nn = (int)integerValue(z);
17259 sqlite3_progress_handler(p->db, nn, progress_handler, p);
17261 #endif /* SQLITE_OMIT_PROGRESS_CALLBACK */
17263 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
17265 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
17268 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
17272 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
17276 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
17277 FILE *inSaved = p->in;
17278 int savedLineno = p->lineno;
17280 raw_printf(stderr, "Usage: .read FILE\n");
17282 goto meta_command_exit;
17284 p->in = fopen(azArg[1], "rb");
17286 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
17289 rc = process_input(p);
17293 p->lineno = savedLineno;
17296 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
17297 const char *zSrcFile;
17300 sqlite3_backup *pBackup;
17304 zSrcFile = azArg[1];
17306 }else if( nArg==3 ){
17307 zSrcFile = azArg[2];
17310 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
17312 goto meta_command_exit;
17314 rc = sqlite3_open(zSrcFile, &pSrc);
17315 if( rc!=SQLITE_OK ){
17316 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
17321 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
17323 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17327 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
17328 || rc==SQLITE_BUSY ){
17329 if( rc==SQLITE_BUSY ){
17330 if( nTimeout++ >= 3 ) break;
17331 sqlite3_sleep(100);
17334 sqlite3_backup_finish(pBackup);
17335 if( rc==SQLITE_DONE ){
17337 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
17338 raw_printf(stderr, "Error: source database is busy\n");
17341 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17347 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
17349 p->scanstatsOn = (u8)booleanValue(azArg[1]);
17350 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
17351 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
17354 raw_printf(stderr, "Usage: .scanstats on|off\n");
17359 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
17363 const char *zDiv = "(";
17364 const char *zName = 0;
17370 memcpy(&data, p, sizeof(data));
17371 data.showHeader = 0;
17372 data.cMode = data.mode = MODE_Semi;
17373 initText(&sSelect);
17374 for(ii=1; ii<nArg; ii++){
17375 if( optionMatch(azArg[ii],"indent") ){
17376 data.cMode = data.mode = MODE_Pretty;
17377 }else if( optionMatch(azArg[ii],"debug") ){
17379 }else if( zName==0 ){
17382 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
17384 goto meta_command_exit;
17388 int isMaster = sqlite3_strlike(zName, "sqlite_master", '\\')==0;
17389 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master", '\\')==0 ){
17390 char *new_argv[2], *new_colv[2];
17391 new_argv[0] = sqlite3_mprintf(
17392 "CREATE TABLE %s (\n"
17395 " tbl_name text,\n"
17396 " rootpage integer,\n"
17398 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
17400 new_colv[0] = "sql";
17402 callback(&data, 1, new_argv, new_colv);
17403 sqlite3_free(new_argv[0]);
17407 sqlite3_stmt *pStmt = 0;
17408 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
17411 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
17412 sqlite3_finalize(pStmt);
17414 goto meta_command_exit;
17416 appendText(&sSelect, "SELECT sql FROM", 0);
17418 while( sqlite3_step(pStmt)==SQLITE_ROW ){
17419 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
17421 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
17422 appendText(&sSelect, zDiv, 0);
17423 zDiv = " UNION ALL ";
17424 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
17425 if( sqlite3_stricmp(zDb, "main")!=0 ){
17426 appendText(&sSelect, zDb, '\'');
17428 appendText(&sSelect, "NULL", 0);
17430 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
17431 appendText(&sSelect, zScNum, 0);
17432 appendText(&sSelect, " AS snum, ", 0);
17433 appendText(&sSelect, zDb, '\'');
17434 appendText(&sSelect, " AS sname FROM ", 0);
17435 appendText(&sSelect, zDb, quoteChar(zDb));
17436 appendText(&sSelect, ".sqlite_master", 0);
17438 sqlite3_finalize(pStmt);
17439 #ifndef SQLITE_OMIT_INTROSPECTION_PRAGMAS
17441 appendText(&sSelect,
17442 " UNION ALL SELECT shell_module_schema(name),"
17443 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list",
17447 appendText(&sSelect, ") WHERE ", 0);
17449 char *zQarg = sqlite3_mprintf("%Q", zName);
17450 int bGlob = strchr(zName, '*') != 0 || strchr(zName, '?') != 0 ||
17451 strchr(zName, '[') != 0;
17452 if( strchr(zName, '.') ){
17453 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
17455 appendText(&sSelect, "lower(tbl_name)", 0);
17457 appendText(&sSelect, bGlob ? " GLOB " : " LIKE ", 0);
17458 appendText(&sSelect, zQarg, 0);
17460 appendText(&sSelect, " ESCAPE '\\' ", 0);
17462 appendText(&sSelect, " AND ", 0);
17463 sqlite3_free(zQarg);
17465 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
17466 " ORDER BY snum, rowid", 0);
17468 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
17470 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
17472 freeText(&sSelect);
17475 utf8_printf(stderr,"Error: %s\n", zErrMsg);
17476 sqlite3_free(zErrMsg);
17478 }else if( rc != SQLITE_OK ){
17479 raw_printf(stderr,"Error: querying schema information\n");
17486 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
17487 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
17488 sqlite3SelectTrace = (int)integerValue(azArg[1]);
17492 #if defined(SQLITE_ENABLE_SESSION)
17493 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
17494 OpenSession *pSession = &p->aSession[0];
17495 char **azCmd = &azArg[1];
17497 int nCmd = nArg - 1;
17499 if( nArg<=1 ) goto session_syntax_error;
17502 for(iSes=0; iSes<p->nSession; iSes++){
17503 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
17505 if( iSes<p->nSession ){
17506 pSession = &p->aSession[iSes];
17510 pSession = &p->aSession[0];
17515 /* .session attach TABLE
17516 ** Invoke the sqlite3session_attach() interface to attach a particular
17517 ** table so that it is never filtered.
17519 if( strcmp(azCmd[0],"attach")==0 ){
17520 if( nCmd!=2 ) goto session_syntax_error;
17521 if( pSession->p==0 ){
17523 raw_printf(stderr, "ERROR: No sessions are open\n");
17525 rc = sqlite3session_attach(pSession->p, azCmd[1]);
17527 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
17533 /* .session changeset FILE
17534 ** .session patchset FILE
17535 ** Write a changeset or patchset into a file. The file is overwritten.
17537 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
17539 if( nCmd!=2 ) goto session_syntax_error;
17540 if( pSession->p==0 ) goto session_not_open;
17541 out = fopen(azCmd[1], "wb");
17543 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n",
17548 if( azCmd[0][0]=='c' ){
17549 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
17551 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
17554 printf("Error: error code %d\n", rc);
17558 && fwrite(pChng, szChng, 1, out)!=1 ){
17559 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
17562 sqlite3_free(pChng);
17568 ** Close the identified session
17570 if( strcmp(azCmd[0], "close")==0 ){
17571 if( nCmd!=1 ) goto session_syntax_error;
17573 session_close(pSession);
17574 p->aSession[iSes] = p->aSession[--p->nSession];
17578 /* .session enable ?BOOLEAN?
17579 ** Query or set the enable flag
17581 if( strcmp(azCmd[0], "enable")==0 ){
17583 if( nCmd>2 ) goto session_syntax_error;
17584 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17586 ii = sqlite3session_enable(pSession->p, ii);
17587 utf8_printf(p->out, "session %s enable flag = %d\n",
17588 pSession->zName, ii);
17592 /* .session filter GLOB ....
17593 ** Set a list of GLOB patterns of table names to be excluded.
17595 if( strcmp(azCmd[0], "filter")==0 ){
17597 if( nCmd<2 ) goto session_syntax_error;
17599 for(ii=0; ii<pSession->nFilter; ii++){
17600 sqlite3_free(pSession->azFilter[ii]);
17602 sqlite3_free(pSession->azFilter);
17603 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
17604 pSession->azFilter = sqlite3_malloc( nByte );
17605 if( pSession->azFilter==0 ){
17606 raw_printf(stderr, "Error: out or memory\n");
17609 for(ii=1; ii<nCmd; ii++){
17610 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
17612 pSession->nFilter = ii-1;
17616 /* .session indirect ?BOOLEAN?
17617 ** Query or set the indirect flag
17619 if( strcmp(azCmd[0], "indirect")==0 ){
17621 if( nCmd>2 ) goto session_syntax_error;
17622 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
17624 ii = sqlite3session_indirect(pSession->p, ii);
17625 utf8_printf(p->out, "session %s indirect flag = %d\n",
17626 pSession->zName, ii);
17630 /* .session isempty
17631 ** Determine if the session is empty
17633 if( strcmp(azCmd[0], "isempty")==0 ){
17635 if( nCmd!=1 ) goto session_syntax_error;
17637 ii = sqlite3session_isempty(pSession->p);
17638 utf8_printf(p->out, "session %s isempty flag = %d\n",
17639 pSession->zName, ii);
17644 ** List all currently open sessions
17646 if( strcmp(azCmd[0],"list")==0 ){
17647 for(i=0; i<p->nSession; i++){
17648 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
17652 /* .session open DB NAME
17653 ** Open a new session called NAME on the attached database DB.
17654 ** DB is normally "main".
17656 if( strcmp(azCmd[0],"open")==0 ){
17658 if( nCmd!=3 ) goto session_syntax_error;
17660 if( zName[0]==0 ) goto session_syntax_error;
17661 for(i=0; i<p->nSession; i++){
17662 if( strcmp(p->aSession[i].zName,zName)==0 ){
17663 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
17664 goto meta_command_exit;
17667 if( p->nSession>=ArraySize(p->aSession) ){
17668 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
17669 goto meta_command_exit;
17671 pSession = &p->aSession[p->nSession];
17672 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
17674 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
17676 goto meta_command_exit;
17678 pSession->nFilter = 0;
17679 sqlite3session_table_filter(pSession->p, session_filter, pSession);
17681 pSession->zName = sqlite3_mprintf("%s", zName);
17683 /* If no command name matches, show a syntax error */
17684 session_syntax_error:
17685 showHelp(p->out, "session");
17689 #ifdef SQLITE_DEBUG
17690 /* Undocumented commands for internal testing. Subject to change
17691 ** without notice. */
17692 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
17693 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
17695 for(i=1; i<nArg; i++){
17696 v = booleanValue(azArg[i]);
17697 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
17700 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
17701 int i; sqlite3_int64 v;
17702 for(i=1; i<nArg; i++){
17704 v = integerValue(azArg[i]);
17705 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
17706 utf8_printf(p->out, "%s", zBuf);
17712 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
17713 int bIsInit = 0; /* True to initialize the SELFTEST table */
17714 int bVerbose = 0; /* Verbose output */
17715 int bSelftestExists; /* True if SELFTEST already exists */
17716 int i, k; /* Loop counters */
17717 int nTest = 0; /* Number of tests runs */
17718 int nErr = 0; /* Number of errors seen */
17719 ShellText str; /* Answer for a query */
17720 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
17723 for(i=1; i<nArg; i++){
17724 const char *z = azArg[i];
17725 if( z[0]=='-' && z[1]=='-' ) z++;
17726 if( strcmp(z,"-init")==0 ){
17729 if( strcmp(z,"-v")==0 ){
17733 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17734 azArg[i], azArg[0]);
17735 raw_printf(stderr, "Should be one of: --init -v\n");
17737 goto meta_command_exit;
17740 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
17742 bSelftestExists = 0;
17744 bSelftestExists = 1;
17747 createSelftestTable(p);
17748 bSelftestExists = 1;
17751 appendText(&str, "x", 0);
17752 for(k=bSelftestExists; k>=0; k--){
17754 rc = sqlite3_prepare_v2(p->db,
17755 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
17758 rc = sqlite3_prepare_v2(p->db,
17759 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
17760 " (1,'run','PRAGMA integrity_check','ok')",
17764 raw_printf(stderr, "Error querying the selftest table\n");
17766 sqlite3_finalize(pStmt);
17767 goto meta_command_exit;
17769 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
17770 int tno = sqlite3_column_int(pStmt, 0);
17771 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
17772 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
17773 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
17777 char *zQuote = sqlite3_mprintf("%q", zSql);
17778 printf("%d: %s %s\n", tno, zOp, zSql);
17779 sqlite3_free(zQuote);
17781 if( strcmp(zOp,"memo")==0 ){
17782 utf8_printf(p->out, "%s\n", zSql);
17784 if( strcmp(zOp,"run")==0 ){
17788 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
17791 utf8_printf(p->out, "Result: %s\n", str.z);
17793 if( rc || zErrMsg ){
17796 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
17797 sqlite3_free(zErrMsg);
17798 }else if( strcmp(zAns,str.z)!=0 ){
17801 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
17802 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
17806 utf8_printf(stderr,
17807 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
17811 } /* End loop over rows of content from SELFTEST */
17812 sqlite3_finalize(pStmt);
17813 } /* End loop over k */
17815 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
17818 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
17819 if( nArg<2 || nArg>3 ){
17820 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
17824 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
17825 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
17828 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
17829 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
17833 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
17834 const char *zLike = 0; /* Which table to checksum. 0 means everything */
17835 int i; /* Loop counter */
17836 int bSchema = 0; /* Also hash the schema */
17837 int bSeparate = 0; /* Hash each table separately */
17838 int iSize = 224; /* Hash algorithm to use */
17839 int bDebug = 0; /* Only show the query that would have run */
17840 sqlite3_stmt *pStmt; /* For querying tables names */
17841 char *zSql; /* SQL to be run */
17842 char *zSep; /* Separator */
17843 ShellText sSql; /* Complete SQL for the query to run the hash */
17844 ShellText sQuery; /* Set of queries used to read all content */
17846 for(i=1; i<nArg; i++){
17847 const char *z = azArg[i];
17850 if( z[0]=='-' ) z++;
17851 if( strcmp(z,"schema")==0 ){
17854 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
17855 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
17857 iSize = atoi(&z[5]);
17859 if( strcmp(z,"debug")==0 ){
17863 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
17864 azArg[i], azArg[0]);
17865 showHelp(p->out, azArg[0]);
17867 goto meta_command_exit;
17870 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
17872 goto meta_command_exit;
17876 if( sqlite3_strlike("sqlite\\_%", zLike, '\\')==0 ) bSchema = 1;
17880 zSql = "SELECT lower(name) FROM sqlite_master"
17881 " WHERE type='table' AND coalesce(rootpage,0)>1"
17882 " UNION ALL SELECT 'sqlite_master'"
17883 " ORDER BY 1 collate nocase";
17885 zSql = "SELECT lower(name) FROM sqlite_master"
17886 " WHERE type='table' AND coalesce(rootpage,0)>1"
17887 " AND name NOT LIKE 'sqlite_%'"
17888 " ORDER BY 1 collate nocase";
17890 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
17893 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
17895 while( SQLITE_ROW==sqlite3_step(pStmt) ){
17896 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
17897 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
17898 if( strncmp(zTab, "sqlite_",7)!=0 ){
17899 appendText(&sQuery,"SELECT * FROM ", 0);
17900 appendText(&sQuery,zTab,'"');
17901 appendText(&sQuery," NOT INDEXED;", 0);
17902 }else if( strcmp(zTab, "sqlite_master")==0 ){
17903 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
17904 " ORDER BY name;", 0);
17905 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
17906 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
17907 " ORDER BY name;", 0);
17908 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
17909 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
17910 " ORDER BY tbl,idx;", 0);
17911 }else if( strcmp(zTab, "sqlite_stat4")==0 ){
17912 appendText(&sQuery, "SELECT * FROM ", 0);
17913 appendText(&sQuery, zTab, 0);
17914 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
17916 appendText(&sSql, zSep, 0);
17917 appendText(&sSql, sQuery.z, '\'');
17919 appendText(&sSql, ",", 0);
17920 appendText(&sSql, zTab, '\'');
17923 sqlite3_finalize(pStmt);
17925 zSql = sqlite3_mprintf(
17927 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
17928 " FROM [sha3sum$query]",
17931 zSql = sqlite3_mprintf(
17933 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
17934 " FROM [sha3sum$query]",
17940 utf8_printf(p->out, "%s\n", zSql);
17942 shell_exec(p, zSql, 0);
17944 sqlite3_free(zSql);
17947 #ifndef SQLITE_NOHAVE_SYSTEM
17949 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
17954 raw_printf(stderr, "Usage: .system COMMAND\n");
17956 goto meta_command_exit;
17958 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
17959 for(i=2; i<nArg; i++){
17960 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
17964 sqlite3_free(zCmd);
17965 if( x ) raw_printf(stderr, "System command returns %d\n", x);
17967 #endif /* !defined(SQLITE_NOHAVE_SYSTEM) */
17969 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
17970 static const char *azBool[] = { "off", "on", "trigger", "full"};
17973 raw_printf(stderr, "Usage: .show\n");
17975 goto meta_command_exit;
17977 utf8_printf(p->out, "%12.12s: %s\n","echo",
17978 azBool[ShellHasFlag(p, SHFLG_Echo)]);
17979 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
17980 utf8_printf(p->out, "%12.12s: %s\n","explain",
17981 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
17982 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
17983 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
17984 utf8_printf(p->out, "%12.12s: ", "nullvalue");
17985 output_c_string(p->out, p->nullValue);
17986 raw_printf(p->out, "\n");
17987 utf8_printf(p->out,"%12.12s: %s\n","output",
17988 strlen30(p->outfile) ? p->outfile : "stdout");
17989 utf8_printf(p->out,"%12.12s: ", "colseparator");
17990 output_c_string(p->out, p->colSeparator);
17991 raw_printf(p->out, "\n");
17992 utf8_printf(p->out,"%12.12s: ", "rowseparator");
17993 output_c_string(p->out, p->rowSeparator);
17994 raw_printf(p->out, "\n");
17995 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
17996 utf8_printf(p->out, "%12.12s: ", "width");
17997 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
17998 raw_printf(p->out, "%d ", p->colWidth[i]);
18000 raw_printf(p->out, "\n");
18001 utf8_printf(p->out, "%12.12s: %s\n", "filename",
18002 p->zDbFilename ? p->zDbFilename : "");
18005 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
18007 p->statsOn = (u8)booleanValue(azArg[1]);
18008 }else if( nArg==1 ){
18009 display_stats(p->db, p, 0);
18011 raw_printf(stderr, "Usage: .stats ?on|off?\n");
18016 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
18017 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
18018 || strncmp(azArg[0], "indexes", n)==0) )
18020 sqlite3_stmt *pStmt;
18027 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
18029 sqlite3_finalize(pStmt);
18030 return shellDatabaseError(p->db);
18033 if( nArg>2 && c=='i' ){
18034 /* It is an historical accident that the .indexes command shows an error
18035 ** when called with the wrong number of arguments whereas the .tables
18036 ** command does not. */
18037 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
18039 sqlite3_finalize(pStmt);
18040 goto meta_command_exit;
18042 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
18043 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
18044 if( zDbName==0 ) continue;
18045 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
18046 if( sqlite3_stricmp(zDbName, "main")==0 ){
18047 appendText(&s, "SELECT name FROM ", 0);
18049 appendText(&s, "SELECT ", 0);
18050 appendText(&s, zDbName, '\'');
18051 appendText(&s, "||'.'||name FROM ", 0);
18053 appendText(&s, zDbName, '"');
18054 appendText(&s, ".sqlite_master ", 0);
18056 appendText(&s," WHERE type IN ('table','view')"
18057 " AND name NOT LIKE 'sqlite_%'"
18058 " AND name LIKE ?1", 0);
18060 appendText(&s," WHERE type='index'"
18061 " AND tbl_name LIKE ?1", 0);
18064 rc = sqlite3_finalize(pStmt);
18065 appendText(&s, " ORDER BY 1", 0);
18066 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
18068 if( rc ) return shellDatabaseError(p->db);
18070 /* Run the SQL statement prepared by the above block. Store the results
18071 ** as an array of nul-terminated strings in azResult[]. */
18075 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
18077 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
18079 while( sqlite3_step(pStmt)==SQLITE_ROW ){
18080 if( nRow>=nAlloc ){
18082 int n2 = nAlloc*2 + 10;
18083 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
18084 if( azNew==0 ) shell_out_of_memory();
18088 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
18089 if( 0==azResult[nRow] ) shell_out_of_memory();
18092 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
18093 rc = shellDatabaseError(p->db);
18096 /* Pretty-print the contents of array azResult[] to the output */
18097 if( rc==0 && nRow>0 ){
18098 int len, maxlen = 0;
18100 int nPrintCol, nPrintRow;
18101 for(i=0; i<nRow; i++){
18102 len = strlen30(azResult[i]);
18103 if( len>maxlen ) maxlen = len;
18105 nPrintCol = 80/(maxlen+2);
18106 if( nPrintCol<1 ) nPrintCol = 1;
18107 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
18108 for(i=0; i<nPrintRow; i++){
18109 for(j=i; j<nRow; j+=nPrintRow){
18110 char *zSp = j<nPrintRow ? "" : " ";
18111 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
18112 azResult[j] ? azResult[j]:"");
18114 raw_printf(p->out, "\n");
18118 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
18119 sqlite3_free(azResult);
18122 /* Begin redirecting output to the file "testcase-out.txt" */
18123 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
18125 p->out = output_file_open("testcase-out.txt", 0);
18127 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
18130 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
18132 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
18136 #ifndef SQLITE_UNTESTABLE
18137 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
18138 static const struct {
18139 const char *zCtrlName; /* Name of a test-control option */
18140 int ctrlCode; /* Integer code for that option */
18141 const char *zUsage; /* Usage notes */
18143 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
18144 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
18145 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
18146 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
18147 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
18148 { "extra_schema_checks",SQLITE_TESTCTRL_EXTRA_SCHEMA_CHECKS,"BOOLEAN" },
18149 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" },*/
18150 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
18151 { "internal_functions", SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, "" },
18152 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
18153 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
18154 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
18156 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
18158 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
18159 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
18160 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
18161 { "prng_seed", SQLITE_TESTCTRL_PRNG_SEED, "SEED ?db?" },
18165 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
18168 const char *zCmd = 0;
18171 zCmd = nArg>=2 ? azArg[1] : "help";
18173 /* The argument can optionally begin with "-" or "--" */
18174 if( zCmd[0]=='-' && zCmd[1] ){
18176 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
18179 /* --help lists all test-controls */
18180 if( strcmp(zCmd,"help")==0 ){
18181 utf8_printf(p->out, "Available test-controls:\n");
18182 for(i=0; i<ArraySize(aCtrl); i++){
18183 utf8_printf(p->out, " .testctrl %s %s\n",
18184 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
18187 goto meta_command_exit;
18190 /* convert testctrl text option to value. allow any unique prefix
18191 ** of the option name, or a numerical value. */
18192 n2 = strlen30(zCmd);
18193 for(i=0; i<ArraySize(aCtrl); i++){
18194 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
18196 testctrl = aCtrl[i].ctrlCode;
18199 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
18200 "Use \".testctrl --help\" for help\n", zCmd);
18202 goto meta_command_exit;
18207 utf8_printf(stderr,"Error: unknown test-control: %s\n"
18208 "Use \".testctrl --help\" for help\n", zCmd);
18212 /* sqlite3_test_control(int, db, int) */
18213 case SQLITE_TESTCTRL_OPTIMIZATIONS:
18215 int opt = (int)strtol(azArg[2], 0, 0);
18216 rc2 = sqlite3_test_control(testctrl, p->db, opt);
18221 /* sqlite3_test_control(int) */
18222 case SQLITE_TESTCTRL_PRNG_SAVE:
18223 case SQLITE_TESTCTRL_PRNG_RESTORE:
18224 case SQLITE_TESTCTRL_PRNG_RESET:
18225 case SQLITE_TESTCTRL_BYTEORDER:
18227 rc2 = sqlite3_test_control(testctrl);
18228 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
18232 /* sqlite3_test_control(int, uint) */
18233 case SQLITE_TESTCTRL_PENDING_BYTE:
18235 unsigned int opt = (unsigned int)integerValue(azArg[2]);
18236 rc2 = sqlite3_test_control(testctrl, opt);
18241 /* sqlite3_test_control(int, int, sqlite3*) */
18242 case SQLITE_TESTCTRL_PRNG_SEED:
18243 if( nArg==3 || nArg==4 ){
18244 int ii = (int)integerValue(azArg[2]);
18246 if( ii==0 && strcmp(azArg[2],"random")==0 ){
18247 sqlite3_randomness(sizeof(ii),&ii);
18248 printf("-- random seed: %d\n", ii);
18254 /* Make sure the schema has been loaded */
18255 sqlite3_table_column_metadata(db, 0, "x", 0, 0, 0, 0, 0, 0);
18257 rc2 = sqlite3_test_control(testctrl, ii, db);
18262 /* sqlite3_test_control(int, int) */
18263 case SQLITE_TESTCTRL_ASSERT:
18264 case SQLITE_TESTCTRL_ALWAYS:
18266 int opt = booleanValue(azArg[2]);
18267 rc2 = sqlite3_test_control(testctrl, opt);
18272 /* sqlite3_test_control(int, int) */
18273 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
18274 case SQLITE_TESTCTRL_NEVER_CORRUPT:
18276 int opt = booleanValue(azArg[2]);
18277 rc2 = sqlite3_test_control(testctrl, opt);
18282 /* sqlite3_test_control(sqlite3*) */
18283 case SQLITE_TESTCTRL_INTERNAL_FUNCTIONS:
18284 rc2 = sqlite3_test_control(testctrl, p->db);
18288 case SQLITE_TESTCTRL_IMPOSTER:
18290 rc2 = sqlite3_test_control(testctrl, p->db,
18292 integerValue(azArg[3]),
18293 integerValue(azArg[4]));
18299 case SQLITE_TESTCTRL_PARSER_COVERAGE:
18301 sqlite3_test_control(testctrl, p->out);
18307 if( isOk==0 && iCtrl>=0 ){
18308 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd,aCtrl[iCtrl].zUsage);
18310 }else if( isOk==1 ){
18311 raw_printf(p->out, "%d\n", rc2);
18312 }else if( isOk==2 ){
18313 raw_printf(p->out, "0x%08x\n", rc2);
18316 #endif /* !defined(SQLITE_UNTESTABLE) */
18318 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
18320 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
18323 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
18325 enableTimer = booleanValue(azArg[1]);
18326 if( enableTimer && !HAS_TIMER ){
18327 raw_printf(stderr, "Error: timer not available on this system.\n");
18331 raw_printf(stderr, "Usage: .timer on|off\n");
18336 #ifndef SQLITE_OMIT_TRACE
18337 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
18341 for(jj=1; jj<nArg; jj++){
18342 const char *z = azArg[jj];
18344 if( optionMatch(z, "expanded") ){
18345 p->eTraceType = SHELL_TRACE_EXPANDED;
18347 #ifdef SQLITE_ENABLE_NORMALIZE
18348 else if( optionMatch(z, "normalized") ){
18349 p->eTraceType = SHELL_TRACE_NORMALIZED;
18352 else if( optionMatch(z, "plain") ){
18353 p->eTraceType = SHELL_TRACE_PLAIN;
18355 else if( optionMatch(z, "profile") ){
18356 mType |= SQLITE_TRACE_PROFILE;
18358 else if( optionMatch(z, "row") ){
18359 mType |= SQLITE_TRACE_ROW;
18361 else if( optionMatch(z, "stmt") ){
18362 mType |= SQLITE_TRACE_STMT;
18364 else if( optionMatch(z, "close") ){
18365 mType |= SQLITE_TRACE_CLOSE;
18368 raw_printf(stderr, "Unknown option \"%s\" on \".trace\"\n", z);
18370 goto meta_command_exit;
18373 output_file_close(p->traceOut);
18374 p->traceOut = output_file_open(azArg[1], 0);
18377 if( p->traceOut==0 ){
18378 sqlite3_trace_v2(p->db, 0, 0, 0);
18380 if( mType==0 ) mType = SQLITE_TRACE_STMT;
18381 sqlite3_trace_v2(p->db, mType, sql_trace_callback, p);
18384 #endif /* !defined(SQLITE_OMIT_TRACE) */
18386 #if defined(SQLITE_DEBUG) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18387 if( c=='u' && strncmp(azArg[0], "unmodule", n)==0 ){
18392 raw_printf(stderr, "Usage: .unmodule [--allexcept] NAME ...\n");
18394 goto meta_command_exit;
18398 if( zOpt[0]=='-' && zOpt[1]=='-' && zOpt[2]!=0 ) zOpt++;
18399 lenOpt = (int)strlen(zOpt);
18400 if( lenOpt>=3 && strncmp(zOpt, "-allexcept",lenOpt)==0 ){
18401 assert( azArg[nArg]==0 );
18402 sqlite3_drop_modules(p->db, nArg>2 ? (const char**)(azArg+2) : 0);
18404 for(ii=1; ii<nArg; ii++){
18405 sqlite3_create_module(p->db, azArg[ii], 0, 0);
18411 #if SQLITE_USER_AUTHENTICATION
18412 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
18414 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
18416 goto meta_command_exit;
18419 if( strcmp(azArg[1],"login")==0 ){
18421 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
18423 goto meta_command_exit;
18425 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3],
18426 strlen30(azArg[3]));
18428 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
18431 }else if( strcmp(azArg[1],"add")==0 ){
18433 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
18435 goto meta_command_exit;
18437 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18438 booleanValue(azArg[4]));
18440 raw_printf(stderr, "User-Add failed: %d\n", rc);
18443 }else if( strcmp(azArg[1],"edit")==0 ){
18445 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
18447 goto meta_command_exit;
18449 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
18450 booleanValue(azArg[4]));
18452 raw_printf(stderr, "User-Edit failed: %d\n", rc);
18455 }else if( strcmp(azArg[1],"delete")==0 ){
18457 raw_printf(stderr, "Usage: .user delete USER\n");
18459 goto meta_command_exit;
18461 rc = sqlite3_user_delete(p->db, azArg[2]);
18463 raw_printf(stderr, "User-Delete failed: %d\n", rc);
18467 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
18469 goto meta_command_exit;
18472 #endif /* SQLITE_USER_AUTHENTICATION */
18474 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
18475 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
18476 sqlite3_libversion(), sqlite3_sourceid());
18477 #if SQLITE_HAVE_ZLIB
18478 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
18480 #define CTIMEOPT_VAL_(opt) #opt
18481 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
18482 #if defined(__clang__) && defined(__clang_major__)
18483 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
18484 CTIMEOPT_VAL(__clang_minor__) "."
18485 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
18486 #elif defined(_MSC_VER)
18487 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
18488 #elif defined(__GNUC__) && defined(__VERSION__)
18489 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
18493 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
18494 const char *zDbName = nArg==2 ? azArg[1] : "main";
18495 sqlite3_vfs *pVfs = 0;
18497 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
18499 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
18500 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
18501 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
18502 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18507 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
18509 sqlite3_vfs *pCurrent = 0;
18511 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
18513 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
18514 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
18515 pVfs==pCurrent ? " <--- CURRENT" : "");
18516 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
18517 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
18518 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
18520 raw_printf(p->out, "-----------------------------------\n");
18525 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
18526 const char *zDbName = nArg==2 ? azArg[1] : "main";
18527 char *zVfsName = 0;
18529 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
18531 utf8_printf(p->out, "%s\n", zVfsName);
18532 sqlite3_free(zVfsName);
18537 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
18538 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
18539 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
18543 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
18545 assert( nArg<=ArraySize(azArg) );
18546 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
18547 p->colWidth[j-1] = (int)integerValue(azArg[j]);
18552 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
18553 " \"%s\". Enter \".help\" for help\n", azArg[0]);
18560 if( p->outCount==0 ) output_reset(p);
18566 ** Return TRUE if a semicolon occurs anywhere in the first N characters
18569 static int line_contains_semicolon(const char *z, int N){
18571 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
18576 ** Test to see if a line consists entirely of whitespace.
18578 static int _all_whitespace(const char *z){
18580 if( IsSpace(z[0]) ) continue;
18581 if( *z=='/' && z[1]=='*' ){
18583 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
18584 if( *z==0 ) return 0;
18588 if( *z=='-' && z[1]=='-' ){
18590 while( *z && *z!='\n' ){ z++; }
18591 if( *z==0 ) return 1;
18600 ** Return TRUE if the line typed in is an SQL command terminator other
18601 ** than a semi-colon. The SQL Server style "go" command is understood
18602 ** as is the Oracle "/".
18604 static int line_is_command_terminator(const char *zLine){
18605 while( IsSpace(zLine[0]) ){ zLine++; };
18606 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
18607 return 1; /* Oracle */
18609 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
18610 && _all_whitespace(&zLine[2]) ){
18611 return 1; /* SQL Server */
18617 ** We need a default sqlite3_complete() implementation to use in case
18618 ** the shell is compiled with SQLITE_OMIT_COMPLETE. The default assumes
18619 ** any arbitrary text is a complete SQL statement. This is not very
18620 ** user-friendly, but it does seem to work.
18622 #ifdef SQLITE_OMIT_COMPLETE
18623 #define sqlite3_complete(x) 1
18627 ** Return true if zSql is a complete SQL statement. Return false if it
18628 ** ends in the middle of a string literal or C-style comment.
18630 static int line_is_complete(char *zSql, int nSql){
18632 if( zSql==0 ) return 1;
18635 rc = sqlite3_complete(zSql);
18641 ** Run a single line of SQL. Return the number of errors.
18643 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
18648 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
18649 if( p->flgProgress & SHELL_PROGRESS_RESET ) p->nProgress = 0;
18651 rc = shell_exec(p, zSql, &zErrMsg);
18653 if( rc || zErrMsg ){
18655 if( in!=0 || !stdin_is_interactive ){
18656 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
18657 "Error: near line %d:", startline);
18659 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
18662 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
18663 sqlite3_free(zErrMsg);
18666 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
18669 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
18670 raw_printf(p->out, "changes: %3d total_changes: %d\n",
18671 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
18678 ** Read input from *in and process it. If *in==0 then input
18679 ** is interactive - the user is typing it it. Otherwise, input
18680 ** is coming from a file or device. A prompt is issued and history
18681 ** is saved only if input is interactive. An interrupt signal will
18682 ** cause this routine to exit immediately, unless input is interactive.
18684 ** Return the number of errors.
18686 static int process_input(ShellState *p){
18687 char *zLine = 0; /* A single input line */
18688 char *zSql = 0; /* Accumulated SQL text */
18689 int nLine; /* Length of current line */
18690 int nSql = 0; /* Bytes of zSql[] used */
18691 int nAlloc = 0; /* Allocated zSql[] space */
18692 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
18693 int rc; /* Error code */
18694 int errCnt = 0; /* Number of errors seen */
18695 int startline = 0; /* Line number for start of current input */
18698 while( errCnt==0 || !bail_on_error || (p->in==0 && stdin_is_interactive) ){
18700 zLine = one_input_line(p->in, zLine, nSql>0);
18703 if( p->in==0 && stdin_is_interactive ) printf("\n");
18706 if( seenInterrupt ){
18707 if( p->in!=0 ) break;
18711 if( nSql==0 && _all_whitespace(zLine) ){
18712 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18715 if( zLine && (zLine[0]=='.' || zLine[0]=='#') && nSql==0 ){
18716 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
18717 if( zLine[0]=='.' ){
18718 rc = do_meta_command(zLine, p);
18719 if( rc==2 ){ /* exit requested */
18727 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
18728 memcpy(zLine,";",2);
18730 nLine = strlen30(zLine);
18731 if( nSql+nLine+2>=nAlloc ){
18732 nAlloc = nSql+nLine+100;
18733 zSql = realloc(zSql, nAlloc);
18734 if( zSql==0 ) shell_out_of_memory();
18739 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
18740 assert( nAlloc>0 && zSql!=0 );
18741 memcpy(zSql, zLine+i, nLine+1-i);
18742 startline = p->lineno;
18745 zSql[nSql++] = '\n';
18746 memcpy(zSql+nSql, zLine, nLine+1);
18749 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
18750 && sqlite3_complete(zSql) ){
18751 errCnt += runOneSqlLine(p, zSql, p->in, startline);
18759 }else if( nSql && _all_whitespace(zSql) ){
18760 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
18764 if( nSql && !_all_whitespace(zSql) ){
18765 errCnt += runOneSqlLine(p, zSql, p->in, startline);
18773 ** Return a pathname which is the user's home directory. A
18774 ** 0 return indicates an error of some kind.
18776 static char *find_home_dir(int clearFlag){
18777 static char *home_dir = NULL;
18783 if( home_dir ) return home_dir;
18785 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
18786 && !defined(__RTP__) && !defined(_WRS_KERNEL)
18788 struct passwd *pwent;
18789 uid_t uid = getuid();
18790 if( (pwent=getpwuid(uid)) != NULL) {
18791 home_dir = pwent->pw_dir;
18796 #if defined(_WIN32_WCE)
18797 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
18802 #if defined(_WIN32) || defined(WIN32)
18804 home_dir = getenv("USERPROFILE");
18809 home_dir = getenv("HOME");
18812 #if defined(_WIN32) || defined(WIN32)
18814 char *zDrive, *zPath;
18816 zDrive = getenv("HOMEDRIVE");
18817 zPath = getenv("HOMEPATH");
18818 if( zDrive && zPath ){
18819 n = strlen30(zDrive) + strlen30(zPath) + 1;
18820 home_dir = malloc( n );
18821 if( home_dir==0 ) return 0;
18822 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
18829 #endif /* !_WIN32_WCE */
18832 int n = strlen30(home_dir) + 1;
18833 char *z = malloc( n );
18834 if( z ) memcpy(z, home_dir, n);
18842 ** Read input from the file given by sqliterc_override. Or if that
18843 ** parameter is NULL, take input from ~/.sqliterc
18845 ** Returns the number of errors.
18847 static void process_sqliterc(
18848 ShellState *p, /* Configuration data */
18849 const char *sqliterc_override /* Name of config file. NULL to use default */
18851 char *home_dir = NULL;
18852 const char *sqliterc = sqliterc_override;
18854 FILE *inSaved = p->in;
18855 int savedLineno = p->lineno;
18857 if (sqliterc == NULL) {
18858 home_dir = find_home_dir(0);
18860 raw_printf(stderr, "-- warning: cannot find home directory;"
18861 " cannot read ~/.sqliterc\n");
18864 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
18867 p->in = fopen(sqliterc,"rb");
18869 if( stdin_is_interactive ){
18870 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
18876 p->lineno = savedLineno;
18877 sqlite3_free(zBuf);
18881 ** Show available command line options
18883 static const char zOptions[] =
18884 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
18885 " -A ARGS... run \".archive ARGS\" and exit\n"
18887 " -append append the database to the end of the file\n"
18888 " -ascii set output mode to 'ascii'\n"
18889 " -bail stop after hitting an error\n"
18890 " -batch force batch I/O\n"
18891 " -column set output mode to 'column'\n"
18892 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
18893 " -csv set output mode to 'csv'\n"
18894 #if defined(SQLITE_ENABLE_DESERIALIZE)
18895 " -deserialize open the database using sqlite3_deserialize()\n"
18897 " -echo print commands before execution\n"
18898 " -init FILENAME read/process named file\n"
18899 " -[no]header turn headers on or off\n"
18900 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
18901 " -heap SIZE Size of heap for memsys3 or memsys5\n"
18903 " -help show this message\n"
18904 " -html set output mode to HTML\n"
18905 " -interactive force interactive I/O\n"
18906 " -line set output mode to 'line'\n"
18907 " -list set output mode to 'list'\n"
18908 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
18909 #if defined(SQLITE_ENABLE_DESERIALIZE)
18910 " -maxsize N maximum size for a --deserialize database\n"
18912 " -memtrace trace all memory allocations and deallocations\n"
18913 " -mmap N default mmap size set to N\n"
18914 #ifdef SQLITE_ENABLE_MULTIPLEX
18915 " -multiplex enable the multiplexor VFS\n"
18917 " -newline SEP set output row separator. Default: '\\n'\n"
18918 " -nofollow refuse to open symbolic links to database files\n"
18919 " -nullvalue TEXT set text string for NULL values. Default ''\n"
18920 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
18921 " -quote set output mode to 'quote'\n"
18922 " -readonly open the database read-only\n"
18923 " -separator SEP set output column separator. Default: '|'\n"
18924 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
18925 " -sorterref SIZE sorter references threshold size\n"
18927 " -stats print memory stats before each finalize\n"
18928 " -version show SQLite version\n"
18929 " -vfs NAME use NAME as the default VFS\n"
18930 #ifdef SQLITE_ENABLE_VFSTRACE
18931 " -vfstrace enable tracing of all VFS calls\n"
18933 #ifdef SQLITE_HAVE_ZLIB
18934 " -zip open the file as a ZIP Archive\n"
18937 static void usage(int showDetail){
18938 utf8_printf(stderr,
18939 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
18940 "FILENAME is the name of an SQLite database. A new database is created\n"
18941 "if the file does not previously exist.\n", Argv0);
18943 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
18945 raw_printf(stderr, "Use the -help option for additional information\n");
18951 ** Internal check: Verify that the SQLite is uninitialized. Print a
18952 ** error message if it is initialized.
18954 static void verify_uninitialized(void){
18955 if( sqlite3_config(-1)==SQLITE_MISUSE ){
18956 utf8_printf(stdout, "WARNING: attempt to configure SQLite after"
18957 " initialization.\n");
18962 ** Initialize the state information in data
18964 static void main_init(ShellState *data) {
18965 memset(data, 0, sizeof(*data));
18966 data->normalMode = data->cMode = data->mode = MODE_List;
18967 data->autoExplain = 1;
18968 memcpy(data->colSeparator,SEP_Column, 2);
18969 memcpy(data->rowSeparator,SEP_Row, 2);
18970 data->showHeader = 0;
18971 data->shellFlgs = SHFLG_Lookaside;
18972 verify_uninitialized();
18973 sqlite3_config(SQLITE_CONFIG_URI, 1);
18974 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
18975 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
18976 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
18977 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
18981 ** Output text to the console in a font that attracts extra attention.
18984 static void printBold(const char *zText){
18985 #if !SQLITE_OS_WINRT
18986 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
18987 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
18988 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
18989 SetConsoleTextAttribute(out,
18990 FOREGROUND_RED|FOREGROUND_INTENSITY
18993 printf("%s", zText);
18994 #if !SQLITE_OS_WINRT
18995 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
18999 static void printBold(const char *zText){
19000 printf("\033[1m%s\033[0m", zText);
19005 ** Get the argument to an --option. Throw an error and die if no argument
19008 static char *cmdline_option_value(int argc, char **argv, int i){
19010 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
19011 argv[0], argv[argc-1]);
19017 #ifndef SQLITE_SHELL_IS_UTF8
19018 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
19019 # define SQLITE_SHELL_IS_UTF8 (0)
19021 # define SQLITE_SHELL_IS_UTF8 (1)
19025 #if SQLITE_SHELL_IS_UTF8
19026 int SQLITE_CDECL main(int argc, char **argv){
19028 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
19033 const char *zInitFile = 0;
19036 int warnInmemoryDb = 0;
19040 const char *zVfs = 0; /* Value of -vfs command-line option */
19041 #if !SQLITE_SHELL_IS_UTF8
19042 char **argvToFree = 0;
19043 int argcToFree = 0;
19046 setBinaryMode(stdin, 0);
19047 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
19048 stdin_is_interactive = isatty(0);
19049 stdout_is_console = isatty(1);
19051 #ifdef SQLITE_DEBUG
19052 registerOomSimulator();
19055 #if !defined(_WIN32_WCE)
19056 if( getenv("SQLITE_DEBUG_BREAK") ){
19057 if( isatty(0) && isatty(2) ){
19059 "attach debugger to process %d and press any key to continue.\n",
19063 #if defined(_WIN32) || defined(WIN32)
19064 #if SQLITE_OS_WINRT
19069 #elif defined(SIGTRAP)
19076 #if USE_SYSTEM_SQLITE+0!=1
19077 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
19078 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
19079 sqlite3_sourceid(), SQLITE_SOURCE_ID);
19085 /* On Windows, we must translate command-line arguments into UTF-8.
19086 ** The SQLite memory allocator subsystem has to be enabled in order to
19087 ** do this. But we want to run an sqlite3_shutdown() afterwards so that
19088 ** subsequent sqlite3_config() calls will work. So copy all results into
19089 ** memory that does not come from the SQLite memory allocator.
19091 #if !SQLITE_SHELL_IS_UTF8
19092 sqlite3_initialize();
19093 argvToFree = malloc(sizeof(argv[0])*argc*2);
19095 argv = argvToFree + argc;
19096 if( argv==0 ) shell_out_of_memory();
19097 for(i=0; i<argc; i++){
19098 char *z = sqlite3_win32_unicode_to_utf8(wargv[i]);
19100 if( z==0 ) shell_out_of_memory();
19101 n = (int)strlen(z);
19102 argv[i] = malloc( n+1 );
19103 if( argv[i]==0 ) shell_out_of_memory();
19104 memcpy(argv[i], z, n+1);
19105 argvToFree[i] = argv[i];
19108 sqlite3_shutdown();
19111 assert( argc>=1 && argv && argv[0] );
19114 /* Make sure we have a valid signal handler early, before anything
19118 signal(SIGINT, interrupt_handler);
19119 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
19120 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
19123 #ifdef SQLITE_SHELL_DBNAME_PROC
19125 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
19126 ** of a C-function that will provide the name of the database file. Use
19127 ** this compile-time option to embed this shell program in larger
19128 ** applications. */
19129 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
19130 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
19131 warnInmemoryDb = 0;
19135 /* Do an initial pass through the command-line argument to locate
19136 ** the name of the database file, the name of the initialization file,
19137 ** the size of the alternative malloc heap,
19138 ** and the first command to execute.
19140 verify_uninitialized();
19141 for(i=1; i<argc; i++){
19145 if( data.zDbFilename==0 ){
19146 data.zDbFilename = z;
19148 /* Excesss arguments are interpreted as SQL (or dot-commands) and
19149 ** mean that nothing is read from stdin */
19152 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
19153 if( azCmd==0 ) shell_out_of_memory();
19157 if( z[1]=='-' ) z++;
19158 if( strcmp(z,"-separator")==0
19159 || strcmp(z,"-nullvalue")==0
19160 || strcmp(z,"-newline")==0
19161 || strcmp(z,"-cmd")==0
19163 (void)cmdline_option_value(argc, argv, ++i);
19164 }else if( strcmp(z,"-init")==0 ){
19165 zInitFile = cmdline_option_value(argc, argv, ++i);
19166 }else if( strcmp(z,"-batch")==0 ){
19167 /* Need to check for batch mode here to so we can avoid printing
19168 ** informational messages (like from process_sqliterc) before
19169 ** we do the actual processing of arguments later in a second pass.
19171 stdin_is_interactive = 0;
19172 }else if( strcmp(z,"-heap")==0 ){
19173 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
19175 sqlite3_int64 szHeap;
19177 zSize = cmdline_option_value(argc, argv, ++i);
19178 szHeap = integerValue(zSize);
19179 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
19180 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
19182 (void)cmdline_option_value(argc, argv, ++i);
19184 }else if( strcmp(z,"-pagecache")==0 ){
19186 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
19187 if( sz>70000 ) sz = 70000;
19189 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
19190 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
19191 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
19192 data.shellFlgs |= SHFLG_Pagecache;
19193 }else if( strcmp(z,"-lookaside")==0 ){
19195 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
19197 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
19199 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
19200 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
19201 #ifdef SQLITE_ENABLE_VFSTRACE
19202 }else if( strcmp(z,"-vfstrace")==0 ){
19203 extern int vfstrace_register(
19204 const char *zTraceName,
19205 const char *zOldVfsName,
19206 int (*xOut)(const char*,void*),
19210 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
19212 #ifdef SQLITE_ENABLE_MULTIPLEX
19213 }else if( strcmp(z,"-multiplex")==0 ){
19214 extern int sqlite3_multiple_initialize(const char*,int);
19215 sqlite3_multiplex_initialize(0, 1);
19217 }else if( strcmp(z,"-mmap")==0 ){
19218 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
19219 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
19220 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
19221 }else if( strcmp(z,"-sorterref")==0 ){
19222 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
19223 sqlite3_config(SQLITE_CONFIG_SORTERREF_SIZE, (int)sz);
19225 }else if( strcmp(z,"-vfs")==0 ){
19226 zVfs = cmdline_option_value(argc, argv, ++i);
19227 #ifdef SQLITE_HAVE_ZLIB
19228 }else if( strcmp(z,"-zip")==0 ){
19229 data.openMode = SHELL_OPEN_ZIPFILE;
19231 }else if( strcmp(z,"-append")==0 ){
19232 data.openMode = SHELL_OPEN_APPENDVFS;
19233 #ifdef SQLITE_ENABLE_DESERIALIZE
19234 }else if( strcmp(z,"-deserialize")==0 ){
19235 data.openMode = SHELL_OPEN_DESERIALIZE;
19236 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
19237 data.szMax = integerValue(argv[++i]);
19239 }else if( strcmp(z,"-readonly")==0 ){
19240 data.openMode = SHELL_OPEN_READONLY;
19241 }else if( strcmp(z,"-nofollow")==0 ){
19242 data.openFlags = SQLITE_OPEN_NOFOLLOW;
19243 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19244 }else if( strncmp(z, "-A",2)==0 ){
19245 /* All remaining command-line arguments are passed to the ".archive"
19246 ** command, so ignore them */
19249 }else if( strcmp(z, "-memtrace")==0 ){
19250 sqlite3MemTraceActivate(stderr);
19253 verify_uninitialized();
19256 #ifdef SQLITE_SHELL_INIT_PROC
19258 /* If the SQLITE_SHELL_INIT_PROC macro is defined, then it is the name
19259 ** of a C-function that will perform initialization actions on SQLite that
19260 ** occur just before or after sqlite3_initialize(). Use this compile-time
19261 ** option to embed this shell program in larger applications. */
19262 extern void SQLITE_SHELL_INIT_PROC(void);
19263 SQLITE_SHELL_INIT_PROC();
19266 /* All the sqlite3_config() calls have now been made. So it is safe
19267 ** to call sqlite3_initialize() and process any command line -vfs option. */
19268 sqlite3_initialize();
19272 sqlite3_vfs *pVfs = sqlite3_vfs_find(zVfs);
19274 sqlite3_vfs_register(pVfs, 1);
19276 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
19281 if( data.zDbFilename==0 ){
19282 #ifndef SQLITE_OMIT_MEMORYDB
19283 data.zDbFilename = ":memory:";
19284 warnInmemoryDb = argc==1;
19286 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
19291 sqlite3_appendvfs_init(0,0,0);
19293 /* Go ahead and open the database file if it already exists. If the
19294 ** file does not exist, delay opening it. This prevents empty database
19295 ** files from being created if a user mistypes the database name argument
19296 ** to the sqlite command-line tool.
19298 if( access(data.zDbFilename, 0)==0 ){
19302 /* Process the initialization file if there is one. If no -init option
19303 ** is given on the command line, look for a file named ~/.sqliterc and
19304 ** try to process it.
19306 process_sqliterc(&data,zInitFile);
19308 /* Make a second pass through the command-line argument and set
19309 ** options. This second pass is delayed until after the initialization
19310 ** file is processed so that the command-line arguments will override
19311 ** settings in the initialization file.
19313 for(i=1; i<argc; i++){
19315 if( z[0]!='-' ) continue;
19316 if( z[1]=='-' ){ z++; }
19317 if( strcmp(z,"-init")==0 ){
19319 }else if( strcmp(z,"-html")==0 ){
19320 data.mode = MODE_Html;
19321 }else if( strcmp(z,"-list")==0 ){
19322 data.mode = MODE_List;
19323 }else if( strcmp(z,"-quote")==0 ){
19324 data.mode = MODE_Quote;
19325 }else if( strcmp(z,"-line")==0 ){
19326 data.mode = MODE_Line;
19327 }else if( strcmp(z,"-column")==0 ){
19328 data.mode = MODE_Column;
19329 }else if( strcmp(z,"-csv")==0 ){
19330 data.mode = MODE_Csv;
19331 memcpy(data.colSeparator,",",2);
19332 #ifdef SQLITE_HAVE_ZLIB
19333 }else if( strcmp(z,"-zip")==0 ){
19334 data.openMode = SHELL_OPEN_ZIPFILE;
19336 }else if( strcmp(z,"-append")==0 ){
19337 data.openMode = SHELL_OPEN_APPENDVFS;
19338 #ifdef SQLITE_ENABLE_DESERIALIZE
19339 }else if( strcmp(z,"-deserialize")==0 ){
19340 data.openMode = SHELL_OPEN_DESERIALIZE;
19341 }else if( strcmp(z,"-maxsize")==0 && i+1<argc ){
19342 data.szMax = integerValue(argv[++i]);
19344 }else if( strcmp(z,"-readonly")==0 ){
19345 data.openMode = SHELL_OPEN_READONLY;
19346 }else if( strcmp(z,"-nofollow")==0 ){
19347 data.openFlags |= SQLITE_OPEN_NOFOLLOW;
19348 }else if( strcmp(z,"-ascii")==0 ){
19349 data.mode = MODE_Ascii;
19350 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
19352 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
19354 }else if( strcmp(z,"-separator")==0 ){
19355 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
19356 "%s",cmdline_option_value(argc,argv,++i));
19357 }else if( strcmp(z,"-newline")==0 ){
19358 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
19359 "%s",cmdline_option_value(argc,argv,++i));
19360 }else if( strcmp(z,"-nullvalue")==0 ){
19361 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
19362 "%s",cmdline_option_value(argc,argv,++i));
19363 }else if( strcmp(z,"-header")==0 ){
19364 data.showHeader = 1;
19365 }else if( strcmp(z,"-noheader")==0 ){
19366 data.showHeader = 0;
19367 }else if( strcmp(z,"-echo")==0 ){
19368 ShellSetFlag(&data, SHFLG_Echo);
19369 }else if( strcmp(z,"-eqp")==0 ){
19370 data.autoEQP = AUTOEQP_on;
19371 }else if( strcmp(z,"-eqpfull")==0 ){
19372 data.autoEQP = AUTOEQP_full;
19373 }else if( strcmp(z,"-stats")==0 ){
19375 }else if( strcmp(z,"-scanstats")==0 ){
19376 data.scanstatsOn = 1;
19377 }else if( strcmp(z,"-backslash")==0 ){
19378 /* Undocumented command-line option: -backslash
19379 ** Causes C-style backslash escapes to be evaluated in SQL statements
19380 ** prior to sending the SQL into SQLite. Useful for injecting
19381 ** crazy bytes in the middle of SQL statements for testing and debugging.
19383 ShellSetFlag(&data, SHFLG_Backslash);
19384 }else if( strcmp(z,"-bail")==0 ){
19386 }else if( strcmp(z,"-version")==0 ){
19387 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
19389 }else if( strcmp(z,"-interactive")==0 ){
19390 stdin_is_interactive = 1;
19391 }else if( strcmp(z,"-batch")==0 ){
19392 stdin_is_interactive = 0;
19393 }else if( strcmp(z,"-heap")==0 ){
19395 }else if( strcmp(z,"-pagecache")==0 ){
19397 }else if( strcmp(z,"-lookaside")==0 ){
19399 }else if( strcmp(z,"-mmap")==0 ){
19401 }else if( strcmp(z,"-memtrace")==0 ){
19403 #ifdef SQLITE_ENABLE_SORTER_REFERENCES
19404 }else if( strcmp(z,"-sorterref")==0 ){
19407 }else if( strcmp(z,"-vfs")==0 ){
19409 #ifdef SQLITE_ENABLE_VFSTRACE
19410 }else if( strcmp(z,"-vfstrace")==0 ){
19413 #ifdef SQLITE_ENABLE_MULTIPLEX
19414 }else if( strcmp(z,"-multiplex")==0 ){
19417 }else if( strcmp(z,"-help")==0 ){
19419 }else if( strcmp(z,"-cmd")==0 ){
19420 /* Run commands that follow -cmd first and separately from commands
19421 ** that simply appear on the command-line. This seems goofy. It would
19422 ** be better if all commands ran in the order that they appear. But
19423 ** we retain the goofy behavior for historical compatibility. */
19424 if( i==argc-1 ) break;
19425 z = cmdline_option_value(argc,argv,++i);
19427 rc = do_meta_command(z, &data);
19428 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
19431 rc = shell_exec(&data, z, &zErrMsg);
19433 utf8_printf(stderr,"Error: %s\n", zErrMsg);
19434 if( bail_on_error ) return rc!=0 ? rc : 1;
19436 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
19437 if( bail_on_error ) return rc;
19440 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
19441 }else if( strncmp(z, "-A", 2)==0 ){
19443 utf8_printf(stderr, "Error: cannot mix regular SQL or dot-commands"
19444 " with \"%s\"\n", z);
19447 open_db(&data, OPEN_DB_ZIPFILE);
19450 arDotCommand(&data, 1, argv+(i-1), argc-(i-1));
19452 arDotCommand(&data, 1, argv+i, argc-i);
19458 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
19459 raw_printf(stderr,"Use -help for a list of options.\n");
19462 data.cMode = data.mode;
19466 /* Run all arguments that do not begin with '-' as if they were separate
19467 ** command-line inputs, except for the argToSkip argument which contains
19468 ** the database filename.
19470 for(i=0; i<nCmd; i++){
19471 if( azCmd[i][0]=='.' ){
19472 rc = do_meta_command(azCmd[i], &data);
19473 if( rc ) return rc==2 ? 0 : rc;
19476 rc = shell_exec(&data, azCmd[i], &zErrMsg);
19478 utf8_printf(stderr,"Error: %s\n", zErrMsg);
19479 return rc!=0 ? rc : 1;
19481 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
19488 /* Run commands received from standard input
19490 if( stdin_is_interactive ){
19495 "SQLite version %s %.19s\n" /*extra-version-info*/
19496 "Enter \".help\" for usage hints.\n",
19497 sqlite3_libversion(), sqlite3_sourceid()
19499 if( warnInmemoryDb ){
19500 printf("Connected to a ");
19501 printBold("transient in-memory database");
19502 printf(".\nUse \".open FILENAME\" to reopen on a "
19503 "persistent database.\n");
19505 zHistory = getenv("SQLITE_HISTORY");
19507 zHistory = strdup(zHistory);
19508 }else if( (zHome = find_home_dir(0))!=0 ){
19509 nHistory = strlen30(zHome) + 20;
19510 if( (zHistory = malloc(nHistory))!=0 ){
19511 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
19514 if( zHistory ){ shell_read_history(zHistory); }
19515 #if HAVE_READLINE || HAVE_EDITLINE
19516 rl_attempted_completion_function = readline_completion;
19517 #elif HAVE_LINENOISE
19518 linenoiseSetCompletionCallback(linenoise_completion);
19521 rc = process_input(&data);
19523 shell_stifle_history(2000);
19524 shell_write_history(zHistory);
19529 rc = process_input(&data);
19532 set_table_name(&data, 0);
19534 session_close_all(&data);
19537 sqlite3_free(data.zFreeOnClose);
19539 output_reset(&data);
19540 data.doXdgOpen = 0;
19541 clearTempFile(&data);
19542 #if !SQLITE_SHELL_IS_UTF8
19543 for(i=0; i<argcToFree; i++) free(argvToFree[i]);
19546 /* Clear the global data structure so that valgrind will detect memory
19548 memset(&data, 0, sizeof(data));