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 ** Warning pragmas copied from msvc.h in the core.
42 #pragma warning(disable : 4054)
43 #pragma warning(disable : 4055)
44 #pragma warning(disable : 4100)
45 #pragma warning(disable : 4127)
46 #pragma warning(disable : 4130)
47 #pragma warning(disable : 4152)
48 #pragma warning(disable : 4189)
49 #pragma warning(disable : 4206)
50 #pragma warning(disable : 4210)
51 #pragma warning(disable : 4232)
52 #pragma warning(disable : 4244)
53 #pragma warning(disable : 4305)
54 #pragma warning(disable : 4306)
55 #pragma warning(disable : 4702)
56 #pragma warning(disable : 4706)
57 #endif /* defined(_MSC_VER) */
60 ** No support for loadable extensions in VxWorks.
62 #if (defined(__RTP__) || defined(_WRS_KERNEL)) && !SQLITE_OMIT_LOAD_EXTENSION
63 # define SQLITE_OMIT_LOAD_EXTENSION 1
67 ** Enable large-file support for fopen() and friends on unix.
69 #ifndef SQLITE_DISABLE_LFS
70 # define _LARGE_FILE 1
71 # ifndef _FILE_OFFSET_BITS
72 # define _FILE_OFFSET_BITS 64
74 # define _LARGEFILE_SOURCE 1
82 typedef sqlite3_int64 i64;
83 typedef sqlite3_uint64 u64;
84 typedef unsigned char u8;
85 #if SQLITE_USER_AUTHENTICATION
86 # include "sqlite3userauth.h"
91 #if !defined(_WIN32) && !defined(WIN32)
93 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
97 #if (!defined(_WIN32) && !defined(WIN32)) || defined(__MINGW32__)
100 # if defined(__MINGW32__)
101 # define DIRENT dirent
103 # define S_ISLNK(mode) (0)
107 #include <sys/types.h>
108 #include <sys/stat.h>
111 # include <readline/readline.h>
112 # include <readline/history.h>
116 # include <editline/readline.h>
119 #if HAVE_EDITLINE || HAVE_READLINE
121 # define shell_add_history(X) add_history(X)
122 # define shell_read_history(X) read_history(X)
123 # define shell_write_history(X) write_history(X)
124 # define shell_stifle_history(X) stifle_history(X)
125 # define shell_readline(X) readline(X)
129 # include "linenoise.h"
130 # define shell_add_history(X) linenoiseHistoryAdd(X)
131 # define shell_read_history(X) linenoiseHistoryLoad(X)
132 # define shell_write_history(X) linenoiseHistorySave(X)
133 # define shell_stifle_history(X) linenoiseHistorySetMaxLen(X)
134 # define shell_readline(X) linenoise(X)
138 # define shell_read_history(X)
139 # define shell_write_history(X)
140 # define shell_stifle_history(X)
142 # define SHELL_USE_LOCAL_GETLINE 1
146 #if defined(_WIN32) || defined(WIN32)
149 # define isatty(h) _isatty(h)
151 # define access(f,m) _access((f),(m))
154 # define popen _popen
156 # define pclose _pclose
158 /* Make sure isatty() has a prototype. */
159 extern int isatty(int);
161 # if !defined(__RTP__) && !defined(_WRS_KERNEL)
162 /* popen and pclose are not C89 functions and so are
163 ** sometimes omitted from the <stdio.h> header */
164 extern FILE *popen(const char*,const char*);
165 extern int pclose(FILE*);
167 # define SQLITE_OMIT_POPEN 1
171 #if defined(_WIN32_WCE)
172 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
173 * thus we always assume that we have a console. That can be
174 * overridden with the -batch command line option.
179 /* ctype macros that work with signed characters */
180 #define IsSpace(X) isspace((unsigned char)X)
181 #define IsDigit(X) isdigit((unsigned char)X)
182 #define ToLower(X) (char)tolower((unsigned char)X)
184 #if defined(_WIN32) || defined(WIN32)
187 /* string conversion routines only needed on Win32 */
188 extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
189 extern char *sqlite3_win32_mbcs_to_utf8_v2(const char *, int);
190 extern char *sqlite3_win32_utf8_to_mbcs_v2(const char *, int);
191 extern LPWSTR sqlite3_win32_utf8_to_unicode(const char *zText);
194 /* On Windows, we normally run with output mode of TEXT so that \n characters
195 ** are automatically translated into \r\n. However, this behavior needs
196 ** to be disabled in some cases (ex: when generating CSV output and when
197 ** rendering quoted strings that contain \n characters). The following
198 ** routines take care of that.
200 #if defined(_WIN32) || defined(WIN32)
201 static void setBinaryMode(FILE *file, int isOutput){
202 if( isOutput ) fflush(file);
203 _setmode(_fileno(file), _O_BINARY);
205 static void setTextMode(FILE *file, int isOutput){
206 if( isOutput ) fflush(file);
207 _setmode(_fileno(file), _O_TEXT);
210 # define setBinaryMode(X,Y)
211 # define setTextMode(X,Y)
215 /* True if the timer is enabled */
216 static int enableTimer = 0;
218 /* Return the current wall-clock time */
219 static sqlite3_int64 timeOfDay(void){
220 static sqlite3_vfs *clockVfs = 0;
222 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
223 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
224 clockVfs->xCurrentTimeInt64(clockVfs, &t);
227 clockVfs->xCurrentTime(clockVfs, &r);
228 t = (sqlite3_int64)(r*86400000.0);
233 #if !defined(_WIN32) && !defined(WIN32) && !defined(__minux)
234 #include <sys/time.h>
235 #include <sys/resource.h>
237 /* VxWorks does not support getrusage() as far as we can determine */
238 #if defined(_WRS_KERNEL) || defined(__RTP__)
240 struct timeval ru_utime; /* user CPU time used */
241 struct timeval ru_stime; /* system CPU time used */
243 #define getrusage(A,B) memset(B,0,sizeof(*B))
246 /* Saved resource information for the beginning of an operation */
247 static struct rusage sBegin; /* CPU time at start */
248 static sqlite3_int64 iBegin; /* Wall-clock time at start */
251 ** Begin timing an operation
253 static void beginTimer(void){
255 getrusage(RUSAGE_SELF, &sBegin);
256 iBegin = timeOfDay();
260 /* Return the difference of two time_structs in seconds */
261 static double timeDiff(struct timeval *pStart, struct timeval *pEnd){
262 return (pEnd->tv_usec - pStart->tv_usec)*0.000001 +
263 (double)(pEnd->tv_sec - pStart->tv_sec);
267 ** Print the timing results.
269 static void endTimer(void){
271 sqlite3_int64 iEnd = timeOfDay();
273 getrusage(RUSAGE_SELF, &sEnd);
274 printf("Run Time: real %.3f user %f sys %f\n",
275 (iEnd - iBegin)*0.001,
276 timeDiff(&sBegin.ru_utime, &sEnd.ru_utime),
277 timeDiff(&sBegin.ru_stime, &sEnd.ru_stime));
281 #define BEGIN_TIMER beginTimer()
282 #define END_TIMER endTimer()
285 #elif (defined(_WIN32) || defined(WIN32))
287 /* Saved resource information for the beginning of an operation */
288 static HANDLE hProcess;
289 static FILETIME ftKernelBegin;
290 static FILETIME ftUserBegin;
291 static sqlite3_int64 ftWallBegin;
292 typedef BOOL (WINAPI *GETPROCTIMES)(HANDLE, LPFILETIME, LPFILETIME,
293 LPFILETIME, LPFILETIME);
294 static GETPROCTIMES getProcessTimesAddr = NULL;
297 ** Check to see if we have timer support. Return 1 if necessary
298 ** support found (or found previously).
300 static int hasTimer(void){
301 if( getProcessTimesAddr ){
304 /* GetProcessTimes() isn't supported in WIN95 and some other Windows
305 ** versions. See if the version we are running on has it, and if it
306 ** does, save off a pointer to it and the current process handle.
308 hProcess = GetCurrentProcess();
310 HINSTANCE hinstLib = LoadLibrary(TEXT("Kernel32.dll"));
311 if( NULL != hinstLib ){
312 getProcessTimesAddr =
313 (GETPROCTIMES) GetProcAddress(hinstLib, "GetProcessTimes");
314 if( NULL != getProcessTimesAddr ){
317 FreeLibrary(hinstLib);
325 ** Begin timing an operation
327 static void beginTimer(void){
328 if( enableTimer && getProcessTimesAddr ){
329 FILETIME ftCreation, ftExit;
330 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,
331 &ftKernelBegin,&ftUserBegin);
332 ftWallBegin = timeOfDay();
336 /* Return the difference of two FILETIME structs in seconds */
337 static double timeDiff(FILETIME *pStart, FILETIME *pEnd){
338 sqlite_int64 i64Start = *((sqlite_int64 *) pStart);
339 sqlite_int64 i64End = *((sqlite_int64 *) pEnd);
340 return (double) ((i64End - i64Start) / 10000000.0);
344 ** Print the timing results.
346 static void endTimer(void){
347 if( enableTimer && getProcessTimesAddr){
348 FILETIME ftCreation, ftExit, ftKernelEnd, ftUserEnd;
349 sqlite3_int64 ftWallEnd = timeOfDay();
350 getProcessTimesAddr(hProcess,&ftCreation,&ftExit,&ftKernelEnd,&ftUserEnd);
351 printf("Run Time: real %.3f user %f sys %f\n",
352 (ftWallEnd - ftWallBegin)*0.001,
353 timeDiff(&ftUserBegin, &ftUserEnd),
354 timeDiff(&ftKernelBegin, &ftKernelEnd));
358 #define BEGIN_TIMER beginTimer()
359 #define END_TIMER endTimer()
360 #define HAS_TIMER hasTimer()
369 ** Used to prevent warnings about unused parameters
371 #define UNUSED_PARAMETER(x) (void)(x)
374 ** Number of elements in an array
376 #define ArraySize(X) (int)(sizeof(X)/sizeof(X[0]))
379 ** If the following flag is set, then command execution stops
380 ** at an error if we are not interactive.
382 static int bail_on_error = 0;
385 ** Threat stdin as an interactive input if the following variable
386 ** is true. Otherwise, assume stdin is connected to a file or pipe.
388 static int stdin_is_interactive = 1;
391 ** On Windows systems we have to know if standard output is a console
392 ** in order to translate UTF-8 into MBCS. The following variable is
393 ** true if translation is required.
395 static int stdout_is_console = 1;
398 ** The following is the open SQLite database. We make a pointer
399 ** to this database a static variable so that it can be accessed
400 ** by the SIGINT handler to interrupt database processing.
402 static sqlite3 *globalDb = 0;
405 ** True if an interrupt (Control-C) has been received.
407 static volatile int seenInterrupt = 0;
410 ** This is the name of our program. It is set in main(), used
411 ** in a number of other places, mostly for error messages.
416 ** Prompt strings. Initialized in main. Settable with
417 ** .prompt main continue
419 static char mainPrompt[20]; /* First line prompt. default: "sqlite> "*/
420 static char continuePrompt[20]; /* Continuation prompt. default: " ...> " */
423 ** Render output like fprintf(). Except, if the output is going to the
424 ** console and if this is running on a Windows machine, translate the
425 ** output from UTF-8 into MBCS.
427 #if defined(_WIN32) || defined(WIN32)
428 void utf8_printf(FILE *out, const char *zFormat, ...){
430 va_start(ap, zFormat);
431 if( stdout_is_console && (out==stdout || out==stderr) ){
432 char *z1 = sqlite3_vmprintf(zFormat, ap);
433 char *z2 = sqlite3_win32_utf8_to_mbcs_v2(z1, 0);
438 vfprintf(out, zFormat, ap);
442 #elif !defined(utf8_printf)
443 # define utf8_printf fprintf
447 ** Render output like fprintf(). This should not be used on anything that
448 ** includes string formatting (e.g. "%s").
450 #if !defined(raw_printf)
451 # define raw_printf fprintf
455 ** Write I/O traces to the following stream.
457 #ifdef SQLITE_ENABLE_IOTRACE
458 static FILE *iotrace = 0;
462 ** This routine works like printf in that its first argument is a
463 ** format string and subsequent arguments are values to be substituted
464 ** in place of % fields. The result of formatting this string
465 ** is written to iotrace.
467 #ifdef SQLITE_ENABLE_IOTRACE
468 static void SQLITE_CDECL iotracePrintf(const char *zFormat, ...){
471 if( iotrace==0 ) return;
472 va_start(ap, zFormat);
473 z = sqlite3_vmprintf(zFormat, ap);
475 utf8_printf(iotrace, "%s", z);
481 ** Output string zUtf to stream pOut as w characters. If w is negative,
482 ** then right-justify the text. W is the width in UTF-8 characters, not
483 ** in bytes. This is different from the %*.*s specification in printf
484 ** since with %*.*s the width is measured in bytes, not characters.
486 static void utf8_width_print(FILE *pOut, int w, const char *zUtf){
489 int aw = w<0 ? -w : w;
491 if( aw>(int)sizeof(zBuf)/3 ) aw = (int)sizeof(zBuf)/3;
492 for(i=n=0; zUtf[i]; i++){
493 if( (zUtf[i]&0xc0)!=0x80 ){
496 do{ i++; }while( (zUtf[i]&0xc0)==0x80 );
502 utf8_printf(pOut, "%.*s", i, zUtf);
504 utf8_printf(pOut, "%*s%s", aw-n, "", zUtf);
506 utf8_printf(pOut, "%s%*s", zUtf, aw-n, "");
512 ** Determines if a string is a number of not.
514 static int isNumber(const char *z, int *realnum){
515 if( *z=='-' || *z=='+' ) z++;
520 if( realnum ) *realnum = 0;
521 while( IsDigit(*z) ){ z++; }
524 if( !IsDigit(*z) ) return 0;
525 while( IsDigit(*z) ){ z++; }
526 if( realnum ) *realnum = 1;
528 if( *z=='e' || *z=='E' ){
530 if( *z=='+' || *z=='-' ) z++;
531 if( !IsDigit(*z) ) return 0;
532 while( IsDigit(*z) ){ z++; }
533 if( realnum ) *realnum = 1;
539 ** Compute a string length that is limited to what can be stored in
540 ** lower 30 bits of a 32-bit signed integer.
542 static int strlen30(const char *z){
544 while( *z2 ){ z2++; }
545 return 0x3fffffff & (int)(z2 - z);
549 ** Return the length of a string in characters. Multibyte UTF8 characters
550 ** count as a single character.
552 static int strlenChar(const char *z){
555 if( (0xc0&*(z++))!=0x80 ) n++;
561 ** This routine reads a line of text from FILE in, stores
562 ** the text in memory obtained from malloc() and returns a pointer
563 ** to the text. NULL is returned at end of file, or if malloc()
566 ** If zLine is not NULL then it is a malloced buffer returned from
567 ** a previous call to this routine that may be reused.
569 static char *local_getline(char *zLine, FILE *in){
570 int nLine = zLine==0 ? 0 : 100;
575 nLine = nLine*2 + 100;
576 zLine = realloc(zLine, nLine);
577 if( zLine==0 ) return 0;
579 if( fgets(&zLine[n], nLine - n, in)==0 ){
587 while( zLine[n] ) n++;
588 if( n>0 && zLine[n-1]=='\n' ){
590 if( n>0 && zLine[n-1]=='\r' ) n--;
595 #if defined(_WIN32) || defined(WIN32)
596 /* For interactive input on Windows systems, translate the
597 ** multi-byte characterset characters into UTF-8. */
598 if( stdin_is_interactive && in==stdin ){
599 char *zTrans = sqlite3_win32_mbcs_to_utf8_v2(zLine, 0);
601 int nTrans = strlen30(zTrans)+1;
603 zLine = realloc(zLine, nTrans);
605 sqlite3_free(zTrans);
609 memcpy(zLine, zTrans, nTrans);
610 sqlite3_free(zTrans);
613 #endif /* defined(_WIN32) || defined(WIN32) */
618 ** Retrieve a single line of input text.
620 ** If in==0 then read from standard input and prompt before each line.
621 ** If isContinuation is true, then a continuation prompt is appropriate.
622 ** If isContinuation is zero, then the main prompt should be used.
624 ** If zPrior is not NULL then it is a buffer from a prior call to this
625 ** routine that can be reused.
627 ** The result is stored in space obtained from malloc() and must either
628 ** be freed by the caller or else passed back into this routine via the
629 ** zPrior argument for reuse.
631 static char *one_input_line(FILE *in, char *zPrior, int isContinuation){
635 zResult = local_getline(zPrior, in);
637 zPrompt = isContinuation ? continuePrompt : mainPrompt;
638 #if SHELL_USE_LOCAL_GETLINE
639 printf("%s", zPrompt);
641 zResult = local_getline(zPrior, stdin);
644 zResult = shell_readline(zPrompt);
645 if( zResult && *zResult ) shell_add_history(zResult);
653 ** Return the value of a hexadecimal digit. Return -1 if the input
654 ** is not a hex digit.
656 static int hexDigitValue(char c){
657 if( c>='0' && c<='9' ) return c - '0';
658 if( c>='a' && c<='f' ) return c - 'a' + 10;
659 if( c>='A' && c<='F' ) return c - 'A' + 10;
664 ** Interpret zArg as an integer value, possibly with suffixes.
666 static sqlite3_int64 integerValue(const char *zArg){
668 static const struct { char *zSuffix; int iMult; } aMult[] = {
670 { "MiB", 1024*1024 },
671 { "GiB", 1024*1024*1024 },
674 { "GB", 1000000000 },
684 }else if( zArg[0]=='+' ){
687 if( zArg[0]=='0' && zArg[1]=='x' ){
690 while( (x = hexDigitValue(zArg[0]))>=0 ){
695 while( IsDigit(zArg[0]) ){
696 v = v*10 + zArg[0] - '0';
700 for(i=0; i<ArraySize(aMult); i++){
701 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
706 return isNeg? -v : v;
710 ** A variable length string to which one can append text.
712 typedef struct ShellText ShellText;
720 ** Initialize and destroy a ShellText object
722 static void initText(ShellText *p){
723 memset(p, 0, sizeof(*p));
725 static void freeText(ShellText *p){
730 /* zIn is either a pointer to a NULL-terminated string in memory obtained
731 ** from malloc(), or a NULL pointer. The string pointed to by zAppend is
732 ** added to zIn, and the result returned in memory obtained from malloc().
733 ** zIn, if it was not NULL, is freed.
735 ** If the third argument, quote, is not '\0', then it is used as a
736 ** quote character for zAppend.
738 static void appendText(ShellText *p, char const *zAppend, char quote){
741 int nAppend = strlen30(zAppend);
743 len = nAppend+p->n+1;
746 for(i=0; i<nAppend; i++){
747 if( zAppend[i]==quote ) len++;
751 if( p->n+len>=p->nAlloc ){
752 p->nAlloc = p->nAlloc*2 + len + 20;
753 p->z = realloc(p->z, p->nAlloc);
755 memset(p, 0, sizeof(*p));
761 char *zCsr = p->z+p->n;
763 for(i=0; i<nAppend; i++){
764 *zCsr++ = zAppend[i];
765 if( zAppend[i]==quote ) *zCsr++ = quote;
768 p->n = (int)(zCsr - p->z);
771 memcpy(p->z+p->n, zAppend, nAppend);
778 ** Attempt to determine if identifier zName needs to be quoted, either
779 ** because it contains non-alphanumeric characters, or because it is an
780 ** SQLite keyword. Be conservative in this estimate: When in doubt assume
781 ** that quoting is required.
783 ** Return '"' if quoting is required. Return 0 if no quoting is required.
785 static char quoteChar(const char *zName){
786 /* All SQLite keywords, in alphabetical order */
787 static const char *azKeywords[] = {
788 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
789 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
790 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
791 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
792 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
793 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
794 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
795 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
796 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
797 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
798 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
799 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
800 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
801 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
802 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
803 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
804 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
807 int i, lwr, upr, mid, c;
808 if( !isalpha((unsigned char)zName[0]) && zName[0]!='_' ) return '"';
809 for(i=0; zName[i]; i++){
810 if( !isalnum((unsigned char)zName[i]) && zName[i]!='_' ) return '"';
813 upr = sizeof(azKeywords)/sizeof(azKeywords[0]) - 1;
816 c = sqlite3_stricmp(azKeywords[mid], zName);
817 if( c==0 ) return '"';
828 ** Construct a fake object name and column list to describe the structure
829 ** of the view, virtual table, or table valued function zSchema.zName.
831 static char *shellFakeSchema(
832 sqlite3 *db, /* The database connection containing the vtab */
833 const char *zSchema, /* Schema of the database holding the vtab */
834 const char *zName /* The name of the virtual table */
836 sqlite3_stmt *pStmt = 0;
843 zSql = sqlite3_mprintf("PRAGMA \"%w\".table_info=%Q;",
844 zSchema ? zSchema : "main", zName);
845 sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
849 cQuote = quoteChar(zSchema);
850 if( cQuote && sqlite3_stricmp(zSchema,"temp")==0 ) cQuote = 0;
851 appendText(&s, zSchema, cQuote);
852 appendText(&s, ".", 0);
854 cQuote = quoteChar(zName);
855 appendText(&s, zName, cQuote);
856 while( sqlite3_step(pStmt)==SQLITE_ROW ){
857 const char *zCol = (const char*)sqlite3_column_text(pStmt, 1);
859 appendText(&s, zDiv, 0);
861 cQuote = quoteChar(zCol);
862 appendText(&s, zCol, cQuote);
864 appendText(&s, ")", 0);
865 sqlite3_finalize(pStmt);
874 ** SQL function: shell_module_schema(X)
876 ** Return a fake schema for the table-valued function or eponymous virtual
879 static void shellModuleSchema(
880 sqlite3_context *pCtx,
882 sqlite3_value **apVal
884 const char *zName = (const char*)sqlite3_value_text(apVal[0]);
885 char *zFake = shellFakeSchema(sqlite3_context_db_handle(pCtx), 0, zName);
886 UNUSED_PARAMETER(nVal);
888 sqlite3_result_text(pCtx, sqlite3_mprintf("/* %s */", zFake),
895 ** SQL function: shell_add_schema(S,X)
897 ** Add the schema name X to the CREATE statement in S and return the result.
900 ** CREATE TABLE t1(x) -> CREATE TABLE xyz.t1(x);
905 ** CREATE UNIQUE INDEX
908 ** CREATE VIRTUAL TABLE
910 ** This UDF is used by the .schema command to insert the schema name of
911 ** attached databases into the middle of the sqlite_master.sql field.
913 static void shellAddSchemaName(
914 sqlite3_context *pCtx,
916 sqlite3_value **apVal
918 static const char *aPrefix[] = {
927 const char *zIn = (const char*)sqlite3_value_text(apVal[0]);
928 const char *zSchema = (const char*)sqlite3_value_text(apVal[1]);
929 const char *zName = (const char*)sqlite3_value_text(apVal[2]);
930 sqlite3 *db = sqlite3_context_db_handle(pCtx);
931 UNUSED_PARAMETER(nVal);
932 if( zIn!=0 && strncmp(zIn, "CREATE ", 7)==0 ){
933 for(i=0; i<(int)(sizeof(aPrefix)/sizeof(aPrefix[0])); i++){
934 int n = strlen30(aPrefix[i]);
935 if( strncmp(zIn+7, aPrefix[i], n)==0 && zIn[n+7]==' ' ){
939 char cQuote = quoteChar(zSchema);
940 if( cQuote && sqlite3_stricmp(zSchema,"temp")!=0 ){
941 z = sqlite3_mprintf("%.*s \"%w\".%s", n+7, zIn, zSchema, zIn+n+8);
943 z = sqlite3_mprintf("%.*s %s.%s", n+7, zIn, zSchema, zIn+n+8);
947 && aPrefix[i][0]=='V'
948 && (zFake = shellFakeSchema(db, zSchema, zName))!=0
951 z = sqlite3_mprintf("%s\n/* %s */", zIn, zFake);
953 z = sqlite3_mprintf("%z\n/* %s */", z, zFake);
958 sqlite3_result_text(pCtx, z, -1, sqlite3_free);
964 sqlite3_result_value(pCtx, apVal[0]);
968 ** The source code for several run-time loadable extensions is inserted
969 ** below by the ../tool/mkshellc.tcl script. Before processing that included
970 ** code, we need to override some macros to make the included program code
971 ** work here in the middle of this regular program.
973 #define SQLITE_EXTENSION_INIT1
974 #define SQLITE_EXTENSION_INIT2(X) (void)(X)
976 #if defined(_WIN32) && defined(_MSC_VER)
977 /************************* Begin test_windirent.h ******************/
981 ** The author disclaims copyright to this source code. In place of
982 ** a legal notice, here is a blessing:
984 ** May you do good and not evil.
985 ** May you find forgiveness for yourself and forgive others.
986 ** May you share freely, never taking more than you give.
988 *************************************************************************
989 ** This file contains declarations for most of the opendir() family of
990 ** POSIX functions on Win32 using the MSVCRT.
993 #if defined(_WIN32) && defined(_MSC_VER) && !defined(SQLITE_WINDIRENT_H)
994 #define SQLITE_WINDIRENT_H
997 ** We need several data types from the Windows SDK header.
1000 #define WIN32_LEAN_AND_MEAN
1001 #include "windows.h"
1004 ** We need several support functions from the SQLite core.
1009 ** We need several things from the ANSI and MSVCRT headers.
1017 #include <sys/types.h>
1018 #include <sys/stat.h>
1021 ** We may need several defines that should have been in "sys/stat.h".
1025 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
1029 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
1033 #define S_ISLNK(mode) (0)
1037 ** We may need to provide the "mode_t" type.
1040 #ifndef MODE_T_DEFINED
1041 #define MODE_T_DEFINED
1042 typedef unsigned short mode_t;
1046 ** We may need to provide the "ino_t" type.
1049 #ifndef INO_T_DEFINED
1050 #define INO_T_DEFINED
1051 typedef unsigned short ino_t;
1055 ** We need to define "NAME_MAX" if it was not present in "limits.h".
1059 # ifdef FILENAME_MAX
1060 # define NAME_MAX (FILENAME_MAX)
1062 # define NAME_MAX (260)
1067 ** We need to define "NULL_INTPTR_T" and "BAD_INTPTR_T".
1070 #ifndef NULL_INTPTR_T
1071 # define NULL_INTPTR_T ((intptr_t)(0))
1074 #ifndef BAD_INTPTR_T
1075 # define BAD_INTPTR_T ((intptr_t)(-1))
1079 ** We need to provide the necessary structures and related types.
1082 #ifndef DIRENT_DEFINED
1083 #define DIRENT_DEFINED
1084 typedef struct DIRENT DIRENT;
1085 typedef DIRENT *LPDIRENT;
1087 ino_t d_ino; /* Sequence number, do not use. */
1088 unsigned d_attributes; /* Win32 file attributes. */
1089 char d_name[NAME_MAX + 1]; /* Name within the directory. */
1095 typedef struct DIR DIR;
1098 intptr_t d_handle; /* Value returned by "_findfirst". */
1099 DIRENT d_first; /* DIRENT constructed based on "_findfirst". */
1100 DIRENT d_next; /* DIRENT constructed based on "_findnext". */
1105 ** Provide a macro, for use by the implementation, to determine if a
1106 ** particular directory entry should be skipped over when searching for
1107 ** the next directory entry that should be returned by the readdir() or
1108 ** readdir_r() functions.
1112 # define is_filtered(a) ((((a).attrib)&_A_HIDDEN) || (((a).attrib)&_A_SYSTEM))
1116 ** Provide the function prototype for the POSIX compatiable getenv()
1117 ** function. This function is not thread-safe.
1120 extern const char *windirent_getenv(const char *name);
1123 ** Finally, we can provide the function prototypes for the opendir(),
1124 ** readdir(), readdir_r(), and closedir() POSIX functions.
1127 extern LPDIR opendir(const char *dirname);
1128 extern LPDIRENT readdir(LPDIR dirp);
1129 extern INT readdir_r(LPDIR dirp, LPDIRENT entry, LPDIRENT *result);
1130 extern INT closedir(LPDIR dirp);
1132 #endif /* defined(WIN32) && defined(_MSC_VER) */
1134 /************************* End test_windirent.h ********************/
1135 /************************* Begin test_windirent.c ******************/
1139 ** The author disclaims copyright to this source code. In place of
1140 ** a legal notice, here is a blessing:
1142 ** May you do good and not evil.
1143 ** May you find forgiveness for yourself and forgive others.
1144 ** May you share freely, never taking more than you give.
1146 *************************************************************************
1147 ** This file contains code to implement most of the opendir() family of
1148 ** POSIX functions on Win32 using the MSVCRT.
1151 #if defined(_WIN32) && defined(_MSC_VER)
1152 /* #include "test_windirent.h" */
1155 ** Implementation of the POSIX getenv() function using the Win32 API.
1156 ** This function is not thread-safe.
1158 const char *windirent_getenv(
1161 static char value[32768]; /* Maximum length, per MSDN */
1162 DWORD dwSize = sizeof(value) / sizeof(char); /* Size in chars */
1163 DWORD dwRet; /* Value returned by GetEnvironmentVariableA() */
1165 memset(value, 0, sizeof(value));
1166 dwRet = GetEnvironmentVariableA(name, value, dwSize);
1167 if( dwRet==0 || dwRet>dwSize ){
1169 ** The function call to GetEnvironmentVariableA() failed -OR-
1170 ** the buffer is not large enough. Either way, return NULL.
1175 ** The function call to GetEnvironmentVariableA() succeeded
1176 ** -AND- the buffer contains the entire value.
1183 ** Implementation of the POSIX opendir() function using the MSVCRT.
1188 struct _finddata_t data;
1189 LPDIR dirp = (LPDIR)sqlite3_malloc(sizeof(DIR));
1190 SIZE_T namesize = sizeof(data.name) / sizeof(data.name[0]);
1192 if( dirp==NULL ) return NULL;
1193 memset(dirp, 0, sizeof(DIR));
1195 /* TODO: Remove this if Unix-style root paths are not used. */
1196 if( sqlite3_stricmp(dirname, "/")==0 ){
1197 dirname = windirent_getenv("SystemDrive");
1200 memset(&data, 0, sizeof(struct _finddata_t));
1201 _snprintf(data.name, namesize, "%s\\*", dirname);
1202 dirp->d_handle = _findfirst(data.name, &data);
1204 if( dirp->d_handle==BAD_INTPTR_T ){
1209 /* TODO: Remove this block to allow hidden and/or system files. */
1210 if( is_filtered(data) ){
1213 memset(&data, 0, sizeof(struct _finddata_t));
1214 if( _findnext(dirp->d_handle, &data)==-1 ){
1219 /* TODO: Remove this block to allow hidden and/or system files. */
1220 if( is_filtered(data) ) goto next;
1223 dirp->d_first.d_attributes = data.attrib;
1224 strncpy(dirp->d_first.d_name, data.name, NAME_MAX);
1225 dirp->d_first.d_name[NAME_MAX] = '\0';
1231 ** Implementation of the POSIX readdir() function using the MSVCRT.
1236 struct _finddata_t data;
1238 if( dirp==NULL ) return NULL;
1240 if( dirp->d_first.d_ino==0 ){
1241 dirp->d_first.d_ino++;
1242 dirp->d_next.d_ino++;
1244 return &dirp->d_first;
1249 memset(&data, 0, sizeof(struct _finddata_t));
1250 if( _findnext(dirp->d_handle, &data)==-1 ) return NULL;
1252 /* TODO: Remove this block to allow hidden and/or system files. */
1253 if( is_filtered(data) ) goto next;
1255 dirp->d_next.d_ino++;
1256 dirp->d_next.d_attributes = data.attrib;
1257 strncpy(dirp->d_next.d_name, data.name, NAME_MAX);
1258 dirp->d_next.d_name[NAME_MAX] = '\0';
1260 return &dirp->d_next;
1264 ** Implementation of the POSIX readdir_r() function using the MSVCRT.
1271 struct _finddata_t data;
1273 if( dirp==NULL ) return EBADF;
1275 if( dirp->d_first.d_ino==0 ){
1276 dirp->d_first.d_ino++;
1277 dirp->d_next.d_ino++;
1279 entry->d_ino = dirp->d_first.d_ino;
1280 entry->d_attributes = dirp->d_first.d_attributes;
1281 strncpy(entry->d_name, dirp->d_first.d_name, NAME_MAX);
1282 entry->d_name[NAME_MAX] = '\0';
1290 memset(&data, 0, sizeof(struct _finddata_t));
1291 if( _findnext(dirp->d_handle, &data)==-1 ){
1296 /* TODO: Remove this block to allow hidden and/or system files. */
1297 if( is_filtered(data) ) goto next;
1299 entry->d_ino = (ino_t)-1; /* not available */
1300 entry->d_attributes = data.attrib;
1301 strncpy(entry->d_name, data.name, NAME_MAX);
1302 entry->d_name[NAME_MAX] = '\0';
1309 ** Implementation of the POSIX closedir() function using the MSVCRT.
1316 if( dirp==NULL ) return EINVAL;
1318 if( dirp->d_handle!=NULL_INTPTR_T && dirp->d_handle!=BAD_INTPTR_T ){
1319 result = _findclose(dirp->d_handle);
1326 #endif /* defined(WIN32) && defined(_MSC_VER) */
1328 /************************* End test_windirent.c ********************/
1329 #define dirent DIRENT
1331 /************************* Begin ../ext/misc/shathree.c ******************/
1335 ** The author disclaims copyright to this source code. In place of
1336 ** a legal notice, here is a blessing:
1338 ** May you do good and not evil.
1339 ** May you find forgiveness for yourself and forgive others.
1340 ** May you share freely, never taking more than you give.
1342 ******************************************************************************
1344 ** This SQLite extension implements a functions that compute SHA1 hashes.
1345 ** Two SQL functions are implemented:
1348 ** sha3_query(Y,SIZE)
1350 ** The sha3(X) function computes the SHA3 hash of the input X, or NULL if
1353 ** The sha3_query(Y) function evalutes all queries in the SQL statements of Y
1354 ** and returns a hash of their results.
1356 ** The SIZE argument is optional. If omitted, the SHA3-256 hash algorithm
1357 ** is used. If SIZE is included it must be one of the integers 224, 256,
1358 ** 384, or 512, to determine SHA3 hash variant that is computed.
1360 SQLITE_EXTENSION_INIT1
1364 /* typedef sqlite3_uint64 u64; */
1366 /******************************************************************************
1370 ** Macros to determine whether the machine is big or little endian,
1371 ** and whether or not that determination is run-time or compile-time.
1373 ** For best performance, an attempt is made to guess at the byte-order
1374 ** using C-preprocessor macros. If that is unsuccessful, or if
1375 ** -DSHA3_BYTEORDER=0 is set, then byte-order is determined
1378 #ifndef SHA3_BYTEORDER
1379 # if defined(i386) || defined(__i386__) || defined(_M_IX86) || \
1380 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
1381 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
1383 # define SHA3_BYTEORDER 1234
1384 # elif defined(sparc) || defined(__ppc__)
1385 # define SHA3_BYTEORDER 4321
1387 # define SHA3_BYTEORDER 0
1393 ** State structure for a SHA3 hash in progress
1395 typedef struct SHA3Context SHA3Context;
1396 struct SHA3Context {
1398 u64 s[25]; /* Keccak state. 5x5 lines of 64 bits each */
1399 unsigned char x[1600]; /* ... or 1600 bytes */
1401 unsigned nRate; /* Bytes of input accepted per Keccak iteration */
1402 unsigned nLoaded; /* Input bytes loaded into u.x[] so far this cycle */
1403 unsigned ixMask; /* Insert next input into u.x[nLoaded^ixMask]. */
1407 ** A single step of the Keccak mixing function for a 1600-bit state
1409 static void KeccakF1600Step(SHA3Context *p){
1411 u64 b0, b1, b2, b3, b4;
1412 u64 c0, c1, c2, c3, c4;
1413 u64 d0, d1, d2, d3, d4;
1414 static const u64 RC[] = {
1415 0x0000000000000001ULL, 0x0000000000008082ULL,
1416 0x800000000000808aULL, 0x8000000080008000ULL,
1417 0x000000000000808bULL, 0x0000000080000001ULL,
1418 0x8000000080008081ULL, 0x8000000000008009ULL,
1419 0x000000000000008aULL, 0x0000000000000088ULL,
1420 0x0000000080008009ULL, 0x000000008000000aULL,
1421 0x000000008000808bULL, 0x800000000000008bULL,
1422 0x8000000000008089ULL, 0x8000000000008003ULL,
1423 0x8000000000008002ULL, 0x8000000000000080ULL,
1424 0x000000000000800aULL, 0x800000008000000aULL,
1425 0x8000000080008081ULL, 0x8000000000008080ULL,
1426 0x0000000080000001ULL, 0x8000000080008008ULL
1428 # define a00 (p->u.s[0])
1429 # define a01 (p->u.s[1])
1430 # define a02 (p->u.s[2])
1431 # define a03 (p->u.s[3])
1432 # define a04 (p->u.s[4])
1433 # define a10 (p->u.s[5])
1434 # define a11 (p->u.s[6])
1435 # define a12 (p->u.s[7])
1436 # define a13 (p->u.s[8])
1437 # define a14 (p->u.s[9])
1438 # define a20 (p->u.s[10])
1439 # define a21 (p->u.s[11])
1440 # define a22 (p->u.s[12])
1441 # define a23 (p->u.s[13])
1442 # define a24 (p->u.s[14])
1443 # define a30 (p->u.s[15])
1444 # define a31 (p->u.s[16])
1445 # define a32 (p->u.s[17])
1446 # define a33 (p->u.s[18])
1447 # define a34 (p->u.s[19])
1448 # define a40 (p->u.s[20])
1449 # define a41 (p->u.s[21])
1450 # define a42 (p->u.s[22])
1451 # define a43 (p->u.s[23])
1452 # define a44 (p->u.s[24])
1453 # define ROL64(a,x) ((a<<x)|(a>>(64-x)))
1455 for(i=0; i<24; i+=4){
1456 c0 = a00^a10^a20^a30^a40;
1457 c1 = a01^a11^a21^a31^a41;
1458 c2 = a02^a12^a22^a32^a42;
1459 c3 = a03^a13^a23^a33^a43;
1460 c4 = a04^a14^a24^a34^a44;
1461 d0 = c4^ROL64(c1, 1);
1462 d1 = c0^ROL64(c2, 1);
1463 d2 = c1^ROL64(c3, 1);
1464 d3 = c2^ROL64(c4, 1);
1465 d4 = c3^ROL64(c0, 1);
1468 b1 = ROL64((a11^d1), 44);
1469 b2 = ROL64((a22^d2), 43);
1470 b3 = ROL64((a33^d3), 21);
1471 b4 = ROL64((a44^d4), 14);
1472 a00 = b0 ^((~b1)& b2 );
1474 a11 = b1 ^((~b2)& b3 );
1475 a22 = b2 ^((~b3)& b4 );
1476 a33 = b3 ^((~b4)& b0 );
1477 a44 = b4 ^((~b0)& b1 );
1479 b2 = ROL64((a20^d0), 3);
1480 b3 = ROL64((a31^d1), 45);
1481 b4 = ROL64((a42^d2), 61);
1482 b0 = ROL64((a03^d3), 28);
1483 b1 = ROL64((a14^d4), 20);
1484 a20 = b0 ^((~b1)& b2 );
1485 a31 = b1 ^((~b2)& b3 );
1486 a42 = b2 ^((~b3)& b4 );
1487 a03 = b3 ^((~b4)& b0 );
1488 a14 = b4 ^((~b0)& b1 );
1490 b4 = ROL64((a40^d0), 18);
1491 b0 = ROL64((a01^d1), 1);
1492 b1 = ROL64((a12^d2), 6);
1493 b2 = ROL64((a23^d3), 25);
1494 b3 = ROL64((a34^d4), 8);
1495 a40 = b0 ^((~b1)& b2 );
1496 a01 = b1 ^((~b2)& b3 );
1497 a12 = b2 ^((~b3)& b4 );
1498 a23 = b3 ^((~b4)& b0 );
1499 a34 = b4 ^((~b0)& b1 );
1501 b1 = ROL64((a10^d0), 36);
1502 b2 = ROL64((a21^d1), 10);
1503 b3 = ROL64((a32^d2), 15);
1504 b4 = ROL64((a43^d3), 56);
1505 b0 = ROL64((a04^d4), 27);
1506 a10 = b0 ^((~b1)& b2 );
1507 a21 = b1 ^((~b2)& b3 );
1508 a32 = b2 ^((~b3)& b4 );
1509 a43 = b3 ^((~b4)& b0 );
1510 a04 = b4 ^((~b0)& b1 );
1512 b3 = ROL64((a30^d0), 41);
1513 b4 = ROL64((a41^d1), 2);
1514 b0 = ROL64((a02^d2), 62);
1515 b1 = ROL64((a13^d3), 55);
1516 b2 = ROL64((a24^d4), 39);
1517 a30 = b0 ^((~b1)& b2 );
1518 a41 = b1 ^((~b2)& b3 );
1519 a02 = b2 ^((~b3)& b4 );
1520 a13 = b3 ^((~b4)& b0 );
1521 a24 = b4 ^((~b0)& b1 );
1523 c0 = a00^a20^a40^a10^a30;
1524 c1 = a11^a31^a01^a21^a41;
1525 c2 = a22^a42^a12^a32^a02;
1526 c3 = a33^a03^a23^a43^a13;
1527 c4 = a44^a14^a34^a04^a24;
1528 d0 = c4^ROL64(c1, 1);
1529 d1 = c0^ROL64(c2, 1);
1530 d2 = c1^ROL64(c3, 1);
1531 d3 = c2^ROL64(c4, 1);
1532 d4 = c3^ROL64(c0, 1);
1535 b1 = ROL64((a31^d1), 44);
1536 b2 = ROL64((a12^d2), 43);
1537 b3 = ROL64((a43^d3), 21);
1538 b4 = ROL64((a24^d4), 14);
1539 a00 = b0 ^((~b1)& b2 );
1541 a31 = b1 ^((~b2)& b3 );
1542 a12 = b2 ^((~b3)& b4 );
1543 a43 = b3 ^((~b4)& b0 );
1544 a24 = b4 ^((~b0)& b1 );
1546 b2 = ROL64((a40^d0), 3);
1547 b3 = ROL64((a21^d1), 45);
1548 b4 = ROL64((a02^d2), 61);
1549 b0 = ROL64((a33^d3), 28);
1550 b1 = ROL64((a14^d4), 20);
1551 a40 = b0 ^((~b1)& b2 );
1552 a21 = b1 ^((~b2)& b3 );
1553 a02 = b2 ^((~b3)& b4 );
1554 a33 = b3 ^((~b4)& b0 );
1555 a14 = b4 ^((~b0)& b1 );
1557 b4 = ROL64((a30^d0), 18);
1558 b0 = ROL64((a11^d1), 1);
1559 b1 = ROL64((a42^d2), 6);
1560 b2 = ROL64((a23^d3), 25);
1561 b3 = ROL64((a04^d4), 8);
1562 a30 = b0 ^((~b1)& b2 );
1563 a11 = b1 ^((~b2)& b3 );
1564 a42 = b2 ^((~b3)& b4 );
1565 a23 = b3 ^((~b4)& b0 );
1566 a04 = b4 ^((~b0)& b1 );
1568 b1 = ROL64((a20^d0), 36);
1569 b2 = ROL64((a01^d1), 10);
1570 b3 = ROL64((a32^d2), 15);
1571 b4 = ROL64((a13^d3), 56);
1572 b0 = ROL64((a44^d4), 27);
1573 a20 = b0 ^((~b1)& b2 );
1574 a01 = b1 ^((~b2)& b3 );
1575 a32 = b2 ^((~b3)& b4 );
1576 a13 = b3 ^((~b4)& b0 );
1577 a44 = b4 ^((~b0)& b1 );
1579 b3 = ROL64((a10^d0), 41);
1580 b4 = ROL64((a41^d1), 2);
1581 b0 = ROL64((a22^d2), 62);
1582 b1 = ROL64((a03^d3), 55);
1583 b2 = ROL64((a34^d4), 39);
1584 a10 = b0 ^((~b1)& b2 );
1585 a41 = b1 ^((~b2)& b3 );
1586 a22 = b2 ^((~b3)& b4 );
1587 a03 = b3 ^((~b4)& b0 );
1588 a34 = b4 ^((~b0)& b1 );
1590 c0 = a00^a40^a30^a20^a10;
1591 c1 = a31^a21^a11^a01^a41;
1592 c2 = a12^a02^a42^a32^a22;
1593 c3 = a43^a33^a23^a13^a03;
1594 c4 = a24^a14^a04^a44^a34;
1595 d0 = c4^ROL64(c1, 1);
1596 d1 = c0^ROL64(c2, 1);
1597 d2 = c1^ROL64(c3, 1);
1598 d3 = c2^ROL64(c4, 1);
1599 d4 = c3^ROL64(c0, 1);
1602 b1 = ROL64((a21^d1), 44);
1603 b2 = ROL64((a42^d2), 43);
1604 b3 = ROL64((a13^d3), 21);
1605 b4 = ROL64((a34^d4), 14);
1606 a00 = b0 ^((~b1)& b2 );
1608 a21 = b1 ^((~b2)& b3 );
1609 a42 = b2 ^((~b3)& b4 );
1610 a13 = b3 ^((~b4)& b0 );
1611 a34 = b4 ^((~b0)& b1 );
1613 b2 = ROL64((a30^d0), 3);
1614 b3 = ROL64((a01^d1), 45);
1615 b4 = ROL64((a22^d2), 61);
1616 b0 = ROL64((a43^d3), 28);
1617 b1 = ROL64((a14^d4), 20);
1618 a30 = b0 ^((~b1)& b2 );
1619 a01 = b1 ^((~b2)& b3 );
1620 a22 = b2 ^((~b3)& b4 );
1621 a43 = b3 ^((~b4)& b0 );
1622 a14 = b4 ^((~b0)& b1 );
1624 b4 = ROL64((a10^d0), 18);
1625 b0 = ROL64((a31^d1), 1);
1626 b1 = ROL64((a02^d2), 6);
1627 b2 = ROL64((a23^d3), 25);
1628 b3 = ROL64((a44^d4), 8);
1629 a10 = b0 ^((~b1)& b2 );
1630 a31 = b1 ^((~b2)& b3 );
1631 a02 = b2 ^((~b3)& b4 );
1632 a23 = b3 ^((~b4)& b0 );
1633 a44 = b4 ^((~b0)& b1 );
1635 b1 = ROL64((a40^d0), 36);
1636 b2 = ROL64((a11^d1), 10);
1637 b3 = ROL64((a32^d2), 15);
1638 b4 = ROL64((a03^d3), 56);
1639 b0 = ROL64((a24^d4), 27);
1640 a40 = b0 ^((~b1)& b2 );
1641 a11 = b1 ^((~b2)& b3 );
1642 a32 = b2 ^((~b3)& b4 );
1643 a03 = b3 ^((~b4)& b0 );
1644 a24 = b4 ^((~b0)& b1 );
1646 b3 = ROL64((a20^d0), 41);
1647 b4 = ROL64((a41^d1), 2);
1648 b0 = ROL64((a12^d2), 62);
1649 b1 = ROL64((a33^d3), 55);
1650 b2 = ROL64((a04^d4), 39);
1651 a20 = b0 ^((~b1)& b2 );
1652 a41 = b1 ^((~b2)& b3 );
1653 a12 = b2 ^((~b3)& b4 );
1654 a33 = b3 ^((~b4)& b0 );
1655 a04 = b4 ^((~b0)& b1 );
1657 c0 = a00^a30^a10^a40^a20;
1658 c1 = a21^a01^a31^a11^a41;
1659 c2 = a42^a22^a02^a32^a12;
1660 c3 = a13^a43^a23^a03^a33;
1661 c4 = a34^a14^a44^a24^a04;
1662 d0 = c4^ROL64(c1, 1);
1663 d1 = c0^ROL64(c2, 1);
1664 d2 = c1^ROL64(c3, 1);
1665 d3 = c2^ROL64(c4, 1);
1666 d4 = c3^ROL64(c0, 1);
1669 b1 = ROL64((a01^d1), 44);
1670 b2 = ROL64((a02^d2), 43);
1671 b3 = ROL64((a03^d3), 21);
1672 b4 = ROL64((a04^d4), 14);
1673 a00 = b0 ^((~b1)& b2 );
1675 a01 = b1 ^((~b2)& b3 );
1676 a02 = b2 ^((~b3)& b4 );
1677 a03 = b3 ^((~b4)& b0 );
1678 a04 = b4 ^((~b0)& b1 );
1680 b2 = ROL64((a10^d0), 3);
1681 b3 = ROL64((a11^d1), 45);
1682 b4 = ROL64((a12^d2), 61);
1683 b0 = ROL64((a13^d3), 28);
1684 b1 = ROL64((a14^d4), 20);
1685 a10 = b0 ^((~b1)& b2 );
1686 a11 = b1 ^((~b2)& b3 );
1687 a12 = b2 ^((~b3)& b4 );
1688 a13 = b3 ^((~b4)& b0 );
1689 a14 = b4 ^((~b0)& b1 );
1691 b4 = ROL64((a20^d0), 18);
1692 b0 = ROL64((a21^d1), 1);
1693 b1 = ROL64((a22^d2), 6);
1694 b2 = ROL64((a23^d3), 25);
1695 b3 = ROL64((a24^d4), 8);
1696 a20 = b0 ^((~b1)& b2 );
1697 a21 = b1 ^((~b2)& b3 );
1698 a22 = b2 ^((~b3)& b4 );
1699 a23 = b3 ^((~b4)& b0 );
1700 a24 = b4 ^((~b0)& b1 );
1702 b1 = ROL64((a30^d0), 36);
1703 b2 = ROL64((a31^d1), 10);
1704 b3 = ROL64((a32^d2), 15);
1705 b4 = ROL64((a33^d3), 56);
1706 b0 = ROL64((a34^d4), 27);
1707 a30 = b0 ^((~b1)& b2 );
1708 a31 = b1 ^((~b2)& b3 );
1709 a32 = b2 ^((~b3)& b4 );
1710 a33 = b3 ^((~b4)& b0 );
1711 a34 = b4 ^((~b0)& b1 );
1713 b3 = ROL64((a40^d0), 41);
1714 b4 = ROL64((a41^d1), 2);
1715 b0 = ROL64((a42^d2), 62);
1716 b1 = ROL64((a43^d3), 55);
1717 b2 = ROL64((a44^d4), 39);
1718 a40 = b0 ^((~b1)& b2 );
1719 a41 = b1 ^((~b2)& b3 );
1720 a42 = b2 ^((~b3)& b4 );
1721 a43 = b3 ^((~b4)& b0 );
1722 a44 = b4 ^((~b0)& b1 );
1727 ** Initialize a new hash. iSize determines the size of the hash
1728 ** in bits and should be one of 224, 256, 384, or 512. Or iSize
1729 ** can be zero to use the default hash size of 256 bits.
1731 static void SHA3Init(SHA3Context *p, int iSize){
1732 memset(p, 0, sizeof(*p));
1733 if( iSize>=128 && iSize<=512 ){
1734 p->nRate = (1600 - ((iSize + 31)&~31)*2)/8;
1736 p->nRate = (1600 - 2*256)/8;
1738 #if SHA3_BYTEORDER==1234
1739 /* Known to be little-endian at compile-time. No-op */
1740 #elif SHA3_BYTEORDER==4321
1741 p->ixMask = 7; /* Big-endian */
1744 static unsigned int one = 1;
1745 if( 1==*(unsigned char*)&one ){
1746 /* Little endian. No byte swapping. */
1749 /* Big endian. Byte swap. */
1757 ** Make consecutive calls to the SHA3Update function to add new content
1760 static void SHA3Update(
1762 const unsigned char *aData,
1766 #if SHA3_BYTEORDER==1234
1767 if( (p->nLoaded % 8)==0 && ((aData - (const unsigned char*)0)&7)==0 ){
1768 for(; i+7<nData; i+=8){
1769 p->u.s[p->nLoaded/8] ^= *(u64*)&aData[i];
1771 if( p->nLoaded>=p->nRate ){
1778 for(; i<nData; i++){
1779 #if SHA3_BYTEORDER==1234
1780 p->u.x[p->nLoaded] ^= aData[i];
1781 #elif SHA3_BYTEORDER==4321
1782 p->u.x[p->nLoaded^0x07] ^= aData[i];
1784 p->u.x[p->nLoaded^p->ixMask] ^= aData[i];
1787 if( p->nLoaded==p->nRate ){
1795 ** After all content has been added, invoke SHA3Final() to compute
1796 ** the final hash. The function returns a pointer to the binary
1799 static unsigned char *SHA3Final(SHA3Context *p){
1801 if( p->nLoaded==p->nRate-1 ){
1802 const unsigned char c1 = 0x86;
1803 SHA3Update(p, &c1, 1);
1805 const unsigned char c2 = 0x06;
1806 const unsigned char c3 = 0x80;
1807 SHA3Update(p, &c2, 1);
1808 p->nLoaded = p->nRate - 1;
1809 SHA3Update(p, &c3, 1);
1811 for(i=0; i<p->nRate; i++){
1812 p->u.x[i+p->nRate] = p->u.x[i^p->ixMask];
1814 return &p->u.x[p->nRate];
1816 /* End of the hashing logic
1817 *****************************************************************************/
1820 ** Implementation of the sha3(X,SIZE) function.
1822 ** Return a BLOB which is the SIZE-bit SHA3 hash of X. The default
1823 ** size is 256. If X is a BLOB, it is hashed as is.
1824 ** For all other non-NULL types of input, X is converted into a UTF-8 string
1825 ** and the string is hashed without the trailing 0x00 terminator. The hash
1826 ** of a NULL value is NULL.
1828 static void sha3Func(
1829 sqlite3_context *context,
1831 sqlite3_value **argv
1834 int eType = sqlite3_value_type(argv[0]);
1835 int nByte = sqlite3_value_bytes(argv[0]);
1840 iSize = sqlite3_value_int(argv[1]);
1841 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1842 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1847 if( eType==SQLITE_NULL ) return;
1848 SHA3Init(&cx, iSize);
1849 if( eType==SQLITE_BLOB ){
1850 SHA3Update(&cx, sqlite3_value_blob(argv[0]), nByte);
1852 SHA3Update(&cx, sqlite3_value_text(argv[0]), nByte);
1854 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
1857 /* Compute a string using sqlite3_vsnprintf() with a maximum length
1858 ** of 50 bytes and add it to the hash.
1860 static void hash_step_vformat(
1861 SHA3Context *p, /* Add content to this context */
1862 const char *zFormat,
1868 va_start(ap, zFormat);
1869 sqlite3_vsnprintf(sizeof(zBuf),zBuf,zFormat,ap);
1871 n = (int)strlen(zBuf);
1872 SHA3Update(p, (unsigned char*)zBuf, n);
1876 ** Implementation of the sha3_query(SQL,SIZE) function.
1878 ** This function compiles and runs the SQL statement(s) given in the
1879 ** argument. The results are hashed using a SIZE-bit SHA3. The default
1882 ** The format of the byte stream that is hashed is summarized as follows:
1892 ** <sql> is the original SQL text for each statement run and <n> is
1893 ** the size of that text. The SQL text is UTF-8. A single R character
1894 ** occurs before the start of each row. N means a NULL value.
1895 ** I mean an 8-byte little-endian integer <int>. F is a floating point
1896 ** number with an 8-byte little-endian IEEE floating point value <ieee-float>.
1897 ** B means blobs of <size> bytes. T means text rendered as <size>
1898 ** bytes of UTF-8. The <n> and <size> values are expressed as an ASCII
1901 ** For each SQL statement in the X input, there is one S segment. Each
1902 ** S segment is followed by zero or more R segments, one for each row in the
1903 ** result set. After each R, there are one or more N, I, F, B, or T segments,
1904 ** one for each column in the result set. Segments are concatentated directly
1905 ** with no delimiters of any kind.
1907 static void sha3QueryFunc(
1908 sqlite3_context *context,
1910 sqlite3_value **argv
1912 sqlite3 *db = sqlite3_context_db_handle(context);
1913 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
1914 sqlite3_stmt *pStmt = 0;
1915 int nCol; /* Number of columns in the result set */
1916 int i; /* Loop counter */
1926 iSize = sqlite3_value_int(argv[1]);
1927 if( iSize!=224 && iSize!=256 && iSize!=384 && iSize!=512 ){
1928 sqlite3_result_error(context, "SHA3 size should be one of: 224 256 "
1933 if( zSql==0 ) return;
1934 SHA3Init(&cx, iSize);
1936 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
1938 char *zMsg = sqlite3_mprintf("error SQL statement [%s]: %s",
1939 zSql, sqlite3_errmsg(db));
1940 sqlite3_finalize(pStmt);
1941 sqlite3_result_error(context, zMsg, -1);
1945 if( !sqlite3_stmt_readonly(pStmt) ){
1946 char *zMsg = sqlite3_mprintf("non-query: [%s]", sqlite3_sql(pStmt));
1947 sqlite3_finalize(pStmt);
1948 sqlite3_result_error(context, zMsg, -1);
1952 nCol = sqlite3_column_count(pStmt);
1953 z = sqlite3_sql(pStmt);
1955 hash_step_vformat(&cx,"S%d:",n);
1956 SHA3Update(&cx,(unsigned char*)z,n);
1958 /* Compute a hash over the result of the query */
1959 while( SQLITE_ROW==sqlite3_step(pStmt) ){
1960 SHA3Update(&cx,(const unsigned char*)"R",1);
1961 for(i=0; i<nCol; i++){
1962 switch( sqlite3_column_type(pStmt,i) ){
1964 SHA3Update(&cx, (const unsigned char*)"N",1);
1967 case SQLITE_INTEGER: {
1971 sqlite3_int64 v = sqlite3_column_int64(pStmt,i);
1973 for(j=8; j>=1; j--){
1978 SHA3Update(&cx, x, 9);
1981 case SQLITE_FLOAT: {
1985 double r = sqlite3_column_double(pStmt,i);
1987 for(j=8; j>=1; j--){
1992 SHA3Update(&cx,x,9);
1996 int n2 = sqlite3_column_bytes(pStmt, i);
1997 const unsigned char *z2 = sqlite3_column_text(pStmt, i);
1998 hash_step_vformat(&cx,"T%d:",n2);
1999 SHA3Update(&cx, z2, n2);
2003 int n2 = sqlite3_column_bytes(pStmt, i);
2004 const unsigned char *z2 = sqlite3_column_blob(pStmt, i);
2005 hash_step_vformat(&cx,"B%d:",n2);
2006 SHA3Update(&cx, z2, n2);
2012 sqlite3_finalize(pStmt);
2014 sqlite3_result_blob(context, SHA3Final(&cx), iSize/8, SQLITE_TRANSIENT);
2021 int sqlite3_shathree_init(
2024 const sqlite3_api_routines *pApi
2027 SQLITE_EXTENSION_INIT2(pApi);
2028 (void)pzErrMsg; /* Unused parameter */
2029 rc = sqlite3_create_function(db, "sha3", 1, SQLITE_UTF8, 0,
2031 if( rc==SQLITE_OK ){
2032 rc = sqlite3_create_function(db, "sha3", 2, SQLITE_UTF8, 0,
2035 if( rc==SQLITE_OK ){
2036 rc = sqlite3_create_function(db, "sha3_query", 1, SQLITE_UTF8, 0,
2037 sha3QueryFunc, 0, 0);
2039 if( rc==SQLITE_OK ){
2040 rc = sqlite3_create_function(db, "sha3_query", 2, SQLITE_UTF8, 0,
2041 sha3QueryFunc, 0, 0);
2046 /************************* End ../ext/misc/shathree.c ********************/
2047 /************************* Begin ../ext/misc/fileio.c ******************/
2051 ** The author disclaims copyright to this source code. In place of
2052 ** a legal notice, here is a blessing:
2054 ** May you do good and not evil.
2055 ** May you find forgiveness for yourself and forgive others.
2056 ** May you share freely, never taking more than you give.
2058 ******************************************************************************
2060 ** This SQLite extension implements SQL functions readfile() and
2061 ** writefile(), and eponymous virtual type "fsdir".
2063 ** WRITEFILE(FILE, DATA [, MODE [, MTIME]]):
2065 ** If neither of the optional arguments is present, then this UDF
2066 ** function writes blob DATA to file FILE. If successful, the number
2067 ** of bytes written is returned. If an error occurs, NULL is returned.
2069 ** If the first option argument - MODE - is present, then it must
2070 ** be passed an integer value that corresponds to a POSIX mode
2071 ** value (file type + permissions, as returned in the stat.st_mode
2072 ** field by the stat() system call). Three types of files may
2073 ** be written/created:
2075 ** regular files: (mode & 0170000)==0100000
2076 ** symbolic links: (mode & 0170000)==0120000
2077 ** directories: (mode & 0170000)==0040000
2079 ** For a directory, the DATA is ignored. For a symbolic link, it is
2080 ** interpreted as text and used as the target of the link. For a
2081 ** regular file, it is interpreted as a blob and written into the
2082 ** named file. Regardless of the type of file, its permissions are
2083 ** set to (mode & 0777) before returning.
2085 ** If the optional MTIME argument is present, then it is interpreted
2086 ** as an integer - the number of seconds since the unix epoch. The
2087 ** modification-time of the target file is set to this value before
2090 ** If three or more arguments are passed to this function and an
2091 ** error is encountered, an exception is raised.
2095 ** Read and return the contents of file FILE (type blob) from disk.
2101 ** SELECT * FROM fsdir($path [, $dir]);
2103 ** Parameter $path is an absolute or relative pathname. If the file that it
2104 ** refers to does not exist, it is an error. If the path refers to a regular
2105 ** file or symbolic link, it returns a single row. Or, if the path refers
2106 ** to a directory, it returns one row for the directory, and one row for each
2107 ** file within the hierarchy rooted at $path.
2109 ** Each row has the following columns:
2111 ** name: Path to file or directory (text value).
2112 ** mode: Value of stat.st_mode for directory entry (an integer).
2113 ** mtime: Value of stat.st_mtime for directory entry (an integer).
2114 ** data: For a regular file, a blob containing the file data. For a
2115 ** symlink, a text value containing the text of the link. For a
2118 ** If a non-NULL value is specified for the optional $dir parameter and
2119 ** $path is a relative path, then $path is interpreted relative to $dir.
2120 ** And the paths returned in the "name" column of the table are also
2121 ** relative to directory $dir.
2123 SQLITE_EXTENSION_INIT1
2128 #include <sys/types.h>
2129 #include <sys/stat.h>
2131 #if !defined(_WIN32) && !defined(WIN32)
2132 # include <unistd.h>
2133 # include <dirent.h>
2135 # include <sys/time.h>
2137 # include "windows.h"
2139 # include <direct.h>
2140 /* # include "test_windirent.h" */
2141 # define dirent DIRENT
2145 # define mkdir(path,mode) _mkdir(path)
2146 # define lstat(path,buf) stat(path,buf)
2152 #define FSDIR_SCHEMA "(name,mode,mtime,data,path HIDDEN,dir HIDDEN)"
2155 ** Set the result stored by context ctx to a blob containing the
2156 ** contents of file zName.
2158 static void readFileContents(sqlite3_context *ctx, const char *zName){
2163 in = fopen(zName, "rb");
2165 fseek(in, 0, SEEK_END);
2168 pBuf = sqlite3_malloc( nIn );
2169 if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
2170 sqlite3_result_blob(ctx, pBuf, nIn, sqlite3_free);
2178 ** Implementation of the "readfile(X)" SQL function. The entire content
2179 ** of the file named X is read and returned as a BLOB. NULL is returned
2180 ** if the file does not exist or is unreadable.
2182 static void readfileFunc(
2183 sqlite3_context *context,
2185 sqlite3_value **argv
2188 (void)(argc); /* Unused parameter */
2189 zName = (const char*)sqlite3_value_text(argv[0]);
2190 if( zName==0 ) return;
2191 readFileContents(context, zName);
2195 ** Set the error message contained in context ctx to the results of
2196 ** vprintf(zFmt, ...).
2198 static void ctxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
2202 zMsg = sqlite3_vmprintf(zFmt, ap);
2203 sqlite3_result_error(ctx, zMsg, -1);
2209 ** Argument zFile is the name of a file that will be created and/or written
2210 ** by SQL function writefile(). This function ensures that the directory
2211 ** zFile will be written to exists, creating it if required. The permissions
2212 ** for any path components created by this function are set to (mode&0777).
2214 ** If an OOM condition is encountered, SQLITE_NOMEM is returned. Otherwise,
2215 ** SQLITE_OK is returned if the directory is successfully created, or
2216 ** SQLITE_ERROR otherwise.
2218 static int makeDirectory(
2222 char *zCopy = sqlite3_mprintf("%s", zFile);
2228 int nCopy = (int)strlen(zCopy);
2231 while( rc==SQLITE_OK ){
2235 for(; zCopy[i]!='/' && i<nCopy; i++);
2236 if( i==nCopy ) break;
2239 rc2 = stat(zCopy, &sStat);
2241 if( mkdir(zCopy, mode & 0777) ) rc = SQLITE_ERROR;
2243 if( !S_ISDIR(sStat.st_mode) ) rc = SQLITE_ERROR;
2249 sqlite3_free(zCopy);
2256 ** This function does the work for the writefile() UDF. Refer to
2257 ** header comments at the top of this file for details.
2259 static int writeFile(
2260 sqlite3_context *pCtx, /* Context to return bytes written in */
2261 const char *zFile, /* File to write */
2262 sqlite3_value *pData, /* Data to write */
2263 mode_t mode, /* MODE parameter passed to writefile() */
2264 sqlite3_int64 mtime /* MTIME parameter (or -1 to not set time) */
2266 #if !defined(_WIN32) && !defined(WIN32)
2267 if( S_ISLNK(mode) ){
2268 const char *zTo = (const char*)sqlite3_value_text(pData);
2269 if( symlink(zTo, zFile)<0 ) return 1;
2273 if( S_ISDIR(mode) ){
2274 if( mkdir(zFile, mode) ){
2275 /* The mkdir() call to create the directory failed. This might not
2276 ** be an error though - if there is already a directory at the same
2277 ** path and either the permissions already match or can be changed
2278 ** to do so using chmod(), it is not an error. */
2281 || 0!=stat(zFile, &sStat)
2282 || !S_ISDIR(sStat.st_mode)
2283 || ((sStat.st_mode&0777)!=(mode&0777) && 0!=chmod(zFile, mode&0777))
2289 sqlite3_int64 nWrite = 0;
2292 FILE *out = fopen(zFile, "wb");
2293 if( out==0 ) return 1;
2294 z = (const char*)sqlite3_value_blob(pData);
2296 sqlite3_int64 n = fwrite(z, 1, sqlite3_value_bytes(pData), out);
2297 nWrite = sqlite3_value_bytes(pData);
2303 if( rc==0 && mode && chmod(zFile, mode & 0777) ){
2307 sqlite3_result_int64(pCtx, nWrite);
2314 FILETIME lastAccess;
2316 SYSTEMTIME currentTime;
2319 GetSystemTime(¤tTime);
2320 SystemTimeToFileTime(¤tTime, &lastAccess);
2321 intervals = Int32x32To64(mtime, 10000000) + 116444736000000000;
2322 lastWrite.dwLowDateTime = (DWORD)intervals;
2323 lastWrite.dwHighDateTime = intervals >> 32;
2325 zFile, FILE_WRITE_ATTRIBUTES, 0, NULL, OPEN_EXISTING,
2326 FILE_FLAG_BACKUP_SEMANTICS, NULL
2328 if( hFile!=INVALID_HANDLE_VALUE ){
2329 BOOL bResult = SetFileTime(hFile, NULL, &lastAccess, &lastWrite);
2335 #elif defined(AT_FDCWD) && 0 /* utimensat() is not univerally available */
2337 struct timespec times[2];
2338 times[0].tv_nsec = times[1].tv_nsec = 0;
2339 times[0].tv_sec = time(0);
2340 times[1].tv_sec = mtime;
2341 if( utimensat(AT_FDCWD, zFile, times, AT_SYMLINK_NOFOLLOW) ){
2346 struct timeval times[2];
2347 times[0].tv_usec = times[1].tv_usec = 0;
2348 times[0].tv_sec = time(0);
2349 times[1].tv_sec = mtime;
2350 if( utimes(zFile, times) ){
2360 ** Implementation of the "writefile(W,X[,Y[,Z]]])" SQL function.
2361 ** Refer to header comments at the top of this file for details.
2363 static void writefileFunc(
2364 sqlite3_context *context,
2366 sqlite3_value **argv
2371 sqlite3_int64 mtime = -1;
2373 if( argc<2 || argc>4 ){
2374 sqlite3_result_error(context,
2375 "wrong number of arguments to function writefile()", -1
2380 zFile = (const char*)sqlite3_value_text(argv[0]);
2381 if( zFile==0 ) return;
2383 mode = (mode_t)sqlite3_value_int(argv[2]);
2386 mtime = sqlite3_value_int64(argv[3]);
2389 res = writeFile(context, zFile, argv[1], mode, mtime);
2390 if( res==1 && errno==ENOENT ){
2391 if( makeDirectory(zFile, mode)==SQLITE_OK ){
2392 res = writeFile(context, zFile, argv[1], mode, mtime);
2396 if( argc>2 && res!=0 ){
2397 if( S_ISLNK(mode) ){
2398 ctxErrorMsg(context, "failed to create symlink: %s", zFile);
2399 }else if( S_ISDIR(mode) ){
2400 ctxErrorMsg(context, "failed to create directory: %s", zFile);
2402 ctxErrorMsg(context, "failed to write file: %s", zFile);
2408 ** SQL function: lsmode(MODE)
2410 ** Given a numberic st_mode from stat(), convert it into a human-readable
2411 ** text string in the style of "ls -l".
2413 static void lsModeFunc(
2414 sqlite3_context *context,
2416 sqlite3_value **argv
2419 int iMode = sqlite3_value_int(argv[0]);
2422 if( S_ISLNK(iMode) ){
2424 }else if( S_ISREG(iMode) ){
2426 }else if( S_ISDIR(iMode) ){
2432 int m = (iMode >> ((2-i)*3));
2433 char *a = &z[1 + i*3];
2434 a[0] = (m & 0x4) ? 'r' : '-';
2435 a[1] = (m & 0x2) ? 'w' : '-';
2436 a[2] = (m & 0x1) ? 'x' : '-';
2439 sqlite3_result_text(context, z, -1, SQLITE_TRANSIENT);
2442 #ifndef SQLITE_OMIT_VIRTUALTABLE
2445 ** Cursor type for recursively iterating through a directory structure.
2447 typedef struct fsdir_cursor fsdir_cursor;
2448 typedef struct FsdirLevel FsdirLevel;
2451 DIR *pDir; /* From opendir() */
2452 char *zDir; /* Name of directory (nul-terminated) */
2455 struct fsdir_cursor {
2456 sqlite3_vtab_cursor base; /* Base class - must be first */
2458 int nLvl; /* Number of entries in aLvl[] array */
2459 int iLvl; /* Index of current entry */
2460 FsdirLevel *aLvl; /* Hierarchy of directories being traversed */
2465 struct stat sStat; /* Current lstat() results */
2466 char *zPath; /* Path to current entry */
2467 sqlite3_int64 iRowid; /* Current rowid */
2470 typedef struct fsdir_tab fsdir_tab;
2472 sqlite3_vtab base; /* Base class - must be first */
2476 ** Construct a new fsdir virtual table object.
2478 static int fsdirConnect(
2481 int argc, const char *const*argv,
2482 sqlite3_vtab **ppVtab,
2485 fsdir_tab *pNew = 0;
2491 rc = sqlite3_declare_vtab(db, "CREATE TABLE x" FSDIR_SCHEMA);
2492 if( rc==SQLITE_OK ){
2493 pNew = (fsdir_tab*)sqlite3_malloc( sizeof(*pNew) );
2494 if( pNew==0 ) return SQLITE_NOMEM;
2495 memset(pNew, 0, sizeof(*pNew));
2497 *ppVtab = (sqlite3_vtab*)pNew;
2502 ** This method is the destructor for fsdir vtab objects.
2504 static int fsdirDisconnect(sqlite3_vtab *pVtab){
2505 sqlite3_free(pVtab);
2510 ** Constructor for a new fsdir_cursor object.
2512 static int fsdirOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
2515 pCur = sqlite3_malloc( sizeof(*pCur) );
2516 if( pCur==0 ) return SQLITE_NOMEM;
2517 memset(pCur, 0, sizeof(*pCur));
2519 *ppCursor = &pCur->base;
2524 ** Reset a cursor back to the state it was in when first returned
2527 static void fsdirResetCursor(fsdir_cursor *pCur){
2529 for(i=0; i<=pCur->iLvl; i++){
2530 FsdirLevel *pLvl = &pCur->aLvl[i];
2531 if( pLvl->pDir ) closedir(pLvl->pDir);
2532 sqlite3_free(pLvl->zDir);
2534 sqlite3_free(pCur->zPath);
2544 ** Destructor for an fsdir_cursor.
2546 static int fsdirClose(sqlite3_vtab_cursor *cur){
2547 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2549 fsdirResetCursor(pCur);
2550 sqlite3_free(pCur->aLvl);
2556 ** Set the error message for the virtual table associated with cursor
2557 ** pCur to the results of vprintf(zFmt, ...).
2559 static void fsdirSetErrmsg(fsdir_cursor *pCur, const char *zFmt, ...){
2562 pCur->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
2568 ** Advance an fsdir_cursor to its next row of output.
2570 static int fsdirNext(sqlite3_vtab_cursor *cur){
2571 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2572 mode_t m = pCur->sStat.st_mode;
2576 /* Descend into this directory */
2577 int iNew = pCur->iLvl + 1;
2579 if( iNew>=pCur->nLvl ){
2581 int nByte = nNew*sizeof(FsdirLevel);
2582 FsdirLevel *aNew = (FsdirLevel*)sqlite3_realloc(pCur->aLvl, nByte);
2583 if( aNew==0 ) return SQLITE_NOMEM;
2584 memset(&aNew[pCur->nLvl], 0, sizeof(FsdirLevel)*(nNew-pCur->nLvl));
2589 pLvl = &pCur->aLvl[iNew];
2591 pLvl->zDir = pCur->zPath;
2593 pLvl->pDir = opendir(pLvl->zDir);
2594 if( pLvl->pDir==0 ){
2595 fsdirSetErrmsg(pCur, "cannot read directory: %s", pCur->zPath);
2596 return SQLITE_ERROR;
2600 while( pCur->iLvl>=0 ){
2601 FsdirLevel *pLvl = &pCur->aLvl[pCur->iLvl];
2602 struct dirent *pEntry = readdir(pLvl->pDir);
2604 if( pEntry->d_name[0]=='.' ){
2605 if( pEntry->d_name[1]=='.' && pEntry->d_name[2]=='\0' ) continue;
2606 if( pEntry->d_name[1]=='\0' ) continue;
2608 sqlite3_free(pCur->zPath);
2609 pCur->zPath = sqlite3_mprintf("%s/%s", pLvl->zDir, pEntry->d_name);
2610 if( pCur->zPath==0 ) return SQLITE_NOMEM;
2611 if( lstat(pCur->zPath, &pCur->sStat) ){
2612 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2613 return SQLITE_ERROR;
2617 closedir(pLvl->pDir);
2618 sqlite3_free(pLvl->zDir);
2625 sqlite3_free(pCur->zPath);
2631 ** Return values of columns for the row at which the series_cursor
2632 ** is currently pointing.
2634 static int fsdirColumn(
2635 sqlite3_vtab_cursor *cur, /* The cursor */
2636 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
2637 int i /* Which column to return */
2639 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2641 case 0: { /* name */
2642 sqlite3_result_text(ctx, &pCur->zPath[pCur->nBase], -1, SQLITE_TRANSIENT);
2647 sqlite3_result_int64(ctx, pCur->sStat.st_mode);
2651 sqlite3_result_int64(ctx, pCur->sStat.st_mtime);
2654 case 3: { /* data */
2655 mode_t m = pCur->sStat.st_mode;
2657 sqlite3_result_null(ctx);
2658 #if !defined(_WIN32) && !defined(WIN32)
2659 }else if( S_ISLNK(m) ){
2661 char *aBuf = aStatic;
2666 n = readlink(pCur->zPath, aBuf, nBuf);
2668 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2670 aBuf = sqlite3_malloc(nBuf);
2672 sqlite3_result_error_nomem(ctx);
2673 return SQLITE_NOMEM;
2677 sqlite3_result_text(ctx, aBuf, n, SQLITE_TRANSIENT);
2678 if( aBuf!=aStatic ) sqlite3_free(aBuf);
2681 readFileContents(ctx, pCur->zPath);
2689 ** Return the rowid for the current row. In this implementation, the
2690 ** first row returned is assigned rowid value 1, and each subsequent
2691 ** row a value 1 more than that of the previous.
2693 static int fsdirRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
2694 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2695 *pRowid = pCur->iRowid;
2700 ** Return TRUE if the cursor has been moved off of the last
2703 static int fsdirEof(sqlite3_vtab_cursor *cur){
2704 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2705 return (pCur->zPath==0);
2709 ** xFilter callback.
2711 static int fsdirFilter(
2712 sqlite3_vtab_cursor *cur,
2713 int idxNum, const char *idxStr,
2714 int argc, sqlite3_value **argv
2716 const char *zDir = 0;
2717 fsdir_cursor *pCur = (fsdir_cursor*)cur;
2719 fsdirResetCursor(pCur);
2722 fsdirSetErrmsg(pCur, "table function fsdir requires an argument");
2723 return SQLITE_ERROR;
2726 assert( argc==idxNum && (argc==1 || argc==2) );
2727 zDir = (const char*)sqlite3_value_text(argv[0]);
2729 fsdirSetErrmsg(pCur, "table function fsdir requires a non-NULL argument");
2730 return SQLITE_ERROR;
2733 pCur->zBase = (const char*)sqlite3_value_text(argv[1]);
2736 pCur->nBase = (int)strlen(pCur->zBase)+1;
2737 pCur->zPath = sqlite3_mprintf("%s/%s", pCur->zBase, zDir);
2739 pCur->zPath = sqlite3_mprintf("%s", zDir);
2742 if( pCur->zPath==0 ){
2743 return SQLITE_NOMEM;
2745 if( lstat(pCur->zPath, &pCur->sStat) ){
2746 fsdirSetErrmsg(pCur, "cannot stat file: %s", pCur->zPath);
2747 return SQLITE_ERROR;
2754 ** SQLite will invoke this method one or more times while planning a query
2755 ** that uses the generate_series virtual table. This routine needs to create
2756 ** a query plan for each invocation and compute an estimated cost for that
2759 ** In this implementation idxNum is used to represent the
2760 ** query plan. idxStr is unused.
2762 ** The query plan is represented by bits in idxNum:
2764 ** (1) start = $value -- constraint exists
2765 ** (2) stop = $value -- constraint exists
2766 ** (4) step = $value -- constraint exists
2767 ** (8) output in descending order
2769 static int fsdirBestIndex(
2771 sqlite3_index_info *pIdxInfo
2773 int i; /* Loop over constraints */
2776 const struct sqlite3_index_constraint *pConstraint;
2779 pConstraint = pIdxInfo->aConstraint;
2780 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
2781 if( pConstraint->usable==0 ) continue;
2782 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
2783 if( pConstraint->iColumn==4 ) idx4 = i;
2784 if( pConstraint->iColumn==5 ) idx5 = i;
2788 pIdxInfo->idxNum = 0;
2789 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
2791 pIdxInfo->aConstraintUsage[idx4].omit = 1;
2792 pIdxInfo->aConstraintUsage[idx4].argvIndex = 1;
2794 pIdxInfo->aConstraintUsage[idx5].omit = 1;
2795 pIdxInfo->aConstraintUsage[idx5].argvIndex = 2;
2796 pIdxInfo->idxNum = 2;
2797 pIdxInfo->estimatedCost = 10.0;
2799 pIdxInfo->idxNum = 1;
2800 pIdxInfo->estimatedCost = 100.0;
2808 ** Register the "fsdir" virtual table.
2810 static int fsdirRegister(sqlite3 *db){
2811 static sqlite3_module fsdirModule = {
2814 fsdirConnect, /* xConnect */
2815 fsdirBestIndex, /* xBestIndex */
2816 fsdirDisconnect, /* xDisconnect */
2818 fsdirOpen, /* xOpen - open a cursor */
2819 fsdirClose, /* xClose - close a cursor */
2820 fsdirFilter, /* xFilter - configure scan constraints */
2821 fsdirNext, /* xNext - advance a cursor */
2822 fsdirEof, /* xEof - check for end of scan */
2823 fsdirColumn, /* xColumn - read data */
2824 fsdirRowid, /* xRowid - read data */
2830 0, /* xFindMethod */
2837 int rc = sqlite3_create_module(db, "fsdir", &fsdirModule, 0);
2840 #else /* SQLITE_OMIT_VIRTUALTABLE */
2841 # define fsdirRegister(x) SQLITE_OK
2847 int sqlite3_fileio_init(
2850 const sqlite3_api_routines *pApi
2853 SQLITE_EXTENSION_INIT2(pApi);
2854 (void)pzErrMsg; /* Unused parameter */
2855 rc = sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
2856 readfileFunc, 0, 0);
2857 if( rc==SQLITE_OK ){
2858 rc = sqlite3_create_function(db, "writefile", -1, SQLITE_UTF8, 0,
2859 writefileFunc, 0, 0);
2861 if( rc==SQLITE_OK ){
2862 rc = sqlite3_create_function(db, "lsmode", 1, SQLITE_UTF8, 0,
2865 if( rc==SQLITE_OK ){
2866 rc = fsdirRegister(db);
2871 /************************* End ../ext/misc/fileio.c ********************/
2872 /************************* Begin ../ext/misc/completion.c ******************/
2876 ** The author disclaims copyright to this source code. In place of
2877 ** a legal notice, here is a blessing:
2879 ** May you do good and not evil.
2880 ** May you find forgiveness for yourself and forgive others.
2881 ** May you share freely, never taking more than you give.
2883 *************************************************************************
2885 ** This file implements an eponymous virtual table that returns suggested
2886 ** completions for a partial SQL input.
2890 ** SELECT DISTINCT candidate COLLATE nocase
2891 ** FROM completion($prefix,$wholeline)
2894 ** The two query parameters are optional. $prefix is the text of the
2895 ** current word being typed and that is to be completed. $wholeline is
2896 ** the complete input line, used for context.
2898 ** The raw completion() table might return the same candidate multiple
2899 ** times, for example if the same column name is used to two or more
2900 ** tables. And the candidates are returned in an arbitrary order. Hence,
2901 ** the DISTINCT and ORDER BY are recommended.
2903 ** This virtual table operates at the speed of human typing, and so there
2904 ** is no attempt to make it fast. Even a slow implementation will be much
2905 ** faster than any human can type.
2908 SQLITE_EXTENSION_INIT1
2913 #ifndef SQLITE_OMIT_VIRTUALTABLE
2915 /* completion_vtab is a subclass of sqlite3_vtab which will
2916 ** serve as the underlying representation of a completion virtual table
2918 typedef struct completion_vtab completion_vtab;
2919 struct completion_vtab {
2920 sqlite3_vtab base; /* Base class - must be first */
2921 sqlite3 *db; /* Database connection for this completion vtab */
2924 /* completion_cursor is a subclass of sqlite3_vtab_cursor which will
2925 ** serve as the underlying representation of a cursor that scans
2926 ** over rows of the result
2928 typedef struct completion_cursor completion_cursor;
2929 struct completion_cursor {
2930 sqlite3_vtab_cursor base; /* Base class - must be first */
2931 sqlite3 *db; /* Database connection for this cursor */
2932 int nPrefix, nLine; /* Number of bytes in zPrefix and zLine */
2933 char *zPrefix; /* The prefix for the word we want to complete */
2934 char *zLine; /* The whole that we want to complete */
2935 const char *zCurrentRow; /* Current output row */
2936 sqlite3_stmt *pStmt; /* Current statement */
2937 sqlite3_int64 iRowid; /* The rowid */
2938 int ePhase; /* Current phase */
2939 int j; /* inter-phase counter */
2942 /* Values for ePhase:
2944 #define COMPLETION_FIRST_PHASE 1
2945 #define COMPLETION_KEYWORDS 1
2946 #define COMPLETION_PRAGMAS 2
2947 #define COMPLETION_FUNCTIONS 3
2948 #define COMPLETION_COLLATIONS 4
2949 #define COMPLETION_INDEXES 5
2950 #define COMPLETION_TRIGGERS 6
2951 #define COMPLETION_DATABASES 7
2952 #define COMPLETION_TABLES 8
2953 #define COMPLETION_COLUMNS 9
2954 #define COMPLETION_MODULES 10
2955 #define COMPLETION_EOF 11
2958 ** The completionConnect() method is invoked to create a new
2959 ** completion_vtab that describes the completion virtual table.
2961 ** Think of this routine as the constructor for completion_vtab objects.
2963 ** All this routine needs to do is:
2965 ** (1) Allocate the completion_vtab object and initialize all fields.
2967 ** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
2968 ** result set of queries against completion will look like.
2970 static int completionConnect(
2973 int argc, const char *const*argv,
2974 sqlite3_vtab **ppVtab,
2977 completion_vtab *pNew;
2980 (void)(pAux); /* Unused parameter */
2981 (void)(argc); /* Unused parameter */
2982 (void)(argv); /* Unused parameter */
2983 (void)(pzErr); /* Unused parameter */
2985 /* Column numbers */
2986 #define COMPLETION_COLUMN_CANDIDATE 0 /* Suggested completion of the input */
2987 #define COMPLETION_COLUMN_PREFIX 1 /* Prefix of the word to be completed */
2988 #define COMPLETION_COLUMN_WHOLELINE 2 /* Entire line seen so far */
2989 #define COMPLETION_COLUMN_PHASE 3 /* ePhase - used for debugging only */
2991 rc = sqlite3_declare_vtab(db,
2994 " prefix TEXT HIDDEN,"
2995 " wholeline TEXT HIDDEN,"
2996 " phase INT HIDDEN" /* Used for debugging only */
2998 if( rc==SQLITE_OK ){
2999 pNew = sqlite3_malloc( sizeof(*pNew) );
3000 *ppVtab = (sqlite3_vtab*)pNew;
3001 if( pNew==0 ) return SQLITE_NOMEM;
3002 memset(pNew, 0, sizeof(*pNew));
3009 ** This method is the destructor for completion_cursor objects.
3011 static int completionDisconnect(sqlite3_vtab *pVtab){
3012 sqlite3_free(pVtab);
3017 ** Constructor for a new completion_cursor object.
3019 static int completionOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
3020 completion_cursor *pCur;
3021 pCur = sqlite3_malloc( sizeof(*pCur) );
3022 if( pCur==0 ) return SQLITE_NOMEM;
3023 memset(pCur, 0, sizeof(*pCur));
3024 pCur->db = ((completion_vtab*)p)->db;
3025 *ppCursor = &pCur->base;
3030 ** Reset the completion_cursor.
3032 static void completionCursorReset(completion_cursor *pCur){
3033 sqlite3_free(pCur->zPrefix); pCur->zPrefix = 0; pCur->nPrefix = 0;
3034 sqlite3_free(pCur->zLine); pCur->zLine = 0; pCur->nLine = 0;
3035 sqlite3_finalize(pCur->pStmt); pCur->pStmt = 0;
3040 ** Destructor for a completion_cursor.
3042 static int completionClose(sqlite3_vtab_cursor *cur){
3043 completionCursorReset((completion_cursor*)cur);
3049 ** All SQL keywords understood by SQLite
3051 static const char *completionKwrds[] = {
3052 "ABORT", "ACTION", "ADD", "AFTER", "ALL", "ALTER", "ANALYZE", "AND", "AS",
3053 "ASC", "ATTACH", "AUTOINCREMENT", "BEFORE", "BEGIN", "BETWEEN", "BY",
3054 "CASCADE", "CASE", "CAST", "CHECK", "COLLATE", "COLUMN", "COMMIT",
3055 "CONFLICT", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_DATE",
3056 "CURRENT_TIME", "CURRENT_TIMESTAMP", "DATABASE", "DEFAULT", "DEFERRABLE",
3057 "DEFERRED", "DELETE", "DESC", "DETACH", "DISTINCT", "DROP", "EACH",
3058 "ELSE", "END", "ESCAPE", "EXCEPT", "EXCLUSIVE", "EXISTS", "EXPLAIN",
3059 "FAIL", "FOR", "FOREIGN", "FROM", "FULL", "GLOB", "GROUP", "HAVING", "IF",
3060 "IGNORE", "IMMEDIATE", "IN", "INDEX", "INDEXED", "INITIALLY", "INNER",
3061 "INSERT", "INSTEAD", "INTERSECT", "INTO", "IS", "ISNULL", "JOIN", "KEY",
3062 "LEFT", "LIKE", "LIMIT", "MATCH", "NATURAL", "NO", "NOT", "NOTNULL",
3063 "NULL", "OF", "OFFSET", "ON", "OR", "ORDER", "OUTER", "PLAN", "PRAGMA",
3064 "PRIMARY", "QUERY", "RAISE", "RECURSIVE", "REFERENCES", "REGEXP",
3065 "REINDEX", "RELEASE", "RENAME", "REPLACE", "RESTRICT", "RIGHT",
3066 "ROLLBACK", "ROW", "SAVEPOINT", "SELECT", "SET", "TABLE", "TEMP",
3067 "TEMPORARY", "THEN", "TO", "TRANSACTION", "TRIGGER", "UNION", "UNIQUE",
3068 "UPDATE", "USING", "VACUUM", "VALUES", "VIEW", "VIRTUAL", "WHEN", "WHERE",
3071 #define completionKwCount \
3072 (int)(sizeof(completionKwrds)/sizeof(completionKwrds[0]))
3075 ** Advance a completion_cursor to its next row of output.
3077 ** The ->ePhase, ->j, and ->pStmt fields of the completion_cursor object
3078 ** record the current state of the scan. This routine sets ->zCurrentRow
3079 ** to the current row of output and then returns. If no more rows remain,
3080 ** then ->ePhase is set to COMPLETION_EOF which will signal the virtual
3081 ** table that has reached the end of its scan.
3083 ** The current implementation just lists potential identifiers and
3084 ** keywords and filters them by zPrefix. Future enhancements should
3085 ** take zLine into account to try to restrict the set of identifiers and
3086 ** keywords based on what would be legal at the current point of input.
3088 static int completionNext(sqlite3_vtab_cursor *cur){
3089 completion_cursor *pCur = (completion_cursor*)cur;
3090 int eNextPhase = 0; /* Next phase to try if current phase reaches end */
3091 int iCol = -1; /* If >=0, step pCur->pStmt and use the i-th column */
3093 while( pCur->ePhase!=COMPLETION_EOF ){
3094 switch( pCur->ePhase ){
3095 case COMPLETION_KEYWORDS: {
3096 if( pCur->j >= completionKwCount ){
3097 pCur->zCurrentRow = 0;
3098 pCur->ePhase = COMPLETION_DATABASES;
3100 pCur->zCurrentRow = completionKwrds[pCur->j++];
3105 case COMPLETION_DATABASES: {
3106 if( pCur->pStmt==0 ){
3107 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1,
3111 eNextPhase = COMPLETION_TABLES;
3114 case COMPLETION_TABLES: {
3115 if( pCur->pStmt==0 ){
3118 const char *zSep = "";
3119 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3120 while( sqlite3_step(pS2)==SQLITE_ROW ){
3121 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3122 zSql = sqlite3_mprintf(
3124 "SELECT name FROM \"%w\".sqlite_master"
3125 " WHERE type='table'",
3128 if( zSql==0 ) return SQLITE_NOMEM;
3131 sqlite3_finalize(pS2);
3132 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3136 eNextPhase = COMPLETION_COLUMNS;
3139 case COMPLETION_COLUMNS: {
3140 if( pCur->pStmt==0 ){
3143 const char *zSep = "";
3144 sqlite3_prepare_v2(pCur->db, "PRAGMA database_list", -1, &pS2, 0);
3145 while( sqlite3_step(pS2)==SQLITE_ROW ){
3146 const char *zDb = (const char*)sqlite3_column_text(pS2, 1);
3147 zSql = sqlite3_mprintf(
3149 "SELECT pti.name FROM \"%w\".sqlite_master AS sm"
3150 " JOIN pragma_table_info(sm.name,%Q) AS pti"
3151 " WHERE sm.type='table'",
3152 zSql, zSep, zDb, zDb
3154 if( zSql==0 ) return SQLITE_NOMEM;
3157 sqlite3_finalize(pS2);
3158 sqlite3_prepare_v2(pCur->db, zSql, -1, &pCur->pStmt, 0);
3162 eNextPhase = COMPLETION_EOF;
3167 /* This case is when the phase presets zCurrentRow */
3168 if( pCur->zCurrentRow==0 ) continue;
3170 if( sqlite3_step(pCur->pStmt)==SQLITE_ROW ){
3171 /* Extract the next row of content */
3172 pCur->zCurrentRow = (const char*)sqlite3_column_text(pCur->pStmt, iCol);
3174 /* When all rows are finished, advance to the next phase */
3175 sqlite3_finalize(pCur->pStmt);
3177 pCur->ePhase = eNextPhase;
3181 if( pCur->nPrefix==0 ) break;
3182 if( sqlite3_strnicmp(pCur->zPrefix, pCur->zCurrentRow, pCur->nPrefix)==0 ){
3191 ** Return values of columns for the row at which the completion_cursor
3192 ** is currently pointing.
3194 static int completionColumn(
3195 sqlite3_vtab_cursor *cur, /* The cursor */
3196 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
3197 int i /* Which column to return */
3199 completion_cursor *pCur = (completion_cursor*)cur;
3201 case COMPLETION_COLUMN_CANDIDATE: {
3202 sqlite3_result_text(ctx, pCur->zCurrentRow, -1, SQLITE_TRANSIENT);
3205 case COMPLETION_COLUMN_PREFIX: {
3206 sqlite3_result_text(ctx, pCur->zPrefix, -1, SQLITE_TRANSIENT);
3209 case COMPLETION_COLUMN_WHOLELINE: {
3210 sqlite3_result_text(ctx, pCur->zLine, -1, SQLITE_TRANSIENT);
3213 case COMPLETION_COLUMN_PHASE: {
3214 sqlite3_result_int(ctx, pCur->ePhase);
3222 ** Return the rowid for the current row. In this implementation, the
3223 ** rowid is the same as the output value.
3225 static int completionRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
3226 completion_cursor *pCur = (completion_cursor*)cur;
3227 *pRowid = pCur->iRowid;
3232 ** Return TRUE if the cursor has been moved off of the last
3235 static int completionEof(sqlite3_vtab_cursor *cur){
3236 completion_cursor *pCur = (completion_cursor*)cur;
3237 return pCur->ePhase >= COMPLETION_EOF;
3241 ** This method is called to "rewind" the completion_cursor object back
3242 ** to the first row of output. This method is always called at least
3243 ** once prior to any call to completionColumn() or completionRowid() or
3246 static int completionFilter(
3247 sqlite3_vtab_cursor *pVtabCursor,
3248 int idxNum, const char *idxStr,
3249 int argc, sqlite3_value **argv
3251 completion_cursor *pCur = (completion_cursor *)pVtabCursor;
3253 (void)(idxStr); /* Unused parameter */
3254 (void)(argc); /* Unused parameter */
3255 completionCursorReset(pCur);
3257 pCur->nPrefix = sqlite3_value_bytes(argv[iArg]);
3258 if( pCur->nPrefix>0 ){
3259 pCur->zPrefix = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3260 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3265 pCur->nLine = sqlite3_value_bytes(argv[iArg]);
3266 if( pCur->nLine>0 ){
3267 pCur->zLine = sqlite3_mprintf("%s", sqlite3_value_text(argv[iArg]));
3268 if( pCur->zLine==0 ) return SQLITE_NOMEM;
3272 if( pCur->zLine!=0 && pCur->zPrefix==0 ){
3273 int i = pCur->nLine;
3274 while( i>0 && (isalnum(pCur->zLine[i-1]) || pCur->zLine[i-1]=='_') ){
3277 pCur->nPrefix = pCur->nLine - i;
3278 if( pCur->nPrefix>0 ){
3279 pCur->zPrefix = sqlite3_mprintf("%.*s", pCur->nPrefix, pCur->zLine + i);
3280 if( pCur->zPrefix==0 ) return SQLITE_NOMEM;
3284 pCur->ePhase = COMPLETION_FIRST_PHASE;
3285 return completionNext(pVtabCursor);
3289 ** SQLite will invoke this method one or more times while planning a query
3290 ** that uses the completion virtual table. This routine needs to create
3291 ** a query plan for each invocation and compute an estimated cost for that
3294 ** There are two hidden parameters that act as arguments to the table-valued
3295 ** function: "prefix" and "wholeline". Bit 0 of idxNum is set if "prefix"
3296 ** is available and bit 1 is set if "wholeline" is available.
3298 static int completionBestIndex(
3300 sqlite3_index_info *pIdxInfo
3302 int i; /* Loop over constraints */
3303 int idxNum = 0; /* The query plan bitmask */
3304 int prefixIdx = -1; /* Index of the start= constraint, or -1 if none */
3305 int wholelineIdx = -1; /* Index of the stop= constraint, or -1 if none */
3306 int nArg = 0; /* Number of arguments that completeFilter() expects */
3307 const struct sqlite3_index_constraint *pConstraint;
3309 (void)(tab); /* Unused parameter */
3310 pConstraint = pIdxInfo->aConstraint;
3311 for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
3312 if( pConstraint->usable==0 ) continue;
3313 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
3314 switch( pConstraint->iColumn ){
3315 case COMPLETION_COLUMN_PREFIX:
3319 case COMPLETION_COLUMN_WHOLELINE:
3326 pIdxInfo->aConstraintUsage[prefixIdx].argvIndex = ++nArg;
3327 pIdxInfo->aConstraintUsage[prefixIdx].omit = 1;
3329 if( wholelineIdx>=0 ){
3330 pIdxInfo->aConstraintUsage[wholelineIdx].argvIndex = ++nArg;
3331 pIdxInfo->aConstraintUsage[wholelineIdx].omit = 1;
3333 pIdxInfo->idxNum = idxNum;
3334 pIdxInfo->estimatedCost = (double)5000 - 1000*nArg;
3335 pIdxInfo->estimatedRows = 500 - 100*nArg;
3340 ** This following structure defines all the methods for the
3341 ** completion virtual table.
3343 static sqlite3_module completionModule = {
3346 completionConnect, /* xConnect */
3347 completionBestIndex, /* xBestIndex */
3348 completionDisconnect, /* xDisconnect */
3350 completionOpen, /* xOpen - open a cursor */
3351 completionClose, /* xClose - close a cursor */
3352 completionFilter, /* xFilter - configure scan constraints */
3353 completionNext, /* xNext - advance a cursor */
3354 completionEof, /* xEof - check for end of scan */
3355 completionColumn, /* xColumn - read data */
3356 completionRowid, /* xRowid - read data */
3362 0, /* xFindMethod */
3369 #endif /* SQLITE_OMIT_VIRTUALTABLE */
3371 int sqlite3CompletionVtabInit(sqlite3 *db){
3373 #ifndef SQLITE_OMIT_VIRTUALTABLE
3374 rc = sqlite3_create_module(db, "completion", &completionModule, 0);
3382 int sqlite3_completion_init(
3385 const sqlite3_api_routines *pApi
3388 SQLITE_EXTENSION_INIT2(pApi);
3389 (void)(pzErrMsg); /* Unused parameter */
3390 #ifndef SQLITE_OMIT_VIRTUALTABLE
3391 rc = sqlite3CompletionVtabInit(db);
3396 /************************* End ../ext/misc/completion.c ********************/
3397 /************************* Begin ../ext/misc/appendvfs.c ******************/
3401 ** The author disclaims copyright to this source code. In place of
3402 ** a legal notice, here is a blessing:
3404 ** May you do good and not evil.
3405 ** May you find forgiveness for yourself and forgive others.
3406 ** May you share freely, never taking more than you give.
3408 ******************************************************************************
3410 ** This file implements a VFS shim that allows an SQLite database to be
3411 ** appended onto the end of some other file, such as an executable.
3413 ** A special record must appear at the end of the file that identifies the
3414 ** file as an appended database and provides an offset to page 1. For
3415 ** best performance page 1 should be located at a disk page boundary, though
3416 ** that is not required.
3418 ** When opening a database using this VFS, the connection might treat
3419 ** the file as an ordinary SQLite database, or it might treat is as a
3420 ** database appended onto some other file. Here are the rules:
3422 ** (1) When opening a new empty file, that file is treated as an ordinary
3425 ** (2) When opening a file that begins with the standard SQLite prefix
3426 ** string "SQLite format 3", that file is treated as an ordinary
3429 ** (3) When opening a file that ends with the appendvfs trailer string
3430 ** "Start-Of-SQLite3-NNNNNNNN" that file is treated as an appended
3433 ** (4) If none of the above apply and the SQLITE_OPEN_CREATE flag is
3434 ** set, then a new database is appended to the already existing file.
3436 ** (5) Otherwise, SQLITE_CANTOPEN is returned.
3438 ** To avoid unnecessary complications with the PENDING_BYTE, the size of
3439 ** the file containing the database is limited to 1GB. This VFS will refuse
3440 ** to read or write past the 1GB mark. This restriction might be lifted in
3441 ** future versions. For now, if you need a large database, then keep the
3442 ** database in a separate file.
3444 ** If the file being opened is not an appended database, then this shim is
3445 ** a pass-through into the default underlying VFS.
3447 SQLITE_EXTENSION_INIT1
3451 /* The append mark at the end of the database is:
3453 ** Start-Of-SQLite3-NNNNNNNN
3454 ** 123456789 123456789 12345
3456 ** The NNNNNNNN represents a 64-bit big-endian unsigned integer which is
3457 ** the offset to page 1.
3459 #define APND_MARK_PREFIX "Start-Of-SQLite3-"
3460 #define APND_MARK_PREFIX_SZ 17
3461 #define APND_MARK_SIZE 25
3464 ** Maximum size of the combined prefix + database + append-mark. This
3465 ** must be less than 0x40000000 to avoid locking issues on Windows.
3467 #define APND_MAX_SIZE (65536*15259)
3470 ** Forward declaration of objects used by this utility
3472 typedef struct sqlite3_vfs ApndVfs;
3473 typedef struct ApndFile ApndFile;
3475 /* Access to a lower-level VFS that (might) implement dynamic loading,
3476 ** access to randomness, etc.
3478 #define ORIGVFS(p) ((sqlite3_vfs*)((p)->pAppData))
3479 #define ORIGFILE(p) ((sqlite3_file*)(((ApndFile*)(p))+1))
3483 sqlite3_file base; /* IO methods */
3484 sqlite3_int64 iPgOne; /* File offset to page 1 */
3485 sqlite3_int64 iMark; /* Start of the append-mark */
3489 ** Methods for ApndFile
3491 static int apndClose(sqlite3_file*);
3492 static int apndRead(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
3493 static int apndWrite(sqlite3_file*,const void*,int iAmt, sqlite3_int64 iOfst);
3494 static int apndTruncate(sqlite3_file*, sqlite3_int64 size);
3495 static int apndSync(sqlite3_file*, int flags);
3496 static int apndFileSize(sqlite3_file*, sqlite3_int64 *pSize);
3497 static int apndLock(sqlite3_file*, int);
3498 static int apndUnlock(sqlite3_file*, int);
3499 static int apndCheckReservedLock(sqlite3_file*, int *pResOut);
3500 static int apndFileControl(sqlite3_file*, int op, void *pArg);
3501 static int apndSectorSize(sqlite3_file*);
3502 static int apndDeviceCharacteristics(sqlite3_file*);
3503 static int apndShmMap(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
3504 static int apndShmLock(sqlite3_file*, int offset, int n, int flags);
3505 static void apndShmBarrier(sqlite3_file*);
3506 static int apndShmUnmap(sqlite3_file*, int deleteFlag);
3507 static int apndFetch(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
3508 static int apndUnfetch(sqlite3_file*, sqlite3_int64 iOfst, void *p);
3511 ** Methods for ApndVfs
3513 static int apndOpen(sqlite3_vfs*, const char *, sqlite3_file*, int , int *);
3514 static int apndDelete(sqlite3_vfs*, const char *zName, int syncDir);
3515 static int apndAccess(sqlite3_vfs*, const char *zName, int flags, int *);
3516 static int apndFullPathname(sqlite3_vfs*, const char *zName, int, char *zOut);
3517 static void *apndDlOpen(sqlite3_vfs*, const char *zFilename);
3518 static void apndDlError(sqlite3_vfs*, int nByte, char *zErrMsg);
3519 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char*zSym))(void);
3520 static void apndDlClose(sqlite3_vfs*, void*);
3521 static int apndRandomness(sqlite3_vfs*, int nByte, char *zOut);
3522 static int apndSleep(sqlite3_vfs*, int microseconds);
3523 static int apndCurrentTime(sqlite3_vfs*, double*);
3524 static int apndGetLastError(sqlite3_vfs*, int, char *);
3525 static int apndCurrentTimeInt64(sqlite3_vfs*, sqlite3_int64*);
3526 static int apndSetSystemCall(sqlite3_vfs*, const char*,sqlite3_syscall_ptr);
3527 static sqlite3_syscall_ptr apndGetSystemCall(sqlite3_vfs*, const char *z);
3528 static const char *apndNextSystemCall(sqlite3_vfs*, const char *zName);
3530 static sqlite3_vfs apnd_vfs = {
3531 3, /* iVersion (set when registered) */
3532 0, /* szOsFile (set when registered) */
3533 1024, /* mxPathname */
3535 "apndvfs", /* zName */
3536 0, /* pAppData (set when registered) */
3537 apndOpen, /* xOpen */
3538 apndDelete, /* xDelete */
3539 apndAccess, /* xAccess */
3540 apndFullPathname, /* xFullPathname */
3541 apndDlOpen, /* xDlOpen */
3542 apndDlError, /* xDlError */
3543 apndDlSym, /* xDlSym */
3544 apndDlClose, /* xDlClose */
3545 apndRandomness, /* xRandomness */
3546 apndSleep, /* xSleep */
3547 apndCurrentTime, /* xCurrentTime */
3548 apndGetLastError, /* xGetLastError */
3549 apndCurrentTimeInt64, /* xCurrentTimeInt64 */
3550 apndSetSystemCall, /* xSetSystemCall */
3551 apndGetSystemCall, /* xGetSystemCall */
3552 apndNextSystemCall /* xNextSystemCall */
3555 static const sqlite3_io_methods apnd_io_methods = {
3557 apndClose, /* xClose */
3558 apndRead, /* xRead */
3559 apndWrite, /* xWrite */
3560 apndTruncate, /* xTruncate */
3561 apndSync, /* xSync */
3562 apndFileSize, /* xFileSize */
3563 apndLock, /* xLock */
3564 apndUnlock, /* xUnlock */
3565 apndCheckReservedLock, /* xCheckReservedLock */
3566 apndFileControl, /* xFileControl */
3567 apndSectorSize, /* xSectorSize */
3568 apndDeviceCharacteristics, /* xDeviceCharacteristics */
3569 apndShmMap, /* xShmMap */
3570 apndShmLock, /* xShmLock */
3571 apndShmBarrier, /* xShmBarrier */
3572 apndShmUnmap, /* xShmUnmap */
3573 apndFetch, /* xFetch */
3574 apndUnfetch /* xUnfetch */
3580 ** Close an apnd-file.
3582 static int apndClose(sqlite3_file *pFile){
3583 pFile = ORIGFILE(pFile);
3584 return pFile->pMethods->xClose(pFile);
3588 ** Read data from an apnd-file.
3590 static int apndRead(
3591 sqlite3_file *pFile,
3596 ApndFile *p = (ApndFile *)pFile;
3597 pFile = ORIGFILE(pFile);
3598 return pFile->pMethods->xRead(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3602 ** Add the append-mark onto the end of the file.
3604 static int apndWriteMark(ApndFile *p, sqlite3_file *pFile){
3606 unsigned char a[APND_MARK_SIZE];
3607 memcpy(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ);
3609 a[APND_MARK_PREFIX_SZ+i] = (p->iPgOne >> (56 - i*8)) & 0xff;
3611 return pFile->pMethods->xWrite(pFile, a, APND_MARK_SIZE, p->iMark);
3615 ** Write data to an apnd-file.
3617 static int apndWrite(
3618 sqlite3_file *pFile,
3624 ApndFile *p = (ApndFile *)pFile;
3625 pFile = ORIGFILE(pFile);
3626 if( iOfst+iAmt>=APND_MAX_SIZE ) return SQLITE_FULL;
3627 rc = pFile->pMethods->xWrite(pFile, zBuf, iAmt, iOfst+p->iPgOne);
3628 if( rc==SQLITE_OK && iOfst + iAmt + p->iPgOne > p->iMark ){
3629 sqlite3_int64 sz = 0;
3630 rc = pFile->pMethods->xFileSize(pFile, &sz);
3631 if( rc==SQLITE_OK ){
3632 p->iMark = sz - APND_MARK_SIZE;
3633 if( iOfst + iAmt + p->iPgOne > p->iMark ){
3634 p->iMark = p->iPgOne + iOfst + iAmt;
3635 rc = apndWriteMark(p, pFile);
3643 ** Truncate an apnd-file.
3645 static int apndTruncate(sqlite3_file *pFile, sqlite_int64 size){
3647 ApndFile *p = (ApndFile *)pFile;
3648 pFile = ORIGFILE(pFile);
3649 rc = pFile->pMethods->xTruncate(pFile, size+p->iPgOne+APND_MARK_SIZE);
3650 if( rc==SQLITE_OK ){
3651 p->iMark = p->iPgOne+size;
3652 rc = apndWriteMark(p, pFile);
3658 ** Sync an apnd-file.
3660 static int apndSync(sqlite3_file *pFile, int flags){
3661 pFile = ORIGFILE(pFile);
3662 return pFile->pMethods->xSync(pFile, flags);
3666 ** Return the current file-size of an apnd-file.
3668 static int apndFileSize(sqlite3_file *pFile, sqlite_int64 *pSize){
3669 ApndFile *p = (ApndFile *)pFile;
3671 pFile = ORIGFILE(p);
3672 rc = pFile->pMethods->xFileSize(pFile, pSize);
3673 if( rc==SQLITE_OK && p->iPgOne ){
3674 *pSize -= p->iPgOne + APND_MARK_SIZE;
3680 ** Lock an apnd-file.
3682 static int apndLock(sqlite3_file *pFile, int eLock){
3683 pFile = ORIGFILE(pFile);
3684 return pFile->pMethods->xLock(pFile, eLock);
3688 ** Unlock an apnd-file.
3690 static int apndUnlock(sqlite3_file *pFile, int eLock){
3691 pFile = ORIGFILE(pFile);
3692 return pFile->pMethods->xUnlock(pFile, eLock);
3696 ** Check if another file-handle holds a RESERVED lock on an apnd-file.
3698 static int apndCheckReservedLock(sqlite3_file *pFile, int *pResOut){
3699 pFile = ORIGFILE(pFile);
3700 return pFile->pMethods->xCheckReservedLock(pFile, pResOut);
3704 ** File control method. For custom operations on an apnd-file.
3706 static int apndFileControl(sqlite3_file *pFile, int op, void *pArg){
3707 ApndFile *p = (ApndFile *)pFile;
3709 pFile = ORIGFILE(pFile);
3710 rc = pFile->pMethods->xFileControl(pFile, op, pArg);
3711 if( rc==SQLITE_OK && op==SQLITE_FCNTL_VFSNAME ){
3712 *(char**)pArg = sqlite3_mprintf("apnd(%lld)/%z", p->iPgOne, *(char**)pArg);
3718 ** Return the sector-size in bytes for an apnd-file.
3720 static int apndSectorSize(sqlite3_file *pFile){
3721 pFile = ORIGFILE(pFile);
3722 return pFile->pMethods->xSectorSize(pFile);
3726 ** Return the device characteristic flags supported by an apnd-file.
3728 static int apndDeviceCharacteristics(sqlite3_file *pFile){
3729 pFile = ORIGFILE(pFile);
3730 return pFile->pMethods->xDeviceCharacteristics(pFile);
3733 /* Create a shared memory file mapping */
3734 static int apndShmMap(
3735 sqlite3_file *pFile,
3741 pFile = ORIGFILE(pFile);
3742 return pFile->pMethods->xShmMap(pFile,iPg,pgsz,bExtend,pp);
3745 /* Perform locking on a shared-memory segment */
3746 static int apndShmLock(sqlite3_file *pFile, int offset, int n, int flags){
3747 pFile = ORIGFILE(pFile);
3748 return pFile->pMethods->xShmLock(pFile,offset,n,flags);
3751 /* Memory barrier operation on shared memory */
3752 static void apndShmBarrier(sqlite3_file *pFile){
3753 pFile = ORIGFILE(pFile);
3754 pFile->pMethods->xShmBarrier(pFile);
3757 /* Unmap a shared memory segment */
3758 static int apndShmUnmap(sqlite3_file *pFile, int deleteFlag){
3759 pFile = ORIGFILE(pFile);
3760 return pFile->pMethods->xShmUnmap(pFile,deleteFlag);
3763 /* Fetch a page of a memory-mapped file */
3764 static int apndFetch(
3765 sqlite3_file *pFile,
3766 sqlite3_int64 iOfst,
3770 ApndFile *p = (ApndFile *)pFile;
3771 pFile = ORIGFILE(pFile);
3772 return pFile->pMethods->xFetch(pFile, iOfst+p->iPgOne, iAmt, pp);
3775 /* Release a memory-mapped page */
3776 static int apndUnfetch(sqlite3_file *pFile, sqlite3_int64 iOfst, void *pPage){
3777 ApndFile *p = (ApndFile *)pFile;
3778 pFile = ORIGFILE(pFile);
3779 return pFile->pMethods->xUnfetch(pFile, iOfst+p->iPgOne, pPage);
3783 ** Check to see if the file is an ordinary SQLite database file.
3785 static int apndIsOrdinaryDatabaseFile(sqlite3_int64 sz, sqlite3_file *pFile){
3788 static const char aSqliteHdr[] = "SQLite format 3";
3789 if( sz<512 ) return 0;
3790 rc = pFile->pMethods->xRead(pFile, zHdr, sizeof(zHdr), 0);
3792 return memcmp(zHdr, aSqliteHdr, sizeof(zHdr))==0;
3796 ** Try to read the append-mark off the end of a file. Return the
3797 ** start of the appended database if the append-mark is present. If
3798 ** there is no append-mark, return -1;
3800 static sqlite3_int64 apndReadMark(sqlite3_int64 sz, sqlite3_file *pFile){
3802 sqlite3_int64 iMark;
3803 unsigned char a[APND_MARK_SIZE];
3805 if( sz<=APND_MARK_SIZE ) return -1;
3806 rc = pFile->pMethods->xRead(pFile, a, APND_MARK_SIZE, sz-APND_MARK_SIZE);
3808 if( memcmp(a, APND_MARK_PREFIX, APND_MARK_PREFIX_SZ)!=0 ) return -1;
3809 iMark = ((sqlite3_int64)(a[APND_MARK_PREFIX_SZ]&0x7f))<<56;
3811 iMark += (sqlite3_int64)a[APND_MARK_PREFIX_SZ+i]<<(56-8*i);
3817 ** Open an apnd file handle.
3819 static int apndOpen(
3822 sqlite3_file *pFile,
3827 sqlite3_file *pSubFile;
3828 sqlite3_vfs *pSubVfs;
3831 pSubVfs = ORIGVFS(pVfs);
3832 if( (flags & SQLITE_OPEN_MAIN_DB)==0 ){
3833 return pSubVfs->xOpen(pSubVfs, zName, pFile, flags, pOutFlags);
3835 p = (ApndFile*)pFile;
3836 memset(p, 0, sizeof(*p));
3837 pSubFile = ORIGFILE(pFile);
3838 p->base.pMethods = &apnd_io_methods;
3839 rc = pSubVfs->xOpen(pSubVfs, zName, pSubFile, flags, pOutFlags);
3840 if( rc ) goto apnd_open_done;
3841 rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
3843 pSubFile->pMethods->xClose(pSubFile);
3844 goto apnd_open_done;
3846 if( apndIsOrdinaryDatabaseFile(sz, pSubFile) ){
3847 memmove(pFile, pSubFile, pSubVfs->szOsFile);
3851 p->iPgOne = apndReadMark(sz, pFile);
3855 if( (flags & SQLITE_OPEN_CREATE)==0 ){
3856 pSubFile->pMethods->xClose(pSubFile);
3857 rc = SQLITE_CANTOPEN;
3859 p->iPgOne = (sz+0xfff) & ~(sqlite3_int64)0xfff;
3861 if( rc ) pFile->pMethods = 0;
3866 ** All other VFS methods are pass-thrus.
3868 static int apndDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
3869 return ORIGVFS(pVfs)->xDelete(ORIGVFS(pVfs), zPath, dirSync);
3871 static int apndAccess(
3877 return ORIGVFS(pVfs)->xAccess(ORIGVFS(pVfs), zPath, flags, pResOut);
3879 static int apndFullPathname(
3885 return ORIGVFS(pVfs)->xFullPathname(ORIGVFS(pVfs),zPath,nOut,zOut);
3887 static void *apndDlOpen(sqlite3_vfs *pVfs, const char *zPath){
3888 return ORIGVFS(pVfs)->xDlOpen(ORIGVFS(pVfs), zPath);
3890 static void apndDlError(sqlite3_vfs *pVfs, int nByte, char *zErrMsg){
3891 ORIGVFS(pVfs)->xDlError(ORIGVFS(pVfs), nByte, zErrMsg);
3893 static void (*apndDlSym(sqlite3_vfs *pVfs, void *p, const char *zSym))(void){
3894 return ORIGVFS(pVfs)->xDlSym(ORIGVFS(pVfs), p, zSym);
3896 static void apndDlClose(sqlite3_vfs *pVfs, void *pHandle){
3897 ORIGVFS(pVfs)->xDlClose(ORIGVFS(pVfs), pHandle);
3899 static int apndRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
3900 return ORIGVFS(pVfs)->xRandomness(ORIGVFS(pVfs), nByte, zBufOut);
3902 static int apndSleep(sqlite3_vfs *pVfs, int nMicro){
3903 return ORIGVFS(pVfs)->xSleep(ORIGVFS(pVfs), nMicro);
3905 static int apndCurrentTime(sqlite3_vfs *pVfs, double *pTimeOut){
3906 return ORIGVFS(pVfs)->xCurrentTime(ORIGVFS(pVfs), pTimeOut);
3908 static int apndGetLastError(sqlite3_vfs *pVfs, int a, char *b){
3909 return ORIGVFS(pVfs)->xGetLastError(ORIGVFS(pVfs), a, b);
3911 static int apndCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *p){
3912 return ORIGVFS(pVfs)->xCurrentTimeInt64(ORIGVFS(pVfs), p);
3914 static int apndSetSystemCall(
3917 sqlite3_syscall_ptr pCall
3919 return ORIGVFS(pVfs)->xSetSystemCall(ORIGVFS(pVfs),zName,pCall);
3921 static sqlite3_syscall_ptr apndGetSystemCall(
3925 return ORIGVFS(pVfs)->xGetSystemCall(ORIGVFS(pVfs),zName);
3927 static const char *apndNextSystemCall(sqlite3_vfs *pVfs, const char *zName){
3928 return ORIGVFS(pVfs)->xNextSystemCall(ORIGVFS(pVfs), zName);
3936 ** This routine is called when the extension is loaded.
3937 ** Register the new VFS.
3939 int sqlite3_appendvfs_init(
3942 const sqlite3_api_routines *pApi
3946 SQLITE_EXTENSION_INIT2(pApi);
3949 pOrig = sqlite3_vfs_find(0);
3950 apnd_vfs.iVersion = pOrig->iVersion;
3951 apnd_vfs.pAppData = pOrig;
3952 apnd_vfs.szOsFile = pOrig->szOsFile + sizeof(ApndFile);
3953 rc = sqlite3_vfs_register(&apnd_vfs, 0);
3954 #ifdef APPENDVFS_TEST
3955 if( rc==SQLITE_OK ){
3956 rc = sqlite3_auto_extension((void(*)(void))apndvfsRegister);
3959 if( rc==SQLITE_OK ) rc = SQLITE_OK_LOAD_PERMANENTLY;
3963 /************************* End ../ext/misc/appendvfs.c ********************/
3964 #ifdef SQLITE_HAVE_ZLIB
3965 /************************* Begin ../ext/misc/zipfile.c ******************/
3969 ** The author disclaims copyright to this source code. In place of
3970 ** a legal notice, here is a blessing:
3972 ** May you do good and not evil.
3973 ** May you find forgiveness for yourself and forgive others.
3974 ** May you share freely, never taking more than you give.
3976 ******************************************************************************
3978 ** This file implements a virtual table for reading and writing ZIP archive
3983 ** SELECT name, sz, datetime(mtime,'unixepoch') FROM zipfile($filename);
3985 ** Current limitations:
3987 ** * No support for encryption
3988 ** * No support for ZIP archives spanning multiple files
3989 ** * No support for zip64 extensions
3990 ** * Only the "inflate/deflate" (zlib) compression method is supported
3992 SQLITE_EXTENSION_INIT1
3997 #include <sys/types.h>
3998 #include <sys/stat.h>
4000 #if !defined(_WIN32) && !defined(WIN32)
4001 # include <unistd.h>
4002 # include <dirent.h>
4012 #ifndef SQLITE_OMIT_VIRTUALTABLE
4014 #ifndef SQLITE_AMALGAMATION
4015 /* typedef sqlite3_int64 i64; */
4016 /* typedef unsigned char u8; */
4017 typedef unsigned short u16;
4018 typedef unsigned long u32;
4019 #define MIN(a,b) ((a)<(b) ? (a) : (b))
4022 static const char ZIPFILE_SCHEMA[] =
4024 "name PRIMARY KEY," /* 0: Name of file in zip archive */
4025 "mode," /* 1: POSIX mode for file */
4026 "mtime," /* 2: Last modification time (secs since 1970)*/
4027 "sz," /* 3: Size of object */
4028 "rawdata," /* 4: Raw data */
4029 "data," /* 5: Uncompressed data */
4030 "method," /* 6: Compression method (integer) */
4031 "z HIDDEN" /* 7: Name of zip file */
4034 #define ZIPFILE_F_COLUMN_IDX 7 /* Index of column "file" in the above */
4035 #define ZIPFILE_BUFFER_SIZE (64*1024)
4039 ** Magic numbers used to read and write zip files.
4041 ** ZIPFILE_NEWENTRY_MADEBY:
4042 ** Use this value for the "version-made-by" field in new zip file
4043 ** entries. The upper byte indicates "unix", and the lower byte
4044 ** indicates that the zip file matches pkzip specification 3.0.
4045 ** This is what info-zip seems to do.
4047 ** ZIPFILE_NEWENTRY_REQUIRED:
4048 ** Value for "version-required-to-extract" field of new entries.
4049 ** Version 2.0 is required to support folders and deflate compression.
4051 ** ZIPFILE_NEWENTRY_FLAGS:
4052 ** Value for "general-purpose-bit-flags" field of new entries. Bit
4053 ** 11 means "utf-8 filename and comment".
4055 ** ZIPFILE_SIGNATURE_CDS:
4056 ** First 4 bytes of a valid CDS record.
4058 ** ZIPFILE_SIGNATURE_LFH:
4059 ** First 4 bytes of a valid LFH record.
4061 #define ZIPFILE_EXTRA_TIMESTAMP 0x5455
4062 #define ZIPFILE_NEWENTRY_MADEBY ((3<<8) + 30)
4063 #define ZIPFILE_NEWENTRY_REQUIRED 20
4064 #define ZIPFILE_NEWENTRY_FLAGS 0x800
4065 #define ZIPFILE_SIGNATURE_CDS 0x02014b50
4066 #define ZIPFILE_SIGNATURE_LFH 0x04034b50
4067 #define ZIPFILE_SIGNATURE_EOCD 0x06054b50
4068 #define ZIPFILE_LFH_FIXED_SZ 30
4071 ** Set the error message contained in context ctx to the results of
4072 ** vprintf(zFmt, ...).
4074 static void zipfileCtxErrorMsg(sqlite3_context *ctx, const char *zFmt, ...){
4078 zMsg = sqlite3_vmprintf(zFmt, ap);
4079 sqlite3_result_error(ctx, zMsg, -1);
4086 *** 4.3.16 End of central directory record:
4088 *** end of central dir signature 4 bytes (0x06054b50)
4089 *** number of this disk 2 bytes
4090 *** number of the disk with the
4091 *** start of the central directory 2 bytes
4092 *** total number of entries in the
4093 *** central directory on this disk 2 bytes
4094 *** total number of entries in
4095 *** the central directory 2 bytes
4096 *** size of the central directory 4 bytes
4097 *** offset of start of central
4098 *** directory with respect to
4099 *** the starting disk number 4 bytes
4100 *** .ZIP file comment length 2 bytes
4101 *** .ZIP file comment (variable size)
4103 typedef struct ZipfileEOCD ZipfileEOCD;
4104 struct ZipfileEOCD {
4114 *** 4.3.12 Central directory structure:
4118 *** central file header signature 4 bytes (0x02014b50)
4119 *** version made by 2 bytes
4120 *** version needed to extract 2 bytes
4121 *** general purpose bit flag 2 bytes
4122 *** compression method 2 bytes
4123 *** last mod file time 2 bytes
4124 *** last mod file date 2 bytes
4126 *** compressed size 4 bytes
4127 *** uncompressed size 4 bytes
4128 *** file name length 2 bytes
4129 *** extra field length 2 bytes
4130 *** file comment length 2 bytes
4131 *** disk number start 2 bytes
4132 *** internal file attributes 2 bytes
4133 *** external file attributes 4 bytes
4134 *** relative offset of local header 4 bytes
4136 typedef struct ZipfileCDS ZipfileCDS;
4139 u16 iVersionExtract;
4154 char *zFile; /* Filename (sqlite3_malloc()) */
4158 *** 4.3.7 Local file header:
4160 *** local file header signature 4 bytes (0x04034b50)
4161 *** version needed to extract 2 bytes
4162 *** general purpose bit flag 2 bytes
4163 *** compression method 2 bytes
4164 *** last mod file time 2 bytes
4165 *** last mod file date 2 bytes
4167 *** compressed size 4 bytes
4168 *** uncompressed size 4 bytes
4169 *** file name length 2 bytes
4170 *** extra field length 2 bytes
4173 typedef struct ZipfileLFH ZipfileLFH;
4175 u16 iVersionExtract;
4187 typedef struct ZipfileEntry ZipfileEntry;
4188 struct ZipfileEntry {
4189 char *zPath; /* Path of zipfile entry */
4190 u8 *aCdsEntry; /* Buffer containing entire CDS entry */
4191 int nCdsEntry; /* Size of buffer aCdsEntry[] in bytes */
4192 int bDeleted; /* True if entry has been deleted */
4193 ZipfileEntry *pNext; /* Next element in in-memory CDS */
4197 ** Cursor type for recursively iterating through a directory structure.
4199 typedef struct ZipfileCsr ZipfileCsr;
4201 sqlite3_vtab_cursor base; /* Base class - must be first */
4202 i64 iId; /* Cursor ID */
4203 int bEof; /* True when at EOF */
4205 /* Used outside of write transactions */
4206 FILE *pFile; /* Zip file */
4207 i64 iNextOff; /* Offset of next record in central directory */
4208 ZipfileEOCD eocd; /* Parse of central directory record */
4210 /* Used inside write transactions */
4211 ZipfileEntry *pCurrent;
4213 ZipfileCDS cds; /* Central Directory Structure */
4214 ZipfileLFH lfh; /* Local File Header for current entry */
4215 i64 iDataOff; /* Offset in zipfile to data */
4216 u32 mTime; /* Extended mtime value */
4217 int flags; /* Flags byte (see below for bits) */
4218 ZipfileCsr *pCsrNext; /* Next cursor on same virtual table */
4222 ** Values for ZipfileCsr.flags.
4224 #define ZIPFILE_MTIME_VALID 0x0001
4226 typedef struct ZipfileTab ZipfileTab;
4228 sqlite3_vtab base; /* Base class - must be first */
4229 char *zFile; /* Zip file this table accesses (may be NULL) */
4230 u8 *aBuffer; /* Temporary buffer used for various tasks */
4232 ZipfileCsr *pCsrList; /* List of cursors */
4235 /* The following are used by write transactions only */
4236 ZipfileEntry *pFirstEntry; /* Linked list of all files (if pWriteFd!=0) */
4237 ZipfileEntry *pLastEntry; /* Last element in pFirstEntry list */
4238 FILE *pWriteFd; /* File handle open on zip archive */
4239 i64 szCurrent; /* Current size of zip archive */
4240 i64 szOrig; /* Size of archive at start of transaction */
4243 static void zipfileDequote(char *zIn){
4245 if( q=='"' || q=='\'' || q=='`' || q=='[' ){
4249 if( q=='[' ) q = ']';
4250 while( (c = zIn[iIn++]) ){
4252 if( zIn[iIn++]!=q ) break;
4261 ** Construct a new ZipfileTab virtual table object.
4263 ** argv[0] -> module name ("zipfile")
4264 ** argv[1] -> database name
4265 ** argv[2] -> table name
4266 ** argv[...] -> "column name" and other module argument fields.
4268 static int zipfileConnect(
4271 int argc, const char *const*argv,
4272 sqlite3_vtab **ppVtab,
4275 int nByte = sizeof(ZipfileTab) + ZIPFILE_BUFFER_SIZE;
4277 const char *zFile = 0;
4278 ZipfileTab *pNew = 0;
4283 nFile = (int)strlen(zFile)+1;
4286 rc = sqlite3_declare_vtab(db, ZIPFILE_SCHEMA);
4287 if( rc==SQLITE_OK ){
4288 pNew = (ZipfileTab*)sqlite3_malloc(nByte+nFile);
4289 if( pNew==0 ) return SQLITE_NOMEM;
4290 memset(pNew, 0, nByte+nFile);
4291 pNew->aBuffer = (u8*)&pNew[1];
4293 pNew->zFile = (char*)&pNew->aBuffer[ZIPFILE_BUFFER_SIZE];
4294 memcpy(pNew->zFile, zFile, nFile);
4295 zipfileDequote(pNew->zFile);
4298 *ppVtab = (sqlite3_vtab*)pNew;
4303 ** This method is the destructor for zipfile vtab objects.
4305 static int zipfileDisconnect(sqlite3_vtab *pVtab){
4306 sqlite3_free(pVtab);
4311 ** Constructor for a new ZipfileCsr object.
4313 static int zipfileOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCsr){
4314 ZipfileTab *pTab = (ZipfileTab*)p;
4316 pCsr = sqlite3_malloc(sizeof(*pCsr));
4317 *ppCsr = (sqlite3_vtab_cursor*)pCsr;
4319 return SQLITE_NOMEM;
4321 memset(pCsr, 0, sizeof(*pCsr));
4322 pCsr->iId = ++pTab->iNextCsrid;
4323 pCsr->pCsrNext = pTab->pCsrList;
4324 pTab->pCsrList = pCsr;
4329 ** Reset a cursor back to the state it was in when first returned
4330 ** by zipfileOpen().
4332 static void zipfileResetCursor(ZipfileCsr *pCsr){
4333 sqlite3_free(pCsr->cds.zFile);
4334 pCsr->cds.zFile = 0;
4337 fclose(pCsr->pFile);
4343 ** Destructor for an ZipfileCsr.
4345 static int zipfileClose(sqlite3_vtab_cursor *cur){
4346 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4347 ZipfileTab *pTab = (ZipfileTab*)(pCsr->base.pVtab);
4349 zipfileResetCursor(pCsr);
4351 /* Remove this cursor from the ZipfileTab.pCsrList list. */
4352 for(pp=&pTab->pCsrList; *pp; pp=&((*pp)->pCsrNext)){
4354 *pp = pCsr->pCsrNext;
4364 ** Set the error message for the virtual table associated with cursor
4365 ** pCsr to the results of vprintf(zFmt, ...).
4367 static void zipfileSetErrmsg(ZipfileCsr *pCsr, const char *zFmt, ...){
4370 pCsr->base.pVtab->zErrMsg = sqlite3_vmprintf(zFmt, ap);
4374 static int zipfileReadData(
4375 FILE *pFile, /* Read from this file */
4376 u8 *aRead, /* Read into this buffer */
4377 int nRead, /* Number of bytes to read */
4378 i64 iOff, /* Offset to read from */
4379 char **pzErrmsg /* OUT: Error message (from sqlite3_malloc) */
4382 fseek(pFile, (long)iOff, SEEK_SET);
4383 n = fread(aRead, 1, nRead, pFile);
4384 if( (int)n!=nRead ){
4385 *pzErrmsg = sqlite3_mprintf("error in fread()");
4386 return SQLITE_ERROR;
4391 static int zipfileAppendData(
4397 fseek(pTab->pWriteFd, (long)pTab->szCurrent, SEEK_SET);
4398 n = fwrite(aWrite, 1, nWrite, pTab->pWriteFd);
4399 if( (int)n!=nWrite ){
4400 pTab->base.zErrMsg = sqlite3_mprintf("error in fwrite()");
4401 return SQLITE_ERROR;
4403 pTab->szCurrent += nWrite;
4407 static u16 zipfileGetU16(const u8 *aBuf){
4408 return (aBuf[1] << 8) + aBuf[0];
4410 static u32 zipfileGetU32(const u8 *aBuf){
4411 return ((u32)(aBuf[3]) << 24)
4412 + ((u32)(aBuf[2]) << 16)
4413 + ((u32)(aBuf[1]) << 8)
4414 + ((u32)(aBuf[0]) << 0);
4417 static void zipfilePutU16(u8 *aBuf, u16 val){
4418 aBuf[0] = val & 0xFF;
4419 aBuf[1] = (val>>8) & 0xFF;
4421 static void zipfilePutU32(u8 *aBuf, u32 val){
4422 aBuf[0] = val & 0xFF;
4423 aBuf[1] = (val>>8) & 0xFF;
4424 aBuf[2] = (val>>16) & 0xFF;
4425 aBuf[3] = (val>>24) & 0xFF;
4428 #define zipfileRead32(aBuf) ( aBuf+=4, zipfileGetU32(aBuf-4) )
4429 #define zipfileRead16(aBuf) ( aBuf+=2, zipfileGetU16(aBuf-2) )
4431 #define zipfileWrite32(aBuf,val) { zipfilePutU32(aBuf,val); aBuf+=4; }
4432 #define zipfileWrite16(aBuf,val) { zipfilePutU16(aBuf,val); aBuf+=2; }
4434 static u8* zipfileCsrBuffer(ZipfileCsr *pCsr){
4435 return ((ZipfileTab*)(pCsr->base.pVtab))->aBuffer;
4439 ** Magic numbers used to read CDS records.
4441 #define ZIPFILE_CDS_FIXED_SZ 46
4442 #define ZIPFILE_CDS_NFILE_OFF 28
4445 ** Decode the CDS record in buffer aBuf into (*pCDS). Return SQLITE_ERROR
4446 ** if the record is not well-formed, or SQLITE_OK otherwise.
4448 static int zipfileReadCDS(u8 *aBuf, ZipfileCDS *pCDS){
4450 u32 sig = zipfileRead32(aRead);
4452 if( sig!=ZIPFILE_SIGNATURE_CDS ){
4455 pCDS->iVersionMadeBy = zipfileRead16(aRead);
4456 pCDS->iVersionExtract = zipfileRead16(aRead);
4457 pCDS->flags = zipfileRead16(aRead);
4458 pCDS->iCompression = zipfileRead16(aRead);
4459 pCDS->mTime = zipfileRead16(aRead);
4460 pCDS->mDate = zipfileRead16(aRead);
4461 pCDS->crc32 = zipfileRead32(aRead);
4462 pCDS->szCompressed = zipfileRead32(aRead);
4463 pCDS->szUncompressed = zipfileRead32(aRead);
4464 assert( aRead==&aBuf[ZIPFILE_CDS_NFILE_OFF] );
4465 pCDS->nFile = zipfileRead16(aRead);
4466 pCDS->nExtra = zipfileRead16(aRead);
4467 pCDS->nComment = zipfileRead16(aRead);
4468 pCDS->iDiskStart = zipfileRead16(aRead);
4469 pCDS->iInternalAttr = zipfileRead16(aRead);
4470 pCDS->iExternalAttr = zipfileRead32(aRead);
4471 pCDS->iOffset = zipfileRead32(aRead);
4472 assert( aRead==&aBuf[ZIPFILE_CDS_FIXED_SZ] );
4479 ** Read the CDS record for the current entry from disk into pCsr->cds.
4481 static int zipfileCsrReadCDS(ZipfileCsr *pCsr){
4482 char **pzErr = &pCsr->base.pVtab->zErrMsg;
4486 sqlite3_free(pCsr->cds.zFile);
4487 pCsr->cds.zFile = 0;
4489 if( pCsr->pCurrent==0 ){
4490 aRead = zipfileCsrBuffer(pCsr);
4491 rc = zipfileReadData(
4492 pCsr->pFile, aRead, ZIPFILE_CDS_FIXED_SZ, pCsr->iNextOff, pzErr
4495 aRead = pCsr->pCurrent->aCdsEntry;
4498 if( rc==SQLITE_OK ){
4499 rc = zipfileReadCDS(aRead, &pCsr->cds);
4500 if( rc!=SQLITE_OK ){
4501 assert( pCsr->pCurrent==0 );
4502 zipfileSetErrmsg(pCsr,"failed to read CDS at offset %lld",pCsr->iNextOff);
4505 if( pCsr->pCurrent==0 ){
4506 nRead = pCsr->cds.nFile + pCsr->cds.nExtra;
4507 aRead = zipfileCsrBuffer(pCsr);
4508 pCsr->iNextOff += ZIPFILE_CDS_FIXED_SZ;
4509 rc = zipfileReadData(pCsr->pFile, aRead, nRead, pCsr->iNextOff, pzErr);
4511 aRead = &aRead[ZIPFILE_CDS_FIXED_SZ];
4514 if( rc==SQLITE_OK ){
4515 pCsr->cds.zFile = sqlite3_mprintf("%.*s", (int)pCsr->cds.nFile, aRead);
4516 pCsr->iNextOff += pCsr->cds.nFile;
4517 pCsr->iNextOff += pCsr->cds.nExtra;
4518 pCsr->iNextOff += pCsr->cds.nComment;
4521 /* Scan the cds.nExtra bytes of "extra" fields for any that can
4522 ** be interpreted. The general format of an extra field is:
4524 ** Header ID 2 bytes
4525 ** Data Size 2 bytes
4529 if( rc==SQLITE_OK ){
4530 u8 *p = &aRead[pCsr->cds.nFile];
4531 u8 *pEnd = &p[pCsr->cds.nExtra];
4534 u16 id = zipfileRead16(p);
4535 u16 nByte = zipfileRead16(p);
4538 case ZIPFILE_EXTRA_TIMESTAMP: {
4540 if( b & 0x01 ){ /* 0x01 -> modtime is present */
4541 pCsr->mTime = zipfileGetU32(&p[1]);
4542 pCsr->flags |= ZIPFILE_MTIME_VALID;
4557 static FILE *zipfileGetFd(ZipfileCsr *pCsr){
4558 if( pCsr->pFile ) return pCsr->pFile;
4559 return ((ZipfileTab*)(pCsr->base.pVtab))->pWriteFd;
4562 static int zipfileReadLFH(
4570 static const int szFix = ZIPFILE_LFH_FIXED_SZ;
4573 rc = zipfileReadData(pFd, aRead, szFix, iOffset, pzErr);
4574 if( rc==SQLITE_OK ){
4575 u32 sig = zipfileRead32(aRead);
4576 if( sig!=ZIPFILE_SIGNATURE_LFH ){
4577 *pzErr = sqlite3_mprintf("failed to read LFH at offset %d", (int)iOffset);
4580 pLFH->iVersionExtract = zipfileRead16(aRead);
4581 pLFH->flags = zipfileRead16(aRead);
4582 pLFH->iCompression = zipfileRead16(aRead);
4583 pLFH->mTime = zipfileRead16(aRead);
4584 pLFH->mDate = zipfileRead16(aRead);
4585 pLFH->crc32 = zipfileRead32(aRead);
4586 pLFH->szCompressed = zipfileRead32(aRead);
4587 pLFH->szUncompressed = zipfileRead32(aRead);
4588 pLFH->nFile = zipfileRead16(aRead);
4589 pLFH->nExtra = zipfileRead16(aRead);
4590 assert( aRead==&aTmp[szFix] );
4596 static int zipfileCsrReadLFH(ZipfileCsr *pCsr){
4597 FILE *pFile = zipfileGetFd(pCsr);
4598 char **pzErr = &pCsr->base.pVtab->zErrMsg;
4599 u8 *aRead = zipfileCsrBuffer(pCsr);
4600 int rc = zipfileReadLFH(pFile, pCsr->cds.iOffset, aRead, &pCsr->lfh, pzErr);
4601 pCsr->iDataOff = pCsr->cds.iOffset + ZIPFILE_LFH_FIXED_SZ;
4602 pCsr->iDataOff += pCsr->lfh.nFile+pCsr->lfh.nExtra;
4608 ** Advance an ZipfileCsr to its next row of output.
4610 static int zipfileNext(sqlite3_vtab_cursor *cur){
4611 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4615 if( pCsr->pCurrent==0 ){
4616 i64 iEof = pCsr->eocd.iOffset + pCsr->eocd.nSize;
4617 if( pCsr->iNextOff>=iEof ){
4621 assert( pCsr->pFile==0 );
4623 pCsr->pCurrent = pCsr->pCurrent->pNext;
4624 }while( pCsr->pCurrent && pCsr->pCurrent->bDeleted );
4625 if( pCsr->pCurrent==0 ){
4630 if( pCsr->bEof==0 ){
4631 rc = zipfileCsrReadCDS(pCsr);
4632 if( rc==SQLITE_OK ){
4633 rc = zipfileCsrReadLFH(pCsr);
4641 ** "Standard" MS-DOS time format:
4643 ** File modification time:
4644 ** Bits 00-04: seconds divided by 2
4645 ** Bits 05-10: minute
4647 ** File modification date:
4649 ** Bits 05-08: month (1-12)
4650 ** Bits 09-15: years from 1980
4652 static time_t zipfileMtime(ZipfileCsr *pCsr){
4654 memset(&t, 0, sizeof(t));
4655 t.tm_sec = (pCsr->cds.mTime & 0x1F)*2;
4656 t.tm_min = (pCsr->cds.mTime >> 5) & 0x2F;
4657 t.tm_hour = (pCsr->cds.mTime >> 11) & 0x1F;
4659 t.tm_mday = (pCsr->cds.mDate & 0x1F);
4660 t.tm_mon = ((pCsr->cds.mDate >> 5) & 0x0F) - 1;
4661 t.tm_year = 80 + ((pCsr->cds.mDate >> 9) & 0x7F);
4666 static void zipfileMtimeToDos(ZipfileCDS *pCds, u32 mTime){
4667 time_t t = (time_t)mTime;
4670 #if !defined(_WIN32) && !defined(WIN32)
4671 localtime_r(&t, &res);
4673 memcpy(&res, localtime(&t), sizeof(struct tm));
4676 pCds->mTime = (u16)(
4679 (res.tm_hour << 11));
4681 pCds->mDate = (u16)(
4683 ((res.tm_mon+1) << 5) +
4684 ((res.tm_year-80) << 9));
4687 static void zipfileInflate(
4688 sqlite3_context *pCtx, /* Store error here, if any */
4689 const u8 *aIn, /* Compressed data */
4690 int nIn, /* Size of buffer aIn[] in bytes */
4691 int nOut /* Expected output size */
4693 u8 *aRes = sqlite3_malloc(nOut);
4695 sqlite3_result_error_nomem(pCtx);
4699 memset(&str, 0, sizeof(str));
4701 str.next_in = (Byte*)aIn;
4703 str.next_out = (Byte*)aRes;
4704 str.avail_out = nOut;
4706 err = inflateInit2(&str, -15);
4708 zipfileCtxErrorMsg(pCtx, "inflateInit2() failed (%d)", err);
4710 err = inflate(&str, Z_NO_FLUSH);
4711 if( err!=Z_STREAM_END ){
4712 zipfileCtxErrorMsg(pCtx, "inflate() failed (%d)", err);
4714 sqlite3_result_blob(pCtx, aRes, nOut, SQLITE_TRANSIENT);
4722 static int zipfileDeflate(
4723 ZipfileTab *pTab, /* Set error message here */
4724 const u8 *aIn, int nIn, /* Input */
4725 u8 **ppOut, int *pnOut /* Output */
4727 int nAlloc = (int)compressBound(nIn);
4731 aOut = (u8*)sqlite3_malloc(nAlloc);
4737 memset(&str, 0, sizeof(str));
4738 str.next_in = (Bytef*)aIn;
4740 str.next_out = aOut;
4741 str.avail_out = nAlloc;
4743 deflateInit2(&str, 9, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
4744 res = deflate(&str, Z_FINISH);
4746 if( res==Z_STREAM_END ){
4748 *pnOut = (int)str.total_out;
4751 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: deflate() error");
4762 ** Return values of columns for the row at which the series_cursor
4763 ** is currently pointing.
4765 static int zipfileColumn(
4766 sqlite3_vtab_cursor *cur, /* The cursor */
4767 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
4768 int i /* Which column to return */
4770 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4774 sqlite3_result_text(ctx, pCsr->cds.zFile, -1, SQLITE_TRANSIENT);
4777 /* TODO: Whether or not the following is correct surely depends on
4778 ** the platform on which the archive was created. */
4779 sqlite3_result_int(ctx, pCsr->cds.iExternalAttr >> 16);
4781 case 2: { /* mtime */
4782 if( pCsr->flags & ZIPFILE_MTIME_VALID ){
4783 sqlite3_result_int64(ctx, pCsr->mTime);
4785 sqlite3_result_int64(ctx, zipfileMtime(pCsr));
4790 if( sqlite3_vtab_nochange(ctx)==0 ){
4791 sqlite3_result_int64(ctx, pCsr->cds.szUncompressed);
4795 case 4: /* rawdata */
4796 if( sqlite3_vtab_nochange(ctx) ) break;
4797 case 5: { /* data */
4798 if( i==4 || pCsr->cds.iCompression==0 || pCsr->cds.iCompression==8 ){
4799 int sz = pCsr->cds.szCompressed;
4800 int szFinal = pCsr->cds.szUncompressed;
4802 u8 *aBuf = sqlite3_malloc(sz);
4806 FILE *pFile = zipfileGetFd(pCsr);
4807 rc = zipfileReadData(pFile, aBuf, sz, pCsr->iDataOff,
4808 &pCsr->base.pVtab->zErrMsg
4811 if( rc==SQLITE_OK ){
4812 if( i==5 && pCsr->cds.iCompression ){
4813 zipfileInflate(ctx, aBuf, sz, szFinal);
4815 sqlite3_result_blob(ctx, aBuf, sz, SQLITE_TRANSIENT);
4820 /* Figure out if this is a directory or a zero-sized file. Consider
4821 ** it to be a directory either if the mode suggests so, or if
4822 ** the final character in the name is '/'. */
4823 u32 mode = pCsr->cds.iExternalAttr >> 16;
4824 if( !(mode & S_IFDIR) && pCsr->cds.zFile[pCsr->cds.nFile-1]!='/' ){
4825 sqlite3_result_blob(ctx, "", 0, SQLITE_STATIC);
4831 case 6: /* method */
4832 sqlite3_result_int(ctx, pCsr->cds.iCompression);
4835 sqlite3_result_int64(ctx, pCsr->iId);
4843 ** Return the rowid for the current row.
4845 static int zipfileRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
4851 ** Return TRUE if the cursor has been moved off of the last
4854 static int zipfileEof(sqlite3_vtab_cursor *cur){
4855 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4861 static int zipfileReadEOCD(
4862 ZipfileTab *pTab, /* Return errors here */
4863 FILE *pFile, /* Read from this file */
4864 ZipfileEOCD *pEOCD /* Object to populate */
4866 u8 *aRead = pTab->aBuffer; /* Temporary buffer */
4867 i64 szFile; /* Total size of file in bytes */
4868 int nRead; /* Bytes to read from file */
4869 i64 iOff; /* Offset to read from */
4872 fseek(pFile, 0, SEEK_END);
4873 szFile = (i64)ftell(pFile);
4875 memset(pEOCD, 0, sizeof(ZipfileEOCD));
4878 nRead = (int)(MIN(szFile, ZIPFILE_BUFFER_SIZE));
4879 iOff = szFile - nRead;
4881 rc = zipfileReadData(pFile, aRead, nRead, iOff, &pTab->base.zErrMsg);
4882 if( rc==SQLITE_OK ){
4885 /* Scan backwards looking for the signature bytes */
4886 for(i=nRead-20; i>=0; i--){
4887 if( aRead[i]==0x50 && aRead[i+1]==0x4b
4888 && aRead[i+2]==0x05 && aRead[i+3]==0x06
4894 pTab->base.zErrMsg = sqlite3_mprintf(
4895 "cannot find end of central directory record"
4897 return SQLITE_ERROR;
4901 pEOCD->iDisk = zipfileRead16(aRead);
4902 pEOCD->iFirstDisk = zipfileRead16(aRead);
4903 pEOCD->nEntry = zipfileRead16(aRead);
4904 pEOCD->nEntryTotal = zipfileRead16(aRead);
4905 pEOCD->nSize = zipfileRead32(aRead);
4906 pEOCD->iOffset = zipfileRead32(aRead);
4909 printf("iDisk=%d iFirstDisk=%d nEntry=%d "
4910 "nEntryTotal=%d nSize=%d iOffset=%d",
4911 (int)pEOCD->iDisk, (int)pEOCD->iFirstDisk, (int)pEOCD->nEntry,
4912 (int)pEOCD->nEntryTotal, (int)pEOCD->nSize, (int)pEOCD->iOffset
4921 ** xFilter callback.
4923 static int zipfileFilter(
4924 sqlite3_vtab_cursor *cur,
4925 int idxNum, const char *idxStr,
4926 int argc, sqlite3_value **argv
4928 ZipfileTab *pTab = (ZipfileTab*)cur->pVtab;
4929 ZipfileCsr *pCsr = (ZipfileCsr*)cur;
4930 const char *zFile; /* Zip file to scan */
4931 int rc = SQLITE_OK; /* Return Code */
4933 zipfileResetCursor(pCsr);
4936 zFile = pTab->zFile;
4937 }else if( idxNum==0 ){
4938 /* Error. This is an eponymous virtual table and the user has not
4939 ** supplied a file name. */
4940 zipfileSetErrmsg(pCsr, "table function zipfile() requires an argument");
4941 return SQLITE_ERROR;
4943 zFile = (const char*)sqlite3_value_text(argv[0]);
4946 if( pTab->pWriteFd==0 ){
4947 pCsr->pFile = fopen(zFile, "rb");
4948 if( pCsr->pFile==0 ){
4949 zipfileSetErrmsg(pCsr, "cannot open file: %s", zFile);
4952 rc = zipfileReadEOCD(pTab, pCsr->pFile, &pCsr->eocd);
4953 if( rc==SQLITE_OK ){
4954 if( pCsr->eocd.nEntry==0 ){
4957 pCsr->iNextOff = pCsr->eocd.iOffset;
4958 rc = zipfileNext(cur);
4964 memset(&e, 0, sizeof(e));
4965 e.pNext = pTab->pFirstEntry;
4966 pCsr->pCurrent = &e;
4967 rc = zipfileNext(cur);
4968 assert( pCsr->pCurrent!=&e );
4975 ** xBestIndex callback.
4977 static int zipfileBestIndex(
4979 sqlite3_index_info *pIdxInfo
4983 for(i=0; i<pIdxInfo->nConstraint; i++){
4984 const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
4985 if( pCons->usable==0 ) continue;
4986 if( pCons->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
4987 if( pCons->iColumn!=ZIPFILE_F_COLUMN_IDX ) continue;
4991 if( i<pIdxInfo->nConstraint ){
4992 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
4993 pIdxInfo->aConstraintUsage[i].omit = 1;
4994 pIdxInfo->estimatedCost = 1000.0;
4995 pIdxInfo->idxNum = 1;
4997 pIdxInfo->estimatedCost = (double)(((sqlite3_int64)1) << 50);
4998 pIdxInfo->idxNum = 0;
5005 ** Add object pNew to the end of the linked list that begins at
5006 ** ZipfileTab.pFirstEntry and ends with pLastEntry.
5008 static void zipfileAddEntry(
5010 ZipfileEntry *pBefore,
5013 assert( (pTab->pFirstEntry==0)==(pTab->pLastEntry==0) );
5014 assert( pNew->pNext==0 );
5016 if( pTab->pFirstEntry==0 ){
5017 pTab->pFirstEntry = pTab->pLastEntry = pNew;
5019 assert( pTab->pLastEntry->pNext==0 );
5020 pTab->pLastEntry->pNext = pNew;
5021 pTab->pLastEntry = pNew;
5025 for(pp=&pTab->pFirstEntry; *pp!=pBefore; pp=&((*pp)->pNext));
5026 pNew->pNext = pBefore;
5031 static int zipfileLoadDirectory(ZipfileTab *pTab){
5035 rc = zipfileReadEOCD(pTab, pTab->pWriteFd, &eocd);
5036 if( rc==SQLITE_OK && eocd.nEntry>0 ){
5039 u8 *aBuf = sqlite3_malloc(eocd.nSize);
5043 rc = zipfileReadData(
5044 pTab->pWriteFd, aBuf, eocd.nSize, eocd.iOffset, &pTab->base.zErrMsg
5048 for(i=0; rc==SQLITE_OK && i<eocd.nEntry; i++){
5053 u8 *aRec = &aBuf[iOff];
5055 nFile = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF]);
5056 nExtra = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+2]);
5057 nComment = zipfileGetU16(&aRec[ZIPFILE_CDS_NFILE_OFF+4]);
5059 pNew = sqlite3_malloc(
5060 sizeof(ZipfileEntry)
5062 + ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment
5067 memset(pNew, 0, sizeof(ZipfileEntry));
5068 pNew->zPath = (char*)&pNew[1];
5069 memcpy(pNew->zPath, &aRec[ZIPFILE_CDS_FIXED_SZ], nFile);
5070 pNew->zPath[nFile] = '\0';
5071 pNew->aCdsEntry = (u8*)&pNew->zPath[nFile+1];
5072 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5073 memcpy(pNew->aCdsEntry, aRec, pNew->nCdsEntry);
5074 zipfileAddEntry(pTab, 0, pNew);
5077 iOff += ZIPFILE_CDS_FIXED_SZ+nFile+nExtra+nComment;
5086 static ZipfileEntry *zipfileNewEntry(
5087 ZipfileCDS *pCds, /* Values for fixed size part of CDS */
5088 const char *zPath, /* Path for new entry */
5089 int nPath, /* strlen(zPath) */
5090 u32 mTime /* Modification time (or 0) */
5094 pCds->nFile = (u16)nPath;
5095 pCds->nExtra = mTime ? 9 : 0;
5096 pNew = (ZipfileEntry*)sqlite3_malloc(
5097 sizeof(ZipfileEntry) +
5099 ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra
5103 memset(pNew, 0, sizeof(ZipfileEntry));
5104 pNew->zPath = (char*)&pNew[1];
5105 pNew->aCdsEntry = (u8*)&pNew->zPath[nPath+1];
5106 pNew->nCdsEntry = ZIPFILE_CDS_FIXED_SZ + nPath + pCds->nExtra;
5107 memcpy(pNew->zPath, zPath, nPath+1);
5109 aWrite = pNew->aCdsEntry;
5110 zipfileWrite32(aWrite, ZIPFILE_SIGNATURE_CDS);
5111 zipfileWrite16(aWrite, pCds->iVersionMadeBy);
5112 zipfileWrite16(aWrite, pCds->iVersionExtract);
5113 zipfileWrite16(aWrite, pCds->flags);
5114 zipfileWrite16(aWrite, pCds->iCompression);
5115 zipfileWrite16(aWrite, pCds->mTime);
5116 zipfileWrite16(aWrite, pCds->mDate);
5117 zipfileWrite32(aWrite, pCds->crc32);
5118 zipfileWrite32(aWrite, pCds->szCompressed);
5119 zipfileWrite32(aWrite, pCds->szUncompressed);
5120 zipfileWrite16(aWrite, pCds->nFile);
5121 zipfileWrite16(aWrite, pCds->nExtra);
5122 zipfileWrite16(aWrite, pCds->nComment); assert( pCds->nComment==0 );
5123 zipfileWrite16(aWrite, pCds->iDiskStart);
5124 zipfileWrite16(aWrite, pCds->iInternalAttr);
5125 zipfileWrite32(aWrite, pCds->iExternalAttr);
5126 zipfileWrite32(aWrite, pCds->iOffset);
5127 assert( aWrite==&pNew->aCdsEntry[ZIPFILE_CDS_FIXED_SZ] );
5128 memcpy(aWrite, zPath, nPath);
5131 zipfileWrite16(aWrite, ZIPFILE_EXTRA_TIMESTAMP);
5132 zipfileWrite16(aWrite, 5);
5134 zipfileWrite32(aWrite, mTime);
5141 static int zipfileAppendEntry(
5144 const char *zPath, /* Path for new entry */
5145 int nPath, /* strlen(zPath) */
5150 u8 *aBuf = pTab->aBuffer;
5153 zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_LFH);
5154 zipfileWrite16(aBuf, pCds->iVersionExtract);
5155 zipfileWrite16(aBuf, pCds->flags);
5156 zipfileWrite16(aBuf, pCds->iCompression);
5157 zipfileWrite16(aBuf, pCds->mTime);
5158 zipfileWrite16(aBuf, pCds->mDate);
5159 zipfileWrite32(aBuf, pCds->crc32);
5160 zipfileWrite32(aBuf, pCds->szCompressed);
5161 zipfileWrite32(aBuf, pCds->szUncompressed);
5162 zipfileWrite16(aBuf, (u16)nPath);
5163 zipfileWrite16(aBuf, pCds->nExtra);
5164 assert( aBuf==&pTab->aBuffer[ZIPFILE_LFH_FIXED_SZ] );
5165 rc = zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5166 if( rc==SQLITE_OK ){
5167 rc = zipfileAppendData(pTab, (const u8*)zPath, nPath);
5170 if( rc==SQLITE_OK && pCds->nExtra ){
5171 aBuf = pTab->aBuffer;
5172 zipfileWrite16(aBuf, ZIPFILE_EXTRA_TIMESTAMP);
5173 zipfileWrite16(aBuf, 5);
5175 zipfileWrite32(aBuf, mTime);
5176 rc = zipfileAppendData(pTab, pTab->aBuffer, 9);
5179 if( rc==SQLITE_OK ){
5180 rc = zipfileAppendData(pTab, pData, nData);
5186 static int zipfileGetMode(
5188 sqlite3_value *pVal,
5189 u32 defaultMode, /* Value to use if pVal IS NULL */
5192 const char *z = (const char*)sqlite3_value_text(pVal);
5196 }else if( z[0]>='0' && z[0]<='9' ){
5197 mode = (unsigned int)sqlite3_value_int(pVal);
5199 const char zTemplate[11] = "-rwxrwxrwx";
5201 if( strlen(z)!=10 ) goto parse_error;
5203 case '-': mode |= S_IFREG; break;
5204 case 'd': mode |= S_IFDIR; break;
5205 #if !defined(_WIN32) && !defined(WIN32)
5206 case 'l': mode |= S_IFLNK; break;
5208 default: goto parse_error;
5210 for(i=1; i<10; i++){
5211 if( z[i]==zTemplate[i] ) mode |= 1 << (9-i);
5212 else if( z[i]!='-' ) goto parse_error;
5219 pTab->base.zErrMsg = sqlite3_mprintf("zipfile: parse error in mode: %s", z);
5220 return SQLITE_ERROR;
5224 ** Both (const char*) arguments point to nul-terminated strings. Argument
5225 ** nB is the value of strlen(zB). This function returns 0 if the strings are
5226 ** identical, ignoring any trailing '/' character in either path. */
5227 static int zipfileComparePath(const char *zA, const char *zB, int nB){
5228 int nA = (int)strlen(zA);
5229 if( zA[nA-1]=='/' ) nA--;
5230 if( zB[nB-1]=='/' ) nB--;
5231 if( nA==nB && memcmp(zA, zB, nA)==0 ) return 0;
5238 static int zipfileUpdate(
5239 sqlite3_vtab *pVtab,
5241 sqlite3_value **apVal,
5242 sqlite_int64 *pRowid
5244 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5245 int rc = SQLITE_OK; /* Return Code */
5246 ZipfileEntry *pNew = 0; /* New in-memory CDS entry */
5248 u32 mode = 0; /* Mode for new entry */
5249 i64 mTime = 0; /* Modification time for new entry */
5250 i64 sz = 0; /* Uncompressed size */
5251 const char *zPath = 0; /* Path for new entry */
5252 int nPath = 0; /* strlen(zPath) */
5253 const u8 *pData = 0; /* Pointer to buffer containing content */
5254 int nData = 0; /* Size of pData buffer in bytes */
5255 int iMethod = 0; /* Compression method for new entry */
5256 u8 *pFree = 0; /* Free this */
5257 char *zFree = 0; /* Also free this */
5258 ZipfileCDS cds; /* New Central Directory Structure entry */
5259 ZipfileEntry *pOld = 0;
5263 assert( pTab->zFile );
5264 assert( pTab->pWriteFd );
5266 if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
5267 const char *zDelete = (const char*)sqlite3_value_text(apVal[0]);
5268 int nDelete = (int)strlen(zDelete);
5269 for(pOld=pTab->pFirstEntry; 1; pOld=pOld->pNext){
5270 if( pOld->bDeleted ) continue;
5271 if( zipfileComparePath(pOld->zPath, zDelete, nDelete)==0 ){
5275 assert( pOld->pNext );
5277 if( nVal==1 ) return SQLITE_OK;
5280 /* Check that "sz" and "rawdata" are both NULL: */
5281 if( sqlite3_value_type(apVal[5])!=SQLITE_NULL
5282 || sqlite3_value_type(apVal[6])!=SQLITE_NULL
5284 rc = SQLITE_CONSTRAINT;
5287 if( rc==SQLITE_OK ){
5288 if( sqlite3_value_type(apVal[7])==SQLITE_NULL ){
5289 /* data=NULL. A directory */
5292 /* Value specified for "data", and possibly "method". This must be
5293 ** a regular file or a symlink. */
5294 const u8 *aIn = sqlite3_value_blob(apVal[7]);
5295 int nIn = sqlite3_value_bytes(apVal[7]);
5296 int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL;
5298 iMethod = sqlite3_value_int(apVal[8]);
5302 if( iMethod!=0 && iMethod!=8 ){
5303 rc = SQLITE_CONSTRAINT;
5305 if( bAuto || iMethod ){
5307 rc = zipfileDeflate(pTab, aIn, nIn, &pFree, &nCmp);
5308 if( rc==SQLITE_OK ){
5309 if( iMethod || nCmp<nIn ){
5316 iCrc32 = crc32(0, aIn, nIn);
5321 if( rc==SQLITE_OK ){
5322 rc = zipfileGetMode(pTab, apVal[3],
5323 (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode
5325 if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){
5326 /* The "mode" attribute is a directory, but data has been specified.
5327 ** Or vice-versa - no data but "mode" is a file or symlink. */
5328 rc = SQLITE_CONSTRAINT;
5332 if( rc==SQLITE_OK ){
5333 zPath = (const char*)sqlite3_value_text(apVal[2]);
5334 nPath = (int)strlen(zPath);
5335 if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){
5336 mTime = (sqlite3_int64)time(0);
5338 mTime = sqlite3_value_int64(apVal[4]);
5342 if( rc==SQLITE_OK && bIsDir ){
5343 /* For a directory, check that the last character in the path is a
5344 ** '/'. This appears to be required for compatibility with info-zip
5345 ** (the unzip command on unix). It does not create directories
5347 if( zPath[nPath-1]!='/' ){
5348 zFree = sqlite3_mprintf("%s/", zPath);
5349 if( zFree==0 ){ rc = SQLITE_NOMEM; }
5350 zPath = (const char*)zFree;
5355 /* Check that we're not inserting a duplicate entry */
5356 if( rc==SQLITE_OK ){
5358 for(p=pTab->pFirstEntry; p; p=p->pNext){
5359 if( p->bDeleted ) continue;
5360 if( zipfileComparePath(p->zPath, zPath, nPath)==0 ){
5361 rc = SQLITE_CONSTRAINT;
5367 if( rc==SQLITE_OK ){
5368 /* Create the new CDS record. */
5369 memset(&cds, 0, sizeof(cds));
5370 cds.iVersionMadeBy = ZIPFILE_NEWENTRY_MADEBY;
5371 cds.iVersionExtract = ZIPFILE_NEWENTRY_REQUIRED;
5372 cds.flags = ZIPFILE_NEWENTRY_FLAGS;
5373 cds.iCompression = (u16)iMethod;
5374 zipfileMtimeToDos(&cds, (u32)mTime);
5376 cds.szCompressed = nData;
5377 cds.szUncompressed = (u32)sz;
5378 cds.iExternalAttr = (mode<<16);
5379 cds.iOffset = (u32)pTab->szCurrent;
5380 pNew = zipfileNewEntry(&cds, zPath, nPath, (u32)mTime);
5384 zipfileAddEntry(pTab, pOld, pNew);
5388 /* Append the new header+file to the archive */
5389 if( rc==SQLITE_OK ){
5390 rc = zipfileAppendEntry(pTab, &cds, zPath, nPath, pData, nData, (u32)mTime);
5393 if( rc!=SQLITE_OK && pOld ){
5396 sqlite3_free(pFree);
5397 sqlite3_free(zFree);
5401 static int zipfileAppendEOCD(ZipfileTab *pTab, ZipfileEOCD *p){
5402 u8 *aBuf = pTab->aBuffer;
5404 zipfileWrite32(aBuf, ZIPFILE_SIGNATURE_EOCD);
5405 zipfileWrite16(aBuf, p->iDisk);
5406 zipfileWrite16(aBuf, p->iFirstDisk);
5407 zipfileWrite16(aBuf, p->nEntry);
5408 zipfileWrite16(aBuf, p->nEntryTotal);
5409 zipfileWrite32(aBuf, p->nSize);
5410 zipfileWrite32(aBuf, p->iOffset);
5411 zipfileWrite16(aBuf, 0); /* Size of trailing comment in bytes*/
5413 assert( (aBuf-pTab->aBuffer)==22 );
5414 return zipfileAppendData(pTab, pTab->aBuffer, (int)(aBuf - pTab->aBuffer));
5417 static void zipfileCleanupTransaction(ZipfileTab *pTab){
5418 ZipfileEntry *pEntry;
5419 ZipfileEntry *pNext;
5421 for(pEntry=pTab->pFirstEntry; pEntry; pEntry=pNext){
5422 pNext = pEntry->pNext;
5423 sqlite3_free(pEntry);
5425 pTab->pFirstEntry = 0;
5426 pTab->pLastEntry = 0;
5427 fclose(pTab->pWriteFd);
5429 pTab->szCurrent = 0;
5433 static int zipfileBegin(sqlite3_vtab *pVtab){
5434 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5437 assert( pTab->pWriteFd==0 );
5439 /* This table is only writable if a default archive path was specified
5440 ** as part of the CREATE VIRTUAL TABLE statement. */
5441 if( pTab->zFile==0 ){
5442 pTab->base.zErrMsg = sqlite3_mprintf(
5443 "zipfile: writing requires a default archive"
5445 return SQLITE_ERROR;
5448 /* Open a write fd on the file. Also load the entire central directory
5449 ** structure into memory. During the transaction any new file data is
5450 ** appended to the archive file, but the central directory is accumulated
5451 ** in main-memory until the transaction is committed. */
5452 pTab->pWriteFd = fopen(pTab->zFile, "ab+");
5453 if( pTab->pWriteFd==0 ){
5454 pTab->base.zErrMsg = sqlite3_mprintf(
5455 "zipfile: failed to open file %s for writing", pTab->zFile
5459 fseek(pTab->pWriteFd, 0, SEEK_END);
5460 pTab->szCurrent = pTab->szOrig = (i64)ftell(pTab->pWriteFd);
5461 rc = zipfileLoadDirectory(pTab);
5464 if( rc!=SQLITE_OK ){
5465 zipfileCleanupTransaction(pTab);
5471 static int zipfileCommit(sqlite3_vtab *pVtab){
5472 ZipfileTab *pTab = (ZipfileTab*)pVtab;
5474 if( pTab->pWriteFd ){
5475 i64 iOffset = pTab->szCurrent;
5480 /* Write out all undeleted entries */
5481 for(p=pTab->pFirstEntry; rc==SQLITE_OK && p; p=p->pNext){
5482 if( p->bDeleted ) continue;
5483 rc = zipfileAppendData(pTab, p->aCdsEntry, p->nCdsEntry);
5487 /* Write out the EOCD record */
5489 eocd.iFirstDisk = 0;
5490 eocd.nEntry = (u16)nEntry;
5491 eocd.nEntryTotal = (u16)nEntry;
5492 eocd.nSize = (u32)(pTab->szCurrent - iOffset);
5493 eocd.iOffset = (u32)iOffset;
5494 rc = zipfileAppendEOCD(pTab, &eocd);
5496 zipfileCleanupTransaction(pTab);
5501 static int zipfileRollback(sqlite3_vtab *pVtab){
5502 return zipfileCommit(pVtab);
5505 static ZipfileCsr *zipfileFindCursor(ZipfileTab *pTab, i64 iId){
5507 for(pCsr=pTab->pCsrList; pCsr; pCsr=pCsr->pCsrNext){
5508 if( iId==pCsr->iId ) break;
5513 static void zipfileFunctionCds(
5514 sqlite3_context *context,
5516 sqlite3_value **argv
5519 ZipfileTab *pTab = (ZipfileTab*)sqlite3_user_data(context);
5522 pCsr = zipfileFindCursor(pTab, sqlite3_value_int64(argv[0]));
5524 ZipfileCDS *p = &pCsr->cds;
5525 char *zRes = sqlite3_mprintf("{"
5526 "\"version-made-by\" : %u, "
5527 "\"version-to-extract\" : %u, "
5529 "\"compression\" : %u, "
5533 "\"compressed-size\" : %u, "
5534 "\"uncompressed-size\" : %u, "
5535 "\"file-name-length\" : %u, "
5536 "\"extra-field-length\" : %u, "
5537 "\"file-comment-length\" : %u, "
5538 "\"disk-number-start\" : %u, "
5539 "\"internal-attr\" : %u, "
5540 "\"external-attr\" : %u, "
5541 "\"offset\" : %u }",
5542 (u32)p->iVersionMadeBy, (u32)p->iVersionExtract,
5543 (u32)p->flags, (u32)p->iCompression,
5544 (u32)p->mTime, (u32)p->mDate,
5545 (u32)p->crc32, (u32)p->szCompressed,
5546 (u32)p->szUncompressed, (u32)p->nFile,
5547 (u32)p->nExtra, (u32)p->nComment,
5548 (u32)p->iDiskStart, (u32)p->iInternalAttr,
5549 (u32)p->iExternalAttr, (u32)p->iOffset
5553 sqlite3_result_error_nomem(context);
5555 sqlite3_result_text(context, zRes, -1, SQLITE_TRANSIENT);
5563 ** xFindFunction method.
5565 static int zipfileFindFunction(
5566 sqlite3_vtab *pVtab, /* Virtual table handle */
5567 int nArg, /* Number of SQL function arguments */
5568 const char *zName, /* Name of SQL function */
5569 void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
5570 void **ppArg /* OUT: User data for *pxFunc */
5573 if( sqlite3_stricmp("zipfile_cds", zName)==0 ){
5574 *pxFunc = zipfileFunctionCds;
5575 *ppArg = (void*)pVtab;
5584 ** Register the "zipfile" virtual table.
5586 static int zipfileRegister(sqlite3 *db){
5587 static sqlite3_module zipfileModule = {
5589 zipfileConnect, /* xCreate */
5590 zipfileConnect, /* xConnect */
5591 zipfileBestIndex, /* xBestIndex */
5592 zipfileDisconnect, /* xDisconnect */
5593 zipfileDisconnect, /* xDestroy */
5594 zipfileOpen, /* xOpen - open a cursor */
5595 zipfileClose, /* xClose - close a cursor */
5596 zipfileFilter, /* xFilter - configure scan constraints */
5597 zipfileNext, /* xNext - advance a cursor */
5598 zipfileEof, /* xEof - check for end of scan */
5599 zipfileColumn, /* xColumn - read data */
5600 zipfileRowid, /* xRowid - read data */
5601 zipfileUpdate, /* xUpdate */
5602 zipfileBegin, /* xBegin */
5604 zipfileCommit, /* xCommit */
5605 zipfileRollback, /* xRollback */
5606 zipfileFindFunction, /* xFindMethod */
5610 int rc = sqlite3_create_module(db, "zipfile" , &zipfileModule, 0);
5611 if( rc==SQLITE_OK ){
5612 rc = sqlite3_overload_function(db, "zipfile_cds", -1);
5616 #else /* SQLITE_OMIT_VIRTUALTABLE */
5617 # define zipfileRegister(x) SQLITE_OK
5623 int sqlite3_zipfile_init(
5626 const sqlite3_api_routines *pApi
5628 SQLITE_EXTENSION_INIT2(pApi);
5629 (void)pzErrMsg; /* Unused parameter */
5630 return zipfileRegister(db);
5633 /************************* End ../ext/misc/zipfile.c ********************/
5634 /************************* Begin ../ext/misc/sqlar.c ******************/
5638 ** The author disclaims copyright to this source code. In place of
5639 ** a legal notice, here is a blessing:
5641 ** May you do good and not evil.
5642 ** May you find forgiveness for yourself and forgive others.
5643 ** May you share freely, never taking more than you give.
5645 ******************************************************************************
5647 ** Utility functions sqlar_compress() and sqlar_uncompress(). Useful
5648 ** for working with sqlar archives and used by the shell tool's built-in
5651 SQLITE_EXTENSION_INIT1
5655 ** Implementation of the "sqlar_compress(X)" SQL function.
5657 ** If the type of X is SQLITE_BLOB, and compressing that blob using
5658 ** zlib utility function compress() yields a smaller blob, return the
5659 ** compressed blob. Otherwise, return a copy of X.
5661 ** SQLar uses the "zlib format" for compressed content. The zlib format
5662 ** contains a two-byte identification header and a four-byte checksum at
5663 ** the end. This is different from ZIP which uses the raw deflate format.
5665 ** Future enhancements to SQLar might add support for new compression formats.
5666 ** If so, those new formats will be identified by alternative headers in the
5669 static void sqlarCompressFunc(
5670 sqlite3_context *context,
5672 sqlite3_value **argv
5675 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
5676 const Bytef *pData = sqlite3_value_blob(argv[0]);
5677 uLong nData = sqlite3_value_bytes(argv[0]);
5678 uLongf nOut = compressBound(nData);
5681 pOut = (Bytef*)sqlite3_malloc(nOut);
5683 sqlite3_result_error_nomem(context);
5686 if( Z_OK!=compress(pOut, &nOut, pData, nData) ){
5687 sqlite3_result_error(context, "error in compress()", -1);
5688 }else if( nOut<nData ){
5689 sqlite3_result_blob(context, pOut, nOut, SQLITE_TRANSIENT);
5691 sqlite3_result_value(context, argv[0]);
5696 sqlite3_result_value(context, argv[0]);
5701 ** Implementation of the "sqlar_uncompress(X,SZ)" SQL function
5703 ** Parameter SZ is interpreted as an integer. If it is less than or
5704 ** equal to zero, then this function returns a copy of X. Or, if
5705 ** SZ is equal to the size of X when interpreted as a blob, also
5706 ** return a copy of X. Otherwise, decompress blob X using zlib
5707 ** utility function uncompress() and return the results (another
5710 static void sqlarUncompressFunc(
5711 sqlite3_context *context,
5713 sqlite3_value **argv
5719 sz = sqlite3_value_int(argv[1]);
5721 if( sz<=0 || sz==(nData = sqlite3_value_bytes(argv[0])) ){
5722 sqlite3_result_value(context, argv[0]);
5724 const Bytef *pData= sqlite3_value_blob(argv[0]);
5725 Bytef *pOut = sqlite3_malloc(sz);
5726 if( Z_OK!=uncompress(pOut, &sz, pData, nData) ){
5727 sqlite3_result_error(context, "error in uncompress()", -1);
5729 sqlite3_result_blob(context, pOut, sz, SQLITE_TRANSIENT);
5739 int sqlite3_sqlar_init(
5742 const sqlite3_api_routines *pApi
5745 SQLITE_EXTENSION_INIT2(pApi);
5746 (void)pzErrMsg; /* Unused parameter */
5747 rc = sqlite3_create_function(db, "sqlar_compress", 1, SQLITE_UTF8, 0,
5748 sqlarCompressFunc, 0, 0);
5749 if( rc==SQLITE_OK ){
5750 rc = sqlite3_create_function(db, "sqlar_uncompress", 2, SQLITE_UTF8, 0,
5751 sqlarUncompressFunc, 0, 0);
5756 /************************* End ../ext/misc/sqlar.c ********************/
5758 /************************* Begin ../ext/expert/sqlite3expert.h ******************/
5762 ** The author disclaims copyright to this source code. In place of
5763 ** a legal notice, here is a blessing:
5765 ** May you do good and not evil.
5766 ** May you find forgiveness for yourself and forgive others.
5767 ** May you share freely, never taking more than you give.
5769 *************************************************************************
5774 typedef struct sqlite3expert sqlite3expert;
5777 ** Create a new sqlite3expert object.
5779 ** If successful, a pointer to the new object is returned and (*pzErr) set
5780 ** to NULL. Or, if an error occurs, NULL is returned and (*pzErr) set to
5781 ** an English-language error message. In this case it is the responsibility
5782 ** of the caller to eventually free the error message buffer using
5785 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErr);
5788 ** Configure an sqlite3expert object.
5790 ** EXPERT_CONFIG_SAMPLE:
5791 ** By default, sqlite3_expert_analyze() generates sqlite_stat1 data for
5792 ** each candidate index. This involves scanning and sorting the entire
5793 ** contents of each user database table once for each candidate index
5794 ** associated with the table. For large databases, this can be
5795 ** prohibitively slow. This option allows the sqlite3expert object to
5796 ** be configured so that sqlite_stat1 data is instead generated based on a
5797 ** subset of each table, or so that no sqlite_stat1 data is used at all.
5799 ** A single integer argument is passed to this option. If the value is less
5800 ** than or equal to zero, then no sqlite_stat1 data is generated or used by
5801 ** the analysis - indexes are recommended based on the database schema only.
5802 ** Or, if the value is 100 or greater, complete sqlite_stat1 data is
5803 ** generated for each candidate index (this is the default). Finally, if the
5804 ** value falls between 0 and 100, then it represents the percentage of user
5805 ** table rows that should be considered when generating sqlite_stat1 data.
5809 ** // Do not generate any sqlite_stat1 data
5810 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 0);
5812 ** // Generate sqlite_stat1 data based on 10% of the rows in each table.
5813 ** sqlite3_expert_config(pExpert, EXPERT_CONFIG_SAMPLE, 10);
5815 int sqlite3_expert_config(sqlite3expert *p, int op, ...);
5817 #define EXPERT_CONFIG_SAMPLE 1 /* int */
5820 ** Specify zero or more SQL statements to be included in the analysis.
5822 ** Buffer zSql must contain zero or more complete SQL statements. This
5823 ** function parses all statements contained in the buffer and adds them
5824 ** to the internal list of statements to analyze. If successful, SQLITE_OK
5825 ** is returned and (*pzErr) set to NULL. Or, if an error occurs - for example
5826 ** due to a error in the SQL - an SQLite error code is returned and (*pzErr)
5827 ** may be set to point to an English language error message. In this case
5828 ** the caller is responsible for eventually freeing the error message buffer
5829 ** using sqlite3_free().
5831 ** If an error does occur while processing one of the statements in the
5832 ** buffer passed as the second argument, none of the statements in the
5833 ** buffer are added to the analysis.
5835 ** This function must be called before sqlite3_expert_analyze(). If a call
5836 ** to this function is made on an sqlite3expert object that has already
5837 ** been passed to sqlite3_expert_analyze() SQLITE_MISUSE is returned
5838 ** immediately and no statements are added to the analysis.
5840 int sqlite3_expert_sql(
5841 sqlite3expert *p, /* From a successful sqlite3_expert_new() */
5842 const char *zSql, /* SQL statement(s) to add */
5843 char **pzErr /* OUT: Error message (if any) */
5848 ** This function is called after the sqlite3expert object has been configured
5849 ** with all SQL statements using sqlite3_expert_sql() to actually perform
5850 ** the analysis. Once this function has been called, it is not possible to
5851 ** add further SQL statements to the analysis.
5853 ** If successful, SQLITE_OK is returned and (*pzErr) is set to NULL. Or, if
5854 ** an error occurs, an SQLite error code is returned and (*pzErr) set to
5855 ** point to a buffer containing an English language error message. In this
5856 ** case it is the responsibility of the caller to eventually free the buffer
5857 ** using sqlite3_free().
5859 ** If an error does occur within this function, the sqlite3expert object
5860 ** is no longer useful for any purpose. At that point it is no longer
5861 ** possible to add further SQL statements to the object or to re-attempt
5862 ** the analysis. The sqlite3expert object must still be freed using a call
5863 ** sqlite3_expert_destroy().
5865 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr);
5868 ** Return the total number of statements loaded using sqlite3_expert_sql().
5869 ** The total number of SQL statements may be different from the total number
5870 ** to calls to sqlite3_expert_sql().
5872 int sqlite3_expert_count(sqlite3expert*);
5875 ** Return a component of the report.
5877 ** This function is called after sqlite3_expert_analyze() to extract the
5878 ** results of the analysis. Each call to this function returns either a
5879 ** NULL pointer or a pointer to a buffer containing a nul-terminated string.
5880 ** The value passed as the third argument must be one of the EXPERT_REPORT_*
5881 ** #define constants defined below.
5883 ** For some EXPERT_REPORT_* parameters, the buffer returned contains
5884 ** information relating to a specific SQL statement. In these cases that
5885 ** SQL statement is identified by the value passed as the second argument.
5886 ** SQL statements are numbered from 0 in the order in which they are parsed.
5887 ** If an out-of-range value (less than zero or equal to or greater than the
5888 ** value returned by sqlite3_expert_count()) is passed as the second argument
5889 ** along with such an EXPERT_REPORT_* parameter, NULL is always returned.
5891 ** EXPERT_REPORT_SQL:
5892 ** Return the text of SQL statement iStmt.
5894 ** EXPERT_REPORT_INDEXES:
5895 ** Return a buffer containing the CREATE INDEX statements for all recommended
5896 ** indexes for statement iStmt. If there are no new recommeded indexes, NULL
5899 ** EXPERT_REPORT_PLAN:
5900 ** Return a buffer containing the EXPLAIN QUERY PLAN output for SQL query
5901 ** iStmt after the proposed indexes have been added to the database schema.
5903 ** EXPERT_REPORT_CANDIDATES:
5904 ** Return a pointer to a buffer containing the CREATE INDEX statements
5905 ** for all indexes that were tested (for all SQL statements). The iStmt
5906 ** parameter is ignored for EXPERT_REPORT_CANDIDATES calls.
5908 const char *sqlite3_expert_report(sqlite3expert*, int iStmt, int eReport);
5911 ** Values for the third argument passed to sqlite3_expert_report().
5913 #define EXPERT_REPORT_SQL 1
5914 #define EXPERT_REPORT_INDEXES 2
5915 #define EXPERT_REPORT_PLAN 3
5916 #define EXPERT_REPORT_CANDIDATES 4
5919 ** Free an (sqlite3expert*) handle and all associated resources. There
5920 ** should be one call to this function for each successful call to
5921 ** sqlite3-expert_new().
5923 void sqlite3_expert_destroy(sqlite3expert*);
5927 /************************* End ../ext/expert/sqlite3expert.h ********************/
5928 /************************* Begin ../ext/expert/sqlite3expert.c ******************/
5932 ** The author disclaims copyright to this source code. In place of
5933 ** a legal notice, here is a blessing:
5935 ** May you do good and not evil.
5936 ** May you find forgiveness for yourself and forgive others.
5937 ** May you share freely, never taking more than you give.
5939 *************************************************************************
5945 #ifndef SQLITE_OMIT_VIRTUALTABLE
5947 /* typedef sqlite3_int64 i64; */
5948 /* typedef sqlite3_uint64 u64; */
5950 typedef struct IdxColumn IdxColumn;
5951 typedef struct IdxConstraint IdxConstraint;
5952 typedef struct IdxScan IdxScan;
5953 typedef struct IdxStatement IdxStatement;
5954 typedef struct IdxTable IdxTable;
5955 typedef struct IdxWrite IdxWrite;
5957 #define STRLEN (int)strlen
5960 ** A temp table name that we assume no user database will actually use.
5961 ** If this assumption proves incorrect triggers on the table with the
5962 ** conflicting name will be ignored.
5964 #define UNIQUE_TABLE_NAME "t592690916721053953805701627921227776"
5967 ** A single constraint. Equivalent to either "col = ?" or "col < ?" (or
5968 ** any other type of single-ended range constraint on a column).
5971 ** Used to temporarily link IdxConstraint objects into lists while
5972 ** creating candidate indexes.
5974 struct IdxConstraint {
5975 char *zColl; /* Collation sequence */
5976 int bRange; /* True for range, false for eq */
5977 int iCol; /* Constrained table column */
5978 int bFlag; /* Used by idxFindCompatible() */
5979 int bDesc; /* True if ORDER BY <expr> DESC */
5980 IdxConstraint *pNext; /* Next constraint in pEq or pRange list */
5981 IdxConstraint *pLink; /* See above */
5985 ** A single scan of a single table.
5988 IdxTable *pTab; /* Associated table object */
5989 int iDb; /* Database containing table zTable */
5990 i64 covering; /* Mask of columns required for cov. index */
5991 IdxConstraint *pOrder; /* ORDER BY columns */
5992 IdxConstraint *pEq; /* List of == constraints */
5993 IdxConstraint *pRange; /* List of < constraints */
5994 IdxScan *pNextScan; /* Next IdxScan object for same analysis */
5998 ** Information regarding a single database table. Extracted from
5999 ** "PRAGMA table_info" by function idxGetTableInfo().
6008 char *zName; /* Table name */
6010 IdxTable *pNext; /* Next table in linked list of all tables */
6014 ** An object of the following type is created for each unique table/write-op
6015 ** seen. The objects are stored in a singly-linked list beginning at
6016 ** sqlite3expert.pWrite.
6020 int eOp; /* SQLITE_UPDATE, DELETE or INSERT */
6025 ** Each statement being analyzed is represented by an instance of this
6028 struct IdxStatement {
6029 int iId; /* Statement number */
6030 char *zSql; /* SQL statement */
6031 char *zIdx; /* Indexes */
6032 char *zEQP; /* Plan */
6033 IdxStatement *pNext;
6038 ** A hash table for storing strings. With space for a payload string
6039 ** with each entry. Methods are:
6046 #define IDX_HASH_SIZE 1023
6047 typedef struct IdxHashEntry IdxHashEntry;
6048 typedef struct IdxHash IdxHash;
6049 struct IdxHashEntry {
6050 char *zKey; /* nul-terminated key */
6051 char *zVal; /* nul-terminated value string */
6052 char *zVal2; /* nul-terminated value string 2 */
6053 IdxHashEntry *pHashNext; /* Next entry in same hash bucket */
6054 IdxHashEntry *pNext; /* Next entry in hash */
6057 IdxHashEntry *pFirst;
6058 IdxHashEntry *aHash[IDX_HASH_SIZE];
6062 ** sqlite3expert object.
6064 struct sqlite3expert {
6065 int iSample; /* Percentage of tables to sample for stat1 */
6066 sqlite3 *db; /* User database */
6067 sqlite3 *dbm; /* In-memory db for this analysis */
6068 sqlite3 *dbv; /* Vtab schema for this analysis */
6069 IdxTable *pTable; /* List of all IdxTable objects */
6070 IdxScan *pScan; /* List of scan objects */
6071 IdxWrite *pWrite; /* List of write objects */
6072 IdxStatement *pStatement; /* List of IdxStatement objects */
6073 int bRun; /* True once analysis has run */
6075 int rc; /* Error code from whereinfo hook */
6076 IdxHash hIdx; /* Hash containing all candidate indexes */
6077 char *zCandidates; /* For EXPERT_REPORT_CANDIDATES */
6082 ** Allocate and return nByte bytes of zeroed memory using sqlite3_malloc().
6083 ** If the allocation fails, set *pRc to SQLITE_NOMEM and return NULL.
6085 static void *idxMalloc(int *pRc, int nByte){
6087 assert( *pRc==SQLITE_OK );
6089 pRet = sqlite3_malloc(nByte);
6091 memset(pRet, 0, nByte);
6093 *pRc = SQLITE_NOMEM;
6099 ** Initialize an IdxHash hash table.
6101 static void idxHashInit(IdxHash *pHash){
6102 memset(pHash, 0, sizeof(IdxHash));
6106 ** Reset an IdxHash hash table.
6108 static void idxHashClear(IdxHash *pHash){
6110 for(i=0; i<IDX_HASH_SIZE; i++){
6111 IdxHashEntry *pEntry;
6112 IdxHashEntry *pNext;
6113 for(pEntry=pHash->aHash[i]; pEntry; pEntry=pNext){
6114 pNext = pEntry->pHashNext;
6115 sqlite3_free(pEntry->zVal2);
6116 sqlite3_free(pEntry);
6119 memset(pHash, 0, sizeof(IdxHash));
6123 ** Return the index of the hash bucket that the string specified by the
6124 ** arguments to this function belongs.
6126 static int idxHashString(const char *z, int n){
6127 unsigned int ret = 0;
6130 ret += (ret<<3) + (unsigned char)(z[i]);
6132 return (int)(ret % IDX_HASH_SIZE);
6136 ** If zKey is already present in the hash table, return non-zero and do
6137 ** nothing. Otherwise, add an entry with key zKey and payload string zVal to
6138 ** the hash table passed as the second argument.
6140 static int idxHashAdd(
6146 int nKey = STRLEN(zKey);
6147 int iHash = idxHashString(zKey, nKey);
6148 int nVal = (zVal ? STRLEN(zVal) : 0);
6149 IdxHashEntry *pEntry;
6151 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6152 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6156 pEntry = idxMalloc(pRc, sizeof(IdxHashEntry) + nKey+1 + nVal+1);
6158 pEntry->zKey = (char*)&pEntry[1];
6159 memcpy(pEntry->zKey, zKey, nKey);
6161 pEntry->zVal = &pEntry->zKey[nKey+1];
6162 memcpy(pEntry->zVal, zVal, nVal);
6164 pEntry->pHashNext = pHash->aHash[iHash];
6165 pHash->aHash[iHash] = pEntry;
6167 pEntry->pNext = pHash->pFirst;
6168 pHash->pFirst = pEntry;
6174 ** If zKey/nKey is present in the hash table, return a pointer to the
6175 ** hash-entry object.
6177 static IdxHashEntry *idxHashFind(IdxHash *pHash, const char *zKey, int nKey){
6179 IdxHashEntry *pEntry;
6180 if( nKey<0 ) nKey = STRLEN(zKey);
6181 iHash = idxHashString(zKey, nKey);
6183 for(pEntry=pHash->aHash[iHash]; pEntry; pEntry=pEntry->pHashNext){
6184 if( STRLEN(pEntry->zKey)==nKey && 0==memcmp(pEntry->zKey, zKey, nKey) ){
6192 ** If the hash table contains an entry with a key equal to the string
6193 ** passed as the final two arguments to this function, return a pointer
6194 ** to the payload string. Otherwise, if zKey/nKey is not present in the
6195 ** hash table, return NULL.
6197 static const char *idxHashSearch(IdxHash *pHash, const char *zKey, int nKey){
6198 IdxHashEntry *pEntry = idxHashFind(pHash, zKey, nKey);
6199 if( pEntry ) return pEntry->zVal;
6204 ** Allocate and return a new IdxConstraint object. Set the IdxConstraint.zColl
6205 ** variable to point to a copy of nul-terminated string zColl.
6207 static IdxConstraint *idxNewConstraint(int *pRc, const char *zColl){
6208 IdxConstraint *pNew;
6209 int nColl = STRLEN(zColl);
6211 assert( *pRc==SQLITE_OK );
6212 pNew = (IdxConstraint*)idxMalloc(pRc, sizeof(IdxConstraint) * nColl + 1);
6214 pNew->zColl = (char*)&pNew[1];
6215 memcpy(pNew->zColl, zColl, nColl+1);
6221 ** An error associated with database handle db has just occurred. Pass
6222 ** the error message to callback function xOut.
6224 static void idxDatabaseError(
6225 sqlite3 *db, /* Database handle */
6226 char **pzErrmsg /* Write error here */
6228 *pzErrmsg = sqlite3_mprintf("%s", sqlite3_errmsg(db));
6232 ** Prepare an SQL statement.
6234 static int idxPrepareStmt(
6235 sqlite3 *db, /* Database handle to compile against */
6236 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6237 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6238 const char *zSql /* SQL statement to compile */
6240 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
6241 if( rc!=SQLITE_OK ){
6243 idxDatabaseError(db, pzErrmsg);
6249 ** Prepare an SQL statement using the results of a printf() formatting.
6251 static int idxPrintfPrepareStmt(
6252 sqlite3 *db, /* Database handle to compile against */
6253 sqlite3_stmt **ppStmt, /* OUT: Compiled SQL statement */
6254 char **pzErrmsg, /* OUT: sqlite3_malloc()ed error message */
6255 const char *zFmt, /* printf() format of SQL statement */
6256 ... /* Trailing printf() arguments */
6262 zSql = sqlite3_vmprintf(zFmt, ap);
6266 rc = idxPrepareStmt(db, ppStmt, pzErrmsg, zSql);
6274 /*************************************************************************
6275 ** Beginning of virtual table implementation.
6277 typedef struct ExpertVtab ExpertVtab;
6281 sqlite3expert *pExpert;
6284 typedef struct ExpertCsr ExpertCsr;
6286 sqlite3_vtab_cursor base;
6287 sqlite3_stmt *pData;
6290 static char *expertDequote(const char *zIn){
6291 int n = STRLEN(zIn);
6292 char *zRet = sqlite3_malloc(n);
6294 assert( zIn[0]=='\'' );
6295 assert( zIn[n-1]=='\'' );
6300 for(iIn=1; iIn<(n-1); iIn++){
6301 if( zIn[iIn]=='\'' ){
6302 assert( zIn[iIn+1]=='\'' );
6305 zRet[iOut++] = zIn[iIn];
6314 ** This function is the implementation of both the xConnect and xCreate
6315 ** methods of the r-tree virtual table.
6317 ** argv[0] -> module name
6318 ** argv[1] -> database name
6319 ** argv[2] -> table name
6320 ** argv[...] -> column names...
6322 static int expertConnect(
6325 int argc, const char *const*argv,
6326 sqlite3_vtab **ppVtab,
6329 sqlite3expert *pExpert = (sqlite3expert*)pAux;
6334 *pzErr = sqlite3_mprintf("internal error!");
6337 char *zCreateTable = expertDequote(argv[3]);
6339 rc = sqlite3_declare_vtab(db, zCreateTable);
6340 if( rc==SQLITE_OK ){
6341 p = idxMalloc(&rc, sizeof(ExpertVtab));
6343 if( rc==SQLITE_OK ){
6344 p->pExpert = pExpert;
6345 p->pTab = pExpert->pTable;
6346 assert( sqlite3_stricmp(p->pTab->zName, argv[2])==0 );
6348 sqlite3_free(zCreateTable);
6354 *ppVtab = (sqlite3_vtab*)p;
6358 static int expertDisconnect(sqlite3_vtab *pVtab){
6359 ExpertVtab *p = (ExpertVtab*)pVtab;
6364 static int expertBestIndex(sqlite3_vtab *pVtab, sqlite3_index_info *pIdxInfo){
6365 ExpertVtab *p = (ExpertVtab*)pVtab;
6370 SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_GT |
6371 SQLITE_INDEX_CONSTRAINT_LT | SQLITE_INDEX_CONSTRAINT_GE |
6372 SQLITE_INDEX_CONSTRAINT_LE;
6374 pScan = idxMalloc(&rc, sizeof(IdxScan));
6378 /* Link the new scan object into the list */
6379 pScan->pTab = p->pTab;
6380 pScan->pNextScan = p->pExpert->pScan;
6381 p->pExpert->pScan = pScan;
6383 /* Add the constraints to the IdxScan object */
6384 for(i=0; i<pIdxInfo->nConstraint; i++){
6385 struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
6387 && pCons->iColumn>=0
6388 && p->pTab->aCol[pCons->iColumn].iPk==0
6389 && (pCons->op & opmask)
6391 IdxConstraint *pNew;
6392 const char *zColl = sqlite3_vtab_collation(pIdxInfo, i);
6393 pNew = idxNewConstraint(&rc, zColl);
6395 pNew->iCol = pCons->iColumn;
6396 if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ ){
6397 pNew->pNext = pScan->pEq;
6401 pNew->pNext = pScan->pRange;
6402 pScan->pRange = pNew;
6406 pIdxInfo->aConstraintUsage[i].argvIndex = n;
6410 /* Add the ORDER BY to the IdxScan object */
6411 for(i=pIdxInfo->nOrderBy-1; i>=0; i--){
6412 int iCol = pIdxInfo->aOrderBy[i].iColumn;
6414 IdxConstraint *pNew = idxNewConstraint(&rc, p->pTab->aCol[iCol].zColl);
6417 pNew->bDesc = pIdxInfo->aOrderBy[i].desc;
6418 pNew->pNext = pScan->pOrder;
6419 pNew->pLink = pScan->pOrder;
6420 pScan->pOrder = pNew;
6427 pIdxInfo->estimatedCost = 1000000.0 / (n+1);
6431 static int expertUpdate(
6432 sqlite3_vtab *pVtab,
6434 sqlite3_value **azData,
6435 sqlite_int64 *pRowid
6445 ** Virtual table module xOpen method.
6447 static int expertOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
6451 pCsr = idxMalloc(&rc, sizeof(ExpertCsr));
6452 *ppCursor = (sqlite3_vtab_cursor*)pCsr;
6457 ** Virtual table module xClose method.
6459 static int expertClose(sqlite3_vtab_cursor *cur){
6460 ExpertCsr *pCsr = (ExpertCsr*)cur;
6461 sqlite3_finalize(pCsr->pData);
6467 ** Virtual table module xEof method.
6469 ** Return non-zero if the cursor does not currently point to a valid
6470 ** record (i.e if the scan has finished), or zero otherwise.
6472 static int expertEof(sqlite3_vtab_cursor *cur){
6473 ExpertCsr *pCsr = (ExpertCsr*)cur;
6474 return pCsr->pData==0;
6478 ** Virtual table module xNext method.
6480 static int expertNext(sqlite3_vtab_cursor *cur){
6481 ExpertCsr *pCsr = (ExpertCsr*)cur;
6484 assert( pCsr->pData );
6485 rc = sqlite3_step(pCsr->pData);
6486 if( rc!=SQLITE_ROW ){
6487 rc = sqlite3_finalize(pCsr->pData);
6497 ** Virtual table module xRowid method.
6499 static int expertRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
6506 ** Virtual table module xColumn method.
6508 static int expertColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
6509 ExpertCsr *pCsr = (ExpertCsr*)cur;
6510 sqlite3_value *pVal;
6511 pVal = sqlite3_column_value(pCsr->pData, i);
6513 sqlite3_result_value(ctx, pVal);
6519 ** Virtual table module xFilter method.
6521 static int expertFilter(
6522 sqlite3_vtab_cursor *cur,
6523 int idxNum, const char *idxStr,
6524 int argc, sqlite3_value **argv
6526 ExpertCsr *pCsr = (ExpertCsr*)cur;
6527 ExpertVtab *pVtab = (ExpertVtab*)(cur->pVtab);
6528 sqlite3expert *pExpert = pVtab->pExpert;
6535 rc = sqlite3_finalize(pCsr->pData);
6537 if( rc==SQLITE_OK ){
6538 rc = idxPrintfPrepareStmt(pExpert->db, &pCsr->pData, &pVtab->base.zErrMsg,
6539 "SELECT * FROM main.%Q WHERE sample()", pVtab->pTab->zName
6543 if( rc==SQLITE_OK ){
6544 rc = expertNext(cur);
6549 static int idxRegisterVtab(sqlite3expert *p){
6550 static sqlite3_module expertModule = {
6552 expertConnect, /* xCreate - create a table */
6553 expertConnect, /* xConnect - connect to an existing table */
6554 expertBestIndex, /* xBestIndex - Determine search strategy */
6555 expertDisconnect, /* xDisconnect - Disconnect from a table */
6556 expertDisconnect, /* xDestroy - Drop a table */
6557 expertOpen, /* xOpen - open a cursor */
6558 expertClose, /* xClose - close a cursor */
6559 expertFilter, /* xFilter - configure scan constraints */
6560 expertNext, /* xNext - advance a cursor */
6561 expertEof, /* xEof */
6562 expertColumn, /* xColumn - read data */
6563 expertRowid, /* xRowid - read data */
6564 expertUpdate, /* xUpdate - write data */
6565 0, /* xBegin - begin transaction */
6566 0, /* xSync - sync transaction */
6567 0, /* xCommit - commit transaction */
6568 0, /* xRollback - rollback transaction */
6569 0, /* xFindFunction - function overloading */
6570 0, /* xRename - rename the table */
6573 0, /* xRollbackTo */
6576 return sqlite3_create_module(p->dbv, "expert", &expertModule, (void*)p);
6579 ** End of virtual table implementation.
6580 *************************************************************************/
6582 ** Finalize SQL statement pStmt. If (*pRc) is SQLITE_OK when this function
6583 ** is called, set it to the return value of sqlite3_finalize() before
6584 ** returning. Otherwise, discard the sqlite3_finalize() return value.
6586 static void idxFinalize(int *pRc, sqlite3_stmt *pStmt){
6587 int rc = sqlite3_finalize(pStmt);
6588 if( *pRc==SQLITE_OK ) *pRc = rc;
6592 ** Attempt to allocate an IdxTable structure corresponding to table zTab
6593 ** in the main database of connection db. If successful, set (*ppOut) to
6594 ** point to the new object and return SQLITE_OK. Otherwise, return an
6595 ** SQLite error code and set (*ppOut) to NULL. In this case *pzErrmsg may be
6596 ** set to point to an error string.
6598 ** It is the responsibility of the caller to eventually free either the
6599 ** IdxTable object or error message using sqlite3_free().
6601 static int idxGetTableInfo(
6602 sqlite3 *db, /* Database connection to read details from */
6603 const char *zTab, /* Table name */
6604 IdxTable **ppOut, /* OUT: New object (if successful) */
6605 char **pzErrmsg /* OUT: Error message (if not) */
6607 sqlite3_stmt *p1 = 0;
6609 int nTab = STRLEN(zTab);
6610 int nByte = sizeof(IdxTable) + nTab + 1;
6615 rc = idxPrintfPrepareStmt(db, &p1, pzErrmsg, "PRAGMA table_info=%Q", zTab);
6616 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
6617 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
6618 nByte += 1 + STRLEN(zCol);
6619 rc = sqlite3_table_column_metadata(
6620 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
6622 nByte += 1 + STRLEN(zCol);
6625 rc2 = sqlite3_reset(p1);
6626 if( rc==SQLITE_OK ) rc = rc2;
6628 nByte += sizeof(IdxColumn) * nCol;
6629 if( rc==SQLITE_OK ){
6630 pNew = idxMalloc(&rc, nByte);
6632 if( rc==SQLITE_OK ){
6633 pNew->aCol = (IdxColumn*)&pNew[1];
6635 pCsr = (char*)&pNew->aCol[nCol];
6639 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(p1) ){
6640 const char *zCol = (const char*)sqlite3_column_text(p1, 1);
6641 int nCopy = STRLEN(zCol) + 1;
6642 pNew->aCol[nCol].zName = pCsr;
6643 pNew->aCol[nCol].iPk = sqlite3_column_int(p1, 5);
6644 memcpy(pCsr, zCol, nCopy);
6647 rc = sqlite3_table_column_metadata(
6648 db, "main", zTab, zCol, 0, &zCol, 0, 0, 0
6650 if( rc==SQLITE_OK ){
6651 nCopy = STRLEN(zCol) + 1;
6652 pNew->aCol[nCol].zColl = pCsr;
6653 memcpy(pCsr, zCol, nCopy);
6659 idxFinalize(&rc, p1);
6661 if( rc!=SQLITE_OK ){
6666 memcpy(pNew->zName, zTab, nTab+1);
6674 ** This function is a no-op if *pRc is set to anything other than
6675 ** SQLITE_OK when it is called.
6677 ** If *pRc is initially set to SQLITE_OK, then the text specified by
6678 ** the printf() style arguments is appended to zIn and the result returned
6679 ** in a buffer allocated by sqlite3_malloc(). sqlite3_free() is called on
6680 ** zIn before returning.
6682 static char *idxAppendText(int *pRc, char *zIn, const char *zFmt, ...){
6686 int nIn = zIn ? STRLEN(zIn) : 0;
6689 if( *pRc==SQLITE_OK ){
6690 zAppend = sqlite3_vmprintf(zFmt, ap);
6692 nAppend = STRLEN(zAppend);
6693 zRet = (char*)sqlite3_malloc(nIn + nAppend + 1);
6695 if( zAppend && zRet ){
6696 if( nIn ) memcpy(zRet, zIn, nIn);
6697 memcpy(&zRet[nIn], zAppend, nAppend+1);
6701 *pRc = SQLITE_NOMEM;
6703 sqlite3_free(zAppend);
6711 ** Return true if zId must be quoted in order to use it as an SQL
6712 ** identifier, or false otherwise.
6714 static int idxIdentifierRequiresQuotes(const char *zId){
6716 for(i=0; zId[i]; i++){
6718 && !(zId[i]>='0' && zId[i]<='9')
6719 && !(zId[i]>='a' && zId[i]<='z')
6720 && !(zId[i]>='A' && zId[i]<='Z')
6729 ** This function appends an index column definition suitable for constraint
6730 ** pCons to the string passed as zIn and returns the result.
6732 static char *idxAppendColDefn(
6733 int *pRc, /* IN/OUT: Error code */
6734 char *zIn, /* Column defn accumulated so far */
6735 IdxTable *pTab, /* Table index will be created on */
6736 IdxConstraint *pCons
6739 IdxColumn *p = &pTab->aCol[pCons->iCol];
6740 if( zRet ) zRet = idxAppendText(pRc, zRet, ", ");
6742 if( idxIdentifierRequiresQuotes(p->zName) ){
6743 zRet = idxAppendText(pRc, zRet, "%Q", p->zName);
6745 zRet = idxAppendText(pRc, zRet, "%s", p->zName);
6748 if( sqlite3_stricmp(p->zColl, pCons->zColl) ){
6749 if( idxIdentifierRequiresQuotes(pCons->zColl) ){
6750 zRet = idxAppendText(pRc, zRet, " COLLATE %Q", pCons->zColl);
6752 zRet = idxAppendText(pRc, zRet, " COLLATE %s", pCons->zColl);
6757 zRet = idxAppendText(pRc, zRet, " DESC");
6763 ** Search database dbm for an index compatible with the one idxCreateFromCons()
6764 ** would create from arguments pScan, pEq and pTail. If no error occurs and
6765 ** such an index is found, return non-zero. Or, if no such index is found,
6768 ** If an error occurs, set *pRc to an SQLite error code and return zero.
6770 static int idxFindCompatible(
6771 int *pRc, /* OUT: Error code */
6772 sqlite3* dbm, /* Database to search */
6773 IdxScan *pScan, /* Scan for table to search for index on */
6774 IdxConstraint *pEq, /* List of == constraints */
6775 IdxConstraint *pTail /* List of range constraints */
6777 const char *zTbl = pScan->pTab->zName;
6778 sqlite3_stmt *pIdxList = 0;
6779 IdxConstraint *pIter;
6780 int nEq = 0; /* Number of elements in pEq */
6783 /* Count the elements in list pEq */
6784 for(pIter=pEq; pIter; pIter=pIter->pLink) nEq++;
6786 rc = idxPrintfPrepareStmt(dbm, &pIdxList, 0, "PRAGMA index_list=%Q", zTbl);
6787 while( rc==SQLITE_OK && sqlite3_step(pIdxList)==SQLITE_ROW ){
6789 IdxConstraint *pT = pTail;
6790 sqlite3_stmt *pInfo = 0;
6791 const char *zIdx = (const char*)sqlite3_column_text(pIdxList, 1);
6793 /* Zero the IdxConstraint.bFlag values in the pEq list */
6794 for(pIter=pEq; pIter; pIter=pIter->pLink) pIter->bFlag = 0;
6796 rc = idxPrintfPrepareStmt(dbm, &pInfo, 0, "PRAGMA index_xInfo=%Q", zIdx);
6797 while( rc==SQLITE_OK && sqlite3_step(pInfo)==SQLITE_ROW ){
6798 int iIdx = sqlite3_column_int(pInfo, 0);
6799 int iCol = sqlite3_column_int(pInfo, 1);
6800 const char *zColl = (const char*)sqlite3_column_text(pInfo, 4);
6803 for(pIter=pEq; pIter; pIter=pIter->pLink){
6804 if( pIter->bFlag ) continue;
6805 if( pIter->iCol!=iCol ) continue;
6806 if( sqlite3_stricmp(pIter->zColl, zColl) ) continue;
6816 if( pT->iCol!=iCol || sqlite3_stricmp(pT->zColl, zColl) ){
6824 idxFinalize(&rc, pInfo);
6826 if( rc==SQLITE_OK && bMatch ){
6827 sqlite3_finalize(pIdxList);
6831 idxFinalize(&rc, pIdxList);
6837 static int idxCreateFromCons(
6841 IdxConstraint *pTail
6843 sqlite3 *dbm = p->dbm;
6845 if( (pEq || pTail) && 0==idxFindCompatible(&rc, dbm, pScan, pEq, pTail) ){
6846 IdxTable *pTab = pScan->pTab;
6849 IdxConstraint *pCons;
6853 for(pCons=pEq; pCons; pCons=pCons->pLink){
6854 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
6856 for(pCons=pTail; pCons; pCons=pCons->pLink){
6857 zCols = idxAppendColDefn(&rc, zCols, pTab, pCons);
6860 if( rc==SQLITE_OK ){
6861 /* Hash the list of columns to come up with a name for the index */
6862 const char *zTable = pScan->pTab->zName;
6863 char *zName; /* Index name */
6865 for(i=0; zCols[i]; i++){
6866 h += ((h<<3) + zCols[i]);
6868 zName = sqlite3_mprintf("%s_idx_%08x", zTable, h);
6872 if( idxIdentifierRequiresQuotes(zTable) ){
6873 zFmt = "CREATE INDEX '%q' ON %Q(%s)";
6875 zFmt = "CREATE INDEX %s ON %s(%s)";
6877 zIdx = sqlite3_mprintf(zFmt, zName, zTable, zCols);
6881 rc = sqlite3_exec(dbm, zIdx, 0, 0, p->pzErrmsg);
6882 idxHashAdd(&rc, &p->hIdx, zName, zIdx);
6884 sqlite3_free(zName);
6889 sqlite3_free(zCols);
6895 ** Return true if list pList (linked by IdxConstraint.pLink) contains
6896 ** a constraint compatible with *p. Otherwise return false.
6898 static int idxFindConstraint(IdxConstraint *pList, IdxConstraint *p){
6899 IdxConstraint *pCmp;
6900 for(pCmp=pList; pCmp; pCmp=pCmp->pLink){
6901 if( p->iCol==pCmp->iCol ) return 1;
6906 static int idxCreateFromWhere(
6908 IdxScan *pScan, /* Create indexes for this scan */
6909 IdxConstraint *pTail /* range/ORDER BY constraints for inclusion */
6911 IdxConstraint *p1 = 0;
6912 IdxConstraint *pCon;
6915 /* Gather up all the == constraints. */
6916 for(pCon=pScan->pEq; pCon; pCon=pCon->pNext){
6917 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
6923 /* Create an index using the == constraints collected above. And the
6924 ** range constraint/ORDER BY terms passed in by the caller, if any. */
6925 rc = idxCreateFromCons(p, pScan, p1, pTail);
6927 /* If no range/ORDER BY passed by the caller, create a version of the
6928 ** index for each range constraint. */
6930 for(pCon=pScan->pRange; rc==SQLITE_OK && pCon; pCon=pCon->pNext){
6931 assert( pCon->pLink==0 );
6932 if( !idxFindConstraint(p1, pCon) && !idxFindConstraint(pTail, pCon) ){
6933 rc = idxCreateFromCons(p, pScan, p1, pCon);
6942 ** Create candidate indexes in database [dbm] based on the data in
6943 ** linked-list pScan.
6945 static int idxCreateCandidates(sqlite3expert *p){
6949 for(pIter=p->pScan; pIter && rc==SQLITE_OK; pIter=pIter->pNextScan){
6950 rc = idxCreateFromWhere(p, pIter, 0);
6951 if( rc==SQLITE_OK && pIter->pOrder ){
6952 rc = idxCreateFromWhere(p, pIter, pIter->pOrder);
6960 ** Free all elements of the linked list starting at pConstraint.
6962 static void idxConstraintFree(IdxConstraint *pConstraint){
6963 IdxConstraint *pNext;
6966 for(p=pConstraint; p; p=pNext){
6973 ** Free all elements of the linked list starting from pScan up until pLast
6974 ** (pLast is not freed).
6976 static void idxScanFree(IdxScan *pScan, IdxScan *pLast){
6979 for(p=pScan; p!=pLast; p=pNext){
6980 pNext = p->pNextScan;
6981 idxConstraintFree(p->pOrder);
6982 idxConstraintFree(p->pEq);
6983 idxConstraintFree(p->pRange);
6989 ** Free all elements of the linked list starting from pStatement up
6990 ** until pLast (pLast is not freed).
6992 static void idxStatementFree(IdxStatement *pStatement, IdxStatement *pLast){
6994 IdxStatement *pNext;
6995 for(p=pStatement; p!=pLast; p=pNext){
6997 sqlite3_free(p->zEQP);
6998 sqlite3_free(p->zIdx);
7004 ** Free the linked list of IdxTable objects starting at pTab.
7006 static void idxTableFree(IdxTable *pTab){
7009 for(pIter=pTab; pIter; pIter=pNext){
7010 pNext = pIter->pNext;
7011 sqlite3_free(pIter);
7016 ** Free the linked list of IdxWrite objects starting at pTab.
7018 static void idxWriteFree(IdxWrite *pTab){
7021 for(pIter=pTab; pIter; pIter=pNext){
7022 pNext = pIter->pNext;
7023 sqlite3_free(pIter);
7030 ** This function is called after candidate indexes have been created. It
7031 ** runs all the queries to see which indexes they prefer, and populates
7032 ** IdxStatement.zIdx and IdxStatement.zEQP with the results.
7036 char **pzErr /* OUT: Error message (sqlite3_malloc) */
7038 IdxStatement *pStmt;
7039 sqlite3 *dbm = p->dbm;
7045 for(pStmt=p->pStatement; rc==SQLITE_OK && pStmt; pStmt=pStmt->pNext){
7046 IdxHashEntry *pEntry;
7047 sqlite3_stmt *pExplain = 0;
7048 idxHashClear(&hIdx);
7049 rc = idxPrintfPrepareStmt(dbm, &pExplain, pzErr,
7050 "EXPLAIN QUERY PLAN %s", pStmt->zSql
7052 while( rc==SQLITE_OK && sqlite3_step(pExplain)==SQLITE_ROW ){
7053 int iSelectid = sqlite3_column_int(pExplain, 0);
7054 int iOrder = sqlite3_column_int(pExplain, 1);
7055 int iFrom = sqlite3_column_int(pExplain, 2);
7056 const char *zDetail = (const char*)sqlite3_column_text(pExplain, 3);
7057 int nDetail = STRLEN(zDetail);
7060 for(i=0; i<nDetail; i++){
7061 const char *zIdx = 0;
7062 if( memcmp(&zDetail[i], " USING INDEX ", 13)==0 ){
7063 zIdx = &zDetail[i+13];
7064 }else if( memcmp(&zDetail[i], " USING COVERING INDEX ", 22)==0 ){
7065 zIdx = &zDetail[i+22];
7070 while( zIdx[nIdx]!='\0' && (zIdx[nIdx]!=' ' || zIdx[nIdx+1]!='(') ){
7073 zSql = idxHashSearch(&p->hIdx, zIdx, nIdx);
7075 idxHashAdd(&rc, &hIdx, zSql, 0);
7076 if( rc ) goto find_indexes_out;
7082 pStmt->zEQP = idxAppendText(&rc, pStmt->zEQP, "%d|%d|%d|%s\n",
7083 iSelectid, iOrder, iFrom, zDetail
7087 for(pEntry=hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7088 pStmt->zIdx = idxAppendText(&rc, pStmt->zIdx, "%s;\n", pEntry->zKey);
7091 idxFinalize(&rc, pExplain);
7095 idxHashClear(&hIdx);
7099 static int idxAuthCallback(
7105 const char *zTrigger
7110 if( eOp==SQLITE_INSERT || eOp==SQLITE_UPDATE || eOp==SQLITE_DELETE ){
7111 if( sqlite3_stricmp(zDb, "main")==0 ){
7112 sqlite3expert *p = (sqlite3expert*)pCtx;
7114 for(pTab=p->pTable; pTab; pTab=pTab->pNext){
7115 if( 0==sqlite3_stricmp(z3, pTab->zName) ) break;
7119 for(pWrite=p->pWrite; pWrite; pWrite=pWrite->pNext){
7120 if( pWrite->pTab==pTab && pWrite->eOp==eOp ) break;
7123 pWrite = idxMalloc(&rc, sizeof(IdxWrite));
7124 if( rc==SQLITE_OK ){
7125 pWrite->pTab = pTab;
7127 pWrite->pNext = p->pWrite;
7137 static int idxProcessOneTrigger(
7142 static const char *zInt = UNIQUE_TABLE_NAME;
7143 static const char *zDrop = "DROP TABLE " UNIQUE_TABLE_NAME;
7144 IdxTable *pTab = pWrite->pTab;
7145 const char *zTab = pTab->zName;
7147 "SELECT 'CREATE TEMP' || substr(sql, 7) FROM sqlite_master "
7148 "WHERE tbl_name = %Q AND type IN ('table', 'trigger') "
7150 sqlite3_stmt *pSelect = 0;
7154 /* Create the table and its triggers in the temp schema */
7155 rc = idxPrintfPrepareStmt(p->db, &pSelect, pzErr, zSql, zTab, zTab);
7156 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSelect) ){
7157 const char *zCreate = (const char*)sqlite3_column_text(pSelect, 0);
7158 rc = sqlite3_exec(p->dbv, zCreate, 0, 0, pzErr);
7160 idxFinalize(&rc, pSelect);
7162 /* Rename the table in the temp schema to zInt */
7163 if( rc==SQLITE_OK ){
7164 char *z = sqlite3_mprintf("ALTER TABLE temp.%Q RENAME TO %Q", zTab, zInt);
7168 rc = sqlite3_exec(p->dbv, z, 0, 0, pzErr);
7173 switch( pWrite->eOp ){
7174 case SQLITE_INSERT: {
7176 zWrite = idxAppendText(&rc, zWrite, "INSERT INTO %Q VALUES(", zInt);
7177 for(i=0; i<pTab->nCol; i++){
7178 zWrite = idxAppendText(&rc, zWrite, "%s?", i==0 ? "" : ", ");
7180 zWrite = idxAppendText(&rc, zWrite, ")");
7183 case SQLITE_UPDATE: {
7185 zWrite = idxAppendText(&rc, zWrite, "UPDATE %Q SET ", zInt);
7186 for(i=0; i<pTab->nCol; i++){
7187 zWrite = idxAppendText(&rc, zWrite, "%s%Q=?", i==0 ? "" : ", ",
7194 assert( pWrite->eOp==SQLITE_DELETE );
7195 if( rc==SQLITE_OK ){
7196 zWrite = sqlite3_mprintf("DELETE FROM %Q", zInt);
7197 if( zWrite==0 ) rc = SQLITE_NOMEM;
7202 if( rc==SQLITE_OK ){
7203 sqlite3_stmt *pX = 0;
7204 rc = sqlite3_prepare_v2(p->dbv, zWrite, -1, &pX, 0);
7205 idxFinalize(&rc, pX);
7206 if( rc!=SQLITE_OK ){
7207 idxDatabaseError(p->dbv, pzErr);
7210 sqlite3_free(zWrite);
7212 if( rc==SQLITE_OK ){
7213 rc = sqlite3_exec(p->dbv, zDrop, 0, 0, pzErr);
7219 static int idxProcessTriggers(sqlite3expert *p, char **pzErr){
7222 IdxWrite *pFirst = p->pWrite;
7224 while( rc==SQLITE_OK && pFirst!=pEnd ){
7226 for(pIter=pFirst; rc==SQLITE_OK && pIter!=pEnd; pIter=pIter->pNext){
7227 rc = idxProcessOneTrigger(p, pIter, pzErr);
7237 static int idxCreateVtabSchema(sqlite3expert *p, char **pzErrmsg){
7238 int rc = idxRegisterVtab(p);
7239 sqlite3_stmt *pSchema = 0;
7241 /* For each table in the main db schema:
7243 ** 1) Add an entry to the p->pTable list, and
7244 ** 2) Create the equivalent virtual table in dbv.
7246 rc = idxPrepareStmt(p->db, &pSchema, pzErrmsg,
7247 "SELECT type, name, sql, 1 FROM sqlite_master "
7248 "WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' "
7250 "SELECT type, name, sql, 2 FROM sqlite_master "
7251 "WHERE type = 'trigger'"
7252 " AND tbl_name IN(SELECT name FROM sqlite_master WHERE type = 'view') "
7255 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSchema) ){
7256 const char *zType = (const char*)sqlite3_column_text(pSchema, 0);
7257 const char *zName = (const char*)sqlite3_column_text(pSchema, 1);
7258 const char *zSql = (const char*)sqlite3_column_text(pSchema, 2);
7260 if( zType[0]=='v' || zType[1]=='r' ){
7261 rc = sqlite3_exec(p->dbv, zSql, 0, 0, pzErrmsg);
7264 rc = idxGetTableInfo(p->db, zName, &pTab, pzErrmsg);
7265 if( rc==SQLITE_OK ){
7269 pTab->pNext = p->pTable;
7272 /* The statement the vtab will pass to sqlite3_declare_vtab() */
7273 zInner = idxAppendText(&rc, 0, "CREATE TABLE x(");
7274 for(i=0; i<pTab->nCol; i++){
7275 zInner = idxAppendText(&rc, zInner, "%s%Q COLLATE %s",
7276 (i==0 ? "" : ", "), pTab->aCol[i].zName, pTab->aCol[i].zColl
7279 zInner = idxAppendText(&rc, zInner, ")");
7281 /* The CVT statement to create the vtab */
7282 zOuter = idxAppendText(&rc, 0,
7283 "CREATE VIRTUAL TABLE %Q USING expert(%Q)", zName, zInner
7285 if( rc==SQLITE_OK ){
7286 rc = sqlite3_exec(p->dbv, zOuter, 0, 0, pzErrmsg);
7288 sqlite3_free(zInner);
7289 sqlite3_free(zOuter);
7293 idxFinalize(&rc, pSchema);
7297 struct IdxSampleCtx {
7299 double target; /* Target nRet/nRow value */
7300 double nRow; /* Number of rows seen */
7301 double nRet; /* Number of rows returned */
7304 static void idxSampleFunc(
7305 sqlite3_context *pCtx,
7307 sqlite3_value **argv
7309 struct IdxSampleCtx *p = (struct IdxSampleCtx*)sqlite3_user_data(pCtx);
7317 bRet = (p->nRet / p->nRow) <= p->target;
7320 sqlite3_randomness(2, (void*)&rnd);
7321 bRet = ((int)rnd % 100) <= p->iTarget;
7325 sqlite3_result_int(pCtx, bRet);
7327 p->nRet += (double)bRet;
7333 int eType; /* SQLITE_NULL, INTEGER, REAL, TEXT, BLOB */
7334 i64 iVal; /* SQLITE_INTEGER value */
7335 double rVal; /* SQLITE_FLOAT value */
7336 int nByte; /* Bytes of space allocated at z */
7337 int n; /* Size of buffer z */
7338 char *z; /* SQLITE_TEXT/BLOB value */
7343 ** Implementation of scalar function rem().
7345 static void idxRemFunc(
7346 sqlite3_context *pCtx,
7348 sqlite3_value **argv
7350 struct IdxRemCtx *p = (struct IdxRemCtx*)sqlite3_user_data(pCtx);
7351 struct IdxRemSlot *pSlot;
7355 iSlot = sqlite3_value_int(argv[0]);
7356 assert( iSlot<=p->nSlot );
7357 pSlot = &p->aSlot[iSlot];
7359 switch( pSlot->eType ){
7364 case SQLITE_INTEGER:
7365 sqlite3_result_int64(pCtx, pSlot->iVal);
7369 sqlite3_result_double(pCtx, pSlot->rVal);
7373 sqlite3_result_blob(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7377 sqlite3_result_text(pCtx, pSlot->z, pSlot->n, SQLITE_TRANSIENT);
7381 pSlot->eType = sqlite3_value_type(argv[1]);
7382 switch( pSlot->eType ){
7387 case SQLITE_INTEGER:
7388 pSlot->iVal = sqlite3_value_int64(argv[1]);
7392 pSlot->rVal = sqlite3_value_double(argv[1]);
7397 int nByte = sqlite3_value_bytes(argv[1]);
7398 if( nByte>pSlot->nByte ){
7399 char *zNew = (char*)sqlite3_realloc(pSlot->z, nByte*2);
7401 sqlite3_result_error_nomem(pCtx);
7404 pSlot->nByte = nByte*2;
7408 if( pSlot->eType==SQLITE_BLOB ){
7409 memcpy(pSlot->z, sqlite3_value_blob(argv[1]), nByte);
7411 memcpy(pSlot->z, sqlite3_value_text(argv[1]), nByte);
7418 static int idxLargestIndex(sqlite3 *db, int *pnMax, char **pzErr){
7421 "SELECT max(i.seqno) FROM "
7422 " sqlite_master AS s, "
7423 " pragma_index_list(s.name) AS l, "
7424 " pragma_index_info(l.name) AS i "
7425 "WHERE s.type = 'table'";
7426 sqlite3_stmt *pMax = 0;
7429 rc = idxPrepareStmt(db, &pMax, pzErr, zMax);
7430 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pMax) ){
7431 *pnMax = sqlite3_column_int(pMax, 0) + 1;
7433 idxFinalize(&rc, pMax);
7438 static int idxPopulateOneStat1(
7440 sqlite3_stmt *pIndexXInfo,
7441 sqlite3_stmt *pWriteStat,
7451 sqlite3_stmt *pQuery = 0;
7455 assert( p->iSample>0 );
7457 /* Formulate the query text */
7458 sqlite3_bind_text(pIndexXInfo, 1, zIdx, -1, SQLITE_STATIC);
7459 while( SQLITE_OK==rc && SQLITE_ROW==sqlite3_step(pIndexXInfo) ){
7460 const char *zComma = zCols==0 ? "" : ", ";
7461 const char *zName = (const char*)sqlite3_column_text(pIndexXInfo, 0);
7462 const char *zColl = (const char*)sqlite3_column_text(pIndexXInfo, 1);
7463 zCols = idxAppendText(&rc, zCols,
7464 "%sx.%Q IS rem(%d, x.%Q) COLLATE %s", zComma, zName, nCol, zName, zColl
7466 zOrder = idxAppendText(&rc, zOrder, "%s%d", zComma, ++nCol);
7468 sqlite3_reset(pIndexXInfo);
7469 if( rc==SQLITE_OK ){
7470 if( p->iSample==100 ){
7471 zQuery = sqlite3_mprintf(
7472 "SELECT %s FROM %Q x ORDER BY %s", zCols, zTab, zOrder
7475 zQuery = sqlite3_mprintf(
7476 "SELECT %s FROM temp."UNIQUE_TABLE_NAME" x ORDER BY %s", zCols, zOrder
7480 sqlite3_free(zCols);
7481 sqlite3_free(zOrder);
7483 /* Formulate the query text */
7484 if( rc==SQLITE_OK ){
7485 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
7486 rc = idxPrepareStmt(dbrem, &pQuery, pzErr, zQuery);
7488 sqlite3_free(zQuery);
7490 if( rc==SQLITE_OK ){
7491 aStat = (int*)idxMalloc(&rc, sizeof(int)*(nCol+1));
7493 if( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
7494 IdxHashEntry *pEntry;
7496 for(i=0; i<=nCol; i++) aStat[i] = 1;
7497 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pQuery) ){
7499 for(i=0; i<nCol; i++){
7500 if( sqlite3_column_int(pQuery, i)==0 ) break;
7502 for(/*no-op*/; i<nCol; i++){
7507 if( rc==SQLITE_OK ){
7509 zStat = sqlite3_mprintf("%d", s0);
7510 if( zStat==0 ) rc = SQLITE_NOMEM;
7511 for(i=1; rc==SQLITE_OK && i<=nCol; i++){
7512 zStat = idxAppendText(&rc, zStat, " %d", (s0+aStat[i]/2) / aStat[i]);
7516 if( rc==SQLITE_OK ){
7517 sqlite3_bind_text(pWriteStat, 1, zTab, -1, SQLITE_STATIC);
7518 sqlite3_bind_text(pWriteStat, 2, zIdx, -1, SQLITE_STATIC);
7519 sqlite3_bind_text(pWriteStat, 3, zStat, -1, SQLITE_STATIC);
7520 sqlite3_step(pWriteStat);
7521 rc = sqlite3_reset(pWriteStat);
7524 pEntry = idxHashFind(&p->hIdx, zIdx, STRLEN(zIdx));
7526 assert( pEntry->zVal2==0 );
7527 pEntry->zVal2 = zStat;
7529 sqlite3_free(zStat);
7532 sqlite3_free(aStat);
7533 idxFinalize(&rc, pQuery);
7538 static int idxBuildSampleTable(sqlite3expert *p, const char *zTab){
7542 rc = sqlite3_exec(p->dbv,"DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
7543 if( rc!=SQLITE_OK ) return rc;
7545 zSql = sqlite3_mprintf(
7546 "CREATE TABLE temp." UNIQUE_TABLE_NAME " AS SELECT * FROM %Q", zTab
7548 if( zSql==0 ) return SQLITE_NOMEM;
7549 rc = sqlite3_exec(p->dbv, zSql, 0, 0, 0);
7556 ** This function is called as part of sqlite3_expert_analyze(). Candidate
7557 ** indexes have already been created in database sqlite3expert.dbm, this
7558 ** function populates sqlite_stat1 table in the same database.
7560 ** The stat1 data is generated by querying the
7562 static int idxPopulateStat1(sqlite3expert *p, char **pzErr){
7565 struct IdxRemCtx *pCtx = 0;
7566 struct IdxSampleCtx samplectx;
7568 i64 iPrev = -100000;
7569 sqlite3_stmt *pAllIndex = 0;
7570 sqlite3_stmt *pIndexXInfo = 0;
7571 sqlite3_stmt *pWrite = 0;
7573 const char *zAllIndex =
7574 "SELECT s.rowid, s.name, l.name FROM "
7575 " sqlite_master AS s, "
7576 " pragma_index_list(s.name) AS l "
7577 "WHERE s.type = 'table'";
7578 const char *zIndexXInfo =
7579 "SELECT name, coll FROM pragma_index_xinfo(?) WHERE key";
7580 const char *zWrite = "INSERT INTO sqlite_stat1 VALUES(?, ?, ?)";
7582 /* If iSample==0, no sqlite_stat1 data is required. */
7583 if( p->iSample==0 ) return SQLITE_OK;
7585 rc = idxLargestIndex(p->dbm, &nMax, pzErr);
7586 if( nMax<=0 || rc!=SQLITE_OK ) return rc;
7588 rc = sqlite3_exec(p->dbm, "ANALYZE; PRAGMA writable_schema=1", 0, 0, 0);
7590 if( rc==SQLITE_OK ){
7591 int nByte = sizeof(struct IdxRemCtx) + (sizeof(struct IdxRemSlot) * nMax);
7592 pCtx = (struct IdxRemCtx*)idxMalloc(&rc, nByte);
7595 if( rc==SQLITE_OK ){
7596 sqlite3 *dbrem = (p->iSample==100 ? p->db : p->dbv);
7597 rc = sqlite3_create_function(
7598 dbrem, "rem", 2, SQLITE_UTF8, (void*)pCtx, idxRemFunc, 0, 0
7601 if( rc==SQLITE_OK ){
7602 rc = sqlite3_create_function(
7603 p->db, "sample", 0, SQLITE_UTF8, (void*)&samplectx, idxSampleFunc, 0, 0
7607 if( rc==SQLITE_OK ){
7608 pCtx->nSlot = nMax+1;
7609 rc = idxPrepareStmt(p->dbm, &pAllIndex, pzErr, zAllIndex);
7611 if( rc==SQLITE_OK ){
7612 rc = idxPrepareStmt(p->dbm, &pIndexXInfo, pzErr, zIndexXInfo);
7614 if( rc==SQLITE_OK ){
7615 rc = idxPrepareStmt(p->dbm, &pWrite, pzErr, zWrite);
7618 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pAllIndex) ){
7619 i64 iRowid = sqlite3_column_int64(pAllIndex, 0);
7620 const char *zTab = (const char*)sqlite3_column_text(pAllIndex, 1);
7621 const char *zIdx = (const char*)sqlite3_column_text(pAllIndex, 2);
7622 if( p->iSample<100 && iPrev!=iRowid ){
7623 samplectx.target = (double)p->iSample / 100.0;
7624 samplectx.iTarget = p->iSample;
7625 samplectx.nRow = 0.0;
7626 samplectx.nRet = 0.0;
7627 rc = idxBuildSampleTable(p, zTab);
7628 if( rc!=SQLITE_OK ) break;
7630 rc = idxPopulateOneStat1(p, pIndexXInfo, pWrite, zTab, zIdx, pzErr);
7633 if( rc==SQLITE_OK && p->iSample<100 ){
7634 rc = sqlite3_exec(p->dbv,
7635 "DROP TABLE IF EXISTS temp." UNIQUE_TABLE_NAME, 0,0,0
7639 idxFinalize(&rc, pAllIndex);
7640 idxFinalize(&rc, pIndexXInfo);
7641 idxFinalize(&rc, pWrite);
7643 for(i=0; i<pCtx->nSlot; i++){
7644 sqlite3_free(pCtx->aSlot[i].z);
7648 if( rc==SQLITE_OK ){
7649 rc = sqlite3_exec(p->dbm, "ANALYZE sqlite_master", 0, 0, 0);
7652 sqlite3_exec(p->db, "DROP TABLE IF EXISTS temp."UNIQUE_TABLE_NAME,0,0,0);
7657 ** Allocate a new sqlite3expert object.
7659 sqlite3expert *sqlite3_expert_new(sqlite3 *db, char **pzErrmsg){
7661 sqlite3expert *pNew;
7663 pNew = (sqlite3expert*)idxMalloc(&rc, sizeof(sqlite3expert));
7665 /* Open two in-memory databases to work with. The "vtab database" (dbv)
7666 ** will contain a virtual table corresponding to each real table in
7667 ** the user database schema, and a copy of each view. It is used to
7668 ** collect information regarding the WHERE, ORDER BY and other clauses
7669 ** of the user's query.
7671 if( rc==SQLITE_OK ){
7673 pNew->iSample = 100;
7674 rc = sqlite3_open(":memory:", &pNew->dbv);
7676 if( rc==SQLITE_OK ){
7677 rc = sqlite3_open(":memory:", &pNew->dbm);
7678 if( rc==SQLITE_OK ){
7679 sqlite3_db_config(pNew->dbm, SQLITE_DBCONFIG_TRIGGER_EQP, 1, (int*)0);
7684 /* Copy the entire schema of database [db] into [dbm]. */
7685 if( rc==SQLITE_OK ){
7687 rc = idxPrintfPrepareStmt(pNew->db, &pSql, pzErrmsg,
7688 "SELECT sql FROM sqlite_master WHERE name NOT LIKE 'sqlite_%%'"
7689 " AND sql NOT LIKE 'CREATE VIRTUAL %%'"
7691 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
7692 const char *zSql = (const char*)sqlite3_column_text(pSql, 0);
7693 rc = sqlite3_exec(pNew->dbm, zSql, 0, 0, pzErrmsg);
7695 idxFinalize(&rc, pSql);
7698 /* Create the vtab schema */
7699 if( rc==SQLITE_OK ){
7700 rc = idxCreateVtabSchema(pNew, pzErrmsg);
7703 /* Register the auth callback with dbv */
7704 if( rc==SQLITE_OK ){
7705 sqlite3_set_authorizer(pNew->dbv, idxAuthCallback, (void*)pNew);
7708 /* If an error has occurred, free the new object and reutrn NULL. Otherwise,
7709 ** return the new sqlite3expert handle. */
7710 if( rc!=SQLITE_OK ){
7711 sqlite3_expert_destroy(pNew);
7718 ** Configure an sqlite3expert object.
7720 int sqlite3_expert_config(sqlite3expert *p, int op, ...){
7725 case EXPERT_CONFIG_SAMPLE: {
7726 int iVal = va_arg(ap, int);
7727 if( iVal<0 ) iVal = 0;
7728 if( iVal>100 ) iVal = 100;
7733 rc = SQLITE_NOTFOUND;
7742 ** Add an SQL statement to the analysis.
7744 int sqlite3_expert_sql(
7745 sqlite3expert *p, /* From sqlite3_expert_new() */
7746 const char *zSql, /* SQL statement to add */
7747 char **pzErr /* OUT: Error message (if any) */
7749 IdxScan *pScanOrig = p->pScan;
7750 IdxStatement *pStmtOrig = p->pStatement;
7752 const char *zStmt = zSql;
7754 if( p->bRun ) return SQLITE_MISUSE;
7756 while( rc==SQLITE_OK && zStmt && zStmt[0] ){
7757 sqlite3_stmt *pStmt = 0;
7758 rc = sqlite3_prepare_v2(p->dbv, zStmt, -1, &pStmt, &zStmt);
7759 if( rc==SQLITE_OK ){
7762 const char *z = sqlite3_sql(pStmt);
7764 pNew = (IdxStatement*)idxMalloc(&rc, sizeof(IdxStatement) + n+1);
7765 if( rc==SQLITE_OK ){
7766 pNew->zSql = (char*)&pNew[1];
7767 memcpy(pNew->zSql, z, n+1);
7768 pNew->pNext = p->pStatement;
7769 if( p->pStatement ) pNew->iId = p->pStatement->iId+1;
7770 p->pStatement = pNew;
7772 sqlite3_finalize(pStmt);
7775 idxDatabaseError(p->dbv, pzErr);
7779 if( rc!=SQLITE_OK ){
7780 idxScanFree(p->pScan, pScanOrig);
7781 idxStatementFree(p->pStatement, pStmtOrig);
7782 p->pScan = pScanOrig;
7783 p->pStatement = pStmtOrig;
7789 int sqlite3_expert_analyze(sqlite3expert *p, char **pzErr){
7791 IdxHashEntry *pEntry;
7793 /* Do trigger processing to collect any extra IdxScan structures */
7794 rc = idxProcessTriggers(p, pzErr);
7796 /* Create candidate indexes within the in-memory database file */
7797 if( rc==SQLITE_OK ){
7798 rc = idxCreateCandidates(p);
7801 /* Generate the stat1 data */
7802 if( rc==SQLITE_OK ){
7803 rc = idxPopulateStat1(p, pzErr);
7806 /* Formulate the EXPERT_REPORT_CANDIDATES text */
7807 for(pEntry=p->hIdx.pFirst; pEntry; pEntry=pEntry->pNext){
7808 p->zCandidates = idxAppendText(&rc, p->zCandidates,
7809 "%s;%s%s\n", pEntry->zVal,
7810 pEntry->zVal2 ? " -- stat1: " : "", pEntry->zVal2
7814 /* Figure out which of the candidate indexes are preferred by the query
7815 ** planner and report the results to the user. */
7816 if( rc==SQLITE_OK ){
7817 rc = idxFindIndexes(p, pzErr);
7820 if( rc==SQLITE_OK ){
7827 ** Return the total number of statements that have been added to this
7828 ** sqlite3expert using sqlite3_expert_sql().
7830 int sqlite3_expert_count(sqlite3expert *p){
7832 if( p->pStatement ) nRet = p->pStatement->iId+1;
7837 ** Return a component of the report.
7839 const char *sqlite3_expert_report(sqlite3expert *p, int iStmt, int eReport){
7840 const char *zRet = 0;
7841 IdxStatement *pStmt;
7843 if( p->bRun==0 ) return 0;
7844 for(pStmt=p->pStatement; pStmt && pStmt->iId!=iStmt; pStmt=pStmt->pNext);
7846 case EXPERT_REPORT_SQL:
7847 if( pStmt ) zRet = pStmt->zSql;
7849 case EXPERT_REPORT_INDEXES:
7850 if( pStmt ) zRet = pStmt->zIdx;
7852 case EXPERT_REPORT_PLAN:
7853 if( pStmt ) zRet = pStmt->zEQP;
7855 case EXPERT_REPORT_CANDIDATES:
7856 zRet = p->zCandidates;
7863 ** Free an sqlite3expert object.
7865 void sqlite3_expert_destroy(sqlite3expert *p){
7867 sqlite3_close(p->dbm);
7868 sqlite3_close(p->dbv);
7869 idxScanFree(p->pScan, 0);
7870 idxStatementFree(p->pStatement, 0);
7871 idxTableFree(p->pTable);
7872 idxWriteFree(p->pWrite);
7873 idxHashClear(&p->hIdx);
7874 sqlite3_free(p->zCandidates);
7879 #endif /* ifndef SQLITE_OMIT_VIRTUAL_TABLE */
7881 /************************* End ../ext/expert/sqlite3expert.c ********************/
7883 #if defined(SQLITE_ENABLE_SESSION)
7885 ** State information for a single open session
7887 typedef struct OpenSession OpenSession;
7888 struct OpenSession {
7889 char *zName; /* Symbolic name for this session */
7890 int nFilter; /* Number of xFilter rejection GLOB patterns */
7891 char **azFilter; /* Array of xFilter rejection GLOB patterns */
7892 sqlite3_session *p; /* The open session */
7897 ** Shell output mode information from before ".explain on",
7898 ** saved so that it can be restored by ".explain off"
7900 typedef struct SavedModeInfo SavedModeInfo;
7901 struct SavedModeInfo {
7902 int valid; /* Is there legit data in here? */
7903 int mode; /* Mode prior to ".explain on" */
7904 int showHeader; /* The ".header" setting prior to ".explain on" */
7905 int colWidth[100]; /* Column widths prior to ".explain on" */
7908 typedef struct ExpertInfo ExpertInfo;
7910 sqlite3expert *pExpert;
7915 ** State information about the database connection is contained in an
7916 ** instance of the following structure.
7918 typedef struct ShellState ShellState;
7920 sqlite3 *db; /* The database */
7921 u8 autoExplain; /* Automatically turn on .explain mode */
7922 u8 autoEQP; /* Run EXPLAIN QUERY PLAN prior to seach SQL stmt */
7923 u8 statsOn; /* True to display memory stats before each finalize */
7924 u8 scanstatsOn; /* True to display scan stats before each finalize */
7925 u8 openMode; /* SHELL_OPEN_NORMAL, _APPENDVFS, or _ZIPFILE */
7926 u8 doXdgOpen; /* Invoke start/open/xdg-open in output_reset() */
7927 int outCount; /* Revert to stdout when reaching zero */
7928 int cnt; /* Number of records displayed so far */
7929 FILE *out; /* Write results here */
7930 FILE *traceOut; /* Output for sqlite3_trace() */
7931 int nErr; /* Number of errors seen */
7932 int mode; /* An output mode setting */
7933 int modePrior; /* Saved mode */
7934 int cMode; /* temporary output mode for the current query */
7935 int normalMode; /* Output mode before ".explain on" */
7936 int writableSchema; /* True if PRAGMA writable_schema=ON */
7937 int showHeader; /* True to show column names in List or Column mode */
7938 int nCheck; /* Number of ".check" commands run */
7939 unsigned shellFlgs; /* Various flags */
7940 char *zDestTable; /* Name of destination table when MODE_Insert */
7941 char *zTempFile; /* Temporary file that might need deleting */
7942 char zTestcase[30]; /* Name of current test case */
7943 char colSeparator[20]; /* Column separator character for several modes */
7944 char rowSeparator[20]; /* Row separator character for MODE_Ascii */
7945 char colSepPrior[20]; /* Saved column separator */
7946 char rowSepPrior[20]; /* Saved row separator */
7947 int colWidth[100]; /* Requested width of each column when in column mode*/
7948 int actualWidth[100]; /* Actual width of each column */
7949 char nullValue[20]; /* The text to print when a NULL comes back from
7951 char outfile[FILENAME_MAX]; /* Filename for *out */
7952 const char *zDbFilename; /* name of the database file */
7953 char *zFreeOnClose; /* Filename to free when closing */
7954 const char *zVfs; /* Name of VFS to use */
7955 sqlite3_stmt *pStmt; /* Current statement if any. */
7956 FILE *pLog; /* Write log output here */
7957 int *aiIndent; /* Array of indents used in MODE_Explain */
7958 int nIndent; /* Size of array aiIndent[] */
7959 int iIndent; /* Index of current op in aiIndent[] */
7960 #if defined(SQLITE_ENABLE_SESSION)
7961 int nSession; /* Number of active sessions */
7962 OpenSession aSession[4]; /* Array of sessions. [0] is in focus. */
7964 ExpertInfo expert; /* Valid if previous command was ".expert OPT..." */
7968 /* Allowed values for ShellState.autoEQP
7970 #define AUTOEQP_off 0
7971 #define AUTOEQP_on 1
7972 #define AUTOEQP_trigger 2
7973 #define AUTOEQP_full 3
7975 /* Allowed values for ShellState.openMode
7977 #define SHELL_OPEN_UNSPEC 0 /* No open-mode specified */
7978 #define SHELL_OPEN_NORMAL 1 /* Normal database file */
7979 #define SHELL_OPEN_APPENDVFS 2 /* Use appendvfs */
7980 #define SHELL_OPEN_ZIPFILE 3 /* Use the zipfile virtual table */
7983 ** These are the allowed shellFlgs values
7985 #define SHFLG_Pagecache 0x00000001 /* The --pagecache option is used */
7986 #define SHFLG_Lookaside 0x00000002 /* Lookaside memory is used */
7987 #define SHFLG_Backslash 0x00000004 /* The --backslash option is used */
7988 #define SHFLG_PreserveRowid 0x00000008 /* .dump preserves rowid values */
7989 #define SHFLG_Newlines 0x00000010 /* .dump --newline flag */
7990 #define SHFLG_CountChanges 0x00000020 /* .changes setting */
7991 #define SHFLG_Echo 0x00000040 /* .echo or --echo setting */
7994 ** Macros for testing and setting shellFlgs
7996 #define ShellHasFlag(P,X) (((P)->shellFlgs & (X))!=0)
7997 #define ShellSetFlag(P,X) ((P)->shellFlgs|=(X))
7998 #define ShellClearFlag(P,X) ((P)->shellFlgs&=(~(X)))
8001 ** These are the allowed modes.
8003 #define MODE_Line 0 /* One column per line. Blank line between records */
8004 #define MODE_Column 1 /* One record per line in neat columns */
8005 #define MODE_List 2 /* One record per line with a separator */
8006 #define MODE_Semi 3 /* Same as MODE_List but append ";" to each line */
8007 #define MODE_Html 4 /* Generate an XHTML table */
8008 #define MODE_Insert 5 /* Generate SQL "insert" statements */
8009 #define MODE_Quote 6 /* Quote values as for SQL */
8010 #define MODE_Tcl 7 /* Generate ANSI-C or TCL quoted elements */
8011 #define MODE_Csv 8 /* Quote strings, numbers are plain */
8012 #define MODE_Explain 9 /* Like MODE_Column, but do not truncate data */
8013 #define MODE_Ascii 10 /* Use ASCII unit and record separators (0x1F/0x1E) */
8014 #define MODE_Pretty 11 /* Pretty-print schemas */
8016 static const char *modeDescr[] = {
8032 ** These are the column/row/line separators used by the various
8033 ** import/export modes.
8035 #define SEP_Column "|"
8036 #define SEP_Row "\n"
8037 #define SEP_Tab "\t"
8038 #define SEP_Space " "
8039 #define SEP_Comma ","
8040 #define SEP_CrLf "\r\n"
8041 #define SEP_Unit "\x1F"
8042 #define SEP_Record "\x1E"
8045 ** A callback for the sqlite3_log() interface.
8047 static void shellLog(void *pArg, int iErrCode, const char *zMsg){
8048 ShellState *p = (ShellState*)pArg;
8049 if( p->pLog==0 ) return;
8050 utf8_printf(p->pLog, "(%d) %s\n", iErrCode, zMsg);
8055 ** SQL function: shell_putsnl(X)
8057 ** Write the text X to the screen (or whatever output is being directed)
8058 ** adding a newline at the end, and then return X.
8060 static void shellPutsFunc(
8061 sqlite3_context *pCtx,
8063 sqlite3_value **apVal
8065 ShellState *p = (ShellState*)sqlite3_user_data(pCtx);
8067 utf8_printf(p->out, "%s\n", sqlite3_value_text(apVal[0]));
8068 sqlite3_result_value(pCtx, apVal[0]);
8072 ** SQL function: edit(VALUE)
8073 ** edit(VALUE,EDITOR)
8077 ** (1) Write VALUE into a temporary file.
8078 ** (2) Run program EDITOR on that temporary file.
8079 ** (3) Read the temporary file back and return its content as the result.
8080 ** (4) Delete the temporary file
8082 ** If the EDITOR argument is omitted, use the value in the VISUAL
8083 ** environment variable. If still there is no EDITOR, through an error.
8085 ** Also throw an error if the EDITOR program returns a non-zero exit code.
8087 static void editFunc(
8088 sqlite3_context *context,
8090 sqlite3_value **argv
8092 const char *zEditor;
8093 char *zTempFile = 0;
8101 unsigned char *p = 0;
8104 zEditor = (const char*)sqlite3_value_text(argv[1]);
8106 zEditor = getenv("VISUAL");
8109 sqlite3_result_error(context, "no editor for edit()", -1);
8112 if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
8113 sqlite3_result_error(context, "NULL input to edit()", -1);
8116 db = sqlite3_context_db_handle(context);
8118 sqlite3_file_control(db, 0, SQLITE_FCNTL_TEMPFILENAME, &zTempFile);
8120 sqlite3_uint64 r = 0;
8121 sqlite3_randomness(sizeof(r), &r);
8122 zTempFile = sqlite3_mprintf("temp%llx", r);
8124 sqlite3_result_error_nomem(context);
8128 bBin = sqlite3_value_type(argv[0])==SQLITE_BLOB;
8129 f = fopen(zTempFile, bBin ? "wb" : "w");
8131 sqlite3_result_error(context, "edit() cannot open temp file", -1);
8134 sz = sqlite3_value_bytes(argv[0]);
8136 x = fwrite(sqlite3_value_blob(argv[0]), 1, sz, f);
8138 x = fwrite(sqlite3_value_text(argv[0]), 1, sz, f);
8143 sqlite3_result_error(context, "edit() could not write the whole file", -1);
8146 zCmd = sqlite3_mprintf("%s \"%s\"", zEditor, zTempFile);
8148 sqlite3_result_error_nomem(context);
8154 sqlite3_result_error(context, "EDITOR returned non-zero", -1);
8157 f = fopen(zTempFile, bBin ? "rb" : "r");
8159 sqlite3_result_error(context,
8160 "edit() cannot reopen temp file after edit", -1);
8163 fseek(f, 0, SEEK_END);
8166 p = sqlite3_malloc64( sz+(bBin==0) );
8168 sqlite3_result_error_nomem(context);
8172 x = fread(p, 1, sz, f);
8174 x = fread(p, 1, sz, f);
8180 sqlite3_result_error(context, "could not read back the whole file", -1);
8184 sqlite3_result_blob(context, p, sz, sqlite3_free);
8186 sqlite3_result_text(context, (const char*)p, sz, sqlite3_free);
8193 sqlite3_free(zTempFile);
8198 ** Save or restore the current output mode
8200 static void outputModePush(ShellState *p){
8201 p->modePrior = p->mode;
8202 memcpy(p->colSepPrior, p->colSeparator, sizeof(p->colSeparator));
8203 memcpy(p->rowSepPrior, p->rowSeparator, sizeof(p->rowSeparator));
8205 static void outputModePop(ShellState *p){
8206 p->mode = p->modePrior;
8207 memcpy(p->colSeparator, p->colSepPrior, sizeof(p->colSeparator));
8208 memcpy(p->rowSeparator, p->rowSepPrior, sizeof(p->rowSeparator));
8212 ** Output the given string as a hex-encoded blob (eg. X'1234' )
8214 static void output_hex_blob(FILE *out, const void *pBlob, int nBlob){
8216 char *zBlob = (char *)pBlob;
8217 raw_printf(out,"X'");
8218 for(i=0; i<nBlob; i++){ raw_printf(out,"%02x",zBlob[i]&0xff); }
8219 raw_printf(out,"'");
8223 ** Find a string that is not found anywhere in z[]. Return a pointer
8226 ** Try to use zA and zB first. If both of those are already found in z[]
8227 ** then make up some string and store it in the buffer zBuf.
8229 static const char *unused_string(
8230 const char *z, /* Result must not appear anywhere in z */
8231 const char *zA, const char *zB, /* Try these first */
8232 char *zBuf /* Space to store a generated string */
8235 if( strstr(z, zA)==0 ) return zA;
8236 if( strstr(z, zB)==0 ) return zB;
8238 sqlite3_snprintf(20,zBuf,"(%s%u)", zA, i++);
8239 }while( strstr(z,zBuf)!=0 );
8244 ** Output the given string as a quoted string using SQL quoting conventions.
8246 ** See also: output_quoted_escaped_string()
8248 static void output_quoted_string(FILE *out, const char *z){
8251 setBinaryMode(out, 1);
8252 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8254 utf8_printf(out,"'%s'",z);
8256 raw_printf(out, "'");
8258 for(i=0; (c = z[i])!=0 && c!='\''; i++){}
8261 utf8_printf(out, "%.*s", i, z);
8265 raw_printf(out, "'");
8273 raw_printf(out, "'");
8275 setTextMode(out, 1);
8279 ** Output the given string as a quoted string using SQL quoting conventions.
8280 ** Additionallly , escape the "\n" and "\r" characters so that they do not
8281 ** get corrupted by end-of-line translation facilities in some operating
8284 ** This is like output_quoted_string() but with the addition of the \r\n
8285 ** escape mechanism.
8287 static void output_quoted_escaped_string(FILE *out, const char *z){
8290 setBinaryMode(out, 1);
8291 for(i=0; (c = z[i])!=0 && c!='\'' && c!='\n' && c!='\r'; i++){}
8293 utf8_printf(out,"'%s'",z);
8295 const char *zNL = 0;
8296 const char *zCR = 0;
8299 char zBuf1[20], zBuf2[20];
8300 for(i=0; z[i]; i++){
8301 if( z[i]=='\n' ) nNL++;
8302 if( z[i]=='\r' ) nCR++;
8305 raw_printf(out, "replace(");
8306 zNL = unused_string(z, "\\n", "\\012", zBuf1);
8309 raw_printf(out, "replace(");
8310 zCR = unused_string(z, "\\r", "\\015", zBuf2);
8312 raw_printf(out, "'");
8314 for(i=0; (c = z[i])!=0 && c!='\n' && c!='\r' && c!='\''; i++){}
8317 utf8_printf(out, "%.*s", i, z);
8321 raw_printf(out, "'");
8329 raw_printf(out, "%s", zNL);
8332 raw_printf(out, "%s", zCR);
8334 raw_printf(out, "'");
8336 raw_printf(out, ",'%s',char(13))", zCR);
8339 raw_printf(out, ",'%s',char(10))", zNL);
8342 setTextMode(out, 1);
8346 ** Output the given string as a quoted according to C or TCL quoting rules.
8348 static void output_c_string(FILE *out, const char *z){
8351 while( (c = *(z++))!=0 ){
8358 }else if( c=='\t' ){
8361 }else if( c=='\n' ){
8364 }else if( c=='\r' ){
8367 }else if( !isprint(c&0xff) ){
8368 raw_printf(out, "\\%03o", c&0xff);
8377 ** Output the given string with characters that are special to
8380 static void output_html_string(FILE *out, const char *z){
8392 utf8_printf(out,"%.*s",i,z);
8395 raw_printf(out,"<");
8396 }else if( z[i]=='&' ){
8397 raw_printf(out,"&");
8398 }else if( z[i]=='>' ){
8399 raw_printf(out,">");
8400 }else if( z[i]=='\"' ){
8401 raw_printf(out,""");
8402 }else if( z[i]=='\'' ){
8403 raw_printf(out,"'");
8412 ** If a field contains any character identified by a 1 in the following
8413 ** array, then the string must be quoted for CSV.
8415 static const char needCsvQuote[] = {
8416 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8417 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8418 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
8419 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8420 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8421 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8422 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8423 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
8424 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8425 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8426 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8427 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8428 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8429 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8430 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8431 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
8435 ** Output a single term of CSV. Actually, p->colSeparator is used for
8436 ** the separator, which may or may not be a comma. p->nullValue is
8437 ** the null value. Strings are quoted if necessary. The separator
8438 ** is only issued if bSep is true.
8440 static void output_csv(ShellState *p, const char *z, int bSep){
8443 utf8_printf(out,"%s",p->nullValue);
8446 int nSep = strlen30(p->colSeparator);
8447 for(i=0; z[i]; i++){
8448 if( needCsvQuote[((unsigned char*)z)[i]]
8449 || (z[i]==p->colSeparator[0] &&
8450 (nSep==1 || memcmp(z, p->colSeparator, nSep)==0)) ){
8456 char *zQuoted = sqlite3_mprintf("\"%w\"", z);
8457 utf8_printf(out, "%s", zQuoted);
8458 sqlite3_free(zQuoted);
8460 utf8_printf(out, "%s", z);
8464 utf8_printf(p->out, "%s", p->colSeparator);
8469 ** This routine runs when the user presses Ctrl-C
8471 static void interrupt_handler(int NotUsed){
8472 UNUSED_PARAMETER(NotUsed);
8474 if( seenInterrupt>2 ) exit(1);
8475 if( globalDb ) sqlite3_interrupt(globalDb);
8478 #if (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
8480 ** This routine runs for console events (e.g. Ctrl-C) on Win32
8482 static BOOL WINAPI ConsoleCtrlHandler(
8483 DWORD dwCtrlType /* One of the CTRL_*_EVENT constants */
8485 if( dwCtrlType==CTRL_C_EVENT ){
8486 interrupt_handler(0);
8493 #ifndef SQLITE_OMIT_AUTHORIZATION
8495 ** When the ".auth ON" is set, the following authorizer callback is
8496 ** invoked. It always returns SQLITE_OK.
8498 static int shellAuth(
8506 ShellState *p = (ShellState*)pClientData;
8507 static const char *azAction[] = { 0,
8508 "CREATE_INDEX", "CREATE_TABLE", "CREATE_TEMP_INDEX",
8509 "CREATE_TEMP_TABLE", "CREATE_TEMP_TRIGGER", "CREATE_TEMP_VIEW",
8510 "CREATE_TRIGGER", "CREATE_VIEW", "DELETE",
8511 "DROP_INDEX", "DROP_TABLE", "DROP_TEMP_INDEX",
8512 "DROP_TEMP_TABLE", "DROP_TEMP_TRIGGER", "DROP_TEMP_VIEW",
8513 "DROP_TRIGGER", "DROP_VIEW", "INSERT",
8514 "PRAGMA", "READ", "SELECT",
8515 "TRANSACTION", "UPDATE", "ATTACH",
8516 "DETACH", "ALTER_TABLE", "REINDEX",
8517 "ANALYZE", "CREATE_VTABLE", "DROP_VTABLE",
8518 "FUNCTION", "SAVEPOINT", "RECURSIVE"
8526 utf8_printf(p->out, "authorizer: %s", azAction[op]);
8528 raw_printf(p->out, " ");
8530 output_c_string(p->out, az[i]);
8532 raw_printf(p->out, "NULL");
8535 raw_printf(p->out, "\n");
8541 ** Print a schema statement. Part of MODE_Semi and MODE_Pretty output.
8543 ** This routine converts some CREATE TABLE statements for shadow tables
8544 ** in FTS3/4/5 into CREATE TABLE IF NOT EXISTS statements.
8546 static void printSchemaLine(FILE *out, const char *z, const char *zTail){
8547 if( sqlite3_strglob("CREATE TABLE ['\"]*", z)==0 ){
8548 utf8_printf(out, "CREATE TABLE IF NOT EXISTS %s%s", z+13, zTail);
8550 utf8_printf(out, "%s%s", z, zTail);
8553 static void printSchemaLineN(FILE *out, char *z, int n, const char *zTail){
8556 printSchemaLine(out, z, zTail);
8561 ** Return true if string z[] has nothing but whitespace and comments to the
8562 ** end of the first line.
8564 static int wsToEol(const char *z){
8566 for(i=0; z[i]; i++){
8567 if( z[i]=='\n' ) return 1;
8568 if( IsSpace(z[i]) ) continue;
8569 if( z[i]=='-' && z[i+1]=='-' ) return 1;
8577 ** This is the callback routine that the shell
8578 ** invokes for each row of a query result.
8580 static int shell_callback(
8582 int nArg, /* Number of result columns */
8583 char **azArg, /* Text of each result column */
8584 char **azCol, /* Column names */
8585 int *aiType /* Column types */
8588 ShellState *p = (ShellState*)pArg;
8590 if( azArg==0 ) return 0;
8594 if( azArg==0 ) break;
8595 for(i=0; i<nArg; i++){
8596 int len = strlen30(azCol[i] ? azCol[i] : "");
8597 if( len>w ) w = len;
8599 if( p->cnt++>0 ) utf8_printf(p->out, "%s", p->rowSeparator);
8600 for(i=0; i<nArg; i++){
8601 utf8_printf(p->out,"%*s = %s%s", w, azCol[i],
8602 azArg[i] ? azArg[i] : p->nullValue, p->rowSeparator);
8608 static const int aExplainWidths[] = {4, 13, 4, 4, 4, 13, 2, 13};
8609 const int *colWidth;
8612 if( p->cMode==MODE_Column ){
8613 colWidth = p->colWidth;
8614 showHdr = p->showHeader;
8615 rowSep = p->rowSeparator;
8617 colWidth = aExplainWidths;
8622 for(i=0; i<nArg; i++){
8624 if( i<ArraySize(p->colWidth) ){
8630 w = strlenChar(azCol[i] ? azCol[i] : "");
8632 n = strlenChar(azArg && azArg[i] ? azArg[i] : p->nullValue);
8635 if( i<ArraySize(p->actualWidth) ){
8636 p->actualWidth[i] = w;
8639 utf8_width_print(p->out, w, azCol[i]);
8640 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
8644 for(i=0; i<nArg; i++){
8646 if( i<ArraySize(p->actualWidth) ){
8647 w = p->actualWidth[i];
8652 utf8_printf(p->out,"%-*.*s%s",w,w,
8653 "----------------------------------------------------------"
8654 "----------------------------------------------------------",
8655 i==nArg-1 ? rowSep : " ");
8659 if( azArg==0 ) break;
8660 for(i=0; i<nArg; i++){
8662 if( i<ArraySize(p->actualWidth) ){
8663 w = p->actualWidth[i];
8667 if( p->cMode==MODE_Explain && azArg[i] && strlenChar(azArg[i])>w ){
8668 w = strlenChar(azArg[i]);
8670 if( i==1 && p->aiIndent && p->pStmt ){
8671 if( p->iIndent<p->nIndent ){
8672 utf8_printf(p->out, "%*.s", p->aiIndent[p->iIndent], "");
8676 utf8_width_print(p->out, w, azArg[i] ? azArg[i] : p->nullValue);
8677 utf8_printf(p->out, "%s", i==nArg-1 ? rowSep : " ");
8681 case MODE_Semi: { /* .schema and .fullschema output */
8682 printSchemaLine(p->out, azArg[0], ";\n");
8685 case MODE_Pretty: { /* .schema and .fullschema with --indent */
8693 if( azArg[0]==0 ) break;
8694 if( sqlite3_strlike("CREATE VIEW%", azArg[0], 0)==0
8695 || sqlite3_strlike("CREATE TRIG%", azArg[0], 0)==0
8697 utf8_printf(p->out, "%s;\n", azArg[0]);
8700 z = sqlite3_mprintf("%s", azArg[0]);
8702 for(i=0; IsSpace(z[i]); i++){}
8703 for(; (c = z[i])!=0; i++){
8705 if( z[j-1]=='\r' ) z[j-1] = '\n';
8706 if( IsSpace(z[j-1]) || z[j-1]=='(' ) continue;
8707 }else if( (c=='(' || c==')') && j>0 && IsSpace(z[j-1]) ){
8712 while( j>0 && IsSpace(z[j-1]) ){ j--; }
8714 if( strlen30(z)>=79 ){
8715 for(i=j=0; (c = z[i])!=0; i++){ /* Copy changes from z[i] back to z[j] */
8718 }else if( c=='"' || c=='\'' || c=='`' ){
8722 }else if( c=='-' && z[i+1]=='-' ){
8728 if( nLine>0 && nParen==0 && j>0 ){
8729 printSchemaLineN(p->out, z, j, "\n");
8734 if( nParen==1 && cEnd==0
8735 && (c=='(' || c=='\n' || (c==',' && !wsToEol(z+i+1)))
8738 printSchemaLineN(p->out, z, j, "\n ");
8741 while( IsSpace(z[i+1]) ){ i++; }
8746 printSchemaLine(p->out, z, ";\n");
8751 if( p->cnt++==0 && p->showHeader ){
8752 for(i=0; i<nArg; i++){
8753 utf8_printf(p->out,"%s%s",azCol[i],
8754 i==nArg-1 ? p->rowSeparator : p->colSeparator);
8757 if( azArg==0 ) break;
8758 for(i=0; i<nArg; i++){
8760 if( z==0 ) z = p->nullValue;
8761 utf8_printf(p->out, "%s", z);
8763 utf8_printf(p->out, "%s", p->colSeparator);
8765 utf8_printf(p->out, "%s", p->rowSeparator);
8771 if( p->cnt++==0 && p->showHeader ){
8772 raw_printf(p->out,"<TR>");
8773 for(i=0; i<nArg; i++){
8774 raw_printf(p->out,"<TH>");
8775 output_html_string(p->out, azCol[i]);
8776 raw_printf(p->out,"</TH>\n");
8778 raw_printf(p->out,"</TR>\n");
8780 if( azArg==0 ) break;
8781 raw_printf(p->out,"<TR>");
8782 for(i=0; i<nArg; i++){
8783 raw_printf(p->out,"<TD>");
8784 output_html_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
8785 raw_printf(p->out,"</TD>\n");
8787 raw_printf(p->out,"</TR>\n");
8791 if( p->cnt++==0 && p->showHeader ){
8792 for(i=0; i<nArg; i++){
8793 output_c_string(p->out,azCol[i] ? azCol[i] : "");
8794 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
8796 utf8_printf(p->out, "%s", p->rowSeparator);
8798 if( azArg==0 ) break;
8799 for(i=0; i<nArg; i++){
8800 output_c_string(p->out, azArg[i] ? azArg[i] : p->nullValue);
8801 if(i<nArg-1) utf8_printf(p->out, "%s", p->colSeparator);
8803 utf8_printf(p->out, "%s", p->rowSeparator);
8807 setBinaryMode(p->out, 1);
8808 if( p->cnt++==0 && p->showHeader ){
8809 for(i=0; i<nArg; i++){
8810 output_csv(p, azCol[i] ? azCol[i] : "", i<nArg-1);
8812 utf8_printf(p->out, "%s", p->rowSeparator);
8815 for(i=0; i<nArg; i++){
8816 output_csv(p, azArg[i], i<nArg-1);
8818 utf8_printf(p->out, "%s", p->rowSeparator);
8820 setTextMode(p->out, 1);
8824 if( azArg==0 ) break;
8825 utf8_printf(p->out,"INSERT INTO %s",p->zDestTable);
8826 if( p->showHeader ){
8827 raw_printf(p->out,"(");
8828 for(i=0; i<nArg; i++){
8829 if( i>0 ) raw_printf(p->out, ",");
8830 if( quoteChar(azCol[i]) ){
8831 char *z = sqlite3_mprintf("\"%w\"", azCol[i]);
8832 utf8_printf(p->out, "%s", z);
8835 raw_printf(p->out, "%s", azCol[i]);
8838 raw_printf(p->out,")");
8841 for(i=0; i<nArg; i++){
8842 raw_printf(p->out, i>0 ? "," : " VALUES(");
8843 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
8844 utf8_printf(p->out,"NULL");
8845 }else if( aiType && aiType[i]==SQLITE_TEXT ){
8846 if( ShellHasFlag(p, SHFLG_Newlines) ){
8847 output_quoted_string(p->out, azArg[i]);
8849 output_quoted_escaped_string(p->out, azArg[i]);
8851 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
8852 utf8_printf(p->out,"%s", azArg[i]);
8853 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
8855 double r = sqlite3_column_double(p->pStmt, i);
8856 sqlite3_snprintf(50,z,"%!.20g", r);
8857 raw_printf(p->out, "%s", z);
8858 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
8859 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
8860 int nBlob = sqlite3_column_bytes(p->pStmt, i);
8861 output_hex_blob(p->out, pBlob, nBlob);
8862 }else if( isNumber(azArg[i], 0) ){
8863 utf8_printf(p->out,"%s", azArg[i]);
8864 }else if( ShellHasFlag(p, SHFLG_Newlines) ){
8865 output_quoted_string(p->out, azArg[i]);
8867 output_quoted_escaped_string(p->out, azArg[i]);
8870 raw_printf(p->out,");\n");
8874 if( azArg==0 ) break;
8875 if( p->cnt==0 && p->showHeader ){
8876 for(i=0; i<nArg; i++){
8877 if( i>0 ) raw_printf(p->out, ",");
8878 output_quoted_string(p->out, azCol[i]);
8880 raw_printf(p->out,"\n");
8883 for(i=0; i<nArg; i++){
8884 if( i>0 ) raw_printf(p->out, ",");
8885 if( (azArg[i]==0) || (aiType && aiType[i]==SQLITE_NULL) ){
8886 utf8_printf(p->out,"NULL");
8887 }else if( aiType && aiType[i]==SQLITE_TEXT ){
8888 output_quoted_string(p->out, azArg[i]);
8889 }else if( aiType && aiType[i]==SQLITE_INTEGER ){
8890 utf8_printf(p->out,"%s", azArg[i]);
8891 }else if( aiType && aiType[i]==SQLITE_FLOAT ){
8893 double r = sqlite3_column_double(p->pStmt, i);
8894 sqlite3_snprintf(50,z,"%!.20g", r);
8895 raw_printf(p->out, "%s", z);
8896 }else if( aiType && aiType[i]==SQLITE_BLOB && p->pStmt ){
8897 const void *pBlob = sqlite3_column_blob(p->pStmt, i);
8898 int nBlob = sqlite3_column_bytes(p->pStmt, i);
8899 output_hex_blob(p->out, pBlob, nBlob);
8900 }else if( isNumber(azArg[i], 0) ){
8901 utf8_printf(p->out,"%s", azArg[i]);
8903 output_quoted_string(p->out, azArg[i]);
8906 raw_printf(p->out,"\n");
8910 if( p->cnt++==0 && p->showHeader ){
8911 for(i=0; i<nArg; i++){
8912 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
8913 utf8_printf(p->out,"%s",azCol[i] ? azCol[i] : "");
8915 utf8_printf(p->out, "%s", p->rowSeparator);
8917 if( azArg==0 ) break;
8918 for(i=0; i<nArg; i++){
8919 if( i>0 ) utf8_printf(p->out, "%s", p->colSeparator);
8920 utf8_printf(p->out,"%s",azArg[i] ? azArg[i] : p->nullValue);
8922 utf8_printf(p->out, "%s", p->rowSeparator);
8930 ** This is the callback routine that the SQLite library
8931 ** invokes for each row of a query result.
8933 static int callback(void *pArg, int nArg, char **azArg, char **azCol){
8934 /* since we don't have type info, call the shell_callback with a NULL value */
8935 return shell_callback(pArg, nArg, azArg, azCol, NULL);
8939 ** This is the callback routine from sqlite3_exec() that appends all
8940 ** output onto the end of a ShellText object.
8942 static int captureOutputCallback(void *pArg, int nArg, char **azArg, char **az){
8943 ShellText *p = (ShellText*)pArg;
8945 UNUSED_PARAMETER(az);
8946 if( azArg==0 ) return 0;
8947 if( p->n ) appendText(p, "|", 0);
8948 for(i=0; i<nArg; i++){
8949 if( i ) appendText(p, ",", 0);
8950 if( azArg[i] ) appendText(p, azArg[i], 0);
8956 ** Generate an appropriate SELFTEST table in the main database.
8958 static void createSelftestTable(ShellState *p){
8961 "SAVEPOINT selftest_init;\n"
8962 "CREATE TABLE IF NOT EXISTS selftest(\n"
8963 " tno INTEGER PRIMARY KEY,\n" /* Test number */
8964 " op TEXT,\n" /* Operator: memo run */
8965 " cmd TEXT,\n" /* Command text */
8966 " ans TEXT\n" /* Desired answer */
8968 "CREATE TEMP TABLE [_shell$self](op,cmd,ans);\n"
8969 "INSERT INTO [_shell$self](rowid,op,cmd)\n"
8970 " VALUES(coalesce((SELECT (max(tno)+100)/10 FROM selftest),10),\n"
8971 " 'memo','Tests generated by --init');\n"
8972 "INSERT INTO [_shell$self]\n"
8974 " 'SELECT hex(sha3_query(''SELECT type,name,tbl_name,sql "
8975 "FROM sqlite_master ORDER BY 2'',224))',\n"
8976 " hex(sha3_query('SELECT type,name,tbl_name,sql "
8977 "FROM sqlite_master ORDER BY 2',224));\n"
8978 "INSERT INTO [_shell$self]\n"
8980 " 'SELECT hex(sha3_query(''SELECT * FROM \"' ||"
8981 " printf('%w',name) || '\" NOT INDEXED'',224))',\n"
8982 " hex(sha3_query(printf('SELECT * FROM \"%w\" NOT INDEXED',name),224))\n"
8984 " SELECT name FROM sqlite_master\n"
8985 " WHERE type='table'\n"
8986 " AND name<>'selftest'\n"
8987 " AND coalesce(rootpage,0)>0\n"
8990 "INSERT INTO [_shell$self]\n"
8991 " VALUES('run','PRAGMA integrity_check','ok');\n"
8992 "INSERT INTO selftest(tno,op,cmd,ans)"
8993 " SELECT rowid*10,op,cmd,ans FROM [_shell$self];\n"
8994 "DROP TABLE [_shell$self];"
8997 utf8_printf(stderr, "SELFTEST initialization failure: %s\n", zErrMsg);
8998 sqlite3_free(zErrMsg);
9000 sqlite3_exec(p->db, "RELEASE selftest_init",0,0,0);
9005 ** Set the destination table field of the ShellState structure to
9006 ** the name of the table given. Escape any quote characters in the
9009 static void set_table_name(ShellState *p, const char *zName){
9014 if( p->zDestTable ){
9015 free(p->zDestTable);
9018 if( zName==0 ) return;
9019 cQuote = quoteChar(zName);
9020 n = strlen30(zName);
9021 if( cQuote ) n += n+2;
9022 z = p->zDestTable = malloc( n+1 );
9024 raw_printf(stderr,"Error: out of memory\n");
9028 if( cQuote ) z[n++] = cQuote;
9029 for(i=0; zName[i]; i++){
9031 if( zName[i]==cQuote ) z[n++] = cQuote;
9033 if( cQuote ) z[n++] = cQuote;
9039 ** Execute a query statement that will generate SQL output. Print
9040 ** the result columns, comma-separated, on a line and then add a
9041 ** semicolon terminator to the end of that line.
9043 ** If the number of columns is 1 and that column contains text "--"
9044 ** then write the semicolon on a separate line. That way, if a
9045 ** "--" comment occurs at the end of the statement, the comment
9046 ** won't consume the semicolon terminator.
9048 static int run_table_dump_query(
9049 ShellState *p, /* Query context */
9050 const char *zSelect, /* SELECT statement to extract content */
9051 const char *zFirstRow /* Print before first row, if not NULL */
9053 sqlite3_stmt *pSelect;
9058 rc = sqlite3_prepare_v2(p->db, zSelect, -1, &pSelect, 0);
9059 if( rc!=SQLITE_OK || !pSelect ){
9060 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9061 sqlite3_errmsg(p->db));
9062 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9065 rc = sqlite3_step(pSelect);
9066 nResult = sqlite3_column_count(pSelect);
9067 while( rc==SQLITE_ROW ){
9069 utf8_printf(p->out, "%s", zFirstRow);
9072 z = (const char*)sqlite3_column_text(pSelect, 0);
9073 utf8_printf(p->out, "%s", z);
9074 for(i=1; i<nResult; i++){
9075 utf8_printf(p->out, ",%s", sqlite3_column_text(pSelect, i));
9078 while( z[0] && (z[0]!='-' || z[1]!='-') ) z++;
9080 raw_printf(p->out, "\n;\n");
9082 raw_printf(p->out, ";\n");
9084 rc = sqlite3_step(pSelect);
9086 rc = sqlite3_finalize(pSelect);
9087 if( rc!=SQLITE_OK ){
9088 utf8_printf(p->out, "/**** ERROR: (%d) %s *****/\n", rc,
9089 sqlite3_errmsg(p->db));
9090 if( (rc&0xff)!=SQLITE_CORRUPT ) p->nErr++;
9096 ** Allocate space and save off current error string.
9098 static char *save_err_msg(
9099 sqlite3 *db /* Database to query */
9101 int nErrMsg = 1+strlen30(sqlite3_errmsg(db));
9102 char *zErrMsg = sqlite3_malloc64(nErrMsg);
9104 memcpy(zErrMsg, sqlite3_errmsg(db), nErrMsg);
9111 ** Attempt to display I/O stats on Linux using /proc/PID/io
9113 static void displayLinuxIoStats(FILE *out){
9116 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
9117 in = fopen(z, "rb");
9119 while( fgets(z, sizeof(z), in)!=0 ){
9120 static const struct {
9121 const char *zPattern;
9124 { "rchar: ", "Bytes received by read():" },
9125 { "wchar: ", "Bytes sent to write():" },
9126 { "syscr: ", "Read() system calls:" },
9127 { "syscw: ", "Write() system calls:" },
9128 { "read_bytes: ", "Bytes read from storage:" },
9129 { "write_bytes: ", "Bytes written to storage:" },
9130 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
9133 for(i=0; i<ArraySize(aTrans); i++){
9134 int n = strlen30(aTrans[i].zPattern);
9135 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
9136 utf8_printf(out, "%-36s %s", aTrans[i].zDesc, &z[n]);
9146 ** Display a single line of status using 64-bit values.
9148 static void displayStatLine(
9149 ShellState *p, /* The shell context */
9150 char *zLabel, /* Label for this one line */
9151 char *zFormat, /* Format for the result */
9152 int iStatusCtrl, /* Which status to display */
9153 int bReset /* True to reset the stats */
9155 sqlite3_int64 iCur = -1;
9156 sqlite3_int64 iHiwtr = -1;
9159 sqlite3_status64(iStatusCtrl, &iCur, &iHiwtr, bReset);
9160 for(i=0, nPercent=0; zFormat[i]; i++){
9161 if( zFormat[i]=='%' ) nPercent++;
9164 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iCur, iHiwtr);
9166 sqlite3_snprintf(sizeof(zLine), zLine, zFormat, iHiwtr);
9168 raw_printf(p->out, "%-36s %s\n", zLabel, zLine);
9172 ** Display memory stats.
9174 static int display_stats(
9175 sqlite3 *db, /* Database to query */
9176 ShellState *pArg, /* Pointer to ShellState */
9177 int bReset /* True to reset the stats */
9182 if( pArg && pArg->out ){
9183 displayStatLine(pArg, "Memory Used:",
9184 "%lld (max %lld) bytes", SQLITE_STATUS_MEMORY_USED, bReset);
9185 displayStatLine(pArg, "Number of Outstanding Allocations:",
9186 "%lld (max %lld)", SQLITE_STATUS_MALLOC_COUNT, bReset);
9187 if( pArg->shellFlgs & SHFLG_Pagecache ){
9188 displayStatLine(pArg, "Number of Pcache Pages Used:",
9189 "%lld (max %lld) pages", SQLITE_STATUS_PAGECACHE_USED, bReset);
9191 displayStatLine(pArg, "Number of Pcache Overflow Bytes:",
9192 "%lld (max %lld) bytes", SQLITE_STATUS_PAGECACHE_OVERFLOW, bReset);
9193 displayStatLine(pArg, "Largest Allocation:",
9194 "%lld bytes", SQLITE_STATUS_MALLOC_SIZE, bReset);
9195 displayStatLine(pArg, "Largest Pcache Allocation:",
9196 "%lld bytes", SQLITE_STATUS_PAGECACHE_SIZE, bReset);
9197 #ifdef YYTRACKMAXSTACKDEPTH
9198 displayStatLine(pArg, "Deepest Parser Stack:",
9199 "%lld (max %lld)", SQLITE_STATUS_PARSER_STACK, bReset);
9203 if( pArg && pArg->out && db ){
9204 if( pArg->shellFlgs & SHFLG_Lookaside ){
9206 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_USED,
9207 &iCur, &iHiwtr, bReset);
9208 raw_printf(pArg->out,
9209 "Lookaside Slots Used: %d (max %d)\n",
9211 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_HIT,
9212 &iCur, &iHiwtr, bReset);
9213 raw_printf(pArg->out, "Successful lookaside attempts: %d\n",
9215 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE,
9216 &iCur, &iHiwtr, bReset);
9217 raw_printf(pArg->out, "Lookaside failures due to size: %d\n",
9219 sqlite3_db_status(db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL,
9220 &iCur, &iHiwtr, bReset);
9221 raw_printf(pArg->out, "Lookaside failures due to OOM: %d\n",
9225 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHiwtr, bReset);
9226 raw_printf(pArg->out, "Pager Heap Usage: %d bytes\n",
9229 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHiwtr, 1);
9230 raw_printf(pArg->out, "Page cache hits: %d\n", iCur);
9232 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHiwtr, 1);
9233 raw_printf(pArg->out, "Page cache misses: %d\n", iCur);
9235 sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHiwtr, 1);
9236 raw_printf(pArg->out, "Page cache writes: %d\n", iCur);
9238 sqlite3_db_status(db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHiwtr, bReset);
9239 raw_printf(pArg->out, "Schema Heap Usage: %d bytes\n",
9242 sqlite3_db_status(db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHiwtr, bReset);
9243 raw_printf(pArg->out, "Statement Heap/Lookaside Usage: %d bytes\n",
9247 if( pArg && pArg->out && db && pArg->pStmt ){
9248 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_FULLSCAN_STEP,
9250 raw_printf(pArg->out, "Fullscan Steps: %d\n", iCur);
9251 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_SORT, bReset);
9252 raw_printf(pArg->out, "Sort Operations: %d\n", iCur);
9253 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_AUTOINDEX,bReset);
9254 raw_printf(pArg->out, "Autoindex Inserts: %d\n", iCur);
9255 iCur = sqlite3_stmt_status(pArg->pStmt, SQLITE_STMTSTATUS_VM_STEP, bReset);
9256 raw_printf(pArg->out, "Virtual Machine Steps: %d\n", iCur);
9260 displayLinuxIoStats(pArg->out);
9263 /* Do not remove this machine readable comment: extra-stats-output-here */
9269 ** Display scan stats.
9271 static void display_scanstats(
9272 sqlite3 *db, /* Database to query */
9273 ShellState *pArg /* Pointer to ShellState */
9275 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
9276 UNUSED_PARAMETER(db);
9277 UNUSED_PARAMETER(pArg);
9280 raw_printf(pArg->out, "-------- scanstats --------\n");
9282 for(k=0; k<=mx; k++){
9283 double rEstLoop = 1.0;
9285 sqlite3_stmt *p = pArg->pStmt;
9286 sqlite3_int64 nLoop, nVisit;
9289 const char *zExplain;
9290 if( sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NLOOP, (void*)&nLoop) ){
9293 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_SELECTID, (void*)&iSid);
9294 if( iSid>mx ) mx = iSid;
9295 if( iSid!=k ) continue;
9297 rEstLoop = (double)nLoop;
9298 if( k>0 ) raw_printf(pArg->out, "-------- subquery %d -------\n", k);
9301 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_NVISIT, (void*)&nVisit);
9302 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EST, (void*)&rEst);
9303 sqlite3_stmt_scanstatus(p, i, SQLITE_SCANSTAT_EXPLAIN, (void*)&zExplain);
9304 utf8_printf(pArg->out, "Loop %2d: %s\n", n, zExplain);
9306 raw_printf(pArg->out,
9307 " nLoop=%-8lld nRow=%-8lld estRow=%-8lld estRow/Loop=%-8g\n",
9308 nLoop, nVisit, (sqlite3_int64)(rEstLoop+0.5), rEst
9312 raw_printf(pArg->out, "---------------------------\n");
9317 ** Parameter azArray points to a zero-terminated array of strings. zStr
9318 ** points to a single nul-terminated string. Return non-zero if zStr
9319 ** is equal, according to strcmp(), to any of the strings in the array.
9320 ** Otherwise, return zero.
9322 static int str_in_array(const char *zStr, const char **azArray){
9324 for(i=0; azArray[i]; i++){
9325 if( 0==strcmp(zStr, azArray[i]) ) return 1;
9331 ** If compiled statement pSql appears to be an EXPLAIN statement, allocate
9332 ** and populate the ShellState.aiIndent[] array with the number of
9333 ** spaces each opcode should be indented before it is output.
9335 ** The indenting rules are:
9337 ** * For each "Next", "Prev", "VNext" or "VPrev" instruction, indent
9338 ** all opcodes that occur between the p2 jump destination and the opcode
9339 ** itself by 2 spaces.
9341 ** * For each "Goto", if the jump destination is earlier in the program
9342 ** and ends on one of:
9343 ** Yield SeekGt SeekLt RowSetRead Rewind
9344 ** or if the P1 parameter is one instead of zero,
9345 ** then indent all opcodes between the earlier instruction
9346 ** and "Goto" by 2 spaces.
9348 static void explain_data_prepare(ShellState *p, sqlite3_stmt *pSql){
9349 const char *zSql; /* The text of the SQL statement */
9350 const char *z; /* Used to check if this is an EXPLAIN */
9351 int *abYield = 0; /* True if op is an OP_Yield */
9352 int nAlloc = 0; /* Allocated size of p->aiIndent[], abYield */
9353 int iOp; /* Index of operation in p->aiIndent[] */
9355 const char *azNext[] = { "Next", "Prev", "VPrev", "VNext", "SorterNext",
9356 "NextIfOpen", "PrevIfOpen", 0 };
9357 const char *azYield[] = { "Yield", "SeekLT", "SeekGT", "RowSetRead",
9359 const char *azGoto[] = { "Goto", 0 };
9361 /* Try to figure out if this is really an EXPLAIN statement. If this
9362 ** cannot be verified, return early. */
9363 if( sqlite3_column_count(pSql)!=8 ){
9367 zSql = sqlite3_sql(pSql);
9368 if( zSql==0 ) return;
9369 for(z=zSql; *z==' ' || *z=='\t' || *z=='\n' || *z=='\f' || *z=='\r'; z++);
9370 if( sqlite3_strnicmp(z, "explain", 7) ){
9375 for(iOp=0; SQLITE_ROW==sqlite3_step(pSql); iOp++){
9377 int iAddr = sqlite3_column_int(pSql, 0);
9378 const char *zOp = (const char*)sqlite3_column_text(pSql, 1);
9380 /* Set p2 to the P2 field of the current opcode. Then, assuming that
9381 ** p2 is an instruction address, set variable p2op to the index of that
9382 ** instruction in the aiIndent[] array. p2 and p2op may be different if
9383 ** the current instruction is part of a sub-program generated by an
9384 ** SQL trigger or foreign key. */
9385 int p2 = sqlite3_column_int(pSql, 3);
9386 int p2op = (p2 + (iOp-iAddr));
9388 /* Grow the p->aiIndent array as required */
9391 /* Do further verfication that this is explain output. Abort if
9393 static const char *explainCols[] = {
9394 "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment" };
9396 for(jj=0; jj<ArraySize(explainCols); jj++){
9397 if( strcmp(sqlite3_column_name(pSql,jj),explainCols[jj])!=0 ){
9399 sqlite3_reset(pSql);
9405 p->aiIndent = (int*)sqlite3_realloc64(p->aiIndent, nAlloc*sizeof(int));
9406 abYield = (int*)sqlite3_realloc64(abYield, nAlloc*sizeof(int));
9408 abYield[iOp] = str_in_array(zOp, azYield);
9409 p->aiIndent[iOp] = 0;
9412 if( str_in_array(zOp, azNext) ){
9413 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
9415 if( str_in_array(zOp, azGoto) && p2op<p->nIndent
9416 && (abYield[p2op] || sqlite3_column_int(pSql, 2))
9418 for(i=p2op; i<iOp; i++) p->aiIndent[i] += 2;
9423 sqlite3_free(abYield);
9424 sqlite3_reset(pSql);
9428 ** Free the array allocated by explain_data_prepare().
9430 static void explain_data_delete(ShellState *p){
9431 sqlite3_free(p->aiIndent);
9438 ** Disable and restore .wheretrace and .selecttrace settings.
9440 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9441 extern int sqlite3SelectTrace;
9442 static int savedSelectTrace;
9444 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9445 extern int sqlite3WhereTrace;
9446 static int savedWhereTrace;
9448 static void disable_debug_trace_modes(void){
9449 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9450 savedSelectTrace = sqlite3SelectTrace;
9451 sqlite3SelectTrace = 0;
9453 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9454 savedWhereTrace = sqlite3WhereTrace;
9455 sqlite3WhereTrace = 0;
9458 static void restore_debug_trace_modes(void){
9459 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
9460 sqlite3SelectTrace = savedSelectTrace;
9462 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
9463 sqlite3WhereTrace = savedWhereTrace;
9468 ** Run a prepared statement
9470 static void exec_prepared_stmt(
9471 ShellState *pArg, /* Pointer to ShellState */
9472 sqlite3_stmt *pStmt, /* Statment to run */
9473 int (*xCallback)(void*,int,char**,char**,int*) /* Callback function */
9477 /* perform the first step. this will tell us if we
9478 ** have a result set or not and how wide it is.
9480 rc = sqlite3_step(pStmt);
9481 /* if we have a result set... */
9482 if( SQLITE_ROW == rc ){
9483 /* if we have a callback... */
9485 /* allocate space for col name ptr, value ptr, and type */
9486 int nCol = sqlite3_column_count(pStmt);
9487 void *pData = sqlite3_malloc64(3*nCol*sizeof(const char*) + 1);
9491 char **azCols = (char **)pData; /* Names of result columns */
9492 char **azVals = &azCols[nCol]; /* Results */
9493 int *aiTypes = (int *)&azVals[nCol]; /* Result types */
9495 assert(sizeof(int) <= sizeof(char *));
9496 /* save off ptrs to column names */
9497 for(i=0; i<nCol; i++){
9498 azCols[i] = (char *)sqlite3_column_name(pStmt, i);
9501 /* extract the data and data types */
9502 for(i=0; i<nCol; i++){
9503 aiTypes[i] = x = sqlite3_column_type(pStmt, i);
9504 if( x==SQLITE_BLOB && pArg && pArg->cMode==MODE_Insert ){
9507 azVals[i] = (char*)sqlite3_column_text(pStmt, i);
9509 if( !azVals[i] && (aiTypes[i]!=SQLITE_NULL) ){
9511 break; /* from for */
9515 /* if data and types extracted successfully... */
9516 if( SQLITE_ROW == rc ){
9517 /* call the supplied callback with the result row data */
9518 if( xCallback(pArg, nCol, azVals, azCols, aiTypes) ){
9521 rc = sqlite3_step(pStmt);
9524 } while( SQLITE_ROW == rc );
9525 sqlite3_free(pData);
9529 rc = sqlite3_step(pStmt);
9530 } while( rc == SQLITE_ROW );
9535 #ifndef SQLITE_OMIT_VIRTUALTABLE
9537 ** This function is called to process SQL if the previous shell command
9538 ** was ".expert". It passes the SQL in the second argument directly to
9539 ** the sqlite3expert object.
9541 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
9542 ** code. In this case, (*pzErr) may be set to point to a buffer containing
9543 ** an English language error message. It is the responsibility of the
9544 ** caller to eventually free this buffer using sqlite3_free().
9546 static int expertHandleSQL(
9551 assert( pState->expert.pExpert );
9552 assert( pzErr==0 || *pzErr==0 );
9553 return sqlite3_expert_sql(pState->expert.pExpert, zSql, pzErr);
9557 ** This function is called either to silently clean up the object
9558 ** created by the ".expert" command (if bCancel==1), or to generate a
9559 ** report from it and then clean it up (if bCancel==0).
9561 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error
9562 ** code. In this case, (*pzErr) may be set to point to a buffer containing
9563 ** an English language error message. It is the responsibility of the
9564 ** caller to eventually free this buffer using sqlite3_free().
9566 static int expertFinish(
9572 sqlite3expert *p = pState->expert.pExpert;
9574 assert( bCancel || pzErr==0 || *pzErr==0 );
9576 FILE *out = pState->out;
9577 int bVerbose = pState->expert.bVerbose;
9579 rc = sqlite3_expert_analyze(p, pzErr);
9580 if( rc==SQLITE_OK ){
9581 int nQuery = sqlite3_expert_count(p);
9585 const char *zCand = sqlite3_expert_report(p,0,EXPERT_REPORT_CANDIDATES);
9586 raw_printf(out, "-- Candidates -----------------------------\n");
9587 raw_printf(out, "%s\n", zCand);
9589 for(i=0; i<nQuery; i++){
9590 const char *zSql = sqlite3_expert_report(p, i, EXPERT_REPORT_SQL);
9591 const char *zIdx = sqlite3_expert_report(p, i, EXPERT_REPORT_INDEXES);
9592 const char *zEQP = sqlite3_expert_report(p, i, EXPERT_REPORT_PLAN);
9593 if( zIdx==0 ) zIdx = "(no new indexes)\n";
9595 raw_printf(out, "-- Query %d --------------------------------\n",i+1);
9596 raw_printf(out, "%s\n\n", zSql);
9598 raw_printf(out, "%s\n", zIdx);
9599 raw_printf(out, "%s\n", zEQP);
9603 sqlite3_expert_destroy(p);
9604 pState->expert.pExpert = 0;
9609 ** Implementation of ".expert" dot command.
9611 static int expertDotCommand(
9612 ShellState *pState, /* Current shell tool state */
9613 char **azArg, /* Array of arguments passed to dot command */
9614 int nArg /* Number of entries in azArg[] */
9621 assert( pState->expert.pExpert==0 );
9622 memset(&pState->expert, 0, sizeof(ExpertInfo));
9624 for(i=1; rc==SQLITE_OK && i<nArg; i++){
9627 if( z[0]=='-' && z[1]=='-' ) z++;
9629 if( n>=2 && 0==strncmp(z, "-verbose", n) ){
9630 pState->expert.bVerbose = 1;
9632 else if( n>=2 && 0==strncmp(z, "-sample", n) ){
9634 raw_printf(stderr, "option requires an argument: %s\n", z);
9637 iSample = (int)integerValue(azArg[++i]);
9638 if( iSample<0 || iSample>100 ){
9639 raw_printf(stderr, "value out of range: %s\n", azArg[i]);
9645 raw_printf(stderr, "unknown option: %s\n", z);
9650 if( rc==SQLITE_OK ){
9651 pState->expert.pExpert = sqlite3_expert_new(pState->db, &zErr);
9652 if( pState->expert.pExpert==0 ){
9653 raw_printf(stderr, "sqlite3_expert_new: %s\n", zErr);
9656 sqlite3_expert_config(
9657 pState->expert.pExpert, EXPERT_CONFIG_SAMPLE, iSample
9664 #endif /* ifndef SQLITE_OMIT_VIRTUALTABLE */
9667 ** Execute a statement or set of statements. Print
9668 ** any result rows/columns depending on the current mode
9669 ** set via the supplied callback.
9671 ** This is very similar to SQLite's built-in sqlite3_exec()
9672 ** function except it takes a slightly different callback
9673 ** and callback data argument.
9675 static int shell_exec(
9676 sqlite3 *db, /* An open database */
9677 const char *zSql, /* SQL to be evaluated */
9678 int (*xCallback)(void*,int,char**,char**,int*), /* Callback function */
9679 /* (not the same as sqlite3_exec) */
9680 ShellState *pArg, /* Pointer to ShellState */
9681 char **pzErrMsg /* Error msg written here */
9683 sqlite3_stmt *pStmt = NULL; /* Statement to execute. */
9684 int rc = SQLITE_OK; /* Return Code */
9686 const char *zLeftover; /* Tail of unprocessed SQL */
9692 #ifndef SQLITE_OMIT_VIRTUALTABLE
9693 if( pArg->expert.pExpert ){
9694 rc = expertHandleSQL(pArg, zSql, pzErrMsg);
9695 return expertFinish(pArg, (rc!=SQLITE_OK), pzErrMsg);
9699 while( zSql[0] && (SQLITE_OK == rc) ){
9700 static const char *zStmtSql;
9701 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
9702 if( SQLITE_OK != rc ){
9704 *pzErrMsg = save_err_msg(db);
9708 /* this happens for a comment or white-space */
9710 while( IsSpace(zSql[0]) ) zSql++;
9713 zStmtSql = sqlite3_sql(pStmt);
9714 if( zStmtSql==0 ) zStmtSql = "";
9715 while( IsSpace(zStmtSql[0]) ) zStmtSql++;
9717 /* save off the prepared statment handle and reset row count */
9719 pArg->pStmt = pStmt;
9723 /* echo the sql statement if echo on */
9724 if( pArg && ShellHasFlag(pArg, SHFLG_Echo) ){
9725 utf8_printf(pArg->out, "%s\n", zStmtSql ? zStmtSql : zSql);
9728 /* Show the EXPLAIN QUERY PLAN if .eqp is on */
9729 if( pArg && pArg->autoEQP && sqlite3_strlike("EXPLAIN%",zStmtSql,0)!=0 ){
9730 sqlite3_stmt *pExplain;
9733 disable_debug_trace_modes();
9734 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, -1, &triggerEQP);
9735 if( pArg->autoEQP>=AUTOEQP_trigger ){
9736 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, 1, 0);
9738 zEQP = sqlite3_mprintf("EXPLAIN QUERY PLAN %s", zStmtSql);
9739 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
9740 if( rc==SQLITE_OK ){
9741 while( sqlite3_step(pExplain)==SQLITE_ROW ){
9742 raw_printf(pArg->out,"--EQP-- %d,",sqlite3_column_int(pExplain, 0));
9743 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 1));
9744 raw_printf(pArg->out,"%d,", sqlite3_column_int(pExplain, 2));
9745 utf8_printf(pArg->out,"%s\n", sqlite3_column_text(pExplain, 3));
9748 sqlite3_finalize(pExplain);
9750 if( pArg->autoEQP>=AUTOEQP_full ){
9751 /* Also do an EXPLAIN for ".eqp full" mode */
9752 zEQP = sqlite3_mprintf("EXPLAIN %s", zStmtSql);
9753 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
9754 if( rc==SQLITE_OK ){
9755 pArg->cMode = MODE_Explain;
9756 explain_data_prepare(pArg, pExplain);
9757 exec_prepared_stmt(pArg, pExplain, xCallback);
9758 explain_data_delete(pArg);
9760 sqlite3_finalize(pExplain);
9763 sqlite3_db_config(db, SQLITE_DBCONFIG_TRIGGER_EQP, triggerEQP, 0);
9764 restore_debug_trace_modes();
9768 pArg->cMode = pArg->mode;
9769 if( pArg->autoExplain
9770 && sqlite3_column_count(pStmt)==8
9771 && sqlite3_strlike("EXPLAIN%", zStmtSql,0)==0
9773 pArg->cMode = MODE_Explain;
9776 /* If the shell is currently in ".explain" mode, gather the extra
9777 ** data required to add indents to the output.*/
9778 if( pArg->cMode==MODE_Explain ){
9779 explain_data_prepare(pArg, pStmt);
9783 exec_prepared_stmt(pArg, pStmt, xCallback);
9784 explain_data_delete(pArg);
9786 /* print usage stats if stats on */
9787 if( pArg && pArg->statsOn ){
9788 display_stats(db, pArg, 0);
9791 /* print loop-counters if required */
9792 if( pArg && pArg->scanstatsOn ){
9793 display_scanstats(db, pArg);
9796 /* Finalize the statement just executed. If this fails, save a
9797 ** copy of the error message. Otherwise, set zSql to point to the
9798 ** next statement to execute. */
9799 rc2 = sqlite3_finalize(pStmt);
9800 if( rc!=SQLITE_NOMEM ) rc = rc2;
9801 if( rc==SQLITE_OK ){
9803 while( IsSpace(zSql[0]) ) zSql++;
9804 }else if( pzErrMsg ){
9805 *pzErrMsg = save_err_msg(db);
9808 /* clear saved stmt handle */
9819 ** Release memory previously allocated by tableColumnList().
9821 static void freeColumnList(char **azCol){
9823 for(i=1; azCol[i]; i++){
9824 sqlite3_free(azCol[i]);
9826 /* azCol[0] is a static string */
9827 sqlite3_free(azCol);
9831 ** Return a list of pointers to strings which are the names of all
9832 ** columns in table zTab. The memory to hold the names is dynamically
9833 ** allocated and must be released by the caller using a subsequent call
9834 ** to freeColumnList().
9836 ** The azCol[0] entry is usually NULL. However, if zTab contains a rowid
9837 ** value that needs to be preserved, then azCol[0] is filled in with the
9838 ** name of the rowid column.
9840 ** The first regular column in the table is azCol[1]. The list is terminated
9841 ** by an entry with azCol[i]==0.
9843 static char **tableColumnList(ShellState *p, const char *zTab){
9845 sqlite3_stmt *pStmt;
9849 int nPK = 0; /* Number of PRIMARY KEY columns seen */
9850 int isIPK = 0; /* True if one PRIMARY KEY column of type INTEGER */
9851 int preserveRowid = ShellHasFlag(p, SHFLG_PreserveRowid);
9854 zSql = sqlite3_mprintf("PRAGMA table_info=%Q", zTab);
9855 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9858 while( sqlite3_step(pStmt)==SQLITE_ROW ){
9859 if( nCol>=nAlloc-2 ){
9860 nAlloc = nAlloc*2 + nCol + 10;
9861 azCol = sqlite3_realloc(azCol, nAlloc*sizeof(azCol[0]));
9863 raw_printf(stderr, "Error: out of memory\n");
9867 azCol[++nCol] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 1));
9868 if( sqlite3_column_int(pStmt, 5) ){
9871 && sqlite3_stricmp((const char*)sqlite3_column_text(pStmt,2),
9880 sqlite3_finalize(pStmt);
9881 if( azCol==0 ) return 0;
9885 /* The decision of whether or not a rowid really needs to be preserved
9886 ** is tricky. We never need to preserve a rowid for a WITHOUT ROWID table
9887 ** or a table with an INTEGER PRIMARY KEY. We are unable to preserve
9888 ** rowids on tables where the rowid is inaccessible because there are other
9889 ** columns in the table named "rowid", "_rowid_", and "oid".
9891 if( preserveRowid && isIPK ){
9892 /* If a single PRIMARY KEY column with type INTEGER was seen, then it
9893 ** might be an alise for the ROWID. But it might also be a WITHOUT ROWID
9894 ** table or a INTEGER PRIMARY KEY DESC column, neither of which are
9895 ** ROWID aliases. To distinguish these cases, check to see if
9896 ** there is a "pk" entry in "PRAGMA index_list". There will be
9897 ** no "pk" index if the PRIMARY KEY really is an alias for the ROWID.
9899 zSql = sqlite3_mprintf("SELECT 1 FROM pragma_index_list(%Q)"
9900 " WHERE origin='pk'", zTab);
9901 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
9904 freeColumnList(azCol);
9907 rc = sqlite3_step(pStmt);
9908 sqlite3_finalize(pStmt);
9909 preserveRowid = rc==SQLITE_ROW;
9911 if( preserveRowid ){
9912 /* Only preserve the rowid if we can find a name to use for the
9914 static char *azRowid[] = { "rowid", "_rowid_", "oid" };
9917 for(i=1; i<=nCol; i++){
9918 if( sqlite3_stricmp(azRowid[j],azCol[i])==0 ) break;
9921 /* At this point, we know that azRowid[j] is not the name of any
9922 ** ordinary column in the table. Verify that azRowid[j] is a valid
9923 ** name for the rowid before adding it to azCol[0]. WITHOUT ROWID
9924 ** tables will fail this last check */
9925 rc = sqlite3_table_column_metadata(p->db,0,zTab,azRowid[j],0,0,0,0,0);
9926 if( rc==SQLITE_OK ) azCol[0] = azRowid[j];
9935 ** Toggle the reverse_unordered_selects setting.
9937 static void toggleSelectOrder(sqlite3 *db){
9938 sqlite3_stmt *pStmt = 0;
9941 sqlite3_prepare_v2(db, "PRAGMA reverse_unordered_selects", -1, &pStmt, 0);
9942 if( sqlite3_step(pStmt)==SQLITE_ROW ){
9943 iSetting = sqlite3_column_int(pStmt, 0);
9945 sqlite3_finalize(pStmt);
9946 sqlite3_snprintf(sizeof(zStmt), zStmt,
9947 "PRAGMA reverse_unordered_selects(%d)", !iSetting);
9948 sqlite3_exec(db, zStmt, 0, 0, 0);
9952 ** This is a different callback routine used for dumping the database.
9953 ** Each row received by this callback consists of a table name,
9954 ** the table type ("index" or "table") and SQL to create the table.
9955 ** This routine should print text sufficient to recreate the table.
9957 static int dump_callback(void *pArg, int nArg, char **azArg, char **azNotUsed){
9962 ShellState *p = (ShellState *)pArg;
9964 UNUSED_PARAMETER(azNotUsed);
9965 if( nArg!=3 || azArg==0 ) return 0;
9970 if( strcmp(zTable, "sqlite_sequence")==0 ){
9971 raw_printf(p->out, "DELETE FROM sqlite_sequence;\n");
9972 }else if( sqlite3_strglob("sqlite_stat?", zTable)==0 ){
9973 raw_printf(p->out, "ANALYZE sqlite_master;\n");
9974 }else if( strncmp(zTable, "sqlite_", 7)==0 ){
9976 }else if( strncmp(zSql, "CREATE VIRTUAL TABLE", 20)==0 ){
9978 if( !p->writableSchema ){
9979 raw_printf(p->out, "PRAGMA writable_schema=ON;\n");
9980 p->writableSchema = 1;
9982 zIns = sqlite3_mprintf(
9983 "INSERT INTO sqlite_master(type,name,tbl_name,rootpage,sql)"
9984 "VALUES('table','%q','%q',0,'%q');",
9985 zTable, zTable, zSql);
9986 utf8_printf(p->out, "%s\n", zIns);
9990 printSchemaLine(p->out, zSql, ";\n");
9993 if( strcmp(zType, "table")==0 ){
9998 char *savedDestTable;
10001 azCol = tableColumnList(p, zTable);
10007 /* Always quote the table name, even if it appears to be pure ascii,
10008 ** in case it is a keyword. Ex: INSERT INTO "table" ... */
10010 appendText(&sTable, zTable, quoteChar(zTable));
10011 /* If preserving the rowid, add a column list after the table name.
10012 ** In other words: "INSERT INTO tab(rowid,a,b,c,...) VALUES(...)"
10013 ** instead of the usual "INSERT INTO tab VALUES(...)".
10016 appendText(&sTable, "(", 0);
10017 appendText(&sTable, azCol[0], 0);
10018 for(i=1; azCol[i]; i++){
10019 appendText(&sTable, ",", 0);
10020 appendText(&sTable, azCol[i], quoteChar(azCol[i]));
10022 appendText(&sTable, ")", 0);
10025 /* Build an appropriate SELECT statement */
10026 initText(&sSelect);
10027 appendText(&sSelect, "SELECT ", 0);
10029 appendText(&sSelect, azCol[0], 0);
10030 appendText(&sSelect, ",", 0);
10032 for(i=1; azCol[i]; i++){
10033 appendText(&sSelect, azCol[i], quoteChar(azCol[i]));
10035 appendText(&sSelect, ",", 0);
10038 freeColumnList(azCol);
10039 appendText(&sSelect, " FROM ", 0);
10040 appendText(&sSelect, zTable, quoteChar(zTable));
10042 savedDestTable = p->zDestTable;
10043 savedMode = p->mode;
10044 p->zDestTable = sTable.z;
10045 p->mode = p->cMode = MODE_Insert;
10046 rc = shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10047 if( (rc&0xff)==SQLITE_CORRUPT ){
10048 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10049 toggleSelectOrder(p->db);
10050 shell_exec(p->db, sSelect.z, shell_callback, p, 0);
10051 toggleSelectOrder(p->db);
10053 p->zDestTable = savedDestTable;
10054 p->mode = savedMode;
10056 freeText(&sSelect);
10057 if( rc ) p->nErr++;
10063 ** Run zQuery. Use dump_callback() as the callback routine so that
10064 ** the contents of the query are output as SQL statements.
10066 ** If we get a SQLITE_CORRUPT error, rerun the query after appending
10067 ** "ORDER BY rowid DESC" to the end.
10069 static int run_schema_dump_query(
10075 rc = sqlite3_exec(p->db, zQuery, dump_callback, p, &zErr);
10076 if( rc==SQLITE_CORRUPT ){
10078 int len = strlen30(zQuery);
10079 raw_printf(p->out, "/****** CORRUPTION ERROR *******/\n");
10081 utf8_printf(p->out, "/****** %s ******/\n", zErr);
10082 sqlite3_free(zErr);
10085 zQ2 = malloc( len+100 );
10086 if( zQ2==0 ) return rc;
10087 sqlite3_snprintf(len+100, zQ2, "%s ORDER BY rowid DESC", zQuery);
10088 rc = sqlite3_exec(p->db, zQ2, dump_callback, p, &zErr);
10090 utf8_printf(p->out, "/****** ERROR: %s ******/\n", zErr);
10092 rc = SQLITE_CORRUPT;
10094 sqlite3_free(zErr);
10101 ** Text of a help message
10103 static char zHelp[] =
10104 #if defined(SQLITE_HAVE_ZLIB) && !defined(SQLITE_OMIT_VIRTUALTABLE)
10105 ".archive ... Manage SQL archives: \".archive --help\" for details\n"
10107 #ifndef SQLITE_OMIT_AUTHORIZATION
10108 ".auth ON|OFF Show authorizer callbacks\n"
10110 ".backup ?DB? FILE Backup DB (default \"main\") to FILE\n"
10111 ".bail on|off Stop after hitting an error. Default OFF\n"
10112 ".binary on|off Turn binary output on or off. Default OFF\n"
10113 ".cd DIRECTORY Change the working directory to DIRECTORY\n"
10114 ".changes on|off Show number of rows changed by SQL\n"
10115 ".check GLOB Fail if output since .testcase does not match\n"
10116 ".clone NEWDB Clone data into NEWDB from the existing database\n"
10117 ".databases List names and files of attached databases\n"
10118 ".dbinfo ?DB? Show status information about the database\n"
10119 ".dump ?TABLE? ... Dump the database in an SQL text format\n"
10120 " If TABLE specified, only dump tables matching\n"
10121 " LIKE pattern TABLE.\n"
10122 ".echo on|off Turn command echo on or off\n"
10123 ".eqp on|off|full Enable or disable automatic EXPLAIN QUERY PLAN\n"
10124 ".excel Display the output of next command in a spreadsheet\n"
10125 ".exit Exit this program\n"
10126 ".expert EXPERIMENTAL. Suggest indexes for specified queries\n"
10127 /* Because explain mode comes on automatically now, the ".explain" mode
10128 ** is removed from the help screen. It is still supported for legacy, however */
10129 /*".explain ?on|off|auto? Turn EXPLAIN output mode on or off or to automatic\n"*/
10130 ".fullschema ?--indent? Show schema and the content of sqlite_stat tables\n"
10131 ".headers on|off Turn display of headers on or off\n"
10132 ".help Show this message\n"
10133 ".import FILE TABLE Import data from FILE into TABLE\n"
10134 #ifndef SQLITE_OMIT_TEST_CONTROL
10135 ".imposter INDEX TABLE Create imposter table TABLE on index INDEX\n"
10137 ".indexes ?TABLE? Show names of all indexes\n"
10138 " If TABLE specified, only show indexes for tables\n"
10139 " matching LIKE pattern TABLE.\n"
10140 #ifdef SQLITE_ENABLE_IOTRACE
10141 ".iotrace FILE Enable I/O diagnostic logging to FILE\n"
10143 ".limit ?LIMIT? ?VAL? Display or change the value of an SQLITE_LIMIT\n"
10144 ".lint OPTIONS Report potential schema issues. Options:\n"
10145 " fkey-indexes Find missing foreign key indexes\n"
10146 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10147 ".load FILE ?ENTRY? Load an extension library\n"
10149 ".log FILE|off Turn logging on or off. FILE can be stderr/stdout\n"
10150 ".mode MODE ?TABLE? Set output mode where MODE is one of:\n"
10151 " ascii Columns/rows delimited by 0x1F and 0x1E\n"
10152 " csv Comma-separated values\n"
10153 " column Left-aligned columns. (See .width)\n"
10154 " html HTML <table> code\n"
10155 " insert SQL insert statements for TABLE\n"
10156 " line One value per line\n"
10157 " list Values delimited by \"|\"\n"
10158 " quote Escape answers as for SQL\n"
10159 " tabs Tab-separated values\n"
10160 " tcl TCL list elements\n"
10161 ".nullvalue STRING Use STRING in place of NULL values\n"
10162 ".once (-e|-x|FILE) Output for the next SQL command only to FILE\n"
10163 " or invoke system text editor (-e) or spreadsheet (-x)\n"
10164 " on the output.\n"
10165 ".open ?OPTIONS? ?FILE? Close existing database and reopen FILE\n"
10166 " The --new option starts with an empty file\n"
10167 ".output ?FILE? Send output to FILE or stdout\n"
10168 ".print STRING... Print literal STRING\n"
10169 ".prompt MAIN CONTINUE Replace the standard prompts\n"
10170 ".quit Exit this program\n"
10171 ".read FILENAME Execute SQL in FILENAME\n"
10172 ".restore ?DB? FILE Restore content of DB (default \"main\") from FILE\n"
10173 ".save FILE Write in-memory database into FILE\n"
10174 ".scanstats on|off Turn sqlite3_stmt_scanstatus() metrics on or off\n"
10175 ".schema ?PATTERN? Show the CREATE statements matching PATTERN\n"
10176 " Add --indent for pretty-printing\n"
10177 ".selftest ?--init? Run tests defined in the SELFTEST table\n"
10178 ".separator COL ?ROW? Change the column separator and optionally the row\n"
10179 " separator for both the output mode and .import\n"
10180 #if defined(SQLITE_ENABLE_SESSION)
10181 ".session CMD ... Create or control sessions\n"
10183 ".sha3sum ?OPTIONS...? Compute a SHA3 hash of database content\n"
10184 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
10185 ".show Show the current values for various settings\n"
10186 ".stats ?on|off? Show stats or turn stats on or off\n"
10187 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
10188 ".tables ?TABLE? List names of tables\n"
10189 " If TABLE specified, only list tables matching\n"
10190 " LIKE pattern TABLE.\n"
10191 ".testcase NAME Begin redirecting output to 'testcase-out.txt'\n"
10192 ".timeout MS Try opening locked tables for MS milliseconds\n"
10193 ".timer on|off Turn SQL timer on or off\n"
10194 ".trace FILE|off Output each SQL statement as it is run\n"
10195 ".vfsinfo ?AUX? Information about the top-level VFS\n"
10196 ".vfslist List all available VFSes\n"
10197 ".vfsname ?AUX? Print the name of the VFS stack\n"
10198 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
10199 " Negative values right-justify\n"
10202 #if defined(SQLITE_ENABLE_SESSION)
10204 ** Print help information for the ".sessions" command
10206 void session_help(ShellState *p){
10208 ".session ?NAME? SUBCOMMAND ?ARGS...?\n"
10209 "If ?NAME? is omitted, the first defined session is used.\n"
10211 " attach TABLE Attach TABLE\n"
10212 " changeset FILE Write a changeset into FILE\n"
10213 " close Close one session\n"
10214 " enable ?BOOLEAN? Set or query the enable bit\n"
10215 " filter GLOB... Reject tables matching GLOBs\n"
10216 " indirect ?BOOLEAN? Mark or query the indirect status\n"
10217 " isempty Query whether the session is empty\n"
10218 " list List currently open session names\n"
10219 " open DB NAME Open a new session on DB\n"
10220 " patchset FILE Write a patchset into FILE\n"
10226 /* Forward reference */
10227 static int process_input(ShellState *p, FILE *in);
10230 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
10231 ** and return a pointer to the buffer. The caller is responsible for freeing
10234 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
10237 ** For convenience, a nul-terminator byte is always appended to the data read
10238 ** from the file before the buffer is returned. This byte is not included in
10239 ** the final value of (*pnByte), if applicable.
10241 ** NULL is returned if any error is encountered. The final value of *pnByte
10242 ** is undefined in this case.
10244 static char *readFile(const char *zName, int *pnByte){
10245 FILE *in = fopen(zName, "rb");
10249 if( in==0 ) return 0;
10250 fseek(in, 0, SEEK_END);
10253 pBuf = sqlite3_malloc64( nIn+1 );
10254 if( pBuf==0 ) return 0;
10255 nRead = fread(pBuf, nIn, 1, in);
10258 sqlite3_free(pBuf);
10262 if( pnByte ) *pnByte = nIn;
10266 #if defined(SQLITE_ENABLE_SESSION)
10268 ** Close a single OpenSession object and release all of its associated
10271 static void session_close(OpenSession *pSession){
10273 sqlite3session_delete(pSession->p);
10274 sqlite3_free(pSession->zName);
10275 for(i=0; i<pSession->nFilter; i++){
10276 sqlite3_free(pSession->azFilter[i]);
10278 sqlite3_free(pSession->azFilter);
10279 memset(pSession, 0, sizeof(OpenSession));
10284 ** Close all OpenSession objects and release all associated resources.
10286 #if defined(SQLITE_ENABLE_SESSION)
10287 static void session_close_all(ShellState *p){
10289 for(i=0; i<p->nSession; i++){
10290 session_close(&p->aSession[i]);
10295 # define session_close_all(X)
10299 ** Implementation of the xFilter function for an open session. Omit
10300 ** any tables named by ".session filter" but let all other table through.
10302 #if defined(SQLITE_ENABLE_SESSION)
10303 static int session_filter(void *pCtx, const char *zTab){
10304 OpenSession *pSession = (OpenSession*)pCtx;
10306 for(i=0; i<pSession->nFilter; i++){
10307 if( sqlite3_strglob(pSession->azFilter[i], zTab)==0 ) return 0;
10314 ** Try to deduce the type of file for zName based on its content. Return
10315 ** one of the SHELL_OPEN_* constants.
10317 static int deduceDatabaseType(const char *zName){
10318 FILE *f = fopen(zName, "rb");
10320 int rc = SHELL_OPEN_UNSPEC;
10322 if( f==0 ) return SHELL_OPEN_NORMAL;
10323 fseek(f, -25, SEEK_END);
10324 n = fread(zBuf, 25, 1, f);
10325 if( n==1 && memcmp(zBuf, "Start-Of-SQLite3-", 17)==0 ){
10326 rc = SHELL_OPEN_APPENDVFS;
10328 fseek(f, -22, SEEK_END);
10329 n = fread(zBuf, 22, 1, f);
10330 if( n==1 && zBuf[0]==0x50 && zBuf[1]==0x4b && zBuf[2]==0x05
10331 && zBuf[3]==0x06 ){
10332 rc = SHELL_OPEN_ZIPFILE;
10340 ** Make sure the database is open. If it is not, then open it. If
10341 ** the database fails to open, print an error message and exit.
10343 static void open_db(ShellState *p, int keepAlive){
10345 sqlite3_initialize();
10346 if( p->openMode==SHELL_OPEN_UNSPEC && access(p->zDbFilename,0)==0 ){
10347 p->openMode = deduceDatabaseType(p->zDbFilename);
10349 switch( p->openMode ){
10350 case SHELL_OPEN_APPENDVFS: {
10351 sqlite3_open_v2(p->zDbFilename, &p->db,
10352 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, "apndvfs");
10355 case SHELL_OPEN_ZIPFILE: {
10356 sqlite3_open(":memory:", &p->db);
10359 case SHELL_OPEN_UNSPEC:
10360 case SHELL_OPEN_NORMAL: {
10361 sqlite3_open(p->zDbFilename, &p->db);
10366 if( p->db==0 || SQLITE_OK!=sqlite3_errcode(p->db) ){
10367 utf8_printf(stderr,"Error: unable to open database \"%s\": %s\n",
10368 p->zDbFilename, sqlite3_errmsg(p->db));
10369 if( keepAlive ) return;
10372 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10373 sqlite3_enable_load_extension(p->db, 1);
10375 sqlite3_fileio_init(p->db, 0, 0);
10376 sqlite3_shathree_init(p->db, 0, 0);
10377 sqlite3_completion_init(p->db, 0, 0);
10378 #ifdef SQLITE_HAVE_ZLIB
10379 sqlite3_zipfile_init(p->db, 0, 0);
10380 sqlite3_sqlar_init(p->db, 0, 0);
10382 sqlite3_create_function(p->db, "shell_add_schema", 3, SQLITE_UTF8, 0,
10383 shellAddSchemaName, 0, 0);
10384 sqlite3_create_function(p->db, "shell_module_schema", 1, SQLITE_UTF8, 0,
10385 shellModuleSchema, 0, 0);
10386 sqlite3_create_function(p->db, "shell_putsnl", 1, SQLITE_UTF8, p,
10387 shellPutsFunc, 0, 0);
10388 sqlite3_create_function(p->db, "edit", 1, SQLITE_UTF8, 0,
10390 sqlite3_create_function(p->db, "edit", 2, SQLITE_UTF8, 0,
10392 if( p->openMode==SHELL_OPEN_ZIPFILE ){
10393 char *zSql = sqlite3_mprintf(
10394 "CREATE VIRTUAL TABLE zip USING zipfile(%Q);", p->zDbFilename);
10395 sqlite3_exec(p->db, zSql, 0, 0, 0);
10396 sqlite3_free(zSql);
10401 #if HAVE_READLINE || HAVE_EDITLINE
10403 ** Readline completion callbacks
10405 static char *readline_completion_generator(const char *text, int state){
10406 static sqlite3_stmt *pStmt = 0;
10410 sqlite3_finalize(pStmt);
10411 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
10412 " FROM completion(%Q) ORDER BY 1", text);
10413 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
10414 sqlite3_free(zSql);
10416 if( sqlite3_step(pStmt)==SQLITE_ROW ){
10417 zRet = strdup((const char*)sqlite3_column_text(pStmt, 0));
10419 sqlite3_finalize(pStmt);
10425 static char **readline_completion(const char *zText, int iStart, int iEnd){
10426 rl_attempted_completion_over = 1;
10427 return rl_completion_matches(zText, readline_completion_generator);
10430 #elif HAVE_LINENOISE
10432 ** Linenoise completion callback
10434 static void linenoise_completion(const char *zLine, linenoiseCompletions *lc){
10435 int nLine = strlen30(zLine);
10437 sqlite3_stmt *pStmt = 0;
10441 if( nLine>sizeof(zBuf)-30 ) return;
10442 if( zLine[0]=='.' ) return;
10443 for(i=nLine-1; i>=0 && (isalnum(zLine[i]) || zLine[i]=='_'); i--){}
10444 if( i==nLine-1 ) return;
10446 memcpy(zBuf, zLine, iStart);
10447 zSql = sqlite3_mprintf("SELECT DISTINCT candidate COLLATE nocase"
10448 " FROM completion(%Q,%Q) ORDER BY 1",
10449 &zLine[iStart], zLine);
10450 sqlite3_prepare_v2(globalDb, zSql, -1, &pStmt, 0);
10451 sqlite3_free(zSql);
10452 sqlite3_exec(globalDb, "PRAGMA page_count", 0, 0, 0); /* Load the schema */
10453 while( sqlite3_step(pStmt)==SQLITE_ROW ){
10454 const char *zCompletion = (const char*)sqlite3_column_text(pStmt, 0);
10455 int nCompletion = sqlite3_column_bytes(pStmt, 0);
10456 if( iStart+nCompletion < sizeof(zBuf)-1 ){
10457 memcpy(zBuf+iStart, zCompletion, nCompletion+1);
10458 linenoiseAddCompletion(lc, zBuf);
10461 sqlite3_finalize(pStmt);
10466 ** Do C-language style dequoting.
10472 ** \v -> vertical tab
10474 ** \r -> carriage return
10479 ** \NNN -> ascii character NNN in octal
10481 static void resolve_backslashes(char *z){
10484 while( *z && *z!='\\' ) z++;
10485 for(i=j=0; (c = z[i])!=0; i++, j++){
10486 if( c=='\\' && z[i+1]!=0 ){
10490 }else if( c=='b' ){
10492 }else if( c=='t' ){
10494 }else if( c=='n' ){
10496 }else if( c=='v' ){
10498 }else if( c=='f' ){
10500 }else if( c=='r' ){
10502 }else if( c=='"' ){
10504 }else if( c=='\'' ){
10506 }else if( c=='\\' ){
10508 }else if( c>='0' && c<='7' ){
10510 if( z[i+1]>='0' && z[i+1]<='7' ){
10512 c = (c<<3) + z[i] - '0';
10513 if( z[i+1]>='0' && z[i+1]<='7' ){
10515 c = (c<<3) + z[i] - '0';
10522 if( j<i ) z[j] = 0;
10526 ** Interpret zArg as either an integer or a boolean value. Return 1 or 0
10527 ** for TRUE and FALSE. Return the integer value if appropriate.
10529 static int booleanValue(const char *zArg){
10531 if( zArg[0]=='0' && zArg[1]=='x' ){
10532 for(i=2; hexDigitValue(zArg[i])>=0; i++){}
10534 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
10536 if( i>0 && zArg[i]==0 ) return (int)(integerValue(zArg) & 0xffffffff);
10537 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
10540 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
10543 utf8_printf(stderr, "ERROR: Not a boolean value: \"%s\". Assuming \"no\".\n",
10549 ** Set or clear a shell flag according to a boolean value.
10551 static void setOrClearFlag(ShellState *p, unsigned mFlag, const char *zArg){
10552 if( booleanValue(zArg) ){
10553 ShellSetFlag(p, mFlag);
10555 ShellClearFlag(p, mFlag);
10560 ** Close an output file, assuming it is not stderr or stdout
10562 static void output_file_close(FILE *f){
10563 if( f && f!=stdout && f!=stderr ) fclose(f);
10567 ** Try to open an output file. The names "stdout" and "stderr" are
10568 ** recognized and do the right thing. NULL is returned if the output
10569 ** filename is "off".
10571 static FILE *output_file_open(const char *zFile, int bTextMode){
10573 if( strcmp(zFile,"stdout")==0 ){
10575 }else if( strcmp(zFile, "stderr")==0 ){
10577 }else if( strcmp(zFile, "off")==0 ){
10580 f = fopen(zFile, bTextMode ? "w" : "wb");
10582 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
10588 #if !defined(SQLITE_UNTESTABLE)
10589 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
10591 ** A routine for handling output from sqlite3_trace().
10593 static int sql_trace_callback(
10599 FILE *f = (FILE*)pArg;
10600 UNUSED_PARAMETER(mType);
10601 UNUSED_PARAMETER(pP);
10603 const char *z = (const char*)pX;
10604 int i = strlen30(z);
10605 while( i>0 && z[i-1]==';' ){ i--; }
10606 utf8_printf(f, "%.*s;\n", i, z);
10614 ** A no-op routine that runs with the ".breakpoint" doc-command. This is
10615 ** a useful spot to set a debugger breakpoint.
10617 static void test_breakpoint(void){
10618 static int nCall = 0;
10623 ** An object used to read a CSV and other files for import.
10625 typedef struct ImportCtx ImportCtx;
10627 const char *zFile; /* Name of the input file */
10628 FILE *in; /* Read the CSV text from this input stream */
10629 char *z; /* Accumulated text for a field */
10630 int n; /* Number of bytes in z */
10631 int nAlloc; /* Space allocated for z[] */
10632 int nLine; /* Current line number */
10633 int bNotFirst; /* True if one or more bytes already read */
10634 int cTerm; /* Character that terminated the most recent field */
10635 int cColSep; /* The column separator character. (Usually ",") */
10636 int cRowSep; /* The row separator character. (Usually "\n") */
10639 /* Append a single byte to z[] */
10640 static void import_append_char(ImportCtx *p, int c){
10641 if( p->n+1>=p->nAlloc ){
10642 p->nAlloc += p->nAlloc + 100;
10643 p->z = sqlite3_realloc64(p->z, p->nAlloc);
10645 raw_printf(stderr, "out of memory\n");
10649 p->z[p->n++] = (char)c;
10652 /* Read a single field of CSV text. Compatible with rfc4180 and extended
10653 ** with the option of having a separator other than ",".
10655 ** + Input comes from p->in.
10656 ** + Store results in p->z of length p->n. Space to hold p->z comes
10657 ** from sqlite3_malloc64().
10658 ** + Use p->cSep as the column separator. The default is ",".
10659 ** + Use p->rSep as the row separator. The default is "\n".
10660 ** + Keep track of the line number in p->nLine.
10661 ** + Store the character that terminates the field in p->cTerm. Store
10662 ** EOF on end-of-file.
10663 ** + Report syntax errors on stderr
10665 static char *SQLITE_CDECL csv_read_one_field(ImportCtx *p){
10667 int cSep = p->cColSep;
10668 int rSep = p->cRowSep;
10671 if( c==EOF || seenInterrupt ){
10677 int startLine = p->nLine;
10682 if( c==rSep ) p->nLine++;
10689 if( (c==cSep && pc==cQuote)
10690 || (c==rSep && pc==cQuote)
10691 || (c==rSep && pc=='\r' && ppc==cQuote)
10692 || (c==EOF && pc==cQuote)
10694 do{ p->n--; }while( p->z[p->n]!=cQuote );
10698 if( pc==cQuote && c!='\r' ){
10699 utf8_printf(stderr, "%s:%d: unescaped %c character\n",
10700 p->zFile, p->nLine, cQuote);
10703 utf8_printf(stderr, "%s:%d: unterminated %c-quoted field\n",
10704 p->zFile, startLine, cQuote);
10708 import_append_char(p, c);
10713 /* If this is the first field being parsed and it begins with the
10714 ** UTF-8 BOM (0xEF BB BF) then skip the BOM */
10715 if( (c&0xff)==0xef && p->bNotFirst==0 ){
10716 import_append_char(p, c);
10718 if( (c&0xff)==0xbb ){
10719 import_append_char(p, c);
10721 if( (c&0xff)==0xbf ){
10724 return csv_read_one_field(p);
10728 while( c!=EOF && c!=cSep && c!=rSep ){
10729 import_append_char(p, c);
10734 if( p->n>0 && p->z[p->n-1]=='\r' ) p->n--;
10738 if( p->z ) p->z[p->n] = 0;
10743 /* Read a single field of ASCII delimited text.
10745 ** + Input comes from p->in.
10746 ** + Store results in p->z of length p->n. Space to hold p->z comes
10747 ** from sqlite3_malloc64().
10748 ** + Use p->cSep as the column separator. The default is "\x1F".
10749 ** + Use p->rSep as the row separator. The default is "\x1E".
10750 ** + Keep track of the row number in p->nLine.
10751 ** + Store the character that terminates the field in p->cTerm. Store
10752 ** EOF on end-of-file.
10753 ** + Report syntax errors on stderr
10755 static char *SQLITE_CDECL ascii_read_one_field(ImportCtx *p){
10757 int cSep = p->cColSep;
10758 int rSep = p->cRowSep;
10761 if( c==EOF || seenInterrupt ){
10765 while( c!=EOF && c!=cSep && c!=rSep ){
10766 import_append_char(p, c);
10773 if( p->z ) p->z[p->n] = 0;
10778 ** Try to transfer data for table zTable. If an error is seen while
10779 ** moving forward, try to go backwards. The backwards movement won't
10780 ** work for WITHOUT ROWID tables.
10782 static void tryToCloneData(
10787 sqlite3_stmt *pQuery = 0;
10788 sqlite3_stmt *pInsert = 0;
10793 int nTable = strlen30(zTable);
10796 const int spinRate = 10000;
10798 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\"", zTable);
10799 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10801 utf8_printf(stderr, "Error %d: %s on [%s]\n",
10802 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10804 goto end_data_xfer;
10806 n = sqlite3_column_count(pQuery);
10807 zInsert = sqlite3_malloc64(200 + nTable + n*3);
10809 raw_printf(stderr, "out of memory\n");
10810 goto end_data_xfer;
10812 sqlite3_snprintf(200+nTable,zInsert,
10813 "INSERT OR IGNORE INTO \"%s\" VALUES(?", zTable);
10814 i = strlen30(zInsert);
10815 for(j=1; j<n; j++){
10816 memcpy(zInsert+i, ",?", 2);
10819 memcpy(zInsert+i, ");", 3);
10820 rc = sqlite3_prepare_v2(newDb, zInsert, -1, &pInsert, 0);
10822 utf8_printf(stderr, "Error %d: %s on [%s]\n",
10823 sqlite3_extended_errcode(newDb), sqlite3_errmsg(newDb),
10825 goto end_data_xfer;
10827 for(k=0; k<2; k++){
10828 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10829 for(i=0; i<n; i++){
10830 switch( sqlite3_column_type(pQuery, i) ){
10831 case SQLITE_NULL: {
10832 sqlite3_bind_null(pInsert, i+1);
10835 case SQLITE_INTEGER: {
10836 sqlite3_bind_int64(pInsert, i+1, sqlite3_column_int64(pQuery,i));
10839 case SQLITE_FLOAT: {
10840 sqlite3_bind_double(pInsert, i+1, sqlite3_column_double(pQuery,i));
10843 case SQLITE_TEXT: {
10844 sqlite3_bind_text(pInsert, i+1,
10845 (const char*)sqlite3_column_text(pQuery,i),
10846 -1, SQLITE_STATIC);
10849 case SQLITE_BLOB: {
10850 sqlite3_bind_blob(pInsert, i+1, sqlite3_column_blob(pQuery,i),
10851 sqlite3_column_bytes(pQuery,i),
10857 rc = sqlite3_step(pInsert);
10858 if( rc!=SQLITE_OK && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
10859 utf8_printf(stderr, "Error %d: %s\n", sqlite3_extended_errcode(newDb),
10860 sqlite3_errmsg(newDb));
10862 sqlite3_reset(pInsert);
10864 if( (cnt%spinRate)==0 ){
10865 printf("%c\b", "|/-\\"[(cnt/spinRate)%4]);
10869 if( rc==SQLITE_DONE ) break;
10870 sqlite3_finalize(pQuery);
10871 sqlite3_free(zQuery);
10872 zQuery = sqlite3_mprintf("SELECT * FROM \"%w\" ORDER BY rowid DESC;",
10874 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10876 utf8_printf(stderr, "Warning: cannot step \"%s\" backwards", zTable);
10879 } /* End for(k=0...) */
10882 sqlite3_finalize(pQuery);
10883 sqlite3_finalize(pInsert);
10884 sqlite3_free(zQuery);
10885 sqlite3_free(zInsert);
10890 ** Try to transfer all rows of the schema that match zWhere. For
10891 ** each row, invoke xForEach() on the object defined by that row.
10892 ** If an error is encountered while moving forward through the
10893 ** sqlite_master table, try again moving backwards.
10895 static void tryToCloneSchema(
10898 const char *zWhere,
10899 void (*xForEach)(ShellState*,sqlite3*,const char*)
10901 sqlite3_stmt *pQuery = 0;
10904 const unsigned char *zName;
10905 const unsigned char *zSql;
10908 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
10909 " WHERE %s", zWhere);
10910 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10912 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
10913 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10915 goto end_schema_xfer;
10917 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10918 zName = sqlite3_column_text(pQuery, 0);
10919 zSql = sqlite3_column_text(pQuery, 1);
10920 printf("%s... ", zName); fflush(stdout);
10921 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
10923 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
10924 sqlite3_free(zErrMsg);
10928 xForEach(p, newDb, (const char*)zName);
10932 if( rc!=SQLITE_DONE ){
10933 sqlite3_finalize(pQuery);
10934 sqlite3_free(zQuery);
10935 zQuery = sqlite3_mprintf("SELECT name, sql FROM sqlite_master"
10936 " WHERE %s ORDER BY rowid DESC", zWhere);
10937 rc = sqlite3_prepare_v2(p->db, zQuery, -1, &pQuery, 0);
10939 utf8_printf(stderr, "Error: (%d) %s on [%s]\n",
10940 sqlite3_extended_errcode(p->db), sqlite3_errmsg(p->db),
10942 goto end_schema_xfer;
10944 while( (rc = sqlite3_step(pQuery))==SQLITE_ROW ){
10945 zName = sqlite3_column_text(pQuery, 0);
10946 zSql = sqlite3_column_text(pQuery, 1);
10947 printf("%s... ", zName); fflush(stdout);
10948 sqlite3_exec(newDb, (const char*)zSql, 0, 0, &zErrMsg);
10950 utf8_printf(stderr, "Error: %s\nSQL: [%s]\n", zErrMsg, zSql);
10951 sqlite3_free(zErrMsg);
10955 xForEach(p, newDb, (const char*)zName);
10961 sqlite3_finalize(pQuery);
10962 sqlite3_free(zQuery);
10966 ** Open a new database file named "zNewDb". Try to recover as much information
10967 ** as possible out of the main database (which might be corrupt) and write it
10970 static void tryToClone(ShellState *p, const char *zNewDb){
10972 sqlite3 *newDb = 0;
10973 if( access(zNewDb,0)==0 ){
10974 utf8_printf(stderr, "File \"%s\" already exists.\n", zNewDb);
10977 rc = sqlite3_open(zNewDb, &newDb);
10979 utf8_printf(stderr, "Cannot create output database: %s\n",
10980 sqlite3_errmsg(newDb));
10982 sqlite3_exec(p->db, "PRAGMA writable_schema=ON;", 0, 0, 0);
10983 sqlite3_exec(newDb, "BEGIN EXCLUSIVE;", 0, 0, 0);
10984 tryToCloneSchema(p, newDb, "type='table'", tryToCloneData);
10985 tryToCloneSchema(p, newDb, "type!='table'", 0);
10986 sqlite3_exec(newDb, "COMMIT;", 0, 0, 0);
10987 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
10989 sqlite3_close(newDb);
10993 ** Change the output file back to stdout.
10995 ** If the p->doXdgOpen flag is set, that means the output was being
10996 ** redirected to a temporary file named by p->zTempFile. In that case,
10997 ** launch start/open/xdg-open on that temporary file.
10999 static void output_reset(ShellState *p){
11000 if( p->outfile[0]=='|' ){
11001 #ifndef SQLITE_OMIT_POPEN
11005 output_file_close(p->out);
11006 if( p->doXdgOpen ){
11007 const char *zXdgOpenCmd =
11008 #if defined(_WIN32)
11010 #elif defined(__APPLE__)
11016 zCmd = sqlite3_mprintf("%s %s", zXdgOpenCmd, p->zTempFile);
11017 if( system(zCmd) ){
11018 utf8_printf(stderr, "Failed: [%s]\n", zCmd);
11020 sqlite3_free(zCmd);
11030 ** Run an SQL command and return the single integer result.
11032 static int db_int(ShellState *p, const char *zSql){
11033 sqlite3_stmt *pStmt;
11035 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
11036 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
11037 res = sqlite3_column_int(pStmt,0);
11039 sqlite3_finalize(pStmt);
11044 ** Convert a 2-byte or 4-byte big-endian integer into a native integer
11046 static unsigned int get2byteInt(unsigned char *a){
11047 return (a[0]<<8) + a[1];
11049 static unsigned int get4byteInt(unsigned char *a){
11050 return (a[0]<<24) + (a[1]<<16) + (a[2]<<8) + a[3];
11054 ** Implementation of the ".info" command.
11056 ** Return 1 on error, 2 to exit, and 0 otherwise.
11058 static int shell_dbinfo_command(ShellState *p, int nArg, char **azArg){
11059 static const struct { const char *zName; int ofst; } aField[] = {
11060 { "file change counter:", 24 },
11061 { "database page count:", 28 },
11062 { "freelist page count:", 36 },
11063 { "schema cookie:", 40 },
11064 { "schema format:", 44 },
11065 { "default cache size:", 48 },
11066 { "autovacuum top root:", 52 },
11067 { "incremental vacuum:", 64 },
11068 { "text encoding:", 56 },
11069 { "user version:", 60 },
11070 { "application id:", 68 },
11071 { "software version:", 96 },
11073 static const struct { const char *zName; const char *zSql; } aQuery[] = {
11074 { "number of tables:",
11075 "SELECT count(*) FROM %s WHERE type='table'" },
11076 { "number of indexes:",
11077 "SELECT count(*) FROM %s WHERE type='index'" },
11078 { "number of triggers:",
11079 "SELECT count(*) FROM %s WHERE type='trigger'" },
11080 { "number of views:",
11081 "SELECT count(*) FROM %s WHERE type='view'" },
11083 "SELECT total(length(sql)) FROM %s" },
11087 char *zDb = nArg>=2 ? azArg[1] : "main";
11088 sqlite3_stmt *pStmt = 0;
11089 unsigned char aHdr[100];
11091 if( p->db==0 ) return 1;
11092 sqlite3_prepare_v2(p->db,"SELECT data FROM sqlite_dbpage(?1) WHERE pgno=1",
11094 sqlite3_bind_text(pStmt, 1, zDb, -1, SQLITE_STATIC);
11095 if( sqlite3_step(pStmt)==SQLITE_ROW
11096 && sqlite3_column_bytes(pStmt,0)>100
11098 memcpy(aHdr, sqlite3_column_blob(pStmt,0), 100);
11099 sqlite3_finalize(pStmt);
11101 raw_printf(stderr, "unable to read database header\n");
11102 sqlite3_finalize(pStmt);
11105 i = get2byteInt(aHdr+16);
11106 if( i==1 ) i = 65536;
11107 utf8_printf(p->out, "%-20s %d\n", "database page size:", i);
11108 utf8_printf(p->out, "%-20s %d\n", "write format:", aHdr[18]);
11109 utf8_printf(p->out, "%-20s %d\n", "read format:", aHdr[19]);
11110 utf8_printf(p->out, "%-20s %d\n", "reserved bytes:", aHdr[20]);
11111 for(i=0; i<ArraySize(aField); i++){
11112 int ofst = aField[i].ofst;
11113 unsigned int val = get4byteInt(aHdr + ofst);
11114 utf8_printf(p->out, "%-20s %u", aField[i].zName, val);
11117 if( val==1 ) raw_printf(p->out, " (utf8)");
11118 if( val==2 ) raw_printf(p->out, " (utf16le)");
11119 if( val==3 ) raw_printf(p->out, " (utf16be)");
11122 raw_printf(p->out, "\n");
11125 zSchemaTab = sqlite3_mprintf("main.sqlite_master");
11126 }else if( strcmp(zDb,"temp")==0 ){
11127 zSchemaTab = sqlite3_mprintf("%s", "sqlite_temp_master");
11129 zSchemaTab = sqlite3_mprintf("\"%w\".sqlite_master", zDb);
11131 for(i=0; i<ArraySize(aQuery); i++){
11132 char *zSql = sqlite3_mprintf(aQuery[i].zSql, zSchemaTab);
11133 int val = db_int(p, zSql);
11134 sqlite3_free(zSql);
11135 utf8_printf(p->out, "%-20s %d\n", aQuery[i].zName, val);
11137 sqlite3_free(zSchemaTab);
11142 ** Print the current sqlite3_errmsg() value to stderr and return 1.
11144 static int shellDatabaseError(sqlite3 *db){
11145 const char *zErr = sqlite3_errmsg(db);
11146 utf8_printf(stderr, "Error: %s\n", zErr);
11151 ** Print an out-of-memory message to stderr and return 1.
11153 static int shellNomemError(void){
11154 raw_printf(stderr, "Error: out of memory\n");
11159 ** Compare the pattern in zGlob[] against the text in z[]. Return TRUE
11160 ** if they match and FALSE (0) if they do not match.
11164 ** '*' Matches any sequence of zero or more characters.
11166 ** '?' Matches exactly one character.
11168 ** [...] Matches one character from the enclosed list of
11171 ** [^...] Matches one character not in the enclosed list.
11173 ** '#' Matches any sequence of one or more digits with an
11174 ** optional + or - sign in front
11176 ** ' ' Any span of whitespace matches any other span of
11179 ** Extra whitespace at the end of z[] is ignored.
11181 static int testcase_glob(const char *zGlob, const char *z){
11186 while( (c = (*(zGlob++)))!=0 ){
11188 if( !IsSpace(*z) ) return 0;
11189 while( IsSpace(*zGlob) ) zGlob++;
11190 while( IsSpace(*z) ) z++;
11191 }else if( c=='*' ){
11192 while( (c=(*(zGlob++))) == '*' || c=='?' ){
11193 if( c=='?' && (*(z++))==0 ) return 0;
11197 }else if( c=='[' ){
11198 while( *z && testcase_glob(zGlob-1,z)==0 ){
11203 while( (c2 = (*(z++)))!=0 ){
11206 if( c2==0 ) return 0;
11208 if( testcase_glob(zGlob,z) ) return 1;
11211 }else if( c=='?' ){
11212 if( (*(z++))==0 ) return 0;
11213 }else if( c=='[' ){
11218 if( c==0 ) return 0;
11225 if( c==']' ) seen = 1;
11228 while( c2 && c2!=']' ){
11229 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
11231 if( c>=prior_c && c<=c2 ) seen = 1;
11241 if( c2==0 || (seen ^ invert)==0 ) return 0;
11242 }else if( c=='#' ){
11243 if( (z[0]=='-' || z[0]=='+') && IsDigit(z[1]) ) z++;
11244 if( !IsDigit(z[0]) ) return 0;
11246 while( IsDigit(z[0]) ){ z++; }
11248 if( c!=(*(z++)) ) return 0;
11251 while( IsSpace(*z) ){ z++; }
11257 ** Compare the string as a command-line option with either one or two
11258 ** initial "-" characters.
11260 static int optionMatch(const char *zStr, const char *zOpt){
11261 if( zStr[0]!='-' ) return 0;
11263 if( zStr[0]=='-' ) zStr++;
11264 return strcmp(zStr, zOpt)==0;
11270 int shellDeleteFile(const char *zFilename){
11273 wchar_t *z = sqlite3_win32_utf8_to_unicode(zFilename);
11277 rc = unlink(zFilename);
11283 ** Try to delete the temporary file (if there is one) and free the
11284 ** memory used to hold the name of the temp file.
11286 static void clearTempFile(ShellState *p){
11287 if( p->zTempFile==0 ) return;
11288 if( p->doXdgOpen ) return;
11289 if( shellDeleteFile(p->zTempFile) ) return;
11290 sqlite3_free(p->zTempFile);
11295 ** Create a new temp file name with the given suffix.
11297 static void newTempFile(ShellState *p, const char *zSuffix){
11299 sqlite3_free(p->zTempFile);
11302 sqlite3_file_control(p->db, 0, SQLITE_FCNTL_TEMPFILENAME, &p->zTempFile);
11304 if( p->zTempFile==0 ){
11306 sqlite3_randomness(sizeof(r), &r);
11307 p->zTempFile = sqlite3_mprintf("temp%llx.%s", r, zSuffix);
11309 p->zTempFile = sqlite3_mprintf("%z.%s", p->zTempFile, zSuffix);
11311 if( p->zTempFile==0 ){
11312 raw_printf(stderr, "out of memory\n");
11319 ** The implementation of SQL scalar function fkey_collate_clause(), used
11320 ** by the ".lint fkey-indexes" command. This scalar function is always
11321 ** called with four arguments - the parent table name, the parent column name,
11322 ** the child table name and the child column name.
11324 ** fkey_collate_clause('parent-tab', 'parent-col', 'child-tab', 'child-col')
11326 ** If either of the named tables or columns do not exist, this function
11327 ** returns an empty string. An empty string is also returned if both tables
11328 ** and columns exist but have the same default collation sequence. Or,
11329 ** if both exist but the default collation sequences are different, this
11330 ** function returns the string " COLLATE <parent-collation>", where
11331 ** <parent-collation> is the default collation sequence of the parent column.
11333 static void shellFkeyCollateClause(
11334 sqlite3_context *pCtx,
11336 sqlite3_value **apVal
11338 sqlite3 *db = sqlite3_context_db_handle(pCtx);
11339 const char *zParent;
11340 const char *zParentCol;
11341 const char *zParentSeq;
11342 const char *zChild;
11343 const char *zChildCol;
11344 const char *zChildSeq = 0; /* Initialize to avoid false-positive warning */
11348 zParent = (const char*)sqlite3_value_text(apVal[0]);
11349 zParentCol = (const char*)sqlite3_value_text(apVal[1]);
11350 zChild = (const char*)sqlite3_value_text(apVal[2]);
11351 zChildCol = (const char*)sqlite3_value_text(apVal[3]);
11353 sqlite3_result_text(pCtx, "", -1, SQLITE_STATIC);
11354 rc = sqlite3_table_column_metadata(
11355 db, "main", zParent, zParentCol, 0, &zParentSeq, 0, 0, 0
11357 if( rc==SQLITE_OK ){
11358 rc = sqlite3_table_column_metadata(
11359 db, "main", zChild, zChildCol, 0, &zChildSeq, 0, 0, 0
11363 if( rc==SQLITE_OK && sqlite3_stricmp(zParentSeq, zChildSeq) ){
11364 char *z = sqlite3_mprintf(" COLLATE %s", zParentSeq);
11365 sqlite3_result_text(pCtx, z, -1, SQLITE_TRANSIENT);
11372 ** The implementation of dot-command ".lint fkey-indexes".
11374 static int lintFkeyIndexes(
11375 ShellState *pState, /* Current shell tool state */
11376 char **azArg, /* Array of arguments passed to dot command */
11377 int nArg /* Number of entries in azArg[] */
11379 sqlite3 *db = pState->db; /* Database handle to query "main" db of */
11380 FILE *out = pState->out; /* Stream to write non-error output to */
11381 int bVerbose = 0; /* If -verbose is present */
11382 int bGroupByParent = 0; /* If -groupbyparent is present */
11383 int i; /* To iterate through azArg[] */
11384 const char *zIndent = ""; /* How much to indent CREATE INDEX by */
11385 int rc; /* Return code */
11386 sqlite3_stmt *pSql = 0; /* Compiled version of SQL statement below */
11389 ** This SELECT statement returns one row for each foreign key constraint
11390 ** in the schema of the main database. The column values are:
11392 ** 0. The text of an SQL statement similar to:
11394 ** "EXPLAIN QUERY PLAN SELECT 1 FROM child_table WHERE child_key=?"
11396 ** This SELECT is similar to the one that the foreign keys implementation
11397 ** needs to run internally on child tables. If there is an index that can
11398 ** be used to optimize this query, then it can also be used by the FK
11399 ** implementation to optimize DELETE or UPDATE statements on the parent
11402 ** 1. A GLOB pattern suitable for sqlite3_strglob(). If the plan output by
11403 ** the EXPLAIN QUERY PLAN command matches this pattern, then the schema
11404 ** contains an index that can be used to optimize the query.
11406 ** 2. Human readable text that describes the child table and columns. e.g.
11408 ** "child_table(child_key1, child_key2)"
11410 ** 3. Human readable text that describes the parent table and columns. e.g.
11412 ** "parent_table(parent_key1, parent_key2)"
11414 ** 4. A full CREATE INDEX statement for an index that could be used to
11415 ** optimize DELETE or UPDATE statements on the parent table. e.g.
11417 ** "CREATE INDEX child_table_child_key ON child_table(child_key)"
11419 ** 5. The name of the parent table.
11421 ** These six values are used by the C logic below to generate the report.
11425 " 'EXPLAIN QUERY PLAN SELECT 1 FROM ' || quote(s.name) || ' WHERE '"
11426 " || group_concat(quote(s.name) || '.' || quote(f.[from]) || '=?' "
11427 " || fkey_collate_clause("
11428 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]),' AND ')"
11430 " 'SEARCH TABLE ' || s.name || ' USING COVERING INDEX*('"
11431 " || group_concat('*=?', ' AND ') || ')'"
11433 " s.name || '(' || group_concat(f.[from], ', ') || ')'"
11435 " f.[table] || '(' || group_concat(COALESCE(f.[to], p.[name])) || ')'"
11437 " 'CREATE INDEX ' || quote(s.name ||'_'|| group_concat(f.[from], '_'))"
11438 " || ' ON ' || quote(s.name) || '('"
11439 " || group_concat(quote(f.[from]) ||"
11440 " fkey_collate_clause("
11441 " f.[table], COALESCE(f.[to], p.[name]), s.name, f.[from]), ', ')"
11445 "FROM sqlite_master AS s, pragma_foreign_key_list(s.name) AS f "
11446 "LEFT JOIN pragma_table_info AS p ON (pk-1=seq AND p.arg=f.[table]) "
11447 "GROUP BY s.name, f.id "
11448 "ORDER BY (CASE WHEN ? THEN f.[table] ELSE s.name END)"
11450 const char *zGlobIPK = "SEARCH TABLE * USING INTEGER PRIMARY KEY (rowid=?)";
11452 for(i=2; i<nArg; i++){
11453 int n = strlen30(azArg[i]);
11454 if( n>1 && sqlite3_strnicmp("-verbose", azArg[i], n)==0 ){
11457 else if( n>1 && sqlite3_strnicmp("-groupbyparent", azArg[i], n)==0 ){
11458 bGroupByParent = 1;
11462 raw_printf(stderr, "Usage: %s %s ?-verbose? ?-groupbyparent?\n",
11465 return SQLITE_ERROR;
11469 /* Register the fkey_collate_clause() SQL function */
11470 rc = sqlite3_create_function(db, "fkey_collate_clause", 4, SQLITE_UTF8,
11471 0, shellFkeyCollateClause, 0, 0
11475 if( rc==SQLITE_OK ){
11476 rc = sqlite3_prepare_v2(db, zSql, -1, &pSql, 0);
11478 if( rc==SQLITE_OK ){
11479 sqlite3_bind_int(pSql, 1, bGroupByParent);
11482 if( rc==SQLITE_OK ){
11485 while( SQLITE_ROW==sqlite3_step(pSql) ){
11487 sqlite3_stmt *pExplain = 0;
11488 const char *zEQP = (const char*)sqlite3_column_text(pSql, 0);
11489 const char *zGlob = (const char*)sqlite3_column_text(pSql, 1);
11490 const char *zFrom = (const char*)sqlite3_column_text(pSql, 2);
11491 const char *zTarget = (const char*)sqlite3_column_text(pSql, 3);
11492 const char *zCI = (const char*)sqlite3_column_text(pSql, 4);
11493 const char *zParent = (const char*)sqlite3_column_text(pSql, 5);
11495 rc = sqlite3_prepare_v2(db, zEQP, -1, &pExplain, 0);
11496 if( rc!=SQLITE_OK ) break;
11497 if( SQLITE_ROW==sqlite3_step(pExplain) ){
11498 const char *zPlan = (const char*)sqlite3_column_text(pExplain, 3);
11500 0==sqlite3_strglob(zGlob, zPlan)
11501 || 0==sqlite3_strglob(zGlobIPK, zPlan)
11504 rc = sqlite3_finalize(pExplain);
11505 if( rc!=SQLITE_OK ) break;
11508 raw_printf(stderr, "Error: internal error");
11512 && (bVerbose || res==0)
11513 && (zPrev==0 || sqlite3_stricmp(zParent, zPrev))
11515 raw_printf(out, "-- Parent table %s\n", zParent);
11516 sqlite3_free(zPrev);
11517 zPrev = sqlite3_mprintf("%s", zParent);
11521 raw_printf(out, "%s%s --> %s\n", zIndent, zCI, zTarget);
11522 }else if( bVerbose ){
11523 raw_printf(out, "%s/* no extra indexes required for %s -> %s */\n",
11524 zIndent, zFrom, zTarget
11529 sqlite3_free(zPrev);
11531 if( rc!=SQLITE_OK ){
11532 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11535 rc2 = sqlite3_finalize(pSql);
11536 if( rc==SQLITE_OK && rc2!=SQLITE_OK ){
11538 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11541 raw_printf(stderr, "%s\n", sqlite3_errmsg(db));
11548 ** Implementation of ".lint" dot command.
11550 static int lintDotCommand(
11551 ShellState *pState, /* Current shell tool state */
11552 char **azArg, /* Array of arguments passed to dot command */
11553 int nArg /* Number of entries in azArg[] */
11556 n = (nArg>=2 ? strlen30(azArg[1]) : 0);
11557 if( n<1 || sqlite3_strnicmp(azArg[1], "fkey-indexes", n) ) goto usage;
11558 return lintFkeyIndexes(pState, azArg, nArg);
11561 raw_printf(stderr, "Usage %s sub-command ?switches...?\n", azArg[0]);
11562 raw_printf(stderr, "Where sub-commands are:\n");
11563 raw_printf(stderr, " fkey-indexes\n");
11564 return SQLITE_ERROR;
11567 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
11568 /*********************************************************************************
11569 ** The ".archive" or ".ar" command.
11571 static void shellPrepare(
11575 sqlite3_stmt **ppStmt
11578 if( *pRc==SQLITE_OK ){
11579 int rc = sqlite3_prepare_v2(db, zSql, -1, ppStmt, 0);
11580 if( rc!=SQLITE_OK ){
11581 raw_printf(stderr, "sql error: %s (%d)\n",
11582 sqlite3_errmsg(db), sqlite3_errcode(db)
11589 static void shellPreparePrintf(
11592 sqlite3_stmt **ppStmt,
11597 if( *pRc==SQLITE_OK ){
11600 va_start(ap, zFmt);
11601 z = sqlite3_vmprintf(zFmt, ap);
11603 *pRc = SQLITE_NOMEM;
11605 shellPrepare(db, pRc, z, ppStmt);
11611 static void shellFinalize(
11613 sqlite3_stmt *pStmt
11616 sqlite3 *db = sqlite3_db_handle(pStmt);
11617 int rc = sqlite3_finalize(pStmt);
11618 if( *pRc==SQLITE_OK ){
11619 if( rc!=SQLITE_OK ){
11620 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
11627 static void shellReset(
11629 sqlite3_stmt *pStmt
11631 int rc = sqlite3_reset(pStmt);
11632 if( *pRc==SQLITE_OK ){
11633 if( rc!=SQLITE_OK ){
11634 sqlite3 *db = sqlite3_db_handle(pStmt);
11635 raw_printf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
11641 ** Structure representing a single ".ar" command.
11643 typedef struct ArCommand ArCommand;
11645 u8 eCmd; /* An AR_CMD_* value */
11646 u8 bVerbose; /* True if --verbose */
11647 u8 bZip; /* True if the archive is a ZIP */
11648 u8 bDryRun; /* True if --dry-run */
11649 u8 bAppend; /* True if --append */
11650 int nArg; /* Number of command arguments */
11651 char *zSrcTable; /* "sqlar", "zipfile($file)" or "zip" */
11652 const char *zFile; /* --file argument, or NULL */
11653 const char *zDir; /* --directory argument, or NULL */
11654 char **azArg; /* Array of command arguments */
11655 ShellState *p; /* Shell state */
11656 sqlite3 *db; /* Database containing the archive */
11660 ** Print a usage message for the .ar command to stderr and return SQLITE_ERROR.
11662 static int arUsage(FILE *f){
11665 "Usage: .ar [OPTION...] [FILE...]\n"
11666 "The .ar command manages sqlar archives.\n"
11669 " .ar -cf archive.sar foo bar # Create archive.sar from files foo and bar\n"
11670 " .ar -tf archive.sar # List members of archive.sar\n"
11671 " .ar -xvf archive.sar # Verbosely extract files from archive.sar\n"
11673 "Each command line must feature exactly one command option:\n"
11674 " -c, --create Create a new archive\n"
11675 " -u, --update Update or add files to an existing archive\n"
11676 " -t, --list List contents of archive\n"
11677 " -x, --extract Extract files from archive\n"
11679 "And zero or more optional options:\n"
11680 " -v, --verbose Print each filename as it is processed\n"
11681 " -f FILE, --file FILE Operate on archive FILE (default is current db)\n"
11682 " -a FILE, --append FILE Operate on FILE opened using the apndvfs VFS\n"
11683 " -C DIR, --directory DIR Change to directory DIR to read/extract files\n"
11684 " -n, --dryrun Show the SQL that would have occurred\n"
11686 "See also: http://sqlite.org/cli.html#sqlar_archive_support\n"
11689 return SQLITE_ERROR;
11693 ** Print an error message for the .ar command to stderr and return
11696 static int arErrorMsg(const char *zFmt, ...){
11699 va_start(ap, zFmt);
11700 z = sqlite3_vmprintf(zFmt, ap);
11702 raw_printf(stderr, "Error: %s (try \".ar --help\")\n", z);
11704 return SQLITE_ERROR;
11708 ** Values for ArCommand.eCmd.
11710 #define AR_CMD_CREATE 1
11711 #define AR_CMD_EXTRACT 2
11712 #define AR_CMD_LIST 3
11713 #define AR_CMD_UPDATE 4
11714 #define AR_CMD_HELP 5
11717 ** Other (non-command) switches.
11719 #define AR_SWITCH_VERBOSE 6
11720 #define AR_SWITCH_FILE 7
11721 #define AR_SWITCH_DIRECTORY 8
11722 #define AR_SWITCH_APPEND 9
11723 #define AR_SWITCH_DRYRUN 10
11725 static int arProcessSwitch(ArCommand *pAr, int eSwitch, const char *zArg){
11727 case AR_CMD_CREATE:
11728 case AR_CMD_EXTRACT:
11730 case AR_CMD_UPDATE:
11733 return arErrorMsg("multiple command options");
11735 pAr->eCmd = eSwitch;
11738 case AR_SWITCH_DRYRUN:
11741 case AR_SWITCH_VERBOSE:
11744 case AR_SWITCH_APPEND:
11746 /* Fall thru into --file */
11747 case AR_SWITCH_FILE:
11750 case AR_SWITCH_DIRECTORY:
11759 ** Parse the command line for an ".ar" command. The results are written into
11760 ** structure (*pAr). SQLITE_OK is returned if the command line is parsed
11761 ** successfully, otherwise an error message is written to stderr and
11762 ** SQLITE_ERROR returned.
11764 static int arParseCommand(
11765 char **azArg, /* Array of arguments passed to dot command */
11766 int nArg, /* Number of entries in azArg[] */
11767 ArCommand *pAr /* Populate this object */
11775 { "create", 'c', AR_CMD_CREATE, 0 },
11776 { "extract", 'x', AR_CMD_EXTRACT, 0 },
11777 { "list", 't', AR_CMD_LIST, 0 },
11778 { "update", 'u', AR_CMD_UPDATE, 0 },
11779 { "help", 'h', AR_CMD_HELP, 0 },
11780 { "verbose", 'v', AR_SWITCH_VERBOSE, 0 },
11781 { "file", 'f', AR_SWITCH_FILE, 1 },
11782 { "append", 'a', AR_SWITCH_APPEND, 1 },
11783 { "directory", 'C', AR_SWITCH_DIRECTORY, 1 },
11784 { "dryrun", 'n', AR_SWITCH_DRYRUN, 0 },
11786 int nSwitch = sizeof(aSwitch) / sizeof(struct ArSwitch);
11787 struct ArSwitch *pEnd = &aSwitch[nSwitch];
11790 return arUsage(stderr);
11792 char *z = azArg[1];
11793 memset(pAr, 0, sizeof(ArCommand));
11796 /* Traditional style [tar] invocation */
11799 for(i=0; z[i]; i++){
11800 const char *zArg = 0;
11801 struct ArSwitch *pOpt;
11802 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11803 if( z[i]==pOpt->cShort ) break;
11806 return arErrorMsg("unrecognized option: %c", z[i]);
11810 return arErrorMsg("option requires an argument: %c",z[i]);
11812 zArg = azArg[iArg++];
11814 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
11816 pAr->nArg = nArg-iArg;
11818 pAr->azArg = &azArg[iArg];
11821 /* Non-traditional invocation */
11823 for(iArg=1; iArg<nArg; iArg++){
11827 /* All remaining command line words are command arguments. */
11828 pAr->azArg = &azArg[iArg];
11829 pAr->nArg = nArg-iArg;
11836 /* One or more short options */
11837 for(i=1; i<n; i++){
11838 const char *zArg = 0;
11839 struct ArSwitch *pOpt;
11840 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11841 if( z[i]==pOpt->cShort ) break;
11844 return arErrorMsg("unrecognized option: %c\n", z[i]);
11851 if( iArg>=(nArg-1) ){
11852 return arErrorMsg("option requires an argument: %c\n",z[i]);
11854 zArg = azArg[++iArg];
11857 if( arProcessSwitch(pAr, pOpt->eSwitch, zArg) ) return SQLITE_ERROR;
11859 }else if( z[2]=='\0' ){
11860 /* A -- option, indicating that all remaining command line words
11861 ** are command arguments. */
11862 pAr->azArg = &azArg[iArg+1];
11863 pAr->nArg = nArg-iArg-1;
11866 /* A long option */
11867 const char *zArg = 0; /* Argument for option, if any */
11868 struct ArSwitch *pMatch = 0; /* Matching option */
11869 struct ArSwitch *pOpt; /* Iterator */
11870 for(pOpt=&aSwitch[0]; pOpt<pEnd; pOpt++){
11871 const char *zLong = pOpt->zLong;
11872 if( (n-2)<=strlen30(zLong) && 0==memcmp(&z[2], zLong, n-2) ){
11874 return arErrorMsg("ambiguous option: %s",z);
11882 return arErrorMsg("unrecognized option: %s", z);
11884 if( pMatch->bArg ){
11885 if( iArg>=(nArg-1) ){
11886 return arErrorMsg("option requires an argument: %s", z);
11888 zArg = azArg[++iArg];
11890 if( arProcessSwitch(pAr, pMatch->eSwitch, zArg) ) return SQLITE_ERROR;
11900 ** This function assumes that all arguments within the ArCommand.azArg[]
11901 ** array refer to archive members, as for the --extract or --list commands.
11902 ** It checks that each of them are present. If any specified file is not
11903 ** present in the archive, an error is printed to stderr and an error
11904 ** code returned. Otherwise, if all specified arguments are present in
11905 ** the archive, SQLITE_OK is returned.
11907 ** This function strips any trailing '/' characters from each argument.
11908 ** This is consistent with the way the [tar] command seems to work on
11911 static int arCheckEntries(ArCommand *pAr){
11912 int rc = SQLITE_OK;
11915 sqlite3_stmt *pTest = 0;
11917 shellPreparePrintf(pAr->db, &rc, &pTest,
11918 "SELECT name FROM %s WHERE name=$name",
11921 j = sqlite3_bind_parameter_index(pTest, "$name");
11922 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
11923 char *z = pAr->azArg[i];
11924 int n = strlen30(z);
11926 while( n>0 && z[n-1]=='/' ) n--;
11928 sqlite3_bind_text(pTest, j, z, -1, SQLITE_STATIC);
11929 if( SQLITE_ROW==sqlite3_step(pTest) ){
11932 shellReset(&rc, pTest);
11933 if( rc==SQLITE_OK && bOk==0 ){
11934 utf8_printf(stderr, "not found in archive: %s\n", z);
11938 shellFinalize(&rc, pTest);
11944 ** Format a WHERE clause that can be used against the "sqlar" table to
11945 ** identify all archive members that match the command arguments held
11946 ** in (*pAr). Leave this WHERE clause in (*pzWhere) before returning.
11947 ** The caller is responsible for eventually calling sqlite3_free() on
11948 ** any non-NULL (*pzWhere) value.
11950 static void arWhereClause(
11953 char **pzWhere /* OUT: New WHERE clause */
11956 if( *pRc==SQLITE_OK ){
11957 if( pAr->nArg==0 ){
11958 zWhere = sqlite3_mprintf("1");
11961 const char *zSep = "";
11962 for(i=0; i<pAr->nArg; i++){
11963 const char *z = pAr->azArg[i];
11964 zWhere = sqlite3_mprintf(
11965 "%z%s name = '%q' OR substr(name,1,%d) = '%q/'",
11966 zWhere, zSep, z, strlen30(z)+1, z
11969 *pRc = SQLITE_NOMEM;
11980 ** Implementation of .ar "lisT" command.
11982 static int arListCommand(ArCommand *pAr){
11983 const char *zSql = "SELECT %s FROM %s WHERE %s";
11984 const char *azCols[] = {
11986 "lsmode(mode), sz, datetime(mtime, 'unixepoch'), name"
11990 sqlite3_stmt *pSql = 0;
11993 rc = arCheckEntries(pAr);
11994 arWhereClause(&rc, pAr, &zWhere);
11996 shellPreparePrintf(pAr->db, &rc, &pSql, zSql, azCols[pAr->bVerbose],
11997 pAr->zSrcTable, zWhere);
11998 if( pAr->bDryRun ){
11999 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12001 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12002 if( pAr->bVerbose ){
12003 utf8_printf(pAr->p->out, "%s % 10d %s %s\n",
12004 sqlite3_column_text(pSql, 0),
12005 sqlite3_column_int(pSql, 1),
12006 sqlite3_column_text(pSql, 2),
12007 sqlite3_column_text(pSql, 3)
12010 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12014 shellFinalize(&rc, pSql);
12020 ** Implementation of .ar "eXtract" command.
12022 static int arExtractCommand(ArCommand *pAr){
12023 const char *zSql1 =
12026 " writefile(($dir || name), %s, mode, mtime) "
12027 "FROM %s WHERE (%s) AND (data IS NULL OR $dirOnly = 0)";
12029 const char *azExtraArg[] = {
12030 "sqlar_uncompress(data, sz)",
12034 sqlite3_stmt *pSql = 0;
12035 int rc = SQLITE_OK;
12040 /* If arguments are specified, check that they actually exist within
12041 ** the archive before proceeding. And formulate a WHERE clause to
12043 rc = arCheckEntries(pAr);
12044 arWhereClause(&rc, pAr, &zWhere);
12046 if( rc==SQLITE_OK ){
12048 zDir = sqlite3_mprintf("%s/", pAr->zDir);
12050 zDir = sqlite3_mprintf("");
12052 if( zDir==0 ) rc = SQLITE_NOMEM;
12055 shellPreparePrintf(pAr->db, &rc, &pSql, zSql1,
12056 azExtraArg[pAr->bZip], pAr->zSrcTable, zWhere
12059 if( rc==SQLITE_OK ){
12060 j = sqlite3_bind_parameter_index(pSql, "$dir");
12061 sqlite3_bind_text(pSql, j, zDir, -1, SQLITE_STATIC);
12063 /* Run the SELECT statement twice. The first time, writefile() is called
12064 ** for all archive members that should be extracted. The second time,
12065 ** only for the directories. This is because the timestamps for
12066 ** extracted directories must be reset after they are populated (as
12067 ** populating them changes the timestamp). */
12068 for(i=0; i<2; i++){
12069 j = sqlite3_bind_parameter_index(pSql, "$dirOnly");
12070 sqlite3_bind_int(pSql, j, i);
12071 if( pAr->bDryRun ){
12072 utf8_printf(pAr->p->out, "%s\n", sqlite3_sql(pSql));
12074 while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pSql) ){
12075 if( i==0 && pAr->bVerbose ){
12076 utf8_printf(pAr->p->out, "%s\n", sqlite3_column_text(pSql, 0));
12080 shellReset(&rc, pSql);
12082 shellFinalize(&rc, pSql);
12085 sqlite3_free(zDir);
12086 sqlite3_free(zWhere);
12091 ** Run the SQL statement in zSql. Or if doing a --dryrun, merely print it out.
12093 static int arExecSql(ArCommand *pAr, const char *zSql){
12095 if( pAr->bDryRun ){
12096 utf8_printf(pAr->p->out, "%s\n", zSql);
12100 rc = sqlite3_exec(pAr->db, zSql, 0, 0, &zErr);
12102 utf8_printf(stdout, "ERROR: %s\n", zErr);
12103 sqlite3_free(zErr);
12111 ** Implementation of .ar "create" and "update" commands.
12113 ** Create the "sqlar" table in the database if it does not already exist.
12114 ** Then add each file in the azFile[] array to the archive. Directories
12115 ** are added recursively. If argument bVerbose is non-zero, a message is
12116 ** printed on stdout for each file archived.
12118 ** The create command is the same as update, except that it drops
12119 ** any existing "sqlar" table before beginning.
12121 static int arCreateOrUpdateCommand(
12122 ArCommand *pAr, /* Command arguments and options */
12123 int bUpdate /* true for a --create. false for --update */
12125 const char *zCreate =
12126 "CREATE TABLE IF NOT EXISTS sqlar(\n"
12127 " name TEXT PRIMARY KEY, -- name of the file\n"
12128 " mode INT, -- access permissions\n"
12129 " mtime INT, -- last modification time\n"
12130 " sz INT, -- original file size\n"
12131 " data BLOB -- compressed content\n"
12133 const char *zDrop = "DROP TABLE IF EXISTS sqlar";
12134 const char *zInsertFmt =
12135 "REPLACE INTO sqlar(name,mode,mtime,sz,data)\n"
12140 " CASE substr(lsmode(mode),1,1)\n"
12141 " WHEN '-' THEN length(data)\n"
12142 " WHEN 'd' THEN 0\n"
12144 " CASE WHEN lsmode(mode) LIKE 'd%%' THEN NULL else data END\n"
12145 " FROM fsdir(%Q,%Q)\n"
12146 " WHERE lsmode(mode) NOT LIKE '?%%';";
12147 int i; /* For iterating through azFile[] */
12148 int rc; /* Return code */
12150 rc = arExecSql(pAr, "SAVEPOINT ar;");
12151 if( rc!=SQLITE_OK ) return rc;
12153 rc = arExecSql(pAr, zDrop);
12154 if( rc!=SQLITE_OK ) return rc;
12156 rc = arExecSql(pAr, zCreate);
12157 for(i=0; i<pAr->nArg && rc==SQLITE_OK; i++){
12158 char *zSql = sqlite3_mprintf(zInsertFmt,
12159 pAr->bVerbose ? "shell_putsnl(name)" : "name",
12160 pAr->azArg[i], pAr->zDir);
12161 rc = arExecSql(pAr, zSql);
12162 sqlite3_free(zSql);
12164 if( rc!=SQLITE_OK ){
12165 arExecSql(pAr, "ROLLBACK TO ar; RELEASE ar;");
12167 rc = arExecSql(pAr, "RELEASE ar;");
12173 ** Implementation of ".ar" dot command.
12175 static int arDotCommand(
12176 ShellState *pState, /* Current shell tool state */
12177 char **azArg, /* Array of arguments passed to dot command */
12178 int nArg /* Number of entries in azArg[] */
12182 memset(&cmd, 0, sizeof(cmd));
12183 rc = arParseCommand(azArg, nArg, &cmd);
12184 if( rc==SQLITE_OK ){
12185 int eDbType = SHELL_OPEN_UNSPEC;
12187 cmd.db = pState->db;
12189 eDbType = deduceDatabaseType(cmd.zFile);
12191 eDbType = pState->openMode;
12193 if( eDbType==SHELL_OPEN_ZIPFILE ){
12194 if( cmd.zFile==0 ){
12195 cmd.zSrcTable = sqlite3_mprintf("zip");
12197 cmd.zSrcTable = sqlite3_mprintf("zipfile(%Q)", cmd.zFile);
12199 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12200 utf8_printf(stderr, "zip archives are read-only\n");
12202 goto end_ar_command;
12205 }else if( cmd.zFile ){
12207 if( cmd.bAppend ) eDbType = SHELL_OPEN_APPENDVFS;
12208 if( cmd.eCmd==AR_CMD_CREATE || cmd.eCmd==AR_CMD_UPDATE ){
12209 flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
12211 flags = SQLITE_OPEN_READONLY;
12215 utf8_printf(pState->out, "-- open database '%s'%s\n", cmd.zFile,
12216 eDbType==SHELL_OPEN_APPENDVFS ? " using 'apndvfs'" : "");
12218 rc = sqlite3_open_v2(cmd.zFile, &cmd.db, flags,
12219 eDbType==SHELL_OPEN_APPENDVFS ? "apndvfs" : 0);
12220 if( rc!=SQLITE_OK ){
12221 utf8_printf(stderr, "cannot open file: %s (%s)\n",
12222 cmd.zFile, sqlite3_errmsg(cmd.db)
12224 goto end_ar_command;
12226 sqlite3_fileio_init(cmd.db, 0, 0);
12227 #ifdef SQLITE_HAVE_ZLIB
12228 sqlite3_sqlar_init(cmd.db, 0, 0);
12230 sqlite3_create_function(cmd.db, "shell_putsnl", 1, SQLITE_UTF8, cmd.p,
12231 shellPutsFunc, 0, 0);
12234 if( cmd.zSrcTable==0 ){
12235 if( cmd.eCmd!=AR_CMD_CREATE
12236 && sqlite3_table_column_metadata(cmd.db,0,"sqlar","name",0,0,0,0,0)
12238 utf8_printf(stderr, "database does not contain an 'sqlar' table\n");
12240 goto end_ar_command;
12242 cmd.zSrcTable = sqlite3_mprintf("sqlar");
12245 switch( cmd.eCmd ){
12246 case AR_CMD_CREATE:
12247 rc = arCreateOrUpdateCommand(&cmd, 0);
12250 case AR_CMD_EXTRACT:
12251 rc = arExtractCommand(&cmd);
12255 rc = arListCommand(&cmd);
12259 arUsage(pState->out);
12263 assert( cmd.eCmd==AR_CMD_UPDATE );
12264 rc = arCreateOrUpdateCommand(&cmd, 1);
12269 if( cmd.db!=pState->db ){
12270 sqlite3_close(cmd.db);
12272 sqlite3_free(cmd.zSrcTable);
12276 /* End of the ".archive" or ".ar" command logic
12277 **********************************************************************************/
12278 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB) */
12282 ** If an input line begins with "." then invoke this routine to
12283 ** process that line.
12285 ** Return 1 on error, 2 to exit, and 0 otherwise.
12287 static int do_meta_command(char *zLine, ShellState *p){
12294 #ifndef SQLITE_OMIT_VIRTUALTABLE
12295 if( p->expert.pExpert ){
12296 expertFinish(p, 1, 0);
12300 /* Parse the input line into tokens.
12302 while( zLine[h] && nArg<ArraySize(azArg) ){
12303 while( IsSpace(zLine[h]) ){ h++; }
12304 if( zLine[h]==0 ) break;
12305 if( zLine[h]=='\'' || zLine[h]=='"' ){
12306 int delim = zLine[h++];
12307 azArg[nArg++] = &zLine[h];
12308 while( zLine[h] && zLine[h]!=delim ){
12309 if( zLine[h]=='\\' && delim=='"' && zLine[h+1]!=0 ) h++;
12312 if( zLine[h]==delim ){
12315 if( delim=='"' ) resolve_backslashes(azArg[nArg-1]);
12317 azArg[nArg++] = &zLine[h];
12318 while( zLine[h] && !IsSpace(zLine[h]) ){ h++; }
12319 if( zLine[h] ) zLine[h++] = 0;
12320 resolve_backslashes(azArg[nArg-1]);
12324 /* Process the input line.
12326 if( nArg==0 ) return 0; /* no tokens, no error */
12327 n = strlen30(azArg[0]);
12331 #ifndef SQLITE_OMIT_AUTHORIZATION
12332 if( c=='a' && strncmp(azArg[0], "auth", n)==0 ){
12334 raw_printf(stderr, "Usage: .auth ON|OFF\n");
12336 goto meta_command_exit;
12339 if( booleanValue(azArg[1]) ){
12340 sqlite3_set_authorizer(p->db, shellAuth, p);
12342 sqlite3_set_authorizer(p->db, 0, 0);
12347 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(SQLITE_HAVE_ZLIB)
12348 if( c=='a' && strncmp(azArg[0], "archive", n)==0 ){
12350 rc = arDotCommand(p, azArg, nArg);
12354 if( (c=='b' && n>=3 && strncmp(azArg[0], "backup", n)==0)
12355 || (c=='s' && n>=3 && strncmp(azArg[0], "save", n)==0)
12357 const char *zDestFile = 0;
12358 const char *zDb = 0;
12360 sqlite3_backup *pBackup;
12362 for(j=1; j<nArg; j++){
12363 const char *z = azArg[j];
12365 while( z[0]=='-' ) z++;
12366 /* No options to process at this time */
12368 utf8_printf(stderr, "unknown option: %s\n", azArg[j]);
12371 }else if( zDestFile==0 ){
12372 zDestFile = azArg[j];
12373 }else if( zDb==0 ){
12375 zDestFile = azArg[j];
12377 raw_printf(stderr, "too many arguments to .backup\n");
12381 if( zDestFile==0 ){
12382 raw_printf(stderr, "missing FILENAME argument on .backup\n");
12385 if( zDb==0 ) zDb = "main";
12386 rc = sqlite3_open(zDestFile, &pDest);
12387 if( rc!=SQLITE_OK ){
12388 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zDestFile);
12389 sqlite3_close(pDest);
12393 pBackup = sqlite3_backup_init(pDest, "main", p->db, zDb);
12395 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
12396 sqlite3_close(pDest);
12399 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK ){}
12400 sqlite3_backup_finish(pBackup);
12401 if( rc==SQLITE_DONE ){
12404 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(pDest));
12407 sqlite3_close(pDest);
12410 if( c=='b' && n>=3 && strncmp(azArg[0], "bail", n)==0 ){
12412 bail_on_error = booleanValue(azArg[1]);
12414 raw_printf(stderr, "Usage: .bail on|off\n");
12419 if( c=='b' && n>=3 && strncmp(azArg[0], "binary", n)==0 ){
12421 if( booleanValue(azArg[1]) ){
12422 setBinaryMode(p->out, 1);
12424 setTextMode(p->out, 1);
12427 raw_printf(stderr, "Usage: .binary on|off\n");
12432 if( c=='c' && strcmp(azArg[0],"cd")==0 ){
12434 #if defined(_WIN32) || defined(WIN32)
12435 wchar_t *z = sqlite3_win32_utf8_to_unicode(azArg[1]);
12436 rc = !SetCurrentDirectoryW(z);
12439 rc = chdir(azArg[1]);
12442 utf8_printf(stderr, "Cannot change to directory \"%s\"\n", azArg[1]);
12446 raw_printf(stderr, "Usage: .cd DIRECTORY\n");
12451 /* The undocumented ".breakpoint" command causes a call to the no-op
12452 ** routine named test_breakpoint().
12454 if( c=='b' && n>=3 && strncmp(azArg[0], "breakpoint", n)==0 ){
12458 if( c=='c' && n>=3 && strncmp(azArg[0], "changes", n)==0 ){
12460 setOrClearFlag(p, SHFLG_CountChanges, azArg[1]);
12462 raw_printf(stderr, "Usage: .changes on|off\n");
12467 /* Cancel output redirection, if it is currently set (by .testcase)
12468 ** Then read the content of the testcase-out.txt file and compare against
12469 ** azArg[1]. If there are differences, report an error and exit.
12471 if( c=='c' && n>=3 && strncmp(azArg[0], "check", n)==0 ){
12475 raw_printf(stderr, "Usage: .check GLOB-PATTERN\n");
12477 }else if( (zRes = readFile("testcase-out.txt", 0))==0 ){
12478 raw_printf(stderr, "Error: cannot read 'testcase-out.txt'\n");
12480 }else if( testcase_glob(azArg[1],zRes)==0 ){
12481 utf8_printf(stderr,
12482 "testcase-%s FAILED\n Expected: [%s]\n Got: [%s]\n",
12483 p->zTestcase, azArg[1], zRes);
12486 utf8_printf(stdout, "testcase-%s ok\n", p->zTestcase);
12489 sqlite3_free(zRes);
12492 if( c=='c' && strncmp(azArg[0], "clone", n)==0 ){
12494 tryToClone(p, azArg[1]);
12496 raw_printf(stderr, "Usage: .clone FILENAME\n");
12501 if( c=='d' && n>1 && strncmp(azArg[0], "databases", n)==0 ){
12505 memcpy(&data, p, sizeof(data));
12506 data.showHeader = 0;
12507 data.cMode = data.mode = MODE_List;
12508 sqlite3_snprintf(sizeof(data.colSeparator),data.colSeparator,": ");
12510 sqlite3_exec(p->db, "SELECT name, file FROM pragma_database_list",
12511 callback, &data, &zErrMsg);
12513 utf8_printf(stderr,"Error: %s\n", zErrMsg);
12514 sqlite3_free(zErrMsg);
12519 if( c=='d' && strncmp(azArg[0], "dbinfo", n)==0 ){
12520 rc = shell_dbinfo_command(p, nArg, azArg);
12523 if( c=='d' && strncmp(azArg[0], "dump", n)==0 ){
12524 const char *zLike = 0;
12526 int savedShowHeader = p->showHeader;
12527 ShellClearFlag(p, SHFLG_PreserveRowid|SHFLG_Newlines);
12528 for(i=1; i<nArg; i++){
12529 if( azArg[i][0]=='-' ){
12530 const char *z = azArg[i]+1;
12531 if( z[0]=='-' ) z++;
12532 if( strcmp(z,"preserve-rowids")==0 ){
12533 #ifdef SQLITE_OMIT_VIRTUALTABLE
12534 raw_printf(stderr, "The --preserve-rowids option is not compatible"
12535 " with SQLITE_OMIT_VIRTUALTABLE\n");
12537 goto meta_command_exit;
12539 ShellSetFlag(p, SHFLG_PreserveRowid);
12542 if( strcmp(z,"newlines")==0 ){
12543 ShellSetFlag(p, SHFLG_Newlines);
12546 raw_printf(stderr, "Unknown option \"%s\" on \".dump\"\n", azArg[i]);
12548 goto meta_command_exit;
12551 raw_printf(stderr, "Usage: .dump ?--preserve-rowids? "
12552 "?--newlines? ?LIKE-PATTERN?\n");
12554 goto meta_command_exit;
12560 /* When playing back a "dump", the content might appear in an order
12561 ** which causes immediate foreign key constraints to be violated.
12562 ** So disable foreign-key constraint enforcement to prevent problems. */
12563 raw_printf(p->out, "PRAGMA foreign_keys=OFF;\n");
12564 raw_printf(p->out, "BEGIN TRANSACTION;\n");
12565 p->writableSchema = 0;
12567 /* Set writable_schema=ON since doing so forces SQLite to initialize
12568 ** as much of the schema as it can even if the sqlite_master table is
12570 sqlite3_exec(p->db, "SAVEPOINT dump; PRAGMA writable_schema=ON", 0, 0, 0);
12573 run_schema_dump_query(p,
12574 "SELECT name, type, sql FROM sqlite_master "
12575 "WHERE sql NOT NULL AND type=='table' AND name!='sqlite_sequence'"
12577 run_schema_dump_query(p,
12578 "SELECT name, type, sql FROM sqlite_master "
12579 "WHERE name=='sqlite_sequence'"
12581 run_table_dump_query(p,
12582 "SELECT sql FROM sqlite_master "
12583 "WHERE sql NOT NULL AND type IN ('index','trigger','view')", 0
12587 zSql = sqlite3_mprintf(
12588 "SELECT name, type, sql FROM sqlite_master "
12589 "WHERE tbl_name LIKE %Q AND type=='table'"
12590 " AND sql NOT NULL", zLike);
12591 run_schema_dump_query(p,zSql);
12592 sqlite3_free(zSql);
12593 zSql = sqlite3_mprintf(
12594 "SELECT sql FROM sqlite_master "
12595 "WHERE sql NOT NULL"
12596 " AND type IN ('index','trigger','view')"
12597 " AND tbl_name LIKE %Q", zLike);
12598 run_table_dump_query(p, zSql, 0);
12599 sqlite3_free(zSql);
12601 if( p->writableSchema ){
12602 raw_printf(p->out, "PRAGMA writable_schema=OFF;\n");
12603 p->writableSchema = 0;
12605 sqlite3_exec(p->db, "PRAGMA writable_schema=OFF;", 0, 0, 0);
12606 sqlite3_exec(p->db, "RELEASE dump;", 0, 0, 0);
12607 raw_printf(p->out, p->nErr ? "ROLLBACK; -- due to errors\n" : "COMMIT;\n");
12608 p->showHeader = savedShowHeader;
12611 if( c=='e' && strncmp(azArg[0], "echo", n)==0 ){
12613 setOrClearFlag(p, SHFLG_Echo, azArg[1]);
12615 raw_printf(stderr, "Usage: .echo on|off\n");
12620 if( c=='e' && strncmp(azArg[0], "eqp", n)==0 ){
12622 if( strcmp(azArg[1],"full")==0 ){
12623 p->autoEQP = AUTOEQP_full;
12624 }else if( strcmp(azArg[1],"trigger")==0 ){
12625 p->autoEQP = AUTOEQP_trigger;
12627 p->autoEQP = booleanValue(azArg[1]);
12630 raw_printf(stderr, "Usage: .eqp off|on|trigger|full\n");
12635 if( c=='e' && strncmp(azArg[0], "exit", n)==0 ){
12636 if( nArg>1 && (rc = (int)integerValue(azArg[1]))!=0 ) exit(rc);
12640 /* The ".explain" command is automatic now. It is largely pointless. It
12641 ** retained purely for backwards compatibility */
12642 if( c=='e' && strncmp(azArg[0], "explain", n)==0 ){
12645 if( strcmp(azArg[1],"auto")==0 ){
12648 val = booleanValue(azArg[1]);
12651 if( val==1 && p->mode!=MODE_Explain ){
12652 p->normalMode = p->mode;
12653 p->mode = MODE_Explain;
12654 p->autoExplain = 0;
12655 }else if( val==0 ){
12656 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12657 p->autoExplain = 0;
12658 }else if( val==99 ){
12659 if( p->mode==MODE_Explain ) p->mode = p->normalMode;
12660 p->autoExplain = 1;
12664 #ifndef SQLITE_OMIT_VIRTUALTABLE
12665 if( c=='e' && strncmp(azArg[0], "expert", n)==0 ){
12667 expertDotCommand(p, azArg, nArg);
12671 if( c=='f' && strncmp(azArg[0], "fullschema", n)==0 ){
12675 memcpy(&data, p, sizeof(data));
12676 data.showHeader = 0;
12677 data.cMode = data.mode = MODE_Semi;
12678 if( nArg==2 && optionMatch(azArg[1], "indent") ){
12679 data.cMode = data.mode = MODE_Pretty;
12683 raw_printf(stderr, "Usage: .fullschema ?--indent?\n");
12685 goto meta_command_exit;
12688 rc = sqlite3_exec(p->db,
12690 " (SELECT sql sql, type type, tbl_name tbl_name, name name, rowid x"
12691 " FROM sqlite_master UNION ALL"
12692 " SELECT sql, type, tbl_name, name, rowid FROM sqlite_temp_master) "
12693 "WHERE type!='meta' AND sql NOTNULL AND name NOT LIKE 'sqlite_%' "
12695 callback, &data, &zErrMsg
12697 if( rc==SQLITE_OK ){
12698 sqlite3_stmt *pStmt;
12699 rc = sqlite3_prepare_v2(p->db,
12700 "SELECT rowid FROM sqlite_master"
12701 " WHERE name GLOB 'sqlite_stat[134]'",
12703 doStats = sqlite3_step(pStmt)==SQLITE_ROW;
12704 sqlite3_finalize(pStmt);
12707 raw_printf(p->out, "/* No STAT tables available */\n");
12709 raw_printf(p->out, "ANALYZE sqlite_master;\n");
12710 sqlite3_exec(p->db, "SELECT 'ANALYZE sqlite_master'",
12711 callback, &data, &zErrMsg);
12712 data.cMode = data.mode = MODE_Insert;
12713 data.zDestTable = "sqlite_stat1";
12714 shell_exec(p->db, "SELECT * FROM sqlite_stat1",
12715 shell_callback, &data,&zErrMsg);
12716 data.zDestTable = "sqlite_stat3";
12717 shell_exec(p->db, "SELECT * FROM sqlite_stat3",
12718 shell_callback, &data,&zErrMsg);
12719 data.zDestTable = "sqlite_stat4";
12720 shell_exec(p->db, "SELECT * FROM sqlite_stat4",
12721 shell_callback, &data, &zErrMsg);
12722 raw_printf(p->out, "ANALYZE sqlite_master;\n");
12726 if( c=='h' && strncmp(azArg[0], "headers", n)==0 ){
12728 p->showHeader = booleanValue(azArg[1]);
12730 raw_printf(stderr, "Usage: .headers on|off\n");
12735 if( c=='h' && strncmp(azArg[0], "help", n)==0 ){
12736 utf8_printf(p->out, "%s", zHelp);
12739 if( c=='i' && strncmp(azArg[0], "import", n)==0 ){
12740 char *zTable; /* Insert data into this table */
12741 char *zFile; /* Name of file to extra content from */
12742 sqlite3_stmt *pStmt = NULL; /* A statement */
12743 int nCol; /* Number of columns in the table */
12744 int nByte; /* Number of bytes in an SQL string */
12745 int i, j; /* Loop counters */
12746 int needCommit; /* True to COMMIT or ROLLBACK at end */
12747 int nSep; /* Number of bytes in p->colSeparator[] */
12748 char *zSql; /* An SQL statement */
12749 ImportCtx sCtx; /* Reader context */
12750 char *(SQLITE_CDECL *xRead)(ImportCtx*); /* Func to read one value */
12751 int (SQLITE_CDECL *xCloser)(FILE*); /* Func to close file */
12754 raw_printf(stderr, "Usage: .import FILE TABLE\n");
12755 goto meta_command_exit;
12760 memset(&sCtx, 0, sizeof(sCtx));
12762 nSep = strlen30(p->colSeparator);
12765 "Error: non-null column separator required for import\n");
12769 raw_printf(stderr, "Error: multi-character column separators not allowed"
12773 nSep = strlen30(p->rowSeparator);
12775 raw_printf(stderr, "Error: non-null row separator required for import\n");
12778 if( nSep==2 && p->mode==MODE_Csv && strcmp(p->rowSeparator, SEP_CrLf)==0 ){
12779 /* When importing CSV (only), if the row separator is set to the
12780 ** default output row separator, change it to the default input
12781 ** row separator. This avoids having to maintain different input
12782 ** and output row separators. */
12783 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
12784 nSep = strlen30(p->rowSeparator);
12787 raw_printf(stderr, "Error: multi-character row separators not allowed"
12791 sCtx.zFile = zFile;
12793 if( sCtx.zFile[0]=='|' ){
12794 #ifdef SQLITE_OMIT_POPEN
12795 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
12798 sCtx.in = popen(sCtx.zFile+1, "r");
12799 sCtx.zFile = "<pipe>";
12803 sCtx.in = fopen(sCtx.zFile, "rb");
12806 if( p->mode==MODE_Ascii ){
12807 xRead = ascii_read_one_field;
12809 xRead = csv_read_one_field;
12812 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zFile);
12815 sCtx.cColSep = p->colSeparator[0];
12816 sCtx.cRowSep = p->rowSeparator[0];
12817 zSql = sqlite3_mprintf("SELECT * FROM %s", zTable);
12819 raw_printf(stderr, "Error: out of memory\n");
12823 nByte = strlen30(zSql);
12824 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12825 import_append_char(&sCtx, 0); /* To ensure sCtx.z is allocated */
12826 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(p->db))==0 ){
12827 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
12829 while( xRead(&sCtx) ){
12830 zCreate = sqlite3_mprintf("%z%c\n \"%w\" TEXT", zCreate, cSep, sCtx.z);
12832 if( sCtx.cTerm!=sCtx.cColSep ) break;
12835 sqlite3_free(zCreate);
12836 sqlite3_free(sCtx.z);
12838 utf8_printf(stderr,"%s: empty file\n", sCtx.zFile);
12841 zCreate = sqlite3_mprintf("%z\n)", zCreate);
12842 rc = sqlite3_exec(p->db, zCreate, 0, 0, 0);
12843 sqlite3_free(zCreate);
12845 utf8_printf(stderr, "CREATE TABLE %s(...) failed: %s\n", zTable,
12846 sqlite3_errmsg(p->db));
12847 sqlite3_free(sCtx.z);
12851 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12853 sqlite3_free(zSql);
12855 if (pStmt) sqlite3_finalize(pStmt);
12856 utf8_printf(stderr,"Error: %s\n", sqlite3_errmsg(p->db));
12860 nCol = sqlite3_column_count(pStmt);
12861 sqlite3_finalize(pStmt);
12863 if( nCol==0 ) return 0; /* no columns, no error */
12864 zSql = sqlite3_malloc64( nByte*2 + 20 + nCol*2 );
12866 raw_printf(stderr, "Error: out of memory\n");
12870 sqlite3_snprintf(nByte+20, zSql, "INSERT INTO \"%w\" VALUES(?", zTable);
12871 j = strlen30(zSql);
12872 for(i=1; i<nCol; i++){
12878 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12879 sqlite3_free(zSql);
12881 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
12882 if (pStmt) sqlite3_finalize(pStmt);
12886 needCommit = sqlite3_get_autocommit(p->db);
12887 if( needCommit ) sqlite3_exec(p->db, "BEGIN", 0, 0, 0);
12889 int startLine = sCtx.nLine;
12890 for(i=0; i<nCol; i++){
12891 char *z = xRead(&sCtx);
12893 ** Did we reach end-of-file before finding any columns?
12894 ** If so, stop instead of NULL filling the remaining columns.
12896 if( z==0 && i==0 ) break;
12898 ** Did we reach end-of-file OR end-of-line before finding any
12899 ** columns in ASCII mode? If so, stop instead of NULL filling
12900 ** the remaining columns.
12902 if( p->mode==MODE_Ascii && (z==0 || z[0]==0) && i==0 ) break;
12903 sqlite3_bind_text(pStmt, i+1, z, -1, SQLITE_TRANSIENT);
12904 if( i<nCol-1 && sCtx.cTerm!=sCtx.cColSep ){
12905 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
12906 "filling the rest with NULL\n",
12907 sCtx.zFile, startLine, nCol, i+1);
12909 while( i<=nCol ){ sqlite3_bind_null(pStmt, i); i++; }
12912 if( sCtx.cTerm==sCtx.cColSep ){
12916 }while( sCtx.cTerm==sCtx.cColSep );
12917 utf8_printf(stderr, "%s:%d: expected %d columns but found %d - "
12918 "extras ignored\n",
12919 sCtx.zFile, startLine, nCol, i);
12922 sqlite3_step(pStmt);
12923 rc = sqlite3_reset(pStmt);
12924 if( rc!=SQLITE_OK ){
12925 utf8_printf(stderr, "%s:%d: INSERT failed: %s\n", sCtx.zFile,
12926 startLine, sqlite3_errmsg(p->db));
12929 }while( sCtx.cTerm!=EOF );
12932 sqlite3_free(sCtx.z);
12933 sqlite3_finalize(pStmt);
12934 if( needCommit ) sqlite3_exec(p->db, "COMMIT", 0, 0, 0);
12937 #ifndef SQLITE_UNTESTABLE
12938 if( c=='i' && strncmp(azArg[0], "imposter", n)==0 ){
12940 char *zCollist = 0;
12941 sqlite3_stmt *pStmt;
12945 utf8_printf(stderr, "Usage: .imposter INDEX IMPOSTER\n");
12947 goto meta_command_exit;
12950 zSql = sqlite3_mprintf("SELECT rootpage FROM sqlite_master"
12951 " WHERE name='%q' AND type='index'", azArg[1]);
12952 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12953 sqlite3_free(zSql);
12954 if( sqlite3_step(pStmt)==SQLITE_ROW ){
12955 tnum = sqlite3_column_int(pStmt, 0);
12957 sqlite3_finalize(pStmt);
12959 utf8_printf(stderr, "no such index: \"%s\"\n", azArg[1]);
12961 goto meta_command_exit;
12963 zSql = sqlite3_mprintf("PRAGMA index_xinfo='%q'", azArg[1]);
12964 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
12965 sqlite3_free(zSql);
12967 while( sqlite3_step(pStmt)==SQLITE_ROW ){
12969 const char *zCol = (const char*)sqlite3_column_text(pStmt,2);
12972 if( sqlite3_column_int(pStmt,1)==-1 ){
12975 sqlite3_snprintf(sizeof(zLabel),zLabel,"expr%d",i);
12980 zCollist = sqlite3_mprintf("\"%w\"", zCol);
12982 zCollist = sqlite3_mprintf("%z,\"%w\"", zCollist, zCol);
12985 sqlite3_finalize(pStmt);
12986 zSql = sqlite3_mprintf(
12987 "CREATE TABLE \"%w\"(%s,PRIMARY KEY(%s))WITHOUT ROWID",
12988 azArg[2], zCollist, zCollist);
12989 sqlite3_free(zCollist);
12990 rc = sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 1, tnum);
12991 if( rc==SQLITE_OK ){
12992 rc = sqlite3_exec(p->db, zSql, 0, 0, 0);
12993 sqlite3_test_control(SQLITE_TESTCTRL_IMPOSTER, p->db, "main", 0, 0);
12995 utf8_printf(stderr, "Error in [%s]: %s\n", zSql, sqlite3_errmsg(p->db));
12997 utf8_printf(stdout, "%s;\n", zSql);
12999 "WARNING: writing to an imposter table will corrupt the index!\n"
13003 raw_printf(stderr, "SQLITE_TESTCTRL_IMPOSTER returns %d\n", rc);
13006 sqlite3_free(zSql);
13008 #endif /* !defined(SQLITE_OMIT_TEST_CONTROL) */
13010 #ifdef SQLITE_ENABLE_IOTRACE
13011 if( c=='i' && strncmp(azArg[0], "iotrace", n)==0 ){
13012 SQLITE_API extern void (SQLITE_CDECL *sqlite3IoTrace)(const char*, ...);
13013 if( iotrace && iotrace!=stdout ) fclose(iotrace);
13016 sqlite3IoTrace = 0;
13017 }else if( strcmp(azArg[1], "-")==0 ){
13018 sqlite3IoTrace = iotracePrintf;
13021 iotrace = fopen(azArg[1], "w");
13023 utf8_printf(stderr, "Error: cannot open \"%s\"\n", azArg[1]);
13024 sqlite3IoTrace = 0;
13027 sqlite3IoTrace = iotracePrintf;
13033 if( c=='l' && n>=5 && strncmp(azArg[0], "limits", n)==0 ){
13034 static const struct {
13035 const char *zLimitName; /* Name of a limit */
13036 int limitCode; /* Integer code for that limit */
13038 { "length", SQLITE_LIMIT_LENGTH },
13039 { "sql_length", SQLITE_LIMIT_SQL_LENGTH },
13040 { "column", SQLITE_LIMIT_COLUMN },
13041 { "expr_depth", SQLITE_LIMIT_EXPR_DEPTH },
13042 { "compound_select", SQLITE_LIMIT_COMPOUND_SELECT },
13043 { "vdbe_op", SQLITE_LIMIT_VDBE_OP },
13044 { "function_arg", SQLITE_LIMIT_FUNCTION_ARG },
13045 { "attached", SQLITE_LIMIT_ATTACHED },
13046 { "like_pattern_length", SQLITE_LIMIT_LIKE_PATTERN_LENGTH },
13047 { "variable_number", SQLITE_LIMIT_VARIABLE_NUMBER },
13048 { "trigger_depth", SQLITE_LIMIT_TRIGGER_DEPTH },
13049 { "worker_threads", SQLITE_LIMIT_WORKER_THREADS },
13054 for(i=0; i<ArraySize(aLimit); i++){
13055 printf("%20s %d\n", aLimit[i].zLimitName,
13056 sqlite3_limit(p->db, aLimit[i].limitCode, -1));
13058 }else if( nArg>3 ){
13059 raw_printf(stderr, "Usage: .limit NAME ?NEW-VALUE?\n");
13061 goto meta_command_exit;
13064 n2 = strlen30(azArg[1]);
13065 for(i=0; i<ArraySize(aLimit); i++){
13066 if( sqlite3_strnicmp(aLimit[i].zLimitName, azArg[1], n2)==0 ){
13070 utf8_printf(stderr, "ambiguous limit: \"%s\"\n", azArg[1]);
13072 goto meta_command_exit;
13077 utf8_printf(stderr, "unknown limit: \"%s\"\n"
13078 "enter \".limits\" with no arguments for a list.\n",
13081 goto meta_command_exit;
13084 sqlite3_limit(p->db, aLimit[iLimit].limitCode,
13085 (int)integerValue(azArg[2]));
13087 printf("%20s %d\n", aLimit[iLimit].zLimitName,
13088 sqlite3_limit(p->db, aLimit[iLimit].limitCode, -1));
13092 if( c=='l' && n>2 && strncmp(azArg[0], "lint", n)==0 ){
13094 lintDotCommand(p, azArg, nArg);
13097 #ifndef SQLITE_OMIT_LOAD_EXTENSION
13098 if( c=='l' && strncmp(azArg[0], "load", n)==0 ){
13099 const char *zFile, *zProc;
13102 raw_printf(stderr, "Usage: .load FILE ?ENTRYPOINT?\n");
13104 goto meta_command_exit;
13107 zProc = nArg>=3 ? azArg[2] : 0;
13109 rc = sqlite3_load_extension(p->db, zFile, zProc, &zErrMsg);
13110 if( rc!=SQLITE_OK ){
13111 utf8_printf(stderr, "Error: %s\n", zErrMsg);
13112 sqlite3_free(zErrMsg);
13118 if( c=='l' && strncmp(azArg[0], "log", n)==0 ){
13120 raw_printf(stderr, "Usage: .log FILENAME\n");
13123 const char *zFile = azArg[1];
13124 output_file_close(p->pLog);
13125 p->pLog = output_file_open(zFile, 0);
13129 if( c=='m' && strncmp(azArg[0], "mode", n)==0 ){
13130 const char *zMode = nArg>=2 ? azArg[1] : "";
13131 int n2 = strlen30(zMode);
13133 if( c2=='l' && n2>2 && strncmp(azArg[1],"lines",n2)==0 ){
13134 p->mode = MODE_Line;
13135 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13136 }else if( c2=='c' && strncmp(azArg[1],"columns",n2)==0 ){
13137 p->mode = MODE_Column;
13138 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13139 }else if( c2=='l' && n2>2 && strncmp(azArg[1],"list",n2)==0 ){
13140 p->mode = MODE_List;
13141 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Column);
13142 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13143 }else if( c2=='h' && strncmp(azArg[1],"html",n2)==0 ){
13144 p->mode = MODE_Html;
13145 }else if( c2=='t' && strncmp(azArg[1],"tcl",n2)==0 ){
13146 p->mode = MODE_Tcl;
13147 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Space);
13148 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Row);
13149 }else if( c2=='c' && strncmp(azArg[1],"csv",n2)==0 ){
13150 p->mode = MODE_Csv;
13151 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13152 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13153 }else if( c2=='t' && strncmp(azArg[1],"tabs",n2)==0 ){
13154 p->mode = MODE_List;
13155 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Tab);
13156 }else if( c2=='i' && strncmp(azArg[1],"insert",n2)==0 ){
13157 p->mode = MODE_Insert;
13158 set_table_name(p, nArg>=3 ? azArg[2] : "table");
13159 }else if( c2=='q' && strncmp(azArg[1],"quote",n2)==0 ){
13160 p->mode = MODE_Quote;
13161 }else if( c2=='a' && strncmp(azArg[1],"ascii",n2)==0 ){
13162 p->mode = MODE_Ascii;
13163 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Unit);
13164 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_Record);
13165 }else if( nArg==1 ){
13166 raw_printf(p->out, "current output mode: %s\n", modeDescr[p->mode]);
13168 raw_printf(stderr, "Error: mode should be one of: "
13169 "ascii column csv html insert line list quote tabs tcl\n");
13172 p->cMode = p->mode;
13175 if( c=='n' && strncmp(azArg[0], "nullvalue", n)==0 ){
13177 sqlite3_snprintf(sizeof(p->nullValue), p->nullValue,
13178 "%.*s", (int)ArraySize(p->nullValue)-1, azArg[1]);
13180 raw_printf(stderr, "Usage: .nullvalue STRING\n");
13185 if( c=='o' && strncmp(azArg[0], "open", n)==0 && n>=2 ){
13186 char *zNewFilename; /* Name of the database file to open */
13187 int iName = 1; /* Index in azArg[] of the filename */
13188 int newFlag = 0; /* True to delete file before opening */
13189 /* Close the existing database */
13190 session_close_all(p);
13191 sqlite3_close(p->db);
13193 p->zDbFilename = 0;
13194 sqlite3_free(p->zFreeOnClose);
13195 p->zFreeOnClose = 0;
13196 p->openMode = SHELL_OPEN_UNSPEC;
13197 /* Check for command-line arguments */
13198 for(iName=1; iName<nArg && azArg[iName][0]=='-'; iName++){
13199 const char *z = azArg[iName];
13200 if( optionMatch(z,"new") ){
13202 #ifdef SQLITE_HAVE_ZIP
13203 }else if( optionMatch(z, "zip") ){
13204 p->openMode = SHELL_OPEN_ZIPFILE;
13206 }else if( optionMatch(z, "append") ){
13207 p->openMode = SHELL_OPEN_APPENDVFS;
13208 }else if( z[0]=='-' ){
13209 utf8_printf(stderr, "unknown option: %s\n", z);
13211 goto meta_command_exit;
13214 /* If a filename is specified, try to open it first */
13215 zNewFilename = nArg>iName ? sqlite3_mprintf("%s", azArg[iName]) : 0;
13216 if( zNewFilename ){
13217 if( newFlag ) shellDeleteFile(zNewFilename);
13218 p->zDbFilename = zNewFilename;
13221 utf8_printf(stderr, "Error: cannot open '%s'\n", zNewFilename);
13222 sqlite3_free(zNewFilename);
13224 p->zFreeOnClose = zNewFilename;
13228 /* As a fall-back open a TEMP database */
13229 p->zDbFilename = 0;
13235 && (strncmp(azArg[0], "output", n)==0||strncmp(azArg[0], "once", n)==0))
13236 || (c=='e' && n==5 && strcmp(azArg[0],"excel")==0)
13238 const char *zFile = nArg>=2 ? azArg[1] : "stdout";
13240 if( azArg[0][0]=='e' ){
13241 /* Transform the ".excel" command into ".once -x" */
13244 zFile = azArg[1] = "-x";
13248 utf8_printf(stderr, "Usage: .%s [-e|-x|FILE]\n", azArg[0]);
13250 goto meta_command_exit;
13252 if( n>1 && strncmp(azArg[0], "once", n)==0 ){
13254 raw_printf(stderr, "Usage: .once (-e|-x|FILE)\n");
13256 goto meta_command_exit;
13263 if( zFile[0]=='-' && zFile[1]=='-' ) zFile++;
13264 if( strcmp(zFile, "-e")==0 || strcmp(zFile, "-x")==0 ){
13267 if( zFile[1]=='x' ){
13268 newTempFile(p, "csv");
13269 p->mode = MODE_Csv;
13270 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator, SEP_Comma);
13271 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator, SEP_CrLf);
13273 newTempFile(p, "txt");
13276 zFile = p->zTempFile;
13278 if( zFile[0]=='|' ){
13279 #ifdef SQLITE_OMIT_POPEN
13280 raw_printf(stderr, "Error: pipes are not supported in this OS\n");
13284 p->out = popen(zFile + 1, "w");
13286 utf8_printf(stderr,"Error: cannot open pipe \"%s\"\n", zFile + 1);
13290 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
13294 p->out = output_file_open(zFile, bTxtMode);
13296 if( strcmp(zFile,"off")!=0 ){
13297 utf8_printf(stderr,"Error: cannot write to \"%s\"\n", zFile);
13302 sqlite3_snprintf(sizeof(p->outfile), p->outfile, "%s", zFile);
13307 if( c=='p' && n>=3 && strncmp(azArg[0], "print", n)==0 ){
13309 for(i=1; i<nArg; i++){
13310 if( i>1 ) raw_printf(p->out, " ");
13311 utf8_printf(p->out, "%s", azArg[i]);
13313 raw_printf(p->out, "\n");
13316 if( c=='p' && strncmp(azArg[0], "prompt", n)==0 ){
13318 strncpy(mainPrompt,azArg[1],(int)ArraySize(mainPrompt)-1);
13321 strncpy(continuePrompt,azArg[2],(int)ArraySize(continuePrompt)-1);
13325 if( c=='q' && strncmp(azArg[0], "quit", n)==0 ){
13329 if( c=='r' && n>=3 && strncmp(azArg[0], "read", n)==0 ){
13332 raw_printf(stderr, "Usage: .read FILE\n");
13334 goto meta_command_exit;
13336 alt = fopen(azArg[1], "rb");
13338 utf8_printf(stderr,"Error: cannot open \"%s\"\n", azArg[1]);
13341 rc = process_input(p, alt);
13346 if( c=='r' && n>=3 && strncmp(azArg[0], "restore", n)==0 ){
13347 const char *zSrcFile;
13350 sqlite3_backup *pBackup;
13354 zSrcFile = azArg[1];
13356 }else if( nArg==3 ){
13357 zSrcFile = azArg[2];
13360 raw_printf(stderr, "Usage: .restore ?DB? FILE\n");
13362 goto meta_command_exit;
13364 rc = sqlite3_open(zSrcFile, &pSrc);
13365 if( rc!=SQLITE_OK ){
13366 utf8_printf(stderr, "Error: cannot open \"%s\"\n", zSrcFile);
13367 sqlite3_close(pSrc);
13371 pBackup = sqlite3_backup_init(p->db, zDb, pSrc, "main");
13373 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13374 sqlite3_close(pSrc);
13377 while( (rc = sqlite3_backup_step(pBackup,100))==SQLITE_OK
13378 || rc==SQLITE_BUSY ){
13379 if( rc==SQLITE_BUSY ){
13380 if( nTimeout++ >= 3 ) break;
13381 sqlite3_sleep(100);
13384 sqlite3_backup_finish(pBackup);
13385 if( rc==SQLITE_DONE ){
13387 }else if( rc==SQLITE_BUSY || rc==SQLITE_LOCKED ){
13388 raw_printf(stderr, "Error: source database is busy\n");
13391 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13394 sqlite3_close(pSrc);
13398 if( c=='s' && strncmp(azArg[0], "scanstats", n)==0 ){
13400 p->scanstatsOn = booleanValue(azArg[1]);
13401 #ifndef SQLITE_ENABLE_STMT_SCANSTATUS
13402 raw_printf(stderr, "Warning: .scanstats not available in this build.\n");
13405 raw_printf(stderr, "Usage: .scanstats on|off\n");
13410 if( c=='s' && strncmp(azArg[0], "schema", n)==0 ){
13414 const char *zDiv = "(";
13415 const char *zName = 0;
13421 memcpy(&data, p, sizeof(data));
13422 data.showHeader = 0;
13423 data.cMode = data.mode = MODE_Semi;
13424 initText(&sSelect);
13425 for(ii=1; ii<nArg; ii++){
13426 if( optionMatch(azArg[ii],"indent") ){
13427 data.cMode = data.mode = MODE_Pretty;
13428 }else if( optionMatch(azArg[ii],"debug") ){
13430 }else if( zName==0 ){
13433 raw_printf(stderr, "Usage: .schema ?--indent? ?LIKE-PATTERN?\n");
13435 goto meta_command_exit;
13439 int isMaster = sqlite3_strlike(zName, "sqlite_master", 0)==0;
13440 if( isMaster || sqlite3_strlike(zName,"sqlite_temp_master",0)==0 ){
13441 char *new_argv[2], *new_colv[2];
13442 new_argv[0] = sqlite3_mprintf(
13443 "CREATE TABLE %s (\n"
13446 " tbl_name text,\n"
13447 " rootpage integer,\n"
13449 ")", isMaster ? "sqlite_master" : "sqlite_temp_master");
13451 new_colv[0] = "sql";
13453 callback(&data, 1, new_argv, new_colv);
13454 sqlite3_free(new_argv[0]);
13458 sqlite3_stmt *pStmt = 0;
13459 rc = sqlite3_prepare_v2(p->db, "SELECT name FROM pragma_database_list",
13462 utf8_printf(stderr, "Error: %s\n", sqlite3_errmsg(p->db));
13463 sqlite3_finalize(pStmt);
13465 goto meta_command_exit;
13467 appendText(&sSelect, "SELECT sql FROM", 0);
13469 while( sqlite3_step(pStmt)==SQLITE_ROW ){
13470 const char *zDb = (const char*)sqlite3_column_text(pStmt, 0);
13472 sqlite3_snprintf(sizeof(zScNum), zScNum, "%d", ++iSchema);
13473 appendText(&sSelect, zDiv, 0);
13474 zDiv = " UNION ALL ";
13475 appendText(&sSelect, "SELECT shell_add_schema(sql,", 0);
13476 if( sqlite3_stricmp(zDb, "main")!=0 ){
13477 appendText(&sSelect, zDb, '"');
13479 appendText(&sSelect, "NULL", 0);
13481 appendText(&sSelect, ",name) AS sql, type, tbl_name, name, rowid,", 0);
13482 appendText(&sSelect, zScNum, 0);
13483 appendText(&sSelect, " AS snum, ", 0);
13484 appendText(&sSelect, zDb, '\'');
13485 appendText(&sSelect, " AS sname FROM ", 0);
13486 appendText(&sSelect, zDb, '"');
13487 appendText(&sSelect, ".sqlite_master", 0);
13489 sqlite3_finalize(pStmt);
13490 #ifdef SQLITE_INTROSPECTION_PRAGMAS
13492 appendText(&sSelect,
13493 " UNION ALL SELECT shell_module_schema(name),"
13494 " 'table', name, name, name, 9e+99, 'main' FROM pragma_module_list", 0);
13497 appendText(&sSelect, ") WHERE ", 0);
13499 char *zQarg = sqlite3_mprintf("%Q", zName);
13500 if( strchr(zName, '.') ){
13501 appendText(&sSelect, "lower(printf('%s.%s',sname,tbl_name))", 0);
13503 appendText(&sSelect, "lower(tbl_name)", 0);
13505 appendText(&sSelect, strchr(zName, '*') ? " GLOB " : " LIKE ", 0);
13506 appendText(&sSelect, zQarg, 0);
13507 appendText(&sSelect, " AND ", 0);
13508 sqlite3_free(zQarg);
13510 appendText(&sSelect, "type!='meta' AND sql IS NOT NULL"
13511 " ORDER BY snum, rowid", 0);
13513 utf8_printf(p->out, "SQL: %s;\n", sSelect.z);
13515 rc = sqlite3_exec(p->db, sSelect.z, callback, &data, &zErrMsg);
13517 freeText(&sSelect);
13520 utf8_printf(stderr,"Error: %s\n", zErrMsg);
13521 sqlite3_free(zErrMsg);
13523 }else if( rc != SQLITE_OK ){
13524 raw_printf(stderr,"Error: querying schema information\n");
13531 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_SELECTTRACE)
13532 if( c=='s' && n==11 && strncmp(azArg[0], "selecttrace", n)==0 ){
13533 sqlite3SelectTrace = (int)integerValue(azArg[1]);
13537 #if defined(SQLITE_ENABLE_SESSION)
13538 if( c=='s' && strncmp(azArg[0],"session",n)==0 && n>=3 ){
13539 OpenSession *pSession = &p->aSession[0];
13540 char **azCmd = &azArg[1];
13542 int nCmd = nArg - 1;
13544 if( nArg<=1 ) goto session_syntax_error;
13547 for(iSes=0; iSes<p->nSession; iSes++){
13548 if( strcmp(p->aSession[iSes].zName, azArg[1])==0 ) break;
13550 if( iSes<p->nSession ){
13551 pSession = &p->aSession[iSes];
13555 pSession = &p->aSession[0];
13560 /* .session attach TABLE
13561 ** Invoke the sqlite3session_attach() interface to attach a particular
13562 ** table so that it is never filtered.
13564 if( strcmp(azCmd[0],"attach")==0 ){
13565 if( nCmd!=2 ) goto session_syntax_error;
13566 if( pSession->p==0 ){
13568 raw_printf(stderr, "ERROR: No sessions are open\n");
13570 rc = sqlite3session_attach(pSession->p, azCmd[1]);
13572 raw_printf(stderr, "ERROR: sqlite3session_attach() returns %d\n", rc);
13578 /* .session changeset FILE
13579 ** .session patchset FILE
13580 ** Write a changeset or patchset into a file. The file is overwritten.
13582 if( strcmp(azCmd[0],"changeset")==0 || strcmp(azCmd[0],"patchset")==0 ){
13584 if( nCmd!=2 ) goto session_syntax_error;
13585 if( pSession->p==0 ) goto session_not_open;
13586 out = fopen(azCmd[1], "wb");
13588 utf8_printf(stderr, "ERROR: cannot open \"%s\" for writing\n", azCmd[1]);
13592 if( azCmd[0][0]=='c' ){
13593 rc = sqlite3session_changeset(pSession->p, &szChng, &pChng);
13595 rc = sqlite3session_patchset(pSession->p, &szChng, &pChng);
13598 printf("Error: error code %d\n", rc);
13602 && fwrite(pChng, szChng, 1, out)!=1 ){
13603 raw_printf(stderr, "ERROR: Failed to write entire %d-byte output\n",
13606 sqlite3_free(pChng);
13612 ** Close the identified session
13614 if( strcmp(azCmd[0], "close")==0 ){
13615 if( nCmd!=1 ) goto session_syntax_error;
13617 session_close(pSession);
13618 p->aSession[iSes] = p->aSession[--p->nSession];
13622 /* .session enable ?BOOLEAN?
13623 ** Query or set the enable flag
13625 if( strcmp(azCmd[0], "enable")==0 ){
13627 if( nCmd>2 ) goto session_syntax_error;
13628 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
13630 ii = sqlite3session_enable(pSession->p, ii);
13631 utf8_printf(p->out, "session %s enable flag = %d\n",
13632 pSession->zName, ii);
13636 /* .session filter GLOB ....
13637 ** Set a list of GLOB patterns of table names to be excluded.
13639 if( strcmp(azCmd[0], "filter")==0 ){
13641 if( nCmd<2 ) goto session_syntax_error;
13643 for(ii=0; ii<pSession->nFilter; ii++){
13644 sqlite3_free(pSession->azFilter[ii]);
13646 sqlite3_free(pSession->azFilter);
13647 nByte = sizeof(pSession->azFilter[0])*(nCmd-1);
13648 pSession->azFilter = sqlite3_malloc( nByte );
13649 if( pSession->azFilter==0 ){
13650 raw_printf(stderr, "Error: out or memory\n");
13653 for(ii=1; ii<nCmd; ii++){
13654 pSession->azFilter[ii-1] = sqlite3_mprintf("%s", azCmd[ii]);
13656 pSession->nFilter = ii-1;
13660 /* .session indirect ?BOOLEAN?
13661 ** Query or set the indirect flag
13663 if( strcmp(azCmd[0], "indirect")==0 ){
13665 if( nCmd>2 ) goto session_syntax_error;
13666 ii = nCmd==1 ? -1 : booleanValue(azCmd[1]);
13668 ii = sqlite3session_indirect(pSession->p, ii);
13669 utf8_printf(p->out, "session %s indirect flag = %d\n",
13670 pSession->zName, ii);
13674 /* .session isempty
13675 ** Determine if the session is empty
13677 if( strcmp(azCmd[0], "isempty")==0 ){
13679 if( nCmd!=1 ) goto session_syntax_error;
13681 ii = sqlite3session_isempty(pSession->p);
13682 utf8_printf(p->out, "session %s isempty flag = %d\n",
13683 pSession->zName, ii);
13688 ** List all currently open sessions
13690 if( strcmp(azCmd[0],"list")==0 ){
13691 for(i=0; i<p->nSession; i++){
13692 utf8_printf(p->out, "%d %s\n", i, p->aSession[i].zName);
13696 /* .session open DB NAME
13697 ** Open a new session called NAME on the attached database DB.
13698 ** DB is normally "main".
13700 if( strcmp(azCmd[0],"open")==0 ){
13702 if( nCmd!=3 ) goto session_syntax_error;
13704 if( zName[0]==0 ) goto session_syntax_error;
13705 for(i=0; i<p->nSession; i++){
13706 if( strcmp(p->aSession[i].zName,zName)==0 ){
13707 utf8_printf(stderr, "Session \"%s\" already exists\n", zName);
13708 goto meta_command_exit;
13711 if( p->nSession>=ArraySize(p->aSession) ){
13712 raw_printf(stderr, "Maximum of %d sessions\n", ArraySize(p->aSession));
13713 goto meta_command_exit;
13715 pSession = &p->aSession[p->nSession];
13716 rc = sqlite3session_create(p->db, azCmd[1], &pSession->p);
13718 raw_printf(stderr, "Cannot open session: error code=%d\n", rc);
13720 goto meta_command_exit;
13722 pSession->nFilter = 0;
13723 sqlite3session_table_filter(pSession->p, session_filter, pSession);
13725 pSession->zName = sqlite3_mprintf("%s", zName);
13727 /* If no command name matches, show a syntax error */
13728 session_syntax_error:
13733 #ifdef SQLITE_DEBUG
13734 /* Undocumented commands for internal testing. Subject to change
13735 ** without notice. */
13736 if( c=='s' && n>=10 && strncmp(azArg[0], "selftest-", 9)==0 ){
13737 if( strncmp(azArg[0]+9, "boolean", n-9)==0 ){
13739 for(i=1; i<nArg; i++){
13740 v = booleanValue(azArg[i]);
13741 utf8_printf(p->out, "%s: %d 0x%x\n", azArg[i], v, v);
13744 if( strncmp(azArg[0]+9, "integer", n-9)==0 ){
13745 int i; sqlite3_int64 v;
13746 for(i=1; i<nArg; i++){
13748 v = integerValue(azArg[i]);
13749 sqlite3_snprintf(sizeof(zBuf),zBuf,"%s: %lld 0x%llx\n", azArg[i],v,v);
13750 utf8_printf(p->out, "%s", zBuf);
13756 if( c=='s' && n>=4 && strncmp(azArg[0],"selftest",n)==0 ){
13757 int bIsInit = 0; /* True to initialize the SELFTEST table */
13758 int bVerbose = 0; /* Verbose output */
13759 int bSelftestExists; /* True if SELFTEST already exists */
13760 int i, k; /* Loop counters */
13761 int nTest = 0; /* Number of tests runs */
13762 int nErr = 0; /* Number of errors seen */
13763 ShellText str; /* Answer for a query */
13764 sqlite3_stmt *pStmt = 0; /* Query against the SELFTEST table */
13767 for(i=1; i<nArg; i++){
13768 const char *z = azArg[i];
13769 if( z[0]=='-' && z[1]=='-' ) z++;
13770 if( strcmp(z,"-init")==0 ){
13773 if( strcmp(z,"-v")==0 ){
13777 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
13778 azArg[i], azArg[0]);
13779 raw_printf(stderr, "Should be one of: --init -v\n");
13781 goto meta_command_exit;
13784 if( sqlite3_table_column_metadata(p->db,"main","selftest",0,0,0,0,0,0)
13786 bSelftestExists = 0;
13788 bSelftestExists = 1;
13791 createSelftestTable(p);
13792 bSelftestExists = 1;
13795 appendText(&str, "x", 0);
13796 for(k=bSelftestExists; k>=0; k--){
13798 rc = sqlite3_prepare_v2(p->db,
13799 "SELECT tno,op,cmd,ans FROM selftest ORDER BY tno",
13802 rc = sqlite3_prepare_v2(p->db,
13803 "VALUES(0,'memo','Missing SELFTEST table - default checks only',''),"
13804 " (1,'run','PRAGMA integrity_check','ok')",
13808 raw_printf(stderr, "Error querying the selftest table\n");
13810 sqlite3_finalize(pStmt);
13811 goto meta_command_exit;
13813 for(i=1; sqlite3_step(pStmt)==SQLITE_ROW; i++){
13814 int tno = sqlite3_column_int(pStmt, 0);
13815 const char *zOp = (const char*)sqlite3_column_text(pStmt, 1);
13816 const char *zSql = (const char*)sqlite3_column_text(pStmt, 2);
13817 const char *zAns = (const char*)sqlite3_column_text(pStmt, 3);
13821 char *zQuote = sqlite3_mprintf("%q", zSql);
13822 printf("%d: %s %s\n", tno, zOp, zSql);
13823 sqlite3_free(zQuote);
13825 if( strcmp(zOp,"memo")==0 ){
13826 utf8_printf(p->out, "%s\n", zSql);
13828 if( strcmp(zOp,"run")==0 ){
13832 rc = sqlite3_exec(p->db, zSql, captureOutputCallback, &str, &zErrMsg);
13835 utf8_printf(p->out, "Result: %s\n", str.z);
13837 if( rc || zErrMsg ){
13840 utf8_printf(p->out, "%d: error-code-%d: %s\n", tno, rc, zErrMsg);
13841 sqlite3_free(zErrMsg);
13842 }else if( strcmp(zAns,str.z)!=0 ){
13845 utf8_printf(p->out, "%d: Expected: [%s]\n", tno, zAns);
13846 utf8_printf(p->out, "%d: Got: [%s]\n", tno, str.z);
13850 utf8_printf(stderr,
13851 "Unknown operation \"%s\" on selftest line %d\n", zOp, tno);
13855 } /* End loop over rows of content from SELFTEST */
13856 sqlite3_finalize(pStmt);
13857 } /* End loop over k */
13859 utf8_printf(p->out, "%d errors out of %d tests\n", nErr, nTest);
13862 if( c=='s' && strncmp(azArg[0], "separator", n)==0 ){
13863 if( nArg<2 || nArg>3 ){
13864 raw_printf(stderr, "Usage: .separator COL ?ROW?\n");
13868 sqlite3_snprintf(sizeof(p->colSeparator), p->colSeparator,
13869 "%.*s", (int)ArraySize(p->colSeparator)-1, azArg[1]);
13872 sqlite3_snprintf(sizeof(p->rowSeparator), p->rowSeparator,
13873 "%.*s", (int)ArraySize(p->rowSeparator)-1, azArg[2]);
13877 if( c=='s' && n>=4 && strncmp(azArg[0],"sha3sum",n)==0 ){
13878 const char *zLike = 0; /* Which table to checksum. 0 means everything */
13879 int i; /* Loop counter */
13880 int bSchema = 0; /* Also hash the schema */
13881 int bSeparate = 0; /* Hash each table separately */
13882 int iSize = 224; /* Hash algorithm to use */
13883 int bDebug = 0; /* Only show the query that would have run */
13884 sqlite3_stmt *pStmt; /* For querying tables names */
13885 char *zSql; /* SQL to be run */
13886 char *zSep; /* Separator */
13887 ShellText sSql; /* Complete SQL for the query to run the hash */
13888 ShellText sQuery; /* Set of queries used to read all content */
13890 for(i=1; i<nArg; i++){
13891 const char *z = azArg[i];
13894 if( z[0]=='-' ) z++;
13895 if( strcmp(z,"schema")==0 ){
13898 if( strcmp(z,"sha3-224")==0 || strcmp(z,"sha3-256")==0
13899 || strcmp(z,"sha3-384")==0 || strcmp(z,"sha3-512")==0
13901 iSize = atoi(&z[5]);
13903 if( strcmp(z,"debug")==0 ){
13907 utf8_printf(stderr, "Unknown option \"%s\" on \"%s\"\n",
13908 azArg[i], azArg[0]);
13909 raw_printf(stderr, "Should be one of: --schema"
13910 " --sha3-224 --sha3-255 --sha3-384 --sha3-512\n");
13912 goto meta_command_exit;
13915 raw_printf(stderr, "Usage: .sha3sum ?OPTIONS? ?LIKE-PATTERN?\n");
13917 goto meta_command_exit;
13921 if( sqlite3_strlike("sqlite_%", zLike, 0)==0 ) bSchema = 1;
13925 zSql = "SELECT lower(name) FROM sqlite_master"
13926 " WHERE type='table' AND coalesce(rootpage,0)>1"
13927 " UNION ALL SELECT 'sqlite_master'"
13928 " ORDER BY 1 collate nocase";
13930 zSql = "SELECT lower(name) FROM sqlite_master"
13931 " WHERE type='table' AND coalesce(rootpage,0)>1"
13932 " AND name NOT LIKE 'sqlite_%'"
13933 " ORDER BY 1 collate nocase";
13935 sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
13938 appendText(&sSql, "WITH [sha3sum$query](a,b) AS(",0);
13940 while( SQLITE_ROW==sqlite3_step(pStmt) ){
13941 const char *zTab = (const char*)sqlite3_column_text(pStmt,0);
13942 if( zLike && sqlite3_strlike(zLike, zTab, 0)!=0 ) continue;
13943 if( strncmp(zTab, "sqlite_",7)!=0 ){
13944 appendText(&sQuery,"SELECT * FROM ", 0);
13945 appendText(&sQuery,zTab,'"');
13946 appendText(&sQuery," NOT INDEXED;", 0);
13947 }else if( strcmp(zTab, "sqlite_master")==0 ){
13948 appendText(&sQuery,"SELECT type,name,tbl_name,sql FROM sqlite_master"
13949 " ORDER BY name;", 0);
13950 }else if( strcmp(zTab, "sqlite_sequence")==0 ){
13951 appendText(&sQuery,"SELECT name,seq FROM sqlite_sequence"
13952 " ORDER BY name;", 0);
13953 }else if( strcmp(zTab, "sqlite_stat1")==0 ){
13954 appendText(&sQuery,"SELECT tbl,idx,stat FROM sqlite_stat1"
13955 " ORDER BY tbl,idx;", 0);
13956 }else if( strcmp(zTab, "sqlite_stat3")==0
13957 || strcmp(zTab, "sqlite_stat4")==0 ){
13958 appendText(&sQuery, "SELECT * FROM ", 0);
13959 appendText(&sQuery, zTab, 0);
13960 appendText(&sQuery, " ORDER BY tbl, idx, rowid;\n", 0);
13962 appendText(&sSql, zSep, 0);
13963 appendText(&sSql, sQuery.z, '\'');
13965 appendText(&sSql, ",", 0);
13966 appendText(&sSql, zTab, '\'');
13969 sqlite3_finalize(pStmt);
13971 zSql = sqlite3_mprintf(
13973 " SELECT lower(hex(sha3_query(a,%d))) AS hash, b AS label"
13974 " FROM [sha3sum$query]",
13977 zSql = sqlite3_mprintf(
13979 " SELECT lower(hex(sha3_query(group_concat(a,''),%d))) AS hash"
13980 " FROM [sha3sum$query]",
13986 utf8_printf(p->out, "%s\n", zSql);
13988 shell_exec(p->db, zSql, shell_callback, p, 0);
13990 sqlite3_free(zSql);
13994 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
13999 raw_printf(stderr, "Usage: .system COMMAND\n");
14001 goto meta_command_exit;
14003 zCmd = sqlite3_mprintf(strchr(azArg[1],' ')==0?"%s":"\"%s\"", azArg[1]);
14004 for(i=2; i<nArg; i++){
14005 zCmd = sqlite3_mprintf(strchr(azArg[i],' ')==0?"%z %s":"%z \"%s\"",
14009 sqlite3_free(zCmd);
14010 if( x ) raw_printf(stderr, "System command returns %d\n", x);
14013 if( c=='s' && strncmp(azArg[0], "show", n)==0 ){
14014 static const char *azBool[] = { "off", "on", "trigger", "full"};
14017 raw_printf(stderr, "Usage: .show\n");
14019 goto meta_command_exit;
14021 utf8_printf(p->out, "%12.12s: %s\n","echo",
14022 azBool[ShellHasFlag(p, SHFLG_Echo)]);
14023 utf8_printf(p->out, "%12.12s: %s\n","eqp", azBool[p->autoEQP&3]);
14024 utf8_printf(p->out, "%12.12s: %s\n","explain",
14025 p->mode==MODE_Explain ? "on" : p->autoExplain ? "auto" : "off");
14026 utf8_printf(p->out,"%12.12s: %s\n","headers", azBool[p->showHeader!=0]);
14027 utf8_printf(p->out, "%12.12s: %s\n","mode", modeDescr[p->mode]);
14028 utf8_printf(p->out, "%12.12s: ", "nullvalue");
14029 output_c_string(p->out, p->nullValue);
14030 raw_printf(p->out, "\n");
14031 utf8_printf(p->out,"%12.12s: %s\n","output",
14032 strlen30(p->outfile) ? p->outfile : "stdout");
14033 utf8_printf(p->out,"%12.12s: ", "colseparator");
14034 output_c_string(p->out, p->colSeparator);
14035 raw_printf(p->out, "\n");
14036 utf8_printf(p->out,"%12.12s: ", "rowseparator");
14037 output_c_string(p->out, p->rowSeparator);
14038 raw_printf(p->out, "\n");
14039 utf8_printf(p->out, "%12.12s: %s\n","stats", azBool[p->statsOn!=0]);
14040 utf8_printf(p->out, "%12.12s: ", "width");
14041 for (i=0;i<(int)ArraySize(p->colWidth) && p->colWidth[i] != 0;i++) {
14042 raw_printf(p->out, "%d ", p->colWidth[i]);
14044 raw_printf(p->out, "\n");
14045 utf8_printf(p->out, "%12.12s: %s\n", "filename",
14046 p->zDbFilename ? p->zDbFilename : "");
14049 if( c=='s' && strncmp(azArg[0], "stats", n)==0 ){
14051 p->statsOn = booleanValue(azArg[1]);
14052 }else if( nArg==1 ){
14053 display_stats(p->db, p, 0);
14055 raw_printf(stderr, "Usage: .stats ?on|off?\n");
14060 if( (c=='t' && n>1 && strncmp(azArg[0], "tables", n)==0)
14061 || (c=='i' && (strncmp(azArg[0], "indices", n)==0
14062 || strncmp(azArg[0], "indexes", n)==0) )
14064 sqlite3_stmt *pStmt;
14071 rc = sqlite3_prepare_v2(p->db, "PRAGMA database_list", -1, &pStmt, 0);
14072 if( rc ) return shellDatabaseError(p->db);
14074 if( nArg>2 && c=='i' ){
14075 /* It is an historical accident that the .indexes command shows an error
14076 ** when called with the wrong number of arguments whereas the .tables
14077 ** command does not. */
14078 raw_printf(stderr, "Usage: .indexes ?LIKE-PATTERN?\n");
14080 goto meta_command_exit;
14082 for(ii=0; sqlite3_step(pStmt)==SQLITE_ROW; ii++){
14083 const char *zDbName = (const char*)sqlite3_column_text(pStmt, 1);
14084 if( zDbName==0 ) continue;
14085 if( s.z && s.z[0] ) appendText(&s, " UNION ALL ", 0);
14086 if( sqlite3_stricmp(zDbName, "main")==0 ){
14087 appendText(&s, "SELECT name FROM ", 0);
14089 appendText(&s, "SELECT ", 0);
14090 appendText(&s, zDbName, '\'');
14091 appendText(&s, "||'.'||name FROM ", 0);
14093 appendText(&s, zDbName, '"');
14094 appendText(&s, ".sqlite_master ", 0);
14096 appendText(&s," WHERE type IN ('table','view')"
14097 " AND name NOT LIKE 'sqlite_%'"
14098 " AND name LIKE ?1", 0);
14100 appendText(&s," WHERE type='index'"
14101 " AND tbl_name LIKE ?1", 0);
14104 rc = sqlite3_finalize(pStmt);
14105 appendText(&s, " ORDER BY 1", 0);
14106 rc = sqlite3_prepare_v2(p->db, s.z, -1, &pStmt, 0);
14108 if( rc ) return shellDatabaseError(p->db);
14110 /* Run the SQL statement prepared by the above block. Store the results
14111 ** as an array of nul-terminated strings in azResult[]. */
14115 sqlite3_bind_text(pStmt, 1, azArg[1], -1, SQLITE_TRANSIENT);
14117 sqlite3_bind_text(pStmt, 1, "%", -1, SQLITE_STATIC);
14119 while( sqlite3_step(pStmt)==SQLITE_ROW ){
14120 if( nRow>=nAlloc ){
14122 int n2 = nAlloc*2 + 10;
14123 azNew = sqlite3_realloc64(azResult, sizeof(azResult[0])*n2);
14125 rc = shellNomemError();
14131 azResult[nRow] = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
14132 if( 0==azResult[nRow] ){
14133 rc = shellNomemError();
14138 if( sqlite3_finalize(pStmt)!=SQLITE_OK ){
14139 rc = shellDatabaseError(p->db);
14142 /* Pretty-print the contents of array azResult[] to the output */
14143 if( rc==0 && nRow>0 ){
14144 int len, maxlen = 0;
14146 int nPrintCol, nPrintRow;
14147 for(i=0; i<nRow; i++){
14148 len = strlen30(azResult[i]);
14149 if( len>maxlen ) maxlen = len;
14151 nPrintCol = 80/(maxlen+2);
14152 if( nPrintCol<1 ) nPrintCol = 1;
14153 nPrintRow = (nRow + nPrintCol - 1)/nPrintCol;
14154 for(i=0; i<nPrintRow; i++){
14155 for(j=i; j<nRow; j+=nPrintRow){
14156 char *zSp = j<nPrintRow ? "" : " ";
14157 utf8_printf(p->out, "%s%-*s", zSp, maxlen,
14158 azResult[j] ? azResult[j]:"");
14160 raw_printf(p->out, "\n");
14164 for(ii=0; ii<nRow; ii++) sqlite3_free(azResult[ii]);
14165 sqlite3_free(azResult);
14168 /* Begin redirecting output to the file "testcase-out.txt" */
14169 if( c=='t' && strcmp(azArg[0],"testcase")==0 ){
14171 p->out = output_file_open("testcase-out.txt", 0);
14173 raw_printf(stderr, "Error: cannot open 'testcase-out.txt'\n");
14176 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "%s", azArg[1]);
14178 sqlite3_snprintf(sizeof(p->zTestcase), p->zTestcase, "?");
14182 #ifndef SQLITE_UNTESTABLE
14183 if( c=='t' && n>=8 && strncmp(azArg[0], "testctrl", n)==0 ){
14184 static const struct {
14185 const char *zCtrlName; /* Name of a test-control option */
14186 int ctrlCode; /* Integer code for that option */
14187 const char *zUsage; /* Usage notes */
14189 { "always", SQLITE_TESTCTRL_ALWAYS, "BOOLEAN" },
14190 { "assert", SQLITE_TESTCTRL_ASSERT, "BOOLEAN" },
14191 /*{ "benign_malloc_hooks",SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS, "" },*/
14192 /*{ "bitvec_test", SQLITE_TESTCTRL_BITVEC_TEST, "" },*/
14193 { "byteorder", SQLITE_TESTCTRL_BYTEORDER, "" },
14194 /*{ "fault_install", SQLITE_TESTCTRL_FAULT_INSTALL, "" }, */
14195 { "imposter", SQLITE_TESTCTRL_IMPOSTER, "SCHEMA ON/OFF ROOTPAGE"},
14196 #ifdef SQLITE_N_KEYWORD
14197 { "iskeyword", SQLITE_TESTCTRL_ISKEYWORD, "IDENTIFIER" },
14199 { "localtime_fault", SQLITE_TESTCTRL_LOCALTIME_FAULT,"BOOLEAN" },
14200 { "never_corrupt", SQLITE_TESTCTRL_NEVER_CORRUPT, "BOOLEAN" },
14201 { "optimizations", SQLITE_TESTCTRL_OPTIMIZATIONS, "DISABLE-MASK" },
14203 { "parser_coverage", SQLITE_TESTCTRL_PARSER_COVERAGE, "" },
14205 { "pending_byte", SQLITE_TESTCTRL_PENDING_BYTE, "OFFSET " },
14206 { "prng_reset", SQLITE_TESTCTRL_PRNG_RESET, "" },
14207 { "prng_restore", SQLITE_TESTCTRL_PRNG_RESTORE, "" },
14208 { "prng_save", SQLITE_TESTCTRL_PRNG_SAVE, "" },
14209 { "reserve", SQLITE_TESTCTRL_RESERVE, "BYTES-OF-RESERVE" },
14213 int rc2 = 0; /* 0: usage. 1: %d 2: %x 3: no-output */
14216 const char *zCmd = 0;
14219 zCmd = nArg>=2 ? azArg[1] : "help";
14221 /* The argument can optionally begin with "-" or "--" */
14222 if( zCmd[0]=='-' && zCmd[1] ){
14224 if( zCmd[0]=='-' && zCmd[1] ) zCmd++;
14227 /* --help lists all test-controls */
14228 if( strcmp(zCmd,"help")==0 ){
14229 utf8_printf(p->out, "Available test-controls:\n");
14230 for(i=0; i<ArraySize(aCtrl); i++){
14231 utf8_printf(p->out, " .testctrl %s %s\n",
14232 aCtrl[i].zCtrlName, aCtrl[i].zUsage);
14235 goto meta_command_exit;
14238 /* convert testctrl text option to value. allow any unique prefix
14239 ** of the option name, or a numerical value. */
14240 n2 = strlen30(zCmd);
14241 for(i=0; i<ArraySize(aCtrl); i++){
14242 if( strncmp(zCmd, aCtrl[i].zCtrlName, n2)==0 ){
14244 testctrl = aCtrl[i].ctrlCode;
14247 utf8_printf(stderr, "Error: ambiguous test-control: \"%s\"\n"
14248 "Use \".testctrl --help\" for help\n", zCmd);
14250 goto meta_command_exit;
14255 utf8_printf(stderr,"Error: unknown test-control: %s\n"
14256 "Use \".testctrl --help\" for help\n", zCmd);
14260 /* sqlite3_test_control(int, db, int) */
14261 case SQLITE_TESTCTRL_OPTIMIZATIONS:
14262 case SQLITE_TESTCTRL_RESERVE:
14264 int opt = (int)strtol(azArg[2], 0, 0);
14265 rc2 = sqlite3_test_control(testctrl, p->db, opt);
14270 /* sqlite3_test_control(int) */
14271 case SQLITE_TESTCTRL_PRNG_SAVE:
14272 case SQLITE_TESTCTRL_PRNG_RESTORE:
14273 case SQLITE_TESTCTRL_PRNG_RESET:
14274 case SQLITE_TESTCTRL_BYTEORDER:
14276 rc2 = sqlite3_test_control(testctrl);
14277 isOk = testctrl==SQLITE_TESTCTRL_BYTEORDER ? 1 : 3;
14281 /* sqlite3_test_control(int, uint) */
14282 case SQLITE_TESTCTRL_PENDING_BYTE:
14284 unsigned int opt = (unsigned int)integerValue(azArg[2]);
14285 rc2 = sqlite3_test_control(testctrl, opt);
14290 /* sqlite3_test_control(int, int) */
14291 case SQLITE_TESTCTRL_ASSERT:
14292 case SQLITE_TESTCTRL_ALWAYS:
14294 int opt = booleanValue(azArg[2]);
14295 rc2 = sqlite3_test_control(testctrl, opt);
14300 /* sqlite3_test_control(int, int) */
14301 case SQLITE_TESTCTRL_LOCALTIME_FAULT:
14302 case SQLITE_TESTCTRL_NEVER_CORRUPT:
14304 int opt = booleanValue(azArg[2]);
14305 rc2 = sqlite3_test_control(testctrl, opt);
14310 /* sqlite3_test_control(int, char *) */
14311 #ifdef SQLITE_N_KEYWORD
14312 case SQLITE_TESTCTRL_ISKEYWORD:
14314 const char *opt = azArg[2];
14315 rc2 = sqlite3_test_control(testctrl, opt);
14321 case SQLITE_TESTCTRL_IMPOSTER:
14323 rc2 = sqlite3_test_control(testctrl, p->db,
14325 integerValue(azArg[3]),
14326 integerValue(azArg[4]));
14332 case SQLITE_TESTCTRL_PARSER_COVERAGE:
14334 sqlite3_test_control(testctrl, p->out);
14340 if( isOk==0 && iCtrl>=0 ){
14341 utf8_printf(p->out, "Usage: .testctrl %s %s\n", zCmd, aCtrl[iCtrl].zUsage);
14343 }else if( isOk==1 ){
14344 raw_printf(p->out, "%d\n", rc2);
14345 }else if( isOk==2 ){
14346 raw_printf(p->out, "0x%08x\n", rc2);
14349 #endif /* !defined(SQLITE_UNTESTABLE) */
14351 if( c=='t' && n>4 && strncmp(azArg[0], "timeout", n)==0 ){
14353 sqlite3_busy_timeout(p->db, nArg>=2 ? (int)integerValue(azArg[1]) : 0);
14356 if( c=='t' && n>=5 && strncmp(azArg[0], "timer", n)==0 ){
14358 enableTimer = booleanValue(azArg[1]);
14359 if( enableTimer && !HAS_TIMER ){
14360 raw_printf(stderr, "Error: timer not available on this system.\n");
14364 raw_printf(stderr, "Usage: .timer on|off\n");
14369 if( c=='t' && strncmp(azArg[0], "trace", n)==0 ){
14372 raw_printf(stderr, "Usage: .trace FILE|off\n");
14374 goto meta_command_exit;
14376 output_file_close(p->traceOut);
14377 p->traceOut = output_file_open(azArg[1], 0);
14378 #if !defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_OMIT_FLOATING_POINT)
14379 if( p->traceOut==0 ){
14380 sqlite3_trace_v2(p->db, 0, 0, 0);
14382 sqlite3_trace_v2(p->db, SQLITE_TRACE_STMT, sql_trace_callback,p->traceOut);
14387 #if SQLITE_USER_AUTHENTICATION
14388 if( c=='u' && strncmp(azArg[0], "user", n)==0 ){
14390 raw_printf(stderr, "Usage: .user SUBCOMMAND ...\n");
14392 goto meta_command_exit;
14395 if( strcmp(azArg[1],"login")==0 ){
14397 raw_printf(stderr, "Usage: .user login USER PASSWORD\n");
14399 goto meta_command_exit;
14401 rc = sqlite3_user_authenticate(p->db, azArg[2], azArg[3], strlen30(azArg[3]));
14403 utf8_printf(stderr, "Authentication failed for user %s\n", azArg[2]);
14406 }else if( strcmp(azArg[1],"add")==0 ){
14408 raw_printf(stderr, "Usage: .user add USER PASSWORD ISADMIN\n");
14410 goto meta_command_exit;
14412 rc = sqlite3_user_add(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
14413 booleanValue(azArg[4]));
14415 raw_printf(stderr, "User-Add failed: %d\n", rc);
14418 }else if( strcmp(azArg[1],"edit")==0 ){
14420 raw_printf(stderr, "Usage: .user edit USER PASSWORD ISADMIN\n");
14422 goto meta_command_exit;
14424 rc = sqlite3_user_change(p->db, azArg[2], azArg[3], strlen30(azArg[3]),
14425 booleanValue(azArg[4]));
14427 raw_printf(stderr, "User-Edit failed: %d\n", rc);
14430 }else if( strcmp(azArg[1],"delete")==0 ){
14432 raw_printf(stderr, "Usage: .user delete USER\n");
14434 goto meta_command_exit;
14436 rc = sqlite3_user_delete(p->db, azArg[2]);
14438 raw_printf(stderr, "User-Delete failed: %d\n", rc);
14442 raw_printf(stderr, "Usage: .user login|add|edit|delete ...\n");
14444 goto meta_command_exit;
14447 #endif /* SQLITE_USER_AUTHENTICATION */
14449 if( c=='v' && strncmp(azArg[0], "version", n)==0 ){
14450 utf8_printf(p->out, "SQLite %s %s\n" /*extra-version-info*/,
14451 sqlite3_libversion(), sqlite3_sourceid());
14452 #if SQLITE_HAVE_ZLIB
14453 utf8_printf(p->out, "zlib version %s\n", zlibVersion());
14455 #define CTIMEOPT_VAL_(opt) #opt
14456 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
14457 #if defined(__clang__) && defined(__clang_major__)
14458 utf8_printf(p->out, "clang-" CTIMEOPT_VAL(__clang_major__) "."
14459 CTIMEOPT_VAL(__clang_minor__) "."
14460 CTIMEOPT_VAL(__clang_patchlevel__) "\n");
14461 #elif defined(_MSC_VER)
14462 utf8_printf(p->out, "msvc-" CTIMEOPT_VAL(_MSC_VER) "\n");
14463 #elif defined(__GNUC__) && defined(__VERSION__)
14464 utf8_printf(p->out, "gcc-" __VERSION__ "\n");
14468 if( c=='v' && strncmp(azArg[0], "vfsinfo", n)==0 ){
14469 const char *zDbName = nArg==2 ? azArg[1] : "main";
14470 sqlite3_vfs *pVfs = 0;
14472 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFS_POINTER, &pVfs);
14474 utf8_printf(p->out, "vfs.zName = \"%s\"\n", pVfs->zName);
14475 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
14476 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
14477 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
14482 if( c=='v' && strncmp(azArg[0], "vfslist", n)==0 ){
14484 sqlite3_vfs *pCurrent = 0;
14486 sqlite3_file_control(p->db, "main", SQLITE_FCNTL_VFS_POINTER, &pCurrent);
14488 for(pVfs=sqlite3_vfs_find(0); pVfs; pVfs=pVfs->pNext){
14489 utf8_printf(p->out, "vfs.zName = \"%s\"%s\n", pVfs->zName,
14490 pVfs==pCurrent ? " <--- CURRENT" : "");
14491 raw_printf(p->out, "vfs.iVersion = %d\n", pVfs->iVersion);
14492 raw_printf(p->out, "vfs.szOsFile = %d\n", pVfs->szOsFile);
14493 raw_printf(p->out, "vfs.mxPathname = %d\n", pVfs->mxPathname);
14495 raw_printf(p->out, "-----------------------------------\n");
14500 if( c=='v' && strncmp(azArg[0], "vfsname", n)==0 ){
14501 const char *zDbName = nArg==2 ? azArg[1] : "main";
14502 char *zVfsName = 0;
14504 sqlite3_file_control(p->db, zDbName, SQLITE_FCNTL_VFSNAME, &zVfsName);
14506 utf8_printf(p->out, "%s\n", zVfsName);
14507 sqlite3_free(zVfsName);
14512 #if defined(SQLITE_DEBUG) && defined(SQLITE_ENABLE_WHERETRACE)
14513 if( c=='w' && strncmp(azArg[0], "wheretrace", n)==0 ){
14514 sqlite3WhereTrace = nArg>=2 ? booleanValue(azArg[1]) : 0xff;
14518 if( c=='w' && strncmp(azArg[0], "width", n)==0 ){
14520 assert( nArg<=ArraySize(azArg) );
14521 for(j=1; j<nArg && j<ArraySize(p->colWidth); j++){
14522 p->colWidth[j-1] = (int)integerValue(azArg[j]);
14527 utf8_printf(stderr, "Error: unknown command or invalid arguments: "
14528 " \"%s\". Enter \".help\" for help\n", azArg[0]);
14535 if( p->outCount==0 ) output_reset(p);
14541 ** Return TRUE if a semicolon occurs anywhere in the first N characters
14544 static int line_contains_semicolon(const char *z, int N){
14546 for(i=0; i<N; i++){ if( z[i]==';' ) return 1; }
14551 ** Test to see if a line consists entirely of whitespace.
14553 static int _all_whitespace(const char *z){
14555 if( IsSpace(z[0]) ) continue;
14556 if( *z=='/' && z[1]=='*' ){
14558 while( *z && (*z!='*' || z[1]!='/') ){ z++; }
14559 if( *z==0 ) return 0;
14563 if( *z=='-' && z[1]=='-' ){
14565 while( *z && *z!='\n' ){ z++; }
14566 if( *z==0 ) return 1;
14575 ** Return TRUE if the line typed in is an SQL command terminator other
14576 ** than a semi-colon. The SQL Server style "go" command is understood
14577 ** as is the Oracle "/".
14579 static int line_is_command_terminator(const char *zLine){
14580 while( IsSpace(zLine[0]) ){ zLine++; };
14581 if( zLine[0]=='/' && _all_whitespace(&zLine[1]) ){
14582 return 1; /* Oracle */
14584 if( ToLower(zLine[0])=='g' && ToLower(zLine[1])=='o'
14585 && _all_whitespace(&zLine[2]) ){
14586 return 1; /* SQL Server */
14592 ** Return true if zSql is a complete SQL statement. Return false if it
14593 ** ends in the middle of a string literal or C-style comment.
14595 static int line_is_complete(char *zSql, int nSql){
14597 if( zSql==0 ) return 1;
14600 rc = sqlite3_complete(zSql);
14606 ** Run a single line of SQL
14608 static int runOneSqlLine(ShellState *p, char *zSql, FILE *in, int startline){
14613 if( ShellHasFlag(p,SHFLG_Backslash) ) resolve_backslashes(zSql);
14615 rc = shell_exec(p->db, zSql, shell_callback, p, &zErrMsg);
14617 if( rc || zErrMsg ){
14619 if( in!=0 || !stdin_is_interactive ){
14620 sqlite3_snprintf(sizeof(zPrefix), zPrefix,
14621 "Error: near line %d:", startline);
14623 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "Error:");
14626 utf8_printf(stderr, "%s %s\n", zPrefix, zErrMsg);
14627 sqlite3_free(zErrMsg);
14630 utf8_printf(stderr, "%s %s\n", zPrefix, sqlite3_errmsg(p->db));
14633 }else if( ShellHasFlag(p, SHFLG_CountChanges) ){
14634 raw_printf(p->out, "changes: %3d total_changes: %d\n",
14635 sqlite3_changes(p->db), sqlite3_total_changes(p->db));
14642 ** Read input from *in and process it. If *in==0 then input
14643 ** is interactive - the user is typing it it. Otherwise, input
14644 ** is coming from a file or device. A prompt is issued and history
14645 ** is saved only if input is interactive. An interrupt signal will
14646 ** cause this routine to exit immediately, unless input is interactive.
14648 ** Return the number of errors.
14650 static int process_input(ShellState *p, FILE *in){
14651 char *zLine = 0; /* A single input line */
14652 char *zSql = 0; /* Accumulated SQL text */
14653 int nLine; /* Length of current line */
14654 int nSql = 0; /* Bytes of zSql[] used */
14655 int nAlloc = 0; /* Allocated zSql[] space */
14656 int nSqlPrior = 0; /* Bytes of zSql[] used by prior line */
14657 int rc; /* Error code */
14658 int errCnt = 0; /* Number of errors seen */
14659 int lineno = 0; /* Current line number */
14660 int startline = 0; /* Line number for start of current input */
14662 while( errCnt==0 || !bail_on_error || (in==0 && stdin_is_interactive) ){
14664 zLine = one_input_line(in, zLine, nSql>0);
14667 if( in==0 && stdin_is_interactive ) printf("\n");
14670 if( seenInterrupt ){
14675 if( nSql==0 && _all_whitespace(zLine) ){
14676 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
14679 if( zLine && zLine[0]=='.' && nSql==0 ){
14680 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zLine);
14681 rc = do_meta_command(zLine, p);
14682 if( rc==2 ){ /* exit requested */
14689 if( line_is_command_terminator(zLine) && line_is_complete(zSql, nSql) ){
14690 memcpy(zLine,";",2);
14692 nLine = strlen30(zLine);
14693 if( nSql+nLine+2>=nAlloc ){
14694 nAlloc = nSql+nLine+100;
14695 zSql = realloc(zSql, nAlloc);
14697 raw_printf(stderr, "Error: out of memory\n");
14704 for(i=0; zLine[i] && IsSpace(zLine[i]); i++){}
14705 assert( nAlloc>0 && zSql!=0 );
14706 memcpy(zSql, zLine+i, nLine+1-i);
14707 startline = lineno;
14710 zSql[nSql++] = '\n';
14711 memcpy(zSql+nSql, zLine, nLine+1);
14714 if( nSql && line_contains_semicolon(&zSql[nSqlPrior], nSql-nSqlPrior)
14715 && sqlite3_complete(zSql) ){
14716 errCnt += runOneSqlLine(p, zSql, in, startline);
14724 }else if( nSql && _all_whitespace(zSql) ){
14725 if( ShellHasFlag(p, SHFLG_Echo) ) printf("%s\n", zSql);
14729 if( nSql && !_all_whitespace(zSql) ){
14730 runOneSqlLine(p, zSql, in, startline);
14738 ** Return a pathname which is the user's home directory. A
14739 ** 0 return indicates an error of some kind.
14741 static char *find_home_dir(int clearFlag){
14742 static char *home_dir = NULL;
14748 if( home_dir ) return home_dir;
14750 #if !defined(_WIN32) && !defined(WIN32) && !defined(_WIN32_WCE) \
14751 && !defined(__RTP__) && !defined(_WRS_KERNEL)
14753 struct passwd *pwent;
14754 uid_t uid = getuid();
14755 if( (pwent=getpwuid(uid)) != NULL) {
14756 home_dir = pwent->pw_dir;
14761 #if defined(_WIN32_WCE)
14762 /* Windows CE (arm-wince-mingw32ce-gcc) does not provide getenv()
14767 #if defined(_WIN32) || defined(WIN32)
14769 home_dir = getenv("USERPROFILE");
14774 home_dir = getenv("HOME");
14777 #if defined(_WIN32) || defined(WIN32)
14779 char *zDrive, *zPath;
14781 zDrive = getenv("HOMEDRIVE");
14782 zPath = getenv("HOMEPATH");
14783 if( zDrive && zPath ){
14784 n = strlen30(zDrive) + strlen30(zPath) + 1;
14785 home_dir = malloc( n );
14786 if( home_dir==0 ) return 0;
14787 sqlite3_snprintf(n, home_dir, "%s%s", zDrive, zPath);
14794 #endif /* !_WIN32_WCE */
14797 int n = strlen30(home_dir) + 1;
14798 char *z = malloc( n );
14799 if( z ) memcpy(z, home_dir, n);
14807 ** Read input from the file given by sqliterc_override. Or if that
14808 ** parameter is NULL, take input from ~/.sqliterc
14810 ** Returns the number of errors.
14812 static void process_sqliterc(
14813 ShellState *p, /* Configuration data */
14814 const char *sqliterc_override /* Name of config file. NULL to use default */
14816 char *home_dir = NULL;
14817 const char *sqliterc = sqliterc_override;
14821 if (sqliterc == NULL) {
14822 home_dir = find_home_dir(0);
14824 raw_printf(stderr, "-- warning: cannot find home directory;"
14825 " cannot read ~/.sqliterc\n");
14828 sqlite3_initialize();
14829 zBuf = sqlite3_mprintf("%s/.sqliterc",home_dir);
14832 in = fopen(sqliterc,"rb");
14834 if( stdin_is_interactive ){
14835 utf8_printf(stderr,"-- Loading resources from %s\n",sqliterc);
14837 process_input(p,in);
14840 sqlite3_free(zBuf);
14844 ** Show available command line options
14846 static const char zOptions[] =
14847 " -ascii set output mode to 'ascii'\n"
14848 " -bail stop after hitting an error\n"
14849 " -batch force batch I/O\n"
14850 " -column set output mode to 'column'\n"
14851 " -cmd COMMAND run \"COMMAND\" before reading stdin\n"
14852 " -csv set output mode to 'csv'\n"
14853 " -echo print commands before execution\n"
14854 " -init FILENAME read/process named file\n"
14855 " -[no]header turn headers on or off\n"
14856 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
14857 " -heap SIZE Size of heap for memsys3 or memsys5\n"
14859 " -help show this message\n"
14860 " -html set output mode to HTML\n"
14861 " -interactive force interactive I/O\n"
14862 " -line set output mode to 'line'\n"
14863 " -list set output mode to 'list'\n"
14864 " -lookaside SIZE N use N entries of SZ bytes for lookaside memory\n"
14865 " -mmap N default mmap size set to N\n"
14866 #ifdef SQLITE_ENABLE_MULTIPLEX
14867 " -multiplex enable the multiplexor VFS\n"
14869 " -newline SEP set output row separator. Default: '\\n'\n"
14870 " -nullvalue TEXT set text string for NULL values. Default ''\n"
14871 " -pagecache SIZE N use N slots of SZ bytes each for page cache memory\n"
14872 " -quote set output mode to 'quote'\n"
14873 " -separator SEP set output column separator. Default: '|'\n"
14874 " -stats print memory stats before each finalize\n"
14875 " -version show SQLite version\n"
14876 " -vfs NAME use NAME as the default VFS\n"
14877 #ifdef SQLITE_ENABLE_VFSTRACE
14878 " -vfstrace enable tracing of all VFS calls\n"
14881 static void usage(int showDetail){
14882 utf8_printf(stderr,
14883 "Usage: %s [OPTIONS] FILENAME [SQL]\n"
14884 "FILENAME is the name of an SQLite database. A new database is created\n"
14885 "if the file does not previously exist.\n", Argv0);
14887 utf8_printf(stderr, "OPTIONS include:\n%s", zOptions);
14889 raw_printf(stderr, "Use the -help option for additional information\n");
14895 ** Initialize the state information in data
14897 static void main_init(ShellState *data) {
14898 memset(data, 0, sizeof(*data));
14899 data->normalMode = data->cMode = data->mode = MODE_List;
14900 data->autoExplain = 1;
14901 memcpy(data->colSeparator,SEP_Column, 2);
14902 memcpy(data->rowSeparator,SEP_Row, 2);
14903 data->showHeader = 0;
14904 data->shellFlgs = SHFLG_Lookaside;
14905 sqlite3_config(SQLITE_CONFIG_URI, 1);
14906 sqlite3_config(SQLITE_CONFIG_LOG, shellLog, data);
14907 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
14908 sqlite3_snprintf(sizeof(mainPrompt), mainPrompt,"sqlite> ");
14909 sqlite3_snprintf(sizeof(continuePrompt), continuePrompt," ...> ");
14913 ** Output text to the console in a font that attracts extra attention.
14916 static void printBold(const char *zText){
14917 HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
14918 CONSOLE_SCREEN_BUFFER_INFO defaultScreenInfo;
14919 GetConsoleScreenBufferInfo(out, &defaultScreenInfo);
14920 SetConsoleTextAttribute(out,
14921 FOREGROUND_RED|FOREGROUND_INTENSITY
14923 printf("%s", zText);
14924 SetConsoleTextAttribute(out, defaultScreenInfo.wAttributes);
14927 static void printBold(const char *zText){
14928 printf("\033[1m%s\033[0m", zText);
14933 ** Get the argument to an --option. Throw an error and die if no argument
14936 static char *cmdline_option_value(int argc, char **argv, int i){
14938 utf8_printf(stderr, "%s: Error: missing argument to %s\n",
14939 argv[0], argv[argc-1]);
14945 #ifndef SQLITE_SHELL_IS_UTF8
14946 # if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
14947 # define SQLITE_SHELL_IS_UTF8 (0)
14949 # define SQLITE_SHELL_IS_UTF8 (1)
14953 #if SQLITE_SHELL_IS_UTF8
14954 int SQLITE_CDECL main(int argc, char **argv){
14956 int SQLITE_CDECL wmain(int argc, wchar_t **wargv){
14961 const char *zInitFile = 0;
14964 int warnInmemoryDb = 0;
14969 setBinaryMode(stdin, 0);
14970 setvbuf(stderr, 0, _IONBF, 0); /* Make sure stderr is unbuffered */
14971 stdin_is_interactive = isatty(0);
14972 stdout_is_console = isatty(1);
14974 #if USE_SYSTEM_SQLITE+0!=1
14975 if( strncmp(sqlite3_sourceid(),SQLITE_SOURCE_ID,60)!=0 ){
14976 utf8_printf(stderr, "SQLite header and source version mismatch\n%s\n%s\n",
14977 sqlite3_sourceid(), SQLITE_SOURCE_ID);
14982 #if !SQLITE_SHELL_IS_UTF8
14983 sqlite3_initialize();
14984 argv = sqlite3_malloc64(sizeof(argv[0])*argc);
14986 raw_printf(stderr, "out of memory\n");
14989 for(i=0; i<argc; i++){
14990 argv[i] = sqlite3_win32_unicode_to_utf8(wargv[i]);
14992 raw_printf(stderr, "out of memory\n");
14997 assert( argc>=1 && argv && argv[0] );
15000 /* Make sure we have a valid signal handler early, before anything
15004 signal(SIGINT, interrupt_handler);
15005 #elif (defined(_WIN32) || defined(WIN32)) && !defined(_WIN32_WCE)
15006 SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
15009 #ifdef SQLITE_SHELL_DBNAME_PROC
15011 /* If the SQLITE_SHELL_DBNAME_PROC macro is defined, then it is the name
15012 ** of a C-function that will provide the name of the database file. Use
15013 ** this compile-time option to embed this shell program in larger
15014 ** applications. */
15015 extern void SQLITE_SHELL_DBNAME_PROC(const char**);
15016 SQLITE_SHELL_DBNAME_PROC(&data.zDbFilename);
15017 warnInmemoryDb = 0;
15021 /* Do an initial pass through the command-line argument to locate
15022 ** the name of the database file, the name of the initialization file,
15023 ** the size of the alternative malloc heap,
15024 ** and the first command to execute.
15026 for(i=1; i<argc; i++){
15030 if( data.zDbFilename==0 ){
15031 data.zDbFilename = z;
15033 /* Excesss arguments are interpreted as SQL (or dot-commands) and
15034 ** mean that nothing is read from stdin */
15037 azCmd = realloc(azCmd, sizeof(azCmd[0])*nCmd);
15039 raw_printf(stderr, "out of memory\n");
15045 if( z[1]=='-' ) z++;
15046 if( strcmp(z,"-separator")==0
15047 || strcmp(z,"-nullvalue")==0
15048 || strcmp(z,"-newline")==0
15049 || strcmp(z,"-cmd")==0
15051 (void)cmdline_option_value(argc, argv, ++i);
15052 }else if( strcmp(z,"-init")==0 ){
15053 zInitFile = cmdline_option_value(argc, argv, ++i);
15054 }else if( strcmp(z,"-batch")==0 ){
15055 /* Need to check for batch mode here to so we can avoid printing
15056 ** informational messages (like from process_sqliterc) before
15057 ** we do the actual processing of arguments later in a second pass.
15059 stdin_is_interactive = 0;
15060 }else if( strcmp(z,"-heap")==0 ){
15061 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
15063 sqlite3_int64 szHeap;
15065 zSize = cmdline_option_value(argc, argv, ++i);
15066 szHeap = integerValue(zSize);
15067 if( szHeap>0x7fff0000 ) szHeap = 0x7fff0000;
15068 sqlite3_config(SQLITE_CONFIG_HEAP, malloc((int)szHeap), (int)szHeap, 64);
15070 (void)cmdline_option_value(argc, argv, ++i);
15072 }else if( strcmp(z,"-pagecache")==0 ){
15074 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15075 if( sz>70000 ) sz = 70000;
15077 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15078 sqlite3_config(SQLITE_CONFIG_PAGECACHE,
15079 (n>0 && sz>0) ? malloc(n*sz) : 0, sz, n);
15080 data.shellFlgs |= SHFLG_Pagecache;
15081 }else if( strcmp(z,"-lookaside")==0 ){
15083 sz = (int)integerValue(cmdline_option_value(argc,argv,++i));
15085 n = (int)integerValue(cmdline_option_value(argc,argv,++i));
15087 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, sz, n);
15088 if( sz*n==0 ) data.shellFlgs &= ~SHFLG_Lookaside;
15089 #ifdef SQLITE_ENABLE_VFSTRACE
15090 }else if( strcmp(z,"-vfstrace")==0 ){
15091 extern int vfstrace_register(
15092 const char *zTraceName,
15093 const char *zOldVfsName,
15094 int (*xOut)(const char*,void*),
15098 vfstrace_register("trace",0,(int(*)(const char*,void*))fputs,stderr,1);
15100 #ifdef SQLITE_ENABLE_MULTIPLEX
15101 }else if( strcmp(z,"-multiplex")==0 ){
15102 extern int sqlite3_multiple_initialize(const char*,int);
15103 sqlite3_multiplex_initialize(0, 1);
15105 }else if( strcmp(z,"-mmap")==0 ){
15106 sqlite3_int64 sz = integerValue(cmdline_option_value(argc,argv,++i));
15107 sqlite3_config(SQLITE_CONFIG_MMAP_SIZE, sz, sz);
15108 }else if( strcmp(z,"-vfs")==0 ){
15109 sqlite3_vfs *pVfs = sqlite3_vfs_find(cmdline_option_value(argc,argv,++i));
15111 sqlite3_vfs_register(pVfs, 1);
15113 utf8_printf(stderr, "no such VFS: \"%s\"\n", argv[i]);
15116 #ifdef SQLITE_HAVE_ZIP
15117 }else if( strcmp(z,"-zip")==0 ){
15118 data.openMode = SHELL_OPEN_ZIPFILE;
15120 }else if( strcmp(z,"-append")==0 ){
15121 data.openMode = SHELL_OPEN_APPENDVFS;
15124 if( data.zDbFilename==0 ){
15125 #ifndef SQLITE_OMIT_MEMORYDB
15126 data.zDbFilename = ":memory:";
15127 warnInmemoryDb = argc==1;
15129 utf8_printf(stderr,"%s: Error: no database filename specified\n", Argv0);
15134 sqlite3_appendvfs_init(0,0,0);
15136 /* Go ahead and open the database file if it already exists. If the
15137 ** file does not exist, delay opening it. This prevents empty database
15138 ** files from being created if a user mistypes the database name argument
15139 ** to the sqlite command-line tool.
15141 if( access(data.zDbFilename, 0)==0 ){
15145 /* Process the initialization file if there is one. If no -init option
15146 ** is given on the command line, look for a file named ~/.sqliterc and
15147 ** try to process it.
15149 process_sqliterc(&data,zInitFile);
15151 /* Make a second pass through the command-line argument and set
15152 ** options. This second pass is delayed until after the initialization
15153 ** file is processed so that the command-line arguments will override
15154 ** settings in the initialization file.
15156 for(i=1; i<argc; i++){
15158 if( z[0]!='-' ) continue;
15159 if( z[1]=='-' ){ z++; }
15160 if( strcmp(z,"-init")==0 ){
15162 }else if( strcmp(z,"-html")==0 ){
15163 data.mode = MODE_Html;
15164 }else if( strcmp(z,"-list")==0 ){
15165 data.mode = MODE_List;
15166 }else if( strcmp(z,"-quote")==0 ){
15167 data.mode = MODE_Quote;
15168 }else if( strcmp(z,"-line")==0 ){
15169 data.mode = MODE_Line;
15170 }else if( strcmp(z,"-column")==0 ){
15171 data.mode = MODE_Column;
15172 }else if( strcmp(z,"-csv")==0 ){
15173 data.mode = MODE_Csv;
15174 memcpy(data.colSeparator,",",2);
15175 #ifdef SQLITE_HAVE_ZIP
15176 }else if( strcmp(z,"-zip")==0 ){
15177 data.openMode = SHELL_OPEN_ZIPFILE;
15179 }else if( strcmp(z,"-append")==0 ){
15180 data.openMode = SHELL_OPEN_APPENDVFS;
15181 }else if( strcmp(z,"-ascii")==0 ){
15182 data.mode = MODE_Ascii;
15183 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15185 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15187 }else if( strcmp(z,"-separator")==0 ){
15188 sqlite3_snprintf(sizeof(data.colSeparator), data.colSeparator,
15189 "%s",cmdline_option_value(argc,argv,++i));
15190 }else if( strcmp(z,"-newline")==0 ){
15191 sqlite3_snprintf(sizeof(data.rowSeparator), data.rowSeparator,
15192 "%s",cmdline_option_value(argc,argv,++i));
15193 }else if( strcmp(z,"-nullvalue")==0 ){
15194 sqlite3_snprintf(sizeof(data.nullValue), data.nullValue,
15195 "%s",cmdline_option_value(argc,argv,++i));
15196 }else if( strcmp(z,"-header")==0 ){
15197 data.showHeader = 1;
15198 }else if( strcmp(z,"-noheader")==0 ){
15199 data.showHeader = 0;
15200 }else if( strcmp(z,"-echo")==0 ){
15201 ShellSetFlag(&data, SHFLG_Echo);
15202 }else if( strcmp(z,"-eqp")==0 ){
15203 data.autoEQP = AUTOEQP_on;
15204 }else if( strcmp(z,"-eqpfull")==0 ){
15205 data.autoEQP = AUTOEQP_full;
15206 }else if( strcmp(z,"-stats")==0 ){
15208 }else if( strcmp(z,"-scanstats")==0 ){
15209 data.scanstatsOn = 1;
15210 }else if( strcmp(z,"-backslash")==0 ){
15211 /* Undocumented command-line option: -backslash
15212 ** Causes C-style backslash escapes to be evaluated in SQL statements
15213 ** prior to sending the SQL into SQLite. Useful for injecting
15214 ** crazy bytes in the middle of SQL statements for testing and debugging.
15216 ShellSetFlag(&data, SHFLG_Backslash);
15217 }else if( strcmp(z,"-bail")==0 ){
15219 }else if( strcmp(z,"-version")==0 ){
15220 printf("%s %s\n", sqlite3_libversion(), sqlite3_sourceid());
15222 }else if( strcmp(z,"-interactive")==0 ){
15223 stdin_is_interactive = 1;
15224 }else if( strcmp(z,"-batch")==0 ){
15225 stdin_is_interactive = 0;
15226 }else if( strcmp(z,"-heap")==0 ){
15228 }else if( strcmp(z,"-pagecache")==0 ){
15230 }else if( strcmp(z,"-lookaside")==0 ){
15232 }else if( strcmp(z,"-mmap")==0 ){
15234 }else if( strcmp(z,"-vfs")==0 ){
15236 #ifdef SQLITE_ENABLE_VFSTRACE
15237 }else if( strcmp(z,"-vfstrace")==0 ){
15240 #ifdef SQLITE_ENABLE_MULTIPLEX
15241 }else if( strcmp(z,"-multiplex")==0 ){
15244 }else if( strcmp(z,"-help")==0 ){
15246 }else if( strcmp(z,"-cmd")==0 ){
15247 /* Run commands that follow -cmd first and separately from commands
15248 ** that simply appear on the command-line. This seems goofy. It would
15249 ** be better if all commands ran in the order that they appear. But
15250 ** we retain the goofy behavior for historical compatibility. */
15251 if( i==argc-1 ) break;
15252 z = cmdline_option_value(argc,argv,++i);
15254 rc = do_meta_command(z, &data);
15255 if( rc && bail_on_error ) return rc==2 ? 0 : rc;
15258 rc = shell_exec(data.db, z, shell_callback, &data, &zErrMsg);
15260 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15261 if( bail_on_error ) return rc!=0 ? rc : 1;
15263 utf8_printf(stderr,"Error: unable to process SQL \"%s\"\n", z);
15264 if( bail_on_error ) return rc;
15268 utf8_printf(stderr,"%s: Error: unknown option: %s\n", Argv0, z);
15269 raw_printf(stderr,"Use -help for a list of options.\n");
15272 data.cMode = data.mode;
15276 /* Run all arguments that do not begin with '-' as if they were separate
15277 ** command-line inputs, except for the argToSkip argument which contains
15278 ** the database filename.
15280 for(i=0; i<nCmd; i++){
15281 if( azCmd[i][0]=='.' ){
15282 rc = do_meta_command(azCmd[i], &data);
15283 if( rc ) return rc==2 ? 0 : rc;
15286 rc = shell_exec(data.db, azCmd[i], shell_callback, &data, &zErrMsg);
15288 utf8_printf(stderr,"Error: %s\n", zErrMsg);
15289 return rc!=0 ? rc : 1;
15291 utf8_printf(stderr,"Error: unable to process SQL: %s\n", azCmd[i]);
15298 /* Run commands received from standard input
15300 if( stdin_is_interactive ){
15302 char *zHistory = 0;
15305 "SQLite version %s %.19s\n" /*extra-version-info*/
15306 "Enter \".help\" for usage hints.\n",
15307 sqlite3_libversion(), sqlite3_sourceid()
15309 if( warnInmemoryDb ){
15310 printf("Connected to a ");
15311 printBold("transient in-memory database");
15312 printf(".\nUse \".open FILENAME\" to reopen on a "
15313 "persistent database.\n");
15315 zHome = find_home_dir(0);
15317 nHistory = strlen30(zHome) + 20;
15318 if( (zHistory = malloc(nHistory))!=0 ){
15319 sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);
15322 if( zHistory ){ shell_read_history(zHistory); }
15323 #if HAVE_READLINE || HAVE_EDITLINE
15324 rl_attempted_completion_function = readline_completion;
15325 #elif HAVE_LINENOISE
15326 linenoiseSetCompletionCallback(linenoise_completion);
15328 rc = process_input(&data, 0);
15330 shell_stifle_history(2000);
15331 shell_write_history(zHistory);
15335 rc = process_input(&data, stdin);
15338 set_table_name(&data, 0);
15340 session_close_all(&data);
15341 sqlite3_close(data.db);
15343 sqlite3_free(data.zFreeOnClose);
15345 output_reset(&data);
15346 data.doXdgOpen = 0;
15347 clearTempFile(&data);
15348 #if !SQLITE_SHELL_IS_UTF8
15349 for(i=0; i<argc; i++) sqlite3_free(argv[i]);
15350 sqlite3_free(argv);